xref: /openbsd-src/sys/dev/usb/if_zyd.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: if_zyd.c,v 1.73 2009/03/27 11:40:56 jsg Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*
21  * ZyDAS ZD1211/ZD1211B USB WLAN driver.
22  */
23 
24 #include "bpfilter.h"
25 
26 #include <sys/param.h>
27 #include <sys/sockio.h>
28 #include <sys/proc.h>
29 #include <sys/mbuf.h>
30 #include <sys/kernel.h>
31 #include <sys/socket.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/timeout.h>
35 #include <sys/conf.h>
36 #include <sys/device.h>
37 
38 #include <machine/bus.h>
39 #include <machine/endian.h>
40 
41 #if NBPFILTER > 0
42 #include <net/bpf.h>
43 #endif
44 #include <net/if.h>
45 #include <net/if_arp.h>
46 #include <net/if_dl.h>
47 #include <net/if_media.h>
48 #include <net/if_types.h>
49 
50 #ifdef INET
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/in_var.h>
54 #include <netinet/if_ether.h>
55 #include <netinet/ip.h>
56 #endif
57 
58 #include <net80211/ieee80211_var.h>
59 #include <net80211/ieee80211_amrr.h>
60 #include <net80211/ieee80211_radiotap.h>
61 
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usbdi_util.h>
65 #include <dev/usb/usbdevs.h>
66 
67 #include <dev/usb/if_zydreg.h>
68 
69 #ifdef USB_DEBUG
70 #define ZYD_DEBUG
71 #endif
72 
73 #ifdef ZYD_DEBUG
74 #define DPRINTF(x)	do { if (zyddebug > 0) printf x; } while (0)
75 #define DPRINTFN(n, x)	do { if (zyddebug > (n)) printf x; } while (0)
76 int zyddebug = 0;
77 #else
78 #define DPRINTF(x)
79 #define DPRINTFN(n, x)
80 #endif
81 
82 static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
83 static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
84 
85 /* various supported device vendors/products */
86 #define ZYD_ZD1211_DEV(v, p)	\
87 	{ { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211 }
88 #define ZYD_ZD1211B_DEV(v, p)	\
89 	{ { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211B }
90 static const struct zyd_type {
91 	struct usb_devno	dev;
92 	uint8_t			rev;
93 #define ZYD_ZD1211	0
94 #define ZYD_ZD1211B	1
95 } zyd_devs[] = {
96 	ZYD_ZD1211_DEV(3COM2,		3CRUSB10075),
97 	ZYD_ZD1211_DEV(ABOCOM,		WL54),
98 	ZYD_ZD1211_DEV(ASUS,		WL159G),
99 	ZYD_ZD1211_DEV(CYBERTAN,	TG54USB),
100 	ZYD_ZD1211_DEV(DRAYTEK,		VIGOR550),
101 	ZYD_ZD1211_DEV(PLANEX2,		GWUS54GD),
102 	ZYD_ZD1211_DEV(PLANEX2,		GWUS54GZL),
103 	ZYD_ZD1211_DEV(PLANEX3,		GWUS54GZ),
104 	ZYD_ZD1211_DEV(PLANEX3,		GWUS54MINI),
105 	ZYD_ZD1211_DEV(SAGEM,		XG760A),
106 	ZYD_ZD1211_DEV(SENAO,		NUB8301),
107 	ZYD_ZD1211_DEV(SITECOMEU,	WL113),
108 	ZYD_ZD1211_DEV(SWEEX,		ZD1211),
109 	ZYD_ZD1211_DEV(TEKRAM,		QUICKWLAN),
110 	ZYD_ZD1211_DEV(TEKRAM,		ZD1211_1),
111 	ZYD_ZD1211_DEV(TEKRAM,		ZD1211_2),
112 	ZYD_ZD1211_DEV(TWINMOS,		G240),
113 	ZYD_ZD1211_DEV(UMEDIA,		ALL0298V2),
114 	ZYD_ZD1211_DEV(UMEDIA,		TEW429UB_A),
115 	ZYD_ZD1211_DEV(UMEDIA,		TEW429UB),
116 	ZYD_ZD1211_DEV(WISTRONNEWEB,	UR055G),
117 	ZYD_ZD1211_DEV(ZCOM,		ZD1211),
118 	ZYD_ZD1211_DEV(ZYDAS,		ZD1211),
119 	ZYD_ZD1211_DEV(ZYXEL,		AG225H),
120 	ZYD_ZD1211_DEV(ZYXEL,		ZYAIRG220),
121 	ZYD_ZD1211_DEV(ZYXEL,		G200V2),
122 	ZYD_ZD1211_DEV(ZYXEL,		G202),
123 
124 	ZYD_ZD1211B_DEV(ACCTON,		SMCWUSBG),
125 	ZYD_ZD1211B_DEV(ACCTON,		ZD1211B),
126 	ZYD_ZD1211B_DEV(ASUS,		A9T_WIFI),
127 	ZYD_ZD1211B_DEV(BELKIN,		F5D7050C),
128 	ZYD_ZD1211B_DEV(BELKIN,		ZD1211B),
129 	ZYD_ZD1211B_DEV(CISCOLINKSYS,	WUSBF54G),
130 	ZYD_ZD1211B_DEV(FIBERLINE,	WL430U),
131 	ZYD_ZD1211B_DEV(MELCO,		KG54L),
132 	ZYD_ZD1211B_DEV(PHILIPS,	SNU5600),
133 	ZYD_ZD1211B_DEV(PLANEX2,	GW_US54GXS),
134 	ZYD_ZD1211B_DEV(SAGEM,		XG76NA),
135 	ZYD_ZD1211B_DEV(SITECOMEU,	WL603),
136 	ZYD_ZD1211B_DEV(SITECOMEU,	ZD1211B),
137 	ZYD_ZD1211B_DEV(UMEDIA,		TEW429UBC1),
138 	ZYD_ZD1211B_DEV(UNKNOWN1,	ZD1211B_1),
139 	ZYD_ZD1211B_DEV(UNKNOWN1,	ZD1211B_2),
140 	ZYD_ZD1211B_DEV(UNKNOWN2,	ZD1211B),
141 	ZYD_ZD1211B_DEV(UNKNOWN3,	ZD1211B),
142 	ZYD_ZD1211B_DEV(USR,		USR5423),
143 	ZYD_ZD1211B_DEV(VTECH,		ZD1211B),
144 	ZYD_ZD1211B_DEV(ZCOM,		ZD1211B),
145 	ZYD_ZD1211B_DEV(ZYDAS,		ZD1211B),
146 	ZYD_ZD1211B_DEV(ZYXEL,		M202),
147 	ZYD_ZD1211B_DEV(ZYXEL,		G220V2),
148 };
149 #define zyd_lookup(v, p)	\
150 	((const struct zyd_type *)usb_lookup(zyd_devs, v, p))
151 
152 int zyd_match(struct device *, void *, void *);
153 void zyd_attach(struct device *, struct device *, void *);
154 int zyd_detach(struct device *, int);
155 int zyd_activate(struct device *, enum devact);
156 
157 struct cfdriver zyd_cd = {
158 	NULL, "zyd", DV_IFNET
159 };
160 
161 const struct cfattach zyd_ca = {
162 	sizeof(struct zyd_softc),
163 	zyd_match,
164 	zyd_attach,
165 	zyd_detach,
166 	zyd_activate,
167 };
168 
169 void		zyd_attachhook(void *);
170 int		zyd_complete_attach(struct zyd_softc *);
171 int		zyd_open_pipes(struct zyd_softc *);
172 void		zyd_close_pipes(struct zyd_softc *);
173 int		zyd_alloc_tx_list(struct zyd_softc *);
174 void		zyd_free_tx_list(struct zyd_softc *);
175 int		zyd_alloc_rx_list(struct zyd_softc *);
176 void		zyd_free_rx_list(struct zyd_softc *);
177 struct		ieee80211_node *zyd_node_alloc(struct ieee80211com *);
178 int		zyd_media_change(struct ifnet *);
179 void		zyd_next_scan(void *);
180 void		zyd_task(void *);
181 int		zyd_newstate(struct ieee80211com *, enum ieee80211_state, int);
182 int		zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
183 		    void *, int, u_int);
184 int		zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
185 int		zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
186 int		zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
187 int		zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
188 int		zyd_rfwrite(struct zyd_softc *, uint32_t);
189 void		zyd_lock_phy(struct zyd_softc *);
190 void		zyd_unlock_phy(struct zyd_softc *);
191 int		zyd_rfmd_init(struct zyd_rf *);
192 int		zyd_rfmd_switch_radio(struct zyd_rf *, int);
193 int		zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
194 int		zyd_al2230_init(struct zyd_rf *);
195 int		zyd_al2230_switch_radio(struct zyd_rf *, int);
196 int		zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
197 int		zyd_al2230_init_b(struct zyd_rf *);
198 int		zyd_al7230B_init(struct zyd_rf *);
199 int		zyd_al7230B_switch_radio(struct zyd_rf *, int);
200 int		zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
201 int		zyd_al2210_init(struct zyd_rf *);
202 int		zyd_al2210_switch_radio(struct zyd_rf *, int);
203 int		zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
204 int		zyd_gct_init(struct zyd_rf *);
205 int		zyd_gct_switch_radio(struct zyd_rf *, int);
206 int		zyd_gct_set_channel(struct zyd_rf *, uint8_t);
207 int		zyd_maxim_init(struct zyd_rf *);
208 int		zyd_maxim_switch_radio(struct zyd_rf *, int);
209 int		zyd_maxim_set_channel(struct zyd_rf *, uint8_t);
210 int		zyd_maxim2_init(struct zyd_rf *);
211 int		zyd_maxim2_switch_radio(struct zyd_rf *, int);
212 int		zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
213 int		zyd_rf_attach(struct zyd_softc *, uint8_t);
214 const char	*zyd_rf_name(uint8_t);
215 int		zyd_hw_init(struct zyd_softc *);
216 int		zyd_read_eeprom(struct zyd_softc *);
217 void		zyd_set_multi(struct zyd_softc *);
218 void		zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
219 void		zyd_set_bssid(struct zyd_softc *, const uint8_t *);
220 int		zyd_switch_radio(struct zyd_softc *, int);
221 void		zyd_set_led(struct zyd_softc *, int, int);
222 int		zyd_set_rxfilter(struct zyd_softc *);
223 void		zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
224 int		zyd_set_beacon_interval(struct zyd_softc *, int);
225 uint8_t		zyd_plcp_signal(int);
226 void		zyd_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
227 void		zyd_rx_data(struct zyd_softc *, const uint8_t *, uint16_t);
228 void		zyd_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
229 void		zyd_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
230 int		zyd_tx_data(struct zyd_softc *, struct mbuf *,
231 		    struct ieee80211_node *);
232 void		zyd_start(struct ifnet *);
233 void		zyd_watchdog(struct ifnet *);
234 int		zyd_ioctl(struct ifnet *, u_long, caddr_t);
235 int		zyd_init(struct ifnet *);
236 void		zyd_stop(struct ifnet *, int);
237 int		zyd_loadfirmware(struct zyd_softc *, u_char *, size_t);
238 void		zyd_iter_func(void *, struct ieee80211_node *);
239 void		zyd_amrr_timeout(void *);
240 void		zyd_newassoc(struct ieee80211com *, struct ieee80211_node *,
241 		    int);
242 
243 int
244 zyd_match(struct device *parent, void *match, void *aux)
245 {
246 	struct usb_attach_arg *uaa = aux;
247 
248 	if (!uaa->iface)
249 		return UMATCH_NONE;
250 
251 	return (zyd_lookup(uaa->vendor, uaa->product) != NULL) ?
252 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
253 }
254 
255 void
256 zyd_attachhook(void *xsc)
257 {
258 	struct zyd_softc *sc = xsc;
259 	const char *fwname;
260 	u_char *fw;
261 	size_t size;
262 	int error;
263 
264 	fwname = (sc->mac_rev == ZYD_ZD1211) ? "zd1211" : "zd1211b";
265 	if ((error = loadfirmware(fwname, &fw, &size)) != 0) {
266 		printf("%s: error %d, could not read firmware file %s\n",
267 		    sc->sc_dev.dv_xname, error, fwname);
268 		return;
269 	}
270 
271 	error = zyd_loadfirmware(sc, fw, size);
272 	free(fw, M_DEVBUF);
273 	if (error != 0) {
274 		printf("%s: could not load firmware (error=%d)\n",
275 		    sc->sc_dev.dv_xname, error);
276 		return;
277 	}
278 
279 	/* complete the attach process */
280 	if (zyd_complete_attach(sc) == 0)
281 		sc->attached = 1;
282 }
283 
284 void
285 zyd_attach(struct device *parent, struct device *self, void *aux)
286 {
287 	struct zyd_softc *sc = (struct zyd_softc *)self;
288 	struct usb_attach_arg *uaa = aux;
289 	usb_device_descriptor_t* ddesc;
290 
291 	sc->sc_udev = uaa->device;
292 
293 	sc->mac_rev = zyd_lookup(uaa->vendor, uaa->product)->rev;
294 
295 	ddesc = usbd_get_device_descriptor(sc->sc_udev);
296 	if (UGETW(ddesc->bcdDevice) < 0x4330) {
297 		printf("%s: device version mismatch: 0x%x "
298 		    "(only >= 43.30 supported)\n", sc->sc_dev.dv_xname,
299 		    UGETW(ddesc->bcdDevice));
300 		return;
301 	}
302 
303 	if (rootvp == NULL)
304 		mountroothook_establish(zyd_attachhook, sc);
305 	else
306 		zyd_attachhook(sc);
307 }
308 
309 int
310 zyd_complete_attach(struct zyd_softc *sc)
311 {
312 	struct ieee80211com *ic = &sc->sc_ic;
313 	struct ifnet *ifp = &ic->ic_if;
314 	usbd_status error;
315 	int i;
316 
317 	usb_init_task(&sc->sc_task, zyd_task, sc);
318 	timeout_set(&sc->scan_to, zyd_next_scan, sc);
319 
320 	sc->amrr.amrr_min_success_threshold =  1;
321 	sc->amrr.amrr_max_success_threshold = 10;
322 	timeout_set(&sc->amrr_to, zyd_amrr_timeout, sc);
323 
324 	error = usbd_set_config_no(sc->sc_udev, ZYD_CONFIG_NO, 1);
325 	if (error != 0) {
326 		printf("%s: setting config no failed\n",
327 		    sc->sc_dev.dv_xname);
328 		goto fail;
329 	}
330 
331 	error = usbd_device2interface_handle(sc->sc_udev, ZYD_IFACE_INDEX,
332 	    &sc->sc_iface);
333 	if (error != 0) {
334 		printf("%s: getting interface handle failed\n",
335 		    sc->sc_dev.dv_xname);
336 		goto fail;
337 	}
338 
339 	if ((error = zyd_open_pipes(sc)) != 0) {
340 		printf("%s: could not open pipes\n", sc->sc_dev.dv_xname);
341 		goto fail;
342 	}
343 
344 	if ((error = zyd_read_eeprom(sc)) != 0) {
345 		printf("%s: could not read EEPROM\n", sc->sc_dev.dv_xname);
346 		goto fail;
347 	}
348 
349 	if ((error = zyd_rf_attach(sc, sc->rf_rev)) != 0) {
350 		printf("%s: could not attach RF\n", sc->sc_dev.dv_xname);
351 		goto fail;
352 	}
353 
354 	if ((error = zyd_hw_init(sc)) != 0) {
355 		printf("%s: hardware initialization failed\n",
356 		    sc->sc_dev.dv_xname);
357 		goto fail;
358 	}
359 
360 	printf("%s: HMAC ZD1211%s, FW %02x.%02x, RF %s, PA %x, address %s\n",
361 	    sc->sc_dev.dv_xname, (sc->mac_rev == ZYD_ZD1211) ? "": "B",
362 	    sc->fw_rev >> 8, sc->fw_rev & 0xff, zyd_rf_name(sc->rf_rev),
363 	    sc->pa_rev, ether_sprintf(ic->ic_myaddr));
364 
365 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
366 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
367 	ic->ic_state = IEEE80211_S_INIT;
368 
369 	/* set device capabilities */
370 	ic->ic_caps =
371 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
372 	    IEEE80211_C_TXPMGT |	/* tx power management */
373 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
374 	    IEEE80211_C_WEP |		/* s/w WEP */
375 	    IEEE80211_C_RSN;		/* WPA/RSN */
376 
377 	/* set supported .11b and .11g rates */
378 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
379 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
380 
381 	/* set supported .11b and .11g channels (1 through 14) */
382 	for (i = 1; i <= 14; i++) {
383 		ic->ic_channels[i].ic_freq =
384 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
385 		ic->ic_channels[i].ic_flags =
386 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
387 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
388 	}
389 
390 	ifp->if_softc = sc;
391 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
392 	ifp->if_init = zyd_init;
393 	ifp->if_ioctl = zyd_ioctl;
394 	ifp->if_start = zyd_start;
395 	ifp->if_watchdog = zyd_watchdog;
396 	IFQ_SET_READY(&ifp->if_snd);
397 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
398 
399 	if_attach(ifp);
400 	ieee80211_ifattach(ifp);
401 	ic->ic_node_alloc = zyd_node_alloc;
402 	ic->ic_newassoc = zyd_newassoc;
403 
404 	/* override state transition machine */
405 	sc->sc_newstate = ic->ic_newstate;
406 	ic->ic_newstate = zyd_newstate;
407 	ieee80211_media_init(ifp, zyd_media_change, ieee80211_media_status);
408 
409 #if NBPFILTER > 0
410 	bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
411 	    sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
412 
413 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
414 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
415 	sc->sc_rxtap.wr_ihdr.it_present = htole32(ZYD_RX_RADIOTAP_PRESENT);
416 
417 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
418 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
419 	sc->sc_txtap.wt_ihdr.it_present = htole32(ZYD_TX_RADIOTAP_PRESENT);
420 #endif
421 
422 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
423 	    &sc->sc_dev);
424 
425 fail:	return error;
426 }
427 
428 int
429 zyd_detach(struct device *self, int flags)
430 {
431 	struct zyd_softc *sc = (struct zyd_softc *)self;
432 	struct ifnet *ifp = &sc->sc_ic.ic_if;
433 	int s;
434 
435 	s = splusb();
436 
437 	usb_rem_task(sc->sc_udev, &sc->sc_task);
438 	timeout_del(&sc->scan_to);
439 	timeout_del(&sc->amrr_to);
440 
441 	zyd_close_pipes(sc);
442 
443 	if (!sc->attached) {
444 		splx(s);
445 		return 0;
446 	}
447 
448 	ieee80211_ifdetach(ifp);
449 	if_detach(ifp);
450 
451 	zyd_free_rx_list(sc);
452 	zyd_free_tx_list(sc);
453 
454 	sc->attached = 0;
455 
456 	splx(s);
457 
458 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
459 	    &sc->sc_dev);
460 
461 	return 0;
462 }
463 
464 int
465 zyd_open_pipes(struct zyd_softc *sc)
466 {
467 	usb_endpoint_descriptor_t *edesc;
468 	int isize;
469 	usbd_status error;
470 
471 	/* interrupt in */
472 	edesc = usbd_get_endpoint_descriptor(sc->sc_iface, 0x83);
473 	if (edesc == NULL)
474 		return EINVAL;
475 
476 	isize = UGETW(edesc->wMaxPacketSize);
477 	if (isize == 0)	/* should not happen */
478 		return EINVAL;
479 
480 	sc->ibuf = malloc(isize, M_USBDEV, M_NOWAIT);
481 	if (sc->ibuf == NULL)
482 		return ENOMEM;
483 
484 	error = usbd_open_pipe_intr(sc->sc_iface, 0x83, USBD_SHORT_XFER_OK,
485 	    &sc->zyd_ep[ZYD_ENDPT_IIN], sc, sc->ibuf, isize, zyd_intr,
486 	    USBD_DEFAULT_INTERVAL);
487 	if (error != 0) {
488 		printf("%s: open rx intr pipe failed: %s\n",
489 		    sc->sc_dev.dv_xname, usbd_errstr(error));
490 		goto fail;
491 	}
492 
493 	/* interrupt out (not necessarily an interrupt pipe) */
494 	error = usbd_open_pipe(sc->sc_iface, 0x04, USBD_EXCLUSIVE_USE,
495 	    &sc->zyd_ep[ZYD_ENDPT_IOUT]);
496 	if (error != 0) {
497 		printf("%s: open tx intr pipe failed: %s\n",
498 		    sc->sc_dev.dv_xname, usbd_errstr(error));
499 		goto fail;
500 	}
501 
502 	/* bulk in */
503 	error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE,
504 	    &sc->zyd_ep[ZYD_ENDPT_BIN]);
505 	if (error != 0) {
506 		printf("%s: open rx pipe failed: %s\n",
507 		    sc->sc_dev.dv_xname, usbd_errstr(error));
508 		goto fail;
509 	}
510 
511 	/* bulk out */
512 	error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE,
513 	    &sc->zyd_ep[ZYD_ENDPT_BOUT]);
514 	if (error != 0) {
515 		printf("%s: open tx pipe failed: %s\n",
516 		    sc->sc_dev.dv_xname, usbd_errstr(error));
517 		goto fail;
518 	}
519 
520 	return 0;
521 
522 fail:	zyd_close_pipes(sc);
523 	return error;
524 }
525 
526 void
527 zyd_close_pipes(struct zyd_softc *sc)
528 {
529 	int i;
530 
531 	for (i = 0; i < ZYD_ENDPT_CNT; i++) {
532 		if (sc->zyd_ep[i] != NULL) {
533 			usbd_abort_pipe(sc->zyd_ep[i]);
534 			usbd_close_pipe(sc->zyd_ep[i]);
535 			sc->zyd_ep[i] = NULL;
536 		}
537 	}
538 	if (sc->ibuf != NULL) {
539 		free(sc->ibuf, M_USBDEV);
540 		sc->ibuf = NULL;
541 	}
542 }
543 
544 int
545 zyd_alloc_tx_list(struct zyd_softc *sc)
546 {
547 	int i, error;
548 
549 	sc->tx_queued = 0;
550 
551 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
552 		struct zyd_tx_data *data = &sc->tx_data[i];
553 
554 		data->sc = sc;	/* backpointer for callbacks */
555 
556 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
557 		if (data->xfer == NULL) {
558 			printf("%s: could not allocate tx xfer\n",
559 			    sc->sc_dev.dv_xname);
560 			error = ENOMEM;
561 			goto fail;
562 		}
563 		data->buf = usbd_alloc_buffer(data->xfer, ZYD_MAX_TXBUFSZ);
564 		if (data->buf == NULL) {
565 			printf("%s: could not allocate tx buffer\n",
566 			    sc->sc_dev.dv_xname);
567 			error = ENOMEM;
568 			goto fail;
569 		}
570 
571 		/* clear Tx descriptor */
572 		bzero(data->buf, sizeof (struct zyd_tx_desc));
573 	}
574 	return 0;
575 
576 fail:	zyd_free_tx_list(sc);
577 	return error;
578 }
579 
580 void
581 zyd_free_tx_list(struct zyd_softc *sc)
582 {
583 	struct ieee80211com *ic = &sc->sc_ic;
584 	int i;
585 
586 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
587 		struct zyd_tx_data *data = &sc->tx_data[i];
588 
589 		if (data->xfer != NULL) {
590 			usbd_free_xfer(data->xfer);
591 			data->xfer = NULL;
592 		}
593 		if (data->ni != NULL) {
594 			ieee80211_release_node(ic, data->ni);
595 			data->ni = NULL;
596 		}
597 	}
598 }
599 
600 int
601 zyd_alloc_rx_list(struct zyd_softc *sc)
602 {
603 	int i, error;
604 
605 	for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
606 		struct zyd_rx_data *data = &sc->rx_data[i];
607 
608 		data->sc = sc;	/* backpointer for callbacks */
609 
610 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
611 		if (data->xfer == NULL) {
612 			printf("%s: could not allocate rx xfer\n",
613 			    sc->sc_dev.dv_xname);
614 			error = ENOMEM;
615 			goto fail;
616 		}
617 		data->buf = usbd_alloc_buffer(data->xfer, ZYX_MAX_RXBUFSZ);
618 		if (data->buf == NULL) {
619 			printf("%s: could not allocate rx buffer\n",
620 			    sc->sc_dev.dv_xname);
621 			error = ENOMEM;
622 			goto fail;
623 		}
624 	}
625 	return 0;
626 
627 fail:	zyd_free_rx_list(sc);
628 	return error;
629 }
630 
631 void
632 zyd_free_rx_list(struct zyd_softc *sc)
633 {
634 	int i;
635 
636 	for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
637 		struct zyd_rx_data *data = &sc->rx_data[i];
638 
639 		if (data->xfer != NULL) {
640 			usbd_free_xfer(data->xfer);
641 			data->xfer = NULL;
642 		}
643 	}
644 }
645 
646 struct ieee80211_node *
647 zyd_node_alloc(struct ieee80211com *ic)
648 {
649 	return malloc(sizeof (struct zyd_node), M_DEVBUF, M_NOWAIT | M_ZERO);
650 }
651 
652 int
653 zyd_media_change(struct ifnet *ifp)
654 {
655 	int error;
656 
657 	error = ieee80211_media_change(ifp);
658 	if (error != ENETRESET)
659 		return error;
660 
661 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
662 		zyd_init(ifp);
663 
664 	return 0;
665 }
666 
667 /*
668  * This function is called periodically (every 200ms) during scanning to
669  * switch from one channel to another.
670  */
671 void
672 zyd_next_scan(void *arg)
673 {
674 	struct zyd_softc *sc = arg;
675 	struct ieee80211com *ic = &sc->sc_ic;
676 	struct ifnet *ifp = &ic->ic_if;
677 
678 	if (ic->ic_state == IEEE80211_S_SCAN)
679 		ieee80211_next_scan(ifp);
680 }
681 
682 void
683 zyd_task(void *arg)
684 {
685 	struct zyd_softc *sc = arg;
686 	struct ieee80211com *ic = &sc->sc_ic;
687 	enum ieee80211_state ostate;
688 
689 	ostate = ic->ic_state;
690 
691 	switch (sc->sc_state) {
692 	case IEEE80211_S_INIT:
693 		if (ostate == IEEE80211_S_RUN) {
694 			/* turn link LED off */
695 			zyd_set_led(sc, ZYD_LED1, 0);
696 
697 			/* stop data LED from blinking */
698 			zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 0);
699 		}
700 		break;
701 
702 	case IEEE80211_S_SCAN:
703 		zyd_set_chan(sc, ic->ic_bss->ni_chan);
704 		timeout_add(&sc->scan_to, hz / 5);
705 		break;
706 
707 	case IEEE80211_S_AUTH:
708 	case IEEE80211_S_ASSOC:
709 		zyd_set_chan(sc, ic->ic_bss->ni_chan);
710 		break;
711 
712 	case IEEE80211_S_RUN:
713 	{
714 		struct ieee80211_node *ni = ic->ic_bss;
715 
716 		zyd_set_chan(sc, ni->ni_chan);
717 
718 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
719 			/* turn link LED on */
720 			zyd_set_led(sc, ZYD_LED1, 1);
721 
722 			/* make data LED blink upon Tx */
723 			zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 1);
724 
725 			zyd_set_bssid(sc, ni->ni_bssid);
726 		}
727 
728 		if (ic->ic_opmode == IEEE80211_M_STA) {
729 			/* fake a join to init the tx rate */
730 			zyd_newassoc(ic, ni, 1);
731 		}
732 
733 		/* start automatic rate control timer */
734 		if (ic->ic_fixed_rate == -1)
735 			timeout_add_sec(&sc->amrr_to, 1);
736 
737 		break;
738 	}
739 	}
740 
741 	sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
742 }
743 
744 int
745 zyd_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
746 {
747 	struct zyd_softc *sc = ic->ic_softc;
748 
749 	usb_rem_task(sc->sc_udev, &sc->sc_task);
750 	timeout_del(&sc->scan_to);
751 	timeout_del(&sc->amrr_to);
752 
753 	/* do it in a process context */
754 	sc->sc_state = nstate;
755 	sc->sc_arg = arg;
756 	usb_add_task(sc->sc_udev, &sc->sc_task);
757 
758 	return 0;
759 }
760 
761 int
762 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
763     void *odata, int olen, u_int flags)
764 {
765 	usbd_xfer_handle xfer;
766 	struct zyd_cmd cmd;
767 	uint16_t xferflags;
768 	usbd_status error;
769 	int s;
770 
771 	if ((xfer = usbd_alloc_xfer(sc->sc_udev)) == NULL)
772 		return ENOMEM;
773 
774 	cmd.code = htole16(code);
775 	bcopy(idata, cmd.data, ilen);
776 
777 	xferflags = USBD_FORCE_SHORT_XFER;
778 	if (!(flags & ZYD_CMD_FLAG_READ))
779 		xferflags |= USBD_SYNCHRONOUS;
780 	else
781 		s = splusb();
782 
783 	sc->odata = odata;
784 	sc->olen  = olen;
785 
786 	usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_IOUT], 0, &cmd,
787 	    sizeof (uint16_t) + ilen, xferflags, ZYD_INTR_TIMEOUT, NULL);
788 	error = usbd_transfer(xfer);
789 	if (error != USBD_IN_PROGRESS && error != 0) {
790 		if (flags & ZYD_CMD_FLAG_READ)
791 			splx(s);
792 		printf("%s: could not send command (error=%s)\n",
793 		    sc->sc_dev.dv_xname, usbd_errstr(error));
794 		(void)usbd_free_xfer(xfer);
795 		return EIO;
796 	}
797 	if (!(flags & ZYD_CMD_FLAG_READ)) {
798 		(void)usbd_free_xfer(xfer);
799 		return 0;	/* write: don't wait for reply */
800 	}
801 	/* wait at most one second for command reply */
802 	error = tsleep(sc, PCATCH, "zydcmd", hz);
803 	sc->odata = NULL;	/* in case answer is received too late */
804 	splx(s);
805 
806 	(void)usbd_free_xfer(xfer);
807 	return error;
808 }
809 
810 int
811 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
812 {
813 	struct zyd_pair tmp;
814 	int error;
815 
816 	reg = htole16(reg);
817 	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof reg, &tmp, sizeof tmp,
818 	    ZYD_CMD_FLAG_READ);
819 	if (error == 0)
820 		*val = letoh16(tmp.val);
821 	return error;
822 }
823 
824 int
825 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
826 {
827 	struct zyd_pair tmp[2];
828 	uint16_t regs[2];
829 	int error;
830 
831 	regs[0] = htole16(ZYD_REG32_HI(reg));
832 	regs[1] = htole16(ZYD_REG32_LO(reg));
833 	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof regs, tmp, sizeof tmp,
834 	    ZYD_CMD_FLAG_READ);
835 	if (error == 0)
836 		*val = letoh16(tmp[0].val) << 16 | letoh16(tmp[1].val);
837 	return error;
838 }
839 
840 int
841 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
842 {
843 	struct zyd_pair pair;
844 
845 	pair.reg = htole16(reg);
846 	pair.val = htole16(val);
847 
848 	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof pair, NULL, 0, 0);
849 }
850 
851 int
852 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
853 {
854 	struct zyd_pair pair[2];
855 
856 	pair[0].reg = htole16(ZYD_REG32_HI(reg));
857 	pair[0].val = htole16(val >> 16);
858 	pair[1].reg = htole16(ZYD_REG32_LO(reg));
859 	pair[1].val = htole16(val & 0xffff);
860 
861 	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof pair, NULL, 0, 0);
862 }
863 
864 int
865 zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
866 {
867 	struct zyd_rf *rf = &sc->sc_rf;
868 	struct zyd_rfwrite req;
869 	uint16_t cr203;
870 	int i;
871 
872 	(void)zyd_read16(sc, ZYD_CR203, &cr203);
873 	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
874 
875 	req.code  = htole16(2);
876 	req.width = htole16(rf->width);
877 	for (i = 0; i < rf->width; i++) {
878 		req.bit[i] = htole16(cr203);
879 		if (val & (1 << (rf->width - 1 - i)))
880 			req.bit[i] |= htole16(ZYD_RF_DATA);
881 	}
882 	return zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
883 }
884 
885 void
886 zyd_lock_phy(struct zyd_softc *sc)
887 {
888 	uint32_t tmp;
889 
890 	(void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
891 	tmp &= ~ZYD_UNLOCK_PHY_REGS;
892 	(void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
893 }
894 
895 void
896 zyd_unlock_phy(struct zyd_softc *sc)
897 {
898 	uint32_t tmp;
899 
900 	(void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
901 	tmp |= ZYD_UNLOCK_PHY_REGS;
902 	(void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
903 }
904 
905 /*
906  * RFMD RF methods.
907  */
908 int
909 zyd_rfmd_init(struct zyd_rf *rf)
910 {
911 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
912 	struct zyd_softc *sc = rf->rf_sc;
913 	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
914 	static const uint32_t rfini[] = ZYD_RFMD_RF;
915 	int i, error;
916 
917 	/* init RF-dependent PHY registers */
918 	for (i = 0; i < N(phyini); i++) {
919 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
920 		if (error != 0)
921 			return error;
922 	}
923 
924 	/* init RFMD radio */
925 	for (i = 0; i < N(rfini); i++) {
926 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
927 			return error;
928 	}
929 	return 0;
930 #undef N
931 }
932 
933 int
934 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
935 {
936 	struct zyd_softc *sc = rf->rf_sc;
937 
938 	(void)zyd_write16(sc, ZYD_CR10, on ? 0x89 : 0x15);
939 	(void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x81);
940 
941 	return 0;
942 }
943 
944 int
945 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
946 {
947 	struct zyd_softc *sc = rf->rf_sc;
948 	static const struct {
949 		uint32_t	r1, r2;
950 	} rfprog[] = ZYD_RFMD_CHANTABLE;
951 
952 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
953 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
954 
955 	return 0;
956 }
957 
958 /*
959  * AL2230 RF methods.
960  */
961 int
962 zyd_al2230_init(struct zyd_rf *rf)
963 {
964 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
965 	struct zyd_softc *sc = rf->rf_sc;
966 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
967 	static const uint32_t rfini[] = ZYD_AL2230_RF;
968 	int i, error;
969 
970 	/* init RF-dependent PHY registers */
971 	for (i = 0; i < N(phyini); i++) {
972 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
973 		if (error != 0)
974 			return error;
975 	}
976 
977 	/* init AL2230 radio */
978 	for (i = 0; i < N(rfini); i++) {
979 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
980 			return error;
981 	}
982 	return 0;
983 #undef N
984 }
985 
986 int
987 zyd_al2230_init_b(struct zyd_rf *rf)
988 {
989 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
990 	struct zyd_softc *sc = rf->rf_sc;
991 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
992 	static const uint32_t rfini[] = ZYD_AL2230_RF_B;
993 	int i, error;
994 
995 	/* init RF-dependent PHY registers */
996 	for (i = 0; i < N(phyini); i++) {
997 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
998 		if (error != 0)
999 			return error;
1000 	}
1001 
1002 	/* init AL2230 radio */
1003 	for (i = 0; i < N(rfini); i++) {
1004 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1005 			return error;
1006 	}
1007 	return 0;
1008 #undef N
1009 }
1010 
1011 int
1012 zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1013 {
1014 	struct zyd_softc *sc = rf->rf_sc;
1015 	int on251 = (sc->mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f;
1016 
1017 	(void)zyd_write16(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1018 	(void)zyd_write16(sc, ZYD_CR251, on ? on251 : 0x2f);
1019 
1020 	return 0;
1021 }
1022 
1023 int
1024 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1025 {
1026 	struct zyd_softc *sc = rf->rf_sc;
1027 	static const struct {
1028 		uint32_t	r1, r2, r3;
1029 	} rfprog[] = ZYD_AL2230_CHANTABLE;
1030 
1031 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1032 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1033 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r3);
1034 
1035 	(void)zyd_write16(sc, ZYD_CR138, 0x28);
1036 	(void)zyd_write16(sc, ZYD_CR203, 0x06);
1037 
1038 	return 0;
1039 }
1040 
1041 /*
1042  * AL7230B RF methods.
1043  */
1044 int
1045 zyd_al7230B_init(struct zyd_rf *rf)
1046 {
1047 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
1048 	struct zyd_softc *sc = rf->rf_sc;
1049 	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1050 	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1051 	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1052 	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1053 	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1054 	int i, error;
1055 
1056 	/* for AL7230B, PHY and RF need to be initialized in "phases" */
1057 
1058 	/* init RF-dependent PHY registers, part one */
1059 	for (i = 0; i < N(phyini_1); i++) {
1060 		error = zyd_write16(sc, phyini_1[i].reg, phyini_1[i].val);
1061 		if (error != 0)
1062 			return error;
1063 	}
1064 	/* init AL7230B radio, part one */
1065 	for (i = 0; i < N(rfini_1); i++) {
1066 		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1067 			return error;
1068 	}
1069 	/* init RF-dependent PHY registers, part two */
1070 	for (i = 0; i < N(phyini_2); i++) {
1071 		error = zyd_write16(sc, phyini_2[i].reg, phyini_2[i].val);
1072 		if (error != 0)
1073 			return error;
1074 	}
1075 	/* init AL7230B radio, part two */
1076 	for (i = 0; i < N(rfini_2); i++) {
1077 		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1078 			return error;
1079 	}
1080 	/* init RF-dependent PHY registers, part three */
1081 	for (i = 0; i < N(phyini_3); i++) {
1082 		error = zyd_write16(sc, phyini_3[i].reg, phyini_3[i].val);
1083 		if (error != 0)
1084 			return error;
1085 	}
1086 
1087 	return 0;
1088 #undef N
1089 }
1090 
1091 int
1092 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1093 {
1094 	struct zyd_softc *sc = rf->rf_sc;
1095 
1096 	(void)zyd_write16(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1097 	(void)zyd_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1098 
1099 	return 0;
1100 }
1101 
1102 int
1103 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1104 {
1105 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
1106 	struct zyd_softc *sc = rf->rf_sc;
1107 	static const struct {
1108 		uint32_t	r1, r2;
1109 	} rfprog[] = ZYD_AL7230B_CHANTABLE;
1110 	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1111 	int i, error;
1112 
1113 	(void)zyd_write16(sc, ZYD_CR240, 0x57);
1114 	(void)zyd_write16(sc, ZYD_CR251, 0x2f);
1115 
1116 	for (i = 0; i < N(rfsc); i++) {
1117 		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1118 			return error;
1119 	}
1120 
1121 	(void)zyd_write16(sc, ZYD_CR128, 0x14);
1122 	(void)zyd_write16(sc, ZYD_CR129, 0x12);
1123 	(void)zyd_write16(sc, ZYD_CR130, 0x10);
1124 	(void)zyd_write16(sc, ZYD_CR38,  0x38);
1125 	(void)zyd_write16(sc, ZYD_CR136, 0xdf);
1126 
1127 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1128 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1129 	(void)zyd_rfwrite(sc, 0x3c9000);
1130 
1131 	(void)zyd_write16(sc, ZYD_CR251, 0x3f);
1132 	(void)zyd_write16(sc, ZYD_CR203, 0x06);
1133 	(void)zyd_write16(sc, ZYD_CR240, 0x08);
1134 
1135 	return 0;
1136 #undef N
1137 }
1138 
1139 /*
1140  * AL2210 RF methods.
1141  */
1142 int
1143 zyd_al2210_init(struct zyd_rf *rf)
1144 {
1145 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
1146 	struct zyd_softc *sc = rf->rf_sc;
1147 	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1148 	static const uint32_t rfini[] = ZYD_AL2210_RF;
1149 	uint32_t tmp;
1150 	int i, error;
1151 
1152 	(void)zyd_write32(sc, ZYD_CR18, 2);
1153 
1154 	/* init RF-dependent PHY registers */
1155 	for (i = 0; i < N(phyini); i++) {
1156 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1157 		if (error != 0)
1158 			return error;
1159 	}
1160 	/* init AL2210 radio */
1161 	for (i = 0; i < N(rfini); i++) {
1162 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1163 			return error;
1164 	}
1165 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
1166 	(void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1167 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1168 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1169 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1170 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1171 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
1172 	(void)zyd_write32(sc, ZYD_CR18, 3);
1173 
1174 	return 0;
1175 #undef N
1176 }
1177 
1178 int
1179 zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1180 {
1181 	/* vendor driver does nothing for this RF chip */
1182 
1183 	return 0;
1184 }
1185 
1186 int
1187 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1188 {
1189 	struct zyd_softc *sc = rf->rf_sc;
1190 	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1191 	uint32_t tmp;
1192 
1193 	(void)zyd_write32(sc, ZYD_CR18, 2);
1194 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
1195 	(void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1196 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1197 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1198 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1199 
1200 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1201 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
1202 
1203 	/* actually set the channel */
1204 	(void)zyd_rfwrite(sc, rfprog[chan - 1]);
1205 
1206 	(void)zyd_write32(sc, ZYD_CR18, 3);
1207 
1208 	return 0;
1209 }
1210 
1211 /*
1212  * GCT RF methods.
1213  */
1214 int
1215 zyd_gct_init(struct zyd_rf *rf)
1216 {
1217 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
1218 	struct zyd_softc *sc = rf->rf_sc;
1219 	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1220 	static const uint32_t rfini[] = ZYD_GCT_RF;
1221 	int i, error;
1222 
1223 	/* init RF-dependent PHY registers */
1224 	for (i = 0; i < N(phyini); i++) {
1225 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1226 		if (error != 0)
1227 			return error;
1228 	}
1229 	/* init cgt radio */
1230 	for (i = 0; i < N(rfini); i++) {
1231 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1232 			return error;
1233 	}
1234 	return 0;
1235 #undef N
1236 }
1237 
1238 int
1239 zyd_gct_switch_radio(struct zyd_rf *rf, int on)
1240 {
1241 	/* vendor driver does nothing for this RF chip */
1242 
1243 	return 0;
1244 }
1245 
1246 int
1247 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1248 {
1249 	struct zyd_softc *sc = rf->rf_sc;
1250 	static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE;
1251 
1252 	(void)zyd_rfwrite(sc, 0x1c0000);
1253 	(void)zyd_rfwrite(sc, rfprog[chan - 1]);
1254 	(void)zyd_rfwrite(sc, 0x1c0008);
1255 
1256 	return 0;
1257 }
1258 
1259 /*
1260  * Maxim RF methods.
1261  */
1262 int
1263 zyd_maxim_init(struct zyd_rf *rf)
1264 {
1265 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
1266 	struct zyd_softc *sc = rf->rf_sc;
1267 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1268 	static const uint32_t rfini[] = ZYD_MAXIM_RF;
1269 	uint16_t tmp;
1270 	int i, error;
1271 
1272 	/* init RF-dependent PHY registers */
1273 	for (i = 0; i < N(phyini); i++) {
1274 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1275 		if (error != 0)
1276 			return error;
1277 	}
1278 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1279 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1280 
1281 	/* init maxim radio */
1282 	for (i = 0; i < N(rfini); i++) {
1283 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1284 			return error;
1285 	}
1286 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1287 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1288 
1289 	return 0;
1290 #undef N
1291 }
1292 
1293 int
1294 zyd_maxim_switch_radio(struct zyd_rf *rf, int on)
1295 {
1296 	/* vendor driver does nothing for this RF chip */
1297 
1298 	return 0;
1299 }
1300 
1301 int
1302 zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan)
1303 {
1304 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
1305 	struct zyd_softc *sc = rf->rf_sc;
1306 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1307 	static const uint32_t rfini[] = ZYD_MAXIM_RF;
1308 	static const struct {
1309 		uint32_t	r1, r2;
1310 	} rfprog[] = ZYD_MAXIM_CHANTABLE;
1311 	uint16_t tmp;
1312 	int i, error;
1313 
1314 	/*
1315 	 * Do the same as we do when initializing it, except for the channel
1316 	 * values coming from the two channel tables.
1317 	 */
1318 
1319 	/* init RF-dependent PHY registers */
1320 	for (i = 0; i < N(phyini); i++) {
1321 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1322 		if (error != 0)
1323 			return error;
1324 	}
1325 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1326 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1327 
1328 	/* first two values taken from the chantables */
1329 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1330 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1331 
1332 	/* init maxim radio - skipping the two first values */
1333 	for (i = 2; i < N(rfini); i++) {
1334 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1335 			return error;
1336 	}
1337 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1338 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1339 
1340 	return 0;
1341 #undef N
1342 }
1343 
1344 /*
1345  * Maxim2 RF methods.
1346  */
1347 int
1348 zyd_maxim2_init(struct zyd_rf *rf)
1349 {
1350 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
1351 	struct zyd_softc *sc = rf->rf_sc;
1352 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1353 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1354 	uint16_t tmp;
1355 	int i, error;
1356 
1357 	/* init RF-dependent PHY registers */
1358 	for (i = 0; i < N(phyini); i++) {
1359 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1360 		if (error != 0)
1361 			return error;
1362 	}
1363 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1364 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1365 
1366 	/* init maxim2 radio */
1367 	for (i = 0; i < N(rfini); i++) {
1368 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1369 			return error;
1370 	}
1371 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1372 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1373 
1374 	return 0;
1375 #undef N
1376 }
1377 
1378 int
1379 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1380 {
1381 	/* vendor driver does nothing for this RF chip */
1382 
1383 	return 0;
1384 }
1385 
1386 int
1387 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1388 {
1389 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
1390 	struct zyd_softc *sc = rf->rf_sc;
1391 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1392 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1393 	static const struct {
1394 		uint32_t	r1, r2;
1395 	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
1396 	uint16_t tmp;
1397 	int i, error;
1398 
1399 	/*
1400 	 * Do the same as we do when initializing it, except for the channel
1401 	 * values coming from the two channel tables.
1402 	 */
1403 
1404 	/* init RF-dependent PHY registers */
1405 	for (i = 0; i < N(phyini); i++) {
1406 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1407 		if (error != 0)
1408 			return error;
1409 	}
1410 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1411 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1412 
1413 	/* first two values taken from the chantables */
1414 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1415 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1416 
1417 	/* init maxim2 radio - skipping the two first values */
1418 	for (i = 2; i < N(rfini); i++) {
1419 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1420 			return error;
1421 	}
1422 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1423 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1424 
1425 	return 0;
1426 #undef N
1427 }
1428 
1429 int
1430 zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1431 {
1432 	struct zyd_rf *rf = &sc->sc_rf;
1433 
1434 	rf->rf_sc = sc;
1435 
1436 	switch (type) {
1437 	case ZYD_RF_RFMD:
1438 		rf->init         = zyd_rfmd_init;
1439 		rf->switch_radio = zyd_rfmd_switch_radio;
1440 		rf->set_channel  = zyd_rfmd_set_channel;
1441 		rf->width        = 24;	/* 24-bit RF values */
1442 		break;
1443 	case ZYD_RF_AL2230:
1444 		if (sc->mac_rev == ZYD_ZD1211B)
1445 			rf->init = zyd_al2230_init_b;
1446 		else
1447 			rf->init = zyd_al2230_init;
1448 		rf->switch_radio = zyd_al2230_switch_radio;
1449 		rf->set_channel  = zyd_al2230_set_channel;
1450 		rf->width        = 24;	/* 24-bit RF values */
1451 		break;
1452 	case ZYD_RF_AL7230B:
1453 		rf->init         = zyd_al7230B_init;
1454 		rf->switch_radio = zyd_al7230B_switch_radio;
1455 		rf->set_channel  = zyd_al7230B_set_channel;
1456 		rf->width        = 24;	/* 24-bit RF values */
1457 		break;
1458 	case ZYD_RF_AL2210:
1459 		rf->init         = zyd_al2210_init;
1460 		rf->switch_radio = zyd_al2210_switch_radio;
1461 		rf->set_channel  = zyd_al2210_set_channel;
1462 		rf->width        = 24;	/* 24-bit RF values */
1463 		break;
1464 	case ZYD_RF_GCT:
1465 		rf->init         = zyd_gct_init;
1466 		rf->switch_radio = zyd_gct_switch_radio;
1467 		rf->set_channel  = zyd_gct_set_channel;
1468 		rf->width        = 21;	/* 21-bit RF values */
1469 		break;
1470 	case ZYD_RF_MAXIM_NEW:
1471 		rf->init         = zyd_maxim_init;
1472 		rf->switch_radio = zyd_maxim_switch_radio;
1473 		rf->set_channel  = zyd_maxim_set_channel;
1474 		rf->width        = 18;	/* 18-bit RF values */
1475 		break;
1476 	case ZYD_RF_MAXIM_NEW2:
1477 		rf->init         = zyd_maxim2_init;
1478 		rf->switch_radio = zyd_maxim2_switch_radio;
1479 		rf->set_channel  = zyd_maxim2_set_channel;
1480 		rf->width        = 18;	/* 18-bit RF values */
1481 		break;
1482 	default:
1483 		printf("%s: sorry, radio \"%s\" is not supported yet\n",
1484 		    sc->sc_dev.dv_xname, zyd_rf_name(type));
1485 		return EINVAL;
1486 	}
1487 	return 0;
1488 }
1489 
1490 const char *
1491 zyd_rf_name(uint8_t type)
1492 {
1493 	static const char * const zyd_rfs[] = {
1494 		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
1495 		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
1496 		"PV2000",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
1497 		"PHILIPS"
1498 	};
1499 	return zyd_rfs[(type > 15) ? 0 : type];
1500 }
1501 
1502 int
1503 zyd_hw_init(struct zyd_softc *sc)
1504 {
1505 	struct zyd_rf *rf = &sc->sc_rf;
1506 	const struct zyd_phy_pair *phyp;
1507 	uint32_t tmp;
1508 	int error;
1509 
1510 	/* specify that the plug and play is finished */
1511 	(void)zyd_write32(sc, ZYD_MAC_AFTER_PNP, 1);
1512 
1513 	(void)zyd_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->fwbase);
1514 	DPRINTF(("firmware base address=0x%04x\n", sc->fwbase));
1515 
1516 	/* retrieve firmware revision number */
1517 	(void)zyd_read16(sc, sc->fwbase + ZYD_FW_FIRMWARE_REV, &sc->fw_rev);
1518 
1519 	(void)zyd_write32(sc, ZYD_CR_GPI_EN, 0);
1520 	(void)zyd_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1521 
1522 	/* disable interrupts */
1523 	(void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
1524 
1525 	/* PHY init */
1526 	zyd_lock_phy(sc);
1527 	phyp = (sc->mac_rev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1528 	for (; phyp->reg != 0; phyp++) {
1529 		if ((error = zyd_write16(sc, phyp->reg, phyp->val)) != 0)
1530 			goto fail;
1531 	}
1532 	if (sc->fix_cr157) {
1533 		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
1534 			(void)zyd_write32(sc, ZYD_CR157, tmp >> 8);
1535 	}
1536 	zyd_unlock_phy(sc);
1537 
1538 	/* HMAC init */
1539 	zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1540 	zyd_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1541 
1542 	if (sc->mac_rev == ZYD_ZD1211) {
1543 		zyd_write32(sc, ZYD_MAC_RETRY, 0x00000002);
1544 	} else {
1545 		zyd_write32(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1546 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1547 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1548 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1549 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1550 		zyd_write32(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1551 		zyd_write32(sc, ZYD_MACB_AIFS_CTL2, 0x008C003c);
1552 		zyd_write32(sc, ZYD_MACB_TXOP, 0x01800824);
1553 	}
1554 
1555 	zyd_write32(sc, ZYD_MAC_SNIFFER, 0x00000000);
1556 	zyd_write32(sc, ZYD_MAC_RXFILTER, 0x00000000);
1557 	zyd_write32(sc, ZYD_MAC_GHTBL, 0x00000000);
1558 	zyd_write32(sc, ZYD_MAC_GHTBH, 0x80000000);
1559 	zyd_write32(sc, ZYD_MAC_MISC, 0x000000a4);
1560 	zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1561 	zyd_write32(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1562 	zyd_write32(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1563 	zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1564 	zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1565 	zyd_write32(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1566 	zyd_write32(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0547c032);
1567 	zyd_write32(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1568 	zyd_write32(sc, ZYD_CR_PS_CTRL, 0x10000000);
1569 	zyd_write32(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1570 	zyd_write32(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1571 	zyd_write32(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1572 
1573 	/* RF chip init */
1574 	zyd_lock_phy(sc);
1575 	error = (*rf->init)(rf);
1576 	zyd_unlock_phy(sc);
1577 	if (error != 0) {
1578 		printf("%s: radio initialization failed\n",
1579 		    sc->sc_dev.dv_xname);
1580 		goto fail;
1581 	}
1582 
1583 	/* init beacon interval to 100ms */
1584 	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1585 		goto fail;
1586 
1587 fail:	return error;
1588 }
1589 
1590 int
1591 zyd_read_eeprom(struct zyd_softc *sc)
1592 {
1593 	struct ieee80211com *ic = &sc->sc_ic;
1594 	uint32_t tmp;
1595 	uint16_t val;
1596 	int i;
1597 
1598 	/* read MAC address */
1599 	(void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P1, &tmp);
1600 	ic->ic_myaddr[0] = tmp & 0xff;
1601 	ic->ic_myaddr[1] = tmp >>  8;
1602 	ic->ic_myaddr[2] = tmp >> 16;
1603 	ic->ic_myaddr[3] = tmp >> 24;
1604 	(void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P2, &tmp);
1605 	ic->ic_myaddr[4] = tmp & 0xff;
1606 	ic->ic_myaddr[5] = tmp >>  8;
1607 
1608 	(void)zyd_read32(sc, ZYD_EEPROM_POD, &tmp);
1609 	sc->rf_rev    = tmp & 0x0f;
1610 	sc->fix_cr47  = (tmp >> 8 ) & 0x01;
1611 	sc->fix_cr157 = (tmp >> 13) & 0x01;
1612 	sc->pa_rev    = (tmp >> 16) & 0x0f;
1613 
1614 	/* read regulatory domain (currently unused) */
1615 	(void)zyd_read32(sc, ZYD_EEPROM_SUBID, &tmp);
1616 	sc->regdomain = tmp >> 16;
1617 	DPRINTF(("regulatory domain %x\n", sc->regdomain));
1618 
1619 	/* read Tx power calibration tables */
1620 	for (i = 0; i < 7; i++) {
1621 		(void)zyd_read16(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1622 		sc->pwr_cal[i * 2] = val >> 8;
1623 		sc->pwr_cal[i * 2 + 1] = val & 0xff;
1624 
1625 		(void)zyd_read16(sc, ZYD_EEPROM_PWR_INT + i, &val);
1626 		sc->pwr_int[i * 2] = val >> 8;
1627 		sc->pwr_int[i * 2 + 1] = val & 0xff;
1628 
1629 		(void)zyd_read16(sc, ZYD_EEPROM_36M_CAL + i, &val);
1630 		sc->ofdm36_cal[i * 2] = val >> 8;
1631 		sc->ofdm36_cal[i * 2 + 1] = val & 0xff;
1632 
1633 		(void)zyd_read16(sc, ZYD_EEPROM_48M_CAL + i, &val);
1634 		sc->ofdm48_cal[i * 2] = val >> 8;
1635 		sc->ofdm48_cal[i * 2 + 1] = val & 0xff;
1636 
1637 		(void)zyd_read16(sc, ZYD_EEPROM_54M_CAL + i, &val);
1638 		sc->ofdm54_cal[i * 2] = val >> 8;
1639 		sc->ofdm54_cal[i * 2 + 1] = val & 0xff;
1640 	}
1641 	return 0;
1642 }
1643 
1644 void
1645 zyd_set_multi(struct zyd_softc *sc)
1646 {
1647 	struct arpcom *ac = &sc->sc_ic.ic_ac;
1648 	struct ifnet *ifp = &ac->ac_if;
1649 	struct ether_multi *enm;
1650 	struct ether_multistep step;
1651 	uint32_t lo, hi;
1652 	uint8_t bit;
1653 
1654 	if ((ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
1655 		lo = hi = 0xffffffff;
1656 		goto done;
1657 	}
1658 	lo = hi = 0;
1659 	ETHER_FIRST_MULTI(step, ac, enm);
1660 	while (enm != NULL) {
1661 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1662 			ifp->if_flags |= IFF_ALLMULTI;
1663 			lo = hi = 0xffffffff;
1664 			goto done;
1665 		}
1666 		bit = enm->enm_addrlo[5] >> 2;
1667 		if (bit < 32)
1668 			lo |= 1 << bit;
1669 		else
1670 			hi |= 1 << (bit - 32);
1671 		ETHER_NEXT_MULTI(step, enm);
1672 	}
1673 
1674 done:
1675 	hi |= 1 << 31;	/* make sure the broadcast bit is set */
1676 	zyd_write32(sc, ZYD_MAC_GHTBL, lo);
1677 	zyd_write32(sc, ZYD_MAC_GHTBH, hi);
1678 }
1679 
1680 void
1681 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1682 {
1683 	uint32_t tmp;
1684 
1685 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1686 	(void)zyd_write32(sc, ZYD_MAC_MACADRL, tmp);
1687 
1688 	tmp = addr[5] << 8 | addr[4];
1689 	(void)zyd_write32(sc, ZYD_MAC_MACADRH, tmp);
1690 }
1691 
1692 void
1693 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1694 {
1695 	uint32_t tmp;
1696 
1697 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1698 	(void)zyd_write32(sc, ZYD_MAC_BSSADRL, tmp);
1699 
1700 	tmp = addr[5] << 8 | addr[4];
1701 	(void)zyd_write32(sc, ZYD_MAC_BSSADRH, tmp);
1702 }
1703 
1704 int
1705 zyd_switch_radio(struct zyd_softc *sc, int on)
1706 {
1707 	struct zyd_rf *rf = &sc->sc_rf;
1708 	int error;
1709 
1710 	zyd_lock_phy(sc);
1711 	error = (*rf->switch_radio)(rf, on);
1712 	zyd_unlock_phy(sc);
1713 
1714 	return error;
1715 }
1716 
1717 void
1718 zyd_set_led(struct zyd_softc *sc, int which, int on)
1719 {
1720 	uint32_t tmp;
1721 
1722 	(void)zyd_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1723 	tmp &= ~which;
1724 	if (on)
1725 		tmp |= which;
1726 	(void)zyd_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1727 }
1728 
1729 int
1730 zyd_set_rxfilter(struct zyd_softc *sc)
1731 {
1732 	uint32_t rxfilter;
1733 
1734 	switch (sc->sc_ic.ic_opmode) {
1735 	case IEEE80211_M_STA:
1736 		rxfilter = ZYD_FILTER_BSS;
1737 		break;
1738 #ifndef IEEE80211_STA_ONLY
1739 	case IEEE80211_M_IBSS:
1740 	case IEEE80211_M_HOSTAP:
1741 		rxfilter = ZYD_FILTER_HOSTAP;
1742 		break;
1743 #endif
1744 	case IEEE80211_M_MONITOR:
1745 		rxfilter = ZYD_FILTER_MONITOR;
1746 		break;
1747 	default:
1748 		/* should not get there */
1749 		return EINVAL;
1750 	}
1751 	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
1752 }
1753 
1754 void
1755 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
1756 {
1757 	struct ieee80211com *ic = &sc->sc_ic;
1758 	struct zyd_rf *rf = &sc->sc_rf;
1759 	uint32_t tmp;
1760 	u_int chan;
1761 
1762 	chan = ieee80211_chan2ieee(ic, c);
1763 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1764 		return;
1765 
1766 	zyd_lock_phy(sc);
1767 
1768 	(*rf->set_channel)(rf, chan);
1769 
1770 	/* update Tx power */
1771 	(void)zyd_write16(sc, ZYD_CR31, sc->pwr_int[chan - 1]);
1772 
1773 	if (sc->mac_rev == ZYD_ZD1211B) {
1774 		(void)zyd_write16(sc, ZYD_CR67, sc->ofdm36_cal[chan - 1]);
1775 		(void)zyd_write16(sc, ZYD_CR66, sc->ofdm48_cal[chan - 1]);
1776 		(void)zyd_write16(sc, ZYD_CR65, sc->ofdm54_cal[chan - 1]);
1777 
1778 		(void)zyd_write16(sc, ZYD_CR68, sc->pwr_cal[chan - 1]);
1779 
1780 		(void)zyd_write16(sc, ZYD_CR69, 0x28);
1781 		(void)zyd_write16(sc, ZYD_CR69, 0x2a);
1782 	}
1783 
1784 	if (sc->fix_cr47) {
1785 		/* set CCK baseband gain from EEPROM */
1786 		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
1787 			(void)zyd_write16(sc, ZYD_CR47, tmp & 0xff);
1788 	}
1789 
1790 	(void)zyd_write32(sc, ZYD_CR_CONFIG_PHILIPS, 0);
1791 
1792 	zyd_unlock_phy(sc);
1793 }
1794 
1795 int
1796 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
1797 {
1798 	/* XXX this is probably broken.. */
1799 	(void)zyd_write32(sc, ZYD_CR_ATIM_WND_PERIOD, bintval - 2);
1800 	(void)zyd_write32(sc, ZYD_CR_PRE_TBTT,        bintval - 1);
1801 	(void)zyd_write32(sc, ZYD_CR_BCN_INTERVAL,    bintval);
1802 
1803 	return 0;
1804 }
1805 
1806 uint8_t
1807 zyd_plcp_signal(int rate)
1808 {
1809 	switch (rate) {
1810 	/* CCK rates (returned values are device-dependent) */
1811 	case 2:		return 0x0;
1812 	case 4:		return 0x1;
1813 	case 11:	return 0x2;
1814 	case 22:	return 0x3;
1815 
1816 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1817 	case 12:	return 0xb;
1818 	case 18:	return 0xf;
1819 	case 24:	return 0xa;
1820 	case 36:	return 0xe;
1821 	case 48:	return 0x9;
1822 	case 72:	return 0xd;
1823 	case 96:	return 0x8;
1824 	case 108:	return 0xc;
1825 
1826 	/* unsupported rates (should not get there) */
1827 	default:	return 0xff;
1828 	}
1829 }
1830 
1831 void
1832 zyd_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1833 {
1834 	struct zyd_softc *sc = (struct zyd_softc *)priv;
1835 	const struct zyd_cmd *cmd;
1836 	uint32_t len;
1837 
1838 	if (status != USBD_NORMAL_COMPLETION) {
1839 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1840 			return;
1841 
1842 		if (status == USBD_STALLED) {
1843 			usbd_clear_endpoint_stall_async(
1844 			    sc->zyd_ep[ZYD_ENDPT_IIN]);
1845 		}
1846 		return;
1847 	}
1848 
1849 	cmd = (const struct zyd_cmd *)sc->ibuf;
1850 
1851 	if (letoh16(cmd->code) == ZYD_NOTIF_RETRYSTATUS) {
1852 		struct zyd_notif_retry *retry =
1853 		    (struct zyd_notif_retry *)cmd->data;
1854 		struct ieee80211com *ic = &sc->sc_ic;
1855 		struct ifnet *ifp = &ic->ic_if;
1856 		struct ieee80211_node *ni;
1857 
1858 		DPRINTF(("retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
1859 		    letoh16(retry->rate), ether_sprintf(retry->macaddr),
1860 		    letoh16(retry->count) & 0xff, letoh16(retry->count)));
1861 
1862 		/*
1863 		 * Find the node to which the packet was sent and update its
1864 		 * retry statistics.  In BSS mode, this node is the AP we're
1865 		 * associated to so no lookup is actually needed.
1866 		 */
1867 		if (ic->ic_opmode != IEEE80211_M_STA) {
1868 			ni = ieee80211_find_node(ic, retry->macaddr);
1869 			if (ni == NULL)
1870 				return;	/* just ignore */
1871 		} else
1872 			ni = ic->ic_bss;
1873 
1874 		((struct zyd_node *)ni)->amn.amn_retrycnt++;
1875 
1876 		if (letoh16(retry->count) & 0x100)
1877 			ifp->if_oerrors++;	/* too many retries */
1878 
1879 	} else if (letoh16(cmd->code) == ZYD_NOTIF_IORD) {
1880 		if (letoh16(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
1881 			return;	/* HMAC interrupt */
1882 
1883 		if (sc->odata == NULL)
1884 			return;	/* unexpected IORD notification */
1885 
1886 		/* copy answer into caller-supplied buffer */
1887 		usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
1888 		bcopy(cmd->data, sc->odata, sc->olen);
1889 
1890 		wakeup(sc);	/* wakeup caller */
1891 
1892 	} else {
1893 		printf("%s: unknown notification %x\n", sc->sc_dev.dv_xname,
1894 		    letoh16(cmd->code));
1895 	}
1896 }
1897 
1898 void
1899 zyd_rx_data(struct zyd_softc *sc, const uint8_t *buf, uint16_t len)
1900 {
1901 	struct ieee80211com *ic = &sc->sc_ic;
1902 	struct ifnet *ifp = &ic->ic_if;
1903 	struct ieee80211_node *ni;
1904 	struct ieee80211_frame *wh;
1905 	struct ieee80211_rxinfo rxi;
1906 	const struct zyd_plcphdr *plcp;
1907 	const struct zyd_rx_stat *stat;
1908 	struct mbuf *m;
1909 	int s;
1910 
1911 	if (len < ZYD_MIN_FRAGSZ) {
1912 		DPRINTFN(2, ("frame too short (length=%d)\n", len));
1913 		ifp->if_ierrors++;
1914 		return;
1915 	}
1916 
1917 	plcp = (const struct zyd_plcphdr *)buf;
1918 	stat = (const struct zyd_rx_stat *)(buf + len - sizeof (*stat));
1919 
1920 	if (stat->flags & ZYD_RX_ERROR) {
1921 		DPRINTF(("%s: RX status indicated error (%x)\n",
1922 		    sc->sc_dev.dv_xname, stat->flags));
1923 		ifp->if_ierrors++;
1924 		return;
1925 	}
1926 
1927 	/* compute actual frame length */
1928 	len -= sizeof (*plcp) - sizeof (*stat) - IEEE80211_CRC_LEN;
1929 
1930 	if (len > MCLBYTES) {
1931 		DPRINTFN(2, ("frame too large (length=%d)\n", len));
1932 		ifp->if_ierrors++;
1933 		return;
1934 	}
1935 
1936 	/* allocate a mbuf to store the frame */
1937 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1938 	if (m == NULL) {
1939 		ifp->if_ierrors++;
1940 		return;
1941 	}
1942 	if (len > MHLEN) {
1943 		MCLGET(m, M_DONTWAIT);
1944 		if (!(m->m_flags & M_EXT)) {
1945 			ifp->if_ierrors++;
1946 			m_freem(m);
1947 			return;
1948 		}
1949 	}
1950 	bcopy(plcp + 1, mtod(m, caddr_t), len);
1951 	m->m_pkthdr.rcvif = ifp;
1952 	m->m_pkthdr.len = m->m_len = len;
1953 
1954 #if NBPFILTER > 0
1955 	if (sc->sc_drvbpf != NULL) {
1956 		struct mbuf mb;
1957 		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
1958 		static const uint8_t rates[] = {
1959 			/* reverse function of zyd_plcp_signal() */
1960 			2, 4, 11, 22, 0, 0, 0, 0,
1961 			96, 48, 24, 12, 108, 72, 36, 18
1962 		};
1963 
1964 		tap->wr_flags = 0;
1965 		tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1966 		tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1967 		tap->wr_rssi = stat->rssi;
1968 		tap->wr_rate = rates[plcp->signal & 0xf];
1969 
1970 		mb.m_data = (caddr_t)tap;
1971 		mb.m_len = sc->sc_rxtap_len;
1972 		mb.m_next = m;
1973 		mb.m_nextpkt = NULL;
1974 		mb.m_type = 0;
1975 		mb.m_flags = 0;
1976 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
1977 	}
1978 #endif
1979 
1980 	s = splnet();
1981 	wh = mtod(m, struct ieee80211_frame *);
1982 	ni = ieee80211_find_rxnode(ic, wh);
1983 	rxi.rxi_flags = 0;
1984 	rxi.rxi_rssi = stat->rssi;
1985 	rxi.rxi_tstamp = 0;	/* unused */
1986 	ieee80211_input(ifp, m, ni, &rxi);
1987 
1988 	/* node is no longer needed */
1989 	ieee80211_release_node(ic, ni);
1990 
1991 	splx(s);
1992 }
1993 
1994 void
1995 zyd_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1996 {
1997 	struct zyd_rx_data *data = priv;
1998 	struct zyd_softc *sc = data->sc;
1999 	struct ieee80211com *ic = &sc->sc_ic;
2000 	struct ifnet *ifp = &ic->ic_if;
2001 	const struct zyd_rx_desc *desc;
2002 	int len;
2003 
2004 	if (status != USBD_NORMAL_COMPLETION) {
2005 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
2006 			return;
2007 
2008 		if (status == USBD_STALLED)
2009 			usbd_clear_endpoint_stall(sc->zyd_ep[ZYD_ENDPT_BIN]);
2010 
2011 		goto skip;
2012 	}
2013 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
2014 
2015 	if (len < ZYD_MIN_RXBUFSZ) {
2016 		DPRINTFN(2, ("xfer too short (length=%d)\n", len));
2017 		ifp->if_ierrors++;
2018 		goto skip;
2019 	}
2020 
2021 	desc = (const struct zyd_rx_desc *)
2022 	    (data->buf + len - sizeof (struct zyd_rx_desc));
2023 
2024 	if (UGETW(desc->tag) == ZYD_TAG_MULTIFRAME) {
2025 		const uint8_t *p = data->buf, *end = p + len;
2026 		int i;
2027 
2028 		DPRINTFN(3, ("received multi-frame transfer\n"));
2029 
2030 		for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2031 			const uint16_t len = UGETW(desc->len[i]);
2032 
2033 			if (len == 0 || p + len > end)
2034 				break;
2035 
2036 			zyd_rx_data(sc, p, len);
2037 			/* next frame is aligned on a 32-bit boundary */
2038 			p += (len + 3) & ~3;
2039 		}
2040 	} else {
2041 		DPRINTFN(3, ("received single-frame transfer\n"));
2042 
2043 		zyd_rx_data(sc, data->buf, len);
2044 	}
2045 
2046 skip:	/* setup a new transfer */
2047 	usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_BIN], data, NULL,
2048 	    ZYX_MAX_RXBUFSZ, USBD_NO_COPY | USBD_SHORT_XFER_OK,
2049 	    USBD_NO_TIMEOUT, zyd_rxeof);
2050 	(void)usbd_transfer(xfer);
2051 }
2052 
2053 void
2054 zyd_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
2055 {
2056 	struct zyd_tx_data *data = priv;
2057 	struct zyd_softc *sc = data->sc;
2058 	struct ieee80211com *ic = &sc->sc_ic;
2059 	struct ifnet *ifp = &ic->ic_if;
2060 	int s;
2061 
2062 	if (status != USBD_NORMAL_COMPLETION) {
2063 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
2064 			return;
2065 
2066 		printf("%s: could not transmit buffer: %s\n",
2067 		    sc->sc_dev.dv_xname, usbd_errstr(status));
2068 
2069 		if (status == USBD_STALLED) {
2070 			usbd_clear_endpoint_stall_async(
2071 			    sc->zyd_ep[ZYD_ENDPT_BOUT]);
2072 		}
2073 		ifp->if_oerrors++;
2074 		return;
2075 	}
2076 
2077 	s = splnet();
2078 
2079 	/* update rate control statistics */
2080 	((struct zyd_node *)data->ni)->amn.amn_txcnt++;
2081 
2082 	ieee80211_release_node(ic, data->ni);
2083 	data->ni = NULL;
2084 
2085 	sc->tx_queued--;
2086 	ifp->if_opackets++;
2087 
2088 	sc->tx_timer = 0;
2089 	ifp->if_flags &= ~IFF_OACTIVE;
2090 	zyd_start(ifp);
2091 
2092 	splx(s);
2093 }
2094 
2095 int
2096 zyd_tx_data(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2097 {
2098 	struct ieee80211com *ic = &sc->sc_ic;
2099 	struct ifnet *ifp = &ic->ic_if;
2100 	struct zyd_tx_desc *desc;
2101 	struct zyd_tx_data *data;
2102 	struct ieee80211_frame *wh;
2103 	struct ieee80211_key *k;
2104 	int xferlen, totlen, rate;
2105 	uint16_t pktlen;
2106 	usbd_status error;
2107 
2108 	wh = mtod(m0, struct ieee80211_frame *);
2109 
2110 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2111 		k = ieee80211_get_txkey(ic, wh, ni);
2112 
2113 		if ((m0 = ieee80211_encrypt(ic, m0, k)) == NULL)
2114 			return ENOBUFS;
2115 
2116 		/* packet header may have moved, reset our local pointer */
2117 		wh = mtod(m0, struct ieee80211_frame *);
2118 	}
2119 
2120 	/* pickup a rate */
2121 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
2122 	    ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
2123 	     IEEE80211_FC0_TYPE_MGT)) {
2124 		/* mgmt/multicast frames are sent at the lowest avail. rate */
2125 		rate = ni->ni_rates.rs_rates[0];
2126 	} else if (ic->ic_fixed_rate != -1) {
2127 		rate = ic->ic_sup_rates[ic->ic_curmode].
2128 		    rs_rates[ic->ic_fixed_rate];
2129 	} else
2130 		rate = ni->ni_rates.rs_rates[ni->ni_txrate];
2131 	rate &= IEEE80211_RATE_VAL;
2132 	if (rate == 0)	/* XXX should not happen */
2133 		rate = 2;
2134 
2135 	data = &sc->tx_data[0];
2136 	desc = (struct zyd_tx_desc *)data->buf;
2137 
2138 	data->ni = ni;
2139 
2140 	xferlen = sizeof (struct zyd_tx_desc) + m0->m_pkthdr.len;
2141 	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2142 
2143 	/* fill Tx descriptor */
2144 	desc->len = htole16(totlen);
2145 
2146 	desc->flags = ZYD_TX_FLAG_BACKOFF;
2147 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2148 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
2149 		if (totlen > ic->ic_rtsthreshold) {
2150 			desc->flags |= ZYD_TX_FLAG_RTS;
2151 		} else if (ZYD_RATE_IS_OFDM(rate) &&
2152 		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
2153 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2154 				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2155 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2156 				desc->flags |= ZYD_TX_FLAG_RTS;
2157 		}
2158 	} else
2159 		desc->flags |= ZYD_TX_FLAG_MULTICAST;
2160 
2161 	if ((wh->i_fc[0] &
2162 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2163 	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2164 		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2165 
2166 	desc->phy = zyd_plcp_signal(rate);
2167 	if (ZYD_RATE_IS_OFDM(rate)) {
2168 		desc->phy |= ZYD_TX_PHY_OFDM;
2169 		if (ic->ic_curmode == IEEE80211_MODE_11A)
2170 			desc->phy |= ZYD_TX_PHY_5GHZ;
2171 	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2172 		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2173 
2174 	/* actual transmit length (XXX why +10?) */
2175 	pktlen = sizeof (struct zyd_tx_desc) + 10;
2176 	if (sc->mac_rev == ZYD_ZD1211)
2177 		pktlen += totlen;
2178 	desc->pktlen = htole16(pktlen);
2179 
2180 	desc->plcp_length = (16 * totlen + rate - 1) / rate;
2181 	desc->plcp_service = 0;
2182 	if (rate == 22) {
2183 		const int remainder = (16 * totlen) % 22;
2184 		if (remainder != 0 && remainder < 7)
2185 			desc->plcp_service |= ZYD_PLCP_LENGEXT;
2186 	}
2187 
2188 #if NBPFILTER > 0
2189 	if (sc->sc_drvbpf != NULL) {
2190 		struct mbuf mb;
2191 		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2192 
2193 		tap->wt_flags = 0;
2194 		tap->wt_rate = rate;
2195 		tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
2196 		tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
2197 
2198 		mb.m_data = (caddr_t)tap;
2199 		mb.m_len = sc->sc_txtap_len;
2200 		mb.m_next = m0;
2201 		mb.m_nextpkt = NULL;
2202 		mb.m_type = 0;
2203 		mb.m_flags = 0;
2204 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
2205 	}
2206 #endif
2207 
2208 	m_copydata(m0, 0, m0->m_pkthdr.len,
2209 	    data->buf + sizeof (struct zyd_tx_desc));
2210 
2211 	DPRINTFN(10, ("%s: sending data frame len=%u rate=%u xferlen=%u\n",
2212 	    sc->sc_dev.dv_xname, m0->m_pkthdr.len, rate, xferlen));
2213 
2214 	m_freem(m0);	/* mbuf no longer needed */
2215 
2216 	usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BOUT], data,
2217 	    data->buf, xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
2218 	    ZYD_TX_TIMEOUT, zyd_txeof);
2219 	error = usbd_transfer(data->xfer);
2220 	if (error != USBD_IN_PROGRESS && error != 0) {
2221 		ifp->if_oerrors++;
2222 		return EIO;
2223 	}
2224 	sc->tx_queued++;
2225 
2226 	return 0;
2227 }
2228 
2229 void
2230 zyd_start(struct ifnet *ifp)
2231 {
2232 	struct zyd_softc *sc = ifp->if_softc;
2233 	struct ieee80211com *ic = &sc->sc_ic;
2234 	struct ieee80211_node *ni;
2235 	struct mbuf *m0;
2236 
2237 	/*
2238 	 * net80211 may still try to send management frames even if the
2239 	 * IFF_RUNNING flag is not set...
2240 	 */
2241 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
2242 		return;
2243 
2244 	for (;;) {
2245 		IF_POLL(&ic->ic_mgtq, m0);
2246 		if (m0 != NULL) {
2247 			if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
2248 				ifp->if_flags |= IFF_OACTIVE;
2249 				break;
2250 			}
2251 			IF_DEQUEUE(&ic->ic_mgtq, m0);
2252 
2253 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
2254 			m0->m_pkthdr.rcvif = NULL;
2255 #if NBPFILTER > 0
2256 			if (ic->ic_rawbpf != NULL)
2257 				bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
2258 #endif
2259 			if (zyd_tx_data(sc, m0, ni) != 0)
2260 				break;
2261 		} else {
2262 			if (ic->ic_state != IEEE80211_S_RUN)
2263 				break;
2264 			IFQ_POLL(&ifp->if_snd, m0);
2265 			if (m0 == NULL)
2266 				break;
2267 			if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
2268 				ifp->if_flags |= IFF_OACTIVE;
2269 				break;
2270 			}
2271 			IFQ_DEQUEUE(&ifp->if_snd, m0);
2272 #if NBPFILTER > 0
2273 			if (ifp->if_bpf != NULL)
2274 				bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
2275 #endif
2276 			if ((m0 = ieee80211_encap(ifp, m0, &ni)) == NULL) {
2277 				ifp->if_oerrors++;
2278 				continue;
2279 			}
2280 #if NBPFILTER > 0
2281 			if (ic->ic_rawbpf != NULL)
2282 				bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
2283 #endif
2284 			if (zyd_tx_data(sc, m0, ni) != 0) {
2285 				if (ni != NULL)
2286 					ieee80211_release_node(ic, ni);
2287 				ifp->if_oerrors++;
2288 				break;
2289 			}
2290 		}
2291 
2292 		sc->tx_timer = 5;
2293 		ifp->if_timer = 1;
2294 	}
2295 }
2296 
2297 void
2298 zyd_watchdog(struct ifnet *ifp)
2299 {
2300 	struct zyd_softc *sc = ifp->if_softc;
2301 
2302 	ifp->if_timer = 0;
2303 
2304 	if (sc->tx_timer > 0) {
2305 		if (--sc->tx_timer == 0) {
2306 			printf("%s: device timeout\n", sc->sc_dev.dv_xname);
2307 			/* zyd_init(ifp); XXX needs a process context ? */
2308 			ifp->if_oerrors++;
2309 			return;
2310 		}
2311 		ifp->if_timer = 1;
2312 	}
2313 
2314 	ieee80211_watchdog(ifp);
2315 }
2316 
2317 int
2318 zyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2319 {
2320 	struct zyd_softc *sc = ifp->if_softc;
2321 	struct ieee80211com *ic = &sc->sc_ic;
2322 	struct ifaddr *ifa;
2323 	struct ifreq *ifr;
2324 	int s, error = 0;
2325 
2326 	s = splnet();
2327 
2328 	switch (cmd) {
2329 	case SIOCSIFADDR:
2330 		ifa = (struct ifaddr *)data;
2331 		ifp->if_flags |= IFF_UP;
2332 #ifdef INET
2333 		if (ifa->ifa_addr->sa_family == AF_INET)
2334 			arp_ifinit(&ic->ic_ac, ifa);
2335 #endif
2336 		/* FALLTHROUGH */
2337 	case SIOCSIFFLAGS:
2338 		if (ifp->if_flags & IFF_UP) {
2339 			/*
2340 			 * If only the PROMISC or ALLMULTI flag changes, then
2341 			 * don't do a full re-init of the chip, just update
2342 			 * the Rx filter.
2343 			 */
2344 			if ((ifp->if_flags & IFF_RUNNING) &&
2345 			    ((ifp->if_flags ^ sc->sc_if_flags) &
2346 			     (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
2347 				zyd_set_multi(sc);
2348 			} else {
2349 				if (!(ifp->if_flags & IFF_RUNNING))
2350 					zyd_init(ifp);
2351 			}
2352 		} else {
2353 			if (ifp->if_flags & IFF_RUNNING)
2354 				zyd_stop(ifp, 1);
2355 		}
2356 		sc->sc_if_flags = ifp->if_flags;
2357 		break;
2358 
2359 	case SIOCADDMULTI:
2360 	case SIOCDELMULTI:
2361 		ifr = (struct ifreq *)data;
2362 		error = (cmd == SIOCADDMULTI) ?
2363 		    ether_addmulti(ifr, &ic->ic_ac) :
2364 		    ether_delmulti(ifr, &ic->ic_ac);
2365 		if (error == ENETRESET) {
2366 			if (ifp->if_flags & IFF_RUNNING)
2367 				zyd_set_multi(sc);
2368 			error = 0;
2369 		}
2370 		break;
2371 
2372 	case SIOCS80211CHANNEL:
2373 		/*
2374 		 * This allows for fast channel switching in monitor mode
2375 		 * (used by kismet). In IBSS mode, we must explicitly reset
2376 		 * the interface to generate a new beacon frame.
2377 		 */
2378 		error = ieee80211_ioctl(ifp, cmd, data);
2379 		if (error == ENETRESET &&
2380 		    ic->ic_opmode == IEEE80211_M_MONITOR) {
2381 			zyd_set_chan(sc, ic->ic_ibss_chan);
2382 			error = 0;
2383 		}
2384 		break;
2385 
2386 	default:
2387 		error = ieee80211_ioctl(ifp, cmd, data);
2388 	}
2389 
2390 	if (error == ENETRESET) {
2391 		if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) ==
2392 		    (IFF_RUNNING | IFF_UP))
2393 			zyd_init(ifp);
2394 		error = 0;
2395 	}
2396 
2397 	splx(s);
2398 
2399 	return error;
2400 }
2401 
2402 int
2403 zyd_init(struct ifnet *ifp)
2404 {
2405 	struct zyd_softc *sc = ifp->if_softc;
2406 	struct ieee80211com *ic = &sc->sc_ic;
2407 	int i, error;
2408 
2409 	zyd_stop(ifp, 0);
2410 
2411 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
2412 	DPRINTF(("setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
2413 	zyd_set_macaddr(sc, ic->ic_myaddr);
2414 
2415 	/* we'll do software WEP decryption for now */
2416 	DPRINTF(("setting encryption type\n"));
2417 	error = zyd_write32(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2418 	if (error != 0)
2419 		return error;
2420 
2421 	/* promiscuous mode */
2422 	(void)zyd_write32(sc, ZYD_MAC_SNIFFER,
2423 	    (ic->ic_opmode == IEEE80211_M_MONITOR) ? 1 : 0);
2424 
2425 	(void)zyd_set_rxfilter(sc);
2426 
2427 	/* switch radio transmitter ON */
2428 	(void)zyd_switch_radio(sc, 1);
2429 
2430 	/* set basic rates */
2431 	if (ic->ic_curmode == IEEE80211_MODE_11B)
2432 		(void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x0003);
2433 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2434 		(void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x1500);
2435 	else	/* assumes 802.11b/g */
2436 		(void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x000f);
2437 
2438 	/* set mandatory rates */
2439 	if (ic->ic_curmode == IEEE80211_MODE_11B)
2440 		(void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x000f);
2441 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2442 		(void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x1500);
2443 	else	/* assumes 802.11b/g */
2444 		(void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x150f);
2445 
2446 	/* set default BSS channel */
2447 	ic->ic_bss->ni_chan = ic->ic_ibss_chan;
2448 	zyd_set_chan(sc, ic->ic_bss->ni_chan);
2449 
2450 	/* enable interrupts */
2451 	(void)zyd_write32(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2452 
2453 	/*
2454 	 * Allocate Tx and Rx xfer queues.
2455 	 */
2456 	if ((error = zyd_alloc_tx_list(sc)) != 0) {
2457 		printf("%s: could not allocate Tx list\n",
2458 		    sc->sc_dev.dv_xname);
2459 		goto fail;
2460 	}
2461 	if ((error = zyd_alloc_rx_list(sc)) != 0) {
2462 		printf("%s: could not allocate Rx list\n",
2463 		    sc->sc_dev.dv_xname);
2464 		goto fail;
2465 	}
2466 
2467 	/*
2468 	 * Start up the receive pipe.
2469 	 */
2470 	for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
2471 		struct zyd_rx_data *data = &sc->rx_data[i];
2472 
2473 		usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BIN], data,
2474 		    NULL, ZYX_MAX_RXBUFSZ, USBD_NO_COPY | USBD_SHORT_XFER_OK,
2475 		    USBD_NO_TIMEOUT, zyd_rxeof);
2476 		error = usbd_transfer(data->xfer);
2477 		if (error != USBD_IN_PROGRESS && error != 0) {
2478 			printf("%s: could not queue Rx transfer\n",
2479 			    sc->sc_dev.dv_xname);
2480 			goto fail;
2481 		}
2482 	}
2483 
2484 	ifp->if_flags &= ~IFF_OACTIVE;
2485 	ifp->if_flags |= IFF_RUNNING;
2486 
2487 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2488 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2489 	else
2490 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2491 
2492 	return 0;
2493 
2494 fail:	zyd_stop(ifp, 1);
2495 	return error;
2496 }
2497 
2498 void
2499 zyd_stop(struct ifnet *ifp, int disable)
2500 {
2501 	struct zyd_softc *sc = ifp->if_softc;
2502 	struct ieee80211com *ic = &sc->sc_ic;
2503 
2504 	sc->tx_timer = 0;
2505 	ifp->if_timer = 0;
2506 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2507 
2508 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);	/* free all nodes */
2509 
2510 	/* switch radio transmitter OFF */
2511 	(void)zyd_switch_radio(sc, 0);
2512 
2513 	/* disable Rx */
2514 	(void)zyd_write32(sc, ZYD_MAC_RXFILTER, 0);
2515 
2516 	/* disable interrupts */
2517 	(void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
2518 
2519 	usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BIN]);
2520 	usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BOUT]);
2521 
2522 	zyd_free_rx_list(sc);
2523 	zyd_free_tx_list(sc);
2524 }
2525 
2526 int
2527 zyd_loadfirmware(struct zyd_softc *sc, u_char *fw, size_t size)
2528 {
2529 	usb_device_request_t req;
2530 	uint16_t addr;
2531 	uint8_t stat;
2532 
2533 	DPRINTF(("firmware size=%d\n", size));
2534 
2535 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2536 	req.bRequest = ZYD_DOWNLOADREQ;
2537 	USETW(req.wIndex, 0);
2538 
2539 	addr = ZYD_FIRMWARE_START_ADDR;
2540 	while (size > 0) {
2541 		const int mlen = min(size, 4096);
2542 
2543 		DPRINTF(("loading firmware block: len=%d, addr=0x%x\n", mlen,
2544 		    addr));
2545 
2546 		USETW(req.wValue, addr);
2547 		USETW(req.wLength, mlen);
2548 		if (usbd_do_request(sc->sc_udev, &req, fw) != 0)
2549 			return EIO;
2550 
2551 		addr += mlen / 2;
2552 		fw   += mlen;
2553 		size -= mlen;
2554 	}
2555 
2556 	/* check whether the upload succeeded */
2557 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2558 	req.bRequest = ZYD_DOWNLOADSTS;
2559 	USETW(req.wValue, 0);
2560 	USETW(req.wIndex, 0);
2561 	USETW(req.wLength, sizeof stat);
2562 	if (usbd_do_request(sc->sc_udev, &req, &stat) != 0)
2563 		return EIO;
2564 
2565 	return (stat & 0x80) ? EIO : 0;
2566 }
2567 
2568 void
2569 zyd_iter_func(void *arg, struct ieee80211_node *ni)
2570 {
2571 	struct zyd_softc *sc = arg;
2572 	struct zyd_node *zn = (struct zyd_node *)ni;
2573 
2574 	ieee80211_amrr_choose(&sc->amrr, ni, &zn->amn);
2575 }
2576 
2577 void
2578 zyd_amrr_timeout(void *arg)
2579 {
2580 	struct zyd_softc *sc = arg;
2581 	struct ieee80211com *ic = &sc->sc_ic;
2582 	int s;
2583 
2584 	s = splnet();
2585 	if (ic->ic_opmode == IEEE80211_M_STA)
2586 		zyd_iter_func(sc, ic->ic_bss);
2587 	else
2588 		ieee80211_iterate_nodes(ic, zyd_iter_func, sc);
2589 	splx(s);
2590 
2591 	timeout_add_sec(&sc->amrr_to, 1);
2592 }
2593 
2594 void
2595 zyd_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
2596 {
2597 	struct zyd_softc *sc = ic->ic_softc;
2598 	int i;
2599 
2600 	ieee80211_amrr_node_init(&sc->amrr, &((struct zyd_node *)ni)->amn);
2601 
2602 	/* set rate to some reasonable initial value */
2603 	for (i = ni->ni_rates.rs_nrates - 1;
2604 	     i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2605 	     i--);
2606 	ni->ni_txrate = i;
2607 }
2608 
2609 int
2610 zyd_activate(struct device *self, enum devact act)
2611 {
2612 	switch (act) {
2613 	case DVACT_ACTIVATE:
2614 		break;
2615 
2616 	case DVACT_DEACTIVATE:
2617 		break;
2618 	}
2619 	return 0;
2620 }
2621