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