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