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