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