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.
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
26 * ZyDAS ZD1211/ZD1211B USB WLAN driver.
29 #include <sys/param.h>
30 #include <sys/sockio.h>
31 #include <sys/sysctl.h>
33 #include <sys/mutex.h>
34 #include <sys/condvar.h>
36 #include <sys/kernel.h>
37 #include <sys/socket.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
42 #include <sys/endian.h>
45 #include <machine/bus.h>
46 #include <machine/resource.h>
51 #include <net/if_arp.h>
52 #include <net/ethernet.h>
53 #include <net/if_dl.h>
54 #include <net/if_media.h>
55 #include <net/if_types.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_var.h>
61 #include <netinet/if_ether.h>
62 #include <netinet/ip.h>
65 #include <net80211/ieee80211_var.h>
66 #include <net80211/ieee80211_regdomain.h>
67 #include <net80211/ieee80211_radiotap.h>
68 #include <net80211/ieee80211_ratectl.h>
70 #include <dev/usb/usb.h>
71 #include <dev/usb/usbdi.h>
72 #include <dev/usb/usbdi_util.h>
75 #include <dev/usb/wlan/if_zydreg.h>
76 #include <dev/usb/wlan/if_zydfw.h>
79 static int zyd_debug = 0;
81 static SYSCTL_NODE(_hw_usb, OID_AUTO, zyd, CTLFLAG_RW, 0, "USB zyd");
82 SYSCTL_INT(_hw_usb_zyd, OID_AUTO, debug, CTLFLAG_RW, &zyd_debug, 0,
86 ZYD_DEBUG_XMIT = 0x00000001, /* basic xmit operation */
87 ZYD_DEBUG_RECV = 0x00000002, /* basic recv operation */
88 ZYD_DEBUG_RESET = 0x00000004, /* reset processing */
89 ZYD_DEBUG_INIT = 0x00000008, /* device init */
90 ZYD_DEBUG_TX_PROC = 0x00000010, /* tx ISR proc */
91 ZYD_DEBUG_RX_PROC = 0x00000020, /* rx ISR proc */
92 ZYD_DEBUG_STATE = 0x00000040, /* 802.11 state transitions */
93 ZYD_DEBUG_STAT = 0x00000080, /* statistic */
94 ZYD_DEBUG_FW = 0x00000100, /* firmware */
95 ZYD_DEBUG_CMD = 0x00000200, /* fw commands */
96 ZYD_DEBUG_ANY = 0xffffffff
98 #define DPRINTF(sc, m, fmt, ...) do { \
99 if (zyd_debug & (m)) \
100 printf("%s: " fmt, __func__, ## __VA_ARGS__); \
103 #define DPRINTF(sc, m, fmt, ...) do { \
108 #define zyd_do_request(sc,req,data) \
109 usbd_do_request_flags((sc)->sc_udev, &(sc)->sc_mtx, req, data, 0, NULL, 5000)
111 static device_probe_t zyd_match;
112 static device_attach_t zyd_attach;
113 static device_detach_t zyd_detach;
115 static usb_callback_t zyd_intr_read_callback;
116 static usb_callback_t zyd_intr_write_callback;
117 static usb_callback_t zyd_bulk_read_callback;
118 static usb_callback_t zyd_bulk_write_callback;
120 static struct ieee80211vap *zyd_vap_create(struct ieee80211com *,
121 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
122 const uint8_t [IEEE80211_ADDR_LEN],
123 const uint8_t [IEEE80211_ADDR_LEN]);
124 static void zyd_vap_delete(struct ieee80211vap *);
125 static void zyd_tx_free(struct zyd_tx_data *, int);
126 static void zyd_setup_tx_list(struct zyd_softc *);
127 static void zyd_unsetup_tx_list(struct zyd_softc *);
128 static int zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int);
129 static int zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
131 static int zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
132 static int zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
133 static int zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
134 static int zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
135 static int zyd_rfwrite(struct zyd_softc *, uint32_t);
136 static int zyd_lock_phy(struct zyd_softc *);
137 static int zyd_unlock_phy(struct zyd_softc *);
138 static int zyd_rf_attach(struct zyd_softc *, uint8_t);
139 static const char *zyd_rf_name(uint8_t);
140 static int zyd_hw_init(struct zyd_softc *);
141 static int zyd_read_pod(struct zyd_softc *);
142 static int zyd_read_eeprom(struct zyd_softc *);
143 static int zyd_get_macaddr(struct zyd_softc *);
144 static int zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
145 static int zyd_set_bssid(struct zyd_softc *, const uint8_t *);
146 static int zyd_switch_radio(struct zyd_softc *, int);
147 static int zyd_set_led(struct zyd_softc *, int, int);
148 static void zyd_set_multi(struct zyd_softc *);
149 static void zyd_update_mcast(struct ifnet *);
150 static int zyd_set_rxfilter(struct zyd_softc *);
151 static void zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
152 static int zyd_set_beacon_interval(struct zyd_softc *, int);
153 static void zyd_rx_data(struct usb_xfer *, int, uint16_t);
154 static int zyd_tx_start(struct zyd_softc *, struct mbuf *,
155 struct ieee80211_node *);
156 static void zyd_start(struct ifnet *);
157 static int zyd_raw_xmit(struct ieee80211_node *, struct mbuf *,
158 const struct ieee80211_bpf_params *);
159 static int zyd_ioctl(struct ifnet *, u_long, caddr_t);
160 static void zyd_init_locked(struct zyd_softc *);
161 static void zyd_init(void *);
162 static void zyd_stop(struct zyd_softc *);
163 static int zyd_loadfirmware(struct zyd_softc *);
164 static void zyd_scan_start(struct ieee80211com *);
165 static void zyd_scan_end(struct ieee80211com *);
166 static void zyd_set_channel(struct ieee80211com *);
167 static int zyd_rfmd_init(struct zyd_rf *);
168 static int zyd_rfmd_switch_radio(struct zyd_rf *, int);
169 static int zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
170 static int zyd_al2230_init(struct zyd_rf *);
171 static int zyd_al2230_switch_radio(struct zyd_rf *, int);
172 static int zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
173 static int zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t);
174 static int zyd_al2230_init_b(struct zyd_rf *);
175 static int zyd_al7230B_init(struct zyd_rf *);
176 static int zyd_al7230B_switch_radio(struct zyd_rf *, int);
177 static int zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
178 static int zyd_al2210_init(struct zyd_rf *);
179 static int zyd_al2210_switch_radio(struct zyd_rf *, int);
180 static int zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
181 static int zyd_gct_init(struct zyd_rf *);
182 static int zyd_gct_switch_radio(struct zyd_rf *, int);
183 static int zyd_gct_set_channel(struct zyd_rf *, uint8_t);
184 static int zyd_gct_mode(struct zyd_rf *);
185 static int zyd_gct_set_channel_synth(struct zyd_rf *, int, int);
186 static int zyd_gct_write(struct zyd_rf *, uint16_t);
187 static int zyd_gct_txgain(struct zyd_rf *, uint8_t);
188 static int zyd_maxim2_init(struct zyd_rf *);
189 static int zyd_maxim2_switch_radio(struct zyd_rf *, int);
190 static int zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
192 static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
193 static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
195 /* various supported device vendors/products */
197 #define ZYD_ZD1211B 1
199 #define ZYD_ZD1211_DEV(v,p) \
200 { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211) }
201 #define ZYD_ZD1211B_DEV(v,p) \
202 { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211B) }
203 static const STRUCT_USB_HOST_ID zyd_devs[] = {
205 ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
206 ZYD_ZD1211_DEV(ABOCOM, WL54),
207 ZYD_ZD1211_DEV(ASUS, WL159G),
208 ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
209 ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
210 ZYD_ZD1211_DEV(PLANEX2, GWUS54GD),
211 ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
212 ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
213 ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
214 ZYD_ZD1211_DEV(SAGEM, XG760A),
215 ZYD_ZD1211_DEV(SENAO, NUB8301),
216 ZYD_ZD1211_DEV(SITECOMEU, WL113),
217 ZYD_ZD1211_DEV(SWEEX, ZD1211),
218 ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
219 ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
220 ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
221 ZYD_ZD1211_DEV(TWINMOS, G240),
222 ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
223 ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
224 ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
225 ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
226 ZYD_ZD1211_DEV(ZCOM, ZD1211),
227 ZYD_ZD1211_DEV(ZYDAS, ZD1211),
228 ZYD_ZD1211_DEV(ZYXEL, AG225H),
229 ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
230 ZYD_ZD1211_DEV(ZYXEL, G200V2),
232 ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG_NF),
233 ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
234 ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
235 ZYD_ZD1211B_DEV(ASUS, A9T_WIFI),
236 ZYD_ZD1211B_DEV(BELKIN, F5D7050_V4000),
237 ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
238 ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
239 ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
240 ZYD_ZD1211B_DEV(MELCO, KG54L),
241 ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
242 ZYD_ZD1211B_DEV(PLANEX2, GW_US54GXS),
243 ZYD_ZD1211B_DEV(SAGEM, XG76NA),
244 ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
245 ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
246 ZYD_ZD1211B_DEV(USR, USR5423),
247 ZYD_ZD1211B_DEV(VTECH, ZD1211B),
248 ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
249 ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
250 ZYD_ZD1211B_DEV(ZYXEL, M202),
251 ZYD_ZD1211B_DEV(ZYXEL, G202),
252 ZYD_ZD1211B_DEV(ZYXEL, G220V2)
255 static const struct usb_config zyd_config[ZYD_N_TRANSFER] = {
258 .endpoint = UE_ADDR_ANY,
259 .direction = UE_DIR_OUT,
260 .bufsize = ZYD_MAX_TXBUFSZ,
261 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
262 .callback = zyd_bulk_write_callback,
264 .timeout = 10000, /* 10 seconds */
268 .endpoint = UE_ADDR_ANY,
269 .direction = UE_DIR_IN,
270 .bufsize = ZYX_MAX_RXBUFSZ,
271 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
272 .callback = zyd_bulk_read_callback,
276 .type = UE_BULK_INTR,
277 .endpoint = UE_ADDR_ANY,
278 .direction = UE_DIR_OUT,
279 .bufsize = sizeof(struct zyd_cmd),
280 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
281 .callback = zyd_intr_write_callback,
282 .timeout = 1000, /* 1 second */
286 .type = UE_INTERRUPT,
287 .endpoint = UE_ADDR_ANY,
288 .direction = UE_DIR_IN,
289 .bufsize = sizeof(struct zyd_cmd),
290 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
291 .callback = zyd_intr_read_callback,
294 #define zyd_read16_m(sc, val, data) do { \
295 error = zyd_read16(sc, val, data); \
299 #define zyd_write16_m(sc, val, data) do { \
300 error = zyd_write16(sc, val, data); \
304 #define zyd_read32_m(sc, val, data) do { \
305 error = zyd_read32(sc, val, data); \
309 #define zyd_write32_m(sc, val, data) do { \
310 error = zyd_write32(sc, val, data); \
316 zyd_match(device_t dev)
318 struct usb_attach_arg *uaa = device_get_ivars(dev);
320 if (uaa->usb_mode != USB_MODE_HOST)
322 if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX)
324 if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX)
327 return (usbd_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa));
331 zyd_attach(device_t dev)
333 struct usb_attach_arg *uaa = device_get_ivars(dev);
334 struct zyd_softc *sc = device_get_softc(dev);
336 struct ieee80211com *ic;
337 uint8_t iface_index, bands;
340 if (uaa->info.bcdDevice < 0x4330) {
341 device_printf(dev, "device version mismatch: 0x%X "
342 "(only >= 43.30 supported)\n",
343 uaa->info.bcdDevice);
347 device_set_usb_desc(dev);
349 sc->sc_udev = uaa->device;
350 sc->sc_macrev = USB_GET_DRIVER_INFO(uaa);
352 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
353 MTX_NETWORK_LOCK, MTX_DEF);
354 STAILQ_INIT(&sc->sc_rqh);
356 iface_index = ZYD_IFACE_INDEX;
357 error = usbd_transfer_setup(uaa->device,
358 &iface_index, sc->sc_xfer, zyd_config,
359 ZYD_N_TRANSFER, sc, &sc->sc_mtx);
361 device_printf(dev, "could not allocate USB transfers, "
362 "err=%s\n", usbd_errstr(error));
367 if ((error = zyd_get_macaddr(sc)) != 0) {
368 device_printf(sc->sc_dev, "could not read EEPROM\n");
374 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
376 device_printf(sc->sc_dev, "can not if_alloc()\n");
380 if_initname(ifp, "zyd", device_get_unit(sc->sc_dev));
381 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
382 ifp->if_init = zyd_init;
383 ifp->if_ioctl = zyd_ioctl;
384 ifp->if_start = zyd_start;
385 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
386 IFQ_SET_READY(&ifp->if_snd);
390 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
391 ic->ic_opmode = IEEE80211_M_STA;
393 /* set device capabilities */
395 IEEE80211_C_STA /* station mode */
396 | IEEE80211_C_MONITOR /* monitor mode */
397 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
398 | IEEE80211_C_SHSLOT /* short slot time supported */
399 | IEEE80211_C_BGSCAN /* capable of bg scanning */
400 | IEEE80211_C_WPA /* 802.11i */
404 setbit(&bands, IEEE80211_MODE_11B);
405 setbit(&bands, IEEE80211_MODE_11G);
406 ieee80211_init_channels(ic, NULL, &bands);
408 ieee80211_ifattach(ic, sc->sc_bssid);
409 ic->ic_raw_xmit = zyd_raw_xmit;
410 ic->ic_scan_start = zyd_scan_start;
411 ic->ic_scan_end = zyd_scan_end;
412 ic->ic_set_channel = zyd_set_channel;
414 ic->ic_vap_create = zyd_vap_create;
415 ic->ic_vap_delete = zyd_vap_delete;
416 ic->ic_update_mcast = zyd_update_mcast;
417 ic->ic_update_promisc = zyd_update_mcast;
419 ieee80211_radiotap_attach(ic,
420 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
421 ZYD_TX_RADIOTAP_PRESENT,
422 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
423 ZYD_RX_RADIOTAP_PRESENT);
426 ieee80211_announce(ic);
432 return (ENXIO); /* failure */
436 zyd_detach(device_t dev)
438 struct zyd_softc *sc = device_get_softc(dev);
439 struct ifnet *ifp = sc->sc_ifp;
440 struct ieee80211com *ic;
444 * Prevent further allocations from RX/TX data
448 sc->sc_flags |= ZYD_FLAG_DETACHED;
449 STAILQ_INIT(&sc->tx_q);
450 STAILQ_INIT(&sc->tx_free);
453 /* drain USB transfers */
454 for (x = 0; x != ZYD_N_TRANSFER; x++)
455 usbd_transfer_drain(sc->sc_xfer[x]);
457 /* free TX list, if any */
459 zyd_unsetup_tx_list(sc);
462 /* free USB transfers and some data buffers */
463 usbd_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER);
467 ieee80211_ifdetach(ic);
470 mtx_destroy(&sc->sc_mtx);
475 static struct ieee80211vap *
476 zyd_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
477 enum ieee80211_opmode opmode, int flags,
478 const uint8_t bssid[IEEE80211_ADDR_LEN],
479 const uint8_t mac[IEEE80211_ADDR_LEN])
482 struct ieee80211vap *vap;
484 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
486 zvp = (struct zyd_vap *) malloc(sizeof(struct zyd_vap),
487 M_80211_VAP, M_NOWAIT | M_ZERO);
492 /* enable s/w bmiss handling for sta mode */
493 if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
494 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac) != 0) {
496 free(zvp, M_80211_VAP);
500 /* override state transition machine */
501 zvp->newstate = vap->iv_newstate;
502 vap->iv_newstate = zyd_newstate;
504 ieee80211_ratectl_init(vap);
505 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
508 ieee80211_vap_attach(vap, ieee80211_media_change,
509 ieee80211_media_status);
510 ic->ic_opmode = opmode;
515 zyd_vap_delete(struct ieee80211vap *vap)
517 struct zyd_vap *zvp = ZYD_VAP(vap);
519 ieee80211_ratectl_deinit(vap);
520 ieee80211_vap_detach(vap);
521 free(zvp, M_80211_VAP);
525 zyd_tx_free(struct zyd_tx_data *data, int txerr)
527 struct zyd_softc *sc = data->sc;
529 if (data->m != NULL) {
530 if (data->m->m_flags & M_TXCB)
531 ieee80211_process_callback(data->ni, data->m,
532 txerr ? ETIMEDOUT : 0);
536 ieee80211_free_node(data->ni);
539 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
544 zyd_setup_tx_list(struct zyd_softc *sc)
546 struct zyd_tx_data *data;
550 STAILQ_INIT(&sc->tx_q);
551 STAILQ_INIT(&sc->tx_free);
553 for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
554 data = &sc->tx_data[i];
557 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
563 zyd_unsetup_tx_list(struct zyd_softc *sc)
565 struct zyd_tx_data *data;
568 /* make sure any subsequent use of the queues will fail */
570 STAILQ_INIT(&sc->tx_q);
571 STAILQ_INIT(&sc->tx_free);
573 /* free up all node references and mbufs */
574 for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
575 data = &sc->tx_data[i];
577 if (data->m != NULL) {
581 if (data->ni != NULL) {
582 ieee80211_free_node(data->ni);
589 zyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
591 struct zyd_vap *zvp = ZYD_VAP(vap);
592 struct ieee80211com *ic = vap->iv_ic;
593 struct zyd_softc *sc = ic->ic_ifp->if_softc;
596 DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__,
597 ieee80211_state_name[vap->iv_state],
598 ieee80211_state_name[nstate]);
600 IEEE80211_UNLOCK(ic);
603 case IEEE80211_S_AUTH:
604 zyd_set_chan(sc, ic->ic_curchan);
606 case IEEE80211_S_RUN:
607 if (vap->iv_opmode == IEEE80211_M_MONITOR)
610 /* turn link LED on */
611 error = zyd_set_led(sc, ZYD_LED1, 1);
615 /* make data LED blink upon Tx */
616 zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1);
618 IEEE80211_ADDR_COPY(sc->sc_bssid, vap->iv_bss->ni_bssid);
619 zyd_set_bssid(sc, sc->sc_bssid);
627 return (zvp->newstate(vap, nstate, arg));
631 * Callback handler for interrupt transfer
634 zyd_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
636 struct zyd_softc *sc = usbd_xfer_softc(xfer);
637 struct ifnet *ifp = sc->sc_ifp;
638 struct ieee80211com *ic = ifp->if_l2com;
639 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
640 struct ieee80211_node *ni;
641 struct zyd_cmd *cmd = &sc->sc_ibuf;
642 struct usb_page_cache *pc;
646 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
648 switch (USB_GET_STATE(xfer)) {
649 case USB_ST_TRANSFERRED:
650 pc = usbd_xfer_get_frame(xfer, 0);
651 usbd_copy_out(pc, 0, cmd, sizeof(*cmd));
653 switch (le16toh(cmd->code)) {
654 case ZYD_NOTIF_RETRYSTATUS:
656 struct zyd_notif_retry *retry =
657 (struct zyd_notif_retry *)cmd->data;
659 DPRINTF(sc, ZYD_DEBUG_TX_PROC,
660 "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
661 le16toh(retry->rate), ether_sprintf(retry->macaddr),
662 le16toh(retry->count)&0xff, le16toh(retry->count));
665 * Find the node to which the packet was sent and
666 * update its retry statistics. In BSS mode, this node
667 * is the AP we're associated to so no lookup is
670 ni = ieee80211_find_txnode(vap, retry->macaddr);
673 (int)(le16toh(retry->count) & 0xff);
675 ieee80211_ratectl_tx_complete(vap, ni,
676 IEEE80211_RATECTL_TX_FAILURE,
678 ieee80211_free_node(ni);
680 if (le16toh(retry->count) & 0x100)
681 ifp->if_oerrors++; /* too many retries */
688 if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
689 break; /* HMAC interrupt */
691 datalen = actlen - sizeof(cmd->code);
692 datalen -= 2; /* XXX: padding? */
694 STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
698 if (rqp->olen != datalen)
700 count = rqp->olen / sizeof(struct zyd_pair);
701 for (i = 0; i < count; i++) {
702 if (*(((const uint16_t *)rqp->idata) + i) !=
703 (((struct zyd_pair *)cmd->data) + i)->reg)
708 /* copy answer into caller-supplied buffer */
709 memcpy(rqp->odata, cmd->data, rqp->olen);
710 DPRINTF(sc, ZYD_DEBUG_CMD,
711 "command %p complete, data = %*D \n",
712 rqp, rqp->olen, (char *)rqp->odata, ":");
713 wakeup(rqp); /* wakeup caller */
717 device_printf(sc->sc_dev,
718 "unexpected IORD notification %*D\n",
719 datalen, cmd->data, ":");
724 device_printf(sc->sc_dev, "unknown notification %x\n",
731 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
732 usbd_transfer_submit(xfer);
736 DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
739 if (error != USB_ERR_CANCELLED) {
740 /* try to clear stall first */
741 usbd_xfer_set_stall(xfer);
749 zyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
751 struct zyd_softc *sc = usbd_xfer_softc(xfer);
752 struct zyd_rq *rqp, *cmd;
753 struct usb_page_cache *pc;
755 switch (USB_GET_STATE(xfer)) {
756 case USB_ST_TRANSFERRED:
757 cmd = usbd_xfer_get_priv(xfer);
758 DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd);
759 STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
760 /* Ensure the cached rq pointer is still valid */
762 (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
763 wakeup(rqp); /* wakeup caller */
769 STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
770 if (rqp->flags & ZYD_CMD_FLAG_SENT)
773 pc = usbd_xfer_get_frame(xfer, 0);
774 usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen);
776 usbd_xfer_set_frame_len(xfer, 0, rqp->ilen);
777 usbd_xfer_set_priv(xfer, rqp);
778 rqp->flags |= ZYD_CMD_FLAG_SENT;
779 usbd_transfer_submit(xfer);
785 DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
788 if (error != USB_ERR_CANCELLED) {
789 /* try to clear stall first */
790 usbd_xfer_set_stall(xfer);
798 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
799 void *odata, int olen, int flags)
805 if (ilen > (int)sizeof(cmd.data))
808 cmd.code = htole16(code);
809 memcpy(cmd.data, idata, ilen);
810 DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
811 &rq, ilen, idata, ":");
816 rq.ilen = sizeof(uint16_t) + ilen;
819 STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
820 usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
821 usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
823 /* wait at most one second for command reply */
824 error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
826 device_printf(sc->sc_dev, "command timeout\n");
827 STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
828 DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
835 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
841 error = zyd_cmd(sc, ZYD_CMD_IORD, ®, sizeof(reg), &tmp, sizeof(tmp),
844 *val = le16toh(tmp.val);
849 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
851 struct zyd_pair tmp[2];
855 regs[0] = htole16(ZYD_REG32_HI(reg));
856 regs[1] = htole16(ZYD_REG32_LO(reg));
857 error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
860 *val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
865 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
867 struct zyd_pair pair;
869 pair.reg = htole16(reg);
870 pair.val = htole16(val);
872 return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
876 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
878 struct zyd_pair pair[2];
880 pair[0].reg = htole16(ZYD_REG32_HI(reg));
881 pair[0].val = htole16(val >> 16);
882 pair[1].reg = htole16(ZYD_REG32_LO(reg));
883 pair[1].val = htole16(val & 0xffff);
885 return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
889 zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
891 struct zyd_rf *rf = &sc->sc_rf;
892 struct zyd_rfwrite_cmd req;
896 zyd_read16_m(sc, ZYD_CR203, &cr203);
897 cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
899 req.code = htole16(2);
900 req.width = htole16(rf->width);
901 for (i = 0; i < rf->width; i++) {
902 req.bit[i] = htole16(cr203);
903 if (val & (1 << (rf->width - 1 - i)))
904 req.bit[i] |= htole16(ZYD_RF_DATA);
906 error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
912 zyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
916 zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
917 zyd_write16_m(sc, ZYD_CR243, (val >> 8) & 0xff);
918 zyd_write16_m(sc, ZYD_CR242, (val >> 0) & 0xff);
924 zyd_lock_phy(struct zyd_softc *sc)
929 zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
930 tmp &= ~ZYD_UNLOCK_PHY_REGS;
931 zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
937 zyd_unlock_phy(struct zyd_softc *sc)
942 zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
943 tmp |= ZYD_UNLOCK_PHY_REGS;
944 zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
953 zyd_rfmd_init(struct zyd_rf *rf)
955 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
956 struct zyd_softc *sc = rf->rf_sc;
957 static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
958 static const uint32_t rfini[] = ZYD_RFMD_RF;
961 /* init RF-dependent PHY registers */
962 for (i = 0; i < N(phyini); i++) {
963 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
966 /* init RFMD radio */
967 for (i = 0; i < N(rfini); i++) {
968 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
977 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
980 struct zyd_softc *sc = rf->rf_sc;
982 zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
983 zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
989 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
992 struct zyd_softc *sc = rf->rf_sc;
993 static const struct {
995 } rfprog[] = ZYD_RFMD_CHANTABLE;
997 error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1000 error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1009 * AL2230 RF methods.
1012 zyd_al2230_init(struct zyd_rf *rf)
1014 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1015 struct zyd_softc *sc = rf->rf_sc;
1016 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
1017 static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1018 static const struct zyd_phy_pair phypll[] = {
1019 { ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
1020 { ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
1022 static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
1023 static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
1024 static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
1027 /* init RF-dependent PHY registers */
1028 for (i = 0; i < N(phyini); i++)
1029 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1031 if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1032 for (i = 0; i < N(phy2230s); i++)
1033 zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1036 /* init AL2230 radio */
1037 for (i = 0; i < N(rfini1); i++) {
1038 error = zyd_rfwrite(sc, rfini1[i]);
1043 if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1044 error = zyd_rfwrite(sc, 0x000824);
1046 error = zyd_rfwrite(sc, 0x0005a4);
1050 for (i = 0; i < N(rfini2); i++) {
1051 error = zyd_rfwrite(sc, rfini2[i]);
1056 for (i = 0; i < N(phypll); i++)
1057 zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
1059 for (i = 0; i < N(rfini3); i++) {
1060 error = zyd_rfwrite(sc, rfini3[i]);
1070 zyd_al2230_fini(struct zyd_rf *rf)
1072 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1074 struct zyd_softc *sc = rf->rf_sc;
1075 static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
1077 for (i = 0; i < N(phy); i++)
1078 zyd_write16_m(sc, phy[i].reg, phy[i].val);
1080 if (sc->sc_newphy != 0)
1081 zyd_write16_m(sc, ZYD_CR9, 0xe1);
1083 zyd_write16_m(sc, ZYD_CR203, 0x6);
1090 zyd_al2230_init_b(struct zyd_rf *rf)
1092 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1093 struct zyd_softc *sc = rf->rf_sc;
1094 static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1095 static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
1096 static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
1097 static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1098 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1099 static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
1100 static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
1101 static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
1102 static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
1105 for (i = 0; i < N(phy1); i++)
1106 zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1108 /* init RF-dependent PHY registers */
1109 for (i = 0; i < N(phyini); i++)
1110 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1112 if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1113 for (i = 0; i < N(phy2230s); i++)
1114 zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1117 for (i = 0; i < 3; i++) {
1118 error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
1123 for (i = 0; i < N(rfini_part1); i++) {
1124 error = zyd_rfwrite_cr(sc, rfini_part1[i]);
1129 if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1130 error = zyd_rfwrite(sc, 0x241000);
1132 error = zyd_rfwrite(sc, 0x25a000);
1136 for (i = 0; i < N(rfini_part2); i++) {
1137 error = zyd_rfwrite_cr(sc, rfini_part2[i]);
1142 for (i = 0; i < N(phy2); i++)
1143 zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
1145 for (i = 0; i < N(rfini_part3); i++) {
1146 error = zyd_rfwrite_cr(sc, rfini_part3[i]);
1151 for (i = 0; i < N(phy3); i++)
1152 zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
1154 error = zyd_al2230_fini(rf);
1161 zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1163 struct zyd_softc *sc = rf->rf_sc;
1164 int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
1166 zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
1167 zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
1173 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1175 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1177 struct zyd_softc *sc = rf->rf_sc;
1178 static const struct zyd_phy_pair phy1[] = {
1179 { ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
1181 static const struct {
1182 uint32_t r1, r2, r3;
1183 } rfprog[] = ZYD_AL2230_CHANTABLE;
1185 error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1188 error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1191 error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
1195 for (i = 0; i < N(phy1); i++)
1196 zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1203 zyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
1205 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1207 struct zyd_softc *sc = rf->rf_sc;
1208 static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1209 static const struct {
1210 uint32_t r1, r2, r3;
1211 } rfprog[] = ZYD_AL2230_CHANTABLE_B;
1213 for (i = 0; i < N(phy1); i++)
1214 zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1216 error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
1219 error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
1222 error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
1225 error = zyd_al2230_fini(rf);
1231 #define ZYD_AL2230_PHY_BANDEDGE6 \
1233 { ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 }, \
1234 { ZYD_CR47, 0x1e } \
1238 zyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
1240 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1242 struct zyd_softc *sc = rf->rf_sc;
1243 struct ifnet *ifp = sc->sc_ifp;
1244 struct ieee80211com *ic = ifp->if_l2com;
1245 struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
1246 int chan = ieee80211_chan2ieee(ic, c);
1248 if (chan == 1 || chan == 11)
1251 for (i = 0; i < N(r); i++)
1252 zyd_write16_m(sc, r[i].reg, r[i].val);
1259 * AL7230B RF methods.
1262 zyd_al7230B_init(struct zyd_rf *rf)
1264 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1265 struct zyd_softc *sc = rf->rf_sc;
1266 static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1267 static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1268 static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1269 static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1270 static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1273 /* for AL7230B, PHY and RF need to be initialized in "phases" */
1275 /* init RF-dependent PHY registers, part one */
1276 for (i = 0; i < N(phyini_1); i++)
1277 zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
1279 /* init AL7230B radio, part one */
1280 for (i = 0; i < N(rfini_1); i++) {
1281 if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1284 /* init RF-dependent PHY registers, part two */
1285 for (i = 0; i < N(phyini_2); i++)
1286 zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
1288 /* init AL7230B radio, part two */
1289 for (i = 0; i < N(rfini_2); i++) {
1290 if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1293 /* init RF-dependent PHY registers, part three */
1294 for (i = 0; i < N(phyini_3); i++)
1295 zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
1302 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1305 struct zyd_softc *sc = rf->rf_sc;
1307 zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
1308 zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1314 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1316 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1317 struct zyd_softc *sc = rf->rf_sc;
1318 static const struct {
1320 } rfprog[] = ZYD_AL7230B_CHANTABLE;
1321 static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1324 zyd_write16_m(sc, ZYD_CR240, 0x57);
1325 zyd_write16_m(sc, ZYD_CR251, 0x2f);
1327 for (i = 0; i < N(rfsc); i++) {
1328 if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1332 zyd_write16_m(sc, ZYD_CR128, 0x14);
1333 zyd_write16_m(sc, ZYD_CR129, 0x12);
1334 zyd_write16_m(sc, ZYD_CR130, 0x10);
1335 zyd_write16_m(sc, ZYD_CR38, 0x38);
1336 zyd_write16_m(sc, ZYD_CR136, 0xdf);
1338 error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1341 error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1344 error = zyd_rfwrite(sc, 0x3c9000);
1348 zyd_write16_m(sc, ZYD_CR251, 0x3f);
1349 zyd_write16_m(sc, ZYD_CR203, 0x06);
1350 zyd_write16_m(sc, ZYD_CR240, 0x08);
1357 * AL2210 RF methods.
1360 zyd_al2210_init(struct zyd_rf *rf)
1362 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1363 struct zyd_softc *sc = rf->rf_sc;
1364 static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1365 static const uint32_t rfini[] = ZYD_AL2210_RF;
1369 zyd_write32_m(sc, ZYD_CR18, 2);
1371 /* init RF-dependent PHY registers */
1372 for (i = 0; i < N(phyini); i++)
1373 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1375 /* init AL2210 radio */
1376 for (i = 0; i < N(rfini); i++) {
1377 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1380 zyd_write16_m(sc, ZYD_CR47, 0x1e);
1381 zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1382 zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1383 zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1384 zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1385 zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1386 zyd_write16_m(sc, ZYD_CR47, 0x1e);
1387 zyd_write32_m(sc, ZYD_CR18, 3);
1394 zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1396 /* vendor driver does nothing for this RF chip */
1402 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1405 struct zyd_softc *sc = rf->rf_sc;
1406 static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1409 zyd_write32_m(sc, ZYD_CR18, 2);
1410 zyd_write16_m(sc, ZYD_CR47, 0x1e);
1411 zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1412 zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1413 zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1414 zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1415 zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1416 zyd_write16_m(sc, ZYD_CR47, 0x1e);
1418 /* actually set the channel */
1419 error = zyd_rfwrite(sc, rfprog[chan - 1]);
1423 zyd_write32_m(sc, ZYD_CR18, 3);
1432 zyd_gct_init(struct zyd_rf *rf)
1434 #define ZYD_GCT_INTR_REG 0x85c1
1435 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1436 struct zyd_softc *sc = rf->rf_sc;
1437 static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1438 static const uint32_t rfini[] = ZYD_GCT_RF;
1439 static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1440 int i, idx = -1, error;
1443 /* init RF-dependent PHY registers */
1444 for (i = 0; i < N(phyini); i++)
1445 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1447 /* init cgt radio */
1448 for (i = 0; i < N(rfini); i++) {
1449 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1453 error = zyd_gct_mode(rf);
1457 for (i = 0; i < (int)(N(vco) - 1); i++) {
1458 error = zyd_gct_set_channel_synth(rf, 1, 0);
1461 error = zyd_gct_write(rf, vco[i][0]);
1464 zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf);
1465 zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data);
1466 if ((data & 0xf) == 0) {
1472 error = zyd_gct_set_channel_synth(rf, 1, 1);
1475 error = zyd_gct_write(rf, 0x6662);
1481 zyd_write16_m(sc, ZYD_CR203, 0x6);
1485 #undef ZYD_GCT_INTR_REG
1489 zyd_gct_mode(struct zyd_rf *rf)
1491 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1492 struct zyd_softc *sc = rf->rf_sc;
1493 static const uint32_t mode[] = {
1494 0x25f98, 0x25f9a, 0x25f94, 0x27fd4
1498 for (i = 0; i < N(mode); i++) {
1499 if ((error = zyd_rfwrite(sc, mode[i])) != 0)
1507 zyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal)
1509 int error, idx = chan - 1;
1510 struct zyd_softc *sc = rf->rf_sc;
1511 static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL;
1512 static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD;
1513 static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV;
1515 error = zyd_rfwrite(sc,
1516 (acal == 1) ? acal_synth[idx] : std_synth[idx]);
1519 return zyd_rfwrite(sc, div_synth[idx]);
1523 zyd_gct_write(struct zyd_rf *rf, uint16_t value)
1525 struct zyd_softc *sc = rf->rf_sc;
1527 return zyd_rfwrite(sc, 0x300000 | 0x40000 | value);
1531 zyd_gct_switch_radio(struct zyd_rf *rf, int on)
1534 struct zyd_softc *sc = rf->rf_sc;
1536 error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90);
1540 zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
1541 zyd_write16_m(sc, ZYD_CR251,
1542 on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f);
1548 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1550 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1552 struct zyd_softc *sc = rf->rf_sc;
1553 static const struct zyd_phy_pair cmd[] = {
1554 { ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 },
1555 { ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 },
1557 static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1559 error = zyd_gct_set_channel_synth(rf, chan, 0);
1562 error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 :
1563 vco[rf->idx][((chan - 1) / 2)]);
1566 error = zyd_gct_mode(rf);
1569 for (i = 0; i < N(cmd); i++)
1570 zyd_write16_m(sc, cmd[i].reg, cmd[i].val);
1571 error = zyd_gct_txgain(rf, chan);
1574 zyd_write16_m(sc, ZYD_CR203, 0x6);
1581 zyd_gct_txgain(struct zyd_rf *rf, uint8_t chan)
1583 #define N(a) (sizeof(a) / sizeof((a)[0]))
1584 struct zyd_softc *sc = rf->rf_sc;
1585 static uint32_t txgain[] = ZYD_GCT_TXGAIN;
1586 uint8_t idx = sc->sc_pwrint[chan - 1];
1588 if (idx >= N(txgain)) {
1589 device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n",
1594 return zyd_rfwrite(sc, 0x700000 | txgain[idx]);
1599 * Maxim2 RF methods.
1602 zyd_maxim2_init(struct zyd_rf *rf)
1604 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1605 struct zyd_softc *sc = rf->rf_sc;
1606 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1607 static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1611 /* init RF-dependent PHY registers */
1612 for (i = 0; i < N(phyini); i++)
1613 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1615 zyd_read16_m(sc, ZYD_CR203, &tmp);
1616 zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1618 /* init maxim2 radio */
1619 for (i = 0; i < N(rfini); i++) {
1620 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1623 zyd_read16_m(sc, ZYD_CR203, &tmp);
1624 zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1631 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1634 /* vendor driver does nothing for this RF chip */
1639 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1641 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1642 struct zyd_softc *sc = rf->rf_sc;
1643 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1644 static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1645 static const struct {
1647 } rfprog[] = ZYD_MAXIM2_CHANTABLE;
1652 * Do the same as we do when initializing it, except for the channel
1653 * values coming from the two channel tables.
1656 /* init RF-dependent PHY registers */
1657 for (i = 0; i < N(phyini); i++)
1658 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1660 zyd_read16_m(sc, ZYD_CR203, &tmp);
1661 zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1663 /* first two values taken from the chantables */
1664 error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1667 error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1671 /* init maxim2 radio - skipping the two first values */
1672 for (i = 2; i < N(rfini); i++) {
1673 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1676 zyd_read16_m(sc, ZYD_CR203, &tmp);
1677 zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1684 zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1686 struct zyd_rf *rf = &sc->sc_rf;
1693 rf->init = zyd_rfmd_init;
1694 rf->switch_radio = zyd_rfmd_switch_radio;
1695 rf->set_channel = zyd_rfmd_set_channel;
1696 rf->width = 24; /* 24-bit RF values */
1699 case ZYD_RF_AL2230S:
1700 if (sc->sc_macrev == ZYD_ZD1211B) {
1701 rf->init = zyd_al2230_init_b;
1702 rf->set_channel = zyd_al2230_set_channel_b;
1704 rf->init = zyd_al2230_init;
1705 rf->set_channel = zyd_al2230_set_channel;
1707 rf->switch_radio = zyd_al2230_switch_radio;
1708 rf->bandedge6 = zyd_al2230_bandedge6;
1709 rf->width = 24; /* 24-bit RF values */
1711 case ZYD_RF_AL7230B:
1712 rf->init = zyd_al7230B_init;
1713 rf->switch_radio = zyd_al7230B_switch_radio;
1714 rf->set_channel = zyd_al7230B_set_channel;
1715 rf->width = 24; /* 24-bit RF values */
1718 rf->init = zyd_al2210_init;
1719 rf->switch_radio = zyd_al2210_switch_radio;
1720 rf->set_channel = zyd_al2210_set_channel;
1721 rf->width = 24; /* 24-bit RF values */
1723 case ZYD_RF_MAXIM_NEW:
1725 rf->init = zyd_gct_init;
1726 rf->switch_radio = zyd_gct_switch_radio;
1727 rf->set_channel = zyd_gct_set_channel;
1728 rf->width = 24; /* 24-bit RF values */
1731 case ZYD_RF_MAXIM_NEW2:
1732 rf->init = zyd_maxim2_init;
1733 rf->switch_radio = zyd_maxim2_switch_radio;
1734 rf->set_channel = zyd_maxim2_set_channel;
1735 rf->width = 18; /* 18-bit RF values */
1738 device_printf(sc->sc_dev,
1739 "sorry, radio \"%s\" is not supported yet\n",
1747 zyd_rf_name(uint8_t type)
1749 static const char * const zyd_rfs[] = {
1750 "unknown", "unknown", "UW2451", "UCHIP", "AL2230",
1751 "AL7230B", "THETA", "AL2210", "MAXIM_NEW", "GCT",
1752 "AL2230S", "RALINK", "INTERSIL", "RFMD", "MAXIM_NEW2",
1756 return zyd_rfs[(type > 15) ? 0 : type];
1760 zyd_hw_init(struct zyd_softc *sc)
1763 const struct zyd_phy_pair *phyp;
1764 struct zyd_rf *rf = &sc->sc_rf;
1767 /* specify that the plug and play is finished */
1768 zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1769 zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
1770 DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
1773 /* retrieve firmware revision number */
1774 zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
1775 zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
1776 zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1777 /* set mandatory rates - XXX assumes 802.11b/g */
1778 zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
1780 /* disable interrupts */
1781 zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
1783 if ((error = zyd_read_pod(sc)) != 0) {
1784 device_printf(sc->sc_dev, "could not read EEPROM\n");
1788 /* PHY init (resetting) */
1789 error = zyd_lock_phy(sc);
1792 phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1793 for (; phyp->reg != 0; phyp++)
1794 zyd_write16_m(sc, phyp->reg, phyp->val);
1795 if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
1796 zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
1797 zyd_write32_m(sc, ZYD_CR157, val >> 8);
1799 error = zyd_unlock_phy(sc);
1804 zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1805 zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1806 zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
1807 zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
1808 zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
1809 zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
1810 zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
1811 zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1812 zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1813 zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1814 zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1815 zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1816 zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1817 zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1818 zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
1819 zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1820 zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1821 zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1822 zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
1823 zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
1825 if (sc->sc_macrev == ZYD_ZD1211) {
1826 zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
1827 zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1829 zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1830 zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1831 zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1832 zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1833 zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1834 zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1835 zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1836 zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
1837 zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
1840 /* init beacon interval to 100ms */
1841 if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1844 if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
1845 device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
1851 error = zyd_lock_phy(sc);
1854 error = (*rf->init)(rf);
1856 device_printf(sc->sc_dev,
1857 "radio initialization failed, error %d\n", error);
1860 error = zyd_unlock_phy(sc);
1864 if ((error = zyd_read_eeprom(sc)) != 0) {
1865 device_printf(sc->sc_dev, "could not read EEPROM\n");
1869 fail: return (error);
1873 zyd_read_pod(struct zyd_softc *sc)
1878 zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
1879 sc->sc_rfrev = tmp & 0x0f;
1880 sc->sc_ledtype = (tmp >> 4) & 0x01;
1881 sc->sc_al2230s = (tmp >> 7) & 0x01;
1882 sc->sc_cckgain = (tmp >> 8) & 0x01;
1883 sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
1884 sc->sc_parev = (tmp >> 16) & 0x0f;
1885 sc->sc_bandedge6 = (tmp >> 21) & 0x01;
1886 sc->sc_newphy = (tmp >> 31) & 0x01;
1887 sc->sc_txled = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
1893 zyd_read_eeprom(struct zyd_softc *sc)
1898 /* read Tx power calibration tables */
1899 for (i = 0; i < 7; i++) {
1900 zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1901 sc->sc_pwrcal[i * 2] = val >> 8;
1902 sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
1903 zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
1904 sc->sc_pwrint[i * 2] = val >> 8;
1905 sc->sc_pwrint[i * 2 + 1] = val & 0xff;
1906 zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
1907 sc->sc_ofdm36_cal[i * 2] = val >> 8;
1908 sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
1909 zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
1910 sc->sc_ofdm48_cal[i * 2] = val >> 8;
1911 sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
1912 zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
1913 sc->sc_ofdm54_cal[i * 2] = val >> 8;
1914 sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
1921 zyd_get_macaddr(struct zyd_softc *sc)
1923 struct usb_device_request req;
1926 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1927 req.bRequest = ZYD_READFWDATAREQ;
1928 USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
1929 USETW(req.wIndex, 0);
1930 USETW(req.wLength, IEEE80211_ADDR_LEN);
1932 error = zyd_do_request(sc, &req, sc->sc_bssid);
1934 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1935 usbd_errstr(error));
1942 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1947 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1948 zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
1949 tmp = addr[5] << 8 | addr[4];
1950 zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
1956 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1961 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1962 zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
1963 tmp = addr[5] << 8 | addr[4];
1964 zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
1970 zyd_switch_radio(struct zyd_softc *sc, int on)
1972 struct zyd_rf *rf = &sc->sc_rf;
1975 error = zyd_lock_phy(sc);
1978 error = (*rf->switch_radio)(rf, on);
1981 error = zyd_unlock_phy(sc);
1987 zyd_set_led(struct zyd_softc *sc, int which, int on)
1992 zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1996 zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
2002 zyd_set_multi(struct zyd_softc *sc)
2005 struct ifnet *ifp = sc->sc_ifp;
2006 struct ieee80211com *ic = ifp->if_l2com;
2007 struct ifmultiaddr *ifma;
2011 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2017 if (ic->ic_opmode == IEEE80211_M_MONITOR ||
2018 (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) {
2022 if_maddr_rlock(ifp);
2023 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2024 if (ifma->ifma_addr->sa_family != AF_LINK)
2026 v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
2027 ifma->ifma_addr))[5] >> 2;
2031 high |= 1 << (v - 32);
2033 if_maddr_runlock(ifp);
2036 /* reprogram multicast global hash table */
2037 zyd_write32_m(sc, ZYD_MAC_GHTBL, low);
2038 zyd_write32_m(sc, ZYD_MAC_GHTBH, high);
2041 device_printf(sc->sc_dev,
2042 "could not set multicast hash table\n");
2046 zyd_update_mcast(struct ifnet *ifp)
2048 struct zyd_softc *sc = ifp->if_softc;
2050 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2059 zyd_set_rxfilter(struct zyd_softc *sc)
2061 struct ifnet *ifp = sc->sc_ifp;
2062 struct ieee80211com *ic = ifp->if_l2com;
2065 switch (ic->ic_opmode) {
2066 case IEEE80211_M_STA:
2067 rxfilter = ZYD_FILTER_BSS;
2069 case IEEE80211_M_IBSS:
2070 case IEEE80211_M_HOSTAP:
2071 rxfilter = ZYD_FILTER_HOSTAP;
2073 case IEEE80211_M_MONITOR:
2074 rxfilter = ZYD_FILTER_MONITOR;
2077 /* should not get there */
2080 return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
2084 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
2087 struct ifnet *ifp = sc->sc_ifp;
2088 struct ieee80211com *ic = ifp->if_l2com;
2089 struct zyd_rf *rf = &sc->sc_rf;
2093 chan = ieee80211_chan2ieee(ic, c);
2094 if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
2095 /* XXX should NEVER happen */
2096 device_printf(sc->sc_dev,
2097 "%s: invalid channel %x\n", __func__, chan);
2101 error = zyd_lock_phy(sc);
2105 error = (*rf->set_channel)(rf, chan);
2109 if (rf->update_pwr) {
2110 /* update Tx power */
2111 zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
2113 if (sc->sc_macrev == ZYD_ZD1211B) {
2114 zyd_write16_m(sc, ZYD_CR67,
2115 sc->sc_ofdm36_cal[chan - 1]);
2116 zyd_write16_m(sc, ZYD_CR66,
2117 sc->sc_ofdm48_cal[chan - 1]);
2118 zyd_write16_m(sc, ZYD_CR65,
2119 sc->sc_ofdm54_cal[chan - 1]);
2120 zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
2121 zyd_write16_m(sc, ZYD_CR69, 0x28);
2122 zyd_write16_m(sc, ZYD_CR69, 0x2a);
2125 if (sc->sc_cckgain) {
2126 /* set CCK baseband gain from EEPROM */
2127 if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
2128 zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
2130 if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
2131 error = (*rf->bandedge6)(rf, c);
2135 zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
2137 error = zyd_unlock_phy(sc);
2141 sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
2142 htole16(c->ic_freq);
2143 sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
2144 htole16(c->ic_flags);
2150 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
2155 zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
2156 sc->sc_atim_wnd = val;
2157 zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
2158 sc->sc_pre_tbtt = val;
2159 sc->sc_bcn_int = bintval;
2161 if (sc->sc_bcn_int <= 5)
2163 if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
2164 sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
2165 if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
2166 sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
2168 zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
2169 zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
2170 zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
2176 zyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
2178 struct zyd_softc *sc = usbd_xfer_softc(xfer);
2179 struct ifnet *ifp = sc->sc_ifp;
2180 struct ieee80211com *ic = ifp->if_l2com;
2181 struct zyd_plcphdr plcp;
2182 struct zyd_rx_stat stat;
2183 struct usb_page_cache *pc;
2187 if (len < ZYD_MIN_FRAGSZ) {
2188 DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
2189 device_get_nameunit(sc->sc_dev), len);
2193 pc = usbd_xfer_get_frame(xfer, 0);
2194 usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2195 usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
2197 if (stat.flags & ZYD_RX_ERROR) {
2198 DPRINTF(sc, ZYD_DEBUG_RECV,
2199 "%s: RX status indicated error (%x)\n",
2200 device_get_nameunit(sc->sc_dev), stat.flags);
2205 /* compute actual frame length */
2206 rlen = len - sizeof(struct zyd_plcphdr) -
2207 sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
2209 /* allocate a mbuf to store the frame */
2210 if (rlen > (int)MCLBYTES) {
2211 DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
2212 device_get_nameunit(sc->sc_dev), rlen);
2215 } else if (rlen > (int)MHLEN)
2216 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2218 m = m_gethdr(M_NOWAIT, MT_DATA);
2220 DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
2221 device_get_nameunit(sc->sc_dev));
2225 m->m_pkthdr.rcvif = ifp;
2226 m->m_pkthdr.len = m->m_len = rlen;
2227 usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
2229 if (ieee80211_radiotap_active(ic)) {
2230 struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
2233 if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
2234 tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2235 /* XXX toss, no way to express errors */
2236 if (stat.flags & ZYD_RX_DECRYPTERR)
2237 tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2238 tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
2239 (stat.flags & ZYD_RX_OFDM) ?
2240 IEEE80211_T_OFDM : IEEE80211_T_CCK);
2241 tap->wr_antsignal = stat.rssi + -95;
2242 tap->wr_antnoise = -95; /* XXX */
2244 rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
2246 sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
2247 sc->sc_rx_data[sc->sc_rx_count].m = m;
2252 zyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
2254 struct zyd_softc *sc = usbd_xfer_softc(xfer);
2255 struct ifnet *ifp = sc->sc_ifp;
2256 struct ieee80211com *ic = ifp->if_l2com;
2257 struct ieee80211_node *ni;
2258 struct zyd_rx_desc desc;
2260 struct usb_page_cache *pc;
2267 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2269 sc->sc_rx_count = 0;
2270 switch (USB_GET_STATE(xfer)) {
2271 case USB_ST_TRANSFERRED:
2272 pc = usbd_xfer_get_frame(xfer, 0);
2273 usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
2276 if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
2277 DPRINTF(sc, ZYD_DEBUG_RECV,
2278 "%s: received multi-frame transfer\n", __func__);
2280 for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2281 uint16_t len16 = UGETW(desc.len[i]);
2283 if (len16 == 0 || len16 > actlen)
2286 zyd_rx_data(xfer, offset, len16);
2288 /* next frame is aligned on a 32-bit boundary */
2289 len16 = (len16 + 3) & ~3;
2296 DPRINTF(sc, ZYD_DEBUG_RECV,
2297 "%s: received single-frame transfer\n", __func__);
2299 zyd_rx_data(xfer, 0, actlen);
2304 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2305 usbd_transfer_submit(xfer);
2308 * At the end of a USB callback it is always safe to unlock
2309 * the private mutex of a device! That is why we do the
2310 * "ieee80211_input" here, and not some lines up!
2313 for (i = 0; i < sc->sc_rx_count; i++) {
2314 rssi = sc->sc_rx_data[i].rssi;
2315 m = sc->sc_rx_data[i].m;
2316 sc->sc_rx_data[i].m = NULL;
2320 ni = ieee80211_find_rxnode(ic,
2321 mtod(m, struct ieee80211_frame_min *));
2323 (void)ieee80211_input(ni, m, rssi, nf);
2324 ieee80211_free_node(ni);
2326 (void)ieee80211_input_all(ic, m, rssi, nf);
2328 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
2329 !IFQ_IS_EMPTY(&ifp->if_snd))
2334 default: /* Error */
2335 DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
2337 if (error != USB_ERR_CANCELLED) {
2338 /* try to clear stall first */
2339 usbd_xfer_set_stall(xfer);
2347 zyd_plcp_signal(struct zyd_softc *sc, int rate)
2350 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
2367 /* CCK rates (NB: not IEEE std, device-specific) */
2378 device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2383 zyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
2385 struct zyd_softc *sc = usbd_xfer_softc(xfer);
2386 struct ifnet *ifp = sc->sc_ifp;
2387 struct ieee80211vap *vap;
2388 struct zyd_tx_data *data;
2390 struct usb_page_cache *pc;
2393 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2395 switch (USB_GET_STATE(xfer)) {
2396 case USB_ST_TRANSFERRED:
2397 DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2400 /* free resources */
2401 data = usbd_xfer_get_priv(xfer);
2402 zyd_tx_free(data, 0);
2403 usbd_xfer_set_priv(xfer, NULL);
2406 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2411 data = STAILQ_FIRST(&sc->tx_q);
2413 STAILQ_REMOVE_HEAD(&sc->tx_q, next);
2416 if (m->m_pkthdr.len > (int)ZYD_MAX_TXBUFSZ) {
2417 DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
2419 m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
2421 pc = usbd_xfer_get_frame(xfer, 0);
2422 usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2423 usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
2426 vap = data->ni->ni_vap;
2427 if (ieee80211_radiotap_active_vap(vap)) {
2428 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2431 tap->wt_rate = data->rate;
2433 ieee80211_radiotap_tx(vap, m);
2436 usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2437 usbd_xfer_set_priv(xfer, data);
2438 usbd_transfer_submit(xfer);
2445 default: /* Error */
2446 DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2447 usbd_errstr(error));
2450 data = usbd_xfer_get_priv(xfer);
2451 usbd_xfer_set_priv(xfer, NULL);
2453 zyd_tx_free(data, error);
2455 if (error != USB_ERR_CANCELLED) {
2456 if (error == USB_ERR_TIMEOUT)
2457 device_printf(sc->sc_dev, "device timeout\n");
2460 * Try to clear stall first, also if other
2461 * errors occur, hence clearing stall
2462 * introduces a 50 ms delay:
2464 usbd_xfer_set_stall(xfer);
2472 zyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2474 struct ieee80211vap *vap = ni->ni_vap;
2475 struct ieee80211com *ic = ni->ni_ic;
2476 struct zyd_tx_desc *desc;
2477 struct zyd_tx_data *data;
2478 struct ieee80211_frame *wh;
2479 const struct ieee80211_txparam *tp;
2480 struct ieee80211_key *k;
2482 static const uint8_t ratediv[] = ZYD_TX_RATEDIV;
2487 wh = mtod(m0, struct ieee80211_frame *);
2488 data = STAILQ_FIRST(&sc->tx_free);
2489 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
2492 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT ||
2493 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) {
2494 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2495 rate = tp->mgmtrate;
2497 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
2498 /* for data frames */
2499 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
2500 rate = tp->mcastrate;
2501 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2502 rate = tp->ucastrate;
2504 (void) ieee80211_ratectl_rate(ni, NULL, 0);
2505 rate = ni->ni_txrate;
2509 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2510 k = ieee80211_crypto_encap(ni, m0);
2515 /* packet header may have moved, reset our local pointer */
2516 wh = mtod(m0, struct ieee80211_frame *);
2523 /* fill Tx descriptor */
2525 phy = zyd_plcp_signal(sc, rate);
2527 if (ZYD_RATE_IS_OFDM(rate)) {
2528 desc->phy |= ZYD_TX_PHY_OFDM;
2529 if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2530 desc->phy |= ZYD_TX_PHY_5GHZ;
2531 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2532 desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2534 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2535 desc->len = htole16(totlen);
2537 desc->flags = ZYD_TX_FLAG_BACKOFF;
2538 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2539 /* multicast frames are not sent at OFDM rates in 802.11b/g */
2540 if (totlen > vap->iv_rtsthreshold) {
2541 desc->flags |= ZYD_TX_FLAG_RTS;
2542 } else if (ZYD_RATE_IS_OFDM(rate) &&
2543 (ic->ic_flags & IEEE80211_F_USEPROT)) {
2544 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2545 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2546 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2547 desc->flags |= ZYD_TX_FLAG_RTS;
2550 desc->flags |= ZYD_TX_FLAG_MULTICAST;
2552 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2553 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2554 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2556 /* actual transmit length (XXX why +10?) */
2557 pktlen = ZYD_TX_DESC_SIZE + 10;
2558 if (sc->sc_macrev == ZYD_ZD1211)
2560 desc->pktlen = htole16(pktlen);
2562 bits = (rate == 11) ? (totlen * 16) + 10 :
2563 ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2564 desc->plcp_length = htole16(bits / ratediv[phy]);
2565 desc->plcp_service = 0;
2566 if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
2567 desc->plcp_service |= ZYD_PLCP_LENGEXT;
2570 if (ieee80211_radiotap_active_vap(vap)) {
2571 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2574 tap->wt_rate = rate;
2576 ieee80211_radiotap_tx(vap, m0);
2579 DPRINTF(sc, ZYD_DEBUG_XMIT,
2580 "%s: sending data frame len=%zu rate=%u\n",
2581 device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2584 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2585 usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
2591 zyd_start(struct ifnet *ifp)
2593 struct zyd_softc *sc = ifp->if_softc;
2594 struct ieee80211_node *ni;
2599 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2602 if (sc->tx_nfree == 0) {
2603 IFQ_DRV_PREPEND(&ifp->if_snd, m);
2604 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2607 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2608 if (zyd_tx_start(sc, m, ni) != 0) {
2609 ieee80211_free_node(ni);
2618 zyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2619 const struct ieee80211_bpf_params *params)
2621 struct ieee80211com *ic = ni->ni_ic;
2622 struct ifnet *ifp = ic->ic_ifp;
2623 struct zyd_softc *sc = ifp->if_softc;
2626 /* prevent management frames from being sent if we're not ready */
2627 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2630 ieee80211_free_node(ni);
2633 if (sc->tx_nfree == 0) {
2634 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2637 ieee80211_free_node(ni);
2638 return (ENOBUFS); /* XXX */
2642 * Legacy path; interpret frame contents to decide
2643 * precisely how to send the frame.
2646 if (zyd_tx_start(sc, m, ni) != 0) {
2649 ieee80211_free_node(ni);
2657 zyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2659 struct zyd_softc *sc = ifp->if_softc;
2660 struct ieee80211com *ic = ifp->if_l2com;
2661 struct ifreq *ifr = (struct ifreq *) data;
2666 error = (sc->sc_flags & ZYD_FLAG_DETACHED) ? ENXIO : 0;
2674 if (ifp->if_flags & IFF_UP) {
2675 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2676 zyd_init_locked(sc);
2681 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2686 ieee80211_start_all(ic);
2689 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2692 error = ether_ioctl(ifp, cmd, data);
2702 zyd_init_locked(struct zyd_softc *sc)
2704 struct ifnet *ifp = sc->sc_ifp;
2705 struct ieee80211com *ic = ifp->if_l2com;
2706 struct usb_config_descriptor *cd;
2710 ZYD_LOCK_ASSERT(sc, MA_OWNED);
2712 if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
2713 error = zyd_loadfirmware(sc);
2715 device_printf(sc->sc_dev,
2716 "could not load firmware (error=%d)\n", error);
2721 cd = usbd_get_config_descriptor(sc->sc_udev);
2722 error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
2723 cd->bConfigurationValue);
2725 device_printf(sc->sc_dev, "reset failed, continuing\n");
2727 error = zyd_hw_init(sc);
2729 device_printf(sc->sc_dev,
2730 "hardware initialization failed\n");
2734 device_printf(sc->sc_dev,
2735 "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
2736 "BE%x NP%x Gain%x F%x\n",
2737 (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
2738 sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
2739 zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
2740 sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
2741 sc->sc_cckgain, sc->sc_fix_cr157);
2743 /* read regulatory domain (currently unused) */
2744 zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
2745 sc->sc_regdomain = val >> 16;
2746 DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
2749 /* we'll do software WEP decryption for now */
2750 DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
2752 zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2754 sc->sc_flags |= ZYD_FLAG_INITONCE;
2757 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2760 DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
2761 IF_LLADDR(ifp), ":");
2762 error = zyd_set_macaddr(sc, IF_LLADDR(ifp));
2766 /* set basic rates */
2767 if (ic->ic_curmode == IEEE80211_MODE_11B)
2768 zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
2769 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2770 zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
2771 else /* assumes 802.11b/g */
2772 zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
2774 /* promiscuous mode */
2775 zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
2776 /* multicast setup */
2779 error = zyd_set_rxfilter(sc);
2783 /* switch radio transmitter ON */
2784 error = zyd_switch_radio(sc, 1);
2787 /* set default BSS channel */
2788 zyd_set_chan(sc, ic->ic_curchan);
2791 * Allocate Tx and Rx xfer queues.
2793 zyd_setup_tx_list(sc);
2795 /* enable interrupts */
2796 zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2798 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2799 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2800 usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2801 usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2802 usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
2811 zyd_init(void *priv)
2813 struct zyd_softc *sc = priv;
2814 struct ifnet *ifp = sc->sc_ifp;
2815 struct ieee80211com *ic = ifp->if_l2com;
2818 zyd_init_locked(sc);
2821 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2822 ieee80211_start_all(ic); /* start all vap's */
2826 zyd_stop(struct zyd_softc *sc)
2828 struct ifnet *ifp = sc->sc_ifp;
2831 ZYD_LOCK_ASSERT(sc, MA_OWNED);
2833 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2836 * Drain all the transfers, if not already drained:
2839 usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2840 usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
2843 zyd_unsetup_tx_list(sc);
2845 /* Stop now if the device was never set up */
2846 if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
2849 /* switch radio transmitter OFF */
2850 error = zyd_switch_radio(sc, 0);
2854 zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
2855 /* disable interrupts */
2856 zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
2863 zyd_loadfirmware(struct zyd_softc *sc)
2865 struct usb_device_request req;
2871 if (sc->sc_flags & ZYD_FLAG_FWLOADED)
2874 if (sc->sc_macrev == ZYD_ZD1211) {
2875 fw = (u_char *)zd1211_firmware;
2876 size = sizeof(zd1211_firmware);
2878 fw = (u_char *)zd1211b_firmware;
2879 size = sizeof(zd1211b_firmware);
2882 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2883 req.bRequest = ZYD_DOWNLOADREQ;
2884 USETW(req.wIndex, 0);
2886 addr = ZYD_FIRMWARE_START_ADDR;
2889 * When the transfer size is 4096 bytes, it is not
2890 * likely to be able to transfer it.
2891 * The cause is port or machine or chip?
2893 const int mlen = min(size, 64);
2895 DPRINTF(sc, ZYD_DEBUG_FW,
2896 "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
2898 USETW(req.wValue, addr);
2899 USETW(req.wLength, mlen);
2900 if (zyd_do_request(sc, &req, fw) != 0)
2908 /* check whether the upload succeeded */
2909 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2910 req.bRequest = ZYD_DOWNLOADSTS;
2911 USETW(req.wValue, 0);
2912 USETW(req.wIndex, 0);
2913 USETW(req.wLength, sizeof(stat));
2914 if (zyd_do_request(sc, &req, &stat) != 0)
2917 sc->sc_flags |= ZYD_FLAG_FWLOADED;
2919 return (stat & 0x80) ? (EIO) : (0);
2923 zyd_scan_start(struct ieee80211com *ic)
2925 struct ifnet *ifp = ic->ic_ifp;
2926 struct zyd_softc *sc = ifp->if_softc;
2929 /* want broadcast address while scanning */
2930 zyd_set_bssid(sc, ifp->if_broadcastaddr);
2935 zyd_scan_end(struct ieee80211com *ic)
2937 struct zyd_softc *sc = ic->ic_ifp->if_softc;
2940 /* restore previous bssid */
2941 zyd_set_bssid(sc, sc->sc_bssid);
2946 zyd_set_channel(struct ieee80211com *ic)
2948 struct zyd_softc *sc = ic->ic_ifp->if_softc;
2951 zyd_set_chan(sc, ic->ic_curchan);
2955 static device_method_t zyd_methods[] = {
2956 /* Device interface */
2957 DEVMETHOD(device_probe, zyd_match),
2958 DEVMETHOD(device_attach, zyd_attach),
2959 DEVMETHOD(device_detach, zyd_detach),
2963 static driver_t zyd_driver = {
2965 .methods = zyd_methods,
2966 .size = sizeof(struct zyd_softc)
2969 static devclass_t zyd_devclass;
2971 DRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0);
2972 MODULE_DEPEND(zyd, usb, 1, 1, 1);
2973 MODULE_DEPEND(zyd, wlan, 1, 1, 1);
2974 MODULE_VERSION(zyd, 1);