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