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