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