xref: /freebsd/sys/dev/usb/wlan/if_zyd.c (revision ca48e43ba9ee73a07cdbad8365117793b01273bb)
102ac6454SAndrew Thompson /*	$OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $	*/
202ac6454SAndrew Thompson /*	$NetBSD: if_zyd.c,v 1.7 2007/06/21 04:04:29 kiyohara Exp $	*/
302ac6454SAndrew Thompson 
402ac6454SAndrew Thompson /*-
502ac6454SAndrew Thompson  * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr>
602ac6454SAndrew Thompson  * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de>
702ac6454SAndrew Thompson  *
802ac6454SAndrew Thompson  * Permission to use, copy, modify, and distribute this software for any
902ac6454SAndrew Thompson  * purpose with or without fee is hereby granted, provided that the above
1002ac6454SAndrew Thompson  * copyright notice and this permission notice appear in all copies.
1102ac6454SAndrew Thompson  *
1202ac6454SAndrew Thompson  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1302ac6454SAndrew Thompson  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1402ac6454SAndrew Thompson  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1502ac6454SAndrew Thompson  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1602ac6454SAndrew Thompson  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1702ac6454SAndrew Thompson  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1802ac6454SAndrew Thompson  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1902ac6454SAndrew Thompson  */
2002ac6454SAndrew Thompson 
2102ac6454SAndrew Thompson /*
2202ac6454SAndrew Thompson  * ZyDAS ZD1211/ZD1211B USB WLAN driver.
2302ac6454SAndrew Thompson  */
2402ac6454SAndrew Thompson 
2563a55eabSAndriy Voskoboinyk #include "opt_wlan.h"
2663a55eabSAndriy Voskoboinyk 
275efea30fSAndrew Thompson #include <sys/param.h>
285efea30fSAndrew Thompson #include <sys/sockio.h>
295efea30fSAndrew Thompson #include <sys/sysctl.h>
305efea30fSAndrew Thompson #include <sys/lock.h>
315efea30fSAndrew Thompson #include <sys/mutex.h>
32ed6d949aSAndrew Thompson #include <sys/condvar.h>
335efea30fSAndrew Thompson #include <sys/mbuf.h>
345efea30fSAndrew Thompson #include <sys/kernel.h>
355efea30fSAndrew Thompson #include <sys/socket.h>
365efea30fSAndrew Thompson #include <sys/systm.h>
375efea30fSAndrew Thompson #include <sys/malloc.h>
385efea30fSAndrew Thompson #include <sys/module.h>
395efea30fSAndrew Thompson #include <sys/bus.h>
405efea30fSAndrew Thompson #include <sys/endian.h>
415efea30fSAndrew Thompson #include <sys/kdb.h>
4202ac6454SAndrew Thompson 
435efea30fSAndrew Thompson #include <net/bpf.h>
445efea30fSAndrew Thompson #include <net/if.h>
4576039bc8SGleb Smirnoff #include <net/if_var.h>
465efea30fSAndrew Thompson #include <net/if_arp.h>
475efea30fSAndrew Thompson #include <net/ethernet.h>
485efea30fSAndrew Thompson #include <net/if_dl.h>
495efea30fSAndrew Thompson #include <net/if_media.h>
505efea30fSAndrew Thompson #include <net/if_types.h>
515efea30fSAndrew Thompson 
525efea30fSAndrew Thompson #ifdef INET
535efea30fSAndrew Thompson #include <netinet/in.h>
545efea30fSAndrew Thompson #include <netinet/in_systm.h>
555efea30fSAndrew Thompson #include <netinet/in_var.h>
565efea30fSAndrew Thompson #include <netinet/if_ether.h>
575efea30fSAndrew Thompson #include <netinet/ip.h>
585efea30fSAndrew Thompson #endif
595efea30fSAndrew Thompson 
605efea30fSAndrew Thompson #include <net80211/ieee80211_var.h>
615efea30fSAndrew Thompson #include <net80211/ieee80211_regdomain.h>
625efea30fSAndrew Thompson #include <net80211/ieee80211_radiotap.h>
63b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h>
645efea30fSAndrew Thompson 
655efea30fSAndrew Thompson #include <dev/usb/usb.h>
66ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
67ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
685efea30fSAndrew Thompson #include "usbdevs.h"
6902ac6454SAndrew Thompson 
7002ac6454SAndrew Thompson #include <dev/usb/wlan/if_zydreg.h>
7102ac6454SAndrew Thompson #include <dev/usb/wlan/if_zydfw.h>
7202ac6454SAndrew Thompson 
73b850ecc1SAndrew Thompson #ifdef USB_DEBUG
7402ac6454SAndrew Thompson static int zyd_debug = 0;
7502ac6454SAndrew Thompson 
76f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, zyd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
77f8d2b1f3SPawel Biernacki     "USB zyd");
78ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_zyd, OID_AUTO, debug, CTLFLAG_RWTUN, &zyd_debug, 0,
7902ac6454SAndrew Thompson     "zyd debug level");
8002ac6454SAndrew Thompson 
8102ac6454SAndrew Thompson enum {
8202ac6454SAndrew Thompson 	ZYD_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
8302ac6454SAndrew Thompson 	ZYD_DEBUG_RECV		= 0x00000002,	/* basic recv operation */
8402ac6454SAndrew Thompson 	ZYD_DEBUG_RESET		= 0x00000004,	/* reset processing */
8502ac6454SAndrew Thompson 	ZYD_DEBUG_INIT		= 0x00000008,	/* device init */
8602ac6454SAndrew Thompson 	ZYD_DEBUG_TX_PROC	= 0x00000010,	/* tx ISR proc */
8702ac6454SAndrew Thompson 	ZYD_DEBUG_RX_PROC	= 0x00000020,	/* rx ISR proc */
8802ac6454SAndrew Thompson 	ZYD_DEBUG_STATE		= 0x00000040,	/* 802.11 state transitions */
8902ac6454SAndrew Thompson 	ZYD_DEBUG_STAT		= 0x00000080,	/* statistic */
9002ac6454SAndrew Thompson 	ZYD_DEBUG_FW		= 0x00000100,	/* firmware */
9102ac6454SAndrew Thompson 	ZYD_DEBUG_CMD		= 0x00000200,	/* fw commands */
9202ac6454SAndrew Thompson 	ZYD_DEBUG_ANY		= 0xffffffff
9302ac6454SAndrew Thompson };
9402ac6454SAndrew Thompson #define	DPRINTF(sc, m, fmt, ...) do {				\
9502ac6454SAndrew Thompson 	if (zyd_debug & (m))					\
9602ac6454SAndrew Thompson 		printf("%s: " fmt, __func__, ## __VA_ARGS__);	\
9702ac6454SAndrew Thompson } while (0)
9802ac6454SAndrew Thompson #else
9902ac6454SAndrew Thompson #define	DPRINTF(sc, m, fmt, ...) do {				\
10002ac6454SAndrew Thompson 	(void) sc;						\
10102ac6454SAndrew Thompson } while (0)
10202ac6454SAndrew Thompson #endif
10302ac6454SAndrew Thompson 
10402ac6454SAndrew Thompson #define	zyd_do_request(sc,req,data) \
105a593f6b8SAndrew Thompson     usbd_do_request_flags((sc)->sc_udev, &(sc)->sc_mtx, req, data, 0, NULL, 5000)
10602ac6454SAndrew Thompson 
10702ac6454SAndrew Thompson static device_probe_t zyd_match;
10802ac6454SAndrew Thompson static device_attach_t zyd_attach;
10902ac6454SAndrew Thompson static device_detach_t zyd_detach;
11002ac6454SAndrew Thompson 
111e0a69b51SAndrew Thompson static usb_callback_t zyd_intr_read_callback;
112e0a69b51SAndrew Thompson static usb_callback_t zyd_intr_write_callback;
113e0a69b51SAndrew Thompson static usb_callback_t zyd_bulk_read_callback;
114e0a69b51SAndrew Thompson static usb_callback_t zyd_bulk_write_callback;
11502ac6454SAndrew Thompson 
11602ac6454SAndrew Thompson static struct ieee80211vap *zyd_vap_create(struct ieee80211com *,
117fcd9500fSBernhard Schmidt 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
118fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN],
119fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN]);
12002ac6454SAndrew Thompson static void	zyd_vap_delete(struct ieee80211vap *);
12102ac6454SAndrew Thompson static void	zyd_tx_free(struct zyd_tx_data *, int);
12202ac6454SAndrew Thompson static void	zyd_setup_tx_list(struct zyd_softc *);
12302ac6454SAndrew Thompson static void	zyd_unsetup_tx_list(struct zyd_softc *);
12402ac6454SAndrew Thompson static int	zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int);
12502ac6454SAndrew Thompson static int	zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
12602ac6454SAndrew Thompson 		    void *, int, int);
12702ac6454SAndrew Thompson static int	zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
12802ac6454SAndrew Thompson static int	zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
12902ac6454SAndrew Thompson static int	zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
13002ac6454SAndrew Thompson static int	zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
13102ac6454SAndrew Thompson static int	zyd_rfwrite(struct zyd_softc *, uint32_t);
13202ac6454SAndrew Thompson static int	zyd_lock_phy(struct zyd_softc *);
13302ac6454SAndrew Thompson static int	zyd_unlock_phy(struct zyd_softc *);
13402ac6454SAndrew Thompson static int	zyd_rf_attach(struct zyd_softc *, uint8_t);
13502ac6454SAndrew Thompson static const char *zyd_rf_name(uint8_t);
13602ac6454SAndrew Thompson static int	zyd_hw_init(struct zyd_softc *);
13702ac6454SAndrew Thompson static int	zyd_read_pod(struct zyd_softc *);
13802ac6454SAndrew Thompson static int	zyd_read_eeprom(struct zyd_softc *);
13902ac6454SAndrew Thompson static int	zyd_get_macaddr(struct zyd_softc *);
14002ac6454SAndrew Thompson static int	zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
14102ac6454SAndrew Thompson static int	zyd_set_bssid(struct zyd_softc *, const uint8_t *);
14202ac6454SAndrew Thompson static int	zyd_switch_radio(struct zyd_softc *, int);
14302ac6454SAndrew Thompson static int	zyd_set_led(struct zyd_softc *, int, int);
14402ac6454SAndrew Thompson static void	zyd_set_multi(struct zyd_softc *);
145272f6adeSGleb Smirnoff static void	zyd_update_mcast(struct ieee80211com *);
14602ac6454SAndrew Thompson static int	zyd_set_rxfilter(struct zyd_softc *);
14702ac6454SAndrew Thompson static void	zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
14802ac6454SAndrew Thompson static int	zyd_set_beacon_interval(struct zyd_softc *, int);
149760bc48eSAndrew Thompson static void	zyd_rx_data(struct usb_xfer *, int, uint16_t);
150e87b19e3SWeongyo Jeong static int	zyd_tx_start(struct zyd_softc *, struct mbuf *,
15102ac6454SAndrew Thompson 		    struct ieee80211_node *);
1527a79cebfSGleb Smirnoff static int	zyd_transmit(struct ieee80211com *, struct mbuf *);
1537a79cebfSGleb Smirnoff static void	zyd_start(struct zyd_softc *);
15402ac6454SAndrew Thompson static int	zyd_raw_xmit(struct ieee80211_node *, struct mbuf *,
15502ac6454SAndrew Thompson 		    const struct ieee80211_bpf_params *);
1567a79cebfSGleb Smirnoff static void	zyd_parent(struct ieee80211com *);
1575efea30fSAndrew Thompson static void	zyd_init_locked(struct zyd_softc *);
1585efea30fSAndrew Thompson static void	zyd_stop(struct zyd_softc *);
15902ac6454SAndrew Thompson static int	zyd_loadfirmware(struct zyd_softc *);
16002ac6454SAndrew Thompson static void	zyd_scan_start(struct ieee80211com *);
16102ac6454SAndrew Thompson static void	zyd_scan_end(struct ieee80211com *);
1625a62dab1SAndriy Voskoboinyk static void	zyd_getradiocaps(struct ieee80211com *, int, int *,
1635a62dab1SAndriy Voskoboinyk 		    struct ieee80211_channel[]);
16402ac6454SAndrew Thompson static void	zyd_set_channel(struct ieee80211com *);
16502ac6454SAndrew Thompson static int	zyd_rfmd_init(struct zyd_rf *);
16602ac6454SAndrew Thompson static int	zyd_rfmd_switch_radio(struct zyd_rf *, int);
16702ac6454SAndrew Thompson static int	zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
16802ac6454SAndrew Thompson static int	zyd_al2230_init(struct zyd_rf *);
16902ac6454SAndrew Thompson static int	zyd_al2230_switch_radio(struct zyd_rf *, int);
17002ac6454SAndrew Thompson static int	zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
17102ac6454SAndrew Thompson static int	zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t);
17202ac6454SAndrew Thompson static int	zyd_al2230_init_b(struct zyd_rf *);
17302ac6454SAndrew Thompson static int	zyd_al7230B_init(struct zyd_rf *);
17402ac6454SAndrew Thompson static int	zyd_al7230B_switch_radio(struct zyd_rf *, int);
17502ac6454SAndrew Thompson static int	zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
17602ac6454SAndrew Thompson static int	zyd_al2210_init(struct zyd_rf *);
17702ac6454SAndrew Thompson static int	zyd_al2210_switch_radio(struct zyd_rf *, int);
17802ac6454SAndrew Thompson static int	zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
17902ac6454SAndrew Thompson static int	zyd_gct_init(struct zyd_rf *);
18002ac6454SAndrew Thompson static int	zyd_gct_switch_radio(struct zyd_rf *, int);
18102ac6454SAndrew Thompson static int	zyd_gct_set_channel(struct zyd_rf *, uint8_t);
18278cc4bbbSWeongyo Jeong static int	zyd_gct_mode(struct zyd_rf *);
18378cc4bbbSWeongyo Jeong static int	zyd_gct_set_channel_synth(struct zyd_rf *, int, int);
18478cc4bbbSWeongyo Jeong static int	zyd_gct_write(struct zyd_rf *, uint16_t);
18578cc4bbbSWeongyo Jeong static int	zyd_gct_txgain(struct zyd_rf *, uint8_t);
18602ac6454SAndrew Thompson static int	zyd_maxim2_init(struct zyd_rf *);
18702ac6454SAndrew Thompson static int	zyd_maxim2_switch_radio(struct zyd_rf *, int);
18802ac6454SAndrew Thompson static int	zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
18902ac6454SAndrew Thompson 
19002ac6454SAndrew Thompson static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
19102ac6454SAndrew Thompson static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
19202ac6454SAndrew Thompson 
19302ac6454SAndrew Thompson /* various supported device vendors/products */
19402ac6454SAndrew Thompson #define ZYD_ZD1211	0
19502ac6454SAndrew Thompson #define ZYD_ZD1211B	1
19602ac6454SAndrew Thompson 
1979dde689cSWeongyo Jeong #define	ZYD_ZD1211_DEV(v,p)	\
1989dde689cSWeongyo Jeong 	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211) }
1999dde689cSWeongyo Jeong #define	ZYD_ZD1211B_DEV(v,p)	\
2009dde689cSWeongyo Jeong 	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211B) }
201f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID zyd_devs[] = {
20202ac6454SAndrew Thompson 	/* ZYD_ZD1211 */
2039dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
2049dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ABOCOM, WL54),
2059dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ASUS, WL159G),
2069dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
2079dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
2089dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(PLANEX2, GWUS54GD),
2099dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
2109dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
2119dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
2129dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(SAGEM, XG760A),
2139dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(SENAO, NUB8301),
2149dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(SITECOMEU, WL113),
2159dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(SWEEX, ZD1211),
2169dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
2179dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
2189dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
2199dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(TWINMOS, G240),
2209dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
2219dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
2229dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
2239dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
2249dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ZCOM, ZD1211),
2259dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ZYDAS, ZD1211),
2269dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ZYXEL, AG225H),
2279dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
2289dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ZYXEL, G200V2),
22902ac6454SAndrew Thompson 	/* ZYD_ZD1211B */
23016169457SGavin Atkinson 	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG_NF),
2319dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
2329dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
2339dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ASUS, A9T_WIFI),
2349dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(BELKIN, F5D7050_V4000),
2359dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
2369dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
2379dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
2389dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(MELCO, KG54L),
2399dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
2409dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(PLANEX2, GW_US54GXS),
2419dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(SAGEM, XG76NA),
2429dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
2439dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
2449dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(USR, USR5423),
2459dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(VTECH, ZD1211B),
2469dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
2479dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
2489dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ZYXEL, M202),
2499dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ZYXEL, G202),
2509dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ZYXEL, G220V2)
25102ac6454SAndrew Thompson };
25202ac6454SAndrew Thompson 
253760bc48eSAndrew Thompson static const struct usb_config zyd_config[ZYD_N_TRANSFER] = {
25402ac6454SAndrew Thompson 	[ZYD_BULK_WR] = {
25502ac6454SAndrew Thompson 		.type = UE_BULK,
25602ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
25702ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2584eae601eSAndrew Thompson 		.bufsize = ZYD_MAX_TXBUFSZ,
2594eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2604eae601eSAndrew Thompson 		.callback = zyd_bulk_write_callback,
26102ac6454SAndrew Thompson 		.ep_index = 0,
2624eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
26302ac6454SAndrew Thompson 	},
26402ac6454SAndrew Thompson 	[ZYD_BULK_RD] = {
26502ac6454SAndrew Thompson 		.type = UE_BULK,
26602ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
26702ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2684eae601eSAndrew Thompson 		.bufsize = ZYX_MAX_RXBUFSZ,
2694eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2704eae601eSAndrew Thompson 		.callback = zyd_bulk_read_callback,
27102ac6454SAndrew Thompson 		.ep_index = 0,
27202ac6454SAndrew Thompson 	},
27302ac6454SAndrew Thompson 	[ZYD_INTR_WR] = {
27402ac6454SAndrew Thompson 		.type = UE_BULK_INTR,
27502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
27602ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2774eae601eSAndrew Thompson 		.bufsize = sizeof(struct zyd_cmd),
2784eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2794eae601eSAndrew Thompson 		.callback = zyd_intr_write_callback,
2804eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
28102ac6454SAndrew Thompson 		.ep_index = 1,
28202ac6454SAndrew Thompson 	},
28302ac6454SAndrew Thompson 	[ZYD_INTR_RD] = {
28402ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
28502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
28602ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2874eae601eSAndrew Thompson 		.bufsize = sizeof(struct zyd_cmd),
2884eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2894eae601eSAndrew Thompson 		.callback = zyd_intr_read_callback,
29002ac6454SAndrew Thompson 	},
29102ac6454SAndrew Thompson };
29202ac6454SAndrew Thompson #define zyd_read16_m(sc, val, data)	do {				\
29302ac6454SAndrew Thompson 	error = zyd_read16(sc, val, data);				\
29402ac6454SAndrew Thompson 	if (error != 0)							\
29502ac6454SAndrew Thompson 		goto fail;						\
29602ac6454SAndrew Thompson } while (0)
29702ac6454SAndrew Thompson #define zyd_write16_m(sc, val, data)	do {				\
29802ac6454SAndrew Thompson 	error = zyd_write16(sc, val, data);				\
29902ac6454SAndrew Thompson 	if (error != 0)							\
30002ac6454SAndrew Thompson 		goto fail;						\
30102ac6454SAndrew Thompson } while (0)
30202ac6454SAndrew Thompson #define zyd_read32_m(sc, val, data)	do {				\
30302ac6454SAndrew Thompson 	error = zyd_read32(sc, val, data);				\
30402ac6454SAndrew Thompson 	if (error != 0)							\
30502ac6454SAndrew Thompson 		goto fail;						\
30602ac6454SAndrew Thompson } while (0)
30702ac6454SAndrew Thompson #define zyd_write32_m(sc, val, data)	do {				\
30802ac6454SAndrew Thompson 	error = zyd_write32(sc, val, data);				\
30902ac6454SAndrew Thompson 	if (error != 0)							\
31002ac6454SAndrew Thompson 		goto fail;						\
31102ac6454SAndrew Thompson } while (0)
31202ac6454SAndrew Thompson 
31302ac6454SAndrew Thompson static int
zyd_match(device_t dev)31402ac6454SAndrew Thompson zyd_match(device_t dev)
31502ac6454SAndrew Thompson {
316760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
31702ac6454SAndrew Thompson 
318f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST)
31902ac6454SAndrew Thompson 		return (ENXIO);
32002ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX)
32102ac6454SAndrew Thompson 		return (ENXIO);
32202ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX)
32302ac6454SAndrew Thompson 		return (ENXIO);
32402ac6454SAndrew Thompson 
325a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa));
32602ac6454SAndrew Thompson }
32702ac6454SAndrew Thompson 
32802ac6454SAndrew Thompson static int
zyd_attach(device_t dev)32902ac6454SAndrew Thompson zyd_attach(device_t dev)
33002ac6454SAndrew Thompson {
331760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
33202ac6454SAndrew Thompson 	struct zyd_softc *sc = device_get_softc(dev);
3337a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3340046e186SAndriy Voskoboinyk 	uint8_t iface_index;
33502ac6454SAndrew Thompson 	int error;
33602ac6454SAndrew Thompson 
33702ac6454SAndrew Thompson 	if (uaa->info.bcdDevice < 0x4330) {
33802ac6454SAndrew Thompson 		device_printf(dev, "device version mismatch: 0x%X "
33902ac6454SAndrew Thompson 		    "(only >= 43.30 supported)\n",
34002ac6454SAndrew Thompson 		    uaa->info.bcdDevice);
34102ac6454SAndrew Thompson 		return (EINVAL);
34202ac6454SAndrew Thompson 	}
34302ac6454SAndrew Thompson 
344a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
34502ac6454SAndrew Thompson 	sc->sc_dev = dev;
34602ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
34702ac6454SAndrew Thompson 	sc->sc_macrev = USB_GET_DRIVER_INFO(uaa);
34802ac6454SAndrew Thompson 
34902ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
35002ac6454SAndrew Thompson 	    MTX_NETWORK_LOCK, MTX_DEF);
35102ac6454SAndrew Thompson 	STAILQ_INIT(&sc->sc_rqh);
3527a79cebfSGleb Smirnoff 	mbufq_init(&sc->sc_snd, ifqmaxlen);
35302ac6454SAndrew Thompson 
35402ac6454SAndrew Thompson 	iface_index = ZYD_IFACE_INDEX;
355a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
35602ac6454SAndrew Thompson 	    &iface_index, sc->sc_xfer, zyd_config,
35702ac6454SAndrew Thompson 	    ZYD_N_TRANSFER, sc, &sc->sc_mtx);
35802ac6454SAndrew Thompson 	if (error) {
35902ac6454SAndrew Thompson 		device_printf(dev, "could not allocate USB transfers, "
360a593f6b8SAndrew Thompson 		    "err=%s\n", usbd_errstr(error));
36102ac6454SAndrew Thompson 		goto detach;
36202ac6454SAndrew Thompson 	}
36302ac6454SAndrew Thompson 
36402ac6454SAndrew Thompson 	ZYD_LOCK(sc);
36502ac6454SAndrew Thompson 	if ((error = zyd_get_macaddr(sc)) != 0) {
36602ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM\n");
3675efea30fSAndrew Thompson 		ZYD_UNLOCK(sc);
3685efea30fSAndrew Thompson 		goto detach;
36902ac6454SAndrew Thompson 	}
37002ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
37102ac6454SAndrew Thompson 
37259686fe9SGleb Smirnoff 	ic->ic_softc = sc;
373c8550c02SGleb Smirnoff 	ic->ic_name = device_get_nameunit(dev);
37402ac6454SAndrew Thompson 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
37502ac6454SAndrew Thompson 	ic->ic_opmode = IEEE80211_M_STA;
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 	/* set device capabilities */
37802ac6454SAndrew Thompson 	ic->ic_caps =
37902ac6454SAndrew Thompson 		  IEEE80211_C_STA		/* station mode */
38002ac6454SAndrew Thompson 		| IEEE80211_C_MONITOR		/* monitor mode */
38102ac6454SAndrew Thompson 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
38202ac6454SAndrew Thompson 	        | IEEE80211_C_SHSLOT		/* short slot time supported */
38302ac6454SAndrew Thompson 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
38402ac6454SAndrew Thompson 	        | IEEE80211_C_WPA		/* 802.11i */
38502ac6454SAndrew Thompson 		;
38602ac6454SAndrew Thompson 
3875a62dab1SAndriy Voskoboinyk 	zyd_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
3885a62dab1SAndriy Voskoboinyk 	    ic->ic_channels);
38902ac6454SAndrew Thompson 
3907a79cebfSGleb Smirnoff 	ieee80211_ifattach(ic);
39102ac6454SAndrew Thompson 	ic->ic_raw_xmit = zyd_raw_xmit;
39202ac6454SAndrew Thompson 	ic->ic_scan_start = zyd_scan_start;
39302ac6454SAndrew Thompson 	ic->ic_scan_end = zyd_scan_end;
3945a62dab1SAndriy Voskoboinyk 	ic->ic_getradiocaps = zyd_getradiocaps;
39502ac6454SAndrew Thompson 	ic->ic_set_channel = zyd_set_channel;
39602ac6454SAndrew Thompson 	ic->ic_vap_create = zyd_vap_create;
39702ac6454SAndrew Thompson 	ic->ic_vap_delete = zyd_vap_delete;
39802ac6454SAndrew Thompson 	ic->ic_update_mcast = zyd_update_mcast;
39902ac6454SAndrew Thompson 	ic->ic_update_promisc = zyd_update_mcast;
4007a79cebfSGleb Smirnoff 	ic->ic_parent = zyd_parent;
4017a79cebfSGleb Smirnoff 	ic->ic_transmit = zyd_transmit;
40202ac6454SAndrew Thompson 
4035463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
4045463c4a4SSam Leffler 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
4055463c4a4SSam Leffler 		ZYD_TX_RADIOTAP_PRESENT,
4065463c4a4SSam Leffler 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
4075463c4a4SSam Leffler 		ZYD_RX_RADIOTAP_PRESENT);
40802ac6454SAndrew Thompson 
40902ac6454SAndrew Thompson 	if (bootverbose)
41002ac6454SAndrew Thompson 		ieee80211_announce(ic);
41102ac6454SAndrew Thompson 
4125efea30fSAndrew Thompson 	return (0);
4135efea30fSAndrew Thompson 
4145efea30fSAndrew Thompson detach:
4155efea30fSAndrew Thompson 	zyd_detach(dev);
4165efea30fSAndrew Thompson 	return (ENXIO);			/* failure */
41702ac6454SAndrew Thompson }
41802ac6454SAndrew Thompson 
41932745980SAdrian Chadd static void
zyd_drain_mbufq(struct zyd_softc * sc)42032745980SAdrian Chadd zyd_drain_mbufq(struct zyd_softc *sc)
42132745980SAdrian Chadd {
42232745980SAdrian Chadd 	struct mbuf *m;
42332745980SAdrian Chadd 	struct ieee80211_node *ni;
42432745980SAdrian Chadd 
42532745980SAdrian Chadd 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
42632745980SAdrian Chadd 	while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
42732745980SAdrian Chadd 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
42832745980SAdrian Chadd 		m->m_pkthdr.rcvif = NULL;
42932745980SAdrian Chadd 		ieee80211_free_node(ni);
43032745980SAdrian Chadd 		m_freem(m);
43132745980SAdrian Chadd 	}
43232745980SAdrian Chadd }
43332745980SAdrian Chadd 
43402ac6454SAndrew Thompson static int
zyd_detach(device_t dev)43502ac6454SAndrew Thompson zyd_detach(device_t dev)
43602ac6454SAndrew Thompson {
43702ac6454SAndrew Thompson 	struct zyd_softc *sc = device_get_softc(dev);
4387a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
43962d42655SHans Petter Selasky 	unsigned x;
44002ac6454SAndrew Thompson 
441645e4d17SHans Petter Selasky 	/*
442645e4d17SHans Petter Selasky 	 * Prevent further allocations from RX/TX data
443645e4d17SHans Petter Selasky 	 * lists and ioctls:
444645e4d17SHans Petter Selasky 	 */
445645e4d17SHans Petter Selasky 	ZYD_LOCK(sc);
446645e4d17SHans Petter Selasky 	sc->sc_flags |= ZYD_FLAG_DETACHED;
44732745980SAdrian Chadd 	zyd_drain_mbufq(sc);
448645e4d17SHans Petter Selasky 	STAILQ_INIT(&sc->tx_q);
449645e4d17SHans Petter Selasky 	STAILQ_INIT(&sc->tx_free);
450645e4d17SHans Petter Selasky 	ZYD_UNLOCK(sc);
451645e4d17SHans Petter Selasky 
452645e4d17SHans Petter Selasky 	/* drain USB transfers */
453645e4d17SHans Petter Selasky 	for (x = 0; x != ZYD_N_TRANSFER; x++)
454645e4d17SHans Petter Selasky 		usbd_transfer_drain(sc->sc_xfer[x]);
45502ac6454SAndrew Thompson 
45602ac6454SAndrew Thompson 	/* free TX list, if any */
457645e4d17SHans Petter Selasky 	ZYD_LOCK(sc);
45802ac6454SAndrew Thompson 	zyd_unsetup_tx_list(sc);
459645e4d17SHans Petter Selasky 	ZYD_UNLOCK(sc);
460645e4d17SHans Petter Selasky 
461645e4d17SHans Petter Selasky 	/* free USB transfers and some data buffers */
462645e4d17SHans Petter Selasky 	usbd_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER);
46302ac6454SAndrew Thompson 
4647a79cebfSGleb Smirnoff 	if (ic->ic_softc == sc)
46502ac6454SAndrew Thompson 		ieee80211_ifdetach(ic);
46602ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
46702ac6454SAndrew Thompson 
46802ac6454SAndrew Thompson 	return (0);
46902ac6454SAndrew Thompson }
47002ac6454SAndrew Thompson 
47102ac6454SAndrew Thompson static struct ieee80211vap *
zyd_vap_create(struct ieee80211com * ic,const char name[IFNAMSIZ],int unit,enum ieee80211_opmode opmode,int flags,const uint8_t bssid[IEEE80211_ADDR_LEN],const uint8_t mac[IEEE80211_ADDR_LEN])472fcd9500fSBernhard Schmidt zyd_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
473fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
47402ac6454SAndrew Thompson     const uint8_t bssid[IEEE80211_ADDR_LEN],
47502ac6454SAndrew Thompson     const uint8_t mac[IEEE80211_ADDR_LEN])
47602ac6454SAndrew Thompson {
47702ac6454SAndrew Thompson 	struct zyd_vap *zvp;
47802ac6454SAndrew Thompson 	struct ieee80211vap *vap;
47902ac6454SAndrew Thompson 
48002ac6454SAndrew Thompson 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
48102ac6454SAndrew Thompson 		return (NULL);
4827a79cebfSGleb Smirnoff 	zvp = malloc(sizeof(struct zyd_vap), M_80211_VAP, M_WAITOK | M_ZERO);
48302ac6454SAndrew Thompson 	vap = &zvp->vap;
484bb2f69e8SHans Petter Selasky 
48502ac6454SAndrew Thompson 	/* enable s/w bmiss handling for sta mode */
486bb2f69e8SHans Petter Selasky 	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
4877a79cebfSGleb Smirnoff 	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
488bb2f69e8SHans Petter Selasky 		/* out of memory */
489bb2f69e8SHans Petter Selasky 		free(zvp, M_80211_VAP);
490bb2f69e8SHans Petter Selasky 		return (NULL);
491bb2f69e8SHans Petter Selasky 	}
49202ac6454SAndrew Thompson 
49302ac6454SAndrew Thompson 	/* override state transition machine */
49402ac6454SAndrew Thompson 	zvp->newstate = vap->iv_newstate;
49502ac6454SAndrew Thompson 	vap->iv_newstate = zyd_newstate;
49602ac6454SAndrew Thompson 
497b6108616SRui Paulo 	ieee80211_ratectl_init(vap);
498b6108616SRui Paulo 	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
49902ac6454SAndrew Thompson 
50002ac6454SAndrew Thompson 	/* complete setup */
50102ac6454SAndrew Thompson 	ieee80211_vap_attach(vap, ieee80211_media_change,
5027a79cebfSGleb Smirnoff 	    ieee80211_media_status, mac);
50302ac6454SAndrew Thompson 	ic->ic_opmode = opmode;
50402ac6454SAndrew Thompson 	return (vap);
50502ac6454SAndrew Thompson }
50602ac6454SAndrew Thompson 
50702ac6454SAndrew Thompson static void
zyd_vap_delete(struct ieee80211vap * vap)50802ac6454SAndrew Thompson zyd_vap_delete(struct ieee80211vap *vap)
50902ac6454SAndrew Thompson {
51002ac6454SAndrew Thompson 	struct zyd_vap *zvp = ZYD_VAP(vap);
51102ac6454SAndrew Thompson 
512b6108616SRui Paulo 	ieee80211_ratectl_deinit(vap);
51302ac6454SAndrew Thompson 	ieee80211_vap_detach(vap);
51402ac6454SAndrew Thompson 	free(zvp, M_80211_VAP);
51502ac6454SAndrew Thompson }
51602ac6454SAndrew Thompson 
51702ac6454SAndrew Thompson static void
zyd_tx_free(struct zyd_tx_data * data,int txerr)51802ac6454SAndrew Thompson zyd_tx_free(struct zyd_tx_data *data, int txerr)
51902ac6454SAndrew Thompson {
52002ac6454SAndrew Thompson 	struct zyd_softc *sc = data->sc;
52102ac6454SAndrew Thompson 
52202ac6454SAndrew Thompson 	if (data->m != NULL) {
5237a79cebfSGleb Smirnoff 		ieee80211_tx_complete(data->ni, data->m, txerr);
52402ac6454SAndrew Thompson 		data->m = NULL;
52502ac6454SAndrew Thompson 		data->ni = NULL;
52602ac6454SAndrew Thompson 	}
52702ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
52802ac6454SAndrew Thompson 	sc->tx_nfree++;
52902ac6454SAndrew Thompson }
53002ac6454SAndrew Thompson 
53102ac6454SAndrew Thompson static void
zyd_setup_tx_list(struct zyd_softc * sc)53202ac6454SAndrew Thompson zyd_setup_tx_list(struct zyd_softc *sc)
53302ac6454SAndrew Thompson {
53402ac6454SAndrew Thompson 	struct zyd_tx_data *data;
53502ac6454SAndrew Thompson 	int i;
53602ac6454SAndrew Thompson 
53702ac6454SAndrew Thompson 	sc->tx_nfree = 0;
53802ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
53902ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
54202ac6454SAndrew Thompson 		data = &sc->tx_data[i];
54302ac6454SAndrew Thompson 
54402ac6454SAndrew Thompson 		data->sc = sc;
54502ac6454SAndrew Thompson 		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
54602ac6454SAndrew Thompson 		sc->tx_nfree++;
54702ac6454SAndrew Thompson 	}
54802ac6454SAndrew Thompson }
54902ac6454SAndrew Thompson 
55002ac6454SAndrew Thompson static void
zyd_unsetup_tx_list(struct zyd_softc * sc)55102ac6454SAndrew Thompson zyd_unsetup_tx_list(struct zyd_softc *sc)
55202ac6454SAndrew Thompson {
55302ac6454SAndrew Thompson 	struct zyd_tx_data *data;
55402ac6454SAndrew Thompson 	int i;
55502ac6454SAndrew Thompson 
55602ac6454SAndrew Thompson 	/* make sure any subsequent use of the queues will fail */
55702ac6454SAndrew Thompson 	sc->tx_nfree = 0;
55802ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
55902ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
56002ac6454SAndrew Thompson 
56102ac6454SAndrew Thompson 	/* free up all node references and mbufs */
56202ac6454SAndrew Thompson 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
56302ac6454SAndrew Thompson 		data = &sc->tx_data[i];
56402ac6454SAndrew Thompson 
56502ac6454SAndrew Thompson 		if (data->m != NULL) {
56602ac6454SAndrew Thompson 			m_freem(data->m);
56702ac6454SAndrew Thompson 			data->m = NULL;
56802ac6454SAndrew Thompson 		}
56902ac6454SAndrew Thompson 		if (data->ni != NULL) {
57002ac6454SAndrew Thompson 			ieee80211_free_node(data->ni);
57102ac6454SAndrew Thompson 			data->ni = NULL;
57202ac6454SAndrew Thompson 		}
57302ac6454SAndrew Thompson 	}
57402ac6454SAndrew Thompson }
57502ac6454SAndrew Thompson 
5765efea30fSAndrew Thompson static int
zyd_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)5775efea30fSAndrew Thompson zyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
57802ac6454SAndrew Thompson {
57902ac6454SAndrew Thompson 	struct zyd_vap *zvp = ZYD_VAP(vap);
5805efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
581d3fdd08cSAdrian Chadd 	struct zyd_softc *sc = ic->ic_softc;
58202ac6454SAndrew Thompson 	int error;
58302ac6454SAndrew Thompson 
5845efea30fSAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__,
5855efea30fSAndrew Thompson 	    ieee80211_state_name[vap->iv_state],
5865efea30fSAndrew Thompson 	    ieee80211_state_name[nstate]);
5875efea30fSAndrew Thompson 
5885efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
5895efea30fSAndrew Thompson 	ZYD_LOCK(sc);
5905efea30fSAndrew Thompson 	switch (nstate) {
59102ac6454SAndrew Thompson 	case IEEE80211_S_AUTH:
59202ac6454SAndrew Thompson 		zyd_set_chan(sc, ic->ic_curchan);
59302ac6454SAndrew Thompson 		break;
59402ac6454SAndrew Thompson 	case IEEE80211_S_RUN:
59502ac6454SAndrew Thompson 		if (vap->iv_opmode == IEEE80211_M_MONITOR)
59602ac6454SAndrew Thompson 			break;
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson 		/* turn link LED on */
59902ac6454SAndrew Thompson 		error = zyd_set_led(sc, ZYD_LED1, 1);
60002ac6454SAndrew Thompson 		if (error != 0)
6015efea30fSAndrew Thompson 			break;
60202ac6454SAndrew Thompson 
60302ac6454SAndrew Thompson 		/* make data LED blink upon Tx */
60402ac6454SAndrew Thompson 		zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1);
60502ac6454SAndrew Thompson 
606ae132f11SAndriy Voskoboinyk 		IEEE80211_ADDR_COPY(sc->sc_bssid, vap->iv_bss->ni_bssid);
607ae132f11SAndriy Voskoboinyk 		zyd_set_bssid(sc, sc->sc_bssid);
60802ac6454SAndrew Thompson 		break;
60902ac6454SAndrew Thompson 	default:
61002ac6454SAndrew Thompson 		break;
61102ac6454SAndrew Thompson 	}
61202ac6454SAndrew Thompson fail:
61302ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
61402ac6454SAndrew Thompson 	IEEE80211_LOCK(ic);
6155efea30fSAndrew Thompson 	return (zvp->newstate(vap, nstate, arg));
61602ac6454SAndrew Thompson }
61702ac6454SAndrew Thompson 
61802ac6454SAndrew Thompson /*
61902ac6454SAndrew Thompson  * Callback handler for interrupt transfer
62002ac6454SAndrew Thompson  */
62102ac6454SAndrew Thompson static void
zyd_intr_read_callback(struct usb_xfer * xfer,usb_error_t error)622ed6d949aSAndrew Thompson zyd_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
62302ac6454SAndrew Thompson {
624ed6d949aSAndrew Thompson 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
6257a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
62602ac6454SAndrew Thompson 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
62702ac6454SAndrew Thompson 	struct ieee80211_node *ni;
62802ac6454SAndrew Thompson 	struct zyd_cmd *cmd = &sc->sc_ibuf;
629ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
63002ac6454SAndrew Thompson 	int datalen;
631ed6d949aSAndrew Thompson 	int actlen;
632ed6d949aSAndrew Thompson 
633ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
63402ac6454SAndrew Thompson 
63502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
63602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
637ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
638ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, cmd, sizeof(*cmd));
63902ac6454SAndrew Thompson 
64002ac6454SAndrew Thompson 		switch (le16toh(cmd->code)) {
64102ac6454SAndrew Thompson 		case ZYD_NOTIF_RETRYSTATUS:
64202ac6454SAndrew Thompson 		{
64302ac6454SAndrew Thompson 			struct zyd_notif_retry *retry =
64402ac6454SAndrew Thompson 			    (struct zyd_notif_retry *)cmd->data;
645a08e9300SAndriy Voskoboinyk 			uint16_t count = le16toh(retry->count);
64602ac6454SAndrew Thompson 
64702ac6454SAndrew Thompson 			DPRINTF(sc, ZYD_DEBUG_TX_PROC,
64802ac6454SAndrew Thompson 			    "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
64902ac6454SAndrew Thompson 			    le16toh(retry->rate), ether_sprintf(retry->macaddr),
650a08e9300SAndriy Voskoboinyk 			    count & 0xff, count);
65102ac6454SAndrew Thompson 
65202ac6454SAndrew Thompson 			/*
65302ac6454SAndrew Thompson 			 * Find the node to which the packet was sent and
65402ac6454SAndrew Thompson 			 * update its retry statistics.  In BSS mode, this node
65502ac6454SAndrew Thompson 			 * is the AP we're associated to so no lookup is
65602ac6454SAndrew Thompson 			 * actually needed.
65702ac6454SAndrew Thompson 			 */
65802ac6454SAndrew Thompson 			ni = ieee80211_find_txnode(vap, retry->macaddr);
65902ac6454SAndrew Thompson 			if (ni != NULL) {
660f6930becSAndriy Voskoboinyk 				struct ieee80211_ratectl_tx_status *txs =
661f6930becSAndriy Voskoboinyk 				    &sc->sc_txs;
662a08e9300SAndriy Voskoboinyk 				int retrycnt = count & 0xff;
663b6108616SRui Paulo 
664f6930becSAndriy Voskoboinyk 				txs->flags =
665f6930becSAndriy Voskoboinyk 				    IEEE80211_RATECTL_STATUS_LONG_RETRY;
666f6930becSAndriy Voskoboinyk 				txs->long_retries = retrycnt;
667a08e9300SAndriy Voskoboinyk 				if (count & 0x100) {
668f6930becSAndriy Voskoboinyk 					txs->status =
669f6930becSAndriy Voskoboinyk 					    IEEE80211_RATECTL_TX_FAIL_LONG;
670f6930becSAndriy Voskoboinyk 				} else {
671f6930becSAndriy Voskoboinyk 					txs->status =
672f6930becSAndriy Voskoboinyk 					    IEEE80211_RATECTL_TX_SUCCESS;
673f6930becSAndriy Voskoboinyk 				}
674f6930becSAndriy Voskoboinyk 
675f6930becSAndriy Voskoboinyk 				ieee80211_ratectl_tx_complete(ni, txs);
67602ac6454SAndrew Thompson 				ieee80211_free_node(ni);
67702ac6454SAndrew Thompson 			}
678a08e9300SAndriy Voskoboinyk 			if (count & 0x100)
6797a79cebfSGleb Smirnoff 				/* too many retries */
6807a79cebfSGleb Smirnoff 				if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS,
6817a79cebfSGleb Smirnoff 				    1);
68202ac6454SAndrew Thompson 			break;
68302ac6454SAndrew Thompson 		}
68402ac6454SAndrew Thompson 		case ZYD_NOTIF_IORD:
68502ac6454SAndrew Thompson 		{
68602ac6454SAndrew Thompson 			struct zyd_rq *rqp;
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson 			if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
68902ac6454SAndrew Thompson 				break;	/* HMAC interrupt */
69002ac6454SAndrew Thompson 
691ed6d949aSAndrew Thompson 			datalen = actlen - sizeof(cmd->code);
69202ac6454SAndrew Thompson 			datalen -= 2;	/* XXX: padding? */
69302ac6454SAndrew Thompson 
69402ac6454SAndrew Thompson 			STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
6956d917491SHans Petter Selasky 				int i;
6966d917491SHans Petter Selasky 				int count;
69702ac6454SAndrew Thompson 
69802ac6454SAndrew Thompson 				if (rqp->olen != datalen)
69902ac6454SAndrew Thompson 					continue;
7006d917491SHans Petter Selasky 				count = rqp->olen / sizeof(struct zyd_pair);
7016d917491SHans Petter Selasky 				for (i = 0; i < count; i++) {
70202ac6454SAndrew Thompson 					if (*(((const uint16_t *)rqp->idata) + i) !=
70302ac6454SAndrew Thompson 					    (((struct zyd_pair *)cmd->data) + i)->reg)
70402ac6454SAndrew Thompson 						break;
70502ac6454SAndrew Thompson 				}
7066d917491SHans Petter Selasky 				if (i != count)
70702ac6454SAndrew Thompson 					continue;
70802ac6454SAndrew Thompson 				/* copy answer into caller-supplied buffer */
709271ae033SHans Petter Selasky 				memcpy(rqp->odata, cmd->data, rqp->olen);
71002ac6454SAndrew Thompson 				DPRINTF(sc, ZYD_DEBUG_CMD,
71102ac6454SAndrew Thompson 				    "command %p complete, data = %*D \n",
71294bceb52SHans Petter Selasky 				    rqp, rqp->olen, (char *)rqp->odata, ":");
71302ac6454SAndrew Thompson 				wakeup(rqp);	/* wakeup caller */
71402ac6454SAndrew Thompson 				break;
71502ac6454SAndrew Thompson 			}
71602ac6454SAndrew Thompson 			if (rqp == NULL) {
71702ac6454SAndrew Thompson 				device_printf(sc->sc_dev,
71802ac6454SAndrew Thompson 				    "unexpected IORD notification %*D\n",
71994bceb52SHans Petter Selasky 				    datalen, cmd->data, ":");
72002ac6454SAndrew Thompson 			}
72102ac6454SAndrew Thompson 			break;
72202ac6454SAndrew Thompson 		}
72302ac6454SAndrew Thompson 		default:
72402ac6454SAndrew Thompson 			device_printf(sc->sc_dev, "unknown notification %x\n",
72502ac6454SAndrew Thompson 			    le16toh(cmd->code));
72602ac6454SAndrew Thompson 		}
72702ac6454SAndrew Thompson 
72802ac6454SAndrew Thompson 		/* FALLTHROUGH */
72902ac6454SAndrew Thompson 	case USB_ST_SETUP:
73002ac6454SAndrew Thompson tr_setup:
731ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
732a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
73302ac6454SAndrew Thompson 		break;
73402ac6454SAndrew Thompson 
73502ac6454SAndrew Thompson 	default:			/* Error */
73602ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
737ed6d949aSAndrew Thompson 		    usbd_errstr(error));
73802ac6454SAndrew Thompson 
739ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
74002ac6454SAndrew Thompson 			/* try to clear stall first */
741ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
74202ac6454SAndrew Thompson 			goto tr_setup;
74302ac6454SAndrew Thompson 		}
74402ac6454SAndrew Thompson 		break;
74502ac6454SAndrew Thompson 	}
74602ac6454SAndrew Thompson }
74702ac6454SAndrew Thompson 
74802ac6454SAndrew Thompson static void
zyd_intr_write_callback(struct usb_xfer * xfer,usb_error_t error)749ed6d949aSAndrew Thompson zyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
75002ac6454SAndrew Thompson {
751ed6d949aSAndrew Thompson 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
752ed6d949aSAndrew Thompson 	struct zyd_rq *rqp, *cmd;
753ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
75402ac6454SAndrew Thompson 
75502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
75602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
757ed6d949aSAndrew Thompson 		cmd = usbd_xfer_get_priv(xfer);
758ed6d949aSAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd);
759d953f720SAndrew Thompson 		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
760d953f720SAndrew Thompson 			/* Ensure the cached rq pointer is still valid */
761ed6d949aSAndrew Thompson 			if (rqp == cmd &&
762d953f720SAndrew Thompson 			    (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
76302ac6454SAndrew Thompson 				wakeup(rqp);	/* wakeup caller */
764d953f720SAndrew Thompson 		}
76502ac6454SAndrew Thompson 
76602ac6454SAndrew Thompson 		/* FALLTHROUGH */
76702ac6454SAndrew Thompson 	case USB_ST_SETUP:
76802ac6454SAndrew Thompson tr_setup:
76902ac6454SAndrew Thompson 		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
77002ac6454SAndrew Thompson 			if (rqp->flags & ZYD_CMD_FLAG_SENT)
77102ac6454SAndrew Thompson 				continue;
77202ac6454SAndrew Thompson 
773ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
774ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen);
77502ac6454SAndrew Thompson 
776ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, rqp->ilen);
777ed6d949aSAndrew Thompson 			usbd_xfer_set_priv(xfer, rqp);
77802ac6454SAndrew Thompson 			rqp->flags |= ZYD_CMD_FLAG_SENT;
779a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
78002ac6454SAndrew Thompson 			break;
78102ac6454SAndrew Thompson 		}
78202ac6454SAndrew Thompson 		break;
78302ac6454SAndrew Thompson 
78402ac6454SAndrew Thompson 	default:			/* Error */
78502ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
786ed6d949aSAndrew Thompson 		    usbd_errstr(error));
78702ac6454SAndrew Thompson 
788ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
78902ac6454SAndrew Thompson 			/* try to clear stall first */
790ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
79102ac6454SAndrew Thompson 			goto tr_setup;
79202ac6454SAndrew Thompson 		}
79302ac6454SAndrew Thompson 		break;
79402ac6454SAndrew Thompson 	}
79502ac6454SAndrew Thompson }
79602ac6454SAndrew Thompson 
79702ac6454SAndrew Thompson static int
zyd_cmd(struct zyd_softc * sc,uint16_t code,const void * idata,int ilen,void * odata,int olen,int flags)79802ac6454SAndrew Thompson zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
79902ac6454SAndrew Thompson     void *odata, int olen, int flags)
80002ac6454SAndrew Thompson {
80102ac6454SAndrew Thompson 	struct zyd_cmd cmd;
80202ac6454SAndrew Thompson 	struct zyd_rq rq;
80302ac6454SAndrew Thompson 	int error;
80402ac6454SAndrew Thompson 
8056d917491SHans Petter Selasky 	if (ilen > (int)sizeof(cmd.data))
80602ac6454SAndrew Thompson 		return (EINVAL);
80702ac6454SAndrew Thompson 
80802ac6454SAndrew Thompson 	cmd.code = htole16(code);
809271ae033SHans Petter Selasky 	memcpy(cmd.data, idata, ilen);
81002ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
81194bceb52SHans Petter Selasky 	    &rq, ilen, idata, ":");
81202ac6454SAndrew Thompson 
81302ac6454SAndrew Thompson 	rq.cmd = &cmd;
81402ac6454SAndrew Thompson 	rq.idata = idata;
81502ac6454SAndrew Thompson 	rq.odata = odata;
81602ac6454SAndrew Thompson 	rq.ilen = sizeof(uint16_t) + ilen;
81702ac6454SAndrew Thompson 	rq.olen = olen;
81802ac6454SAndrew Thompson 	rq.flags = flags;
81902ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
820a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
821a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
82202ac6454SAndrew Thompson 
82302ac6454SAndrew Thompson 	/* wait at most one second for command reply */
82402ac6454SAndrew Thompson 	error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
82502ac6454SAndrew Thompson 	if (error)
82602ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "command timeout\n");
82702ac6454SAndrew Thompson 	STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
82802ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
82902ac6454SAndrew Thompson 	    &rq, error);
83002ac6454SAndrew Thompson 
83102ac6454SAndrew Thompson 	return (error);
83202ac6454SAndrew Thompson }
83302ac6454SAndrew Thompson 
83402ac6454SAndrew Thompson static int
zyd_read16(struct zyd_softc * sc,uint16_t reg,uint16_t * val)83502ac6454SAndrew Thompson zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
83602ac6454SAndrew Thompson {
83702ac6454SAndrew Thompson 	struct zyd_pair tmp;
83802ac6454SAndrew Thompson 	int error;
83902ac6454SAndrew Thompson 
84002ac6454SAndrew Thompson 	reg = htole16(reg);
84102ac6454SAndrew Thompson 	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
84202ac6454SAndrew Thompson 	    ZYD_CMD_FLAG_READ);
84302ac6454SAndrew Thompson 	if (error == 0)
84402ac6454SAndrew Thompson 		*val = le16toh(tmp.val);
84502ac6454SAndrew Thompson 	return (error);
84602ac6454SAndrew Thompson }
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson static int
zyd_read32(struct zyd_softc * sc,uint16_t reg,uint32_t * val)84902ac6454SAndrew Thompson zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
85002ac6454SAndrew Thompson {
85102ac6454SAndrew Thompson 	struct zyd_pair tmp[2];
85202ac6454SAndrew Thompson 	uint16_t regs[2];
85302ac6454SAndrew Thompson 	int error;
85402ac6454SAndrew Thompson 
85502ac6454SAndrew Thompson 	regs[0] = htole16(ZYD_REG32_HI(reg));
85602ac6454SAndrew Thompson 	regs[1] = htole16(ZYD_REG32_LO(reg));
85702ac6454SAndrew Thompson 	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
85802ac6454SAndrew Thompson 	    ZYD_CMD_FLAG_READ);
85902ac6454SAndrew Thompson 	if (error == 0)
86002ac6454SAndrew Thompson 		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
86102ac6454SAndrew Thompson 	return (error);
86202ac6454SAndrew Thompson }
86302ac6454SAndrew Thompson 
86402ac6454SAndrew Thompson static int
zyd_write16(struct zyd_softc * sc,uint16_t reg,uint16_t val)86502ac6454SAndrew Thompson zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
86602ac6454SAndrew Thompson {
86702ac6454SAndrew Thompson 	struct zyd_pair pair;
86802ac6454SAndrew Thompson 
86902ac6454SAndrew Thompson 	pair.reg = htole16(reg);
87002ac6454SAndrew Thompson 	pair.val = htole16(val);
87102ac6454SAndrew Thompson 
87202ac6454SAndrew Thompson 	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
87302ac6454SAndrew Thompson }
87402ac6454SAndrew Thompson 
87502ac6454SAndrew Thompson static int
zyd_write32(struct zyd_softc * sc,uint16_t reg,uint32_t val)87602ac6454SAndrew Thompson zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
87702ac6454SAndrew Thompson {
87802ac6454SAndrew Thompson 	struct zyd_pair pair[2];
87902ac6454SAndrew Thompson 
88002ac6454SAndrew Thompson 	pair[0].reg = htole16(ZYD_REG32_HI(reg));
88102ac6454SAndrew Thompson 	pair[0].val = htole16(val >> 16);
88202ac6454SAndrew Thompson 	pair[1].reg = htole16(ZYD_REG32_LO(reg));
88302ac6454SAndrew Thompson 	pair[1].val = htole16(val & 0xffff);
88402ac6454SAndrew Thompson 
88502ac6454SAndrew Thompson 	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
88602ac6454SAndrew Thompson }
88702ac6454SAndrew Thompson 
88802ac6454SAndrew Thompson static int
zyd_rfwrite(struct zyd_softc * sc,uint32_t val)88902ac6454SAndrew Thompson zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
89002ac6454SAndrew Thompson {
89102ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
89202ac6454SAndrew Thompson 	struct zyd_rfwrite_cmd req;
89302ac6454SAndrew Thompson 	uint16_t cr203;
89402ac6454SAndrew Thompson 	int error, i;
89502ac6454SAndrew Thompson 
89602ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &cr203);
89702ac6454SAndrew Thompson 	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
89802ac6454SAndrew Thompson 
89902ac6454SAndrew Thompson 	req.code  = htole16(2);
90002ac6454SAndrew Thompson 	req.width = htole16(rf->width);
90102ac6454SAndrew Thompson 	for (i = 0; i < rf->width; i++) {
90202ac6454SAndrew Thompson 		req.bit[i] = htole16(cr203);
90302ac6454SAndrew Thompson 		if (val & (1 << (rf->width - 1 - i)))
90402ac6454SAndrew Thompson 			req.bit[i] |= htole16(ZYD_RF_DATA);
90502ac6454SAndrew Thompson 	}
90602ac6454SAndrew Thompson 	error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
90702ac6454SAndrew Thompson fail:
90802ac6454SAndrew Thompson 	return (error);
90902ac6454SAndrew Thompson }
91002ac6454SAndrew Thompson 
91102ac6454SAndrew Thompson static int
zyd_rfwrite_cr(struct zyd_softc * sc,uint32_t val)91202ac6454SAndrew Thompson zyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
91302ac6454SAndrew Thompson {
91402ac6454SAndrew Thompson 	int error;
91502ac6454SAndrew Thompson 
91602ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
91702ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR243, (val >>  8) & 0xff);
91802ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR242, (val >>  0) & 0xff);
91902ac6454SAndrew Thompson fail:
92002ac6454SAndrew Thompson 	return (error);
92102ac6454SAndrew Thompson }
92202ac6454SAndrew Thompson 
92302ac6454SAndrew Thompson static int
zyd_lock_phy(struct zyd_softc * sc)92402ac6454SAndrew Thompson zyd_lock_phy(struct zyd_softc *sc)
92502ac6454SAndrew Thompson {
92602ac6454SAndrew Thompson 	int error;
92702ac6454SAndrew Thompson 	uint32_t tmp;
92802ac6454SAndrew Thompson 
92902ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
93002ac6454SAndrew Thompson 	tmp &= ~ZYD_UNLOCK_PHY_REGS;
93102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
93202ac6454SAndrew Thompson fail:
93302ac6454SAndrew Thompson 	return (error);
93402ac6454SAndrew Thompson }
93502ac6454SAndrew Thompson 
93602ac6454SAndrew Thompson static int
zyd_unlock_phy(struct zyd_softc * sc)93702ac6454SAndrew Thompson zyd_unlock_phy(struct zyd_softc *sc)
93802ac6454SAndrew Thompson {
93902ac6454SAndrew Thompson 	int error;
94002ac6454SAndrew Thompson 	uint32_t tmp;
94102ac6454SAndrew Thompson 
94202ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
94302ac6454SAndrew Thompson 	tmp |= ZYD_UNLOCK_PHY_REGS;
94402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
94502ac6454SAndrew Thompson fail:
94602ac6454SAndrew Thompson 	return (error);
94702ac6454SAndrew Thompson }
94802ac6454SAndrew Thompson 
94902ac6454SAndrew Thompson /*
95002ac6454SAndrew Thompson  * RFMD RF methods.
95102ac6454SAndrew Thompson  */
95202ac6454SAndrew Thompson static int
zyd_rfmd_init(struct zyd_rf * rf)95302ac6454SAndrew Thompson zyd_rfmd_init(struct zyd_rf *rf)
95402ac6454SAndrew Thompson {
95502ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
95602ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
95702ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_RFMD_RF;
95802ac6454SAndrew Thompson 	int i, error;
95902ac6454SAndrew Thompson 
96002ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
96182e8c646SAdrian Chadd 	for (i = 0; i < nitems(phyini); i++) {
96202ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
96302ac6454SAndrew Thompson 	}
96402ac6454SAndrew Thompson 
96502ac6454SAndrew Thompson 	/* init RFMD radio */
96682e8c646SAdrian Chadd 	for (i = 0; i < nitems(rfini); i++) {
96702ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
96802ac6454SAndrew Thompson 			return (error);
96902ac6454SAndrew Thompson 	}
97002ac6454SAndrew Thompson fail:
97102ac6454SAndrew Thompson 	return (error);
97202ac6454SAndrew Thompson }
97302ac6454SAndrew Thompson 
97402ac6454SAndrew Thompson static int
zyd_rfmd_switch_radio(struct zyd_rf * rf,int on)97502ac6454SAndrew Thompson zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
97602ac6454SAndrew Thompson {
97702ac6454SAndrew Thompson 	int error;
97802ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
97902ac6454SAndrew Thompson 
98002ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
98102ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
98202ac6454SAndrew Thompson fail:
98302ac6454SAndrew Thompson 	return (error);
98402ac6454SAndrew Thompson }
98502ac6454SAndrew Thompson 
98602ac6454SAndrew Thompson static int
zyd_rfmd_set_channel(struct zyd_rf * rf,uint8_t chan)98702ac6454SAndrew Thompson zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
98802ac6454SAndrew Thompson {
98902ac6454SAndrew Thompson 	int error;
99002ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
99102ac6454SAndrew Thompson 	static const struct {
99202ac6454SAndrew Thompson 		uint32_t	r1, r2;
99302ac6454SAndrew Thompson 	} rfprog[] = ZYD_RFMD_CHANTABLE;
99402ac6454SAndrew Thompson 
99502ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
99602ac6454SAndrew Thompson 	if (error != 0)
99702ac6454SAndrew Thompson 		goto fail;
99802ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
99902ac6454SAndrew Thompson 	if (error != 0)
100002ac6454SAndrew Thompson 		goto fail;
100102ac6454SAndrew Thompson 
100202ac6454SAndrew Thompson fail:
100302ac6454SAndrew Thompson 	return (error);
100402ac6454SAndrew Thompson }
100502ac6454SAndrew Thompson 
100602ac6454SAndrew Thompson /*
100702ac6454SAndrew Thompson  * AL2230 RF methods.
100802ac6454SAndrew Thompson  */
100902ac6454SAndrew Thompson static int
zyd_al2230_init(struct zyd_rf * rf)101002ac6454SAndrew Thompson zyd_al2230_init(struct zyd_rf *rf)
101102ac6454SAndrew Thompson {
101202ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
101302ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
101402ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
101502ac6454SAndrew Thompson 	static const struct zyd_phy_pair phypll[] = {
101602ac6454SAndrew Thompson 		{ ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
101702ac6454SAndrew Thompson 		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
101802ac6454SAndrew Thompson 	};
101902ac6454SAndrew Thompson 	static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
102002ac6454SAndrew Thompson 	static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
102102ac6454SAndrew Thompson 	static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
102202ac6454SAndrew Thompson 	int i, error;
102302ac6454SAndrew Thompson 
102402ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
102582e8c646SAdrian Chadd 	for (i = 0; i < nitems(phyini); i++)
102602ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
102702ac6454SAndrew Thompson 
102802ac6454SAndrew Thompson 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
102982e8c646SAdrian Chadd 		for (i = 0; i < nitems(phy2230s); i++)
103002ac6454SAndrew Thompson 			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
103102ac6454SAndrew Thompson 	}
103202ac6454SAndrew Thompson 
103302ac6454SAndrew Thompson 	/* init AL2230 radio */
103482e8c646SAdrian Chadd 	for (i = 0; i < nitems(rfini1); i++) {
103502ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, rfini1[i]);
103602ac6454SAndrew Thompson 		if (error != 0)
103702ac6454SAndrew Thompson 			goto fail;
103802ac6454SAndrew Thompson 	}
103902ac6454SAndrew Thompson 
104002ac6454SAndrew Thompson 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
104102ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, 0x000824);
104202ac6454SAndrew Thompson 	else
104302ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, 0x0005a4);
104402ac6454SAndrew Thompson 	if (error != 0)
104502ac6454SAndrew Thompson 		goto fail;
104602ac6454SAndrew Thompson 
104782e8c646SAdrian Chadd 	for (i = 0; i < nitems(rfini2); i++) {
104802ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, rfini2[i]);
104902ac6454SAndrew Thompson 		if (error != 0)
105002ac6454SAndrew Thompson 			goto fail;
105102ac6454SAndrew Thompson 	}
105202ac6454SAndrew Thompson 
105382e8c646SAdrian Chadd 	for (i = 0; i < nitems(phypll); i++)
105402ac6454SAndrew Thompson 		zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
105502ac6454SAndrew Thompson 
105682e8c646SAdrian Chadd 	for (i = 0; i < nitems(rfini3); i++) {
105702ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, rfini3[i]);
105802ac6454SAndrew Thompson 		if (error != 0)
105902ac6454SAndrew Thompson 			goto fail;
106002ac6454SAndrew Thompson 	}
106102ac6454SAndrew Thompson fail:
106202ac6454SAndrew Thompson 	return (error);
106302ac6454SAndrew Thompson }
106402ac6454SAndrew Thompson 
106502ac6454SAndrew Thompson static int
zyd_al2230_fini(struct zyd_rf * rf)106602ac6454SAndrew Thompson zyd_al2230_fini(struct zyd_rf *rf)
106702ac6454SAndrew Thompson {
106802ac6454SAndrew Thompson 	int error, i;
106902ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
107002ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
107102ac6454SAndrew Thompson 
107282e8c646SAdrian Chadd 	for (i = 0; i < nitems(phy); i++)
107302ac6454SAndrew Thompson 		zyd_write16_m(sc, phy[i].reg, phy[i].val);
107402ac6454SAndrew Thompson 
107502ac6454SAndrew Thompson 	if (sc->sc_newphy != 0)
107602ac6454SAndrew Thompson 		zyd_write16_m(sc, ZYD_CR9, 0xe1);
107702ac6454SAndrew Thompson 
107802ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, 0x6);
107902ac6454SAndrew Thompson fail:
108002ac6454SAndrew Thompson 	return (error);
108102ac6454SAndrew Thompson }
108202ac6454SAndrew Thompson 
108302ac6454SAndrew Thompson static int
zyd_al2230_init_b(struct zyd_rf * rf)108402ac6454SAndrew Thompson zyd_al2230_init_b(struct zyd_rf *rf)
108502ac6454SAndrew Thompson {
108602ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
108702ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
108802ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
108902ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
109002ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
109102ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
109202ac6454SAndrew Thompson 	static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
109302ac6454SAndrew Thompson 	static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
109402ac6454SAndrew Thompson 	static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
109502ac6454SAndrew Thompson 	static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
109602ac6454SAndrew Thompson 	int i, error;
109702ac6454SAndrew Thompson 
109882e8c646SAdrian Chadd 	for (i = 0; i < nitems(phy1); i++)
109902ac6454SAndrew Thompson 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
110002ac6454SAndrew Thompson 
110102ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
110282e8c646SAdrian Chadd 	for (i = 0; i < nitems(phyini); i++)
110302ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
110402ac6454SAndrew Thompson 
110502ac6454SAndrew Thompson 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
110682e8c646SAdrian Chadd 		for (i = 0; i < nitems(phy2230s); i++)
110702ac6454SAndrew Thompson 			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
110802ac6454SAndrew Thompson 	}
110902ac6454SAndrew Thompson 
111002ac6454SAndrew Thompson 	for (i = 0; i < 3; i++) {
111102ac6454SAndrew Thompson 		error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
111202ac6454SAndrew Thompson 		if (error != 0)
111302ac6454SAndrew Thompson 			return (error);
111402ac6454SAndrew Thompson 	}
111502ac6454SAndrew Thompson 
111682e8c646SAdrian Chadd 	for (i = 0; i < nitems(rfini_part1); i++) {
111702ac6454SAndrew Thompson 		error = zyd_rfwrite_cr(sc, rfini_part1[i]);
111802ac6454SAndrew Thompson 		if (error != 0)
111902ac6454SAndrew Thompson 			return (error);
112002ac6454SAndrew Thompson 	}
112102ac6454SAndrew Thompson 
112202ac6454SAndrew Thompson 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
112302ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, 0x241000);
112402ac6454SAndrew Thompson 	else
112502ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, 0x25a000);
112602ac6454SAndrew Thompson 	if (error != 0)
112702ac6454SAndrew Thompson 		goto fail;
112802ac6454SAndrew Thompson 
112982e8c646SAdrian Chadd 	for (i = 0; i < nitems(rfini_part2); i++) {
113002ac6454SAndrew Thompson 		error = zyd_rfwrite_cr(sc, rfini_part2[i]);
113102ac6454SAndrew Thompson 		if (error != 0)
113202ac6454SAndrew Thompson 			return (error);
113302ac6454SAndrew Thompson 	}
113402ac6454SAndrew Thompson 
113582e8c646SAdrian Chadd 	for (i = 0; i < nitems(phy2); i++)
113602ac6454SAndrew Thompson 		zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
113702ac6454SAndrew Thompson 
113882e8c646SAdrian Chadd 	for (i = 0; i < nitems(rfini_part3); i++) {
113902ac6454SAndrew Thompson 		error = zyd_rfwrite_cr(sc, rfini_part3[i]);
114002ac6454SAndrew Thompson 		if (error != 0)
114102ac6454SAndrew Thompson 			return (error);
114202ac6454SAndrew Thompson 	}
114302ac6454SAndrew Thompson 
114482e8c646SAdrian Chadd 	for (i = 0; i < nitems(phy3); i++)
114502ac6454SAndrew Thompson 		zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
114602ac6454SAndrew Thompson 
114702ac6454SAndrew Thompson 	error = zyd_al2230_fini(rf);
114802ac6454SAndrew Thompson fail:
114902ac6454SAndrew Thompson 	return (error);
115002ac6454SAndrew Thompson }
115102ac6454SAndrew Thompson 
115202ac6454SAndrew Thompson static int
zyd_al2230_switch_radio(struct zyd_rf * rf,int on)115302ac6454SAndrew Thompson zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
115402ac6454SAndrew Thompson {
115502ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
115602ac6454SAndrew Thompson 	int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
115702ac6454SAndrew Thompson 
115802ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
115902ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
116002ac6454SAndrew Thompson fail:
116102ac6454SAndrew Thompson 	return (error);
116202ac6454SAndrew Thompson }
116302ac6454SAndrew Thompson 
116402ac6454SAndrew Thompson static int
zyd_al2230_set_channel(struct zyd_rf * rf,uint8_t chan)116502ac6454SAndrew Thompson zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
116602ac6454SAndrew Thompson {
116702ac6454SAndrew Thompson 	int error, i;
116802ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
116902ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy1[] = {
117002ac6454SAndrew Thompson 		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
117102ac6454SAndrew Thompson 	};
117202ac6454SAndrew Thompson 	static const struct {
117302ac6454SAndrew Thompson 		uint32_t	r1, r2, r3;
117402ac6454SAndrew Thompson 	} rfprog[] = ZYD_AL2230_CHANTABLE;
117502ac6454SAndrew Thompson 
117602ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
117702ac6454SAndrew Thompson 	if (error != 0)
117802ac6454SAndrew Thompson 		goto fail;
117902ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
118002ac6454SAndrew Thompson 	if (error != 0)
118102ac6454SAndrew Thompson 		goto fail;
118202ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
118302ac6454SAndrew Thompson 	if (error != 0)
118402ac6454SAndrew Thompson 		goto fail;
118502ac6454SAndrew Thompson 
118682e8c646SAdrian Chadd 	for (i = 0; i < nitems(phy1); i++)
118702ac6454SAndrew Thompson 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
118802ac6454SAndrew Thompson fail:
118902ac6454SAndrew Thompson 	return (error);
119002ac6454SAndrew Thompson }
119102ac6454SAndrew Thompson 
119202ac6454SAndrew Thompson static int
zyd_al2230_set_channel_b(struct zyd_rf * rf,uint8_t chan)119302ac6454SAndrew Thompson zyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
119402ac6454SAndrew Thompson {
119502ac6454SAndrew Thompson 	int error, i;
119602ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
119702ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
119802ac6454SAndrew Thompson 	static const struct {
119902ac6454SAndrew Thompson 		uint32_t	r1, r2, r3;
120002ac6454SAndrew Thompson 	} rfprog[] = ZYD_AL2230_CHANTABLE_B;
120102ac6454SAndrew Thompson 
120282e8c646SAdrian Chadd 	for (i = 0; i < nitems(phy1); i++)
120302ac6454SAndrew Thompson 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
120402ac6454SAndrew Thompson 
120502ac6454SAndrew Thompson 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
120602ac6454SAndrew Thompson 	if (error != 0)
120702ac6454SAndrew Thompson 		goto fail;
120802ac6454SAndrew Thompson 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
120902ac6454SAndrew Thompson 	if (error != 0)
121002ac6454SAndrew Thompson 		goto fail;
121102ac6454SAndrew Thompson 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
121202ac6454SAndrew Thompson 	if (error != 0)
121302ac6454SAndrew Thompson 		goto fail;
121402ac6454SAndrew Thompson 	error = zyd_al2230_fini(rf);
121502ac6454SAndrew Thompson fail:
121602ac6454SAndrew Thompson 	return (error);
121702ac6454SAndrew Thompson }
121802ac6454SAndrew Thompson 
121902ac6454SAndrew Thompson #define	ZYD_AL2230_PHY_BANDEDGE6					\
122002ac6454SAndrew Thompson {									\
122102ac6454SAndrew Thompson 	{ ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 },	\
122202ac6454SAndrew Thompson 	{ ZYD_CR47,  0x1e }						\
122302ac6454SAndrew Thompson }
122402ac6454SAndrew Thompson 
122502ac6454SAndrew Thompson static int
zyd_al2230_bandedge6(struct zyd_rf * rf,struct ieee80211_channel * c)122602ac6454SAndrew Thompson zyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
122702ac6454SAndrew Thompson {
122802ac6454SAndrew Thompson 	int error = 0, i;
122902ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
12307a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
123102ac6454SAndrew Thompson 	struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
123202ac6454SAndrew Thompson 	int chan = ieee80211_chan2ieee(ic, c);
123302ac6454SAndrew Thompson 
123402ac6454SAndrew Thompson 	if (chan == 1 || chan == 11)
123502ac6454SAndrew Thompson 		r[0].val = 0x12;
123602ac6454SAndrew Thompson 
123782e8c646SAdrian Chadd 	for (i = 0; i < nitems(r); i++)
123802ac6454SAndrew Thompson 		zyd_write16_m(sc, r[i].reg, r[i].val);
123902ac6454SAndrew Thompson fail:
124002ac6454SAndrew Thompson 	return (error);
124102ac6454SAndrew Thompson }
124202ac6454SAndrew Thompson 
124302ac6454SAndrew Thompson /*
124402ac6454SAndrew Thompson  * AL7230B RF methods.
124502ac6454SAndrew Thompson  */
124602ac6454SAndrew Thompson static int
zyd_al7230B_init(struct zyd_rf * rf)124702ac6454SAndrew Thompson zyd_al7230B_init(struct zyd_rf *rf)
124802ac6454SAndrew Thompson {
124902ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
125002ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
125102ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
125202ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
125302ac6454SAndrew Thompson 	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
125402ac6454SAndrew Thompson 	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
125502ac6454SAndrew Thompson 	int i, error;
125602ac6454SAndrew Thompson 
125702ac6454SAndrew Thompson 	/* for AL7230B, PHY and RF need to be initialized in "phases" */
125802ac6454SAndrew Thompson 
125902ac6454SAndrew Thompson 	/* init RF-dependent PHY registers, part one */
126082e8c646SAdrian Chadd 	for (i = 0; i < nitems(phyini_1); i++)
126102ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
126202ac6454SAndrew Thompson 
126302ac6454SAndrew Thompson 	/* init AL7230B radio, part one */
126482e8c646SAdrian Chadd 	for (i = 0; i < nitems(rfini_1); i++) {
126502ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
126602ac6454SAndrew Thompson 			return (error);
126702ac6454SAndrew Thompson 	}
126802ac6454SAndrew Thompson 	/* init RF-dependent PHY registers, part two */
126982e8c646SAdrian Chadd 	for (i = 0; i < nitems(phyini_2); i++)
127002ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
127102ac6454SAndrew Thompson 
127202ac6454SAndrew Thompson 	/* init AL7230B radio, part two */
127382e8c646SAdrian Chadd 	for (i = 0; i < nitems(rfini_2); i++) {
127402ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
127502ac6454SAndrew Thompson 			return (error);
127602ac6454SAndrew Thompson 	}
127702ac6454SAndrew Thompson 	/* init RF-dependent PHY registers, part three */
127882e8c646SAdrian Chadd 	for (i = 0; i < nitems(phyini_3); i++)
127902ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
128002ac6454SAndrew Thompson fail:
128102ac6454SAndrew Thompson 	return (error);
128202ac6454SAndrew Thompson }
128302ac6454SAndrew Thompson 
128402ac6454SAndrew Thompson static int
zyd_al7230B_switch_radio(struct zyd_rf * rf,int on)128502ac6454SAndrew Thompson zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
128602ac6454SAndrew Thompson {
128702ac6454SAndrew Thompson 	int error;
128802ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
128902ac6454SAndrew Thompson 
129002ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
129102ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
129202ac6454SAndrew Thompson fail:
129302ac6454SAndrew Thompson 	return (error);
129402ac6454SAndrew Thompson }
129502ac6454SAndrew Thompson 
129602ac6454SAndrew Thompson static int
zyd_al7230B_set_channel(struct zyd_rf * rf,uint8_t chan)129702ac6454SAndrew Thompson zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
129802ac6454SAndrew Thompson {
129902ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
130002ac6454SAndrew Thompson 	static const struct {
130102ac6454SAndrew Thompson 		uint32_t	r1, r2;
130202ac6454SAndrew Thompson 	} rfprog[] = ZYD_AL7230B_CHANTABLE;
130302ac6454SAndrew Thompson 	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
130402ac6454SAndrew Thompson 	int i, error;
130502ac6454SAndrew Thompson 
130602ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR240, 0x57);
130702ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR251, 0x2f);
130802ac6454SAndrew Thompson 
130982e8c646SAdrian Chadd 	for (i = 0; i < nitems(rfsc); i++) {
131002ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
131102ac6454SAndrew Thompson 			return (error);
131202ac6454SAndrew Thompson 	}
131302ac6454SAndrew Thompson 
131402ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR128, 0x14);
131502ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR129, 0x12);
131602ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR130, 0x10);
131702ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR38,  0x38);
131802ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR136, 0xdf);
131902ac6454SAndrew Thompson 
132002ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
132102ac6454SAndrew Thompson 	if (error != 0)
132202ac6454SAndrew Thompson 		goto fail;
132302ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
132402ac6454SAndrew Thompson 	if (error != 0)
132502ac6454SAndrew Thompson 		goto fail;
132602ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, 0x3c9000);
132702ac6454SAndrew Thompson 	if (error != 0)
132802ac6454SAndrew Thompson 		goto fail;
132902ac6454SAndrew Thompson 
133002ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR251, 0x3f);
133102ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, 0x06);
133202ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR240, 0x08);
133302ac6454SAndrew Thompson fail:
133402ac6454SAndrew Thompson 	return (error);
133502ac6454SAndrew Thompson }
133602ac6454SAndrew Thompson 
133702ac6454SAndrew Thompson /*
133802ac6454SAndrew Thompson  * AL2210 RF methods.
133902ac6454SAndrew Thompson  */
134002ac6454SAndrew Thompson static int
zyd_al2210_init(struct zyd_rf * rf)134102ac6454SAndrew Thompson zyd_al2210_init(struct zyd_rf *rf)
134202ac6454SAndrew Thompson {
134302ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
134402ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
134502ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_AL2210_RF;
134602ac6454SAndrew Thompson 	uint32_t tmp;
134702ac6454SAndrew Thompson 	int i, error;
134802ac6454SAndrew Thompson 
134902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR18, 2);
135002ac6454SAndrew Thompson 
135102ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
135282e8c646SAdrian Chadd 	for (i = 0; i < nitems(phyini); i++)
135302ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
135402ac6454SAndrew Thompson 
135502ac6454SAndrew Thompson 	/* init AL2210 radio */
135682e8c646SAdrian Chadd 	for (i = 0; i < nitems(rfini); i++) {
135702ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
135802ac6454SAndrew Thompson 			return (error);
135902ac6454SAndrew Thompson 	}
136002ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
136102ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
136202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
136302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
136402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
136502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
136602ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
136702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR18, 3);
136802ac6454SAndrew Thompson fail:
136902ac6454SAndrew Thompson 	return (error);
137002ac6454SAndrew Thompson }
137102ac6454SAndrew Thompson 
137202ac6454SAndrew Thompson static int
zyd_al2210_switch_radio(struct zyd_rf * rf,int on)137302ac6454SAndrew Thompson zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
137402ac6454SAndrew Thompson {
137502ac6454SAndrew Thompson 	/* vendor driver does nothing for this RF chip */
137602ac6454SAndrew Thompson 
137702ac6454SAndrew Thompson 	return (0);
137802ac6454SAndrew Thompson }
137902ac6454SAndrew Thompson 
138002ac6454SAndrew Thompson static int
zyd_al2210_set_channel(struct zyd_rf * rf,uint8_t chan)138102ac6454SAndrew Thompson zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
138202ac6454SAndrew Thompson {
138302ac6454SAndrew Thompson 	int error;
138402ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
138502ac6454SAndrew Thompson 	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
138602ac6454SAndrew Thompson 	uint32_t tmp;
138702ac6454SAndrew Thompson 
138802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR18, 2);
138902ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
139002ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
139102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
139202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
139302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
139402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
139502ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
139602ac6454SAndrew Thompson 
139702ac6454SAndrew Thompson 	/* actually set the channel */
139802ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1]);
139902ac6454SAndrew Thompson 	if (error != 0)
140002ac6454SAndrew Thompson 		goto fail;
140102ac6454SAndrew Thompson 
140202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR18, 3);
140302ac6454SAndrew Thompson fail:
140402ac6454SAndrew Thompson 	return (error);
140502ac6454SAndrew Thompson }
140602ac6454SAndrew Thompson 
140702ac6454SAndrew Thompson /*
140802ac6454SAndrew Thompson  * GCT RF methods.
140902ac6454SAndrew Thompson  */
141002ac6454SAndrew Thompson static int
zyd_gct_init(struct zyd_rf * rf)141102ac6454SAndrew Thompson zyd_gct_init(struct zyd_rf *rf)
141202ac6454SAndrew Thompson {
141378cc4bbbSWeongyo Jeong #define	ZYD_GCT_INTR_REG	0x85c1
141402ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
141502ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
141602ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_GCT_RF;
141778cc4bbbSWeongyo Jeong 	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
141878cc4bbbSWeongyo Jeong 	int i, idx = -1, error;
141978cc4bbbSWeongyo Jeong 	uint16_t data;
142002ac6454SAndrew Thompson 
142102ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
142282e8c646SAdrian Chadd 	for (i = 0; i < nitems(phyini); i++)
142302ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
142402ac6454SAndrew Thompson 
142502ac6454SAndrew Thompson 	/* init cgt radio */
142682e8c646SAdrian Chadd 	for (i = 0; i < nitems(rfini); i++) {
142702ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
142802ac6454SAndrew Thompson 			return (error);
142902ac6454SAndrew Thompson 	}
143078cc4bbbSWeongyo Jeong 
143178cc4bbbSWeongyo Jeong 	error = zyd_gct_mode(rf);
143278cc4bbbSWeongyo Jeong 	if (error != 0)
143378cc4bbbSWeongyo Jeong 		return (error);
143478cc4bbbSWeongyo Jeong 
143582e8c646SAdrian Chadd 	for (i = 0; i < (int)(nitems(vco) - 1); i++) {
143678cc4bbbSWeongyo Jeong 		error = zyd_gct_set_channel_synth(rf, 1, 0);
143778cc4bbbSWeongyo Jeong 		if (error != 0)
143878cc4bbbSWeongyo Jeong 			goto fail;
143978cc4bbbSWeongyo Jeong 		error = zyd_gct_write(rf, vco[i][0]);
144078cc4bbbSWeongyo Jeong 		if (error != 0)
144178cc4bbbSWeongyo Jeong 			goto fail;
144278cc4bbbSWeongyo Jeong 		zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf);
144378cc4bbbSWeongyo Jeong 		zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data);
144478cc4bbbSWeongyo Jeong 		if ((data & 0xf) == 0) {
144578cc4bbbSWeongyo Jeong 			idx = i;
144678cc4bbbSWeongyo Jeong 			break;
144778cc4bbbSWeongyo Jeong 		}
144878cc4bbbSWeongyo Jeong 	}
144978cc4bbbSWeongyo Jeong 	if (idx == -1) {
145078cc4bbbSWeongyo Jeong 		error = zyd_gct_set_channel_synth(rf, 1, 1);
145178cc4bbbSWeongyo Jeong 		if (error != 0)
145278cc4bbbSWeongyo Jeong 			goto fail;
145378cc4bbbSWeongyo Jeong 		error = zyd_gct_write(rf, 0x6662);
145478cc4bbbSWeongyo Jeong 		if (error != 0)
145578cc4bbbSWeongyo Jeong 			goto fail;
145678cc4bbbSWeongyo Jeong 	}
145778cc4bbbSWeongyo Jeong 
145878cc4bbbSWeongyo Jeong 	rf->idx = idx;
145978cc4bbbSWeongyo Jeong 	zyd_write16_m(sc, ZYD_CR203, 0x6);
146002ac6454SAndrew Thompson fail:
146102ac6454SAndrew Thompson 	return (error);
146278cc4bbbSWeongyo Jeong #undef ZYD_GCT_INTR_REG
146378cc4bbbSWeongyo Jeong }
146478cc4bbbSWeongyo Jeong 
146578cc4bbbSWeongyo Jeong static int
zyd_gct_mode(struct zyd_rf * rf)146678cc4bbbSWeongyo Jeong zyd_gct_mode(struct zyd_rf *rf)
146778cc4bbbSWeongyo Jeong {
146878cc4bbbSWeongyo Jeong 	struct zyd_softc *sc = rf->rf_sc;
146978cc4bbbSWeongyo Jeong 	static const uint32_t mode[] = {
147078cc4bbbSWeongyo Jeong 		0x25f98, 0x25f9a, 0x25f94, 0x27fd4
147178cc4bbbSWeongyo Jeong 	};
147278cc4bbbSWeongyo Jeong 	int i, error;
147378cc4bbbSWeongyo Jeong 
147482e8c646SAdrian Chadd 	for (i = 0; i < nitems(mode); i++) {
147578cc4bbbSWeongyo Jeong 		if ((error = zyd_rfwrite(sc, mode[i])) != 0)
147678cc4bbbSWeongyo Jeong 			break;
147778cc4bbbSWeongyo Jeong 	}
147878cc4bbbSWeongyo Jeong 	return (error);
147978cc4bbbSWeongyo Jeong }
148078cc4bbbSWeongyo Jeong 
148178cc4bbbSWeongyo Jeong static int
zyd_gct_set_channel_synth(struct zyd_rf * rf,int chan,int acal)148278cc4bbbSWeongyo Jeong zyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal)
148378cc4bbbSWeongyo Jeong {
148478cc4bbbSWeongyo Jeong 	int error, idx = chan - 1;
148578cc4bbbSWeongyo Jeong 	struct zyd_softc *sc = rf->rf_sc;
148678cc4bbbSWeongyo Jeong 	static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL;
148778cc4bbbSWeongyo Jeong 	static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD;
148878cc4bbbSWeongyo Jeong 	static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV;
148978cc4bbbSWeongyo Jeong 
149078cc4bbbSWeongyo Jeong 	error = zyd_rfwrite(sc,
149178cc4bbbSWeongyo Jeong 	    (acal == 1) ? acal_synth[idx] : std_synth[idx]);
149278cc4bbbSWeongyo Jeong 	if (error != 0)
149378cc4bbbSWeongyo Jeong 		return (error);
149478cc4bbbSWeongyo Jeong 	return zyd_rfwrite(sc, div_synth[idx]);
149578cc4bbbSWeongyo Jeong }
149678cc4bbbSWeongyo Jeong 
149778cc4bbbSWeongyo Jeong static int
zyd_gct_write(struct zyd_rf * rf,uint16_t value)149878cc4bbbSWeongyo Jeong zyd_gct_write(struct zyd_rf *rf, uint16_t value)
149978cc4bbbSWeongyo Jeong {
150078cc4bbbSWeongyo Jeong 	struct zyd_softc *sc = rf->rf_sc;
150178cc4bbbSWeongyo Jeong 
150278cc4bbbSWeongyo Jeong 	return zyd_rfwrite(sc, 0x300000 | 0x40000 | value);
150302ac6454SAndrew Thompson }
150402ac6454SAndrew Thompson 
150502ac6454SAndrew Thompson static int
zyd_gct_switch_radio(struct zyd_rf * rf,int on)150602ac6454SAndrew Thompson zyd_gct_switch_radio(struct zyd_rf *rf, int on)
150702ac6454SAndrew Thompson {
150878cc4bbbSWeongyo Jeong 	int error;
150978cc4bbbSWeongyo Jeong 	struct zyd_softc *sc = rf->rf_sc;
151002ac6454SAndrew Thompson 
151178cc4bbbSWeongyo Jeong 	error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90);
151278cc4bbbSWeongyo Jeong 	if (error != 0)
151378cc4bbbSWeongyo Jeong 		return (error);
151478cc4bbbSWeongyo Jeong 
151578cc4bbbSWeongyo Jeong 	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
151678cc4bbbSWeongyo Jeong 	zyd_write16_m(sc, ZYD_CR251,
151778cc4bbbSWeongyo Jeong 	    on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f);
151878cc4bbbSWeongyo Jeong fail:
151978cc4bbbSWeongyo Jeong 	return (error);
152002ac6454SAndrew Thompson }
152102ac6454SAndrew Thompson 
152202ac6454SAndrew Thompson static int
zyd_gct_set_channel(struct zyd_rf * rf,uint8_t chan)152302ac6454SAndrew Thompson zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
152402ac6454SAndrew Thompson {
152578cc4bbbSWeongyo Jeong 	int error, i;
152602ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
152778cc4bbbSWeongyo Jeong 	static const struct zyd_phy_pair cmd[] = {
152878cc4bbbSWeongyo Jeong 		{ ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 },
152978cc4bbbSWeongyo Jeong 		{ ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 },
153078cc4bbbSWeongyo Jeong 	};
153178cc4bbbSWeongyo Jeong 	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
153202ac6454SAndrew Thompson 
153378cc4bbbSWeongyo Jeong 	error = zyd_gct_set_channel_synth(rf, chan, 0);
153478cc4bbbSWeongyo Jeong 	if (error != 0)
153578cc4bbbSWeongyo Jeong 		goto fail;
153678cc4bbbSWeongyo Jeong 	error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 :
153778cc4bbbSWeongyo Jeong 	    vco[rf->idx][((chan - 1) / 2)]);
153878cc4bbbSWeongyo Jeong 	if (error != 0)
153978cc4bbbSWeongyo Jeong 		goto fail;
154078cc4bbbSWeongyo Jeong 	error = zyd_gct_mode(rf);
154178cc4bbbSWeongyo Jeong 	if (error != 0)
154202ac6454SAndrew Thompson 		return (error);
154382e8c646SAdrian Chadd 	for (i = 0; i < nitems(cmd); i++)
154478cc4bbbSWeongyo Jeong 		zyd_write16_m(sc, cmd[i].reg, cmd[i].val);
154578cc4bbbSWeongyo Jeong 	error = zyd_gct_txgain(rf, chan);
154678cc4bbbSWeongyo Jeong 	if (error != 0)
154778cc4bbbSWeongyo Jeong 		return (error);
154878cc4bbbSWeongyo Jeong 	zyd_write16_m(sc, ZYD_CR203, 0x6);
154902ac6454SAndrew Thompson fail:
155002ac6454SAndrew Thompson 	return (error);
155102ac6454SAndrew Thompson }
155202ac6454SAndrew Thompson 
155302ac6454SAndrew Thompson static int
zyd_gct_txgain(struct zyd_rf * rf,uint8_t chan)155478cc4bbbSWeongyo Jeong zyd_gct_txgain(struct zyd_rf *rf, uint8_t chan)
155502ac6454SAndrew Thompson {
155602ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
155778cc4bbbSWeongyo Jeong 	static uint32_t txgain[] = ZYD_GCT_TXGAIN;
155878cc4bbbSWeongyo Jeong 	uint8_t idx = sc->sc_pwrint[chan - 1];
155902ac6454SAndrew Thompson 
156082e8c646SAdrian Chadd 	if (idx >= nitems(txgain)) {
156178cc4bbbSWeongyo Jeong 		device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n",
156278cc4bbbSWeongyo Jeong 		    chan, idx);
156378cc4bbbSWeongyo Jeong 		return 0;
156402ac6454SAndrew Thompson 	}
156578cc4bbbSWeongyo Jeong 
156678cc4bbbSWeongyo Jeong 	return zyd_rfwrite(sc, 0x700000 | txgain[idx]);
156702ac6454SAndrew Thompson }
156802ac6454SAndrew Thompson 
156902ac6454SAndrew Thompson /*
157002ac6454SAndrew Thompson  * Maxim2 RF methods.
157102ac6454SAndrew Thompson  */
157202ac6454SAndrew Thompson static int
zyd_maxim2_init(struct zyd_rf * rf)157302ac6454SAndrew Thompson zyd_maxim2_init(struct zyd_rf *rf)
157402ac6454SAndrew Thompson {
157502ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
157602ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
157702ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
157802ac6454SAndrew Thompson 	uint16_t tmp;
157902ac6454SAndrew Thompson 	int i, error;
158002ac6454SAndrew Thompson 
158102ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
158282e8c646SAdrian Chadd 	for (i = 0; i < nitems(phyini); i++)
158302ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
158402ac6454SAndrew Thompson 
158502ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
158602ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
158702ac6454SAndrew Thompson 
158802ac6454SAndrew Thompson 	/* init maxim2 radio */
158982e8c646SAdrian Chadd 	for (i = 0; i < nitems(rfini); i++) {
159002ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
159102ac6454SAndrew Thompson 			return (error);
159202ac6454SAndrew Thompson 	}
159302ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
159402ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
159502ac6454SAndrew Thompson fail:
159602ac6454SAndrew Thompson 	return (error);
159702ac6454SAndrew Thompson }
159802ac6454SAndrew Thompson 
159902ac6454SAndrew Thompson static int
zyd_maxim2_switch_radio(struct zyd_rf * rf,int on)160002ac6454SAndrew Thompson zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
160102ac6454SAndrew Thompson {
160202ac6454SAndrew Thompson 
160302ac6454SAndrew Thompson 	/* vendor driver does nothing for this RF chip */
160402ac6454SAndrew Thompson 	return (0);
160502ac6454SAndrew Thompson }
160602ac6454SAndrew Thompson 
160702ac6454SAndrew Thompson static int
zyd_maxim2_set_channel(struct zyd_rf * rf,uint8_t chan)160802ac6454SAndrew Thompson zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
160902ac6454SAndrew Thompson {
161002ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
161102ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
161202ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
161302ac6454SAndrew Thompson 	static const struct {
161402ac6454SAndrew Thompson 		uint32_t	r1, r2;
161502ac6454SAndrew Thompson 	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
161602ac6454SAndrew Thompson 	uint16_t tmp;
161702ac6454SAndrew Thompson 	int i, error;
161802ac6454SAndrew Thompson 
161902ac6454SAndrew Thompson 	/*
162002ac6454SAndrew Thompson 	 * Do the same as we do when initializing it, except for the channel
162102ac6454SAndrew Thompson 	 * values coming from the two channel tables.
162202ac6454SAndrew Thompson 	 */
162302ac6454SAndrew Thompson 
162402ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
162582e8c646SAdrian Chadd 	for (i = 0; i < nitems(phyini); i++)
162602ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
162702ac6454SAndrew Thompson 
162802ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
162902ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
163002ac6454SAndrew Thompson 
163102ac6454SAndrew Thompson 	/* first two values taken from the chantables */
163202ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
163302ac6454SAndrew Thompson 	if (error != 0)
163402ac6454SAndrew Thompson 		goto fail;
163502ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
163602ac6454SAndrew Thompson 	if (error != 0)
163702ac6454SAndrew Thompson 		goto fail;
163802ac6454SAndrew Thompson 
163902ac6454SAndrew Thompson 	/* init maxim2 radio - skipping the two first values */
164082e8c646SAdrian Chadd 	for (i = 2; i < nitems(rfini); i++) {
164102ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
164202ac6454SAndrew Thompson 			return (error);
164302ac6454SAndrew Thompson 	}
164402ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
164502ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
164602ac6454SAndrew Thompson fail:
164702ac6454SAndrew Thompson 	return (error);
164802ac6454SAndrew Thompson }
164902ac6454SAndrew Thompson 
165002ac6454SAndrew Thompson static int
zyd_rf_attach(struct zyd_softc * sc,uint8_t type)165102ac6454SAndrew Thompson zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
165202ac6454SAndrew Thompson {
165302ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
165402ac6454SAndrew Thompson 
165502ac6454SAndrew Thompson 	rf->rf_sc = sc;
165678cc4bbbSWeongyo Jeong 	rf->update_pwr = 1;
165702ac6454SAndrew Thompson 
165802ac6454SAndrew Thompson 	switch (type) {
165902ac6454SAndrew Thompson 	case ZYD_RF_RFMD:
166002ac6454SAndrew Thompson 		rf->init         = zyd_rfmd_init;
166102ac6454SAndrew Thompson 		rf->switch_radio = zyd_rfmd_switch_radio;
166202ac6454SAndrew Thompson 		rf->set_channel  = zyd_rfmd_set_channel;
166302ac6454SAndrew Thompson 		rf->width        = 24;	/* 24-bit RF values */
166402ac6454SAndrew Thompson 		break;
166502ac6454SAndrew Thompson 	case ZYD_RF_AL2230:
166602ac6454SAndrew Thompson 	case ZYD_RF_AL2230S:
166702ac6454SAndrew Thompson 		if (sc->sc_macrev == ZYD_ZD1211B) {
166802ac6454SAndrew Thompson 			rf->init = zyd_al2230_init_b;
166902ac6454SAndrew Thompson 			rf->set_channel = zyd_al2230_set_channel_b;
167002ac6454SAndrew Thompson 		} else {
167102ac6454SAndrew Thompson 			rf->init = zyd_al2230_init;
167202ac6454SAndrew Thompson 			rf->set_channel = zyd_al2230_set_channel;
167302ac6454SAndrew Thompson 		}
167402ac6454SAndrew Thompson 		rf->switch_radio = zyd_al2230_switch_radio;
167502ac6454SAndrew Thompson 		rf->bandedge6	 = zyd_al2230_bandedge6;
167602ac6454SAndrew Thompson 		rf->width        = 24;	/* 24-bit RF values */
167702ac6454SAndrew Thompson 		break;
167802ac6454SAndrew Thompson 	case ZYD_RF_AL7230B:
167902ac6454SAndrew Thompson 		rf->init         = zyd_al7230B_init;
168002ac6454SAndrew Thompson 		rf->switch_radio = zyd_al7230B_switch_radio;
168102ac6454SAndrew Thompson 		rf->set_channel  = zyd_al7230B_set_channel;
168202ac6454SAndrew Thompson 		rf->width        = 24;	/* 24-bit RF values */
168302ac6454SAndrew Thompson 		break;
168402ac6454SAndrew Thompson 	case ZYD_RF_AL2210:
168502ac6454SAndrew Thompson 		rf->init         = zyd_al2210_init;
168602ac6454SAndrew Thompson 		rf->switch_radio = zyd_al2210_switch_radio;
168702ac6454SAndrew Thompson 		rf->set_channel  = zyd_al2210_set_channel;
168802ac6454SAndrew Thompson 		rf->width        = 24;	/* 24-bit RF values */
168902ac6454SAndrew Thompson 		break;
169078cc4bbbSWeongyo Jeong 	case ZYD_RF_MAXIM_NEW:
169102ac6454SAndrew Thompson 	case ZYD_RF_GCT:
169202ac6454SAndrew Thompson 		rf->init         = zyd_gct_init;
169302ac6454SAndrew Thompson 		rf->switch_radio = zyd_gct_switch_radio;
169402ac6454SAndrew Thompson 		rf->set_channel  = zyd_gct_set_channel;
169578cc4bbbSWeongyo Jeong 		rf->width        = 24;	/* 24-bit RF values */
169678cc4bbbSWeongyo Jeong 		rf->update_pwr   = 0;
169702ac6454SAndrew Thompson 		break;
169802ac6454SAndrew Thompson 	case ZYD_RF_MAXIM_NEW2:
169902ac6454SAndrew Thompson 		rf->init         = zyd_maxim2_init;
170002ac6454SAndrew Thompson 		rf->switch_radio = zyd_maxim2_switch_radio;
170102ac6454SAndrew Thompson 		rf->set_channel  = zyd_maxim2_set_channel;
170202ac6454SAndrew Thompson 		rf->width        = 18;	/* 18-bit RF values */
170302ac6454SAndrew Thompson 		break;
170402ac6454SAndrew Thompson 	default:
170502ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
170602ac6454SAndrew Thompson 		    "sorry, radio \"%s\" is not supported yet\n",
170702ac6454SAndrew Thompson 		    zyd_rf_name(type));
170802ac6454SAndrew Thompson 		return (EINVAL);
170902ac6454SAndrew Thompson 	}
171002ac6454SAndrew Thompson 	return (0);
171102ac6454SAndrew Thompson }
171202ac6454SAndrew Thompson 
171302ac6454SAndrew Thompson static const char *
zyd_rf_name(uint8_t type)171402ac6454SAndrew Thompson zyd_rf_name(uint8_t type)
171502ac6454SAndrew Thompson {
171602ac6454SAndrew Thompson 	static const char * const zyd_rfs[] = {
171702ac6454SAndrew Thompson 		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
171802ac6454SAndrew Thompson 		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
171902ac6454SAndrew Thompson 		"AL2230S",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
172002ac6454SAndrew Thompson 		"PHILIPS"
172102ac6454SAndrew Thompson 	};
172202ac6454SAndrew Thompson 
172302ac6454SAndrew Thompson 	return zyd_rfs[(type > 15) ? 0 : type];
172402ac6454SAndrew Thompson }
172502ac6454SAndrew Thompson 
172602ac6454SAndrew Thompson static int
zyd_hw_init(struct zyd_softc * sc)172702ac6454SAndrew Thompson zyd_hw_init(struct zyd_softc *sc)
172802ac6454SAndrew Thompson {
172902ac6454SAndrew Thompson 	int error;
173002ac6454SAndrew Thompson 	const struct zyd_phy_pair *phyp;
173102ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
173202ac6454SAndrew Thompson 	uint16_t val;
173302ac6454SAndrew Thompson 
173402ac6454SAndrew Thompson 	/* specify that the plug and play is finished */
173502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
173602ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
173702ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
173802ac6454SAndrew Thompson 	    sc->sc_fwbase);
173902ac6454SAndrew Thompson 
174002ac6454SAndrew Thompson 	/* retrieve firmware revision number */
174102ac6454SAndrew Thompson 	zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
174202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
174302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
174402ac6454SAndrew Thompson 	/* set mandatory rates - XXX assumes 802.11b/g */
174502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
174602ac6454SAndrew Thompson 
174702ac6454SAndrew Thompson 	/* disable interrupts */
174802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
174902ac6454SAndrew Thompson 
175002ac6454SAndrew Thompson 	if ((error = zyd_read_pod(sc)) != 0) {
175102ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM\n");
175202ac6454SAndrew Thompson 		goto fail;
175302ac6454SAndrew Thompson 	}
175402ac6454SAndrew Thompson 
175502ac6454SAndrew Thompson 	/* PHY init (resetting) */
175602ac6454SAndrew Thompson 	error = zyd_lock_phy(sc);
175702ac6454SAndrew Thompson 	if (error != 0)
175802ac6454SAndrew Thompson 		goto fail;
175902ac6454SAndrew Thompson 	phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
176002ac6454SAndrew Thompson 	for (; phyp->reg != 0; phyp++)
176102ac6454SAndrew Thompson 		zyd_write16_m(sc, phyp->reg, phyp->val);
176202ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
176302ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
176402ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_CR157, val >> 8);
176502ac6454SAndrew Thompson 	}
176602ac6454SAndrew Thompson 	error = zyd_unlock_phy(sc);
176702ac6454SAndrew Thompson 	if (error != 0)
176802ac6454SAndrew Thompson 		goto fail;
176902ac6454SAndrew Thompson 
177002ac6454SAndrew Thompson 	/* HMAC init */
177102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
177202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
177302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
177402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
177502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
177602ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
177702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
177802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
177902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
178002ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
178102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
178202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
178302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
178402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
178502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
178602ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
178702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
178802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
178902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
179002ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
179102ac6454SAndrew Thompson 
179202ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211) {
179302ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
179402ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
179502ac6454SAndrew Thompson 	} else {
179602ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
179702ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
179802ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
179902ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
180002ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
180102ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
180202ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
180302ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
180402ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
180502ac6454SAndrew Thompson 	}
180602ac6454SAndrew Thompson 
180702ac6454SAndrew Thompson 	/* init beacon interval to 100ms */
180802ac6454SAndrew Thompson 	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
180902ac6454SAndrew Thompson 		goto fail;
181002ac6454SAndrew Thompson 
181102ac6454SAndrew Thompson 	if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
181202ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
181302ac6454SAndrew Thompson 		    sc->sc_rfrev);
181402ac6454SAndrew Thompson 		goto fail;
181502ac6454SAndrew Thompson 	}
181602ac6454SAndrew Thompson 
181702ac6454SAndrew Thompson 	/* RF chip init */
181802ac6454SAndrew Thompson 	error = zyd_lock_phy(sc);
181902ac6454SAndrew Thompson 	if (error != 0)
182002ac6454SAndrew Thompson 		goto fail;
182102ac6454SAndrew Thompson 	error = (*rf->init)(rf);
182202ac6454SAndrew Thompson 	if (error != 0) {
182302ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
182402ac6454SAndrew Thompson 		    "radio initialization failed, error %d\n", error);
182502ac6454SAndrew Thompson 		goto fail;
182602ac6454SAndrew Thompson 	}
182702ac6454SAndrew Thompson 	error = zyd_unlock_phy(sc);
182802ac6454SAndrew Thompson 	if (error != 0)
182902ac6454SAndrew Thompson 		goto fail;
183002ac6454SAndrew Thompson 
183102ac6454SAndrew Thompson 	if ((error = zyd_read_eeprom(sc)) != 0) {
183202ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM\n");
183302ac6454SAndrew Thompson 		goto fail;
183402ac6454SAndrew Thompson 	}
183502ac6454SAndrew Thompson 
183602ac6454SAndrew Thompson fail:	return (error);
183702ac6454SAndrew Thompson }
183802ac6454SAndrew Thompson 
183902ac6454SAndrew Thompson static int
zyd_read_pod(struct zyd_softc * sc)184002ac6454SAndrew Thompson zyd_read_pod(struct zyd_softc *sc)
184102ac6454SAndrew Thompson {
184202ac6454SAndrew Thompson 	int error;
184302ac6454SAndrew Thompson 	uint32_t tmp;
184402ac6454SAndrew Thompson 
184502ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
184602ac6454SAndrew Thompson 	sc->sc_rfrev     = tmp & 0x0f;
184702ac6454SAndrew Thompson 	sc->sc_ledtype   = (tmp >>  4) & 0x01;
184802ac6454SAndrew Thompson 	sc->sc_al2230s   = (tmp >>  7) & 0x01;
184902ac6454SAndrew Thompson 	sc->sc_cckgain   = (tmp >>  8) & 0x01;
185002ac6454SAndrew Thompson 	sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
185102ac6454SAndrew Thompson 	sc->sc_parev     = (tmp >> 16) & 0x0f;
185202ac6454SAndrew Thompson 	sc->sc_bandedge6 = (tmp >> 21) & 0x01;
185302ac6454SAndrew Thompson 	sc->sc_newphy    = (tmp >> 31) & 0x01;
185402ac6454SAndrew Thompson 	sc->sc_txled     = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
185502ac6454SAndrew Thompson fail:
185602ac6454SAndrew Thompson 	return (error);
185702ac6454SAndrew Thompson }
185802ac6454SAndrew Thompson 
185902ac6454SAndrew Thompson static int
zyd_read_eeprom(struct zyd_softc * sc)186002ac6454SAndrew Thompson zyd_read_eeprom(struct zyd_softc *sc)
186102ac6454SAndrew Thompson {
186202ac6454SAndrew Thompson 	uint16_t val;
186302ac6454SAndrew Thompson 	int error, i;
186402ac6454SAndrew Thompson 
186502ac6454SAndrew Thompson 	/* read Tx power calibration tables */
186602ac6454SAndrew Thompson 	for (i = 0; i < 7; i++) {
186702ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
186802ac6454SAndrew Thompson 		sc->sc_pwrcal[i * 2] = val >> 8;
186902ac6454SAndrew Thompson 		sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
187002ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
187102ac6454SAndrew Thompson 		sc->sc_pwrint[i * 2] = val >> 8;
187202ac6454SAndrew Thompson 		sc->sc_pwrint[i * 2 + 1] = val & 0xff;
187302ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
187402ac6454SAndrew Thompson 		sc->sc_ofdm36_cal[i * 2] = val >> 8;
187502ac6454SAndrew Thompson 		sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
187602ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
187702ac6454SAndrew Thompson 		sc->sc_ofdm48_cal[i * 2] = val >> 8;
187802ac6454SAndrew Thompson 		sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
187902ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
188002ac6454SAndrew Thompson 		sc->sc_ofdm54_cal[i * 2] = val >> 8;
188102ac6454SAndrew Thompson 		sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
188202ac6454SAndrew Thompson 	}
188302ac6454SAndrew Thompson fail:
188402ac6454SAndrew Thompson 	return (error);
188502ac6454SAndrew Thompson }
188602ac6454SAndrew Thompson 
188702ac6454SAndrew Thompson static int
zyd_get_macaddr(struct zyd_softc * sc)188802ac6454SAndrew Thompson zyd_get_macaddr(struct zyd_softc *sc)
188902ac6454SAndrew Thompson {
1890760bc48eSAndrew Thompson 	struct usb_device_request req;
1891e0a69b51SAndrew Thompson 	usb_error_t error;
189202ac6454SAndrew Thompson 
189302ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
189402ac6454SAndrew Thompson 	req.bRequest = ZYD_READFWDATAREQ;
189502ac6454SAndrew Thompson 	USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
189602ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
189702ac6454SAndrew Thompson 	USETW(req.wLength, IEEE80211_ADDR_LEN);
189802ac6454SAndrew Thompson 
18997a79cebfSGleb Smirnoff 	error = zyd_do_request(sc, &req, sc->sc_ic.ic_macaddr);
190002ac6454SAndrew Thompson 	if (error != 0) {
190102ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1902a593f6b8SAndrew Thompson 		    usbd_errstr(error));
190302ac6454SAndrew Thompson 	}
190402ac6454SAndrew Thompson 
190502ac6454SAndrew Thompson 	return (error);
190602ac6454SAndrew Thompson }
190702ac6454SAndrew Thompson 
190802ac6454SAndrew Thompson static int
zyd_set_macaddr(struct zyd_softc * sc,const uint8_t * addr)190902ac6454SAndrew Thompson zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
191002ac6454SAndrew Thompson {
191102ac6454SAndrew Thompson 	int error;
191202ac6454SAndrew Thompson 	uint32_t tmp;
191302ac6454SAndrew Thompson 
191402ac6454SAndrew Thompson 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
191502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
191602ac6454SAndrew Thompson 	tmp = addr[5] << 8 | addr[4];
191702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
191802ac6454SAndrew Thompson fail:
191902ac6454SAndrew Thompson 	return (error);
192002ac6454SAndrew Thompson }
192102ac6454SAndrew Thompson 
192202ac6454SAndrew Thompson static int
zyd_set_bssid(struct zyd_softc * sc,const uint8_t * addr)192302ac6454SAndrew Thompson zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
192402ac6454SAndrew Thompson {
192502ac6454SAndrew Thompson 	int error;
192602ac6454SAndrew Thompson 	uint32_t tmp;
192702ac6454SAndrew Thompson 
192802ac6454SAndrew Thompson 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
192902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
193002ac6454SAndrew Thompson 	tmp = addr[5] << 8 | addr[4];
193102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
193202ac6454SAndrew Thompson fail:
193302ac6454SAndrew Thompson 	return (error);
193402ac6454SAndrew Thompson }
193502ac6454SAndrew Thompson 
193602ac6454SAndrew Thompson static int
zyd_switch_radio(struct zyd_softc * sc,int on)193702ac6454SAndrew Thompson zyd_switch_radio(struct zyd_softc *sc, int on)
193802ac6454SAndrew Thompson {
193902ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
194002ac6454SAndrew Thompson 	int error;
194102ac6454SAndrew Thompson 
194202ac6454SAndrew Thompson 	error = zyd_lock_phy(sc);
194302ac6454SAndrew Thompson 	if (error != 0)
194402ac6454SAndrew Thompson 		goto fail;
194502ac6454SAndrew Thompson 	error = (*rf->switch_radio)(rf, on);
194602ac6454SAndrew Thompson 	if (error != 0)
194702ac6454SAndrew Thompson 		goto fail;
194802ac6454SAndrew Thompson 	error = zyd_unlock_phy(sc);
194902ac6454SAndrew Thompson fail:
195002ac6454SAndrew Thompson 	return (error);
195102ac6454SAndrew Thompson }
195202ac6454SAndrew Thompson 
195302ac6454SAndrew Thompson static int
zyd_set_led(struct zyd_softc * sc,int which,int on)195402ac6454SAndrew Thompson zyd_set_led(struct zyd_softc *sc, int which, int on)
195502ac6454SAndrew Thompson {
195602ac6454SAndrew Thompson 	int error;
195702ac6454SAndrew Thompson 	uint32_t tmp;
195802ac6454SAndrew Thompson 
195902ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
196002ac6454SAndrew Thompson 	tmp &= ~which;
196102ac6454SAndrew Thompson 	if (on)
196202ac6454SAndrew Thompson 		tmp |= which;
196302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
196402ac6454SAndrew Thompson fail:
196502ac6454SAndrew Thompson 	return (error);
196602ac6454SAndrew Thompson }
196702ac6454SAndrew Thompson 
19683e7e8744SGleb Smirnoff static u_int
zyd_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)19693e7e8744SGleb Smirnoff zyd_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
19703e7e8744SGleb Smirnoff {
19713e7e8744SGleb Smirnoff 	uint32_t *hash = arg;
19723e7e8744SGleb Smirnoff 	uint8_t v;
19733e7e8744SGleb Smirnoff 
19743e7e8744SGleb Smirnoff 	v = ((uint8_t *)LLADDR(sdl))[5] >> 2;
19753e7e8744SGleb Smirnoff 	if (v < 32)
19763e7e8744SGleb Smirnoff 		hash[0] |= 1 << v;
19773e7e8744SGleb Smirnoff 	else
19783e7e8744SGleb Smirnoff 		hash[1] |= 1 << (v - 32);
19793e7e8744SGleb Smirnoff 
19803e7e8744SGleb Smirnoff 	return (1);
19813e7e8744SGleb Smirnoff }
19823e7e8744SGleb Smirnoff 
198302ac6454SAndrew Thompson static void
zyd_set_multi(struct zyd_softc * sc)198402ac6454SAndrew Thompson zyd_set_multi(struct zyd_softc *sc)
198502ac6454SAndrew Thompson {
19867a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
19873e7e8744SGleb Smirnoff 	uint32_t hash[2];
19887a79cebfSGleb Smirnoff 	int error;
198902ac6454SAndrew Thompson 
19907a79cebfSGleb Smirnoff 	if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0)
199102ac6454SAndrew Thompson 		return;
199202ac6454SAndrew Thompson 
19933e7e8744SGleb Smirnoff 	hash[0] = 0x00000000;
19943e7e8744SGleb Smirnoff 	hash[1] = 0x80000000;
199502ac6454SAndrew Thompson 
19967a79cebfSGleb Smirnoff 	if (ic->ic_opmode == IEEE80211_M_MONITOR || ic->ic_allmulti > 0 ||
19977a79cebfSGleb Smirnoff 	    ic->ic_promisc > 0) {
19983e7e8744SGleb Smirnoff 		hash[0] = 0xffffffff;
19993e7e8744SGleb Smirnoff 		hash[1] = 0xffffffff;
200002ac6454SAndrew Thompson 	} else {
20017a79cebfSGleb Smirnoff 		struct ieee80211vap *vap;
20027a79cebfSGleb Smirnoff 
20033e7e8744SGleb Smirnoff 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
20043e7e8744SGleb Smirnoff 			if_foreach_llmaddr(vap->iv_ifp, zyd_hash_maddr, &hash);
20057a79cebfSGleb Smirnoff 	}
200602ac6454SAndrew Thompson 
200702ac6454SAndrew Thompson 	/* reprogram multicast global hash table */
20083e7e8744SGleb Smirnoff 	zyd_write32_m(sc, ZYD_MAC_GHTBL, hash[0]);
20093e7e8744SGleb Smirnoff 	zyd_write32_m(sc, ZYD_MAC_GHTBH, hash[1]);
201002ac6454SAndrew Thompson fail:
201102ac6454SAndrew Thompson 	if (error != 0)
201202ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
201302ac6454SAndrew Thompson 		    "could not set multicast hash table\n");
201402ac6454SAndrew Thompson }
201502ac6454SAndrew Thompson 
201602ac6454SAndrew Thompson static void
zyd_update_mcast(struct ieee80211com * ic)2017272f6adeSGleb Smirnoff zyd_update_mcast(struct ieee80211com *ic)
201802ac6454SAndrew Thompson {
2019272f6adeSGleb Smirnoff 	struct zyd_softc *sc = ic->ic_softc;
202002ac6454SAndrew Thompson 
202102ac6454SAndrew Thompson 	ZYD_LOCK(sc);
20225efea30fSAndrew Thompson 	zyd_set_multi(sc);
202302ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
202402ac6454SAndrew Thompson }
202502ac6454SAndrew Thompson 
202602ac6454SAndrew Thompson static int
zyd_set_rxfilter(struct zyd_softc * sc)202702ac6454SAndrew Thompson zyd_set_rxfilter(struct zyd_softc *sc)
202802ac6454SAndrew Thompson {
20297a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
203002ac6454SAndrew Thompson 	uint32_t rxfilter;
203102ac6454SAndrew Thompson 
203202ac6454SAndrew Thompson 	switch (ic->ic_opmode) {
203302ac6454SAndrew Thompson 	case IEEE80211_M_STA:
203402ac6454SAndrew Thompson 		rxfilter = ZYD_FILTER_BSS;
203502ac6454SAndrew Thompson 		break;
203602ac6454SAndrew Thompson 	case IEEE80211_M_IBSS:
203702ac6454SAndrew Thompson 	case IEEE80211_M_HOSTAP:
203802ac6454SAndrew Thompson 		rxfilter = ZYD_FILTER_HOSTAP;
203902ac6454SAndrew Thompson 		break;
204002ac6454SAndrew Thompson 	case IEEE80211_M_MONITOR:
204102ac6454SAndrew Thompson 		rxfilter = ZYD_FILTER_MONITOR;
204202ac6454SAndrew Thompson 		break;
204302ac6454SAndrew Thompson 	default:
204402ac6454SAndrew Thompson 		/* should not get there */
204502ac6454SAndrew Thompson 		return (EINVAL);
204602ac6454SAndrew Thompson 	}
204702ac6454SAndrew Thompson 	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
204802ac6454SAndrew Thompson }
204902ac6454SAndrew Thompson 
205002ac6454SAndrew Thompson static void
zyd_set_chan(struct zyd_softc * sc,struct ieee80211_channel * c)205102ac6454SAndrew Thompson zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
205202ac6454SAndrew Thompson {
205302ac6454SAndrew Thompson 	int error;
20547a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
205502ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
205602ac6454SAndrew Thompson 	uint32_t tmp;
205702ac6454SAndrew Thompson 	int chan;
205802ac6454SAndrew Thompson 
205902ac6454SAndrew Thompson 	chan = ieee80211_chan2ieee(ic, c);
206002ac6454SAndrew Thompson 	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
206102ac6454SAndrew Thompson 		/* XXX should NEVER happen */
206202ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
206302ac6454SAndrew Thompson 		    "%s: invalid channel %x\n", __func__, chan);
206402ac6454SAndrew Thompson 		return;
206502ac6454SAndrew Thompson 	}
206602ac6454SAndrew Thompson 
206702ac6454SAndrew Thompson 	error = zyd_lock_phy(sc);
206802ac6454SAndrew Thompson 	if (error != 0)
206902ac6454SAndrew Thompson 		goto fail;
207002ac6454SAndrew Thompson 
207102ac6454SAndrew Thompson 	error = (*rf->set_channel)(rf, chan);
207202ac6454SAndrew Thompson 	if (error != 0)
207302ac6454SAndrew Thompson 		goto fail;
207402ac6454SAndrew Thompson 
207578cc4bbbSWeongyo Jeong 	if (rf->update_pwr) {
207602ac6454SAndrew Thompson 		/* update Tx power */
207702ac6454SAndrew Thompson 		zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
207802ac6454SAndrew Thompson 
207902ac6454SAndrew Thompson 		if (sc->sc_macrev == ZYD_ZD1211B) {
208078cc4bbbSWeongyo Jeong 			zyd_write16_m(sc, ZYD_CR67,
208178cc4bbbSWeongyo Jeong 			    sc->sc_ofdm36_cal[chan - 1]);
208278cc4bbbSWeongyo Jeong 			zyd_write16_m(sc, ZYD_CR66,
208378cc4bbbSWeongyo Jeong 			    sc->sc_ofdm48_cal[chan - 1]);
208478cc4bbbSWeongyo Jeong 			zyd_write16_m(sc, ZYD_CR65,
208578cc4bbbSWeongyo Jeong 			    sc->sc_ofdm54_cal[chan - 1]);
208602ac6454SAndrew Thompson 			zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
208702ac6454SAndrew Thompson 			zyd_write16_m(sc, ZYD_CR69, 0x28);
208802ac6454SAndrew Thompson 			zyd_write16_m(sc, ZYD_CR69, 0x2a);
208902ac6454SAndrew Thompson 		}
209078cc4bbbSWeongyo Jeong 	}
209102ac6454SAndrew Thompson 	if (sc->sc_cckgain) {
209202ac6454SAndrew Thompson 		/* set CCK baseband gain from EEPROM */
209302ac6454SAndrew Thompson 		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
209402ac6454SAndrew Thompson 			zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
209502ac6454SAndrew Thompson 	}
209602ac6454SAndrew Thompson 	if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
209702ac6454SAndrew Thompson 		error = (*rf->bandedge6)(rf, c);
209802ac6454SAndrew Thompson 		if (error != 0)
209902ac6454SAndrew Thompson 			goto fail;
210002ac6454SAndrew Thompson 	}
210102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
210202ac6454SAndrew Thompson 
210302ac6454SAndrew Thompson 	error = zyd_unlock_phy(sc);
210402ac6454SAndrew Thompson 	if (error != 0)
210502ac6454SAndrew Thompson 		goto fail;
210602ac6454SAndrew Thompson 
210702ac6454SAndrew Thompson 	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
210802ac6454SAndrew Thompson 	    htole16(c->ic_freq);
210902ac6454SAndrew Thompson 	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
211002ac6454SAndrew Thompson 	    htole16(c->ic_flags);
211102ac6454SAndrew Thompson fail:
211202ac6454SAndrew Thompson 	return;
211302ac6454SAndrew Thompson }
211402ac6454SAndrew Thompson 
211502ac6454SAndrew Thompson static int
zyd_set_beacon_interval(struct zyd_softc * sc,int bintval)211602ac6454SAndrew Thompson zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
211702ac6454SAndrew Thompson {
211802ac6454SAndrew Thompson 	int error;
211902ac6454SAndrew Thompson 	uint32_t val;
212002ac6454SAndrew Thompson 
212102ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
212202ac6454SAndrew Thompson 	sc->sc_atim_wnd = val;
212302ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
212402ac6454SAndrew Thompson 	sc->sc_pre_tbtt = val;
212502ac6454SAndrew Thompson 	sc->sc_bcn_int = bintval;
212602ac6454SAndrew Thompson 
212702ac6454SAndrew Thompson 	if (sc->sc_bcn_int <= 5)
212802ac6454SAndrew Thompson 		sc->sc_bcn_int = 5;
212902ac6454SAndrew Thompson 	if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
213002ac6454SAndrew Thompson 		sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
213102ac6454SAndrew Thompson 	if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
213202ac6454SAndrew Thompson 		sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
213302ac6454SAndrew Thompson 
213402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
213502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
213602ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
213702ac6454SAndrew Thompson fail:
213802ac6454SAndrew Thompson 	return (error);
213902ac6454SAndrew Thompson }
214002ac6454SAndrew Thompson 
214102ac6454SAndrew Thompson static void
zyd_rx_data(struct usb_xfer * xfer,int offset,uint16_t len)2142760bc48eSAndrew Thompson zyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
214302ac6454SAndrew Thompson {
2144ed6d949aSAndrew Thompson 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
21457a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
214602ac6454SAndrew Thompson 	struct zyd_plcphdr plcp;
214702ac6454SAndrew Thompson 	struct zyd_rx_stat stat;
2148ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
214902ac6454SAndrew Thompson 	struct mbuf *m;
215002ac6454SAndrew Thompson 	int rlen, rssi;
215102ac6454SAndrew Thompson 
215202ac6454SAndrew Thompson 	if (len < ZYD_MIN_FRAGSZ) {
215302ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
215402ac6454SAndrew Thompson 		    device_get_nameunit(sc->sc_dev), len);
21557a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_ierrors, 1);
215602ac6454SAndrew Thompson 		return;
215702ac6454SAndrew Thompson 	}
2158ed6d949aSAndrew Thompson 	pc = usbd_xfer_get_frame(xfer, 0);
2159ed6d949aSAndrew Thompson 	usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2160ed6d949aSAndrew Thompson 	usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
216102ac6454SAndrew Thompson 
216202ac6454SAndrew Thompson 	if (stat.flags & ZYD_RX_ERROR) {
216302ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_RECV,
216402ac6454SAndrew Thompson 		    "%s: RX status indicated error (%x)\n",
216502ac6454SAndrew Thompson 		    device_get_nameunit(sc->sc_dev), stat.flags);
21667a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_ierrors, 1);
216702ac6454SAndrew Thompson 		return;
216802ac6454SAndrew Thompson 	}
216902ac6454SAndrew Thompson 
217002ac6454SAndrew Thompson 	/* compute actual frame length */
217102ac6454SAndrew Thompson 	rlen = len - sizeof(struct zyd_plcphdr) -
217202ac6454SAndrew Thompson 	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
217302ac6454SAndrew Thompson 
217402ac6454SAndrew Thompson 	/* allocate a mbuf to store the frame */
21756d917491SHans Petter Selasky 	if (rlen > (int)MCLBYTES) {
217602ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
217702ac6454SAndrew Thompson 		    device_get_nameunit(sc->sc_dev), rlen);
21787a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_ierrors, 1);
217902ac6454SAndrew Thompson 		return;
21806d917491SHans Petter Selasky 	} else if (rlen > (int)MHLEN)
2181c6499eccSGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
218202ac6454SAndrew Thompson 	else
2183c6499eccSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
218402ac6454SAndrew Thompson 	if (m == NULL) {
218502ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
218602ac6454SAndrew Thompson 		    device_get_nameunit(sc->sc_dev));
21877a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_ierrors, 1);
218802ac6454SAndrew Thompson 		return;
218902ac6454SAndrew Thompson 	}
219002ac6454SAndrew Thompson 	m->m_pkthdr.len = m->m_len = rlen;
2191ed6d949aSAndrew Thompson 	usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
219202ac6454SAndrew Thompson 
21935463c4a4SSam Leffler 	if (ieee80211_radiotap_active(ic)) {
219402ac6454SAndrew Thompson 		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
219502ac6454SAndrew Thompson 
219602ac6454SAndrew Thompson 		tap->wr_flags = 0;
219702ac6454SAndrew Thompson 		if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
219802ac6454SAndrew Thompson 			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
219902ac6454SAndrew Thompson 		/* XXX toss, no way to express errors */
220002ac6454SAndrew Thompson 		if (stat.flags & ZYD_RX_DECRYPTERR)
220102ac6454SAndrew Thompson 			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
220202ac6454SAndrew Thompson 		tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
220302ac6454SAndrew Thompson 		    (stat.flags & ZYD_RX_OFDM) ?
220402ac6454SAndrew Thompson 			IEEE80211_T_OFDM : IEEE80211_T_CCK);
220502ac6454SAndrew Thompson 		tap->wr_antsignal = stat.rssi + -95;
220602ac6454SAndrew Thompson 		tap->wr_antnoise = -95;	/* XXX */
220702ac6454SAndrew Thompson 	}
220802ac6454SAndrew Thompson 	rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
220902ac6454SAndrew Thompson 
221002ac6454SAndrew Thompson 	sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
221102ac6454SAndrew Thompson 	sc->sc_rx_data[sc->sc_rx_count].m = m;
221202ac6454SAndrew Thompson 	sc->sc_rx_count++;
221302ac6454SAndrew Thompson }
221402ac6454SAndrew Thompson 
221502ac6454SAndrew Thompson static void
zyd_bulk_read_callback(struct usb_xfer * xfer,usb_error_t error)2216ed6d949aSAndrew Thompson zyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
221702ac6454SAndrew Thompson {
2218ed6d949aSAndrew Thompson 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
22197a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
222002ac6454SAndrew Thompson 	struct ieee80211_node *ni;
222102ac6454SAndrew Thompson 	struct zyd_rx_desc desc;
222202ac6454SAndrew Thompson 	struct mbuf *m;
2223ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
222402ac6454SAndrew Thompson 	uint32_t offset;
222502ac6454SAndrew Thompson 	uint8_t rssi;
222602ac6454SAndrew Thompson 	int8_t nf;
222702ac6454SAndrew Thompson 	int i;
2228ed6d949aSAndrew Thompson 	int actlen;
2229ed6d949aSAndrew Thompson 
2230ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
223102ac6454SAndrew Thompson 
223202ac6454SAndrew Thompson 	sc->sc_rx_count = 0;
223302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
223402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
2235ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
2236ed6d949aSAndrew Thompson 		usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
223702ac6454SAndrew Thompson 
223802ac6454SAndrew Thompson 		offset = 0;
223902ac6454SAndrew Thompson 		if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
224002ac6454SAndrew Thompson 			DPRINTF(sc, ZYD_DEBUG_RECV,
224102ac6454SAndrew Thompson 			    "%s: received multi-frame transfer\n", __func__);
224202ac6454SAndrew Thompson 
224302ac6454SAndrew Thompson 			for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
224402ac6454SAndrew Thompson 				uint16_t len16 = UGETW(desc.len[i]);
224502ac6454SAndrew Thompson 
2246ed6d949aSAndrew Thompson 				if (len16 == 0 || len16 > actlen)
224702ac6454SAndrew Thompson 					break;
224802ac6454SAndrew Thompson 
224902ac6454SAndrew Thompson 				zyd_rx_data(xfer, offset, len16);
225002ac6454SAndrew Thompson 
225102ac6454SAndrew Thompson 				/* next frame is aligned on a 32-bit boundary */
225202ac6454SAndrew Thompson 				len16 = (len16 + 3) & ~3;
225302ac6454SAndrew Thompson 				offset += len16;
2254ed6d949aSAndrew Thompson 				if (len16 > actlen)
225502ac6454SAndrew Thompson 					break;
2256ed6d949aSAndrew Thompson 				actlen -= len16;
225702ac6454SAndrew Thompson 			}
225802ac6454SAndrew Thompson 		} else {
225902ac6454SAndrew Thompson 			DPRINTF(sc, ZYD_DEBUG_RECV,
226002ac6454SAndrew Thompson 			    "%s: received single-frame transfer\n", __func__);
226102ac6454SAndrew Thompson 
2262ed6d949aSAndrew Thompson 			zyd_rx_data(xfer, 0, actlen);
226302ac6454SAndrew Thompson 		}
226402ac6454SAndrew Thompson 		/* FALLTHROUGH */
226502ac6454SAndrew Thompson 	case USB_ST_SETUP:
226602ac6454SAndrew Thompson tr_setup:
2267ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2268a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
226902ac6454SAndrew Thompson 
227002ac6454SAndrew Thompson 		/*
227102ac6454SAndrew Thompson 		 * At the end of a USB callback it is always safe to unlock
227202ac6454SAndrew Thompson 		 * the private mutex of a device! That is why we do the
227302ac6454SAndrew Thompson 		 * "ieee80211_input" here, and not some lines up!
227402ac6454SAndrew Thompson 		 */
227502ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
227602ac6454SAndrew Thompson 		for (i = 0; i < sc->sc_rx_count; i++) {
227702ac6454SAndrew Thompson 			rssi = sc->sc_rx_data[i].rssi;
227802ac6454SAndrew Thompson 			m = sc->sc_rx_data[i].m;
227902ac6454SAndrew Thompson 			sc->sc_rx_data[i].m = NULL;
228002ac6454SAndrew Thompson 
228102ac6454SAndrew Thompson 			nf = -95;	/* XXX */
228202ac6454SAndrew Thompson 
228302ac6454SAndrew Thompson 			ni = ieee80211_find_rxnode(ic,
228402ac6454SAndrew Thompson 			    mtod(m, struct ieee80211_frame_min *));
228502ac6454SAndrew Thompson 			if (ni != NULL) {
22865463c4a4SSam Leffler 				(void)ieee80211_input(ni, m, rssi, nf);
228702ac6454SAndrew Thompson 				ieee80211_free_node(ni);
228802ac6454SAndrew Thompson 			} else
22895463c4a4SSam Leffler 				(void)ieee80211_input_all(ic, m, rssi, nf);
229002ac6454SAndrew Thompson 		}
229102ac6454SAndrew Thompson 		ZYD_LOCK(sc);
22927a79cebfSGleb Smirnoff 		zyd_start(sc);
229302ac6454SAndrew Thompson 		break;
229402ac6454SAndrew Thompson 
229502ac6454SAndrew Thompson 	default:			/* Error */
2296ed6d949aSAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
229702ac6454SAndrew Thompson 
2298ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
229902ac6454SAndrew Thompson 			/* try to clear stall first */
2300ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
230102ac6454SAndrew Thompson 			goto tr_setup;
230202ac6454SAndrew Thompson 		}
230302ac6454SAndrew Thompson 		break;
230402ac6454SAndrew Thompson 	}
230502ac6454SAndrew Thompson }
230602ac6454SAndrew Thompson 
230702ac6454SAndrew Thompson static uint8_t
zyd_plcp_signal(struct zyd_softc * sc,int rate)2308e87b19e3SWeongyo Jeong zyd_plcp_signal(struct zyd_softc *sc, int rate)
230902ac6454SAndrew Thompson {
231002ac6454SAndrew Thompson 	switch (rate) {
231102ac6454SAndrew Thompson 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
231202ac6454SAndrew Thompson 	case 12:
231302ac6454SAndrew Thompson 		return (0xb);
231402ac6454SAndrew Thompson 	case 18:
231502ac6454SAndrew Thompson 		return (0xf);
231602ac6454SAndrew Thompson 	case 24:
231702ac6454SAndrew Thompson 		return (0xa);
231802ac6454SAndrew Thompson 	case 36:
231902ac6454SAndrew Thompson 		return (0xe);
232002ac6454SAndrew Thompson 	case 48:
232102ac6454SAndrew Thompson 		return (0x9);
232202ac6454SAndrew Thompson 	case 72:
232302ac6454SAndrew Thompson 		return (0xd);
232402ac6454SAndrew Thompson 	case 96:
232502ac6454SAndrew Thompson 		return (0x8);
232602ac6454SAndrew Thompson 	case 108:
232702ac6454SAndrew Thompson 		return (0xc);
232802ac6454SAndrew Thompson 	/* CCK rates (NB: not IEEE std, device-specific) */
232902ac6454SAndrew Thompson 	case 2:
233002ac6454SAndrew Thompson 		return (0x0);
233102ac6454SAndrew Thompson 	case 4:
233202ac6454SAndrew Thompson 		return (0x1);
233302ac6454SAndrew Thompson 	case 11:
233402ac6454SAndrew Thompson 		return (0x2);
233502ac6454SAndrew Thompson 	case 22:
233602ac6454SAndrew Thompson 		return (0x3);
233702ac6454SAndrew Thompson 	}
233802ac6454SAndrew Thompson 
2339e87b19e3SWeongyo Jeong 	device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2340e87b19e3SWeongyo Jeong 	return (0x0);
234102ac6454SAndrew Thompson }
234202ac6454SAndrew Thompson 
234302ac6454SAndrew Thompson static void
zyd_bulk_write_callback(struct usb_xfer * xfer,usb_error_t error)2344ed6d949aSAndrew Thompson zyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
234502ac6454SAndrew Thompson {
2346ed6d949aSAndrew Thompson 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
23475463c4a4SSam Leffler 	struct ieee80211vap *vap;
234802ac6454SAndrew Thompson 	struct zyd_tx_data *data;
234902ac6454SAndrew Thompson 	struct mbuf *m;
2350ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
2351ed6d949aSAndrew Thompson 	int actlen;
2352ed6d949aSAndrew Thompson 
2353ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
235402ac6454SAndrew Thompson 
235502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
235602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
235702ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2358ed6d949aSAndrew Thompson 		    actlen);
235902ac6454SAndrew Thompson 
236002ac6454SAndrew Thompson 		/* free resources */
2361ed6d949aSAndrew Thompson 		data = usbd_xfer_get_priv(xfer);
236202ac6454SAndrew Thompson 		zyd_tx_free(data, 0);
2363ed6d949aSAndrew Thompson 		usbd_xfer_set_priv(xfer, NULL);
236402ac6454SAndrew Thompson 
236502ac6454SAndrew Thompson 		/* FALLTHROUGH */
236602ac6454SAndrew Thompson 	case USB_ST_SETUP:
236702ac6454SAndrew Thompson tr_setup:
236802ac6454SAndrew Thompson 		data = STAILQ_FIRST(&sc->tx_q);
236902ac6454SAndrew Thompson 		if (data) {
237002ac6454SAndrew Thompson 			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
237102ac6454SAndrew Thompson 			m = data->m;
237202ac6454SAndrew Thompson 
23736d917491SHans Petter Selasky 			if (m->m_pkthdr.len > (int)ZYD_MAX_TXBUFSZ) {
237402ac6454SAndrew Thompson 				DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
237502ac6454SAndrew Thompson 				    m->m_pkthdr.len);
237602ac6454SAndrew Thompson 				m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
237702ac6454SAndrew Thompson 			}
2378ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
2379ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2380ed6d949aSAndrew Thompson 			usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
238102ac6454SAndrew Thompson 			    m->m_pkthdr.len);
238202ac6454SAndrew Thompson 
23835463c4a4SSam Leffler 			vap = data->ni->ni_vap;
23845463c4a4SSam Leffler 			if (ieee80211_radiotap_active_vap(vap)) {
238502ac6454SAndrew Thompson 				struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
238602ac6454SAndrew Thompson 
238702ac6454SAndrew Thompson 				tap->wt_flags = 0;
238802ac6454SAndrew Thompson 				tap->wt_rate = data->rate;
238902ac6454SAndrew Thompson 
23905463c4a4SSam Leffler 				ieee80211_radiotap_tx(vap, m);
239102ac6454SAndrew Thompson 			}
239202ac6454SAndrew Thompson 
2393ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2394ed6d949aSAndrew Thompson 			usbd_xfer_set_priv(xfer, data);
2395a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
239602ac6454SAndrew Thompson 		}
23977a79cebfSGleb Smirnoff 		zyd_start(sc);
239802ac6454SAndrew Thompson 		break;
239902ac6454SAndrew Thompson 
240002ac6454SAndrew Thompson 	default:			/* Error */
240102ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2402ed6d949aSAndrew Thompson 		    usbd_errstr(error));
240302ac6454SAndrew Thompson 
24047a79cebfSGleb Smirnoff 		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
2405ed6d949aSAndrew Thompson 		data = usbd_xfer_get_priv(xfer);
2406ed6d949aSAndrew Thompson 		usbd_xfer_set_priv(xfer, NULL);
240702ac6454SAndrew Thompson 		if (data != NULL)
2408ed6d949aSAndrew Thompson 			zyd_tx_free(data, error);
240902ac6454SAndrew Thompson 
24100283fab7SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
24110283fab7SAndrew Thompson 			if (error == USB_ERR_TIMEOUT)
24120283fab7SAndrew Thompson 				device_printf(sc->sc_dev, "device timeout\n");
24130283fab7SAndrew Thompson 
24140283fab7SAndrew Thompson 			/*
24150283fab7SAndrew Thompson 			 * Try to clear stall first, also if other
24160283fab7SAndrew Thompson 			 * errors occur, hence clearing stall
24170283fab7SAndrew Thompson 			 * introduces a 50 ms delay:
24180283fab7SAndrew Thompson 			 */
2419ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
242002ac6454SAndrew Thompson 			goto tr_setup;
242102ac6454SAndrew Thompson 		}
242202ac6454SAndrew Thompson 		break;
242302ac6454SAndrew Thompson 	}
242402ac6454SAndrew Thompson }
242502ac6454SAndrew Thompson 
242602ac6454SAndrew Thompson static int
zyd_tx_start(struct zyd_softc * sc,struct mbuf * m0,struct ieee80211_node * ni)2427e87b19e3SWeongyo Jeong zyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
242802ac6454SAndrew Thompson {
242902ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
243002ac6454SAndrew Thompson 	struct ieee80211com *ic = ni->ni_ic;
243102ac6454SAndrew Thompson 	struct zyd_tx_desc *desc;
243202ac6454SAndrew Thompson 	struct zyd_tx_data *data;
243302ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
2434f6313575SAndriy Voskoboinyk 	const struct ieee80211_txparam *tp = ni->ni_txparms;
243502ac6454SAndrew Thompson 	struct ieee80211_key *k;
243646e18fc6SAndriy Voskoboinyk 	int rate, totlen, type, ismcast;
2437b8e3094cSHans Petter Selasky 	static const uint8_t ratediv[] = ZYD_TX_RATEDIV;
2438e87b19e3SWeongyo Jeong 	uint8_t phy;
243902ac6454SAndrew Thompson 	uint16_t pktlen;
2440e87b19e3SWeongyo Jeong 	uint32_t bits;
244102ac6454SAndrew Thompson 
244202ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
244302ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
244402ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
244502ac6454SAndrew Thompson 	sc->tx_nfree--;
244602ac6454SAndrew Thompson 
244746e18fc6SAndriy Voskoboinyk 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
244846e18fc6SAndriy Voskoboinyk 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
244946e18fc6SAndriy Voskoboinyk 
245046e18fc6SAndriy Voskoboinyk 	if (type == IEEE80211_FC0_TYPE_MGT ||
245146e18fc6SAndriy Voskoboinyk 	    type == IEEE80211_FC0_TYPE_CTL ||
2452f6313575SAndriy Voskoboinyk 	    (m0->m_flags & M_EAPOL) != 0) {
2453e87b19e3SWeongyo Jeong 		rate = tp->mgmtrate;
245402ac6454SAndrew Thompson 	} else {
2455e87b19e3SWeongyo Jeong 		/* for data frames */
245646e18fc6SAndriy Voskoboinyk 		if (ismcast)
2457e87b19e3SWeongyo Jeong 			rate = tp->mcastrate;
2458e87b19e3SWeongyo Jeong 		else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2459e87b19e3SWeongyo Jeong 			rate = tp->ucastrate;
2460e87b19e3SWeongyo Jeong 		else {
2461b6108616SRui Paulo 			(void) ieee80211_ratectl_rate(ni, NULL, 0);
2462*70674500SAdrian Chadd 			rate = ieee80211_node_get_txrate_dot11rate(ni);
246302ac6454SAndrew Thompson 		}
2464e87b19e3SWeongyo Jeong 	}
246502ac6454SAndrew Thompson 
24665945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
246702ac6454SAndrew Thompson 		k = ieee80211_crypto_encap(ni, m0);
246802ac6454SAndrew Thompson 		if (k == NULL) {
246902ac6454SAndrew Thompson 			return (ENOBUFS);
247002ac6454SAndrew Thompson 		}
247102ac6454SAndrew Thompson 		/* packet header may have moved, reset our local pointer */
247202ac6454SAndrew Thompson 		wh = mtod(m0, struct ieee80211_frame *);
247302ac6454SAndrew Thompson 	}
247402ac6454SAndrew Thompson 
247502ac6454SAndrew Thompson 	data->ni = ni;
247602ac6454SAndrew Thompson 	data->m = m0;
2477e87b19e3SWeongyo Jeong 	data->rate = rate;
247802ac6454SAndrew Thompson 
247902ac6454SAndrew Thompson 	/* fill Tx descriptor */
2480e87b19e3SWeongyo Jeong 	desc = &data->desc;
2481e87b19e3SWeongyo Jeong 	phy = zyd_plcp_signal(sc, rate);
2482e87b19e3SWeongyo Jeong 	desc->phy = phy;
2483e87b19e3SWeongyo Jeong 	if (ZYD_RATE_IS_OFDM(rate)) {
2484e87b19e3SWeongyo Jeong 		desc->phy |= ZYD_TX_PHY_OFDM;
2485e87b19e3SWeongyo Jeong 		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2486e87b19e3SWeongyo Jeong 			desc->phy |= ZYD_TX_PHY_5GHZ;
2487e87b19e3SWeongyo Jeong 	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2488e87b19e3SWeongyo Jeong 		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2489e87b19e3SWeongyo Jeong 
2490e87b19e3SWeongyo Jeong 	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
249102ac6454SAndrew Thompson 	desc->len = htole16(totlen);
249202ac6454SAndrew Thompson 
2493e87b19e3SWeongyo Jeong 	desc->flags = ZYD_TX_FLAG_BACKOFF;
249446e18fc6SAndriy Voskoboinyk 	if (!ismcast) {
249502ac6454SAndrew Thompson 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
249602ac6454SAndrew Thompson 		if (totlen > vap->iv_rtsthreshold) {
249702ac6454SAndrew Thompson 			desc->flags |= ZYD_TX_FLAG_RTS;
249802ac6454SAndrew Thompson 		} else if (ZYD_RATE_IS_OFDM(rate) &&
249902ac6454SAndrew Thompson 		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
250002ac6454SAndrew Thompson 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
250102ac6454SAndrew Thompson 				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
250202ac6454SAndrew Thompson 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
250302ac6454SAndrew Thompson 				desc->flags |= ZYD_TX_FLAG_RTS;
250402ac6454SAndrew Thompson 		}
2505e87b19e3SWeongyo Jeong 	} else
2506e87b19e3SWeongyo Jeong 		desc->flags |= ZYD_TX_FLAG_MULTICAST;
2507c249cc38SAdrian Chadd 	if (IEEE80211_IS_CTL_PS_POLL(wh))
250802ac6454SAndrew Thompson 		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
250902ac6454SAndrew Thompson 
251002ac6454SAndrew Thompson 	/* actual transmit length (XXX why +10?) */
2511e87b19e3SWeongyo Jeong 	pktlen = ZYD_TX_DESC_SIZE + 10;
251202ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211)
251302ac6454SAndrew Thompson 		pktlen += totlen;
251402ac6454SAndrew Thompson 	desc->pktlen = htole16(pktlen);
251502ac6454SAndrew Thompson 
2516e87b19e3SWeongyo Jeong 	bits = (rate == 11) ? (totlen * 16) + 10 :
2517e87b19e3SWeongyo Jeong 	    ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2518a6b8e0e9SWeongyo Jeong 	desc->plcp_length = htole16(bits / ratediv[phy]);
251902ac6454SAndrew Thompson 	desc->plcp_service = 0;
2520e87b19e3SWeongyo Jeong 	if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
252102ac6454SAndrew Thompson 		desc->plcp_service |= ZYD_PLCP_LENGEXT;
2522e87b19e3SWeongyo Jeong 	desc->nextlen = 0;
2523e87b19e3SWeongyo Jeong 
2524e87b19e3SWeongyo Jeong 	if (ieee80211_radiotap_active_vap(vap)) {
2525e87b19e3SWeongyo Jeong 		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2526e87b19e3SWeongyo Jeong 
2527e87b19e3SWeongyo Jeong 		tap->wt_flags = 0;
2528e87b19e3SWeongyo Jeong 		tap->wt_rate = rate;
2529e87b19e3SWeongyo Jeong 
2530e87b19e3SWeongyo Jeong 		ieee80211_radiotap_tx(vap, m0);
253102ac6454SAndrew Thompson 	}
253202ac6454SAndrew Thompson 
253302ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_XMIT,
253402ac6454SAndrew Thompson 	    "%s: sending data frame len=%zu rate=%u\n",
253502ac6454SAndrew Thompson 	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
253602ac6454SAndrew Thompson 		rate);
253702ac6454SAndrew Thompson 
253802ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2539a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
254002ac6454SAndrew Thompson 
254102ac6454SAndrew Thompson 	return (0);
254202ac6454SAndrew Thompson }
254302ac6454SAndrew Thompson 
25447a79cebfSGleb Smirnoff static int
zyd_transmit(struct ieee80211com * ic,struct mbuf * m)25457a79cebfSGleb Smirnoff zyd_transmit(struct ieee80211com *ic, struct mbuf *m)
254679d2c5e8SGleb Smirnoff {
25477a79cebfSGleb Smirnoff 	struct zyd_softc *sc = ic->ic_softc;
25487a79cebfSGleb Smirnoff 	int error;
25497a79cebfSGleb Smirnoff 
25507a79cebfSGleb Smirnoff 	ZYD_LOCK(sc);
25517a79cebfSGleb Smirnoff 	if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) {
25527a79cebfSGleb Smirnoff 		ZYD_UNLOCK(sc);
25537a79cebfSGleb Smirnoff 		return (ENXIO);
25547a79cebfSGleb Smirnoff 	}
25557a79cebfSGleb Smirnoff 	error = mbufq_enqueue(&sc->sc_snd, m);
25567a79cebfSGleb Smirnoff 	if (error) {
25577a79cebfSGleb Smirnoff 		ZYD_UNLOCK(sc);
25587a79cebfSGleb Smirnoff 		return (error);
25597a79cebfSGleb Smirnoff 	}
25607a79cebfSGleb Smirnoff 	zyd_start(sc);
25617a79cebfSGleb Smirnoff 	ZYD_UNLOCK(sc);
25627a79cebfSGleb Smirnoff 
25637a79cebfSGleb Smirnoff 	return (0);
25647a79cebfSGleb Smirnoff }
25657a79cebfSGleb Smirnoff 
25667a79cebfSGleb Smirnoff static void
zyd_start(struct zyd_softc * sc)25677a79cebfSGleb Smirnoff zyd_start(struct zyd_softc *sc)
25687a79cebfSGleb Smirnoff {
256902ac6454SAndrew Thompson 	struct ieee80211_node *ni;
257002ac6454SAndrew Thompson 	struct mbuf *m;
257102ac6454SAndrew Thompson 
25727a79cebfSGleb Smirnoff 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
25737a79cebfSGleb Smirnoff 
25747a79cebfSGleb Smirnoff 	while (sc->tx_nfree > 0 && (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
257502ac6454SAndrew Thompson 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2576e87b19e3SWeongyo Jeong 		if (zyd_tx_start(sc, m, ni) != 0) {
257732745980SAdrian Chadd 			m_freem(m);
25787a79cebfSGleb Smirnoff 			if_inc_counter(ni->ni_vap->iv_ifp,
25797a79cebfSGleb Smirnoff 			    IFCOUNTER_OERRORS, 1);
2580ce017db1SAndriy Voskoboinyk 			ieee80211_free_node(ni);
258102ac6454SAndrew Thompson 			break;
258202ac6454SAndrew Thompson 		}
258302ac6454SAndrew Thompson 	}
258402ac6454SAndrew Thompson }
258502ac6454SAndrew Thompson 
258602ac6454SAndrew Thompson static int
zyd_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)258702ac6454SAndrew Thompson zyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
258802ac6454SAndrew Thompson 	const struct ieee80211_bpf_params *params)
258902ac6454SAndrew Thompson {
259002ac6454SAndrew Thompson 	struct ieee80211com *ic = ni->ni_ic;
2591d3fdd08cSAdrian Chadd 	struct zyd_softc *sc = ic->ic_softc;
259202ac6454SAndrew Thompson 
259302ac6454SAndrew Thompson 	ZYD_LOCK(sc);
259402ac6454SAndrew Thompson 	/* prevent management frames from being sent if we're not ready */
25957a79cebfSGleb Smirnoff 	if (!(sc->sc_flags & ZYD_FLAG_RUNNING)) {
259602ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
259702ac6454SAndrew Thompson 		m_freem(m);
259802ac6454SAndrew Thompson 		return (ENETDOWN);
259902ac6454SAndrew Thompson 	}
260002ac6454SAndrew Thompson 	if (sc->tx_nfree == 0) {
260102ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
260202ac6454SAndrew Thompson 		m_freem(m);
260302ac6454SAndrew Thompson 		return (ENOBUFS);		/* XXX */
260402ac6454SAndrew Thompson 	}
260502ac6454SAndrew Thompson 
260602ac6454SAndrew Thompson 	/*
260702ac6454SAndrew Thompson 	 * Legacy path; interpret frame contents to decide
260802ac6454SAndrew Thompson 	 * precisely how to send the frame.
260902ac6454SAndrew Thompson 	 * XXX raw path
261002ac6454SAndrew Thompson 	 */
2611e87b19e3SWeongyo Jeong 	if (zyd_tx_start(sc, m, ni) != 0) {
261202ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
261332745980SAdrian Chadd 		m_freem(m);
261402ac6454SAndrew Thompson 		return (EIO);
261502ac6454SAndrew Thompson 	}
261602ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
261702ac6454SAndrew Thompson 	return (0);
261802ac6454SAndrew Thompson }
261902ac6454SAndrew Thompson 
26207a79cebfSGleb Smirnoff static void
zyd_parent(struct ieee80211com * ic)26217a79cebfSGleb Smirnoff zyd_parent(struct ieee80211com *ic)
262202ac6454SAndrew Thompson {
2623d3fdd08cSAdrian Chadd 	struct zyd_softc *sc = ic->ic_softc;
2624645e4d17SHans Petter Selasky 	int startall = 0;
2625645e4d17SHans Petter Selasky 
2626645e4d17SHans Petter Selasky 	ZYD_LOCK(sc);
26277a79cebfSGleb Smirnoff 	if (sc->sc_flags & ZYD_FLAG_DETACHED) {
2628645e4d17SHans Petter Selasky 		ZYD_UNLOCK(sc);
26297a79cebfSGleb Smirnoff 		return;
26307a79cebfSGleb Smirnoff 	}
26317a79cebfSGleb Smirnoff 	if (ic->ic_nrunning > 0) {
26327a79cebfSGleb Smirnoff 		if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) {
26335efea30fSAndrew Thompson 			zyd_init_locked(sc);
263402ac6454SAndrew Thompson 			startall = 1;
26355efea30fSAndrew Thompson 		} else
26365efea30fSAndrew Thompson 			zyd_set_multi(sc);
26377a79cebfSGleb Smirnoff 	} else if (sc->sc_flags & ZYD_FLAG_RUNNING)
26385efea30fSAndrew Thompson 		zyd_stop(sc);
263902ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
264002ac6454SAndrew Thompson 	if (startall)
264102ac6454SAndrew Thompson 		ieee80211_start_all(ic);
264202ac6454SAndrew Thompson }
264302ac6454SAndrew Thompson 
264402ac6454SAndrew Thompson static void
zyd_init_locked(struct zyd_softc * sc)26455efea30fSAndrew Thompson zyd_init_locked(struct zyd_softc *sc)
264602ac6454SAndrew Thompson {
26477a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
26487a79cebfSGleb Smirnoff 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2649760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
265002ac6454SAndrew Thompson 	int error;
265102ac6454SAndrew Thompson 	uint32_t val;
265202ac6454SAndrew Thompson 
265302ac6454SAndrew Thompson 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
265402ac6454SAndrew Thompson 
265502ac6454SAndrew Thompson 	if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
265602ac6454SAndrew Thompson 		error = zyd_loadfirmware(sc);
265702ac6454SAndrew Thompson 		if (error != 0) {
265802ac6454SAndrew Thompson 			device_printf(sc->sc_dev,
265902ac6454SAndrew Thompson 			    "could not load firmware (error=%d)\n", error);
266002ac6454SAndrew Thompson 			goto fail;
266102ac6454SAndrew Thompson 		}
266202ac6454SAndrew Thompson 
266302ac6454SAndrew Thompson 		/* reset device */
2664a593f6b8SAndrew Thompson 		cd = usbd_get_config_descriptor(sc->sc_udev);
2665a593f6b8SAndrew Thompson 		error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
266602ac6454SAndrew Thompson 		    cd->bConfigurationValue);
266702ac6454SAndrew Thompson 		if (error)
266802ac6454SAndrew Thompson 			device_printf(sc->sc_dev, "reset failed, continuing\n");
266902ac6454SAndrew Thompson 
267002ac6454SAndrew Thompson 		error = zyd_hw_init(sc);
267102ac6454SAndrew Thompson 		if (error) {
267202ac6454SAndrew Thompson 			device_printf(sc->sc_dev,
267302ac6454SAndrew Thompson 			    "hardware initialization failed\n");
267402ac6454SAndrew Thompson 			goto fail;
267502ac6454SAndrew Thompson 		}
267602ac6454SAndrew Thompson 
267702ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
267802ac6454SAndrew Thompson 		    "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
267902ac6454SAndrew Thompson 		    "BE%x NP%x Gain%x F%x\n",
268002ac6454SAndrew Thompson 		    (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
268102ac6454SAndrew Thompson 		    sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
268202ac6454SAndrew Thompson 		    zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
268302ac6454SAndrew Thompson 		    sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
268402ac6454SAndrew Thompson 		    sc->sc_cckgain, sc->sc_fix_cr157);
268502ac6454SAndrew Thompson 
268602ac6454SAndrew Thompson 		/* read regulatory domain (currently unused) */
268702ac6454SAndrew Thompson 		zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
268802ac6454SAndrew Thompson 		sc->sc_regdomain = val >> 16;
268902ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
269002ac6454SAndrew Thompson 		    sc->sc_regdomain);
269102ac6454SAndrew Thompson 
269202ac6454SAndrew Thompson 		/* we'll do software WEP decryption for now */
269302ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
269402ac6454SAndrew Thompson 		    __func__);
269502ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
269602ac6454SAndrew Thompson 
269702ac6454SAndrew Thompson 		sc->sc_flags |= ZYD_FLAG_INITONCE;
269802ac6454SAndrew Thompson 	}
269902ac6454SAndrew Thompson 
27007a79cebfSGleb Smirnoff 	if (sc->sc_flags & ZYD_FLAG_RUNNING)
27015efea30fSAndrew Thompson 		zyd_stop(sc);
270202ac6454SAndrew Thompson 
270329aca940SSam Leffler 	DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
27047a79cebfSGleb Smirnoff 	    vap ? vap->iv_myaddr : ic->ic_macaddr, ":");
27057a79cebfSGleb Smirnoff 	error = zyd_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
270602ac6454SAndrew Thompson 	if (error != 0)
270702ac6454SAndrew Thompson 		return;
270802ac6454SAndrew Thompson 
270902ac6454SAndrew Thompson 	/* set basic rates */
271002ac6454SAndrew Thompson 	if (ic->ic_curmode == IEEE80211_MODE_11B)
271102ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
271202ac6454SAndrew Thompson 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
271302ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
271402ac6454SAndrew Thompson 	else	/* assumes 802.11b/g */
271502ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
271602ac6454SAndrew Thompson 
271702ac6454SAndrew Thompson 	/* promiscuous mode */
271802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
271902ac6454SAndrew Thompson 	/* multicast setup */
272002ac6454SAndrew Thompson 	zyd_set_multi(sc);
272102ac6454SAndrew Thompson 	/* set RX filter  */
272202ac6454SAndrew Thompson 	error = zyd_set_rxfilter(sc);
272302ac6454SAndrew Thompson 	if (error != 0)
272402ac6454SAndrew Thompson 		goto fail;
272502ac6454SAndrew Thompson 
272602ac6454SAndrew Thompson 	/* switch radio transmitter ON */
272702ac6454SAndrew Thompson 	error = zyd_switch_radio(sc, 1);
272802ac6454SAndrew Thompson 	if (error != 0)
272902ac6454SAndrew Thompson 		goto fail;
273002ac6454SAndrew Thompson 	/* set default BSS channel */
273102ac6454SAndrew Thompson 	zyd_set_chan(sc, ic->ic_curchan);
273202ac6454SAndrew Thompson 
273302ac6454SAndrew Thompson 	/*
273402ac6454SAndrew Thompson 	 * Allocate Tx and Rx xfer queues.
273502ac6454SAndrew Thompson 	 */
273602ac6454SAndrew Thompson 	zyd_setup_tx_list(sc);
273702ac6454SAndrew Thompson 
273802ac6454SAndrew Thompson 	/* enable interrupts */
273902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
274002ac6454SAndrew Thompson 
27417a79cebfSGleb Smirnoff 	sc->sc_flags |= ZYD_FLAG_RUNNING;
2742ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2743a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2744a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
274502ac6454SAndrew Thompson 
274602ac6454SAndrew Thompson 	return;
274702ac6454SAndrew Thompson 
27485efea30fSAndrew Thompson fail:	zyd_stop(sc);
274902ac6454SAndrew Thompson 	return;
275002ac6454SAndrew Thompson }
275102ac6454SAndrew Thompson 
275202ac6454SAndrew Thompson static void
zyd_stop(struct zyd_softc * sc)27535efea30fSAndrew Thompson zyd_stop(struct zyd_softc *sc)
275402ac6454SAndrew Thompson {
275502ac6454SAndrew Thompson 	int error;
275602ac6454SAndrew Thompson 
275702ac6454SAndrew Thompson 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
275802ac6454SAndrew Thompson 
27597a79cebfSGleb Smirnoff 	sc->sc_flags &= ~ZYD_FLAG_RUNNING;
276032745980SAdrian Chadd 	zyd_drain_mbufq(sc);
276102ac6454SAndrew Thompson 
276202ac6454SAndrew Thompson 	/*
276302ac6454SAndrew Thompson 	 * Drain all the transfers, if not already drained:
276402ac6454SAndrew Thompson 	 */
276502ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
2766a593f6b8SAndrew Thompson 	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2767a593f6b8SAndrew Thompson 	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
276802ac6454SAndrew Thompson 	ZYD_LOCK(sc);
276902ac6454SAndrew Thompson 
277002ac6454SAndrew Thompson 	zyd_unsetup_tx_list(sc);
277102ac6454SAndrew Thompson 
277202ac6454SAndrew Thompson 	/* Stop now if the device was never set up */
277302ac6454SAndrew Thompson 	if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
277402ac6454SAndrew Thompson 		return;
277502ac6454SAndrew Thompson 
277602ac6454SAndrew Thompson 	/* switch radio transmitter OFF */
277702ac6454SAndrew Thompson 	error = zyd_switch_radio(sc, 0);
277802ac6454SAndrew Thompson 	if (error != 0)
277902ac6454SAndrew Thompson 		goto fail;
278002ac6454SAndrew Thompson 	/* disable Rx */
278102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
278202ac6454SAndrew Thompson 	/* disable interrupts */
278302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
278402ac6454SAndrew Thompson 
278502ac6454SAndrew Thompson fail:
278602ac6454SAndrew Thompson 	return;
278702ac6454SAndrew Thompson }
278802ac6454SAndrew Thompson 
278902ac6454SAndrew Thompson static int
zyd_loadfirmware(struct zyd_softc * sc)279002ac6454SAndrew Thompson zyd_loadfirmware(struct zyd_softc *sc)
279102ac6454SAndrew Thompson {
2792760bc48eSAndrew Thompson 	struct usb_device_request req;
279302ac6454SAndrew Thompson 	size_t size;
279402ac6454SAndrew Thompson 	u_char *fw;
279502ac6454SAndrew Thompson 	uint8_t stat;
279602ac6454SAndrew Thompson 	uint16_t addr;
279702ac6454SAndrew Thompson 
279802ac6454SAndrew Thompson 	if (sc->sc_flags & ZYD_FLAG_FWLOADED)
279902ac6454SAndrew Thompson 		return (0);
280002ac6454SAndrew Thompson 
280102ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211) {
280202ac6454SAndrew Thompson 		fw = (u_char *)zd1211_firmware;
280302ac6454SAndrew Thompson 		size = sizeof(zd1211_firmware);
280402ac6454SAndrew Thompson 	} else {
280502ac6454SAndrew Thompson 		fw = (u_char *)zd1211b_firmware;
280602ac6454SAndrew Thompson 		size = sizeof(zd1211b_firmware);
280702ac6454SAndrew Thompson 	}
280802ac6454SAndrew Thompson 
280902ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
281002ac6454SAndrew Thompson 	req.bRequest = ZYD_DOWNLOADREQ;
281102ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
281202ac6454SAndrew Thompson 
281302ac6454SAndrew Thompson 	addr = ZYD_FIRMWARE_START_ADDR;
281402ac6454SAndrew Thompson 	while (size > 0) {
281502ac6454SAndrew Thompson 		/*
281602ac6454SAndrew Thompson 		 * When the transfer size is 4096 bytes, it is not
281702ac6454SAndrew Thompson 		 * likely to be able to transfer it.
281802ac6454SAndrew Thompson 		 * The cause is port or machine or chip?
281902ac6454SAndrew Thompson 		 */
282002ac6454SAndrew Thompson 		const int mlen = min(size, 64);
282102ac6454SAndrew Thompson 
282202ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_FW,
282302ac6454SAndrew Thompson 		    "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
282402ac6454SAndrew Thompson 
282502ac6454SAndrew Thompson 		USETW(req.wValue, addr);
282602ac6454SAndrew Thompson 		USETW(req.wLength, mlen);
282702ac6454SAndrew Thompson 		if (zyd_do_request(sc, &req, fw) != 0)
282802ac6454SAndrew Thompson 			return (EIO);
282902ac6454SAndrew Thompson 
283002ac6454SAndrew Thompson 		addr += mlen / 2;
283102ac6454SAndrew Thompson 		fw   += mlen;
283202ac6454SAndrew Thompson 		size -= mlen;
283302ac6454SAndrew Thompson 	}
283402ac6454SAndrew Thompson 
283502ac6454SAndrew Thompson 	/* check whether the upload succeeded */
283602ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
283702ac6454SAndrew Thompson 	req.bRequest = ZYD_DOWNLOADSTS;
283802ac6454SAndrew Thompson 	USETW(req.wValue, 0);
283902ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
284002ac6454SAndrew Thompson 	USETW(req.wLength, sizeof(stat));
284102ac6454SAndrew Thompson 	if (zyd_do_request(sc, &req, &stat) != 0)
284202ac6454SAndrew Thompson 		return (EIO);
284302ac6454SAndrew Thompson 
284402ac6454SAndrew Thompson 	sc->sc_flags |= ZYD_FLAG_FWLOADED;
284502ac6454SAndrew Thompson 
284602ac6454SAndrew Thompson 	return (stat & 0x80) ? (EIO) : (0);
284702ac6454SAndrew Thompson }
284802ac6454SAndrew Thompson 
284902ac6454SAndrew Thompson static void
zyd_scan_start(struct ieee80211com * ic)285002ac6454SAndrew Thompson zyd_scan_start(struct ieee80211com *ic)
285102ac6454SAndrew Thompson {
2852d3fdd08cSAdrian Chadd 	struct zyd_softc *sc = ic->ic_softc;
285302ac6454SAndrew Thompson 
285402ac6454SAndrew Thompson 	ZYD_LOCK(sc);
28555efea30fSAndrew Thompson 	/* want broadcast address while scanning */
28567a79cebfSGleb Smirnoff 	zyd_set_bssid(sc, ieee80211broadcastaddr);
285702ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
285802ac6454SAndrew Thompson }
285902ac6454SAndrew Thompson 
286002ac6454SAndrew Thompson static void
zyd_scan_end(struct ieee80211com * ic)286102ac6454SAndrew Thompson zyd_scan_end(struct ieee80211com *ic)
286202ac6454SAndrew Thompson {
2863d3fdd08cSAdrian Chadd 	struct zyd_softc *sc = ic->ic_softc;
286402ac6454SAndrew Thompson 
286502ac6454SAndrew Thompson 	ZYD_LOCK(sc);
28665efea30fSAndrew Thompson 	/* restore previous bssid */
2867ae132f11SAndriy Voskoboinyk 	zyd_set_bssid(sc, sc->sc_bssid);
286802ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
286902ac6454SAndrew Thompson }
287002ac6454SAndrew Thompson 
287102ac6454SAndrew Thompson static void
zyd_getradiocaps(struct ieee80211com * ic,int maxchans,int * nchans,struct ieee80211_channel chans[])28725a62dab1SAndriy Voskoboinyk zyd_getradiocaps(struct ieee80211com *ic,
28735a62dab1SAndriy Voskoboinyk     int maxchans, int *nchans, struct ieee80211_channel chans[])
28745a62dab1SAndriy Voskoboinyk {
28755a62dab1SAndriy Voskoboinyk 	uint8_t bands[IEEE80211_MODE_BYTES];
28765a62dab1SAndriy Voskoboinyk 
28775a62dab1SAndriy Voskoboinyk 	memset(bands, 0, sizeof(bands));
28785a62dab1SAndriy Voskoboinyk 	setbit(bands, IEEE80211_MODE_11B);
28795a62dab1SAndriy Voskoboinyk 	setbit(bands, IEEE80211_MODE_11G);
2880b84b3638SAndriy Voskoboinyk 	ieee80211_add_channels_default_2ghz(chans, maxchans, nchans, bands, 0);
28815a62dab1SAndriy Voskoboinyk }
28825a62dab1SAndriy Voskoboinyk 
28835a62dab1SAndriy Voskoboinyk static void
zyd_set_channel(struct ieee80211com * ic)288402ac6454SAndrew Thompson zyd_set_channel(struct ieee80211com *ic)
288502ac6454SAndrew Thompson {
2886d3fdd08cSAdrian Chadd 	struct zyd_softc *sc = ic->ic_softc;
288702ac6454SAndrew Thompson 
288802ac6454SAndrew Thompson 	ZYD_LOCK(sc);
288902ac6454SAndrew Thompson 	zyd_set_chan(sc, ic->ic_curchan);
28905efea30fSAndrew Thompson 	ZYD_UNLOCK(sc);
289102ac6454SAndrew Thompson }
289202ac6454SAndrew Thompson 
289302ac6454SAndrew Thompson static device_method_t zyd_methods[] = {
289402ac6454SAndrew Thompson         /* Device interface */
289502ac6454SAndrew Thompson         DEVMETHOD(device_probe, zyd_match),
289602ac6454SAndrew Thompson         DEVMETHOD(device_attach, zyd_attach),
289702ac6454SAndrew Thompson         DEVMETHOD(device_detach, zyd_detach),
2898645e4d17SHans Petter Selasky 	DEVMETHOD_END
289902ac6454SAndrew Thompson };
290002ac6454SAndrew Thompson 
290102ac6454SAndrew Thompson static driver_t zyd_driver = {
29026d917491SHans Petter Selasky 	.name = "zyd",
29036d917491SHans Petter Selasky 	.methods = zyd_methods,
29046d917491SHans Petter Selasky 	.size = sizeof(struct zyd_softc)
290502ac6454SAndrew Thompson };
290602ac6454SAndrew Thompson 
2907bc9372d7SJohn Baldwin DRIVER_MODULE(zyd, uhub, zyd_driver, NULL, NULL);
290802ac6454SAndrew Thompson MODULE_DEPEND(zyd, usb, 1, 1, 1);
290902ac6454SAndrew Thompson MODULE_DEPEND(zyd, wlan, 1, 1, 1);
2910910cb8feSAndrew Thompson MODULE_VERSION(zyd, 1);
2911f809f280SWarner Losh USB_PNP_HOST_INFO(zyd_devs);
2912