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