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