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