188 UPGT_DEV(WISTRONNEWEB, UR045G), 189 UPGT_DEV(XYRATEX, PRISM_GT_1), 190 UPGT_DEV(XYRATEX, PRISM_GT_2), 191 UPGT_DEV(ZCOM, XG703A), 192 UPGT_DEV(ZCOM, XM142) 193}; 194 195static usb_callback_t upgt_bulk_rx_callback; 196static usb_callback_t upgt_bulk_tx_callback; 197 198static const struct usb_config upgt_config[UPGT_N_XFERS] = { 199 [UPGT_BULK_TX] = { 200 .type = UE_BULK, 201 .endpoint = UE_ADDR_ANY, 202 .direction = UE_DIR_OUT, 203 .bufsize = MCLBYTES, 204 .flags = { 205 .ext_buffer = 1, 206 .force_short_xfer = 1, 207 .pipe_bof = 1 208 }, 209 .callback = upgt_bulk_tx_callback, 210 .timeout = UPGT_USB_TIMEOUT, /* ms */ 211 }, 212 [UPGT_BULK_RX] = { 213 .type = UE_BULK, 214 .endpoint = UE_ADDR_ANY, 215 .direction = UE_DIR_IN, 216 .bufsize = MCLBYTES, 217 .flags = { 218 .ext_buffer = 1, 219 .pipe_bof = 1, 220 .short_xfer_ok = 1 221 }, 222 .callback = upgt_bulk_rx_callback, 223 }, 224}; 225 226static int 227upgt_match(device_t dev) 228{ 229 struct usb_attach_arg *uaa = device_get_ivars(dev); 230 231 if (uaa->usb_mode != USB_MODE_HOST) 232 return (ENXIO); 233 if (uaa->info.bConfigIndex != UPGT_CONFIG_INDEX) 234 return (ENXIO); 235 if (uaa->info.bIfaceIndex != UPGT_IFACE_INDEX) 236 return (ENXIO); 237 238 return (usbd_lookup_id_by_uaa(upgt_devs_2, sizeof(upgt_devs_2), uaa)); 239} 240 241static int 242upgt_attach(device_t dev) 243{ 244 int error; 245 struct ieee80211com *ic; 246 struct ifnet *ifp; 247 struct upgt_softc *sc = device_get_softc(dev); 248 struct usb_attach_arg *uaa = device_get_ivars(dev); 249 uint8_t bands, iface_index = UPGT_IFACE_INDEX; 250 251 sc->sc_dev = dev; 252 sc->sc_udev = uaa->device; 253#ifdef UPGT_DEBUG 254 sc->sc_debug = upgt_debug; 255#endif 256 device_set_usb_desc(dev); 257 258 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK, 259 MTX_DEF); 260 callout_init(&sc->sc_led_ch, 0); 261 callout_init(&sc->sc_watchdog_ch, 0); 262 263 /* Allocate TX and RX xfers. */ 264 error = upgt_alloc_tx(sc); 265 if (error) 266 goto fail1; 267 error = upgt_alloc_rx(sc); 268 if (error) 269 goto fail2; 270 271 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer, 272 upgt_config, UPGT_N_XFERS, sc, &sc->sc_mtx); 273 if (error) { 274 device_printf(dev, "could not allocate USB transfers, " 275 "err=%s\n", usbd_errstr(error)); 276 goto fail3; 277 } 278 279 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 280 if (ifp == NULL) { 281 device_printf(dev, "can not if_alloc()\n"); 282 goto fail4; 283 } 284 285 /* Initialize the device. */ 286 error = upgt_device_reset(sc); 287 if (error) 288 goto fail5; 289 /* Verify the firmware. */ 290 error = upgt_fw_verify(sc); 291 if (error) 292 goto fail5; 293 /* Calculate device memory space. */ 294 if (sc->sc_memaddr_frame_start == 0 || sc->sc_memaddr_frame_end == 0) { 295 device_printf(dev, 296 "could not find memory space addresses on FW\n"); 297 error = EIO; 298 goto fail5; 299 } 300 sc->sc_memaddr_frame_end -= UPGT_MEMSIZE_RX + 1; 301 sc->sc_memaddr_rx_start = sc->sc_memaddr_frame_end + 1; 302 303 DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame start=0x%08x\n", 304 sc->sc_memaddr_frame_start); 305 DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame end=0x%08x\n", 306 sc->sc_memaddr_frame_end); 307 DPRINTF(sc, UPGT_DEBUG_FW, "memory address rx start=0x%08x\n", 308 sc->sc_memaddr_rx_start); 309 310 upgt_mem_init(sc); 311 312 /* Load the firmware. */ 313 error = upgt_fw_load(sc); 314 if (error) 315 goto fail5; 316 317 /* Read the whole EEPROM content and parse it. */ 318 error = upgt_eeprom_read(sc); 319 if (error) 320 goto fail5; 321 error = upgt_eeprom_parse(sc); 322 if (error) 323 goto fail5; 324 325 /* all works related with the device have done here. */ 326 upgt_abort_xfers(sc); 327 328 /* Setup the 802.11 device. */ 329 ifp->if_softc = sc; 330 if_initname(ifp, "upgt", device_get_unit(sc->sc_dev)); 331 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 332 ifp->if_init = upgt_init; 333 ifp->if_ioctl = upgt_ioctl; 334 ifp->if_start = upgt_start; 335 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 336 IFQ_SET_READY(&ifp->if_snd); 337 338 ic = ifp->if_l2com; 339 ic->ic_ifp = ifp; 340 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 341 ic->ic_opmode = IEEE80211_M_STA; 342 /* set device capabilities */ 343 ic->ic_caps = 344 IEEE80211_C_STA /* station mode */ 345 | IEEE80211_C_MONITOR /* monitor mode */ 346 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 347 | IEEE80211_C_SHSLOT /* short slot time supported */ 348 | IEEE80211_C_BGSCAN /* capable of bg scanning */ 349 | IEEE80211_C_WPA /* 802.11i */ 350 ; 351 352 bands = 0; 353 setbit(&bands, IEEE80211_MODE_11B); 354 setbit(&bands, IEEE80211_MODE_11G); 355 ieee80211_init_channels(ic, NULL, &bands); 356 357 ieee80211_ifattach(ic, sc->sc_myaddr); 358 ic->ic_raw_xmit = upgt_raw_xmit; 359 ic->ic_scan_start = upgt_scan_start; 360 ic->ic_scan_end = upgt_scan_end; 361 ic->ic_set_channel = upgt_set_channel; 362 363 ic->ic_vap_create = upgt_vap_create; 364 ic->ic_vap_delete = upgt_vap_delete; 365 ic->ic_update_mcast = upgt_update_mcast; 366 367 ieee80211_radiotap_attach(ic, 368 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 369 UPGT_TX_RADIOTAP_PRESENT, 370 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 371 UPGT_RX_RADIOTAP_PRESENT); 372 373 upgt_sysctl_node(sc); 374 375 if (bootverbose) 376 ieee80211_announce(ic); 377 378 return (0); 379 380fail5: if_free(ifp); 381fail4: usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS); 382fail3: upgt_free_rx(sc); 383fail2: upgt_free_tx(sc); 384fail1: mtx_destroy(&sc->sc_mtx); 385 386 return (error); 387} 388 389static void 390upgt_txeof(struct usb_xfer *xfer, struct upgt_data *data) 391{ 392 struct upgt_softc *sc = usbd_xfer_softc(xfer); 393 struct ifnet *ifp = sc->sc_ifp; 394 struct mbuf *m; 395 396 UPGT_ASSERT_LOCKED(sc); 397 398 /* 399 * Do any tx complete callback. Note this must be done before releasing 400 * the node reference. 401 */ 402 if (data->m) { 403 m = data->m; 404 if (m->m_flags & M_TXCB) { 405 /* XXX status? */ 406 ieee80211_process_callback(data->ni, m, 0); 407 } 408 m_freem(m); 409 data->m = NULL; 410 } 411 if (data->ni) { 412 ieee80211_free_node(data->ni); 413 data->ni = NULL; 414 } 415 ifp->if_opackets++; 416} 417 418static void 419upgt_get_stats(struct upgt_softc *sc) 420{ 421 struct upgt_data *data_cmd; 422 struct upgt_lmac_mem *mem; 423 struct upgt_lmac_stats *stats; 424 425 data_cmd = upgt_getbuf(sc); 426 if (data_cmd == NULL) { 427 device_printf(sc->sc_dev, "%s: out of buffer.\n", __func__); 428 return; 429 } 430 431 /* 432 * Transmit the URB containing the CMD data. 433 */ 434 bzero(data_cmd->buf, MCLBYTES); 435 436 mem = (struct upgt_lmac_mem *)data_cmd->buf; 437 mem->addr = htole32(sc->sc_memaddr_frame_start + 438 UPGT_MEMSIZE_FRAME_HEAD); 439 440 stats = (struct upgt_lmac_stats *)(mem + 1); 441 442 stats->header1.flags = 0; 443 stats->header1.type = UPGT_H1_TYPE_CTRL; 444 stats->header1.len = htole16( 445 sizeof(struct upgt_lmac_stats) - sizeof(struct upgt_lmac_header)); 446 447 stats->header2.reqid = htole32(sc->sc_memaddr_frame_start); 448 stats->header2.type = htole16(UPGT_H2_TYPE_STATS); 449 stats->header2.flags = 0; 450 451 data_cmd->buflen = sizeof(*mem) + sizeof(*stats); 452 453 mem->chksum = upgt_chksum_le((uint32_t *)stats, 454 data_cmd->buflen - sizeof(*mem)); 455 456 upgt_bulk_tx(sc, data_cmd); 457} 458 459static int 460upgt_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 461{ 462 struct upgt_softc *sc = ifp->if_softc; 463 struct ieee80211com *ic = ifp->if_l2com; 464 struct ifreq *ifr = (struct ifreq *) data; 465 int error = 0, startall = 0; 466 467 switch (cmd) { 468 case SIOCSIFFLAGS: 469 if (ifp->if_flags & IFF_UP) { 470 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 471 if ((ifp->if_flags ^ sc->sc_if_flags) & 472 (IFF_ALLMULTI | IFF_PROMISC)) 473 upgt_set_multi(sc); 474 } else { 475 upgt_init(sc); 476 startall = 1; 477 } 478 } else { 479 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 480 upgt_stop(sc); 481 } 482 sc->sc_if_flags = ifp->if_flags; 483 if (startall) 484 ieee80211_start_all(ic); 485 break; 486 case SIOCGIFMEDIA: 487 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 488 break; 489 case SIOCGIFADDR: 490 error = ether_ioctl(ifp, cmd, data); 491 break; 492 default: 493 error = EINVAL; 494 break; 495 } 496 return error; 497} 498 499static void 500upgt_stop_locked(struct upgt_softc *sc) 501{ 502 struct ifnet *ifp = sc->sc_ifp; 503 504 UPGT_ASSERT_LOCKED(sc); 505 506 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 507 upgt_set_macfilter(sc, IEEE80211_S_INIT); 508 upgt_abort_xfers_locked(sc); 509} 510 511static void 512upgt_stop(struct upgt_softc *sc) 513{ 514 struct ifnet *ifp = sc->sc_ifp; 515 516 UPGT_LOCK(sc); 517 upgt_stop_locked(sc); 518 UPGT_UNLOCK(sc); 519 520 /* device down */ 521 sc->sc_tx_timer = 0; 522 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 523 sc->sc_flags &= ~UPGT_FLAG_INITDONE; 524} 525 526static void 527upgt_set_led(struct upgt_softc *sc, int action) 528{ 529 struct upgt_data *data_cmd; 530 struct upgt_lmac_mem *mem; 531 struct upgt_lmac_led *led; 532 533 data_cmd = upgt_getbuf(sc); 534 if (data_cmd == NULL) { 535 device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__); 536 return; 537 } 538 539 /* 540 * Transmit the URB containing the CMD data. 541 */ 542 bzero(data_cmd->buf, MCLBYTES); 543 544 mem = (struct upgt_lmac_mem *)data_cmd->buf; 545 mem->addr = htole32(sc->sc_memaddr_frame_start + 546 UPGT_MEMSIZE_FRAME_HEAD); 547 548 led = (struct upgt_lmac_led *)(mem + 1); 549 550 led->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK; 551 led->header1.type = UPGT_H1_TYPE_CTRL; 552 led->header1.len = htole16( 553 sizeof(struct upgt_lmac_led) - 554 sizeof(struct upgt_lmac_header)); 555 556 led->header2.reqid = htole32(sc->sc_memaddr_frame_start); 557 led->header2.type = htole16(UPGT_H2_TYPE_LED); 558 led->header2.flags = 0; 559 560 switch (action) { 561 case UPGT_LED_OFF: 562 led->mode = htole16(UPGT_LED_MODE_SET); 563 led->action_fix = 0; 564 led->action_tmp = htole16(UPGT_LED_ACTION_OFF); 565 led->action_tmp_dur = 0; 566 break; 567 case UPGT_LED_ON: 568 led->mode = htole16(UPGT_LED_MODE_SET); 569 led->action_fix = 0; 570 led->action_tmp = htole16(UPGT_LED_ACTION_ON); 571 led->action_tmp_dur = 0; 572 break; 573 case UPGT_LED_BLINK: 574 if (sc->sc_state != IEEE80211_S_RUN) { 575 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next); 576 return; 577 } 578 if (sc->sc_led_blink) { 579 /* previous blink was not finished */ 580 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next); 581 return; 582 } 583 led->mode = htole16(UPGT_LED_MODE_SET); 584 led->action_fix = htole16(UPGT_LED_ACTION_OFF); 585 led->action_tmp = htole16(UPGT_LED_ACTION_ON); 586 led->action_tmp_dur = htole16(UPGT_LED_ACTION_TMP_DUR); 587 /* lock blink */ 588 sc->sc_led_blink = 1; 589 callout_reset(&sc->sc_led_ch, hz, upgt_set_led_blink, sc); 590 break; 591 default: 592 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next); 593 return; 594 } 595 596 data_cmd->buflen = sizeof(*mem) + sizeof(*led); 597 598 mem->chksum = upgt_chksum_le((uint32_t *)led, 599 data_cmd->buflen - sizeof(*mem)); 600 601 upgt_bulk_tx(sc, data_cmd); 602} 603 604static void 605upgt_set_led_blink(void *arg) 606{ 607 struct upgt_softc *sc = arg; 608 609 /* blink finished, we are ready for a next one */ 610 sc->sc_led_blink = 0; 611} 612 613static void 614upgt_init(void *priv) 615{ 616 struct upgt_softc *sc = priv; 617 struct ifnet *ifp = sc->sc_ifp; 618 struct ieee80211com *ic = ifp->if_l2com; 619 620 UPGT_LOCK(sc); 621 upgt_init_locked(sc); 622 UPGT_UNLOCK(sc); 623 624 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 625 ieee80211_start_all(ic); /* start all vap's */ 626} 627 628static void 629upgt_init_locked(struct upgt_softc *sc) 630{ 631 struct ifnet *ifp = sc->sc_ifp; 632 633 UPGT_ASSERT_LOCKED(sc); 634 635 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 636 upgt_stop_locked(sc); 637 638 usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]); 639 640 (void)upgt_set_macfilter(sc, IEEE80211_S_SCAN); 641 642 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 643 ifp->if_drv_flags |= IFF_DRV_RUNNING; 644 sc->sc_flags |= UPGT_FLAG_INITDONE; 645 646 callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc); 647} 648 649static int 650upgt_set_macfilter(struct upgt_softc *sc, uint8_t state) 651{ 652 struct ifnet *ifp = sc->sc_ifp; 653 struct ieee80211com *ic = ifp->if_l2com; 654 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 655 struct ieee80211_node *ni = vap->iv_bss; 656 struct upgt_data *data_cmd; 657 struct upgt_lmac_mem *mem; 658 struct upgt_lmac_filter *filter; 659 uint8_t broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 660 661 UPGT_ASSERT_LOCKED(sc); 662 663 data_cmd = upgt_getbuf(sc); 664 if (data_cmd == NULL) { 665 device_printf(sc->sc_dev, "out of TX buffers.\n"); 666 return (ENOBUFS); 667 } 668 669 /* 670 * Transmit the URB containing the CMD data. 671 */ 672 bzero(data_cmd->buf, MCLBYTES); 673 674 mem = (struct upgt_lmac_mem *)data_cmd->buf; 675 mem->addr = htole32(sc->sc_memaddr_frame_start + 676 UPGT_MEMSIZE_FRAME_HEAD); 677 678 filter = (struct upgt_lmac_filter *)(mem + 1); 679 680 filter->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK; 681 filter->header1.type = UPGT_H1_TYPE_CTRL; 682 filter->header1.len = htole16( 683 sizeof(struct upgt_lmac_filter) - 684 sizeof(struct upgt_lmac_header)); 685 686 filter->header2.reqid = htole32(sc->sc_memaddr_frame_start); 687 filter->header2.type = htole16(UPGT_H2_TYPE_MACFILTER); 688 filter->header2.flags = 0; 689 690 switch (state) { 691 case IEEE80211_S_INIT: 692 DPRINTF(sc, UPGT_DEBUG_STATE, "%s: set MAC filter to INIT\n", 693 __func__); 694 filter->type = htole16(UPGT_FILTER_TYPE_RESET); 695 break; 696 case IEEE80211_S_SCAN: 697 DPRINTF(sc, UPGT_DEBUG_STATE, 698 "set MAC filter to SCAN (bssid %s)\n", 699 ether_sprintf(broadcast)); 700 filter->type = htole16(UPGT_FILTER_TYPE_NONE); 701 IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr); 702 IEEE80211_ADDR_COPY(filter->src, broadcast); 703 filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1); 704 filter->rxaddr = htole32(sc->sc_memaddr_rx_start); 705 filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2); 706 filter->rxhw = htole32(sc->sc_eeprom_hwrx); 707 filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3); 708 break; 709 case IEEE80211_S_RUN: 710 /* XXX monitor mode isn't tested yet. */ 711 if (vap->iv_opmode == IEEE80211_M_MONITOR) { 712 filter->type = htole16(UPGT_FILTER_TYPE_MONITOR); 713 IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr); 714 IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid); 715 filter->unknown1 = htole16(UPGT_FILTER_MONITOR_UNKNOWN1); 716 filter->rxaddr = htole32(sc->sc_memaddr_rx_start); 717 filter->unknown2 = htole16(UPGT_FILTER_MONITOR_UNKNOWN2); 718 filter->rxhw = htole32(sc->sc_eeprom_hwrx); 719 filter->unknown3 = htole16(UPGT_FILTER_MONITOR_UNKNOWN3); 720 } else { 721 DPRINTF(sc, UPGT_DEBUG_STATE, 722 "set MAC filter to RUN (bssid %s)\n", 723 ether_sprintf(ni->ni_bssid)); 724 filter->type = htole16(UPGT_FILTER_TYPE_STA); 725 IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr); 726 IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid); 727 filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1); 728 filter->rxaddr = htole32(sc->sc_memaddr_rx_start); 729 filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2); 730 filter->rxhw = htole32(sc->sc_eeprom_hwrx); 731 filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3); 732 } 733 break; 734 default: 735 device_printf(sc->sc_dev, 736 "MAC filter does not know that state\n"); 737 break; 738 } 739 740 data_cmd->buflen = sizeof(*mem) + sizeof(*filter); 741 742 mem->chksum = upgt_chksum_le((uint32_t *)filter, 743 data_cmd->buflen - sizeof(*mem)); 744 745 upgt_bulk_tx(sc, data_cmd); 746 747 return (0); 748} 749 750static void 751upgt_setup_rates(struct ieee80211vap *vap, struct ieee80211com *ic) 752{ 753 struct ifnet *ifp = ic->ic_ifp; 754 struct upgt_softc *sc = ifp->if_softc; 755 const struct ieee80211_txparam *tp; 756 757 /* 758 * 0x01 = OFMD6 0x10 = DS1 759 * 0x04 = OFDM9 0x11 = DS2 760 * 0x06 = OFDM12 0x12 = DS5 761 * 0x07 = OFDM18 0x13 = DS11 762 * 0x08 = OFDM24 763 * 0x09 = OFDM36 764 * 0x0a = OFDM48 765 * 0x0b = OFDM54 766 */ 767 const uint8_t rateset_auto_11b[] = 768 { 0x13, 0x13, 0x12, 0x11, 0x11, 0x10, 0x10, 0x10 }; 769 const uint8_t rateset_auto_11g[] = 770 { 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x04, 0x01 }; 771 const uint8_t rateset_fix_11bg[] = 772 { 0x10, 0x11, 0x12, 0x13, 0x01, 0x04, 0x06, 0x07, 773 0x08, 0x09, 0x0a, 0x0b }; 774 775 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 776 777 /* XXX */ 778 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) { 779 /* 780 * Automatic rate control is done by the device. 781 * We just pass the rateset from which the device 782 * will pickup a rate. 783 */ 784 if (ic->ic_curmode == IEEE80211_MODE_11B) 785 bcopy(rateset_auto_11b, sc->sc_cur_rateset, 786 sizeof(sc->sc_cur_rateset)); 787 if (ic->ic_curmode == IEEE80211_MODE_11G || 788 ic->ic_curmode == IEEE80211_MODE_AUTO) 789 bcopy(rateset_auto_11g, sc->sc_cur_rateset, 790 sizeof(sc->sc_cur_rateset)); 791 } else { 792 /* set a fixed rate */ 793 memset(sc->sc_cur_rateset, rateset_fix_11bg[tp->ucastrate], 794 sizeof(sc->sc_cur_rateset)); 795 } 796} 797 798static void 799upgt_set_multi(void *arg) 800{ 801 struct upgt_softc *sc = arg; 802 struct ifnet *ifp = sc->sc_ifp; 803 804 if (!(ifp->if_flags & IFF_UP)) 805 return; 806 807 /* 808 * XXX don't know how to set a device. Lack of docs. Just try to set 809 * IFF_ALLMULTI flag here. 810 */ 811 ifp->if_flags |= IFF_ALLMULTI; 812} 813 814static void 815upgt_start(struct ifnet *ifp) 816{ 817 struct upgt_softc *sc = ifp->if_softc; 818 struct upgt_data *data_tx; 819 struct ieee80211_node *ni; 820 struct mbuf *m; 821 822 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 823 return; 824 825 UPGT_LOCK(sc); 826 for (;;) { 827 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 828 if (m == NULL) 829 break; 830 831 data_tx = upgt_gettxbuf(sc); 832 if (data_tx == NULL) { 833 IFQ_DRV_PREPEND(&ifp->if_snd, m); 834 break; 835 } 836 837 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 838 m->m_pkthdr.rcvif = NULL; 839 840 if (upgt_tx_start(sc, m, ni, data_tx) != 0) { 841 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next); 842 UPGT_STAT_INC(sc, st_tx_inactive); 843 ieee80211_free_node(ni); 844 ifp->if_oerrors++; 845 continue; 846 } 847 sc->sc_tx_timer = 5; 848 } 849 UPGT_UNLOCK(sc); 850} 851 852static int 853upgt_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 854 const struct ieee80211_bpf_params *params) 855{ 856 struct ieee80211com *ic = ni->ni_ic; 857 struct ifnet *ifp = ic->ic_ifp; 858 struct upgt_softc *sc = ifp->if_softc; 859 struct upgt_data *data_tx = NULL; 860 861 /* prevent management frames from being sent if we're not ready */ 862 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 863 m_freem(m); 864 ieee80211_free_node(ni); 865 return ENETDOWN; 866 } 867 868 UPGT_LOCK(sc); 869 data_tx = upgt_gettxbuf(sc); 870 if (data_tx == NULL) { 871 ieee80211_free_node(ni); 872 m_freem(m); 873 UPGT_UNLOCK(sc); 874 return (ENOBUFS); 875 } 876 877 if (upgt_tx_start(sc, m, ni, data_tx) != 0) { 878 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next); 879 UPGT_STAT_INC(sc, st_tx_inactive); 880 ieee80211_free_node(ni); 881 ifp->if_oerrors++; 882 UPGT_UNLOCK(sc); 883 return (EIO); 884 } 885 UPGT_UNLOCK(sc); 886 887 sc->sc_tx_timer = 5; 888 return (0); 889} 890 891static void 892upgt_watchdog(void *arg) 893{ 894 struct upgt_softc *sc = arg; 895 struct ifnet *ifp = sc->sc_ifp; 896 897 if (sc->sc_tx_timer > 0) { 898 if (--sc->sc_tx_timer == 0) { 899 device_printf(sc->sc_dev, "watchdog timeout\n"); 900 /* upgt_init(ifp); XXX needs a process context ? */ 901 ifp->if_oerrors++; 902 return; 903 } 904 callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc); 905 } 906} 907 908static uint32_t 909upgt_mem_alloc(struct upgt_softc *sc) 910{ 911 int i; 912 913 for (i = 0; i < sc->sc_memory.pages; i++) { 914 if (sc->sc_memory.page[i].used == 0) { 915 sc->sc_memory.page[i].used = 1; 916 return (sc->sc_memory.page[i].addr); 917 } 918 } 919 920 return (0); 921} 922 923static void 924upgt_scan_start(struct ieee80211com *ic) 925{ 926 /* do nothing. */ 927} 928 929static void 930upgt_scan_end(struct ieee80211com *ic) 931{ 932 /* do nothing. */ 933} 934 935static void 936upgt_set_channel(struct ieee80211com *ic) 937{ 938 struct upgt_softc *sc = ic->ic_ifp->if_softc; 939 940 UPGT_LOCK(sc); 941 upgt_set_chan(sc, ic->ic_curchan); 942 UPGT_UNLOCK(sc); 943} 944 945static void 946upgt_set_chan(struct upgt_softc *sc, struct ieee80211_channel *c) 947{ 948 struct ifnet *ifp = sc->sc_ifp; 949 struct ieee80211com *ic = ifp->if_l2com; 950 struct upgt_data *data_cmd; 951 struct upgt_lmac_mem *mem; 952 struct upgt_lmac_channel *chan; 953 int channel; 954 955 UPGT_ASSERT_LOCKED(sc); 956 957 channel = ieee80211_chan2ieee(ic, c); 958 if (channel == 0 || channel == IEEE80211_CHAN_ANY) { 959 /* XXX should NEVER happen */ 960 device_printf(sc->sc_dev, 961 "%s: invalid channel %x\n", __func__, channel); 962 return; 963 } 964 965 DPRINTF(sc, UPGT_DEBUG_STATE, "%s: channel %d\n", __func__, channel); 966 967 data_cmd = upgt_getbuf(sc); 968 if (data_cmd == NULL) { 969 device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__); 970 return; 971 } 972 /* 973 * Transmit the URB containing the CMD data. 974 */ 975 bzero(data_cmd->buf, MCLBYTES); 976 977 mem = (struct upgt_lmac_mem *)data_cmd->buf; 978 mem->addr = htole32(sc->sc_memaddr_frame_start + 979 UPGT_MEMSIZE_FRAME_HEAD); 980 981 chan = (struct upgt_lmac_channel *)(mem + 1); 982 983 chan->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK; 984 chan->header1.type = UPGT_H1_TYPE_CTRL; 985 chan->header1.len = htole16( 986 sizeof(struct upgt_lmac_channel) - sizeof(struct upgt_lmac_header)); 987 988 chan->header2.reqid = htole32(sc->sc_memaddr_frame_start); 989 chan->header2.type = htole16(UPGT_H2_TYPE_CHANNEL); 990 chan->header2.flags = 0; 991 992 chan->unknown1 = htole16(UPGT_CHANNEL_UNKNOWN1); 993 chan->unknown2 = htole16(UPGT_CHANNEL_UNKNOWN2); 994 chan->freq6 = sc->sc_eeprom_freq6[channel]; 995 chan->settings = sc->sc_eeprom_freq6_settings; 996 chan->unknown3 = UPGT_CHANNEL_UNKNOWN3; 997 998 bcopy(&sc->sc_eeprom_freq3[channel].data, chan->freq3_1, 999 sizeof(chan->freq3_1)); 1000 bcopy(&sc->sc_eeprom_freq4[channel], chan->freq4, 1001 sizeof(sc->sc_eeprom_freq4[channel])); 1002 bcopy(&sc->sc_eeprom_freq3[channel].data, chan->freq3_2, 1003 sizeof(chan->freq3_2)); 1004 1005 data_cmd->buflen = sizeof(*mem) + sizeof(*chan); 1006 1007 mem->chksum = upgt_chksum_le((uint32_t *)chan, 1008 data_cmd->buflen - sizeof(*mem)); 1009 1010 upgt_bulk_tx(sc, data_cmd); 1011} 1012 1013static struct ieee80211vap * 1014upgt_vap_create(struct ieee80211com *ic, 1015 const char name[IFNAMSIZ], int unit, int opmode, int flags, 1016 const uint8_t bssid[IEEE80211_ADDR_LEN], 1017 const uint8_t mac[IEEE80211_ADDR_LEN]) 1018{ 1019 struct upgt_vap *uvp; 1020 struct ieee80211vap *vap; 1021 1022 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 1023 return NULL; 1024 uvp = (struct upgt_vap *) malloc(sizeof(struct upgt_vap), 1025 M_80211_VAP, M_NOWAIT | M_ZERO); 1026 if (uvp == NULL) 1027 return NULL; 1028 vap = &uvp->vap; 1029 /* enable s/w bmiss handling for sta mode */ 1030 ieee80211_vap_setup(ic, vap, name, unit, opmode, 1031 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac); 1032 1033 /* override state transition machine */ 1034 uvp->newstate = vap->iv_newstate; 1035 vap->iv_newstate = upgt_newstate; 1036 1037 /* setup device rates */ 1038 upgt_setup_rates(vap, ic); 1039 1040 /* complete setup */ 1041 ieee80211_vap_attach(vap, ieee80211_media_change, 1042 ieee80211_media_status); 1043 ic->ic_opmode = opmode; 1044 return vap; 1045} 1046 1047static int 1048upgt_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1049{ 1050 struct upgt_vap *uvp = UPGT_VAP(vap); 1051 struct ieee80211com *ic = vap->iv_ic; 1052 struct upgt_softc *sc = ic->ic_ifp->if_softc; 1053 1054 /* do it in a process context */ 1055 sc->sc_state = nstate; 1056 1057 IEEE80211_UNLOCK(ic); 1058 UPGT_LOCK(sc); 1059 callout_stop(&sc->sc_led_ch); 1060 callout_stop(&sc->sc_watchdog_ch); 1061 1062 switch (nstate) { 1063 case IEEE80211_S_INIT: 1064 /* do not accept any frames if the device is down */ 1065 (void)upgt_set_macfilter(sc, sc->sc_state); 1066 upgt_set_led(sc, UPGT_LED_OFF); 1067 break; 1068 case IEEE80211_S_SCAN: 1069 upgt_set_chan(sc, ic->ic_curchan); 1070 break; 1071 case IEEE80211_S_AUTH: 1072 upgt_set_chan(sc, ic->ic_curchan); 1073 break; 1074 case IEEE80211_S_ASSOC: 1075 break; 1076 case IEEE80211_S_RUN: 1077 upgt_set_macfilter(sc, sc->sc_state); 1078 upgt_set_led(sc, UPGT_LED_ON); 1079 break; 1080 default: 1081 break; 1082 } 1083 UPGT_UNLOCK(sc); 1084 IEEE80211_LOCK(ic); 1085 return (uvp->newstate(vap, nstate, arg)); 1086} 1087 1088static void 1089upgt_vap_delete(struct ieee80211vap *vap) 1090{ 1091 struct upgt_vap *uvp = UPGT_VAP(vap); 1092 1093 ieee80211_vap_detach(vap); 1094 free(uvp, M_80211_VAP); 1095} 1096 1097static void 1098upgt_update_mcast(struct ifnet *ifp) 1099{ 1100 struct upgt_softc *sc = ifp->if_softc; 1101 1102 upgt_set_multi(sc); 1103} 1104 1105static int 1106upgt_eeprom_parse(struct upgt_softc *sc) 1107{ 1108 struct upgt_eeprom_header *eeprom_header; 1109 struct upgt_eeprom_option *eeprom_option; 1110 uint16_t option_len; 1111 uint16_t option_type; 1112 uint16_t preamble_len; 1113 int option_end = 0; 1114 1115 /* calculate eeprom options start offset */ 1116 eeprom_header = (struct upgt_eeprom_header *)sc->sc_eeprom; 1117 preamble_len = le16toh(eeprom_header->preamble_len); 1118 eeprom_option = (struct upgt_eeprom_option *)(sc->sc_eeprom + 1119 (sizeof(struct upgt_eeprom_header) + preamble_len)); 1120 1121 while (!option_end) { 1122 /* the eeprom option length is stored in words */ 1123 option_len = 1124 (le16toh(eeprom_option->len) - 1) * sizeof(uint16_t); 1125 option_type = 1126 le16toh(eeprom_option->type); 1127 1128 switch (option_type) { 1129 case UPGT_EEPROM_TYPE_NAME: 1130 DPRINTF(sc, UPGT_DEBUG_FW, 1131 "EEPROM name len=%d\n", option_len); 1132 break; 1133 case UPGT_EEPROM_TYPE_SERIAL: 1134 DPRINTF(sc, UPGT_DEBUG_FW, 1135 "EEPROM serial len=%d\n", option_len); 1136 break; 1137 case UPGT_EEPROM_TYPE_MAC: 1138 DPRINTF(sc, UPGT_DEBUG_FW, 1139 "EEPROM mac len=%d\n", option_len); 1140 1141 IEEE80211_ADDR_COPY(sc->sc_myaddr, eeprom_option->data); 1142 break; 1143 case UPGT_EEPROM_TYPE_HWRX: 1144 DPRINTF(sc, UPGT_DEBUG_FW, 1145 "EEPROM hwrx len=%d\n", option_len); 1146 1147 upgt_eeprom_parse_hwrx(sc, eeprom_option->data); 1148 break; 1149 case UPGT_EEPROM_TYPE_CHIP: 1150 DPRINTF(sc, UPGT_DEBUG_FW, 1151 "EEPROM chip len=%d\n", option_len); 1152 break; 1153 case UPGT_EEPROM_TYPE_FREQ3: 1154 DPRINTF(sc, UPGT_DEBUG_FW, 1155 "EEPROM freq3 len=%d\n", option_len); 1156 1157 upgt_eeprom_parse_freq3(sc, eeprom_option->data, 1158 option_len); 1159 break; 1160 case UPGT_EEPROM_TYPE_FREQ4: 1161 DPRINTF(sc, UPGT_DEBUG_FW, 1162 "EEPROM freq4 len=%d\n", option_len); 1163 1164 upgt_eeprom_parse_freq4(sc, eeprom_option->data, 1165 option_len); 1166 break; 1167 case UPGT_EEPROM_TYPE_FREQ5: 1168 DPRINTF(sc, UPGT_DEBUG_FW, 1169 "EEPROM freq5 len=%d\n", option_len); 1170 break; 1171 case UPGT_EEPROM_TYPE_FREQ6: 1172 DPRINTF(sc, UPGT_DEBUG_FW, 1173 "EEPROM freq6 len=%d\n", option_len); 1174 1175 upgt_eeprom_parse_freq6(sc, eeprom_option->data, 1176 option_len); 1177 break; 1178 case UPGT_EEPROM_TYPE_END: 1179 DPRINTF(sc, UPGT_DEBUG_FW, 1180 "EEPROM end len=%d\n", option_len); 1181 option_end = 1; 1182 break; 1183 case UPGT_EEPROM_TYPE_OFF: 1184 DPRINTF(sc, UPGT_DEBUG_FW, 1185 "%s: EEPROM off without end option\n", __func__); 1186 return (EIO); 1187 default: 1188 DPRINTF(sc, UPGT_DEBUG_FW, 1189 "EEPROM unknown type 0x%04x len=%d\n", 1190 option_type, option_len); 1191 break; 1192 } 1193 1194 /* jump to next EEPROM option */ 1195 eeprom_option = (struct upgt_eeprom_option *) 1196 (eeprom_option->data + option_len); 1197 } 1198 1199 return (0); 1200} 1201 1202static void 1203upgt_eeprom_parse_freq3(struct upgt_softc *sc, uint8_t *data, int len) 1204{ 1205 struct upgt_eeprom_freq3_header *freq3_header; 1206 struct upgt_lmac_freq3 *freq3; 1207 int i, elements, flags; 1208 unsigned channel; 1209 1210 freq3_header = (struct upgt_eeprom_freq3_header *)data; 1211 freq3 = (struct upgt_lmac_freq3 *)(freq3_header + 1); 1212 1213 flags = freq3_header->flags; 1214 elements = freq3_header->elements; 1215 1216 DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d\n", 1217 flags, elements); 1218 1219 for (i = 0; i < elements; i++) { 1220 channel = ieee80211_mhz2ieee(le16toh(freq3[i].freq), 0); 1221 if (!(channel >= 0 && channel < IEEE80211_CHAN_MAX)) 1222 continue; 1223 1224 sc->sc_eeprom_freq3[channel] = freq3[i]; 1225 1226 DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n", 1227 le16toh(sc->sc_eeprom_freq3[channel].freq), channel); 1228 } 1229} 1230 1231void 1232upgt_eeprom_parse_freq4(struct upgt_softc *sc, uint8_t *data, int len) 1233{ 1234 struct upgt_eeprom_freq4_header *freq4_header; 1235 struct upgt_eeprom_freq4_1 *freq4_1; 1236 struct upgt_eeprom_freq4_2 *freq4_2; 1237 int i, j, elements, settings, flags; 1238 unsigned channel; 1239 1240 freq4_header = (struct upgt_eeprom_freq4_header *)data; 1241 freq4_1 = (struct upgt_eeprom_freq4_1 *)(freq4_header + 1); 1242 flags = freq4_header->flags; 1243 elements = freq4_header->elements; 1244 settings = freq4_header->settings; 1245 1246 /* we need this value later */ 1247 sc->sc_eeprom_freq6_settings = freq4_header->settings; 1248 1249 DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d settings=%d\n", 1250 flags, elements, settings); 1251 1252 for (i = 0; i < elements; i++) { 1253 channel = ieee80211_mhz2ieee(le16toh(freq4_1[i].freq), 0); 1254 if (!(channel >= 0 && channel < IEEE80211_CHAN_MAX)) 1255 continue; 1256 1257 freq4_2 = (struct upgt_eeprom_freq4_2 *)freq4_1[i].data; 1258 for (j = 0; j < settings; j++) { 1259 sc->sc_eeprom_freq4[channel][j].cmd = freq4_2[j]; 1260 sc->sc_eeprom_freq4[channel][j].pad = 0; 1261 } 1262 1263 DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n", 1264 le16toh(freq4_1[i].freq), channel); 1265 } 1266} 1267 1268void 1269upgt_eeprom_parse_freq6(struct upgt_softc *sc, uint8_t *data, int len) 1270{ 1271 struct upgt_lmac_freq6 *freq6; 1272 int i, elements; 1273 unsigned channel; 1274 1275 freq6 = (struct upgt_lmac_freq6 *)data; 1276 elements = len / sizeof(struct upgt_lmac_freq6); 1277 1278 DPRINTF(sc, UPGT_DEBUG_FW, "elements=%d\n", elements); 1279 1280 for (i = 0; i < elements; i++) { 1281 channel = ieee80211_mhz2ieee(le16toh(freq6[i].freq), 0); 1282 if (!(channel >= 0 && channel < IEEE80211_CHAN_MAX)) 1283 continue; 1284 1285 sc->sc_eeprom_freq6[channel] = freq6[i]; 1286 1287 DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n", 1288 le16toh(sc->sc_eeprom_freq6[channel].freq), channel); 1289 } 1290} 1291 1292static void 1293upgt_eeprom_parse_hwrx(struct upgt_softc *sc, uint8_t *data) 1294{ 1295 struct upgt_eeprom_option_hwrx *option_hwrx; 1296 1297 option_hwrx = (struct upgt_eeprom_option_hwrx *)data; 1298 1299 sc->sc_eeprom_hwrx = option_hwrx->rxfilter - UPGT_EEPROM_RX_CONST; 1300 1301 DPRINTF(sc, UPGT_DEBUG_FW, "hwrx option value=0x%04x\n", 1302 sc->sc_eeprom_hwrx); 1303} 1304 1305static int 1306upgt_eeprom_read(struct upgt_softc *sc) 1307{ 1308 struct upgt_data *data_cmd; 1309 struct upgt_lmac_mem *mem; 1310 struct upgt_lmac_eeprom *eeprom; 1311 int block, error, offset; 1312 1313 UPGT_LOCK(sc); 1314 usb_pause_mtx(&sc->sc_mtx, 100); 1315 1316 offset = 0; 1317 block = UPGT_EEPROM_BLOCK_SIZE; 1318 while (offset < UPGT_EEPROM_SIZE) { 1319 DPRINTF(sc, UPGT_DEBUG_FW, 1320 "request EEPROM block (offset=%d, len=%d)\n", offset, block); 1321 1322 data_cmd = upgt_getbuf(sc); 1323 if (data_cmd == NULL) { 1324 UPGT_UNLOCK(sc); 1325 return (ENOBUFS); 1326 } 1327 1328 /* 1329 * Transmit the URB containing the CMD data. 1330 */ 1331 bzero(data_cmd->buf, MCLBYTES); 1332 1333 mem = (struct upgt_lmac_mem *)data_cmd->buf; 1334 mem->addr = htole32(sc->sc_memaddr_frame_start + 1335 UPGT_MEMSIZE_FRAME_HEAD); 1336 1337 eeprom = (struct upgt_lmac_eeprom *)(mem + 1); 1338 eeprom->header1.flags = 0; 1339 eeprom->header1.type = UPGT_H1_TYPE_CTRL; 1340 eeprom->header1.len = htole16(( 1341 sizeof(struct upgt_lmac_eeprom) - 1342 sizeof(struct upgt_lmac_header)) + block); 1343 1344 eeprom->header2.reqid = htole32(sc->sc_memaddr_frame_start); 1345 eeprom->header2.type = htole16(UPGT_H2_TYPE_EEPROM); 1346 eeprom->header2.flags = 0; 1347 1348 eeprom->offset = htole16(offset); 1349 eeprom->len = htole16(block); 1350 1351 data_cmd->buflen = sizeof(*mem) + sizeof(*eeprom) + block; 1352 1353 mem->chksum = upgt_chksum_le((uint32_t *)eeprom, 1354 data_cmd->buflen - sizeof(*mem)); 1355 upgt_bulk_tx(sc, data_cmd); 1356 1357 error = mtx_sleep(sc, &sc->sc_mtx, 0, "eeprom_request", hz); 1358 if (error != 0) { 1359 device_printf(sc->sc_dev, 1360 "timeout while waiting for EEPROM data\n"); 1361 UPGT_UNLOCK(sc); 1362 return (EIO); 1363 } 1364 1365 offset += block; 1366 if (UPGT_EEPROM_SIZE - offset < block) 1367 block = UPGT_EEPROM_SIZE - offset; 1368 } 1369 1370 UPGT_UNLOCK(sc); 1371 return (0); 1372} 1373 1374/* 1375 * When a rx data came in the function returns a mbuf and a rssi values. 1376 */ 1377static struct mbuf * 1378upgt_rxeof(struct usb_xfer *xfer, struct upgt_data *data, int *rssi) 1379{ 1380 struct mbuf *m = NULL; 1381 struct upgt_softc *sc = usbd_xfer_softc(xfer); 1382 struct upgt_lmac_header *header; 1383 struct upgt_lmac_eeprom *eeprom; 1384 uint8_t h1_type; 1385 uint16_t h2_type; 1386 int actlen, sumlen; 1387 1388 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 1389 1390 UPGT_ASSERT_LOCKED(sc); 1391 1392 if (actlen < 1) 1393 return (NULL); 1394 1395 /* Check only at the very beginning. */ 1396 if (!(sc->sc_flags & UPGT_FLAG_FWLOADED) && 1397 (memcmp(data->buf, "OK", 2) == 0)) { 1398 sc->sc_flags |= UPGT_FLAG_FWLOADED; 1399 wakeup_one(sc); 1400 return (NULL); 1401 } 1402 1403 if (actlen < UPGT_RX_MINSZ) 1404 return (NULL); 1405 1406 /* 1407 * Check what type of frame came in. 1408 */ 1409 header = (struct upgt_lmac_header *)(data->buf + 4); 1410 1411 h1_type = header->header1.type; 1412 h2_type = le16toh(header->header2.type); 1413 1414 if (h1_type == UPGT_H1_TYPE_CTRL && h2_type == UPGT_H2_TYPE_EEPROM) { 1415 eeprom = (struct upgt_lmac_eeprom *)(data->buf + 4); 1416 uint16_t eeprom_offset = le16toh(eeprom->offset); 1417 uint16_t eeprom_len = le16toh(eeprom->len); 1418 1419 DPRINTF(sc, UPGT_DEBUG_FW, 1420 "received EEPROM block (offset=%d, len=%d)\n", 1421 eeprom_offset, eeprom_len); 1422 1423 bcopy(data->buf + sizeof(struct upgt_lmac_eeprom) + 4, 1424 sc->sc_eeprom + eeprom_offset, eeprom_len); 1425 1426 /* EEPROM data has arrived in time, wakeup. */ 1427 wakeup(sc); 1428 } else if (h1_type == UPGT_H1_TYPE_CTRL && 1429 h2_type == UPGT_H2_TYPE_TX_DONE) { 1430 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: received 802.11 TX done\n", 1431 __func__); 1432 upgt_tx_done(sc, data->buf + 4); 1433 } else if (h1_type == UPGT_H1_TYPE_RX_DATA || 1434 h1_type == UPGT_H1_TYPE_RX_DATA_MGMT) { 1435 DPRINTF(sc, UPGT_DEBUG_RECV, "%s: received 802.11 RX data\n", 1436 __func__); 1437 m = upgt_rx(sc, data->buf + 4, le16toh(header->header1.len), 1438 rssi); 1439 } else if (h1_type == UPGT_H1_TYPE_CTRL && 1440 h2_type == UPGT_H2_TYPE_STATS) { 1441 DPRINTF(sc, UPGT_DEBUG_STAT, "%s: received statistic data\n", 1442 __func__); 1443 /* TODO: what could we do with the statistic data? */ 1444 } else { 1445 /* ignore unknown frame types */ 1446 DPRINTF(sc, UPGT_DEBUG_INTR, 1447 "received unknown frame type 0x%02x\n", 1448 header->header1.type); 1449 } 1450 return (m); 1451} 1452 1453/* 1454 * The firmware awaits a checksum for each frame we send to it. 1455 * The algorithm used therefor is uncommon but somehow similar to CRC32. 1456 */ 1457static uint32_t 1458upgt_chksum_le(const uint32_t *buf, size_t size) 1459{ 1460 int i; 1461 uint32_t crc = 0; 1462 1463 for (i = 0; i < size; i += sizeof(uint32_t)) { 1464 crc = htole32(crc ^ *buf++); 1465 crc = htole32((crc >> 5) ^ (crc << 3)); 1466 } 1467 1468 return (crc); 1469} 1470 1471static struct mbuf * 1472upgt_rx(struct upgt_softc *sc, uint8_t *data, int pkglen, int *rssi) 1473{ 1474 struct ifnet *ifp = sc->sc_ifp; 1475 struct ieee80211com *ic = ifp->if_l2com; 1476 struct upgt_lmac_rx_desc *rxdesc; 1477 struct mbuf *m; 1478 1479 /* 1480 * don't pass packets to the ieee80211 framework if the driver isn't 1481 * RUNNING. 1482 */ 1483 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 1484 return (NULL); 1485 1486 /* access RX packet descriptor */ 1487 rxdesc = (struct upgt_lmac_rx_desc *)data; 1488 1489 /* create mbuf which is suitable for strict alignment archs */ 1490 KASSERT((pkglen + ETHER_ALIGN) < MCLBYTES, 1491 ("A current mbuf storage is small (%d)", pkglen + ETHER_ALIGN)); 1492 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1493 if (m == NULL) { 1494 device_printf(sc->sc_dev, "could not create RX mbuf\n"); 1495 return (NULL); 1496 } 1497 m_adj(m, ETHER_ALIGN); 1498 bcopy(rxdesc->data, mtod(m, char *), pkglen); 1499 /* trim FCS */ 1500 m->m_len = m->m_pkthdr.len = pkglen - IEEE80211_CRC_LEN; 1501 m->m_pkthdr.rcvif = ifp; 1502 1503 if (ieee80211_radiotap_active(ic)) { 1504 struct upgt_rx_radiotap_header *tap = &sc->sc_rxtap; 1505 1506 tap->wr_flags = 0; 1507 tap->wr_rate = upgt_rx_rate(sc, rxdesc->rate); 1508 tap->wr_antsignal = rxdesc->rssi; 1509 } 1510 ifp->if_ipackets++; 1511 1512 DPRINTF(sc, UPGT_DEBUG_RX_PROC, "%s: RX done\n", __func__); 1513 *rssi = rxdesc->rssi; 1514 return (m); 1515} 1516 1517static uint8_t 1518upgt_rx_rate(struct upgt_softc *sc, const int rate) 1519{ 1520 struct ifnet *ifp = sc->sc_ifp; 1521 struct ieee80211com *ic = ifp->if_l2com; 1522 static const uint8_t cck_upgt2rate[4] = { 2, 4, 11, 22 }; 1523 static const uint8_t ofdm_upgt2rate[12] = 1524 { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 }; 1525 1526 if (ic->ic_curmode == IEEE80211_MODE_11B && 1527 !(rate < 0 || rate > 3)) 1528 return cck_upgt2rate[rate & 0xf]; 1529 1530 if (ic->ic_curmode == IEEE80211_MODE_11G && 1531 !(rate < 0 || rate > 11)) 1532 return ofdm_upgt2rate[rate & 0xf]; 1533 1534 return (0); 1535} 1536 1537static void 1538upgt_tx_done(struct upgt_softc *sc, uint8_t *data) 1539{ 1540 struct ifnet *ifp = sc->sc_ifp; 1541 struct upgt_lmac_tx_done_desc *desc; 1542 int i, freed = 0; 1543 1544 UPGT_ASSERT_LOCKED(sc); 1545 1546 desc = (struct upgt_lmac_tx_done_desc *)data; 1547 1548 for (i = 0; i < UPGT_TX_MAXCOUNT; i++) { 1549 struct upgt_data *data_tx = &sc->sc_tx_data[i]; 1550 1551 if (data_tx->addr == le32toh(desc->header2.reqid)) { 1552 upgt_mem_free(sc, data_tx->addr); 1553 data_tx->ni = NULL; 1554 data_tx->addr = 0; 1555 data_tx->m = NULL; 1556 data_tx->use = 0; 1557 1558 DPRINTF(sc, UPGT_DEBUG_TX_PROC, 1559 "TX done: memaddr=0x%08x, status=0x%04x, rssi=%d, ", 1560 le32toh(desc->header2.reqid), 1561 le16toh(desc->status), le16toh(desc->rssi)); 1562 DPRINTF(sc, UPGT_DEBUG_TX_PROC, "seq=%d\n", 1563 le16toh(desc->seq)); 1564 1565 freed++; 1566 } 1567 } 1568 1569 if (freed != 0) { 1570 sc->sc_tx_timer = 0; 1571 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1572 UPGT_UNLOCK(sc); 1573 upgt_start(ifp); 1574 UPGT_LOCK(sc); 1575 } 1576} 1577 1578static void 1579upgt_mem_free(struct upgt_softc *sc, uint32_t addr) 1580{ 1581 int i; 1582 1583 for (i = 0; i < sc->sc_memory.pages; i++) { 1584 if (sc->sc_memory.page[i].addr == addr) { 1585 sc->sc_memory.page[i].used = 0; 1586 return; 1587 } 1588 } 1589 1590 device_printf(sc->sc_dev, 1591 "could not free memory address 0x%08x\n", addr); 1592} 1593 1594static int 1595upgt_fw_load(struct upgt_softc *sc) 1596{ 1597 const struct firmware *fw; 1598 struct upgt_data *data_cmd; 1599 struct upgt_fw_x2_header *x2; 1600 char start_fwload_cmd[] = { 0x3c, 0x0d }; 1601 int error = 0, offset, bsize, n; 1602 uint32_t crc32; 1603 1604 fw = firmware_get(upgt_fwname); 1605 if (fw == NULL) { 1606 device_printf(sc->sc_dev, "could not read microcode %s\n", 1607 upgt_fwname); 1608 return (EIO); 1609 } 1610 1611 UPGT_LOCK(sc); 1612 1613 /* send firmware start load command */ 1614 data_cmd = upgt_getbuf(sc); 1615 if (data_cmd == NULL) { 1616 error = ENOBUFS; 1617 goto fail; 1618 } 1619 data_cmd->buflen = sizeof(start_fwload_cmd); 1620 bcopy(start_fwload_cmd, data_cmd->buf, data_cmd->buflen); 1621 upgt_bulk_tx(sc, data_cmd); 1622 1623 /* send X2 header */ 1624 data_cmd = upgt_getbuf(sc); 1625 if (data_cmd == NULL) { 1626 error = ENOBUFS; 1627 goto fail; 1628 } 1629 data_cmd->buflen = sizeof(struct upgt_fw_x2_header); 1630 x2 = (struct upgt_fw_x2_header *)data_cmd->buf; 1631 bcopy(UPGT_X2_SIGNATURE, x2->signature, UPGT_X2_SIGNATURE_SIZE); 1632 x2->startaddr = htole32(UPGT_MEMADDR_FIRMWARE_START); 1633 x2->len = htole32(fw->datasize); 1634 x2->crc = upgt_crc32_le((uint8_t *)data_cmd->buf + 1635 UPGT_X2_SIGNATURE_SIZE, 1636 sizeof(struct upgt_fw_x2_header) - UPGT_X2_SIGNATURE_SIZE - 1637 sizeof(uint32_t)); 1638 upgt_bulk_tx(sc, data_cmd); 1639 1640 /* download firmware */ 1641 for (offset = 0; offset < fw->datasize; offset += bsize) { 1642 if (fw->datasize - offset > UPGT_FW_BLOCK_SIZE) 1643 bsize = UPGT_FW_BLOCK_SIZE; 1644 else 1645 bsize = fw->datasize - offset; 1646 1647 data_cmd = upgt_getbuf(sc); 1648 if (data_cmd == NULL) { 1649 error = ENOBUFS; 1650 goto fail; 1651 } 1652 n = upgt_fw_copy((const uint8_t *)fw->data + offset, 1653 data_cmd->buf, bsize); 1654 data_cmd->buflen = bsize; 1655 upgt_bulk_tx(sc, data_cmd); 1656 1657 DPRINTF(sc, UPGT_DEBUG_FW, "FW offset=%d, read=%d, sent=%d\n", 1658 offset, n, bsize); 1659 bsize = n; 1660 } 1661 DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware downloaded\n", __func__); 1662 1663 /* load firmware */ 1664 data_cmd = upgt_getbuf(sc); 1665 if (data_cmd == NULL) { 1666 error = ENOBUFS; 1667 goto fail; 1668 } 1669 crc32 = upgt_crc32_le(fw->data, fw->datasize); 1670 *((uint32_t *)(data_cmd->buf) ) = crc32; 1671 *((uint8_t *)(data_cmd->buf) + 4) = 'g'; 1672 *((uint8_t *)(data_cmd->buf) + 5) = '\r'; 1673 data_cmd->buflen = 6; 1674 upgt_bulk_tx(sc, data_cmd); 1675 1676 /* waiting 'OK' response. */ 1677 usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]); 1678 error = mtx_sleep(sc, &sc->sc_mtx, 0, "upgtfw", 2 * hz); 1679 if (error != 0) { 1680 device_printf(sc->sc_dev, "firmware load failed\n"); 1681 error = EIO; 1682 } 1683 1684 DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware loaded\n", __func__); 1685fail: 1686 UPGT_UNLOCK(sc); 1687 firmware_put(fw, FIRMWARE_UNLOAD); 1688 return (error); 1689} 1690 1691static uint32_t 1692upgt_crc32_le(const void *buf, size_t size) 1693{ 1694 uint32_t crc; 1695 1696 crc = ether_crc32_le(buf, size); 1697 1698 /* apply final XOR value as common for CRC-32 */ 1699 crc = htole32(crc ^ 0xffffffffU); 1700 1701 return (crc); 1702} 1703 1704/* 1705 * While copying the version 2 firmware, we need to replace two characters: 1706 * 1707 * 0x7e -> 0x7d 0x5e 1708 * 0x7d -> 0x7d 0x5d 1709 */ 1710static int 1711upgt_fw_copy(const uint8_t *src, char *dst, int size) 1712{ 1713 int i, j; 1714 1715 for (i = 0, j = 0; i < size && j < size; i++) { 1716 switch (src[i]) { 1717 case 0x7e: 1718 dst[j] = 0x7d; 1719 j++; 1720 dst[j] = 0x5e; 1721 j++; 1722 break; 1723 case 0x7d: 1724 dst[j] = 0x7d; 1725 j++; 1726 dst[j] = 0x5d; 1727 j++; 1728 break; 1729 default: 1730 dst[j] = src[i]; 1731 j++; 1732 break; 1733 } 1734 } 1735 1736 return (i); 1737} 1738 1739static int 1740upgt_mem_init(struct upgt_softc *sc) 1741{ 1742 int i; 1743 1744 for (i = 0; i < UPGT_MEMORY_MAX_PAGES; i++) { 1745 sc->sc_memory.page[i].used = 0; 1746 1747 if (i == 0) { 1748 /* 1749 * The first memory page is always reserved for 1750 * command data. 1751 */ 1752 sc->sc_memory.page[i].addr = 1753 sc->sc_memaddr_frame_start + MCLBYTES; 1754 } else { 1755 sc->sc_memory.page[i].addr = 1756 sc->sc_memory.page[i - 1].addr + MCLBYTES; 1757 } 1758 1759 if (sc->sc_memory.page[i].addr + MCLBYTES >= 1760 sc->sc_memaddr_frame_end) 1761 break; 1762 1763 DPRINTF(sc, UPGT_DEBUG_FW, "memory address page %d=0x%08x\n", 1764 i, sc->sc_memory.page[i].addr); 1765 } 1766 1767 sc->sc_memory.pages = i; 1768 1769 DPRINTF(sc, UPGT_DEBUG_FW, "memory pages=%d\n", sc->sc_memory.pages); 1770 return (0); 1771} 1772 1773static int 1774upgt_fw_verify(struct upgt_softc *sc) 1775{ 1776 const struct firmware *fw; 1777 const struct upgt_fw_bra_option *bra_opt; 1778 const struct upgt_fw_bra_descr *descr; 1779 const uint8_t *p; 1780 const uint32_t *uc; 1781 uint32_t bra_option_type, bra_option_len; 1782 int offset, bra_end = 0, error = 0; 1783 1784 fw = firmware_get(upgt_fwname); 1785 if (fw == NULL) { 1786 device_printf(sc->sc_dev, "could not read microcode %s\n", 1787 upgt_fwname); 1788 return EIO; 1789 } 1790 1791 /* 1792 * Seek to beginning of Boot Record Area (BRA). 1793 */ 1794 for (offset = 0; offset < fw->datasize; offset += sizeof(*uc)) { 1795 uc = (const uint32_t *)((const uint8_t *)fw->data + offset); 1796 if (*uc == 0) 1797 break; 1798 } 1799 for (; offset < fw->datasize; offset += sizeof(*uc)) { 1800 uc = (const uint32_t *)((const uint8_t *)fw->data + offset); 1801 if (*uc != 0) 1802 break; 1803 } 1804 if (offset == fw->datasize) { 1805 device_printf(sc->sc_dev, 1806 "firmware Boot Record Area not found\n"); 1807 error = EIO; 1808 goto fail; 1809 } 1810 1811 DPRINTF(sc, UPGT_DEBUG_FW, 1812 "firmware Boot Record Area found at offset %d\n", offset); 1813 1814 /* 1815 * Parse Boot Record Area (BRA) options. 1816 */ 1817 while (offset < fw->datasize && bra_end == 0) { 1818 /* get current BRA option */ 1819 p = (const uint8_t *)fw->data + offset; 1820 bra_opt = (const struct upgt_fw_bra_option *)p; 1821 bra_option_type = le32toh(bra_opt->type); 1822 bra_option_len = le32toh(bra_opt->len) * sizeof(*uc); 1823 1824 switch (bra_option_type) { 1825 case UPGT_BRA_TYPE_FW: 1826 DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_FW len=%d\n", 1827 bra_option_len); 1828 1829 if (bra_option_len != UPGT_BRA_FWTYPE_SIZE) { 1830 device_printf(sc->sc_dev, 1831 "wrong UPGT_BRA_TYPE_FW len\n"); 1832 error = EIO; 1833 goto fail; 1834 } 1835 if (memcmp(UPGT_BRA_FWTYPE_LM86, bra_opt->data, 1836 bra_option_len) == 0) { 1837 sc->sc_fw_type = UPGT_FWTYPE_LM86; 1838 break; 1839 } 1840 if (memcmp(UPGT_BRA_FWTYPE_LM87, bra_opt->data, 1841 bra_option_len) == 0) { 1842 sc->sc_fw_type = UPGT_FWTYPE_LM87; 1843 break; 1844 } 1845 device_printf(sc->sc_dev, 1846 "unsupported firmware type\n"); 1847 error = EIO; 1848 goto fail; 1849 case UPGT_BRA_TYPE_VERSION: 1850 DPRINTF(sc, UPGT_DEBUG_FW, 1851 "UPGT_BRA_TYPE_VERSION len=%d\n", bra_option_len); 1852 break; 1853 case UPGT_BRA_TYPE_DEPIF: 1854 DPRINTF(sc, UPGT_DEBUG_FW, 1855 "UPGT_BRA_TYPE_DEPIF len=%d\n", bra_option_len); 1856 break; 1857 case UPGT_BRA_TYPE_EXPIF: 1858 DPRINTF(sc, UPGT_DEBUG_FW, 1859 "UPGT_BRA_TYPE_EXPIF len=%d\n", bra_option_len); 1860 break; 1861 case UPGT_BRA_TYPE_DESCR: 1862 DPRINTF(sc, UPGT_DEBUG_FW, 1863 "UPGT_BRA_TYPE_DESCR len=%d\n", bra_option_len); 1864 1865 descr = (const struct upgt_fw_bra_descr *)bra_opt->data; 1866 1867 sc->sc_memaddr_frame_start = 1868 le32toh(descr->memaddr_space_start); 1869 sc->sc_memaddr_frame_end = 1870 le32toh(descr->memaddr_space_end); 1871 1872 DPRINTF(sc, UPGT_DEBUG_FW, 1873 "memory address space start=0x%08x\n", 1874 sc->sc_memaddr_frame_start); 1875 DPRINTF(sc, UPGT_DEBUG_FW, 1876 "memory address space end=0x%08x\n", 1877 sc->sc_memaddr_frame_end); 1878 break; 1879 case UPGT_BRA_TYPE_END: 1880 DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_END len=%d\n", 1881 bra_option_len); 1882 bra_end = 1; 1883 break; 1884 default: 1885 DPRINTF(sc, UPGT_DEBUG_FW, "unknown BRA option len=%d\n", 1886 bra_option_len); 1887 error = EIO; 1888 goto fail; 1889 } 1890 1891 /* jump to next BRA option */ 1892 offset += sizeof(struct upgt_fw_bra_option) + bra_option_len; 1893 } 1894 1895 DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware verified", __func__); 1896fail: 1897 firmware_put(fw, FIRMWARE_UNLOAD); 1898 return (error); 1899} 1900 1901static void 1902upgt_bulk_tx(struct upgt_softc *sc, struct upgt_data *data) 1903{ 1904 1905 UPGT_ASSERT_LOCKED(sc); 1906 1907 STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next); 1908 UPGT_STAT_INC(sc, st_tx_pending); 1909 usbd_transfer_start(sc->sc_xfer[UPGT_BULK_TX]); 1910} 1911 1912static int 1913upgt_device_reset(struct upgt_softc *sc) 1914{ 1915 struct upgt_data *data; 1916 char init_cmd[] = { 0x7e, 0x7e, 0x7e, 0x7e }; 1917 1918 UPGT_LOCK(sc); 1919 1920 data = upgt_getbuf(sc); 1921 if (data == NULL) { 1922 UPGT_UNLOCK(sc); 1923 return (ENOBUFS); 1924 } 1925 bcopy(init_cmd, data->buf, sizeof(init_cmd)); 1926 data->buflen = sizeof(init_cmd); 1927 upgt_bulk_tx(sc, data); 1928 usb_pause_mtx(&sc->sc_mtx, 100); 1929 1930 UPGT_UNLOCK(sc); 1931 DPRINTF(sc, UPGT_DEBUG_FW, "%s: device initialized\n", __func__); 1932 return (0); 1933} 1934 1935static int 1936upgt_alloc_tx(struct upgt_softc *sc) 1937{ 1938 int i; 1939 1940 STAILQ_INIT(&sc->sc_tx_active); 1941 STAILQ_INIT(&sc->sc_tx_inactive); 1942 STAILQ_INIT(&sc->sc_tx_pending); 1943 1944 for (i = 0; i < UPGT_TX_MAXCOUNT; i++) { 1945 struct upgt_data *data = &sc->sc_tx_data[i]; 1946 1947 data->buf = malloc(MCLBYTES, M_USBDEV, M_NOWAIT | M_ZERO); 1948 if (data->buf == NULL) { 1949 device_printf(sc->sc_dev, 1950 "could not allocate TX buffer\n"); 1951 return (ENOMEM); 1952 } 1953 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next); 1954 UPGT_STAT_INC(sc, st_tx_inactive); 1955 } 1956 1957 return (0); 1958} 1959 1960static int 1961upgt_alloc_rx(struct upgt_softc *sc) 1962{ 1963 int i; 1964 1965 STAILQ_INIT(&sc->sc_rx_active); 1966 STAILQ_INIT(&sc->sc_rx_inactive); 1967 1968 for (i = 0; i < UPGT_RX_MAXCOUNT; i++) { 1969 struct upgt_data *data = &sc->sc_rx_data[i]; 1970 1971 data->buf = malloc(MCLBYTES, M_USBDEV, M_NOWAIT | M_ZERO); 1972 if (data->buf == NULL) { 1973 device_printf(sc->sc_dev, 1974 "could not allocate RX buffer\n"); 1975 return (ENOMEM); 1976 } 1977 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 1978 } 1979 1980 return (0); 1981} 1982 1983static int 1984upgt_detach(device_t dev) 1985{ 1986 struct upgt_softc *sc = device_get_softc(dev); 1987 struct ifnet *ifp = sc->sc_ifp; 1988 struct ieee80211com *ic = ifp->if_l2com; 1989 1990 if (!device_is_attached(dev)) 1991 return 0; 1992 1993 upgt_stop(sc); 1994 1995 callout_drain(&sc->sc_led_ch); 1996 callout_drain(&sc->sc_watchdog_ch); 1997 1998 usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS); 1999 ieee80211_ifdetach(ic); 2000 upgt_free_rx(sc); 2001 upgt_free_tx(sc); 2002 2003 if_free(ifp); 2004 mtx_destroy(&sc->sc_mtx); 2005 2006 return (0); 2007} 2008 2009static void 2010upgt_free_rx(struct upgt_softc *sc) 2011{ 2012 int i; 2013 2014 for (i = 0; i < UPGT_RX_MAXCOUNT; i++) { 2015 struct upgt_data *data = &sc->sc_rx_data[i]; 2016 2017 free(data->buf, M_USBDEV); 2018 data->ni = NULL; 2019 } 2020} 2021 2022static void 2023upgt_free_tx(struct upgt_softc *sc) 2024{ 2025 int i; 2026 2027 for (i = 0; i < UPGT_TX_MAXCOUNT; i++) { 2028 struct upgt_data *data = &sc->sc_tx_data[i]; 2029 2030 free(data->buf, M_USBDEV); 2031 data->ni = NULL; 2032 } 2033} 2034 2035static void 2036upgt_abort_xfers_locked(struct upgt_softc *sc) 2037{ 2038 int i; 2039 2040 UPGT_ASSERT_LOCKED(sc); 2041 /* abort any pending transfers */ 2042 for (i = 0; i < UPGT_N_XFERS; i++) 2043 usbd_transfer_stop(sc->sc_xfer[i]); 2044} 2045 2046static void 2047upgt_abort_xfers(struct upgt_softc *sc) 2048{ 2049 2050 UPGT_LOCK(sc); 2051 upgt_abort_xfers_locked(sc); 2052 UPGT_UNLOCK(sc); 2053} 2054 2055#define UPGT_SYSCTL_STAT_ADD32(c, h, n, p, d) \ 2056 SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d) 2057 2058static void 2059upgt_sysctl_node(struct upgt_softc *sc) 2060{ 2061 struct sysctl_ctx_list *ctx; 2062 struct sysctl_oid_list *child; 2063 struct sysctl_oid *tree; 2064 struct upgt_stat *stats; 2065 2066 stats = &sc->sc_stat; 2067 ctx = device_get_sysctl_ctx(sc->sc_dev); 2068 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sc_dev)); 2069 2070 tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD, 2071 NULL, "UPGT statistics"); 2072 child = SYSCTL_CHILDREN(tree); 2073 UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_active", 2074 &stats->st_tx_active, "Active numbers in TX queue"); 2075 UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_inactive", 2076 &stats->st_tx_inactive, "Inactive numbers in TX queue"); 2077 UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_pending", 2078 &stats->st_tx_pending, "Pending numbers in TX queue"); 2079} 2080 2081#undef UPGT_SYSCTL_STAT_ADD32 2082 2083static struct upgt_data * 2084_upgt_getbuf(struct upgt_softc *sc) 2085{ 2086 struct upgt_data *bf; 2087 2088 bf = STAILQ_FIRST(&sc->sc_tx_inactive); 2089 if (bf != NULL) { 2090 STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next); 2091 UPGT_STAT_DEC(sc, st_tx_inactive); 2092 } else 2093 bf = NULL; 2094 if (bf == NULL) 2095 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: %s\n", __func__, 2096 "out of xmit buffers"); 2097 return (bf); 2098} 2099 2100static struct upgt_data * 2101upgt_getbuf(struct upgt_softc *sc) 2102{ 2103 struct upgt_data *bf; 2104 2105 UPGT_ASSERT_LOCKED(sc); 2106 2107 bf = _upgt_getbuf(sc); 2108 if (bf == NULL) { 2109 struct ifnet *ifp = sc->sc_ifp; 2110 2111 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: stop queue\n", __func__); 2112 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2113 } 2114 2115 return (bf); 2116} 2117 2118static struct upgt_data * 2119upgt_gettxbuf(struct upgt_softc *sc) 2120{ 2121 struct upgt_data *bf; 2122 2123 UPGT_ASSERT_LOCKED(sc); 2124 2125 bf = upgt_getbuf(sc); 2126 if (bf == NULL) 2127 return (NULL); 2128 2129 bf->addr = upgt_mem_alloc(sc); 2130 if (bf->addr == 0) { 2131 struct ifnet *ifp = sc->sc_ifp; 2132 2133 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: no free prism memory!\n", 2134 __func__); 2135 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next); 2136 UPGT_STAT_INC(sc, st_tx_inactive); 2137 if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE)) 2138 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2139 return (NULL); 2140 } 2141 return (bf); 2142} 2143 2144static int 2145upgt_tx_start(struct upgt_softc *sc, struct mbuf *m, struct ieee80211_node *ni, 2146 struct upgt_data *data) 2147{ 2148 struct ieee80211vap *vap = ni->ni_vap; 2149 int error = 0, len; 2150 struct ieee80211_frame *wh; 2151 struct ieee80211_key *k; 2152 struct ifnet *ifp = sc->sc_ifp; 2153 struct upgt_lmac_mem *mem; 2154 struct upgt_lmac_tx_desc *txdesc; 2155 2156 UPGT_ASSERT_LOCKED(sc); 2157 2158 upgt_set_led(sc, UPGT_LED_BLINK); 2159 2160 /* 2161 * Software crypto. 2162 */ 2163 wh = mtod(m, struct ieee80211_frame *); 2164 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 2165 k = ieee80211_crypto_encap(ni, m); 2166 if (k == NULL) { 2167 device_printf(sc->sc_dev, 2168 "ieee80211_crypto_encap returns NULL.\n"); 2169 error = EIO; 2170 goto done; 2171 } 2172 2173 /* in case packet header moved, reset pointer */ 2174 wh = mtod(m, struct ieee80211_frame *); 2175 } 2176 2177 /* Transmit the URB containing the TX data. */ 2178 bzero(data->buf, MCLBYTES); 2179 mem = (struct upgt_lmac_mem *)data->buf; 2180 mem->addr = htole32(data->addr); 2181 txdesc = (struct upgt_lmac_tx_desc *)(mem + 1); 2182 2183 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 2184 IEEE80211_FC0_TYPE_MGT) { 2185 /* mgmt frames */ 2186 txdesc->header1.flags = UPGT_H1_FLAGS_TX_MGMT; 2187 /* always send mgmt frames at lowest rate (DS1) */ 2188 memset(txdesc->rates, 0x10, sizeof(txdesc->rates)); 2189 } else { 2190 /* data frames */ 2191 txdesc->header1.flags = UPGT_H1_FLAGS_TX_DATA; 2192 bcopy(sc->sc_cur_rateset, txdesc->rates, sizeof(txdesc->rates)); 2193 } 2194 txdesc->header1.type = UPGT_H1_TYPE_TX_DATA; 2195 txdesc->header1.len = htole16(m->m_pkthdr.len); 2196 txdesc->header2.reqid = htole32(data->addr); 2197 txdesc->header2.type = htole16(UPGT_H2_TYPE_TX_ACK_YES); 2198 txdesc->header2.flags = htole16(UPGT_H2_FLAGS_TX_ACK_YES); 2199 txdesc->type = htole32(UPGT_TX_DESC_TYPE_DATA); 2200 txdesc->pad3[0] = UPGT_TX_DESC_PAD3_SIZE; 2201 2202 if (ieee80211_radiotap_active_vap(vap)) { 2203 struct upgt_tx_radiotap_header *tap = &sc->sc_txtap; 2204 2205 tap->wt_flags = 0; 2206 tap->wt_rate = 0; /* XXX where to get from? */ 2207 2208 ieee80211_radiotap_tx(vap, m); 2209 } 2210 2211 /* copy frame below our TX descriptor header */ 2212 m_copydata(m, 0, m->m_pkthdr.len, 2213 data->buf + (sizeof(*mem) + sizeof(*txdesc))); 2214 /* calculate frame size */ 2215 len = sizeof(*mem) + sizeof(*txdesc) + m->m_pkthdr.len; 2216 /* we need to align the frame to a 4 byte boundary */ 2217 len = (len + 3) & ~3; 2218 /* calculate frame checksum */ 2219 mem->chksum = upgt_chksum_le((uint32_t *)txdesc, len - sizeof(*mem)); 2220 data->ni = ni; 2221 data->m = m; 2222 data->buflen = len; 2223 2224 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: TX start data sending (%d bytes)\n", 2225 __func__, len); 2226 KASSERT(len <= MCLBYTES, ("mbuf is small for saving data")); 2227 2228 upgt_bulk_tx(sc, data); 2229done: 2230 /* 2231 * If we don't regulary read the device statistics, the RX queue 2232 * will stall. It's strange, but it works, so we keep reading 2233 * the statistics here. *shrug* 2234 */ 2235 if (!(ifp->if_opackets % UPGT_TX_STAT_INTERVAL)) 2236 upgt_get_stats(sc); 2237 2238 return (error); 2239} 2240 2241static void 2242upgt_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error) 2243{ 2244 struct upgt_softc *sc = usbd_xfer_softc(xfer); 2245 struct ifnet *ifp = sc->sc_ifp; 2246 struct ieee80211com *ic = ifp->if_l2com; 2247 struct ieee80211_frame *wh; 2248 struct ieee80211_node *ni; 2249 struct mbuf *m = NULL; 2250 struct upgt_data *data; 2251 int8_t nf; 2252 int rssi = -1; 2253 2254 UPGT_ASSERT_LOCKED(sc); 2255 2256 switch (USB_GET_STATE(xfer)) { 2257 case USB_ST_TRANSFERRED: 2258 data = STAILQ_FIRST(&sc->sc_rx_active); 2259 if (data == NULL) 2260 goto setup; 2261 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next); 2262 m = upgt_rxeof(xfer, data, &rssi); 2263 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 2264 /* FALLTHROUGH */ 2265 case USB_ST_SETUP: 2266setup: 2267 data = STAILQ_FIRST(&sc->sc_rx_inactive); 2268 if (data == NULL) 2269 return; 2270 STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next); 2271 STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next); 2272 usbd_xfer_set_frame_data(xfer, 0, data->buf, 2273 usbd_xfer_max_len(xfer)); 2274 usbd_transfer_submit(xfer); 2275 2276 /* 2277 * To avoid LOR we should unlock our private mutex here to call 2278 * ieee80211_input() because here is at the end of a USB 2279 * callback and safe to unlock. 2280 */ 2281 UPGT_UNLOCK(sc); 2282 if (m != NULL) { 2283 wh = mtod(m, struct ieee80211_frame *); 2284 ni = ieee80211_find_rxnode(ic, 2285 (struct ieee80211_frame_min *)wh); 2286 nf = -95; /* XXX */ 2287 if (ni != NULL) { 2288 (void) ieee80211_input(ni, m, rssi, nf); 2289 /* node is no longer needed */ 2290 ieee80211_free_node(ni); 2291 } else 2292 (void) ieee80211_input_all(ic, m, rssi, nf); 2293 m = NULL; 2294 } 2295 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 && 2296 !IFQ_IS_EMPTY(&ifp->if_snd)) 2297 upgt_start(ifp); 2298 UPGT_LOCK(sc); 2299 break; 2300 default: 2301 /* needs it to the inactive queue due to a error. */ 2302 data = STAILQ_FIRST(&sc->sc_rx_active); 2303 if (data != NULL) { 2304 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next); 2305 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 2306 } 2307 if (error != USB_ERR_CANCELLED) { 2308 usbd_xfer_set_stall(xfer); 2309 ifp->if_ierrors++; 2310 goto setup; 2311 } 2312 break; 2313 } 2314} 2315 2316static void 2317upgt_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error) 2318{ 2319 struct upgt_softc *sc = usbd_xfer_softc(xfer); 2320 struct ifnet *ifp = sc->sc_ifp; 2321 struct upgt_data *data; 2322 2323 UPGT_ASSERT_LOCKED(sc); 2324 switch (USB_GET_STATE(xfer)) { 2325 case USB_ST_TRANSFERRED: 2326 data = STAILQ_FIRST(&sc->sc_tx_active); 2327 if (data == NULL) 2328 goto setup; 2329 STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next); 2330 UPGT_STAT_DEC(sc, st_tx_active); 2331 upgt_txeof(xfer, data); 2332 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next); 2333 UPGT_STAT_INC(sc, st_tx_inactive); 2334 /* FALLTHROUGH */ 2335 case USB_ST_SETUP: 2336setup: 2337 data = STAILQ_FIRST(&sc->sc_tx_pending); 2338 if (data == NULL) { 2339 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: empty pending queue\n", 2340 __func__); 2341 return; 2342 } 2343 STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next); 2344 UPGT_STAT_DEC(sc, st_tx_pending); 2345 STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next); 2346 UPGT_STAT_INC(sc, st_tx_active); 2347 2348 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen); 2349 usbd_transfer_submit(xfer); 2350 UPGT_UNLOCK(sc); 2351 upgt_start(ifp); 2352 UPGT_LOCK(sc); 2353 break; 2354 default: 2355 data = STAILQ_FIRST(&sc->sc_tx_active); 2356 if (data == NULL) 2357 goto setup; 2358 if (data->ni != NULL) { 2359 ieee80211_free_node(data->ni); 2360 data->ni = NULL; 2361 ifp->if_oerrors++; 2362 } 2363 if (error != USB_ERR_CANCELLED) { 2364 usbd_xfer_set_stall(xfer); 2365 goto setup; 2366 } 2367 break; 2368 } 2369} 2370 2371static device_method_t upgt_methods[] = { 2372 /* Device interface */ 2373 DEVMETHOD(device_probe, upgt_match), 2374 DEVMETHOD(device_attach, upgt_attach), 2375 DEVMETHOD(device_detach, upgt_detach), 2376 2377 { 0, 0 } 2378}; 2379 2380static driver_t upgt_driver = { 2381 "upgt", 2382 upgt_methods, 2383 sizeof(struct upgt_softc) 2384}; 2385 2386static devclass_t upgt_devclass; 2387 2388DRIVER_MODULE(if_upgt, uhub, upgt_driver, upgt_devclass, NULL, 0); 2389MODULE_VERSION(if_upgt, 1); 2390MODULE_DEPEND(if_upgt, usb, 1, 1, 1); 2391MODULE_DEPEND(if_upgt, wlan, 1, 1, 1); 2392MODULE_DEPEND(if_upgt, upgtfw_fw, 1, 1, 1);
|