if_zyd.c revision 262007
1/*	$OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $	*/
2/*	$NetBSD: if_zyd.c,v 1.7 2007/06/21 04:04:29 kiyohara Exp $	*/
3/*	$FreeBSD: stable/10/sys/dev/usb/wlan/if_zyd.c 262007 2014-02-17 01:36:53Z kevlo $	*/
4
5/*-
6 * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr>
7 * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22#include <sys/cdefs.h>
23__FBSDID("$FreeBSD: stable/10/sys/dev/usb/wlan/if_zyd.c 262007 2014-02-17 01:36:53Z kevlo $");
24
25/*
26 * ZyDAS ZD1211/ZD1211B USB WLAN driver.
27 */
28
29#include <sys/param.h>
30#include <sys/sockio.h>
31#include <sys/sysctl.h>
32#include <sys/lock.h>
33#include <sys/mutex.h>
34#include <sys/condvar.h>
35#include <sys/mbuf.h>
36#include <sys/kernel.h>
37#include <sys/socket.h>
38#include <sys/systm.h>
39#include <sys/malloc.h>
40#include <sys/module.h>
41#include <sys/bus.h>
42#include <sys/endian.h>
43#include <sys/kdb.h>
44
45#include <machine/bus.h>
46#include <machine/resource.h>
47#include <sys/rman.h>
48
49#include <net/bpf.h>
50#include <net/if.h>
51#include <net/if_arp.h>
52#include <net/ethernet.h>
53#include <net/if_dl.h>
54#include <net/if_media.h>
55#include <net/if_types.h>
56
57#ifdef INET
58#include <netinet/in.h>
59#include <netinet/in_systm.h>
60#include <netinet/in_var.h>
61#include <netinet/if_ether.h>
62#include <netinet/ip.h>
63#endif
64
65#include <net80211/ieee80211_var.h>
66#include <net80211/ieee80211_regdomain.h>
67#include <net80211/ieee80211_radiotap.h>
68#include <net80211/ieee80211_ratectl.h>
69
70#include <dev/usb/usb.h>
71#include <dev/usb/usbdi.h>
72#include <dev/usb/usbdi_util.h>
73#include "usbdevs.h"
74
75#include <dev/usb/wlan/if_zydreg.h>
76#include <dev/usb/wlan/if_zydfw.h>
77
78#ifdef USB_DEBUG
79static int zyd_debug = 0;
80
81static SYSCTL_NODE(_hw_usb, OID_AUTO, zyd, CTLFLAG_RW, 0, "USB zyd");
82SYSCTL_INT(_hw_usb_zyd, OID_AUTO, debug, CTLFLAG_RW, &zyd_debug, 0,
83    "zyd debug level");
84
85enum {
86	ZYD_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
87	ZYD_DEBUG_RECV		= 0x00000002,	/* basic recv operation */
88	ZYD_DEBUG_RESET		= 0x00000004,	/* reset processing */
89	ZYD_DEBUG_INIT		= 0x00000008,	/* device init */
90	ZYD_DEBUG_TX_PROC	= 0x00000010,	/* tx ISR proc */
91	ZYD_DEBUG_RX_PROC	= 0x00000020,	/* rx ISR proc */
92	ZYD_DEBUG_STATE		= 0x00000040,	/* 802.11 state transitions */
93	ZYD_DEBUG_STAT		= 0x00000080,	/* statistic */
94	ZYD_DEBUG_FW		= 0x00000100,	/* firmware */
95	ZYD_DEBUG_CMD		= 0x00000200,	/* fw commands */
96	ZYD_DEBUG_ANY		= 0xffffffff
97};
98#define	DPRINTF(sc, m, fmt, ...) do {				\
99	if (zyd_debug & (m))					\
100		printf("%s: " fmt, __func__, ## __VA_ARGS__);	\
101} while (0)
102#else
103#define	DPRINTF(sc, m, fmt, ...) do {				\
104	(void) sc;						\
105} while (0)
106#endif
107
108#define	zyd_do_request(sc,req,data) \
109    usbd_do_request_flags((sc)->sc_udev, &(sc)->sc_mtx, req, data, 0, NULL, 5000)
110
111static device_probe_t zyd_match;
112static device_attach_t zyd_attach;
113static device_detach_t zyd_detach;
114
115static usb_callback_t zyd_intr_read_callback;
116static usb_callback_t zyd_intr_write_callback;
117static usb_callback_t zyd_bulk_read_callback;
118static usb_callback_t zyd_bulk_write_callback;
119
120static struct ieee80211vap *zyd_vap_create(struct ieee80211com *,
121		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
122		    const uint8_t [IEEE80211_ADDR_LEN],
123		    const uint8_t [IEEE80211_ADDR_LEN]);
124static void	zyd_vap_delete(struct ieee80211vap *);
125static void	zyd_tx_free(struct zyd_tx_data *, int);
126static void	zyd_setup_tx_list(struct zyd_softc *);
127static void	zyd_unsetup_tx_list(struct zyd_softc *);
128static int	zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int);
129static int	zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
130		    void *, int, int);
131static int	zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
132static int	zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
133static int	zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
134static int	zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
135static int	zyd_rfwrite(struct zyd_softc *, uint32_t);
136static int	zyd_lock_phy(struct zyd_softc *);
137static int	zyd_unlock_phy(struct zyd_softc *);
138static int	zyd_rf_attach(struct zyd_softc *, uint8_t);
139static const char *zyd_rf_name(uint8_t);
140static int	zyd_hw_init(struct zyd_softc *);
141static int	zyd_read_pod(struct zyd_softc *);
142static int	zyd_read_eeprom(struct zyd_softc *);
143static int	zyd_get_macaddr(struct zyd_softc *);
144static int	zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
145static int	zyd_set_bssid(struct zyd_softc *, const uint8_t *);
146static int	zyd_switch_radio(struct zyd_softc *, int);
147static int	zyd_set_led(struct zyd_softc *, int, int);
148static void	zyd_set_multi(struct zyd_softc *);
149static void	zyd_update_mcast(struct ifnet *);
150static int	zyd_set_rxfilter(struct zyd_softc *);
151static void	zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
152static int	zyd_set_beacon_interval(struct zyd_softc *, int);
153static void	zyd_rx_data(struct usb_xfer *, int, uint16_t);
154static int	zyd_tx_start(struct zyd_softc *, struct mbuf *,
155		    struct ieee80211_node *);
156static void	zyd_start(struct ifnet *);
157static int	zyd_raw_xmit(struct ieee80211_node *, struct mbuf *,
158		    const struct ieee80211_bpf_params *);
159static int	zyd_ioctl(struct ifnet *, u_long, caddr_t);
160static void	zyd_init_locked(struct zyd_softc *);
161static void	zyd_init(void *);
162static void	zyd_stop(struct zyd_softc *);
163static int	zyd_loadfirmware(struct zyd_softc *);
164static void	zyd_scan_start(struct ieee80211com *);
165static void	zyd_scan_end(struct ieee80211com *);
166static void	zyd_set_channel(struct ieee80211com *);
167static int	zyd_rfmd_init(struct zyd_rf *);
168static int	zyd_rfmd_switch_radio(struct zyd_rf *, int);
169static int	zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
170static int	zyd_al2230_init(struct zyd_rf *);
171static int	zyd_al2230_switch_radio(struct zyd_rf *, int);
172static int	zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
173static int	zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t);
174static int	zyd_al2230_init_b(struct zyd_rf *);
175static int	zyd_al7230B_init(struct zyd_rf *);
176static int	zyd_al7230B_switch_radio(struct zyd_rf *, int);
177static int	zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
178static int	zyd_al2210_init(struct zyd_rf *);
179static int	zyd_al2210_switch_radio(struct zyd_rf *, int);
180static int	zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
181static int	zyd_gct_init(struct zyd_rf *);
182static int	zyd_gct_switch_radio(struct zyd_rf *, int);
183static int	zyd_gct_set_channel(struct zyd_rf *, uint8_t);
184static int	zyd_gct_mode(struct zyd_rf *);
185static int	zyd_gct_set_channel_synth(struct zyd_rf *, int, int);
186static int	zyd_gct_write(struct zyd_rf *, uint16_t);
187static int	zyd_gct_txgain(struct zyd_rf *, uint8_t);
188static int	zyd_maxim2_init(struct zyd_rf *);
189static int	zyd_maxim2_switch_radio(struct zyd_rf *, int);
190static int	zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
191
192static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
193static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
194
195/* various supported device vendors/products */
196#define ZYD_ZD1211	0
197#define ZYD_ZD1211B	1
198
199#define	ZYD_ZD1211_DEV(v,p)	\
200	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211) }
201#define	ZYD_ZD1211B_DEV(v,p)	\
202	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211B) }
203static const STRUCT_USB_HOST_ID zyd_devs[] = {
204	/* ZYD_ZD1211 */
205	ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
206	ZYD_ZD1211_DEV(ABOCOM, WL54),
207	ZYD_ZD1211_DEV(ASUS, WL159G),
208	ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
209	ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
210	ZYD_ZD1211_DEV(PLANEX2, GWUS54GD),
211	ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
212	ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
213	ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
214	ZYD_ZD1211_DEV(SAGEM, XG760A),
215	ZYD_ZD1211_DEV(SENAO, NUB8301),
216	ZYD_ZD1211_DEV(SITECOMEU, WL113),
217	ZYD_ZD1211_DEV(SWEEX, ZD1211),
218	ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
219	ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
220	ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
221	ZYD_ZD1211_DEV(TWINMOS, G240),
222	ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
223	ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
224	ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
225	ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
226	ZYD_ZD1211_DEV(ZCOM, ZD1211),
227	ZYD_ZD1211_DEV(ZYDAS, ZD1211),
228	ZYD_ZD1211_DEV(ZYXEL, AG225H),
229	ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
230	ZYD_ZD1211_DEV(ZYXEL, G200V2),
231	/* ZYD_ZD1211B */
232	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG_NF),
233	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
234	ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
235	ZYD_ZD1211B_DEV(ASUS, A9T_WIFI),
236	ZYD_ZD1211B_DEV(BELKIN, F5D7050_V4000),
237	ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
238	ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
239	ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
240	ZYD_ZD1211B_DEV(MELCO, KG54L),
241	ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
242	ZYD_ZD1211B_DEV(PLANEX2, GW_US54GXS),
243	ZYD_ZD1211B_DEV(SAGEM, XG76NA),
244	ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
245	ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
246	ZYD_ZD1211B_DEV(USR, USR5423),
247	ZYD_ZD1211B_DEV(VTECH, ZD1211B),
248	ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
249	ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
250	ZYD_ZD1211B_DEV(ZYXEL, M202),
251	ZYD_ZD1211B_DEV(ZYXEL, G202),
252	ZYD_ZD1211B_DEV(ZYXEL, G220V2)
253};
254
255static const struct usb_config zyd_config[ZYD_N_TRANSFER] = {
256	[ZYD_BULK_WR] = {
257		.type = UE_BULK,
258		.endpoint = UE_ADDR_ANY,
259		.direction = UE_DIR_OUT,
260		.bufsize = ZYD_MAX_TXBUFSZ,
261		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
262		.callback = zyd_bulk_write_callback,
263		.ep_index = 0,
264		.timeout = 10000,	/* 10 seconds */
265	},
266	[ZYD_BULK_RD] = {
267		.type = UE_BULK,
268		.endpoint = UE_ADDR_ANY,
269		.direction = UE_DIR_IN,
270		.bufsize = ZYX_MAX_RXBUFSZ,
271		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
272		.callback = zyd_bulk_read_callback,
273		.ep_index = 0,
274	},
275	[ZYD_INTR_WR] = {
276		.type = UE_BULK_INTR,
277		.endpoint = UE_ADDR_ANY,
278		.direction = UE_DIR_OUT,
279		.bufsize = sizeof(struct zyd_cmd),
280		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
281		.callback = zyd_intr_write_callback,
282		.timeout = 1000,	/* 1 second */
283		.ep_index = 1,
284	},
285	[ZYD_INTR_RD] = {
286		.type = UE_INTERRUPT,
287		.endpoint = UE_ADDR_ANY,
288		.direction = UE_DIR_IN,
289		.bufsize = sizeof(struct zyd_cmd),
290		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
291		.callback = zyd_intr_read_callback,
292	},
293};
294#define zyd_read16_m(sc, val, data)	do {				\
295	error = zyd_read16(sc, val, data);				\
296	if (error != 0)							\
297		goto fail;						\
298} while (0)
299#define zyd_write16_m(sc, val, data)	do {				\
300	error = zyd_write16(sc, val, data);				\
301	if (error != 0)							\
302		goto fail;						\
303} while (0)
304#define zyd_read32_m(sc, val, data)	do {				\
305	error = zyd_read32(sc, val, data);				\
306	if (error != 0)							\
307		goto fail;						\
308} while (0)
309#define zyd_write32_m(sc, val, data)	do {				\
310	error = zyd_write32(sc, val, data);				\
311	if (error != 0)							\
312		goto fail;						\
313} while (0)
314
315static int
316zyd_match(device_t dev)
317{
318	struct usb_attach_arg *uaa = device_get_ivars(dev);
319
320	if (uaa->usb_mode != USB_MODE_HOST)
321		return (ENXIO);
322	if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX)
323		return (ENXIO);
324	if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX)
325		return (ENXIO);
326
327	return (usbd_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa));
328}
329
330static int
331zyd_attach(device_t dev)
332{
333	struct usb_attach_arg *uaa = device_get_ivars(dev);
334	struct zyd_softc *sc = device_get_softc(dev);
335	struct ifnet *ifp;
336	struct ieee80211com *ic;
337	uint8_t iface_index, bands;
338	int error;
339
340	if (uaa->info.bcdDevice < 0x4330) {
341		device_printf(dev, "device version mismatch: 0x%X "
342		    "(only >= 43.30 supported)\n",
343		    uaa->info.bcdDevice);
344		return (EINVAL);
345	}
346
347	device_set_usb_desc(dev);
348	sc->sc_dev = dev;
349	sc->sc_udev = uaa->device;
350	sc->sc_macrev = USB_GET_DRIVER_INFO(uaa);
351
352	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
353	    MTX_NETWORK_LOCK, MTX_DEF);
354	STAILQ_INIT(&sc->sc_rqh);
355
356	iface_index = ZYD_IFACE_INDEX;
357	error = usbd_transfer_setup(uaa->device,
358	    &iface_index, sc->sc_xfer, zyd_config,
359	    ZYD_N_TRANSFER, sc, &sc->sc_mtx);
360	if (error) {
361		device_printf(dev, "could not allocate USB transfers, "
362		    "err=%s\n", usbd_errstr(error));
363		goto detach;
364	}
365
366	ZYD_LOCK(sc);
367	if ((error = zyd_get_macaddr(sc)) != 0) {
368		device_printf(sc->sc_dev, "could not read EEPROM\n");
369		ZYD_UNLOCK(sc);
370		goto detach;
371	}
372	ZYD_UNLOCK(sc);
373
374	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
375	if (ifp == NULL) {
376		device_printf(sc->sc_dev, "can not if_alloc()\n");
377		goto detach;
378	}
379	ifp->if_softc = sc;
380	if_initname(ifp, "zyd", device_get_unit(sc->sc_dev));
381	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
382	ifp->if_init = zyd_init;
383	ifp->if_ioctl = zyd_ioctl;
384	ifp->if_start = zyd_start;
385	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
386	IFQ_SET_READY(&ifp->if_snd);
387
388	ic = ifp->if_l2com;
389	ic->ic_ifp = ifp;
390	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
391	ic->ic_opmode = IEEE80211_M_STA;
392
393	/* set device capabilities */
394	ic->ic_caps =
395		  IEEE80211_C_STA		/* station mode */
396		| IEEE80211_C_MONITOR		/* monitor mode */
397		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
398	        | IEEE80211_C_SHSLOT		/* short slot time supported */
399		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
400	        | IEEE80211_C_WPA		/* 802.11i */
401		;
402
403	bands = 0;
404	setbit(&bands, IEEE80211_MODE_11B);
405	setbit(&bands, IEEE80211_MODE_11G);
406	ieee80211_init_channels(ic, NULL, &bands);
407
408	ieee80211_ifattach(ic, sc->sc_bssid);
409	ic->ic_raw_xmit = zyd_raw_xmit;
410	ic->ic_scan_start = zyd_scan_start;
411	ic->ic_scan_end = zyd_scan_end;
412	ic->ic_set_channel = zyd_set_channel;
413
414	ic->ic_vap_create = zyd_vap_create;
415	ic->ic_vap_delete = zyd_vap_delete;
416	ic->ic_update_mcast = zyd_update_mcast;
417	ic->ic_update_promisc = zyd_update_mcast;
418
419	ieee80211_radiotap_attach(ic,
420	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
421		ZYD_TX_RADIOTAP_PRESENT,
422	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
423		ZYD_RX_RADIOTAP_PRESENT);
424
425	if (bootverbose)
426		ieee80211_announce(ic);
427
428	return (0);
429
430detach:
431	zyd_detach(dev);
432	return (ENXIO);			/* failure */
433}
434
435static int
436zyd_detach(device_t dev)
437{
438	struct zyd_softc *sc = device_get_softc(dev);
439	struct ifnet *ifp = sc->sc_ifp;
440	struct ieee80211com *ic;
441	unsigned int x;
442
443	/*
444	 * Prevent further allocations from RX/TX data
445	 * lists and ioctls:
446	 */
447	ZYD_LOCK(sc);
448	sc->sc_flags |= ZYD_FLAG_DETACHED;
449	STAILQ_INIT(&sc->tx_q);
450	STAILQ_INIT(&sc->tx_free);
451	ZYD_UNLOCK(sc);
452
453	/* drain USB transfers */
454	for (x = 0; x != ZYD_N_TRANSFER; x++)
455		usbd_transfer_drain(sc->sc_xfer[x]);
456
457	/* free TX list, if any */
458	ZYD_LOCK(sc);
459	zyd_unsetup_tx_list(sc);
460	ZYD_UNLOCK(sc);
461
462	/* free USB transfers and some data buffers */
463	usbd_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER);
464
465	if (ifp) {
466		ic = ifp->if_l2com;
467		ieee80211_ifdetach(ic);
468		if_free(ifp);
469	}
470	mtx_destroy(&sc->sc_mtx);
471
472	return (0);
473}
474
475static struct ieee80211vap *
476zyd_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
477    enum ieee80211_opmode opmode, int flags,
478    const uint8_t bssid[IEEE80211_ADDR_LEN],
479    const uint8_t mac[IEEE80211_ADDR_LEN])
480{
481	struct zyd_vap *zvp;
482	struct ieee80211vap *vap;
483
484	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
485		return (NULL);
486	zvp = (struct zyd_vap *) malloc(sizeof(struct zyd_vap),
487	    M_80211_VAP, M_NOWAIT | M_ZERO);
488	if (zvp == NULL)
489		return (NULL);
490	vap = &zvp->vap;
491
492	/* enable s/w bmiss handling for sta mode */
493	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
494	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac) != 0) {
495		/* out of memory */
496		free(zvp, M_80211_VAP);
497		return (NULL);
498	}
499
500	/* override state transition machine */
501	zvp->newstate = vap->iv_newstate;
502	vap->iv_newstate = zyd_newstate;
503
504	ieee80211_ratectl_init(vap);
505	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
506
507	/* complete setup */
508	ieee80211_vap_attach(vap, ieee80211_media_change,
509	    ieee80211_media_status);
510	ic->ic_opmode = opmode;
511	return (vap);
512}
513
514static void
515zyd_vap_delete(struct ieee80211vap *vap)
516{
517	struct zyd_vap *zvp = ZYD_VAP(vap);
518
519	ieee80211_ratectl_deinit(vap);
520	ieee80211_vap_detach(vap);
521	free(zvp, M_80211_VAP);
522}
523
524static void
525zyd_tx_free(struct zyd_tx_data *data, int txerr)
526{
527	struct zyd_softc *sc = data->sc;
528
529	if (data->m != NULL) {
530		if (data->m->m_flags & M_TXCB)
531			ieee80211_process_callback(data->ni, data->m,
532			    txerr ? ETIMEDOUT : 0);
533		m_freem(data->m);
534		data->m = NULL;
535
536		ieee80211_free_node(data->ni);
537		data->ni = NULL;
538	}
539	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
540	sc->tx_nfree++;
541}
542
543static void
544zyd_setup_tx_list(struct zyd_softc *sc)
545{
546	struct zyd_tx_data *data;
547	int i;
548
549	sc->tx_nfree = 0;
550	STAILQ_INIT(&sc->tx_q);
551	STAILQ_INIT(&sc->tx_free);
552
553	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
554		data = &sc->tx_data[i];
555
556		data->sc = sc;
557		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
558		sc->tx_nfree++;
559	}
560}
561
562static void
563zyd_unsetup_tx_list(struct zyd_softc *sc)
564{
565	struct zyd_tx_data *data;
566	int i;
567
568	/* make sure any subsequent use of the queues will fail */
569	sc->tx_nfree = 0;
570	STAILQ_INIT(&sc->tx_q);
571	STAILQ_INIT(&sc->tx_free);
572
573	/* free up all node references and mbufs */
574	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
575		data = &sc->tx_data[i];
576
577		if (data->m != NULL) {
578			m_freem(data->m);
579			data->m = NULL;
580		}
581		if (data->ni != NULL) {
582			ieee80211_free_node(data->ni);
583			data->ni = NULL;
584		}
585	}
586}
587
588static int
589zyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
590{
591	struct zyd_vap *zvp = ZYD_VAP(vap);
592	struct ieee80211com *ic = vap->iv_ic;
593	struct zyd_softc *sc = ic->ic_ifp->if_softc;
594	int error;
595
596	DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__,
597	    ieee80211_state_name[vap->iv_state],
598	    ieee80211_state_name[nstate]);
599
600	IEEE80211_UNLOCK(ic);
601	ZYD_LOCK(sc);
602	switch (nstate) {
603	case IEEE80211_S_AUTH:
604		zyd_set_chan(sc, ic->ic_curchan);
605		break;
606	case IEEE80211_S_RUN:
607		if (vap->iv_opmode == IEEE80211_M_MONITOR)
608			break;
609
610		/* turn link LED on */
611		error = zyd_set_led(sc, ZYD_LED1, 1);
612		if (error != 0)
613			break;
614
615		/* make data LED blink upon Tx */
616		zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1);
617
618		IEEE80211_ADDR_COPY(sc->sc_bssid, vap->iv_bss->ni_bssid);
619		zyd_set_bssid(sc, sc->sc_bssid);
620		break;
621	default:
622		break;
623	}
624fail:
625	ZYD_UNLOCK(sc);
626	IEEE80211_LOCK(ic);
627	return (zvp->newstate(vap, nstate, arg));
628}
629
630/*
631 * Callback handler for interrupt transfer
632 */
633static void
634zyd_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
635{
636	struct zyd_softc *sc = usbd_xfer_softc(xfer);
637	struct ifnet *ifp = sc->sc_ifp;
638	struct ieee80211com *ic = ifp->if_l2com;
639	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
640	struct ieee80211_node *ni;
641	struct zyd_cmd *cmd = &sc->sc_ibuf;
642	struct usb_page_cache *pc;
643	int datalen;
644	int actlen;
645
646	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
647
648	switch (USB_GET_STATE(xfer)) {
649	case USB_ST_TRANSFERRED:
650		pc = usbd_xfer_get_frame(xfer, 0);
651		usbd_copy_out(pc, 0, cmd, sizeof(*cmd));
652
653		switch (le16toh(cmd->code)) {
654		case ZYD_NOTIF_RETRYSTATUS:
655		{
656			struct zyd_notif_retry *retry =
657			    (struct zyd_notif_retry *)cmd->data;
658
659			DPRINTF(sc, ZYD_DEBUG_TX_PROC,
660			    "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
661			    le16toh(retry->rate), ether_sprintf(retry->macaddr),
662			    le16toh(retry->count)&0xff, le16toh(retry->count));
663
664			/*
665			 * Find the node to which the packet was sent and
666			 * update its retry statistics.  In BSS mode, this node
667			 * is the AP we're associated to so no lookup is
668			 * actually needed.
669			 */
670			ni = ieee80211_find_txnode(vap, retry->macaddr);
671			if (ni != NULL) {
672				int retrycnt =
673				    (int)(le16toh(retry->count) & 0xff);
674
675				ieee80211_ratectl_tx_complete(vap, ni,
676				    IEEE80211_RATECTL_TX_FAILURE,
677				    &retrycnt, NULL);
678				ieee80211_free_node(ni);
679			}
680			if (le16toh(retry->count) & 0x100)
681				ifp->if_oerrors++;	/* too many retries */
682			break;
683		}
684		case ZYD_NOTIF_IORD:
685		{
686			struct zyd_rq *rqp;
687
688			if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
689				break;	/* HMAC interrupt */
690
691			datalen = actlen - sizeof(cmd->code);
692			datalen -= 2;	/* XXX: padding? */
693
694			STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
695				int i;
696				int count;
697
698				if (rqp->olen != datalen)
699					continue;
700				count = rqp->olen / sizeof(struct zyd_pair);
701				for (i = 0; i < count; i++) {
702					if (*(((const uint16_t *)rqp->idata) + i) !=
703					    (((struct zyd_pair *)cmd->data) + i)->reg)
704						break;
705				}
706				if (i != count)
707					continue;
708				/* copy answer into caller-supplied buffer */
709				memcpy(rqp->odata, cmd->data, rqp->olen);
710				DPRINTF(sc, ZYD_DEBUG_CMD,
711				    "command %p complete, data = %*D \n",
712				    rqp, rqp->olen, (char *)rqp->odata, ":");
713				wakeup(rqp);	/* wakeup caller */
714				break;
715			}
716			if (rqp == NULL) {
717				device_printf(sc->sc_dev,
718				    "unexpected IORD notification %*D\n",
719				    datalen, cmd->data, ":");
720			}
721			break;
722		}
723		default:
724			device_printf(sc->sc_dev, "unknown notification %x\n",
725			    le16toh(cmd->code));
726		}
727
728		/* FALLTHROUGH */
729	case USB_ST_SETUP:
730tr_setup:
731		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
732		usbd_transfer_submit(xfer);
733		break;
734
735	default:			/* Error */
736		DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
737		    usbd_errstr(error));
738
739		if (error != USB_ERR_CANCELLED) {
740			/* try to clear stall first */
741			usbd_xfer_set_stall(xfer);
742			goto tr_setup;
743		}
744		break;
745	}
746}
747
748static void
749zyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
750{
751	struct zyd_softc *sc = usbd_xfer_softc(xfer);
752	struct zyd_rq *rqp, *cmd;
753	struct usb_page_cache *pc;
754
755	switch (USB_GET_STATE(xfer)) {
756	case USB_ST_TRANSFERRED:
757		cmd = usbd_xfer_get_priv(xfer);
758		DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd);
759		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
760			/* Ensure the cached rq pointer is still valid */
761			if (rqp == cmd &&
762			    (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
763				wakeup(rqp);	/* wakeup caller */
764		}
765
766		/* FALLTHROUGH */
767	case USB_ST_SETUP:
768tr_setup:
769		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
770			if (rqp->flags & ZYD_CMD_FLAG_SENT)
771				continue;
772
773			pc = usbd_xfer_get_frame(xfer, 0);
774			usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen);
775
776			usbd_xfer_set_frame_len(xfer, 0, rqp->ilen);
777			usbd_xfer_set_priv(xfer, rqp);
778			rqp->flags |= ZYD_CMD_FLAG_SENT;
779			usbd_transfer_submit(xfer);
780			break;
781		}
782		break;
783
784	default:			/* Error */
785		DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
786		    usbd_errstr(error));
787
788		if (error != USB_ERR_CANCELLED) {
789			/* try to clear stall first */
790			usbd_xfer_set_stall(xfer);
791			goto tr_setup;
792		}
793		break;
794	}
795}
796
797static int
798zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
799    void *odata, int olen, int flags)
800{
801	struct zyd_cmd cmd;
802	struct zyd_rq rq;
803	int error;
804
805	if (ilen > (int)sizeof(cmd.data))
806		return (EINVAL);
807
808	cmd.code = htole16(code);
809	memcpy(cmd.data, idata, ilen);
810	DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
811	    &rq, ilen, idata, ":");
812
813	rq.cmd = &cmd;
814	rq.idata = idata;
815	rq.odata = odata;
816	rq.ilen = sizeof(uint16_t) + ilen;
817	rq.olen = olen;
818	rq.flags = flags;
819	STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
820	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
821	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
822
823	/* wait at most one second for command reply */
824	error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
825	if (error)
826		device_printf(sc->sc_dev, "command timeout\n");
827	STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
828	DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
829	    &rq, error);
830
831	return (error);
832}
833
834static int
835zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
836{
837	struct zyd_pair tmp;
838	int error;
839
840	reg = htole16(reg);
841	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
842	    ZYD_CMD_FLAG_READ);
843	if (error == 0)
844		*val = le16toh(tmp.val);
845	return (error);
846}
847
848static int
849zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
850{
851	struct zyd_pair tmp[2];
852	uint16_t regs[2];
853	int error;
854
855	regs[0] = htole16(ZYD_REG32_HI(reg));
856	regs[1] = htole16(ZYD_REG32_LO(reg));
857	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
858	    ZYD_CMD_FLAG_READ);
859	if (error == 0)
860		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
861	return (error);
862}
863
864static int
865zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
866{
867	struct zyd_pair pair;
868
869	pair.reg = htole16(reg);
870	pair.val = htole16(val);
871
872	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
873}
874
875static int
876zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
877{
878	struct zyd_pair pair[2];
879
880	pair[0].reg = htole16(ZYD_REG32_HI(reg));
881	pair[0].val = htole16(val >> 16);
882	pair[1].reg = htole16(ZYD_REG32_LO(reg));
883	pair[1].val = htole16(val & 0xffff);
884
885	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
886}
887
888static int
889zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
890{
891	struct zyd_rf *rf = &sc->sc_rf;
892	struct zyd_rfwrite_cmd req;
893	uint16_t cr203;
894	int error, i;
895
896	zyd_read16_m(sc, ZYD_CR203, &cr203);
897	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
898
899	req.code  = htole16(2);
900	req.width = htole16(rf->width);
901	for (i = 0; i < rf->width; i++) {
902		req.bit[i] = htole16(cr203);
903		if (val & (1 << (rf->width - 1 - i)))
904			req.bit[i] |= htole16(ZYD_RF_DATA);
905	}
906	error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
907fail:
908	return (error);
909}
910
911static int
912zyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
913{
914	int error;
915
916	zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
917	zyd_write16_m(sc, ZYD_CR243, (val >>  8) & 0xff);
918	zyd_write16_m(sc, ZYD_CR242, (val >>  0) & 0xff);
919fail:
920	return (error);
921}
922
923static int
924zyd_lock_phy(struct zyd_softc *sc)
925{
926	int error;
927	uint32_t tmp;
928
929	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
930	tmp &= ~ZYD_UNLOCK_PHY_REGS;
931	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
932fail:
933	return (error);
934}
935
936static int
937zyd_unlock_phy(struct zyd_softc *sc)
938{
939	int error;
940	uint32_t tmp;
941
942	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
943	tmp |= ZYD_UNLOCK_PHY_REGS;
944	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
945fail:
946	return (error);
947}
948
949/*
950 * RFMD RF methods.
951 */
952static int
953zyd_rfmd_init(struct zyd_rf *rf)
954{
955#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
956	struct zyd_softc *sc = rf->rf_sc;
957	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
958	static const uint32_t rfini[] = ZYD_RFMD_RF;
959	int i, error;
960
961	/* init RF-dependent PHY registers */
962	for (i = 0; i < N(phyini); i++) {
963		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
964	}
965
966	/* init RFMD radio */
967	for (i = 0; i < N(rfini); i++) {
968		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
969			return (error);
970	}
971fail:
972	return (error);
973#undef N
974}
975
976static int
977zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
978{
979	int error;
980	struct zyd_softc *sc = rf->rf_sc;
981
982	zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
983	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
984fail:
985	return (error);
986}
987
988static int
989zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
990{
991	int error;
992	struct zyd_softc *sc = rf->rf_sc;
993	static const struct {
994		uint32_t	r1, r2;
995	} rfprog[] = ZYD_RFMD_CHANTABLE;
996
997	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
998	if (error != 0)
999		goto fail;
1000	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1001	if (error != 0)
1002		goto fail;
1003
1004fail:
1005	return (error);
1006}
1007
1008/*
1009 * AL2230 RF methods.
1010 */
1011static int
1012zyd_al2230_init(struct zyd_rf *rf)
1013{
1014#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1015	struct zyd_softc *sc = rf->rf_sc;
1016	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
1017	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1018	static const struct zyd_phy_pair phypll[] = {
1019		{ ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
1020		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
1021	};
1022	static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
1023	static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
1024	static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
1025	int i, error;
1026
1027	/* init RF-dependent PHY registers */
1028	for (i = 0; i < N(phyini); i++)
1029		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1030
1031	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1032		for (i = 0; i < N(phy2230s); i++)
1033			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1034	}
1035
1036	/* init AL2230 radio */
1037	for (i = 0; i < N(rfini1); i++) {
1038		error = zyd_rfwrite(sc, rfini1[i]);
1039		if (error != 0)
1040			goto fail;
1041	}
1042
1043	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1044		error = zyd_rfwrite(sc, 0x000824);
1045	else
1046		error = zyd_rfwrite(sc, 0x0005a4);
1047	if (error != 0)
1048		goto fail;
1049
1050	for (i = 0; i < N(rfini2); i++) {
1051		error = zyd_rfwrite(sc, rfini2[i]);
1052		if (error != 0)
1053			goto fail;
1054	}
1055
1056	for (i = 0; i < N(phypll); i++)
1057		zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
1058
1059	for (i = 0; i < N(rfini3); i++) {
1060		error = zyd_rfwrite(sc, rfini3[i]);
1061		if (error != 0)
1062			goto fail;
1063	}
1064fail:
1065	return (error);
1066#undef N
1067}
1068
1069static int
1070zyd_al2230_fini(struct zyd_rf *rf)
1071{
1072#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1073	int error, i;
1074	struct zyd_softc *sc = rf->rf_sc;
1075	static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
1076
1077	for (i = 0; i < N(phy); i++)
1078		zyd_write16_m(sc, phy[i].reg, phy[i].val);
1079
1080	if (sc->sc_newphy != 0)
1081		zyd_write16_m(sc, ZYD_CR9, 0xe1);
1082
1083	zyd_write16_m(sc, ZYD_CR203, 0x6);
1084fail:
1085	return (error);
1086#undef N
1087}
1088
1089static int
1090zyd_al2230_init_b(struct zyd_rf *rf)
1091{
1092#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1093	struct zyd_softc *sc = rf->rf_sc;
1094	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1095	static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
1096	static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
1097	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1098	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1099	static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
1100	static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
1101	static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
1102	static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
1103	int i, error;
1104
1105	for (i = 0; i < N(phy1); i++)
1106		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1107
1108	/* init RF-dependent PHY registers */
1109	for (i = 0; i < N(phyini); i++)
1110		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1111
1112	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1113		for (i = 0; i < N(phy2230s); i++)
1114			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1115	}
1116
1117	for (i = 0; i < 3; i++) {
1118		error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
1119		if (error != 0)
1120			return (error);
1121	}
1122
1123	for (i = 0; i < N(rfini_part1); i++) {
1124		error = zyd_rfwrite_cr(sc, rfini_part1[i]);
1125		if (error != 0)
1126			return (error);
1127	}
1128
1129	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1130		error = zyd_rfwrite(sc, 0x241000);
1131	else
1132		error = zyd_rfwrite(sc, 0x25a000);
1133	if (error != 0)
1134		goto fail;
1135
1136	for (i = 0; i < N(rfini_part2); i++) {
1137		error = zyd_rfwrite_cr(sc, rfini_part2[i]);
1138		if (error != 0)
1139			return (error);
1140	}
1141
1142	for (i = 0; i < N(phy2); i++)
1143		zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
1144
1145	for (i = 0; i < N(rfini_part3); i++) {
1146		error = zyd_rfwrite_cr(sc, rfini_part3[i]);
1147		if (error != 0)
1148			return (error);
1149	}
1150
1151	for (i = 0; i < N(phy3); i++)
1152		zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
1153
1154	error = zyd_al2230_fini(rf);
1155fail:
1156	return (error);
1157#undef N
1158}
1159
1160static int
1161zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1162{
1163	struct zyd_softc *sc = rf->rf_sc;
1164	int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
1165
1166	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1167	zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
1168fail:
1169	return (error);
1170}
1171
1172static int
1173zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1174{
1175#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1176	int error, i;
1177	struct zyd_softc *sc = rf->rf_sc;
1178	static const struct zyd_phy_pair phy1[] = {
1179		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
1180	};
1181	static const struct {
1182		uint32_t	r1, r2, r3;
1183	} rfprog[] = ZYD_AL2230_CHANTABLE;
1184
1185	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1186	if (error != 0)
1187		goto fail;
1188	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1189	if (error != 0)
1190		goto fail;
1191	error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
1192	if (error != 0)
1193		goto fail;
1194
1195	for (i = 0; i < N(phy1); i++)
1196		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1197fail:
1198	return (error);
1199#undef N
1200}
1201
1202static int
1203zyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
1204{
1205#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1206	int error, i;
1207	struct zyd_softc *sc = rf->rf_sc;
1208	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1209	static const struct {
1210		uint32_t	r1, r2, r3;
1211	} rfprog[] = ZYD_AL2230_CHANTABLE_B;
1212
1213	for (i = 0; i < N(phy1); i++)
1214		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1215
1216	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
1217	if (error != 0)
1218		goto fail;
1219	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
1220	if (error != 0)
1221		goto fail;
1222	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
1223	if (error != 0)
1224		goto fail;
1225	error = zyd_al2230_fini(rf);
1226fail:
1227	return (error);
1228#undef N
1229}
1230
1231#define	ZYD_AL2230_PHY_BANDEDGE6					\
1232{									\
1233	{ ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 },	\
1234	{ ZYD_CR47,  0x1e }						\
1235}
1236
1237static int
1238zyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
1239{
1240#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1241	int error = 0, i;
1242	struct zyd_softc *sc = rf->rf_sc;
1243	struct ifnet *ifp = sc->sc_ifp;
1244	struct ieee80211com *ic = ifp->if_l2com;
1245	struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
1246	int chan = ieee80211_chan2ieee(ic, c);
1247
1248	if (chan == 1 || chan == 11)
1249		r[0].val = 0x12;
1250
1251	for (i = 0; i < N(r); i++)
1252		zyd_write16_m(sc, r[i].reg, r[i].val);
1253fail:
1254	return (error);
1255#undef N
1256}
1257
1258/*
1259 * AL7230B RF methods.
1260 */
1261static int
1262zyd_al7230B_init(struct zyd_rf *rf)
1263{
1264#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1265	struct zyd_softc *sc = rf->rf_sc;
1266	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1267	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1268	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1269	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1270	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1271	int i, error;
1272
1273	/* for AL7230B, PHY and RF need to be initialized in "phases" */
1274
1275	/* init RF-dependent PHY registers, part one */
1276	for (i = 0; i < N(phyini_1); i++)
1277		zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
1278
1279	/* init AL7230B radio, part one */
1280	for (i = 0; i < N(rfini_1); i++) {
1281		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1282			return (error);
1283	}
1284	/* init RF-dependent PHY registers, part two */
1285	for (i = 0; i < N(phyini_2); i++)
1286		zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
1287
1288	/* init AL7230B radio, part two */
1289	for (i = 0; i < N(rfini_2); i++) {
1290		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1291			return (error);
1292	}
1293	/* init RF-dependent PHY registers, part three */
1294	for (i = 0; i < N(phyini_3); i++)
1295		zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
1296fail:
1297	return (error);
1298#undef N
1299}
1300
1301static int
1302zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1303{
1304	int error;
1305	struct zyd_softc *sc = rf->rf_sc;
1306
1307	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1308	zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1309fail:
1310	return (error);
1311}
1312
1313static int
1314zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1315{
1316#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1317	struct zyd_softc *sc = rf->rf_sc;
1318	static const struct {
1319		uint32_t	r1, r2;
1320	} rfprog[] = ZYD_AL7230B_CHANTABLE;
1321	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1322	int i, error;
1323
1324	zyd_write16_m(sc, ZYD_CR240, 0x57);
1325	zyd_write16_m(sc, ZYD_CR251, 0x2f);
1326
1327	for (i = 0; i < N(rfsc); i++) {
1328		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1329			return (error);
1330	}
1331
1332	zyd_write16_m(sc, ZYD_CR128, 0x14);
1333	zyd_write16_m(sc, ZYD_CR129, 0x12);
1334	zyd_write16_m(sc, ZYD_CR130, 0x10);
1335	zyd_write16_m(sc, ZYD_CR38,  0x38);
1336	zyd_write16_m(sc, ZYD_CR136, 0xdf);
1337
1338	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1339	if (error != 0)
1340		goto fail;
1341	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1342	if (error != 0)
1343		goto fail;
1344	error = zyd_rfwrite(sc, 0x3c9000);
1345	if (error != 0)
1346		goto fail;
1347
1348	zyd_write16_m(sc, ZYD_CR251, 0x3f);
1349	zyd_write16_m(sc, ZYD_CR203, 0x06);
1350	zyd_write16_m(sc, ZYD_CR240, 0x08);
1351fail:
1352	return (error);
1353#undef N
1354}
1355
1356/*
1357 * AL2210 RF methods.
1358 */
1359static int
1360zyd_al2210_init(struct zyd_rf *rf)
1361{
1362#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1363	struct zyd_softc *sc = rf->rf_sc;
1364	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1365	static const uint32_t rfini[] = ZYD_AL2210_RF;
1366	uint32_t tmp;
1367	int i, error;
1368
1369	zyd_write32_m(sc, ZYD_CR18, 2);
1370
1371	/* init RF-dependent PHY registers */
1372	for (i = 0; i < N(phyini); i++)
1373		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1374
1375	/* init AL2210 radio */
1376	for (i = 0; i < N(rfini); i++) {
1377		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1378			return (error);
1379	}
1380	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1381	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1382	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1383	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1384	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1385	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1386	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1387	zyd_write32_m(sc, ZYD_CR18, 3);
1388fail:
1389	return (error);
1390#undef N
1391}
1392
1393static int
1394zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1395{
1396	/* vendor driver does nothing for this RF chip */
1397
1398	return (0);
1399}
1400
1401static int
1402zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1403{
1404	int error;
1405	struct zyd_softc *sc = rf->rf_sc;
1406	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1407	uint32_t tmp;
1408
1409	zyd_write32_m(sc, ZYD_CR18, 2);
1410	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1411	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1412	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1413	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1414	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1415	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1416	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1417
1418	/* actually set the channel */
1419	error = zyd_rfwrite(sc, rfprog[chan - 1]);
1420	if (error != 0)
1421		goto fail;
1422
1423	zyd_write32_m(sc, ZYD_CR18, 3);
1424fail:
1425	return (error);
1426}
1427
1428/*
1429 * GCT RF methods.
1430 */
1431static int
1432zyd_gct_init(struct zyd_rf *rf)
1433{
1434#define	ZYD_GCT_INTR_REG	0x85c1
1435#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1436	struct zyd_softc *sc = rf->rf_sc;
1437	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1438	static const uint32_t rfini[] = ZYD_GCT_RF;
1439	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1440	int i, idx = -1, error;
1441	uint16_t data;
1442
1443	/* init RF-dependent PHY registers */
1444	for (i = 0; i < N(phyini); i++)
1445		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1446
1447	/* init cgt radio */
1448	for (i = 0; i < N(rfini); i++) {
1449		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1450			return (error);
1451	}
1452
1453	error = zyd_gct_mode(rf);
1454	if (error != 0)
1455		return (error);
1456
1457	for (i = 0; i < (int)(N(vco) - 1); i++) {
1458		error = zyd_gct_set_channel_synth(rf, 1, 0);
1459		if (error != 0)
1460			goto fail;
1461		error = zyd_gct_write(rf, vco[i][0]);
1462		if (error != 0)
1463			goto fail;
1464		zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf);
1465		zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data);
1466		if ((data & 0xf) == 0) {
1467			idx = i;
1468			break;
1469		}
1470	}
1471	if (idx == -1) {
1472		error = zyd_gct_set_channel_synth(rf, 1, 1);
1473		if (error != 0)
1474			goto fail;
1475		error = zyd_gct_write(rf, 0x6662);
1476		if (error != 0)
1477			goto fail;
1478	}
1479
1480	rf->idx = idx;
1481	zyd_write16_m(sc, ZYD_CR203, 0x6);
1482fail:
1483	return (error);
1484#undef N
1485#undef ZYD_GCT_INTR_REG
1486}
1487
1488static int
1489zyd_gct_mode(struct zyd_rf *rf)
1490{
1491#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1492	struct zyd_softc *sc = rf->rf_sc;
1493	static const uint32_t mode[] = {
1494		0x25f98, 0x25f9a, 0x25f94, 0x27fd4
1495	};
1496	int i, error;
1497
1498	for (i = 0; i < N(mode); i++) {
1499		if ((error = zyd_rfwrite(sc, mode[i])) != 0)
1500			break;
1501	}
1502	return (error);
1503#undef N
1504}
1505
1506static int
1507zyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal)
1508{
1509	int error, idx = chan - 1;
1510	struct zyd_softc *sc = rf->rf_sc;
1511	static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL;
1512	static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD;
1513	static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV;
1514
1515	error = zyd_rfwrite(sc,
1516	    (acal == 1) ? acal_synth[idx] : std_synth[idx]);
1517	if (error != 0)
1518		return (error);
1519	return zyd_rfwrite(sc, div_synth[idx]);
1520}
1521
1522static int
1523zyd_gct_write(struct zyd_rf *rf, uint16_t value)
1524{
1525	struct zyd_softc *sc = rf->rf_sc;
1526
1527	return zyd_rfwrite(sc, 0x300000 | 0x40000 | value);
1528}
1529
1530static int
1531zyd_gct_switch_radio(struct zyd_rf *rf, int on)
1532{
1533	int error;
1534	struct zyd_softc *sc = rf->rf_sc;
1535
1536	error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90);
1537	if (error != 0)
1538		return (error);
1539
1540	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
1541	zyd_write16_m(sc, ZYD_CR251,
1542	    on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f);
1543fail:
1544	return (error);
1545}
1546
1547static int
1548zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1549{
1550#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1551	int error, i;
1552	struct zyd_softc *sc = rf->rf_sc;
1553	static const struct zyd_phy_pair cmd[] = {
1554		{ ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 },
1555		{ ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 },
1556	};
1557	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1558
1559	error = zyd_gct_set_channel_synth(rf, chan, 0);
1560	if (error != 0)
1561		goto fail;
1562	error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 :
1563	    vco[rf->idx][((chan - 1) / 2)]);
1564	if (error != 0)
1565		goto fail;
1566	error = zyd_gct_mode(rf);
1567	if (error != 0)
1568		return (error);
1569	for (i = 0; i < N(cmd); i++)
1570		zyd_write16_m(sc, cmd[i].reg, cmd[i].val);
1571	error = zyd_gct_txgain(rf, chan);
1572	if (error != 0)
1573		return (error);
1574	zyd_write16_m(sc, ZYD_CR203, 0x6);
1575fail:
1576	return (error);
1577#undef N
1578}
1579
1580static int
1581zyd_gct_txgain(struct zyd_rf *rf, uint8_t chan)
1582{
1583#define N(a)	(sizeof(a) / sizeof((a)[0]))
1584	struct zyd_softc *sc = rf->rf_sc;
1585	static uint32_t txgain[] = ZYD_GCT_TXGAIN;
1586	uint8_t idx = sc->sc_pwrint[chan - 1];
1587
1588	if (idx >= N(txgain)) {
1589		device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n",
1590		    chan, idx);
1591		return 0;
1592	}
1593
1594	return zyd_rfwrite(sc, 0x700000 | txgain[idx]);
1595#undef N
1596}
1597
1598/*
1599 * Maxim2 RF methods.
1600 */
1601static int
1602zyd_maxim2_init(struct zyd_rf *rf)
1603{
1604#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1605	struct zyd_softc *sc = rf->rf_sc;
1606	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1607	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1608	uint16_t tmp;
1609	int i, error;
1610
1611	/* init RF-dependent PHY registers */
1612	for (i = 0; i < N(phyini); i++)
1613		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1614
1615	zyd_read16_m(sc, ZYD_CR203, &tmp);
1616	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1617
1618	/* init maxim2 radio */
1619	for (i = 0; i < N(rfini); i++) {
1620		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1621			return (error);
1622	}
1623	zyd_read16_m(sc, ZYD_CR203, &tmp);
1624	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1625fail:
1626	return (error);
1627#undef N
1628}
1629
1630static int
1631zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1632{
1633
1634	/* vendor driver does nothing for this RF chip */
1635	return (0);
1636}
1637
1638static int
1639zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1640{
1641#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1642	struct zyd_softc *sc = rf->rf_sc;
1643	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1644	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1645	static const struct {
1646		uint32_t	r1, r2;
1647	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
1648	uint16_t tmp;
1649	int i, error;
1650
1651	/*
1652	 * Do the same as we do when initializing it, except for the channel
1653	 * values coming from the two channel tables.
1654	 */
1655
1656	/* init RF-dependent PHY registers */
1657	for (i = 0; i < N(phyini); i++)
1658		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1659
1660	zyd_read16_m(sc, ZYD_CR203, &tmp);
1661	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1662
1663	/* first two values taken from the chantables */
1664	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1665	if (error != 0)
1666		goto fail;
1667	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1668	if (error != 0)
1669		goto fail;
1670
1671	/* init maxim2 radio - skipping the two first values */
1672	for (i = 2; i < N(rfini); i++) {
1673		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1674			return (error);
1675	}
1676	zyd_read16_m(sc, ZYD_CR203, &tmp);
1677	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1678fail:
1679	return (error);
1680#undef N
1681}
1682
1683static int
1684zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1685{
1686	struct zyd_rf *rf = &sc->sc_rf;
1687
1688	rf->rf_sc = sc;
1689	rf->update_pwr = 1;
1690
1691	switch (type) {
1692	case ZYD_RF_RFMD:
1693		rf->init         = zyd_rfmd_init;
1694		rf->switch_radio = zyd_rfmd_switch_radio;
1695		rf->set_channel  = zyd_rfmd_set_channel;
1696		rf->width        = 24;	/* 24-bit RF values */
1697		break;
1698	case ZYD_RF_AL2230:
1699	case ZYD_RF_AL2230S:
1700		if (sc->sc_macrev == ZYD_ZD1211B) {
1701			rf->init = zyd_al2230_init_b;
1702			rf->set_channel = zyd_al2230_set_channel_b;
1703		} else {
1704			rf->init = zyd_al2230_init;
1705			rf->set_channel = zyd_al2230_set_channel;
1706		}
1707		rf->switch_radio = zyd_al2230_switch_radio;
1708		rf->bandedge6	 = zyd_al2230_bandedge6;
1709		rf->width        = 24;	/* 24-bit RF values */
1710		break;
1711	case ZYD_RF_AL7230B:
1712		rf->init         = zyd_al7230B_init;
1713		rf->switch_radio = zyd_al7230B_switch_radio;
1714		rf->set_channel  = zyd_al7230B_set_channel;
1715		rf->width        = 24;	/* 24-bit RF values */
1716		break;
1717	case ZYD_RF_AL2210:
1718		rf->init         = zyd_al2210_init;
1719		rf->switch_radio = zyd_al2210_switch_radio;
1720		rf->set_channel  = zyd_al2210_set_channel;
1721		rf->width        = 24;	/* 24-bit RF values */
1722		break;
1723	case ZYD_RF_MAXIM_NEW:
1724	case ZYD_RF_GCT:
1725		rf->init         = zyd_gct_init;
1726		rf->switch_radio = zyd_gct_switch_radio;
1727		rf->set_channel  = zyd_gct_set_channel;
1728		rf->width        = 24;	/* 24-bit RF values */
1729		rf->update_pwr   = 0;
1730		break;
1731	case ZYD_RF_MAXIM_NEW2:
1732		rf->init         = zyd_maxim2_init;
1733		rf->switch_radio = zyd_maxim2_switch_radio;
1734		rf->set_channel  = zyd_maxim2_set_channel;
1735		rf->width        = 18;	/* 18-bit RF values */
1736		break;
1737	default:
1738		device_printf(sc->sc_dev,
1739		    "sorry, radio \"%s\" is not supported yet\n",
1740		    zyd_rf_name(type));
1741		return (EINVAL);
1742	}
1743	return (0);
1744}
1745
1746static const char *
1747zyd_rf_name(uint8_t type)
1748{
1749	static const char * const zyd_rfs[] = {
1750		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
1751		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
1752		"AL2230S",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
1753		"PHILIPS"
1754	};
1755
1756	return zyd_rfs[(type > 15) ? 0 : type];
1757}
1758
1759static int
1760zyd_hw_init(struct zyd_softc *sc)
1761{
1762	int error;
1763	const struct zyd_phy_pair *phyp;
1764	struct zyd_rf *rf = &sc->sc_rf;
1765	uint16_t val;
1766
1767	/* specify that the plug and play is finished */
1768	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1769	zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
1770	DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
1771	    sc->sc_fwbase);
1772
1773	/* retrieve firmware revision number */
1774	zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
1775	zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
1776	zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1777	/* set mandatory rates - XXX assumes 802.11b/g */
1778	zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
1779
1780	/* disable interrupts */
1781	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
1782
1783	if ((error = zyd_read_pod(sc)) != 0) {
1784		device_printf(sc->sc_dev, "could not read EEPROM\n");
1785		goto fail;
1786	}
1787
1788	/* PHY init (resetting) */
1789	error = zyd_lock_phy(sc);
1790	if (error != 0)
1791		goto fail;
1792	phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1793	for (; phyp->reg != 0; phyp++)
1794		zyd_write16_m(sc, phyp->reg, phyp->val);
1795	if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
1796		zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
1797		zyd_write32_m(sc, ZYD_CR157, val >> 8);
1798	}
1799	error = zyd_unlock_phy(sc);
1800	if (error != 0)
1801		goto fail;
1802
1803	/* HMAC init */
1804	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1805	zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1806	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
1807	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
1808	zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
1809	zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
1810	zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
1811	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1812	zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1813	zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1814	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1815	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1816	zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1817	zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1818	zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
1819	zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1820	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1821	zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1822	zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
1823	zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
1824
1825	if (sc->sc_macrev == ZYD_ZD1211) {
1826		zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
1827		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1828	} else {
1829		zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1830		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1831		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1832		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1833		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1834		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1835		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1836		zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
1837		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
1838	}
1839
1840	/* init beacon interval to 100ms */
1841	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1842		goto fail;
1843
1844	if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
1845		device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
1846		    sc->sc_rfrev);
1847		goto fail;
1848	}
1849
1850	/* RF chip init */
1851	error = zyd_lock_phy(sc);
1852	if (error != 0)
1853		goto fail;
1854	error = (*rf->init)(rf);
1855	if (error != 0) {
1856		device_printf(sc->sc_dev,
1857		    "radio initialization failed, error %d\n", error);
1858		goto fail;
1859	}
1860	error = zyd_unlock_phy(sc);
1861	if (error != 0)
1862		goto fail;
1863
1864	if ((error = zyd_read_eeprom(sc)) != 0) {
1865		device_printf(sc->sc_dev, "could not read EEPROM\n");
1866		goto fail;
1867	}
1868
1869fail:	return (error);
1870}
1871
1872static int
1873zyd_read_pod(struct zyd_softc *sc)
1874{
1875	int error;
1876	uint32_t tmp;
1877
1878	zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
1879	sc->sc_rfrev     = tmp & 0x0f;
1880	sc->sc_ledtype   = (tmp >>  4) & 0x01;
1881	sc->sc_al2230s   = (tmp >>  7) & 0x01;
1882	sc->sc_cckgain   = (tmp >>  8) & 0x01;
1883	sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
1884	sc->sc_parev     = (tmp >> 16) & 0x0f;
1885	sc->sc_bandedge6 = (tmp >> 21) & 0x01;
1886	sc->sc_newphy    = (tmp >> 31) & 0x01;
1887	sc->sc_txled     = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
1888fail:
1889	return (error);
1890}
1891
1892static int
1893zyd_read_eeprom(struct zyd_softc *sc)
1894{
1895	uint16_t val;
1896	int error, i;
1897
1898	/* read Tx power calibration tables */
1899	for (i = 0; i < 7; i++) {
1900		zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1901		sc->sc_pwrcal[i * 2] = val >> 8;
1902		sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
1903		zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
1904		sc->sc_pwrint[i * 2] = val >> 8;
1905		sc->sc_pwrint[i * 2 + 1] = val & 0xff;
1906		zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
1907		sc->sc_ofdm36_cal[i * 2] = val >> 8;
1908		sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
1909		zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
1910		sc->sc_ofdm48_cal[i * 2] = val >> 8;
1911		sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
1912		zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
1913		sc->sc_ofdm54_cal[i * 2] = val >> 8;
1914		sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
1915	}
1916fail:
1917	return (error);
1918}
1919
1920static int
1921zyd_get_macaddr(struct zyd_softc *sc)
1922{
1923	struct usb_device_request req;
1924	usb_error_t error;
1925
1926	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1927	req.bRequest = ZYD_READFWDATAREQ;
1928	USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
1929	USETW(req.wIndex, 0);
1930	USETW(req.wLength, IEEE80211_ADDR_LEN);
1931
1932	error = zyd_do_request(sc, &req, sc->sc_bssid);
1933	if (error != 0) {
1934		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1935		    usbd_errstr(error));
1936	}
1937
1938	return (error);
1939}
1940
1941static int
1942zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1943{
1944	int error;
1945	uint32_t tmp;
1946
1947	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1948	zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
1949	tmp = addr[5] << 8 | addr[4];
1950	zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
1951fail:
1952	return (error);
1953}
1954
1955static int
1956zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1957{
1958	int error;
1959	uint32_t tmp;
1960
1961	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1962	zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
1963	tmp = addr[5] << 8 | addr[4];
1964	zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
1965fail:
1966	return (error);
1967}
1968
1969static int
1970zyd_switch_radio(struct zyd_softc *sc, int on)
1971{
1972	struct zyd_rf *rf = &sc->sc_rf;
1973	int error;
1974
1975	error = zyd_lock_phy(sc);
1976	if (error != 0)
1977		goto fail;
1978	error = (*rf->switch_radio)(rf, on);
1979	if (error != 0)
1980		goto fail;
1981	error = zyd_unlock_phy(sc);
1982fail:
1983	return (error);
1984}
1985
1986static int
1987zyd_set_led(struct zyd_softc *sc, int which, int on)
1988{
1989	int error;
1990	uint32_t tmp;
1991
1992	zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1993	tmp &= ~which;
1994	if (on)
1995		tmp |= which;
1996	zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1997fail:
1998	return (error);
1999}
2000
2001static void
2002zyd_set_multi(struct zyd_softc *sc)
2003{
2004	int error;
2005	struct ifnet *ifp = sc->sc_ifp;
2006	struct ieee80211com *ic = ifp->if_l2com;
2007	struct ifmultiaddr *ifma;
2008	uint32_t low, high;
2009	uint8_t v;
2010
2011	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2012		return;
2013
2014	low = 0x00000000;
2015	high = 0x80000000;
2016
2017	if (ic->ic_opmode == IEEE80211_M_MONITOR ||
2018	    (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) {
2019		low = 0xffffffff;
2020		high = 0xffffffff;
2021	} else {
2022		if_maddr_rlock(ifp);
2023		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2024			if (ifma->ifma_addr->sa_family != AF_LINK)
2025				continue;
2026			v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
2027			    ifma->ifma_addr))[5] >> 2;
2028			if (v < 32)
2029				low |= 1 << v;
2030			else
2031				high |= 1 << (v - 32);
2032		}
2033		if_maddr_runlock(ifp);
2034	}
2035
2036	/* reprogram multicast global hash table */
2037	zyd_write32_m(sc, ZYD_MAC_GHTBL, low);
2038	zyd_write32_m(sc, ZYD_MAC_GHTBH, high);
2039fail:
2040	if (error != 0)
2041		device_printf(sc->sc_dev,
2042		    "could not set multicast hash table\n");
2043}
2044
2045static void
2046zyd_update_mcast(struct ifnet *ifp)
2047{
2048	struct zyd_softc *sc = ifp->if_softc;
2049
2050	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2051		return;
2052
2053	ZYD_LOCK(sc);
2054	zyd_set_multi(sc);
2055	ZYD_UNLOCK(sc);
2056}
2057
2058static int
2059zyd_set_rxfilter(struct zyd_softc *sc)
2060{
2061	struct ifnet *ifp = sc->sc_ifp;
2062	struct ieee80211com *ic = ifp->if_l2com;
2063	uint32_t rxfilter;
2064
2065	switch (ic->ic_opmode) {
2066	case IEEE80211_M_STA:
2067		rxfilter = ZYD_FILTER_BSS;
2068		break;
2069	case IEEE80211_M_IBSS:
2070	case IEEE80211_M_HOSTAP:
2071		rxfilter = ZYD_FILTER_HOSTAP;
2072		break;
2073	case IEEE80211_M_MONITOR:
2074		rxfilter = ZYD_FILTER_MONITOR;
2075		break;
2076	default:
2077		/* should not get there */
2078		return (EINVAL);
2079	}
2080	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
2081}
2082
2083static void
2084zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
2085{
2086	int error;
2087	struct ifnet *ifp = sc->sc_ifp;
2088	struct ieee80211com *ic = ifp->if_l2com;
2089	struct zyd_rf *rf = &sc->sc_rf;
2090	uint32_t tmp;
2091	int chan;
2092
2093	chan = ieee80211_chan2ieee(ic, c);
2094	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
2095		/* XXX should NEVER happen */
2096		device_printf(sc->sc_dev,
2097		    "%s: invalid channel %x\n", __func__, chan);
2098		return;
2099	}
2100
2101	error = zyd_lock_phy(sc);
2102	if (error != 0)
2103		goto fail;
2104
2105	error = (*rf->set_channel)(rf, chan);
2106	if (error != 0)
2107		goto fail;
2108
2109	if (rf->update_pwr) {
2110		/* update Tx power */
2111		zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
2112
2113		if (sc->sc_macrev == ZYD_ZD1211B) {
2114			zyd_write16_m(sc, ZYD_CR67,
2115			    sc->sc_ofdm36_cal[chan - 1]);
2116			zyd_write16_m(sc, ZYD_CR66,
2117			    sc->sc_ofdm48_cal[chan - 1]);
2118			zyd_write16_m(sc, ZYD_CR65,
2119			    sc->sc_ofdm54_cal[chan - 1]);
2120			zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
2121			zyd_write16_m(sc, ZYD_CR69, 0x28);
2122			zyd_write16_m(sc, ZYD_CR69, 0x2a);
2123		}
2124	}
2125	if (sc->sc_cckgain) {
2126		/* set CCK baseband gain from EEPROM */
2127		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
2128			zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
2129	}
2130	if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
2131		error = (*rf->bandedge6)(rf, c);
2132		if (error != 0)
2133			goto fail;
2134	}
2135	zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
2136
2137	error = zyd_unlock_phy(sc);
2138	if (error != 0)
2139		goto fail;
2140
2141	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
2142	    htole16(c->ic_freq);
2143	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
2144	    htole16(c->ic_flags);
2145fail:
2146	return;
2147}
2148
2149static int
2150zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
2151{
2152	int error;
2153	uint32_t val;
2154
2155	zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
2156	sc->sc_atim_wnd = val;
2157	zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
2158	sc->sc_pre_tbtt = val;
2159	sc->sc_bcn_int = bintval;
2160
2161	if (sc->sc_bcn_int <= 5)
2162		sc->sc_bcn_int = 5;
2163	if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
2164		sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
2165	if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
2166		sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
2167
2168	zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
2169	zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
2170	zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
2171fail:
2172	return (error);
2173}
2174
2175static void
2176zyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
2177{
2178	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2179	struct ifnet *ifp = sc->sc_ifp;
2180	struct ieee80211com *ic = ifp->if_l2com;
2181	struct zyd_plcphdr plcp;
2182	struct zyd_rx_stat stat;
2183	struct usb_page_cache *pc;
2184	struct mbuf *m;
2185	int rlen, rssi;
2186
2187	if (len < ZYD_MIN_FRAGSZ) {
2188		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
2189		    device_get_nameunit(sc->sc_dev), len);
2190		ifp->if_ierrors++;
2191		return;
2192	}
2193	pc = usbd_xfer_get_frame(xfer, 0);
2194	usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2195	usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
2196
2197	if (stat.flags & ZYD_RX_ERROR) {
2198		DPRINTF(sc, ZYD_DEBUG_RECV,
2199		    "%s: RX status indicated error (%x)\n",
2200		    device_get_nameunit(sc->sc_dev), stat.flags);
2201		ifp->if_ierrors++;
2202		return;
2203	}
2204
2205	/* compute actual frame length */
2206	rlen = len - sizeof(struct zyd_plcphdr) -
2207	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
2208
2209	/* allocate a mbuf to store the frame */
2210	if (rlen > (int)MCLBYTES) {
2211		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
2212		    device_get_nameunit(sc->sc_dev), rlen);
2213		ifp->if_ierrors++;
2214		return;
2215	} else if (rlen > (int)MHLEN)
2216		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2217	else
2218		m = m_gethdr(M_NOWAIT, MT_DATA);
2219	if (m == NULL) {
2220		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
2221		    device_get_nameunit(sc->sc_dev));
2222		ifp->if_ierrors++;
2223		return;
2224	}
2225	m->m_pkthdr.rcvif = ifp;
2226	m->m_pkthdr.len = m->m_len = rlen;
2227	usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
2228
2229	if (ieee80211_radiotap_active(ic)) {
2230		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
2231
2232		tap->wr_flags = 0;
2233		if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
2234			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2235		/* XXX toss, no way to express errors */
2236		if (stat.flags & ZYD_RX_DECRYPTERR)
2237			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2238		tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
2239		    (stat.flags & ZYD_RX_OFDM) ?
2240			IEEE80211_T_OFDM : IEEE80211_T_CCK);
2241		tap->wr_antsignal = stat.rssi + -95;
2242		tap->wr_antnoise = -95;	/* XXX */
2243	}
2244	rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
2245
2246	sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
2247	sc->sc_rx_data[sc->sc_rx_count].m = m;
2248	sc->sc_rx_count++;
2249}
2250
2251static void
2252zyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
2253{
2254	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2255	struct ifnet *ifp = sc->sc_ifp;
2256	struct ieee80211com *ic = ifp->if_l2com;
2257	struct ieee80211_node *ni;
2258	struct zyd_rx_desc desc;
2259	struct mbuf *m;
2260	struct usb_page_cache *pc;
2261	uint32_t offset;
2262	uint8_t rssi;
2263	int8_t nf;
2264	int i;
2265	int actlen;
2266
2267	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2268
2269	sc->sc_rx_count = 0;
2270	switch (USB_GET_STATE(xfer)) {
2271	case USB_ST_TRANSFERRED:
2272		pc = usbd_xfer_get_frame(xfer, 0);
2273		usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
2274
2275		offset = 0;
2276		if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
2277			DPRINTF(sc, ZYD_DEBUG_RECV,
2278			    "%s: received multi-frame transfer\n", __func__);
2279
2280			for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2281				uint16_t len16 = UGETW(desc.len[i]);
2282
2283				if (len16 == 0 || len16 > actlen)
2284					break;
2285
2286				zyd_rx_data(xfer, offset, len16);
2287
2288				/* next frame is aligned on a 32-bit boundary */
2289				len16 = (len16 + 3) & ~3;
2290				offset += len16;
2291				if (len16 > actlen)
2292					break;
2293				actlen -= len16;
2294			}
2295		} else {
2296			DPRINTF(sc, ZYD_DEBUG_RECV,
2297			    "%s: received single-frame transfer\n", __func__);
2298
2299			zyd_rx_data(xfer, 0, actlen);
2300		}
2301		/* FALLTHROUGH */
2302	case USB_ST_SETUP:
2303tr_setup:
2304		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2305		usbd_transfer_submit(xfer);
2306
2307		/*
2308		 * At the end of a USB callback it is always safe to unlock
2309		 * the private mutex of a device! That is why we do the
2310		 * "ieee80211_input" here, and not some lines up!
2311		 */
2312		ZYD_UNLOCK(sc);
2313		for (i = 0; i < sc->sc_rx_count; i++) {
2314			rssi = sc->sc_rx_data[i].rssi;
2315			m = sc->sc_rx_data[i].m;
2316			sc->sc_rx_data[i].m = NULL;
2317
2318			nf = -95;	/* XXX */
2319
2320			ni = ieee80211_find_rxnode(ic,
2321			    mtod(m, struct ieee80211_frame_min *));
2322			if (ni != NULL) {
2323				(void)ieee80211_input(ni, m, rssi, nf);
2324				ieee80211_free_node(ni);
2325			} else
2326				(void)ieee80211_input_all(ic, m, rssi, nf);
2327		}
2328		if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
2329		    !IFQ_IS_EMPTY(&ifp->if_snd))
2330			zyd_start(ifp);
2331		ZYD_LOCK(sc);
2332		break;
2333
2334	default:			/* Error */
2335		DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
2336
2337		if (error != USB_ERR_CANCELLED) {
2338			/* try to clear stall first */
2339			usbd_xfer_set_stall(xfer);
2340			goto tr_setup;
2341		}
2342		break;
2343	}
2344}
2345
2346static uint8_t
2347zyd_plcp_signal(struct zyd_softc *sc, int rate)
2348{
2349	switch (rate) {
2350	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
2351	case 12:
2352		return (0xb);
2353	case 18:
2354		return (0xf);
2355	case 24:
2356		return (0xa);
2357	case 36:
2358		return (0xe);
2359	case 48:
2360		return (0x9);
2361	case 72:
2362		return (0xd);
2363	case 96:
2364		return (0x8);
2365	case 108:
2366		return (0xc);
2367	/* CCK rates (NB: not IEEE std, device-specific) */
2368	case 2:
2369		return (0x0);
2370	case 4:
2371		return (0x1);
2372	case 11:
2373		return (0x2);
2374	case 22:
2375		return (0x3);
2376	}
2377
2378	device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2379	return (0x0);
2380}
2381
2382static void
2383zyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
2384{
2385	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2386	struct ifnet *ifp = sc->sc_ifp;
2387	struct ieee80211vap *vap;
2388	struct zyd_tx_data *data;
2389	struct mbuf *m;
2390	struct usb_page_cache *pc;
2391	int actlen;
2392
2393	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2394
2395	switch (USB_GET_STATE(xfer)) {
2396	case USB_ST_TRANSFERRED:
2397		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2398		    actlen);
2399
2400		/* free resources */
2401		data = usbd_xfer_get_priv(xfer);
2402		zyd_tx_free(data, 0);
2403		usbd_xfer_set_priv(xfer, NULL);
2404
2405		ifp->if_opackets++;
2406		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2407
2408		/* FALLTHROUGH */
2409	case USB_ST_SETUP:
2410tr_setup:
2411		data = STAILQ_FIRST(&sc->tx_q);
2412		if (data) {
2413			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
2414			m = data->m;
2415
2416			if (m->m_pkthdr.len > (int)ZYD_MAX_TXBUFSZ) {
2417				DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
2418				    m->m_pkthdr.len);
2419				m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
2420			}
2421			pc = usbd_xfer_get_frame(xfer, 0);
2422			usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2423			usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
2424			    m->m_pkthdr.len);
2425
2426			vap = data->ni->ni_vap;
2427			if (ieee80211_radiotap_active_vap(vap)) {
2428				struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2429
2430				tap->wt_flags = 0;
2431				tap->wt_rate = data->rate;
2432
2433				ieee80211_radiotap_tx(vap, m);
2434			}
2435
2436			usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2437			usbd_xfer_set_priv(xfer, data);
2438			usbd_transfer_submit(xfer);
2439		}
2440		ZYD_UNLOCK(sc);
2441		zyd_start(ifp);
2442		ZYD_LOCK(sc);
2443		break;
2444
2445	default:			/* Error */
2446		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2447		    usbd_errstr(error));
2448
2449		ifp->if_oerrors++;
2450		data = usbd_xfer_get_priv(xfer);
2451		usbd_xfer_set_priv(xfer, NULL);
2452		if (data != NULL)
2453			zyd_tx_free(data, error);
2454
2455		if (error != USB_ERR_CANCELLED) {
2456			if (error == USB_ERR_TIMEOUT)
2457				device_printf(sc->sc_dev, "device timeout\n");
2458
2459			/*
2460			 * Try to clear stall first, also if other
2461			 * errors occur, hence clearing stall
2462			 * introduces a 50 ms delay:
2463			 */
2464			usbd_xfer_set_stall(xfer);
2465			goto tr_setup;
2466		}
2467		break;
2468	}
2469}
2470
2471static int
2472zyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2473{
2474	struct ieee80211vap *vap = ni->ni_vap;
2475	struct ieee80211com *ic = ni->ni_ic;
2476	struct zyd_tx_desc *desc;
2477	struct zyd_tx_data *data;
2478	struct ieee80211_frame *wh;
2479	const struct ieee80211_txparam *tp;
2480	struct ieee80211_key *k;
2481	int rate, totlen;
2482	static uint8_t ratediv[] = ZYD_TX_RATEDIV;
2483	uint8_t phy;
2484	uint16_t pktlen;
2485	uint32_t bits;
2486
2487	wh = mtod(m0, struct ieee80211_frame *);
2488	data = STAILQ_FIRST(&sc->tx_free);
2489	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
2490	sc->tx_nfree--;
2491
2492	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT ||
2493	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) {
2494		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2495		rate = tp->mgmtrate;
2496	} else {
2497		tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
2498		/* for data frames */
2499		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
2500			rate = tp->mcastrate;
2501		else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2502			rate = tp->ucastrate;
2503		else {
2504			(void) ieee80211_ratectl_rate(ni, NULL, 0);
2505			rate = ni->ni_txrate;
2506		}
2507	}
2508
2509	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2510		k = ieee80211_crypto_encap(ni, m0);
2511		if (k == NULL) {
2512			m_freem(m0);
2513			return (ENOBUFS);
2514		}
2515		/* packet header may have moved, reset our local pointer */
2516		wh = mtod(m0, struct ieee80211_frame *);
2517	}
2518
2519	data->ni = ni;
2520	data->m = m0;
2521	data->rate = rate;
2522
2523	/* fill Tx descriptor */
2524	desc = &data->desc;
2525	phy = zyd_plcp_signal(sc, rate);
2526	desc->phy = phy;
2527	if (ZYD_RATE_IS_OFDM(rate)) {
2528		desc->phy |= ZYD_TX_PHY_OFDM;
2529		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2530			desc->phy |= ZYD_TX_PHY_5GHZ;
2531	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2532		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2533
2534	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2535	desc->len = htole16(totlen);
2536
2537	desc->flags = ZYD_TX_FLAG_BACKOFF;
2538	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2539		/* multicast frames are not sent at OFDM rates in 802.11b/g */
2540		if (totlen > vap->iv_rtsthreshold) {
2541			desc->flags |= ZYD_TX_FLAG_RTS;
2542		} else if (ZYD_RATE_IS_OFDM(rate) &&
2543		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
2544			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2545				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2546			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2547				desc->flags |= ZYD_TX_FLAG_RTS;
2548		}
2549	} else
2550		desc->flags |= ZYD_TX_FLAG_MULTICAST;
2551	if ((wh->i_fc[0] &
2552	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2553	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2554		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2555
2556	/* actual transmit length (XXX why +10?) */
2557	pktlen = ZYD_TX_DESC_SIZE + 10;
2558	if (sc->sc_macrev == ZYD_ZD1211)
2559		pktlen += totlen;
2560	desc->pktlen = htole16(pktlen);
2561
2562	bits = (rate == 11) ? (totlen * 16) + 10 :
2563	    ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2564	desc->plcp_length = htole16(bits / ratediv[phy]);
2565	desc->plcp_service = 0;
2566	if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
2567		desc->plcp_service |= ZYD_PLCP_LENGEXT;
2568	desc->nextlen = 0;
2569
2570	if (ieee80211_radiotap_active_vap(vap)) {
2571		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2572
2573		tap->wt_flags = 0;
2574		tap->wt_rate = rate;
2575
2576		ieee80211_radiotap_tx(vap, m0);
2577	}
2578
2579	DPRINTF(sc, ZYD_DEBUG_XMIT,
2580	    "%s: sending data frame len=%zu rate=%u\n",
2581	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2582		rate);
2583
2584	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2585	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
2586
2587	return (0);
2588}
2589
2590static void
2591zyd_start(struct ifnet *ifp)
2592{
2593	struct zyd_softc *sc = ifp->if_softc;
2594	struct ieee80211_node *ni;
2595	struct mbuf *m;
2596
2597	ZYD_LOCK(sc);
2598	for (;;) {
2599		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2600		if (m == NULL)
2601			break;
2602		if (sc->tx_nfree == 0) {
2603			IFQ_DRV_PREPEND(&ifp->if_snd, m);
2604			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2605			break;
2606		}
2607		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2608		if (zyd_tx_start(sc, m, ni) != 0) {
2609			ieee80211_free_node(ni);
2610			ifp->if_oerrors++;
2611			break;
2612		}
2613	}
2614	ZYD_UNLOCK(sc);
2615}
2616
2617static int
2618zyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2619	const struct ieee80211_bpf_params *params)
2620{
2621	struct ieee80211com *ic = ni->ni_ic;
2622	struct ifnet *ifp = ic->ic_ifp;
2623	struct zyd_softc *sc = ifp->if_softc;
2624
2625	ZYD_LOCK(sc);
2626	/* prevent management frames from being sent if we're not ready */
2627	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2628		ZYD_UNLOCK(sc);
2629		m_freem(m);
2630		ieee80211_free_node(ni);
2631		return (ENETDOWN);
2632	}
2633	if (sc->tx_nfree == 0) {
2634		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2635		ZYD_UNLOCK(sc);
2636		m_freem(m);
2637		ieee80211_free_node(ni);
2638		return (ENOBUFS);		/* XXX */
2639	}
2640
2641	/*
2642	 * Legacy path; interpret frame contents to decide
2643	 * precisely how to send the frame.
2644	 * XXX raw path
2645	 */
2646	if (zyd_tx_start(sc, m, ni) != 0) {
2647		ZYD_UNLOCK(sc);
2648		ifp->if_oerrors++;
2649		ieee80211_free_node(ni);
2650		return (EIO);
2651	}
2652	ZYD_UNLOCK(sc);
2653	return (0);
2654}
2655
2656static int
2657zyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2658{
2659	struct zyd_softc *sc = ifp->if_softc;
2660	struct ieee80211com *ic = ifp->if_l2com;
2661	struct ifreq *ifr = (struct ifreq *) data;
2662	int error;
2663	int startall = 0;
2664
2665	ZYD_LOCK(sc);
2666	error = (sc->sc_flags & ZYD_FLAG_DETACHED) ? ENXIO : 0;
2667	ZYD_UNLOCK(sc);
2668	if (error)
2669		return (error);
2670
2671	switch (cmd) {
2672	case SIOCSIFFLAGS:
2673		ZYD_LOCK(sc);
2674		if (ifp->if_flags & IFF_UP) {
2675			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2676				zyd_init_locked(sc);
2677				startall = 1;
2678			} else
2679				zyd_set_multi(sc);
2680		} else {
2681			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2682				zyd_stop(sc);
2683		}
2684		ZYD_UNLOCK(sc);
2685		if (startall)
2686			ieee80211_start_all(ic);
2687		break;
2688	case SIOCGIFMEDIA:
2689		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2690		break;
2691	case SIOCGIFADDR:
2692		error = ether_ioctl(ifp, cmd, data);
2693		break;
2694	default:
2695		error = EINVAL;
2696		break;
2697	}
2698	return (error);
2699}
2700
2701static void
2702zyd_init_locked(struct zyd_softc *sc)
2703{
2704	struct ifnet *ifp = sc->sc_ifp;
2705	struct ieee80211com *ic = ifp->if_l2com;
2706	struct usb_config_descriptor *cd;
2707	int error;
2708	uint32_t val;
2709
2710	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2711
2712	if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
2713		error = zyd_loadfirmware(sc);
2714		if (error != 0) {
2715			device_printf(sc->sc_dev,
2716			    "could not load firmware (error=%d)\n", error);
2717			goto fail;
2718		}
2719
2720		/* reset device */
2721		cd = usbd_get_config_descriptor(sc->sc_udev);
2722		error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
2723		    cd->bConfigurationValue);
2724		if (error)
2725			device_printf(sc->sc_dev, "reset failed, continuing\n");
2726
2727		error = zyd_hw_init(sc);
2728		if (error) {
2729			device_printf(sc->sc_dev,
2730			    "hardware initialization failed\n");
2731			goto fail;
2732		}
2733
2734		device_printf(sc->sc_dev,
2735		    "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
2736		    "BE%x NP%x Gain%x F%x\n",
2737		    (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
2738		    sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
2739		    zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
2740		    sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
2741		    sc->sc_cckgain, sc->sc_fix_cr157);
2742
2743		/* read regulatory domain (currently unused) */
2744		zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
2745		sc->sc_regdomain = val >> 16;
2746		DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
2747		    sc->sc_regdomain);
2748
2749		/* we'll do software WEP decryption for now */
2750		DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
2751		    __func__);
2752		zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2753
2754		sc->sc_flags |= ZYD_FLAG_INITONCE;
2755	}
2756
2757	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2758		zyd_stop(sc);
2759
2760	DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
2761	    IF_LLADDR(ifp), ":");
2762	error = zyd_set_macaddr(sc, IF_LLADDR(ifp));
2763	if (error != 0)
2764		return;
2765
2766	/* set basic rates */
2767	if (ic->ic_curmode == IEEE80211_MODE_11B)
2768		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
2769	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2770		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
2771	else	/* assumes 802.11b/g */
2772		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
2773
2774	/* promiscuous mode */
2775	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
2776	/* multicast setup */
2777	zyd_set_multi(sc);
2778	/* set RX filter  */
2779	error = zyd_set_rxfilter(sc);
2780	if (error != 0)
2781		goto fail;
2782
2783	/* switch radio transmitter ON */
2784	error = zyd_switch_radio(sc, 1);
2785	if (error != 0)
2786		goto fail;
2787	/* set default BSS channel */
2788	zyd_set_chan(sc, ic->ic_curchan);
2789
2790	/*
2791	 * Allocate Tx and Rx xfer queues.
2792	 */
2793	zyd_setup_tx_list(sc);
2794
2795	/* enable interrupts */
2796	zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2797
2798	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2799	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2800	usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2801	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2802	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
2803
2804	return;
2805
2806fail:	zyd_stop(sc);
2807	return;
2808}
2809
2810static void
2811zyd_init(void *priv)
2812{
2813	struct zyd_softc *sc = priv;
2814	struct ifnet *ifp = sc->sc_ifp;
2815	struct ieee80211com *ic = ifp->if_l2com;
2816
2817	ZYD_LOCK(sc);
2818	zyd_init_locked(sc);
2819	ZYD_UNLOCK(sc);
2820
2821	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2822		ieee80211_start_all(ic);		/* start all vap's */
2823}
2824
2825static void
2826zyd_stop(struct zyd_softc *sc)
2827{
2828	struct ifnet *ifp = sc->sc_ifp;
2829	int error;
2830
2831	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2832
2833	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2834
2835	/*
2836	 * Drain all the transfers, if not already drained:
2837	 */
2838	ZYD_UNLOCK(sc);
2839	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2840	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
2841	ZYD_LOCK(sc);
2842
2843	zyd_unsetup_tx_list(sc);
2844
2845	/* Stop now if the device was never set up */
2846	if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
2847		return;
2848
2849	/* switch radio transmitter OFF */
2850	error = zyd_switch_radio(sc, 0);
2851	if (error != 0)
2852		goto fail;
2853	/* disable Rx */
2854	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
2855	/* disable interrupts */
2856	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
2857
2858fail:
2859	return;
2860}
2861
2862static int
2863zyd_loadfirmware(struct zyd_softc *sc)
2864{
2865	struct usb_device_request req;
2866	size_t size;
2867	u_char *fw;
2868	uint8_t stat;
2869	uint16_t addr;
2870
2871	if (sc->sc_flags & ZYD_FLAG_FWLOADED)
2872		return (0);
2873
2874	if (sc->sc_macrev == ZYD_ZD1211) {
2875		fw = (u_char *)zd1211_firmware;
2876		size = sizeof(zd1211_firmware);
2877	} else {
2878		fw = (u_char *)zd1211b_firmware;
2879		size = sizeof(zd1211b_firmware);
2880	}
2881
2882	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2883	req.bRequest = ZYD_DOWNLOADREQ;
2884	USETW(req.wIndex, 0);
2885
2886	addr = ZYD_FIRMWARE_START_ADDR;
2887	while (size > 0) {
2888		/*
2889		 * When the transfer size is 4096 bytes, it is not
2890		 * likely to be able to transfer it.
2891		 * The cause is port or machine or chip?
2892		 */
2893		const int mlen = min(size, 64);
2894
2895		DPRINTF(sc, ZYD_DEBUG_FW,
2896		    "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
2897
2898		USETW(req.wValue, addr);
2899		USETW(req.wLength, mlen);
2900		if (zyd_do_request(sc, &req, fw) != 0)
2901			return (EIO);
2902
2903		addr += mlen / 2;
2904		fw   += mlen;
2905		size -= mlen;
2906	}
2907
2908	/* check whether the upload succeeded */
2909	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2910	req.bRequest = ZYD_DOWNLOADSTS;
2911	USETW(req.wValue, 0);
2912	USETW(req.wIndex, 0);
2913	USETW(req.wLength, sizeof(stat));
2914	if (zyd_do_request(sc, &req, &stat) != 0)
2915		return (EIO);
2916
2917	sc->sc_flags |= ZYD_FLAG_FWLOADED;
2918
2919	return (stat & 0x80) ? (EIO) : (0);
2920}
2921
2922static void
2923zyd_scan_start(struct ieee80211com *ic)
2924{
2925	struct ifnet *ifp = ic->ic_ifp;
2926	struct zyd_softc *sc = ifp->if_softc;
2927
2928	ZYD_LOCK(sc);
2929	/* want broadcast address while scanning */
2930	zyd_set_bssid(sc, ifp->if_broadcastaddr);
2931	ZYD_UNLOCK(sc);
2932}
2933
2934static void
2935zyd_scan_end(struct ieee80211com *ic)
2936{
2937	struct zyd_softc *sc = ic->ic_ifp->if_softc;
2938
2939	ZYD_LOCK(sc);
2940	/* restore previous bssid */
2941	zyd_set_bssid(sc, sc->sc_bssid);
2942	ZYD_UNLOCK(sc);
2943}
2944
2945static void
2946zyd_set_channel(struct ieee80211com *ic)
2947{
2948	struct zyd_softc *sc = ic->ic_ifp->if_softc;
2949
2950	ZYD_LOCK(sc);
2951	zyd_set_chan(sc, ic->ic_curchan);
2952	ZYD_UNLOCK(sc);
2953}
2954
2955static device_method_t zyd_methods[] = {
2956        /* Device interface */
2957        DEVMETHOD(device_probe, zyd_match),
2958        DEVMETHOD(device_attach, zyd_attach),
2959        DEVMETHOD(device_detach, zyd_detach),
2960	DEVMETHOD_END
2961};
2962
2963static driver_t zyd_driver = {
2964	.name = "zyd",
2965	.methods = zyd_methods,
2966	.size = sizeof(struct zyd_softc)
2967};
2968
2969static devclass_t zyd_devclass;
2970
2971DRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0);
2972MODULE_DEPEND(zyd, usb, 1, 1, 1);
2973MODULE_DEPEND(zyd, wlan, 1, 1, 1);
2974MODULE_VERSION(zyd, 1);
2975