if_zyd.c revision 1.19 1 /* $OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $ */
2 /* $NetBSD: if_zyd.c,v 1.19 2009/06/26 00:06:27 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.19 2009/06/26 00:06:27 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 (struct ieee80211_node *)zn;
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 usb_rem_task(sc->sc_udev, &sc->sc_task);
788 callout_stop(&sc->sc_scan_ch);
789 callout_stop(&sc->sc_amrr_ch);
790
791 /* do it in a process context */
792 sc->sc_state = nstate;
793 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
794
795 return 0;
796 }
797
798 Static int
799 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
800 void *odata, int olen, u_int flags)
801 {
802 usbd_xfer_handle xfer;
803 struct zyd_cmd cmd;
804 struct rq rq;
805 uint16_t xferflags;
806 usbd_status error;
807 int s = 0;
808
809 if ((xfer = usbd_alloc_xfer(sc->sc_udev)) == NULL)
810 return ENOMEM;
811
812 cmd.code = htole16(code);
813 bcopy(idata, cmd.data, ilen);
814
815 xferflags = USBD_FORCE_SHORT_XFER;
816 if (!(flags & ZYD_CMD_FLAG_READ))
817 xferflags |= USBD_SYNCHRONOUS;
818 else {
819 s = splusb();
820 rq.idata = idata;
821 rq.odata = odata;
822 rq.len = olen / sizeof (struct zyd_pair);
823 SIMPLEQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
824 }
825
826 usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_IOUT], 0, &cmd,
827 sizeof (uint16_t) + ilen, xferflags, ZYD_INTR_TIMEOUT, NULL);
828 error = usbd_transfer(xfer);
829 if (error != USBD_IN_PROGRESS && error != 0) {
830 if (flags & ZYD_CMD_FLAG_READ)
831 splx(s);
832 printf("%s: could not send command (error=%s)\n",
833 device_xname(sc->sc_dev), usbd_errstr(error));
834 (void)usbd_free_xfer(xfer);
835 return EIO;
836 }
837 if (!(flags & ZYD_CMD_FLAG_READ)) {
838 (void)usbd_free_xfer(xfer);
839 return 0; /* write: don't wait for reply */
840 }
841 /* wait at most one second for command reply */
842 error = tsleep(odata, PCATCH, "zydcmd", hz);
843 if (error == EWOULDBLOCK)
844 printf("%s: zyd_read sleep timeout\n", device_xname(sc->sc_dev));
845 SIMPLEQ_REMOVE(&sc->sc_rqh, &rq, rq, rq);
846 splx(s);
847
848 (void)usbd_free_xfer(xfer);
849 return error;
850 }
851
852 Static int
853 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
854 {
855 struct zyd_pair tmp;
856 int error;
857
858 reg = htole16(reg);
859 error = zyd_cmd(sc, ZYD_CMD_IORD, ®, sizeof reg, &tmp, sizeof tmp,
860 ZYD_CMD_FLAG_READ);
861 if (error == 0)
862 *val = le16toh(tmp.val);
863 return error;
864 }
865
866 Static int
867 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
868 {
869 struct zyd_pair tmp[2];
870 uint16_t regs[2];
871 int error;
872
873 regs[0] = htole16(ZYD_REG32_HI(reg));
874 regs[1] = htole16(ZYD_REG32_LO(reg));
875 error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof regs, tmp, sizeof tmp,
876 ZYD_CMD_FLAG_READ);
877 if (error == 0)
878 *val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
879 return error;
880 }
881
882 Static int
883 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
884 {
885 struct zyd_pair pair;
886
887 pair.reg = htole16(reg);
888 pair.val = htole16(val);
889
890 return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof pair, NULL, 0, 0);
891 }
892
893 Static int
894 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
895 {
896 struct zyd_pair pair[2];
897
898 pair[0].reg = htole16(ZYD_REG32_HI(reg));
899 pair[0].val = htole16(val >> 16);
900 pair[1].reg = htole16(ZYD_REG32_LO(reg));
901 pair[1].val = htole16(val & 0xffff);
902
903 return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof pair, NULL, 0, 0);
904 }
905
906 Static int
907 zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
908 {
909 struct zyd_rf *rf = &sc->sc_rf;
910 struct zyd_rfwrite req;
911 uint16_t cr203;
912 int i;
913
914 (void)zyd_read16(sc, ZYD_CR203, &cr203);
915 cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
916
917 req.code = htole16(2);
918 req.width = htole16(rf->width);
919 for (i = 0; i < rf->width; i++) {
920 req.bit[i] = htole16(cr203);
921 if (val & (1 << (rf->width - 1 - i)))
922 req.bit[i] |= htole16(ZYD_RF_DATA);
923 }
924 return zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
925 }
926
927 Static void
928 zyd_lock_phy(struct zyd_softc *sc)
929 {
930 uint32_t tmp;
931
932 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
933 tmp &= ~ZYD_UNLOCK_PHY_REGS;
934 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
935 }
936
937 Static void
938 zyd_unlock_phy(struct zyd_softc *sc)
939 {
940 uint32_t tmp;
941
942 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
943 tmp |= ZYD_UNLOCK_PHY_REGS;
944 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
945 }
946
947 /*
948 * RFMD RF methods.
949 */
950 Static int
951 zyd_rfmd_init(struct zyd_rf *rf)
952 {
953 #define N(a) (sizeof (a) / sizeof ((a)[0]))
954 struct zyd_softc *sc = rf->rf_sc;
955 static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
956 static const uint32_t rfini[] = ZYD_RFMD_RF;
957 int i, error;
958
959 /* init RF-dependent PHY registers */
960 for (i = 0; i < N(phyini); i++) {
961 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
962 if (error != 0)
963 return error;
964 }
965
966 /* init RFMD radio */
967 for (i = 0; i < N(rfini); i++) {
968 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
969 return error;
970 }
971 return 0;
972 #undef N
973 }
974
975 Static int
976 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
977 {
978 struct zyd_softc *sc = rf->rf_sc;
979
980 (void)zyd_write16(sc, ZYD_CR10, on ? 0x89 : 0x15);
981 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x81);
982
983 return 0;
984 }
985
986 Static int
987 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
988 {
989 struct zyd_softc *sc = rf->rf_sc;
990 static const struct {
991 uint32_t r1, r2;
992 } rfprog[] = ZYD_RFMD_CHANTABLE;
993
994 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
995 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
996
997 return 0;
998 }
999
1000 /*
1001 * AL2230 RF methods.
1002 */
1003 Static int
1004 zyd_al2230_init(struct zyd_rf *rf)
1005 {
1006 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1007 struct zyd_softc *sc = rf->rf_sc;
1008 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
1009 static const uint32_t rfini[] = ZYD_AL2230_RF;
1010 int i, error;
1011
1012 /* init RF-dependent PHY registers */
1013 for (i = 0; i < N(phyini); i++) {
1014 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1015 if (error != 0)
1016 return error;
1017 }
1018
1019 /* init AL2230 radio */
1020 for (i = 0; i < N(rfini); i++) {
1021 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1022 return error;
1023 }
1024 return 0;
1025 #undef N
1026 }
1027
1028 Static int
1029 zyd_al2230_init_b(struct zyd_rf *rf)
1030 {
1031 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1032 struct zyd_softc *sc = rf->rf_sc;
1033 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1034 static const uint32_t rfini[] = ZYD_AL2230_RF_B;
1035 int i, error;
1036
1037 /* init RF-dependent PHY registers */
1038 for (i = 0; i < N(phyini); i++) {
1039 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1040 if (error != 0)
1041 return error;
1042 }
1043
1044 /* init AL2230 radio */
1045 for (i = 0; i < N(rfini); i++) {
1046 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1047 return error;
1048 }
1049 return 0;
1050 #undef N
1051 }
1052
1053 Static int
1054 zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1055 {
1056 struct zyd_softc *sc = rf->rf_sc;
1057 int on251 = (sc->mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f;
1058
1059 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04);
1060 (void)zyd_write16(sc, ZYD_CR251, on ? on251 : 0x2f);
1061
1062 return 0;
1063 }
1064
1065 Static int
1066 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1067 {
1068 struct zyd_softc *sc = rf->rf_sc;
1069 static const struct {
1070 uint32_t r1, r2, r3;
1071 } rfprog[] = ZYD_AL2230_CHANTABLE;
1072
1073 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1074 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1075 (void)zyd_rfwrite(sc, rfprog[chan - 1].r3);
1076
1077 (void)zyd_write16(sc, ZYD_CR138, 0x28);
1078 (void)zyd_write16(sc, ZYD_CR203, 0x06);
1079
1080 return 0;
1081 }
1082
1083 /*
1084 * AL7230B RF methods.
1085 */
1086 Static int
1087 zyd_al7230B_init(struct zyd_rf *rf)
1088 {
1089 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1090 struct zyd_softc *sc = rf->rf_sc;
1091 static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1092 static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1093 static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1094 static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1095 static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1096 int i, error;
1097
1098 /* for AL7230B, PHY and RF need to be initialized in "phases" */
1099
1100 /* init RF-dependent PHY registers, part one */
1101 for (i = 0; i < N(phyini_1); i++) {
1102 error = zyd_write16(sc, phyini_1[i].reg, phyini_1[i].val);
1103 if (error != 0)
1104 return error;
1105 }
1106 /* init AL7230B radio, part one */
1107 for (i = 0; i < N(rfini_1); i++) {
1108 if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1109 return error;
1110 }
1111 /* init RF-dependent PHY registers, part two */
1112 for (i = 0; i < N(phyini_2); i++) {
1113 error = zyd_write16(sc, phyini_2[i].reg, phyini_2[i].val);
1114 if (error != 0)
1115 return error;
1116 }
1117 /* init AL7230B radio, part two */
1118 for (i = 0; i < N(rfini_2); i++) {
1119 if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1120 return error;
1121 }
1122 /* init RF-dependent PHY registers, part three */
1123 for (i = 0; i < N(phyini_3); i++) {
1124 error = zyd_write16(sc, phyini_3[i].reg, phyini_3[i].val);
1125 if (error != 0)
1126 return error;
1127 }
1128
1129 return 0;
1130 #undef N
1131 }
1132
1133 Static int
1134 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1135 {
1136 struct zyd_softc *sc = rf->rf_sc;
1137
1138 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04);
1139 (void)zyd_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1140
1141 return 0;
1142 }
1143
1144 Static int
1145 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1146 {
1147 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1148 struct zyd_softc *sc = rf->rf_sc;
1149 static const struct {
1150 uint32_t r1, r2;
1151 } rfprog[] = ZYD_AL7230B_CHANTABLE;
1152 static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1153 int i, error;
1154
1155 (void)zyd_write16(sc, ZYD_CR240, 0x57);
1156 (void)zyd_write16(sc, ZYD_CR251, 0x2f);
1157
1158 for (i = 0; i < N(rfsc); i++) {
1159 if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1160 return error;
1161 }
1162
1163 (void)zyd_write16(sc, ZYD_CR128, 0x14);
1164 (void)zyd_write16(sc, ZYD_CR129, 0x12);
1165 (void)zyd_write16(sc, ZYD_CR130, 0x10);
1166 (void)zyd_write16(sc, ZYD_CR38, 0x38);
1167 (void)zyd_write16(sc, ZYD_CR136, 0xdf);
1168
1169 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1170 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1171 (void)zyd_rfwrite(sc, 0x3c9000);
1172
1173 (void)zyd_write16(sc, ZYD_CR251, 0x3f);
1174 (void)zyd_write16(sc, ZYD_CR203, 0x06);
1175 (void)zyd_write16(sc, ZYD_CR240, 0x08);
1176
1177 return 0;
1178 #undef N
1179 }
1180
1181 /*
1182 * AL2210 RF methods.
1183 */
1184 Static int
1185 zyd_al2210_init(struct zyd_rf *rf)
1186 {
1187 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1188 struct zyd_softc *sc = rf->rf_sc;
1189 static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1190 static const uint32_t rfini[] = ZYD_AL2210_RF;
1191 uint32_t tmp;
1192 int i, error;
1193
1194 (void)zyd_write32(sc, ZYD_CR18, 2);
1195
1196 /* init RF-dependent PHY registers */
1197 for (i = 0; i < N(phyini); i++) {
1198 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1199 if (error != 0)
1200 return error;
1201 }
1202 /* init AL2210 radio */
1203 for (i = 0; i < N(rfini); i++) {
1204 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1205 return error;
1206 }
1207 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1208 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1209 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1210 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1211 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1212 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1213 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1214 (void)zyd_write32(sc, ZYD_CR18, 3);
1215
1216 return 0;
1217 #undef N
1218 }
1219
1220 Static int
1221 zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1222 {
1223 /* vendor driver does nothing for this RF chip */
1224
1225 return 0;
1226 }
1227
1228 Static int
1229 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1230 {
1231 struct zyd_softc *sc = rf->rf_sc;
1232 static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1233 uint32_t tmp;
1234
1235 (void)zyd_write32(sc, ZYD_CR18, 2);
1236 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1237 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1238 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1239 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1240 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1241
1242 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1243 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1244
1245 /* actually set the channel */
1246 (void)zyd_rfwrite(sc, rfprog[chan - 1]);
1247
1248 (void)zyd_write32(sc, ZYD_CR18, 3);
1249
1250 return 0;
1251 }
1252
1253 /*
1254 * GCT RF methods.
1255 */
1256 Static int
1257 zyd_gct_init(struct zyd_rf *rf)
1258 {
1259 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1260 struct zyd_softc *sc = rf->rf_sc;
1261 static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1262 static const uint32_t rfini[] = ZYD_GCT_RF;
1263 int i, error;
1264
1265 /* init RF-dependent PHY registers */
1266 for (i = 0; i < N(phyini); i++) {
1267 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1268 if (error != 0)
1269 return error;
1270 }
1271 /* init cgt radio */
1272 for (i = 0; i < N(rfini); i++) {
1273 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1274 return error;
1275 }
1276 return 0;
1277 #undef N
1278 }
1279
1280 Static int
1281 zyd_gct_switch_radio(struct zyd_rf *rf, int on)
1282 {
1283 /* vendor driver does nothing for this RF chip */
1284
1285 return 0;
1286 }
1287
1288 Static int
1289 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1290 {
1291 struct zyd_softc *sc = rf->rf_sc;
1292 static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE;
1293
1294 (void)zyd_rfwrite(sc, 0x1c0000);
1295 (void)zyd_rfwrite(sc, rfprog[chan - 1]);
1296 (void)zyd_rfwrite(sc, 0x1c0008);
1297
1298 return 0;
1299 }
1300
1301 /*
1302 * Maxim RF methods.
1303 */
1304 Static int
1305 zyd_maxim_init(struct zyd_rf *rf)
1306 {
1307 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1308 struct zyd_softc *sc = rf->rf_sc;
1309 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1310 static const uint32_t rfini[] = ZYD_MAXIM_RF;
1311 uint16_t tmp;
1312 int i, error;
1313
1314 /* init RF-dependent PHY registers */
1315 for (i = 0; i < N(phyini); i++) {
1316 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1317 if (error != 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 /* init maxim radio */
1324 for (i = 0; i < N(rfini); i++) {
1325 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1326 return error;
1327 }
1328 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1329 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1330
1331 return 0;
1332 #undef N
1333 }
1334
1335 Static int
1336 zyd_maxim_switch_radio(struct zyd_rf *rf, int on)
1337 {
1338 /* vendor driver does nothing for this RF chip */
1339
1340 return 0;
1341 }
1342
1343 Static int
1344 zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan)
1345 {
1346 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1347 struct zyd_softc *sc = rf->rf_sc;
1348 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1349 static const uint32_t rfini[] = ZYD_MAXIM_RF;
1350 static const struct {
1351 uint32_t r1, r2;
1352 } rfprog[] = ZYD_MAXIM_CHANTABLE;
1353 uint16_t tmp;
1354 int i, error;
1355
1356 /*
1357 * Do the same as we do when initializing it, except for the channel
1358 * values coming from the two channel tables.
1359 */
1360
1361 /* init RF-dependent PHY registers */
1362 for (i = 0; i < N(phyini); i++) {
1363 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1364 if (error != 0)
1365 return error;
1366 }
1367 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1368 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1369
1370 /* first two values taken from the chantables */
1371 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1372 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1373
1374 /* init maxim radio - skipping the two first values */
1375 for (i = 2; i < N(rfini); i++) {
1376 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1377 return error;
1378 }
1379 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1380 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1381
1382 return 0;
1383 #undef N
1384 }
1385
1386 /*
1387 * Maxim2 RF methods.
1388 */
1389 Static int
1390 zyd_maxim2_init(struct zyd_rf *rf)
1391 {
1392 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1393 struct zyd_softc *sc = rf->rf_sc;
1394 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1395 static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1396 uint16_t tmp;
1397 int i, error;
1398
1399 /* init RF-dependent PHY registers */
1400 for (i = 0; i < N(phyini); i++) {
1401 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1402 if (error != 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 /* init maxim2 radio */
1409 for (i = 0; i < N(rfini); i++) {
1410 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1411 return error;
1412 }
1413 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1414 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1415
1416 return 0;
1417 #undef N
1418 }
1419
1420 Static int
1421 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1422 {
1423 /* vendor driver does nothing for this RF chip */
1424
1425 return 0;
1426 }
1427
1428 Static int
1429 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1430 {
1431 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1432 struct zyd_softc *sc = rf->rf_sc;
1433 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1434 static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1435 static const struct {
1436 uint32_t r1, r2;
1437 } rfprog[] = ZYD_MAXIM2_CHANTABLE;
1438 uint16_t tmp;
1439 int i, error;
1440
1441 /*
1442 * Do the same as we do when initializing it, except for the channel
1443 * values coming from the two channel tables.
1444 */
1445
1446 /* init RF-dependent PHY registers */
1447 for (i = 0; i < N(phyini); i++) {
1448 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1449 if (error != 0)
1450 return error;
1451 }
1452 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1453 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1454
1455 /* first two values taken from the chantables */
1456 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1457 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1458
1459 /* init maxim2 radio - skipping the two first values */
1460 for (i = 2; i < N(rfini); i++) {
1461 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1462 return error;
1463 }
1464 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1465 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1466
1467 return 0;
1468 #undef N
1469 }
1470
1471 Static int
1472 zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1473 {
1474 struct zyd_rf *rf = &sc->sc_rf;
1475
1476 rf->rf_sc = sc;
1477
1478 switch (type) {
1479 case ZYD_RF_RFMD:
1480 rf->init = zyd_rfmd_init;
1481 rf->switch_radio = zyd_rfmd_switch_radio;
1482 rf->set_channel = zyd_rfmd_set_channel;
1483 rf->width = 24; /* 24-bit RF values */
1484 break;
1485 case ZYD_RF_AL2230:
1486 if (sc->mac_rev == ZYD_ZD1211B)
1487 rf->init = zyd_al2230_init_b;
1488 else
1489 rf->init = zyd_al2230_init;
1490 rf->switch_radio = zyd_al2230_switch_radio;
1491 rf->set_channel = zyd_al2230_set_channel;
1492 rf->width = 24; /* 24-bit RF values */
1493 break;
1494 case ZYD_RF_AL7230B:
1495 rf->init = zyd_al7230B_init;
1496 rf->switch_radio = zyd_al7230B_switch_radio;
1497 rf->set_channel = zyd_al7230B_set_channel;
1498 rf->width = 24; /* 24-bit RF values */
1499 break;
1500 case ZYD_RF_AL2210:
1501 rf->init = zyd_al2210_init;
1502 rf->switch_radio = zyd_al2210_switch_radio;
1503 rf->set_channel = zyd_al2210_set_channel;
1504 rf->width = 24; /* 24-bit RF values */
1505 break;
1506 case ZYD_RF_GCT:
1507 rf->init = zyd_gct_init;
1508 rf->switch_radio = zyd_gct_switch_radio;
1509 rf->set_channel = zyd_gct_set_channel;
1510 rf->width = 21; /* 21-bit RF values */
1511 break;
1512 case ZYD_RF_MAXIM_NEW:
1513 rf->init = zyd_maxim_init;
1514 rf->switch_radio = zyd_maxim_switch_radio;
1515 rf->set_channel = zyd_maxim_set_channel;
1516 rf->width = 18; /* 18-bit RF values */
1517 break;
1518 case ZYD_RF_MAXIM_NEW2:
1519 rf->init = zyd_maxim2_init;
1520 rf->switch_radio = zyd_maxim2_switch_radio;
1521 rf->set_channel = zyd_maxim2_set_channel;
1522 rf->width = 18; /* 18-bit RF values */
1523 break;
1524 default:
1525 printf("%s: sorry, radio \"%s\" is not supported yet\n",
1526 device_xname(sc->sc_dev), zyd_rf_name(type));
1527 return EINVAL;
1528 }
1529 return 0;
1530 }
1531
1532 Static const char *
1533 zyd_rf_name(uint8_t type)
1534 {
1535 static const char * const zyd_rfs[] = {
1536 "unknown", "unknown", "UW2451", "UCHIP", "AL2230",
1537 "AL7230B", "THETA", "AL2210", "MAXIM_NEW", "GCT",
1538 "PV2000", "RALINK", "INTERSIL", "RFMD", "MAXIM_NEW2",
1539 "PHILIPS"
1540 };
1541
1542 return zyd_rfs[(type > 15) ? 0 : type];
1543 }
1544
1545 Static int
1546 zyd_hw_init(struct zyd_softc *sc)
1547 {
1548 struct zyd_rf *rf = &sc->sc_rf;
1549 const struct zyd_phy_pair *phyp;
1550 int error;
1551
1552 /* specify that the plug and play is finished */
1553 (void)zyd_write32(sc, ZYD_MAC_AFTER_PNP, 1);
1554
1555 (void)zyd_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->fwbase);
1556 DPRINTF(("firmware base address=0x%04x\n", sc->fwbase));
1557
1558 /* retrieve firmware revision number */
1559 (void)zyd_read16(sc, sc->fwbase + ZYD_FW_FIRMWARE_REV, &sc->fw_rev);
1560
1561 (void)zyd_write32(sc, ZYD_CR_GPI_EN, 0);
1562 (void)zyd_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1563
1564 /* disable interrupts */
1565 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
1566
1567 /* PHY init */
1568 zyd_lock_phy(sc);
1569 phyp = (sc->mac_rev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1570 for (; phyp->reg != 0; phyp++) {
1571 if ((error = zyd_write16(sc, phyp->reg, phyp->val)) != 0)
1572 goto fail;
1573 }
1574 zyd_unlock_phy(sc);
1575
1576 /* HMAC init */
1577 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1578 zyd_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1579
1580 if (sc->mac_rev == ZYD_ZD1211) {
1581 zyd_write32(sc, ZYD_MAC_RETRY, 0x00000002);
1582 } else {
1583 zyd_write32(sc, ZYD_MAC_RETRY, 0x02020202);
1584 zyd_write32(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1585 zyd_write32(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1586 zyd_write32(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1587 zyd_write32(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1588 zyd_write32(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1589 zyd_write32(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1590 zyd_write32(sc, ZYD_MACB_TXOP, 0x01800824);
1591 }
1592
1593 zyd_write32(sc, ZYD_MAC_SNIFFER, 0x00000000);
1594 zyd_write32(sc, ZYD_MAC_RXFILTER, 0x00000000);
1595 zyd_write32(sc, ZYD_MAC_GHTBL, 0x00000000);
1596 zyd_write32(sc, ZYD_MAC_GHTBH, 0x80000000);
1597 zyd_write32(sc, ZYD_MAC_MISC, 0x000000a4);
1598 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1599 zyd_write32(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1600 zyd_write32(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1601 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1602 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1603 zyd_write32(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1604 zyd_write32(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0547c032);
1605 zyd_write32(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1606 zyd_write32(sc, ZYD_CR_PS_CTRL, 0x10000000);
1607 zyd_write32(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1608 zyd_write32(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1609 zyd_write32(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1610
1611 /* RF chip init */
1612 zyd_lock_phy(sc);
1613 error = (*rf->init)(rf);
1614 zyd_unlock_phy(sc);
1615 if (error != 0) {
1616 printf("%s: radio initialization failed\n",
1617 device_xname(sc->sc_dev));
1618 goto fail;
1619 }
1620
1621 /* init beacon interval to 100ms */
1622 if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1623 goto fail;
1624
1625 fail: return error;
1626 }
1627
1628 Static int
1629 zyd_read_eeprom(struct zyd_softc *sc)
1630 {
1631 struct ieee80211com *ic = &sc->sc_ic;
1632 uint32_t tmp;
1633 uint16_t val;
1634 int i;
1635
1636 /* read MAC address */
1637 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P1, &tmp);
1638 ic->ic_myaddr[0] = tmp & 0xff;
1639 ic->ic_myaddr[1] = tmp >> 8;
1640 ic->ic_myaddr[2] = tmp >> 16;
1641 ic->ic_myaddr[3] = tmp >> 24;
1642 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P2, &tmp);
1643 ic->ic_myaddr[4] = tmp & 0xff;
1644 ic->ic_myaddr[5] = tmp >> 8;
1645
1646 (void)zyd_read32(sc, ZYD_EEPROM_POD, &tmp);
1647 sc->rf_rev = tmp & 0x0f;
1648 sc->pa_rev = (tmp >> 16) & 0x0f;
1649
1650 /* read regulatory domain (currently unused) */
1651 (void)zyd_read32(sc, ZYD_EEPROM_SUBID, &tmp);
1652 sc->regdomain = tmp >> 16;
1653 DPRINTF(("regulatory domain %x\n", sc->regdomain));
1654
1655 /* read Tx power calibration tables */
1656 for (i = 0; i < 7; i++) {
1657 (void)zyd_read16(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1658 sc->pwr_cal[i * 2] = val >> 8;
1659 sc->pwr_cal[i * 2 + 1] = val & 0xff;
1660
1661 (void)zyd_read16(sc, ZYD_EEPROM_PWR_INT + i, &val);
1662 sc->pwr_int[i * 2] = val >> 8;
1663 sc->pwr_int[i * 2 + 1] = val & 0xff;
1664
1665 (void)zyd_read16(sc, ZYD_EEPROM_36M_CAL + i, &val);
1666 sc->ofdm36_cal[i * 2] = val >> 8;
1667 sc->ofdm36_cal[i * 2 + 1] = val & 0xff;
1668
1669 (void)zyd_read16(sc, ZYD_EEPROM_48M_CAL + i, &val);
1670 sc->ofdm48_cal[i * 2] = val >> 8;
1671 sc->ofdm48_cal[i * 2 + 1] = val & 0xff;
1672
1673 (void)zyd_read16(sc, ZYD_EEPROM_54M_CAL + i, &val);
1674 sc->ofdm54_cal[i * 2] = val >> 8;
1675 sc->ofdm54_cal[i * 2 + 1] = val & 0xff;
1676 }
1677 return 0;
1678 }
1679
1680 Static int
1681 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1682 {
1683 uint32_t tmp;
1684
1685 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1686 (void)zyd_write32(sc, ZYD_MAC_MACADRL, tmp);
1687
1688 tmp = addr[5] << 8 | addr[4];
1689 (void)zyd_write32(sc, ZYD_MAC_MACADRH, tmp);
1690
1691 return 0;
1692 }
1693
1694 Static int
1695 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1696 {
1697 uint32_t tmp;
1698
1699 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1700 (void)zyd_write32(sc, ZYD_MAC_BSSADRL, tmp);
1701
1702 tmp = addr[5] << 8 | addr[4];
1703 (void)zyd_write32(sc, ZYD_MAC_BSSADRH, tmp);
1704
1705 return 0;
1706 }
1707
1708 Static int
1709 zyd_switch_radio(struct zyd_softc *sc, int on)
1710 {
1711 struct zyd_rf *rf = &sc->sc_rf;
1712 int error;
1713
1714 zyd_lock_phy(sc);
1715 error = (*rf->switch_radio)(rf, on);
1716 zyd_unlock_phy(sc);
1717
1718 return error;
1719 }
1720
1721 Static void
1722 zyd_set_led(struct zyd_softc *sc, int which, int on)
1723 {
1724 uint32_t tmp;
1725
1726 (void)zyd_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1727 tmp &= ~which;
1728 if (on)
1729 tmp |= which;
1730 (void)zyd_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1731 }
1732
1733 Static int
1734 zyd_set_rxfilter(struct zyd_softc *sc)
1735 {
1736 uint32_t rxfilter;
1737
1738 switch (sc->sc_ic.ic_opmode) {
1739 case IEEE80211_M_STA:
1740 rxfilter = ZYD_FILTER_BSS;
1741 break;
1742 case IEEE80211_M_IBSS:
1743 case IEEE80211_M_HOSTAP:
1744 rxfilter = ZYD_FILTER_HOSTAP;
1745 break;
1746 case IEEE80211_M_MONITOR:
1747 rxfilter = ZYD_FILTER_MONITOR;
1748 break;
1749 default:
1750 /* should not get there */
1751 return EINVAL;
1752 }
1753 return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
1754 }
1755
1756 Static void
1757 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
1758 {
1759 struct ieee80211com *ic = &sc->sc_ic;
1760 struct zyd_rf *rf = &sc->sc_rf;
1761 u_int chan;
1762
1763 chan = ieee80211_chan2ieee(ic, c);
1764 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1765 return;
1766
1767 zyd_lock_phy(sc);
1768
1769 (*rf->set_channel)(rf, chan);
1770
1771 /* update Tx power */
1772 (void)zyd_write32(sc, ZYD_CR31, sc->pwr_int[chan - 1]);
1773 (void)zyd_write32(sc, ZYD_CR68, sc->pwr_cal[chan - 1]);
1774
1775 if (sc->mac_rev == ZYD_ZD1211B) {
1776 (void)zyd_write32(sc, ZYD_CR67, sc->ofdm36_cal[chan - 1]);
1777 (void)zyd_write32(sc, ZYD_CR66, sc->ofdm48_cal[chan - 1]);
1778 (void)zyd_write32(sc, ZYD_CR65, sc->ofdm54_cal[chan - 1]);
1779
1780 (void)zyd_write32(sc, ZYD_CR69, 0x28);
1781 (void)zyd_write32(sc, ZYD_CR69, 0x2a);
1782 }
1783
1784 zyd_unlock_phy(sc);
1785 }
1786
1787 Static int
1788 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
1789 {
1790 /* XXX this is probably broken.. */
1791 (void)zyd_write32(sc, ZYD_CR_ATIM_WND_PERIOD, bintval - 2);
1792 (void)zyd_write32(sc, ZYD_CR_PRE_TBTT, bintval - 1);
1793 (void)zyd_write32(sc, ZYD_CR_BCN_INTERVAL, bintval);
1794
1795 return 0;
1796 }
1797
1798 Static uint8_t
1799 zyd_plcp_signal(int rate)
1800 {
1801 switch (rate) {
1802 /* CCK rates (returned values are device-dependent) */
1803 case 2: return 0x0;
1804 case 4: return 0x1;
1805 case 11: return 0x2;
1806 case 22: return 0x3;
1807
1808 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1809 case 12: return 0xb;
1810 case 18: return 0xf;
1811 case 24: return 0xa;
1812 case 36: return 0xe;
1813 case 48: return 0x9;
1814 case 72: return 0xd;
1815 case 96: return 0x8;
1816 case 108: return 0xc;
1817
1818 /* unsupported rates (should not get there) */
1819 default: return 0xff;
1820 }
1821 }
1822
1823 Static void
1824 zyd_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1825 {
1826 struct zyd_softc *sc = (struct zyd_softc *)priv;
1827 struct zyd_cmd *cmd;
1828 uint32_t datalen;
1829
1830 if (status != USBD_NORMAL_COMPLETION) {
1831 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1832 return;
1833
1834 if (status == USBD_STALLED) {
1835 usbd_clear_endpoint_stall_async(
1836 sc->zyd_ep[ZYD_ENDPT_IIN]);
1837 }
1838 return;
1839 }
1840
1841 cmd = (struct zyd_cmd *)sc->ibuf;
1842
1843 if (le16toh(cmd->code) == ZYD_NOTIF_RETRYSTATUS) {
1844 struct zyd_notif_retry *retry =
1845 (struct zyd_notif_retry *)cmd->data;
1846 struct ieee80211com *ic = &sc->sc_ic;
1847 struct ifnet *ifp = &sc->sc_if;
1848 struct ieee80211_node *ni;
1849
1850 DPRINTF(("retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
1851 le16toh(retry->rate), ether_sprintf(retry->macaddr),
1852 le16toh(retry->count) & 0xff, le16toh(retry->count)));
1853
1854 /*
1855 * Find the node to which the packet was sent and update its
1856 * retry statistics. In BSS mode, this node is the AP we're
1857 * associated to so no lookup is actually needed.
1858 */
1859 if (ic->ic_opmode != IEEE80211_M_STA) {
1860 ni = ieee80211_find_node(&ic->ic_scan, retry->macaddr);
1861 if (ni == NULL)
1862 return; /* just ignore */
1863 } else
1864 ni = ic->ic_bss;
1865
1866 ((struct zyd_node *)ni)->amn.amn_retrycnt++;
1867
1868 if (le16toh(retry->count) & 0x100)
1869 ifp->if_oerrors++; /* too many retries */
1870
1871 } else if (le16toh(cmd->code) == ZYD_NOTIF_IORD) {
1872 struct rq *rqp;
1873
1874 if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
1875 return; /* HMAC interrupt */
1876
1877 usbd_get_xfer_status(xfer, NULL, NULL, &datalen, NULL);
1878 datalen -= sizeof(cmd->code);
1879 datalen -= 2; /* XXX: padding? */
1880
1881 SIMPLEQ_FOREACH(rqp, &sc->sc_rqh, rq) {
1882 int i;
1883
1884 if (sizeof(struct zyd_pair) * rqp->len != datalen)
1885 continue;
1886 for (i = 0; i < rqp->len; i++) {
1887 if (*(((const uint16_t *)rqp->idata) + i) !=
1888 (((struct zyd_pair *)cmd->data) + i)->reg)
1889 break;
1890 }
1891 if (i != rqp->len)
1892 continue;
1893
1894 /* copy answer into caller-supplied buffer */
1895 bcopy(cmd->data, rqp->odata,
1896 sizeof(struct zyd_pair) * rqp->len);
1897 wakeup(rqp->odata); /* wakeup caller */
1898
1899 return;
1900 }
1901 return; /* unexpected IORD notification */
1902 } else {
1903 printf("%s: unknown notification %x\n", device_xname(sc->sc_dev),
1904 le16toh(cmd->code));
1905 }
1906 }
1907
1908 Static void
1909 zyd_rx_data(struct zyd_softc *sc, const uint8_t *buf, uint16_t len)
1910 {
1911 struct ieee80211com *ic = &sc->sc_ic;
1912 struct ifnet *ifp = &sc->sc_if;
1913 struct ieee80211_node *ni;
1914 struct ieee80211_frame *wh;
1915 const struct zyd_plcphdr *plcp;
1916 const struct zyd_rx_stat *stat;
1917 struct mbuf *m;
1918 int rlen, s;
1919
1920 if (len < ZYD_MIN_FRAGSZ) {
1921 printf("%s: frame too short (length=%d)\n",
1922 device_xname(sc->sc_dev), len);
1923 ifp->if_ierrors++;
1924 return;
1925 }
1926
1927 plcp = (const struct zyd_plcphdr *)buf;
1928 stat = (const struct zyd_rx_stat *)
1929 (buf + len - sizeof (struct zyd_rx_stat));
1930
1931 if (stat->flags & ZYD_RX_ERROR) {
1932 DPRINTF(("%s: RX status indicated error (%x)\n",
1933 device_xname(sc->sc_dev), stat->flags));
1934 ifp->if_ierrors++;
1935 return;
1936 }
1937
1938 /* compute actual frame length */
1939 rlen = len - sizeof (struct zyd_plcphdr) -
1940 sizeof (struct zyd_rx_stat) - IEEE80211_CRC_LEN;
1941
1942 /* allocate a mbuf to store the frame */
1943 MGETHDR(m, M_DONTWAIT, MT_DATA);
1944 if (m == NULL) {
1945 printf("%s: could not allocate rx mbuf\n",
1946 device_xname(sc->sc_dev));
1947 ifp->if_ierrors++;
1948 return;
1949 }
1950 if (rlen > MHLEN) {
1951 MCLGET(m, M_DONTWAIT);
1952 if (!(m->m_flags & M_EXT)) {
1953 printf("%s: could not allocate rx mbuf cluster\n",
1954 device_xname(sc->sc_dev));
1955 m_freem(m);
1956 ifp->if_ierrors++;
1957 return;
1958 }
1959 }
1960 m->m_pkthdr.rcvif = ifp;
1961 m->m_pkthdr.len = m->m_len = rlen;
1962 bcopy((const uint8_t *)(plcp + 1), mtod(m, uint8_t *), rlen);
1963
1964 s = splnet();
1965
1966 #if NBPFILTER > 0
1967 if (sc->sc_drvbpf != NULL) {
1968 struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
1969 static const uint8_t rates[] = {
1970 /* reverse function of zyd_plcp_signal() */
1971 2, 4, 11, 22, 0, 0, 0, 0,
1972 96, 48, 24, 12, 108, 72, 36, 18
1973 };
1974
1975 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
1976 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1977 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1978 tap->wr_rssi = stat->rssi;
1979 tap->wr_rate = rates[plcp->signal & 0xf];
1980
1981 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1982 }
1983 #endif
1984
1985 wh = mtod(m, struct ieee80211_frame *);
1986 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1987 ieee80211_input(ic, m, ni, stat->rssi, 0);
1988
1989 /* node is no longer needed */
1990 ieee80211_free_node(ni);
1991
1992 splx(s);
1993 }
1994
1995 Static void
1996 zyd_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1997 {
1998 struct zyd_rx_data *data = priv;
1999 struct zyd_softc *sc = data->sc;
2000 struct ifnet *ifp = &sc->sc_if;
2001 const struct zyd_rx_desc *desc;
2002 int len;
2003
2004 if (status != USBD_NORMAL_COMPLETION) {
2005 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
2006 return;
2007
2008 if (status == USBD_STALLED)
2009 usbd_clear_endpoint_stall(sc->zyd_ep[ZYD_ENDPT_BIN]);
2010
2011 goto skip;
2012 }
2013 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
2014
2015 if (len < ZYD_MIN_RXBUFSZ) {
2016 printf("%s: xfer too short (length=%d)\n",
2017 device_xname(sc->sc_dev), len);
2018 ifp->if_ierrors++;
2019 goto skip;
2020 }
2021
2022 desc = (const struct zyd_rx_desc *)
2023 (data->buf + len - sizeof (struct zyd_rx_desc));
2024
2025 if (UGETW(desc->tag) == ZYD_TAG_MULTIFRAME) {
2026 const uint8_t *p = data->buf, *end = p + len;
2027 int i;
2028
2029 DPRINTFN(3, ("received multi-frame transfer\n"));
2030
2031 for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2032 const uint16_t len16 = UGETW(desc->len[i]);
2033
2034 if (len16 == 0 || p + len16 > end)
2035 break;
2036
2037 zyd_rx_data(sc, p, len16);
2038 /* next frame is aligned on a 32-bit boundary */
2039 p += (len16 + 3) & ~3;
2040 }
2041 } else {
2042 DPRINTFN(3, ("received single-frame transfer\n"));
2043
2044 zyd_rx_data(sc, data->buf, len);
2045 }
2046
2047 skip: /* setup a new transfer */
2048 usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_BIN], data, NULL,
2049 ZYX_MAX_RXBUFSZ, USBD_NO_COPY | USBD_SHORT_XFER_OK,
2050 USBD_NO_TIMEOUT, zyd_rxeof);
2051 (void)usbd_transfer(xfer);
2052 }
2053
2054 Static int
2055 zyd_tx_mgt(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2056 {
2057 struct ieee80211com *ic = &sc->sc_ic;
2058 struct ifnet *ifp = &sc->sc_if;
2059 struct zyd_tx_desc *desc;
2060 struct zyd_tx_data *data;
2061 struct ieee80211_frame *wh;
2062 struct ieee80211_key *k;
2063 int xferlen, totlen, rate;
2064 uint16_t pktlen;
2065 usbd_status error;
2066
2067 data = &sc->tx_data[0];
2068 desc = (struct zyd_tx_desc *)data->buf;
2069
2070 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
2071
2072 wh = mtod(m0, struct ieee80211_frame *);
2073
2074 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2075 k = ieee80211_crypto_encap(ic, ni, m0);
2076 if (k == NULL) {
2077 m_freem(m0);
2078 return ENOBUFS;
2079 }
2080 }
2081
2082 data->ni = ni;
2083
2084 wh = mtod(m0, struct ieee80211_frame *);
2085
2086 xferlen = sizeof (struct zyd_tx_desc) + m0->m_pkthdr.len;
2087 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2088
2089 /* fill Tx descriptor */
2090 desc->len = htole16(totlen);
2091
2092 desc->flags = ZYD_TX_FLAG_BACKOFF;
2093 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2094 /* multicast frames are not sent at OFDM rates in 802.11b/g */
2095 if (totlen > ic->ic_rtsthreshold) {
2096 desc->flags |= ZYD_TX_FLAG_RTS;
2097 } else if (ZYD_RATE_IS_OFDM(rate) &&
2098 (ic->ic_flags & IEEE80211_F_USEPROT)) {
2099 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2100 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2101 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2102 desc->flags |= ZYD_TX_FLAG_RTS;
2103 }
2104 } else
2105 desc->flags |= ZYD_TX_FLAG_MULTICAST;
2106
2107 if ((wh->i_fc[0] &
2108 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2109 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2110 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2111
2112 desc->phy = zyd_plcp_signal(rate);
2113 if (ZYD_RATE_IS_OFDM(rate)) {
2114 desc->phy |= ZYD_TX_PHY_OFDM;
2115 if (ic->ic_curmode == IEEE80211_MODE_11A)
2116 desc->phy |= ZYD_TX_PHY_5GHZ;
2117 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2118 desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2119
2120 /* actual transmit length (XXX why +10?) */
2121 pktlen = sizeof (struct zyd_tx_desc) + 10;
2122 if (sc->mac_rev == ZYD_ZD1211)
2123 pktlen += totlen;
2124 desc->pktlen = htole16(pktlen);
2125
2126 desc->plcp_length = (16 * totlen + rate - 1) / rate;
2127 desc->plcp_service = 0;
2128 if (rate == 22) {
2129 const int remainder = (16 * totlen) % 22;
2130 if (remainder != 0 && remainder < 7)
2131 desc->plcp_service |= ZYD_PLCP_LENGEXT;
2132 }
2133
2134 #if NBPFILTER > 0
2135 if (sc->sc_drvbpf != NULL) {
2136 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2137
2138 tap->wt_flags = 0;
2139 tap->wt_rate = rate;
2140 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2141 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2142
2143 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
2144 }
2145 #endif
2146
2147 m_copydata(m0, 0, m0->m_pkthdr.len,
2148 data->buf + sizeof (struct zyd_tx_desc));
2149
2150 DPRINTFN(10, ("%s: sending mgt frame len=%zu rate=%u xferlen=%u\n",
2151 device_xname(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen));
2152
2153 m_freem(m0); /* mbuf no longer needed */
2154
2155 usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BOUT], data,
2156 data->buf, xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
2157 ZYD_TX_TIMEOUT, zyd_txeof);
2158 error = usbd_transfer(data->xfer);
2159 if (error != USBD_IN_PROGRESS && error != 0) {
2160 ifp->if_oerrors++;
2161 return EIO;
2162 }
2163 sc->tx_queued++;
2164
2165 return 0;
2166 }
2167
2168 Static void
2169 zyd_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
2170 {
2171 struct zyd_tx_data *data = priv;
2172 struct zyd_softc *sc = data->sc;
2173 struct ifnet *ifp = &sc->sc_if;
2174 int s;
2175
2176 if (status != USBD_NORMAL_COMPLETION) {
2177 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
2178 return;
2179
2180 printf("%s: could not transmit buffer: %s\n",
2181 device_xname(sc->sc_dev), usbd_errstr(status));
2182
2183 if (status == USBD_STALLED) {
2184 usbd_clear_endpoint_stall_async(
2185 sc->zyd_ep[ZYD_ENDPT_BOUT]);
2186 }
2187 ifp->if_oerrors++;
2188 return;
2189 }
2190
2191 s = splnet();
2192
2193 /* update rate control statistics */
2194 ((struct zyd_node *)data->ni)->amn.amn_txcnt++;
2195
2196 ieee80211_free_node(data->ni);
2197 data->ni = NULL;
2198
2199 sc->tx_queued--;
2200 ifp->if_opackets++;
2201
2202 sc->tx_timer = 0;
2203 ifp->if_flags &= ~IFF_OACTIVE;
2204 zyd_start(ifp);
2205
2206 splx(s);
2207 }
2208
2209 Static int
2210 zyd_tx_data(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2211 {
2212 struct ieee80211com *ic = &sc->sc_ic;
2213 struct ifnet *ifp = &sc->sc_if;
2214 struct zyd_tx_desc *desc;
2215 struct zyd_tx_data *data;
2216 struct ieee80211_frame *wh;
2217 struct ieee80211_key *k;
2218 int xferlen, totlen, rate;
2219 uint16_t pktlen;
2220 usbd_status error;
2221
2222 wh = mtod(m0, struct ieee80211_frame *);
2223
2224 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
2225 rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate];
2226 else
2227 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
2228 rate &= IEEE80211_RATE_VAL;
2229
2230 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2231 k = ieee80211_crypto_encap(ic, ni, m0);
2232 if (k == NULL) {
2233 m_freem(m0);
2234 return ENOBUFS;
2235 }
2236
2237 /* packet header may have moved, reset our local pointer */
2238 wh = mtod(m0, struct ieee80211_frame *);
2239 }
2240
2241 data = &sc->tx_data[0];
2242 desc = (struct zyd_tx_desc *)data->buf;
2243
2244 data->ni = ni;
2245
2246 xferlen = sizeof (struct zyd_tx_desc) + m0->m_pkthdr.len;
2247 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2248
2249 /* fill Tx descriptor */
2250 desc->len = htole16(totlen);
2251
2252 desc->flags = ZYD_TX_FLAG_BACKOFF;
2253 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2254 /* multicast frames are not sent at OFDM rates in 802.11b/g */
2255 if (totlen > ic->ic_rtsthreshold) {
2256 desc->flags |= ZYD_TX_FLAG_RTS;
2257 } else if (ZYD_RATE_IS_OFDM(rate) &&
2258 (ic->ic_flags & IEEE80211_F_USEPROT)) {
2259 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2260 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2261 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2262 desc->flags |= ZYD_TX_FLAG_RTS;
2263 }
2264 } else
2265 desc->flags |= ZYD_TX_FLAG_MULTICAST;
2266
2267 if ((wh->i_fc[0] &
2268 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2269 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2270 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2271
2272 desc->phy = zyd_plcp_signal(rate);
2273 if (ZYD_RATE_IS_OFDM(rate)) {
2274 desc->phy |= ZYD_TX_PHY_OFDM;
2275 if (ic->ic_curmode == IEEE80211_MODE_11A)
2276 desc->phy |= ZYD_TX_PHY_5GHZ;
2277 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2278 desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2279
2280 /* actual transmit length (XXX why +10?) */
2281 pktlen = sizeof (struct zyd_tx_desc) + 10;
2282 if (sc->mac_rev == ZYD_ZD1211)
2283 pktlen += totlen;
2284 desc->pktlen = htole16(pktlen);
2285
2286 desc->plcp_length = (16 * totlen + rate - 1) / rate;
2287 desc->plcp_service = 0;
2288 if (rate == 22) {
2289 const int remainder = (16 * totlen) % 22;
2290 if (remainder != 0 && remainder < 7)
2291 desc->plcp_service |= ZYD_PLCP_LENGEXT;
2292 }
2293
2294 #if NBPFILTER > 0
2295 if (sc->sc_drvbpf != NULL) {
2296 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2297
2298 tap->wt_flags = 0;
2299 tap->wt_rate = rate;
2300 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2301 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2302
2303 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
2304 }
2305 #endif
2306
2307 m_copydata(m0, 0, m0->m_pkthdr.len,
2308 data->buf + sizeof (struct zyd_tx_desc));
2309
2310 DPRINTFN(10, ("%s: sending data frame len=%zu rate=%u xferlen=%u\n",
2311 device_xname(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen));
2312
2313 m_freem(m0); /* mbuf no longer needed */
2314
2315 usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BOUT], data,
2316 data->buf, xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
2317 ZYD_TX_TIMEOUT, zyd_txeof);
2318 error = usbd_transfer(data->xfer);
2319 if (error != USBD_IN_PROGRESS && error != 0) {
2320 ifp->if_oerrors++;
2321 return EIO;
2322 }
2323 sc->tx_queued++;
2324
2325 return 0;
2326 }
2327
2328 Static void
2329 zyd_start(struct ifnet *ifp)
2330 {
2331 struct zyd_softc *sc = ifp->if_softc;
2332 struct ieee80211com *ic = &sc->sc_ic;
2333 struct ether_header *eh;
2334 struct ieee80211_node *ni;
2335 struct mbuf *m0;
2336
2337 for (;;) {
2338 IF_POLL(&ic->ic_mgtq, m0);
2339 if (m0 != NULL) {
2340 if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
2341 ifp->if_flags |= IFF_OACTIVE;
2342 break;
2343 }
2344 IF_DEQUEUE(&ic->ic_mgtq, m0);
2345
2346 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
2347 m0->m_pkthdr.rcvif = NULL;
2348 #if NBPFILTER > 0
2349 if (ic->ic_rawbpf != NULL)
2350 bpf_mtap(ic->ic_rawbpf, m0);
2351 #endif
2352 if (zyd_tx_mgt(sc, m0, ni) != 0)
2353 break;
2354 } else {
2355 if (ic->ic_state != IEEE80211_S_RUN)
2356 break;
2357 IFQ_POLL(&ifp->if_snd, m0);
2358 if (m0 == NULL)
2359 break;
2360 if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
2361 ifp->if_flags |= IFF_OACTIVE;
2362 break;
2363 }
2364 IFQ_DEQUEUE(&ifp->if_snd, m0);
2365
2366 if (m0->m_len < sizeof(struct ether_header) &&
2367 !(m0 = m_pullup(m0, sizeof(struct ether_header))))
2368 continue;
2369
2370 eh = mtod(m0, struct ether_header *);
2371 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2372 if (ni == NULL) {
2373 m_freem(m0);
2374 continue;
2375 }
2376 #if NBPFILTER > 0
2377 if (ifp->if_bpf != NULL)
2378 bpf_mtap(ifp->if_bpf, m0);
2379 #endif
2380 if ((m0 = ieee80211_encap(ic, m0, ni)) == NULL) {
2381 ieee80211_free_node(ni);
2382 ifp->if_oerrors++;
2383 continue;
2384 }
2385 #if NBPFILTER > 0
2386 if (ic->ic_rawbpf != NULL)
2387 bpf_mtap(ic->ic_rawbpf, m0);
2388 #endif
2389 if (zyd_tx_data(sc, m0, ni) != 0) {
2390 ieee80211_free_node(ni);
2391 ifp->if_oerrors++;
2392 break;
2393 }
2394 }
2395
2396 sc->tx_timer = 5;
2397 ifp->if_timer = 1;
2398 }
2399 }
2400
2401 Static void
2402 zyd_watchdog(struct ifnet *ifp)
2403 {
2404 struct zyd_softc *sc = ifp->if_softc;
2405 struct ieee80211com *ic = &sc->sc_ic;
2406
2407 ifp->if_timer = 0;
2408
2409 if (sc->tx_timer > 0) {
2410 if (--sc->tx_timer == 0) {
2411 printf("%s: device timeout\n", device_xname(sc->sc_dev));
2412 /* zyd_init(ifp); XXX needs a process context ? */
2413 ifp->if_oerrors++;
2414 return;
2415 }
2416 ifp->if_timer = 1;
2417 }
2418
2419 ieee80211_watchdog(ic);
2420 }
2421
2422 Static int
2423 zyd_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2424 {
2425 struct zyd_softc *sc = ifp->if_softc;
2426 struct ieee80211com *ic = &sc->sc_ic;
2427 int s, error = 0;
2428
2429 s = splnet();
2430
2431 switch (cmd) {
2432 case SIOCSIFFLAGS:
2433 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
2434 break;
2435 /* XXX re-use ether_ioctl() */
2436 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
2437 case IFF_UP:
2438 zyd_init(ifp);
2439 break;
2440 case IFF_RUNNING:
2441 zyd_stop(ifp, 1);
2442 break;
2443 default:
2444 break;
2445 }
2446 break;
2447
2448 default:
2449 if (!sc->attached)
2450 error = ENXIO;
2451 else
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