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