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