xref: /freebsd/sys/dev/usb/wlan/if_zyd.c (revision a6b8e0e91597e48b4bae920cf6ddbebe55d940f1)
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 /*	$FreeBSD$	*/
402ac6454SAndrew Thompson 
502ac6454SAndrew Thompson /*-
602ac6454SAndrew Thompson  * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr>
702ac6454SAndrew Thompson  * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de>
802ac6454SAndrew Thompson  *
902ac6454SAndrew Thompson  * Permission to use, copy, modify, and distribute this software for any
1002ac6454SAndrew Thompson  * purpose with or without fee is hereby granted, provided that the above
1102ac6454SAndrew Thompson  * copyright notice and this permission notice appear in all copies.
1202ac6454SAndrew Thompson  *
1302ac6454SAndrew Thompson  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1402ac6454SAndrew Thompson  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1502ac6454SAndrew Thompson  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1602ac6454SAndrew Thompson  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1702ac6454SAndrew Thompson  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1802ac6454SAndrew Thompson  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1902ac6454SAndrew Thompson  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2002ac6454SAndrew Thompson  */
2102ac6454SAndrew Thompson 
2202ac6454SAndrew Thompson #include <sys/cdefs.h>
2302ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
2402ac6454SAndrew Thompson 
2502ac6454SAndrew Thompson /*
2602ac6454SAndrew Thompson  * ZyDAS ZD1211/ZD1211B USB WLAN driver.
2702ac6454SAndrew Thompson  */
2802ac6454SAndrew Thompson 
295efea30fSAndrew Thompson #include <sys/param.h>
305efea30fSAndrew Thompson #include <sys/sockio.h>
315efea30fSAndrew Thompson #include <sys/sysctl.h>
325efea30fSAndrew Thompson #include <sys/lock.h>
335efea30fSAndrew Thompson #include <sys/mutex.h>
34ed6d949aSAndrew Thompson #include <sys/condvar.h>
355efea30fSAndrew Thompson #include <sys/mbuf.h>
365efea30fSAndrew Thompson #include <sys/kernel.h>
375efea30fSAndrew Thompson #include <sys/socket.h>
385efea30fSAndrew Thompson #include <sys/systm.h>
395efea30fSAndrew Thompson #include <sys/malloc.h>
405efea30fSAndrew Thompson #include <sys/module.h>
415efea30fSAndrew Thompson #include <sys/bus.h>
425efea30fSAndrew Thompson #include <sys/endian.h>
435efea30fSAndrew Thompson #include <sys/kdb.h>
4402ac6454SAndrew Thompson 
455efea30fSAndrew Thompson #include <machine/bus.h>
465efea30fSAndrew Thompson #include <machine/resource.h>
475efea30fSAndrew Thompson #include <sys/rman.h>
485efea30fSAndrew Thompson 
495efea30fSAndrew Thompson #include <net/bpf.h>
505efea30fSAndrew Thompson #include <net/if.h>
515efea30fSAndrew Thompson #include <net/if_arp.h>
525efea30fSAndrew Thompson #include <net/ethernet.h>
535efea30fSAndrew Thompson #include <net/if_dl.h>
545efea30fSAndrew Thompson #include <net/if_media.h>
555efea30fSAndrew Thompson #include <net/if_types.h>
565efea30fSAndrew Thompson 
575efea30fSAndrew Thompson #ifdef INET
585efea30fSAndrew Thompson #include <netinet/in.h>
595efea30fSAndrew Thompson #include <netinet/in_systm.h>
605efea30fSAndrew Thompson #include <netinet/in_var.h>
615efea30fSAndrew Thompson #include <netinet/if_ether.h>
625efea30fSAndrew Thompson #include <netinet/ip.h>
635efea30fSAndrew Thompson #endif
645efea30fSAndrew Thompson 
655efea30fSAndrew Thompson #include <net80211/ieee80211_var.h>
665efea30fSAndrew Thompson #include <net80211/ieee80211_regdomain.h>
675efea30fSAndrew Thompson #include <net80211/ieee80211_radiotap.h>
685efea30fSAndrew Thompson #include <net80211/ieee80211_amrr.h>
695efea30fSAndrew Thompson 
705efea30fSAndrew Thompson #include <dev/usb/usb.h>
71ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
72ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
735efea30fSAndrew Thompson #include "usbdevs.h"
7402ac6454SAndrew Thompson 
7502ac6454SAndrew Thompson #include <dev/usb/wlan/if_zydreg.h>
7602ac6454SAndrew Thompson #include <dev/usb/wlan/if_zydfw.h>
7702ac6454SAndrew Thompson 
7802ac6454SAndrew Thompson #if USB_DEBUG
7902ac6454SAndrew Thompson static int zyd_debug = 0;
8002ac6454SAndrew Thompson 
819360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, zyd, CTLFLAG_RW, 0, "USB zyd");
829360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_zyd, OID_AUTO, debug, CTLFLAG_RW, &zyd_debug, 0,
8302ac6454SAndrew Thompson     "zyd debug level");
8402ac6454SAndrew Thompson 
8502ac6454SAndrew Thompson enum {
8602ac6454SAndrew Thompson 	ZYD_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
8702ac6454SAndrew Thompson 	ZYD_DEBUG_RECV		= 0x00000002,	/* basic recv operation */
8802ac6454SAndrew Thompson 	ZYD_DEBUG_RESET		= 0x00000004,	/* reset processing */
8902ac6454SAndrew Thompson 	ZYD_DEBUG_INIT		= 0x00000008,	/* device init */
9002ac6454SAndrew Thompson 	ZYD_DEBUG_TX_PROC	= 0x00000010,	/* tx ISR proc */
9102ac6454SAndrew Thompson 	ZYD_DEBUG_RX_PROC	= 0x00000020,	/* rx ISR proc */
9202ac6454SAndrew Thompson 	ZYD_DEBUG_STATE		= 0x00000040,	/* 802.11 state transitions */
9302ac6454SAndrew Thompson 	ZYD_DEBUG_STAT		= 0x00000080,	/* statistic */
9402ac6454SAndrew Thompson 	ZYD_DEBUG_FW		= 0x00000100,	/* firmware */
9502ac6454SAndrew Thompson 	ZYD_DEBUG_CMD		= 0x00000200,	/* fw commands */
9602ac6454SAndrew Thompson 	ZYD_DEBUG_ANY		= 0xffffffff
9702ac6454SAndrew Thompson };
9802ac6454SAndrew Thompson #define	DPRINTF(sc, m, fmt, ...) do {				\
9902ac6454SAndrew Thompson 	if (zyd_debug & (m))					\
10002ac6454SAndrew Thompson 		printf("%s: " fmt, __func__, ## __VA_ARGS__);	\
10102ac6454SAndrew Thompson } while (0)
10202ac6454SAndrew Thompson #else
10302ac6454SAndrew Thompson #define	DPRINTF(sc, m, fmt, ...) do {				\
10402ac6454SAndrew Thompson 	(void) sc;						\
10502ac6454SAndrew Thompson } while (0)
10602ac6454SAndrew Thompson #endif
10702ac6454SAndrew Thompson 
10802ac6454SAndrew Thompson #define	zyd_do_request(sc,req,data) \
109a593f6b8SAndrew Thompson     usbd_do_request_flags((sc)->sc_udev, &(sc)->sc_mtx, req, data, 0, NULL, 5000)
11002ac6454SAndrew Thompson 
11102ac6454SAndrew Thompson static device_probe_t zyd_match;
11202ac6454SAndrew Thompson static device_attach_t zyd_attach;
11302ac6454SAndrew Thompson static device_detach_t zyd_detach;
11402ac6454SAndrew Thompson 
115e0a69b51SAndrew Thompson static usb_callback_t zyd_intr_read_callback;
116e0a69b51SAndrew Thompson static usb_callback_t zyd_intr_write_callback;
117e0a69b51SAndrew Thompson static usb_callback_t zyd_bulk_read_callback;
118e0a69b51SAndrew Thompson static usb_callback_t zyd_bulk_write_callback;
11902ac6454SAndrew Thompson 
12002ac6454SAndrew Thompson static struct ieee80211vap *zyd_vap_create(struct ieee80211com *,
12102ac6454SAndrew Thompson 		    const char name[IFNAMSIZ], int unit, int opmode,
12202ac6454SAndrew Thompson 		    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
12302ac6454SAndrew Thompson 		    const uint8_t mac[IEEE80211_ADDR_LEN]);
12402ac6454SAndrew Thompson static void	zyd_vap_delete(struct ieee80211vap *);
12502ac6454SAndrew Thompson static void	zyd_tx_free(struct zyd_tx_data *, int);
12602ac6454SAndrew Thompson static void	zyd_setup_tx_list(struct zyd_softc *);
12702ac6454SAndrew Thompson static void	zyd_unsetup_tx_list(struct zyd_softc *);
12802ac6454SAndrew Thompson static struct ieee80211_node *zyd_node_alloc(struct ieee80211vap *,
12902ac6454SAndrew Thompson 			    const uint8_t mac[IEEE80211_ADDR_LEN]);
13002ac6454SAndrew Thompson static int	zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int);
13102ac6454SAndrew Thompson static int	zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
13202ac6454SAndrew Thompson 		    void *, int, int);
13302ac6454SAndrew Thompson static int	zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
13402ac6454SAndrew Thompson static int	zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
13502ac6454SAndrew Thompson static int	zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
13602ac6454SAndrew Thompson static int	zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
13702ac6454SAndrew Thompson static int	zyd_rfwrite(struct zyd_softc *, uint32_t);
13802ac6454SAndrew Thompson static int	zyd_lock_phy(struct zyd_softc *);
13902ac6454SAndrew Thompson static int	zyd_unlock_phy(struct zyd_softc *);
14002ac6454SAndrew Thompson static int	zyd_rf_attach(struct zyd_softc *, uint8_t);
14102ac6454SAndrew Thompson static const char *zyd_rf_name(uint8_t);
14202ac6454SAndrew Thompson static int	zyd_hw_init(struct zyd_softc *);
14302ac6454SAndrew Thompson static int	zyd_read_pod(struct zyd_softc *);
14402ac6454SAndrew Thompson static int	zyd_read_eeprom(struct zyd_softc *);
14502ac6454SAndrew Thompson static int	zyd_get_macaddr(struct zyd_softc *);
14602ac6454SAndrew Thompson static int	zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
14702ac6454SAndrew Thompson static int	zyd_set_bssid(struct zyd_softc *, const uint8_t *);
14802ac6454SAndrew Thompson static int	zyd_switch_radio(struct zyd_softc *, int);
14902ac6454SAndrew Thompson static int	zyd_set_led(struct zyd_softc *, int, int);
15002ac6454SAndrew Thompson static void	zyd_set_multi(struct zyd_softc *);
15102ac6454SAndrew Thompson static void	zyd_update_mcast(struct ifnet *);
15202ac6454SAndrew Thompson static int	zyd_set_rxfilter(struct zyd_softc *);
15302ac6454SAndrew Thompson static void	zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
15402ac6454SAndrew Thompson static int	zyd_set_beacon_interval(struct zyd_softc *, int);
155760bc48eSAndrew Thompson static void	zyd_rx_data(struct usb_xfer *, int, uint16_t);
156e87b19e3SWeongyo Jeong static int	zyd_tx_start(struct zyd_softc *, struct mbuf *,
15702ac6454SAndrew Thompson 		    struct ieee80211_node *);
15802ac6454SAndrew Thompson static void	zyd_start(struct ifnet *);
15902ac6454SAndrew Thompson static int	zyd_raw_xmit(struct ieee80211_node *, struct mbuf *,
16002ac6454SAndrew Thompson 		    const struct ieee80211_bpf_params *);
16102ac6454SAndrew Thompson static int	zyd_ioctl(struct ifnet *, u_long, caddr_t);
1625efea30fSAndrew Thompson static void	zyd_init_locked(struct zyd_softc *);
16302ac6454SAndrew Thompson static void	zyd_init(void *);
1645efea30fSAndrew Thompson static void	zyd_stop(struct zyd_softc *);
16502ac6454SAndrew Thompson static int	zyd_loadfirmware(struct zyd_softc *);
16602ac6454SAndrew Thompson static void	zyd_newassoc(struct ieee80211_node *, int);
16702ac6454SAndrew Thompson static void	zyd_scan_start(struct ieee80211com *);
16802ac6454SAndrew Thompson static void	zyd_scan_end(struct ieee80211com *);
16902ac6454SAndrew Thompson static void	zyd_set_channel(struct ieee80211com *);
17002ac6454SAndrew Thompson static int	zyd_rfmd_init(struct zyd_rf *);
17102ac6454SAndrew Thompson static int	zyd_rfmd_switch_radio(struct zyd_rf *, int);
17202ac6454SAndrew Thompson static int	zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
17302ac6454SAndrew Thompson static int	zyd_al2230_init(struct zyd_rf *);
17402ac6454SAndrew Thompson static int	zyd_al2230_switch_radio(struct zyd_rf *, int);
17502ac6454SAndrew Thompson static int	zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
17602ac6454SAndrew Thompson static int	zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t);
17702ac6454SAndrew Thompson static int	zyd_al2230_init_b(struct zyd_rf *);
17802ac6454SAndrew Thompson static int	zyd_al7230B_init(struct zyd_rf *);
17902ac6454SAndrew Thompson static int	zyd_al7230B_switch_radio(struct zyd_rf *, int);
18002ac6454SAndrew Thompson static int	zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
18102ac6454SAndrew Thompson static int	zyd_al2210_init(struct zyd_rf *);
18202ac6454SAndrew Thompson static int	zyd_al2210_switch_radio(struct zyd_rf *, int);
18302ac6454SAndrew Thompson static int	zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
18402ac6454SAndrew Thompson static int	zyd_gct_init(struct zyd_rf *);
18502ac6454SAndrew Thompson static int	zyd_gct_switch_radio(struct zyd_rf *, int);
18602ac6454SAndrew Thompson static int	zyd_gct_set_channel(struct zyd_rf *, uint8_t);
18778cc4bbbSWeongyo Jeong static int	zyd_gct_mode(struct zyd_rf *);
18878cc4bbbSWeongyo Jeong static int	zyd_gct_set_channel_synth(struct zyd_rf *, int, int);
18978cc4bbbSWeongyo Jeong static int	zyd_gct_write(struct zyd_rf *, uint16_t);
19078cc4bbbSWeongyo Jeong static int	zyd_gct_txgain(struct zyd_rf *, uint8_t);
19102ac6454SAndrew Thompson static int	zyd_maxim2_init(struct zyd_rf *);
19202ac6454SAndrew Thompson static int	zyd_maxim2_switch_radio(struct zyd_rf *, int);
19302ac6454SAndrew Thompson static int	zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
19402ac6454SAndrew Thompson 
19502ac6454SAndrew Thompson static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
19602ac6454SAndrew Thompson static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
19702ac6454SAndrew Thompson 
19802ac6454SAndrew Thompson /* various supported device vendors/products */
19902ac6454SAndrew Thompson #define ZYD_ZD1211	0
20002ac6454SAndrew Thompson #define ZYD_ZD1211B	1
20102ac6454SAndrew Thompson 
2029dde689cSWeongyo Jeong #define	ZYD_ZD1211_DEV(v,p)	\
2039dde689cSWeongyo Jeong 	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211) }
2049dde689cSWeongyo Jeong #define	ZYD_ZD1211B_DEV(v,p)	\
2059dde689cSWeongyo Jeong 	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211B) }
206760bc48eSAndrew Thompson static const struct usb_device_id zyd_devs[] = {
20702ac6454SAndrew Thompson 	/* ZYD_ZD1211 */
2089dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
2099dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ABOCOM, WL54),
2109dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ASUS, WL159G),
2119dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
2129dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
2139dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(PLANEX2, GWUS54GD),
2149dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
2159dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
2169dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
2179dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(SAGEM, XG760A),
2189dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(SENAO, NUB8301),
2199dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(SITECOMEU, WL113),
2209dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(SWEEX, ZD1211),
2219dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
2229dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
2239dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
2249dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(TWINMOS, G240),
2259dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
2269dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
2279dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
2289dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
2299dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ZCOM, ZD1211),
2309dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ZYDAS, ZD1211),
2319dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ZYXEL, AG225H),
2329dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
2339dde689cSWeongyo Jeong 	ZYD_ZD1211_DEV(ZYXEL, G200V2),
23402ac6454SAndrew Thompson 	/* ZYD_ZD1211B */
2359dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
2369dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
2379dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ASUS, A9T_WIFI),
2389dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(BELKIN, F5D7050_V4000),
2399dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
2409dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
2419dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
2429dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(MELCO, KG54L),
2439dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
2449dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(PLANEX2, GW_US54GXS),
2459dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(SAGEM, XG76NA),
2469dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
2479dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
2489dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(USR, USR5423),
2499dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(VTECH, ZD1211B),
2509dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
2519dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
2529dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ZYXEL, M202),
2539dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ZYXEL, G202),
2549dde689cSWeongyo Jeong 	ZYD_ZD1211B_DEV(ZYXEL, G220V2)
25502ac6454SAndrew Thompson };
25602ac6454SAndrew Thompson 
257760bc48eSAndrew Thompson static const struct usb_config zyd_config[ZYD_N_TRANSFER] = {
25802ac6454SAndrew Thompson 	[ZYD_BULK_WR] = {
25902ac6454SAndrew Thompson 		.type = UE_BULK,
26002ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
26102ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2624eae601eSAndrew Thompson 		.bufsize = ZYD_MAX_TXBUFSZ,
2634eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2644eae601eSAndrew Thompson 		.callback = zyd_bulk_write_callback,
26502ac6454SAndrew Thompson 		.ep_index = 0,
2664eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
26702ac6454SAndrew Thompson 	},
26802ac6454SAndrew Thompson 	[ZYD_BULK_RD] = {
26902ac6454SAndrew Thompson 		.type = UE_BULK,
27002ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
27102ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2724eae601eSAndrew Thompson 		.bufsize = ZYX_MAX_RXBUFSZ,
2734eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2744eae601eSAndrew Thompson 		.callback = zyd_bulk_read_callback,
27502ac6454SAndrew Thompson 		.ep_index = 0,
27602ac6454SAndrew Thompson 	},
27702ac6454SAndrew Thompson 	[ZYD_INTR_WR] = {
27802ac6454SAndrew Thompson 		.type = UE_BULK_INTR,
27902ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
28002ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2814eae601eSAndrew Thompson 		.bufsize = sizeof(struct zyd_cmd),
2824eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2834eae601eSAndrew Thompson 		.callback = zyd_intr_write_callback,
2844eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
28502ac6454SAndrew Thompson 		.ep_index = 1,
28602ac6454SAndrew Thompson 	},
28702ac6454SAndrew Thompson 	[ZYD_INTR_RD] = {
28802ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
28902ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
29002ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2914eae601eSAndrew Thompson 		.bufsize = sizeof(struct zyd_cmd),
2924eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2934eae601eSAndrew Thompson 		.callback = zyd_intr_read_callback,
29402ac6454SAndrew Thompson 	},
29502ac6454SAndrew Thompson };
29602ac6454SAndrew Thompson #define zyd_read16_m(sc, val, data)	do {				\
29702ac6454SAndrew Thompson 	error = zyd_read16(sc, val, data);				\
29802ac6454SAndrew Thompson 	if (error != 0)							\
29902ac6454SAndrew Thompson 		goto fail;						\
30002ac6454SAndrew Thompson } while (0)
30102ac6454SAndrew Thompson #define zyd_write16_m(sc, val, data)	do {				\
30202ac6454SAndrew Thompson 	error = zyd_write16(sc, val, data);				\
30302ac6454SAndrew Thompson 	if (error != 0)							\
30402ac6454SAndrew Thompson 		goto fail;						\
30502ac6454SAndrew Thompson } while (0)
30602ac6454SAndrew Thompson #define zyd_read32_m(sc, val, data)	do {				\
30702ac6454SAndrew Thompson 	error = zyd_read32(sc, val, data);				\
30802ac6454SAndrew Thompson 	if (error != 0)							\
30902ac6454SAndrew Thompson 		goto fail;						\
31002ac6454SAndrew Thompson } while (0)
31102ac6454SAndrew Thompson #define zyd_write32_m(sc, val, data)	do {				\
31202ac6454SAndrew Thompson 	error = zyd_write32(sc, val, data);				\
31302ac6454SAndrew Thompson 	if (error != 0)							\
31402ac6454SAndrew Thompson 		goto fail;						\
31502ac6454SAndrew Thompson } while (0)
31602ac6454SAndrew Thompson 
31702ac6454SAndrew Thompson static int
31802ac6454SAndrew Thompson zyd_match(device_t dev)
31902ac6454SAndrew Thompson {
320760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
32102ac6454SAndrew Thompson 
322f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST)
32302ac6454SAndrew Thompson 		return (ENXIO);
32402ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX)
32502ac6454SAndrew Thompson 		return (ENXIO);
32602ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX)
32702ac6454SAndrew Thompson 		return (ENXIO);
32802ac6454SAndrew Thompson 
329a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa));
33002ac6454SAndrew Thompson }
33102ac6454SAndrew Thompson 
33202ac6454SAndrew Thompson static int
33302ac6454SAndrew Thompson zyd_attach(device_t dev)
33402ac6454SAndrew Thompson {
335760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
33602ac6454SAndrew Thompson 	struct zyd_softc *sc = device_get_softc(dev);
3375efea30fSAndrew Thompson 	struct ifnet *ifp;
3385efea30fSAndrew Thompson 	struct ieee80211com *ic;
3395efea30fSAndrew Thompson 	uint8_t iface_index, bands;
34002ac6454SAndrew Thompson 	int error;
34102ac6454SAndrew Thompson 
34202ac6454SAndrew Thompson 	if (uaa->info.bcdDevice < 0x4330) {
34302ac6454SAndrew Thompson 		device_printf(dev, "device version mismatch: 0x%X "
34402ac6454SAndrew Thompson 		    "(only >= 43.30 supported)\n",
34502ac6454SAndrew Thompson 		    uaa->info.bcdDevice);
34602ac6454SAndrew Thompson 		return (EINVAL);
34702ac6454SAndrew Thompson 	}
34802ac6454SAndrew Thompson 
349a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
35002ac6454SAndrew Thompson 	sc->sc_dev = dev;
35102ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
35202ac6454SAndrew Thompson 	sc->sc_macrev = USB_GET_DRIVER_INFO(uaa);
35302ac6454SAndrew Thompson 
35402ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
35502ac6454SAndrew Thompson 	    MTX_NETWORK_LOCK, MTX_DEF);
35602ac6454SAndrew Thompson 	STAILQ_INIT(&sc->sc_rqh);
35702ac6454SAndrew Thompson 
35802ac6454SAndrew Thompson 	iface_index = ZYD_IFACE_INDEX;
359a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
36002ac6454SAndrew Thompson 	    &iface_index, sc->sc_xfer, zyd_config,
36102ac6454SAndrew Thompson 	    ZYD_N_TRANSFER, sc, &sc->sc_mtx);
36202ac6454SAndrew Thompson 	if (error) {
36302ac6454SAndrew Thompson 		device_printf(dev, "could not allocate USB transfers, "
364a593f6b8SAndrew Thompson 		    "err=%s\n", usbd_errstr(error));
36502ac6454SAndrew Thompson 		goto detach;
36602ac6454SAndrew Thompson 	}
36702ac6454SAndrew Thompson 
36802ac6454SAndrew Thompson 	ZYD_LOCK(sc);
36902ac6454SAndrew Thompson 	if ((error = zyd_get_macaddr(sc)) != 0) {
37002ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM\n");
3715efea30fSAndrew Thompson 		ZYD_UNLOCK(sc);
3725efea30fSAndrew Thompson 		goto detach;
37302ac6454SAndrew Thompson 	}
37402ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
37502ac6454SAndrew Thompson 
37602ac6454SAndrew Thompson 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
37702ac6454SAndrew Thompson 	if (ifp == NULL) {
37802ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "can not if_alloc()\n");
3795efea30fSAndrew Thompson 		goto detach;
38002ac6454SAndrew Thompson 	}
38102ac6454SAndrew Thompson 	ifp->if_softc = sc;
38202ac6454SAndrew Thompson 	if_initname(ifp, "zyd", device_get_unit(sc->sc_dev));
38302ac6454SAndrew Thompson 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
38402ac6454SAndrew Thompson 	ifp->if_init = zyd_init;
38502ac6454SAndrew Thompson 	ifp->if_ioctl = zyd_ioctl;
38602ac6454SAndrew Thompson 	ifp->if_start = zyd_start;
38702ac6454SAndrew Thompson 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
38802ac6454SAndrew Thompson 	IFQ_SET_READY(&ifp->if_snd);
38902ac6454SAndrew Thompson 
39002ac6454SAndrew Thompson 	ic = ifp->if_l2com;
39102ac6454SAndrew Thompson 	ic->ic_ifp = ifp;
39202ac6454SAndrew Thompson 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
39302ac6454SAndrew Thompson 	ic->ic_opmode = IEEE80211_M_STA;
39402ac6454SAndrew Thompson 
39502ac6454SAndrew Thompson 	/* set device capabilities */
39602ac6454SAndrew Thompson 	ic->ic_caps =
39702ac6454SAndrew Thompson 		  IEEE80211_C_STA		/* station mode */
39802ac6454SAndrew Thompson 		| IEEE80211_C_MONITOR		/* monitor mode */
39902ac6454SAndrew Thompson 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
40002ac6454SAndrew Thompson 	        | IEEE80211_C_SHSLOT		/* short slot time supported */
40102ac6454SAndrew Thompson 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
40202ac6454SAndrew Thompson 	        | IEEE80211_C_WPA		/* 802.11i */
40302ac6454SAndrew Thompson 		;
40402ac6454SAndrew Thompson 
40502ac6454SAndrew Thompson 	bands = 0;
40602ac6454SAndrew Thompson 	setbit(&bands, IEEE80211_MODE_11B);
40702ac6454SAndrew Thompson 	setbit(&bands, IEEE80211_MODE_11G);
40802ac6454SAndrew Thompson 	ieee80211_init_channels(ic, NULL, &bands);
40902ac6454SAndrew Thompson 
41029aca940SSam Leffler 	ieee80211_ifattach(ic, sc->sc_bssid);
41102ac6454SAndrew Thompson 	ic->ic_newassoc = zyd_newassoc;
41202ac6454SAndrew Thompson 	ic->ic_raw_xmit = zyd_raw_xmit;
41302ac6454SAndrew Thompson 	ic->ic_node_alloc = zyd_node_alloc;
41402ac6454SAndrew Thompson 	ic->ic_scan_start = zyd_scan_start;
41502ac6454SAndrew Thompson 	ic->ic_scan_end = zyd_scan_end;
41602ac6454SAndrew Thompson 	ic->ic_set_channel = zyd_set_channel;
41702ac6454SAndrew Thompson 
41802ac6454SAndrew Thompson 	ic->ic_vap_create = zyd_vap_create;
41902ac6454SAndrew Thompson 	ic->ic_vap_delete = zyd_vap_delete;
42002ac6454SAndrew Thompson 	ic->ic_update_mcast = zyd_update_mcast;
42102ac6454SAndrew Thompson 	ic->ic_update_promisc = zyd_update_mcast;
42202ac6454SAndrew Thompson 
4235463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
4245463c4a4SSam Leffler 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
4255463c4a4SSam Leffler 		ZYD_TX_RADIOTAP_PRESENT,
4265463c4a4SSam Leffler 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
4275463c4a4SSam Leffler 		ZYD_RX_RADIOTAP_PRESENT);
42802ac6454SAndrew Thompson 
42902ac6454SAndrew Thompson 	if (bootverbose)
43002ac6454SAndrew Thompson 		ieee80211_announce(ic);
43102ac6454SAndrew Thompson 
4325efea30fSAndrew Thompson 	return (0);
4335efea30fSAndrew Thompson 
4345efea30fSAndrew Thompson detach:
4355efea30fSAndrew Thompson 	zyd_detach(dev);
4365efea30fSAndrew Thompson 	return (ENXIO);			/* failure */
43702ac6454SAndrew Thompson }
43802ac6454SAndrew Thompson 
43902ac6454SAndrew Thompson static int
44002ac6454SAndrew Thompson zyd_detach(device_t dev)
44102ac6454SAndrew Thompson {
44202ac6454SAndrew Thompson 	struct zyd_softc *sc = device_get_softc(dev);
44302ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
44402ac6454SAndrew Thompson 	struct ieee80211com *ic;
44502ac6454SAndrew Thompson 
44602ac6454SAndrew Thompson 	/* stop all USB transfers */
447a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER);
44802ac6454SAndrew Thompson 
44902ac6454SAndrew Thompson 	/* free TX list, if any */
45002ac6454SAndrew Thompson 	zyd_unsetup_tx_list(sc);
45102ac6454SAndrew Thompson 
45202ac6454SAndrew Thompson 	if (ifp) {
45302ac6454SAndrew Thompson 		ic = ifp->if_l2com;
45402ac6454SAndrew Thompson 		ieee80211_ifdetach(ic);
45502ac6454SAndrew Thompson 		if_free(ifp);
45602ac6454SAndrew Thompson 	}
45702ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
45802ac6454SAndrew Thompson 
45902ac6454SAndrew Thompson 	return (0);
46002ac6454SAndrew Thompson }
46102ac6454SAndrew Thompson 
46202ac6454SAndrew Thompson static struct ieee80211vap *
46302ac6454SAndrew Thompson zyd_vap_create(struct ieee80211com *ic,
46402ac6454SAndrew Thompson 	const char name[IFNAMSIZ], int unit, int opmode, int flags,
46502ac6454SAndrew Thompson 	const uint8_t bssid[IEEE80211_ADDR_LEN],
46602ac6454SAndrew Thompson 	const uint8_t mac[IEEE80211_ADDR_LEN])
46702ac6454SAndrew Thompson {
46802ac6454SAndrew Thompson 	struct zyd_vap *zvp;
46902ac6454SAndrew Thompson 	struct ieee80211vap *vap;
47002ac6454SAndrew Thompson 
47102ac6454SAndrew Thompson 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
47202ac6454SAndrew Thompson 		return (NULL);
47302ac6454SAndrew Thompson 	zvp = (struct zyd_vap *) malloc(sizeof(struct zyd_vap),
47402ac6454SAndrew Thompson 	    M_80211_VAP, M_NOWAIT | M_ZERO);
47502ac6454SAndrew Thompson 	if (zvp == NULL)
47602ac6454SAndrew Thompson 		return (NULL);
47702ac6454SAndrew Thompson 	vap = &zvp->vap;
47802ac6454SAndrew Thompson 	/* enable s/w bmiss handling for sta mode */
47902ac6454SAndrew Thompson 	ieee80211_vap_setup(ic, vap, name, unit, opmode,
48002ac6454SAndrew Thompson 	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
48102ac6454SAndrew Thompson 
48202ac6454SAndrew Thompson 	/* override state transition machine */
48302ac6454SAndrew Thompson 	zvp->newstate = vap->iv_newstate;
48402ac6454SAndrew Thompson 	vap->iv_newstate = zyd_newstate;
48502ac6454SAndrew Thompson 
48602ac6454SAndrew Thompson 	ieee80211_amrr_init(&zvp->amrr, vap,
48702ac6454SAndrew Thompson 	    IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
48802ac6454SAndrew Thompson 	    IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD,
48902ac6454SAndrew Thompson 	    1000 /* 1 sec */);
49002ac6454SAndrew Thompson 
49102ac6454SAndrew Thompson 	/* complete setup */
49202ac6454SAndrew Thompson 	ieee80211_vap_attach(vap, ieee80211_media_change,
49302ac6454SAndrew Thompson 	    ieee80211_media_status);
49402ac6454SAndrew Thompson 	ic->ic_opmode = opmode;
49502ac6454SAndrew Thompson 	return (vap);
49602ac6454SAndrew Thompson }
49702ac6454SAndrew Thompson 
49802ac6454SAndrew Thompson static void
49902ac6454SAndrew Thompson zyd_vap_delete(struct ieee80211vap *vap)
50002ac6454SAndrew Thompson {
50102ac6454SAndrew Thompson 	struct zyd_vap *zvp = ZYD_VAP(vap);
50202ac6454SAndrew Thompson 
50302ac6454SAndrew Thompson 	ieee80211_amrr_cleanup(&zvp->amrr);
50402ac6454SAndrew Thompson 	ieee80211_vap_detach(vap);
50502ac6454SAndrew Thompson 	free(zvp, M_80211_VAP);
50602ac6454SAndrew Thompson }
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson static void
50902ac6454SAndrew Thompson zyd_tx_free(struct zyd_tx_data *data, int txerr)
51002ac6454SAndrew Thompson {
51102ac6454SAndrew Thompson 	struct zyd_softc *sc = data->sc;
51202ac6454SAndrew Thompson 
51302ac6454SAndrew Thompson 	if (data->m != NULL) {
51402ac6454SAndrew Thompson 		if (data->m->m_flags & M_TXCB)
51502ac6454SAndrew Thompson 			ieee80211_process_callback(data->ni, data->m,
51602ac6454SAndrew Thompson 			    txerr ? ETIMEDOUT : 0);
51702ac6454SAndrew Thompson 		m_freem(data->m);
51802ac6454SAndrew Thompson 		data->m = NULL;
51902ac6454SAndrew Thompson 
52090cbee43SWeongyo Jeong 		if (txerr == 0)
52190cbee43SWeongyo Jeong 			ieee80211_amrr_tx_complete(&ZYD_NODE(data->ni)->amn,
52290cbee43SWeongyo Jeong 			    IEEE80211_AMRR_SUCCESS, 0);
52302ac6454SAndrew Thompson 		ieee80211_free_node(data->ni);
52402ac6454SAndrew Thompson 		data->ni = NULL;
52502ac6454SAndrew Thompson 	}
52602ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
52702ac6454SAndrew Thompson 	sc->tx_nfree++;
52802ac6454SAndrew Thompson }
52902ac6454SAndrew Thompson 
53002ac6454SAndrew Thompson static void
53102ac6454SAndrew Thompson zyd_setup_tx_list(struct zyd_softc *sc)
53202ac6454SAndrew Thompson {
53302ac6454SAndrew Thompson 	struct zyd_tx_data *data;
53402ac6454SAndrew Thompson 	int i;
53502ac6454SAndrew Thompson 
53602ac6454SAndrew Thompson 	sc->tx_nfree = 0;
53702ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
53802ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
53902ac6454SAndrew Thompson 
54002ac6454SAndrew Thompson 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
54102ac6454SAndrew Thompson 		data = &sc->tx_data[i];
54202ac6454SAndrew Thompson 
54302ac6454SAndrew Thompson 		data->sc = sc;
54402ac6454SAndrew Thompson 		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
54502ac6454SAndrew Thompson 		sc->tx_nfree++;
54602ac6454SAndrew Thompson 	}
54702ac6454SAndrew Thompson }
54802ac6454SAndrew Thompson 
54902ac6454SAndrew Thompson static void
55002ac6454SAndrew Thompson zyd_unsetup_tx_list(struct zyd_softc *sc)
55102ac6454SAndrew Thompson {
55202ac6454SAndrew Thompson 	struct zyd_tx_data *data;
55302ac6454SAndrew Thompson 	int i;
55402ac6454SAndrew Thompson 
55502ac6454SAndrew Thompson 	/* make sure any subsequent use of the queues will fail */
55602ac6454SAndrew Thompson 	sc->tx_nfree = 0;
55702ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
55802ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
55902ac6454SAndrew Thompson 
56002ac6454SAndrew Thompson 	/* free up all node references and mbufs */
56102ac6454SAndrew Thompson 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
56202ac6454SAndrew Thompson 		data = &sc->tx_data[i];
56302ac6454SAndrew Thompson 
56402ac6454SAndrew Thompson 		if (data->m != NULL) {
56502ac6454SAndrew Thompson 			m_freem(data->m);
56602ac6454SAndrew Thompson 			data->m = NULL;
56702ac6454SAndrew Thompson 		}
56802ac6454SAndrew Thompson 		if (data->ni != NULL) {
56902ac6454SAndrew Thompson 			ieee80211_free_node(data->ni);
57002ac6454SAndrew Thompson 			data->ni = NULL;
57102ac6454SAndrew Thompson 		}
57202ac6454SAndrew Thompson 	}
57302ac6454SAndrew Thompson }
57402ac6454SAndrew Thompson 
57502ac6454SAndrew Thompson /* ARGUSED */
57602ac6454SAndrew Thompson static struct ieee80211_node *
57702ac6454SAndrew Thompson zyd_node_alloc(struct ieee80211vap *vap __unused,
57802ac6454SAndrew Thompson 	const uint8_t mac[IEEE80211_ADDR_LEN] __unused)
57902ac6454SAndrew Thompson {
58002ac6454SAndrew Thompson 	struct zyd_node *zn;
58102ac6454SAndrew Thompson 
58202ac6454SAndrew Thompson 	zn = malloc(sizeof(struct zyd_node), M_80211_NODE, M_NOWAIT | M_ZERO);
58302ac6454SAndrew Thompson 	return (zn != NULL) ? (&zn->ni) : (NULL);
58402ac6454SAndrew Thompson }
58502ac6454SAndrew Thompson 
5865efea30fSAndrew Thompson static int
5875efea30fSAndrew Thompson zyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
58802ac6454SAndrew Thompson {
58902ac6454SAndrew Thompson 	struct zyd_vap *zvp = ZYD_VAP(vap);
5905efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
5915efea30fSAndrew Thompson 	struct zyd_softc *sc = ic->ic_ifp->if_softc;
5925efea30fSAndrew Thompson 	struct ieee80211_node *ni;
59302ac6454SAndrew Thompson 	int error;
59402ac6454SAndrew Thompson 
5955efea30fSAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__,
5965efea30fSAndrew Thompson 	    ieee80211_state_name[vap->iv_state],
5975efea30fSAndrew Thompson 	    ieee80211_state_name[nstate]);
5985efea30fSAndrew Thompson 
5995efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
6005efea30fSAndrew Thompson 	ZYD_LOCK(sc);
6015efea30fSAndrew Thompson 	switch (nstate) {
60202ac6454SAndrew Thompson 	case IEEE80211_S_AUTH:
60302ac6454SAndrew Thompson 		zyd_set_chan(sc, ic->ic_curchan);
60402ac6454SAndrew Thompson 		break;
60502ac6454SAndrew Thompson 	case IEEE80211_S_RUN:
6065efea30fSAndrew Thompson 		ni = vap->iv_bss;
60702ac6454SAndrew Thompson 		if (vap->iv_opmode == IEEE80211_M_MONITOR)
60802ac6454SAndrew Thompson 			break;
60902ac6454SAndrew Thompson 
61002ac6454SAndrew Thompson 		/* turn link LED on */
61102ac6454SAndrew Thompson 		error = zyd_set_led(sc, ZYD_LED1, 1);
61202ac6454SAndrew Thompson 		if (error != 0)
6135efea30fSAndrew Thompson 			break;
61402ac6454SAndrew Thompson 
61502ac6454SAndrew Thompson 		/* make data LED blink upon Tx */
61602ac6454SAndrew Thompson 		zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1);
61702ac6454SAndrew Thompson 
61802ac6454SAndrew Thompson 		IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
61902ac6454SAndrew Thompson 		zyd_set_bssid(sc, sc->sc_bssid);
62002ac6454SAndrew Thompson 		break;
62102ac6454SAndrew Thompson 	default:
62202ac6454SAndrew Thompson 		break;
62302ac6454SAndrew Thompson 	}
62402ac6454SAndrew Thompson fail:
62502ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
62602ac6454SAndrew Thompson 	IEEE80211_LOCK(ic);
6275efea30fSAndrew Thompson 	return (zvp->newstate(vap, nstate, arg));
62802ac6454SAndrew Thompson }
62902ac6454SAndrew Thompson 
63002ac6454SAndrew Thompson /*
63102ac6454SAndrew Thompson  * Callback handler for interrupt transfer
63202ac6454SAndrew Thompson  */
63302ac6454SAndrew Thompson static void
634ed6d949aSAndrew Thompson zyd_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
63502ac6454SAndrew Thompson {
636ed6d949aSAndrew Thompson 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
63702ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
63802ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
63902ac6454SAndrew Thompson 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
64002ac6454SAndrew Thompson 	struct ieee80211_node *ni;
64102ac6454SAndrew Thompson 	struct zyd_cmd *cmd = &sc->sc_ibuf;
642ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
64302ac6454SAndrew Thompson 	int datalen;
644ed6d949aSAndrew Thompson 	int actlen;
645ed6d949aSAndrew Thompson 
646ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
64702ac6454SAndrew Thompson 
64802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
64902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
650ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
651ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, cmd, sizeof(*cmd));
65202ac6454SAndrew Thompson 
65302ac6454SAndrew Thompson 		switch (le16toh(cmd->code)) {
65402ac6454SAndrew Thompson 		case ZYD_NOTIF_RETRYSTATUS:
65502ac6454SAndrew Thompson 		{
65602ac6454SAndrew Thompson 			struct zyd_notif_retry *retry =
65702ac6454SAndrew Thompson 			    (struct zyd_notif_retry *)cmd->data;
65802ac6454SAndrew Thompson 
65902ac6454SAndrew Thompson 			DPRINTF(sc, ZYD_DEBUG_TX_PROC,
66002ac6454SAndrew Thompson 			    "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
66102ac6454SAndrew Thompson 			    le16toh(retry->rate), ether_sprintf(retry->macaddr),
66202ac6454SAndrew Thompson 			    le16toh(retry->count)&0xff, le16toh(retry->count));
66302ac6454SAndrew Thompson 
66402ac6454SAndrew Thompson 			/*
66502ac6454SAndrew Thompson 			 * Find the node to which the packet was sent and
66602ac6454SAndrew Thompson 			 * update its retry statistics.  In BSS mode, this node
66702ac6454SAndrew Thompson 			 * is the AP we're associated to so no lookup is
66802ac6454SAndrew Thompson 			 * actually needed.
66902ac6454SAndrew Thompson 			 */
67002ac6454SAndrew Thompson 			ni = ieee80211_find_txnode(vap, retry->macaddr);
67102ac6454SAndrew Thompson 			if (ni != NULL) {
67202ac6454SAndrew Thompson 				ieee80211_amrr_tx_complete(&ZYD_NODE(ni)->amn,
6737f97ca03SWeongyo Jeong 				    IEEE80211_AMRR_FAILURE,
6747f97ca03SWeongyo Jeong 				    (int)(le16toh(retry->count) & 0xff));
67502ac6454SAndrew Thompson 				ieee80211_free_node(ni);
67602ac6454SAndrew Thompson 			}
67702ac6454SAndrew Thompson 			if (le16toh(retry->count) & 0x100)
67802ac6454SAndrew Thompson 				ifp->if_oerrors++;	/* too many retries */
67902ac6454SAndrew Thompson 			break;
68002ac6454SAndrew Thompson 		}
68102ac6454SAndrew Thompson 		case ZYD_NOTIF_IORD:
68202ac6454SAndrew Thompson 		{
68302ac6454SAndrew Thompson 			struct zyd_rq *rqp;
68402ac6454SAndrew Thompson 
68502ac6454SAndrew Thompson 			if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
68602ac6454SAndrew Thompson 				break;	/* HMAC interrupt */
68702ac6454SAndrew Thompson 
688ed6d949aSAndrew Thompson 			datalen = actlen - sizeof(cmd->code);
68902ac6454SAndrew Thompson 			datalen -= 2;	/* XXX: padding? */
69002ac6454SAndrew Thompson 
69102ac6454SAndrew Thompson 			STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
69202ac6454SAndrew Thompson 				int i, cnt;
69302ac6454SAndrew Thompson 
69402ac6454SAndrew Thompson 				if (rqp->olen != datalen)
69502ac6454SAndrew Thompson 					continue;
69602ac6454SAndrew Thompson 				cnt = rqp->olen / sizeof(struct zyd_pair);
69702ac6454SAndrew Thompson 				for (i = 0; i < cnt; i++) {
69802ac6454SAndrew Thompson 					if (*(((const uint16_t *)rqp->idata) + i) !=
69902ac6454SAndrew Thompson 					    (((struct zyd_pair *)cmd->data) + i)->reg)
70002ac6454SAndrew Thompson 						break;
70102ac6454SAndrew Thompson 				}
70202ac6454SAndrew Thompson 				if (i != cnt)
70302ac6454SAndrew Thompson 					continue;
70402ac6454SAndrew Thompson 				/* copy answer into caller-supplied buffer */
70502ac6454SAndrew Thompson 				bcopy(cmd->data, rqp->odata, rqp->olen);
70602ac6454SAndrew Thompson 				DPRINTF(sc, ZYD_DEBUG_CMD,
70702ac6454SAndrew Thompson 				    "command %p complete, data = %*D \n",
70802ac6454SAndrew Thompson 				    rqp, rqp->olen, rqp->odata, ":");
70902ac6454SAndrew Thompson 				wakeup(rqp);	/* wakeup caller */
71002ac6454SAndrew Thompson 				break;
71102ac6454SAndrew Thompson 			}
71202ac6454SAndrew Thompson 			if (rqp == NULL) {
71302ac6454SAndrew Thompson 				device_printf(sc->sc_dev,
71402ac6454SAndrew Thompson 				    "unexpected IORD notification %*D\n",
71502ac6454SAndrew Thompson 				    datalen, cmd->data, ":");
71602ac6454SAndrew Thompson 			}
71702ac6454SAndrew Thompson 			break;
71802ac6454SAndrew Thompson 		}
71902ac6454SAndrew Thompson 		default:
72002ac6454SAndrew Thompson 			device_printf(sc->sc_dev, "unknown notification %x\n",
72102ac6454SAndrew Thompson 			    le16toh(cmd->code));
72202ac6454SAndrew Thompson 		}
72302ac6454SAndrew Thompson 
72402ac6454SAndrew Thompson 		/* FALLTHROUGH */
72502ac6454SAndrew Thompson 	case USB_ST_SETUP:
72602ac6454SAndrew Thompson tr_setup:
727ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
728a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
72902ac6454SAndrew Thompson 		break;
73002ac6454SAndrew Thompson 
73102ac6454SAndrew Thompson 	default:			/* Error */
73202ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
733ed6d949aSAndrew Thompson 		    usbd_errstr(error));
73402ac6454SAndrew Thompson 
735ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
73602ac6454SAndrew Thompson 			/* try to clear stall first */
737ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
73802ac6454SAndrew Thompson 			goto tr_setup;
73902ac6454SAndrew Thompson 		}
74002ac6454SAndrew Thompson 		break;
74102ac6454SAndrew Thompson 	}
74202ac6454SAndrew Thompson }
74302ac6454SAndrew Thompson 
74402ac6454SAndrew Thompson static void
745ed6d949aSAndrew Thompson zyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
74602ac6454SAndrew Thompson {
747ed6d949aSAndrew Thompson 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
748ed6d949aSAndrew Thompson 	struct zyd_rq *rqp, *cmd;
749ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
75002ac6454SAndrew Thompson 
75102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
75202ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
753ed6d949aSAndrew Thompson 		cmd = usbd_xfer_get_priv(xfer);
754ed6d949aSAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd);
755d953f720SAndrew Thompson 		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
756d953f720SAndrew Thompson 			/* Ensure the cached rq pointer is still valid */
757ed6d949aSAndrew Thompson 			if (rqp == cmd &&
758d953f720SAndrew Thompson 			    (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
75902ac6454SAndrew Thompson 				wakeup(rqp);	/* wakeup caller */
760d953f720SAndrew Thompson 		}
76102ac6454SAndrew Thompson 
76202ac6454SAndrew Thompson 		/* FALLTHROUGH */
76302ac6454SAndrew Thompson 	case USB_ST_SETUP:
76402ac6454SAndrew Thompson tr_setup:
76502ac6454SAndrew Thompson 		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
76602ac6454SAndrew Thompson 			if (rqp->flags & ZYD_CMD_FLAG_SENT)
76702ac6454SAndrew Thompson 				continue;
76802ac6454SAndrew Thompson 
769ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
770ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen);
77102ac6454SAndrew Thompson 
772ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, rqp->ilen);
773ed6d949aSAndrew Thompson 			usbd_xfer_set_priv(xfer, rqp);
77402ac6454SAndrew Thompson 			rqp->flags |= ZYD_CMD_FLAG_SENT;
775a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
77602ac6454SAndrew Thompson 			break;
77702ac6454SAndrew Thompson 		}
77802ac6454SAndrew Thompson 		break;
77902ac6454SAndrew Thompson 
78002ac6454SAndrew Thompson 	default:			/* Error */
78102ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
782ed6d949aSAndrew Thompson 		    usbd_errstr(error));
78302ac6454SAndrew Thompson 
784ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
78502ac6454SAndrew Thompson 			/* try to clear stall first */
786ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
78702ac6454SAndrew Thompson 			goto tr_setup;
78802ac6454SAndrew Thompson 		}
78902ac6454SAndrew Thompson 		break;
79002ac6454SAndrew Thompson 	}
79102ac6454SAndrew Thompson }
79202ac6454SAndrew Thompson 
79302ac6454SAndrew Thompson static int
79402ac6454SAndrew Thompson zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
79502ac6454SAndrew Thompson     void *odata, int olen, int flags)
79602ac6454SAndrew Thompson {
79702ac6454SAndrew Thompson 	struct zyd_cmd cmd;
79802ac6454SAndrew Thompson 	struct zyd_rq rq;
79902ac6454SAndrew Thompson 	int error;
80002ac6454SAndrew Thompson 
80102ac6454SAndrew Thompson 	if (ilen > sizeof(cmd.data))
80202ac6454SAndrew Thompson 		return (EINVAL);
80302ac6454SAndrew Thompson 
80402ac6454SAndrew Thompson 	cmd.code = htole16(code);
80502ac6454SAndrew Thompson 	bcopy(idata, cmd.data, ilen);
80602ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
80702ac6454SAndrew Thompson 	    &rq, ilen, idata, ":");
80802ac6454SAndrew Thompson 
80902ac6454SAndrew Thompson 	rq.cmd = &cmd;
81002ac6454SAndrew Thompson 	rq.idata = idata;
81102ac6454SAndrew Thompson 	rq.odata = odata;
81202ac6454SAndrew Thompson 	rq.ilen = sizeof(uint16_t) + ilen;
81302ac6454SAndrew Thompson 	rq.olen = olen;
81402ac6454SAndrew Thompson 	rq.flags = flags;
81502ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
816a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
817a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
81802ac6454SAndrew Thompson 
81902ac6454SAndrew Thompson 	/* wait at most one second for command reply */
82002ac6454SAndrew Thompson 	error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
82102ac6454SAndrew Thompson 	if (error)
82202ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "command timeout\n");
82302ac6454SAndrew Thompson 	STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
82402ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
82502ac6454SAndrew Thompson 	    &rq, error);
82602ac6454SAndrew Thompson 
82702ac6454SAndrew Thompson 	return (error);
82802ac6454SAndrew Thompson }
82902ac6454SAndrew Thompson 
83002ac6454SAndrew Thompson static int
83102ac6454SAndrew Thompson zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
83202ac6454SAndrew Thompson {
83302ac6454SAndrew Thompson 	struct zyd_pair tmp;
83402ac6454SAndrew Thompson 	int error;
83502ac6454SAndrew Thompson 
83602ac6454SAndrew Thompson 	reg = htole16(reg);
83702ac6454SAndrew Thompson 	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
83802ac6454SAndrew Thompson 	    ZYD_CMD_FLAG_READ);
83902ac6454SAndrew Thompson 	if (error == 0)
84002ac6454SAndrew Thompson 		*val = le16toh(tmp.val);
84102ac6454SAndrew Thompson 	return (error);
84202ac6454SAndrew Thompson }
84302ac6454SAndrew Thompson 
84402ac6454SAndrew Thompson static int
84502ac6454SAndrew Thompson zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
84602ac6454SAndrew Thompson {
84702ac6454SAndrew Thompson 	struct zyd_pair tmp[2];
84802ac6454SAndrew Thompson 	uint16_t regs[2];
84902ac6454SAndrew Thompson 	int error;
85002ac6454SAndrew Thompson 
85102ac6454SAndrew Thompson 	regs[0] = htole16(ZYD_REG32_HI(reg));
85202ac6454SAndrew Thompson 	regs[1] = htole16(ZYD_REG32_LO(reg));
85302ac6454SAndrew Thompson 	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
85402ac6454SAndrew Thompson 	    ZYD_CMD_FLAG_READ);
85502ac6454SAndrew Thompson 	if (error == 0)
85602ac6454SAndrew Thompson 		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
85702ac6454SAndrew Thompson 	return (error);
85802ac6454SAndrew Thompson }
85902ac6454SAndrew Thompson 
86002ac6454SAndrew Thompson static int
86102ac6454SAndrew Thompson zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
86202ac6454SAndrew Thompson {
86302ac6454SAndrew Thompson 	struct zyd_pair pair;
86402ac6454SAndrew Thompson 
86502ac6454SAndrew Thompson 	pair.reg = htole16(reg);
86602ac6454SAndrew Thompson 	pair.val = htole16(val);
86702ac6454SAndrew Thompson 
86802ac6454SAndrew Thompson 	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
86902ac6454SAndrew Thompson }
87002ac6454SAndrew Thompson 
87102ac6454SAndrew Thompson static int
87202ac6454SAndrew Thompson zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
87302ac6454SAndrew Thompson {
87402ac6454SAndrew Thompson 	struct zyd_pair pair[2];
87502ac6454SAndrew Thompson 
87602ac6454SAndrew Thompson 	pair[0].reg = htole16(ZYD_REG32_HI(reg));
87702ac6454SAndrew Thompson 	pair[0].val = htole16(val >> 16);
87802ac6454SAndrew Thompson 	pair[1].reg = htole16(ZYD_REG32_LO(reg));
87902ac6454SAndrew Thompson 	pair[1].val = htole16(val & 0xffff);
88002ac6454SAndrew Thompson 
88102ac6454SAndrew Thompson 	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
88202ac6454SAndrew Thompson }
88302ac6454SAndrew Thompson 
88402ac6454SAndrew Thompson static int
88502ac6454SAndrew Thompson zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
88602ac6454SAndrew Thompson {
88702ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
88802ac6454SAndrew Thompson 	struct zyd_rfwrite_cmd req;
88902ac6454SAndrew Thompson 	uint16_t cr203;
89002ac6454SAndrew Thompson 	int error, i;
89102ac6454SAndrew Thompson 
89202ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &cr203);
89302ac6454SAndrew Thompson 	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
89402ac6454SAndrew Thompson 
89502ac6454SAndrew Thompson 	req.code  = htole16(2);
89602ac6454SAndrew Thompson 	req.width = htole16(rf->width);
89702ac6454SAndrew Thompson 	for (i = 0; i < rf->width; i++) {
89802ac6454SAndrew Thompson 		req.bit[i] = htole16(cr203);
89902ac6454SAndrew Thompson 		if (val & (1 << (rf->width - 1 - i)))
90002ac6454SAndrew Thompson 			req.bit[i] |= htole16(ZYD_RF_DATA);
90102ac6454SAndrew Thompson 	}
90202ac6454SAndrew Thompson 	error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
90302ac6454SAndrew Thompson fail:
90402ac6454SAndrew Thompson 	return (error);
90502ac6454SAndrew Thompson }
90602ac6454SAndrew Thompson 
90702ac6454SAndrew Thompson static int
90802ac6454SAndrew Thompson zyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
90902ac6454SAndrew Thompson {
91002ac6454SAndrew Thompson 	int error;
91102ac6454SAndrew Thompson 
91202ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
91302ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR243, (val >>  8) & 0xff);
91402ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR242, (val >>  0) & 0xff);
91502ac6454SAndrew Thompson fail:
91602ac6454SAndrew Thompson 	return (error);
91702ac6454SAndrew Thompson }
91802ac6454SAndrew Thompson 
91902ac6454SAndrew Thompson static int
92002ac6454SAndrew Thompson zyd_lock_phy(struct zyd_softc *sc)
92102ac6454SAndrew Thompson {
92202ac6454SAndrew Thompson 	int error;
92302ac6454SAndrew Thompson 	uint32_t tmp;
92402ac6454SAndrew Thompson 
92502ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
92602ac6454SAndrew Thompson 	tmp &= ~ZYD_UNLOCK_PHY_REGS;
92702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
92802ac6454SAndrew Thompson fail:
92902ac6454SAndrew Thompson 	return (error);
93002ac6454SAndrew Thompson }
93102ac6454SAndrew Thompson 
93202ac6454SAndrew Thompson static int
93302ac6454SAndrew Thompson zyd_unlock_phy(struct zyd_softc *sc)
93402ac6454SAndrew Thompson {
93502ac6454SAndrew Thompson 	int error;
93602ac6454SAndrew Thompson 	uint32_t tmp;
93702ac6454SAndrew Thompson 
93802ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
93902ac6454SAndrew Thompson 	tmp |= ZYD_UNLOCK_PHY_REGS;
94002ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
94102ac6454SAndrew Thompson fail:
94202ac6454SAndrew Thompson 	return (error);
94302ac6454SAndrew Thompson }
94402ac6454SAndrew Thompson 
94502ac6454SAndrew Thompson /*
94602ac6454SAndrew Thompson  * RFMD RF methods.
94702ac6454SAndrew Thompson  */
94802ac6454SAndrew Thompson static int
94902ac6454SAndrew Thompson zyd_rfmd_init(struct zyd_rf *rf)
95002ac6454SAndrew Thompson {
95102ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
95202ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
95302ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
95402ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_RFMD_RF;
95502ac6454SAndrew Thompson 	int i, error;
95602ac6454SAndrew Thompson 
95702ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
95802ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++) {
95902ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
96002ac6454SAndrew Thompson 	}
96102ac6454SAndrew Thompson 
96202ac6454SAndrew Thompson 	/* init RFMD radio */
96302ac6454SAndrew Thompson 	for (i = 0; i < N(rfini); i++) {
96402ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
96502ac6454SAndrew Thompson 			return (error);
96602ac6454SAndrew Thompson 	}
96702ac6454SAndrew Thompson fail:
96802ac6454SAndrew Thompson 	return (error);
96902ac6454SAndrew Thompson #undef N
97002ac6454SAndrew Thompson }
97102ac6454SAndrew Thompson 
97202ac6454SAndrew Thompson static int
97302ac6454SAndrew Thompson zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
97402ac6454SAndrew Thompson {
97502ac6454SAndrew Thompson 	int error;
97602ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
97702ac6454SAndrew Thompson 
97802ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
97902ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
98002ac6454SAndrew Thompson fail:
98102ac6454SAndrew Thompson 	return (error);
98202ac6454SAndrew Thompson }
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson static int
98502ac6454SAndrew Thompson zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
98602ac6454SAndrew Thompson {
98702ac6454SAndrew Thompson 	int error;
98802ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
98902ac6454SAndrew Thompson 	static const struct {
99002ac6454SAndrew Thompson 		uint32_t	r1, r2;
99102ac6454SAndrew Thompson 	} rfprog[] = ZYD_RFMD_CHANTABLE;
99202ac6454SAndrew Thompson 
99302ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
99402ac6454SAndrew Thompson 	if (error != 0)
99502ac6454SAndrew Thompson 		goto fail;
99602ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
99702ac6454SAndrew Thompson 	if (error != 0)
99802ac6454SAndrew Thompson 		goto fail;
99902ac6454SAndrew Thompson 
100002ac6454SAndrew Thompson fail:
100102ac6454SAndrew Thompson 	return (error);
100202ac6454SAndrew Thompson }
100302ac6454SAndrew Thompson 
100402ac6454SAndrew Thompson /*
100502ac6454SAndrew Thompson  * AL2230 RF methods.
100602ac6454SAndrew Thompson  */
100702ac6454SAndrew Thompson static int
100802ac6454SAndrew Thompson zyd_al2230_init(struct zyd_rf *rf)
100902ac6454SAndrew Thompson {
101002ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
101102ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
101202ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
101302ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
101402ac6454SAndrew Thompson 	static const struct zyd_phy_pair phypll[] = {
101502ac6454SAndrew Thompson 		{ ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
101602ac6454SAndrew Thompson 		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
101702ac6454SAndrew Thompson 	};
101802ac6454SAndrew Thompson 	static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
101902ac6454SAndrew Thompson 	static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
102002ac6454SAndrew Thompson 	static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
102102ac6454SAndrew Thompson 	int i, error;
102202ac6454SAndrew Thompson 
102302ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
102402ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
102502ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
102602ac6454SAndrew Thompson 
102702ac6454SAndrew Thompson 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
102802ac6454SAndrew Thompson 		for (i = 0; i < N(phy2230s); i++)
102902ac6454SAndrew Thompson 			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
103002ac6454SAndrew Thompson 	}
103102ac6454SAndrew Thompson 
103202ac6454SAndrew Thompson 	/* init AL2230 radio */
103302ac6454SAndrew Thompson 	for (i = 0; i < N(rfini1); i++) {
103402ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, rfini1[i]);
103502ac6454SAndrew Thompson 		if (error != 0)
103602ac6454SAndrew Thompson 			goto fail;
103702ac6454SAndrew Thompson 	}
103802ac6454SAndrew Thompson 
103902ac6454SAndrew Thompson 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
104002ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, 0x000824);
104102ac6454SAndrew Thompson 	else
104202ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, 0x0005a4);
104302ac6454SAndrew Thompson 	if (error != 0)
104402ac6454SAndrew Thompson 		goto fail;
104502ac6454SAndrew Thompson 
104602ac6454SAndrew Thompson 	for (i = 0; i < N(rfini2); i++) {
104702ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, rfini2[i]);
104802ac6454SAndrew Thompson 		if (error != 0)
104902ac6454SAndrew Thompson 			goto fail;
105002ac6454SAndrew Thompson 	}
105102ac6454SAndrew Thompson 
105202ac6454SAndrew Thompson 	for (i = 0; i < N(phypll); i++)
105302ac6454SAndrew Thompson 		zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
105402ac6454SAndrew Thompson 
105502ac6454SAndrew Thompson 	for (i = 0; i < N(rfini3); i++) {
105602ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, rfini3[i]);
105702ac6454SAndrew Thompson 		if (error != 0)
105802ac6454SAndrew Thompson 			goto fail;
105902ac6454SAndrew Thompson 	}
106002ac6454SAndrew Thompson fail:
106102ac6454SAndrew Thompson 	return (error);
106202ac6454SAndrew Thompson #undef N
106302ac6454SAndrew Thompson }
106402ac6454SAndrew Thompson 
106502ac6454SAndrew Thompson static int
106602ac6454SAndrew Thompson zyd_al2230_fini(struct zyd_rf *rf)
106702ac6454SAndrew Thompson {
106802ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
106902ac6454SAndrew Thompson 	int error, i;
107002ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
107102ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
107202ac6454SAndrew Thompson 
107302ac6454SAndrew Thompson 	for (i = 0; i < N(phy); i++)
107402ac6454SAndrew Thompson 		zyd_write16_m(sc, phy[i].reg, phy[i].val);
107502ac6454SAndrew Thompson 
107602ac6454SAndrew Thompson 	if (sc->sc_newphy != 0)
107702ac6454SAndrew Thompson 		zyd_write16_m(sc, ZYD_CR9, 0xe1);
107802ac6454SAndrew Thompson 
107902ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, 0x6);
108002ac6454SAndrew Thompson fail:
108102ac6454SAndrew Thompson 	return (error);
108202ac6454SAndrew Thompson #undef N
108302ac6454SAndrew Thompson }
108402ac6454SAndrew Thompson 
108502ac6454SAndrew Thompson static int
108602ac6454SAndrew Thompson zyd_al2230_init_b(struct zyd_rf *rf)
108702ac6454SAndrew Thompson {
108802ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
108902ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
109002ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
109102ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
109202ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
109302ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
109402ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
109502ac6454SAndrew Thompson 	static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
109602ac6454SAndrew Thompson 	static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
109702ac6454SAndrew Thompson 	static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
109802ac6454SAndrew Thompson 	static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
109902ac6454SAndrew Thompson 	int i, error;
110002ac6454SAndrew Thompson 
110102ac6454SAndrew Thompson 	for (i = 0; i < N(phy1); i++)
110202ac6454SAndrew Thompson 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
110302ac6454SAndrew Thompson 
110402ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
110502ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
110602ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
110702ac6454SAndrew Thompson 
110802ac6454SAndrew Thompson 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
110902ac6454SAndrew Thompson 		for (i = 0; i < N(phy2230s); i++)
111002ac6454SAndrew Thompson 			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
111102ac6454SAndrew Thompson 	}
111202ac6454SAndrew Thompson 
111302ac6454SAndrew Thompson 	for (i = 0; i < 3; i++) {
111402ac6454SAndrew Thompson 		error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
111502ac6454SAndrew Thompson 		if (error != 0)
111602ac6454SAndrew Thompson 			return (error);
111702ac6454SAndrew Thompson 	}
111802ac6454SAndrew Thompson 
111902ac6454SAndrew Thompson 	for (i = 0; i < N(rfini_part1); i++) {
112002ac6454SAndrew Thompson 		error = zyd_rfwrite_cr(sc, rfini_part1[i]);
112102ac6454SAndrew Thompson 		if (error != 0)
112202ac6454SAndrew Thompson 			return (error);
112302ac6454SAndrew Thompson 	}
112402ac6454SAndrew Thompson 
112502ac6454SAndrew Thompson 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
112602ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, 0x241000);
112702ac6454SAndrew Thompson 	else
112802ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, 0x25a000);
112902ac6454SAndrew Thompson 	if (error != 0)
113002ac6454SAndrew Thompson 		goto fail;
113102ac6454SAndrew Thompson 
113202ac6454SAndrew Thompson 	for (i = 0; i < N(rfini_part2); i++) {
113302ac6454SAndrew Thompson 		error = zyd_rfwrite_cr(sc, rfini_part2[i]);
113402ac6454SAndrew Thompson 		if (error != 0)
113502ac6454SAndrew Thompson 			return (error);
113602ac6454SAndrew Thompson 	}
113702ac6454SAndrew Thompson 
113802ac6454SAndrew Thompson 	for (i = 0; i < N(phy2); i++)
113902ac6454SAndrew Thompson 		zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
114002ac6454SAndrew Thompson 
114102ac6454SAndrew Thompson 	for (i = 0; i < N(rfini_part3); i++) {
114202ac6454SAndrew Thompson 		error = zyd_rfwrite_cr(sc, rfini_part3[i]);
114302ac6454SAndrew Thompson 		if (error != 0)
114402ac6454SAndrew Thompson 			return (error);
114502ac6454SAndrew Thompson 	}
114602ac6454SAndrew Thompson 
114702ac6454SAndrew Thompson 	for (i = 0; i < N(phy3); i++)
114802ac6454SAndrew Thompson 		zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
114902ac6454SAndrew Thompson 
115002ac6454SAndrew Thompson 	error = zyd_al2230_fini(rf);
115102ac6454SAndrew Thompson fail:
115202ac6454SAndrew Thompson 	return (error);
115302ac6454SAndrew Thompson #undef N
115402ac6454SAndrew Thompson }
115502ac6454SAndrew Thompson 
115602ac6454SAndrew Thompson static int
115702ac6454SAndrew Thompson zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
115802ac6454SAndrew Thompson {
115902ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
116002ac6454SAndrew Thompson 	int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
116102ac6454SAndrew Thompson 
116202ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
116302ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
116402ac6454SAndrew Thompson fail:
116502ac6454SAndrew Thompson 	return (error);
116602ac6454SAndrew Thompson }
116702ac6454SAndrew Thompson 
116802ac6454SAndrew Thompson static int
116902ac6454SAndrew Thompson zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
117002ac6454SAndrew Thompson {
117102ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
117202ac6454SAndrew Thompson 	int error, i;
117302ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
117402ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy1[] = {
117502ac6454SAndrew Thompson 		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
117602ac6454SAndrew Thompson 	};
117702ac6454SAndrew Thompson 	static const struct {
117802ac6454SAndrew Thompson 		uint32_t	r1, r2, r3;
117902ac6454SAndrew Thompson 	} rfprog[] = ZYD_AL2230_CHANTABLE;
118002ac6454SAndrew Thompson 
118102ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
118202ac6454SAndrew Thompson 	if (error != 0)
118302ac6454SAndrew Thompson 		goto fail;
118402ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
118502ac6454SAndrew Thompson 	if (error != 0)
118602ac6454SAndrew Thompson 		goto fail;
118702ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
118802ac6454SAndrew Thompson 	if (error != 0)
118902ac6454SAndrew Thompson 		goto fail;
119002ac6454SAndrew Thompson 
119102ac6454SAndrew Thompson 	for (i = 0; i < N(phy1); i++)
119202ac6454SAndrew Thompson 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
119302ac6454SAndrew Thompson fail:
119402ac6454SAndrew Thompson 	return (error);
119502ac6454SAndrew Thompson #undef N
119602ac6454SAndrew Thompson }
119702ac6454SAndrew Thompson 
119802ac6454SAndrew Thompson static int
119902ac6454SAndrew Thompson zyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
120002ac6454SAndrew Thompson {
120102ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
120202ac6454SAndrew Thompson 	int error, i;
120302ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
120402ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
120502ac6454SAndrew Thompson 	static const struct {
120602ac6454SAndrew Thompson 		uint32_t	r1, r2, r3;
120702ac6454SAndrew Thompson 	} rfprog[] = ZYD_AL2230_CHANTABLE_B;
120802ac6454SAndrew Thompson 
120902ac6454SAndrew Thompson 	for (i = 0; i < N(phy1); i++)
121002ac6454SAndrew Thompson 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
121102ac6454SAndrew Thompson 
121202ac6454SAndrew Thompson 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
121302ac6454SAndrew Thompson 	if (error != 0)
121402ac6454SAndrew Thompson 		goto fail;
121502ac6454SAndrew Thompson 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
121602ac6454SAndrew Thompson 	if (error != 0)
121702ac6454SAndrew Thompson 		goto fail;
121802ac6454SAndrew Thompson 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
121902ac6454SAndrew Thompson 	if (error != 0)
122002ac6454SAndrew Thompson 		goto fail;
122102ac6454SAndrew Thompson 	error = zyd_al2230_fini(rf);
122202ac6454SAndrew Thompson fail:
122302ac6454SAndrew Thompson 	return (error);
122402ac6454SAndrew Thompson #undef N
122502ac6454SAndrew Thompson }
122602ac6454SAndrew Thompson 
122702ac6454SAndrew Thompson #define	ZYD_AL2230_PHY_BANDEDGE6					\
122802ac6454SAndrew Thompson {									\
122902ac6454SAndrew Thompson 	{ ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 },	\
123002ac6454SAndrew Thompson 	{ ZYD_CR47,  0x1e }						\
123102ac6454SAndrew Thompson }
123202ac6454SAndrew Thompson 
123302ac6454SAndrew Thompson static int
123402ac6454SAndrew Thompson zyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
123502ac6454SAndrew Thompson {
123602ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
123702ac6454SAndrew Thompson 	int error = 0, i;
123802ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
123902ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
124002ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
124102ac6454SAndrew Thompson 	struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
124202ac6454SAndrew Thompson 	int chan = ieee80211_chan2ieee(ic, c);
124302ac6454SAndrew Thompson 
124402ac6454SAndrew Thompson 	if (chan == 1 || chan == 11)
124502ac6454SAndrew Thompson 		r[0].val = 0x12;
124602ac6454SAndrew Thompson 
124702ac6454SAndrew Thompson 	for (i = 0; i < N(r); i++)
124802ac6454SAndrew Thompson 		zyd_write16_m(sc, r[i].reg, r[i].val);
124902ac6454SAndrew Thompson fail:
125002ac6454SAndrew Thompson 	return (error);
125102ac6454SAndrew Thompson #undef N
125202ac6454SAndrew Thompson }
125302ac6454SAndrew Thompson 
125402ac6454SAndrew Thompson /*
125502ac6454SAndrew Thompson  * AL7230B RF methods.
125602ac6454SAndrew Thompson  */
125702ac6454SAndrew Thompson static int
125802ac6454SAndrew Thompson zyd_al7230B_init(struct zyd_rf *rf)
125902ac6454SAndrew Thompson {
126002ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
126102ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
126202ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
126302ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
126402ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
126502ac6454SAndrew Thompson 	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
126602ac6454SAndrew Thompson 	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
126702ac6454SAndrew Thompson 	int i, error;
126802ac6454SAndrew Thompson 
126902ac6454SAndrew Thompson 	/* for AL7230B, PHY and RF need to be initialized in "phases" */
127002ac6454SAndrew Thompson 
127102ac6454SAndrew Thompson 	/* init RF-dependent PHY registers, part one */
127202ac6454SAndrew Thompson 	for (i = 0; i < N(phyini_1); i++)
127302ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
127402ac6454SAndrew Thompson 
127502ac6454SAndrew Thompson 	/* init AL7230B radio, part one */
127602ac6454SAndrew Thompson 	for (i = 0; i < N(rfini_1); i++) {
127702ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
127802ac6454SAndrew Thompson 			return (error);
127902ac6454SAndrew Thompson 	}
128002ac6454SAndrew Thompson 	/* init RF-dependent PHY registers, part two */
128102ac6454SAndrew Thompson 	for (i = 0; i < N(phyini_2); i++)
128202ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
128302ac6454SAndrew Thompson 
128402ac6454SAndrew Thompson 	/* init AL7230B radio, part two */
128502ac6454SAndrew Thompson 	for (i = 0; i < N(rfini_2); i++) {
128602ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
128702ac6454SAndrew Thompson 			return (error);
128802ac6454SAndrew Thompson 	}
128902ac6454SAndrew Thompson 	/* init RF-dependent PHY registers, part three */
129002ac6454SAndrew Thompson 	for (i = 0; i < N(phyini_3); i++)
129102ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
129202ac6454SAndrew Thompson fail:
129302ac6454SAndrew Thompson 	return (error);
129402ac6454SAndrew Thompson #undef N
129502ac6454SAndrew Thompson }
129602ac6454SAndrew Thompson 
129702ac6454SAndrew Thompson static int
129802ac6454SAndrew Thompson zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
129902ac6454SAndrew Thompson {
130002ac6454SAndrew Thompson 	int error;
130102ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
130202ac6454SAndrew Thompson 
130302ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
130402ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
130502ac6454SAndrew Thompson fail:
130602ac6454SAndrew Thompson 	return (error);
130702ac6454SAndrew Thompson }
130802ac6454SAndrew Thompson 
130902ac6454SAndrew Thompson static int
131002ac6454SAndrew Thompson zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
131102ac6454SAndrew Thompson {
131202ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
131302ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
131402ac6454SAndrew Thompson 	static const struct {
131502ac6454SAndrew Thompson 		uint32_t	r1, r2;
131602ac6454SAndrew Thompson 	} rfprog[] = ZYD_AL7230B_CHANTABLE;
131702ac6454SAndrew Thompson 	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
131802ac6454SAndrew Thompson 	int i, error;
131902ac6454SAndrew Thompson 
132002ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR240, 0x57);
132102ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR251, 0x2f);
132202ac6454SAndrew Thompson 
132302ac6454SAndrew Thompson 	for (i = 0; i < N(rfsc); i++) {
132402ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
132502ac6454SAndrew Thompson 			return (error);
132602ac6454SAndrew Thompson 	}
132702ac6454SAndrew Thompson 
132802ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR128, 0x14);
132902ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR129, 0x12);
133002ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR130, 0x10);
133102ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR38,  0x38);
133202ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR136, 0xdf);
133302ac6454SAndrew Thompson 
133402ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
133502ac6454SAndrew Thompson 	if (error != 0)
133602ac6454SAndrew Thompson 		goto fail;
133702ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
133802ac6454SAndrew Thompson 	if (error != 0)
133902ac6454SAndrew Thompson 		goto fail;
134002ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, 0x3c9000);
134102ac6454SAndrew Thompson 	if (error != 0)
134202ac6454SAndrew Thompson 		goto fail;
134302ac6454SAndrew Thompson 
134402ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR251, 0x3f);
134502ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, 0x06);
134602ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR240, 0x08);
134702ac6454SAndrew Thompson fail:
134802ac6454SAndrew Thompson 	return (error);
134902ac6454SAndrew Thompson #undef N
135002ac6454SAndrew Thompson }
135102ac6454SAndrew Thompson 
135202ac6454SAndrew Thompson /*
135302ac6454SAndrew Thompson  * AL2210 RF methods.
135402ac6454SAndrew Thompson  */
135502ac6454SAndrew Thompson static int
135602ac6454SAndrew Thompson zyd_al2210_init(struct zyd_rf *rf)
135702ac6454SAndrew Thompson {
135802ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
135902ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
136002ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
136102ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_AL2210_RF;
136202ac6454SAndrew Thompson 	uint32_t tmp;
136302ac6454SAndrew Thompson 	int i, error;
136402ac6454SAndrew Thompson 
136502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR18, 2);
136602ac6454SAndrew Thompson 
136702ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
136802ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
136902ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
137002ac6454SAndrew Thompson 
137102ac6454SAndrew Thompson 	/* init AL2210 radio */
137202ac6454SAndrew Thompson 	for (i = 0; i < N(rfini); i++) {
137302ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
137402ac6454SAndrew Thompson 			return (error);
137502ac6454SAndrew Thompson 	}
137602ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
137702ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
137802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
137902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
138002ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
138102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
138202ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
138302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR18, 3);
138402ac6454SAndrew Thompson fail:
138502ac6454SAndrew Thompson 	return (error);
138602ac6454SAndrew Thompson #undef N
138702ac6454SAndrew Thompson }
138802ac6454SAndrew Thompson 
138902ac6454SAndrew Thompson static int
139002ac6454SAndrew Thompson zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
139102ac6454SAndrew Thompson {
139202ac6454SAndrew Thompson 	/* vendor driver does nothing for this RF chip */
139302ac6454SAndrew Thompson 
139402ac6454SAndrew Thompson 	return (0);
139502ac6454SAndrew Thompson }
139602ac6454SAndrew Thompson 
139702ac6454SAndrew Thompson static int
139802ac6454SAndrew Thompson zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
139902ac6454SAndrew Thompson {
140002ac6454SAndrew Thompson 	int error;
140102ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
140202ac6454SAndrew Thompson 	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
140302ac6454SAndrew Thompson 	uint32_t tmp;
140402ac6454SAndrew Thompson 
140502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR18, 2);
140602ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
140702ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
140802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
140902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
141002ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
141102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
141202ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
141302ac6454SAndrew Thompson 
141402ac6454SAndrew Thompson 	/* actually set the channel */
141502ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1]);
141602ac6454SAndrew Thompson 	if (error != 0)
141702ac6454SAndrew Thompson 		goto fail;
141802ac6454SAndrew Thompson 
141902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR18, 3);
142002ac6454SAndrew Thompson fail:
142102ac6454SAndrew Thompson 	return (error);
142202ac6454SAndrew Thompson }
142302ac6454SAndrew Thompson 
142402ac6454SAndrew Thompson /*
142502ac6454SAndrew Thompson  * GCT RF methods.
142602ac6454SAndrew Thompson  */
142702ac6454SAndrew Thompson static int
142802ac6454SAndrew Thompson zyd_gct_init(struct zyd_rf *rf)
142902ac6454SAndrew Thompson {
143078cc4bbbSWeongyo Jeong #define	ZYD_GCT_INTR_REG	0x85c1
143102ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
143202ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
143302ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
143402ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_GCT_RF;
143578cc4bbbSWeongyo Jeong 	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
143678cc4bbbSWeongyo Jeong 	int i, idx = -1, error;
143778cc4bbbSWeongyo Jeong 	uint16_t data;
143802ac6454SAndrew Thompson 
143902ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
144002ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
144102ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
144202ac6454SAndrew Thompson 
144302ac6454SAndrew Thompson 	/* init cgt radio */
144402ac6454SAndrew Thompson 	for (i = 0; i < N(rfini); i++) {
144502ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
144602ac6454SAndrew Thompson 			return (error);
144702ac6454SAndrew Thompson 	}
144878cc4bbbSWeongyo Jeong 
144978cc4bbbSWeongyo Jeong 	error = zyd_gct_mode(rf);
145078cc4bbbSWeongyo Jeong 	if (error != 0)
145178cc4bbbSWeongyo Jeong 		return (error);
145278cc4bbbSWeongyo Jeong 
145378cc4bbbSWeongyo Jeong 	for (i = 0; i < N(vco) - 1; i++) {
145478cc4bbbSWeongyo Jeong 		error = zyd_gct_set_channel_synth(rf, 1, 0);
145578cc4bbbSWeongyo Jeong 		if (error != 0)
145678cc4bbbSWeongyo Jeong 			goto fail;
145778cc4bbbSWeongyo Jeong 		error = zyd_gct_write(rf, vco[i][0]);
145878cc4bbbSWeongyo Jeong 		if (error != 0)
145978cc4bbbSWeongyo Jeong 			goto fail;
146078cc4bbbSWeongyo Jeong 		zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf);
146178cc4bbbSWeongyo Jeong 		zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data);
146278cc4bbbSWeongyo Jeong 		if ((data & 0xf) == 0) {
146378cc4bbbSWeongyo Jeong 			idx = i;
146478cc4bbbSWeongyo Jeong 			break;
146578cc4bbbSWeongyo Jeong 		}
146678cc4bbbSWeongyo Jeong 	}
146778cc4bbbSWeongyo Jeong 	if (idx == -1) {
146878cc4bbbSWeongyo Jeong 		error = zyd_gct_set_channel_synth(rf, 1, 1);
146978cc4bbbSWeongyo Jeong 		if (error != 0)
147078cc4bbbSWeongyo Jeong 			goto fail;
147178cc4bbbSWeongyo Jeong 		error = zyd_gct_write(rf, 0x6662);
147278cc4bbbSWeongyo Jeong 		if (error != 0)
147378cc4bbbSWeongyo Jeong 			goto fail;
147478cc4bbbSWeongyo Jeong 	}
147578cc4bbbSWeongyo Jeong 
147678cc4bbbSWeongyo Jeong 	rf->idx = idx;
147778cc4bbbSWeongyo Jeong 	zyd_write16_m(sc, ZYD_CR203, 0x6);
147802ac6454SAndrew Thompson fail:
147902ac6454SAndrew Thompson 	return (error);
148002ac6454SAndrew Thompson #undef N
148178cc4bbbSWeongyo Jeong #undef ZYD_GCT_INTR_REG
148278cc4bbbSWeongyo Jeong }
148378cc4bbbSWeongyo Jeong 
148478cc4bbbSWeongyo Jeong static int
148578cc4bbbSWeongyo Jeong zyd_gct_mode(struct zyd_rf *rf)
148678cc4bbbSWeongyo Jeong {
148778cc4bbbSWeongyo Jeong #define N(a)	(sizeof(a) / sizeof((a)[0]))
148878cc4bbbSWeongyo Jeong 	struct zyd_softc *sc = rf->rf_sc;
148978cc4bbbSWeongyo Jeong 	static const uint32_t mode[] = {
149078cc4bbbSWeongyo Jeong 		0x25f98, 0x25f9a, 0x25f94, 0x27fd4
149178cc4bbbSWeongyo Jeong 	};
149278cc4bbbSWeongyo Jeong 	int i, error;
149378cc4bbbSWeongyo Jeong 
149478cc4bbbSWeongyo Jeong 	for (i = 0; i < N(mode); i++) {
149578cc4bbbSWeongyo Jeong 		if ((error = zyd_rfwrite(sc, mode[i])) != 0)
149678cc4bbbSWeongyo Jeong 			break;
149778cc4bbbSWeongyo Jeong 	}
149878cc4bbbSWeongyo Jeong 	return (error);
149978cc4bbbSWeongyo Jeong #undef N
150078cc4bbbSWeongyo Jeong }
150178cc4bbbSWeongyo Jeong 
150278cc4bbbSWeongyo Jeong static int
150378cc4bbbSWeongyo Jeong zyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal)
150478cc4bbbSWeongyo Jeong {
150578cc4bbbSWeongyo Jeong 	int error, idx = chan - 1;
150678cc4bbbSWeongyo Jeong 	struct zyd_softc *sc = rf->rf_sc;
150778cc4bbbSWeongyo Jeong 	static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL;
150878cc4bbbSWeongyo Jeong 	static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD;
150978cc4bbbSWeongyo Jeong 	static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV;
151078cc4bbbSWeongyo Jeong 
151178cc4bbbSWeongyo Jeong 	error = zyd_rfwrite(sc,
151278cc4bbbSWeongyo Jeong 	    (acal == 1) ? acal_synth[idx] : std_synth[idx]);
151378cc4bbbSWeongyo Jeong 	if (error != 0)
151478cc4bbbSWeongyo Jeong 		return (error);
151578cc4bbbSWeongyo Jeong 	return zyd_rfwrite(sc, div_synth[idx]);
151678cc4bbbSWeongyo Jeong }
151778cc4bbbSWeongyo Jeong 
151878cc4bbbSWeongyo Jeong static int
151978cc4bbbSWeongyo Jeong zyd_gct_write(struct zyd_rf *rf, uint16_t value)
152078cc4bbbSWeongyo Jeong {
152178cc4bbbSWeongyo Jeong 	struct zyd_softc *sc = rf->rf_sc;
152278cc4bbbSWeongyo Jeong 
152378cc4bbbSWeongyo Jeong 	return zyd_rfwrite(sc, 0x300000 | 0x40000 | value);
152402ac6454SAndrew Thompson }
152502ac6454SAndrew Thompson 
152602ac6454SAndrew Thompson static int
152702ac6454SAndrew Thompson zyd_gct_switch_radio(struct zyd_rf *rf, int on)
152802ac6454SAndrew Thompson {
152978cc4bbbSWeongyo Jeong #define N(a)	(sizeof(a) / sizeof((a)[0]))
153078cc4bbbSWeongyo Jeong 	int error;
153178cc4bbbSWeongyo Jeong 	struct zyd_softc *sc = rf->rf_sc;
153202ac6454SAndrew Thompson 
153378cc4bbbSWeongyo Jeong 	error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90);
153478cc4bbbSWeongyo Jeong 	if (error != 0)
153578cc4bbbSWeongyo Jeong 		return (error);
153678cc4bbbSWeongyo Jeong 
153778cc4bbbSWeongyo Jeong 	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
153878cc4bbbSWeongyo Jeong 	zyd_write16_m(sc, ZYD_CR251,
153978cc4bbbSWeongyo Jeong 	    on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f);
154078cc4bbbSWeongyo Jeong fail:
154178cc4bbbSWeongyo Jeong 	return (error);
154202ac6454SAndrew Thompson }
154302ac6454SAndrew Thompson 
154402ac6454SAndrew Thompson static int
154502ac6454SAndrew Thompson zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
154602ac6454SAndrew Thompson {
154702ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
154878cc4bbbSWeongyo Jeong 	int error, i;
154902ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
155078cc4bbbSWeongyo Jeong 	static const struct zyd_phy_pair cmd[] = {
155178cc4bbbSWeongyo Jeong 		{ ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 },
155278cc4bbbSWeongyo Jeong 		{ ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 },
155378cc4bbbSWeongyo Jeong 	};
155478cc4bbbSWeongyo Jeong 	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
155502ac6454SAndrew Thompson 
155678cc4bbbSWeongyo Jeong 	error = zyd_gct_set_channel_synth(rf, chan, 0);
155778cc4bbbSWeongyo Jeong 	if (error != 0)
155878cc4bbbSWeongyo Jeong 		goto fail;
155978cc4bbbSWeongyo Jeong 	error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 :
156078cc4bbbSWeongyo Jeong 	    vco[rf->idx][((chan - 1) / 2)]);
156178cc4bbbSWeongyo Jeong 	if (error != 0)
156278cc4bbbSWeongyo Jeong 		goto fail;
156378cc4bbbSWeongyo Jeong 	error = zyd_gct_mode(rf);
156478cc4bbbSWeongyo Jeong 	if (error != 0)
156502ac6454SAndrew Thompson 		return (error);
156678cc4bbbSWeongyo Jeong 	for (i = 0; i < N(cmd); i++)
156778cc4bbbSWeongyo Jeong 		zyd_write16_m(sc, cmd[i].reg, cmd[i].val);
156878cc4bbbSWeongyo Jeong 	error = zyd_gct_txgain(rf, chan);
156978cc4bbbSWeongyo Jeong 	if (error != 0)
157078cc4bbbSWeongyo Jeong 		return (error);
157178cc4bbbSWeongyo Jeong 	zyd_write16_m(sc, ZYD_CR203, 0x6);
157202ac6454SAndrew Thompson fail:
157302ac6454SAndrew Thompson 	return (error);
157402ac6454SAndrew Thompson #undef N
157502ac6454SAndrew Thompson }
157602ac6454SAndrew Thompson 
157702ac6454SAndrew Thompson static int
157878cc4bbbSWeongyo Jeong zyd_gct_txgain(struct zyd_rf *rf, uint8_t chan)
157902ac6454SAndrew Thompson {
158002ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
158102ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
158278cc4bbbSWeongyo Jeong 	static uint32_t txgain[] = ZYD_GCT_TXGAIN;
158378cc4bbbSWeongyo Jeong 	uint8_t idx = sc->sc_pwrint[chan - 1];
158402ac6454SAndrew Thompson 
158578cc4bbbSWeongyo Jeong 	if (idx >= N(txgain)) {
158678cc4bbbSWeongyo Jeong 		device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n",
158778cc4bbbSWeongyo Jeong 		    chan, idx);
158878cc4bbbSWeongyo Jeong 		return 0;
158902ac6454SAndrew Thompson 	}
159078cc4bbbSWeongyo Jeong 
159178cc4bbbSWeongyo Jeong 	return zyd_rfwrite(sc, 0x700000 | txgain[idx]);
159202ac6454SAndrew Thompson #undef N
159302ac6454SAndrew Thompson }
159402ac6454SAndrew Thompson 
159502ac6454SAndrew Thompson /*
159602ac6454SAndrew Thompson  * Maxim2 RF methods.
159702ac6454SAndrew Thompson  */
159802ac6454SAndrew Thompson static int
159902ac6454SAndrew Thompson zyd_maxim2_init(struct zyd_rf *rf)
160002ac6454SAndrew Thompson {
160102ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
160202ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
160302ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
160402ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
160502ac6454SAndrew Thompson 	uint16_t tmp;
160602ac6454SAndrew Thompson 	int i, error;
160702ac6454SAndrew Thompson 
160802ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
160902ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
161002ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
161102ac6454SAndrew Thompson 
161202ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
161302ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
161402ac6454SAndrew Thompson 
161502ac6454SAndrew Thompson 	/* init maxim2 radio */
161602ac6454SAndrew Thompson 	for (i = 0; i < N(rfini); i++) {
161702ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
161802ac6454SAndrew Thompson 			return (error);
161902ac6454SAndrew Thompson 	}
162002ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
162102ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
162202ac6454SAndrew Thompson fail:
162302ac6454SAndrew Thompson 	return (error);
162402ac6454SAndrew Thompson #undef N
162502ac6454SAndrew Thompson }
162602ac6454SAndrew Thompson 
162702ac6454SAndrew Thompson static int
162802ac6454SAndrew Thompson zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
162902ac6454SAndrew Thompson {
163002ac6454SAndrew Thompson 
163102ac6454SAndrew Thompson 	/* vendor driver does nothing for this RF chip */
163202ac6454SAndrew Thompson 	return (0);
163302ac6454SAndrew Thompson }
163402ac6454SAndrew Thompson 
163502ac6454SAndrew Thompson static int
163602ac6454SAndrew Thompson zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
163702ac6454SAndrew Thompson {
163802ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
163902ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
164002ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
164102ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
164202ac6454SAndrew Thompson 	static const struct {
164302ac6454SAndrew Thompson 		uint32_t	r1, r2;
164402ac6454SAndrew Thompson 	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
164502ac6454SAndrew Thompson 	uint16_t tmp;
164602ac6454SAndrew Thompson 	int i, error;
164702ac6454SAndrew Thompson 
164802ac6454SAndrew Thompson 	/*
164902ac6454SAndrew Thompson 	 * Do the same as we do when initializing it, except for the channel
165002ac6454SAndrew Thompson 	 * values coming from the two channel tables.
165102ac6454SAndrew Thompson 	 */
165202ac6454SAndrew Thompson 
165302ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
165402ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
165502ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
165602ac6454SAndrew Thompson 
165702ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
165802ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
165902ac6454SAndrew Thompson 
166002ac6454SAndrew Thompson 	/* first two values taken from the chantables */
166102ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
166202ac6454SAndrew Thompson 	if (error != 0)
166302ac6454SAndrew Thompson 		goto fail;
166402ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
166502ac6454SAndrew Thompson 	if (error != 0)
166602ac6454SAndrew Thompson 		goto fail;
166702ac6454SAndrew Thompson 
166802ac6454SAndrew Thompson 	/* init maxim2 radio - skipping the two first values */
166902ac6454SAndrew Thompson 	for (i = 2; i < N(rfini); i++) {
167002ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
167102ac6454SAndrew Thompson 			return (error);
167202ac6454SAndrew Thompson 	}
167302ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
167402ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
167502ac6454SAndrew Thompson fail:
167602ac6454SAndrew Thompson 	return (error);
167702ac6454SAndrew Thompson #undef N
167802ac6454SAndrew Thompson }
167902ac6454SAndrew Thompson 
168002ac6454SAndrew Thompson static int
168102ac6454SAndrew Thompson zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
168202ac6454SAndrew Thompson {
168302ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
168402ac6454SAndrew Thompson 
168502ac6454SAndrew Thompson 	rf->rf_sc = sc;
168678cc4bbbSWeongyo Jeong 	rf->update_pwr = 1;
168702ac6454SAndrew Thompson 
168802ac6454SAndrew Thompson 	switch (type) {
168902ac6454SAndrew Thompson 	case ZYD_RF_RFMD:
169002ac6454SAndrew Thompson 		rf->init         = zyd_rfmd_init;
169102ac6454SAndrew Thompson 		rf->switch_radio = zyd_rfmd_switch_radio;
169202ac6454SAndrew Thompson 		rf->set_channel  = zyd_rfmd_set_channel;
169302ac6454SAndrew Thompson 		rf->width        = 24;	/* 24-bit RF values */
169402ac6454SAndrew Thompson 		break;
169502ac6454SAndrew Thompson 	case ZYD_RF_AL2230:
169602ac6454SAndrew Thompson 	case ZYD_RF_AL2230S:
169702ac6454SAndrew Thompson 		if (sc->sc_macrev == ZYD_ZD1211B) {
169802ac6454SAndrew Thompson 			rf->init = zyd_al2230_init_b;
169902ac6454SAndrew Thompson 			rf->set_channel = zyd_al2230_set_channel_b;
170002ac6454SAndrew Thompson 		} else {
170102ac6454SAndrew Thompson 			rf->init = zyd_al2230_init;
170202ac6454SAndrew Thompson 			rf->set_channel = zyd_al2230_set_channel;
170302ac6454SAndrew Thompson 		}
170402ac6454SAndrew Thompson 		rf->switch_radio = zyd_al2230_switch_radio;
170502ac6454SAndrew Thompson 		rf->bandedge6	 = zyd_al2230_bandedge6;
170602ac6454SAndrew Thompson 		rf->width        = 24;	/* 24-bit RF values */
170702ac6454SAndrew Thompson 		break;
170802ac6454SAndrew Thompson 	case ZYD_RF_AL7230B:
170902ac6454SAndrew Thompson 		rf->init         = zyd_al7230B_init;
171002ac6454SAndrew Thompson 		rf->switch_radio = zyd_al7230B_switch_radio;
171102ac6454SAndrew Thompson 		rf->set_channel  = zyd_al7230B_set_channel;
171202ac6454SAndrew Thompson 		rf->width        = 24;	/* 24-bit RF values */
171302ac6454SAndrew Thompson 		break;
171402ac6454SAndrew Thompson 	case ZYD_RF_AL2210:
171502ac6454SAndrew Thompson 		rf->init         = zyd_al2210_init;
171602ac6454SAndrew Thompson 		rf->switch_radio = zyd_al2210_switch_radio;
171702ac6454SAndrew Thompson 		rf->set_channel  = zyd_al2210_set_channel;
171802ac6454SAndrew Thompson 		rf->width        = 24;	/* 24-bit RF values */
171902ac6454SAndrew Thompson 		break;
172078cc4bbbSWeongyo Jeong 	case ZYD_RF_MAXIM_NEW:
172102ac6454SAndrew Thompson 	case ZYD_RF_GCT:
172202ac6454SAndrew Thompson 		rf->init         = zyd_gct_init;
172302ac6454SAndrew Thompson 		rf->switch_radio = zyd_gct_switch_radio;
172402ac6454SAndrew Thompson 		rf->set_channel  = zyd_gct_set_channel;
172578cc4bbbSWeongyo Jeong 		rf->width        = 24;	/* 24-bit RF values */
172678cc4bbbSWeongyo Jeong 		rf->update_pwr   = 0;
172702ac6454SAndrew Thompson 		break;
172802ac6454SAndrew Thompson 	case ZYD_RF_MAXIM_NEW2:
172902ac6454SAndrew Thompson 		rf->init         = zyd_maxim2_init;
173002ac6454SAndrew Thompson 		rf->switch_radio = zyd_maxim2_switch_radio;
173102ac6454SAndrew Thompson 		rf->set_channel  = zyd_maxim2_set_channel;
173202ac6454SAndrew Thompson 		rf->width        = 18;	/* 18-bit RF values */
173302ac6454SAndrew Thompson 		break;
173402ac6454SAndrew Thompson 	default:
173502ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
173602ac6454SAndrew Thompson 		    "sorry, radio \"%s\" is not supported yet\n",
173702ac6454SAndrew Thompson 		    zyd_rf_name(type));
173802ac6454SAndrew Thompson 		return (EINVAL);
173902ac6454SAndrew Thompson 	}
174002ac6454SAndrew Thompson 	return (0);
174102ac6454SAndrew Thompson }
174202ac6454SAndrew Thompson 
174302ac6454SAndrew Thompson static const char *
174402ac6454SAndrew Thompson zyd_rf_name(uint8_t type)
174502ac6454SAndrew Thompson {
174602ac6454SAndrew Thompson 	static const char * const zyd_rfs[] = {
174702ac6454SAndrew Thompson 		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
174802ac6454SAndrew Thompson 		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
174902ac6454SAndrew Thompson 		"AL2230S",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
175002ac6454SAndrew Thompson 		"PHILIPS"
175102ac6454SAndrew Thompson 	};
175202ac6454SAndrew Thompson 
175302ac6454SAndrew Thompson 	return zyd_rfs[(type > 15) ? 0 : type];
175402ac6454SAndrew Thompson }
175502ac6454SAndrew Thompson 
175602ac6454SAndrew Thompson static int
175702ac6454SAndrew Thompson zyd_hw_init(struct zyd_softc *sc)
175802ac6454SAndrew Thompson {
175902ac6454SAndrew Thompson 	int error;
176002ac6454SAndrew Thompson 	const struct zyd_phy_pair *phyp;
176102ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
176202ac6454SAndrew Thompson 	uint16_t val;
176302ac6454SAndrew Thompson 
176402ac6454SAndrew Thompson 	/* specify that the plug and play is finished */
176502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
176602ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
176702ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
176802ac6454SAndrew Thompson 	    sc->sc_fwbase);
176902ac6454SAndrew Thompson 
177002ac6454SAndrew Thompson 	/* retrieve firmware revision number */
177102ac6454SAndrew Thompson 	zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
177202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
177302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
177402ac6454SAndrew Thompson 	/* set mandatory rates - XXX assumes 802.11b/g */
177502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
177602ac6454SAndrew Thompson 
177702ac6454SAndrew Thompson 	/* disable interrupts */
177802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
177902ac6454SAndrew Thompson 
178002ac6454SAndrew Thompson 	if ((error = zyd_read_pod(sc)) != 0) {
178102ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM\n");
178202ac6454SAndrew Thompson 		goto fail;
178302ac6454SAndrew Thompson 	}
178402ac6454SAndrew Thompson 
178502ac6454SAndrew Thompson 	/* PHY init (resetting) */
178602ac6454SAndrew Thompson 	error = zyd_lock_phy(sc);
178702ac6454SAndrew Thompson 	if (error != 0)
178802ac6454SAndrew Thompson 		goto fail;
178902ac6454SAndrew Thompson 	phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
179002ac6454SAndrew Thompson 	for (; phyp->reg != 0; phyp++)
179102ac6454SAndrew Thompson 		zyd_write16_m(sc, phyp->reg, phyp->val);
179202ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
179302ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
179402ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_CR157, val >> 8);
179502ac6454SAndrew Thompson 	}
179602ac6454SAndrew Thompson 	error = zyd_unlock_phy(sc);
179702ac6454SAndrew Thompson 	if (error != 0)
179802ac6454SAndrew Thompson 		goto fail;
179902ac6454SAndrew Thompson 
180002ac6454SAndrew Thompson 	/* HMAC init */
180102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
180202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
180302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
180402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
180502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
180602ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
180702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
180802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
180902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
181002ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
181102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
181202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
181302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
181402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
181502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
181602ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
181702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
181802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
181902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
182002ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
182102ac6454SAndrew Thompson 
182202ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211) {
182302ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
182402ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
182502ac6454SAndrew Thompson 	} else {
182602ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
182702ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
182802ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
182902ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
183002ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
183102ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
183202ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
183302ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
183402ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
183502ac6454SAndrew Thompson 	}
183602ac6454SAndrew Thompson 
183702ac6454SAndrew Thompson 	/* init beacon interval to 100ms */
183802ac6454SAndrew Thompson 	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
183902ac6454SAndrew Thompson 		goto fail;
184002ac6454SAndrew Thompson 
184102ac6454SAndrew Thompson 	if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
184202ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
184302ac6454SAndrew Thompson 		    sc->sc_rfrev);
184402ac6454SAndrew Thompson 		goto fail;
184502ac6454SAndrew Thompson 	}
184602ac6454SAndrew Thompson 
184702ac6454SAndrew Thompson 	/* RF chip init */
184802ac6454SAndrew Thompson 	error = zyd_lock_phy(sc);
184902ac6454SAndrew Thompson 	if (error != 0)
185002ac6454SAndrew Thompson 		goto fail;
185102ac6454SAndrew Thompson 	error = (*rf->init)(rf);
185202ac6454SAndrew Thompson 	if (error != 0) {
185302ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
185402ac6454SAndrew Thompson 		    "radio initialization failed, error %d\n", error);
185502ac6454SAndrew Thompson 		goto fail;
185602ac6454SAndrew Thompson 	}
185702ac6454SAndrew Thompson 	error = zyd_unlock_phy(sc);
185802ac6454SAndrew Thompson 	if (error != 0)
185902ac6454SAndrew Thompson 		goto fail;
186002ac6454SAndrew Thompson 
186102ac6454SAndrew Thompson 	if ((error = zyd_read_eeprom(sc)) != 0) {
186202ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM\n");
186302ac6454SAndrew Thompson 		goto fail;
186402ac6454SAndrew Thompson 	}
186502ac6454SAndrew Thompson 
186602ac6454SAndrew Thompson fail:	return (error);
186702ac6454SAndrew Thompson }
186802ac6454SAndrew Thompson 
186902ac6454SAndrew Thompson static int
187002ac6454SAndrew Thompson zyd_read_pod(struct zyd_softc *sc)
187102ac6454SAndrew Thompson {
187202ac6454SAndrew Thompson 	int error;
187302ac6454SAndrew Thompson 	uint32_t tmp;
187402ac6454SAndrew Thompson 
187502ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
187602ac6454SAndrew Thompson 	sc->sc_rfrev     = tmp & 0x0f;
187702ac6454SAndrew Thompson 	sc->sc_ledtype   = (tmp >>  4) & 0x01;
187802ac6454SAndrew Thompson 	sc->sc_al2230s   = (tmp >>  7) & 0x01;
187902ac6454SAndrew Thompson 	sc->sc_cckgain   = (tmp >>  8) & 0x01;
188002ac6454SAndrew Thompson 	sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
188102ac6454SAndrew Thompson 	sc->sc_parev     = (tmp >> 16) & 0x0f;
188202ac6454SAndrew Thompson 	sc->sc_bandedge6 = (tmp >> 21) & 0x01;
188302ac6454SAndrew Thompson 	sc->sc_newphy    = (tmp >> 31) & 0x01;
188402ac6454SAndrew Thompson 	sc->sc_txled     = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
188502ac6454SAndrew Thompson fail:
188602ac6454SAndrew Thompson 	return (error);
188702ac6454SAndrew Thompson }
188802ac6454SAndrew Thompson 
188902ac6454SAndrew Thompson static int
189002ac6454SAndrew Thompson zyd_read_eeprom(struct zyd_softc *sc)
189102ac6454SAndrew Thompson {
189202ac6454SAndrew Thompson 	uint16_t val;
189302ac6454SAndrew Thompson 	int error, i;
189402ac6454SAndrew Thompson 
189502ac6454SAndrew Thompson 	/* read Tx power calibration tables */
189602ac6454SAndrew Thompson 	for (i = 0; i < 7; i++) {
189702ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
189802ac6454SAndrew Thompson 		sc->sc_pwrcal[i * 2] = val >> 8;
189902ac6454SAndrew Thompson 		sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
190002ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
190102ac6454SAndrew Thompson 		sc->sc_pwrint[i * 2] = val >> 8;
190202ac6454SAndrew Thompson 		sc->sc_pwrint[i * 2 + 1] = val & 0xff;
190302ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
190402ac6454SAndrew Thompson 		sc->sc_ofdm36_cal[i * 2] = val >> 8;
190502ac6454SAndrew Thompson 		sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
190602ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
190702ac6454SAndrew Thompson 		sc->sc_ofdm48_cal[i * 2] = val >> 8;
190802ac6454SAndrew Thompson 		sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
190902ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
191002ac6454SAndrew Thompson 		sc->sc_ofdm54_cal[i * 2] = val >> 8;
191102ac6454SAndrew Thompson 		sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
191202ac6454SAndrew Thompson 	}
191302ac6454SAndrew Thompson fail:
191402ac6454SAndrew Thompson 	return (error);
191502ac6454SAndrew Thompson }
191602ac6454SAndrew Thompson 
191702ac6454SAndrew Thompson static int
191802ac6454SAndrew Thompson zyd_get_macaddr(struct zyd_softc *sc)
191902ac6454SAndrew Thompson {
1920760bc48eSAndrew Thompson 	struct usb_device_request req;
1921e0a69b51SAndrew Thompson 	usb_error_t error;
192202ac6454SAndrew Thompson 
192302ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
192402ac6454SAndrew Thompson 	req.bRequest = ZYD_READFWDATAREQ;
192502ac6454SAndrew Thompson 	USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
192602ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
192702ac6454SAndrew Thompson 	USETW(req.wLength, IEEE80211_ADDR_LEN);
192802ac6454SAndrew Thompson 
192902ac6454SAndrew Thompson 	error = zyd_do_request(sc, &req, sc->sc_bssid);
193002ac6454SAndrew Thompson 	if (error != 0) {
193102ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1932a593f6b8SAndrew Thompson 		    usbd_errstr(error));
193302ac6454SAndrew Thompson 	}
193402ac6454SAndrew Thompson 
193502ac6454SAndrew Thompson 	return (error);
193602ac6454SAndrew Thompson }
193702ac6454SAndrew Thompson 
193802ac6454SAndrew Thompson static int
193902ac6454SAndrew Thompson zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
194002ac6454SAndrew Thompson {
194102ac6454SAndrew Thompson 	int error;
194202ac6454SAndrew Thompson 	uint32_t tmp;
194302ac6454SAndrew Thompson 
194402ac6454SAndrew Thompson 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
194502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
194602ac6454SAndrew Thompson 	tmp = addr[5] << 8 | addr[4];
194702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
194802ac6454SAndrew Thompson fail:
194902ac6454SAndrew Thompson 	return (error);
195002ac6454SAndrew Thompson }
195102ac6454SAndrew Thompson 
195202ac6454SAndrew Thompson static int
195302ac6454SAndrew Thompson zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
195402ac6454SAndrew Thompson {
195502ac6454SAndrew Thompson 	int error;
195602ac6454SAndrew Thompson 	uint32_t tmp;
195702ac6454SAndrew Thompson 
195802ac6454SAndrew Thompson 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
195902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
196002ac6454SAndrew Thompson 	tmp = addr[5] << 8 | addr[4];
196102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
196202ac6454SAndrew Thompson fail:
196302ac6454SAndrew Thompson 	return (error);
196402ac6454SAndrew Thompson }
196502ac6454SAndrew Thompson 
196602ac6454SAndrew Thompson static int
196702ac6454SAndrew Thompson zyd_switch_radio(struct zyd_softc *sc, int on)
196802ac6454SAndrew Thompson {
196902ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
197002ac6454SAndrew Thompson 	int error;
197102ac6454SAndrew Thompson 
197202ac6454SAndrew Thompson 	error = zyd_lock_phy(sc);
197302ac6454SAndrew Thompson 	if (error != 0)
197402ac6454SAndrew Thompson 		goto fail;
197502ac6454SAndrew Thompson 	error = (*rf->switch_radio)(rf, on);
197602ac6454SAndrew Thompson 	if (error != 0)
197702ac6454SAndrew Thompson 		goto fail;
197802ac6454SAndrew Thompson 	error = zyd_unlock_phy(sc);
197902ac6454SAndrew Thompson fail:
198002ac6454SAndrew Thompson 	return (error);
198102ac6454SAndrew Thompson }
198202ac6454SAndrew Thompson 
198302ac6454SAndrew Thompson static int
198402ac6454SAndrew Thompson zyd_set_led(struct zyd_softc *sc, int which, int on)
198502ac6454SAndrew Thompson {
198602ac6454SAndrew Thompson 	int error;
198702ac6454SAndrew Thompson 	uint32_t tmp;
198802ac6454SAndrew Thompson 
198902ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
199002ac6454SAndrew Thompson 	tmp &= ~which;
199102ac6454SAndrew Thompson 	if (on)
199202ac6454SAndrew Thompson 		tmp |= which;
199302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
199402ac6454SAndrew Thompson fail:
199502ac6454SAndrew Thompson 	return (error);
199602ac6454SAndrew Thompson }
199702ac6454SAndrew Thompson 
199802ac6454SAndrew Thompson static void
199902ac6454SAndrew Thompson zyd_set_multi(struct zyd_softc *sc)
200002ac6454SAndrew Thompson {
200102ac6454SAndrew Thompson 	int error;
200202ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
200302ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
200402ac6454SAndrew Thompson 	struct ifmultiaddr *ifma;
200502ac6454SAndrew Thompson 	uint32_t low, high;
200602ac6454SAndrew Thompson 	uint8_t v;
200702ac6454SAndrew Thompson 
200802ac6454SAndrew Thompson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
200902ac6454SAndrew Thompson 		return;
201002ac6454SAndrew Thompson 
201102ac6454SAndrew Thompson 	low = 0x00000000;
201202ac6454SAndrew Thompson 	high = 0x80000000;
201302ac6454SAndrew Thompson 
201402ac6454SAndrew Thompson 	if (ic->ic_opmode == IEEE80211_M_MONITOR ||
201502ac6454SAndrew Thompson 	    (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) {
201602ac6454SAndrew Thompson 		low = 0xffffffff;
201702ac6454SAndrew Thompson 		high = 0xffffffff;
201802ac6454SAndrew Thompson 	} else {
2019eb956cd0SRobert Watson 		if_maddr_rlock(ifp);
202002ac6454SAndrew Thompson 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
202102ac6454SAndrew Thompson 			if (ifma->ifma_addr->sa_family != AF_LINK)
202202ac6454SAndrew Thompson 				continue;
202302ac6454SAndrew Thompson 			v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
202402ac6454SAndrew Thompson 			    ifma->ifma_addr))[5] >> 2;
202502ac6454SAndrew Thompson 			if (v < 32)
202602ac6454SAndrew Thompson 				low |= 1 << v;
202702ac6454SAndrew Thompson 			else
202802ac6454SAndrew Thompson 				high |= 1 << (v - 32);
202902ac6454SAndrew Thompson 		}
2030eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
203102ac6454SAndrew Thompson 	}
203202ac6454SAndrew Thompson 
203302ac6454SAndrew Thompson 	/* reprogram multicast global hash table */
203402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_GHTBL, low);
203502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_GHTBH, high);
203602ac6454SAndrew Thompson fail:
203702ac6454SAndrew Thompson 	if (error != 0)
203802ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
203902ac6454SAndrew Thompson 		    "could not set multicast hash table\n");
204002ac6454SAndrew Thompson }
204102ac6454SAndrew Thompson 
204202ac6454SAndrew Thompson static void
204302ac6454SAndrew Thompson zyd_update_mcast(struct ifnet *ifp)
204402ac6454SAndrew Thompson {
204502ac6454SAndrew Thompson 	struct zyd_softc *sc = ifp->if_softc;
204602ac6454SAndrew Thompson 
204702ac6454SAndrew Thompson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
204802ac6454SAndrew Thompson 		return;
204902ac6454SAndrew Thompson 
205002ac6454SAndrew Thompson 	ZYD_LOCK(sc);
20515efea30fSAndrew Thompson 	zyd_set_multi(sc);
205202ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
205302ac6454SAndrew Thompson }
205402ac6454SAndrew Thompson 
205502ac6454SAndrew Thompson static int
205602ac6454SAndrew Thompson zyd_set_rxfilter(struct zyd_softc *sc)
205702ac6454SAndrew Thompson {
205802ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
205902ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
206002ac6454SAndrew Thompson 	uint32_t rxfilter;
206102ac6454SAndrew Thompson 
206202ac6454SAndrew Thompson 	switch (ic->ic_opmode) {
206302ac6454SAndrew Thompson 	case IEEE80211_M_STA:
206402ac6454SAndrew Thompson 		rxfilter = ZYD_FILTER_BSS;
206502ac6454SAndrew Thompson 		break;
206602ac6454SAndrew Thompson 	case IEEE80211_M_IBSS:
206702ac6454SAndrew Thompson 	case IEEE80211_M_HOSTAP:
206802ac6454SAndrew Thompson 		rxfilter = ZYD_FILTER_HOSTAP;
206902ac6454SAndrew Thompson 		break;
207002ac6454SAndrew Thompson 	case IEEE80211_M_MONITOR:
207102ac6454SAndrew Thompson 		rxfilter = ZYD_FILTER_MONITOR;
207202ac6454SAndrew Thompson 		break;
207302ac6454SAndrew Thompson 	default:
207402ac6454SAndrew Thompson 		/* should not get there */
207502ac6454SAndrew Thompson 		return (EINVAL);
207602ac6454SAndrew Thompson 	}
207702ac6454SAndrew Thompson 	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
207802ac6454SAndrew Thompson }
207902ac6454SAndrew Thompson 
208002ac6454SAndrew Thompson static void
208102ac6454SAndrew Thompson zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
208202ac6454SAndrew Thompson {
208302ac6454SAndrew Thompson 	int error;
208402ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
208502ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
208602ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
208702ac6454SAndrew Thompson 	uint32_t tmp;
208802ac6454SAndrew Thompson 	int chan;
208902ac6454SAndrew Thompson 
209002ac6454SAndrew Thompson 	chan = ieee80211_chan2ieee(ic, c);
209102ac6454SAndrew Thompson 	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
209202ac6454SAndrew Thompson 		/* XXX should NEVER happen */
209302ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
209402ac6454SAndrew Thompson 		    "%s: invalid channel %x\n", __func__, chan);
209502ac6454SAndrew Thompson 		return;
209602ac6454SAndrew Thompson 	}
209702ac6454SAndrew Thompson 
209802ac6454SAndrew Thompson 	error = zyd_lock_phy(sc);
209902ac6454SAndrew Thompson 	if (error != 0)
210002ac6454SAndrew Thompson 		goto fail;
210102ac6454SAndrew Thompson 
210202ac6454SAndrew Thompson 	error = (*rf->set_channel)(rf, chan);
210302ac6454SAndrew Thompson 	if (error != 0)
210402ac6454SAndrew Thompson 		goto fail;
210502ac6454SAndrew Thompson 
210678cc4bbbSWeongyo Jeong 	if (rf->update_pwr) {
210702ac6454SAndrew Thompson 		/* update Tx power */
210802ac6454SAndrew Thompson 		zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
210902ac6454SAndrew Thompson 
211002ac6454SAndrew Thompson 		if (sc->sc_macrev == ZYD_ZD1211B) {
211178cc4bbbSWeongyo Jeong 			zyd_write16_m(sc, ZYD_CR67,
211278cc4bbbSWeongyo Jeong 			    sc->sc_ofdm36_cal[chan - 1]);
211378cc4bbbSWeongyo Jeong 			zyd_write16_m(sc, ZYD_CR66,
211478cc4bbbSWeongyo Jeong 			    sc->sc_ofdm48_cal[chan - 1]);
211578cc4bbbSWeongyo Jeong 			zyd_write16_m(sc, ZYD_CR65,
211678cc4bbbSWeongyo Jeong 			    sc->sc_ofdm54_cal[chan - 1]);
211702ac6454SAndrew Thompson 			zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
211802ac6454SAndrew Thompson 			zyd_write16_m(sc, ZYD_CR69, 0x28);
211902ac6454SAndrew Thompson 			zyd_write16_m(sc, ZYD_CR69, 0x2a);
212002ac6454SAndrew Thompson 		}
212178cc4bbbSWeongyo Jeong 	}
212202ac6454SAndrew Thompson 	if (sc->sc_cckgain) {
212302ac6454SAndrew Thompson 		/* set CCK baseband gain from EEPROM */
212402ac6454SAndrew Thompson 		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
212502ac6454SAndrew Thompson 			zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
212602ac6454SAndrew Thompson 	}
212702ac6454SAndrew Thompson 	if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
212802ac6454SAndrew Thompson 		error = (*rf->bandedge6)(rf, c);
212902ac6454SAndrew Thompson 		if (error != 0)
213002ac6454SAndrew Thompson 			goto fail;
213102ac6454SAndrew Thompson 	}
213202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
213302ac6454SAndrew Thompson 
213402ac6454SAndrew Thompson 	error = zyd_unlock_phy(sc);
213502ac6454SAndrew Thompson 	if (error != 0)
213602ac6454SAndrew Thompson 		goto fail;
213702ac6454SAndrew Thompson 
213802ac6454SAndrew Thompson 	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
213902ac6454SAndrew Thompson 	    htole16(c->ic_freq);
214002ac6454SAndrew Thompson 	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
214102ac6454SAndrew Thompson 	    htole16(c->ic_flags);
214202ac6454SAndrew Thompson fail:
214302ac6454SAndrew Thompson 	return;
214402ac6454SAndrew Thompson }
214502ac6454SAndrew Thompson 
214602ac6454SAndrew Thompson static int
214702ac6454SAndrew Thompson zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
214802ac6454SAndrew Thompson {
214902ac6454SAndrew Thompson 	int error;
215002ac6454SAndrew Thompson 	uint32_t val;
215102ac6454SAndrew Thompson 
215202ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
215302ac6454SAndrew Thompson 	sc->sc_atim_wnd = val;
215402ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
215502ac6454SAndrew Thompson 	sc->sc_pre_tbtt = val;
215602ac6454SAndrew Thompson 	sc->sc_bcn_int = bintval;
215702ac6454SAndrew Thompson 
215802ac6454SAndrew Thompson 	if (sc->sc_bcn_int <= 5)
215902ac6454SAndrew Thompson 		sc->sc_bcn_int = 5;
216002ac6454SAndrew Thompson 	if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
216102ac6454SAndrew Thompson 		sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
216202ac6454SAndrew Thompson 	if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
216302ac6454SAndrew Thompson 		sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
216402ac6454SAndrew Thompson 
216502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
216602ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
216702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
216802ac6454SAndrew Thompson fail:
216902ac6454SAndrew Thompson 	return (error);
217002ac6454SAndrew Thompson }
217102ac6454SAndrew Thompson 
217202ac6454SAndrew Thompson static void
2173760bc48eSAndrew Thompson zyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
217402ac6454SAndrew Thompson {
2175ed6d949aSAndrew Thompson 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
217602ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
21775463c4a4SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
217802ac6454SAndrew Thompson 	struct zyd_plcphdr plcp;
217902ac6454SAndrew Thompson 	struct zyd_rx_stat stat;
2180ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
218102ac6454SAndrew Thompson 	struct mbuf *m;
218202ac6454SAndrew Thompson 	int rlen, rssi;
218302ac6454SAndrew Thompson 
218402ac6454SAndrew Thompson 	if (len < ZYD_MIN_FRAGSZ) {
218502ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
218602ac6454SAndrew Thompson 		    device_get_nameunit(sc->sc_dev), len);
218702ac6454SAndrew Thompson 		ifp->if_ierrors++;
218802ac6454SAndrew Thompson 		return;
218902ac6454SAndrew Thompson 	}
2190ed6d949aSAndrew Thompson 	pc = usbd_xfer_get_frame(xfer, 0);
2191ed6d949aSAndrew Thompson 	usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2192ed6d949aSAndrew Thompson 	usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
219302ac6454SAndrew Thompson 
219402ac6454SAndrew Thompson 	if (stat.flags & ZYD_RX_ERROR) {
219502ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_RECV,
219602ac6454SAndrew Thompson 		    "%s: RX status indicated error (%x)\n",
219702ac6454SAndrew Thompson 		    device_get_nameunit(sc->sc_dev), stat.flags);
219802ac6454SAndrew Thompson 		ifp->if_ierrors++;
219902ac6454SAndrew Thompson 		return;
220002ac6454SAndrew Thompson 	}
220102ac6454SAndrew Thompson 
220202ac6454SAndrew Thompson 	/* compute actual frame length */
220302ac6454SAndrew Thompson 	rlen = len - sizeof(struct zyd_plcphdr) -
220402ac6454SAndrew Thompson 	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
220502ac6454SAndrew Thompson 
220602ac6454SAndrew Thompson 	/* allocate a mbuf to store the frame */
220702ac6454SAndrew Thompson 	if (rlen > MCLBYTES) {
220802ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
220902ac6454SAndrew Thompson 		    device_get_nameunit(sc->sc_dev), rlen);
221002ac6454SAndrew Thompson 		ifp->if_ierrors++;
221102ac6454SAndrew Thompson 		return;
221202ac6454SAndrew Thompson 	} else if (rlen > MHLEN)
221302ac6454SAndrew Thompson 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
221402ac6454SAndrew Thompson 	else
221502ac6454SAndrew Thompson 		m = m_gethdr(M_DONTWAIT, MT_DATA);
221602ac6454SAndrew Thompson 	if (m == NULL) {
221702ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
221802ac6454SAndrew Thompson 		    device_get_nameunit(sc->sc_dev));
221902ac6454SAndrew Thompson 		ifp->if_ierrors++;
222002ac6454SAndrew Thompson 		return;
222102ac6454SAndrew Thompson 	}
222202ac6454SAndrew Thompson 	m->m_pkthdr.rcvif = ifp;
222302ac6454SAndrew Thompson 	m->m_pkthdr.len = m->m_len = rlen;
2224ed6d949aSAndrew Thompson 	usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
222502ac6454SAndrew Thompson 
22265463c4a4SSam Leffler 	if (ieee80211_radiotap_active(ic)) {
222702ac6454SAndrew Thompson 		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
222802ac6454SAndrew Thompson 
222902ac6454SAndrew Thompson 		tap->wr_flags = 0;
223002ac6454SAndrew Thompson 		if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
223102ac6454SAndrew Thompson 			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
223202ac6454SAndrew Thompson 		/* XXX toss, no way to express errors */
223302ac6454SAndrew Thompson 		if (stat.flags & ZYD_RX_DECRYPTERR)
223402ac6454SAndrew Thompson 			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
223502ac6454SAndrew Thompson 		tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
223602ac6454SAndrew Thompson 		    (stat.flags & ZYD_RX_OFDM) ?
223702ac6454SAndrew Thompson 			IEEE80211_T_OFDM : IEEE80211_T_CCK);
223802ac6454SAndrew Thompson 		tap->wr_antsignal = stat.rssi + -95;
223902ac6454SAndrew Thompson 		tap->wr_antnoise = -95;	/* XXX */
224002ac6454SAndrew Thompson 	}
224102ac6454SAndrew Thompson 	rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
224202ac6454SAndrew Thompson 
224302ac6454SAndrew Thompson 	sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
224402ac6454SAndrew Thompson 	sc->sc_rx_data[sc->sc_rx_count].m = m;
224502ac6454SAndrew Thompson 	sc->sc_rx_count++;
224602ac6454SAndrew Thompson }
224702ac6454SAndrew Thompson 
224802ac6454SAndrew Thompson static void
2249ed6d949aSAndrew Thompson zyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
225002ac6454SAndrew Thompson {
2251ed6d949aSAndrew Thompson 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
225202ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
225302ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
225402ac6454SAndrew Thompson 	struct ieee80211_node *ni;
225502ac6454SAndrew Thompson 	struct zyd_rx_desc desc;
225602ac6454SAndrew Thompson 	struct mbuf *m;
2257ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
225802ac6454SAndrew Thompson 	uint32_t offset;
225902ac6454SAndrew Thompson 	uint8_t rssi;
226002ac6454SAndrew Thompson 	int8_t nf;
226102ac6454SAndrew Thompson 	int i;
2262ed6d949aSAndrew Thompson 	int actlen;
2263ed6d949aSAndrew Thompson 
2264ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
226502ac6454SAndrew Thompson 
226602ac6454SAndrew Thompson 	sc->sc_rx_count = 0;
226702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
226802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
2269ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
2270ed6d949aSAndrew Thompson 		usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
227102ac6454SAndrew Thompson 
227202ac6454SAndrew Thompson 		offset = 0;
227302ac6454SAndrew Thompson 		if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
227402ac6454SAndrew Thompson 			DPRINTF(sc, ZYD_DEBUG_RECV,
227502ac6454SAndrew Thompson 			    "%s: received multi-frame transfer\n", __func__);
227602ac6454SAndrew Thompson 
227702ac6454SAndrew Thompson 			for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
227802ac6454SAndrew Thompson 				uint16_t len16 = UGETW(desc.len[i]);
227902ac6454SAndrew Thompson 
2280ed6d949aSAndrew Thompson 				if (len16 == 0 || len16 > actlen)
228102ac6454SAndrew Thompson 					break;
228202ac6454SAndrew Thompson 
228302ac6454SAndrew Thompson 				zyd_rx_data(xfer, offset, len16);
228402ac6454SAndrew Thompson 
228502ac6454SAndrew Thompson 				/* next frame is aligned on a 32-bit boundary */
228602ac6454SAndrew Thompson 				len16 = (len16 + 3) & ~3;
228702ac6454SAndrew Thompson 				offset += len16;
2288ed6d949aSAndrew Thompson 				if (len16 > actlen)
228902ac6454SAndrew Thompson 					break;
2290ed6d949aSAndrew Thompson 				actlen -= len16;
229102ac6454SAndrew Thompson 			}
229202ac6454SAndrew Thompson 		} else {
229302ac6454SAndrew Thompson 			DPRINTF(sc, ZYD_DEBUG_RECV,
229402ac6454SAndrew Thompson 			    "%s: received single-frame transfer\n", __func__);
229502ac6454SAndrew Thompson 
2296ed6d949aSAndrew Thompson 			zyd_rx_data(xfer, 0, actlen);
229702ac6454SAndrew Thompson 		}
229802ac6454SAndrew Thompson 		/* FALLTHROUGH */
229902ac6454SAndrew Thompson 	case USB_ST_SETUP:
230002ac6454SAndrew Thompson tr_setup:
2301ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2302a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
230302ac6454SAndrew Thompson 
230402ac6454SAndrew Thompson 		/*
230502ac6454SAndrew Thompson 		 * At the end of a USB callback it is always safe to unlock
230602ac6454SAndrew Thompson 		 * the private mutex of a device! That is why we do the
230702ac6454SAndrew Thompson 		 * "ieee80211_input" here, and not some lines up!
230802ac6454SAndrew Thompson 		 */
230902ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
231002ac6454SAndrew Thompson 		for (i = 0; i < sc->sc_rx_count; i++) {
231102ac6454SAndrew Thompson 			rssi = sc->sc_rx_data[i].rssi;
231202ac6454SAndrew Thompson 			m = sc->sc_rx_data[i].m;
231302ac6454SAndrew Thompson 			sc->sc_rx_data[i].m = NULL;
231402ac6454SAndrew Thompson 
231502ac6454SAndrew Thompson 			nf = -95;	/* XXX */
231602ac6454SAndrew Thompson 
231702ac6454SAndrew Thompson 			ni = ieee80211_find_rxnode(ic,
231802ac6454SAndrew Thompson 			    mtod(m, struct ieee80211_frame_min *));
231902ac6454SAndrew Thompson 			if (ni != NULL) {
23205463c4a4SSam Leffler 				(void)ieee80211_input(ni, m, rssi, nf);
232102ac6454SAndrew Thompson 				ieee80211_free_node(ni);
232202ac6454SAndrew Thompson 			} else
23235463c4a4SSam Leffler 				(void)ieee80211_input_all(ic, m, rssi, nf);
232402ac6454SAndrew Thompson 		}
232502ac6454SAndrew Thompson 		ZYD_LOCK(sc);
232602ac6454SAndrew Thompson 		break;
232702ac6454SAndrew Thompson 
232802ac6454SAndrew Thompson 	default:			/* Error */
2329ed6d949aSAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
233002ac6454SAndrew Thompson 
2331ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
233202ac6454SAndrew Thompson 			/* try to clear stall first */
2333ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
233402ac6454SAndrew Thompson 			goto tr_setup;
233502ac6454SAndrew Thompson 		}
233602ac6454SAndrew Thompson 		break;
233702ac6454SAndrew Thompson 	}
233802ac6454SAndrew Thompson }
233902ac6454SAndrew Thompson 
234002ac6454SAndrew Thompson static uint8_t
2341e87b19e3SWeongyo Jeong zyd_plcp_signal(struct zyd_softc *sc, int rate)
234202ac6454SAndrew Thompson {
234302ac6454SAndrew Thompson 	switch (rate) {
234402ac6454SAndrew Thompson 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
234502ac6454SAndrew Thompson 	case 12:
234602ac6454SAndrew Thompson 		return (0xb);
234702ac6454SAndrew Thompson 	case 18:
234802ac6454SAndrew Thompson 		return (0xf);
234902ac6454SAndrew Thompson 	case 24:
235002ac6454SAndrew Thompson 		return (0xa);
235102ac6454SAndrew Thompson 	case 36:
235202ac6454SAndrew Thompson 		return (0xe);
235302ac6454SAndrew Thompson 	case 48:
235402ac6454SAndrew Thompson 		return (0x9);
235502ac6454SAndrew Thompson 	case 72:
235602ac6454SAndrew Thompson 		return (0xd);
235702ac6454SAndrew Thompson 	case 96:
235802ac6454SAndrew Thompson 		return (0x8);
235902ac6454SAndrew Thompson 	case 108:
236002ac6454SAndrew Thompson 		return (0xc);
236102ac6454SAndrew Thompson 	/* CCK rates (NB: not IEEE std, device-specific) */
236202ac6454SAndrew Thompson 	case 2:
236302ac6454SAndrew Thompson 		return (0x0);
236402ac6454SAndrew Thompson 	case 4:
236502ac6454SAndrew Thompson 		return (0x1);
236602ac6454SAndrew Thompson 	case 11:
236702ac6454SAndrew Thompson 		return (0x2);
236802ac6454SAndrew Thompson 	case 22:
236902ac6454SAndrew Thompson 		return (0x3);
237002ac6454SAndrew Thompson 	}
237102ac6454SAndrew Thompson 
2372e87b19e3SWeongyo Jeong 	device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2373e87b19e3SWeongyo Jeong 	return (0x0);
237402ac6454SAndrew Thompson }
237502ac6454SAndrew Thompson 
237602ac6454SAndrew Thompson static void
2377ed6d949aSAndrew Thompson zyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
237802ac6454SAndrew Thompson {
2379ed6d949aSAndrew Thompson 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
238002ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
23815463c4a4SSam Leffler 	struct ieee80211vap *vap;
238202ac6454SAndrew Thompson 	struct zyd_tx_data *data;
238302ac6454SAndrew Thompson 	struct mbuf *m;
2384ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
2385ed6d949aSAndrew Thompson 	int actlen;
2386ed6d949aSAndrew Thompson 
2387ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
238802ac6454SAndrew Thompson 
238902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
239002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
239102ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2392ed6d949aSAndrew Thompson 		    actlen);
239302ac6454SAndrew Thompson 
239402ac6454SAndrew Thompson 		/* free resources */
2395ed6d949aSAndrew Thompson 		data = usbd_xfer_get_priv(xfer);
239602ac6454SAndrew Thompson 		zyd_tx_free(data, 0);
2397ed6d949aSAndrew Thompson 		usbd_xfer_set_priv(xfer, NULL);
239802ac6454SAndrew Thompson 
239902ac6454SAndrew Thompson 		ifp->if_opackets++;
240002ac6454SAndrew Thompson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
240102ac6454SAndrew Thompson 
240202ac6454SAndrew Thompson 		/* FALLTHROUGH */
240302ac6454SAndrew Thompson 	case USB_ST_SETUP:
240402ac6454SAndrew Thompson tr_setup:
240502ac6454SAndrew Thompson 		data = STAILQ_FIRST(&sc->tx_q);
240602ac6454SAndrew Thompson 		if (data) {
240702ac6454SAndrew Thompson 			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
240802ac6454SAndrew Thompson 			m = data->m;
240902ac6454SAndrew Thompson 
241002ac6454SAndrew Thompson 			if (m->m_pkthdr.len > ZYD_MAX_TXBUFSZ) {
241102ac6454SAndrew Thompson 				DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
241202ac6454SAndrew Thompson 				    m->m_pkthdr.len);
241302ac6454SAndrew Thompson 				m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
241402ac6454SAndrew Thompson 			}
2415ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
2416ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2417ed6d949aSAndrew Thompson 			usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
241802ac6454SAndrew Thompson 			    m->m_pkthdr.len);
241902ac6454SAndrew Thompson 
24205463c4a4SSam Leffler 			vap = data->ni->ni_vap;
24215463c4a4SSam Leffler 			if (ieee80211_radiotap_active_vap(vap)) {
242202ac6454SAndrew Thompson 				struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
242302ac6454SAndrew Thompson 
242402ac6454SAndrew Thompson 				tap->wt_flags = 0;
242502ac6454SAndrew Thompson 				tap->wt_rate = data->rate;
242602ac6454SAndrew Thompson 
24275463c4a4SSam Leffler 				ieee80211_radiotap_tx(vap, m);
242802ac6454SAndrew Thompson 			}
242902ac6454SAndrew Thompson 
2430ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2431ed6d949aSAndrew Thompson 			usbd_xfer_set_priv(xfer, data);
2432a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
243302ac6454SAndrew Thompson 		}
243402ac6454SAndrew Thompson 		break;
243502ac6454SAndrew Thompson 
243602ac6454SAndrew Thompson 	default:			/* Error */
243702ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2438ed6d949aSAndrew Thompson 		    usbd_errstr(error));
243902ac6454SAndrew Thompson 
244002ac6454SAndrew Thompson 		ifp->if_oerrors++;
2441ed6d949aSAndrew Thompson 		data = usbd_xfer_get_priv(xfer);
2442ed6d949aSAndrew Thompson 		usbd_xfer_set_priv(xfer, NULL);
244302ac6454SAndrew Thompson 		if (data != NULL)
2444ed6d949aSAndrew Thompson 			zyd_tx_free(data, error);
244502ac6454SAndrew Thompson 
2446ed6d949aSAndrew Thompson 		if (error == USB_ERR_STALLED) {
244702ac6454SAndrew Thompson 			/* try to clear stall first */
2448ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
244902ac6454SAndrew Thompson 			goto tr_setup;
245002ac6454SAndrew Thompson 		}
2451ed6d949aSAndrew Thompson 		if (error == USB_ERR_TIMEOUT)
245202ac6454SAndrew Thompson 			device_printf(sc->sc_dev, "device timeout\n");
245302ac6454SAndrew Thompson 		break;
245402ac6454SAndrew Thompson 	}
245502ac6454SAndrew Thompson }
245602ac6454SAndrew Thompson 
245702ac6454SAndrew Thompson static int
2458e87b19e3SWeongyo Jeong zyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
245902ac6454SAndrew Thompson {
246002ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
246102ac6454SAndrew Thompson 	struct ieee80211com *ic = ni->ni_ic;
246202ac6454SAndrew Thompson 	struct zyd_tx_desc *desc;
246302ac6454SAndrew Thompson 	struct zyd_tx_data *data;
246402ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
246502ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
246602ac6454SAndrew Thompson 	struct ieee80211_key *k;
246702ac6454SAndrew Thompson 	int rate, totlen;
2468e87b19e3SWeongyo Jeong 	static uint8_t ratediv[] = ZYD_TX_RATEDIV;
2469e87b19e3SWeongyo Jeong 	uint8_t phy;
247002ac6454SAndrew Thompson 	uint16_t pktlen;
2471e87b19e3SWeongyo Jeong 	uint32_t bits;
247202ac6454SAndrew Thompson 
247302ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
247402ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
247502ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
247602ac6454SAndrew Thompson 	sc->tx_nfree--;
247702ac6454SAndrew Thompson 
2478e87b19e3SWeongyo Jeong 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT ||
2479e87b19e3SWeongyo Jeong 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) {
2480e87b19e3SWeongyo Jeong 		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2481e87b19e3SWeongyo Jeong 		rate = tp->mgmtrate;
248202ac6454SAndrew Thompson 	} else {
2483e87b19e3SWeongyo Jeong 		tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
2484e87b19e3SWeongyo Jeong 		/* for data frames */
2485e87b19e3SWeongyo Jeong 		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
2486e87b19e3SWeongyo Jeong 			rate = tp->mcastrate;
2487e87b19e3SWeongyo Jeong 		else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2488e87b19e3SWeongyo Jeong 			rate = tp->ucastrate;
2489e87b19e3SWeongyo Jeong 		else {
249002ac6454SAndrew Thompson 			(void) ieee80211_amrr_choose(ni, &ZYD_NODE(ni)->amn);
249102ac6454SAndrew Thompson 			rate = ni->ni_txrate;
249202ac6454SAndrew Thompson 		}
2493e87b19e3SWeongyo Jeong 	}
249402ac6454SAndrew Thompson 
249502ac6454SAndrew Thompson 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
249602ac6454SAndrew Thompson 		k = ieee80211_crypto_encap(ni, m0);
249702ac6454SAndrew Thompson 		if (k == NULL) {
249802ac6454SAndrew Thompson 			m_freem(m0);
249902ac6454SAndrew Thompson 			return (ENOBUFS);
250002ac6454SAndrew Thompson 		}
250102ac6454SAndrew Thompson 		/* packet header may have moved, reset our local pointer */
250202ac6454SAndrew Thompson 		wh = mtod(m0, struct ieee80211_frame *);
250302ac6454SAndrew Thompson 	}
250402ac6454SAndrew Thompson 
250502ac6454SAndrew Thompson 	data->ni = ni;
250602ac6454SAndrew Thompson 	data->m = m0;
2507e87b19e3SWeongyo Jeong 	data->rate = rate;
250802ac6454SAndrew Thompson 
250902ac6454SAndrew Thompson 	/* fill Tx descriptor */
2510e87b19e3SWeongyo Jeong 	desc = &data->desc;
2511e87b19e3SWeongyo Jeong 	phy = zyd_plcp_signal(sc, rate);
2512e87b19e3SWeongyo Jeong 	desc->phy = phy;
2513e87b19e3SWeongyo Jeong 	if (ZYD_RATE_IS_OFDM(rate)) {
2514e87b19e3SWeongyo Jeong 		desc->phy |= ZYD_TX_PHY_OFDM;
2515e87b19e3SWeongyo Jeong 		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2516e87b19e3SWeongyo Jeong 			desc->phy |= ZYD_TX_PHY_5GHZ;
2517e87b19e3SWeongyo Jeong 	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2518e87b19e3SWeongyo Jeong 		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2519e87b19e3SWeongyo Jeong 
2520e87b19e3SWeongyo Jeong 	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
252102ac6454SAndrew Thompson 	desc->len = htole16(totlen);
252202ac6454SAndrew Thompson 
2523e87b19e3SWeongyo Jeong 	desc->flags = ZYD_TX_FLAG_BACKOFF;
252402ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
252502ac6454SAndrew Thompson 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
252602ac6454SAndrew Thompson 		if (totlen > vap->iv_rtsthreshold) {
252702ac6454SAndrew Thompson 			desc->flags |= ZYD_TX_FLAG_RTS;
252802ac6454SAndrew Thompson 		} else if (ZYD_RATE_IS_OFDM(rate) &&
252902ac6454SAndrew Thompson 		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
253002ac6454SAndrew Thompson 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
253102ac6454SAndrew Thompson 				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
253202ac6454SAndrew Thompson 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
253302ac6454SAndrew Thompson 				desc->flags |= ZYD_TX_FLAG_RTS;
253402ac6454SAndrew Thompson 		}
2535e87b19e3SWeongyo Jeong 	} else
2536e87b19e3SWeongyo Jeong 		desc->flags |= ZYD_TX_FLAG_MULTICAST;
253702ac6454SAndrew Thompson 	if ((wh->i_fc[0] &
253802ac6454SAndrew Thompson 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
253902ac6454SAndrew Thompson 	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
254002ac6454SAndrew Thompson 		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
254102ac6454SAndrew Thompson 
254202ac6454SAndrew Thompson 	/* actual transmit length (XXX why +10?) */
2543e87b19e3SWeongyo Jeong 	pktlen = ZYD_TX_DESC_SIZE + 10;
254402ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211)
254502ac6454SAndrew Thompson 		pktlen += totlen;
254602ac6454SAndrew Thompson 	desc->pktlen = htole16(pktlen);
254702ac6454SAndrew Thompson 
2548e87b19e3SWeongyo Jeong 	bits = (rate == 11) ? (totlen * 16) + 10 :
2549e87b19e3SWeongyo Jeong 	    ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2550a6b8e0e9SWeongyo Jeong 	desc->plcp_length = htole16(bits / ratediv[phy]);
255102ac6454SAndrew Thompson 	desc->plcp_service = 0;
2552e87b19e3SWeongyo Jeong 	if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
255302ac6454SAndrew Thompson 		desc->plcp_service |= ZYD_PLCP_LENGEXT;
2554e87b19e3SWeongyo Jeong 	desc->nextlen = 0;
2555e87b19e3SWeongyo Jeong 
2556e87b19e3SWeongyo Jeong 	if (ieee80211_radiotap_active_vap(vap)) {
2557e87b19e3SWeongyo Jeong 		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2558e87b19e3SWeongyo Jeong 
2559e87b19e3SWeongyo Jeong 		tap->wt_flags = 0;
2560e87b19e3SWeongyo Jeong 		tap->wt_rate = rate;
2561e87b19e3SWeongyo Jeong 
2562e87b19e3SWeongyo Jeong 		ieee80211_radiotap_tx(vap, m0);
256302ac6454SAndrew Thompson 	}
256402ac6454SAndrew Thompson 
256502ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_XMIT,
256602ac6454SAndrew Thompson 	    "%s: sending data frame len=%zu rate=%u\n",
256702ac6454SAndrew Thompson 	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
256802ac6454SAndrew Thompson 		rate);
256902ac6454SAndrew Thompson 
257002ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2571a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
257202ac6454SAndrew Thompson 
257302ac6454SAndrew Thompson 	return (0);
257402ac6454SAndrew Thompson }
257502ac6454SAndrew Thompson 
257602ac6454SAndrew Thompson static void
257702ac6454SAndrew Thompson zyd_start(struct ifnet *ifp)
257802ac6454SAndrew Thompson {
257902ac6454SAndrew Thompson 	struct zyd_softc *sc = ifp->if_softc;
258002ac6454SAndrew Thompson 	struct ieee80211_node *ni;
258102ac6454SAndrew Thompson 	struct mbuf *m;
258202ac6454SAndrew Thompson 
258302ac6454SAndrew Thompson 	ZYD_LOCK(sc);
258402ac6454SAndrew Thompson 	for (;;) {
258502ac6454SAndrew Thompson 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
258602ac6454SAndrew Thompson 		if (m == NULL)
258702ac6454SAndrew Thompson 			break;
258802ac6454SAndrew Thompson 		if (sc->tx_nfree == 0) {
258902ac6454SAndrew Thompson 			IFQ_DRV_PREPEND(&ifp->if_snd, m);
259002ac6454SAndrew Thompson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
259102ac6454SAndrew Thompson 			break;
259202ac6454SAndrew Thompson 		}
259302ac6454SAndrew Thompson 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2594e87b19e3SWeongyo Jeong 		if (zyd_tx_start(sc, m, ni) != 0) {
259502ac6454SAndrew Thompson 			ieee80211_free_node(ni);
259602ac6454SAndrew Thompson 			ifp->if_oerrors++;
259702ac6454SAndrew Thompson 			break;
259802ac6454SAndrew Thompson 		}
259902ac6454SAndrew Thompson 	}
260002ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
260102ac6454SAndrew Thompson }
260202ac6454SAndrew Thompson 
260302ac6454SAndrew Thompson static int
260402ac6454SAndrew Thompson zyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
260502ac6454SAndrew Thompson 	const struct ieee80211_bpf_params *params)
260602ac6454SAndrew Thompson {
260702ac6454SAndrew Thompson 	struct ieee80211com *ic = ni->ni_ic;
260802ac6454SAndrew Thompson 	struct ifnet *ifp = ic->ic_ifp;
260902ac6454SAndrew Thompson 	struct zyd_softc *sc = ifp->if_softc;
261002ac6454SAndrew Thompson 
261102ac6454SAndrew Thompson 	ZYD_LOCK(sc);
261202ac6454SAndrew Thompson 	/* prevent management frames from being sent if we're not ready */
261302ac6454SAndrew Thompson 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
261402ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
261502ac6454SAndrew Thompson 		m_freem(m);
261602ac6454SAndrew Thompson 		ieee80211_free_node(ni);
261702ac6454SAndrew Thompson 		return (ENETDOWN);
261802ac6454SAndrew Thompson 	}
261902ac6454SAndrew Thompson 	if (sc->tx_nfree == 0) {
262002ac6454SAndrew Thompson 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
262102ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
262202ac6454SAndrew Thompson 		m_freem(m);
262302ac6454SAndrew Thompson 		ieee80211_free_node(ni);
262402ac6454SAndrew Thompson 		return (ENOBUFS);		/* XXX */
262502ac6454SAndrew Thompson 	}
262602ac6454SAndrew Thompson 
262702ac6454SAndrew Thompson 	/*
262802ac6454SAndrew Thompson 	 * Legacy path; interpret frame contents to decide
262902ac6454SAndrew Thompson 	 * precisely how to send the frame.
263002ac6454SAndrew Thompson 	 * XXX raw path
263102ac6454SAndrew Thompson 	 */
2632e87b19e3SWeongyo Jeong 	if (zyd_tx_start(sc, m, ni) != 0) {
263302ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
263402ac6454SAndrew Thompson 		ifp->if_oerrors++;
263502ac6454SAndrew Thompson 		ieee80211_free_node(ni);
263602ac6454SAndrew Thompson 		return (EIO);
263702ac6454SAndrew Thompson 	}
263802ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
263902ac6454SAndrew Thompson 	return (0);
264002ac6454SAndrew Thompson }
264102ac6454SAndrew Thompson 
264202ac6454SAndrew Thompson static int
264302ac6454SAndrew Thompson zyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
264402ac6454SAndrew Thompson {
264502ac6454SAndrew Thompson 	struct zyd_softc *sc = ifp->if_softc;
264602ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
264702ac6454SAndrew Thompson 	struct ifreq *ifr = (struct ifreq *) data;
264802ac6454SAndrew Thompson 	int error = 0, startall = 0;
264902ac6454SAndrew Thompson 
265002ac6454SAndrew Thompson 	switch (cmd) {
265102ac6454SAndrew Thompson 	case SIOCSIFFLAGS:
265202ac6454SAndrew Thompson 		ZYD_LOCK(sc);
265302ac6454SAndrew Thompson 		if (ifp->if_flags & IFF_UP) {
26545efea30fSAndrew Thompson 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
26555efea30fSAndrew Thompson 				zyd_init_locked(sc);
265602ac6454SAndrew Thompson 				startall = 1;
26575efea30fSAndrew Thompson 			} else
26585efea30fSAndrew Thompson 				zyd_set_multi(sc);
265902ac6454SAndrew Thompson 		} else {
26605efea30fSAndrew Thompson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
26615efea30fSAndrew Thompson 				zyd_stop(sc);
266202ac6454SAndrew Thompson 		}
266302ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
266402ac6454SAndrew Thompson 		if (startall)
266502ac6454SAndrew Thompson 			ieee80211_start_all(ic);
266602ac6454SAndrew Thompson 		break;
266702ac6454SAndrew Thompson 	case SIOCGIFMEDIA:
266802ac6454SAndrew Thompson 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
266902ac6454SAndrew Thompson 		break;
267002ac6454SAndrew Thompson 	case SIOCGIFADDR:
267102ac6454SAndrew Thompson 		error = ether_ioctl(ifp, cmd, data);
267202ac6454SAndrew Thompson 		break;
267302ac6454SAndrew Thompson 	default:
267402ac6454SAndrew Thompson 		error = EINVAL;
267502ac6454SAndrew Thompson 		break;
267602ac6454SAndrew Thompson 	}
267702ac6454SAndrew Thompson 	return (error);
267802ac6454SAndrew Thompson }
267902ac6454SAndrew Thompson 
268002ac6454SAndrew Thompson static void
26815efea30fSAndrew Thompson zyd_init_locked(struct zyd_softc *sc)
268202ac6454SAndrew Thompson {
268302ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
268402ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
2685760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
268602ac6454SAndrew Thompson 	int error;
268702ac6454SAndrew Thompson 	uint32_t val;
268802ac6454SAndrew Thompson 
268902ac6454SAndrew Thompson 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
269002ac6454SAndrew Thompson 
269102ac6454SAndrew Thompson 	if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
269202ac6454SAndrew Thompson 		error = zyd_loadfirmware(sc);
269302ac6454SAndrew Thompson 		if (error != 0) {
269402ac6454SAndrew Thompson 			device_printf(sc->sc_dev,
269502ac6454SAndrew Thompson 			    "could not load firmware (error=%d)\n", error);
269602ac6454SAndrew Thompson 			goto fail;
269702ac6454SAndrew Thompson 		}
269802ac6454SAndrew Thompson 
269902ac6454SAndrew Thompson 		/* reset device */
2700a593f6b8SAndrew Thompson 		cd = usbd_get_config_descriptor(sc->sc_udev);
2701a593f6b8SAndrew Thompson 		error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
270202ac6454SAndrew Thompson 		    cd->bConfigurationValue);
270302ac6454SAndrew Thompson 		if (error)
270402ac6454SAndrew Thompson 			device_printf(sc->sc_dev, "reset failed, continuing\n");
270502ac6454SAndrew Thompson 
270602ac6454SAndrew Thompson 		error = zyd_hw_init(sc);
270702ac6454SAndrew Thompson 		if (error) {
270802ac6454SAndrew Thompson 			device_printf(sc->sc_dev,
270902ac6454SAndrew Thompson 			    "hardware initialization failed\n");
271002ac6454SAndrew Thompson 			goto fail;
271102ac6454SAndrew Thompson 		}
271202ac6454SAndrew Thompson 
271302ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
271402ac6454SAndrew Thompson 		    "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
271502ac6454SAndrew Thompson 		    "BE%x NP%x Gain%x F%x\n",
271602ac6454SAndrew Thompson 		    (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
271702ac6454SAndrew Thompson 		    sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
271802ac6454SAndrew Thompson 		    zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
271902ac6454SAndrew Thompson 		    sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
272002ac6454SAndrew Thompson 		    sc->sc_cckgain, sc->sc_fix_cr157);
272102ac6454SAndrew Thompson 
272202ac6454SAndrew Thompson 		/* read regulatory domain (currently unused) */
272302ac6454SAndrew Thompson 		zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
272402ac6454SAndrew Thompson 		sc->sc_regdomain = val >> 16;
272502ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
272602ac6454SAndrew Thompson 		    sc->sc_regdomain);
272702ac6454SAndrew Thompson 
272802ac6454SAndrew Thompson 		/* we'll do software WEP decryption for now */
272902ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
273002ac6454SAndrew Thompson 		    __func__);
273102ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
273202ac6454SAndrew Thompson 
273302ac6454SAndrew Thompson 		sc->sc_flags |= ZYD_FLAG_INITONCE;
273402ac6454SAndrew Thompson 	}
273502ac6454SAndrew Thompson 
273602ac6454SAndrew Thompson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
27375efea30fSAndrew Thompson 		zyd_stop(sc);
273802ac6454SAndrew Thompson 
273929aca940SSam Leffler 	DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
274029aca940SSam Leffler 	    IF_LLADDR(ifp), ":");
274129aca940SSam Leffler 	error = zyd_set_macaddr(sc, IF_LLADDR(ifp));
274202ac6454SAndrew Thompson 	if (error != 0)
274302ac6454SAndrew Thompson 		return;
274402ac6454SAndrew Thompson 
274502ac6454SAndrew Thompson 	/* set basic rates */
274602ac6454SAndrew Thompson 	if (ic->ic_curmode == IEEE80211_MODE_11B)
274702ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
274802ac6454SAndrew Thompson 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
274902ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
275002ac6454SAndrew Thompson 	else	/* assumes 802.11b/g */
275102ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
275202ac6454SAndrew Thompson 
275302ac6454SAndrew Thompson 	/* promiscuous mode */
275402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
275502ac6454SAndrew Thompson 	/* multicast setup */
275602ac6454SAndrew Thompson 	zyd_set_multi(sc);
275702ac6454SAndrew Thompson 	/* set RX filter  */
275802ac6454SAndrew Thompson 	error = zyd_set_rxfilter(sc);
275902ac6454SAndrew Thompson 	if (error != 0)
276002ac6454SAndrew Thompson 		goto fail;
276102ac6454SAndrew Thompson 
276202ac6454SAndrew Thompson 	/* switch radio transmitter ON */
276302ac6454SAndrew Thompson 	error = zyd_switch_radio(sc, 1);
276402ac6454SAndrew Thompson 	if (error != 0)
276502ac6454SAndrew Thompson 		goto fail;
276602ac6454SAndrew Thompson 	/* set default BSS channel */
276702ac6454SAndrew Thompson 	zyd_set_chan(sc, ic->ic_curchan);
276802ac6454SAndrew Thompson 
276902ac6454SAndrew Thompson 	/*
277002ac6454SAndrew Thompson 	 * Allocate Tx and Rx xfer queues.
277102ac6454SAndrew Thompson 	 */
277202ac6454SAndrew Thompson 	zyd_setup_tx_list(sc);
277302ac6454SAndrew Thompson 
277402ac6454SAndrew Thompson 	/* enable interrupts */
277502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
277602ac6454SAndrew Thompson 
277702ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
277802ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2779ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2780a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2781a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
278202ac6454SAndrew Thompson 
278302ac6454SAndrew Thompson 	return;
278402ac6454SAndrew Thompson 
27855efea30fSAndrew Thompson fail:	zyd_stop(sc);
278602ac6454SAndrew Thompson 	return;
278702ac6454SAndrew Thompson }
278802ac6454SAndrew Thompson 
278902ac6454SAndrew Thompson static void
279002ac6454SAndrew Thompson zyd_init(void *priv)
279102ac6454SAndrew Thompson {
279202ac6454SAndrew Thompson 	struct zyd_softc *sc = priv;
279302ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
279402ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
279502ac6454SAndrew Thompson 
279602ac6454SAndrew Thompson 	ZYD_LOCK(sc);
27975efea30fSAndrew Thompson 	zyd_init_locked(sc);
279802ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
279902ac6454SAndrew Thompson 
280002ac6454SAndrew Thompson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
280102ac6454SAndrew Thompson 		ieee80211_start_all(ic);		/* start all vap's */
280202ac6454SAndrew Thompson }
280302ac6454SAndrew Thompson 
280402ac6454SAndrew Thompson static void
28055efea30fSAndrew Thompson zyd_stop(struct zyd_softc *sc)
280602ac6454SAndrew Thompson {
280702ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
280802ac6454SAndrew Thompson 	int error;
280902ac6454SAndrew Thompson 
281002ac6454SAndrew Thompson 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
281102ac6454SAndrew Thompson 
281202ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
281302ac6454SAndrew Thompson 
281402ac6454SAndrew Thompson 	/*
281502ac6454SAndrew Thompson 	 * Drain all the transfers, if not already drained:
281602ac6454SAndrew Thompson 	 */
281702ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
2818a593f6b8SAndrew Thompson 	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2819a593f6b8SAndrew Thompson 	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
282002ac6454SAndrew Thompson 	ZYD_LOCK(sc);
282102ac6454SAndrew Thompson 
282202ac6454SAndrew Thompson 	zyd_unsetup_tx_list(sc);
282302ac6454SAndrew Thompson 
282402ac6454SAndrew Thompson 	/* Stop now if the device was never set up */
282502ac6454SAndrew Thompson 	if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
282602ac6454SAndrew Thompson 		return;
282702ac6454SAndrew Thompson 
282802ac6454SAndrew Thompson 	/* switch radio transmitter OFF */
282902ac6454SAndrew Thompson 	error = zyd_switch_radio(sc, 0);
283002ac6454SAndrew Thompson 	if (error != 0)
283102ac6454SAndrew Thompson 		goto fail;
283202ac6454SAndrew Thompson 	/* disable Rx */
283302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
283402ac6454SAndrew Thompson 	/* disable interrupts */
283502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
283602ac6454SAndrew Thompson 
283702ac6454SAndrew Thompson fail:
283802ac6454SAndrew Thompson 	return;
283902ac6454SAndrew Thompson }
284002ac6454SAndrew Thompson 
284102ac6454SAndrew Thompson static int
284202ac6454SAndrew Thompson zyd_loadfirmware(struct zyd_softc *sc)
284302ac6454SAndrew Thompson {
2844760bc48eSAndrew Thompson 	struct usb_device_request req;
284502ac6454SAndrew Thompson 	size_t size;
284602ac6454SAndrew Thompson 	u_char *fw;
284702ac6454SAndrew Thompson 	uint8_t stat;
284802ac6454SAndrew Thompson 	uint16_t addr;
284902ac6454SAndrew Thompson 
285002ac6454SAndrew Thompson 	if (sc->sc_flags & ZYD_FLAG_FWLOADED)
285102ac6454SAndrew Thompson 		return (0);
285202ac6454SAndrew Thompson 
285302ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211) {
285402ac6454SAndrew Thompson 		fw = (u_char *)zd1211_firmware;
285502ac6454SAndrew Thompson 		size = sizeof(zd1211_firmware);
285602ac6454SAndrew Thompson 	} else {
285702ac6454SAndrew Thompson 		fw = (u_char *)zd1211b_firmware;
285802ac6454SAndrew Thompson 		size = sizeof(zd1211b_firmware);
285902ac6454SAndrew Thompson 	}
286002ac6454SAndrew Thompson 
286102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
286202ac6454SAndrew Thompson 	req.bRequest = ZYD_DOWNLOADREQ;
286302ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
286402ac6454SAndrew Thompson 
286502ac6454SAndrew Thompson 	addr = ZYD_FIRMWARE_START_ADDR;
286602ac6454SAndrew Thompson 	while (size > 0) {
286702ac6454SAndrew Thompson 		/*
286802ac6454SAndrew Thompson 		 * When the transfer size is 4096 bytes, it is not
286902ac6454SAndrew Thompson 		 * likely to be able to transfer it.
287002ac6454SAndrew Thompson 		 * The cause is port or machine or chip?
287102ac6454SAndrew Thompson 		 */
287202ac6454SAndrew Thompson 		const int mlen = min(size, 64);
287302ac6454SAndrew Thompson 
287402ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_FW,
287502ac6454SAndrew Thompson 		    "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
287602ac6454SAndrew Thompson 
287702ac6454SAndrew Thompson 		USETW(req.wValue, addr);
287802ac6454SAndrew Thompson 		USETW(req.wLength, mlen);
287902ac6454SAndrew Thompson 		if (zyd_do_request(sc, &req, fw) != 0)
288002ac6454SAndrew Thompson 			return (EIO);
288102ac6454SAndrew Thompson 
288202ac6454SAndrew Thompson 		addr += mlen / 2;
288302ac6454SAndrew Thompson 		fw   += mlen;
288402ac6454SAndrew Thompson 		size -= mlen;
288502ac6454SAndrew Thompson 	}
288602ac6454SAndrew Thompson 
288702ac6454SAndrew Thompson 	/* check whether the upload succeeded */
288802ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
288902ac6454SAndrew Thompson 	req.bRequest = ZYD_DOWNLOADSTS;
289002ac6454SAndrew Thompson 	USETW(req.wValue, 0);
289102ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
289202ac6454SAndrew Thompson 	USETW(req.wLength, sizeof(stat));
289302ac6454SAndrew Thompson 	if (zyd_do_request(sc, &req, &stat) != 0)
289402ac6454SAndrew Thompson 		return (EIO);
289502ac6454SAndrew Thompson 
289602ac6454SAndrew Thompson 	sc->sc_flags |= ZYD_FLAG_FWLOADED;
289702ac6454SAndrew Thompson 
289802ac6454SAndrew Thompson 	return (stat & 0x80) ? (EIO) : (0);
289902ac6454SAndrew Thompson }
290002ac6454SAndrew Thompson 
290102ac6454SAndrew Thompson static void
290202ac6454SAndrew Thompson zyd_newassoc(struct ieee80211_node *ni, int isnew)
290302ac6454SAndrew Thompson {
290402ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
290502ac6454SAndrew Thompson 
290602ac6454SAndrew Thompson 	ieee80211_amrr_node_init(&ZYD_VAP(vap)->amrr, &ZYD_NODE(ni)->amn, ni);
290702ac6454SAndrew Thompson }
290802ac6454SAndrew Thompson 
290902ac6454SAndrew Thompson static void
291002ac6454SAndrew Thompson zyd_scan_start(struct ieee80211com *ic)
291102ac6454SAndrew Thompson {
29125efea30fSAndrew Thompson 	struct ifnet *ifp = ic->ic_ifp;
29135efea30fSAndrew Thompson 	struct zyd_softc *sc = ifp->if_softc;
291402ac6454SAndrew Thompson 
291502ac6454SAndrew Thompson 	ZYD_LOCK(sc);
29165efea30fSAndrew Thompson 	/* want broadcast address while scanning */
29175efea30fSAndrew Thompson 	zyd_set_bssid(sc, ifp->if_broadcastaddr);
291802ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
291902ac6454SAndrew Thompson }
292002ac6454SAndrew Thompson 
292102ac6454SAndrew Thompson static void
292202ac6454SAndrew Thompson zyd_scan_end(struct ieee80211com *ic)
292302ac6454SAndrew Thompson {
292402ac6454SAndrew Thompson 	struct zyd_softc *sc = ic->ic_ifp->if_softc;
292502ac6454SAndrew Thompson 
292602ac6454SAndrew Thompson 	ZYD_LOCK(sc);
29275efea30fSAndrew Thompson 	/* restore previous bssid */
29285efea30fSAndrew Thompson 	zyd_set_bssid(sc, sc->sc_bssid);
292902ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
293002ac6454SAndrew Thompson }
293102ac6454SAndrew Thompson 
293202ac6454SAndrew Thompson static void
293302ac6454SAndrew Thompson zyd_set_channel(struct ieee80211com *ic)
293402ac6454SAndrew Thompson {
293502ac6454SAndrew Thompson 	struct zyd_softc *sc = ic->ic_ifp->if_softc;
293602ac6454SAndrew Thompson 
293702ac6454SAndrew Thompson 	ZYD_LOCK(sc);
293802ac6454SAndrew Thompson 	zyd_set_chan(sc, ic->ic_curchan);
29395efea30fSAndrew Thompson 	ZYD_UNLOCK(sc);
294002ac6454SAndrew Thompson }
294102ac6454SAndrew Thompson 
294202ac6454SAndrew Thompson static device_method_t zyd_methods[] = {
294302ac6454SAndrew Thompson         /* Device interface */
294402ac6454SAndrew Thompson         DEVMETHOD(device_probe, zyd_match),
294502ac6454SAndrew Thompson         DEVMETHOD(device_attach, zyd_attach),
294602ac6454SAndrew Thompson         DEVMETHOD(device_detach, zyd_detach),
294702ac6454SAndrew Thompson 
294802ac6454SAndrew Thompson 	{ 0, 0 }
294902ac6454SAndrew Thompson };
295002ac6454SAndrew Thompson 
295102ac6454SAndrew Thompson static driver_t zyd_driver = {
295202ac6454SAndrew Thompson         "zyd",
295302ac6454SAndrew Thompson         zyd_methods,
295402ac6454SAndrew Thompson         sizeof(struct zyd_softc)
295502ac6454SAndrew Thompson };
295602ac6454SAndrew Thompson 
295702ac6454SAndrew Thompson static devclass_t zyd_devclass;
295802ac6454SAndrew Thompson 
29599aef556dSAndrew Thompson DRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0);
296002ac6454SAndrew Thompson MODULE_DEPEND(zyd, usb, 1, 1, 1);
296102ac6454SAndrew Thompson MODULE_DEPEND(zyd, wlan, 1, 1, 1);
296202ac6454SAndrew Thompson MODULE_DEPEND(zyd, wlan_amrr, 1, 1, 1);
2963