xref: /freebsd/sys/dev/usb/wlan/if_zyd.c (revision 29aca940805022fa5b7d02641f5309e67ab1f765)
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 
2902ac6454SAndrew Thompson #include "usbdevs.h"
3002ac6454SAndrew Thompson #include <dev/usb/usb.h>
3102ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
3202ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
3302ac6454SAndrew Thompson 
3402ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
3502ac6454SAndrew Thompson #include <dev/usb/usb_lookup.h>
3602ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
3702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
3802ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
3902ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
4002ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
4102ac6454SAndrew Thompson 
4202ac6454SAndrew Thompson #include <dev/usb/wlan/usb_wlan.h>
4302ac6454SAndrew Thompson #include <dev/usb/wlan/if_zydreg.h>
4402ac6454SAndrew Thompson #include <dev/usb/wlan/if_zydfw.h>
4502ac6454SAndrew Thompson 
4602ac6454SAndrew Thompson #if USB_DEBUG
4702ac6454SAndrew Thompson static int zyd_debug = 0;
4802ac6454SAndrew Thompson 
4902ac6454SAndrew Thompson SYSCTL_NODE(_hw_usb2, OID_AUTO, zyd, CTLFLAG_RW, 0, "USB zyd");
5002ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_zyd, OID_AUTO, debug, CTLFLAG_RW, &zyd_debug, 0,
5102ac6454SAndrew Thompson     "zyd debug level");
5202ac6454SAndrew Thompson 
5302ac6454SAndrew Thompson enum {
5402ac6454SAndrew Thompson 	ZYD_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
5502ac6454SAndrew Thompson 	ZYD_DEBUG_RECV		= 0x00000002,	/* basic recv operation */
5602ac6454SAndrew Thompson 	ZYD_DEBUG_RESET		= 0x00000004,	/* reset processing */
5702ac6454SAndrew Thompson 	ZYD_DEBUG_INIT		= 0x00000008,	/* device init */
5802ac6454SAndrew Thompson 	ZYD_DEBUG_TX_PROC	= 0x00000010,	/* tx ISR proc */
5902ac6454SAndrew Thompson 	ZYD_DEBUG_RX_PROC	= 0x00000020,	/* rx ISR proc */
6002ac6454SAndrew Thompson 	ZYD_DEBUG_STATE		= 0x00000040,	/* 802.11 state transitions */
6102ac6454SAndrew Thompson 	ZYD_DEBUG_STAT		= 0x00000080,	/* statistic */
6202ac6454SAndrew Thompson 	ZYD_DEBUG_FW		= 0x00000100,	/* firmware */
6302ac6454SAndrew Thompson 	ZYD_DEBUG_CMD		= 0x00000200,	/* fw commands */
6402ac6454SAndrew Thompson 	ZYD_DEBUG_ANY		= 0xffffffff
6502ac6454SAndrew Thompson };
6602ac6454SAndrew Thompson #define	DPRINTF(sc, m, fmt, ...) do {				\
6702ac6454SAndrew Thompson 	if (zyd_debug & (m))					\
6802ac6454SAndrew Thompson 		printf("%s: " fmt, __func__, ## __VA_ARGS__);	\
6902ac6454SAndrew Thompson } while (0)
7002ac6454SAndrew Thompson #else
7102ac6454SAndrew Thompson #define	DPRINTF(sc, m, fmt, ...) do {				\
7202ac6454SAndrew Thompson 	(void) sc;						\
7302ac6454SAndrew Thompson } while (0)
7402ac6454SAndrew Thompson #endif
7502ac6454SAndrew Thompson 
7602ac6454SAndrew Thompson #define	zyd_do_request(sc,req,data) \
7702ac6454SAndrew Thompson     usb2_do_request_proc((sc)->sc_udev, &(sc)->sc_tq, req, data, 0, NULL, 5000)
7802ac6454SAndrew Thompson 
7902ac6454SAndrew Thompson static device_probe_t zyd_match;
8002ac6454SAndrew Thompson static device_attach_t zyd_attach;
8102ac6454SAndrew Thompson static device_detach_t zyd_detach;
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson static usb2_callback_t zyd_intr_read_callback;
8402ac6454SAndrew Thompson static usb2_callback_t zyd_intr_write_callback;
8502ac6454SAndrew Thompson static usb2_callback_t zyd_bulk_read_callback;
8602ac6454SAndrew Thompson static usb2_callback_t zyd_bulk_write_callback;
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson static usb2_proc_callback_t zyd_attach_post;
8902ac6454SAndrew Thompson static usb2_proc_callback_t zyd_task;
9002ac6454SAndrew Thompson static usb2_proc_callback_t zyd_scantask;
9102ac6454SAndrew Thompson static usb2_proc_callback_t zyd_multitask;
9202ac6454SAndrew Thompson static usb2_proc_callback_t zyd_init_task;
9302ac6454SAndrew Thompson static usb2_proc_callback_t zyd_stop_task;
9477ddf3d3SAndrew Thompson static usb2_proc_callback_t zyd_flush_task;
9502ac6454SAndrew Thompson 
9602ac6454SAndrew Thompson static struct ieee80211vap *zyd_vap_create(struct ieee80211com *,
9702ac6454SAndrew Thompson 		    const char name[IFNAMSIZ], int unit, int opmode,
9802ac6454SAndrew Thompson 		    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
9902ac6454SAndrew Thompson 		    const uint8_t mac[IEEE80211_ADDR_LEN]);
10002ac6454SAndrew Thompson static void	zyd_vap_delete(struct ieee80211vap *);
10102ac6454SAndrew Thompson static void	zyd_tx_free(struct zyd_tx_data *, int);
10202ac6454SAndrew Thompson static void	zyd_setup_tx_list(struct zyd_softc *);
10302ac6454SAndrew Thompson static void	zyd_unsetup_tx_list(struct zyd_softc *);
10402ac6454SAndrew Thompson static struct ieee80211_node *zyd_node_alloc(struct ieee80211vap *,
10502ac6454SAndrew Thompson 			    const uint8_t mac[IEEE80211_ADDR_LEN]);
10602ac6454SAndrew Thompson static int	zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int);
10702ac6454SAndrew Thompson static int	zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
10802ac6454SAndrew Thompson 		    void *, int, int);
10902ac6454SAndrew Thompson static int	zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
11002ac6454SAndrew Thompson static int	zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
11102ac6454SAndrew Thompson static int	zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
11202ac6454SAndrew Thompson static int	zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
11302ac6454SAndrew Thompson static int	zyd_rfwrite(struct zyd_softc *, uint32_t);
11402ac6454SAndrew Thompson static int	zyd_lock_phy(struct zyd_softc *);
11502ac6454SAndrew Thompson static int	zyd_unlock_phy(struct zyd_softc *);
11602ac6454SAndrew Thompson static int	zyd_rf_attach(struct zyd_softc *, uint8_t);
11702ac6454SAndrew Thompson static const char *zyd_rf_name(uint8_t);
11802ac6454SAndrew Thompson static int	zyd_hw_init(struct zyd_softc *);
11902ac6454SAndrew Thompson static int	zyd_read_pod(struct zyd_softc *);
12002ac6454SAndrew Thompson static int	zyd_read_eeprom(struct zyd_softc *);
12102ac6454SAndrew Thompson static int	zyd_get_macaddr(struct zyd_softc *);
12202ac6454SAndrew Thompson static int	zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
12302ac6454SAndrew Thompson static int	zyd_set_bssid(struct zyd_softc *, const uint8_t *);
12402ac6454SAndrew Thompson static int	zyd_switch_radio(struct zyd_softc *, int);
12502ac6454SAndrew Thompson static int	zyd_set_led(struct zyd_softc *, int, int);
12602ac6454SAndrew Thompson static void	zyd_set_multi(struct zyd_softc *);
12702ac6454SAndrew Thompson static void	zyd_update_mcast(struct ifnet *);
12802ac6454SAndrew Thompson static int	zyd_set_rxfilter(struct zyd_softc *);
12902ac6454SAndrew Thompson static void	zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
13002ac6454SAndrew Thompson static int	zyd_set_beacon_interval(struct zyd_softc *, int);
13102ac6454SAndrew Thompson static void	zyd_rx_data(struct usb2_xfer *, int, uint16_t);
13202ac6454SAndrew Thompson static int	zyd_tx_mgt(struct zyd_softc *, struct mbuf *,
13302ac6454SAndrew Thompson 		    struct ieee80211_node *);
13402ac6454SAndrew Thompson static int	zyd_tx_data(struct zyd_softc *, struct mbuf *,
13502ac6454SAndrew Thompson 		    struct ieee80211_node *);
13602ac6454SAndrew Thompson static void	zyd_start(struct ifnet *);
13702ac6454SAndrew Thompson static int	zyd_raw_xmit(struct ieee80211_node *, struct mbuf *,
13802ac6454SAndrew Thompson 		    const struct ieee80211_bpf_params *);
13902ac6454SAndrew Thompson static int	zyd_ioctl(struct ifnet *, u_long, caddr_t);
14002ac6454SAndrew Thompson static void	zyd_init(void *);
14102ac6454SAndrew Thompson static int	zyd_loadfirmware(struct zyd_softc *);
14202ac6454SAndrew Thompson static void	zyd_newassoc(struct ieee80211_node *, int);
14302ac6454SAndrew Thompson static void	zyd_scan_start(struct ieee80211com *);
14402ac6454SAndrew Thompson static void	zyd_scan_end(struct ieee80211com *);
14502ac6454SAndrew Thompson static void	zyd_set_channel(struct ieee80211com *);
14602ac6454SAndrew Thompson static int	zyd_rfmd_init(struct zyd_rf *);
14702ac6454SAndrew Thompson static int	zyd_rfmd_switch_radio(struct zyd_rf *, int);
14802ac6454SAndrew Thompson static int	zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
14902ac6454SAndrew Thompson static int	zyd_al2230_init(struct zyd_rf *);
15002ac6454SAndrew Thompson static int	zyd_al2230_switch_radio(struct zyd_rf *, int);
15102ac6454SAndrew Thompson static int	zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
15202ac6454SAndrew Thompson static int	zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t);
15302ac6454SAndrew Thompson static int	zyd_al2230_init_b(struct zyd_rf *);
15402ac6454SAndrew Thompson static int	zyd_al7230B_init(struct zyd_rf *);
15502ac6454SAndrew Thompson static int	zyd_al7230B_switch_radio(struct zyd_rf *, int);
15602ac6454SAndrew Thompson static int	zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
15702ac6454SAndrew Thompson static int	zyd_al2210_init(struct zyd_rf *);
15802ac6454SAndrew Thompson static int	zyd_al2210_switch_radio(struct zyd_rf *, int);
15902ac6454SAndrew Thompson static int	zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
16002ac6454SAndrew Thompson static int	zyd_gct_init(struct zyd_rf *);
16102ac6454SAndrew Thompson static int	zyd_gct_switch_radio(struct zyd_rf *, int);
16202ac6454SAndrew Thompson static int	zyd_gct_set_channel(struct zyd_rf *, uint8_t);
16302ac6454SAndrew Thompson static int	zyd_maxim_init(struct zyd_rf *);
16402ac6454SAndrew Thompson static int	zyd_maxim_switch_radio(struct zyd_rf *, int);
16502ac6454SAndrew Thompson static int	zyd_maxim_set_channel(struct zyd_rf *, uint8_t);
16602ac6454SAndrew Thompson static int	zyd_maxim2_init(struct zyd_rf *);
16702ac6454SAndrew Thompson static int	zyd_maxim2_switch_radio(struct zyd_rf *, int);
16802ac6454SAndrew Thompson static int	zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
16902ac6454SAndrew Thompson static void	zyd_queue_command(struct zyd_softc *, usb2_proc_callback_t *,
17002ac6454SAndrew Thompson 		    struct usb2_proc_msg *, struct usb2_proc_msg *);
17102ac6454SAndrew Thompson 
17202ac6454SAndrew Thompson static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
17302ac6454SAndrew Thompson static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
17402ac6454SAndrew Thompson 
17502ac6454SAndrew Thompson /* various supported device vendors/products */
17602ac6454SAndrew Thompson #define ZYD_ZD1211	0
17702ac6454SAndrew Thompson #define ZYD_ZD1211B	1
17802ac6454SAndrew Thompson 
17902ac6454SAndrew Thompson static const struct usb2_device_id zyd_devs[] = {
18002ac6454SAndrew Thompson     /* ZYD_ZD1211 */
18102ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_3COM2, USB_PRODUCT_3COM2_3CRUSB10075, ZYD_ZD1211)},
18202ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_WL54, ZYD_ZD1211)},
18302ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_WL159G, ZYD_ZD1211)},
18402ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_CYBERTAN, USB_PRODUCT_CYBERTAN_TG54USB, ZYD_ZD1211)},
18502ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_DRAYTEK, USB_PRODUCT_DRAYTEK_VIGOR550, ZYD_ZD1211)},
18602ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54GD, ZYD_ZD1211)},
18702ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54GZL, ZYD_ZD1211)},
18802ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GWUS54GZ, ZYD_ZD1211)},
18902ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GWUS54MINI, ZYD_ZD1211)},
19002ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_XG760A, ZYD_ZD1211)},
19102ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_SENAO, USB_PRODUCT_SENAO_NUB8301, ZYD_ZD1211)},
19202ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113, ZYD_ZD1211)},
19302ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_SWEEX, USB_PRODUCT_SWEEX_ZD1211, ZYD_ZD1211)},
19402ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_TEKRAM, USB_PRODUCT_TEKRAM_QUICKWLAN, ZYD_ZD1211)},
19502ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_TEKRAM, USB_PRODUCT_TEKRAM_ZD1211_1, ZYD_ZD1211)},
19602ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_TEKRAM, USB_PRODUCT_TEKRAM_ZD1211_2, ZYD_ZD1211)},
19702ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_TWINMOS, USB_PRODUCT_TWINMOS_G240, ZYD_ZD1211)},
19802ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_UMEDIA, USB_PRODUCT_UMEDIA_ALL0298V2, ZYD_ZD1211)},
19902ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_UMEDIA, USB_PRODUCT_UMEDIA_TEW429UB_A, ZYD_ZD1211)},
20002ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_UMEDIA, USB_PRODUCT_UMEDIA_TEW429UB, ZYD_ZD1211)},
20102ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_WISTRONNEWEB, USB_PRODUCT_WISTRONNEWEB_UR055G, ZYD_ZD1211)},
20202ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_ZD1211, ZYD_ZD1211)},
20302ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ZYDAS, USB_PRODUCT_ZYDAS_ZD1211, ZYD_ZD1211)},
20402ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_AG225H, ZYD_ZD1211)},
20502ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_ZYAIRG220, ZYD_ZD1211)},
20602ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_G200V2, ZYD_ZD1211)},
20702ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_G202, ZYD_ZD1211)},
20802ac6454SAndrew Thompson     /* ZYD_ZD1211B */
20902ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SMCWUSBG, ZYD_ZD1211B)},
21002ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_ZD1211B, ZYD_ZD1211B)},
21102ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_A9T_WIFI, ZYD_ZD1211B)},
21202ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050_V4000, ZYD_ZD1211B)},
21302ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_ZD1211B, ZYD_ZD1211B)},
21402ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSBF54G, ZYD_ZD1211B)},
21502ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_FIBERLINE, USB_PRODUCT_FIBERLINE_WL430U, ZYD_ZD1211B)},
21602ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54L, ZYD_ZD1211B)},
21702ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_PHILIPS, USB_PRODUCT_PHILIPS_SNU5600, ZYD_ZD1211B)},
21802ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GW_US54GXS, ZYD_ZD1211B)},
21902ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_XG76NA, ZYD_ZD1211B)},
22002ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_ZD1211B, ZYD_ZD1211B)},
22102ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_UMEDIA, USB_PRODUCT_UMEDIA_TEW429UBC1, ZYD_ZD1211B)},
22202ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_USR, USB_PRODUCT_USR_USR5423, ZYD_ZD1211B)},
22302ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_VTECH, USB_PRODUCT_VTECH_ZD1211B, ZYD_ZD1211B)},
22402ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_ZD1211B, ZYD_ZD1211B)},
22502ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ZYDAS, USB_PRODUCT_ZYDAS_ZD1211B, ZYD_ZD1211B)},
22602ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_M202, ZYD_ZD1211B)},
22702ac6454SAndrew Thompson     {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_G220V2, ZYD_ZD1211B)},
22802ac6454SAndrew Thompson };
22902ac6454SAndrew Thompson 
23002ac6454SAndrew Thompson static const struct usb2_config zyd_config[ZYD_N_TRANSFER] = {
23102ac6454SAndrew Thompson 	[ZYD_BULK_WR] = {
23202ac6454SAndrew Thompson 		.type = UE_BULK,
23302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
23402ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
23502ac6454SAndrew Thompson 		.mh.bufsize = ZYD_MAX_TXBUFSZ,
23602ac6454SAndrew Thompson 		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
23702ac6454SAndrew Thompson 		.mh.callback = zyd_bulk_write_callback,
23802ac6454SAndrew Thompson 		.ep_index = 0,
23902ac6454SAndrew Thompson 		.mh.timeout = 10000,	/* 10 seconds */
24002ac6454SAndrew Thompson 	},
24102ac6454SAndrew Thompson 	[ZYD_BULK_RD] = {
24202ac6454SAndrew Thompson 		.type = UE_BULK,
24302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
24402ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
24502ac6454SAndrew Thompson 		.mh.bufsize = ZYX_MAX_RXBUFSZ,
24602ac6454SAndrew Thompson 		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
24702ac6454SAndrew Thompson 		.mh.callback = zyd_bulk_read_callback,
24802ac6454SAndrew Thompson 		.ep_index = 0,
24902ac6454SAndrew Thompson 	},
25002ac6454SAndrew Thompson 	[ZYD_INTR_WR] = {
25102ac6454SAndrew Thompson 		.type = UE_BULK_INTR,
25202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
25302ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
25402ac6454SAndrew Thompson 		.mh.bufsize = sizeof(struct zyd_cmd),
25502ac6454SAndrew Thompson 		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
25602ac6454SAndrew Thompson 		.mh.callback = zyd_intr_write_callback,
25702ac6454SAndrew Thompson 		.mh.timeout = 1000,	/* 1 second */
25802ac6454SAndrew Thompson 		.ep_index = 1,
25902ac6454SAndrew Thompson 	},
26002ac6454SAndrew Thompson 	[ZYD_INTR_RD] = {
26102ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
26202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
26302ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
26402ac6454SAndrew Thompson 		.mh.bufsize = sizeof(struct zyd_cmd),
26502ac6454SAndrew Thompson 		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
26602ac6454SAndrew Thompson 		.mh.callback = zyd_intr_read_callback,
26702ac6454SAndrew Thompson 	},
26802ac6454SAndrew Thompson };
26902ac6454SAndrew Thompson #define zyd_read16_m(sc, val, data)	do {				\
27002ac6454SAndrew Thompson 	error = zyd_read16(sc, val, data);				\
27102ac6454SAndrew Thompson 	if (error != 0)							\
27202ac6454SAndrew Thompson 		goto fail;						\
27302ac6454SAndrew Thompson } while (0)
27402ac6454SAndrew Thompson #define zyd_write16_m(sc, val, data)	do {				\
27502ac6454SAndrew Thompson 	error = zyd_write16(sc, val, data);				\
27602ac6454SAndrew Thompson 	if (error != 0)							\
27702ac6454SAndrew Thompson 		goto fail;						\
27802ac6454SAndrew Thompson } while (0)
27902ac6454SAndrew Thompson #define zyd_read32_m(sc, val, data)	do {				\
28002ac6454SAndrew Thompson 	error = zyd_read32(sc, val, data);				\
28102ac6454SAndrew Thompson 	if (error != 0)							\
28202ac6454SAndrew Thompson 		goto fail;						\
28302ac6454SAndrew Thompson } while (0)
28402ac6454SAndrew Thompson #define zyd_write32_m(sc, val, data)	do {				\
28502ac6454SAndrew Thompson 	error = zyd_write32(sc, val, data);				\
28602ac6454SAndrew Thompson 	if (error != 0)							\
28702ac6454SAndrew Thompson 		goto fail;						\
28802ac6454SAndrew Thompson } while (0)
28902ac6454SAndrew Thompson 
29002ac6454SAndrew Thompson static int
29102ac6454SAndrew Thompson zyd_match(device_t dev)
29202ac6454SAndrew Thompson {
29302ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
29402ac6454SAndrew Thompson 
29502ac6454SAndrew Thompson 	if (uaa->usb2_mode != USB_MODE_HOST)
29602ac6454SAndrew Thompson 		return (ENXIO);
29702ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX)
29802ac6454SAndrew Thompson 		return (ENXIO);
29902ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX)
30002ac6454SAndrew Thompson 		return (ENXIO);
30102ac6454SAndrew Thompson 
30202ac6454SAndrew Thompson 	return (usb2_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa));
30302ac6454SAndrew Thompson }
30402ac6454SAndrew Thompson 
30502ac6454SAndrew Thompson static int
30602ac6454SAndrew Thompson zyd_attach(device_t dev)
30702ac6454SAndrew Thompson {
30802ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
30902ac6454SAndrew Thompson 	struct zyd_softc *sc = device_get_softc(dev);
31002ac6454SAndrew Thompson 	int error;
31102ac6454SAndrew Thompson 	uint8_t iface_index;
31202ac6454SAndrew Thompson 
31302ac6454SAndrew Thompson 	if (uaa->info.bcdDevice < 0x4330) {
31402ac6454SAndrew Thompson 		device_printf(dev, "device version mismatch: 0x%X "
31502ac6454SAndrew Thompson 		    "(only >= 43.30 supported)\n",
31602ac6454SAndrew Thompson 		    uaa->info.bcdDevice);
31702ac6454SAndrew Thompson 		return (EINVAL);
31802ac6454SAndrew Thompson 	}
31902ac6454SAndrew Thompson 
32002ac6454SAndrew Thompson 	device_set_usb2_desc(dev);
32102ac6454SAndrew Thompson 	sc->sc_dev = dev;
32202ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
32302ac6454SAndrew Thompson 	sc->sc_macrev = USB_GET_DRIVER_INFO(uaa);
32402ac6454SAndrew Thompson 
32502ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
32602ac6454SAndrew Thompson 	    MTX_NETWORK_LOCK, MTX_DEF);
32777ddf3d3SAndrew Thompson 	cv_init(&sc->sc_cmd_cv, "wtxdone");
32802ac6454SAndrew Thompson 	STAILQ_INIT(&sc->sc_rqh);
32902ac6454SAndrew Thompson 
33002ac6454SAndrew Thompson 	iface_index = ZYD_IFACE_INDEX;
33102ac6454SAndrew Thompson 	error = usb2_transfer_setup(uaa->device,
33202ac6454SAndrew Thompson 	    &iface_index, sc->sc_xfer, zyd_config,
33302ac6454SAndrew Thompson 	    ZYD_N_TRANSFER, sc, &sc->sc_mtx);
33402ac6454SAndrew Thompson 	if (error) {
33502ac6454SAndrew Thompson 		device_printf(dev, "could not allocate USB transfers, "
33602ac6454SAndrew Thompson 		    "err=%s\n", usb2_errstr(error));
33702ac6454SAndrew Thompson 		goto detach;
33802ac6454SAndrew Thompson 	}
33902ac6454SAndrew Thompson 	error = usb2_proc_create(&sc->sc_tq, &sc->sc_mtx,
34002ac6454SAndrew Thompson 	    device_get_nameunit(dev), USB_PRI_MED);
34102ac6454SAndrew Thompson 	if (error) {
34202ac6454SAndrew Thompson 		device_printf(dev, "could not setup config thread!\n");
34302ac6454SAndrew Thompson 		goto detach;
34402ac6454SAndrew Thompson 	}
34502ac6454SAndrew Thompson 
34602ac6454SAndrew Thompson 	/* fork rest of the attach code */
34702ac6454SAndrew Thompson 	ZYD_LOCK(sc);
34802ac6454SAndrew Thompson 	zyd_queue_command(sc, zyd_attach_post,
34902ac6454SAndrew Thompson 	    &sc->sc_synctask[0].hdr,
35002ac6454SAndrew Thompson 	    &sc->sc_synctask[1].hdr);
35102ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
35202ac6454SAndrew Thompson 	return (0);
35302ac6454SAndrew Thompson 
35402ac6454SAndrew Thompson detach:
35502ac6454SAndrew Thompson 	zyd_detach(dev);
35602ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
35702ac6454SAndrew Thompson }
35802ac6454SAndrew Thompson 
35902ac6454SAndrew Thompson static void
36002ac6454SAndrew Thompson zyd_attach_post(struct usb2_proc_msg *pm)
36102ac6454SAndrew Thompson {
36202ac6454SAndrew Thompson 	struct zyd_task *task = (struct zyd_task *)pm;
36302ac6454SAndrew Thompson 	struct zyd_softc *sc = task->sc;
36402ac6454SAndrew Thompson 	struct ifnet *ifp;
36502ac6454SAndrew Thompson 	struct ieee80211com *ic;
36602ac6454SAndrew Thompson 	int error;
36702ac6454SAndrew Thompson 	uint8_t bands;
36802ac6454SAndrew Thompson 
36902ac6454SAndrew Thompson 	if ((error = zyd_get_macaddr(sc)) != 0) {
37002ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM\n");
37102ac6454SAndrew Thompson 		return;
37202ac6454SAndrew Thompson 	}
37302ac6454SAndrew Thompson 
37477ddf3d3SAndrew Thompson 	/* XXX Async attach race */
37577ddf3d3SAndrew Thompson 	if (usb2_proc_is_gone(&sc->sc_tq))
37677ddf3d3SAndrew Thompson 		return;
37777ddf3d3SAndrew Thompson 
37802ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
37902ac6454SAndrew Thompson 
38002ac6454SAndrew Thompson 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
38102ac6454SAndrew Thompson 	if (ifp == NULL) {
38202ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "can not if_alloc()\n");
38302ac6454SAndrew Thompson 		ZYD_LOCK(sc);
38402ac6454SAndrew Thompson 		return;
38502ac6454SAndrew Thompson 	}
38602ac6454SAndrew Thompson 	ifp->if_softc = sc;
38702ac6454SAndrew Thompson 	if_initname(ifp, "zyd", device_get_unit(sc->sc_dev));
38802ac6454SAndrew Thompson 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
38902ac6454SAndrew Thompson 	ifp->if_init = zyd_init;
39002ac6454SAndrew Thompson 	ifp->if_ioctl = zyd_ioctl;
39102ac6454SAndrew Thompson 	ifp->if_start = zyd_start;
39202ac6454SAndrew Thompson 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
39302ac6454SAndrew Thompson 	IFQ_SET_READY(&ifp->if_snd);
39402ac6454SAndrew Thompson 
39502ac6454SAndrew Thompson 	ic = ifp->if_l2com;
39602ac6454SAndrew Thompson 	ic->ic_ifp = ifp;
39702ac6454SAndrew Thompson 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
39802ac6454SAndrew Thompson 	ic->ic_opmode = IEEE80211_M_STA;
39902ac6454SAndrew Thompson 
40002ac6454SAndrew Thompson 	/* set device capabilities */
40102ac6454SAndrew Thompson 	ic->ic_caps =
40202ac6454SAndrew Thompson 		  IEEE80211_C_STA		/* station mode */
40302ac6454SAndrew Thompson 		| IEEE80211_C_MONITOR		/* monitor mode */
40402ac6454SAndrew Thompson 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
40502ac6454SAndrew Thompson 	        | IEEE80211_C_SHSLOT		/* short slot time supported */
40602ac6454SAndrew Thompson 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
40702ac6454SAndrew Thompson 	        | IEEE80211_C_WPA		/* 802.11i */
40802ac6454SAndrew Thompson 		;
40902ac6454SAndrew Thompson 
41002ac6454SAndrew Thompson 	bands = 0;
41102ac6454SAndrew Thompson 	setbit(&bands, IEEE80211_MODE_11B);
41202ac6454SAndrew Thompson 	setbit(&bands, IEEE80211_MODE_11G);
41302ac6454SAndrew Thompson 	ieee80211_init_channels(ic, NULL, &bands);
41402ac6454SAndrew Thompson 
41529aca940SSam Leffler 	ieee80211_ifattach(ic, sc->sc_bssid);
41602ac6454SAndrew Thompson 	ic->ic_newassoc = zyd_newassoc;
41702ac6454SAndrew Thompson 	ic->ic_raw_xmit = zyd_raw_xmit;
41802ac6454SAndrew Thompson 	ic->ic_node_alloc = zyd_node_alloc;
41902ac6454SAndrew Thompson 	ic->ic_scan_start = zyd_scan_start;
42002ac6454SAndrew Thompson 	ic->ic_scan_end = zyd_scan_end;
42102ac6454SAndrew Thompson 	ic->ic_set_channel = zyd_set_channel;
42202ac6454SAndrew Thompson 
42302ac6454SAndrew Thompson 	ic->ic_vap_create = zyd_vap_create;
42402ac6454SAndrew Thompson 	ic->ic_vap_delete = zyd_vap_delete;
42502ac6454SAndrew Thompson 	ic->ic_update_mcast = zyd_update_mcast;
42602ac6454SAndrew Thompson 	ic->ic_update_promisc = zyd_update_mcast;
42702ac6454SAndrew Thompson 
42802ac6454SAndrew Thompson 	bpfattach(ifp, DLT_IEEE802_11_RADIO,
42902ac6454SAndrew Thompson 	    sizeof(struct ieee80211_frame) + sizeof(sc->sc_txtap));
43002ac6454SAndrew Thompson 	sc->sc_rxtap_len = sizeof(sc->sc_rxtap);
43102ac6454SAndrew Thompson 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
43202ac6454SAndrew Thompson 	sc->sc_rxtap.wr_ihdr.it_present = htole32(ZYD_RX_RADIOTAP_PRESENT);
43302ac6454SAndrew Thompson 	sc->sc_txtap_len = sizeof(sc->sc_txtap);
43402ac6454SAndrew Thompson 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
43502ac6454SAndrew Thompson 	sc->sc_txtap.wt_ihdr.it_present = htole32(ZYD_TX_RADIOTAP_PRESENT);
43602ac6454SAndrew Thompson 
43702ac6454SAndrew Thompson 	if (bootverbose)
43802ac6454SAndrew Thompson 		ieee80211_announce(ic);
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 	ZYD_LOCK(sc);
44102ac6454SAndrew Thompson }
44202ac6454SAndrew Thompson 
44302ac6454SAndrew Thompson static int
44402ac6454SAndrew Thompson zyd_detach(device_t dev)
44502ac6454SAndrew Thompson {
44602ac6454SAndrew Thompson 	struct zyd_softc *sc = device_get_softc(dev);
44702ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
44802ac6454SAndrew Thompson 	struct ieee80211com *ic;
44902ac6454SAndrew Thompson 
45002ac6454SAndrew Thompson 	/* wait for any post attach or other command to complete */
45102ac6454SAndrew Thompson 	usb2_proc_drain(&sc->sc_tq);
45202ac6454SAndrew Thompson 
45302ac6454SAndrew Thompson 	/* stop all USB transfers */
45402ac6454SAndrew Thompson 	usb2_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER);
45502ac6454SAndrew Thompson 	usb2_proc_free(&sc->sc_tq);
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson 	/* free TX list, if any */
45802ac6454SAndrew Thompson 	zyd_unsetup_tx_list(sc);
45902ac6454SAndrew Thompson 
46002ac6454SAndrew Thompson 	if (ifp) {
46102ac6454SAndrew Thompson 		ic = ifp->if_l2com;
46202ac6454SAndrew Thompson 		bpfdetach(ifp);
46302ac6454SAndrew Thompson 		ieee80211_ifdetach(ic);
46402ac6454SAndrew Thompson 		if_free(ifp);
46502ac6454SAndrew Thompson 	}
46677ddf3d3SAndrew Thompson 	cv_destroy(&sc->sc_cmd_cv);
46702ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
46802ac6454SAndrew Thompson 
46902ac6454SAndrew Thompson 	return (0);
47002ac6454SAndrew Thompson }
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson static struct ieee80211vap *
47302ac6454SAndrew Thompson zyd_vap_create(struct ieee80211com *ic,
47402ac6454SAndrew Thompson 	const char name[IFNAMSIZ], int unit, int opmode, int flags,
47502ac6454SAndrew Thompson 	const uint8_t bssid[IEEE80211_ADDR_LEN],
47602ac6454SAndrew Thompson 	const uint8_t mac[IEEE80211_ADDR_LEN])
47702ac6454SAndrew Thompson {
47877ddf3d3SAndrew Thompson 	struct zyd_softc *sc = ic->ic_ifp->if_softc;
47902ac6454SAndrew Thompson 	struct zyd_vap *zvp;
48002ac6454SAndrew Thompson 	struct ieee80211vap *vap;
48102ac6454SAndrew Thompson 
48202ac6454SAndrew Thompson 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
48302ac6454SAndrew Thompson 		return (NULL);
48402ac6454SAndrew Thompson 	zvp = (struct zyd_vap *) malloc(sizeof(struct zyd_vap),
48502ac6454SAndrew Thompson 	    M_80211_VAP, M_NOWAIT | M_ZERO);
48602ac6454SAndrew Thompson 	if (zvp == NULL)
48702ac6454SAndrew Thompson 		return (NULL);
48802ac6454SAndrew Thompson 	vap = &zvp->vap;
48902ac6454SAndrew Thompson 	/* enable s/w bmiss handling for sta mode */
49002ac6454SAndrew Thompson 	ieee80211_vap_setup(ic, vap, name, unit, opmode,
49102ac6454SAndrew Thompson 	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
49202ac6454SAndrew Thompson 
49302ac6454SAndrew Thompson 	/* override state transition machine */
49402ac6454SAndrew Thompson 	zvp->newstate = vap->iv_newstate;
49502ac6454SAndrew Thompson 	vap->iv_newstate = zyd_newstate;
49602ac6454SAndrew Thompson 
49777ddf3d3SAndrew Thompson 	zvp->sc = sc;
49802ac6454SAndrew Thompson 	ieee80211_amrr_init(&zvp->amrr, vap,
49902ac6454SAndrew Thompson 	    IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
50002ac6454SAndrew Thompson 	    IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD,
50102ac6454SAndrew Thompson 	    1000 /* 1 sec */);
50202ac6454SAndrew Thompson 
50302ac6454SAndrew Thompson 	/* complete setup */
50402ac6454SAndrew Thompson 	ieee80211_vap_attach(vap, ieee80211_media_change,
50502ac6454SAndrew Thompson 	    ieee80211_media_status);
50602ac6454SAndrew Thompson 	ic->ic_opmode = opmode;
50702ac6454SAndrew Thompson 	return (vap);
50802ac6454SAndrew Thompson }
50902ac6454SAndrew Thompson 
51002ac6454SAndrew Thompson static void
51177ddf3d3SAndrew Thompson zyd_flush_task(struct usb2_proc_msg *_pm)
51277ddf3d3SAndrew Thompson {
51377ddf3d3SAndrew Thompson 	/* nothing to do */
51477ddf3d3SAndrew Thompson }
51577ddf3d3SAndrew Thompson 
51677ddf3d3SAndrew Thompson static void
51702ac6454SAndrew Thompson zyd_vap_delete(struct ieee80211vap *vap)
51802ac6454SAndrew Thompson {
51902ac6454SAndrew Thompson 	struct zyd_vap *zvp = ZYD_VAP(vap);
52077ddf3d3SAndrew Thompson 	struct zyd_softc *sc = zvp->sc;
52177ddf3d3SAndrew Thompson 
52277ddf3d3SAndrew Thompson 	ZYD_LOCK(sc);
52377ddf3d3SAndrew Thompson 	/* wait for any pending tasks to complete */
52477ddf3d3SAndrew Thompson 	zyd_queue_command(sc, zyd_flush_task,
52577ddf3d3SAndrew Thompson 	   &sc->sc_synctask[0].hdr,
52677ddf3d3SAndrew Thompson 	   &sc->sc_synctask[1].hdr);
52777ddf3d3SAndrew Thompson 	ZYD_UNLOCK(sc);
52802ac6454SAndrew Thompson 
52902ac6454SAndrew Thompson 	ieee80211_amrr_cleanup(&zvp->amrr);
53002ac6454SAndrew Thompson 	ieee80211_vap_detach(vap);
53102ac6454SAndrew Thompson 	free(zvp, M_80211_VAP);
53202ac6454SAndrew Thompson }
53302ac6454SAndrew Thompson 
53402ac6454SAndrew Thompson static void
53502ac6454SAndrew Thompson zyd_tx_free(struct zyd_tx_data *data, int txerr)
53602ac6454SAndrew Thompson {
53702ac6454SAndrew Thompson 	struct zyd_softc *sc = data->sc;
53802ac6454SAndrew Thompson 
53902ac6454SAndrew Thompson 	if (data->m != NULL) {
54002ac6454SAndrew Thompson 		if (data->m->m_flags & M_TXCB)
54102ac6454SAndrew Thompson 			ieee80211_process_callback(data->ni, data->m,
54202ac6454SAndrew Thompson 			    txerr ? ETIMEDOUT : 0);
54302ac6454SAndrew Thompson 		m_freem(data->m);
54402ac6454SAndrew Thompson 		data->m = NULL;
54502ac6454SAndrew Thompson 
54602ac6454SAndrew Thompson 		ieee80211_free_node(data->ni);
54702ac6454SAndrew Thompson 		data->ni = NULL;
54802ac6454SAndrew Thompson 	}
54902ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
55002ac6454SAndrew Thompson 	sc->tx_nfree++;
55102ac6454SAndrew Thompson }
55202ac6454SAndrew Thompson 
55302ac6454SAndrew Thompson static void
55402ac6454SAndrew Thompson zyd_setup_tx_list(struct zyd_softc *sc)
55502ac6454SAndrew Thompson {
55602ac6454SAndrew Thompson 	struct zyd_tx_data *data;
55702ac6454SAndrew Thompson 	int i;
55802ac6454SAndrew Thompson 
55902ac6454SAndrew Thompson 	sc->tx_nfree = 0;
56002ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
56102ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
56202ac6454SAndrew Thompson 
56302ac6454SAndrew Thompson 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
56402ac6454SAndrew Thompson 		data = &sc->tx_data[i];
56502ac6454SAndrew Thompson 
56602ac6454SAndrew Thompson 		data->sc = sc;
56702ac6454SAndrew Thompson 		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
56802ac6454SAndrew Thompson 		sc->tx_nfree++;
56902ac6454SAndrew Thompson 	}
57002ac6454SAndrew Thompson }
57102ac6454SAndrew Thompson 
57202ac6454SAndrew Thompson static void
57302ac6454SAndrew Thompson zyd_unsetup_tx_list(struct zyd_softc *sc)
57402ac6454SAndrew Thompson {
57502ac6454SAndrew Thompson 	struct zyd_tx_data *data;
57602ac6454SAndrew Thompson 	int i;
57702ac6454SAndrew Thompson 
57802ac6454SAndrew Thompson 	/* make sure any subsequent use of the queues will fail */
57902ac6454SAndrew Thompson 	sc->tx_nfree = 0;
58002ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
58102ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
58202ac6454SAndrew Thompson 
58302ac6454SAndrew Thompson 	/* free up all node references and mbufs */
58402ac6454SAndrew Thompson 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
58502ac6454SAndrew Thompson 		data = &sc->tx_data[i];
58602ac6454SAndrew Thompson 
58702ac6454SAndrew Thompson 		if (data->m != NULL) {
58802ac6454SAndrew Thompson 			m_freem(data->m);
58902ac6454SAndrew Thompson 			data->m = NULL;
59002ac6454SAndrew Thompson 		}
59102ac6454SAndrew Thompson 		if (data->ni != NULL) {
59202ac6454SAndrew Thompson 			ieee80211_free_node(data->ni);
59302ac6454SAndrew Thompson 			data->ni = NULL;
59402ac6454SAndrew Thompson 		}
59502ac6454SAndrew Thompson 	}
59602ac6454SAndrew Thompson }
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson /* ARGUSED */
59902ac6454SAndrew Thompson static struct ieee80211_node *
60002ac6454SAndrew Thompson zyd_node_alloc(struct ieee80211vap *vap __unused,
60102ac6454SAndrew Thompson 	const uint8_t mac[IEEE80211_ADDR_LEN] __unused)
60202ac6454SAndrew Thompson {
60302ac6454SAndrew Thompson 	struct zyd_node *zn;
60402ac6454SAndrew Thompson 
60502ac6454SAndrew Thompson 	zn = malloc(sizeof(struct zyd_node), M_80211_NODE, M_NOWAIT | M_ZERO);
60602ac6454SAndrew Thompson 	return (zn != NULL) ? (&zn->ni) : (NULL);
60702ac6454SAndrew Thompson }
60802ac6454SAndrew Thompson 
60902ac6454SAndrew Thompson static void
61002ac6454SAndrew Thompson zyd_task(struct usb2_proc_msg *pm)
61102ac6454SAndrew Thompson {
61202ac6454SAndrew Thompson 	struct zyd_task *task = (struct zyd_task *)pm;
61302ac6454SAndrew Thompson 	struct zyd_softc *sc = task->sc;
61402ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
61502ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
61602ac6454SAndrew Thompson 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
61702ac6454SAndrew Thompson 	struct ieee80211_node *ni = vap->iv_bss;
61802ac6454SAndrew Thompson 	struct zyd_vap *zvp = ZYD_VAP(vap);
61902ac6454SAndrew Thompson 	int error;
62002ac6454SAndrew Thompson 
62102ac6454SAndrew Thompson 	switch (sc->sc_state) {
62202ac6454SAndrew Thompson 	case IEEE80211_S_AUTH:
62302ac6454SAndrew Thompson 		zyd_set_chan(sc, ic->ic_curchan);
62402ac6454SAndrew Thompson 		break;
62502ac6454SAndrew Thompson 	case IEEE80211_S_RUN:
62602ac6454SAndrew Thompson 		if (vap->iv_opmode == IEEE80211_M_MONITOR)
62702ac6454SAndrew Thompson 			break;
62802ac6454SAndrew Thompson 
62902ac6454SAndrew Thompson 		/* turn link LED on */
63002ac6454SAndrew Thompson 		error = zyd_set_led(sc, ZYD_LED1, 1);
63102ac6454SAndrew Thompson 		if (error != 0)
63202ac6454SAndrew Thompson 			goto fail;
63302ac6454SAndrew Thompson 
63402ac6454SAndrew Thompson 		/* make data LED blink upon Tx */
63502ac6454SAndrew Thompson 		zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1);
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 		IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
63802ac6454SAndrew Thompson 		zyd_set_bssid(sc, sc->sc_bssid);
63902ac6454SAndrew Thompson 		break;
64002ac6454SAndrew Thompson 	default:
64102ac6454SAndrew Thompson 		break;
64202ac6454SAndrew Thompson 	}
64302ac6454SAndrew Thompson fail:
64402ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
64502ac6454SAndrew Thompson 	IEEE80211_LOCK(ic);
64602ac6454SAndrew Thompson 	zvp->newstate(vap, sc->sc_state, sc->sc_arg);
64702ac6454SAndrew Thompson 	if (vap->iv_newstate_cb != NULL)
64802ac6454SAndrew Thompson 		vap->iv_newstate_cb(vap, sc->sc_state, sc->sc_arg);
64902ac6454SAndrew Thompson 	IEEE80211_UNLOCK(ic);
65002ac6454SAndrew Thompson 	ZYD_LOCK(sc);
65102ac6454SAndrew Thompson }
65202ac6454SAndrew Thompson 
65302ac6454SAndrew Thompson static int
65402ac6454SAndrew Thompson zyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
65502ac6454SAndrew Thompson {
65602ac6454SAndrew Thompson 	struct zyd_vap *zvp = ZYD_VAP(vap);
65702ac6454SAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
65802ac6454SAndrew Thompson 	struct zyd_softc *sc = ic->ic_ifp->if_softc;
65902ac6454SAndrew Thompson 
66002ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__,
66102ac6454SAndrew Thompson 	    ieee80211_state_name[vap->iv_state],
66202ac6454SAndrew Thompson 	    ieee80211_state_name[nstate]);
66302ac6454SAndrew Thompson 
66402ac6454SAndrew Thompson 	ZYD_LOCK(sc);
66502ac6454SAndrew Thompson 	/* do it in a process context */
66602ac6454SAndrew Thompson 	sc->sc_state = nstate;
66702ac6454SAndrew Thompson 	sc->sc_arg = arg;
66802ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
66902ac6454SAndrew Thompson 
67002ac6454SAndrew Thompson 	if (nstate == IEEE80211_S_INIT) {
67102ac6454SAndrew Thompson 		zvp->newstate(vap, nstate, arg);
67202ac6454SAndrew Thompson 		return (0);
67302ac6454SAndrew Thompson 	} else {
67402ac6454SAndrew Thompson 		ZYD_LOCK(sc);
67502ac6454SAndrew Thompson 		zyd_queue_command(sc, zyd_task, &sc->sc_task[0].hdr,
67602ac6454SAndrew Thompson 		    &sc->sc_task[1].hdr);
67702ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
67802ac6454SAndrew Thompson 		return (EINPROGRESS);
67902ac6454SAndrew Thompson 	}
68002ac6454SAndrew Thompson }
68102ac6454SAndrew Thompson 
68202ac6454SAndrew Thompson /*
68302ac6454SAndrew Thompson  * Callback handler for interrupt transfer
68402ac6454SAndrew Thompson  */
68502ac6454SAndrew Thompson static void
68602ac6454SAndrew Thompson zyd_intr_read_callback(struct usb2_xfer *xfer)
68702ac6454SAndrew Thompson {
68802ac6454SAndrew Thompson 	struct zyd_softc *sc = xfer->priv_sc;
68902ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
69002ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
69102ac6454SAndrew Thompson 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
69202ac6454SAndrew Thompson 	struct ieee80211_node *ni;
69302ac6454SAndrew Thompson 	struct zyd_cmd *cmd = &sc->sc_ibuf;
69402ac6454SAndrew Thompson 	int datalen;
69502ac6454SAndrew Thompson 
69602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
69702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
69802ac6454SAndrew Thompson 		usb2_copy_out(xfer->frbuffers, 0, cmd, sizeof(*cmd));
69902ac6454SAndrew Thompson 
70002ac6454SAndrew Thompson 		switch (le16toh(cmd->code)) {
70102ac6454SAndrew Thompson 		case ZYD_NOTIF_RETRYSTATUS:
70202ac6454SAndrew Thompson 		{
70302ac6454SAndrew Thompson 			struct zyd_notif_retry *retry =
70402ac6454SAndrew Thompson 			    (struct zyd_notif_retry *)cmd->data;
70502ac6454SAndrew Thompson 
70602ac6454SAndrew Thompson 			DPRINTF(sc, ZYD_DEBUG_TX_PROC,
70702ac6454SAndrew Thompson 			    "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
70802ac6454SAndrew Thompson 			    le16toh(retry->rate), ether_sprintf(retry->macaddr),
70902ac6454SAndrew Thompson 			    le16toh(retry->count)&0xff, le16toh(retry->count));
71002ac6454SAndrew Thompson 
71102ac6454SAndrew Thompson 			/*
71202ac6454SAndrew Thompson 			 * Find the node to which the packet was sent and
71302ac6454SAndrew Thompson 			 * update its retry statistics.  In BSS mode, this node
71402ac6454SAndrew Thompson 			 * is the AP we're associated to so no lookup is
71502ac6454SAndrew Thompson 			 * actually needed.
71602ac6454SAndrew Thompson 			 */
71702ac6454SAndrew Thompson 			ni = ieee80211_find_txnode(vap, retry->macaddr);
71802ac6454SAndrew Thompson 			if (ni != NULL) {
71902ac6454SAndrew Thompson 				ieee80211_amrr_tx_complete(&ZYD_NODE(ni)->amn,
72002ac6454SAndrew Thompson 				    IEEE80211_AMRR_FAILURE, 1);
72102ac6454SAndrew Thompson 				ieee80211_free_node(ni);
72202ac6454SAndrew Thompson 			}
72302ac6454SAndrew Thompson 			if (le16toh(retry->count) & 0x100)
72402ac6454SAndrew Thompson 				ifp->if_oerrors++;	/* too many retries */
72502ac6454SAndrew Thompson 			break;
72602ac6454SAndrew Thompson 		}
72702ac6454SAndrew Thompson 		case ZYD_NOTIF_IORD:
72802ac6454SAndrew Thompson 		{
72902ac6454SAndrew Thompson 			struct zyd_rq *rqp;
73002ac6454SAndrew Thompson 
73102ac6454SAndrew Thompson 			if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
73202ac6454SAndrew Thompson 				break;	/* HMAC interrupt */
73302ac6454SAndrew Thompson 
73402ac6454SAndrew Thompson 			datalen = xfer->actlen - sizeof(cmd->code);
73502ac6454SAndrew Thompson 			datalen -= 2;	/* XXX: padding? */
73602ac6454SAndrew Thompson 
73702ac6454SAndrew Thompson 			STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
73802ac6454SAndrew Thompson 				int i, cnt;
73902ac6454SAndrew Thompson 
74002ac6454SAndrew Thompson 				if (rqp->olen != datalen)
74102ac6454SAndrew Thompson 					continue;
74202ac6454SAndrew Thompson 				cnt = rqp->olen / sizeof(struct zyd_pair);
74302ac6454SAndrew Thompson 				for (i = 0; i < cnt; i++) {
74402ac6454SAndrew Thompson 					if (*(((const uint16_t *)rqp->idata) + i) !=
74502ac6454SAndrew Thompson 					    (((struct zyd_pair *)cmd->data) + i)->reg)
74602ac6454SAndrew Thompson 						break;
74702ac6454SAndrew Thompson 				}
74802ac6454SAndrew Thompson 				if (i != cnt)
74902ac6454SAndrew Thompson 					continue;
75002ac6454SAndrew Thompson 				/* copy answer into caller-supplied buffer */
75102ac6454SAndrew Thompson 				bcopy(cmd->data, rqp->odata, rqp->olen);
75202ac6454SAndrew Thompson 				DPRINTF(sc, ZYD_DEBUG_CMD,
75302ac6454SAndrew Thompson 				    "command %p complete, data = %*D \n",
75402ac6454SAndrew Thompson 				    rqp, rqp->olen, rqp->odata, ":");
75502ac6454SAndrew Thompson 				wakeup(rqp);	/* wakeup caller */
75602ac6454SAndrew Thompson 				break;
75702ac6454SAndrew Thompson 			}
75802ac6454SAndrew Thompson 			if (rqp == NULL) {
75902ac6454SAndrew Thompson 				device_printf(sc->sc_dev,
76002ac6454SAndrew Thompson 				    "unexpected IORD notification %*D\n",
76102ac6454SAndrew Thompson 				    datalen, cmd->data, ":");
76202ac6454SAndrew Thompson 			}
76302ac6454SAndrew Thompson 			break;
76402ac6454SAndrew Thompson 		}
76502ac6454SAndrew Thompson 		default:
76602ac6454SAndrew Thompson 			device_printf(sc->sc_dev, "unknown notification %x\n",
76702ac6454SAndrew Thompson 			    le16toh(cmd->code));
76802ac6454SAndrew Thompson 		}
76902ac6454SAndrew Thompson 
77002ac6454SAndrew Thompson 		/* FALLTHROUGH */
77102ac6454SAndrew Thompson 	case USB_ST_SETUP:
77202ac6454SAndrew Thompson tr_setup:
77302ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
77402ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
77502ac6454SAndrew Thompson 		break;
77602ac6454SAndrew Thompson 
77702ac6454SAndrew Thompson 	default:			/* Error */
77802ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
77902ac6454SAndrew Thompson 		    usb2_errstr(xfer->error));
78002ac6454SAndrew Thompson 
78102ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
78202ac6454SAndrew Thompson 			/* try to clear stall first */
78302ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
78402ac6454SAndrew Thompson 			goto tr_setup;
78502ac6454SAndrew Thompson 		}
78602ac6454SAndrew Thompson 		break;
78702ac6454SAndrew Thompson 	}
78802ac6454SAndrew Thompson }
78902ac6454SAndrew Thompson 
79002ac6454SAndrew Thompson static void
79102ac6454SAndrew Thompson zyd_intr_write_callback(struct usb2_xfer *xfer)
79202ac6454SAndrew Thompson {
79302ac6454SAndrew Thompson 	struct zyd_softc *sc = xfer->priv_sc;
79402ac6454SAndrew Thompson 	struct zyd_rq *rqp;
79502ac6454SAndrew Thompson 
79602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
79702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
798d953f720SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n",
799d953f720SAndrew Thompson 		    xfer->priv_fifo);
800d953f720SAndrew Thompson 		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
801d953f720SAndrew Thompson 			/* Ensure the cached rq pointer is still valid */
802d953f720SAndrew Thompson 			if (rqp == xfer->priv_fifo &&
803d953f720SAndrew Thompson 			    (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
80402ac6454SAndrew Thompson 				wakeup(rqp);	/* wakeup caller */
805d953f720SAndrew Thompson 		}
80602ac6454SAndrew Thompson 
80702ac6454SAndrew Thompson 		/* FALLTHROUGH */
80802ac6454SAndrew Thompson 	case USB_ST_SETUP:
80902ac6454SAndrew Thompson tr_setup:
81002ac6454SAndrew Thompson 		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
81102ac6454SAndrew Thompson 			if (rqp->flags & ZYD_CMD_FLAG_SENT)
81202ac6454SAndrew Thompson 				continue;
81302ac6454SAndrew Thompson 
81402ac6454SAndrew Thompson 			usb2_copy_in(xfer->frbuffers, 0, rqp->cmd, rqp->ilen);
81502ac6454SAndrew Thompson 
81602ac6454SAndrew Thompson 			xfer->frlengths[0] = rqp->ilen;
81702ac6454SAndrew Thompson 			xfer->priv_fifo = rqp;
81802ac6454SAndrew Thompson 			rqp->flags |= ZYD_CMD_FLAG_SENT;
81902ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
82002ac6454SAndrew Thompson 			break;
82102ac6454SAndrew Thompson 		}
82202ac6454SAndrew Thompson 		break;
82302ac6454SAndrew Thompson 
82402ac6454SAndrew Thompson 	default:			/* Error */
82502ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
82602ac6454SAndrew Thompson 		    usb2_errstr(xfer->error));
82702ac6454SAndrew Thompson 
82802ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
82902ac6454SAndrew Thompson 			/* try to clear stall first */
83002ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
83102ac6454SAndrew Thompson 			goto tr_setup;
83202ac6454SAndrew Thompson 		}
83302ac6454SAndrew Thompson 		break;
83402ac6454SAndrew Thompson 	}
83502ac6454SAndrew Thompson }
83602ac6454SAndrew Thompson 
83702ac6454SAndrew Thompson static int
83802ac6454SAndrew Thompson zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
83902ac6454SAndrew Thompson     void *odata, int olen, int flags)
84002ac6454SAndrew Thompson {
84102ac6454SAndrew Thompson 	struct zyd_cmd cmd;
84202ac6454SAndrew Thompson 	struct zyd_rq rq;
84302ac6454SAndrew Thompson 	int error;
84402ac6454SAndrew Thompson 
84502ac6454SAndrew Thompson 	if (ilen > sizeof(cmd.data))
84602ac6454SAndrew Thompson 		return (EINVAL);
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson 	if (usb2_proc_is_gone(&sc->sc_tq))
84902ac6454SAndrew Thompson 		return (ENXIO);
85002ac6454SAndrew Thompson 
85102ac6454SAndrew Thompson 	cmd.code = htole16(code);
85202ac6454SAndrew Thompson 	bcopy(idata, cmd.data, ilen);
85302ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
85402ac6454SAndrew Thompson 	    &rq, ilen, idata, ":");
85502ac6454SAndrew Thompson 
85602ac6454SAndrew Thompson 	rq.cmd = &cmd;
85702ac6454SAndrew Thompson 	rq.idata = idata;
85802ac6454SAndrew Thompson 	rq.odata = odata;
85902ac6454SAndrew Thompson 	rq.ilen = sizeof(uint16_t) + ilen;
86002ac6454SAndrew Thompson 	rq.olen = olen;
86102ac6454SAndrew Thompson 	rq.flags = flags;
86202ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
86302ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
86402ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
86502ac6454SAndrew Thompson 
86602ac6454SAndrew Thompson 	/* wait at most one second for command reply */
86702ac6454SAndrew Thompson 	error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
86802ac6454SAndrew Thompson 	if (error)
86902ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "command timeout\n");
87002ac6454SAndrew Thompson 	STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
87102ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
87202ac6454SAndrew Thompson 	    &rq, error);
87302ac6454SAndrew Thompson 
87402ac6454SAndrew Thompson 	return (error);
87502ac6454SAndrew Thompson }
87602ac6454SAndrew Thompson 
87702ac6454SAndrew Thompson static int
87802ac6454SAndrew Thompson zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
87902ac6454SAndrew Thompson {
88002ac6454SAndrew Thompson 	struct zyd_pair tmp;
88102ac6454SAndrew Thompson 	int error;
88202ac6454SAndrew Thompson 
88302ac6454SAndrew Thompson 	reg = htole16(reg);
88402ac6454SAndrew Thompson 	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
88502ac6454SAndrew Thompson 	    ZYD_CMD_FLAG_READ);
88602ac6454SAndrew Thompson 	if (error == 0)
88702ac6454SAndrew Thompson 		*val = le16toh(tmp.val);
88802ac6454SAndrew Thompson 	return (error);
88902ac6454SAndrew Thompson }
89002ac6454SAndrew Thompson 
89102ac6454SAndrew Thompson static int
89202ac6454SAndrew Thompson zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
89302ac6454SAndrew Thompson {
89402ac6454SAndrew Thompson 	struct zyd_pair tmp[2];
89502ac6454SAndrew Thompson 	uint16_t regs[2];
89602ac6454SAndrew Thompson 	int error;
89702ac6454SAndrew Thompson 
89802ac6454SAndrew Thompson 	regs[0] = htole16(ZYD_REG32_HI(reg));
89902ac6454SAndrew Thompson 	regs[1] = htole16(ZYD_REG32_LO(reg));
90002ac6454SAndrew Thompson 	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
90102ac6454SAndrew Thompson 	    ZYD_CMD_FLAG_READ);
90202ac6454SAndrew Thompson 	if (error == 0)
90302ac6454SAndrew Thompson 		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
90402ac6454SAndrew Thompson 	return (error);
90502ac6454SAndrew Thompson }
90602ac6454SAndrew Thompson 
90702ac6454SAndrew Thompson static int
90802ac6454SAndrew Thompson zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
90902ac6454SAndrew Thompson {
91002ac6454SAndrew Thompson 	struct zyd_pair pair;
91102ac6454SAndrew Thompson 
91202ac6454SAndrew Thompson 	pair.reg = htole16(reg);
91302ac6454SAndrew Thompson 	pair.val = htole16(val);
91402ac6454SAndrew Thompson 
91502ac6454SAndrew Thompson 	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
91602ac6454SAndrew Thompson }
91702ac6454SAndrew Thompson 
91802ac6454SAndrew Thompson static int
91902ac6454SAndrew Thompson zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
92002ac6454SAndrew Thompson {
92102ac6454SAndrew Thompson 	struct zyd_pair pair[2];
92202ac6454SAndrew Thompson 
92302ac6454SAndrew Thompson 	pair[0].reg = htole16(ZYD_REG32_HI(reg));
92402ac6454SAndrew Thompson 	pair[0].val = htole16(val >> 16);
92502ac6454SAndrew Thompson 	pair[1].reg = htole16(ZYD_REG32_LO(reg));
92602ac6454SAndrew Thompson 	pair[1].val = htole16(val & 0xffff);
92702ac6454SAndrew Thompson 
92802ac6454SAndrew Thompson 	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
92902ac6454SAndrew Thompson }
93002ac6454SAndrew Thompson 
93102ac6454SAndrew Thompson static int
93202ac6454SAndrew Thompson zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
93302ac6454SAndrew Thompson {
93402ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
93502ac6454SAndrew Thompson 	struct zyd_rfwrite_cmd req;
93602ac6454SAndrew Thompson 	uint16_t cr203;
93702ac6454SAndrew Thompson 	int error, i;
93802ac6454SAndrew Thompson 
93902ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &cr203);
94002ac6454SAndrew Thompson 	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
94102ac6454SAndrew Thompson 
94202ac6454SAndrew Thompson 	req.code  = htole16(2);
94302ac6454SAndrew Thompson 	req.width = htole16(rf->width);
94402ac6454SAndrew Thompson 	for (i = 0; i < rf->width; i++) {
94502ac6454SAndrew Thompson 		req.bit[i] = htole16(cr203);
94602ac6454SAndrew Thompson 		if (val & (1 << (rf->width - 1 - i)))
94702ac6454SAndrew Thompson 			req.bit[i] |= htole16(ZYD_RF_DATA);
94802ac6454SAndrew Thompson 	}
94902ac6454SAndrew Thompson 	error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
95002ac6454SAndrew Thompson fail:
95102ac6454SAndrew Thompson 	return (error);
95202ac6454SAndrew Thompson }
95302ac6454SAndrew Thompson 
95402ac6454SAndrew Thompson static int
95502ac6454SAndrew Thompson zyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
95602ac6454SAndrew Thompson {
95702ac6454SAndrew Thompson 	int error;
95802ac6454SAndrew Thompson 
95902ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
96002ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR243, (val >>  8) & 0xff);
96102ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR242, (val >>  0) & 0xff);
96202ac6454SAndrew Thompson fail:
96302ac6454SAndrew Thompson 	return (error);
96402ac6454SAndrew Thompson }
96502ac6454SAndrew Thompson 
96602ac6454SAndrew Thompson static int
96702ac6454SAndrew Thompson zyd_lock_phy(struct zyd_softc *sc)
96802ac6454SAndrew Thompson {
96902ac6454SAndrew Thompson 	int error;
97002ac6454SAndrew Thompson 	uint32_t tmp;
97102ac6454SAndrew Thompson 
97202ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
97302ac6454SAndrew Thompson 	tmp &= ~ZYD_UNLOCK_PHY_REGS;
97402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
97502ac6454SAndrew Thompson fail:
97602ac6454SAndrew Thompson 	return (error);
97702ac6454SAndrew Thompson }
97802ac6454SAndrew Thompson 
97902ac6454SAndrew Thompson static int
98002ac6454SAndrew Thompson zyd_unlock_phy(struct zyd_softc *sc)
98102ac6454SAndrew Thompson {
98202ac6454SAndrew Thompson 	int error;
98302ac6454SAndrew Thompson 	uint32_t tmp;
98402ac6454SAndrew Thompson 
98502ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
98602ac6454SAndrew Thompson 	tmp |= ZYD_UNLOCK_PHY_REGS;
98702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
98802ac6454SAndrew Thompson fail:
98902ac6454SAndrew Thompson 	return (error);
99002ac6454SAndrew Thompson }
99102ac6454SAndrew Thompson 
99202ac6454SAndrew Thompson /*
99302ac6454SAndrew Thompson  * RFMD RF methods.
99402ac6454SAndrew Thompson  */
99502ac6454SAndrew Thompson static int
99602ac6454SAndrew Thompson zyd_rfmd_init(struct zyd_rf *rf)
99702ac6454SAndrew Thompson {
99802ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
99902ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
100002ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
100102ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_RFMD_RF;
100202ac6454SAndrew Thompson 	int i, error;
100302ac6454SAndrew Thompson 
100402ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
100502ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++) {
100602ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
100702ac6454SAndrew Thompson 	}
100802ac6454SAndrew Thompson 
100902ac6454SAndrew Thompson 	/* init RFMD radio */
101002ac6454SAndrew Thompson 	for (i = 0; i < N(rfini); i++) {
101102ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
101202ac6454SAndrew Thompson 			return (error);
101302ac6454SAndrew Thompson 	}
101402ac6454SAndrew Thompson fail:
101502ac6454SAndrew Thompson 	return (error);
101602ac6454SAndrew Thompson #undef N
101702ac6454SAndrew Thompson }
101802ac6454SAndrew Thompson 
101902ac6454SAndrew Thompson static int
102002ac6454SAndrew Thompson zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
102102ac6454SAndrew Thompson {
102202ac6454SAndrew Thompson 	int error;
102302ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
102402ac6454SAndrew Thompson 
102502ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
102602ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
102702ac6454SAndrew Thompson fail:
102802ac6454SAndrew Thompson 	return (error);
102902ac6454SAndrew Thompson }
103002ac6454SAndrew Thompson 
103102ac6454SAndrew Thompson static int
103202ac6454SAndrew Thompson zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
103302ac6454SAndrew Thompson {
103402ac6454SAndrew Thompson 	int error;
103502ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
103602ac6454SAndrew Thompson 	static const struct {
103702ac6454SAndrew Thompson 		uint32_t	r1, r2;
103802ac6454SAndrew Thompson 	} rfprog[] = ZYD_RFMD_CHANTABLE;
103902ac6454SAndrew Thompson 
104002ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
104102ac6454SAndrew Thompson 	if (error != 0)
104202ac6454SAndrew Thompson 		goto fail;
104302ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
104402ac6454SAndrew Thompson 	if (error != 0)
104502ac6454SAndrew Thompson 		goto fail;
104602ac6454SAndrew Thompson 
104702ac6454SAndrew Thompson fail:
104802ac6454SAndrew Thompson 	return (error);
104902ac6454SAndrew Thompson }
105002ac6454SAndrew Thompson 
105102ac6454SAndrew Thompson /*
105202ac6454SAndrew Thompson  * AL2230 RF methods.
105302ac6454SAndrew Thompson  */
105402ac6454SAndrew Thompson static int
105502ac6454SAndrew Thompson zyd_al2230_init(struct zyd_rf *rf)
105602ac6454SAndrew Thompson {
105702ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
105802ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
105902ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
106002ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
106102ac6454SAndrew Thompson 	static const struct zyd_phy_pair phypll[] = {
106202ac6454SAndrew Thompson 		{ ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
106302ac6454SAndrew Thompson 		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
106402ac6454SAndrew Thompson 	};
106502ac6454SAndrew Thompson 	static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
106602ac6454SAndrew Thompson 	static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
106702ac6454SAndrew Thompson 	static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
106802ac6454SAndrew Thompson 	int i, error;
106902ac6454SAndrew Thompson 
107002ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
107102ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
107202ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
107302ac6454SAndrew Thompson 
107402ac6454SAndrew Thompson 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
107502ac6454SAndrew Thompson 		for (i = 0; i < N(phy2230s); i++)
107602ac6454SAndrew Thompson 			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
107702ac6454SAndrew Thompson 	}
107802ac6454SAndrew Thompson 
107902ac6454SAndrew Thompson 	/* init AL2230 radio */
108002ac6454SAndrew Thompson 	for (i = 0; i < N(rfini1); i++) {
108102ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, rfini1[i]);
108202ac6454SAndrew Thompson 		if (error != 0)
108302ac6454SAndrew Thompson 			goto fail;
108402ac6454SAndrew Thompson 	}
108502ac6454SAndrew Thompson 
108602ac6454SAndrew Thompson 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
108702ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, 0x000824);
108802ac6454SAndrew Thompson 	else
108902ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, 0x0005a4);
109002ac6454SAndrew Thompson 	if (error != 0)
109102ac6454SAndrew Thompson 		goto fail;
109202ac6454SAndrew Thompson 
109302ac6454SAndrew Thompson 	for (i = 0; i < N(rfini2); i++) {
109402ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, rfini2[i]);
109502ac6454SAndrew Thompson 		if (error != 0)
109602ac6454SAndrew Thompson 			goto fail;
109702ac6454SAndrew Thompson 	}
109802ac6454SAndrew Thompson 
109902ac6454SAndrew Thompson 	for (i = 0; i < N(phypll); i++)
110002ac6454SAndrew Thompson 		zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
110102ac6454SAndrew Thompson 
110202ac6454SAndrew Thompson 	for (i = 0; i < N(rfini3); i++) {
110302ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, rfini3[i]);
110402ac6454SAndrew Thompson 		if (error != 0)
110502ac6454SAndrew Thompson 			goto fail;
110602ac6454SAndrew Thompson 	}
110702ac6454SAndrew Thompson fail:
110802ac6454SAndrew Thompson 	return (error);
110902ac6454SAndrew Thompson #undef N
111002ac6454SAndrew Thompson }
111102ac6454SAndrew Thompson 
111202ac6454SAndrew Thompson static int
111302ac6454SAndrew Thompson zyd_al2230_fini(struct zyd_rf *rf)
111402ac6454SAndrew Thompson {
111502ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
111602ac6454SAndrew Thompson 	int error, i;
111702ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
111802ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
111902ac6454SAndrew Thompson 
112002ac6454SAndrew Thompson 	for (i = 0; i < N(phy); i++)
112102ac6454SAndrew Thompson 		zyd_write16_m(sc, phy[i].reg, phy[i].val);
112202ac6454SAndrew Thompson 
112302ac6454SAndrew Thompson 	if (sc->sc_newphy != 0)
112402ac6454SAndrew Thompson 		zyd_write16_m(sc, ZYD_CR9, 0xe1);
112502ac6454SAndrew Thompson 
112602ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, 0x6);
112702ac6454SAndrew Thompson fail:
112802ac6454SAndrew Thompson 	return (error);
112902ac6454SAndrew Thompson #undef N
113002ac6454SAndrew Thompson }
113102ac6454SAndrew Thompson 
113202ac6454SAndrew Thompson static int
113302ac6454SAndrew Thompson zyd_al2230_init_b(struct zyd_rf *rf)
113402ac6454SAndrew Thompson {
113502ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
113602ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
113702ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
113802ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
113902ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
114002ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
114102ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
114202ac6454SAndrew Thompson 	static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
114302ac6454SAndrew Thompson 	static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
114402ac6454SAndrew Thompson 	static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
114502ac6454SAndrew Thompson 	static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
114602ac6454SAndrew Thompson 	int i, error;
114702ac6454SAndrew Thompson 
114802ac6454SAndrew Thompson 	for (i = 0; i < N(phy1); i++)
114902ac6454SAndrew Thompson 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
115002ac6454SAndrew Thompson 
115102ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
115202ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
115302ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
115402ac6454SAndrew Thompson 
115502ac6454SAndrew Thompson 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
115602ac6454SAndrew Thompson 		for (i = 0; i < N(phy2230s); i++)
115702ac6454SAndrew Thompson 			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
115802ac6454SAndrew Thompson 	}
115902ac6454SAndrew Thompson 
116002ac6454SAndrew Thompson 	for (i = 0; i < 3; i++) {
116102ac6454SAndrew Thompson 		error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
116202ac6454SAndrew Thompson 		if (error != 0)
116302ac6454SAndrew Thompson 			return (error);
116402ac6454SAndrew Thompson 	}
116502ac6454SAndrew Thompson 
116602ac6454SAndrew Thompson 	for (i = 0; i < N(rfini_part1); i++) {
116702ac6454SAndrew Thompson 		error = zyd_rfwrite_cr(sc, rfini_part1[i]);
116802ac6454SAndrew Thompson 		if (error != 0)
116902ac6454SAndrew Thompson 			return (error);
117002ac6454SAndrew Thompson 	}
117102ac6454SAndrew Thompson 
117202ac6454SAndrew Thompson 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
117302ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, 0x241000);
117402ac6454SAndrew Thompson 	else
117502ac6454SAndrew Thompson 		error = zyd_rfwrite(sc, 0x25a000);
117602ac6454SAndrew Thompson 	if (error != 0)
117702ac6454SAndrew Thompson 		goto fail;
117802ac6454SAndrew Thompson 
117902ac6454SAndrew Thompson 	for (i = 0; i < N(rfini_part2); i++) {
118002ac6454SAndrew Thompson 		error = zyd_rfwrite_cr(sc, rfini_part2[i]);
118102ac6454SAndrew Thompson 		if (error != 0)
118202ac6454SAndrew Thompson 			return (error);
118302ac6454SAndrew Thompson 	}
118402ac6454SAndrew Thompson 
118502ac6454SAndrew Thompson 	for (i = 0; i < N(phy2); i++)
118602ac6454SAndrew Thompson 		zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
118702ac6454SAndrew Thompson 
118802ac6454SAndrew Thompson 	for (i = 0; i < N(rfini_part3); i++) {
118902ac6454SAndrew Thompson 		error = zyd_rfwrite_cr(sc, rfini_part3[i]);
119002ac6454SAndrew Thompson 		if (error != 0)
119102ac6454SAndrew Thompson 			return (error);
119202ac6454SAndrew Thompson 	}
119302ac6454SAndrew Thompson 
119402ac6454SAndrew Thompson 	for (i = 0; i < N(phy3); i++)
119502ac6454SAndrew Thompson 		zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
119602ac6454SAndrew Thompson 
119702ac6454SAndrew Thompson 	error = zyd_al2230_fini(rf);
119802ac6454SAndrew Thompson fail:
119902ac6454SAndrew Thompson 	return (error);
120002ac6454SAndrew Thompson #undef N
120102ac6454SAndrew Thompson }
120202ac6454SAndrew Thompson 
120302ac6454SAndrew Thompson static int
120402ac6454SAndrew Thompson zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
120502ac6454SAndrew Thompson {
120602ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
120702ac6454SAndrew Thompson 	int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
120802ac6454SAndrew Thompson 
120902ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
121002ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
121102ac6454SAndrew Thompson fail:
121202ac6454SAndrew Thompson 	return (error);
121302ac6454SAndrew Thompson }
121402ac6454SAndrew Thompson 
121502ac6454SAndrew Thompson static int
121602ac6454SAndrew Thompson zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
121702ac6454SAndrew Thompson {
121802ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
121902ac6454SAndrew Thompson 	int error, i;
122002ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
122102ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy1[] = {
122202ac6454SAndrew Thompson 		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
122302ac6454SAndrew Thompson 	};
122402ac6454SAndrew Thompson 	static const struct {
122502ac6454SAndrew Thompson 		uint32_t	r1, r2, r3;
122602ac6454SAndrew Thompson 	} rfprog[] = ZYD_AL2230_CHANTABLE;
122702ac6454SAndrew Thompson 
122802ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
122902ac6454SAndrew Thompson 	if (error != 0)
123002ac6454SAndrew Thompson 		goto fail;
123102ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
123202ac6454SAndrew Thompson 	if (error != 0)
123302ac6454SAndrew Thompson 		goto fail;
123402ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
123502ac6454SAndrew Thompson 	if (error != 0)
123602ac6454SAndrew Thompson 		goto fail;
123702ac6454SAndrew Thompson 
123802ac6454SAndrew Thompson 	for (i = 0; i < N(phy1); i++)
123902ac6454SAndrew Thompson 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
124002ac6454SAndrew Thompson fail:
124102ac6454SAndrew Thompson 	return (error);
124202ac6454SAndrew Thompson #undef N
124302ac6454SAndrew Thompson }
124402ac6454SAndrew Thompson 
124502ac6454SAndrew Thompson static int
124602ac6454SAndrew Thompson zyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
124702ac6454SAndrew Thompson {
124802ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
124902ac6454SAndrew Thompson 	int error, i;
125002ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
125102ac6454SAndrew Thompson 	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
125202ac6454SAndrew Thompson 	static const struct {
125302ac6454SAndrew Thompson 		uint32_t	r1, r2, r3;
125402ac6454SAndrew Thompson 	} rfprog[] = ZYD_AL2230_CHANTABLE_B;
125502ac6454SAndrew Thompson 
125602ac6454SAndrew Thompson 	for (i = 0; i < N(phy1); i++)
125702ac6454SAndrew Thompson 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
125802ac6454SAndrew Thompson 
125902ac6454SAndrew Thompson 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
126002ac6454SAndrew Thompson 	if (error != 0)
126102ac6454SAndrew Thompson 		goto fail;
126202ac6454SAndrew Thompson 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
126302ac6454SAndrew Thompson 	if (error != 0)
126402ac6454SAndrew Thompson 		goto fail;
126502ac6454SAndrew Thompson 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
126602ac6454SAndrew Thompson 	if (error != 0)
126702ac6454SAndrew Thompson 		goto fail;
126802ac6454SAndrew Thompson 	error = zyd_al2230_fini(rf);
126902ac6454SAndrew Thompson fail:
127002ac6454SAndrew Thompson 	return (error);
127102ac6454SAndrew Thompson #undef N
127202ac6454SAndrew Thompson }
127302ac6454SAndrew Thompson 
127402ac6454SAndrew Thompson #define	ZYD_AL2230_PHY_BANDEDGE6					\
127502ac6454SAndrew Thompson {									\
127602ac6454SAndrew Thompson 	{ ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 },	\
127702ac6454SAndrew Thompson 	{ ZYD_CR47,  0x1e }						\
127802ac6454SAndrew Thompson }
127902ac6454SAndrew Thompson 
128002ac6454SAndrew Thompson static int
128102ac6454SAndrew Thompson zyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
128202ac6454SAndrew Thompson {
128302ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
128402ac6454SAndrew Thompson 	int error = 0, i;
128502ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
128602ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
128702ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
128802ac6454SAndrew Thompson 	struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
128902ac6454SAndrew Thompson 	int chan = ieee80211_chan2ieee(ic, c);
129002ac6454SAndrew Thompson 
129102ac6454SAndrew Thompson 	if (chan == 1 || chan == 11)
129202ac6454SAndrew Thompson 		r[0].val = 0x12;
129302ac6454SAndrew Thompson 
129402ac6454SAndrew Thompson 	for (i = 0; i < N(r); i++)
129502ac6454SAndrew Thompson 		zyd_write16_m(sc, r[i].reg, r[i].val);
129602ac6454SAndrew Thompson fail:
129702ac6454SAndrew Thompson 	return (error);
129802ac6454SAndrew Thompson #undef N
129902ac6454SAndrew Thompson }
130002ac6454SAndrew Thompson 
130102ac6454SAndrew Thompson /*
130202ac6454SAndrew Thompson  * AL7230B RF methods.
130302ac6454SAndrew Thompson  */
130402ac6454SAndrew Thompson static int
130502ac6454SAndrew Thompson zyd_al7230B_init(struct zyd_rf *rf)
130602ac6454SAndrew Thompson {
130702ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
130802ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
130902ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
131002ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
131102ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
131202ac6454SAndrew Thompson 	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
131302ac6454SAndrew Thompson 	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
131402ac6454SAndrew Thompson 	int i, error;
131502ac6454SAndrew Thompson 
131602ac6454SAndrew Thompson 	/* for AL7230B, PHY and RF need to be initialized in "phases" */
131702ac6454SAndrew Thompson 
131802ac6454SAndrew Thompson 	/* init RF-dependent PHY registers, part one */
131902ac6454SAndrew Thompson 	for (i = 0; i < N(phyini_1); i++)
132002ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
132102ac6454SAndrew Thompson 
132202ac6454SAndrew Thompson 	/* init AL7230B radio, part one */
132302ac6454SAndrew Thompson 	for (i = 0; i < N(rfini_1); i++) {
132402ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
132502ac6454SAndrew Thompson 			return (error);
132602ac6454SAndrew Thompson 	}
132702ac6454SAndrew Thompson 	/* init RF-dependent PHY registers, part two */
132802ac6454SAndrew Thompson 	for (i = 0; i < N(phyini_2); i++)
132902ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
133002ac6454SAndrew Thompson 
133102ac6454SAndrew Thompson 	/* init AL7230B radio, part two */
133202ac6454SAndrew Thompson 	for (i = 0; i < N(rfini_2); i++) {
133302ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
133402ac6454SAndrew Thompson 			return (error);
133502ac6454SAndrew Thompson 	}
133602ac6454SAndrew Thompson 	/* init RF-dependent PHY registers, part three */
133702ac6454SAndrew Thompson 	for (i = 0; i < N(phyini_3); i++)
133802ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
133902ac6454SAndrew Thompson fail:
134002ac6454SAndrew Thompson 	return (error);
134102ac6454SAndrew Thompson #undef N
134202ac6454SAndrew Thompson }
134302ac6454SAndrew Thompson 
134402ac6454SAndrew Thompson static int
134502ac6454SAndrew Thompson zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
134602ac6454SAndrew Thompson {
134702ac6454SAndrew Thompson 	int error;
134802ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
134902ac6454SAndrew Thompson 
135002ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
135102ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
135202ac6454SAndrew Thompson fail:
135302ac6454SAndrew Thompson 	return (error);
135402ac6454SAndrew Thompson }
135502ac6454SAndrew Thompson 
135602ac6454SAndrew Thompson static int
135702ac6454SAndrew Thompson zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
135802ac6454SAndrew Thompson {
135902ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
136002ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
136102ac6454SAndrew Thompson 	static const struct {
136202ac6454SAndrew Thompson 		uint32_t	r1, r2;
136302ac6454SAndrew Thompson 	} rfprog[] = ZYD_AL7230B_CHANTABLE;
136402ac6454SAndrew Thompson 	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
136502ac6454SAndrew Thompson 	int i, error;
136602ac6454SAndrew Thompson 
136702ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR240, 0x57);
136802ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR251, 0x2f);
136902ac6454SAndrew Thompson 
137002ac6454SAndrew Thompson 	for (i = 0; i < N(rfsc); i++) {
137102ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
137202ac6454SAndrew Thompson 			return (error);
137302ac6454SAndrew Thompson 	}
137402ac6454SAndrew Thompson 
137502ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR128, 0x14);
137602ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR129, 0x12);
137702ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR130, 0x10);
137802ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR38,  0x38);
137902ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR136, 0xdf);
138002ac6454SAndrew Thompson 
138102ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
138202ac6454SAndrew Thompson 	if (error != 0)
138302ac6454SAndrew Thompson 		goto fail;
138402ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
138502ac6454SAndrew Thompson 	if (error != 0)
138602ac6454SAndrew Thompson 		goto fail;
138702ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, 0x3c9000);
138802ac6454SAndrew Thompson 	if (error != 0)
138902ac6454SAndrew Thompson 		goto fail;
139002ac6454SAndrew Thompson 
139102ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR251, 0x3f);
139202ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, 0x06);
139302ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR240, 0x08);
139402ac6454SAndrew Thompson fail:
139502ac6454SAndrew Thompson 	return (error);
139602ac6454SAndrew Thompson #undef N
139702ac6454SAndrew Thompson }
139802ac6454SAndrew Thompson 
139902ac6454SAndrew Thompson /*
140002ac6454SAndrew Thompson  * AL2210 RF methods.
140102ac6454SAndrew Thompson  */
140202ac6454SAndrew Thompson static int
140302ac6454SAndrew Thompson zyd_al2210_init(struct zyd_rf *rf)
140402ac6454SAndrew Thompson {
140502ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
140602ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
140702ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
140802ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_AL2210_RF;
140902ac6454SAndrew Thompson 	uint32_t tmp;
141002ac6454SAndrew Thompson 	int i, error;
141102ac6454SAndrew Thompson 
141202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR18, 2);
141302ac6454SAndrew Thompson 
141402ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
141502ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
141602ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
141702ac6454SAndrew Thompson 
141802ac6454SAndrew Thompson 	/* init AL2210 radio */
141902ac6454SAndrew Thompson 	for (i = 0; i < N(rfini); i++) {
142002ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
142102ac6454SAndrew Thompson 			return (error);
142202ac6454SAndrew Thompson 	}
142302ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
142402ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
142502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
142602ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
142702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
142802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
142902ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
143002ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR18, 3);
143102ac6454SAndrew Thompson fail:
143202ac6454SAndrew Thompson 	return (error);
143302ac6454SAndrew Thompson #undef N
143402ac6454SAndrew Thompson }
143502ac6454SAndrew Thompson 
143602ac6454SAndrew Thompson static int
143702ac6454SAndrew Thompson zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
143802ac6454SAndrew Thompson {
143902ac6454SAndrew Thompson 	/* vendor driver does nothing for this RF chip */
144002ac6454SAndrew Thompson 
144102ac6454SAndrew Thompson 	return (0);
144202ac6454SAndrew Thompson }
144302ac6454SAndrew Thompson 
144402ac6454SAndrew Thompson static int
144502ac6454SAndrew Thompson zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
144602ac6454SAndrew Thompson {
144702ac6454SAndrew Thompson 	int error;
144802ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
144902ac6454SAndrew Thompson 	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
145002ac6454SAndrew Thompson 	uint32_t tmp;
145102ac6454SAndrew Thompson 
145202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR18, 2);
145302ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
145402ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
145502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
145602ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
145702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
145802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
145902ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
146002ac6454SAndrew Thompson 
146102ac6454SAndrew Thompson 	/* actually set the channel */
146202ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1]);
146302ac6454SAndrew Thompson 	if (error != 0)
146402ac6454SAndrew Thompson 		goto fail;
146502ac6454SAndrew Thompson 
146602ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR18, 3);
146702ac6454SAndrew Thompson fail:
146802ac6454SAndrew Thompson 	return (error);
146902ac6454SAndrew Thompson }
147002ac6454SAndrew Thompson 
147102ac6454SAndrew Thompson /*
147202ac6454SAndrew Thompson  * GCT RF methods.
147302ac6454SAndrew Thompson  */
147402ac6454SAndrew Thompson static int
147502ac6454SAndrew Thompson zyd_gct_init(struct zyd_rf *rf)
147602ac6454SAndrew Thompson {
147702ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
147802ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
147902ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
148002ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_GCT_RF;
148102ac6454SAndrew Thompson 	int i, error;
148202ac6454SAndrew Thompson 
148302ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
148402ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
148502ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
148602ac6454SAndrew Thompson 
148702ac6454SAndrew Thompson 	/* init cgt radio */
148802ac6454SAndrew Thompson 	for (i = 0; i < N(rfini); i++) {
148902ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
149002ac6454SAndrew Thompson 			return (error);
149102ac6454SAndrew Thompson 	}
149202ac6454SAndrew Thompson fail:
149302ac6454SAndrew Thompson 	return (error);
149402ac6454SAndrew Thompson #undef N
149502ac6454SAndrew Thompson }
149602ac6454SAndrew Thompson 
149702ac6454SAndrew Thompson static int
149802ac6454SAndrew Thompson zyd_gct_switch_radio(struct zyd_rf *rf, int on)
149902ac6454SAndrew Thompson {
150002ac6454SAndrew Thompson 	/* vendor driver does nothing for this RF chip */
150102ac6454SAndrew Thompson 
150202ac6454SAndrew Thompson 	return (0);
150302ac6454SAndrew Thompson }
150402ac6454SAndrew Thompson 
150502ac6454SAndrew Thompson static int
150602ac6454SAndrew Thompson zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
150702ac6454SAndrew Thompson {
150802ac6454SAndrew Thompson 	int error;
150902ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
151002ac6454SAndrew Thompson 	static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE;
151102ac6454SAndrew Thompson 
151202ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, 0x1c0000);
151302ac6454SAndrew Thompson 	if (error != 0)
151402ac6454SAndrew Thompson 		goto fail;
151502ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1]);
151602ac6454SAndrew Thompson 	if (error != 0)
151702ac6454SAndrew Thompson 		goto fail;
151802ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, 0x1c0008);
151902ac6454SAndrew Thompson fail:
152002ac6454SAndrew Thompson 	return (error);
152102ac6454SAndrew Thompson }
152202ac6454SAndrew Thompson 
152302ac6454SAndrew Thompson /*
152402ac6454SAndrew Thompson  * Maxim RF methods.
152502ac6454SAndrew Thompson  */
152602ac6454SAndrew Thompson static int
152702ac6454SAndrew Thompson zyd_maxim_init(struct zyd_rf *rf)
152802ac6454SAndrew Thompson {
152902ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
153002ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
153102ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
153202ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_MAXIM_RF;
153302ac6454SAndrew Thompson 	uint16_t tmp;
153402ac6454SAndrew Thompson 	int i, error;
153502ac6454SAndrew Thompson 
153602ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
153702ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
153802ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
153902ac6454SAndrew Thompson 
154002ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
154102ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
154202ac6454SAndrew Thompson 
154302ac6454SAndrew Thompson 	/* init maxim radio */
154402ac6454SAndrew Thompson 	for (i = 0; i < N(rfini); i++) {
154502ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
154602ac6454SAndrew Thompson 			return (error);
154702ac6454SAndrew Thompson 	}
154802ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
154902ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
155002ac6454SAndrew Thompson fail:
155102ac6454SAndrew Thompson 	return (error);
155202ac6454SAndrew Thompson #undef N
155302ac6454SAndrew Thompson }
155402ac6454SAndrew Thompson 
155502ac6454SAndrew Thompson static int
155602ac6454SAndrew Thompson zyd_maxim_switch_radio(struct zyd_rf *rf, int on)
155702ac6454SAndrew Thompson {
155802ac6454SAndrew Thompson 
155902ac6454SAndrew Thompson 	/* vendor driver does nothing for this RF chip */
156002ac6454SAndrew Thompson 	return (0);
156102ac6454SAndrew Thompson }
156202ac6454SAndrew Thompson 
156302ac6454SAndrew Thompson static int
156402ac6454SAndrew Thompson zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan)
156502ac6454SAndrew Thompson {
156602ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
156702ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
156802ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
156902ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_MAXIM_RF;
157002ac6454SAndrew Thompson 	static const struct {
157102ac6454SAndrew Thompson 		uint32_t	r1, r2;
157202ac6454SAndrew Thompson 	} rfprog[] = ZYD_MAXIM_CHANTABLE;
157302ac6454SAndrew Thompson 	uint16_t tmp;
157402ac6454SAndrew Thompson 	int i, error;
157502ac6454SAndrew Thompson 
157602ac6454SAndrew Thompson 	/*
157702ac6454SAndrew Thompson 	 * Do the same as we do when initializing it, except for the channel
157802ac6454SAndrew Thompson 	 * values coming from the two channel tables.
157902ac6454SAndrew Thompson 	 */
158002ac6454SAndrew Thompson 
158102ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
158202ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
158302ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
158402ac6454SAndrew Thompson 
158502ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
158602ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
158702ac6454SAndrew Thompson 
158802ac6454SAndrew Thompson 	/* first two values taken from the chantables */
158902ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
159002ac6454SAndrew Thompson 	if (error != 0)
159102ac6454SAndrew Thompson 		goto fail;
159202ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
159302ac6454SAndrew Thompson 	if (error != 0)
159402ac6454SAndrew Thompson 		goto fail;
159502ac6454SAndrew Thompson 
159602ac6454SAndrew Thompson 	/* init maxim radio - skipping the two first values */
159702ac6454SAndrew Thompson 	for (i = 2; i < N(rfini); i++) {
159802ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
159902ac6454SAndrew Thompson 			return (error);
160002ac6454SAndrew Thompson 	}
160102ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
160202ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
160302ac6454SAndrew Thompson fail:
160402ac6454SAndrew Thompson 	return (error);
160502ac6454SAndrew Thompson #undef N
160602ac6454SAndrew Thompson }
160702ac6454SAndrew Thompson 
160802ac6454SAndrew Thompson /*
160902ac6454SAndrew Thompson  * Maxim2 RF methods.
161002ac6454SAndrew Thompson  */
161102ac6454SAndrew Thompson static int
161202ac6454SAndrew Thompson zyd_maxim2_init(struct zyd_rf *rf)
161302ac6454SAndrew Thompson {
161402ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
161502ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
161602ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
161702ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
161802ac6454SAndrew Thompson 	uint16_t tmp;
161902ac6454SAndrew Thompson 	int i, error;
162002ac6454SAndrew Thompson 
162102ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
162202ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
162302ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
162402ac6454SAndrew Thompson 
162502ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
162602ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
162702ac6454SAndrew Thompson 
162802ac6454SAndrew Thompson 	/* init maxim2 radio */
162902ac6454SAndrew Thompson 	for (i = 0; i < N(rfini); i++) {
163002ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
163102ac6454SAndrew Thompson 			return (error);
163202ac6454SAndrew Thompson 	}
163302ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
163402ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
163502ac6454SAndrew Thompson fail:
163602ac6454SAndrew Thompson 	return (error);
163702ac6454SAndrew Thompson #undef N
163802ac6454SAndrew Thompson }
163902ac6454SAndrew Thompson 
164002ac6454SAndrew Thompson static int
164102ac6454SAndrew Thompson zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
164202ac6454SAndrew Thompson {
164302ac6454SAndrew Thompson 
164402ac6454SAndrew Thompson 	/* vendor driver does nothing for this RF chip */
164502ac6454SAndrew Thompson 	return (0);
164602ac6454SAndrew Thompson }
164702ac6454SAndrew Thompson 
164802ac6454SAndrew Thompson static int
164902ac6454SAndrew Thompson zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
165002ac6454SAndrew Thompson {
165102ac6454SAndrew Thompson #define N(a)	(sizeof(a) / sizeof((a)[0]))
165202ac6454SAndrew Thompson 	struct zyd_softc *sc = rf->rf_sc;
165302ac6454SAndrew Thompson 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
165402ac6454SAndrew Thompson 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
165502ac6454SAndrew Thompson 	static const struct {
165602ac6454SAndrew Thompson 		uint32_t	r1, r2;
165702ac6454SAndrew Thompson 	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
165802ac6454SAndrew Thompson 	uint16_t tmp;
165902ac6454SAndrew Thompson 	int i, error;
166002ac6454SAndrew Thompson 
166102ac6454SAndrew Thompson 	/*
166202ac6454SAndrew Thompson 	 * Do the same as we do when initializing it, except for the channel
166302ac6454SAndrew Thompson 	 * values coming from the two channel tables.
166402ac6454SAndrew Thompson 	 */
166502ac6454SAndrew Thompson 
166602ac6454SAndrew Thompson 	/* init RF-dependent PHY registers */
166702ac6454SAndrew Thompson 	for (i = 0; i < N(phyini); i++)
166802ac6454SAndrew Thompson 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
166902ac6454SAndrew Thompson 
167002ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
167102ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
167202ac6454SAndrew Thompson 
167302ac6454SAndrew Thompson 	/* first two values taken from the chantables */
167402ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
167502ac6454SAndrew Thompson 	if (error != 0)
167602ac6454SAndrew Thompson 		goto fail;
167702ac6454SAndrew Thompson 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
167802ac6454SAndrew Thompson 	if (error != 0)
167902ac6454SAndrew Thompson 		goto fail;
168002ac6454SAndrew Thompson 
168102ac6454SAndrew Thompson 	/* init maxim2 radio - skipping the two first values */
168202ac6454SAndrew Thompson 	for (i = 2; i < N(rfini); i++) {
168302ac6454SAndrew Thompson 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
168402ac6454SAndrew Thompson 			return (error);
168502ac6454SAndrew Thompson 	}
168602ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_CR203, &tmp);
168702ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
168802ac6454SAndrew Thompson fail:
168902ac6454SAndrew Thompson 	return (error);
169002ac6454SAndrew Thompson #undef N
169102ac6454SAndrew Thompson }
169202ac6454SAndrew Thompson 
169302ac6454SAndrew Thompson static int
169402ac6454SAndrew Thompson zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
169502ac6454SAndrew Thompson {
169602ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
169702ac6454SAndrew Thompson 
169802ac6454SAndrew Thompson 	rf->rf_sc = sc;
169902ac6454SAndrew Thompson 
170002ac6454SAndrew Thompson 	switch (type) {
170102ac6454SAndrew Thompson 	case ZYD_RF_RFMD:
170202ac6454SAndrew Thompson 		rf->init         = zyd_rfmd_init;
170302ac6454SAndrew Thompson 		rf->switch_radio = zyd_rfmd_switch_radio;
170402ac6454SAndrew Thompson 		rf->set_channel  = zyd_rfmd_set_channel;
170502ac6454SAndrew Thompson 		rf->width        = 24;	/* 24-bit RF values */
170602ac6454SAndrew Thompson 		break;
170702ac6454SAndrew Thompson 	case ZYD_RF_AL2230:
170802ac6454SAndrew Thompson 	case ZYD_RF_AL2230S:
170902ac6454SAndrew Thompson 		if (sc->sc_macrev == ZYD_ZD1211B) {
171002ac6454SAndrew Thompson 			rf->init = zyd_al2230_init_b;
171102ac6454SAndrew Thompson 			rf->set_channel = zyd_al2230_set_channel_b;
171202ac6454SAndrew Thompson 		} else {
171302ac6454SAndrew Thompson 			rf->init = zyd_al2230_init;
171402ac6454SAndrew Thompson 			rf->set_channel = zyd_al2230_set_channel;
171502ac6454SAndrew Thompson 		}
171602ac6454SAndrew Thompson 		rf->switch_radio = zyd_al2230_switch_radio;
171702ac6454SAndrew Thompson 		rf->bandedge6	 = zyd_al2230_bandedge6;
171802ac6454SAndrew Thompson 		rf->width        = 24;	/* 24-bit RF values */
171902ac6454SAndrew Thompson 		break;
172002ac6454SAndrew Thompson 	case ZYD_RF_AL7230B:
172102ac6454SAndrew Thompson 		rf->init         = zyd_al7230B_init;
172202ac6454SAndrew Thompson 		rf->switch_radio = zyd_al7230B_switch_radio;
172302ac6454SAndrew Thompson 		rf->set_channel  = zyd_al7230B_set_channel;
172402ac6454SAndrew Thompson 		rf->width        = 24;	/* 24-bit RF values */
172502ac6454SAndrew Thompson 		break;
172602ac6454SAndrew Thompson 	case ZYD_RF_AL2210:
172702ac6454SAndrew Thompson 		rf->init         = zyd_al2210_init;
172802ac6454SAndrew Thompson 		rf->switch_radio = zyd_al2210_switch_radio;
172902ac6454SAndrew Thompson 		rf->set_channel  = zyd_al2210_set_channel;
173002ac6454SAndrew Thompson 		rf->width        = 24;	/* 24-bit RF values */
173102ac6454SAndrew Thompson 		break;
173202ac6454SAndrew Thompson 	case ZYD_RF_GCT:
173302ac6454SAndrew Thompson 		rf->init         = zyd_gct_init;
173402ac6454SAndrew Thompson 		rf->switch_radio = zyd_gct_switch_radio;
173502ac6454SAndrew Thompson 		rf->set_channel  = zyd_gct_set_channel;
173602ac6454SAndrew Thompson 		rf->width        = 21;	/* 21-bit RF values */
173702ac6454SAndrew Thompson 		break;
173802ac6454SAndrew Thompson 	case ZYD_RF_MAXIM_NEW:
173902ac6454SAndrew Thompson 		rf->init         = zyd_maxim_init;
174002ac6454SAndrew Thompson 		rf->switch_radio = zyd_maxim_switch_radio;
174102ac6454SAndrew Thompson 		rf->set_channel  = zyd_maxim_set_channel;
174202ac6454SAndrew Thompson 		rf->width        = 18;	/* 18-bit RF values */
174302ac6454SAndrew Thompson 		break;
174402ac6454SAndrew Thompson 	case ZYD_RF_MAXIM_NEW2:
174502ac6454SAndrew Thompson 		rf->init         = zyd_maxim2_init;
174602ac6454SAndrew Thompson 		rf->switch_radio = zyd_maxim2_switch_radio;
174702ac6454SAndrew Thompson 		rf->set_channel  = zyd_maxim2_set_channel;
174802ac6454SAndrew Thompson 		rf->width        = 18;	/* 18-bit RF values */
174902ac6454SAndrew Thompson 		break;
175002ac6454SAndrew Thompson 	default:
175102ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
175202ac6454SAndrew Thompson 		    "sorry, radio \"%s\" is not supported yet\n",
175302ac6454SAndrew Thompson 		    zyd_rf_name(type));
175402ac6454SAndrew Thompson 		return (EINVAL);
175502ac6454SAndrew Thompson 	}
175602ac6454SAndrew Thompson 	return (0);
175702ac6454SAndrew Thompson }
175802ac6454SAndrew Thompson 
175902ac6454SAndrew Thompson static const char *
176002ac6454SAndrew Thompson zyd_rf_name(uint8_t type)
176102ac6454SAndrew Thompson {
176202ac6454SAndrew Thompson 	static const char * const zyd_rfs[] = {
176302ac6454SAndrew Thompson 		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
176402ac6454SAndrew Thompson 		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
176502ac6454SAndrew Thompson 		"AL2230S",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
176602ac6454SAndrew Thompson 		"PHILIPS"
176702ac6454SAndrew Thompson 	};
176802ac6454SAndrew Thompson 
176902ac6454SAndrew Thompson 	return zyd_rfs[(type > 15) ? 0 : type];
177002ac6454SAndrew Thompson }
177102ac6454SAndrew Thompson 
177202ac6454SAndrew Thompson static int
177302ac6454SAndrew Thompson zyd_hw_init(struct zyd_softc *sc)
177402ac6454SAndrew Thompson {
177502ac6454SAndrew Thompson 	int error;
177602ac6454SAndrew Thompson 	const struct zyd_phy_pair *phyp;
177702ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
177802ac6454SAndrew Thompson 	uint16_t val;
177902ac6454SAndrew Thompson 
178002ac6454SAndrew Thompson 	/* specify that the plug and play is finished */
178102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
178202ac6454SAndrew Thompson 	zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
178302ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
178402ac6454SAndrew Thompson 	    sc->sc_fwbase);
178502ac6454SAndrew Thompson 
178602ac6454SAndrew Thompson 	/* retrieve firmware revision number */
178702ac6454SAndrew Thompson 	zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
178802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
178902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
179002ac6454SAndrew Thompson 	/* set mandatory rates - XXX assumes 802.11b/g */
179102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
179202ac6454SAndrew Thompson 
179302ac6454SAndrew Thompson 	/* disable interrupts */
179402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
179502ac6454SAndrew Thompson 
179602ac6454SAndrew Thompson 	if ((error = zyd_read_pod(sc)) != 0) {
179702ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM\n");
179802ac6454SAndrew Thompson 		goto fail;
179902ac6454SAndrew Thompson 	}
180002ac6454SAndrew Thompson 
180102ac6454SAndrew Thompson 	/* PHY init (resetting) */
180202ac6454SAndrew Thompson 	error = zyd_lock_phy(sc);
180302ac6454SAndrew Thompson 	if (error != 0)
180402ac6454SAndrew Thompson 		goto fail;
180502ac6454SAndrew Thompson 	phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
180602ac6454SAndrew Thompson 	for (; phyp->reg != 0; phyp++)
180702ac6454SAndrew Thompson 		zyd_write16_m(sc, phyp->reg, phyp->val);
180802ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
180902ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
181002ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_CR157, val >> 8);
181102ac6454SAndrew Thompson 	}
181202ac6454SAndrew Thompson 	error = zyd_unlock_phy(sc);
181302ac6454SAndrew Thompson 	if (error != 0)
181402ac6454SAndrew Thompson 		goto fail;
181502ac6454SAndrew Thompson 
181602ac6454SAndrew Thompson 	/* HMAC init */
181702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
181802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
181902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
182002ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
182102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
182202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
182302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
182402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
182502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
182602ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
182702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
182802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
182902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
183002ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
183102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
183202ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
183302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
183402ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
183502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
183602ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
183702ac6454SAndrew Thompson 
183802ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211) {
183902ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
184002ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
184102ac6454SAndrew Thompson 	} else {
184202ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
184302ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
184402ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
184502ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
184602ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
184702ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
184802ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
184902ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
185002ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
185102ac6454SAndrew Thompson 	}
185202ac6454SAndrew Thompson 
185302ac6454SAndrew Thompson 	/* init beacon interval to 100ms */
185402ac6454SAndrew Thompson 	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
185502ac6454SAndrew Thompson 		goto fail;
185602ac6454SAndrew Thompson 
185702ac6454SAndrew Thompson 	if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
185802ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
185902ac6454SAndrew Thompson 		    sc->sc_rfrev);
186002ac6454SAndrew Thompson 		goto fail;
186102ac6454SAndrew Thompson 	}
186202ac6454SAndrew Thompson 
186302ac6454SAndrew Thompson 	/* RF chip init */
186402ac6454SAndrew Thompson 	error = zyd_lock_phy(sc);
186502ac6454SAndrew Thompson 	if (error != 0)
186602ac6454SAndrew Thompson 		goto fail;
186702ac6454SAndrew Thompson 	error = (*rf->init)(rf);
186802ac6454SAndrew Thompson 	if (error != 0) {
186902ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
187002ac6454SAndrew Thompson 		    "radio initialization failed, error %d\n", error);
187102ac6454SAndrew Thompson 		goto fail;
187202ac6454SAndrew Thompson 	}
187302ac6454SAndrew Thompson 	error = zyd_unlock_phy(sc);
187402ac6454SAndrew Thompson 	if (error != 0)
187502ac6454SAndrew Thompson 		goto fail;
187602ac6454SAndrew Thompson 
187702ac6454SAndrew Thompson 	if ((error = zyd_read_eeprom(sc)) != 0) {
187802ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM\n");
187902ac6454SAndrew Thompson 		goto fail;
188002ac6454SAndrew Thompson 	}
188102ac6454SAndrew Thompson 
188202ac6454SAndrew Thompson fail:	return (error);
188302ac6454SAndrew Thompson }
188402ac6454SAndrew Thompson 
188502ac6454SAndrew Thompson static int
188602ac6454SAndrew Thompson zyd_read_pod(struct zyd_softc *sc)
188702ac6454SAndrew Thompson {
188802ac6454SAndrew Thompson 	int error;
188902ac6454SAndrew Thompson 	uint32_t tmp;
189002ac6454SAndrew Thompson 
189102ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
189202ac6454SAndrew Thompson 	sc->sc_rfrev     = tmp & 0x0f;
189302ac6454SAndrew Thompson 	sc->sc_ledtype   = (tmp >>  4) & 0x01;
189402ac6454SAndrew Thompson 	sc->sc_al2230s   = (tmp >>  7) & 0x01;
189502ac6454SAndrew Thompson 	sc->sc_cckgain   = (tmp >>  8) & 0x01;
189602ac6454SAndrew Thompson 	sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
189702ac6454SAndrew Thompson 	sc->sc_parev     = (tmp >> 16) & 0x0f;
189802ac6454SAndrew Thompson 	sc->sc_bandedge6 = (tmp >> 21) & 0x01;
189902ac6454SAndrew Thompson 	sc->sc_newphy    = (tmp >> 31) & 0x01;
190002ac6454SAndrew Thompson 	sc->sc_txled     = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
190102ac6454SAndrew Thompson fail:
190202ac6454SAndrew Thompson 	return (error);
190302ac6454SAndrew Thompson }
190402ac6454SAndrew Thompson 
190502ac6454SAndrew Thompson static int
190602ac6454SAndrew Thompson zyd_read_eeprom(struct zyd_softc *sc)
190702ac6454SAndrew Thompson {
190802ac6454SAndrew Thompson 	uint16_t val;
190902ac6454SAndrew Thompson 	int error, i;
191002ac6454SAndrew Thompson 
191102ac6454SAndrew Thompson 	/* read Tx power calibration tables */
191202ac6454SAndrew Thompson 	for (i = 0; i < 7; i++) {
191302ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
191402ac6454SAndrew Thompson 		sc->sc_pwrcal[i * 2] = val >> 8;
191502ac6454SAndrew Thompson 		sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
191602ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
191702ac6454SAndrew Thompson 		sc->sc_pwrint[i * 2] = val >> 8;
191802ac6454SAndrew Thompson 		sc->sc_pwrint[i * 2 + 1] = val & 0xff;
191902ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
192002ac6454SAndrew Thompson 		sc->sc_ofdm36_cal[i * 2] = val >> 8;
192102ac6454SAndrew Thompson 		sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
192202ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
192302ac6454SAndrew Thompson 		sc->sc_ofdm48_cal[i * 2] = val >> 8;
192402ac6454SAndrew Thompson 		sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
192502ac6454SAndrew Thompson 		zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
192602ac6454SAndrew Thompson 		sc->sc_ofdm54_cal[i * 2] = val >> 8;
192702ac6454SAndrew Thompson 		sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
192802ac6454SAndrew Thompson 	}
192902ac6454SAndrew Thompson fail:
193002ac6454SAndrew Thompson 	return (error);
193102ac6454SAndrew Thompson }
193202ac6454SAndrew Thompson 
193302ac6454SAndrew Thompson static int
193402ac6454SAndrew Thompson zyd_get_macaddr(struct zyd_softc *sc)
193502ac6454SAndrew Thompson {
193602ac6454SAndrew Thompson 	struct usb2_device_request req;
193702ac6454SAndrew Thompson 	usb2_error_t error;
193802ac6454SAndrew Thompson 
193902ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
194002ac6454SAndrew Thompson 	req.bRequest = ZYD_READFWDATAREQ;
194102ac6454SAndrew Thompson 	USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
194202ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
194302ac6454SAndrew Thompson 	USETW(req.wLength, IEEE80211_ADDR_LEN);
194402ac6454SAndrew Thompson 
194502ac6454SAndrew Thompson 	error = zyd_do_request(sc, &req, sc->sc_bssid);
194602ac6454SAndrew Thompson 	if (error != 0) {
194702ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
194802ac6454SAndrew Thompson 		    usb2_errstr(error));
194902ac6454SAndrew Thompson 	}
195002ac6454SAndrew Thompson 
195102ac6454SAndrew Thompson 	return (error);
195202ac6454SAndrew Thompson }
195302ac6454SAndrew Thompson 
195402ac6454SAndrew Thompson static int
195502ac6454SAndrew Thompson zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
195602ac6454SAndrew Thompson {
195702ac6454SAndrew Thompson 	int error;
195802ac6454SAndrew Thompson 	uint32_t tmp;
195902ac6454SAndrew Thompson 
196002ac6454SAndrew Thompson 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
196102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
196202ac6454SAndrew Thompson 	tmp = addr[5] << 8 | addr[4];
196302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
196402ac6454SAndrew Thompson fail:
196502ac6454SAndrew Thompson 	return (error);
196602ac6454SAndrew Thompson }
196702ac6454SAndrew Thompson 
196802ac6454SAndrew Thompson static int
196902ac6454SAndrew Thompson zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
197002ac6454SAndrew Thompson {
197102ac6454SAndrew Thompson 	int error;
197202ac6454SAndrew Thompson 	uint32_t tmp;
197302ac6454SAndrew Thompson 
197402ac6454SAndrew Thompson 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
197502ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
197602ac6454SAndrew Thompson 	tmp = addr[5] << 8 | addr[4];
197702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
197802ac6454SAndrew Thompson fail:
197902ac6454SAndrew Thompson 	return (error);
198002ac6454SAndrew Thompson }
198102ac6454SAndrew Thompson 
198202ac6454SAndrew Thompson static int
198302ac6454SAndrew Thompson zyd_switch_radio(struct zyd_softc *sc, int on)
198402ac6454SAndrew Thompson {
198502ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
198602ac6454SAndrew Thompson 	int error;
198702ac6454SAndrew Thompson 
198802ac6454SAndrew Thompson 	error = zyd_lock_phy(sc);
198902ac6454SAndrew Thompson 	if (error != 0)
199002ac6454SAndrew Thompson 		goto fail;
199102ac6454SAndrew Thompson 	error = (*rf->switch_radio)(rf, on);
199202ac6454SAndrew Thompson 	if (error != 0)
199302ac6454SAndrew Thompson 		goto fail;
199402ac6454SAndrew Thompson 	error = zyd_unlock_phy(sc);
199502ac6454SAndrew Thompson fail:
199602ac6454SAndrew Thompson 	return (error);
199702ac6454SAndrew Thompson }
199802ac6454SAndrew Thompson 
199902ac6454SAndrew Thompson static int
200002ac6454SAndrew Thompson zyd_set_led(struct zyd_softc *sc, int which, int on)
200102ac6454SAndrew Thompson {
200202ac6454SAndrew Thompson 	int error;
200302ac6454SAndrew Thompson 	uint32_t tmp;
200402ac6454SAndrew Thompson 
200502ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
200602ac6454SAndrew Thompson 	tmp &= ~which;
200702ac6454SAndrew Thompson 	if (on)
200802ac6454SAndrew Thompson 		tmp |= which;
200902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
201002ac6454SAndrew Thompson fail:
201102ac6454SAndrew Thompson 	return (error);
201202ac6454SAndrew Thompson }
201302ac6454SAndrew Thompson 
201402ac6454SAndrew Thompson static void
201502ac6454SAndrew Thompson zyd_multitask(struct usb2_proc_msg *pm)
201602ac6454SAndrew Thompson {
201702ac6454SAndrew Thompson 	struct zyd_task *task = (struct zyd_task *)pm;
201802ac6454SAndrew Thompson 	struct zyd_softc *sc = task->sc;
201902ac6454SAndrew Thompson 
202002ac6454SAndrew Thompson 	zyd_set_multi(sc);
202102ac6454SAndrew Thompson }
202202ac6454SAndrew Thompson 
202302ac6454SAndrew Thompson static void
202402ac6454SAndrew Thompson zyd_set_multi(struct zyd_softc *sc)
202502ac6454SAndrew Thompson {
202602ac6454SAndrew Thompson 	int error;
202702ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
202802ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
202902ac6454SAndrew Thompson 	struct ifmultiaddr *ifma;
203002ac6454SAndrew Thompson 	uint32_t low, high;
203102ac6454SAndrew Thompson 	uint8_t v;
203202ac6454SAndrew Thompson 
203302ac6454SAndrew Thompson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
203402ac6454SAndrew Thompson 		return;
203502ac6454SAndrew Thompson 
203602ac6454SAndrew Thompson 	low = 0x00000000;
203702ac6454SAndrew Thompson 	high = 0x80000000;
203802ac6454SAndrew Thompson 
203902ac6454SAndrew Thompson 	if (ic->ic_opmode == IEEE80211_M_MONITOR ||
204002ac6454SAndrew Thompson 	    (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) {
204102ac6454SAndrew Thompson 		low = 0xffffffff;
204202ac6454SAndrew Thompson 		high = 0xffffffff;
204302ac6454SAndrew Thompson 	} else {
204402ac6454SAndrew Thompson 		IF_ADDR_LOCK(ifp);
204502ac6454SAndrew Thompson 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
204602ac6454SAndrew Thompson 			if (ifma->ifma_addr->sa_family != AF_LINK)
204702ac6454SAndrew Thompson 				continue;
204802ac6454SAndrew Thompson 			v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
204902ac6454SAndrew Thompson 			    ifma->ifma_addr))[5] >> 2;
205002ac6454SAndrew Thompson 			if (v < 32)
205102ac6454SAndrew Thompson 				low |= 1 << v;
205202ac6454SAndrew Thompson 			else
205302ac6454SAndrew Thompson 				high |= 1 << (v - 32);
205402ac6454SAndrew Thompson 		}
205502ac6454SAndrew Thompson 		IF_ADDR_UNLOCK(ifp);
205602ac6454SAndrew Thompson 	}
205702ac6454SAndrew Thompson 
205802ac6454SAndrew Thompson 	/* reprogram multicast global hash table */
205902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_GHTBL, low);
206002ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_GHTBH, high);
206102ac6454SAndrew Thompson fail:
206202ac6454SAndrew Thompson 	if (error != 0)
206302ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
206402ac6454SAndrew Thompson 		    "could not set multicast hash table\n");
206502ac6454SAndrew Thompson }
206602ac6454SAndrew Thompson 
206702ac6454SAndrew Thompson static void
206802ac6454SAndrew Thompson zyd_update_mcast(struct ifnet *ifp)
206902ac6454SAndrew Thompson {
207002ac6454SAndrew Thompson 	struct zyd_softc *sc = ifp->if_softc;
207102ac6454SAndrew Thompson 
207202ac6454SAndrew Thompson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
207302ac6454SAndrew Thompson 		return;
207402ac6454SAndrew Thompson 
207502ac6454SAndrew Thompson 	ZYD_LOCK(sc);
207602ac6454SAndrew Thompson 	zyd_queue_command(sc, zyd_multitask,
207702ac6454SAndrew Thompson 	    &sc->sc_mcasttask[0].hdr, &sc->sc_mcasttask[1].hdr);
207802ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
207902ac6454SAndrew Thompson }
208002ac6454SAndrew Thompson 
208102ac6454SAndrew Thompson static int
208202ac6454SAndrew Thompson zyd_set_rxfilter(struct zyd_softc *sc)
208302ac6454SAndrew Thompson {
208402ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
208502ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
208602ac6454SAndrew Thompson 	uint32_t rxfilter;
208702ac6454SAndrew Thompson 
208802ac6454SAndrew Thompson 	switch (ic->ic_opmode) {
208902ac6454SAndrew Thompson 	case IEEE80211_M_STA:
209002ac6454SAndrew Thompson 		rxfilter = ZYD_FILTER_BSS;
209102ac6454SAndrew Thompson 		break;
209202ac6454SAndrew Thompson 	case IEEE80211_M_IBSS:
209302ac6454SAndrew Thompson 	case IEEE80211_M_HOSTAP:
209402ac6454SAndrew Thompson 		rxfilter = ZYD_FILTER_HOSTAP;
209502ac6454SAndrew Thompson 		break;
209602ac6454SAndrew Thompson 	case IEEE80211_M_MONITOR:
209702ac6454SAndrew Thompson 		rxfilter = ZYD_FILTER_MONITOR;
209802ac6454SAndrew Thompson 		break;
209902ac6454SAndrew Thompson 	default:
210002ac6454SAndrew Thompson 		/* should not get there */
210102ac6454SAndrew Thompson 		return (EINVAL);
210202ac6454SAndrew Thompson 	}
210302ac6454SAndrew Thompson 	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
210402ac6454SAndrew Thompson }
210502ac6454SAndrew Thompson 
210602ac6454SAndrew Thompson static void
210702ac6454SAndrew Thompson zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
210802ac6454SAndrew Thompson {
210902ac6454SAndrew Thompson 	int error;
211002ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
211102ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
211202ac6454SAndrew Thompson 	struct zyd_rf *rf = &sc->sc_rf;
211302ac6454SAndrew Thompson 	uint32_t tmp;
211402ac6454SAndrew Thompson 	int chan;
211502ac6454SAndrew Thompson 
211602ac6454SAndrew Thompson 	chan = ieee80211_chan2ieee(ic, c);
211702ac6454SAndrew Thompson 	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
211802ac6454SAndrew Thompson 		/* XXX should NEVER happen */
211902ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
212002ac6454SAndrew Thompson 		    "%s: invalid channel %x\n", __func__, chan);
212102ac6454SAndrew Thompson 		return;
212202ac6454SAndrew Thompson 	}
212302ac6454SAndrew Thompson 
212402ac6454SAndrew Thompson 	error = zyd_lock_phy(sc);
212502ac6454SAndrew Thompson 	if (error != 0)
212602ac6454SAndrew Thompson 		goto fail;
212702ac6454SAndrew Thompson 
212802ac6454SAndrew Thompson 	error = (*rf->set_channel)(rf, chan);
212902ac6454SAndrew Thompson 	if (error != 0)
213002ac6454SAndrew Thompson 		goto fail;
213102ac6454SAndrew Thompson 
213202ac6454SAndrew Thompson 	/* update Tx power */
213302ac6454SAndrew Thompson 	zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
213402ac6454SAndrew Thompson 
213502ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211B) {
213602ac6454SAndrew Thompson 		zyd_write16_m(sc, ZYD_CR67, sc->sc_ofdm36_cal[chan - 1]);
213702ac6454SAndrew Thompson 		zyd_write16_m(sc, ZYD_CR66, sc->sc_ofdm48_cal[chan - 1]);
213802ac6454SAndrew Thompson 		zyd_write16_m(sc, ZYD_CR65, sc->sc_ofdm54_cal[chan - 1]);
213902ac6454SAndrew Thompson 		zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
214002ac6454SAndrew Thompson 		zyd_write16_m(sc, ZYD_CR69, 0x28);
214102ac6454SAndrew Thompson 		zyd_write16_m(sc, ZYD_CR69, 0x2a);
214202ac6454SAndrew Thompson 	}
214302ac6454SAndrew Thompson 	if (sc->sc_cckgain) {
214402ac6454SAndrew Thompson 		/* set CCK baseband gain from EEPROM */
214502ac6454SAndrew Thompson 		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
214602ac6454SAndrew Thompson 			zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
214702ac6454SAndrew Thompson 	}
214802ac6454SAndrew Thompson 	if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
214902ac6454SAndrew Thompson 		error = (*rf->bandedge6)(rf, c);
215002ac6454SAndrew Thompson 		if (error != 0)
215102ac6454SAndrew Thompson 			goto fail;
215202ac6454SAndrew Thompson 	}
215302ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
215402ac6454SAndrew Thompson 
215502ac6454SAndrew Thompson 	error = zyd_unlock_phy(sc);
215602ac6454SAndrew Thompson 	if (error != 0)
215702ac6454SAndrew Thompson 		goto fail;
215802ac6454SAndrew Thompson 
215902ac6454SAndrew Thompson 	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
216002ac6454SAndrew Thompson 	    htole16(c->ic_freq);
216102ac6454SAndrew Thompson 	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
216202ac6454SAndrew Thompson 	    htole16(c->ic_flags);
216302ac6454SAndrew Thompson fail:
216402ac6454SAndrew Thompson 	return;
216502ac6454SAndrew Thompson }
216602ac6454SAndrew Thompson 
216702ac6454SAndrew Thompson static int
216802ac6454SAndrew Thompson zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
216902ac6454SAndrew Thompson {
217002ac6454SAndrew Thompson 	int error;
217102ac6454SAndrew Thompson 	uint32_t val;
217202ac6454SAndrew Thompson 
217302ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
217402ac6454SAndrew Thompson 	sc->sc_atim_wnd = val;
217502ac6454SAndrew Thompson 	zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
217602ac6454SAndrew Thompson 	sc->sc_pre_tbtt = val;
217702ac6454SAndrew Thompson 	sc->sc_bcn_int = bintval;
217802ac6454SAndrew Thompson 
217902ac6454SAndrew Thompson 	if (sc->sc_bcn_int <= 5)
218002ac6454SAndrew Thompson 		sc->sc_bcn_int = 5;
218102ac6454SAndrew Thompson 	if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
218202ac6454SAndrew Thompson 		sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
218302ac6454SAndrew Thompson 	if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
218402ac6454SAndrew Thompson 		sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
218502ac6454SAndrew Thompson 
218602ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
218702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
218802ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
218902ac6454SAndrew Thompson fail:
219002ac6454SAndrew Thompson 	return (error);
219102ac6454SAndrew Thompson }
219202ac6454SAndrew Thompson 
219302ac6454SAndrew Thompson static void
219402ac6454SAndrew Thompson zyd_rx_data(struct usb2_xfer *xfer, int offset, uint16_t len)
219502ac6454SAndrew Thompson {
219602ac6454SAndrew Thompson 	struct zyd_softc *sc = xfer->priv_sc;
219702ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
219802ac6454SAndrew Thompson 	struct zyd_plcphdr plcp;
219902ac6454SAndrew Thompson 	struct zyd_rx_stat stat;
220002ac6454SAndrew Thompson 	struct mbuf *m;
220102ac6454SAndrew Thompson 	int rlen, rssi;
220202ac6454SAndrew Thompson 
220302ac6454SAndrew Thompson 	if (len < ZYD_MIN_FRAGSZ) {
220402ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
220502ac6454SAndrew Thompson 		    device_get_nameunit(sc->sc_dev), len);
220602ac6454SAndrew Thompson 		ifp->if_ierrors++;
220702ac6454SAndrew Thompson 		return;
220802ac6454SAndrew Thompson 	}
220902ac6454SAndrew Thompson 	usb2_copy_out(xfer->frbuffers, offset, &plcp, sizeof(plcp));
221002ac6454SAndrew Thompson 	usb2_copy_out(xfer->frbuffers, offset + len - sizeof(stat),
221102ac6454SAndrew Thompson 	    &stat, sizeof(stat));
221202ac6454SAndrew Thompson 
221302ac6454SAndrew Thompson 	if (stat.flags & ZYD_RX_ERROR) {
221402ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_RECV,
221502ac6454SAndrew Thompson 		    "%s: RX status indicated error (%x)\n",
221602ac6454SAndrew Thompson 		    device_get_nameunit(sc->sc_dev), stat.flags);
221702ac6454SAndrew Thompson 		ifp->if_ierrors++;
221802ac6454SAndrew Thompson 		return;
221902ac6454SAndrew Thompson 	}
222002ac6454SAndrew Thompson 
222102ac6454SAndrew Thompson 	/* compute actual frame length */
222202ac6454SAndrew Thompson 	rlen = len - sizeof(struct zyd_plcphdr) -
222302ac6454SAndrew Thompson 	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
222402ac6454SAndrew Thompson 
222502ac6454SAndrew Thompson 	/* allocate a mbuf to store the frame */
222602ac6454SAndrew Thompson 	if (rlen > MCLBYTES) {
222702ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
222802ac6454SAndrew Thompson 		    device_get_nameunit(sc->sc_dev), rlen);
222902ac6454SAndrew Thompson 		ifp->if_ierrors++;
223002ac6454SAndrew Thompson 		return;
223102ac6454SAndrew Thompson 	} else if (rlen > MHLEN)
223202ac6454SAndrew Thompson 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
223302ac6454SAndrew Thompson 	else
223402ac6454SAndrew Thompson 		m = m_gethdr(M_DONTWAIT, MT_DATA);
223502ac6454SAndrew Thompson 	if (m == NULL) {
223602ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
223702ac6454SAndrew Thompson 		    device_get_nameunit(sc->sc_dev));
223802ac6454SAndrew Thompson 		ifp->if_ierrors++;
223902ac6454SAndrew Thompson 		return;
224002ac6454SAndrew Thompson 	}
224102ac6454SAndrew Thompson 	m->m_pkthdr.rcvif = ifp;
224202ac6454SAndrew Thompson 	m->m_pkthdr.len = m->m_len = rlen;
224302ac6454SAndrew Thompson 	usb2_copy_out(xfer->frbuffers, offset + sizeof(plcp),
224402ac6454SAndrew Thompson 	    mtod(m, uint8_t *), rlen);
224502ac6454SAndrew Thompson 
224602ac6454SAndrew Thompson 	if (bpf_peers_present(ifp->if_bpf)) {
224702ac6454SAndrew Thompson 		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
224802ac6454SAndrew Thompson 
224902ac6454SAndrew Thompson 		tap->wr_flags = 0;
225002ac6454SAndrew Thompson 		if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
225102ac6454SAndrew Thompson 			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
225202ac6454SAndrew Thompson 		/* XXX toss, no way to express errors */
225302ac6454SAndrew Thompson 		if (stat.flags & ZYD_RX_DECRYPTERR)
225402ac6454SAndrew Thompson 			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
225502ac6454SAndrew Thompson 		tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
225602ac6454SAndrew Thompson 		    (stat.flags & ZYD_RX_OFDM) ?
225702ac6454SAndrew Thompson 			IEEE80211_T_OFDM : IEEE80211_T_CCK);
225802ac6454SAndrew Thompson 		tap->wr_antsignal = stat.rssi + -95;
225902ac6454SAndrew Thompson 		tap->wr_antnoise = -95;	/* XXX */
226002ac6454SAndrew Thompson 
226102ac6454SAndrew Thompson 		bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m);
226202ac6454SAndrew Thompson 	}
226302ac6454SAndrew Thompson 	rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
226402ac6454SAndrew Thompson 
226502ac6454SAndrew Thompson 	sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
226602ac6454SAndrew Thompson 	sc->sc_rx_data[sc->sc_rx_count].m = m;
226702ac6454SAndrew Thompson 	sc->sc_rx_count++;
226802ac6454SAndrew Thompson }
226902ac6454SAndrew Thompson 
227002ac6454SAndrew Thompson static void
227102ac6454SAndrew Thompson zyd_bulk_read_callback(struct usb2_xfer *xfer)
227202ac6454SAndrew Thompson {
227302ac6454SAndrew Thompson 	struct zyd_softc *sc = xfer->priv_sc;
227402ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
227502ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
227602ac6454SAndrew Thompson 	struct ieee80211_node *ni;
227702ac6454SAndrew Thompson 	struct zyd_rx_desc desc;
227802ac6454SAndrew Thompson 	struct mbuf *m;
227902ac6454SAndrew Thompson 	uint32_t offset;
228002ac6454SAndrew Thompson 	uint8_t rssi;
228102ac6454SAndrew Thompson 	int8_t nf;
228202ac6454SAndrew Thompson 	int i;
228302ac6454SAndrew Thompson 
228402ac6454SAndrew Thompson 	sc->sc_rx_count = 0;
228502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
228602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
228702ac6454SAndrew Thompson 		usb2_copy_out(xfer->frbuffers, xfer->actlen - sizeof(desc),
228802ac6454SAndrew Thompson 		    &desc, sizeof(desc));
228902ac6454SAndrew Thompson 
229002ac6454SAndrew Thompson 		offset = 0;
229102ac6454SAndrew Thompson 		if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
229202ac6454SAndrew Thompson 			DPRINTF(sc, ZYD_DEBUG_RECV,
229302ac6454SAndrew Thompson 			    "%s: received multi-frame transfer\n", __func__);
229402ac6454SAndrew Thompson 
229502ac6454SAndrew Thompson 			for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
229602ac6454SAndrew Thompson 				uint16_t len16 = UGETW(desc.len[i]);
229702ac6454SAndrew Thompson 
229802ac6454SAndrew Thompson 				if (len16 == 0 || len16 > xfer->actlen)
229902ac6454SAndrew Thompson 					break;
230002ac6454SAndrew Thompson 
230102ac6454SAndrew Thompson 				zyd_rx_data(xfer, offset, len16);
230202ac6454SAndrew Thompson 
230302ac6454SAndrew Thompson 				/* next frame is aligned on a 32-bit boundary */
230402ac6454SAndrew Thompson 				len16 = (len16 + 3) & ~3;
230502ac6454SAndrew Thompson 				offset += len16;
230602ac6454SAndrew Thompson 				if (len16 > xfer->actlen)
230702ac6454SAndrew Thompson 					break;
230802ac6454SAndrew Thompson 				xfer->actlen -= len16;
230902ac6454SAndrew Thompson 			}
231002ac6454SAndrew Thompson 		} else {
231102ac6454SAndrew Thompson 			DPRINTF(sc, ZYD_DEBUG_RECV,
231202ac6454SAndrew Thompson 			    "%s: received single-frame transfer\n", __func__);
231302ac6454SAndrew Thompson 
231402ac6454SAndrew Thompson 			zyd_rx_data(xfer, 0, xfer->actlen);
231502ac6454SAndrew Thompson 		}
231602ac6454SAndrew Thompson 		/* FALLTHROUGH */
231702ac6454SAndrew Thompson 	case USB_ST_SETUP:
231802ac6454SAndrew Thompson tr_setup:
231902ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
232002ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
232102ac6454SAndrew Thompson 
232202ac6454SAndrew Thompson 		/*
232302ac6454SAndrew Thompson 		 * At the end of a USB callback it is always safe to unlock
232402ac6454SAndrew Thompson 		 * the private mutex of a device! That is why we do the
232502ac6454SAndrew Thompson 		 * "ieee80211_input" here, and not some lines up!
232602ac6454SAndrew Thompson 		 */
232702ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
232802ac6454SAndrew Thompson 		for (i = 0; i < sc->sc_rx_count; i++) {
232902ac6454SAndrew Thompson 			rssi = sc->sc_rx_data[i].rssi;
233002ac6454SAndrew Thompson 			m = sc->sc_rx_data[i].m;
233102ac6454SAndrew Thompson 			sc->sc_rx_data[i].m = NULL;
233202ac6454SAndrew Thompson 
233302ac6454SAndrew Thompson 			nf = -95;	/* XXX */
233402ac6454SAndrew Thompson 
233502ac6454SAndrew Thompson 			ni = ieee80211_find_rxnode(ic,
233602ac6454SAndrew Thompson 			    mtod(m, struct ieee80211_frame_min *));
233702ac6454SAndrew Thompson 			if (ni != NULL) {
233802ac6454SAndrew Thompson 				(void)ieee80211_input(ni, m, rssi, nf, 0);
233902ac6454SAndrew Thompson 				ieee80211_free_node(ni);
234002ac6454SAndrew Thompson 			} else
234102ac6454SAndrew Thompson 				(void)ieee80211_input_all(ic, m, rssi, nf, 0);
234202ac6454SAndrew Thompson 		}
234302ac6454SAndrew Thompson 		ZYD_LOCK(sc);
234402ac6454SAndrew Thompson 		break;
234502ac6454SAndrew Thompson 
234602ac6454SAndrew Thompson 	default:			/* Error */
234702ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usb2_errstr(xfer->error));
234802ac6454SAndrew Thompson 
234902ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
235002ac6454SAndrew Thompson 			/* try to clear stall first */
235102ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
235202ac6454SAndrew Thompson 			goto tr_setup;
235302ac6454SAndrew Thompson 		}
235402ac6454SAndrew Thompson 		break;
235502ac6454SAndrew Thompson 	}
235602ac6454SAndrew Thompson }
235702ac6454SAndrew Thompson 
235802ac6454SAndrew Thompson static uint8_t
235902ac6454SAndrew Thompson zyd_plcp_signal(int rate)
236002ac6454SAndrew Thompson {
236102ac6454SAndrew Thompson 	switch (rate) {
236202ac6454SAndrew Thompson 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
236302ac6454SAndrew Thompson 	case 12:
236402ac6454SAndrew Thompson 		return (0xb);
236502ac6454SAndrew Thompson 	case 18:
236602ac6454SAndrew Thompson 		return (0xf);
236702ac6454SAndrew Thompson 	case 24:
236802ac6454SAndrew Thompson 		return (0xa);
236902ac6454SAndrew Thompson 	case 36:
237002ac6454SAndrew Thompson 		return (0xe);
237102ac6454SAndrew Thompson 	case 48:
237202ac6454SAndrew Thompson 		return (0x9);
237302ac6454SAndrew Thompson 	case 72:
237402ac6454SAndrew Thompson 		return (0xd);
237502ac6454SAndrew Thompson 	case 96:
237602ac6454SAndrew Thompson 		return (0x8);
237702ac6454SAndrew Thompson 	case 108:
237802ac6454SAndrew Thompson 		return (0xc);
237902ac6454SAndrew Thompson 	/* CCK rates (NB: not IEEE std, device-specific) */
238002ac6454SAndrew Thompson 	case 2:
238102ac6454SAndrew Thompson 		return (0x0);
238202ac6454SAndrew Thompson 	case 4:
238302ac6454SAndrew Thompson 		return (0x1);
238402ac6454SAndrew Thompson 	case 11:
238502ac6454SAndrew Thompson 		return (0x2);
238602ac6454SAndrew Thompson 	case 22:
238702ac6454SAndrew Thompson 		return (0x3);
238802ac6454SAndrew Thompson 	}
238902ac6454SAndrew Thompson 	return (0xff);		/* XXX unsupported/unknown rate */
239002ac6454SAndrew Thompson }
239102ac6454SAndrew Thompson 
239202ac6454SAndrew Thompson static int
239302ac6454SAndrew Thompson zyd_tx_mgt(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
239402ac6454SAndrew Thompson {
239502ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
239602ac6454SAndrew Thompson 	struct ieee80211com *ic = ni->ni_ic;
239702ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
239802ac6454SAndrew Thompson 	struct zyd_tx_desc *desc;
239902ac6454SAndrew Thompson 	struct zyd_tx_data *data;
240002ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
240102ac6454SAndrew Thompson 	struct ieee80211_key *k;
240202ac6454SAndrew Thompson 	int rate, totlen;
240302ac6454SAndrew Thompson 	uint16_t pktlen;
240402ac6454SAndrew Thompson 
240502ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
240602ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
240702ac6454SAndrew Thompson 	sc->tx_nfree--;
240802ac6454SAndrew Thompson 	desc = &data->desc;
240902ac6454SAndrew Thompson 
241002ac6454SAndrew Thompson 	rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
241102ac6454SAndrew Thompson 
241202ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
241302ac6454SAndrew Thompson 
241402ac6454SAndrew Thompson 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
241502ac6454SAndrew Thompson 		k = ieee80211_crypto_encap(ni, m0);
241602ac6454SAndrew Thompson 		if (k == NULL) {
241702ac6454SAndrew Thompson 			m_freem(m0);
241802ac6454SAndrew Thompson 			return (ENOBUFS);
241902ac6454SAndrew Thompson 		}
242002ac6454SAndrew Thompson 	}
242102ac6454SAndrew Thompson 
242202ac6454SAndrew Thompson 	data->ni = ni;
242302ac6454SAndrew Thompson 	data->m = m0;
242402ac6454SAndrew Thompson 	data->rate = rate;
242502ac6454SAndrew Thompson 
242602ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
242702ac6454SAndrew Thompson 
242802ac6454SAndrew Thompson 	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
242902ac6454SAndrew Thompson 
243002ac6454SAndrew Thompson 	/* fill Tx descriptor */
243102ac6454SAndrew Thompson 	desc->len = htole16(totlen);
243202ac6454SAndrew Thompson 
243302ac6454SAndrew Thompson 	desc->flags = ZYD_TX_FLAG_BACKOFF;
243402ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
243502ac6454SAndrew Thompson 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
243602ac6454SAndrew Thompson 		if (totlen > vap->iv_rtsthreshold) {
243702ac6454SAndrew Thompson 			desc->flags |= ZYD_TX_FLAG_RTS;
243802ac6454SAndrew Thompson 		} else if (ZYD_RATE_IS_OFDM(rate) &&
243902ac6454SAndrew Thompson 		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
244002ac6454SAndrew Thompson 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
244102ac6454SAndrew Thompson 				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
244202ac6454SAndrew Thompson 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
244302ac6454SAndrew Thompson 				desc->flags |= ZYD_TX_FLAG_RTS;
244402ac6454SAndrew Thompson 		}
244502ac6454SAndrew Thompson 	} else
244602ac6454SAndrew Thompson 		desc->flags |= ZYD_TX_FLAG_MULTICAST;
244702ac6454SAndrew Thompson 
244802ac6454SAndrew Thompson 	if ((wh->i_fc[0] &
244902ac6454SAndrew Thompson 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
245002ac6454SAndrew Thompson 	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
245102ac6454SAndrew Thompson 		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
245202ac6454SAndrew Thompson 
245302ac6454SAndrew Thompson 	desc->phy = zyd_plcp_signal(rate);
245402ac6454SAndrew Thompson 	if (ZYD_RATE_IS_OFDM(rate)) {
245502ac6454SAndrew Thompson 		desc->phy |= ZYD_TX_PHY_OFDM;
245602ac6454SAndrew Thompson 		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
245702ac6454SAndrew Thompson 			desc->phy |= ZYD_TX_PHY_5GHZ;
245802ac6454SAndrew Thompson 	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
245902ac6454SAndrew Thompson 		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
246002ac6454SAndrew Thompson 
246102ac6454SAndrew Thompson 	/* actual transmit length (XXX why +10?) */
246202ac6454SAndrew Thompson 	pktlen = ZYD_TX_DESC_SIZE + 10;
246302ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211)
246402ac6454SAndrew Thompson 		pktlen += totlen;
246502ac6454SAndrew Thompson 	desc->pktlen = htole16(pktlen);
246602ac6454SAndrew Thompson 
246702ac6454SAndrew Thompson 	desc->plcp_length = (16 * totlen + rate - 1) / rate;
246802ac6454SAndrew Thompson 	desc->plcp_service = 0;
246902ac6454SAndrew Thompson 	if (rate == 22) {
247002ac6454SAndrew Thompson 		const int remainder = (16 * totlen) % 22;
247102ac6454SAndrew Thompson 		if (remainder != 0 && remainder < 7)
247202ac6454SAndrew Thompson 			desc->plcp_service |= ZYD_PLCP_LENGEXT;
247302ac6454SAndrew Thompson 	}
247402ac6454SAndrew Thompson 
247502ac6454SAndrew Thompson 	if (bpf_peers_present(ifp->if_bpf)) {
247602ac6454SAndrew Thompson 		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
247702ac6454SAndrew Thompson 
247802ac6454SAndrew Thompson 		tap->wt_flags = 0;
247902ac6454SAndrew Thompson 		tap->wt_rate = rate;
248002ac6454SAndrew Thompson 
248102ac6454SAndrew Thompson 		bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
248202ac6454SAndrew Thompson 	}
248302ac6454SAndrew Thompson 
248402ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_XMIT,
248502ac6454SAndrew Thompson 	    "%s: sending mgt frame len=%zu rate=%u\n",
248602ac6454SAndrew Thompson 	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
248702ac6454SAndrew Thompson 		rate);
248802ac6454SAndrew Thompson 
248902ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
249002ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
249102ac6454SAndrew Thompson 
249202ac6454SAndrew Thompson 	return (0);
249302ac6454SAndrew Thompson }
249402ac6454SAndrew Thompson 
249502ac6454SAndrew Thompson static void
249602ac6454SAndrew Thompson zyd_bulk_write_callback(struct usb2_xfer *xfer)
249702ac6454SAndrew Thompson {
249802ac6454SAndrew Thompson 	struct zyd_softc *sc = xfer->priv_sc;
249902ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
250002ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
250102ac6454SAndrew Thompson 	struct ieee80211_channel *c = ic->ic_curchan;
250202ac6454SAndrew Thompson 	struct zyd_tx_data *data;
250302ac6454SAndrew Thompson 	struct mbuf *m;
250402ac6454SAndrew Thompson 
250577ddf3d3SAndrew Thompson 	/* wakeup any waiting command, if any */
250677ddf3d3SAndrew Thompson 	if (sc->sc_last_task != NULL)
250777ddf3d3SAndrew Thompson 		cv_signal(&sc->sc_cmd_cv);
250877ddf3d3SAndrew Thompson 
250902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
251002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
251102ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
251202ac6454SAndrew Thompson 		    xfer->actlen);
251302ac6454SAndrew Thompson 
251402ac6454SAndrew Thompson 		/* free resources */
251502ac6454SAndrew Thompson 		data = xfer->priv_fifo;
251602ac6454SAndrew Thompson 		zyd_tx_free(data, 0);
251702ac6454SAndrew Thompson 		xfer->priv_fifo = NULL;
251802ac6454SAndrew Thompson 
251902ac6454SAndrew Thompson 		ifp->if_opackets++;
252002ac6454SAndrew Thompson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
252102ac6454SAndrew Thompson 
252202ac6454SAndrew Thompson 		/* FALLTHROUGH */
252302ac6454SAndrew Thompson 	case USB_ST_SETUP:
252402ac6454SAndrew Thompson tr_setup:
252577ddf3d3SAndrew Thompson 		/* wait for command to complete, if any */
252677ddf3d3SAndrew Thompson 		if (sc->sc_last_task != NULL)
252777ddf3d3SAndrew Thompson 			break;
252877ddf3d3SAndrew Thompson 
252902ac6454SAndrew Thompson 		data = STAILQ_FIRST(&sc->tx_q);
253002ac6454SAndrew Thompson 		if (data) {
253102ac6454SAndrew Thompson 			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
253202ac6454SAndrew Thompson 			m = data->m;
253302ac6454SAndrew Thompson 
253402ac6454SAndrew Thompson 			if (m->m_pkthdr.len > ZYD_MAX_TXBUFSZ) {
253502ac6454SAndrew Thompson 				DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
253602ac6454SAndrew Thompson 				    m->m_pkthdr.len);
253702ac6454SAndrew Thompson 				m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
253802ac6454SAndrew Thompson 			}
253902ac6454SAndrew Thompson 			usb2_copy_in(xfer->frbuffers, 0, &data->desc,
254002ac6454SAndrew Thompson 			    ZYD_TX_DESC_SIZE);
254102ac6454SAndrew Thompson 			usb2_m_copy_in(xfer->frbuffers, ZYD_TX_DESC_SIZE, m, 0,
254202ac6454SAndrew Thompson 			    m->m_pkthdr.len);
254302ac6454SAndrew Thompson 
254402ac6454SAndrew Thompson 			if (bpf_peers_present(ifp->if_bpf)) {
254502ac6454SAndrew Thompson 				struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
254602ac6454SAndrew Thompson 
254702ac6454SAndrew Thompson 				tap->wt_flags = 0;
254802ac6454SAndrew Thompson 				tap->wt_rate = data->rate;
254902ac6454SAndrew Thompson 				tap->wt_chan_freq = htole16(c->ic_freq);
255002ac6454SAndrew Thompson 				tap->wt_chan_flags = htole16(c->ic_flags);
255102ac6454SAndrew Thompson 
255202ac6454SAndrew Thompson 				bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m);
255302ac6454SAndrew Thompson 			}
255402ac6454SAndrew Thompson 
255502ac6454SAndrew Thompson 			xfer->frlengths[0] = ZYD_TX_DESC_SIZE + m->m_pkthdr.len;
255602ac6454SAndrew Thompson 			xfer->priv_fifo = data;
255702ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
255802ac6454SAndrew Thompson 		}
255902ac6454SAndrew Thompson 		break;
256002ac6454SAndrew Thompson 
256102ac6454SAndrew Thompson 	default:			/* Error */
256202ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
256302ac6454SAndrew Thompson 		    usb2_errstr(xfer->error));
256402ac6454SAndrew Thompson 
256502ac6454SAndrew Thompson 		ifp->if_oerrors++;
256602ac6454SAndrew Thompson 		data = xfer->priv_fifo;
256702ac6454SAndrew Thompson 		xfer->priv_fifo = NULL;
256802ac6454SAndrew Thompson 		if (data != NULL)
256902ac6454SAndrew Thompson 			zyd_tx_free(data, xfer->error);
257002ac6454SAndrew Thompson 
257102ac6454SAndrew Thompson 		if (xfer->error == USB_ERR_STALLED) {
257202ac6454SAndrew Thompson 			/* try to clear stall first */
257302ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
257402ac6454SAndrew Thompson 			goto tr_setup;
257502ac6454SAndrew Thompson 		}
257602ac6454SAndrew Thompson 		if (xfer->error == USB_ERR_TIMEOUT)
257702ac6454SAndrew Thompson 			device_printf(sc->sc_dev, "device timeout\n");
257802ac6454SAndrew Thompson 		break;
257902ac6454SAndrew Thompson 	}
258002ac6454SAndrew Thompson }
258102ac6454SAndrew Thompson 
258202ac6454SAndrew Thompson static int
258302ac6454SAndrew Thompson zyd_tx_data(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
258402ac6454SAndrew Thompson {
258502ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
258602ac6454SAndrew Thompson 	struct ieee80211com *ic = ni->ni_ic;
258702ac6454SAndrew Thompson 	struct zyd_tx_desc *desc;
258802ac6454SAndrew Thompson 	struct zyd_tx_data *data;
258902ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
259002ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
259102ac6454SAndrew Thompson 	struct ieee80211_key *k;
259202ac6454SAndrew Thompson 	int rate, totlen;
259302ac6454SAndrew Thompson 	uint16_t pktlen;
259402ac6454SAndrew Thompson 
259502ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
259602ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
259702ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
259802ac6454SAndrew Thompson 	sc->tx_nfree--;
259902ac6454SAndrew Thompson 	desc = &data->desc;
260002ac6454SAndrew Thompson 
260102ac6454SAndrew Thompson 	desc->flags = ZYD_TX_FLAG_BACKOFF;
260202ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
260302ac6454SAndrew Thompson 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
260402ac6454SAndrew Thompson 		rate = tp->mcastrate;
260502ac6454SAndrew Thompson 		desc->flags |= ZYD_TX_FLAG_MULTICAST;
260602ac6454SAndrew Thompson 	} else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
260702ac6454SAndrew Thompson 		rate = tp->ucastrate;
260802ac6454SAndrew Thompson 	} else {
260902ac6454SAndrew Thompson 		(void) ieee80211_amrr_choose(ni, &ZYD_NODE(ni)->amn);
261002ac6454SAndrew Thompson 		rate = ni->ni_txrate;
261102ac6454SAndrew Thompson 	}
261202ac6454SAndrew Thompson 
261302ac6454SAndrew Thompson 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
261402ac6454SAndrew Thompson 		k = ieee80211_crypto_encap(ni, m0);
261502ac6454SAndrew Thompson 		if (k == NULL) {
261602ac6454SAndrew Thompson 			m_freem(m0);
261702ac6454SAndrew Thompson 			return (ENOBUFS);
261802ac6454SAndrew Thompson 		}
261902ac6454SAndrew Thompson 		/* packet header may have moved, reset our local pointer */
262002ac6454SAndrew Thompson 		wh = mtod(m0, struct ieee80211_frame *);
262102ac6454SAndrew Thompson 	}
262202ac6454SAndrew Thompson 
262302ac6454SAndrew Thompson 	data->ni = ni;
262402ac6454SAndrew Thompson 	data->m = m0;
262502ac6454SAndrew Thompson 
262602ac6454SAndrew Thompson 	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
262702ac6454SAndrew Thompson 
262802ac6454SAndrew Thompson 	/* fill Tx descriptor */
262902ac6454SAndrew Thompson 	desc->len = htole16(totlen);
263002ac6454SAndrew Thompson 
263102ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
263202ac6454SAndrew Thompson 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
263302ac6454SAndrew Thompson 		if (totlen > vap->iv_rtsthreshold) {
263402ac6454SAndrew Thompson 			desc->flags |= ZYD_TX_FLAG_RTS;
263502ac6454SAndrew Thompson 		} else if (ZYD_RATE_IS_OFDM(rate) &&
263602ac6454SAndrew Thompson 		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
263702ac6454SAndrew Thompson 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
263802ac6454SAndrew Thompson 				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
263902ac6454SAndrew Thompson 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
264002ac6454SAndrew Thompson 				desc->flags |= ZYD_TX_FLAG_RTS;
264102ac6454SAndrew Thompson 		}
264202ac6454SAndrew Thompson 	}
264302ac6454SAndrew Thompson 
264402ac6454SAndrew Thompson 	if ((wh->i_fc[0] &
264502ac6454SAndrew Thompson 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
264602ac6454SAndrew Thompson 	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
264702ac6454SAndrew Thompson 		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
264802ac6454SAndrew Thompson 
264902ac6454SAndrew Thompson 	desc->phy = zyd_plcp_signal(rate);
265002ac6454SAndrew Thompson 	if (ZYD_RATE_IS_OFDM(rate)) {
265102ac6454SAndrew Thompson 		desc->phy |= ZYD_TX_PHY_OFDM;
265202ac6454SAndrew Thompson 		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
265302ac6454SAndrew Thompson 			desc->phy |= ZYD_TX_PHY_5GHZ;
265402ac6454SAndrew Thompson 	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
265502ac6454SAndrew Thompson 		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
265602ac6454SAndrew Thompson 
265702ac6454SAndrew Thompson 	/* actual transmit length (XXX why +10?) */
265802ac6454SAndrew Thompson 	pktlen = sizeof(struct zyd_tx_desc) + 10;
265902ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211)
266002ac6454SAndrew Thompson 		pktlen += totlen;
266102ac6454SAndrew Thompson 	desc->pktlen = htole16(pktlen);
266202ac6454SAndrew Thompson 
266302ac6454SAndrew Thompson 	desc->plcp_length = (16 * totlen + rate - 1) / rate;
266402ac6454SAndrew Thompson 	desc->plcp_service = 0;
266502ac6454SAndrew Thompson 	if (rate == 22) {
266602ac6454SAndrew Thompson 		const int remainder = (16 * totlen) % 22;
266702ac6454SAndrew Thompson 		if (remainder != 0 && remainder < 7)
266802ac6454SAndrew Thompson 			desc->plcp_service |= ZYD_PLCP_LENGEXT;
266902ac6454SAndrew Thompson 	}
267002ac6454SAndrew Thompson 
267102ac6454SAndrew Thompson 	DPRINTF(sc, ZYD_DEBUG_XMIT,
267202ac6454SAndrew Thompson 	    "%s: sending data frame len=%zu rate=%u\n",
267302ac6454SAndrew Thompson 	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
267402ac6454SAndrew Thompson 		rate);
267502ac6454SAndrew Thompson 
267602ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
267702ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
267802ac6454SAndrew Thompson 
267902ac6454SAndrew Thompson 	return (0);
268002ac6454SAndrew Thompson }
268102ac6454SAndrew Thompson 
268202ac6454SAndrew Thompson static void
268302ac6454SAndrew Thompson zyd_start(struct ifnet *ifp)
268402ac6454SAndrew Thompson {
268502ac6454SAndrew Thompson 	struct zyd_softc *sc = ifp->if_softc;
268602ac6454SAndrew Thompson 	struct ieee80211_node *ni;
268702ac6454SAndrew Thompson 	struct mbuf *m;
268802ac6454SAndrew Thompson 
268902ac6454SAndrew Thompson 	ZYD_LOCK(sc);
269002ac6454SAndrew Thompson 	for (;;) {
269102ac6454SAndrew Thompson 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
269202ac6454SAndrew Thompson 		if (m == NULL)
269302ac6454SAndrew Thompson 			break;
269402ac6454SAndrew Thompson 		if (sc->tx_nfree == 0) {
269502ac6454SAndrew Thompson 			IFQ_DRV_PREPEND(&ifp->if_snd, m);
269602ac6454SAndrew Thompson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
269702ac6454SAndrew Thompson 			break;
269802ac6454SAndrew Thompson 		}
269902ac6454SAndrew Thompson 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
270002ac6454SAndrew Thompson 		m = ieee80211_encap(ni, m);
270102ac6454SAndrew Thompson 		if (m == NULL) {
270202ac6454SAndrew Thompson 			ieee80211_free_node(ni);
270302ac6454SAndrew Thompson 			ifp->if_oerrors++;
270402ac6454SAndrew Thompson 			continue;
270502ac6454SAndrew Thompson 		}
270602ac6454SAndrew Thompson 		if (zyd_tx_data(sc, m, ni) != 0) {
270702ac6454SAndrew Thompson 			ieee80211_free_node(ni);
270802ac6454SAndrew Thompson 			ifp->if_oerrors++;
270902ac6454SAndrew Thompson 			break;
271002ac6454SAndrew Thompson 		}
271102ac6454SAndrew Thompson 	}
271202ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
271302ac6454SAndrew Thompson }
271402ac6454SAndrew Thompson 
271502ac6454SAndrew Thompson static int
271602ac6454SAndrew Thompson zyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
271702ac6454SAndrew Thompson 	const struct ieee80211_bpf_params *params)
271802ac6454SAndrew Thompson {
271902ac6454SAndrew Thompson 	struct ieee80211com *ic = ni->ni_ic;
272002ac6454SAndrew Thompson 	struct ifnet *ifp = ic->ic_ifp;
272102ac6454SAndrew Thompson 	struct zyd_softc *sc = ifp->if_softc;
272202ac6454SAndrew Thompson 
272302ac6454SAndrew Thompson 	ZYD_LOCK(sc);
272402ac6454SAndrew Thompson 	/* prevent management frames from being sent if we're not ready */
272502ac6454SAndrew Thompson 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
272602ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
272702ac6454SAndrew Thompson 		m_freem(m);
272802ac6454SAndrew Thompson 		ieee80211_free_node(ni);
272902ac6454SAndrew Thompson 		return (ENETDOWN);
273002ac6454SAndrew Thompson 	}
273102ac6454SAndrew Thompson 	if (sc->tx_nfree == 0) {
273202ac6454SAndrew Thompson 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
273302ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
273402ac6454SAndrew Thompson 		m_freem(m);
273502ac6454SAndrew Thompson 		ieee80211_free_node(ni);
273602ac6454SAndrew Thompson 		return (ENOBUFS);		/* XXX */
273702ac6454SAndrew Thompson 	}
273802ac6454SAndrew Thompson 
273902ac6454SAndrew Thompson 	/*
274002ac6454SAndrew Thompson 	 * Legacy path; interpret frame contents to decide
274102ac6454SAndrew Thompson 	 * precisely how to send the frame.
274202ac6454SAndrew Thompson 	 * XXX raw path
274302ac6454SAndrew Thompson 	 */
274402ac6454SAndrew Thompson 	if (zyd_tx_mgt(sc, m, ni) != 0) {
274502ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
274602ac6454SAndrew Thompson 		ifp->if_oerrors++;
274702ac6454SAndrew Thompson 		ieee80211_free_node(ni);
274802ac6454SAndrew Thompson 		return (EIO);
274902ac6454SAndrew Thompson 	}
275002ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
275102ac6454SAndrew Thompson 	return (0);
275202ac6454SAndrew Thompson }
275302ac6454SAndrew Thompson 
275402ac6454SAndrew Thompson static int
275502ac6454SAndrew Thompson zyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
275602ac6454SAndrew Thompson {
275702ac6454SAndrew Thompson 	struct zyd_softc *sc = ifp->if_softc;
275802ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
275902ac6454SAndrew Thompson 	struct ifreq *ifr = (struct ifreq *) data;
276002ac6454SAndrew Thompson 	int error = 0, startall = 0;
276102ac6454SAndrew Thompson 
276202ac6454SAndrew Thompson 	switch (cmd) {
276302ac6454SAndrew Thompson 	case SIOCSIFFLAGS:
276402ac6454SAndrew Thompson 		ZYD_LOCK(sc);
276502ac6454SAndrew Thompson 		if (ifp->if_flags & IFF_UP) {
276602ac6454SAndrew Thompson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
276777ddf3d3SAndrew Thompson 				zyd_queue_command(sc, zyd_multitask,
276877ddf3d3SAndrew Thompson 				    &sc->sc_mcasttask[0].hdr,
276977ddf3d3SAndrew Thompson 				    &sc->sc_mcasttask[1].hdr);
277002ac6454SAndrew Thompson 			} else {
277102ac6454SAndrew Thompson 				zyd_queue_command(sc, zyd_init_task,
277202ac6454SAndrew Thompson 				    &sc->sc_synctask[0].hdr,
277302ac6454SAndrew Thompson 				    &sc->sc_synctask[1].hdr);
277402ac6454SAndrew Thompson 				startall = 1;
277502ac6454SAndrew Thompson 			}
277602ac6454SAndrew Thompson 		} else {
277702ac6454SAndrew Thompson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
277802ac6454SAndrew Thompson 				zyd_queue_command(sc, zyd_stop_task,
277902ac6454SAndrew Thompson 				    &sc->sc_synctask[0].hdr,
278002ac6454SAndrew Thompson 				    &sc->sc_synctask[1].hdr);
278102ac6454SAndrew Thompson 			}
278202ac6454SAndrew Thompson 		}
278302ac6454SAndrew Thompson 		ZYD_UNLOCK(sc);
278402ac6454SAndrew Thompson 		if (startall)
278502ac6454SAndrew Thompson 			ieee80211_start_all(ic);
278602ac6454SAndrew Thompson 		break;
278702ac6454SAndrew Thompson 	case SIOCGIFMEDIA:
278802ac6454SAndrew Thompson 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
278902ac6454SAndrew Thompson 		break;
279002ac6454SAndrew Thompson 	case SIOCGIFADDR:
279102ac6454SAndrew Thompson 		error = ether_ioctl(ifp, cmd, data);
279202ac6454SAndrew Thompson 		break;
279302ac6454SAndrew Thompson 	default:
279402ac6454SAndrew Thompson 		error = EINVAL;
279502ac6454SAndrew Thompson 		break;
279602ac6454SAndrew Thompson 	}
279702ac6454SAndrew Thompson 	return (error);
279802ac6454SAndrew Thompson }
279902ac6454SAndrew Thompson 
280002ac6454SAndrew Thompson static void
280102ac6454SAndrew Thompson zyd_init_task(struct usb2_proc_msg *pm)
280202ac6454SAndrew Thompson {
280302ac6454SAndrew Thompson 	struct zyd_task *task = (struct zyd_task *)pm;
280402ac6454SAndrew Thompson 	struct zyd_softc *sc = task->sc;
280502ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
280602ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
280702ac6454SAndrew Thompson 	struct usb2_config_descriptor *cd;
280802ac6454SAndrew Thompson 	int error;
280902ac6454SAndrew Thompson 	uint32_t val;
281002ac6454SAndrew Thompson 
281102ac6454SAndrew Thompson 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
281202ac6454SAndrew Thompson 
281302ac6454SAndrew Thompson 	if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
281402ac6454SAndrew Thompson 		error = zyd_loadfirmware(sc);
281502ac6454SAndrew Thompson 		if (error != 0) {
281602ac6454SAndrew Thompson 			device_printf(sc->sc_dev,
281702ac6454SAndrew Thompson 			    "could not load firmware (error=%d)\n", error);
281802ac6454SAndrew Thompson 			goto fail;
281902ac6454SAndrew Thompson 		}
282002ac6454SAndrew Thompson 
282102ac6454SAndrew Thompson 		/* reset device */
282202ac6454SAndrew Thompson 		cd = usb2_get_config_descriptor(sc->sc_udev);
282302ac6454SAndrew Thompson 		error = usb2_req_set_config(sc->sc_udev, &sc->sc_mtx,
282402ac6454SAndrew Thompson 		    cd->bConfigurationValue);
282502ac6454SAndrew Thompson 		if (error)
282602ac6454SAndrew Thompson 			device_printf(sc->sc_dev, "reset failed, continuing\n");
282702ac6454SAndrew Thompson 
282802ac6454SAndrew Thompson 		error = zyd_hw_init(sc);
282902ac6454SAndrew Thompson 		if (error) {
283002ac6454SAndrew Thompson 			device_printf(sc->sc_dev,
283102ac6454SAndrew Thompson 			    "hardware initialization failed\n");
283202ac6454SAndrew Thompson 			goto fail;
283302ac6454SAndrew Thompson 		}
283402ac6454SAndrew Thompson 
283502ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
283602ac6454SAndrew Thompson 		    "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
283702ac6454SAndrew Thompson 		    "BE%x NP%x Gain%x F%x\n",
283802ac6454SAndrew Thompson 		    (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
283902ac6454SAndrew Thompson 		    sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
284002ac6454SAndrew Thompson 		    zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
284102ac6454SAndrew Thompson 		    sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
284202ac6454SAndrew Thompson 		    sc->sc_cckgain, sc->sc_fix_cr157);
284302ac6454SAndrew Thompson 
284402ac6454SAndrew Thompson 		/* read regulatory domain (currently unused) */
284502ac6454SAndrew Thompson 		zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
284602ac6454SAndrew Thompson 		sc->sc_regdomain = val >> 16;
284702ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
284802ac6454SAndrew Thompson 		    sc->sc_regdomain);
284902ac6454SAndrew Thompson 
285002ac6454SAndrew Thompson 		/* we'll do software WEP decryption for now */
285102ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
285202ac6454SAndrew Thompson 		    __func__);
285302ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
285402ac6454SAndrew Thompson 
285502ac6454SAndrew Thompson 		sc->sc_flags |= ZYD_FLAG_INITONCE;
285602ac6454SAndrew Thompson 	}
285702ac6454SAndrew Thompson 
285802ac6454SAndrew Thompson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
285902ac6454SAndrew Thompson 		zyd_stop_task(pm);
286002ac6454SAndrew Thompson 
286129aca940SSam Leffler 	DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
286229aca940SSam Leffler 	    IF_LLADDR(ifp), ":");
286329aca940SSam Leffler 	error = zyd_set_macaddr(sc, IF_LLADDR(ifp));
286402ac6454SAndrew Thompson 	if (error != 0)
286502ac6454SAndrew Thompson 		return;
286602ac6454SAndrew Thompson 
286702ac6454SAndrew Thompson 	/* set basic rates */
286802ac6454SAndrew Thompson 	if (ic->ic_curmode == IEEE80211_MODE_11B)
286902ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
287002ac6454SAndrew Thompson 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
287102ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
287202ac6454SAndrew Thompson 	else	/* assumes 802.11b/g */
287302ac6454SAndrew Thompson 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
287402ac6454SAndrew Thompson 
287502ac6454SAndrew Thompson 	/* promiscuous mode */
287602ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
287702ac6454SAndrew Thompson 	/* multicast setup */
287802ac6454SAndrew Thompson 	zyd_set_multi(sc);
287902ac6454SAndrew Thompson 	/* set RX filter  */
288002ac6454SAndrew Thompson 	error = zyd_set_rxfilter(sc);
288102ac6454SAndrew Thompson 	if (error != 0)
288202ac6454SAndrew Thompson 		goto fail;
288302ac6454SAndrew Thompson 
288402ac6454SAndrew Thompson 	/* switch radio transmitter ON */
288502ac6454SAndrew Thompson 	error = zyd_switch_radio(sc, 1);
288602ac6454SAndrew Thompson 	if (error != 0)
288702ac6454SAndrew Thompson 		goto fail;
288802ac6454SAndrew Thompson 	/* set default BSS channel */
288902ac6454SAndrew Thompson 	zyd_set_chan(sc, ic->ic_curchan);
289002ac6454SAndrew Thompson 
289102ac6454SAndrew Thompson 	/*
289202ac6454SAndrew Thompson 	 * Allocate Tx and Rx xfer queues.
289302ac6454SAndrew Thompson 	 */
289402ac6454SAndrew Thompson 	zyd_setup_tx_list(sc);
289502ac6454SAndrew Thompson 
289602ac6454SAndrew Thompson 	/* enable interrupts */
289702ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
289802ac6454SAndrew Thompson 
289902ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
290002ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
290177ddf3d3SAndrew Thompson 	usb2_transfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
290202ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
290302ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
290402ac6454SAndrew Thompson 
290502ac6454SAndrew Thompson 	return;
290602ac6454SAndrew Thompson 
290702ac6454SAndrew Thompson fail:	zyd_stop_task(pm);
290802ac6454SAndrew Thompson 	return;
290902ac6454SAndrew Thompson }
291002ac6454SAndrew Thompson 
291102ac6454SAndrew Thompson static void
291202ac6454SAndrew Thompson zyd_init(void *priv)
291302ac6454SAndrew Thompson {
291402ac6454SAndrew Thompson 	struct zyd_softc *sc = priv;
291502ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
291602ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
291702ac6454SAndrew Thompson 
291802ac6454SAndrew Thompson 	ZYD_LOCK(sc);
291902ac6454SAndrew Thompson 	zyd_queue_command(sc, zyd_init_task,
292002ac6454SAndrew Thompson 	    &sc->sc_synctask[0].hdr,
292102ac6454SAndrew Thompson 	    &sc->sc_synctask[1].hdr);
292202ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
292302ac6454SAndrew Thompson 
292402ac6454SAndrew Thompson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
292502ac6454SAndrew Thompson 		ieee80211_start_all(ic);		/* start all vap's */
292602ac6454SAndrew Thompson }
292702ac6454SAndrew Thompson 
292802ac6454SAndrew Thompson static void
292902ac6454SAndrew Thompson zyd_stop_task(struct usb2_proc_msg *pm)
293002ac6454SAndrew Thompson {
293102ac6454SAndrew Thompson 	struct zyd_task *task = (struct zyd_task *)pm;
293202ac6454SAndrew Thompson 	struct zyd_softc *sc = task->sc;
293302ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
293402ac6454SAndrew Thompson 	int error;
293502ac6454SAndrew Thompson 
293602ac6454SAndrew Thompson 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
293702ac6454SAndrew Thompson 
293802ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
293902ac6454SAndrew Thompson 
294002ac6454SAndrew Thompson 	/*
294102ac6454SAndrew Thompson 	 * Drain all the transfers, if not already drained:
294202ac6454SAndrew Thompson 	 */
294302ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
294402ac6454SAndrew Thompson 	usb2_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
294502ac6454SAndrew Thompson 	usb2_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
294602ac6454SAndrew Thompson 	ZYD_LOCK(sc);
294702ac6454SAndrew Thompson 
294802ac6454SAndrew Thompson 	zyd_unsetup_tx_list(sc);
294902ac6454SAndrew Thompson 
295002ac6454SAndrew Thompson 	/* Stop now if the device was never set up */
295102ac6454SAndrew Thompson 	if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
295202ac6454SAndrew Thompson 		return;
295302ac6454SAndrew Thompson 
295402ac6454SAndrew Thompson 	/* switch radio transmitter OFF */
295502ac6454SAndrew Thompson 	error = zyd_switch_radio(sc, 0);
295602ac6454SAndrew Thompson 	if (error != 0)
295702ac6454SAndrew Thompson 		goto fail;
295802ac6454SAndrew Thompson 	/* disable Rx */
295902ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
296002ac6454SAndrew Thompson 	/* disable interrupts */
296102ac6454SAndrew Thompson 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
296202ac6454SAndrew Thompson 
296302ac6454SAndrew Thompson fail:
296402ac6454SAndrew Thompson 	return;
296502ac6454SAndrew Thompson }
296602ac6454SAndrew Thompson 
296702ac6454SAndrew Thompson static int
296802ac6454SAndrew Thompson zyd_loadfirmware(struct zyd_softc *sc)
296902ac6454SAndrew Thompson {
297002ac6454SAndrew Thompson 	struct usb2_device_request req;
297102ac6454SAndrew Thompson 	size_t size;
297202ac6454SAndrew Thompson 	u_char *fw;
297302ac6454SAndrew Thompson 	uint8_t stat;
297402ac6454SAndrew Thompson 	uint16_t addr;
297502ac6454SAndrew Thompson 
297602ac6454SAndrew Thompson 	if (sc->sc_flags & ZYD_FLAG_FWLOADED)
297702ac6454SAndrew Thompson 		return (0);
297802ac6454SAndrew Thompson 
297902ac6454SAndrew Thompson 	if (sc->sc_macrev == ZYD_ZD1211) {
298002ac6454SAndrew Thompson 		fw = (u_char *)zd1211_firmware;
298102ac6454SAndrew Thompson 		size = sizeof(zd1211_firmware);
298202ac6454SAndrew Thompson 	} else {
298302ac6454SAndrew Thompson 		fw = (u_char *)zd1211b_firmware;
298402ac6454SAndrew Thompson 		size = sizeof(zd1211b_firmware);
298502ac6454SAndrew Thompson 	}
298602ac6454SAndrew Thompson 
298702ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
298802ac6454SAndrew Thompson 	req.bRequest = ZYD_DOWNLOADREQ;
298902ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
299002ac6454SAndrew Thompson 
299102ac6454SAndrew Thompson 	addr = ZYD_FIRMWARE_START_ADDR;
299202ac6454SAndrew Thompson 	while (size > 0) {
299302ac6454SAndrew Thompson 		/*
299402ac6454SAndrew Thompson 		 * When the transfer size is 4096 bytes, it is not
299502ac6454SAndrew Thompson 		 * likely to be able to transfer it.
299602ac6454SAndrew Thompson 		 * The cause is port or machine or chip?
299702ac6454SAndrew Thompson 		 */
299802ac6454SAndrew Thompson 		const int mlen = min(size, 64);
299902ac6454SAndrew Thompson 
300002ac6454SAndrew Thompson 		DPRINTF(sc, ZYD_DEBUG_FW,
300102ac6454SAndrew Thompson 		    "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
300202ac6454SAndrew Thompson 
300302ac6454SAndrew Thompson 		USETW(req.wValue, addr);
300402ac6454SAndrew Thompson 		USETW(req.wLength, mlen);
300502ac6454SAndrew Thompson 		if (zyd_do_request(sc, &req, fw) != 0)
300602ac6454SAndrew Thompson 			return (EIO);
300702ac6454SAndrew Thompson 
300802ac6454SAndrew Thompson 		addr += mlen / 2;
300902ac6454SAndrew Thompson 		fw   += mlen;
301002ac6454SAndrew Thompson 		size -= mlen;
301102ac6454SAndrew Thompson 	}
301202ac6454SAndrew Thompson 
301302ac6454SAndrew Thompson 	/* check whether the upload succeeded */
301402ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
301502ac6454SAndrew Thompson 	req.bRequest = ZYD_DOWNLOADSTS;
301602ac6454SAndrew Thompson 	USETW(req.wValue, 0);
301702ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
301802ac6454SAndrew Thompson 	USETW(req.wLength, sizeof(stat));
301902ac6454SAndrew Thompson 	if (zyd_do_request(sc, &req, &stat) != 0)
302002ac6454SAndrew Thompson 		return (EIO);
302102ac6454SAndrew Thompson 
302202ac6454SAndrew Thompson 	sc->sc_flags |= ZYD_FLAG_FWLOADED;
302302ac6454SAndrew Thompson 
302402ac6454SAndrew Thompson 	return (stat & 0x80) ? (EIO) : (0);
302502ac6454SAndrew Thompson }
302602ac6454SAndrew Thompson 
302702ac6454SAndrew Thompson static void
302802ac6454SAndrew Thompson zyd_newassoc(struct ieee80211_node *ni, int isnew)
302902ac6454SAndrew Thompson {
303002ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
303102ac6454SAndrew Thompson 
303202ac6454SAndrew Thompson 	ieee80211_amrr_node_init(&ZYD_VAP(vap)->amrr, &ZYD_NODE(ni)->amn, ni);
303302ac6454SAndrew Thompson }
303402ac6454SAndrew Thompson 
303502ac6454SAndrew Thompson static void
303602ac6454SAndrew Thompson zyd_scan_start(struct ieee80211com *ic)
303702ac6454SAndrew Thompson {
303802ac6454SAndrew Thompson 	struct zyd_softc *sc = ic->ic_ifp->if_softc;
303902ac6454SAndrew Thompson 
304002ac6454SAndrew Thompson 	ZYD_LOCK(sc);
304102ac6454SAndrew Thompson 	/* do it in a process context */
304202ac6454SAndrew Thompson 	sc->sc_scan_action = ZYD_SCAN_START;
304302ac6454SAndrew Thompson 	zyd_queue_command(sc, zyd_scantask,
304402ac6454SAndrew Thompson 	    &sc->sc_scantask[0].hdr, &sc->sc_scantask[1].hdr);
304502ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
304602ac6454SAndrew Thompson }
304702ac6454SAndrew Thompson 
304802ac6454SAndrew Thompson static void
304902ac6454SAndrew Thompson zyd_scan_end(struct ieee80211com *ic)
305002ac6454SAndrew Thompson {
305102ac6454SAndrew Thompson 	struct zyd_softc *sc = ic->ic_ifp->if_softc;
305202ac6454SAndrew Thompson 
305302ac6454SAndrew Thompson 	ZYD_LOCK(sc);
305402ac6454SAndrew Thompson 	/* do it in a process context */
305502ac6454SAndrew Thompson 	sc->sc_scan_action = ZYD_SCAN_END;
305602ac6454SAndrew Thompson 	zyd_queue_command(sc, zyd_scantask,
305702ac6454SAndrew Thompson 	    &sc->sc_scantask[0].hdr, &sc->sc_scantask[1].hdr);
305802ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
305902ac6454SAndrew Thompson }
306002ac6454SAndrew Thompson 
306102ac6454SAndrew Thompson static void
306202ac6454SAndrew Thompson zyd_set_channel(struct ieee80211com *ic)
306302ac6454SAndrew Thompson {
306402ac6454SAndrew Thompson 	struct zyd_softc *sc = ic->ic_ifp->if_softc;
306502ac6454SAndrew Thompson 
306602ac6454SAndrew Thompson 	ZYD_LOCK(sc);
306702ac6454SAndrew Thompson 	/* do it in a process context */
306802ac6454SAndrew Thompson 	sc->sc_scan_action = ZYD_SET_CHANNEL;
306902ac6454SAndrew Thompson 	zyd_queue_command(sc, zyd_scantask,
307002ac6454SAndrew Thompson 	    &sc->sc_scantask[0].hdr, &sc->sc_scantask[1].hdr);
307102ac6454SAndrew Thompson 	ZYD_UNLOCK(sc);
307202ac6454SAndrew Thompson }
307302ac6454SAndrew Thompson 
307402ac6454SAndrew Thompson static void
307502ac6454SAndrew Thompson zyd_scantask(struct usb2_proc_msg *pm)
307602ac6454SAndrew Thompson {
307702ac6454SAndrew Thompson 	struct zyd_task *task = (struct zyd_task *)pm;
307802ac6454SAndrew Thompson 	struct zyd_softc *sc = task->sc;
307902ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
308002ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
308102ac6454SAndrew Thompson 
308202ac6454SAndrew Thompson 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
308302ac6454SAndrew Thompson 
308402ac6454SAndrew Thompson 	switch (sc->sc_scan_action) {
308502ac6454SAndrew Thompson 	case ZYD_SCAN_START:
308602ac6454SAndrew Thompson 		/* want broadcast address while scanning */
308702ac6454SAndrew Thompson                 zyd_set_bssid(sc, ifp->if_broadcastaddr);
308802ac6454SAndrew Thompson                 break;
308902ac6454SAndrew Thompson 
309002ac6454SAndrew Thompson         case ZYD_SET_CHANNEL:
309102ac6454SAndrew Thompson                 zyd_set_chan(sc, ic->ic_curchan);
309202ac6454SAndrew Thompson                 break;
309302ac6454SAndrew Thompson 
309402ac6454SAndrew Thompson         default: /* ZYD_SCAN_END */
309502ac6454SAndrew Thompson 		/* restore previous bssid */
309602ac6454SAndrew Thompson                 zyd_set_bssid(sc, sc->sc_bssid);
309702ac6454SAndrew Thompson                 break;
309802ac6454SAndrew Thompson 	}
309902ac6454SAndrew Thompson }
310002ac6454SAndrew Thompson 
310102ac6454SAndrew Thompson static void
310277ddf3d3SAndrew Thompson zyd_command_wrapper(struct usb2_proc_msg *pm)
310377ddf3d3SAndrew Thompson {
310477ddf3d3SAndrew Thompson 	struct zyd_task *task = (struct zyd_task *)pm;
310577ddf3d3SAndrew Thompson 	struct zyd_softc *sc = task->sc;
310677ddf3d3SAndrew Thompson 	struct ifnet *ifp;
310777ddf3d3SAndrew Thompson 
310877ddf3d3SAndrew Thompson 	/* wait for pending transfer, if any */
310977ddf3d3SAndrew Thompson 	while (usb2_transfer_pending(sc->sc_xfer[ZYD_BULK_WR]))
311077ddf3d3SAndrew Thompson 		cv_wait(&sc->sc_cmd_cv, &sc->sc_mtx);
311177ddf3d3SAndrew Thompson 
311277ddf3d3SAndrew Thompson 	/* make sure any hardware FIFOs are emptied */
311377ddf3d3SAndrew Thompson 	usb2_pause_mtx(&sc->sc_mtx, hz / 1000);
311477ddf3d3SAndrew Thompson 
311577ddf3d3SAndrew Thompson 	/* execute task */
311677ddf3d3SAndrew Thompson 	task->func(pm);
311777ddf3d3SAndrew Thompson 
311877ddf3d3SAndrew Thompson 	/* check if this is the last task executed */
311977ddf3d3SAndrew Thompson 	if (sc->sc_last_task == task) {
312077ddf3d3SAndrew Thompson 		sc->sc_last_task = NULL;
312177ddf3d3SAndrew Thompson 		ifp = sc->sc_ifp;
312277ddf3d3SAndrew Thompson 		/* re-start TX, if any */
312377ddf3d3SAndrew Thompson 		if ((ifp != NULL) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
312477ddf3d3SAndrew Thompson 			usb2_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
312577ddf3d3SAndrew Thompson 	}
312677ddf3d3SAndrew Thompson }
312777ddf3d3SAndrew Thompson 
312877ddf3d3SAndrew Thompson static void
312902ac6454SAndrew Thompson zyd_queue_command(struct zyd_softc *sc, usb2_proc_callback_t *fn,
313002ac6454SAndrew Thompson     struct usb2_proc_msg *t0, struct usb2_proc_msg *t1)
313102ac6454SAndrew Thompson {
313202ac6454SAndrew Thompson 	struct zyd_task *task;
313302ac6454SAndrew Thompson 
313402ac6454SAndrew Thompson 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
313502ac6454SAndrew Thompson 
313602ac6454SAndrew Thompson 	/*
313702ac6454SAndrew Thompson 	 * NOTE: The task cannot get executed before we drop the
313802ac6454SAndrew Thompson 	 * "sc_mtx" mutex. It is safe to update fields in the message
313902ac6454SAndrew Thompson 	 * structure after that the message got queued.
314002ac6454SAndrew Thompson 	 */
314102ac6454SAndrew Thompson 	task = (struct zyd_task *)
314202ac6454SAndrew Thompson 	  usb2_proc_msignal(&sc->sc_tq, t0, t1);
314302ac6454SAndrew Thompson 
314402ac6454SAndrew Thompson 	/* Setup callback and softc pointers */
314577ddf3d3SAndrew Thompson 	task->hdr.pm_callback = zyd_command_wrapper;
314677ddf3d3SAndrew Thompson 	task->func = fn;
314702ac6454SAndrew Thompson 	task->sc = sc;
314802ac6454SAndrew Thompson 
314977ddf3d3SAndrew Thompson 	/* Make sure that any TX operation will stop */
315077ddf3d3SAndrew Thompson 	sc->sc_last_task = task;
315177ddf3d3SAndrew Thompson 
315202ac6454SAndrew Thompson 	/*
315302ac6454SAndrew Thompson 	 * Init and stop must be synchronous!
315402ac6454SAndrew Thompson 	 */
315577ddf3d3SAndrew Thompson 	if ((fn == zyd_init_task) || (fn == zyd_stop_task) ||
315677ddf3d3SAndrew Thompson 	    (fn == zyd_flush_task))
315702ac6454SAndrew Thompson 		usb2_proc_mwait(&sc->sc_tq, t0, t1);
315802ac6454SAndrew Thompson }
315902ac6454SAndrew Thompson 
316002ac6454SAndrew Thompson static device_method_t zyd_methods[] = {
316102ac6454SAndrew Thompson         /* Device interface */
316202ac6454SAndrew Thompson         DEVMETHOD(device_probe, zyd_match),
316302ac6454SAndrew Thompson         DEVMETHOD(device_attach, zyd_attach),
316402ac6454SAndrew Thompson         DEVMETHOD(device_detach, zyd_detach),
316502ac6454SAndrew Thompson 
316602ac6454SAndrew Thompson 	{ 0, 0 }
316702ac6454SAndrew Thompson };
316802ac6454SAndrew Thompson 
316902ac6454SAndrew Thompson static driver_t zyd_driver = {
317002ac6454SAndrew Thompson         "zyd",
317102ac6454SAndrew Thompson         zyd_methods,
317202ac6454SAndrew Thompson         sizeof(struct zyd_softc)
317302ac6454SAndrew Thompson };
317402ac6454SAndrew Thompson 
317502ac6454SAndrew Thompson static devclass_t zyd_devclass;
317602ac6454SAndrew Thompson 
31779aef556dSAndrew Thompson DRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0);
317802ac6454SAndrew Thompson MODULE_DEPEND(zyd, usb, 1, 1, 1);
317902ac6454SAndrew Thompson MODULE_DEPEND(zyd, wlan, 1, 1, 1);
318002ac6454SAndrew Thompson MODULE_DEPEND(zyd, wlan_amrr, 1, 1, 1);
3181