if_zyd.c revision 269127
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: head/sys/dev/usb/wlan/if_zyd.c 269127 2014-07-26 16:06:01Z hselasky $	*/
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: head/sys/dev/usb/wlan/if_zyd.c 269127 2014-07-26 16:06:01Z hselasky $");
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");
83192502SthompsaSYSCTL_INT(_hw_usb_zyd, OID_AUTO, debug, CTLFLAG_RW, &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 *);
150188417Sthompsastatic void	zyd_update_mcast(struct ifnet *);
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 *);
157188417Sthompsastatic void	zyd_start(struct ifnet *);
158188417Sthompsastatic int	zyd_raw_xmit(struct ieee80211_node *, struct mbuf *,
159185948Sthompsa		    const struct ieee80211_bpf_params *);
160188417Sthompsastatic int	zyd_ioctl(struct ifnet *, u_long, caddr_t);
161191746Sthompsastatic void	zyd_init_locked(struct zyd_softc *);
162188417Sthompsastatic void	zyd_init(void *);
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 *);
167188417Sthompsastatic void	zyd_set_channel(struct ieee80211com *);
168188417Sthompsastatic int	zyd_rfmd_init(struct zyd_rf *);
169188417Sthompsastatic int	zyd_rfmd_switch_radio(struct zyd_rf *, int);
170188417Sthompsastatic int	zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
171188417Sthompsastatic int	zyd_al2230_init(struct zyd_rf *);
172188417Sthompsastatic int	zyd_al2230_switch_radio(struct zyd_rf *, int);
173188417Sthompsastatic int	zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
174188417Sthompsastatic int	zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t);
175188417Sthompsastatic int	zyd_al2230_init_b(struct zyd_rf *);
176188417Sthompsastatic int	zyd_al7230B_init(struct zyd_rf *);
177188417Sthompsastatic int	zyd_al7230B_switch_radio(struct zyd_rf *, int);
178188417Sthompsastatic int	zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
179188417Sthompsastatic int	zyd_al2210_init(struct zyd_rf *);
180188417Sthompsastatic int	zyd_al2210_switch_radio(struct zyd_rf *, int);
181188417Sthompsastatic int	zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
182188417Sthompsastatic int	zyd_gct_init(struct zyd_rf *);
183188417Sthompsastatic int	zyd_gct_switch_radio(struct zyd_rf *, int);
184188417Sthompsastatic int	zyd_gct_set_channel(struct zyd_rf *, uint8_t);
185193420Sweongyostatic int	zyd_gct_mode(struct zyd_rf *);
186193420Sweongyostatic int	zyd_gct_set_channel_synth(struct zyd_rf *, int, int);
187193420Sweongyostatic int	zyd_gct_write(struct zyd_rf *, uint16_t);
188193420Sweongyostatic int	zyd_gct_txgain(struct zyd_rf *, uint8_t);
189188417Sthompsastatic int	zyd_maxim2_init(struct zyd_rf *);
190188417Sthompsastatic int	zyd_maxim2_switch_radio(struct zyd_rf *, int);
191188417Sthompsastatic int	zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
192184610Salfred
193184610Salfredstatic const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
194184610Salfredstatic const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
195184610Salfred
196184610Salfred/* various supported device vendors/products */
197188417Sthompsa#define ZYD_ZD1211	0
198188417Sthompsa#define ZYD_ZD1211B	1
199184610Salfred
200193419Sweongyo#define	ZYD_ZD1211_DEV(v,p)	\
201193419Sweongyo	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211) }
202193419Sweongyo#define	ZYD_ZD1211B_DEV(v,p)	\
203193419Sweongyo	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211B) }
204223486Shselaskystatic const STRUCT_USB_HOST_ID zyd_devs[] = {
205193419Sweongyo	/* ZYD_ZD1211 */
206193419Sweongyo	ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
207193419Sweongyo	ZYD_ZD1211_DEV(ABOCOM, WL54),
208193419Sweongyo	ZYD_ZD1211_DEV(ASUS, WL159G),
209193419Sweongyo	ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
210193419Sweongyo	ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
211193419Sweongyo	ZYD_ZD1211_DEV(PLANEX2, GWUS54GD),
212193419Sweongyo	ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
213193419Sweongyo	ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
214193419Sweongyo	ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
215193419Sweongyo	ZYD_ZD1211_DEV(SAGEM, XG760A),
216193419Sweongyo	ZYD_ZD1211_DEV(SENAO, NUB8301),
217193419Sweongyo	ZYD_ZD1211_DEV(SITECOMEU, WL113),
218193419Sweongyo	ZYD_ZD1211_DEV(SWEEX, ZD1211),
219193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
220193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
221193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
222193419Sweongyo	ZYD_ZD1211_DEV(TWINMOS, G240),
223193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
224193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
225193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
226193419Sweongyo	ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
227193419Sweongyo	ZYD_ZD1211_DEV(ZCOM, ZD1211),
228193419Sweongyo	ZYD_ZD1211_DEV(ZYDAS, ZD1211),
229193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, AG225H),
230193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
231193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, G200V2),
232193419Sweongyo	/* ZYD_ZD1211B */
233223566Sgavin	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG_NF),
234193419Sweongyo	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
235193419Sweongyo	ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
236193419Sweongyo	ZYD_ZD1211B_DEV(ASUS, A9T_WIFI),
237193419Sweongyo	ZYD_ZD1211B_DEV(BELKIN, F5D7050_V4000),
238193419Sweongyo	ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
239193419Sweongyo	ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
240193419Sweongyo	ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
241193419Sweongyo	ZYD_ZD1211B_DEV(MELCO, KG54L),
242193419Sweongyo	ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
243193419Sweongyo	ZYD_ZD1211B_DEV(PLANEX2, GW_US54GXS),
244193419Sweongyo	ZYD_ZD1211B_DEV(SAGEM, XG76NA),
245193419Sweongyo	ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
246193419Sweongyo	ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
247193419Sweongyo	ZYD_ZD1211B_DEV(USR, USR5423),
248193419Sweongyo	ZYD_ZD1211B_DEV(VTECH, ZD1211B),
249193419Sweongyo	ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
250193419Sweongyo	ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
251193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, M202),
252193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, G202),
253193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, G220V2)
254184610Salfred};
255184610Salfred
256192984Sthompsastatic const struct usb_config zyd_config[ZYD_N_TRANSFER] = {
257188417Sthompsa	[ZYD_BULK_WR] = {
258184610Salfred		.type = UE_BULK,
259184610Salfred		.endpoint = UE_ADDR_ANY,
260184610Salfred		.direction = UE_DIR_OUT,
261190734Sthompsa		.bufsize = ZYD_MAX_TXBUFSZ,
262190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
263190734Sthompsa		.callback = zyd_bulk_write_callback,
264184610Salfred		.ep_index = 0,
265190734Sthompsa		.timeout = 10000,	/* 10 seconds */
266184610Salfred	},
267188417Sthompsa	[ZYD_BULK_RD] = {
268184610Salfred		.type = UE_BULK,
269184610Salfred		.endpoint = UE_ADDR_ANY,
270184610Salfred		.direction = UE_DIR_IN,
271190734Sthompsa		.bufsize = ZYX_MAX_RXBUFSZ,
272190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
273190734Sthompsa		.callback = zyd_bulk_read_callback,
274184610Salfred		.ep_index = 0,
275184610Salfred	},
276188417Sthompsa	[ZYD_INTR_WR] = {
277184610Salfred		.type = UE_BULK_INTR,
278184610Salfred		.endpoint = UE_ADDR_ANY,
279184610Salfred		.direction = UE_DIR_OUT,
280190734Sthompsa		.bufsize = sizeof(struct zyd_cmd),
281190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
282190734Sthompsa		.callback = zyd_intr_write_callback,
283190734Sthompsa		.timeout = 1000,	/* 1 second */
284184610Salfred		.ep_index = 1,
285184610Salfred	},
286188417Sthompsa	[ZYD_INTR_RD] = {
287188417Sthompsa		.type = UE_INTERRUPT,
288184610Salfred		.endpoint = UE_ADDR_ANY,
289184610Salfred		.direction = UE_DIR_IN,
290190734Sthompsa		.bufsize = sizeof(struct zyd_cmd),
291190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
292190734Sthompsa		.callback = zyd_intr_read_callback,
293184610Salfred	},
294184610Salfred};
295188417Sthompsa#define zyd_read16_m(sc, val, data)	do {				\
296188417Sthompsa	error = zyd_read16(sc, val, data);				\
297188417Sthompsa	if (error != 0)							\
298188417Sthompsa		goto fail;						\
299188417Sthompsa} while (0)
300188417Sthompsa#define zyd_write16_m(sc, val, data)	do {				\
301188417Sthompsa	error = zyd_write16(sc, val, data);				\
302188417Sthompsa	if (error != 0)							\
303188417Sthompsa		goto fail;						\
304188417Sthompsa} while (0)
305188417Sthompsa#define zyd_read32_m(sc, val, data)	do {				\
306188417Sthompsa	error = zyd_read32(sc, val, data);				\
307188417Sthompsa	if (error != 0)							\
308188417Sthompsa		goto fail;						\
309188417Sthompsa} while (0)
310188417Sthompsa#define zyd_write32_m(sc, val, data)	do {				\
311188417Sthompsa	error = zyd_write32(sc, val, data);				\
312188417Sthompsa	if (error != 0)							\
313188417Sthompsa		goto fail;						\
314188417Sthompsa} while (0)
315184610Salfred
316188417Sthompsastatic int
317188417Sthompsazyd_match(device_t dev)
318184610Salfred{
319192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
320184610Salfred
321192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
322188417Sthompsa		return (ENXIO);
323188419Sthompsa	if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX)
324188417Sthompsa		return (ENXIO);
325188417Sthompsa	if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX)
326188417Sthompsa		return (ENXIO);
327184610Salfred
328194228Sthompsa	return (usbd_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa));
329184610Salfred}
330184610Salfred
331188417Sthompsastatic int
332188417Sthompsazyd_attach(device_t dev)
333184610Salfred{
334192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
335188417Sthompsa	struct zyd_softc *sc = device_get_softc(dev);
336191746Sthompsa	struct ifnet *ifp;
337191746Sthompsa	struct ieee80211com *ic;
338191746Sthompsa	uint8_t iface_index, bands;
339188419Sthompsa	int error;
340184610Salfred
341188417Sthompsa	if (uaa->info.bcdDevice < 0x4330) {
342188417Sthompsa		device_printf(dev, "device version mismatch: 0x%X "
343188417Sthompsa		    "(only >= 43.30 supported)\n",
344188417Sthompsa		    uaa->info.bcdDevice);
345188417Sthompsa		return (EINVAL);
346184610Salfred	}
347184610Salfred
348194228Sthompsa	device_set_usb_desc(dev);
349188417Sthompsa	sc->sc_dev = dev;
350188417Sthompsa	sc->sc_udev = uaa->device;
351188417Sthompsa	sc->sc_macrev = USB_GET_DRIVER_INFO(uaa);
352188419Sthompsa
353188417Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
354188417Sthompsa	    MTX_NETWORK_LOCK, MTX_DEF);
355188417Sthompsa	STAILQ_INIT(&sc->sc_rqh);
356184610Salfred
357188417Sthompsa	iface_index = ZYD_IFACE_INDEX;
358194228Sthompsa	error = usbd_transfer_setup(uaa->device,
359188417Sthompsa	    &iface_index, sc->sc_xfer, zyd_config,
360188417Sthompsa	    ZYD_N_TRANSFER, sc, &sc->sc_mtx);
361188417Sthompsa	if (error) {
362188417Sthompsa		device_printf(dev, "could not allocate USB transfers, "
363194228Sthompsa		    "err=%s\n", usbd_errstr(error));
364188419Sthompsa		goto detach;
365184610Salfred	}
366184610Salfred
367188419Sthompsa	ZYD_LOCK(sc);
368188417Sthompsa	if ((error = zyd_get_macaddr(sc)) != 0) {
369188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
370191746Sthompsa		ZYD_UNLOCK(sc);
371191746Sthompsa		goto detach;
372188417Sthompsa	}
373188419Sthompsa	ZYD_UNLOCK(sc);
374188419Sthompsa
375188417Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
376188417Sthompsa	if (ifp == NULL) {
377188419Sthompsa		device_printf(sc->sc_dev, "can not if_alloc()\n");
378191746Sthompsa		goto detach;
379184610Salfred	}
380188417Sthompsa	ifp->if_softc = sc;
381188417Sthompsa	if_initname(ifp, "zyd", device_get_unit(sc->sc_dev));
382188417Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
383188417Sthompsa	ifp->if_init = zyd_init;
384188417Sthompsa	ifp->if_ioctl = zyd_ioctl;
385188417Sthompsa	ifp->if_start = zyd_start;
386207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
387188417Sthompsa	IFQ_SET_READY(&ifp->if_snd);
388184610Salfred
389188417Sthompsa	ic = ifp->if_l2com;
390188417Sthompsa	ic->ic_ifp = ifp;
391188417Sthompsa	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
392188417Sthompsa	ic->ic_opmode = IEEE80211_M_STA;
393184610Salfred
394188417Sthompsa	/* set device capabilities */
395188417Sthompsa	ic->ic_caps =
396188417Sthompsa		  IEEE80211_C_STA		/* station mode */
397188417Sthompsa		| IEEE80211_C_MONITOR		/* monitor mode */
398188417Sthompsa		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
399188417Sthompsa	        | IEEE80211_C_SHSLOT		/* short slot time supported */
400188417Sthompsa		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
401188417Sthompsa	        | IEEE80211_C_WPA		/* 802.11i */
402188417Sthompsa		;
403184610Salfred
404188417Sthompsa	bands = 0;
405188417Sthompsa	setbit(&bands, IEEE80211_MODE_11B);
406188417Sthompsa	setbit(&bands, IEEE80211_MODE_11G);
407188417Sthompsa	ieee80211_init_channels(ic, NULL, &bands);
408184610Salfred
409190526Ssam	ieee80211_ifattach(ic, sc->sc_bssid);
410188417Sthompsa	ic->ic_raw_xmit = zyd_raw_xmit;
411188417Sthompsa	ic->ic_scan_start = zyd_scan_start;
412188417Sthompsa	ic->ic_scan_end = zyd_scan_end;
413188417Sthompsa	ic->ic_set_channel = zyd_set_channel;
414184610Salfred
415188417Sthompsa	ic->ic_vap_create = zyd_vap_create;
416188417Sthompsa	ic->ic_vap_delete = zyd_vap_delete;
417188417Sthompsa	ic->ic_update_mcast = zyd_update_mcast;
418188601Sthompsa	ic->ic_update_promisc = zyd_update_mcast;
419184610Salfred
420192468Ssam	ieee80211_radiotap_attach(ic,
421192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
422192468Ssam		ZYD_TX_RADIOTAP_PRESENT,
423192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
424192468Ssam		ZYD_RX_RADIOTAP_PRESENT);
425184610Salfred
426188417Sthompsa	if (bootverbose)
427188417Sthompsa		ieee80211_announce(ic);
428184610Salfred
429191746Sthompsa	return (0);
430191746Sthompsa
431191746Sthompsadetach:
432191746Sthompsa	zyd_detach(dev);
433191746Sthompsa	return (ENXIO);			/* failure */
434184610Salfred}
435184610Salfred
436188417Sthompsastatic int
437188417Sthompsazyd_detach(device_t dev)
438184610Salfred{
439188417Sthompsa	struct zyd_softc *sc = device_get_softc(dev);
440188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
441188601Sthompsa	struct ieee80211com *ic;
442246614Shselasky	unsigned int x;
443184610Salfred
444246614Shselasky	/*
445246614Shselasky	 * Prevent further allocations from RX/TX data
446246614Shselasky	 * lists and ioctls:
447246614Shselasky	 */
448246614Shselasky	ZYD_LOCK(sc);
449246614Shselasky	sc->sc_flags |= ZYD_FLAG_DETACHED;
450246614Shselasky	STAILQ_INIT(&sc->tx_q);
451246614Shselasky	STAILQ_INIT(&sc->tx_free);
452246614Shselasky	ZYD_UNLOCK(sc);
453184610Salfred
454246614Shselasky	/* drain USB transfers */
455246614Shselasky	for (x = 0; x != ZYD_N_TRANSFER; x++)
456246614Shselasky		usbd_transfer_drain(sc->sc_xfer[x]);
457246614Shselasky
458188419Sthompsa	/* free TX list, if any */
459246614Shselasky	ZYD_LOCK(sc);
460188419Sthompsa	zyd_unsetup_tx_list(sc);
461246614Shselasky	ZYD_UNLOCK(sc);
462188419Sthompsa
463246614Shselasky	/* free USB transfers and some data buffers */
464246614Shselasky	usbd_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER);
465246614Shselasky
466188417Sthompsa	if (ifp) {
467188601Sthompsa		ic = ifp->if_l2com;
468188417Sthompsa		ieee80211_ifdetach(ic);
469188417Sthompsa		if_free(ifp);
470184610Salfred	}
471188417Sthompsa	mtx_destroy(&sc->sc_mtx);
472184610Salfred
473188417Sthompsa	return (0);
474184610Salfred}
475184610Salfred
476188417Sthompsastatic struct ieee80211vap *
477228621Sbschmidtzyd_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
478228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
479228621Sbschmidt    const uint8_t bssid[IEEE80211_ADDR_LEN],
480228621Sbschmidt    const uint8_t mac[IEEE80211_ADDR_LEN])
481184610Salfred{
482188417Sthompsa	struct zyd_vap *zvp;
483188417Sthompsa	struct ieee80211vap *vap;
484184610Salfred
485188417Sthompsa	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
486188417Sthompsa		return (NULL);
487188417Sthompsa	zvp = (struct zyd_vap *) malloc(sizeof(struct zyd_vap),
488188417Sthompsa	    M_80211_VAP, M_NOWAIT | M_ZERO);
489188417Sthompsa	if (zvp == NULL)
490188417Sthompsa		return (NULL);
491188417Sthompsa	vap = &zvp->vap;
492257743Shselasky
493188417Sthompsa	/* enable s/w bmiss handling for sta mode */
494257743Shselasky	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
495257743Shselasky	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac) != 0) {
496257743Shselasky		/* out of memory */
497257743Shselasky		free(zvp, M_80211_VAP);
498257743Shselasky		return (NULL);
499257743Shselasky	}
500184610Salfred
501188417Sthompsa	/* override state transition machine */
502188417Sthompsa	zvp->newstate = vap->iv_newstate;
503188417Sthompsa	vap->iv_newstate = zyd_newstate;
504184610Salfred
505206358Srpaulo	ieee80211_ratectl_init(vap);
506206358Srpaulo	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
507184610Salfred
508188417Sthompsa	/* complete setup */
509188417Sthompsa	ieee80211_vap_attach(vap, ieee80211_media_change,
510188417Sthompsa	    ieee80211_media_status);
511188417Sthompsa	ic->ic_opmode = opmode;
512188417Sthompsa	return (vap);
513184610Salfred}
514184610Salfred
515184610Salfredstatic void
516188417Sthompsazyd_vap_delete(struct ieee80211vap *vap)
517184610Salfred{
518188417Sthompsa	struct zyd_vap *zvp = ZYD_VAP(vap);
519184610Salfred
520206358Srpaulo	ieee80211_ratectl_deinit(vap);
521188417Sthompsa	ieee80211_vap_detach(vap);
522188417Sthompsa	free(zvp, M_80211_VAP);
523184610Salfred}
524184610Salfred
525184610Salfredstatic void
526188417Sthompsazyd_tx_free(struct zyd_tx_data *data, int txerr)
527184610Salfred{
528188417Sthompsa	struct zyd_softc *sc = data->sc;
529184610Salfred
530188417Sthompsa	if (data->m != NULL) {
531188417Sthompsa		if (data->m->m_flags & M_TXCB)
532188417Sthompsa			ieee80211_process_callback(data->ni, data->m,
533188417Sthompsa			    txerr ? ETIMEDOUT : 0);
534188417Sthompsa		m_freem(data->m);
535188417Sthompsa		data->m = NULL;
536188417Sthompsa
537188417Sthompsa		ieee80211_free_node(data->ni);
538188417Sthompsa		data->ni = NULL;
539184610Salfred	}
540188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
541188417Sthompsa	sc->tx_nfree++;
542184610Salfred}
543184610Salfred
544188419Sthompsastatic void
545188419Sthompsazyd_setup_tx_list(struct zyd_softc *sc)
546184610Salfred{
547188417Sthompsa	struct zyd_tx_data *data;
548188417Sthompsa	int i;
549184610Salfred
550188417Sthompsa	sc->tx_nfree = 0;
551188417Sthompsa	STAILQ_INIT(&sc->tx_q);
552188417Sthompsa	STAILQ_INIT(&sc->tx_free);
553184610Salfred
554188417Sthompsa	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
555188417Sthompsa		data = &sc->tx_data[i];
556184610Salfred
557188417Sthompsa		data->sc = sc;
558188417Sthompsa		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
559188417Sthompsa		sc->tx_nfree++;
560188417Sthompsa	}
561184610Salfred}
562184610Salfred
563184610Salfredstatic void
564188419Sthompsazyd_unsetup_tx_list(struct zyd_softc *sc)
565184610Salfred{
566188417Sthompsa	struct zyd_tx_data *data;
567188417Sthompsa	int i;
568184610Salfred
569188419Sthompsa	/* make sure any subsequent use of the queues will fail */
570188419Sthompsa	sc->tx_nfree = 0;
571188419Sthompsa	STAILQ_INIT(&sc->tx_q);
572188419Sthompsa	STAILQ_INIT(&sc->tx_free);
573184610Salfred
574188419Sthompsa	/* free up all node references and mbufs */
575188417Sthompsa	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
576188417Sthompsa		data = &sc->tx_data[i];
577184610Salfred
578188417Sthompsa		if (data->m != NULL) {
579188417Sthompsa			m_freem(data->m);
580188417Sthompsa			data->m = NULL;
581188417Sthompsa		}
582188417Sthompsa		if (data->ni != NULL) {
583188417Sthompsa			ieee80211_free_node(data->ni);
584188417Sthompsa			data->ni = NULL;
585188417Sthompsa		}
586188417Sthompsa	}
587184610Salfred}
588184610Salfred
589191746Sthompsastatic int
590191746Sthompsazyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
591186730Salfred{
592188417Sthompsa	struct zyd_vap *zvp = ZYD_VAP(vap);
593191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
594191746Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
595188417Sthompsa	int error;
596186730Salfred
597191746Sthompsa	DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__,
598191746Sthompsa	    ieee80211_state_name[vap->iv_state],
599191746Sthompsa	    ieee80211_state_name[nstate]);
600191746Sthompsa
601191746Sthompsa	IEEE80211_UNLOCK(ic);
602191746Sthompsa	ZYD_LOCK(sc);
603191746Sthompsa	switch (nstate) {
604188417Sthompsa	case IEEE80211_S_AUTH:
605188417Sthompsa		zyd_set_chan(sc, ic->ic_curchan);
606188417Sthompsa		break;
607188417Sthompsa	case IEEE80211_S_RUN:
608188417Sthompsa		if (vap->iv_opmode == IEEE80211_M_MONITOR)
609188417Sthompsa			break;
610184610Salfred
611188417Sthompsa		/* turn link LED on */
612188417Sthompsa		error = zyd_set_led(sc, ZYD_LED1, 1);
613188417Sthompsa		if (error != 0)
614191746Sthompsa			break;
615191746Sthompsa
616188417Sthompsa		/* make data LED blink upon Tx */
617188417Sthompsa		zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1);
618191746Sthompsa
619212127Sthompsa		IEEE80211_ADDR_COPY(sc->sc_bssid, vap->iv_bss->ni_bssid);
620188417Sthompsa		zyd_set_bssid(sc, sc->sc_bssid);
621188417Sthompsa		break;
622188417Sthompsa	default:
623188417Sthompsa		break;
624184610Salfred	}
625188417Sthompsafail:
626188417Sthompsa	ZYD_UNLOCK(sc);
627188417Sthompsa	IEEE80211_LOCK(ic);
628191746Sthompsa	return (zvp->newstate(vap, nstate, arg));
629184610Salfred}
630184610Salfred
631188417Sthompsa/*
632188417Sthompsa * Callback handler for interrupt transfer
633188417Sthompsa */
634184610Salfredstatic void
635194677Sthompsazyd_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
636184610Salfred{
637194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
638184610Salfred	struct ifnet *ifp = sc->sc_ifp;
639184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
640188417Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
641184610Salfred	struct ieee80211_node *ni;
642188417Sthompsa	struct zyd_cmd *cmd = &sc->sc_ibuf;
643194677Sthompsa	struct usb_page_cache *pc;
644188417Sthompsa	int datalen;
645194677Sthompsa	int actlen;
646184610Salfred
647194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
648194677Sthompsa
649184610Salfred	switch (USB_GET_STATE(xfer)) {
650184610Salfred	case USB_ST_TRANSFERRED:
651194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
652194677Sthompsa		usbd_copy_out(pc, 0, cmd, sizeof(*cmd));
653184610Salfred
654188417Sthompsa		switch (le16toh(cmd->code)) {
655188417Sthompsa		case ZYD_NOTIF_RETRYSTATUS:
656188417Sthompsa		{
657188417Sthompsa			struct zyd_notif_retry *retry =
658188417Sthompsa			    (struct zyd_notif_retry *)cmd->data;
659184610Salfred
660188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_TX_PROC,
661188417Sthompsa			    "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
662188417Sthompsa			    le16toh(retry->rate), ether_sprintf(retry->macaddr),
663188417Sthompsa			    le16toh(retry->count)&0xff, le16toh(retry->count));
664184610Salfred
665188417Sthompsa			/*
666188417Sthompsa			 * Find the node to which the packet was sent and
667188417Sthompsa			 * update its retry statistics.  In BSS mode, this node
668188417Sthompsa			 * is the AP we're associated to so no lookup is
669188417Sthompsa			 * actually needed.
670188417Sthompsa			 */
671188417Sthompsa			ni = ieee80211_find_txnode(vap, retry->macaddr);
672188417Sthompsa			if (ni != NULL) {
673206358Srpaulo				int retrycnt =
674206358Srpaulo				    (int)(le16toh(retry->count) & 0xff);
675206358Srpaulo
676206358Srpaulo				ieee80211_ratectl_tx_complete(vap, ni,
677206358Srpaulo				    IEEE80211_RATECTL_TX_FAILURE,
678206358Srpaulo				    &retrycnt, NULL);
679188417Sthompsa				ieee80211_free_node(ni);
680188417Sthompsa			}
681188417Sthompsa			if (le16toh(retry->count) & 0x100)
682188417Sthompsa				ifp->if_oerrors++;	/* too many retries */
683188417Sthompsa			break;
684188417Sthompsa		}
685188417Sthompsa		case ZYD_NOTIF_IORD:
686188417Sthompsa		{
687188417Sthompsa			struct zyd_rq *rqp;
688184610Salfred
689188417Sthompsa			if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
690188417Sthompsa				break;	/* HMAC interrupt */
691184610Salfred
692194677Sthompsa			datalen = actlen - sizeof(cmd->code);
693188417Sthompsa			datalen -= 2;	/* XXX: padding? */
694184610Salfred
695188417Sthompsa			STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
696233774Shselasky				int i;
697233774Shselasky				int count;
698184610Salfred
699188417Sthompsa				if (rqp->olen != datalen)
700188417Sthompsa					continue;
701233774Shselasky				count = rqp->olen / sizeof(struct zyd_pair);
702233774Shselasky				for (i = 0; i < count; i++) {
703188417Sthompsa					if (*(((const uint16_t *)rqp->idata) + i) !=
704188417Sthompsa					    (((struct zyd_pair *)cmd->data) + i)->reg)
705188417Sthompsa						break;
706184610Salfred				}
707233774Shselasky				if (i != count)
708188417Sthompsa					continue;
709188417Sthompsa				/* copy answer into caller-supplied buffer */
710227461Shselasky				memcpy(rqp->odata, cmd->data, rqp->olen);
711188417Sthompsa				DPRINTF(sc, ZYD_DEBUG_CMD,
712188417Sthompsa				    "command %p complete, data = %*D \n",
713228303Shselasky				    rqp, rqp->olen, (char *)rqp->odata, ":");
714188417Sthompsa				wakeup(rqp);	/* wakeup caller */
715188417Sthompsa				break;
716184610Salfred			}
717188417Sthompsa			if (rqp == NULL) {
718188417Sthompsa				device_printf(sc->sc_dev,
719188417Sthompsa				    "unexpected IORD notification %*D\n",
720188417Sthompsa				    datalen, cmd->data, ":");
721188417Sthompsa			}
722188417Sthompsa			break;
723184610Salfred		}
724188417Sthompsa		default:
725188417Sthompsa			device_printf(sc->sc_dev, "unknown notification %x\n",
726188417Sthompsa			    le16toh(cmd->code));
727188417Sthompsa		}
728184610Salfred
729188417Sthompsa		/* FALLTHROUGH */
730184610Salfred	case USB_ST_SETUP:
731184610Salfredtr_setup:
732194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
733194228Sthompsa		usbd_transfer_submit(xfer);
734188417Sthompsa		break;
735184610Salfred
736188417Sthompsa	default:			/* Error */
737188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
738194677Sthompsa		    usbd_errstr(error));
739188417Sthompsa
740194677Sthompsa		if (error != USB_ERR_CANCELLED) {
741188417Sthompsa			/* try to clear stall first */
742194677Sthompsa			usbd_xfer_set_stall(xfer);
743188417Sthompsa			goto tr_setup;
744184610Salfred		}
745188417Sthompsa		break;
746188417Sthompsa	}
747188417Sthompsa}
748184610Salfred
749188417Sthompsastatic void
750194677Sthompsazyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
751188417Sthompsa{
752194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
753194677Sthompsa	struct zyd_rq *rqp, *cmd;
754194677Sthompsa	struct usb_page_cache *pc;
755184610Salfred
756188417Sthompsa	switch (USB_GET_STATE(xfer)) {
757188417Sthompsa	case USB_ST_TRANSFERRED:
758194677Sthompsa		cmd = usbd_xfer_get_priv(xfer);
759194677Sthompsa		DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd);
760189452Sthompsa		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
761189452Sthompsa			/* Ensure the cached rq pointer is still valid */
762194677Sthompsa			if (rqp == cmd &&
763189452Sthompsa			    (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
764189452Sthompsa				wakeup(rqp);	/* wakeup caller */
765189452Sthompsa		}
766184610Salfred
767188417Sthompsa		/* FALLTHROUGH */
768188417Sthompsa	case USB_ST_SETUP:
769188417Sthompsatr_setup:
770188417Sthompsa		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
771188417Sthompsa			if (rqp->flags & ZYD_CMD_FLAG_SENT)
772188417Sthompsa				continue;
773184610Salfred
774194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
775194677Sthompsa			usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen);
776184610Salfred
777194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, rqp->ilen);
778194677Sthompsa			usbd_xfer_set_priv(xfer, rqp);
779188417Sthompsa			rqp->flags |= ZYD_CMD_FLAG_SENT;
780194228Sthompsa			usbd_transfer_submit(xfer);
781188417Sthompsa			break;
782184610Salfred		}
783184610Salfred		break;
784184610Salfred
785184610Salfred	default:			/* Error */
786188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
787194677Sthompsa		    usbd_errstr(error));
788184610Salfred
789194677Sthompsa		if (error != USB_ERR_CANCELLED) {
790184610Salfred			/* try to clear stall first */
791194677Sthompsa			usbd_xfer_set_stall(xfer);
792188417Sthompsa			goto tr_setup;
793184610Salfred		}
794184610Salfred		break;
795184610Salfred	}
796184610Salfred}
797184610Salfred
798188417Sthompsastatic int
799188417Sthompsazyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
800188601Sthompsa    void *odata, int olen, int flags)
801184610Salfred{
802188417Sthompsa	struct zyd_cmd cmd;
803188417Sthompsa	struct zyd_rq rq;
804188417Sthompsa	int error;
805184610Salfred
806233774Shselasky	if (ilen > (int)sizeof(cmd.data))
807188417Sthompsa		return (EINVAL);
808188419Sthompsa
809188417Sthompsa	cmd.code = htole16(code);
810227461Shselasky	memcpy(cmd.data, idata, ilen);
811188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
812188417Sthompsa	    &rq, ilen, idata, ":");
813184610Salfred
814188417Sthompsa	rq.cmd = &cmd;
815188417Sthompsa	rq.idata = idata;
816188417Sthompsa	rq.odata = odata;
817188417Sthompsa	rq.ilen = sizeof(uint16_t) + ilen;
818188417Sthompsa	rq.olen = olen;
819188417Sthompsa	rq.flags = flags;
820188417Sthompsa	STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
821194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
822194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
823184610Salfred
824188417Sthompsa	/* wait at most one second for command reply */
825188417Sthompsa	error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
826188417Sthompsa	if (error)
827188417Sthompsa		device_printf(sc->sc_dev, "command timeout\n");
828188417Sthompsa	STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
829188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
830188417Sthompsa	    &rq, error);
831184610Salfred
832188417Sthompsa	return (error);
833184610Salfred}
834184610Salfred
835184610Salfredstatic int
836188417Sthompsazyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
837184610Salfred{
838188417Sthompsa	struct zyd_pair tmp;
839188417Sthompsa	int error;
840184610Salfred
841188417Sthompsa	reg = htole16(reg);
842188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
843188417Sthompsa	    ZYD_CMD_FLAG_READ);
844188417Sthompsa	if (error == 0)
845188417Sthompsa		*val = le16toh(tmp.val);
846188417Sthompsa	return (error);
847184610Salfred}
848184610Salfred
849184610Salfredstatic int
850188417Sthompsazyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
851184610Salfred{
852188417Sthompsa	struct zyd_pair tmp[2];
853188417Sthompsa	uint16_t regs[2];
854184610Salfred	int error;
855184610Salfred
856188417Sthompsa	regs[0] = htole16(ZYD_REG32_HI(reg));
857188417Sthompsa	regs[1] = htole16(ZYD_REG32_LO(reg));
858188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
859188417Sthompsa	    ZYD_CMD_FLAG_READ);
860188417Sthompsa	if (error == 0)
861188417Sthompsa		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
862188417Sthompsa	return (error);
863188417Sthompsa}
864184610Salfred
865188417Sthompsastatic int
866188417Sthompsazyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
867188417Sthompsa{
868188417Sthompsa	struct zyd_pair pair;
869184610Salfred
870188417Sthompsa	pair.reg = htole16(reg);
871188417Sthompsa	pair.val = htole16(val);
872184610Salfred
873188417Sthompsa	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
874188417Sthompsa}
875184610Salfred
876188417Sthompsastatic int
877188417Sthompsazyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
878188417Sthompsa{
879188417Sthompsa	struct zyd_pair pair[2];
880184610Salfred
881188417Sthompsa	pair[0].reg = htole16(ZYD_REG32_HI(reg));
882188417Sthompsa	pair[0].val = htole16(val >> 16);
883188417Sthompsa	pair[1].reg = htole16(ZYD_REG32_LO(reg));
884188417Sthompsa	pair[1].val = htole16(val & 0xffff);
885184610Salfred
886188417Sthompsa	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
887184610Salfred}
888184610Salfred
889188417Sthompsastatic int
890188417Sthompsazyd_rfwrite(struct zyd_softc *sc, uint32_t val)
891184610Salfred{
892188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
893188417Sthompsa	struct zyd_rfwrite_cmd req;
894188417Sthompsa	uint16_t cr203;
895188417Sthompsa	int error, i;
896184610Salfred
897188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &cr203);
898188417Sthompsa	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
899188417Sthompsa
900188417Sthompsa	req.code  = htole16(2);
901188417Sthompsa	req.width = htole16(rf->width);
902188417Sthompsa	for (i = 0; i < rf->width; i++) {
903188417Sthompsa		req.bit[i] = htole16(cr203);
904188417Sthompsa		if (val & (1 << (rf->width - 1 - i)))
905188417Sthompsa			req.bit[i] |= htole16(ZYD_RF_DATA);
906188417Sthompsa	}
907188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
908188417Sthompsafail:
909188417Sthompsa	return (error);
910184610Salfred}
911184610Salfred
912188417Sthompsastatic int
913188417Sthompsazyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
914184610Salfred{
915188417Sthompsa	int error;
916184610Salfred
917188417Sthompsa	zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
918188417Sthompsa	zyd_write16_m(sc, ZYD_CR243, (val >>  8) & 0xff);
919188417Sthompsa	zyd_write16_m(sc, ZYD_CR242, (val >>  0) & 0xff);
920188417Sthompsafail:
921188417Sthompsa	return (error);
922184610Salfred}
923184610Salfred
924188417Sthompsastatic int
925188417Sthompsazyd_lock_phy(struct zyd_softc *sc)
926184610Salfred{
927188417Sthompsa	int error;
928188417Sthompsa	uint32_t tmp;
929186730Salfred
930188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
931188417Sthompsa	tmp &= ~ZYD_UNLOCK_PHY_REGS;
932188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
933188417Sthompsafail:
934188417Sthompsa	return (error);
935184610Salfred}
936184610Salfred
937188417Sthompsastatic int
938188417Sthompsazyd_unlock_phy(struct zyd_softc *sc)
939184610Salfred{
940188417Sthompsa	int error;
941188417Sthompsa	uint32_t tmp;
942184610Salfred
943188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
944188417Sthompsa	tmp |= ZYD_UNLOCK_PHY_REGS;
945188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
946188417Sthompsafail:
947188417Sthompsa	return (error);
948184610Salfred}
949184610Salfred
950184610Salfred/*
951188417Sthompsa * RFMD RF methods.
952184610Salfred */
953188417Sthompsastatic int
954188417Sthompsazyd_rfmd_init(struct zyd_rf *rf)
955184610Salfred{
956233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
957188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
958184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
959184610Salfred	static const uint32_t rfini[] = ZYD_RFMD_RF;
960188417Sthompsa	int i, error;
961184610Salfred
962184610Salfred	/* init RF-dependent PHY registers */
963188417Sthompsa	for (i = 0; i < N(phyini); i++) {
964188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
965184610Salfred	}
966184610Salfred
967184610Salfred	/* init RFMD radio */
968188417Sthompsa	for (i = 0; i < N(rfini); i++) {
969188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
970188417Sthompsa			return (error);
971184610Salfred	}
972188417Sthompsafail:
973188417Sthompsa	return (error);
974188417Sthompsa#undef N
975184610Salfred}
976184610Salfred
977188417Sthompsastatic int
978188417Sthompsazyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
979184610Salfred{
980188417Sthompsa	int error;
981188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
982188417Sthompsa
983188417Sthompsa	zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
984188417Sthompsa	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
985188417Sthompsafail:
986188417Sthompsa	return (error);
987184610Salfred}
988184610Salfred
989188417Sthompsastatic int
990188417Sthompsazyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
991184610Salfred{
992188417Sthompsa	int error;
993188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
994184610Salfred	static const struct {
995188417Sthompsa		uint32_t	r1, r2;
996188417Sthompsa	} rfprog[] = ZYD_RFMD_CHANTABLE;
997184610Salfred
998188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
999188417Sthompsa	if (error != 0)
1000188417Sthompsa		goto fail;
1001188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1002188417Sthompsa	if (error != 0)
1003188417Sthompsa		goto fail;
1004184610Salfred
1005188417Sthompsafail:
1006188417Sthompsa	return (error);
1007184610Salfred}
1008184610Salfred
1009184610Salfred/*
1010188417Sthompsa * AL2230 RF methods.
1011184610Salfred */
1012188417Sthompsastatic int
1013188417Sthompsazyd_al2230_init(struct zyd_rf *rf)
1014184610Salfred{
1015233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1016188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1017184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
1018186730Salfred	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1019186730Salfred	static const struct zyd_phy_pair phypll[] = {
1020188417Sthompsa		{ ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
1021188417Sthompsa		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
1022186730Salfred	};
1023186730Salfred	static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
1024186730Salfred	static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
1025186730Salfred	static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
1026188417Sthompsa	int i, error;
1027184610Salfred
1028184610Salfred	/* init RF-dependent PHY registers */
1029188417Sthompsa	for (i = 0; i < N(phyini); i++)
1030188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1031186730Salfred
1032188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1033188417Sthompsa		for (i = 0; i < N(phy2230s); i++)
1034188417Sthompsa			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1035184610Salfred	}
1036188417Sthompsa
1037186730Salfred	/* init AL2230 radio */
1038188417Sthompsa	for (i = 0; i < N(rfini1); i++) {
1039188417Sthompsa		error = zyd_rfwrite(sc, rfini1[i]);
1040188417Sthompsa		if (error != 0)
1041188417Sthompsa			goto fail;
1042188417Sthompsa	}
1043184610Salfred
1044188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1045188417Sthompsa		error = zyd_rfwrite(sc, 0x000824);
1046186730Salfred	else
1047188417Sthompsa		error = zyd_rfwrite(sc, 0x0005a4);
1048188417Sthompsa	if (error != 0)
1049188417Sthompsa		goto fail;
1050186730Salfred
1051188417Sthompsa	for (i = 0; i < N(rfini2); i++) {
1052188417Sthompsa		error = zyd_rfwrite(sc, rfini2[i]);
1053188417Sthompsa		if (error != 0)
1054188417Sthompsa			goto fail;
1055188417Sthompsa	}
1056186730Salfred
1057188417Sthompsa	for (i = 0; i < N(phypll); i++)
1058188417Sthompsa		zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
1059186730Salfred
1060188417Sthompsa	for (i = 0; i < N(rfini3); i++) {
1061188417Sthompsa		error = zyd_rfwrite(sc, rfini3[i]);
1062188417Sthompsa		if (error != 0)
1063188417Sthompsa			goto fail;
1064188417Sthompsa	}
1065188417Sthompsafail:
1066188417Sthompsa	return (error);
1067188417Sthompsa#undef N
1068184610Salfred}
1069184610Salfred
1070188417Sthompsastatic int
1071188417Sthompsazyd_al2230_fini(struct zyd_rf *rf)
1072186730Salfred{
1073233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1074188417Sthompsa	int error, i;
1075188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1076186730Salfred	static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
1077186730Salfred
1078188417Sthompsa	for (i = 0; i < N(phy); i++)
1079188417Sthompsa		zyd_write16_m(sc, phy[i].reg, phy[i].val);
1080186730Salfred
1081186730Salfred	if (sc->sc_newphy != 0)
1082188417Sthompsa		zyd_write16_m(sc, ZYD_CR9, 0xe1);
1083188417Sthompsa
1084188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, 0x6);
1085188417Sthompsafail:
1086188417Sthompsa	return (error);
1087188417Sthompsa#undef N
1088186730Salfred}
1089186730Salfred
1090188417Sthompsastatic int
1091188417Sthompsazyd_al2230_init_b(struct zyd_rf *rf)
1092184610Salfred{
1093233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1094188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1095186730Salfred	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1096186730Salfred	static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
1097186730Salfred	static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
1098186730Salfred	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1099188417Sthompsa	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1100186730Salfred	static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
1101186730Salfred	static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
1102186730Salfred	static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
1103186730Salfred	static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
1104188417Sthompsa	int i, error;
1105184610Salfred
1106188417Sthompsa	for (i = 0; i < N(phy1); i++)
1107188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1108186730Salfred
1109184610Salfred	/* init RF-dependent PHY registers */
1110188417Sthompsa	for (i = 0; i < N(phyini); i++)
1111188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1112184610Salfred
1113188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1114188417Sthompsa		for (i = 0; i < N(phy2230s); i++)
1115188417Sthompsa			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1116188417Sthompsa	}
1117186730Salfred
1118188417Sthompsa	for (i = 0; i < 3; i++) {
1119188417Sthompsa		error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
1120188417Sthompsa		if (error != 0)
1121188417Sthompsa			return (error);
1122188417Sthompsa	}
1123186730Salfred
1124188417Sthompsa	for (i = 0; i < N(rfini_part1); i++) {
1125188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part1[i]);
1126188417Sthompsa		if (error != 0)
1127188417Sthompsa			return (error);
1128188417Sthompsa	}
1129186730Salfred
1130188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1131188417Sthompsa		error = zyd_rfwrite(sc, 0x241000);
1132186730Salfred	else
1133188417Sthompsa		error = zyd_rfwrite(sc, 0x25a000);
1134188417Sthompsa	if (error != 0)
1135188417Sthompsa		goto fail;
1136186730Salfred
1137188417Sthompsa	for (i = 0; i < N(rfini_part2); i++) {
1138188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part2[i]);
1139188417Sthompsa		if (error != 0)
1140188417Sthompsa			return (error);
1141188417Sthompsa	}
1142186730Salfred
1143188417Sthompsa	for (i = 0; i < N(phy2); i++)
1144188417Sthompsa		zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
1145186730Salfred
1146188417Sthompsa	for (i = 0; i < N(rfini_part3); i++) {
1147188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part3[i]);
1148188417Sthompsa		if (error != 0)
1149188417Sthompsa			return (error);
1150188417Sthompsa	}
1151186730Salfred
1152188417Sthompsa	for (i = 0; i < N(phy3); i++)
1153188417Sthompsa		zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
1154186730Salfred
1155188417Sthompsa	error = zyd_al2230_fini(rf);
1156188417Sthompsafail:
1157188417Sthompsa	return (error);
1158188417Sthompsa#undef N
1159184610Salfred}
1160184610Salfred
1161188417Sthompsastatic int
1162188417Sthompsazyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1163184610Salfred{
1164188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1165188417Sthompsa	int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
1166188417Sthompsa
1167188417Sthompsa	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1168188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
1169188417Sthompsafail:
1170188417Sthompsa	return (error);
1171188417Sthompsa}
1172188417Sthompsa
1173188417Sthompsastatic int
1174188417Sthompsazyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1175188417Sthompsa{
1176233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1177188417Sthompsa	int error, i;
1178188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1179186730Salfred	static const struct zyd_phy_pair phy1[] = {
1180188417Sthompsa		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
1181186730Salfred	};
1182184610Salfred	static const struct {
1183188417Sthompsa		uint32_t	r1, r2, r3;
1184188417Sthompsa	} rfprog[] = ZYD_AL2230_CHANTABLE;
1185184610Salfred
1186188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1187188417Sthompsa	if (error != 0)
1188188417Sthompsa		goto fail;
1189188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1190188417Sthompsa	if (error != 0)
1191188417Sthompsa		goto fail;
1192188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
1193188417Sthompsa	if (error != 0)
1194188417Sthompsa		goto fail;
1195184610Salfred
1196188417Sthompsa	for (i = 0; i < N(phy1); i++)
1197188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1198188417Sthompsafail:
1199188417Sthompsa	return (error);
1200188417Sthompsa#undef N
1201184610Salfred}
1202184610Salfred
1203188417Sthompsastatic int
1204188417Sthompsazyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
1205186730Salfred{
1206233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1207188417Sthompsa	int error, i;
1208188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1209186730Salfred	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1210186730Salfred	static const struct {
1211188417Sthompsa		uint32_t	r1, r2, r3;
1212188417Sthompsa	} rfprog[] = ZYD_AL2230_CHANTABLE_B;
1213186730Salfred
1214188417Sthompsa	for (i = 0; i < N(phy1); i++)
1215188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1216186730Salfred
1217188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
1218188417Sthompsa	if (error != 0)
1219188417Sthompsa		goto fail;
1220188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
1221188417Sthompsa	if (error != 0)
1222188417Sthompsa		goto fail;
1223188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
1224188417Sthompsa	if (error != 0)
1225188417Sthompsa		goto fail;
1226188417Sthompsa	error = zyd_al2230_fini(rf);
1227188417Sthompsafail:
1228188417Sthompsa	return (error);
1229188417Sthompsa#undef N
1230186730Salfred}
1231186730Salfred
1232186730Salfred#define	ZYD_AL2230_PHY_BANDEDGE6					\
1233186730Salfred{									\
1234188417Sthompsa	{ ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 },	\
1235186730Salfred	{ ZYD_CR47,  0x1e }						\
1236186730Salfred}
1237186730Salfred
1238188417Sthompsastatic int
1239188417Sthompsazyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
1240186730Salfred{
1241233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1242188417Sthompsa	int error = 0, i;
1243188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1244188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1245188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1246186730Salfred	struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
1247188601Sthompsa	int chan = ieee80211_chan2ieee(ic, c);
1248186730Salfred
1249188417Sthompsa	if (chan == 1 || chan == 11)
1250186730Salfred		r[0].val = 0x12;
1251188417Sthompsa
1252188417Sthompsa	for (i = 0; i < N(r); i++)
1253188417Sthompsa		zyd_write16_m(sc, r[i].reg, r[i].val);
1254188417Sthompsafail:
1255188417Sthompsa	return (error);
1256188417Sthompsa#undef N
1257186730Salfred}
1258186730Salfred
1259184610Salfred/*
1260184610Salfred * AL7230B RF methods.
1261184610Salfred */
1262188417Sthompsastatic int
1263188417Sthompsazyd_al7230B_init(struct zyd_rf *rf)
1264184610Salfred{
1265233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1266188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1267184610Salfred	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1268184610Salfred	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1269184610Salfred	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1270184610Salfred	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1271184610Salfred	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1272188417Sthompsa	int i, error;
1273184610Salfred
1274184610Salfred	/* for AL7230B, PHY and RF need to be initialized in "phases" */
1275184610Salfred
1276184610Salfred	/* init RF-dependent PHY registers, part one */
1277188417Sthompsa	for (i = 0; i < N(phyini_1); i++)
1278188417Sthompsa		zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
1279188417Sthompsa
1280184610Salfred	/* init AL7230B radio, part one */
1281188417Sthompsa	for (i = 0; i < N(rfini_1); i++) {
1282188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1283188417Sthompsa			return (error);
1284184610Salfred	}
1285184610Salfred	/* init RF-dependent PHY registers, part two */
1286188417Sthompsa	for (i = 0; i < N(phyini_2); i++)
1287188417Sthompsa		zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
1288188417Sthompsa
1289184610Salfred	/* init AL7230B radio, part two */
1290188417Sthompsa	for (i = 0; i < N(rfini_2); i++) {
1291188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1292188417Sthompsa			return (error);
1293184610Salfred	}
1294184610Salfred	/* init RF-dependent PHY registers, part three */
1295188417Sthompsa	for (i = 0; i < N(phyini_3); i++)
1296188417Sthompsa		zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
1297188417Sthompsafail:
1298188417Sthompsa	return (error);
1299188417Sthompsa#undef N
1300184610Salfred}
1301184610Salfred
1302188417Sthompsastatic int
1303188417Sthompsazyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1304184610Salfred{
1305188417Sthompsa	int error;
1306188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1307188417Sthompsa
1308188417Sthompsa	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1309188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1310188417Sthompsafail:
1311188417Sthompsa	return (error);
1312188417Sthompsa}
1313188417Sthompsa
1314188417Sthompsastatic int
1315188417Sthompsazyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1316188417Sthompsa{
1317233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1318188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1319184610Salfred	static const struct {
1320188417Sthompsa		uint32_t	r1, r2;
1321188417Sthompsa	} rfprog[] = ZYD_AL7230B_CHANTABLE;
1322184610Salfred	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1323188417Sthompsa	int i, error;
1324184610Salfred
1325188417Sthompsa	zyd_write16_m(sc, ZYD_CR240, 0x57);
1326188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, 0x2f);
1327184610Salfred
1328188417Sthompsa	for (i = 0; i < N(rfsc); i++) {
1329188417Sthompsa		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1330188417Sthompsa			return (error);
1331184610Salfred	}
1332184610Salfred
1333188417Sthompsa	zyd_write16_m(sc, ZYD_CR128, 0x14);
1334188417Sthompsa	zyd_write16_m(sc, ZYD_CR129, 0x12);
1335188417Sthompsa	zyd_write16_m(sc, ZYD_CR130, 0x10);
1336188417Sthompsa	zyd_write16_m(sc, ZYD_CR38,  0x38);
1337188417Sthompsa	zyd_write16_m(sc, ZYD_CR136, 0xdf);
1338184610Salfred
1339188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1340188417Sthompsa	if (error != 0)
1341188417Sthompsa		goto fail;
1342188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1343188417Sthompsa	if (error != 0)
1344188417Sthompsa		goto fail;
1345188417Sthompsa	error = zyd_rfwrite(sc, 0x3c9000);
1346188417Sthompsa	if (error != 0)
1347188417Sthompsa		goto fail;
1348184610Salfred
1349188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, 0x3f);
1350188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, 0x06);
1351188417Sthompsa	zyd_write16_m(sc, ZYD_CR240, 0x08);
1352188417Sthompsafail:
1353188417Sthompsa	return (error);
1354188417Sthompsa#undef N
1355184610Salfred}
1356184610Salfred
1357184610Salfred/*
1358184610Salfred * AL2210 RF methods.
1359184610Salfred */
1360188417Sthompsastatic int
1361188417Sthompsazyd_al2210_init(struct zyd_rf *rf)
1362184610Salfred{
1363233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1364188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1365184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1366184610Salfred	static const uint32_t rfini[] = ZYD_AL2210_RF;
1367184610Salfred	uint32_t tmp;
1368188417Sthompsa	int i, error;
1369184610Salfred
1370188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 2);
1371184610Salfred
1372184610Salfred	/* init RF-dependent PHY registers */
1373188417Sthompsa	for (i = 0; i < N(phyini); i++)
1374188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1375188417Sthompsa
1376184610Salfred	/* init AL2210 radio */
1377188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1378188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1379188417Sthompsa			return (error);
1380184610Salfred	}
1381188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1382188417Sthompsa	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1383188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1384188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1385188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1386188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1387188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1388188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 3);
1389188417Sthompsafail:
1390188417Sthompsa	return (error);
1391188417Sthompsa#undef N
1392184610Salfred}
1393184610Salfred
1394188417Sthompsastatic int
1395188417Sthompsazyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1396184610Salfred{
1397188417Sthompsa	/* vendor driver does nothing for this RF chip */
1398188417Sthompsa
1399188417Sthompsa	return (0);
1400188417Sthompsa}
1401188417Sthompsa
1402188417Sthompsastatic int
1403188417Sthompsazyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1404188417Sthompsa{
1405188417Sthompsa	int error;
1406188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1407184610Salfred	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1408184610Salfred	uint32_t tmp;
1409184610Salfred
1410188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 2);
1411188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1412188417Sthompsa	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1413188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1414188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1415188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1416188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1417188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1418184610Salfred
1419184610Salfred	/* actually set the channel */
1420188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1]);
1421188417Sthompsa	if (error != 0)
1422188417Sthompsa		goto fail;
1423184610Salfred
1424188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 3);
1425188417Sthompsafail:
1426188417Sthompsa	return (error);
1427184610Salfred}
1428184610Salfred
1429184610Salfred/*
1430184610Salfred * GCT RF methods.
1431184610Salfred */
1432188417Sthompsastatic int
1433188417Sthompsazyd_gct_init(struct zyd_rf *rf)
1434184610Salfred{
1435193420Sweongyo#define	ZYD_GCT_INTR_REG	0x85c1
1436233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1437188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1438184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1439184610Salfred	static const uint32_t rfini[] = ZYD_GCT_RF;
1440193420Sweongyo	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1441193420Sweongyo	int i, idx = -1, error;
1442193420Sweongyo	uint16_t data;
1443184610Salfred
1444184610Salfred	/* init RF-dependent PHY registers */
1445188417Sthompsa	for (i = 0; i < N(phyini); i++)
1446188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1447188417Sthompsa
1448184610Salfred	/* init cgt radio */
1449188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1450188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1451188417Sthompsa			return (error);
1452184610Salfred	}
1453193420Sweongyo
1454193420Sweongyo	error = zyd_gct_mode(rf);
1455193420Sweongyo	if (error != 0)
1456193420Sweongyo		return (error);
1457193420Sweongyo
1458233774Shselasky	for (i = 0; i < (int)(N(vco) - 1); i++) {
1459193420Sweongyo		error = zyd_gct_set_channel_synth(rf, 1, 0);
1460193420Sweongyo		if (error != 0)
1461193420Sweongyo			goto fail;
1462193420Sweongyo		error = zyd_gct_write(rf, vco[i][0]);
1463193420Sweongyo		if (error != 0)
1464193420Sweongyo			goto fail;
1465193420Sweongyo		zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf);
1466193420Sweongyo		zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data);
1467193420Sweongyo		if ((data & 0xf) == 0) {
1468193420Sweongyo			idx = i;
1469193420Sweongyo			break;
1470193420Sweongyo		}
1471193420Sweongyo	}
1472193420Sweongyo	if (idx == -1) {
1473193420Sweongyo		error = zyd_gct_set_channel_synth(rf, 1, 1);
1474193420Sweongyo		if (error != 0)
1475193420Sweongyo			goto fail;
1476193420Sweongyo		error = zyd_gct_write(rf, 0x6662);
1477193420Sweongyo		if (error != 0)
1478193420Sweongyo			goto fail;
1479193420Sweongyo	}
1480193420Sweongyo
1481193420Sweongyo	rf->idx = idx;
1482193420Sweongyo	zyd_write16_m(sc, ZYD_CR203, 0x6);
1483188417Sthompsafail:
1484188417Sthompsa	return (error);
1485188417Sthompsa#undef N
1486193420Sweongyo#undef ZYD_GCT_INTR_REG
1487184610Salfred}
1488184610Salfred
1489188417Sthompsastatic int
1490193420Sweongyozyd_gct_mode(struct zyd_rf *rf)
1491184610Salfred{
1492233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1493193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1494193420Sweongyo	static const uint32_t mode[] = {
1495193420Sweongyo		0x25f98, 0x25f9a, 0x25f94, 0x27fd4
1496193420Sweongyo	};
1497193420Sweongyo	int i, error;
1498188417Sthompsa
1499193420Sweongyo	for (i = 0; i < N(mode); i++) {
1500193420Sweongyo		if ((error = zyd_rfwrite(sc, mode[i])) != 0)
1501193420Sweongyo			break;
1502193420Sweongyo	}
1503193420Sweongyo	return (error);
1504193420Sweongyo#undef N
1505188417Sthompsa}
1506188417Sthompsa
1507188417Sthompsastatic int
1508193420Sweongyozyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal)
1509188417Sthompsa{
1510193420Sweongyo	int error, idx = chan - 1;
1511188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1512193420Sweongyo	static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL;
1513193420Sweongyo	static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD;
1514193420Sweongyo	static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV;
1515184610Salfred
1516193420Sweongyo	error = zyd_rfwrite(sc,
1517193420Sweongyo	    (acal == 1) ? acal_synth[idx] : std_synth[idx]);
1518188417Sthompsa	if (error != 0)
1519193420Sweongyo		return (error);
1520193420Sweongyo	return zyd_rfwrite(sc, div_synth[idx]);
1521184610Salfred}
1522184610Salfred
1523188417Sthompsastatic int
1524193420Sweongyozyd_gct_write(struct zyd_rf *rf, uint16_t value)
1525184610Salfred{
1526188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1527184610Salfred
1528193420Sweongyo	return zyd_rfwrite(sc, 0x300000 | 0x40000 | value);
1529184610Salfred}
1530184610Salfred
1531188417Sthompsastatic int
1532193420Sweongyozyd_gct_switch_radio(struct zyd_rf *rf, int on)
1533184610Salfred{
1534193420Sweongyo	int error;
1535193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1536188417Sthompsa
1537193420Sweongyo	error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90);
1538193420Sweongyo	if (error != 0)
1539193420Sweongyo		return (error);
1540193420Sweongyo
1541193420Sweongyo	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
1542193420Sweongyo	zyd_write16_m(sc, ZYD_CR251,
1543193420Sweongyo	    on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f);
1544193420Sweongyofail:
1545193420Sweongyo	return (error);
1546188417Sthompsa}
1547188417Sthompsa
1548188417Sthompsastatic int
1549193420Sweongyozyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1550188417Sthompsa{
1551233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1552193420Sweongyo	int error, i;
1553188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1554193420Sweongyo	static const struct zyd_phy_pair cmd[] = {
1555193420Sweongyo		{ ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 },
1556193420Sweongyo		{ ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 },
1557193420Sweongyo	};
1558193420Sweongyo	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1559184610Salfred
1560193420Sweongyo	error = zyd_gct_set_channel_synth(rf, chan, 0);
1561188417Sthompsa	if (error != 0)
1562188417Sthompsa		goto fail;
1563193420Sweongyo	error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 :
1564193420Sweongyo	    vco[rf->idx][((chan - 1) / 2)]);
1565188417Sthompsa	if (error != 0)
1566188417Sthompsa		goto fail;
1567193420Sweongyo	error = zyd_gct_mode(rf);
1568193420Sweongyo	if (error != 0)
1569193420Sweongyo		return (error);
1570193420Sweongyo	for (i = 0; i < N(cmd); i++)
1571193420Sweongyo		zyd_write16_m(sc, cmd[i].reg, cmd[i].val);
1572193420Sweongyo	error = zyd_gct_txgain(rf, chan);
1573193420Sweongyo	if (error != 0)
1574193420Sweongyo		return (error);
1575193420Sweongyo	zyd_write16_m(sc, ZYD_CR203, 0x6);
1576188417Sthompsafail:
1577188417Sthompsa	return (error);
1578188417Sthompsa#undef N
1579184610Salfred}
1580184610Salfred
1581193420Sweongyostatic int
1582193420Sweongyozyd_gct_txgain(struct zyd_rf *rf, uint8_t chan)
1583193420Sweongyo{
1584193420Sweongyo#define N(a)	(sizeof(a) / sizeof((a)[0]))
1585193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1586193420Sweongyo	static uint32_t txgain[] = ZYD_GCT_TXGAIN;
1587193420Sweongyo	uint8_t idx = sc->sc_pwrint[chan - 1];
1588193420Sweongyo
1589193420Sweongyo	if (idx >= N(txgain)) {
1590193420Sweongyo		device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n",
1591193420Sweongyo		    chan, idx);
1592193420Sweongyo		return 0;
1593193420Sweongyo	}
1594193420Sweongyo
1595193420Sweongyo	return zyd_rfwrite(sc, 0x700000 | txgain[idx]);
1596193420Sweongyo#undef N
1597193420Sweongyo}
1598193420Sweongyo
1599184610Salfred/*
1600184610Salfred * Maxim2 RF methods.
1601184610Salfred */
1602188417Sthompsastatic int
1603188417Sthompsazyd_maxim2_init(struct zyd_rf *rf)
1604184610Salfred{
1605233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1606188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1607184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1608184610Salfred	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1609184610Salfred	uint16_t tmp;
1610188417Sthompsa	int i, error;
1611184610Salfred
1612184610Salfred	/* init RF-dependent PHY registers */
1613188417Sthompsa	for (i = 0; i < N(phyini); i++)
1614188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1615184610Salfred
1616188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1617188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1618188417Sthompsa
1619184610Salfred	/* init maxim2 radio */
1620188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1621188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1622188417Sthompsa			return (error);
1623184610Salfred	}
1624188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1625188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1626188417Sthompsafail:
1627188417Sthompsa	return (error);
1628188417Sthompsa#undef N
1629184610Salfred}
1630184610Salfred
1631188417Sthompsastatic int
1632188417Sthompsazyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1633184610Salfred{
1634188417Sthompsa
1635188417Sthompsa	/* vendor driver does nothing for this RF chip */
1636188417Sthompsa	return (0);
1637188417Sthompsa}
1638188417Sthompsa
1639188417Sthompsastatic int
1640188417Sthompsazyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1641188417Sthompsa{
1642233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1643188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1644184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1645184610Salfred	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1646184610Salfred	static const struct {
1647188417Sthompsa		uint32_t	r1, r2;
1648188417Sthompsa	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
1649184610Salfred	uint16_t tmp;
1650188417Sthompsa	int i, error;
1651184610Salfred
1652184610Salfred	/*
1653184610Salfred	 * Do the same as we do when initializing it, except for the channel
1654184610Salfred	 * values coming from the two channel tables.
1655184610Salfred	 */
1656184610Salfred
1657184610Salfred	/* init RF-dependent PHY registers */
1658188417Sthompsa	for (i = 0; i < N(phyini); i++)
1659188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1660184610Salfred
1661188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1662188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1663188417Sthompsa
1664184610Salfred	/* first two values taken from the chantables */
1665188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1666188417Sthompsa	if (error != 0)
1667188417Sthompsa		goto fail;
1668188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1669188417Sthompsa	if (error != 0)
1670188417Sthompsa		goto fail;
1671184610Salfred
1672184610Salfred	/* init maxim2 radio - skipping the two first values */
1673188417Sthompsa	for (i = 2; i < N(rfini); i++) {
1674188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1675188417Sthompsa			return (error);
1676184610Salfred	}
1677188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1678188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1679188417Sthompsafail:
1680188417Sthompsa	return (error);
1681188417Sthompsa#undef N
1682184610Salfred}
1683184610Salfred
1684188417Sthompsastatic int
1685188417Sthompsazyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1686184610Salfred{
1687188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1688184610Salfred
1689188417Sthompsa	rf->rf_sc = sc;
1690193420Sweongyo	rf->update_pwr = 1;
1691188417Sthompsa
1692188417Sthompsa	switch (type) {
1693184610Salfred	case ZYD_RF_RFMD:
1694188417Sthompsa		rf->init         = zyd_rfmd_init;
1695188417Sthompsa		rf->switch_radio = zyd_rfmd_switch_radio;
1696188417Sthompsa		rf->set_channel  = zyd_rfmd_set_channel;
1697188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1698184610Salfred		break;
1699184610Salfred	case ZYD_RF_AL2230:
1700186730Salfred	case ZYD_RF_AL2230S:
1701188417Sthompsa		if (sc->sc_macrev == ZYD_ZD1211B) {
1702188417Sthompsa			rf->init = zyd_al2230_init_b;
1703188417Sthompsa			rf->set_channel = zyd_al2230_set_channel_b;
1704186730Salfred		} else {
1705188417Sthompsa			rf->init = zyd_al2230_init;
1706188417Sthompsa			rf->set_channel = zyd_al2230_set_channel;
1707186730Salfred		}
1708188417Sthompsa		rf->switch_radio = zyd_al2230_switch_radio;
1709188417Sthompsa		rf->bandedge6	 = zyd_al2230_bandedge6;
1710188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1711184610Salfred		break;
1712184610Salfred	case ZYD_RF_AL7230B:
1713188417Sthompsa		rf->init         = zyd_al7230B_init;
1714188417Sthompsa		rf->switch_radio = zyd_al7230B_switch_radio;
1715188417Sthompsa		rf->set_channel  = zyd_al7230B_set_channel;
1716188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1717184610Salfred		break;
1718184610Salfred	case ZYD_RF_AL2210:
1719188417Sthompsa		rf->init         = zyd_al2210_init;
1720188417Sthompsa		rf->switch_radio = zyd_al2210_switch_radio;
1721188417Sthompsa		rf->set_channel  = zyd_al2210_set_channel;
1722188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1723184610Salfred		break;
1724193420Sweongyo	case ZYD_RF_MAXIM_NEW:
1725184610Salfred	case ZYD_RF_GCT:
1726188417Sthompsa		rf->init         = zyd_gct_init;
1727188417Sthompsa		rf->switch_radio = zyd_gct_switch_radio;
1728188417Sthompsa		rf->set_channel  = zyd_gct_set_channel;
1729193420Sweongyo		rf->width        = 24;	/* 24-bit RF values */
1730193420Sweongyo		rf->update_pwr   = 0;
1731184610Salfred		break;
1732184610Salfred	case ZYD_RF_MAXIM_NEW2:
1733188417Sthompsa		rf->init         = zyd_maxim2_init;
1734188417Sthompsa		rf->switch_radio = zyd_maxim2_switch_radio;
1735188417Sthompsa		rf->set_channel  = zyd_maxim2_set_channel;
1736188417Sthompsa		rf->width        = 18;	/* 18-bit RF values */
1737184610Salfred		break;
1738184610Salfred	default:
1739188417Sthompsa		device_printf(sc->sc_dev,
1740188417Sthompsa		    "sorry, radio \"%s\" is not supported yet\n",
1741188417Sthompsa		    zyd_rf_name(type));
1742188417Sthompsa		return (EINVAL);
1743184610Salfred	}
1744188417Sthompsa	return (0);
1745188417Sthompsa}
1746184610Salfred
1747188417Sthompsastatic const char *
1748188417Sthompsazyd_rf_name(uint8_t type)
1749188417Sthompsa{
1750188417Sthompsa	static const char * const zyd_rfs[] = {
1751188417Sthompsa		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
1752188417Sthompsa		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
1753188417Sthompsa		"AL2230S",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
1754188417Sthompsa		"PHILIPS"
1755188417Sthompsa	};
1756184610Salfred
1757188417Sthompsa	return zyd_rfs[(type > 15) ? 0 : type];
1758184610Salfred}
1759184610Salfred
1760188417Sthompsastatic int
1761188417Sthompsazyd_hw_init(struct zyd_softc *sc)
1762184610Salfred{
1763188417Sthompsa	int error;
1764184610Salfred	const struct zyd_phy_pair *phyp;
1765188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1766188417Sthompsa	uint16_t val;
1767184610Salfred
1768184610Salfred	/* specify that the plug and play is finished */
1769188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1770188417Sthompsa	zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
1771188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
1772188417Sthompsa	    sc->sc_fwbase);
1773184610Salfred
1774184610Salfred	/* retrieve firmware revision number */
1775188417Sthompsa	zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
1776188417Sthompsa	zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
1777188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1778186730Salfred	/* set mandatory rates - XXX assumes 802.11b/g */
1779188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
1780186730Salfred
1781184610Salfred	/* disable interrupts */
1782188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
1783184610Salfred
1784188417Sthompsa	if ((error = zyd_read_pod(sc)) != 0) {
1785188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
1786188417Sthompsa		goto fail;
1787184610Salfred	}
1788188417Sthompsa
1789188417Sthompsa	/* PHY init (resetting) */
1790188417Sthompsa	error = zyd_lock_phy(sc);
1791188417Sthompsa	if (error != 0)
1792188417Sthompsa		goto fail;
1793188417Sthompsa	phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1794188417Sthompsa	for (; phyp->reg != 0; phyp++)
1795188417Sthompsa		zyd_write16_m(sc, phyp->reg, phyp->val);
1796188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
1797188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
1798188417Sthompsa		zyd_write32_m(sc, ZYD_CR157, val >> 8);
1799184610Salfred	}
1800188417Sthompsa	error = zyd_unlock_phy(sc);
1801188417Sthompsa	if (error != 0)
1802188417Sthompsa		goto fail;
1803184610Salfred
1804184610Salfred	/* HMAC init */
1805188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1806188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1807188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
1808188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
1809188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
1810188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
1811188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
1812188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1813188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1814188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1815188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1816188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1817188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1818188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1819188417Sthompsa	zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
1820188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1821188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1822188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1823188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
1824188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
1825184610Salfred
1826188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211) {
1827188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
1828188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1829186730Salfred	} else {
1830188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1831188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1832188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1833188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1834188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1835188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1836188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1837188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
1838188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
1839186730Salfred	}
1840186730Salfred
1841184610Salfred	/* init beacon interval to 100ms */
1842188417Sthompsa	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1843188417Sthompsa		goto fail;
1844184610Salfred
1845188417Sthompsa	if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
1846188417Sthompsa		device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
1847188417Sthompsa		    sc->sc_rfrev);
1848188417Sthompsa		goto fail;
1849188417Sthompsa	}
1850188417Sthompsa
1851188417Sthompsa	/* RF chip init */
1852188417Sthompsa	error = zyd_lock_phy(sc);
1853188417Sthompsa	if (error != 0)
1854188417Sthompsa		goto fail;
1855188417Sthompsa	error = (*rf->init)(rf);
1856188417Sthompsa	if (error != 0) {
1857188417Sthompsa		device_printf(sc->sc_dev,
1858188417Sthompsa		    "radio initialization failed, error %d\n", error);
1859188417Sthompsa		goto fail;
1860188417Sthompsa	}
1861188417Sthompsa	error = zyd_unlock_phy(sc);
1862188417Sthompsa	if (error != 0)
1863188417Sthompsa		goto fail;
1864188417Sthompsa
1865188417Sthompsa	if ((error = zyd_read_eeprom(sc)) != 0) {
1866188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
1867188417Sthompsa		goto fail;
1868188417Sthompsa	}
1869188417Sthompsa
1870188417Sthompsafail:	return (error);
1871184610Salfred}
1872184610Salfred
1873188417Sthompsastatic int
1874188417Sthompsazyd_read_pod(struct zyd_softc *sc)
1875184610Salfred{
1876188417Sthompsa	int error;
1877184610Salfred	uint32_t tmp;
1878184610Salfred
1879188417Sthompsa	zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
1880188417Sthompsa	sc->sc_rfrev     = tmp & 0x0f;
1881188417Sthompsa	sc->sc_ledtype   = (tmp >>  4) & 0x01;
1882188417Sthompsa	sc->sc_al2230s   = (tmp >>  7) & 0x01;
1883188417Sthompsa	sc->sc_cckgain   = (tmp >>  8) & 0x01;
1884184610Salfred	sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
1885188417Sthompsa	sc->sc_parev     = (tmp >> 16) & 0x0f;
1886186730Salfred	sc->sc_bandedge6 = (tmp >> 21) & 0x01;
1887188417Sthompsa	sc->sc_newphy    = (tmp >> 31) & 0x01;
1888188417Sthompsa	sc->sc_txled     = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
1889188417Sthompsafail:
1890188417Sthompsa	return (error);
1891188417Sthompsa}
1892184610Salfred
1893188417Sthompsastatic int
1894188417Sthompsazyd_read_eeprom(struct zyd_softc *sc)
1895188417Sthompsa{
1896188417Sthompsa	uint16_t val;
1897188417Sthompsa	int error, i;
1898184610Salfred
1899184610Salfred	/* read Tx power calibration tables */
1900184610Salfred	for (i = 0; i < 7; i++) {
1901188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1902188417Sthompsa		sc->sc_pwrcal[i * 2] = val >> 8;
1903188417Sthompsa		sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
1904188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
1905188417Sthompsa		sc->sc_pwrint[i * 2] = val >> 8;
1906188417Sthompsa		sc->sc_pwrint[i * 2 + 1] = val & 0xff;
1907188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
1908188417Sthompsa		sc->sc_ofdm36_cal[i * 2] = val >> 8;
1909188417Sthompsa		sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
1910188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
1911188417Sthompsa		sc->sc_ofdm48_cal[i * 2] = val >> 8;
1912188417Sthompsa		sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
1913188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
1914188417Sthompsa		sc->sc_ofdm54_cal[i * 2] = val >> 8;
1915188417Sthompsa		sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
1916184610Salfred	}
1917188417Sthompsafail:
1918188417Sthompsa	return (error);
1919184610Salfred}
1920184610Salfred
1921188417Sthompsastatic int
1922188417Sthompsazyd_get_macaddr(struct zyd_softc *sc)
1923186730Salfred{
1924192984Sthompsa	struct usb_device_request req;
1925193045Sthompsa	usb_error_t error;
1926186730Salfred
1927186730Salfred	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1928186730Salfred	req.bRequest = ZYD_READFWDATAREQ;
1929186730Salfred	USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
1930186730Salfred	USETW(req.wIndex, 0);
1931186730Salfred	USETW(req.wLength, IEEE80211_ADDR_LEN);
1932186730Salfred
1933188419Sthompsa	error = zyd_do_request(sc, &req, sc->sc_bssid);
1934188417Sthompsa	if (error != 0) {
1935188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1936194228Sthompsa		    usbd_errstr(error));
1937188417Sthompsa	}
1938188417Sthompsa
1939188417Sthompsa	return (error);
1940186730Salfred}
1941186730Salfred
1942188417Sthompsastatic int
1943188417Sthompsazyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1944184610Salfred{
1945188417Sthompsa	int error;
1946184610Salfred	uint32_t tmp;
1947184610Salfred
1948188417Sthompsa	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1949188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
1950188417Sthompsa	tmp = addr[5] << 8 | addr[4];
1951188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
1952188417Sthompsafail:
1953188417Sthompsa	return (error);
1954188417Sthompsa}
1955184610Salfred
1956188417Sthompsastatic int
1957188417Sthompsazyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1958188417Sthompsa{
1959188417Sthompsa	int error;
1960188417Sthompsa	uint32_t tmp;
1961188417Sthompsa
1962188417Sthompsa	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1963188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
1964188417Sthompsa	tmp = addr[5] << 8 | addr[4];
1965188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
1966188417Sthompsafail:
1967188417Sthompsa	return (error);
1968184610Salfred}
1969184610Salfred
1970188417Sthompsastatic int
1971188417Sthompsazyd_switch_radio(struct zyd_softc *sc, int on)
1972184610Salfred{
1973188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1974188417Sthompsa	int error;
1975188417Sthompsa
1976188417Sthompsa	error = zyd_lock_phy(sc);
1977188417Sthompsa	if (error != 0)
1978188417Sthompsa		goto fail;
1979188417Sthompsa	error = (*rf->switch_radio)(rf, on);
1980188417Sthompsa	if (error != 0)
1981188417Sthompsa		goto fail;
1982188417Sthompsa	error = zyd_unlock_phy(sc);
1983188417Sthompsafail:
1984188417Sthompsa	return (error);
1985184610Salfred}
1986184610Salfred
1987188417Sthompsastatic int
1988188417Sthompsazyd_set_led(struct zyd_softc *sc, int which, int on)
1989184610Salfred{
1990188417Sthompsa	int error;
1991184610Salfred	uint32_t tmp;
1992184610Salfred
1993188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1994188417Sthompsa	tmp &= ~which;
1995188417Sthompsa	if (on)
1996188417Sthompsa		tmp |= which;
1997188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1998188417Sthompsafail:
1999188417Sthompsa	return (error);
2000188417Sthompsa}
2001184610Salfred
2002188417Sthompsastatic void
2003188417Sthompsazyd_set_multi(struct zyd_softc *sc)
2004184610Salfred{
2005188417Sthompsa	int error;
2006188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2007188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2008188417Sthompsa	struct ifmultiaddr *ifma;
2009188417Sthompsa	uint32_t low, high;
2010188417Sthompsa	uint8_t v;
2011184610Salfred
2012188417Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2013188417Sthompsa		return;
2014184610Salfred
2015188417Sthompsa	low = 0x00000000;
2016188417Sthompsa	high = 0x80000000;
2017184610Salfred
2018188417Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR ||
2019188417Sthompsa	    (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) {
2020188417Sthompsa		low = 0xffffffff;
2021188417Sthompsa		high = 0xffffffff;
2022184610Salfred	} else {
2023195049Srwatson		if_maddr_rlock(ifp);
2024188417Sthompsa		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2025188417Sthompsa			if (ifma->ifma_addr->sa_family != AF_LINK)
2026188417Sthompsa				continue;
2027188417Sthompsa			v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
2028188417Sthompsa			    ifma->ifma_addr))[5] >> 2;
2029188417Sthompsa			if (v < 32)
2030188417Sthompsa				low |= 1 << v;
2031188417Sthompsa			else
2032188417Sthompsa				high |= 1 << (v - 32);
2033188417Sthompsa		}
2034195049Srwatson		if_maddr_runlock(ifp);
2035184610Salfred	}
2036184610Salfred
2037188417Sthompsa	/* reprogram multicast global hash table */
2038188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBL, low);
2039188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBH, high);
2040188417Sthompsafail:
2041188417Sthompsa	if (error != 0)
2042188417Sthompsa		device_printf(sc->sc_dev,
2043188417Sthompsa		    "could not set multicast hash table\n");
2044188417Sthompsa}
2045184610Salfred
2046188417Sthompsastatic void
2047188417Sthompsazyd_update_mcast(struct ifnet *ifp)
2048188417Sthompsa{
2049188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2050184610Salfred
2051188417Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2052184610Salfred		return;
2053184610Salfred
2054188417Sthompsa	ZYD_LOCK(sc);
2055191746Sthompsa	zyd_set_multi(sc);
2056188417Sthompsa	ZYD_UNLOCK(sc);
2057188417Sthompsa}
2058184610Salfred
2059188417Sthompsastatic int
2060188417Sthompsazyd_set_rxfilter(struct zyd_softc *sc)
2061188417Sthompsa{
2062188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2063188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2064188417Sthompsa	uint32_t rxfilter;
2065184610Salfred
2066188417Sthompsa	switch (ic->ic_opmode) {
2067188417Sthompsa	case IEEE80211_M_STA:
2068188417Sthompsa		rxfilter = ZYD_FILTER_BSS;
2069188417Sthompsa		break;
2070188417Sthompsa	case IEEE80211_M_IBSS:
2071188417Sthompsa	case IEEE80211_M_HOSTAP:
2072188417Sthompsa		rxfilter = ZYD_FILTER_HOSTAP;
2073188417Sthompsa		break;
2074188417Sthompsa	case IEEE80211_M_MONITOR:
2075188417Sthompsa		rxfilter = ZYD_FILTER_MONITOR;
2076188417Sthompsa		break;
2077188417Sthompsa	default:
2078188417Sthompsa		/* should not get there */
2079188417Sthompsa		return (EINVAL);
2080184610Salfred	}
2081188417Sthompsa	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
2082188417Sthompsa}
2083184610Salfred
2084188417Sthompsastatic void
2085188417Sthompsazyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
2086188417Sthompsa{
2087188417Sthompsa	int error;
2088188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2089188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2090188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
2091188417Sthompsa	uint32_t tmp;
2092188601Sthompsa	int chan;
2093184610Salfred
2094188417Sthompsa	chan = ieee80211_chan2ieee(ic, c);
2095188417Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
2096188417Sthompsa		/* XXX should NEVER happen */
2097188417Sthompsa		device_printf(sc->sc_dev,
2098188417Sthompsa		    "%s: invalid channel %x\n", __func__, chan);
2099188417Sthompsa		return;
2100188417Sthompsa	}
2101184610Salfred
2102188417Sthompsa	error = zyd_lock_phy(sc);
2103188417Sthompsa	if (error != 0)
2104188417Sthompsa		goto fail;
2105184610Salfred
2106188417Sthompsa	error = (*rf->set_channel)(rf, chan);
2107188417Sthompsa	if (error != 0)
2108188417Sthompsa		goto fail;
2109184610Salfred
2110193420Sweongyo	if (rf->update_pwr) {
2111193420Sweongyo		/* update Tx power */
2112193420Sweongyo		zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
2113184610Salfred
2114193420Sweongyo		if (sc->sc_macrev == ZYD_ZD1211B) {
2115193420Sweongyo			zyd_write16_m(sc, ZYD_CR67,
2116193420Sweongyo			    sc->sc_ofdm36_cal[chan - 1]);
2117193420Sweongyo			zyd_write16_m(sc, ZYD_CR66,
2118193420Sweongyo			    sc->sc_ofdm48_cal[chan - 1]);
2119193420Sweongyo			zyd_write16_m(sc, ZYD_CR65,
2120193420Sweongyo			    sc->sc_ofdm54_cal[chan - 1]);
2121193420Sweongyo			zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
2122193420Sweongyo			zyd_write16_m(sc, ZYD_CR69, 0x28);
2123193420Sweongyo			zyd_write16_m(sc, ZYD_CR69, 0x2a);
2124193420Sweongyo		}
2125188417Sthompsa	}
2126188417Sthompsa	if (sc->sc_cckgain) {
2127188417Sthompsa		/* set CCK baseband gain from EEPROM */
2128188417Sthompsa		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
2129188417Sthompsa			zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
2130188417Sthompsa	}
2131188417Sthompsa	if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
2132188417Sthompsa		error = (*rf->bandedge6)(rf, c);
2133188417Sthompsa		if (error != 0)
2134188417Sthompsa			goto fail;
2135188417Sthompsa	}
2136188417Sthompsa	zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
2137184610Salfred
2138188417Sthompsa	error = zyd_unlock_phy(sc);
2139188417Sthompsa	if (error != 0)
2140188417Sthompsa		goto fail;
2141184610Salfred
2142188417Sthompsa	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
2143188417Sthompsa	    htole16(c->ic_freq);
2144188417Sthompsa	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
2145188417Sthompsa	    htole16(c->ic_flags);
2146188417Sthompsafail:
2147184610Salfred	return;
2148184610Salfred}
2149184610Salfred
2150184610Salfredstatic int
2151188417Sthompsazyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
2152184610Salfred{
2153188417Sthompsa	int error;
2154188417Sthompsa	uint32_t val;
2155184610Salfred
2156188417Sthompsa	zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
2157188417Sthompsa	sc->sc_atim_wnd = val;
2158188417Sthompsa	zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
2159188417Sthompsa	sc->sc_pre_tbtt = val;
2160188417Sthompsa	sc->sc_bcn_int = bintval;
2161184610Salfred
2162188417Sthompsa	if (sc->sc_bcn_int <= 5)
2163188417Sthompsa		sc->sc_bcn_int = 5;
2164188417Sthompsa	if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
2165188417Sthompsa		sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
2166188417Sthompsa	if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
2167188417Sthompsa		sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
2168184610Salfred
2169188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
2170188417Sthompsa	zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
2171188417Sthompsa	zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
2172188417Sthompsafail:
2173188417Sthompsa	return (error);
2174188417Sthompsa}
2175184610Salfred
2176188417Sthompsastatic void
2177192984Sthompsazyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
2178188417Sthompsa{
2179194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2180188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2181192468Ssam	struct ieee80211com *ic = ifp->if_l2com;
2182188417Sthompsa	struct zyd_plcphdr plcp;
2183188417Sthompsa	struct zyd_rx_stat stat;
2184194677Sthompsa	struct usb_page_cache *pc;
2185188417Sthompsa	struct mbuf *m;
2186188417Sthompsa	int rlen, rssi;
2187184610Salfred
2188188417Sthompsa	if (len < ZYD_MIN_FRAGSZ) {
2189188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
2190188417Sthompsa		    device_get_nameunit(sc->sc_dev), len);
2191188417Sthompsa		ifp->if_ierrors++;
2192188417Sthompsa		return;
2193188417Sthompsa	}
2194194677Sthompsa	pc = usbd_xfer_get_frame(xfer, 0);
2195194677Sthompsa	usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2196194677Sthompsa	usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
2197184610Salfred
2198188417Sthompsa	if (stat.flags & ZYD_RX_ERROR) {
2199188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV,
2200188417Sthompsa		    "%s: RX status indicated error (%x)\n",
2201188417Sthompsa		    device_get_nameunit(sc->sc_dev), stat.flags);
2202188417Sthompsa		ifp->if_ierrors++;
2203188417Sthompsa		return;
2204188417Sthompsa	}
2205184610Salfred
2206188417Sthompsa	/* compute actual frame length */
2207188417Sthompsa	rlen = len - sizeof(struct zyd_plcphdr) -
2208188417Sthompsa	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
2209184610Salfred
2210188417Sthompsa	/* allocate a mbuf to store the frame */
2211233774Shselasky	if (rlen > (int)MCLBYTES) {
2212188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
2213188417Sthompsa		    device_get_nameunit(sc->sc_dev), rlen);
2214188417Sthompsa		ifp->if_ierrors++;
2215188417Sthompsa		return;
2216233774Shselasky	} else if (rlen > (int)MHLEN)
2217243857Sglebius		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2218188417Sthompsa	else
2219243857Sglebius		m = m_gethdr(M_NOWAIT, MT_DATA);
2220188417Sthompsa	if (m == NULL) {
2221188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
2222188417Sthompsa		    device_get_nameunit(sc->sc_dev));
2223188417Sthompsa		ifp->if_ierrors++;
2224188417Sthompsa		return;
2225184610Salfred	}
2226188417Sthompsa	m->m_pkthdr.rcvif = ifp;
2227188417Sthompsa	m->m_pkthdr.len = m->m_len = rlen;
2228194677Sthompsa	usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
2229184610Salfred
2230192468Ssam	if (ieee80211_radiotap_active(ic)) {
2231188417Sthompsa		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
2232184610Salfred
2233188417Sthompsa		tap->wr_flags = 0;
2234188417Sthompsa		if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
2235188417Sthompsa			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2236188417Sthompsa		/* XXX toss, no way to express errors */
2237188417Sthompsa		if (stat.flags & ZYD_RX_DECRYPTERR)
2238188417Sthompsa			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2239188417Sthompsa		tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
2240188417Sthompsa		    (stat.flags & ZYD_RX_OFDM) ?
2241188417Sthompsa			IEEE80211_T_OFDM : IEEE80211_T_CCK);
2242188417Sthompsa		tap->wr_antsignal = stat.rssi + -95;
2243188417Sthompsa		tap->wr_antnoise = -95;	/* XXX */
2244188417Sthompsa	}
2245188417Sthompsa	rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
2246184610Salfred
2247188417Sthompsa	sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
2248188417Sthompsa	sc->sc_rx_data[sc->sc_rx_count].m = m;
2249188417Sthompsa	sc->sc_rx_count++;
2250184610Salfred}
2251184610Salfred
2252184610Salfredstatic void
2253194677Sthompsazyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
2254184610Salfred{
2255194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2256184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2257184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
2258188417Sthompsa	struct ieee80211_node *ni;
2259188417Sthompsa	struct zyd_rx_desc desc;
2260188417Sthompsa	struct mbuf *m;
2261194677Sthompsa	struct usb_page_cache *pc;
2262188417Sthompsa	uint32_t offset;
2263188417Sthompsa	uint8_t rssi;
2264188417Sthompsa	int8_t nf;
2265188417Sthompsa	int i;
2266194677Sthompsa	int actlen;
2267184610Salfred
2268194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2269194677Sthompsa
2270188417Sthompsa	sc->sc_rx_count = 0;
2271188417Sthompsa	switch (USB_GET_STATE(xfer)) {
2272188417Sthompsa	case USB_ST_TRANSFERRED:
2273194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
2274194677Sthompsa		usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
2275184610Salfred
2276188417Sthompsa		offset = 0;
2277188417Sthompsa		if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
2278188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_RECV,
2279188417Sthompsa			    "%s: received multi-frame transfer\n", __func__);
2280184610Salfred
2281188417Sthompsa			for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2282188417Sthompsa				uint16_t len16 = UGETW(desc.len[i]);
2283184610Salfred
2284194677Sthompsa				if (len16 == 0 || len16 > actlen)
2285188417Sthompsa					break;
2286184610Salfred
2287188417Sthompsa				zyd_rx_data(xfer, offset, len16);
2288184610Salfred
2289188417Sthompsa				/* next frame is aligned on a 32-bit boundary */
2290188417Sthompsa				len16 = (len16 + 3) & ~3;
2291188417Sthompsa				offset += len16;
2292194677Sthompsa				if (len16 > actlen)
2293188417Sthompsa					break;
2294194677Sthompsa				actlen -= len16;
2295188417Sthompsa			}
2296188417Sthompsa		} else {
2297188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_RECV,
2298188417Sthompsa			    "%s: received single-frame transfer\n", __func__);
2299184610Salfred
2300194677Sthompsa			zyd_rx_data(xfer, 0, actlen);
2301188417Sthompsa		}
2302188417Sthompsa		/* FALLTHROUGH */
2303188417Sthompsa	case USB_ST_SETUP:
2304188417Sthompsatr_setup:
2305194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2306194228Sthompsa		usbd_transfer_submit(xfer);
2307184610Salfred
2308188417Sthompsa		/*
2309188417Sthompsa		 * At the end of a USB callback it is always safe to unlock
2310188417Sthompsa		 * the private mutex of a device! That is why we do the
2311188417Sthompsa		 * "ieee80211_input" here, and not some lines up!
2312188417Sthompsa		 */
2313188417Sthompsa		ZYD_UNLOCK(sc);
2314188417Sthompsa		for (i = 0; i < sc->sc_rx_count; i++) {
2315188417Sthompsa			rssi = sc->sc_rx_data[i].rssi;
2316188417Sthompsa			m = sc->sc_rx_data[i].m;
2317188417Sthompsa			sc->sc_rx_data[i].m = NULL;
2318184610Salfred
2319188417Sthompsa			nf = -95;	/* XXX */
2320184610Salfred
2321188417Sthompsa			ni = ieee80211_find_rxnode(ic,
2322188417Sthompsa			    mtod(m, struct ieee80211_frame_min *));
2323188417Sthompsa			if (ni != NULL) {
2324192468Ssam				(void)ieee80211_input(ni, m, rssi, nf);
2325188417Sthompsa				ieee80211_free_node(ni);
2326188417Sthompsa			} else
2327192468Ssam				(void)ieee80211_input_all(ic, m, rssi, nf);
2328188417Sthompsa		}
2329198099Sweongyo		if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
2330198099Sweongyo		    !IFQ_IS_EMPTY(&ifp->if_snd))
2331198099Sweongyo			zyd_start(ifp);
2332188417Sthompsa		ZYD_LOCK(sc);
2333188417Sthompsa		break;
2334184610Salfred
2335188417Sthompsa	default:			/* Error */
2336194677Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
2337184610Salfred
2338194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2339188417Sthompsa			/* try to clear stall first */
2340194677Sthompsa			usbd_xfer_set_stall(xfer);
2341188417Sthompsa			goto tr_setup;
2342184610Salfred		}
2343188417Sthompsa		break;
2344184610Salfred	}
2345184610Salfred}
2346184610Salfred
2347184610Salfredstatic uint8_t
2348193803Sweongyozyd_plcp_signal(struct zyd_softc *sc, int rate)
2349184610Salfred{
2350184610Salfred	switch (rate) {
2351188417Sthompsa	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
2352184610Salfred	case 12:
2353184610Salfred		return (0xb);
2354184610Salfred	case 18:
2355184610Salfred		return (0xf);
2356184610Salfred	case 24:
2357184610Salfred		return (0xa);
2358184610Salfred	case 36:
2359184610Salfred		return (0xe);
2360184610Salfred	case 48:
2361184610Salfred		return (0x9);
2362184610Salfred	case 72:
2363184610Salfred		return (0xd);
2364184610Salfred	case 96:
2365184610Salfred		return (0x8);
2366184610Salfred	case 108:
2367184610Salfred		return (0xc);
2368188417Sthompsa	/* CCK rates (NB: not IEEE std, device-specific) */
2369188417Sthompsa	case 2:
2370188417Sthompsa		return (0x0);
2371188417Sthompsa	case 4:
2372188417Sthompsa		return (0x1);
2373188417Sthompsa	case 11:
2374188417Sthompsa		return (0x2);
2375188417Sthompsa	case 22:
2376188417Sthompsa		return (0x3);
2377184610Salfred	}
2378184610Salfred
2379193803Sweongyo	device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2380193803Sweongyo	return (0x0);
2381184610Salfred}
2382184610Salfred
2383184610Salfredstatic void
2384194677Sthompsazyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
2385184610Salfred{
2386194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2387184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2388192468Ssam	struct ieee80211vap *vap;
2389188417Sthompsa	struct zyd_tx_data *data;
2390188417Sthompsa	struct mbuf *m;
2391194677Sthompsa	struct usb_page_cache *pc;
2392194677Sthompsa	int actlen;
2393184610Salfred
2394194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2395194677Sthompsa
2396188417Sthompsa	switch (USB_GET_STATE(xfer)) {
2397188417Sthompsa	case USB_ST_TRANSFERRED:
2398188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2399194677Sthompsa		    actlen);
2400184610Salfred
2401188417Sthompsa		/* free resources */
2402194677Sthompsa		data = usbd_xfer_get_priv(xfer);
2403188417Sthompsa		zyd_tx_free(data, 0);
2404194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2405184610Salfred
2406188417Sthompsa		ifp->if_opackets++;
2407188417Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2408184610Salfred
2409188417Sthompsa		/* FALLTHROUGH */
2410188417Sthompsa	case USB_ST_SETUP:
2411188417Sthompsatr_setup:
2412188417Sthompsa		data = STAILQ_FIRST(&sc->tx_q);
2413188417Sthompsa		if (data) {
2414188417Sthompsa			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
2415188417Sthompsa			m = data->m;
2416184610Salfred
2417233774Shselasky			if (m->m_pkthdr.len > (int)ZYD_MAX_TXBUFSZ) {
2418188417Sthompsa				DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
2419188417Sthompsa				    m->m_pkthdr.len);
2420188417Sthompsa				m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
2421188417Sthompsa			}
2422194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
2423194677Sthompsa			usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2424194677Sthompsa			usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
2425188417Sthompsa			    m->m_pkthdr.len);
2426184610Salfred
2427192468Ssam			vap = data->ni->ni_vap;
2428192468Ssam			if (ieee80211_radiotap_active_vap(vap)) {
2429188417Sthompsa				struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2430184610Salfred
2431188417Sthompsa				tap->wt_flags = 0;
2432188417Sthompsa				tap->wt_rate = data->rate;
2433184610Salfred
2434192468Ssam				ieee80211_radiotap_tx(vap, m);
2435188417Sthompsa			}
2436184610Salfred
2437194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2438194677Sthompsa			usbd_xfer_set_priv(xfer, data);
2439194228Sthompsa			usbd_transfer_submit(xfer);
2440188417Sthompsa		}
2441198099Sweongyo		ZYD_UNLOCK(sc);
2442198099Sweongyo		zyd_start(ifp);
2443198099Sweongyo		ZYD_LOCK(sc);
2444188417Sthompsa		break;
2445184610Salfred
2446188417Sthompsa	default:			/* Error */
2447188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2448194677Sthompsa		    usbd_errstr(error));
2449184610Salfred
2450188419Sthompsa		ifp->if_oerrors++;
2451194677Sthompsa		data = usbd_xfer_get_priv(xfer);
2452194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2453188419Sthompsa		if (data != NULL)
2454194677Sthompsa			zyd_tx_free(data, error);
2455188419Sthompsa
2456203141Sthompsa		if (error != USB_ERR_CANCELLED) {
2457203141Sthompsa			if (error == USB_ERR_TIMEOUT)
2458203141Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2459203141Sthompsa
2460203141Sthompsa			/*
2461203141Sthompsa			 * Try to clear stall first, also if other
2462203141Sthompsa			 * errors occur, hence clearing stall
2463203141Sthompsa			 * introduces a 50 ms delay:
2464203141Sthompsa			 */
2465194677Sthompsa			usbd_xfer_set_stall(xfer);
2466188417Sthompsa			goto tr_setup;
2467188417Sthompsa		}
2468184610Salfred		break;
2469184610Salfred	}
2470184610Salfred}
2471184610Salfred
2472188417Sthompsastatic int
2473193803Sweongyozyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2474184610Salfred{
2475188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2476188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2477188417Sthompsa	struct zyd_tx_desc *desc;
2478188417Sthompsa	struct zyd_tx_data *data;
2479188417Sthompsa	struct ieee80211_frame *wh;
2480188417Sthompsa	const struct ieee80211_txparam *tp;
2481188417Sthompsa	struct ieee80211_key *k;
2482188417Sthompsa	int rate, totlen;
2483269127Shselasky	static const uint8_t ratediv[] = ZYD_TX_RATEDIV;
2484193803Sweongyo	uint8_t phy;
2485188417Sthompsa	uint16_t pktlen;
2486193803Sweongyo	uint32_t bits;
2487184610Salfred
2488188417Sthompsa	wh = mtod(m0, struct ieee80211_frame *);
2489188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
2490188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
2491188417Sthompsa	sc->tx_nfree--;
2492184610Salfred
2493193803Sweongyo	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT ||
2494193803Sweongyo	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) {
2495193803Sweongyo		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2496193803Sweongyo		rate = tp->mgmtrate;
2497188417Sthompsa	} else {
2498193803Sweongyo		tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
2499193803Sweongyo		/* for data frames */
2500193803Sweongyo		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
2501193803Sweongyo			rate = tp->mcastrate;
2502193803Sweongyo		else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2503193803Sweongyo			rate = tp->ucastrate;
2504193803Sweongyo		else {
2505206358Srpaulo			(void) ieee80211_ratectl_rate(ni, NULL, 0);
2506193803Sweongyo			rate = ni->ni_txrate;
2507193803Sweongyo		}
2508184610Salfred	}
2509184610Salfred
2510260444Skevlo	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2511188417Sthompsa		k = ieee80211_crypto_encap(ni, m0);
2512188417Sthompsa		if (k == NULL) {
2513188417Sthompsa			m_freem(m0);
2514188417Sthompsa			return (ENOBUFS);
2515188417Sthompsa		}
2516188417Sthompsa		/* packet header may have moved, reset our local pointer */
2517188417Sthompsa		wh = mtod(m0, struct ieee80211_frame *);
2518184610Salfred	}
2519184610Salfred
2520188417Sthompsa	data->ni = ni;
2521188417Sthompsa	data->m = m0;
2522193803Sweongyo	data->rate = rate;
2523184610Salfred
2524193803Sweongyo	/* fill Tx descriptor */
2525193803Sweongyo	desc = &data->desc;
2526193803Sweongyo	phy = zyd_plcp_signal(sc, rate);
2527193803Sweongyo	desc->phy = phy;
2528193803Sweongyo	if (ZYD_RATE_IS_OFDM(rate)) {
2529193803Sweongyo		desc->phy |= ZYD_TX_PHY_OFDM;
2530193803Sweongyo		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2531193803Sweongyo			desc->phy |= ZYD_TX_PHY_5GHZ;
2532193803Sweongyo	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2533193803Sweongyo		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2534193803Sweongyo
2535188417Sthompsa	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2536188417Sthompsa	desc->len = htole16(totlen);
2537184610Salfred
2538193803Sweongyo	desc->flags = ZYD_TX_FLAG_BACKOFF;
2539188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2540188417Sthompsa		/* multicast frames are not sent at OFDM rates in 802.11b/g */
2541188417Sthompsa		if (totlen > vap->iv_rtsthreshold) {
2542188417Sthompsa			desc->flags |= ZYD_TX_FLAG_RTS;
2543188417Sthompsa		} else if (ZYD_RATE_IS_OFDM(rate) &&
2544188417Sthompsa		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
2545188417Sthompsa			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2546188417Sthompsa				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2547188417Sthompsa			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2548188417Sthompsa				desc->flags |= ZYD_TX_FLAG_RTS;
2549188417Sthompsa		}
2550193803Sweongyo	} else
2551193803Sweongyo		desc->flags |= ZYD_TX_FLAG_MULTICAST;
2552188417Sthompsa	if ((wh->i_fc[0] &
2553188417Sthompsa	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2554188417Sthompsa	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2555188417Sthompsa		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2556188417Sthompsa
2557184610Salfred	/* actual transmit length (XXX why +10?) */
2558193803Sweongyo	pktlen = ZYD_TX_DESC_SIZE + 10;
2559188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211)
2560184610Salfred		pktlen += totlen;
2561188417Sthompsa	desc->pktlen = htole16(pktlen);
2562184610Salfred
2563193803Sweongyo	bits = (rate == 11) ? (totlen * 16) + 10 :
2564193803Sweongyo	    ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2565196809Sweongyo	desc->plcp_length = htole16(bits / ratediv[phy]);
2566188417Sthompsa	desc->plcp_service = 0;
2567193803Sweongyo	if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
2568193803Sweongyo		desc->plcp_service |= ZYD_PLCP_LENGEXT;
2569193803Sweongyo	desc->nextlen = 0;
2570193803Sweongyo
2571193803Sweongyo	if (ieee80211_radiotap_active_vap(vap)) {
2572193803Sweongyo		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2573193803Sweongyo
2574193803Sweongyo		tap->wt_flags = 0;
2575193803Sweongyo		tap->wt_rate = rate;
2576193803Sweongyo
2577193803Sweongyo		ieee80211_radiotap_tx(vap, m0);
2578184610Salfred	}
2579184610Salfred
2580188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_XMIT,
2581188417Sthompsa	    "%s: sending data frame len=%zu rate=%u\n",
2582188417Sthompsa	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2583188417Sthompsa		rate);
2584184610Salfred
2585188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2586194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
2587184610Salfred
2588188417Sthompsa	return (0);
2589184610Salfred}
2590184610Salfred
2591184610Salfredstatic void
2592188417Sthompsazyd_start(struct ifnet *ifp)
2593184610Salfred{
2594188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2595188417Sthompsa	struct ieee80211_node *ni;
2596184610Salfred	struct mbuf *m;
2597184610Salfred
2598188417Sthompsa	ZYD_LOCK(sc);
2599188417Sthompsa	for (;;) {
2600188417Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2601188417Sthompsa		if (m == NULL)
2602184610Salfred			break;
2603188417Sthompsa		if (sc->tx_nfree == 0) {
2604188417Sthompsa			IFQ_DRV_PREPEND(&ifp->if_snd, m);
2605188417Sthompsa			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2606184610Salfred			break;
2607184610Salfred		}
2608188417Sthompsa		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2609193803Sweongyo		if (zyd_tx_start(sc, m, ni) != 0) {
2610188417Sthompsa			ieee80211_free_node(ni);
2611188417Sthompsa			ifp->if_oerrors++;
2612188417Sthompsa			break;
2613184610Salfred		}
2614184610Salfred	}
2615188417Sthompsa	ZYD_UNLOCK(sc);
2616184610Salfred}
2617184610Salfred
2618188417Sthompsastatic int
2619188417Sthompsazyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2620188417Sthompsa	const struct ieee80211_bpf_params *params)
2621184610Salfred{
2622188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2623188417Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2624188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2625184610Salfred
2626188417Sthompsa	ZYD_LOCK(sc);
2627188417Sthompsa	/* prevent management frames from being sent if we're not ready */
2628188417Sthompsa	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2629188417Sthompsa		ZYD_UNLOCK(sc);
2630188417Sthompsa		m_freem(m);
2631188417Sthompsa		ieee80211_free_node(ni);
2632188417Sthompsa		return (ENETDOWN);
2633188417Sthompsa	}
2634188417Sthompsa	if (sc->tx_nfree == 0) {
2635188417Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2636188417Sthompsa		ZYD_UNLOCK(sc);
2637188417Sthompsa		m_freem(m);
2638188417Sthompsa		ieee80211_free_node(ni);
2639188417Sthompsa		return (ENOBUFS);		/* XXX */
2640188417Sthompsa	}
2641188417Sthompsa
2642188417Sthompsa	/*
2643188417Sthompsa	 * Legacy path; interpret frame contents to decide
2644188417Sthompsa	 * precisely how to send the frame.
2645188417Sthompsa	 * XXX raw path
2646188417Sthompsa	 */
2647193803Sweongyo	if (zyd_tx_start(sc, m, ni) != 0) {
2648188417Sthompsa		ZYD_UNLOCK(sc);
2649188417Sthompsa		ifp->if_oerrors++;
2650188417Sthompsa		ieee80211_free_node(ni);
2651188417Sthompsa		return (EIO);
2652188417Sthompsa	}
2653188417Sthompsa	ZYD_UNLOCK(sc);
2654188417Sthompsa	return (0);
2655184610Salfred}
2656184610Salfred
2657184610Salfredstatic int
2658188417Sthompsazyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2659184610Salfred{
2660184610Salfred	struct zyd_softc *sc = ifp->if_softc;
2661184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
2662188417Sthompsa	struct ifreq *ifr = (struct ifreq *) data;
2663246614Shselasky	int error;
2664246614Shselasky	int startall = 0;
2665184610Salfred
2666246614Shselasky	ZYD_LOCK(sc);
2667246614Shselasky	error = (sc->sc_flags & ZYD_FLAG_DETACHED) ? ENXIO : 0;
2668246614Shselasky	ZYD_UNLOCK(sc);
2669246614Shselasky	if (error)
2670246614Shselasky		return (error);
2671246614Shselasky
2672184610Salfred	switch (cmd) {
2673184610Salfred	case SIOCSIFFLAGS:
2674188417Sthompsa		ZYD_LOCK(sc);
2675184610Salfred		if (ifp->if_flags & IFF_UP) {
2676191746Sthompsa			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2677191746Sthompsa				zyd_init_locked(sc);
2678188417Sthompsa				startall = 1;
2679191746Sthompsa			} else
2680191746Sthompsa				zyd_set_multi(sc);
2681184610Salfred		} else {
2682191746Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2683191746Sthompsa				zyd_stop(sc);
2684184610Salfred		}
2685188417Sthompsa		ZYD_UNLOCK(sc);
2686188417Sthompsa		if (startall)
2687188417Sthompsa			ieee80211_start_all(ic);
2688184610Salfred		break;
2689184610Salfred	case SIOCGIFMEDIA:
2690188417Sthompsa		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2691184610Salfred		break;
2692188417Sthompsa	case SIOCGIFADDR:
2693184610Salfred		error = ether_ioctl(ifp, cmd, data);
2694184610Salfred		break;
2695188417Sthompsa	default:
2696188417Sthompsa		error = EINVAL;
2697188417Sthompsa		break;
2698184610Salfred	}
2699184610Salfred	return (error);
2700184610Salfred}
2701184610Salfred
2702184610Salfredstatic void
2703191746Sthompsazyd_init_locked(struct zyd_softc *sc)
2704184610Salfred{
2705188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2706188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2707192984Sthompsa	struct usb_config_descriptor *cd;
2708188419Sthompsa	int error;
2709188417Sthompsa	uint32_t val;
2710184610Salfred
2711188417Sthompsa	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2712184610Salfred
2713188417Sthompsa	if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
2714188417Sthompsa		error = zyd_loadfirmware(sc);
2715188417Sthompsa		if (error != 0) {
2716188417Sthompsa			device_printf(sc->sc_dev,
2717188417Sthompsa			    "could not load firmware (error=%d)\n", error);
2718188417Sthompsa			goto fail;
2719188417Sthompsa		}
2720188417Sthompsa
2721188417Sthompsa		/* reset device */
2722194228Sthompsa		cd = usbd_get_config_descriptor(sc->sc_udev);
2723194228Sthompsa		error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
2724188417Sthompsa		    cd->bConfigurationValue);
2725188417Sthompsa		if (error)
2726188417Sthompsa			device_printf(sc->sc_dev, "reset failed, continuing\n");
2727188417Sthompsa
2728188417Sthompsa		error = zyd_hw_init(sc);
2729188417Sthompsa		if (error) {
2730188417Sthompsa			device_printf(sc->sc_dev,
2731188417Sthompsa			    "hardware initialization failed\n");
2732188417Sthompsa			goto fail;
2733188417Sthompsa		}
2734188417Sthompsa
2735188417Sthompsa		device_printf(sc->sc_dev,
2736188417Sthompsa		    "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
2737188417Sthompsa		    "BE%x NP%x Gain%x F%x\n",
2738188417Sthompsa		    (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
2739188417Sthompsa		    sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
2740188417Sthompsa		    zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
2741188417Sthompsa		    sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
2742188417Sthompsa		    sc->sc_cckgain, sc->sc_fix_cr157);
2743188417Sthompsa
2744188417Sthompsa		/* read regulatory domain (currently unused) */
2745188417Sthompsa		zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
2746188417Sthompsa		sc->sc_regdomain = val >> 16;
2747188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
2748188417Sthompsa		    sc->sc_regdomain);
2749188417Sthompsa
2750188417Sthompsa		/* we'll do software WEP decryption for now */
2751188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
2752188417Sthompsa		    __func__);
2753188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2754188417Sthompsa
2755188417Sthompsa		sc->sc_flags |= ZYD_FLAG_INITONCE;
2756184610Salfred	}
2757184610Salfred
2758188417Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2759191746Sthompsa		zyd_stop(sc);
2760188417Sthompsa
2761190526Ssam	DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
2762190526Ssam	    IF_LLADDR(ifp), ":");
2763190526Ssam	error = zyd_set_macaddr(sc, IF_LLADDR(ifp));
2764188417Sthompsa	if (error != 0)
2765184610Salfred		return;
2766184610Salfred
2767188417Sthompsa	/* set basic rates */
2768188417Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B)
2769188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
2770188417Sthompsa	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2771188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
2772188417Sthompsa	else	/* assumes 802.11b/g */
2773188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
2774184610Salfred
2775188417Sthompsa	/* promiscuous mode */
2776188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
2777188417Sthompsa	/* multicast setup */
2778188417Sthompsa	zyd_set_multi(sc);
2779188417Sthompsa	/* set RX filter  */
2780188417Sthompsa	error = zyd_set_rxfilter(sc);
2781188417Sthompsa	if (error != 0)
2782188417Sthompsa		goto fail;
2783184610Salfred
2784188417Sthompsa	/* switch radio transmitter ON */
2785188417Sthompsa	error = zyd_switch_radio(sc, 1);
2786188417Sthompsa	if (error != 0)
2787188417Sthompsa		goto fail;
2788188417Sthompsa	/* set default BSS channel */
2789188417Sthompsa	zyd_set_chan(sc, ic->ic_curchan);
2790184610Salfred
2791188417Sthompsa	/*
2792188417Sthompsa	 * Allocate Tx and Rx xfer queues.
2793188417Sthompsa	 */
2794188419Sthompsa	zyd_setup_tx_list(sc);
2795184610Salfred
2796188417Sthompsa	/* enable interrupts */
2797188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2798184610Salfred
2799188417Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2800188417Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2801194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2802194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2803194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
2804184610Salfred
2805188417Sthompsa	return;
2806184610Salfred
2807191746Sthompsafail:	zyd_stop(sc);
2808188417Sthompsa	return;
2809184610Salfred}
2810184610Salfred
2811184610Salfredstatic void
2812188417Sthompsazyd_init(void *priv)
2813184610Salfred{
2814188417Sthompsa	struct zyd_softc *sc = priv;
2815188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2816188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2817184610Salfred
2818188417Sthompsa	ZYD_LOCK(sc);
2819191746Sthompsa	zyd_init_locked(sc);
2820188417Sthompsa	ZYD_UNLOCK(sc);
2821188417Sthompsa
2822188417Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2823188417Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2824184610Salfred}
2825184610Salfred
2826184610Salfredstatic void
2827191746Sthompsazyd_stop(struct zyd_softc *sc)
2828184610Salfred{
2829188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2830188417Sthompsa	int error;
2831184610Salfred
2832188417Sthompsa	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2833184610Salfred
2834188417Sthompsa	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2835184610Salfred
2836188417Sthompsa	/*
2837188419Sthompsa	 * Drain all the transfers, if not already drained:
2838188417Sthompsa	 */
2839188419Sthompsa	ZYD_UNLOCK(sc);
2840194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2841194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
2842188419Sthompsa	ZYD_LOCK(sc);
2843188417Sthompsa
2844188419Sthompsa	zyd_unsetup_tx_list(sc);
2845188417Sthompsa
2846188419Sthompsa	/* Stop now if the device was never set up */
2847188419Sthompsa	if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
2848184610Salfred		return;
2849184610Salfred
2850188417Sthompsa	/* switch radio transmitter OFF */
2851188417Sthompsa	error = zyd_switch_radio(sc, 0);
2852188417Sthompsa	if (error != 0)
2853188417Sthompsa		goto fail;
2854188417Sthompsa	/* disable Rx */
2855188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
2856188417Sthompsa	/* disable interrupts */
2857188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
2858184610Salfred
2859188417Sthompsafail:
2860188417Sthompsa	return;
2861184610Salfred}
2862184610Salfred
2863188417Sthompsastatic int
2864188417Sthompsazyd_loadfirmware(struct zyd_softc *sc)
2865184610Salfred{
2866192984Sthompsa	struct usb_device_request req;
2867188417Sthompsa	size_t size;
2868188417Sthompsa	u_char *fw;
2869188417Sthompsa	uint8_t stat;
2870188417Sthompsa	uint16_t addr;
2871184610Salfred
2872188417Sthompsa	if (sc->sc_flags & ZYD_FLAG_FWLOADED)
2873188417Sthompsa		return (0);
2874184610Salfred
2875188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211) {
2876188417Sthompsa		fw = (u_char *)zd1211_firmware;
2877188417Sthompsa		size = sizeof(zd1211_firmware);
2878188417Sthompsa	} else {
2879188417Sthompsa		fw = (u_char *)zd1211b_firmware;
2880188417Sthompsa		size = sizeof(zd1211b_firmware);
2881184610Salfred	}
2882184610Salfred
2883188417Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2884188417Sthompsa	req.bRequest = ZYD_DOWNLOADREQ;
2885188417Sthompsa	USETW(req.wIndex, 0);
2886184610Salfred
2887188417Sthompsa	addr = ZYD_FIRMWARE_START_ADDR;
2888188417Sthompsa	while (size > 0) {
2889188417Sthompsa		/*
2890188417Sthompsa		 * When the transfer size is 4096 bytes, it is not
2891188417Sthompsa		 * likely to be able to transfer it.
2892188417Sthompsa		 * The cause is port or machine or chip?
2893188417Sthompsa		 */
2894188417Sthompsa		const int mlen = min(size, 64);
2895184610Salfred
2896188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_FW,
2897188417Sthompsa		    "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
2898184610Salfred
2899188417Sthompsa		USETW(req.wValue, addr);
2900188417Sthompsa		USETW(req.wLength, mlen);
2901188419Sthompsa		if (zyd_do_request(sc, &req, fw) != 0)
2902188417Sthompsa			return (EIO);
2903184610Salfred
2904188417Sthompsa		addr += mlen / 2;
2905188417Sthompsa		fw   += mlen;
2906188417Sthompsa		size -= mlen;
2907184610Salfred	}
2908184610Salfred
2909188417Sthompsa	/* check whether the upload succeeded */
2910188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2911188417Sthompsa	req.bRequest = ZYD_DOWNLOADSTS;
2912188417Sthompsa	USETW(req.wValue, 0);
2913188417Sthompsa	USETW(req.wIndex, 0);
2914188417Sthompsa	USETW(req.wLength, sizeof(stat));
2915188419Sthompsa	if (zyd_do_request(sc, &req, &stat) != 0)
2916188417Sthompsa		return (EIO);
2917184610Salfred
2918188417Sthompsa	sc->sc_flags |= ZYD_FLAG_FWLOADED;
2919184610Salfred
2920188417Sthompsa	return (stat & 0x80) ? (EIO) : (0);
2921184610Salfred}
2922184610Salfred
2923184610Salfredstatic void
2924188417Sthompsazyd_scan_start(struct ieee80211com *ic)
2925184610Salfred{
2926191746Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2927191746Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2928184610Salfred
2929188417Sthompsa	ZYD_LOCK(sc);
2930191746Sthompsa	/* want broadcast address while scanning */
2931191746Sthompsa	zyd_set_bssid(sc, ifp->if_broadcastaddr);
2932188417Sthompsa	ZYD_UNLOCK(sc);
2933184610Salfred}
2934184610Salfred
2935184610Salfredstatic void
2936188417Sthompsazyd_scan_end(struct ieee80211com *ic)
2937184610Salfred{
2938188417Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
2939184610Salfred
2940188417Sthompsa	ZYD_LOCK(sc);
2941191746Sthompsa	/* restore previous bssid */
2942191746Sthompsa	zyd_set_bssid(sc, sc->sc_bssid);
2943188417Sthompsa	ZYD_UNLOCK(sc);
2944184610Salfred}
2945184610Salfred
2946184610Salfredstatic void
2947188417Sthompsazyd_set_channel(struct ieee80211com *ic)
2948184610Salfred{
2949188417Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
2950184610Salfred
2951188417Sthompsa	ZYD_LOCK(sc);
2952191746Sthompsa	zyd_set_chan(sc, ic->ic_curchan);
2953188417Sthompsa	ZYD_UNLOCK(sc);
2954184610Salfred}
2955184610Salfred
2956188417Sthompsastatic device_method_t zyd_methods[] = {
2957188417Sthompsa        /* Device interface */
2958188417Sthompsa        DEVMETHOD(device_probe, zyd_match),
2959188417Sthompsa        DEVMETHOD(device_attach, zyd_attach),
2960188417Sthompsa        DEVMETHOD(device_detach, zyd_detach),
2961246614Shselasky	DEVMETHOD_END
2962188417Sthompsa};
2963184610Salfred
2964188417Sthompsastatic driver_t zyd_driver = {
2965233774Shselasky	.name = "zyd",
2966233774Shselasky	.methods = zyd_methods,
2967233774Shselasky	.size = sizeof(struct zyd_softc)
2968188417Sthompsa};
2969184610Salfred
2970188417Sthompsastatic devclass_t zyd_devclass;
2971184610Salfred
2972189275SthompsaDRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0);
2973188942SthompsaMODULE_DEPEND(zyd, usb, 1, 1, 1);
2974188417SthompsaMODULE_DEPEND(zyd, wlan, 1, 1, 1);
2975212122SthompsaMODULE_VERSION(zyd, 1);
2976