1 /* $OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $ */ 2 /* $NetBSD: if_zyd.c,v 1.48 2018/06/26 06:48:02 msaitoh 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.48 2018/06/26 06:48:02 msaitoh 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 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 usb_rem_task(sc->sc_udev, &sc->sc_task); 470 callout_stop(&sc->sc_scan_ch); 471 callout_stop(&sc->sc_amrr_ch); 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 usb_rem_task(sc->sc_udev, &sc->sc_task); 765 callout_stop(&sc->sc_scan_ch); 766 callout_stop(&sc->sc_amrr_ch); 767 768 /* do it in a process context */ 769 sc->sc_state = nstate; 770 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER); 771 772 return 0; 773 } 774 775 Static int 776 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen, 777 void *odata, int olen, u_int flags) 778 { 779 struct usbd_xfer *xfer; 780 struct zyd_cmd cmd; 781 struct rq rq; 782 uint16_t xferflags; 783 int error; 784 usbd_status uerror; 785 int s = 0; 786 787 error = usbd_create_xfer(sc->zyd_ep[ZYD_ENDPT_IOUT], 788 sizeof(uint16_t) + ilen, USBD_FORCE_SHORT_XFER, 0, &xfer); 789 if (error) 790 return error; 791 792 cmd.code = htole16(code); 793 memcpy(cmd.data, idata, ilen); 794 795 xferflags = USBD_FORCE_SHORT_XFER; 796 if (!(flags & ZYD_CMD_FLAG_READ)) 797 xferflags |= USBD_SYNCHRONOUS; 798 else { 799 s = splusb(); 800 rq.idata = idata; 801 rq.odata = odata; 802 rq.len = olen / sizeof(struct zyd_pair); 803 SIMPLEQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq); 804 } 805 806 usbd_setup_xfer(xfer, 0, &cmd, sizeof(uint16_t) + ilen, xferflags, 807 ZYD_INTR_TIMEOUT, NULL); 808 uerror = usbd_transfer(xfer); 809 if (uerror != USBD_IN_PROGRESS && uerror != 0) { 810 if (flags & ZYD_CMD_FLAG_READ) 811 splx(s); 812 printf("%s: could not send command (error=%s)\n", 813 device_xname(sc->sc_dev), usbd_errstr(uerror)); 814 (void)usbd_destroy_xfer(xfer); 815 return EIO; 816 } 817 if (!(flags & ZYD_CMD_FLAG_READ)) { 818 (void)usbd_destroy_xfer(xfer); 819 return 0; /* write: don't wait for reply */ 820 } 821 /* wait at most one second for command reply */ 822 error = tsleep(odata, PCATCH, "zydcmd", hz); 823 if (error == EWOULDBLOCK) 824 printf("%s: zyd_read sleep timeout\n", device_xname(sc->sc_dev)); 825 SIMPLEQ_REMOVE(&sc->sc_rqh, &rq, rq, rq); 826 splx(s); 827 828 (void)usbd_destroy_xfer(xfer); 829 return error; 830 } 831 832 Static int 833 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val) 834 { 835 struct zyd_pair tmp; 836 int error; 837 838 reg = htole16(reg); 839 error = zyd_cmd(sc, ZYD_CMD_IORD, ®, sizeof(reg), &tmp, sizeof(tmp), 840 ZYD_CMD_FLAG_READ); 841 if (error == 0) 842 *val = le16toh(tmp.val); 843 else 844 *val = 0; 845 return error; 846 } 847 848 Static int 849 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val) 850 { 851 struct zyd_pair tmp[2]; 852 uint16_t regs[2]; 853 int error; 854 855 regs[0] = htole16(ZYD_REG32_HI(reg)); 856 regs[1] = htole16(ZYD_REG32_LO(reg)); 857 error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp), 858 ZYD_CMD_FLAG_READ); 859 if (error == 0) 860 *val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val); 861 else 862 *val = 0; 863 return error; 864 } 865 866 Static int 867 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val) 868 { 869 struct zyd_pair pair; 870 871 pair.reg = htole16(reg); 872 pair.val = htole16(val); 873 874 return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0); 875 } 876 877 Static int 878 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val) 879 { 880 struct zyd_pair pair[2]; 881 882 pair[0].reg = htole16(ZYD_REG32_HI(reg)); 883 pair[0].val = htole16(val >> 16); 884 pair[1].reg = htole16(ZYD_REG32_LO(reg)); 885 pair[1].val = htole16(val & 0xffff); 886 887 return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0); 888 } 889 890 Static int 891 zyd_rfwrite(struct zyd_softc *sc, uint32_t val) 892 { 893 struct zyd_rf *rf = &sc->sc_rf; 894 struct zyd_rfwrite req; 895 uint16_t cr203; 896 int i; 897 898 (void)zyd_read16(sc, ZYD_CR203, &cr203); 899 cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA); 900 901 req.code = htole16(2); 902 req.width = htole16(rf->width); 903 for (i = 0; i < rf->width; i++) { 904 req.bit[i] = htole16(cr203); 905 if (val & (1 << (rf->width - 1 - i))) 906 req.bit[i] |= htole16(ZYD_RF_DATA); 907 } 908 return zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0); 909 } 910 911 Static void 912 zyd_lock_phy(struct zyd_softc *sc) 913 { 914 uint32_t tmp; 915 916 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp); 917 tmp &= ~ZYD_UNLOCK_PHY_REGS; 918 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp); 919 } 920 921 Static void 922 zyd_unlock_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 /* 932 * RFMD RF methods. 933 */ 934 Static int 935 zyd_rfmd_init(struct zyd_rf *rf) 936 { 937 struct zyd_softc *sc = rf->rf_sc; 938 static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY; 939 static const uint32_t rfini[] = ZYD_RFMD_RF; 940 int error; 941 size_t i; 942 943 /* init RF-dependent PHY registers */ 944 for (i = 0; i < __arraycount(phyini); i++) { 945 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 946 if (error != 0) 947 return error; 948 } 949 950 /* init RFMD radio */ 951 for (i = 0; i < __arraycount(rfini); i++) { 952 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 953 return error; 954 } 955 return 0; 956 } 957 958 Static int 959 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on) 960 { 961 struct zyd_softc *sc = rf->rf_sc; 962 963 (void)zyd_write16(sc, ZYD_CR10, on ? 0x89 : 0x15); 964 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x81); 965 966 return 0; 967 } 968 969 Static int 970 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan) 971 { 972 struct zyd_softc *sc = rf->rf_sc; 973 static const struct { 974 uint32_t r1, r2; 975 } rfprog[] = ZYD_RFMD_CHANTABLE; 976 977 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 978 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 979 980 return 0; 981 } 982 983 /* 984 * AL2230 RF methods. 985 */ 986 Static int 987 zyd_al2230_init(struct zyd_rf *rf) 988 { 989 struct zyd_softc *sc = rf->rf_sc; 990 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY; 991 static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT; 992 static const uint32_t rfini[] = ZYD_AL2230_RF; 993 int error; 994 size_t i; 995 996 /* init RF-dependent PHY registers */ 997 for (i = 0; i < __arraycount(phyini); i++) { 998 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 999 if (error != 0) 1000 return error; 1001 } 1002 1003 if (sc->rf_rev == ZYD_RF_AL2230S) { 1004 for (i = 0; i < __arraycount(phy2230s); i++) { 1005 error = zyd_write16(sc, phy2230s[i].reg, 1006 phy2230s[i].val); 1007 if (error != 0) 1008 return error; 1009 } 1010 } 1011 1012 /* init AL2230 radio */ 1013 for (i = 0; i < __arraycount(rfini); i++) { 1014 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1015 return error; 1016 } 1017 return 0; 1018 } 1019 1020 Static int 1021 zyd_al2230_init_b(struct zyd_rf *rf) 1022 { 1023 struct zyd_softc *sc = rf->rf_sc; 1024 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B; 1025 static const uint32_t rfini[] = ZYD_AL2230_RF_B; 1026 int error; 1027 size_t i; 1028 1029 /* init RF-dependent PHY registers */ 1030 for (i = 0; i < __arraycount(phyini); i++) { 1031 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1032 if (error != 0) 1033 return error; 1034 } 1035 1036 /* init AL2230 radio */ 1037 for (i = 0; i < __arraycount(rfini); i++) { 1038 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1039 return error; 1040 } 1041 return 0; 1042 } 1043 1044 Static int 1045 zyd_al2230_switch_radio(struct zyd_rf *rf, int on) 1046 { 1047 struct zyd_softc *sc = rf->rf_sc; 1048 int on251 = (sc->mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f; 1049 1050 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04); 1051 (void)zyd_write16(sc, ZYD_CR251, on ? on251 : 0x2f); 1052 1053 return 0; 1054 } 1055 1056 Static int 1057 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan) 1058 { 1059 struct zyd_softc *sc = rf->rf_sc; 1060 static const struct { 1061 uint32_t r1, r2, r3; 1062 } rfprog[] = ZYD_AL2230_CHANTABLE; 1063 1064 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 1065 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 1066 (void)zyd_rfwrite(sc, rfprog[chan - 1].r3); 1067 1068 (void)zyd_write16(sc, ZYD_CR138, 0x28); 1069 (void)zyd_write16(sc, ZYD_CR203, 0x06); 1070 1071 return 0; 1072 } 1073 1074 /* 1075 * AL7230B RF methods. 1076 */ 1077 Static int 1078 zyd_al7230B_init(struct zyd_rf *rf) 1079 { 1080 struct zyd_softc *sc = rf->rf_sc; 1081 static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1; 1082 static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2; 1083 static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3; 1084 static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1; 1085 static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2; 1086 int error; 1087 size_t i; 1088 1089 /* for AL7230B, PHY and RF need to be initialized in "phases" */ 1090 1091 /* init RF-dependent PHY registers, part one */ 1092 for (i = 0; i < __arraycount(phyini_1); i++) { 1093 error = zyd_write16(sc, phyini_1[i].reg, phyini_1[i].val); 1094 if (error != 0) 1095 return error; 1096 } 1097 /* init AL7230B radio, part one */ 1098 for (i = 0; i < __arraycount(rfini_1); i++) { 1099 if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0) 1100 return error; 1101 } 1102 /* init RF-dependent PHY registers, part two */ 1103 for (i = 0; i < __arraycount(phyini_2); i++) { 1104 error = zyd_write16(sc, phyini_2[i].reg, phyini_2[i].val); 1105 if (error != 0) 1106 return error; 1107 } 1108 /* init AL7230B radio, part two */ 1109 for (i = 0; i < __arraycount(rfini_2); i++) { 1110 if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0) 1111 return error; 1112 } 1113 /* init RF-dependent PHY registers, part three */ 1114 for (i = 0; i < __arraycount(phyini_3); i++) { 1115 error = zyd_write16(sc, phyini_3[i].reg, phyini_3[i].val); 1116 if (error != 0) 1117 return error; 1118 } 1119 1120 return 0; 1121 } 1122 1123 Static int 1124 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on) 1125 { 1126 struct zyd_softc *sc = rf->rf_sc; 1127 1128 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04); 1129 (void)zyd_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f); 1130 1131 return 0; 1132 } 1133 1134 Static int 1135 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan) 1136 { 1137 struct zyd_softc *sc = rf->rf_sc; 1138 static const struct { 1139 uint32_t r1, r2; 1140 } rfprog[] = ZYD_AL7230B_CHANTABLE; 1141 static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL; 1142 int error; 1143 size_t i; 1144 1145 (void)zyd_write16(sc, ZYD_CR240, 0x57); 1146 (void)zyd_write16(sc, ZYD_CR251, 0x2f); 1147 1148 for (i = 0; i < __arraycount(rfsc); i++) { 1149 if ((error = zyd_rfwrite(sc, rfsc[i])) != 0) 1150 return error; 1151 } 1152 1153 (void)zyd_write16(sc, ZYD_CR128, 0x14); 1154 (void)zyd_write16(sc, ZYD_CR129, 0x12); 1155 (void)zyd_write16(sc, ZYD_CR130, 0x10); 1156 (void)zyd_write16(sc, ZYD_CR38, 0x38); 1157 (void)zyd_write16(sc, ZYD_CR136, 0xdf); 1158 1159 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 1160 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 1161 (void)zyd_rfwrite(sc, 0x3c9000); 1162 1163 (void)zyd_write16(sc, ZYD_CR251, 0x3f); 1164 (void)zyd_write16(sc, ZYD_CR203, 0x06); 1165 (void)zyd_write16(sc, ZYD_CR240, 0x08); 1166 1167 return 0; 1168 } 1169 1170 /* 1171 * AL2210 RF methods. 1172 */ 1173 Static int 1174 zyd_al2210_init(struct zyd_rf *rf) 1175 { 1176 struct zyd_softc *sc = rf->rf_sc; 1177 static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY; 1178 static const uint32_t rfini[] = ZYD_AL2210_RF; 1179 uint32_t tmp; 1180 int error; 1181 size_t i; 1182 1183 (void)zyd_write32(sc, ZYD_CR18, 2); 1184 1185 /* init RF-dependent PHY registers */ 1186 for (i = 0; i < __arraycount(phyini); i++) { 1187 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1188 if (error != 0) 1189 return error; 1190 } 1191 /* init AL2210 radio */ 1192 for (i = 0; i < __arraycount(rfini); i++) { 1193 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1194 return error; 1195 } 1196 (void)zyd_write16(sc, ZYD_CR47, 0x1e); 1197 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp); 1198 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1); 1199 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1); 1200 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05); 1201 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00); 1202 (void)zyd_write16(sc, ZYD_CR47, 0x1e); 1203 (void)zyd_write32(sc, ZYD_CR18, 3); 1204 1205 return 0; 1206 } 1207 1208 Static int 1209 zyd_al2210_switch_radio(struct zyd_rf *rf, int on) 1210 { 1211 /* vendor driver does nothing for this RF chip */ 1212 1213 return 0; 1214 } 1215 1216 Static int 1217 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan) 1218 { 1219 struct zyd_softc *sc = rf->rf_sc; 1220 static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE; 1221 uint32_t tmp; 1222 1223 (void)zyd_write32(sc, ZYD_CR18, 2); 1224 (void)zyd_write16(sc, ZYD_CR47, 0x1e); 1225 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp); 1226 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1); 1227 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1); 1228 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05); 1229 1230 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00); 1231 (void)zyd_write16(sc, ZYD_CR47, 0x1e); 1232 1233 /* actually set the channel */ 1234 (void)zyd_rfwrite(sc, rfprog[chan - 1]); 1235 1236 (void)zyd_write32(sc, ZYD_CR18, 3); 1237 1238 return 0; 1239 } 1240 1241 /* 1242 * GCT RF methods. 1243 */ 1244 Static int 1245 zyd_gct_init(struct zyd_rf *rf) 1246 { 1247 struct zyd_softc *sc = rf->rf_sc; 1248 static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY; 1249 static const uint32_t rfini[] = ZYD_GCT_RF; 1250 int error; 1251 size_t i; 1252 1253 /* init RF-dependent PHY registers */ 1254 for (i = 0; i < __arraycount(phyini); i++) { 1255 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1256 if (error != 0) 1257 return error; 1258 } 1259 /* init cgt radio */ 1260 for (i = 0; i < __arraycount(rfini); i++) { 1261 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1262 return error; 1263 } 1264 return 0; 1265 } 1266 1267 Static int 1268 zyd_gct_switch_radio(struct zyd_rf *rf, int on) 1269 { 1270 /* vendor driver does nothing for this RF chip */ 1271 1272 return 0; 1273 } 1274 1275 Static int 1276 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan) 1277 { 1278 struct zyd_softc *sc = rf->rf_sc; 1279 static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE; 1280 1281 (void)zyd_rfwrite(sc, 0x1c0000); 1282 (void)zyd_rfwrite(sc, rfprog[chan - 1]); 1283 (void)zyd_rfwrite(sc, 0x1c0008); 1284 1285 return 0; 1286 } 1287 1288 /* 1289 * Maxim RF methods. 1290 */ 1291 Static int 1292 zyd_maxim_init(struct zyd_rf *rf) 1293 { 1294 struct zyd_softc *sc = rf->rf_sc; 1295 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY; 1296 static const uint32_t rfini[] = ZYD_MAXIM_RF; 1297 uint16_t tmp; 1298 int error; 1299 size_t i; 1300 1301 /* init RF-dependent PHY registers */ 1302 for (i = 0; i < __arraycount(phyini); i++) { 1303 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1304 if (error != 0) 1305 return error; 1306 } 1307 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1308 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1309 1310 /* init maxim radio */ 1311 for (i = 0; i < __arraycount(rfini); i++) { 1312 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1313 return error; 1314 } 1315 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1316 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1317 1318 return 0; 1319 } 1320 1321 Static int 1322 zyd_maxim_switch_radio(struct zyd_rf *rf, int on) 1323 { 1324 /* vendor driver does nothing for this RF chip */ 1325 1326 return 0; 1327 } 1328 1329 Static int 1330 zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan) 1331 { 1332 struct zyd_softc *sc = rf->rf_sc; 1333 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY; 1334 static const uint32_t rfini[] = ZYD_MAXIM_RF; 1335 static const struct { 1336 uint32_t r1, r2; 1337 } rfprog[] = ZYD_MAXIM_CHANTABLE; 1338 uint16_t tmp; 1339 int error; 1340 size_t i; 1341 1342 /* 1343 * Do the same as we do when initializing it, except for the channel 1344 * values coming from the two channel tables. 1345 */ 1346 1347 /* init RF-dependent PHY registers */ 1348 for (i = 0; i < __arraycount(phyini); i++) { 1349 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1350 if (error != 0) 1351 return error; 1352 } 1353 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1354 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1355 1356 /* first two values taken from the chantables */ 1357 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 1358 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 1359 1360 /* init maxim radio - skipping the two first values */ 1361 for (i = 2; i < __arraycount(rfini); i++) { 1362 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1363 return error; 1364 } 1365 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1366 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1367 1368 return 0; 1369 } 1370 1371 /* 1372 * Maxim2 RF methods. 1373 */ 1374 Static int 1375 zyd_maxim2_init(struct zyd_rf *rf) 1376 { 1377 struct zyd_softc *sc = rf->rf_sc; 1378 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY; 1379 static const uint32_t rfini[] = ZYD_MAXIM2_RF; 1380 uint16_t tmp; 1381 int error; 1382 size_t i; 1383 1384 /* init RF-dependent PHY registers */ 1385 for (i = 0; i < __arraycount(phyini); i++) { 1386 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1387 if (error != 0) 1388 return error; 1389 } 1390 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1391 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1392 1393 /* init maxim2 radio */ 1394 for (i = 0; i < __arraycount(rfini); i++) { 1395 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1396 return error; 1397 } 1398 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1399 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1400 1401 return 0; 1402 } 1403 1404 Static int 1405 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on) 1406 { 1407 /* vendor driver does nothing for this RF chip */ 1408 1409 return 0; 1410 } 1411 1412 Static int 1413 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan) 1414 { 1415 struct zyd_softc *sc = rf->rf_sc; 1416 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY; 1417 static const uint32_t rfini[] = ZYD_MAXIM2_RF; 1418 static const struct { 1419 uint32_t r1, r2; 1420 } rfprog[] = ZYD_MAXIM2_CHANTABLE; 1421 uint16_t tmp; 1422 int error; 1423 size_t i; 1424 1425 /* 1426 * Do the same as we do when initializing it, except for the channel 1427 * values coming from the two channel tables. 1428 */ 1429 1430 /* init RF-dependent PHY registers */ 1431 for (i = 0; i < __arraycount(phyini); i++) { 1432 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1433 if (error != 0) 1434 return error; 1435 } 1436 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1437 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1438 1439 /* first two values taken from the chantables */ 1440 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 1441 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 1442 1443 /* init maxim2 radio - skipping the two first values */ 1444 for (i = 2; i < __arraycount(rfini); i++) { 1445 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1446 return error; 1447 } 1448 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1449 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1450 1451 return 0; 1452 } 1453 1454 Static int 1455 zyd_rf_attach(struct zyd_softc *sc, uint8_t type) 1456 { 1457 struct zyd_rf *rf = &sc->sc_rf; 1458 1459 rf->rf_sc = sc; 1460 1461 switch (type) { 1462 case ZYD_RF_RFMD: 1463 rf->init = zyd_rfmd_init; 1464 rf->switch_radio = zyd_rfmd_switch_radio; 1465 rf->set_channel = zyd_rfmd_set_channel; 1466 rf->width = 24; /* 24-bit RF values */ 1467 break; 1468 case ZYD_RF_AL2230: 1469 case ZYD_RF_AL2230S: 1470 if (sc->mac_rev == ZYD_ZD1211B) 1471 rf->init = zyd_al2230_init_b; 1472 else 1473 rf->init = zyd_al2230_init; 1474 rf->switch_radio = zyd_al2230_switch_radio; 1475 rf->set_channel = zyd_al2230_set_channel; 1476 rf->width = 24; /* 24-bit RF values */ 1477 break; 1478 case ZYD_RF_AL7230B: 1479 rf->init = zyd_al7230B_init; 1480 rf->switch_radio = zyd_al7230B_switch_radio; 1481 rf->set_channel = zyd_al7230B_set_channel; 1482 rf->width = 24; /* 24-bit RF values */ 1483 break; 1484 case ZYD_RF_AL2210: 1485 rf->init = zyd_al2210_init; 1486 rf->switch_radio = zyd_al2210_switch_radio; 1487 rf->set_channel = zyd_al2210_set_channel; 1488 rf->width = 24; /* 24-bit RF values */ 1489 break; 1490 case ZYD_RF_GCT: 1491 rf->init = zyd_gct_init; 1492 rf->switch_radio = zyd_gct_switch_radio; 1493 rf->set_channel = zyd_gct_set_channel; 1494 rf->width = 21; /* 21-bit RF values */ 1495 break; 1496 case ZYD_RF_MAXIM_NEW: 1497 rf->init = zyd_maxim_init; 1498 rf->switch_radio = zyd_maxim_switch_radio; 1499 rf->set_channel = zyd_maxim_set_channel; 1500 rf->width = 18; /* 18-bit RF values */ 1501 break; 1502 case ZYD_RF_MAXIM_NEW2: 1503 rf->init = zyd_maxim2_init; 1504 rf->switch_radio = zyd_maxim2_switch_radio; 1505 rf->set_channel = zyd_maxim2_set_channel; 1506 rf->width = 18; /* 18-bit RF values */ 1507 break; 1508 default: 1509 printf("%s: sorry, radio \"%s\" is not supported yet\n", 1510 device_xname(sc->sc_dev), zyd_rf_name(type)); 1511 return EINVAL; 1512 } 1513 return 0; 1514 } 1515 1516 Static const char * 1517 zyd_rf_name(uint8_t type) 1518 { 1519 static const char * const zyd_rfs[] = { 1520 "unknown", "unknown", "UW2451", "UCHIP", "AL2230", 1521 "AL7230B", "THETA", "AL2210", "MAXIM_NEW", "GCT", 1522 "AL2230S", "RALINK", "INTERSIL", "RFMD", "MAXIM_NEW2", 1523 "PHILIPS" 1524 }; 1525 1526 return zyd_rfs[(type > 15) ? 0 : type]; 1527 } 1528 1529 Static int 1530 zyd_hw_init(struct zyd_softc *sc) 1531 { 1532 struct zyd_rf *rf = &sc->sc_rf; 1533 const struct zyd_phy_pair *phyp; 1534 int error; 1535 1536 /* specify that the plug and play is finished */ 1537 (void)zyd_write32(sc, ZYD_MAC_AFTER_PNP, 1); 1538 1539 (void)zyd_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->fwbase); 1540 DPRINTF(("firmware base address=0x%04x\n", sc->fwbase)); 1541 1542 /* retrieve firmware revision number */ 1543 (void)zyd_read16(sc, sc->fwbase + ZYD_FW_FIRMWARE_REV, &sc->fw_rev); 1544 1545 (void)zyd_write32(sc, ZYD_CR_GPI_EN, 0); 1546 (void)zyd_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f); 1547 1548 /* disable interrupts */ 1549 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0); 1550 1551 /* PHY init */ 1552 zyd_lock_phy(sc); 1553 phyp = (sc->mac_rev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy; 1554 for (; phyp->reg != 0; phyp++) { 1555 if ((error = zyd_write16(sc, phyp->reg, phyp->val)) != 0) 1556 goto fail; 1557 } 1558 zyd_unlock_phy(sc); 1559 1560 /* HMAC init */ 1561 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020); 1562 zyd_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808); 1563 1564 if (sc->mac_rev == ZYD_ZD1211) { 1565 zyd_write32(sc, ZYD_MAC_RETRY, 0x00000002); 1566 } else { 1567 zyd_write32(sc, ZYD_MAC_RETRY, 0x02020202); 1568 zyd_write32(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f); 1569 zyd_write32(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f); 1570 zyd_write32(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f); 1571 zyd_write32(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f); 1572 zyd_write32(sc, ZYD_MACB_AIFS_CTL1, 0x00280028); 1573 zyd_write32(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C); 1574 zyd_write32(sc, ZYD_MACB_TXOP, 0x01800824); 1575 } 1576 1577 zyd_write32(sc, ZYD_MAC_SNIFFER, 0x00000000); 1578 zyd_write32(sc, ZYD_MAC_RXFILTER, 0x00000000); 1579 zyd_write32(sc, ZYD_MAC_GHTBL, 0x00000000); 1580 zyd_write32(sc, ZYD_MAC_GHTBH, 0x80000000); 1581 zyd_write32(sc, ZYD_MAC_MISC, 0x000000a4); 1582 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f); 1583 zyd_write32(sc, ZYD_MAC_BCNCFG, 0x00f00401); 1584 zyd_write32(sc, ZYD_MAC_PHY_DELAY2, 0x00000000); 1585 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000080); 1586 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000); 1587 zyd_write32(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100); 1588 zyd_write32(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0547c032); 1589 zyd_write32(sc, ZYD_CR_RX_PE_DELAY, 0x00000070); 1590 zyd_write32(sc, ZYD_CR_PS_CTRL, 0x10000000); 1591 zyd_write32(sc, ZYD_MAC_RTSCTSRATE, 0x02030203); 1592 zyd_write32(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640); 1593 zyd_write32(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114); 1594 1595 /* RF chip init */ 1596 zyd_lock_phy(sc); 1597 error = (*rf->init)(rf); 1598 zyd_unlock_phy(sc); 1599 if (error != 0) { 1600 printf("%s: radio initialization failed\n", 1601 device_xname(sc->sc_dev)); 1602 goto fail; 1603 } 1604 1605 /* init beacon interval to 100ms */ 1606 if ((error = zyd_set_beacon_interval(sc, 100)) != 0) 1607 goto fail; 1608 1609 fail: return error; 1610 } 1611 1612 Static int 1613 zyd_read_eeprom(struct zyd_softc *sc) 1614 { 1615 struct ieee80211com *ic = &sc->sc_ic; 1616 uint32_t tmp; 1617 uint16_t val; 1618 int i; 1619 1620 /* read MAC address */ 1621 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P1, &tmp); 1622 ic->ic_myaddr[0] = tmp & 0xff; 1623 ic->ic_myaddr[1] = tmp >> 8; 1624 ic->ic_myaddr[2] = tmp >> 16; 1625 ic->ic_myaddr[3] = tmp >> 24; 1626 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P2, &tmp); 1627 ic->ic_myaddr[4] = tmp & 0xff; 1628 ic->ic_myaddr[5] = tmp >> 8; 1629 1630 (void)zyd_read32(sc, ZYD_EEPROM_POD, &tmp); 1631 sc->rf_rev = tmp & 0x0f; 1632 sc->pa_rev = (tmp >> 16) & 0x0f; 1633 1634 /* read regulatory domain (currently unused) */ 1635 (void)zyd_read32(sc, ZYD_EEPROM_SUBID, &tmp); 1636 sc->regdomain = tmp >> 16; 1637 DPRINTF(("regulatory domain %x\n", sc->regdomain)); 1638 1639 /* read Tx power calibration tables */ 1640 for (i = 0; i < 7; i++) { 1641 (void)zyd_read16(sc, ZYD_EEPROM_PWR_CAL + i, &val); 1642 sc->pwr_cal[i * 2] = val >> 8; 1643 sc->pwr_cal[i * 2 + 1] = val & 0xff; 1644 1645 (void)zyd_read16(sc, ZYD_EEPROM_PWR_INT + i, &val); 1646 sc->pwr_int[i * 2] = val >> 8; 1647 sc->pwr_int[i * 2 + 1] = val & 0xff; 1648 1649 (void)zyd_read16(sc, ZYD_EEPROM_36M_CAL + i, &val); 1650 sc->ofdm36_cal[i * 2] = val >> 8; 1651 sc->ofdm36_cal[i * 2 + 1] = val & 0xff; 1652 1653 (void)zyd_read16(sc, ZYD_EEPROM_48M_CAL + i, &val); 1654 sc->ofdm48_cal[i * 2] = val >> 8; 1655 sc->ofdm48_cal[i * 2 + 1] = val & 0xff; 1656 1657 (void)zyd_read16(sc, ZYD_EEPROM_54M_CAL + i, &val); 1658 sc->ofdm54_cal[i * 2] = val >> 8; 1659 sc->ofdm54_cal[i * 2 + 1] = val & 0xff; 1660 } 1661 return 0; 1662 } 1663 1664 Static int 1665 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr) 1666 { 1667 uint32_t tmp; 1668 1669 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]; 1670 (void)zyd_write32(sc, ZYD_MAC_MACADRL, tmp); 1671 1672 tmp = addr[5] << 8 | addr[4]; 1673 (void)zyd_write32(sc, ZYD_MAC_MACADRH, tmp); 1674 1675 return 0; 1676 } 1677 1678 Static int 1679 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr) 1680 { 1681 uint32_t tmp; 1682 1683 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]; 1684 (void)zyd_write32(sc, ZYD_MAC_BSSADRL, tmp); 1685 1686 tmp = addr[5] << 8 | addr[4]; 1687 (void)zyd_write32(sc, ZYD_MAC_BSSADRH, tmp); 1688 1689 return 0; 1690 } 1691 1692 Static int 1693 zyd_switch_radio(struct zyd_softc *sc, int on) 1694 { 1695 struct zyd_rf *rf = &sc->sc_rf; 1696 int error; 1697 1698 zyd_lock_phy(sc); 1699 error = (*rf->switch_radio)(rf, on); 1700 zyd_unlock_phy(sc); 1701 1702 return error; 1703 } 1704 1705 Static void 1706 zyd_set_led(struct zyd_softc *sc, int which, int on) 1707 { 1708 uint32_t tmp; 1709 1710 (void)zyd_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp); 1711 tmp &= ~which; 1712 if (on) 1713 tmp |= which; 1714 (void)zyd_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp); 1715 } 1716 1717 Static int 1718 zyd_set_rxfilter(struct zyd_softc *sc) 1719 { 1720 uint32_t rxfilter; 1721 1722 switch (sc->sc_ic.ic_opmode) { 1723 case IEEE80211_M_STA: 1724 rxfilter = ZYD_FILTER_BSS; 1725 break; 1726 case IEEE80211_M_IBSS: 1727 case IEEE80211_M_HOSTAP: 1728 rxfilter = ZYD_FILTER_HOSTAP; 1729 break; 1730 case IEEE80211_M_MONITOR: 1731 rxfilter = ZYD_FILTER_MONITOR; 1732 break; 1733 default: 1734 /* should not get there */ 1735 return EINVAL; 1736 } 1737 return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter); 1738 } 1739 1740 Static void 1741 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c) 1742 { 1743 struct ieee80211com *ic = &sc->sc_ic; 1744 struct zyd_rf *rf = &sc->sc_rf; 1745 u_int chan; 1746 1747 chan = ieee80211_chan2ieee(ic, c); 1748 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1749 return; 1750 1751 zyd_lock_phy(sc); 1752 1753 (*rf->set_channel)(rf, chan); 1754 1755 /* update Tx power */ 1756 (void)zyd_write32(sc, ZYD_CR31, sc->pwr_int[chan - 1]); 1757 (void)zyd_write32(sc, ZYD_CR68, sc->pwr_cal[chan - 1]); 1758 1759 if (sc->mac_rev == ZYD_ZD1211B) { 1760 (void)zyd_write32(sc, ZYD_CR67, sc->ofdm36_cal[chan - 1]); 1761 (void)zyd_write32(sc, ZYD_CR66, sc->ofdm48_cal[chan - 1]); 1762 (void)zyd_write32(sc, ZYD_CR65, sc->ofdm54_cal[chan - 1]); 1763 1764 (void)zyd_write32(sc, ZYD_CR69, 0x28); 1765 (void)zyd_write32(sc, ZYD_CR69, 0x2a); 1766 } 1767 1768 zyd_unlock_phy(sc); 1769 } 1770 1771 Static int 1772 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval) 1773 { 1774 /* XXX this is probably broken.. */ 1775 (void)zyd_write32(sc, ZYD_CR_ATIM_WND_PERIOD, bintval - 2); 1776 (void)zyd_write32(sc, ZYD_CR_PRE_TBTT, bintval - 1); 1777 (void)zyd_write32(sc, ZYD_CR_BCN_INTERVAL, bintval); 1778 1779 return 0; 1780 } 1781 1782 Static uint8_t 1783 zyd_plcp_signal(int rate) 1784 { 1785 switch (rate) { 1786 /* CCK rates (returned values are device-dependent) */ 1787 case 2: return 0x0; 1788 case 4: return 0x1; 1789 case 11: return 0x2; 1790 case 22: return 0x3; 1791 1792 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1793 case 12: return 0xb; 1794 case 18: return 0xf; 1795 case 24: return 0xa; 1796 case 36: return 0xe; 1797 case 48: return 0x9; 1798 case 72: return 0xd; 1799 case 96: return 0x8; 1800 case 108: return 0xc; 1801 1802 /* unsupported rates (should not get there) */ 1803 default: return 0xff; 1804 } 1805 } 1806 1807 Static void 1808 zyd_intr(struct usbd_xfer *xfer, void * priv, usbd_status status) 1809 { 1810 struct zyd_softc *sc = (struct zyd_softc *)priv; 1811 struct zyd_cmd *cmd; 1812 uint32_t datalen; 1813 1814 if (status != USBD_NORMAL_COMPLETION) { 1815 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1816 return; 1817 1818 if (status == USBD_STALLED) { 1819 usbd_clear_endpoint_stall_async( 1820 sc->zyd_ep[ZYD_ENDPT_IIN]); 1821 } 1822 return; 1823 } 1824 1825 cmd = (struct zyd_cmd *)sc->ibuf; 1826 1827 if (le16toh(cmd->code) == ZYD_NOTIF_RETRYSTATUS) { 1828 struct zyd_notif_retry *retry = 1829 (struct zyd_notif_retry *)cmd->data; 1830 struct ieee80211com *ic = &sc->sc_ic; 1831 struct ifnet *ifp = &sc->sc_if; 1832 struct ieee80211_node *ni; 1833 1834 DPRINTF(("retry intr: rate=0x%x addr=%s count=%d (0x%x)\n", 1835 le16toh(retry->rate), ether_sprintf(retry->macaddr), 1836 le16toh(retry->count) & 0xff, le16toh(retry->count))); 1837 1838 /* 1839 * Find the node to which the packet was sent and update its 1840 * retry statistics. In BSS mode, this node is the AP we're 1841 * associated to so no lookup is actually needed. 1842 */ 1843 if (ic->ic_opmode != IEEE80211_M_STA) { 1844 ni = ieee80211_find_node(&ic->ic_scan, retry->macaddr); 1845 if (ni == NULL) 1846 return; /* just ignore */ 1847 } else 1848 ni = ic->ic_bss; 1849 1850 ((struct zyd_node *)ni)->amn.amn_retrycnt++; 1851 1852 if (le16toh(retry->count) & 0x100) 1853 ifp->if_oerrors++; /* too many retries */ 1854 1855 } else if (le16toh(cmd->code) == ZYD_NOTIF_IORD) { 1856 struct rq *rqp; 1857 1858 if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT) 1859 return; /* HMAC interrupt */ 1860 1861 usbd_get_xfer_status(xfer, NULL, NULL, &datalen, NULL); 1862 datalen -= sizeof(cmd->code); 1863 datalen -= 2; /* XXX: padding? */ 1864 1865 SIMPLEQ_FOREACH(rqp, &sc->sc_rqh, rq) { 1866 int i; 1867 1868 if (sizeof(struct zyd_pair) * rqp->len != datalen) 1869 continue; 1870 for (i = 0; i < rqp->len; i++) { 1871 if (*(((const uint16_t *)rqp->idata) + i) != 1872 (((struct zyd_pair *)cmd->data) + i)->reg) 1873 break; 1874 } 1875 if (i != rqp->len) 1876 continue; 1877 1878 /* copy answer into caller-supplied buffer */ 1879 memcpy(rqp->odata, cmd->data, 1880 sizeof(struct zyd_pair) * rqp->len); 1881 wakeup(rqp->odata); /* wakeup caller */ 1882 1883 return; 1884 } 1885 return; /* unexpected IORD notification */ 1886 } else { 1887 printf("%s: unknown notification %x\n", device_xname(sc->sc_dev), 1888 le16toh(cmd->code)); 1889 } 1890 } 1891 1892 Static void 1893 zyd_rx_data(struct zyd_softc *sc, const uint8_t *buf, uint16_t len) 1894 { 1895 struct ieee80211com *ic = &sc->sc_ic; 1896 struct ifnet *ifp = &sc->sc_if; 1897 struct ieee80211_node *ni; 1898 struct ieee80211_frame *wh; 1899 const struct zyd_plcphdr *plcp; 1900 const struct zyd_rx_stat *stat; 1901 struct mbuf *m; 1902 int rlen, s; 1903 1904 if (len < ZYD_MIN_FRAGSZ) { 1905 printf("%s: frame too short (length=%d)\n", 1906 device_xname(sc->sc_dev), len); 1907 ifp->if_ierrors++; 1908 return; 1909 } 1910 1911 plcp = (const struct zyd_plcphdr *)buf; 1912 stat = (const struct zyd_rx_stat *) 1913 (buf + len - sizeof(struct zyd_rx_stat)); 1914 1915 if (stat->flags & ZYD_RX_ERROR) { 1916 DPRINTF(("%s: RX status indicated error (%x)\n", 1917 device_xname(sc->sc_dev), stat->flags)); 1918 ifp->if_ierrors++; 1919 return; 1920 } 1921 1922 /* compute actual frame length */ 1923 rlen = len - sizeof(struct zyd_plcphdr) - 1924 sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN; 1925 1926 /* allocate a mbuf to store the frame */ 1927 MGETHDR(m, M_DONTWAIT, MT_DATA); 1928 if (m == NULL) { 1929 printf("%s: could not allocate rx mbuf\n", 1930 device_xname(sc->sc_dev)); 1931 ifp->if_ierrors++; 1932 return; 1933 } 1934 if (rlen > MHLEN) { 1935 MCLGET(m, M_DONTWAIT); 1936 if (!(m->m_flags & M_EXT)) { 1937 printf("%s: could not allocate rx mbuf cluster\n", 1938 device_xname(sc->sc_dev)); 1939 m_freem(m); 1940 ifp->if_ierrors++; 1941 return; 1942 } 1943 } 1944 m_set_rcvif(m, ifp); 1945 m->m_pkthdr.len = m->m_len = rlen; 1946 memcpy(mtod(m, uint8_t *), (const uint8_t *)(plcp + 1), rlen); 1947 1948 s = splnet(); 1949 1950 if (sc->sc_drvbpf != NULL) { 1951 struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap; 1952 static const uint8_t rates[] = { 1953 /* reverse function of zyd_plcp_signal() */ 1954 2, 4, 11, 22, 0, 0, 0, 0, 1955 96, 48, 24, 12, 108, 72, 36, 18 1956 }; 1957 1958 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS; 1959 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); 1960 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 1961 tap->wr_rssi = stat->rssi; 1962 tap->wr_rate = rates[plcp->signal & 0xf]; 1963 1964 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m, BPF_D_IN); 1965 } 1966 1967 wh = mtod(m, struct ieee80211_frame *); 1968 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh); 1969 ieee80211_input(ic, m, ni, stat->rssi, 0); 1970 1971 /* node is no longer needed */ 1972 ieee80211_free_node(ni); 1973 1974 splx(s); 1975 } 1976 1977 Static void 1978 zyd_rxeof(struct usbd_xfer *xfer, void * priv, usbd_status status) 1979 { 1980 struct zyd_rx_data *data = priv; 1981 struct zyd_softc *sc = data->sc; 1982 struct ifnet *ifp = &sc->sc_if; 1983 const struct zyd_rx_desc *desc; 1984 int len; 1985 1986 if (status != USBD_NORMAL_COMPLETION) { 1987 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1988 return; 1989 1990 if (status == USBD_STALLED) 1991 usbd_clear_endpoint_stall(sc->zyd_ep[ZYD_ENDPT_BIN]); 1992 1993 goto skip; 1994 } 1995 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 1996 1997 if (len < ZYD_MIN_RXBUFSZ) { 1998 printf("%s: xfer too short (length=%d)\n", 1999 device_xname(sc->sc_dev), len); 2000 ifp->if_ierrors++; 2001 goto skip; 2002 } 2003 2004 desc = (const struct zyd_rx_desc *) 2005 (data->buf + len - sizeof(struct zyd_rx_desc)); 2006 2007 if (UGETW(desc->tag) == ZYD_TAG_MULTIFRAME) { 2008 const uint8_t *p = data->buf, *end = p + len; 2009 int i; 2010 2011 DPRINTFN(3, ("received multi-frame transfer\n")); 2012 2013 for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) { 2014 const uint16_t len16 = UGETW(desc->len[i]); 2015 2016 if (len16 == 0 || p + len16 > end) 2017 break; 2018 2019 zyd_rx_data(sc, p, len16); 2020 /* next frame is aligned on a 32-bit boundary */ 2021 p += (len16 + 3) & ~3; 2022 } 2023 } else { 2024 DPRINTFN(3, ("received single-frame transfer\n")); 2025 2026 zyd_rx_data(sc, data->buf, len); 2027 } 2028 2029 skip: /* setup a new transfer */ 2030 2031 usbd_setup_xfer(xfer, data, NULL, ZYX_MAX_RXBUFSZ, USBD_SHORT_XFER_OK, 2032 USBD_NO_TIMEOUT, zyd_rxeof); 2033 (void)usbd_transfer(xfer); 2034 } 2035 2036 Static int 2037 zyd_tx_mgt(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 2038 { 2039 struct ieee80211com *ic = &sc->sc_ic; 2040 struct ifnet *ifp = &sc->sc_if; 2041 struct zyd_tx_desc *desc; 2042 struct zyd_tx_data *data; 2043 struct ieee80211_frame *wh; 2044 struct ieee80211_key *k; 2045 int xferlen, totlen, rate; 2046 uint16_t pktlen; 2047 usbd_status error; 2048 2049 data = &sc->tx_data[0]; 2050 desc = (struct zyd_tx_desc *)data->buf; 2051 2052 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2; 2053 2054 wh = mtod(m0, struct ieee80211_frame *); 2055 2056 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 2057 k = ieee80211_crypto_encap(ic, ni, m0); 2058 if (k == NULL) { 2059 m_freem(m0); 2060 return ENOBUFS; 2061 } 2062 } 2063 2064 data->ni = ni; 2065 2066 wh = mtod(m0, struct ieee80211_frame *); 2067 2068 xferlen = sizeof(struct zyd_tx_desc) + m0->m_pkthdr.len; 2069 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN; 2070 2071 /* fill Tx descriptor */ 2072 desc->len = htole16(totlen); 2073 2074 desc->flags = ZYD_TX_FLAG_BACKOFF; 2075 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 2076 /* multicast frames are not sent at OFDM rates in 802.11b/g */ 2077 if (totlen > ic->ic_rtsthreshold) { 2078 desc->flags |= ZYD_TX_FLAG_RTS; 2079 } else if (ZYD_RATE_IS_OFDM(rate) && 2080 (ic->ic_flags & IEEE80211_F_USEPROT)) { 2081 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 2082 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF; 2083 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 2084 desc->flags |= ZYD_TX_FLAG_RTS; 2085 } 2086 } else 2087 desc->flags |= ZYD_TX_FLAG_MULTICAST; 2088 2089 if ((wh->i_fc[0] & 2090 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 2091 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL)) 2092 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL); 2093 2094 desc->phy = zyd_plcp_signal(rate); 2095 if (ZYD_RATE_IS_OFDM(rate)) { 2096 desc->phy |= ZYD_TX_PHY_OFDM; 2097 if (ic->ic_curmode == IEEE80211_MODE_11A) 2098 desc->phy |= ZYD_TX_PHY_5GHZ; 2099 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 2100 desc->phy |= ZYD_TX_PHY_SHPREAMBLE; 2101 2102 /* actual transmit length (XXX why +10?) */ 2103 pktlen = sizeof(struct zyd_tx_desc) + 10; 2104 if (sc->mac_rev == ZYD_ZD1211) 2105 pktlen += totlen; 2106 desc->pktlen = htole16(pktlen); 2107 2108 desc->plcp_length = (16 * totlen + rate - 1) / rate; 2109 desc->plcp_service = 0; 2110 if (rate == 22) { 2111 const int remainder = (16 * totlen) % 22; 2112 if (remainder != 0 && remainder < 7) 2113 desc->plcp_service |= ZYD_PLCP_LENGEXT; 2114 } 2115 2116 if (sc->sc_drvbpf != NULL) { 2117 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap; 2118 2119 tap->wt_flags = 0; 2120 tap->wt_rate = rate; 2121 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 2122 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 2123 2124 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0, BPF_D_OUT); 2125 } 2126 2127 m_copydata(m0, 0, m0->m_pkthdr.len, 2128 data->buf + sizeof(struct zyd_tx_desc)); 2129 2130 DPRINTFN(10, ("%s: sending mgt frame len=%zu rate=%u xferlen=%u\n", 2131 device_xname(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen)); 2132 2133 m_freem(m0); /* mbuf no longer needed */ 2134 2135 usbd_setup_xfer(data->xfer, data, data->buf, xferlen, 2136 USBD_FORCE_SHORT_XFER, ZYD_TX_TIMEOUT, zyd_txeof); 2137 error = usbd_transfer(data->xfer); 2138 if (error != USBD_IN_PROGRESS && error != 0) { 2139 ifp->if_oerrors++; 2140 return EIO; 2141 } 2142 sc->tx_queued++; 2143 2144 return 0; 2145 } 2146 2147 Static void 2148 zyd_txeof(struct usbd_xfer *xfer, void * priv, usbd_status status) 2149 { 2150 struct zyd_tx_data *data = priv; 2151 struct zyd_softc *sc = data->sc; 2152 struct ifnet *ifp = &sc->sc_if; 2153 int s; 2154 2155 if (status != USBD_NORMAL_COMPLETION) { 2156 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 2157 return; 2158 2159 printf("%s: could not transmit buffer: %s\n", 2160 device_xname(sc->sc_dev), usbd_errstr(status)); 2161 2162 if (status == USBD_STALLED) { 2163 usbd_clear_endpoint_stall_async( 2164 sc->zyd_ep[ZYD_ENDPT_BOUT]); 2165 } 2166 ifp->if_oerrors++; 2167 return; 2168 } 2169 2170 s = splnet(); 2171 2172 /* update rate control statistics */ 2173 ((struct zyd_node *)data->ni)->amn.amn_txcnt++; 2174 2175 ieee80211_free_node(data->ni); 2176 data->ni = NULL; 2177 2178 sc->tx_queued--; 2179 ifp->if_opackets++; 2180 2181 sc->tx_timer = 0; 2182 ifp->if_flags &= ~IFF_OACTIVE; 2183 zyd_start(ifp); 2184 2185 splx(s); 2186 } 2187 2188 Static int 2189 zyd_tx_data(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 2190 { 2191 struct ieee80211com *ic = &sc->sc_ic; 2192 struct ifnet *ifp = &sc->sc_if; 2193 struct zyd_tx_desc *desc; 2194 struct zyd_tx_data *data; 2195 struct ieee80211_frame *wh; 2196 struct ieee80211_key *k; 2197 int xferlen, totlen, rate; 2198 uint16_t pktlen; 2199 usbd_status error; 2200 2201 wh = mtod(m0, struct ieee80211_frame *); 2202 2203 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) 2204 rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate]; 2205 else 2206 rate = ni->ni_rates.rs_rates[ni->ni_txrate]; 2207 rate &= IEEE80211_RATE_VAL; 2208 2209 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 2210 k = ieee80211_crypto_encap(ic, ni, m0); 2211 if (k == NULL) { 2212 m_freem(m0); 2213 return ENOBUFS; 2214 } 2215 2216 /* packet header may have moved, reset our local pointer */ 2217 wh = mtod(m0, struct ieee80211_frame *); 2218 } 2219 2220 data = &sc->tx_data[0]; 2221 desc = (struct zyd_tx_desc *)data->buf; 2222 2223 data->ni = ni; 2224 2225 xferlen = sizeof(struct zyd_tx_desc) + m0->m_pkthdr.len; 2226 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN; 2227 2228 /* fill Tx descriptor */ 2229 desc->len = htole16(totlen); 2230 2231 desc->flags = ZYD_TX_FLAG_BACKOFF; 2232 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 2233 /* multicast frames are not sent at OFDM rates in 802.11b/g */ 2234 if (totlen > ic->ic_rtsthreshold) { 2235 desc->flags |= ZYD_TX_FLAG_RTS; 2236 } else if (ZYD_RATE_IS_OFDM(rate) && 2237 (ic->ic_flags & IEEE80211_F_USEPROT)) { 2238 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 2239 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF; 2240 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 2241 desc->flags |= ZYD_TX_FLAG_RTS; 2242 } 2243 } else 2244 desc->flags |= ZYD_TX_FLAG_MULTICAST; 2245 2246 if ((wh->i_fc[0] & 2247 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 2248 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL)) 2249 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL); 2250 2251 desc->phy = zyd_plcp_signal(rate); 2252 if (ZYD_RATE_IS_OFDM(rate)) { 2253 desc->phy |= ZYD_TX_PHY_OFDM; 2254 if (ic->ic_curmode == IEEE80211_MODE_11A) 2255 desc->phy |= ZYD_TX_PHY_5GHZ; 2256 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 2257 desc->phy |= ZYD_TX_PHY_SHPREAMBLE; 2258 2259 /* actual transmit length (XXX why +10?) */ 2260 pktlen = sizeof(struct zyd_tx_desc) + 10; 2261 if (sc->mac_rev == ZYD_ZD1211) 2262 pktlen += totlen; 2263 desc->pktlen = htole16(pktlen); 2264 2265 desc->plcp_length = (16 * totlen + rate - 1) / rate; 2266 desc->plcp_service = 0; 2267 if (rate == 22) { 2268 const int remainder = (16 * totlen) % 22; 2269 if (remainder != 0 && remainder < 7) 2270 desc->plcp_service |= ZYD_PLCP_LENGEXT; 2271 } 2272 2273 if (sc->sc_drvbpf != NULL) { 2274 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap; 2275 2276 tap->wt_flags = 0; 2277 tap->wt_rate = rate; 2278 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 2279 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 2280 2281 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0, BPF_D_OUT); 2282 } 2283 2284 m_copydata(m0, 0, m0->m_pkthdr.len, 2285 data->buf + sizeof(struct zyd_tx_desc)); 2286 2287 DPRINTFN(10, ("%s: sending data frame len=%zu rate=%u xferlen=%u\n", 2288 device_xname(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen)); 2289 2290 m_freem(m0); /* mbuf no longer needed */ 2291 2292 usbd_setup_xfer(data->xfer, data, data->buf, xferlen, 2293 USBD_FORCE_SHORT_XFER, ZYD_TX_TIMEOUT, zyd_txeof); 2294 error = usbd_transfer(data->xfer); 2295 if (error != USBD_IN_PROGRESS && error != 0) { 2296 ifp->if_oerrors++; 2297 return EIO; 2298 } 2299 sc->tx_queued++; 2300 2301 return 0; 2302 } 2303 2304 Static void 2305 zyd_start(struct ifnet *ifp) 2306 { 2307 struct zyd_softc *sc = ifp->if_softc; 2308 struct ieee80211com *ic = &sc->sc_ic; 2309 struct ether_header *eh; 2310 struct ieee80211_node *ni; 2311 struct mbuf *m0; 2312 2313 for (;;) { 2314 IF_POLL(&ic->ic_mgtq, m0); 2315 if (m0 != NULL) { 2316 if (sc->tx_queued >= ZYD_TX_LIST_CNT) { 2317 ifp->if_flags |= IFF_OACTIVE; 2318 break; 2319 } 2320 IF_DEQUEUE(&ic->ic_mgtq, m0); 2321 2322 ni = M_GETCTX(m0, struct ieee80211_node *); 2323 M_CLEARCTX(m0); 2324 bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT); 2325 if (zyd_tx_mgt(sc, m0, ni) != 0) 2326 break; 2327 } else { 2328 if (ic->ic_state != IEEE80211_S_RUN) 2329 break; 2330 IFQ_POLL(&ifp->if_snd, m0); 2331 if (m0 == NULL) 2332 break; 2333 if (sc->tx_queued >= ZYD_TX_LIST_CNT) { 2334 ifp->if_flags |= IFF_OACTIVE; 2335 break; 2336 } 2337 IFQ_DEQUEUE(&ifp->if_snd, m0); 2338 2339 if (m0->m_len < sizeof(struct ether_header) && 2340 !(m0 = m_pullup(m0, sizeof(struct ether_header)))) 2341 continue; 2342 2343 eh = mtod(m0, struct ether_header *); 2344 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 2345 if (ni == NULL) { 2346 m_freem(m0); 2347 continue; 2348 } 2349 bpf_mtap(ifp, m0, BPF_D_OUT); 2350 if ((m0 = ieee80211_encap(ic, m0, ni)) == NULL) { 2351 ieee80211_free_node(ni); 2352 ifp->if_oerrors++; 2353 continue; 2354 } 2355 bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT); 2356 if (zyd_tx_data(sc, m0, ni) != 0) { 2357 ieee80211_free_node(ni); 2358 ifp->if_oerrors++; 2359 break; 2360 } 2361 } 2362 2363 sc->tx_timer = 5; 2364 ifp->if_timer = 1; 2365 } 2366 } 2367 2368 Static void 2369 zyd_watchdog(struct ifnet *ifp) 2370 { 2371 struct zyd_softc *sc = ifp->if_softc; 2372 struct ieee80211com *ic = &sc->sc_ic; 2373 2374 ifp->if_timer = 0; 2375 2376 if (sc->tx_timer > 0) { 2377 if (--sc->tx_timer == 0) { 2378 printf("%s: device timeout\n", device_xname(sc->sc_dev)); 2379 /* zyd_init(ifp); XXX needs a process context ? */ 2380 ifp->if_oerrors++; 2381 return; 2382 } 2383 ifp->if_timer = 1; 2384 } 2385 2386 ieee80211_watchdog(ic); 2387 } 2388 2389 Static int 2390 zyd_ioctl(struct ifnet *ifp, u_long cmd, void *data) 2391 { 2392 struct zyd_softc *sc = ifp->if_softc; 2393 struct ieee80211com *ic = &sc->sc_ic; 2394 int s, error = 0; 2395 2396 s = splnet(); 2397 2398 switch (cmd) { 2399 case SIOCSIFFLAGS: 2400 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 2401 break; 2402 /* XXX re-use ether_ioctl() */ 2403 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { 2404 case IFF_UP: 2405 zyd_init(ifp); 2406 break; 2407 case IFF_RUNNING: 2408 zyd_stop(ifp, 1); 2409 break; 2410 default: 2411 break; 2412 } 2413 break; 2414 2415 default: 2416 error = ieee80211_ioctl(ic, cmd, data); 2417 } 2418 2419 if (error == ENETRESET) { 2420 if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) == 2421 (IFF_RUNNING | IFF_UP)) 2422 zyd_init(ifp); 2423 error = 0; 2424 } 2425 2426 splx(s); 2427 2428 return error; 2429 } 2430 2431 Static int 2432 zyd_init(struct ifnet *ifp) 2433 { 2434 struct zyd_softc *sc = ifp->if_softc; 2435 struct ieee80211com *ic = &sc->sc_ic; 2436 int i, error; 2437 2438 zyd_stop(ifp, 0); 2439 2440 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl)); 2441 DPRINTF(("setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr))); 2442 error = zyd_set_macaddr(sc, ic->ic_myaddr); 2443 if (error != 0) 2444 return error; 2445 2446 /* we'll do software WEP decryption for now */ 2447 DPRINTF(("setting encryption type\n")); 2448 error = zyd_write32(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER); 2449 if (error != 0) 2450 return error; 2451 2452 /* promiscuous mode */ 2453 (void)zyd_write32(sc, ZYD_MAC_SNIFFER, 2454 (ic->ic_opmode == IEEE80211_M_MONITOR) ? 1 : 0); 2455 2456 (void)zyd_set_rxfilter(sc); 2457 2458 /* switch radio transmitter ON */ 2459 (void)zyd_switch_radio(sc, 1); 2460 2461 /* set basic rates */ 2462 if (ic->ic_curmode == IEEE80211_MODE_11B) 2463 (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x0003); 2464 else if (ic->ic_curmode == IEEE80211_MODE_11A) 2465 (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x1500); 2466 else /* assumes 802.11b/g */ 2467 (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x000f); 2468 2469 /* set mandatory rates */ 2470 if (ic->ic_curmode == IEEE80211_MODE_11B) 2471 (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x000f); 2472 else if (ic->ic_curmode == IEEE80211_MODE_11A) 2473 (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x1500); 2474 else /* assumes 802.11b/g */ 2475 (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x150f); 2476 2477 /* set default BSS channel */ 2478 ic->ic_bss->ni_chan = ic->ic_ibss_chan; 2479 zyd_set_chan(sc, ic->ic_bss->ni_chan); 2480 2481 /* enable interrupts */ 2482 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK); 2483 2484 /* 2485 * Allocate Tx and Rx xfer queues. 2486 */ 2487 if ((error = zyd_alloc_tx_list(sc)) != 0) { 2488 printf("%s: could not allocate Tx list\n", 2489 device_xname(sc->sc_dev)); 2490 goto fail; 2491 } 2492 if ((error = zyd_alloc_rx_list(sc)) != 0) { 2493 printf("%s: could not allocate Rx list\n", 2494 device_xname(sc->sc_dev)); 2495 goto fail; 2496 } 2497 2498 /* 2499 * Start up the receive pipe. 2500 */ 2501 for (i = 0; i < ZYD_RX_LIST_CNT; i++) { 2502 struct zyd_rx_data *data = &sc->rx_data[i]; 2503 2504 usbd_setup_xfer(data->xfer, data, NULL, ZYX_MAX_RXBUFSZ, 2505 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, zyd_rxeof); 2506 error = usbd_transfer(data->xfer); 2507 if (error != USBD_IN_PROGRESS && error != 0) { 2508 printf("%s: could not queue Rx transfer\n", 2509 device_xname(sc->sc_dev)); 2510 goto fail; 2511 } 2512 } 2513 2514 ifp->if_flags &= ~IFF_OACTIVE; 2515 ifp->if_flags |= IFF_RUNNING; 2516 2517 if (ic->ic_opmode == IEEE80211_M_MONITOR) 2518 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 2519 else 2520 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2521 2522 return 0; 2523 2524 fail: zyd_stop(ifp, 1); 2525 return error; 2526 } 2527 2528 Static void 2529 zyd_stop(struct ifnet *ifp, int disable) 2530 { 2531 struct zyd_softc *sc = ifp->if_softc; 2532 struct ieee80211com *ic = &sc->sc_ic; 2533 2534 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */ 2535 2536 sc->tx_timer = 0; 2537 ifp->if_timer = 0; 2538 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2539 2540 /* switch radio transmitter OFF */ 2541 (void)zyd_switch_radio(sc, 0); 2542 2543 /* disable Rx */ 2544 (void)zyd_write32(sc, ZYD_MAC_RXFILTER, 0); 2545 2546 /* disable interrupts */ 2547 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0); 2548 2549 usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BIN]); 2550 usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BOUT]); 2551 2552 zyd_free_rx_list(sc); 2553 zyd_free_tx_list(sc); 2554 } 2555 2556 Static int 2557 zyd_loadfirmware(struct zyd_softc *sc, u_char *fw, size_t size) 2558 { 2559 usb_device_request_t req; 2560 uint16_t addr; 2561 uint8_t stat; 2562 2563 DPRINTF(("firmware size=%zu\n", size)); 2564 2565 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 2566 req.bRequest = ZYD_DOWNLOADREQ; 2567 USETW(req.wIndex, 0); 2568 2569 addr = ZYD_FIRMWARE_START_ADDR; 2570 while (size > 0) { 2571 #if 0 2572 const int mlen = min(size, 4096); 2573 #else 2574 /* 2575 * XXXX: When the transfer size is 4096 bytes, it is not 2576 * likely to be able to transfer it. 2577 * The cause is port or machine or chip? 2578 */ 2579 const int mlen = min(size, 64); 2580 #endif 2581 2582 DPRINTF(("loading firmware block: len=%d, addr=0x%x\n", mlen, 2583 addr)); 2584 2585 USETW(req.wValue, addr); 2586 USETW(req.wLength, mlen); 2587 if (usbd_do_request(sc->sc_udev, &req, fw) != 0) 2588 return EIO; 2589 2590 addr += mlen / 2; 2591 fw += mlen; 2592 size -= mlen; 2593 } 2594 2595 /* check whether the upload succeeded */ 2596 req.bmRequestType = UT_READ_VENDOR_DEVICE; 2597 req.bRequest = ZYD_DOWNLOADSTS; 2598 USETW(req.wValue, 0); 2599 USETW(req.wIndex, 0); 2600 USETW(req.wLength, sizeof(stat)); 2601 if (usbd_do_request(sc->sc_udev, &req, &stat) != 0) 2602 return EIO; 2603 2604 return (stat & 0x80) ? EIO : 0; 2605 } 2606 2607 Static void 2608 zyd_iter_func(void *arg, struct ieee80211_node *ni) 2609 { 2610 struct zyd_softc *sc = arg; 2611 struct zyd_node *zn = (struct zyd_node *)ni; 2612 2613 ieee80211_amrr_choose(&sc->amrr, ni, &zn->amn); 2614 } 2615 2616 Static void 2617 zyd_amrr_timeout(void *arg) 2618 { 2619 struct zyd_softc *sc = arg; 2620 struct ieee80211com *ic = &sc->sc_ic; 2621 int s; 2622 2623 s = splnet(); 2624 if (ic->ic_opmode == IEEE80211_M_STA) 2625 zyd_iter_func(sc, ic->ic_bss); 2626 else 2627 ieee80211_iterate_nodes(&ic->ic_sta, zyd_iter_func, sc); 2628 splx(s); 2629 2630 callout_reset(&sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc); 2631 } 2632 2633 Static void 2634 zyd_newassoc(struct ieee80211_node *ni, int isnew) 2635 { 2636 struct zyd_softc *sc = ni->ni_ic->ic_ifp->if_softc; 2637 int i; 2638 2639 ieee80211_amrr_node_init(&sc->amrr, &((struct zyd_node *)ni)->amn); 2640 2641 /* set rate to some reasonable initial value */ 2642 for (i = ni->ni_rates.rs_nrates - 1; 2643 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72; 2644 i--); 2645 ni->ni_txrate = i; 2646 } 2647 2648 int 2649 zyd_activate(device_t self, enum devact act) 2650 { 2651 struct zyd_softc *sc = device_private(self); 2652 2653 switch (act) { 2654 case DVACT_DEACTIVATE: 2655 if_deactivate(&sc->sc_if); 2656 return 0; 2657 default: 2658 return EOPNOTSUPP; 2659 } 2660 } 2661