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