if_zyd.c revision 192499
1/* $OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $ */ 2/* $NetBSD: if_zyd.c,v 1.7 2007/06/21 04:04:29 kiyohara Exp $ */ 3/* $FreeBSD: head/sys/dev/usb/wlan/if_zyd.c 192499 2009-05-21 00:04:17Z thompsa $ */ 4 5/*- 6 * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr> 7 * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de> 8 * 9 * Permission to use, copy, modify, and distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 */ 21 22#include <sys/cdefs.h> 23__FBSDID("$FreeBSD: head/sys/dev/usb/wlan/if_zyd.c 192499 2009-05-21 00:04:17Z thompsa $"); 24 25/* 26 * ZyDAS ZD1211/ZD1211B USB WLAN driver. 27 */ 28 29#include <sys/param.h> 30#include <sys/sockio.h> 31#include <sys/sysctl.h> 32#include <sys/lock.h> 33#include <sys/mutex.h> 34#include <sys/mbuf.h> 35#include <sys/kernel.h> 36#include <sys/socket.h> 37#include <sys/systm.h> 38#include <sys/malloc.h> 39#include <sys/module.h> 40#include <sys/bus.h> 41#include <sys/endian.h> 42#include <sys/kdb.h> 43 44#include <machine/bus.h> 45#include <machine/resource.h> 46#include <sys/rman.h> 47 48#include <net/bpf.h> 49#include <net/if.h> 50#include <net/if_arp.h> 51#include <net/ethernet.h> 52#include <net/if_dl.h> 53#include <net/if_media.h> 54#include <net/if_types.h> 55 56#ifdef INET 57#include <netinet/in.h> 58#include <netinet/in_systm.h> 59#include <netinet/in_var.h> 60#include <netinet/if_ether.h> 61#include <netinet/ip.h> 62#endif 63 64#include <net80211/ieee80211_var.h> 65#include <net80211/ieee80211_regdomain.h> 66#include <net80211/ieee80211_radiotap.h> 67#include <net80211/ieee80211_amrr.h> 68 69#include <dev/usb/usb.h> 70#include <dev/usb/usb_error.h> 71#include <dev/usb/usb_core.h> 72#include <dev/usb/usb_lookup.h> 73#include <dev/usb/usb_debug.h> 74#include <dev/usb/usb_request.h> 75#include <dev/usb/usb_busdma.h> 76#include <dev/usb/usb_util.h> 77#include "usbdevs.h" 78 79#include <dev/usb/wlan/if_zydreg.h> 80#include <dev/usb/wlan/if_zydfw.h> 81 82#if USB_DEBUG 83static int zyd_debug = 0; 84 85SYSCTL_NODE(_hw_usb2, OID_AUTO, zyd, CTLFLAG_RW, 0, "USB zyd"); 86SYSCTL_INT(_hw_usb2_zyd, OID_AUTO, debug, CTLFLAG_RW, &zyd_debug, 0, 87 "zyd debug level"); 88 89enum { 90 ZYD_DEBUG_XMIT = 0x00000001, /* basic xmit operation */ 91 ZYD_DEBUG_RECV = 0x00000002, /* basic recv operation */ 92 ZYD_DEBUG_RESET = 0x00000004, /* reset processing */ 93 ZYD_DEBUG_INIT = 0x00000008, /* device init */ 94 ZYD_DEBUG_TX_PROC = 0x00000010, /* tx ISR proc */ 95 ZYD_DEBUG_RX_PROC = 0x00000020, /* rx ISR proc */ 96 ZYD_DEBUG_STATE = 0x00000040, /* 802.11 state transitions */ 97 ZYD_DEBUG_STAT = 0x00000080, /* statistic */ 98 ZYD_DEBUG_FW = 0x00000100, /* firmware */ 99 ZYD_DEBUG_CMD = 0x00000200, /* fw commands */ 100 ZYD_DEBUG_ANY = 0xffffffff 101}; 102#define DPRINTF(sc, m, fmt, ...) do { \ 103 if (zyd_debug & (m)) \ 104 printf("%s: " fmt, __func__, ## __VA_ARGS__); \ 105} while (0) 106#else 107#define DPRINTF(sc, m, fmt, ...) do { \ 108 (void) sc; \ 109} while (0) 110#endif 111 112#define zyd_do_request(sc,req,data) \ 113 usb2_do_request_flags((sc)->sc_udev, &(sc)->sc_mtx, req, data, 0, NULL, 5000) 114 115static device_probe_t zyd_match; 116static device_attach_t zyd_attach; 117static device_detach_t zyd_detach; 118 119static usb2_callback_t zyd_intr_read_callback; 120static usb2_callback_t zyd_intr_write_callback; 121static usb2_callback_t zyd_bulk_read_callback; 122static usb2_callback_t zyd_bulk_write_callback; 123 124static struct ieee80211vap *zyd_vap_create(struct ieee80211com *, 125 const char name[IFNAMSIZ], int unit, int opmode, 126 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN], 127 const uint8_t mac[IEEE80211_ADDR_LEN]); 128static void zyd_vap_delete(struct ieee80211vap *); 129static void zyd_tx_free(struct zyd_tx_data *, int); 130static void zyd_setup_tx_list(struct zyd_softc *); 131static void zyd_unsetup_tx_list(struct zyd_softc *); 132static struct ieee80211_node *zyd_node_alloc(struct ieee80211vap *, 133 const uint8_t mac[IEEE80211_ADDR_LEN]); 134static int zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int); 135static int zyd_cmd(struct zyd_softc *, uint16_t, const void *, int, 136 void *, int, int); 137static int zyd_read16(struct zyd_softc *, uint16_t, uint16_t *); 138static int zyd_read32(struct zyd_softc *, uint16_t, uint32_t *); 139static int zyd_write16(struct zyd_softc *, uint16_t, uint16_t); 140static int zyd_write32(struct zyd_softc *, uint16_t, uint32_t); 141static int zyd_rfwrite(struct zyd_softc *, uint32_t); 142static int zyd_lock_phy(struct zyd_softc *); 143static int zyd_unlock_phy(struct zyd_softc *); 144static int zyd_rf_attach(struct zyd_softc *, uint8_t); 145static const char *zyd_rf_name(uint8_t); 146static int zyd_hw_init(struct zyd_softc *); 147static int zyd_read_pod(struct zyd_softc *); 148static int zyd_read_eeprom(struct zyd_softc *); 149static int zyd_get_macaddr(struct zyd_softc *); 150static int zyd_set_macaddr(struct zyd_softc *, const uint8_t *); 151static int zyd_set_bssid(struct zyd_softc *, const uint8_t *); 152static int zyd_switch_radio(struct zyd_softc *, int); 153static int zyd_set_led(struct zyd_softc *, int, int); 154static void zyd_set_multi(struct zyd_softc *); 155static void zyd_update_mcast(struct ifnet *); 156static int zyd_set_rxfilter(struct zyd_softc *); 157static void zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *); 158static int zyd_set_beacon_interval(struct zyd_softc *, int); 159static void zyd_rx_data(struct usb2_xfer *, int, uint16_t); 160static int zyd_tx_mgt(struct zyd_softc *, struct mbuf *, 161 struct ieee80211_node *); 162static int zyd_tx_data(struct zyd_softc *, struct mbuf *, 163 struct ieee80211_node *); 164static void zyd_start(struct ifnet *); 165static int zyd_raw_xmit(struct ieee80211_node *, struct mbuf *, 166 const struct ieee80211_bpf_params *); 167static int zyd_ioctl(struct ifnet *, u_long, caddr_t); 168static void zyd_init_locked(struct zyd_softc *); 169static void zyd_init(void *); 170static void zyd_stop(struct zyd_softc *); 171static int zyd_loadfirmware(struct zyd_softc *); 172static void zyd_newassoc(struct ieee80211_node *, int); 173static void zyd_scan_start(struct ieee80211com *); 174static void zyd_scan_end(struct ieee80211com *); 175static void zyd_set_channel(struct ieee80211com *); 176static int zyd_rfmd_init(struct zyd_rf *); 177static int zyd_rfmd_switch_radio(struct zyd_rf *, int); 178static int zyd_rfmd_set_channel(struct zyd_rf *, uint8_t); 179static int zyd_al2230_init(struct zyd_rf *); 180static int zyd_al2230_switch_radio(struct zyd_rf *, int); 181static int zyd_al2230_set_channel(struct zyd_rf *, uint8_t); 182static int zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t); 183static int zyd_al2230_init_b(struct zyd_rf *); 184static int zyd_al7230B_init(struct zyd_rf *); 185static int zyd_al7230B_switch_radio(struct zyd_rf *, int); 186static int zyd_al7230B_set_channel(struct zyd_rf *, uint8_t); 187static int zyd_al2210_init(struct zyd_rf *); 188static int zyd_al2210_switch_radio(struct zyd_rf *, int); 189static int zyd_al2210_set_channel(struct zyd_rf *, uint8_t); 190static int zyd_gct_init(struct zyd_rf *); 191static int zyd_gct_switch_radio(struct zyd_rf *, int); 192static int zyd_gct_set_channel(struct zyd_rf *, uint8_t); 193static int zyd_maxim_init(struct zyd_rf *); 194static int zyd_maxim_switch_radio(struct zyd_rf *, int); 195static int zyd_maxim_set_channel(struct zyd_rf *, uint8_t); 196static int zyd_maxim2_init(struct zyd_rf *); 197static int zyd_maxim2_switch_radio(struct zyd_rf *, int); 198static int zyd_maxim2_set_channel(struct zyd_rf *, uint8_t); 199 200static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY; 201static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB; 202 203/* various supported device vendors/products */ 204#define ZYD_ZD1211 0 205#define ZYD_ZD1211B 1 206 207static const struct usb2_device_id zyd_devs[] = { 208 /* ZYD_ZD1211 */ 209 {USB_VPI(USB_VENDOR_3COM2, USB_PRODUCT_3COM2_3CRUSB10075, ZYD_ZD1211)}, 210 {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_WL54, ZYD_ZD1211)}, 211 {USB_VPI(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_WL159G, ZYD_ZD1211)}, 212 {USB_VPI(USB_VENDOR_CYBERTAN, USB_PRODUCT_CYBERTAN_TG54USB, ZYD_ZD1211)}, 213 {USB_VPI(USB_VENDOR_DRAYTEK, USB_PRODUCT_DRAYTEK_VIGOR550, ZYD_ZD1211)}, 214 {USB_VPI(USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54GD, ZYD_ZD1211)}, 215 {USB_VPI(USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54GZL, ZYD_ZD1211)}, 216 {USB_VPI(USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GWUS54GZ, ZYD_ZD1211)}, 217 {USB_VPI(USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GWUS54MINI, ZYD_ZD1211)}, 218 {USB_VPI(USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_XG760A, ZYD_ZD1211)}, 219 {USB_VPI(USB_VENDOR_SENAO, USB_PRODUCT_SENAO_NUB8301, ZYD_ZD1211)}, 220 {USB_VPI(USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113, ZYD_ZD1211)}, 221 {USB_VPI(USB_VENDOR_SWEEX, USB_PRODUCT_SWEEX_ZD1211, ZYD_ZD1211)}, 222 {USB_VPI(USB_VENDOR_TEKRAM, USB_PRODUCT_TEKRAM_QUICKWLAN, ZYD_ZD1211)}, 223 {USB_VPI(USB_VENDOR_TEKRAM, USB_PRODUCT_TEKRAM_ZD1211_1, ZYD_ZD1211)}, 224 {USB_VPI(USB_VENDOR_TEKRAM, USB_PRODUCT_TEKRAM_ZD1211_2, ZYD_ZD1211)}, 225 {USB_VPI(USB_VENDOR_TWINMOS, USB_PRODUCT_TWINMOS_G240, ZYD_ZD1211)}, 226 {USB_VPI(USB_VENDOR_UMEDIA, USB_PRODUCT_UMEDIA_ALL0298V2, ZYD_ZD1211)}, 227 {USB_VPI(USB_VENDOR_UMEDIA, USB_PRODUCT_UMEDIA_TEW429UB_A, ZYD_ZD1211)}, 228 {USB_VPI(USB_VENDOR_UMEDIA, USB_PRODUCT_UMEDIA_TEW429UB, ZYD_ZD1211)}, 229 {USB_VPI(USB_VENDOR_WISTRONNEWEB, USB_PRODUCT_WISTRONNEWEB_UR055G, ZYD_ZD1211)}, 230 {USB_VPI(USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_ZD1211, ZYD_ZD1211)}, 231 {USB_VPI(USB_VENDOR_ZYDAS, USB_PRODUCT_ZYDAS_ZD1211, ZYD_ZD1211)}, 232 {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_AG225H, ZYD_ZD1211)}, 233 {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_ZYAIRG220, ZYD_ZD1211)}, 234 {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_G200V2, ZYD_ZD1211)}, 235 {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_G202, ZYD_ZD1211)}, 236 /* ZYD_ZD1211B */ 237 {USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SMCWUSBG, ZYD_ZD1211B)}, 238 {USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_ZD1211B, ZYD_ZD1211B)}, 239 {USB_VPI(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_A9T_WIFI, ZYD_ZD1211B)}, 240 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050_V4000, ZYD_ZD1211B)}, 241 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_ZD1211B, ZYD_ZD1211B)}, 242 {USB_VPI(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSBF54G, ZYD_ZD1211B)}, 243 {USB_VPI(USB_VENDOR_FIBERLINE, USB_PRODUCT_FIBERLINE_WL430U, ZYD_ZD1211B)}, 244 {USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54L, ZYD_ZD1211B)}, 245 {USB_VPI(USB_VENDOR_PHILIPS, USB_PRODUCT_PHILIPS_SNU5600, ZYD_ZD1211B)}, 246 {USB_VPI(USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GW_US54GXS, ZYD_ZD1211B)}, 247 {USB_VPI(USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_XG76NA, ZYD_ZD1211B)}, 248 {USB_VPI(USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_ZD1211B, ZYD_ZD1211B)}, 249 {USB_VPI(USB_VENDOR_UMEDIA, USB_PRODUCT_UMEDIA_TEW429UBC1, ZYD_ZD1211B)}, 250 {USB_VPI(USB_VENDOR_USR, USB_PRODUCT_USR_USR5423, ZYD_ZD1211B)}, 251 {USB_VPI(USB_VENDOR_VTECH, USB_PRODUCT_VTECH_ZD1211B, ZYD_ZD1211B)}, 252 {USB_VPI(USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_ZD1211B, ZYD_ZD1211B)}, 253 {USB_VPI(USB_VENDOR_ZYDAS, USB_PRODUCT_ZYDAS_ZD1211B, ZYD_ZD1211B)}, 254 {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_M202, ZYD_ZD1211B)}, 255 {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_G220V2, ZYD_ZD1211B)}, 256}; 257 258static const struct usb2_config zyd_config[ZYD_N_TRANSFER] = { 259 [ZYD_BULK_WR] = { 260 .type = UE_BULK, 261 .endpoint = UE_ADDR_ANY, 262 .direction = UE_DIR_OUT, 263 .bufsize = ZYD_MAX_TXBUFSZ, 264 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 265 .callback = zyd_bulk_write_callback, 266 .ep_index = 0, 267 .timeout = 10000, /* 10 seconds */ 268 }, 269 [ZYD_BULK_RD] = { 270 .type = UE_BULK, 271 .endpoint = UE_ADDR_ANY, 272 .direction = UE_DIR_IN, 273 .bufsize = ZYX_MAX_RXBUFSZ, 274 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 275 .callback = zyd_bulk_read_callback, 276 .ep_index = 0, 277 }, 278 [ZYD_INTR_WR] = { 279 .type = UE_BULK_INTR, 280 .endpoint = UE_ADDR_ANY, 281 .direction = UE_DIR_OUT, 282 .bufsize = sizeof(struct zyd_cmd), 283 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 284 .callback = zyd_intr_write_callback, 285 .timeout = 1000, /* 1 second */ 286 .ep_index = 1, 287 }, 288 [ZYD_INTR_RD] = { 289 .type = UE_INTERRUPT, 290 .endpoint = UE_ADDR_ANY, 291 .direction = UE_DIR_IN, 292 .bufsize = sizeof(struct zyd_cmd), 293 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 294 .callback = zyd_intr_read_callback, 295 }, 296}; 297#define zyd_read16_m(sc, val, data) do { \ 298 error = zyd_read16(sc, val, data); \ 299 if (error != 0) \ 300 goto fail; \ 301} while (0) 302#define zyd_write16_m(sc, val, data) do { \ 303 error = zyd_write16(sc, val, data); \ 304 if (error != 0) \ 305 goto fail; \ 306} while (0) 307#define zyd_read32_m(sc, val, data) do { \ 308 error = zyd_read32(sc, val, data); \ 309 if (error != 0) \ 310 goto fail; \ 311} while (0) 312#define zyd_write32_m(sc, val, data) do { \ 313 error = zyd_write32(sc, val, data); \ 314 if (error != 0) \ 315 goto fail; \ 316} while (0) 317 318static int 319zyd_match(device_t dev) 320{ 321 struct usb2_attach_arg *uaa = device_get_ivars(dev); 322 323 if (uaa->usb_mode != USB_MODE_HOST) 324 return (ENXIO); 325 if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX) 326 return (ENXIO); 327 if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX) 328 return (ENXIO); 329 330 return (usb2_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa)); 331} 332 333static int 334zyd_attach(device_t dev) 335{ 336 struct usb2_attach_arg *uaa = device_get_ivars(dev); 337 struct zyd_softc *sc = device_get_softc(dev); 338 struct ifnet *ifp; 339 struct ieee80211com *ic; 340 uint8_t iface_index, bands; 341 int error; 342 343 if (uaa->info.bcdDevice < 0x4330) { 344 device_printf(dev, "device version mismatch: 0x%X " 345 "(only >= 43.30 supported)\n", 346 uaa->info.bcdDevice); 347 return (EINVAL); 348 } 349 350 device_set_usb2_desc(dev); 351 sc->sc_dev = dev; 352 sc->sc_udev = uaa->device; 353 sc->sc_macrev = USB_GET_DRIVER_INFO(uaa); 354 355 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), 356 MTX_NETWORK_LOCK, MTX_DEF); 357 STAILQ_INIT(&sc->sc_rqh); 358 359 iface_index = ZYD_IFACE_INDEX; 360 error = usb2_transfer_setup(uaa->device, 361 &iface_index, sc->sc_xfer, zyd_config, 362 ZYD_N_TRANSFER, sc, &sc->sc_mtx); 363 if (error) { 364 device_printf(dev, "could not allocate USB transfers, " 365 "err=%s\n", usb2_errstr(error)); 366 goto detach; 367 } 368 369 ZYD_LOCK(sc); 370 if ((error = zyd_get_macaddr(sc)) != 0) { 371 device_printf(sc->sc_dev, "could not read EEPROM\n"); 372 ZYD_UNLOCK(sc); 373 goto detach; 374 } 375 ZYD_UNLOCK(sc); 376 377 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 378 if (ifp == NULL) { 379 device_printf(sc->sc_dev, "can not if_alloc()\n"); 380 goto detach; 381 } 382 ifp->if_softc = sc; 383 if_initname(ifp, "zyd", device_get_unit(sc->sc_dev)); 384 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 385 ifp->if_init = zyd_init; 386 ifp->if_ioctl = zyd_ioctl; 387 ifp->if_start = zyd_start; 388 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 389 IFQ_SET_READY(&ifp->if_snd); 390 391 ic = ifp->if_l2com; 392 ic->ic_ifp = ifp; 393 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 394 ic->ic_opmode = IEEE80211_M_STA; 395 396 /* set device capabilities */ 397 ic->ic_caps = 398 IEEE80211_C_STA /* station mode */ 399 | IEEE80211_C_MONITOR /* monitor mode */ 400 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 401 | IEEE80211_C_SHSLOT /* short slot time supported */ 402 | IEEE80211_C_BGSCAN /* capable of bg scanning */ 403 | IEEE80211_C_WPA /* 802.11i */ 404 ; 405 406 bands = 0; 407 setbit(&bands, IEEE80211_MODE_11B); 408 setbit(&bands, IEEE80211_MODE_11G); 409 ieee80211_init_channels(ic, NULL, &bands); 410 411 ieee80211_ifattach(ic, sc->sc_bssid); 412 ic->ic_newassoc = zyd_newassoc; 413 ic->ic_raw_xmit = zyd_raw_xmit; 414 ic->ic_node_alloc = zyd_node_alloc; 415 ic->ic_scan_start = zyd_scan_start; 416 ic->ic_scan_end = zyd_scan_end; 417 ic->ic_set_channel = zyd_set_channel; 418 419 ic->ic_vap_create = zyd_vap_create; 420 ic->ic_vap_delete = zyd_vap_delete; 421 ic->ic_update_mcast = zyd_update_mcast; 422 ic->ic_update_promisc = zyd_update_mcast; 423 424 ieee80211_radiotap_attach(ic, 425 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 426 ZYD_TX_RADIOTAP_PRESENT, 427 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 428 ZYD_RX_RADIOTAP_PRESENT); 429 430 if (bootverbose) 431 ieee80211_announce(ic); 432 433 return (0); 434 435detach: 436 zyd_detach(dev); 437 return (ENXIO); /* failure */ 438} 439 440static int 441zyd_detach(device_t dev) 442{ 443 struct zyd_softc *sc = device_get_softc(dev); 444 struct ifnet *ifp = sc->sc_ifp; 445 struct ieee80211com *ic; 446 447 /* stop all USB transfers */ 448 usb2_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER); 449 450 /* free TX list, if any */ 451 zyd_unsetup_tx_list(sc); 452 453 if (ifp) { 454 ic = ifp->if_l2com; 455 ieee80211_ifdetach(ic); 456 if_free(ifp); 457 } 458 mtx_destroy(&sc->sc_mtx); 459 460 return (0); 461} 462 463static struct ieee80211vap * 464zyd_vap_create(struct ieee80211com *ic, 465 const char name[IFNAMSIZ], int unit, int opmode, int flags, 466 const uint8_t bssid[IEEE80211_ADDR_LEN], 467 const uint8_t mac[IEEE80211_ADDR_LEN]) 468{ 469 struct zyd_vap *zvp; 470 struct ieee80211vap *vap; 471 472 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 473 return (NULL); 474 zvp = (struct zyd_vap *) malloc(sizeof(struct zyd_vap), 475 M_80211_VAP, M_NOWAIT | M_ZERO); 476 if (zvp == NULL) 477 return (NULL); 478 vap = &zvp->vap; 479 /* enable s/w bmiss handling for sta mode */ 480 ieee80211_vap_setup(ic, vap, name, unit, opmode, 481 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac); 482 483 /* override state transition machine */ 484 zvp->newstate = vap->iv_newstate; 485 vap->iv_newstate = zyd_newstate; 486 487 ieee80211_amrr_init(&zvp->amrr, vap, 488 IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD, 489 IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD, 490 1000 /* 1 sec */); 491 492 /* complete setup */ 493 ieee80211_vap_attach(vap, ieee80211_media_change, 494 ieee80211_media_status); 495 ic->ic_opmode = opmode; 496 return (vap); 497} 498 499static void 500zyd_vap_delete(struct ieee80211vap *vap) 501{ 502 struct zyd_vap *zvp = ZYD_VAP(vap); 503 504 ieee80211_amrr_cleanup(&zvp->amrr); 505 ieee80211_vap_detach(vap); 506 free(zvp, M_80211_VAP); 507} 508 509static void 510zyd_tx_free(struct zyd_tx_data *data, int txerr) 511{ 512 struct zyd_softc *sc = data->sc; 513 514 if (data->m != NULL) { 515 if (data->m->m_flags & M_TXCB) 516 ieee80211_process_callback(data->ni, data->m, 517 txerr ? ETIMEDOUT : 0); 518 m_freem(data->m); 519 data->m = NULL; 520 521 ieee80211_free_node(data->ni); 522 data->ni = NULL; 523 } 524 STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 525 sc->tx_nfree++; 526} 527 528static void 529zyd_setup_tx_list(struct zyd_softc *sc) 530{ 531 struct zyd_tx_data *data; 532 int i; 533 534 sc->tx_nfree = 0; 535 STAILQ_INIT(&sc->tx_q); 536 STAILQ_INIT(&sc->tx_free); 537 538 for (i = 0; i < ZYD_TX_LIST_CNT; i++) { 539 data = &sc->tx_data[i]; 540 541 data->sc = sc; 542 STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 543 sc->tx_nfree++; 544 } 545} 546 547static void 548zyd_unsetup_tx_list(struct zyd_softc *sc) 549{ 550 struct zyd_tx_data *data; 551 int i; 552 553 /* make sure any subsequent use of the queues will fail */ 554 sc->tx_nfree = 0; 555 STAILQ_INIT(&sc->tx_q); 556 STAILQ_INIT(&sc->tx_free); 557 558 /* free up all node references and mbufs */ 559 for (i = 0; i < ZYD_TX_LIST_CNT; i++) { 560 data = &sc->tx_data[i]; 561 562 if (data->m != NULL) { 563 m_freem(data->m); 564 data->m = NULL; 565 } 566 if (data->ni != NULL) { 567 ieee80211_free_node(data->ni); 568 data->ni = NULL; 569 } 570 } 571} 572 573/* ARGUSED */ 574static struct ieee80211_node * 575zyd_node_alloc(struct ieee80211vap *vap __unused, 576 const uint8_t mac[IEEE80211_ADDR_LEN] __unused) 577{ 578 struct zyd_node *zn; 579 580 zn = malloc(sizeof(struct zyd_node), M_80211_NODE, M_NOWAIT | M_ZERO); 581 return (zn != NULL) ? (&zn->ni) : (NULL); 582} 583 584static int 585zyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 586{ 587 struct zyd_vap *zvp = ZYD_VAP(vap); 588 struct ieee80211com *ic = vap->iv_ic; 589 struct zyd_softc *sc = ic->ic_ifp->if_softc; 590 struct ieee80211_node *ni; 591 int error; 592 593 DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__, 594 ieee80211_state_name[vap->iv_state], 595 ieee80211_state_name[nstate]); 596 597 IEEE80211_UNLOCK(ic); 598 ZYD_LOCK(sc); 599 switch (nstate) { 600 case IEEE80211_S_AUTH: 601 zyd_set_chan(sc, ic->ic_curchan); 602 break; 603 case IEEE80211_S_RUN: 604 ni = vap->iv_bss; 605 if (vap->iv_opmode == IEEE80211_M_MONITOR) 606 break; 607 608 /* turn link LED on */ 609 error = zyd_set_led(sc, ZYD_LED1, 1); 610 if (error != 0) 611 break; 612 613 /* make data LED blink upon Tx */ 614 zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1); 615 616 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid); 617 zyd_set_bssid(sc, sc->sc_bssid); 618 break; 619 default: 620 break; 621 } 622fail: 623 ZYD_UNLOCK(sc); 624 IEEE80211_LOCK(ic); 625 return (zvp->newstate(vap, nstate, arg)); 626} 627 628/* 629 * Callback handler for interrupt transfer 630 */ 631static void 632zyd_intr_read_callback(struct usb2_xfer *xfer) 633{ 634 struct zyd_softc *sc = xfer->priv_sc; 635 struct ifnet *ifp = sc->sc_ifp; 636 struct ieee80211com *ic = ifp->if_l2com; 637 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 638 struct ieee80211_node *ni; 639 struct zyd_cmd *cmd = &sc->sc_ibuf; 640 int datalen; 641 642 switch (USB_GET_STATE(xfer)) { 643 case USB_ST_TRANSFERRED: 644 usb2_copy_out(xfer->frbuffers, 0, cmd, sizeof(*cmd)); 645 646 switch (le16toh(cmd->code)) { 647 case ZYD_NOTIF_RETRYSTATUS: 648 { 649 struct zyd_notif_retry *retry = 650 (struct zyd_notif_retry *)cmd->data; 651 652 DPRINTF(sc, ZYD_DEBUG_TX_PROC, 653 "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n", 654 le16toh(retry->rate), ether_sprintf(retry->macaddr), 655 le16toh(retry->count)&0xff, le16toh(retry->count)); 656 657 /* 658 * Find the node to which the packet was sent and 659 * update its retry statistics. In BSS mode, this node 660 * is the AP we're associated to so no lookup is 661 * actually needed. 662 */ 663 ni = ieee80211_find_txnode(vap, retry->macaddr); 664 if (ni != NULL) { 665 ieee80211_amrr_tx_complete(&ZYD_NODE(ni)->amn, 666 IEEE80211_AMRR_FAILURE, 1); 667 ieee80211_free_node(ni); 668 } 669 if (le16toh(retry->count) & 0x100) 670 ifp->if_oerrors++; /* too many retries */ 671 break; 672 } 673 case ZYD_NOTIF_IORD: 674 { 675 struct zyd_rq *rqp; 676 677 if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT) 678 break; /* HMAC interrupt */ 679 680 datalen = xfer->actlen - sizeof(cmd->code); 681 datalen -= 2; /* XXX: padding? */ 682 683 STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) { 684 int i, cnt; 685 686 if (rqp->olen != datalen) 687 continue; 688 cnt = rqp->olen / sizeof(struct zyd_pair); 689 for (i = 0; i < cnt; i++) { 690 if (*(((const uint16_t *)rqp->idata) + i) != 691 (((struct zyd_pair *)cmd->data) + i)->reg) 692 break; 693 } 694 if (i != cnt) 695 continue; 696 /* copy answer into caller-supplied buffer */ 697 bcopy(cmd->data, rqp->odata, rqp->olen); 698 DPRINTF(sc, ZYD_DEBUG_CMD, 699 "command %p complete, data = %*D \n", 700 rqp, rqp->olen, rqp->odata, ":"); 701 wakeup(rqp); /* wakeup caller */ 702 break; 703 } 704 if (rqp == NULL) { 705 device_printf(sc->sc_dev, 706 "unexpected IORD notification %*D\n", 707 datalen, cmd->data, ":"); 708 } 709 break; 710 } 711 default: 712 device_printf(sc->sc_dev, "unknown notification %x\n", 713 le16toh(cmd->code)); 714 } 715 716 /* FALLTHROUGH */ 717 case USB_ST_SETUP: 718tr_setup: 719 xfer->frlengths[0] = xfer->max_data_length; 720 usb2_start_hardware(xfer); 721 break; 722 723 default: /* Error */ 724 DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n", 725 usb2_errstr(xfer->error)); 726 727 if (xfer->error != USB_ERR_CANCELLED) { 728 /* try to clear stall first */ 729 xfer->flags.stall_pipe = 1; 730 goto tr_setup; 731 } 732 break; 733 } 734} 735 736static void 737zyd_intr_write_callback(struct usb2_xfer *xfer) 738{ 739 struct zyd_softc *sc = xfer->priv_sc; 740 struct zyd_rq *rqp; 741 742 switch (USB_GET_STATE(xfer)) { 743 case USB_ST_TRANSFERRED: 744 DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", 745 xfer->priv_fifo); 746 STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) { 747 /* Ensure the cached rq pointer is still valid */ 748 if (rqp == xfer->priv_fifo && 749 (rqp->flags & ZYD_CMD_FLAG_READ) == 0) 750 wakeup(rqp); /* wakeup caller */ 751 } 752 753 /* FALLTHROUGH */ 754 case USB_ST_SETUP: 755tr_setup: 756 STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) { 757 if (rqp->flags & ZYD_CMD_FLAG_SENT) 758 continue; 759 760 usb2_copy_in(xfer->frbuffers, 0, rqp->cmd, rqp->ilen); 761 762 xfer->frlengths[0] = rqp->ilen; 763 xfer->priv_fifo = rqp; 764 rqp->flags |= ZYD_CMD_FLAG_SENT; 765 usb2_start_hardware(xfer); 766 break; 767 } 768 break; 769 770 default: /* Error */ 771 DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n", 772 usb2_errstr(xfer->error)); 773 774 if (xfer->error != USB_ERR_CANCELLED) { 775 /* try to clear stall first */ 776 xfer->flags.stall_pipe = 1; 777 goto tr_setup; 778 } 779 break; 780 } 781} 782 783static int 784zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen, 785 void *odata, int olen, int flags) 786{ 787 struct zyd_cmd cmd; 788 struct zyd_rq rq; 789 int error; 790 791 if (ilen > sizeof(cmd.data)) 792 return (EINVAL); 793 794 cmd.code = htole16(code); 795 bcopy(idata, cmd.data, ilen); 796 DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n", 797 &rq, ilen, idata, ":"); 798 799 rq.cmd = &cmd; 800 rq.idata = idata; 801 rq.odata = odata; 802 rq.ilen = sizeof(uint16_t) + ilen; 803 rq.olen = olen; 804 rq.flags = flags; 805 STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq); 806 usb2_transfer_start(sc->sc_xfer[ZYD_INTR_RD]); 807 usb2_transfer_start(sc->sc_xfer[ZYD_INTR_WR]); 808 809 /* wait at most one second for command reply */ 810 error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz); 811 if (error) 812 device_printf(sc->sc_dev, "command timeout\n"); 813 STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq); 814 DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n", 815 &rq, error); 816 817 return (error); 818} 819 820static int 821zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val) 822{ 823 struct zyd_pair tmp; 824 int error; 825 826 reg = htole16(reg); 827 error = zyd_cmd(sc, ZYD_CMD_IORD, ®, sizeof(reg), &tmp, sizeof(tmp), 828 ZYD_CMD_FLAG_READ); 829 if (error == 0) 830 *val = le16toh(tmp.val); 831 return (error); 832} 833 834static int 835zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val) 836{ 837 struct zyd_pair tmp[2]; 838 uint16_t regs[2]; 839 int error; 840 841 regs[0] = htole16(ZYD_REG32_HI(reg)); 842 regs[1] = htole16(ZYD_REG32_LO(reg)); 843 error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp), 844 ZYD_CMD_FLAG_READ); 845 if (error == 0) 846 *val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val); 847 return (error); 848} 849 850static int 851zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val) 852{ 853 struct zyd_pair pair; 854 855 pair.reg = htole16(reg); 856 pair.val = htole16(val); 857 858 return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0); 859} 860 861static int 862zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val) 863{ 864 struct zyd_pair pair[2]; 865 866 pair[0].reg = htole16(ZYD_REG32_HI(reg)); 867 pair[0].val = htole16(val >> 16); 868 pair[1].reg = htole16(ZYD_REG32_LO(reg)); 869 pair[1].val = htole16(val & 0xffff); 870 871 return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0); 872} 873 874static int 875zyd_rfwrite(struct zyd_softc *sc, uint32_t val) 876{ 877 struct zyd_rf *rf = &sc->sc_rf; 878 struct zyd_rfwrite_cmd req; 879 uint16_t cr203; 880 int error, i; 881 882 zyd_read16_m(sc, ZYD_CR203, &cr203); 883 cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA); 884 885 req.code = htole16(2); 886 req.width = htole16(rf->width); 887 for (i = 0; i < rf->width; i++) { 888 req.bit[i] = htole16(cr203); 889 if (val & (1 << (rf->width - 1 - i))) 890 req.bit[i] |= htole16(ZYD_RF_DATA); 891 } 892 error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0); 893fail: 894 return (error); 895} 896 897static int 898zyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val) 899{ 900 int error; 901 902 zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff); 903 zyd_write16_m(sc, ZYD_CR243, (val >> 8) & 0xff); 904 zyd_write16_m(sc, ZYD_CR242, (val >> 0) & 0xff); 905fail: 906 return (error); 907} 908 909static int 910zyd_lock_phy(struct zyd_softc *sc) 911{ 912 int error; 913 uint32_t tmp; 914 915 zyd_read32_m(sc, ZYD_MAC_MISC, &tmp); 916 tmp &= ~ZYD_UNLOCK_PHY_REGS; 917 zyd_write32_m(sc, ZYD_MAC_MISC, tmp); 918fail: 919 return (error); 920} 921 922static int 923zyd_unlock_phy(struct zyd_softc *sc) 924{ 925 int error; 926 uint32_t tmp; 927 928 zyd_read32_m(sc, ZYD_MAC_MISC, &tmp); 929 tmp |= ZYD_UNLOCK_PHY_REGS; 930 zyd_write32_m(sc, ZYD_MAC_MISC, tmp); 931fail: 932 return (error); 933} 934 935/* 936 * RFMD RF methods. 937 */ 938static int 939zyd_rfmd_init(struct zyd_rf *rf) 940{ 941#define N(a) (sizeof(a) / sizeof((a)[0])) 942 struct zyd_softc *sc = rf->rf_sc; 943 static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY; 944 static const uint32_t rfini[] = ZYD_RFMD_RF; 945 int i, error; 946 947 /* init RF-dependent PHY registers */ 948 for (i = 0; i < N(phyini); i++) { 949 zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 950 } 951 952 /* init RFMD radio */ 953 for (i = 0; i < N(rfini); i++) { 954 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 955 return (error); 956 } 957fail: 958 return (error); 959#undef N 960} 961 962static int 963zyd_rfmd_switch_radio(struct zyd_rf *rf, int on) 964{ 965 int error; 966 struct zyd_softc *sc = rf->rf_sc; 967 968 zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15); 969 zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81); 970fail: 971 return (error); 972} 973 974static int 975zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan) 976{ 977 int error; 978 struct zyd_softc *sc = rf->rf_sc; 979 static const struct { 980 uint32_t r1, r2; 981 } rfprog[] = ZYD_RFMD_CHANTABLE; 982 983 error = zyd_rfwrite(sc, rfprog[chan - 1].r1); 984 if (error != 0) 985 goto fail; 986 error = zyd_rfwrite(sc, rfprog[chan - 1].r2); 987 if (error != 0) 988 goto fail; 989 990fail: 991 return (error); 992} 993 994/* 995 * AL2230 RF methods. 996 */ 997static int 998zyd_al2230_init(struct zyd_rf *rf) 999{ 1000#define N(a) (sizeof(a) / sizeof((a)[0])) 1001 struct zyd_softc *sc = rf->rf_sc; 1002 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY; 1003 static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT; 1004 static const struct zyd_phy_pair phypll[] = { 1005 { ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f }, 1006 { ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 } 1007 }; 1008 static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1; 1009 static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2; 1010 static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3; 1011 int i, error; 1012 1013 /* init RF-dependent PHY registers */ 1014 for (i = 0; i < N(phyini); i++) 1015 zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1016 1017 if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) { 1018 for (i = 0; i < N(phy2230s); i++) 1019 zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val); 1020 } 1021 1022 /* init AL2230 radio */ 1023 for (i = 0; i < N(rfini1); i++) { 1024 error = zyd_rfwrite(sc, rfini1[i]); 1025 if (error != 0) 1026 goto fail; 1027 } 1028 1029 if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) 1030 error = zyd_rfwrite(sc, 0x000824); 1031 else 1032 error = zyd_rfwrite(sc, 0x0005a4); 1033 if (error != 0) 1034 goto fail; 1035 1036 for (i = 0; i < N(rfini2); i++) { 1037 error = zyd_rfwrite(sc, rfini2[i]); 1038 if (error != 0) 1039 goto fail; 1040 } 1041 1042 for (i = 0; i < N(phypll); i++) 1043 zyd_write16_m(sc, phypll[i].reg, phypll[i].val); 1044 1045 for (i = 0; i < N(rfini3); i++) { 1046 error = zyd_rfwrite(sc, rfini3[i]); 1047 if (error != 0) 1048 goto fail; 1049 } 1050fail: 1051 return (error); 1052#undef N 1053} 1054 1055static int 1056zyd_al2230_fini(struct zyd_rf *rf) 1057{ 1058#define N(a) (sizeof(a) / sizeof((a)[0])) 1059 int error, i; 1060 struct zyd_softc *sc = rf->rf_sc; 1061 static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1; 1062 1063 for (i = 0; i < N(phy); i++) 1064 zyd_write16_m(sc, phy[i].reg, phy[i].val); 1065 1066 if (sc->sc_newphy != 0) 1067 zyd_write16_m(sc, ZYD_CR9, 0xe1); 1068 1069 zyd_write16_m(sc, ZYD_CR203, 0x6); 1070fail: 1071 return (error); 1072#undef N 1073} 1074 1075static int 1076zyd_al2230_init_b(struct zyd_rf *rf) 1077{ 1078#define N(a) (sizeof(a) / sizeof((a)[0])) 1079 struct zyd_softc *sc = rf->rf_sc; 1080 static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1; 1081 static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2; 1082 static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3; 1083 static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT; 1084 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B; 1085 static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1; 1086 static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2; 1087 static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3; 1088 static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE; 1089 int i, error; 1090 1091 for (i = 0; i < N(phy1); i++) 1092 zyd_write16_m(sc, phy1[i].reg, phy1[i].val); 1093 1094 /* init RF-dependent PHY registers */ 1095 for (i = 0; i < N(phyini); i++) 1096 zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1097 1098 if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) { 1099 for (i = 0; i < N(phy2230s); i++) 1100 zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val); 1101 } 1102 1103 for (i = 0; i < 3; i++) { 1104 error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]); 1105 if (error != 0) 1106 return (error); 1107 } 1108 1109 for (i = 0; i < N(rfini_part1); i++) { 1110 error = zyd_rfwrite_cr(sc, rfini_part1[i]); 1111 if (error != 0) 1112 return (error); 1113 } 1114 1115 if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) 1116 error = zyd_rfwrite(sc, 0x241000); 1117 else 1118 error = zyd_rfwrite(sc, 0x25a000); 1119 if (error != 0) 1120 goto fail; 1121 1122 for (i = 0; i < N(rfini_part2); i++) { 1123 error = zyd_rfwrite_cr(sc, rfini_part2[i]); 1124 if (error != 0) 1125 return (error); 1126 } 1127 1128 for (i = 0; i < N(phy2); i++) 1129 zyd_write16_m(sc, phy2[i].reg, phy2[i].val); 1130 1131 for (i = 0; i < N(rfini_part3); i++) { 1132 error = zyd_rfwrite_cr(sc, rfini_part3[i]); 1133 if (error != 0) 1134 return (error); 1135 } 1136 1137 for (i = 0; i < N(phy3); i++) 1138 zyd_write16_m(sc, phy3[i].reg, phy3[i].val); 1139 1140 error = zyd_al2230_fini(rf); 1141fail: 1142 return (error); 1143#undef N 1144} 1145 1146static int 1147zyd_al2230_switch_radio(struct zyd_rf *rf, int on) 1148{ 1149 struct zyd_softc *sc = rf->rf_sc; 1150 int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f; 1151 1152 zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04); 1153 zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f); 1154fail: 1155 return (error); 1156} 1157 1158static int 1159zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan) 1160{ 1161#define N(a) (sizeof(a) / sizeof((a)[0])) 1162 int error, i; 1163 struct zyd_softc *sc = rf->rf_sc; 1164 static const struct zyd_phy_pair phy1[] = { 1165 { ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }, 1166 }; 1167 static const struct { 1168 uint32_t r1, r2, r3; 1169 } rfprog[] = ZYD_AL2230_CHANTABLE; 1170 1171 error = zyd_rfwrite(sc, rfprog[chan - 1].r1); 1172 if (error != 0) 1173 goto fail; 1174 error = zyd_rfwrite(sc, rfprog[chan - 1].r2); 1175 if (error != 0) 1176 goto fail; 1177 error = zyd_rfwrite(sc, rfprog[chan - 1].r3); 1178 if (error != 0) 1179 goto fail; 1180 1181 for (i = 0; i < N(phy1); i++) 1182 zyd_write16_m(sc, phy1[i].reg, phy1[i].val); 1183fail: 1184 return (error); 1185#undef N 1186} 1187 1188static int 1189zyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan) 1190{ 1191#define N(a) (sizeof(a) / sizeof((a)[0])) 1192 int error, i; 1193 struct zyd_softc *sc = rf->rf_sc; 1194 static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1; 1195 static const struct { 1196 uint32_t r1, r2, r3; 1197 } rfprog[] = ZYD_AL2230_CHANTABLE_B; 1198 1199 for (i = 0; i < N(phy1); i++) 1200 zyd_write16_m(sc, phy1[i].reg, phy1[i].val); 1201 1202 error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1); 1203 if (error != 0) 1204 goto fail; 1205 error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2); 1206 if (error != 0) 1207 goto fail; 1208 error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3); 1209 if (error != 0) 1210 goto fail; 1211 error = zyd_al2230_fini(rf); 1212fail: 1213 return (error); 1214#undef N 1215} 1216 1217#define ZYD_AL2230_PHY_BANDEDGE6 \ 1218{ \ 1219 { ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 }, \ 1220 { ZYD_CR47, 0x1e } \ 1221} 1222 1223static int 1224zyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c) 1225{ 1226#define N(a) (sizeof(a) / sizeof((a)[0])) 1227 int error = 0, i; 1228 struct zyd_softc *sc = rf->rf_sc; 1229 struct ifnet *ifp = sc->sc_ifp; 1230 struct ieee80211com *ic = ifp->if_l2com; 1231 struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6; 1232 int chan = ieee80211_chan2ieee(ic, c); 1233 1234 if (chan == 1 || chan == 11) 1235 r[0].val = 0x12; 1236 1237 for (i = 0; i < N(r); i++) 1238 zyd_write16_m(sc, r[i].reg, r[i].val); 1239fail: 1240 return (error); 1241#undef N 1242} 1243 1244/* 1245 * AL7230B RF methods. 1246 */ 1247static int 1248zyd_al7230B_init(struct zyd_rf *rf) 1249{ 1250#define N(a) (sizeof(a) / sizeof((a)[0])) 1251 struct zyd_softc *sc = rf->rf_sc; 1252 static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1; 1253 static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2; 1254 static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3; 1255 static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1; 1256 static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2; 1257 int i, error; 1258 1259 /* for AL7230B, PHY and RF need to be initialized in "phases" */ 1260 1261 /* init RF-dependent PHY registers, part one */ 1262 for (i = 0; i < N(phyini_1); i++) 1263 zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val); 1264 1265 /* init AL7230B radio, part one */ 1266 for (i = 0; i < N(rfini_1); i++) { 1267 if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0) 1268 return (error); 1269 } 1270 /* init RF-dependent PHY registers, part two */ 1271 for (i = 0; i < N(phyini_2); i++) 1272 zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val); 1273 1274 /* init AL7230B radio, part two */ 1275 for (i = 0; i < N(rfini_2); i++) { 1276 if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0) 1277 return (error); 1278 } 1279 /* init RF-dependent PHY registers, part three */ 1280 for (i = 0; i < N(phyini_3); i++) 1281 zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val); 1282fail: 1283 return (error); 1284#undef N 1285} 1286 1287static int 1288zyd_al7230B_switch_radio(struct zyd_rf *rf, int on) 1289{ 1290 int error; 1291 struct zyd_softc *sc = rf->rf_sc; 1292 1293 zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04); 1294 zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f); 1295fail: 1296 return (error); 1297} 1298 1299static int 1300zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan) 1301{ 1302#define N(a) (sizeof(a) / sizeof((a)[0])) 1303 struct zyd_softc *sc = rf->rf_sc; 1304 static const struct { 1305 uint32_t r1, r2; 1306 } rfprog[] = ZYD_AL7230B_CHANTABLE; 1307 static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL; 1308 int i, error; 1309 1310 zyd_write16_m(sc, ZYD_CR240, 0x57); 1311 zyd_write16_m(sc, ZYD_CR251, 0x2f); 1312 1313 for (i = 0; i < N(rfsc); i++) { 1314 if ((error = zyd_rfwrite(sc, rfsc[i])) != 0) 1315 return (error); 1316 } 1317 1318 zyd_write16_m(sc, ZYD_CR128, 0x14); 1319 zyd_write16_m(sc, ZYD_CR129, 0x12); 1320 zyd_write16_m(sc, ZYD_CR130, 0x10); 1321 zyd_write16_m(sc, ZYD_CR38, 0x38); 1322 zyd_write16_m(sc, ZYD_CR136, 0xdf); 1323 1324 error = zyd_rfwrite(sc, rfprog[chan - 1].r1); 1325 if (error != 0) 1326 goto fail; 1327 error = zyd_rfwrite(sc, rfprog[chan - 1].r2); 1328 if (error != 0) 1329 goto fail; 1330 error = zyd_rfwrite(sc, 0x3c9000); 1331 if (error != 0) 1332 goto fail; 1333 1334 zyd_write16_m(sc, ZYD_CR251, 0x3f); 1335 zyd_write16_m(sc, ZYD_CR203, 0x06); 1336 zyd_write16_m(sc, ZYD_CR240, 0x08); 1337fail: 1338 return (error); 1339#undef N 1340} 1341 1342/* 1343 * AL2210 RF methods. 1344 */ 1345static int 1346zyd_al2210_init(struct zyd_rf *rf) 1347{ 1348#define N(a) (sizeof(a) / sizeof((a)[0])) 1349 struct zyd_softc *sc = rf->rf_sc; 1350 static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY; 1351 static const uint32_t rfini[] = ZYD_AL2210_RF; 1352 uint32_t tmp; 1353 int i, error; 1354 1355 zyd_write32_m(sc, ZYD_CR18, 2); 1356 1357 /* init RF-dependent PHY registers */ 1358 for (i = 0; i < N(phyini); i++) 1359 zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1360 1361 /* init AL2210 radio */ 1362 for (i = 0; i < N(rfini); i++) { 1363 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1364 return (error); 1365 } 1366 zyd_write16_m(sc, ZYD_CR47, 0x1e); 1367 zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp); 1368 zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1); 1369 zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1); 1370 zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05); 1371 zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00); 1372 zyd_write16_m(sc, ZYD_CR47, 0x1e); 1373 zyd_write32_m(sc, ZYD_CR18, 3); 1374fail: 1375 return (error); 1376#undef N 1377} 1378 1379static int 1380zyd_al2210_switch_radio(struct zyd_rf *rf, int on) 1381{ 1382 /* vendor driver does nothing for this RF chip */ 1383 1384 return (0); 1385} 1386 1387static int 1388zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan) 1389{ 1390 int error; 1391 struct zyd_softc *sc = rf->rf_sc; 1392 static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE; 1393 uint32_t tmp; 1394 1395 zyd_write32_m(sc, ZYD_CR18, 2); 1396 zyd_write16_m(sc, ZYD_CR47, 0x1e); 1397 zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp); 1398 zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1); 1399 zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1); 1400 zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05); 1401 zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00); 1402 zyd_write16_m(sc, ZYD_CR47, 0x1e); 1403 1404 /* actually set the channel */ 1405 error = zyd_rfwrite(sc, rfprog[chan - 1]); 1406 if (error != 0) 1407 goto fail; 1408 1409 zyd_write32_m(sc, ZYD_CR18, 3); 1410fail: 1411 return (error); 1412} 1413 1414/* 1415 * GCT RF methods. 1416 */ 1417static int 1418zyd_gct_init(struct zyd_rf *rf) 1419{ 1420#define N(a) (sizeof(a) / sizeof((a)[0])) 1421 struct zyd_softc *sc = rf->rf_sc; 1422 static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY; 1423 static const uint32_t rfini[] = ZYD_GCT_RF; 1424 int i, error; 1425 1426 /* init RF-dependent PHY registers */ 1427 for (i = 0; i < N(phyini); i++) 1428 zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1429 1430 /* init cgt radio */ 1431 for (i = 0; i < N(rfini); i++) { 1432 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1433 return (error); 1434 } 1435fail: 1436 return (error); 1437#undef N 1438} 1439 1440static int 1441zyd_gct_switch_radio(struct zyd_rf *rf, int on) 1442{ 1443 /* vendor driver does nothing for this RF chip */ 1444 1445 return (0); 1446} 1447 1448static int 1449zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan) 1450{ 1451 int error; 1452 struct zyd_softc *sc = rf->rf_sc; 1453 static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE; 1454 1455 error = zyd_rfwrite(sc, 0x1c0000); 1456 if (error != 0) 1457 goto fail; 1458 error = zyd_rfwrite(sc, rfprog[chan - 1]); 1459 if (error != 0) 1460 goto fail; 1461 error = zyd_rfwrite(sc, 0x1c0008); 1462fail: 1463 return (error); 1464} 1465 1466/* 1467 * Maxim RF methods. 1468 */ 1469static int 1470zyd_maxim_init(struct zyd_rf *rf) 1471{ 1472#define N(a) (sizeof(a) / sizeof((a)[0])) 1473 struct zyd_softc *sc = rf->rf_sc; 1474 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY; 1475 static const uint32_t rfini[] = ZYD_MAXIM_RF; 1476 uint16_t tmp; 1477 int i, error; 1478 1479 /* init RF-dependent PHY registers */ 1480 for (i = 0; i < N(phyini); i++) 1481 zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1482 1483 zyd_read16_m(sc, ZYD_CR203, &tmp); 1484 zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4)); 1485 1486 /* init maxim radio */ 1487 for (i = 0; i < N(rfini); i++) { 1488 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1489 return (error); 1490 } 1491 zyd_read16_m(sc, ZYD_CR203, &tmp); 1492 zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4)); 1493fail: 1494 return (error); 1495#undef N 1496} 1497 1498static int 1499zyd_maxim_switch_radio(struct zyd_rf *rf, int on) 1500{ 1501 1502 /* vendor driver does nothing for this RF chip */ 1503 return (0); 1504} 1505 1506static int 1507zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan) 1508{ 1509#define N(a) (sizeof(a) / sizeof((a)[0])) 1510 struct zyd_softc *sc = rf->rf_sc; 1511 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY; 1512 static const uint32_t rfini[] = ZYD_MAXIM_RF; 1513 static const struct { 1514 uint32_t r1, r2; 1515 } rfprog[] = ZYD_MAXIM_CHANTABLE; 1516 uint16_t tmp; 1517 int i, error; 1518 1519 /* 1520 * Do the same as we do when initializing it, except for the channel 1521 * values coming from the two channel tables. 1522 */ 1523 1524 /* init RF-dependent PHY registers */ 1525 for (i = 0; i < N(phyini); i++) 1526 zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1527 1528 zyd_read16_m(sc, ZYD_CR203, &tmp); 1529 zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4)); 1530 1531 /* first two values taken from the chantables */ 1532 error = zyd_rfwrite(sc, rfprog[chan - 1].r1); 1533 if (error != 0) 1534 goto fail; 1535 error = zyd_rfwrite(sc, rfprog[chan - 1].r2); 1536 if (error != 0) 1537 goto fail; 1538 1539 /* init maxim radio - skipping the two first values */ 1540 for (i = 2; i < N(rfini); i++) { 1541 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1542 return (error); 1543 } 1544 zyd_read16_m(sc, ZYD_CR203, &tmp); 1545 zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4)); 1546fail: 1547 return (error); 1548#undef N 1549} 1550 1551/* 1552 * Maxim2 RF methods. 1553 */ 1554static int 1555zyd_maxim2_init(struct zyd_rf *rf) 1556{ 1557#define N(a) (sizeof(a) / sizeof((a)[0])) 1558 struct zyd_softc *sc = rf->rf_sc; 1559 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY; 1560 static const uint32_t rfini[] = ZYD_MAXIM2_RF; 1561 uint16_t tmp; 1562 int i, error; 1563 1564 /* init RF-dependent PHY registers */ 1565 for (i = 0; i < N(phyini); i++) 1566 zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1567 1568 zyd_read16_m(sc, ZYD_CR203, &tmp); 1569 zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4)); 1570 1571 /* init maxim2 radio */ 1572 for (i = 0; i < N(rfini); i++) { 1573 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1574 return (error); 1575 } 1576 zyd_read16_m(sc, ZYD_CR203, &tmp); 1577 zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4)); 1578fail: 1579 return (error); 1580#undef N 1581} 1582 1583static int 1584zyd_maxim2_switch_radio(struct zyd_rf *rf, int on) 1585{ 1586 1587 /* vendor driver does nothing for this RF chip */ 1588 return (0); 1589} 1590 1591static int 1592zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan) 1593{ 1594#define N(a) (sizeof(a) / sizeof((a)[0])) 1595 struct zyd_softc *sc = rf->rf_sc; 1596 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY; 1597 static const uint32_t rfini[] = ZYD_MAXIM2_RF; 1598 static const struct { 1599 uint32_t r1, r2; 1600 } rfprog[] = ZYD_MAXIM2_CHANTABLE; 1601 uint16_t tmp; 1602 int i, error; 1603 1604 /* 1605 * Do the same as we do when initializing it, except for the channel 1606 * values coming from the two channel tables. 1607 */ 1608 1609 /* init RF-dependent PHY registers */ 1610 for (i = 0; i < N(phyini); i++) 1611 zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1612 1613 zyd_read16_m(sc, ZYD_CR203, &tmp); 1614 zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4)); 1615 1616 /* first two values taken from the chantables */ 1617 error = zyd_rfwrite(sc, rfprog[chan - 1].r1); 1618 if (error != 0) 1619 goto fail; 1620 error = zyd_rfwrite(sc, rfprog[chan - 1].r2); 1621 if (error != 0) 1622 goto fail; 1623 1624 /* init maxim2 radio - skipping the two first values */ 1625 for (i = 2; i < N(rfini); i++) { 1626 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1627 return (error); 1628 } 1629 zyd_read16_m(sc, ZYD_CR203, &tmp); 1630 zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4)); 1631fail: 1632 return (error); 1633#undef N 1634} 1635 1636static int 1637zyd_rf_attach(struct zyd_softc *sc, uint8_t type) 1638{ 1639 struct zyd_rf *rf = &sc->sc_rf; 1640 1641 rf->rf_sc = sc; 1642 1643 switch (type) { 1644 case ZYD_RF_RFMD: 1645 rf->init = zyd_rfmd_init; 1646 rf->switch_radio = zyd_rfmd_switch_radio; 1647 rf->set_channel = zyd_rfmd_set_channel; 1648 rf->width = 24; /* 24-bit RF values */ 1649 break; 1650 case ZYD_RF_AL2230: 1651 case ZYD_RF_AL2230S: 1652 if (sc->sc_macrev == ZYD_ZD1211B) { 1653 rf->init = zyd_al2230_init_b; 1654 rf->set_channel = zyd_al2230_set_channel_b; 1655 } else { 1656 rf->init = zyd_al2230_init; 1657 rf->set_channel = zyd_al2230_set_channel; 1658 } 1659 rf->switch_radio = zyd_al2230_switch_radio; 1660 rf->bandedge6 = zyd_al2230_bandedge6; 1661 rf->width = 24; /* 24-bit RF values */ 1662 break; 1663 case ZYD_RF_AL7230B: 1664 rf->init = zyd_al7230B_init; 1665 rf->switch_radio = zyd_al7230B_switch_radio; 1666 rf->set_channel = zyd_al7230B_set_channel; 1667 rf->width = 24; /* 24-bit RF values */ 1668 break; 1669 case ZYD_RF_AL2210: 1670 rf->init = zyd_al2210_init; 1671 rf->switch_radio = zyd_al2210_switch_radio; 1672 rf->set_channel = zyd_al2210_set_channel; 1673 rf->width = 24; /* 24-bit RF values */ 1674 break; 1675 case ZYD_RF_GCT: 1676 rf->init = zyd_gct_init; 1677 rf->switch_radio = zyd_gct_switch_radio; 1678 rf->set_channel = zyd_gct_set_channel; 1679 rf->width = 21; /* 21-bit RF values */ 1680 break; 1681 case ZYD_RF_MAXIM_NEW: 1682 rf->init = zyd_maxim_init; 1683 rf->switch_radio = zyd_maxim_switch_radio; 1684 rf->set_channel = zyd_maxim_set_channel; 1685 rf->width = 18; /* 18-bit RF values */ 1686 break; 1687 case ZYD_RF_MAXIM_NEW2: 1688 rf->init = zyd_maxim2_init; 1689 rf->switch_radio = zyd_maxim2_switch_radio; 1690 rf->set_channel = zyd_maxim2_set_channel; 1691 rf->width = 18; /* 18-bit RF values */ 1692 break; 1693 default: 1694 device_printf(sc->sc_dev, 1695 "sorry, radio \"%s\" is not supported yet\n", 1696 zyd_rf_name(type)); 1697 return (EINVAL); 1698 } 1699 return (0); 1700} 1701 1702static const char * 1703zyd_rf_name(uint8_t type) 1704{ 1705 static const char * const zyd_rfs[] = { 1706 "unknown", "unknown", "UW2451", "UCHIP", "AL2230", 1707 "AL7230B", "THETA", "AL2210", "MAXIM_NEW", "GCT", 1708 "AL2230S", "RALINK", "INTERSIL", "RFMD", "MAXIM_NEW2", 1709 "PHILIPS" 1710 }; 1711 1712 return zyd_rfs[(type > 15) ? 0 : type]; 1713} 1714 1715static int 1716zyd_hw_init(struct zyd_softc *sc) 1717{ 1718 int error; 1719 const struct zyd_phy_pair *phyp; 1720 struct zyd_rf *rf = &sc->sc_rf; 1721 uint16_t val; 1722 1723 /* specify that the plug and play is finished */ 1724 zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1); 1725 zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase); 1726 DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n", 1727 sc->sc_fwbase); 1728 1729 /* retrieve firmware revision number */ 1730 zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev); 1731 zyd_write32_m(sc, ZYD_CR_GPI_EN, 0); 1732 zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f); 1733 /* set mandatory rates - XXX assumes 802.11b/g */ 1734 zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f); 1735 1736 /* disable interrupts */ 1737 zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0); 1738 1739 if ((error = zyd_read_pod(sc)) != 0) { 1740 device_printf(sc->sc_dev, "could not read EEPROM\n"); 1741 goto fail; 1742 } 1743 1744 /* PHY init (resetting) */ 1745 error = zyd_lock_phy(sc); 1746 if (error != 0) 1747 goto fail; 1748 phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy; 1749 for (; phyp->reg != 0; phyp++) 1750 zyd_write16_m(sc, phyp->reg, phyp->val); 1751 if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) { 1752 zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val); 1753 zyd_write32_m(sc, ZYD_CR157, val >> 8); 1754 } 1755 error = zyd_unlock_phy(sc); 1756 if (error != 0) 1757 goto fail; 1758 1759 /* HMAC init */ 1760 zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020); 1761 zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808); 1762 zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000); 1763 zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000); 1764 zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000); 1765 zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000); 1766 zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4); 1767 zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f); 1768 zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401); 1769 zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000); 1770 zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080); 1771 zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000); 1772 zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100); 1773 zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070); 1774 zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000); 1775 zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203); 1776 zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1); 1777 zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114); 1778 zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032); 1779 zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3); 1780 1781 if (sc->sc_macrev == ZYD_ZD1211) { 1782 zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002); 1783 zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640); 1784 } else { 1785 zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202); 1786 zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f); 1787 zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f); 1788 zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f); 1789 zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f); 1790 zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028); 1791 zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C); 1792 zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824); 1793 zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff); 1794 } 1795 1796 /* init beacon interval to 100ms */ 1797 if ((error = zyd_set_beacon_interval(sc, 100)) != 0) 1798 goto fail; 1799 1800 if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) { 1801 device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n", 1802 sc->sc_rfrev); 1803 goto fail; 1804 } 1805 1806 /* RF chip init */ 1807 error = zyd_lock_phy(sc); 1808 if (error != 0) 1809 goto fail; 1810 error = (*rf->init)(rf); 1811 if (error != 0) { 1812 device_printf(sc->sc_dev, 1813 "radio initialization failed, error %d\n", error); 1814 goto fail; 1815 } 1816 error = zyd_unlock_phy(sc); 1817 if (error != 0) 1818 goto fail; 1819 1820 if ((error = zyd_read_eeprom(sc)) != 0) { 1821 device_printf(sc->sc_dev, "could not read EEPROM\n"); 1822 goto fail; 1823 } 1824 1825fail: return (error); 1826} 1827 1828static int 1829zyd_read_pod(struct zyd_softc *sc) 1830{ 1831 int error; 1832 uint32_t tmp; 1833 1834 zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp); 1835 sc->sc_rfrev = tmp & 0x0f; 1836 sc->sc_ledtype = (tmp >> 4) & 0x01; 1837 sc->sc_al2230s = (tmp >> 7) & 0x01; 1838 sc->sc_cckgain = (tmp >> 8) & 0x01; 1839 sc->sc_fix_cr157 = (tmp >> 13) & 0x01; 1840 sc->sc_parev = (tmp >> 16) & 0x0f; 1841 sc->sc_bandedge6 = (tmp >> 21) & 0x01; 1842 sc->sc_newphy = (tmp >> 31) & 0x01; 1843 sc->sc_txled = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1; 1844fail: 1845 return (error); 1846} 1847 1848static int 1849zyd_read_eeprom(struct zyd_softc *sc) 1850{ 1851 uint16_t val; 1852 int error, i; 1853 1854 /* read Tx power calibration tables */ 1855 for (i = 0; i < 7; i++) { 1856 zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val); 1857 sc->sc_pwrcal[i * 2] = val >> 8; 1858 sc->sc_pwrcal[i * 2 + 1] = val & 0xff; 1859 zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val); 1860 sc->sc_pwrint[i * 2] = val >> 8; 1861 sc->sc_pwrint[i * 2 + 1] = val & 0xff; 1862 zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val); 1863 sc->sc_ofdm36_cal[i * 2] = val >> 8; 1864 sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff; 1865 zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val); 1866 sc->sc_ofdm48_cal[i * 2] = val >> 8; 1867 sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff; 1868 zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val); 1869 sc->sc_ofdm54_cal[i * 2] = val >> 8; 1870 sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff; 1871 } 1872fail: 1873 return (error); 1874} 1875 1876static int 1877zyd_get_macaddr(struct zyd_softc *sc) 1878{ 1879 struct usb2_device_request req; 1880 usb2_error_t error; 1881 1882 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1883 req.bRequest = ZYD_READFWDATAREQ; 1884 USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1); 1885 USETW(req.wIndex, 0); 1886 USETW(req.wLength, IEEE80211_ADDR_LEN); 1887 1888 error = zyd_do_request(sc, &req, sc->sc_bssid); 1889 if (error != 0) { 1890 device_printf(sc->sc_dev, "could not read EEPROM: %s\n", 1891 usb2_errstr(error)); 1892 } 1893 1894 return (error); 1895} 1896 1897static int 1898zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr) 1899{ 1900 int error; 1901 uint32_t tmp; 1902 1903 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]; 1904 zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp); 1905 tmp = addr[5] << 8 | addr[4]; 1906 zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp); 1907fail: 1908 return (error); 1909} 1910 1911static int 1912zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr) 1913{ 1914 int error; 1915 uint32_t tmp; 1916 1917 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]; 1918 zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp); 1919 tmp = addr[5] << 8 | addr[4]; 1920 zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp); 1921fail: 1922 return (error); 1923} 1924 1925static int 1926zyd_switch_radio(struct zyd_softc *sc, int on) 1927{ 1928 struct zyd_rf *rf = &sc->sc_rf; 1929 int error; 1930 1931 error = zyd_lock_phy(sc); 1932 if (error != 0) 1933 goto fail; 1934 error = (*rf->switch_radio)(rf, on); 1935 if (error != 0) 1936 goto fail; 1937 error = zyd_unlock_phy(sc); 1938fail: 1939 return (error); 1940} 1941 1942static int 1943zyd_set_led(struct zyd_softc *sc, int which, int on) 1944{ 1945 int error; 1946 uint32_t tmp; 1947 1948 zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp); 1949 tmp &= ~which; 1950 if (on) 1951 tmp |= which; 1952 zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp); 1953fail: 1954 return (error); 1955} 1956 1957static void 1958zyd_set_multi(struct zyd_softc *sc) 1959{ 1960 int error; 1961 struct ifnet *ifp = sc->sc_ifp; 1962 struct ieee80211com *ic = ifp->if_l2com; 1963 struct ifmultiaddr *ifma; 1964 uint32_t low, high; 1965 uint8_t v; 1966 1967 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1968 return; 1969 1970 low = 0x00000000; 1971 high = 0x80000000; 1972 1973 if (ic->ic_opmode == IEEE80211_M_MONITOR || 1974 (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) { 1975 low = 0xffffffff; 1976 high = 0xffffffff; 1977 } else { 1978 IF_ADDR_LOCK(ifp); 1979 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1980 if (ifma->ifma_addr->sa_family != AF_LINK) 1981 continue; 1982 v = ((uint8_t *)LLADDR((struct sockaddr_dl *) 1983 ifma->ifma_addr))[5] >> 2; 1984 if (v < 32) 1985 low |= 1 << v; 1986 else 1987 high |= 1 << (v - 32); 1988 } 1989 IF_ADDR_UNLOCK(ifp); 1990 } 1991 1992 /* reprogram multicast global hash table */ 1993 zyd_write32_m(sc, ZYD_MAC_GHTBL, low); 1994 zyd_write32_m(sc, ZYD_MAC_GHTBH, high); 1995fail: 1996 if (error != 0) 1997 device_printf(sc->sc_dev, 1998 "could not set multicast hash table\n"); 1999} 2000 2001static void 2002zyd_update_mcast(struct ifnet *ifp) 2003{ 2004 struct zyd_softc *sc = ifp->if_softc; 2005 2006 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 2007 return; 2008 2009 ZYD_LOCK(sc); 2010 zyd_set_multi(sc); 2011 ZYD_UNLOCK(sc); 2012} 2013 2014static int 2015zyd_set_rxfilter(struct zyd_softc *sc) 2016{ 2017 struct ifnet *ifp = sc->sc_ifp; 2018 struct ieee80211com *ic = ifp->if_l2com; 2019 uint32_t rxfilter; 2020 2021 switch (ic->ic_opmode) { 2022 case IEEE80211_M_STA: 2023 rxfilter = ZYD_FILTER_BSS; 2024 break; 2025 case IEEE80211_M_IBSS: 2026 case IEEE80211_M_HOSTAP: 2027 rxfilter = ZYD_FILTER_HOSTAP; 2028 break; 2029 case IEEE80211_M_MONITOR: 2030 rxfilter = ZYD_FILTER_MONITOR; 2031 break; 2032 default: 2033 /* should not get there */ 2034 return (EINVAL); 2035 } 2036 return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter); 2037} 2038 2039static void 2040zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c) 2041{ 2042 int error; 2043 struct ifnet *ifp = sc->sc_ifp; 2044 struct ieee80211com *ic = ifp->if_l2com; 2045 struct zyd_rf *rf = &sc->sc_rf; 2046 uint32_t tmp; 2047 int chan; 2048 2049 chan = ieee80211_chan2ieee(ic, c); 2050 if (chan == 0 || chan == IEEE80211_CHAN_ANY) { 2051 /* XXX should NEVER happen */ 2052 device_printf(sc->sc_dev, 2053 "%s: invalid channel %x\n", __func__, chan); 2054 return; 2055 } 2056 2057 error = zyd_lock_phy(sc); 2058 if (error != 0) 2059 goto fail; 2060 2061 error = (*rf->set_channel)(rf, chan); 2062 if (error != 0) 2063 goto fail; 2064 2065 /* update Tx power */ 2066 zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]); 2067 2068 if (sc->sc_macrev == ZYD_ZD1211B) { 2069 zyd_write16_m(sc, ZYD_CR67, sc->sc_ofdm36_cal[chan - 1]); 2070 zyd_write16_m(sc, ZYD_CR66, sc->sc_ofdm48_cal[chan - 1]); 2071 zyd_write16_m(sc, ZYD_CR65, sc->sc_ofdm54_cal[chan - 1]); 2072 zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]); 2073 zyd_write16_m(sc, ZYD_CR69, 0x28); 2074 zyd_write16_m(sc, ZYD_CR69, 0x2a); 2075 } 2076 if (sc->sc_cckgain) { 2077 /* set CCK baseband gain from EEPROM */ 2078 if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0) 2079 zyd_write16_m(sc, ZYD_CR47, tmp & 0xff); 2080 } 2081 if (sc->sc_bandedge6 && rf->bandedge6 != NULL) { 2082 error = (*rf->bandedge6)(rf, c); 2083 if (error != 0) 2084 goto fail; 2085 } 2086 zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0); 2087 2088 error = zyd_unlock_phy(sc); 2089 if (error != 0) 2090 goto fail; 2091 2092 sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq = 2093 htole16(c->ic_freq); 2094 sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags = 2095 htole16(c->ic_flags); 2096fail: 2097 return; 2098} 2099 2100static int 2101zyd_set_beacon_interval(struct zyd_softc *sc, int bintval) 2102{ 2103 int error; 2104 uint32_t val; 2105 2106 zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val); 2107 sc->sc_atim_wnd = val; 2108 zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val); 2109 sc->sc_pre_tbtt = val; 2110 sc->sc_bcn_int = bintval; 2111 2112 if (sc->sc_bcn_int <= 5) 2113 sc->sc_bcn_int = 5; 2114 if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int) 2115 sc->sc_pre_tbtt = sc->sc_bcn_int - 1; 2116 if (sc->sc_atim_wnd >= sc->sc_pre_tbtt) 2117 sc->sc_atim_wnd = sc->sc_pre_tbtt - 1; 2118 2119 zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd); 2120 zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt); 2121 zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int); 2122fail: 2123 return (error); 2124} 2125 2126static void 2127zyd_rx_data(struct usb2_xfer *xfer, int offset, uint16_t len) 2128{ 2129 struct zyd_softc *sc = xfer->priv_sc; 2130 struct ifnet *ifp = sc->sc_ifp; 2131 struct ieee80211com *ic = ifp->if_l2com; 2132 struct zyd_plcphdr plcp; 2133 struct zyd_rx_stat stat; 2134 struct mbuf *m; 2135 int rlen, rssi; 2136 2137 if (len < ZYD_MIN_FRAGSZ) { 2138 DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n", 2139 device_get_nameunit(sc->sc_dev), len); 2140 ifp->if_ierrors++; 2141 return; 2142 } 2143 usb2_copy_out(xfer->frbuffers, offset, &plcp, sizeof(plcp)); 2144 usb2_copy_out(xfer->frbuffers, offset + len - sizeof(stat), 2145 &stat, sizeof(stat)); 2146 2147 if (stat.flags & ZYD_RX_ERROR) { 2148 DPRINTF(sc, ZYD_DEBUG_RECV, 2149 "%s: RX status indicated error (%x)\n", 2150 device_get_nameunit(sc->sc_dev), stat.flags); 2151 ifp->if_ierrors++; 2152 return; 2153 } 2154 2155 /* compute actual frame length */ 2156 rlen = len - sizeof(struct zyd_plcphdr) - 2157 sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN; 2158 2159 /* allocate a mbuf to store the frame */ 2160 if (rlen > MCLBYTES) { 2161 DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n", 2162 device_get_nameunit(sc->sc_dev), rlen); 2163 ifp->if_ierrors++; 2164 return; 2165 } else if (rlen > MHLEN) 2166 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 2167 else 2168 m = m_gethdr(M_DONTWAIT, MT_DATA); 2169 if (m == NULL) { 2170 DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n", 2171 device_get_nameunit(sc->sc_dev)); 2172 ifp->if_ierrors++; 2173 return; 2174 } 2175 m->m_pkthdr.rcvif = ifp; 2176 m->m_pkthdr.len = m->m_len = rlen; 2177 usb2_copy_out(xfer->frbuffers, offset + sizeof(plcp), 2178 mtod(m, uint8_t *), rlen); 2179 2180 if (ieee80211_radiotap_active(ic)) { 2181 struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap; 2182 2183 tap->wr_flags = 0; 2184 if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32)) 2185 tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS; 2186 /* XXX toss, no way to express errors */ 2187 if (stat.flags & ZYD_RX_DECRYPTERR) 2188 tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS; 2189 tap->wr_rate = ieee80211_plcp2rate(plcp.signal, 2190 (stat.flags & ZYD_RX_OFDM) ? 2191 IEEE80211_T_OFDM : IEEE80211_T_CCK); 2192 tap->wr_antsignal = stat.rssi + -95; 2193 tap->wr_antnoise = -95; /* XXX */ 2194 } 2195 rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi; 2196 2197 sc->sc_rx_data[sc->sc_rx_count].rssi = rssi; 2198 sc->sc_rx_data[sc->sc_rx_count].m = m; 2199 sc->sc_rx_count++; 2200} 2201 2202static void 2203zyd_bulk_read_callback(struct usb2_xfer *xfer) 2204{ 2205 struct zyd_softc *sc = xfer->priv_sc; 2206 struct ifnet *ifp = sc->sc_ifp; 2207 struct ieee80211com *ic = ifp->if_l2com; 2208 struct ieee80211_node *ni; 2209 struct zyd_rx_desc desc; 2210 struct mbuf *m; 2211 uint32_t offset; 2212 uint8_t rssi; 2213 int8_t nf; 2214 int i; 2215 2216 sc->sc_rx_count = 0; 2217 switch (USB_GET_STATE(xfer)) { 2218 case USB_ST_TRANSFERRED: 2219 usb2_copy_out(xfer->frbuffers, xfer->actlen - sizeof(desc), 2220 &desc, sizeof(desc)); 2221 2222 offset = 0; 2223 if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) { 2224 DPRINTF(sc, ZYD_DEBUG_RECV, 2225 "%s: received multi-frame transfer\n", __func__); 2226 2227 for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) { 2228 uint16_t len16 = UGETW(desc.len[i]); 2229 2230 if (len16 == 0 || len16 > xfer->actlen) 2231 break; 2232 2233 zyd_rx_data(xfer, offset, len16); 2234 2235 /* next frame is aligned on a 32-bit boundary */ 2236 len16 = (len16 + 3) & ~3; 2237 offset += len16; 2238 if (len16 > xfer->actlen) 2239 break; 2240 xfer->actlen -= len16; 2241 } 2242 } else { 2243 DPRINTF(sc, ZYD_DEBUG_RECV, 2244 "%s: received single-frame transfer\n", __func__); 2245 2246 zyd_rx_data(xfer, 0, xfer->actlen); 2247 } 2248 /* FALLTHROUGH */ 2249 case USB_ST_SETUP: 2250tr_setup: 2251 xfer->frlengths[0] = xfer->max_data_length; 2252 usb2_start_hardware(xfer); 2253 2254 /* 2255 * At the end of a USB callback it is always safe to unlock 2256 * the private mutex of a device! That is why we do the 2257 * "ieee80211_input" here, and not some lines up! 2258 */ 2259 ZYD_UNLOCK(sc); 2260 for (i = 0; i < sc->sc_rx_count; i++) { 2261 rssi = sc->sc_rx_data[i].rssi; 2262 m = sc->sc_rx_data[i].m; 2263 sc->sc_rx_data[i].m = NULL; 2264 2265 nf = -95; /* XXX */ 2266 2267 ni = ieee80211_find_rxnode(ic, 2268 mtod(m, struct ieee80211_frame_min *)); 2269 if (ni != NULL) { 2270 (void)ieee80211_input(ni, m, rssi, nf); 2271 ieee80211_free_node(ni); 2272 } else 2273 (void)ieee80211_input_all(ic, m, rssi, nf); 2274 } 2275 ZYD_LOCK(sc); 2276 break; 2277 2278 default: /* Error */ 2279 DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usb2_errstr(xfer->error)); 2280 2281 if (xfer->error != USB_ERR_CANCELLED) { 2282 /* try to clear stall first */ 2283 xfer->flags.stall_pipe = 1; 2284 goto tr_setup; 2285 } 2286 break; 2287 } 2288} 2289 2290static uint8_t 2291zyd_plcp_signal(int rate) 2292{ 2293 switch (rate) { 2294 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 2295 case 12: 2296 return (0xb); 2297 case 18: 2298 return (0xf); 2299 case 24: 2300 return (0xa); 2301 case 36: 2302 return (0xe); 2303 case 48: 2304 return (0x9); 2305 case 72: 2306 return (0xd); 2307 case 96: 2308 return (0x8); 2309 case 108: 2310 return (0xc); 2311 /* CCK rates (NB: not IEEE std, device-specific) */ 2312 case 2: 2313 return (0x0); 2314 case 4: 2315 return (0x1); 2316 case 11: 2317 return (0x2); 2318 case 22: 2319 return (0x3); 2320 } 2321 return (0xff); /* XXX unsupported/unknown rate */ 2322} 2323 2324static int 2325zyd_tx_mgt(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 2326{ 2327 struct ieee80211vap *vap = ni->ni_vap; 2328 struct ieee80211com *ic = ni->ni_ic; 2329 struct zyd_tx_desc *desc; 2330 struct zyd_tx_data *data; 2331 struct ieee80211_frame *wh; 2332 struct ieee80211_key *k; 2333 int rate, totlen; 2334 uint16_t pktlen; 2335 2336 data = STAILQ_FIRST(&sc->tx_free); 2337 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 2338 sc->tx_nfree--; 2339 desc = &data->desc; 2340 2341 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2; 2342 2343 wh = mtod(m0, struct ieee80211_frame *); 2344 2345 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 2346 k = ieee80211_crypto_encap(ni, m0); 2347 if (k == NULL) { 2348 m_freem(m0); 2349 return (ENOBUFS); 2350 } 2351 } 2352 2353 data->ni = ni; 2354 data->m = m0; 2355 data->rate = rate; 2356 2357 wh = mtod(m0, struct ieee80211_frame *); 2358 2359 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN; 2360 2361 /* fill Tx descriptor */ 2362 desc->len = htole16(totlen); 2363 2364 desc->flags = ZYD_TX_FLAG_BACKOFF; 2365 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 2366 /* multicast frames are not sent at OFDM rates in 802.11b/g */ 2367 if (totlen > vap->iv_rtsthreshold) { 2368 desc->flags |= ZYD_TX_FLAG_RTS; 2369 } else if (ZYD_RATE_IS_OFDM(rate) && 2370 (ic->ic_flags & IEEE80211_F_USEPROT)) { 2371 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 2372 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF; 2373 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 2374 desc->flags |= ZYD_TX_FLAG_RTS; 2375 } 2376 } else 2377 desc->flags |= ZYD_TX_FLAG_MULTICAST; 2378 2379 if ((wh->i_fc[0] & 2380 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 2381 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL)) 2382 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL); 2383 2384 desc->phy = zyd_plcp_signal(rate); 2385 if (ZYD_RATE_IS_OFDM(rate)) { 2386 desc->phy |= ZYD_TX_PHY_OFDM; 2387 if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) 2388 desc->phy |= ZYD_TX_PHY_5GHZ; 2389 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 2390 desc->phy |= ZYD_TX_PHY_SHPREAMBLE; 2391 2392 /* actual transmit length (XXX why +10?) */ 2393 pktlen = ZYD_TX_DESC_SIZE + 10; 2394 if (sc->sc_macrev == ZYD_ZD1211) 2395 pktlen += totlen; 2396 desc->pktlen = htole16(pktlen); 2397 2398 desc->plcp_length = (16 * totlen + rate - 1) / rate; 2399 desc->plcp_service = 0; 2400 if (rate == 22) { 2401 const int remainder = (16 * totlen) % 22; 2402 if (remainder != 0 && remainder < 7) 2403 desc->plcp_service |= ZYD_PLCP_LENGEXT; 2404 } 2405 2406 if (ieee80211_radiotap_active_vap(vap)) { 2407 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap; 2408 2409 tap->wt_flags = 0; 2410 tap->wt_rate = rate; 2411 2412 ieee80211_radiotap_tx(vap, m0); 2413 } 2414 2415 DPRINTF(sc, ZYD_DEBUG_XMIT, 2416 "%s: sending mgt frame len=%zu rate=%u\n", 2417 device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len, 2418 rate); 2419 2420 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 2421 usb2_transfer_start(sc->sc_xfer[ZYD_BULK_WR]); 2422 2423 return (0); 2424} 2425 2426static void 2427zyd_bulk_write_callback(struct usb2_xfer *xfer) 2428{ 2429 struct zyd_softc *sc = xfer->priv_sc; 2430 struct ifnet *ifp = sc->sc_ifp; 2431 struct ieee80211vap *vap; 2432 struct zyd_tx_data *data; 2433 struct mbuf *m; 2434 2435 switch (USB_GET_STATE(xfer)) { 2436 case USB_ST_TRANSFERRED: 2437 DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n", 2438 xfer->actlen); 2439 2440 /* free resources */ 2441 data = xfer->priv_fifo; 2442 zyd_tx_free(data, 0); 2443 xfer->priv_fifo = NULL; 2444 2445 ifp->if_opackets++; 2446 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2447 2448 /* FALLTHROUGH */ 2449 case USB_ST_SETUP: 2450tr_setup: 2451 data = STAILQ_FIRST(&sc->tx_q); 2452 if (data) { 2453 STAILQ_REMOVE_HEAD(&sc->tx_q, next); 2454 m = data->m; 2455 2456 if (m->m_pkthdr.len > ZYD_MAX_TXBUFSZ) { 2457 DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n", 2458 m->m_pkthdr.len); 2459 m->m_pkthdr.len = ZYD_MAX_TXBUFSZ; 2460 } 2461 usb2_copy_in(xfer->frbuffers, 0, &data->desc, 2462 ZYD_TX_DESC_SIZE); 2463 usb2_m_copy_in(xfer->frbuffers, ZYD_TX_DESC_SIZE, m, 0, 2464 m->m_pkthdr.len); 2465 2466 vap = data->ni->ni_vap; 2467 if (ieee80211_radiotap_active_vap(vap)) { 2468 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap; 2469 2470 tap->wt_flags = 0; 2471 tap->wt_rate = data->rate; 2472 2473 ieee80211_radiotap_tx(vap, m); 2474 } 2475 2476 xfer->frlengths[0] = ZYD_TX_DESC_SIZE + m->m_pkthdr.len; 2477 xfer->priv_fifo = data; 2478 usb2_start_hardware(xfer); 2479 } 2480 break; 2481 2482 default: /* Error */ 2483 DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n", 2484 usb2_errstr(xfer->error)); 2485 2486 ifp->if_oerrors++; 2487 data = xfer->priv_fifo; 2488 xfer->priv_fifo = NULL; 2489 if (data != NULL) 2490 zyd_tx_free(data, xfer->error); 2491 2492 if (xfer->error == USB_ERR_STALLED) { 2493 /* try to clear stall first */ 2494 xfer->flags.stall_pipe = 1; 2495 goto tr_setup; 2496 } 2497 if (xfer->error == USB_ERR_TIMEOUT) 2498 device_printf(sc->sc_dev, "device timeout\n"); 2499 break; 2500 } 2501} 2502 2503static int 2504zyd_tx_data(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 2505{ 2506 struct ieee80211vap *vap = ni->ni_vap; 2507 struct ieee80211com *ic = ni->ni_ic; 2508 struct zyd_tx_desc *desc; 2509 struct zyd_tx_data *data; 2510 struct ieee80211_frame *wh; 2511 const struct ieee80211_txparam *tp; 2512 struct ieee80211_key *k; 2513 int rate, totlen; 2514 uint16_t pktlen; 2515 2516 wh = mtod(m0, struct ieee80211_frame *); 2517 data = STAILQ_FIRST(&sc->tx_free); 2518 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 2519 sc->tx_nfree--; 2520 desc = &data->desc; 2521 2522 desc->flags = ZYD_TX_FLAG_BACKOFF; 2523 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 2524 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 2525 rate = tp->mcastrate; 2526 desc->flags |= ZYD_TX_FLAG_MULTICAST; 2527 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) { 2528 rate = tp->ucastrate; 2529 } else { 2530 (void) ieee80211_amrr_choose(ni, &ZYD_NODE(ni)->amn); 2531 rate = ni->ni_txrate; 2532 } 2533 2534 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 2535 k = ieee80211_crypto_encap(ni, m0); 2536 if (k == NULL) { 2537 m_freem(m0); 2538 return (ENOBUFS); 2539 } 2540 /* packet header may have moved, reset our local pointer */ 2541 wh = mtod(m0, struct ieee80211_frame *); 2542 } 2543 2544 data->ni = ni; 2545 data->m = m0; 2546 2547 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN; 2548 2549 /* fill Tx descriptor */ 2550 desc->len = htole16(totlen); 2551 2552 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 2553 /* multicast frames are not sent at OFDM rates in 802.11b/g */ 2554 if (totlen > vap->iv_rtsthreshold) { 2555 desc->flags |= ZYD_TX_FLAG_RTS; 2556 } else if (ZYD_RATE_IS_OFDM(rate) && 2557 (ic->ic_flags & IEEE80211_F_USEPROT)) { 2558 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 2559 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF; 2560 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 2561 desc->flags |= ZYD_TX_FLAG_RTS; 2562 } 2563 } 2564 2565 if ((wh->i_fc[0] & 2566 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 2567 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL)) 2568 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL); 2569 2570 desc->phy = zyd_plcp_signal(rate); 2571 if (ZYD_RATE_IS_OFDM(rate)) { 2572 desc->phy |= ZYD_TX_PHY_OFDM; 2573 if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) 2574 desc->phy |= ZYD_TX_PHY_5GHZ; 2575 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 2576 desc->phy |= ZYD_TX_PHY_SHPREAMBLE; 2577 2578 /* actual transmit length (XXX why +10?) */ 2579 pktlen = sizeof(struct zyd_tx_desc) + 10; 2580 if (sc->sc_macrev == ZYD_ZD1211) 2581 pktlen += totlen; 2582 desc->pktlen = htole16(pktlen); 2583 2584 desc->plcp_length = (16 * totlen + rate - 1) / rate; 2585 desc->plcp_service = 0; 2586 if (rate == 22) { 2587 const int remainder = (16 * totlen) % 22; 2588 if (remainder != 0 && remainder < 7) 2589 desc->plcp_service |= ZYD_PLCP_LENGEXT; 2590 } 2591 2592 DPRINTF(sc, ZYD_DEBUG_XMIT, 2593 "%s: sending data frame len=%zu rate=%u\n", 2594 device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len, 2595 rate); 2596 2597 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 2598 usb2_transfer_start(sc->sc_xfer[ZYD_BULK_WR]); 2599 2600 return (0); 2601} 2602 2603static void 2604zyd_start(struct ifnet *ifp) 2605{ 2606 struct zyd_softc *sc = ifp->if_softc; 2607 struct ieee80211_node *ni; 2608 struct mbuf *m; 2609 2610 ZYD_LOCK(sc); 2611 for (;;) { 2612 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 2613 if (m == NULL) 2614 break; 2615 if (sc->tx_nfree == 0) { 2616 IFQ_DRV_PREPEND(&ifp->if_snd, m); 2617 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2618 break; 2619 } 2620 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 2621 if (zyd_tx_data(sc, m, ni) != 0) { 2622 ieee80211_free_node(ni); 2623 ifp->if_oerrors++; 2624 break; 2625 } 2626 } 2627 ZYD_UNLOCK(sc); 2628} 2629 2630static int 2631zyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2632 const struct ieee80211_bpf_params *params) 2633{ 2634 struct ieee80211com *ic = ni->ni_ic; 2635 struct ifnet *ifp = ic->ic_ifp; 2636 struct zyd_softc *sc = ifp->if_softc; 2637 2638 ZYD_LOCK(sc); 2639 /* prevent management frames from being sent if we're not ready */ 2640 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2641 ZYD_UNLOCK(sc); 2642 m_freem(m); 2643 ieee80211_free_node(ni); 2644 return (ENETDOWN); 2645 } 2646 if (sc->tx_nfree == 0) { 2647 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2648 ZYD_UNLOCK(sc); 2649 m_freem(m); 2650 ieee80211_free_node(ni); 2651 return (ENOBUFS); /* XXX */ 2652 } 2653 2654 /* 2655 * Legacy path; interpret frame contents to decide 2656 * precisely how to send the frame. 2657 * XXX raw path 2658 */ 2659 if (zyd_tx_mgt(sc, m, ni) != 0) { 2660 ZYD_UNLOCK(sc); 2661 ifp->if_oerrors++; 2662 ieee80211_free_node(ni); 2663 return (EIO); 2664 } 2665 ZYD_UNLOCK(sc); 2666 return (0); 2667} 2668 2669static int 2670zyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2671{ 2672 struct zyd_softc *sc = ifp->if_softc; 2673 struct ieee80211com *ic = ifp->if_l2com; 2674 struct ifreq *ifr = (struct ifreq *) data; 2675 int error = 0, startall = 0; 2676 2677 switch (cmd) { 2678 case SIOCSIFFLAGS: 2679 ZYD_LOCK(sc); 2680 if (ifp->if_flags & IFF_UP) { 2681 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2682 zyd_init_locked(sc); 2683 startall = 1; 2684 } else 2685 zyd_set_multi(sc); 2686 } else { 2687 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2688 zyd_stop(sc); 2689 } 2690 ZYD_UNLOCK(sc); 2691 if (startall) 2692 ieee80211_start_all(ic); 2693 break; 2694 case SIOCGIFMEDIA: 2695 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 2696 break; 2697 case SIOCGIFADDR: 2698 error = ether_ioctl(ifp, cmd, data); 2699 break; 2700 default: 2701 error = EINVAL; 2702 break; 2703 } 2704 return (error); 2705} 2706 2707static void 2708zyd_init_locked(struct zyd_softc *sc) 2709{ 2710 struct ifnet *ifp = sc->sc_ifp; 2711 struct ieee80211com *ic = ifp->if_l2com; 2712 struct usb2_config_descriptor *cd; 2713 int error; 2714 uint32_t val; 2715 2716 ZYD_LOCK_ASSERT(sc, MA_OWNED); 2717 2718 if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) { 2719 error = zyd_loadfirmware(sc); 2720 if (error != 0) { 2721 device_printf(sc->sc_dev, 2722 "could not load firmware (error=%d)\n", error); 2723 goto fail; 2724 } 2725 2726 /* reset device */ 2727 cd = usb2_get_config_descriptor(sc->sc_udev); 2728 error = usb2_req_set_config(sc->sc_udev, &sc->sc_mtx, 2729 cd->bConfigurationValue); 2730 if (error) 2731 device_printf(sc->sc_dev, "reset failed, continuing\n"); 2732 2733 error = zyd_hw_init(sc); 2734 if (error) { 2735 device_printf(sc->sc_dev, 2736 "hardware initialization failed\n"); 2737 goto fail; 2738 } 2739 2740 device_printf(sc->sc_dev, 2741 "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x " 2742 "BE%x NP%x Gain%x F%x\n", 2743 (sc->sc_macrev == ZYD_ZD1211) ? "": "B", 2744 sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff, 2745 zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev, 2746 sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy, 2747 sc->sc_cckgain, sc->sc_fix_cr157); 2748 2749 /* read regulatory domain (currently unused) */ 2750 zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val); 2751 sc->sc_regdomain = val >> 16; 2752 DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n", 2753 sc->sc_regdomain); 2754 2755 /* we'll do software WEP decryption for now */ 2756 DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n", 2757 __func__); 2758 zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER); 2759 2760 sc->sc_flags |= ZYD_FLAG_INITONCE; 2761 } 2762 2763 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2764 zyd_stop(sc); 2765 2766 DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n", 2767 IF_LLADDR(ifp), ":"); 2768 error = zyd_set_macaddr(sc, IF_LLADDR(ifp)); 2769 if (error != 0) 2770 return; 2771 2772 /* set basic rates */ 2773 if (ic->ic_curmode == IEEE80211_MODE_11B) 2774 zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003); 2775 else if (ic->ic_curmode == IEEE80211_MODE_11A) 2776 zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500); 2777 else /* assumes 802.11b/g */ 2778 zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f); 2779 2780 /* promiscuous mode */ 2781 zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0); 2782 /* multicast setup */ 2783 zyd_set_multi(sc); 2784 /* set RX filter */ 2785 error = zyd_set_rxfilter(sc); 2786 if (error != 0) 2787 goto fail; 2788 2789 /* switch radio transmitter ON */ 2790 error = zyd_switch_radio(sc, 1); 2791 if (error != 0) 2792 goto fail; 2793 /* set default BSS channel */ 2794 zyd_set_chan(sc, ic->ic_curchan); 2795 2796 /* 2797 * Allocate Tx and Rx xfer queues. 2798 */ 2799 zyd_setup_tx_list(sc); 2800 2801 /* enable interrupts */ 2802 zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK); 2803 2804 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2805 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2806 usb2_transfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]); 2807 usb2_transfer_start(sc->sc_xfer[ZYD_BULK_RD]); 2808 usb2_transfer_start(sc->sc_xfer[ZYD_INTR_RD]); 2809 2810 return; 2811 2812fail: zyd_stop(sc); 2813 return; 2814} 2815 2816static void 2817zyd_init(void *priv) 2818{ 2819 struct zyd_softc *sc = priv; 2820 struct ifnet *ifp = sc->sc_ifp; 2821 struct ieee80211com *ic = ifp->if_l2com; 2822 2823 ZYD_LOCK(sc); 2824 zyd_init_locked(sc); 2825 ZYD_UNLOCK(sc); 2826 2827 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2828 ieee80211_start_all(ic); /* start all vap's */ 2829} 2830 2831static void 2832zyd_stop(struct zyd_softc *sc) 2833{ 2834 struct ifnet *ifp = sc->sc_ifp; 2835 int error; 2836 2837 ZYD_LOCK_ASSERT(sc, MA_OWNED); 2838 2839 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2840 2841 /* 2842 * Drain all the transfers, if not already drained: 2843 */ 2844 ZYD_UNLOCK(sc); 2845 usb2_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]); 2846 usb2_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]); 2847 ZYD_LOCK(sc); 2848 2849 zyd_unsetup_tx_list(sc); 2850 2851 /* Stop now if the device was never set up */ 2852 if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) 2853 return; 2854 2855 /* switch radio transmitter OFF */ 2856 error = zyd_switch_radio(sc, 0); 2857 if (error != 0) 2858 goto fail; 2859 /* disable Rx */ 2860 zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0); 2861 /* disable interrupts */ 2862 zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0); 2863 2864fail: 2865 return; 2866} 2867 2868static int 2869zyd_loadfirmware(struct zyd_softc *sc) 2870{ 2871 struct usb2_device_request req; 2872 size_t size; 2873 u_char *fw; 2874 uint8_t stat; 2875 uint16_t addr; 2876 2877 if (sc->sc_flags & ZYD_FLAG_FWLOADED) 2878 return (0); 2879 2880 if (sc->sc_macrev == ZYD_ZD1211) { 2881 fw = (u_char *)zd1211_firmware; 2882 size = sizeof(zd1211_firmware); 2883 } else { 2884 fw = (u_char *)zd1211b_firmware; 2885 size = sizeof(zd1211b_firmware); 2886 } 2887 2888 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 2889 req.bRequest = ZYD_DOWNLOADREQ; 2890 USETW(req.wIndex, 0); 2891 2892 addr = ZYD_FIRMWARE_START_ADDR; 2893 while (size > 0) { 2894 /* 2895 * When the transfer size is 4096 bytes, it is not 2896 * likely to be able to transfer it. 2897 * The cause is port or machine or chip? 2898 */ 2899 const int mlen = min(size, 64); 2900 2901 DPRINTF(sc, ZYD_DEBUG_FW, 2902 "loading firmware block: len=%d, addr=0x%x\n", mlen, addr); 2903 2904 USETW(req.wValue, addr); 2905 USETW(req.wLength, mlen); 2906 if (zyd_do_request(sc, &req, fw) != 0) 2907 return (EIO); 2908 2909 addr += mlen / 2; 2910 fw += mlen; 2911 size -= mlen; 2912 } 2913 2914 /* check whether the upload succeeded */ 2915 req.bmRequestType = UT_READ_VENDOR_DEVICE; 2916 req.bRequest = ZYD_DOWNLOADSTS; 2917 USETW(req.wValue, 0); 2918 USETW(req.wIndex, 0); 2919 USETW(req.wLength, sizeof(stat)); 2920 if (zyd_do_request(sc, &req, &stat) != 0) 2921 return (EIO); 2922 2923 sc->sc_flags |= ZYD_FLAG_FWLOADED; 2924 2925 return (stat & 0x80) ? (EIO) : (0); 2926} 2927 2928static void 2929zyd_newassoc(struct ieee80211_node *ni, int isnew) 2930{ 2931 struct ieee80211vap *vap = ni->ni_vap; 2932 2933 ieee80211_amrr_node_init(&ZYD_VAP(vap)->amrr, &ZYD_NODE(ni)->amn, ni); 2934} 2935 2936static void 2937zyd_scan_start(struct ieee80211com *ic) 2938{ 2939 struct ifnet *ifp = ic->ic_ifp; 2940 struct zyd_softc *sc = ifp->if_softc; 2941 2942 ZYD_LOCK(sc); 2943 /* want broadcast address while scanning */ 2944 zyd_set_bssid(sc, ifp->if_broadcastaddr); 2945 ZYD_UNLOCK(sc); 2946} 2947 2948static void 2949zyd_scan_end(struct ieee80211com *ic) 2950{ 2951 struct zyd_softc *sc = ic->ic_ifp->if_softc; 2952 2953 ZYD_LOCK(sc); 2954 /* restore previous bssid */ 2955 zyd_set_bssid(sc, sc->sc_bssid); 2956 ZYD_UNLOCK(sc); 2957} 2958 2959static void 2960zyd_set_channel(struct ieee80211com *ic) 2961{ 2962 struct zyd_softc *sc = ic->ic_ifp->if_softc; 2963 2964 ZYD_LOCK(sc); 2965 zyd_set_chan(sc, ic->ic_curchan); 2966 ZYD_UNLOCK(sc); 2967} 2968 2969static device_method_t zyd_methods[] = { 2970 /* Device interface */ 2971 DEVMETHOD(device_probe, zyd_match), 2972 DEVMETHOD(device_attach, zyd_attach), 2973 DEVMETHOD(device_detach, zyd_detach), 2974 2975 { 0, 0 } 2976}; 2977 2978static driver_t zyd_driver = { 2979 "zyd", 2980 zyd_methods, 2981 sizeof(struct zyd_softc) 2982}; 2983 2984static devclass_t zyd_devclass; 2985 2986DRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0); 2987MODULE_DEPEND(zyd, usb, 1, 1, 1); 2988MODULE_DEPEND(zyd, wlan, 1, 1, 1); 2989MODULE_DEPEND(zyd, wlan_amrr, 1, 1, 1); 2990