if_zyd.c revision 206358
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 206358 2010-04-07 15:29:13Z rpaulo $	*/
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 206358 2010-04-07 15:29:13Z rpaulo $");
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>
51191746Sthompsa#include <net/if_arp.h>
52191746Sthompsa#include <net/ethernet.h>
53191746Sthompsa#include <net/if_dl.h>
54191746Sthompsa#include <net/if_media.h>
55191746Sthompsa#include <net/if_types.h>
56191746Sthompsa
57191746Sthompsa#ifdef INET
58191746Sthompsa#include <netinet/in.h>
59191746Sthompsa#include <netinet/in_systm.h>
60191746Sthompsa#include <netinet/in_var.h>
61191746Sthompsa#include <netinet/if_ether.h>
62191746Sthompsa#include <netinet/ip.h>
63191746Sthompsa#endif
64191746Sthompsa
65191746Sthompsa#include <net80211/ieee80211_var.h>
66191746Sthompsa#include <net80211/ieee80211_regdomain.h>
67191746Sthompsa#include <net80211/ieee80211_radiotap.h>
68206358Srpaulo#include <net80211/ieee80211_ratectl.h>
69191746Sthompsa
70188942Sthompsa#include <dev/usb/usb.h>
71194677Sthompsa#include <dev/usb/usbdi.h>
72194677Sthompsa#include <dev/usb/usbdi_util.h>
73191746Sthompsa#include "usbdevs.h"
74184610Salfred
75188942Sthompsa#include <dev/usb/wlan/if_zydreg.h>
76188942Sthompsa#include <dev/usb/wlan/if_zydfw.h>
77184610Salfred
78184610Salfred#if USB_DEBUG
79184610Salfredstatic int zyd_debug = 0;
80184610Salfred
81192502SthompsaSYSCTL_NODE(_hw_usb, OID_AUTO, zyd, CTLFLAG_RW, 0, "USB zyd");
82192502SthompsaSYSCTL_INT(_hw_usb_zyd, OID_AUTO, debug, CTLFLAG_RW, &zyd_debug, 0,
83184610Salfred    "zyd debug level");
84188417Sthompsa
85188417Sthompsaenum {
86188417Sthompsa	ZYD_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
87188417Sthompsa	ZYD_DEBUG_RECV		= 0x00000002,	/* basic recv operation */
88188417Sthompsa	ZYD_DEBUG_RESET		= 0x00000004,	/* reset processing */
89188417Sthompsa	ZYD_DEBUG_INIT		= 0x00000008,	/* device init */
90188417Sthompsa	ZYD_DEBUG_TX_PROC	= 0x00000010,	/* tx ISR proc */
91188417Sthompsa	ZYD_DEBUG_RX_PROC	= 0x00000020,	/* rx ISR proc */
92188417Sthompsa	ZYD_DEBUG_STATE		= 0x00000040,	/* 802.11 state transitions */
93188417Sthompsa	ZYD_DEBUG_STAT		= 0x00000080,	/* statistic */
94188417Sthompsa	ZYD_DEBUG_FW		= 0x00000100,	/* firmware */
95188417Sthompsa	ZYD_DEBUG_CMD		= 0x00000200,	/* fw commands */
96188417Sthompsa	ZYD_DEBUG_ANY		= 0xffffffff
97188417Sthompsa};
98188417Sthompsa#define	DPRINTF(sc, m, fmt, ...) do {				\
99188419Sthompsa	if (zyd_debug & (m))					\
100188417Sthompsa		printf("%s: " fmt, __func__, ## __VA_ARGS__);	\
101188417Sthompsa} while (0)
102188417Sthompsa#else
103188417Sthompsa#define	DPRINTF(sc, m, fmt, ...) do {				\
104188417Sthompsa	(void) sc;						\
105188417Sthompsa} while (0)
106184610Salfred#endif
107184610Salfred
108188419Sthompsa#define	zyd_do_request(sc,req,data) \
109194228Sthompsa    usbd_do_request_flags((sc)->sc_udev, &(sc)->sc_mtx, req, data, 0, NULL, 5000)
110188419Sthompsa
111188417Sthompsastatic device_probe_t zyd_match;
112184610Salfredstatic device_attach_t zyd_attach;
113184610Salfredstatic device_detach_t zyd_detach;
114184610Salfred
115193045Sthompsastatic usb_callback_t zyd_intr_read_callback;
116193045Sthompsastatic usb_callback_t zyd_intr_write_callback;
117193045Sthompsastatic usb_callback_t zyd_bulk_read_callback;
118193045Sthompsastatic usb_callback_t zyd_bulk_write_callback;
119184610Salfred
120185948Sthompsastatic struct ieee80211vap *zyd_vap_create(struct ieee80211com *,
121188417Sthompsa		    const char name[IFNAMSIZ], int unit, int opmode,
122188417Sthompsa		    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
123188417Sthompsa		    const uint8_t mac[IEEE80211_ADDR_LEN]);
124185948Sthompsastatic void	zyd_vap_delete(struct ieee80211vap *);
125188417Sthompsastatic void	zyd_tx_free(struct zyd_tx_data *, int);
126188419Sthompsastatic void	zyd_setup_tx_list(struct zyd_softc *);
127188419Sthompsastatic void	zyd_unsetup_tx_list(struct zyd_softc *);
128188417Sthompsastatic int	zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int);
129188417Sthompsastatic int	zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
130188601Sthompsa		    void *, int, int);
131188417Sthompsastatic int	zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
132188417Sthompsastatic int	zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
133188417Sthompsastatic int	zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
134188417Sthompsastatic int	zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
135188417Sthompsastatic int	zyd_rfwrite(struct zyd_softc *, uint32_t);
136188417Sthompsastatic int	zyd_lock_phy(struct zyd_softc *);
137188417Sthompsastatic int	zyd_unlock_phy(struct zyd_softc *);
138188417Sthompsastatic int	zyd_rf_attach(struct zyd_softc *, uint8_t);
139188417Sthompsastatic const char *zyd_rf_name(uint8_t);
140188417Sthompsastatic int	zyd_hw_init(struct zyd_softc *);
141188417Sthompsastatic int	zyd_read_pod(struct zyd_softc *);
142188417Sthompsastatic int	zyd_read_eeprom(struct zyd_softc *);
143188417Sthompsastatic int	zyd_get_macaddr(struct zyd_softc *);
144188417Sthompsastatic int	zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
145188417Sthompsastatic int	zyd_set_bssid(struct zyd_softc *, const uint8_t *);
146188417Sthompsastatic int	zyd_switch_radio(struct zyd_softc *, int);
147188417Sthompsastatic int	zyd_set_led(struct zyd_softc *, int, int);
148188417Sthompsastatic void	zyd_set_multi(struct zyd_softc *);
149188417Sthompsastatic void	zyd_update_mcast(struct ifnet *);
150188417Sthompsastatic int	zyd_set_rxfilter(struct zyd_softc *);
151188417Sthompsastatic void	zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
152188417Sthompsastatic int	zyd_set_beacon_interval(struct zyd_softc *, int);
153192984Sthompsastatic void	zyd_rx_data(struct usb_xfer *, int, uint16_t);
154193803Sweongyostatic int	zyd_tx_start(struct zyd_softc *, struct mbuf *,
155185948Sthompsa		    struct ieee80211_node *);
156188417Sthompsastatic void	zyd_start(struct ifnet *);
157188417Sthompsastatic int	zyd_raw_xmit(struct ieee80211_node *, struct mbuf *,
158185948Sthompsa		    const struct ieee80211_bpf_params *);
159188417Sthompsastatic int	zyd_ioctl(struct ifnet *, u_long, caddr_t);
160191746Sthompsastatic void	zyd_init_locked(struct zyd_softc *);
161188417Sthompsastatic void	zyd_init(void *);
162191746Sthompsastatic void	zyd_stop(struct zyd_softc *);
163188417Sthompsastatic int	zyd_loadfirmware(struct zyd_softc *);
164188417Sthompsastatic void	zyd_scan_start(struct ieee80211com *);
165188417Sthompsastatic void	zyd_scan_end(struct ieee80211com *);
166188417Sthompsastatic void	zyd_set_channel(struct ieee80211com *);
167188417Sthompsastatic int	zyd_rfmd_init(struct zyd_rf *);
168188417Sthompsastatic int	zyd_rfmd_switch_radio(struct zyd_rf *, int);
169188417Sthompsastatic int	zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
170188417Sthompsastatic int	zyd_al2230_init(struct zyd_rf *);
171188417Sthompsastatic int	zyd_al2230_switch_radio(struct zyd_rf *, int);
172188417Sthompsastatic int	zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
173188417Sthompsastatic int	zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t);
174188417Sthompsastatic int	zyd_al2230_init_b(struct zyd_rf *);
175188417Sthompsastatic int	zyd_al7230B_init(struct zyd_rf *);
176188417Sthompsastatic int	zyd_al7230B_switch_radio(struct zyd_rf *, int);
177188417Sthompsastatic int	zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
178188417Sthompsastatic int	zyd_al2210_init(struct zyd_rf *);
179188417Sthompsastatic int	zyd_al2210_switch_radio(struct zyd_rf *, int);
180188417Sthompsastatic int	zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
181188417Sthompsastatic int	zyd_gct_init(struct zyd_rf *);
182188417Sthompsastatic int	zyd_gct_switch_radio(struct zyd_rf *, int);
183188417Sthompsastatic int	zyd_gct_set_channel(struct zyd_rf *, uint8_t);
184193420Sweongyostatic int	zyd_gct_mode(struct zyd_rf *);
185193420Sweongyostatic int	zyd_gct_set_channel_synth(struct zyd_rf *, int, int);
186193420Sweongyostatic int	zyd_gct_write(struct zyd_rf *, uint16_t);
187193420Sweongyostatic int	zyd_gct_txgain(struct zyd_rf *, uint8_t);
188188417Sthompsastatic int	zyd_maxim2_init(struct zyd_rf *);
189188417Sthompsastatic int	zyd_maxim2_switch_radio(struct zyd_rf *, int);
190188417Sthompsastatic int	zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
191184610Salfred
192184610Salfredstatic const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
193184610Salfredstatic const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
194184610Salfred
195184610Salfred/* various supported device vendors/products */
196188417Sthompsa#define ZYD_ZD1211	0
197188417Sthompsa#define ZYD_ZD1211B	1
198184610Salfred
199193419Sweongyo#define	ZYD_ZD1211_DEV(v,p)	\
200193419Sweongyo	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211) }
201193419Sweongyo#define	ZYD_ZD1211B_DEV(v,p)	\
202193419Sweongyo	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211B) }
203192984Sthompsastatic const struct usb_device_id zyd_devs[] = {
204193419Sweongyo	/* ZYD_ZD1211 */
205193419Sweongyo	ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
206193419Sweongyo	ZYD_ZD1211_DEV(ABOCOM, WL54),
207193419Sweongyo	ZYD_ZD1211_DEV(ASUS, WL159G),
208193419Sweongyo	ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
209193419Sweongyo	ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
210193419Sweongyo	ZYD_ZD1211_DEV(PLANEX2, GWUS54GD),
211193419Sweongyo	ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
212193419Sweongyo	ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
213193419Sweongyo	ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
214193419Sweongyo	ZYD_ZD1211_DEV(SAGEM, XG760A),
215193419Sweongyo	ZYD_ZD1211_DEV(SENAO, NUB8301),
216193419Sweongyo	ZYD_ZD1211_DEV(SITECOMEU, WL113),
217193419Sweongyo	ZYD_ZD1211_DEV(SWEEX, ZD1211),
218193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
219193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
220193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
221193419Sweongyo	ZYD_ZD1211_DEV(TWINMOS, G240),
222193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
223193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
224193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
225193419Sweongyo	ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
226193419Sweongyo	ZYD_ZD1211_DEV(ZCOM, ZD1211),
227193419Sweongyo	ZYD_ZD1211_DEV(ZYDAS, ZD1211),
228193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, AG225H),
229193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
230193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, G200V2),
231193419Sweongyo	/* ZYD_ZD1211B */
232193419Sweongyo	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
233193419Sweongyo	ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
234193419Sweongyo	ZYD_ZD1211B_DEV(ASUS, A9T_WIFI),
235193419Sweongyo	ZYD_ZD1211B_DEV(BELKIN, F5D7050_V4000),
236193419Sweongyo	ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
237193419Sweongyo	ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
238193419Sweongyo	ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
239193419Sweongyo	ZYD_ZD1211B_DEV(MELCO, KG54L),
240193419Sweongyo	ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
241193419Sweongyo	ZYD_ZD1211B_DEV(PLANEX2, GW_US54GXS),
242193419Sweongyo	ZYD_ZD1211B_DEV(SAGEM, XG76NA),
243193419Sweongyo	ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
244193419Sweongyo	ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
245193419Sweongyo	ZYD_ZD1211B_DEV(USR, USR5423),
246193419Sweongyo	ZYD_ZD1211B_DEV(VTECH, ZD1211B),
247193419Sweongyo	ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
248193419Sweongyo	ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
249193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, M202),
250193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, G202),
251193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, G220V2)
252184610Salfred};
253184610Salfred
254192984Sthompsastatic const struct usb_config zyd_config[ZYD_N_TRANSFER] = {
255188417Sthompsa	[ZYD_BULK_WR] = {
256184610Salfred		.type = UE_BULK,
257184610Salfred		.endpoint = UE_ADDR_ANY,
258184610Salfred		.direction = UE_DIR_OUT,
259190734Sthompsa		.bufsize = ZYD_MAX_TXBUFSZ,
260190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
261190734Sthompsa		.callback = zyd_bulk_write_callback,
262184610Salfred		.ep_index = 0,
263190734Sthompsa		.timeout = 10000,	/* 10 seconds */
264184610Salfred	},
265188417Sthompsa	[ZYD_BULK_RD] = {
266184610Salfred		.type = UE_BULK,
267184610Salfred		.endpoint = UE_ADDR_ANY,
268184610Salfred		.direction = UE_DIR_IN,
269190734Sthompsa		.bufsize = ZYX_MAX_RXBUFSZ,
270190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
271190734Sthompsa		.callback = zyd_bulk_read_callback,
272184610Salfred		.ep_index = 0,
273184610Salfred	},
274188417Sthompsa	[ZYD_INTR_WR] = {
275184610Salfred		.type = UE_BULK_INTR,
276184610Salfred		.endpoint = UE_ADDR_ANY,
277184610Salfred		.direction = UE_DIR_OUT,
278190734Sthompsa		.bufsize = sizeof(struct zyd_cmd),
279190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
280190734Sthompsa		.callback = zyd_intr_write_callback,
281190734Sthompsa		.timeout = 1000,	/* 1 second */
282184610Salfred		.ep_index = 1,
283184610Salfred	},
284188417Sthompsa	[ZYD_INTR_RD] = {
285188417Sthompsa		.type = UE_INTERRUPT,
286184610Salfred		.endpoint = UE_ADDR_ANY,
287184610Salfred		.direction = UE_DIR_IN,
288190734Sthompsa		.bufsize = sizeof(struct zyd_cmd),
289190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
290190734Sthompsa		.callback = zyd_intr_read_callback,
291184610Salfred	},
292184610Salfred};
293188417Sthompsa#define zyd_read16_m(sc, val, data)	do {				\
294188417Sthompsa	error = zyd_read16(sc, val, data);				\
295188417Sthompsa	if (error != 0)							\
296188417Sthompsa		goto fail;						\
297188417Sthompsa} while (0)
298188417Sthompsa#define zyd_write16_m(sc, val, data)	do {				\
299188417Sthompsa	error = zyd_write16(sc, val, data);				\
300188417Sthompsa	if (error != 0)							\
301188417Sthompsa		goto fail;						\
302188417Sthompsa} while (0)
303188417Sthompsa#define zyd_read32_m(sc, val, data)	do {				\
304188417Sthompsa	error = zyd_read32(sc, val, data);				\
305188417Sthompsa	if (error != 0)							\
306188417Sthompsa		goto fail;						\
307188417Sthompsa} while (0)
308188417Sthompsa#define zyd_write32_m(sc, val, data)	do {				\
309188417Sthompsa	error = zyd_write32(sc, val, data);				\
310188417Sthompsa	if (error != 0)							\
311188417Sthompsa		goto fail;						\
312188417Sthompsa} while (0)
313184610Salfred
314188417Sthompsastatic int
315188417Sthompsazyd_match(device_t dev)
316184610Salfred{
317192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
318184610Salfred
319192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
320188417Sthompsa		return (ENXIO);
321188419Sthompsa	if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX)
322188417Sthompsa		return (ENXIO);
323188417Sthompsa	if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX)
324188417Sthompsa		return (ENXIO);
325184610Salfred
326194228Sthompsa	return (usbd_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa));
327184610Salfred}
328184610Salfred
329188417Sthompsastatic int
330188417Sthompsazyd_attach(device_t dev)
331184610Salfred{
332192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
333188417Sthompsa	struct zyd_softc *sc = device_get_softc(dev);
334191746Sthompsa	struct ifnet *ifp;
335191746Sthompsa	struct ieee80211com *ic;
336191746Sthompsa	uint8_t iface_index, bands;
337188419Sthompsa	int error;
338184610Salfred
339188417Sthompsa	if (uaa->info.bcdDevice < 0x4330) {
340188417Sthompsa		device_printf(dev, "device version mismatch: 0x%X "
341188417Sthompsa		    "(only >= 43.30 supported)\n",
342188417Sthompsa		    uaa->info.bcdDevice);
343188417Sthompsa		return (EINVAL);
344184610Salfred	}
345184610Salfred
346194228Sthompsa	device_set_usb_desc(dev);
347188417Sthompsa	sc->sc_dev = dev;
348188417Sthompsa	sc->sc_udev = uaa->device;
349188417Sthompsa	sc->sc_macrev = USB_GET_DRIVER_INFO(uaa);
350188419Sthompsa
351188417Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
352188417Sthompsa	    MTX_NETWORK_LOCK, MTX_DEF);
353188417Sthompsa	STAILQ_INIT(&sc->sc_rqh);
354184610Salfred
355188417Sthompsa	iface_index = ZYD_IFACE_INDEX;
356194228Sthompsa	error = usbd_transfer_setup(uaa->device,
357188417Sthompsa	    &iface_index, sc->sc_xfer, zyd_config,
358188417Sthompsa	    ZYD_N_TRANSFER, sc, &sc->sc_mtx);
359188417Sthompsa	if (error) {
360188417Sthompsa		device_printf(dev, "could not allocate USB transfers, "
361194228Sthompsa		    "err=%s\n", usbd_errstr(error));
362188419Sthompsa		goto detach;
363184610Salfred	}
364184610Salfred
365188419Sthompsa	ZYD_LOCK(sc);
366188417Sthompsa	if ((error = zyd_get_macaddr(sc)) != 0) {
367188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
368191746Sthompsa		ZYD_UNLOCK(sc);
369191746Sthompsa		goto detach;
370188417Sthompsa	}
371188419Sthompsa	ZYD_UNLOCK(sc);
372188419Sthompsa
373188417Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
374188417Sthompsa	if (ifp == NULL) {
375188419Sthompsa		device_printf(sc->sc_dev, "can not if_alloc()\n");
376191746Sthompsa		goto detach;
377184610Salfred	}
378188417Sthompsa	ifp->if_softc = sc;
379188417Sthompsa	if_initname(ifp, "zyd", device_get_unit(sc->sc_dev));
380188417Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
381188417Sthompsa	ifp->if_init = zyd_init;
382188417Sthompsa	ifp->if_ioctl = zyd_ioctl;
383188417Sthompsa	ifp->if_start = zyd_start;
384188417Sthompsa	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
385188417Sthompsa	IFQ_SET_READY(&ifp->if_snd);
386184610Salfred
387188417Sthompsa	ic = ifp->if_l2com;
388188417Sthompsa	ic->ic_ifp = ifp;
389188417Sthompsa	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
390188417Sthompsa	ic->ic_opmode = IEEE80211_M_STA;
391184610Salfred
392188417Sthompsa	/* set device capabilities */
393188417Sthompsa	ic->ic_caps =
394188417Sthompsa		  IEEE80211_C_STA		/* station mode */
395188417Sthompsa		| IEEE80211_C_MONITOR		/* monitor mode */
396188417Sthompsa		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
397188417Sthompsa	        | IEEE80211_C_SHSLOT		/* short slot time supported */
398188417Sthompsa		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
399188417Sthompsa	        | IEEE80211_C_WPA		/* 802.11i */
400188417Sthompsa		;
401184610Salfred
402188417Sthompsa	bands = 0;
403188417Sthompsa	setbit(&bands, IEEE80211_MODE_11B);
404188417Sthompsa	setbit(&bands, IEEE80211_MODE_11G);
405188417Sthompsa	ieee80211_init_channels(ic, NULL, &bands);
406184610Salfred
407190526Ssam	ieee80211_ifattach(ic, sc->sc_bssid);
408188417Sthompsa	ic->ic_raw_xmit = zyd_raw_xmit;
409188417Sthompsa	ic->ic_scan_start = zyd_scan_start;
410188417Sthompsa	ic->ic_scan_end = zyd_scan_end;
411188417Sthompsa	ic->ic_set_channel = zyd_set_channel;
412184610Salfred
413188417Sthompsa	ic->ic_vap_create = zyd_vap_create;
414188417Sthompsa	ic->ic_vap_delete = zyd_vap_delete;
415188417Sthompsa	ic->ic_update_mcast = zyd_update_mcast;
416188601Sthompsa	ic->ic_update_promisc = zyd_update_mcast;
417184610Salfred
418192468Ssam	ieee80211_radiotap_attach(ic,
419192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
420192468Ssam		ZYD_TX_RADIOTAP_PRESENT,
421192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
422192468Ssam		ZYD_RX_RADIOTAP_PRESENT);
423184610Salfred
424188417Sthompsa	if (bootverbose)
425188417Sthompsa		ieee80211_announce(ic);
426184610Salfred
427191746Sthompsa	return (0);
428191746Sthompsa
429191746Sthompsadetach:
430191746Sthompsa	zyd_detach(dev);
431191746Sthompsa	return (ENXIO);			/* failure */
432184610Salfred}
433184610Salfred
434188417Sthompsastatic int
435188417Sthompsazyd_detach(device_t dev)
436184610Salfred{
437188417Sthompsa	struct zyd_softc *sc = device_get_softc(dev);
438188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
439188601Sthompsa	struct ieee80211com *ic;
440184610Salfred
441188419Sthompsa	/* stop all USB transfers */
442194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER);
443184610Salfred
444188419Sthompsa	/* free TX list, if any */
445188419Sthompsa	zyd_unsetup_tx_list(sc);
446188419Sthompsa
447188417Sthompsa	if (ifp) {
448188601Sthompsa		ic = ifp->if_l2com;
449188417Sthompsa		ieee80211_ifdetach(ic);
450188417Sthompsa		if_free(ifp);
451184610Salfred	}
452188417Sthompsa	mtx_destroy(&sc->sc_mtx);
453184610Salfred
454188417Sthompsa	return (0);
455184610Salfred}
456184610Salfred
457188417Sthompsastatic struct ieee80211vap *
458188417Sthompsazyd_vap_create(struct ieee80211com *ic,
459188417Sthompsa	const char name[IFNAMSIZ], int unit, int opmode, int flags,
460188417Sthompsa	const uint8_t bssid[IEEE80211_ADDR_LEN],
461188417Sthompsa	const uint8_t mac[IEEE80211_ADDR_LEN])
462184610Salfred{
463188417Sthompsa	struct zyd_vap *zvp;
464188417Sthompsa	struct ieee80211vap *vap;
465184610Salfred
466188417Sthompsa	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
467188417Sthompsa		return (NULL);
468188417Sthompsa	zvp = (struct zyd_vap *) malloc(sizeof(struct zyd_vap),
469188417Sthompsa	    M_80211_VAP, M_NOWAIT | M_ZERO);
470188417Sthompsa	if (zvp == NULL)
471188417Sthompsa		return (NULL);
472188417Sthompsa	vap = &zvp->vap;
473188417Sthompsa	/* enable s/w bmiss handling for sta mode */
474188417Sthompsa	ieee80211_vap_setup(ic, vap, name, unit, opmode,
475188417Sthompsa	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
476184610Salfred
477188417Sthompsa	/* override state transition machine */
478188417Sthompsa	zvp->newstate = vap->iv_newstate;
479188417Sthompsa	vap->iv_newstate = zyd_newstate;
480184610Salfred
481206358Srpaulo	ieee80211_ratectl_init(vap);
482206358Srpaulo	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
483184610Salfred
484188417Sthompsa	/* complete setup */
485188417Sthompsa	ieee80211_vap_attach(vap, ieee80211_media_change,
486188417Sthompsa	    ieee80211_media_status);
487188417Sthompsa	ic->ic_opmode = opmode;
488188417Sthompsa	return (vap);
489184610Salfred}
490184610Salfred
491184610Salfredstatic void
492188417Sthompsazyd_vap_delete(struct ieee80211vap *vap)
493184610Salfred{
494188417Sthompsa	struct zyd_vap *zvp = ZYD_VAP(vap);
495184610Salfred
496206358Srpaulo	ieee80211_ratectl_deinit(vap);
497188417Sthompsa	ieee80211_vap_detach(vap);
498188417Sthompsa	free(zvp, M_80211_VAP);
499184610Salfred}
500184610Salfred
501184610Salfredstatic void
502188417Sthompsazyd_tx_free(struct zyd_tx_data *data, int txerr)
503184610Salfred{
504188417Sthompsa	struct zyd_softc *sc = data->sc;
505184610Salfred
506188417Sthompsa	if (data->m != NULL) {
507188417Sthompsa		if (data->m->m_flags & M_TXCB)
508188417Sthompsa			ieee80211_process_callback(data->ni, data->m,
509188417Sthompsa			    txerr ? ETIMEDOUT : 0);
510188417Sthompsa		m_freem(data->m);
511188417Sthompsa		data->m = NULL;
512188417Sthompsa
513194939Sweongyo		if (txerr == 0)
514206358Srpaulo			ieee80211_ratectl_tx_complete(data->ni->ni_vap,
515206358Srpaulo			    data->ni, IEEE80211_RATECTL_TX_SUCCESS, NULL, NULL);
516188417Sthompsa		ieee80211_free_node(data->ni);
517188417Sthompsa		data->ni = NULL;
518184610Salfred	}
519188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
520188417Sthompsa	sc->tx_nfree++;
521184610Salfred}
522184610Salfred
523188419Sthompsastatic void
524188419Sthompsazyd_setup_tx_list(struct zyd_softc *sc)
525184610Salfred{
526188417Sthompsa	struct zyd_tx_data *data;
527188417Sthompsa	int i;
528184610Salfred
529188417Sthompsa	sc->tx_nfree = 0;
530188417Sthompsa	STAILQ_INIT(&sc->tx_q);
531188417Sthompsa	STAILQ_INIT(&sc->tx_free);
532184610Salfred
533188417Sthompsa	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
534188417Sthompsa		data = &sc->tx_data[i];
535184610Salfred
536188417Sthompsa		data->sc = sc;
537188417Sthompsa		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
538188417Sthompsa		sc->tx_nfree++;
539188417Sthompsa	}
540184610Salfred}
541184610Salfred
542184610Salfredstatic void
543188419Sthompsazyd_unsetup_tx_list(struct zyd_softc *sc)
544184610Salfred{
545188417Sthompsa	struct zyd_tx_data *data;
546188417Sthompsa	int i;
547184610Salfred
548188419Sthompsa	/* make sure any subsequent use of the queues will fail */
549188419Sthompsa	sc->tx_nfree = 0;
550188419Sthompsa	STAILQ_INIT(&sc->tx_q);
551188419Sthompsa	STAILQ_INIT(&sc->tx_free);
552184610Salfred
553188419Sthompsa	/* free up all node references and mbufs */
554188417Sthompsa	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
555188417Sthompsa		data = &sc->tx_data[i];
556184610Salfred
557188417Sthompsa		if (data->m != NULL) {
558188417Sthompsa			m_freem(data->m);
559188417Sthompsa			data->m = NULL;
560188417Sthompsa		}
561188417Sthompsa		if (data->ni != NULL) {
562188417Sthompsa			ieee80211_free_node(data->ni);
563188417Sthompsa			data->ni = NULL;
564188417Sthompsa		}
565188417Sthompsa	}
566184610Salfred}
567184610Salfred
568191746Sthompsastatic int
569191746Sthompsazyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
570186730Salfred{
571188417Sthompsa	struct zyd_vap *zvp = ZYD_VAP(vap);
572191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
573191746Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
574191746Sthompsa	struct ieee80211_node *ni;
575188417Sthompsa	int error;
576186730Salfred
577191746Sthompsa	DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__,
578191746Sthompsa	    ieee80211_state_name[vap->iv_state],
579191746Sthompsa	    ieee80211_state_name[nstate]);
580191746Sthompsa
581191746Sthompsa	IEEE80211_UNLOCK(ic);
582191746Sthompsa	ZYD_LOCK(sc);
583191746Sthompsa	switch (nstate) {
584188417Sthompsa	case IEEE80211_S_AUTH:
585188417Sthompsa		zyd_set_chan(sc, ic->ic_curchan);
586188417Sthompsa		break;
587188417Sthompsa	case IEEE80211_S_RUN:
588191746Sthompsa		ni = vap->iv_bss;
589188417Sthompsa		if (vap->iv_opmode == IEEE80211_M_MONITOR)
590188417Sthompsa			break;
591184610Salfred
592188417Sthompsa		/* turn link LED on */
593188417Sthompsa		error = zyd_set_led(sc, ZYD_LED1, 1);
594188417Sthompsa		if (error != 0)
595191746Sthompsa			break;
596191746Sthompsa
597188417Sthompsa		/* make data LED blink upon Tx */
598188417Sthompsa		zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1);
599191746Sthompsa
600188417Sthompsa		IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
601188417Sthompsa		zyd_set_bssid(sc, sc->sc_bssid);
602188417Sthompsa		break;
603188417Sthompsa	default:
604188417Sthompsa		break;
605184610Salfred	}
606188417Sthompsafail:
607188417Sthompsa	ZYD_UNLOCK(sc);
608188417Sthompsa	IEEE80211_LOCK(ic);
609191746Sthompsa	return (zvp->newstate(vap, nstate, arg));
610184610Salfred}
611184610Salfred
612188417Sthompsa/*
613188417Sthompsa * Callback handler for interrupt transfer
614188417Sthompsa */
615184610Salfredstatic void
616194677Sthompsazyd_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
617184610Salfred{
618194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
619184610Salfred	struct ifnet *ifp = sc->sc_ifp;
620184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
621188417Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
622184610Salfred	struct ieee80211_node *ni;
623188417Sthompsa	struct zyd_cmd *cmd = &sc->sc_ibuf;
624194677Sthompsa	struct usb_page_cache *pc;
625188417Sthompsa	int datalen;
626194677Sthompsa	int actlen;
627184610Salfred
628194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
629194677Sthompsa
630184610Salfred	switch (USB_GET_STATE(xfer)) {
631184610Salfred	case USB_ST_TRANSFERRED:
632194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
633194677Sthompsa		usbd_copy_out(pc, 0, cmd, sizeof(*cmd));
634184610Salfred
635188417Sthompsa		switch (le16toh(cmd->code)) {
636188417Sthompsa		case ZYD_NOTIF_RETRYSTATUS:
637188417Sthompsa		{
638188417Sthompsa			struct zyd_notif_retry *retry =
639188417Sthompsa			    (struct zyd_notif_retry *)cmd->data;
640184610Salfred
641188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_TX_PROC,
642188417Sthompsa			    "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
643188417Sthompsa			    le16toh(retry->rate), ether_sprintf(retry->macaddr),
644188417Sthompsa			    le16toh(retry->count)&0xff, le16toh(retry->count));
645184610Salfred
646188417Sthompsa			/*
647188417Sthompsa			 * Find the node to which the packet was sent and
648188417Sthompsa			 * update its retry statistics.  In BSS mode, this node
649188417Sthompsa			 * is the AP we're associated to so no lookup is
650188417Sthompsa			 * actually needed.
651188417Sthompsa			 */
652188417Sthompsa			ni = ieee80211_find_txnode(vap, retry->macaddr);
653188417Sthompsa			if (ni != NULL) {
654206358Srpaulo				int retrycnt =
655206358Srpaulo				    (int)(le16toh(retry->count) & 0xff);
656206358Srpaulo
657206358Srpaulo				ieee80211_ratectl_tx_complete(vap, ni,
658206358Srpaulo				    IEEE80211_RATECTL_TX_FAILURE,
659206358Srpaulo				    &retrycnt, NULL);
660188417Sthompsa				ieee80211_free_node(ni);
661188417Sthompsa			}
662188417Sthompsa			if (le16toh(retry->count) & 0x100)
663188417Sthompsa				ifp->if_oerrors++;	/* too many retries */
664188417Sthompsa			break;
665188417Sthompsa		}
666188417Sthompsa		case ZYD_NOTIF_IORD:
667188417Sthompsa		{
668188417Sthompsa			struct zyd_rq *rqp;
669184610Salfred
670188417Sthompsa			if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
671188417Sthompsa				break;	/* HMAC interrupt */
672184610Salfred
673194677Sthompsa			datalen = actlen - sizeof(cmd->code);
674188417Sthompsa			datalen -= 2;	/* XXX: padding? */
675184610Salfred
676188417Sthompsa			STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
677188417Sthompsa				int i, cnt;
678184610Salfred
679188417Sthompsa				if (rqp->olen != datalen)
680188417Sthompsa					continue;
681188417Sthompsa				cnt = rqp->olen / sizeof(struct zyd_pair);
682188417Sthompsa				for (i = 0; i < cnt; i++) {
683188417Sthompsa					if (*(((const uint16_t *)rqp->idata) + i) !=
684188417Sthompsa					    (((struct zyd_pair *)cmd->data) + i)->reg)
685188417Sthompsa						break;
686184610Salfred				}
687188417Sthompsa				if (i != cnt)
688188417Sthompsa					continue;
689188417Sthompsa				/* copy answer into caller-supplied buffer */
690188417Sthompsa				bcopy(cmd->data, rqp->odata, rqp->olen);
691188417Sthompsa				DPRINTF(sc, ZYD_DEBUG_CMD,
692188417Sthompsa				    "command %p complete, data = %*D \n",
693188417Sthompsa				    rqp, rqp->olen, rqp->odata, ":");
694188417Sthompsa				wakeup(rqp);	/* wakeup caller */
695188417Sthompsa				break;
696184610Salfred			}
697188417Sthompsa			if (rqp == NULL) {
698188417Sthompsa				device_printf(sc->sc_dev,
699188417Sthompsa				    "unexpected IORD notification %*D\n",
700188417Sthompsa				    datalen, cmd->data, ":");
701188417Sthompsa			}
702188417Sthompsa			break;
703184610Salfred		}
704188417Sthompsa		default:
705188417Sthompsa			device_printf(sc->sc_dev, "unknown notification %x\n",
706188417Sthompsa			    le16toh(cmd->code));
707188417Sthompsa		}
708184610Salfred
709188417Sthompsa		/* FALLTHROUGH */
710184610Salfred	case USB_ST_SETUP:
711184610Salfredtr_setup:
712194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
713194228Sthompsa		usbd_transfer_submit(xfer);
714188417Sthompsa		break;
715184610Salfred
716188417Sthompsa	default:			/* Error */
717188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
718194677Sthompsa		    usbd_errstr(error));
719188417Sthompsa
720194677Sthompsa		if (error != USB_ERR_CANCELLED) {
721188417Sthompsa			/* try to clear stall first */
722194677Sthompsa			usbd_xfer_set_stall(xfer);
723188417Sthompsa			goto tr_setup;
724184610Salfred		}
725188417Sthompsa		break;
726188417Sthompsa	}
727188417Sthompsa}
728184610Salfred
729188417Sthompsastatic void
730194677Sthompsazyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
731188417Sthompsa{
732194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
733194677Sthompsa	struct zyd_rq *rqp, *cmd;
734194677Sthompsa	struct usb_page_cache *pc;
735184610Salfred
736188417Sthompsa	switch (USB_GET_STATE(xfer)) {
737188417Sthompsa	case USB_ST_TRANSFERRED:
738194677Sthompsa		cmd = usbd_xfer_get_priv(xfer);
739194677Sthompsa		DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd);
740189452Sthompsa		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
741189452Sthompsa			/* Ensure the cached rq pointer is still valid */
742194677Sthompsa			if (rqp == cmd &&
743189452Sthompsa			    (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
744189452Sthompsa				wakeup(rqp);	/* wakeup caller */
745189452Sthompsa		}
746184610Salfred
747188417Sthompsa		/* FALLTHROUGH */
748188417Sthompsa	case USB_ST_SETUP:
749188417Sthompsatr_setup:
750188417Sthompsa		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
751188417Sthompsa			if (rqp->flags & ZYD_CMD_FLAG_SENT)
752188417Sthompsa				continue;
753184610Salfred
754194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
755194677Sthompsa			usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen);
756184610Salfred
757194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, rqp->ilen);
758194677Sthompsa			usbd_xfer_set_priv(xfer, rqp);
759188417Sthompsa			rqp->flags |= ZYD_CMD_FLAG_SENT;
760194228Sthompsa			usbd_transfer_submit(xfer);
761188417Sthompsa			break;
762184610Salfred		}
763184610Salfred		break;
764184610Salfred
765184610Salfred	default:			/* Error */
766188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
767194677Sthompsa		    usbd_errstr(error));
768184610Salfred
769194677Sthompsa		if (error != USB_ERR_CANCELLED) {
770184610Salfred			/* try to clear stall first */
771194677Sthompsa			usbd_xfer_set_stall(xfer);
772188417Sthompsa			goto tr_setup;
773184610Salfred		}
774184610Salfred		break;
775184610Salfred	}
776184610Salfred}
777184610Salfred
778188417Sthompsastatic int
779188417Sthompsazyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
780188601Sthompsa    void *odata, int olen, int flags)
781184610Salfred{
782188417Sthompsa	struct zyd_cmd cmd;
783188417Sthompsa	struct zyd_rq rq;
784188417Sthompsa	int error;
785184610Salfred
786188417Sthompsa	if (ilen > sizeof(cmd.data))
787188417Sthompsa		return (EINVAL);
788188419Sthompsa
789188417Sthompsa	cmd.code = htole16(code);
790188417Sthompsa	bcopy(idata, cmd.data, ilen);
791188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
792188417Sthompsa	    &rq, ilen, idata, ":");
793184610Salfred
794188417Sthompsa	rq.cmd = &cmd;
795188417Sthompsa	rq.idata = idata;
796188417Sthompsa	rq.odata = odata;
797188417Sthompsa	rq.ilen = sizeof(uint16_t) + ilen;
798188417Sthompsa	rq.olen = olen;
799188417Sthompsa	rq.flags = flags;
800188417Sthompsa	STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
801194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
802194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
803184610Salfred
804188417Sthompsa	/* wait at most one second for command reply */
805188417Sthompsa	error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
806188417Sthompsa	if (error)
807188417Sthompsa		device_printf(sc->sc_dev, "command timeout\n");
808188417Sthompsa	STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
809188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
810188417Sthompsa	    &rq, error);
811184610Salfred
812188417Sthompsa	return (error);
813184610Salfred}
814184610Salfred
815184610Salfredstatic int
816188417Sthompsazyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
817184610Salfred{
818188417Sthompsa	struct zyd_pair tmp;
819188417Sthompsa	int error;
820184610Salfred
821188417Sthompsa	reg = htole16(reg);
822188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
823188417Sthompsa	    ZYD_CMD_FLAG_READ);
824188417Sthompsa	if (error == 0)
825188417Sthompsa		*val = le16toh(tmp.val);
826188417Sthompsa	return (error);
827184610Salfred}
828184610Salfred
829184610Salfredstatic int
830188417Sthompsazyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
831184610Salfred{
832188417Sthompsa	struct zyd_pair tmp[2];
833188417Sthompsa	uint16_t regs[2];
834184610Salfred	int error;
835184610Salfred
836188417Sthompsa	regs[0] = htole16(ZYD_REG32_HI(reg));
837188417Sthompsa	regs[1] = htole16(ZYD_REG32_LO(reg));
838188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
839188417Sthompsa	    ZYD_CMD_FLAG_READ);
840188417Sthompsa	if (error == 0)
841188417Sthompsa		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
842188417Sthompsa	return (error);
843188417Sthompsa}
844184610Salfred
845188417Sthompsastatic int
846188417Sthompsazyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
847188417Sthompsa{
848188417Sthompsa	struct zyd_pair pair;
849184610Salfred
850188417Sthompsa	pair.reg = htole16(reg);
851188417Sthompsa	pair.val = htole16(val);
852184610Salfred
853188417Sthompsa	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
854188417Sthompsa}
855184610Salfred
856188417Sthompsastatic int
857188417Sthompsazyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
858188417Sthompsa{
859188417Sthompsa	struct zyd_pair pair[2];
860184610Salfred
861188417Sthompsa	pair[0].reg = htole16(ZYD_REG32_HI(reg));
862188417Sthompsa	pair[0].val = htole16(val >> 16);
863188417Sthompsa	pair[1].reg = htole16(ZYD_REG32_LO(reg));
864188417Sthompsa	pair[1].val = htole16(val & 0xffff);
865184610Salfred
866188417Sthompsa	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
867184610Salfred}
868184610Salfred
869188417Sthompsastatic int
870188417Sthompsazyd_rfwrite(struct zyd_softc *sc, uint32_t val)
871184610Salfred{
872188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
873188417Sthompsa	struct zyd_rfwrite_cmd req;
874188417Sthompsa	uint16_t cr203;
875188417Sthompsa	int error, i;
876184610Salfred
877188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &cr203);
878188417Sthompsa	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
879188417Sthompsa
880188417Sthompsa	req.code  = htole16(2);
881188417Sthompsa	req.width = htole16(rf->width);
882188417Sthompsa	for (i = 0; i < rf->width; i++) {
883188417Sthompsa		req.bit[i] = htole16(cr203);
884188417Sthompsa		if (val & (1 << (rf->width - 1 - i)))
885188417Sthompsa			req.bit[i] |= htole16(ZYD_RF_DATA);
886188417Sthompsa	}
887188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
888188417Sthompsafail:
889188417Sthompsa	return (error);
890184610Salfred}
891184610Salfred
892188417Sthompsastatic int
893188417Sthompsazyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
894184610Salfred{
895188417Sthompsa	int error;
896184610Salfred
897188417Sthompsa	zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
898188417Sthompsa	zyd_write16_m(sc, ZYD_CR243, (val >>  8) & 0xff);
899188417Sthompsa	zyd_write16_m(sc, ZYD_CR242, (val >>  0) & 0xff);
900188417Sthompsafail:
901188417Sthompsa	return (error);
902184610Salfred}
903184610Salfred
904188417Sthompsastatic int
905188417Sthompsazyd_lock_phy(struct zyd_softc *sc)
906184610Salfred{
907188417Sthompsa	int error;
908188417Sthompsa	uint32_t tmp;
909186730Salfred
910188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
911188417Sthompsa	tmp &= ~ZYD_UNLOCK_PHY_REGS;
912188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
913188417Sthompsafail:
914188417Sthompsa	return (error);
915184610Salfred}
916184610Salfred
917188417Sthompsastatic int
918188417Sthompsazyd_unlock_phy(struct zyd_softc *sc)
919184610Salfred{
920188417Sthompsa	int error;
921188417Sthompsa	uint32_t tmp;
922184610Salfred
923188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
924188417Sthompsa	tmp |= ZYD_UNLOCK_PHY_REGS;
925188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
926188417Sthompsafail:
927188417Sthompsa	return (error);
928184610Salfred}
929184610Salfred
930184610Salfred/*
931188417Sthompsa * RFMD RF methods.
932184610Salfred */
933188417Sthompsastatic int
934188417Sthompsazyd_rfmd_init(struct zyd_rf *rf)
935184610Salfred{
936188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
937188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
938184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
939184610Salfred	static const uint32_t rfini[] = ZYD_RFMD_RF;
940188417Sthompsa	int i, error;
941184610Salfred
942184610Salfred	/* init RF-dependent PHY registers */
943188417Sthompsa	for (i = 0; i < N(phyini); i++) {
944188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
945184610Salfred	}
946184610Salfred
947184610Salfred	/* init RFMD radio */
948188417Sthompsa	for (i = 0; i < N(rfini); i++) {
949188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
950188417Sthompsa			return (error);
951184610Salfred	}
952188417Sthompsafail:
953188417Sthompsa	return (error);
954188417Sthompsa#undef N
955184610Salfred}
956184610Salfred
957188417Sthompsastatic int
958188417Sthompsazyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
959184610Salfred{
960188417Sthompsa	int error;
961188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
962188417Sthompsa
963188417Sthompsa	zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
964188417Sthompsa	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
965188417Sthompsafail:
966188417Sthompsa	return (error);
967184610Salfred}
968184610Salfred
969188417Sthompsastatic int
970188417Sthompsazyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
971184610Salfred{
972188417Sthompsa	int error;
973188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
974184610Salfred	static const struct {
975188417Sthompsa		uint32_t	r1, r2;
976188417Sthompsa	} rfprog[] = ZYD_RFMD_CHANTABLE;
977184610Salfred
978188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
979188417Sthompsa	if (error != 0)
980188417Sthompsa		goto fail;
981188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
982188417Sthompsa	if (error != 0)
983188417Sthompsa		goto fail;
984184610Salfred
985188417Sthompsafail:
986188417Sthompsa	return (error);
987184610Salfred}
988184610Salfred
989184610Salfred/*
990188417Sthompsa * AL2230 RF methods.
991184610Salfred */
992188417Sthompsastatic int
993188417Sthompsazyd_al2230_init(struct zyd_rf *rf)
994184610Salfred{
995188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
996188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
997184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
998186730Salfred	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
999186730Salfred	static const struct zyd_phy_pair phypll[] = {
1000188417Sthompsa		{ ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
1001188417Sthompsa		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
1002186730Salfred	};
1003186730Salfred	static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
1004186730Salfred	static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
1005186730Salfred	static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
1006188417Sthompsa	int i, error;
1007184610Salfred
1008184610Salfred	/* init RF-dependent PHY registers */
1009188417Sthompsa	for (i = 0; i < N(phyini); i++)
1010188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1011186730Salfred
1012188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1013188417Sthompsa		for (i = 0; i < N(phy2230s); i++)
1014188417Sthompsa			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1015184610Salfred	}
1016188417Sthompsa
1017186730Salfred	/* init AL2230 radio */
1018188417Sthompsa	for (i = 0; i < N(rfini1); i++) {
1019188417Sthompsa		error = zyd_rfwrite(sc, rfini1[i]);
1020188417Sthompsa		if (error != 0)
1021188417Sthompsa			goto fail;
1022188417Sthompsa	}
1023184610Salfred
1024188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1025188417Sthompsa		error = zyd_rfwrite(sc, 0x000824);
1026186730Salfred	else
1027188417Sthompsa		error = zyd_rfwrite(sc, 0x0005a4);
1028188417Sthompsa	if (error != 0)
1029188417Sthompsa		goto fail;
1030186730Salfred
1031188417Sthompsa	for (i = 0; i < N(rfini2); i++) {
1032188417Sthompsa		error = zyd_rfwrite(sc, rfini2[i]);
1033188417Sthompsa		if (error != 0)
1034188417Sthompsa			goto fail;
1035188417Sthompsa	}
1036186730Salfred
1037188417Sthompsa	for (i = 0; i < N(phypll); i++)
1038188417Sthompsa		zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
1039186730Salfred
1040188417Sthompsa	for (i = 0; i < N(rfini3); i++) {
1041188417Sthompsa		error = zyd_rfwrite(sc, rfini3[i]);
1042188417Sthompsa		if (error != 0)
1043188417Sthompsa			goto fail;
1044188417Sthompsa	}
1045188417Sthompsafail:
1046188417Sthompsa	return (error);
1047188417Sthompsa#undef N
1048184610Salfred}
1049184610Salfred
1050188417Sthompsastatic int
1051188417Sthompsazyd_al2230_fini(struct zyd_rf *rf)
1052186730Salfred{
1053188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1054188417Sthompsa	int error, i;
1055188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1056186730Salfred	static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
1057186730Salfred
1058188417Sthompsa	for (i = 0; i < N(phy); i++)
1059188417Sthompsa		zyd_write16_m(sc, phy[i].reg, phy[i].val);
1060186730Salfred
1061186730Salfred	if (sc->sc_newphy != 0)
1062188417Sthompsa		zyd_write16_m(sc, ZYD_CR9, 0xe1);
1063188417Sthompsa
1064188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, 0x6);
1065188417Sthompsafail:
1066188417Sthompsa	return (error);
1067188417Sthompsa#undef N
1068186730Salfred}
1069186730Salfred
1070188417Sthompsastatic int
1071188417Sthompsazyd_al2230_init_b(struct zyd_rf *rf)
1072184610Salfred{
1073188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1074188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1075186730Salfred	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1076186730Salfred	static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
1077186730Salfred	static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
1078186730Salfred	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1079188417Sthompsa	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1080186730Salfred	static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
1081186730Salfred	static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
1082186730Salfred	static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
1083186730Salfred	static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
1084188417Sthompsa	int i, error;
1085184610Salfred
1086188417Sthompsa	for (i = 0; i < N(phy1); i++)
1087188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1088186730Salfred
1089184610Salfred	/* init RF-dependent PHY registers */
1090188417Sthompsa	for (i = 0; i < N(phyini); i++)
1091188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1092184610Salfred
1093188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1094188417Sthompsa		for (i = 0; i < N(phy2230s); i++)
1095188417Sthompsa			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1096188417Sthompsa	}
1097186730Salfred
1098188417Sthompsa	for (i = 0; i < 3; i++) {
1099188417Sthompsa		error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
1100188417Sthompsa		if (error != 0)
1101188417Sthompsa			return (error);
1102188417Sthompsa	}
1103186730Salfred
1104188417Sthompsa	for (i = 0; i < N(rfini_part1); i++) {
1105188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part1[i]);
1106188417Sthompsa		if (error != 0)
1107188417Sthompsa			return (error);
1108188417Sthompsa	}
1109186730Salfred
1110188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1111188417Sthompsa		error = zyd_rfwrite(sc, 0x241000);
1112186730Salfred	else
1113188417Sthompsa		error = zyd_rfwrite(sc, 0x25a000);
1114188417Sthompsa	if (error != 0)
1115188417Sthompsa		goto fail;
1116186730Salfred
1117188417Sthompsa	for (i = 0; i < N(rfini_part2); i++) {
1118188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part2[i]);
1119188417Sthompsa		if (error != 0)
1120188417Sthompsa			return (error);
1121188417Sthompsa	}
1122186730Salfred
1123188417Sthompsa	for (i = 0; i < N(phy2); i++)
1124188417Sthompsa		zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
1125186730Salfred
1126188417Sthompsa	for (i = 0; i < N(rfini_part3); i++) {
1127188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part3[i]);
1128188417Sthompsa		if (error != 0)
1129188417Sthompsa			return (error);
1130188417Sthompsa	}
1131186730Salfred
1132188417Sthompsa	for (i = 0; i < N(phy3); i++)
1133188417Sthompsa		zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
1134186730Salfred
1135188417Sthompsa	error = zyd_al2230_fini(rf);
1136188417Sthompsafail:
1137188417Sthompsa	return (error);
1138188417Sthompsa#undef N
1139184610Salfred}
1140184610Salfred
1141188417Sthompsastatic int
1142188417Sthompsazyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1143184610Salfred{
1144188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1145188417Sthompsa	int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
1146188417Sthompsa
1147188417Sthompsa	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1148188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
1149188417Sthompsafail:
1150188417Sthompsa	return (error);
1151188417Sthompsa}
1152188417Sthompsa
1153188417Sthompsastatic int
1154188417Sthompsazyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1155188417Sthompsa{
1156188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1157188417Sthompsa	int error, i;
1158188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1159186730Salfred	static const struct zyd_phy_pair phy1[] = {
1160188417Sthompsa		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
1161186730Salfred	};
1162184610Salfred	static const struct {
1163188417Sthompsa		uint32_t	r1, r2, r3;
1164188417Sthompsa	} rfprog[] = ZYD_AL2230_CHANTABLE;
1165184610Salfred
1166188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1167188417Sthompsa	if (error != 0)
1168188417Sthompsa		goto fail;
1169188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1170188417Sthompsa	if (error != 0)
1171188417Sthompsa		goto fail;
1172188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
1173188417Sthompsa	if (error != 0)
1174188417Sthompsa		goto fail;
1175184610Salfred
1176188417Sthompsa	for (i = 0; i < N(phy1); i++)
1177188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1178188417Sthompsafail:
1179188417Sthompsa	return (error);
1180188417Sthompsa#undef N
1181184610Salfred}
1182184610Salfred
1183188417Sthompsastatic int
1184188417Sthompsazyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
1185186730Salfred{
1186188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1187188417Sthompsa	int error, i;
1188188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1189186730Salfred	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1190186730Salfred	static const struct {
1191188417Sthompsa		uint32_t	r1, r2, r3;
1192188417Sthompsa	} rfprog[] = ZYD_AL2230_CHANTABLE_B;
1193186730Salfred
1194188417Sthompsa	for (i = 0; i < N(phy1); i++)
1195188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1196186730Salfred
1197188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
1198188417Sthompsa	if (error != 0)
1199188417Sthompsa		goto fail;
1200188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
1201188417Sthompsa	if (error != 0)
1202188417Sthompsa		goto fail;
1203188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
1204188417Sthompsa	if (error != 0)
1205188417Sthompsa		goto fail;
1206188417Sthompsa	error = zyd_al2230_fini(rf);
1207188417Sthompsafail:
1208188417Sthompsa	return (error);
1209188417Sthompsa#undef N
1210186730Salfred}
1211186730Salfred
1212186730Salfred#define	ZYD_AL2230_PHY_BANDEDGE6					\
1213186730Salfred{									\
1214188417Sthompsa	{ ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 },	\
1215186730Salfred	{ ZYD_CR47,  0x1e }						\
1216186730Salfred}
1217186730Salfred
1218188417Sthompsastatic int
1219188417Sthompsazyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
1220186730Salfred{
1221188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1222188417Sthompsa	int error = 0, i;
1223188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1224188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1225188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1226186730Salfred	struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
1227188601Sthompsa	int chan = ieee80211_chan2ieee(ic, c);
1228186730Salfred
1229188417Sthompsa	if (chan == 1 || chan == 11)
1230186730Salfred		r[0].val = 0x12;
1231188417Sthompsa
1232188417Sthompsa	for (i = 0; i < N(r); i++)
1233188417Sthompsa		zyd_write16_m(sc, r[i].reg, r[i].val);
1234188417Sthompsafail:
1235188417Sthompsa	return (error);
1236188417Sthompsa#undef N
1237186730Salfred}
1238186730Salfred
1239184610Salfred/*
1240184610Salfred * AL7230B RF methods.
1241184610Salfred */
1242188417Sthompsastatic int
1243188417Sthompsazyd_al7230B_init(struct zyd_rf *rf)
1244184610Salfred{
1245188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1246188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1247184610Salfred	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1248184610Salfred	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1249184610Salfred	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1250184610Salfred	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1251184610Salfred	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1252188417Sthompsa	int i, error;
1253184610Salfred
1254184610Salfred	/* for AL7230B, PHY and RF need to be initialized in "phases" */
1255184610Salfred
1256184610Salfred	/* init RF-dependent PHY registers, part one */
1257188417Sthompsa	for (i = 0; i < N(phyini_1); i++)
1258188417Sthompsa		zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
1259188417Sthompsa
1260184610Salfred	/* init AL7230B radio, part one */
1261188417Sthompsa	for (i = 0; i < N(rfini_1); i++) {
1262188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1263188417Sthompsa			return (error);
1264184610Salfred	}
1265184610Salfred	/* init RF-dependent PHY registers, part two */
1266188417Sthompsa	for (i = 0; i < N(phyini_2); i++)
1267188417Sthompsa		zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
1268188417Sthompsa
1269184610Salfred	/* init AL7230B radio, part two */
1270188417Sthompsa	for (i = 0; i < N(rfini_2); i++) {
1271188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1272188417Sthompsa			return (error);
1273184610Salfred	}
1274184610Salfred	/* init RF-dependent PHY registers, part three */
1275188417Sthompsa	for (i = 0; i < N(phyini_3); i++)
1276188417Sthompsa		zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
1277188417Sthompsafail:
1278188417Sthompsa	return (error);
1279188417Sthompsa#undef N
1280184610Salfred}
1281184610Salfred
1282188417Sthompsastatic int
1283188417Sthompsazyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1284184610Salfred{
1285188417Sthompsa	int error;
1286188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1287188417Sthompsa
1288188417Sthompsa	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1289188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1290188417Sthompsafail:
1291188417Sthompsa	return (error);
1292188417Sthompsa}
1293188417Sthompsa
1294188417Sthompsastatic int
1295188417Sthompsazyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1296188417Sthompsa{
1297188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1298188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1299184610Salfred	static const struct {
1300188417Sthompsa		uint32_t	r1, r2;
1301188417Sthompsa	} rfprog[] = ZYD_AL7230B_CHANTABLE;
1302184610Salfred	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1303188417Sthompsa	int i, error;
1304184610Salfred
1305188417Sthompsa	zyd_write16_m(sc, ZYD_CR240, 0x57);
1306188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, 0x2f);
1307184610Salfred
1308188417Sthompsa	for (i = 0; i < N(rfsc); i++) {
1309188417Sthompsa		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1310188417Sthompsa			return (error);
1311184610Salfred	}
1312184610Salfred
1313188417Sthompsa	zyd_write16_m(sc, ZYD_CR128, 0x14);
1314188417Sthompsa	zyd_write16_m(sc, ZYD_CR129, 0x12);
1315188417Sthompsa	zyd_write16_m(sc, ZYD_CR130, 0x10);
1316188417Sthompsa	zyd_write16_m(sc, ZYD_CR38,  0x38);
1317188417Sthompsa	zyd_write16_m(sc, ZYD_CR136, 0xdf);
1318184610Salfred
1319188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1320188417Sthompsa	if (error != 0)
1321188417Sthompsa		goto fail;
1322188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1323188417Sthompsa	if (error != 0)
1324188417Sthompsa		goto fail;
1325188417Sthompsa	error = zyd_rfwrite(sc, 0x3c9000);
1326188417Sthompsa	if (error != 0)
1327188417Sthompsa		goto fail;
1328184610Salfred
1329188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, 0x3f);
1330188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, 0x06);
1331188417Sthompsa	zyd_write16_m(sc, ZYD_CR240, 0x08);
1332188417Sthompsafail:
1333188417Sthompsa	return (error);
1334188417Sthompsa#undef N
1335184610Salfred}
1336184610Salfred
1337184610Salfred/*
1338184610Salfred * AL2210 RF methods.
1339184610Salfred */
1340188417Sthompsastatic int
1341188417Sthompsazyd_al2210_init(struct zyd_rf *rf)
1342184610Salfred{
1343188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1344188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1345184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1346184610Salfred	static const uint32_t rfini[] = ZYD_AL2210_RF;
1347184610Salfred	uint32_t tmp;
1348188417Sthompsa	int i, error;
1349184610Salfred
1350188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 2);
1351184610Salfred
1352184610Salfred	/* init RF-dependent PHY registers */
1353188417Sthompsa	for (i = 0; i < N(phyini); i++)
1354188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1355188417Sthompsa
1356184610Salfred	/* init AL2210 radio */
1357188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1358188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1359188417Sthompsa			return (error);
1360184610Salfred	}
1361188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1362188417Sthompsa	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1363188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1364188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1365188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1366188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1367188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1368188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 3);
1369188417Sthompsafail:
1370188417Sthompsa	return (error);
1371188417Sthompsa#undef N
1372184610Salfred}
1373184610Salfred
1374188417Sthompsastatic int
1375188417Sthompsazyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1376184610Salfred{
1377188417Sthompsa	/* vendor driver does nothing for this RF chip */
1378188417Sthompsa
1379188417Sthompsa	return (0);
1380188417Sthompsa}
1381188417Sthompsa
1382188417Sthompsastatic int
1383188417Sthompsazyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1384188417Sthompsa{
1385188417Sthompsa	int error;
1386188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1387184610Salfred	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1388184610Salfred	uint32_t tmp;
1389184610Salfred
1390188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 2);
1391188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1392188417Sthompsa	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1393188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1394188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1395188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1396188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1397188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1398184610Salfred
1399184610Salfred	/* actually set the channel */
1400188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1]);
1401188417Sthompsa	if (error != 0)
1402188417Sthompsa		goto fail;
1403184610Salfred
1404188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 3);
1405188417Sthompsafail:
1406188417Sthompsa	return (error);
1407184610Salfred}
1408184610Salfred
1409184610Salfred/*
1410184610Salfred * GCT RF methods.
1411184610Salfred */
1412188417Sthompsastatic int
1413188417Sthompsazyd_gct_init(struct zyd_rf *rf)
1414184610Salfred{
1415193420Sweongyo#define	ZYD_GCT_INTR_REG	0x85c1
1416188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1417188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1418184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1419184610Salfred	static const uint32_t rfini[] = ZYD_GCT_RF;
1420193420Sweongyo	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1421193420Sweongyo	int i, idx = -1, error;
1422193420Sweongyo	uint16_t data;
1423184610Salfred
1424184610Salfred	/* init RF-dependent PHY registers */
1425188417Sthompsa	for (i = 0; i < N(phyini); i++)
1426188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1427188417Sthompsa
1428184610Salfred	/* init cgt radio */
1429188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1430188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1431188417Sthompsa			return (error);
1432184610Salfred	}
1433193420Sweongyo
1434193420Sweongyo	error = zyd_gct_mode(rf);
1435193420Sweongyo	if (error != 0)
1436193420Sweongyo		return (error);
1437193420Sweongyo
1438193420Sweongyo	for (i = 0; i < N(vco) - 1; i++) {
1439193420Sweongyo		error = zyd_gct_set_channel_synth(rf, 1, 0);
1440193420Sweongyo		if (error != 0)
1441193420Sweongyo			goto fail;
1442193420Sweongyo		error = zyd_gct_write(rf, vco[i][0]);
1443193420Sweongyo		if (error != 0)
1444193420Sweongyo			goto fail;
1445193420Sweongyo		zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf);
1446193420Sweongyo		zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data);
1447193420Sweongyo		if ((data & 0xf) == 0) {
1448193420Sweongyo			idx = i;
1449193420Sweongyo			break;
1450193420Sweongyo		}
1451193420Sweongyo	}
1452193420Sweongyo	if (idx == -1) {
1453193420Sweongyo		error = zyd_gct_set_channel_synth(rf, 1, 1);
1454193420Sweongyo		if (error != 0)
1455193420Sweongyo			goto fail;
1456193420Sweongyo		error = zyd_gct_write(rf, 0x6662);
1457193420Sweongyo		if (error != 0)
1458193420Sweongyo			goto fail;
1459193420Sweongyo	}
1460193420Sweongyo
1461193420Sweongyo	rf->idx = idx;
1462193420Sweongyo	zyd_write16_m(sc, ZYD_CR203, 0x6);
1463188417Sthompsafail:
1464188417Sthompsa	return (error);
1465188417Sthompsa#undef N
1466193420Sweongyo#undef ZYD_GCT_INTR_REG
1467184610Salfred}
1468184610Salfred
1469188417Sthompsastatic int
1470193420Sweongyozyd_gct_mode(struct zyd_rf *rf)
1471184610Salfred{
1472193420Sweongyo#define N(a)	(sizeof(a) / sizeof((a)[0]))
1473193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1474193420Sweongyo	static const uint32_t mode[] = {
1475193420Sweongyo		0x25f98, 0x25f9a, 0x25f94, 0x27fd4
1476193420Sweongyo	};
1477193420Sweongyo	int i, error;
1478188417Sthompsa
1479193420Sweongyo	for (i = 0; i < N(mode); i++) {
1480193420Sweongyo		if ((error = zyd_rfwrite(sc, mode[i])) != 0)
1481193420Sweongyo			break;
1482193420Sweongyo	}
1483193420Sweongyo	return (error);
1484193420Sweongyo#undef N
1485188417Sthompsa}
1486188417Sthompsa
1487188417Sthompsastatic int
1488193420Sweongyozyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal)
1489188417Sthompsa{
1490193420Sweongyo	int error, idx = chan - 1;
1491188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1492193420Sweongyo	static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL;
1493193420Sweongyo	static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD;
1494193420Sweongyo	static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV;
1495184610Salfred
1496193420Sweongyo	error = zyd_rfwrite(sc,
1497193420Sweongyo	    (acal == 1) ? acal_synth[idx] : std_synth[idx]);
1498188417Sthompsa	if (error != 0)
1499193420Sweongyo		return (error);
1500193420Sweongyo	return zyd_rfwrite(sc, div_synth[idx]);
1501184610Salfred}
1502184610Salfred
1503188417Sthompsastatic int
1504193420Sweongyozyd_gct_write(struct zyd_rf *rf, uint16_t value)
1505184610Salfred{
1506188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1507184610Salfred
1508193420Sweongyo	return zyd_rfwrite(sc, 0x300000 | 0x40000 | value);
1509184610Salfred}
1510184610Salfred
1511188417Sthompsastatic int
1512193420Sweongyozyd_gct_switch_radio(struct zyd_rf *rf, int on)
1513184610Salfred{
1514193420Sweongyo#define N(a)	(sizeof(a) / sizeof((a)[0]))
1515193420Sweongyo	int error;
1516193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1517188417Sthompsa
1518193420Sweongyo	error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90);
1519193420Sweongyo	if (error != 0)
1520193420Sweongyo		return (error);
1521193420Sweongyo
1522193420Sweongyo	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
1523193420Sweongyo	zyd_write16_m(sc, ZYD_CR251,
1524193420Sweongyo	    on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f);
1525193420Sweongyofail:
1526193420Sweongyo	return (error);
1527188417Sthompsa}
1528188417Sthompsa
1529188417Sthompsastatic int
1530193420Sweongyozyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1531188417Sthompsa{
1532188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1533193420Sweongyo	int error, i;
1534188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1535193420Sweongyo	static const struct zyd_phy_pair cmd[] = {
1536193420Sweongyo		{ ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 },
1537193420Sweongyo		{ ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 },
1538193420Sweongyo	};
1539193420Sweongyo	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1540184610Salfred
1541193420Sweongyo	error = zyd_gct_set_channel_synth(rf, chan, 0);
1542188417Sthompsa	if (error != 0)
1543188417Sthompsa		goto fail;
1544193420Sweongyo	error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 :
1545193420Sweongyo	    vco[rf->idx][((chan - 1) / 2)]);
1546188417Sthompsa	if (error != 0)
1547188417Sthompsa		goto fail;
1548193420Sweongyo	error = zyd_gct_mode(rf);
1549193420Sweongyo	if (error != 0)
1550193420Sweongyo		return (error);
1551193420Sweongyo	for (i = 0; i < N(cmd); i++)
1552193420Sweongyo		zyd_write16_m(sc, cmd[i].reg, cmd[i].val);
1553193420Sweongyo	error = zyd_gct_txgain(rf, chan);
1554193420Sweongyo	if (error != 0)
1555193420Sweongyo		return (error);
1556193420Sweongyo	zyd_write16_m(sc, ZYD_CR203, 0x6);
1557188417Sthompsafail:
1558188417Sthompsa	return (error);
1559188417Sthompsa#undef N
1560184610Salfred}
1561184610Salfred
1562193420Sweongyostatic int
1563193420Sweongyozyd_gct_txgain(struct zyd_rf *rf, uint8_t chan)
1564193420Sweongyo{
1565193420Sweongyo#define N(a)	(sizeof(a) / sizeof((a)[0]))
1566193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1567193420Sweongyo	static uint32_t txgain[] = ZYD_GCT_TXGAIN;
1568193420Sweongyo	uint8_t idx = sc->sc_pwrint[chan - 1];
1569193420Sweongyo
1570193420Sweongyo	if (idx >= N(txgain)) {
1571193420Sweongyo		device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n",
1572193420Sweongyo		    chan, idx);
1573193420Sweongyo		return 0;
1574193420Sweongyo	}
1575193420Sweongyo
1576193420Sweongyo	return zyd_rfwrite(sc, 0x700000 | txgain[idx]);
1577193420Sweongyo#undef N
1578193420Sweongyo}
1579193420Sweongyo
1580184610Salfred/*
1581184610Salfred * Maxim2 RF methods.
1582184610Salfred */
1583188417Sthompsastatic int
1584188417Sthompsazyd_maxim2_init(struct zyd_rf *rf)
1585184610Salfred{
1586188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1587188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1588184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1589184610Salfred	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1590184610Salfred	uint16_t tmp;
1591188417Sthompsa	int i, error;
1592184610Salfred
1593184610Salfred	/* init RF-dependent PHY registers */
1594188417Sthompsa	for (i = 0; i < N(phyini); i++)
1595188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1596184610Salfred
1597188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1598188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1599188417Sthompsa
1600184610Salfred	/* init maxim2 radio */
1601188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1602188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1603188417Sthompsa			return (error);
1604184610Salfred	}
1605188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1606188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1607188417Sthompsafail:
1608188417Sthompsa	return (error);
1609188417Sthompsa#undef N
1610184610Salfred}
1611184610Salfred
1612188417Sthompsastatic int
1613188417Sthompsazyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1614184610Salfred{
1615188417Sthompsa
1616188417Sthompsa	/* vendor driver does nothing for this RF chip */
1617188417Sthompsa	return (0);
1618188417Sthompsa}
1619188417Sthompsa
1620188417Sthompsastatic int
1621188417Sthompsazyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1622188417Sthompsa{
1623188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1624188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1625184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1626184610Salfred	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1627184610Salfred	static const struct {
1628188417Sthompsa		uint32_t	r1, r2;
1629188417Sthompsa	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
1630184610Salfred	uint16_t tmp;
1631188417Sthompsa	int i, error;
1632184610Salfred
1633184610Salfred	/*
1634184610Salfred	 * Do the same as we do when initializing it, except for the channel
1635184610Salfred	 * values coming from the two channel tables.
1636184610Salfred	 */
1637184610Salfred
1638184610Salfred	/* init RF-dependent PHY registers */
1639188417Sthompsa	for (i = 0; i < N(phyini); i++)
1640188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1641184610Salfred
1642188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1643188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1644188417Sthompsa
1645184610Salfred	/* first two values taken from the chantables */
1646188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1647188417Sthompsa	if (error != 0)
1648188417Sthompsa		goto fail;
1649188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1650188417Sthompsa	if (error != 0)
1651188417Sthompsa		goto fail;
1652184610Salfred
1653184610Salfred	/* init maxim2 radio - skipping the two first values */
1654188417Sthompsa	for (i = 2; i < N(rfini); i++) {
1655188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1656188417Sthompsa			return (error);
1657184610Salfred	}
1658188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1659188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1660188417Sthompsafail:
1661188417Sthompsa	return (error);
1662188417Sthompsa#undef N
1663184610Salfred}
1664184610Salfred
1665188417Sthompsastatic int
1666188417Sthompsazyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1667184610Salfred{
1668188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1669184610Salfred
1670188417Sthompsa	rf->rf_sc = sc;
1671193420Sweongyo	rf->update_pwr = 1;
1672188417Sthompsa
1673188417Sthompsa	switch (type) {
1674184610Salfred	case ZYD_RF_RFMD:
1675188417Sthompsa		rf->init         = zyd_rfmd_init;
1676188417Sthompsa		rf->switch_radio = zyd_rfmd_switch_radio;
1677188417Sthompsa		rf->set_channel  = zyd_rfmd_set_channel;
1678188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1679184610Salfred		break;
1680184610Salfred	case ZYD_RF_AL2230:
1681186730Salfred	case ZYD_RF_AL2230S:
1682188417Sthompsa		if (sc->sc_macrev == ZYD_ZD1211B) {
1683188417Sthompsa			rf->init = zyd_al2230_init_b;
1684188417Sthompsa			rf->set_channel = zyd_al2230_set_channel_b;
1685186730Salfred		} else {
1686188417Sthompsa			rf->init = zyd_al2230_init;
1687188417Sthompsa			rf->set_channel = zyd_al2230_set_channel;
1688186730Salfred		}
1689188417Sthompsa		rf->switch_radio = zyd_al2230_switch_radio;
1690188417Sthompsa		rf->bandedge6	 = zyd_al2230_bandedge6;
1691188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1692184610Salfred		break;
1693184610Salfred	case ZYD_RF_AL7230B:
1694188417Sthompsa		rf->init         = zyd_al7230B_init;
1695188417Sthompsa		rf->switch_radio = zyd_al7230B_switch_radio;
1696188417Sthompsa		rf->set_channel  = zyd_al7230B_set_channel;
1697188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1698184610Salfred		break;
1699184610Salfred	case ZYD_RF_AL2210:
1700188417Sthompsa		rf->init         = zyd_al2210_init;
1701188417Sthompsa		rf->switch_radio = zyd_al2210_switch_radio;
1702188417Sthompsa		rf->set_channel  = zyd_al2210_set_channel;
1703188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1704184610Salfred		break;
1705193420Sweongyo	case ZYD_RF_MAXIM_NEW:
1706184610Salfred	case ZYD_RF_GCT:
1707188417Sthompsa		rf->init         = zyd_gct_init;
1708188417Sthompsa		rf->switch_radio = zyd_gct_switch_radio;
1709188417Sthompsa		rf->set_channel  = zyd_gct_set_channel;
1710193420Sweongyo		rf->width        = 24;	/* 24-bit RF values */
1711193420Sweongyo		rf->update_pwr   = 0;
1712184610Salfred		break;
1713184610Salfred	case ZYD_RF_MAXIM_NEW2:
1714188417Sthompsa		rf->init         = zyd_maxim2_init;
1715188417Sthompsa		rf->switch_radio = zyd_maxim2_switch_radio;
1716188417Sthompsa		rf->set_channel  = zyd_maxim2_set_channel;
1717188417Sthompsa		rf->width        = 18;	/* 18-bit RF values */
1718184610Salfred		break;
1719184610Salfred	default:
1720188417Sthompsa		device_printf(sc->sc_dev,
1721188417Sthompsa		    "sorry, radio \"%s\" is not supported yet\n",
1722188417Sthompsa		    zyd_rf_name(type));
1723188417Sthompsa		return (EINVAL);
1724184610Salfred	}
1725188417Sthompsa	return (0);
1726188417Sthompsa}
1727184610Salfred
1728188417Sthompsastatic const char *
1729188417Sthompsazyd_rf_name(uint8_t type)
1730188417Sthompsa{
1731188417Sthompsa	static const char * const zyd_rfs[] = {
1732188417Sthompsa		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
1733188417Sthompsa		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
1734188417Sthompsa		"AL2230S",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
1735188417Sthompsa		"PHILIPS"
1736188417Sthompsa	};
1737184610Salfred
1738188417Sthompsa	return zyd_rfs[(type > 15) ? 0 : type];
1739184610Salfred}
1740184610Salfred
1741188417Sthompsastatic int
1742188417Sthompsazyd_hw_init(struct zyd_softc *sc)
1743184610Salfred{
1744188417Sthompsa	int error;
1745184610Salfred	const struct zyd_phy_pair *phyp;
1746188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1747188417Sthompsa	uint16_t val;
1748184610Salfred
1749184610Salfred	/* specify that the plug and play is finished */
1750188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1751188417Sthompsa	zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
1752188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
1753188417Sthompsa	    sc->sc_fwbase);
1754184610Salfred
1755184610Salfred	/* retrieve firmware revision number */
1756188417Sthompsa	zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
1757188417Sthompsa	zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
1758188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1759186730Salfred	/* set mandatory rates - XXX assumes 802.11b/g */
1760188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
1761186730Salfred
1762184610Salfred	/* disable interrupts */
1763188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
1764184610Salfred
1765188417Sthompsa	if ((error = zyd_read_pod(sc)) != 0) {
1766188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
1767188417Sthompsa		goto fail;
1768184610Salfred	}
1769188417Sthompsa
1770188417Sthompsa	/* PHY init (resetting) */
1771188417Sthompsa	error = zyd_lock_phy(sc);
1772188417Sthompsa	if (error != 0)
1773188417Sthompsa		goto fail;
1774188417Sthompsa	phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1775188417Sthompsa	for (; phyp->reg != 0; phyp++)
1776188417Sthompsa		zyd_write16_m(sc, phyp->reg, phyp->val);
1777188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
1778188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
1779188417Sthompsa		zyd_write32_m(sc, ZYD_CR157, val >> 8);
1780184610Salfred	}
1781188417Sthompsa	error = zyd_unlock_phy(sc);
1782188417Sthompsa	if (error != 0)
1783188417Sthompsa		goto fail;
1784184610Salfred
1785184610Salfred	/* HMAC init */
1786188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1787188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1788188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
1789188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
1790188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
1791188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
1792188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
1793188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1794188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1795188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1796188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1797188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1798188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1799188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1800188417Sthompsa	zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
1801188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1802188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1803188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1804188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
1805188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
1806184610Salfred
1807188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211) {
1808188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
1809188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1810186730Salfred	} else {
1811188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1812188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1813188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1814188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1815188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1816188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1817188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1818188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
1819188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
1820186730Salfred	}
1821186730Salfred
1822184610Salfred	/* init beacon interval to 100ms */
1823188417Sthompsa	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1824188417Sthompsa		goto fail;
1825184610Salfred
1826188417Sthompsa	if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
1827188417Sthompsa		device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
1828188417Sthompsa		    sc->sc_rfrev);
1829188417Sthompsa		goto fail;
1830188417Sthompsa	}
1831188417Sthompsa
1832188417Sthompsa	/* RF chip init */
1833188417Sthompsa	error = zyd_lock_phy(sc);
1834188417Sthompsa	if (error != 0)
1835188417Sthompsa		goto fail;
1836188417Sthompsa	error = (*rf->init)(rf);
1837188417Sthompsa	if (error != 0) {
1838188417Sthompsa		device_printf(sc->sc_dev,
1839188417Sthompsa		    "radio initialization failed, error %d\n", error);
1840188417Sthompsa		goto fail;
1841188417Sthompsa	}
1842188417Sthompsa	error = zyd_unlock_phy(sc);
1843188417Sthompsa	if (error != 0)
1844188417Sthompsa		goto fail;
1845188417Sthompsa
1846188417Sthompsa	if ((error = zyd_read_eeprom(sc)) != 0) {
1847188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
1848188417Sthompsa		goto fail;
1849188417Sthompsa	}
1850188417Sthompsa
1851188417Sthompsafail:	return (error);
1852184610Salfred}
1853184610Salfred
1854188417Sthompsastatic int
1855188417Sthompsazyd_read_pod(struct zyd_softc *sc)
1856184610Salfred{
1857188417Sthompsa	int error;
1858184610Salfred	uint32_t tmp;
1859184610Salfred
1860188417Sthompsa	zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
1861188417Sthompsa	sc->sc_rfrev     = tmp & 0x0f;
1862188417Sthompsa	sc->sc_ledtype   = (tmp >>  4) & 0x01;
1863188417Sthompsa	sc->sc_al2230s   = (tmp >>  7) & 0x01;
1864188417Sthompsa	sc->sc_cckgain   = (tmp >>  8) & 0x01;
1865184610Salfred	sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
1866188417Sthompsa	sc->sc_parev     = (tmp >> 16) & 0x0f;
1867186730Salfred	sc->sc_bandedge6 = (tmp >> 21) & 0x01;
1868188417Sthompsa	sc->sc_newphy    = (tmp >> 31) & 0x01;
1869188417Sthompsa	sc->sc_txled     = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
1870188417Sthompsafail:
1871188417Sthompsa	return (error);
1872188417Sthompsa}
1873184610Salfred
1874188417Sthompsastatic int
1875188417Sthompsazyd_read_eeprom(struct zyd_softc *sc)
1876188417Sthompsa{
1877188417Sthompsa	uint16_t val;
1878188417Sthompsa	int error, i;
1879184610Salfred
1880184610Salfred	/* read Tx power calibration tables */
1881184610Salfred	for (i = 0; i < 7; i++) {
1882188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1883188417Sthompsa		sc->sc_pwrcal[i * 2] = val >> 8;
1884188417Sthompsa		sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
1885188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
1886188417Sthompsa		sc->sc_pwrint[i * 2] = val >> 8;
1887188417Sthompsa		sc->sc_pwrint[i * 2 + 1] = val & 0xff;
1888188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
1889188417Sthompsa		sc->sc_ofdm36_cal[i * 2] = val >> 8;
1890188417Sthompsa		sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
1891188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
1892188417Sthompsa		sc->sc_ofdm48_cal[i * 2] = val >> 8;
1893188417Sthompsa		sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
1894188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
1895188417Sthompsa		sc->sc_ofdm54_cal[i * 2] = val >> 8;
1896188417Sthompsa		sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
1897184610Salfred	}
1898188417Sthompsafail:
1899188417Sthompsa	return (error);
1900184610Salfred}
1901184610Salfred
1902188417Sthompsastatic int
1903188417Sthompsazyd_get_macaddr(struct zyd_softc *sc)
1904186730Salfred{
1905192984Sthompsa	struct usb_device_request req;
1906193045Sthompsa	usb_error_t error;
1907186730Salfred
1908186730Salfred	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1909186730Salfred	req.bRequest = ZYD_READFWDATAREQ;
1910186730Salfred	USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
1911186730Salfred	USETW(req.wIndex, 0);
1912186730Salfred	USETW(req.wLength, IEEE80211_ADDR_LEN);
1913186730Salfred
1914188419Sthompsa	error = zyd_do_request(sc, &req, sc->sc_bssid);
1915188417Sthompsa	if (error != 0) {
1916188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1917194228Sthompsa		    usbd_errstr(error));
1918188417Sthompsa	}
1919188417Sthompsa
1920188417Sthompsa	return (error);
1921186730Salfred}
1922186730Salfred
1923188417Sthompsastatic int
1924188417Sthompsazyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1925184610Salfred{
1926188417Sthompsa	int error;
1927184610Salfred	uint32_t tmp;
1928184610Salfred
1929188417Sthompsa	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1930188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
1931188417Sthompsa	tmp = addr[5] << 8 | addr[4];
1932188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
1933188417Sthompsafail:
1934188417Sthompsa	return (error);
1935188417Sthompsa}
1936184610Salfred
1937188417Sthompsastatic int
1938188417Sthompsazyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1939188417Sthompsa{
1940188417Sthompsa	int error;
1941188417Sthompsa	uint32_t tmp;
1942188417Sthompsa
1943188417Sthompsa	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1944188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
1945188417Sthompsa	tmp = addr[5] << 8 | addr[4];
1946188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
1947188417Sthompsafail:
1948188417Sthompsa	return (error);
1949184610Salfred}
1950184610Salfred
1951188417Sthompsastatic int
1952188417Sthompsazyd_switch_radio(struct zyd_softc *sc, int on)
1953184610Salfred{
1954188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1955188417Sthompsa	int error;
1956188417Sthompsa
1957188417Sthompsa	error = zyd_lock_phy(sc);
1958188417Sthompsa	if (error != 0)
1959188417Sthompsa		goto fail;
1960188417Sthompsa	error = (*rf->switch_radio)(rf, on);
1961188417Sthompsa	if (error != 0)
1962188417Sthompsa		goto fail;
1963188417Sthompsa	error = zyd_unlock_phy(sc);
1964188417Sthompsafail:
1965188417Sthompsa	return (error);
1966184610Salfred}
1967184610Salfred
1968188417Sthompsastatic int
1969188417Sthompsazyd_set_led(struct zyd_softc *sc, int which, int on)
1970184610Salfred{
1971188417Sthompsa	int error;
1972184610Salfred	uint32_t tmp;
1973184610Salfred
1974188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1975188417Sthompsa	tmp &= ~which;
1976188417Sthompsa	if (on)
1977188417Sthompsa		tmp |= which;
1978188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1979188417Sthompsafail:
1980188417Sthompsa	return (error);
1981188417Sthompsa}
1982184610Salfred
1983188417Sthompsastatic void
1984188417Sthompsazyd_set_multi(struct zyd_softc *sc)
1985184610Salfred{
1986188417Sthompsa	int error;
1987188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1988188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1989188417Sthompsa	struct ifmultiaddr *ifma;
1990188417Sthompsa	uint32_t low, high;
1991188417Sthompsa	uint8_t v;
1992184610Salfred
1993188417Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1994188417Sthompsa		return;
1995184610Salfred
1996188417Sthompsa	low = 0x00000000;
1997188417Sthompsa	high = 0x80000000;
1998184610Salfred
1999188417Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR ||
2000188417Sthompsa	    (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) {
2001188417Sthompsa		low = 0xffffffff;
2002188417Sthompsa		high = 0xffffffff;
2003184610Salfred	} else {
2004195049Srwatson		if_maddr_rlock(ifp);
2005188417Sthompsa		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2006188417Sthompsa			if (ifma->ifma_addr->sa_family != AF_LINK)
2007188417Sthompsa				continue;
2008188417Sthompsa			v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
2009188417Sthompsa			    ifma->ifma_addr))[5] >> 2;
2010188417Sthompsa			if (v < 32)
2011188417Sthompsa				low |= 1 << v;
2012188417Sthompsa			else
2013188417Sthompsa				high |= 1 << (v - 32);
2014188417Sthompsa		}
2015195049Srwatson		if_maddr_runlock(ifp);
2016184610Salfred	}
2017184610Salfred
2018188417Sthompsa	/* reprogram multicast global hash table */
2019188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBL, low);
2020188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBH, high);
2021188417Sthompsafail:
2022188417Sthompsa	if (error != 0)
2023188417Sthompsa		device_printf(sc->sc_dev,
2024188417Sthompsa		    "could not set multicast hash table\n");
2025188417Sthompsa}
2026184610Salfred
2027188417Sthompsastatic void
2028188417Sthompsazyd_update_mcast(struct ifnet *ifp)
2029188417Sthompsa{
2030188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2031184610Salfred
2032188417Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2033184610Salfred		return;
2034184610Salfred
2035188417Sthompsa	ZYD_LOCK(sc);
2036191746Sthompsa	zyd_set_multi(sc);
2037188417Sthompsa	ZYD_UNLOCK(sc);
2038188417Sthompsa}
2039184610Salfred
2040188417Sthompsastatic int
2041188417Sthompsazyd_set_rxfilter(struct zyd_softc *sc)
2042188417Sthompsa{
2043188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2044188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2045188417Sthompsa	uint32_t rxfilter;
2046184610Salfred
2047188417Sthompsa	switch (ic->ic_opmode) {
2048188417Sthompsa	case IEEE80211_M_STA:
2049188417Sthompsa		rxfilter = ZYD_FILTER_BSS;
2050188417Sthompsa		break;
2051188417Sthompsa	case IEEE80211_M_IBSS:
2052188417Sthompsa	case IEEE80211_M_HOSTAP:
2053188417Sthompsa		rxfilter = ZYD_FILTER_HOSTAP;
2054188417Sthompsa		break;
2055188417Sthompsa	case IEEE80211_M_MONITOR:
2056188417Sthompsa		rxfilter = ZYD_FILTER_MONITOR;
2057188417Sthompsa		break;
2058188417Sthompsa	default:
2059188417Sthompsa		/* should not get there */
2060188417Sthompsa		return (EINVAL);
2061184610Salfred	}
2062188417Sthompsa	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
2063188417Sthompsa}
2064184610Salfred
2065188417Sthompsastatic void
2066188417Sthompsazyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
2067188417Sthompsa{
2068188417Sthompsa	int error;
2069188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2070188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2071188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
2072188417Sthompsa	uint32_t tmp;
2073188601Sthompsa	int chan;
2074184610Salfred
2075188417Sthompsa	chan = ieee80211_chan2ieee(ic, c);
2076188417Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
2077188417Sthompsa		/* XXX should NEVER happen */
2078188417Sthompsa		device_printf(sc->sc_dev,
2079188417Sthompsa		    "%s: invalid channel %x\n", __func__, chan);
2080188417Sthompsa		return;
2081188417Sthompsa	}
2082184610Salfred
2083188417Sthompsa	error = zyd_lock_phy(sc);
2084188417Sthompsa	if (error != 0)
2085188417Sthompsa		goto fail;
2086184610Salfred
2087188417Sthompsa	error = (*rf->set_channel)(rf, chan);
2088188417Sthompsa	if (error != 0)
2089188417Sthompsa		goto fail;
2090184610Salfred
2091193420Sweongyo	if (rf->update_pwr) {
2092193420Sweongyo		/* update Tx power */
2093193420Sweongyo		zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
2094184610Salfred
2095193420Sweongyo		if (sc->sc_macrev == ZYD_ZD1211B) {
2096193420Sweongyo			zyd_write16_m(sc, ZYD_CR67,
2097193420Sweongyo			    sc->sc_ofdm36_cal[chan - 1]);
2098193420Sweongyo			zyd_write16_m(sc, ZYD_CR66,
2099193420Sweongyo			    sc->sc_ofdm48_cal[chan - 1]);
2100193420Sweongyo			zyd_write16_m(sc, ZYD_CR65,
2101193420Sweongyo			    sc->sc_ofdm54_cal[chan - 1]);
2102193420Sweongyo			zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
2103193420Sweongyo			zyd_write16_m(sc, ZYD_CR69, 0x28);
2104193420Sweongyo			zyd_write16_m(sc, ZYD_CR69, 0x2a);
2105193420Sweongyo		}
2106188417Sthompsa	}
2107188417Sthompsa	if (sc->sc_cckgain) {
2108188417Sthompsa		/* set CCK baseband gain from EEPROM */
2109188417Sthompsa		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
2110188417Sthompsa			zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
2111188417Sthompsa	}
2112188417Sthompsa	if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
2113188417Sthompsa		error = (*rf->bandedge6)(rf, c);
2114188417Sthompsa		if (error != 0)
2115188417Sthompsa			goto fail;
2116188417Sthompsa	}
2117188417Sthompsa	zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
2118184610Salfred
2119188417Sthompsa	error = zyd_unlock_phy(sc);
2120188417Sthompsa	if (error != 0)
2121188417Sthompsa		goto fail;
2122184610Salfred
2123188417Sthompsa	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
2124188417Sthompsa	    htole16(c->ic_freq);
2125188417Sthompsa	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
2126188417Sthompsa	    htole16(c->ic_flags);
2127188417Sthompsafail:
2128184610Salfred	return;
2129184610Salfred}
2130184610Salfred
2131184610Salfredstatic int
2132188417Sthompsazyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
2133184610Salfred{
2134188417Sthompsa	int error;
2135188417Sthompsa	uint32_t val;
2136184610Salfred
2137188417Sthompsa	zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
2138188417Sthompsa	sc->sc_atim_wnd = val;
2139188417Sthompsa	zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
2140188417Sthompsa	sc->sc_pre_tbtt = val;
2141188417Sthompsa	sc->sc_bcn_int = bintval;
2142184610Salfred
2143188417Sthompsa	if (sc->sc_bcn_int <= 5)
2144188417Sthompsa		sc->sc_bcn_int = 5;
2145188417Sthompsa	if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
2146188417Sthompsa		sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
2147188417Sthompsa	if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
2148188417Sthompsa		sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
2149184610Salfred
2150188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
2151188417Sthompsa	zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
2152188417Sthompsa	zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
2153188417Sthompsafail:
2154188417Sthompsa	return (error);
2155188417Sthompsa}
2156184610Salfred
2157188417Sthompsastatic void
2158192984Sthompsazyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
2159188417Sthompsa{
2160194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2161188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2162192468Ssam	struct ieee80211com *ic = ifp->if_l2com;
2163188417Sthompsa	struct zyd_plcphdr plcp;
2164188417Sthompsa	struct zyd_rx_stat stat;
2165194677Sthompsa	struct usb_page_cache *pc;
2166188417Sthompsa	struct mbuf *m;
2167188417Sthompsa	int rlen, rssi;
2168184610Salfred
2169188417Sthompsa	if (len < ZYD_MIN_FRAGSZ) {
2170188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
2171188417Sthompsa		    device_get_nameunit(sc->sc_dev), len);
2172188417Sthompsa		ifp->if_ierrors++;
2173188417Sthompsa		return;
2174188417Sthompsa	}
2175194677Sthompsa	pc = usbd_xfer_get_frame(xfer, 0);
2176194677Sthompsa	usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2177194677Sthompsa	usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
2178184610Salfred
2179188417Sthompsa	if (stat.flags & ZYD_RX_ERROR) {
2180188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV,
2181188417Sthompsa		    "%s: RX status indicated error (%x)\n",
2182188417Sthompsa		    device_get_nameunit(sc->sc_dev), stat.flags);
2183188417Sthompsa		ifp->if_ierrors++;
2184188417Sthompsa		return;
2185188417Sthompsa	}
2186184610Salfred
2187188417Sthompsa	/* compute actual frame length */
2188188417Sthompsa	rlen = len - sizeof(struct zyd_plcphdr) -
2189188417Sthompsa	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
2190184610Salfred
2191188417Sthompsa	/* allocate a mbuf to store the frame */
2192188417Sthompsa	if (rlen > MCLBYTES) {
2193188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
2194188417Sthompsa		    device_get_nameunit(sc->sc_dev), rlen);
2195188417Sthompsa		ifp->if_ierrors++;
2196188417Sthompsa		return;
2197188417Sthompsa	} else if (rlen > MHLEN)
2198188417Sthompsa		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2199188417Sthompsa	else
2200188417Sthompsa		m = m_gethdr(M_DONTWAIT, MT_DATA);
2201188417Sthompsa	if (m == NULL) {
2202188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
2203188417Sthompsa		    device_get_nameunit(sc->sc_dev));
2204188417Sthompsa		ifp->if_ierrors++;
2205188417Sthompsa		return;
2206184610Salfred	}
2207188417Sthompsa	m->m_pkthdr.rcvif = ifp;
2208188417Sthompsa	m->m_pkthdr.len = m->m_len = rlen;
2209194677Sthompsa	usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
2210184610Salfred
2211192468Ssam	if (ieee80211_radiotap_active(ic)) {
2212188417Sthompsa		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
2213184610Salfred
2214188417Sthompsa		tap->wr_flags = 0;
2215188417Sthompsa		if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
2216188417Sthompsa			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2217188417Sthompsa		/* XXX toss, no way to express errors */
2218188417Sthompsa		if (stat.flags & ZYD_RX_DECRYPTERR)
2219188417Sthompsa			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2220188417Sthompsa		tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
2221188417Sthompsa		    (stat.flags & ZYD_RX_OFDM) ?
2222188417Sthompsa			IEEE80211_T_OFDM : IEEE80211_T_CCK);
2223188417Sthompsa		tap->wr_antsignal = stat.rssi + -95;
2224188417Sthompsa		tap->wr_antnoise = -95;	/* XXX */
2225188417Sthompsa	}
2226188417Sthompsa	rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
2227184610Salfred
2228188417Sthompsa	sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
2229188417Sthompsa	sc->sc_rx_data[sc->sc_rx_count].m = m;
2230188417Sthompsa	sc->sc_rx_count++;
2231184610Salfred}
2232184610Salfred
2233184610Salfredstatic void
2234194677Sthompsazyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
2235184610Salfred{
2236194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2237184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2238184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
2239188417Sthompsa	struct ieee80211_node *ni;
2240188417Sthompsa	struct zyd_rx_desc desc;
2241188417Sthompsa	struct mbuf *m;
2242194677Sthompsa	struct usb_page_cache *pc;
2243188417Sthompsa	uint32_t offset;
2244188417Sthompsa	uint8_t rssi;
2245188417Sthompsa	int8_t nf;
2246188417Sthompsa	int i;
2247194677Sthompsa	int actlen;
2248184610Salfred
2249194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2250194677Sthompsa
2251188417Sthompsa	sc->sc_rx_count = 0;
2252188417Sthompsa	switch (USB_GET_STATE(xfer)) {
2253188417Sthompsa	case USB_ST_TRANSFERRED:
2254194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
2255194677Sthompsa		usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
2256184610Salfred
2257188417Sthompsa		offset = 0;
2258188417Sthompsa		if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
2259188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_RECV,
2260188417Sthompsa			    "%s: received multi-frame transfer\n", __func__);
2261184610Salfred
2262188417Sthompsa			for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2263188417Sthompsa				uint16_t len16 = UGETW(desc.len[i]);
2264184610Salfred
2265194677Sthompsa				if (len16 == 0 || len16 > actlen)
2266188417Sthompsa					break;
2267184610Salfred
2268188417Sthompsa				zyd_rx_data(xfer, offset, len16);
2269184610Salfred
2270188417Sthompsa				/* next frame is aligned on a 32-bit boundary */
2271188417Sthompsa				len16 = (len16 + 3) & ~3;
2272188417Sthompsa				offset += len16;
2273194677Sthompsa				if (len16 > actlen)
2274188417Sthompsa					break;
2275194677Sthompsa				actlen -= len16;
2276188417Sthompsa			}
2277188417Sthompsa		} else {
2278188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_RECV,
2279188417Sthompsa			    "%s: received single-frame transfer\n", __func__);
2280184610Salfred
2281194677Sthompsa			zyd_rx_data(xfer, 0, actlen);
2282188417Sthompsa		}
2283188417Sthompsa		/* FALLTHROUGH */
2284188417Sthompsa	case USB_ST_SETUP:
2285188417Sthompsatr_setup:
2286194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2287194228Sthompsa		usbd_transfer_submit(xfer);
2288184610Salfred
2289188417Sthompsa		/*
2290188417Sthompsa		 * At the end of a USB callback it is always safe to unlock
2291188417Sthompsa		 * the private mutex of a device! That is why we do the
2292188417Sthompsa		 * "ieee80211_input" here, and not some lines up!
2293188417Sthompsa		 */
2294188417Sthompsa		ZYD_UNLOCK(sc);
2295188417Sthompsa		for (i = 0; i < sc->sc_rx_count; i++) {
2296188417Sthompsa			rssi = sc->sc_rx_data[i].rssi;
2297188417Sthompsa			m = sc->sc_rx_data[i].m;
2298188417Sthompsa			sc->sc_rx_data[i].m = NULL;
2299184610Salfred
2300188417Sthompsa			nf = -95;	/* XXX */
2301184610Salfred
2302188417Sthompsa			ni = ieee80211_find_rxnode(ic,
2303188417Sthompsa			    mtod(m, struct ieee80211_frame_min *));
2304188417Sthompsa			if (ni != NULL) {
2305192468Ssam				(void)ieee80211_input(ni, m, rssi, nf);
2306188417Sthompsa				ieee80211_free_node(ni);
2307188417Sthompsa			} else
2308192468Ssam				(void)ieee80211_input_all(ic, m, rssi, nf);
2309188417Sthompsa		}
2310198099Sweongyo		if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
2311198099Sweongyo		    !IFQ_IS_EMPTY(&ifp->if_snd))
2312198099Sweongyo			zyd_start(ifp);
2313188417Sthompsa		ZYD_LOCK(sc);
2314188417Sthompsa		break;
2315184610Salfred
2316188417Sthompsa	default:			/* Error */
2317194677Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
2318184610Salfred
2319194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2320188417Sthompsa			/* try to clear stall first */
2321194677Sthompsa			usbd_xfer_set_stall(xfer);
2322188417Sthompsa			goto tr_setup;
2323184610Salfred		}
2324188417Sthompsa		break;
2325184610Salfred	}
2326184610Salfred}
2327184610Salfred
2328184610Salfredstatic uint8_t
2329193803Sweongyozyd_plcp_signal(struct zyd_softc *sc, int rate)
2330184610Salfred{
2331184610Salfred	switch (rate) {
2332188417Sthompsa	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
2333184610Salfred	case 12:
2334184610Salfred		return (0xb);
2335184610Salfred	case 18:
2336184610Salfred		return (0xf);
2337184610Salfred	case 24:
2338184610Salfred		return (0xa);
2339184610Salfred	case 36:
2340184610Salfred		return (0xe);
2341184610Salfred	case 48:
2342184610Salfred		return (0x9);
2343184610Salfred	case 72:
2344184610Salfred		return (0xd);
2345184610Salfred	case 96:
2346184610Salfred		return (0x8);
2347184610Salfred	case 108:
2348184610Salfred		return (0xc);
2349188417Sthompsa	/* CCK rates (NB: not IEEE std, device-specific) */
2350188417Sthompsa	case 2:
2351188417Sthompsa		return (0x0);
2352188417Sthompsa	case 4:
2353188417Sthompsa		return (0x1);
2354188417Sthompsa	case 11:
2355188417Sthompsa		return (0x2);
2356188417Sthompsa	case 22:
2357188417Sthompsa		return (0x3);
2358184610Salfred	}
2359184610Salfred
2360193803Sweongyo	device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2361193803Sweongyo	return (0x0);
2362184610Salfred}
2363184610Salfred
2364184610Salfredstatic void
2365194677Sthompsazyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
2366184610Salfred{
2367194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2368184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2369192468Ssam	struct ieee80211vap *vap;
2370188417Sthompsa	struct zyd_tx_data *data;
2371188417Sthompsa	struct mbuf *m;
2372194677Sthompsa	struct usb_page_cache *pc;
2373194677Sthompsa	int actlen;
2374184610Salfred
2375194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2376194677Sthompsa
2377188417Sthompsa	switch (USB_GET_STATE(xfer)) {
2378188417Sthompsa	case USB_ST_TRANSFERRED:
2379188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2380194677Sthompsa		    actlen);
2381184610Salfred
2382188417Sthompsa		/* free resources */
2383194677Sthompsa		data = usbd_xfer_get_priv(xfer);
2384188417Sthompsa		zyd_tx_free(data, 0);
2385194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2386184610Salfred
2387188417Sthompsa		ifp->if_opackets++;
2388188417Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2389184610Salfred
2390188417Sthompsa		/* FALLTHROUGH */
2391188417Sthompsa	case USB_ST_SETUP:
2392188417Sthompsatr_setup:
2393188417Sthompsa		data = STAILQ_FIRST(&sc->tx_q);
2394188417Sthompsa		if (data) {
2395188417Sthompsa			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
2396188417Sthompsa			m = data->m;
2397184610Salfred
2398188417Sthompsa			if (m->m_pkthdr.len > ZYD_MAX_TXBUFSZ) {
2399188417Sthompsa				DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
2400188417Sthompsa				    m->m_pkthdr.len);
2401188417Sthompsa				m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
2402188417Sthompsa			}
2403194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
2404194677Sthompsa			usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2405194677Sthompsa			usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
2406188417Sthompsa			    m->m_pkthdr.len);
2407184610Salfred
2408192468Ssam			vap = data->ni->ni_vap;
2409192468Ssam			if (ieee80211_radiotap_active_vap(vap)) {
2410188417Sthompsa				struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2411184610Salfred
2412188417Sthompsa				tap->wt_flags = 0;
2413188417Sthompsa				tap->wt_rate = data->rate;
2414184610Salfred
2415192468Ssam				ieee80211_radiotap_tx(vap, m);
2416188417Sthompsa			}
2417184610Salfred
2418194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2419194677Sthompsa			usbd_xfer_set_priv(xfer, data);
2420194228Sthompsa			usbd_transfer_submit(xfer);
2421188417Sthompsa		}
2422198099Sweongyo		ZYD_UNLOCK(sc);
2423198099Sweongyo		zyd_start(ifp);
2424198099Sweongyo		ZYD_LOCK(sc);
2425188417Sthompsa		break;
2426184610Salfred
2427188417Sthompsa	default:			/* Error */
2428188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2429194677Sthompsa		    usbd_errstr(error));
2430184610Salfred
2431188419Sthompsa		ifp->if_oerrors++;
2432194677Sthompsa		data = usbd_xfer_get_priv(xfer);
2433194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2434188419Sthompsa		if (data != NULL)
2435194677Sthompsa			zyd_tx_free(data, error);
2436188419Sthompsa
2437203141Sthompsa		if (error != USB_ERR_CANCELLED) {
2438203141Sthompsa			if (error == USB_ERR_TIMEOUT)
2439203141Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2440203141Sthompsa
2441203141Sthompsa			/*
2442203141Sthompsa			 * Try to clear stall first, also if other
2443203141Sthompsa			 * errors occur, hence clearing stall
2444203141Sthompsa			 * introduces a 50 ms delay:
2445203141Sthompsa			 */
2446194677Sthompsa			usbd_xfer_set_stall(xfer);
2447188417Sthompsa			goto tr_setup;
2448188417Sthompsa		}
2449184610Salfred		break;
2450184610Salfred	}
2451184610Salfred}
2452184610Salfred
2453188417Sthompsastatic int
2454193803Sweongyozyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2455184610Salfred{
2456188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2457188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2458188417Sthompsa	struct zyd_tx_desc *desc;
2459188417Sthompsa	struct zyd_tx_data *data;
2460188417Sthompsa	struct ieee80211_frame *wh;
2461188417Sthompsa	const struct ieee80211_txparam *tp;
2462188417Sthompsa	struct ieee80211_key *k;
2463188417Sthompsa	int rate, totlen;
2464193803Sweongyo	static uint8_t ratediv[] = ZYD_TX_RATEDIV;
2465193803Sweongyo	uint8_t phy;
2466188417Sthompsa	uint16_t pktlen;
2467193803Sweongyo	uint32_t bits;
2468184610Salfred
2469188417Sthompsa	wh = mtod(m0, struct ieee80211_frame *);
2470188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
2471188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
2472188417Sthompsa	sc->tx_nfree--;
2473184610Salfred
2474193803Sweongyo	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT ||
2475193803Sweongyo	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) {
2476193803Sweongyo		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2477193803Sweongyo		rate = tp->mgmtrate;
2478188417Sthompsa	} else {
2479193803Sweongyo		tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
2480193803Sweongyo		/* for data frames */
2481193803Sweongyo		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
2482193803Sweongyo			rate = tp->mcastrate;
2483193803Sweongyo		else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2484193803Sweongyo			rate = tp->ucastrate;
2485193803Sweongyo		else {
2486206358Srpaulo			(void) ieee80211_ratectl_rate(ni, NULL, 0);
2487193803Sweongyo			rate = ni->ni_txrate;
2488193803Sweongyo		}
2489184610Salfred	}
2490184610Salfred
2491188417Sthompsa	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2492188417Sthompsa		k = ieee80211_crypto_encap(ni, m0);
2493188417Sthompsa		if (k == NULL) {
2494188417Sthompsa			m_freem(m0);
2495188417Sthompsa			return (ENOBUFS);
2496188417Sthompsa		}
2497188417Sthompsa		/* packet header may have moved, reset our local pointer */
2498188417Sthompsa		wh = mtod(m0, struct ieee80211_frame *);
2499184610Salfred	}
2500184610Salfred
2501188417Sthompsa	data->ni = ni;
2502188417Sthompsa	data->m = m0;
2503193803Sweongyo	data->rate = rate;
2504184610Salfred
2505193803Sweongyo	/* fill Tx descriptor */
2506193803Sweongyo	desc = &data->desc;
2507193803Sweongyo	phy = zyd_plcp_signal(sc, rate);
2508193803Sweongyo	desc->phy = phy;
2509193803Sweongyo	if (ZYD_RATE_IS_OFDM(rate)) {
2510193803Sweongyo		desc->phy |= ZYD_TX_PHY_OFDM;
2511193803Sweongyo		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2512193803Sweongyo			desc->phy |= ZYD_TX_PHY_5GHZ;
2513193803Sweongyo	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2514193803Sweongyo		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2515193803Sweongyo
2516188417Sthompsa	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2517188417Sthompsa	desc->len = htole16(totlen);
2518184610Salfred
2519193803Sweongyo	desc->flags = ZYD_TX_FLAG_BACKOFF;
2520188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2521188417Sthompsa		/* multicast frames are not sent at OFDM rates in 802.11b/g */
2522188417Sthompsa		if (totlen > vap->iv_rtsthreshold) {
2523188417Sthompsa			desc->flags |= ZYD_TX_FLAG_RTS;
2524188417Sthompsa		} else if (ZYD_RATE_IS_OFDM(rate) &&
2525188417Sthompsa		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
2526188417Sthompsa			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2527188417Sthompsa				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2528188417Sthompsa			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2529188417Sthompsa				desc->flags |= ZYD_TX_FLAG_RTS;
2530188417Sthompsa		}
2531193803Sweongyo	} else
2532193803Sweongyo		desc->flags |= ZYD_TX_FLAG_MULTICAST;
2533188417Sthompsa	if ((wh->i_fc[0] &
2534188417Sthompsa	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2535188417Sthompsa	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2536188417Sthompsa		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2537188417Sthompsa
2538184610Salfred	/* actual transmit length (XXX why +10?) */
2539193803Sweongyo	pktlen = ZYD_TX_DESC_SIZE + 10;
2540188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211)
2541184610Salfred		pktlen += totlen;
2542188417Sthompsa	desc->pktlen = htole16(pktlen);
2543184610Salfred
2544193803Sweongyo	bits = (rate == 11) ? (totlen * 16) + 10 :
2545193803Sweongyo	    ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2546196809Sweongyo	desc->plcp_length = htole16(bits / ratediv[phy]);
2547188417Sthompsa	desc->plcp_service = 0;
2548193803Sweongyo	if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
2549193803Sweongyo		desc->plcp_service |= ZYD_PLCP_LENGEXT;
2550193803Sweongyo	desc->nextlen = 0;
2551193803Sweongyo
2552193803Sweongyo	if (ieee80211_radiotap_active_vap(vap)) {
2553193803Sweongyo		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2554193803Sweongyo
2555193803Sweongyo		tap->wt_flags = 0;
2556193803Sweongyo		tap->wt_rate = rate;
2557193803Sweongyo
2558193803Sweongyo		ieee80211_radiotap_tx(vap, m0);
2559184610Salfred	}
2560184610Salfred
2561188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_XMIT,
2562188417Sthompsa	    "%s: sending data frame len=%zu rate=%u\n",
2563188417Sthompsa	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2564188417Sthompsa		rate);
2565184610Salfred
2566188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2567194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
2568184610Salfred
2569188417Sthompsa	return (0);
2570184610Salfred}
2571184610Salfred
2572184610Salfredstatic void
2573188417Sthompsazyd_start(struct ifnet *ifp)
2574184610Salfred{
2575188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2576188417Sthompsa	struct ieee80211_node *ni;
2577184610Salfred	struct mbuf *m;
2578184610Salfred
2579188417Sthompsa	ZYD_LOCK(sc);
2580188417Sthompsa	for (;;) {
2581188417Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2582188417Sthompsa		if (m == NULL)
2583184610Salfred			break;
2584188417Sthompsa		if (sc->tx_nfree == 0) {
2585188417Sthompsa			IFQ_DRV_PREPEND(&ifp->if_snd, m);
2586188417Sthompsa			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2587184610Salfred			break;
2588184610Salfred		}
2589188417Sthompsa		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2590193803Sweongyo		if (zyd_tx_start(sc, m, ni) != 0) {
2591188417Sthompsa			ieee80211_free_node(ni);
2592188417Sthompsa			ifp->if_oerrors++;
2593188417Sthompsa			break;
2594184610Salfred		}
2595184610Salfred	}
2596188417Sthompsa	ZYD_UNLOCK(sc);
2597184610Salfred}
2598184610Salfred
2599188417Sthompsastatic int
2600188417Sthompsazyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2601188417Sthompsa	const struct ieee80211_bpf_params *params)
2602184610Salfred{
2603188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2604188417Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2605188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2606184610Salfred
2607188417Sthompsa	ZYD_LOCK(sc);
2608188417Sthompsa	/* prevent management frames from being sent if we're not ready */
2609188417Sthompsa	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2610188417Sthompsa		ZYD_UNLOCK(sc);
2611188417Sthompsa		m_freem(m);
2612188417Sthompsa		ieee80211_free_node(ni);
2613188417Sthompsa		return (ENETDOWN);
2614188417Sthompsa	}
2615188417Sthompsa	if (sc->tx_nfree == 0) {
2616188417Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2617188417Sthompsa		ZYD_UNLOCK(sc);
2618188417Sthompsa		m_freem(m);
2619188417Sthompsa		ieee80211_free_node(ni);
2620188417Sthompsa		return (ENOBUFS);		/* XXX */
2621188417Sthompsa	}
2622188417Sthompsa
2623188417Sthompsa	/*
2624188417Sthompsa	 * Legacy path; interpret frame contents to decide
2625188417Sthompsa	 * precisely how to send the frame.
2626188417Sthompsa	 * XXX raw path
2627188417Sthompsa	 */
2628193803Sweongyo	if (zyd_tx_start(sc, m, ni) != 0) {
2629188417Sthompsa		ZYD_UNLOCK(sc);
2630188417Sthompsa		ifp->if_oerrors++;
2631188417Sthompsa		ieee80211_free_node(ni);
2632188417Sthompsa		return (EIO);
2633188417Sthompsa	}
2634188417Sthompsa	ZYD_UNLOCK(sc);
2635188417Sthompsa	return (0);
2636184610Salfred}
2637184610Salfred
2638184610Salfredstatic int
2639188417Sthompsazyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2640184610Salfred{
2641184610Salfred	struct zyd_softc *sc = ifp->if_softc;
2642184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
2643188417Sthompsa	struct ifreq *ifr = (struct ifreq *) data;
2644188417Sthompsa	int error = 0, startall = 0;
2645184610Salfred
2646184610Salfred	switch (cmd) {
2647184610Salfred	case SIOCSIFFLAGS:
2648188417Sthompsa		ZYD_LOCK(sc);
2649184610Salfred		if (ifp->if_flags & IFF_UP) {
2650191746Sthompsa			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2651191746Sthompsa				zyd_init_locked(sc);
2652188417Sthompsa				startall = 1;
2653191746Sthompsa			} else
2654191746Sthompsa				zyd_set_multi(sc);
2655184610Salfred		} else {
2656191746Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2657191746Sthompsa				zyd_stop(sc);
2658184610Salfred		}
2659188417Sthompsa		ZYD_UNLOCK(sc);
2660188417Sthompsa		if (startall)
2661188417Sthompsa			ieee80211_start_all(ic);
2662184610Salfred		break;
2663184610Salfred	case SIOCGIFMEDIA:
2664188417Sthompsa		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2665184610Salfred		break;
2666188417Sthompsa	case SIOCGIFADDR:
2667184610Salfred		error = ether_ioctl(ifp, cmd, data);
2668184610Salfred		break;
2669188417Sthompsa	default:
2670188417Sthompsa		error = EINVAL;
2671188417Sthompsa		break;
2672184610Salfred	}
2673184610Salfred	return (error);
2674184610Salfred}
2675184610Salfred
2676184610Salfredstatic void
2677191746Sthompsazyd_init_locked(struct zyd_softc *sc)
2678184610Salfred{
2679188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2680188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2681192984Sthompsa	struct usb_config_descriptor *cd;
2682188419Sthompsa	int error;
2683188417Sthompsa	uint32_t val;
2684184610Salfred
2685188417Sthompsa	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2686184610Salfred
2687188417Sthompsa	if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
2688188417Sthompsa		error = zyd_loadfirmware(sc);
2689188417Sthompsa		if (error != 0) {
2690188417Sthompsa			device_printf(sc->sc_dev,
2691188417Sthompsa			    "could not load firmware (error=%d)\n", error);
2692188417Sthompsa			goto fail;
2693188417Sthompsa		}
2694188417Sthompsa
2695188417Sthompsa		/* reset device */
2696194228Sthompsa		cd = usbd_get_config_descriptor(sc->sc_udev);
2697194228Sthompsa		error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
2698188417Sthompsa		    cd->bConfigurationValue);
2699188417Sthompsa		if (error)
2700188417Sthompsa			device_printf(sc->sc_dev, "reset failed, continuing\n");
2701188417Sthompsa
2702188417Sthompsa		error = zyd_hw_init(sc);
2703188417Sthompsa		if (error) {
2704188417Sthompsa			device_printf(sc->sc_dev,
2705188417Sthompsa			    "hardware initialization failed\n");
2706188417Sthompsa			goto fail;
2707188417Sthompsa		}
2708188417Sthompsa
2709188417Sthompsa		device_printf(sc->sc_dev,
2710188417Sthompsa		    "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
2711188417Sthompsa		    "BE%x NP%x Gain%x F%x\n",
2712188417Sthompsa		    (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
2713188417Sthompsa		    sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
2714188417Sthompsa		    zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
2715188417Sthompsa		    sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
2716188417Sthompsa		    sc->sc_cckgain, sc->sc_fix_cr157);
2717188417Sthompsa
2718188417Sthompsa		/* read regulatory domain (currently unused) */
2719188417Sthompsa		zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
2720188417Sthompsa		sc->sc_regdomain = val >> 16;
2721188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
2722188417Sthompsa		    sc->sc_regdomain);
2723188417Sthompsa
2724188417Sthompsa		/* we'll do software WEP decryption for now */
2725188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
2726188417Sthompsa		    __func__);
2727188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2728188417Sthompsa
2729188417Sthompsa		sc->sc_flags |= ZYD_FLAG_INITONCE;
2730184610Salfred	}
2731184610Salfred
2732188417Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2733191746Sthompsa		zyd_stop(sc);
2734188417Sthompsa
2735190526Ssam	DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
2736190526Ssam	    IF_LLADDR(ifp), ":");
2737190526Ssam	error = zyd_set_macaddr(sc, IF_LLADDR(ifp));
2738188417Sthompsa	if (error != 0)
2739184610Salfred		return;
2740184610Salfred
2741188417Sthompsa	/* set basic rates */
2742188417Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B)
2743188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
2744188417Sthompsa	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2745188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
2746188417Sthompsa	else	/* assumes 802.11b/g */
2747188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
2748184610Salfred
2749188417Sthompsa	/* promiscuous mode */
2750188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
2751188417Sthompsa	/* multicast setup */
2752188417Sthompsa	zyd_set_multi(sc);
2753188417Sthompsa	/* set RX filter  */
2754188417Sthompsa	error = zyd_set_rxfilter(sc);
2755188417Sthompsa	if (error != 0)
2756188417Sthompsa		goto fail;
2757184610Salfred
2758188417Sthompsa	/* switch radio transmitter ON */
2759188417Sthompsa	error = zyd_switch_radio(sc, 1);
2760188417Sthompsa	if (error != 0)
2761188417Sthompsa		goto fail;
2762188417Sthompsa	/* set default BSS channel */
2763188417Sthompsa	zyd_set_chan(sc, ic->ic_curchan);
2764184610Salfred
2765188417Sthompsa	/*
2766188417Sthompsa	 * Allocate Tx and Rx xfer queues.
2767188417Sthompsa	 */
2768188419Sthompsa	zyd_setup_tx_list(sc);
2769184610Salfred
2770188417Sthompsa	/* enable interrupts */
2771188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2772184610Salfred
2773188417Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2774188417Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2775194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2776194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2777194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
2778184610Salfred
2779188417Sthompsa	return;
2780184610Salfred
2781191746Sthompsafail:	zyd_stop(sc);
2782188417Sthompsa	return;
2783184610Salfred}
2784184610Salfred
2785184610Salfredstatic void
2786188417Sthompsazyd_init(void *priv)
2787184610Salfred{
2788188417Sthompsa	struct zyd_softc *sc = priv;
2789188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2790188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2791184610Salfred
2792188417Sthompsa	ZYD_LOCK(sc);
2793191746Sthompsa	zyd_init_locked(sc);
2794188417Sthompsa	ZYD_UNLOCK(sc);
2795188417Sthompsa
2796188417Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2797188417Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2798184610Salfred}
2799184610Salfred
2800184610Salfredstatic void
2801191746Sthompsazyd_stop(struct zyd_softc *sc)
2802184610Salfred{
2803188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2804188417Sthompsa	int error;
2805184610Salfred
2806188417Sthompsa	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2807184610Salfred
2808188417Sthompsa	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2809184610Salfred
2810188417Sthompsa	/*
2811188419Sthompsa	 * Drain all the transfers, if not already drained:
2812188417Sthompsa	 */
2813188419Sthompsa	ZYD_UNLOCK(sc);
2814194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2815194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
2816188419Sthompsa	ZYD_LOCK(sc);
2817188417Sthompsa
2818188419Sthompsa	zyd_unsetup_tx_list(sc);
2819188417Sthompsa
2820188419Sthompsa	/* Stop now if the device was never set up */
2821188419Sthompsa	if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
2822184610Salfred		return;
2823184610Salfred
2824188417Sthompsa	/* switch radio transmitter OFF */
2825188417Sthompsa	error = zyd_switch_radio(sc, 0);
2826188417Sthompsa	if (error != 0)
2827188417Sthompsa		goto fail;
2828188417Sthompsa	/* disable Rx */
2829188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
2830188417Sthompsa	/* disable interrupts */
2831188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
2832184610Salfred
2833188417Sthompsafail:
2834188417Sthompsa	return;
2835184610Salfred}
2836184610Salfred
2837188417Sthompsastatic int
2838188417Sthompsazyd_loadfirmware(struct zyd_softc *sc)
2839184610Salfred{
2840192984Sthompsa	struct usb_device_request req;
2841188417Sthompsa	size_t size;
2842188417Sthompsa	u_char *fw;
2843188417Sthompsa	uint8_t stat;
2844188417Sthompsa	uint16_t addr;
2845184610Salfred
2846188417Sthompsa	if (sc->sc_flags & ZYD_FLAG_FWLOADED)
2847188417Sthompsa		return (0);
2848184610Salfred
2849188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211) {
2850188417Sthompsa		fw = (u_char *)zd1211_firmware;
2851188417Sthompsa		size = sizeof(zd1211_firmware);
2852188417Sthompsa	} else {
2853188417Sthompsa		fw = (u_char *)zd1211b_firmware;
2854188417Sthompsa		size = sizeof(zd1211b_firmware);
2855184610Salfred	}
2856184610Salfred
2857188417Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2858188417Sthompsa	req.bRequest = ZYD_DOWNLOADREQ;
2859188417Sthompsa	USETW(req.wIndex, 0);
2860184610Salfred
2861188417Sthompsa	addr = ZYD_FIRMWARE_START_ADDR;
2862188417Sthompsa	while (size > 0) {
2863188417Sthompsa		/*
2864188417Sthompsa		 * When the transfer size is 4096 bytes, it is not
2865188417Sthompsa		 * likely to be able to transfer it.
2866188417Sthompsa		 * The cause is port or machine or chip?
2867188417Sthompsa		 */
2868188417Sthompsa		const int mlen = min(size, 64);
2869184610Salfred
2870188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_FW,
2871188417Sthompsa		    "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
2872184610Salfred
2873188417Sthompsa		USETW(req.wValue, addr);
2874188417Sthompsa		USETW(req.wLength, mlen);
2875188419Sthompsa		if (zyd_do_request(sc, &req, fw) != 0)
2876188417Sthompsa			return (EIO);
2877184610Salfred
2878188417Sthompsa		addr += mlen / 2;
2879188417Sthompsa		fw   += mlen;
2880188417Sthompsa		size -= mlen;
2881184610Salfred	}
2882184610Salfred
2883188417Sthompsa	/* check whether the upload succeeded */
2884188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2885188417Sthompsa	req.bRequest = ZYD_DOWNLOADSTS;
2886188417Sthompsa	USETW(req.wValue, 0);
2887188417Sthompsa	USETW(req.wIndex, 0);
2888188417Sthompsa	USETW(req.wLength, sizeof(stat));
2889188419Sthompsa	if (zyd_do_request(sc, &req, &stat) != 0)
2890188417Sthompsa		return (EIO);
2891184610Salfred
2892188417Sthompsa	sc->sc_flags |= ZYD_FLAG_FWLOADED;
2893184610Salfred
2894188417Sthompsa	return (stat & 0x80) ? (EIO) : (0);
2895184610Salfred}
2896184610Salfred
2897184610Salfredstatic void
2898188417Sthompsazyd_scan_start(struct ieee80211com *ic)
2899184610Salfred{
2900191746Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2901191746Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2902184610Salfred
2903188417Sthompsa	ZYD_LOCK(sc);
2904191746Sthompsa	/* want broadcast address while scanning */
2905191746Sthompsa	zyd_set_bssid(sc, ifp->if_broadcastaddr);
2906188417Sthompsa	ZYD_UNLOCK(sc);
2907184610Salfred}
2908184610Salfred
2909184610Salfredstatic void
2910188417Sthompsazyd_scan_end(struct ieee80211com *ic)
2911184610Salfred{
2912188417Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
2913184610Salfred
2914188417Sthompsa	ZYD_LOCK(sc);
2915191746Sthompsa	/* restore previous bssid */
2916191746Sthompsa	zyd_set_bssid(sc, sc->sc_bssid);
2917188417Sthompsa	ZYD_UNLOCK(sc);
2918184610Salfred}
2919184610Salfred
2920184610Salfredstatic void
2921188417Sthompsazyd_set_channel(struct ieee80211com *ic)
2922184610Salfred{
2923188417Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
2924184610Salfred
2925188417Sthompsa	ZYD_LOCK(sc);
2926191746Sthompsa	zyd_set_chan(sc, ic->ic_curchan);
2927188417Sthompsa	ZYD_UNLOCK(sc);
2928184610Salfred}
2929184610Salfred
2930188417Sthompsastatic device_method_t zyd_methods[] = {
2931188417Sthompsa        /* Device interface */
2932188417Sthompsa        DEVMETHOD(device_probe, zyd_match),
2933188417Sthompsa        DEVMETHOD(device_attach, zyd_attach),
2934188417Sthompsa        DEVMETHOD(device_detach, zyd_detach),
2935191746Sthompsa
2936188417Sthompsa	{ 0, 0 }
2937188417Sthompsa};
2938184610Salfred
2939188417Sthompsastatic driver_t zyd_driver = {
2940188417Sthompsa        "zyd",
2941188417Sthompsa        zyd_methods,
2942188417Sthompsa        sizeof(struct zyd_softc)
2943188417Sthompsa};
2944184610Salfred
2945188417Sthompsastatic devclass_t zyd_devclass;
2946184610Salfred
2947189275SthompsaDRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0);
2948188942SthompsaMODULE_DEPEND(zyd, usb, 1, 1, 1);
2949188417SthompsaMODULE_DEPEND(zyd, wlan, 1, 1, 1);
2950