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