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, 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),
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),
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),
150 #define zyd_lookup(v, p) \
151 ((const struct zyd_type *)usb_lookup(zyd_devs, v, p))
153 static device_probe_t zyd_match;
154 static device_attach_t zyd_attach;
155 static device_detach_t zyd_detach;
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,
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 *);
236 zyd_match(device_t dev)
238 struct usb_attach_arg *uaa = device_get_ivars(dev);
243 return (zyd_lookup(uaa->vendor, uaa->product) != NULL) ?
244 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
248 zyd_attachhook(struct zyd_softc *sc)
253 if (sc->mac_rev == ZYD_ZD1211) {
254 firmware = (u_char *)zd1211_firmware;
255 len = sizeof(zd1211_firmware);
257 firmware = (u_char *)zd1211b_firmware;
258 len = sizeof(zd1211b_firmware);
261 error = zyd_loadfirmware(sc, firmware, len);
263 device_printf(sc->sc_dev,
264 "could not load firmware (error=%d)\n", error);
268 sc->sc_flags |= ZD1211_FWLOADED;
270 /* complete the attach process */
271 return zyd_complete_attach(sc);
275 zyd_attach(device_t dev)
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;
285 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
287 device_printf(dev, "can not if_alloc()\n");
291 sc->sc_udev = uaa->device;
293 sc->mac_rev = zyd_lookup(uaa->vendor, uaa->product)->rev;
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));
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);
313 STAILQ_INIT(&sc->sc_rqh);
315 error = zyd_attachhook(sc);
326 zyd_complete_attach(struct zyd_softc *sc)
328 struct ieee80211com *ic = &sc->sc_ic;
329 struct ifnet *ifp = sc->sc_ifp;
333 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
334 MTX_DEF | MTX_RECURSE);
336 usb_init_task(&sc->sc_scantask, zyd_scantask, sc);
337 usb_init_task(&sc->sc_task, zyd_task, sc);
339 callout_init(&sc->sc_amrr_ch, 0);
340 callout_init(&sc->sc_watchdog_ch, 0);
342 error = usbd_set_config_no(sc->sc_udev, ZYD_CONFIG_NO, 1);
344 device_printf(sc->sc_dev, "setting config no failed\n");
349 error = usbd_device2interface_handle(sc->sc_udev, ZYD_IFACE_INDEX,
352 device_printf(sc->sc_dev, "getting interface handle failed\n");
357 if ((error = zyd_open_pipes(sc)) != 0) {
358 device_printf(sc->sc_dev, "could not open pipes\n");
362 if ((error = zyd_read_eeprom(sc)) != 0) {
363 device_printf(sc->sc_dev, "could not read EEPROM\n");
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",
373 if ((error = zyd_hw_init(sc)) != 0) {
374 device_printf(sc->sc_dev, "hardware initialization failed\n");
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));
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;
389 /* set device capabilities */
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 */
399 setbit(&bands, IEEE80211_MODE_11B);
400 setbit(&bands, IEEE80211_MODE_11G);
401 ieee80211_init_channels(ic, 0, CTRY_DEFAULT, bands, 0, 1);
403 ieee80211_ifattach(ic);
404 ic->ic_node_alloc = zyd_node_alloc;
405 ic->ic_newassoc = zyd_newassoc;
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;
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);
421 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
422 sizeof(struct ieee80211_frame) + sizeof(sc->sc_txtap),
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);
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);
434 ieee80211_announce(ic);
436 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
441 mtx_destroy(&sc->sc_mtx);
447 zyd_detach(device_t dev)
449 struct zyd_softc *sc = device_get_softc(dev);
450 struct ieee80211com *ic = &sc->sc_ic;
451 struct ifnet *ifp = sc->sc_ifp;
453 if (!device_is_attached(dev))
456 /* protect a race when we have listeners related with the driver. */
457 ifp->if_flags &= ~IFF_UP;
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);
468 ieee80211_ifdetach(ic);
471 mtx_destroy(&sc->sc_mtx);
473 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
479 zyd_open_pipes(struct zyd_softc *sc)
481 usb_endpoint_descriptor_t *edesc;
486 edesc = usbd_get_endpoint_descriptor(sc->sc_iface, 0x83);
490 isize = UGETW(edesc->wMaxPacketSize);
491 if (isize == 0) /* should not happen */
494 sc->ibuf = malloc(isize, M_USBDEV, M_NOWAIT);
495 if (sc->ibuf == NULL)
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);
502 device_printf(sc->sc_dev, "open rx intr pipe failed: %s\n",
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]);
511 device_printf(sc->sc_dev, "open tx intr pipe failed: %s\n",
517 error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE,
518 &sc->zyd_ep[ZYD_ENDPT_BIN]);
520 device_printf(sc->sc_dev, "open rx pipe failed: %s\n",
526 error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE,
527 &sc->zyd_ep[ZYD_ENDPT_BOUT]);
529 device_printf(sc->sc_dev, "open tx pipe failed: %s\n",
536 fail: zyd_close_pipes(sc);
541 zyd_close_pipes(struct zyd_softc *sc)
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;
552 if (sc->ibuf != NULL) {
553 free(sc->ibuf, M_USBDEV);
559 zyd_alloc_tx_list(struct zyd_softc *sc)
565 for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
566 struct zyd_tx_data *data = &sc->tx_data[i];
568 data->sc = sc; /* backpointer for callbacks */
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");
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");
585 /* clear Tx descriptor */
586 bzero(data->buf, sizeof(struct zyd_tx_desc));
590 fail: zyd_free_tx_list(sc);
595 zyd_free_tx_list(struct zyd_softc *sc)
599 for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
600 struct zyd_tx_data *data = &sc->tx_data[i];
602 if (data->xfer != NULL) {
603 usbd_free_xfer(data->xfer);
606 if (data->ni != NULL) {
607 ieee80211_free_node(data->ni);
614 zyd_alloc_rx_list(struct zyd_softc *sc)
618 for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
619 struct zyd_rx_data *data = &sc->rx_data[i];
621 data->sc = sc; /* backpointer for callbacks */
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");
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");
640 fail: zyd_free_rx_list(sc);
645 zyd_free_rx_list(struct zyd_softc *sc)
649 for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
650 struct zyd_rx_data *data = &sc->rx_data[i];
652 if (data->xfer != NULL) {
653 usbd_free_xfer(data->xfer);
660 static struct ieee80211_node *
661 zyd_node_alloc(struct ieee80211_node_table *nt __unused)
665 zn = malloc(sizeof(struct zyd_node), M_80211_NODE, M_NOWAIT | M_ZERO);
666 return zn != NULL ? &zn->ni : NULL;
670 zyd_media_change(struct ifnet *ifp)
672 struct zyd_softc *sc = ifp->if_softc;
675 error = ieee80211_media_change(ifp);
676 if (error != ENETRESET)
679 if ((ifp->if_flags & IFF_UP) == IFF_UP &&
680 (ifp->if_drv_flags & IFF_DRV_RUNNING) == IFF_DRV_RUNNING)
689 struct zyd_softc *sc = arg;
690 struct ieee80211com *ic = &sc->sc_ic;
691 enum ieee80211_state ostate;
693 ostate = ic->ic_state;
695 switch (sc->sc_state) {
696 case IEEE80211_S_RUN:
698 struct ieee80211_node *ni = ic->ic_bss;
700 zyd_set_chan(sc, ic->ic_curchan);
702 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
703 /* turn link LED on */
704 zyd_set_led(sc, ZYD_LED1, 1);
706 /* make data LED blink upon Tx */
707 zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 1);
709 zyd_set_bssid(sc, ni->ni_bssid);
712 if (ic->ic_opmode == IEEE80211_M_STA) {
713 /* fake a join to init the tx rate */
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);
728 sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
732 zyd_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
734 struct zyd_softc *sc = ic->ic_ifp->if_softc;
736 usb_rem_task(sc->sc_udev, &sc->sc_task);
737 callout_stop(&sc->sc_amrr_ch);
739 /* do it in a process context */
740 sc->sc_state = nstate;
743 if (nstate == IEEE80211_S_INIT)
744 sc->sc_newstate(ic, nstate, arg);
746 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
752 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
753 void *odata, int olen, u_int flags)
755 usbd_xfer_handle xfer;
761 if ((xfer = usbd_alloc_xfer(sc->sc_udev)) == NULL)
764 cmd.code = htole16(code);
765 bcopy(idata, cmd.data, ilen);
767 xferflags = USBD_FORCE_SHORT_XFER;
768 if (!(flags & ZYD_CMD_FLAG_READ))
769 xferflags |= USBD_SYNCHRONOUS;
773 rq.len = olen / sizeof(struct zyd_pair);
774 STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
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",
783 (void)usbd_free_xfer(xfer);
786 if (!(flags & ZYD_CMD_FLAG_READ)) {
787 (void)usbd_free_xfer(xfer);
788 return 0; /* write: don't wait for reply */
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);
796 (void)usbd_free_xfer(xfer);
801 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
807 error = zyd_cmd(sc, ZYD_CMD_IORD, ®, sizeof(reg), &tmp, sizeof(tmp),
810 *val = le16toh(tmp.val);
815 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
817 struct zyd_pair tmp[2];
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),
826 *val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
831 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
833 struct zyd_pair pair;
835 pair.reg = htole16(reg);
836 pair.val = htole16(val);
838 return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
842 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
844 struct zyd_pair pair[2];
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);
851 return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
855 zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
857 struct zyd_rf *rf = &sc->sc_rf;
858 struct zyd_rfwrite req;
862 (void)zyd_read16(sc, ZYD_CR203, &cr203);
863 cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
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);
872 return zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
876 zyd_lock_phy(struct zyd_softc *sc)
880 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
881 tmp &= ~ZYD_UNLOCK_PHY_REGS;
882 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
886 zyd_unlock_phy(struct zyd_softc *sc)
890 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
891 tmp |= ZYD_UNLOCK_PHY_REGS;
892 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
899 zyd_rfmd_init(struct zyd_rf *rf)
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;
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);
914 /* init RFMD radio */
915 for (i = 0; i < N(rfini); i++) {
916 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
924 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
926 struct zyd_softc *sc = rf->rf_sc;
928 (void)zyd_write16(sc, ZYD_CR10, on ? 0x89 : 0x15);
929 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x81);
935 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
937 struct zyd_softc *sc = rf->rf_sc;
938 static const struct {
940 } rfprog[] = ZYD_RFMD_CHANTABLE;
942 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
943 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
952 zyd_al2230_init(struct zyd_rf *rf)
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;
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);
967 /* init AL2230 radio */
968 for (i = 0; i < N(rfini); i++) {
969 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
977 zyd_al2230_init_b(struct zyd_rf *rf)
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;
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);
992 /* init AL2230 radio */
993 for (i = 0; i < N(rfini); i++) {
994 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1002 zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1004 struct zyd_softc *sc = rf->rf_sc;
1005 int on251 = (sc->mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f;
1007 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04);
1008 (void)zyd_write16(sc, ZYD_CR251, on ? on251 : 0x2f);
1014 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1016 struct zyd_softc *sc = rf->rf_sc;
1017 static const struct {
1018 uint32_t r1, r2, r3;
1019 } rfprog[] = ZYD_AL2230_CHANTABLE;
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);
1025 (void)zyd_write16(sc, ZYD_CR138, 0x28);
1026 (void)zyd_write16(sc, ZYD_CR203, 0x06);
1032 * AL7230B RF methods.
1035 zyd_al7230B_init(struct zyd_rf *rf)
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;
1046 /* for AL7230B, PHY and RF need to be initialized in "phases" */
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);
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)
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);
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)
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);
1082 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1084 struct zyd_softc *sc = rf->rf_sc;
1086 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04);
1087 (void)zyd_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1093 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1095 #define N(a) (sizeof(a) / sizeof((a)[0]))
1096 struct zyd_softc *sc = rf->rf_sc;
1097 static const struct {
1099 } rfprog[] = ZYD_AL7230B_CHANTABLE;
1100 static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1103 (void)zyd_write16(sc, ZYD_CR240, 0x57);
1104 (void)zyd_write16(sc, ZYD_CR251, 0x2f);
1106 for (i = 0; i < N(rfsc); i++) {
1107 if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
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);
1117 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1118 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1119 (void)zyd_rfwrite(sc, 0x3c9000);
1121 (void)zyd_write16(sc, ZYD_CR251, 0x3f);
1122 (void)zyd_write16(sc, ZYD_CR203, 0x06);
1123 (void)zyd_write16(sc, ZYD_CR240, 0x08);
1130 * AL2210 RF methods.
1133 zyd_al2210_init(struct zyd_rf *rf)
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;
1142 (void)zyd_write32(sc, ZYD_CR18, 2);
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);
1150 /* init AL2210 radio */
1151 for (i = 0; i < N(rfini); i++) {
1152 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
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);
1169 zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1171 /* vendor driver does nothing for this RF chip */
1177 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1179 struct zyd_softc *sc = rf->rf_sc;
1180 static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
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);
1190 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1191 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1193 /* actually set the channel */
1194 (void)zyd_rfwrite(sc, rfprog[chan - 1]);
1196 (void)zyd_write32(sc, ZYD_CR18, 3);
1205 zyd_gct_init(struct zyd_rf *rf)
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;
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);
1219 /* init cgt radio */
1220 for (i = 0; i < N(rfini); i++) {
1221 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1229 zyd_gct_switch_radio(struct zyd_rf *rf, int on)
1231 /* vendor driver does nothing for this RF chip */
1237 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1239 struct zyd_softc *sc = rf->rf_sc;
1240 static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE;
1242 (void)zyd_rfwrite(sc, 0x1c0000);
1243 (void)zyd_rfwrite(sc, rfprog[chan - 1]);
1244 (void)zyd_rfwrite(sc, 0x1c0008);
1253 zyd_maxim_init(struct zyd_rf *rf)
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;
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);
1268 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1269 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1271 /* init maxim radio */
1272 for (i = 0; i < N(rfini); i++) {
1273 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1276 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1277 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1284 zyd_maxim_switch_radio(struct zyd_rf *rf, int on)
1286 /* vendor driver does nothing for this RF chip */
1292 zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan)
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 {
1300 } rfprog[] = ZYD_MAXIM_CHANTABLE;
1305 * Do the same as we do when initializing it, except for the channel
1306 * values coming from the two channel tables.
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);
1315 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1316 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
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);
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)
1327 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1328 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1335 * Maxim2 RF methods.
1338 zyd_maxim2_init(struct zyd_rf *rf)
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;
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);
1353 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1354 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1356 /* init maxim2 radio */
1357 for (i = 0; i < N(rfini); i++) {
1358 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1361 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1362 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1369 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1371 /* vendor driver does nothing for this RF chip */
1377 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
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 {
1385 } rfprog[] = ZYD_MAXIM2_CHANTABLE;
1390 * Do the same as we do when initializing it, except for the channel
1391 * values coming from the two channel tables.
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);
1400 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1401 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
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);
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)
1412 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1413 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1420 zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1422 struct zyd_rf *rf = &sc->sc_rf;
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 */
1434 if (sc->mac_rev == ZYD_ZD1211B)
1435 rf->init = zyd_al2230_init_b;
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 */
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 */
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 */
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 */
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 */
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 */
1473 device_printf(sc->sc_dev,
1474 "sorry, radio \"%s\" is not supported yet\n",
1482 zyd_rf_name(uint8_t type)
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",
1491 return zyd_rfs[(type > 15) ? 0 : type];
1495 zyd_hw_init(struct zyd_softc *sc)
1497 struct zyd_rf *rf = &sc->sc_rf;
1498 const struct zyd_phy_pair *phyp;
1502 /* specify that the plug and play is finished */
1503 (void)zyd_write32(sc, ZYD_MAC_AFTER_PNP, 1);
1505 (void)zyd_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->fwbase);
1506 DPRINTF(("firmware base address=0x%04x\n", sc->fwbase));
1508 /* retrieve firmware revision number */
1509 (void)zyd_read16(sc, sc->fwbase + ZYD_FW_FIRMWARE_REV, &sc->fw_rev);
1511 (void)zyd_write32(sc, ZYD_CR_GPI_EN, 0);
1512 (void)zyd_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1514 /* disable interrupts */
1515 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
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)
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);
1531 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1532 zyd_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1534 if (sc->mac_rev == ZYD_ZD1211) {
1535 zyd_write32(sc, ZYD_MAC_RETRY, 0x00000002);
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);
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);
1567 error = (*rf->init)(rf);
1570 device_printf(sc->sc_dev,
1571 "radio initialization failed, error %d\n", error);
1575 /* init beacon interval to 100ms */
1576 if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1583 zyd_read_eeprom(struct zyd_softc *sc)
1585 struct ieee80211com *ic = &sc->sc_ic;
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;
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;
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));
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;
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;
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;
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;
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;
1637 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1641 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1642 (void)zyd_write32(sc, ZYD_MAC_MACADRL, tmp);
1644 tmp = addr[5] << 8 | addr[4];
1645 (void)zyd_write32(sc, ZYD_MAC_MACADRH, tmp);
1651 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1655 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1656 (void)zyd_write32(sc, ZYD_MAC_BSSADRL, tmp);
1658 tmp = addr[5] << 8 | addr[4];
1659 (void)zyd_write32(sc, ZYD_MAC_BSSADRH, tmp);
1665 zyd_switch_radio(struct zyd_softc *sc, int on)
1667 struct zyd_rf *rf = &sc->sc_rf;
1671 error = (*rf->switch_radio)(rf, on);
1678 zyd_set_led(struct zyd_softc *sc, int which, int on)
1682 (void)zyd_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1686 (void)zyd_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1690 zyd_set_multi(struct zyd_softc *sc)
1692 struct ieee80211com *ic = &sc->sc_ic;
1693 struct ifnet *ifp = ic->ic_ifp;
1694 struct ifmultiaddr *ifma;
1698 if (!(ifp->if_flags & IFF_UP))
1704 if (ic->ic_opmode == IEEE80211_M_MONITOR ||
1705 (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) {
1710 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1711 if (ifma->ifma_addr->sa_family != AF_LINK)
1713 v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
1714 ifma->ifma_addr))[5] >> 2;
1718 high |= 1 << (v - 32);
1720 IF_ADDR_UNLOCK(ifp);
1723 /* reprogram multicast global hash table */
1724 zyd_write32(sc, ZYD_MAC_GHTBL, low);
1725 zyd_write32(sc, ZYD_MAC_GHTBH, high);
1729 zyd_set_rxfilter(struct zyd_softc *sc)
1733 switch (sc->sc_ic.ic_opmode) {
1734 case IEEE80211_M_STA:
1735 rxfilter = ZYD_FILTER_BSS;
1737 case IEEE80211_M_IBSS:
1738 case IEEE80211_M_HOSTAP:
1739 rxfilter = ZYD_FILTER_HOSTAP;
1741 case IEEE80211_M_MONITOR:
1742 rxfilter = ZYD_FILTER_MONITOR;
1745 /* should not get there */
1748 return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
1752 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
1754 struct ieee80211com *ic = &sc->sc_ic;
1755 struct zyd_rf *rf = &sc->sc_rf;
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);
1769 (*rf->set_channel)(rf, chan);
1771 /* update Tx power */
1772 (void)zyd_write16(sc, ZYD_CR31, sc->pwr_int[chan - 1]);
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]);
1779 (void)zyd_write16(sc, ZYD_CR68, sc->pwr_cal[chan - 1]);
1781 (void)zyd_write16(sc, ZYD_CR69, 0x28);
1782 (void)zyd_write16(sc, ZYD_CR69, 0x2a);
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);
1791 (void)zyd_write32(sc, ZYD_CR_CONFIG_PHILIPS, 0);
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);
1802 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
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);
1813 zyd_plcp_signal(int rate)
1816 /* CCK rates (returned values are device-dependent) */
1819 case 11: return 0x2;
1820 case 22: return 0x3;
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;
1832 /* unsupported rates (should not get there) */
1833 default: return 0xff;
1838 zyd_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1840 struct zyd_softc *sc = (struct zyd_softc *)priv;
1841 struct zyd_cmd *cmd;
1844 if (status != USBD_NORMAL_COMPLETION) {
1845 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1848 if (status == USBD_STALLED) {
1849 usbd_clear_endpoint_stall_async(
1850 sc->zyd_ep[ZYD_ENDPT_IIN]);
1855 cmd = (struct zyd_cmd *)sc->ibuf;
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;
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)));
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.
1873 if (ic->ic_opmode != IEEE80211_M_STA) {
1874 ni = ieee80211_find_node(&ic->ic_sta, retry->macaddr);
1876 return; /* just ignore */
1880 ((struct zyd_node *)ni)->amn.amn_retrycnt++;
1882 if (le16toh(retry->count) & 0x100)
1883 ifp->if_oerrors++; /* too many retries */
1884 } else if (le16toh(cmd->code) == ZYD_NOTIF_IORD) {
1887 if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
1888 return; /* HMAC interrupt */
1890 usbd_get_xfer_status(xfer, NULL, NULL, &datalen, NULL);
1891 datalen -= sizeof(cmd->code);
1892 datalen -= 2; /* XXX: padding? */
1894 STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
1897 if (sizeof(struct zyd_pair) * rqp->len != datalen)
1899 for (i = 0; i < rqp->len; i++) {
1900 if (*(((const uint16_t *)rqp->idata) + i) !=
1901 (((struct zyd_pair *)cmd->data) + i)->reg)
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 */
1914 return; /* unexpected IORD notification */
1916 device_printf(sc->sc_dev, "unknown notification %x\n",
1917 le16toh(cmd->code));
1921 static __inline uint8_t
1922 zyd_plcp2ieee(int signal, int 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];
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];
1936 zyd_rx_data(struct zyd_softc *sc, const uint8_t *buf, uint16_t len)
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;
1946 if (len < ZYD_MIN_FRAGSZ) {
1947 DPRINTF(("%s: frame too short (length=%d)\n",
1948 device_get_nameunit(sc->sc_dev), len));
1953 plcp = (const struct zyd_plcphdr *)buf;
1954 stat = (const struct zyd_rx_stat *)
1955 (buf + len - sizeof(struct zyd_rx_stat));
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));
1964 /* compute actual frame length */
1965 rlen = len - sizeof(struct zyd_plcphdr) -
1966 sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
1968 /* allocate a mbuf to store the frame */
1970 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1972 m = m_gethdr(M_DONTWAIT, MT_DATA);
1974 DPRINTF(("%s: could not allocate rx mbuf\n",
1975 device_get_nameunit(sc->sc_dev)));
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);
1983 if (bpf_peers_present(sc->sc_drvbpf)) {
1984 struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
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;
1993 zyd_plcp2ieee(plcp->signal, stat->flags & ZYD_RX_OFDM);
1994 tap->wr_antsignal = stat->rssi + -95;
1995 tap->wr_antnoise = -95; /* XXX */
1997 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
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);
2004 /* node is no longer needed */
2005 ieee80211_free_node(ni);
2009 zyd_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
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;
2017 if (status != USBD_NORMAL_COMPLETION) {
2018 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
2021 if (status == USBD_STALLED)
2022 usbd_clear_endpoint_stall(sc->zyd_ep[ZYD_ENDPT_BIN]);
2026 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
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 */
2035 desc = (const struct zyd_rx_desc *)
2036 (data->buf + len - sizeof(struct zyd_rx_desc));
2038 if (UGETW(desc->tag) == ZYD_TAG_MULTIFRAME) {
2039 const uint8_t *p = data->buf, *end = p + len;
2042 DPRINTFN(3, ("received multi-frame transfer\n"));
2044 for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2045 const uint16_t len16 = UGETW(desc->len[i]);
2047 if (len16 == 0 || p + len16 > end)
2050 zyd_rx_data(sc, p, len16);
2051 /* next frame is aligned on a 32-bit boundary */
2052 p += (len16 + 3) & ~3;
2055 DPRINTFN(3, ("received single-frame transfer\n"));
2057 zyd_rx_data(sc, data->buf, len);
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);
2068 zyd_tx_mgt(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
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;
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;
2087 wh = mtod(m0, struct ieee80211_frame *);
2089 xferlen = sizeof(struct zyd_tx_desc) + m0->m_pkthdr.len;
2090 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2092 /* fill Tx descriptor */
2093 desc->len = htole16(totlen);
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;
2108 desc->flags |= ZYD_TX_FLAG_MULTICAST;
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);
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;
2123 /* actual transmit length (XXX why +10?) */
2124 pktlen = sizeof(struct zyd_tx_desc) + 10;
2125 if (sc->mac_rev == ZYD_ZD1211)
2127 desc->pktlen = htole16(pktlen);
2129 desc->plcp_length = (16 * totlen + rate - 1) / rate;
2130 desc->plcp_service = 0;
2132 const int remainder = (16 * totlen) % 22;
2133 if (remainder != 0 && remainder < 7)
2134 desc->plcp_service |= ZYD_PLCP_LENGEXT;
2137 if (bpf_peers_present(sc->sc_drvbpf)) {
2138 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2141 tap->wt_rate = rate;
2143 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
2146 m_copydata(m0, 0, m0->m_pkthdr.len,
2147 data->buf + sizeof(struct zyd_tx_desc));
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,
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) {
2167 zyd_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
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;
2175 if (status != USBD_NORMAL_COMPLETION) {
2176 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
2179 device_printf(sc->sc_dev, "could not transmit buffer: %s\n",
2180 usbd_errstr(status));
2182 if (status == USBD_STALLED) {
2183 usbd_clear_endpoint_stall_async(
2184 sc->zyd_ep[ZYD_ENDPT_BOUT]);
2191 /* update rate control statistics */
2192 ((struct zyd_node *)ni)->amn.amn_txcnt++;
2195 * Do any tx complete callback. Note this must
2196 * be done before releasing the node reference.
2199 if (m != NULL && m->m_flags & M_TXCB) {
2200 ieee80211_process_callback(ni, m, 0); /* XXX status? */
2205 ieee80211_free_node(ni);
2212 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2217 zyd_tx_data(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
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;
2229 wh = mtod(m0, struct ieee80211_frame *);
2230 data = &sc->tx_data[0];
2231 desc = (struct zyd_tx_desc *)data->buf;
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];
2240 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
2241 rate &= IEEE80211_RATE_VAL;
2243 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2244 k = ieee80211_crypto_encap(ic, ni, m0);
2250 /* packet header may have moved, reset our local pointer */
2251 wh = mtod(m0, struct ieee80211_frame *);
2257 xferlen = sizeof(struct zyd_tx_desc) + m0->m_pkthdr.len;
2258 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2260 /* fill Tx descriptor */
2261 desc->len = htole16(totlen);
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;
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);
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;
2289 /* actual transmit length (XXX why +10?) */
2290 pktlen = sizeof(struct zyd_tx_desc) + 10;
2291 if (sc->mac_rev == ZYD_ZD1211)
2293 desc->pktlen = htole16(pktlen);
2295 desc->plcp_length = (16 * totlen + rate - 1) / rate;
2296 desc->plcp_service = 0;
2298 const int remainder = (16 * totlen) % 22;
2299 if (remainder != 0 && remainder < 7)
2300 desc->plcp_service |= ZYD_PLCP_LENGEXT;
2303 if (bpf_peers_present(sc->sc_drvbpf)) {
2304 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
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);
2311 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
2314 m_copydata(m0, 0, m0->m_pkthdr.len,
2315 data->buf + sizeof(struct zyd_tx_desc));
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,
2321 m_freem(m0); /* mbuf no longer needed */
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) {
2337 zyd_start(struct ifnet *ifp)
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;
2346 IF_POLL(&ic->ic_mgtq, m0);
2348 if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
2349 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2352 IF_DEQUEUE(&ic->ic_mgtq, m0);
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)
2361 if (ic->ic_state != IEEE80211_S_RUN)
2363 IFQ_POLL(&ifp->if_snd, m0);
2366 if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
2367 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2370 IFQ_DEQUEUE(&ifp->if_snd, m0);
2372 * Cancel any background scan.
2374 if (ic->ic_flags & IEEE80211_F_SCAN)
2375 ieee80211_cancel_scan(ic);
2377 if (m0->m_len < sizeof(struct ether_header) &&
2378 !(m0 = m_pullup(m0, sizeof(struct ether_header))))
2381 eh = mtod(m0, struct ether_header *);
2382 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
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);
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);
2404 ic->ic_lastdata = ticks;
2405 callout_reset(&sc->sc_watchdog_ch, hz, zyd_watchdog, sc);
2410 zyd_watchdog(void *arg)
2412 struct zyd_softc *sc = arg;
2413 struct ieee80211com *ic = &sc->sc_ic;
2414 struct ifnet *ifp = ic->ic_ifp;
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 ? */
2423 callout_reset(&sc->sc_watchdog_ch, hz, zyd_watchdog, sc);
2428 zyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2430 struct zyd_softc *sc = ifp->if_softc;
2431 struct ieee80211com *ic = &sc->sc_ic;
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))
2446 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2449 sc->sc_if_flags = ifp->if_flags;
2454 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2459 error = ieee80211_ioctl(ic, cmd, data);
2462 if (error == ENETRESET) {
2463 if ((ifp->if_flags & IFF_UP) == IFF_UP &&
2464 (ifp->if_drv_flags & IFF_DRV_RUNNING) == IFF_DRV_RUNNING)
2475 zyd_init(void *priv)
2477 struct zyd_softc *sc = priv;
2478 struct ieee80211com *ic = &sc->sc_ic;
2479 struct ifnet *ifp = ic->ic_ifp;
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);
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);
2496 /* promiscuous mode */
2497 (void)zyd_write32(sc, ZYD_MAC_SNIFFER, 0);
2499 /* multicast setup */
2500 (void)zyd_set_multi(sc);
2502 (void)zyd_set_rxfilter(sc);
2504 /* switch radio transmitter ON */
2505 (void)zyd_switch_radio(sc, 1);
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);
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);
2524 /* set default BSS channel */
2525 zyd_set_chan(sc, ic->ic_curchan);
2527 /* enable interrupts */
2528 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2531 * Allocate Tx and Rx xfer queues.
2533 if ((error = zyd_alloc_tx_list(sc)) != 0) {
2534 device_printf(sc->sc_dev, "could not allocate Tx list\n");
2537 if ((error = zyd_alloc_rx_list(sc)) != 0) {
2538 device_printf(sc->sc_dev, "could not allocate Rx list\n");
2543 * Start up the receive pipe.
2545 for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
2546 struct zyd_rx_data *data = &sc->rx_data[i];
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");
2559 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2560 ifp->if_drv_flags |= IFF_DRV_RUNNING;
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);
2566 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2570 fail: zyd_stop(sc, 1);
2575 zyd_stop(struct zyd_softc *sc, int disable)
2577 struct ifnet *ifp = sc->sc_ifp;
2578 struct ieee80211com *ic = &sc->sc_ic;
2580 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */
2583 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2585 /* switch radio transmitter OFF */
2586 (void)zyd_switch_radio(sc, 0);
2589 (void)zyd_write32(sc, ZYD_MAC_RXFILTER, 0);
2591 /* disable interrupts */
2592 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
2594 usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BIN]);
2595 usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BOUT]);
2597 zyd_free_rx_list(sc);
2598 zyd_free_tx_list(sc);
2602 zyd_loadfirmware(struct zyd_softc *sc, u_char *fw, size_t size)
2604 usb_device_request_t req;
2608 DPRINTF(("firmware size=%zu\n", size));
2610 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2611 req.bRequest = ZYD_DOWNLOADREQ;
2612 USETW(req.wIndex, 0);
2614 addr = ZYD_FIRMWARE_START_ADDR;
2617 const int mlen = min(size, 4096);
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?
2624 const int mlen = min(size, 64);
2627 DPRINTF(("loading firmware block: len=%d, addr=0x%x\n", mlen,
2630 USETW(req.wValue, addr);
2631 USETW(req.wLength, mlen);
2632 if (usbd_do_request(sc->sc_udev, &req, fw) != 0)
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)
2649 return (stat & 0x80) ? EIO : 0;
2653 zyd_iter_func(void *arg, struct ieee80211_node *ni)
2655 struct zyd_softc *sc = arg;
2656 struct zyd_node *zn = (struct zyd_node *)ni;
2658 ieee80211_amrr_choose(&sc->amrr, ni, &zn->amn);
2662 zyd_amrr_timeout(void *arg)
2664 struct zyd_softc *sc = arg;
2665 struct ieee80211com *ic = &sc->sc_ic;
2668 if (ic->ic_opmode == IEEE80211_M_STA)
2669 zyd_iter_func(sc, ic->ic_bss);
2671 ieee80211_iterate_nodes(&ic->ic_sta, zyd_iter_func, sc);
2674 callout_reset(&sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc);
2678 zyd_newassoc(struct ieee80211_node *ni, int isnew)
2680 struct zyd_softc *sc = ni->ni_ic->ic_ifp->if_softc;
2683 ieee80211_amrr_node_init(&sc->amrr, &((struct zyd_node *)ni)->amn);
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;
2693 zyd_scan_start(struct ieee80211com *ic)
2695 struct zyd_softc *sc = ic->ic_ifp->if_softc;
2697 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
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);
2705 zyd_scan_end(struct ieee80211com *ic)
2707 struct zyd_softc *sc = ic->ic_ifp->if_softc;
2709 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
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);
2717 zyd_set_channel(struct ieee80211com *ic)
2719 struct zyd_softc *sc = ic->ic_ifp->if_softc;
2721 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
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);
2729 zyd_scantask(void *arg)
2731 struct zyd_softc *sc = arg;
2732 struct ieee80211com *ic = &sc->sc_ic;
2733 struct ifnet *ifp = ic->ic_ifp;
2737 switch (sc->sc_scan_action) {
2738 case ZYD_SCAN_START:
2739 zyd_set_bssid(sc, ifp->if_broadcastaddr);
2743 zyd_set_bssid(sc, ic->ic_bss->ni_bssid);
2746 case ZYD_SET_CHANNEL:
2748 zyd_set_chan(sc, ic->ic_curchan);
2753 device_printf(sc->sc_dev, "unknown scan action %d\n",
2754 sc->sc_scan_action);
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),
2770 static driver_t zyd_driver = {
2773 sizeof(struct zyd_softc)
2776 static devclass_t zyd_devclass;
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);