if_zyd.c revision 314222
1184610Salfred/*	$OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $	*/
2184610Salfred/*	$NetBSD: if_zyd.c,v 1.7 2007/06/21 04:04:29 kiyohara Exp $	*/
3184610Salfred/*	$FreeBSD: stable/11/sys/dev/usb/wlan/if_zyd.c 314222 2017-02-24 21:19:40Z avos $	*/
4184610Salfred
5184610Salfred/*-
6184610Salfred * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr>
7184610Salfred * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de>
8184610Salfred *
9184610Salfred * Permission to use, copy, modify, and distribute this software for any
10184610Salfred * purpose with or without fee is hereby granted, provided that the above
11184610Salfred * copyright notice and this permission notice appear in all copies.
12184610Salfred *
13184610Salfred * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14184610Salfred * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15184610Salfred * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16184610Salfred * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17184610Salfred * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18184610Salfred * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19184610Salfred * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20184610Salfred */
21184610Salfred
22184610Salfred#include <sys/cdefs.h>
23184610Salfred__FBSDID("$FreeBSD: stable/11/sys/dev/usb/wlan/if_zyd.c 314222 2017-02-24 21:19:40Z avos $");
24184610Salfred
25184610Salfred/*
26188417Sthompsa * ZyDAS ZD1211/ZD1211B USB WLAN driver.
27184610Salfred */
28184610Salfred
29191746Sthompsa#include <sys/param.h>
30191746Sthompsa#include <sys/sockio.h>
31191746Sthompsa#include <sys/sysctl.h>
32191746Sthompsa#include <sys/lock.h>
33191746Sthompsa#include <sys/mutex.h>
34194677Sthompsa#include <sys/condvar.h>
35191746Sthompsa#include <sys/mbuf.h>
36191746Sthompsa#include <sys/kernel.h>
37191746Sthompsa#include <sys/socket.h>
38191746Sthompsa#include <sys/systm.h>
39191746Sthompsa#include <sys/malloc.h>
40191746Sthompsa#include <sys/module.h>
41191746Sthompsa#include <sys/bus.h>
42191746Sthompsa#include <sys/endian.h>
43191746Sthompsa#include <sys/kdb.h>
44191746Sthompsa
45191746Sthompsa#include <machine/bus.h>
46191746Sthompsa#include <machine/resource.h>
47191746Sthompsa#include <sys/rman.h>
48191746Sthompsa
49191746Sthompsa#include <net/bpf.h>
50191746Sthompsa#include <net/if.h>
51257176Sglebius#include <net/if_var.h>
52191746Sthompsa#include <net/if_arp.h>
53191746Sthompsa#include <net/ethernet.h>
54191746Sthompsa#include <net/if_dl.h>
55191746Sthompsa#include <net/if_media.h>
56191746Sthompsa#include <net/if_types.h>
57191746Sthompsa
58191746Sthompsa#ifdef INET
59191746Sthompsa#include <netinet/in.h>
60191746Sthompsa#include <netinet/in_systm.h>
61191746Sthompsa#include <netinet/in_var.h>
62191746Sthompsa#include <netinet/if_ether.h>
63191746Sthompsa#include <netinet/ip.h>
64191746Sthompsa#endif
65191746Sthompsa
66191746Sthompsa#include <net80211/ieee80211_var.h>
67191746Sthompsa#include <net80211/ieee80211_regdomain.h>
68191746Sthompsa#include <net80211/ieee80211_radiotap.h>
69206358Srpaulo#include <net80211/ieee80211_ratectl.h>
70191746Sthompsa
71188942Sthompsa#include <dev/usb/usb.h>
72194677Sthompsa#include <dev/usb/usbdi.h>
73194677Sthompsa#include <dev/usb/usbdi_util.h>
74191746Sthompsa#include "usbdevs.h"
75184610Salfred
76188942Sthompsa#include <dev/usb/wlan/if_zydreg.h>
77188942Sthompsa#include <dev/usb/wlan/if_zydfw.h>
78184610Salfred
79207077Sthompsa#ifdef USB_DEBUG
80184610Salfredstatic int zyd_debug = 0;
81184610Salfred
82227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, zyd, CTLFLAG_RW, 0, "USB zyd");
83276701ShselaskySYSCTL_INT(_hw_usb_zyd, OID_AUTO, debug, CTLFLAG_RWTUN, &zyd_debug, 0,
84184610Salfred    "zyd debug level");
85188417Sthompsa
86188417Sthompsaenum {
87188417Sthompsa	ZYD_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
88188417Sthompsa	ZYD_DEBUG_RECV		= 0x00000002,	/* basic recv operation */
89188417Sthompsa	ZYD_DEBUG_RESET		= 0x00000004,	/* reset processing */
90188417Sthompsa	ZYD_DEBUG_INIT		= 0x00000008,	/* device init */
91188417Sthompsa	ZYD_DEBUG_TX_PROC	= 0x00000010,	/* tx ISR proc */
92188417Sthompsa	ZYD_DEBUG_RX_PROC	= 0x00000020,	/* rx ISR proc */
93188417Sthompsa	ZYD_DEBUG_STATE		= 0x00000040,	/* 802.11 state transitions */
94188417Sthompsa	ZYD_DEBUG_STAT		= 0x00000080,	/* statistic */
95188417Sthompsa	ZYD_DEBUG_FW		= 0x00000100,	/* firmware */
96188417Sthompsa	ZYD_DEBUG_CMD		= 0x00000200,	/* fw commands */
97188417Sthompsa	ZYD_DEBUG_ANY		= 0xffffffff
98188417Sthompsa};
99188417Sthompsa#define	DPRINTF(sc, m, fmt, ...) do {				\
100188419Sthompsa	if (zyd_debug & (m))					\
101188417Sthompsa		printf("%s: " fmt, __func__, ## __VA_ARGS__);	\
102188417Sthompsa} while (0)
103188417Sthompsa#else
104188417Sthompsa#define	DPRINTF(sc, m, fmt, ...) do {				\
105188417Sthompsa	(void) sc;						\
106188417Sthompsa} while (0)
107184610Salfred#endif
108184610Salfred
109188419Sthompsa#define	zyd_do_request(sc,req,data) \
110194228Sthompsa    usbd_do_request_flags((sc)->sc_udev, &(sc)->sc_mtx, req, data, 0, NULL, 5000)
111188419Sthompsa
112188417Sthompsastatic device_probe_t zyd_match;
113184610Salfredstatic device_attach_t zyd_attach;
114184610Salfredstatic device_detach_t zyd_detach;
115184610Salfred
116193045Sthompsastatic usb_callback_t zyd_intr_read_callback;
117193045Sthompsastatic usb_callback_t zyd_intr_write_callback;
118193045Sthompsastatic usb_callback_t zyd_bulk_read_callback;
119193045Sthompsastatic usb_callback_t zyd_bulk_write_callback;
120184610Salfred
121185948Sthompsastatic struct ieee80211vap *zyd_vap_create(struct ieee80211com *,
122228621Sbschmidt		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
123228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN],
124228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN]);
125185948Sthompsastatic void	zyd_vap_delete(struct ieee80211vap *);
126188417Sthompsastatic void	zyd_tx_free(struct zyd_tx_data *, int);
127188419Sthompsastatic void	zyd_setup_tx_list(struct zyd_softc *);
128188419Sthompsastatic void	zyd_unsetup_tx_list(struct zyd_softc *);
129188417Sthompsastatic int	zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int);
130188417Sthompsastatic int	zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
131188601Sthompsa		    void *, int, int);
132188417Sthompsastatic int	zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
133188417Sthompsastatic int	zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
134188417Sthompsastatic int	zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
135188417Sthompsastatic int	zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
136188417Sthompsastatic int	zyd_rfwrite(struct zyd_softc *, uint32_t);
137188417Sthompsastatic int	zyd_lock_phy(struct zyd_softc *);
138188417Sthompsastatic int	zyd_unlock_phy(struct zyd_softc *);
139188417Sthompsastatic int	zyd_rf_attach(struct zyd_softc *, uint8_t);
140188417Sthompsastatic const char *zyd_rf_name(uint8_t);
141188417Sthompsastatic int	zyd_hw_init(struct zyd_softc *);
142188417Sthompsastatic int	zyd_read_pod(struct zyd_softc *);
143188417Sthompsastatic int	zyd_read_eeprom(struct zyd_softc *);
144188417Sthompsastatic int	zyd_get_macaddr(struct zyd_softc *);
145188417Sthompsastatic int	zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
146188417Sthompsastatic int	zyd_set_bssid(struct zyd_softc *, const uint8_t *);
147188417Sthompsastatic int	zyd_switch_radio(struct zyd_softc *, int);
148188417Sthompsastatic int	zyd_set_led(struct zyd_softc *, int, int);
149188417Sthompsastatic void	zyd_set_multi(struct zyd_softc *);
150283540Sglebiusstatic void	zyd_update_mcast(struct ieee80211com *);
151188417Sthompsastatic int	zyd_set_rxfilter(struct zyd_softc *);
152188417Sthompsastatic void	zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
153188417Sthompsastatic int	zyd_set_beacon_interval(struct zyd_softc *, int);
154192984Sthompsastatic void	zyd_rx_data(struct usb_xfer *, int, uint16_t);
155193803Sweongyostatic int	zyd_tx_start(struct zyd_softc *, struct mbuf *,
156185948Sthompsa		    struct ieee80211_node *);
157287197Sglebiusstatic int	zyd_transmit(struct ieee80211com *, struct mbuf *);
158287197Sglebiusstatic void	zyd_start(struct zyd_softc *);
159188417Sthompsastatic int	zyd_raw_xmit(struct ieee80211_node *, struct mbuf *,
160185948Sthompsa		    const struct ieee80211_bpf_params *);
161287197Sglebiusstatic void	zyd_parent(struct ieee80211com *);
162191746Sthompsastatic void	zyd_init_locked(struct zyd_softc *);
163191746Sthompsastatic void	zyd_stop(struct zyd_softc *);
164188417Sthompsastatic int	zyd_loadfirmware(struct zyd_softc *);
165188417Sthompsastatic void	zyd_scan_start(struct ieee80211com *);
166188417Sthompsastatic void	zyd_scan_end(struct ieee80211com *);
167300751Savosstatic void	zyd_getradiocaps(struct ieee80211com *, int, int *,
168300751Savos		    struct ieee80211_channel[]);
169188417Sthompsastatic void	zyd_set_channel(struct ieee80211com *);
170188417Sthompsastatic int	zyd_rfmd_init(struct zyd_rf *);
171188417Sthompsastatic int	zyd_rfmd_switch_radio(struct zyd_rf *, int);
172188417Sthompsastatic int	zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
173188417Sthompsastatic int	zyd_al2230_init(struct zyd_rf *);
174188417Sthompsastatic int	zyd_al2230_switch_radio(struct zyd_rf *, int);
175188417Sthompsastatic int	zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
176188417Sthompsastatic int	zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t);
177188417Sthompsastatic int	zyd_al2230_init_b(struct zyd_rf *);
178188417Sthompsastatic int	zyd_al7230B_init(struct zyd_rf *);
179188417Sthompsastatic int	zyd_al7230B_switch_radio(struct zyd_rf *, int);
180188417Sthompsastatic int	zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
181188417Sthompsastatic int	zyd_al2210_init(struct zyd_rf *);
182188417Sthompsastatic int	zyd_al2210_switch_radio(struct zyd_rf *, int);
183188417Sthompsastatic int	zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
184188417Sthompsastatic int	zyd_gct_init(struct zyd_rf *);
185188417Sthompsastatic int	zyd_gct_switch_radio(struct zyd_rf *, int);
186188417Sthompsastatic int	zyd_gct_set_channel(struct zyd_rf *, uint8_t);
187193420Sweongyostatic int	zyd_gct_mode(struct zyd_rf *);
188193420Sweongyostatic int	zyd_gct_set_channel_synth(struct zyd_rf *, int, int);
189193420Sweongyostatic int	zyd_gct_write(struct zyd_rf *, uint16_t);
190193420Sweongyostatic int	zyd_gct_txgain(struct zyd_rf *, uint8_t);
191188417Sthompsastatic int	zyd_maxim2_init(struct zyd_rf *);
192188417Sthompsastatic int	zyd_maxim2_switch_radio(struct zyd_rf *, int);
193188417Sthompsastatic int	zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
194184610Salfred
195184610Salfredstatic const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
196184610Salfredstatic const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
197184610Salfred
198184610Salfred/* various supported device vendors/products */
199188417Sthompsa#define ZYD_ZD1211	0
200188417Sthompsa#define ZYD_ZD1211B	1
201184610Salfred
202193419Sweongyo#define	ZYD_ZD1211_DEV(v,p)	\
203193419Sweongyo	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211) }
204193419Sweongyo#define	ZYD_ZD1211B_DEV(v,p)	\
205193419Sweongyo	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211B) }
206223486Shselaskystatic const STRUCT_USB_HOST_ID zyd_devs[] = {
207193419Sweongyo	/* ZYD_ZD1211 */
208193419Sweongyo	ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
209193419Sweongyo	ZYD_ZD1211_DEV(ABOCOM, WL54),
210193419Sweongyo	ZYD_ZD1211_DEV(ASUS, WL159G),
211193419Sweongyo	ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
212193419Sweongyo	ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
213193419Sweongyo	ZYD_ZD1211_DEV(PLANEX2, GWUS54GD),
214193419Sweongyo	ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
215193419Sweongyo	ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
216193419Sweongyo	ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
217193419Sweongyo	ZYD_ZD1211_DEV(SAGEM, XG760A),
218193419Sweongyo	ZYD_ZD1211_DEV(SENAO, NUB8301),
219193419Sweongyo	ZYD_ZD1211_DEV(SITECOMEU, WL113),
220193419Sweongyo	ZYD_ZD1211_DEV(SWEEX, ZD1211),
221193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
222193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
223193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
224193419Sweongyo	ZYD_ZD1211_DEV(TWINMOS, G240),
225193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
226193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
227193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
228193419Sweongyo	ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
229193419Sweongyo	ZYD_ZD1211_DEV(ZCOM, ZD1211),
230193419Sweongyo	ZYD_ZD1211_DEV(ZYDAS, ZD1211),
231193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, AG225H),
232193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
233193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, G200V2),
234193419Sweongyo	/* ZYD_ZD1211B */
235223566Sgavin	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG_NF),
236193419Sweongyo	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
237193419Sweongyo	ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
238193419Sweongyo	ZYD_ZD1211B_DEV(ASUS, A9T_WIFI),
239193419Sweongyo	ZYD_ZD1211B_DEV(BELKIN, F5D7050_V4000),
240193419Sweongyo	ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
241193419Sweongyo	ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
242193419Sweongyo	ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
243193419Sweongyo	ZYD_ZD1211B_DEV(MELCO, KG54L),
244193419Sweongyo	ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
245193419Sweongyo	ZYD_ZD1211B_DEV(PLANEX2, GW_US54GXS),
246193419Sweongyo	ZYD_ZD1211B_DEV(SAGEM, XG76NA),
247193419Sweongyo	ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
248193419Sweongyo	ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
249193419Sweongyo	ZYD_ZD1211B_DEV(USR, USR5423),
250193419Sweongyo	ZYD_ZD1211B_DEV(VTECH, ZD1211B),
251193419Sweongyo	ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
252193419Sweongyo	ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
253193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, M202),
254193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, G202),
255193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, G220V2)
256184610Salfred};
257184610Salfred
258192984Sthompsastatic const struct usb_config zyd_config[ZYD_N_TRANSFER] = {
259188417Sthompsa	[ZYD_BULK_WR] = {
260184610Salfred		.type = UE_BULK,
261184610Salfred		.endpoint = UE_ADDR_ANY,
262184610Salfred		.direction = UE_DIR_OUT,
263190734Sthompsa		.bufsize = ZYD_MAX_TXBUFSZ,
264190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
265190734Sthompsa		.callback = zyd_bulk_write_callback,
266184610Salfred		.ep_index = 0,
267190734Sthompsa		.timeout = 10000,	/* 10 seconds */
268184610Salfred	},
269188417Sthompsa	[ZYD_BULK_RD] = {
270184610Salfred		.type = UE_BULK,
271184610Salfred		.endpoint = UE_ADDR_ANY,
272184610Salfred		.direction = UE_DIR_IN,
273190734Sthompsa		.bufsize = ZYX_MAX_RXBUFSZ,
274190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
275190734Sthompsa		.callback = zyd_bulk_read_callback,
276184610Salfred		.ep_index = 0,
277184610Salfred	},
278188417Sthompsa	[ZYD_INTR_WR] = {
279184610Salfred		.type = UE_BULK_INTR,
280184610Salfred		.endpoint = UE_ADDR_ANY,
281184610Salfred		.direction = UE_DIR_OUT,
282190734Sthompsa		.bufsize = sizeof(struct zyd_cmd),
283190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
284190734Sthompsa		.callback = zyd_intr_write_callback,
285190734Sthompsa		.timeout = 1000,	/* 1 second */
286184610Salfred		.ep_index = 1,
287184610Salfred	},
288188417Sthompsa	[ZYD_INTR_RD] = {
289188417Sthompsa		.type = UE_INTERRUPT,
290184610Salfred		.endpoint = UE_ADDR_ANY,
291184610Salfred		.direction = UE_DIR_IN,
292190734Sthompsa		.bufsize = sizeof(struct zyd_cmd),
293190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
294190734Sthompsa		.callback = zyd_intr_read_callback,
295184610Salfred	},
296184610Salfred};
297188417Sthompsa#define zyd_read16_m(sc, val, data)	do {				\
298188417Sthompsa	error = zyd_read16(sc, val, data);				\
299188417Sthompsa	if (error != 0)							\
300188417Sthompsa		goto fail;						\
301188417Sthompsa} while (0)
302188417Sthompsa#define zyd_write16_m(sc, val, data)	do {				\
303188417Sthompsa	error = zyd_write16(sc, val, data);				\
304188417Sthompsa	if (error != 0)							\
305188417Sthompsa		goto fail;						\
306188417Sthompsa} while (0)
307188417Sthompsa#define zyd_read32_m(sc, val, data)	do {				\
308188417Sthompsa	error = zyd_read32(sc, val, data);				\
309188417Sthompsa	if (error != 0)							\
310188417Sthompsa		goto fail;						\
311188417Sthompsa} while (0)
312188417Sthompsa#define zyd_write32_m(sc, val, data)	do {				\
313188417Sthompsa	error = zyd_write32(sc, val, data);				\
314188417Sthompsa	if (error != 0)							\
315188417Sthompsa		goto fail;						\
316188417Sthompsa} while (0)
317184610Salfred
318188417Sthompsastatic int
319188417Sthompsazyd_match(device_t dev)
320184610Salfred{
321192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
322184610Salfred
323192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
324188417Sthompsa		return (ENXIO);
325188419Sthompsa	if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX)
326188417Sthompsa		return (ENXIO);
327188417Sthompsa	if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX)
328188417Sthompsa		return (ENXIO);
329184610Salfred
330194228Sthompsa	return (usbd_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa));
331184610Salfred}
332184610Salfred
333188417Sthompsastatic int
334188417Sthompsazyd_attach(device_t dev)
335184610Salfred{
336192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
337188417Sthompsa	struct zyd_softc *sc = device_get_softc(dev);
338287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
339293339Savos	uint8_t iface_index;
340188419Sthompsa	int error;
341184610Salfred
342188417Sthompsa	if (uaa->info.bcdDevice < 0x4330) {
343188417Sthompsa		device_printf(dev, "device version mismatch: 0x%X "
344188417Sthompsa		    "(only >= 43.30 supported)\n",
345188417Sthompsa		    uaa->info.bcdDevice);
346188417Sthompsa		return (EINVAL);
347184610Salfred	}
348184610Salfred
349194228Sthompsa	device_set_usb_desc(dev);
350188417Sthompsa	sc->sc_dev = dev;
351188417Sthompsa	sc->sc_udev = uaa->device;
352188417Sthompsa	sc->sc_macrev = USB_GET_DRIVER_INFO(uaa);
353188419Sthompsa
354188417Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
355188417Sthompsa	    MTX_NETWORK_LOCK, MTX_DEF);
356188417Sthompsa	STAILQ_INIT(&sc->sc_rqh);
357287197Sglebius	mbufq_init(&sc->sc_snd, ifqmaxlen);
358184610Salfred
359188417Sthompsa	iface_index = ZYD_IFACE_INDEX;
360194228Sthompsa	error = usbd_transfer_setup(uaa->device,
361188417Sthompsa	    &iface_index, sc->sc_xfer, zyd_config,
362188417Sthompsa	    ZYD_N_TRANSFER, sc, &sc->sc_mtx);
363188417Sthompsa	if (error) {
364188417Sthompsa		device_printf(dev, "could not allocate USB transfers, "
365194228Sthompsa		    "err=%s\n", usbd_errstr(error));
366188419Sthompsa		goto detach;
367184610Salfred	}
368184610Salfred
369188419Sthompsa	ZYD_LOCK(sc);
370188417Sthompsa	if ((error = zyd_get_macaddr(sc)) != 0) {
371188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
372191746Sthompsa		ZYD_UNLOCK(sc);
373191746Sthompsa		goto detach;
374188417Sthompsa	}
375188419Sthompsa	ZYD_UNLOCK(sc);
376188419Sthompsa
377283537Sglebius	ic->ic_softc = sc;
378283527Sglebius	ic->ic_name = device_get_nameunit(dev);
379188417Sthompsa	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
380188417Sthompsa	ic->ic_opmode = IEEE80211_M_STA;
381184610Salfred
382188417Sthompsa	/* set device capabilities */
383188417Sthompsa	ic->ic_caps =
384188417Sthompsa		  IEEE80211_C_STA		/* station mode */
385188417Sthompsa		| IEEE80211_C_MONITOR		/* monitor mode */
386188417Sthompsa		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
387188417Sthompsa	        | IEEE80211_C_SHSLOT		/* short slot time supported */
388188417Sthompsa		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
389188417Sthompsa	        | IEEE80211_C_WPA		/* 802.11i */
390188417Sthompsa		;
391184610Salfred
392300751Savos	zyd_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
393300751Savos	    ic->ic_channels);
394184610Salfred
395287197Sglebius	ieee80211_ifattach(ic);
396188417Sthompsa	ic->ic_raw_xmit = zyd_raw_xmit;
397188417Sthompsa	ic->ic_scan_start = zyd_scan_start;
398188417Sthompsa	ic->ic_scan_end = zyd_scan_end;
399300751Savos	ic->ic_getradiocaps = zyd_getradiocaps;
400188417Sthompsa	ic->ic_set_channel = zyd_set_channel;
401188417Sthompsa	ic->ic_vap_create = zyd_vap_create;
402188417Sthompsa	ic->ic_vap_delete = zyd_vap_delete;
403188417Sthompsa	ic->ic_update_mcast = zyd_update_mcast;
404188601Sthompsa	ic->ic_update_promisc = zyd_update_mcast;
405287197Sglebius	ic->ic_parent = zyd_parent;
406287197Sglebius	ic->ic_transmit = zyd_transmit;
407184610Salfred
408192468Ssam	ieee80211_radiotap_attach(ic,
409192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
410192468Ssam		ZYD_TX_RADIOTAP_PRESENT,
411192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
412192468Ssam		ZYD_RX_RADIOTAP_PRESENT);
413184610Salfred
414188417Sthompsa	if (bootverbose)
415188417Sthompsa		ieee80211_announce(ic);
416184610Salfred
417191746Sthompsa	return (0);
418191746Sthompsa
419191746Sthompsadetach:
420191746Sthompsa	zyd_detach(dev);
421191746Sthompsa	return (ENXIO);			/* failure */
422184610Salfred}
423184610Salfred
424288648Sadrianstatic void
425288648Sadrianzyd_drain_mbufq(struct zyd_softc *sc)
426288648Sadrian{
427288648Sadrian	struct mbuf *m;
428288648Sadrian	struct ieee80211_node *ni;
429288648Sadrian
430288648Sadrian	ZYD_LOCK_ASSERT(sc, MA_OWNED);
431288648Sadrian	while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
432288648Sadrian		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
433288648Sadrian		m->m_pkthdr.rcvif = NULL;
434288648Sadrian		ieee80211_free_node(ni);
435288648Sadrian		m_freem(m);
436288648Sadrian	}
437288648Sadrian}
438288648Sadrian
439288648Sadrian
440188417Sthompsastatic int
441188417Sthompsazyd_detach(device_t dev)
442184610Salfred{
443188417Sthompsa	struct zyd_softc *sc = device_get_softc(dev);
444287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
445246614Shselasky	unsigned int x;
446184610Salfred
447246614Shselasky	/*
448246614Shselasky	 * Prevent further allocations from RX/TX data
449246614Shselasky	 * lists and ioctls:
450246614Shselasky	 */
451246614Shselasky	ZYD_LOCK(sc);
452246614Shselasky	sc->sc_flags |= ZYD_FLAG_DETACHED;
453288648Sadrian	zyd_drain_mbufq(sc);
454246614Shselasky	STAILQ_INIT(&sc->tx_q);
455246614Shselasky	STAILQ_INIT(&sc->tx_free);
456246614Shselasky	ZYD_UNLOCK(sc);
457184610Salfred
458246614Shselasky	/* drain USB transfers */
459246614Shselasky	for (x = 0; x != ZYD_N_TRANSFER; x++)
460246614Shselasky		usbd_transfer_drain(sc->sc_xfer[x]);
461246614Shselasky
462188419Sthompsa	/* free TX list, if any */
463246614Shselasky	ZYD_LOCK(sc);
464188419Sthompsa	zyd_unsetup_tx_list(sc);
465246614Shselasky	ZYD_UNLOCK(sc);
466188419Sthompsa
467246614Shselasky	/* free USB transfers and some data buffers */
468246614Shselasky	usbd_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER);
469246614Shselasky
470287197Sglebius	if (ic->ic_softc == sc)
471188417Sthompsa		ieee80211_ifdetach(ic);
472188417Sthompsa	mtx_destroy(&sc->sc_mtx);
473184610Salfred
474188417Sthompsa	return (0);
475184610Salfred}
476184610Salfred
477188417Sthompsastatic struct ieee80211vap *
478228621Sbschmidtzyd_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
479228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
480228621Sbschmidt    const uint8_t bssid[IEEE80211_ADDR_LEN],
481228621Sbschmidt    const uint8_t mac[IEEE80211_ADDR_LEN])
482184610Salfred{
483188417Sthompsa	struct zyd_vap *zvp;
484188417Sthompsa	struct ieee80211vap *vap;
485184610Salfred
486188417Sthompsa	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
487188417Sthompsa		return (NULL);
488287197Sglebius	zvp = malloc(sizeof(struct zyd_vap), M_80211_VAP, M_WAITOK | M_ZERO);
489188417Sthompsa	vap = &zvp->vap;
490257743Shselasky
491188417Sthompsa	/* enable s/w bmiss handling for sta mode */
492257743Shselasky	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
493287197Sglebius	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
494257743Shselasky		/* out of memory */
495257743Shselasky		free(zvp, M_80211_VAP);
496257743Shselasky		return (NULL);
497257743Shselasky	}
498184610Salfred
499188417Sthompsa	/* override state transition machine */
500188417Sthompsa	zvp->newstate = vap->iv_newstate;
501188417Sthompsa	vap->iv_newstate = zyd_newstate;
502184610Salfred
503206358Srpaulo	ieee80211_ratectl_init(vap);
504206358Srpaulo	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
505184610Salfred
506188417Sthompsa	/* complete setup */
507188417Sthompsa	ieee80211_vap_attach(vap, ieee80211_media_change,
508287197Sglebius	    ieee80211_media_status, mac);
509188417Sthompsa	ic->ic_opmode = opmode;
510188417Sthompsa	return (vap);
511184610Salfred}
512184610Salfred
513184610Salfredstatic void
514188417Sthompsazyd_vap_delete(struct ieee80211vap *vap)
515184610Salfred{
516188417Sthompsa	struct zyd_vap *zvp = ZYD_VAP(vap);
517184610Salfred
518206358Srpaulo	ieee80211_ratectl_deinit(vap);
519188417Sthompsa	ieee80211_vap_detach(vap);
520188417Sthompsa	free(zvp, M_80211_VAP);
521184610Salfred}
522184610Salfred
523184610Salfredstatic void
524188417Sthompsazyd_tx_free(struct zyd_tx_data *data, int txerr)
525184610Salfred{
526188417Sthompsa	struct zyd_softc *sc = data->sc;
527184610Salfred
528188417Sthompsa	if (data->m != NULL) {
529287197Sglebius		ieee80211_tx_complete(data->ni, data->m, txerr);
530188417Sthompsa		data->m = NULL;
531188417Sthompsa		data->ni = NULL;
532184610Salfred	}
533188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
534188417Sthompsa	sc->tx_nfree++;
535184610Salfred}
536184610Salfred
537188419Sthompsastatic void
538188419Sthompsazyd_setup_tx_list(struct zyd_softc *sc)
539184610Salfred{
540188417Sthompsa	struct zyd_tx_data *data;
541188417Sthompsa	int i;
542184610Salfred
543188417Sthompsa	sc->tx_nfree = 0;
544188417Sthompsa	STAILQ_INIT(&sc->tx_q);
545188417Sthompsa	STAILQ_INIT(&sc->tx_free);
546184610Salfred
547188417Sthompsa	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
548188417Sthompsa		data = &sc->tx_data[i];
549184610Salfred
550188417Sthompsa		data->sc = sc;
551188417Sthompsa		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
552188417Sthompsa		sc->tx_nfree++;
553188417Sthompsa	}
554184610Salfred}
555184610Salfred
556184610Salfredstatic void
557188419Sthompsazyd_unsetup_tx_list(struct zyd_softc *sc)
558184610Salfred{
559188417Sthompsa	struct zyd_tx_data *data;
560188417Sthompsa	int i;
561184610Salfred
562188419Sthompsa	/* make sure any subsequent use of the queues will fail */
563188419Sthompsa	sc->tx_nfree = 0;
564188419Sthompsa	STAILQ_INIT(&sc->tx_q);
565188419Sthompsa	STAILQ_INIT(&sc->tx_free);
566184610Salfred
567188419Sthompsa	/* free up all node references and mbufs */
568188417Sthompsa	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
569188417Sthompsa		data = &sc->tx_data[i];
570184610Salfred
571188417Sthompsa		if (data->m != NULL) {
572188417Sthompsa			m_freem(data->m);
573188417Sthompsa			data->m = NULL;
574188417Sthompsa		}
575188417Sthompsa		if (data->ni != NULL) {
576188417Sthompsa			ieee80211_free_node(data->ni);
577188417Sthompsa			data->ni = NULL;
578188417Sthompsa		}
579188417Sthompsa	}
580184610Salfred}
581184610Salfred
582191746Sthompsastatic int
583191746Sthompsazyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
584186730Salfred{
585188417Sthompsa	struct zyd_vap *zvp = ZYD_VAP(vap);
586191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
587286950Sadrian	struct zyd_softc *sc = ic->ic_softc;
588188417Sthompsa	int error;
589186730Salfred
590191746Sthompsa	DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__,
591191746Sthompsa	    ieee80211_state_name[vap->iv_state],
592191746Sthompsa	    ieee80211_state_name[nstate]);
593191746Sthompsa
594191746Sthompsa	IEEE80211_UNLOCK(ic);
595191746Sthompsa	ZYD_LOCK(sc);
596191746Sthompsa	switch (nstate) {
597188417Sthompsa	case IEEE80211_S_AUTH:
598188417Sthompsa		zyd_set_chan(sc, ic->ic_curchan);
599188417Sthompsa		break;
600188417Sthompsa	case IEEE80211_S_RUN:
601188417Sthompsa		if (vap->iv_opmode == IEEE80211_M_MONITOR)
602188417Sthompsa			break;
603184610Salfred
604188417Sthompsa		/* turn link LED on */
605188417Sthompsa		error = zyd_set_led(sc, ZYD_LED1, 1);
606188417Sthompsa		if (error != 0)
607191746Sthompsa			break;
608191746Sthompsa
609188417Sthompsa		/* make data LED blink upon Tx */
610188417Sthompsa		zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1);
611191746Sthompsa
612296356Savos		IEEE80211_ADDR_COPY(sc->sc_bssid, vap->iv_bss->ni_bssid);
613296356Savos		zyd_set_bssid(sc, sc->sc_bssid);
614188417Sthompsa		break;
615188417Sthompsa	default:
616188417Sthompsa		break;
617184610Salfred	}
618188417Sthompsafail:
619188417Sthompsa	ZYD_UNLOCK(sc);
620188417Sthompsa	IEEE80211_LOCK(ic);
621191746Sthompsa	return (zvp->newstate(vap, nstate, arg));
622184610Salfred}
623184610Salfred
624188417Sthompsa/*
625188417Sthompsa * Callback handler for interrupt transfer
626188417Sthompsa */
627184610Salfredstatic void
628194677Sthompsazyd_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
629184610Salfred{
630194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
631287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
632188417Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
633184610Salfred	struct ieee80211_node *ni;
634188417Sthompsa	struct zyd_cmd *cmd = &sc->sc_ibuf;
635194677Sthompsa	struct usb_page_cache *pc;
636188417Sthompsa	int datalen;
637194677Sthompsa	int actlen;
638184610Salfred
639194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
640194677Sthompsa
641184610Salfred	switch (USB_GET_STATE(xfer)) {
642184610Salfred	case USB_ST_TRANSFERRED:
643194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
644194677Sthompsa		usbd_copy_out(pc, 0, cmd, sizeof(*cmd));
645184610Salfred
646188417Sthompsa		switch (le16toh(cmd->code)) {
647188417Sthompsa		case ZYD_NOTIF_RETRYSTATUS:
648188417Sthompsa		{
649188417Sthompsa			struct zyd_notif_retry *retry =
650188417Sthompsa			    (struct zyd_notif_retry *)cmd->data;
651184610Salfred
652188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_TX_PROC,
653188417Sthompsa			    "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
654188417Sthompsa			    le16toh(retry->rate), ether_sprintf(retry->macaddr),
655188417Sthompsa			    le16toh(retry->count)&0xff, le16toh(retry->count));
656184610Salfred
657188417Sthompsa			/*
658188417Sthompsa			 * Find the node to which the packet was sent and
659188417Sthompsa			 * update its retry statistics.  In BSS mode, this node
660188417Sthompsa			 * is the AP we're associated to so no lookup is
661188417Sthompsa			 * actually needed.
662188417Sthompsa			 */
663188417Sthompsa			ni = ieee80211_find_txnode(vap, retry->macaddr);
664188417Sthompsa			if (ni != NULL) {
665206358Srpaulo				int retrycnt =
666206358Srpaulo				    (int)(le16toh(retry->count) & 0xff);
667206358Srpaulo
668206358Srpaulo				ieee80211_ratectl_tx_complete(vap, ni,
669206358Srpaulo				    IEEE80211_RATECTL_TX_FAILURE,
670206358Srpaulo				    &retrycnt, NULL);
671188417Sthompsa				ieee80211_free_node(ni);
672188417Sthompsa			}
673188417Sthompsa			if (le16toh(retry->count) & 0x100)
674287197Sglebius				/* too many retries */
675287197Sglebius				if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS,
676287197Sglebius				    1);
677188417Sthompsa			break;
678188417Sthompsa		}
679188417Sthompsa		case ZYD_NOTIF_IORD:
680188417Sthompsa		{
681188417Sthompsa			struct zyd_rq *rqp;
682184610Salfred
683188417Sthompsa			if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
684188417Sthompsa				break;	/* HMAC interrupt */
685184610Salfred
686194677Sthompsa			datalen = actlen - sizeof(cmd->code);
687188417Sthompsa			datalen -= 2;	/* XXX: padding? */
688184610Salfred
689188417Sthompsa			STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
690233774Shselasky				int i;
691233774Shselasky				int count;
692184610Salfred
693188417Sthompsa				if (rqp->olen != datalen)
694188417Sthompsa					continue;
695233774Shselasky				count = rqp->olen / sizeof(struct zyd_pair);
696233774Shselasky				for (i = 0; i < count; i++) {
697188417Sthompsa					if (*(((const uint16_t *)rqp->idata) + i) !=
698188417Sthompsa					    (((struct zyd_pair *)cmd->data) + i)->reg)
699188417Sthompsa						break;
700184610Salfred				}
701233774Shselasky				if (i != count)
702188417Sthompsa					continue;
703188417Sthompsa				/* copy answer into caller-supplied buffer */
704227461Shselasky				memcpy(rqp->odata, cmd->data, rqp->olen);
705188417Sthompsa				DPRINTF(sc, ZYD_DEBUG_CMD,
706188417Sthompsa				    "command %p complete, data = %*D \n",
707290328Shselasky				    rqp, rqp->olen, (char *)rqp->odata, ":");
708188417Sthompsa				wakeup(rqp);	/* wakeup caller */
709188417Sthompsa				break;
710184610Salfred			}
711188417Sthompsa			if (rqp == NULL) {
712188417Sthompsa				device_printf(sc->sc_dev,
713188417Sthompsa				    "unexpected IORD notification %*D\n",
714290328Shselasky				    datalen, cmd->data, ":");
715188417Sthompsa			}
716188417Sthompsa			break;
717184610Salfred		}
718188417Sthompsa		default:
719188417Sthompsa			device_printf(sc->sc_dev, "unknown notification %x\n",
720188417Sthompsa			    le16toh(cmd->code));
721188417Sthompsa		}
722184610Salfred
723188417Sthompsa		/* FALLTHROUGH */
724184610Salfred	case USB_ST_SETUP:
725184610Salfredtr_setup:
726194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
727194228Sthompsa		usbd_transfer_submit(xfer);
728188417Sthompsa		break;
729184610Salfred
730188417Sthompsa	default:			/* Error */
731188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
732194677Sthompsa		    usbd_errstr(error));
733188417Sthompsa
734194677Sthompsa		if (error != USB_ERR_CANCELLED) {
735188417Sthompsa			/* try to clear stall first */
736194677Sthompsa			usbd_xfer_set_stall(xfer);
737188417Sthompsa			goto tr_setup;
738184610Salfred		}
739188417Sthompsa		break;
740188417Sthompsa	}
741188417Sthompsa}
742184610Salfred
743188417Sthompsastatic void
744194677Sthompsazyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
745188417Sthompsa{
746194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
747194677Sthompsa	struct zyd_rq *rqp, *cmd;
748194677Sthompsa	struct usb_page_cache *pc;
749184610Salfred
750188417Sthompsa	switch (USB_GET_STATE(xfer)) {
751188417Sthompsa	case USB_ST_TRANSFERRED:
752194677Sthompsa		cmd = usbd_xfer_get_priv(xfer);
753194677Sthompsa		DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd);
754189452Sthompsa		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
755189452Sthompsa			/* Ensure the cached rq pointer is still valid */
756194677Sthompsa			if (rqp == cmd &&
757189452Sthompsa			    (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
758189452Sthompsa				wakeup(rqp);	/* wakeup caller */
759189452Sthompsa		}
760184610Salfred
761188417Sthompsa		/* FALLTHROUGH */
762188417Sthompsa	case USB_ST_SETUP:
763188417Sthompsatr_setup:
764188417Sthompsa		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
765188417Sthompsa			if (rqp->flags & ZYD_CMD_FLAG_SENT)
766188417Sthompsa				continue;
767184610Salfred
768194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
769194677Sthompsa			usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen);
770184610Salfred
771194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, rqp->ilen);
772194677Sthompsa			usbd_xfer_set_priv(xfer, rqp);
773188417Sthompsa			rqp->flags |= ZYD_CMD_FLAG_SENT;
774194228Sthompsa			usbd_transfer_submit(xfer);
775188417Sthompsa			break;
776184610Salfred		}
777184610Salfred		break;
778184610Salfred
779184610Salfred	default:			/* Error */
780188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
781194677Sthompsa		    usbd_errstr(error));
782184610Salfred
783194677Sthompsa		if (error != USB_ERR_CANCELLED) {
784184610Salfred			/* try to clear stall first */
785194677Sthompsa			usbd_xfer_set_stall(xfer);
786188417Sthompsa			goto tr_setup;
787184610Salfred		}
788184610Salfred		break;
789184610Salfred	}
790184610Salfred}
791184610Salfred
792188417Sthompsastatic int
793188417Sthompsazyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
794188601Sthompsa    void *odata, int olen, int flags)
795184610Salfred{
796188417Sthompsa	struct zyd_cmd cmd;
797188417Sthompsa	struct zyd_rq rq;
798188417Sthompsa	int error;
799184610Salfred
800233774Shselasky	if (ilen > (int)sizeof(cmd.data))
801188417Sthompsa		return (EINVAL);
802188419Sthompsa
803188417Sthompsa	cmd.code = htole16(code);
804227461Shselasky	memcpy(cmd.data, idata, ilen);
805188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
806290328Shselasky	    &rq, ilen, idata, ":");
807184610Salfred
808188417Sthompsa	rq.cmd = &cmd;
809188417Sthompsa	rq.idata = idata;
810188417Sthompsa	rq.odata = odata;
811188417Sthompsa	rq.ilen = sizeof(uint16_t) + ilen;
812188417Sthompsa	rq.olen = olen;
813188417Sthompsa	rq.flags = flags;
814188417Sthompsa	STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
815194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
816194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
817184610Salfred
818188417Sthompsa	/* wait at most one second for command reply */
819188417Sthompsa	error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
820188417Sthompsa	if (error)
821188417Sthompsa		device_printf(sc->sc_dev, "command timeout\n");
822188417Sthompsa	STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
823188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
824188417Sthompsa	    &rq, error);
825184610Salfred
826188417Sthompsa	return (error);
827184610Salfred}
828184610Salfred
829184610Salfredstatic int
830188417Sthompsazyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
831184610Salfred{
832188417Sthompsa	struct zyd_pair tmp;
833188417Sthompsa	int error;
834184610Salfred
835188417Sthompsa	reg = htole16(reg);
836188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
837188417Sthompsa	    ZYD_CMD_FLAG_READ);
838188417Sthompsa	if (error == 0)
839188417Sthompsa		*val = le16toh(tmp.val);
840188417Sthompsa	return (error);
841184610Salfred}
842184610Salfred
843184610Salfredstatic int
844188417Sthompsazyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
845184610Salfred{
846188417Sthompsa	struct zyd_pair tmp[2];
847188417Sthompsa	uint16_t regs[2];
848184610Salfred	int error;
849184610Salfred
850188417Sthompsa	regs[0] = htole16(ZYD_REG32_HI(reg));
851188417Sthompsa	regs[1] = htole16(ZYD_REG32_LO(reg));
852188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
853188417Sthompsa	    ZYD_CMD_FLAG_READ);
854188417Sthompsa	if (error == 0)
855188417Sthompsa		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
856188417Sthompsa	return (error);
857188417Sthompsa}
858184610Salfred
859188417Sthompsastatic int
860188417Sthompsazyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
861188417Sthompsa{
862188417Sthompsa	struct zyd_pair pair;
863184610Salfred
864188417Sthompsa	pair.reg = htole16(reg);
865188417Sthompsa	pair.val = htole16(val);
866184610Salfred
867188417Sthompsa	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
868188417Sthompsa}
869184610Salfred
870188417Sthompsastatic int
871188417Sthompsazyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
872188417Sthompsa{
873188417Sthompsa	struct zyd_pair pair[2];
874184610Salfred
875188417Sthompsa	pair[0].reg = htole16(ZYD_REG32_HI(reg));
876188417Sthompsa	pair[0].val = htole16(val >> 16);
877188417Sthompsa	pair[1].reg = htole16(ZYD_REG32_LO(reg));
878188417Sthompsa	pair[1].val = htole16(val & 0xffff);
879184610Salfred
880188417Sthompsa	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
881184610Salfred}
882184610Salfred
883188417Sthompsastatic int
884188417Sthompsazyd_rfwrite(struct zyd_softc *sc, uint32_t val)
885184610Salfred{
886188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
887188417Sthompsa	struct zyd_rfwrite_cmd req;
888188417Sthompsa	uint16_t cr203;
889188417Sthompsa	int error, i;
890184610Salfred
891188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &cr203);
892188417Sthompsa	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
893188417Sthompsa
894188417Sthompsa	req.code  = htole16(2);
895188417Sthompsa	req.width = htole16(rf->width);
896188417Sthompsa	for (i = 0; i < rf->width; i++) {
897188417Sthompsa		req.bit[i] = htole16(cr203);
898188417Sthompsa		if (val & (1 << (rf->width - 1 - i)))
899188417Sthompsa			req.bit[i] |= htole16(ZYD_RF_DATA);
900188417Sthompsa	}
901188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
902188417Sthompsafail:
903188417Sthompsa	return (error);
904184610Salfred}
905184610Salfred
906188417Sthompsastatic int
907188417Sthompsazyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
908184610Salfred{
909188417Sthompsa	int error;
910184610Salfred
911188417Sthompsa	zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
912188417Sthompsa	zyd_write16_m(sc, ZYD_CR243, (val >>  8) & 0xff);
913188417Sthompsa	zyd_write16_m(sc, ZYD_CR242, (val >>  0) & 0xff);
914188417Sthompsafail:
915188417Sthompsa	return (error);
916184610Salfred}
917184610Salfred
918188417Sthompsastatic int
919188417Sthompsazyd_lock_phy(struct zyd_softc *sc)
920184610Salfred{
921188417Sthompsa	int error;
922188417Sthompsa	uint32_t tmp;
923186730Salfred
924188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
925188417Sthompsa	tmp &= ~ZYD_UNLOCK_PHY_REGS;
926188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
927188417Sthompsafail:
928188417Sthompsa	return (error);
929184610Salfred}
930184610Salfred
931188417Sthompsastatic int
932188417Sthompsazyd_unlock_phy(struct zyd_softc *sc)
933184610Salfred{
934188417Sthompsa	int error;
935188417Sthompsa	uint32_t tmp;
936184610Salfred
937188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
938188417Sthompsa	tmp |= ZYD_UNLOCK_PHY_REGS;
939188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
940188417Sthompsafail:
941188417Sthompsa	return (error);
942184610Salfred}
943184610Salfred
944184610Salfred/*
945188417Sthompsa * RFMD RF methods.
946184610Salfred */
947188417Sthompsastatic int
948188417Sthompsazyd_rfmd_init(struct zyd_rf *rf)
949184610Salfred{
950188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
951184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
952184610Salfred	static const uint32_t rfini[] = ZYD_RFMD_RF;
953188417Sthompsa	int i, error;
954184610Salfred
955184610Salfred	/* init RF-dependent PHY registers */
956288088Sadrian	for (i = 0; i < nitems(phyini); i++) {
957188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
958184610Salfred	}
959184610Salfred
960184610Salfred	/* init RFMD radio */
961288088Sadrian	for (i = 0; i < nitems(rfini); i++) {
962188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
963188417Sthompsa			return (error);
964184610Salfred	}
965188417Sthompsafail:
966188417Sthompsa	return (error);
967184610Salfred}
968184610Salfred
969188417Sthompsastatic int
970188417Sthompsazyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
971184610Salfred{
972188417Sthompsa	int error;
973188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
974188417Sthompsa
975188417Sthompsa	zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
976188417Sthompsa	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
977188417Sthompsafail:
978188417Sthompsa	return (error);
979184610Salfred}
980184610Salfred
981188417Sthompsastatic int
982188417Sthompsazyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
983184610Salfred{
984188417Sthompsa	int error;
985188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
986184610Salfred	static const struct {
987188417Sthompsa		uint32_t	r1, r2;
988188417Sthompsa	} rfprog[] = ZYD_RFMD_CHANTABLE;
989184610Salfred
990188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
991188417Sthompsa	if (error != 0)
992188417Sthompsa		goto fail;
993188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
994188417Sthompsa	if (error != 0)
995188417Sthompsa		goto fail;
996184610Salfred
997188417Sthompsafail:
998188417Sthompsa	return (error);
999184610Salfred}
1000184610Salfred
1001184610Salfred/*
1002188417Sthompsa * AL2230 RF methods.
1003184610Salfred */
1004188417Sthompsastatic int
1005188417Sthompsazyd_al2230_init(struct zyd_rf *rf)
1006184610Salfred{
1007188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1008184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
1009186730Salfred	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1010186730Salfred	static const struct zyd_phy_pair phypll[] = {
1011188417Sthompsa		{ ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
1012188417Sthompsa		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
1013186730Salfred	};
1014186730Salfred	static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
1015186730Salfred	static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
1016186730Salfred	static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
1017188417Sthompsa	int i, error;
1018184610Salfred
1019184610Salfred	/* init RF-dependent PHY registers */
1020288088Sadrian	for (i = 0; i < nitems(phyini); i++)
1021188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1022186730Salfred
1023188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1024288088Sadrian		for (i = 0; i < nitems(phy2230s); i++)
1025188417Sthompsa			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1026184610Salfred	}
1027188417Sthompsa
1028186730Salfred	/* init AL2230 radio */
1029288088Sadrian	for (i = 0; i < nitems(rfini1); i++) {
1030188417Sthompsa		error = zyd_rfwrite(sc, rfini1[i]);
1031188417Sthompsa		if (error != 0)
1032188417Sthompsa			goto fail;
1033188417Sthompsa	}
1034184610Salfred
1035188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1036188417Sthompsa		error = zyd_rfwrite(sc, 0x000824);
1037186730Salfred	else
1038188417Sthompsa		error = zyd_rfwrite(sc, 0x0005a4);
1039188417Sthompsa	if (error != 0)
1040188417Sthompsa		goto fail;
1041186730Salfred
1042288088Sadrian	for (i = 0; i < nitems(rfini2); i++) {
1043188417Sthompsa		error = zyd_rfwrite(sc, rfini2[i]);
1044188417Sthompsa		if (error != 0)
1045188417Sthompsa			goto fail;
1046188417Sthompsa	}
1047186730Salfred
1048288088Sadrian	for (i = 0; i < nitems(phypll); i++)
1049188417Sthompsa		zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
1050186730Salfred
1051288088Sadrian	for (i = 0; i < nitems(rfini3); i++) {
1052188417Sthompsa		error = zyd_rfwrite(sc, rfini3[i]);
1053188417Sthompsa		if (error != 0)
1054188417Sthompsa			goto fail;
1055188417Sthompsa	}
1056188417Sthompsafail:
1057188417Sthompsa	return (error);
1058184610Salfred}
1059184610Salfred
1060188417Sthompsastatic int
1061188417Sthompsazyd_al2230_fini(struct zyd_rf *rf)
1062186730Salfred{
1063188417Sthompsa	int error, i;
1064188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1065186730Salfred	static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
1066186730Salfred
1067288088Sadrian	for (i = 0; i < nitems(phy); i++)
1068188417Sthompsa		zyd_write16_m(sc, phy[i].reg, phy[i].val);
1069186730Salfred
1070186730Salfred	if (sc->sc_newphy != 0)
1071188417Sthompsa		zyd_write16_m(sc, ZYD_CR9, 0xe1);
1072188417Sthompsa
1073188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, 0x6);
1074188417Sthompsafail:
1075188417Sthompsa	return (error);
1076186730Salfred}
1077186730Salfred
1078188417Sthompsastatic int
1079188417Sthompsazyd_al2230_init_b(struct zyd_rf *rf)
1080184610Salfred{
1081188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1082186730Salfred	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1083186730Salfred	static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
1084186730Salfred	static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
1085186730Salfred	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1086188417Sthompsa	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1087186730Salfred	static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
1088186730Salfred	static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
1089186730Salfred	static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
1090186730Salfred	static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
1091188417Sthompsa	int i, error;
1092184610Salfred
1093288088Sadrian	for (i = 0; i < nitems(phy1); i++)
1094188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1095186730Salfred
1096184610Salfred	/* init RF-dependent PHY registers */
1097288088Sadrian	for (i = 0; i < nitems(phyini); i++)
1098188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1099184610Salfred
1100188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1101288088Sadrian		for (i = 0; i < nitems(phy2230s); i++)
1102188417Sthompsa			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1103188417Sthompsa	}
1104186730Salfred
1105188417Sthompsa	for (i = 0; i < 3; i++) {
1106188417Sthompsa		error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
1107188417Sthompsa		if (error != 0)
1108188417Sthompsa			return (error);
1109188417Sthompsa	}
1110186730Salfred
1111288088Sadrian	for (i = 0; i < nitems(rfini_part1); i++) {
1112188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part1[i]);
1113188417Sthompsa		if (error != 0)
1114188417Sthompsa			return (error);
1115188417Sthompsa	}
1116186730Salfred
1117188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1118188417Sthompsa		error = zyd_rfwrite(sc, 0x241000);
1119186730Salfred	else
1120188417Sthompsa		error = zyd_rfwrite(sc, 0x25a000);
1121188417Sthompsa	if (error != 0)
1122188417Sthompsa		goto fail;
1123186730Salfred
1124288088Sadrian	for (i = 0; i < nitems(rfini_part2); i++) {
1125188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part2[i]);
1126188417Sthompsa		if (error != 0)
1127188417Sthompsa			return (error);
1128188417Sthompsa	}
1129186730Salfred
1130288088Sadrian	for (i = 0; i < nitems(phy2); i++)
1131188417Sthompsa		zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
1132186730Salfred
1133288088Sadrian	for (i = 0; i < nitems(rfini_part3); i++) {
1134188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part3[i]);
1135188417Sthompsa		if (error != 0)
1136188417Sthompsa			return (error);
1137188417Sthompsa	}
1138186730Salfred
1139288088Sadrian	for (i = 0; i < nitems(phy3); i++)
1140188417Sthompsa		zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
1141186730Salfred
1142188417Sthompsa	error = zyd_al2230_fini(rf);
1143188417Sthompsafail:
1144188417Sthompsa	return (error);
1145184610Salfred}
1146184610Salfred
1147188417Sthompsastatic int
1148188417Sthompsazyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1149184610Salfred{
1150188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1151188417Sthompsa	int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
1152188417Sthompsa
1153188417Sthompsa	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1154188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
1155188417Sthompsafail:
1156188417Sthompsa	return (error);
1157188417Sthompsa}
1158188417Sthompsa
1159188417Sthompsastatic int
1160188417Sthompsazyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1161188417Sthompsa{
1162188417Sthompsa	int error, i;
1163188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1164186730Salfred	static const struct zyd_phy_pair phy1[] = {
1165188417Sthompsa		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
1166186730Salfred	};
1167184610Salfred	static const struct {
1168188417Sthompsa		uint32_t	r1, r2, r3;
1169188417Sthompsa	} rfprog[] = ZYD_AL2230_CHANTABLE;
1170184610Salfred
1171188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1172188417Sthompsa	if (error != 0)
1173188417Sthompsa		goto fail;
1174188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1175188417Sthompsa	if (error != 0)
1176188417Sthompsa		goto fail;
1177188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
1178188417Sthompsa	if (error != 0)
1179188417Sthompsa		goto fail;
1180184610Salfred
1181288088Sadrian	for (i = 0; i < nitems(phy1); i++)
1182188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1183188417Sthompsafail:
1184188417Sthompsa	return (error);
1185184610Salfred}
1186184610Salfred
1187188417Sthompsastatic int
1188188417Sthompsazyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
1189186730Salfred{
1190188417Sthompsa	int error, i;
1191188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1192186730Salfred	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1193186730Salfred	static const struct {
1194188417Sthompsa		uint32_t	r1, r2, r3;
1195188417Sthompsa	} rfprog[] = ZYD_AL2230_CHANTABLE_B;
1196186730Salfred
1197288088Sadrian	for (i = 0; i < nitems(phy1); i++)
1198188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1199186730Salfred
1200188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
1201188417Sthompsa	if (error != 0)
1202188417Sthompsa		goto fail;
1203188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
1204188417Sthompsa	if (error != 0)
1205188417Sthompsa		goto fail;
1206188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
1207188417Sthompsa	if (error != 0)
1208188417Sthompsa		goto fail;
1209188417Sthompsa	error = zyd_al2230_fini(rf);
1210188417Sthompsafail:
1211188417Sthompsa	return (error);
1212186730Salfred}
1213186730Salfred
1214186730Salfred#define	ZYD_AL2230_PHY_BANDEDGE6					\
1215186730Salfred{									\
1216188417Sthompsa	{ ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 },	\
1217186730Salfred	{ ZYD_CR47,  0x1e }						\
1218186730Salfred}
1219186730Salfred
1220188417Sthompsastatic int
1221188417Sthompsazyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
1222186730Salfred{
1223188417Sthompsa	int error = 0, i;
1224188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1225287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1226186730Salfred	struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
1227188601Sthompsa	int chan = ieee80211_chan2ieee(ic, c);
1228186730Salfred
1229188417Sthompsa	if (chan == 1 || chan == 11)
1230186730Salfred		r[0].val = 0x12;
1231188417Sthompsa
1232288088Sadrian	for (i = 0; i < nitems(r); i++)
1233188417Sthompsa		zyd_write16_m(sc, r[i].reg, r[i].val);
1234188417Sthompsafail:
1235188417Sthompsa	return (error);
1236186730Salfred}
1237186730Salfred
1238184610Salfred/*
1239184610Salfred * AL7230B RF methods.
1240184610Salfred */
1241188417Sthompsastatic int
1242188417Sthompsazyd_al7230B_init(struct zyd_rf *rf)
1243184610Salfred{
1244188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1245184610Salfred	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1246184610Salfred	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1247184610Salfred	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1248184610Salfred	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1249184610Salfred	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1250188417Sthompsa	int i, error;
1251184610Salfred
1252184610Salfred	/* for AL7230B, PHY and RF need to be initialized in "phases" */
1253184610Salfred
1254184610Salfred	/* init RF-dependent PHY registers, part one */
1255288088Sadrian	for (i = 0; i < nitems(phyini_1); i++)
1256188417Sthompsa		zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
1257188417Sthompsa
1258184610Salfred	/* init AL7230B radio, part one */
1259288088Sadrian	for (i = 0; i < nitems(rfini_1); i++) {
1260188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1261188417Sthompsa			return (error);
1262184610Salfred	}
1263184610Salfred	/* init RF-dependent PHY registers, part two */
1264288088Sadrian	for (i = 0; i < nitems(phyini_2); i++)
1265188417Sthompsa		zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
1266188417Sthompsa
1267184610Salfred	/* init AL7230B radio, part two */
1268288088Sadrian	for (i = 0; i < nitems(rfini_2); i++) {
1269188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1270188417Sthompsa			return (error);
1271184610Salfred	}
1272184610Salfred	/* init RF-dependent PHY registers, part three */
1273288088Sadrian	for (i = 0; i < nitems(phyini_3); i++)
1274188417Sthompsa		zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
1275188417Sthompsafail:
1276188417Sthompsa	return (error);
1277184610Salfred}
1278184610Salfred
1279188417Sthompsastatic int
1280188417Sthompsazyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1281184610Salfred{
1282188417Sthompsa	int error;
1283188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1284188417Sthompsa
1285188417Sthompsa	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1286188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1287188417Sthompsafail:
1288188417Sthompsa	return (error);
1289188417Sthompsa}
1290188417Sthompsa
1291188417Sthompsastatic int
1292188417Sthompsazyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1293188417Sthompsa{
1294188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1295184610Salfred	static const struct {
1296188417Sthompsa		uint32_t	r1, r2;
1297188417Sthompsa	} rfprog[] = ZYD_AL7230B_CHANTABLE;
1298184610Salfred	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1299188417Sthompsa	int i, error;
1300184610Salfred
1301188417Sthompsa	zyd_write16_m(sc, ZYD_CR240, 0x57);
1302188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, 0x2f);
1303184610Salfred
1304288088Sadrian	for (i = 0; i < nitems(rfsc); i++) {
1305188417Sthompsa		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1306188417Sthompsa			return (error);
1307184610Salfred	}
1308184610Salfred
1309188417Sthompsa	zyd_write16_m(sc, ZYD_CR128, 0x14);
1310188417Sthompsa	zyd_write16_m(sc, ZYD_CR129, 0x12);
1311188417Sthompsa	zyd_write16_m(sc, ZYD_CR130, 0x10);
1312188417Sthompsa	zyd_write16_m(sc, ZYD_CR38,  0x38);
1313188417Sthompsa	zyd_write16_m(sc, ZYD_CR136, 0xdf);
1314184610Salfred
1315188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1316188417Sthompsa	if (error != 0)
1317188417Sthompsa		goto fail;
1318188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1319188417Sthompsa	if (error != 0)
1320188417Sthompsa		goto fail;
1321188417Sthompsa	error = zyd_rfwrite(sc, 0x3c9000);
1322188417Sthompsa	if (error != 0)
1323188417Sthompsa		goto fail;
1324184610Salfred
1325188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, 0x3f);
1326188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, 0x06);
1327188417Sthompsa	zyd_write16_m(sc, ZYD_CR240, 0x08);
1328188417Sthompsafail:
1329188417Sthompsa	return (error);
1330184610Salfred}
1331184610Salfred
1332184610Salfred/*
1333184610Salfred * AL2210 RF methods.
1334184610Salfred */
1335188417Sthompsastatic int
1336188417Sthompsazyd_al2210_init(struct zyd_rf *rf)
1337184610Salfred{
1338188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1339184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1340184610Salfred	static const uint32_t rfini[] = ZYD_AL2210_RF;
1341184610Salfred	uint32_t tmp;
1342188417Sthompsa	int i, error;
1343184610Salfred
1344188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 2);
1345184610Salfred
1346184610Salfred	/* init RF-dependent PHY registers */
1347288088Sadrian	for (i = 0; i < nitems(phyini); i++)
1348188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1349188417Sthompsa
1350184610Salfred	/* init AL2210 radio */
1351288088Sadrian	for (i = 0; i < nitems(rfini); i++) {
1352188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1353188417Sthompsa			return (error);
1354184610Salfred	}
1355188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1356188417Sthompsa	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1357188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1358188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1359188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1360188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1361188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1362188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 3);
1363188417Sthompsafail:
1364188417Sthompsa	return (error);
1365184610Salfred}
1366184610Salfred
1367188417Sthompsastatic int
1368188417Sthompsazyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1369184610Salfred{
1370188417Sthompsa	/* vendor driver does nothing for this RF chip */
1371188417Sthompsa
1372188417Sthompsa	return (0);
1373188417Sthompsa}
1374188417Sthompsa
1375188417Sthompsastatic int
1376188417Sthompsazyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1377188417Sthompsa{
1378188417Sthompsa	int error;
1379188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1380184610Salfred	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1381184610Salfred	uint32_t tmp;
1382184610Salfred
1383188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 2);
1384188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1385188417Sthompsa	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1386188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1387188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1388188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1389188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1390188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1391184610Salfred
1392184610Salfred	/* actually set the channel */
1393188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1]);
1394188417Sthompsa	if (error != 0)
1395188417Sthompsa		goto fail;
1396184610Salfred
1397188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 3);
1398188417Sthompsafail:
1399188417Sthompsa	return (error);
1400184610Salfred}
1401184610Salfred
1402184610Salfred/*
1403184610Salfred * GCT RF methods.
1404184610Salfred */
1405188417Sthompsastatic int
1406188417Sthompsazyd_gct_init(struct zyd_rf *rf)
1407184610Salfred{
1408193420Sweongyo#define	ZYD_GCT_INTR_REG	0x85c1
1409188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1410184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1411184610Salfred	static const uint32_t rfini[] = ZYD_GCT_RF;
1412193420Sweongyo	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1413193420Sweongyo	int i, idx = -1, error;
1414193420Sweongyo	uint16_t data;
1415184610Salfred
1416184610Salfred	/* init RF-dependent PHY registers */
1417288088Sadrian	for (i = 0; i < nitems(phyini); i++)
1418188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1419188417Sthompsa
1420184610Salfred	/* init cgt radio */
1421288088Sadrian	for (i = 0; i < nitems(rfini); i++) {
1422188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1423188417Sthompsa			return (error);
1424184610Salfred	}
1425193420Sweongyo
1426193420Sweongyo	error = zyd_gct_mode(rf);
1427193420Sweongyo	if (error != 0)
1428193420Sweongyo		return (error);
1429193420Sweongyo
1430288088Sadrian	for (i = 0; i < (int)(nitems(vco) - 1); i++) {
1431193420Sweongyo		error = zyd_gct_set_channel_synth(rf, 1, 0);
1432193420Sweongyo		if (error != 0)
1433193420Sweongyo			goto fail;
1434193420Sweongyo		error = zyd_gct_write(rf, vco[i][0]);
1435193420Sweongyo		if (error != 0)
1436193420Sweongyo			goto fail;
1437193420Sweongyo		zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf);
1438193420Sweongyo		zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data);
1439193420Sweongyo		if ((data & 0xf) == 0) {
1440193420Sweongyo			idx = i;
1441193420Sweongyo			break;
1442193420Sweongyo		}
1443193420Sweongyo	}
1444193420Sweongyo	if (idx == -1) {
1445193420Sweongyo		error = zyd_gct_set_channel_synth(rf, 1, 1);
1446193420Sweongyo		if (error != 0)
1447193420Sweongyo			goto fail;
1448193420Sweongyo		error = zyd_gct_write(rf, 0x6662);
1449193420Sweongyo		if (error != 0)
1450193420Sweongyo			goto fail;
1451193420Sweongyo	}
1452193420Sweongyo
1453193420Sweongyo	rf->idx = idx;
1454193420Sweongyo	zyd_write16_m(sc, ZYD_CR203, 0x6);
1455188417Sthompsafail:
1456188417Sthompsa	return (error);
1457193420Sweongyo#undef ZYD_GCT_INTR_REG
1458184610Salfred}
1459184610Salfred
1460188417Sthompsastatic int
1461193420Sweongyozyd_gct_mode(struct zyd_rf *rf)
1462184610Salfred{
1463193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1464193420Sweongyo	static const uint32_t mode[] = {
1465193420Sweongyo		0x25f98, 0x25f9a, 0x25f94, 0x27fd4
1466193420Sweongyo	};
1467193420Sweongyo	int i, error;
1468188417Sthompsa
1469288088Sadrian	for (i = 0; i < nitems(mode); i++) {
1470193420Sweongyo		if ((error = zyd_rfwrite(sc, mode[i])) != 0)
1471193420Sweongyo			break;
1472193420Sweongyo	}
1473193420Sweongyo	return (error);
1474188417Sthompsa}
1475188417Sthompsa
1476188417Sthompsastatic int
1477193420Sweongyozyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal)
1478188417Sthompsa{
1479193420Sweongyo	int error, idx = chan - 1;
1480188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1481193420Sweongyo	static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL;
1482193420Sweongyo	static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD;
1483193420Sweongyo	static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV;
1484184610Salfred
1485193420Sweongyo	error = zyd_rfwrite(sc,
1486193420Sweongyo	    (acal == 1) ? acal_synth[idx] : std_synth[idx]);
1487188417Sthompsa	if (error != 0)
1488193420Sweongyo		return (error);
1489193420Sweongyo	return zyd_rfwrite(sc, div_synth[idx]);
1490184610Salfred}
1491184610Salfred
1492188417Sthompsastatic int
1493193420Sweongyozyd_gct_write(struct zyd_rf *rf, uint16_t value)
1494184610Salfred{
1495188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1496184610Salfred
1497193420Sweongyo	return zyd_rfwrite(sc, 0x300000 | 0x40000 | value);
1498184610Salfred}
1499184610Salfred
1500188417Sthompsastatic int
1501193420Sweongyozyd_gct_switch_radio(struct zyd_rf *rf, int on)
1502184610Salfred{
1503193420Sweongyo	int error;
1504193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1505188417Sthompsa
1506193420Sweongyo	error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90);
1507193420Sweongyo	if (error != 0)
1508193420Sweongyo		return (error);
1509193420Sweongyo
1510193420Sweongyo	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
1511193420Sweongyo	zyd_write16_m(sc, ZYD_CR251,
1512193420Sweongyo	    on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f);
1513193420Sweongyofail:
1514193420Sweongyo	return (error);
1515188417Sthompsa}
1516188417Sthompsa
1517188417Sthompsastatic int
1518193420Sweongyozyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1519188417Sthompsa{
1520193420Sweongyo	int error, i;
1521188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1522193420Sweongyo	static const struct zyd_phy_pair cmd[] = {
1523193420Sweongyo		{ ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 },
1524193420Sweongyo		{ ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 },
1525193420Sweongyo	};
1526193420Sweongyo	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1527184610Salfred
1528193420Sweongyo	error = zyd_gct_set_channel_synth(rf, chan, 0);
1529188417Sthompsa	if (error != 0)
1530188417Sthompsa		goto fail;
1531193420Sweongyo	error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 :
1532193420Sweongyo	    vco[rf->idx][((chan - 1) / 2)]);
1533188417Sthompsa	if (error != 0)
1534188417Sthompsa		goto fail;
1535193420Sweongyo	error = zyd_gct_mode(rf);
1536193420Sweongyo	if (error != 0)
1537193420Sweongyo		return (error);
1538288088Sadrian	for (i = 0; i < nitems(cmd); i++)
1539193420Sweongyo		zyd_write16_m(sc, cmd[i].reg, cmd[i].val);
1540193420Sweongyo	error = zyd_gct_txgain(rf, chan);
1541193420Sweongyo	if (error != 0)
1542193420Sweongyo		return (error);
1543193420Sweongyo	zyd_write16_m(sc, ZYD_CR203, 0x6);
1544188417Sthompsafail:
1545188417Sthompsa	return (error);
1546184610Salfred}
1547184610Salfred
1548193420Sweongyostatic int
1549193420Sweongyozyd_gct_txgain(struct zyd_rf *rf, uint8_t chan)
1550193420Sweongyo{
1551193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1552193420Sweongyo	static uint32_t txgain[] = ZYD_GCT_TXGAIN;
1553193420Sweongyo	uint8_t idx = sc->sc_pwrint[chan - 1];
1554193420Sweongyo
1555288088Sadrian	if (idx >= nitems(txgain)) {
1556193420Sweongyo		device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n",
1557193420Sweongyo		    chan, idx);
1558193420Sweongyo		return 0;
1559193420Sweongyo	}
1560193420Sweongyo
1561193420Sweongyo	return zyd_rfwrite(sc, 0x700000 | txgain[idx]);
1562193420Sweongyo}
1563193420Sweongyo
1564184610Salfred/*
1565184610Salfred * Maxim2 RF methods.
1566184610Salfred */
1567188417Sthompsastatic int
1568188417Sthompsazyd_maxim2_init(struct zyd_rf *rf)
1569184610Salfred{
1570188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1571184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1572184610Salfred	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1573184610Salfred	uint16_t tmp;
1574188417Sthompsa	int i, error;
1575184610Salfred
1576184610Salfred	/* init RF-dependent PHY registers */
1577288088Sadrian	for (i = 0; i < nitems(phyini); i++)
1578188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1579184610Salfred
1580188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1581188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1582188417Sthompsa
1583184610Salfred	/* init maxim2 radio */
1584288088Sadrian	for (i = 0; i < nitems(rfini); i++) {
1585188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1586188417Sthompsa			return (error);
1587184610Salfred	}
1588188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1589188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1590188417Sthompsafail:
1591188417Sthompsa	return (error);
1592184610Salfred}
1593184610Salfred
1594188417Sthompsastatic int
1595188417Sthompsazyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1596184610Salfred{
1597188417Sthompsa
1598188417Sthompsa	/* vendor driver does nothing for this RF chip */
1599188417Sthompsa	return (0);
1600188417Sthompsa}
1601188417Sthompsa
1602188417Sthompsastatic int
1603188417Sthompsazyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1604188417Sthompsa{
1605188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1606184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1607184610Salfred	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1608184610Salfred	static const struct {
1609188417Sthompsa		uint32_t	r1, r2;
1610188417Sthompsa	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
1611184610Salfred	uint16_t tmp;
1612188417Sthompsa	int i, error;
1613184610Salfred
1614184610Salfred	/*
1615184610Salfred	 * Do the same as we do when initializing it, except for the channel
1616184610Salfred	 * values coming from the two channel tables.
1617184610Salfred	 */
1618184610Salfred
1619184610Salfred	/* init RF-dependent PHY registers */
1620288088Sadrian	for (i = 0; i < nitems(phyini); i++)
1621188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1622184610Salfred
1623188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1624188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1625188417Sthompsa
1626184610Salfred	/* first two values taken from the chantables */
1627188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1628188417Sthompsa	if (error != 0)
1629188417Sthompsa		goto fail;
1630188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1631188417Sthompsa	if (error != 0)
1632188417Sthompsa		goto fail;
1633184610Salfred
1634184610Salfred	/* init maxim2 radio - skipping the two first values */
1635288088Sadrian	for (i = 2; i < nitems(rfini); i++) {
1636188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1637188417Sthompsa			return (error);
1638184610Salfred	}
1639188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1640188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1641188417Sthompsafail:
1642188417Sthompsa	return (error);
1643184610Salfred}
1644184610Salfred
1645188417Sthompsastatic int
1646188417Sthompsazyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1647184610Salfred{
1648188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1649184610Salfred
1650188417Sthompsa	rf->rf_sc = sc;
1651193420Sweongyo	rf->update_pwr = 1;
1652188417Sthompsa
1653188417Sthompsa	switch (type) {
1654184610Salfred	case ZYD_RF_RFMD:
1655188417Sthompsa		rf->init         = zyd_rfmd_init;
1656188417Sthompsa		rf->switch_radio = zyd_rfmd_switch_radio;
1657188417Sthompsa		rf->set_channel  = zyd_rfmd_set_channel;
1658188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1659184610Salfred		break;
1660184610Salfred	case ZYD_RF_AL2230:
1661186730Salfred	case ZYD_RF_AL2230S:
1662188417Sthompsa		if (sc->sc_macrev == ZYD_ZD1211B) {
1663188417Sthompsa			rf->init = zyd_al2230_init_b;
1664188417Sthompsa			rf->set_channel = zyd_al2230_set_channel_b;
1665186730Salfred		} else {
1666188417Sthompsa			rf->init = zyd_al2230_init;
1667188417Sthompsa			rf->set_channel = zyd_al2230_set_channel;
1668186730Salfred		}
1669188417Sthompsa		rf->switch_radio = zyd_al2230_switch_radio;
1670188417Sthompsa		rf->bandedge6	 = zyd_al2230_bandedge6;
1671188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1672184610Salfred		break;
1673184610Salfred	case ZYD_RF_AL7230B:
1674188417Sthompsa		rf->init         = zyd_al7230B_init;
1675188417Sthompsa		rf->switch_radio = zyd_al7230B_switch_radio;
1676188417Sthompsa		rf->set_channel  = zyd_al7230B_set_channel;
1677188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1678184610Salfred		break;
1679184610Salfred	case ZYD_RF_AL2210:
1680188417Sthompsa		rf->init         = zyd_al2210_init;
1681188417Sthompsa		rf->switch_radio = zyd_al2210_switch_radio;
1682188417Sthompsa		rf->set_channel  = zyd_al2210_set_channel;
1683188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1684184610Salfred		break;
1685193420Sweongyo	case ZYD_RF_MAXIM_NEW:
1686184610Salfred	case ZYD_RF_GCT:
1687188417Sthompsa		rf->init         = zyd_gct_init;
1688188417Sthompsa		rf->switch_radio = zyd_gct_switch_radio;
1689188417Sthompsa		rf->set_channel  = zyd_gct_set_channel;
1690193420Sweongyo		rf->width        = 24;	/* 24-bit RF values */
1691193420Sweongyo		rf->update_pwr   = 0;
1692184610Salfred		break;
1693184610Salfred	case ZYD_RF_MAXIM_NEW2:
1694188417Sthompsa		rf->init         = zyd_maxim2_init;
1695188417Sthompsa		rf->switch_radio = zyd_maxim2_switch_radio;
1696188417Sthompsa		rf->set_channel  = zyd_maxim2_set_channel;
1697188417Sthompsa		rf->width        = 18;	/* 18-bit RF values */
1698184610Salfred		break;
1699184610Salfred	default:
1700188417Sthompsa		device_printf(sc->sc_dev,
1701188417Sthompsa		    "sorry, radio \"%s\" is not supported yet\n",
1702188417Sthompsa		    zyd_rf_name(type));
1703188417Sthompsa		return (EINVAL);
1704184610Salfred	}
1705188417Sthompsa	return (0);
1706188417Sthompsa}
1707184610Salfred
1708188417Sthompsastatic const char *
1709188417Sthompsazyd_rf_name(uint8_t type)
1710188417Sthompsa{
1711188417Sthompsa	static const char * const zyd_rfs[] = {
1712188417Sthompsa		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
1713188417Sthompsa		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
1714188417Sthompsa		"AL2230S",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
1715188417Sthompsa		"PHILIPS"
1716188417Sthompsa	};
1717184610Salfred
1718188417Sthompsa	return zyd_rfs[(type > 15) ? 0 : type];
1719184610Salfred}
1720184610Salfred
1721188417Sthompsastatic int
1722188417Sthompsazyd_hw_init(struct zyd_softc *sc)
1723184610Salfred{
1724188417Sthompsa	int error;
1725184610Salfred	const struct zyd_phy_pair *phyp;
1726188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1727188417Sthompsa	uint16_t val;
1728184610Salfred
1729184610Salfred	/* specify that the plug and play is finished */
1730188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1731188417Sthompsa	zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
1732188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
1733188417Sthompsa	    sc->sc_fwbase);
1734184610Salfred
1735184610Salfred	/* retrieve firmware revision number */
1736188417Sthompsa	zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
1737188417Sthompsa	zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
1738188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1739186730Salfred	/* set mandatory rates - XXX assumes 802.11b/g */
1740188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
1741186730Salfred
1742184610Salfred	/* disable interrupts */
1743188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
1744184610Salfred
1745188417Sthompsa	if ((error = zyd_read_pod(sc)) != 0) {
1746188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
1747188417Sthompsa		goto fail;
1748184610Salfred	}
1749188417Sthompsa
1750188417Sthompsa	/* PHY init (resetting) */
1751188417Sthompsa	error = zyd_lock_phy(sc);
1752188417Sthompsa	if (error != 0)
1753188417Sthompsa		goto fail;
1754188417Sthompsa	phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1755188417Sthompsa	for (; phyp->reg != 0; phyp++)
1756188417Sthompsa		zyd_write16_m(sc, phyp->reg, phyp->val);
1757188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
1758188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
1759188417Sthompsa		zyd_write32_m(sc, ZYD_CR157, val >> 8);
1760184610Salfred	}
1761188417Sthompsa	error = zyd_unlock_phy(sc);
1762188417Sthompsa	if (error != 0)
1763188417Sthompsa		goto fail;
1764184610Salfred
1765184610Salfred	/* HMAC init */
1766188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1767188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1768188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
1769188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
1770188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
1771188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
1772188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
1773188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1774188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1775188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1776188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1777188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1778188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1779188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1780188417Sthompsa	zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
1781188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1782188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1783188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1784188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
1785188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
1786184610Salfred
1787188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211) {
1788188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
1789188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1790186730Salfred	} else {
1791188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1792188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1793188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1794188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1795188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1796188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1797188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1798188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
1799188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
1800186730Salfred	}
1801186730Salfred
1802184610Salfred	/* init beacon interval to 100ms */
1803188417Sthompsa	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1804188417Sthompsa		goto fail;
1805184610Salfred
1806188417Sthompsa	if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
1807188417Sthompsa		device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
1808188417Sthompsa		    sc->sc_rfrev);
1809188417Sthompsa		goto fail;
1810188417Sthompsa	}
1811188417Sthompsa
1812188417Sthompsa	/* RF chip init */
1813188417Sthompsa	error = zyd_lock_phy(sc);
1814188417Sthompsa	if (error != 0)
1815188417Sthompsa		goto fail;
1816188417Sthompsa	error = (*rf->init)(rf);
1817188417Sthompsa	if (error != 0) {
1818188417Sthompsa		device_printf(sc->sc_dev,
1819188417Sthompsa		    "radio initialization failed, error %d\n", error);
1820188417Sthompsa		goto fail;
1821188417Sthompsa	}
1822188417Sthompsa	error = zyd_unlock_phy(sc);
1823188417Sthompsa	if (error != 0)
1824188417Sthompsa		goto fail;
1825188417Sthompsa
1826188417Sthompsa	if ((error = zyd_read_eeprom(sc)) != 0) {
1827188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
1828188417Sthompsa		goto fail;
1829188417Sthompsa	}
1830188417Sthompsa
1831188417Sthompsafail:	return (error);
1832184610Salfred}
1833184610Salfred
1834188417Sthompsastatic int
1835188417Sthompsazyd_read_pod(struct zyd_softc *sc)
1836184610Salfred{
1837188417Sthompsa	int error;
1838184610Salfred	uint32_t tmp;
1839184610Salfred
1840188417Sthompsa	zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
1841188417Sthompsa	sc->sc_rfrev     = tmp & 0x0f;
1842188417Sthompsa	sc->sc_ledtype   = (tmp >>  4) & 0x01;
1843188417Sthompsa	sc->sc_al2230s   = (tmp >>  7) & 0x01;
1844188417Sthompsa	sc->sc_cckgain   = (tmp >>  8) & 0x01;
1845184610Salfred	sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
1846188417Sthompsa	sc->sc_parev     = (tmp >> 16) & 0x0f;
1847186730Salfred	sc->sc_bandedge6 = (tmp >> 21) & 0x01;
1848188417Sthompsa	sc->sc_newphy    = (tmp >> 31) & 0x01;
1849188417Sthompsa	sc->sc_txled     = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
1850188417Sthompsafail:
1851188417Sthompsa	return (error);
1852188417Sthompsa}
1853184610Salfred
1854188417Sthompsastatic int
1855188417Sthompsazyd_read_eeprom(struct zyd_softc *sc)
1856188417Sthompsa{
1857188417Sthompsa	uint16_t val;
1858188417Sthompsa	int error, i;
1859184610Salfred
1860184610Salfred	/* read Tx power calibration tables */
1861184610Salfred	for (i = 0; i < 7; i++) {
1862188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1863188417Sthompsa		sc->sc_pwrcal[i * 2] = val >> 8;
1864188417Sthompsa		sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
1865188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
1866188417Sthompsa		sc->sc_pwrint[i * 2] = val >> 8;
1867188417Sthompsa		sc->sc_pwrint[i * 2 + 1] = val & 0xff;
1868188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
1869188417Sthompsa		sc->sc_ofdm36_cal[i * 2] = val >> 8;
1870188417Sthompsa		sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
1871188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
1872188417Sthompsa		sc->sc_ofdm48_cal[i * 2] = val >> 8;
1873188417Sthompsa		sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
1874188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
1875188417Sthompsa		sc->sc_ofdm54_cal[i * 2] = val >> 8;
1876188417Sthompsa		sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
1877184610Salfred	}
1878188417Sthompsafail:
1879188417Sthompsa	return (error);
1880184610Salfred}
1881184610Salfred
1882188417Sthompsastatic int
1883188417Sthompsazyd_get_macaddr(struct zyd_softc *sc)
1884186730Salfred{
1885192984Sthompsa	struct usb_device_request req;
1886193045Sthompsa	usb_error_t error;
1887186730Salfred
1888186730Salfred	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1889186730Salfred	req.bRequest = ZYD_READFWDATAREQ;
1890186730Salfred	USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
1891186730Salfred	USETW(req.wIndex, 0);
1892186730Salfred	USETW(req.wLength, IEEE80211_ADDR_LEN);
1893186730Salfred
1894287197Sglebius	error = zyd_do_request(sc, &req, sc->sc_ic.ic_macaddr);
1895188417Sthompsa	if (error != 0) {
1896188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1897194228Sthompsa		    usbd_errstr(error));
1898188417Sthompsa	}
1899188417Sthompsa
1900188417Sthompsa	return (error);
1901186730Salfred}
1902186730Salfred
1903188417Sthompsastatic int
1904188417Sthompsazyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1905184610Salfred{
1906188417Sthompsa	int error;
1907184610Salfred	uint32_t tmp;
1908184610Salfred
1909188417Sthompsa	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1910188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
1911188417Sthompsa	tmp = addr[5] << 8 | addr[4];
1912188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
1913188417Sthompsafail:
1914188417Sthompsa	return (error);
1915188417Sthompsa}
1916184610Salfred
1917188417Sthompsastatic int
1918188417Sthompsazyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1919188417Sthompsa{
1920188417Sthompsa	int error;
1921188417Sthompsa	uint32_t tmp;
1922188417Sthompsa
1923188417Sthompsa	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1924188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
1925188417Sthompsa	tmp = addr[5] << 8 | addr[4];
1926188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
1927188417Sthompsafail:
1928188417Sthompsa	return (error);
1929184610Salfred}
1930184610Salfred
1931188417Sthompsastatic int
1932188417Sthompsazyd_switch_radio(struct zyd_softc *sc, int on)
1933184610Salfred{
1934188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1935188417Sthompsa	int error;
1936188417Sthompsa
1937188417Sthompsa	error = zyd_lock_phy(sc);
1938188417Sthompsa	if (error != 0)
1939188417Sthompsa		goto fail;
1940188417Sthompsa	error = (*rf->switch_radio)(rf, on);
1941188417Sthompsa	if (error != 0)
1942188417Sthompsa		goto fail;
1943188417Sthompsa	error = zyd_unlock_phy(sc);
1944188417Sthompsafail:
1945188417Sthompsa	return (error);
1946184610Salfred}
1947184610Salfred
1948188417Sthompsastatic int
1949188417Sthompsazyd_set_led(struct zyd_softc *sc, int which, int on)
1950184610Salfred{
1951188417Sthompsa	int error;
1952184610Salfred	uint32_t tmp;
1953184610Salfred
1954188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1955188417Sthompsa	tmp &= ~which;
1956188417Sthompsa	if (on)
1957188417Sthompsa		tmp |= which;
1958188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1959188417Sthompsafail:
1960188417Sthompsa	return (error);
1961188417Sthompsa}
1962184610Salfred
1963188417Sthompsastatic void
1964188417Sthompsazyd_set_multi(struct zyd_softc *sc)
1965184610Salfred{
1966287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1967287197Sglebius	uint32_t low, high;
1968286437Sadrian	int error;
1969184610Salfred
1970287197Sglebius	if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0)
1971188417Sthompsa		return;
1972184610Salfred
1973188417Sthompsa	low = 0x00000000;
1974188417Sthompsa	high = 0x80000000;
1975184610Salfred
1976287197Sglebius	if (ic->ic_opmode == IEEE80211_M_MONITOR || ic->ic_allmulti > 0 ||
1977287197Sglebius	    ic->ic_promisc > 0) {
1978188417Sthompsa		low = 0xffffffff;
1979188417Sthompsa		high = 0xffffffff;
1980184610Salfred	} else {
1981287197Sglebius		struct ieee80211vap *vap;
1982287197Sglebius		struct ifnet *ifp;
1983287197Sglebius		struct ifmultiaddr *ifma;
1984287197Sglebius		uint8_t v;
1985287197Sglebius
1986287197Sglebius		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1987287197Sglebius			ifp = vap->iv_ifp;
1988287197Sglebius			if_maddr_rlock(ifp);
1989287197Sglebius			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1990287197Sglebius				if (ifma->ifma_addr->sa_family != AF_LINK)
1991287197Sglebius					continue;
1992287197Sglebius				v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
1993287197Sglebius				    ifma->ifma_addr))[5] >> 2;
1994287197Sglebius				if (v < 32)
1995287197Sglebius					low |= 1 << v;
1996287197Sglebius				else
1997287197Sglebius					high |= 1 << (v - 32);
1998287197Sglebius			}
1999287197Sglebius			if_maddr_runlock(ifp);
2000188417Sthompsa		}
2001184610Salfred	}
2002184610Salfred
2003188417Sthompsa	/* reprogram multicast global hash table */
2004188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBL, low);
2005188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBH, high);
2006188417Sthompsafail:
2007188417Sthompsa	if (error != 0)
2008188417Sthompsa		device_printf(sc->sc_dev,
2009188417Sthompsa		    "could not set multicast hash table\n");
2010188417Sthompsa}
2011184610Salfred
2012188417Sthompsastatic void
2013283540Sglebiuszyd_update_mcast(struct ieee80211com *ic)
2014188417Sthompsa{
2015283540Sglebius	struct zyd_softc *sc = ic->ic_softc;
2016184610Salfred
2017188417Sthompsa	ZYD_LOCK(sc);
2018191746Sthompsa	zyd_set_multi(sc);
2019188417Sthompsa	ZYD_UNLOCK(sc);
2020188417Sthompsa}
2021184610Salfred
2022188417Sthompsastatic int
2023188417Sthompsazyd_set_rxfilter(struct zyd_softc *sc)
2024188417Sthompsa{
2025287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2026188417Sthompsa	uint32_t rxfilter;
2027184610Salfred
2028188417Sthompsa	switch (ic->ic_opmode) {
2029188417Sthompsa	case IEEE80211_M_STA:
2030188417Sthompsa		rxfilter = ZYD_FILTER_BSS;
2031188417Sthompsa		break;
2032188417Sthompsa	case IEEE80211_M_IBSS:
2033188417Sthompsa	case IEEE80211_M_HOSTAP:
2034188417Sthompsa		rxfilter = ZYD_FILTER_HOSTAP;
2035188417Sthompsa		break;
2036188417Sthompsa	case IEEE80211_M_MONITOR:
2037188417Sthompsa		rxfilter = ZYD_FILTER_MONITOR;
2038188417Sthompsa		break;
2039188417Sthompsa	default:
2040188417Sthompsa		/* should not get there */
2041188417Sthompsa		return (EINVAL);
2042184610Salfred	}
2043188417Sthompsa	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
2044188417Sthompsa}
2045184610Salfred
2046188417Sthompsastatic void
2047188417Sthompsazyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
2048188417Sthompsa{
2049188417Sthompsa	int error;
2050287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2051188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
2052188417Sthompsa	uint32_t tmp;
2053188601Sthompsa	int chan;
2054184610Salfred
2055188417Sthompsa	chan = ieee80211_chan2ieee(ic, c);
2056188417Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
2057188417Sthompsa		/* XXX should NEVER happen */
2058188417Sthompsa		device_printf(sc->sc_dev,
2059188417Sthompsa		    "%s: invalid channel %x\n", __func__, chan);
2060188417Sthompsa		return;
2061188417Sthompsa	}
2062184610Salfred
2063188417Sthompsa	error = zyd_lock_phy(sc);
2064188417Sthompsa	if (error != 0)
2065188417Sthompsa		goto fail;
2066184610Salfred
2067188417Sthompsa	error = (*rf->set_channel)(rf, chan);
2068188417Sthompsa	if (error != 0)
2069188417Sthompsa		goto fail;
2070184610Salfred
2071193420Sweongyo	if (rf->update_pwr) {
2072193420Sweongyo		/* update Tx power */
2073193420Sweongyo		zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
2074184610Salfred
2075193420Sweongyo		if (sc->sc_macrev == ZYD_ZD1211B) {
2076193420Sweongyo			zyd_write16_m(sc, ZYD_CR67,
2077193420Sweongyo			    sc->sc_ofdm36_cal[chan - 1]);
2078193420Sweongyo			zyd_write16_m(sc, ZYD_CR66,
2079193420Sweongyo			    sc->sc_ofdm48_cal[chan - 1]);
2080193420Sweongyo			zyd_write16_m(sc, ZYD_CR65,
2081193420Sweongyo			    sc->sc_ofdm54_cal[chan - 1]);
2082193420Sweongyo			zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
2083193420Sweongyo			zyd_write16_m(sc, ZYD_CR69, 0x28);
2084193420Sweongyo			zyd_write16_m(sc, ZYD_CR69, 0x2a);
2085193420Sweongyo		}
2086188417Sthompsa	}
2087188417Sthompsa	if (sc->sc_cckgain) {
2088188417Sthompsa		/* set CCK baseband gain from EEPROM */
2089188417Sthompsa		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
2090188417Sthompsa			zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
2091188417Sthompsa	}
2092188417Sthompsa	if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
2093188417Sthompsa		error = (*rf->bandedge6)(rf, c);
2094188417Sthompsa		if (error != 0)
2095188417Sthompsa			goto fail;
2096188417Sthompsa	}
2097188417Sthompsa	zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
2098184610Salfred
2099188417Sthompsa	error = zyd_unlock_phy(sc);
2100188417Sthompsa	if (error != 0)
2101188417Sthompsa		goto fail;
2102184610Salfred
2103188417Sthompsa	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
2104188417Sthompsa	    htole16(c->ic_freq);
2105188417Sthompsa	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
2106188417Sthompsa	    htole16(c->ic_flags);
2107188417Sthompsafail:
2108184610Salfred	return;
2109184610Salfred}
2110184610Salfred
2111184610Salfredstatic int
2112188417Sthompsazyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
2113184610Salfred{
2114188417Sthompsa	int error;
2115188417Sthompsa	uint32_t val;
2116184610Salfred
2117188417Sthompsa	zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
2118188417Sthompsa	sc->sc_atim_wnd = val;
2119188417Sthompsa	zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
2120188417Sthompsa	sc->sc_pre_tbtt = val;
2121188417Sthompsa	sc->sc_bcn_int = bintval;
2122184610Salfred
2123188417Sthompsa	if (sc->sc_bcn_int <= 5)
2124188417Sthompsa		sc->sc_bcn_int = 5;
2125188417Sthompsa	if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
2126188417Sthompsa		sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
2127188417Sthompsa	if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
2128188417Sthompsa		sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
2129184610Salfred
2130188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
2131188417Sthompsa	zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
2132188417Sthompsa	zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
2133188417Sthompsafail:
2134188417Sthompsa	return (error);
2135188417Sthompsa}
2136184610Salfred
2137188417Sthompsastatic void
2138192984Sthompsazyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
2139188417Sthompsa{
2140194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2141287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2142188417Sthompsa	struct zyd_plcphdr plcp;
2143188417Sthompsa	struct zyd_rx_stat stat;
2144194677Sthompsa	struct usb_page_cache *pc;
2145188417Sthompsa	struct mbuf *m;
2146188417Sthompsa	int rlen, rssi;
2147184610Salfred
2148188417Sthompsa	if (len < ZYD_MIN_FRAGSZ) {
2149188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
2150188417Sthompsa		    device_get_nameunit(sc->sc_dev), len);
2151287197Sglebius		counter_u64_add(ic->ic_ierrors, 1);
2152188417Sthompsa		return;
2153188417Sthompsa	}
2154194677Sthompsa	pc = usbd_xfer_get_frame(xfer, 0);
2155194677Sthompsa	usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2156194677Sthompsa	usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
2157184610Salfred
2158188417Sthompsa	if (stat.flags & ZYD_RX_ERROR) {
2159188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV,
2160188417Sthompsa		    "%s: RX status indicated error (%x)\n",
2161188417Sthompsa		    device_get_nameunit(sc->sc_dev), stat.flags);
2162287197Sglebius		counter_u64_add(ic->ic_ierrors, 1);
2163188417Sthompsa		return;
2164188417Sthompsa	}
2165184610Salfred
2166188417Sthompsa	/* compute actual frame length */
2167188417Sthompsa	rlen = len - sizeof(struct zyd_plcphdr) -
2168188417Sthompsa	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
2169184610Salfred
2170188417Sthompsa	/* allocate a mbuf to store the frame */
2171233774Shselasky	if (rlen > (int)MCLBYTES) {
2172188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
2173188417Sthompsa		    device_get_nameunit(sc->sc_dev), rlen);
2174287197Sglebius		counter_u64_add(ic->ic_ierrors, 1);
2175188417Sthompsa		return;
2176233774Shselasky	} else if (rlen > (int)MHLEN)
2177243857Sglebius		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2178188417Sthompsa	else
2179243857Sglebius		m = m_gethdr(M_NOWAIT, MT_DATA);
2180188417Sthompsa	if (m == NULL) {
2181188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
2182188417Sthompsa		    device_get_nameunit(sc->sc_dev));
2183287197Sglebius		counter_u64_add(ic->ic_ierrors, 1);
2184188417Sthompsa		return;
2185184610Salfred	}
2186188417Sthompsa	m->m_pkthdr.len = m->m_len = rlen;
2187194677Sthompsa	usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
2188184610Salfred
2189192468Ssam	if (ieee80211_radiotap_active(ic)) {
2190188417Sthompsa		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
2191184610Salfred
2192188417Sthompsa		tap->wr_flags = 0;
2193188417Sthompsa		if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
2194188417Sthompsa			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2195188417Sthompsa		/* XXX toss, no way to express errors */
2196188417Sthompsa		if (stat.flags & ZYD_RX_DECRYPTERR)
2197188417Sthompsa			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2198188417Sthompsa		tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
2199188417Sthompsa		    (stat.flags & ZYD_RX_OFDM) ?
2200188417Sthompsa			IEEE80211_T_OFDM : IEEE80211_T_CCK);
2201188417Sthompsa		tap->wr_antsignal = stat.rssi + -95;
2202188417Sthompsa		tap->wr_antnoise = -95;	/* XXX */
2203188417Sthompsa	}
2204188417Sthompsa	rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
2205184610Salfred
2206188417Sthompsa	sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
2207188417Sthompsa	sc->sc_rx_data[sc->sc_rx_count].m = m;
2208188417Sthompsa	sc->sc_rx_count++;
2209184610Salfred}
2210184610Salfred
2211184610Salfredstatic void
2212194677Sthompsazyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
2213184610Salfred{
2214194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2215287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2216188417Sthompsa	struct ieee80211_node *ni;
2217188417Sthompsa	struct zyd_rx_desc desc;
2218188417Sthompsa	struct mbuf *m;
2219194677Sthompsa	struct usb_page_cache *pc;
2220188417Sthompsa	uint32_t offset;
2221188417Sthompsa	uint8_t rssi;
2222188417Sthompsa	int8_t nf;
2223188417Sthompsa	int i;
2224194677Sthompsa	int actlen;
2225184610Salfred
2226194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2227194677Sthompsa
2228188417Sthompsa	sc->sc_rx_count = 0;
2229188417Sthompsa	switch (USB_GET_STATE(xfer)) {
2230188417Sthompsa	case USB_ST_TRANSFERRED:
2231194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
2232194677Sthompsa		usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
2233184610Salfred
2234188417Sthompsa		offset = 0;
2235188417Sthompsa		if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
2236188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_RECV,
2237188417Sthompsa			    "%s: received multi-frame transfer\n", __func__);
2238184610Salfred
2239188417Sthompsa			for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2240188417Sthompsa				uint16_t len16 = UGETW(desc.len[i]);
2241184610Salfred
2242194677Sthompsa				if (len16 == 0 || len16 > actlen)
2243188417Sthompsa					break;
2244184610Salfred
2245188417Sthompsa				zyd_rx_data(xfer, offset, len16);
2246184610Salfred
2247188417Sthompsa				/* next frame is aligned on a 32-bit boundary */
2248188417Sthompsa				len16 = (len16 + 3) & ~3;
2249188417Sthompsa				offset += len16;
2250194677Sthompsa				if (len16 > actlen)
2251188417Sthompsa					break;
2252194677Sthompsa				actlen -= len16;
2253188417Sthompsa			}
2254188417Sthompsa		} else {
2255188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_RECV,
2256188417Sthompsa			    "%s: received single-frame transfer\n", __func__);
2257184610Salfred
2258194677Sthompsa			zyd_rx_data(xfer, 0, actlen);
2259188417Sthompsa		}
2260188417Sthompsa		/* FALLTHROUGH */
2261188417Sthompsa	case USB_ST_SETUP:
2262188417Sthompsatr_setup:
2263194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2264194228Sthompsa		usbd_transfer_submit(xfer);
2265184610Salfred
2266188417Sthompsa		/*
2267188417Sthompsa		 * At the end of a USB callback it is always safe to unlock
2268188417Sthompsa		 * the private mutex of a device! That is why we do the
2269188417Sthompsa		 * "ieee80211_input" here, and not some lines up!
2270188417Sthompsa		 */
2271188417Sthompsa		ZYD_UNLOCK(sc);
2272188417Sthompsa		for (i = 0; i < sc->sc_rx_count; i++) {
2273188417Sthompsa			rssi = sc->sc_rx_data[i].rssi;
2274188417Sthompsa			m = sc->sc_rx_data[i].m;
2275188417Sthompsa			sc->sc_rx_data[i].m = NULL;
2276184610Salfred
2277188417Sthompsa			nf = -95;	/* XXX */
2278184610Salfred
2279188417Sthompsa			ni = ieee80211_find_rxnode(ic,
2280188417Sthompsa			    mtod(m, struct ieee80211_frame_min *));
2281188417Sthompsa			if (ni != NULL) {
2282192468Ssam				(void)ieee80211_input(ni, m, rssi, nf);
2283188417Sthompsa				ieee80211_free_node(ni);
2284188417Sthompsa			} else
2285192468Ssam				(void)ieee80211_input_all(ic, m, rssi, nf);
2286188417Sthompsa		}
2287188417Sthompsa		ZYD_LOCK(sc);
2288287197Sglebius		zyd_start(sc);
2289188417Sthompsa		break;
2290184610Salfred
2291188417Sthompsa	default:			/* Error */
2292194677Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
2293184610Salfred
2294194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2295188417Sthompsa			/* try to clear stall first */
2296194677Sthompsa			usbd_xfer_set_stall(xfer);
2297188417Sthompsa			goto tr_setup;
2298184610Salfred		}
2299188417Sthompsa		break;
2300184610Salfred	}
2301184610Salfred}
2302184610Salfred
2303184610Salfredstatic uint8_t
2304193803Sweongyozyd_plcp_signal(struct zyd_softc *sc, int rate)
2305184610Salfred{
2306184610Salfred	switch (rate) {
2307188417Sthompsa	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
2308184610Salfred	case 12:
2309184610Salfred		return (0xb);
2310184610Salfred	case 18:
2311184610Salfred		return (0xf);
2312184610Salfred	case 24:
2313184610Salfred		return (0xa);
2314184610Salfred	case 36:
2315184610Salfred		return (0xe);
2316184610Salfred	case 48:
2317184610Salfred		return (0x9);
2318184610Salfred	case 72:
2319184610Salfred		return (0xd);
2320184610Salfred	case 96:
2321184610Salfred		return (0x8);
2322184610Salfred	case 108:
2323184610Salfred		return (0xc);
2324188417Sthompsa	/* CCK rates (NB: not IEEE std, device-specific) */
2325188417Sthompsa	case 2:
2326188417Sthompsa		return (0x0);
2327188417Sthompsa	case 4:
2328188417Sthompsa		return (0x1);
2329188417Sthompsa	case 11:
2330188417Sthompsa		return (0x2);
2331188417Sthompsa	case 22:
2332188417Sthompsa		return (0x3);
2333184610Salfred	}
2334184610Salfred
2335193803Sweongyo	device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2336193803Sweongyo	return (0x0);
2337184610Salfred}
2338184610Salfred
2339184610Salfredstatic void
2340194677Sthompsazyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
2341184610Salfred{
2342194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2343192468Ssam	struct ieee80211vap *vap;
2344188417Sthompsa	struct zyd_tx_data *data;
2345188417Sthompsa	struct mbuf *m;
2346194677Sthompsa	struct usb_page_cache *pc;
2347194677Sthompsa	int actlen;
2348184610Salfred
2349194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2350194677Sthompsa
2351188417Sthompsa	switch (USB_GET_STATE(xfer)) {
2352188417Sthompsa	case USB_ST_TRANSFERRED:
2353188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2354194677Sthompsa		    actlen);
2355184610Salfred
2356188417Sthompsa		/* free resources */
2357194677Sthompsa		data = usbd_xfer_get_priv(xfer);
2358188417Sthompsa		zyd_tx_free(data, 0);
2359194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2360184610Salfred
2361188417Sthompsa		/* FALLTHROUGH */
2362188417Sthompsa	case USB_ST_SETUP:
2363188417Sthompsatr_setup:
2364188417Sthompsa		data = STAILQ_FIRST(&sc->tx_q);
2365188417Sthompsa		if (data) {
2366188417Sthompsa			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
2367188417Sthompsa			m = data->m;
2368184610Salfred
2369233774Shselasky			if (m->m_pkthdr.len > (int)ZYD_MAX_TXBUFSZ) {
2370188417Sthompsa				DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
2371188417Sthompsa				    m->m_pkthdr.len);
2372188417Sthompsa				m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
2373188417Sthompsa			}
2374194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
2375194677Sthompsa			usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2376194677Sthompsa			usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
2377188417Sthompsa			    m->m_pkthdr.len);
2378184610Salfred
2379192468Ssam			vap = data->ni->ni_vap;
2380192468Ssam			if (ieee80211_radiotap_active_vap(vap)) {
2381188417Sthompsa				struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2382184610Salfred
2383188417Sthompsa				tap->wt_flags = 0;
2384188417Sthompsa				tap->wt_rate = data->rate;
2385184610Salfred
2386192468Ssam				ieee80211_radiotap_tx(vap, m);
2387188417Sthompsa			}
2388184610Salfred
2389194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2390194677Sthompsa			usbd_xfer_set_priv(xfer, data);
2391194228Sthompsa			usbd_transfer_submit(xfer);
2392188417Sthompsa		}
2393287197Sglebius		zyd_start(sc);
2394188417Sthompsa		break;
2395184610Salfred
2396188417Sthompsa	default:			/* Error */
2397188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2398194677Sthompsa		    usbd_errstr(error));
2399184610Salfred
2400287197Sglebius		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
2401194677Sthompsa		data = usbd_xfer_get_priv(xfer);
2402194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2403188419Sthompsa		if (data != NULL)
2404194677Sthompsa			zyd_tx_free(data, error);
2405188419Sthompsa
2406203141Sthompsa		if (error != USB_ERR_CANCELLED) {
2407203141Sthompsa			if (error == USB_ERR_TIMEOUT)
2408203141Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2409203141Sthompsa
2410203141Sthompsa			/*
2411203141Sthompsa			 * Try to clear stall first, also if other
2412203141Sthompsa			 * errors occur, hence clearing stall
2413203141Sthompsa			 * introduces a 50 ms delay:
2414203141Sthompsa			 */
2415194677Sthompsa			usbd_xfer_set_stall(xfer);
2416188417Sthompsa			goto tr_setup;
2417188417Sthompsa		}
2418184610Salfred		break;
2419184610Salfred	}
2420184610Salfred}
2421184610Salfred
2422188417Sthompsastatic int
2423193803Sweongyozyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2424184610Salfred{
2425188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2426188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2427188417Sthompsa	struct zyd_tx_desc *desc;
2428188417Sthompsa	struct zyd_tx_data *data;
2429188417Sthompsa	struct ieee80211_frame *wh;
2430188417Sthompsa	const struct ieee80211_txparam *tp;
2431188417Sthompsa	struct ieee80211_key *k;
2432188417Sthompsa	int rate, totlen;
2433269127Shselasky	static const uint8_t ratediv[] = ZYD_TX_RATEDIV;
2434193803Sweongyo	uint8_t phy;
2435188417Sthompsa	uint16_t pktlen;
2436193803Sweongyo	uint32_t bits;
2437184610Salfred
2438188417Sthompsa	wh = mtod(m0, struct ieee80211_frame *);
2439188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
2440188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
2441188417Sthompsa	sc->tx_nfree--;
2442184610Salfred
2443193803Sweongyo	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT ||
2444193803Sweongyo	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) {
2445193803Sweongyo		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2446193803Sweongyo		rate = tp->mgmtrate;
2447188417Sthompsa	} else {
2448193803Sweongyo		tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
2449193803Sweongyo		/* for data frames */
2450193803Sweongyo		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
2451193803Sweongyo			rate = tp->mcastrate;
2452193803Sweongyo		else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2453193803Sweongyo			rate = tp->ucastrate;
2454193803Sweongyo		else {
2455206358Srpaulo			(void) ieee80211_ratectl_rate(ni, NULL, 0);
2456193803Sweongyo			rate = ni->ni_txrate;
2457193803Sweongyo		}
2458184610Salfred	}
2459184610Salfred
2460260444Skevlo	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2461188417Sthompsa		k = ieee80211_crypto_encap(ni, m0);
2462188417Sthompsa		if (k == NULL) {
2463188417Sthompsa			return (ENOBUFS);
2464188417Sthompsa		}
2465188417Sthompsa		/* packet header may have moved, reset our local pointer */
2466188417Sthompsa		wh = mtod(m0, struct ieee80211_frame *);
2467184610Salfred	}
2468184610Salfred
2469188417Sthompsa	data->ni = ni;
2470188417Sthompsa	data->m = m0;
2471193803Sweongyo	data->rate = rate;
2472184610Salfred
2473193803Sweongyo	/* fill Tx descriptor */
2474193803Sweongyo	desc = &data->desc;
2475193803Sweongyo	phy = zyd_plcp_signal(sc, rate);
2476193803Sweongyo	desc->phy = phy;
2477193803Sweongyo	if (ZYD_RATE_IS_OFDM(rate)) {
2478193803Sweongyo		desc->phy |= ZYD_TX_PHY_OFDM;
2479193803Sweongyo		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2480193803Sweongyo			desc->phy |= ZYD_TX_PHY_5GHZ;
2481193803Sweongyo	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2482193803Sweongyo		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2483193803Sweongyo
2484188417Sthompsa	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2485188417Sthompsa	desc->len = htole16(totlen);
2486184610Salfred
2487193803Sweongyo	desc->flags = ZYD_TX_FLAG_BACKOFF;
2488188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2489188417Sthompsa		/* multicast frames are not sent at OFDM rates in 802.11b/g */
2490188417Sthompsa		if (totlen > vap->iv_rtsthreshold) {
2491188417Sthompsa			desc->flags |= ZYD_TX_FLAG_RTS;
2492188417Sthompsa		} else if (ZYD_RATE_IS_OFDM(rate) &&
2493188417Sthompsa		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
2494188417Sthompsa			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2495188417Sthompsa				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2496188417Sthompsa			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2497188417Sthompsa				desc->flags |= ZYD_TX_FLAG_RTS;
2498188417Sthompsa		}
2499193803Sweongyo	} else
2500193803Sweongyo		desc->flags |= ZYD_TX_FLAG_MULTICAST;
2501188417Sthompsa	if ((wh->i_fc[0] &
2502188417Sthompsa	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2503188417Sthompsa	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2504188417Sthompsa		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2505188417Sthompsa
2506184610Salfred	/* actual transmit length (XXX why +10?) */
2507193803Sweongyo	pktlen = ZYD_TX_DESC_SIZE + 10;
2508188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211)
2509184610Salfred		pktlen += totlen;
2510188417Sthompsa	desc->pktlen = htole16(pktlen);
2511184610Salfred
2512193803Sweongyo	bits = (rate == 11) ? (totlen * 16) + 10 :
2513193803Sweongyo	    ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2514196809Sweongyo	desc->plcp_length = htole16(bits / ratediv[phy]);
2515188417Sthompsa	desc->plcp_service = 0;
2516193803Sweongyo	if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
2517193803Sweongyo		desc->plcp_service |= ZYD_PLCP_LENGEXT;
2518193803Sweongyo	desc->nextlen = 0;
2519193803Sweongyo
2520193803Sweongyo	if (ieee80211_radiotap_active_vap(vap)) {
2521193803Sweongyo		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2522193803Sweongyo
2523193803Sweongyo		tap->wt_flags = 0;
2524193803Sweongyo		tap->wt_rate = rate;
2525193803Sweongyo
2526193803Sweongyo		ieee80211_radiotap_tx(vap, m0);
2527184610Salfred	}
2528184610Salfred
2529188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_XMIT,
2530188417Sthompsa	    "%s: sending data frame len=%zu rate=%u\n",
2531188417Sthompsa	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2532188417Sthompsa		rate);
2533184610Salfred
2534188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2535194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
2536184610Salfred
2537188417Sthompsa	return (0);
2538184610Salfred}
2539184610Salfred
2540287197Sglebiusstatic int
2541287197Sglebiuszyd_transmit(struct ieee80211com *ic, struct mbuf *m)
2542287197Sglebius{
2543287197Sglebius	struct zyd_softc *sc = ic->ic_softc;
2544287197Sglebius	int error;
2545287197Sglebius
2546287197Sglebius	ZYD_LOCK(sc);
2547287197Sglebius	if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) {
2548287197Sglebius		ZYD_UNLOCK(sc);
2549287197Sglebius		return (ENXIO);
2550287197Sglebius	}
2551287197Sglebius	error = mbufq_enqueue(&sc->sc_snd, m);
2552287197Sglebius	if (error) {
2553287197Sglebius		ZYD_UNLOCK(sc);
2554287197Sglebius		return (error);
2555287197Sglebius	}
2556287197Sglebius	zyd_start(sc);
2557287197Sglebius	ZYD_UNLOCK(sc);
2558287197Sglebius
2559287197Sglebius	return (0);
2560287197Sglebius}
2561287197Sglebius
2562184610Salfredstatic void
2563287197Sglebiuszyd_start(struct zyd_softc *sc)
2564184610Salfred{
2565188417Sthompsa	struct ieee80211_node *ni;
2566184610Salfred	struct mbuf *m;
2567184610Salfred
2568287197Sglebius	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2569287197Sglebius
2570287197Sglebius	while (sc->tx_nfree > 0 && (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
2571188417Sthompsa		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2572193803Sweongyo		if (zyd_tx_start(sc, m, ni) != 0) {
2573288648Sadrian			m_freem(m);
2574287197Sglebius			if_inc_counter(ni->ni_vap->iv_ifp,
2575287197Sglebius			    IFCOUNTER_OERRORS, 1);
2576314222Savos			ieee80211_free_node(ni);
2577188417Sthompsa			break;
2578184610Salfred		}
2579184610Salfred	}
2580184610Salfred}
2581184610Salfred
2582188417Sthompsastatic int
2583188417Sthompsazyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2584188417Sthompsa	const struct ieee80211_bpf_params *params)
2585184610Salfred{
2586188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2587286950Sadrian	struct zyd_softc *sc = ic->ic_softc;
2588184610Salfred
2589188417Sthompsa	ZYD_LOCK(sc);
2590188417Sthompsa	/* prevent management frames from being sent if we're not ready */
2591287197Sglebius	if (!(sc->sc_flags & ZYD_FLAG_RUNNING)) {
2592188417Sthompsa		ZYD_UNLOCK(sc);
2593188417Sthompsa		m_freem(m);
2594188417Sthompsa		return (ENETDOWN);
2595188417Sthompsa	}
2596188417Sthompsa	if (sc->tx_nfree == 0) {
2597188417Sthompsa		ZYD_UNLOCK(sc);
2598188417Sthompsa		m_freem(m);
2599188417Sthompsa		return (ENOBUFS);		/* XXX */
2600188417Sthompsa	}
2601188417Sthompsa
2602188417Sthompsa	/*
2603188417Sthompsa	 * Legacy path; interpret frame contents to decide
2604188417Sthompsa	 * precisely how to send the frame.
2605188417Sthompsa	 * XXX raw path
2606188417Sthompsa	 */
2607193803Sweongyo	if (zyd_tx_start(sc, m, ni) != 0) {
2608188417Sthompsa		ZYD_UNLOCK(sc);
2609288648Sadrian		m_freem(m);
2610188417Sthompsa		return (EIO);
2611188417Sthompsa	}
2612188417Sthompsa	ZYD_UNLOCK(sc);
2613188417Sthompsa	return (0);
2614184610Salfred}
2615184610Salfred
2616287197Sglebiusstatic void
2617287197Sglebiuszyd_parent(struct ieee80211com *ic)
2618184610Salfred{
2619286950Sadrian	struct zyd_softc *sc = ic->ic_softc;
2620246614Shselasky	int startall = 0;
2621184610Salfred
2622246614Shselasky	ZYD_LOCK(sc);
2623287197Sglebius	if (sc->sc_flags & ZYD_FLAG_DETACHED) {
2624188417Sthompsa		ZYD_UNLOCK(sc);
2625287197Sglebius		return;
2626184610Salfred	}
2627287197Sglebius	if (ic->ic_nrunning > 0) {
2628287197Sglebius		if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) {
2629287197Sglebius			zyd_init_locked(sc);
2630287197Sglebius			startall = 1;
2631287197Sglebius		} else
2632287197Sglebius			zyd_set_multi(sc);
2633287197Sglebius	} else if (sc->sc_flags & ZYD_FLAG_RUNNING)
2634287197Sglebius		zyd_stop(sc);
2635287197Sglebius	ZYD_UNLOCK(sc);
2636287197Sglebius	if (startall)
2637287197Sglebius		ieee80211_start_all(ic);
2638184610Salfred}
2639184610Salfred
2640184610Salfredstatic void
2641191746Sthompsazyd_init_locked(struct zyd_softc *sc)
2642184610Salfred{
2643287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2644287197Sglebius	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2645192984Sthompsa	struct usb_config_descriptor *cd;
2646188419Sthompsa	int error;
2647188417Sthompsa	uint32_t val;
2648184610Salfred
2649188417Sthompsa	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2650184610Salfred
2651188417Sthompsa	if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
2652188417Sthompsa		error = zyd_loadfirmware(sc);
2653188417Sthompsa		if (error != 0) {
2654188417Sthompsa			device_printf(sc->sc_dev,
2655188417Sthompsa			    "could not load firmware (error=%d)\n", error);
2656188417Sthompsa			goto fail;
2657188417Sthompsa		}
2658188417Sthompsa
2659188417Sthompsa		/* reset device */
2660194228Sthompsa		cd = usbd_get_config_descriptor(sc->sc_udev);
2661194228Sthompsa		error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
2662188417Sthompsa		    cd->bConfigurationValue);
2663188417Sthompsa		if (error)
2664188417Sthompsa			device_printf(sc->sc_dev, "reset failed, continuing\n");
2665188417Sthompsa
2666188417Sthompsa		error = zyd_hw_init(sc);
2667188417Sthompsa		if (error) {
2668188417Sthompsa			device_printf(sc->sc_dev,
2669188417Sthompsa			    "hardware initialization failed\n");
2670188417Sthompsa			goto fail;
2671188417Sthompsa		}
2672188417Sthompsa
2673188417Sthompsa		device_printf(sc->sc_dev,
2674188417Sthompsa		    "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
2675188417Sthompsa		    "BE%x NP%x Gain%x F%x\n",
2676188417Sthompsa		    (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
2677188417Sthompsa		    sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
2678188417Sthompsa		    zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
2679188417Sthompsa		    sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
2680188417Sthompsa		    sc->sc_cckgain, sc->sc_fix_cr157);
2681188417Sthompsa
2682188417Sthompsa		/* read regulatory domain (currently unused) */
2683188417Sthompsa		zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
2684188417Sthompsa		sc->sc_regdomain = val >> 16;
2685188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
2686188417Sthompsa		    sc->sc_regdomain);
2687188417Sthompsa
2688188417Sthompsa		/* we'll do software WEP decryption for now */
2689188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
2690188417Sthompsa		    __func__);
2691188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2692188417Sthompsa
2693188417Sthompsa		sc->sc_flags |= ZYD_FLAG_INITONCE;
2694184610Salfred	}
2695184610Salfred
2696287197Sglebius	if (sc->sc_flags & ZYD_FLAG_RUNNING)
2697191746Sthompsa		zyd_stop(sc);
2698188417Sthompsa
2699190526Ssam	DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
2700287197Sglebius	    vap ? vap->iv_myaddr : ic->ic_macaddr, ":");
2701287197Sglebius	error = zyd_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2702188417Sthompsa	if (error != 0)
2703184610Salfred		return;
2704184610Salfred
2705188417Sthompsa	/* set basic rates */
2706188417Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B)
2707188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
2708188417Sthompsa	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2709188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
2710188417Sthompsa	else	/* assumes 802.11b/g */
2711188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
2712184610Salfred
2713188417Sthompsa	/* promiscuous mode */
2714188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
2715188417Sthompsa	/* multicast setup */
2716188417Sthompsa	zyd_set_multi(sc);
2717188417Sthompsa	/* set RX filter  */
2718188417Sthompsa	error = zyd_set_rxfilter(sc);
2719188417Sthompsa	if (error != 0)
2720188417Sthompsa		goto fail;
2721184610Salfred
2722188417Sthompsa	/* switch radio transmitter ON */
2723188417Sthompsa	error = zyd_switch_radio(sc, 1);
2724188417Sthompsa	if (error != 0)
2725188417Sthompsa		goto fail;
2726188417Sthompsa	/* set default BSS channel */
2727188417Sthompsa	zyd_set_chan(sc, ic->ic_curchan);
2728184610Salfred
2729188417Sthompsa	/*
2730188417Sthompsa	 * Allocate Tx and Rx xfer queues.
2731188417Sthompsa	 */
2732188419Sthompsa	zyd_setup_tx_list(sc);
2733184610Salfred
2734188417Sthompsa	/* enable interrupts */
2735188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2736184610Salfred
2737287197Sglebius	sc->sc_flags |= ZYD_FLAG_RUNNING;
2738194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2739194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2740194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
2741184610Salfred
2742188417Sthompsa	return;
2743184610Salfred
2744191746Sthompsafail:	zyd_stop(sc);
2745188417Sthompsa	return;
2746184610Salfred}
2747184610Salfred
2748184610Salfredstatic void
2749191746Sthompsazyd_stop(struct zyd_softc *sc)
2750184610Salfred{
2751188417Sthompsa	int error;
2752184610Salfred
2753188417Sthompsa	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2754184610Salfred
2755287197Sglebius	sc->sc_flags &= ~ZYD_FLAG_RUNNING;
2756288648Sadrian	zyd_drain_mbufq(sc);
2757184610Salfred
2758188417Sthompsa	/*
2759188419Sthompsa	 * Drain all the transfers, if not already drained:
2760188417Sthompsa	 */
2761188419Sthompsa	ZYD_UNLOCK(sc);
2762194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2763194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
2764188419Sthompsa	ZYD_LOCK(sc);
2765188417Sthompsa
2766188419Sthompsa	zyd_unsetup_tx_list(sc);
2767188417Sthompsa
2768188419Sthompsa	/* Stop now if the device was never set up */
2769188419Sthompsa	if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
2770184610Salfred		return;
2771184610Salfred
2772188417Sthompsa	/* switch radio transmitter OFF */
2773188417Sthompsa	error = zyd_switch_radio(sc, 0);
2774188417Sthompsa	if (error != 0)
2775188417Sthompsa		goto fail;
2776188417Sthompsa	/* disable Rx */
2777188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
2778188417Sthompsa	/* disable interrupts */
2779188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
2780184610Salfred
2781188417Sthompsafail:
2782188417Sthompsa	return;
2783184610Salfred}
2784184610Salfred
2785188417Sthompsastatic int
2786188417Sthompsazyd_loadfirmware(struct zyd_softc *sc)
2787184610Salfred{
2788192984Sthompsa	struct usb_device_request req;
2789188417Sthompsa	size_t size;
2790188417Sthompsa	u_char *fw;
2791188417Sthompsa	uint8_t stat;
2792188417Sthompsa	uint16_t addr;
2793184610Salfred
2794188417Sthompsa	if (sc->sc_flags & ZYD_FLAG_FWLOADED)
2795188417Sthompsa		return (0);
2796184610Salfred
2797188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211) {
2798188417Sthompsa		fw = (u_char *)zd1211_firmware;
2799188417Sthompsa		size = sizeof(zd1211_firmware);
2800188417Sthompsa	} else {
2801188417Sthompsa		fw = (u_char *)zd1211b_firmware;
2802188417Sthompsa		size = sizeof(zd1211b_firmware);
2803184610Salfred	}
2804184610Salfred
2805188417Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2806188417Sthompsa	req.bRequest = ZYD_DOWNLOADREQ;
2807188417Sthompsa	USETW(req.wIndex, 0);
2808184610Salfred
2809188417Sthompsa	addr = ZYD_FIRMWARE_START_ADDR;
2810188417Sthompsa	while (size > 0) {
2811188417Sthompsa		/*
2812188417Sthompsa		 * When the transfer size is 4096 bytes, it is not
2813188417Sthompsa		 * likely to be able to transfer it.
2814188417Sthompsa		 * The cause is port or machine or chip?
2815188417Sthompsa		 */
2816188417Sthompsa		const int mlen = min(size, 64);
2817184610Salfred
2818188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_FW,
2819188417Sthompsa		    "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
2820184610Salfred
2821188417Sthompsa		USETW(req.wValue, addr);
2822188417Sthompsa		USETW(req.wLength, mlen);
2823188419Sthompsa		if (zyd_do_request(sc, &req, fw) != 0)
2824188417Sthompsa			return (EIO);
2825184610Salfred
2826188417Sthompsa		addr += mlen / 2;
2827188417Sthompsa		fw   += mlen;
2828188417Sthompsa		size -= mlen;
2829184610Salfred	}
2830184610Salfred
2831188417Sthompsa	/* check whether the upload succeeded */
2832188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2833188417Sthompsa	req.bRequest = ZYD_DOWNLOADSTS;
2834188417Sthompsa	USETW(req.wValue, 0);
2835188417Sthompsa	USETW(req.wIndex, 0);
2836188417Sthompsa	USETW(req.wLength, sizeof(stat));
2837188419Sthompsa	if (zyd_do_request(sc, &req, &stat) != 0)
2838188417Sthompsa		return (EIO);
2839184610Salfred
2840188417Sthompsa	sc->sc_flags |= ZYD_FLAG_FWLOADED;
2841184610Salfred
2842188417Sthompsa	return (stat & 0x80) ? (EIO) : (0);
2843184610Salfred}
2844184610Salfred
2845184610Salfredstatic void
2846188417Sthompsazyd_scan_start(struct ieee80211com *ic)
2847184610Salfred{
2848286950Sadrian	struct zyd_softc *sc = ic->ic_softc;
2849184610Salfred
2850188417Sthompsa	ZYD_LOCK(sc);
2851191746Sthompsa	/* want broadcast address while scanning */
2852287197Sglebius	zyd_set_bssid(sc, ieee80211broadcastaddr);
2853188417Sthompsa	ZYD_UNLOCK(sc);
2854184610Salfred}
2855184610Salfred
2856184610Salfredstatic void
2857188417Sthompsazyd_scan_end(struct ieee80211com *ic)
2858184610Salfred{
2859286950Sadrian	struct zyd_softc *sc = ic->ic_softc;
2860184610Salfred
2861188417Sthompsa	ZYD_LOCK(sc);
2862191746Sthompsa	/* restore previous bssid */
2863296356Savos	zyd_set_bssid(sc, sc->sc_bssid);
2864188417Sthompsa	ZYD_UNLOCK(sc);
2865184610Salfred}
2866184610Salfred
2867184610Salfredstatic void
2868300751Savoszyd_getradiocaps(struct ieee80211com *ic,
2869300751Savos    int maxchans, int *nchans, struct ieee80211_channel chans[])
2870300751Savos{
2871300751Savos	uint8_t bands[IEEE80211_MODE_BYTES];
2872300751Savos
2873300751Savos	memset(bands, 0, sizeof(bands));
2874300751Savos	setbit(bands, IEEE80211_MODE_11B);
2875300751Savos	setbit(bands, IEEE80211_MODE_11G);
2876300751Savos	ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
2877300751Savos	    zyd_chan_2ghz, nitems(zyd_chan_2ghz), bands, 0);
2878300751Savos}
2879300751Savos
2880300751Savosstatic void
2881188417Sthompsazyd_set_channel(struct ieee80211com *ic)
2882184610Salfred{
2883286950Sadrian	struct zyd_softc *sc = ic->ic_softc;
2884184610Salfred
2885188417Sthompsa	ZYD_LOCK(sc);
2886191746Sthompsa	zyd_set_chan(sc, ic->ic_curchan);
2887188417Sthompsa	ZYD_UNLOCK(sc);
2888184610Salfred}
2889184610Salfred
2890188417Sthompsastatic device_method_t zyd_methods[] = {
2891188417Sthompsa        /* Device interface */
2892188417Sthompsa        DEVMETHOD(device_probe, zyd_match),
2893188417Sthompsa        DEVMETHOD(device_attach, zyd_attach),
2894188417Sthompsa        DEVMETHOD(device_detach, zyd_detach),
2895246614Shselasky	DEVMETHOD_END
2896188417Sthompsa};
2897184610Salfred
2898188417Sthompsastatic driver_t zyd_driver = {
2899233774Shselasky	.name = "zyd",
2900233774Shselasky	.methods = zyd_methods,
2901233774Shselasky	.size = sizeof(struct zyd_softc)
2902188417Sthompsa};
2903184610Salfred
2904188417Sthompsastatic devclass_t zyd_devclass;
2905184610Salfred
2906189275SthompsaDRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0);
2907188942SthompsaMODULE_DEPEND(zyd, usb, 1, 1, 1);
2908188417SthompsaMODULE_DEPEND(zyd, wlan, 1, 1, 1);
2909212122SthompsaMODULE_VERSION(zyd, 1);
2910292080SimpUSB_PNP_HOST_INFO(zyd_devs);
2911