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