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