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