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 $ */
6 * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr>
7 * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de>
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.
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.
23 * ZyDAS ZD1211/ZD1211B USB WLAN driver.
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/sockio.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>
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>
46 #include <machine/bus.h>
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>
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>
62 #include <dev/usb/usb_ethersubr.h>
64 #include <dev/mii/mii.h>
65 #include <dev/mii/miivar.h>
67 #include <dev/usb/if_zydreg.h>
68 #include <dev/usb/if_zydfw.h>
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)
80 #define DPRINTFN(n, x)
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;
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 {
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, GWUS54GD),
103 ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
104 ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
105 ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
106 ZYD_ZD1211_DEV(SAGEM, XG760A),
107 ZYD_ZD1211_DEV(SENAO, NUB8301),
108 ZYD_ZD1211_DEV(SITECOMEU, WL113),
109 ZYD_ZD1211_DEV(SWEEX, ZD1211),
110 ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
111 ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
112 ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
113 ZYD_ZD1211_DEV(TWINMOS, G240),
114 ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
115 ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
116 ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
117 ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
118 ZYD_ZD1211_DEV(ZCOM, ZD1211),
119 ZYD_ZD1211_DEV(ZYDAS, ZD1211),
120 ZYD_ZD1211_DEV(ZYXEL, AG225H),
121 ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
122 ZYD_ZD1211_DEV(ZYXEL, G200V2),
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(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),
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),
149 #define zyd_lookup(v, p) \
150 ((const struct zyd_type *)usb_lookup(zyd_devs, v, p))
152 static device_probe_t zyd_match;
153 static device_attach_t zyd_attach;
154 static device_detach_t zyd_detach;
156 static int zyd_attachhook(struct zyd_softc *);
157 static int zyd_complete_attach(struct zyd_softc *);
158 static int zyd_open_pipes(struct zyd_softc *);
159 static void zyd_close_pipes(struct zyd_softc *);
160 static int zyd_alloc_tx_list(struct zyd_softc *);
161 static void zyd_free_tx_list(struct zyd_softc *);
162 static int zyd_alloc_rx_list(struct zyd_softc *);
163 static void zyd_free_rx_list(struct zyd_softc *);
164 static struct ieee80211_node *zyd_node_alloc(struct ieee80211_node_table *);
165 static int zyd_media_change(struct ifnet *);
166 static void zyd_task(void *);
167 static int zyd_newstate(struct ieee80211com *, enum ieee80211_state, int);
168 static int zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
170 static int zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
171 static int zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
172 static int zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
173 static int zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
174 static int zyd_rfwrite(struct zyd_softc *, uint32_t);
175 static void zyd_lock_phy(struct zyd_softc *);
176 static void zyd_unlock_phy(struct zyd_softc *);
177 static int zyd_rfmd_init(struct zyd_rf *);
178 static int zyd_rfmd_switch_radio(struct zyd_rf *, int);
179 static int zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
180 static int zyd_al2230_init(struct zyd_rf *);
181 static int zyd_al2230_switch_radio(struct zyd_rf *, int);
182 static int zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
183 static int zyd_al2230_init_b(struct zyd_rf *);
184 static int zyd_al7230B_init(struct zyd_rf *);
185 static int zyd_al7230B_switch_radio(struct zyd_rf *, int);
186 static int zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
187 static int zyd_al2210_init(struct zyd_rf *);
188 static int zyd_al2210_switch_radio(struct zyd_rf *, int);
189 static int zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
190 static int zyd_gct_init(struct zyd_rf *);
191 static int zyd_gct_switch_radio(struct zyd_rf *, int);
192 static int zyd_gct_set_channel(struct zyd_rf *, uint8_t);
193 static int zyd_maxim_init(struct zyd_rf *);
194 static int zyd_maxim_switch_radio(struct zyd_rf *, int);
195 static int zyd_maxim_set_channel(struct zyd_rf *, uint8_t);
196 static int zyd_maxim2_init(struct zyd_rf *);
197 static int zyd_maxim2_switch_radio(struct zyd_rf *, int);
198 static int zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
199 static int zyd_rf_attach(struct zyd_softc *, uint8_t);
200 static const char *zyd_rf_name(uint8_t);
201 static int zyd_hw_init(struct zyd_softc *);
202 static int zyd_read_eeprom(struct zyd_softc *);
203 static int zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
204 static int zyd_set_bssid(struct zyd_softc *, const uint8_t *);
205 static int zyd_switch_radio(struct zyd_softc *, int);
206 static void zyd_set_led(struct zyd_softc *, int, int);
207 static void zyd_set_multi(struct zyd_softc *);
208 static int zyd_set_rxfilter(struct zyd_softc *);
209 static void zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
210 static int zyd_set_beacon_interval(struct zyd_softc *, int);
211 static uint8_t zyd_plcp_signal(int);
212 static void zyd_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
213 static void zyd_rx_data(struct zyd_softc *, const uint8_t *, uint16_t);
214 static void zyd_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
215 static void zyd_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
216 static int zyd_tx_mgt(struct zyd_softc *, struct mbuf *,
217 struct ieee80211_node *);
218 static int zyd_tx_data(struct zyd_softc *, struct mbuf *,
219 struct ieee80211_node *);
220 static void zyd_start(struct ifnet *);
221 static void zyd_watchdog(void *);
222 static int zyd_ioctl(struct ifnet *, u_long, caddr_t);
223 static void zyd_init(void *);
224 static void zyd_stop(struct zyd_softc *, int);
225 static int zyd_loadfirmware(struct zyd_softc *, u_char *, size_t);
226 static void zyd_iter_func(void *, struct ieee80211_node *);
227 static void zyd_amrr_timeout(void *);
228 static void zyd_newassoc(struct ieee80211_node *, int);
229 static void zyd_scantask(void *);
230 static void zyd_scan_start(struct ieee80211com *);
231 static void zyd_scan_end(struct ieee80211com *);
232 static void zyd_set_channel(struct ieee80211com *);
235 zyd_match(device_t dev)
237 struct usb_attach_arg *uaa = device_get_ivars(dev);
242 return (zyd_lookup(uaa->vendor, uaa->product) != NULL) ?
243 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
247 zyd_attachhook(struct zyd_softc *sc)
252 if (sc->mac_rev == ZYD_ZD1211) {
253 firmware = (u_char *)zd1211_firmware;
254 len = sizeof(zd1211_firmware);
256 firmware = (u_char *)zd1211b_firmware;
257 len = sizeof(zd1211b_firmware);
260 error = zyd_loadfirmware(sc, firmware, len);
262 device_printf(sc->sc_dev,
263 "could not load firmware (error=%d)\n", error);
267 sc->sc_flags |= ZD1211_FWLOADED;
269 /* complete the attach process */
270 return zyd_complete_attach(sc);
274 zyd_attach(device_t dev)
277 struct zyd_softc *sc = device_get_softc(dev);
278 struct usb_attach_arg *uaa = device_get_ivars(dev);
279 usb_device_descriptor_t* ddesc;
284 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
286 device_printf(dev, "can not if_alloc()\n");
290 sc->sc_udev = uaa->device;
292 sc->mac_rev = zyd_lookup(uaa->vendor, uaa->product)->rev;
294 ddesc = usbd_get_device_descriptor(sc->sc_udev);
295 if (UGETW(ddesc->bcdDevice) < 0x4330) {
296 device_printf(dev, "device version mismatch: 0x%x "
297 "(only >= 43.30 supported)\n",
298 UGETW(ddesc->bcdDevice));
303 if_initname(ifp, "zyd", device_get_unit(sc->sc_dev));
304 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
305 IFF_NEEDSGIANT; /* USB stack is still under Giant lock */
306 ifp->if_init = zyd_init;
307 ifp->if_ioctl = zyd_ioctl;
308 ifp->if_start = zyd_start;
309 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
310 IFQ_SET_READY(&ifp->if_snd);
312 STAILQ_INIT(&sc->sc_rqh);
314 error = zyd_attachhook(sc);
325 zyd_complete_attach(struct zyd_softc *sc)
327 struct ieee80211com *ic = &sc->sc_ic;
328 struct ifnet *ifp = sc->sc_ifp;
332 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
333 MTX_DEF | MTX_RECURSE);
335 usb_init_task(&sc->sc_scantask, zyd_scantask, sc);
336 usb_init_task(&sc->sc_task, zyd_task, sc);
338 callout_init(&sc->sc_amrr_ch, 0);
339 callout_init(&sc->sc_watchdog_ch, 0);
341 error = usbd_set_config_no(sc->sc_udev, ZYD_CONFIG_NO, 1);
343 device_printf(sc->sc_dev, "setting config no failed\n");
348 error = usbd_device2interface_handle(sc->sc_udev, ZYD_IFACE_INDEX,
351 device_printf(sc->sc_dev, "getting interface handle failed\n");
356 if ((error = zyd_open_pipes(sc)) != 0) {
357 device_printf(sc->sc_dev, "could not open pipes\n");
361 if ((error = zyd_read_eeprom(sc)) != 0) {
362 device_printf(sc->sc_dev, "could not read EEPROM\n");
366 if ((error = zyd_rf_attach(sc, sc->rf_rev)) != 0) {
367 device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
372 if ((error = zyd_hw_init(sc)) != 0) {
373 device_printf(sc->sc_dev, "hardware initialization failed\n");
377 device_printf(sc->sc_dev,
378 "HMAC ZD1211%s, FW %02x.%02x, RF %s, PA %x, address %s\n",
379 (sc->mac_rev == ZYD_ZD1211) ? "": "B",
380 sc->fw_rev >> 8, sc->fw_rev & 0xff, zyd_rf_name(sc->rf_rev),
381 sc->pa_rev, ether_sprintf(ic->ic_myaddr));
384 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
385 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
386 ic->ic_state = IEEE80211_S_INIT;
388 /* set device capabilities */
390 IEEE80211_C_MONITOR /* monitor mode */
391 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
392 | IEEE80211_C_SHSLOT /* short slot time supported */
393 | IEEE80211_C_BGSCAN /* capable of bg scanning */
394 | IEEE80211_C_WPA /* 802.11i */
398 setbit(&bands, IEEE80211_MODE_11B);
399 setbit(&bands, IEEE80211_MODE_11G);
400 ieee80211_init_channels(ic, 0, CTRY_DEFAULT, bands, 0, 1);
402 ieee80211_ifattach(ic);
403 ic->ic_node_alloc = zyd_node_alloc;
404 ic->ic_newassoc = zyd_newassoc;
406 /* enable s/w bmiss handling in sta mode */
407 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
408 ic->ic_scan_start = zyd_scan_start;
409 ic->ic_scan_end = zyd_scan_end;
410 ic->ic_set_channel = zyd_set_channel;
412 /* override state transition machine */
413 sc->sc_newstate = ic->ic_newstate;
414 ic->ic_newstate = zyd_newstate;
415 ieee80211_media_init(ic, zyd_media_change, ieee80211_media_status);
416 ieee80211_amrr_init(&sc->amrr, ic,
417 IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
418 IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD);
420 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
421 sizeof(struct ieee80211_frame) + sizeof(sc->sc_txtap),
424 sc->sc_rxtap_len = sizeof(sc->sc_rxtap);
425 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
426 sc->sc_rxtap.wr_ihdr.it_present = htole32(ZYD_RX_RADIOTAP_PRESENT);
428 sc->sc_txtap_len = sizeof(sc->sc_txtap);
429 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
430 sc->sc_txtap.wt_ihdr.it_present = htole32(ZYD_TX_RADIOTAP_PRESENT);
433 ieee80211_announce(ic);
435 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
440 mtx_destroy(&sc->sc_mtx);
446 zyd_detach(device_t dev)
448 struct zyd_softc *sc = device_get_softc(dev);
449 struct ieee80211com *ic = &sc->sc_ic;
450 struct ifnet *ifp = sc->sc_ifp;
452 if (!device_is_attached(dev))
455 /* protect a race when we have listeners related with the driver. */
456 ifp->if_flags &= ~IFF_UP;
459 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
460 usb_rem_task(sc->sc_udev, &sc->sc_task);
461 callout_stop(&sc->sc_amrr_ch);
462 callout_stop(&sc->sc_watchdog_ch);
467 ieee80211_ifdetach(ic);
470 mtx_destroy(&sc->sc_mtx);
472 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
478 zyd_open_pipes(struct zyd_softc *sc)
480 usb_endpoint_descriptor_t *edesc;
485 edesc = usbd_get_endpoint_descriptor(sc->sc_iface, 0x83);
489 isize = UGETW(edesc->wMaxPacketSize);
490 if (isize == 0) /* should not happen */
493 sc->ibuf = malloc(isize, M_USBDEV, M_NOWAIT);
494 if (sc->ibuf == NULL)
497 error = usbd_open_pipe_intr(sc->sc_iface, 0x83, USBD_SHORT_XFER_OK,
498 &sc->zyd_ep[ZYD_ENDPT_IIN], sc, sc->ibuf, isize, zyd_intr,
499 USBD_DEFAULT_INTERVAL);
501 device_printf(sc->sc_dev, "open rx intr pipe failed: %s\n",
506 /* interrupt out (not necessarily an interrupt pipe) */
507 error = usbd_open_pipe(sc->sc_iface, 0x04, USBD_EXCLUSIVE_USE,
508 &sc->zyd_ep[ZYD_ENDPT_IOUT]);
510 device_printf(sc->sc_dev, "open tx intr pipe failed: %s\n",
516 error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE,
517 &sc->zyd_ep[ZYD_ENDPT_BIN]);
519 device_printf(sc->sc_dev, "open rx pipe failed: %s\n",
525 error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE,
526 &sc->zyd_ep[ZYD_ENDPT_BOUT]);
528 device_printf(sc->sc_dev, "open tx pipe failed: %s\n",
535 fail: zyd_close_pipes(sc);
540 zyd_close_pipes(struct zyd_softc *sc)
544 for (i = 0; i < ZYD_ENDPT_CNT; i++) {
545 if (sc->zyd_ep[i] != NULL) {
546 usbd_abort_pipe(sc->zyd_ep[i]);
547 usbd_close_pipe(sc->zyd_ep[i]);
548 sc->zyd_ep[i] = NULL;
551 if (sc->ibuf != NULL) {
552 free(sc->ibuf, M_USBDEV);
558 zyd_alloc_tx_list(struct zyd_softc *sc)
564 for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
565 struct zyd_tx_data *data = &sc->tx_data[i];
567 data->sc = sc; /* backpointer for callbacks */
569 data->xfer = usbd_alloc_xfer(sc->sc_udev);
570 if (data->xfer == NULL) {
571 device_printf(sc->sc_dev,
572 "could not allocate tx xfer\n");
576 data->buf = usbd_alloc_buffer(data->xfer, ZYD_MAX_TXBUFSZ);
577 if (data->buf == NULL) {
578 device_printf(sc->sc_dev,
579 "could not allocate tx buffer\n");
584 /* clear Tx descriptor */
585 bzero(data->buf, sizeof(struct zyd_tx_desc));
589 fail: zyd_free_tx_list(sc);
594 zyd_free_tx_list(struct zyd_softc *sc)
598 for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
599 struct zyd_tx_data *data = &sc->tx_data[i];
601 if (data->xfer != NULL) {
602 usbd_free_xfer(data->xfer);
605 if (data->ni != NULL) {
606 ieee80211_free_node(data->ni);
613 zyd_alloc_rx_list(struct zyd_softc *sc)
617 for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
618 struct zyd_rx_data *data = &sc->rx_data[i];
620 data->sc = sc; /* backpointer for callbacks */
622 data->xfer = usbd_alloc_xfer(sc->sc_udev);
623 if (data->xfer == NULL) {
624 device_printf(sc->sc_dev,
625 "could not allocate rx xfer\n");
629 data->buf = usbd_alloc_buffer(data->xfer, ZYX_MAX_RXBUFSZ);
630 if (data->buf == NULL) {
631 device_printf(sc->sc_dev,
632 "could not allocate rx buffer\n");
639 fail: zyd_free_rx_list(sc);
644 zyd_free_rx_list(struct zyd_softc *sc)
648 for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
649 struct zyd_rx_data *data = &sc->rx_data[i];
651 if (data->xfer != NULL) {
652 usbd_free_xfer(data->xfer);
659 static struct ieee80211_node *
660 zyd_node_alloc(struct ieee80211_node_table *nt __unused)
664 zn = malloc(sizeof(struct zyd_node), M_80211_NODE, M_NOWAIT | M_ZERO);
665 return zn != NULL ? &zn->ni : NULL;
669 zyd_media_change(struct ifnet *ifp)
671 struct zyd_softc *sc = ifp->if_softc;
674 error = ieee80211_media_change(ifp);
675 if (error != ENETRESET)
678 if ((ifp->if_flags & IFF_UP) == IFF_UP &&
679 (ifp->if_drv_flags & IFF_DRV_RUNNING) == IFF_DRV_RUNNING)
688 struct zyd_softc *sc = arg;
689 struct ieee80211com *ic = &sc->sc_ic;
690 enum ieee80211_state ostate;
692 ostate = ic->ic_state;
694 switch (sc->sc_state) {
695 case IEEE80211_S_RUN:
697 struct ieee80211_node *ni = ic->ic_bss;
699 zyd_set_chan(sc, ic->ic_curchan);
701 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
702 /* turn link LED on */
703 zyd_set_led(sc, ZYD_LED1, 1);
705 /* make data LED blink upon Tx */
706 zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 1);
708 zyd_set_bssid(sc, ni->ni_bssid);
711 if (ic->ic_opmode == IEEE80211_M_STA) {
712 /* fake a join to init the tx rate */
716 /* start automatic rate control timer */
717 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
718 callout_reset(&sc->sc_amrr_ch, hz,
719 zyd_amrr_timeout, sc);
727 sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
731 zyd_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
733 struct zyd_softc *sc = ic->ic_ifp->if_softc;
735 usb_rem_task(sc->sc_udev, &sc->sc_task);
736 callout_stop(&sc->sc_amrr_ch);
738 /* do it in a process context */
739 sc->sc_state = nstate;
742 if (nstate == IEEE80211_S_INIT)
743 sc->sc_newstate(ic, nstate, arg);
745 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
751 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
752 void *odata, int olen, u_int flags)
754 usbd_xfer_handle xfer;
760 if ((xfer = usbd_alloc_xfer(sc->sc_udev)) == NULL)
763 cmd.code = htole16(code);
764 bcopy(idata, cmd.data, ilen);
766 xferflags = USBD_FORCE_SHORT_XFER;
767 if (!(flags & ZYD_CMD_FLAG_READ))
768 xferflags |= USBD_SYNCHRONOUS;
772 rq.len = olen / sizeof(struct zyd_pair);
773 STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
776 usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_IOUT], 0, &cmd,
777 sizeof(uint16_t) + ilen, xferflags, ZYD_INTR_TIMEOUT, NULL);
778 error = usbd_transfer(xfer);
779 if (error != USBD_IN_PROGRESS && error != 0) {
780 device_printf(sc->sc_dev, "could not send command (error=%s)\n",
782 (void)usbd_free_xfer(xfer);
785 if (!(flags & ZYD_CMD_FLAG_READ)) {
786 (void)usbd_free_xfer(xfer);
787 return 0; /* write: don't wait for reply */
789 /* wait at most one second for command reply */
790 error = tsleep(odata, PCATCH, "zydcmd", hz);
791 if (error == EWOULDBLOCK)
792 device_printf(sc->sc_dev, "zyd_read sleep timeout\n");
793 STAILQ_REMOVE(&sc->sc_rqh, &rq, rq, rq);
795 (void)usbd_free_xfer(xfer);
800 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
806 error = zyd_cmd(sc, ZYD_CMD_IORD, ®, sizeof(reg), &tmp, sizeof(tmp),
809 *val = le16toh(tmp.val);
814 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
816 struct zyd_pair tmp[2];
820 regs[0] = htole16(ZYD_REG32_HI(reg));
821 regs[1] = htole16(ZYD_REG32_LO(reg));
822 error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
825 *val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
830 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
832 struct zyd_pair pair;
834 pair.reg = htole16(reg);
835 pair.val = htole16(val);
837 return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
841 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
843 struct zyd_pair pair[2];
845 pair[0].reg = htole16(ZYD_REG32_HI(reg));
846 pair[0].val = htole16(val >> 16);
847 pair[1].reg = htole16(ZYD_REG32_LO(reg));
848 pair[1].val = htole16(val & 0xffff);
850 return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
854 zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
856 struct zyd_rf *rf = &sc->sc_rf;
857 struct zyd_rfwrite req;
861 (void)zyd_read16(sc, ZYD_CR203, &cr203);
862 cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
864 req.code = htole16(2);
865 req.width = htole16(rf->width);
866 for (i = 0; i < rf->width; i++) {
867 req.bit[i] = htole16(cr203);
868 if (val & (1 << (rf->width - 1 - i)))
869 req.bit[i] |= htole16(ZYD_RF_DATA);
871 return zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
875 zyd_lock_phy(struct zyd_softc *sc)
879 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
880 tmp &= ~ZYD_UNLOCK_PHY_REGS;
881 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
885 zyd_unlock_phy(struct zyd_softc *sc)
889 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
890 tmp |= ZYD_UNLOCK_PHY_REGS;
891 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
898 zyd_rfmd_init(struct zyd_rf *rf)
900 #define N(a) (sizeof(a) / sizeof((a)[0]))
901 struct zyd_softc *sc = rf->rf_sc;
902 static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
903 static const uint32_t rfini[] = ZYD_RFMD_RF;
906 /* init RF-dependent PHY registers */
907 for (i = 0; i < N(phyini); i++) {
908 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
913 /* init RFMD radio */
914 for (i = 0; i < N(rfini); i++) {
915 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
923 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
925 struct zyd_softc *sc = rf->rf_sc;
927 (void)zyd_write16(sc, ZYD_CR10, on ? 0x89 : 0x15);
928 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x81);
934 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
936 struct zyd_softc *sc = rf->rf_sc;
937 static const struct {
939 } rfprog[] = ZYD_RFMD_CHANTABLE;
941 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
942 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
951 zyd_al2230_init(struct zyd_rf *rf)
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_AL2230_PHY;
956 static const uint32_t rfini[] = ZYD_AL2230_RF;
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);
966 /* init AL2230 radio */
967 for (i = 0; i < N(rfini); i++) {
968 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
976 zyd_al2230_init_b(struct zyd_rf *rf)
978 #define N(a) (sizeof(a) / sizeof((a)[0]))
979 struct zyd_softc *sc = rf->rf_sc;
980 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
981 static const uint32_t rfini[] = ZYD_AL2230_RF_B;
984 /* init RF-dependent PHY registers */
985 for (i = 0; i < N(phyini); i++) {
986 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
991 /* init AL2230 radio */
992 for (i = 0; i < N(rfini); i++) {
993 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1001 zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1003 struct zyd_softc *sc = rf->rf_sc;
1004 int on251 = (sc->mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f;
1006 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04);
1007 (void)zyd_write16(sc, ZYD_CR251, on ? on251 : 0x2f);
1013 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1015 struct zyd_softc *sc = rf->rf_sc;
1016 static const struct {
1017 uint32_t r1, r2, r3;
1018 } rfprog[] = ZYD_AL2230_CHANTABLE;
1020 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1021 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1022 (void)zyd_rfwrite(sc, rfprog[chan - 1].r3);
1024 (void)zyd_write16(sc, ZYD_CR138, 0x28);
1025 (void)zyd_write16(sc, ZYD_CR203, 0x06);
1031 * AL7230B RF methods.
1034 zyd_al7230B_init(struct zyd_rf *rf)
1036 #define N(a) (sizeof(a) / sizeof((a)[0]))
1037 struct zyd_softc *sc = rf->rf_sc;
1038 static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1039 static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1040 static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1041 static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1042 static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1045 /* for AL7230B, PHY and RF need to be initialized in "phases" */
1047 /* init RF-dependent PHY registers, part one */
1048 for (i = 0; i < N(phyini_1); i++) {
1049 error = zyd_write16(sc, phyini_1[i].reg, phyini_1[i].val);
1053 /* init AL7230B radio, part one */
1054 for (i = 0; i < N(rfini_1); i++) {
1055 if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1058 /* init RF-dependent PHY registers, part two */
1059 for (i = 0; i < N(phyini_2); i++) {
1060 error = zyd_write16(sc, phyini_2[i].reg, phyini_2[i].val);
1064 /* init AL7230B radio, part two */
1065 for (i = 0; i < N(rfini_2); i++) {
1066 if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1069 /* init RF-dependent PHY registers, part three */
1070 for (i = 0; i < N(phyini_3); i++) {
1071 error = zyd_write16(sc, phyini_3[i].reg, phyini_3[i].val);
1081 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1083 struct zyd_softc *sc = rf->rf_sc;
1085 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04);
1086 (void)zyd_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1092 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1094 #define N(a) (sizeof(a) / sizeof((a)[0]))
1095 struct zyd_softc *sc = rf->rf_sc;
1096 static const struct {
1098 } rfprog[] = ZYD_AL7230B_CHANTABLE;
1099 static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1102 (void)zyd_write16(sc, ZYD_CR240, 0x57);
1103 (void)zyd_write16(sc, ZYD_CR251, 0x2f);
1105 for (i = 0; i < N(rfsc); i++) {
1106 if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1110 (void)zyd_write16(sc, ZYD_CR128, 0x14);
1111 (void)zyd_write16(sc, ZYD_CR129, 0x12);
1112 (void)zyd_write16(sc, ZYD_CR130, 0x10);
1113 (void)zyd_write16(sc, ZYD_CR38, 0x38);
1114 (void)zyd_write16(sc, ZYD_CR136, 0xdf);
1116 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1117 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1118 (void)zyd_rfwrite(sc, 0x3c9000);
1120 (void)zyd_write16(sc, ZYD_CR251, 0x3f);
1121 (void)zyd_write16(sc, ZYD_CR203, 0x06);
1122 (void)zyd_write16(sc, ZYD_CR240, 0x08);
1129 * AL2210 RF methods.
1132 zyd_al2210_init(struct zyd_rf *rf)
1134 #define N(a) (sizeof(a) / sizeof((a)[0]))
1135 struct zyd_softc *sc = rf->rf_sc;
1136 static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1137 static const uint32_t rfini[] = ZYD_AL2210_RF;
1141 (void)zyd_write32(sc, ZYD_CR18, 2);
1143 /* init RF-dependent PHY registers */
1144 for (i = 0; i < N(phyini); i++) {
1145 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1149 /* init AL2210 radio */
1150 for (i = 0; i < N(rfini); i++) {
1151 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1154 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1155 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1156 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1157 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1158 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1159 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1160 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1161 (void)zyd_write32(sc, ZYD_CR18, 3);
1168 zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1170 /* vendor driver does nothing for this RF chip */
1176 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1178 struct zyd_softc *sc = rf->rf_sc;
1179 static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1182 (void)zyd_write32(sc, ZYD_CR18, 2);
1183 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1184 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1185 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1186 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1187 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1189 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1190 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1192 /* actually set the channel */
1193 (void)zyd_rfwrite(sc, rfprog[chan - 1]);
1195 (void)zyd_write32(sc, ZYD_CR18, 3);
1204 zyd_gct_init(struct zyd_rf *rf)
1206 #define N(a) (sizeof(a) / sizeof((a)[0]))
1207 struct zyd_softc *sc = rf->rf_sc;
1208 static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1209 static const uint32_t rfini[] = ZYD_GCT_RF;
1212 /* init RF-dependent PHY registers */
1213 for (i = 0; i < N(phyini); i++) {
1214 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1218 /* init cgt radio */
1219 for (i = 0; i < N(rfini); i++) {
1220 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1228 zyd_gct_switch_radio(struct zyd_rf *rf, int on)
1230 /* vendor driver does nothing for this RF chip */
1236 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1238 struct zyd_softc *sc = rf->rf_sc;
1239 static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE;
1241 (void)zyd_rfwrite(sc, 0x1c0000);
1242 (void)zyd_rfwrite(sc, rfprog[chan - 1]);
1243 (void)zyd_rfwrite(sc, 0x1c0008);
1252 zyd_maxim_init(struct zyd_rf *rf)
1254 #define N(a) (sizeof(a) / sizeof((a)[0]))
1255 struct zyd_softc *sc = rf->rf_sc;
1256 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1257 static const uint32_t rfini[] = ZYD_MAXIM_RF;
1261 /* init RF-dependent PHY registers */
1262 for (i = 0; i < N(phyini); i++) {
1263 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1267 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1268 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1270 /* init maxim radio */
1271 for (i = 0; i < N(rfini); i++) {
1272 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1275 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1276 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1283 zyd_maxim_switch_radio(struct zyd_rf *rf, int on)
1285 /* vendor driver does nothing for this RF chip */
1291 zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan)
1293 #define N(a) (sizeof(a) / sizeof((a)[0]))
1294 struct zyd_softc *sc = rf->rf_sc;
1295 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1296 static const uint32_t rfini[] = ZYD_MAXIM_RF;
1297 static const struct {
1299 } rfprog[] = ZYD_MAXIM_CHANTABLE;
1304 * Do the same as we do when initializing it, except for the channel
1305 * values coming from the two channel tables.
1308 /* init RF-dependent PHY registers */
1309 for (i = 0; i < N(phyini); i++) {
1310 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1314 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1315 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1317 /* first two values taken from the chantables */
1318 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1319 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1321 /* init maxim radio - skipping the two first values */
1322 for (i = 2; i < N(rfini); i++) {
1323 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1326 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1327 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1334 * Maxim2 RF methods.
1337 zyd_maxim2_init(struct zyd_rf *rf)
1339 #define N(a) (sizeof(a) / sizeof((a)[0]))
1340 struct zyd_softc *sc = rf->rf_sc;
1341 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1342 static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1346 /* init RF-dependent PHY registers */
1347 for (i = 0; i < N(phyini); i++) {
1348 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1352 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1353 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1355 /* init maxim2 radio */
1356 for (i = 0; i < N(rfini); i++) {
1357 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1360 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1361 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1368 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1370 /* vendor driver does nothing for this RF chip */
1376 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1378 #define N(a) (sizeof(a) / sizeof((a)[0]))
1379 struct zyd_softc *sc = rf->rf_sc;
1380 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1381 static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1382 static const struct {
1384 } rfprog[] = ZYD_MAXIM2_CHANTABLE;
1389 * Do the same as we do when initializing it, except for the channel
1390 * values coming from the two channel tables.
1393 /* init RF-dependent PHY registers */
1394 for (i = 0; i < N(phyini); i++) {
1395 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1399 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1400 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1402 /* first two values taken from the chantables */
1403 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1404 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1406 /* init maxim2 radio - skipping the two first values */
1407 for (i = 2; i < N(rfini); i++) {
1408 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1411 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1412 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1419 zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1421 struct zyd_rf *rf = &sc->sc_rf;
1427 rf->init = zyd_rfmd_init;
1428 rf->switch_radio = zyd_rfmd_switch_radio;
1429 rf->set_channel = zyd_rfmd_set_channel;
1430 rf->width = 24; /* 24-bit RF values */
1433 if (sc->mac_rev == ZYD_ZD1211B)
1434 rf->init = zyd_al2230_init_b;
1436 rf->init = zyd_al2230_init;
1437 rf->switch_radio = zyd_al2230_switch_radio;
1438 rf->set_channel = zyd_al2230_set_channel;
1439 rf->width = 24; /* 24-bit RF values */
1441 case ZYD_RF_AL7230B:
1442 rf->init = zyd_al7230B_init;
1443 rf->switch_radio = zyd_al7230B_switch_radio;
1444 rf->set_channel = zyd_al7230B_set_channel;
1445 rf->width = 24; /* 24-bit RF values */
1448 rf->init = zyd_al2210_init;
1449 rf->switch_radio = zyd_al2210_switch_radio;
1450 rf->set_channel = zyd_al2210_set_channel;
1451 rf->width = 24; /* 24-bit RF values */
1454 rf->init = zyd_gct_init;
1455 rf->switch_radio = zyd_gct_switch_radio;
1456 rf->set_channel = zyd_gct_set_channel;
1457 rf->width = 21; /* 21-bit RF values */
1459 case ZYD_RF_MAXIM_NEW:
1460 rf->init = zyd_maxim_init;
1461 rf->switch_radio = zyd_maxim_switch_radio;
1462 rf->set_channel = zyd_maxim_set_channel;
1463 rf->width = 18; /* 18-bit RF values */
1465 case ZYD_RF_MAXIM_NEW2:
1466 rf->init = zyd_maxim2_init;
1467 rf->switch_radio = zyd_maxim2_switch_radio;
1468 rf->set_channel = zyd_maxim2_set_channel;
1469 rf->width = 18; /* 18-bit RF values */
1472 device_printf(sc->sc_dev,
1473 "sorry, radio \"%s\" is not supported yet\n",
1481 zyd_rf_name(uint8_t type)
1483 static const char * const zyd_rfs[] = {
1484 "unknown", "unknown", "UW2451", "UCHIP", "AL2230",
1485 "AL7230B", "THETA", "AL2210", "MAXIM_NEW", "GCT",
1486 "PV2000", "RALINK", "INTERSIL", "RFMD", "MAXIM_NEW2",
1490 return zyd_rfs[(type > 15) ? 0 : type];
1494 zyd_hw_init(struct zyd_softc *sc)
1496 struct zyd_rf *rf = &sc->sc_rf;
1497 const struct zyd_phy_pair *phyp;
1501 /* specify that the plug and play is finished */
1502 (void)zyd_write32(sc, ZYD_MAC_AFTER_PNP, 1);
1504 (void)zyd_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->fwbase);
1505 DPRINTF(("firmware base address=0x%04x\n", sc->fwbase));
1507 /* retrieve firmware revision number */
1508 (void)zyd_read16(sc, sc->fwbase + ZYD_FW_FIRMWARE_REV, &sc->fw_rev);
1510 (void)zyd_write32(sc, ZYD_CR_GPI_EN, 0);
1511 (void)zyd_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1513 /* disable interrupts */
1514 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
1518 phyp = (sc->mac_rev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1519 for (; phyp->reg != 0; phyp++) {
1520 if ((error = zyd_write16(sc, phyp->reg, phyp->val)) != 0)
1523 if (sc->fix_cr157) {
1524 if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
1525 (void)zyd_write32(sc, ZYD_CR157, tmp >> 8);
1530 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1531 zyd_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1533 if (sc->mac_rev == ZYD_ZD1211) {
1534 zyd_write32(sc, ZYD_MAC_RETRY, 0x00000002);
1536 zyd_write32(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1537 zyd_write32(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1538 zyd_write32(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1539 zyd_write32(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1540 zyd_write32(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1541 zyd_write32(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1542 zyd_write32(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1543 zyd_write32(sc, ZYD_MACB_TXOP, 0x01800824);
1546 zyd_write32(sc, ZYD_MAC_SNIFFER, 0x00000000);
1547 zyd_write32(sc, ZYD_MAC_RXFILTER, 0x00000000);
1548 zyd_write32(sc, ZYD_MAC_GHTBL, 0x00000000);
1549 zyd_write32(sc, ZYD_MAC_GHTBH, 0x80000000);
1550 zyd_write32(sc, ZYD_MAC_MISC, 0x000000a4);
1551 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1552 zyd_write32(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1553 zyd_write32(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1554 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1555 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1556 zyd_write32(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1557 zyd_write32(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0547c032);
1558 zyd_write32(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1559 zyd_write32(sc, ZYD_CR_PS_CTRL, 0x10000000);
1560 zyd_write32(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1561 zyd_write32(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1562 zyd_write32(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1566 error = (*rf->init)(rf);
1569 device_printf(sc->sc_dev,
1570 "radio initialization failed, error %d\n", error);
1574 /* init beacon interval to 100ms */
1575 if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1582 zyd_read_eeprom(struct zyd_softc *sc)
1584 struct ieee80211com *ic = &sc->sc_ic;
1589 /* read MAC address */
1590 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P1, &tmp);
1591 ic->ic_myaddr[0] = tmp & 0xff;
1592 ic->ic_myaddr[1] = tmp >> 8;
1593 ic->ic_myaddr[2] = tmp >> 16;
1594 ic->ic_myaddr[3] = tmp >> 24;
1595 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P2, &tmp);
1596 ic->ic_myaddr[4] = tmp & 0xff;
1597 ic->ic_myaddr[5] = tmp >> 8;
1599 (void)zyd_read32(sc, ZYD_EEPROM_POD, &tmp);
1600 sc->rf_rev = tmp & 0x0f;
1601 sc->fix_cr47 = (tmp >> 8 ) & 0x01;
1602 sc->fix_cr157 = (tmp >> 13) & 0x01;
1603 sc->pa_rev = (tmp >> 16) & 0x0f;
1605 /* read regulatory domain (currently unused) */
1606 (void)zyd_read32(sc, ZYD_EEPROM_SUBID, &tmp);
1607 sc->regdomain = tmp >> 16;
1608 DPRINTF(("regulatory domain %x\n", sc->regdomain));
1610 /* read Tx power calibration tables */
1611 for (i = 0; i < 7; i++) {
1612 (void)zyd_read16(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1613 sc->pwr_cal[i * 2] = val >> 8;
1614 sc->pwr_cal[i * 2 + 1] = val & 0xff;
1616 (void)zyd_read16(sc, ZYD_EEPROM_PWR_INT + i, &val);
1617 sc->pwr_int[i * 2] = val >> 8;
1618 sc->pwr_int[i * 2 + 1] = val & 0xff;
1620 (void)zyd_read16(sc, ZYD_EEPROM_36M_CAL + i, &val);
1621 sc->ofdm36_cal[i * 2] = val >> 8;
1622 sc->ofdm36_cal[i * 2 + 1] = val & 0xff;
1624 (void)zyd_read16(sc, ZYD_EEPROM_48M_CAL + i, &val);
1625 sc->ofdm48_cal[i * 2] = val >> 8;
1626 sc->ofdm48_cal[i * 2 + 1] = val & 0xff;
1628 (void)zyd_read16(sc, ZYD_EEPROM_54M_CAL + i, &val);
1629 sc->ofdm54_cal[i * 2] = val >> 8;
1630 sc->ofdm54_cal[i * 2 + 1] = val & 0xff;
1636 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1640 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1641 (void)zyd_write32(sc, ZYD_MAC_MACADRL, tmp);
1643 tmp = addr[5] << 8 | addr[4];
1644 (void)zyd_write32(sc, ZYD_MAC_MACADRH, tmp);
1650 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1654 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1655 (void)zyd_write32(sc, ZYD_MAC_BSSADRL, tmp);
1657 tmp = addr[5] << 8 | addr[4];
1658 (void)zyd_write32(sc, ZYD_MAC_BSSADRH, tmp);
1664 zyd_switch_radio(struct zyd_softc *sc, int on)
1666 struct zyd_rf *rf = &sc->sc_rf;
1670 error = (*rf->switch_radio)(rf, on);
1677 zyd_set_led(struct zyd_softc *sc, int which, int on)
1681 (void)zyd_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1685 (void)zyd_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1689 zyd_set_multi(struct zyd_softc *sc)
1691 struct ieee80211com *ic = &sc->sc_ic;
1692 struct ifnet *ifp = ic->ic_ifp;
1693 struct ifmultiaddr *ifma;
1697 if (!(ifp->if_flags & IFF_UP))
1703 if (ic->ic_opmode == IEEE80211_M_MONITOR ||
1704 (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) {
1709 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1710 if (ifma->ifma_addr->sa_family != AF_LINK)
1712 v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
1713 ifma->ifma_addr))[5] >> 2;
1717 high |= 1 << (v - 32);
1719 IF_ADDR_UNLOCK(ifp);
1722 /* reprogram multicast global hash table */
1723 zyd_write32(sc, ZYD_MAC_GHTBL, low);
1724 zyd_write32(sc, ZYD_MAC_GHTBH, high);
1728 zyd_set_rxfilter(struct zyd_softc *sc)
1732 switch (sc->sc_ic.ic_opmode) {
1733 case IEEE80211_M_STA:
1734 rxfilter = ZYD_FILTER_BSS;
1736 case IEEE80211_M_IBSS:
1737 case IEEE80211_M_HOSTAP:
1738 rxfilter = ZYD_FILTER_HOSTAP;
1740 case IEEE80211_M_MONITOR:
1741 rxfilter = ZYD_FILTER_MONITOR;
1744 /* should not get there */
1747 return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
1751 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
1753 struct ieee80211com *ic = &sc->sc_ic;
1754 struct zyd_rf *rf = &sc->sc_rf;
1758 chan = ieee80211_chan2ieee(ic, c);
1759 if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
1760 /* XXX should NEVER happen */
1761 device_printf(sc->sc_dev,
1762 "%s: invalid channel %x\n", __func__, chan);
1768 (*rf->set_channel)(rf, chan);
1770 /* update Tx power */
1771 (void)zyd_write16(sc, ZYD_CR31, sc->pwr_int[chan - 1]);
1773 if (sc->mac_rev == ZYD_ZD1211B) {
1774 (void)zyd_write16(sc, ZYD_CR67, sc->ofdm36_cal[chan - 1]);
1775 (void)zyd_write16(sc, ZYD_CR66, sc->ofdm48_cal[chan - 1]);
1776 (void)zyd_write16(sc, ZYD_CR65, sc->ofdm54_cal[chan - 1]);
1778 (void)zyd_write16(sc, ZYD_CR68, sc->pwr_cal[chan - 1]);
1780 (void)zyd_write16(sc, ZYD_CR69, 0x28);
1781 (void)zyd_write16(sc, ZYD_CR69, 0x2a);
1785 /* set CCK baseband gain from EEPROM */
1786 if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
1787 (void)zyd_write16(sc, ZYD_CR47, tmp & 0xff);
1790 (void)zyd_write32(sc, ZYD_CR_CONFIG_PHILIPS, 0);
1794 sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
1795 htole16(c->ic_freq);
1796 sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
1797 htole16(c->ic_flags);
1801 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
1803 /* XXX this is probably broken.. */
1804 (void)zyd_write32(sc, ZYD_CR_ATIM_WND_PERIOD, bintval - 2);
1805 (void)zyd_write32(sc, ZYD_CR_PRE_TBTT, bintval - 1);
1806 (void)zyd_write32(sc, ZYD_CR_BCN_INTERVAL, bintval);
1812 zyd_plcp_signal(int rate)
1815 /* CCK rates (returned values are device-dependent) */
1818 case 11: return 0x2;
1819 case 22: return 0x3;
1821 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1822 case 12: return 0xb;
1823 case 18: return 0xf;
1824 case 24: return 0xa;
1825 case 36: return 0xe;
1826 case 48: return 0x9;
1827 case 72: return 0xd;
1828 case 96: return 0x8;
1829 case 108: return 0xc;
1831 /* unsupported rates (should not get there) */
1832 default: return 0xff;
1837 zyd_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1839 struct zyd_softc *sc = (struct zyd_softc *)priv;
1840 struct zyd_cmd *cmd;
1843 if (status != USBD_NORMAL_COMPLETION) {
1844 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1847 if (status == USBD_STALLED) {
1848 usbd_clear_endpoint_stall_async(
1849 sc->zyd_ep[ZYD_ENDPT_IIN]);
1854 cmd = (struct zyd_cmd *)sc->ibuf;
1856 if (le16toh(cmd->code) == ZYD_NOTIF_RETRYSTATUS) {
1857 struct zyd_notif_retry *retry =
1858 (struct zyd_notif_retry *)cmd->data;
1859 struct ieee80211com *ic = &sc->sc_ic;
1860 struct ifnet *ifp = sc->sc_ifp;
1861 struct ieee80211_node *ni;
1863 DPRINTF(("retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
1864 le16toh(retry->rate), ether_sprintf(retry->macaddr),
1865 le16toh(retry->count) & 0xff, le16toh(retry->count)));
1868 * Find the node to which the packet was sent and update its
1869 * retry statistics. In BSS mode, this node is the AP we're
1870 * associated to so no lookup is actually needed.
1872 if (ic->ic_opmode != IEEE80211_M_STA) {
1873 ni = ieee80211_find_node(&ic->ic_sta, retry->macaddr);
1875 return; /* just ignore */
1879 ((struct zyd_node *)ni)->amn.amn_retrycnt++;
1881 if (le16toh(retry->count) & 0x100)
1882 ifp->if_oerrors++; /* too many retries */
1883 } else if (le16toh(cmd->code) == ZYD_NOTIF_IORD) {
1886 if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
1887 return; /* HMAC interrupt */
1889 usbd_get_xfer_status(xfer, NULL, NULL, &datalen, NULL);
1890 datalen -= sizeof(cmd->code);
1891 datalen -= 2; /* XXX: padding? */
1893 STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
1896 if (sizeof(struct zyd_pair) * rqp->len != datalen)
1898 for (i = 0; i < rqp->len; i++) {
1899 if (*(((const uint16_t *)rqp->idata) + i) !=
1900 (((struct zyd_pair *)cmd->data) + i)->reg)
1906 /* copy answer into caller-supplied buffer */
1907 bcopy(cmd->data, rqp->odata,
1908 sizeof(struct zyd_pair) * rqp->len);
1909 wakeup(rqp->odata); /* wakeup caller */
1913 return; /* unexpected IORD notification */
1915 device_printf(sc->sc_dev, "unknown notification %x\n",
1916 le16toh(cmd->code));
1920 static __inline uint8_t
1921 zyd_plcp2ieee(int signal, int isofdm)
1924 static const uint8_t ofdmrates[16] =
1925 { 0, 0, 0, 0, 0, 0, 0, 96, 48, 24, 12, 108, 72, 36, 18 };
1926 return ofdmrates[signal & 0xf];
1928 static const uint8_t cckrates[16] =
1929 { 0, 0, 0, 0, 4, 0, 0, 11, 0, 0, 2, 0, 0, 0, 22, 0 };
1930 return cckrates[signal & 0xf];
1935 zyd_rx_data(struct zyd_softc *sc, const uint8_t *buf, uint16_t len)
1937 struct ieee80211com *ic = &sc->sc_ic;
1938 struct ifnet *ifp = sc->sc_ifp;
1939 struct ieee80211_node *ni;
1940 const struct zyd_plcphdr *plcp;
1941 const struct zyd_rx_stat *stat;
1945 if (len < ZYD_MIN_FRAGSZ) {
1946 DPRINTF(("%s: frame too short (length=%d)\n",
1947 device_get_nameunit(sc->sc_dev), len));
1952 plcp = (const struct zyd_plcphdr *)buf;
1953 stat = (const struct zyd_rx_stat *)
1954 (buf + len - sizeof(struct zyd_rx_stat));
1956 if (stat->flags & ZYD_RX_ERROR) {
1957 DPRINTF(("%s: RX status indicated error (%x)\n",
1958 device_get_nameunit(sc->sc_dev), stat->flags));
1963 /* compute actual frame length */
1964 rlen = len - sizeof(struct zyd_plcphdr) -
1965 sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
1967 /* allocate a mbuf to store the frame */
1969 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1971 m = m_gethdr(M_DONTWAIT, MT_DATA);
1973 DPRINTF(("%s: could not allocate rx mbuf\n",
1974 device_get_nameunit(sc->sc_dev)));
1978 m->m_pkthdr.rcvif = ifp;
1979 m->m_pkthdr.len = m->m_len = rlen;
1980 bcopy((const uint8_t *)(plcp + 1), mtod(m, uint8_t *), rlen);
1982 if (bpf_peers_present(sc->sc_drvbpf)) {
1983 struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
1986 if (stat->flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
1987 tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
1988 /* XXX toss, no way to express errors */
1989 if (stat->flags & ZYD_RX_DECRYPTERR)
1990 tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
1992 zyd_plcp2ieee(plcp->signal, stat->flags & ZYD_RX_OFDM);
1993 tap->wr_antsignal = stat->rssi + -95;
1994 tap->wr_antnoise = -95; /* XXX */
1996 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1999 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
2000 ieee80211_input(ic, m, ni,
2001 stat->rssi > 63 ? 127 : 2 * stat->rssi, -95/*XXX*/, 0);
2003 /* node is no longer needed */
2004 ieee80211_free_node(ni);
2008 zyd_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
2010 struct zyd_rx_data *data = priv;
2011 struct zyd_softc *sc = data->sc;
2012 struct ifnet *ifp = sc->sc_ifp;
2013 const struct zyd_rx_desc *desc;
2016 if (status != USBD_NORMAL_COMPLETION) {
2017 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
2020 if (status == USBD_STALLED)
2021 usbd_clear_endpoint_stall(sc->zyd_ep[ZYD_ENDPT_BIN]);
2025 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
2027 if (len < ZYD_MIN_RXBUFSZ) {
2028 DPRINTFN(3, ("%s: xfer too short (length=%d)\n",
2029 device_get_nameunit(sc->sc_dev), len));
2030 ifp->if_ierrors++; /* XXX not really errors */
2034 desc = (const struct zyd_rx_desc *)
2035 (data->buf + len - sizeof(struct zyd_rx_desc));
2037 if (UGETW(desc->tag) == ZYD_TAG_MULTIFRAME) {
2038 const uint8_t *p = data->buf, *end = p + len;
2041 DPRINTFN(3, ("received multi-frame transfer\n"));
2043 for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2044 const uint16_t len16 = UGETW(desc->len[i]);
2046 if (len16 == 0 || p + len16 > end)
2049 zyd_rx_data(sc, p, len16);
2050 /* next frame is aligned on a 32-bit boundary */
2051 p += (len16 + 3) & ~3;
2054 DPRINTFN(3, ("received single-frame transfer\n"));
2056 zyd_rx_data(sc, data->buf, len);
2059 skip: /* setup a new transfer */
2060 usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_BIN], data, NULL,
2061 ZYX_MAX_RXBUFSZ, USBD_NO_COPY | USBD_SHORT_XFER_OK,
2062 USBD_NO_TIMEOUT, zyd_rxeof);
2063 (void)usbd_transfer(xfer);
2067 zyd_tx_mgt(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2069 struct ieee80211com *ic = &sc->sc_ic;
2070 struct ifnet *ifp = sc->sc_ifp;
2071 struct zyd_tx_desc *desc;
2072 struct zyd_tx_data *data;
2073 struct ieee80211_frame *wh;
2074 struct ieee80211_key *k;
2075 int xferlen, totlen, rate;
2079 data = &sc->tx_data[0];
2080 desc = (struct zyd_tx_desc *)data->buf;
2082 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
2084 wh = mtod(m0, struct ieee80211_frame *);
2086 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2087 k = ieee80211_crypto_encap(ic, ni, m0);
2097 wh = mtod(m0, struct ieee80211_frame *);
2099 xferlen = sizeof(struct zyd_tx_desc) + m0->m_pkthdr.len;
2100 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2102 /* fill Tx descriptor */
2103 desc->len = htole16(totlen);
2105 desc->flags = ZYD_TX_FLAG_BACKOFF;
2106 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2107 /* multicast frames are not sent at OFDM rates in 802.11b/g */
2108 if (totlen > ic->ic_rtsthreshold) {
2109 desc->flags |= ZYD_TX_FLAG_RTS;
2110 } else if (ZYD_RATE_IS_OFDM(rate) &&
2111 (ic->ic_flags & IEEE80211_F_USEPROT)) {
2112 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2113 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2114 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2115 desc->flags |= ZYD_TX_FLAG_RTS;
2118 desc->flags |= ZYD_TX_FLAG_MULTICAST;
2121 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2122 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2123 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2125 desc->phy = zyd_plcp_signal(rate);
2126 if (ZYD_RATE_IS_OFDM(rate)) {
2127 desc->phy |= ZYD_TX_PHY_OFDM;
2128 if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2129 desc->phy |= ZYD_TX_PHY_5GHZ;
2130 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2131 desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2133 /* actual transmit length (XXX why +10?) */
2134 pktlen = sizeof(struct zyd_tx_desc) + 10;
2135 if (sc->mac_rev == ZYD_ZD1211)
2137 desc->pktlen = htole16(pktlen);
2139 desc->plcp_length = (16 * totlen + rate - 1) / rate;
2140 desc->plcp_service = 0;
2142 const int remainder = (16 * totlen) % 22;
2143 if (remainder != 0 && remainder < 7)
2144 desc->plcp_service |= ZYD_PLCP_LENGEXT;
2147 if (bpf_peers_present(sc->sc_drvbpf)) {
2148 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2151 tap->wt_rate = rate;
2153 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
2156 m_copydata(m0, 0, m0->m_pkthdr.len,
2157 data->buf + sizeof(struct zyd_tx_desc));
2159 DPRINTFN(10, ("%s: sending mgt frame len=%zu rate=%u xferlen=%u\n",
2160 device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2163 usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BOUT], data,
2164 data->buf, xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
2165 ZYD_TX_TIMEOUT, zyd_txeof);
2166 error = usbd_transfer(data->xfer);
2167 if (error != USBD_IN_PROGRESS && error != 0) {
2177 zyd_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
2179 struct zyd_tx_data *data = priv;
2180 struct zyd_softc *sc = data->sc;
2181 struct ifnet *ifp = sc->sc_ifp;
2182 struct ieee80211_node *ni;
2185 if (status != USBD_NORMAL_COMPLETION) {
2186 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
2189 device_printf(sc->sc_dev, "could not transmit buffer: %s\n",
2190 usbd_errstr(status));
2192 if (status == USBD_STALLED) {
2193 usbd_clear_endpoint_stall_async(
2194 sc->zyd_ep[ZYD_ENDPT_BOUT]);
2201 /* update rate control statistics */
2202 ((struct zyd_node *)ni)->amn.amn_txcnt++;
2205 * Do any tx complete callback. Note this must
2206 * be done before releasing the node reference.
2209 if (m != NULL && m->m_flags & M_TXCB) {
2210 ieee80211_process_callback(ni, m, 0); /* XXX status? */
2215 ieee80211_free_node(ni);
2222 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2227 zyd_tx_data(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2229 struct ieee80211com *ic = &sc->sc_ic;
2230 struct ifnet *ifp = sc->sc_ifp;
2231 struct zyd_tx_desc *desc;
2232 struct zyd_tx_data *data;
2233 struct ieee80211_frame *wh;
2234 struct ieee80211_key *k;
2235 int xferlen, totlen, rate;
2239 wh = mtod(m0, struct ieee80211_frame *);
2240 data = &sc->tx_data[0];
2241 desc = (struct zyd_tx_desc *)data->buf;
2243 desc->flags = ZYD_TX_FLAG_BACKOFF;
2244 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2245 rate = ic->ic_mcast_rate;
2246 desc->flags |= ZYD_TX_FLAG_MULTICAST;
2247 } else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
2248 rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate];
2250 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
2251 rate &= IEEE80211_RATE_VAL;
2253 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2254 k = ieee80211_crypto_encap(ic, ni, m0);
2260 /* packet header may have moved, reset our local pointer */
2261 wh = mtod(m0, struct ieee80211_frame *);
2267 xferlen = sizeof(struct zyd_tx_desc) + m0->m_pkthdr.len;
2268 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2270 /* fill Tx descriptor */
2271 desc->len = htole16(totlen);
2273 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2274 /* multicast frames are not sent at OFDM rates in 802.11b/g */
2275 if (totlen > ic->ic_rtsthreshold) {
2276 desc->flags |= ZYD_TX_FLAG_RTS;
2277 } else if (ZYD_RATE_IS_OFDM(rate) &&
2278 (ic->ic_flags & IEEE80211_F_USEPROT)) {
2279 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2280 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2281 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2282 desc->flags |= ZYD_TX_FLAG_RTS;
2287 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2288 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2289 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2291 desc->phy = zyd_plcp_signal(rate);
2292 if (ZYD_RATE_IS_OFDM(rate)) {
2293 desc->phy |= ZYD_TX_PHY_OFDM;
2294 if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2295 desc->phy |= ZYD_TX_PHY_5GHZ;
2296 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2297 desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2299 /* actual transmit length (XXX why +10?) */
2300 pktlen = sizeof(struct zyd_tx_desc) + 10;
2301 if (sc->mac_rev == ZYD_ZD1211)
2303 desc->pktlen = htole16(pktlen);
2305 desc->plcp_length = (16 * totlen + rate - 1) / rate;
2306 desc->plcp_service = 0;
2308 const int remainder = (16 * totlen) % 22;
2309 if (remainder != 0 && remainder < 7)
2310 desc->plcp_service |= ZYD_PLCP_LENGEXT;
2313 if (bpf_peers_present(sc->sc_drvbpf)) {
2314 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2317 tap->wt_rate = rate;
2318 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2319 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2321 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
2324 m_copydata(m0, 0, m0->m_pkthdr.len,
2325 data->buf + sizeof(struct zyd_tx_desc));
2327 DPRINTFN(10, ("%s: sending data frame len=%zu rate=%u xferlen=%u\n",
2328 device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2331 m_freem(m0); /* mbuf no longer needed */
2333 usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BOUT], data,
2334 data->buf, xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
2335 ZYD_TX_TIMEOUT, zyd_txeof);
2336 error = usbd_transfer(data->xfer);
2337 if (error != USBD_IN_PROGRESS && error != 0) {
2347 zyd_start(struct ifnet *ifp)
2349 struct zyd_softc *sc = ifp->if_softc;
2350 struct ieee80211com *ic = &sc->sc_ic;
2351 struct ether_header *eh;
2352 struct ieee80211_node *ni;
2356 IF_POLL(&ic->ic_mgtq, m0);
2358 if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
2359 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2362 IF_DEQUEUE(&ic->ic_mgtq, m0);
2364 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
2365 m0->m_pkthdr.rcvif = NULL;
2366 if (bpf_peers_present(ic->ic_rawbpf))
2367 bpf_mtap(ic->ic_rawbpf, m0);
2368 if (zyd_tx_mgt(sc, m0, ni) != 0)
2371 if (ic->ic_state != IEEE80211_S_RUN)
2373 IFQ_POLL(&ifp->if_snd, m0);
2376 if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
2377 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2380 IFQ_DEQUEUE(&ifp->if_snd, m0);
2382 * Cancel any background scan.
2384 if (ic->ic_flags & IEEE80211_F_SCAN)
2385 ieee80211_cancel_scan(ic);
2387 if (m0->m_len < sizeof(struct ether_header) &&
2388 !(m0 = m_pullup(m0, sizeof(struct ether_header))))
2391 eh = mtod(m0, struct ether_header *);
2392 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2397 if (bpf_peers_present(ifp->if_bpf))
2398 bpf_mtap(ifp->if_bpf, m0);
2399 if ((m0 = ieee80211_encap(ic, m0, ni)) == NULL) {
2400 ieee80211_free_node(ni);
2404 if (bpf_peers_present(ic->ic_rawbpf))
2405 bpf_mtap(ic->ic_rawbpf, m0);
2406 if (zyd_tx_data(sc, m0, ni) != 0) {
2407 ieee80211_free_node(ni);
2414 ic->ic_lastdata = ticks;
2415 callout_reset(&sc->sc_watchdog_ch, hz, zyd_watchdog, sc);
2420 zyd_watchdog(void *arg)
2422 struct zyd_softc *sc = arg;
2423 struct ieee80211com *ic = &sc->sc_ic;
2424 struct ifnet *ifp = ic->ic_ifp;
2426 if (sc->tx_timer > 0) {
2427 if (--sc->tx_timer == 0) {
2428 device_printf(sc->sc_dev, "device timeout\n");
2429 /* zyd_init(ifp); XXX needs a process context ? */
2433 callout_reset(&sc->sc_watchdog_ch, hz, zyd_watchdog, sc);
2438 zyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2440 struct zyd_softc *sc = ifp->if_softc;
2441 struct ieee80211com *ic = &sc->sc_ic;
2448 if (ifp->if_flags & IFF_UP) {
2449 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2450 if ((ifp->if_flags ^ sc->sc_if_flags) &
2451 (IFF_ALLMULTI | IFF_PROMISC))
2456 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2459 sc->sc_if_flags = ifp->if_flags;
2464 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2469 error = ieee80211_ioctl(ic, cmd, data);
2472 if (error == ENETRESET) {
2473 if ((ifp->if_flags & IFF_UP) == IFF_UP &&
2474 (ifp->if_drv_flags & IFF_DRV_RUNNING) == IFF_DRV_RUNNING)
2485 zyd_init(void *priv)
2487 struct zyd_softc *sc = priv;
2488 struct ieee80211com *ic = &sc->sc_ic;
2489 struct ifnet *ifp = ic->ic_ifp;
2494 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2495 DPRINTF(("setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
2496 error = zyd_set_macaddr(sc, ic->ic_myaddr);
2500 /* we'll do software WEP decryption for now */
2501 DPRINTF(("setting encryption type\n"));
2502 error = zyd_write32(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2506 /* promiscuous mode */
2507 (void)zyd_write32(sc, ZYD_MAC_SNIFFER,
2508 (ic->ic_opmode == IEEE80211_M_MONITOR) ? 1 : 0);
2510 /* multicast setup */
2511 (void)zyd_set_multi(sc);
2513 (void)zyd_set_rxfilter(sc);
2515 /* switch radio transmitter ON */
2516 (void)zyd_switch_radio(sc, 1);
2518 /* XXX wrong, can't set here */
2519 /* set basic rates */
2520 if (ic->ic_curmode == IEEE80211_MODE_11B)
2521 (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x0003);
2522 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2523 (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x1500);
2524 else /* assumes 802.11b/g */
2525 (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x000f);
2527 /* set mandatory rates */
2528 if (ic->ic_curmode == IEEE80211_MODE_11B)
2529 (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x000f);
2530 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2531 (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x1500);
2532 else /* assumes 802.11b/g */
2533 (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x150f);
2535 /* set default BSS channel */
2536 zyd_set_chan(sc, ic->ic_curchan);
2538 /* enable interrupts */
2539 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2542 * Allocate Tx and Rx xfer queues.
2544 if ((error = zyd_alloc_tx_list(sc)) != 0) {
2545 device_printf(sc->sc_dev, "could not allocate Tx list\n");
2548 if ((error = zyd_alloc_rx_list(sc)) != 0) {
2549 device_printf(sc->sc_dev, "could not allocate Rx list\n");
2554 * Start up the receive pipe.
2556 for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
2557 struct zyd_rx_data *data = &sc->rx_data[i];
2559 usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BIN], data,
2560 NULL, ZYX_MAX_RXBUFSZ, USBD_NO_COPY | USBD_SHORT_XFER_OK,
2561 USBD_NO_TIMEOUT, zyd_rxeof);
2562 error = usbd_transfer(data->xfer);
2563 if (error != USBD_IN_PROGRESS && error != 0) {
2564 device_printf(sc->sc_dev,
2565 "could not queue Rx transfer\n");
2570 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2571 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2573 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2574 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2575 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2577 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2581 fail: zyd_stop(sc, 1);
2586 zyd_stop(struct zyd_softc *sc, int disable)
2588 struct ifnet *ifp = sc->sc_ifp;
2589 struct ieee80211com *ic = &sc->sc_ic;
2591 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */
2594 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2596 /* switch radio transmitter OFF */
2597 (void)zyd_switch_radio(sc, 0);
2600 (void)zyd_write32(sc, ZYD_MAC_RXFILTER, 0);
2602 /* disable interrupts */
2603 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
2605 usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BIN]);
2606 usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BOUT]);
2608 zyd_free_rx_list(sc);
2609 zyd_free_tx_list(sc);
2613 zyd_loadfirmware(struct zyd_softc *sc, u_char *fw, size_t size)
2615 usb_device_request_t req;
2619 DPRINTF(("firmware size=%zu\n", size));
2621 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2622 req.bRequest = ZYD_DOWNLOADREQ;
2623 USETW(req.wIndex, 0);
2625 addr = ZYD_FIRMWARE_START_ADDR;
2628 const int mlen = min(size, 4096);
2631 * XXXX: When the transfer size is 4096 bytes, it is not
2632 * likely to be able to transfer it.
2633 * The cause is port or machine or chip?
2635 const int mlen = min(size, 64);
2638 DPRINTF(("loading firmware block: len=%d, addr=0x%x\n", mlen,
2641 USETW(req.wValue, addr);
2642 USETW(req.wLength, mlen);
2643 if (usbd_do_request(sc->sc_udev, &req, fw) != 0)
2651 /* check whether the upload succeeded */
2652 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2653 req.bRequest = ZYD_DOWNLOADSTS;
2654 USETW(req.wValue, 0);
2655 USETW(req.wIndex, 0);
2656 USETW(req.wLength, sizeof(stat));
2657 if (usbd_do_request(sc->sc_udev, &req, &stat) != 0)
2660 return (stat & 0x80) ? EIO : 0;
2664 zyd_iter_func(void *arg, struct ieee80211_node *ni)
2666 struct zyd_softc *sc = arg;
2667 struct zyd_node *zn = (struct zyd_node *)ni;
2669 ieee80211_amrr_choose(&sc->amrr, ni, &zn->amn);
2673 zyd_amrr_timeout(void *arg)
2675 struct zyd_softc *sc = arg;
2676 struct ieee80211com *ic = &sc->sc_ic;
2679 if (ic->ic_opmode == IEEE80211_M_STA)
2680 zyd_iter_func(sc, ic->ic_bss);
2682 ieee80211_iterate_nodes(&ic->ic_sta, zyd_iter_func, sc);
2685 callout_reset(&sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc);
2689 zyd_newassoc(struct ieee80211_node *ni, int isnew)
2691 struct zyd_softc *sc = ni->ni_ic->ic_ifp->if_softc;
2694 ieee80211_amrr_node_init(&sc->amrr, &((struct zyd_node *)ni)->amn);
2696 /* set rate to some reasonable initial value */
2697 for (i = ni->ni_rates.rs_nrates - 1;
2698 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2704 zyd_scan_start(struct ieee80211com *ic)
2706 struct zyd_softc *sc = ic->ic_ifp->if_softc;
2708 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2710 /* do it in a process context */
2711 sc->sc_scan_action = ZYD_SCAN_START;
2712 usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2716 zyd_scan_end(struct ieee80211com *ic)
2718 struct zyd_softc *sc = ic->ic_ifp->if_softc;
2720 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2722 /* do it in a process context */
2723 sc->sc_scan_action = ZYD_SCAN_END;
2724 usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2728 zyd_set_channel(struct ieee80211com *ic)
2730 struct zyd_softc *sc = ic->ic_ifp->if_softc;
2732 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2734 /* do it in a process context */
2735 sc->sc_scan_action = ZYD_SET_CHANNEL;
2736 usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2740 zyd_scantask(void *arg)
2742 struct zyd_softc *sc = arg;
2743 struct ieee80211com *ic = &sc->sc_ic;
2744 struct ifnet *ifp = ic->ic_ifp;
2748 switch (sc->sc_scan_action) {
2749 case ZYD_SCAN_START:
2750 zyd_set_bssid(sc, ifp->if_broadcastaddr);
2754 zyd_set_bssid(sc, ic->ic_bss->ni_bssid);
2757 case ZYD_SET_CHANNEL:
2759 zyd_set_chan(sc, ic->ic_curchan);
2764 device_printf(sc->sc_dev, "unknown scan action %d\n",
2765 sc->sc_scan_action);
2772 static device_method_t zyd_methods[] = {
2773 /* Device interface */
2774 DEVMETHOD(device_probe, zyd_match),
2775 DEVMETHOD(device_attach, zyd_attach),
2776 DEVMETHOD(device_detach, zyd_detach),
2781 static driver_t zyd_driver = {
2784 sizeof(struct zyd_softc)
2787 static devclass_t zyd_devclass;
2789 DRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, usbd_driver_load, 0);
2790 MODULE_DEPEND(rum, wlan, 1, 1, 1);
2791 MODULE_DEPEND(rum, wlan_amrr, 1, 1, 1);
2792 MODULE_DEPEND(rum, usb, 1, 1, 1);