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