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