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