1 /* $OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $ */ 2 /* $NetBSD: if_zyd.c,v 1.52 2019/05/05 03:17:54 mrg Exp $ */ 3 4 /*- 5 * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr> 6 * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 /*- 22 * ZyDAS ZD1211/ZD1211B USB WLAN driver. 23 */ 24 25 #include <sys/cdefs.h> 26 __KERNEL_RCSID(0, "$NetBSD: if_zyd.c,v 1.52 2019/05/05 03:17:54 mrg Exp $"); 27 28 #ifdef _KERNEL_OPT 29 #include "opt_usb.h" 30 #endif 31 32 #include <sys/param.h> 33 #include <sys/sockio.h> 34 #include <sys/proc.h> 35 #include <sys/mbuf.h> 36 #include <sys/kernel.h> 37 #include <sys/kmem.h> 38 #include <sys/socket.h> 39 #include <sys/systm.h> 40 #include <sys/malloc.h> 41 #include <sys/conf.h> 42 #include <sys/device.h> 43 44 #include <sys/bus.h> 45 #include <machine/endian.h> 46 47 #include <net/bpf.h> 48 #include <net/if.h> 49 #include <net/if_arp.h> 50 #include <net/if_dl.h> 51 #include <net/if_ether.h> 52 #include <net/if_media.h> 53 #include <net/if_types.h> 54 55 #include <netinet/in.h> 56 #include <netinet/in_systm.h> 57 #include <netinet/in_var.h> 58 #include <netinet/ip.h> 59 60 #include <net80211/ieee80211_netbsd.h> 61 #include <net80211/ieee80211_var.h> 62 #include <net80211/ieee80211_amrr.h> 63 #include <net80211/ieee80211_radiotap.h> 64 65 #include <dev/firmload.h> 66 67 #include <dev/usb/usb.h> 68 #include <dev/usb/usbdi.h> 69 #include <dev/usb/usbdi_util.h> 70 #include <dev/usb/usbdevs.h> 71 72 #include <dev/usb/if_zydreg.h> 73 74 #ifdef ZYD_DEBUG 75 #define DPRINTF(x) do { if (zyddebug > 0) printf x; } while (0) 76 #define DPRINTFN(n, x) do { if (zyddebug > (n)) printf x; } while (0) 77 int zyddebug = 0; 78 #else 79 #define DPRINTF(x) 80 #define DPRINTFN(n, x) 81 #endif 82 83 static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY; 84 static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB; 85 86 /* various supported device vendors/products */ 87 #define ZYD_ZD1211_DEV(v, p) \ 88 { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211 } 89 #define ZYD_ZD1211B_DEV(v, p) \ 90 { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211B } 91 static const struct zyd_type { 92 struct usb_devno dev; 93 uint8_t rev; 94 #define ZYD_ZD1211 0 95 #define ZYD_ZD1211B 1 96 } zyd_devs[] = { 97 ZYD_ZD1211_DEV(3COM2, 3CRUSB10075), 98 ZYD_ZD1211_DEV(ABOCOM, WL54), 99 ZYD_ZD1211_DEV(ASUSTEK, WL159G), 100 ZYD_ZD1211_DEV(CYBERTAN, TG54USB), 101 ZYD_ZD1211_DEV(DRAYTEK, VIGOR550), 102 ZYD_ZD1211_DEV(PLANEX2, GWUS54GD), 103 ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL), 104 ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ), 105 ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI), 106 ZYD_ZD1211_DEV(SAGEM, XG760A), 107 ZYD_ZD1211_DEV(SENAO, NUB8301), 108 ZYD_ZD1211_DEV(SITECOMEU, WL113), 109 ZYD_ZD1211_DEV(SWEEX, ZD1211), 110 ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN), 111 ZYD_ZD1211_DEV(TEKRAM, ZD1211_1), 112 ZYD_ZD1211_DEV(TEKRAM, ZD1211_2), 113 ZYD_ZD1211_DEV(TWINMOS, G240), 114 ZYD_ZD1211_DEV(UMEDIA, ALL0298V2), 115 ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A), 116 ZYD_ZD1211_DEV(UMEDIA, TEW429UB), 117 ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G), 118 ZYD_ZD1211_DEV(ZCOM, ZD1211), 119 ZYD_ZD1211_DEV(ZYDAS, ZD1211), 120 ZYD_ZD1211_DEV(ZYXEL, AG225H), 121 ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220), 122 ZYD_ZD1211_DEV(ZYXEL, G200V2), 123 124 ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG), 125 ZYD_ZD1211B_DEV(ACCTON, WN4501H_LF_IR), 126 ZYD_ZD1211B_DEV(ACCTON, WUS201), 127 ZYD_ZD1211B_DEV(ACCTON, ZD1211B), 128 ZYD_ZD1211B_DEV(ASUSTEK, A9T_WIFI), 129 ZYD_ZD1211B_DEV(BELKIN, F5D7050C), 130 ZYD_ZD1211B_DEV(BELKIN, ZD1211B), 131 ZYD_ZD1211B_DEV(BEWAN, BWIFI_USB54AR), 132 ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G), 133 ZYD_ZD1211B_DEV(CYBERTAN, ZD1211B), 134 ZYD_ZD1211B_DEV(FIBERLINE, WL430U), 135 ZYD_ZD1211B_DEV(MELCO, KG54L), 136 ZYD_ZD1211B_DEV(PHILIPS, SNU5600), 137 ZYD_ZD1211B_DEV(PHILIPS, SNU5630NS05), 138 ZYD_ZD1211B_DEV(PLANEX2, GWUS54GXS), 139 ZYD_ZD1211B_DEV(SAGEM, XG76NA), 140 ZYD_ZD1211B_DEV(SITECOMEU, WL603), 141 ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B), 142 ZYD_ZD1211B_DEV(SONY, IFU_WLM2), 143 ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1), 144 ZYD_ZD1211B_DEV(UNKNOWN1, ZD1211B_1), 145 ZYD_ZD1211B_DEV(UNKNOWN1, ZD1211B_2), 146 ZYD_ZD1211B_DEV(UNKNOWN2, ZD1211B), 147 ZYD_ZD1211B_DEV(UNKNOWN3, ZD1211B), 148 ZYD_ZD1211B_DEV(USR, USR5423), 149 ZYD_ZD1211B_DEV(VTECH, ZD1211B), 150 ZYD_ZD1211B_DEV(ZCOM, ZD1211B), 151 ZYD_ZD1211B_DEV(ZYDAS, ZD1211B), 152 ZYD_ZD1211B_DEV(ZYDAS, ZD1211B_2), 153 ZYD_ZD1211B_DEV(ZYXEL, M202), 154 ZYD_ZD1211B_DEV(ZYXEL, G220V2), 155 }; 156 #define zyd_lookup(v, p) \ 157 ((const struct zyd_type *)usb_lookup(zyd_devs, v, p)) 158 159 int zyd_match(device_t, cfdata_t, void *); 160 void zyd_attach(device_t, device_t, void *); 161 int zyd_detach(device_t, int); 162 int zyd_activate(device_t, enum devact); 163 164 165 CFATTACH_DECL_NEW(zyd, sizeof(struct zyd_softc), zyd_match, 166 zyd_attach, zyd_detach, zyd_activate); 167 168 Static void zyd_attachhook(device_t); 169 Static int zyd_complete_attach(struct zyd_softc *); 170 Static int zyd_open_pipes(struct zyd_softc *); 171 Static void zyd_close_pipes(struct zyd_softc *); 172 Static int zyd_alloc_tx_list(struct zyd_softc *); 173 Static void zyd_free_tx_list(struct zyd_softc *); 174 Static int zyd_alloc_rx_list(struct zyd_softc *); 175 Static void zyd_free_rx_list(struct zyd_softc *); 176 Static struct ieee80211_node *zyd_node_alloc(struct ieee80211_node_table *); 177 Static int zyd_media_change(struct ifnet *); 178 Static void zyd_next_scan(void *); 179 Static void zyd_task(void *); 180 Static int zyd_newstate(struct ieee80211com *, enum ieee80211_state, int); 181 Static int zyd_cmd(struct zyd_softc *, uint16_t, const void *, int, 182 void *, int, u_int); 183 Static int zyd_read16(struct zyd_softc *, uint16_t, uint16_t *); 184 Static int zyd_read32(struct zyd_softc *, uint16_t, uint32_t *); 185 Static int zyd_write16(struct zyd_softc *, uint16_t, uint16_t); 186 Static int zyd_write32(struct zyd_softc *, uint16_t, uint32_t); 187 Static int zyd_rfwrite(struct zyd_softc *, uint32_t); 188 Static void zyd_lock_phy(struct zyd_softc *); 189 Static void zyd_unlock_phy(struct zyd_softc *); 190 Static int zyd_rfmd_init(struct zyd_rf *); 191 Static int zyd_rfmd_switch_radio(struct zyd_rf *, int); 192 Static int zyd_rfmd_set_channel(struct zyd_rf *, uint8_t); 193 Static int zyd_al2230_init(struct zyd_rf *); 194 Static int zyd_al2230_switch_radio(struct zyd_rf *, int); 195 Static int zyd_al2230_set_channel(struct zyd_rf *, uint8_t); 196 Static int zyd_al2230_init_b(struct zyd_rf *); 197 Static int zyd_al7230B_init(struct zyd_rf *); 198 Static int zyd_al7230B_switch_radio(struct zyd_rf *, int); 199 Static int zyd_al7230B_set_channel(struct zyd_rf *, uint8_t); 200 Static int zyd_al2210_init(struct zyd_rf *); 201 Static int zyd_al2210_switch_radio(struct zyd_rf *, int); 202 Static int zyd_al2210_set_channel(struct zyd_rf *, uint8_t); 203 Static int zyd_gct_init(struct zyd_rf *); 204 Static int zyd_gct_switch_radio(struct zyd_rf *, int); 205 Static int zyd_gct_set_channel(struct zyd_rf *, uint8_t); 206 Static int zyd_maxim_init(struct zyd_rf *); 207 Static int zyd_maxim_switch_radio(struct zyd_rf *, int); 208 Static int zyd_maxim_set_channel(struct zyd_rf *, uint8_t); 209 Static int zyd_maxim2_init(struct zyd_rf *); 210 Static int zyd_maxim2_switch_radio(struct zyd_rf *, int); 211 Static int zyd_maxim2_set_channel(struct zyd_rf *, uint8_t); 212 Static int zyd_rf_attach(struct zyd_softc *, uint8_t); 213 Static const char *zyd_rf_name(uint8_t); 214 Static int zyd_hw_init(struct zyd_softc *); 215 Static int zyd_read_eeprom(struct zyd_softc *); 216 Static int zyd_set_macaddr(struct zyd_softc *, const uint8_t *); 217 Static int zyd_set_bssid(struct zyd_softc *, const uint8_t *); 218 Static int zyd_switch_radio(struct zyd_softc *, int); 219 Static void zyd_set_led(struct zyd_softc *, int, int); 220 Static int zyd_set_rxfilter(struct zyd_softc *); 221 Static void zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *); 222 Static int zyd_set_beacon_interval(struct zyd_softc *, int); 223 Static uint8_t zyd_plcp_signal(int); 224 Static void zyd_intr(struct usbd_xfer *, void *, usbd_status); 225 Static void zyd_rx_data(struct zyd_softc *, const uint8_t *, uint16_t); 226 Static void zyd_rxeof(struct usbd_xfer *, void *, usbd_status); 227 Static void zyd_txeof(struct usbd_xfer *, void *, usbd_status); 228 Static int zyd_tx_mgt(struct zyd_softc *, struct mbuf *, 229 struct ieee80211_node *); 230 Static int zyd_tx_data(struct zyd_softc *, struct mbuf *, 231 struct ieee80211_node *); 232 Static void zyd_start(struct ifnet *); 233 Static void zyd_watchdog(struct ifnet *); 234 Static int zyd_ioctl(struct ifnet *, u_long, void *); 235 Static int zyd_init(struct ifnet *); 236 Static void zyd_stop(struct ifnet *, int); 237 Static int zyd_loadfirmware(struct zyd_softc *, u_char *, size_t); 238 Static void zyd_iter_func(void *, struct ieee80211_node *); 239 Static void zyd_amrr_timeout(void *); 240 Static void zyd_newassoc(struct ieee80211_node *, int); 241 242 int 243 zyd_match(device_t parent, cfdata_t match, void *aux) 244 { 245 struct usb_attach_arg *uaa = aux; 246 247 return (zyd_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL) ? 248 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 249 } 250 251 Static void 252 zyd_attachhook(device_t self) 253 { 254 struct zyd_softc *sc = device_private(self); 255 firmware_handle_t fwh; 256 const char *fwname; 257 u_char *fw; 258 size_t size; 259 int error; 260 261 fwname = (sc->mac_rev == ZYD_ZD1211) ? "zyd-zd1211" : "zyd-zd1211b"; 262 if ((error = firmware_open("zyd", fwname, &fwh)) != 0) { 263 aprint_error_dev(sc->sc_dev, 264 "failed to open firmware %s (error=%d)\n", fwname, error); 265 return; 266 } 267 size = firmware_get_size(fwh); 268 fw = firmware_malloc(size); 269 if (fw == NULL) { 270 aprint_error_dev(sc->sc_dev, 271 "failed to allocate firmware memory\n"); 272 firmware_close(fwh); 273 return; 274 } 275 error = firmware_read(fwh, 0, fw, size); 276 firmware_close(fwh); 277 if (error != 0) { 278 aprint_error_dev(sc->sc_dev, 279 "failed to read firmware (error %d)\n", error); 280 firmware_free(fw, size); 281 return; 282 } 283 284 error = zyd_loadfirmware(sc, fw, size); 285 if (error != 0) { 286 aprint_error_dev(sc->sc_dev, 287 "could not load firmware (error=%d)\n", error); 288 firmware_free(fw, size); 289 return; 290 } 291 292 firmware_free(fw, size); 293 294 /* complete the attach process */ 295 if ((error = zyd_complete_attach(sc)) == 0) 296 sc->attached = 1; 297 return; 298 } 299 300 void 301 zyd_attach(device_t parent, device_t self, void *aux) 302 { 303 struct zyd_softc *sc = device_private(self); 304 struct usb_attach_arg *uaa = aux; 305 char *devinfop; 306 usb_device_descriptor_t* ddesc; 307 struct ifnet *ifp = &sc->sc_if; 308 309 sc->sc_dev = self; 310 sc->sc_udev = uaa->uaa_device; 311 312 aprint_naive("\n"); 313 aprint_normal("\n"); 314 315 devinfop = usbd_devinfo_alloc(uaa->uaa_device, 0); 316 aprint_normal_dev(self, "%s\n", devinfop); 317 usbd_devinfo_free(devinfop); 318 319 sc->mac_rev = zyd_lookup(uaa->uaa_vendor, uaa->uaa_product)->rev; 320 321 ddesc = usbd_get_device_descriptor(sc->sc_udev); 322 if (UGETW(ddesc->bcdDevice) < 0x4330) { 323 aprint_error_dev(self, "device version mismatch: 0x%x " 324 "(only >= 43.30 supported)\n", UGETW(ddesc->bcdDevice)); 325 return; 326 } 327 328 ifp->if_softc = sc; 329 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 330 ifp->if_init = zyd_init; 331 ifp->if_ioctl = zyd_ioctl; 332 ifp->if_start = zyd_start; 333 ifp->if_watchdog = zyd_watchdog; 334 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 335 IFQ_SET_READY(&ifp->if_snd); 336 memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ); 337 338 SIMPLEQ_INIT(&sc->sc_rqh); 339 340 /* defer configrations after file system is ready to load firmware */ 341 config_mountroot(self, zyd_attachhook); 342 } 343 344 Static int 345 zyd_complete_attach(struct zyd_softc *sc) 346 { 347 struct ieee80211com *ic = &sc->sc_ic; 348 struct ifnet *ifp = &sc->sc_if; 349 usbd_status error; 350 int i; 351 352 usb_init_task(&sc->sc_task, zyd_task, sc, 0); 353 callout_init(&(sc->sc_scan_ch), 0); 354 355 sc->amrr.amrr_min_success_threshold = 1; 356 sc->amrr.amrr_max_success_threshold = 10; 357 callout_init(&sc->sc_amrr_ch, 0); 358 359 error = usbd_set_config_no(sc->sc_udev, ZYD_CONFIG_NO, 1); 360 if (error != 0) { 361 aprint_error_dev(sc->sc_dev, "failed to set configuration" 362 ", err=%s\n", usbd_errstr(error)); 363 goto fail; 364 } 365 366 error = usbd_device2interface_handle(sc->sc_udev, ZYD_IFACE_INDEX, 367 &sc->sc_iface); 368 if (error != 0) { 369 aprint_error_dev(sc->sc_dev, 370 "getting interface handle failed\n"); 371 goto fail; 372 } 373 374 if ((error = zyd_open_pipes(sc)) != 0) { 375 aprint_error_dev(sc->sc_dev, "could not open pipes\n"); 376 goto fail; 377 } 378 379 if ((error = zyd_read_eeprom(sc)) != 0) { 380 aprint_error_dev(sc->sc_dev, "could not read EEPROM\n"); 381 goto fail; 382 } 383 384 if ((error = zyd_rf_attach(sc, sc->rf_rev)) != 0) { 385 aprint_error_dev(sc->sc_dev, "could not attach RF\n"); 386 goto fail; 387 } 388 389 if ((error = zyd_hw_init(sc)) != 0) { 390 aprint_error_dev(sc->sc_dev, 391 "hardware initialization failed\n"); 392 goto fail; 393 } 394 395 aprint_normal_dev(sc->sc_dev, 396 "HMAC ZD1211%s, FW %02x.%02x, RF %s, PA %x, address %s\n", 397 (sc->mac_rev == ZYD_ZD1211) ? "": "B", 398 sc->fw_rev >> 8, sc->fw_rev & 0xff, zyd_rf_name(sc->rf_rev), 399 sc->pa_rev, ether_sprintf(ic->ic_myaddr)); 400 401 ic->ic_ifp = ifp; 402 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 403 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 404 ic->ic_state = IEEE80211_S_INIT; 405 406 /* set device capabilities */ 407 ic->ic_caps = 408 IEEE80211_C_MONITOR | /* monitor mode supported */ 409 IEEE80211_C_TXPMGT | /* tx power management */ 410 IEEE80211_C_SHPREAMBLE | /* short preamble supported */ 411 IEEE80211_C_WEP; /* s/w WEP */ 412 413 /* set supported .11b and .11g rates */ 414 ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b; 415 ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g; 416 417 /* set supported .11b and .11g channels (1 through 14) */ 418 for (i = 1; i <= 14; i++) { 419 ic->ic_channels[i].ic_freq = 420 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ); 421 ic->ic_channels[i].ic_flags = 422 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 423 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 424 } 425 426 if_attach(ifp); 427 ieee80211_ifattach(ic); 428 ic->ic_node_alloc = zyd_node_alloc; 429 ic->ic_newassoc = zyd_newassoc; 430 431 /* override state transition machine */ 432 sc->sc_newstate = ic->ic_newstate; 433 ic->ic_newstate = zyd_newstate; 434 ieee80211_media_init(ic, zyd_media_change, ieee80211_media_status); 435 436 bpf_attach2(ifp, DLT_IEEE802_11_RADIO, 437 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN, 438 &sc->sc_drvbpf); 439 440 sc->sc_rxtap_len = sizeof(sc->sc_rxtapu); 441 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 442 sc->sc_rxtap.wr_ihdr.it_present = htole32(ZYD_RX_RADIOTAP_PRESENT); 443 444 sc->sc_txtap_len = sizeof(sc->sc_txtapu); 445 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 446 sc->sc_txtap.wt_ihdr.it_present = htole32(ZYD_TX_RADIOTAP_PRESENT); 447 448 ieee80211_announce(ic); 449 450 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 451 452 fail: return error; 453 } 454 455 int 456 zyd_detach(device_t self, int flags) 457 { 458 struct zyd_softc *sc = device_private(self); 459 struct ieee80211com *ic = &sc->sc_ic; 460 struct ifnet *ifp = &sc->sc_if; 461 int s; 462 463 if (!sc->attached) 464 return 0; 465 466 s = splusb(); 467 468 zyd_stop(ifp, 1); 469 callout_halt(&sc->sc_scan_ch, NULL); 470 callout_halt(&sc->sc_amrr_ch, NULL); 471 usb_rem_task_wait(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER, NULL); 472 473 /* Abort, etc. done by zyd_stop */ 474 zyd_close_pipes(sc); 475 476 sc->attached = 0; 477 478 bpf_detach(ifp); 479 ieee80211_ifdetach(ic); 480 if_detach(ifp); 481 482 splx(s); 483 484 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 485 486 return 0; 487 } 488 489 Static int 490 zyd_open_pipes(struct zyd_softc *sc) 491 { 492 usb_endpoint_descriptor_t *edesc; 493 usbd_status error; 494 495 /* interrupt in */ 496 edesc = usbd_get_endpoint_descriptor(sc->sc_iface, 0x83); 497 if (edesc == NULL) 498 return EINVAL; 499 500 sc->ibuf_size = UGETW(edesc->wMaxPacketSize); 501 if (sc->ibuf_size == 0) /* should not happen */ 502 return EINVAL; 503 504 sc->ibuf = kmem_alloc(sc->ibuf_size, KM_SLEEP); 505 506 error = usbd_open_pipe_intr(sc->sc_iface, 0x83, USBD_SHORT_XFER_OK, 507 &sc->zyd_ep[ZYD_ENDPT_IIN], sc, sc->ibuf, sc->ibuf_size, zyd_intr, 508 USBD_DEFAULT_INTERVAL); 509 if (error != 0) { 510 printf("%s: open rx intr pipe failed: %s\n", 511 device_xname(sc->sc_dev), usbd_errstr(error)); 512 goto fail; 513 } 514 515 /* interrupt out (not necessarily an interrupt pipe) */ 516 error = usbd_open_pipe(sc->sc_iface, 0x04, USBD_EXCLUSIVE_USE, 517 &sc->zyd_ep[ZYD_ENDPT_IOUT]); 518 if (error != 0) { 519 printf("%s: open tx intr pipe failed: %s\n", 520 device_xname(sc->sc_dev), usbd_errstr(error)); 521 goto fail; 522 } 523 524 /* bulk in */ 525 error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE, 526 &sc->zyd_ep[ZYD_ENDPT_BIN]); 527 if (error != 0) { 528 printf("%s: open rx pipe failed: %s\n", 529 device_xname(sc->sc_dev), usbd_errstr(error)); 530 goto fail; 531 } 532 533 /* bulk out */ 534 error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE, 535 &sc->zyd_ep[ZYD_ENDPT_BOUT]); 536 if (error != 0) { 537 printf("%s: open tx pipe failed: %s\n", 538 device_xname(sc->sc_dev), usbd_errstr(error)); 539 goto fail; 540 } 541 542 return 0; 543 544 fail: zyd_close_pipes(sc); 545 return error; 546 } 547 548 Static void 549 zyd_close_pipes(struct zyd_softc *sc) 550 { 551 int i; 552 553 for (i = 0; i < ZYD_ENDPT_CNT; i++) { 554 if (sc->zyd_ep[i] != NULL) { 555 usbd_close_pipe(sc->zyd_ep[i]); 556 sc->zyd_ep[i] = NULL; 557 } 558 } 559 if (sc->ibuf != NULL) { 560 kmem_free(sc->ibuf, sc->ibuf_size); 561 sc->ibuf = NULL; 562 } 563 } 564 565 Static int 566 zyd_alloc_tx_list(struct zyd_softc *sc) 567 { 568 int i, error; 569 570 sc->tx_queued = 0; 571 572 for (i = 0; i < ZYD_TX_LIST_CNT; i++) { 573 struct zyd_tx_data *data = &sc->tx_data[i]; 574 575 data->sc = sc; /* backpointer for callbacks */ 576 577 error = usbd_create_xfer(sc->zyd_ep[ZYD_ENDPT_BOUT], 578 ZYD_MAX_TXBUFSZ, USBD_FORCE_SHORT_XFER, 0, &data->xfer); 579 if (error) { 580 printf("%s: could not allocate tx xfer\n", 581 device_xname(sc->sc_dev)); 582 goto fail; 583 } 584 data->buf = usbd_get_buffer(data->xfer); 585 586 /* clear Tx descriptor */ 587 memset(data->buf, 0, sizeof(struct zyd_tx_desc)); 588 } 589 return 0; 590 591 fail: zyd_free_tx_list(sc); 592 return error; 593 } 594 595 Static void 596 zyd_free_tx_list(struct zyd_softc *sc) 597 { 598 int i; 599 600 for (i = 0; i < ZYD_TX_LIST_CNT; i++) { 601 struct zyd_tx_data *data = &sc->tx_data[i]; 602 603 if (data->xfer != NULL) { 604 usbd_destroy_xfer(data->xfer); 605 data->xfer = NULL; 606 } 607 if (data->ni != NULL) { 608 ieee80211_free_node(data->ni); 609 data->ni = NULL; 610 } 611 } 612 } 613 614 Static int 615 zyd_alloc_rx_list(struct zyd_softc *sc) 616 { 617 int i, error; 618 619 for (i = 0; i < ZYD_RX_LIST_CNT; i++) { 620 struct zyd_rx_data *data = &sc->rx_data[i]; 621 622 data->sc = sc; /* backpointer for callbacks */ 623 624 error = usbd_create_xfer(sc->zyd_ep[ZYD_ENDPT_BIN], 625 ZYX_MAX_RXBUFSZ, 0, 0, &data->xfer); 626 if (error) { 627 printf("%s: could not allocate rx xfer\n", 628 device_xname(sc->sc_dev)); 629 goto fail; 630 } 631 data->buf = usbd_get_buffer(data->xfer); 632 } 633 return 0; 634 635 fail: zyd_free_rx_list(sc); 636 return error; 637 } 638 639 Static void 640 zyd_free_rx_list(struct zyd_softc *sc) 641 { 642 int i; 643 644 for (i = 0; i < ZYD_RX_LIST_CNT; i++) { 645 struct zyd_rx_data *data = &sc->rx_data[i]; 646 647 if (data->xfer != NULL) { 648 usbd_destroy_xfer(data->xfer); 649 data->xfer = NULL; 650 } 651 } 652 } 653 654 /* ARGUSED */ 655 Static struct ieee80211_node * 656 zyd_node_alloc(struct ieee80211_node_table *nt __unused) 657 { 658 struct zyd_node *zn; 659 660 zn = malloc(sizeof(struct zyd_node), M_80211_NODE, M_NOWAIT | M_ZERO); 661 662 return &zn->ni; 663 } 664 665 Static int 666 zyd_media_change(struct ifnet *ifp) 667 { 668 int error; 669 670 error = ieee80211_media_change(ifp); 671 if (error != ENETRESET) 672 return error; 673 674 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) 675 zyd_init(ifp); 676 677 return 0; 678 } 679 680 /* 681 * This function is called periodically (every 200ms) during scanning to 682 * switch from one channel to another. 683 */ 684 Static void 685 zyd_next_scan(void *arg) 686 { 687 struct zyd_softc *sc = arg; 688 struct ieee80211com *ic = &sc->sc_ic; 689 690 if (ic->ic_state == IEEE80211_S_SCAN) 691 ieee80211_next_scan(ic); 692 } 693 694 Static void 695 zyd_task(void *arg) 696 { 697 struct zyd_softc *sc = arg; 698 struct ieee80211com *ic = &sc->sc_ic; 699 enum ieee80211_state ostate; 700 701 ostate = ic->ic_state; 702 703 switch (sc->sc_state) { 704 case IEEE80211_S_INIT: 705 if (ostate == IEEE80211_S_RUN) { 706 /* turn link LED off */ 707 zyd_set_led(sc, ZYD_LED1, 0); 708 709 /* stop data LED from blinking */ 710 zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 0); 711 } 712 break; 713 714 case IEEE80211_S_SCAN: 715 zyd_set_chan(sc, ic->ic_curchan); 716 callout_reset(&sc->sc_scan_ch, hz / 5, zyd_next_scan, sc); 717 break; 718 719 case IEEE80211_S_AUTH: 720 case IEEE80211_S_ASSOC: 721 zyd_set_chan(sc, ic->ic_curchan); 722 break; 723 724 case IEEE80211_S_RUN: 725 { 726 struct ieee80211_node *ni = ic->ic_bss; 727 728 zyd_set_chan(sc, ic->ic_curchan); 729 730 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 731 /* turn link LED on */ 732 zyd_set_led(sc, ZYD_LED1, 1); 733 734 /* make data LED blink upon Tx */ 735 zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 1); 736 737 zyd_set_bssid(sc, ni->ni_bssid); 738 } 739 740 if (ic->ic_opmode == IEEE80211_M_STA) { 741 /* fake a join to init the tx rate */ 742 zyd_newassoc(ni, 1); 743 } 744 745 /* start automatic rate control timer */ 746 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) 747 callout_reset(&sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc); 748 749 break; 750 } 751 } 752 753 sc->sc_newstate(ic, sc->sc_state, -1); 754 } 755 756 Static int 757 zyd_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 758 { 759 struct zyd_softc *sc = ic->ic_ifp->if_softc; 760 761 if (!sc->attached) 762 return ENXIO; 763 764 /* 765 * XXXSMP: This does not wait for the task, if it is in flight, 766 * to complete. If this code works at all, it must rely on the 767 * kernel lock to serialize with the USB task thread. 768 */ 769 usb_rem_task(sc->sc_udev, &sc->sc_task); 770 callout_stop(&sc->sc_scan_ch); 771 callout_stop(&sc->sc_amrr_ch); 772 773 /* do it in a process context */ 774 sc->sc_state = nstate; 775 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER); 776 777 return 0; 778 } 779 780 Static int 781 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen, 782 void *odata, int olen, u_int flags) 783 { 784 struct usbd_xfer *xfer; 785 struct zyd_cmd cmd; 786 struct rq rq; 787 uint16_t xferflags; 788 int error; 789 usbd_status uerror; 790 int s = 0; 791 792 error = usbd_create_xfer(sc->zyd_ep[ZYD_ENDPT_IOUT], 793 sizeof(uint16_t) + ilen, USBD_FORCE_SHORT_XFER, 0, &xfer); 794 if (error) 795 return error; 796 797 cmd.code = htole16(code); 798 memcpy(cmd.data, idata, ilen); 799 800 xferflags = USBD_FORCE_SHORT_XFER; 801 if (!(flags & ZYD_CMD_FLAG_READ)) 802 xferflags |= USBD_SYNCHRONOUS; 803 else { 804 s = splusb(); 805 rq.idata = idata; 806 rq.odata = odata; 807 rq.len = olen / sizeof(struct zyd_pair); 808 SIMPLEQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq); 809 } 810 811 usbd_setup_xfer(xfer, 0, &cmd, sizeof(uint16_t) + ilen, xferflags, 812 ZYD_INTR_TIMEOUT, NULL); 813 uerror = usbd_transfer(xfer); 814 if (uerror != USBD_IN_PROGRESS && uerror != 0) { 815 if (flags & ZYD_CMD_FLAG_READ) 816 splx(s); 817 printf("%s: could not send command (error=%s)\n", 818 device_xname(sc->sc_dev), usbd_errstr(uerror)); 819 (void)usbd_destroy_xfer(xfer); 820 return EIO; 821 } 822 if (!(flags & ZYD_CMD_FLAG_READ)) { 823 (void)usbd_destroy_xfer(xfer); 824 return 0; /* write: don't wait for reply */ 825 } 826 /* wait at most one second for command reply */ 827 error = tsleep(odata, PCATCH, "zydcmd", hz); 828 if (error == EWOULDBLOCK) 829 printf("%s: zyd_read sleep timeout\n", device_xname(sc->sc_dev)); 830 SIMPLEQ_REMOVE(&sc->sc_rqh, &rq, rq, rq); 831 splx(s); 832 833 (void)usbd_destroy_xfer(xfer); 834 return error; 835 } 836 837 Static int 838 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val) 839 { 840 struct zyd_pair tmp; 841 int error; 842 843 reg = htole16(reg); 844 error = zyd_cmd(sc, ZYD_CMD_IORD, ®, sizeof(reg), &tmp, sizeof(tmp), 845 ZYD_CMD_FLAG_READ); 846 if (error == 0) 847 *val = le16toh(tmp.val); 848 else 849 *val = 0; 850 return error; 851 } 852 853 Static int 854 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val) 855 { 856 struct zyd_pair tmp[2]; 857 uint16_t regs[2]; 858 int error; 859 860 regs[0] = htole16(ZYD_REG32_HI(reg)); 861 regs[1] = htole16(ZYD_REG32_LO(reg)); 862 error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp), 863 ZYD_CMD_FLAG_READ); 864 if (error == 0) 865 *val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val); 866 else 867 *val = 0; 868 return error; 869 } 870 871 Static int 872 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val) 873 { 874 struct zyd_pair pair; 875 876 pair.reg = htole16(reg); 877 pair.val = htole16(val); 878 879 return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0); 880 } 881 882 Static int 883 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val) 884 { 885 struct zyd_pair pair[2]; 886 887 pair[0].reg = htole16(ZYD_REG32_HI(reg)); 888 pair[0].val = htole16(val >> 16); 889 pair[1].reg = htole16(ZYD_REG32_LO(reg)); 890 pair[1].val = htole16(val & 0xffff); 891 892 return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0); 893 } 894 895 Static int 896 zyd_rfwrite(struct zyd_softc *sc, uint32_t val) 897 { 898 struct zyd_rf *rf = &sc->sc_rf; 899 struct zyd_rfwrite req; 900 uint16_t cr203; 901 int i; 902 903 (void)zyd_read16(sc, ZYD_CR203, &cr203); 904 cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA); 905 906 req.code = htole16(2); 907 req.width = htole16(rf->width); 908 for (i = 0; i < rf->width; i++) { 909 req.bit[i] = htole16(cr203); 910 if (val & (1 << (rf->width - 1 - i))) 911 req.bit[i] |= htole16(ZYD_RF_DATA); 912 } 913 return zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0); 914 } 915 916 Static void 917 zyd_lock_phy(struct zyd_softc *sc) 918 { 919 uint32_t tmp; 920 921 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp); 922 tmp &= ~ZYD_UNLOCK_PHY_REGS; 923 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp); 924 } 925 926 Static void 927 zyd_unlock_phy(struct zyd_softc *sc) 928 { 929 uint32_t tmp; 930 931 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp); 932 tmp |= ZYD_UNLOCK_PHY_REGS; 933 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp); 934 } 935 936 /* 937 * RFMD RF methods. 938 */ 939 Static int 940 zyd_rfmd_init(struct zyd_rf *rf) 941 { 942 struct zyd_softc *sc = rf->rf_sc; 943 static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY; 944 static const uint32_t rfini[] = ZYD_RFMD_RF; 945 int error; 946 size_t i; 947 948 /* init RF-dependent PHY registers */ 949 for (i = 0; i < __arraycount(phyini); i++) { 950 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 951 if (error != 0) 952 return error; 953 } 954 955 /* init RFMD radio */ 956 for (i = 0; i < __arraycount(rfini); i++) { 957 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 958 return error; 959 } 960 return 0; 961 } 962 963 Static int 964 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on) 965 { 966 struct zyd_softc *sc = rf->rf_sc; 967 968 (void)zyd_write16(sc, ZYD_CR10, on ? 0x89 : 0x15); 969 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x81); 970 971 return 0; 972 } 973 974 Static int 975 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan) 976 { 977 struct zyd_softc *sc = rf->rf_sc; 978 static const struct { 979 uint32_t r1, r2; 980 } rfprog[] = ZYD_RFMD_CHANTABLE; 981 982 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 983 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 984 985 return 0; 986 } 987 988 /* 989 * AL2230 RF methods. 990 */ 991 Static int 992 zyd_al2230_init(struct zyd_rf *rf) 993 { 994 struct zyd_softc *sc = rf->rf_sc; 995 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY; 996 static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT; 997 static const uint32_t rfini[] = ZYD_AL2230_RF; 998 int error; 999 size_t i; 1000 1001 /* init RF-dependent PHY registers */ 1002 for (i = 0; i < __arraycount(phyini); i++) { 1003 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1004 if (error != 0) 1005 return error; 1006 } 1007 1008 if (sc->rf_rev == ZYD_RF_AL2230S) { 1009 for (i = 0; i < __arraycount(phy2230s); i++) { 1010 error = zyd_write16(sc, phy2230s[i].reg, 1011 phy2230s[i].val); 1012 if (error != 0) 1013 return error; 1014 } 1015 } 1016 1017 /* init AL2230 radio */ 1018 for (i = 0; i < __arraycount(rfini); i++) { 1019 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1020 return error; 1021 } 1022 return 0; 1023 } 1024 1025 Static int 1026 zyd_al2230_init_b(struct zyd_rf *rf) 1027 { 1028 struct zyd_softc *sc = rf->rf_sc; 1029 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B; 1030 static const uint32_t rfini[] = ZYD_AL2230_RF_B; 1031 int error; 1032 size_t i; 1033 1034 /* init RF-dependent PHY registers */ 1035 for (i = 0; i < __arraycount(phyini); i++) { 1036 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1037 if (error != 0) 1038 return error; 1039 } 1040 1041 /* init AL2230 radio */ 1042 for (i = 0; i < __arraycount(rfini); i++) { 1043 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1044 return error; 1045 } 1046 return 0; 1047 } 1048 1049 Static int 1050 zyd_al2230_switch_radio(struct zyd_rf *rf, int on) 1051 { 1052 struct zyd_softc *sc = rf->rf_sc; 1053 int on251 = (sc->mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f; 1054 1055 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04); 1056 (void)zyd_write16(sc, ZYD_CR251, on ? on251 : 0x2f); 1057 1058 return 0; 1059 } 1060 1061 Static int 1062 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan) 1063 { 1064 struct zyd_softc *sc = rf->rf_sc; 1065 static const struct { 1066 uint32_t r1, r2, r3; 1067 } rfprog[] = ZYD_AL2230_CHANTABLE; 1068 1069 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 1070 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 1071 (void)zyd_rfwrite(sc, rfprog[chan - 1].r3); 1072 1073 (void)zyd_write16(sc, ZYD_CR138, 0x28); 1074 (void)zyd_write16(sc, ZYD_CR203, 0x06); 1075 1076 return 0; 1077 } 1078 1079 /* 1080 * AL7230B RF methods. 1081 */ 1082 Static int 1083 zyd_al7230B_init(struct zyd_rf *rf) 1084 { 1085 struct zyd_softc *sc = rf->rf_sc; 1086 static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1; 1087 static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2; 1088 static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3; 1089 static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1; 1090 static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2; 1091 int error; 1092 size_t i; 1093 1094 /* for AL7230B, PHY and RF need to be initialized in "phases" */ 1095 1096 /* init RF-dependent PHY registers, part one */ 1097 for (i = 0; i < __arraycount(phyini_1); i++) { 1098 error = zyd_write16(sc, phyini_1[i].reg, phyini_1[i].val); 1099 if (error != 0) 1100 return error; 1101 } 1102 /* init AL7230B radio, part one */ 1103 for (i = 0; i < __arraycount(rfini_1); i++) { 1104 if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0) 1105 return error; 1106 } 1107 /* init RF-dependent PHY registers, part two */ 1108 for (i = 0; i < __arraycount(phyini_2); i++) { 1109 error = zyd_write16(sc, phyini_2[i].reg, phyini_2[i].val); 1110 if (error != 0) 1111 return error; 1112 } 1113 /* init AL7230B radio, part two */ 1114 for (i = 0; i < __arraycount(rfini_2); i++) { 1115 if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0) 1116 return error; 1117 } 1118 /* init RF-dependent PHY registers, part three */ 1119 for (i = 0; i < __arraycount(phyini_3); i++) { 1120 error = zyd_write16(sc, phyini_3[i].reg, phyini_3[i].val); 1121 if (error != 0) 1122 return error; 1123 } 1124 1125 return 0; 1126 } 1127 1128 Static int 1129 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on) 1130 { 1131 struct zyd_softc *sc = rf->rf_sc; 1132 1133 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04); 1134 (void)zyd_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f); 1135 1136 return 0; 1137 } 1138 1139 Static int 1140 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan) 1141 { 1142 struct zyd_softc *sc = rf->rf_sc; 1143 static const struct { 1144 uint32_t r1, r2; 1145 } rfprog[] = ZYD_AL7230B_CHANTABLE; 1146 static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL; 1147 int error; 1148 size_t i; 1149 1150 (void)zyd_write16(sc, ZYD_CR240, 0x57); 1151 (void)zyd_write16(sc, ZYD_CR251, 0x2f); 1152 1153 for (i = 0; i < __arraycount(rfsc); i++) { 1154 if ((error = zyd_rfwrite(sc, rfsc[i])) != 0) 1155 return error; 1156 } 1157 1158 (void)zyd_write16(sc, ZYD_CR128, 0x14); 1159 (void)zyd_write16(sc, ZYD_CR129, 0x12); 1160 (void)zyd_write16(sc, ZYD_CR130, 0x10); 1161 (void)zyd_write16(sc, ZYD_CR38, 0x38); 1162 (void)zyd_write16(sc, ZYD_CR136, 0xdf); 1163 1164 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 1165 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 1166 (void)zyd_rfwrite(sc, 0x3c9000); 1167 1168 (void)zyd_write16(sc, ZYD_CR251, 0x3f); 1169 (void)zyd_write16(sc, ZYD_CR203, 0x06); 1170 (void)zyd_write16(sc, ZYD_CR240, 0x08); 1171 1172 return 0; 1173 } 1174 1175 /* 1176 * AL2210 RF methods. 1177 */ 1178 Static int 1179 zyd_al2210_init(struct zyd_rf *rf) 1180 { 1181 struct zyd_softc *sc = rf->rf_sc; 1182 static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY; 1183 static const uint32_t rfini[] = ZYD_AL2210_RF; 1184 uint32_t tmp; 1185 int error; 1186 size_t i; 1187 1188 (void)zyd_write32(sc, ZYD_CR18, 2); 1189 1190 /* init RF-dependent PHY registers */ 1191 for (i = 0; i < __arraycount(phyini); i++) { 1192 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1193 if (error != 0) 1194 return error; 1195 } 1196 /* init AL2210 radio */ 1197 for (i = 0; i < __arraycount(rfini); i++) { 1198 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1199 return error; 1200 } 1201 (void)zyd_write16(sc, ZYD_CR47, 0x1e); 1202 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp); 1203 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1); 1204 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1); 1205 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05); 1206 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00); 1207 (void)zyd_write16(sc, ZYD_CR47, 0x1e); 1208 (void)zyd_write32(sc, ZYD_CR18, 3); 1209 1210 return 0; 1211 } 1212 1213 Static int 1214 zyd_al2210_switch_radio(struct zyd_rf *rf, int on) 1215 { 1216 /* vendor driver does nothing for this RF chip */ 1217 1218 return 0; 1219 } 1220 1221 Static int 1222 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan) 1223 { 1224 struct zyd_softc *sc = rf->rf_sc; 1225 static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE; 1226 uint32_t tmp; 1227 1228 (void)zyd_write32(sc, ZYD_CR18, 2); 1229 (void)zyd_write16(sc, ZYD_CR47, 0x1e); 1230 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp); 1231 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1); 1232 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1); 1233 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05); 1234 1235 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00); 1236 (void)zyd_write16(sc, ZYD_CR47, 0x1e); 1237 1238 /* actually set the channel */ 1239 (void)zyd_rfwrite(sc, rfprog[chan - 1]); 1240 1241 (void)zyd_write32(sc, ZYD_CR18, 3); 1242 1243 return 0; 1244 } 1245 1246 /* 1247 * GCT RF methods. 1248 */ 1249 Static int 1250 zyd_gct_init(struct zyd_rf *rf) 1251 { 1252 struct zyd_softc *sc = rf->rf_sc; 1253 static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY; 1254 static const uint32_t rfini[] = ZYD_GCT_RF; 1255 int error; 1256 size_t i; 1257 1258 /* init RF-dependent PHY registers */ 1259 for (i = 0; i < __arraycount(phyini); i++) { 1260 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1261 if (error != 0) 1262 return error; 1263 } 1264 /* init cgt radio */ 1265 for (i = 0; i < __arraycount(rfini); i++) { 1266 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1267 return error; 1268 } 1269 return 0; 1270 } 1271 1272 Static int 1273 zyd_gct_switch_radio(struct zyd_rf *rf, int on) 1274 { 1275 /* vendor driver does nothing for this RF chip */ 1276 1277 return 0; 1278 } 1279 1280 Static int 1281 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan) 1282 { 1283 struct zyd_softc *sc = rf->rf_sc; 1284 static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE; 1285 1286 (void)zyd_rfwrite(sc, 0x1c0000); 1287 (void)zyd_rfwrite(sc, rfprog[chan - 1]); 1288 (void)zyd_rfwrite(sc, 0x1c0008); 1289 1290 return 0; 1291 } 1292 1293 /* 1294 * Maxim RF methods. 1295 */ 1296 Static int 1297 zyd_maxim_init(struct zyd_rf *rf) 1298 { 1299 struct zyd_softc *sc = rf->rf_sc; 1300 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY; 1301 static const uint32_t rfini[] = ZYD_MAXIM_RF; 1302 uint16_t tmp; 1303 int error; 1304 size_t i; 1305 1306 /* init RF-dependent PHY registers */ 1307 for (i = 0; i < __arraycount(phyini); i++) { 1308 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1309 if (error != 0) 1310 return error; 1311 } 1312 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1313 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1314 1315 /* init maxim radio */ 1316 for (i = 0; i < __arraycount(rfini); i++) { 1317 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1318 return error; 1319 } 1320 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1321 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1322 1323 return 0; 1324 } 1325 1326 Static int 1327 zyd_maxim_switch_radio(struct zyd_rf *rf, int on) 1328 { 1329 /* vendor driver does nothing for this RF chip */ 1330 1331 return 0; 1332 } 1333 1334 Static int 1335 zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan) 1336 { 1337 struct zyd_softc *sc = rf->rf_sc; 1338 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY; 1339 static const uint32_t rfini[] = ZYD_MAXIM_RF; 1340 static const struct { 1341 uint32_t r1, r2; 1342 } rfprog[] = ZYD_MAXIM_CHANTABLE; 1343 uint16_t tmp; 1344 int error; 1345 size_t i; 1346 1347 /* 1348 * Do the same as we do when initializing it, except for the channel 1349 * values coming from the two channel tables. 1350 */ 1351 1352 /* init RF-dependent PHY registers */ 1353 for (i = 0; i < __arraycount(phyini); i++) { 1354 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1355 if (error != 0) 1356 return error; 1357 } 1358 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1359 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1360 1361 /* first two values taken from the chantables */ 1362 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 1363 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 1364 1365 /* init maxim radio - skipping the two first values */ 1366 for (i = 2; i < __arraycount(rfini); i++) { 1367 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1368 return error; 1369 } 1370 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1371 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1372 1373 return 0; 1374 } 1375 1376 /* 1377 * Maxim2 RF methods. 1378 */ 1379 Static int 1380 zyd_maxim2_init(struct zyd_rf *rf) 1381 { 1382 struct zyd_softc *sc = rf->rf_sc; 1383 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY; 1384 static const uint32_t rfini[] = ZYD_MAXIM2_RF; 1385 uint16_t tmp; 1386 int error; 1387 size_t i; 1388 1389 /* init RF-dependent PHY registers */ 1390 for (i = 0; i < __arraycount(phyini); i++) { 1391 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1392 if (error != 0) 1393 return error; 1394 } 1395 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1396 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1397 1398 /* init maxim2 radio */ 1399 for (i = 0; i < __arraycount(rfini); i++) { 1400 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1401 return error; 1402 } 1403 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1404 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1405 1406 return 0; 1407 } 1408 1409 Static int 1410 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on) 1411 { 1412 /* vendor driver does nothing for this RF chip */ 1413 1414 return 0; 1415 } 1416 1417 Static int 1418 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan) 1419 { 1420 struct zyd_softc *sc = rf->rf_sc; 1421 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY; 1422 static const uint32_t rfini[] = ZYD_MAXIM2_RF; 1423 static const struct { 1424 uint32_t r1, r2; 1425 } rfprog[] = ZYD_MAXIM2_CHANTABLE; 1426 uint16_t tmp; 1427 int error; 1428 size_t i; 1429 1430 /* 1431 * Do the same as we do when initializing it, except for the channel 1432 * values coming from the two channel tables. 1433 */ 1434 1435 /* init RF-dependent PHY registers */ 1436 for (i = 0; i < __arraycount(phyini); i++) { 1437 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1438 if (error != 0) 1439 return error; 1440 } 1441 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1442 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1443 1444 /* first two values taken from the chantables */ 1445 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 1446 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 1447 1448 /* init maxim2 radio - skipping the two first values */ 1449 for (i = 2; i < __arraycount(rfini); i++) { 1450 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1451 return error; 1452 } 1453 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1454 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1455 1456 return 0; 1457 } 1458 1459 Static int 1460 zyd_rf_attach(struct zyd_softc *sc, uint8_t type) 1461 { 1462 struct zyd_rf *rf = &sc->sc_rf; 1463 1464 rf->rf_sc = sc; 1465 1466 switch (type) { 1467 case ZYD_RF_RFMD: 1468 rf->init = zyd_rfmd_init; 1469 rf->switch_radio = zyd_rfmd_switch_radio; 1470 rf->set_channel = zyd_rfmd_set_channel; 1471 rf->width = 24; /* 24-bit RF values */ 1472 break; 1473 case ZYD_RF_AL2230: 1474 case ZYD_RF_AL2230S: 1475 if (sc->mac_rev == ZYD_ZD1211B) 1476 rf->init = zyd_al2230_init_b; 1477 else 1478 rf->init = zyd_al2230_init; 1479 rf->switch_radio = zyd_al2230_switch_radio; 1480 rf->set_channel = zyd_al2230_set_channel; 1481 rf->width = 24; /* 24-bit RF values */ 1482 break; 1483 case ZYD_RF_AL7230B: 1484 rf->init = zyd_al7230B_init; 1485 rf->switch_radio = zyd_al7230B_switch_radio; 1486 rf->set_channel = zyd_al7230B_set_channel; 1487 rf->width = 24; /* 24-bit RF values */ 1488 break; 1489 case ZYD_RF_AL2210: 1490 rf->init = zyd_al2210_init; 1491 rf->switch_radio = zyd_al2210_switch_radio; 1492 rf->set_channel = zyd_al2210_set_channel; 1493 rf->width = 24; /* 24-bit RF values */ 1494 break; 1495 case ZYD_RF_GCT: 1496 rf->init = zyd_gct_init; 1497 rf->switch_radio = zyd_gct_switch_radio; 1498 rf->set_channel = zyd_gct_set_channel; 1499 rf->width = 21; /* 21-bit RF values */ 1500 break; 1501 case ZYD_RF_MAXIM_NEW: 1502 rf->init = zyd_maxim_init; 1503 rf->switch_radio = zyd_maxim_switch_radio; 1504 rf->set_channel = zyd_maxim_set_channel; 1505 rf->width = 18; /* 18-bit RF values */ 1506 break; 1507 case ZYD_RF_MAXIM_NEW2: 1508 rf->init = zyd_maxim2_init; 1509 rf->switch_radio = zyd_maxim2_switch_radio; 1510 rf->set_channel = zyd_maxim2_set_channel; 1511 rf->width = 18; /* 18-bit RF values */ 1512 break; 1513 default: 1514 printf("%s: sorry, radio \"%s\" is not supported yet\n", 1515 device_xname(sc->sc_dev), zyd_rf_name(type)); 1516 return EINVAL; 1517 } 1518 return 0; 1519 } 1520 1521 Static const char * 1522 zyd_rf_name(uint8_t type) 1523 { 1524 static const char * const zyd_rfs[] = { 1525 "unknown", "unknown", "UW2451", "UCHIP", "AL2230", 1526 "AL7230B", "THETA", "AL2210", "MAXIM_NEW", "GCT", 1527 "AL2230S", "RALINK", "INTERSIL", "RFMD", "MAXIM_NEW2", 1528 "PHILIPS" 1529 }; 1530 1531 return zyd_rfs[(type > 15) ? 0 : type]; 1532 } 1533 1534 Static int 1535 zyd_hw_init(struct zyd_softc *sc) 1536 { 1537 struct zyd_rf *rf = &sc->sc_rf; 1538 const struct zyd_phy_pair *phyp; 1539 int error; 1540 1541 /* specify that the plug and play is finished */ 1542 (void)zyd_write32(sc, ZYD_MAC_AFTER_PNP, 1); 1543 1544 (void)zyd_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->fwbase); 1545 DPRINTF(("firmware base address=0x%04x\n", sc->fwbase)); 1546 1547 /* retrieve firmware revision number */ 1548 (void)zyd_read16(sc, sc->fwbase + ZYD_FW_FIRMWARE_REV, &sc->fw_rev); 1549 1550 (void)zyd_write32(sc, ZYD_CR_GPI_EN, 0); 1551 (void)zyd_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f); 1552 1553 /* disable interrupts */ 1554 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0); 1555 1556 /* PHY init */ 1557 zyd_lock_phy(sc); 1558 phyp = (sc->mac_rev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy; 1559 for (; phyp->reg != 0; phyp++) { 1560 if ((error = zyd_write16(sc, phyp->reg, phyp->val)) != 0) 1561 goto fail; 1562 } 1563 zyd_unlock_phy(sc); 1564 1565 /* HMAC init */ 1566 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020); 1567 zyd_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808); 1568 1569 if (sc->mac_rev == ZYD_ZD1211) { 1570 zyd_write32(sc, ZYD_MAC_RETRY, 0x00000002); 1571 } else { 1572 zyd_write32(sc, ZYD_MAC_RETRY, 0x02020202); 1573 zyd_write32(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f); 1574 zyd_write32(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f); 1575 zyd_write32(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f); 1576 zyd_write32(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f); 1577 zyd_write32(sc, ZYD_MACB_AIFS_CTL1, 0x00280028); 1578 zyd_write32(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C); 1579 zyd_write32(sc, ZYD_MACB_TXOP, 0x01800824); 1580 } 1581 1582 zyd_write32(sc, ZYD_MAC_SNIFFER, 0x00000000); 1583 zyd_write32(sc, ZYD_MAC_RXFILTER, 0x00000000); 1584 zyd_write32(sc, ZYD_MAC_GHTBL, 0x00000000); 1585 zyd_write32(sc, ZYD_MAC_GHTBH, 0x80000000); 1586 zyd_write32(sc, ZYD_MAC_MISC, 0x000000a4); 1587 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f); 1588 zyd_write32(sc, ZYD_MAC_BCNCFG, 0x00f00401); 1589 zyd_write32(sc, ZYD_MAC_PHY_DELAY2, 0x00000000); 1590 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000080); 1591 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000); 1592 zyd_write32(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100); 1593 zyd_write32(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0547c032); 1594 zyd_write32(sc, ZYD_CR_RX_PE_DELAY, 0x00000070); 1595 zyd_write32(sc, ZYD_CR_PS_CTRL, 0x10000000); 1596 zyd_write32(sc, ZYD_MAC_RTSCTSRATE, 0x02030203); 1597 zyd_write32(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640); 1598 zyd_write32(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114); 1599 1600 /* RF chip init */ 1601 zyd_lock_phy(sc); 1602 error = (*rf->init)(rf); 1603 zyd_unlock_phy(sc); 1604 if (error != 0) { 1605 printf("%s: radio initialization failed\n", 1606 device_xname(sc->sc_dev)); 1607 goto fail; 1608 } 1609 1610 /* init beacon interval to 100ms */ 1611 if ((error = zyd_set_beacon_interval(sc, 100)) != 0) 1612 goto fail; 1613 1614 fail: return error; 1615 } 1616 1617 Static int 1618 zyd_read_eeprom(struct zyd_softc *sc) 1619 { 1620 struct ieee80211com *ic = &sc->sc_ic; 1621 uint32_t tmp; 1622 uint16_t val; 1623 int i; 1624 1625 /* read MAC address */ 1626 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P1, &tmp); 1627 ic->ic_myaddr[0] = tmp & 0xff; 1628 ic->ic_myaddr[1] = tmp >> 8; 1629 ic->ic_myaddr[2] = tmp >> 16; 1630 ic->ic_myaddr[3] = tmp >> 24; 1631 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P2, &tmp); 1632 ic->ic_myaddr[4] = tmp & 0xff; 1633 ic->ic_myaddr[5] = tmp >> 8; 1634 1635 (void)zyd_read32(sc, ZYD_EEPROM_POD, &tmp); 1636 sc->rf_rev = tmp & 0x0f; 1637 sc->pa_rev = (tmp >> 16) & 0x0f; 1638 1639 /* read regulatory domain (currently unused) */ 1640 (void)zyd_read32(sc, ZYD_EEPROM_SUBID, &tmp); 1641 sc->regdomain = tmp >> 16; 1642 DPRINTF(("regulatory domain %x\n", sc->regdomain)); 1643 1644 /* read Tx power calibration tables */ 1645 for (i = 0; i < 7; i++) { 1646 (void)zyd_read16(sc, ZYD_EEPROM_PWR_CAL + i, &val); 1647 sc->pwr_cal[i * 2] = val >> 8; 1648 sc->pwr_cal[i * 2 + 1] = val & 0xff; 1649 1650 (void)zyd_read16(sc, ZYD_EEPROM_PWR_INT + i, &val); 1651 sc->pwr_int[i * 2] = val >> 8; 1652 sc->pwr_int[i * 2 + 1] = val & 0xff; 1653 1654 (void)zyd_read16(sc, ZYD_EEPROM_36M_CAL + i, &val); 1655 sc->ofdm36_cal[i * 2] = val >> 8; 1656 sc->ofdm36_cal[i * 2 + 1] = val & 0xff; 1657 1658 (void)zyd_read16(sc, ZYD_EEPROM_48M_CAL + i, &val); 1659 sc->ofdm48_cal[i * 2] = val >> 8; 1660 sc->ofdm48_cal[i * 2 + 1] = val & 0xff; 1661 1662 (void)zyd_read16(sc, ZYD_EEPROM_54M_CAL + i, &val); 1663 sc->ofdm54_cal[i * 2] = val >> 8; 1664 sc->ofdm54_cal[i * 2 + 1] = val & 0xff; 1665 } 1666 return 0; 1667 } 1668 1669 Static int 1670 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr) 1671 { 1672 uint32_t tmp; 1673 1674 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]; 1675 (void)zyd_write32(sc, ZYD_MAC_MACADRL, tmp); 1676 1677 tmp = addr[5] << 8 | addr[4]; 1678 (void)zyd_write32(sc, ZYD_MAC_MACADRH, tmp); 1679 1680 return 0; 1681 } 1682 1683 Static int 1684 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr) 1685 { 1686 uint32_t tmp; 1687 1688 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]; 1689 (void)zyd_write32(sc, ZYD_MAC_BSSADRL, tmp); 1690 1691 tmp = addr[5] << 8 | addr[4]; 1692 (void)zyd_write32(sc, ZYD_MAC_BSSADRH, tmp); 1693 1694 return 0; 1695 } 1696 1697 Static int 1698 zyd_switch_radio(struct zyd_softc *sc, int on) 1699 { 1700 struct zyd_rf *rf = &sc->sc_rf; 1701 int error; 1702 1703 zyd_lock_phy(sc); 1704 error = (*rf->switch_radio)(rf, on); 1705 zyd_unlock_phy(sc); 1706 1707 return error; 1708 } 1709 1710 Static void 1711 zyd_set_led(struct zyd_softc *sc, int which, int on) 1712 { 1713 uint32_t tmp; 1714 1715 (void)zyd_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp); 1716 tmp &= ~which; 1717 if (on) 1718 tmp |= which; 1719 (void)zyd_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp); 1720 } 1721 1722 Static int 1723 zyd_set_rxfilter(struct zyd_softc *sc) 1724 { 1725 uint32_t rxfilter; 1726 1727 switch (sc->sc_ic.ic_opmode) { 1728 case IEEE80211_M_STA: 1729 rxfilter = ZYD_FILTER_BSS; 1730 break; 1731 case IEEE80211_M_IBSS: 1732 case IEEE80211_M_HOSTAP: 1733 rxfilter = ZYD_FILTER_HOSTAP; 1734 break; 1735 case IEEE80211_M_MONITOR: 1736 rxfilter = ZYD_FILTER_MONITOR; 1737 break; 1738 default: 1739 /* should not get there */ 1740 return EINVAL; 1741 } 1742 return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter); 1743 } 1744 1745 Static void 1746 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c) 1747 { 1748 struct ieee80211com *ic = &sc->sc_ic; 1749 struct zyd_rf *rf = &sc->sc_rf; 1750 u_int chan; 1751 1752 chan = ieee80211_chan2ieee(ic, c); 1753 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1754 return; 1755 1756 zyd_lock_phy(sc); 1757 1758 (*rf->set_channel)(rf, chan); 1759 1760 /* update Tx power */ 1761 (void)zyd_write32(sc, ZYD_CR31, sc->pwr_int[chan - 1]); 1762 (void)zyd_write32(sc, ZYD_CR68, sc->pwr_cal[chan - 1]); 1763 1764 if (sc->mac_rev == ZYD_ZD1211B) { 1765 (void)zyd_write32(sc, ZYD_CR67, sc->ofdm36_cal[chan - 1]); 1766 (void)zyd_write32(sc, ZYD_CR66, sc->ofdm48_cal[chan - 1]); 1767 (void)zyd_write32(sc, ZYD_CR65, sc->ofdm54_cal[chan - 1]); 1768 1769 (void)zyd_write32(sc, ZYD_CR69, 0x28); 1770 (void)zyd_write32(sc, ZYD_CR69, 0x2a); 1771 } 1772 1773 zyd_unlock_phy(sc); 1774 } 1775 1776 Static int 1777 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval) 1778 { 1779 /* XXX this is probably broken.. */ 1780 (void)zyd_write32(sc, ZYD_CR_ATIM_WND_PERIOD, bintval - 2); 1781 (void)zyd_write32(sc, ZYD_CR_PRE_TBTT, bintval - 1); 1782 (void)zyd_write32(sc, ZYD_CR_BCN_INTERVAL, bintval); 1783 1784 return 0; 1785 } 1786 1787 Static uint8_t 1788 zyd_plcp_signal(int rate) 1789 { 1790 switch (rate) { 1791 /* CCK rates (returned values are device-dependent) */ 1792 case 2: return 0x0; 1793 case 4: return 0x1; 1794 case 11: return 0x2; 1795 case 22: return 0x3; 1796 1797 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1798 case 12: return 0xb; 1799 case 18: return 0xf; 1800 case 24: return 0xa; 1801 case 36: return 0xe; 1802 case 48: return 0x9; 1803 case 72: return 0xd; 1804 case 96: return 0x8; 1805 case 108: return 0xc; 1806 1807 /* unsupported rates (should not get there) */ 1808 default: return 0xff; 1809 } 1810 } 1811 1812 Static void 1813 zyd_intr(struct usbd_xfer *xfer, void * priv, usbd_status status) 1814 { 1815 struct zyd_softc *sc = (struct zyd_softc *)priv; 1816 struct zyd_cmd *cmd; 1817 uint32_t datalen; 1818 1819 if (status != USBD_NORMAL_COMPLETION) { 1820 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1821 return; 1822 1823 if (status == USBD_STALLED) { 1824 usbd_clear_endpoint_stall_async( 1825 sc->zyd_ep[ZYD_ENDPT_IIN]); 1826 } 1827 return; 1828 } 1829 1830 cmd = (struct zyd_cmd *)sc->ibuf; 1831 1832 if (le16toh(cmd->code) == ZYD_NOTIF_RETRYSTATUS) { 1833 struct zyd_notif_retry *retry = 1834 (struct zyd_notif_retry *)cmd->data; 1835 struct ieee80211com *ic = &sc->sc_ic; 1836 struct ifnet *ifp = &sc->sc_if; 1837 struct ieee80211_node *ni; 1838 1839 DPRINTF(("retry intr: rate=0x%x addr=%s count=%d (0x%x)\n", 1840 le16toh(retry->rate), ether_sprintf(retry->macaddr), 1841 le16toh(retry->count) & 0xff, le16toh(retry->count))); 1842 1843 /* 1844 * Find the node to which the packet was sent and update its 1845 * retry statistics. In BSS mode, this node is the AP we're 1846 * associated to so no lookup is actually needed. 1847 */ 1848 if (ic->ic_opmode != IEEE80211_M_STA) { 1849 ni = ieee80211_find_node(&ic->ic_scan, retry->macaddr); 1850 if (ni == NULL) 1851 return; /* just ignore */ 1852 } else 1853 ni = ic->ic_bss; 1854 1855 ((struct zyd_node *)ni)->amn.amn_retrycnt++; 1856 1857 if (le16toh(retry->count) & 0x100) 1858 ifp->if_oerrors++; /* too many retries */ 1859 1860 } else if (le16toh(cmd->code) == ZYD_NOTIF_IORD) { 1861 struct rq *rqp; 1862 1863 if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT) 1864 return; /* HMAC interrupt */ 1865 1866 usbd_get_xfer_status(xfer, NULL, NULL, &datalen, NULL); 1867 datalen -= sizeof(cmd->code); 1868 datalen -= 2; /* XXX: padding? */ 1869 1870 SIMPLEQ_FOREACH(rqp, &sc->sc_rqh, rq) { 1871 int i; 1872 1873 if (sizeof(struct zyd_pair) * rqp->len != datalen) 1874 continue; 1875 for (i = 0; i < rqp->len; i++) { 1876 if (*(((const uint16_t *)rqp->idata) + i) != 1877 (((struct zyd_pair *)cmd->data) + i)->reg) 1878 break; 1879 } 1880 if (i != rqp->len) 1881 continue; 1882 1883 /* copy answer into caller-supplied buffer */ 1884 memcpy(rqp->odata, cmd->data, 1885 sizeof(struct zyd_pair) * rqp->len); 1886 wakeup(rqp->odata); /* wakeup caller */ 1887 1888 return; 1889 } 1890 return; /* unexpected IORD notification */ 1891 } else { 1892 printf("%s: unknown notification %x\n", device_xname(sc->sc_dev), 1893 le16toh(cmd->code)); 1894 } 1895 } 1896 1897 Static void 1898 zyd_rx_data(struct zyd_softc *sc, const uint8_t *buf, uint16_t len) 1899 { 1900 struct ieee80211com *ic = &sc->sc_ic; 1901 struct ifnet *ifp = &sc->sc_if; 1902 struct ieee80211_node *ni; 1903 struct ieee80211_frame *wh; 1904 const struct zyd_plcphdr *plcp; 1905 const struct zyd_rx_stat *stat; 1906 struct mbuf *m; 1907 int rlen, s; 1908 1909 if (len < ZYD_MIN_FRAGSZ) { 1910 printf("%s: frame too short (length=%d)\n", 1911 device_xname(sc->sc_dev), len); 1912 ifp->if_ierrors++; 1913 return; 1914 } 1915 1916 plcp = (const struct zyd_plcphdr *)buf; 1917 stat = (const struct zyd_rx_stat *) 1918 (buf + len - sizeof(struct zyd_rx_stat)); 1919 1920 if (stat->flags & ZYD_RX_ERROR) { 1921 DPRINTF(("%s: RX status indicated error (%x)\n", 1922 device_xname(sc->sc_dev), stat->flags)); 1923 ifp->if_ierrors++; 1924 return; 1925 } 1926 1927 /* compute actual frame length */ 1928 rlen = len - sizeof(struct zyd_plcphdr) - 1929 sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN; 1930 1931 /* allocate a mbuf to store the frame */ 1932 MGETHDR(m, M_DONTWAIT, MT_DATA); 1933 if (m == NULL) { 1934 printf("%s: could not allocate rx mbuf\n", 1935 device_xname(sc->sc_dev)); 1936 ifp->if_ierrors++; 1937 return; 1938 } 1939 if (rlen > MHLEN) { 1940 MCLGET(m, M_DONTWAIT); 1941 if (!(m->m_flags & M_EXT)) { 1942 printf("%s: could not allocate rx mbuf cluster\n", 1943 device_xname(sc->sc_dev)); 1944 m_freem(m); 1945 ifp->if_ierrors++; 1946 return; 1947 } 1948 } 1949 m_set_rcvif(m, ifp); 1950 m->m_pkthdr.len = m->m_len = rlen; 1951 memcpy(mtod(m, uint8_t *), (const uint8_t *)(plcp + 1), rlen); 1952 1953 s = splnet(); 1954 1955 if (sc->sc_drvbpf != NULL) { 1956 struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap; 1957 static const uint8_t rates[] = { 1958 /* reverse function of zyd_plcp_signal() */ 1959 2, 4, 11, 22, 0, 0, 0, 0, 1960 96, 48, 24, 12, 108, 72, 36, 18 1961 }; 1962 1963 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS; 1964 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); 1965 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 1966 tap->wr_rssi = stat->rssi; 1967 tap->wr_rate = rates[plcp->signal & 0xf]; 1968 1969 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m, BPF_D_IN); 1970 } 1971 1972 wh = mtod(m, struct ieee80211_frame *); 1973 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh); 1974 ieee80211_input(ic, m, ni, stat->rssi, 0); 1975 1976 /* node is no longer needed */ 1977 ieee80211_free_node(ni); 1978 1979 splx(s); 1980 } 1981 1982 Static void 1983 zyd_rxeof(struct usbd_xfer *xfer, void * priv, usbd_status status) 1984 { 1985 struct zyd_rx_data *data = priv; 1986 struct zyd_softc *sc = data->sc; 1987 struct ifnet *ifp = &sc->sc_if; 1988 const struct zyd_rx_desc *desc; 1989 int len; 1990 1991 if (status != USBD_NORMAL_COMPLETION) { 1992 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1993 return; 1994 1995 if (status == USBD_STALLED) 1996 usbd_clear_endpoint_stall(sc->zyd_ep[ZYD_ENDPT_BIN]); 1997 1998 goto skip; 1999 } 2000 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 2001 2002 if (len < ZYD_MIN_RXBUFSZ) { 2003 printf("%s: xfer too short (length=%d)\n", 2004 device_xname(sc->sc_dev), len); 2005 ifp->if_ierrors++; 2006 goto skip; 2007 } 2008 2009 desc = (const struct zyd_rx_desc *) 2010 (data->buf + len - sizeof(struct zyd_rx_desc)); 2011 2012 if (UGETW(desc->tag) == ZYD_TAG_MULTIFRAME) { 2013 const uint8_t *p = data->buf, *end = p + len; 2014 int i; 2015 2016 DPRINTFN(3, ("received multi-frame transfer\n")); 2017 2018 for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) { 2019 const uint16_t len16 = UGETW(desc->len[i]); 2020 2021 if (len16 == 0 || p + len16 > end) 2022 break; 2023 2024 zyd_rx_data(sc, p, len16); 2025 /* next frame is aligned on a 32-bit boundary */ 2026 p += (len16 + 3) & ~3; 2027 } 2028 } else { 2029 DPRINTFN(3, ("received single-frame transfer\n")); 2030 2031 zyd_rx_data(sc, data->buf, len); 2032 } 2033 2034 skip: /* setup a new transfer */ 2035 2036 usbd_setup_xfer(xfer, data, NULL, ZYX_MAX_RXBUFSZ, USBD_SHORT_XFER_OK, 2037 USBD_NO_TIMEOUT, zyd_rxeof); 2038 (void)usbd_transfer(xfer); 2039 } 2040 2041 Static int 2042 zyd_tx_mgt(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 2043 { 2044 struct ieee80211com *ic = &sc->sc_ic; 2045 struct ifnet *ifp = &sc->sc_if; 2046 struct zyd_tx_desc *desc; 2047 struct zyd_tx_data *data; 2048 struct ieee80211_frame *wh; 2049 struct ieee80211_key *k; 2050 int xferlen, totlen, rate; 2051 uint16_t pktlen; 2052 usbd_status error; 2053 2054 data = &sc->tx_data[0]; 2055 desc = (struct zyd_tx_desc *)data->buf; 2056 2057 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2; 2058 2059 wh = mtod(m0, struct ieee80211_frame *); 2060 2061 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 2062 k = ieee80211_crypto_encap(ic, ni, m0); 2063 if (k == NULL) { 2064 m_freem(m0); 2065 return ENOBUFS; 2066 } 2067 } 2068 2069 data->ni = ni; 2070 2071 wh = mtod(m0, struct ieee80211_frame *); 2072 2073 xferlen = sizeof(struct zyd_tx_desc) + m0->m_pkthdr.len; 2074 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN; 2075 2076 /* fill Tx descriptor */ 2077 desc->len = htole16(totlen); 2078 2079 desc->flags = ZYD_TX_FLAG_BACKOFF; 2080 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 2081 /* multicast frames are not sent at OFDM rates in 802.11b/g */ 2082 if (totlen > ic->ic_rtsthreshold) { 2083 desc->flags |= ZYD_TX_FLAG_RTS; 2084 } else if (ZYD_RATE_IS_OFDM(rate) && 2085 (ic->ic_flags & IEEE80211_F_USEPROT)) { 2086 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 2087 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF; 2088 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 2089 desc->flags |= ZYD_TX_FLAG_RTS; 2090 } 2091 } else 2092 desc->flags |= ZYD_TX_FLAG_MULTICAST; 2093 2094 if ((wh->i_fc[0] & 2095 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 2096 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL)) 2097 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL); 2098 2099 desc->phy = zyd_plcp_signal(rate); 2100 if (ZYD_RATE_IS_OFDM(rate)) { 2101 desc->phy |= ZYD_TX_PHY_OFDM; 2102 if (ic->ic_curmode == IEEE80211_MODE_11A) 2103 desc->phy |= ZYD_TX_PHY_5GHZ; 2104 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 2105 desc->phy |= ZYD_TX_PHY_SHPREAMBLE; 2106 2107 /* actual transmit length (XXX why +10?) */ 2108 pktlen = sizeof(struct zyd_tx_desc) + 10; 2109 if (sc->mac_rev == ZYD_ZD1211) 2110 pktlen += totlen; 2111 desc->pktlen = htole16(pktlen); 2112 2113 desc->plcp_length = (16 * totlen + rate - 1) / rate; 2114 desc->plcp_service = 0; 2115 if (rate == 22) { 2116 const int remainder = (16 * totlen) % 22; 2117 if (remainder != 0 && remainder < 7) 2118 desc->plcp_service |= ZYD_PLCP_LENGEXT; 2119 } 2120 2121 if (sc->sc_drvbpf != NULL) { 2122 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap; 2123 2124 tap->wt_flags = 0; 2125 tap->wt_rate = rate; 2126 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 2127 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 2128 2129 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0, BPF_D_OUT); 2130 } 2131 2132 m_copydata(m0, 0, m0->m_pkthdr.len, 2133 data->buf + sizeof(struct zyd_tx_desc)); 2134 2135 DPRINTFN(10, ("%s: sending mgt frame len=%zu rate=%u xferlen=%u\n", 2136 device_xname(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen)); 2137 2138 m_freem(m0); /* mbuf no longer needed */ 2139 2140 usbd_setup_xfer(data->xfer, data, data->buf, xferlen, 2141 USBD_FORCE_SHORT_XFER, ZYD_TX_TIMEOUT, zyd_txeof); 2142 error = usbd_transfer(data->xfer); 2143 if (error != USBD_IN_PROGRESS && error != 0) { 2144 ifp->if_oerrors++; 2145 return EIO; 2146 } 2147 sc->tx_queued++; 2148 2149 return 0; 2150 } 2151 2152 Static void 2153 zyd_txeof(struct usbd_xfer *xfer, void * priv, usbd_status status) 2154 { 2155 struct zyd_tx_data *data = priv; 2156 struct zyd_softc *sc = data->sc; 2157 struct ifnet *ifp = &sc->sc_if; 2158 int s; 2159 2160 if (status != USBD_NORMAL_COMPLETION) { 2161 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 2162 return; 2163 2164 printf("%s: could not transmit buffer: %s\n", 2165 device_xname(sc->sc_dev), usbd_errstr(status)); 2166 2167 if (status == USBD_STALLED) { 2168 usbd_clear_endpoint_stall_async( 2169 sc->zyd_ep[ZYD_ENDPT_BOUT]); 2170 } 2171 ifp->if_oerrors++; 2172 return; 2173 } 2174 2175 s = splnet(); 2176 2177 /* update rate control statistics */ 2178 ((struct zyd_node *)data->ni)->amn.amn_txcnt++; 2179 2180 ieee80211_free_node(data->ni); 2181 data->ni = NULL; 2182 2183 sc->tx_queued--; 2184 ifp->if_opackets++; 2185 2186 sc->tx_timer = 0; 2187 ifp->if_flags &= ~IFF_OACTIVE; 2188 zyd_start(ifp); 2189 2190 splx(s); 2191 } 2192 2193 Static int 2194 zyd_tx_data(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 2195 { 2196 struct ieee80211com *ic = &sc->sc_ic; 2197 struct ifnet *ifp = &sc->sc_if; 2198 struct zyd_tx_desc *desc; 2199 struct zyd_tx_data *data; 2200 struct ieee80211_frame *wh; 2201 struct ieee80211_key *k; 2202 int xferlen, totlen, rate; 2203 uint16_t pktlen; 2204 usbd_status error; 2205 2206 wh = mtod(m0, struct ieee80211_frame *); 2207 2208 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) 2209 rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate]; 2210 else 2211 rate = ni->ni_rates.rs_rates[ni->ni_txrate]; 2212 rate &= IEEE80211_RATE_VAL; 2213 2214 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 2215 k = ieee80211_crypto_encap(ic, ni, m0); 2216 if (k == NULL) { 2217 m_freem(m0); 2218 return ENOBUFS; 2219 } 2220 2221 /* packet header may have moved, reset our local pointer */ 2222 wh = mtod(m0, struct ieee80211_frame *); 2223 } 2224 2225 data = &sc->tx_data[0]; 2226 desc = (struct zyd_tx_desc *)data->buf; 2227 2228 data->ni = ni; 2229 2230 xferlen = sizeof(struct zyd_tx_desc) + m0->m_pkthdr.len; 2231 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN; 2232 2233 /* fill Tx descriptor */ 2234 desc->len = htole16(totlen); 2235 2236 desc->flags = ZYD_TX_FLAG_BACKOFF; 2237 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 2238 /* multicast frames are not sent at OFDM rates in 802.11b/g */ 2239 if (totlen > ic->ic_rtsthreshold) { 2240 desc->flags |= ZYD_TX_FLAG_RTS; 2241 } else if (ZYD_RATE_IS_OFDM(rate) && 2242 (ic->ic_flags & IEEE80211_F_USEPROT)) { 2243 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 2244 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF; 2245 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 2246 desc->flags |= ZYD_TX_FLAG_RTS; 2247 } 2248 } else 2249 desc->flags |= ZYD_TX_FLAG_MULTICAST; 2250 2251 if ((wh->i_fc[0] & 2252 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 2253 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL)) 2254 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL); 2255 2256 desc->phy = zyd_plcp_signal(rate); 2257 if (ZYD_RATE_IS_OFDM(rate)) { 2258 desc->phy |= ZYD_TX_PHY_OFDM; 2259 if (ic->ic_curmode == IEEE80211_MODE_11A) 2260 desc->phy |= ZYD_TX_PHY_5GHZ; 2261 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 2262 desc->phy |= ZYD_TX_PHY_SHPREAMBLE; 2263 2264 /* actual transmit length (XXX why +10?) */ 2265 pktlen = sizeof(struct zyd_tx_desc) + 10; 2266 if (sc->mac_rev == ZYD_ZD1211) 2267 pktlen += totlen; 2268 desc->pktlen = htole16(pktlen); 2269 2270 desc->plcp_length = (16 * totlen + rate - 1) / rate; 2271 desc->plcp_service = 0; 2272 if (rate == 22) { 2273 const int remainder = (16 * totlen) % 22; 2274 if (remainder != 0 && remainder < 7) 2275 desc->plcp_service |= ZYD_PLCP_LENGEXT; 2276 } 2277 2278 if (sc->sc_drvbpf != NULL) { 2279 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap; 2280 2281 tap->wt_flags = 0; 2282 tap->wt_rate = rate; 2283 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 2284 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 2285 2286 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0, BPF_D_OUT); 2287 } 2288 2289 m_copydata(m0, 0, m0->m_pkthdr.len, 2290 data->buf + sizeof(struct zyd_tx_desc)); 2291 2292 DPRINTFN(10, ("%s: sending data frame len=%zu rate=%u xferlen=%u\n", 2293 device_xname(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen)); 2294 2295 m_freem(m0); /* mbuf no longer needed */ 2296 2297 usbd_setup_xfer(data->xfer, data, data->buf, xferlen, 2298 USBD_FORCE_SHORT_XFER, ZYD_TX_TIMEOUT, zyd_txeof); 2299 error = usbd_transfer(data->xfer); 2300 if (error != USBD_IN_PROGRESS && error != 0) { 2301 ifp->if_oerrors++; 2302 return EIO; 2303 } 2304 sc->tx_queued++; 2305 2306 return 0; 2307 } 2308 2309 Static void 2310 zyd_start(struct ifnet *ifp) 2311 { 2312 struct zyd_softc *sc = ifp->if_softc; 2313 struct ieee80211com *ic = &sc->sc_ic; 2314 struct ether_header *eh; 2315 struct ieee80211_node *ni; 2316 struct mbuf *m0; 2317 2318 for (;;) { 2319 IF_POLL(&ic->ic_mgtq, m0); 2320 if (m0 != NULL) { 2321 if (sc->tx_queued >= ZYD_TX_LIST_CNT) { 2322 ifp->if_flags |= IFF_OACTIVE; 2323 break; 2324 } 2325 IF_DEQUEUE(&ic->ic_mgtq, m0); 2326 2327 ni = M_GETCTX(m0, struct ieee80211_node *); 2328 M_CLEARCTX(m0); 2329 bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT); 2330 if (zyd_tx_mgt(sc, m0, ni) != 0) 2331 break; 2332 } else { 2333 if (ic->ic_state != IEEE80211_S_RUN) 2334 break; 2335 IFQ_POLL(&ifp->if_snd, m0); 2336 if (m0 == NULL) 2337 break; 2338 if (sc->tx_queued >= ZYD_TX_LIST_CNT) { 2339 ifp->if_flags |= IFF_OACTIVE; 2340 break; 2341 } 2342 IFQ_DEQUEUE(&ifp->if_snd, m0); 2343 2344 if (m0->m_len < sizeof(struct ether_header) && 2345 !(m0 = m_pullup(m0, sizeof(struct ether_header)))) 2346 continue; 2347 2348 eh = mtod(m0, struct ether_header *); 2349 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 2350 if (ni == NULL) { 2351 m_freem(m0); 2352 continue; 2353 } 2354 bpf_mtap(ifp, m0, BPF_D_OUT); 2355 if ((m0 = ieee80211_encap(ic, m0, ni)) == NULL) { 2356 ieee80211_free_node(ni); 2357 ifp->if_oerrors++; 2358 continue; 2359 } 2360 bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT); 2361 if (zyd_tx_data(sc, m0, ni) != 0) { 2362 ieee80211_free_node(ni); 2363 ifp->if_oerrors++; 2364 break; 2365 } 2366 } 2367 2368 sc->tx_timer = 5; 2369 ifp->if_timer = 1; 2370 } 2371 } 2372 2373 Static void 2374 zyd_watchdog(struct ifnet *ifp) 2375 { 2376 struct zyd_softc *sc = ifp->if_softc; 2377 struct ieee80211com *ic = &sc->sc_ic; 2378 2379 ifp->if_timer = 0; 2380 2381 if (sc->tx_timer > 0) { 2382 if (--sc->tx_timer == 0) { 2383 printf("%s: device timeout\n", device_xname(sc->sc_dev)); 2384 /* zyd_init(ifp); XXX needs a process context ? */ 2385 ifp->if_oerrors++; 2386 return; 2387 } 2388 ifp->if_timer = 1; 2389 } 2390 2391 ieee80211_watchdog(ic); 2392 } 2393 2394 Static int 2395 zyd_ioctl(struct ifnet *ifp, u_long cmd, void *data) 2396 { 2397 struct zyd_softc *sc = ifp->if_softc; 2398 struct ieee80211com *ic = &sc->sc_ic; 2399 int s, error = 0; 2400 2401 s = splnet(); 2402 2403 switch (cmd) { 2404 case SIOCSIFFLAGS: 2405 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 2406 break; 2407 /* XXX re-use ether_ioctl() */ 2408 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { 2409 case IFF_UP: 2410 zyd_init(ifp); 2411 break; 2412 case IFF_RUNNING: 2413 zyd_stop(ifp, 1); 2414 break; 2415 default: 2416 break; 2417 } 2418 break; 2419 2420 default: 2421 error = ieee80211_ioctl(ic, cmd, data); 2422 } 2423 2424 if (error == ENETRESET) { 2425 if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) == 2426 (IFF_RUNNING | IFF_UP)) 2427 zyd_init(ifp); 2428 error = 0; 2429 } 2430 2431 splx(s); 2432 2433 return error; 2434 } 2435 2436 Static int 2437 zyd_init(struct ifnet *ifp) 2438 { 2439 struct zyd_softc *sc = ifp->if_softc; 2440 struct ieee80211com *ic = &sc->sc_ic; 2441 int i, error; 2442 2443 zyd_stop(ifp, 0); 2444 2445 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl)); 2446 DPRINTF(("setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr))); 2447 error = zyd_set_macaddr(sc, ic->ic_myaddr); 2448 if (error != 0) 2449 return error; 2450 2451 /* we'll do software WEP decryption for now */ 2452 DPRINTF(("setting encryption type\n")); 2453 error = zyd_write32(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER); 2454 if (error != 0) 2455 return error; 2456 2457 /* promiscuous mode */ 2458 (void)zyd_write32(sc, ZYD_MAC_SNIFFER, 2459 (ic->ic_opmode == IEEE80211_M_MONITOR) ? 1 : 0); 2460 2461 (void)zyd_set_rxfilter(sc); 2462 2463 /* switch radio transmitter ON */ 2464 (void)zyd_switch_radio(sc, 1); 2465 2466 /* set basic rates */ 2467 if (ic->ic_curmode == IEEE80211_MODE_11B) 2468 (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x0003); 2469 else if (ic->ic_curmode == IEEE80211_MODE_11A) 2470 (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x1500); 2471 else /* assumes 802.11b/g */ 2472 (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x000f); 2473 2474 /* set mandatory rates */ 2475 if (ic->ic_curmode == IEEE80211_MODE_11B) 2476 (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x000f); 2477 else if (ic->ic_curmode == IEEE80211_MODE_11A) 2478 (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x1500); 2479 else /* assumes 802.11b/g */ 2480 (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x150f); 2481 2482 /* set default BSS channel */ 2483 ic->ic_bss->ni_chan = ic->ic_ibss_chan; 2484 zyd_set_chan(sc, ic->ic_bss->ni_chan); 2485 2486 /* enable interrupts */ 2487 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK); 2488 2489 /* 2490 * Allocate Tx and Rx xfer queues. 2491 */ 2492 if ((error = zyd_alloc_tx_list(sc)) != 0) { 2493 printf("%s: could not allocate Tx list\n", 2494 device_xname(sc->sc_dev)); 2495 goto fail; 2496 } 2497 if ((error = zyd_alloc_rx_list(sc)) != 0) { 2498 printf("%s: could not allocate Rx list\n", 2499 device_xname(sc->sc_dev)); 2500 goto fail; 2501 } 2502 2503 /* 2504 * Start up the receive pipe. 2505 */ 2506 for (i = 0; i < ZYD_RX_LIST_CNT; i++) { 2507 struct zyd_rx_data *data = &sc->rx_data[i]; 2508 2509 usbd_setup_xfer(data->xfer, data, NULL, ZYX_MAX_RXBUFSZ, 2510 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, zyd_rxeof); 2511 error = usbd_transfer(data->xfer); 2512 if (error != USBD_IN_PROGRESS && error != 0) { 2513 printf("%s: could not queue Rx transfer\n", 2514 device_xname(sc->sc_dev)); 2515 goto fail; 2516 } 2517 } 2518 2519 ifp->if_flags &= ~IFF_OACTIVE; 2520 ifp->if_flags |= IFF_RUNNING; 2521 2522 if (ic->ic_opmode == IEEE80211_M_MONITOR) 2523 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 2524 else 2525 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2526 2527 return 0; 2528 2529 fail: zyd_stop(ifp, 1); 2530 return error; 2531 } 2532 2533 Static void 2534 zyd_stop(struct ifnet *ifp, int disable) 2535 { 2536 struct zyd_softc *sc = ifp->if_softc; 2537 struct ieee80211com *ic = &sc->sc_ic; 2538 2539 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */ 2540 2541 sc->tx_timer = 0; 2542 ifp->if_timer = 0; 2543 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2544 2545 /* switch radio transmitter OFF */ 2546 (void)zyd_switch_radio(sc, 0); 2547 2548 /* disable Rx */ 2549 (void)zyd_write32(sc, ZYD_MAC_RXFILTER, 0); 2550 2551 /* disable interrupts */ 2552 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0); 2553 2554 usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BIN]); 2555 usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BOUT]); 2556 2557 zyd_free_rx_list(sc); 2558 zyd_free_tx_list(sc); 2559 } 2560 2561 Static int 2562 zyd_loadfirmware(struct zyd_softc *sc, u_char *fw, size_t size) 2563 { 2564 usb_device_request_t req; 2565 uint16_t addr; 2566 uint8_t stat; 2567 2568 DPRINTF(("firmware size=%zu\n", size)); 2569 2570 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 2571 req.bRequest = ZYD_DOWNLOADREQ; 2572 USETW(req.wIndex, 0); 2573 2574 addr = ZYD_FIRMWARE_START_ADDR; 2575 while (size > 0) { 2576 #if 0 2577 const int mlen = uimin(size, 4096); 2578 #else 2579 /* 2580 * XXXX: When the transfer size is 4096 bytes, it is not 2581 * likely to be able to transfer it. 2582 * The cause is port or machine or chip? 2583 */ 2584 const int mlen = uimin(size, 64); 2585 #endif 2586 2587 DPRINTF(("loading firmware block: len=%d, addr=0x%x\n", mlen, 2588 addr)); 2589 2590 USETW(req.wValue, addr); 2591 USETW(req.wLength, mlen); 2592 if (usbd_do_request(sc->sc_udev, &req, fw) != 0) 2593 return EIO; 2594 2595 addr += mlen / 2; 2596 fw += mlen; 2597 size -= mlen; 2598 } 2599 2600 /* check whether the upload succeeded */ 2601 req.bmRequestType = UT_READ_VENDOR_DEVICE; 2602 req.bRequest = ZYD_DOWNLOADSTS; 2603 USETW(req.wValue, 0); 2604 USETW(req.wIndex, 0); 2605 USETW(req.wLength, sizeof(stat)); 2606 if (usbd_do_request(sc->sc_udev, &req, &stat) != 0) 2607 return EIO; 2608 2609 return (stat & 0x80) ? EIO : 0; 2610 } 2611 2612 Static void 2613 zyd_iter_func(void *arg, struct ieee80211_node *ni) 2614 { 2615 struct zyd_softc *sc = arg; 2616 struct zyd_node *zn = (struct zyd_node *)ni; 2617 2618 ieee80211_amrr_choose(&sc->amrr, ni, &zn->amn); 2619 } 2620 2621 Static void 2622 zyd_amrr_timeout(void *arg) 2623 { 2624 struct zyd_softc *sc = arg; 2625 struct ieee80211com *ic = &sc->sc_ic; 2626 int s; 2627 2628 s = splnet(); 2629 if (ic->ic_opmode == IEEE80211_M_STA) 2630 zyd_iter_func(sc, ic->ic_bss); 2631 else 2632 ieee80211_iterate_nodes(&ic->ic_sta, zyd_iter_func, sc); 2633 splx(s); 2634 2635 callout_reset(&sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc); 2636 } 2637 2638 Static void 2639 zyd_newassoc(struct ieee80211_node *ni, int isnew) 2640 { 2641 struct zyd_softc *sc = ni->ni_ic->ic_ifp->if_softc; 2642 int i; 2643 2644 ieee80211_amrr_node_init(&sc->amrr, &((struct zyd_node *)ni)->amn); 2645 2646 /* set rate to some reasonable initial value */ 2647 for (i = ni->ni_rates.rs_nrates - 1; 2648 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72; 2649 i--); 2650 ni->ni_txrate = i; 2651 } 2652 2653 int 2654 zyd_activate(device_t self, enum devact act) 2655 { 2656 struct zyd_softc *sc = device_private(self); 2657 2658 switch (act) { 2659 case DVACT_DEACTIVATE: 2660 if_deactivate(&sc->sc_if); 2661 return 0; 2662 default: 2663 return EOPNOTSUPP; 2664 } 2665 } 2666