145static device_probe_t rum_match; 146static device_attach_t rum_attach; 147static device_detach_t rum_detach; 148 149static usb_callback_t rum_bulk_read_callback; 150static usb_callback_t rum_bulk_write_callback; 151 152static usb_error_t rum_do_request(struct rum_softc *sc, 153 struct usb_device_request *req, void *data); 154static struct ieee80211vap *rum_vap_create(struct ieee80211com *, 155 const char name[IFNAMSIZ], int unit, int opmode, 156 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN], 157 const uint8_t mac[IEEE80211_ADDR_LEN]); 158static void rum_vap_delete(struct ieee80211vap *); 159static void rum_tx_free(struct rum_tx_data *, int); 160static void rum_setup_tx_list(struct rum_softc *); 161static void rum_unsetup_tx_list(struct rum_softc *); 162static int rum_newstate(struct ieee80211vap *, 163 enum ieee80211_state, int); 164static void rum_setup_tx_desc(struct rum_softc *, 165 struct rum_tx_desc *, uint32_t, uint16_t, int, 166 int); 167static int rum_tx_mgt(struct rum_softc *, struct mbuf *, 168 struct ieee80211_node *); 169static int rum_tx_raw(struct rum_softc *, struct mbuf *, 170 struct ieee80211_node *, 171 const struct ieee80211_bpf_params *); 172static int rum_tx_data(struct rum_softc *, struct mbuf *, 173 struct ieee80211_node *); 174static void rum_start(struct ifnet *); 175static int rum_ioctl(struct ifnet *, u_long, caddr_t); 176static void rum_eeprom_read(struct rum_softc *, uint16_t, void *, 177 int); 178static uint32_t rum_read(struct rum_softc *, uint16_t); 179static void rum_read_multi(struct rum_softc *, uint16_t, void *, 180 int); 181static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t); 182static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *, 183 size_t); 184static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t); 185static uint8_t rum_bbp_read(struct rum_softc *, uint8_t); 186static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t); 187static void rum_select_antenna(struct rum_softc *); 188static void rum_enable_mrr(struct rum_softc *); 189static void rum_set_txpreamble(struct rum_softc *); 190static void rum_set_basicrates(struct rum_softc *); 191static void rum_select_band(struct rum_softc *, 192 struct ieee80211_channel *); 193static void rum_set_chan(struct rum_softc *, 194 struct ieee80211_channel *); 195static void rum_enable_tsf_sync(struct rum_softc *); 196static void rum_enable_tsf(struct rum_softc *); 197static void rum_update_slot(struct ifnet *); 198static void rum_set_bssid(struct rum_softc *, const uint8_t *); 199static void rum_set_macaddr(struct rum_softc *, const uint8_t *); 200static void rum_update_mcast(struct ifnet *); 201static void rum_update_promisc(struct ifnet *); 202static void rum_setpromisc(struct rum_softc *); 203static const char *rum_get_rf(int); 204static void rum_read_eeprom(struct rum_softc *); 205static int rum_bbp_init(struct rum_softc *); 206static void rum_init_locked(struct rum_softc *); 207static void rum_init(void *); 208static void rum_stop(struct rum_softc *); 209static void rum_load_microcode(struct rum_softc *, const uint8_t *, 210 size_t); 211static int rum_prepare_beacon(struct rum_softc *, 212 struct ieee80211vap *); 213static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *, 214 const struct ieee80211_bpf_params *); 215static void rum_scan_start(struct ieee80211com *); 216static void rum_scan_end(struct ieee80211com *); 217static void rum_set_channel(struct ieee80211com *); 218static int rum_get_rssi(struct rum_softc *, uint8_t); 219static void rum_ratectl_start(struct rum_softc *, 220 struct ieee80211_node *); 221static void rum_ratectl_timeout(void *); 222static void rum_ratectl_task(void *, int); 223static int rum_pause(struct rum_softc *, int); 224 225static const struct { 226 uint32_t reg; 227 uint32_t val; 228} rum_def_mac[] = { 229 { RT2573_TXRX_CSR0, 0x025fb032 }, 230 { RT2573_TXRX_CSR1, 0x9eaa9eaf }, 231 { RT2573_TXRX_CSR2, 0x8a8b8c8d }, 232 { RT2573_TXRX_CSR3, 0x00858687 }, 233 { RT2573_TXRX_CSR7, 0x2e31353b }, 234 { RT2573_TXRX_CSR8, 0x2a2a2a2c }, 235 { RT2573_TXRX_CSR15, 0x0000000f }, 236 { RT2573_MAC_CSR6, 0x00000fff }, 237 { RT2573_MAC_CSR8, 0x016c030a }, 238 { RT2573_MAC_CSR10, 0x00000718 }, 239 { RT2573_MAC_CSR12, 0x00000004 }, 240 { RT2573_MAC_CSR13, 0x00007f00 }, 241 { RT2573_SEC_CSR0, 0x00000000 }, 242 { RT2573_SEC_CSR1, 0x00000000 }, 243 { RT2573_SEC_CSR5, 0x00000000 }, 244 { RT2573_PHY_CSR1, 0x000023b0 }, 245 { RT2573_PHY_CSR5, 0x00040a06 }, 246 { RT2573_PHY_CSR6, 0x00080606 }, 247 { RT2573_PHY_CSR7, 0x00000408 }, 248 { RT2573_AIFSN_CSR, 0x00002273 }, 249 { RT2573_CWMIN_CSR, 0x00002344 }, 250 { RT2573_CWMAX_CSR, 0x000034aa } 251}; 252 253static const struct { 254 uint8_t reg; 255 uint8_t val; 256} rum_def_bbp[] = { 257 { 3, 0x80 }, 258 { 15, 0x30 }, 259 { 17, 0x20 }, 260 { 21, 0xc8 }, 261 { 22, 0x38 }, 262 { 23, 0x06 }, 263 { 24, 0xfe }, 264 { 25, 0x0a }, 265 { 26, 0x0d }, 266 { 32, 0x0b }, 267 { 34, 0x12 }, 268 { 37, 0x07 }, 269 { 39, 0xf8 }, 270 { 41, 0x60 }, 271 { 53, 0x10 }, 272 { 54, 0x18 }, 273 { 60, 0x10 }, 274 { 61, 0x04 }, 275 { 62, 0x04 }, 276 { 75, 0xfe }, 277 { 86, 0xfe }, 278 { 88, 0xfe }, 279 { 90, 0x0f }, 280 { 99, 0x00 }, 281 { 102, 0x16 }, 282 { 107, 0x04 } 283}; 284 285static const struct rfprog { 286 uint8_t chan; 287 uint32_t r1, r2, r3, r4; 288} rum_rf5226[] = { 289 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 }, 290 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 }, 291 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 }, 292 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 }, 293 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 }, 294 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 }, 295 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 }, 296 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 }, 297 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 }, 298 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 }, 299 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 }, 300 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 }, 301 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 }, 302 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 }, 303 304 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 }, 305 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 }, 306 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 }, 307 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 }, 308 309 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 }, 310 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 }, 311 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 }, 312 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 }, 313 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 }, 314 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 }, 315 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 }, 316 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 }, 317 318 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 }, 319 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 }, 320 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 }, 321 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 }, 322 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 }, 323 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 }, 324 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 }, 325 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 }, 326 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 }, 327 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 }, 328 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 }, 329 330 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 }, 331 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 }, 332 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 }, 333 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 }, 334 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 } 335}, rum_rf5225[] = { 336 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 }, 337 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 }, 338 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 }, 339 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 }, 340 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 }, 341 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 }, 342 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 }, 343 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 }, 344 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 }, 345 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 }, 346 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 }, 347 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 }, 348 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 }, 349 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 }, 350 351 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 }, 352 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 }, 353 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 }, 354 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 }, 355 356 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 }, 357 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 }, 358 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 }, 359 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 }, 360 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 }, 361 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 }, 362 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 }, 363 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 }, 364 365 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 }, 366 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 }, 367 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 }, 368 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 }, 369 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 }, 370 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 }, 371 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 }, 372 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 }, 373 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 }, 374 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 }, 375 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 }, 376 377 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 }, 378 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 }, 379 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 }, 380 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 }, 381 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 } 382}; 383 384static const struct usb_config rum_config[RUM_N_TRANSFER] = { 385 [RUM_BULK_WR] = { 386 .type = UE_BULK, 387 .endpoint = UE_ADDR_ANY, 388 .direction = UE_DIR_OUT, 389 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8), 390 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 391 .callback = rum_bulk_write_callback, 392 .timeout = 5000, /* ms */ 393 }, 394 [RUM_BULK_RD] = { 395 .type = UE_BULK, 396 .endpoint = UE_ADDR_ANY, 397 .direction = UE_DIR_IN, 398 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE), 399 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 400 .callback = rum_bulk_read_callback, 401 }, 402}; 403 404static int 405rum_match(device_t self) 406{ 407 struct usb_attach_arg *uaa = device_get_ivars(self); 408 409 if (uaa->usb_mode != USB_MODE_HOST) 410 return (ENXIO); 411 if (uaa->info.bConfigIndex != 0) 412 return (ENXIO); 413 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX) 414 return (ENXIO); 415 416 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa)); 417} 418 419static int 420rum_attach(device_t self) 421{ 422 struct usb_attach_arg *uaa = device_get_ivars(self); 423 struct rum_softc *sc = device_get_softc(self); 424 struct ieee80211com *ic; 425 struct ifnet *ifp; 426 uint8_t iface_index, bands; 427 uint32_t tmp; 428 int error, ntries; 429 430 device_set_usb_desc(self); 431 sc->sc_udev = uaa->device; 432 sc->sc_dev = self; 433 434 mtx_init(&sc->sc_mtx, device_get_nameunit(self), 435 MTX_NETWORK_LOCK, MTX_DEF); 436 437 iface_index = RT2573_IFACE_INDEX; 438 error = usbd_transfer_setup(uaa->device, &iface_index, 439 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx); 440 if (error) { 441 device_printf(self, "could not allocate USB transfers, " 442 "err=%s\n", usbd_errstr(error)); 443 goto detach; 444 } 445 446 RUM_LOCK(sc); 447 /* retrieve RT2573 rev. no */ 448 for (ntries = 0; ntries < 100; ntries++) { 449 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0) 450 break; 451 if (rum_pause(sc, hz / 100)) 452 break; 453 } 454 if (ntries == 100) { 455 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n"); 456 RUM_UNLOCK(sc); 457 goto detach; 458 } 459 460 /* retrieve MAC address and various other things from EEPROM */ 461 rum_read_eeprom(sc); 462 463 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n", 464 tmp, rum_get_rf(sc->rf_rev)); 465 466 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode)); 467 RUM_UNLOCK(sc); 468 469 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 470 if (ifp == NULL) { 471 device_printf(sc->sc_dev, "can not if_alloc()\n"); 472 goto detach; 473 } 474 ic = ifp->if_l2com; 475 476 ifp->if_softc = sc; 477 if_initname(ifp, "rum", device_get_unit(sc->sc_dev)); 478 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 479 ifp->if_init = rum_init; 480 ifp->if_ioctl = rum_ioctl; 481 ifp->if_start = rum_start; 482 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 483 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 484 IFQ_SET_READY(&ifp->if_snd); 485 486 ic->ic_ifp = ifp; 487 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 488 489 /* set device capabilities */ 490 ic->ic_caps = 491 IEEE80211_C_STA /* station mode supported */ 492 | IEEE80211_C_IBSS /* IBSS mode supported */ 493 | IEEE80211_C_MONITOR /* monitor mode supported */ 494 | IEEE80211_C_HOSTAP /* HostAp mode supported */ 495 | IEEE80211_C_TXPMGT /* tx power management */ 496 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 497 | IEEE80211_C_SHSLOT /* short slot time supported */ 498 | IEEE80211_C_BGSCAN /* bg scanning supported */ 499 | IEEE80211_C_WPA /* 802.11i */ 500 | IEEE80211_C_RATECTL /* use ratectl */ 501 ; 502 503 bands = 0; 504 setbit(&bands, IEEE80211_MODE_11B); 505 setbit(&bands, IEEE80211_MODE_11G); 506 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) 507 setbit(&bands, IEEE80211_MODE_11A); 508 ieee80211_init_channels(ic, NULL, &bands); 509 510 ieee80211_ifattach(ic, sc->sc_bssid); 511 ic->ic_update_promisc = rum_update_promisc; 512 ic->ic_raw_xmit = rum_raw_xmit; 513 ic->ic_scan_start = rum_scan_start; 514 ic->ic_scan_end = rum_scan_end; 515 ic->ic_set_channel = rum_set_channel; 516 517 ic->ic_vap_create = rum_vap_create; 518 ic->ic_vap_delete = rum_vap_delete; 519 ic->ic_update_mcast = rum_update_mcast; 520 521 ieee80211_radiotap_attach(ic, 522 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 523 RT2573_TX_RADIOTAP_PRESENT, 524 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 525 RT2573_RX_RADIOTAP_PRESENT); 526 527 if (bootverbose) 528 ieee80211_announce(ic); 529 530 return (0); 531 532detach: 533 rum_detach(self); 534 return (ENXIO); /* failure */ 535} 536 537static int 538rum_detach(device_t self) 539{ 540 struct rum_softc *sc = device_get_softc(self); 541 struct ifnet *ifp = sc->sc_ifp; 542 struct ieee80211com *ic; 543 544 /* stop all USB transfers */ 545 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER); 546 547 /* free TX list, if any */ 548 RUM_LOCK(sc); 549 rum_unsetup_tx_list(sc); 550 RUM_UNLOCK(sc); 551 552 if (ifp) { 553 ic = ifp->if_l2com; 554 ieee80211_ifdetach(ic); 555 if_free(ifp); 556 } 557 mtx_destroy(&sc->sc_mtx); 558 559 return (0); 560} 561 562static usb_error_t 563rum_do_request(struct rum_softc *sc, 564 struct usb_device_request *req, void *data) 565{ 566 usb_error_t err; 567 int ntries = 10; 568 569 while (ntries--) { 570 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, 571 req, data, 0, NULL, 250 /* ms */); 572 if (err == 0) 573 break; 574 575 DPRINTFN(1, "Control request failed, %s (retrying)\n", 576 usbd_errstr(err)); 577 if (rum_pause(sc, hz / 100)) 578 break; 579 } 580 return (err); 581} 582 583static struct ieee80211vap * 584rum_vap_create(struct ieee80211com *ic, 585 const char name[IFNAMSIZ], int unit, int opmode, int flags, 586 const uint8_t bssid[IEEE80211_ADDR_LEN], 587 const uint8_t mac[IEEE80211_ADDR_LEN]) 588{ 589 struct rum_softc *sc = ic->ic_ifp->if_softc; 590 struct rum_vap *rvp; 591 struct ieee80211vap *vap; 592 593 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 594 return NULL; 595 rvp = (struct rum_vap *) malloc(sizeof(struct rum_vap), 596 M_80211_VAP, M_NOWAIT | M_ZERO); 597 if (rvp == NULL) 598 return NULL; 599 vap = &rvp->vap; 600 /* enable s/w bmiss handling for sta mode */ 601 ieee80211_vap_setup(ic, vap, name, unit, opmode, 602 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac); 603 604 /* override state transition machine */ 605 rvp->newstate = vap->iv_newstate; 606 vap->iv_newstate = rum_newstate; 607 608 usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0); 609 TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp); 610 ieee80211_ratectl_init(vap); 611 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */); 612 /* complete setup */ 613 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status); 614 ic->ic_opmode = opmode; 615 return vap; 616} 617 618static void 619rum_vap_delete(struct ieee80211vap *vap) 620{ 621 struct rum_vap *rvp = RUM_VAP(vap); 622 struct ieee80211com *ic = vap->iv_ic; 623 624 usb_callout_drain(&rvp->ratectl_ch); 625 ieee80211_draintask(ic, &rvp->ratectl_task); 626 ieee80211_ratectl_deinit(vap); 627 ieee80211_vap_detach(vap); 628 free(rvp, M_80211_VAP); 629} 630 631static void 632rum_tx_free(struct rum_tx_data *data, int txerr) 633{ 634 struct rum_softc *sc = data->sc; 635 636 if (data->m != NULL) { 637 if (data->m->m_flags & M_TXCB) 638 ieee80211_process_callback(data->ni, data->m, 639 txerr ? ETIMEDOUT : 0); 640 m_freem(data->m); 641 data->m = NULL; 642 643 ieee80211_free_node(data->ni); 644 data->ni = NULL; 645 } 646 STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 647 sc->tx_nfree++; 648} 649 650static void 651rum_setup_tx_list(struct rum_softc *sc) 652{ 653 struct rum_tx_data *data; 654 int i; 655 656 sc->tx_nfree = 0; 657 STAILQ_INIT(&sc->tx_q); 658 STAILQ_INIT(&sc->tx_free); 659 660 for (i = 0; i < RUM_TX_LIST_COUNT; i++) { 661 data = &sc->tx_data[i]; 662 663 data->sc = sc; 664 STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 665 sc->tx_nfree++; 666 } 667} 668 669static void 670rum_unsetup_tx_list(struct rum_softc *sc) 671{ 672 struct rum_tx_data *data; 673 int i; 674 675 /* make sure any subsequent use of the queues will fail */ 676 sc->tx_nfree = 0; 677 STAILQ_INIT(&sc->tx_q); 678 STAILQ_INIT(&sc->tx_free); 679 680 /* free up all node references and mbufs */ 681 for (i = 0; i < RUM_TX_LIST_COUNT; i++) { 682 data = &sc->tx_data[i]; 683 684 if (data->m != NULL) { 685 m_freem(data->m); 686 data->m = NULL; 687 } 688 if (data->ni != NULL) { 689 ieee80211_free_node(data->ni); 690 data->ni = NULL; 691 } 692 } 693} 694 695static int 696rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 697{ 698 struct rum_vap *rvp = RUM_VAP(vap); 699 struct ieee80211com *ic = vap->iv_ic; 700 struct rum_softc *sc = ic->ic_ifp->if_softc; 701 const struct ieee80211_txparam *tp; 702 enum ieee80211_state ostate; 703 struct ieee80211_node *ni; 704 uint32_t tmp; 705 706 ostate = vap->iv_state; 707 DPRINTF("%s -> %s\n", 708 ieee80211_state_name[ostate], 709 ieee80211_state_name[nstate]); 710 711 IEEE80211_UNLOCK(ic); 712 RUM_LOCK(sc); 713 usb_callout_stop(&rvp->ratectl_ch); 714 715 switch (nstate) { 716 case IEEE80211_S_INIT: 717 if (ostate == IEEE80211_S_RUN) { 718 /* abort TSF synchronization */ 719 tmp = rum_read(sc, RT2573_TXRX_CSR9); 720 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff); 721 } 722 break; 723 724 case IEEE80211_S_RUN: 725 ni = vap->iv_bss; 726 727 if (vap->iv_opmode != IEEE80211_M_MONITOR) { 728 rum_update_slot(ic->ic_ifp); 729 rum_enable_mrr(sc); 730 rum_set_txpreamble(sc); 731 rum_set_basicrates(sc); 732 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid); 733 rum_set_bssid(sc, sc->sc_bssid); 734 } 735 736 if (vap->iv_opmode == IEEE80211_M_HOSTAP || 737 vap->iv_opmode == IEEE80211_M_IBSS) 738 rum_prepare_beacon(sc, vap); 739 740 if (vap->iv_opmode != IEEE80211_M_MONITOR) 741 rum_enable_tsf_sync(sc); 742 else 743 rum_enable_tsf(sc); 744 745 /* enable automatic rate adaptation */ 746 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 747 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) 748 rum_ratectl_start(sc, ni); 749 break; 750 default: 751 break; 752 } 753 RUM_UNLOCK(sc); 754 IEEE80211_LOCK(ic); 755 return (rvp->newstate(vap, nstate, arg)); 756} 757 758static void 759rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 760{ 761 struct rum_softc *sc = usbd_xfer_softc(xfer); 762 struct ifnet *ifp = sc->sc_ifp; 763 struct ieee80211vap *vap; 764 struct rum_tx_data *data; 765 struct mbuf *m; 766 struct usb_page_cache *pc; 767 unsigned int len; 768 int actlen, sumlen; 769 770 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 771 772 switch (USB_GET_STATE(xfer)) { 773 case USB_ST_TRANSFERRED: 774 DPRINTFN(11, "transfer complete, %d bytes\n", actlen); 775 776 /* free resources */ 777 data = usbd_xfer_get_priv(xfer); 778 rum_tx_free(data, 0); 779 usbd_xfer_set_priv(xfer, NULL); 780 781 ifp->if_opackets++; 782 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 783 784 /* FALLTHROUGH */ 785 case USB_ST_SETUP: 786tr_setup: 787 data = STAILQ_FIRST(&sc->tx_q); 788 if (data) { 789 STAILQ_REMOVE_HEAD(&sc->tx_q, next); 790 m = data->m; 791 792 if (m->m_pkthdr.len > (MCLBYTES + RT2573_TX_DESC_SIZE)) { 793 DPRINTFN(0, "data overflow, %u bytes\n", 794 m->m_pkthdr.len); 795 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE); 796 } 797 pc = usbd_xfer_get_frame(xfer, 0); 798 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE); 799 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0, 800 m->m_pkthdr.len); 801 802 vap = data->ni->ni_vap; 803 if (ieee80211_radiotap_active_vap(vap)) { 804 struct rum_tx_radiotap_header *tap = &sc->sc_txtap; 805 806 tap->wt_flags = 0; 807 tap->wt_rate = data->rate; 808 tap->wt_antenna = sc->tx_ant; 809 810 ieee80211_radiotap_tx(vap, m); 811 } 812 813 /* align end on a 4-bytes boundary */ 814 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3; 815 if ((len % 64) == 0) 816 len += 4; 817 818 DPRINTFN(11, "sending frame len=%u xferlen=%u\n", 819 m->m_pkthdr.len, len); 820 821 usbd_xfer_set_frame_len(xfer, 0, len); 822 usbd_xfer_set_priv(xfer, data); 823 824 usbd_transfer_submit(xfer); 825 } 826 RUM_UNLOCK(sc); 827 rum_start(ifp); 828 RUM_LOCK(sc); 829 break; 830 831 default: /* Error */ 832 DPRINTFN(11, "transfer error, %s\n", 833 usbd_errstr(error)); 834 835 ifp->if_oerrors++; 836 data = usbd_xfer_get_priv(xfer); 837 if (data != NULL) { 838 rum_tx_free(data, error); 839 usbd_xfer_set_priv(xfer, NULL); 840 } 841 842 if (error != USB_ERR_CANCELLED) { 843 if (error == USB_ERR_TIMEOUT) 844 device_printf(sc->sc_dev, "device timeout\n"); 845 846 /* 847 * Try to clear stall first, also if other 848 * errors occur, hence clearing stall 849 * introduces a 50 ms delay: 850 */ 851 usbd_xfer_set_stall(xfer); 852 goto tr_setup; 853 } 854 break; 855 } 856} 857 858static void 859rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 860{ 861 struct rum_softc *sc = usbd_xfer_softc(xfer); 862 struct ifnet *ifp = sc->sc_ifp; 863 struct ieee80211com *ic = ifp->if_l2com; 864 struct ieee80211_node *ni; 865 struct mbuf *m = NULL; 866 struct usb_page_cache *pc; 867 uint32_t flags; 868 uint8_t rssi = 0; 869 int len; 870 871 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 872 873 switch (USB_GET_STATE(xfer)) { 874 case USB_ST_TRANSFERRED: 875 876 DPRINTFN(15, "rx done, actlen=%d\n", len); 877 878 if (len < RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN) { 879 DPRINTF("%s: xfer too short %d\n", 880 device_get_nameunit(sc->sc_dev), len); 881 ifp->if_ierrors++; 882 goto tr_setup; 883 } 884 885 len -= RT2573_RX_DESC_SIZE; 886 pc = usbd_xfer_get_frame(xfer, 0); 887 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE); 888 889 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi); 890 flags = le32toh(sc->sc_rx_desc.flags); 891 if (flags & RT2573_RX_CRC_ERROR) { 892 /* 893 * This should not happen since we did not 894 * request to receive those frames when we 895 * filled RUM_TXRX_CSR2: 896 */ 897 DPRINTFN(5, "PHY or CRC error\n"); 898 ifp->if_ierrors++; 899 goto tr_setup; 900 } 901 902 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 903 if (m == NULL) { 904 DPRINTF("could not allocate mbuf\n"); 905 ifp->if_ierrors++; 906 goto tr_setup; 907 } 908 usbd_copy_out(pc, RT2573_RX_DESC_SIZE, 909 mtod(m, uint8_t *), len); 910 911 /* finalize mbuf */ 912 m->m_pkthdr.rcvif = ifp; 913 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff; 914 915 if (ieee80211_radiotap_active(ic)) { 916 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap; 917 918 /* XXX read tsf */ 919 tap->wr_flags = 0; 920 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate, 921 (flags & RT2573_RX_OFDM) ? 922 IEEE80211_T_OFDM : IEEE80211_T_CCK); 923 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi; 924 tap->wr_antnoise = RT2573_NOISE_FLOOR; 925 tap->wr_antenna = sc->rx_ant; 926 } 927 /* FALLTHROUGH */ 928 case USB_ST_SETUP: 929tr_setup: 930 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 931 usbd_transfer_submit(xfer); 932 933 /* 934 * At the end of a USB callback it is always safe to unlock 935 * the private mutex of a device! That is why we do the 936 * "ieee80211_input" here, and not some lines up! 937 */ 938 RUM_UNLOCK(sc); 939 if (m) { 940 ni = ieee80211_find_rxnode(ic, 941 mtod(m, struct ieee80211_frame_min *)); 942 if (ni != NULL) { 943 (void) ieee80211_input(ni, m, rssi, 944 RT2573_NOISE_FLOOR); 945 ieee80211_free_node(ni); 946 } else 947 (void) ieee80211_input_all(ic, m, rssi, 948 RT2573_NOISE_FLOOR); 949 } 950 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 && 951 !IFQ_IS_EMPTY(&ifp->if_snd)) 952 rum_start(ifp); 953 RUM_LOCK(sc); 954 return; 955 956 default: /* Error */ 957 if (error != USB_ERR_CANCELLED) { 958 /* try to clear stall first */ 959 usbd_xfer_set_stall(xfer); 960 goto tr_setup; 961 } 962 return; 963 } 964} 965 966static uint8_t 967rum_plcp_signal(int rate) 968{ 969 switch (rate) { 970 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 971 case 12: return 0xb; 972 case 18: return 0xf; 973 case 24: return 0xa; 974 case 36: return 0xe; 975 case 48: return 0x9; 976 case 72: return 0xd; 977 case 96: return 0x8; 978 case 108: return 0xc; 979 980 /* CCK rates (NB: not IEEE std, device-specific) */ 981 case 2: return 0x0; 982 case 4: return 0x1; 983 case 11: return 0x2; 984 case 22: return 0x3; 985 } 986 return 0xff; /* XXX unsupported/unknown rate */ 987} 988 989static void 990rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc, 991 uint32_t flags, uint16_t xflags, int len, int rate) 992{ 993 struct ifnet *ifp = sc->sc_ifp; 994 struct ieee80211com *ic = ifp->if_l2com; 995 uint16_t plcp_length; 996 int remainder; 997 998 desc->flags = htole32(flags); 999 desc->flags |= htole32(RT2573_TX_VALID); 1000 desc->flags |= htole32(len << 16); 1001 1002 desc->xflags = htole16(xflags); 1003 1004 desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) | 1005 RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10)); 1006 1007 /* setup PLCP fields */ 1008 desc->plcp_signal = rum_plcp_signal(rate); 1009 desc->plcp_service = 4; 1010 1011 len += IEEE80211_CRC_LEN; 1012 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) { 1013 desc->flags |= htole32(RT2573_TX_OFDM); 1014 1015 plcp_length = len & 0xfff; 1016 desc->plcp_length_hi = plcp_length >> 6; 1017 desc->plcp_length_lo = plcp_length & 0x3f; 1018 } else { 1019 plcp_length = (16 * len + rate - 1) / rate; 1020 if (rate == 22) { 1021 remainder = (16 * len) % 22; 1022 if (remainder != 0 && remainder < 7) 1023 desc->plcp_service |= RT2573_PLCP_LENGEXT; 1024 } 1025 desc->plcp_length_hi = plcp_length >> 8; 1026 desc->plcp_length_lo = plcp_length & 0xff; 1027 1028 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1029 desc->plcp_signal |= 0x08; 1030 } 1031} 1032 1033static int 1034rum_sendprot(struct rum_softc *sc, 1035 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 1036{ 1037 struct ieee80211com *ic = ni->ni_ic; 1038 const struct ieee80211_frame *wh; 1039 struct rum_tx_data *data; 1040 struct mbuf *mprot; 1041 int protrate, ackrate, pktlen, flags, isshort; 1042 uint16_t dur; 1043 1044 RUM_LOCK_ASSERT(sc, MA_OWNED); 1045 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 1046 ("protection %d", prot)); 1047 1048 wh = mtod(m, const struct ieee80211_frame *); 1049 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 1050 1051 protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 1052 ackrate = ieee80211_ack_rate(ic->ic_rt, rate); 1053 1054 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 1055 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort) 1056 + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1057 flags = RT2573_TX_MORE_FRAG; 1058 if (prot == IEEE80211_PROT_RTSCTS) { 1059 /* NB: CTS is the same size as an ACK */ 1060 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1061 flags |= RT2573_TX_NEED_ACK; 1062 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 1063 } else { 1064 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 1065 } 1066 if (mprot == NULL) { 1067 /* XXX stat + msg */ 1068 return (ENOBUFS); 1069 } 1070 data = STAILQ_FIRST(&sc->tx_free); 1071 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1072 sc->tx_nfree--; 1073 1074 data->m = mprot; 1075 data->ni = ieee80211_ref_node(ni); 1076 data->rate = protrate; 1077 rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate); 1078 1079 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1080 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1081 1082 return 0; 1083} 1084 1085static int 1086rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1087{ 1088 struct ieee80211vap *vap = ni->ni_vap; 1089 struct ifnet *ifp = sc->sc_ifp; 1090 struct ieee80211com *ic = ifp->if_l2com; 1091 struct rum_tx_data *data; 1092 struct ieee80211_frame *wh; 1093 const struct ieee80211_txparam *tp; 1094 struct ieee80211_key *k; 1095 uint32_t flags = 0; 1096 uint16_t dur; 1097 1098 RUM_LOCK_ASSERT(sc, MA_OWNED); 1099 1100 data = STAILQ_FIRST(&sc->tx_free); 1101 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1102 sc->tx_nfree--; 1103 1104 wh = mtod(m0, struct ieee80211_frame *); 1105 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1106 k = ieee80211_crypto_encap(ni, m0); 1107 if (k == NULL) { 1108 m_freem(m0); 1109 return ENOBUFS; 1110 } 1111 wh = mtod(m0, struct ieee80211_frame *); 1112 } 1113 1114 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1115 1116 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1117 flags |= RT2573_TX_NEED_ACK; 1118 1119 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate, 1120 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1121 *(uint16_t *)wh->i_dur = htole16(dur); 1122 1123 /* tell hardware to add timestamp for probe responses */ 1124 if ((wh->i_fc[0] & 1125 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 1126 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) 1127 flags |= RT2573_TX_TIMESTAMP; 1128 } 1129 1130 data->m = m0; 1131 data->ni = ni; 1132 data->rate = tp->mgmtrate; 1133 1134 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate); 1135 1136 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", 1137 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate); 1138 1139 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1140 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1141 1142 return (0); 1143} 1144 1145static int 1146rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1147 const struct ieee80211_bpf_params *params) 1148{ 1149 struct ieee80211com *ic = ni->ni_ic; 1150 struct rum_tx_data *data; 1151 uint32_t flags; 1152 int rate, error; 1153 1154 RUM_LOCK_ASSERT(sc, MA_OWNED); 1155 KASSERT(params != NULL, ("no raw xmit params")); 1156 1157 rate = params->ibp_rate0; 1158 if (!ieee80211_isratevalid(ic->ic_rt, rate)) { 1159 m_freem(m0); 1160 return EINVAL; 1161 } 1162 flags = 0; 1163 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 1164 flags |= RT2573_TX_NEED_ACK; 1165 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 1166 error = rum_sendprot(sc, m0, ni, 1167 params->ibp_flags & IEEE80211_BPF_RTS ? 1168 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 1169 rate); 1170 if (error || sc->tx_nfree == 0) { 1171 m_freem(m0); 1172 return ENOBUFS; 1173 } 1174 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1175 } 1176 1177 data = STAILQ_FIRST(&sc->tx_free); 1178 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1179 sc->tx_nfree--; 1180 1181 data->m = m0; 1182 data->ni = ni; 1183 data->rate = rate; 1184 1185 /* XXX need to setup descriptor ourself */ 1186 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate); 1187 1188 DPRINTFN(10, "sending raw frame len=%u rate=%u\n", 1189 m0->m_pkthdr.len, rate); 1190 1191 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1192 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1193 1194 return 0; 1195} 1196 1197static int 1198rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1199{ 1200 struct ieee80211vap *vap = ni->ni_vap; 1201 struct ifnet *ifp = sc->sc_ifp; 1202 struct ieee80211com *ic = ifp->if_l2com; 1203 struct rum_tx_data *data; 1204 struct ieee80211_frame *wh; 1205 const struct ieee80211_txparam *tp; 1206 struct ieee80211_key *k; 1207 uint32_t flags = 0; 1208 uint16_t dur; 1209 int error, rate; 1210 1211 RUM_LOCK_ASSERT(sc, MA_OWNED); 1212 1213 wh = mtod(m0, struct ieee80211_frame *); 1214 1215 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 1216 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1217 rate = tp->mcastrate; 1218 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 1219 rate = tp->ucastrate; 1220 else 1221 rate = ni->ni_txrate; 1222 1223 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1224 k = ieee80211_crypto_encap(ni, m0); 1225 if (k == NULL) { 1226 m_freem(m0); 1227 return ENOBUFS; 1228 } 1229 1230 /* packet header may have moved, reset our local pointer */ 1231 wh = mtod(m0, struct ieee80211_frame *); 1232 } 1233 1234 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1235 int prot = IEEE80211_PROT_NONE; 1236 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 1237 prot = IEEE80211_PROT_RTSCTS; 1238 else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1239 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) 1240 prot = ic->ic_protmode; 1241 if (prot != IEEE80211_PROT_NONE) { 1242 error = rum_sendprot(sc, m0, ni, prot, rate); 1243 if (error || sc->tx_nfree == 0) { 1244 m_freem(m0); 1245 return ENOBUFS; 1246 } 1247 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1248 } 1249 } 1250 1251 data = STAILQ_FIRST(&sc->tx_free); 1252 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1253 sc->tx_nfree--; 1254 1255 data->m = m0; 1256 data->ni = ni; 1257 data->rate = rate; 1258 1259 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1260 flags |= RT2573_TX_NEED_ACK; 1261 flags |= RT2573_TX_MORE_FRAG; 1262 1263 dur = ieee80211_ack_duration(ic->ic_rt, rate, 1264 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1265 *(uint16_t *)wh->i_dur = htole16(dur); 1266 } 1267 1268 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate); 1269 1270 DPRINTFN(10, "sending frame len=%d rate=%d\n", 1271 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate); 1272 1273 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1274 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1275 1276 return 0; 1277} 1278 1279static void 1280rum_start(struct ifnet *ifp) 1281{ 1282 struct rum_softc *sc = ifp->if_softc; 1283 struct ieee80211_node *ni; 1284 struct mbuf *m; 1285 1286 RUM_LOCK(sc); 1287 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1288 RUM_UNLOCK(sc); 1289 return; 1290 } 1291 for (;;) { 1292 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1293 if (m == NULL) 1294 break; 1295 if (sc->tx_nfree < RUM_TX_MINFREE) { 1296 IFQ_DRV_PREPEND(&ifp->if_snd, m); 1297 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1298 break; 1299 } 1300 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1301 if (rum_tx_data(sc, m, ni) != 0) { 1302 ieee80211_free_node(ni); 1303 ifp->if_oerrors++; 1304 break; 1305 } 1306 } 1307 RUM_UNLOCK(sc); 1308} 1309 1310static int 1311rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1312{ 1313 struct rum_softc *sc = ifp->if_softc; 1314 struct ieee80211com *ic = ifp->if_l2com; 1315 struct ifreq *ifr = (struct ifreq *) data; 1316 int error = 0, startall = 0; 1317 1318 switch (cmd) { 1319 case SIOCSIFFLAGS: 1320 RUM_LOCK(sc); 1321 if (ifp->if_flags & IFF_UP) { 1322 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1323 rum_init_locked(sc); 1324 startall = 1; 1325 } else 1326 rum_setpromisc(sc); 1327 } else { 1328 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1329 rum_stop(sc); 1330 } 1331 RUM_UNLOCK(sc); 1332 if (startall) 1333 ieee80211_start_all(ic); 1334 break; 1335 case SIOCGIFMEDIA: 1336 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 1337 break; 1338 case SIOCGIFADDR: 1339 error = ether_ioctl(ifp, cmd, data); 1340 break; 1341 default: 1342 error = EINVAL; 1343 break; 1344 } 1345 return error; 1346} 1347 1348static void 1349rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len) 1350{ 1351 struct usb_device_request req; 1352 usb_error_t error; 1353 1354 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1355 req.bRequest = RT2573_READ_EEPROM; 1356 USETW(req.wValue, 0); 1357 USETW(req.wIndex, addr); 1358 USETW(req.wLength, len); 1359 1360 error = rum_do_request(sc, &req, buf); 1361 if (error != 0) { 1362 device_printf(sc->sc_dev, "could not read EEPROM: %s\n", 1363 usbd_errstr(error)); 1364 } 1365} 1366 1367static uint32_t 1368rum_read(struct rum_softc *sc, uint16_t reg) 1369{ 1370 uint32_t val; 1371 1372 rum_read_multi(sc, reg, &val, sizeof val); 1373 1374 return le32toh(val); 1375} 1376 1377static void 1378rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len) 1379{ 1380 struct usb_device_request req; 1381 usb_error_t error; 1382 1383 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1384 req.bRequest = RT2573_READ_MULTI_MAC; 1385 USETW(req.wValue, 0); 1386 USETW(req.wIndex, reg); 1387 USETW(req.wLength, len); 1388 1389 error = rum_do_request(sc, &req, buf); 1390 if (error != 0) { 1391 device_printf(sc->sc_dev, 1392 "could not multi read MAC register: %s\n", 1393 usbd_errstr(error)); 1394 } 1395} 1396 1397static usb_error_t 1398rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val) 1399{ 1400 uint32_t tmp = htole32(val); 1401 1402 return (rum_write_multi(sc, reg, &tmp, sizeof tmp)); 1403} 1404 1405static usb_error_t 1406rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len) 1407{ 1408 struct usb_device_request req; 1409 usb_error_t error; 1410 1411 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1412 req.bRequest = RT2573_WRITE_MULTI_MAC; 1413 USETW(req.wValue, 0); 1414 USETW(req.wIndex, reg); 1415 USETW(req.wLength, len); 1416 1417 error = rum_do_request(sc, &req, buf); 1418 if (error != 0) { 1419 device_printf(sc->sc_dev, 1420 "could not multi write MAC register: %s\n", 1421 usbd_errstr(error)); 1422 } 1423 return (error); 1424} 1425 1426static void 1427rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val) 1428{ 1429 uint32_t tmp; 1430 int ntries; 1431 1432 DPRINTFN(2, "reg=0x%08x\n", reg); 1433 1434 for (ntries = 0; ntries < 100; ntries++) { 1435 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1436 break; 1437 if (rum_pause(sc, hz / 100)) 1438 break; 1439 } 1440 if (ntries == 100) { 1441 device_printf(sc->sc_dev, "could not write to BBP\n"); 1442 return; 1443 } 1444 1445 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val; 1446 rum_write(sc, RT2573_PHY_CSR3, tmp); 1447} 1448 1449static uint8_t 1450rum_bbp_read(struct rum_softc *sc, uint8_t reg) 1451{ 1452 uint32_t val; 1453 int ntries; 1454 1455 DPRINTFN(2, "reg=0x%08x\n", reg); 1456 1457 for (ntries = 0; ntries < 100; ntries++) { 1458 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1459 break; 1460 if (rum_pause(sc, hz / 100)) 1461 break; 1462 } 1463 if (ntries == 100) { 1464 device_printf(sc->sc_dev, "could not read BBP\n"); 1465 return 0; 1466 } 1467 1468 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8; 1469 rum_write(sc, RT2573_PHY_CSR3, val); 1470 1471 for (ntries = 0; ntries < 100; ntries++) { 1472 val = rum_read(sc, RT2573_PHY_CSR3); 1473 if (!(val & RT2573_BBP_BUSY)) 1474 return val & 0xff; 1475 if (rum_pause(sc, hz / 100)) 1476 break; 1477 } 1478 1479 device_printf(sc->sc_dev, "could not read BBP\n"); 1480 return 0; 1481} 1482 1483static void 1484rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val) 1485{ 1486 uint32_t tmp; 1487 int ntries; 1488 1489 for (ntries = 0; ntries < 100; ntries++) { 1490 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY)) 1491 break; 1492 if (rum_pause(sc, hz / 100)) 1493 break; 1494 } 1495 if (ntries == 100) { 1496 device_printf(sc->sc_dev, "could not write to RF\n"); 1497 return; 1498 } 1499 1500 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 | 1501 (reg & 3); 1502 rum_write(sc, RT2573_PHY_CSR4, tmp); 1503 1504 /* remember last written value in sc */ 1505 sc->rf_regs[reg] = val; 1506 1507 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff); 1508} 1509 1510static void 1511rum_select_antenna(struct rum_softc *sc) 1512{ 1513 uint8_t bbp4, bbp77; 1514 uint32_t tmp; 1515 1516 bbp4 = rum_bbp_read(sc, 4); 1517 bbp77 = rum_bbp_read(sc, 77); 1518 1519 /* TBD */ 1520 1521 /* make sure Rx is disabled before switching antenna */ 1522 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1523 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 1524 1525 rum_bbp_write(sc, 4, bbp4); 1526 rum_bbp_write(sc, 77, bbp77); 1527 1528 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1529} 1530 1531/* 1532 * Enable multi-rate retries for frames sent at OFDM rates. 1533 * In 802.11b/g mode, allow fallback to CCK rates. 1534 */ 1535static void 1536rum_enable_mrr(struct rum_softc *sc) 1537{ 1538 struct ifnet *ifp = sc->sc_ifp; 1539 struct ieee80211com *ic = ifp->if_l2com; 1540 uint32_t tmp; 1541 1542 tmp = rum_read(sc, RT2573_TXRX_CSR4); 1543 1544 tmp &= ~RT2573_MRR_CCK_FALLBACK; 1545 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) 1546 tmp |= RT2573_MRR_CCK_FALLBACK; 1547 tmp |= RT2573_MRR_ENABLED; 1548 1549 rum_write(sc, RT2573_TXRX_CSR4, tmp); 1550} 1551 1552static void 1553rum_set_txpreamble(struct rum_softc *sc) 1554{ 1555 struct ifnet *ifp = sc->sc_ifp; 1556 struct ieee80211com *ic = ifp->if_l2com; 1557 uint32_t tmp; 1558 1559 tmp = rum_read(sc, RT2573_TXRX_CSR4); 1560 1561 tmp &= ~RT2573_SHORT_PREAMBLE; 1562 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1563 tmp |= RT2573_SHORT_PREAMBLE; 1564 1565 rum_write(sc, RT2573_TXRX_CSR4, tmp); 1566} 1567 1568static void 1569rum_set_basicrates(struct rum_softc *sc) 1570{ 1571 struct ifnet *ifp = sc->sc_ifp; 1572 struct ieee80211com *ic = ifp->if_l2com; 1573 1574 /* update basic rate set */ 1575 if (ic->ic_curmode == IEEE80211_MODE_11B) { 1576 /* 11b basic rates: 1, 2Mbps */ 1577 rum_write(sc, RT2573_TXRX_CSR5, 0x3); 1578 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) { 1579 /* 11a basic rates: 6, 12, 24Mbps */ 1580 rum_write(sc, RT2573_TXRX_CSR5, 0x150); 1581 } else { 1582 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */ 1583 rum_write(sc, RT2573_TXRX_CSR5, 0xf); 1584 } 1585} 1586 1587/* 1588 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference 1589 * driver. 1590 */ 1591static void 1592rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c) 1593{ 1594 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104; 1595 uint32_t tmp; 1596 1597 /* update all BBP registers that depend on the band */ 1598 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c; 1599 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48; 1600 if (IEEE80211_IS_CHAN_5GHZ(c)) { 1601 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c; 1602 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10; 1603 } 1604 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1605 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1606 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10; 1607 } 1608 1609 sc->bbp17 = bbp17; 1610 rum_bbp_write(sc, 17, bbp17); 1611 rum_bbp_write(sc, 96, bbp96); 1612 rum_bbp_write(sc, 104, bbp104); 1613 1614 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1615 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1616 rum_bbp_write(sc, 75, 0x80); 1617 rum_bbp_write(sc, 86, 0x80); 1618 rum_bbp_write(sc, 88, 0x80); 1619 } 1620 1621 rum_bbp_write(sc, 35, bbp35); 1622 rum_bbp_write(sc, 97, bbp97); 1623 rum_bbp_write(sc, 98, bbp98); 1624 1625 tmp = rum_read(sc, RT2573_PHY_CSR0); 1626 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ); 1627 if (IEEE80211_IS_CHAN_2GHZ(c)) 1628 tmp |= RT2573_PA_PE_2GHZ; 1629 else 1630 tmp |= RT2573_PA_PE_5GHZ; 1631 rum_write(sc, RT2573_PHY_CSR0, tmp); 1632} 1633 1634static void 1635rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c) 1636{ 1637 struct ifnet *ifp = sc->sc_ifp; 1638 struct ieee80211com *ic = ifp->if_l2com; 1639 const struct rfprog *rfprog; 1640 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT; 1641 int8_t power; 1642 int i, chan; 1643 1644 chan = ieee80211_chan2ieee(ic, c); 1645 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1646 return; 1647 1648 /* select the appropriate RF settings based on what EEPROM says */ 1649 rfprog = (sc->rf_rev == RT2573_RF_5225 || 1650 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226; 1651 1652 /* find the settings for this channel (we know it exists) */ 1653 for (i = 0; rfprog[i].chan != chan; i++); 1654 1655 power = sc->txpow[i]; 1656 if (power < 0) { 1657 bbp94 += power; 1658 power = 0; 1659 } else if (power > 31) { 1660 bbp94 += power - 31; 1661 power = 31; 1662 } 1663 1664 /* 1665 * If we are switching from the 2GHz band to the 5GHz band or 1666 * vice-versa, BBP registers need to be reprogrammed. 1667 */ 1668 if (c->ic_flags != ic->ic_curchan->ic_flags) { 1669 rum_select_band(sc, c); 1670 rum_select_antenna(sc); 1671 } 1672 ic->ic_curchan = c; 1673 1674 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1675 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1676 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1677 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1678 1679 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1680 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1681 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1); 1682 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1683 1684 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1685 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1686 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1687 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1688 1689 rum_pause(sc, hz / 100); 1690 1691 /* enable smart mode for MIMO-capable RFs */ 1692 bbp3 = rum_bbp_read(sc, 3); 1693 1694 bbp3 &= ~RT2573_SMART_MODE; 1695 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527) 1696 bbp3 |= RT2573_SMART_MODE; 1697 1698 rum_bbp_write(sc, 3, bbp3); 1699 1700 if (bbp94 != RT2573_BBPR94_DEFAULT) 1701 rum_bbp_write(sc, 94, bbp94); 1702 1703 /* give the chip some extra time to do the switchover */ 1704 rum_pause(sc, hz / 100); 1705} 1706 1707/* 1708 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS 1709 * and HostAP operating modes. 1710 */ 1711static void 1712rum_enable_tsf_sync(struct rum_softc *sc) 1713{ 1714 struct ifnet *ifp = sc->sc_ifp; 1715 struct ieee80211com *ic = ifp->if_l2com; 1716 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1717 uint32_t tmp; 1718 1719 if (vap->iv_opmode != IEEE80211_M_STA) { 1720 /* 1721 * Change default 16ms TBTT adjustment to 8ms. 1722 * Must be done before enabling beacon generation. 1723 */ 1724 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8); 1725 } 1726 1727 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000; 1728 1729 /* set beacon interval (in 1/16ms unit) */ 1730 tmp |= vap->iv_bss->ni_intval * 16; 1731 1732 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT; 1733 if (vap->iv_opmode == IEEE80211_M_STA) 1734 tmp |= RT2573_TSF_MODE(1); 1735 else 1736 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON; 1737 1738 rum_write(sc, RT2573_TXRX_CSR9, tmp); 1739} 1740 1741static void 1742rum_enable_tsf(struct rum_softc *sc) 1743{ 1744 rum_write(sc, RT2573_TXRX_CSR9, 1745 (rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000) | 1746 RT2573_TSF_TICKING | RT2573_TSF_MODE(2)); 1747} 1748 1749static void 1750rum_update_slot(struct ifnet *ifp) 1751{ 1752 struct rum_softc *sc = ifp->if_softc; 1753 struct ieee80211com *ic = ifp->if_l2com; 1754 uint8_t slottime; 1755 uint32_t tmp; 1756 1757 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 1758 1759 tmp = rum_read(sc, RT2573_MAC_CSR9); 1760 tmp = (tmp & ~0xff) | slottime; 1761 rum_write(sc, RT2573_MAC_CSR9, tmp); 1762 1763 DPRINTF("setting slot time to %uus\n", slottime); 1764} 1765 1766static void 1767rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid) 1768{ 1769 uint32_t tmp; 1770 1771 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24; 1772 rum_write(sc, RT2573_MAC_CSR4, tmp); 1773 1774 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16; 1775 rum_write(sc, RT2573_MAC_CSR5, tmp); 1776} 1777 1778static void 1779rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr) 1780{ 1781 uint32_t tmp; 1782 1783 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24; 1784 rum_write(sc, RT2573_MAC_CSR2, tmp); 1785 1786 tmp = addr[4] | addr[5] << 8 | 0xff << 16; 1787 rum_write(sc, RT2573_MAC_CSR3, tmp); 1788} 1789 1790static void 1791rum_setpromisc(struct rum_softc *sc) 1792{ 1793 struct ifnet *ifp = sc->sc_ifp; 1794 uint32_t tmp; 1795 1796 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1797 1798 tmp &= ~RT2573_DROP_NOT_TO_ME; 1799 if (!(ifp->if_flags & IFF_PROMISC)) 1800 tmp |= RT2573_DROP_NOT_TO_ME; 1801 1802 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1803 1804 DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 1805 "entering" : "leaving"); 1806} 1807 1808static void 1809rum_update_promisc(struct ifnet *ifp) 1810{ 1811 struct rum_softc *sc = ifp->if_softc; 1812 1813 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1814 return; 1815 1816 RUM_LOCK(sc); 1817 rum_setpromisc(sc); 1818 RUM_UNLOCK(sc); 1819} 1820 1821static void 1822rum_update_mcast(struct ifnet *ifp) 1823{ 1824 1825 /* XXX do nothing? */ 1826} 1827 1828static const char * 1829rum_get_rf(int rev) 1830{ 1831 switch (rev) { 1832 case RT2573_RF_2527: return "RT2527 (MIMO XR)"; 1833 case RT2573_RF_2528: return "RT2528"; 1834 case RT2573_RF_5225: return "RT5225 (MIMO XR)"; 1835 case RT2573_RF_5226: return "RT5226"; 1836 default: return "unknown"; 1837 } 1838} 1839 1840static void 1841rum_read_eeprom(struct rum_softc *sc) 1842{ 1843 uint16_t val; 1844#ifdef RUM_DEBUG 1845 int i; 1846#endif 1847 1848 /* read MAC address */ 1849 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_bssid, 6); 1850 1851 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2); 1852 val = le16toh(val); 1853 sc->rf_rev = (val >> 11) & 0x1f; 1854 sc->hw_radio = (val >> 10) & 0x1; 1855 sc->rx_ant = (val >> 4) & 0x3; 1856 sc->tx_ant = (val >> 2) & 0x3; 1857 sc->nb_ant = val & 0x3; 1858 1859 DPRINTF("RF revision=%d\n", sc->rf_rev); 1860 1861 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2); 1862 val = le16toh(val); 1863 sc->ext_5ghz_lna = (val >> 6) & 0x1; 1864 sc->ext_2ghz_lna = (val >> 4) & 0x1; 1865 1866 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n", 1867 sc->ext_2ghz_lna, sc->ext_5ghz_lna); 1868 1869 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2); 1870 val = le16toh(val); 1871 if ((val & 0xff) != 0xff) 1872 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */ 1873 1874 /* Only [-10, 10] is valid */ 1875 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10) 1876 sc->rssi_2ghz_corr = 0; 1877 1878 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2); 1879 val = le16toh(val); 1880 if ((val & 0xff) != 0xff) 1881 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */ 1882 1883 /* Only [-10, 10] is valid */ 1884 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10) 1885 sc->rssi_5ghz_corr = 0; 1886 1887 if (sc->ext_2ghz_lna) 1888 sc->rssi_2ghz_corr -= 14; 1889 if (sc->ext_5ghz_lna) 1890 sc->rssi_5ghz_corr -= 14; 1891 1892 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n", 1893 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr); 1894 1895 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2); 1896 val = le16toh(val); 1897 if ((val & 0xff) != 0xff) 1898 sc->rffreq = val & 0xff; 1899 1900 DPRINTF("RF freq=%d\n", sc->rffreq); 1901 1902 /* read Tx power for all a/b/g channels */ 1903 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14); 1904 /* XXX default Tx power for 802.11a channels */ 1905 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14); 1906#ifdef RUM_DEBUG 1907 for (i = 0; i < 14; i++) 1908 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]); 1909#endif 1910 1911 /* read default values for BBP registers */ 1912 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16); 1913#ifdef RUM_DEBUG 1914 for (i = 0; i < 14; i++) { 1915 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 1916 continue; 1917 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg, 1918 sc->bbp_prom[i].val); 1919 } 1920#endif 1921} 1922 1923static int 1924rum_bbp_init(struct rum_softc *sc) 1925{ 1926#define N(a) (sizeof (a) / sizeof ((a)[0])) 1927 int i, ntries; 1928 1929 /* wait for BBP to be ready */ 1930 for (ntries = 0; ntries < 100; ntries++) { 1931 const uint8_t val = rum_bbp_read(sc, 0); 1932 if (val != 0 && val != 0xff) 1933 break; 1934 if (rum_pause(sc, hz / 100)) 1935 break; 1936 } 1937 if (ntries == 100) { 1938 device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 1939 return EIO; 1940 } 1941 1942 /* initialize BBP registers to default values */ 1943 for (i = 0; i < N(rum_def_bbp); i++) 1944 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val); 1945 1946 /* write vendor-specific BBP values (from EEPROM) */ 1947 for (i = 0; i < 16; i++) { 1948 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 1949 continue; 1950 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 1951 } 1952 1953 return 0; 1954#undef N 1955} 1956 1957static void 1958rum_init_locked(struct rum_softc *sc) 1959{ 1960#define N(a) (sizeof (a) / sizeof ((a)[0])) 1961 struct ifnet *ifp = sc->sc_ifp; 1962 struct ieee80211com *ic = ifp->if_l2com; 1963 uint32_t tmp; 1964 usb_error_t error; 1965 int i, ntries; 1966 1967 RUM_LOCK_ASSERT(sc, MA_OWNED); 1968 1969 rum_stop(sc); 1970 1971 /* initialize MAC registers to default values */ 1972 for (i = 0; i < N(rum_def_mac); i++) 1973 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val); 1974 1975 /* set host ready */ 1976 rum_write(sc, RT2573_MAC_CSR1, 3); 1977 rum_write(sc, RT2573_MAC_CSR1, 0); 1978 1979 /* wait for BBP/RF to wakeup */ 1980 for (ntries = 0; ntries < 100; ntries++) { 1981 if (rum_read(sc, RT2573_MAC_CSR12) & 8) 1982 break; 1983 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */ 1984 if (rum_pause(sc, hz / 100)) 1985 break; 1986 } 1987 if (ntries == 100) { 1988 device_printf(sc->sc_dev, 1989 "timeout waiting for BBP/RF to wakeup\n"); 1990 goto fail; 1991 } 1992 1993 if ((error = rum_bbp_init(sc)) != 0) 1994 goto fail; 1995 1996 /* select default channel */ 1997 rum_select_band(sc, ic->ic_curchan); 1998 rum_select_antenna(sc); 1999 rum_set_chan(sc, ic->ic_curchan); 2000 2001 /* clear STA registers */ 2002 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2003 2004 rum_set_macaddr(sc, IF_LLADDR(ifp)); 2005 2006 /* initialize ASIC */ 2007 rum_write(sc, RT2573_MAC_CSR1, 4); 2008 2009 /* 2010 * Allocate Tx and Rx xfer queues. 2011 */ 2012 rum_setup_tx_list(sc); 2013 2014 /* update Rx filter */ 2015 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff; 2016 2017 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR; 2018 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2019 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR | 2020 RT2573_DROP_ACKCTS; 2021 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2022 tmp |= RT2573_DROP_TODS; 2023 if (!(ifp->if_flags & IFF_PROMISC)) 2024 tmp |= RT2573_DROP_NOT_TO_ME; 2025 } 2026 rum_write(sc, RT2573_TXRX_CSR0, tmp); 2027 2028 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2029 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2030 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]); 2031 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]); 2032 return; 2033 2034fail: rum_stop(sc); 2035#undef N 2036} 2037 2038static void 2039rum_init(void *priv) 2040{ 2041 struct rum_softc *sc = priv; 2042 struct ifnet *ifp = sc->sc_ifp; 2043 struct ieee80211com *ic = ifp->if_l2com; 2044 2045 RUM_LOCK(sc); 2046 rum_init_locked(sc); 2047 RUM_UNLOCK(sc); 2048 2049 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2050 ieee80211_start_all(ic); /* start all vap's */ 2051} 2052 2053static void 2054rum_stop(struct rum_softc *sc) 2055{ 2056 struct ifnet *ifp = sc->sc_ifp; 2057 uint32_t tmp; 2058 2059 RUM_LOCK_ASSERT(sc, MA_OWNED); 2060 2061 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2062 2063 RUM_UNLOCK(sc); 2064 2065 /* 2066 * Drain the USB transfers, if not already drained: 2067 */ 2068 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]); 2069 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]); 2070 2071 RUM_LOCK(sc); 2072 2073 rum_unsetup_tx_list(sc); 2074 2075 /* disable Rx */ 2076 tmp = rum_read(sc, RT2573_TXRX_CSR0); 2077 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 2078 2079 /* reset ASIC */ 2080 rum_write(sc, RT2573_MAC_CSR1, 3); 2081 rum_write(sc, RT2573_MAC_CSR1, 0); 2082} 2083 2084static void 2085rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size) 2086{ 2087 struct usb_device_request req; 2088 uint16_t reg = RT2573_MCU_CODE_BASE; 2089 usb_error_t err; 2090 2091 /* copy firmware image into NIC */ 2092 for (; size >= 4; reg += 4, ucode += 4, size -= 4) { 2093 err = rum_write(sc, reg, UGETDW(ucode)); 2094 if (err) { 2095 /* firmware already loaded ? */ 2096 device_printf(sc->sc_dev, "Firmware load " 2097 "failure! (ignored)\n"); 2098 break; 2099 } 2100 } 2101 2102 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 2103 req.bRequest = RT2573_MCU_CNTL; 2104 USETW(req.wValue, RT2573_MCU_RUN); 2105 USETW(req.wIndex, 0); 2106 USETW(req.wLength, 0); 2107 2108 err = rum_do_request(sc, &req, NULL); 2109 if (err != 0) { 2110 device_printf(sc->sc_dev, "could not run firmware: %s\n", 2111 usbd_errstr(err)); 2112 } 2113 2114 /* give the chip some time to boot */ 2115 rum_pause(sc, hz / 8); 2116} 2117 2118static int 2119rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap) 2120{ 2121 struct ieee80211com *ic = vap->iv_ic; 2122 const struct ieee80211_txparam *tp; 2123 struct rum_tx_desc desc; 2124 struct mbuf *m0; 2125 2126 m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo); 2127 if (m0 == NULL) { 2128 return ENOBUFS; 2129 } 2130 2131 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 2132 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ, 2133 m0->m_pkthdr.len, tp->mgmtrate); 2134 2135 /* copy the first 24 bytes of Tx descriptor into NIC memory */ 2136 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24); 2137 2138 /* copy beacon header and payload into NIC memory */ 2139 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *), 2140 m0->m_pkthdr.len); 2141 2142 m_freem(m0); 2143 2144 return 0; 2145} 2146 2147static int 2148rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2149 const struct ieee80211_bpf_params *params) 2150{ 2151 struct ifnet *ifp = ni->ni_ic->ic_ifp; 2152 struct rum_softc *sc = ifp->if_softc; 2153 2154 RUM_LOCK(sc); 2155 /* prevent management frames from being sent if we're not ready */ 2156 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2157 RUM_UNLOCK(sc); 2158 m_freem(m); 2159 ieee80211_free_node(ni); 2160 return ENETDOWN; 2161 } 2162 if (sc->tx_nfree < RUM_TX_MINFREE) { 2163 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2164 RUM_UNLOCK(sc); 2165 m_freem(m); 2166 ieee80211_free_node(ni); 2167 return EIO; 2168 } 2169 2170 ifp->if_opackets++; 2171 2172 if (params == NULL) { 2173 /* 2174 * Legacy path; interpret frame contents to decide 2175 * precisely how to send the frame. 2176 */ 2177 if (rum_tx_mgt(sc, m, ni) != 0) 2178 goto bad; 2179 } else { 2180 /* 2181 * Caller supplied explicit parameters to use in 2182 * sending the frame. 2183 */ 2184 if (rum_tx_raw(sc, m, ni, params) != 0) 2185 goto bad; 2186 } 2187 RUM_UNLOCK(sc); 2188 2189 return 0; 2190bad: 2191 ifp->if_oerrors++; 2192 RUM_UNLOCK(sc); 2193 ieee80211_free_node(ni); 2194 return EIO; 2195} 2196 2197static void 2198rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni) 2199{ 2200 struct ieee80211vap *vap = ni->ni_vap; 2201 struct rum_vap *rvp = RUM_VAP(vap); 2202 2203 /* clear statistic registers (STA_CSR0 to STA_CSR5) */ 2204 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2205 2206 ieee80211_ratectl_node_init(ni); 2207 2208 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp); 2209} 2210 2211static void 2212rum_ratectl_timeout(void *arg) 2213{ 2214 struct rum_vap *rvp = arg; 2215 struct ieee80211vap *vap = &rvp->vap; 2216 struct ieee80211com *ic = vap->iv_ic; 2217 2218 ieee80211_runtask(ic, &rvp->ratectl_task); 2219} 2220 2221static void 2222rum_ratectl_task(void *arg, int pending) 2223{ 2224 struct rum_vap *rvp = arg; 2225 struct ieee80211vap *vap = &rvp->vap; 2226 struct ieee80211com *ic = vap->iv_ic; 2227 struct ifnet *ifp = ic->ic_ifp; 2228 struct rum_softc *sc = ifp->if_softc; 2229 struct ieee80211_node *ni = vap->iv_bss; 2230 int ok, fail; 2231 int sum, retrycnt; 2232 2233 RUM_LOCK(sc); 2234 /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */ 2235 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta)); 2236 2237 ok = (le32toh(sc->sta[4]) >> 16) + /* TX ok w/o retry */ 2238 (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ retry */ 2239 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */ 2240 sum = ok+fail; 2241 retrycnt = (le32toh(sc->sta[5]) & 0xffff) + fail; 2242 2243 ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt); 2244 (void) ieee80211_ratectl_rate(ni, NULL, 0); 2245 2246 ifp->if_oerrors += fail; /* count TX retry-fail as Tx errors */ 2247 2248 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp); 2249 RUM_UNLOCK(sc); 2250} 2251 2252static void 2253rum_scan_start(struct ieee80211com *ic) 2254{ 2255 struct ifnet *ifp = ic->ic_ifp; 2256 struct rum_softc *sc = ifp->if_softc; 2257 uint32_t tmp; 2258 2259 RUM_LOCK(sc); 2260 /* abort TSF synchronization */ 2261 tmp = rum_read(sc, RT2573_TXRX_CSR9); 2262 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff); 2263 rum_set_bssid(sc, ifp->if_broadcastaddr); 2264 RUM_UNLOCK(sc); 2265 2266} 2267 2268static void 2269rum_scan_end(struct ieee80211com *ic) 2270{ 2271 struct rum_softc *sc = ic->ic_ifp->if_softc; 2272 2273 RUM_LOCK(sc); 2274 rum_enable_tsf_sync(sc); 2275 rum_set_bssid(sc, sc->sc_bssid); 2276 RUM_UNLOCK(sc); 2277 2278} 2279 2280static void 2281rum_set_channel(struct ieee80211com *ic) 2282{ 2283 struct rum_softc *sc = ic->ic_ifp->if_softc; 2284 2285 RUM_LOCK(sc); 2286 rum_set_chan(sc, ic->ic_curchan); 2287 RUM_UNLOCK(sc); 2288} 2289 2290static int 2291rum_get_rssi(struct rum_softc *sc, uint8_t raw) 2292{ 2293 struct ifnet *ifp = sc->sc_ifp; 2294 struct ieee80211com *ic = ifp->if_l2com; 2295 int lna, agc, rssi; 2296 2297 lna = (raw >> 5) & 0x3; 2298 agc = raw & 0x1f; 2299 2300 if (lna == 0) { 2301 /* 2302 * No RSSI mapping 2303 * 2304 * NB: Since RSSI is relative to noise floor, -1 is 2305 * adequate for caller to know error happened. 2306 */ 2307 return -1; 2308 } 2309 2310 rssi = (2 * agc) - RT2573_NOISE_FLOOR; 2311 2312 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) { 2313 rssi += sc->rssi_2ghz_corr; 2314 2315 if (lna == 1) 2316 rssi -= 64; 2317 else if (lna == 2) 2318 rssi -= 74; 2319 else if (lna == 3) 2320 rssi -= 90; 2321 } else { 2322 rssi += sc->rssi_5ghz_corr; 2323 2324 if (!sc->ext_5ghz_lna && lna != 1) 2325 rssi += 4; 2326 2327 if (lna == 1) 2328 rssi -= 64; 2329 else if (lna == 2) 2330 rssi -= 86; 2331 else if (lna == 3) 2332 rssi -= 100; 2333 } 2334 return rssi; 2335} 2336 2337static int 2338rum_pause(struct rum_softc *sc, int timeout) 2339{ 2340 2341 usb_pause_mtx(&sc->sc_mtx, timeout); 2342 return (0); 2343} 2344 2345static device_method_t rum_methods[] = { 2346 /* Device interface */ 2347 DEVMETHOD(device_probe, rum_match), 2348 DEVMETHOD(device_attach, rum_attach), 2349 DEVMETHOD(device_detach, rum_detach), 2350 2351 { 0, 0 } 2352}; 2353 2354static driver_t rum_driver = { 2355 .name = "rum", 2356 .methods = rum_methods, 2357 .size = sizeof(struct rum_softc), 2358}; 2359 2360static devclass_t rum_devclass; 2361 2362DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
| 142static device_probe_t rum_match; 143static device_attach_t rum_attach; 144static device_detach_t rum_detach; 145 146static usb_callback_t rum_bulk_read_callback; 147static usb_callback_t rum_bulk_write_callback; 148 149static usb_error_t rum_do_request(struct rum_softc *sc, 150 struct usb_device_request *req, void *data); 151static struct ieee80211vap *rum_vap_create(struct ieee80211com *, 152 const char name[IFNAMSIZ], int unit, int opmode, 153 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN], 154 const uint8_t mac[IEEE80211_ADDR_LEN]); 155static void rum_vap_delete(struct ieee80211vap *); 156static void rum_tx_free(struct rum_tx_data *, int); 157static void rum_setup_tx_list(struct rum_softc *); 158static void rum_unsetup_tx_list(struct rum_softc *); 159static int rum_newstate(struct ieee80211vap *, 160 enum ieee80211_state, int); 161static void rum_setup_tx_desc(struct rum_softc *, 162 struct rum_tx_desc *, uint32_t, uint16_t, int, 163 int); 164static int rum_tx_mgt(struct rum_softc *, struct mbuf *, 165 struct ieee80211_node *); 166static int rum_tx_raw(struct rum_softc *, struct mbuf *, 167 struct ieee80211_node *, 168 const struct ieee80211_bpf_params *); 169static int rum_tx_data(struct rum_softc *, struct mbuf *, 170 struct ieee80211_node *); 171static void rum_start(struct ifnet *); 172static int rum_ioctl(struct ifnet *, u_long, caddr_t); 173static void rum_eeprom_read(struct rum_softc *, uint16_t, void *, 174 int); 175static uint32_t rum_read(struct rum_softc *, uint16_t); 176static void rum_read_multi(struct rum_softc *, uint16_t, void *, 177 int); 178static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t); 179static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *, 180 size_t); 181static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t); 182static uint8_t rum_bbp_read(struct rum_softc *, uint8_t); 183static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t); 184static void rum_select_antenna(struct rum_softc *); 185static void rum_enable_mrr(struct rum_softc *); 186static void rum_set_txpreamble(struct rum_softc *); 187static void rum_set_basicrates(struct rum_softc *); 188static void rum_select_band(struct rum_softc *, 189 struct ieee80211_channel *); 190static void rum_set_chan(struct rum_softc *, 191 struct ieee80211_channel *); 192static void rum_enable_tsf_sync(struct rum_softc *); 193static void rum_enable_tsf(struct rum_softc *); 194static void rum_update_slot(struct ifnet *); 195static void rum_set_bssid(struct rum_softc *, const uint8_t *); 196static void rum_set_macaddr(struct rum_softc *, const uint8_t *); 197static void rum_update_mcast(struct ifnet *); 198static void rum_update_promisc(struct ifnet *); 199static void rum_setpromisc(struct rum_softc *); 200static const char *rum_get_rf(int); 201static void rum_read_eeprom(struct rum_softc *); 202static int rum_bbp_init(struct rum_softc *); 203static void rum_init_locked(struct rum_softc *); 204static void rum_init(void *); 205static void rum_stop(struct rum_softc *); 206static void rum_load_microcode(struct rum_softc *, const uint8_t *, 207 size_t); 208static int rum_prepare_beacon(struct rum_softc *, 209 struct ieee80211vap *); 210static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *, 211 const struct ieee80211_bpf_params *); 212static void rum_scan_start(struct ieee80211com *); 213static void rum_scan_end(struct ieee80211com *); 214static void rum_set_channel(struct ieee80211com *); 215static int rum_get_rssi(struct rum_softc *, uint8_t); 216static void rum_ratectl_start(struct rum_softc *, 217 struct ieee80211_node *); 218static void rum_ratectl_timeout(void *); 219static void rum_ratectl_task(void *, int); 220static int rum_pause(struct rum_softc *, int); 221 222static const struct { 223 uint32_t reg; 224 uint32_t val; 225} rum_def_mac[] = { 226 { RT2573_TXRX_CSR0, 0x025fb032 }, 227 { RT2573_TXRX_CSR1, 0x9eaa9eaf }, 228 { RT2573_TXRX_CSR2, 0x8a8b8c8d }, 229 { RT2573_TXRX_CSR3, 0x00858687 }, 230 { RT2573_TXRX_CSR7, 0x2e31353b }, 231 { RT2573_TXRX_CSR8, 0x2a2a2a2c }, 232 { RT2573_TXRX_CSR15, 0x0000000f }, 233 { RT2573_MAC_CSR6, 0x00000fff }, 234 { RT2573_MAC_CSR8, 0x016c030a }, 235 { RT2573_MAC_CSR10, 0x00000718 }, 236 { RT2573_MAC_CSR12, 0x00000004 }, 237 { RT2573_MAC_CSR13, 0x00007f00 }, 238 { RT2573_SEC_CSR0, 0x00000000 }, 239 { RT2573_SEC_CSR1, 0x00000000 }, 240 { RT2573_SEC_CSR5, 0x00000000 }, 241 { RT2573_PHY_CSR1, 0x000023b0 }, 242 { RT2573_PHY_CSR5, 0x00040a06 }, 243 { RT2573_PHY_CSR6, 0x00080606 }, 244 { RT2573_PHY_CSR7, 0x00000408 }, 245 { RT2573_AIFSN_CSR, 0x00002273 }, 246 { RT2573_CWMIN_CSR, 0x00002344 }, 247 { RT2573_CWMAX_CSR, 0x000034aa } 248}; 249 250static const struct { 251 uint8_t reg; 252 uint8_t val; 253} rum_def_bbp[] = { 254 { 3, 0x80 }, 255 { 15, 0x30 }, 256 { 17, 0x20 }, 257 { 21, 0xc8 }, 258 { 22, 0x38 }, 259 { 23, 0x06 }, 260 { 24, 0xfe }, 261 { 25, 0x0a }, 262 { 26, 0x0d }, 263 { 32, 0x0b }, 264 { 34, 0x12 }, 265 { 37, 0x07 }, 266 { 39, 0xf8 }, 267 { 41, 0x60 }, 268 { 53, 0x10 }, 269 { 54, 0x18 }, 270 { 60, 0x10 }, 271 { 61, 0x04 }, 272 { 62, 0x04 }, 273 { 75, 0xfe }, 274 { 86, 0xfe }, 275 { 88, 0xfe }, 276 { 90, 0x0f }, 277 { 99, 0x00 }, 278 { 102, 0x16 }, 279 { 107, 0x04 } 280}; 281 282static const struct rfprog { 283 uint8_t chan; 284 uint32_t r1, r2, r3, r4; 285} rum_rf5226[] = { 286 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 }, 287 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 }, 288 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 }, 289 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 }, 290 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 }, 291 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 }, 292 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 }, 293 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 }, 294 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 }, 295 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 }, 296 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 }, 297 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 }, 298 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 }, 299 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 }, 300 301 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 }, 302 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 }, 303 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 }, 304 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 }, 305 306 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 }, 307 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 }, 308 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 }, 309 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 }, 310 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 }, 311 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 }, 312 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 }, 313 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 }, 314 315 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 }, 316 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 }, 317 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 }, 318 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 }, 319 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 }, 320 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 }, 321 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 }, 322 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 }, 323 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 }, 324 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 }, 325 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 }, 326 327 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 }, 328 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 }, 329 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 }, 330 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 }, 331 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 } 332}, rum_rf5225[] = { 333 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 }, 334 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 }, 335 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 }, 336 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 }, 337 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 }, 338 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 }, 339 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 }, 340 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 }, 341 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 }, 342 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 }, 343 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 }, 344 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 }, 345 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 }, 346 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 }, 347 348 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 }, 349 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 }, 350 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 }, 351 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 }, 352 353 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 }, 354 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 }, 355 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 }, 356 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 }, 357 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 }, 358 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 }, 359 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 }, 360 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 }, 361 362 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 }, 363 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 }, 364 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 }, 365 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 }, 366 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 }, 367 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 }, 368 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 }, 369 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 }, 370 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 }, 371 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 }, 372 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 }, 373 374 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 }, 375 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 }, 376 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 }, 377 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 }, 378 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 } 379}; 380 381static const struct usb_config rum_config[RUM_N_TRANSFER] = { 382 [RUM_BULK_WR] = { 383 .type = UE_BULK, 384 .endpoint = UE_ADDR_ANY, 385 .direction = UE_DIR_OUT, 386 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8), 387 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 388 .callback = rum_bulk_write_callback, 389 .timeout = 5000, /* ms */ 390 }, 391 [RUM_BULK_RD] = { 392 .type = UE_BULK, 393 .endpoint = UE_ADDR_ANY, 394 .direction = UE_DIR_IN, 395 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE), 396 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 397 .callback = rum_bulk_read_callback, 398 }, 399}; 400 401static int 402rum_match(device_t self) 403{ 404 struct usb_attach_arg *uaa = device_get_ivars(self); 405 406 if (uaa->usb_mode != USB_MODE_HOST) 407 return (ENXIO); 408 if (uaa->info.bConfigIndex != 0) 409 return (ENXIO); 410 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX) 411 return (ENXIO); 412 413 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa)); 414} 415 416static int 417rum_attach(device_t self) 418{ 419 struct usb_attach_arg *uaa = device_get_ivars(self); 420 struct rum_softc *sc = device_get_softc(self); 421 struct ieee80211com *ic; 422 struct ifnet *ifp; 423 uint8_t iface_index, bands; 424 uint32_t tmp; 425 int error, ntries; 426 427 device_set_usb_desc(self); 428 sc->sc_udev = uaa->device; 429 sc->sc_dev = self; 430 431 mtx_init(&sc->sc_mtx, device_get_nameunit(self), 432 MTX_NETWORK_LOCK, MTX_DEF); 433 434 iface_index = RT2573_IFACE_INDEX; 435 error = usbd_transfer_setup(uaa->device, &iface_index, 436 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx); 437 if (error) { 438 device_printf(self, "could not allocate USB transfers, " 439 "err=%s\n", usbd_errstr(error)); 440 goto detach; 441 } 442 443 RUM_LOCK(sc); 444 /* retrieve RT2573 rev. no */ 445 for (ntries = 0; ntries < 100; ntries++) { 446 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0) 447 break; 448 if (rum_pause(sc, hz / 100)) 449 break; 450 } 451 if (ntries == 100) { 452 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n"); 453 RUM_UNLOCK(sc); 454 goto detach; 455 } 456 457 /* retrieve MAC address and various other things from EEPROM */ 458 rum_read_eeprom(sc); 459 460 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n", 461 tmp, rum_get_rf(sc->rf_rev)); 462 463 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode)); 464 RUM_UNLOCK(sc); 465 466 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 467 if (ifp == NULL) { 468 device_printf(sc->sc_dev, "can not if_alloc()\n"); 469 goto detach; 470 } 471 ic = ifp->if_l2com; 472 473 ifp->if_softc = sc; 474 if_initname(ifp, "rum", device_get_unit(sc->sc_dev)); 475 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 476 ifp->if_init = rum_init; 477 ifp->if_ioctl = rum_ioctl; 478 ifp->if_start = rum_start; 479 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 480 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 481 IFQ_SET_READY(&ifp->if_snd); 482 483 ic->ic_ifp = ifp; 484 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 485 486 /* set device capabilities */ 487 ic->ic_caps = 488 IEEE80211_C_STA /* station mode supported */ 489 | IEEE80211_C_IBSS /* IBSS mode supported */ 490 | IEEE80211_C_MONITOR /* monitor mode supported */ 491 | IEEE80211_C_HOSTAP /* HostAp mode supported */ 492 | IEEE80211_C_TXPMGT /* tx power management */ 493 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 494 | IEEE80211_C_SHSLOT /* short slot time supported */ 495 | IEEE80211_C_BGSCAN /* bg scanning supported */ 496 | IEEE80211_C_WPA /* 802.11i */ 497 | IEEE80211_C_RATECTL /* use ratectl */ 498 ; 499 500 bands = 0; 501 setbit(&bands, IEEE80211_MODE_11B); 502 setbit(&bands, IEEE80211_MODE_11G); 503 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) 504 setbit(&bands, IEEE80211_MODE_11A); 505 ieee80211_init_channels(ic, NULL, &bands); 506 507 ieee80211_ifattach(ic, sc->sc_bssid); 508 ic->ic_update_promisc = rum_update_promisc; 509 ic->ic_raw_xmit = rum_raw_xmit; 510 ic->ic_scan_start = rum_scan_start; 511 ic->ic_scan_end = rum_scan_end; 512 ic->ic_set_channel = rum_set_channel; 513 514 ic->ic_vap_create = rum_vap_create; 515 ic->ic_vap_delete = rum_vap_delete; 516 ic->ic_update_mcast = rum_update_mcast; 517 518 ieee80211_radiotap_attach(ic, 519 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 520 RT2573_TX_RADIOTAP_PRESENT, 521 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 522 RT2573_RX_RADIOTAP_PRESENT); 523 524 if (bootverbose) 525 ieee80211_announce(ic); 526 527 return (0); 528 529detach: 530 rum_detach(self); 531 return (ENXIO); /* failure */ 532} 533 534static int 535rum_detach(device_t self) 536{ 537 struct rum_softc *sc = device_get_softc(self); 538 struct ifnet *ifp = sc->sc_ifp; 539 struct ieee80211com *ic; 540 541 /* stop all USB transfers */ 542 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER); 543 544 /* free TX list, if any */ 545 RUM_LOCK(sc); 546 rum_unsetup_tx_list(sc); 547 RUM_UNLOCK(sc); 548 549 if (ifp) { 550 ic = ifp->if_l2com; 551 ieee80211_ifdetach(ic); 552 if_free(ifp); 553 } 554 mtx_destroy(&sc->sc_mtx); 555 556 return (0); 557} 558 559static usb_error_t 560rum_do_request(struct rum_softc *sc, 561 struct usb_device_request *req, void *data) 562{ 563 usb_error_t err; 564 int ntries = 10; 565 566 while (ntries--) { 567 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, 568 req, data, 0, NULL, 250 /* ms */); 569 if (err == 0) 570 break; 571 572 DPRINTFN(1, "Control request failed, %s (retrying)\n", 573 usbd_errstr(err)); 574 if (rum_pause(sc, hz / 100)) 575 break; 576 } 577 return (err); 578} 579 580static struct ieee80211vap * 581rum_vap_create(struct ieee80211com *ic, 582 const char name[IFNAMSIZ], int unit, int opmode, int flags, 583 const uint8_t bssid[IEEE80211_ADDR_LEN], 584 const uint8_t mac[IEEE80211_ADDR_LEN]) 585{ 586 struct rum_softc *sc = ic->ic_ifp->if_softc; 587 struct rum_vap *rvp; 588 struct ieee80211vap *vap; 589 590 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 591 return NULL; 592 rvp = (struct rum_vap *) malloc(sizeof(struct rum_vap), 593 M_80211_VAP, M_NOWAIT | M_ZERO); 594 if (rvp == NULL) 595 return NULL; 596 vap = &rvp->vap; 597 /* enable s/w bmiss handling for sta mode */ 598 ieee80211_vap_setup(ic, vap, name, unit, opmode, 599 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac); 600 601 /* override state transition machine */ 602 rvp->newstate = vap->iv_newstate; 603 vap->iv_newstate = rum_newstate; 604 605 usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0); 606 TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp); 607 ieee80211_ratectl_init(vap); 608 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */); 609 /* complete setup */ 610 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status); 611 ic->ic_opmode = opmode; 612 return vap; 613} 614 615static void 616rum_vap_delete(struct ieee80211vap *vap) 617{ 618 struct rum_vap *rvp = RUM_VAP(vap); 619 struct ieee80211com *ic = vap->iv_ic; 620 621 usb_callout_drain(&rvp->ratectl_ch); 622 ieee80211_draintask(ic, &rvp->ratectl_task); 623 ieee80211_ratectl_deinit(vap); 624 ieee80211_vap_detach(vap); 625 free(rvp, M_80211_VAP); 626} 627 628static void 629rum_tx_free(struct rum_tx_data *data, int txerr) 630{ 631 struct rum_softc *sc = data->sc; 632 633 if (data->m != NULL) { 634 if (data->m->m_flags & M_TXCB) 635 ieee80211_process_callback(data->ni, data->m, 636 txerr ? ETIMEDOUT : 0); 637 m_freem(data->m); 638 data->m = NULL; 639 640 ieee80211_free_node(data->ni); 641 data->ni = NULL; 642 } 643 STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 644 sc->tx_nfree++; 645} 646 647static void 648rum_setup_tx_list(struct rum_softc *sc) 649{ 650 struct rum_tx_data *data; 651 int i; 652 653 sc->tx_nfree = 0; 654 STAILQ_INIT(&sc->tx_q); 655 STAILQ_INIT(&sc->tx_free); 656 657 for (i = 0; i < RUM_TX_LIST_COUNT; i++) { 658 data = &sc->tx_data[i]; 659 660 data->sc = sc; 661 STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 662 sc->tx_nfree++; 663 } 664} 665 666static void 667rum_unsetup_tx_list(struct rum_softc *sc) 668{ 669 struct rum_tx_data *data; 670 int i; 671 672 /* make sure any subsequent use of the queues will fail */ 673 sc->tx_nfree = 0; 674 STAILQ_INIT(&sc->tx_q); 675 STAILQ_INIT(&sc->tx_free); 676 677 /* free up all node references and mbufs */ 678 for (i = 0; i < RUM_TX_LIST_COUNT; i++) { 679 data = &sc->tx_data[i]; 680 681 if (data->m != NULL) { 682 m_freem(data->m); 683 data->m = NULL; 684 } 685 if (data->ni != NULL) { 686 ieee80211_free_node(data->ni); 687 data->ni = NULL; 688 } 689 } 690} 691 692static int 693rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 694{ 695 struct rum_vap *rvp = RUM_VAP(vap); 696 struct ieee80211com *ic = vap->iv_ic; 697 struct rum_softc *sc = ic->ic_ifp->if_softc; 698 const struct ieee80211_txparam *tp; 699 enum ieee80211_state ostate; 700 struct ieee80211_node *ni; 701 uint32_t tmp; 702 703 ostate = vap->iv_state; 704 DPRINTF("%s -> %s\n", 705 ieee80211_state_name[ostate], 706 ieee80211_state_name[nstate]); 707 708 IEEE80211_UNLOCK(ic); 709 RUM_LOCK(sc); 710 usb_callout_stop(&rvp->ratectl_ch); 711 712 switch (nstate) { 713 case IEEE80211_S_INIT: 714 if (ostate == IEEE80211_S_RUN) { 715 /* abort TSF synchronization */ 716 tmp = rum_read(sc, RT2573_TXRX_CSR9); 717 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff); 718 } 719 break; 720 721 case IEEE80211_S_RUN: 722 ni = vap->iv_bss; 723 724 if (vap->iv_opmode != IEEE80211_M_MONITOR) { 725 rum_update_slot(ic->ic_ifp); 726 rum_enable_mrr(sc); 727 rum_set_txpreamble(sc); 728 rum_set_basicrates(sc); 729 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid); 730 rum_set_bssid(sc, sc->sc_bssid); 731 } 732 733 if (vap->iv_opmode == IEEE80211_M_HOSTAP || 734 vap->iv_opmode == IEEE80211_M_IBSS) 735 rum_prepare_beacon(sc, vap); 736 737 if (vap->iv_opmode != IEEE80211_M_MONITOR) 738 rum_enable_tsf_sync(sc); 739 else 740 rum_enable_tsf(sc); 741 742 /* enable automatic rate adaptation */ 743 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 744 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) 745 rum_ratectl_start(sc, ni); 746 break; 747 default: 748 break; 749 } 750 RUM_UNLOCK(sc); 751 IEEE80211_LOCK(ic); 752 return (rvp->newstate(vap, nstate, arg)); 753} 754 755static void 756rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 757{ 758 struct rum_softc *sc = usbd_xfer_softc(xfer); 759 struct ifnet *ifp = sc->sc_ifp; 760 struct ieee80211vap *vap; 761 struct rum_tx_data *data; 762 struct mbuf *m; 763 struct usb_page_cache *pc; 764 unsigned int len; 765 int actlen, sumlen; 766 767 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 768 769 switch (USB_GET_STATE(xfer)) { 770 case USB_ST_TRANSFERRED: 771 DPRINTFN(11, "transfer complete, %d bytes\n", actlen); 772 773 /* free resources */ 774 data = usbd_xfer_get_priv(xfer); 775 rum_tx_free(data, 0); 776 usbd_xfer_set_priv(xfer, NULL); 777 778 ifp->if_opackets++; 779 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 780 781 /* FALLTHROUGH */ 782 case USB_ST_SETUP: 783tr_setup: 784 data = STAILQ_FIRST(&sc->tx_q); 785 if (data) { 786 STAILQ_REMOVE_HEAD(&sc->tx_q, next); 787 m = data->m; 788 789 if (m->m_pkthdr.len > (MCLBYTES + RT2573_TX_DESC_SIZE)) { 790 DPRINTFN(0, "data overflow, %u bytes\n", 791 m->m_pkthdr.len); 792 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE); 793 } 794 pc = usbd_xfer_get_frame(xfer, 0); 795 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE); 796 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0, 797 m->m_pkthdr.len); 798 799 vap = data->ni->ni_vap; 800 if (ieee80211_radiotap_active_vap(vap)) { 801 struct rum_tx_radiotap_header *tap = &sc->sc_txtap; 802 803 tap->wt_flags = 0; 804 tap->wt_rate = data->rate; 805 tap->wt_antenna = sc->tx_ant; 806 807 ieee80211_radiotap_tx(vap, m); 808 } 809 810 /* align end on a 4-bytes boundary */ 811 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3; 812 if ((len % 64) == 0) 813 len += 4; 814 815 DPRINTFN(11, "sending frame len=%u xferlen=%u\n", 816 m->m_pkthdr.len, len); 817 818 usbd_xfer_set_frame_len(xfer, 0, len); 819 usbd_xfer_set_priv(xfer, data); 820 821 usbd_transfer_submit(xfer); 822 } 823 RUM_UNLOCK(sc); 824 rum_start(ifp); 825 RUM_LOCK(sc); 826 break; 827 828 default: /* Error */ 829 DPRINTFN(11, "transfer error, %s\n", 830 usbd_errstr(error)); 831 832 ifp->if_oerrors++; 833 data = usbd_xfer_get_priv(xfer); 834 if (data != NULL) { 835 rum_tx_free(data, error); 836 usbd_xfer_set_priv(xfer, NULL); 837 } 838 839 if (error != USB_ERR_CANCELLED) { 840 if (error == USB_ERR_TIMEOUT) 841 device_printf(sc->sc_dev, "device timeout\n"); 842 843 /* 844 * Try to clear stall first, also if other 845 * errors occur, hence clearing stall 846 * introduces a 50 ms delay: 847 */ 848 usbd_xfer_set_stall(xfer); 849 goto tr_setup; 850 } 851 break; 852 } 853} 854 855static void 856rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 857{ 858 struct rum_softc *sc = usbd_xfer_softc(xfer); 859 struct ifnet *ifp = sc->sc_ifp; 860 struct ieee80211com *ic = ifp->if_l2com; 861 struct ieee80211_node *ni; 862 struct mbuf *m = NULL; 863 struct usb_page_cache *pc; 864 uint32_t flags; 865 uint8_t rssi = 0; 866 int len; 867 868 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 869 870 switch (USB_GET_STATE(xfer)) { 871 case USB_ST_TRANSFERRED: 872 873 DPRINTFN(15, "rx done, actlen=%d\n", len); 874 875 if (len < RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN) { 876 DPRINTF("%s: xfer too short %d\n", 877 device_get_nameunit(sc->sc_dev), len); 878 ifp->if_ierrors++; 879 goto tr_setup; 880 } 881 882 len -= RT2573_RX_DESC_SIZE; 883 pc = usbd_xfer_get_frame(xfer, 0); 884 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE); 885 886 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi); 887 flags = le32toh(sc->sc_rx_desc.flags); 888 if (flags & RT2573_RX_CRC_ERROR) { 889 /* 890 * This should not happen since we did not 891 * request to receive those frames when we 892 * filled RUM_TXRX_CSR2: 893 */ 894 DPRINTFN(5, "PHY or CRC error\n"); 895 ifp->if_ierrors++; 896 goto tr_setup; 897 } 898 899 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 900 if (m == NULL) { 901 DPRINTF("could not allocate mbuf\n"); 902 ifp->if_ierrors++; 903 goto tr_setup; 904 } 905 usbd_copy_out(pc, RT2573_RX_DESC_SIZE, 906 mtod(m, uint8_t *), len); 907 908 /* finalize mbuf */ 909 m->m_pkthdr.rcvif = ifp; 910 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff; 911 912 if (ieee80211_radiotap_active(ic)) { 913 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap; 914 915 /* XXX read tsf */ 916 tap->wr_flags = 0; 917 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate, 918 (flags & RT2573_RX_OFDM) ? 919 IEEE80211_T_OFDM : IEEE80211_T_CCK); 920 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi; 921 tap->wr_antnoise = RT2573_NOISE_FLOOR; 922 tap->wr_antenna = sc->rx_ant; 923 } 924 /* FALLTHROUGH */ 925 case USB_ST_SETUP: 926tr_setup: 927 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 928 usbd_transfer_submit(xfer); 929 930 /* 931 * At the end of a USB callback it is always safe to unlock 932 * the private mutex of a device! That is why we do the 933 * "ieee80211_input" here, and not some lines up! 934 */ 935 RUM_UNLOCK(sc); 936 if (m) { 937 ni = ieee80211_find_rxnode(ic, 938 mtod(m, struct ieee80211_frame_min *)); 939 if (ni != NULL) { 940 (void) ieee80211_input(ni, m, rssi, 941 RT2573_NOISE_FLOOR); 942 ieee80211_free_node(ni); 943 } else 944 (void) ieee80211_input_all(ic, m, rssi, 945 RT2573_NOISE_FLOOR); 946 } 947 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 && 948 !IFQ_IS_EMPTY(&ifp->if_snd)) 949 rum_start(ifp); 950 RUM_LOCK(sc); 951 return; 952 953 default: /* Error */ 954 if (error != USB_ERR_CANCELLED) { 955 /* try to clear stall first */ 956 usbd_xfer_set_stall(xfer); 957 goto tr_setup; 958 } 959 return; 960 } 961} 962 963static uint8_t 964rum_plcp_signal(int rate) 965{ 966 switch (rate) { 967 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 968 case 12: return 0xb; 969 case 18: return 0xf; 970 case 24: return 0xa; 971 case 36: return 0xe; 972 case 48: return 0x9; 973 case 72: return 0xd; 974 case 96: return 0x8; 975 case 108: return 0xc; 976 977 /* CCK rates (NB: not IEEE std, device-specific) */ 978 case 2: return 0x0; 979 case 4: return 0x1; 980 case 11: return 0x2; 981 case 22: return 0x3; 982 } 983 return 0xff; /* XXX unsupported/unknown rate */ 984} 985 986static void 987rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc, 988 uint32_t flags, uint16_t xflags, int len, int rate) 989{ 990 struct ifnet *ifp = sc->sc_ifp; 991 struct ieee80211com *ic = ifp->if_l2com; 992 uint16_t plcp_length; 993 int remainder; 994 995 desc->flags = htole32(flags); 996 desc->flags |= htole32(RT2573_TX_VALID); 997 desc->flags |= htole32(len << 16); 998 999 desc->xflags = htole16(xflags); 1000 1001 desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) | 1002 RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10)); 1003 1004 /* setup PLCP fields */ 1005 desc->plcp_signal = rum_plcp_signal(rate); 1006 desc->plcp_service = 4; 1007 1008 len += IEEE80211_CRC_LEN; 1009 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) { 1010 desc->flags |= htole32(RT2573_TX_OFDM); 1011 1012 plcp_length = len & 0xfff; 1013 desc->plcp_length_hi = plcp_length >> 6; 1014 desc->plcp_length_lo = plcp_length & 0x3f; 1015 } else { 1016 plcp_length = (16 * len + rate - 1) / rate; 1017 if (rate == 22) { 1018 remainder = (16 * len) % 22; 1019 if (remainder != 0 && remainder < 7) 1020 desc->plcp_service |= RT2573_PLCP_LENGEXT; 1021 } 1022 desc->plcp_length_hi = plcp_length >> 8; 1023 desc->plcp_length_lo = plcp_length & 0xff; 1024 1025 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1026 desc->plcp_signal |= 0x08; 1027 } 1028} 1029 1030static int 1031rum_sendprot(struct rum_softc *sc, 1032 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 1033{ 1034 struct ieee80211com *ic = ni->ni_ic; 1035 const struct ieee80211_frame *wh; 1036 struct rum_tx_data *data; 1037 struct mbuf *mprot; 1038 int protrate, ackrate, pktlen, flags, isshort; 1039 uint16_t dur; 1040 1041 RUM_LOCK_ASSERT(sc, MA_OWNED); 1042 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 1043 ("protection %d", prot)); 1044 1045 wh = mtod(m, const struct ieee80211_frame *); 1046 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 1047 1048 protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 1049 ackrate = ieee80211_ack_rate(ic->ic_rt, rate); 1050 1051 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 1052 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort) 1053 + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1054 flags = RT2573_TX_MORE_FRAG; 1055 if (prot == IEEE80211_PROT_RTSCTS) { 1056 /* NB: CTS is the same size as an ACK */ 1057 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1058 flags |= RT2573_TX_NEED_ACK; 1059 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 1060 } else { 1061 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 1062 } 1063 if (mprot == NULL) { 1064 /* XXX stat + msg */ 1065 return (ENOBUFS); 1066 } 1067 data = STAILQ_FIRST(&sc->tx_free); 1068 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1069 sc->tx_nfree--; 1070 1071 data->m = mprot; 1072 data->ni = ieee80211_ref_node(ni); 1073 data->rate = protrate; 1074 rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate); 1075 1076 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1077 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1078 1079 return 0; 1080} 1081 1082static int 1083rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1084{ 1085 struct ieee80211vap *vap = ni->ni_vap; 1086 struct ifnet *ifp = sc->sc_ifp; 1087 struct ieee80211com *ic = ifp->if_l2com; 1088 struct rum_tx_data *data; 1089 struct ieee80211_frame *wh; 1090 const struct ieee80211_txparam *tp; 1091 struct ieee80211_key *k; 1092 uint32_t flags = 0; 1093 uint16_t dur; 1094 1095 RUM_LOCK_ASSERT(sc, MA_OWNED); 1096 1097 data = STAILQ_FIRST(&sc->tx_free); 1098 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1099 sc->tx_nfree--; 1100 1101 wh = mtod(m0, struct ieee80211_frame *); 1102 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1103 k = ieee80211_crypto_encap(ni, m0); 1104 if (k == NULL) { 1105 m_freem(m0); 1106 return ENOBUFS; 1107 } 1108 wh = mtod(m0, struct ieee80211_frame *); 1109 } 1110 1111 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1112 1113 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1114 flags |= RT2573_TX_NEED_ACK; 1115 1116 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate, 1117 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1118 *(uint16_t *)wh->i_dur = htole16(dur); 1119 1120 /* tell hardware to add timestamp for probe responses */ 1121 if ((wh->i_fc[0] & 1122 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 1123 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) 1124 flags |= RT2573_TX_TIMESTAMP; 1125 } 1126 1127 data->m = m0; 1128 data->ni = ni; 1129 data->rate = tp->mgmtrate; 1130 1131 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate); 1132 1133 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", 1134 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate); 1135 1136 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1137 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1138 1139 return (0); 1140} 1141 1142static int 1143rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1144 const struct ieee80211_bpf_params *params) 1145{ 1146 struct ieee80211com *ic = ni->ni_ic; 1147 struct rum_tx_data *data; 1148 uint32_t flags; 1149 int rate, error; 1150 1151 RUM_LOCK_ASSERT(sc, MA_OWNED); 1152 KASSERT(params != NULL, ("no raw xmit params")); 1153 1154 rate = params->ibp_rate0; 1155 if (!ieee80211_isratevalid(ic->ic_rt, rate)) { 1156 m_freem(m0); 1157 return EINVAL; 1158 } 1159 flags = 0; 1160 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 1161 flags |= RT2573_TX_NEED_ACK; 1162 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 1163 error = rum_sendprot(sc, m0, ni, 1164 params->ibp_flags & IEEE80211_BPF_RTS ? 1165 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 1166 rate); 1167 if (error || sc->tx_nfree == 0) { 1168 m_freem(m0); 1169 return ENOBUFS; 1170 } 1171 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1172 } 1173 1174 data = STAILQ_FIRST(&sc->tx_free); 1175 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1176 sc->tx_nfree--; 1177 1178 data->m = m0; 1179 data->ni = ni; 1180 data->rate = rate; 1181 1182 /* XXX need to setup descriptor ourself */ 1183 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate); 1184 1185 DPRINTFN(10, "sending raw frame len=%u rate=%u\n", 1186 m0->m_pkthdr.len, rate); 1187 1188 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1189 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1190 1191 return 0; 1192} 1193 1194static int 1195rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1196{ 1197 struct ieee80211vap *vap = ni->ni_vap; 1198 struct ifnet *ifp = sc->sc_ifp; 1199 struct ieee80211com *ic = ifp->if_l2com; 1200 struct rum_tx_data *data; 1201 struct ieee80211_frame *wh; 1202 const struct ieee80211_txparam *tp; 1203 struct ieee80211_key *k; 1204 uint32_t flags = 0; 1205 uint16_t dur; 1206 int error, rate; 1207 1208 RUM_LOCK_ASSERT(sc, MA_OWNED); 1209 1210 wh = mtod(m0, struct ieee80211_frame *); 1211 1212 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 1213 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1214 rate = tp->mcastrate; 1215 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 1216 rate = tp->ucastrate; 1217 else 1218 rate = ni->ni_txrate; 1219 1220 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1221 k = ieee80211_crypto_encap(ni, m0); 1222 if (k == NULL) { 1223 m_freem(m0); 1224 return ENOBUFS; 1225 } 1226 1227 /* packet header may have moved, reset our local pointer */ 1228 wh = mtod(m0, struct ieee80211_frame *); 1229 } 1230 1231 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1232 int prot = IEEE80211_PROT_NONE; 1233 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 1234 prot = IEEE80211_PROT_RTSCTS; 1235 else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1236 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) 1237 prot = ic->ic_protmode; 1238 if (prot != IEEE80211_PROT_NONE) { 1239 error = rum_sendprot(sc, m0, ni, prot, rate); 1240 if (error || sc->tx_nfree == 0) { 1241 m_freem(m0); 1242 return ENOBUFS; 1243 } 1244 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1245 } 1246 } 1247 1248 data = STAILQ_FIRST(&sc->tx_free); 1249 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1250 sc->tx_nfree--; 1251 1252 data->m = m0; 1253 data->ni = ni; 1254 data->rate = rate; 1255 1256 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1257 flags |= RT2573_TX_NEED_ACK; 1258 flags |= RT2573_TX_MORE_FRAG; 1259 1260 dur = ieee80211_ack_duration(ic->ic_rt, rate, 1261 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1262 *(uint16_t *)wh->i_dur = htole16(dur); 1263 } 1264 1265 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate); 1266 1267 DPRINTFN(10, "sending frame len=%d rate=%d\n", 1268 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate); 1269 1270 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1271 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1272 1273 return 0; 1274} 1275 1276static void 1277rum_start(struct ifnet *ifp) 1278{ 1279 struct rum_softc *sc = ifp->if_softc; 1280 struct ieee80211_node *ni; 1281 struct mbuf *m; 1282 1283 RUM_LOCK(sc); 1284 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1285 RUM_UNLOCK(sc); 1286 return; 1287 } 1288 for (;;) { 1289 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1290 if (m == NULL) 1291 break; 1292 if (sc->tx_nfree < RUM_TX_MINFREE) { 1293 IFQ_DRV_PREPEND(&ifp->if_snd, m); 1294 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1295 break; 1296 } 1297 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1298 if (rum_tx_data(sc, m, ni) != 0) { 1299 ieee80211_free_node(ni); 1300 ifp->if_oerrors++; 1301 break; 1302 } 1303 } 1304 RUM_UNLOCK(sc); 1305} 1306 1307static int 1308rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1309{ 1310 struct rum_softc *sc = ifp->if_softc; 1311 struct ieee80211com *ic = ifp->if_l2com; 1312 struct ifreq *ifr = (struct ifreq *) data; 1313 int error = 0, startall = 0; 1314 1315 switch (cmd) { 1316 case SIOCSIFFLAGS: 1317 RUM_LOCK(sc); 1318 if (ifp->if_flags & IFF_UP) { 1319 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1320 rum_init_locked(sc); 1321 startall = 1; 1322 } else 1323 rum_setpromisc(sc); 1324 } else { 1325 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1326 rum_stop(sc); 1327 } 1328 RUM_UNLOCK(sc); 1329 if (startall) 1330 ieee80211_start_all(ic); 1331 break; 1332 case SIOCGIFMEDIA: 1333 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 1334 break; 1335 case SIOCGIFADDR: 1336 error = ether_ioctl(ifp, cmd, data); 1337 break; 1338 default: 1339 error = EINVAL; 1340 break; 1341 } 1342 return error; 1343} 1344 1345static void 1346rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len) 1347{ 1348 struct usb_device_request req; 1349 usb_error_t error; 1350 1351 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1352 req.bRequest = RT2573_READ_EEPROM; 1353 USETW(req.wValue, 0); 1354 USETW(req.wIndex, addr); 1355 USETW(req.wLength, len); 1356 1357 error = rum_do_request(sc, &req, buf); 1358 if (error != 0) { 1359 device_printf(sc->sc_dev, "could not read EEPROM: %s\n", 1360 usbd_errstr(error)); 1361 } 1362} 1363 1364static uint32_t 1365rum_read(struct rum_softc *sc, uint16_t reg) 1366{ 1367 uint32_t val; 1368 1369 rum_read_multi(sc, reg, &val, sizeof val); 1370 1371 return le32toh(val); 1372} 1373 1374static void 1375rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len) 1376{ 1377 struct usb_device_request req; 1378 usb_error_t error; 1379 1380 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1381 req.bRequest = RT2573_READ_MULTI_MAC; 1382 USETW(req.wValue, 0); 1383 USETW(req.wIndex, reg); 1384 USETW(req.wLength, len); 1385 1386 error = rum_do_request(sc, &req, buf); 1387 if (error != 0) { 1388 device_printf(sc->sc_dev, 1389 "could not multi read MAC register: %s\n", 1390 usbd_errstr(error)); 1391 } 1392} 1393 1394static usb_error_t 1395rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val) 1396{ 1397 uint32_t tmp = htole32(val); 1398 1399 return (rum_write_multi(sc, reg, &tmp, sizeof tmp)); 1400} 1401 1402static usb_error_t 1403rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len) 1404{ 1405 struct usb_device_request req; 1406 usb_error_t error; 1407 1408 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1409 req.bRequest = RT2573_WRITE_MULTI_MAC; 1410 USETW(req.wValue, 0); 1411 USETW(req.wIndex, reg); 1412 USETW(req.wLength, len); 1413 1414 error = rum_do_request(sc, &req, buf); 1415 if (error != 0) { 1416 device_printf(sc->sc_dev, 1417 "could not multi write MAC register: %s\n", 1418 usbd_errstr(error)); 1419 } 1420 return (error); 1421} 1422 1423static void 1424rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val) 1425{ 1426 uint32_t tmp; 1427 int ntries; 1428 1429 DPRINTFN(2, "reg=0x%08x\n", reg); 1430 1431 for (ntries = 0; ntries < 100; ntries++) { 1432 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1433 break; 1434 if (rum_pause(sc, hz / 100)) 1435 break; 1436 } 1437 if (ntries == 100) { 1438 device_printf(sc->sc_dev, "could not write to BBP\n"); 1439 return; 1440 } 1441 1442 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val; 1443 rum_write(sc, RT2573_PHY_CSR3, tmp); 1444} 1445 1446static uint8_t 1447rum_bbp_read(struct rum_softc *sc, uint8_t reg) 1448{ 1449 uint32_t val; 1450 int ntries; 1451 1452 DPRINTFN(2, "reg=0x%08x\n", reg); 1453 1454 for (ntries = 0; ntries < 100; ntries++) { 1455 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1456 break; 1457 if (rum_pause(sc, hz / 100)) 1458 break; 1459 } 1460 if (ntries == 100) { 1461 device_printf(sc->sc_dev, "could not read BBP\n"); 1462 return 0; 1463 } 1464 1465 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8; 1466 rum_write(sc, RT2573_PHY_CSR3, val); 1467 1468 for (ntries = 0; ntries < 100; ntries++) { 1469 val = rum_read(sc, RT2573_PHY_CSR3); 1470 if (!(val & RT2573_BBP_BUSY)) 1471 return val & 0xff; 1472 if (rum_pause(sc, hz / 100)) 1473 break; 1474 } 1475 1476 device_printf(sc->sc_dev, "could not read BBP\n"); 1477 return 0; 1478} 1479 1480static void 1481rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val) 1482{ 1483 uint32_t tmp; 1484 int ntries; 1485 1486 for (ntries = 0; ntries < 100; ntries++) { 1487 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY)) 1488 break; 1489 if (rum_pause(sc, hz / 100)) 1490 break; 1491 } 1492 if (ntries == 100) { 1493 device_printf(sc->sc_dev, "could not write to RF\n"); 1494 return; 1495 } 1496 1497 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 | 1498 (reg & 3); 1499 rum_write(sc, RT2573_PHY_CSR4, tmp); 1500 1501 /* remember last written value in sc */ 1502 sc->rf_regs[reg] = val; 1503 1504 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff); 1505} 1506 1507static void 1508rum_select_antenna(struct rum_softc *sc) 1509{ 1510 uint8_t bbp4, bbp77; 1511 uint32_t tmp; 1512 1513 bbp4 = rum_bbp_read(sc, 4); 1514 bbp77 = rum_bbp_read(sc, 77); 1515 1516 /* TBD */ 1517 1518 /* make sure Rx is disabled before switching antenna */ 1519 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1520 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 1521 1522 rum_bbp_write(sc, 4, bbp4); 1523 rum_bbp_write(sc, 77, bbp77); 1524 1525 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1526} 1527 1528/* 1529 * Enable multi-rate retries for frames sent at OFDM rates. 1530 * In 802.11b/g mode, allow fallback to CCK rates. 1531 */ 1532static void 1533rum_enable_mrr(struct rum_softc *sc) 1534{ 1535 struct ifnet *ifp = sc->sc_ifp; 1536 struct ieee80211com *ic = ifp->if_l2com; 1537 uint32_t tmp; 1538 1539 tmp = rum_read(sc, RT2573_TXRX_CSR4); 1540 1541 tmp &= ~RT2573_MRR_CCK_FALLBACK; 1542 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) 1543 tmp |= RT2573_MRR_CCK_FALLBACK; 1544 tmp |= RT2573_MRR_ENABLED; 1545 1546 rum_write(sc, RT2573_TXRX_CSR4, tmp); 1547} 1548 1549static void 1550rum_set_txpreamble(struct rum_softc *sc) 1551{ 1552 struct ifnet *ifp = sc->sc_ifp; 1553 struct ieee80211com *ic = ifp->if_l2com; 1554 uint32_t tmp; 1555 1556 tmp = rum_read(sc, RT2573_TXRX_CSR4); 1557 1558 tmp &= ~RT2573_SHORT_PREAMBLE; 1559 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1560 tmp |= RT2573_SHORT_PREAMBLE; 1561 1562 rum_write(sc, RT2573_TXRX_CSR4, tmp); 1563} 1564 1565static void 1566rum_set_basicrates(struct rum_softc *sc) 1567{ 1568 struct ifnet *ifp = sc->sc_ifp; 1569 struct ieee80211com *ic = ifp->if_l2com; 1570 1571 /* update basic rate set */ 1572 if (ic->ic_curmode == IEEE80211_MODE_11B) { 1573 /* 11b basic rates: 1, 2Mbps */ 1574 rum_write(sc, RT2573_TXRX_CSR5, 0x3); 1575 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) { 1576 /* 11a basic rates: 6, 12, 24Mbps */ 1577 rum_write(sc, RT2573_TXRX_CSR5, 0x150); 1578 } else { 1579 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */ 1580 rum_write(sc, RT2573_TXRX_CSR5, 0xf); 1581 } 1582} 1583 1584/* 1585 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference 1586 * driver. 1587 */ 1588static void 1589rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c) 1590{ 1591 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104; 1592 uint32_t tmp; 1593 1594 /* update all BBP registers that depend on the band */ 1595 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c; 1596 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48; 1597 if (IEEE80211_IS_CHAN_5GHZ(c)) { 1598 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c; 1599 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10; 1600 } 1601 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1602 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1603 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10; 1604 } 1605 1606 sc->bbp17 = bbp17; 1607 rum_bbp_write(sc, 17, bbp17); 1608 rum_bbp_write(sc, 96, bbp96); 1609 rum_bbp_write(sc, 104, bbp104); 1610 1611 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1612 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1613 rum_bbp_write(sc, 75, 0x80); 1614 rum_bbp_write(sc, 86, 0x80); 1615 rum_bbp_write(sc, 88, 0x80); 1616 } 1617 1618 rum_bbp_write(sc, 35, bbp35); 1619 rum_bbp_write(sc, 97, bbp97); 1620 rum_bbp_write(sc, 98, bbp98); 1621 1622 tmp = rum_read(sc, RT2573_PHY_CSR0); 1623 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ); 1624 if (IEEE80211_IS_CHAN_2GHZ(c)) 1625 tmp |= RT2573_PA_PE_2GHZ; 1626 else 1627 tmp |= RT2573_PA_PE_5GHZ; 1628 rum_write(sc, RT2573_PHY_CSR0, tmp); 1629} 1630 1631static void 1632rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c) 1633{ 1634 struct ifnet *ifp = sc->sc_ifp; 1635 struct ieee80211com *ic = ifp->if_l2com; 1636 const struct rfprog *rfprog; 1637 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT; 1638 int8_t power; 1639 int i, chan; 1640 1641 chan = ieee80211_chan2ieee(ic, c); 1642 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1643 return; 1644 1645 /* select the appropriate RF settings based on what EEPROM says */ 1646 rfprog = (sc->rf_rev == RT2573_RF_5225 || 1647 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226; 1648 1649 /* find the settings for this channel (we know it exists) */ 1650 for (i = 0; rfprog[i].chan != chan; i++); 1651 1652 power = sc->txpow[i]; 1653 if (power < 0) { 1654 bbp94 += power; 1655 power = 0; 1656 } else if (power > 31) { 1657 bbp94 += power - 31; 1658 power = 31; 1659 } 1660 1661 /* 1662 * If we are switching from the 2GHz band to the 5GHz band or 1663 * vice-versa, BBP registers need to be reprogrammed. 1664 */ 1665 if (c->ic_flags != ic->ic_curchan->ic_flags) { 1666 rum_select_band(sc, c); 1667 rum_select_antenna(sc); 1668 } 1669 ic->ic_curchan = c; 1670 1671 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1672 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1673 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1674 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1675 1676 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1677 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1678 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1); 1679 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1680 1681 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1682 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1683 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1684 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1685 1686 rum_pause(sc, hz / 100); 1687 1688 /* enable smart mode for MIMO-capable RFs */ 1689 bbp3 = rum_bbp_read(sc, 3); 1690 1691 bbp3 &= ~RT2573_SMART_MODE; 1692 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527) 1693 bbp3 |= RT2573_SMART_MODE; 1694 1695 rum_bbp_write(sc, 3, bbp3); 1696 1697 if (bbp94 != RT2573_BBPR94_DEFAULT) 1698 rum_bbp_write(sc, 94, bbp94); 1699 1700 /* give the chip some extra time to do the switchover */ 1701 rum_pause(sc, hz / 100); 1702} 1703 1704/* 1705 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS 1706 * and HostAP operating modes. 1707 */ 1708static void 1709rum_enable_tsf_sync(struct rum_softc *sc) 1710{ 1711 struct ifnet *ifp = sc->sc_ifp; 1712 struct ieee80211com *ic = ifp->if_l2com; 1713 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1714 uint32_t tmp; 1715 1716 if (vap->iv_opmode != IEEE80211_M_STA) { 1717 /* 1718 * Change default 16ms TBTT adjustment to 8ms. 1719 * Must be done before enabling beacon generation. 1720 */ 1721 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8); 1722 } 1723 1724 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000; 1725 1726 /* set beacon interval (in 1/16ms unit) */ 1727 tmp |= vap->iv_bss->ni_intval * 16; 1728 1729 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT; 1730 if (vap->iv_opmode == IEEE80211_M_STA) 1731 tmp |= RT2573_TSF_MODE(1); 1732 else 1733 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON; 1734 1735 rum_write(sc, RT2573_TXRX_CSR9, tmp); 1736} 1737 1738static void 1739rum_enable_tsf(struct rum_softc *sc) 1740{ 1741 rum_write(sc, RT2573_TXRX_CSR9, 1742 (rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000) | 1743 RT2573_TSF_TICKING | RT2573_TSF_MODE(2)); 1744} 1745 1746static void 1747rum_update_slot(struct ifnet *ifp) 1748{ 1749 struct rum_softc *sc = ifp->if_softc; 1750 struct ieee80211com *ic = ifp->if_l2com; 1751 uint8_t slottime; 1752 uint32_t tmp; 1753 1754 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 1755 1756 tmp = rum_read(sc, RT2573_MAC_CSR9); 1757 tmp = (tmp & ~0xff) | slottime; 1758 rum_write(sc, RT2573_MAC_CSR9, tmp); 1759 1760 DPRINTF("setting slot time to %uus\n", slottime); 1761} 1762 1763static void 1764rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid) 1765{ 1766 uint32_t tmp; 1767 1768 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24; 1769 rum_write(sc, RT2573_MAC_CSR4, tmp); 1770 1771 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16; 1772 rum_write(sc, RT2573_MAC_CSR5, tmp); 1773} 1774 1775static void 1776rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr) 1777{ 1778 uint32_t tmp; 1779 1780 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24; 1781 rum_write(sc, RT2573_MAC_CSR2, tmp); 1782 1783 tmp = addr[4] | addr[5] << 8 | 0xff << 16; 1784 rum_write(sc, RT2573_MAC_CSR3, tmp); 1785} 1786 1787static void 1788rum_setpromisc(struct rum_softc *sc) 1789{ 1790 struct ifnet *ifp = sc->sc_ifp; 1791 uint32_t tmp; 1792 1793 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1794 1795 tmp &= ~RT2573_DROP_NOT_TO_ME; 1796 if (!(ifp->if_flags & IFF_PROMISC)) 1797 tmp |= RT2573_DROP_NOT_TO_ME; 1798 1799 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1800 1801 DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 1802 "entering" : "leaving"); 1803} 1804 1805static void 1806rum_update_promisc(struct ifnet *ifp) 1807{ 1808 struct rum_softc *sc = ifp->if_softc; 1809 1810 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1811 return; 1812 1813 RUM_LOCK(sc); 1814 rum_setpromisc(sc); 1815 RUM_UNLOCK(sc); 1816} 1817 1818static void 1819rum_update_mcast(struct ifnet *ifp) 1820{ 1821 1822 /* XXX do nothing? */ 1823} 1824 1825static const char * 1826rum_get_rf(int rev) 1827{ 1828 switch (rev) { 1829 case RT2573_RF_2527: return "RT2527 (MIMO XR)"; 1830 case RT2573_RF_2528: return "RT2528"; 1831 case RT2573_RF_5225: return "RT5225 (MIMO XR)"; 1832 case RT2573_RF_5226: return "RT5226"; 1833 default: return "unknown"; 1834 } 1835} 1836 1837static void 1838rum_read_eeprom(struct rum_softc *sc) 1839{ 1840 uint16_t val; 1841#ifdef RUM_DEBUG 1842 int i; 1843#endif 1844 1845 /* read MAC address */ 1846 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_bssid, 6); 1847 1848 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2); 1849 val = le16toh(val); 1850 sc->rf_rev = (val >> 11) & 0x1f; 1851 sc->hw_radio = (val >> 10) & 0x1; 1852 sc->rx_ant = (val >> 4) & 0x3; 1853 sc->tx_ant = (val >> 2) & 0x3; 1854 sc->nb_ant = val & 0x3; 1855 1856 DPRINTF("RF revision=%d\n", sc->rf_rev); 1857 1858 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2); 1859 val = le16toh(val); 1860 sc->ext_5ghz_lna = (val >> 6) & 0x1; 1861 sc->ext_2ghz_lna = (val >> 4) & 0x1; 1862 1863 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n", 1864 sc->ext_2ghz_lna, sc->ext_5ghz_lna); 1865 1866 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2); 1867 val = le16toh(val); 1868 if ((val & 0xff) != 0xff) 1869 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */ 1870 1871 /* Only [-10, 10] is valid */ 1872 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10) 1873 sc->rssi_2ghz_corr = 0; 1874 1875 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2); 1876 val = le16toh(val); 1877 if ((val & 0xff) != 0xff) 1878 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */ 1879 1880 /* Only [-10, 10] is valid */ 1881 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10) 1882 sc->rssi_5ghz_corr = 0; 1883 1884 if (sc->ext_2ghz_lna) 1885 sc->rssi_2ghz_corr -= 14; 1886 if (sc->ext_5ghz_lna) 1887 sc->rssi_5ghz_corr -= 14; 1888 1889 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n", 1890 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr); 1891 1892 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2); 1893 val = le16toh(val); 1894 if ((val & 0xff) != 0xff) 1895 sc->rffreq = val & 0xff; 1896 1897 DPRINTF("RF freq=%d\n", sc->rffreq); 1898 1899 /* read Tx power for all a/b/g channels */ 1900 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14); 1901 /* XXX default Tx power for 802.11a channels */ 1902 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14); 1903#ifdef RUM_DEBUG 1904 for (i = 0; i < 14; i++) 1905 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]); 1906#endif 1907 1908 /* read default values for BBP registers */ 1909 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16); 1910#ifdef RUM_DEBUG 1911 for (i = 0; i < 14; i++) { 1912 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 1913 continue; 1914 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg, 1915 sc->bbp_prom[i].val); 1916 } 1917#endif 1918} 1919 1920static int 1921rum_bbp_init(struct rum_softc *sc) 1922{ 1923#define N(a) (sizeof (a) / sizeof ((a)[0])) 1924 int i, ntries; 1925 1926 /* wait for BBP to be ready */ 1927 for (ntries = 0; ntries < 100; ntries++) { 1928 const uint8_t val = rum_bbp_read(sc, 0); 1929 if (val != 0 && val != 0xff) 1930 break; 1931 if (rum_pause(sc, hz / 100)) 1932 break; 1933 } 1934 if (ntries == 100) { 1935 device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 1936 return EIO; 1937 } 1938 1939 /* initialize BBP registers to default values */ 1940 for (i = 0; i < N(rum_def_bbp); i++) 1941 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val); 1942 1943 /* write vendor-specific BBP values (from EEPROM) */ 1944 for (i = 0; i < 16; i++) { 1945 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 1946 continue; 1947 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 1948 } 1949 1950 return 0; 1951#undef N 1952} 1953 1954static void 1955rum_init_locked(struct rum_softc *sc) 1956{ 1957#define N(a) (sizeof (a) / sizeof ((a)[0])) 1958 struct ifnet *ifp = sc->sc_ifp; 1959 struct ieee80211com *ic = ifp->if_l2com; 1960 uint32_t tmp; 1961 usb_error_t error; 1962 int i, ntries; 1963 1964 RUM_LOCK_ASSERT(sc, MA_OWNED); 1965 1966 rum_stop(sc); 1967 1968 /* initialize MAC registers to default values */ 1969 for (i = 0; i < N(rum_def_mac); i++) 1970 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val); 1971 1972 /* set host ready */ 1973 rum_write(sc, RT2573_MAC_CSR1, 3); 1974 rum_write(sc, RT2573_MAC_CSR1, 0); 1975 1976 /* wait for BBP/RF to wakeup */ 1977 for (ntries = 0; ntries < 100; ntries++) { 1978 if (rum_read(sc, RT2573_MAC_CSR12) & 8) 1979 break; 1980 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */ 1981 if (rum_pause(sc, hz / 100)) 1982 break; 1983 } 1984 if (ntries == 100) { 1985 device_printf(sc->sc_dev, 1986 "timeout waiting for BBP/RF to wakeup\n"); 1987 goto fail; 1988 } 1989 1990 if ((error = rum_bbp_init(sc)) != 0) 1991 goto fail; 1992 1993 /* select default channel */ 1994 rum_select_band(sc, ic->ic_curchan); 1995 rum_select_antenna(sc); 1996 rum_set_chan(sc, ic->ic_curchan); 1997 1998 /* clear STA registers */ 1999 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2000 2001 rum_set_macaddr(sc, IF_LLADDR(ifp)); 2002 2003 /* initialize ASIC */ 2004 rum_write(sc, RT2573_MAC_CSR1, 4); 2005 2006 /* 2007 * Allocate Tx and Rx xfer queues. 2008 */ 2009 rum_setup_tx_list(sc); 2010 2011 /* update Rx filter */ 2012 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff; 2013 2014 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR; 2015 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2016 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR | 2017 RT2573_DROP_ACKCTS; 2018 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2019 tmp |= RT2573_DROP_TODS; 2020 if (!(ifp->if_flags & IFF_PROMISC)) 2021 tmp |= RT2573_DROP_NOT_TO_ME; 2022 } 2023 rum_write(sc, RT2573_TXRX_CSR0, tmp); 2024 2025 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2026 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2027 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]); 2028 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]); 2029 return; 2030 2031fail: rum_stop(sc); 2032#undef N 2033} 2034 2035static void 2036rum_init(void *priv) 2037{ 2038 struct rum_softc *sc = priv; 2039 struct ifnet *ifp = sc->sc_ifp; 2040 struct ieee80211com *ic = ifp->if_l2com; 2041 2042 RUM_LOCK(sc); 2043 rum_init_locked(sc); 2044 RUM_UNLOCK(sc); 2045 2046 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2047 ieee80211_start_all(ic); /* start all vap's */ 2048} 2049 2050static void 2051rum_stop(struct rum_softc *sc) 2052{ 2053 struct ifnet *ifp = sc->sc_ifp; 2054 uint32_t tmp; 2055 2056 RUM_LOCK_ASSERT(sc, MA_OWNED); 2057 2058 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2059 2060 RUM_UNLOCK(sc); 2061 2062 /* 2063 * Drain the USB transfers, if not already drained: 2064 */ 2065 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]); 2066 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]); 2067 2068 RUM_LOCK(sc); 2069 2070 rum_unsetup_tx_list(sc); 2071 2072 /* disable Rx */ 2073 tmp = rum_read(sc, RT2573_TXRX_CSR0); 2074 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 2075 2076 /* reset ASIC */ 2077 rum_write(sc, RT2573_MAC_CSR1, 3); 2078 rum_write(sc, RT2573_MAC_CSR1, 0); 2079} 2080 2081static void 2082rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size) 2083{ 2084 struct usb_device_request req; 2085 uint16_t reg = RT2573_MCU_CODE_BASE; 2086 usb_error_t err; 2087 2088 /* copy firmware image into NIC */ 2089 for (; size >= 4; reg += 4, ucode += 4, size -= 4) { 2090 err = rum_write(sc, reg, UGETDW(ucode)); 2091 if (err) { 2092 /* firmware already loaded ? */ 2093 device_printf(sc->sc_dev, "Firmware load " 2094 "failure! (ignored)\n"); 2095 break; 2096 } 2097 } 2098 2099 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 2100 req.bRequest = RT2573_MCU_CNTL; 2101 USETW(req.wValue, RT2573_MCU_RUN); 2102 USETW(req.wIndex, 0); 2103 USETW(req.wLength, 0); 2104 2105 err = rum_do_request(sc, &req, NULL); 2106 if (err != 0) { 2107 device_printf(sc->sc_dev, "could not run firmware: %s\n", 2108 usbd_errstr(err)); 2109 } 2110 2111 /* give the chip some time to boot */ 2112 rum_pause(sc, hz / 8); 2113} 2114 2115static int 2116rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap) 2117{ 2118 struct ieee80211com *ic = vap->iv_ic; 2119 const struct ieee80211_txparam *tp; 2120 struct rum_tx_desc desc; 2121 struct mbuf *m0; 2122 2123 m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo); 2124 if (m0 == NULL) { 2125 return ENOBUFS; 2126 } 2127 2128 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 2129 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ, 2130 m0->m_pkthdr.len, tp->mgmtrate); 2131 2132 /* copy the first 24 bytes of Tx descriptor into NIC memory */ 2133 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24); 2134 2135 /* copy beacon header and payload into NIC memory */ 2136 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *), 2137 m0->m_pkthdr.len); 2138 2139 m_freem(m0); 2140 2141 return 0; 2142} 2143 2144static int 2145rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2146 const struct ieee80211_bpf_params *params) 2147{ 2148 struct ifnet *ifp = ni->ni_ic->ic_ifp; 2149 struct rum_softc *sc = ifp->if_softc; 2150 2151 RUM_LOCK(sc); 2152 /* prevent management frames from being sent if we're not ready */ 2153 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2154 RUM_UNLOCK(sc); 2155 m_freem(m); 2156 ieee80211_free_node(ni); 2157 return ENETDOWN; 2158 } 2159 if (sc->tx_nfree < RUM_TX_MINFREE) { 2160 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2161 RUM_UNLOCK(sc); 2162 m_freem(m); 2163 ieee80211_free_node(ni); 2164 return EIO; 2165 } 2166 2167 ifp->if_opackets++; 2168 2169 if (params == NULL) { 2170 /* 2171 * Legacy path; interpret frame contents to decide 2172 * precisely how to send the frame. 2173 */ 2174 if (rum_tx_mgt(sc, m, ni) != 0) 2175 goto bad; 2176 } else { 2177 /* 2178 * Caller supplied explicit parameters to use in 2179 * sending the frame. 2180 */ 2181 if (rum_tx_raw(sc, m, ni, params) != 0) 2182 goto bad; 2183 } 2184 RUM_UNLOCK(sc); 2185 2186 return 0; 2187bad: 2188 ifp->if_oerrors++; 2189 RUM_UNLOCK(sc); 2190 ieee80211_free_node(ni); 2191 return EIO; 2192} 2193 2194static void 2195rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni) 2196{ 2197 struct ieee80211vap *vap = ni->ni_vap; 2198 struct rum_vap *rvp = RUM_VAP(vap); 2199 2200 /* clear statistic registers (STA_CSR0 to STA_CSR5) */ 2201 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2202 2203 ieee80211_ratectl_node_init(ni); 2204 2205 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp); 2206} 2207 2208static void 2209rum_ratectl_timeout(void *arg) 2210{ 2211 struct rum_vap *rvp = arg; 2212 struct ieee80211vap *vap = &rvp->vap; 2213 struct ieee80211com *ic = vap->iv_ic; 2214 2215 ieee80211_runtask(ic, &rvp->ratectl_task); 2216} 2217 2218static void 2219rum_ratectl_task(void *arg, int pending) 2220{ 2221 struct rum_vap *rvp = arg; 2222 struct ieee80211vap *vap = &rvp->vap; 2223 struct ieee80211com *ic = vap->iv_ic; 2224 struct ifnet *ifp = ic->ic_ifp; 2225 struct rum_softc *sc = ifp->if_softc; 2226 struct ieee80211_node *ni = vap->iv_bss; 2227 int ok, fail; 2228 int sum, retrycnt; 2229 2230 RUM_LOCK(sc); 2231 /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */ 2232 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta)); 2233 2234 ok = (le32toh(sc->sta[4]) >> 16) + /* TX ok w/o retry */ 2235 (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ retry */ 2236 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */ 2237 sum = ok+fail; 2238 retrycnt = (le32toh(sc->sta[5]) & 0xffff) + fail; 2239 2240 ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt); 2241 (void) ieee80211_ratectl_rate(ni, NULL, 0); 2242 2243 ifp->if_oerrors += fail; /* count TX retry-fail as Tx errors */ 2244 2245 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp); 2246 RUM_UNLOCK(sc); 2247} 2248 2249static void 2250rum_scan_start(struct ieee80211com *ic) 2251{ 2252 struct ifnet *ifp = ic->ic_ifp; 2253 struct rum_softc *sc = ifp->if_softc; 2254 uint32_t tmp; 2255 2256 RUM_LOCK(sc); 2257 /* abort TSF synchronization */ 2258 tmp = rum_read(sc, RT2573_TXRX_CSR9); 2259 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff); 2260 rum_set_bssid(sc, ifp->if_broadcastaddr); 2261 RUM_UNLOCK(sc); 2262 2263} 2264 2265static void 2266rum_scan_end(struct ieee80211com *ic) 2267{ 2268 struct rum_softc *sc = ic->ic_ifp->if_softc; 2269 2270 RUM_LOCK(sc); 2271 rum_enable_tsf_sync(sc); 2272 rum_set_bssid(sc, sc->sc_bssid); 2273 RUM_UNLOCK(sc); 2274 2275} 2276 2277static void 2278rum_set_channel(struct ieee80211com *ic) 2279{ 2280 struct rum_softc *sc = ic->ic_ifp->if_softc; 2281 2282 RUM_LOCK(sc); 2283 rum_set_chan(sc, ic->ic_curchan); 2284 RUM_UNLOCK(sc); 2285} 2286 2287static int 2288rum_get_rssi(struct rum_softc *sc, uint8_t raw) 2289{ 2290 struct ifnet *ifp = sc->sc_ifp; 2291 struct ieee80211com *ic = ifp->if_l2com; 2292 int lna, agc, rssi; 2293 2294 lna = (raw >> 5) & 0x3; 2295 agc = raw & 0x1f; 2296 2297 if (lna == 0) { 2298 /* 2299 * No RSSI mapping 2300 * 2301 * NB: Since RSSI is relative to noise floor, -1 is 2302 * adequate for caller to know error happened. 2303 */ 2304 return -1; 2305 } 2306 2307 rssi = (2 * agc) - RT2573_NOISE_FLOOR; 2308 2309 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) { 2310 rssi += sc->rssi_2ghz_corr; 2311 2312 if (lna == 1) 2313 rssi -= 64; 2314 else if (lna == 2) 2315 rssi -= 74; 2316 else if (lna == 3) 2317 rssi -= 90; 2318 } else { 2319 rssi += sc->rssi_5ghz_corr; 2320 2321 if (!sc->ext_5ghz_lna && lna != 1) 2322 rssi += 4; 2323 2324 if (lna == 1) 2325 rssi -= 64; 2326 else if (lna == 2) 2327 rssi -= 86; 2328 else if (lna == 3) 2329 rssi -= 100; 2330 } 2331 return rssi; 2332} 2333 2334static int 2335rum_pause(struct rum_softc *sc, int timeout) 2336{ 2337 2338 usb_pause_mtx(&sc->sc_mtx, timeout); 2339 return (0); 2340} 2341 2342static device_method_t rum_methods[] = { 2343 /* Device interface */ 2344 DEVMETHOD(device_probe, rum_match), 2345 DEVMETHOD(device_attach, rum_attach), 2346 DEVMETHOD(device_detach, rum_detach), 2347 2348 { 0, 0 } 2349}; 2350 2351static driver_t rum_driver = { 2352 .name = "rum", 2353 .methods = rum_methods, 2354 .size = sizeof(struct rum_softc), 2355}; 2356 2357static devclass_t rum_devclass; 2358 2359DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
|