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