if_zyd.c revision 228303
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 228303 2011-12-06 07:55:25Z hselasky $	*/
4184610Salfred
5184610Salfred/*-
6184610Salfred * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr>
7184610Salfred * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de>
8184610Salfred *
9184610Salfred * Permission to use, copy, modify, and distribute this software for any
10184610Salfred * purpose with or without fee is hereby granted, provided that the above
11184610Salfred * copyright notice and this permission notice appear in all copies.
12184610Salfred *
13184610Salfred * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14184610Salfred * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15184610Salfred * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16184610Salfred * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17184610Salfred * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18184610Salfred * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19184610Salfred * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20184610Salfred */
21184610Salfred
22184610Salfred#include <sys/cdefs.h>
23184610Salfred__FBSDID("$FreeBSD: head/sys/dev/usb/wlan/if_zyd.c 228303 2011-12-06 07:55:25Z hselasky $");
24184610Salfred
25184610Salfred/*
26188417Sthompsa * ZyDAS ZD1211/ZD1211B USB WLAN driver.
27184610Salfred */
28184610Salfred
29191746Sthompsa#include <sys/param.h>
30191746Sthompsa#include <sys/sockio.h>
31191746Sthompsa#include <sys/sysctl.h>
32191746Sthompsa#include <sys/lock.h>
33191746Sthompsa#include <sys/mutex.h>
34194677Sthompsa#include <sys/condvar.h>
35191746Sthompsa#include <sys/mbuf.h>
36191746Sthompsa#include <sys/kernel.h>
37191746Sthompsa#include <sys/socket.h>
38191746Sthompsa#include <sys/systm.h>
39191746Sthompsa#include <sys/malloc.h>
40191746Sthompsa#include <sys/module.h>
41191746Sthompsa#include <sys/bus.h>
42191746Sthompsa#include <sys/endian.h>
43191746Sthompsa#include <sys/kdb.h>
44191746Sthompsa
45191746Sthompsa#include <machine/bus.h>
46191746Sthompsa#include <machine/resource.h>
47191746Sthompsa#include <sys/rman.h>
48191746Sthompsa
49191746Sthompsa#include <net/bpf.h>
50191746Sthompsa#include <net/if.h>
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
78207077Sthompsa#ifdef USB_DEBUG
79184610Salfredstatic int zyd_debug = 0;
80184610Salfred
81227309Sedstatic SYSCTL_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) }
203223486Shselaskystatic const STRUCT_USB_HOST_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 */
232223566Sgavin	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG_NF),
233193419Sweongyo	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
234193419Sweongyo	ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
235193419Sweongyo	ZYD_ZD1211B_DEV(ASUS, A9T_WIFI),
236193419Sweongyo	ZYD_ZD1211B_DEV(BELKIN, F5D7050_V4000),
237193419Sweongyo	ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
238193419Sweongyo	ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
239193419Sweongyo	ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
240193419Sweongyo	ZYD_ZD1211B_DEV(MELCO, KG54L),
241193419Sweongyo	ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
242193419Sweongyo	ZYD_ZD1211B_DEV(PLANEX2, GW_US54GXS),
243193419Sweongyo	ZYD_ZD1211B_DEV(SAGEM, XG76NA),
244193419Sweongyo	ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
245193419Sweongyo	ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
246193419Sweongyo	ZYD_ZD1211B_DEV(USR, USR5423),
247193419Sweongyo	ZYD_ZD1211B_DEV(VTECH, ZD1211B),
248193419Sweongyo	ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
249193419Sweongyo	ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
250193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, M202),
251193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, G202),
252193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, G220V2)
253184610Salfred};
254184610Salfred
255192984Sthompsastatic const struct usb_config zyd_config[ZYD_N_TRANSFER] = {
256188417Sthompsa	[ZYD_BULK_WR] = {
257184610Salfred		.type = UE_BULK,
258184610Salfred		.endpoint = UE_ADDR_ANY,
259184610Salfred		.direction = UE_DIR_OUT,
260190734Sthompsa		.bufsize = ZYD_MAX_TXBUFSZ,
261190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
262190734Sthompsa		.callback = zyd_bulk_write_callback,
263184610Salfred		.ep_index = 0,
264190734Sthompsa		.timeout = 10000,	/* 10 seconds */
265184610Salfred	},
266188417Sthompsa	[ZYD_BULK_RD] = {
267184610Salfred		.type = UE_BULK,
268184610Salfred		.endpoint = UE_ADDR_ANY,
269184610Salfred		.direction = UE_DIR_IN,
270190734Sthompsa		.bufsize = ZYX_MAX_RXBUFSZ,
271190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
272190734Sthompsa		.callback = zyd_bulk_read_callback,
273184610Salfred		.ep_index = 0,
274184610Salfred	},
275188417Sthompsa	[ZYD_INTR_WR] = {
276184610Salfred		.type = UE_BULK_INTR,
277184610Salfred		.endpoint = UE_ADDR_ANY,
278184610Salfred		.direction = UE_DIR_OUT,
279190734Sthompsa		.bufsize = sizeof(struct zyd_cmd),
280190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
281190734Sthompsa		.callback = zyd_intr_write_callback,
282190734Sthompsa		.timeout = 1000,	/* 1 second */
283184610Salfred		.ep_index = 1,
284184610Salfred	},
285188417Sthompsa	[ZYD_INTR_RD] = {
286188417Sthompsa		.type = UE_INTERRUPT,
287184610Salfred		.endpoint = UE_ADDR_ANY,
288184610Salfred		.direction = UE_DIR_IN,
289190734Sthompsa		.bufsize = sizeof(struct zyd_cmd),
290190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
291190734Sthompsa		.callback = zyd_intr_read_callback,
292184610Salfred	},
293184610Salfred};
294188417Sthompsa#define zyd_read16_m(sc, val, data)	do {				\
295188417Sthompsa	error = zyd_read16(sc, val, data);				\
296188417Sthompsa	if (error != 0)							\
297188417Sthompsa		goto fail;						\
298188417Sthompsa} while (0)
299188417Sthompsa#define zyd_write16_m(sc, val, data)	do {				\
300188417Sthompsa	error = zyd_write16(sc, val, data);				\
301188417Sthompsa	if (error != 0)							\
302188417Sthompsa		goto fail;						\
303188417Sthompsa} while (0)
304188417Sthompsa#define zyd_read32_m(sc, val, data)	do {				\
305188417Sthompsa	error = zyd_read32(sc, val, data);				\
306188417Sthompsa	if (error != 0)							\
307188417Sthompsa		goto fail;						\
308188417Sthompsa} while (0)
309188417Sthompsa#define zyd_write32_m(sc, val, data)	do {				\
310188417Sthompsa	error = zyd_write32(sc, val, data);				\
311188417Sthompsa	if (error != 0)							\
312188417Sthompsa		goto fail;						\
313188417Sthompsa} while (0)
314184610Salfred
315188417Sthompsastatic int
316188417Sthompsazyd_match(device_t dev)
317184610Salfred{
318192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
319184610Salfred
320192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
321188417Sthompsa		return (ENXIO);
322188419Sthompsa	if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX)
323188417Sthompsa		return (ENXIO);
324188417Sthompsa	if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX)
325188417Sthompsa		return (ENXIO);
326184610Salfred
327194228Sthompsa	return (usbd_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa));
328184610Salfred}
329184610Salfred
330188417Sthompsastatic int
331188417Sthompsazyd_attach(device_t dev)
332184610Salfred{
333192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
334188417Sthompsa	struct zyd_softc *sc = device_get_softc(dev);
335191746Sthompsa	struct ifnet *ifp;
336191746Sthompsa	struct ieee80211com *ic;
337191746Sthompsa	uint8_t iface_index, bands;
338188419Sthompsa	int error;
339184610Salfred
340188417Sthompsa	if (uaa->info.bcdDevice < 0x4330) {
341188417Sthompsa		device_printf(dev, "device version mismatch: 0x%X "
342188417Sthompsa		    "(only >= 43.30 supported)\n",
343188417Sthompsa		    uaa->info.bcdDevice);
344188417Sthompsa		return (EINVAL);
345184610Salfred	}
346184610Salfred
347194228Sthompsa	device_set_usb_desc(dev);
348188417Sthompsa	sc->sc_dev = dev;
349188417Sthompsa	sc->sc_udev = uaa->device;
350188417Sthompsa	sc->sc_macrev = USB_GET_DRIVER_INFO(uaa);
351188419Sthompsa
352188417Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
353188417Sthompsa	    MTX_NETWORK_LOCK, MTX_DEF);
354188417Sthompsa	STAILQ_INIT(&sc->sc_rqh);
355184610Salfred
356188417Sthompsa	iface_index = ZYD_IFACE_INDEX;
357194228Sthompsa	error = usbd_transfer_setup(uaa->device,
358188417Sthompsa	    &iface_index, sc->sc_xfer, zyd_config,
359188417Sthompsa	    ZYD_N_TRANSFER, sc, &sc->sc_mtx);
360188417Sthompsa	if (error) {
361188417Sthompsa		device_printf(dev, "could not allocate USB transfers, "
362194228Sthompsa		    "err=%s\n", usbd_errstr(error));
363188419Sthompsa		goto detach;
364184610Salfred	}
365184610Salfred
366188419Sthompsa	ZYD_LOCK(sc);
367188417Sthompsa	if ((error = zyd_get_macaddr(sc)) != 0) {
368188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
369191746Sthompsa		ZYD_UNLOCK(sc);
370191746Sthompsa		goto detach;
371188417Sthompsa	}
372188419Sthompsa	ZYD_UNLOCK(sc);
373188419Sthompsa
374188417Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
375188417Sthompsa	if (ifp == NULL) {
376188419Sthompsa		device_printf(sc->sc_dev, "can not if_alloc()\n");
377191746Sthompsa		goto detach;
378184610Salfred	}
379188417Sthompsa	ifp->if_softc = sc;
380188417Sthompsa	if_initname(ifp, "zyd", device_get_unit(sc->sc_dev));
381188417Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
382188417Sthompsa	ifp->if_init = zyd_init;
383188417Sthompsa	ifp->if_ioctl = zyd_ioctl;
384188417Sthompsa	ifp->if_start = zyd_start;
385207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
386188417Sthompsa	IFQ_SET_READY(&ifp->if_snd);
387184610Salfred
388188417Sthompsa	ic = ifp->if_l2com;
389188417Sthompsa	ic->ic_ifp = ifp;
390188417Sthompsa	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
391188417Sthompsa	ic->ic_opmode = IEEE80211_M_STA;
392184610Salfred
393188417Sthompsa	/* set device capabilities */
394188417Sthompsa	ic->ic_caps =
395188417Sthompsa		  IEEE80211_C_STA		/* station mode */
396188417Sthompsa		| IEEE80211_C_MONITOR		/* monitor mode */
397188417Sthompsa		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
398188417Sthompsa	        | IEEE80211_C_SHSLOT		/* short slot time supported */
399188417Sthompsa		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
400188417Sthompsa	        | IEEE80211_C_WPA		/* 802.11i */
401188417Sthompsa		;
402184610Salfred
403188417Sthompsa	bands = 0;
404188417Sthompsa	setbit(&bands, IEEE80211_MODE_11B);
405188417Sthompsa	setbit(&bands, IEEE80211_MODE_11G);
406188417Sthompsa	ieee80211_init_channels(ic, NULL, &bands);
407184610Salfred
408190526Ssam	ieee80211_ifattach(ic, sc->sc_bssid);
409188417Sthompsa	ic->ic_raw_xmit = zyd_raw_xmit;
410188417Sthompsa	ic->ic_scan_start = zyd_scan_start;
411188417Sthompsa	ic->ic_scan_end = zyd_scan_end;
412188417Sthompsa	ic->ic_set_channel = zyd_set_channel;
413184610Salfred
414188417Sthompsa	ic->ic_vap_create = zyd_vap_create;
415188417Sthompsa	ic->ic_vap_delete = zyd_vap_delete;
416188417Sthompsa	ic->ic_update_mcast = zyd_update_mcast;
417188601Sthompsa	ic->ic_update_promisc = zyd_update_mcast;
418184610Salfred
419192468Ssam	ieee80211_radiotap_attach(ic,
420192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
421192468Ssam		ZYD_TX_RADIOTAP_PRESENT,
422192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
423192468Ssam		ZYD_RX_RADIOTAP_PRESENT);
424184610Salfred
425188417Sthompsa	if (bootverbose)
426188417Sthompsa		ieee80211_announce(ic);
427184610Salfred
428191746Sthompsa	return (0);
429191746Sthompsa
430191746Sthompsadetach:
431191746Sthompsa	zyd_detach(dev);
432191746Sthompsa	return (ENXIO);			/* failure */
433184610Salfred}
434184610Salfred
435188417Sthompsastatic int
436188417Sthompsazyd_detach(device_t dev)
437184610Salfred{
438188417Sthompsa	struct zyd_softc *sc = device_get_softc(dev);
439188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
440188601Sthompsa	struct ieee80211com *ic;
441184610Salfred
442188419Sthompsa	/* stop all USB transfers */
443194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER);
444184610Salfred
445188419Sthompsa	/* free TX list, if any */
446188419Sthompsa	zyd_unsetup_tx_list(sc);
447188419Sthompsa
448188417Sthompsa	if (ifp) {
449188601Sthompsa		ic = ifp->if_l2com;
450188417Sthompsa		ieee80211_ifdetach(ic);
451188417Sthompsa		if_free(ifp);
452184610Salfred	}
453188417Sthompsa	mtx_destroy(&sc->sc_mtx);
454184610Salfred
455188417Sthompsa	return (0);
456184610Salfred}
457184610Salfred
458188417Sthompsastatic struct ieee80211vap *
459188417Sthompsazyd_vap_create(struct ieee80211com *ic,
460188417Sthompsa	const char name[IFNAMSIZ], int unit, int opmode, int flags,
461188417Sthompsa	const uint8_t bssid[IEEE80211_ADDR_LEN],
462188417Sthompsa	const uint8_t mac[IEEE80211_ADDR_LEN])
463184610Salfred{
464188417Sthompsa	struct zyd_vap *zvp;
465188417Sthompsa	struct ieee80211vap *vap;
466184610Salfred
467188417Sthompsa	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
468188417Sthompsa		return (NULL);
469188417Sthompsa	zvp = (struct zyd_vap *) malloc(sizeof(struct zyd_vap),
470188417Sthompsa	    M_80211_VAP, M_NOWAIT | M_ZERO);
471188417Sthompsa	if (zvp == NULL)
472188417Sthompsa		return (NULL);
473188417Sthompsa	vap = &zvp->vap;
474188417Sthompsa	/* enable s/w bmiss handling for sta mode */
475188417Sthompsa	ieee80211_vap_setup(ic, vap, name, unit, opmode,
476188417Sthompsa	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
477184610Salfred
478188417Sthompsa	/* override state transition machine */
479188417Sthompsa	zvp->newstate = vap->iv_newstate;
480188417Sthompsa	vap->iv_newstate = zyd_newstate;
481184610Salfred
482206358Srpaulo	ieee80211_ratectl_init(vap);
483206358Srpaulo	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
484184610Salfred
485188417Sthompsa	/* complete setup */
486188417Sthompsa	ieee80211_vap_attach(vap, ieee80211_media_change,
487188417Sthompsa	    ieee80211_media_status);
488188417Sthompsa	ic->ic_opmode = opmode;
489188417Sthompsa	return (vap);
490184610Salfred}
491184610Salfred
492184610Salfredstatic void
493188417Sthompsazyd_vap_delete(struct ieee80211vap *vap)
494184610Salfred{
495188417Sthompsa	struct zyd_vap *zvp = ZYD_VAP(vap);
496184610Salfred
497206358Srpaulo	ieee80211_ratectl_deinit(vap);
498188417Sthompsa	ieee80211_vap_detach(vap);
499188417Sthompsa	free(zvp, M_80211_VAP);
500184610Salfred}
501184610Salfred
502184610Salfredstatic void
503188417Sthompsazyd_tx_free(struct zyd_tx_data *data, int txerr)
504184610Salfred{
505188417Sthompsa	struct zyd_softc *sc = data->sc;
506184610Salfred
507188417Sthompsa	if (data->m != NULL) {
508188417Sthompsa		if (data->m->m_flags & M_TXCB)
509188417Sthompsa			ieee80211_process_callback(data->ni, data->m,
510188417Sthompsa			    txerr ? ETIMEDOUT : 0);
511188417Sthompsa		m_freem(data->m);
512188417Sthompsa		data->m = NULL;
513188417Sthompsa
514188417Sthompsa		ieee80211_free_node(data->ni);
515188417Sthompsa		data->ni = NULL;
516184610Salfred	}
517188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
518188417Sthompsa	sc->tx_nfree++;
519184610Salfred}
520184610Salfred
521188419Sthompsastatic void
522188419Sthompsazyd_setup_tx_list(struct zyd_softc *sc)
523184610Salfred{
524188417Sthompsa	struct zyd_tx_data *data;
525188417Sthompsa	int i;
526184610Salfred
527188417Sthompsa	sc->tx_nfree = 0;
528188417Sthompsa	STAILQ_INIT(&sc->tx_q);
529188417Sthompsa	STAILQ_INIT(&sc->tx_free);
530184610Salfred
531188417Sthompsa	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
532188417Sthompsa		data = &sc->tx_data[i];
533184610Salfred
534188417Sthompsa		data->sc = sc;
535188417Sthompsa		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
536188417Sthompsa		sc->tx_nfree++;
537188417Sthompsa	}
538184610Salfred}
539184610Salfred
540184610Salfredstatic void
541188419Sthompsazyd_unsetup_tx_list(struct zyd_softc *sc)
542184610Salfred{
543188417Sthompsa	struct zyd_tx_data *data;
544188417Sthompsa	int i;
545184610Salfred
546188419Sthompsa	/* make sure any subsequent use of the queues will fail */
547188419Sthompsa	sc->tx_nfree = 0;
548188419Sthompsa	STAILQ_INIT(&sc->tx_q);
549188419Sthompsa	STAILQ_INIT(&sc->tx_free);
550184610Salfred
551188419Sthompsa	/* free up all node references and mbufs */
552188417Sthompsa	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
553188417Sthompsa		data = &sc->tx_data[i];
554184610Salfred
555188417Sthompsa		if (data->m != NULL) {
556188417Sthompsa			m_freem(data->m);
557188417Sthompsa			data->m = NULL;
558188417Sthompsa		}
559188417Sthompsa		if (data->ni != NULL) {
560188417Sthompsa			ieee80211_free_node(data->ni);
561188417Sthompsa			data->ni = NULL;
562188417Sthompsa		}
563188417Sthompsa	}
564184610Salfred}
565184610Salfred
566191746Sthompsastatic int
567191746Sthompsazyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
568186730Salfred{
569188417Sthompsa	struct zyd_vap *zvp = ZYD_VAP(vap);
570191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
571191746Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
572188417Sthompsa	int error;
573186730Salfred
574191746Sthompsa	DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__,
575191746Sthompsa	    ieee80211_state_name[vap->iv_state],
576191746Sthompsa	    ieee80211_state_name[nstate]);
577191746Sthompsa
578191746Sthompsa	IEEE80211_UNLOCK(ic);
579191746Sthompsa	ZYD_LOCK(sc);
580191746Sthompsa	switch (nstate) {
581188417Sthompsa	case IEEE80211_S_AUTH:
582188417Sthompsa		zyd_set_chan(sc, ic->ic_curchan);
583188417Sthompsa		break;
584188417Sthompsa	case IEEE80211_S_RUN:
585188417Sthompsa		if (vap->iv_opmode == IEEE80211_M_MONITOR)
586188417Sthompsa			break;
587184610Salfred
588188417Sthompsa		/* turn link LED on */
589188417Sthompsa		error = zyd_set_led(sc, ZYD_LED1, 1);
590188417Sthompsa		if (error != 0)
591191746Sthompsa			break;
592191746Sthompsa
593188417Sthompsa		/* make data LED blink upon Tx */
594188417Sthompsa		zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1);
595191746Sthompsa
596212127Sthompsa		IEEE80211_ADDR_COPY(sc->sc_bssid, vap->iv_bss->ni_bssid);
597188417Sthompsa		zyd_set_bssid(sc, sc->sc_bssid);
598188417Sthompsa		break;
599188417Sthompsa	default:
600188417Sthompsa		break;
601184610Salfred	}
602188417Sthompsafail:
603188417Sthompsa	ZYD_UNLOCK(sc);
604188417Sthompsa	IEEE80211_LOCK(ic);
605191746Sthompsa	return (zvp->newstate(vap, nstate, arg));
606184610Salfred}
607184610Salfred
608188417Sthompsa/*
609188417Sthompsa * Callback handler for interrupt transfer
610188417Sthompsa */
611184610Salfredstatic void
612194677Sthompsazyd_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
613184610Salfred{
614194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
615184610Salfred	struct ifnet *ifp = sc->sc_ifp;
616184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
617188417Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
618184610Salfred	struct ieee80211_node *ni;
619188417Sthompsa	struct zyd_cmd *cmd = &sc->sc_ibuf;
620194677Sthompsa	struct usb_page_cache *pc;
621188417Sthompsa	int datalen;
622194677Sthompsa	int actlen;
623184610Salfred
624194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
625194677Sthompsa
626184610Salfred	switch (USB_GET_STATE(xfer)) {
627184610Salfred	case USB_ST_TRANSFERRED:
628194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
629194677Sthompsa		usbd_copy_out(pc, 0, cmd, sizeof(*cmd));
630184610Salfred
631188417Sthompsa		switch (le16toh(cmd->code)) {
632188417Sthompsa		case ZYD_NOTIF_RETRYSTATUS:
633188417Sthompsa		{
634188417Sthompsa			struct zyd_notif_retry *retry =
635188417Sthompsa			    (struct zyd_notif_retry *)cmd->data;
636184610Salfred
637188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_TX_PROC,
638188417Sthompsa			    "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
639188417Sthompsa			    le16toh(retry->rate), ether_sprintf(retry->macaddr),
640188417Sthompsa			    le16toh(retry->count)&0xff, le16toh(retry->count));
641184610Salfred
642188417Sthompsa			/*
643188417Sthompsa			 * Find the node to which the packet was sent and
644188417Sthompsa			 * update its retry statistics.  In BSS mode, this node
645188417Sthompsa			 * is the AP we're associated to so no lookup is
646188417Sthompsa			 * actually needed.
647188417Sthompsa			 */
648188417Sthompsa			ni = ieee80211_find_txnode(vap, retry->macaddr);
649188417Sthompsa			if (ni != NULL) {
650206358Srpaulo				int retrycnt =
651206358Srpaulo				    (int)(le16toh(retry->count) & 0xff);
652206358Srpaulo
653206358Srpaulo				ieee80211_ratectl_tx_complete(vap, ni,
654206358Srpaulo				    IEEE80211_RATECTL_TX_FAILURE,
655206358Srpaulo				    &retrycnt, NULL);
656188417Sthompsa				ieee80211_free_node(ni);
657188417Sthompsa			}
658188417Sthompsa			if (le16toh(retry->count) & 0x100)
659188417Sthompsa				ifp->if_oerrors++;	/* too many retries */
660188417Sthompsa			break;
661188417Sthompsa		}
662188417Sthompsa		case ZYD_NOTIF_IORD:
663188417Sthompsa		{
664188417Sthompsa			struct zyd_rq *rqp;
665184610Salfred
666188417Sthompsa			if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
667188417Sthompsa				break;	/* HMAC interrupt */
668184610Salfred
669194677Sthompsa			datalen = actlen - sizeof(cmd->code);
670188417Sthompsa			datalen -= 2;	/* XXX: padding? */
671184610Salfred
672188417Sthompsa			STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
673188417Sthompsa				int i, cnt;
674184610Salfred
675188417Sthompsa				if (rqp->olen != datalen)
676188417Sthompsa					continue;
677188417Sthompsa				cnt = rqp->olen / sizeof(struct zyd_pair);
678188417Sthompsa				for (i = 0; i < cnt; i++) {
679188417Sthompsa					if (*(((const uint16_t *)rqp->idata) + i) !=
680188417Sthompsa					    (((struct zyd_pair *)cmd->data) + i)->reg)
681188417Sthompsa						break;
682184610Salfred				}
683188417Sthompsa				if (i != cnt)
684188417Sthompsa					continue;
685188417Sthompsa				/* copy answer into caller-supplied buffer */
686227461Shselasky				memcpy(rqp->odata, cmd->data, rqp->olen);
687188417Sthompsa				DPRINTF(sc, ZYD_DEBUG_CMD,
688188417Sthompsa				    "command %p complete, data = %*D \n",
689228303Shselasky				    rqp, rqp->olen, (char *)rqp->odata, ":");
690188417Sthompsa				wakeup(rqp);	/* wakeup caller */
691188417Sthompsa				break;
692184610Salfred			}
693188417Sthompsa			if (rqp == NULL) {
694188417Sthompsa				device_printf(sc->sc_dev,
695188417Sthompsa				    "unexpected IORD notification %*D\n",
696188417Sthompsa				    datalen, cmd->data, ":");
697188417Sthompsa			}
698188417Sthompsa			break;
699184610Salfred		}
700188417Sthompsa		default:
701188417Sthompsa			device_printf(sc->sc_dev, "unknown notification %x\n",
702188417Sthompsa			    le16toh(cmd->code));
703188417Sthompsa		}
704184610Salfred
705188417Sthompsa		/* FALLTHROUGH */
706184610Salfred	case USB_ST_SETUP:
707184610Salfredtr_setup:
708194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
709194228Sthompsa		usbd_transfer_submit(xfer);
710188417Sthompsa		break;
711184610Salfred
712188417Sthompsa	default:			/* Error */
713188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
714194677Sthompsa		    usbd_errstr(error));
715188417Sthompsa
716194677Sthompsa		if (error != USB_ERR_CANCELLED) {
717188417Sthompsa			/* try to clear stall first */
718194677Sthompsa			usbd_xfer_set_stall(xfer);
719188417Sthompsa			goto tr_setup;
720184610Salfred		}
721188417Sthompsa		break;
722188417Sthompsa	}
723188417Sthompsa}
724184610Salfred
725188417Sthompsastatic void
726194677Sthompsazyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
727188417Sthompsa{
728194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
729194677Sthompsa	struct zyd_rq *rqp, *cmd;
730194677Sthompsa	struct usb_page_cache *pc;
731184610Salfred
732188417Sthompsa	switch (USB_GET_STATE(xfer)) {
733188417Sthompsa	case USB_ST_TRANSFERRED:
734194677Sthompsa		cmd = usbd_xfer_get_priv(xfer);
735194677Sthompsa		DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd);
736189452Sthompsa		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
737189452Sthompsa			/* Ensure the cached rq pointer is still valid */
738194677Sthompsa			if (rqp == cmd &&
739189452Sthompsa			    (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
740189452Sthompsa				wakeup(rqp);	/* wakeup caller */
741189452Sthompsa		}
742184610Salfred
743188417Sthompsa		/* FALLTHROUGH */
744188417Sthompsa	case USB_ST_SETUP:
745188417Sthompsatr_setup:
746188417Sthompsa		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
747188417Sthompsa			if (rqp->flags & ZYD_CMD_FLAG_SENT)
748188417Sthompsa				continue;
749184610Salfred
750194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
751194677Sthompsa			usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen);
752184610Salfred
753194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, rqp->ilen);
754194677Sthompsa			usbd_xfer_set_priv(xfer, rqp);
755188417Sthompsa			rqp->flags |= ZYD_CMD_FLAG_SENT;
756194228Sthompsa			usbd_transfer_submit(xfer);
757188417Sthompsa			break;
758184610Salfred		}
759184610Salfred		break;
760184610Salfred
761184610Salfred	default:			/* Error */
762188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
763194677Sthompsa		    usbd_errstr(error));
764184610Salfred
765194677Sthompsa		if (error != USB_ERR_CANCELLED) {
766184610Salfred			/* try to clear stall first */
767194677Sthompsa			usbd_xfer_set_stall(xfer);
768188417Sthompsa			goto tr_setup;
769184610Salfred		}
770184610Salfred		break;
771184610Salfred	}
772184610Salfred}
773184610Salfred
774188417Sthompsastatic int
775188417Sthompsazyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
776188601Sthompsa    void *odata, int olen, int flags)
777184610Salfred{
778188417Sthompsa	struct zyd_cmd cmd;
779188417Sthompsa	struct zyd_rq rq;
780188417Sthompsa	int error;
781184610Salfred
782188417Sthompsa	if (ilen > sizeof(cmd.data))
783188417Sthompsa		return (EINVAL);
784188419Sthompsa
785188417Sthompsa	cmd.code = htole16(code);
786227461Shselasky	memcpy(cmd.data, idata, ilen);
787188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
788188417Sthompsa	    &rq, ilen, idata, ":");
789184610Salfred
790188417Sthompsa	rq.cmd = &cmd;
791188417Sthompsa	rq.idata = idata;
792188417Sthompsa	rq.odata = odata;
793188417Sthompsa	rq.ilen = sizeof(uint16_t) + ilen;
794188417Sthompsa	rq.olen = olen;
795188417Sthompsa	rq.flags = flags;
796188417Sthompsa	STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
797194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
798194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
799184610Salfred
800188417Sthompsa	/* wait at most one second for command reply */
801188417Sthompsa	error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
802188417Sthompsa	if (error)
803188417Sthompsa		device_printf(sc->sc_dev, "command timeout\n");
804188417Sthompsa	STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
805188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
806188417Sthompsa	    &rq, error);
807184610Salfred
808188417Sthompsa	return (error);
809184610Salfred}
810184610Salfred
811184610Salfredstatic int
812188417Sthompsazyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
813184610Salfred{
814188417Sthompsa	struct zyd_pair tmp;
815188417Sthompsa	int error;
816184610Salfred
817188417Sthompsa	reg = htole16(reg);
818188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
819188417Sthompsa	    ZYD_CMD_FLAG_READ);
820188417Sthompsa	if (error == 0)
821188417Sthompsa		*val = le16toh(tmp.val);
822188417Sthompsa	return (error);
823184610Salfred}
824184610Salfred
825184610Salfredstatic int
826188417Sthompsazyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
827184610Salfred{
828188417Sthompsa	struct zyd_pair tmp[2];
829188417Sthompsa	uint16_t regs[2];
830184610Salfred	int error;
831184610Salfred
832188417Sthompsa	regs[0] = htole16(ZYD_REG32_HI(reg));
833188417Sthompsa	regs[1] = htole16(ZYD_REG32_LO(reg));
834188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
835188417Sthompsa	    ZYD_CMD_FLAG_READ);
836188417Sthompsa	if (error == 0)
837188417Sthompsa		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
838188417Sthompsa	return (error);
839188417Sthompsa}
840184610Salfred
841188417Sthompsastatic int
842188417Sthompsazyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
843188417Sthompsa{
844188417Sthompsa	struct zyd_pair pair;
845184610Salfred
846188417Sthompsa	pair.reg = htole16(reg);
847188417Sthompsa	pair.val = htole16(val);
848184610Salfred
849188417Sthompsa	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
850188417Sthompsa}
851184610Salfred
852188417Sthompsastatic int
853188417Sthompsazyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
854188417Sthompsa{
855188417Sthompsa	struct zyd_pair pair[2];
856184610Salfred
857188417Sthompsa	pair[0].reg = htole16(ZYD_REG32_HI(reg));
858188417Sthompsa	pair[0].val = htole16(val >> 16);
859188417Sthompsa	pair[1].reg = htole16(ZYD_REG32_LO(reg));
860188417Sthompsa	pair[1].val = htole16(val & 0xffff);
861184610Salfred
862188417Sthompsa	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
863184610Salfred}
864184610Salfred
865188417Sthompsastatic int
866188417Sthompsazyd_rfwrite(struct zyd_softc *sc, uint32_t val)
867184610Salfred{
868188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
869188417Sthompsa	struct zyd_rfwrite_cmd req;
870188417Sthompsa	uint16_t cr203;
871188417Sthompsa	int error, i;
872184610Salfred
873188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &cr203);
874188417Sthompsa	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
875188417Sthompsa
876188417Sthompsa	req.code  = htole16(2);
877188417Sthompsa	req.width = htole16(rf->width);
878188417Sthompsa	for (i = 0; i < rf->width; i++) {
879188417Sthompsa		req.bit[i] = htole16(cr203);
880188417Sthompsa		if (val & (1 << (rf->width - 1 - i)))
881188417Sthompsa			req.bit[i] |= htole16(ZYD_RF_DATA);
882188417Sthompsa	}
883188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
884188417Sthompsafail:
885188417Sthompsa	return (error);
886184610Salfred}
887184610Salfred
888188417Sthompsastatic int
889188417Sthompsazyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
890184610Salfred{
891188417Sthompsa	int error;
892184610Salfred
893188417Sthompsa	zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
894188417Sthompsa	zyd_write16_m(sc, ZYD_CR243, (val >>  8) & 0xff);
895188417Sthompsa	zyd_write16_m(sc, ZYD_CR242, (val >>  0) & 0xff);
896188417Sthompsafail:
897188417Sthompsa	return (error);
898184610Salfred}
899184610Salfred
900188417Sthompsastatic int
901188417Sthompsazyd_lock_phy(struct zyd_softc *sc)
902184610Salfred{
903188417Sthompsa	int error;
904188417Sthompsa	uint32_t tmp;
905186730Salfred
906188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
907188417Sthompsa	tmp &= ~ZYD_UNLOCK_PHY_REGS;
908188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
909188417Sthompsafail:
910188417Sthompsa	return (error);
911184610Salfred}
912184610Salfred
913188417Sthompsastatic int
914188417Sthompsazyd_unlock_phy(struct zyd_softc *sc)
915184610Salfred{
916188417Sthompsa	int error;
917188417Sthompsa	uint32_t tmp;
918184610Salfred
919188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
920188417Sthompsa	tmp |= ZYD_UNLOCK_PHY_REGS;
921188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
922188417Sthompsafail:
923188417Sthompsa	return (error);
924184610Salfred}
925184610Salfred
926184610Salfred/*
927188417Sthompsa * RFMD RF methods.
928184610Salfred */
929188417Sthompsastatic int
930188417Sthompsazyd_rfmd_init(struct zyd_rf *rf)
931184610Salfred{
932188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
933188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
934184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
935184610Salfred	static const uint32_t rfini[] = ZYD_RFMD_RF;
936188417Sthompsa	int i, error;
937184610Salfred
938184610Salfred	/* init RF-dependent PHY registers */
939188417Sthompsa	for (i = 0; i < N(phyini); i++) {
940188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
941184610Salfred	}
942184610Salfred
943184610Salfred	/* init RFMD radio */
944188417Sthompsa	for (i = 0; i < N(rfini); i++) {
945188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
946188417Sthompsa			return (error);
947184610Salfred	}
948188417Sthompsafail:
949188417Sthompsa	return (error);
950188417Sthompsa#undef N
951184610Salfred}
952184610Salfred
953188417Sthompsastatic int
954188417Sthompsazyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
955184610Salfred{
956188417Sthompsa	int error;
957188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
958188417Sthompsa
959188417Sthompsa	zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
960188417Sthompsa	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
961188417Sthompsafail:
962188417Sthompsa	return (error);
963184610Salfred}
964184610Salfred
965188417Sthompsastatic int
966188417Sthompsazyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
967184610Salfred{
968188417Sthompsa	int error;
969188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
970184610Salfred	static const struct {
971188417Sthompsa		uint32_t	r1, r2;
972188417Sthompsa	} rfprog[] = ZYD_RFMD_CHANTABLE;
973184610Salfred
974188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
975188417Sthompsa	if (error != 0)
976188417Sthompsa		goto fail;
977188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
978188417Sthompsa	if (error != 0)
979188417Sthompsa		goto fail;
980184610Salfred
981188417Sthompsafail:
982188417Sthompsa	return (error);
983184610Salfred}
984184610Salfred
985184610Salfred/*
986188417Sthompsa * AL2230 RF methods.
987184610Salfred */
988188417Sthompsastatic int
989188417Sthompsazyd_al2230_init(struct zyd_rf *rf)
990184610Salfred{
991188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
992188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
993184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
994186730Salfred	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
995186730Salfred	static const struct zyd_phy_pair phypll[] = {
996188417Sthompsa		{ ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
997188417Sthompsa		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
998186730Salfred	};
999186730Salfred	static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
1000186730Salfred	static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
1001186730Salfred	static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
1002188417Sthompsa	int i, error;
1003184610Salfred
1004184610Salfred	/* init RF-dependent PHY registers */
1005188417Sthompsa	for (i = 0; i < N(phyini); i++)
1006188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1007186730Salfred
1008188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1009188417Sthompsa		for (i = 0; i < N(phy2230s); i++)
1010188417Sthompsa			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1011184610Salfred	}
1012188417Sthompsa
1013186730Salfred	/* init AL2230 radio */
1014188417Sthompsa	for (i = 0; i < N(rfini1); i++) {
1015188417Sthompsa		error = zyd_rfwrite(sc, rfini1[i]);
1016188417Sthompsa		if (error != 0)
1017188417Sthompsa			goto fail;
1018188417Sthompsa	}
1019184610Salfred
1020188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1021188417Sthompsa		error = zyd_rfwrite(sc, 0x000824);
1022186730Salfred	else
1023188417Sthompsa		error = zyd_rfwrite(sc, 0x0005a4);
1024188417Sthompsa	if (error != 0)
1025188417Sthompsa		goto fail;
1026186730Salfred
1027188417Sthompsa	for (i = 0; i < N(rfini2); i++) {
1028188417Sthompsa		error = zyd_rfwrite(sc, rfini2[i]);
1029188417Sthompsa		if (error != 0)
1030188417Sthompsa			goto fail;
1031188417Sthompsa	}
1032186730Salfred
1033188417Sthompsa	for (i = 0; i < N(phypll); i++)
1034188417Sthompsa		zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
1035186730Salfred
1036188417Sthompsa	for (i = 0; i < N(rfini3); i++) {
1037188417Sthompsa		error = zyd_rfwrite(sc, rfini3[i]);
1038188417Sthompsa		if (error != 0)
1039188417Sthompsa			goto fail;
1040188417Sthompsa	}
1041188417Sthompsafail:
1042188417Sthompsa	return (error);
1043188417Sthompsa#undef N
1044184610Salfred}
1045184610Salfred
1046188417Sthompsastatic int
1047188417Sthompsazyd_al2230_fini(struct zyd_rf *rf)
1048186730Salfred{
1049188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1050188417Sthompsa	int error, i;
1051188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1052186730Salfred	static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
1053186730Salfred
1054188417Sthompsa	for (i = 0; i < N(phy); i++)
1055188417Sthompsa		zyd_write16_m(sc, phy[i].reg, phy[i].val);
1056186730Salfred
1057186730Salfred	if (sc->sc_newphy != 0)
1058188417Sthompsa		zyd_write16_m(sc, ZYD_CR9, 0xe1);
1059188417Sthompsa
1060188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, 0x6);
1061188417Sthompsafail:
1062188417Sthompsa	return (error);
1063188417Sthompsa#undef N
1064186730Salfred}
1065186730Salfred
1066188417Sthompsastatic int
1067188417Sthompsazyd_al2230_init_b(struct zyd_rf *rf)
1068184610Salfred{
1069188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1070188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1071186730Salfred	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1072186730Salfred	static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
1073186730Salfred	static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
1074186730Salfred	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1075188417Sthompsa	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1076186730Salfred	static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
1077186730Salfred	static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
1078186730Salfred	static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
1079186730Salfred	static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
1080188417Sthompsa	int i, error;
1081184610Salfred
1082188417Sthompsa	for (i = 0; i < N(phy1); i++)
1083188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1084186730Salfred
1085184610Salfred	/* init RF-dependent PHY registers */
1086188417Sthompsa	for (i = 0; i < N(phyini); i++)
1087188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1088184610Salfred
1089188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1090188417Sthompsa		for (i = 0; i < N(phy2230s); i++)
1091188417Sthompsa			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1092188417Sthompsa	}
1093186730Salfred
1094188417Sthompsa	for (i = 0; i < 3; i++) {
1095188417Sthompsa		error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
1096188417Sthompsa		if (error != 0)
1097188417Sthompsa			return (error);
1098188417Sthompsa	}
1099186730Salfred
1100188417Sthompsa	for (i = 0; i < N(rfini_part1); i++) {
1101188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part1[i]);
1102188417Sthompsa		if (error != 0)
1103188417Sthompsa			return (error);
1104188417Sthompsa	}
1105186730Salfred
1106188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1107188417Sthompsa		error = zyd_rfwrite(sc, 0x241000);
1108186730Salfred	else
1109188417Sthompsa		error = zyd_rfwrite(sc, 0x25a000);
1110188417Sthompsa	if (error != 0)
1111188417Sthompsa		goto fail;
1112186730Salfred
1113188417Sthompsa	for (i = 0; i < N(rfini_part2); i++) {
1114188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part2[i]);
1115188417Sthompsa		if (error != 0)
1116188417Sthompsa			return (error);
1117188417Sthompsa	}
1118186730Salfred
1119188417Sthompsa	for (i = 0; i < N(phy2); i++)
1120188417Sthompsa		zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
1121186730Salfred
1122188417Sthompsa	for (i = 0; i < N(rfini_part3); i++) {
1123188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part3[i]);
1124188417Sthompsa		if (error != 0)
1125188417Sthompsa			return (error);
1126188417Sthompsa	}
1127186730Salfred
1128188417Sthompsa	for (i = 0; i < N(phy3); i++)
1129188417Sthompsa		zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
1130186730Salfred
1131188417Sthompsa	error = zyd_al2230_fini(rf);
1132188417Sthompsafail:
1133188417Sthompsa	return (error);
1134188417Sthompsa#undef N
1135184610Salfred}
1136184610Salfred
1137188417Sthompsastatic int
1138188417Sthompsazyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1139184610Salfred{
1140188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1141188417Sthompsa	int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
1142188417Sthompsa
1143188417Sthompsa	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1144188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
1145188417Sthompsafail:
1146188417Sthompsa	return (error);
1147188417Sthompsa}
1148188417Sthompsa
1149188417Sthompsastatic int
1150188417Sthompsazyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1151188417Sthompsa{
1152188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1153188417Sthompsa	int error, i;
1154188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1155186730Salfred	static const struct zyd_phy_pair phy1[] = {
1156188417Sthompsa		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
1157186730Salfred	};
1158184610Salfred	static const struct {
1159188417Sthompsa		uint32_t	r1, r2, r3;
1160188417Sthompsa	} rfprog[] = ZYD_AL2230_CHANTABLE;
1161184610Salfred
1162188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1163188417Sthompsa	if (error != 0)
1164188417Sthompsa		goto fail;
1165188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1166188417Sthompsa	if (error != 0)
1167188417Sthompsa		goto fail;
1168188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
1169188417Sthompsa	if (error != 0)
1170188417Sthompsa		goto fail;
1171184610Salfred
1172188417Sthompsa	for (i = 0; i < N(phy1); i++)
1173188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1174188417Sthompsafail:
1175188417Sthompsa	return (error);
1176188417Sthompsa#undef N
1177184610Salfred}
1178184610Salfred
1179188417Sthompsastatic int
1180188417Sthompsazyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
1181186730Salfred{
1182188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1183188417Sthompsa	int error, i;
1184188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1185186730Salfred	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1186186730Salfred	static const struct {
1187188417Sthompsa		uint32_t	r1, r2, r3;
1188188417Sthompsa	} rfprog[] = ZYD_AL2230_CHANTABLE_B;
1189186730Salfred
1190188417Sthompsa	for (i = 0; i < N(phy1); i++)
1191188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1192186730Salfred
1193188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
1194188417Sthompsa	if (error != 0)
1195188417Sthompsa		goto fail;
1196188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
1197188417Sthompsa	if (error != 0)
1198188417Sthompsa		goto fail;
1199188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
1200188417Sthompsa	if (error != 0)
1201188417Sthompsa		goto fail;
1202188417Sthompsa	error = zyd_al2230_fini(rf);
1203188417Sthompsafail:
1204188417Sthompsa	return (error);
1205188417Sthompsa#undef N
1206186730Salfred}
1207186730Salfred
1208186730Salfred#define	ZYD_AL2230_PHY_BANDEDGE6					\
1209186730Salfred{									\
1210188417Sthompsa	{ ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 },	\
1211186730Salfred	{ ZYD_CR47,  0x1e }						\
1212186730Salfred}
1213186730Salfred
1214188417Sthompsastatic int
1215188417Sthompsazyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
1216186730Salfred{
1217188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1218188417Sthompsa	int error = 0, i;
1219188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1220188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1221188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1222186730Salfred	struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
1223188601Sthompsa	int chan = ieee80211_chan2ieee(ic, c);
1224186730Salfred
1225188417Sthompsa	if (chan == 1 || chan == 11)
1226186730Salfred		r[0].val = 0x12;
1227188417Sthompsa
1228188417Sthompsa	for (i = 0; i < N(r); i++)
1229188417Sthompsa		zyd_write16_m(sc, r[i].reg, r[i].val);
1230188417Sthompsafail:
1231188417Sthompsa	return (error);
1232188417Sthompsa#undef N
1233186730Salfred}
1234186730Salfred
1235184610Salfred/*
1236184610Salfred * AL7230B RF methods.
1237184610Salfred */
1238188417Sthompsastatic int
1239188417Sthompsazyd_al7230B_init(struct zyd_rf *rf)
1240184610Salfred{
1241188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1242188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1243184610Salfred	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1244184610Salfred	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1245184610Salfred	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1246184610Salfred	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1247184610Salfred	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1248188417Sthompsa	int i, error;
1249184610Salfred
1250184610Salfred	/* for AL7230B, PHY and RF need to be initialized in "phases" */
1251184610Salfred
1252184610Salfred	/* init RF-dependent PHY registers, part one */
1253188417Sthompsa	for (i = 0; i < N(phyini_1); i++)
1254188417Sthompsa		zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
1255188417Sthompsa
1256184610Salfred	/* init AL7230B radio, part one */
1257188417Sthompsa	for (i = 0; i < N(rfini_1); i++) {
1258188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1259188417Sthompsa			return (error);
1260184610Salfred	}
1261184610Salfred	/* init RF-dependent PHY registers, part two */
1262188417Sthompsa	for (i = 0; i < N(phyini_2); i++)
1263188417Sthompsa		zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
1264188417Sthompsa
1265184610Salfred	/* init AL7230B radio, part two */
1266188417Sthompsa	for (i = 0; i < N(rfini_2); i++) {
1267188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1268188417Sthompsa			return (error);
1269184610Salfred	}
1270184610Salfred	/* init RF-dependent PHY registers, part three */
1271188417Sthompsa	for (i = 0; i < N(phyini_3); i++)
1272188417Sthompsa		zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
1273188417Sthompsafail:
1274188417Sthompsa	return (error);
1275188417Sthompsa#undef N
1276184610Salfred}
1277184610Salfred
1278188417Sthompsastatic int
1279188417Sthompsazyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1280184610Salfred{
1281188417Sthompsa	int error;
1282188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1283188417Sthompsa
1284188417Sthompsa	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1285188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1286188417Sthompsafail:
1287188417Sthompsa	return (error);
1288188417Sthompsa}
1289188417Sthompsa
1290188417Sthompsastatic int
1291188417Sthompsazyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1292188417Sthompsa{
1293188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1294188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1295184610Salfred	static const struct {
1296188417Sthompsa		uint32_t	r1, r2;
1297188417Sthompsa	} rfprog[] = ZYD_AL7230B_CHANTABLE;
1298184610Salfred	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1299188417Sthompsa	int i, error;
1300184610Salfred
1301188417Sthompsa	zyd_write16_m(sc, ZYD_CR240, 0x57);
1302188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, 0x2f);
1303184610Salfred
1304188417Sthompsa	for (i = 0; i < N(rfsc); i++) {
1305188417Sthompsa		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1306188417Sthompsa			return (error);
1307184610Salfred	}
1308184610Salfred
1309188417Sthompsa	zyd_write16_m(sc, ZYD_CR128, 0x14);
1310188417Sthompsa	zyd_write16_m(sc, ZYD_CR129, 0x12);
1311188417Sthompsa	zyd_write16_m(sc, ZYD_CR130, 0x10);
1312188417Sthompsa	zyd_write16_m(sc, ZYD_CR38,  0x38);
1313188417Sthompsa	zyd_write16_m(sc, ZYD_CR136, 0xdf);
1314184610Salfred
1315188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1316188417Sthompsa	if (error != 0)
1317188417Sthompsa		goto fail;
1318188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1319188417Sthompsa	if (error != 0)
1320188417Sthompsa		goto fail;
1321188417Sthompsa	error = zyd_rfwrite(sc, 0x3c9000);
1322188417Sthompsa	if (error != 0)
1323188417Sthompsa		goto fail;
1324184610Salfred
1325188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, 0x3f);
1326188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, 0x06);
1327188417Sthompsa	zyd_write16_m(sc, ZYD_CR240, 0x08);
1328188417Sthompsafail:
1329188417Sthompsa	return (error);
1330188417Sthompsa#undef N
1331184610Salfred}
1332184610Salfred
1333184610Salfred/*
1334184610Salfred * AL2210 RF methods.
1335184610Salfred */
1336188417Sthompsastatic int
1337188417Sthompsazyd_al2210_init(struct zyd_rf *rf)
1338184610Salfred{
1339188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1340188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1341184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1342184610Salfred	static const uint32_t rfini[] = ZYD_AL2210_RF;
1343184610Salfred	uint32_t tmp;
1344188417Sthompsa	int i, error;
1345184610Salfred
1346188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 2);
1347184610Salfred
1348184610Salfred	/* init RF-dependent PHY registers */
1349188417Sthompsa	for (i = 0; i < N(phyini); i++)
1350188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1351188417Sthompsa
1352184610Salfred	/* init AL2210 radio */
1353188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1354188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1355188417Sthompsa			return (error);
1356184610Salfred	}
1357188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1358188417Sthompsa	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1359188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1360188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1361188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1362188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1363188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1364188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 3);
1365188417Sthompsafail:
1366188417Sthompsa	return (error);
1367188417Sthompsa#undef N
1368184610Salfred}
1369184610Salfred
1370188417Sthompsastatic int
1371188417Sthompsazyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1372184610Salfred{
1373188417Sthompsa	/* vendor driver does nothing for this RF chip */
1374188417Sthompsa
1375188417Sthompsa	return (0);
1376188417Sthompsa}
1377188417Sthompsa
1378188417Sthompsastatic int
1379188417Sthompsazyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1380188417Sthompsa{
1381188417Sthompsa	int error;
1382188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1383184610Salfred	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1384184610Salfred	uint32_t tmp;
1385184610Salfred
1386188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 2);
1387188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1388188417Sthompsa	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1389188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1390188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1391188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1392188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1393188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1394184610Salfred
1395184610Salfred	/* actually set the channel */
1396188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1]);
1397188417Sthompsa	if (error != 0)
1398188417Sthompsa		goto fail;
1399184610Salfred
1400188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 3);
1401188417Sthompsafail:
1402188417Sthompsa	return (error);
1403184610Salfred}
1404184610Salfred
1405184610Salfred/*
1406184610Salfred * GCT RF methods.
1407184610Salfred */
1408188417Sthompsastatic int
1409188417Sthompsazyd_gct_init(struct zyd_rf *rf)
1410184610Salfred{
1411193420Sweongyo#define	ZYD_GCT_INTR_REG	0x85c1
1412188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1413188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1414184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1415184610Salfred	static const uint32_t rfini[] = ZYD_GCT_RF;
1416193420Sweongyo	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1417193420Sweongyo	int i, idx = -1, error;
1418193420Sweongyo	uint16_t data;
1419184610Salfred
1420184610Salfred	/* init RF-dependent PHY registers */
1421188417Sthompsa	for (i = 0; i < N(phyini); i++)
1422188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1423188417Sthompsa
1424184610Salfred	/* init cgt radio */
1425188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1426188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1427188417Sthompsa			return (error);
1428184610Salfred	}
1429193420Sweongyo
1430193420Sweongyo	error = zyd_gct_mode(rf);
1431193420Sweongyo	if (error != 0)
1432193420Sweongyo		return (error);
1433193420Sweongyo
1434193420Sweongyo	for (i = 0; i < N(vco) - 1; i++) {
1435193420Sweongyo		error = zyd_gct_set_channel_synth(rf, 1, 0);
1436193420Sweongyo		if (error != 0)
1437193420Sweongyo			goto fail;
1438193420Sweongyo		error = zyd_gct_write(rf, vco[i][0]);
1439193420Sweongyo		if (error != 0)
1440193420Sweongyo			goto fail;
1441193420Sweongyo		zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf);
1442193420Sweongyo		zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data);
1443193420Sweongyo		if ((data & 0xf) == 0) {
1444193420Sweongyo			idx = i;
1445193420Sweongyo			break;
1446193420Sweongyo		}
1447193420Sweongyo	}
1448193420Sweongyo	if (idx == -1) {
1449193420Sweongyo		error = zyd_gct_set_channel_synth(rf, 1, 1);
1450193420Sweongyo		if (error != 0)
1451193420Sweongyo			goto fail;
1452193420Sweongyo		error = zyd_gct_write(rf, 0x6662);
1453193420Sweongyo		if (error != 0)
1454193420Sweongyo			goto fail;
1455193420Sweongyo	}
1456193420Sweongyo
1457193420Sweongyo	rf->idx = idx;
1458193420Sweongyo	zyd_write16_m(sc, ZYD_CR203, 0x6);
1459188417Sthompsafail:
1460188417Sthompsa	return (error);
1461188417Sthompsa#undef N
1462193420Sweongyo#undef ZYD_GCT_INTR_REG
1463184610Salfred}
1464184610Salfred
1465188417Sthompsastatic int
1466193420Sweongyozyd_gct_mode(struct zyd_rf *rf)
1467184610Salfred{
1468193420Sweongyo#define N(a)	(sizeof(a) / sizeof((a)[0]))
1469193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1470193420Sweongyo	static const uint32_t mode[] = {
1471193420Sweongyo		0x25f98, 0x25f9a, 0x25f94, 0x27fd4
1472193420Sweongyo	};
1473193420Sweongyo	int i, error;
1474188417Sthompsa
1475193420Sweongyo	for (i = 0; i < N(mode); i++) {
1476193420Sweongyo		if ((error = zyd_rfwrite(sc, mode[i])) != 0)
1477193420Sweongyo			break;
1478193420Sweongyo	}
1479193420Sweongyo	return (error);
1480193420Sweongyo#undef N
1481188417Sthompsa}
1482188417Sthompsa
1483188417Sthompsastatic int
1484193420Sweongyozyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal)
1485188417Sthompsa{
1486193420Sweongyo	int error, idx = chan - 1;
1487188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1488193420Sweongyo	static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL;
1489193420Sweongyo	static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD;
1490193420Sweongyo	static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV;
1491184610Salfred
1492193420Sweongyo	error = zyd_rfwrite(sc,
1493193420Sweongyo	    (acal == 1) ? acal_synth[idx] : std_synth[idx]);
1494188417Sthompsa	if (error != 0)
1495193420Sweongyo		return (error);
1496193420Sweongyo	return zyd_rfwrite(sc, div_synth[idx]);
1497184610Salfred}
1498184610Salfred
1499188417Sthompsastatic int
1500193420Sweongyozyd_gct_write(struct zyd_rf *rf, uint16_t value)
1501184610Salfred{
1502188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1503184610Salfred
1504193420Sweongyo	return zyd_rfwrite(sc, 0x300000 | 0x40000 | value);
1505184610Salfred}
1506184610Salfred
1507188417Sthompsastatic int
1508193420Sweongyozyd_gct_switch_radio(struct zyd_rf *rf, int on)
1509184610Salfred{
1510193420Sweongyo#define N(a)	(sizeof(a) / sizeof((a)[0]))
1511193420Sweongyo	int error;
1512193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1513188417Sthompsa
1514193420Sweongyo	error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90);
1515193420Sweongyo	if (error != 0)
1516193420Sweongyo		return (error);
1517193420Sweongyo
1518193420Sweongyo	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
1519193420Sweongyo	zyd_write16_m(sc, ZYD_CR251,
1520193420Sweongyo	    on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f);
1521193420Sweongyofail:
1522193420Sweongyo	return (error);
1523188417Sthompsa}
1524188417Sthompsa
1525188417Sthompsastatic int
1526193420Sweongyozyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1527188417Sthompsa{
1528188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1529193420Sweongyo	int error, i;
1530188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1531193420Sweongyo	static const struct zyd_phy_pair cmd[] = {
1532193420Sweongyo		{ ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 },
1533193420Sweongyo		{ ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 },
1534193420Sweongyo	};
1535193420Sweongyo	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1536184610Salfred
1537193420Sweongyo	error = zyd_gct_set_channel_synth(rf, chan, 0);
1538188417Sthompsa	if (error != 0)
1539188417Sthompsa		goto fail;
1540193420Sweongyo	error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 :
1541193420Sweongyo	    vco[rf->idx][((chan - 1) / 2)]);
1542188417Sthompsa	if (error != 0)
1543188417Sthompsa		goto fail;
1544193420Sweongyo	error = zyd_gct_mode(rf);
1545193420Sweongyo	if (error != 0)
1546193420Sweongyo		return (error);
1547193420Sweongyo	for (i = 0; i < N(cmd); i++)
1548193420Sweongyo		zyd_write16_m(sc, cmd[i].reg, cmd[i].val);
1549193420Sweongyo	error = zyd_gct_txgain(rf, chan);
1550193420Sweongyo	if (error != 0)
1551193420Sweongyo		return (error);
1552193420Sweongyo	zyd_write16_m(sc, ZYD_CR203, 0x6);
1553188417Sthompsafail:
1554188417Sthompsa	return (error);
1555188417Sthompsa#undef N
1556184610Salfred}
1557184610Salfred
1558193420Sweongyostatic int
1559193420Sweongyozyd_gct_txgain(struct zyd_rf *rf, uint8_t chan)
1560193420Sweongyo{
1561193420Sweongyo#define N(a)	(sizeof(a) / sizeof((a)[0]))
1562193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1563193420Sweongyo	static uint32_t txgain[] = ZYD_GCT_TXGAIN;
1564193420Sweongyo	uint8_t idx = sc->sc_pwrint[chan - 1];
1565193420Sweongyo
1566193420Sweongyo	if (idx >= N(txgain)) {
1567193420Sweongyo		device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n",
1568193420Sweongyo		    chan, idx);
1569193420Sweongyo		return 0;
1570193420Sweongyo	}
1571193420Sweongyo
1572193420Sweongyo	return zyd_rfwrite(sc, 0x700000 | txgain[idx]);
1573193420Sweongyo#undef N
1574193420Sweongyo}
1575193420Sweongyo
1576184610Salfred/*
1577184610Salfred * Maxim2 RF methods.
1578184610Salfred */
1579188417Sthompsastatic int
1580188417Sthompsazyd_maxim2_init(struct zyd_rf *rf)
1581184610Salfred{
1582188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1583188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1584184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1585184610Salfred	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1586184610Salfred	uint16_t tmp;
1587188417Sthompsa	int i, error;
1588184610Salfred
1589184610Salfred	/* init RF-dependent PHY registers */
1590188417Sthompsa	for (i = 0; i < N(phyini); i++)
1591188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1592184610Salfred
1593188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1594188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1595188417Sthompsa
1596184610Salfred	/* init maxim2 radio */
1597188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1598188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1599188417Sthompsa			return (error);
1600184610Salfred	}
1601188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1602188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1603188417Sthompsafail:
1604188417Sthompsa	return (error);
1605188417Sthompsa#undef N
1606184610Salfred}
1607184610Salfred
1608188417Sthompsastatic int
1609188417Sthompsazyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1610184610Salfred{
1611188417Sthompsa
1612188417Sthompsa	/* vendor driver does nothing for this RF chip */
1613188417Sthompsa	return (0);
1614188417Sthompsa}
1615188417Sthompsa
1616188417Sthompsastatic int
1617188417Sthompsazyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1618188417Sthompsa{
1619188417Sthompsa#define N(a)	(sizeof(a) / sizeof((a)[0]))
1620188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1621184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1622184610Salfred	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1623184610Salfred	static const struct {
1624188417Sthompsa		uint32_t	r1, r2;
1625188417Sthompsa	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
1626184610Salfred	uint16_t tmp;
1627188417Sthompsa	int i, error;
1628184610Salfred
1629184610Salfred	/*
1630184610Salfred	 * Do the same as we do when initializing it, except for the channel
1631184610Salfred	 * values coming from the two channel tables.
1632184610Salfred	 */
1633184610Salfred
1634184610Salfred	/* init RF-dependent PHY registers */
1635188417Sthompsa	for (i = 0; i < N(phyini); i++)
1636188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1637184610Salfred
1638188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1639188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1640188417Sthompsa
1641184610Salfred	/* first two values taken from the chantables */
1642188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1643188417Sthompsa	if (error != 0)
1644188417Sthompsa		goto fail;
1645188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1646188417Sthompsa	if (error != 0)
1647188417Sthompsa		goto fail;
1648184610Salfred
1649184610Salfred	/* init maxim2 radio - skipping the two first values */
1650188417Sthompsa	for (i = 2; i < N(rfini); i++) {
1651188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1652188417Sthompsa			return (error);
1653184610Salfred	}
1654188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1655188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1656188417Sthompsafail:
1657188417Sthompsa	return (error);
1658188417Sthompsa#undef N
1659184610Salfred}
1660184610Salfred
1661188417Sthompsastatic int
1662188417Sthompsazyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1663184610Salfred{
1664188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1665184610Salfred
1666188417Sthompsa	rf->rf_sc = sc;
1667193420Sweongyo	rf->update_pwr = 1;
1668188417Sthompsa
1669188417Sthompsa	switch (type) {
1670184610Salfred	case ZYD_RF_RFMD:
1671188417Sthompsa		rf->init         = zyd_rfmd_init;
1672188417Sthompsa		rf->switch_radio = zyd_rfmd_switch_radio;
1673188417Sthompsa		rf->set_channel  = zyd_rfmd_set_channel;
1674188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1675184610Salfred		break;
1676184610Salfred	case ZYD_RF_AL2230:
1677186730Salfred	case ZYD_RF_AL2230S:
1678188417Sthompsa		if (sc->sc_macrev == ZYD_ZD1211B) {
1679188417Sthompsa			rf->init = zyd_al2230_init_b;
1680188417Sthompsa			rf->set_channel = zyd_al2230_set_channel_b;
1681186730Salfred		} else {
1682188417Sthompsa			rf->init = zyd_al2230_init;
1683188417Sthompsa			rf->set_channel = zyd_al2230_set_channel;
1684186730Salfred		}
1685188417Sthompsa		rf->switch_radio = zyd_al2230_switch_radio;
1686188417Sthompsa		rf->bandedge6	 = zyd_al2230_bandedge6;
1687188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1688184610Salfred		break;
1689184610Salfred	case ZYD_RF_AL7230B:
1690188417Sthompsa		rf->init         = zyd_al7230B_init;
1691188417Sthompsa		rf->switch_radio = zyd_al7230B_switch_radio;
1692188417Sthompsa		rf->set_channel  = zyd_al7230B_set_channel;
1693188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1694184610Salfred		break;
1695184610Salfred	case ZYD_RF_AL2210:
1696188417Sthompsa		rf->init         = zyd_al2210_init;
1697188417Sthompsa		rf->switch_radio = zyd_al2210_switch_radio;
1698188417Sthompsa		rf->set_channel  = zyd_al2210_set_channel;
1699188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1700184610Salfred		break;
1701193420Sweongyo	case ZYD_RF_MAXIM_NEW:
1702184610Salfred	case ZYD_RF_GCT:
1703188417Sthompsa		rf->init         = zyd_gct_init;
1704188417Sthompsa		rf->switch_radio = zyd_gct_switch_radio;
1705188417Sthompsa		rf->set_channel  = zyd_gct_set_channel;
1706193420Sweongyo		rf->width        = 24;	/* 24-bit RF values */
1707193420Sweongyo		rf->update_pwr   = 0;
1708184610Salfred		break;
1709184610Salfred	case ZYD_RF_MAXIM_NEW2:
1710188417Sthompsa		rf->init         = zyd_maxim2_init;
1711188417Sthompsa		rf->switch_radio = zyd_maxim2_switch_radio;
1712188417Sthompsa		rf->set_channel  = zyd_maxim2_set_channel;
1713188417Sthompsa		rf->width        = 18;	/* 18-bit RF values */
1714184610Salfred		break;
1715184610Salfred	default:
1716188417Sthompsa		device_printf(sc->sc_dev,
1717188417Sthompsa		    "sorry, radio \"%s\" is not supported yet\n",
1718188417Sthompsa		    zyd_rf_name(type));
1719188417Sthompsa		return (EINVAL);
1720184610Salfred	}
1721188417Sthompsa	return (0);
1722188417Sthompsa}
1723184610Salfred
1724188417Sthompsastatic const char *
1725188417Sthompsazyd_rf_name(uint8_t type)
1726188417Sthompsa{
1727188417Sthompsa	static const char * const zyd_rfs[] = {
1728188417Sthompsa		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
1729188417Sthompsa		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
1730188417Sthompsa		"AL2230S",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
1731188417Sthompsa		"PHILIPS"
1732188417Sthompsa	};
1733184610Salfred
1734188417Sthompsa	return zyd_rfs[(type > 15) ? 0 : type];
1735184610Salfred}
1736184610Salfred
1737188417Sthompsastatic int
1738188417Sthompsazyd_hw_init(struct zyd_softc *sc)
1739184610Salfred{
1740188417Sthompsa	int error;
1741184610Salfred	const struct zyd_phy_pair *phyp;
1742188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1743188417Sthompsa	uint16_t val;
1744184610Salfred
1745184610Salfred	/* specify that the plug and play is finished */
1746188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1747188417Sthompsa	zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
1748188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
1749188417Sthompsa	    sc->sc_fwbase);
1750184610Salfred
1751184610Salfred	/* retrieve firmware revision number */
1752188417Sthompsa	zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
1753188417Sthompsa	zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
1754188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1755186730Salfred	/* set mandatory rates - XXX assumes 802.11b/g */
1756188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
1757186730Salfred
1758184610Salfred	/* disable interrupts */
1759188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
1760184610Salfred
1761188417Sthompsa	if ((error = zyd_read_pod(sc)) != 0) {
1762188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
1763188417Sthompsa		goto fail;
1764184610Salfred	}
1765188417Sthompsa
1766188417Sthompsa	/* PHY init (resetting) */
1767188417Sthompsa	error = zyd_lock_phy(sc);
1768188417Sthompsa	if (error != 0)
1769188417Sthompsa		goto fail;
1770188417Sthompsa	phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1771188417Sthompsa	for (; phyp->reg != 0; phyp++)
1772188417Sthompsa		zyd_write16_m(sc, phyp->reg, phyp->val);
1773188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
1774188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
1775188417Sthompsa		zyd_write32_m(sc, ZYD_CR157, val >> 8);
1776184610Salfred	}
1777188417Sthompsa	error = zyd_unlock_phy(sc);
1778188417Sthompsa	if (error != 0)
1779188417Sthompsa		goto fail;
1780184610Salfred
1781184610Salfred	/* HMAC init */
1782188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1783188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1784188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
1785188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
1786188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
1787188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
1788188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
1789188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1790188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1791188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1792188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1793188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1794188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1795188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1796188417Sthompsa	zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
1797188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1798188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1799188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1800188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
1801188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
1802184610Salfred
1803188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211) {
1804188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
1805188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1806186730Salfred	} else {
1807188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1808188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1809188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1810188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1811188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1812188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1813188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1814188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
1815188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
1816186730Salfred	}
1817186730Salfred
1818184610Salfred	/* init beacon interval to 100ms */
1819188417Sthompsa	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1820188417Sthompsa		goto fail;
1821184610Salfred
1822188417Sthompsa	if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
1823188417Sthompsa		device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
1824188417Sthompsa		    sc->sc_rfrev);
1825188417Sthompsa		goto fail;
1826188417Sthompsa	}
1827188417Sthompsa
1828188417Sthompsa	/* RF chip init */
1829188417Sthompsa	error = zyd_lock_phy(sc);
1830188417Sthompsa	if (error != 0)
1831188417Sthompsa		goto fail;
1832188417Sthompsa	error = (*rf->init)(rf);
1833188417Sthompsa	if (error != 0) {
1834188417Sthompsa		device_printf(sc->sc_dev,
1835188417Sthompsa		    "radio initialization failed, error %d\n", error);
1836188417Sthompsa		goto fail;
1837188417Sthompsa	}
1838188417Sthompsa	error = zyd_unlock_phy(sc);
1839188417Sthompsa	if (error != 0)
1840188417Sthompsa		goto fail;
1841188417Sthompsa
1842188417Sthompsa	if ((error = zyd_read_eeprom(sc)) != 0) {
1843188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
1844188417Sthompsa		goto fail;
1845188417Sthompsa	}
1846188417Sthompsa
1847188417Sthompsafail:	return (error);
1848184610Salfred}
1849184610Salfred
1850188417Sthompsastatic int
1851188417Sthompsazyd_read_pod(struct zyd_softc *sc)
1852184610Salfred{
1853188417Sthompsa	int error;
1854184610Salfred	uint32_t tmp;
1855184610Salfred
1856188417Sthompsa	zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
1857188417Sthompsa	sc->sc_rfrev     = tmp & 0x0f;
1858188417Sthompsa	sc->sc_ledtype   = (tmp >>  4) & 0x01;
1859188417Sthompsa	sc->sc_al2230s   = (tmp >>  7) & 0x01;
1860188417Sthompsa	sc->sc_cckgain   = (tmp >>  8) & 0x01;
1861184610Salfred	sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
1862188417Sthompsa	sc->sc_parev     = (tmp >> 16) & 0x0f;
1863186730Salfred	sc->sc_bandedge6 = (tmp >> 21) & 0x01;
1864188417Sthompsa	sc->sc_newphy    = (tmp >> 31) & 0x01;
1865188417Sthompsa	sc->sc_txled     = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
1866188417Sthompsafail:
1867188417Sthompsa	return (error);
1868188417Sthompsa}
1869184610Salfred
1870188417Sthompsastatic int
1871188417Sthompsazyd_read_eeprom(struct zyd_softc *sc)
1872188417Sthompsa{
1873188417Sthompsa	uint16_t val;
1874188417Sthompsa	int error, i;
1875184610Salfred
1876184610Salfred	/* read Tx power calibration tables */
1877184610Salfred	for (i = 0; i < 7; i++) {
1878188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1879188417Sthompsa		sc->sc_pwrcal[i * 2] = val >> 8;
1880188417Sthompsa		sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
1881188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
1882188417Sthompsa		sc->sc_pwrint[i * 2] = val >> 8;
1883188417Sthompsa		sc->sc_pwrint[i * 2 + 1] = val & 0xff;
1884188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
1885188417Sthompsa		sc->sc_ofdm36_cal[i * 2] = val >> 8;
1886188417Sthompsa		sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
1887188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
1888188417Sthompsa		sc->sc_ofdm48_cal[i * 2] = val >> 8;
1889188417Sthompsa		sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
1890188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
1891188417Sthompsa		sc->sc_ofdm54_cal[i * 2] = val >> 8;
1892188417Sthompsa		sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
1893184610Salfred	}
1894188417Sthompsafail:
1895188417Sthompsa	return (error);
1896184610Salfred}
1897184610Salfred
1898188417Sthompsastatic int
1899188417Sthompsazyd_get_macaddr(struct zyd_softc *sc)
1900186730Salfred{
1901192984Sthompsa	struct usb_device_request req;
1902193045Sthompsa	usb_error_t error;
1903186730Salfred
1904186730Salfred	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1905186730Salfred	req.bRequest = ZYD_READFWDATAREQ;
1906186730Salfred	USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
1907186730Salfred	USETW(req.wIndex, 0);
1908186730Salfred	USETW(req.wLength, IEEE80211_ADDR_LEN);
1909186730Salfred
1910188419Sthompsa	error = zyd_do_request(sc, &req, sc->sc_bssid);
1911188417Sthompsa	if (error != 0) {
1912188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1913194228Sthompsa		    usbd_errstr(error));
1914188417Sthompsa	}
1915188417Sthompsa
1916188417Sthompsa	return (error);
1917186730Salfred}
1918186730Salfred
1919188417Sthompsastatic int
1920188417Sthompsazyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1921184610Salfred{
1922188417Sthompsa	int error;
1923184610Salfred	uint32_t tmp;
1924184610Salfred
1925188417Sthompsa	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1926188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
1927188417Sthompsa	tmp = addr[5] << 8 | addr[4];
1928188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
1929188417Sthompsafail:
1930188417Sthompsa	return (error);
1931188417Sthompsa}
1932184610Salfred
1933188417Sthompsastatic int
1934188417Sthompsazyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1935188417Sthompsa{
1936188417Sthompsa	int error;
1937188417Sthompsa	uint32_t tmp;
1938188417Sthompsa
1939188417Sthompsa	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1940188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
1941188417Sthompsa	tmp = addr[5] << 8 | addr[4];
1942188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
1943188417Sthompsafail:
1944188417Sthompsa	return (error);
1945184610Salfred}
1946184610Salfred
1947188417Sthompsastatic int
1948188417Sthompsazyd_switch_radio(struct zyd_softc *sc, int on)
1949184610Salfred{
1950188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1951188417Sthompsa	int error;
1952188417Sthompsa
1953188417Sthompsa	error = zyd_lock_phy(sc);
1954188417Sthompsa	if (error != 0)
1955188417Sthompsa		goto fail;
1956188417Sthompsa	error = (*rf->switch_radio)(rf, on);
1957188417Sthompsa	if (error != 0)
1958188417Sthompsa		goto fail;
1959188417Sthompsa	error = zyd_unlock_phy(sc);
1960188417Sthompsafail:
1961188417Sthompsa	return (error);
1962184610Salfred}
1963184610Salfred
1964188417Sthompsastatic int
1965188417Sthompsazyd_set_led(struct zyd_softc *sc, int which, int on)
1966184610Salfred{
1967188417Sthompsa	int error;
1968184610Salfred	uint32_t tmp;
1969184610Salfred
1970188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1971188417Sthompsa	tmp &= ~which;
1972188417Sthompsa	if (on)
1973188417Sthompsa		tmp |= which;
1974188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1975188417Sthompsafail:
1976188417Sthompsa	return (error);
1977188417Sthompsa}
1978184610Salfred
1979188417Sthompsastatic void
1980188417Sthompsazyd_set_multi(struct zyd_softc *sc)
1981184610Salfred{
1982188417Sthompsa	int error;
1983188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1984188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1985188417Sthompsa	struct ifmultiaddr *ifma;
1986188417Sthompsa	uint32_t low, high;
1987188417Sthompsa	uint8_t v;
1988184610Salfred
1989188417Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1990188417Sthompsa		return;
1991184610Salfred
1992188417Sthompsa	low = 0x00000000;
1993188417Sthompsa	high = 0x80000000;
1994184610Salfred
1995188417Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR ||
1996188417Sthompsa	    (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) {
1997188417Sthompsa		low = 0xffffffff;
1998188417Sthompsa		high = 0xffffffff;
1999184610Salfred	} else {
2000195049Srwatson		if_maddr_rlock(ifp);
2001188417Sthompsa		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2002188417Sthompsa			if (ifma->ifma_addr->sa_family != AF_LINK)
2003188417Sthompsa				continue;
2004188417Sthompsa			v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
2005188417Sthompsa			    ifma->ifma_addr))[5] >> 2;
2006188417Sthompsa			if (v < 32)
2007188417Sthompsa				low |= 1 << v;
2008188417Sthompsa			else
2009188417Sthompsa				high |= 1 << (v - 32);
2010188417Sthompsa		}
2011195049Srwatson		if_maddr_runlock(ifp);
2012184610Salfred	}
2013184610Salfred
2014188417Sthompsa	/* reprogram multicast global hash table */
2015188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBL, low);
2016188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBH, high);
2017188417Sthompsafail:
2018188417Sthompsa	if (error != 0)
2019188417Sthompsa		device_printf(sc->sc_dev,
2020188417Sthompsa		    "could not set multicast hash table\n");
2021188417Sthompsa}
2022184610Salfred
2023188417Sthompsastatic void
2024188417Sthompsazyd_update_mcast(struct ifnet *ifp)
2025188417Sthompsa{
2026188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2027184610Salfred
2028188417Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2029184610Salfred		return;
2030184610Salfred
2031188417Sthompsa	ZYD_LOCK(sc);
2032191746Sthompsa	zyd_set_multi(sc);
2033188417Sthompsa	ZYD_UNLOCK(sc);
2034188417Sthompsa}
2035184610Salfred
2036188417Sthompsastatic int
2037188417Sthompsazyd_set_rxfilter(struct zyd_softc *sc)
2038188417Sthompsa{
2039188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2040188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2041188417Sthompsa	uint32_t rxfilter;
2042184610Salfred
2043188417Sthompsa	switch (ic->ic_opmode) {
2044188417Sthompsa	case IEEE80211_M_STA:
2045188417Sthompsa		rxfilter = ZYD_FILTER_BSS;
2046188417Sthompsa		break;
2047188417Sthompsa	case IEEE80211_M_IBSS:
2048188417Sthompsa	case IEEE80211_M_HOSTAP:
2049188417Sthompsa		rxfilter = ZYD_FILTER_HOSTAP;
2050188417Sthompsa		break;
2051188417Sthompsa	case IEEE80211_M_MONITOR:
2052188417Sthompsa		rxfilter = ZYD_FILTER_MONITOR;
2053188417Sthompsa		break;
2054188417Sthompsa	default:
2055188417Sthompsa		/* should not get there */
2056188417Sthompsa		return (EINVAL);
2057184610Salfred	}
2058188417Sthompsa	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
2059188417Sthompsa}
2060184610Salfred
2061188417Sthompsastatic void
2062188417Sthompsazyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
2063188417Sthompsa{
2064188417Sthompsa	int error;
2065188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2066188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2067188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
2068188417Sthompsa	uint32_t tmp;
2069188601Sthompsa	int chan;
2070184610Salfred
2071188417Sthompsa	chan = ieee80211_chan2ieee(ic, c);
2072188417Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
2073188417Sthompsa		/* XXX should NEVER happen */
2074188417Sthompsa		device_printf(sc->sc_dev,
2075188417Sthompsa		    "%s: invalid channel %x\n", __func__, chan);
2076188417Sthompsa		return;
2077188417Sthompsa	}
2078184610Salfred
2079188417Sthompsa	error = zyd_lock_phy(sc);
2080188417Sthompsa	if (error != 0)
2081188417Sthompsa		goto fail;
2082184610Salfred
2083188417Sthompsa	error = (*rf->set_channel)(rf, chan);
2084188417Sthompsa	if (error != 0)
2085188417Sthompsa		goto fail;
2086184610Salfred
2087193420Sweongyo	if (rf->update_pwr) {
2088193420Sweongyo		/* update Tx power */
2089193420Sweongyo		zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
2090184610Salfred
2091193420Sweongyo		if (sc->sc_macrev == ZYD_ZD1211B) {
2092193420Sweongyo			zyd_write16_m(sc, ZYD_CR67,
2093193420Sweongyo			    sc->sc_ofdm36_cal[chan - 1]);
2094193420Sweongyo			zyd_write16_m(sc, ZYD_CR66,
2095193420Sweongyo			    sc->sc_ofdm48_cal[chan - 1]);
2096193420Sweongyo			zyd_write16_m(sc, ZYD_CR65,
2097193420Sweongyo			    sc->sc_ofdm54_cal[chan - 1]);
2098193420Sweongyo			zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
2099193420Sweongyo			zyd_write16_m(sc, ZYD_CR69, 0x28);
2100193420Sweongyo			zyd_write16_m(sc, ZYD_CR69, 0x2a);
2101193420Sweongyo		}
2102188417Sthompsa	}
2103188417Sthompsa	if (sc->sc_cckgain) {
2104188417Sthompsa		/* set CCK baseband gain from EEPROM */
2105188417Sthompsa		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
2106188417Sthompsa			zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
2107188417Sthompsa	}
2108188417Sthompsa	if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
2109188417Sthompsa		error = (*rf->bandedge6)(rf, c);
2110188417Sthompsa		if (error != 0)
2111188417Sthompsa			goto fail;
2112188417Sthompsa	}
2113188417Sthompsa	zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
2114184610Salfred
2115188417Sthompsa	error = zyd_unlock_phy(sc);
2116188417Sthompsa	if (error != 0)
2117188417Sthompsa		goto fail;
2118184610Salfred
2119188417Sthompsa	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
2120188417Sthompsa	    htole16(c->ic_freq);
2121188417Sthompsa	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
2122188417Sthompsa	    htole16(c->ic_flags);
2123188417Sthompsafail:
2124184610Salfred	return;
2125184610Salfred}
2126184610Salfred
2127184610Salfredstatic int
2128188417Sthompsazyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
2129184610Salfred{
2130188417Sthompsa	int error;
2131188417Sthompsa	uint32_t val;
2132184610Salfred
2133188417Sthompsa	zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
2134188417Sthompsa	sc->sc_atim_wnd = val;
2135188417Sthompsa	zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
2136188417Sthompsa	sc->sc_pre_tbtt = val;
2137188417Sthompsa	sc->sc_bcn_int = bintval;
2138184610Salfred
2139188417Sthompsa	if (sc->sc_bcn_int <= 5)
2140188417Sthompsa		sc->sc_bcn_int = 5;
2141188417Sthompsa	if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
2142188417Sthompsa		sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
2143188417Sthompsa	if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
2144188417Sthompsa		sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
2145184610Salfred
2146188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
2147188417Sthompsa	zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
2148188417Sthompsa	zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
2149188417Sthompsafail:
2150188417Sthompsa	return (error);
2151188417Sthompsa}
2152184610Salfred
2153188417Sthompsastatic void
2154192984Sthompsazyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
2155188417Sthompsa{
2156194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2157188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2158192468Ssam	struct ieee80211com *ic = ifp->if_l2com;
2159188417Sthompsa	struct zyd_plcphdr plcp;
2160188417Sthompsa	struct zyd_rx_stat stat;
2161194677Sthompsa	struct usb_page_cache *pc;
2162188417Sthompsa	struct mbuf *m;
2163188417Sthompsa	int rlen, rssi;
2164184610Salfred
2165188417Sthompsa	if (len < ZYD_MIN_FRAGSZ) {
2166188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
2167188417Sthompsa		    device_get_nameunit(sc->sc_dev), len);
2168188417Sthompsa		ifp->if_ierrors++;
2169188417Sthompsa		return;
2170188417Sthompsa	}
2171194677Sthompsa	pc = usbd_xfer_get_frame(xfer, 0);
2172194677Sthompsa	usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2173194677Sthompsa	usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
2174184610Salfred
2175188417Sthompsa	if (stat.flags & ZYD_RX_ERROR) {
2176188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV,
2177188417Sthompsa		    "%s: RX status indicated error (%x)\n",
2178188417Sthompsa		    device_get_nameunit(sc->sc_dev), stat.flags);
2179188417Sthompsa		ifp->if_ierrors++;
2180188417Sthompsa		return;
2181188417Sthompsa	}
2182184610Salfred
2183188417Sthompsa	/* compute actual frame length */
2184188417Sthompsa	rlen = len - sizeof(struct zyd_plcphdr) -
2185188417Sthompsa	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
2186184610Salfred
2187188417Sthompsa	/* allocate a mbuf to store the frame */
2188188417Sthompsa	if (rlen > MCLBYTES) {
2189188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
2190188417Sthompsa		    device_get_nameunit(sc->sc_dev), rlen);
2191188417Sthompsa		ifp->if_ierrors++;
2192188417Sthompsa		return;
2193188417Sthompsa	} else if (rlen > MHLEN)
2194188417Sthompsa		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2195188417Sthompsa	else
2196188417Sthompsa		m = m_gethdr(M_DONTWAIT, MT_DATA);
2197188417Sthompsa	if (m == NULL) {
2198188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
2199188417Sthompsa		    device_get_nameunit(sc->sc_dev));
2200188417Sthompsa		ifp->if_ierrors++;
2201188417Sthompsa		return;
2202184610Salfred	}
2203188417Sthompsa	m->m_pkthdr.rcvif = ifp;
2204188417Sthompsa	m->m_pkthdr.len = m->m_len = rlen;
2205194677Sthompsa	usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
2206184610Salfred
2207192468Ssam	if (ieee80211_radiotap_active(ic)) {
2208188417Sthompsa		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
2209184610Salfred
2210188417Sthompsa		tap->wr_flags = 0;
2211188417Sthompsa		if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
2212188417Sthompsa			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2213188417Sthompsa		/* XXX toss, no way to express errors */
2214188417Sthompsa		if (stat.flags & ZYD_RX_DECRYPTERR)
2215188417Sthompsa			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2216188417Sthompsa		tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
2217188417Sthompsa		    (stat.flags & ZYD_RX_OFDM) ?
2218188417Sthompsa			IEEE80211_T_OFDM : IEEE80211_T_CCK);
2219188417Sthompsa		tap->wr_antsignal = stat.rssi + -95;
2220188417Sthompsa		tap->wr_antnoise = -95;	/* XXX */
2221188417Sthompsa	}
2222188417Sthompsa	rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
2223184610Salfred
2224188417Sthompsa	sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
2225188417Sthompsa	sc->sc_rx_data[sc->sc_rx_count].m = m;
2226188417Sthompsa	sc->sc_rx_count++;
2227184610Salfred}
2228184610Salfred
2229184610Salfredstatic void
2230194677Sthompsazyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
2231184610Salfred{
2232194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2233184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2234184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
2235188417Sthompsa	struct ieee80211_node *ni;
2236188417Sthompsa	struct zyd_rx_desc desc;
2237188417Sthompsa	struct mbuf *m;
2238194677Sthompsa	struct usb_page_cache *pc;
2239188417Sthompsa	uint32_t offset;
2240188417Sthompsa	uint8_t rssi;
2241188417Sthompsa	int8_t nf;
2242188417Sthompsa	int i;
2243194677Sthompsa	int actlen;
2244184610Salfred
2245194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2246194677Sthompsa
2247188417Sthompsa	sc->sc_rx_count = 0;
2248188417Sthompsa	switch (USB_GET_STATE(xfer)) {
2249188417Sthompsa	case USB_ST_TRANSFERRED:
2250194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
2251194677Sthompsa		usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
2252184610Salfred
2253188417Sthompsa		offset = 0;
2254188417Sthompsa		if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
2255188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_RECV,
2256188417Sthompsa			    "%s: received multi-frame transfer\n", __func__);
2257184610Salfred
2258188417Sthompsa			for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2259188417Sthompsa				uint16_t len16 = UGETW(desc.len[i]);
2260184610Salfred
2261194677Sthompsa				if (len16 == 0 || len16 > actlen)
2262188417Sthompsa					break;
2263184610Salfred
2264188417Sthompsa				zyd_rx_data(xfer, offset, len16);
2265184610Salfred
2266188417Sthompsa				/* next frame is aligned on a 32-bit boundary */
2267188417Sthompsa				len16 = (len16 + 3) & ~3;
2268188417Sthompsa				offset += len16;
2269194677Sthompsa				if (len16 > actlen)
2270188417Sthompsa					break;
2271194677Sthompsa				actlen -= len16;
2272188417Sthompsa			}
2273188417Sthompsa		} else {
2274188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_RECV,
2275188417Sthompsa			    "%s: received single-frame transfer\n", __func__);
2276184610Salfred
2277194677Sthompsa			zyd_rx_data(xfer, 0, actlen);
2278188417Sthompsa		}
2279188417Sthompsa		/* FALLTHROUGH */
2280188417Sthompsa	case USB_ST_SETUP:
2281188417Sthompsatr_setup:
2282194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2283194228Sthompsa		usbd_transfer_submit(xfer);
2284184610Salfred
2285188417Sthompsa		/*
2286188417Sthompsa		 * At the end of a USB callback it is always safe to unlock
2287188417Sthompsa		 * the private mutex of a device! That is why we do the
2288188417Sthompsa		 * "ieee80211_input" here, and not some lines up!
2289188417Sthompsa		 */
2290188417Sthompsa		ZYD_UNLOCK(sc);
2291188417Sthompsa		for (i = 0; i < sc->sc_rx_count; i++) {
2292188417Sthompsa			rssi = sc->sc_rx_data[i].rssi;
2293188417Sthompsa			m = sc->sc_rx_data[i].m;
2294188417Sthompsa			sc->sc_rx_data[i].m = NULL;
2295184610Salfred
2296188417Sthompsa			nf = -95;	/* XXX */
2297184610Salfred
2298188417Sthompsa			ni = ieee80211_find_rxnode(ic,
2299188417Sthompsa			    mtod(m, struct ieee80211_frame_min *));
2300188417Sthompsa			if (ni != NULL) {
2301192468Ssam				(void)ieee80211_input(ni, m, rssi, nf);
2302188417Sthompsa				ieee80211_free_node(ni);
2303188417Sthompsa			} else
2304192468Ssam				(void)ieee80211_input_all(ic, m, rssi, nf);
2305188417Sthompsa		}
2306198099Sweongyo		if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
2307198099Sweongyo		    !IFQ_IS_EMPTY(&ifp->if_snd))
2308198099Sweongyo			zyd_start(ifp);
2309188417Sthompsa		ZYD_LOCK(sc);
2310188417Sthompsa		break;
2311184610Salfred
2312188417Sthompsa	default:			/* Error */
2313194677Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
2314184610Salfred
2315194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2316188417Sthompsa			/* try to clear stall first */
2317194677Sthompsa			usbd_xfer_set_stall(xfer);
2318188417Sthompsa			goto tr_setup;
2319184610Salfred		}
2320188417Sthompsa		break;
2321184610Salfred	}
2322184610Salfred}
2323184610Salfred
2324184610Salfredstatic uint8_t
2325193803Sweongyozyd_plcp_signal(struct zyd_softc *sc, int rate)
2326184610Salfred{
2327184610Salfred	switch (rate) {
2328188417Sthompsa	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
2329184610Salfred	case 12:
2330184610Salfred		return (0xb);
2331184610Salfred	case 18:
2332184610Salfred		return (0xf);
2333184610Salfred	case 24:
2334184610Salfred		return (0xa);
2335184610Salfred	case 36:
2336184610Salfred		return (0xe);
2337184610Salfred	case 48:
2338184610Salfred		return (0x9);
2339184610Salfred	case 72:
2340184610Salfred		return (0xd);
2341184610Salfred	case 96:
2342184610Salfred		return (0x8);
2343184610Salfred	case 108:
2344184610Salfred		return (0xc);
2345188417Sthompsa	/* CCK rates (NB: not IEEE std, device-specific) */
2346188417Sthompsa	case 2:
2347188417Sthompsa		return (0x0);
2348188417Sthompsa	case 4:
2349188417Sthompsa		return (0x1);
2350188417Sthompsa	case 11:
2351188417Sthompsa		return (0x2);
2352188417Sthompsa	case 22:
2353188417Sthompsa		return (0x3);
2354184610Salfred	}
2355184610Salfred
2356193803Sweongyo	device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2357193803Sweongyo	return (0x0);
2358184610Salfred}
2359184610Salfred
2360184610Salfredstatic void
2361194677Sthompsazyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
2362184610Salfred{
2363194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2364184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2365192468Ssam	struct ieee80211vap *vap;
2366188417Sthompsa	struct zyd_tx_data *data;
2367188417Sthompsa	struct mbuf *m;
2368194677Sthompsa	struct usb_page_cache *pc;
2369194677Sthompsa	int actlen;
2370184610Salfred
2371194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2372194677Sthompsa
2373188417Sthompsa	switch (USB_GET_STATE(xfer)) {
2374188417Sthompsa	case USB_ST_TRANSFERRED:
2375188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2376194677Sthompsa		    actlen);
2377184610Salfred
2378188417Sthompsa		/* free resources */
2379194677Sthompsa		data = usbd_xfer_get_priv(xfer);
2380188417Sthompsa		zyd_tx_free(data, 0);
2381194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2382184610Salfred
2383188417Sthompsa		ifp->if_opackets++;
2384188417Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2385184610Salfred
2386188417Sthompsa		/* FALLTHROUGH */
2387188417Sthompsa	case USB_ST_SETUP:
2388188417Sthompsatr_setup:
2389188417Sthompsa		data = STAILQ_FIRST(&sc->tx_q);
2390188417Sthompsa		if (data) {
2391188417Sthompsa			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
2392188417Sthompsa			m = data->m;
2393184610Salfred
2394188417Sthompsa			if (m->m_pkthdr.len > ZYD_MAX_TXBUFSZ) {
2395188417Sthompsa				DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
2396188417Sthompsa				    m->m_pkthdr.len);
2397188417Sthompsa				m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
2398188417Sthompsa			}
2399194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
2400194677Sthompsa			usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2401194677Sthompsa			usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
2402188417Sthompsa			    m->m_pkthdr.len);
2403184610Salfred
2404192468Ssam			vap = data->ni->ni_vap;
2405192468Ssam			if (ieee80211_radiotap_active_vap(vap)) {
2406188417Sthompsa				struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2407184610Salfred
2408188417Sthompsa				tap->wt_flags = 0;
2409188417Sthompsa				tap->wt_rate = data->rate;
2410184610Salfred
2411192468Ssam				ieee80211_radiotap_tx(vap, m);
2412188417Sthompsa			}
2413184610Salfred
2414194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2415194677Sthompsa			usbd_xfer_set_priv(xfer, data);
2416194228Sthompsa			usbd_transfer_submit(xfer);
2417188417Sthompsa		}
2418198099Sweongyo		ZYD_UNLOCK(sc);
2419198099Sweongyo		zyd_start(ifp);
2420198099Sweongyo		ZYD_LOCK(sc);
2421188417Sthompsa		break;
2422184610Salfred
2423188417Sthompsa	default:			/* Error */
2424188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2425194677Sthompsa		    usbd_errstr(error));
2426184610Salfred
2427188419Sthompsa		ifp->if_oerrors++;
2428194677Sthompsa		data = usbd_xfer_get_priv(xfer);
2429194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2430188419Sthompsa		if (data != NULL)
2431194677Sthompsa			zyd_tx_free(data, error);
2432188419Sthompsa
2433203141Sthompsa		if (error != USB_ERR_CANCELLED) {
2434203141Sthompsa			if (error == USB_ERR_TIMEOUT)
2435203141Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2436203141Sthompsa
2437203141Sthompsa			/*
2438203141Sthompsa			 * Try to clear stall first, also if other
2439203141Sthompsa			 * errors occur, hence clearing stall
2440203141Sthompsa			 * introduces a 50 ms delay:
2441203141Sthompsa			 */
2442194677Sthompsa			usbd_xfer_set_stall(xfer);
2443188417Sthompsa			goto tr_setup;
2444188417Sthompsa		}
2445184610Salfred		break;
2446184610Salfred	}
2447184610Salfred}
2448184610Salfred
2449188417Sthompsastatic int
2450193803Sweongyozyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2451184610Salfred{
2452188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2453188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2454188417Sthompsa	struct zyd_tx_desc *desc;
2455188417Sthompsa	struct zyd_tx_data *data;
2456188417Sthompsa	struct ieee80211_frame *wh;
2457188417Sthompsa	const struct ieee80211_txparam *tp;
2458188417Sthompsa	struct ieee80211_key *k;
2459188417Sthompsa	int rate, totlen;
2460193803Sweongyo	static uint8_t ratediv[] = ZYD_TX_RATEDIV;
2461193803Sweongyo	uint8_t phy;
2462188417Sthompsa	uint16_t pktlen;
2463193803Sweongyo	uint32_t bits;
2464184610Salfred
2465188417Sthompsa	wh = mtod(m0, struct ieee80211_frame *);
2466188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
2467188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
2468188417Sthompsa	sc->tx_nfree--;
2469184610Salfred
2470193803Sweongyo	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT ||
2471193803Sweongyo	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) {
2472193803Sweongyo		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2473193803Sweongyo		rate = tp->mgmtrate;
2474188417Sthompsa	} else {
2475193803Sweongyo		tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
2476193803Sweongyo		/* for data frames */
2477193803Sweongyo		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
2478193803Sweongyo			rate = tp->mcastrate;
2479193803Sweongyo		else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2480193803Sweongyo			rate = tp->ucastrate;
2481193803Sweongyo		else {
2482206358Srpaulo			(void) ieee80211_ratectl_rate(ni, NULL, 0);
2483193803Sweongyo			rate = ni->ni_txrate;
2484193803Sweongyo		}
2485184610Salfred	}
2486184610Salfred
2487188417Sthompsa	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2488188417Sthompsa		k = ieee80211_crypto_encap(ni, m0);
2489188417Sthompsa		if (k == NULL) {
2490188417Sthompsa			m_freem(m0);
2491188417Sthompsa			return (ENOBUFS);
2492188417Sthompsa		}
2493188417Sthompsa		/* packet header may have moved, reset our local pointer */
2494188417Sthompsa		wh = mtod(m0, struct ieee80211_frame *);
2495184610Salfred	}
2496184610Salfred
2497188417Sthompsa	data->ni = ni;
2498188417Sthompsa	data->m = m0;
2499193803Sweongyo	data->rate = rate;
2500184610Salfred
2501193803Sweongyo	/* fill Tx descriptor */
2502193803Sweongyo	desc = &data->desc;
2503193803Sweongyo	phy = zyd_plcp_signal(sc, rate);
2504193803Sweongyo	desc->phy = phy;
2505193803Sweongyo	if (ZYD_RATE_IS_OFDM(rate)) {
2506193803Sweongyo		desc->phy |= ZYD_TX_PHY_OFDM;
2507193803Sweongyo		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2508193803Sweongyo			desc->phy |= ZYD_TX_PHY_5GHZ;
2509193803Sweongyo	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2510193803Sweongyo		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2511193803Sweongyo
2512188417Sthompsa	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2513188417Sthompsa	desc->len = htole16(totlen);
2514184610Salfred
2515193803Sweongyo	desc->flags = ZYD_TX_FLAG_BACKOFF;
2516188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2517188417Sthompsa		/* multicast frames are not sent at OFDM rates in 802.11b/g */
2518188417Sthompsa		if (totlen > vap->iv_rtsthreshold) {
2519188417Sthompsa			desc->flags |= ZYD_TX_FLAG_RTS;
2520188417Sthompsa		} else if (ZYD_RATE_IS_OFDM(rate) &&
2521188417Sthompsa		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
2522188417Sthompsa			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2523188417Sthompsa				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2524188417Sthompsa			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2525188417Sthompsa				desc->flags |= ZYD_TX_FLAG_RTS;
2526188417Sthompsa		}
2527193803Sweongyo	} else
2528193803Sweongyo		desc->flags |= ZYD_TX_FLAG_MULTICAST;
2529188417Sthompsa	if ((wh->i_fc[0] &
2530188417Sthompsa	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2531188417Sthompsa	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2532188417Sthompsa		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2533188417Sthompsa
2534184610Salfred	/* actual transmit length (XXX why +10?) */
2535193803Sweongyo	pktlen = ZYD_TX_DESC_SIZE + 10;
2536188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211)
2537184610Salfred		pktlen += totlen;
2538188417Sthompsa	desc->pktlen = htole16(pktlen);
2539184610Salfred
2540193803Sweongyo	bits = (rate == 11) ? (totlen * 16) + 10 :
2541193803Sweongyo	    ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2542196809Sweongyo	desc->plcp_length = htole16(bits / ratediv[phy]);
2543188417Sthompsa	desc->plcp_service = 0;
2544193803Sweongyo	if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
2545193803Sweongyo		desc->plcp_service |= ZYD_PLCP_LENGEXT;
2546193803Sweongyo	desc->nextlen = 0;
2547193803Sweongyo
2548193803Sweongyo	if (ieee80211_radiotap_active_vap(vap)) {
2549193803Sweongyo		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2550193803Sweongyo
2551193803Sweongyo		tap->wt_flags = 0;
2552193803Sweongyo		tap->wt_rate = rate;
2553193803Sweongyo
2554193803Sweongyo		ieee80211_radiotap_tx(vap, m0);
2555184610Salfred	}
2556184610Salfred
2557188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_XMIT,
2558188417Sthompsa	    "%s: sending data frame len=%zu rate=%u\n",
2559188417Sthompsa	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2560188417Sthompsa		rate);
2561184610Salfred
2562188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2563194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
2564184610Salfred
2565188417Sthompsa	return (0);
2566184610Salfred}
2567184610Salfred
2568184610Salfredstatic void
2569188417Sthompsazyd_start(struct ifnet *ifp)
2570184610Salfred{
2571188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2572188417Sthompsa	struct ieee80211_node *ni;
2573184610Salfred	struct mbuf *m;
2574184610Salfred
2575188417Sthompsa	ZYD_LOCK(sc);
2576188417Sthompsa	for (;;) {
2577188417Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2578188417Sthompsa		if (m == NULL)
2579184610Salfred			break;
2580188417Sthompsa		if (sc->tx_nfree == 0) {
2581188417Sthompsa			IFQ_DRV_PREPEND(&ifp->if_snd, m);
2582188417Sthompsa			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2583184610Salfred			break;
2584184610Salfred		}
2585188417Sthompsa		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2586193803Sweongyo		if (zyd_tx_start(sc, m, ni) != 0) {
2587188417Sthompsa			ieee80211_free_node(ni);
2588188417Sthompsa			ifp->if_oerrors++;
2589188417Sthompsa			break;
2590184610Salfred		}
2591184610Salfred	}
2592188417Sthompsa	ZYD_UNLOCK(sc);
2593184610Salfred}
2594184610Salfred
2595188417Sthompsastatic int
2596188417Sthompsazyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2597188417Sthompsa	const struct ieee80211_bpf_params *params)
2598184610Salfred{
2599188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2600188417Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2601188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2602184610Salfred
2603188417Sthompsa	ZYD_LOCK(sc);
2604188417Sthompsa	/* prevent management frames from being sent if we're not ready */
2605188417Sthompsa	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2606188417Sthompsa		ZYD_UNLOCK(sc);
2607188417Sthompsa		m_freem(m);
2608188417Sthompsa		ieee80211_free_node(ni);
2609188417Sthompsa		return (ENETDOWN);
2610188417Sthompsa	}
2611188417Sthompsa	if (sc->tx_nfree == 0) {
2612188417Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2613188417Sthompsa		ZYD_UNLOCK(sc);
2614188417Sthompsa		m_freem(m);
2615188417Sthompsa		ieee80211_free_node(ni);
2616188417Sthompsa		return (ENOBUFS);		/* XXX */
2617188417Sthompsa	}
2618188417Sthompsa
2619188417Sthompsa	/*
2620188417Sthompsa	 * Legacy path; interpret frame contents to decide
2621188417Sthompsa	 * precisely how to send the frame.
2622188417Sthompsa	 * XXX raw path
2623188417Sthompsa	 */
2624193803Sweongyo	if (zyd_tx_start(sc, m, ni) != 0) {
2625188417Sthompsa		ZYD_UNLOCK(sc);
2626188417Sthompsa		ifp->if_oerrors++;
2627188417Sthompsa		ieee80211_free_node(ni);
2628188417Sthompsa		return (EIO);
2629188417Sthompsa	}
2630188417Sthompsa	ZYD_UNLOCK(sc);
2631188417Sthompsa	return (0);
2632184610Salfred}
2633184610Salfred
2634184610Salfredstatic int
2635188417Sthompsazyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2636184610Salfred{
2637184610Salfred	struct zyd_softc *sc = ifp->if_softc;
2638184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
2639188417Sthompsa	struct ifreq *ifr = (struct ifreq *) data;
2640188417Sthompsa	int error = 0, startall = 0;
2641184610Salfred
2642184610Salfred	switch (cmd) {
2643184610Salfred	case SIOCSIFFLAGS:
2644188417Sthompsa		ZYD_LOCK(sc);
2645184610Salfred		if (ifp->if_flags & IFF_UP) {
2646191746Sthompsa			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2647191746Sthompsa				zyd_init_locked(sc);
2648188417Sthompsa				startall = 1;
2649191746Sthompsa			} else
2650191746Sthompsa				zyd_set_multi(sc);
2651184610Salfred		} else {
2652191746Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2653191746Sthompsa				zyd_stop(sc);
2654184610Salfred		}
2655188417Sthompsa		ZYD_UNLOCK(sc);
2656188417Sthompsa		if (startall)
2657188417Sthompsa			ieee80211_start_all(ic);
2658184610Salfred		break;
2659184610Salfred	case SIOCGIFMEDIA:
2660188417Sthompsa		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2661184610Salfred		break;
2662188417Sthompsa	case SIOCGIFADDR:
2663184610Salfred		error = ether_ioctl(ifp, cmd, data);
2664184610Salfred		break;
2665188417Sthompsa	default:
2666188417Sthompsa		error = EINVAL;
2667188417Sthompsa		break;
2668184610Salfred	}
2669184610Salfred	return (error);
2670184610Salfred}
2671184610Salfred
2672184610Salfredstatic void
2673191746Sthompsazyd_init_locked(struct zyd_softc *sc)
2674184610Salfred{
2675188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2676188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2677192984Sthompsa	struct usb_config_descriptor *cd;
2678188419Sthompsa	int error;
2679188417Sthompsa	uint32_t val;
2680184610Salfred
2681188417Sthompsa	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2682184610Salfred
2683188417Sthompsa	if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
2684188417Sthompsa		error = zyd_loadfirmware(sc);
2685188417Sthompsa		if (error != 0) {
2686188417Sthompsa			device_printf(sc->sc_dev,
2687188417Sthompsa			    "could not load firmware (error=%d)\n", error);
2688188417Sthompsa			goto fail;
2689188417Sthompsa		}
2690188417Sthompsa
2691188417Sthompsa		/* reset device */
2692194228Sthompsa		cd = usbd_get_config_descriptor(sc->sc_udev);
2693194228Sthompsa		error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
2694188417Sthompsa		    cd->bConfigurationValue);
2695188417Sthompsa		if (error)
2696188417Sthompsa			device_printf(sc->sc_dev, "reset failed, continuing\n");
2697188417Sthompsa
2698188417Sthompsa		error = zyd_hw_init(sc);
2699188417Sthompsa		if (error) {
2700188417Sthompsa			device_printf(sc->sc_dev,
2701188417Sthompsa			    "hardware initialization failed\n");
2702188417Sthompsa			goto fail;
2703188417Sthompsa		}
2704188417Sthompsa
2705188417Sthompsa		device_printf(sc->sc_dev,
2706188417Sthompsa		    "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
2707188417Sthompsa		    "BE%x NP%x Gain%x F%x\n",
2708188417Sthompsa		    (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
2709188417Sthompsa		    sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
2710188417Sthompsa		    zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
2711188417Sthompsa		    sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
2712188417Sthompsa		    sc->sc_cckgain, sc->sc_fix_cr157);
2713188417Sthompsa
2714188417Sthompsa		/* read regulatory domain (currently unused) */
2715188417Sthompsa		zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
2716188417Sthompsa		sc->sc_regdomain = val >> 16;
2717188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
2718188417Sthompsa		    sc->sc_regdomain);
2719188417Sthompsa
2720188417Sthompsa		/* we'll do software WEP decryption for now */
2721188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
2722188417Sthompsa		    __func__);
2723188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2724188417Sthompsa
2725188417Sthompsa		sc->sc_flags |= ZYD_FLAG_INITONCE;
2726184610Salfred	}
2727184610Salfred
2728188417Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2729191746Sthompsa		zyd_stop(sc);
2730188417Sthompsa
2731190526Ssam	DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
2732190526Ssam	    IF_LLADDR(ifp), ":");
2733190526Ssam	error = zyd_set_macaddr(sc, IF_LLADDR(ifp));
2734188417Sthompsa	if (error != 0)
2735184610Salfred		return;
2736184610Salfred
2737188417Sthompsa	/* set basic rates */
2738188417Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B)
2739188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
2740188417Sthompsa	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2741188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
2742188417Sthompsa	else	/* assumes 802.11b/g */
2743188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
2744184610Salfred
2745188417Sthompsa	/* promiscuous mode */
2746188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
2747188417Sthompsa	/* multicast setup */
2748188417Sthompsa	zyd_set_multi(sc);
2749188417Sthompsa	/* set RX filter  */
2750188417Sthompsa	error = zyd_set_rxfilter(sc);
2751188417Sthompsa	if (error != 0)
2752188417Sthompsa		goto fail;
2753184610Salfred
2754188417Sthompsa	/* switch radio transmitter ON */
2755188417Sthompsa	error = zyd_switch_radio(sc, 1);
2756188417Sthompsa	if (error != 0)
2757188417Sthompsa		goto fail;
2758188417Sthompsa	/* set default BSS channel */
2759188417Sthompsa	zyd_set_chan(sc, ic->ic_curchan);
2760184610Salfred
2761188417Sthompsa	/*
2762188417Sthompsa	 * Allocate Tx and Rx xfer queues.
2763188417Sthompsa	 */
2764188419Sthompsa	zyd_setup_tx_list(sc);
2765184610Salfred
2766188417Sthompsa	/* enable interrupts */
2767188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2768184610Salfred
2769188417Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2770188417Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2771194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2772194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2773194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
2774184610Salfred
2775188417Sthompsa	return;
2776184610Salfred
2777191746Sthompsafail:	zyd_stop(sc);
2778188417Sthompsa	return;
2779184610Salfred}
2780184610Salfred
2781184610Salfredstatic void
2782188417Sthompsazyd_init(void *priv)
2783184610Salfred{
2784188417Sthompsa	struct zyd_softc *sc = priv;
2785188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2786188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2787184610Salfred
2788188417Sthompsa	ZYD_LOCK(sc);
2789191746Sthompsa	zyd_init_locked(sc);
2790188417Sthompsa	ZYD_UNLOCK(sc);
2791188417Sthompsa
2792188417Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2793188417Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2794184610Salfred}
2795184610Salfred
2796184610Salfredstatic void
2797191746Sthompsazyd_stop(struct zyd_softc *sc)
2798184610Salfred{
2799188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2800188417Sthompsa	int error;
2801184610Salfred
2802188417Sthompsa	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2803184610Salfred
2804188417Sthompsa	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2805184610Salfred
2806188417Sthompsa	/*
2807188419Sthompsa	 * Drain all the transfers, if not already drained:
2808188417Sthompsa	 */
2809188419Sthompsa	ZYD_UNLOCK(sc);
2810194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2811194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
2812188419Sthompsa	ZYD_LOCK(sc);
2813188417Sthompsa
2814188419Sthompsa	zyd_unsetup_tx_list(sc);
2815188417Sthompsa
2816188419Sthompsa	/* Stop now if the device was never set up */
2817188419Sthompsa	if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
2818184610Salfred		return;
2819184610Salfred
2820188417Sthompsa	/* switch radio transmitter OFF */
2821188417Sthompsa	error = zyd_switch_radio(sc, 0);
2822188417Sthompsa	if (error != 0)
2823188417Sthompsa		goto fail;
2824188417Sthompsa	/* disable Rx */
2825188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
2826188417Sthompsa	/* disable interrupts */
2827188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
2828184610Salfred
2829188417Sthompsafail:
2830188417Sthompsa	return;
2831184610Salfred}
2832184610Salfred
2833188417Sthompsastatic int
2834188417Sthompsazyd_loadfirmware(struct zyd_softc *sc)
2835184610Salfred{
2836192984Sthompsa	struct usb_device_request req;
2837188417Sthompsa	size_t size;
2838188417Sthompsa	u_char *fw;
2839188417Sthompsa	uint8_t stat;
2840188417Sthompsa	uint16_t addr;
2841184610Salfred
2842188417Sthompsa	if (sc->sc_flags & ZYD_FLAG_FWLOADED)
2843188417Sthompsa		return (0);
2844184610Salfred
2845188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211) {
2846188417Sthompsa		fw = (u_char *)zd1211_firmware;
2847188417Sthompsa		size = sizeof(zd1211_firmware);
2848188417Sthompsa	} else {
2849188417Sthompsa		fw = (u_char *)zd1211b_firmware;
2850188417Sthompsa		size = sizeof(zd1211b_firmware);
2851184610Salfred	}
2852184610Salfred
2853188417Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2854188417Sthompsa	req.bRequest = ZYD_DOWNLOADREQ;
2855188417Sthompsa	USETW(req.wIndex, 0);
2856184610Salfred
2857188417Sthompsa	addr = ZYD_FIRMWARE_START_ADDR;
2858188417Sthompsa	while (size > 0) {
2859188417Sthompsa		/*
2860188417Sthompsa		 * When the transfer size is 4096 bytes, it is not
2861188417Sthompsa		 * likely to be able to transfer it.
2862188417Sthompsa		 * The cause is port or machine or chip?
2863188417Sthompsa		 */
2864188417Sthompsa		const int mlen = min(size, 64);
2865184610Salfred
2866188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_FW,
2867188417Sthompsa		    "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
2868184610Salfred
2869188417Sthompsa		USETW(req.wValue, addr);
2870188417Sthompsa		USETW(req.wLength, mlen);
2871188419Sthompsa		if (zyd_do_request(sc, &req, fw) != 0)
2872188417Sthompsa			return (EIO);
2873184610Salfred
2874188417Sthompsa		addr += mlen / 2;
2875188417Sthompsa		fw   += mlen;
2876188417Sthompsa		size -= mlen;
2877184610Salfred	}
2878184610Salfred
2879188417Sthompsa	/* check whether the upload succeeded */
2880188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2881188417Sthompsa	req.bRequest = ZYD_DOWNLOADSTS;
2882188417Sthompsa	USETW(req.wValue, 0);
2883188417Sthompsa	USETW(req.wIndex, 0);
2884188417Sthompsa	USETW(req.wLength, sizeof(stat));
2885188419Sthompsa	if (zyd_do_request(sc, &req, &stat) != 0)
2886188417Sthompsa		return (EIO);
2887184610Salfred
2888188417Sthompsa	sc->sc_flags |= ZYD_FLAG_FWLOADED;
2889184610Salfred
2890188417Sthompsa	return (stat & 0x80) ? (EIO) : (0);
2891184610Salfred}
2892184610Salfred
2893184610Salfredstatic void
2894188417Sthompsazyd_scan_start(struct ieee80211com *ic)
2895184610Salfred{
2896191746Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2897191746Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2898184610Salfred
2899188417Sthompsa	ZYD_LOCK(sc);
2900191746Sthompsa	/* want broadcast address while scanning */
2901191746Sthompsa	zyd_set_bssid(sc, ifp->if_broadcastaddr);
2902188417Sthompsa	ZYD_UNLOCK(sc);
2903184610Salfred}
2904184610Salfred
2905184610Salfredstatic void
2906188417Sthompsazyd_scan_end(struct ieee80211com *ic)
2907184610Salfred{
2908188417Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
2909184610Salfred
2910188417Sthompsa	ZYD_LOCK(sc);
2911191746Sthompsa	/* restore previous bssid */
2912191746Sthompsa	zyd_set_bssid(sc, sc->sc_bssid);
2913188417Sthompsa	ZYD_UNLOCK(sc);
2914184610Salfred}
2915184610Salfred
2916184610Salfredstatic void
2917188417Sthompsazyd_set_channel(struct ieee80211com *ic)
2918184610Salfred{
2919188417Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
2920184610Salfred
2921188417Sthompsa	ZYD_LOCK(sc);
2922191746Sthompsa	zyd_set_chan(sc, ic->ic_curchan);
2923188417Sthompsa	ZYD_UNLOCK(sc);
2924184610Salfred}
2925184610Salfred
2926188417Sthompsastatic device_method_t zyd_methods[] = {
2927188417Sthompsa        /* Device interface */
2928188417Sthompsa        DEVMETHOD(device_probe, zyd_match),
2929188417Sthompsa        DEVMETHOD(device_attach, zyd_attach),
2930188417Sthompsa        DEVMETHOD(device_detach, zyd_detach),
2931191746Sthompsa
2932188417Sthompsa	{ 0, 0 }
2933188417Sthompsa};
2934184610Salfred
2935188417Sthompsastatic driver_t zyd_driver = {
2936188417Sthompsa        "zyd",
2937188417Sthompsa        zyd_methods,
2938188417Sthompsa        sizeof(struct zyd_softc)
2939188417Sthompsa};
2940184610Salfred
2941188417Sthompsastatic devclass_t zyd_devclass;
2942184610Salfred
2943189275SthompsaDRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0);
2944188942SthompsaMODULE_DEPEND(zyd, usb, 1, 1, 1);
2945188417SthompsaMODULE_DEPEND(zyd, wlan, 1, 1, 1);
2946212122SthompsaMODULE_VERSION(zyd, 1);
2947