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