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