1 /* $OpenBSD: if_athn_usb.c,v 1.51 2018/09/06 11:50:54 jsg Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 Damien Bergamini <damien.bergamini@free.fr> 5 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* 21 * USB front-end for Atheros AR9271 and AR7010 chipsets. 22 */ 23 24 #include "bpfilter.h" 25 26 #include <sys/param.h> 27 #include <sys/sockio.h> 28 #include <sys/mbuf.h> 29 #include <sys/kernel.h> 30 #include <sys/socket.h> 31 #include <sys/systm.h> 32 #include <sys/timeout.h> 33 #include <sys/conf.h> 34 #include <sys/device.h> 35 #include <sys/endian.h> 36 37 #include <machine/bus.h> 38 #include <machine/intr.h> 39 40 #if NBPFILTER > 0 41 #include <net/bpf.h> 42 #endif 43 #include <net/if.h> 44 #include <net/if_dl.h> 45 #include <net/if_media.h> 46 47 #include <netinet/in.h> 48 #include <netinet/if_ether.h> 49 50 #include <net80211/ieee80211_var.h> 51 #include <net80211/ieee80211_amrr.h> 52 #include <net80211/ieee80211_mira.h> 53 #include <net80211/ieee80211_radiotap.h> 54 55 #include <dev/ic/athnreg.h> 56 #include <dev/ic/athnvar.h> 57 58 #include <dev/usb/usb.h> 59 #include <dev/usb/usbdi.h> 60 #include <dev/usb/usbdi_util.h> 61 #include <dev/usb/usbdevs.h> 62 63 #include <dev/usb/if_athn_usb.h> 64 65 static const struct athn_usb_type { 66 struct usb_devno devno; 67 u_int flags; 68 } athn_usb_devs[] = { 69 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_AR9280 }, 70 ATHN_USB_FLAG_AR7010 }, 71 {{ USB_VENDOR_ACTIONTEC, USB_PRODUCT_ACTIONTEC_AR9287 }, 72 ATHN_USB_FLAG_AR7010 }, 73 {{ USB_VENDOR_ATHEROS2, USB_PRODUCT_ATHEROS2_AR9271_1 }}, 74 {{ USB_VENDOR_ATHEROS2, USB_PRODUCT_ATHEROS2_AR9271_2 }}, 75 {{ USB_VENDOR_ATHEROS2, USB_PRODUCT_ATHEROS2_AR9271_3 }}, 76 {{ USB_VENDOR_ATHEROS2, USB_PRODUCT_ATHEROS2_AR9280 }, 77 ATHN_USB_FLAG_AR7010 }, 78 {{ USB_VENDOR_ATHEROS2, USB_PRODUCT_ATHEROS2_AR9287 }, 79 ATHN_USB_FLAG_AR7010 }, 80 {{ USB_VENDOR_AZUREWAVE, USB_PRODUCT_AZUREWAVE_AR9271_1 }}, 81 {{ USB_VENDOR_AZUREWAVE, USB_PRODUCT_AZUREWAVE_AR9271_2 }}, 82 {{ USB_VENDOR_AZUREWAVE, USB_PRODUCT_AZUREWAVE_AR9271_3 }}, 83 {{ USB_VENDOR_AZUREWAVE, USB_PRODUCT_AZUREWAVE_AR9271_4 }}, 84 {{ USB_VENDOR_AZUREWAVE, USB_PRODUCT_AZUREWAVE_AR9271_5 }}, 85 {{ USB_VENDOR_AZUREWAVE, USB_PRODUCT_AZUREWAVE_AR9271_6 }}, 86 {{ USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_AR9271 }}, 87 {{ USB_VENDOR_LITEON, USB_PRODUCT_LITEON_AR9271 }}, 88 {{ USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_WNA1100 }}, 89 {{ USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_WNDA3200 }, 90 ATHN_USB_FLAG_AR7010 }, 91 {{ USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_N5HBZ0000055 }, 92 ATHN_USB_FLAG_AR7010 }, 93 {{ USB_VENDOR_VIA, USB_PRODUCT_VIA_AR9271 }} 94 }; 95 #define athn_usb_lookup(v, p) \ 96 ((const struct athn_usb_type *)usb_lookup(athn_usb_devs, v, p)) 97 98 int athn_usb_match(struct device *, void *, void *); 99 void athn_usb_attach(struct device *, struct device *, void *); 100 int athn_usb_detach(struct device *, int); 101 void athn_usb_attachhook(struct device *); 102 int athn_usb_open_pipes(struct athn_usb_softc *); 103 void athn_usb_close_pipes(struct athn_usb_softc *); 104 int athn_usb_alloc_rx_list(struct athn_usb_softc *); 105 void athn_usb_free_rx_list(struct athn_usb_softc *); 106 int athn_usb_alloc_tx_list(struct athn_usb_softc *); 107 void athn_usb_free_tx_list(struct athn_usb_softc *); 108 int athn_usb_alloc_tx_cmd(struct athn_usb_softc *); 109 void athn_usb_free_tx_cmd(struct athn_usb_softc *); 110 void athn_usb_task(void *); 111 void athn_usb_do_async(struct athn_usb_softc *, 112 void (*)(struct athn_usb_softc *, void *), void *, int); 113 void athn_usb_wait_async(struct athn_usb_softc *); 114 int athn_usb_load_firmware(struct athn_usb_softc *); 115 int athn_usb_htc_msg(struct athn_usb_softc *, uint16_t, void *, 116 int); 117 int athn_usb_htc_setup(struct athn_usb_softc *); 118 int athn_usb_htc_connect_svc(struct athn_usb_softc *, uint16_t, 119 uint8_t, uint8_t, uint8_t *); 120 int athn_usb_wmi_xcmd(struct athn_usb_softc *, uint16_t, void *, 121 int, void *); 122 int athn_usb_read_rom(struct athn_softc *); 123 uint32_t athn_usb_read(struct athn_softc *, uint32_t); 124 void athn_usb_write(struct athn_softc *, uint32_t, uint32_t); 125 void athn_usb_write_barrier(struct athn_softc *); 126 int athn_usb_media_change(struct ifnet *); 127 void athn_usb_next_scan(void *); 128 int athn_usb_newstate(struct ieee80211com *, enum ieee80211_state, 129 int); 130 void athn_usb_newstate_cb(struct athn_usb_softc *, void *); 131 void athn_usb_newassoc(struct ieee80211com *, 132 struct ieee80211_node *, int); 133 void athn_usb_newassoc_cb(struct athn_usb_softc *, void *); 134 struct ieee80211_node *athn_usb_node_alloc(struct ieee80211com *); 135 void athn_usb_count_active_sta(void *, struct ieee80211_node *); 136 void athn_usb_newauth_cb(struct athn_usb_softc *, void *); 137 int athn_usb_newauth(struct ieee80211com *, 138 struct ieee80211_node *, int, uint16_t); 139 void athn_usb_node_free(struct ieee80211com *, 140 struct ieee80211_node *); 141 void athn_usb_node_free_cb(struct athn_usb_softc *, void *); 142 int athn_usb_ampdu_tx_start(struct ieee80211com *, 143 struct ieee80211_node *, uint8_t); 144 void athn_usb_ampdu_tx_start_cb(struct athn_usb_softc *, void *); 145 void athn_usb_ampdu_tx_stop(struct ieee80211com *, 146 struct ieee80211_node *, uint8_t); 147 void athn_usb_ampdu_tx_stop_cb(struct athn_usb_softc *, void *); 148 void athn_usb_clean_nodes(void *, struct ieee80211_node *); 149 int athn_usb_create_node(struct athn_usb_softc *, 150 struct ieee80211_node *); 151 int athn_usb_node_set_rates(struct athn_usb_softc *, 152 struct ieee80211_node *); 153 int athn_usb_remove_node(struct athn_usb_softc *, 154 struct ieee80211_node *); 155 void athn_usb_rx_enable(struct athn_softc *); 156 int athn_set_chan(struct athn_softc *, struct ieee80211_channel *, 157 struct ieee80211_channel *); 158 int athn_usb_switch_chan(struct athn_softc *, 159 struct ieee80211_channel *, struct ieee80211_channel *); 160 void athn_usb_updateedca(struct ieee80211com *); 161 void athn_usb_updateedca_cb(struct athn_usb_softc *, void *); 162 void athn_usb_updateslot(struct ieee80211com *); 163 void athn_usb_updateslot_cb(struct athn_usb_softc *, void *); 164 int athn_usb_set_key(struct ieee80211com *, 165 struct ieee80211_node *, struct ieee80211_key *); 166 void athn_usb_set_key_cb(struct athn_usb_softc *, void *); 167 void athn_usb_delete_key(struct ieee80211com *, 168 struct ieee80211_node *, struct ieee80211_key *); 169 void athn_usb_delete_key_cb(struct athn_usb_softc *, void *); 170 void athn_usb_bcneof(struct usbd_xfer *, void *, 171 usbd_status); 172 void athn_usb_swba(struct athn_usb_softc *); 173 void athn_usb_tx_status(void *, struct ieee80211_node *); 174 void athn_usb_rx_wmi_ctrl(struct athn_usb_softc *, uint8_t *, int); 175 void athn_usb_intr(struct usbd_xfer *, void *, 176 usbd_status); 177 void athn_usb_rx_radiotap(struct athn_softc *, struct mbuf *, 178 struct ar_rx_status *); 179 void athn_usb_rx_frame(struct athn_usb_softc *, struct mbuf *); 180 void athn_usb_rxeof(struct usbd_xfer *, void *, 181 usbd_status); 182 void athn_usb_txeof(struct usbd_xfer *, void *, 183 usbd_status); 184 int athn_usb_tx(struct athn_softc *, struct mbuf *, 185 struct ieee80211_node *); 186 void athn_usb_start(struct ifnet *); 187 void athn_usb_watchdog(struct ifnet *); 188 int athn_usb_ioctl(struct ifnet *, u_long, caddr_t); 189 int athn_usb_init(struct ifnet *); 190 void athn_usb_stop(struct ifnet *); 191 void ar9271_load_ani(struct athn_softc *); 192 193 /* Shortcut. */ 194 #define athn_usb_wmi_cmd(sc, cmd_id) \ 195 athn_usb_wmi_xcmd(sc, cmd_id, NULL, 0, NULL) 196 197 /* Extern functions. */ 198 void athn_led_init(struct athn_softc *); 199 void athn_set_led(struct athn_softc *, int); 200 void athn_btcoex_init(struct athn_softc *); 201 void athn_set_rxfilter(struct athn_softc *, uint32_t); 202 int athn_reset(struct athn_softc *, int); 203 void athn_init_pll(struct athn_softc *, 204 const struct ieee80211_channel *); 205 int athn_set_power_awake(struct athn_softc *); 206 void athn_set_power_sleep(struct athn_softc *); 207 void athn_reset_key(struct athn_softc *, int); 208 int athn_set_key(struct ieee80211com *, struct ieee80211_node *, 209 struct ieee80211_key *); 210 void athn_delete_key(struct ieee80211com *, struct ieee80211_node *, 211 struct ieee80211_key *); 212 void athn_rx_start(struct athn_softc *); 213 void athn_set_sta_timers(struct athn_softc *); 214 void athn_set_hostap_timers(struct athn_softc *); 215 void athn_set_opmode(struct athn_softc *); 216 void athn_set_bss(struct athn_softc *, struct ieee80211_node *); 217 int athn_hw_reset(struct athn_softc *, struct ieee80211_channel *, 218 struct ieee80211_channel *, int); 219 void athn_updateedca(struct ieee80211com *); 220 void athn_updateslot(struct ieee80211com *); 221 222 const struct cfattach athn_usb_ca = { 223 sizeof(struct athn_usb_softc), 224 athn_usb_match, 225 athn_usb_attach, 226 athn_usb_detach 227 }; 228 229 int 230 athn_usb_match(struct device *parent, void *match, void *aux) 231 { 232 struct usb_attach_arg *uaa = aux; 233 234 if (uaa->iface == NULL || uaa->configno != 1) 235 return (UMATCH_NONE); 236 237 return ((athn_usb_lookup(uaa->vendor, uaa->product) != NULL) ? 238 UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE); 239 } 240 241 void 242 athn_usb_attach(struct device *parent, struct device *self, void *aux) 243 { 244 struct athn_usb_softc *usc = (struct athn_usb_softc *)self; 245 struct athn_softc *sc = &usc->sc_sc; 246 struct usb_attach_arg *uaa = aux; 247 248 usc->sc_udev = uaa->device; 249 usc->sc_iface = uaa->iface; 250 251 usc->flags = athn_usb_lookup(uaa->vendor, uaa->product)->flags; 252 sc->flags |= ATHN_FLAG_USB; 253 #ifdef notyet 254 /* Check if it is a combo WiFi+Bluetooth (WB193) device. */ 255 if (strncmp(product, "wb193", 5) == 0) 256 sc->flags |= ATHN_FLAG_BTCOEX3WIRE; 257 #endif 258 259 sc->ops.read = athn_usb_read; 260 sc->ops.write = athn_usb_write; 261 sc->ops.write_barrier = athn_usb_write_barrier; 262 263 usb_init_task(&usc->sc_task, athn_usb_task, sc, USB_TASK_TYPE_GENERIC); 264 265 if (athn_usb_open_pipes(usc) != 0) 266 return; 267 268 /* Allocate xfer for firmware commands. */ 269 if (athn_usb_alloc_tx_cmd(usc) != 0) 270 return; 271 272 config_mountroot(self, athn_usb_attachhook); 273 } 274 275 int 276 athn_usb_detach(struct device *self, int flags) 277 { 278 struct athn_usb_softc *usc = (struct athn_usb_softc *)self; 279 struct athn_softc *sc = &usc->sc_sc; 280 281 if (usc->sc_athn_attached) 282 athn_detach(sc); 283 284 /* Wait for all async commands to complete. */ 285 athn_usb_wait_async(usc); 286 287 usbd_ref_wait(usc->sc_udev); 288 289 /* Abort and close Tx/Rx pipes. */ 290 athn_usb_close_pipes(usc); 291 292 /* Free Tx/Rx buffers. */ 293 athn_usb_free_tx_cmd(usc); 294 athn_usb_free_tx_list(usc); 295 athn_usb_free_rx_list(usc); 296 297 return (0); 298 } 299 300 void 301 athn_usb_attachhook(struct device *self) 302 { 303 struct athn_usb_softc *usc = (struct athn_usb_softc *)self; 304 struct athn_softc *sc = &usc->sc_sc; 305 struct athn_ops *ops = &sc->ops; 306 struct ieee80211com *ic = &sc->sc_ic; 307 struct ifnet *ifp = &ic->ic_if; 308 int s, i, error; 309 310 /* Load firmware. */ 311 error = athn_usb_load_firmware(usc); 312 if (error != 0) { 313 printf("%s: could not load firmware\n", sc->sc_dev.dv_xname); 314 return; 315 } 316 317 /* Setup the host transport communication interface. */ 318 error = athn_usb_htc_setup(usc); 319 if (error != 0) 320 return; 321 322 /* We're now ready to attach the bus agnostic driver. */ 323 s = splnet(); 324 error = athn_attach(sc); 325 if (error != 0) { 326 splx(s); 327 return; 328 } 329 usc->sc_athn_attached = 1; 330 /* Override some operations for USB. */ 331 ifp->if_ioctl = athn_usb_ioctl; 332 ifp->if_start = athn_usb_start; 333 ifp->if_watchdog = athn_usb_watchdog; 334 ic->ic_node_alloc = athn_usb_node_alloc; 335 ic->ic_newauth = athn_usb_newauth; 336 ic->ic_newassoc = athn_usb_newassoc; 337 #ifndef IEEE80211_STA_ONLY 338 usc->sc_node_free = ic->ic_node_free; 339 ic->ic_node_free = athn_usb_node_free; 340 #endif 341 ic->ic_updateslot = athn_usb_updateslot; 342 ic->ic_updateedca = athn_usb_updateedca; 343 #ifdef notyet 344 ic->ic_set_key = athn_usb_set_key; 345 ic->ic_delete_key = athn_usb_delete_key; 346 ic->ic_ampdu_tx_start = athn_usb_ampdu_tx_start; 347 ic->ic_ampdu_tx_stop = athn_usb_ampdu_tx_stop; 348 #endif 349 ic->ic_newstate = athn_usb_newstate; 350 ic->ic_media.ifm_change = athn_usb_media_change; 351 timeout_set(&sc->scan_to, athn_usb_next_scan, usc); 352 353 ops->rx_enable = athn_usb_rx_enable; 354 splx(s); 355 356 /* Reset HW key cache entries. */ 357 for (i = 0; i < sc->kc_entries; i++) 358 athn_reset_key(sc, i); 359 360 ops->enable_antenna_diversity(sc); 361 362 #ifdef ATHN_BT_COEXISTENCE 363 /* Configure bluetooth coexistence for combo chips. */ 364 if (sc->flags & ATHN_FLAG_BTCOEX) 365 athn_btcoex_init(sc); 366 #endif 367 /* Configure LED. */ 368 athn_led_init(sc); 369 } 370 371 int 372 athn_usb_open_pipes(struct athn_usb_softc *usc) 373 { 374 usb_endpoint_descriptor_t *ed; 375 int isize, error; 376 377 error = usbd_open_pipe(usc->sc_iface, AR_PIPE_TX_DATA, 0, 378 &usc->tx_data_pipe); 379 if (error != 0) { 380 printf("%s: could not open Tx bulk pipe\n", 381 usc->usb_dev.dv_xname); 382 goto fail; 383 } 384 385 error = usbd_open_pipe(usc->sc_iface, AR_PIPE_RX_DATA, 0, 386 &usc->rx_data_pipe); 387 if (error != 0) { 388 printf("%s: could not open Rx bulk pipe\n", 389 usc->usb_dev.dv_xname); 390 goto fail; 391 } 392 393 ed = usbd_get_endpoint_descriptor(usc->sc_iface, AR_PIPE_RX_INTR); 394 if (ed == NULL) { 395 printf("%s: could not retrieve Rx intr pipe descriptor\n", 396 usc->usb_dev.dv_xname); 397 goto fail; 398 } 399 isize = UGETW(ed->wMaxPacketSize); 400 if (isize == 0) { 401 printf("%s: invalid Rx intr pipe descriptor\n", 402 usc->usb_dev.dv_xname); 403 goto fail; 404 } 405 usc->ibuf = malloc(isize, M_USBDEV, M_NOWAIT); 406 if (usc->ibuf == NULL) { 407 printf("%s: could not allocate Rx intr buffer\n", 408 usc->usb_dev.dv_xname); 409 goto fail; 410 } 411 usc->ibuflen = isize; 412 error = usbd_open_pipe_intr(usc->sc_iface, AR_PIPE_RX_INTR, 413 USBD_SHORT_XFER_OK, &usc->rx_intr_pipe, usc, usc->ibuf, isize, 414 athn_usb_intr, USBD_DEFAULT_INTERVAL); 415 if (error != 0) { 416 printf("%s: could not open Rx intr pipe\n", 417 usc->usb_dev.dv_xname); 418 goto fail; 419 } 420 421 error = usbd_open_pipe(usc->sc_iface, AR_PIPE_TX_INTR, 0, 422 &usc->tx_intr_pipe); 423 if (error != 0) { 424 printf("%s: could not open Tx intr pipe\n", 425 usc->usb_dev.dv_xname); 426 goto fail; 427 } 428 fail: 429 if (error != 0) 430 athn_usb_close_pipes(usc); 431 return (error); 432 } 433 434 void 435 athn_usb_close_pipes(struct athn_usb_softc *usc) 436 { 437 if (usc->tx_data_pipe != NULL) { 438 usbd_close_pipe(usc->tx_data_pipe); 439 usc->tx_data_pipe = NULL; 440 } 441 if (usc->rx_data_pipe != NULL) { 442 usbd_close_pipe(usc->rx_data_pipe); 443 usc->rx_data_pipe = NULL; 444 } 445 if (usc->tx_intr_pipe != NULL) { 446 usbd_close_pipe(usc->tx_intr_pipe); 447 usc->tx_intr_pipe = NULL; 448 } 449 if (usc->rx_intr_pipe != NULL) { 450 usbd_close_pipe(usc->rx_intr_pipe); 451 usc->rx_intr_pipe = NULL; 452 } 453 if (usc->ibuf != NULL) { 454 free(usc->ibuf, M_USBDEV, usc->ibuflen); 455 usc->ibuf = NULL; 456 } 457 } 458 459 int 460 athn_usb_alloc_rx_list(struct athn_usb_softc *usc) 461 { 462 struct athn_usb_rx_data *data; 463 int i, error = 0; 464 465 for (i = 0; i < ATHN_USB_RX_LIST_COUNT; i++) { 466 data = &usc->rx_data[i]; 467 468 data->sc = usc; /* Backpointer for callbacks. */ 469 470 data->xfer = usbd_alloc_xfer(usc->sc_udev); 471 if (data->xfer == NULL) { 472 printf("%s: could not allocate xfer\n", 473 usc->usb_dev.dv_xname); 474 error = ENOMEM; 475 break; 476 } 477 data->buf = usbd_alloc_buffer(data->xfer, ATHN_USB_RXBUFSZ); 478 if (data->buf == NULL) { 479 printf("%s: could not allocate xfer buffer\n", 480 usc->usb_dev.dv_xname); 481 error = ENOMEM; 482 break; 483 } 484 } 485 if (error != 0) 486 athn_usb_free_rx_list(usc); 487 return (error); 488 } 489 490 void 491 athn_usb_free_rx_list(struct athn_usb_softc *usc) 492 { 493 int i; 494 495 /* NB: Caller must abort pipe first. */ 496 for (i = 0; i < ATHN_USB_RX_LIST_COUNT; i++) { 497 if (usc->rx_data[i].xfer != NULL) 498 usbd_free_xfer(usc->rx_data[i].xfer); 499 usc->rx_data[i].xfer = NULL; 500 } 501 } 502 503 int 504 athn_usb_alloc_tx_list(struct athn_usb_softc *usc) 505 { 506 struct athn_usb_tx_data *data; 507 int i, error = 0; 508 509 TAILQ_INIT(&usc->tx_free_list); 510 for (i = 0; i < ATHN_USB_TX_LIST_COUNT; i++) { 511 data = &usc->tx_data[i]; 512 513 data->sc = usc; /* Backpointer for callbacks. */ 514 515 data->xfer = usbd_alloc_xfer(usc->sc_udev); 516 if (data->xfer == NULL) { 517 printf("%s: could not allocate xfer\n", 518 usc->usb_dev.dv_xname); 519 error = ENOMEM; 520 break; 521 } 522 data->buf = usbd_alloc_buffer(data->xfer, ATHN_USB_TXBUFSZ); 523 if (data->buf == NULL) { 524 printf("%s: could not allocate xfer buffer\n", 525 usc->usb_dev.dv_xname); 526 error = ENOMEM; 527 break; 528 } 529 /* Append this Tx buffer to our free list. */ 530 TAILQ_INSERT_TAIL(&usc->tx_free_list, data, next); 531 } 532 if (error != 0) 533 athn_usb_free_tx_list(usc); 534 return (error); 535 } 536 537 void 538 athn_usb_free_tx_list(struct athn_usb_softc *usc) 539 { 540 int i; 541 542 /* NB: Caller must abort pipe first. */ 543 for (i = 0; i < ATHN_USB_TX_LIST_COUNT; i++) { 544 if (usc->tx_data[i].xfer != NULL) 545 usbd_free_xfer(usc->tx_data[i].xfer); 546 usc->tx_data[i].xfer = NULL; 547 } 548 } 549 550 int 551 athn_usb_alloc_tx_cmd(struct athn_usb_softc *usc) 552 { 553 struct athn_usb_tx_data *data = &usc->tx_cmd; 554 555 data->sc = usc; /* Backpointer for callbacks. */ 556 557 data->xfer = usbd_alloc_xfer(usc->sc_udev); 558 if (data->xfer == NULL) { 559 printf("%s: could not allocate xfer\n", 560 usc->usb_dev.dv_xname); 561 return (ENOMEM); 562 } 563 data->buf = usbd_alloc_buffer(data->xfer, ATHN_USB_TXCMDSZ); 564 if (data->buf == NULL) { 565 printf("%s: could not allocate xfer buffer\n", 566 usc->usb_dev.dv_xname); 567 return (ENOMEM); 568 } 569 return (0); 570 } 571 572 void 573 athn_usb_free_tx_cmd(struct athn_usb_softc *usc) 574 { 575 if (usc->tx_cmd.xfer != NULL) 576 usbd_free_xfer(usc->tx_cmd.xfer); 577 usc->tx_cmd.xfer = NULL; 578 } 579 580 void 581 athn_usb_task(void *arg) 582 { 583 struct athn_usb_softc *usc = arg; 584 struct athn_usb_host_cmd_ring *ring = &usc->cmdq; 585 struct athn_usb_host_cmd *cmd; 586 int s; 587 588 /* Process host commands. */ 589 s = splusb(); 590 while (ring->next != ring->cur) { 591 cmd = &ring->cmd[ring->next]; 592 splx(s); 593 /* Invoke callback. */ 594 cmd->cb(usc, cmd->data); 595 s = splusb(); 596 ring->queued--; 597 ring->next = (ring->next + 1) % ATHN_USB_HOST_CMD_RING_COUNT; 598 } 599 splx(s); 600 } 601 602 void 603 athn_usb_do_async(struct athn_usb_softc *usc, 604 void (*cb)(struct athn_usb_softc *, void *), void *arg, int len) 605 { 606 struct athn_usb_host_cmd_ring *ring = &usc->cmdq; 607 struct athn_usb_host_cmd *cmd; 608 int s; 609 610 if (ring->queued == ATHN_USB_HOST_CMD_RING_COUNT) { 611 printf("%s: host cmd queue overrun\n", usc->usb_dev.dv_xname); 612 return; /* XXX */ 613 } 614 615 s = splusb(); 616 cmd = &ring->cmd[ring->cur]; 617 cmd->cb = cb; 618 KASSERT(len <= sizeof(cmd->data)); 619 memcpy(cmd->data, arg, len); 620 ring->cur = (ring->cur + 1) % ATHN_USB_HOST_CMD_RING_COUNT; 621 622 /* If there is no pending command already, schedule a task. */ 623 if (++ring->queued == 1) 624 usb_add_task(usc->sc_udev, &usc->sc_task); 625 splx(s); 626 } 627 628 void 629 athn_usb_wait_async(struct athn_usb_softc *usc) 630 { 631 /* Wait for all queued asynchronous commands to complete. */ 632 usb_wait_task(usc->sc_udev, &usc->sc_task); 633 } 634 635 int 636 athn_usb_load_firmware(struct athn_usb_softc *usc) 637 { 638 usb_device_descriptor_t *dd; 639 usb_device_request_t req; 640 const char *name; 641 u_char *fw, *ptr; 642 size_t fwsize, size; 643 uint32_t addr; 644 int s, mlen, error; 645 646 /* Determine which firmware image to load. */ 647 if (usc->flags & ATHN_USB_FLAG_AR7010) { 648 dd = usbd_get_device_descriptor(usc->sc_udev); 649 name = "athn-open-ar7010"; 650 } else 651 name = "athn-open-ar9271"; 652 /* Read firmware image from the filesystem. */ 653 if ((error = loadfirmware(name, &fw, &fwsize)) != 0) { 654 printf("%s: failed loadfirmware of file %s (error %d)\n", 655 usc->usb_dev.dv_xname, name, error); 656 return (error); 657 } 658 /* Load firmware image. */ 659 ptr = fw; 660 addr = AR9271_FIRMWARE >> 8; 661 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 662 req.bRequest = AR_FW_DOWNLOAD; 663 USETW(req.wIndex, 0); 664 size = fwsize; 665 while (size > 0) { 666 mlen = MIN(size, 4096); 667 668 USETW(req.wValue, addr); 669 USETW(req.wLength, mlen); 670 error = usbd_do_request(usc->sc_udev, &req, ptr); 671 if (error != 0) { 672 free(fw, M_DEVBUF, fwsize); 673 return (error); 674 } 675 addr += mlen >> 8; 676 ptr += mlen; 677 size -= mlen; 678 } 679 free(fw, M_DEVBUF, fwsize); 680 681 /* Start firmware. */ 682 if (usc->flags & ATHN_USB_FLAG_AR7010) 683 addr = AR7010_FIRMWARE_TEXT >> 8; 684 else 685 addr = AR9271_FIRMWARE_TEXT >> 8; 686 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 687 req.bRequest = AR_FW_DOWNLOAD_COMP; 688 USETW(req.wIndex, 0); 689 USETW(req.wValue, addr); 690 USETW(req.wLength, 0); 691 s = splusb(); 692 usc->wait_msg_id = AR_HTC_MSG_READY; 693 error = usbd_do_request(usc->sc_udev, &req, NULL); 694 /* Wait at most 1 second for firmware to boot. */ 695 if (error == 0 && usc->wait_msg_id != 0) 696 error = tsleep(&usc->wait_msg_id, 0, "athnfw", hz); 697 usc->wait_msg_id = 0; 698 splx(s); 699 return (error); 700 } 701 702 int 703 athn_usb_htc_msg(struct athn_usb_softc *usc, uint16_t msg_id, void *buf, 704 int len) 705 { 706 struct athn_usb_tx_data *data = &usc->tx_cmd; 707 struct ar_htc_frame_hdr *htc; 708 struct ar_htc_msg_hdr *msg; 709 710 htc = (struct ar_htc_frame_hdr *)data->buf; 711 memset(htc, 0, sizeof(*htc)); 712 htc->endpoint_id = 0; 713 htc->payload_len = htobe16(sizeof(*msg) + len); 714 715 msg = (struct ar_htc_msg_hdr *)&htc[1]; 716 msg->msg_id = htobe16(msg_id); 717 718 memcpy(&msg[1], buf, len); 719 720 usbd_setup_xfer(data->xfer, usc->tx_intr_pipe, NULL, data->buf, 721 sizeof(*htc) + sizeof(*msg) + len, 722 USBD_SHORT_XFER_OK | USBD_NO_COPY | USBD_SYNCHRONOUS, 723 ATHN_USB_CMD_TIMEOUT, NULL); 724 return (usbd_transfer(data->xfer)); 725 } 726 727 int 728 athn_usb_htc_setup(struct athn_usb_softc *usc) 729 { 730 struct ar_htc_msg_config_pipe cfg; 731 int s, error; 732 733 /* 734 * Connect WMI services to USB pipes. 735 */ 736 error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_CONTROL, 737 AR_PIPE_TX_INTR, AR_PIPE_RX_INTR, &usc->ep_ctrl); 738 if (error != 0) 739 return (error); 740 error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_BEACON, 741 AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->ep_bcn); 742 if (error != 0) 743 return (error); 744 error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_CAB, 745 AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->ep_cab); 746 if (error != 0) 747 return (error); 748 error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_UAPSD, 749 AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->ep_uapsd); 750 if (error != 0) 751 return (error); 752 error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_MGMT, 753 AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->ep_mgmt); 754 if (error != 0) 755 return (error); 756 error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_BE, 757 AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->ep_data[EDCA_AC_BE]); 758 if (error != 0) 759 return (error); 760 error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_BK, 761 AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->ep_data[EDCA_AC_BK]); 762 if (error != 0) 763 return (error); 764 error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_VI, 765 AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->ep_data[EDCA_AC_VI]); 766 if (error != 0) 767 return (error); 768 error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_VO, 769 AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->ep_data[EDCA_AC_VO]); 770 if (error != 0) 771 return (error); 772 773 /* Set credits for WLAN Tx pipe. */ 774 memset(&cfg, 0, sizeof(cfg)); 775 cfg.pipe_id = UE_GET_ADDR(AR_PIPE_TX_DATA); 776 cfg.credits = (usc->flags & ATHN_USB_FLAG_AR7010) ? 45 : 33; 777 s = splusb(); 778 usc->wait_msg_id = AR_HTC_MSG_CONF_PIPE_RSP; 779 error = athn_usb_htc_msg(usc, AR_HTC_MSG_CONF_PIPE, &cfg, sizeof(cfg)); 780 if (error == 0 && usc->wait_msg_id != 0) 781 error = tsleep(&usc->wait_msg_id, 0, "athnhtc", hz); 782 usc->wait_msg_id = 0; 783 splx(s); 784 if (error != 0) { 785 printf("%s: could not configure pipe\n", 786 usc->usb_dev.dv_xname); 787 return (error); 788 } 789 790 error = athn_usb_htc_msg(usc, AR_HTC_MSG_SETUP_COMPLETE, NULL, 0); 791 if (error != 0) { 792 printf("%s: could not complete setup\n", 793 usc->usb_dev.dv_xname); 794 return (error); 795 } 796 return (0); 797 } 798 799 int 800 athn_usb_htc_connect_svc(struct athn_usb_softc *usc, uint16_t svc_id, 801 uint8_t ul_pipe, uint8_t dl_pipe, uint8_t *endpoint_id) 802 { 803 struct ar_htc_msg_conn_svc msg; 804 struct ar_htc_msg_conn_svc_rsp rsp; 805 int s, error; 806 807 memset(&msg, 0, sizeof(msg)); 808 msg.svc_id = htobe16(svc_id); 809 msg.dl_pipeid = UE_GET_ADDR(dl_pipe); 810 msg.ul_pipeid = UE_GET_ADDR(ul_pipe); 811 s = splusb(); 812 usc->msg_conn_svc_rsp = &rsp; 813 usc->wait_msg_id = AR_HTC_MSG_CONN_SVC_RSP; 814 error = athn_usb_htc_msg(usc, AR_HTC_MSG_CONN_SVC, &msg, sizeof(msg)); 815 /* Wait at most 1 second for response. */ 816 if (error == 0 && usc->wait_msg_id != 0) 817 error = tsleep(&usc->wait_msg_id, 0, "athnhtc", hz); 818 usc->wait_msg_id = 0; 819 splx(s); 820 if (error != 0) { 821 printf("%s: error waiting for service %d connection\n", 822 usc->usb_dev.dv_xname, svc_id); 823 return (error); 824 } 825 if (rsp.status != AR_HTC_SVC_SUCCESS) { 826 printf("%s: service %d connection failed, error %d\n", 827 usc->usb_dev.dv_xname, svc_id, rsp.status); 828 return (EIO); 829 } 830 DPRINTF(("service %d successfully connected to endpoint %d\n", 831 svc_id, rsp.endpoint_id)); 832 833 /* Return endpoint id. */ 834 *endpoint_id = rsp.endpoint_id; 835 return (0); 836 } 837 838 int 839 athn_usb_wmi_xcmd(struct athn_usb_softc *usc, uint16_t cmd_id, void *ibuf, 840 int ilen, void *obuf) 841 { 842 struct athn_usb_tx_data *data = &usc->tx_cmd; 843 struct ar_htc_frame_hdr *htc; 844 struct ar_wmi_cmd_hdr *wmi; 845 int s, error; 846 847 if (usbd_is_dying(usc->sc_udev)) 848 return ENXIO; 849 850 s = splusb(); 851 while (usc->wait_cmd_id) { 852 /* 853 * The previous USB transfer is not done yet. We can't use 854 * data->xfer until it is done or we'll cause major confusion 855 * in the USB stack. 856 */ 857 tsleep(&usc->wait_cmd_id, 0, "athnwmx", ATHN_USB_CMD_TIMEOUT); 858 if (usbd_is_dying(usc->sc_udev)) { 859 splx(s); 860 return ENXIO; 861 } 862 } 863 splx(s); 864 865 htc = (struct ar_htc_frame_hdr *)data->buf; 866 memset(htc, 0, sizeof(*htc)); 867 htc->endpoint_id = usc->ep_ctrl; 868 htc->payload_len = htobe16(sizeof(*wmi) + ilen); 869 870 wmi = (struct ar_wmi_cmd_hdr *)&htc[1]; 871 wmi->cmd_id = htobe16(cmd_id); 872 usc->wmi_seq_no++; 873 wmi->seq_no = htobe16(usc->wmi_seq_no); 874 875 memcpy(&wmi[1], ibuf, ilen); 876 877 usbd_setup_xfer(data->xfer, usc->tx_intr_pipe, NULL, data->buf, 878 sizeof(*htc) + sizeof(*wmi) + ilen, 879 USBD_SHORT_XFER_OK | USBD_NO_COPY, ATHN_USB_CMD_TIMEOUT, 880 NULL); 881 s = splusb(); 882 error = usbd_transfer(data->xfer); 883 if (__predict_false(error != USBD_IN_PROGRESS && error != 0)) { 884 splx(s); 885 return (error); 886 } 887 usc->obuf = obuf; 888 usc->wait_cmd_id = cmd_id; 889 /* 890 * Wait for WMI command complete interrupt. In case it does not fire 891 * wait until the USB transfer times out to avoid racing the transfer. 892 */ 893 error = tsleep(&usc->wait_cmd_id, 0, "athnwmi", ATHN_USB_CMD_TIMEOUT); 894 if (error) { 895 if (error == EWOULDBLOCK) { 896 printf("%s: firmware command 0x%x timed out\n", 897 usc->usb_dev.dv_xname, cmd_id); 898 error = ETIMEDOUT; 899 } 900 } 901 902 /* 903 * Both the WMI command and transfer are done or have timed out. 904 * Allow other threads to enter this function and use data->xfer. 905 */ 906 usc->wait_cmd_id = 0; 907 wakeup(&usc->wait_cmd_id); 908 909 splx(s); 910 return (error); 911 } 912 913 int 914 athn_usb_read_rom(struct athn_softc *sc) 915 { 916 struct athn_usb_softc *usc = (struct athn_usb_softc *)sc; 917 uint32_t addrs[8], vals[8], addr; 918 uint16_t *eep; 919 int i, j, error; 920 921 /* Read EEPROM by blocks of 16 bytes. */ 922 eep = sc->eep; 923 addr = AR_EEPROM_OFFSET(sc->eep_base); 924 for (i = 0; i < sc->eep_size / 16; i++) { 925 for (j = 0; j < 8; j++, addr += 4) 926 addrs[j] = htobe32(addr); 927 error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_REG_READ, 928 addrs, sizeof(addrs), vals); 929 if (error != 0) 930 break; 931 for (j = 0; j < 8; j++) 932 *eep++ = betoh32(vals[j]); 933 } 934 return (error); 935 } 936 937 uint32_t 938 athn_usb_read(struct athn_softc *sc, uint32_t addr) 939 { 940 struct athn_usb_softc *usc = (struct athn_usb_softc *)sc; 941 uint32_t val; 942 int error; 943 944 /* Flush pending writes for strict consistency. */ 945 athn_usb_write_barrier(sc); 946 947 addr = htobe32(addr); 948 error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_REG_READ, 949 &addr, sizeof(addr), &val); 950 if (error != 0) 951 return (0xdeadbeef); 952 return (betoh32(val)); 953 } 954 955 void 956 athn_usb_write(struct athn_softc *sc, uint32_t addr, uint32_t val) 957 { 958 struct athn_usb_softc *usc = (struct athn_usb_softc *)sc; 959 960 usc->wbuf[usc->wcount].addr = htobe32(addr); 961 usc->wbuf[usc->wcount].val = htobe32(val); 962 if (++usc->wcount == AR_MAX_WRITE_COUNT) 963 athn_usb_write_barrier(sc); 964 } 965 966 void 967 athn_usb_write_barrier(struct athn_softc *sc) 968 { 969 struct athn_usb_softc *usc = (struct athn_usb_softc *)sc; 970 971 if (usc->wcount == 0) 972 return; /* Nothing to write. */ 973 974 (void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_REG_WRITE, 975 usc->wbuf, usc->wcount * sizeof(usc->wbuf[0]), NULL); 976 usc->wcount = 0; /* Always flush buffer. */ 977 } 978 979 int 980 athn_usb_media_change(struct ifnet *ifp) 981 { 982 struct athn_usb_softc *usc = (struct athn_usb_softc *)ifp->if_softc; 983 int error; 984 985 if (usbd_is_dying(usc->sc_udev)) 986 return ENXIO; 987 988 error = ieee80211_media_change(ifp); 989 if (error != ENETRESET) 990 return (error); 991 992 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 993 (IFF_UP | IFF_RUNNING)) { 994 athn_usb_stop(ifp); 995 error = athn_usb_init(ifp); 996 } 997 return (error); 998 } 999 1000 void 1001 athn_usb_next_scan(void *arg) 1002 { 1003 struct athn_usb_softc *usc = arg; 1004 struct athn_softc *sc = &usc->sc_sc; 1005 struct ieee80211com *ic = &sc->sc_ic; 1006 int s; 1007 1008 if (usbd_is_dying(usc->sc_udev)) 1009 return; 1010 1011 usbd_ref_incr(usc->sc_udev); 1012 1013 s = splnet(); 1014 if (ic->ic_state == IEEE80211_S_SCAN) 1015 ieee80211_next_scan(&ic->ic_if); 1016 splx(s); 1017 1018 usbd_ref_decr(usc->sc_udev); 1019 } 1020 1021 int 1022 athn_usb_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, 1023 int arg) 1024 { 1025 struct athn_usb_softc *usc = ic->ic_softc; 1026 struct athn_usb_cmd_newstate cmd; 1027 1028 /* Do it in a process context. */ 1029 cmd.state = nstate; 1030 cmd.arg = arg; 1031 athn_usb_do_async(usc, athn_usb_newstate_cb, &cmd, sizeof(cmd)); 1032 return (0); 1033 } 1034 1035 void 1036 athn_usb_newstate_cb(struct athn_usb_softc *usc, void *arg) 1037 { 1038 struct athn_usb_cmd_newstate *cmd = arg; 1039 struct athn_softc *sc = &usc->sc_sc; 1040 struct ieee80211com *ic = &sc->sc_ic; 1041 enum ieee80211_state ostate; 1042 uint32_t reg, imask; 1043 int s, error; 1044 1045 timeout_del(&sc->calib_to); 1046 1047 s = splnet(); 1048 ostate = ic->ic_state; 1049 1050 if (ostate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA) { 1051 athn_usb_remove_node(usc, ic->ic_bss); 1052 reg = AR_READ(sc, AR_RX_FILTER); 1053 reg = (reg & ~AR_RX_FILTER_MYBEACON) | 1054 AR_RX_FILTER_BEACON; 1055 AR_WRITE(sc, AR_RX_FILTER, reg); 1056 AR_WRITE_BARRIER(sc); 1057 } 1058 switch (cmd->state) { 1059 case IEEE80211_S_INIT: 1060 athn_set_led(sc, 0); 1061 break; 1062 case IEEE80211_S_SCAN: 1063 /* Make the LED blink while scanning. */ 1064 athn_set_led(sc, !sc->led_state); 1065 error = athn_usb_switch_chan(sc, ic->ic_bss->ni_chan, NULL); 1066 if (error) 1067 printf("%s: could not switch to channel %d\n", 1068 usc->usb_dev.dv_xname, 1069 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)); 1070 if (!usbd_is_dying(usc->sc_udev)) 1071 timeout_add_msec(&sc->scan_to, 200); 1072 break; 1073 case IEEE80211_S_AUTH: 1074 athn_set_led(sc, 0); 1075 error = athn_usb_switch_chan(sc, ic->ic_bss->ni_chan, NULL); 1076 if (error) 1077 printf("%s: could not switch to channel %d\n", 1078 usc->usb_dev.dv_xname, 1079 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)); 1080 break; 1081 case IEEE80211_S_ASSOC: 1082 break; 1083 case IEEE80211_S_RUN: 1084 athn_set_led(sc, 1); 1085 1086 if (ic->ic_opmode == IEEE80211_M_MONITOR) 1087 break; 1088 1089 if (ic->ic_opmode == IEEE80211_M_STA) { 1090 /* Create node entry for our BSS */ 1091 error = athn_usb_create_node(usc, ic->ic_bss); 1092 if (error) 1093 printf("%s: could not update firmware station " 1094 "table\n", usc->usb_dev.dv_xname); 1095 } 1096 athn_set_bss(sc, ic->ic_bss); 1097 athn_usb_wmi_cmd(usc, AR_WMI_CMD_DISABLE_INTR); 1098 #ifndef IEEE80211_STA_ONLY 1099 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 1100 athn_usb_switch_chan(sc, ic->ic_bss->ni_chan, NULL); 1101 athn_set_hostap_timers(sc); 1102 /* Enable software beacon alert interrupts. */ 1103 imask = htobe32(AR_IMR_SWBA); 1104 } else 1105 #endif 1106 { 1107 athn_set_sta_timers(sc); 1108 /* Enable beacon miss interrupts. */ 1109 imask = htobe32(AR_IMR_BMISS); 1110 1111 /* Stop receiving beacons from other BSS. */ 1112 reg = AR_READ(sc, AR_RX_FILTER); 1113 reg = (reg & ~AR_RX_FILTER_BEACON) | 1114 AR_RX_FILTER_MYBEACON; 1115 AR_WRITE(sc, AR_RX_FILTER, reg); 1116 AR_WRITE_BARRIER(sc); 1117 } 1118 athn_usb_wmi_xcmd(usc, AR_WMI_CMD_ENABLE_INTR, 1119 &imask, sizeof(imask), NULL); 1120 break; 1121 } 1122 (void)sc->sc_newstate(ic, cmd->state, cmd->arg); 1123 splx(s); 1124 } 1125 1126 void 1127 athn_usb_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, 1128 int isnew) 1129 { 1130 #ifndef IEEE80211_STA_ONLY 1131 struct athn_usb_softc *usc = ic->ic_softc; 1132 1133 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 1134 ic->ic_state != IEEE80211_S_RUN) 1135 return; 1136 1137 /* Update the node's supported rates in a process context. */ 1138 ieee80211_ref_node(ni); 1139 athn_usb_do_async(usc, athn_usb_newassoc_cb, &ni, sizeof(ni)); 1140 #endif 1141 } 1142 1143 #ifndef IEEE80211_STA_ONLY 1144 void 1145 athn_usb_newassoc_cb(struct athn_usb_softc *usc, void *arg) 1146 { 1147 struct ieee80211com *ic = &usc->sc_sc.sc_ic; 1148 struct ieee80211_node *ni = *(void **)arg; 1149 struct athn_node *an = (struct athn_node *)ni; 1150 int s; 1151 1152 if (ic->ic_state != IEEE80211_S_RUN) 1153 return; 1154 1155 s = splnet(); 1156 /* NB: Node may have left before we got scheduled. */ 1157 if (an->sta_index != 0) 1158 (void)athn_usb_node_set_rates(usc, ni); 1159 ieee80211_release_node(ic, ni); 1160 splx(s); 1161 } 1162 #endif 1163 1164 struct ieee80211_node * 1165 athn_usb_node_alloc(struct ieee80211com *ic) 1166 { 1167 struct athn_node *an; 1168 1169 an = malloc(sizeof(struct athn_node), M_DEVBUF, M_NOWAIT | M_ZERO); 1170 return (struct ieee80211_node *)an; 1171 } 1172 1173 1174 #ifndef IEEE80211_STA_ONLY 1175 void 1176 athn_usb_count_active_sta(void *arg, struct ieee80211_node *ni) 1177 { 1178 int *nsta = arg; 1179 struct athn_node *an = (struct athn_node *)ni; 1180 1181 if (an->sta_index == 0) 1182 return; 1183 1184 if ((ni->ni_state == IEEE80211_STA_AUTH || 1185 ni->ni_state == IEEE80211_STA_ASSOC) && 1186 ni->ni_inact < IEEE80211_INACT_MAX) 1187 (*nsta)++; 1188 } 1189 1190 struct athn_usb_newauth_cb_arg { 1191 struct ieee80211_node *ni; 1192 uint16_t seq; 1193 }; 1194 1195 void 1196 athn_usb_newauth_cb(struct athn_usb_softc *usc, void *arg) 1197 { 1198 struct ieee80211com *ic = &usc->sc_sc.sc_ic; 1199 struct athn_usb_newauth_cb_arg *a = arg; 1200 struct ieee80211_node *ni = a->ni; 1201 uint16_t seq = a->seq; 1202 struct athn_node *an = (struct athn_node *)ni; 1203 int s, error = 0; 1204 1205 free(arg, M_DEVBUF, sizeof(*arg)); 1206 1207 if (ic->ic_state != IEEE80211_S_RUN) 1208 return; 1209 1210 s = splnet(); 1211 if (an->sta_index == 0) { 1212 error = athn_usb_create_node(usc, ni); 1213 if (error) 1214 printf("%s: could not add station %s to firmware " 1215 "table\n", usc->usb_dev.dv_xname, 1216 ether_sprintf(ni->ni_macaddr)); 1217 } 1218 if (error == 0) 1219 ieee80211_auth_open_confirm(ic, ni, seq); 1220 ieee80211_unref_node(&ni); 1221 splx(s); 1222 } 1223 #endif 1224 1225 int 1226 athn_usb_newauth(struct ieee80211com *ic, struct ieee80211_node *ni, 1227 int isnew, uint16_t seq) 1228 { 1229 #ifndef IEEE80211_STA_ONLY 1230 struct athn_usb_softc *usc = ic->ic_softc; 1231 struct ifnet *ifp = &ic->ic_if; 1232 struct athn_node *an = (struct athn_node *)ni; 1233 int nsta; 1234 struct athn_usb_newauth_cb_arg *arg; 1235 1236 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 1237 return 0; 1238 1239 if (!isnew && an->sta_index != 0) /* already in firmware table */ 1240 return 0; 1241 1242 /* Check if we have room in the firmware table. */ 1243 nsta = 1; /* Account for default node. */ 1244 ieee80211_iterate_nodes(ic, athn_usb_count_active_sta, &nsta); 1245 if (nsta >= AR_USB_MAX_STA) { 1246 if (ifp->if_flags & IFF_DEBUG) 1247 printf("%s: cannot authenticate station %s: firmware " 1248 "table is full\n", usc->usb_dev.dv_xname, 1249 ether_sprintf(ni->ni_macaddr)); 1250 return ENOSPC; 1251 } 1252 1253 /* 1254 * In a process context, try to add this node to the 1255 * firmware table and confirm the AUTH request. 1256 */ 1257 arg = malloc(sizeof(*arg), M_DEVBUF, M_NOWAIT); 1258 if (arg == NULL) 1259 return ENOMEM; 1260 arg->ni = ieee80211_ref_node(ni); 1261 arg->seq = seq; 1262 athn_usb_do_async(usc, athn_usb_newauth_cb, arg, sizeof(*arg)); 1263 return EBUSY; 1264 #else 1265 return 0; 1266 #endif /* IEEE80211_STA_ONLY */ 1267 } 1268 1269 #ifndef IEEE80211_STA_ONLY 1270 void 1271 athn_usb_node_free(struct ieee80211com *ic, struct ieee80211_node *ni) 1272 { 1273 struct athn_usb_softc *usc = ic->ic_softc; 1274 struct athn_node *an = (struct athn_node *)ni; 1275 1276 /* 1277 * Remove the node from the firmware table in a process context. 1278 * Pass an index rather than the pointer which we will free. 1279 */ 1280 if (an->sta_index != 0) 1281 athn_usb_do_async(usc, athn_usb_node_free_cb, 1282 &an->sta_index, sizeof(an->sta_index)); 1283 usc->sc_node_free(ic, ni); 1284 } 1285 1286 void 1287 athn_usb_node_free_cb(struct athn_usb_softc *usc, void *arg) 1288 { 1289 struct ieee80211com *ic = &usc->sc_sc.sc_ic; 1290 struct ifnet *ifp = &ic->ic_if; 1291 uint8_t sta_index = *(uint8_t *)arg; 1292 int error; 1293 1294 error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_NODE_REMOVE, 1295 &sta_index, sizeof(sta_index), NULL); 1296 if (error) { 1297 printf("%s: could not remove station %u from firmware table\n", 1298 usc->usb_dev.dv_xname, sta_index); 1299 return; 1300 } 1301 usc->free_node_slots |= (1 << sta_index); 1302 if (ifp->if_flags & IFF_DEBUG) 1303 printf("%s: station %u removed from firmware table\n", 1304 usc->usb_dev.dv_xname, sta_index); 1305 } 1306 #endif /* IEEE80211_STA_ONLY */ 1307 1308 int 1309 athn_usb_ampdu_tx_start(struct ieee80211com *ic, struct ieee80211_node *ni, 1310 uint8_t tid) 1311 { 1312 struct athn_usb_softc *usc = ic->ic_softc; 1313 struct athn_node *an = (struct athn_node *)ni; 1314 struct athn_usb_aggr_cmd cmd; 1315 1316 /* Do it in a process context. */ 1317 cmd.sta_index = an->sta_index; 1318 cmd.tid = tid; 1319 athn_usb_do_async(usc, athn_usb_ampdu_tx_start_cb, &cmd, sizeof(cmd)); 1320 return (0); 1321 } 1322 1323 void 1324 athn_usb_ampdu_tx_start_cb(struct athn_usb_softc *usc, void *arg) 1325 { 1326 struct athn_usb_aggr_cmd *cmd = arg; 1327 struct ar_htc_target_aggr aggr; 1328 1329 memset(&aggr, 0, sizeof(aggr)); 1330 aggr.sta_index = cmd->sta_index; 1331 aggr.tidno = cmd->tid; 1332 aggr.aggr_enable = 1; 1333 (void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_TX_AGGR_ENABLE, 1334 &aggr, sizeof(aggr), NULL); 1335 } 1336 1337 void 1338 athn_usb_ampdu_tx_stop(struct ieee80211com *ic, struct ieee80211_node *ni, 1339 uint8_t tid) 1340 { 1341 struct athn_usb_softc *usc = ic->ic_softc; 1342 struct athn_node *an = (struct athn_node *)ni; 1343 struct athn_usb_aggr_cmd cmd; 1344 1345 /* Do it in a process context. */ 1346 cmd.sta_index = an->sta_index; 1347 cmd.tid = tid; 1348 athn_usb_do_async(usc, athn_usb_ampdu_tx_stop_cb, &cmd, sizeof(cmd)); 1349 } 1350 1351 void 1352 athn_usb_ampdu_tx_stop_cb(struct athn_usb_softc *usc, void *arg) 1353 { 1354 struct athn_usb_aggr_cmd *cmd = arg; 1355 struct ar_htc_target_aggr aggr; 1356 1357 memset(&aggr, 0, sizeof(aggr)); 1358 aggr.sta_index = cmd->sta_index; 1359 aggr.tidno = cmd->tid; 1360 aggr.aggr_enable = 0; 1361 (void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_TX_AGGR_ENABLE, 1362 &aggr, sizeof(aggr), NULL); 1363 } 1364 1365 #ifndef IEEE80211_STA_ONLY 1366 /* Try to find a node we can evict to make room in the firmware table. */ 1367 void 1368 athn_usb_clean_nodes(void *arg, struct ieee80211_node *ni) 1369 { 1370 struct athn_usb_softc *usc = arg; 1371 struct ieee80211com *ic = &usc->sc_sc.sc_ic; 1372 struct athn_node *an = (struct athn_node *)ni; 1373 1374 /* 1375 * Don't remove the default node (used for management frames). 1376 * Nodes which are not in the firmware table also have index zero. 1377 */ 1378 if (an->sta_index == 0) 1379 return; 1380 1381 /* Remove non-associated nodes. */ 1382 if (ni->ni_state != IEEE80211_STA_AUTH && 1383 ni->ni_state != IEEE80211_STA_ASSOC) { 1384 athn_usb_remove_node(usc, ni); 1385 return; 1386 } 1387 1388 /* 1389 * Kick off inactive associated nodes. This won't help 1390 * immediately but will help if the new STA retries later. 1391 */ 1392 if (ni->ni_inact >= IEEE80211_INACT_MAX) { 1393 IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH, 1394 IEEE80211_REASON_AUTH_EXPIRE); 1395 ieee80211_node_leave(ic, ni); 1396 } 1397 } 1398 #endif 1399 1400 int 1401 athn_usb_create_node(struct athn_usb_softc *usc, struct ieee80211_node *ni) 1402 { 1403 struct athn_node *an = (struct athn_node *)ni; 1404 struct ar_htc_target_sta sta; 1405 int error, sta_index; 1406 #ifndef IEEE80211_STA_ONLY 1407 struct ieee80211com *ic = &usc->sc_sc.sc_ic; 1408 struct ifnet *ifp = &ic->ic_if; 1409 1410 /* Firmware cannot handle more than 8 STAs. Try to make room first. */ 1411 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1412 ieee80211_iterate_nodes(ic, athn_usb_clean_nodes, usc); 1413 #endif 1414 if (usc->free_node_slots == 0x00) 1415 return ENOBUFS; 1416 1417 sta_index = ffs(usc->free_node_slots) - 1; 1418 if (sta_index < 0 || sta_index >= AR_USB_MAX_STA) 1419 return ENOSPC; 1420 1421 /* Create node entry on target. */ 1422 memset(&sta, 0, sizeof(sta)); 1423 IEEE80211_ADDR_COPY(sta.macaddr, ni->ni_macaddr); 1424 IEEE80211_ADDR_COPY(sta.bssid, ni->ni_bssid); 1425 sta.sta_index = sta_index; 1426 sta.maxampdu = 0xffff; 1427 if (ni->ni_flags & IEEE80211_NODE_HT) 1428 sta.flags |= htobe16(AR_HTC_STA_HT); 1429 error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_NODE_CREATE, 1430 &sta, sizeof(sta), NULL); 1431 if (error != 0) 1432 return (error); 1433 an->sta_index = sta_index; 1434 usc->free_node_slots &= ~(1 << an->sta_index); 1435 1436 #ifndef IEEE80211_STA_ONLY 1437 if (ifp->if_flags & IFF_DEBUG) 1438 printf("%s: station %u (%s) added to firmware table\n", 1439 usc->usb_dev.dv_xname, sta_index, 1440 ether_sprintf(ni->ni_macaddr)); 1441 #endif 1442 return athn_usb_node_set_rates(usc, ni); 1443 } 1444 1445 int 1446 athn_usb_node_set_rates(struct athn_usb_softc *usc, struct ieee80211_node *ni) 1447 { 1448 struct athn_node *an = (struct athn_node *)ni; 1449 struct ar_htc_target_rate rate; 1450 int i, j; 1451 1452 /* Setup supported rates. */ 1453 memset(&rate, 0, sizeof(rate)); 1454 rate.sta_index = an->sta_index; 1455 rate.isnew = 1; 1456 rate.lg_rates.rs_nrates = ni->ni_rates.rs_nrates; 1457 memcpy(rate.lg_rates.rs_rates, ni->ni_rates.rs_rates, 1458 ni->ni_rates.rs_nrates); 1459 if (ni->ni_flags & IEEE80211_NODE_HT) { 1460 rate.capflags |= htobe32(AR_RC_HT_FLAG); 1461 /* Setup HT rates. */ 1462 for (i = 0, j = 0; i < IEEE80211_HT_NUM_MCS; i++) { 1463 if (!isset(ni->ni_rxmcs, i)) 1464 continue; 1465 if (j >= AR_HTC_RATE_MAX) 1466 break; 1467 rate.ht_rates.rs_rates[j++] = i; 1468 } 1469 rate.ht_rates.rs_nrates = j; 1470 1471 if (ni->ni_rxmcs[1]) /* dual-stream MIMO rates */ 1472 rate.capflags |= htobe32(AR_RC_DS_FLAG); 1473 #ifdef notyet 1474 if (ni->ni_htcaps & IEEE80211_HTCAP_CBW20_40) 1475 rate.capflags |= htobe32(AR_RC_40_FLAG); 1476 if (ni->ni_htcaps & IEEE80211_HTCAP_SGI40) 1477 rate.capflags |= htobe32(AR_RC_SGI_FLAG); 1478 if (ni->ni_htcaps & IEEE80211_HTCAP_SGI20) 1479 rate.capflags |= htobe32(AR_RC_SGI_FLAG); 1480 #endif 1481 } 1482 1483 return athn_usb_wmi_xcmd(usc, AR_WMI_CMD_RC_RATE_UPDATE, 1484 &rate, sizeof(rate), NULL); 1485 } 1486 1487 int 1488 athn_usb_remove_node(struct athn_usb_softc *usc, struct ieee80211_node *ni) 1489 { 1490 struct athn_node *an = (struct athn_node *)ni; 1491 int error; 1492 #ifndef IEEE80211_STA_ONLY 1493 struct ieee80211com *ic = &usc->sc_sc.sc_ic; 1494 struct ifnet *ifp = &ic->ic_if; 1495 #endif 1496 1497 error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_NODE_REMOVE, 1498 &an->sta_index, sizeof(an->sta_index), NULL); 1499 if (error) { 1500 printf("%s: could not remove station %u (%s) from " 1501 "firmware table\n", usc->usb_dev.dv_xname, an->sta_index, 1502 ether_sprintf(ni->ni_macaddr)); 1503 return error; 1504 } 1505 1506 #ifndef IEEE80211_STA_ONLY 1507 if (ifp->if_flags & IFF_DEBUG) 1508 printf("%s: station %u (%s) removed from firmware table\n", 1509 usc->usb_dev.dv_xname, an->sta_index, 1510 ether_sprintf(ni->ni_macaddr)); 1511 #endif 1512 1513 usc->free_node_slots |= (1 << an->sta_index); 1514 an->sta_index = 0; 1515 return 0; 1516 } 1517 1518 void 1519 athn_usb_rx_enable(struct athn_softc *sc) 1520 { 1521 AR_WRITE(sc, AR_CR, AR_CR_RXE); 1522 AR_WRITE_BARRIER(sc); 1523 } 1524 1525 int 1526 athn_usb_switch_chan(struct athn_softc *sc, struct ieee80211_channel *c, 1527 struct ieee80211_channel *extc) 1528 { 1529 struct athn_usb_softc *usc = (struct athn_usb_softc *)sc; 1530 uint16_t mode; 1531 int error; 1532 1533 /* Disable interrupts. */ 1534 error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_DISABLE_INTR); 1535 if (error != 0) 1536 goto reset; 1537 /* Stop all Tx queues. */ 1538 error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_DRAIN_TXQ_ALL); 1539 if (error != 0) 1540 goto reset; 1541 /* Stop Rx. */ 1542 error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_STOP_RECV); 1543 if (error != 0) 1544 goto reset; 1545 1546 /* If band or bandwidth changes, we need to do a full reset. */ 1547 if (c->ic_flags != sc->curchan->ic_flags || 1548 ((extc != NULL) ^ (sc->curchanext != NULL))) { 1549 DPRINTFN(2, ("channel band switch\n")); 1550 goto reset; 1551 } 1552 1553 error = athn_set_chan(sc, c, extc); 1554 if (AR_SREV_9271(sc) && error == 0) 1555 ar9271_load_ani(sc); 1556 if (error != 0) { 1557 reset: /* Error found, try a full reset. */ 1558 DPRINTFN(3, ("needs a full reset\n")); 1559 error = athn_hw_reset(sc, c, extc, 0); 1560 if (error != 0) /* Hopeless case. */ 1561 return (error); 1562 1563 error = athn_set_chan(sc, c, extc); 1564 if (AR_SREV_9271(sc) && error == 0) 1565 ar9271_load_ani(sc); 1566 if (error != 0) 1567 return (error); 1568 } 1569 1570 sc->ops.set_txpower(sc, c, extc); 1571 1572 error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_START_RECV); 1573 if (error != 0) 1574 return (error); 1575 athn_rx_start(sc); 1576 1577 mode = htobe16(IEEE80211_IS_CHAN_2GHZ(c) ? 1578 AR_HTC_MODE_11NG : AR_HTC_MODE_11NA); 1579 error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_SET_MODE, 1580 &mode, sizeof(mode), NULL); 1581 if (error != 0) 1582 return (error); 1583 1584 /* Re-enable interrupts. */ 1585 error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_ENABLE_INTR); 1586 return (error); 1587 } 1588 1589 void 1590 athn_usb_updateedca(struct ieee80211com *ic) 1591 { 1592 struct athn_usb_softc *usc = ic->ic_softc; 1593 1594 /* Do it in a process context. */ 1595 athn_usb_do_async(usc, athn_usb_updateedca_cb, NULL, 0); 1596 } 1597 1598 void 1599 athn_usb_updateedca_cb(struct athn_usb_softc *usc, void *arg) 1600 { 1601 int s; 1602 1603 s = splnet(); 1604 athn_updateedca(&usc->sc_sc.sc_ic); 1605 splx(s); 1606 } 1607 1608 void 1609 athn_usb_updateslot(struct ieee80211com *ic) 1610 { 1611 struct athn_usb_softc *usc = ic->ic_softc; 1612 1613 return; /* XXX */ 1614 /* Do it in a process context. */ 1615 athn_usb_do_async(usc, athn_usb_updateslot_cb, NULL, 0); 1616 } 1617 1618 void 1619 athn_usb_updateslot_cb(struct athn_usb_softc *usc, void *arg) 1620 { 1621 int s; 1622 1623 s = splnet(); 1624 athn_updateslot(&usc->sc_sc.sc_ic); 1625 splx(s); 1626 } 1627 1628 int 1629 athn_usb_set_key(struct ieee80211com *ic, struct ieee80211_node *ni, 1630 struct ieee80211_key *k) 1631 { 1632 struct athn_usb_softc *usc = ic->ic_softc; 1633 struct athn_usb_cmd_key cmd; 1634 1635 /* Defer setting of WEP keys until interface is brought up. */ 1636 if ((ic->ic_if.if_flags & (IFF_UP | IFF_RUNNING)) != 1637 (IFF_UP | IFF_RUNNING)) 1638 return (0); 1639 1640 /* Do it in a process context. */ 1641 cmd.ni = (ni != NULL) ? ieee80211_ref_node(ni) : NULL; 1642 cmd.key = k; 1643 athn_usb_do_async(usc, athn_usb_set_key_cb, &cmd, sizeof(cmd)); 1644 return (0); 1645 } 1646 1647 void 1648 athn_usb_set_key_cb(struct athn_usb_softc *usc, void *arg) 1649 { 1650 struct ieee80211com *ic = &usc->sc_sc.sc_ic; 1651 struct athn_usb_cmd_key *cmd = arg; 1652 int s; 1653 1654 s = splnet(); 1655 athn_set_key(ic, cmd->ni, cmd->key); 1656 if (cmd->ni != NULL) 1657 ieee80211_release_node(ic, cmd->ni); 1658 splx(s); 1659 } 1660 1661 void 1662 athn_usb_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni, 1663 struct ieee80211_key *k) 1664 { 1665 struct athn_usb_softc *usc = ic->ic_softc; 1666 struct athn_usb_cmd_key cmd; 1667 1668 if (!(ic->ic_if.if_flags & IFF_RUNNING) || 1669 ic->ic_state != IEEE80211_S_RUN) 1670 return; /* Nothing to do. */ 1671 1672 /* Do it in a process context. */ 1673 cmd.ni = (ni != NULL) ? ieee80211_ref_node(ni) : NULL; 1674 cmd.key = k; 1675 athn_usb_do_async(usc, athn_usb_delete_key_cb, &cmd, sizeof(cmd)); 1676 } 1677 1678 void 1679 athn_usb_delete_key_cb(struct athn_usb_softc *usc, void *arg) 1680 { 1681 struct ieee80211com *ic = &usc->sc_sc.sc_ic; 1682 struct athn_usb_cmd_key *cmd = arg; 1683 int s; 1684 1685 s = splnet(); 1686 athn_delete_key(ic, cmd->ni, cmd->key); 1687 if (cmd->ni != NULL) 1688 ieee80211_release_node(ic, cmd->ni); 1689 splx(s); 1690 } 1691 1692 #ifndef IEEE80211_STA_ONLY 1693 void 1694 athn_usb_bcneof(struct usbd_xfer *xfer, void *priv, 1695 usbd_status status) 1696 { 1697 struct athn_usb_tx_data *data = priv; 1698 struct athn_usb_softc *usc = data->sc; 1699 1700 if (__predict_false(status == USBD_STALLED)) 1701 usbd_clear_endpoint_stall_async(usc->tx_data_pipe); 1702 usc->tx_bcn = data; 1703 } 1704 1705 /* 1706 * Process Software Beacon Alert interrupts. 1707 */ 1708 void 1709 athn_usb_swba(struct athn_usb_softc *usc) 1710 { 1711 struct athn_softc *sc = &usc->sc_sc; 1712 struct ieee80211com *ic = &sc->sc_ic; 1713 struct athn_usb_tx_data *data; 1714 struct ieee80211_frame *wh; 1715 struct ar_stream_hdr *hdr; 1716 struct ar_htc_frame_hdr *htc; 1717 struct ar_tx_bcn *bcn; 1718 struct mbuf *m; 1719 int error; 1720 1721 if (ic->ic_dtim_count == 0) 1722 ic->ic_dtim_count = ic->ic_dtim_period - 1; 1723 else 1724 ic->ic_dtim_count--; 1725 1726 /* Make sure previous beacon has been sent. */ 1727 if (usc->tx_bcn == NULL) 1728 return; 1729 data = usc->tx_bcn; 1730 1731 /* Get new beacon. */ 1732 m = ieee80211_beacon_alloc(ic, ic->ic_bss); 1733 if (__predict_false(m == NULL)) 1734 return; 1735 /* Assign sequence number. */ 1736 wh = mtod(m, struct ieee80211_frame *); 1737 *(uint16_t *)&wh->i_seq[0] = 1738 htole16(ic->ic_bss->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT); 1739 ic->ic_bss->ni_txseq++; 1740 1741 hdr = (struct ar_stream_hdr *)data->buf; 1742 hdr->tag = htole16(AR_USB_TX_STREAM_TAG); 1743 hdr->len = htole16(sizeof(*htc) + sizeof(*bcn) + m->m_pkthdr.len); 1744 1745 htc = (struct ar_htc_frame_hdr *)&hdr[1]; 1746 memset(htc, 0, sizeof(*htc)); 1747 htc->endpoint_id = usc->ep_bcn; 1748 htc->payload_len = htobe16(sizeof(*bcn) + m->m_pkthdr.len); 1749 1750 bcn = (struct ar_tx_bcn *)&htc[1]; 1751 memset(bcn, 0, sizeof(*bcn)); 1752 bcn->vif_idx = 0; 1753 1754 m_copydata(m, 0, m->m_pkthdr.len, (caddr_t)&bcn[1]); 1755 1756 usbd_setup_xfer(data->xfer, usc->tx_data_pipe, data, data->buf, 1757 sizeof(*hdr) + sizeof(*htc) + sizeof(*bcn) + m->m_pkthdr.len, 1758 USBD_SHORT_XFER_OK | USBD_NO_COPY, ATHN_USB_TX_TIMEOUT, 1759 athn_usb_bcneof); 1760 1761 m_freem(m); 1762 usc->tx_bcn = NULL; 1763 error = usbd_transfer(data->xfer); 1764 if (__predict_false(error != USBD_IN_PROGRESS && error != 0)) 1765 usc->tx_bcn = data; 1766 } 1767 #endif 1768 1769 /* Update current transmit rate for a node based on firmware Tx status. */ 1770 void 1771 athn_usb_tx_status(void *arg, struct ieee80211_node *ni) 1772 { 1773 struct ar_wmi_evt_txstatus *ts = arg; 1774 struct athn_node *an = (struct athn_node *)ni; 1775 uint8_t rate_index = (ts->rate & AR_HTC_TXSTAT_RATE); 1776 1777 if (an->sta_index != ts->cookie) /* Tx report for a different node */ 1778 return; 1779 1780 if (ts->flags & AR_HTC_TXSTAT_MCS) { 1781 if (isset(ni->ni_rxmcs, rate_index)) 1782 ni->ni_txmcs = rate_index; 1783 } else if (rate_index < ni->ni_rates.rs_nrates) 1784 ni->ni_txrate = rate_index; 1785 } 1786 1787 void 1788 athn_usb_rx_wmi_ctrl(struct athn_usb_softc *usc, uint8_t *buf, int len) 1789 { 1790 struct ar_wmi_cmd_hdr *wmi; 1791 uint16_t cmd_id; 1792 1793 if (__predict_false(len < sizeof(*wmi))) 1794 return; 1795 wmi = (struct ar_wmi_cmd_hdr *)buf; 1796 cmd_id = betoh16(wmi->cmd_id); 1797 1798 if (!(cmd_id & AR_WMI_EVT_FLAG)) { 1799 if (usc->wait_cmd_id != cmd_id) 1800 return; /* Unexpected reply. */ 1801 if (usc->obuf != NULL) { 1802 /* Copy answer into caller supplied buffer. */ 1803 memcpy(usc->obuf, &wmi[1], len - sizeof(*wmi)); 1804 } 1805 /* Notify caller of completion. */ 1806 wakeup(&usc->wait_cmd_id); 1807 return; 1808 } 1809 switch (cmd_id & 0xfff) { 1810 #ifndef IEEE80211_STA_ONLY 1811 case AR_WMI_EVT_SWBA: 1812 athn_usb_swba(usc); 1813 break; 1814 #endif 1815 case AR_WMI_EVT_TXSTATUS: { 1816 struct ar_wmi_evt_txstatus_list *tsl; 1817 int i; 1818 1819 tsl = (struct ar_wmi_evt_txstatus_list *)&wmi[1]; 1820 for (i = 0; i < tsl->count && i < nitems(tsl->ts); i++) { 1821 struct ieee80211com *ic = &usc->sc_sc.sc_ic; 1822 struct athn_node *an = (struct athn_node *)ic->ic_bss; 1823 struct ar_wmi_evt_txstatus *ts = &tsl->ts[i]; 1824 uint8_t qid; 1825 1826 /* Skip the node we use to send management frames. */ 1827 if (ts->cookie == 0) 1828 continue; 1829 1830 /* Skip Tx reports for non-data frame endpoints. */ 1831 qid = (ts->rate & AR_HTC_TXSTAT_EPID) >> 1832 AR_HTC_TXSTAT_EPID_SHIFT; 1833 if (qid != usc->ep_data[EDCA_AC_BE] && 1834 qid != usc->ep_data[EDCA_AC_BK] && 1835 qid != usc->ep_data[EDCA_AC_VI] && 1836 qid != usc->ep_data[EDCA_AC_VO]) 1837 continue; 1838 1839 if (ts->cookie == an->sta_index) 1840 athn_usb_tx_status(ts, ic->ic_bss); 1841 else 1842 ieee80211_iterate_nodes(ic, athn_usb_tx_status, 1843 ts); 1844 } 1845 break; 1846 } 1847 case AR_WMI_EVT_FATAL: 1848 printf("%s: fatal firmware error\n", usc->usb_dev.dv_xname); 1849 break; 1850 default: 1851 DPRINTF(("WMI event %d ignored\n", cmd_id)); 1852 break; 1853 } 1854 } 1855 1856 void 1857 athn_usb_intr(struct usbd_xfer *xfer, void *priv, 1858 usbd_status status) 1859 { 1860 struct athn_usb_softc *usc = priv; 1861 struct ar_htc_frame_hdr *htc; 1862 struct ar_htc_msg_hdr *msg; 1863 uint8_t *buf = usc->ibuf; 1864 uint16_t msg_id; 1865 int len; 1866 1867 if (__predict_false(status != USBD_NORMAL_COMPLETION)) { 1868 DPRINTF(("intr status=%d\n", status)); 1869 if (status == USBD_STALLED) 1870 usbd_clear_endpoint_stall_async(usc->rx_intr_pipe); 1871 else if (status == USBD_IOERROR) { 1872 /* 1873 * The device has gone away. If async commands are 1874 * pending or running ensure the device dies ASAP 1875 * and any blocked processes are woken up. 1876 */ 1877 if (usc->cmdq.queued > 0) 1878 usbd_deactivate(usc->sc_udev); 1879 } 1880 return; 1881 } 1882 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 1883 1884 /* Skip watchdog pattern if present. */ 1885 if (len >= 4 && *(uint32_t *)buf == htobe32(0x00c60000)) { 1886 buf += 4; 1887 len -= 4; 1888 } 1889 if (__predict_false(len < sizeof(*htc))) 1890 return; 1891 htc = (struct ar_htc_frame_hdr *)buf; 1892 /* Skip HTC header. */ 1893 buf += sizeof(*htc); 1894 len -= sizeof(*htc); 1895 1896 if (htc->endpoint_id != 0) { 1897 if (__predict_false(htc->endpoint_id != usc->ep_ctrl)) 1898 return; 1899 /* Remove trailer if present. */ 1900 if (htc->flags & AR_HTC_FLAG_TRAILER) { 1901 if (__predict_false(len < htc->control[0])) 1902 return; 1903 len -= htc->control[0]; 1904 } 1905 athn_usb_rx_wmi_ctrl(usc, buf, len); 1906 return; 1907 } 1908 /* Endpoint 0 carries HTC messages. */ 1909 if (__predict_false(len < sizeof(*msg))) 1910 return; 1911 msg = (struct ar_htc_msg_hdr *)buf; 1912 msg_id = betoh16(msg->msg_id); 1913 DPRINTF(("Rx HTC message %d\n", msg_id)); 1914 switch (msg_id) { 1915 case AR_HTC_MSG_READY: 1916 if (usc->wait_msg_id != msg_id) 1917 break; 1918 usc->wait_msg_id = 0; 1919 wakeup(&usc->wait_msg_id); 1920 break; 1921 case AR_HTC_MSG_CONN_SVC_RSP: 1922 if (usc->wait_msg_id != msg_id) 1923 break; 1924 if (usc->msg_conn_svc_rsp != NULL) { 1925 memcpy(usc->msg_conn_svc_rsp, &msg[1], 1926 sizeof(struct ar_htc_msg_conn_svc_rsp)); 1927 } 1928 usc->wait_msg_id = 0; 1929 wakeup(&usc->wait_msg_id); 1930 break; 1931 case AR_HTC_MSG_CONF_PIPE_RSP: 1932 if (usc->wait_msg_id != msg_id) 1933 break; 1934 usc->wait_msg_id = 0; 1935 wakeup(&usc->wait_msg_id); 1936 break; 1937 default: 1938 DPRINTF(("HTC message %d ignored\n", msg_id)); 1939 break; 1940 } 1941 } 1942 1943 #if NBPFILTER > 0 1944 void 1945 athn_usb_rx_radiotap(struct athn_softc *sc, struct mbuf *m, 1946 struct ar_rx_status *rs) 1947 { 1948 #define IEEE80211_RADIOTAP_F_SHORTGI 0x80 /* XXX from FBSD */ 1949 1950 struct athn_rx_radiotap_header *tap = &sc->sc_rxtap; 1951 struct ieee80211com *ic = &sc->sc_ic; 1952 struct mbuf mb; 1953 uint8_t rate; 1954 1955 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS; 1956 tap->wr_tsft = htole64(betoh64(rs->rs_tstamp)); 1957 tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq); 1958 tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags); 1959 tap->wr_dbm_antsignal = rs->rs_rssi; 1960 /* XXX noise. */ 1961 tap->wr_antenna = rs->rs_antenna; 1962 tap->wr_rate = 0; /* In case it can't be found below. */ 1963 rate = rs->rs_rate; 1964 if (rate & 0x80) { /* HT. */ 1965 /* Bit 7 set means HT MCS instead of rate. */ 1966 tap->wr_rate = rate; 1967 if (!(rs->rs_flags & AR_RXS_FLAG_GI)) 1968 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI; 1969 1970 } else if (rate & 0x10) { /* CCK. */ 1971 if (rate & 0x04) 1972 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 1973 switch (rate & ~0x14) { 1974 case 0xb: tap->wr_rate = 2; break; 1975 case 0xa: tap->wr_rate = 4; break; 1976 case 0x9: tap->wr_rate = 11; break; 1977 case 0x8: tap->wr_rate = 22; break; 1978 } 1979 } else { /* OFDM. */ 1980 switch (rate) { 1981 case 0xb: tap->wr_rate = 12; break; 1982 case 0xf: tap->wr_rate = 18; break; 1983 case 0xa: tap->wr_rate = 24; break; 1984 case 0xe: tap->wr_rate = 36; break; 1985 case 0x9: tap->wr_rate = 48; break; 1986 case 0xd: tap->wr_rate = 72; break; 1987 case 0x8: tap->wr_rate = 96; break; 1988 case 0xc: tap->wr_rate = 108; break; 1989 } 1990 } 1991 mb.m_data = (caddr_t)tap; 1992 mb.m_len = sc->sc_rxtap_len; 1993 mb.m_next = m; 1994 mb.m_nextpkt = NULL; 1995 mb.m_type = 0; 1996 mb.m_flags = 0; 1997 bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN); 1998 } 1999 #endif 2000 2001 void 2002 athn_usb_rx_frame(struct athn_usb_softc *usc, struct mbuf *m) 2003 { 2004 struct athn_softc *sc = &usc->sc_sc; 2005 struct ieee80211com *ic = &sc->sc_ic; 2006 struct ifnet *ifp = &ic->ic_if; 2007 struct ieee80211_frame *wh; 2008 struct ieee80211_node *ni; 2009 struct ieee80211_rxinfo rxi; 2010 struct ar_htc_frame_hdr *htc; 2011 struct ar_rx_status *rs; 2012 uint16_t datalen; 2013 int s; 2014 2015 if (__predict_false(m->m_len < sizeof(*htc))) 2016 goto skip; 2017 htc = mtod(m, struct ar_htc_frame_hdr *); 2018 if (__predict_false(htc->endpoint_id == 0)) { 2019 DPRINTF(("bad endpoint %d\n", htc->endpoint_id)); 2020 goto skip; 2021 } 2022 if (htc->flags & AR_HTC_FLAG_TRAILER) { 2023 if (m->m_len < htc->control[0]) 2024 goto skip; 2025 m_adj(m, -(int)htc->control[0]); 2026 } 2027 m_adj(m, sizeof(*htc)); /* Strip HTC header. */ 2028 2029 if (__predict_false(m->m_len < sizeof(*rs))) 2030 goto skip; 2031 rs = mtod(m, struct ar_rx_status *); 2032 2033 /* Make sure that payload fits. */ 2034 datalen = betoh16(rs->rs_datalen); 2035 if (__predict_false(m->m_len < sizeof(*rs) + datalen)) 2036 goto skip; 2037 2038 if (__predict_false(datalen < sizeof(*wh) + IEEE80211_CRC_LEN)) 2039 goto skip; 2040 2041 m_adj(m, sizeof(*rs)); /* Strip Rx status. */ 2042 2043 s = splnet(); 2044 2045 /* Grab a reference to the source node. */ 2046 wh = mtod(m, struct ieee80211_frame *); 2047 ni = ieee80211_find_rxnode(ic, wh); 2048 2049 /* Remove any HW padding after the 802.11 header. */ 2050 if (!(wh->i_fc[0] & IEEE80211_FC0_TYPE_CTL)) { 2051 u_int hdrlen = ieee80211_get_hdrlen(wh); 2052 if (hdrlen & 3) { 2053 memmove((caddr_t)wh + 2, wh, hdrlen); 2054 m_adj(m, 2); 2055 } 2056 } 2057 #if NBPFILTER > 0 2058 if (__predict_false(sc->sc_drvbpf != NULL)) 2059 athn_usb_rx_radiotap(sc, m, rs); 2060 #endif 2061 /* Trim 802.11 FCS after radiotap. */ 2062 m_adj(m, -IEEE80211_CRC_LEN); 2063 2064 /* Send the frame to the 802.11 layer. */ 2065 rxi.rxi_flags = 0; 2066 rxi.rxi_rssi = rs->rs_rssi + AR_USB_DEFAULT_NF; 2067 rxi.rxi_tstamp = betoh64(rs->rs_tstamp); 2068 ieee80211_input(ifp, m, ni, &rxi); 2069 2070 /* Node is no longer needed. */ 2071 ieee80211_release_node(ic, ni); 2072 splx(s); 2073 return; 2074 skip: 2075 m_freem(m); 2076 } 2077 2078 void 2079 athn_usb_rxeof(struct usbd_xfer *xfer, void *priv, 2080 usbd_status status) 2081 { 2082 struct athn_usb_rx_data *data = priv; 2083 struct athn_usb_softc *usc = data->sc; 2084 struct athn_softc *sc = &usc->sc_sc; 2085 struct ifnet *ifp = &sc->sc_ic.ic_if; 2086 struct athn_usb_rx_stream *stream = &usc->rx_stream; 2087 uint8_t *buf = data->buf; 2088 struct ar_stream_hdr *hdr; 2089 struct mbuf *m; 2090 uint16_t pktlen; 2091 int off, len; 2092 2093 if (__predict_false(status != USBD_NORMAL_COMPLETION)) { 2094 DPRINTF(("RX status=%d\n", status)); 2095 if (status == USBD_STALLED) 2096 usbd_clear_endpoint_stall_async(usc->rx_data_pipe); 2097 if (status != USBD_CANCELLED) 2098 goto resubmit; 2099 return; 2100 } 2101 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 2102 2103 if (stream->left > 0) { 2104 if (len >= stream->left) { 2105 /* We have all our pktlen bytes now. */ 2106 if (__predict_true(stream->m != NULL)) { 2107 memcpy(mtod(stream->m, uint8_t *) + 2108 stream->moff, buf, stream->left); 2109 athn_usb_rx_frame(usc, stream->m); 2110 stream->m = NULL; 2111 } 2112 /* Next header is 32-bit aligned. */ 2113 off = (stream->left + 3) & ~3; 2114 buf += off; 2115 len -= off; 2116 stream->left = 0; 2117 } else { 2118 /* Still need more bytes, save what we have. */ 2119 if (__predict_true(stream->m != NULL)) { 2120 memcpy(mtod(stream->m, uint8_t *) + 2121 stream->moff, buf, len); 2122 stream->moff += len; 2123 } 2124 stream->left -= len; 2125 goto resubmit; 2126 } 2127 } 2128 KASSERT(stream->left == 0); 2129 while (len >= sizeof(*hdr)) { 2130 hdr = (struct ar_stream_hdr *)buf; 2131 if (hdr->tag != htole16(AR_USB_RX_STREAM_TAG)) { 2132 DPRINTF(("invalid tag 0x%x\n", hdr->tag)); 2133 break; 2134 } 2135 pktlen = letoh16(hdr->len); 2136 buf += sizeof(*hdr); 2137 len -= sizeof(*hdr); 2138 2139 if (__predict_true(pktlen <= MCLBYTES)) { 2140 /* Allocate an mbuf to store the next pktlen bytes. */ 2141 MGETHDR(m, M_DONTWAIT, MT_DATA); 2142 if (__predict_true(m != NULL)) { 2143 m->m_pkthdr.len = m->m_len = pktlen; 2144 if (pktlen > MHLEN) { 2145 MCLGET(m, M_DONTWAIT); 2146 if (!(m->m_flags & M_EXT)) { 2147 m_free(m); 2148 m = NULL; 2149 } 2150 } 2151 } 2152 } else /* Drop frames larger than MCLBYTES. */ 2153 m = NULL; 2154 2155 if (m == NULL) 2156 ifp->if_ierrors++; 2157 2158 /* 2159 * NB: m can be NULL, in which case the next pktlen bytes 2160 * will be discarded from the Rx stream. 2161 */ 2162 if (pktlen > len) { 2163 /* Need more bytes, save what we have. */ 2164 stream->m = m; /* NB: m can be NULL. */ 2165 if (__predict_true(stream->m != NULL)) { 2166 memcpy(mtod(stream->m, uint8_t *), buf, len); 2167 stream->moff = len; 2168 } 2169 stream->left = pktlen - len; 2170 goto resubmit; 2171 } 2172 if (__predict_true(m != NULL)) { 2173 /* We have all the pktlen bytes in this xfer. */ 2174 memcpy(mtod(m, uint8_t *), buf, pktlen); 2175 athn_usb_rx_frame(usc, m); 2176 } 2177 2178 /* Next header is 32-bit aligned. */ 2179 off = (pktlen + 3) & ~3; 2180 buf += off; 2181 len -= off; 2182 } 2183 2184 resubmit: 2185 /* Setup a new transfer. */ 2186 usbd_setup_xfer(xfer, usc->rx_data_pipe, data, data->buf, 2187 ATHN_USB_RXBUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, 2188 USBD_NO_TIMEOUT, athn_usb_rxeof); 2189 (void)usbd_transfer(xfer); 2190 } 2191 2192 void 2193 athn_usb_txeof(struct usbd_xfer *xfer, void *priv, 2194 usbd_status status) 2195 { 2196 struct athn_usb_tx_data *data = priv; 2197 struct athn_usb_softc *usc = data->sc; 2198 struct athn_softc *sc = &usc->sc_sc; 2199 struct ifnet *ifp = &sc->sc_ic.ic_if; 2200 int s; 2201 2202 s = splnet(); 2203 /* Put this Tx buffer back to our free list. */ 2204 TAILQ_INSERT_TAIL(&usc->tx_free_list, data, next); 2205 2206 if (__predict_false(status != USBD_NORMAL_COMPLETION)) { 2207 DPRINTF(("TX status=%d\n", status)); 2208 if (status == USBD_STALLED) 2209 usbd_clear_endpoint_stall_async(usc->tx_data_pipe); 2210 ifp->if_oerrors++; 2211 splx(s); 2212 /* XXX Why return? */ 2213 return; 2214 } 2215 sc->sc_tx_timer = 0; 2216 2217 /* We just released a Tx buffer, notify Tx. */ 2218 if (ifq_is_oactive(&ifp->if_snd)) { 2219 ifq_clr_oactive(&ifp->if_snd); 2220 ifp->if_start(ifp); 2221 } 2222 splx(s); 2223 } 2224 2225 int 2226 athn_usb_tx(struct athn_softc *sc, struct mbuf *m, struct ieee80211_node *ni) 2227 { 2228 struct athn_usb_softc *usc = (struct athn_usb_softc *)sc; 2229 struct athn_node *an = (struct athn_node *)ni; 2230 struct ieee80211com *ic = &sc->sc_ic; 2231 struct ieee80211_frame *wh; 2232 struct ieee80211_key *k = NULL; 2233 struct athn_usb_tx_data *data; 2234 struct ar_stream_hdr *hdr; 2235 struct ar_htc_frame_hdr *htc; 2236 struct ar_tx_frame *txf; 2237 struct ar_tx_mgmt *txm; 2238 uint8_t *frm; 2239 uint16_t qos; 2240 uint8_t qid, tid = 0; 2241 int hasqos, xferlen, error; 2242 2243 wh = mtod(m, struct ieee80211_frame *); 2244 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 2245 k = ieee80211_get_txkey(ic, wh, ni); 2246 if ((m = ieee80211_encrypt(ic, m, k)) == NULL) 2247 return (ENOBUFS); 2248 wh = mtod(m, struct ieee80211_frame *); 2249 } 2250 if ((hasqos = ieee80211_has_qos(wh))) { 2251 qos = ieee80211_get_qos(wh); 2252 tid = qos & IEEE80211_QOS_TID; 2253 qid = ieee80211_up_to_ac(ic, tid); 2254 } else 2255 qid = EDCA_AC_BE; 2256 2257 /* Grab a Tx buffer from our free list. */ 2258 data = TAILQ_FIRST(&usc->tx_free_list); 2259 TAILQ_REMOVE(&usc->tx_free_list, data, next); 2260 2261 #if NBPFILTER > 0 2262 /* XXX Change radiotap Tx header for USB (no txrate). */ 2263 if (__predict_false(sc->sc_drvbpf != NULL)) { 2264 struct athn_tx_radiotap_header *tap = &sc->sc_txtap; 2265 struct mbuf mb; 2266 2267 tap->wt_flags = 0; 2268 tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq); 2269 tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags); 2270 mb.m_data = (caddr_t)tap; 2271 mb.m_len = sc->sc_txtap_len; 2272 mb.m_next = m; 2273 mb.m_nextpkt = NULL; 2274 mb.m_type = 0; 2275 mb.m_flags = 0; 2276 bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT); 2277 } 2278 #endif 2279 2280 /* NB: We don't take advantage of USB Tx stream mode for now. */ 2281 hdr = (struct ar_stream_hdr *)data->buf; 2282 hdr->tag = htole16(AR_USB_TX_STREAM_TAG); 2283 2284 htc = (struct ar_htc_frame_hdr *)&hdr[1]; 2285 memset(htc, 0, sizeof(*htc)); 2286 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 2287 IEEE80211_FC0_TYPE_DATA) { 2288 htc->endpoint_id = usc->ep_data[qid]; 2289 2290 txf = (struct ar_tx_frame *)&htc[1]; 2291 memset(txf, 0, sizeof(*txf)); 2292 txf->data_type = AR_HTC_NORMAL; 2293 txf->node_idx = an->sta_index; 2294 txf->vif_idx = 0; 2295 txf->tid = tid; 2296 if (m->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold) 2297 txf->flags |= htobe32(AR_HTC_TX_RTSCTS); 2298 else if (ic->ic_flags & IEEE80211_F_USEPROT) { 2299 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 2300 txf->flags |= htobe32(AR_HTC_TX_CTSONLY); 2301 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 2302 txf->flags |= htobe32(AR_HTC_TX_RTSCTS); 2303 } 2304 txf->key_idx = 0xff; 2305 txf->cookie = an->sta_index; 2306 frm = (uint8_t *)&txf[1]; 2307 } else { 2308 htc->endpoint_id = usc->ep_mgmt; 2309 2310 txm = (struct ar_tx_mgmt *)&htc[1]; 2311 memset(txm, 0, sizeof(*txm)); 2312 txm->node_idx = an->sta_index; 2313 txm->vif_idx = 0; 2314 txm->key_idx = 0xff; 2315 txm->cookie = an->sta_index; 2316 frm = (uint8_t *)&txm[1]; 2317 } 2318 /* Copy payload. */ 2319 m_copydata(m, 0, m->m_pkthdr.len, (caddr_t)frm); 2320 frm += m->m_pkthdr.len; 2321 m_freem(m); 2322 2323 /* Finalize headers. */ 2324 htc->payload_len = htobe16(frm - (uint8_t *)&htc[1]); 2325 hdr->len = htole16(frm - (uint8_t *)&hdr[1]); 2326 xferlen = frm - data->buf; 2327 2328 usbd_setup_xfer(data->xfer, usc->tx_data_pipe, data, data->buf, 2329 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, ATHN_USB_TX_TIMEOUT, 2330 athn_usb_txeof); 2331 error = usbd_transfer(data->xfer); 2332 if (__predict_false(error != USBD_IN_PROGRESS && error != 0)) { 2333 /* Put this Tx buffer back to our free list. */ 2334 TAILQ_INSERT_TAIL(&usc->tx_free_list, data, next); 2335 return (error); 2336 } 2337 ieee80211_release_node(ic, ni); 2338 return (0); 2339 } 2340 2341 void 2342 athn_usb_start(struct ifnet *ifp) 2343 { 2344 struct athn_softc *sc = ifp->if_softc; 2345 struct athn_usb_softc *usc = (struct athn_usb_softc *)sc; 2346 struct ieee80211com *ic = &sc->sc_ic; 2347 struct ieee80211_node *ni; 2348 struct mbuf *m; 2349 2350 if (!(ifp->if_flags & IFF_RUNNING) || ifq_is_oactive(&ifp->if_snd)) 2351 return; 2352 2353 for (;;) { 2354 if (TAILQ_EMPTY(&usc->tx_free_list)) { 2355 ifq_set_oactive(&ifp->if_snd); 2356 break; 2357 } 2358 /* Send pending management frames first. */ 2359 m = mq_dequeue(&ic->ic_mgtq); 2360 if (m != NULL) { 2361 ni = m->m_pkthdr.ph_cookie; 2362 goto sendit; 2363 } 2364 if (ic->ic_state != IEEE80211_S_RUN) 2365 break; 2366 2367 /* Encapsulate and send data frames. */ 2368 IFQ_DEQUEUE(&ifp->if_snd, m); 2369 if (m == NULL) 2370 break; 2371 #if NBPFILTER > 0 2372 if (ifp->if_bpf != NULL) 2373 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT); 2374 #endif 2375 if ((m = ieee80211_encap(ifp, m, &ni)) == NULL) 2376 continue; 2377 sendit: 2378 #if NBPFILTER > 0 2379 if (ic->ic_rawbpf != NULL) 2380 bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT); 2381 #endif 2382 if (athn_usb_tx(sc, m, ni) != 0) { 2383 ieee80211_release_node(ic, ni); 2384 ifp->if_oerrors++; 2385 continue; 2386 } 2387 2388 sc->sc_tx_timer = 5; 2389 ifp->if_timer = 1; 2390 } 2391 } 2392 2393 void 2394 athn_usb_watchdog(struct ifnet *ifp) 2395 { 2396 struct athn_softc *sc = ifp->if_softc; 2397 2398 ifp->if_timer = 0; 2399 2400 if (sc->sc_tx_timer > 0) { 2401 if (--sc->sc_tx_timer == 0) { 2402 printf("%s: device timeout\n", sc->sc_dev.dv_xname); 2403 /* athn_usb_init(ifp); XXX needs a process context! */ 2404 ifp->if_oerrors++; 2405 return; 2406 } 2407 ifp->if_timer = 1; 2408 } 2409 ieee80211_watchdog(ifp); 2410 } 2411 2412 int 2413 athn_usb_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2414 { 2415 struct athn_softc *sc = ifp->if_softc; 2416 struct athn_usb_softc *usc = (struct athn_usb_softc *)sc; 2417 struct ieee80211com *ic = &sc->sc_ic; 2418 int s, error = 0; 2419 2420 if (usbd_is_dying(usc->sc_udev)) 2421 return ENXIO; 2422 2423 usbd_ref_incr(usc->sc_udev); 2424 2425 s = splnet(); 2426 2427 switch (cmd) { 2428 case SIOCSIFADDR: 2429 ifp->if_flags |= IFF_UP; 2430 /* FALLTHROUGH */ 2431 case SIOCSIFFLAGS: 2432 if (ifp->if_flags & IFF_UP) { 2433 if (!(ifp->if_flags & IFF_RUNNING)) 2434 error = athn_usb_init(ifp); 2435 } else { 2436 if (ifp->if_flags & IFF_RUNNING) 2437 athn_usb_stop(ifp); 2438 } 2439 break; 2440 case SIOCS80211CHANNEL: 2441 error = ieee80211_ioctl(ifp, cmd, data); 2442 if (error == ENETRESET && 2443 ic->ic_opmode == IEEE80211_M_MONITOR) { 2444 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 2445 (IFF_UP | IFF_RUNNING)) { 2446 athn_usb_switch_chan(sc, ic->ic_ibss_chan, 2447 NULL); 2448 } 2449 error = 0; 2450 } 2451 break; 2452 default: 2453 error = ieee80211_ioctl(ifp, cmd, data); 2454 } 2455 2456 if (error == ENETRESET) { 2457 error = 0; 2458 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 2459 (IFF_UP | IFF_RUNNING)) { 2460 athn_usb_stop(ifp); 2461 error = athn_usb_init(ifp); 2462 } 2463 } 2464 splx(s); 2465 2466 usbd_ref_decr(usc->sc_udev); 2467 2468 return (error); 2469 } 2470 2471 int 2472 athn_usb_init(struct ifnet *ifp) 2473 { 2474 struct athn_softc *sc = ifp->if_softc; 2475 struct athn_usb_softc *usc = (struct athn_usb_softc *)sc; 2476 struct athn_ops *ops = &sc->ops; 2477 struct ieee80211com *ic = &sc->sc_ic; 2478 struct ieee80211_channel *c, *extc; 2479 struct athn_usb_rx_data *data; 2480 struct ar_htc_target_vif hvif; 2481 struct ar_htc_target_sta sta; 2482 struct ar_htc_cap_target hic; 2483 uint16_t mode; 2484 int i, error; 2485 2486 /* Init host async commands ring. */ 2487 usc->cmdq.cur = usc->cmdq.next = usc->cmdq.queued = 0; 2488 2489 /* Allocate Tx/Rx buffers. */ 2490 error = athn_usb_alloc_rx_list(usc); 2491 if (error != 0) 2492 goto fail; 2493 error = athn_usb_alloc_tx_list(usc); 2494 if (error != 0) 2495 goto fail; 2496 /* Steal one buffer for beacons. */ 2497 usc->tx_bcn = TAILQ_FIRST(&usc->tx_free_list); 2498 TAILQ_REMOVE(&usc->tx_free_list, usc->tx_bcn, next); 2499 2500 c = ic->ic_bss->ni_chan = ic->ic_ibss_chan; 2501 extc = NULL; 2502 2503 /* In case a new MAC address has been configured. */ 2504 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl)); 2505 2506 error = athn_set_power_awake(sc); 2507 if (error != 0) 2508 goto fail; 2509 2510 error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_FLUSH_RECV); 2511 if (error != 0) 2512 goto fail; 2513 2514 error = athn_hw_reset(sc, c, extc, 1); 2515 if (error != 0) 2516 goto fail; 2517 2518 ops->set_txpower(sc, c, extc); 2519 2520 mode = htobe16(IEEE80211_IS_CHAN_2GHZ(c) ? 2521 AR_HTC_MODE_11NG : AR_HTC_MODE_11NA); 2522 error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_SET_MODE, 2523 &mode, sizeof(mode), NULL); 2524 if (error != 0) 2525 goto fail; 2526 2527 error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_ATH_INIT); 2528 if (error != 0) 2529 goto fail; 2530 2531 error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_START_RECV); 2532 if (error != 0) 2533 goto fail; 2534 2535 athn_rx_start(sc); 2536 2537 /* Create main interface on target. */ 2538 memset(&hvif, 0, sizeof(hvif)); 2539 hvif.index = 0; 2540 IEEE80211_ADDR_COPY(hvif.myaddr, ic->ic_myaddr); 2541 switch (ic->ic_opmode) { 2542 case IEEE80211_M_STA: 2543 hvif.opmode = htobe32(AR_HTC_M_STA); 2544 break; 2545 case IEEE80211_M_MONITOR: 2546 hvif.opmode = htobe32(AR_HTC_M_MONITOR); 2547 break; 2548 #ifndef IEEE80211_STA_ONLY 2549 case IEEE80211_M_IBSS: 2550 hvif.opmode = htobe32(AR_HTC_M_IBSS); 2551 break; 2552 case IEEE80211_M_AHDEMO: 2553 hvif.opmode = htobe32(AR_HTC_M_AHDEMO); 2554 break; 2555 case IEEE80211_M_HOSTAP: 2556 hvif.opmode = htobe32(AR_HTC_M_HOSTAP); 2557 break; 2558 #endif 2559 } 2560 hvif.rtsthreshold = htobe16(ic->ic_rtsthreshold); 2561 DPRINTF(("creating VAP\n")); 2562 error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_VAP_CREATE, 2563 &hvif, sizeof(hvif), NULL); 2564 if (error != 0) 2565 goto fail; 2566 2567 /* Create a fake node to send management frames before assoc. */ 2568 memset(&sta, 0, sizeof(sta)); 2569 IEEE80211_ADDR_COPY(sta.macaddr, ic->ic_myaddr); 2570 sta.sta_index = 0; 2571 sta.is_vif_sta = 1; 2572 sta.vif_index = hvif.index; 2573 sta.maxampdu = 0xffff; 2574 DPRINTF(("creating default node\n")); 2575 error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_NODE_CREATE, 2576 &sta, sizeof(sta), NULL); 2577 if (error != 0) 2578 goto fail; 2579 usc->free_node_slots = ~(1 << sta.sta_index); 2580 2581 /* Update target capabilities. */ 2582 memset(&hic, 0, sizeof(hic)); 2583 hic.ampdu_limit = htobe32(0x0000ffff); 2584 hic.ampdu_subframes = 20; 2585 hic.txchainmask = sc->txchainmask; 2586 DPRINTF(("updating target configuration\n")); 2587 error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_TARGET_IC_UPDATE, 2588 &hic, sizeof(hic), NULL); 2589 if (error != 0) 2590 goto fail; 2591 2592 /* Queue Rx xfers. */ 2593 for (i = 0; i < ATHN_USB_RX_LIST_COUNT; i++) { 2594 data = &usc->rx_data[i]; 2595 2596 usbd_setup_xfer(data->xfer, usc->rx_data_pipe, data, data->buf, 2597 ATHN_USB_RXBUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, 2598 USBD_NO_TIMEOUT, athn_usb_rxeof); 2599 error = usbd_transfer(data->xfer); 2600 if (error != 0 && error != USBD_IN_PROGRESS) 2601 goto fail; 2602 } 2603 /* We're ready to go. */ 2604 ifp->if_flags |= IFF_RUNNING; 2605 ifq_clr_oactive(&ifp->if_snd); 2606 2607 #ifdef notyet 2608 if (ic->ic_flags & IEEE80211_F_WEPON) { 2609 /* Install WEP keys. */ 2610 for (i = 0; i < IEEE80211_WEP_NKID; i++) 2611 athn_usb_set_key(ic, NULL, &ic->ic_nw_keys[i]); 2612 } 2613 #endif 2614 if (ic->ic_opmode == IEEE80211_M_MONITOR) 2615 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 2616 else 2617 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2618 athn_usb_wait_async(usc); 2619 return (0); 2620 fail: 2621 athn_usb_stop(ifp); 2622 return (error); 2623 } 2624 2625 void 2626 athn_usb_stop(struct ifnet *ifp) 2627 { 2628 struct athn_softc *sc = ifp->if_softc; 2629 struct athn_usb_softc *usc = (struct athn_usb_softc *)sc; 2630 struct ieee80211com *ic = &sc->sc_ic; 2631 struct ar_htc_target_vif hvif; 2632 uint8_t sta_index; 2633 int s; 2634 2635 sc->sc_tx_timer = 0; 2636 ifp->if_timer = 0; 2637 ifp->if_flags &= ~IFF_RUNNING; 2638 ifq_clr_oactive(&ifp->if_snd); 2639 2640 s = splusb(); 2641 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2642 2643 /* Wait for all async commands to complete. */ 2644 athn_usb_wait_async(usc); 2645 2646 timeout_del(&sc->scan_to); 2647 timeout_del(&sc->calib_to); 2648 2649 /* Remove all non-default nodes. */ 2650 for (sta_index = 1; sta_index < AR_USB_MAX_STA; sta_index++) { 2651 if (usc->free_node_slots & (1 << sta_index)) 2652 continue; 2653 (void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_NODE_REMOVE, 2654 &sta_index, sizeof(sta_index), NULL); 2655 } 2656 2657 /* Remove main interface. This also invalidates our default node. */ 2658 memset(&hvif, 0, sizeof(hvif)); 2659 hvif.index = 0; 2660 IEEE80211_ADDR_COPY(hvif.myaddr, ic->ic_myaddr); 2661 (void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_VAP_REMOVE, 2662 &hvif, sizeof(hvif), NULL); 2663 2664 usc->free_node_slots = 0xff; 2665 2666 (void)athn_usb_wmi_cmd(usc, AR_WMI_CMD_DISABLE_INTR); 2667 (void)athn_usb_wmi_cmd(usc, AR_WMI_CMD_DRAIN_TXQ_ALL); 2668 (void)athn_usb_wmi_cmd(usc, AR_WMI_CMD_STOP_RECV); 2669 2670 athn_reset(sc, 0); 2671 athn_init_pll(sc, NULL); 2672 athn_set_power_awake(sc); 2673 athn_reset(sc, 1); 2674 athn_init_pll(sc, NULL); 2675 athn_set_power_sleep(sc); 2676 2677 /* Abort Tx/Rx. */ 2678 usbd_abort_pipe(usc->tx_data_pipe); 2679 usbd_abort_pipe(usc->rx_data_pipe); 2680 2681 /* Free Tx/Rx buffers. */ 2682 athn_usb_free_tx_list(usc); 2683 athn_usb_free_rx_list(usc); 2684 splx(s); 2685 2686 /* Flush Rx stream. */ 2687 m_freem(usc->rx_stream.m); 2688 usc->rx_stream.m = NULL; 2689 usc->rx_stream.left = 0; 2690 } 2691