1 /* $OpenBSD: if_rsu.c,v 1.17 2013/04/15 09:23:01 mglocker Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 #include <sys/cdefs.h> 19 __FBSDID("$FreeBSD$"); 20 21 /* 22 * Driver for Realtek RTL8188SU/RTL8191SU/RTL8192SU. 23 * 24 * TODO: 25 * o h/w crypto 26 * o hostap / ibss / mesh 27 * o sensible RSSI levels 28 * o power-save operation 29 */ 30 31 #include "opt_wlan.h" 32 33 #include <sys/param.h> 34 #include <sys/endian.h> 35 #include <sys/sockio.h> 36 #include <sys/malloc.h> 37 #include <sys/mbuf.h> 38 #include <sys/kernel.h> 39 #include <sys/socket.h> 40 #include <sys/systm.h> 41 #include <sys/conf.h> 42 #include <sys/bus.h> 43 #include <sys/rman.h> 44 #include <sys/firmware.h> 45 #include <sys/module.h> 46 47 #include <machine/bus.h> 48 #include <machine/resource.h> 49 50 #include <net/bpf.h> 51 #include <net/if.h> 52 #include <net/if_var.h> 53 #include <net/if_arp.h> 54 #include <net/if_dl.h> 55 #include <net/if_media.h> 56 #include <net/if_types.h> 57 58 #include <netinet/in.h> 59 #include <netinet/in_systm.h> 60 #include <netinet/in_var.h> 61 #include <netinet/if_ether.h> 62 #include <netinet/ip.h> 63 64 #include <net80211/ieee80211_var.h> 65 #include <net80211/ieee80211_regdomain.h> 66 #include <net80211/ieee80211_radiotap.h> 67 68 #include <dev/usb/usb.h> 69 #include <dev/usb/usbdi.h> 70 #include "usbdevs.h" 71 72 #define USB_DEBUG_VAR rsu_debug 73 #include <dev/usb/usb_debug.h> 74 75 #include <dev/usb/wlan/if_rsureg.h> 76 77 #ifdef USB_DEBUG 78 static int rsu_debug = 0; 79 SYSCTL_NODE(_hw_usb, OID_AUTO, rsu, CTLFLAG_RW, 0, "USB rsu"); 80 SYSCTL_INT(_hw_usb_rsu, OID_AUTO, debug, CTLFLAG_RWTUN, &rsu_debug, 0, 81 "Debug level"); 82 #define RSU_DPRINTF(_sc, _flg, ...) \ 83 do \ 84 if (((_flg) == (RSU_DEBUG_ANY)) || (rsu_debug & (_flg))) \ 85 device_printf((_sc)->sc_dev, __VA_ARGS__); \ 86 while (0) 87 #else 88 #define RSU_DPRINTF(_sc, _flg, ...) 89 #endif 90 91 static int rsu_enable_11n = 1; 92 TUNABLE_INT("hw.usb.rsu.enable_11n", &rsu_enable_11n); 93 94 #define RSU_DEBUG_ANY 0xffffffff 95 #define RSU_DEBUG_TX 0x00000001 96 #define RSU_DEBUG_RX 0x00000002 97 #define RSU_DEBUG_RESET 0x00000004 98 #define RSU_DEBUG_CALIB 0x00000008 99 #define RSU_DEBUG_STATE 0x00000010 100 #define RSU_DEBUG_SCAN 0x00000020 101 #define RSU_DEBUG_FWCMD 0x00000040 102 #define RSU_DEBUG_TXDONE 0x00000080 103 #define RSU_DEBUG_FW 0x00000100 104 #define RSU_DEBUG_FWDBG 0x00000200 105 #define RSU_DEBUG_AMPDU 0x00000400 106 107 static const STRUCT_USB_HOST_ID rsu_devs[] = { 108 #define RSU_HT_NOT_SUPPORTED 0 109 #define RSU_HT_SUPPORTED 1 110 #define RSU_DEV_HT(v,p) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \ 111 RSU_HT_SUPPORTED) } 112 #define RSU_DEV(v,p) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \ 113 RSU_HT_NOT_SUPPORTED) } 114 RSU_DEV(ASUS, RTL8192SU), 115 RSU_DEV(AZUREWAVE, RTL8192SU_4), 116 RSU_DEV_HT(ACCTON, RTL8192SU), 117 RSU_DEV_HT(ASUS, USBN10), 118 RSU_DEV_HT(AZUREWAVE, RTL8192SU_1), 119 RSU_DEV_HT(AZUREWAVE, RTL8192SU_2), 120 RSU_DEV_HT(AZUREWAVE, RTL8192SU_3), 121 RSU_DEV_HT(AZUREWAVE, RTL8192SU_5), 122 RSU_DEV_HT(BELKIN, RTL8192SU_1), 123 RSU_DEV_HT(BELKIN, RTL8192SU_2), 124 RSU_DEV_HT(BELKIN, RTL8192SU_3), 125 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_1), 126 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_2), 127 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_3), 128 RSU_DEV_HT(COREGA, RTL8192SU), 129 RSU_DEV_HT(DLINK2, DWA131A1), 130 RSU_DEV_HT(DLINK2, RTL8192SU_1), 131 RSU_DEV_HT(DLINK2, RTL8192SU_2), 132 RSU_DEV_HT(EDIMAX, RTL8192SU_1), 133 RSU_DEV_HT(EDIMAX, RTL8192SU_2), 134 RSU_DEV_HT(EDIMAX, EW7622UMN), 135 RSU_DEV_HT(GUILLEMOT, HWGUN54), 136 RSU_DEV_HT(GUILLEMOT, HWNUM300), 137 RSU_DEV_HT(HAWKING, RTL8192SU_1), 138 RSU_DEV_HT(HAWKING, RTL8192SU_2), 139 RSU_DEV_HT(PLANEX2, GWUSNANO), 140 RSU_DEV_HT(REALTEK, RTL8171), 141 RSU_DEV_HT(REALTEK, RTL8172), 142 RSU_DEV_HT(REALTEK, RTL8173), 143 RSU_DEV_HT(REALTEK, RTL8174), 144 RSU_DEV_HT(REALTEK, RTL8192SU), 145 RSU_DEV_HT(REALTEK, RTL8712), 146 RSU_DEV_HT(REALTEK, RTL8713), 147 RSU_DEV_HT(SENAO, RTL8192SU_1), 148 RSU_DEV_HT(SENAO, RTL8192SU_2), 149 RSU_DEV_HT(SITECOMEU, WL349V1), 150 RSU_DEV_HT(SITECOMEU, WL353), 151 RSU_DEV_HT(SWEEX2, LW154), 152 RSU_DEV_HT(TRENDNET, TEW646UBH), 153 #undef RSU_DEV_HT 154 #undef RSU_DEV 155 }; 156 157 static device_probe_t rsu_match; 158 static device_attach_t rsu_attach; 159 static device_detach_t rsu_detach; 160 static usb_callback_t rsu_bulk_tx_callback_be_bk; 161 static usb_callback_t rsu_bulk_tx_callback_vi_vo; 162 static usb_callback_t rsu_bulk_tx_callback_h2c; 163 static usb_callback_t rsu_bulk_rx_callback; 164 static usb_error_t rsu_do_request(struct rsu_softc *, 165 struct usb_device_request *, void *); 166 static struct ieee80211vap * 167 rsu_vap_create(struct ieee80211com *, const char name[], 168 int, enum ieee80211_opmode, int, const uint8_t bssid[], 169 const uint8_t mac[]); 170 static void rsu_vap_delete(struct ieee80211vap *); 171 static void rsu_scan_start(struct ieee80211com *); 172 static void rsu_scan_end(struct ieee80211com *); 173 static void rsu_set_channel(struct ieee80211com *); 174 static void rsu_update_mcast(struct ieee80211com *); 175 static int rsu_alloc_rx_list(struct rsu_softc *); 176 static void rsu_free_rx_list(struct rsu_softc *); 177 static int rsu_alloc_tx_list(struct rsu_softc *); 178 static void rsu_free_tx_list(struct rsu_softc *); 179 static void rsu_free_list(struct rsu_softc *, struct rsu_data [], int); 180 static struct rsu_data *_rsu_getbuf(struct rsu_softc *); 181 static struct rsu_data *rsu_getbuf(struct rsu_softc *); 182 static void rsu_freebuf(struct rsu_softc *, struct rsu_data *); 183 static int rsu_write_region_1(struct rsu_softc *, uint16_t, uint8_t *, 184 int); 185 static void rsu_write_1(struct rsu_softc *, uint16_t, uint8_t); 186 static void rsu_write_2(struct rsu_softc *, uint16_t, uint16_t); 187 static void rsu_write_4(struct rsu_softc *, uint16_t, uint32_t); 188 static int rsu_read_region_1(struct rsu_softc *, uint16_t, uint8_t *, 189 int); 190 static uint8_t rsu_read_1(struct rsu_softc *, uint16_t); 191 static uint16_t rsu_read_2(struct rsu_softc *, uint16_t); 192 static uint32_t rsu_read_4(struct rsu_softc *, uint16_t); 193 static int rsu_fw_iocmd(struct rsu_softc *, uint32_t); 194 static uint8_t rsu_efuse_read_1(struct rsu_softc *, uint16_t); 195 static int rsu_read_rom(struct rsu_softc *); 196 static int rsu_fw_cmd(struct rsu_softc *, uint8_t, void *, int); 197 static void rsu_calib_task(void *, int); 198 static void rsu_tx_task(void *, int); 199 static int rsu_newstate(struct ieee80211vap *, enum ieee80211_state, int); 200 #ifdef notyet 201 static void rsu_set_key(struct rsu_softc *, const struct ieee80211_key *); 202 static void rsu_delete_key(struct rsu_softc *, const struct ieee80211_key *); 203 #endif 204 static int rsu_site_survey(struct rsu_softc *, struct ieee80211vap *); 205 static int rsu_join_bss(struct rsu_softc *, struct ieee80211_node *); 206 static int rsu_disconnect(struct rsu_softc *); 207 static int rsu_hwrssi_to_rssi(struct rsu_softc *, int hw_rssi); 208 static void rsu_event_survey(struct rsu_softc *, uint8_t *, int); 209 static void rsu_event_join_bss(struct rsu_softc *, uint8_t *, int); 210 static void rsu_rx_event(struct rsu_softc *, uint8_t, uint8_t *, int); 211 static void rsu_rx_multi_event(struct rsu_softc *, uint8_t *, int); 212 #if 0 213 static int8_t rsu_get_rssi(struct rsu_softc *, int, void *); 214 #endif 215 static struct mbuf * rsu_rx_frame(struct rsu_softc *, uint8_t *, int); 216 static struct mbuf * rsu_rx_multi_frame(struct rsu_softc *, uint8_t *, int); 217 static struct mbuf * 218 rsu_rxeof(struct usb_xfer *, struct rsu_data *); 219 static void rsu_txeof(struct usb_xfer *, struct rsu_data *); 220 static int rsu_raw_xmit(struct ieee80211_node *, struct mbuf *, 221 const struct ieee80211_bpf_params *); 222 static void rsu_init(struct rsu_softc *); 223 static int rsu_tx_start(struct rsu_softc *, struct ieee80211_node *, 224 struct mbuf *, struct rsu_data *); 225 static int rsu_transmit(struct ieee80211com *, struct mbuf *); 226 static void rsu_start(struct rsu_softc *); 227 static void _rsu_start(struct rsu_softc *); 228 static void rsu_parent(struct ieee80211com *); 229 static void rsu_stop(struct rsu_softc *); 230 static void rsu_ms_delay(struct rsu_softc *, int); 231 232 static device_method_t rsu_methods[] = { 233 DEVMETHOD(device_probe, rsu_match), 234 DEVMETHOD(device_attach, rsu_attach), 235 DEVMETHOD(device_detach, rsu_detach), 236 237 DEVMETHOD_END 238 }; 239 240 static driver_t rsu_driver = { 241 .name = "rsu", 242 .methods = rsu_methods, 243 .size = sizeof(struct rsu_softc) 244 }; 245 246 static devclass_t rsu_devclass; 247 248 DRIVER_MODULE(rsu, uhub, rsu_driver, rsu_devclass, NULL, 0); 249 MODULE_DEPEND(rsu, wlan, 1, 1, 1); 250 MODULE_DEPEND(rsu, usb, 1, 1, 1); 251 MODULE_DEPEND(rsu, firmware, 1, 1, 1); 252 MODULE_VERSION(rsu, 1); 253 USB_PNP_HOST_INFO(rsu_devs); 254 255 static uint8_t rsu_wme_ac_xfer_map[4] = { 256 [WME_AC_BE] = RSU_BULK_TX_BE_BK, 257 [WME_AC_BK] = RSU_BULK_TX_BE_BK, 258 [WME_AC_VI] = RSU_BULK_TX_VI_VO, 259 [WME_AC_VO] = RSU_BULK_TX_VI_VO, 260 }; 261 262 /* XXX hard-coded */ 263 #define RSU_H2C_ENDPOINT 3 264 265 static const struct usb_config rsu_config[RSU_N_TRANSFER] = { 266 [RSU_BULK_RX] = { 267 .type = UE_BULK, 268 .endpoint = UE_ADDR_ANY, 269 .direction = UE_DIR_IN, 270 .bufsize = RSU_RXBUFSZ, 271 .flags = { 272 .pipe_bof = 1, 273 .short_xfer_ok = 1 274 }, 275 .callback = rsu_bulk_rx_callback 276 }, 277 [RSU_BULK_TX_BE_BK] = { 278 .type = UE_BULK, 279 .endpoint = 0x06, 280 .direction = UE_DIR_OUT, 281 .bufsize = RSU_TXBUFSZ, 282 .flags = { 283 .ext_buffer = 1, 284 .pipe_bof = 1, 285 .force_short_xfer = 1 286 }, 287 .callback = rsu_bulk_tx_callback_be_bk, 288 .timeout = RSU_TX_TIMEOUT 289 }, 290 [RSU_BULK_TX_VI_VO] = { 291 .type = UE_BULK, 292 .endpoint = 0x04, 293 .direction = UE_DIR_OUT, 294 .bufsize = RSU_TXBUFSZ, 295 .flags = { 296 .ext_buffer = 1, 297 .pipe_bof = 1, 298 .force_short_xfer = 1 299 }, 300 .callback = rsu_bulk_tx_callback_vi_vo, 301 .timeout = RSU_TX_TIMEOUT 302 }, 303 [RSU_BULK_TX_H2C] = { 304 .type = UE_BULK, 305 .endpoint = 0x0d, 306 .direction = UE_DIR_OUT, 307 .bufsize = RSU_TXBUFSZ, 308 .flags = { 309 .ext_buffer = 1, 310 .pipe_bof = 1, 311 .short_xfer_ok = 1 312 }, 313 .callback = rsu_bulk_tx_callback_h2c, 314 .timeout = RSU_TX_TIMEOUT 315 }, 316 }; 317 318 static int 319 rsu_match(device_t self) 320 { 321 struct usb_attach_arg *uaa = device_get_ivars(self); 322 323 if (uaa->usb_mode != USB_MODE_HOST || 324 uaa->info.bIfaceIndex != 0 || 325 uaa->info.bConfigIndex != 0) 326 return (ENXIO); 327 328 return (usbd_lookup_id_by_uaa(rsu_devs, sizeof(rsu_devs), uaa)); 329 } 330 331 static int 332 rsu_send_mgmt(struct ieee80211_node *ni, int type, int arg) 333 { 334 335 return (ENOTSUP); 336 } 337 338 static void 339 rsu_update_chw(struct ieee80211com *ic) 340 { 341 342 } 343 344 /* 345 * notification from net80211 that it'd like to do A-MPDU on the given TID. 346 * 347 * Note: this actually hangs traffic at the present moment, so don't use it. 348 * The firmware debug does indiciate it's sending and establishing a TX AMPDU 349 * session, but then no traffic flows. 350 */ 351 static int 352 rsu_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap) 353 { 354 #if 0 355 struct rsu_softc *sc = ni->ni_ic->ic_softc; 356 struct r92s_add_ba_req req; 357 358 /* Don't enable if it's requested or running */ 359 if (IEEE80211_AMPDU_REQUESTED(tap)) 360 return (0); 361 if (IEEE80211_AMPDU_RUNNING(tap)) 362 return (0); 363 364 /* We've decided to send addba; so send it */ 365 req.tid = htole32(tap->txa_tid); 366 367 /* Attempt net80211 state */ 368 if (ieee80211_ampdu_tx_request_ext(ni, tap->txa_tid) != 1) 369 return (0); 370 371 /* Send the firmware command */ 372 RSU_DPRINTF(sc, RSU_DEBUG_AMPDU, "%s: establishing AMPDU TX for TID %d\n", 373 __func__, 374 tap->txa_tid); 375 376 RSU_LOCK(sc); 377 if (rsu_fw_cmd(sc, R92S_CMD_ADDBA_REQ, &req, sizeof(req)) != 1) { 378 RSU_UNLOCK(sc); 379 /* Mark failure */ 380 (void) ieee80211_ampdu_tx_request_active_ext(ni, tap->txa_tid, 0); 381 return (0); 382 } 383 RSU_UNLOCK(sc); 384 385 /* Mark success; we don't get any further notifications */ 386 (void) ieee80211_ampdu_tx_request_active_ext(ni, tap->txa_tid, 1); 387 #endif 388 /* Return 0, we're driving this ourselves */ 389 return (0); 390 } 391 392 static int 393 rsu_wme_update(struct ieee80211com *ic) 394 { 395 396 /* Firmware handles this; not our problem */ 397 return (0); 398 } 399 400 static int 401 rsu_attach(device_t self) 402 { 403 struct usb_attach_arg *uaa = device_get_ivars(self); 404 struct rsu_softc *sc = device_get_softc(self); 405 struct ieee80211com *ic = &sc->sc_ic; 406 int error; 407 uint8_t bands[howmany(IEEE80211_MODE_MAX, 8)]; 408 uint8_t iface_index; 409 struct usb_interface *iface; 410 const char *rft; 411 412 device_set_usb_desc(self); 413 sc->sc_udev = uaa->device; 414 sc->sc_dev = self; 415 if (rsu_enable_11n) 416 sc->sc_ht = !! (USB_GET_DRIVER_INFO(uaa) & RSU_HT_SUPPORTED); 417 418 /* Get number of endpoints */ 419 iface = usbd_get_iface(sc->sc_udev, 0); 420 sc->sc_nendpoints = iface->idesc->bNumEndpoints; 421 422 /* Endpoints are hard-coded for now, so enforce 4-endpoint only */ 423 if (sc->sc_nendpoints != 4) { 424 device_printf(sc->sc_dev, 425 "the driver currently only supports 4-endpoint devices\n"); 426 return (ENXIO); 427 } 428 429 mtx_init(&sc->sc_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK, 430 MTX_DEF); 431 TIMEOUT_TASK_INIT(taskqueue_thread, &sc->calib_task, 0, 432 rsu_calib_task, sc); 433 TASK_INIT(&sc->tx_task, 0, rsu_tx_task, sc); 434 mbufq_init(&sc->sc_snd, ifqmaxlen); 435 436 /* Allocate Tx/Rx buffers. */ 437 error = rsu_alloc_rx_list(sc); 438 if (error != 0) { 439 device_printf(sc->sc_dev, "could not allocate Rx buffers\n"); 440 goto fail_usb; 441 } 442 443 error = rsu_alloc_tx_list(sc); 444 if (error != 0) { 445 device_printf(sc->sc_dev, "could not allocate Tx buffers\n"); 446 rsu_free_rx_list(sc); 447 goto fail_usb; 448 } 449 450 iface_index = 0; 451 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer, 452 rsu_config, RSU_N_TRANSFER, sc, &sc->sc_mtx); 453 if (error) { 454 device_printf(sc->sc_dev, 455 "could not allocate USB transfers, err=%s\n", 456 usbd_errstr(error)); 457 goto fail_usb; 458 } 459 RSU_LOCK(sc); 460 /* Read chip revision. */ 461 sc->cut = MS(rsu_read_4(sc, R92S_PMC_FSM), R92S_PMC_FSM_CUT); 462 if (sc->cut != 3) 463 sc->cut = (sc->cut >> 1) + 1; 464 error = rsu_read_rom(sc); 465 RSU_UNLOCK(sc); 466 if (error != 0) { 467 device_printf(self, "could not read ROM\n"); 468 goto fail_rom; 469 } 470 471 /* Figure out TX/RX streams */ 472 switch (sc->rom[84]) { 473 case 0x0: 474 sc->sc_rftype = RTL8712_RFCONFIG_1T1R; 475 sc->sc_nrxstream = 1; 476 sc->sc_ntxstream = 1; 477 rft = "1T1R"; 478 break; 479 case 0x1: 480 sc->sc_rftype = RTL8712_RFCONFIG_1T2R; 481 sc->sc_nrxstream = 2; 482 sc->sc_ntxstream = 1; 483 rft = "1T2R"; 484 break; 485 case 0x2: 486 sc->sc_rftype = RTL8712_RFCONFIG_2T2R; 487 sc->sc_nrxstream = 2; 488 sc->sc_ntxstream = 2; 489 rft = "2T2R"; 490 break; 491 default: 492 device_printf(sc->sc_dev, 493 "%s: unknown board type (rfconfig=0x%02x)\n", 494 __func__, 495 sc->rom[84]); 496 goto fail_rom; 497 } 498 499 IEEE80211_ADDR_COPY(ic->ic_macaddr, &sc->rom[0x12]); 500 device_printf(self, "MAC/BB RTL8712 cut %d %s\n", sc->cut, rft); 501 502 ic->ic_softc = sc; 503 ic->ic_name = device_get_nameunit(self); 504 ic->ic_phytype = IEEE80211_T_OFDM; /* Not only, but not used. */ 505 ic->ic_opmode = IEEE80211_M_STA; /* Default to BSS mode. */ 506 507 /* Set device capabilities. */ 508 ic->ic_caps = 509 IEEE80211_C_STA | /* station mode */ 510 #if 0 511 IEEE80211_C_BGSCAN | /* Background scan. */ 512 #endif 513 IEEE80211_C_SHPREAMBLE | /* Short preamble supported. */ 514 IEEE80211_C_WME | /* WME/QoS */ 515 IEEE80211_C_SHSLOT | /* Short slot time supported. */ 516 IEEE80211_C_WPA; /* WPA/RSN. */ 517 518 /* Check if HT support is present. */ 519 if (sc->sc_ht) { 520 device_printf(sc->sc_dev, "%s: enabling 11n\n", __func__); 521 522 /* Enable basic HT */ 523 ic->ic_htcaps = IEEE80211_HTC_HT | 524 IEEE80211_HTC_AMPDU | 525 IEEE80211_HTC_AMSDU | 526 IEEE80211_HTCAP_MAXAMSDU_3839 | 527 IEEE80211_HTCAP_SMPS_OFF; 528 ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40; 529 530 /* set number of spatial streams */ 531 ic->ic_txstream = sc->sc_ntxstream; 532 ic->ic_rxstream = sc->sc_nrxstream; 533 } 534 535 /* Set supported .11b and .11g rates. */ 536 memset(bands, 0, sizeof(bands)); 537 setbit(bands, IEEE80211_MODE_11B); 538 setbit(bands, IEEE80211_MODE_11G); 539 if (sc->sc_ht) 540 setbit(bands, IEEE80211_MODE_11NG); 541 ieee80211_init_channels(ic, NULL, bands); 542 543 ieee80211_ifattach(ic); 544 ic->ic_raw_xmit = rsu_raw_xmit; 545 ic->ic_scan_start = rsu_scan_start; 546 ic->ic_scan_end = rsu_scan_end; 547 ic->ic_set_channel = rsu_set_channel; 548 ic->ic_vap_create = rsu_vap_create; 549 ic->ic_vap_delete = rsu_vap_delete; 550 ic->ic_update_mcast = rsu_update_mcast; 551 ic->ic_parent = rsu_parent; 552 ic->ic_transmit = rsu_transmit; 553 ic->ic_send_mgmt = rsu_send_mgmt; 554 ic->ic_update_chw = rsu_update_chw; 555 ic->ic_ampdu_enable = rsu_ampdu_enable; 556 ic->ic_wme.wme_update = rsu_wme_update; 557 558 ieee80211_radiotap_attach(ic, &sc->sc_txtap.wt_ihdr, 559 sizeof(sc->sc_txtap), RSU_TX_RADIOTAP_PRESENT, 560 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 561 RSU_RX_RADIOTAP_PRESENT); 562 563 if (bootverbose) 564 ieee80211_announce(ic); 565 566 return (0); 567 568 fail_rom: 569 usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER); 570 fail_usb: 571 mtx_destroy(&sc->sc_mtx); 572 return (ENXIO); 573 } 574 575 static int 576 rsu_detach(device_t self) 577 { 578 struct rsu_softc *sc = device_get_softc(self); 579 struct ieee80211com *ic = &sc->sc_ic; 580 581 RSU_LOCK(sc); 582 rsu_stop(sc); 583 RSU_UNLOCK(sc); 584 585 usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER); 586 587 /* 588 * Free buffers /before/ we detach from net80211, else node 589 * references to destroyed vaps will lead to a panic. 590 */ 591 /* Free Tx/Rx buffers. */ 592 RSU_LOCK(sc); 593 rsu_free_tx_list(sc); 594 rsu_free_rx_list(sc); 595 RSU_UNLOCK(sc); 596 597 /* Frames are freed; detach from net80211 */ 598 ieee80211_ifdetach(ic); 599 600 taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task); 601 taskqueue_drain(taskqueue_thread, &sc->tx_task); 602 603 mtx_destroy(&sc->sc_mtx); 604 605 return (0); 606 } 607 608 static usb_error_t 609 rsu_do_request(struct rsu_softc *sc, struct usb_device_request *req, 610 void *data) 611 { 612 usb_error_t err; 613 int ntries = 10; 614 615 RSU_ASSERT_LOCKED(sc); 616 617 while (ntries--) { 618 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, 619 req, data, 0, NULL, 250 /* ms */); 620 if (err == 0 || err == USB_ERR_NOT_CONFIGURED) 621 break; 622 DPRINTFN(1, "Control request failed, %s (retrying)\n", 623 usbd_errstr(err)); 624 rsu_ms_delay(sc, 10); 625 } 626 627 return (err); 628 } 629 630 static struct ieee80211vap * 631 rsu_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 632 enum ieee80211_opmode opmode, int flags, 633 const uint8_t bssid[IEEE80211_ADDR_LEN], 634 const uint8_t mac[IEEE80211_ADDR_LEN]) 635 { 636 struct rsu_vap *uvp; 637 struct ieee80211vap *vap; 638 639 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 640 return (NULL); 641 642 uvp = malloc(sizeof(struct rsu_vap), M_80211_VAP, M_WAITOK | M_ZERO); 643 vap = &uvp->vap; 644 645 if (ieee80211_vap_setup(ic, vap, name, unit, opmode, 646 flags, bssid) != 0) { 647 /* out of memory */ 648 free(uvp, M_80211_VAP); 649 return (NULL); 650 } 651 652 /* override state transition machine */ 653 uvp->newstate = vap->iv_newstate; 654 vap->iv_newstate = rsu_newstate; 655 656 /* Limits from the r92su driver */ 657 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_16; 658 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_32K; 659 660 /* complete setup */ 661 ieee80211_vap_attach(vap, ieee80211_media_change, 662 ieee80211_media_status, mac); 663 ic->ic_opmode = opmode; 664 665 return (vap); 666 } 667 668 static void 669 rsu_vap_delete(struct ieee80211vap *vap) 670 { 671 struct rsu_vap *uvp = RSU_VAP(vap); 672 673 ieee80211_vap_detach(vap); 674 free(uvp, M_80211_VAP); 675 } 676 677 static void 678 rsu_scan_start(struct ieee80211com *ic) 679 { 680 struct rsu_softc *sc = ic->ic_softc; 681 int error; 682 683 /* Scanning is done by the firmware. */ 684 RSU_LOCK(sc); 685 /* XXX TODO: force awake if in in network-sleep? */ 686 error = rsu_site_survey(sc, TAILQ_FIRST(&ic->ic_vaps)); 687 RSU_UNLOCK(sc); 688 if (error != 0) 689 device_printf(sc->sc_dev, 690 "could not send site survey command\n"); 691 } 692 693 static void 694 rsu_scan_end(struct ieee80211com *ic) 695 { 696 /* Nothing to do here. */ 697 } 698 699 static void 700 rsu_set_channel(struct ieee80211com *ic __unused) 701 { 702 /* We are unable to switch channels, yet. */ 703 } 704 705 static void 706 rsu_update_mcast(struct ieee80211com *ic) 707 { 708 /* XXX do nothing? */ 709 } 710 711 static int 712 rsu_alloc_list(struct rsu_softc *sc, struct rsu_data data[], 713 int ndata, int maxsz) 714 { 715 int i, error; 716 717 for (i = 0; i < ndata; i++) { 718 struct rsu_data *dp = &data[i]; 719 dp->sc = sc; 720 dp->m = NULL; 721 dp->buf = malloc(maxsz, M_USBDEV, M_NOWAIT); 722 if (dp->buf == NULL) { 723 device_printf(sc->sc_dev, 724 "could not allocate buffer\n"); 725 error = ENOMEM; 726 goto fail; 727 } 728 dp->ni = NULL; 729 } 730 731 return (0); 732 fail: 733 rsu_free_list(sc, data, ndata); 734 return (error); 735 } 736 737 static int 738 rsu_alloc_rx_list(struct rsu_softc *sc) 739 { 740 int error, i; 741 742 error = rsu_alloc_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT, 743 RSU_RXBUFSZ); 744 if (error != 0) 745 return (error); 746 747 STAILQ_INIT(&sc->sc_rx_active); 748 STAILQ_INIT(&sc->sc_rx_inactive); 749 750 for (i = 0; i < RSU_RX_LIST_COUNT; i++) 751 STAILQ_INSERT_HEAD(&sc->sc_rx_inactive, &sc->sc_rx[i], next); 752 753 return (0); 754 } 755 756 static int 757 rsu_alloc_tx_list(struct rsu_softc *sc) 758 { 759 int error, i; 760 761 error = rsu_alloc_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT, 762 RSU_TXBUFSZ); 763 if (error != 0) 764 return (error); 765 766 STAILQ_INIT(&sc->sc_tx_inactive); 767 768 for (i = 0; i != RSU_N_TRANSFER; i++) { 769 STAILQ_INIT(&sc->sc_tx_active[i]); 770 STAILQ_INIT(&sc->sc_tx_pending[i]); 771 } 772 773 for (i = 0; i < RSU_TX_LIST_COUNT; i++) { 774 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, &sc->sc_tx[i], next); 775 } 776 777 return (0); 778 } 779 780 static void 781 rsu_free_tx_list(struct rsu_softc *sc) 782 { 783 int i; 784 785 /* prevent further allocations from TX list(s) */ 786 STAILQ_INIT(&sc->sc_tx_inactive); 787 788 for (i = 0; i != RSU_N_TRANSFER; i++) { 789 STAILQ_INIT(&sc->sc_tx_active[i]); 790 STAILQ_INIT(&sc->sc_tx_pending[i]); 791 } 792 793 rsu_free_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT); 794 } 795 796 static void 797 rsu_free_rx_list(struct rsu_softc *sc) 798 { 799 /* prevent further allocations from RX list(s) */ 800 STAILQ_INIT(&sc->sc_rx_inactive); 801 STAILQ_INIT(&sc->sc_rx_active); 802 803 rsu_free_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT); 804 } 805 806 static void 807 rsu_free_list(struct rsu_softc *sc, struct rsu_data data[], int ndata) 808 { 809 int i; 810 811 for (i = 0; i < ndata; i++) { 812 struct rsu_data *dp = &data[i]; 813 814 if (dp->buf != NULL) { 815 free(dp->buf, M_USBDEV); 816 dp->buf = NULL; 817 } 818 if (dp->ni != NULL) { 819 ieee80211_free_node(dp->ni); 820 dp->ni = NULL; 821 } 822 } 823 } 824 825 static struct rsu_data * 826 _rsu_getbuf(struct rsu_softc *sc) 827 { 828 struct rsu_data *bf; 829 830 bf = STAILQ_FIRST(&sc->sc_tx_inactive); 831 if (bf != NULL) 832 STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next); 833 else 834 bf = NULL; 835 return (bf); 836 } 837 838 static struct rsu_data * 839 rsu_getbuf(struct rsu_softc *sc) 840 { 841 struct rsu_data *bf; 842 843 RSU_ASSERT_LOCKED(sc); 844 845 bf = _rsu_getbuf(sc); 846 if (bf == NULL) { 847 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: no buffers\n", __func__); 848 } 849 return (bf); 850 } 851 852 static void 853 rsu_freebuf(struct rsu_softc *sc, struct rsu_data *bf) 854 { 855 856 RSU_ASSERT_LOCKED(sc); 857 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, bf, next); 858 } 859 860 static int 861 rsu_write_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf, 862 int len) 863 { 864 usb_device_request_t req; 865 866 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 867 req.bRequest = R92S_REQ_REGS; 868 USETW(req.wValue, addr); 869 USETW(req.wIndex, 0); 870 USETW(req.wLength, len); 871 872 return (rsu_do_request(sc, &req, buf)); 873 } 874 875 static void 876 rsu_write_1(struct rsu_softc *sc, uint16_t addr, uint8_t val) 877 { 878 rsu_write_region_1(sc, addr, &val, 1); 879 } 880 881 static void 882 rsu_write_2(struct rsu_softc *sc, uint16_t addr, uint16_t val) 883 { 884 val = htole16(val); 885 rsu_write_region_1(sc, addr, (uint8_t *)&val, 2); 886 } 887 888 static void 889 rsu_write_4(struct rsu_softc *sc, uint16_t addr, uint32_t val) 890 { 891 val = htole32(val); 892 rsu_write_region_1(sc, addr, (uint8_t *)&val, 4); 893 } 894 895 static int 896 rsu_read_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf, 897 int len) 898 { 899 usb_device_request_t req; 900 901 req.bmRequestType = UT_READ_VENDOR_DEVICE; 902 req.bRequest = R92S_REQ_REGS; 903 USETW(req.wValue, addr); 904 USETW(req.wIndex, 0); 905 USETW(req.wLength, len); 906 907 return (rsu_do_request(sc, &req, buf)); 908 } 909 910 static uint8_t 911 rsu_read_1(struct rsu_softc *sc, uint16_t addr) 912 { 913 uint8_t val; 914 915 if (rsu_read_region_1(sc, addr, &val, 1) != 0) 916 return (0xff); 917 return (val); 918 } 919 920 static uint16_t 921 rsu_read_2(struct rsu_softc *sc, uint16_t addr) 922 { 923 uint16_t val; 924 925 if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 2) != 0) 926 return (0xffff); 927 return (le16toh(val)); 928 } 929 930 static uint32_t 931 rsu_read_4(struct rsu_softc *sc, uint16_t addr) 932 { 933 uint32_t val; 934 935 if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 4) != 0) 936 return (0xffffffff); 937 return (le32toh(val)); 938 } 939 940 static int 941 rsu_fw_iocmd(struct rsu_softc *sc, uint32_t iocmd) 942 { 943 int ntries; 944 945 rsu_write_4(sc, R92S_IOCMD_CTRL, iocmd); 946 rsu_ms_delay(sc, 1); 947 for (ntries = 0; ntries < 50; ntries++) { 948 if (rsu_read_4(sc, R92S_IOCMD_CTRL) == 0) 949 return (0); 950 rsu_ms_delay(sc, 1); 951 } 952 return (ETIMEDOUT); 953 } 954 955 static uint8_t 956 rsu_efuse_read_1(struct rsu_softc *sc, uint16_t addr) 957 { 958 uint32_t reg; 959 int ntries; 960 961 reg = rsu_read_4(sc, R92S_EFUSE_CTRL); 962 reg = RW(reg, R92S_EFUSE_CTRL_ADDR, addr); 963 reg &= ~R92S_EFUSE_CTRL_VALID; 964 rsu_write_4(sc, R92S_EFUSE_CTRL, reg); 965 /* Wait for read operation to complete. */ 966 for (ntries = 0; ntries < 100; ntries++) { 967 reg = rsu_read_4(sc, R92S_EFUSE_CTRL); 968 if (reg & R92S_EFUSE_CTRL_VALID) 969 return (MS(reg, R92S_EFUSE_CTRL_DATA)); 970 rsu_ms_delay(sc, 1); 971 } 972 device_printf(sc->sc_dev, 973 "could not read efuse byte at address 0x%x\n", addr); 974 return (0xff); 975 } 976 977 static int 978 rsu_read_rom(struct rsu_softc *sc) 979 { 980 uint8_t *rom = sc->rom; 981 uint16_t addr = 0; 982 uint32_t reg; 983 uint8_t off, msk; 984 int i; 985 986 /* Make sure that ROM type is eFuse and that autoload succeeded. */ 987 reg = rsu_read_1(sc, R92S_EE_9346CR); 988 if ((reg & (R92S_9356SEL | R92S_EEPROM_EN)) != R92S_EEPROM_EN) 989 return (EIO); 990 991 /* Turn on 2.5V to prevent eFuse leakage. */ 992 reg = rsu_read_1(sc, R92S_EFUSE_TEST + 3); 993 rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg | 0x80); 994 rsu_ms_delay(sc, 1); 995 rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg & ~0x80); 996 997 /* Read full ROM image. */ 998 memset(&sc->rom, 0xff, sizeof(sc->rom)); 999 while (addr < 512) { 1000 reg = rsu_efuse_read_1(sc, addr); 1001 if (reg == 0xff) 1002 break; 1003 addr++; 1004 off = reg >> 4; 1005 msk = reg & 0xf; 1006 for (i = 0; i < 4; i++) { 1007 if (msk & (1 << i)) 1008 continue; 1009 rom[off * 8 + i * 2 + 0] = 1010 rsu_efuse_read_1(sc, addr); 1011 addr++; 1012 rom[off * 8 + i * 2 + 1] = 1013 rsu_efuse_read_1(sc, addr); 1014 addr++; 1015 } 1016 } 1017 #ifdef USB_DEBUG 1018 if (rsu_debug >= 5) { 1019 /* Dump ROM content. */ 1020 printf("\n"); 1021 for (i = 0; i < sizeof(sc->rom); i++) 1022 printf("%02x:", rom[i]); 1023 printf("\n"); 1024 } 1025 #endif 1026 return (0); 1027 } 1028 1029 static int 1030 rsu_fw_cmd(struct rsu_softc *sc, uint8_t code, void *buf, int len) 1031 { 1032 const uint8_t which = RSU_H2C_ENDPOINT; 1033 struct rsu_data *data; 1034 struct r92s_tx_desc *txd; 1035 struct r92s_fw_cmd_hdr *cmd; 1036 int cmdsz; 1037 int xferlen; 1038 1039 RSU_ASSERT_LOCKED(sc); 1040 1041 data = rsu_getbuf(sc); 1042 if (data == NULL) 1043 return (ENOMEM); 1044 1045 /* Blank the entire payload, just to be safe */ 1046 memset(data->buf, '\0', RSU_TXBUFSZ); 1047 1048 /* Round-up command length to a multiple of 8 bytes. */ 1049 /* XXX TODO: is this required? */ 1050 cmdsz = (len + 7) & ~7; 1051 1052 xferlen = sizeof(*txd) + sizeof(*cmd) + cmdsz; 1053 KASSERT(xferlen <= RSU_TXBUFSZ, ("%s: invalid length", __func__)); 1054 memset(data->buf, 0, xferlen); 1055 1056 /* Setup Tx descriptor. */ 1057 txd = (struct r92s_tx_desc *)data->buf; 1058 txd->txdw0 = htole32( 1059 SM(R92S_TXDW0_OFFSET, sizeof(*txd)) | 1060 SM(R92S_TXDW0_PKTLEN, sizeof(*cmd) + cmdsz) | 1061 R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG); 1062 txd->txdw1 = htole32(SM(R92S_TXDW1_QSEL, R92S_TXDW1_QSEL_H2C)); 1063 1064 /* Setup command header. */ 1065 cmd = (struct r92s_fw_cmd_hdr *)&txd[1]; 1066 cmd->len = htole16(cmdsz); 1067 cmd->code = code; 1068 cmd->seq = sc->cmd_seq; 1069 sc->cmd_seq = (sc->cmd_seq + 1) & 0x7f; 1070 1071 /* Copy command payload. */ 1072 memcpy(&cmd[1], buf, len); 1073 1074 RSU_DPRINTF(sc, RSU_DEBUG_TX | RSU_DEBUG_FWCMD, 1075 "%s: Tx cmd code=0x%x len=0x%x\n", 1076 __func__, code, cmdsz); 1077 data->buflen = xferlen; 1078 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next); 1079 usbd_transfer_start(sc->sc_xfer[which]); 1080 1081 return (0); 1082 } 1083 1084 /* ARGSUSED */ 1085 static void 1086 rsu_calib_task(void *arg, int pending __unused) 1087 { 1088 struct rsu_softc *sc = arg; 1089 #ifdef notyet 1090 uint32_t reg; 1091 #endif 1092 1093 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "%s: running calibration task\n", 1094 __func__); 1095 1096 RSU_LOCK(sc); 1097 #ifdef notyet 1098 /* Read WPS PBC status. */ 1099 rsu_write_1(sc, R92S_MAC_PINMUX_CTRL, 1100 R92S_GPIOMUX_EN | SM(R92S_GPIOSEL_GPIO, R92S_GPIOSEL_GPIO_JTAG)); 1101 rsu_write_1(sc, R92S_GPIO_IO_SEL, 1102 rsu_read_1(sc, R92S_GPIO_IO_SEL) & ~R92S_GPIO_WPS); 1103 reg = rsu_read_1(sc, R92S_GPIO_CTRL); 1104 if (reg != 0xff && (reg & R92S_GPIO_WPS)) 1105 DPRINTF(("WPS PBC is pushed\n")); 1106 #endif 1107 /* Read current signal level. */ 1108 if (rsu_fw_iocmd(sc, 0xf4000001) == 0) { 1109 sc->sc_currssi = rsu_read_4(sc, R92S_IOCMD_DATA); 1110 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "%s: RSSI=%d (%d)\n", 1111 __func__, sc->sc_currssi, 1112 rsu_hwrssi_to_rssi(sc, sc->sc_currssi)); 1113 } 1114 if (sc->sc_calibrating) 1115 taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task, hz); 1116 RSU_UNLOCK(sc); 1117 } 1118 1119 static void 1120 rsu_tx_task(void *arg, int pending __unused) 1121 { 1122 struct rsu_softc *sc = arg; 1123 1124 RSU_LOCK(sc); 1125 _rsu_start(sc); 1126 RSU_UNLOCK(sc); 1127 } 1128 1129 #define RSU_PWR_UNKNOWN 0x0 1130 #define RSU_PWR_ACTIVE 0x1 1131 #define RSU_PWR_OFF 0x2 1132 #define RSU_PWR_SLEEP 0x3 1133 1134 /* 1135 * Set the current power state. 1136 * 1137 * The rtlwifi code doesn't do this so aggressively; it 1138 * waits for an idle period after association with 1139 * no traffic before doing this. 1140 * 1141 * For now - it's on in all states except RUN, and 1142 * in RUN it'll transition to allow sleep. 1143 */ 1144 1145 struct r92s_pwr_cmd { 1146 uint8_t mode; 1147 uint8_t smart_ps; 1148 uint8_t bcn_pass_time; 1149 }; 1150 1151 static int 1152 rsu_set_fw_power_state(struct rsu_softc *sc, int state) 1153 { 1154 struct r92s_set_pwr_mode cmd; 1155 //struct r92s_pwr_cmd cmd; 1156 int error; 1157 1158 RSU_ASSERT_LOCKED(sc); 1159 1160 /* only change state if required */ 1161 if (sc->sc_curpwrstate == state) 1162 return (0); 1163 1164 memset(&cmd, 0, sizeof(cmd)); 1165 1166 switch (state) { 1167 case RSU_PWR_ACTIVE: 1168 /* Force the hardware awake */ 1169 rsu_write_1(sc, R92S_USB_HRPWM, 1170 R92S_USB_HRPWM_PS_ST_ACTIVE | R92S_USB_HRPWM_PS_ALL_ON); 1171 cmd.mode = R92S_PS_MODE_ACTIVE; 1172 break; 1173 case RSU_PWR_SLEEP: 1174 cmd.mode = R92S_PS_MODE_DTIM; /* XXX configurable? */ 1175 cmd.smart_ps = 1; /* XXX 2 if doing p2p */ 1176 cmd.bcn_pass_time = 5; /* in 100mS usb.c, linux/rtlwifi */ 1177 break; 1178 case RSU_PWR_OFF: 1179 cmd.mode = R92S_PS_MODE_RADIOOFF; 1180 break; 1181 default: 1182 device_printf(sc->sc_dev, "%s: unknown ps mode (%d)\n", 1183 __func__, 1184 state); 1185 return (ENXIO); 1186 } 1187 1188 RSU_DPRINTF(sc, RSU_DEBUG_RESET, 1189 "%s: setting ps mode to %d (mode %d)\n", 1190 __func__, state, cmd.mode); 1191 error = rsu_fw_cmd(sc, R92S_CMD_SET_PWR_MODE, &cmd, sizeof(cmd)); 1192 if (error == 0) 1193 sc->sc_curpwrstate = state; 1194 1195 return (error); 1196 } 1197 1198 static int 1199 rsu_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1200 { 1201 struct rsu_vap *uvp = RSU_VAP(vap); 1202 struct ieee80211com *ic = vap->iv_ic; 1203 struct rsu_softc *sc = ic->ic_softc; 1204 struct ieee80211_node *ni; 1205 struct ieee80211_rateset *rs; 1206 enum ieee80211_state ostate; 1207 int error, startcal = 0; 1208 1209 ostate = vap->iv_state; 1210 RSU_DPRINTF(sc, RSU_DEBUG_STATE, "%s: %s -> %s\n", 1211 __func__, 1212 ieee80211_state_name[ostate], 1213 ieee80211_state_name[nstate]); 1214 1215 IEEE80211_UNLOCK(ic); 1216 if (ostate == IEEE80211_S_RUN) { 1217 RSU_LOCK(sc); 1218 /* Stop calibration. */ 1219 sc->sc_calibrating = 0; 1220 RSU_UNLOCK(sc); 1221 taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task); 1222 taskqueue_drain(taskqueue_thread, &sc->tx_task); 1223 /* Disassociate from our current BSS. */ 1224 RSU_LOCK(sc); 1225 rsu_disconnect(sc); 1226 } else 1227 RSU_LOCK(sc); 1228 switch (nstate) { 1229 case IEEE80211_S_INIT: 1230 (void) rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE); 1231 break; 1232 case IEEE80211_S_AUTH: 1233 ni = ieee80211_ref_node(vap->iv_bss); 1234 (void) rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE); 1235 error = rsu_join_bss(sc, ni); 1236 ieee80211_free_node(ni); 1237 if (error != 0) { 1238 device_printf(sc->sc_dev, 1239 "could not send join command\n"); 1240 } 1241 break; 1242 case IEEE80211_S_RUN: 1243 ni = ieee80211_ref_node(vap->iv_bss); 1244 rs = &ni->ni_rates; 1245 /* Indicate highest supported rate. */ 1246 ni->ni_txrate = rs->rs_rates[rs->rs_nrates - 1]; 1247 (void) rsu_set_fw_power_state(sc, RSU_PWR_SLEEP); 1248 ieee80211_free_node(ni); 1249 startcal = 1; 1250 break; 1251 default: 1252 break; 1253 } 1254 sc->sc_calibrating = 1; 1255 /* Start periodic calibration. */ 1256 taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task, hz); 1257 RSU_UNLOCK(sc); 1258 IEEE80211_LOCK(ic); 1259 return (uvp->newstate(vap, nstate, arg)); 1260 } 1261 1262 #ifdef notyet 1263 static void 1264 rsu_set_key(struct rsu_softc *sc, const struct ieee80211_key *k) 1265 { 1266 struct r92s_fw_cmd_set_key key; 1267 1268 memset(&key, 0, sizeof(key)); 1269 /* Map net80211 cipher to HW crypto algorithm. */ 1270 switch (k->wk_cipher->ic_cipher) { 1271 case IEEE80211_CIPHER_WEP: 1272 if (k->wk_keylen < 8) 1273 key.algo = R92S_KEY_ALGO_WEP40; 1274 else 1275 key.algo = R92S_KEY_ALGO_WEP104; 1276 break; 1277 case IEEE80211_CIPHER_TKIP: 1278 key.algo = R92S_KEY_ALGO_TKIP; 1279 break; 1280 case IEEE80211_CIPHER_AES_CCM: 1281 key.algo = R92S_KEY_ALGO_AES; 1282 break; 1283 default: 1284 return; 1285 } 1286 key.id = k->wk_keyix; 1287 key.grpkey = (k->wk_flags & IEEE80211_KEY_GROUP) != 0; 1288 memcpy(key.key, k->wk_key, MIN(k->wk_keylen, sizeof(key.key))); 1289 (void)rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key)); 1290 } 1291 1292 static void 1293 rsu_delete_key(struct rsu_softc *sc, const struct ieee80211_key *k) 1294 { 1295 struct r92s_fw_cmd_set_key key; 1296 1297 memset(&key, 0, sizeof(key)); 1298 key.id = k->wk_keyix; 1299 (void)rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key)); 1300 } 1301 #endif 1302 1303 static int 1304 rsu_site_survey(struct rsu_softc *sc, struct ieee80211vap *vap) 1305 { 1306 struct r92s_fw_cmd_sitesurvey cmd; 1307 struct ieee80211com *ic = &sc->sc_ic; 1308 int r; 1309 1310 RSU_ASSERT_LOCKED(sc); 1311 1312 memset(&cmd, 0, sizeof(cmd)); 1313 if ((ic->ic_flags & IEEE80211_F_ASCAN) || sc->sc_scan_pass == 1) 1314 cmd.active = htole32(1); 1315 cmd.limit = htole32(48); 1316 if (sc->sc_scan_pass == 1 && vap->iv_des_nssid > 0) { 1317 /* Do a directed scan for second pass. */ 1318 cmd.ssidlen = htole32(vap->iv_des_ssid[0].len); 1319 memcpy(cmd.ssid, vap->iv_des_ssid[0].ssid, 1320 vap->iv_des_ssid[0].len); 1321 1322 } 1323 DPRINTF("sending site survey command, pass=%d\n", sc->sc_scan_pass); 1324 r = rsu_fw_cmd(sc, R92S_CMD_SITE_SURVEY, &cmd, sizeof(cmd)); 1325 if (r == 0) { 1326 sc->sc_scanning = 1; 1327 } 1328 return (r); 1329 } 1330 1331 static int 1332 rsu_join_bss(struct rsu_softc *sc, struct ieee80211_node *ni) 1333 { 1334 struct ieee80211com *ic = &sc->sc_ic; 1335 struct ieee80211vap *vap = ni->ni_vap; 1336 struct ndis_wlan_bssid_ex *bss; 1337 struct ndis_802_11_fixed_ies *fixed; 1338 struct r92s_fw_cmd_auth auth; 1339 uint8_t buf[sizeof(*bss) + 128] __aligned(4); 1340 uint8_t *frm; 1341 uint8_t opmode; 1342 int error; 1343 int cnt; 1344 char *msg = "rsujoin"; 1345 1346 RSU_ASSERT_LOCKED(sc); 1347 1348 /* 1349 * Until net80211 scanning doesn't automatically finish 1350 * before we tell it to, let's just wait until any pending 1351 * scan is done. 1352 * 1353 * XXX TODO: yes, this releases and re-acquires the lock. 1354 * We should re-verify the state whenever we re-attempt this! 1355 */ 1356 cnt = 0; 1357 while (sc->sc_scanning && cnt < 10) { 1358 device_printf(sc->sc_dev, 1359 "%s: still scanning! (attempt %d)\n", 1360 __func__, cnt); 1361 msleep(msg, &sc->sc_mtx, 0, msg, hz / 2); 1362 cnt++; 1363 } 1364 1365 /* Let the FW decide the opmode based on the capinfo field. */ 1366 opmode = NDIS802_11AUTOUNKNOWN; 1367 RSU_DPRINTF(sc, RSU_DEBUG_RESET, 1368 "%s: setting operating mode to %d\n", 1369 __func__, opmode); 1370 error = rsu_fw_cmd(sc, R92S_CMD_SET_OPMODE, &opmode, sizeof(opmode)); 1371 if (error != 0) 1372 return (error); 1373 1374 memset(&auth, 0, sizeof(auth)); 1375 if (vap->iv_flags & IEEE80211_F_WPA) { 1376 auth.mode = R92S_AUTHMODE_WPA; 1377 auth.dot1x = (ni->ni_authmode == IEEE80211_AUTH_8021X); 1378 } else 1379 auth.mode = R92S_AUTHMODE_OPEN; 1380 RSU_DPRINTF(sc, RSU_DEBUG_RESET, 1381 "%s: setting auth mode to %d\n", 1382 __func__, auth.mode); 1383 error = rsu_fw_cmd(sc, R92S_CMD_SET_AUTH, &auth, sizeof(auth)); 1384 if (error != 0) 1385 return (error); 1386 1387 memset(buf, 0, sizeof(buf)); 1388 bss = (struct ndis_wlan_bssid_ex *)buf; 1389 IEEE80211_ADDR_COPY(bss->macaddr, ni->ni_bssid); 1390 bss->ssid.ssidlen = htole32(ni->ni_esslen); 1391 memcpy(bss->ssid.ssid, ni->ni_essid, ni->ni_esslen); 1392 if (vap->iv_flags & (IEEE80211_F_PRIVACY | IEEE80211_F_WPA)) 1393 bss->privacy = htole32(1); 1394 bss->rssi = htole32(ni->ni_avgrssi); 1395 if (ic->ic_curmode == IEEE80211_MODE_11B) 1396 bss->networktype = htole32(NDIS802_11DS); 1397 else 1398 bss->networktype = htole32(NDIS802_11OFDM24); 1399 bss->config.len = htole32(sizeof(bss->config)); 1400 bss->config.bintval = htole32(ni->ni_intval); 1401 bss->config.dsconfig = htole32(ieee80211_chan2ieee(ic, ni->ni_chan)); 1402 bss->inframode = htole32(NDIS802_11INFRASTRUCTURE); 1403 /* XXX verify how this is supposed to look! */ 1404 memcpy(bss->supprates, ni->ni_rates.rs_rates, 1405 ni->ni_rates.rs_nrates); 1406 /* Write the fixed fields of the beacon frame. */ 1407 fixed = (struct ndis_802_11_fixed_ies *)&bss[1]; 1408 memcpy(&fixed->tstamp, ni->ni_tstamp.data, 8); 1409 fixed->bintval = htole16(ni->ni_intval); 1410 fixed->capabilities = htole16(ni->ni_capinfo); 1411 /* Write IEs to be included in the association request. */ 1412 frm = (uint8_t *)&fixed[1]; 1413 frm = ieee80211_add_rsn(frm, vap); 1414 frm = ieee80211_add_wpa(frm, vap); 1415 frm = ieee80211_add_qos(frm, ni); 1416 if ((ic->ic_flags & IEEE80211_F_WME) && 1417 (ni->ni_ies.wme_ie != NULL)) 1418 frm = ieee80211_add_wme_info(frm, &ic->ic_wme); 1419 if (ni->ni_flags & IEEE80211_NODE_HT) { 1420 frm = ieee80211_add_htcap(frm, ni); 1421 frm = ieee80211_add_htinfo(frm, ni); 1422 } 1423 bss->ieslen = htole32(frm - (uint8_t *)fixed); 1424 bss->len = htole32(((frm - buf) + 3) & ~3); 1425 RSU_DPRINTF(sc, RSU_DEBUG_RESET | RSU_DEBUG_FWCMD, 1426 "%s: sending join bss command to %s chan %d\n", 1427 __func__, 1428 ether_sprintf(bss->macaddr), le32toh(bss->config.dsconfig)); 1429 return (rsu_fw_cmd(sc, R92S_CMD_JOIN_BSS, buf, sizeof(buf))); 1430 } 1431 1432 static int 1433 rsu_disconnect(struct rsu_softc *sc) 1434 { 1435 uint32_t zero = 0; /* :-) */ 1436 1437 /* Disassociate from our current BSS. */ 1438 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD, 1439 "%s: sending disconnect command\n", __func__); 1440 return (rsu_fw_cmd(sc, R92S_CMD_DISCONNECT, &zero, sizeof(zero))); 1441 } 1442 1443 /* 1444 * Map the hardware provided RSSI value to a signal level. 1445 * For the most part it's just something we divide by and cap 1446 * so it doesn't overflow the representation by net80211. 1447 */ 1448 static int 1449 rsu_hwrssi_to_rssi(struct rsu_softc *sc, int hw_rssi) 1450 { 1451 int v; 1452 1453 if (hw_rssi == 0) 1454 return (0); 1455 v = hw_rssi >> 4; 1456 if (v > 80) 1457 v = 80; 1458 return (v); 1459 } 1460 1461 static void 1462 rsu_event_survey(struct rsu_softc *sc, uint8_t *buf, int len) 1463 { 1464 struct ieee80211com *ic = &sc->sc_ic; 1465 struct ieee80211_frame *wh; 1466 struct ndis_wlan_bssid_ex *bss; 1467 struct ieee80211_rx_stats rxs; 1468 struct mbuf *m; 1469 int pktlen; 1470 1471 if (__predict_false(len < sizeof(*bss))) 1472 return; 1473 bss = (struct ndis_wlan_bssid_ex *)buf; 1474 if (__predict_false(len < sizeof(*bss) + le32toh(bss->ieslen))) 1475 return; 1476 1477 RSU_DPRINTF(sc, RSU_DEBUG_SCAN, 1478 "%s: found BSS %s: len=%d chan=%d inframode=%d " 1479 "networktype=%d privacy=%d, RSSI=%d\n", 1480 __func__, 1481 ether_sprintf(bss->macaddr), le32toh(bss->len), 1482 le32toh(bss->config.dsconfig), le32toh(bss->inframode), 1483 le32toh(bss->networktype), le32toh(bss->privacy), 1484 le32toh(bss->rssi)); 1485 1486 /* Build a fake beacon frame to let net80211 do all the parsing. */ 1487 /* XXX TODO: just call the new scan API methods! */ 1488 pktlen = sizeof(*wh) + le32toh(bss->ieslen); 1489 if (__predict_false(pktlen > MCLBYTES)) 1490 return; 1491 m = m_get2(pktlen, M_NOWAIT, MT_DATA, M_PKTHDR); 1492 if (__predict_false(m == NULL)) 1493 return; 1494 wh = mtod(m, struct ieee80211_frame *); 1495 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 1496 IEEE80211_FC0_SUBTYPE_BEACON; 1497 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1498 USETW(wh->i_dur, 0); 1499 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr); 1500 IEEE80211_ADDR_COPY(wh->i_addr2, bss->macaddr); 1501 IEEE80211_ADDR_COPY(wh->i_addr3, bss->macaddr); 1502 *(uint16_t *)wh->i_seq = 0; 1503 memcpy(&wh[1], (uint8_t *)&bss[1], le32toh(bss->ieslen)); 1504 1505 /* Finalize mbuf. */ 1506 m->m_pkthdr.len = m->m_len = pktlen; 1507 1508 /* Set channel flags for input path */ 1509 bzero(&rxs, sizeof(rxs)); 1510 rxs.r_flags |= IEEE80211_R_IEEE | IEEE80211_R_FREQ; 1511 rxs.r_flags |= IEEE80211_R_NF | IEEE80211_R_RSSI; 1512 rxs.c_ieee = le32toh(bss->config.dsconfig); 1513 rxs.c_freq = ieee80211_ieee2mhz(rxs.c_ieee, IEEE80211_CHAN_2GHZ); 1514 /* This is a number from 0..100; so let's just divide it down a bit */ 1515 rxs.rssi = le32toh(bss->rssi) / 2; 1516 rxs.nf = -96; 1517 1518 /* XXX avoid a LOR */ 1519 RSU_UNLOCK(sc); 1520 ieee80211_input_mimo_all(ic, m, &rxs); 1521 RSU_LOCK(sc); 1522 } 1523 1524 static void 1525 rsu_event_join_bss(struct rsu_softc *sc, uint8_t *buf, int len) 1526 { 1527 struct ieee80211com *ic = &sc->sc_ic; 1528 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1529 struct ieee80211_node *ni = vap->iv_bss; 1530 struct r92s_event_join_bss *rsp; 1531 uint32_t tmp; 1532 int res; 1533 1534 if (__predict_false(len < sizeof(*rsp))) 1535 return; 1536 rsp = (struct r92s_event_join_bss *)buf; 1537 res = (int)le32toh(rsp->join_res); 1538 1539 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD, 1540 "%s: Rx join BSS event len=%d res=%d\n", 1541 __func__, len, res); 1542 1543 /* 1544 * XXX Don't do this; there's likely a better way to tell 1545 * the caller we failed. 1546 */ 1547 if (res <= 0) { 1548 RSU_UNLOCK(sc); 1549 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1); 1550 RSU_LOCK(sc); 1551 return; 1552 } 1553 1554 tmp = le32toh(rsp->associd); 1555 if (tmp >= vap->iv_max_aid) { 1556 DPRINTF("Assoc ID overflow\n"); 1557 tmp = 1; 1558 } 1559 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD, 1560 "%s: associated with %s associd=%d\n", 1561 __func__, ether_sprintf(rsp->bss.macaddr), tmp); 1562 /* XXX is this required? What's the top two bits for again? */ 1563 ni->ni_associd = tmp | 0xc000; 1564 RSU_UNLOCK(sc); 1565 ieee80211_new_state(vap, IEEE80211_S_RUN, 1566 IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 1567 RSU_LOCK(sc); 1568 } 1569 1570 static void 1571 rsu_event_addba_req_report(struct rsu_softc *sc, uint8_t *buf, int len) 1572 { 1573 struct ieee80211com *ic = &sc->sc_ic; 1574 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1575 struct r92s_add_ba_event *ba = (void *) buf; 1576 struct ieee80211_node *ni; 1577 1578 if (len < sizeof(*ba)) { 1579 device_printf(sc->sc_dev, "%s: short read (%d)\n", __func__, len); 1580 return; 1581 } 1582 1583 if (vap == NULL) 1584 return; 1585 1586 RSU_DPRINTF(sc, RSU_DEBUG_AMPDU, "%s: mac=%s, tid=%d, ssn=%d\n", 1587 __func__, 1588 ether_sprintf(ba->mac_addr), 1589 (int) ba->tid, 1590 (int) le16toh(ba->ssn)); 1591 1592 /* XXX do node lookup; this is STA specific */ 1593 1594 ni = ieee80211_ref_node(vap->iv_bss); 1595 ieee80211_ampdu_rx_start_ext(ni, ba->tid, le16toh(ba->ssn) >> 4, 32); 1596 ieee80211_free_node(ni); 1597 } 1598 1599 static void 1600 rsu_rx_event(struct rsu_softc *sc, uint8_t code, uint8_t *buf, int len) 1601 { 1602 struct ieee80211com *ic = &sc->sc_ic; 1603 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1604 1605 RSU_DPRINTF(sc, RSU_DEBUG_RX | RSU_DEBUG_FWCMD, 1606 "%s: Rx event code=%d len=%d\n", __func__, code, len); 1607 switch (code) { 1608 case R92S_EVT_SURVEY: 1609 rsu_event_survey(sc, buf, len); 1610 break; 1611 case R92S_EVT_SURVEY_DONE: 1612 RSU_DPRINTF(sc, RSU_DEBUG_SCAN, 1613 "%s: site survey pass %d done, found %d BSS\n", 1614 __func__, sc->sc_scan_pass, le32toh(*(uint32_t *)buf)); 1615 sc->sc_scanning = 0; 1616 if (vap->iv_state != IEEE80211_S_SCAN) 1617 break; /* Ignore if not scanning. */ 1618 1619 /* 1620 * XXX TODO: This needs to be done without a transition to 1621 * the SCAN state again. Grr. 1622 */ 1623 if (sc->sc_scan_pass == 0 && vap->iv_des_nssid != 0) { 1624 /* Schedule a directed scan for hidden APs. */ 1625 /* XXX bad! */ 1626 sc->sc_scan_pass = 1; 1627 RSU_UNLOCK(sc); 1628 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1); 1629 RSU_LOCK(sc); 1630 break; 1631 } 1632 sc->sc_scan_pass = 0; 1633 break; 1634 case R92S_EVT_JOIN_BSS: 1635 if (vap->iv_state == IEEE80211_S_AUTH) 1636 rsu_event_join_bss(sc, buf, len); 1637 break; 1638 case R92S_EVT_DEL_STA: 1639 RSU_DPRINTF(sc, RSU_DEBUG_FWCMD | RSU_DEBUG_STATE, 1640 "%s: disassociated from %s\n", __func__, 1641 ether_sprintf(buf)); 1642 if (vap->iv_state == IEEE80211_S_RUN && 1643 IEEE80211_ADDR_EQ(vap->iv_bss->ni_bssid, buf)) { 1644 RSU_UNLOCK(sc); 1645 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1); 1646 RSU_LOCK(sc); 1647 } 1648 break; 1649 case R92S_EVT_WPS_PBC: 1650 RSU_DPRINTF(sc, RSU_DEBUG_RX | RSU_DEBUG_FWCMD, 1651 "%s: WPS PBC pushed.\n", __func__); 1652 break; 1653 case R92S_EVT_FWDBG: 1654 buf[60] = '\0'; 1655 RSU_DPRINTF(sc, RSU_DEBUG_FWDBG, "FWDBG: %s\n", (char *)buf); 1656 break; 1657 case R92S_EVT_ADDBA_REQ_REPORT: 1658 rsu_event_addba_req_report(sc, buf, len); 1659 break; 1660 default: 1661 device_printf(sc->sc_dev, "%s: unhandled code (%d)\n", __func__, code); 1662 break; 1663 } 1664 } 1665 1666 static void 1667 rsu_rx_multi_event(struct rsu_softc *sc, uint8_t *buf, int len) 1668 { 1669 struct r92s_fw_cmd_hdr *cmd; 1670 int cmdsz; 1671 1672 RSU_DPRINTF(sc, RSU_DEBUG_RX, "%s: Rx events len=%d\n", __func__, len); 1673 1674 /* Skip Rx status. */ 1675 buf += sizeof(struct r92s_rx_stat); 1676 len -= sizeof(struct r92s_rx_stat); 1677 1678 /* Process all events. */ 1679 for (;;) { 1680 /* Check that command header fits. */ 1681 if (__predict_false(len < sizeof(*cmd))) 1682 break; 1683 cmd = (struct r92s_fw_cmd_hdr *)buf; 1684 /* Check that command payload fits. */ 1685 cmdsz = le16toh(cmd->len); 1686 if (__predict_false(len < sizeof(*cmd) + cmdsz)) 1687 break; 1688 1689 /* Process firmware event. */ 1690 rsu_rx_event(sc, cmd->code, (uint8_t *)&cmd[1], cmdsz); 1691 1692 if (!(cmd->seq & R92S_FW_CMD_MORE)) 1693 break; 1694 buf += sizeof(*cmd) + cmdsz; 1695 len -= sizeof(*cmd) + cmdsz; 1696 } 1697 } 1698 1699 #if 0 1700 static int8_t 1701 rsu_get_rssi(struct rsu_softc *sc, int rate, void *physt) 1702 { 1703 static const int8_t cckoff[] = { 14, -2, -20, -40 }; 1704 struct r92s_rx_phystat *phy; 1705 struct r92s_rx_cck *cck; 1706 uint8_t rpt; 1707 int8_t rssi; 1708 1709 if (rate <= 3) { 1710 cck = (struct r92s_rx_cck *)physt; 1711 rpt = (cck->agc_rpt >> 6) & 0x3; 1712 rssi = cck->agc_rpt & 0x3e; 1713 rssi = cckoff[rpt] - rssi; 1714 } else { /* OFDM/HT. */ 1715 phy = (struct r92s_rx_phystat *)physt; 1716 rssi = ((le32toh(phy->phydw1) >> 1) & 0x7f) - 106; 1717 } 1718 return (rssi); 1719 } 1720 #endif 1721 1722 static struct mbuf * 1723 rsu_rx_frame(struct rsu_softc *sc, uint8_t *buf, int pktlen) 1724 { 1725 struct ieee80211com *ic = &sc->sc_ic; 1726 struct ieee80211_frame *wh; 1727 struct r92s_rx_stat *stat; 1728 uint32_t rxdw0, rxdw3; 1729 struct mbuf *m; 1730 uint8_t rate; 1731 int infosz; 1732 1733 stat = (struct r92s_rx_stat *)buf; 1734 rxdw0 = le32toh(stat->rxdw0); 1735 rxdw3 = le32toh(stat->rxdw3); 1736 1737 if (__predict_false(rxdw0 & R92S_RXDW0_CRCERR)) { 1738 counter_u64_add(ic->ic_ierrors, 1); 1739 return NULL; 1740 } 1741 if (__predict_false(pktlen < sizeof(*wh) || pktlen > MCLBYTES)) { 1742 counter_u64_add(ic->ic_ierrors, 1); 1743 return NULL; 1744 } 1745 1746 rate = MS(rxdw3, R92S_RXDW3_RATE); 1747 infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8; 1748 1749 #if 0 1750 /* Get RSSI from PHY status descriptor if present. */ 1751 if (infosz != 0) 1752 *rssi = rsu_get_rssi(sc, rate, &stat[1]); 1753 else 1754 *rssi = 0; 1755 #endif 1756 1757 RSU_DPRINTF(sc, RSU_DEBUG_RX, 1758 "%s: Rx frame len=%d rate=%d infosz=%d\n", 1759 __func__, pktlen, rate, infosz); 1760 1761 m = m_get2(pktlen, M_NOWAIT, MT_DATA, M_PKTHDR); 1762 if (__predict_false(m == NULL)) { 1763 counter_u64_add(ic->ic_ierrors, 1); 1764 return NULL; 1765 } 1766 /* Hardware does Rx TCP checksum offload. */ 1767 if (rxdw3 & R92S_RXDW3_TCPCHKVALID) { 1768 if (__predict_true(rxdw3 & R92S_RXDW3_TCPCHKRPT)) 1769 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID; 1770 } 1771 wh = (struct ieee80211_frame *)((uint8_t *)&stat[1] + infosz); 1772 memcpy(mtod(m, uint8_t *), wh, pktlen); 1773 m->m_pkthdr.len = m->m_len = pktlen; 1774 1775 if (ieee80211_radiotap_active(ic)) { 1776 struct rsu_rx_radiotap_header *tap = &sc->sc_rxtap; 1777 1778 /* Map HW rate index to 802.11 rate. */ 1779 tap->wr_flags = 2; 1780 if (!(rxdw3 & R92S_RXDW3_HTC)) { 1781 switch (rate) { 1782 /* CCK. */ 1783 case 0: tap->wr_rate = 2; break; 1784 case 1: tap->wr_rate = 4; break; 1785 case 2: tap->wr_rate = 11; break; 1786 case 3: tap->wr_rate = 22; break; 1787 /* OFDM. */ 1788 case 4: tap->wr_rate = 12; break; 1789 case 5: tap->wr_rate = 18; break; 1790 case 6: tap->wr_rate = 24; break; 1791 case 7: tap->wr_rate = 36; break; 1792 case 8: tap->wr_rate = 48; break; 1793 case 9: tap->wr_rate = 72; break; 1794 case 10: tap->wr_rate = 96; break; 1795 case 11: tap->wr_rate = 108; break; 1796 } 1797 } else if (rate >= 12) { /* MCS0~15. */ 1798 /* Bit 7 set means HT MCS instead of rate. */ 1799 tap->wr_rate = 0x80 | (rate - 12); 1800 } 1801 #if 0 1802 tap->wr_dbm_antsignal = *rssi; 1803 #endif 1804 /* XXX not nice */ 1805 tap->wr_dbm_antsignal = rsu_hwrssi_to_rssi(sc, sc->sc_currssi); 1806 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); 1807 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 1808 } 1809 1810 return (m); 1811 } 1812 1813 static struct mbuf * 1814 rsu_rx_multi_frame(struct rsu_softc *sc, uint8_t *buf, int len) 1815 { 1816 struct r92s_rx_stat *stat; 1817 uint32_t rxdw0; 1818 int totlen, pktlen, infosz, npkts; 1819 struct mbuf *m, *m0 = NULL, *prevm = NULL; 1820 1821 /* Get the number of encapsulated frames. */ 1822 stat = (struct r92s_rx_stat *)buf; 1823 npkts = MS(le32toh(stat->rxdw2), R92S_RXDW2_PKTCNT); 1824 RSU_DPRINTF(sc, RSU_DEBUG_RX, 1825 "%s: Rx %d frames in one chunk\n", __func__, npkts); 1826 1827 /* Process all of them. */ 1828 while (npkts-- > 0) { 1829 if (__predict_false(len < sizeof(*stat))) 1830 break; 1831 stat = (struct r92s_rx_stat *)buf; 1832 rxdw0 = le32toh(stat->rxdw0); 1833 1834 pktlen = MS(rxdw0, R92S_RXDW0_PKTLEN); 1835 if (__predict_false(pktlen == 0)) 1836 break; 1837 1838 infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8; 1839 1840 /* Make sure everything fits in xfer. */ 1841 totlen = sizeof(*stat) + infosz + pktlen; 1842 if (__predict_false(totlen > len)) 1843 break; 1844 1845 /* Process 802.11 frame. */ 1846 m = rsu_rx_frame(sc, buf, pktlen); 1847 if (m0 == NULL) 1848 m0 = m; 1849 if (prevm == NULL) 1850 prevm = m; 1851 else { 1852 prevm->m_next = m; 1853 prevm = m; 1854 } 1855 /* Next chunk is 128-byte aligned. */ 1856 totlen = (totlen + 127) & ~127; 1857 buf += totlen; 1858 len -= totlen; 1859 } 1860 1861 return (m0); 1862 } 1863 1864 static struct mbuf * 1865 rsu_rxeof(struct usb_xfer *xfer, struct rsu_data *data) 1866 { 1867 struct rsu_softc *sc = data->sc; 1868 struct ieee80211com *ic = &sc->sc_ic; 1869 struct r92s_rx_stat *stat; 1870 int len; 1871 1872 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 1873 1874 if (__predict_false(len < sizeof(*stat))) { 1875 DPRINTF("xfer too short %d\n", len); 1876 counter_u64_add(ic->ic_ierrors, 1); 1877 return (NULL); 1878 } 1879 /* Determine if it is a firmware C2H event or an 802.11 frame. */ 1880 stat = (struct r92s_rx_stat *)data->buf; 1881 if ((le32toh(stat->rxdw1) & 0x1ff) == 0x1ff) { 1882 rsu_rx_multi_event(sc, data->buf, len); 1883 /* No packets to process. */ 1884 return (NULL); 1885 } else 1886 return (rsu_rx_multi_frame(sc, data->buf, len)); 1887 } 1888 1889 static void 1890 rsu_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error) 1891 { 1892 struct rsu_softc *sc = usbd_xfer_softc(xfer); 1893 struct ieee80211com *ic = &sc->sc_ic; 1894 struct ieee80211_frame *wh; 1895 struct ieee80211_node *ni; 1896 struct mbuf *m = NULL, *next; 1897 struct rsu_data *data; 1898 1899 RSU_ASSERT_LOCKED(sc); 1900 1901 switch (USB_GET_STATE(xfer)) { 1902 case USB_ST_TRANSFERRED: 1903 data = STAILQ_FIRST(&sc->sc_rx_active); 1904 if (data == NULL) 1905 goto tr_setup; 1906 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next); 1907 m = rsu_rxeof(xfer, data); 1908 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 1909 /* FALLTHROUGH */ 1910 case USB_ST_SETUP: 1911 tr_setup: 1912 /* 1913 * XXX TODO: if we have an mbuf list, but then 1914 * we hit data == NULL, what now? 1915 */ 1916 data = STAILQ_FIRST(&sc->sc_rx_inactive); 1917 if (data == NULL) { 1918 KASSERT(m == NULL, ("mbuf isn't NULL")); 1919 return; 1920 } 1921 STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next); 1922 STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next); 1923 usbd_xfer_set_frame_data(xfer, 0, data->buf, 1924 usbd_xfer_max_len(xfer)); 1925 usbd_transfer_submit(xfer); 1926 /* 1927 * To avoid LOR we should unlock our private mutex here to call 1928 * ieee80211_input() because here is at the end of a USB 1929 * callback and safe to unlock. 1930 */ 1931 RSU_UNLOCK(sc); 1932 while (m != NULL) { 1933 int rssi; 1934 1935 /* Cheat and get the last calibrated RSSI */ 1936 rssi = rsu_hwrssi_to_rssi(sc, sc->sc_currssi); 1937 1938 next = m->m_next; 1939 m->m_next = NULL; 1940 wh = mtod(m, struct ieee80211_frame *); 1941 ni = ieee80211_find_rxnode(ic, 1942 (struct ieee80211_frame_min *)wh); 1943 if (ni != NULL) { 1944 if (ni->ni_flags & IEEE80211_NODE_HT) 1945 m->m_flags |= M_AMPDU; 1946 (void)ieee80211_input(ni, m, rssi, -96); 1947 ieee80211_free_node(ni); 1948 } else 1949 (void)ieee80211_input_all(ic, m, rssi, -96); 1950 m = next; 1951 } 1952 RSU_LOCK(sc); 1953 break; 1954 default: 1955 /* needs it to the inactive queue due to a error. */ 1956 data = STAILQ_FIRST(&sc->sc_rx_active); 1957 if (data != NULL) { 1958 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next); 1959 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 1960 } 1961 if (error != USB_ERR_CANCELLED) { 1962 usbd_xfer_set_stall(xfer); 1963 counter_u64_add(ic->ic_ierrors, 1); 1964 goto tr_setup; 1965 } 1966 break; 1967 } 1968 1969 } 1970 1971 static void 1972 rsu_txeof(struct usb_xfer *xfer, struct rsu_data *data) 1973 { 1974 #ifdef USB_DEBUG 1975 struct rsu_softc *sc = usbd_xfer_softc(xfer); 1976 #endif 1977 1978 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, "%s: called; data=%p\n", 1979 __func__, 1980 data); 1981 1982 if (data->m) { 1983 /* XXX status? */ 1984 ieee80211_tx_complete(data->ni, data->m, 0); 1985 data->m = NULL; 1986 data->ni = NULL; 1987 } 1988 } 1989 1990 static void 1991 rsu_bulk_tx_callback_sub(struct usb_xfer *xfer, usb_error_t error, 1992 uint8_t which) 1993 { 1994 struct rsu_softc *sc = usbd_xfer_softc(xfer); 1995 struct ieee80211com *ic = &sc->sc_ic; 1996 struct rsu_data *data; 1997 1998 RSU_ASSERT_LOCKED(sc); 1999 2000 switch (USB_GET_STATE(xfer)) { 2001 case USB_ST_TRANSFERRED: 2002 data = STAILQ_FIRST(&sc->sc_tx_active[which]); 2003 if (data == NULL) 2004 goto tr_setup; 2005 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, "%s: transfer done %p\n", 2006 __func__, data); 2007 STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next); 2008 rsu_txeof(xfer, data); 2009 rsu_freebuf(sc, data); 2010 /* FALLTHROUGH */ 2011 case USB_ST_SETUP: 2012 tr_setup: 2013 data = STAILQ_FIRST(&sc->sc_tx_pending[which]); 2014 if (data == NULL) { 2015 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, 2016 "%s: empty pending queue sc %p\n", __func__, sc); 2017 return; 2018 } 2019 STAILQ_REMOVE_HEAD(&sc->sc_tx_pending[which], next); 2020 STAILQ_INSERT_TAIL(&sc->sc_tx_active[which], data, next); 2021 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen); 2022 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, 2023 "%s: submitting transfer %p\n", 2024 __func__, 2025 data); 2026 usbd_transfer_submit(xfer); 2027 break; 2028 default: 2029 data = STAILQ_FIRST(&sc->sc_tx_active[which]); 2030 if (data != NULL) { 2031 STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next); 2032 rsu_txeof(xfer, data); 2033 rsu_freebuf(sc, data); 2034 } 2035 counter_u64_add(ic->ic_oerrors, 1); 2036 2037 if (error != USB_ERR_CANCELLED) { 2038 usbd_xfer_set_stall(xfer); 2039 goto tr_setup; 2040 } 2041 break; 2042 } 2043 2044 /* 2045 * XXX TODO: if the queue is low, flush out FF TX frames. 2046 * Remember to unlock the driver for now; net80211 doesn't 2047 * defer it for us. 2048 */ 2049 } 2050 2051 static void 2052 rsu_bulk_tx_callback_be_bk(struct usb_xfer *xfer, usb_error_t error) 2053 { 2054 struct rsu_softc *sc = usbd_xfer_softc(xfer); 2055 2056 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_BE_BK); 2057 2058 /* This kicks the TX taskqueue */ 2059 rsu_start(sc); 2060 } 2061 2062 static void 2063 rsu_bulk_tx_callback_vi_vo(struct usb_xfer *xfer, usb_error_t error) 2064 { 2065 struct rsu_softc *sc = usbd_xfer_softc(xfer); 2066 2067 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_VI_VO); 2068 2069 /* This kicks the TX taskqueue */ 2070 rsu_start(sc); 2071 } 2072 2073 static void 2074 rsu_bulk_tx_callback_h2c(struct usb_xfer *xfer, usb_error_t error) 2075 { 2076 struct rsu_softc *sc = usbd_xfer_softc(xfer); 2077 2078 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_H2C); 2079 2080 /* This kicks the TX taskqueue */ 2081 rsu_start(sc); 2082 } 2083 2084 /* 2085 * Transmit the given frame. 2086 * 2087 * This doesn't free the node or mbuf upon failure. 2088 */ 2089 static int 2090 rsu_tx_start(struct rsu_softc *sc, struct ieee80211_node *ni, 2091 struct mbuf *m0, struct rsu_data *data) 2092 { 2093 struct ieee80211com *ic = &sc->sc_ic; 2094 struct ieee80211vap *vap = ni->ni_vap; 2095 struct ieee80211_frame *wh; 2096 struct ieee80211_key *k = NULL; 2097 struct r92s_tx_desc *txd; 2098 uint8_t type; 2099 int prio = 0; 2100 uint8_t which; 2101 int hasqos; 2102 int xferlen; 2103 int qid; 2104 2105 RSU_ASSERT_LOCKED(sc); 2106 2107 wh = mtod(m0, struct ieee80211_frame *); 2108 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 2109 2110 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: data=%p, m=%p\n", 2111 __func__, data, m0); 2112 2113 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 2114 k = ieee80211_crypto_encap(ni, m0); 2115 if (k == NULL) { 2116 device_printf(sc->sc_dev, 2117 "ieee80211_crypto_encap returns NULL.\n"); 2118 /* XXX we don't expect the fragmented frames */ 2119 return (ENOBUFS); 2120 } 2121 wh = mtod(m0, struct ieee80211_frame *); 2122 } 2123 /* If we have QoS then use it */ 2124 /* XXX TODO: mbuf WME/PRI versus TID? */ 2125 if (IEEE80211_QOS_HAS_SEQ(wh)) { 2126 /* Has QoS */ 2127 prio = M_WME_GETAC(m0); 2128 which = rsu_wme_ac_xfer_map[prio]; 2129 hasqos = 1; 2130 } else { 2131 /* Non-QoS TID */ 2132 /* XXX TODO: tid=0 for non-qos TID? */ 2133 which = rsu_wme_ac_xfer_map[WME_AC_BE]; 2134 hasqos = 0; 2135 prio = 0; 2136 } 2137 2138 qid = rsu_ac2qid[prio]; 2139 #if 0 2140 switch (type) { 2141 case IEEE80211_FC0_TYPE_CTL: 2142 case IEEE80211_FC0_TYPE_MGT: 2143 which = rsu_wme_ac_xfer_map[WME_AC_VO]; 2144 break; 2145 default: 2146 which = rsu_wme_ac_xfer_map[M_WME_GETAC(m0)]; 2147 break; 2148 } 2149 hasqos = 0; 2150 #endif 2151 2152 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: pri=%d, which=%d, hasqos=%d\n", 2153 __func__, 2154 prio, 2155 which, 2156 hasqos); 2157 2158 /* Fill Tx descriptor. */ 2159 txd = (struct r92s_tx_desc *)data->buf; 2160 memset(txd, 0, sizeof(*txd)); 2161 2162 txd->txdw0 |= htole32( 2163 SM(R92S_TXDW0_PKTLEN, m0->m_pkthdr.len) | 2164 SM(R92S_TXDW0_OFFSET, sizeof(*txd)) | 2165 R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG); 2166 2167 txd->txdw1 |= htole32( 2168 SM(R92S_TXDW1_MACID, R92S_MACID_BSS) | SM(R92S_TXDW1_QSEL, qid)); 2169 if (!hasqos) 2170 txd->txdw1 |= htole32(R92S_TXDW1_NONQOS); 2171 #ifdef notyet 2172 if (k != NULL) { 2173 switch (k->wk_cipher->ic_cipher) { 2174 case IEEE80211_CIPHER_WEP: 2175 cipher = R92S_TXDW1_CIPHER_WEP; 2176 break; 2177 case IEEE80211_CIPHER_TKIP: 2178 cipher = R92S_TXDW1_CIPHER_TKIP; 2179 break; 2180 case IEEE80211_CIPHER_AES_CCM: 2181 cipher = R92S_TXDW1_CIPHER_AES; 2182 break; 2183 default: 2184 cipher = R92S_TXDW1_CIPHER_NONE; 2185 } 2186 txd->txdw1 |= htole32( 2187 SM(R92S_TXDW1_CIPHER, cipher) | 2188 SM(R92S_TXDW1_KEYIDX, k->k_id)); 2189 } 2190 #endif 2191 /* XXX todo: set AGGEN bit if appropriate? */ 2192 txd->txdw2 |= htole32(R92S_TXDW2_BK); 2193 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 2194 txd->txdw2 |= htole32(R92S_TXDW2_BMCAST); 2195 /* 2196 * Firmware will use and increment the sequence number for the 2197 * specified priority. 2198 */ 2199 txd->txdw3 |= htole32(SM(R92S_TXDW3_SEQ, prio)); 2200 2201 if (ieee80211_radiotap_active_vap(vap)) { 2202 struct rsu_tx_radiotap_header *tap = &sc->sc_txtap; 2203 2204 tap->wt_flags = 0; 2205 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 2206 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 2207 ieee80211_radiotap_tx(vap, m0); 2208 } 2209 2210 xferlen = sizeof(*txd) + m0->m_pkthdr.len; 2211 m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&txd[1]); 2212 2213 data->buflen = xferlen; 2214 data->ni = ni; 2215 data->m = m0; 2216 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next); 2217 2218 /* start transfer, if any */ 2219 usbd_transfer_start(sc->sc_xfer[which]); 2220 return (0); 2221 } 2222 2223 static int 2224 rsu_transmit(struct ieee80211com *ic, struct mbuf *m) 2225 { 2226 struct rsu_softc *sc = ic->ic_softc; 2227 int error; 2228 2229 RSU_LOCK(sc); 2230 if (!sc->sc_running) { 2231 RSU_UNLOCK(sc); 2232 return (ENXIO); 2233 } 2234 2235 /* 2236 * XXX TODO: ensure that we treat 'm' as a list of frames 2237 * to transmit! 2238 */ 2239 error = mbufq_enqueue(&sc->sc_snd, m); 2240 if (error) { 2241 RSU_DPRINTF(sc, RSU_DEBUG_TX, 2242 "%s: mbufq_enable: failed (%d)\n", 2243 __func__, 2244 error); 2245 RSU_UNLOCK(sc); 2246 return (error); 2247 } 2248 RSU_UNLOCK(sc); 2249 2250 /* This kicks the TX taskqueue */ 2251 rsu_start(sc); 2252 2253 return (0); 2254 } 2255 2256 static void 2257 rsu_drain_mbufq(struct rsu_softc *sc) 2258 { 2259 struct mbuf *m; 2260 struct ieee80211_node *ni; 2261 2262 RSU_ASSERT_LOCKED(sc); 2263 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 2264 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 2265 m->m_pkthdr.rcvif = NULL; 2266 ieee80211_free_node(ni); 2267 m_freem(m); 2268 } 2269 } 2270 2271 static void 2272 _rsu_start(struct rsu_softc *sc) 2273 { 2274 struct ieee80211_node *ni; 2275 struct rsu_data *bf; 2276 struct mbuf *m; 2277 2278 RSU_ASSERT_LOCKED(sc); 2279 2280 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 2281 bf = rsu_getbuf(sc); 2282 if (bf == NULL) { 2283 RSU_DPRINTF(sc, RSU_DEBUG_TX, 2284 "%s: failed to get buffer\n", __func__); 2285 mbufq_prepend(&sc->sc_snd, m); 2286 break; 2287 } 2288 2289 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 2290 m->m_pkthdr.rcvif = NULL; 2291 2292 if (rsu_tx_start(sc, ni, m, bf) != 0) { 2293 RSU_DPRINTF(sc, RSU_DEBUG_TX, 2294 "%s: failed to transmit\n", __func__); 2295 if_inc_counter(ni->ni_vap->iv_ifp, 2296 IFCOUNTER_OERRORS, 1); 2297 rsu_freebuf(sc, bf); 2298 ieee80211_free_node(ni); 2299 m_freem(m); 2300 break; 2301 } 2302 } 2303 } 2304 2305 static void 2306 rsu_start(struct rsu_softc *sc) 2307 { 2308 2309 taskqueue_enqueue(taskqueue_thread, &sc->tx_task); 2310 } 2311 2312 static void 2313 rsu_parent(struct ieee80211com *ic) 2314 { 2315 struct rsu_softc *sc = ic->ic_softc; 2316 int startall = 0; 2317 2318 RSU_LOCK(sc); 2319 if (ic->ic_nrunning > 0) { 2320 if (!sc->sc_running) { 2321 rsu_init(sc); 2322 startall = 1; 2323 } 2324 } else if (sc->sc_running) 2325 rsu_stop(sc); 2326 RSU_UNLOCK(sc); 2327 2328 if (startall) 2329 ieee80211_start_all(ic); 2330 } 2331 2332 /* 2333 * Power on sequence for A-cut adapters. 2334 */ 2335 static void 2336 rsu_power_on_acut(struct rsu_softc *sc) 2337 { 2338 uint32_t reg; 2339 2340 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53); 2341 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57); 2342 2343 /* Enable AFE macro block's bandgap and Mbias. */ 2344 rsu_write_1(sc, R92S_AFE_MISC, 2345 rsu_read_1(sc, R92S_AFE_MISC) | 2346 R92S_AFE_MISC_BGEN | R92S_AFE_MISC_MBEN); 2347 /* Enable LDOA15 block. */ 2348 rsu_write_1(sc, R92S_LDOA15_CTRL, 2349 rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN); 2350 2351 rsu_write_1(sc, R92S_SPS1_CTRL, 2352 rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_LDEN); 2353 rsu_ms_delay(sc, 2000); 2354 /* Enable switch regulator block. */ 2355 rsu_write_1(sc, R92S_SPS1_CTRL, 2356 rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_SWEN); 2357 2358 rsu_write_4(sc, R92S_SPS1_CTRL, 0x00a7b267); 2359 2360 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 2361 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08); 2362 2363 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 2364 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20); 2365 2366 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 2367 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x90); 2368 2369 /* Enable AFE clock. */ 2370 rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1, 2371 rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04); 2372 /* Enable AFE PLL macro block. */ 2373 rsu_write_1(sc, R92S_AFE_PLL_CTRL, 2374 rsu_read_1(sc, R92S_AFE_PLL_CTRL) | 0x11); 2375 /* Attach AFE PLL to MACTOP/BB. */ 2376 rsu_write_1(sc, R92S_SYS_ISO_CTRL, 2377 rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11); 2378 2379 /* Switch to 40MHz clock instead of 80MHz. */ 2380 rsu_write_2(sc, R92S_SYS_CLKR, 2381 rsu_read_2(sc, R92S_SYS_CLKR) & ~R92S_SYS_CLKSEL); 2382 2383 /* Enable MAC clock. */ 2384 rsu_write_2(sc, R92S_SYS_CLKR, 2385 rsu_read_2(sc, R92S_SYS_CLKR) | 2386 R92S_MAC_CLK_EN | R92S_SYS_CLK_EN); 2387 2388 rsu_write_1(sc, R92S_PMC_FSM, 0x02); 2389 2390 /* Enable digital core and IOREG R/W. */ 2391 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 2392 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08); 2393 2394 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 2395 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80); 2396 2397 /* Switch the control path to firmware. */ 2398 reg = rsu_read_2(sc, R92S_SYS_CLKR); 2399 reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL; 2400 rsu_write_2(sc, R92S_SYS_CLKR, reg); 2401 2402 rsu_write_2(sc, R92S_CR, 0x37fc); 2403 2404 /* Fix USB RX FIFO issue. */ 2405 rsu_write_1(sc, 0xfe5c, 2406 rsu_read_1(sc, 0xfe5c) | 0x80); 2407 rsu_write_1(sc, 0x00ab, 2408 rsu_read_1(sc, 0x00ab) | 0xc0); 2409 2410 rsu_write_1(sc, R92S_SYS_CLKR, 2411 rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL); 2412 } 2413 2414 /* 2415 * Power on sequence for B-cut and C-cut adapters. 2416 */ 2417 static void 2418 rsu_power_on_bcut(struct rsu_softc *sc) 2419 { 2420 uint32_t reg; 2421 int ntries; 2422 2423 /* Prevent eFuse leakage. */ 2424 rsu_write_1(sc, 0x37, 0xb0); 2425 rsu_ms_delay(sc, 10); 2426 rsu_write_1(sc, 0x37, 0x30); 2427 2428 /* Switch the control path to hardware. */ 2429 reg = rsu_read_2(sc, R92S_SYS_CLKR); 2430 if (reg & R92S_FWHW_SEL) { 2431 rsu_write_2(sc, R92S_SYS_CLKR, 2432 reg & ~(R92S_SWHW_SEL | R92S_FWHW_SEL)); 2433 } 2434 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 2435 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) & ~0x8c); 2436 rsu_ms_delay(sc, 1); 2437 2438 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53); 2439 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57); 2440 2441 reg = rsu_read_1(sc, R92S_AFE_MISC); 2442 rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN); 2443 rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN | 2444 R92S_AFE_MISC_MBEN | R92S_AFE_MISC_I32_EN); 2445 2446 /* Enable PLL. */ 2447 rsu_write_1(sc, R92S_LDOA15_CTRL, 2448 rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN); 2449 2450 rsu_write_1(sc, R92S_LDOV12D_CTRL, 2451 rsu_read_1(sc, R92S_LDOV12D_CTRL) | R92S_LDV12_EN); 2452 2453 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 2454 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08); 2455 2456 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 2457 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20); 2458 2459 /* Support 64KB IMEM. */ 2460 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 2461 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x97); 2462 2463 /* Enable AFE clock. */ 2464 rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1, 2465 rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04); 2466 /* Enable AFE PLL macro block. */ 2467 reg = rsu_read_1(sc, R92S_AFE_PLL_CTRL); 2468 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11); 2469 rsu_ms_delay(sc, 1); 2470 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x51); 2471 rsu_ms_delay(sc, 1); 2472 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11); 2473 rsu_ms_delay(sc, 1); 2474 2475 /* Attach AFE PLL to MACTOP/BB. */ 2476 rsu_write_1(sc, R92S_SYS_ISO_CTRL, 2477 rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11); 2478 2479 /* Switch to 40MHz clock. */ 2480 rsu_write_1(sc, R92S_SYS_CLKR, 0x00); 2481 /* Disable CPU clock and 80MHz SSC. */ 2482 rsu_write_1(sc, R92S_SYS_CLKR, 2483 rsu_read_1(sc, R92S_SYS_CLKR) | 0xa0); 2484 /* Enable MAC clock. */ 2485 rsu_write_2(sc, R92S_SYS_CLKR, 2486 rsu_read_2(sc, R92S_SYS_CLKR) | 2487 R92S_MAC_CLK_EN | R92S_SYS_CLK_EN); 2488 2489 rsu_write_1(sc, R92S_PMC_FSM, 0x02); 2490 2491 /* Enable digital core and IOREG R/W. */ 2492 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 2493 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08); 2494 2495 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 2496 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80); 2497 2498 /* Switch the control path to firmware. */ 2499 reg = rsu_read_2(sc, R92S_SYS_CLKR); 2500 reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL; 2501 rsu_write_2(sc, R92S_SYS_CLKR, reg); 2502 2503 rsu_write_2(sc, R92S_CR, 0x37fc); 2504 2505 /* Fix USB RX FIFO issue. */ 2506 rsu_write_1(sc, 0xfe5c, 2507 rsu_read_1(sc, 0xfe5c) | 0x80); 2508 2509 rsu_write_1(sc, R92S_SYS_CLKR, 2510 rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL); 2511 2512 rsu_write_1(sc, 0xfe1c, 0x80); 2513 2514 /* Make sure TxDMA is ready to download firmware. */ 2515 for (ntries = 0; ntries < 20; ntries++) { 2516 reg = rsu_read_1(sc, R92S_TCR); 2517 if ((reg & (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT)) == 2518 (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT)) 2519 break; 2520 rsu_ms_delay(sc, 1); 2521 } 2522 if (ntries == 20) { 2523 RSU_DPRINTF(sc, RSU_DEBUG_RESET | RSU_DEBUG_TX, 2524 "%s: TxDMA is not ready\n", 2525 __func__); 2526 /* Reset TxDMA. */ 2527 reg = rsu_read_1(sc, R92S_CR); 2528 rsu_write_1(sc, R92S_CR, reg & ~R92S_CR_TXDMA_EN); 2529 rsu_ms_delay(sc, 1); 2530 rsu_write_1(sc, R92S_CR, reg | R92S_CR_TXDMA_EN); 2531 } 2532 } 2533 2534 static void 2535 rsu_power_off(struct rsu_softc *sc) 2536 { 2537 /* Turn RF off. */ 2538 rsu_write_1(sc, R92S_RF_CTRL, 0x00); 2539 rsu_ms_delay(sc, 5); 2540 2541 /* Turn MAC off. */ 2542 /* Switch control path. */ 2543 rsu_write_1(sc, R92S_SYS_CLKR + 1, 0x38); 2544 /* Reset MACTOP. */ 2545 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x70); 2546 rsu_write_1(sc, R92S_PMC_FSM, 0x06); 2547 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 0, 0xf9); 2548 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 0xe8); 2549 2550 /* Disable AFE PLL. */ 2551 rsu_write_1(sc, R92S_AFE_PLL_CTRL, 0x00); 2552 /* Disable A15V. */ 2553 rsu_write_1(sc, R92S_LDOA15_CTRL, 0x54); 2554 /* Disable eFuse 1.2V. */ 2555 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x50); 2556 rsu_write_1(sc, R92S_LDOV12D_CTRL, 0x24); 2557 /* Enable AFE macro block's bandgap and Mbias. */ 2558 rsu_write_1(sc, R92S_AFE_MISC, 0x30); 2559 /* Disable 1.6V LDO. */ 2560 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x56); 2561 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x43); 2562 2563 /* Firmware - tell it to switch things off */ 2564 (void) rsu_set_fw_power_state(sc, RSU_PWR_OFF); 2565 } 2566 2567 static int 2568 rsu_fw_loadsection(struct rsu_softc *sc, const uint8_t *buf, int len) 2569 { 2570 const uint8_t which = rsu_wme_ac_xfer_map[WME_AC_VO]; 2571 struct rsu_data *data; 2572 struct r92s_tx_desc *txd; 2573 int mlen; 2574 2575 while (len > 0) { 2576 data = rsu_getbuf(sc); 2577 if (data == NULL) 2578 return (ENOMEM); 2579 txd = (struct r92s_tx_desc *)data->buf; 2580 memset(txd, 0, sizeof(*txd)); 2581 if (len <= RSU_TXBUFSZ - sizeof(*txd)) { 2582 /* Last chunk. */ 2583 txd->txdw0 |= htole32(R92S_TXDW0_LINIP); 2584 mlen = len; 2585 } else 2586 mlen = RSU_TXBUFSZ - sizeof(*txd); 2587 txd->txdw0 |= htole32(SM(R92S_TXDW0_PKTLEN, mlen)); 2588 memcpy(&txd[1], buf, mlen); 2589 data->buflen = sizeof(*txd) + mlen; 2590 RSU_DPRINTF(sc, RSU_DEBUG_TX | RSU_DEBUG_FW | RSU_DEBUG_RESET, 2591 "%s: starting transfer %p\n", 2592 __func__, data); 2593 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next); 2594 buf += mlen; 2595 len -= mlen; 2596 } 2597 usbd_transfer_start(sc->sc_xfer[which]); 2598 return (0); 2599 } 2600 2601 static int 2602 rsu_load_firmware(struct rsu_softc *sc) 2603 { 2604 const struct r92s_fw_hdr *hdr; 2605 struct r92s_fw_priv *dmem; 2606 struct ieee80211com *ic = &sc->sc_ic; 2607 const uint8_t *imem, *emem; 2608 int imemsz, ememsz; 2609 const struct firmware *fw; 2610 size_t size; 2611 uint32_t reg; 2612 int ntries, error; 2613 2614 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY) { 2615 RSU_DPRINTF(sc, RSU_DEBUG_ANY, 2616 "%s: Firmware already loaded\n", 2617 __func__); 2618 return (0); 2619 } 2620 2621 RSU_UNLOCK(sc); 2622 /* Read firmware image from the filesystem. */ 2623 if ((fw = firmware_get("rsu-rtl8712fw")) == NULL) { 2624 device_printf(sc->sc_dev, 2625 "%s: failed load firmware of file rsu-rtl8712fw\n", 2626 __func__); 2627 RSU_LOCK(sc); 2628 return (ENXIO); 2629 } 2630 RSU_LOCK(sc); 2631 size = fw->datasize; 2632 if (size < sizeof(*hdr)) { 2633 device_printf(sc->sc_dev, "firmware too short\n"); 2634 error = EINVAL; 2635 goto fail; 2636 } 2637 hdr = (const struct r92s_fw_hdr *)fw->data; 2638 if (hdr->signature != htole16(0x8712) && 2639 hdr->signature != htole16(0x8192)) { 2640 device_printf(sc->sc_dev, 2641 "invalid firmware signature 0x%x\n", 2642 le16toh(hdr->signature)); 2643 error = EINVAL; 2644 goto fail; 2645 } 2646 DPRINTF("FW V%d %02x-%02x %02x:%02x\n", le16toh(hdr->version), 2647 hdr->month, hdr->day, hdr->hour, hdr->minute); 2648 2649 /* Make sure that driver and firmware are in sync. */ 2650 if (hdr->privsz != htole32(sizeof(*dmem))) { 2651 device_printf(sc->sc_dev, "unsupported firmware image\n"); 2652 error = EINVAL; 2653 goto fail; 2654 } 2655 /* Get FW sections sizes. */ 2656 imemsz = le32toh(hdr->imemsz); 2657 ememsz = le32toh(hdr->sramsz); 2658 /* Check that all FW sections fit in image. */ 2659 if (size < sizeof(*hdr) + imemsz + ememsz) { 2660 device_printf(sc->sc_dev, "firmware too short\n"); 2661 error = EINVAL; 2662 goto fail; 2663 } 2664 imem = (const uint8_t *)&hdr[1]; 2665 emem = imem + imemsz; 2666 2667 /* Load IMEM section. */ 2668 error = rsu_fw_loadsection(sc, imem, imemsz); 2669 if (error != 0) { 2670 device_printf(sc->sc_dev, 2671 "could not load firmware section %s\n", "IMEM"); 2672 goto fail; 2673 } 2674 /* Wait for load to complete. */ 2675 for (ntries = 0; ntries != 50; ntries++) { 2676 rsu_ms_delay(sc, 10); 2677 reg = rsu_read_1(sc, R92S_TCR); 2678 if (reg & R92S_TCR_IMEM_CODE_DONE) 2679 break; 2680 } 2681 if (ntries == 50) { 2682 device_printf(sc->sc_dev, "timeout waiting for IMEM transfer\n"); 2683 error = ETIMEDOUT; 2684 goto fail; 2685 } 2686 /* Load EMEM section. */ 2687 error = rsu_fw_loadsection(sc, emem, ememsz); 2688 if (error != 0) { 2689 device_printf(sc->sc_dev, 2690 "could not load firmware section %s\n", "EMEM"); 2691 goto fail; 2692 } 2693 /* Wait for load to complete. */ 2694 for (ntries = 0; ntries != 50; ntries++) { 2695 rsu_ms_delay(sc, 10); 2696 reg = rsu_read_2(sc, R92S_TCR); 2697 if (reg & R92S_TCR_EMEM_CODE_DONE) 2698 break; 2699 } 2700 if (ntries == 50) { 2701 device_printf(sc->sc_dev, "timeout waiting for EMEM transfer\n"); 2702 error = ETIMEDOUT; 2703 goto fail; 2704 } 2705 /* Enable CPU. */ 2706 rsu_write_1(sc, R92S_SYS_CLKR, 2707 rsu_read_1(sc, R92S_SYS_CLKR) | R92S_SYS_CPU_CLKSEL); 2708 if (!(rsu_read_1(sc, R92S_SYS_CLKR) & R92S_SYS_CPU_CLKSEL)) { 2709 device_printf(sc->sc_dev, "could not enable system clock\n"); 2710 error = EIO; 2711 goto fail; 2712 } 2713 rsu_write_2(sc, R92S_SYS_FUNC_EN, 2714 rsu_read_2(sc, R92S_SYS_FUNC_EN) | R92S_FEN_CPUEN); 2715 if (!(rsu_read_2(sc, R92S_SYS_FUNC_EN) & R92S_FEN_CPUEN)) { 2716 device_printf(sc->sc_dev, 2717 "could not enable microcontroller\n"); 2718 error = EIO; 2719 goto fail; 2720 } 2721 /* Wait for CPU to initialize. */ 2722 for (ntries = 0; ntries < 100; ntries++) { 2723 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_IMEM_RDY) 2724 break; 2725 rsu_ms_delay(sc, 1); 2726 } 2727 if (ntries == 100) { 2728 device_printf(sc->sc_dev, 2729 "timeout waiting for microcontroller\n"); 2730 error = ETIMEDOUT; 2731 goto fail; 2732 } 2733 2734 /* Update DMEM section before loading. */ 2735 dmem = __DECONST(struct r92s_fw_priv *, &hdr->priv); 2736 memset(dmem, 0, sizeof(*dmem)); 2737 dmem->hci_sel = R92S_HCI_SEL_USB | R92S_HCI_SEL_8172; 2738 dmem->nendpoints = sc->sc_nendpoints; 2739 dmem->chip_version = sc->cut; 2740 dmem->rf_config = sc->sc_rftype; 2741 dmem->vcs_type = R92S_VCS_TYPE_AUTO; 2742 dmem->vcs_mode = R92S_VCS_MODE_RTS_CTS; 2743 dmem->turbo_mode = 0; 2744 dmem->bw40_en = !! (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40); 2745 dmem->amsdu2ampdu_en = !! (sc->sc_ht); 2746 dmem->ampdu_en = !! (sc->sc_ht); 2747 dmem->agg_offload = !! (sc->sc_ht); 2748 dmem->qos_en = 1; 2749 dmem->ps_offload = 1; 2750 dmem->lowpower_mode = 1; /* XXX TODO: configurable? */ 2751 /* Load DMEM section. */ 2752 error = rsu_fw_loadsection(sc, (uint8_t *)dmem, sizeof(*dmem)); 2753 if (error != 0) { 2754 device_printf(sc->sc_dev, 2755 "could not load firmware section %s\n", "DMEM"); 2756 goto fail; 2757 } 2758 /* Wait for load to complete. */ 2759 for (ntries = 0; ntries < 100; ntries++) { 2760 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_DMEM_CODE_DONE) 2761 break; 2762 rsu_ms_delay(sc, 1); 2763 } 2764 if (ntries == 100) { 2765 device_printf(sc->sc_dev, "timeout waiting for %s transfer\n", 2766 "DMEM"); 2767 error = ETIMEDOUT; 2768 goto fail; 2769 } 2770 /* Wait for firmware readiness. */ 2771 for (ntries = 0; ntries < 60; ntries++) { 2772 if (!(rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY)) 2773 break; 2774 rsu_ms_delay(sc, 1); 2775 } 2776 if (ntries == 60) { 2777 device_printf(sc->sc_dev, 2778 "timeout waiting for firmware readiness\n"); 2779 error = ETIMEDOUT; 2780 goto fail; 2781 } 2782 fail: 2783 firmware_put(fw, FIRMWARE_UNLOAD); 2784 return (error); 2785 } 2786 2787 2788 static int 2789 rsu_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2790 const struct ieee80211_bpf_params *params) 2791 { 2792 struct ieee80211com *ic = ni->ni_ic; 2793 struct rsu_softc *sc = ic->ic_softc; 2794 struct rsu_data *bf; 2795 2796 /* prevent management frames from being sent if we're not ready */ 2797 if (!sc->sc_running) { 2798 m_freem(m); 2799 return (ENETDOWN); 2800 } 2801 RSU_LOCK(sc); 2802 bf = rsu_getbuf(sc); 2803 if (bf == NULL) { 2804 m_freem(m); 2805 RSU_UNLOCK(sc); 2806 return (ENOBUFS); 2807 } 2808 if (rsu_tx_start(sc, ni, m, bf) != 0) { 2809 m_freem(m); 2810 rsu_freebuf(sc, bf); 2811 RSU_UNLOCK(sc); 2812 return (EIO); 2813 } 2814 RSU_UNLOCK(sc); 2815 2816 return (0); 2817 } 2818 2819 static void 2820 rsu_init(struct rsu_softc *sc) 2821 { 2822 struct ieee80211com *ic = &sc->sc_ic; 2823 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2824 uint8_t macaddr[IEEE80211_ADDR_LEN]; 2825 int error; 2826 int i; 2827 2828 RSU_ASSERT_LOCKED(sc); 2829 2830 /* Ensure the mbuf queue is drained */ 2831 rsu_drain_mbufq(sc); 2832 2833 /* Init host async commands ring. */ 2834 sc->cmdq.cur = sc->cmdq.next = sc->cmdq.queued = 0; 2835 2836 /* Reset power management state. */ 2837 rsu_write_1(sc, R92S_USB_HRPWM, 0); 2838 2839 /* Power on adapter. */ 2840 if (sc->cut == 1) 2841 rsu_power_on_acut(sc); 2842 else 2843 rsu_power_on_bcut(sc); 2844 2845 /* Load firmware. */ 2846 error = rsu_load_firmware(sc); 2847 if (error != 0) 2848 goto fail; 2849 2850 /* Enable Rx TCP checksum offload. */ 2851 rsu_write_4(sc, R92S_RCR, 2852 rsu_read_4(sc, R92S_RCR) | 0x04000000); 2853 /* Append PHY status. */ 2854 rsu_write_4(sc, R92S_RCR, 2855 rsu_read_4(sc, R92S_RCR) | 0x02000000); 2856 2857 rsu_write_4(sc, R92S_CR, 2858 rsu_read_4(sc, R92S_CR) & ~0xff000000); 2859 2860 /* Use 128 bytes pages. */ 2861 rsu_write_1(sc, 0x00b5, 2862 rsu_read_1(sc, 0x00b5) | 0x01); 2863 /* Enable USB Rx aggregation. */ 2864 rsu_write_1(sc, 0x00bd, 2865 rsu_read_1(sc, 0x00bd) | 0x80); 2866 /* Set USB Rx aggregation threshold. */ 2867 rsu_write_1(sc, 0x00d9, 0x01); 2868 /* Set USB Rx aggregation timeout (1.7ms/4). */ 2869 rsu_write_1(sc, 0xfe5b, 0x04); 2870 /* Fix USB Rx FIFO issue. */ 2871 rsu_write_1(sc, 0xfe5c, 2872 rsu_read_1(sc, 0xfe5c) | 0x80); 2873 2874 /* Set MAC address. */ 2875 IEEE80211_ADDR_COPY(macaddr, vap ? vap->iv_myaddr : ic->ic_macaddr); 2876 rsu_write_region_1(sc, R92S_MACID, macaddr, IEEE80211_ADDR_LEN); 2877 2878 /* It really takes 1.5 seconds for the firmware to boot: */ 2879 rsu_ms_delay(sc, 2000); 2880 2881 RSU_DPRINTF(sc, RSU_DEBUG_RESET, "%s: setting MAC address to %s\n", 2882 __func__, 2883 ether_sprintf(macaddr)); 2884 error = rsu_fw_cmd(sc, R92S_CMD_SET_MAC_ADDRESS, macaddr, 2885 IEEE80211_ADDR_LEN); 2886 if (error != 0) { 2887 device_printf(sc->sc_dev, "could not set MAC address\n"); 2888 goto fail; 2889 } 2890 2891 /* Set PS mode fully active */ 2892 error = rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE); 2893 2894 if (error != 0) { 2895 device_printf(sc->sc_dev, "could not set PS mode\n"); 2896 goto fail; 2897 } 2898 2899 sc->sc_scan_pass = 0; 2900 usbd_transfer_start(sc->sc_xfer[RSU_BULK_RX]); 2901 2902 /* We're ready to go. */ 2903 sc->sc_running = 1; 2904 sc->sc_scanning = 0; 2905 return; 2906 fail: 2907 /* Need to stop all failed transfers, if any */ 2908 for (i = 0; i != RSU_N_TRANSFER; i++) 2909 usbd_transfer_stop(sc->sc_xfer[i]); 2910 } 2911 2912 static void 2913 rsu_stop(struct rsu_softc *sc) 2914 { 2915 int i; 2916 2917 RSU_ASSERT_LOCKED(sc); 2918 2919 sc->sc_running = 0; 2920 sc->sc_calibrating = 0; 2921 taskqueue_cancel_timeout(taskqueue_thread, &sc->calib_task, NULL); 2922 taskqueue_cancel(taskqueue_thread, &sc->tx_task, NULL); 2923 2924 /* Power off adapter. */ 2925 rsu_power_off(sc); 2926 2927 for (i = 0; i < RSU_N_TRANSFER; i++) 2928 usbd_transfer_stop(sc->sc_xfer[i]); 2929 2930 /* Ensure the mbuf queue is drained */ 2931 rsu_drain_mbufq(sc); 2932 } 2933 2934 /* 2935 * Note: usb_pause_mtx() actually releases the mutex before calling pause(), 2936 * which breaks any kind of driver serialisation. 2937 */ 2938 static void 2939 rsu_ms_delay(struct rsu_softc *sc, int ms) 2940 { 2941 2942 //usb_pause_mtx(&sc->sc_mtx, hz / 1000); 2943 DELAY(ms * 1000); 2944 } 2945