1 /* $OpenBSD: if_upgt.c,v 1.47 2009/08/10 20:02:19 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Marcus Glocker <mglocker@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include "bpfilter.h" 20 21 #include <sys/param.h> 22 #include <sys/sockio.h> 23 #include <sys/sysctl.h> 24 #include <sys/mbuf.h> 25 #include <sys/kernel.h> 26 #include <sys/socket.h> 27 #include <sys/systm.h> 28 #include <sys/timeout.h> 29 #include <sys/conf.h> 30 #include <sys/device.h> 31 32 #include <machine/bus.h> 33 #include <machine/endian.h> 34 #include <machine/intr.h> 35 36 #if NBPFILTER > 0 37 #include <net/bpf.h> 38 #endif 39 #include <net/if.h> 40 #include <net/if_arp.h> 41 #include <net/if_dl.h> 42 #include <net/if_media.h> 43 #include <net/if_types.h> 44 45 #include <netinet/in.h> 46 #include <netinet/in_systm.h> 47 #include <netinet/in_var.h> 48 #include <netinet/if_ether.h> 49 #include <netinet/ip.h> 50 51 #include <net80211/ieee80211_var.h> 52 #include <net80211/ieee80211_radiotap.h> 53 54 #include <dev/usb/usb.h> 55 #include <dev/usb/usbdi.h> 56 #include <dev/usb/usbdi_util.h> 57 #include <dev/usb/usbdevs.h> 58 59 #include <dev/usb/if_upgtvar.h> 60 61 /* 62 * Driver for the USB PrismGT devices. 63 * 64 * For now just USB 2.0 devices with the GW3887 chipset are supported. 65 * The driver has been written based on the firmware version 2.13.1.0_LM87. 66 * 67 * TODO's: 68 * - Fix MONITOR mode (MAC filter). 69 * - Add HOSTAP mode. 70 * - Add IBSS mode. 71 * - Support the USB 1.0 devices (NET2280, ISL3880, ISL3886 chipsets). 72 * 73 * Parts of this driver has been influenced by reading the p54u driver 74 * written by Jean-Baptiste Note <jean-baptiste.note@m4x.org> and 75 * Sebastien Bourdeauducq <lekernel@prism54.org>. 76 */ 77 78 #ifdef UPGT_DEBUG 79 int upgt_debug = 2; 80 #define DPRINTF(l, x...) do { if ((l) <= upgt_debug) printf(x); } while (0) 81 #else 82 #define DPRINTF(l, x...) 83 #endif 84 85 /* 86 * Prototypes. 87 */ 88 int upgt_match(struct device *, void *, void *); 89 void upgt_attach(struct device *, struct device *, void *); 90 void upgt_attach_hook(void *); 91 int upgt_detach(struct device *, int); 92 int upgt_activate(struct device *, enum devact); 93 94 int upgt_device_type(struct upgt_softc *, uint16_t, uint16_t); 95 int upgt_device_init(struct upgt_softc *); 96 int upgt_mem_init(struct upgt_softc *); 97 uint32_t upgt_mem_alloc(struct upgt_softc *); 98 void upgt_mem_free(struct upgt_softc *, uint32_t); 99 int upgt_fw_alloc(struct upgt_softc *); 100 void upgt_fw_free(struct upgt_softc *); 101 int upgt_fw_verify(struct upgt_softc *); 102 int upgt_fw_load(struct upgt_softc *); 103 int upgt_fw_copy(char *, char *, int); 104 int upgt_eeprom_read(struct upgt_softc *); 105 int upgt_eeprom_parse(struct upgt_softc *); 106 void upgt_eeprom_parse_hwrx(struct upgt_softc *, uint8_t *); 107 void upgt_eeprom_parse_freq3(struct upgt_softc *, uint8_t *, int); 108 void upgt_eeprom_parse_freq4(struct upgt_softc *, uint8_t *, int); 109 void upgt_eeprom_parse_freq6(struct upgt_softc *, uint8_t *, int); 110 111 int upgt_ioctl(struct ifnet *, u_long, caddr_t); 112 int upgt_init(struct ifnet *); 113 void upgt_stop(struct upgt_softc *); 114 int upgt_media_change(struct ifnet *); 115 void upgt_newassoc(struct ieee80211com *, struct ieee80211_node *, 116 int); 117 int upgt_newstate(struct ieee80211com *, enum ieee80211_state, int); 118 void upgt_newstate_task(void *); 119 void upgt_next_scan(void *); 120 void upgt_start(struct ifnet *); 121 void upgt_watchdog(struct ifnet *); 122 void upgt_tx_task(void *); 123 void upgt_tx_done(struct upgt_softc *, uint8_t *); 124 void upgt_rx_cb(usbd_xfer_handle, usbd_private_handle, usbd_status); 125 void upgt_rx(struct upgt_softc *, uint8_t *, int); 126 void upgt_setup_rates(struct upgt_softc *); 127 uint8_t upgt_rx_rate(struct upgt_softc *, const int); 128 int upgt_set_macfilter(struct upgt_softc *, uint8_t state); 129 int upgt_set_channel(struct upgt_softc *, unsigned); 130 void upgt_set_led(struct upgt_softc *, int); 131 void upgt_set_led_blink(void *); 132 int upgt_get_stats(struct upgt_softc *); 133 134 int upgt_alloc_tx(struct upgt_softc *); 135 int upgt_alloc_rx(struct upgt_softc *); 136 int upgt_alloc_cmd(struct upgt_softc *); 137 void upgt_free_tx(struct upgt_softc *); 138 void upgt_free_rx(struct upgt_softc *); 139 void upgt_free_cmd(struct upgt_softc *); 140 int upgt_bulk_xmit(struct upgt_softc *, struct upgt_data *, 141 usbd_pipe_handle, uint32_t *, int); 142 143 void upgt_hexdump(void *, int); 144 uint32_t upgt_crc32_le(const void *, size_t); 145 uint32_t upgt_chksum_le(const uint32_t *, size_t); 146 147 struct cfdriver upgt_cd = { 148 NULL, "upgt", DV_IFNET 149 }; 150 151 const struct cfattach upgt_ca = { 152 sizeof(struct upgt_softc), 153 upgt_match, 154 upgt_attach, 155 upgt_detach, 156 upgt_activate, 157 }; 158 159 static const struct usb_devno upgt_devs_1[] = { 160 /* version 1 devices */ 161 { USB_VENDOR_ALCATELT, USB_PRODUCT_ALCATELT_ST120G } 162 }; 163 164 static const struct usb_devno upgt_devs_2[] = { 165 /* version 2 devices */ 166 { USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_PRISM_GT }, 167 { USB_VENDOR_ALCATELT, USB_PRODUCT_ALCATELT_ST121G }, 168 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050 }, 169 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54AG }, 170 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GV2 }, 171 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_PRISM_GT }, 172 { USB_VENDOR_DELL, USB_PRODUCT_DELL_PRISM_GT_1 }, 173 { USB_VENDOR_DELL, USB_PRODUCT_DELL_PRISM_GT_2 }, 174 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWLG122A2 }, 175 { USB_VENDOR_FSC, USB_PRODUCT_FSC_E5400 }, 176 { USB_VENDOR_GLOBESPAN, USB_PRODUCT_GLOBESPAN_PRISM_GT_1 }, 177 { USB_VENDOR_GLOBESPAN, USB_PRODUCT_GLOBESPAN_PRISM_GT_2 }, 178 { USB_VENDOR_INTERSIL, USB_PRODUCT_INTERSIL_PRISM_GT }, 179 { USB_VENDOR_PHEENET, USB_PRODUCT_PHEENET_GWU513 }, 180 { USB_VENDOR_PHILIPS, USB_PRODUCT_PHILIPS_CPWUA054 }, 181 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2862WG }, 182 { USB_VENDOR_USR, USB_PRODUCT_USR_USR5422 }, 183 { USB_VENDOR_WISTRONNEWEB, USB_PRODUCT_WISTRONNEWEB_UR045G }, 184 { USB_VENDOR_XYRATEX, USB_PRODUCT_XYRATEX_PRISM_GT_1 }, 185 { USB_VENDOR_XYRATEX, USB_PRODUCT_XYRATEX_PRISM_GT_2 }, 186 { USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_MD40900 }, 187 { USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_XG703A } 188 }; 189 190 int 191 upgt_match(struct device *parent, void *match, void *aux) 192 { 193 struct usb_attach_arg *uaa = aux; 194 195 if (uaa->iface != NULL) 196 return (UMATCH_NONE); 197 198 if (usb_lookup(upgt_devs_1, uaa->vendor, uaa->product) != NULL) 199 return (UMATCH_VENDOR_PRODUCT); 200 201 if (usb_lookup(upgt_devs_2, uaa->vendor, uaa->product) != NULL) 202 return (UMATCH_VENDOR_PRODUCT); 203 204 return (UMATCH_NONE); 205 } 206 207 void 208 upgt_attach(struct device *parent, struct device *self, void *aux) 209 { 210 struct upgt_softc *sc = (struct upgt_softc *)self; 211 struct usb_attach_arg *uaa = aux; 212 usb_interface_descriptor_t *id; 213 usb_endpoint_descriptor_t *ed; 214 usbd_status error; 215 int i; 216 217 /* 218 * Attach USB device. 219 */ 220 sc->sc_udev = uaa->device; 221 222 /* check device type */ 223 if (upgt_device_type(sc, uaa->vendor, uaa->product) != 0) 224 return; 225 226 /* set configuration number */ 227 if (usbd_set_config_no(sc->sc_udev, UPGT_CONFIG_NO, 0) != 0) { 228 printf("%s: could not set configuration no!\n", 229 sc->sc_dev.dv_xname); 230 return; 231 } 232 233 /* get the first interface handle */ 234 error = usbd_device2interface_handle(sc->sc_udev, UPGT_IFACE_INDEX, 235 &sc->sc_iface); 236 if (error != 0) { 237 printf("%s: could not get interface handle!\n", 238 sc->sc_dev.dv_xname); 239 return; 240 } 241 242 /* find endpoints */ 243 id = usbd_get_interface_descriptor(sc->sc_iface); 244 sc->sc_rx_no = sc->sc_tx_no = -1; 245 for (i = 0; i < id->bNumEndpoints; i++) { 246 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 247 if (ed == NULL) { 248 printf("%s: no endpoint descriptor for iface %d!\n", 249 sc->sc_dev.dv_xname, i); 250 return; 251 } 252 253 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 254 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 255 sc->sc_tx_no = ed->bEndpointAddress; 256 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 257 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 258 sc->sc_rx_no = ed->bEndpointAddress; 259 260 /* 261 * 0x01 TX pipe 262 * 0x81 RX pipe 263 * 264 * Deprecated scheme (not used with fw version >2.5.6.x): 265 * 0x02 TX MGMT pipe 266 * 0x82 TX MGMT pipe 267 */ 268 if (sc->sc_tx_no != -1 && sc->sc_rx_no != -1) 269 break; 270 } 271 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) { 272 printf("%s: missing endpoint!\n", sc->sc_dev.dv_xname); 273 return; 274 } 275 276 /* setup tasks and timeouts */ 277 usb_init_task(&sc->sc_task_newstate, upgt_newstate_task, sc); 278 usb_init_task(&sc->sc_task_tx, upgt_tx_task, sc); 279 timeout_set(&sc->scan_to, upgt_next_scan, sc); 280 timeout_set(&sc->led_to, upgt_set_led_blink, sc); 281 282 /* 283 * Open TX and RX USB bulk pipes. 284 */ 285 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE, 286 &sc->sc_tx_pipeh); 287 if (error != 0) { 288 printf("%s: could not open TX pipe: %s!\n", 289 sc->sc_dev.dv_xname, usbd_errstr(error)); 290 goto fail; 291 } 292 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE, 293 &sc->sc_rx_pipeh); 294 if (error != 0) { 295 printf("%s: could not open RX pipe: %s!\n", 296 sc->sc_dev.dv_xname, usbd_errstr(error)); 297 goto fail; 298 } 299 300 /* 301 * Allocate TX, RX, and CMD xfers. 302 */ 303 if (upgt_alloc_tx(sc) != 0) 304 goto fail; 305 if (upgt_alloc_rx(sc) != 0) 306 goto fail; 307 if (upgt_alloc_cmd(sc) != 0) 308 goto fail; 309 310 /* 311 * We need the firmware loaded to complete the attach. 312 */ 313 if (rootvp == NULL) 314 mountroothook_establish(upgt_attach_hook, sc); 315 else 316 upgt_attach_hook(sc); 317 318 return; 319 fail: 320 printf("%s: %s failed!\n", sc->sc_dev.dv_xname, __func__); 321 } 322 323 void 324 upgt_attach_hook(void *arg) 325 { 326 struct upgt_softc *sc = arg; 327 struct ieee80211com *ic = &sc->sc_ic; 328 struct ifnet *ifp = &ic->ic_if; 329 usbd_status error; 330 int i; 331 332 /* 333 * Load firmware file into memory. 334 */ 335 if (upgt_fw_alloc(sc) != 0) 336 goto fail; 337 338 /* 339 * Initialize the device. 340 */ 341 if (upgt_device_init(sc) != 0) 342 goto fail; 343 344 /* 345 * Verify the firmware. 346 */ 347 if (upgt_fw_verify(sc) != 0) 348 goto fail; 349 350 /* 351 * Calculate device memory space. 352 */ 353 if (sc->sc_memaddr_frame_start == 0 || sc->sc_memaddr_frame_end == 0) { 354 printf("%s: could not find memory space addresses on FW!\n", 355 sc->sc_dev.dv_xname); 356 goto fail; 357 } 358 sc->sc_memaddr_frame_end -= UPGT_MEMSIZE_RX + 1; 359 sc->sc_memaddr_rx_start = sc->sc_memaddr_frame_end + 1; 360 361 DPRINTF(1, "%s: memory address frame start=0x%08x\n", 362 sc->sc_dev.dv_xname, sc->sc_memaddr_frame_start); 363 DPRINTF(1, "%s: memory address frame end=0x%08x\n", 364 sc->sc_dev.dv_xname, sc->sc_memaddr_frame_end); 365 DPRINTF(1, "%s: memory address rx start=0x%08x\n", 366 sc->sc_dev.dv_xname, sc->sc_memaddr_rx_start); 367 368 upgt_mem_init(sc); 369 370 /* 371 * Load the firmware. 372 */ 373 if (upgt_fw_load(sc) != 0) 374 goto fail; 375 376 /* 377 * Startup the RX pipe. 378 */ 379 struct upgt_data *data_rx = &sc->rx_data; 380 381 usbd_setup_xfer(data_rx->xfer, sc->sc_rx_pipeh, data_rx, data_rx->buf, 382 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, upgt_rx_cb); 383 error = usbd_transfer(data_rx->xfer); 384 if (error != 0 && error != USBD_IN_PROGRESS) { 385 printf("%s: could not queue RX transfer!\n", 386 sc->sc_dev.dv_xname); 387 goto fail; 388 } 389 usbd_delay_ms(sc->sc_udev, 100); 390 391 /* 392 * Read the whole EEPROM content and parse it. 393 */ 394 if (upgt_eeprom_read(sc) != 0) 395 goto fail; 396 if (upgt_eeprom_parse(sc) != 0) 397 goto fail; 398 399 /* 400 * Setup the 802.11 device. 401 */ 402 ic->ic_phytype = IEEE80211_T_OFDM; 403 ic->ic_opmode = IEEE80211_M_STA; 404 ic->ic_state = IEEE80211_S_INIT; 405 ic->ic_caps = 406 IEEE80211_C_MONITOR | 407 IEEE80211_C_SHPREAMBLE | 408 IEEE80211_C_SHSLOT | 409 IEEE80211_C_WEP | 410 IEEE80211_C_RSN; 411 412 ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b; 413 ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g; 414 415 for (i = 1; i <= 14; i++) { 416 ic->ic_channels[i].ic_freq = 417 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ); 418 ic->ic_channels[i].ic_flags = 419 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 420 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 421 } 422 423 ifp->if_softc = sc; 424 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 425 ifp->if_init = upgt_init; 426 ifp->if_ioctl = upgt_ioctl; 427 ifp->if_start = upgt_start; 428 ifp->if_watchdog = upgt_watchdog; 429 IFQ_SET_READY(&ifp->if_snd); 430 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ); 431 432 if_attach(ifp); 433 ieee80211_ifattach(ifp); 434 ic->ic_newassoc = upgt_newassoc; 435 436 sc->sc_newstate = ic->ic_newstate; 437 ic->ic_newstate = upgt_newstate; 438 ieee80211_media_init(ifp, upgt_media_change, ieee80211_media_status); 439 440 #if NBPFILTER > 0 441 bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO, 442 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN); 443 444 sc->sc_rxtap_len = sizeof(sc->sc_rxtapu); 445 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 446 sc->sc_rxtap.wr_ihdr.it_present = htole32(UPGT_RX_RADIOTAP_PRESENT); 447 448 sc->sc_txtap_len = sizeof(sc->sc_txtapu); 449 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 450 sc->sc_txtap.wt_ihdr.it_present = htole32(UPGT_TX_RADIOTAP_PRESENT); 451 #endif 452 453 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, &sc->sc_dev); 454 455 printf("%s: address %s\n", 456 sc->sc_dev.dv_xname, ether_sprintf(ic->ic_myaddr)); 457 458 /* device attached */ 459 sc->sc_flags |= UPGT_DEVICE_ATTACHED; 460 461 return; 462 fail: 463 printf("%s: %s failed!\n", sc->sc_dev.dv_xname, __func__); 464 } 465 466 int 467 upgt_detach(struct device *self, int flags) 468 { 469 struct upgt_softc *sc = (struct upgt_softc *)self; 470 struct ifnet *ifp = &sc->sc_ic.ic_if; 471 int s; 472 473 DPRINTF(1, "%s: %s\n", sc->sc_dev.dv_xname, __func__); 474 475 s = splusb(); 476 477 /* abort and close TX / RX pipes */ 478 if (sc->sc_tx_pipeh != NULL) { 479 usbd_abort_pipe(sc->sc_tx_pipeh); 480 usbd_close_pipe(sc->sc_tx_pipeh); 481 } 482 if (sc->sc_rx_pipeh != NULL) { 483 usbd_abort_pipe(sc->sc_rx_pipeh); 484 usbd_close_pipe(sc->sc_rx_pipeh); 485 } 486 487 /* remove tasks and timeouts */ 488 usb_rem_task(sc->sc_udev, &sc->sc_task_newstate); 489 usb_rem_task(sc->sc_udev, &sc->sc_task_tx); 490 timeout_del(&sc->scan_to); 491 timeout_del(&sc->led_to); 492 493 /* free xfers */ 494 upgt_free_tx(sc); 495 upgt_free_rx(sc); 496 upgt_free_cmd(sc); 497 498 /* free firmware */ 499 upgt_fw_free(sc); 500 501 if (sc->sc_flags & UPGT_DEVICE_ATTACHED) { 502 /* detach interface */ 503 ieee80211_ifdetach(ifp); 504 if_detach(ifp); 505 } 506 507 splx(s); 508 509 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, &sc->sc_dev); 510 511 return (0); 512 } 513 514 int 515 upgt_activate(struct device *self, enum devact act) 516 { 517 switch (act) { 518 case DVACT_ACTIVATE: 519 break; 520 case DVACT_DEACTIVATE: 521 break; 522 } 523 524 return (0); 525 } 526 527 int 528 upgt_device_type(struct upgt_softc *sc, uint16_t vendor, uint16_t product) 529 { 530 if (usb_lookup(upgt_devs_1, vendor, product) != NULL) { 531 sc->sc_device_type = 1; 532 /* XXX */ 533 printf("%s: version 1 devices not supported yet!\n", 534 sc->sc_dev.dv_xname); 535 return (1); 536 } else { 537 sc->sc_device_type = 2; 538 } 539 540 return (0); 541 } 542 543 int 544 upgt_device_init(struct upgt_softc *sc) 545 { 546 struct upgt_data *data_cmd = &sc->cmd_data; 547 char init_cmd[] = { 0x7e, 0x7e, 0x7e, 0x7e }; 548 int len; 549 550 len = sizeof(init_cmd); 551 bcopy(init_cmd, data_cmd->buf, len); 552 if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) { 553 printf("%s: could not send device init string!\n", 554 sc->sc_dev.dv_xname); 555 return (EIO); 556 } 557 usbd_delay_ms(sc->sc_udev, 100); 558 559 DPRINTF(1, "%s: device initialized\n", sc->sc_dev.dv_xname); 560 561 return (0); 562 } 563 564 int 565 upgt_mem_init(struct upgt_softc *sc) 566 { 567 int i; 568 569 for (i = 0; i < UPGT_MEMORY_MAX_PAGES; i++) { 570 sc->sc_memory.page[i].used = 0; 571 572 if (i == 0) { 573 /* 574 * The first memory page is always reserved for 575 * command data. 576 */ 577 sc->sc_memory.page[i].addr = 578 sc->sc_memaddr_frame_start + MCLBYTES; 579 } else { 580 sc->sc_memory.page[i].addr = 581 sc->sc_memory.page[i - 1].addr + MCLBYTES; 582 } 583 584 if (sc->sc_memory.page[i].addr + MCLBYTES >= 585 sc->sc_memaddr_frame_end) 586 break; 587 588 DPRINTF(2, "%s: memory address page %d=0x%08x\n", 589 sc->sc_dev.dv_xname, i, sc->sc_memory.page[i].addr); 590 } 591 592 sc->sc_memory.pages = i; 593 594 DPRINTF(2, "%s: memory pages=%d\n", 595 sc->sc_dev.dv_xname, sc->sc_memory.pages); 596 597 return (0); 598 } 599 600 uint32_t 601 upgt_mem_alloc(struct upgt_softc *sc) 602 { 603 int i; 604 605 for (i = 0; i < sc->sc_memory.pages; i++) { 606 if (sc->sc_memory.page[i].used == 0) { 607 sc->sc_memory.page[i].used = 1; 608 return (sc->sc_memory.page[i].addr); 609 } 610 } 611 612 return (0); 613 } 614 615 void 616 upgt_mem_free(struct upgt_softc *sc, uint32_t addr) 617 { 618 int i; 619 620 for (i = 0; i < sc->sc_memory.pages; i++) { 621 if (sc->sc_memory.page[i].addr == addr) { 622 sc->sc_memory.page[i].used = 0; 623 return; 624 } 625 } 626 627 printf("%s: could not free memory address 0x%08x!\n", 628 sc->sc_dev.dv_xname, addr); 629 } 630 631 632 int 633 upgt_fw_alloc(struct upgt_softc *sc) 634 { 635 const char *name = "upgt-gw3887"; 636 int error; 637 638 if (sc->sc_fw == NULL) { 639 error = loadfirmware(name, &sc->sc_fw, &sc->sc_fw_size); 640 if (error != 0) { 641 printf("%s: error %d, could not read firmware %s!\n", 642 sc->sc_dev.dv_xname, error, name); 643 return (EIO); 644 } 645 } 646 647 DPRINTF(1, "%s: firmware %s allocated\n", sc->sc_dev.dv_xname, name); 648 649 return (0); 650 } 651 652 void 653 upgt_fw_free(struct upgt_softc *sc) 654 { 655 if (sc->sc_fw != NULL) { 656 free(sc->sc_fw, M_DEVBUF); 657 sc->sc_fw = NULL; 658 DPRINTF(1, "%s: firmware freed\n", sc->sc_dev.dv_xname); 659 } 660 } 661 662 int 663 upgt_fw_verify(struct upgt_softc *sc) 664 { 665 struct upgt_fw_bra_option *bra_option; 666 uint32_t bra_option_type, bra_option_len; 667 uint32_t *uc; 668 int offset, bra_end = 0; 669 670 /* 671 * Seek to beginning of Boot Record Area (BRA). 672 */ 673 for (offset = 0; offset < sc->sc_fw_size; offset += sizeof(*uc)) { 674 uc = (uint32_t *)(sc->sc_fw + offset); 675 if (*uc == 0) 676 break; 677 } 678 for (; offset < sc->sc_fw_size; offset += sizeof(*uc)) { 679 uc = (uint32_t *)(sc->sc_fw + offset); 680 if (*uc != 0) 681 break; 682 } 683 if (offset == sc->sc_fw_size) { 684 printf("%s: firmware Boot Record Area not found!\n", 685 sc->sc_dev.dv_xname); 686 return (EIO); 687 } 688 DPRINTF(1, "%s: firmware Boot Record Area found at offset %d\n", 689 sc->sc_dev.dv_xname, offset); 690 691 /* 692 * Parse Boot Record Area (BRA) options. 693 */ 694 while (offset < sc->sc_fw_size && bra_end == 0) { 695 /* get current BRA option */ 696 bra_option = (struct upgt_fw_bra_option *)(sc->sc_fw + offset); 697 bra_option_type = letoh32(bra_option->type); 698 bra_option_len = letoh32(bra_option->len) * sizeof(*uc); 699 700 switch (bra_option_type) { 701 case UPGT_BRA_TYPE_FW: 702 DPRINTF(1, "%s: UPGT_BRA_TYPE_FW len=%d\n", 703 sc->sc_dev.dv_xname, bra_option_len); 704 705 if (bra_option_len != UPGT_BRA_FWTYPE_SIZE) { 706 printf("%s: wrong UPGT_BRA_TYPE_FW len!\n", 707 sc->sc_dev.dv_xname); 708 return (EIO); 709 } 710 if (memcmp(UPGT_BRA_FWTYPE_LM86, bra_option->data, 711 bra_option_len) == 0) { 712 sc->sc_fw_type = UPGT_FWTYPE_LM86; 713 break; 714 } 715 if (memcmp(UPGT_BRA_FWTYPE_LM87, bra_option->data, 716 bra_option_len) == 0) { 717 sc->sc_fw_type = UPGT_FWTYPE_LM87; 718 break; 719 } 720 if (memcmp(UPGT_BRA_FWTYPE_FMAC, bra_option->data, 721 bra_option_len) == 0) { 722 sc->sc_fw_type = UPGT_FWTYPE_FMAC; 723 break; 724 } 725 printf("%s: unsupported firmware type!\n", 726 sc->sc_dev.dv_xname); 727 return (EIO); 728 case UPGT_BRA_TYPE_VERSION: 729 DPRINTF(1, "%s: UPGT_BRA_TYPE_VERSION len=%d\n", 730 sc->sc_dev.dv_xname, bra_option_len); 731 break; 732 case UPGT_BRA_TYPE_DEPIF: 733 DPRINTF(1, "%s: UPGT_BRA_TYPE_DEPIF len=%d\n", 734 sc->sc_dev.dv_xname, bra_option_len); 735 break; 736 case UPGT_BRA_TYPE_EXPIF: 737 DPRINTF(1, "%s: UPGT_BRA_TYPE_EXPIF len=%d\n", 738 sc->sc_dev.dv_xname, bra_option_len); 739 break; 740 case UPGT_BRA_TYPE_DESCR: 741 DPRINTF(1, "%s: UPGT_BRA_TYPE_DESCR len=%d\n", 742 sc->sc_dev.dv_xname, bra_option_len); 743 744 struct upgt_fw_bra_descr *descr = 745 (struct upgt_fw_bra_descr *)bra_option->data; 746 747 sc->sc_memaddr_frame_start = 748 letoh32(descr->memaddr_space_start); 749 sc->sc_memaddr_frame_end = 750 letoh32(descr->memaddr_space_end); 751 752 DPRINTF(2, "%s: memory address space start=0x%08x\n", 753 sc->sc_dev.dv_xname, sc->sc_memaddr_frame_start); 754 DPRINTF(2, "%s: memory address space end=0x%08x\n", 755 sc->sc_dev.dv_xname, sc->sc_memaddr_frame_end); 756 break; 757 case UPGT_BRA_TYPE_END: 758 DPRINTF(1, "%s: UPGT_BRA_TYPE_END len=%d\n", 759 sc->sc_dev.dv_xname, bra_option_len); 760 bra_end = 1; 761 break; 762 default: 763 DPRINTF(1, "%s: unknown BRA option len=%d\n", 764 sc->sc_dev.dv_xname, bra_option_len); 765 return (EIO); 766 } 767 768 /* jump to next BRA option */ 769 offset += sizeof(struct upgt_fw_bra_option) + bra_option_len; 770 } 771 772 DPRINTF(1, "%s: firmware verified\n", sc->sc_dev.dv_xname); 773 774 return (0); 775 } 776 777 int 778 upgt_fw_load(struct upgt_softc *sc) 779 { 780 struct upgt_data *data_cmd = &sc->cmd_data; 781 struct upgt_data *data_rx = &sc->rx_data; 782 char start_fwload_cmd[] = { 0x3c, 0x0d }; 783 int offset, bsize, n, i, len; 784 uint32_t crc32; 785 786 /* send firmware start load command */ 787 len = sizeof(start_fwload_cmd); 788 bcopy(start_fwload_cmd, data_cmd->buf, len); 789 if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) { 790 printf("%s: could not send start_firmware_load command!\n", 791 sc->sc_dev.dv_xname); 792 return (EIO); 793 } 794 795 /* send X2 header */ 796 len = sizeof(struct upgt_fw_x2_header); 797 struct upgt_fw_x2_header *x2 = data_cmd->buf; 798 bcopy(UPGT_X2_SIGNATURE, x2->signature, UPGT_X2_SIGNATURE_SIZE); 799 x2->startaddr = htole32(UPGT_MEMADDR_FIRMWARE_START); 800 x2->len = htole32(sc->sc_fw_size); 801 x2->crc = upgt_crc32_le(data_cmd->buf + UPGT_X2_SIGNATURE_SIZE, 802 sizeof(struct upgt_fw_x2_header) - UPGT_X2_SIGNATURE_SIZE - 803 sizeof(uint32_t)); 804 if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) { 805 printf("%s: could not send firmware X2 header!\n", 806 sc->sc_dev.dv_xname); 807 return (EIO); 808 } 809 810 /* download firmware */ 811 for (offset = 0; offset < sc->sc_fw_size; offset += bsize) { 812 if (sc->sc_fw_size - offset > UPGT_FW_BLOCK_SIZE) 813 bsize = UPGT_FW_BLOCK_SIZE; 814 else 815 bsize = sc->sc_fw_size - offset; 816 817 n = upgt_fw_copy(sc->sc_fw + offset, data_cmd->buf, bsize); 818 819 DPRINTF(1, "%s: FW offset=%d, read=%d, sent=%d\n", 820 sc->sc_dev.dv_xname, offset, n, bsize); 821 822 if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &bsize, 0) 823 != 0) { 824 printf("%s: error while downloading firmware block!\n", 825 sc->sc_dev.dv_xname); 826 return (EIO); 827 } 828 829 bsize = n; 830 } 831 DPRINTF(1, "%s: firmware downloaded\n", sc->sc_dev.dv_xname); 832 833 /* load firmware */ 834 crc32 = upgt_crc32_le(sc->sc_fw, sc->sc_fw_size); 835 *((uint32_t *)(data_cmd->buf) ) = crc32; 836 *((uint8_t *)(data_cmd->buf) + 4) = 'g'; 837 *((uint8_t *)(data_cmd->buf) + 5) = '\r'; 838 len = 6; 839 if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) { 840 printf("%s: could not send load_firmware command!\n", 841 sc->sc_dev.dv_xname); 842 return (EIO); 843 } 844 845 for (i = 0; i < UPGT_FIRMWARE_TIMEOUT; i++) { 846 len = UPGT_FW_BLOCK_SIZE; 847 bzero(data_rx->buf, MCLBYTES); 848 if (upgt_bulk_xmit(sc, data_rx, sc->sc_rx_pipeh, &len, 849 USBD_SHORT_XFER_OK) != 0) { 850 printf("%s: could not read firmware response!\n", 851 sc->sc_dev.dv_xname); 852 return (EIO); 853 } 854 855 if (memcmp(data_rx->buf, "OK", 2) == 0) 856 break; /* firmware load was successful */ 857 } 858 if (i == UPGT_FIRMWARE_TIMEOUT) { 859 printf("%s: firmware load failed!\n", sc->sc_dev.dv_xname); 860 return (EIO); 861 } 862 DPRINTF(1, "%s: firmware loaded\n", sc->sc_dev.dv_xname); 863 864 return (0); 865 } 866 867 /* 868 * While copying the version 2 firmware, we need to replace two characters: 869 * 870 * 0x7e -> 0x7d 0x5e 871 * 0x7d -> 0x7d 0x5d 872 */ 873 int 874 upgt_fw_copy(char *src, char *dst, int size) 875 { 876 int i, j; 877 878 for (i = 0, j = 0; i < size && j < size; i++) { 879 switch (src[i]) { 880 case 0x7e: 881 dst[j] = 0x7d; 882 j++; 883 dst[j] = 0x5e; 884 j++; 885 break; 886 case 0x7d: 887 dst[j] = 0x7d; 888 j++; 889 dst[j] = 0x5d; 890 j++; 891 break; 892 default: 893 dst[j] = src[i]; 894 j++; 895 break; 896 } 897 } 898 899 return (i); 900 } 901 902 int 903 upgt_eeprom_read(struct upgt_softc *sc) 904 { 905 struct upgt_data *data_cmd = &sc->cmd_data; 906 struct upgt_lmac_mem *mem; 907 struct upgt_lmac_eeprom *eeprom; 908 int offset, block, len; 909 910 offset = 0; 911 block = UPGT_EEPROM_BLOCK_SIZE; 912 while (offset < UPGT_EEPROM_SIZE) { 913 DPRINTF(1, "%s: request EEPROM block (offset=%d, len=%d)\n", 914 sc->sc_dev.dv_xname, offset, block); 915 916 /* 917 * Transmit the URB containing the CMD data. 918 */ 919 bzero(data_cmd->buf, MCLBYTES); 920 921 mem = (struct upgt_lmac_mem *)data_cmd->buf; 922 mem->addr = htole32(sc->sc_memaddr_frame_start + 923 UPGT_MEMSIZE_FRAME_HEAD); 924 925 eeprom = (struct upgt_lmac_eeprom *)(mem + 1); 926 eeprom->header1.flags = 0; 927 eeprom->header1.type = UPGT_H1_TYPE_CTRL; 928 eeprom->header1.len = htole16(( 929 sizeof(struct upgt_lmac_eeprom) - 930 sizeof(struct upgt_lmac_header)) + block); 931 932 eeprom->header2.reqid = htole32(sc->sc_memaddr_frame_start); 933 eeprom->header2.type = htole16(UPGT_H2_TYPE_EEPROM); 934 eeprom->header2.flags = 0; 935 936 eeprom->offset = htole16(offset); 937 eeprom->len = htole16(block); 938 939 len = sizeof(*mem) + sizeof(*eeprom) + block; 940 941 mem->chksum = upgt_chksum_le((uint32_t *)eeprom, 942 len - sizeof(*mem)); 943 944 if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 945 USBD_FORCE_SHORT_XFER) != 0) { 946 printf("%s: could not transmit EEPROM data URB!\n", 947 sc->sc_dev.dv_xname); 948 return (EIO); 949 } 950 if (tsleep(sc, 0, "eeprom_request", UPGT_USB_TIMEOUT)) { 951 printf("%s: timeout while waiting for EEPROM data!\n", 952 sc->sc_dev.dv_xname); 953 return (EIO); 954 } 955 956 offset += block; 957 if (UPGT_EEPROM_SIZE - offset < block) 958 block = UPGT_EEPROM_SIZE - offset; 959 } 960 961 return (0); 962 } 963 964 int 965 upgt_eeprom_parse(struct upgt_softc *sc) 966 { 967 struct ieee80211com *ic = &sc->sc_ic; 968 struct upgt_eeprom_header *eeprom_header; 969 struct upgt_eeprom_option *eeprom_option; 970 uint16_t option_len; 971 uint16_t option_type; 972 uint16_t preamble_len; 973 int option_end = 0; 974 975 /* calculate eeprom options start offset */ 976 eeprom_header = (struct upgt_eeprom_header *)sc->sc_eeprom; 977 preamble_len = letoh16(eeprom_header->preamble_len); 978 eeprom_option = (struct upgt_eeprom_option *)(sc->sc_eeprom + 979 (sizeof(struct upgt_eeprom_header) + preamble_len)); 980 981 while (!option_end) { 982 /* the eeprom option length is stored in words */ 983 option_len = 984 (letoh16(eeprom_option->len) - 1) * sizeof(uint16_t); 985 option_type = 986 letoh16(eeprom_option->type); 987 988 switch (option_type) { 989 case UPGT_EEPROM_TYPE_NAME: 990 DPRINTF(1, "%s: EEPROM name len=%d\n", 991 sc->sc_dev.dv_xname, option_len); 992 break; 993 case UPGT_EEPROM_TYPE_SERIAL: 994 DPRINTF(1, "%s: EEPROM serial len=%d\n", 995 sc->sc_dev.dv_xname, option_len); 996 break; 997 case UPGT_EEPROM_TYPE_MAC: 998 DPRINTF(1, "%s: EEPROM mac len=%d\n", 999 sc->sc_dev.dv_xname, option_len); 1000 1001 IEEE80211_ADDR_COPY(ic->ic_myaddr, eeprom_option->data); 1002 break; 1003 case UPGT_EEPROM_TYPE_HWRX: 1004 DPRINTF(1, "%s: EEPROM hwrx len=%d\n", 1005 sc->sc_dev.dv_xname, option_len); 1006 1007 upgt_eeprom_parse_hwrx(sc, eeprom_option->data); 1008 break; 1009 case UPGT_EEPROM_TYPE_CHIP: 1010 DPRINTF(1, "%s: EEPROM chip len=%d\n", 1011 sc->sc_dev.dv_xname, option_len); 1012 break; 1013 case UPGT_EEPROM_TYPE_FREQ3: 1014 DPRINTF(1, "%s: EEPROM freq3 len=%d\n", 1015 sc->sc_dev.dv_xname, option_len); 1016 1017 upgt_eeprom_parse_freq3(sc, eeprom_option->data, 1018 option_len); 1019 break; 1020 case UPGT_EEPROM_TYPE_FREQ4: 1021 DPRINTF(1, "%s: EEPROM freq4 len=%d\n", 1022 sc->sc_dev.dv_xname, option_len); 1023 1024 upgt_eeprom_parse_freq4(sc, eeprom_option->data, 1025 option_len); 1026 break; 1027 case UPGT_EEPROM_TYPE_FREQ5: 1028 DPRINTF(1, "%s: EEPROM freq5 len=%d\n", 1029 sc->sc_dev.dv_xname, option_len); 1030 break; 1031 case UPGT_EEPROM_TYPE_FREQ6: 1032 DPRINTF(1, "%s: EEPROM freq6 len=%d\n", 1033 sc->sc_dev.dv_xname, option_len); 1034 1035 upgt_eeprom_parse_freq6(sc, eeprom_option->data, 1036 option_len); 1037 break; 1038 case UPGT_EEPROM_TYPE_END: 1039 DPRINTF(1, "%s: EEPROM end len=%d\n", 1040 sc->sc_dev.dv_xname, option_len); 1041 option_end = 1; 1042 break; 1043 case UPGT_EEPROM_TYPE_OFF: 1044 DPRINTF(1, "%s: EEPROM off without end option!\n", 1045 sc->sc_dev.dv_xname); 1046 return (EIO); 1047 default: 1048 DPRINTF(1, "%s: EEPROM unknown type 0x%04x len=%d\n", 1049 sc->sc_dev.dv_xname, option_type, option_len); 1050 break; 1051 } 1052 1053 /* jump to next EEPROM option */ 1054 eeprom_option = (struct upgt_eeprom_option *) 1055 (eeprom_option->data + option_len); 1056 } 1057 1058 return (0); 1059 } 1060 1061 void 1062 upgt_eeprom_parse_hwrx(struct upgt_softc *sc, uint8_t *data) 1063 { 1064 struct upgt_eeprom_option_hwrx *option_hwrx; 1065 1066 option_hwrx = (struct upgt_eeprom_option_hwrx *)data; 1067 1068 sc->sc_eeprom_hwrx = option_hwrx->rxfilter - UPGT_EEPROM_RX_CONST; 1069 1070 DPRINTF(2, "%s: hwrx option value=0x%04x\n", 1071 sc->sc_dev.dv_xname, sc->sc_eeprom_hwrx); 1072 } 1073 1074 void 1075 upgt_eeprom_parse_freq3(struct upgt_softc *sc, uint8_t *data, int len) 1076 { 1077 struct upgt_eeprom_freq3_header *freq3_header; 1078 struct upgt_lmac_freq3 *freq3; 1079 int i, elements, flags; 1080 unsigned channel; 1081 1082 freq3_header = (struct upgt_eeprom_freq3_header *)data; 1083 freq3 = (struct upgt_lmac_freq3 *)(freq3_header + 1); 1084 1085 flags = freq3_header->flags; 1086 elements = freq3_header->elements; 1087 1088 DPRINTF(2, "%s: flags=0x%02x\n", sc->sc_dev.dv_xname, flags); 1089 DPRINTF(2, "%s: elements=%d\n", sc->sc_dev.dv_xname, elements); 1090 1091 for (i = 0; i < elements; i++) { 1092 channel = ieee80211_mhz2ieee(letoh16(freq3[i].freq), 0); 1093 1094 sc->sc_eeprom_freq3[channel] = freq3[i]; 1095 1096 DPRINTF(2, "%s: frequence=%d, channel=%d\n", 1097 sc->sc_dev.dv_xname, 1098 letoh16(sc->sc_eeprom_freq3[channel].freq), channel); 1099 } 1100 } 1101 1102 void 1103 upgt_eeprom_parse_freq4(struct upgt_softc *sc, uint8_t *data, int len) 1104 { 1105 struct upgt_eeprom_freq4_header *freq4_header; 1106 struct upgt_eeprom_freq4_1 *freq4_1; 1107 struct upgt_eeprom_freq4_2 *freq4_2; 1108 int i, j, elements, settings, flags; 1109 unsigned channel; 1110 1111 freq4_header = (struct upgt_eeprom_freq4_header *)data; 1112 freq4_1 = (struct upgt_eeprom_freq4_1 *)(freq4_header + 1); 1113 1114 flags = freq4_header->flags; 1115 elements = freq4_header->elements; 1116 settings = freq4_header->settings; 1117 1118 /* we need this value later */ 1119 sc->sc_eeprom_freq6_settings = freq4_header->settings; 1120 1121 DPRINTF(2, "%s: flags=0x%02x\n", sc->sc_dev.dv_xname, flags); 1122 DPRINTF(2, "%s: elements=%d\n", sc->sc_dev.dv_xname, elements); 1123 DPRINTF(2, "%s: settings=%d\n", sc->sc_dev.dv_xname, settings); 1124 1125 for (i = 0; i < elements; i++) { 1126 channel = ieee80211_mhz2ieee(letoh16(freq4_1[i].freq), 0); 1127 1128 freq4_2 = (struct upgt_eeprom_freq4_2 *)freq4_1[i].data; 1129 1130 for (j = 0; j < settings; j++) { 1131 sc->sc_eeprom_freq4[channel][j].cmd = freq4_2[j]; 1132 sc->sc_eeprom_freq4[channel][j].pad = 0; 1133 } 1134 1135 DPRINTF(2, "%s: frequence=%d, channel=%d\n", 1136 sc->sc_dev.dv_xname, 1137 letoh16(freq4_1[i].freq), channel); 1138 } 1139 } 1140 1141 void 1142 upgt_eeprom_parse_freq6(struct upgt_softc *sc, uint8_t *data, int len) 1143 { 1144 struct upgt_lmac_freq6 *freq6; 1145 int i, elements; 1146 unsigned channel; 1147 1148 freq6 = (struct upgt_lmac_freq6 *)data; 1149 1150 elements = len / sizeof(struct upgt_lmac_freq6); 1151 1152 DPRINTF(2, "%s: elements=%d\n", sc->sc_dev.dv_xname, elements); 1153 1154 for (i = 0; i < elements; i++) { 1155 channel = ieee80211_mhz2ieee(letoh16(freq6[i].freq), 0); 1156 1157 sc->sc_eeprom_freq6[channel] = freq6[i]; 1158 1159 DPRINTF(2, "%s: frequence=%d, channel=%d\n", 1160 sc->sc_dev.dv_xname, 1161 letoh16(sc->sc_eeprom_freq6[channel].freq), channel); 1162 } 1163 } 1164 1165 int 1166 upgt_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1167 { 1168 struct upgt_softc *sc = ifp->if_softc; 1169 struct ieee80211com *ic = &sc->sc_ic; 1170 struct ifaddr *ifa; 1171 struct ifreq *ifr; 1172 int s, error = 0; 1173 uint8_t chan; 1174 1175 s = splnet(); 1176 1177 switch (cmd) { 1178 case SIOCSIFADDR: 1179 ifa = (struct ifaddr *)data; 1180 ifp->if_flags |= IFF_UP; 1181 #ifdef INET 1182 if (ifa->ifa_addr->sa_family == AF_INET) 1183 arp_ifinit(&ic->ic_ac, ifa); 1184 #endif 1185 /* FALLTHROUGH */ 1186 case SIOCSIFFLAGS: 1187 if (ifp->if_flags & IFF_UP) { 1188 if ((ifp->if_flags & IFF_RUNNING) == 0) 1189 upgt_init(ifp); 1190 } else { 1191 if (ifp->if_flags & IFF_RUNNING) 1192 upgt_stop(sc); 1193 } 1194 break; 1195 case SIOCADDMULTI: 1196 case SIOCDELMULTI: 1197 ifr = (struct ifreq *)data; 1198 error = (cmd == SIOCADDMULTI) ? 1199 ether_addmulti(ifr, &ic->ic_ac) : 1200 ether_delmulti(ifr, &ic->ic_ac); 1201 if (error == ENETRESET) 1202 error = 0; 1203 break; 1204 case SIOCS80211CHANNEL: 1205 /* allow fast channel switching in monitor mode */ 1206 error = ieee80211_ioctl(ifp, cmd, data); 1207 if (error == ENETRESET && 1208 ic->ic_opmode == IEEE80211_M_MONITOR) { 1209 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 1210 (IFF_UP | IFF_RUNNING)) { 1211 ic->ic_bss->ni_chan = ic->ic_ibss_chan; 1212 chan = ieee80211_chan2ieee(ic, 1213 ic->ic_bss->ni_chan); 1214 upgt_set_channel(sc, chan); 1215 } 1216 error = 0; 1217 } 1218 break; 1219 default: 1220 error = ieee80211_ioctl(ifp, cmd, data); 1221 break; 1222 } 1223 1224 if (error == ENETRESET) { 1225 if (ifp->if_flags & (IFF_UP | IFF_RUNNING)) 1226 upgt_init(ifp); 1227 error = 0; 1228 } 1229 1230 splx(s); 1231 1232 return (error); 1233 } 1234 1235 int 1236 upgt_init(struct ifnet *ifp) 1237 { 1238 struct upgt_softc *sc = ifp->if_softc; 1239 struct ieee80211com *ic = &sc->sc_ic; 1240 1241 DPRINTF(1, "%s: %s\n", sc->sc_dev.dv_xname, __func__); 1242 1243 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl)); 1244 1245 /* select default channel */ 1246 ic->ic_bss->ni_chan = ic->ic_ibss_chan; 1247 sc->sc_cur_chan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan); 1248 1249 /* setup device rates */ 1250 upgt_setup_rates(sc); 1251 1252 ifp->if_flags |= IFF_RUNNING; 1253 ifp->if_flags &= ~IFF_OACTIVE; 1254 1255 upgt_set_macfilter(sc, IEEE80211_S_SCAN); 1256 1257 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 1258 upgt_set_channel(sc, sc->sc_cur_chan); 1259 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1260 } else 1261 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 1262 1263 return (0); 1264 } 1265 1266 void 1267 upgt_stop(struct upgt_softc *sc) 1268 { 1269 struct ieee80211com *ic = &sc->sc_ic; 1270 struct ifnet *ifp = &ic->ic_if; 1271 1272 DPRINTF(1, "%s: %s\n", sc->sc_dev.dv_xname, __func__); 1273 1274 /* device down */ 1275 ifp->if_timer = 0; 1276 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1277 1278 upgt_set_led(sc, UPGT_LED_OFF); 1279 1280 /* change device back to initial state */ 1281 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 1282 } 1283 1284 int 1285 upgt_media_change(struct ifnet *ifp) 1286 { 1287 struct upgt_softc *sc = ifp->if_softc; 1288 int error; 1289 1290 DPRINTF(1, "%s: %s\n", sc->sc_dev.dv_xname, __func__); 1291 1292 if ((error = ieee80211_media_change(ifp) != ENETRESET)) 1293 return (error); 1294 1295 if (ifp->if_flags & (IFF_UP | IFF_RUNNING)) { 1296 /* give pending USB transfers a chance to finish */ 1297 usbd_delay_ms(sc->sc_udev, 100); 1298 upgt_init(ifp); 1299 } 1300 1301 return (0); 1302 } 1303 1304 void 1305 upgt_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew) 1306 { 1307 ni->ni_txrate = 0; 1308 } 1309 1310 int 1311 upgt_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 1312 { 1313 struct upgt_softc *sc = ic->ic_if.if_softc; 1314 1315 usb_rem_task(sc->sc_udev, &sc->sc_task_newstate); 1316 timeout_del(&sc->scan_to); 1317 1318 /* do it in a process context */ 1319 sc->sc_state = nstate; 1320 sc->sc_arg = arg; 1321 usb_add_task(sc->sc_udev, &sc->sc_task_newstate); 1322 1323 return (0); 1324 } 1325 1326 void 1327 upgt_newstate_task(void *arg) 1328 { 1329 struct upgt_softc *sc = arg; 1330 struct ieee80211com *ic = &sc->sc_ic; 1331 struct ieee80211_node *ni; 1332 unsigned channel; 1333 1334 switch (sc->sc_state) { 1335 case IEEE80211_S_INIT: 1336 DPRINTF(1, "%s: newstate is IEEE80211_S_INIT\n", 1337 sc->sc_dev.dv_xname); 1338 1339 /* do not accept any frames if the device is down */ 1340 upgt_set_macfilter(sc, IEEE80211_S_INIT); 1341 upgt_set_led(sc, UPGT_LED_OFF); 1342 break; 1343 case IEEE80211_S_SCAN: 1344 DPRINTF(1, "%s: newstate is IEEE80211_S_SCAN\n", 1345 sc->sc_dev.dv_xname); 1346 1347 channel = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan); 1348 upgt_set_channel(sc, channel); 1349 timeout_add_msec(&sc->scan_to, 200); 1350 break; 1351 case IEEE80211_S_AUTH: 1352 DPRINTF(1, "%s: newstate is IEEE80211_S_AUTH\n", 1353 sc->sc_dev.dv_xname); 1354 1355 channel = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan); 1356 upgt_set_channel(sc, channel); 1357 break; 1358 case IEEE80211_S_ASSOC: 1359 DPRINTF(1, "%s: newstate is IEEE80211_S_ASSOC\n", 1360 sc->sc_dev.dv_xname); 1361 break; 1362 case IEEE80211_S_RUN: 1363 DPRINTF(1, "%s: newstate is IEEE80211_S_RUN\n", 1364 sc->sc_dev.dv_xname); 1365 1366 ni = ic->ic_bss; 1367 1368 /* 1369 * TX rate control is done by the firmware. 1370 * Report the maximum rate which is available therefore. 1371 */ 1372 ni->ni_txrate = ni->ni_rates.rs_nrates - 1; 1373 1374 if (ic->ic_opmode != IEEE80211_M_MONITOR) 1375 upgt_set_macfilter(sc, IEEE80211_S_RUN); 1376 upgt_set_led(sc, UPGT_LED_ON); 1377 break; 1378 } 1379 1380 sc->sc_newstate(ic, sc->sc_state, sc->sc_arg); 1381 } 1382 1383 void 1384 upgt_next_scan(void *arg) 1385 { 1386 struct upgt_softc *sc = arg; 1387 struct ieee80211com *ic = &sc->sc_ic; 1388 struct ifnet *ifp = &ic->ic_if; 1389 1390 DPRINTF(2, "%s: %s\n", sc->sc_dev.dv_xname, __func__); 1391 1392 if (ic->ic_state == IEEE80211_S_SCAN) 1393 ieee80211_next_scan(ifp); 1394 } 1395 1396 void 1397 upgt_start(struct ifnet *ifp) 1398 { 1399 struct upgt_softc *sc = ifp->if_softc; 1400 struct ieee80211com *ic = &sc->sc_ic; 1401 struct ieee80211_node *ni; 1402 struct mbuf *m; 1403 int i; 1404 1405 /* don't transmit packets if interface is busy or down */ 1406 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 1407 return; 1408 1409 DPRINTF(2, "%s: %s\n", sc->sc_dev.dv_xname, __func__); 1410 1411 for (i = 0; i < UPGT_TX_COUNT; i++) { 1412 struct upgt_data *data_tx = &sc->tx_data[i]; 1413 1414 IF_POLL(&ic->ic_mgtq, m); 1415 if (m != NULL) { 1416 /* management frame */ 1417 IF_DEQUEUE(&ic->ic_mgtq, m); 1418 1419 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 1420 m->m_pkthdr.rcvif = NULL; 1421 #if NBPFILTER > 0 1422 if (ic->ic_rawbpf != NULL) 1423 bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT); 1424 #endif 1425 if ((data_tx->addr = upgt_mem_alloc(sc)) == 0) { 1426 printf("%s: no free prism memory!\n", 1427 sc->sc_dev.dv_xname); 1428 return; 1429 } 1430 data_tx->ni = ni; 1431 data_tx->m = m; 1432 sc->tx_queued++; 1433 } else { 1434 /* data frame */ 1435 if (ic->ic_state != IEEE80211_S_RUN) 1436 break; 1437 1438 IFQ_POLL(&ifp->if_snd, m); 1439 if (m == NULL) 1440 break; 1441 1442 IFQ_DEQUEUE(&ifp->if_snd, m); 1443 #if NBPFILTER > 0 1444 if (ifp->if_bpf != NULL) 1445 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT); 1446 #endif 1447 m = ieee80211_encap(ifp, m, &ni); 1448 if (m == NULL) 1449 continue; 1450 #if NBPFILTER > 0 1451 if (ic->ic_rawbpf != NULL) 1452 bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT); 1453 #endif 1454 if ((data_tx->addr = upgt_mem_alloc(sc)) == 0) { 1455 printf("%s: no free prism memory!\n", 1456 sc->sc_dev.dv_xname); 1457 return; 1458 } 1459 data_tx->ni = ni; 1460 data_tx->m = m; 1461 sc->tx_queued++; 1462 } 1463 } 1464 1465 if (sc->tx_queued > 0) { 1466 DPRINTF(2, "%s: tx_queued=%d\n", 1467 sc->sc_dev.dv_xname, sc->tx_queued); 1468 /* process the TX queue in process context */ 1469 ifp->if_timer = 5; 1470 ifp->if_flags |= IFF_OACTIVE; 1471 usb_rem_task(sc->sc_udev, &sc->sc_task_tx); 1472 usb_add_task(sc->sc_udev, &sc->sc_task_tx); 1473 } 1474 } 1475 1476 void 1477 upgt_watchdog(struct ifnet *ifp) 1478 { 1479 struct upgt_softc *sc = ifp->if_softc; 1480 struct ieee80211com *ic = &sc->sc_ic; 1481 1482 if (ic->ic_state == IEEE80211_S_INIT) 1483 return; 1484 1485 printf("%s: watchdog timeout!\n", sc->sc_dev.dv_xname); 1486 1487 /* TODO: what shall we do on TX timeout? */ 1488 1489 ieee80211_watchdog(ifp); 1490 } 1491 1492 void 1493 upgt_tx_task(void *arg) 1494 { 1495 struct upgt_softc *sc = arg; 1496 struct ieee80211com *ic = &sc->sc_ic; 1497 struct ieee80211_frame *wh; 1498 struct ieee80211_key *k; 1499 struct upgt_lmac_mem *mem; 1500 struct upgt_lmac_tx_desc *txdesc; 1501 struct mbuf *m; 1502 uint32_t addr; 1503 int len, i, s; 1504 usbd_status error; 1505 1506 s = splusb(); 1507 1508 upgt_set_led(sc, UPGT_LED_BLINK); 1509 1510 for (i = 0; i < UPGT_TX_COUNT; i++) { 1511 struct upgt_data *data_tx = &sc->tx_data[i]; 1512 1513 if (data_tx->m == NULL) { 1514 DPRINTF(2, "%s: %d: m is NULL\n", 1515 sc->sc_dev.dv_xname, i); 1516 continue; 1517 } 1518 1519 m = data_tx->m; 1520 addr = data_tx->addr + UPGT_MEMSIZE_FRAME_HEAD; 1521 1522 /* 1523 * Software crypto. 1524 */ 1525 wh = mtod(m, struct ieee80211_frame *); 1526 1527 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1528 k = ieee80211_get_txkey(ic, wh, ic->ic_bss); 1529 1530 if ((m = ieee80211_encrypt(ic, m, k)) == NULL) 1531 return; 1532 1533 /* in case packet header moved, reset pointer */ 1534 wh = mtod(m, struct ieee80211_frame *); 1535 } 1536 1537 /* 1538 * Transmit the URB containing the TX data. 1539 */ 1540 bzero(data_tx->buf, MCLBYTES); 1541 1542 mem = (struct upgt_lmac_mem *)data_tx->buf; 1543 mem->addr = htole32(addr); 1544 1545 txdesc = (struct upgt_lmac_tx_desc *)(mem + 1); 1546 1547 /* XXX differ between data and mgmt frames? */ 1548 txdesc->header1.flags = UPGT_H1_FLAGS_TX_DATA; 1549 txdesc->header1.type = UPGT_H1_TYPE_TX_DATA; 1550 txdesc->header1.len = htole16(m->m_pkthdr.len); 1551 1552 txdesc->header2.reqid = htole32(data_tx->addr); 1553 txdesc->header2.type = htole16(UPGT_H2_TYPE_TX_ACK_YES); 1554 txdesc->header2.flags = htole16(UPGT_H2_FLAGS_TX_ACK_YES); 1555 1556 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 1557 IEEE80211_FC0_TYPE_MGT) { 1558 /* always send mgmt frames at lowest rate (DS1) */ 1559 memset(txdesc->rates, 0x10, sizeof(txdesc->rates)); 1560 } else { 1561 bcopy(sc->sc_cur_rateset, txdesc->rates, 1562 sizeof(txdesc->rates)); 1563 } 1564 txdesc->type = htole32(UPGT_TX_DESC_TYPE_DATA); 1565 txdesc->pad3[0] = UPGT_TX_DESC_PAD3_SIZE; 1566 1567 #if NBPFILTER > 0 1568 if (sc->sc_drvbpf != NULL) { 1569 struct mbuf mb; 1570 struct upgt_tx_radiotap_header *tap = &sc->sc_txtap; 1571 1572 tap->wt_flags = 0; 1573 tap->wt_rate = 0; /* TODO: where to get from? */ 1574 tap->wt_chan_freq = 1575 htole16(ic->ic_bss->ni_chan->ic_freq); 1576 tap->wt_chan_flags = 1577 htole16(ic->ic_bss->ni_chan->ic_flags); 1578 1579 mb.m_data = (caddr_t)tap; 1580 mb.m_len = sc->sc_txtap_len; 1581 mb.m_next = m; 1582 mb.m_nextpkt = NULL; 1583 mb.m_type = 0; 1584 mb.m_flags = 0; 1585 bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT); 1586 } 1587 #endif 1588 /* copy frame below our TX descriptor header */ 1589 m_copydata(m, 0, m->m_pkthdr.len, 1590 data_tx->buf + (sizeof(*mem) + sizeof(*txdesc))); 1591 1592 /* calculate frame size */ 1593 len = sizeof(*mem) + sizeof(*txdesc) + m->m_pkthdr.len; 1594 1595 /* we need to align the frame to a 4 byte boundary */ 1596 len = (len + 3) & ~3; 1597 1598 /* calculate frame checksum */ 1599 mem->chksum = upgt_chksum_le((uint32_t *)txdesc, 1600 len - sizeof(*mem)); 1601 1602 /* we do not need the mbuf anymore */ 1603 m_freem(m); 1604 data_tx->m = NULL; 1605 1606 DPRINTF(2, "%s: TX start data sending\n", sc->sc_dev.dv_xname); 1607 1608 usbd_setup_xfer(data_tx->xfer, sc->sc_tx_pipeh, data_tx, 1609 data_tx->buf, len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 1610 UPGT_USB_TIMEOUT, NULL); 1611 error = usbd_transfer(data_tx->xfer); 1612 if (error != 0 && error != USBD_IN_PROGRESS) { 1613 printf("%s: could not transmit TX data URB!\n", 1614 sc->sc_dev.dv_xname); 1615 return; 1616 } 1617 1618 DPRINTF(2, "%s: TX sent (%d bytes)\n", 1619 sc->sc_dev.dv_xname, len); 1620 } 1621 1622 /* 1623 * If we don't regulary read the device statistics, the RX queue 1624 * will stall. It's strange, but it works, so we keep reading 1625 * the statistics here. *shrug* 1626 */ 1627 upgt_get_stats(sc); 1628 1629 splx(s); 1630 } 1631 1632 void 1633 upgt_tx_done(struct upgt_softc *sc, uint8_t *data) 1634 { 1635 struct ieee80211com *ic = &sc->sc_ic; 1636 struct ifnet *ifp = &ic->ic_if; 1637 struct upgt_lmac_tx_done_desc *desc; 1638 int i, s; 1639 1640 s = splnet(); 1641 1642 desc = (struct upgt_lmac_tx_done_desc *)data; 1643 1644 for (i = 0; i < UPGT_TX_COUNT; i++) { 1645 struct upgt_data *data_tx = &sc->tx_data[i]; 1646 1647 if (data_tx->addr == letoh32(desc->header2.reqid)) { 1648 upgt_mem_free(sc, data_tx->addr); 1649 ieee80211_release_node(ic, data_tx->ni); 1650 data_tx->ni = NULL; 1651 data_tx->addr = 0; 1652 1653 sc->tx_queued--; 1654 ifp->if_opackets++; 1655 1656 DPRINTF(2, "%s: TX done: ", sc->sc_dev.dv_xname); 1657 DPRINTF(2, "memaddr=0x%08x, status=0x%04x, rssi=%d, ", 1658 letoh32(desc->header2.reqid), 1659 letoh16(desc->status), 1660 letoh16(desc->rssi)); 1661 DPRINTF(2, "seq=%d\n", letoh16(desc->seq)); 1662 break; 1663 } 1664 } 1665 1666 if (sc->tx_queued == 0) { 1667 /* TX queued was processed, continue */ 1668 ifp->if_timer = 0; 1669 ifp->if_flags &= ~IFF_OACTIVE; 1670 upgt_start(ifp); 1671 } 1672 1673 splx(s); 1674 } 1675 1676 void 1677 upgt_rx_cb(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 1678 { 1679 struct upgt_data *data_rx = priv; 1680 struct upgt_softc *sc = data_rx->sc; 1681 int len; 1682 struct upgt_lmac_header *header; 1683 struct upgt_lmac_eeprom *eeprom; 1684 uint8_t h1_type; 1685 uint16_t h2_type; 1686 1687 DPRINTF(3, "%s: %s\n", sc->sc_dev.dv_xname, __func__); 1688 1689 if (status != USBD_NORMAL_COMPLETION) { 1690 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1691 return; 1692 if (status == USBD_STALLED) 1693 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh); 1694 goto skip; 1695 } 1696 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 1697 1698 /* 1699 * Check what type of frame came in. 1700 */ 1701 header = (struct upgt_lmac_header *)(data_rx->buf + 4); 1702 1703 h1_type = header->header1.type; 1704 h2_type = letoh16(header->header2.type); 1705 1706 if (h1_type == UPGT_H1_TYPE_CTRL && 1707 h2_type == UPGT_H2_TYPE_EEPROM) { 1708 eeprom = (struct upgt_lmac_eeprom *)(data_rx->buf + 4); 1709 uint16_t eeprom_offset = letoh16(eeprom->offset); 1710 uint16_t eeprom_len = letoh16(eeprom->len); 1711 1712 DPRINTF(2, "%s: received EEPROM block (offset=%d, len=%d)\n", 1713 sc->sc_dev.dv_xname, eeprom_offset, eeprom_len); 1714 1715 bcopy(data_rx->buf + sizeof(struct upgt_lmac_eeprom) + 4, 1716 sc->sc_eeprom + eeprom_offset, eeprom_len); 1717 1718 /* EEPROM data has arrived in time, wakeup tsleep() */ 1719 wakeup(sc); 1720 } else 1721 if (h1_type == UPGT_H1_TYPE_CTRL && 1722 h2_type == UPGT_H2_TYPE_TX_DONE) { 1723 DPRINTF(2, "%s: received 802.11 TX done\n", 1724 sc->sc_dev.dv_xname); 1725 1726 upgt_tx_done(sc, data_rx->buf + 4); 1727 } else 1728 if (h1_type == UPGT_H1_TYPE_RX_DATA || 1729 h1_type == UPGT_H1_TYPE_RX_DATA_MGMT) { 1730 DPRINTF(3, "%s: received 802.11 RX data\n", 1731 sc->sc_dev.dv_xname); 1732 1733 upgt_rx(sc, data_rx->buf + 4, letoh16(header->header1.len)); 1734 } else 1735 if (h1_type == UPGT_H1_TYPE_CTRL && 1736 h2_type == UPGT_H2_TYPE_STATS) { 1737 DPRINTF(2, "%s: received statistic data\n", 1738 sc->sc_dev.dv_xname); 1739 1740 /* TODO: what could we do with the statistic data? */ 1741 } else { 1742 /* ignore unknown frame types */ 1743 DPRINTF(1, "%s: received unknown frame type 0x%02x\n", 1744 sc->sc_dev.dv_xname, header->header1.type); 1745 } 1746 1747 skip: /* setup new transfer */ 1748 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data_rx, data_rx->buf, MCLBYTES, 1749 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, upgt_rx_cb); 1750 (void)usbd_transfer(xfer); 1751 } 1752 1753 void 1754 upgt_rx(struct upgt_softc *sc, uint8_t *data, int pkglen) 1755 { 1756 struct ieee80211com *ic = &sc->sc_ic; 1757 struct ifnet *ifp = &ic->ic_if; 1758 struct upgt_lmac_rx_desc *rxdesc; 1759 struct ieee80211_frame *wh; 1760 struct ieee80211_rxinfo rxi; 1761 struct ieee80211_node *ni; 1762 struct mbuf *m; 1763 int s; 1764 1765 /* access RX packet descriptor */ 1766 rxdesc = (struct upgt_lmac_rx_desc *)data; 1767 1768 /* create mbuf which is suitable for strict alignment archs */ 1769 m = m_devget(rxdesc->data, pkglen, ETHER_ALIGN, ifp, NULL); 1770 if (m == NULL) { 1771 DPRINTF(1, "%s: could not create RX mbuf!\n", sc->sc_dev.dv_xname); 1772 ifp->if_ierrors++; 1773 return; 1774 } 1775 1776 s = splnet(); 1777 1778 #if NBPFILTER > 0 1779 if (sc->sc_drvbpf != NULL) { 1780 struct mbuf mb; 1781 struct upgt_rx_radiotap_header *tap = &sc->sc_rxtap; 1782 1783 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS; 1784 tap->wr_rate = upgt_rx_rate(sc, rxdesc->rate); 1785 tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq); 1786 tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags); 1787 tap->wr_antsignal = rxdesc->rssi; 1788 1789 mb.m_data = (caddr_t)tap; 1790 mb.m_len = sc->sc_rxtap_len; 1791 mb.m_next = m; 1792 mb.m_nextpkt = NULL; 1793 mb.m_type = 0; 1794 mb.m_flags = 0; 1795 bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN); 1796 } 1797 #endif 1798 /* trim FCS */ 1799 m_adj(m, -IEEE80211_CRC_LEN); 1800 1801 wh = mtod(m, struct ieee80211_frame *); 1802 ni = ieee80211_find_rxnode(ic, wh); 1803 1804 /* push the frame up to the 802.11 stack */ 1805 rxi.rxi_flags = 0; 1806 rxi.rxi_rssi = rxdesc->rssi; 1807 rxi.rxi_tstamp = 0; /* unused */ 1808 ieee80211_input(ifp, m, ni, &rxi); 1809 1810 /* node is no longer needed */ 1811 ieee80211_release_node(ic, ni); 1812 1813 splx(s); 1814 1815 DPRINTF(3, "%s: RX done\n", sc->sc_dev.dv_xname); 1816 } 1817 1818 void 1819 upgt_setup_rates(struct upgt_softc *sc) 1820 { 1821 struct ieee80211com *ic = &sc->sc_ic; 1822 1823 /* 1824 * 0x01 = OFMD6 0x10 = DS1 1825 * 0x04 = OFDM9 0x11 = DS2 1826 * 0x06 = OFDM12 0x12 = DS5 1827 * 0x07 = OFDM18 0x13 = DS11 1828 * 0x08 = OFDM24 1829 * 0x09 = OFDM36 1830 * 0x0a = OFDM48 1831 * 0x0b = OFDM54 1832 */ 1833 const uint8_t rateset_auto_11b[] = 1834 { 0x13, 0x13, 0x12, 0x11, 0x11, 0x10, 0x10, 0x10 }; 1835 const uint8_t rateset_auto_11g[] = 1836 { 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x04, 0x01 }; 1837 const uint8_t rateset_fix_11bg[] = 1838 { 0x10, 0x11, 0x12, 0x13, 0x01, 0x04, 0x06, 0x07, 1839 0x08, 0x09, 0x0a, 0x0b }; 1840 1841 if (ic->ic_fixed_rate == -1) { 1842 /* 1843 * Automatic rate control is done by the device. 1844 * We just pass the rateset from which the device 1845 * will pickup a rate. 1846 */ 1847 if (ic->ic_curmode == IEEE80211_MODE_11B) 1848 bcopy(rateset_auto_11b, sc->sc_cur_rateset, 1849 sizeof(sc->sc_cur_rateset)); 1850 if (ic->ic_curmode == IEEE80211_MODE_11G || 1851 ic->ic_curmode == IEEE80211_MODE_AUTO) 1852 bcopy(rateset_auto_11g, sc->sc_cur_rateset, 1853 sizeof(sc->sc_cur_rateset)); 1854 } else { 1855 /* set a fixed rate */ 1856 memset(sc->sc_cur_rateset, rateset_fix_11bg[ic->ic_fixed_rate], 1857 sizeof(sc->sc_cur_rateset)); 1858 } 1859 } 1860 1861 uint8_t 1862 upgt_rx_rate(struct upgt_softc *sc, const int rate) 1863 { 1864 struct ieee80211com *ic = &sc->sc_ic; 1865 1866 if (ic->ic_curmode == IEEE80211_MODE_11B) { 1867 if (rate < 0 || rate > 3) 1868 /* invalid rate */ 1869 return (0); 1870 1871 switch (rate) { 1872 case 0: 1873 return (2); 1874 case 1: 1875 return (4); 1876 case 2: 1877 return (11); 1878 case 3: 1879 return (22); 1880 default: 1881 return (0); 1882 } 1883 } 1884 1885 if (ic->ic_curmode == IEEE80211_MODE_11G) { 1886 if (rate < 0 || rate > 11) 1887 /* invalid rate */ 1888 return (0); 1889 1890 switch (rate) { 1891 case 0: 1892 return (2); 1893 case 1: 1894 return (4); 1895 case 2: 1896 return (11); 1897 case 3: 1898 return (22); 1899 case 4: 1900 return (12); 1901 case 5: 1902 return (18); 1903 case 6: 1904 return (24); 1905 case 7: 1906 return (36); 1907 case 8: 1908 return (48); 1909 case 9: 1910 return (72); 1911 case 10: 1912 return (96); 1913 case 11: 1914 return (108); 1915 default: 1916 return (0); 1917 } 1918 } 1919 1920 return (0); 1921 } 1922 1923 int 1924 upgt_set_macfilter(struct upgt_softc *sc, uint8_t state) 1925 { 1926 struct ieee80211com *ic = &sc->sc_ic; 1927 struct ieee80211_node *ni = ic->ic_bss; 1928 struct upgt_data *data_cmd = &sc->cmd_data; 1929 struct upgt_lmac_mem *mem; 1930 struct upgt_lmac_filter *filter; 1931 int len; 1932 uint8_t broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 1933 1934 /* 1935 * Transmit the URB containing the CMD data. 1936 */ 1937 bzero(data_cmd->buf, MCLBYTES); 1938 1939 mem = (struct upgt_lmac_mem *)data_cmd->buf; 1940 mem->addr = htole32(sc->sc_memaddr_frame_start + 1941 UPGT_MEMSIZE_FRAME_HEAD); 1942 1943 filter = (struct upgt_lmac_filter *)(mem + 1); 1944 1945 filter->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK; 1946 filter->header1.type = UPGT_H1_TYPE_CTRL; 1947 filter->header1.len = htole16( 1948 sizeof(struct upgt_lmac_filter) - 1949 sizeof(struct upgt_lmac_header)); 1950 1951 filter->header2.reqid = htole32(sc->sc_memaddr_frame_start); 1952 filter->header2.type = htole16(UPGT_H2_TYPE_MACFILTER); 1953 filter->header2.flags = 0; 1954 1955 switch (state) { 1956 case IEEE80211_S_INIT: 1957 DPRINTF(1, "%s: set MAC filter to INIT\n", 1958 sc->sc_dev.dv_xname); 1959 1960 filter->type = htole16(UPGT_FILTER_TYPE_RESET); 1961 break; 1962 case IEEE80211_S_SCAN: 1963 DPRINTF(1, "%s: set MAC filter to SCAN (bssid %s)\n", 1964 sc->sc_dev.dv_xname, ether_sprintf(broadcast)); 1965 1966 filter->type = htole16(UPGT_FILTER_TYPE_NONE); 1967 IEEE80211_ADDR_COPY(filter->dst, ic->ic_myaddr); 1968 IEEE80211_ADDR_COPY(filter->src, broadcast); 1969 filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1); 1970 filter->rxaddr = htole32(sc->sc_memaddr_rx_start); 1971 filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2); 1972 filter->rxhw = htole32(sc->sc_eeprom_hwrx); 1973 filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3); 1974 break; 1975 case IEEE80211_S_RUN: 1976 DPRINTF(1, "%s: set MAC filter to RUN (bssid %s)\n", 1977 sc->sc_dev.dv_xname, ether_sprintf(ni->ni_bssid)); 1978 1979 filter->type = htole16(UPGT_FILTER_TYPE_STA); 1980 IEEE80211_ADDR_COPY(filter->dst, ic->ic_myaddr); 1981 IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid); 1982 filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1); 1983 filter->rxaddr = htole32(sc->sc_memaddr_rx_start); 1984 filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2); 1985 filter->rxhw = htole32(sc->sc_eeprom_hwrx); 1986 filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3); 1987 break; 1988 default: 1989 printf("%s: MAC filter does not know that state!\n", 1990 sc->sc_dev.dv_xname); 1991 break; 1992 } 1993 1994 len = sizeof(*mem) + sizeof(*filter); 1995 1996 mem->chksum = upgt_chksum_le((uint32_t *)filter, 1997 len - sizeof(*mem)); 1998 1999 if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) { 2000 printf("%s: could not transmit macfilter CMD data URB!\n", 2001 sc->sc_dev.dv_xname); 2002 return (EIO); 2003 } 2004 2005 return (0); 2006 } 2007 2008 int 2009 upgt_set_channel(struct upgt_softc *sc, unsigned channel) 2010 { 2011 struct upgt_data *data_cmd = &sc->cmd_data; 2012 struct upgt_lmac_mem *mem; 2013 struct upgt_lmac_channel *chan; 2014 int len; 2015 2016 DPRINTF(1, "%s: %s: %d\n", sc->sc_dev.dv_xname, __func__, channel); 2017 2018 /* 2019 * Transmit the URB containing the CMD data. 2020 */ 2021 bzero(data_cmd->buf, MCLBYTES); 2022 2023 mem = (struct upgt_lmac_mem *)data_cmd->buf; 2024 mem->addr = htole32(sc->sc_memaddr_frame_start + 2025 UPGT_MEMSIZE_FRAME_HEAD); 2026 2027 chan = (struct upgt_lmac_channel *)(mem + 1); 2028 2029 chan->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK; 2030 chan->header1.type = UPGT_H1_TYPE_CTRL; 2031 chan->header1.len = htole16( 2032 sizeof(struct upgt_lmac_channel) - 2033 sizeof(struct upgt_lmac_header)); 2034 2035 chan->header2.reqid = htole32(sc->sc_memaddr_frame_start); 2036 chan->header2.type = htole16(UPGT_H2_TYPE_CHANNEL); 2037 chan->header2.flags = 0; 2038 2039 chan->unknown1 = htole16(UPGT_CHANNEL_UNKNOWN1); 2040 chan->unknown2 = htole16(UPGT_CHANNEL_UNKNOWN2); 2041 chan->freq6 = sc->sc_eeprom_freq6[channel]; 2042 chan->settings = sc->sc_eeprom_freq6_settings; 2043 chan->unknown3 = UPGT_CHANNEL_UNKNOWN3; 2044 2045 bcopy(&sc->sc_eeprom_freq3[channel].data, chan->freq3_1, 2046 sizeof(chan->freq3_1)); 2047 2048 bcopy(&sc->sc_eeprom_freq4[channel], chan->freq4, 2049 sizeof(sc->sc_eeprom_freq4[channel])); 2050 2051 bcopy(&sc->sc_eeprom_freq3[channel].data, chan->freq3_2, 2052 sizeof(chan->freq3_2)); 2053 2054 len = sizeof(*mem) + sizeof(*chan); 2055 2056 mem->chksum = upgt_chksum_le((uint32_t *)chan, 2057 len - sizeof(*mem)); 2058 2059 if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) { 2060 printf("%s: could not transmit channel CMD data URB!\n", 2061 sc->sc_dev.dv_xname); 2062 return (EIO); 2063 } 2064 2065 return (0); 2066 } 2067 2068 void 2069 upgt_set_led(struct upgt_softc *sc, int action) 2070 { 2071 struct ieee80211com *ic = &sc->sc_ic; 2072 struct upgt_data *data_cmd = &sc->cmd_data; 2073 struct upgt_lmac_mem *mem; 2074 struct upgt_lmac_led *led; 2075 struct timeval t; 2076 int len; 2077 2078 /* 2079 * Transmit the URB containing the CMD data. 2080 */ 2081 bzero(data_cmd->buf, MCLBYTES); 2082 2083 mem = (struct upgt_lmac_mem *)data_cmd->buf; 2084 mem->addr = htole32(sc->sc_memaddr_frame_start + 2085 UPGT_MEMSIZE_FRAME_HEAD); 2086 2087 led = (struct upgt_lmac_led *)(mem + 1); 2088 2089 led->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK; 2090 led->header1.type = UPGT_H1_TYPE_CTRL; 2091 led->header1.len = htole16( 2092 sizeof(struct upgt_lmac_led) - 2093 sizeof(struct upgt_lmac_header)); 2094 2095 led->header2.reqid = htole32(sc->sc_memaddr_frame_start); 2096 led->header2.type = htole16(UPGT_H2_TYPE_LED); 2097 led->header2.flags = 0; 2098 2099 switch (action) { 2100 case UPGT_LED_OFF: 2101 led->mode = htole16(UPGT_LED_MODE_SET); 2102 led->action_fix = 0; 2103 led->action_tmp = htole16(UPGT_LED_ACTION_OFF); 2104 led->action_tmp_dur = 0; 2105 break; 2106 case UPGT_LED_ON: 2107 led->mode = htole16(UPGT_LED_MODE_SET); 2108 led->action_fix = 0; 2109 led->action_tmp = htole16(UPGT_LED_ACTION_ON); 2110 led->action_tmp_dur = 0; 2111 break; 2112 case UPGT_LED_BLINK: 2113 if (ic->ic_state != IEEE80211_S_RUN) 2114 return; 2115 if (sc->sc_led_blink) 2116 /* previous blink was not finished */ 2117 return; 2118 led->mode = htole16(UPGT_LED_MODE_SET); 2119 led->action_fix = htole16(UPGT_LED_ACTION_OFF); 2120 led->action_tmp = htole16(UPGT_LED_ACTION_ON); 2121 led->action_tmp_dur = htole16(UPGT_LED_ACTION_TMP_DUR); 2122 /* lock blink */ 2123 sc->sc_led_blink = 1; 2124 t.tv_sec = 0; 2125 t.tv_usec = UPGT_LED_ACTION_TMP_DUR * 1000L; 2126 timeout_add(&sc->led_to, tvtohz(&t)); 2127 break; 2128 default: 2129 return; 2130 } 2131 2132 len = sizeof(*mem) + sizeof(*led); 2133 2134 mem->chksum = upgt_chksum_le((uint32_t *)led, 2135 len - sizeof(*mem)); 2136 2137 if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) { 2138 printf("%s: could not transmit led CMD URB!\n", 2139 sc->sc_dev.dv_xname); 2140 } 2141 } 2142 2143 void 2144 upgt_set_led_blink(void *arg) 2145 { 2146 struct upgt_softc *sc = arg; 2147 2148 /* blink finished, we are ready for a next one */ 2149 sc->sc_led_blink = 0; 2150 timeout_del(&sc->led_to); 2151 } 2152 2153 int 2154 upgt_get_stats(struct upgt_softc *sc) 2155 { 2156 struct upgt_data *data_cmd = &sc->cmd_data; 2157 struct upgt_lmac_mem *mem; 2158 struct upgt_lmac_stats *stats; 2159 int len; 2160 2161 /* 2162 * Transmit the URB containing the CMD data. 2163 */ 2164 bzero(data_cmd->buf, MCLBYTES); 2165 2166 mem = (struct upgt_lmac_mem *)data_cmd->buf; 2167 mem->addr = htole32(sc->sc_memaddr_frame_start + 2168 UPGT_MEMSIZE_FRAME_HEAD); 2169 2170 stats = (struct upgt_lmac_stats *)(mem + 1); 2171 2172 stats->header1.flags = 0; 2173 stats->header1.type = UPGT_H1_TYPE_CTRL; 2174 stats->header1.len = htole16( 2175 sizeof(struct upgt_lmac_stats) - 2176 sizeof(struct upgt_lmac_header)); 2177 2178 stats->header2.reqid = htole32(sc->sc_memaddr_frame_start); 2179 stats->header2.type = htole16(UPGT_H2_TYPE_STATS); 2180 stats->header2.flags = 0; 2181 2182 len = sizeof(*mem) + sizeof(*stats); 2183 2184 mem->chksum = upgt_chksum_le((uint32_t *)stats, 2185 len - sizeof(*mem)); 2186 2187 if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) { 2188 printf("%s: could not transmit statistics CMD data URB!\n", 2189 sc->sc_dev.dv_xname); 2190 return (EIO); 2191 } 2192 2193 return (0); 2194 2195 } 2196 2197 int 2198 upgt_alloc_tx(struct upgt_softc *sc) 2199 { 2200 int i; 2201 2202 sc->tx_queued = 0; 2203 2204 for (i = 0; i < UPGT_TX_COUNT; i++) { 2205 struct upgt_data *data_tx = &sc->tx_data[i]; 2206 2207 data_tx->sc = sc; 2208 2209 data_tx->xfer = usbd_alloc_xfer(sc->sc_udev); 2210 if (data_tx->xfer == NULL) { 2211 printf("%s: could not allocate TX xfer!\n", 2212 sc->sc_dev.dv_xname); 2213 return (ENOMEM); 2214 } 2215 2216 data_tx->buf = usbd_alloc_buffer(data_tx->xfer, MCLBYTES); 2217 if (data_tx->buf == NULL) { 2218 printf("%s: could not allocate TX buffer!\n", 2219 sc->sc_dev.dv_xname); 2220 return (ENOMEM); 2221 } 2222 2223 bzero(data_tx->buf, MCLBYTES); 2224 } 2225 2226 return (0); 2227 } 2228 2229 int 2230 upgt_alloc_rx(struct upgt_softc *sc) 2231 { 2232 struct upgt_data *data_rx = &sc->rx_data; 2233 2234 data_rx->sc = sc; 2235 2236 data_rx->xfer = usbd_alloc_xfer(sc->sc_udev); 2237 if (data_rx->xfer == NULL) { 2238 printf("%s: could not allocate RX xfer!\n", 2239 sc->sc_dev.dv_xname); 2240 return (ENOMEM); 2241 } 2242 2243 data_rx->buf = usbd_alloc_buffer(data_rx->xfer, MCLBYTES); 2244 if (data_rx->buf == NULL) { 2245 printf("%s: could not allocate RX buffer!\n", 2246 sc->sc_dev.dv_xname); 2247 return (ENOMEM); 2248 } 2249 2250 bzero(data_rx->buf, MCLBYTES); 2251 2252 return (0); 2253 } 2254 2255 int 2256 upgt_alloc_cmd(struct upgt_softc *sc) 2257 { 2258 struct upgt_data *data_cmd = &sc->cmd_data; 2259 2260 data_cmd->sc = sc; 2261 2262 data_cmd->xfer = usbd_alloc_xfer(sc->sc_udev); 2263 if (data_cmd->xfer == NULL) { 2264 printf("%s: could not allocate RX xfer!\n", 2265 sc->sc_dev.dv_xname); 2266 return (ENOMEM); 2267 } 2268 2269 data_cmd->buf = usbd_alloc_buffer(data_cmd->xfer, MCLBYTES); 2270 if (data_cmd->buf == NULL) { 2271 printf("%s: could not allocate RX buffer!\n", 2272 sc->sc_dev.dv_xname); 2273 return (ENOMEM); 2274 } 2275 2276 bzero(data_cmd->buf, MCLBYTES); 2277 2278 return (0); 2279 } 2280 2281 void 2282 upgt_free_tx(struct upgt_softc *sc) 2283 { 2284 int i; 2285 2286 for (i = 0; i < UPGT_TX_COUNT; i++) { 2287 struct upgt_data *data_tx = &sc->tx_data[i]; 2288 2289 if (data_tx->xfer != NULL) { 2290 usbd_free_xfer(data_tx->xfer); 2291 data_tx->xfer = NULL; 2292 } 2293 2294 data_tx->ni = NULL; 2295 } 2296 } 2297 2298 void 2299 upgt_free_rx(struct upgt_softc *sc) 2300 { 2301 struct upgt_data *data_rx = &sc->rx_data; 2302 2303 if (data_rx->xfer != NULL) { 2304 usbd_free_xfer(data_rx->xfer); 2305 data_rx->xfer = NULL; 2306 } 2307 2308 data_rx->ni = NULL; 2309 } 2310 2311 void 2312 upgt_free_cmd(struct upgt_softc *sc) 2313 { 2314 struct upgt_data *data_cmd = &sc->cmd_data; 2315 2316 if (data_cmd->xfer != NULL) { 2317 usbd_free_xfer(data_cmd->xfer); 2318 data_cmd->xfer = NULL; 2319 } 2320 } 2321 2322 int 2323 upgt_bulk_xmit(struct upgt_softc *sc, struct upgt_data *data, 2324 usbd_pipe_handle pipeh, uint32_t *size, int flags) 2325 { 2326 usbd_status status; 2327 2328 status = usbd_bulk_transfer(data->xfer, pipeh, 2329 USBD_NO_COPY | flags, UPGT_USB_TIMEOUT, data->buf, size, 2330 "upgt_bulk_xmit"); 2331 if (status != USBD_NORMAL_COMPLETION) { 2332 printf("%s: %s: error %s!\n", 2333 sc->sc_dev.dv_xname, __func__, usbd_errstr(status)); 2334 return (EIO); 2335 } 2336 2337 return (0); 2338 } 2339 2340 void 2341 upgt_hexdump(void *buf, int len) 2342 { 2343 int i; 2344 2345 for (i = 0; i < len; i++) { 2346 if (i % 16 == 0) 2347 printf("%s%5i:", i ? "\n" : "", i); 2348 if (i % 4 == 0) 2349 printf(" "); 2350 printf("%02x", (int)*((u_char *)buf + i)); 2351 } 2352 printf("\n"); 2353 } 2354 2355 uint32_t 2356 upgt_crc32_le(const void *buf, size_t size) 2357 { 2358 uint32_t crc; 2359 2360 crc = ether_crc32_le(buf, size); 2361 2362 /* apply final XOR value as common for CRC-32 */ 2363 crc = htole32(crc ^ 0xffffffffU); 2364 2365 return (crc); 2366 } 2367 2368 /* 2369 * The firmware awaits a checksum for each frame we send to it. 2370 * The algorithm used therefor is uncommon but somehow similar to CRC32. 2371 */ 2372 uint32_t 2373 upgt_chksum_le(const uint32_t *buf, size_t size) 2374 { 2375 int i; 2376 uint32_t crc = 0; 2377 2378 for (i = 0; i < size; i += sizeof(uint32_t)) { 2379 crc = htole32(crc ^ *buf++); 2380 crc = htole32((crc >> 5) ^ (crc << 3)); 2381 } 2382 2383 return (crc); 2384 } 2385