1184610Salfred/* $OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $ */ 2184610Salfred/* $NetBSD: if_zyd.c,v 1.7 2007/06/21 04:04:29 kiyohara Exp $ */ 3184610Salfred/* $FreeBSD: stable/11/sys/dev/usb/wlan/if_zyd.c 343976 2019-02-10 21:00:02Z avos $ */ 4184610Salfred 5184610Salfred/*- 6184610Salfred * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr> 7184610Salfred * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de> 8184610Salfred * 9184610Salfred * Permission to use, copy, modify, and distribute this software for any 10184610Salfred * purpose with or without fee is hereby granted, provided that the above 11184610Salfred * copyright notice and this permission notice appear in all copies. 12184610Salfred * 13184610Salfred * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14184610Salfred * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15184610Salfred * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16184610Salfred * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17184610Salfred * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18184610Salfred * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19184610Salfred * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20184610Salfred */ 21184610Salfred 22184610Salfred#include <sys/cdefs.h> 23184610Salfred__FBSDID("$FreeBSD: stable/11/sys/dev/usb/wlan/if_zyd.c 343976 2019-02-10 21:00:02Z avos $"); 24184610Salfred 25184610Salfred/* 26188417Sthompsa * ZyDAS ZD1211/ZD1211B USB WLAN driver. 27184610Salfred */ 28184610Salfred 29191746Sthompsa#include <sys/param.h> 30191746Sthompsa#include <sys/sockio.h> 31191746Sthompsa#include <sys/sysctl.h> 32191746Sthompsa#include <sys/lock.h> 33191746Sthompsa#include <sys/mutex.h> 34194677Sthompsa#include <sys/condvar.h> 35191746Sthompsa#include <sys/mbuf.h> 36191746Sthompsa#include <sys/kernel.h> 37191746Sthompsa#include <sys/socket.h> 38191746Sthompsa#include <sys/systm.h> 39191746Sthompsa#include <sys/malloc.h> 40191746Sthompsa#include <sys/module.h> 41191746Sthompsa#include <sys/bus.h> 42191746Sthompsa#include <sys/endian.h> 43191746Sthompsa#include <sys/kdb.h> 44191746Sthompsa 45191746Sthompsa#include <net/bpf.h> 46191746Sthompsa#include <net/if.h> 47257176Sglebius#include <net/if_var.h> 48191746Sthompsa#include <net/if_arp.h> 49191746Sthompsa#include <net/ethernet.h> 50191746Sthompsa#include <net/if_dl.h> 51191746Sthompsa#include <net/if_media.h> 52191746Sthompsa#include <net/if_types.h> 53191746Sthompsa 54191746Sthompsa#ifdef INET 55191746Sthompsa#include <netinet/in.h> 56191746Sthompsa#include <netinet/in_systm.h> 57191746Sthompsa#include <netinet/in_var.h> 58191746Sthompsa#include <netinet/if_ether.h> 59191746Sthompsa#include <netinet/ip.h> 60191746Sthompsa#endif 61191746Sthompsa 62191746Sthompsa#include <net80211/ieee80211_var.h> 63191746Sthompsa#include <net80211/ieee80211_regdomain.h> 64191746Sthompsa#include <net80211/ieee80211_radiotap.h> 65206358Srpaulo#include <net80211/ieee80211_ratectl.h> 66191746Sthompsa 67188942Sthompsa#include <dev/usb/usb.h> 68194677Sthompsa#include <dev/usb/usbdi.h> 69194677Sthompsa#include <dev/usb/usbdi_util.h> 70191746Sthompsa#include "usbdevs.h" 71184610Salfred 72188942Sthompsa#include <dev/usb/wlan/if_zydreg.h> 73188942Sthompsa#include <dev/usb/wlan/if_zydfw.h> 74184610Salfred 75207077Sthompsa#ifdef USB_DEBUG 76184610Salfredstatic int zyd_debug = 0; 77184610Salfred 78227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, zyd, CTLFLAG_RW, 0, "USB zyd"); 79276701ShselaskySYSCTL_INT(_hw_usb_zyd, OID_AUTO, debug, CTLFLAG_RWTUN, &zyd_debug, 0, 80184610Salfred "zyd debug level"); 81188417Sthompsa 82188417Sthompsaenum { 83188417Sthompsa ZYD_DEBUG_XMIT = 0x00000001, /* basic xmit operation */ 84188417Sthompsa ZYD_DEBUG_RECV = 0x00000002, /* basic recv operation */ 85188417Sthompsa ZYD_DEBUG_RESET = 0x00000004, /* reset processing */ 86188417Sthompsa ZYD_DEBUG_INIT = 0x00000008, /* device init */ 87188417Sthompsa ZYD_DEBUG_TX_PROC = 0x00000010, /* tx ISR proc */ 88188417Sthompsa ZYD_DEBUG_RX_PROC = 0x00000020, /* rx ISR proc */ 89188417Sthompsa ZYD_DEBUG_STATE = 0x00000040, /* 802.11 state transitions */ 90188417Sthompsa ZYD_DEBUG_STAT = 0x00000080, /* statistic */ 91188417Sthompsa ZYD_DEBUG_FW = 0x00000100, /* firmware */ 92188417Sthompsa ZYD_DEBUG_CMD = 0x00000200, /* fw commands */ 93188417Sthompsa ZYD_DEBUG_ANY = 0xffffffff 94188417Sthompsa}; 95188417Sthompsa#define DPRINTF(sc, m, fmt, ...) do { \ 96188419Sthompsa if (zyd_debug & (m)) \ 97188417Sthompsa printf("%s: " fmt, __func__, ## __VA_ARGS__); \ 98188417Sthompsa} while (0) 99188417Sthompsa#else 100188417Sthompsa#define DPRINTF(sc, m, fmt, ...) do { \ 101188417Sthompsa (void) sc; \ 102188417Sthompsa} while (0) 103184610Salfred#endif 104184610Salfred 105188419Sthompsa#define zyd_do_request(sc,req,data) \ 106194228Sthompsa usbd_do_request_flags((sc)->sc_udev, &(sc)->sc_mtx, req, data, 0, NULL, 5000) 107188419Sthompsa 108188417Sthompsastatic device_probe_t zyd_match; 109184610Salfredstatic device_attach_t zyd_attach; 110184610Salfredstatic device_detach_t zyd_detach; 111184610Salfred 112193045Sthompsastatic usb_callback_t zyd_intr_read_callback; 113193045Sthompsastatic usb_callback_t zyd_intr_write_callback; 114193045Sthompsastatic usb_callback_t zyd_bulk_read_callback; 115193045Sthompsastatic usb_callback_t zyd_bulk_write_callback; 116184610Salfred 117185948Sthompsastatic struct ieee80211vap *zyd_vap_create(struct ieee80211com *, 118228621Sbschmidt const char [IFNAMSIZ], int, enum ieee80211_opmode, int, 119228621Sbschmidt const uint8_t [IEEE80211_ADDR_LEN], 120228621Sbschmidt const uint8_t [IEEE80211_ADDR_LEN]); 121185948Sthompsastatic void zyd_vap_delete(struct ieee80211vap *); 122188417Sthompsastatic void zyd_tx_free(struct zyd_tx_data *, int); 123188419Sthompsastatic void zyd_setup_tx_list(struct zyd_softc *); 124188419Sthompsastatic void zyd_unsetup_tx_list(struct zyd_softc *); 125188417Sthompsastatic int zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int); 126188417Sthompsastatic int zyd_cmd(struct zyd_softc *, uint16_t, const void *, int, 127188601Sthompsa void *, int, int); 128188417Sthompsastatic int zyd_read16(struct zyd_softc *, uint16_t, uint16_t *); 129188417Sthompsastatic int zyd_read32(struct zyd_softc *, uint16_t, uint32_t *); 130188417Sthompsastatic int zyd_write16(struct zyd_softc *, uint16_t, uint16_t); 131188417Sthompsastatic int zyd_write32(struct zyd_softc *, uint16_t, uint32_t); 132188417Sthompsastatic int zyd_rfwrite(struct zyd_softc *, uint32_t); 133188417Sthompsastatic int zyd_lock_phy(struct zyd_softc *); 134188417Sthompsastatic int zyd_unlock_phy(struct zyd_softc *); 135188417Sthompsastatic int zyd_rf_attach(struct zyd_softc *, uint8_t); 136188417Sthompsastatic const char *zyd_rf_name(uint8_t); 137188417Sthompsastatic int zyd_hw_init(struct zyd_softc *); 138188417Sthompsastatic int zyd_read_pod(struct zyd_softc *); 139188417Sthompsastatic int zyd_read_eeprom(struct zyd_softc *); 140188417Sthompsastatic int zyd_get_macaddr(struct zyd_softc *); 141188417Sthompsastatic int zyd_set_macaddr(struct zyd_softc *, const uint8_t *); 142188417Sthompsastatic int zyd_set_bssid(struct zyd_softc *, const uint8_t *); 143188417Sthompsastatic int zyd_switch_radio(struct zyd_softc *, int); 144188417Sthompsastatic int zyd_set_led(struct zyd_softc *, int, int); 145188417Sthompsastatic void zyd_set_multi(struct zyd_softc *); 146283540Sglebiusstatic void zyd_update_mcast(struct ieee80211com *); 147188417Sthompsastatic int zyd_set_rxfilter(struct zyd_softc *); 148188417Sthompsastatic void zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *); 149188417Sthompsastatic int zyd_set_beacon_interval(struct zyd_softc *, int); 150192984Sthompsastatic void zyd_rx_data(struct usb_xfer *, int, uint16_t); 151193803Sweongyostatic int zyd_tx_start(struct zyd_softc *, struct mbuf *, 152185948Sthompsa struct ieee80211_node *); 153287197Sglebiusstatic int zyd_transmit(struct ieee80211com *, struct mbuf *); 154287197Sglebiusstatic void zyd_start(struct zyd_softc *); 155188417Sthompsastatic int zyd_raw_xmit(struct ieee80211_node *, struct mbuf *, 156185948Sthompsa const struct ieee80211_bpf_params *); 157287197Sglebiusstatic void zyd_parent(struct ieee80211com *); 158191746Sthompsastatic void zyd_init_locked(struct zyd_softc *); 159191746Sthompsastatic void zyd_stop(struct zyd_softc *); 160188417Sthompsastatic int zyd_loadfirmware(struct zyd_softc *); 161188417Sthompsastatic void zyd_scan_start(struct ieee80211com *); 162188417Sthompsastatic void zyd_scan_end(struct ieee80211com *); 163300751Savosstatic void zyd_getradiocaps(struct ieee80211com *, int, int *, 164300751Savos struct ieee80211_channel[]); 165188417Sthompsastatic void zyd_set_channel(struct ieee80211com *); 166188417Sthompsastatic int zyd_rfmd_init(struct zyd_rf *); 167188417Sthompsastatic int zyd_rfmd_switch_radio(struct zyd_rf *, int); 168188417Sthompsastatic int zyd_rfmd_set_channel(struct zyd_rf *, uint8_t); 169188417Sthompsastatic int zyd_al2230_init(struct zyd_rf *); 170188417Sthompsastatic int zyd_al2230_switch_radio(struct zyd_rf *, int); 171188417Sthompsastatic int zyd_al2230_set_channel(struct zyd_rf *, uint8_t); 172188417Sthompsastatic int zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t); 173188417Sthompsastatic int zyd_al2230_init_b(struct zyd_rf *); 174188417Sthompsastatic int zyd_al7230B_init(struct zyd_rf *); 175188417Sthompsastatic int zyd_al7230B_switch_radio(struct zyd_rf *, int); 176188417Sthompsastatic int zyd_al7230B_set_channel(struct zyd_rf *, uint8_t); 177188417Sthompsastatic int zyd_al2210_init(struct zyd_rf *); 178188417Sthompsastatic int zyd_al2210_switch_radio(struct zyd_rf *, int); 179188417Sthompsastatic int zyd_al2210_set_channel(struct zyd_rf *, uint8_t); 180188417Sthompsastatic int zyd_gct_init(struct zyd_rf *); 181188417Sthompsastatic int zyd_gct_switch_radio(struct zyd_rf *, int); 182188417Sthompsastatic int zyd_gct_set_channel(struct zyd_rf *, uint8_t); 183193420Sweongyostatic int zyd_gct_mode(struct zyd_rf *); 184193420Sweongyostatic int zyd_gct_set_channel_synth(struct zyd_rf *, int, int); 185193420Sweongyostatic int zyd_gct_write(struct zyd_rf *, uint16_t); 186193420Sweongyostatic int zyd_gct_txgain(struct zyd_rf *, uint8_t); 187188417Sthompsastatic int zyd_maxim2_init(struct zyd_rf *); 188188417Sthompsastatic int zyd_maxim2_switch_radio(struct zyd_rf *, int); 189188417Sthompsastatic int zyd_maxim2_set_channel(struct zyd_rf *, uint8_t); 190184610Salfred 191184610Salfredstatic const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY; 192184610Salfredstatic const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB; 193184610Salfred 194184610Salfred/* various supported device vendors/products */ 195188417Sthompsa#define ZYD_ZD1211 0 196188417Sthompsa#define ZYD_ZD1211B 1 197184610Salfred 198193419Sweongyo#define ZYD_ZD1211_DEV(v,p) \ 199193419Sweongyo { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211) } 200193419Sweongyo#define ZYD_ZD1211B_DEV(v,p) \ 201193419Sweongyo { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211B) } 202223486Shselaskystatic const STRUCT_USB_HOST_ID zyd_devs[] = { 203193419Sweongyo /* ZYD_ZD1211 */ 204193419Sweongyo ZYD_ZD1211_DEV(3COM2, 3CRUSB10075), 205193419Sweongyo ZYD_ZD1211_DEV(ABOCOM, WL54), 206193419Sweongyo ZYD_ZD1211_DEV(ASUS, WL159G), 207193419Sweongyo ZYD_ZD1211_DEV(CYBERTAN, TG54USB), 208193419Sweongyo ZYD_ZD1211_DEV(DRAYTEK, VIGOR550), 209193419Sweongyo ZYD_ZD1211_DEV(PLANEX2, GWUS54GD), 210193419Sweongyo ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL), 211193419Sweongyo ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ), 212193419Sweongyo ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI), 213193419Sweongyo ZYD_ZD1211_DEV(SAGEM, XG760A), 214193419Sweongyo ZYD_ZD1211_DEV(SENAO, NUB8301), 215193419Sweongyo ZYD_ZD1211_DEV(SITECOMEU, WL113), 216193419Sweongyo ZYD_ZD1211_DEV(SWEEX, ZD1211), 217193419Sweongyo ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN), 218193419Sweongyo ZYD_ZD1211_DEV(TEKRAM, ZD1211_1), 219193419Sweongyo ZYD_ZD1211_DEV(TEKRAM, ZD1211_2), 220193419Sweongyo ZYD_ZD1211_DEV(TWINMOS, G240), 221193419Sweongyo ZYD_ZD1211_DEV(UMEDIA, ALL0298V2), 222193419Sweongyo ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A), 223193419Sweongyo ZYD_ZD1211_DEV(UMEDIA, TEW429UB), 224193419Sweongyo ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G), 225193419Sweongyo ZYD_ZD1211_DEV(ZCOM, ZD1211), 226193419Sweongyo ZYD_ZD1211_DEV(ZYDAS, ZD1211), 227193419Sweongyo ZYD_ZD1211_DEV(ZYXEL, AG225H), 228193419Sweongyo ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220), 229193419Sweongyo ZYD_ZD1211_DEV(ZYXEL, G200V2), 230193419Sweongyo /* ZYD_ZD1211B */ 231223566Sgavin ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG_NF), 232193419Sweongyo ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG), 233193419Sweongyo ZYD_ZD1211B_DEV(ACCTON, ZD1211B), 234193419Sweongyo ZYD_ZD1211B_DEV(ASUS, A9T_WIFI), 235193419Sweongyo ZYD_ZD1211B_DEV(BELKIN, F5D7050_V4000), 236193419Sweongyo ZYD_ZD1211B_DEV(BELKIN, ZD1211B), 237193419Sweongyo ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G), 238193419Sweongyo ZYD_ZD1211B_DEV(FIBERLINE, WL430U), 239193419Sweongyo ZYD_ZD1211B_DEV(MELCO, KG54L), 240193419Sweongyo ZYD_ZD1211B_DEV(PHILIPS, SNU5600), 241193419Sweongyo ZYD_ZD1211B_DEV(PLANEX2, GW_US54GXS), 242193419Sweongyo ZYD_ZD1211B_DEV(SAGEM, XG76NA), 243193419Sweongyo ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B), 244193419Sweongyo ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1), 245193419Sweongyo ZYD_ZD1211B_DEV(USR, USR5423), 246193419Sweongyo ZYD_ZD1211B_DEV(VTECH, ZD1211B), 247193419Sweongyo ZYD_ZD1211B_DEV(ZCOM, ZD1211B), 248193419Sweongyo ZYD_ZD1211B_DEV(ZYDAS, ZD1211B), 249193419Sweongyo ZYD_ZD1211B_DEV(ZYXEL, M202), 250193419Sweongyo ZYD_ZD1211B_DEV(ZYXEL, G202), 251193419Sweongyo ZYD_ZD1211B_DEV(ZYXEL, G220V2) 252184610Salfred}; 253184610Salfred 254192984Sthompsastatic const struct usb_config zyd_config[ZYD_N_TRANSFER] = { 255188417Sthompsa [ZYD_BULK_WR] = { 256184610Salfred .type = UE_BULK, 257184610Salfred .endpoint = UE_ADDR_ANY, 258184610Salfred .direction = UE_DIR_OUT, 259190734Sthompsa .bufsize = ZYD_MAX_TXBUFSZ, 260190734Sthompsa .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 261190734Sthompsa .callback = zyd_bulk_write_callback, 262184610Salfred .ep_index = 0, 263190734Sthompsa .timeout = 10000, /* 10 seconds */ 264184610Salfred }, 265188417Sthompsa [ZYD_BULK_RD] = { 266184610Salfred .type = UE_BULK, 267184610Salfred .endpoint = UE_ADDR_ANY, 268184610Salfred .direction = UE_DIR_IN, 269190734Sthompsa .bufsize = ZYX_MAX_RXBUFSZ, 270190734Sthompsa .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 271190734Sthompsa .callback = zyd_bulk_read_callback, 272184610Salfred .ep_index = 0, 273184610Salfred }, 274188417Sthompsa [ZYD_INTR_WR] = { 275184610Salfred .type = UE_BULK_INTR, 276184610Salfred .endpoint = UE_ADDR_ANY, 277184610Salfred .direction = UE_DIR_OUT, 278190734Sthompsa .bufsize = sizeof(struct zyd_cmd), 279190734Sthompsa .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 280190734Sthompsa .callback = zyd_intr_write_callback, 281190734Sthompsa .timeout = 1000, /* 1 second */ 282184610Salfred .ep_index = 1, 283184610Salfred }, 284188417Sthompsa [ZYD_INTR_RD] = { 285188417Sthompsa .type = UE_INTERRUPT, 286184610Salfred .endpoint = UE_ADDR_ANY, 287184610Salfred .direction = UE_DIR_IN, 288190734Sthompsa .bufsize = sizeof(struct zyd_cmd), 289190734Sthompsa .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 290190734Sthompsa .callback = zyd_intr_read_callback, 291184610Salfred }, 292184610Salfred}; 293188417Sthompsa#define zyd_read16_m(sc, val, data) do { \ 294188417Sthompsa error = zyd_read16(sc, val, data); \ 295188417Sthompsa if (error != 0) \ 296188417Sthompsa goto fail; \ 297188417Sthompsa} while (0) 298188417Sthompsa#define zyd_write16_m(sc, val, data) do { \ 299188417Sthompsa error = zyd_write16(sc, val, data); \ 300188417Sthompsa if (error != 0) \ 301188417Sthompsa goto fail; \ 302188417Sthompsa} while (0) 303188417Sthompsa#define zyd_read32_m(sc, val, data) do { \ 304188417Sthompsa error = zyd_read32(sc, val, data); \ 305188417Sthompsa if (error != 0) \ 306188417Sthompsa goto fail; \ 307188417Sthompsa} while (0) 308188417Sthompsa#define zyd_write32_m(sc, val, data) do { \ 309188417Sthompsa error = zyd_write32(sc, val, data); \ 310188417Sthompsa if (error != 0) \ 311188417Sthompsa goto fail; \ 312188417Sthompsa} while (0) 313184610Salfred 314188417Sthompsastatic int 315188417Sthompsazyd_match(device_t dev) 316184610Salfred{ 317192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 318184610Salfred 319192499Sthompsa if (uaa->usb_mode != USB_MODE_HOST) 320188417Sthompsa return (ENXIO); 321188419Sthompsa if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX) 322188417Sthompsa return (ENXIO); 323188417Sthompsa if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX) 324188417Sthompsa return (ENXIO); 325184610Salfred 326194228Sthompsa return (usbd_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa)); 327184610Salfred} 328184610Salfred 329188417Sthompsastatic int 330188417Sthompsazyd_attach(device_t dev) 331184610Salfred{ 332192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 333188417Sthompsa struct zyd_softc *sc = device_get_softc(dev); 334287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 335293339Savos uint8_t iface_index; 336188419Sthompsa int error; 337184610Salfred 338188417Sthompsa if (uaa->info.bcdDevice < 0x4330) { 339188417Sthompsa device_printf(dev, "device version mismatch: 0x%X " 340188417Sthompsa "(only >= 43.30 supported)\n", 341188417Sthompsa uaa->info.bcdDevice); 342188417Sthompsa return (EINVAL); 343184610Salfred } 344184610Salfred 345194228Sthompsa device_set_usb_desc(dev); 346188417Sthompsa sc->sc_dev = dev; 347188417Sthompsa sc->sc_udev = uaa->device; 348188417Sthompsa sc->sc_macrev = USB_GET_DRIVER_INFO(uaa); 349188419Sthompsa 350188417Sthompsa mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), 351188417Sthompsa MTX_NETWORK_LOCK, MTX_DEF); 352188417Sthompsa STAILQ_INIT(&sc->sc_rqh); 353287197Sglebius mbufq_init(&sc->sc_snd, ifqmaxlen); 354184610Salfred 355188417Sthompsa iface_index = ZYD_IFACE_INDEX; 356194228Sthompsa error = usbd_transfer_setup(uaa->device, 357188417Sthompsa &iface_index, sc->sc_xfer, zyd_config, 358188417Sthompsa ZYD_N_TRANSFER, sc, &sc->sc_mtx); 359188417Sthompsa if (error) { 360188417Sthompsa device_printf(dev, "could not allocate USB transfers, " 361194228Sthompsa "err=%s\n", usbd_errstr(error)); 362188419Sthompsa goto detach; 363184610Salfred } 364184610Salfred 365188419Sthompsa ZYD_LOCK(sc); 366188417Sthompsa if ((error = zyd_get_macaddr(sc)) != 0) { 367188417Sthompsa device_printf(sc->sc_dev, "could not read EEPROM\n"); 368191746Sthompsa ZYD_UNLOCK(sc); 369191746Sthompsa goto detach; 370188417Sthompsa } 371188419Sthompsa ZYD_UNLOCK(sc); 372188419Sthompsa 373283537Sglebius ic->ic_softc = sc; 374283527Sglebius ic->ic_name = device_get_nameunit(dev); 375188417Sthompsa ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 376188417Sthompsa ic->ic_opmode = IEEE80211_M_STA; 377184610Salfred 378188417Sthompsa /* set device capabilities */ 379188417Sthompsa ic->ic_caps = 380188417Sthompsa IEEE80211_C_STA /* station mode */ 381188417Sthompsa | IEEE80211_C_MONITOR /* monitor mode */ 382188417Sthompsa | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 383188417Sthompsa | IEEE80211_C_SHSLOT /* short slot time supported */ 384188417Sthompsa | IEEE80211_C_BGSCAN /* capable of bg scanning */ 385188417Sthompsa | IEEE80211_C_WPA /* 802.11i */ 386188417Sthompsa ; 387184610Salfred 388300751Savos zyd_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans, 389300751Savos ic->ic_channels); 390184610Salfred 391287197Sglebius ieee80211_ifattach(ic); 392188417Sthompsa ic->ic_raw_xmit = zyd_raw_xmit; 393188417Sthompsa ic->ic_scan_start = zyd_scan_start; 394188417Sthompsa ic->ic_scan_end = zyd_scan_end; 395300751Savos ic->ic_getradiocaps = zyd_getradiocaps; 396188417Sthompsa ic->ic_set_channel = zyd_set_channel; 397188417Sthompsa ic->ic_vap_create = zyd_vap_create; 398188417Sthompsa ic->ic_vap_delete = zyd_vap_delete; 399188417Sthompsa ic->ic_update_mcast = zyd_update_mcast; 400188601Sthompsa ic->ic_update_promisc = zyd_update_mcast; 401287197Sglebius ic->ic_parent = zyd_parent; 402287197Sglebius ic->ic_transmit = zyd_transmit; 403184610Salfred 404192468Ssam ieee80211_radiotap_attach(ic, 405192468Ssam &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 406192468Ssam ZYD_TX_RADIOTAP_PRESENT, 407192468Ssam &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 408192468Ssam ZYD_RX_RADIOTAP_PRESENT); 409184610Salfred 410188417Sthompsa if (bootverbose) 411188417Sthompsa ieee80211_announce(ic); 412184610Salfred 413191746Sthompsa return (0); 414191746Sthompsa 415191746Sthompsadetach: 416191746Sthompsa zyd_detach(dev); 417191746Sthompsa return (ENXIO); /* failure */ 418184610Salfred} 419184610Salfred 420288648Sadrianstatic void 421288648Sadrianzyd_drain_mbufq(struct zyd_softc *sc) 422288648Sadrian{ 423288648Sadrian struct mbuf *m; 424288648Sadrian struct ieee80211_node *ni; 425288648Sadrian 426288648Sadrian ZYD_LOCK_ASSERT(sc, MA_OWNED); 427288648Sadrian while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 428288648Sadrian ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 429288648Sadrian m->m_pkthdr.rcvif = NULL; 430288648Sadrian ieee80211_free_node(ni); 431288648Sadrian m_freem(m); 432288648Sadrian } 433288648Sadrian} 434288648Sadrian 435288648Sadrian 436188417Sthompsastatic int 437188417Sthompsazyd_detach(device_t dev) 438184610Salfred{ 439188417Sthompsa struct zyd_softc *sc = device_get_softc(dev); 440287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 441246614Shselasky unsigned int x; 442184610Salfred 443246614Shselasky /* 444246614Shselasky * Prevent further allocations from RX/TX data 445246614Shselasky * lists and ioctls: 446246614Shselasky */ 447246614Shselasky ZYD_LOCK(sc); 448246614Shselasky sc->sc_flags |= ZYD_FLAG_DETACHED; 449288648Sadrian zyd_drain_mbufq(sc); 450246614Shselasky STAILQ_INIT(&sc->tx_q); 451246614Shselasky STAILQ_INIT(&sc->tx_free); 452246614Shselasky ZYD_UNLOCK(sc); 453184610Salfred 454246614Shselasky /* drain USB transfers */ 455246614Shselasky for (x = 0; x != ZYD_N_TRANSFER; x++) 456246614Shselasky usbd_transfer_drain(sc->sc_xfer[x]); 457246614Shselasky 458188419Sthompsa /* free TX list, if any */ 459246614Shselasky ZYD_LOCK(sc); 460188419Sthompsa zyd_unsetup_tx_list(sc); 461246614Shselasky ZYD_UNLOCK(sc); 462188419Sthompsa 463246614Shselasky /* free USB transfers and some data buffers */ 464246614Shselasky usbd_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER); 465246614Shselasky 466287197Sglebius if (ic->ic_softc == sc) 467188417Sthompsa ieee80211_ifdetach(ic); 468188417Sthompsa mtx_destroy(&sc->sc_mtx); 469184610Salfred 470188417Sthompsa return (0); 471184610Salfred} 472184610Salfred 473188417Sthompsastatic struct ieee80211vap * 474228621Sbschmidtzyd_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 475228621Sbschmidt enum ieee80211_opmode opmode, int flags, 476228621Sbschmidt const uint8_t bssid[IEEE80211_ADDR_LEN], 477228621Sbschmidt const uint8_t mac[IEEE80211_ADDR_LEN]) 478184610Salfred{ 479188417Sthompsa struct zyd_vap *zvp; 480188417Sthompsa struct ieee80211vap *vap; 481184610Salfred 482188417Sthompsa if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 483188417Sthompsa return (NULL); 484287197Sglebius zvp = malloc(sizeof(struct zyd_vap), M_80211_VAP, M_WAITOK | M_ZERO); 485188417Sthompsa vap = &zvp->vap; 486257743Shselasky 487188417Sthompsa /* enable s/w bmiss handling for sta mode */ 488257743Shselasky if (ieee80211_vap_setup(ic, vap, name, unit, opmode, 489287197Sglebius flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) { 490257743Shselasky /* out of memory */ 491257743Shselasky free(zvp, M_80211_VAP); 492257743Shselasky return (NULL); 493257743Shselasky } 494184610Salfred 495188417Sthompsa /* override state transition machine */ 496188417Sthompsa zvp->newstate = vap->iv_newstate; 497188417Sthompsa vap->iv_newstate = zyd_newstate; 498184610Salfred 499206358Srpaulo ieee80211_ratectl_init(vap); 500206358Srpaulo ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */); 501184610Salfred 502188417Sthompsa /* complete setup */ 503188417Sthompsa ieee80211_vap_attach(vap, ieee80211_media_change, 504287197Sglebius ieee80211_media_status, mac); 505188417Sthompsa ic->ic_opmode = opmode; 506188417Sthompsa return (vap); 507184610Salfred} 508184610Salfred 509184610Salfredstatic void 510188417Sthompsazyd_vap_delete(struct ieee80211vap *vap) 511184610Salfred{ 512188417Sthompsa struct zyd_vap *zvp = ZYD_VAP(vap); 513184610Salfred 514206358Srpaulo ieee80211_ratectl_deinit(vap); 515188417Sthompsa ieee80211_vap_detach(vap); 516188417Sthompsa free(zvp, M_80211_VAP); 517184610Salfred} 518184610Salfred 519184610Salfredstatic void 520188417Sthompsazyd_tx_free(struct zyd_tx_data *data, int txerr) 521184610Salfred{ 522188417Sthompsa struct zyd_softc *sc = data->sc; 523184610Salfred 524188417Sthompsa if (data->m != NULL) { 525287197Sglebius ieee80211_tx_complete(data->ni, data->m, txerr); 526188417Sthompsa data->m = NULL; 527188417Sthompsa data->ni = NULL; 528184610Salfred } 529188417Sthompsa STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 530188417Sthompsa sc->tx_nfree++; 531184610Salfred} 532184610Salfred 533188419Sthompsastatic void 534188419Sthompsazyd_setup_tx_list(struct zyd_softc *sc) 535184610Salfred{ 536188417Sthompsa struct zyd_tx_data *data; 537188417Sthompsa int i; 538184610Salfred 539188417Sthompsa sc->tx_nfree = 0; 540188417Sthompsa STAILQ_INIT(&sc->tx_q); 541188417Sthompsa STAILQ_INIT(&sc->tx_free); 542184610Salfred 543188417Sthompsa for (i = 0; i < ZYD_TX_LIST_CNT; i++) { 544188417Sthompsa data = &sc->tx_data[i]; 545184610Salfred 546188417Sthompsa data->sc = sc; 547188417Sthompsa STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 548188417Sthompsa sc->tx_nfree++; 549188417Sthompsa } 550184610Salfred} 551184610Salfred 552184610Salfredstatic void 553188419Sthompsazyd_unsetup_tx_list(struct zyd_softc *sc) 554184610Salfred{ 555188417Sthompsa struct zyd_tx_data *data; 556188417Sthompsa int i; 557184610Salfred 558188419Sthompsa /* make sure any subsequent use of the queues will fail */ 559188419Sthompsa sc->tx_nfree = 0; 560188419Sthompsa STAILQ_INIT(&sc->tx_q); 561188419Sthompsa STAILQ_INIT(&sc->tx_free); 562184610Salfred 563188419Sthompsa /* free up all node references and mbufs */ 564188417Sthompsa for (i = 0; i < ZYD_TX_LIST_CNT; i++) { 565188417Sthompsa data = &sc->tx_data[i]; 566184610Salfred 567188417Sthompsa if (data->m != NULL) { 568188417Sthompsa m_freem(data->m); 569188417Sthompsa data->m = NULL; 570188417Sthompsa } 571188417Sthompsa if (data->ni != NULL) { 572188417Sthompsa ieee80211_free_node(data->ni); 573188417Sthompsa data->ni = NULL; 574188417Sthompsa } 575188417Sthompsa } 576184610Salfred} 577184610Salfred 578191746Sthompsastatic int 579191746Sthompsazyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 580186730Salfred{ 581188417Sthompsa struct zyd_vap *zvp = ZYD_VAP(vap); 582191746Sthompsa struct ieee80211com *ic = vap->iv_ic; 583286950Sadrian struct zyd_softc *sc = ic->ic_softc; 584188417Sthompsa int error; 585186730Salfred 586191746Sthompsa DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__, 587191746Sthompsa ieee80211_state_name[vap->iv_state], 588191746Sthompsa ieee80211_state_name[nstate]); 589191746Sthompsa 590191746Sthompsa IEEE80211_UNLOCK(ic); 591191746Sthompsa ZYD_LOCK(sc); 592191746Sthompsa switch (nstate) { 593188417Sthompsa case IEEE80211_S_AUTH: 594188417Sthompsa zyd_set_chan(sc, ic->ic_curchan); 595188417Sthompsa break; 596188417Sthompsa case IEEE80211_S_RUN: 597188417Sthompsa if (vap->iv_opmode == IEEE80211_M_MONITOR) 598188417Sthompsa break; 599184610Salfred 600188417Sthompsa /* turn link LED on */ 601188417Sthompsa error = zyd_set_led(sc, ZYD_LED1, 1); 602188417Sthompsa if (error != 0) 603191746Sthompsa break; 604191746Sthompsa 605188417Sthompsa /* make data LED blink upon Tx */ 606188417Sthompsa zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1); 607191746Sthompsa 608296356Savos IEEE80211_ADDR_COPY(sc->sc_bssid, vap->iv_bss->ni_bssid); 609296356Savos zyd_set_bssid(sc, sc->sc_bssid); 610188417Sthompsa break; 611188417Sthompsa default: 612188417Sthompsa break; 613184610Salfred } 614188417Sthompsafail: 615188417Sthompsa ZYD_UNLOCK(sc); 616188417Sthompsa IEEE80211_LOCK(ic); 617191746Sthompsa return (zvp->newstate(vap, nstate, arg)); 618184610Salfred} 619184610Salfred 620188417Sthompsa/* 621188417Sthompsa * Callback handler for interrupt transfer 622188417Sthompsa */ 623184610Salfredstatic void 624194677Sthompsazyd_intr_read_callback(struct usb_xfer *xfer, usb_error_t error) 625184610Salfred{ 626194677Sthompsa struct zyd_softc *sc = usbd_xfer_softc(xfer); 627287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 628188417Sthompsa struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 629184610Salfred struct ieee80211_node *ni; 630188417Sthompsa struct zyd_cmd *cmd = &sc->sc_ibuf; 631194677Sthompsa struct usb_page_cache *pc; 632188417Sthompsa int datalen; 633194677Sthompsa int actlen; 634184610Salfred 635194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 636194677Sthompsa 637184610Salfred switch (USB_GET_STATE(xfer)) { 638184610Salfred case USB_ST_TRANSFERRED: 639194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 640194677Sthompsa usbd_copy_out(pc, 0, cmd, sizeof(*cmd)); 641184610Salfred 642188417Sthompsa switch (le16toh(cmd->code)) { 643188417Sthompsa case ZYD_NOTIF_RETRYSTATUS: 644188417Sthompsa { 645188417Sthompsa struct zyd_notif_retry *retry = 646188417Sthompsa (struct zyd_notif_retry *)cmd->data; 647184610Salfred 648188417Sthompsa DPRINTF(sc, ZYD_DEBUG_TX_PROC, 649188417Sthompsa "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n", 650188417Sthompsa le16toh(retry->rate), ether_sprintf(retry->macaddr), 651188417Sthompsa le16toh(retry->count)&0xff, le16toh(retry->count)); 652184610Salfred 653188417Sthompsa /* 654188417Sthompsa * Find the node to which the packet was sent and 655188417Sthompsa * update its retry statistics. In BSS mode, this node 656188417Sthompsa * is the AP we're associated to so no lookup is 657188417Sthompsa * actually needed. 658188417Sthompsa */ 659188417Sthompsa ni = ieee80211_find_txnode(vap, retry->macaddr); 660188417Sthompsa if (ni != NULL) { 661206358Srpaulo int retrycnt = 662206358Srpaulo (int)(le16toh(retry->count) & 0xff); 663206358Srpaulo 664206358Srpaulo ieee80211_ratectl_tx_complete(vap, ni, 665206358Srpaulo IEEE80211_RATECTL_TX_FAILURE, 666206358Srpaulo &retrycnt, NULL); 667188417Sthompsa ieee80211_free_node(ni); 668188417Sthompsa } 669188417Sthompsa if (le16toh(retry->count) & 0x100) 670287197Sglebius /* too many retries */ 671287197Sglebius if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 672287197Sglebius 1); 673188417Sthompsa break; 674188417Sthompsa } 675188417Sthompsa case ZYD_NOTIF_IORD: 676188417Sthompsa { 677188417Sthompsa struct zyd_rq *rqp; 678184610Salfred 679188417Sthompsa if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT) 680188417Sthompsa break; /* HMAC interrupt */ 681184610Salfred 682194677Sthompsa datalen = actlen - sizeof(cmd->code); 683188417Sthompsa datalen -= 2; /* XXX: padding? */ 684184610Salfred 685188417Sthompsa STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) { 686233774Shselasky int i; 687233774Shselasky int count; 688184610Salfred 689188417Sthompsa if (rqp->olen != datalen) 690188417Sthompsa continue; 691233774Shselasky count = rqp->olen / sizeof(struct zyd_pair); 692233774Shselasky for (i = 0; i < count; i++) { 693188417Sthompsa if (*(((const uint16_t *)rqp->idata) + i) != 694188417Sthompsa (((struct zyd_pair *)cmd->data) + i)->reg) 695188417Sthompsa break; 696184610Salfred } 697233774Shselasky if (i != count) 698188417Sthompsa continue; 699188417Sthompsa /* copy answer into caller-supplied buffer */ 700227461Shselasky memcpy(rqp->odata, cmd->data, rqp->olen); 701188417Sthompsa DPRINTF(sc, ZYD_DEBUG_CMD, 702188417Sthompsa "command %p complete, data = %*D \n", 703290328Shselasky rqp, rqp->olen, (char *)rqp->odata, ":"); 704188417Sthompsa wakeup(rqp); /* wakeup caller */ 705188417Sthompsa break; 706184610Salfred } 707188417Sthompsa if (rqp == NULL) { 708188417Sthompsa device_printf(sc->sc_dev, 709188417Sthompsa "unexpected IORD notification %*D\n", 710290328Shselasky datalen, cmd->data, ":"); 711188417Sthompsa } 712188417Sthompsa break; 713184610Salfred } 714188417Sthompsa default: 715188417Sthompsa device_printf(sc->sc_dev, "unknown notification %x\n", 716188417Sthompsa le16toh(cmd->code)); 717188417Sthompsa } 718184610Salfred 719188417Sthompsa /* FALLTHROUGH */ 720184610Salfred case USB_ST_SETUP: 721184610Salfredtr_setup: 722194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 723194228Sthompsa usbd_transfer_submit(xfer); 724188417Sthompsa break; 725184610Salfred 726188417Sthompsa default: /* Error */ 727188417Sthompsa DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n", 728194677Sthompsa usbd_errstr(error)); 729188417Sthompsa 730194677Sthompsa if (error != USB_ERR_CANCELLED) { 731188417Sthompsa /* try to clear stall first */ 732194677Sthompsa usbd_xfer_set_stall(xfer); 733188417Sthompsa goto tr_setup; 734184610Salfred } 735188417Sthompsa break; 736188417Sthompsa } 737188417Sthompsa} 738184610Salfred 739188417Sthompsastatic void 740194677Sthompsazyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error) 741188417Sthompsa{ 742194677Sthompsa struct zyd_softc *sc = usbd_xfer_softc(xfer); 743194677Sthompsa struct zyd_rq *rqp, *cmd; 744194677Sthompsa struct usb_page_cache *pc; 745184610Salfred 746188417Sthompsa switch (USB_GET_STATE(xfer)) { 747188417Sthompsa case USB_ST_TRANSFERRED: 748194677Sthompsa cmd = usbd_xfer_get_priv(xfer); 749194677Sthompsa DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd); 750189452Sthompsa STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) { 751189452Sthompsa /* Ensure the cached rq pointer is still valid */ 752194677Sthompsa if (rqp == cmd && 753189452Sthompsa (rqp->flags & ZYD_CMD_FLAG_READ) == 0) 754189452Sthompsa wakeup(rqp); /* wakeup caller */ 755189452Sthompsa } 756184610Salfred 757188417Sthompsa /* FALLTHROUGH */ 758188417Sthompsa case USB_ST_SETUP: 759188417Sthompsatr_setup: 760188417Sthompsa STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) { 761188417Sthompsa if (rqp->flags & ZYD_CMD_FLAG_SENT) 762188417Sthompsa continue; 763184610Salfred 764194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 765194677Sthompsa usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen); 766184610Salfred 767194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, rqp->ilen); 768194677Sthompsa usbd_xfer_set_priv(xfer, rqp); 769188417Sthompsa rqp->flags |= ZYD_CMD_FLAG_SENT; 770194228Sthompsa usbd_transfer_submit(xfer); 771188417Sthompsa break; 772184610Salfred } 773184610Salfred break; 774184610Salfred 775184610Salfred default: /* Error */ 776188417Sthompsa DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n", 777194677Sthompsa usbd_errstr(error)); 778184610Salfred 779194677Sthompsa if (error != USB_ERR_CANCELLED) { 780184610Salfred /* try to clear stall first */ 781194677Sthompsa usbd_xfer_set_stall(xfer); 782188417Sthompsa goto tr_setup; 783184610Salfred } 784184610Salfred break; 785184610Salfred } 786184610Salfred} 787184610Salfred 788188417Sthompsastatic int 789188417Sthompsazyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen, 790188601Sthompsa void *odata, int olen, int flags) 791184610Salfred{ 792188417Sthompsa struct zyd_cmd cmd; 793188417Sthompsa struct zyd_rq rq; 794188417Sthompsa int error; 795184610Salfred 796233774Shselasky if (ilen > (int)sizeof(cmd.data)) 797188417Sthompsa return (EINVAL); 798188419Sthompsa 799188417Sthompsa cmd.code = htole16(code); 800227461Shselasky memcpy(cmd.data, idata, ilen); 801188417Sthompsa DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n", 802290328Shselasky &rq, ilen, idata, ":"); 803184610Salfred 804188417Sthompsa rq.cmd = &cmd; 805188417Sthompsa rq.idata = idata; 806188417Sthompsa rq.odata = odata; 807188417Sthompsa rq.ilen = sizeof(uint16_t) + ilen; 808188417Sthompsa rq.olen = olen; 809188417Sthompsa rq.flags = flags; 810188417Sthompsa STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq); 811194228Sthompsa usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]); 812194228Sthompsa usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]); 813184610Salfred 814188417Sthompsa /* wait at most one second for command reply */ 815188417Sthompsa error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz); 816188417Sthompsa if (error) 817188417Sthompsa device_printf(sc->sc_dev, "command timeout\n"); 818188417Sthompsa STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq); 819188417Sthompsa DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n", 820188417Sthompsa &rq, error); 821184610Salfred 822188417Sthompsa return (error); 823184610Salfred} 824184610Salfred 825184610Salfredstatic int 826188417Sthompsazyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val) 827184610Salfred{ 828188417Sthompsa struct zyd_pair tmp; 829188417Sthompsa int error; 830184610Salfred 831188417Sthompsa reg = htole16(reg); 832188417Sthompsa error = zyd_cmd(sc, ZYD_CMD_IORD, ®, sizeof(reg), &tmp, sizeof(tmp), 833188417Sthompsa ZYD_CMD_FLAG_READ); 834188417Sthompsa if (error == 0) 835188417Sthompsa *val = le16toh(tmp.val); 836188417Sthompsa return (error); 837184610Salfred} 838184610Salfred 839184610Salfredstatic int 840188417Sthompsazyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val) 841184610Salfred{ 842188417Sthompsa struct zyd_pair tmp[2]; 843188417Sthompsa uint16_t regs[2]; 844184610Salfred int error; 845184610Salfred 846188417Sthompsa regs[0] = htole16(ZYD_REG32_HI(reg)); 847188417Sthompsa regs[1] = htole16(ZYD_REG32_LO(reg)); 848188417Sthompsa error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp), 849188417Sthompsa ZYD_CMD_FLAG_READ); 850188417Sthompsa if (error == 0) 851188417Sthompsa *val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val); 852188417Sthompsa return (error); 853188417Sthompsa} 854184610Salfred 855188417Sthompsastatic int 856188417Sthompsazyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val) 857188417Sthompsa{ 858188417Sthompsa struct zyd_pair pair; 859184610Salfred 860188417Sthompsa pair.reg = htole16(reg); 861188417Sthompsa pair.val = htole16(val); 862184610Salfred 863188417Sthompsa return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0); 864188417Sthompsa} 865184610Salfred 866188417Sthompsastatic int 867188417Sthompsazyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val) 868188417Sthompsa{ 869188417Sthompsa struct zyd_pair pair[2]; 870184610Salfred 871188417Sthompsa pair[0].reg = htole16(ZYD_REG32_HI(reg)); 872188417Sthompsa pair[0].val = htole16(val >> 16); 873188417Sthompsa pair[1].reg = htole16(ZYD_REG32_LO(reg)); 874188417Sthompsa pair[1].val = htole16(val & 0xffff); 875184610Salfred 876188417Sthompsa return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0); 877184610Salfred} 878184610Salfred 879188417Sthompsastatic int 880188417Sthompsazyd_rfwrite(struct zyd_softc *sc, uint32_t val) 881184610Salfred{ 882188417Sthompsa struct zyd_rf *rf = &sc->sc_rf; 883188417Sthompsa struct zyd_rfwrite_cmd req; 884188417Sthompsa uint16_t cr203; 885188417Sthompsa int error, i; 886184610Salfred 887188417Sthompsa zyd_read16_m(sc, ZYD_CR203, &cr203); 888188417Sthompsa cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA); 889188417Sthompsa 890188417Sthompsa req.code = htole16(2); 891188417Sthompsa req.width = htole16(rf->width); 892188417Sthompsa for (i = 0; i < rf->width; i++) { 893188417Sthompsa req.bit[i] = htole16(cr203); 894188417Sthompsa if (val & (1 << (rf->width - 1 - i))) 895188417Sthompsa req.bit[i] |= htole16(ZYD_RF_DATA); 896188417Sthompsa } 897188417Sthompsa error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0); 898188417Sthompsafail: 899188417Sthompsa return (error); 900184610Salfred} 901184610Salfred 902188417Sthompsastatic int 903188417Sthompsazyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val) 904184610Salfred{ 905188417Sthompsa int error; 906184610Salfred 907188417Sthompsa zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff); 908188417Sthompsa zyd_write16_m(sc, ZYD_CR243, (val >> 8) & 0xff); 909188417Sthompsa zyd_write16_m(sc, ZYD_CR242, (val >> 0) & 0xff); 910188417Sthompsafail: 911188417Sthompsa return (error); 912184610Salfred} 913184610Salfred 914188417Sthompsastatic int 915188417Sthompsazyd_lock_phy(struct zyd_softc *sc) 916184610Salfred{ 917188417Sthompsa int error; 918188417Sthompsa uint32_t tmp; 919186730Salfred 920188417Sthompsa zyd_read32_m(sc, ZYD_MAC_MISC, &tmp); 921188417Sthompsa tmp &= ~ZYD_UNLOCK_PHY_REGS; 922188417Sthompsa zyd_write32_m(sc, ZYD_MAC_MISC, tmp); 923188417Sthompsafail: 924188417Sthompsa return (error); 925184610Salfred} 926184610Salfred 927188417Sthompsastatic int 928188417Sthompsazyd_unlock_phy(struct zyd_softc *sc) 929184610Salfred{ 930188417Sthompsa int error; 931188417Sthompsa uint32_t tmp; 932184610Salfred 933188417Sthompsa zyd_read32_m(sc, ZYD_MAC_MISC, &tmp); 934188417Sthompsa tmp |= ZYD_UNLOCK_PHY_REGS; 935188417Sthompsa zyd_write32_m(sc, ZYD_MAC_MISC, tmp); 936188417Sthompsafail: 937188417Sthompsa return (error); 938184610Salfred} 939184610Salfred 940184610Salfred/* 941188417Sthompsa * RFMD RF methods. 942184610Salfred */ 943188417Sthompsastatic int 944188417Sthompsazyd_rfmd_init(struct zyd_rf *rf) 945184610Salfred{ 946188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 947184610Salfred static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY; 948184610Salfred static const uint32_t rfini[] = ZYD_RFMD_RF; 949188417Sthompsa int i, error; 950184610Salfred 951184610Salfred /* init RF-dependent PHY registers */ 952288088Sadrian for (i = 0; i < nitems(phyini); i++) { 953188417Sthompsa zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 954184610Salfred } 955184610Salfred 956184610Salfred /* init RFMD radio */ 957288088Sadrian for (i = 0; i < nitems(rfini); i++) { 958188417Sthompsa if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 959188417Sthompsa return (error); 960184610Salfred } 961188417Sthompsafail: 962188417Sthompsa return (error); 963184610Salfred} 964184610Salfred 965188417Sthompsastatic int 966188417Sthompsazyd_rfmd_switch_radio(struct zyd_rf *rf, int on) 967184610Salfred{ 968188417Sthompsa int error; 969188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 970188417Sthompsa 971188417Sthompsa zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15); 972188417Sthompsa zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81); 973188417Sthompsafail: 974188417Sthompsa return (error); 975184610Salfred} 976184610Salfred 977188417Sthompsastatic int 978188417Sthompsazyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan) 979184610Salfred{ 980188417Sthompsa int error; 981188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 982184610Salfred static const struct { 983188417Sthompsa uint32_t r1, r2; 984188417Sthompsa } rfprog[] = ZYD_RFMD_CHANTABLE; 985184610Salfred 986188417Sthompsa error = zyd_rfwrite(sc, rfprog[chan - 1].r1); 987188417Sthompsa if (error != 0) 988188417Sthompsa goto fail; 989188417Sthompsa error = zyd_rfwrite(sc, rfprog[chan - 1].r2); 990188417Sthompsa if (error != 0) 991188417Sthompsa goto fail; 992184610Salfred 993188417Sthompsafail: 994188417Sthompsa return (error); 995184610Salfred} 996184610Salfred 997184610Salfred/* 998188417Sthompsa * AL2230 RF methods. 999184610Salfred */ 1000188417Sthompsastatic int 1001188417Sthompsazyd_al2230_init(struct zyd_rf *rf) 1002184610Salfred{ 1003188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1004184610Salfred static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY; 1005186730Salfred static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT; 1006186730Salfred static const struct zyd_phy_pair phypll[] = { 1007188417Sthompsa { ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f }, 1008188417Sthompsa { ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 } 1009186730Salfred }; 1010186730Salfred static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1; 1011186730Salfred static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2; 1012186730Salfred static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3; 1013188417Sthompsa int i, error; 1014184610Salfred 1015184610Salfred /* init RF-dependent PHY registers */ 1016288088Sadrian for (i = 0; i < nitems(phyini); i++) 1017188417Sthompsa zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1018186730Salfred 1019188417Sthompsa if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) { 1020288088Sadrian for (i = 0; i < nitems(phy2230s); i++) 1021188417Sthompsa zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val); 1022184610Salfred } 1023188417Sthompsa 1024186730Salfred /* init AL2230 radio */ 1025288088Sadrian for (i = 0; i < nitems(rfini1); i++) { 1026188417Sthompsa error = zyd_rfwrite(sc, rfini1[i]); 1027188417Sthompsa if (error != 0) 1028188417Sthompsa goto fail; 1029188417Sthompsa } 1030184610Salfred 1031188417Sthompsa if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) 1032188417Sthompsa error = zyd_rfwrite(sc, 0x000824); 1033186730Salfred else 1034188417Sthompsa error = zyd_rfwrite(sc, 0x0005a4); 1035188417Sthompsa if (error != 0) 1036188417Sthompsa goto fail; 1037186730Salfred 1038288088Sadrian for (i = 0; i < nitems(rfini2); i++) { 1039188417Sthompsa error = zyd_rfwrite(sc, rfini2[i]); 1040188417Sthompsa if (error != 0) 1041188417Sthompsa goto fail; 1042188417Sthompsa } 1043186730Salfred 1044288088Sadrian for (i = 0; i < nitems(phypll); i++) 1045188417Sthompsa zyd_write16_m(sc, phypll[i].reg, phypll[i].val); 1046186730Salfred 1047288088Sadrian for (i = 0; i < nitems(rfini3); i++) { 1048188417Sthompsa error = zyd_rfwrite(sc, rfini3[i]); 1049188417Sthompsa if (error != 0) 1050188417Sthompsa goto fail; 1051188417Sthompsa } 1052188417Sthompsafail: 1053188417Sthompsa return (error); 1054184610Salfred} 1055184610Salfred 1056188417Sthompsastatic int 1057188417Sthompsazyd_al2230_fini(struct zyd_rf *rf) 1058186730Salfred{ 1059188417Sthompsa int error, i; 1060188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1061186730Salfred static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1; 1062186730Salfred 1063288088Sadrian for (i = 0; i < nitems(phy); i++) 1064188417Sthompsa zyd_write16_m(sc, phy[i].reg, phy[i].val); 1065186730Salfred 1066186730Salfred if (sc->sc_newphy != 0) 1067188417Sthompsa zyd_write16_m(sc, ZYD_CR9, 0xe1); 1068188417Sthompsa 1069188417Sthompsa zyd_write16_m(sc, ZYD_CR203, 0x6); 1070188417Sthompsafail: 1071188417Sthompsa return (error); 1072186730Salfred} 1073186730Salfred 1074188417Sthompsastatic int 1075188417Sthompsazyd_al2230_init_b(struct zyd_rf *rf) 1076184610Salfred{ 1077188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1078186730Salfred static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1; 1079186730Salfred static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2; 1080186730Salfred static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3; 1081186730Salfred static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT; 1082188417Sthompsa static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B; 1083186730Salfred static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1; 1084186730Salfred static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2; 1085186730Salfred static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3; 1086186730Salfred static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE; 1087188417Sthompsa int i, error; 1088184610Salfred 1089288088Sadrian for (i = 0; i < nitems(phy1); i++) 1090188417Sthompsa zyd_write16_m(sc, phy1[i].reg, phy1[i].val); 1091186730Salfred 1092184610Salfred /* init RF-dependent PHY registers */ 1093288088Sadrian for (i = 0; i < nitems(phyini); i++) 1094188417Sthompsa zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1095184610Salfred 1096188417Sthompsa if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) { 1097288088Sadrian for (i = 0; i < nitems(phy2230s); i++) 1098188417Sthompsa zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val); 1099188417Sthompsa } 1100186730Salfred 1101188417Sthompsa for (i = 0; i < 3; i++) { 1102188417Sthompsa error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]); 1103188417Sthompsa if (error != 0) 1104188417Sthompsa return (error); 1105188417Sthompsa } 1106186730Salfred 1107288088Sadrian for (i = 0; i < nitems(rfini_part1); i++) { 1108188417Sthompsa error = zyd_rfwrite_cr(sc, rfini_part1[i]); 1109188417Sthompsa if (error != 0) 1110188417Sthompsa return (error); 1111188417Sthompsa } 1112186730Salfred 1113188417Sthompsa if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) 1114188417Sthompsa error = zyd_rfwrite(sc, 0x241000); 1115186730Salfred else 1116188417Sthompsa error = zyd_rfwrite(sc, 0x25a000); 1117188417Sthompsa if (error != 0) 1118188417Sthompsa goto fail; 1119186730Salfred 1120288088Sadrian for (i = 0; i < nitems(rfini_part2); i++) { 1121188417Sthompsa error = zyd_rfwrite_cr(sc, rfini_part2[i]); 1122188417Sthompsa if (error != 0) 1123188417Sthompsa return (error); 1124188417Sthompsa } 1125186730Salfred 1126288088Sadrian for (i = 0; i < nitems(phy2); i++) 1127188417Sthompsa zyd_write16_m(sc, phy2[i].reg, phy2[i].val); 1128186730Salfred 1129288088Sadrian for (i = 0; i < nitems(rfini_part3); i++) { 1130188417Sthompsa error = zyd_rfwrite_cr(sc, rfini_part3[i]); 1131188417Sthompsa if (error != 0) 1132188417Sthompsa return (error); 1133188417Sthompsa } 1134186730Salfred 1135288088Sadrian for (i = 0; i < nitems(phy3); i++) 1136188417Sthompsa zyd_write16_m(sc, phy3[i].reg, phy3[i].val); 1137186730Salfred 1138188417Sthompsa error = zyd_al2230_fini(rf); 1139188417Sthompsafail: 1140188417Sthompsa return (error); 1141184610Salfred} 1142184610Salfred 1143188417Sthompsastatic int 1144188417Sthompsazyd_al2230_switch_radio(struct zyd_rf *rf, int on) 1145184610Salfred{ 1146188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1147188417Sthompsa int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f; 1148188417Sthompsa 1149188417Sthompsa zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04); 1150188417Sthompsa zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f); 1151188417Sthompsafail: 1152188417Sthompsa return (error); 1153188417Sthompsa} 1154188417Sthompsa 1155188417Sthompsastatic int 1156188417Sthompsazyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan) 1157188417Sthompsa{ 1158188417Sthompsa int error, i; 1159188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1160186730Salfred static const struct zyd_phy_pair phy1[] = { 1161188417Sthompsa { ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }, 1162186730Salfred }; 1163184610Salfred static const struct { 1164188417Sthompsa uint32_t r1, r2, r3; 1165188417Sthompsa } rfprog[] = ZYD_AL2230_CHANTABLE; 1166184610Salfred 1167188417Sthompsa error = zyd_rfwrite(sc, rfprog[chan - 1].r1); 1168188417Sthompsa if (error != 0) 1169188417Sthompsa goto fail; 1170188417Sthompsa error = zyd_rfwrite(sc, rfprog[chan - 1].r2); 1171188417Sthompsa if (error != 0) 1172188417Sthompsa goto fail; 1173188417Sthompsa error = zyd_rfwrite(sc, rfprog[chan - 1].r3); 1174188417Sthompsa if (error != 0) 1175188417Sthompsa goto fail; 1176184610Salfred 1177288088Sadrian for (i = 0; i < nitems(phy1); i++) 1178188417Sthompsa zyd_write16_m(sc, phy1[i].reg, phy1[i].val); 1179188417Sthompsafail: 1180188417Sthompsa return (error); 1181184610Salfred} 1182184610Salfred 1183188417Sthompsastatic int 1184188417Sthompsazyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan) 1185186730Salfred{ 1186188417Sthompsa int error, i; 1187188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1188186730Salfred static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1; 1189186730Salfred static const struct { 1190188417Sthompsa uint32_t r1, r2, r3; 1191188417Sthompsa } rfprog[] = ZYD_AL2230_CHANTABLE_B; 1192186730Salfred 1193288088Sadrian for (i = 0; i < nitems(phy1); i++) 1194188417Sthompsa zyd_write16_m(sc, phy1[i].reg, phy1[i].val); 1195186730Salfred 1196188417Sthompsa error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1); 1197188417Sthompsa if (error != 0) 1198188417Sthompsa goto fail; 1199188417Sthompsa error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2); 1200188417Sthompsa if (error != 0) 1201188417Sthompsa goto fail; 1202188417Sthompsa error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3); 1203188417Sthompsa if (error != 0) 1204188417Sthompsa goto fail; 1205188417Sthompsa error = zyd_al2230_fini(rf); 1206188417Sthompsafail: 1207188417Sthompsa return (error); 1208186730Salfred} 1209186730Salfred 1210186730Salfred#define ZYD_AL2230_PHY_BANDEDGE6 \ 1211186730Salfred{ \ 1212188417Sthompsa { ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 }, \ 1213186730Salfred { ZYD_CR47, 0x1e } \ 1214186730Salfred} 1215186730Salfred 1216188417Sthompsastatic int 1217188417Sthompsazyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c) 1218186730Salfred{ 1219188417Sthompsa int error = 0, i; 1220188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1221287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 1222186730Salfred struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6; 1223188601Sthompsa int chan = ieee80211_chan2ieee(ic, c); 1224186730Salfred 1225188417Sthompsa if (chan == 1 || chan == 11) 1226186730Salfred r[0].val = 0x12; 1227188417Sthompsa 1228288088Sadrian for (i = 0; i < nitems(r); i++) 1229188417Sthompsa zyd_write16_m(sc, r[i].reg, r[i].val); 1230188417Sthompsafail: 1231188417Sthompsa return (error); 1232186730Salfred} 1233186730Salfred 1234184610Salfred/* 1235184610Salfred * AL7230B RF methods. 1236184610Salfred */ 1237188417Sthompsastatic int 1238188417Sthompsazyd_al7230B_init(struct zyd_rf *rf) 1239184610Salfred{ 1240188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1241184610Salfred static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1; 1242184610Salfred static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2; 1243184610Salfred static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3; 1244184610Salfred static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1; 1245184610Salfred static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2; 1246188417Sthompsa int i, error; 1247184610Salfred 1248184610Salfred /* for AL7230B, PHY and RF need to be initialized in "phases" */ 1249184610Salfred 1250184610Salfred /* init RF-dependent PHY registers, part one */ 1251288088Sadrian for (i = 0; i < nitems(phyini_1); i++) 1252188417Sthompsa zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val); 1253188417Sthompsa 1254184610Salfred /* init AL7230B radio, part one */ 1255288088Sadrian for (i = 0; i < nitems(rfini_1); i++) { 1256188417Sthompsa if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0) 1257188417Sthompsa return (error); 1258184610Salfred } 1259184610Salfred /* init RF-dependent PHY registers, part two */ 1260288088Sadrian for (i = 0; i < nitems(phyini_2); i++) 1261188417Sthompsa zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val); 1262188417Sthompsa 1263184610Salfred /* init AL7230B radio, part two */ 1264288088Sadrian for (i = 0; i < nitems(rfini_2); i++) { 1265188417Sthompsa if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0) 1266188417Sthompsa return (error); 1267184610Salfred } 1268184610Salfred /* init RF-dependent PHY registers, part three */ 1269288088Sadrian for (i = 0; i < nitems(phyini_3); i++) 1270188417Sthompsa zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val); 1271188417Sthompsafail: 1272188417Sthompsa return (error); 1273184610Salfred} 1274184610Salfred 1275188417Sthompsastatic int 1276188417Sthompsazyd_al7230B_switch_radio(struct zyd_rf *rf, int on) 1277184610Salfred{ 1278188417Sthompsa int error; 1279188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1280188417Sthompsa 1281188417Sthompsa zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04); 1282188417Sthompsa zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f); 1283188417Sthompsafail: 1284188417Sthompsa return (error); 1285188417Sthompsa} 1286188417Sthompsa 1287188417Sthompsastatic int 1288188417Sthompsazyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan) 1289188417Sthompsa{ 1290188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1291184610Salfred static const struct { 1292188417Sthompsa uint32_t r1, r2; 1293188417Sthompsa } rfprog[] = ZYD_AL7230B_CHANTABLE; 1294184610Salfred static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL; 1295188417Sthompsa int i, error; 1296184610Salfred 1297188417Sthompsa zyd_write16_m(sc, ZYD_CR240, 0x57); 1298188417Sthompsa zyd_write16_m(sc, ZYD_CR251, 0x2f); 1299184610Salfred 1300288088Sadrian for (i = 0; i < nitems(rfsc); i++) { 1301188417Sthompsa if ((error = zyd_rfwrite(sc, rfsc[i])) != 0) 1302188417Sthompsa return (error); 1303184610Salfred } 1304184610Salfred 1305188417Sthompsa zyd_write16_m(sc, ZYD_CR128, 0x14); 1306188417Sthompsa zyd_write16_m(sc, ZYD_CR129, 0x12); 1307188417Sthompsa zyd_write16_m(sc, ZYD_CR130, 0x10); 1308188417Sthompsa zyd_write16_m(sc, ZYD_CR38, 0x38); 1309188417Sthompsa zyd_write16_m(sc, ZYD_CR136, 0xdf); 1310184610Salfred 1311188417Sthompsa error = zyd_rfwrite(sc, rfprog[chan - 1].r1); 1312188417Sthompsa if (error != 0) 1313188417Sthompsa goto fail; 1314188417Sthompsa error = zyd_rfwrite(sc, rfprog[chan - 1].r2); 1315188417Sthompsa if (error != 0) 1316188417Sthompsa goto fail; 1317188417Sthompsa error = zyd_rfwrite(sc, 0x3c9000); 1318188417Sthompsa if (error != 0) 1319188417Sthompsa goto fail; 1320184610Salfred 1321188417Sthompsa zyd_write16_m(sc, ZYD_CR251, 0x3f); 1322188417Sthompsa zyd_write16_m(sc, ZYD_CR203, 0x06); 1323188417Sthompsa zyd_write16_m(sc, ZYD_CR240, 0x08); 1324188417Sthompsafail: 1325188417Sthompsa return (error); 1326184610Salfred} 1327184610Salfred 1328184610Salfred/* 1329184610Salfred * AL2210 RF methods. 1330184610Salfred */ 1331188417Sthompsastatic int 1332188417Sthompsazyd_al2210_init(struct zyd_rf *rf) 1333184610Salfred{ 1334188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1335184610Salfred static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY; 1336184610Salfred static const uint32_t rfini[] = ZYD_AL2210_RF; 1337184610Salfred uint32_t tmp; 1338188417Sthompsa int i, error; 1339184610Salfred 1340188417Sthompsa zyd_write32_m(sc, ZYD_CR18, 2); 1341184610Salfred 1342184610Salfred /* init RF-dependent PHY registers */ 1343288088Sadrian for (i = 0; i < nitems(phyini); i++) 1344188417Sthompsa zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1345188417Sthompsa 1346184610Salfred /* init AL2210 radio */ 1347288088Sadrian for (i = 0; i < nitems(rfini); i++) { 1348188417Sthompsa if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1349188417Sthompsa return (error); 1350184610Salfred } 1351188417Sthompsa zyd_write16_m(sc, ZYD_CR47, 0x1e); 1352188417Sthompsa zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp); 1353188417Sthompsa zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1); 1354188417Sthompsa zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1); 1355188417Sthompsa zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05); 1356188417Sthompsa zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00); 1357188417Sthompsa zyd_write16_m(sc, ZYD_CR47, 0x1e); 1358188417Sthompsa zyd_write32_m(sc, ZYD_CR18, 3); 1359188417Sthompsafail: 1360188417Sthompsa return (error); 1361184610Salfred} 1362184610Salfred 1363188417Sthompsastatic int 1364188417Sthompsazyd_al2210_switch_radio(struct zyd_rf *rf, int on) 1365184610Salfred{ 1366188417Sthompsa /* vendor driver does nothing for this RF chip */ 1367188417Sthompsa 1368188417Sthompsa return (0); 1369188417Sthompsa} 1370188417Sthompsa 1371188417Sthompsastatic int 1372188417Sthompsazyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan) 1373188417Sthompsa{ 1374188417Sthompsa int error; 1375188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1376184610Salfred static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE; 1377184610Salfred uint32_t tmp; 1378184610Salfred 1379188417Sthompsa zyd_write32_m(sc, ZYD_CR18, 2); 1380188417Sthompsa zyd_write16_m(sc, ZYD_CR47, 0x1e); 1381188417Sthompsa zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp); 1382188417Sthompsa zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1); 1383188417Sthompsa zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1); 1384188417Sthompsa zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05); 1385188417Sthompsa zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00); 1386188417Sthompsa zyd_write16_m(sc, ZYD_CR47, 0x1e); 1387184610Salfred 1388184610Salfred /* actually set the channel */ 1389188417Sthompsa error = zyd_rfwrite(sc, rfprog[chan - 1]); 1390188417Sthompsa if (error != 0) 1391188417Sthompsa goto fail; 1392184610Salfred 1393188417Sthompsa zyd_write32_m(sc, ZYD_CR18, 3); 1394188417Sthompsafail: 1395188417Sthompsa return (error); 1396184610Salfred} 1397184610Salfred 1398184610Salfred/* 1399184610Salfred * GCT RF methods. 1400184610Salfred */ 1401188417Sthompsastatic int 1402188417Sthompsazyd_gct_init(struct zyd_rf *rf) 1403184610Salfred{ 1404193420Sweongyo#define ZYD_GCT_INTR_REG 0x85c1 1405188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1406184610Salfred static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY; 1407184610Salfred static const uint32_t rfini[] = ZYD_GCT_RF; 1408193420Sweongyo static const uint16_t vco[11][7] = ZYD_GCT_VCO; 1409193420Sweongyo int i, idx = -1, error; 1410193420Sweongyo uint16_t data; 1411184610Salfred 1412184610Salfred /* init RF-dependent PHY registers */ 1413288088Sadrian for (i = 0; i < nitems(phyini); i++) 1414188417Sthompsa zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1415188417Sthompsa 1416184610Salfred /* init cgt radio */ 1417288088Sadrian for (i = 0; i < nitems(rfini); i++) { 1418188417Sthompsa if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1419188417Sthompsa return (error); 1420184610Salfred } 1421193420Sweongyo 1422193420Sweongyo error = zyd_gct_mode(rf); 1423193420Sweongyo if (error != 0) 1424193420Sweongyo return (error); 1425193420Sweongyo 1426288088Sadrian for (i = 0; i < (int)(nitems(vco) - 1); i++) { 1427193420Sweongyo error = zyd_gct_set_channel_synth(rf, 1, 0); 1428193420Sweongyo if (error != 0) 1429193420Sweongyo goto fail; 1430193420Sweongyo error = zyd_gct_write(rf, vco[i][0]); 1431193420Sweongyo if (error != 0) 1432193420Sweongyo goto fail; 1433193420Sweongyo zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf); 1434193420Sweongyo zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data); 1435193420Sweongyo if ((data & 0xf) == 0) { 1436193420Sweongyo idx = i; 1437193420Sweongyo break; 1438193420Sweongyo } 1439193420Sweongyo } 1440193420Sweongyo if (idx == -1) { 1441193420Sweongyo error = zyd_gct_set_channel_synth(rf, 1, 1); 1442193420Sweongyo if (error != 0) 1443193420Sweongyo goto fail; 1444193420Sweongyo error = zyd_gct_write(rf, 0x6662); 1445193420Sweongyo if (error != 0) 1446193420Sweongyo goto fail; 1447193420Sweongyo } 1448193420Sweongyo 1449193420Sweongyo rf->idx = idx; 1450193420Sweongyo zyd_write16_m(sc, ZYD_CR203, 0x6); 1451188417Sthompsafail: 1452188417Sthompsa return (error); 1453193420Sweongyo#undef ZYD_GCT_INTR_REG 1454184610Salfred} 1455184610Salfred 1456188417Sthompsastatic int 1457193420Sweongyozyd_gct_mode(struct zyd_rf *rf) 1458184610Salfred{ 1459193420Sweongyo struct zyd_softc *sc = rf->rf_sc; 1460193420Sweongyo static const uint32_t mode[] = { 1461193420Sweongyo 0x25f98, 0x25f9a, 0x25f94, 0x27fd4 1462193420Sweongyo }; 1463193420Sweongyo int i, error; 1464188417Sthompsa 1465288088Sadrian for (i = 0; i < nitems(mode); i++) { 1466193420Sweongyo if ((error = zyd_rfwrite(sc, mode[i])) != 0) 1467193420Sweongyo break; 1468193420Sweongyo } 1469193420Sweongyo return (error); 1470188417Sthompsa} 1471188417Sthompsa 1472188417Sthompsastatic int 1473193420Sweongyozyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal) 1474188417Sthompsa{ 1475193420Sweongyo int error, idx = chan - 1; 1476188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1477193420Sweongyo static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL; 1478193420Sweongyo static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD; 1479193420Sweongyo static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV; 1480184610Salfred 1481193420Sweongyo error = zyd_rfwrite(sc, 1482193420Sweongyo (acal == 1) ? acal_synth[idx] : std_synth[idx]); 1483188417Sthompsa if (error != 0) 1484193420Sweongyo return (error); 1485193420Sweongyo return zyd_rfwrite(sc, div_synth[idx]); 1486184610Salfred} 1487184610Salfred 1488188417Sthompsastatic int 1489193420Sweongyozyd_gct_write(struct zyd_rf *rf, uint16_t value) 1490184610Salfred{ 1491188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1492184610Salfred 1493193420Sweongyo return zyd_rfwrite(sc, 0x300000 | 0x40000 | value); 1494184610Salfred} 1495184610Salfred 1496188417Sthompsastatic int 1497193420Sweongyozyd_gct_switch_radio(struct zyd_rf *rf, int on) 1498184610Salfred{ 1499193420Sweongyo int error; 1500193420Sweongyo struct zyd_softc *sc = rf->rf_sc; 1501188417Sthompsa 1502193420Sweongyo error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90); 1503193420Sweongyo if (error != 0) 1504193420Sweongyo return (error); 1505193420Sweongyo 1506193420Sweongyo zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04); 1507193420Sweongyo zyd_write16_m(sc, ZYD_CR251, 1508193420Sweongyo on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f); 1509193420Sweongyofail: 1510193420Sweongyo return (error); 1511188417Sthompsa} 1512188417Sthompsa 1513188417Sthompsastatic int 1514193420Sweongyozyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan) 1515188417Sthompsa{ 1516193420Sweongyo int error, i; 1517188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1518193420Sweongyo static const struct zyd_phy_pair cmd[] = { 1519193420Sweongyo { ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 }, 1520193420Sweongyo { ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 }, 1521193420Sweongyo }; 1522193420Sweongyo static const uint16_t vco[11][7] = ZYD_GCT_VCO; 1523184610Salfred 1524193420Sweongyo error = zyd_gct_set_channel_synth(rf, chan, 0); 1525188417Sthompsa if (error != 0) 1526188417Sthompsa goto fail; 1527193420Sweongyo error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 : 1528193420Sweongyo vco[rf->idx][((chan - 1) / 2)]); 1529188417Sthompsa if (error != 0) 1530188417Sthompsa goto fail; 1531193420Sweongyo error = zyd_gct_mode(rf); 1532193420Sweongyo if (error != 0) 1533193420Sweongyo return (error); 1534288088Sadrian for (i = 0; i < nitems(cmd); i++) 1535193420Sweongyo zyd_write16_m(sc, cmd[i].reg, cmd[i].val); 1536193420Sweongyo error = zyd_gct_txgain(rf, chan); 1537193420Sweongyo if (error != 0) 1538193420Sweongyo return (error); 1539193420Sweongyo zyd_write16_m(sc, ZYD_CR203, 0x6); 1540188417Sthompsafail: 1541188417Sthompsa return (error); 1542184610Salfred} 1543184610Salfred 1544193420Sweongyostatic int 1545193420Sweongyozyd_gct_txgain(struct zyd_rf *rf, uint8_t chan) 1546193420Sweongyo{ 1547193420Sweongyo struct zyd_softc *sc = rf->rf_sc; 1548193420Sweongyo static uint32_t txgain[] = ZYD_GCT_TXGAIN; 1549193420Sweongyo uint8_t idx = sc->sc_pwrint[chan - 1]; 1550193420Sweongyo 1551288088Sadrian if (idx >= nitems(txgain)) { 1552193420Sweongyo device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n", 1553193420Sweongyo chan, idx); 1554193420Sweongyo return 0; 1555193420Sweongyo } 1556193420Sweongyo 1557193420Sweongyo return zyd_rfwrite(sc, 0x700000 | txgain[idx]); 1558193420Sweongyo} 1559193420Sweongyo 1560184610Salfred/* 1561184610Salfred * Maxim2 RF methods. 1562184610Salfred */ 1563188417Sthompsastatic int 1564188417Sthompsazyd_maxim2_init(struct zyd_rf *rf) 1565184610Salfred{ 1566188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1567184610Salfred static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY; 1568184610Salfred static const uint32_t rfini[] = ZYD_MAXIM2_RF; 1569184610Salfred uint16_t tmp; 1570188417Sthompsa int i, error; 1571184610Salfred 1572184610Salfred /* init RF-dependent PHY registers */ 1573288088Sadrian for (i = 0; i < nitems(phyini); i++) 1574188417Sthompsa zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1575184610Salfred 1576188417Sthompsa zyd_read16_m(sc, ZYD_CR203, &tmp); 1577188417Sthompsa zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4)); 1578188417Sthompsa 1579184610Salfred /* init maxim2 radio */ 1580288088Sadrian for (i = 0; i < nitems(rfini); i++) { 1581188417Sthompsa if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1582188417Sthompsa return (error); 1583184610Salfred } 1584188417Sthompsa zyd_read16_m(sc, ZYD_CR203, &tmp); 1585188417Sthompsa zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4)); 1586188417Sthompsafail: 1587188417Sthompsa return (error); 1588184610Salfred} 1589184610Salfred 1590188417Sthompsastatic int 1591188417Sthompsazyd_maxim2_switch_radio(struct zyd_rf *rf, int on) 1592184610Salfred{ 1593188417Sthompsa 1594188417Sthompsa /* vendor driver does nothing for this RF chip */ 1595188417Sthompsa return (0); 1596188417Sthompsa} 1597188417Sthompsa 1598188417Sthompsastatic int 1599188417Sthompsazyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan) 1600188417Sthompsa{ 1601188417Sthompsa struct zyd_softc *sc = rf->rf_sc; 1602184610Salfred static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY; 1603184610Salfred static const uint32_t rfini[] = ZYD_MAXIM2_RF; 1604184610Salfred static const struct { 1605188417Sthompsa uint32_t r1, r2; 1606188417Sthompsa } rfprog[] = ZYD_MAXIM2_CHANTABLE; 1607184610Salfred uint16_t tmp; 1608188417Sthompsa int i, error; 1609184610Salfred 1610184610Salfred /* 1611184610Salfred * Do the same as we do when initializing it, except for the channel 1612184610Salfred * values coming from the two channel tables. 1613184610Salfred */ 1614184610Salfred 1615184610Salfred /* init RF-dependent PHY registers */ 1616288088Sadrian for (i = 0; i < nitems(phyini); i++) 1617188417Sthompsa zyd_write16_m(sc, phyini[i].reg, phyini[i].val); 1618184610Salfred 1619188417Sthompsa zyd_read16_m(sc, ZYD_CR203, &tmp); 1620188417Sthompsa zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4)); 1621188417Sthompsa 1622184610Salfred /* first two values taken from the chantables */ 1623188417Sthompsa error = zyd_rfwrite(sc, rfprog[chan - 1].r1); 1624188417Sthompsa if (error != 0) 1625188417Sthompsa goto fail; 1626188417Sthompsa error = zyd_rfwrite(sc, rfprog[chan - 1].r2); 1627188417Sthompsa if (error != 0) 1628188417Sthompsa goto fail; 1629184610Salfred 1630184610Salfred /* init maxim2 radio - skipping the two first values */ 1631288088Sadrian for (i = 2; i < nitems(rfini); i++) { 1632188417Sthompsa if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1633188417Sthompsa return (error); 1634184610Salfred } 1635188417Sthompsa zyd_read16_m(sc, ZYD_CR203, &tmp); 1636188417Sthompsa zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4)); 1637188417Sthompsafail: 1638188417Sthompsa return (error); 1639184610Salfred} 1640184610Salfred 1641188417Sthompsastatic int 1642188417Sthompsazyd_rf_attach(struct zyd_softc *sc, uint8_t type) 1643184610Salfred{ 1644188417Sthompsa struct zyd_rf *rf = &sc->sc_rf; 1645184610Salfred 1646188417Sthompsa rf->rf_sc = sc; 1647193420Sweongyo rf->update_pwr = 1; 1648188417Sthompsa 1649188417Sthompsa switch (type) { 1650184610Salfred case ZYD_RF_RFMD: 1651188417Sthompsa rf->init = zyd_rfmd_init; 1652188417Sthompsa rf->switch_radio = zyd_rfmd_switch_radio; 1653188417Sthompsa rf->set_channel = zyd_rfmd_set_channel; 1654188417Sthompsa rf->width = 24; /* 24-bit RF values */ 1655184610Salfred break; 1656184610Salfred case ZYD_RF_AL2230: 1657186730Salfred case ZYD_RF_AL2230S: 1658188417Sthompsa if (sc->sc_macrev == ZYD_ZD1211B) { 1659188417Sthompsa rf->init = zyd_al2230_init_b; 1660188417Sthompsa rf->set_channel = zyd_al2230_set_channel_b; 1661186730Salfred } else { 1662188417Sthompsa rf->init = zyd_al2230_init; 1663188417Sthompsa rf->set_channel = zyd_al2230_set_channel; 1664186730Salfred } 1665188417Sthompsa rf->switch_radio = zyd_al2230_switch_radio; 1666188417Sthompsa rf->bandedge6 = zyd_al2230_bandedge6; 1667188417Sthompsa rf->width = 24; /* 24-bit RF values */ 1668184610Salfred break; 1669184610Salfred case ZYD_RF_AL7230B: 1670188417Sthompsa rf->init = zyd_al7230B_init; 1671188417Sthompsa rf->switch_radio = zyd_al7230B_switch_radio; 1672188417Sthompsa rf->set_channel = zyd_al7230B_set_channel; 1673188417Sthompsa rf->width = 24; /* 24-bit RF values */ 1674184610Salfred break; 1675184610Salfred case ZYD_RF_AL2210: 1676188417Sthompsa rf->init = zyd_al2210_init; 1677188417Sthompsa rf->switch_radio = zyd_al2210_switch_radio; 1678188417Sthompsa rf->set_channel = zyd_al2210_set_channel; 1679188417Sthompsa rf->width = 24; /* 24-bit RF values */ 1680184610Salfred break; 1681193420Sweongyo case ZYD_RF_MAXIM_NEW: 1682184610Salfred case ZYD_RF_GCT: 1683188417Sthompsa rf->init = zyd_gct_init; 1684188417Sthompsa rf->switch_radio = zyd_gct_switch_radio; 1685188417Sthompsa rf->set_channel = zyd_gct_set_channel; 1686193420Sweongyo rf->width = 24; /* 24-bit RF values */ 1687193420Sweongyo rf->update_pwr = 0; 1688184610Salfred break; 1689184610Salfred case ZYD_RF_MAXIM_NEW2: 1690188417Sthompsa rf->init = zyd_maxim2_init; 1691188417Sthompsa rf->switch_radio = zyd_maxim2_switch_radio; 1692188417Sthompsa rf->set_channel = zyd_maxim2_set_channel; 1693188417Sthompsa rf->width = 18; /* 18-bit RF values */ 1694184610Salfred break; 1695184610Salfred default: 1696188417Sthompsa device_printf(sc->sc_dev, 1697188417Sthompsa "sorry, radio \"%s\" is not supported yet\n", 1698188417Sthompsa zyd_rf_name(type)); 1699188417Sthompsa return (EINVAL); 1700184610Salfred } 1701188417Sthompsa return (0); 1702188417Sthompsa} 1703184610Salfred 1704188417Sthompsastatic const char * 1705188417Sthompsazyd_rf_name(uint8_t type) 1706188417Sthompsa{ 1707188417Sthompsa static const char * const zyd_rfs[] = { 1708188417Sthompsa "unknown", "unknown", "UW2451", "UCHIP", "AL2230", 1709188417Sthompsa "AL7230B", "THETA", "AL2210", "MAXIM_NEW", "GCT", 1710188417Sthompsa "AL2230S", "RALINK", "INTERSIL", "RFMD", "MAXIM_NEW2", 1711188417Sthompsa "PHILIPS" 1712188417Sthompsa }; 1713184610Salfred 1714188417Sthompsa return zyd_rfs[(type > 15) ? 0 : type]; 1715184610Salfred} 1716184610Salfred 1717188417Sthompsastatic int 1718188417Sthompsazyd_hw_init(struct zyd_softc *sc) 1719184610Salfred{ 1720188417Sthompsa int error; 1721184610Salfred const struct zyd_phy_pair *phyp; 1722188417Sthompsa struct zyd_rf *rf = &sc->sc_rf; 1723188417Sthompsa uint16_t val; 1724184610Salfred 1725184610Salfred /* specify that the plug and play is finished */ 1726188417Sthompsa zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1); 1727188417Sthompsa zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase); 1728188417Sthompsa DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n", 1729188417Sthompsa sc->sc_fwbase); 1730184610Salfred 1731184610Salfred /* retrieve firmware revision number */ 1732188417Sthompsa zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev); 1733188417Sthompsa zyd_write32_m(sc, ZYD_CR_GPI_EN, 0); 1734188417Sthompsa zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f); 1735186730Salfred /* set mandatory rates - XXX assumes 802.11b/g */ 1736188417Sthompsa zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f); 1737186730Salfred 1738184610Salfred /* disable interrupts */ 1739188417Sthompsa zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0); 1740184610Salfred 1741188417Sthompsa if ((error = zyd_read_pod(sc)) != 0) { 1742188417Sthompsa device_printf(sc->sc_dev, "could not read EEPROM\n"); 1743188417Sthompsa goto fail; 1744184610Salfred } 1745188417Sthompsa 1746188417Sthompsa /* PHY init (resetting) */ 1747188417Sthompsa error = zyd_lock_phy(sc); 1748188417Sthompsa if (error != 0) 1749188417Sthompsa goto fail; 1750188417Sthompsa phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy; 1751188417Sthompsa for (; phyp->reg != 0; phyp++) 1752188417Sthompsa zyd_write16_m(sc, phyp->reg, phyp->val); 1753188417Sthompsa if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) { 1754188417Sthompsa zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val); 1755188417Sthompsa zyd_write32_m(sc, ZYD_CR157, val >> 8); 1756184610Salfred } 1757188417Sthompsa error = zyd_unlock_phy(sc); 1758188417Sthompsa if (error != 0) 1759188417Sthompsa goto fail; 1760184610Salfred 1761184610Salfred /* HMAC init */ 1762188417Sthompsa zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020); 1763188417Sthompsa zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808); 1764188417Sthompsa zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000); 1765188417Sthompsa zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000); 1766188417Sthompsa zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000); 1767188417Sthompsa zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000); 1768188417Sthompsa zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4); 1769188417Sthompsa zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f); 1770188417Sthompsa zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401); 1771188417Sthompsa zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000); 1772188417Sthompsa zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080); 1773188417Sthompsa zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000); 1774188417Sthompsa zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100); 1775188417Sthompsa zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070); 1776188417Sthompsa zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000); 1777188417Sthompsa zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203); 1778188417Sthompsa zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1); 1779188417Sthompsa zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114); 1780188417Sthompsa zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032); 1781188417Sthompsa zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3); 1782184610Salfred 1783188417Sthompsa if (sc->sc_macrev == ZYD_ZD1211) { 1784188417Sthompsa zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002); 1785188417Sthompsa zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640); 1786186730Salfred } else { 1787188417Sthompsa zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202); 1788188417Sthompsa zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f); 1789188417Sthompsa zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f); 1790188417Sthompsa zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f); 1791188417Sthompsa zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f); 1792188417Sthompsa zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028); 1793188417Sthompsa zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C); 1794188417Sthompsa zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824); 1795188417Sthompsa zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff); 1796186730Salfred } 1797186730Salfred 1798184610Salfred /* init beacon interval to 100ms */ 1799188417Sthompsa if ((error = zyd_set_beacon_interval(sc, 100)) != 0) 1800188417Sthompsa goto fail; 1801184610Salfred 1802188417Sthompsa if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) { 1803188417Sthompsa device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n", 1804188417Sthompsa sc->sc_rfrev); 1805188417Sthompsa goto fail; 1806188417Sthompsa } 1807188417Sthompsa 1808188417Sthompsa /* RF chip init */ 1809188417Sthompsa error = zyd_lock_phy(sc); 1810188417Sthompsa if (error != 0) 1811188417Sthompsa goto fail; 1812188417Sthompsa error = (*rf->init)(rf); 1813188417Sthompsa if (error != 0) { 1814188417Sthompsa device_printf(sc->sc_dev, 1815188417Sthompsa "radio initialization failed, error %d\n", error); 1816188417Sthompsa goto fail; 1817188417Sthompsa } 1818188417Sthompsa error = zyd_unlock_phy(sc); 1819188417Sthompsa if (error != 0) 1820188417Sthompsa goto fail; 1821188417Sthompsa 1822188417Sthompsa if ((error = zyd_read_eeprom(sc)) != 0) { 1823188417Sthompsa device_printf(sc->sc_dev, "could not read EEPROM\n"); 1824188417Sthompsa goto fail; 1825188417Sthompsa } 1826188417Sthompsa 1827188417Sthompsafail: return (error); 1828184610Salfred} 1829184610Salfred 1830188417Sthompsastatic int 1831188417Sthompsazyd_read_pod(struct zyd_softc *sc) 1832184610Salfred{ 1833188417Sthompsa int error; 1834184610Salfred uint32_t tmp; 1835184610Salfred 1836188417Sthompsa zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp); 1837188417Sthompsa sc->sc_rfrev = tmp & 0x0f; 1838188417Sthompsa sc->sc_ledtype = (tmp >> 4) & 0x01; 1839188417Sthompsa sc->sc_al2230s = (tmp >> 7) & 0x01; 1840188417Sthompsa sc->sc_cckgain = (tmp >> 8) & 0x01; 1841184610Salfred sc->sc_fix_cr157 = (tmp >> 13) & 0x01; 1842188417Sthompsa sc->sc_parev = (tmp >> 16) & 0x0f; 1843186730Salfred sc->sc_bandedge6 = (tmp >> 21) & 0x01; 1844188417Sthompsa sc->sc_newphy = (tmp >> 31) & 0x01; 1845188417Sthompsa sc->sc_txled = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1; 1846188417Sthompsafail: 1847188417Sthompsa return (error); 1848188417Sthompsa} 1849184610Salfred 1850188417Sthompsastatic int 1851188417Sthompsazyd_read_eeprom(struct zyd_softc *sc) 1852188417Sthompsa{ 1853188417Sthompsa uint16_t val; 1854188417Sthompsa int error, i; 1855184610Salfred 1856184610Salfred /* read Tx power calibration tables */ 1857184610Salfred for (i = 0; i < 7; i++) { 1858188417Sthompsa zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val); 1859188417Sthompsa sc->sc_pwrcal[i * 2] = val >> 8; 1860188417Sthompsa sc->sc_pwrcal[i * 2 + 1] = val & 0xff; 1861188417Sthompsa zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val); 1862188417Sthompsa sc->sc_pwrint[i * 2] = val >> 8; 1863188417Sthompsa sc->sc_pwrint[i * 2 + 1] = val & 0xff; 1864188417Sthompsa zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val); 1865188417Sthompsa sc->sc_ofdm36_cal[i * 2] = val >> 8; 1866188417Sthompsa sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff; 1867188417Sthompsa zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val); 1868188417Sthompsa sc->sc_ofdm48_cal[i * 2] = val >> 8; 1869188417Sthompsa sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff; 1870188417Sthompsa zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val); 1871188417Sthompsa sc->sc_ofdm54_cal[i * 2] = val >> 8; 1872188417Sthompsa sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff; 1873184610Salfred } 1874188417Sthompsafail: 1875188417Sthompsa return (error); 1876184610Salfred} 1877184610Salfred 1878188417Sthompsastatic int 1879188417Sthompsazyd_get_macaddr(struct zyd_softc *sc) 1880186730Salfred{ 1881192984Sthompsa struct usb_device_request req; 1882193045Sthompsa usb_error_t error; 1883186730Salfred 1884186730Salfred req.bmRequestType = UT_READ_VENDOR_DEVICE; 1885186730Salfred req.bRequest = ZYD_READFWDATAREQ; 1886186730Salfred USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1); 1887186730Salfred USETW(req.wIndex, 0); 1888186730Salfred USETW(req.wLength, IEEE80211_ADDR_LEN); 1889186730Salfred 1890287197Sglebius error = zyd_do_request(sc, &req, sc->sc_ic.ic_macaddr); 1891188417Sthompsa if (error != 0) { 1892188417Sthompsa device_printf(sc->sc_dev, "could not read EEPROM: %s\n", 1893194228Sthompsa usbd_errstr(error)); 1894188417Sthompsa } 1895188417Sthompsa 1896188417Sthompsa return (error); 1897186730Salfred} 1898186730Salfred 1899188417Sthompsastatic int 1900188417Sthompsazyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr) 1901184610Salfred{ 1902188417Sthompsa int error; 1903184610Salfred uint32_t tmp; 1904184610Salfred 1905188417Sthompsa tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]; 1906188417Sthompsa zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp); 1907188417Sthompsa tmp = addr[5] << 8 | addr[4]; 1908188417Sthompsa zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp); 1909188417Sthompsafail: 1910188417Sthompsa return (error); 1911188417Sthompsa} 1912184610Salfred 1913188417Sthompsastatic int 1914188417Sthompsazyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr) 1915188417Sthompsa{ 1916188417Sthompsa int error; 1917188417Sthompsa uint32_t tmp; 1918188417Sthompsa 1919188417Sthompsa tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]; 1920188417Sthompsa zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp); 1921188417Sthompsa tmp = addr[5] << 8 | addr[4]; 1922188417Sthompsa zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp); 1923188417Sthompsafail: 1924188417Sthompsa return (error); 1925184610Salfred} 1926184610Salfred 1927188417Sthompsastatic int 1928188417Sthompsazyd_switch_radio(struct zyd_softc *sc, int on) 1929184610Salfred{ 1930188417Sthompsa struct zyd_rf *rf = &sc->sc_rf; 1931188417Sthompsa int error; 1932188417Sthompsa 1933188417Sthompsa error = zyd_lock_phy(sc); 1934188417Sthompsa if (error != 0) 1935188417Sthompsa goto fail; 1936188417Sthompsa error = (*rf->switch_radio)(rf, on); 1937188417Sthompsa if (error != 0) 1938188417Sthompsa goto fail; 1939188417Sthompsa error = zyd_unlock_phy(sc); 1940188417Sthompsafail: 1941188417Sthompsa return (error); 1942184610Salfred} 1943184610Salfred 1944188417Sthompsastatic int 1945188417Sthompsazyd_set_led(struct zyd_softc *sc, int which, int on) 1946184610Salfred{ 1947188417Sthompsa int error; 1948184610Salfred uint32_t tmp; 1949184610Salfred 1950188417Sthompsa zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp); 1951188417Sthompsa tmp &= ~which; 1952188417Sthompsa if (on) 1953188417Sthompsa tmp |= which; 1954188417Sthompsa zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp); 1955188417Sthompsafail: 1956188417Sthompsa return (error); 1957188417Sthompsa} 1958184610Salfred 1959188417Sthompsastatic void 1960188417Sthompsazyd_set_multi(struct zyd_softc *sc) 1961184610Salfred{ 1962287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 1963287197Sglebius uint32_t low, high; 1964286437Sadrian int error; 1965184610Salfred 1966287197Sglebius if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) 1967188417Sthompsa return; 1968184610Salfred 1969188417Sthompsa low = 0x00000000; 1970188417Sthompsa high = 0x80000000; 1971184610Salfred 1972287197Sglebius if (ic->ic_opmode == IEEE80211_M_MONITOR || ic->ic_allmulti > 0 || 1973287197Sglebius ic->ic_promisc > 0) { 1974188417Sthompsa low = 0xffffffff; 1975188417Sthompsa high = 0xffffffff; 1976184610Salfred } else { 1977287197Sglebius struct ieee80211vap *vap; 1978287197Sglebius struct ifnet *ifp; 1979287197Sglebius struct ifmultiaddr *ifma; 1980287197Sglebius uint8_t v; 1981287197Sglebius 1982287197Sglebius TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 1983287197Sglebius ifp = vap->iv_ifp; 1984287197Sglebius if_maddr_rlock(ifp); 1985287197Sglebius TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1986287197Sglebius if (ifma->ifma_addr->sa_family != AF_LINK) 1987287197Sglebius continue; 1988287197Sglebius v = ((uint8_t *)LLADDR((struct sockaddr_dl *) 1989287197Sglebius ifma->ifma_addr))[5] >> 2; 1990287197Sglebius if (v < 32) 1991287197Sglebius low |= 1 << v; 1992287197Sglebius else 1993287197Sglebius high |= 1 << (v - 32); 1994287197Sglebius } 1995287197Sglebius if_maddr_runlock(ifp); 1996188417Sthompsa } 1997184610Salfred } 1998184610Salfred 1999188417Sthompsa /* reprogram multicast global hash table */ 2000188417Sthompsa zyd_write32_m(sc, ZYD_MAC_GHTBL, low); 2001188417Sthompsa zyd_write32_m(sc, ZYD_MAC_GHTBH, high); 2002188417Sthompsafail: 2003188417Sthompsa if (error != 0) 2004188417Sthompsa device_printf(sc->sc_dev, 2005188417Sthompsa "could not set multicast hash table\n"); 2006188417Sthompsa} 2007184610Salfred 2008188417Sthompsastatic void 2009283540Sglebiuszyd_update_mcast(struct ieee80211com *ic) 2010188417Sthompsa{ 2011283540Sglebius struct zyd_softc *sc = ic->ic_softc; 2012184610Salfred 2013188417Sthompsa ZYD_LOCK(sc); 2014191746Sthompsa zyd_set_multi(sc); 2015188417Sthompsa ZYD_UNLOCK(sc); 2016188417Sthompsa} 2017184610Salfred 2018188417Sthompsastatic int 2019188417Sthompsazyd_set_rxfilter(struct zyd_softc *sc) 2020188417Sthompsa{ 2021287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 2022188417Sthompsa uint32_t rxfilter; 2023184610Salfred 2024188417Sthompsa switch (ic->ic_opmode) { 2025188417Sthompsa case IEEE80211_M_STA: 2026188417Sthompsa rxfilter = ZYD_FILTER_BSS; 2027188417Sthompsa break; 2028188417Sthompsa case IEEE80211_M_IBSS: 2029188417Sthompsa case IEEE80211_M_HOSTAP: 2030188417Sthompsa rxfilter = ZYD_FILTER_HOSTAP; 2031188417Sthompsa break; 2032188417Sthompsa case IEEE80211_M_MONITOR: 2033188417Sthompsa rxfilter = ZYD_FILTER_MONITOR; 2034188417Sthompsa break; 2035188417Sthompsa default: 2036188417Sthompsa /* should not get there */ 2037188417Sthompsa return (EINVAL); 2038184610Salfred } 2039188417Sthompsa return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter); 2040188417Sthompsa} 2041184610Salfred 2042188417Sthompsastatic void 2043188417Sthompsazyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c) 2044188417Sthompsa{ 2045188417Sthompsa int error; 2046287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 2047188417Sthompsa struct zyd_rf *rf = &sc->sc_rf; 2048188417Sthompsa uint32_t tmp; 2049188601Sthompsa int chan; 2050184610Salfred 2051188417Sthompsa chan = ieee80211_chan2ieee(ic, c); 2052188417Sthompsa if (chan == 0 || chan == IEEE80211_CHAN_ANY) { 2053188417Sthompsa /* XXX should NEVER happen */ 2054188417Sthompsa device_printf(sc->sc_dev, 2055188417Sthompsa "%s: invalid channel %x\n", __func__, chan); 2056188417Sthompsa return; 2057188417Sthompsa } 2058184610Salfred 2059188417Sthompsa error = zyd_lock_phy(sc); 2060188417Sthompsa if (error != 0) 2061188417Sthompsa goto fail; 2062184610Salfred 2063188417Sthompsa error = (*rf->set_channel)(rf, chan); 2064188417Sthompsa if (error != 0) 2065188417Sthompsa goto fail; 2066184610Salfred 2067193420Sweongyo if (rf->update_pwr) { 2068193420Sweongyo /* update Tx power */ 2069193420Sweongyo zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]); 2070184610Salfred 2071193420Sweongyo if (sc->sc_macrev == ZYD_ZD1211B) { 2072193420Sweongyo zyd_write16_m(sc, ZYD_CR67, 2073193420Sweongyo sc->sc_ofdm36_cal[chan - 1]); 2074193420Sweongyo zyd_write16_m(sc, ZYD_CR66, 2075193420Sweongyo sc->sc_ofdm48_cal[chan - 1]); 2076193420Sweongyo zyd_write16_m(sc, ZYD_CR65, 2077193420Sweongyo sc->sc_ofdm54_cal[chan - 1]); 2078193420Sweongyo zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]); 2079193420Sweongyo zyd_write16_m(sc, ZYD_CR69, 0x28); 2080193420Sweongyo zyd_write16_m(sc, ZYD_CR69, 0x2a); 2081193420Sweongyo } 2082188417Sthompsa } 2083188417Sthompsa if (sc->sc_cckgain) { 2084188417Sthompsa /* set CCK baseband gain from EEPROM */ 2085188417Sthompsa if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0) 2086188417Sthompsa zyd_write16_m(sc, ZYD_CR47, tmp & 0xff); 2087188417Sthompsa } 2088188417Sthompsa if (sc->sc_bandedge6 && rf->bandedge6 != NULL) { 2089188417Sthompsa error = (*rf->bandedge6)(rf, c); 2090188417Sthompsa if (error != 0) 2091188417Sthompsa goto fail; 2092188417Sthompsa } 2093188417Sthompsa zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0); 2094184610Salfred 2095188417Sthompsa error = zyd_unlock_phy(sc); 2096188417Sthompsa if (error != 0) 2097188417Sthompsa goto fail; 2098184610Salfred 2099188417Sthompsa sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq = 2100188417Sthompsa htole16(c->ic_freq); 2101188417Sthompsa sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags = 2102188417Sthompsa htole16(c->ic_flags); 2103188417Sthompsafail: 2104184610Salfred return; 2105184610Salfred} 2106184610Salfred 2107184610Salfredstatic int 2108188417Sthompsazyd_set_beacon_interval(struct zyd_softc *sc, int bintval) 2109184610Salfred{ 2110188417Sthompsa int error; 2111188417Sthompsa uint32_t val; 2112184610Salfred 2113188417Sthompsa zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val); 2114188417Sthompsa sc->sc_atim_wnd = val; 2115188417Sthompsa zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val); 2116188417Sthompsa sc->sc_pre_tbtt = val; 2117188417Sthompsa sc->sc_bcn_int = bintval; 2118184610Salfred 2119188417Sthompsa if (sc->sc_bcn_int <= 5) 2120188417Sthompsa sc->sc_bcn_int = 5; 2121188417Sthompsa if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int) 2122188417Sthompsa sc->sc_pre_tbtt = sc->sc_bcn_int - 1; 2123188417Sthompsa if (sc->sc_atim_wnd >= sc->sc_pre_tbtt) 2124188417Sthompsa sc->sc_atim_wnd = sc->sc_pre_tbtt - 1; 2125184610Salfred 2126188417Sthompsa zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd); 2127188417Sthompsa zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt); 2128188417Sthompsa zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int); 2129188417Sthompsafail: 2130188417Sthompsa return (error); 2131188417Sthompsa} 2132184610Salfred 2133188417Sthompsastatic void 2134192984Sthompsazyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len) 2135188417Sthompsa{ 2136194677Sthompsa struct zyd_softc *sc = usbd_xfer_softc(xfer); 2137287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 2138188417Sthompsa struct zyd_plcphdr plcp; 2139188417Sthompsa struct zyd_rx_stat stat; 2140194677Sthompsa struct usb_page_cache *pc; 2141188417Sthompsa struct mbuf *m; 2142188417Sthompsa int rlen, rssi; 2143184610Salfred 2144188417Sthompsa if (len < ZYD_MIN_FRAGSZ) { 2145188417Sthompsa DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n", 2146188417Sthompsa device_get_nameunit(sc->sc_dev), len); 2147287197Sglebius counter_u64_add(ic->ic_ierrors, 1); 2148188417Sthompsa return; 2149188417Sthompsa } 2150194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 2151194677Sthompsa usbd_copy_out(pc, offset, &plcp, sizeof(plcp)); 2152194677Sthompsa usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat)); 2153184610Salfred 2154188417Sthompsa if (stat.flags & ZYD_RX_ERROR) { 2155188417Sthompsa DPRINTF(sc, ZYD_DEBUG_RECV, 2156188417Sthompsa "%s: RX status indicated error (%x)\n", 2157188417Sthompsa device_get_nameunit(sc->sc_dev), stat.flags); 2158287197Sglebius counter_u64_add(ic->ic_ierrors, 1); 2159188417Sthompsa return; 2160188417Sthompsa } 2161184610Salfred 2162188417Sthompsa /* compute actual frame length */ 2163188417Sthompsa rlen = len - sizeof(struct zyd_plcphdr) - 2164188417Sthompsa sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN; 2165184610Salfred 2166188417Sthompsa /* allocate a mbuf to store the frame */ 2167233774Shselasky if (rlen > (int)MCLBYTES) { 2168188417Sthompsa DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n", 2169188417Sthompsa device_get_nameunit(sc->sc_dev), rlen); 2170287197Sglebius counter_u64_add(ic->ic_ierrors, 1); 2171188417Sthompsa return; 2172233774Shselasky } else if (rlen > (int)MHLEN) 2173243857Sglebius m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 2174188417Sthompsa else 2175243857Sglebius m = m_gethdr(M_NOWAIT, MT_DATA); 2176188417Sthompsa if (m == NULL) { 2177188417Sthompsa DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n", 2178188417Sthompsa device_get_nameunit(sc->sc_dev)); 2179287197Sglebius counter_u64_add(ic->ic_ierrors, 1); 2180188417Sthompsa return; 2181184610Salfred } 2182188417Sthompsa m->m_pkthdr.len = m->m_len = rlen; 2183194677Sthompsa usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen); 2184184610Salfred 2185192468Ssam if (ieee80211_radiotap_active(ic)) { 2186188417Sthompsa struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap; 2187184610Salfred 2188188417Sthompsa tap->wr_flags = 0; 2189188417Sthompsa if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32)) 2190188417Sthompsa tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS; 2191188417Sthompsa /* XXX toss, no way to express errors */ 2192188417Sthompsa if (stat.flags & ZYD_RX_DECRYPTERR) 2193188417Sthompsa tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS; 2194188417Sthompsa tap->wr_rate = ieee80211_plcp2rate(plcp.signal, 2195188417Sthompsa (stat.flags & ZYD_RX_OFDM) ? 2196188417Sthompsa IEEE80211_T_OFDM : IEEE80211_T_CCK); 2197188417Sthompsa tap->wr_antsignal = stat.rssi + -95; 2198188417Sthompsa tap->wr_antnoise = -95; /* XXX */ 2199188417Sthompsa } 2200188417Sthompsa rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi; 2201184610Salfred 2202188417Sthompsa sc->sc_rx_data[sc->sc_rx_count].rssi = rssi; 2203188417Sthompsa sc->sc_rx_data[sc->sc_rx_count].m = m; 2204188417Sthompsa sc->sc_rx_count++; 2205184610Salfred} 2206184610Salfred 2207184610Salfredstatic void 2208194677Sthompsazyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 2209184610Salfred{ 2210194677Sthompsa struct zyd_softc *sc = usbd_xfer_softc(xfer); 2211287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 2212188417Sthompsa struct ieee80211_node *ni; 2213188417Sthompsa struct zyd_rx_desc desc; 2214188417Sthompsa struct mbuf *m; 2215194677Sthompsa struct usb_page_cache *pc; 2216188417Sthompsa uint32_t offset; 2217188417Sthompsa uint8_t rssi; 2218188417Sthompsa int8_t nf; 2219188417Sthompsa int i; 2220194677Sthompsa int actlen; 2221184610Salfred 2222194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 2223194677Sthompsa 2224188417Sthompsa sc->sc_rx_count = 0; 2225188417Sthompsa switch (USB_GET_STATE(xfer)) { 2226188417Sthompsa case USB_ST_TRANSFERRED: 2227194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 2228194677Sthompsa usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc)); 2229184610Salfred 2230188417Sthompsa offset = 0; 2231188417Sthompsa if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) { 2232188417Sthompsa DPRINTF(sc, ZYD_DEBUG_RECV, 2233188417Sthompsa "%s: received multi-frame transfer\n", __func__); 2234184610Salfred 2235188417Sthompsa for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) { 2236188417Sthompsa uint16_t len16 = UGETW(desc.len[i]); 2237184610Salfred 2238194677Sthompsa if (len16 == 0 || len16 > actlen) 2239188417Sthompsa break; 2240184610Salfred 2241188417Sthompsa zyd_rx_data(xfer, offset, len16); 2242184610Salfred 2243188417Sthompsa /* next frame is aligned on a 32-bit boundary */ 2244188417Sthompsa len16 = (len16 + 3) & ~3; 2245188417Sthompsa offset += len16; 2246194677Sthompsa if (len16 > actlen) 2247188417Sthompsa break; 2248194677Sthompsa actlen -= len16; 2249188417Sthompsa } 2250188417Sthompsa } else { 2251188417Sthompsa DPRINTF(sc, ZYD_DEBUG_RECV, 2252188417Sthompsa "%s: received single-frame transfer\n", __func__); 2253184610Salfred 2254194677Sthompsa zyd_rx_data(xfer, 0, actlen); 2255188417Sthompsa } 2256188417Sthompsa /* FALLTHROUGH */ 2257188417Sthompsa case USB_ST_SETUP: 2258188417Sthompsatr_setup: 2259194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 2260194228Sthompsa usbd_transfer_submit(xfer); 2261184610Salfred 2262188417Sthompsa /* 2263188417Sthompsa * At the end of a USB callback it is always safe to unlock 2264188417Sthompsa * the private mutex of a device! That is why we do the 2265188417Sthompsa * "ieee80211_input" here, and not some lines up! 2266188417Sthompsa */ 2267188417Sthompsa ZYD_UNLOCK(sc); 2268188417Sthompsa for (i = 0; i < sc->sc_rx_count; i++) { 2269188417Sthompsa rssi = sc->sc_rx_data[i].rssi; 2270188417Sthompsa m = sc->sc_rx_data[i].m; 2271188417Sthompsa sc->sc_rx_data[i].m = NULL; 2272184610Salfred 2273188417Sthompsa nf = -95; /* XXX */ 2274184610Salfred 2275188417Sthompsa ni = ieee80211_find_rxnode(ic, 2276188417Sthompsa mtod(m, struct ieee80211_frame_min *)); 2277188417Sthompsa if (ni != NULL) { 2278192468Ssam (void)ieee80211_input(ni, m, rssi, nf); 2279188417Sthompsa ieee80211_free_node(ni); 2280188417Sthompsa } else 2281192468Ssam (void)ieee80211_input_all(ic, m, rssi, nf); 2282188417Sthompsa } 2283188417Sthompsa ZYD_LOCK(sc); 2284287197Sglebius zyd_start(sc); 2285188417Sthompsa break; 2286184610Salfred 2287188417Sthompsa default: /* Error */ 2288194677Sthompsa DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error)); 2289184610Salfred 2290194677Sthompsa if (error != USB_ERR_CANCELLED) { 2291188417Sthompsa /* try to clear stall first */ 2292194677Sthompsa usbd_xfer_set_stall(xfer); 2293188417Sthompsa goto tr_setup; 2294184610Salfred } 2295188417Sthompsa break; 2296184610Salfred } 2297184610Salfred} 2298184610Salfred 2299184610Salfredstatic uint8_t 2300193803Sweongyozyd_plcp_signal(struct zyd_softc *sc, int rate) 2301184610Salfred{ 2302184610Salfred switch (rate) { 2303188417Sthompsa /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 2304184610Salfred case 12: 2305184610Salfred return (0xb); 2306184610Salfred case 18: 2307184610Salfred return (0xf); 2308184610Salfred case 24: 2309184610Salfred return (0xa); 2310184610Salfred case 36: 2311184610Salfred return (0xe); 2312184610Salfred case 48: 2313184610Salfred return (0x9); 2314184610Salfred case 72: 2315184610Salfred return (0xd); 2316184610Salfred case 96: 2317184610Salfred return (0x8); 2318184610Salfred case 108: 2319184610Salfred return (0xc); 2320188417Sthompsa /* CCK rates (NB: not IEEE std, device-specific) */ 2321188417Sthompsa case 2: 2322188417Sthompsa return (0x0); 2323188417Sthompsa case 4: 2324188417Sthompsa return (0x1); 2325188417Sthompsa case 11: 2326188417Sthompsa return (0x2); 2327188417Sthompsa case 22: 2328188417Sthompsa return (0x3); 2329184610Salfred } 2330184610Salfred 2331193803Sweongyo device_printf(sc->sc_dev, "unsupported rate %d\n", rate); 2332193803Sweongyo return (0x0); 2333184610Salfred} 2334184610Salfred 2335184610Salfredstatic void 2336194677Sthompsazyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 2337184610Salfred{ 2338194677Sthompsa struct zyd_softc *sc = usbd_xfer_softc(xfer); 2339192468Ssam struct ieee80211vap *vap; 2340188417Sthompsa struct zyd_tx_data *data; 2341188417Sthompsa struct mbuf *m; 2342194677Sthompsa struct usb_page_cache *pc; 2343194677Sthompsa int actlen; 2344184610Salfred 2345194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 2346194677Sthompsa 2347188417Sthompsa switch (USB_GET_STATE(xfer)) { 2348188417Sthompsa case USB_ST_TRANSFERRED: 2349188417Sthompsa DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n", 2350194677Sthompsa actlen); 2351184610Salfred 2352188417Sthompsa /* free resources */ 2353194677Sthompsa data = usbd_xfer_get_priv(xfer); 2354188417Sthompsa zyd_tx_free(data, 0); 2355194677Sthompsa usbd_xfer_set_priv(xfer, NULL); 2356184610Salfred 2357188417Sthompsa /* FALLTHROUGH */ 2358188417Sthompsa case USB_ST_SETUP: 2359188417Sthompsatr_setup: 2360188417Sthompsa data = STAILQ_FIRST(&sc->tx_q); 2361188417Sthompsa if (data) { 2362188417Sthompsa STAILQ_REMOVE_HEAD(&sc->tx_q, next); 2363188417Sthompsa m = data->m; 2364184610Salfred 2365233774Shselasky if (m->m_pkthdr.len > (int)ZYD_MAX_TXBUFSZ) { 2366188417Sthompsa DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n", 2367188417Sthompsa m->m_pkthdr.len); 2368188417Sthompsa m->m_pkthdr.len = ZYD_MAX_TXBUFSZ; 2369188417Sthompsa } 2370194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 2371194677Sthompsa usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE); 2372194677Sthompsa usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0, 2373188417Sthompsa m->m_pkthdr.len); 2374184610Salfred 2375192468Ssam vap = data->ni->ni_vap; 2376192468Ssam if (ieee80211_radiotap_active_vap(vap)) { 2377188417Sthompsa struct zyd_tx_radiotap_header *tap = &sc->sc_txtap; 2378184610Salfred 2379188417Sthompsa tap->wt_flags = 0; 2380188417Sthompsa tap->wt_rate = data->rate; 2381184610Salfred 2382192468Ssam ieee80211_radiotap_tx(vap, m); 2383188417Sthompsa } 2384184610Salfred 2385194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len); 2386194677Sthompsa usbd_xfer_set_priv(xfer, data); 2387194228Sthompsa usbd_transfer_submit(xfer); 2388188417Sthompsa } 2389287197Sglebius zyd_start(sc); 2390188417Sthompsa break; 2391184610Salfred 2392188417Sthompsa default: /* Error */ 2393188417Sthompsa DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n", 2394194677Sthompsa usbd_errstr(error)); 2395184610Salfred 2396287197Sglebius counter_u64_add(sc->sc_ic.ic_oerrors, 1); 2397194677Sthompsa data = usbd_xfer_get_priv(xfer); 2398194677Sthompsa usbd_xfer_set_priv(xfer, NULL); 2399188419Sthompsa if (data != NULL) 2400194677Sthompsa zyd_tx_free(data, error); 2401188419Sthompsa 2402203141Sthompsa if (error != USB_ERR_CANCELLED) { 2403203141Sthompsa if (error == USB_ERR_TIMEOUT) 2404203141Sthompsa device_printf(sc->sc_dev, "device timeout\n"); 2405203141Sthompsa 2406203141Sthompsa /* 2407203141Sthompsa * Try to clear stall first, also if other 2408203141Sthompsa * errors occur, hence clearing stall 2409203141Sthompsa * introduces a 50 ms delay: 2410203141Sthompsa */ 2411194677Sthompsa usbd_xfer_set_stall(xfer); 2412188417Sthompsa goto tr_setup; 2413188417Sthompsa } 2414184610Salfred break; 2415184610Salfred } 2416184610Salfred} 2417184610Salfred 2418188417Sthompsastatic int 2419193803Sweongyozyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 2420184610Salfred{ 2421188417Sthompsa struct ieee80211vap *vap = ni->ni_vap; 2422188417Sthompsa struct ieee80211com *ic = ni->ni_ic; 2423188417Sthompsa struct zyd_tx_desc *desc; 2424188417Sthompsa struct zyd_tx_data *data; 2425188417Sthompsa struct ieee80211_frame *wh; 2426188417Sthompsa const struct ieee80211_txparam *tp; 2427188417Sthompsa struct ieee80211_key *k; 2428188417Sthompsa int rate, totlen; 2429269127Shselasky static const uint8_t ratediv[] = ZYD_TX_RATEDIV; 2430193803Sweongyo uint8_t phy; 2431188417Sthompsa uint16_t pktlen; 2432193803Sweongyo uint32_t bits; 2433184610Salfred 2434188417Sthompsa wh = mtod(m0, struct ieee80211_frame *); 2435188417Sthompsa data = STAILQ_FIRST(&sc->tx_free); 2436188417Sthompsa STAILQ_REMOVE_HEAD(&sc->tx_free, next); 2437188417Sthompsa sc->tx_nfree--; 2438184610Salfred 2439193803Sweongyo if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT || 2440193803Sweongyo (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) { 2441193803Sweongyo tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 2442193803Sweongyo rate = tp->mgmtrate; 2443188417Sthompsa } else { 2444193803Sweongyo tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 2445193803Sweongyo /* for data frames */ 2446193803Sweongyo if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 2447193803Sweongyo rate = tp->mcastrate; 2448193803Sweongyo else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 2449193803Sweongyo rate = tp->ucastrate; 2450193803Sweongyo else { 2451206358Srpaulo (void) ieee80211_ratectl_rate(ni, NULL, 0); 2452193803Sweongyo rate = ni->ni_txrate; 2453193803Sweongyo } 2454184610Salfred } 2455184610Salfred 2456260444Skevlo if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 2457188417Sthompsa k = ieee80211_crypto_encap(ni, m0); 2458188417Sthompsa if (k == NULL) { 2459188417Sthompsa return (ENOBUFS); 2460188417Sthompsa } 2461188417Sthompsa /* packet header may have moved, reset our local pointer */ 2462188417Sthompsa wh = mtod(m0, struct ieee80211_frame *); 2463184610Salfred } 2464184610Salfred 2465188417Sthompsa data->ni = ni; 2466188417Sthompsa data->m = m0; 2467193803Sweongyo data->rate = rate; 2468184610Salfred 2469193803Sweongyo /* fill Tx descriptor */ 2470193803Sweongyo desc = &data->desc; 2471193803Sweongyo phy = zyd_plcp_signal(sc, rate); 2472193803Sweongyo desc->phy = phy; 2473193803Sweongyo if (ZYD_RATE_IS_OFDM(rate)) { 2474193803Sweongyo desc->phy |= ZYD_TX_PHY_OFDM; 2475193803Sweongyo if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) 2476193803Sweongyo desc->phy |= ZYD_TX_PHY_5GHZ; 2477193803Sweongyo } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 2478193803Sweongyo desc->phy |= ZYD_TX_PHY_SHPREAMBLE; 2479193803Sweongyo 2480188417Sthompsa totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN; 2481188417Sthompsa desc->len = htole16(totlen); 2482184610Salfred 2483193803Sweongyo desc->flags = ZYD_TX_FLAG_BACKOFF; 2484188417Sthompsa if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 2485188417Sthompsa /* multicast frames are not sent at OFDM rates in 802.11b/g */ 2486188417Sthompsa if (totlen > vap->iv_rtsthreshold) { 2487188417Sthompsa desc->flags |= ZYD_TX_FLAG_RTS; 2488188417Sthompsa } else if (ZYD_RATE_IS_OFDM(rate) && 2489188417Sthompsa (ic->ic_flags & IEEE80211_F_USEPROT)) { 2490188417Sthompsa if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 2491188417Sthompsa desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF; 2492188417Sthompsa else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 2493188417Sthompsa desc->flags |= ZYD_TX_FLAG_RTS; 2494188417Sthompsa } 2495193803Sweongyo } else 2496193803Sweongyo desc->flags |= ZYD_TX_FLAG_MULTICAST; 2497188417Sthompsa if ((wh->i_fc[0] & 2498188417Sthompsa (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 2499188417Sthompsa (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL)) 2500188417Sthompsa desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL); 2501188417Sthompsa 2502184610Salfred /* actual transmit length (XXX why +10?) */ 2503193803Sweongyo pktlen = ZYD_TX_DESC_SIZE + 10; 2504188417Sthompsa if (sc->sc_macrev == ZYD_ZD1211) 2505184610Salfred pktlen += totlen; 2506188417Sthompsa desc->pktlen = htole16(pktlen); 2507184610Salfred 2508193803Sweongyo bits = (rate == 11) ? (totlen * 16) + 10 : 2509193803Sweongyo ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8)); 2510196809Sweongyo desc->plcp_length = htole16(bits / ratediv[phy]); 2511188417Sthompsa desc->plcp_service = 0; 2512193803Sweongyo if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3) 2513193803Sweongyo desc->plcp_service |= ZYD_PLCP_LENGEXT; 2514193803Sweongyo desc->nextlen = 0; 2515193803Sweongyo 2516193803Sweongyo if (ieee80211_radiotap_active_vap(vap)) { 2517193803Sweongyo struct zyd_tx_radiotap_header *tap = &sc->sc_txtap; 2518193803Sweongyo 2519193803Sweongyo tap->wt_flags = 0; 2520193803Sweongyo tap->wt_rate = rate; 2521193803Sweongyo 2522193803Sweongyo ieee80211_radiotap_tx(vap, m0); 2523184610Salfred } 2524184610Salfred 2525188417Sthompsa DPRINTF(sc, ZYD_DEBUG_XMIT, 2526188417Sthompsa "%s: sending data frame len=%zu rate=%u\n", 2527188417Sthompsa device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len, 2528188417Sthompsa rate); 2529184610Salfred 2530188417Sthompsa STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 2531194228Sthompsa usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]); 2532184610Salfred 2533188417Sthompsa return (0); 2534184610Salfred} 2535184610Salfred 2536287197Sglebiusstatic int 2537287197Sglebiuszyd_transmit(struct ieee80211com *ic, struct mbuf *m) 2538287197Sglebius{ 2539287197Sglebius struct zyd_softc *sc = ic->ic_softc; 2540287197Sglebius int error; 2541287197Sglebius 2542287197Sglebius ZYD_LOCK(sc); 2543287197Sglebius if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) { 2544287197Sglebius ZYD_UNLOCK(sc); 2545287197Sglebius return (ENXIO); 2546287197Sglebius } 2547287197Sglebius error = mbufq_enqueue(&sc->sc_snd, m); 2548287197Sglebius if (error) { 2549287197Sglebius ZYD_UNLOCK(sc); 2550287197Sglebius return (error); 2551287197Sglebius } 2552287197Sglebius zyd_start(sc); 2553287197Sglebius ZYD_UNLOCK(sc); 2554287197Sglebius 2555287197Sglebius return (0); 2556287197Sglebius} 2557287197Sglebius 2558184610Salfredstatic void 2559287197Sglebiuszyd_start(struct zyd_softc *sc) 2560184610Salfred{ 2561188417Sthompsa struct ieee80211_node *ni; 2562184610Salfred struct mbuf *m; 2563184610Salfred 2564287197Sglebius ZYD_LOCK_ASSERT(sc, MA_OWNED); 2565287197Sglebius 2566287197Sglebius while (sc->tx_nfree > 0 && (m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 2567188417Sthompsa ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 2568193803Sweongyo if (zyd_tx_start(sc, m, ni) != 0) { 2569288648Sadrian m_freem(m); 2570287197Sglebius if_inc_counter(ni->ni_vap->iv_ifp, 2571287197Sglebius IFCOUNTER_OERRORS, 1); 2572314222Savos ieee80211_free_node(ni); 2573188417Sthompsa break; 2574184610Salfred } 2575184610Salfred } 2576184610Salfred} 2577184610Salfred 2578188417Sthompsastatic int 2579188417Sthompsazyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2580188417Sthompsa const struct ieee80211_bpf_params *params) 2581184610Salfred{ 2582188417Sthompsa struct ieee80211com *ic = ni->ni_ic; 2583286950Sadrian struct zyd_softc *sc = ic->ic_softc; 2584184610Salfred 2585188417Sthompsa ZYD_LOCK(sc); 2586188417Sthompsa /* prevent management frames from being sent if we're not ready */ 2587287197Sglebius if (!(sc->sc_flags & ZYD_FLAG_RUNNING)) { 2588188417Sthompsa ZYD_UNLOCK(sc); 2589188417Sthompsa m_freem(m); 2590188417Sthompsa return (ENETDOWN); 2591188417Sthompsa } 2592188417Sthompsa if (sc->tx_nfree == 0) { 2593188417Sthompsa ZYD_UNLOCK(sc); 2594188417Sthompsa m_freem(m); 2595188417Sthompsa return (ENOBUFS); /* XXX */ 2596188417Sthompsa } 2597188417Sthompsa 2598188417Sthompsa /* 2599188417Sthompsa * Legacy path; interpret frame contents to decide 2600188417Sthompsa * precisely how to send the frame. 2601188417Sthompsa * XXX raw path 2602188417Sthompsa */ 2603193803Sweongyo if (zyd_tx_start(sc, m, ni) != 0) { 2604188417Sthompsa ZYD_UNLOCK(sc); 2605288648Sadrian m_freem(m); 2606188417Sthompsa return (EIO); 2607188417Sthompsa } 2608188417Sthompsa ZYD_UNLOCK(sc); 2609188417Sthompsa return (0); 2610184610Salfred} 2611184610Salfred 2612287197Sglebiusstatic void 2613287197Sglebiuszyd_parent(struct ieee80211com *ic) 2614184610Salfred{ 2615286950Sadrian struct zyd_softc *sc = ic->ic_softc; 2616246614Shselasky int startall = 0; 2617184610Salfred 2618246614Shselasky ZYD_LOCK(sc); 2619287197Sglebius if (sc->sc_flags & ZYD_FLAG_DETACHED) { 2620188417Sthompsa ZYD_UNLOCK(sc); 2621287197Sglebius return; 2622184610Salfred } 2623287197Sglebius if (ic->ic_nrunning > 0) { 2624287197Sglebius if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) { 2625287197Sglebius zyd_init_locked(sc); 2626287197Sglebius startall = 1; 2627287197Sglebius } else 2628287197Sglebius zyd_set_multi(sc); 2629287197Sglebius } else if (sc->sc_flags & ZYD_FLAG_RUNNING) 2630287197Sglebius zyd_stop(sc); 2631287197Sglebius ZYD_UNLOCK(sc); 2632287197Sglebius if (startall) 2633287197Sglebius ieee80211_start_all(ic); 2634184610Salfred} 2635184610Salfred 2636184610Salfredstatic void 2637191746Sthompsazyd_init_locked(struct zyd_softc *sc) 2638184610Salfred{ 2639287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 2640287197Sglebius struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2641192984Sthompsa struct usb_config_descriptor *cd; 2642188419Sthompsa int error; 2643188417Sthompsa uint32_t val; 2644184610Salfred 2645188417Sthompsa ZYD_LOCK_ASSERT(sc, MA_OWNED); 2646184610Salfred 2647188417Sthompsa if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) { 2648188417Sthompsa error = zyd_loadfirmware(sc); 2649188417Sthompsa if (error != 0) { 2650188417Sthompsa device_printf(sc->sc_dev, 2651188417Sthompsa "could not load firmware (error=%d)\n", error); 2652188417Sthompsa goto fail; 2653188417Sthompsa } 2654188417Sthompsa 2655188417Sthompsa /* reset device */ 2656194228Sthompsa cd = usbd_get_config_descriptor(sc->sc_udev); 2657194228Sthompsa error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx, 2658188417Sthompsa cd->bConfigurationValue); 2659188417Sthompsa if (error) 2660188417Sthompsa device_printf(sc->sc_dev, "reset failed, continuing\n"); 2661188417Sthompsa 2662188417Sthompsa error = zyd_hw_init(sc); 2663188417Sthompsa if (error) { 2664188417Sthompsa device_printf(sc->sc_dev, 2665188417Sthompsa "hardware initialization failed\n"); 2666188417Sthompsa goto fail; 2667188417Sthompsa } 2668188417Sthompsa 2669188417Sthompsa device_printf(sc->sc_dev, 2670188417Sthompsa "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x " 2671188417Sthompsa "BE%x NP%x Gain%x F%x\n", 2672188417Sthompsa (sc->sc_macrev == ZYD_ZD1211) ? "": "B", 2673188417Sthompsa sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff, 2674188417Sthompsa zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev, 2675188417Sthompsa sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy, 2676188417Sthompsa sc->sc_cckgain, sc->sc_fix_cr157); 2677188417Sthompsa 2678188417Sthompsa /* read regulatory domain (currently unused) */ 2679188417Sthompsa zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val); 2680188417Sthompsa sc->sc_regdomain = val >> 16; 2681188417Sthompsa DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n", 2682188417Sthompsa sc->sc_regdomain); 2683188417Sthompsa 2684188417Sthompsa /* we'll do software WEP decryption for now */ 2685188417Sthompsa DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n", 2686188417Sthompsa __func__); 2687188417Sthompsa zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER); 2688188417Sthompsa 2689188417Sthompsa sc->sc_flags |= ZYD_FLAG_INITONCE; 2690184610Salfred } 2691184610Salfred 2692287197Sglebius if (sc->sc_flags & ZYD_FLAG_RUNNING) 2693191746Sthompsa zyd_stop(sc); 2694188417Sthompsa 2695190526Ssam DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n", 2696287197Sglebius vap ? vap->iv_myaddr : ic->ic_macaddr, ":"); 2697287197Sglebius error = zyd_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr); 2698188417Sthompsa if (error != 0) 2699184610Salfred return; 2700184610Salfred 2701188417Sthompsa /* set basic rates */ 2702188417Sthompsa if (ic->ic_curmode == IEEE80211_MODE_11B) 2703188417Sthompsa zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003); 2704188417Sthompsa else if (ic->ic_curmode == IEEE80211_MODE_11A) 2705188417Sthompsa zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500); 2706188417Sthompsa else /* assumes 802.11b/g */ 2707188417Sthompsa zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f); 2708184610Salfred 2709188417Sthompsa /* promiscuous mode */ 2710188417Sthompsa zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0); 2711188417Sthompsa /* multicast setup */ 2712188417Sthompsa zyd_set_multi(sc); 2713188417Sthompsa /* set RX filter */ 2714188417Sthompsa error = zyd_set_rxfilter(sc); 2715188417Sthompsa if (error != 0) 2716188417Sthompsa goto fail; 2717184610Salfred 2718188417Sthompsa /* switch radio transmitter ON */ 2719188417Sthompsa error = zyd_switch_radio(sc, 1); 2720188417Sthompsa if (error != 0) 2721188417Sthompsa goto fail; 2722188417Sthompsa /* set default BSS channel */ 2723188417Sthompsa zyd_set_chan(sc, ic->ic_curchan); 2724184610Salfred 2725188417Sthompsa /* 2726188417Sthompsa * Allocate Tx and Rx xfer queues. 2727188417Sthompsa */ 2728188419Sthompsa zyd_setup_tx_list(sc); 2729184610Salfred 2730188417Sthompsa /* enable interrupts */ 2731188417Sthompsa zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK); 2732184610Salfred 2733287197Sglebius sc->sc_flags |= ZYD_FLAG_RUNNING; 2734194677Sthompsa usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]); 2735194228Sthompsa usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]); 2736194228Sthompsa usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]); 2737184610Salfred 2738188417Sthompsa return; 2739184610Salfred 2740191746Sthompsafail: zyd_stop(sc); 2741188417Sthompsa return; 2742184610Salfred} 2743184610Salfred 2744184610Salfredstatic void 2745191746Sthompsazyd_stop(struct zyd_softc *sc) 2746184610Salfred{ 2747188417Sthompsa int error; 2748184610Salfred 2749188417Sthompsa ZYD_LOCK_ASSERT(sc, MA_OWNED); 2750184610Salfred 2751287197Sglebius sc->sc_flags &= ~ZYD_FLAG_RUNNING; 2752288648Sadrian zyd_drain_mbufq(sc); 2753184610Salfred 2754188417Sthompsa /* 2755188419Sthompsa * Drain all the transfers, if not already drained: 2756188417Sthompsa */ 2757188419Sthompsa ZYD_UNLOCK(sc); 2758194228Sthompsa usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]); 2759194228Sthompsa usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]); 2760188419Sthompsa ZYD_LOCK(sc); 2761188417Sthompsa 2762188419Sthompsa zyd_unsetup_tx_list(sc); 2763188417Sthompsa 2764188419Sthompsa /* Stop now if the device was never set up */ 2765188419Sthompsa if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) 2766184610Salfred return; 2767184610Salfred 2768188417Sthompsa /* switch radio transmitter OFF */ 2769188417Sthompsa error = zyd_switch_radio(sc, 0); 2770188417Sthompsa if (error != 0) 2771188417Sthompsa goto fail; 2772188417Sthompsa /* disable Rx */ 2773188417Sthompsa zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0); 2774188417Sthompsa /* disable interrupts */ 2775188417Sthompsa zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0); 2776184610Salfred 2777188417Sthompsafail: 2778188417Sthompsa return; 2779184610Salfred} 2780184610Salfred 2781188417Sthompsastatic int 2782188417Sthompsazyd_loadfirmware(struct zyd_softc *sc) 2783184610Salfred{ 2784192984Sthompsa struct usb_device_request req; 2785188417Sthompsa size_t size; 2786188417Sthompsa u_char *fw; 2787188417Sthompsa uint8_t stat; 2788188417Sthompsa uint16_t addr; 2789184610Salfred 2790188417Sthompsa if (sc->sc_flags & ZYD_FLAG_FWLOADED) 2791188417Sthompsa return (0); 2792184610Salfred 2793188417Sthompsa if (sc->sc_macrev == ZYD_ZD1211) { 2794188417Sthompsa fw = (u_char *)zd1211_firmware; 2795188417Sthompsa size = sizeof(zd1211_firmware); 2796188417Sthompsa } else { 2797188417Sthompsa fw = (u_char *)zd1211b_firmware; 2798188417Sthompsa size = sizeof(zd1211b_firmware); 2799184610Salfred } 2800184610Salfred 2801188417Sthompsa req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 2802188417Sthompsa req.bRequest = ZYD_DOWNLOADREQ; 2803188417Sthompsa USETW(req.wIndex, 0); 2804184610Salfred 2805188417Sthompsa addr = ZYD_FIRMWARE_START_ADDR; 2806188417Sthompsa while (size > 0) { 2807188417Sthompsa /* 2808188417Sthompsa * When the transfer size is 4096 bytes, it is not 2809188417Sthompsa * likely to be able to transfer it. 2810188417Sthompsa * The cause is port or machine or chip? 2811188417Sthompsa */ 2812188417Sthompsa const int mlen = min(size, 64); 2813184610Salfred 2814188417Sthompsa DPRINTF(sc, ZYD_DEBUG_FW, 2815188417Sthompsa "loading firmware block: len=%d, addr=0x%x\n", mlen, addr); 2816184610Salfred 2817188417Sthompsa USETW(req.wValue, addr); 2818188417Sthompsa USETW(req.wLength, mlen); 2819188419Sthompsa if (zyd_do_request(sc, &req, fw) != 0) 2820188417Sthompsa return (EIO); 2821184610Salfred 2822188417Sthompsa addr += mlen / 2; 2823188417Sthompsa fw += mlen; 2824188417Sthompsa size -= mlen; 2825184610Salfred } 2826184610Salfred 2827188417Sthompsa /* check whether the upload succeeded */ 2828188417Sthompsa req.bmRequestType = UT_READ_VENDOR_DEVICE; 2829188417Sthompsa req.bRequest = ZYD_DOWNLOADSTS; 2830188417Sthompsa USETW(req.wValue, 0); 2831188417Sthompsa USETW(req.wIndex, 0); 2832188417Sthompsa USETW(req.wLength, sizeof(stat)); 2833188419Sthompsa if (zyd_do_request(sc, &req, &stat) != 0) 2834188417Sthompsa return (EIO); 2835184610Salfred 2836188417Sthompsa sc->sc_flags |= ZYD_FLAG_FWLOADED; 2837184610Salfred 2838188417Sthompsa return (stat & 0x80) ? (EIO) : (0); 2839184610Salfred} 2840184610Salfred 2841184610Salfredstatic void 2842188417Sthompsazyd_scan_start(struct ieee80211com *ic) 2843184610Salfred{ 2844286950Sadrian struct zyd_softc *sc = ic->ic_softc; 2845184610Salfred 2846188417Sthompsa ZYD_LOCK(sc); 2847191746Sthompsa /* want broadcast address while scanning */ 2848287197Sglebius zyd_set_bssid(sc, ieee80211broadcastaddr); 2849188417Sthompsa ZYD_UNLOCK(sc); 2850184610Salfred} 2851184610Salfred 2852184610Salfredstatic void 2853188417Sthompsazyd_scan_end(struct ieee80211com *ic) 2854184610Salfred{ 2855286950Sadrian struct zyd_softc *sc = ic->ic_softc; 2856184610Salfred 2857188417Sthompsa ZYD_LOCK(sc); 2858191746Sthompsa /* restore previous bssid */ 2859296356Savos zyd_set_bssid(sc, sc->sc_bssid); 2860188417Sthompsa ZYD_UNLOCK(sc); 2861184610Salfred} 2862184610Salfred 2863184610Salfredstatic void 2864300751Savoszyd_getradiocaps(struct ieee80211com *ic, 2865300751Savos int maxchans, int *nchans, struct ieee80211_channel chans[]) 2866300751Savos{ 2867300751Savos uint8_t bands[IEEE80211_MODE_BYTES]; 2868300751Savos 2869300751Savos memset(bands, 0, sizeof(bands)); 2870300751Savos setbit(bands, IEEE80211_MODE_11B); 2871300751Savos setbit(bands, IEEE80211_MODE_11G); 2872343976Savos ieee80211_add_channels_default_2ghz(chans, maxchans, nchans, bands, 0); 2873300751Savos} 2874300751Savos 2875300751Savosstatic void 2876188417Sthompsazyd_set_channel(struct ieee80211com *ic) 2877184610Salfred{ 2878286950Sadrian struct zyd_softc *sc = ic->ic_softc; 2879184610Salfred 2880188417Sthompsa ZYD_LOCK(sc); 2881191746Sthompsa zyd_set_chan(sc, ic->ic_curchan); 2882188417Sthompsa ZYD_UNLOCK(sc); 2883184610Salfred} 2884184610Salfred 2885188417Sthompsastatic device_method_t zyd_methods[] = { 2886188417Sthompsa /* Device interface */ 2887188417Sthompsa DEVMETHOD(device_probe, zyd_match), 2888188417Sthompsa DEVMETHOD(device_attach, zyd_attach), 2889188417Sthompsa DEVMETHOD(device_detach, zyd_detach), 2890246614Shselasky DEVMETHOD_END 2891188417Sthompsa}; 2892184610Salfred 2893188417Sthompsastatic driver_t zyd_driver = { 2894233774Shselasky .name = "zyd", 2895233774Shselasky .methods = zyd_methods, 2896233774Shselasky .size = sizeof(struct zyd_softc) 2897188417Sthompsa}; 2898184610Salfred 2899188417Sthompsastatic devclass_t zyd_devclass; 2900184610Salfred 2901189275SthompsaDRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0); 2902188942SthompsaMODULE_DEPEND(zyd, usb, 1, 1, 1); 2903188417SthompsaMODULE_DEPEND(zyd, wlan, 1, 1, 1); 2904212122SthompsaMODULE_VERSION(zyd, 1); 2905292080SimpUSB_PNP_HOST_INFO(zyd_devs); 2906