1 /* $OpenBSD: if_uath.c,v 1.10 2006/09/20 19:47:17 damien Exp $ */ 2 3 /*- 4 * Copyright (c) 2006 5 * Damien Bergamini <damien.bergamini@free.fr> 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 /*- 21 * Driver for Atheros AR5005UG/AR5005UX chipsets. 22 * http://www.atheros.com/pt/bulletins/AR5005UGBulletin.pdf 23 * http://www.atheros.com/pt/bulletins/AR5005UXBulletin.pdf 24 * 25 * IMPORTANT NOTICE: 26 * This driver was written without any documentation or support from Atheros 27 * Communications. It is based on a black-box analysis of the Windows binary 28 * driver. It handles both pre and post-firmware devices. 29 */ 30 31 #include "bpfilter.h" 32 33 #include <sys/param.h> 34 #include <sys/sockio.h> 35 #include <sys/sysctl.h> 36 #include <sys/mbuf.h> 37 #include <sys/kernel.h> 38 #include <sys/socket.h> 39 #include <sys/systm.h> 40 #include <sys/malloc.h> 41 #include <sys/timeout.h> 42 #include <sys/conf.h> 43 #include <sys/device.h> 44 45 #include <machine/bus.h> 46 #include <machine/endian.h> 47 #include <machine/intr.h> 48 49 #if NBPFILTER > 0 50 #include <net/bpf.h> 51 #endif 52 #include <net/if.h> 53 #include <net/if_arp.h> 54 #include <net/if_dl.h> 55 #include <net/if_media.h> 56 #include <net/if_types.h> 57 58 #include <netinet/in.h> 59 #include <netinet/in_systm.h> 60 #include <netinet/in_var.h> 61 #include <netinet/if_ether.h> 62 #include <netinet/ip.h> 63 64 #include <net80211/ieee80211_var.h> 65 #include <net80211/ieee80211_amrr.h> 66 #include <net80211/ieee80211_radiotap.h> 67 68 #include <dev/rndvar.h> 69 #include <crypto/arc4.h> 70 71 #include <dev/usb/usb.h> 72 #include <dev/usb/usbdi.h> 73 #include <dev/usb/usbdi_util.h> 74 #include <dev/usb/usbdevs.h> 75 76 #include <dev/usb/if_uathreg.h> 77 #include <dev/usb/if_uathvar.h> 78 79 #ifdef USB_DEBUG 80 #define UATH_DEBUG 81 #endif 82 83 #ifdef UATH_DEBUG 84 #define DPRINTF(x) do { if (uath_debug) logprintf x; } while (0) 85 #define DPRINTFN(n, x) do { if (uath_debug >= (n)) logprintf x; } while (0) 86 int uath_debug = 1; 87 #else 88 #define DPRINTF(x) 89 #define DPRINTFN(n, x) 90 #endif 91 92 /*- 93 * Various supported device vendors/products. 94 * UB51: AR5005UG 802.11b/g, UB52: AR5005UX 802.11a/b/g 95 */ 96 #define UATH_DEV(v, p, f) \ 97 { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, (f) }, \ 98 { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p##_NF }, \ 99 (f) | UATH_FLAG_PRE_FIRMWARE } 100 #define UATH_DEV_UG(v, p) UATH_DEV(v, p, 0) 101 #define UATH_DEV_UX(v, p) UATH_DEV(v, p, UATH_FLAG_ABG) 102 static const struct uath_type { 103 struct usb_devno dev; 104 unsigned int flags; 105 #define UATH_FLAG_PRE_FIRMWARE (1 << 0) 106 #define UATH_FLAG_ABG (1 << 1) 107 } uath_devs[] = { 108 UATH_DEV_UG(ATHEROS, AR5523), 109 UATH_DEV_UG(ATHEROS2, AR5523_1), 110 UATH_DEV_UG(ATHEROS2, AR5523_2), 111 UATH_DEV_UX(ATHEROS2, AR5523_3), 112 UATH_DEV_UG(CONCEPTRONIC, AR5523_1), 113 UATH_DEV_UX(CONCEPTRONIC, AR5523_2), 114 UATH_DEV_UX(DLINK, DWLAG122), 115 UATH_DEV_UX(DLINK, DWLAG132), 116 UATH_DEV_UG(DLINK, DWLG132), 117 UATH_DEV_UG(GIGASET, SMCWUSBTG), 118 UATH_DEV_UG(GIGASET, AR5523), 119 UATH_DEV_UG(GLOBALSUN, AR5523_1), 120 UATH_DEV_UX(GLOBALSUN, AR5523_2), 121 UATH_DEV_UX(NETGEAR, WG111U), 122 UATH_DEV_UG(NETGEAR3, WG111T), 123 UATH_DEV_UG(NETGEAR3, WPN111), 124 UATH_DEV_UG(UMEDIA, AR5523_1), 125 UATH_DEV_UX(UMEDIA, AR5523_2), 126 UATH_DEV_UG(UMEDIA, TEW444UBEU), 127 UATH_DEV_UG(WISTRONNEWEB, AR5523_1), 128 UATH_DEV_UX(WISTRONNEWEB, AR5523_2), 129 UATH_DEV_UG(ZCOM, AR5523) 130 }; 131 #define uath_lookup(v, p) \ 132 ((struct uath_type *)usb_lookup(uath_devs, v, p)) 133 134 Static void uath_attachhook(void *); 135 Static int uath_open_pipes(struct uath_softc *); 136 Static void uath_close_pipes(struct uath_softc *); 137 Static int uath_alloc_tx_data_list(struct uath_softc *); 138 Static void uath_free_tx_data_list(struct uath_softc *); 139 Static int uath_alloc_rx_data_list(struct uath_softc *); 140 Static void uath_free_rx_data_list(struct uath_softc *); 141 Static void uath_free_rx_data(caddr_t, u_int, void *); 142 Static int uath_alloc_tx_cmd_list(struct uath_softc *); 143 Static void uath_free_tx_cmd_list(struct uath_softc *); 144 Static int uath_alloc_rx_cmd_list(struct uath_softc *); 145 Static void uath_free_rx_cmd_list(struct uath_softc *); 146 Static int uath_media_change(struct ifnet *); 147 Static void uath_stat(void *); 148 Static void uath_next_scan(void *); 149 Static void uath_task(void *); 150 Static int uath_newstate(struct ieee80211com *, enum ieee80211_state, 151 int); 152 #ifdef UATH_DEBUG 153 Static void uath_dump_cmd(const uint8_t *, int, char); 154 #endif 155 Static int uath_cmd(struct uath_softc *, uint32_t, const void *, int, 156 void *, int); 157 Static int uath_cmd_write(struct uath_softc *, uint32_t, const void *, 158 int, int); 159 Static int uath_cmd_read(struct uath_softc *, uint32_t, const void *, 160 int, void *, int); 161 Static int uath_write_reg(struct uath_softc *, uint32_t, uint32_t); 162 Static int uath_write_multi(struct uath_softc *, uint32_t, const void *, 163 int); 164 Static int uath_read_reg(struct uath_softc *, uint32_t, uint32_t *); 165 Static int uath_read_eeprom(struct uath_softc *, uint32_t, void *); 166 Static void uath_cmd_rxeof(usbd_xfer_handle, usbd_private_handle, 167 usbd_status); 168 Static void uath_data_rxeof(usbd_xfer_handle, usbd_private_handle, 169 usbd_status); 170 Static void uath_data_txeof(usbd_xfer_handle, usbd_private_handle, 171 usbd_status); 172 Static int uath_tx_null(struct uath_softc *); 173 Static int uath_tx_data(struct uath_softc *, struct mbuf *, 174 struct ieee80211_node *); 175 Static void uath_start(struct ifnet *); 176 Static void uath_watchdog(struct ifnet *); 177 Static int uath_ioctl(struct ifnet *, u_long, caddr_t); 178 Static int uath_query_eeprom(struct uath_softc *); 179 Static int uath_reset(struct uath_softc *); 180 Static int uath_reset_tx_queues(struct uath_softc *); 181 Static int uath_wme_init(struct uath_softc *); 182 Static int uath_set_chan(struct uath_softc *, struct ieee80211_channel *); 183 Static int uath_set_key(struct uath_softc *, 184 const struct ieee80211_wepkey *, int); 185 Static int uath_set_keys(struct uath_softc *); 186 Static int uath_set_rates(struct uath_softc *, 187 const struct ieee80211_rateset *); 188 Static int uath_set_rxfilter(struct uath_softc *, uint32_t, uint32_t); 189 Static int uath_set_led(struct uath_softc *, int, int); 190 Static int uath_switch_channel(struct uath_softc *, 191 struct ieee80211_channel *); 192 Static int uath_init(struct ifnet *); 193 Static void uath_stop(struct ifnet *, int); 194 Static int uath_loadfirmware(struct uath_softc *, const u_char *, int); 195 Static int uath_activate(device_ptr_t, enum devact); 196 197 /* 198 * Supported rates for 802.11b/g modes (in 500Kbps unit). 199 */ 200 static const struct ieee80211_rateset uath_rateset_11b = 201 { 4, { 2, 4, 11, 22 } }; 202 203 static const struct ieee80211_rateset uath_rateset_11g = 204 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } }; 205 206 USB_DECLARE_DRIVER(uath); 207 208 USB_MATCH(uath) 209 { 210 USB_MATCH_START(uath, uaa); 211 212 if (uaa->iface != NULL) 213 return UMATCH_NONE; 214 215 return (uath_lookup(uaa->vendor, uaa->product) != NULL) ? 216 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 217 } 218 219 Static void 220 uath_attachhook(void *xsc) 221 { 222 struct uath_softc *sc = xsc; 223 u_char *fw; 224 size_t size; 225 int error; 226 227 if ((error = loadfirmware("uath-ar5523", &fw, &size)) != 0) { 228 printf("%s: could not read firmware (error=%d)\n", 229 USBDEVNAME(sc->sc_dev), error); 230 return; 231 } 232 233 if ((error = uath_loadfirmware(sc, fw, size)) != 0) { 234 printf("%s: could not load firmware (error=%s)\n", 235 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 236 } 237 238 free(fw, M_DEVBUF); 239 } 240 241 USB_ATTACH(uath) 242 { 243 USB_ATTACH_START(uath, sc, uaa); 244 struct ieee80211com *ic = &sc->sc_ic; 245 struct ifnet *ifp = &ic->ic_if; 246 usbd_status error; 247 char *devinfop; 248 int i; 249 250 sc->sc_udev = uaa->device; 251 252 devinfop = usbd_devinfo_alloc(uaa->device, 0); 253 USB_ATTACH_SETUP; 254 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop); 255 usbd_devinfo_free(devinfop); 256 257 sc->sc_flags = uath_lookup(uaa->vendor, uaa->product)->flags; 258 259 if (usbd_set_config_no(sc->sc_udev, UATH_CONFIG_NO, 0) != 0) { 260 printf("%s: could not set configuration no\n", 261 USBDEVNAME(sc->sc_dev)); 262 USB_ATTACH_ERROR_RETURN; 263 } 264 265 /* get the first interface handle */ 266 error = usbd_device2interface_handle(sc->sc_udev, UATH_IFACE_INDEX, 267 &sc->sc_iface); 268 if (error != 0) { 269 printf("%s: could not get interface handle\n", 270 USBDEVNAME(sc->sc_dev)); 271 USB_ATTACH_ERROR_RETURN; 272 } 273 274 /* 275 * We must open the pipes early because they're used to upload the 276 * firmware (pre-firmware devices) or to send firmware commands. 277 */ 278 if (uath_open_pipes(sc) != 0) { 279 printf("%s: could not open pipes\n", USBDEVNAME(sc->sc_dev)); 280 USB_ATTACH_ERROR_RETURN; 281 } 282 283 if (sc->sc_flags & UATH_FLAG_PRE_FIRMWARE) { 284 if (rootvp == NULL) 285 mountroothook_establish(uath_attachhook, sc); 286 else 287 uath_attachhook(sc); 288 USB_ATTACH_SUCCESS_RETURN; 289 } 290 291 /* 292 * Only post-firmware devices here. 293 */ 294 usb_init_task(&sc->sc_task, uath_task, sc); 295 timeout_set(&sc->scan_to, uath_next_scan, sc); 296 timeout_set(&sc->stat_to, uath_stat, sc); 297 298 /* 299 * Allocate xfers for firmware commands. 300 */ 301 if (uath_alloc_tx_cmd_list(sc) != 0) { 302 printf("%s: could not allocate Tx command list\n", 303 USBDEVNAME(sc->sc_dev)); 304 goto fail1; 305 } 306 if (uath_alloc_rx_cmd_list(sc) != 0) { 307 printf("%s: could not allocate Rx command list\n", 308 USBDEVNAME(sc->sc_dev)); 309 goto fail2; 310 } 311 312 /* 313 * Queue Rx command xfers. 314 */ 315 for (i = 0; i < UATH_RX_CMD_LIST_COUNT; i++) { 316 struct uath_rx_cmd *cmd = &sc->rx_cmd[i]; 317 318 usbd_setup_xfer(cmd->xfer, sc->cmd_rx_pipe, cmd, cmd->buf, 319 UATH_MAX_RXCMDSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, 320 USBD_NO_TIMEOUT, uath_cmd_rxeof); 321 error = usbd_transfer(cmd->xfer); 322 if (error != USBD_IN_PROGRESS && error != 0) { 323 printf("%s: could not queue Rx command xfer\n", 324 USBDEVNAME(sc->sc_dev)); 325 goto fail3; 326 } 327 } 328 329 /* 330 * We're now ready to send/receive firmware commands. 331 */ 332 if (uath_reset(sc) != 0) { 333 printf("%s: could not initialize adapter\n", 334 USBDEVNAME(sc->sc_dev)); 335 goto fail3; 336 } 337 if (uath_query_eeprom(sc) != 0) { 338 printf("%s: could not read EEPROM\n", USBDEVNAME(sc->sc_dev)); 339 goto fail3; 340 } 341 342 printf("%s: MAC/BBP AR5523, RF AR%c112, address %s\n", 343 USBDEVNAME(sc->sc_dev), (sc->sc_flags & UATH_FLAG_ABG) ? '5': '2', 344 ether_sprintf(ic->ic_myaddr)); 345 346 /* 347 * Allocate xfers for Tx/Rx data pipes. 348 */ 349 if (uath_alloc_tx_data_list(sc) != 0) { 350 printf("%s: could not allocate Tx data list\n", 351 USBDEVNAME(sc->sc_dev)); 352 goto fail3; 353 } 354 if (uath_alloc_rx_data_list(sc) != 0) { 355 printf("%s: could not allocate Rx data list\n", 356 USBDEVNAME(sc->sc_dev)); 357 goto fail4; 358 } 359 360 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 361 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 362 ic->ic_state = IEEE80211_S_INIT; 363 364 /* set device capabilities */ 365 ic->ic_caps = 366 IEEE80211_C_TXPMGT | /* tx power management */ 367 IEEE80211_C_SHPREAMBLE | /* short preamble supported */ 368 IEEE80211_C_SHSLOT | /* short slot time supported */ 369 IEEE80211_C_WEP; /* h/w WEP */ 370 371 /* set supported .11b and .11g rates */ 372 ic->ic_sup_rates[IEEE80211_MODE_11B] = uath_rateset_11b; 373 ic->ic_sup_rates[IEEE80211_MODE_11G] = uath_rateset_11g; 374 375 /* set supported .11b and .11g channels (1 through 14) */ 376 for (i = 1; i <= 14; i++) { 377 ic->ic_channels[i].ic_freq = 378 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ); 379 ic->ic_channels[i].ic_flags = 380 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 381 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 382 } 383 384 ifp->if_softc = sc; 385 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 386 ifp->if_init = uath_init; 387 ifp->if_ioctl = uath_ioctl; 388 ifp->if_start = uath_start; 389 ifp->if_watchdog = uath_watchdog; 390 IFQ_SET_READY(&ifp->if_snd); 391 memcpy(ifp->if_xname, USBDEVNAME(sc->sc_dev), IFNAMSIZ); 392 393 if_attach(ifp); 394 ieee80211_ifattach(ifp); 395 396 /* override state transition machine */ 397 sc->sc_newstate = ic->ic_newstate; 398 ic->ic_newstate = uath_newstate; 399 ieee80211_media_init(ifp, uath_media_change, ieee80211_media_status); 400 401 #if NBPFILTER > 0 402 bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO, 403 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN); 404 405 sc->sc_rxtap_len = sizeof sc->sc_rxtapu; 406 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 407 sc->sc_rxtap.wr_ihdr.it_present = htole32(UATH_RX_RADIOTAP_PRESENT); 408 409 sc->sc_txtap_len = sizeof sc->sc_txtapu; 410 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 411 sc->sc_txtap.wt_ihdr.it_present = htole32(UATH_TX_RADIOTAP_PRESENT); 412 #endif 413 414 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 415 USBDEV(sc->sc_dev)); 416 417 USB_ATTACH_SUCCESS_RETURN; 418 419 fail4: uath_free_tx_data_list(sc); 420 fail3: uath_free_rx_cmd_list(sc); 421 fail2: uath_free_tx_cmd_list(sc); 422 fail1: uath_close_pipes(sc); 423 424 USB_ATTACH_ERROR_RETURN; 425 } 426 427 USB_DETACH(uath) 428 { 429 USB_DETACH_START(uath, sc); 430 struct ifnet *ifp = &sc->sc_ic.ic_if; 431 int s; 432 433 s = splnet(); 434 435 if (sc->sc_flags & UATH_FLAG_PRE_FIRMWARE) { 436 uath_close_pipes(sc); 437 splx(s); 438 return 0; 439 } 440 441 /* post-firmware device */ 442 443 usb_rem_task(sc->sc_udev, &sc->sc_task); 444 timeout_del(&sc->scan_to); 445 timeout_del(&sc->stat_to); 446 447 ieee80211_ifdetach(ifp); /* free all nodes */ 448 if_detach(ifp); 449 450 sc->sc_dying = 1; 451 DPRINTF(("reclaiming %d references\n", sc->sc_refcnt)); 452 while (sc->sc_refcnt > 0) 453 (void)tsleep(UATH_COND_NOREF(sc), 0, "uathdet", 0); 454 DPRINTF(("all references reclaimed\n")); 455 456 /* abort and free xfers */ 457 uath_free_tx_data_list(sc); 458 uath_free_rx_data_list(sc); 459 uath_free_tx_cmd_list(sc); 460 uath_free_rx_cmd_list(sc); 461 462 /* close Tx/Rx pipes */ 463 uath_close_pipes(sc); 464 465 splx(s); 466 467 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 468 USBDEV(sc->sc_dev)); 469 470 return 0; 471 } 472 473 Static int 474 uath_open_pipes(struct uath_softc *sc) 475 { 476 int error; 477 478 /* 479 * XXX pipes numbers are hardcoded because we don't have any way 480 * to distinguish the data pipes from the firmware command pipes 481 * (both are bulk pipes) using the endpoints descriptors. 482 */ 483 error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE, 484 &sc->cmd_tx_pipe); 485 if (error != 0) { 486 printf("%s: could not open Tx command pipe: %s\n", 487 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 488 goto fail; 489 } 490 491 error = usbd_open_pipe(sc->sc_iface, 0x02, USBD_EXCLUSIVE_USE, 492 &sc->data_tx_pipe); 493 if (error != 0) { 494 printf("%s: could not open Tx data pipe: %s\n", 495 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 496 goto fail; 497 } 498 499 error = usbd_open_pipe(sc->sc_iface, 0x81, USBD_EXCLUSIVE_USE, 500 &sc->cmd_rx_pipe); 501 if (error != 0) { 502 printf("%s: could not open Rx command pipe: %s\n", 503 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 504 goto fail; 505 } 506 507 error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE, 508 &sc->data_rx_pipe); 509 if (error != 0) { 510 printf("%s: could not open Rx data pipe: %s\n", 511 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 512 goto fail; 513 } 514 515 return 0; 516 517 fail: uath_close_pipes(sc); 518 return error; 519 } 520 521 Static void 522 uath_close_pipes(struct uath_softc *sc) 523 { 524 /* assumes no transfers are pending on the pipes */ 525 526 if (sc->data_tx_pipe != NULL) 527 usbd_close_pipe(sc->data_tx_pipe); 528 529 if (sc->data_rx_pipe != NULL) 530 usbd_close_pipe(sc->data_rx_pipe); 531 532 if (sc->cmd_tx_pipe != NULL) 533 usbd_close_pipe(sc->cmd_tx_pipe); 534 535 if (sc->cmd_rx_pipe != NULL) 536 usbd_close_pipe(sc->cmd_rx_pipe); 537 } 538 539 Static int 540 uath_alloc_tx_data_list(struct uath_softc *sc) 541 { 542 int i, error; 543 544 for (i = 0; i < UATH_TX_DATA_LIST_COUNT; i++) { 545 struct uath_tx_data *data = &sc->tx_data[i]; 546 547 data->sc = sc; /* backpointer for callbacks */ 548 549 data->xfer = usbd_alloc_xfer(sc->sc_udev); 550 if (data->xfer == NULL) { 551 printf("%s: could not allocate xfer\n", 552 USBDEVNAME(sc->sc_dev)); 553 error = ENOMEM; 554 goto fail; 555 } 556 data->buf = usbd_alloc_buffer(data->xfer, UATH_MAX_TXBUFSZ); 557 if (data->buf == NULL) { 558 printf("%s: could not allocate xfer buffer\n", 559 USBDEVNAME(sc->sc_dev)); 560 error = ENOMEM; 561 goto fail; 562 } 563 } 564 return 0; 565 566 fail: uath_free_tx_data_list(sc); 567 return error; 568 } 569 570 Static void 571 uath_free_tx_data_list(struct uath_softc *sc) 572 { 573 int i; 574 575 /* make sure no transfers are pending */ 576 usbd_abort_pipe(sc->data_tx_pipe); 577 578 for (i = 0; i < UATH_TX_DATA_LIST_COUNT; i++) 579 if (sc->tx_data[i].xfer != NULL) 580 usbd_free_xfer(sc->tx_data[i].xfer); 581 } 582 583 Static int 584 uath_alloc_rx_data_list(struct uath_softc *sc) 585 { 586 int i, error; 587 588 for (i = 0; i < UATH_RX_DATA_POOL_COUNT; i++) { 589 struct uath_rx_data *data = &sc->rx_data[i]; 590 591 data->sc = sc; /* backpointer for callbacks */ 592 593 data->xfer = usbd_alloc_xfer(sc->sc_udev); 594 if (data->xfer == NULL) { 595 printf("%s: could not allocate xfer\n", 596 USBDEVNAME(sc->sc_dev)); 597 error = ENOMEM; 598 goto fail; 599 } 600 data->buf = usbd_alloc_buffer(data->xfer, sc->rxbufsz); 601 if (data->buf == NULL) { 602 printf("%s: could not allocate xfer buffer\n", 603 USBDEVNAME(sc->sc_dev)); 604 error = ENOMEM; 605 goto fail; 606 } 607 SLIST_INSERT_HEAD(&sc->rx_freelist, data, next); 608 } 609 return 0; 610 611 fail: uath_free_rx_data_list(sc); 612 return error; 613 } 614 615 Static void 616 uath_free_rx_data_list(struct uath_softc *sc) 617 { 618 int i; 619 620 /* make sure no transfers are pending */ 621 usbd_abort_pipe(sc->data_rx_pipe); 622 623 for (i = 0; i < UATH_RX_DATA_POOL_COUNT; i++) 624 if (sc->rx_data[i].xfer != NULL) 625 usbd_free_xfer(sc->rx_data[i].xfer); 626 } 627 628 Static void 629 uath_free_rx_data(caddr_t buf, u_int size, void *arg) 630 { 631 struct uath_rx_data *data = arg; 632 struct uath_softc *sc = data->sc; 633 634 /* put the buffer back in the free list */ 635 SLIST_INSERT_HEAD(&sc->rx_freelist, data, next); 636 637 /* release reference to softc */ 638 if (--sc->sc_refcnt == 0 && sc->sc_dying) 639 wakeup(UATH_COND_NOREF(sc)); 640 } 641 642 Static int 643 uath_alloc_tx_cmd_list(struct uath_softc *sc) 644 { 645 int i, error; 646 647 for (i = 0; i < UATH_TX_CMD_LIST_COUNT; i++) { 648 struct uath_tx_cmd *cmd = &sc->tx_cmd[i]; 649 650 cmd->sc = sc; /* backpointer for callbacks */ 651 652 cmd->xfer = usbd_alloc_xfer(sc->sc_udev); 653 if (cmd->xfer == NULL) { 654 printf("%s: could not allocate xfer\n", 655 USBDEVNAME(sc->sc_dev)); 656 error = ENOMEM; 657 goto fail; 658 } 659 cmd->buf = usbd_alloc_buffer(cmd->xfer, UATH_MAX_TXCMDSZ); 660 if (cmd->buf == NULL) { 661 printf("%s: could not allocate xfer buffer\n", 662 USBDEVNAME(sc->sc_dev)); 663 error = ENOMEM; 664 goto fail; 665 } 666 } 667 return 0; 668 669 fail: uath_free_tx_cmd_list(sc); 670 return error; 671 } 672 673 Static void 674 uath_free_tx_cmd_list(struct uath_softc *sc) 675 { 676 int i; 677 678 /* make sure no transfers are pending */ 679 usbd_abort_pipe(sc->cmd_tx_pipe); 680 681 for (i = 0; i < UATH_TX_CMD_LIST_COUNT; i++) 682 if (sc->tx_cmd[i].xfer != NULL) 683 usbd_free_xfer(sc->tx_cmd[i].xfer); 684 } 685 686 Static int 687 uath_alloc_rx_cmd_list(struct uath_softc *sc) 688 { 689 int i, error; 690 691 for (i = 0; i < UATH_RX_CMD_LIST_COUNT; i++) { 692 struct uath_rx_cmd *cmd = &sc->rx_cmd[i]; 693 694 cmd->sc = sc; /* backpointer for callbacks */ 695 696 cmd->xfer = usbd_alloc_xfer(sc->sc_udev); 697 if (cmd->xfer == NULL) { 698 printf("%s: could not allocate xfer\n", 699 USBDEVNAME(sc->sc_dev)); 700 error = ENOMEM; 701 goto fail; 702 } 703 cmd->buf = usbd_alloc_buffer(cmd->xfer, UATH_MAX_RXCMDSZ); 704 if (cmd->buf == NULL) { 705 printf("%s: could not allocate xfer buffer\n", 706 USBDEVNAME(sc->sc_dev)); 707 error = ENOMEM; 708 goto fail; 709 } 710 } 711 return 0; 712 713 fail: uath_free_rx_cmd_list(sc); 714 return error; 715 } 716 717 Static void 718 uath_free_rx_cmd_list(struct uath_softc *sc) 719 { 720 int i; 721 722 /* make sure no transfers are pending */ 723 usbd_abort_pipe(sc->cmd_rx_pipe); 724 725 for (i = 0; i < UATH_RX_CMD_LIST_COUNT; i++) 726 if (sc->rx_cmd[i].xfer != NULL) 727 usbd_free_xfer(sc->rx_cmd[i].xfer); 728 } 729 730 Static int 731 uath_media_change(struct ifnet *ifp) 732 { 733 int error; 734 735 error = ieee80211_media_change(ifp); 736 if (error != ENETRESET) 737 return error; 738 739 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) 740 uath_init(ifp); 741 742 return 0; 743 } 744 745 /* 746 * This function is called periodically (every second) when associated to 747 * query device statistics. 748 */ 749 Static void 750 uath_stat(void *arg) 751 { 752 struct uath_softc *sc = arg; 753 int error; 754 755 /* 756 * Send request for statistics asynchronously. The timer will be 757 * restarted when we'll get the stats notification. 758 */ 759 error = uath_cmd_write(sc, UATH_CMD_STATS, NULL, 0, 760 UATH_CMD_FLAG_ASYNC); 761 if (error != 0) { 762 printf("%s: could not query statistics (error=%d)\n", 763 USBDEVNAME(sc->sc_dev), error); 764 } 765 } 766 767 /* 768 * This function is called periodically (every 250ms) during scanning to 769 * switch from one channel to another. 770 */ 771 Static void 772 uath_next_scan(void *arg) 773 { 774 struct uath_softc *sc = arg; 775 struct ieee80211com *ic = &sc->sc_ic; 776 struct ifnet *ifp = &ic->ic_if; 777 778 if (ic->ic_state == IEEE80211_S_SCAN) 779 ieee80211_next_scan(ifp); 780 } 781 782 Static void 783 uath_task(void *arg) 784 { 785 struct uath_softc *sc = arg; 786 struct ieee80211com *ic = &sc->sc_ic; 787 enum ieee80211_state ostate; 788 789 ostate = ic->ic_state; 790 791 switch (sc->sc_state) { 792 case IEEE80211_S_INIT: 793 if (ostate == IEEE80211_S_RUN) { 794 /* turn link and activity LEDs off */ 795 (void)uath_set_led(sc, UATH_LED_LINK, 0); 796 (void)uath_set_led(sc, UATH_LED_ACTIVITY, 0); 797 } 798 break; 799 800 case IEEE80211_S_SCAN: 801 if (uath_switch_channel(sc, ic->ic_bss->ni_chan) != 0) { 802 printf("%s: could not switch channel\n", 803 USBDEVNAME(sc->sc_dev)); 804 break; 805 } 806 timeout_add(&sc->scan_to, hz / 4); 807 break; 808 809 case IEEE80211_S_AUTH: 810 { 811 struct ieee80211_node *ni = ic->ic_bss; 812 struct uath_cmd_bssid bssid; 813 struct uath_cmd_0b cmd0b; 814 struct uath_cmd_0c cmd0c; 815 816 if (uath_switch_channel(sc, ni->ni_chan) != 0) { 817 printf("%s: could not switch channel\n", 818 USBDEVNAME(sc->sc_dev)); 819 break; 820 } 821 822 (void)uath_cmd_write(sc, UATH_CMD_24, NULL, 0, 0); 823 824 bzero(&bssid, sizeof bssid); 825 bssid.len = htobe32(IEEE80211_ADDR_LEN); 826 IEEE80211_ADDR_COPY(bssid.bssid, ni->ni_bssid); 827 (void)uath_cmd_write(sc, UATH_CMD_SET_BSSID, &bssid, 828 sizeof bssid, 0); 829 830 bzero(&cmd0b, sizeof cmd0b); 831 cmd0b.code = htobe32(2); 832 cmd0b.size = htobe32(sizeof (cmd0b.data)); 833 (void)uath_cmd_write(sc, UATH_CMD_0B, &cmd0b, sizeof cmd0b, 0); 834 835 bzero(&cmd0c, sizeof cmd0c); 836 cmd0c.magic1 = htobe32(2); 837 cmd0c.magic2 = htobe32(7); 838 cmd0c.magic3 = htobe32(1); 839 (void)uath_cmd_write(sc, UATH_CMD_0C, &cmd0c, sizeof cmd0c, 0); 840 841 if (uath_set_rates(sc, &ni->ni_rates) != 0) { 842 printf("%s: could not set negotiated rate set\n", 843 USBDEVNAME(sc->sc_dev)); 844 break; 845 } 846 break; 847 } 848 849 case IEEE80211_S_ASSOC: 850 break; 851 852 case IEEE80211_S_RUN: 853 { 854 struct ieee80211_node *ni = ic->ic_bss; 855 struct uath_cmd_bssid bssid; 856 struct uath_cmd_xled xled; 857 uint32_t val; 858 859 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 860 /* make both LEDs blink while monitoring */ 861 bzero(&xled, sizeof xled); 862 xled.which = htobe32(0); 863 xled.rate = htobe32(1); 864 xled.mode = htobe32(2); 865 (void)uath_cmd_write(sc, UATH_CMD_SET_XLED, &xled, 866 sizeof xled, 0); 867 break; 868 } 869 870 /* 871 * Tx rate is controlled by firmware, report the maximum 872 * negotiated rate in ifconfig output. 873 */ 874 ni->ni_txrate = ni->ni_rates.rs_nrates - 1; 875 876 val = htobe32(1); 877 (void)uath_cmd_write(sc, UATH_CMD_2E, &val, sizeof val, 0); 878 879 bzero(&bssid, sizeof bssid); 880 bssid.flags1 = htobe32(0xc004); 881 bssid.flags2 = htobe32(0x003b); 882 bssid.len = htobe32(IEEE80211_ADDR_LEN); 883 IEEE80211_ADDR_COPY(bssid.bssid, ni->ni_bssid); 884 (void)uath_cmd_write(sc, UATH_CMD_SET_BSSID, &bssid, 885 sizeof bssid, 0); 886 887 /* turn link LED on */ 888 (void)uath_set_led(sc, UATH_LED_LINK, 1); 889 890 /* make activity LED blink */ 891 bzero(&xled, sizeof xled); 892 xled.which = htobe32(1); 893 xled.rate = htobe32(1); 894 xled.mode = htobe32(2); 895 (void)uath_cmd_write(sc, UATH_CMD_SET_XLED, &xled, sizeof xled, 896 0); 897 898 /* set state to associated */ 899 val = htobe32(1); 900 (void)uath_cmd_write(sc, UATH_CMD_SET_STATE, &val, sizeof val, 901 0); 902 903 /* start statistics timer */ 904 timeout_add(&sc->stat_to, hz); 905 break; 906 } 907 } 908 sc->sc_newstate(ic, sc->sc_state, sc->sc_arg); 909 } 910 911 Static int 912 uath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 913 { 914 struct uath_softc *sc = ic->ic_softc; 915 916 usb_rem_task(sc->sc_udev, &sc->sc_task); 917 timeout_del(&sc->scan_to); 918 timeout_del(&sc->stat_to); 919 920 /* do it in a process context */ 921 sc->sc_state = nstate; 922 sc->sc_arg = arg; 923 usb_add_task(sc->sc_udev, &sc->sc_task); 924 925 return 0; 926 } 927 928 #ifdef UATH_DEBUG 929 Static void 930 uath_dump_cmd(const uint8_t *buf, int len, char prefix) 931 { 932 int i; 933 934 for (i = 0; i < len; i++) { 935 if ((i % 16) == 0) 936 printf("\n%c ", prefix); 937 else if ((i % 4) == 0) 938 printf(" "); 939 printf("%02x", buf[i]); 940 } 941 printf("\n"); 942 } 943 #endif 944 945 /* 946 * Low-level function to send read or write commands to the firmware. 947 */ 948 Static int 949 uath_cmd(struct uath_softc *sc, uint32_t code, const void *idata, int ilen, 950 void *odata, int flags) 951 { 952 struct uath_cmd_hdr *hdr; 953 struct uath_tx_cmd *cmd; 954 uint16_t xferflags; 955 int s, xferlen, error; 956 957 /* grab a xfer */ 958 cmd = &sc->tx_cmd[sc->cmd_idx]; 959 960 /* always bulk-out a multiple of 4 bytes */ 961 xferlen = (sizeof (struct uath_cmd_hdr) + ilen + 3) & ~3; 962 963 hdr = (struct uath_cmd_hdr *)cmd->buf; 964 bzero(hdr, sizeof (struct uath_cmd_hdr)); 965 hdr->len = htobe32(xferlen); 966 hdr->code = htobe32(code); 967 hdr->priv = sc->cmd_idx; /* don't care about endianness */ 968 hdr->magic = htobe32((flags & UATH_CMD_FLAG_MAGIC) ? 1 << 24 : 0); 969 bcopy(idata, (uint8_t *)(hdr + 1), ilen); 970 971 #ifdef UATH_DEBUG 972 if (uath_debug >= 5) { 973 printf("sending command code=0x%02x flags=0x%x index=%u", 974 code, flags, sc->cmd_idx); 975 uath_dump_cmd(cmd->buf, xferlen, '+'); 976 } 977 #endif 978 xferflags = USBD_FORCE_SHORT_XFER | USBD_NO_COPY; 979 if (!(flags & UATH_CMD_FLAG_READ)) { 980 if (!(flags & UATH_CMD_FLAG_ASYNC)) 981 xferflags |= USBD_SYNCHRONOUS; 982 } else 983 s = splusb(); 984 985 cmd->odata = odata; 986 987 usbd_setup_xfer(cmd->xfer, sc->cmd_tx_pipe, cmd, cmd->buf, xferlen, 988 xferflags, UATH_CMD_TIMEOUT, NULL); 989 error = usbd_transfer(cmd->xfer); 990 if (error != USBD_IN_PROGRESS && error != 0) { 991 if (flags & UATH_CMD_FLAG_READ) 992 splx(s); 993 printf("%s: could not send command (error=%s)\n", 994 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 995 return error; 996 } 997 sc->cmd_idx = (sc->cmd_idx + 1) % UATH_TX_CMD_LIST_COUNT; 998 999 if (!(flags & UATH_CMD_FLAG_READ)) 1000 return 0; /* write: don't wait for reply */ 1001 1002 /* wait at most two seconds for command reply */ 1003 error = tsleep(cmd, PCATCH, "uathcmd", 2 * hz); 1004 cmd->odata = NULL; /* in case answer is received too late */ 1005 splx(s); 1006 if (error != 0) { 1007 printf("%s: timeout waiting for command reply\n", 1008 USBDEVNAME(sc->sc_dev)); 1009 } 1010 return error; 1011 } 1012 1013 Static int 1014 uath_cmd_write(struct uath_softc *sc, uint32_t code, const void *data, int len, 1015 int flags) 1016 { 1017 flags &= ~UATH_CMD_FLAG_READ; 1018 return uath_cmd(sc, code, data, len, NULL, flags); 1019 } 1020 1021 Static int 1022 uath_cmd_read(struct uath_softc *sc, uint32_t code, const void *idata, 1023 int ilen, void *odata, int flags) 1024 { 1025 flags |= UATH_CMD_FLAG_READ; 1026 return uath_cmd(sc, code, idata, ilen, odata, flags); 1027 } 1028 1029 Static int 1030 uath_write_reg(struct uath_softc *sc, uint32_t reg, uint32_t val) 1031 { 1032 struct uath_write_mac write; 1033 int error; 1034 1035 write.reg = htobe32(reg); 1036 write.len = htobe32(0); /* 0 = single write */ 1037 *(uint32_t *)write.data = htobe32(val); 1038 1039 error = uath_cmd_write(sc, UATH_CMD_WRITE_MAC, &write, 1040 3 * sizeof (uint32_t), 0); 1041 if (error != 0) { 1042 printf("%s: could not write register 0x%02x\n", 1043 USBDEVNAME(sc->sc_dev), reg); 1044 } 1045 return error; 1046 } 1047 1048 Static int 1049 uath_write_multi(struct uath_softc *sc, uint32_t reg, const void *data, 1050 int len) 1051 { 1052 struct uath_write_mac write; 1053 int error; 1054 1055 write.reg = htobe32(reg); 1056 write.len = htobe32(len); 1057 bcopy(data, write.data, len); 1058 1059 /* properly handle the case where len is zero (reset) */ 1060 error = uath_cmd_write(sc, UATH_CMD_WRITE_MAC, &write, 1061 (len == 0) ? sizeof (uint32_t) : 2 * sizeof (uint32_t) + len, 0); 1062 if (error != 0) { 1063 printf("%s: could not write %d bytes to register 0x%02x\n", 1064 USBDEVNAME(sc->sc_dev), len, reg); 1065 } 1066 return error; 1067 } 1068 1069 Static int 1070 uath_read_reg(struct uath_softc *sc, uint32_t reg, uint32_t *val) 1071 { 1072 struct uath_read_mac read; 1073 int error; 1074 1075 reg = htobe32(reg); 1076 error = uath_cmd_read(sc, UATH_CMD_READ_MAC, ®, sizeof reg, &read, 1077 0); 1078 if (error != 0) { 1079 printf("%s: could not read register 0x%02x\n", 1080 USBDEVNAME(sc->sc_dev), betoh32(reg)); 1081 return error; 1082 } 1083 *val = betoh32(*(uint32_t *)read.data); 1084 return error; 1085 } 1086 1087 Static int 1088 uath_read_eeprom(struct uath_softc *sc, uint32_t reg, void *odata) 1089 { 1090 struct uath_read_mac read; 1091 int len, error; 1092 1093 reg = htobe32(reg); 1094 error = uath_cmd_read(sc, UATH_CMD_READ_EEPROM, ®, sizeof reg, 1095 &read, 0); 1096 if (error != 0) { 1097 printf("%s: could not read EEPROM offset 0x%02x\n", 1098 USBDEVNAME(sc->sc_dev), betoh32(reg)); 1099 return error; 1100 } 1101 len = betoh32(read.len); 1102 bcopy(read.data, odata, (len == 0) ? sizeof (uint32_t) : len); 1103 return error; 1104 } 1105 1106 Static void 1107 uath_cmd_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, 1108 usbd_status status) 1109 { 1110 struct uath_rx_cmd *cmd = priv; 1111 struct uath_softc *sc = cmd->sc; 1112 struct uath_cmd_hdr *hdr; 1113 1114 if (status != USBD_NORMAL_COMPLETION) { 1115 if (status == USBD_STALLED) 1116 usbd_clear_endpoint_stall_async(sc->cmd_rx_pipe); 1117 return; 1118 } 1119 1120 hdr = (struct uath_cmd_hdr *)cmd->buf; 1121 1122 #ifdef UATH_DEBUG 1123 if (uath_debug >= 5) { 1124 printf("received command code=0x%x index=%u len=%u", 1125 betoh32(hdr->code), hdr->priv, betoh32(hdr->len)); 1126 uath_dump_cmd(cmd->buf, betoh32(hdr->len), '-'); 1127 } 1128 #endif 1129 1130 switch (betoh32(hdr->code) & 0xff) { 1131 /* reply to a read command */ 1132 default: 1133 { 1134 struct uath_tx_cmd *txcmd = &sc->tx_cmd[hdr->priv]; 1135 1136 if (txcmd->odata != NULL) { 1137 /* copy answer into caller's supplied buffer */ 1138 bcopy((uint8_t *)(hdr + 1), txcmd->odata, 1139 betoh32(hdr->len) - sizeof (struct uath_cmd_hdr)); 1140 } 1141 wakeup(txcmd); /* wake up caller */ 1142 break; 1143 } 1144 /* spontaneous firmware notifications */ 1145 case UATH_NOTIF_READY: 1146 DPRINTF(("received device ready notification\n")); 1147 wakeup(UATH_COND_INIT(sc)); 1148 break; 1149 1150 case UATH_NOTIF_TX: 1151 /* this notification is sent when UATH_TX_NOTIFY is set */ 1152 DPRINTF(("received Tx notification\n")); 1153 break; 1154 1155 case UATH_NOTIF_STATS: 1156 DPRINTFN(2, ("received device statistics\n")); 1157 timeout_add(&sc->stat_to, hz); 1158 break; 1159 } 1160 1161 /* setup a new transfer */ 1162 usbd_setup_xfer(xfer, sc->cmd_rx_pipe, cmd, cmd->buf, UATH_MAX_RXCMDSZ, 1163 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, 1164 uath_cmd_rxeof); 1165 (void)usbd_transfer(xfer); 1166 } 1167 1168 Static void 1169 uath_data_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, 1170 usbd_status status) 1171 { 1172 struct uath_rx_data *data = priv; 1173 struct uath_softc *sc = data->sc; 1174 struct ieee80211com *ic = &sc->sc_ic; 1175 struct ifnet *ifp = &ic->ic_if; 1176 struct ieee80211_frame *wh; 1177 struct ieee80211_node *ni; 1178 struct uath_rx_data *ndata; 1179 struct uath_rx_desc *desc; 1180 struct mbuf *m; 1181 uint32_t hdr; 1182 int s, len; 1183 1184 if (status != USBD_NORMAL_COMPLETION) { 1185 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1186 return; 1187 1188 if (status == USBD_STALLED) 1189 usbd_clear_endpoint_stall_async(sc->data_rx_pipe); 1190 1191 ifp->if_ierrors++; 1192 return; 1193 } 1194 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 1195 1196 if (len < UATH_MIN_RXBUFSZ || len > sc->rxbufsz) { 1197 DPRINTF(("wrong xfer size: !(%d <= %d <= %d)\n", 1198 UATH_MIN_RXBUFSZ, len, sc->rxbufsz)); 1199 ifp->if_ierrors++; 1200 goto skip; 1201 } 1202 1203 hdr = betoh32(*(uint32_t *)data->buf); 1204 1205 /* Rx descriptor is located at the end, 32-bit aligned */ 1206 desc = (struct uath_rx_desc *) 1207 (data->buf + len - sizeof (struct uath_rx_desc)); 1208 1209 /* there's probably a "bad CRC" flag somewhere in the descriptor.. */ 1210 1211 MGETHDR(m, M_DONTWAIT, MT_DATA); 1212 if (m == NULL) { 1213 ifp->if_ierrors++; 1214 goto skip; 1215 } 1216 1217 /* grab a new Rx buffer */ 1218 ndata = SLIST_FIRST(&sc->rx_freelist); 1219 if (ndata == NULL) { 1220 printf("%s: could not allocate Rx buffer\n", 1221 USBDEVNAME(sc->sc_dev)); 1222 m_freem(m); 1223 ifp->if_ierrors++; 1224 goto skip; 1225 } 1226 SLIST_REMOVE_HEAD(&sc->rx_freelist, next); 1227 1228 MEXTADD(m, data->buf, sc->rxbufsz, 0, uath_free_rx_data, data); 1229 1230 /* finalize mbuf */ 1231 m->m_pkthdr.rcvif = ifp; 1232 m->m_data = data->buf + sizeof (uint32_t); 1233 m->m_pkthdr.len = m->m_len = 1234 betoh32(desc->len) - sizeof (struct uath_rx_desc); 1235 1236 data = ndata; 1237 1238 wh = mtod(m, struct ieee80211_frame *); 1239 if ((wh->i_fc[1] & IEEE80211_FC1_WEP) && 1240 ic->ic_opmode != IEEE80211_M_MONITOR) { 1241 /* 1242 * Hardware decrypts the frame itself but leaves the WEP bit 1243 * set in the 802.11 header and doesn't remove the IV and CRC 1244 * fields. 1245 */ 1246 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 1247 ovbcopy(wh, (caddr_t)wh + IEEE80211_WEP_IVLEN + 1248 IEEE80211_WEP_KIDLEN, sizeof (struct ieee80211_frame)); 1249 m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN); 1250 m_adj(m, -IEEE80211_WEP_CRCLEN); 1251 wh = mtod(m, struct ieee80211_frame *); 1252 } 1253 1254 #if NBPFILTER > 0 1255 /* there are a lot more fields in the Rx descriptor */ 1256 if (sc->sc_drvbpf != NULL) { 1257 struct mbuf mb; 1258 struct uath_rx_radiotap_header *tap = &sc->sc_rxtap; 1259 1260 tap->wr_flags = 0; 1261 tap->wr_chan_freq = htole16(betoh32(desc->freq)); 1262 tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags); 1263 tap->wr_dbm_antsignal = (int8_t)betoh32(desc->rssi); 1264 1265 M_DUP_PKTHDR(&mb, m); 1266 mb.m_data = (caddr_t)tap; 1267 mb.m_len = sc->sc_rxtap_len; 1268 mb.m_next = m; 1269 mb.m_pkthdr.len += mb.m_len; 1270 bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN); 1271 } 1272 #endif 1273 1274 s = splnet(); 1275 sc->sc_refcnt++; 1276 ni = ieee80211_find_rxnode(ic, wh); 1277 ieee80211_input(ifp, m, ni, (int)betoh32(desc->rssi), 0); 1278 1279 /* node is no longer needed */ 1280 ieee80211_release_node(ic, ni); 1281 splx(s); 1282 1283 skip: /* setup a new transfer */ 1284 usbd_setup_xfer(data->xfer, sc->data_rx_pipe, data, data->buf, 1285 sc->rxbufsz, USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, 1286 uath_data_rxeof); 1287 (void)usbd_transfer(data->xfer); 1288 } 1289 1290 Static int 1291 uath_tx_null(struct uath_softc *sc) 1292 { 1293 struct uath_tx_data *data; 1294 struct uath_tx_desc *desc; 1295 1296 data = &sc->tx_data[sc->data_idx]; 1297 1298 data->ni = NULL; 1299 1300 *(uint32_t *)data->buf = UATH_MAKECTL(1, sizeof (struct uath_tx_desc)); 1301 desc = (struct uath_tx_desc *)(data->buf + sizeof (uint32_t)); 1302 1303 bzero(desc, sizeof (struct uath_tx_desc)); 1304 desc->len = htobe32(sizeof (struct uath_tx_desc)); 1305 desc->type = htobe32(UATH_TX_NULL); 1306 1307 usbd_setup_xfer(data->xfer, sc->data_tx_pipe, data, data->buf, 1308 sizeof (uint32_t) + sizeof (struct uath_tx_desc), USBD_NO_COPY | 1309 USBD_FORCE_SHORT_XFER, UATH_DATA_TIMEOUT, NULL); 1310 if (usbd_sync_transfer(data->xfer) != 0) 1311 return EIO; 1312 1313 sc->data_idx = (sc->data_idx + 1) % UATH_TX_DATA_LIST_COUNT; 1314 1315 return uath_cmd_write(sc, UATH_CMD_0F, NULL, 0, UATH_CMD_FLAG_ASYNC); 1316 } 1317 1318 Static void 1319 uath_data_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, 1320 usbd_status status) 1321 { 1322 struct uath_tx_data *data = priv; 1323 struct uath_softc *sc = data->sc; 1324 struct ieee80211com *ic = &sc->sc_ic; 1325 struct ifnet *ifp = &ic->ic_if; 1326 int s; 1327 1328 if (status != USBD_NORMAL_COMPLETION) { 1329 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1330 return; 1331 1332 printf("%s: could not transmit buffer: %s\n", 1333 USBDEVNAME(sc->sc_dev), usbd_errstr(status)); 1334 1335 if (status == USBD_STALLED) 1336 usbd_clear_endpoint_stall_async(sc->data_tx_pipe); 1337 1338 ifp->if_oerrors++; 1339 return; 1340 } 1341 1342 s = splnet(); 1343 1344 ieee80211_release_node(ic, data->ni); 1345 data->ni = NULL; 1346 1347 sc->tx_queued--; 1348 ifp->if_opackets++; 1349 1350 sc->sc_tx_timer = 0; 1351 ifp->if_flags &= ~IFF_OACTIVE; 1352 uath_start(ifp); 1353 1354 splx(s); 1355 } 1356 1357 Static int 1358 uath_tx_data(struct uath_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1359 { 1360 struct ieee80211com *ic = &sc->sc_ic; 1361 struct uath_tx_data *data; 1362 struct uath_tx_desc *desc; 1363 const struct ieee80211_frame *wh; 1364 int paylen, totlen, xferlen, error; 1365 1366 data = &sc->tx_data[sc->data_idx]; 1367 desc = (struct uath_tx_desc *)(data->buf + sizeof (uint32_t)); 1368 1369 data->ni = ni; 1370 1371 #if NBPFILTER > 0 1372 if (sc->sc_drvbpf != NULL) { 1373 struct mbuf mb; 1374 struct uath_tx_radiotap_header *tap = &sc->sc_txtap; 1375 1376 tap->wt_flags = 0; 1377 tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq); 1378 tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags); 1379 1380 M_DUP_PKTHDR(&mb, m0); 1381 mb.m_data = (caddr_t)tap; 1382 mb.m_len = sc->sc_txtap_len; 1383 mb.m_next = m0; 1384 mb.m_pkthdr.len += mb.m_len; 1385 bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT); 1386 } 1387 #endif 1388 1389 paylen = m0->m_pkthdr.len; 1390 xferlen = sizeof (uint32_t) + sizeof (struct uath_tx_desc) + paylen; 1391 1392 wh = mtod(m0, struct ieee80211_frame *); 1393 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1394 uint8_t *frm = (uint8_t *)(desc + 1); 1395 uint32_t iv; 1396 1397 /* h/w WEP: it's up to the host to fill the IV field */ 1398 bcopy(wh, frm, sizeof (struct ieee80211_frame)); 1399 frm += sizeof (struct ieee80211_frame); 1400 1401 /* insert IV: code copied from net80211 */ 1402 iv = (ic->ic_iv != 0) ? ic->ic_iv : arc4random(); 1403 if (iv >= 0x03ff00 && (iv & 0xf8ff00) == 0x00ff00) 1404 iv += 0x000100; 1405 ic->ic_iv = iv + 1; 1406 1407 *frm++ = iv & 0xff; 1408 *frm++ = (iv >> 8) & 0xff; 1409 *frm++ = (iv >> 16) & 0xff; 1410 *frm++ = ic->ic_wep_txkey << 6; 1411 1412 m_copydata(m0, sizeof (struct ieee80211_frame), 1413 m0->m_pkthdr.len - sizeof (struct ieee80211_frame), frm); 1414 1415 paylen += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN; 1416 xferlen += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN; 1417 totlen = xferlen + IEEE80211_WEP_CRCLEN; 1418 } else { 1419 m_copydata(m0, 0, m0->m_pkthdr.len, (uint8_t *)(desc + 1)); 1420 totlen = xferlen; 1421 } 1422 1423 /* fill Tx descriptor */ 1424 *(uint32_t *)data->buf = UATH_MAKECTL(1, xferlen - sizeof (uint32_t)); 1425 1426 desc->len = htobe32(totlen); 1427 desc->priv = sc->data_idx; /* don't care about endianness */ 1428 desc->paylen = htobe32(paylen); 1429 desc->type = htobe32(UATH_TX_DATA); 1430 desc->flags = htobe32(0); 1431 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1432 desc->dest = htobe32(UATH_ID_BROADCAST); 1433 desc->magic = htobe32(3); 1434 } else { 1435 desc->dest = htobe32(UATH_ID_BSS); 1436 desc->magic = htobe32(1); 1437 } 1438 1439 m_freem(m0); /* mbuf is no longer needed */ 1440 1441 #ifdef UATH_DEBUG 1442 if (uath_debug >= 6) { 1443 printf("sending frame index=%u len=%d xferlen=%d", 1444 sc->data_idx, paylen, xferlen); 1445 uath_dump_cmd(data->buf, xferlen, '+'); 1446 } 1447 #endif 1448 usbd_setup_xfer(data->xfer, sc->data_tx_pipe, data, data->buf, xferlen, 1449 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, UATH_DATA_TIMEOUT, 1450 uath_data_txeof); 1451 error = usbd_transfer(data->xfer); 1452 if (error != USBD_IN_PROGRESS && error != 0) { 1453 ic->ic_if.if_oerrors++; 1454 return error; 1455 } 1456 sc->data_idx = (sc->data_idx + 1) % UATH_TX_DATA_LIST_COUNT; 1457 sc->tx_queued++; 1458 1459 return 0; 1460 } 1461 1462 Static void 1463 uath_start(struct ifnet *ifp) 1464 { 1465 struct uath_softc *sc = ifp->if_softc; 1466 struct ieee80211com *ic = &sc->sc_ic; 1467 struct ieee80211_node *ni; 1468 struct mbuf *m0; 1469 1470 /* 1471 * net80211 may still try to send management frames even if the 1472 * IFF_RUNNING flag is not set... 1473 */ 1474 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 1475 return; 1476 1477 for (;;) { 1478 IF_POLL(&ic->ic_mgtq, m0); 1479 if (m0 != NULL) { 1480 if (sc->tx_queued >= UATH_TX_DATA_LIST_COUNT) { 1481 ifp->if_flags |= IFF_OACTIVE; 1482 break; 1483 } 1484 IF_DEQUEUE(&ic->ic_mgtq, m0); 1485 1486 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif; 1487 m0->m_pkthdr.rcvif = NULL; 1488 #if NBPFILTER > 0 1489 if (ic->ic_rawbpf != NULL) 1490 bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT); 1491 #endif 1492 if (uath_tx_data(sc, m0, ni) != 0) 1493 break; 1494 } else { 1495 if (ic->ic_state != IEEE80211_S_RUN) 1496 break; 1497 IFQ_POLL(&ifp->if_snd, m0); 1498 if (m0 == NULL) 1499 break; 1500 if (sc->tx_queued >= UATH_TX_DATA_LIST_COUNT) { 1501 ifp->if_flags |= IFF_OACTIVE; 1502 break; 1503 } 1504 IFQ_DEQUEUE(&ifp->if_snd, m0); 1505 #if NBPFILTER > 0 1506 if (ifp->if_bpf != NULL) 1507 bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT); 1508 #endif 1509 m0 = ieee80211_encap(ifp, m0, &ni); 1510 if (m0 == NULL) 1511 continue; 1512 #if NBPFILTER > 0 1513 if (ic->ic_rawbpf != NULL) 1514 bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT); 1515 #endif 1516 if (uath_tx_data(sc, m0, ni) != 0) { 1517 if (ni != NULL) 1518 ieee80211_release_node(ic, ni); 1519 ifp->if_oerrors++; 1520 break; 1521 } 1522 } 1523 1524 sc->sc_tx_timer = 5; 1525 ifp->if_timer = 1; 1526 } 1527 } 1528 1529 Static void 1530 uath_watchdog(struct ifnet *ifp) 1531 { 1532 struct uath_softc *sc = ifp->if_softc; 1533 1534 ifp->if_timer = 0; 1535 1536 if (sc->sc_tx_timer > 0) { 1537 if (--sc->sc_tx_timer == 0) { 1538 printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev)); 1539 /*uath_init(ifp); XXX needs a process context! */ 1540 ifp->if_oerrors++; 1541 return; 1542 } 1543 ifp->if_timer = 1; 1544 } 1545 1546 ieee80211_watchdog(ifp); 1547 } 1548 1549 Static int 1550 uath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1551 { 1552 struct uath_softc *sc = ifp->if_softc; 1553 struct ieee80211com *ic = &sc->sc_ic; 1554 struct ifaddr *ifa; 1555 struct ifreq *ifr; 1556 int s, error = 0; 1557 1558 s = splnet(); 1559 1560 switch (cmd) { 1561 case SIOCSIFADDR: 1562 ifa = (struct ifaddr *)data; 1563 ifp->if_flags |= IFF_UP; 1564 #ifdef INET 1565 if (ifa->ifa_addr->sa_family == AF_INET) 1566 arp_ifinit(&ic->ic_ac, ifa); 1567 #endif 1568 /* FALLTHROUGH */ 1569 case SIOCSIFFLAGS: 1570 if (ifp->if_flags & IFF_UP) { 1571 if (!(ifp->if_flags & IFF_RUNNING)) 1572 uath_init(ifp); 1573 } else { 1574 if (ifp->if_flags & IFF_RUNNING) 1575 uath_stop(ifp, 1); 1576 } 1577 break; 1578 1579 case SIOCADDMULTI: 1580 case SIOCDELMULTI: 1581 ifr = (struct ifreq *)data; 1582 error = (cmd == SIOCADDMULTI) ? 1583 ether_addmulti(ifr, &ic->ic_ac) : 1584 ether_delmulti(ifr, &ic->ic_ac); 1585 if (error == ENETRESET) 1586 error = 0; 1587 break; 1588 1589 default: 1590 error = ieee80211_ioctl(ifp, cmd, data); 1591 } 1592 1593 if (error == ENETRESET) { 1594 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 1595 (IFF_UP | IFF_RUNNING)) 1596 uath_init(ifp); 1597 error = 0; 1598 } 1599 1600 splx(s); 1601 1602 return error; 1603 } 1604 1605 Static int 1606 uath_query_eeprom(struct uath_softc *sc) 1607 { 1608 uint32_t tmp; 1609 int error; 1610 1611 /* retrieve MAC address */ 1612 error = uath_read_eeprom(sc, UATH_EEPROM_MACADDR, sc->sc_ic.ic_myaddr); 1613 if (error != 0) { 1614 printf("%s: could not read MAC address\n", 1615 USBDEVNAME(sc->sc_dev)); 1616 return error; 1617 } 1618 1619 /* retrieve the maximum frame size that the hardware can receive */ 1620 error = uath_read_eeprom(sc, UATH_EEPROM_RXBUFSZ, &tmp); 1621 if (error != 0) { 1622 printf("%s: could not read maximum Rx buffer size\n", 1623 USBDEVNAME(sc->sc_dev)); 1624 return error; 1625 } 1626 sc->rxbufsz = betoh32(tmp) & 0xfff; 1627 DPRINTF(("maximum Rx buffer size %d\n", sc->rxbufsz)); 1628 return 0; 1629 } 1630 1631 Static int 1632 uath_reset(struct uath_softc *sc) 1633 { 1634 struct uath_cmd_setup setup; 1635 uint32_t reg, val; 1636 int s, error; 1637 1638 /* init device with some voodoo incantations.. */ 1639 setup.magic1 = htobe32(1); 1640 setup.magic2 = htobe32(5); 1641 setup.magic3 = htobe32(200); 1642 setup.magic4 = htobe32(27); 1643 s = splusb(); 1644 error = uath_cmd_write(sc, UATH_CMD_SETUP, &setup, sizeof setup, 1645 UATH_CMD_FLAG_ASYNC); 1646 /* ..and wait until firmware notifies us that it is ready */ 1647 if (error == 0) 1648 error = tsleep(UATH_COND_INIT(sc), PCATCH, "uathinit", 5 * hz); 1649 splx(s); 1650 if (error != 0) 1651 return error; 1652 1653 /* read PHY registers */ 1654 for (reg = 0x09; reg <= 0x24; reg++) { 1655 if (reg == 0x0b || reg == 0x0c) 1656 continue; 1657 if ((error = uath_read_reg(sc, reg, &val)) != 0) 1658 return error; 1659 DPRINTFN(2, ("reg 0x%02x=0x%08x\n", reg, val)); 1660 } 1661 return error; 1662 } 1663 1664 Static int 1665 uath_reset_tx_queues(struct uath_softc *sc) 1666 { 1667 int ac, error; 1668 1669 for (ac = 0; ac < 4; ac++) { 1670 const uint32_t qid = htobe32(UATH_AC_TO_QID(ac)); 1671 1672 DPRINTF(("resetting Tx queue %d\n", UATH_AC_TO_QID(ac))); 1673 error = uath_cmd_write(sc, UATH_CMD_RESET_QUEUE, &qid, 1674 sizeof qid, 0); 1675 if (error != 0) 1676 break; 1677 } 1678 return error; 1679 } 1680 1681 Static int 1682 uath_wme_init(struct uath_softc *sc) 1683 { 1684 struct uath_qinfo qinfo; 1685 int ac, error; 1686 static const struct uath_wme_settings uath_wme_11g[4] = { 1687 { 7, 4, 10, 0, 0 }, /* Background */ 1688 { 3, 4, 10, 0, 0 }, /* Best-Effort */ 1689 { 3, 3, 4, 26, 0 }, /* Video */ 1690 { 2, 2, 3, 47, 0 } /* Voice */ 1691 }; 1692 1693 bzero(&qinfo, sizeof qinfo); 1694 qinfo.size = htobe32(32); 1695 qinfo.magic1 = htobe32(1); /* XXX ack policy? */ 1696 qinfo.magic2 = htobe32(1); 1697 for (ac = 0; ac < 4; ac++) { 1698 qinfo.qid = htobe32(UATH_AC_TO_QID(ac)); 1699 qinfo.ac = htobe32(ac); 1700 qinfo.aifsn = htobe32(uath_wme_11g[ac].aifsn); 1701 qinfo.logcwmin = htobe32(uath_wme_11g[ac].logcwmin); 1702 qinfo.logcwmax = htobe32(uath_wme_11g[ac].logcwmax); 1703 qinfo.txop = htobe32(UATH_TXOP_TO_US( 1704 uath_wme_11g[ac].txop)); 1705 qinfo.acm = htobe32(uath_wme_11g[ac].acm); 1706 1707 DPRINTF(("setting up Tx queue %d\n", UATH_AC_TO_QID(ac))); 1708 error = uath_cmd_write(sc, UATH_CMD_SET_QUEUE, &qinfo, 1709 sizeof qinfo, 0); 1710 if (error != 0) 1711 break; 1712 } 1713 return error; 1714 } 1715 1716 Static int 1717 uath_set_chan(struct uath_softc *sc, struct ieee80211_channel *c) 1718 { 1719 struct uath_set_chan chan; 1720 1721 bzero(&chan, sizeof chan); 1722 chan.flags = htobe32(0x1400); 1723 chan.freq = htobe32(c->ic_freq); 1724 chan.magic1 = htobe32(20); 1725 chan.magic2 = htobe32(50); 1726 chan.magic3 = htobe32(1); 1727 1728 DPRINTF(("switching to channel %d\n", 1729 ieee80211_chan2ieee(&sc->sc_ic, c))); 1730 return uath_cmd_write(sc, UATH_CMD_SET_CHAN, &chan, sizeof chan, 0); 1731 } 1732 1733 Static int 1734 uath_set_key(struct uath_softc *sc, const struct ieee80211_wepkey *wk, 1735 int index) 1736 { 1737 struct uath_cmd_crypto crypto; 1738 int i; 1739 1740 bzero(&crypto, sizeof crypto); 1741 crypto.keyidx = htobe32(index); 1742 crypto.magic1 = htobe32(1); 1743 crypto.size = htobe32(368); 1744 crypto.mask = htobe32(0xffff); 1745 crypto.flags = htobe32(0x80000068); 1746 if (index != UATH_DEFAULT_KEY) 1747 crypto.flags |= htobe32(index << 16); 1748 memset(crypto.magic2, 0xff, sizeof crypto.magic2); 1749 1750 /* 1751 * Each byte of the key must be XOR'ed with 10101010 before being 1752 * transmitted to the firmware. 1753 */ 1754 for (i = 0; i < wk->wk_len; i++) 1755 crypto.key[i] = wk->wk_key[i] ^ 0xaa; 1756 1757 DPRINTF(("setting crypto key index=%d len=%d\n", index, wk->wk_len)); 1758 return uath_cmd_write(sc, UATH_CMD_CRYPTO, &crypto, sizeof crypto, 0); 1759 } 1760 1761 Static int 1762 uath_set_keys(struct uath_softc *sc) 1763 { 1764 const struct ieee80211com *ic = &sc->sc_ic; 1765 int i, error; 1766 1767 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1768 const struct ieee80211_wepkey *wk = &ic->ic_nw_keys[i]; 1769 1770 if (wk->wk_len > 0 && 1771 (error = uath_set_key(sc, wk, i)) != 0) 1772 return error; 1773 } 1774 return uath_set_key(sc, &ic->ic_nw_keys[ic->ic_wep_txkey], 1775 UATH_DEFAULT_KEY); 1776 } 1777 1778 Static int 1779 uath_set_rates(struct uath_softc *sc, const struct ieee80211_rateset *rs) 1780 { 1781 struct uath_cmd_rates rates; 1782 1783 bzero(&rates, sizeof rates); 1784 rates.magic1 = htobe32(0x02); 1785 rates.size = htobe32(1 + sizeof rates.rates); 1786 rates.nrates = rs->rs_nrates; 1787 bcopy(rs->rs_rates, rates.rates, rs->rs_nrates); 1788 1789 DPRINTF(("setting supported rates nrates=%d\n", rs->rs_nrates)); 1790 return uath_cmd_write(sc, UATH_CMD_SET_RATES, &rates, sizeof rates, 0); 1791 } 1792 1793 Static int 1794 uath_set_rxfilter(struct uath_softc *sc, uint32_t filter, uint32_t flags) 1795 { 1796 struct uath_cmd_filter rxfilter; 1797 1798 rxfilter.filter = htobe32(filter); 1799 rxfilter.flags = htobe32(flags); 1800 1801 DPRINTF(("setting Rx filter=0x%x flags=0x%x\n", filter, flags)); 1802 return uath_cmd_write(sc, UATH_CMD_SET_FILTER, &rxfilter, 1803 sizeof rxfilter, 0); 1804 } 1805 1806 Static int 1807 uath_set_led(struct uath_softc *sc, int which, int on) 1808 { 1809 struct uath_cmd_led led; 1810 1811 led.which = htobe32(which); 1812 led.state = htobe32(on ? UATH_LED_ON : UATH_LED_OFF); 1813 1814 DPRINTFN(2, ("switching %s led %s\n", 1815 (which == UATH_LED_LINK) ? "link" : "activity", 1816 on ? "on" : "off")); 1817 return uath_cmd_write(sc, UATH_CMD_SET_LED, &led, sizeof led, 0); 1818 } 1819 1820 Static int 1821 uath_switch_channel(struct uath_softc *sc, struct ieee80211_channel *c) 1822 { 1823 uint32_t val; 1824 int error; 1825 1826 /* set radio frequency */ 1827 if ((error = uath_set_chan(sc, c)) != 0) { 1828 printf("%s: could not set channel\n", USBDEVNAME(sc->sc_dev)); 1829 return error; 1830 } 1831 1832 /* reset Tx rings */ 1833 if ((error = uath_reset_tx_queues(sc)) != 0) { 1834 printf("%s: could not reset Tx queues\n", 1835 USBDEVNAME(sc->sc_dev)); 1836 return error; 1837 } 1838 1839 /* set Tx rings WME properties */ 1840 if ((error = uath_wme_init(sc)) != 0) { 1841 printf("%s: could not init Tx queues\n", 1842 USBDEVNAME(sc->sc_dev)); 1843 return error; 1844 } 1845 1846 val = htobe32(0); 1847 error = uath_cmd_write(sc, UATH_CMD_SET_STATE, &val, sizeof val, 0); 1848 if (error != 0) { 1849 printf("%s: could not set state\n", USBDEVNAME(sc->sc_dev)); 1850 return error; 1851 } 1852 1853 return uath_tx_null(sc); 1854 } 1855 1856 Static int 1857 uath_init(struct ifnet *ifp) 1858 { 1859 struct uath_softc *sc = ifp->if_softc; 1860 struct ieee80211com *ic = &sc->sc_ic; 1861 struct uath_cmd_31 cmd31; 1862 uint32_t val; 1863 int i, error; 1864 1865 /* reset data and command rings */ 1866 sc->tx_queued = sc->data_idx = sc->cmd_idx = 0; 1867 1868 val = htobe32(0); 1869 (void)uath_cmd_write(sc, UATH_CMD_02, &val, sizeof val, 0); 1870 1871 /* set MAC address */ 1872 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl)); 1873 (void)uath_write_multi(sc, 0x13, ic->ic_myaddr, IEEE80211_ADDR_LEN); 1874 1875 (void)uath_write_reg(sc, 0x02, 0x00000001); 1876 (void)uath_write_reg(sc, 0x0e, 0x0000003f); 1877 (void)uath_write_reg(sc, 0x10, 0x00000001); 1878 (void)uath_write_reg(sc, 0x06, 0x0000001e); 1879 1880 /* 1881 * Queue Rx data xfers. 1882 */ 1883 for (i = 0; i < UATH_RX_DATA_LIST_COUNT; i++) { 1884 struct uath_rx_data *data = SLIST_FIRST(&sc->rx_freelist); 1885 1886 usbd_setup_xfer(data->xfer, sc->data_rx_pipe, data, data->buf, 1887 sc->rxbufsz, USBD_SHORT_XFER_OK | USBD_NO_COPY, 1888 USBD_NO_TIMEOUT, uath_data_rxeof); 1889 error = usbd_transfer(data->xfer); 1890 if (error != USBD_IN_PROGRESS && error != 0) { 1891 printf("%s: could not queue Rx transfer\n", 1892 USBDEVNAME(sc->sc_dev)); 1893 goto fail; 1894 } 1895 SLIST_REMOVE_HEAD(&sc->rx_freelist, next); 1896 } 1897 1898 error = uath_cmd_read(sc, UATH_CMD_07, 0, NULL, &val, 1899 UATH_CMD_FLAG_MAGIC); 1900 if (error != 0) { 1901 printf("%s: could not send read command 07h\n", 1902 USBDEVNAME(sc->sc_dev)); 1903 goto fail; 1904 } 1905 DPRINTF(("command 07h return code: %x\n", betoh32(val))); 1906 1907 /* set default channel */ 1908 ic->ic_bss->ni_chan = ic->ic_ibss_chan; 1909 if ((error = uath_set_chan(sc, ic->ic_bss->ni_chan)) != 0) { 1910 printf("%s: could not set channel\n", USBDEVNAME(sc->sc_dev)); 1911 goto fail; 1912 } 1913 1914 if ((error = uath_wme_init(sc)) != 0) { 1915 printf("%s: could not setup WME parameters\n", 1916 USBDEVNAME(sc->sc_dev)); 1917 goto fail; 1918 } 1919 1920 /* init MAC registers */ 1921 (void)uath_write_reg(sc, 0x19, 0x00000000); 1922 (void)uath_write_reg(sc, 0x1a, 0x0000003c); 1923 (void)uath_write_reg(sc, 0x1b, 0x0000003c); 1924 (void)uath_write_reg(sc, 0x1c, 0x00000000); 1925 (void)uath_write_reg(sc, 0x1e, 0x00000000); 1926 (void)uath_write_reg(sc, 0x1f, 0x00000003); 1927 (void)uath_write_reg(sc, 0x0c, 0x00000000); 1928 (void)uath_write_reg(sc, 0x0f, 0x00000002); 1929 (void)uath_write_reg(sc, 0x0a, 0x00000007); /* XXX retry? */ 1930 (void)uath_write_reg(sc, 0x09, ic->ic_rtsthreshold); 1931 1932 val = htobe32(4); 1933 (void)uath_cmd_write(sc, UATH_CMD_27, &val, sizeof val, 0); 1934 (void)uath_cmd_write(sc, UATH_CMD_27, &val, sizeof val, 0); 1935 (void)uath_cmd_write(sc, UATH_CMD_1B, NULL, 0, 0); 1936 1937 if ((error = uath_set_keys(sc)) != 0) { 1938 printf("%s: could not set crypto keys\n", 1939 USBDEVNAME(sc->sc_dev)); 1940 goto fail; 1941 } 1942 1943 /* enable Rx */ 1944 (void)uath_set_rxfilter(sc, 0x0000, 4); 1945 (void)uath_set_rxfilter(sc, 0x0817, 1); 1946 1947 cmd31.magic1 = htobe32(0xffffffff); 1948 cmd31.magic2 = htobe32(0xffffffff); 1949 (void)uath_cmd_write(sc, UATH_CMD_31, &cmd31, sizeof cmd31, 0); 1950 1951 ifp->if_flags &= ~IFF_OACTIVE; 1952 ifp->if_flags |= IFF_RUNNING; 1953 1954 if (ic->ic_opmode == IEEE80211_M_MONITOR) 1955 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1956 else 1957 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 1958 1959 return 0; 1960 1961 fail: uath_stop(ifp, 1); 1962 return error; 1963 } 1964 1965 Static void 1966 uath_stop(struct ifnet *ifp, int disable) 1967 { 1968 struct uath_softc *sc = ifp->if_softc; 1969 struct ieee80211com *ic = &sc->sc_ic; 1970 uint32_t val; 1971 int s; 1972 1973 s = splusb(); 1974 1975 sc->sc_tx_timer = 0; 1976 ifp->if_timer = 0; 1977 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1978 1979 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */ 1980 1981 val = htobe32(0); 1982 (void)uath_cmd_write(sc, UATH_CMD_SET_STATE, &val, sizeof val, 0); 1983 (void)uath_cmd_write(sc, UATH_CMD_RESET, NULL, 0, 0); 1984 1985 val = htobe32(0); 1986 (void)uath_cmd_write(sc, UATH_CMD_15, &val, sizeof val, 0); 1987 1988 #if 0 1989 (void)uath_cmd_read(sc, UATH_CMD_SHUTDOWN, NULL, 0, NULL, 1990 UATH_CMD_FLAG_MAGIC); 1991 #endif 1992 1993 /* abort any pending transfers */ 1994 usbd_abort_pipe(sc->data_tx_pipe); 1995 usbd_abort_pipe(sc->data_rx_pipe); 1996 usbd_abort_pipe(sc->cmd_tx_pipe); 1997 usbd_abort_pipe(sc->cmd_rx_pipe); 1998 1999 splx(s); 2000 } 2001 2002 /* 2003 * Load the MIPS R4000 microcode into the device. Once the image is loaded, 2004 * the device will detach itself from the bus and reattach later with a new 2005 * product Id (a la ezusb). XXX this could also be implemented in userland 2006 * through /dev/ugen. 2007 */ 2008 Static int 2009 uath_loadfirmware(struct uath_softc *sc, const u_char *fw, int len) 2010 { 2011 usbd_xfer_handle ctlxfer, txxfer, rxxfer; 2012 struct uath_fwblock *txblock, *rxblock; 2013 uint8_t *txdata; 2014 int error = 0; 2015 2016 if ((ctlxfer = usbd_alloc_xfer(sc->sc_udev)) == NULL) { 2017 printf("%s: could not allocate Tx control xfer\n", 2018 USBDEVNAME(sc->sc_dev)); 2019 error = USBD_NOMEM; 2020 goto fail1; 2021 } 2022 txblock = usbd_alloc_buffer(ctlxfer, sizeof (struct uath_fwblock)); 2023 if (txblock == NULL) { 2024 printf("%s: could not allocate Tx control block\n", 2025 USBDEVNAME(sc->sc_dev)); 2026 error = USBD_NOMEM; 2027 goto fail2; 2028 } 2029 2030 if ((txxfer = usbd_alloc_xfer(sc->sc_udev)) == NULL) { 2031 printf("%s: could not allocate Tx xfer\n", 2032 USBDEVNAME(sc->sc_dev)); 2033 error = USBD_NOMEM; 2034 goto fail2; 2035 } 2036 txdata = usbd_alloc_buffer(txxfer, UATH_MAX_FWBLOCK_SIZE); 2037 if (txdata == NULL) { 2038 printf("%s: could not allocate Tx buffer\n", 2039 USBDEVNAME(sc->sc_dev)); 2040 error = USBD_NOMEM; 2041 goto fail3; 2042 } 2043 2044 if ((rxxfer = usbd_alloc_xfer(sc->sc_udev)) == NULL) { 2045 printf("%s: could not allocate Rx control xfer\n", 2046 USBDEVNAME(sc->sc_dev)); 2047 error = USBD_NOMEM; 2048 goto fail3; 2049 } 2050 rxblock = usbd_alloc_buffer(rxxfer, sizeof (struct uath_fwblock)); 2051 if (rxblock == NULL) { 2052 printf("%s: could not allocate Rx control block\n", 2053 USBDEVNAME(sc->sc_dev)); 2054 error = USBD_NOMEM; 2055 goto fail4; 2056 } 2057 2058 bzero(txblock, sizeof (struct uath_fwblock)); 2059 txblock->flags = htobe32(UATH_WRITE_BLOCK); 2060 txblock->total = htobe32(len); 2061 2062 while (len > 0) { 2063 int mlen = min(len, UATH_MAX_FWBLOCK_SIZE); 2064 2065 txblock->remain = htobe32(len - mlen); 2066 txblock->len = htobe32(mlen); 2067 2068 DPRINTF(("sending firmware block: %d bytes remaining\n", 2069 len - mlen)); 2070 2071 /* send firmware block meta-data */ 2072 usbd_setup_xfer(ctlxfer, sc->cmd_tx_pipe, sc, txblock, 2073 sizeof (struct uath_fwblock), USBD_NO_COPY, 2074 UATH_CMD_TIMEOUT, NULL); 2075 if ((error = usbd_sync_transfer(ctlxfer)) != 0) { 2076 printf("%s: could not send firmware block info\n", 2077 USBDEVNAME(sc->sc_dev)); 2078 break; 2079 } 2080 2081 /* send firmware block data */ 2082 bcopy(fw, txdata, mlen); 2083 usbd_setup_xfer(txxfer, sc->data_tx_pipe, sc, txdata, mlen, 2084 USBD_NO_COPY, UATH_DATA_TIMEOUT, NULL); 2085 if ((error = usbd_sync_transfer(txxfer)) != 0) { 2086 printf("%s: could not send firmware block data\n", 2087 USBDEVNAME(sc->sc_dev)); 2088 break; 2089 } 2090 2091 /* wait for ack from firmware */ 2092 usbd_setup_xfer(rxxfer, sc->cmd_rx_pipe, sc, rxblock, 2093 sizeof (struct uath_fwblock), USBD_SHORT_XFER_OK | 2094 USBD_NO_COPY, UATH_CMD_TIMEOUT, NULL); 2095 if ((error = usbd_sync_transfer(rxxfer)) != 0) { 2096 printf("%s: could not read firmware answer\n", 2097 USBDEVNAME(sc->sc_dev)); 2098 break; 2099 } 2100 2101 DPRINTFN(2, ("rxblock flags=0x%x total=%d\n", 2102 betoh32(rxblock->flags), betoh32(rxblock->rxtotal))); 2103 fw += mlen; 2104 len -= mlen; 2105 } 2106 2107 fail4: usbd_free_xfer(rxxfer); 2108 fail3: usbd_free_xfer(txxfer); 2109 fail2: usbd_free_xfer(ctlxfer); 2110 fail1: return error; 2111 } 2112 2113 Static int 2114 uath_activate(device_ptr_t self, enum devact act) 2115 { 2116 switch (act) { 2117 case DVACT_ACTIVATE: 2118 break; 2119 2120 case DVACT_DEACTIVATE: 2121 /*if_deactivate(&sc->sc_ic.ic_if);*/ 2122 break; 2123 } 2124 return 0; 2125 } 2126