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 tx a-mpdu 26 * o hostap / ibss / mesh 27 * o power-save operation 28 */ 29 30 #include "opt_wlan.h" 31 32 #include <sys/param.h> 33 #include <sys/endian.h> 34 #include <sys/sockio.h> 35 #include <sys/malloc.h> 36 #include <sys/mbuf.h> 37 #include <sys/kernel.h> 38 #include <sys/socket.h> 39 #include <sys/systm.h> 40 #include <sys/conf.h> 41 #include <sys/bus.h> 42 #include <sys/rman.h> 43 #include <sys/firmware.h> 44 #include <sys/module.h> 45 46 #include <machine/bus.h> 47 #include <machine/resource.h> 48 49 #include <net/bpf.h> 50 #include <net/if.h> 51 #include <net/if_var.h> 52 #include <net/if_arp.h> 53 #include <net/if_dl.h> 54 #include <net/if_media.h> 55 #include <net/if_types.h> 56 57 #include <netinet/in.h> 58 #include <netinet/in_systm.h> 59 #include <netinet/in_var.h> 60 #include <netinet/if_ether.h> 61 #include <netinet/ip.h> 62 63 #include <net80211/ieee80211_var.h> 64 #include <net80211/ieee80211_regdomain.h> 65 #include <net80211/ieee80211_radiotap.h> 66 67 #include <dev/usb/usb.h> 68 #include <dev/usb/usbdi.h> 69 #include "usbdevs.h" 70 71 #include <dev/rtwn/if_rtwn_ridx.h> /* XXX */ 72 #include <dev/usb/wlan/if_rsureg.h> 73 74 #define RSU_RATE_IS_CCK RTWN_RATE_IS_CCK 75 76 #ifdef USB_DEBUG 77 static int rsu_debug = 0; 78 SYSCTL_NODE(_hw_usb, OID_AUTO, rsu, CTLFLAG_RW, 0, "USB rsu"); 79 SYSCTL_INT(_hw_usb_rsu, OID_AUTO, debug, CTLFLAG_RWTUN, &rsu_debug, 0, 80 "Debug level"); 81 #define RSU_DPRINTF(_sc, _flg, ...) \ 82 do \ 83 if (((_flg) == (RSU_DEBUG_ANY)) || (rsu_debug & (_flg))) \ 84 device_printf((_sc)->sc_dev, __VA_ARGS__); \ 85 while (0) 86 #else 87 #define RSU_DPRINTF(_sc, _flg, ...) 88 #endif 89 90 static int rsu_enable_11n = 1; 91 TUNABLE_INT("hw.usb.rsu.enable_11n", &rsu_enable_11n); 92 93 #define RSU_DEBUG_ANY 0xffffffff 94 #define RSU_DEBUG_TX 0x00000001 95 #define RSU_DEBUG_RX 0x00000002 96 #define RSU_DEBUG_RESET 0x00000004 97 #define RSU_DEBUG_CALIB 0x00000008 98 #define RSU_DEBUG_STATE 0x00000010 99 #define RSU_DEBUG_SCAN 0x00000020 100 #define RSU_DEBUG_FWCMD 0x00000040 101 #define RSU_DEBUG_TXDONE 0x00000080 102 #define RSU_DEBUG_FW 0x00000100 103 #define RSU_DEBUG_FWDBG 0x00000200 104 #define RSU_DEBUG_AMPDU 0x00000400 105 #define RSU_DEBUG_KEY 0x00000800 106 #define RSU_DEBUG_USB 0x00001000 107 108 static const STRUCT_USB_HOST_ID rsu_devs[] = { 109 #define RSU_HT_NOT_SUPPORTED 0 110 #define RSU_HT_SUPPORTED 1 111 #define RSU_DEV_HT(v,p) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \ 112 RSU_HT_SUPPORTED) } 113 #define RSU_DEV(v,p) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \ 114 RSU_HT_NOT_SUPPORTED) } 115 RSU_DEV(ASUS, RTL8192SU), 116 RSU_DEV(AZUREWAVE, RTL8192SU_4), 117 RSU_DEV(SITECOMEU, WLA1000), 118 RSU_DEV_HT(ACCTON, RTL8192SU), 119 RSU_DEV_HT(ASUS, USBN10), 120 RSU_DEV_HT(AZUREWAVE, RTL8192SU_1), 121 RSU_DEV_HT(AZUREWAVE, RTL8192SU_2), 122 RSU_DEV_HT(AZUREWAVE, RTL8192SU_3), 123 RSU_DEV_HT(AZUREWAVE, RTL8192SU_5), 124 RSU_DEV_HT(BELKIN, RTL8192SU_1), 125 RSU_DEV_HT(BELKIN, RTL8192SU_2), 126 RSU_DEV_HT(BELKIN, RTL8192SU_3), 127 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_1), 128 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_2), 129 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_3), 130 RSU_DEV_HT(COREGA, RTL8192SU), 131 RSU_DEV_HT(DLINK2, DWA131A1), 132 RSU_DEV_HT(DLINK2, RTL8192SU_1), 133 RSU_DEV_HT(DLINK2, RTL8192SU_2), 134 RSU_DEV_HT(EDIMAX, RTL8192SU_1), 135 RSU_DEV_HT(EDIMAX, RTL8192SU_2), 136 RSU_DEV_HT(EDIMAX, EW7622UMN), 137 RSU_DEV_HT(GUILLEMOT, HWGUN54), 138 RSU_DEV_HT(GUILLEMOT, HWNUM300), 139 RSU_DEV_HT(HAWKING, RTL8192SU_1), 140 RSU_DEV_HT(HAWKING, RTL8192SU_2), 141 RSU_DEV_HT(PLANEX2, GWUSNANO), 142 RSU_DEV_HT(REALTEK, RTL8171), 143 RSU_DEV_HT(REALTEK, RTL8172), 144 RSU_DEV_HT(REALTEK, RTL8173), 145 RSU_DEV_HT(REALTEK, RTL8174), 146 RSU_DEV_HT(REALTEK, RTL8192SU), 147 RSU_DEV_HT(REALTEK, RTL8712), 148 RSU_DEV_HT(REALTEK, RTL8713), 149 RSU_DEV_HT(SENAO, RTL8192SU_1), 150 RSU_DEV_HT(SENAO, RTL8192SU_2), 151 RSU_DEV_HT(SITECOMEU, WL349V1), 152 RSU_DEV_HT(SITECOMEU, WL353), 153 RSU_DEV_HT(SWEEX2, LW154), 154 RSU_DEV_HT(TRENDNET, TEW646UBH), 155 #undef RSU_DEV_HT 156 #undef RSU_DEV 157 }; 158 159 static device_probe_t rsu_match; 160 static device_attach_t rsu_attach; 161 static device_detach_t rsu_detach; 162 static usb_callback_t rsu_bulk_tx_callback_be_bk; 163 static usb_callback_t rsu_bulk_tx_callback_vi_vo; 164 static usb_callback_t rsu_bulk_tx_callback_h2c; 165 static usb_callback_t rsu_bulk_rx_callback; 166 static usb_error_t rsu_do_request(struct rsu_softc *, 167 struct usb_device_request *, void *); 168 static struct ieee80211vap * 169 rsu_vap_create(struct ieee80211com *, const char name[], 170 int, enum ieee80211_opmode, int, const uint8_t bssid[], 171 const uint8_t mac[]); 172 static void rsu_vap_delete(struct ieee80211vap *); 173 static void rsu_scan_start(struct ieee80211com *); 174 static void rsu_scan_end(struct ieee80211com *); 175 static void rsu_getradiocaps(struct ieee80211com *, int, int *, 176 struct ieee80211_channel[]); 177 static void rsu_set_channel(struct ieee80211com *); 178 static void rsu_scan_curchan(struct ieee80211_scan_state *, unsigned long); 179 static void rsu_scan_mindwell(struct ieee80211_scan_state *); 180 static void rsu_update_promisc(struct ieee80211com *); 181 static uint8_t rsu_get_multi_pos(const uint8_t[]); 182 static void rsu_set_multi(struct rsu_softc *); 183 static void rsu_update_mcast(struct ieee80211com *); 184 static int rsu_alloc_rx_list(struct rsu_softc *); 185 static void rsu_free_rx_list(struct rsu_softc *); 186 static int rsu_alloc_tx_list(struct rsu_softc *); 187 static void rsu_free_tx_list(struct rsu_softc *); 188 static void rsu_free_list(struct rsu_softc *, struct rsu_data [], int); 189 static struct rsu_data *_rsu_getbuf(struct rsu_softc *); 190 static struct rsu_data *rsu_getbuf(struct rsu_softc *); 191 static void rsu_freebuf(struct rsu_softc *, struct rsu_data *); 192 static int rsu_write_region_1(struct rsu_softc *, uint16_t, uint8_t *, 193 int); 194 static void rsu_write_1(struct rsu_softc *, uint16_t, uint8_t); 195 static void rsu_write_2(struct rsu_softc *, uint16_t, uint16_t); 196 static void rsu_write_4(struct rsu_softc *, uint16_t, uint32_t); 197 static int rsu_read_region_1(struct rsu_softc *, uint16_t, uint8_t *, 198 int); 199 static uint8_t rsu_read_1(struct rsu_softc *, uint16_t); 200 static uint16_t rsu_read_2(struct rsu_softc *, uint16_t); 201 static uint32_t rsu_read_4(struct rsu_softc *, uint16_t); 202 static int rsu_fw_iocmd(struct rsu_softc *, uint32_t); 203 static uint8_t rsu_efuse_read_1(struct rsu_softc *, uint16_t); 204 static int rsu_read_rom(struct rsu_softc *); 205 static int rsu_fw_cmd(struct rsu_softc *, uint8_t, void *, int); 206 static void rsu_calib_task(void *, int); 207 static void rsu_tx_task(void *, int); 208 static void rsu_set_led(struct rsu_softc *, int); 209 static int rsu_monitor_newstate(struct ieee80211vap *, 210 enum ieee80211_state, int); 211 static int rsu_newstate(struct ieee80211vap *, enum ieee80211_state, int); 212 static int rsu_key_alloc(struct ieee80211vap *, struct ieee80211_key *, 213 ieee80211_keyix *, ieee80211_keyix *); 214 static int rsu_process_key(struct ieee80211vap *, 215 const struct ieee80211_key *, int); 216 static int rsu_key_set(struct ieee80211vap *, 217 const struct ieee80211_key *); 218 static int rsu_key_delete(struct ieee80211vap *, 219 const struct ieee80211_key *); 220 static int rsu_cam_read(struct rsu_softc *, uint8_t, uint32_t *); 221 static void rsu_cam_write(struct rsu_softc *, uint8_t, uint32_t); 222 static int rsu_key_check(struct rsu_softc *, ieee80211_keyix, int); 223 static uint8_t rsu_crypto_mode(struct rsu_softc *, u_int, int); 224 static int rsu_set_key_group(struct rsu_softc *, 225 const struct ieee80211_key *); 226 static int rsu_set_key_pair(struct rsu_softc *, 227 const struct ieee80211_key *); 228 static int rsu_reinit_static_keys(struct rsu_softc *); 229 static int rsu_delete_key(struct rsu_softc *sc, ieee80211_keyix); 230 static void rsu_delete_key_pair_cb(void *, int); 231 static int rsu_site_survey(struct rsu_softc *, 232 struct ieee80211_scan_ssid *); 233 static int rsu_join_bss(struct rsu_softc *, struct ieee80211_node *); 234 static int rsu_disconnect(struct rsu_softc *); 235 static int rsu_hwrssi_to_rssi(struct rsu_softc *, int hw_rssi); 236 static void rsu_event_survey(struct rsu_softc *, uint8_t *, int); 237 static void rsu_event_join_bss(struct rsu_softc *, uint8_t *, int); 238 static void rsu_rx_event(struct rsu_softc *, uint8_t, uint8_t *, int); 239 static void rsu_rx_multi_event(struct rsu_softc *, uint8_t *, int); 240 static int8_t rsu_get_rssi(struct rsu_softc *, int, void *); 241 static struct mbuf * rsu_rx_copy_to_mbuf(struct rsu_softc *, 242 struct r92s_rx_stat *, int); 243 static uint32_t rsu_get_tsf_low(struct rsu_softc *); 244 static uint32_t rsu_get_tsf_high(struct rsu_softc *); 245 static struct ieee80211_node * rsu_rx_frame(struct rsu_softc *, struct mbuf *); 246 static struct mbuf * rsu_rx_multi_frame(struct rsu_softc *, uint8_t *, int); 247 static struct mbuf * 248 rsu_rxeof(struct usb_xfer *, struct rsu_data *); 249 static void rsu_txeof(struct usb_xfer *, struct rsu_data *); 250 static int rsu_raw_xmit(struct ieee80211_node *, struct mbuf *, 251 const struct ieee80211_bpf_params *); 252 static void rsu_rxfilter_init(struct rsu_softc *); 253 static void rsu_rxfilter_set(struct rsu_softc *, uint32_t, uint32_t); 254 static void rsu_rxfilter_refresh(struct rsu_softc *); 255 static int rsu_init(struct rsu_softc *); 256 static int rsu_tx_start(struct rsu_softc *, struct ieee80211_node *, 257 struct mbuf *, struct rsu_data *); 258 static int rsu_transmit(struct ieee80211com *, struct mbuf *); 259 static void rsu_start(struct rsu_softc *); 260 static void _rsu_start(struct rsu_softc *); 261 static int rsu_ioctl_net(struct ieee80211com *, u_long, void *); 262 static void rsu_parent(struct ieee80211com *); 263 static void rsu_stop(struct rsu_softc *); 264 static void rsu_ms_delay(struct rsu_softc *, int); 265 266 static device_method_t rsu_methods[] = { 267 DEVMETHOD(device_probe, rsu_match), 268 DEVMETHOD(device_attach, rsu_attach), 269 DEVMETHOD(device_detach, rsu_detach), 270 271 DEVMETHOD_END 272 }; 273 274 static driver_t rsu_driver = { 275 .name = "rsu", 276 .methods = rsu_methods, 277 .size = sizeof(struct rsu_softc) 278 }; 279 280 static devclass_t rsu_devclass; 281 282 DRIVER_MODULE(rsu, uhub, rsu_driver, rsu_devclass, NULL, 0); 283 MODULE_DEPEND(rsu, wlan, 1, 1, 1); 284 MODULE_DEPEND(rsu, usb, 1, 1, 1); 285 MODULE_DEPEND(rsu, firmware, 1, 1, 1); 286 MODULE_VERSION(rsu, 1); 287 USB_PNP_HOST_INFO(rsu_devs); 288 289 static const uint8_t rsu_chan_2ghz[] = 290 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; 291 292 static uint8_t rsu_wme_ac_xfer_map[4] = { 293 [WME_AC_BE] = RSU_BULK_TX_BE_BK, 294 [WME_AC_BK] = RSU_BULK_TX_BE_BK, 295 [WME_AC_VI] = RSU_BULK_TX_VI_VO, 296 [WME_AC_VO] = RSU_BULK_TX_VI_VO, 297 }; 298 299 /* XXX hard-coded */ 300 #define RSU_H2C_ENDPOINT 3 301 302 static const struct usb_config rsu_config[RSU_N_TRANSFER] = { 303 [RSU_BULK_RX] = { 304 .type = UE_BULK, 305 .endpoint = UE_ADDR_ANY, 306 .direction = UE_DIR_IN, 307 .bufsize = RSU_RXBUFSZ, 308 .flags = { 309 .pipe_bof = 1, 310 .short_xfer_ok = 1 311 }, 312 .callback = rsu_bulk_rx_callback 313 }, 314 [RSU_BULK_TX_BE_BK] = { 315 .type = UE_BULK, 316 .endpoint = 0x06, 317 .direction = UE_DIR_OUT, 318 .bufsize = RSU_TXBUFSZ, 319 .flags = { 320 .ext_buffer = 1, 321 .pipe_bof = 1, 322 .force_short_xfer = 1 323 }, 324 .callback = rsu_bulk_tx_callback_be_bk, 325 .timeout = RSU_TX_TIMEOUT 326 }, 327 [RSU_BULK_TX_VI_VO] = { 328 .type = UE_BULK, 329 .endpoint = 0x04, 330 .direction = UE_DIR_OUT, 331 .bufsize = RSU_TXBUFSZ, 332 .flags = { 333 .ext_buffer = 1, 334 .pipe_bof = 1, 335 .force_short_xfer = 1 336 }, 337 .callback = rsu_bulk_tx_callback_vi_vo, 338 .timeout = RSU_TX_TIMEOUT 339 }, 340 [RSU_BULK_TX_H2C] = { 341 .type = UE_BULK, 342 .endpoint = 0x0d, 343 .direction = UE_DIR_OUT, 344 .bufsize = RSU_TXBUFSZ, 345 .flags = { 346 .ext_buffer = 1, 347 .pipe_bof = 1, 348 .short_xfer_ok = 1 349 }, 350 .callback = rsu_bulk_tx_callback_h2c, 351 .timeout = RSU_TX_TIMEOUT 352 }, 353 }; 354 355 static int 356 rsu_match(device_t self) 357 { 358 struct usb_attach_arg *uaa = device_get_ivars(self); 359 360 if (uaa->usb_mode != USB_MODE_HOST || 361 uaa->info.bIfaceIndex != 0 || 362 uaa->info.bConfigIndex != 0) 363 return (ENXIO); 364 365 return (usbd_lookup_id_by_uaa(rsu_devs, sizeof(rsu_devs), uaa)); 366 } 367 368 static int 369 rsu_send_mgmt(struct ieee80211_node *ni, int type, int arg) 370 { 371 372 return (ENOTSUP); 373 } 374 375 static void 376 rsu_update_chw(struct ieee80211com *ic) 377 { 378 379 } 380 381 /* 382 * notification from net80211 that it'd like to do A-MPDU on the given TID. 383 * 384 * Note: this actually hangs traffic at the present moment, so don't use it. 385 * The firmware debug does indiciate it's sending and establishing a TX AMPDU 386 * session, but then no traffic flows. 387 */ 388 static int 389 rsu_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap) 390 { 391 #if 0 392 struct rsu_softc *sc = ni->ni_ic->ic_softc; 393 struct r92s_add_ba_req req; 394 395 /* Don't enable if it's requested or running */ 396 if (IEEE80211_AMPDU_REQUESTED(tap)) 397 return (0); 398 if (IEEE80211_AMPDU_RUNNING(tap)) 399 return (0); 400 401 /* We've decided to send addba; so send it */ 402 req.tid = htole32(tap->txa_tid); 403 404 /* Attempt net80211 state */ 405 if (ieee80211_ampdu_tx_request_ext(ni, tap->txa_tid) != 1) 406 return (0); 407 408 /* Send the firmware command */ 409 RSU_DPRINTF(sc, RSU_DEBUG_AMPDU, "%s: establishing AMPDU TX for TID %d\n", 410 __func__, 411 tap->txa_tid); 412 413 RSU_LOCK(sc); 414 if (rsu_fw_cmd(sc, R92S_CMD_ADDBA_REQ, &req, sizeof(req)) != 1) { 415 RSU_UNLOCK(sc); 416 /* Mark failure */ 417 (void) ieee80211_ampdu_tx_request_active_ext(ni, tap->txa_tid, 0); 418 return (0); 419 } 420 RSU_UNLOCK(sc); 421 422 /* Mark success; we don't get any further notifications */ 423 (void) ieee80211_ampdu_tx_request_active_ext(ni, tap->txa_tid, 1); 424 #endif 425 /* Return 0, we're driving this ourselves */ 426 return (0); 427 } 428 429 static int 430 rsu_wme_update(struct ieee80211com *ic) 431 { 432 433 /* Firmware handles this; not our problem */ 434 return (0); 435 } 436 437 static int 438 rsu_attach(device_t self) 439 { 440 struct usb_attach_arg *uaa = device_get_ivars(self); 441 struct rsu_softc *sc = device_get_softc(self); 442 struct ieee80211com *ic = &sc->sc_ic; 443 int error; 444 uint8_t iface_index; 445 struct usb_interface *iface; 446 const char *rft; 447 448 device_set_usb_desc(self); 449 sc->sc_udev = uaa->device; 450 sc->sc_dev = self; 451 sc->sc_rx_checksum_enable = 1; 452 if (rsu_enable_11n) 453 sc->sc_ht = !! (USB_GET_DRIVER_INFO(uaa) & RSU_HT_SUPPORTED); 454 455 /* Get number of endpoints */ 456 iface = usbd_get_iface(sc->sc_udev, 0); 457 sc->sc_nendpoints = iface->idesc->bNumEndpoints; 458 459 /* Endpoints are hard-coded for now, so enforce 4-endpoint only */ 460 if (sc->sc_nendpoints != 4) { 461 device_printf(sc->sc_dev, 462 "the driver currently only supports 4-endpoint devices\n"); 463 return (ENXIO); 464 } 465 466 mtx_init(&sc->sc_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK, 467 MTX_DEF); 468 RSU_DELKEY_BMAP_LOCK_INIT(sc); 469 TIMEOUT_TASK_INIT(taskqueue_thread, &sc->calib_task, 0, 470 rsu_calib_task, sc); 471 TASK_INIT(&sc->del_key_task, 0, rsu_delete_key_pair_cb, sc); 472 TASK_INIT(&sc->tx_task, 0, rsu_tx_task, sc); 473 mbufq_init(&sc->sc_snd, ifqmaxlen); 474 475 /* Allocate Tx/Rx buffers. */ 476 error = rsu_alloc_rx_list(sc); 477 if (error != 0) { 478 device_printf(sc->sc_dev, "could not allocate Rx buffers\n"); 479 goto fail_usb; 480 } 481 482 error = rsu_alloc_tx_list(sc); 483 if (error != 0) { 484 device_printf(sc->sc_dev, "could not allocate Tx buffers\n"); 485 rsu_free_rx_list(sc); 486 goto fail_usb; 487 } 488 489 iface_index = 0; 490 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer, 491 rsu_config, RSU_N_TRANSFER, sc, &sc->sc_mtx); 492 if (error) { 493 device_printf(sc->sc_dev, 494 "could not allocate USB transfers, err=%s\n", 495 usbd_errstr(error)); 496 goto fail_usb; 497 } 498 RSU_LOCK(sc); 499 /* Read chip revision. */ 500 sc->cut = MS(rsu_read_4(sc, R92S_PMC_FSM), R92S_PMC_FSM_CUT); 501 if (sc->cut != 3) 502 sc->cut = (sc->cut >> 1) + 1; 503 error = rsu_read_rom(sc); 504 RSU_UNLOCK(sc); 505 if (error != 0) { 506 device_printf(self, "could not read ROM\n"); 507 goto fail_rom; 508 } 509 510 /* Figure out TX/RX streams */ 511 switch (sc->rom[84]) { 512 case 0x0: 513 sc->sc_rftype = RTL8712_RFCONFIG_1T1R; 514 sc->sc_nrxstream = 1; 515 sc->sc_ntxstream = 1; 516 rft = "1T1R"; 517 break; 518 case 0x1: 519 sc->sc_rftype = RTL8712_RFCONFIG_1T2R; 520 sc->sc_nrxstream = 2; 521 sc->sc_ntxstream = 1; 522 rft = "1T2R"; 523 break; 524 case 0x2: 525 sc->sc_rftype = RTL8712_RFCONFIG_2T2R; 526 sc->sc_nrxstream = 2; 527 sc->sc_ntxstream = 2; 528 rft = "2T2R"; 529 break; 530 case 0x3: /* "green" NIC */ 531 sc->sc_rftype = RTL8712_RFCONFIG_1T2R; 532 sc->sc_nrxstream = 2; 533 sc->sc_ntxstream = 1; 534 rft = "1T2R ('green')"; 535 break; 536 default: 537 device_printf(sc->sc_dev, 538 "%s: unknown board type (rfconfig=0x%02x)\n", 539 __func__, 540 sc->rom[84]); 541 goto fail_rom; 542 } 543 544 IEEE80211_ADDR_COPY(ic->ic_macaddr, &sc->rom[0x12]); 545 device_printf(self, "MAC/BB RTL8712 cut %d %s\n", sc->cut, rft); 546 547 ic->ic_softc = sc; 548 ic->ic_name = device_get_nameunit(self); 549 ic->ic_phytype = IEEE80211_T_OFDM; /* Not only, but not used. */ 550 ic->ic_opmode = IEEE80211_M_STA; /* Default to BSS mode. */ 551 552 /* Set device capabilities. */ 553 ic->ic_caps = 554 IEEE80211_C_STA | /* station mode */ 555 IEEE80211_C_MONITOR | /* monitor mode supported */ 556 #if 0 557 IEEE80211_C_BGSCAN | /* Background scan. */ 558 #endif 559 IEEE80211_C_SHPREAMBLE | /* Short preamble supported. */ 560 IEEE80211_C_WME | /* WME/QoS */ 561 IEEE80211_C_SHSLOT | /* Short slot time supported. */ 562 IEEE80211_C_WPA; /* WPA/RSN. */ 563 564 ic->ic_cryptocaps = 565 IEEE80211_CRYPTO_WEP | 566 IEEE80211_CRYPTO_TKIP | 567 IEEE80211_CRYPTO_AES_CCM; 568 569 /* Check if HT support is present. */ 570 if (sc->sc_ht) { 571 device_printf(sc->sc_dev, "%s: enabling 11n\n", __func__); 572 573 /* Enable basic HT */ 574 ic->ic_htcaps = IEEE80211_HTC_HT | 575 #if 0 576 IEEE80211_HTC_AMPDU | 577 #endif 578 IEEE80211_HTC_AMSDU | 579 IEEE80211_HTCAP_MAXAMSDU_3839 | 580 IEEE80211_HTCAP_SMPS_OFF; 581 ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40; 582 583 /* set number of spatial streams */ 584 ic->ic_txstream = sc->sc_ntxstream; 585 ic->ic_rxstream = sc->sc_nrxstream; 586 } 587 ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD; 588 589 rsu_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans, 590 ic->ic_channels); 591 592 ieee80211_ifattach(ic); 593 ic->ic_raw_xmit = rsu_raw_xmit; 594 ic->ic_scan_start = rsu_scan_start; 595 ic->ic_scan_end = rsu_scan_end; 596 ic->ic_getradiocaps = rsu_getradiocaps; 597 ic->ic_set_channel = rsu_set_channel; 598 ic->ic_scan_curchan = rsu_scan_curchan; 599 ic->ic_scan_mindwell = rsu_scan_mindwell; 600 ic->ic_vap_create = rsu_vap_create; 601 ic->ic_vap_delete = rsu_vap_delete; 602 ic->ic_update_promisc = rsu_update_promisc; 603 ic->ic_update_mcast = rsu_update_mcast; 604 ic->ic_ioctl = rsu_ioctl_net; 605 ic->ic_parent = rsu_parent; 606 ic->ic_transmit = rsu_transmit; 607 ic->ic_send_mgmt = rsu_send_mgmt; 608 ic->ic_update_chw = rsu_update_chw; 609 ic->ic_ampdu_enable = rsu_ampdu_enable; 610 ic->ic_wme.wme_update = rsu_wme_update; 611 612 ieee80211_radiotap_attach(ic, &sc->sc_txtap.wt_ihdr, 613 sizeof(sc->sc_txtap), RSU_TX_RADIOTAP_PRESENT, 614 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 615 RSU_RX_RADIOTAP_PRESENT); 616 617 if (bootverbose) 618 ieee80211_announce(ic); 619 620 return (0); 621 622 fail_rom: 623 usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER); 624 fail_usb: 625 mtx_destroy(&sc->sc_mtx); 626 return (ENXIO); 627 } 628 629 static int 630 rsu_detach(device_t self) 631 { 632 struct rsu_softc *sc = device_get_softc(self); 633 struct ieee80211com *ic = &sc->sc_ic; 634 635 rsu_stop(sc); 636 637 usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER); 638 639 /* 640 * Free buffers /before/ we detach from net80211, else node 641 * references to destroyed vaps will lead to a panic. 642 */ 643 /* Free Tx/Rx buffers. */ 644 RSU_LOCK(sc); 645 rsu_free_tx_list(sc); 646 rsu_free_rx_list(sc); 647 RSU_UNLOCK(sc); 648 649 /* Frames are freed; detach from net80211 */ 650 ieee80211_ifdetach(ic); 651 652 taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task); 653 taskqueue_drain(taskqueue_thread, &sc->del_key_task); 654 taskqueue_drain(taskqueue_thread, &sc->tx_task); 655 656 RSU_DELKEY_BMAP_LOCK_DESTROY(sc); 657 mtx_destroy(&sc->sc_mtx); 658 659 return (0); 660 } 661 662 static usb_error_t 663 rsu_do_request(struct rsu_softc *sc, struct usb_device_request *req, 664 void *data) 665 { 666 usb_error_t err; 667 int ntries = 10; 668 669 RSU_ASSERT_LOCKED(sc); 670 671 while (ntries--) { 672 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, 673 req, data, 0, NULL, 250 /* ms */); 674 if (err == 0 || err == USB_ERR_NOT_CONFIGURED) 675 break; 676 RSU_DPRINTF(sc, RSU_DEBUG_USB, 677 "Control request failed, %s (retries left: %d)\n", 678 usbd_errstr(err), ntries); 679 rsu_ms_delay(sc, 10); 680 } 681 682 return (err); 683 } 684 685 static struct ieee80211vap * 686 rsu_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 687 enum ieee80211_opmode opmode, int flags, 688 const uint8_t bssid[IEEE80211_ADDR_LEN], 689 const uint8_t mac[IEEE80211_ADDR_LEN]) 690 { 691 struct rsu_softc *sc = ic->ic_softc; 692 struct rsu_vap *uvp; 693 struct ieee80211vap *vap; 694 struct ifnet *ifp; 695 696 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 697 return (NULL); 698 699 uvp = malloc(sizeof(struct rsu_vap), M_80211_VAP, M_WAITOK | M_ZERO); 700 vap = &uvp->vap; 701 702 if (ieee80211_vap_setup(ic, vap, name, unit, opmode, 703 flags, bssid) != 0) { 704 /* out of memory */ 705 free(uvp, M_80211_VAP); 706 return (NULL); 707 } 708 709 ifp = vap->iv_ifp; 710 ifp->if_capabilities = IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6; 711 RSU_LOCK(sc); 712 if (sc->sc_rx_checksum_enable) 713 ifp->if_capenable |= IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6; 714 RSU_UNLOCK(sc); 715 716 /* override state transition machine */ 717 uvp->newstate = vap->iv_newstate; 718 if (opmode == IEEE80211_M_MONITOR) 719 vap->iv_newstate = rsu_monitor_newstate; 720 else 721 vap->iv_newstate = rsu_newstate; 722 vap->iv_key_alloc = rsu_key_alloc; 723 vap->iv_key_set = rsu_key_set; 724 vap->iv_key_delete = rsu_key_delete; 725 726 /* Limits from the r92su driver */ 727 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_16; 728 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_32K; 729 730 /* complete setup */ 731 ieee80211_vap_attach(vap, ieee80211_media_change, 732 ieee80211_media_status, mac); 733 ic->ic_opmode = opmode; 734 735 return (vap); 736 } 737 738 static void 739 rsu_vap_delete(struct ieee80211vap *vap) 740 { 741 struct rsu_vap *uvp = RSU_VAP(vap); 742 743 ieee80211_vap_detach(vap); 744 free(uvp, M_80211_VAP); 745 } 746 747 static void 748 rsu_scan_start(struct ieee80211com *ic) 749 { 750 struct rsu_softc *sc = ic->ic_softc; 751 struct ieee80211_scan_state *ss = ic->ic_scan; 752 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 753 int error; 754 755 /* Scanning is done by the firmware. */ 756 RSU_LOCK(sc); 757 sc->sc_active_scan = !!(ss->ss_flags & IEEE80211_SCAN_ACTIVE); 758 /* XXX TODO: force awake if in network-sleep? */ 759 error = rsu_site_survey(sc, ss->ss_nssid > 0 ? &ss->ss_ssid[0] : NULL); 760 RSU_UNLOCK(sc); 761 if (error != 0) { 762 device_printf(sc->sc_dev, 763 "could not send site survey command\n"); 764 ieee80211_cancel_scan(vap); 765 } 766 } 767 768 static void 769 rsu_scan_end(struct ieee80211com *ic) 770 { 771 /* Nothing to do here. */ 772 } 773 774 static void 775 rsu_getradiocaps(struct ieee80211com *ic, 776 int maxchans, int *nchans, struct ieee80211_channel chans[]) 777 { 778 struct rsu_softc *sc = ic->ic_softc; 779 uint8_t bands[IEEE80211_MODE_BYTES]; 780 781 /* Set supported .11b and .11g rates. */ 782 memset(bands, 0, sizeof(bands)); 783 setbit(bands, IEEE80211_MODE_11B); 784 setbit(bands, IEEE80211_MODE_11G); 785 if (sc->sc_ht) 786 setbit(bands, IEEE80211_MODE_11NG); 787 ieee80211_add_channel_list_2ghz(chans, maxchans, nchans, 788 rsu_chan_2ghz, nitems(rsu_chan_2ghz), bands, 789 (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0); 790 } 791 792 static void 793 rsu_set_channel(struct ieee80211com *ic) 794 { 795 struct rsu_softc *sc = ic->ic_softc; 796 797 /* 798 * Only need to set the channel in Monitor mode. AP scanning and auth 799 * are already taken care of by their respective firmware commands. 800 */ 801 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 802 struct r92s_set_channel cmd; 803 int error; 804 805 cmd.channel = IEEE80211_CHAN2IEEE(ic->ic_curchan); 806 807 RSU_LOCK(sc); 808 error = rsu_fw_cmd(sc, R92S_CMD_SET_CHANNEL, &cmd, 809 sizeof(cmd)); 810 if (error != 0) { 811 device_printf(sc->sc_dev, 812 "%s: error %d setting channel\n", __func__, 813 error); 814 } 815 RSU_UNLOCK(sc); 816 } 817 } 818 819 static void 820 rsu_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell) 821 { 822 /* Scan is done in rsu_scan_start(). */ 823 } 824 825 /** 826 * Called by the net80211 framework to indicate 827 * the minimum dwell time has been met, terminate the scan. 828 * We don't actually terminate the scan as the firmware will notify 829 * us when it's finished and we have no way to interrupt it. 830 */ 831 static void 832 rsu_scan_mindwell(struct ieee80211_scan_state *ss) 833 { 834 /* NB: don't try to abort scan; wait for firmware to finish */ 835 } 836 837 static void 838 rsu_update_promisc(struct ieee80211com *ic) 839 { 840 struct rsu_softc *sc = ic->ic_softc; 841 842 RSU_LOCK(sc); 843 if (sc->sc_running) 844 rsu_rxfilter_refresh(sc); 845 RSU_UNLOCK(sc); 846 } 847 848 /* 849 * The same as rtwn_get_multi_pos() / rtwn_set_multi(). 850 */ 851 static uint8_t 852 rsu_get_multi_pos(const uint8_t maddr[]) 853 { 854 uint64_t mask = 0x00004d101df481b4; 855 uint8_t pos = 0x27; /* initial value */ 856 int i, j; 857 858 for (i = 0; i < IEEE80211_ADDR_LEN; i++) 859 for (j = (i == 0) ? 1 : 0; j < 8; j++) 860 if ((maddr[i] >> j) & 1) 861 pos ^= (mask >> (i * 8 + j - 1)); 862 863 pos &= 0x3f; 864 865 return (pos); 866 } 867 868 static void 869 rsu_set_multi(struct rsu_softc *sc) 870 { 871 struct ieee80211com *ic = &sc->sc_ic; 872 uint32_t mfilt[2]; 873 874 RSU_ASSERT_LOCKED(sc); 875 876 /* general structure was copied from ath(4). */ 877 if (ic->ic_allmulti == 0) { 878 struct ieee80211vap *vap; 879 struct ifnet *ifp; 880 struct ifmultiaddr *ifma; 881 882 /* 883 * Merge multicast addresses to form the hardware filter. 884 */ 885 mfilt[0] = mfilt[1] = 0; 886 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 887 ifp = vap->iv_ifp; 888 if_maddr_rlock(ifp); 889 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 890 caddr_t dl; 891 uint8_t pos; 892 893 dl = LLADDR((struct sockaddr_dl *) 894 ifma->ifma_addr); 895 pos = rsu_get_multi_pos(dl); 896 897 mfilt[pos / 32] |= (1 << (pos % 32)); 898 } 899 if_maddr_runlock(ifp); 900 } 901 } else 902 mfilt[0] = mfilt[1] = ~0; 903 904 rsu_write_4(sc, R92S_MAR + 0, mfilt[0]); 905 rsu_write_4(sc, R92S_MAR + 4, mfilt[1]); 906 907 RSU_DPRINTF(sc, RSU_DEBUG_STATE, "%s: MC filter %08x:%08x\n", 908 __func__, mfilt[0], mfilt[1]); 909 } 910 911 static void 912 rsu_update_mcast(struct ieee80211com *ic) 913 { 914 struct rsu_softc *sc = ic->ic_softc; 915 916 RSU_LOCK(sc); 917 if (sc->sc_running) 918 rsu_set_multi(sc); 919 RSU_UNLOCK(sc); 920 } 921 922 static int 923 rsu_alloc_list(struct rsu_softc *sc, struct rsu_data data[], 924 int ndata, int maxsz) 925 { 926 int i, error; 927 928 for (i = 0; i < ndata; i++) { 929 struct rsu_data *dp = &data[i]; 930 dp->sc = sc; 931 dp->m = NULL; 932 dp->buf = malloc(maxsz, M_USBDEV, M_NOWAIT); 933 if (dp->buf == NULL) { 934 device_printf(sc->sc_dev, 935 "could not allocate buffer\n"); 936 error = ENOMEM; 937 goto fail; 938 } 939 dp->ni = NULL; 940 } 941 942 return (0); 943 fail: 944 rsu_free_list(sc, data, ndata); 945 return (error); 946 } 947 948 static int 949 rsu_alloc_rx_list(struct rsu_softc *sc) 950 { 951 int error, i; 952 953 error = rsu_alloc_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT, 954 RSU_RXBUFSZ); 955 if (error != 0) 956 return (error); 957 958 STAILQ_INIT(&sc->sc_rx_active); 959 STAILQ_INIT(&sc->sc_rx_inactive); 960 961 for (i = 0; i < RSU_RX_LIST_COUNT; i++) 962 STAILQ_INSERT_HEAD(&sc->sc_rx_inactive, &sc->sc_rx[i], next); 963 964 return (0); 965 } 966 967 static int 968 rsu_alloc_tx_list(struct rsu_softc *sc) 969 { 970 int error, i; 971 972 error = rsu_alloc_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT, 973 RSU_TXBUFSZ); 974 if (error != 0) 975 return (error); 976 977 STAILQ_INIT(&sc->sc_tx_inactive); 978 979 for (i = 0; i != RSU_N_TRANSFER; i++) { 980 STAILQ_INIT(&sc->sc_tx_active[i]); 981 STAILQ_INIT(&sc->sc_tx_pending[i]); 982 } 983 984 for (i = 0; i < RSU_TX_LIST_COUNT; i++) { 985 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, &sc->sc_tx[i], next); 986 } 987 988 return (0); 989 } 990 991 static void 992 rsu_free_tx_list(struct rsu_softc *sc) 993 { 994 int i; 995 996 /* prevent further allocations from TX list(s) */ 997 STAILQ_INIT(&sc->sc_tx_inactive); 998 999 for (i = 0; i != RSU_N_TRANSFER; i++) { 1000 STAILQ_INIT(&sc->sc_tx_active[i]); 1001 STAILQ_INIT(&sc->sc_tx_pending[i]); 1002 } 1003 1004 rsu_free_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT); 1005 } 1006 1007 static void 1008 rsu_free_rx_list(struct rsu_softc *sc) 1009 { 1010 /* prevent further allocations from RX list(s) */ 1011 STAILQ_INIT(&sc->sc_rx_inactive); 1012 STAILQ_INIT(&sc->sc_rx_active); 1013 1014 rsu_free_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT); 1015 } 1016 1017 static void 1018 rsu_free_list(struct rsu_softc *sc, struct rsu_data data[], int ndata) 1019 { 1020 int i; 1021 1022 for (i = 0; i < ndata; i++) { 1023 struct rsu_data *dp = &data[i]; 1024 1025 if (dp->buf != NULL) { 1026 free(dp->buf, M_USBDEV); 1027 dp->buf = NULL; 1028 } 1029 if (dp->ni != NULL) { 1030 ieee80211_free_node(dp->ni); 1031 dp->ni = NULL; 1032 } 1033 } 1034 } 1035 1036 static struct rsu_data * 1037 _rsu_getbuf(struct rsu_softc *sc) 1038 { 1039 struct rsu_data *bf; 1040 1041 bf = STAILQ_FIRST(&sc->sc_tx_inactive); 1042 if (bf != NULL) 1043 STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next); 1044 else 1045 bf = NULL; 1046 return (bf); 1047 } 1048 1049 static struct rsu_data * 1050 rsu_getbuf(struct rsu_softc *sc) 1051 { 1052 struct rsu_data *bf; 1053 1054 RSU_ASSERT_LOCKED(sc); 1055 1056 bf = _rsu_getbuf(sc); 1057 if (bf == NULL) { 1058 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: no buffers\n", __func__); 1059 } 1060 return (bf); 1061 } 1062 1063 static void 1064 rsu_freebuf(struct rsu_softc *sc, struct rsu_data *bf) 1065 { 1066 1067 RSU_ASSERT_LOCKED(sc); 1068 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, bf, next); 1069 } 1070 1071 static int 1072 rsu_write_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf, 1073 int len) 1074 { 1075 usb_device_request_t req; 1076 1077 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1078 req.bRequest = R92S_REQ_REGS; 1079 USETW(req.wValue, addr); 1080 USETW(req.wIndex, 0); 1081 USETW(req.wLength, len); 1082 1083 return (rsu_do_request(sc, &req, buf)); 1084 } 1085 1086 static void 1087 rsu_write_1(struct rsu_softc *sc, uint16_t addr, uint8_t val) 1088 { 1089 rsu_write_region_1(sc, addr, &val, 1); 1090 } 1091 1092 static void 1093 rsu_write_2(struct rsu_softc *sc, uint16_t addr, uint16_t val) 1094 { 1095 val = htole16(val); 1096 rsu_write_region_1(sc, addr, (uint8_t *)&val, 2); 1097 } 1098 1099 static void 1100 rsu_write_4(struct rsu_softc *sc, uint16_t addr, uint32_t val) 1101 { 1102 val = htole32(val); 1103 rsu_write_region_1(sc, addr, (uint8_t *)&val, 4); 1104 } 1105 1106 static int 1107 rsu_read_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf, 1108 int len) 1109 { 1110 usb_device_request_t req; 1111 1112 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1113 req.bRequest = R92S_REQ_REGS; 1114 USETW(req.wValue, addr); 1115 USETW(req.wIndex, 0); 1116 USETW(req.wLength, len); 1117 1118 return (rsu_do_request(sc, &req, buf)); 1119 } 1120 1121 static uint8_t 1122 rsu_read_1(struct rsu_softc *sc, uint16_t addr) 1123 { 1124 uint8_t val; 1125 1126 if (rsu_read_region_1(sc, addr, &val, 1) != 0) 1127 return (0xff); 1128 return (val); 1129 } 1130 1131 static uint16_t 1132 rsu_read_2(struct rsu_softc *sc, uint16_t addr) 1133 { 1134 uint16_t val; 1135 1136 if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 2) != 0) 1137 return (0xffff); 1138 return (le16toh(val)); 1139 } 1140 1141 static uint32_t 1142 rsu_read_4(struct rsu_softc *sc, uint16_t addr) 1143 { 1144 uint32_t val; 1145 1146 if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 4) != 0) 1147 return (0xffffffff); 1148 return (le32toh(val)); 1149 } 1150 1151 static int 1152 rsu_fw_iocmd(struct rsu_softc *sc, uint32_t iocmd) 1153 { 1154 int ntries; 1155 1156 rsu_write_4(sc, R92S_IOCMD_CTRL, iocmd); 1157 rsu_ms_delay(sc, 1); 1158 for (ntries = 0; ntries < 50; ntries++) { 1159 if (rsu_read_4(sc, R92S_IOCMD_CTRL) == 0) 1160 return (0); 1161 rsu_ms_delay(sc, 1); 1162 } 1163 return (ETIMEDOUT); 1164 } 1165 1166 static uint8_t 1167 rsu_efuse_read_1(struct rsu_softc *sc, uint16_t addr) 1168 { 1169 uint32_t reg; 1170 int ntries; 1171 1172 reg = rsu_read_4(sc, R92S_EFUSE_CTRL); 1173 reg = RW(reg, R92S_EFUSE_CTRL_ADDR, addr); 1174 reg &= ~R92S_EFUSE_CTRL_VALID; 1175 rsu_write_4(sc, R92S_EFUSE_CTRL, reg); 1176 /* Wait for read operation to complete. */ 1177 for (ntries = 0; ntries < 100; ntries++) { 1178 reg = rsu_read_4(sc, R92S_EFUSE_CTRL); 1179 if (reg & R92S_EFUSE_CTRL_VALID) 1180 return (MS(reg, R92S_EFUSE_CTRL_DATA)); 1181 rsu_ms_delay(sc, 1); 1182 } 1183 device_printf(sc->sc_dev, 1184 "could not read efuse byte at address 0x%x\n", addr); 1185 return (0xff); 1186 } 1187 1188 static int 1189 rsu_read_rom(struct rsu_softc *sc) 1190 { 1191 uint8_t *rom = sc->rom; 1192 uint16_t addr = 0; 1193 uint32_t reg; 1194 uint8_t off, msk; 1195 int i; 1196 1197 /* Make sure that ROM type is eFuse and that autoload succeeded. */ 1198 reg = rsu_read_1(sc, R92S_EE_9346CR); 1199 if ((reg & (R92S_9356SEL | R92S_EEPROM_EN)) != R92S_EEPROM_EN) 1200 return (EIO); 1201 1202 /* Turn on 2.5V to prevent eFuse leakage. */ 1203 reg = rsu_read_1(sc, R92S_EFUSE_TEST + 3); 1204 rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg | 0x80); 1205 rsu_ms_delay(sc, 1); 1206 rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg & ~0x80); 1207 1208 /* Read full ROM image. */ 1209 memset(&sc->rom, 0xff, sizeof(sc->rom)); 1210 while (addr < 512) { 1211 reg = rsu_efuse_read_1(sc, addr); 1212 if (reg == 0xff) 1213 break; 1214 addr++; 1215 off = reg >> 4; 1216 msk = reg & 0xf; 1217 for (i = 0; i < 4; i++) { 1218 if (msk & (1 << i)) 1219 continue; 1220 rom[off * 8 + i * 2 + 0] = 1221 rsu_efuse_read_1(sc, addr); 1222 addr++; 1223 rom[off * 8 + i * 2 + 1] = 1224 rsu_efuse_read_1(sc, addr); 1225 addr++; 1226 } 1227 } 1228 #ifdef USB_DEBUG 1229 if (rsu_debug & RSU_DEBUG_RESET) { 1230 /* Dump ROM content. */ 1231 printf("\n"); 1232 for (i = 0; i < sizeof(sc->rom); i++) 1233 printf("%02x:", rom[i]); 1234 printf("\n"); 1235 } 1236 #endif 1237 return (0); 1238 } 1239 1240 static int 1241 rsu_fw_cmd(struct rsu_softc *sc, uint8_t code, void *buf, int len) 1242 { 1243 const uint8_t which = RSU_H2C_ENDPOINT; 1244 struct rsu_data *data; 1245 struct r92s_tx_desc *txd; 1246 struct r92s_fw_cmd_hdr *cmd; 1247 int cmdsz; 1248 int xferlen; 1249 1250 RSU_ASSERT_LOCKED(sc); 1251 1252 data = rsu_getbuf(sc); 1253 if (data == NULL) 1254 return (ENOMEM); 1255 1256 /* Blank the entire payload, just to be safe */ 1257 memset(data->buf, '\0', RSU_TXBUFSZ); 1258 1259 /* Round-up command length to a multiple of 8 bytes. */ 1260 /* XXX TODO: is this required? */ 1261 cmdsz = (len + 7) & ~7; 1262 1263 xferlen = sizeof(*txd) + sizeof(*cmd) + cmdsz; 1264 KASSERT(xferlen <= RSU_TXBUFSZ, ("%s: invalid length", __func__)); 1265 memset(data->buf, 0, xferlen); 1266 1267 /* Setup Tx descriptor. */ 1268 txd = (struct r92s_tx_desc *)data->buf; 1269 txd->txdw0 = htole32( 1270 SM(R92S_TXDW0_OFFSET, sizeof(*txd)) | 1271 SM(R92S_TXDW0_PKTLEN, sizeof(*cmd) + cmdsz) | 1272 R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG); 1273 txd->txdw1 = htole32(SM(R92S_TXDW1_QSEL, R92S_TXDW1_QSEL_H2C)); 1274 1275 /* Setup command header. */ 1276 cmd = (struct r92s_fw_cmd_hdr *)&txd[1]; 1277 cmd->len = htole16(cmdsz); 1278 cmd->code = code; 1279 cmd->seq = sc->cmd_seq; 1280 sc->cmd_seq = (sc->cmd_seq + 1) & 0x7f; 1281 1282 /* Copy command payload. */ 1283 memcpy(&cmd[1], buf, len); 1284 1285 RSU_DPRINTF(sc, RSU_DEBUG_TX | RSU_DEBUG_FWCMD, 1286 "%s: Tx cmd code=0x%x len=0x%x\n", 1287 __func__, code, cmdsz); 1288 data->buflen = xferlen; 1289 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next); 1290 usbd_transfer_start(sc->sc_xfer[which]); 1291 1292 return (0); 1293 } 1294 1295 /* ARGSUSED */ 1296 static void 1297 rsu_calib_task(void *arg, int pending __unused) 1298 { 1299 struct rsu_softc *sc = arg; 1300 #ifdef notyet 1301 uint32_t reg; 1302 #endif 1303 1304 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "%s: running calibration task\n", 1305 __func__); 1306 1307 RSU_LOCK(sc); 1308 #ifdef notyet 1309 /* Read WPS PBC status. */ 1310 rsu_write_1(sc, R92S_MAC_PINMUX_CTRL, 1311 R92S_GPIOMUX_EN | SM(R92S_GPIOSEL_GPIO, R92S_GPIOSEL_GPIO_JTAG)); 1312 rsu_write_1(sc, R92S_GPIO_IO_SEL, 1313 rsu_read_1(sc, R92S_GPIO_IO_SEL) & ~R92S_GPIO_WPS); 1314 reg = rsu_read_1(sc, R92S_GPIO_CTRL); 1315 if (reg != 0xff && (reg & R92S_GPIO_WPS)) 1316 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "WPS PBC is pushed\n"); 1317 #endif 1318 /* Read current signal level. */ 1319 if (rsu_fw_iocmd(sc, 0xf4000001) == 0) { 1320 sc->sc_currssi = rsu_read_4(sc, R92S_IOCMD_DATA); 1321 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "%s: RSSI=%d (%d)\n", 1322 __func__, sc->sc_currssi, 1323 rsu_hwrssi_to_rssi(sc, sc->sc_currssi)); 1324 } 1325 if (sc->sc_calibrating) 1326 taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task, hz); 1327 RSU_UNLOCK(sc); 1328 } 1329 1330 static void 1331 rsu_tx_task(void *arg, int pending __unused) 1332 { 1333 struct rsu_softc *sc = arg; 1334 1335 RSU_LOCK(sc); 1336 _rsu_start(sc); 1337 RSU_UNLOCK(sc); 1338 } 1339 1340 #define RSU_PWR_UNKNOWN 0x0 1341 #define RSU_PWR_ACTIVE 0x1 1342 #define RSU_PWR_OFF 0x2 1343 #define RSU_PWR_SLEEP 0x3 1344 1345 /* 1346 * Set the current power state. 1347 * 1348 * The rtlwifi code doesn't do this so aggressively; it 1349 * waits for an idle period after association with 1350 * no traffic before doing this. 1351 * 1352 * For now - it's on in all states except RUN, and 1353 * in RUN it'll transition to allow sleep. 1354 */ 1355 1356 struct r92s_pwr_cmd { 1357 uint8_t mode; 1358 uint8_t smart_ps; 1359 uint8_t bcn_pass_time; 1360 }; 1361 1362 static int 1363 rsu_set_fw_power_state(struct rsu_softc *sc, int state) 1364 { 1365 struct r92s_set_pwr_mode cmd; 1366 //struct r92s_pwr_cmd cmd; 1367 int error; 1368 1369 RSU_ASSERT_LOCKED(sc); 1370 1371 /* only change state if required */ 1372 if (sc->sc_curpwrstate == state) 1373 return (0); 1374 1375 memset(&cmd, 0, sizeof(cmd)); 1376 1377 switch (state) { 1378 case RSU_PWR_ACTIVE: 1379 /* Force the hardware awake */ 1380 rsu_write_1(sc, R92S_USB_HRPWM, 1381 R92S_USB_HRPWM_PS_ST_ACTIVE | R92S_USB_HRPWM_PS_ALL_ON); 1382 cmd.mode = R92S_PS_MODE_ACTIVE; 1383 break; 1384 case RSU_PWR_SLEEP: 1385 cmd.mode = R92S_PS_MODE_DTIM; /* XXX configurable? */ 1386 cmd.smart_ps = 1; /* XXX 2 if doing p2p */ 1387 cmd.bcn_pass_time = 5; /* in 100mS usb.c, linux/rtlwifi */ 1388 break; 1389 case RSU_PWR_OFF: 1390 cmd.mode = R92S_PS_MODE_RADIOOFF; 1391 break; 1392 default: 1393 device_printf(sc->sc_dev, "%s: unknown ps mode (%d)\n", 1394 __func__, 1395 state); 1396 return (ENXIO); 1397 } 1398 1399 RSU_DPRINTF(sc, RSU_DEBUG_RESET, 1400 "%s: setting ps mode to %d (mode %d)\n", 1401 __func__, state, cmd.mode); 1402 error = rsu_fw_cmd(sc, R92S_CMD_SET_PWR_MODE, &cmd, sizeof(cmd)); 1403 if (error == 0) 1404 sc->sc_curpwrstate = state; 1405 1406 return (error); 1407 } 1408 1409 static void 1410 rsu_set_led(struct rsu_softc *sc, int on) 1411 { 1412 rsu_write_1(sc, R92S_LEDCFG, 1413 (rsu_read_1(sc, R92S_LEDCFG) & 0xf0) | (!on << 3)); 1414 } 1415 1416 static int 1417 rsu_monitor_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, 1418 int arg) 1419 { 1420 struct ieee80211com *ic = vap->iv_ic; 1421 struct rsu_softc *sc = ic->ic_softc; 1422 struct rsu_vap *uvp = RSU_VAP(vap); 1423 1424 if (vap->iv_state != nstate) { 1425 IEEE80211_UNLOCK(ic); 1426 RSU_LOCK(sc); 1427 1428 switch (nstate) { 1429 case IEEE80211_S_INIT: 1430 sc->sc_vap_is_running = 0; 1431 rsu_set_led(sc, 0); 1432 break; 1433 case IEEE80211_S_RUN: 1434 sc->sc_vap_is_running = 1; 1435 rsu_set_led(sc, 1); 1436 break; 1437 default: 1438 /* NOTREACHED */ 1439 break; 1440 } 1441 rsu_rxfilter_refresh(sc); 1442 1443 RSU_UNLOCK(sc); 1444 IEEE80211_LOCK(ic); 1445 } 1446 1447 return (uvp->newstate(vap, nstate, arg)); 1448 } 1449 1450 static int 1451 rsu_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1452 { 1453 struct rsu_vap *uvp = RSU_VAP(vap); 1454 struct ieee80211com *ic = vap->iv_ic; 1455 struct rsu_softc *sc = ic->ic_softc; 1456 struct ieee80211_node *ni; 1457 struct ieee80211_rateset *rs; 1458 enum ieee80211_state ostate; 1459 int error, startcal = 0; 1460 1461 ostate = vap->iv_state; 1462 RSU_DPRINTF(sc, RSU_DEBUG_STATE, "%s: %s -> %s\n", 1463 __func__, 1464 ieee80211_state_name[ostate], 1465 ieee80211_state_name[nstate]); 1466 1467 IEEE80211_UNLOCK(ic); 1468 if (ostate == IEEE80211_S_RUN) { 1469 RSU_LOCK(sc); 1470 /* Stop calibration. */ 1471 sc->sc_calibrating = 0; 1472 1473 /* Pause Tx for AC queues. */ 1474 rsu_write_1(sc, R92S_TXPAUSE, R92S_TXPAUSE_AC); 1475 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10)); 1476 1477 RSU_UNLOCK(sc); 1478 taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task); 1479 taskqueue_drain(taskqueue_thread, &sc->tx_task); 1480 RSU_LOCK(sc); 1481 /* Disassociate from our current BSS. */ 1482 rsu_disconnect(sc); 1483 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10)); 1484 1485 /* Refresh Rx filter (may be modified by firmware). */ 1486 sc->sc_vap_is_running = 0; 1487 rsu_rxfilter_refresh(sc); 1488 1489 /* Reinstall static keys. */ 1490 if (sc->sc_running) 1491 rsu_reinit_static_keys(sc); 1492 } else 1493 RSU_LOCK(sc); 1494 switch (nstate) { 1495 case IEEE80211_S_INIT: 1496 (void) rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE); 1497 break; 1498 case IEEE80211_S_AUTH: 1499 ni = ieee80211_ref_node(vap->iv_bss); 1500 (void) rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE); 1501 error = rsu_join_bss(sc, ni); 1502 ieee80211_free_node(ni); 1503 if (error != 0) { 1504 device_printf(sc->sc_dev, 1505 "could not send join command\n"); 1506 } 1507 break; 1508 case IEEE80211_S_RUN: 1509 /* Flush all AC queues. */ 1510 rsu_write_1(sc, R92S_TXPAUSE, 0); 1511 1512 ni = ieee80211_ref_node(vap->iv_bss); 1513 rs = &ni->ni_rates; 1514 /* Indicate highest supported rate. */ 1515 ni->ni_txrate = rs->rs_rates[rs->rs_nrates - 1]; 1516 (void) rsu_set_fw_power_state(sc, RSU_PWR_SLEEP); 1517 ieee80211_free_node(ni); 1518 startcal = 1; 1519 break; 1520 default: 1521 break; 1522 } 1523 if (startcal != 0) { 1524 sc->sc_calibrating = 1; 1525 /* Start periodic calibration. */ 1526 taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task, 1527 hz); 1528 } 1529 RSU_UNLOCK(sc); 1530 IEEE80211_LOCK(ic); 1531 return (uvp->newstate(vap, nstate, arg)); 1532 } 1533 1534 static int 1535 rsu_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k, 1536 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 1537 { 1538 struct rsu_softc *sc = vap->iv_ic->ic_softc; 1539 int is_checked = 0; 1540 1541 if (&vap->iv_nw_keys[0] <= k && 1542 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) { 1543 *keyix = ieee80211_crypto_get_key_wepidx(vap, k); 1544 } else { 1545 if (vap->iv_opmode != IEEE80211_M_STA) { 1546 *keyix = 0; 1547 /* TODO: obtain keyix from node id */ 1548 is_checked = 1; 1549 k->wk_flags |= IEEE80211_KEY_SWCRYPT; 1550 } else 1551 *keyix = R92S_MACID_BSS; 1552 } 1553 1554 if (!is_checked) { 1555 RSU_LOCK(sc); 1556 if (isset(sc->keys_bmap, *keyix)) { 1557 device_printf(sc->sc_dev, 1558 "%s: key slot %d is already used!\n", 1559 __func__, *keyix); 1560 RSU_UNLOCK(sc); 1561 return (0); 1562 } 1563 setbit(sc->keys_bmap, *keyix); 1564 RSU_UNLOCK(sc); 1565 } 1566 1567 *rxkeyix = *keyix; 1568 1569 return (1); 1570 } 1571 1572 static int 1573 rsu_process_key(struct ieee80211vap *vap, const struct ieee80211_key *k, 1574 int set) 1575 { 1576 struct rsu_softc *sc = vap->iv_ic->ic_softc; 1577 int ret; 1578 1579 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) { 1580 /* Not for us. */ 1581 return (1); 1582 } 1583 1584 /* Handle group keys. */ 1585 if (&vap->iv_nw_keys[0] <= k && 1586 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) { 1587 KASSERT(k->wk_keyix < nitems(sc->group_keys), 1588 ("keyix %u > %zu\n", k->wk_keyix, nitems(sc->group_keys))); 1589 1590 RSU_LOCK(sc); 1591 sc->group_keys[k->wk_keyix] = (set ? k : NULL); 1592 if (!sc->sc_running) { 1593 /* Static keys will be set during device startup. */ 1594 RSU_UNLOCK(sc); 1595 return (1); 1596 } 1597 1598 if (set) 1599 ret = rsu_set_key_group(sc, k); 1600 else 1601 ret = rsu_delete_key(sc, k->wk_keyix); 1602 RSU_UNLOCK(sc); 1603 1604 return (!ret); 1605 } 1606 1607 if (set) { 1608 /* wait for pending key removal */ 1609 taskqueue_drain(taskqueue_thread, &sc->del_key_task); 1610 1611 RSU_LOCK(sc); 1612 ret = rsu_set_key_pair(sc, k); 1613 RSU_UNLOCK(sc); 1614 } else { 1615 RSU_DELKEY_BMAP_LOCK(sc); 1616 setbit(sc->free_keys_bmap, k->wk_keyix); 1617 RSU_DELKEY_BMAP_UNLOCK(sc); 1618 1619 /* workaround ieee80211_node_delucastkey() locking */ 1620 taskqueue_enqueue(taskqueue_thread, &sc->del_key_task); 1621 ret = 0; /* fake success */ 1622 } 1623 1624 return (!ret); 1625 } 1626 1627 static int 1628 rsu_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k) 1629 { 1630 return (rsu_process_key(vap, k, 1)); 1631 } 1632 1633 static int 1634 rsu_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k) 1635 { 1636 return (rsu_process_key(vap, k, 0)); 1637 } 1638 1639 static int 1640 rsu_cam_read(struct rsu_softc *sc, uint8_t addr, uint32_t *val) 1641 { 1642 int ntries; 1643 1644 rsu_write_4(sc, R92S_CAMCMD, 1645 R92S_CAMCMD_POLLING | SM(R92S_CAMCMD_ADDR, addr)); 1646 for (ntries = 0; ntries < 10; ntries++) { 1647 if (!(rsu_read_4(sc, R92S_CAMCMD) & R92S_CAMCMD_POLLING)) 1648 break; 1649 1650 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(1)); 1651 } 1652 if (ntries == 10) { 1653 device_printf(sc->sc_dev, 1654 "%s: cannot read CAM entry at address %02X\n", 1655 __func__, addr); 1656 return (ETIMEDOUT); 1657 } 1658 1659 *val = rsu_read_4(sc, R92S_CAMREAD); 1660 1661 return (0); 1662 } 1663 1664 static void 1665 rsu_cam_write(struct rsu_softc *sc, uint8_t addr, uint32_t data) 1666 { 1667 1668 rsu_write_4(sc, R92S_CAMWRITE, data); 1669 rsu_write_4(sc, R92S_CAMCMD, 1670 R92S_CAMCMD_POLLING | R92S_CAMCMD_WRITE | 1671 SM(R92S_CAMCMD_ADDR, addr)); 1672 } 1673 1674 static int 1675 rsu_key_check(struct rsu_softc *sc, ieee80211_keyix keyix, int is_valid) 1676 { 1677 uint32_t val; 1678 int error, ntries; 1679 1680 for (ntries = 0; ntries < 20; ntries++) { 1681 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(1)); 1682 1683 error = rsu_cam_read(sc, R92S_CAM_CTL0(keyix), &val); 1684 if (error != 0) { 1685 device_printf(sc->sc_dev, 1686 "%s: cannot check key status!\n", __func__); 1687 return (error); 1688 } 1689 if (((val & R92S_CAM_VALID) == 0) ^ is_valid) 1690 break; 1691 } 1692 if (ntries == 20) { 1693 device_printf(sc->sc_dev, 1694 "%s: key %d is %s marked as valid, rejecting request\n", 1695 __func__, keyix, is_valid ? "not" : "still"); 1696 return (EIO); 1697 } 1698 1699 return (0); 1700 } 1701 1702 /* 1703 * Map net80211 cipher to RTL8712 security mode. 1704 */ 1705 static uint8_t 1706 rsu_crypto_mode(struct rsu_softc *sc, u_int cipher, int keylen) 1707 { 1708 switch (cipher) { 1709 case IEEE80211_CIPHER_WEP: 1710 return keylen < 8 ? R92S_KEY_ALGO_WEP40 : R92S_KEY_ALGO_WEP104; 1711 case IEEE80211_CIPHER_TKIP: 1712 return R92S_KEY_ALGO_TKIP; 1713 case IEEE80211_CIPHER_AES_CCM: 1714 return R92S_KEY_ALGO_AES; 1715 default: 1716 device_printf(sc->sc_dev, "unknown cipher %d\n", cipher); 1717 return R92S_KEY_ALGO_INVALID; 1718 } 1719 } 1720 1721 static int 1722 rsu_set_key_group(struct rsu_softc *sc, const struct ieee80211_key *k) 1723 { 1724 struct r92s_fw_cmd_set_key key; 1725 uint8_t algo; 1726 int error; 1727 1728 RSU_ASSERT_LOCKED(sc); 1729 1730 /* Map net80211 cipher to HW crypto algorithm. */ 1731 algo = rsu_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen); 1732 if (algo == R92S_KEY_ALGO_INVALID) 1733 return (EINVAL); 1734 1735 memset(&key, 0, sizeof(key)); 1736 key.algo = algo; 1737 key.cam_id = k->wk_keyix; 1738 key.grpkey = (k->wk_flags & IEEE80211_KEY_GROUP) != 0; 1739 memcpy(key.key, k->wk_key, MIN(k->wk_keylen, sizeof(key.key))); 1740 1741 RSU_DPRINTF(sc, RSU_DEBUG_KEY | RSU_DEBUG_FWCMD, 1742 "%s: keyix %u, group %u, algo %u/%u, flags %04X, len %u, " 1743 "macaddr %s\n", __func__, key.cam_id, key.grpkey, 1744 k->wk_cipher->ic_cipher, key.algo, k->wk_flags, k->wk_keylen, 1745 ether_sprintf(k->wk_macaddr)); 1746 1747 error = rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key)); 1748 if (error != 0) { 1749 device_printf(sc->sc_dev, 1750 "%s: cannot send firmware command, error %d\n", 1751 __func__, error); 1752 return (error); 1753 } 1754 1755 return (rsu_key_check(sc, k->wk_keyix, 1)); 1756 } 1757 1758 static int 1759 rsu_set_key_pair(struct rsu_softc *sc, const struct ieee80211_key *k) 1760 { 1761 struct r92s_fw_cmd_set_key_mac key; 1762 uint8_t algo; 1763 int error; 1764 1765 RSU_ASSERT_LOCKED(sc); 1766 1767 if (!sc->sc_running) 1768 return (ESHUTDOWN); 1769 1770 /* Map net80211 cipher to HW crypto algorithm. */ 1771 algo = rsu_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen); 1772 if (algo == R92S_KEY_ALGO_INVALID) 1773 return (EINVAL); 1774 1775 memset(&key, 0, sizeof(key)); 1776 key.algo = algo; 1777 memcpy(key.macaddr, k->wk_macaddr, sizeof(key.macaddr)); 1778 memcpy(key.key, k->wk_key, MIN(k->wk_keylen, sizeof(key.key))); 1779 1780 RSU_DPRINTF(sc, RSU_DEBUG_KEY | RSU_DEBUG_FWCMD, 1781 "%s: keyix %u, algo %u/%u, flags %04X, len %u, macaddr %s\n", 1782 __func__, k->wk_keyix, k->wk_cipher->ic_cipher, key.algo, 1783 k->wk_flags, k->wk_keylen, ether_sprintf(key.macaddr)); 1784 1785 error = rsu_fw_cmd(sc, R92S_CMD_SET_STA_KEY, &key, sizeof(key)); 1786 if (error != 0) { 1787 device_printf(sc->sc_dev, 1788 "%s: cannot send firmware command, error %d\n", 1789 __func__, error); 1790 return (error); 1791 } 1792 1793 return (rsu_key_check(sc, k->wk_keyix, 1)); 1794 } 1795 1796 static int 1797 rsu_reinit_static_keys(struct rsu_softc *sc) 1798 { 1799 int i, error; 1800 1801 for (i = 0; i < nitems(sc->group_keys); i++) { 1802 if (sc->group_keys[i] != NULL) { 1803 error = rsu_set_key_group(sc, sc->group_keys[i]); 1804 if (error != 0) { 1805 device_printf(sc->sc_dev, 1806 "%s: failed to set static key %d, " 1807 "error %d\n", __func__, i, error); 1808 return (error); 1809 } 1810 } 1811 } 1812 1813 return (0); 1814 } 1815 1816 static int 1817 rsu_delete_key(struct rsu_softc *sc, ieee80211_keyix keyix) 1818 { 1819 struct r92s_fw_cmd_set_key key; 1820 uint32_t val; 1821 int error; 1822 1823 RSU_ASSERT_LOCKED(sc); 1824 1825 if (!sc->sc_running) 1826 return (0); 1827 1828 /* check if it was automatically removed by firmware */ 1829 error = rsu_cam_read(sc, R92S_CAM_CTL0(keyix), &val); 1830 if (error == 0 && (val & R92S_CAM_VALID) == 0) { 1831 RSU_DPRINTF(sc, RSU_DEBUG_KEY, 1832 "%s: key %u does not exist\n", __func__, keyix); 1833 clrbit(sc->keys_bmap, keyix); 1834 return (0); 1835 } 1836 1837 memset(&key, 0, sizeof(key)); 1838 key.cam_id = keyix; 1839 1840 RSU_DPRINTF(sc, RSU_DEBUG_KEY | RSU_DEBUG_FWCMD, 1841 "%s: removing key %u\n", __func__, key.cam_id); 1842 1843 error = rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key)); 1844 if (error != 0) { 1845 device_printf(sc->sc_dev, 1846 "%s: cannot send firmware command, error %d\n", 1847 __func__, error); 1848 goto finish; 1849 } 1850 1851 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(5)); 1852 1853 /* 1854 * Clear 'valid' bit manually (cannot be done via firmware command). 1855 * Used for key check + when firmware command cannot be sent. 1856 */ 1857 finish: 1858 rsu_cam_write(sc, R92S_CAM_CTL0(keyix), 0); 1859 1860 clrbit(sc->keys_bmap, keyix); 1861 1862 return (rsu_key_check(sc, keyix, 0)); 1863 } 1864 1865 static void 1866 rsu_delete_key_pair_cb(void *arg, int pending __unused) 1867 { 1868 struct rsu_softc *sc = arg; 1869 int i; 1870 1871 RSU_DELKEY_BMAP_LOCK(sc); 1872 for (i = IEEE80211_WEP_NKID; i < R92S_CAM_ENTRY_LIMIT; i++) { 1873 if (isset(sc->free_keys_bmap, i)) { 1874 RSU_DELKEY_BMAP_UNLOCK(sc); 1875 1876 RSU_LOCK(sc); 1877 RSU_DPRINTF(sc, RSU_DEBUG_KEY, 1878 "%s: calling rsu_delete_key() with keyix = %d\n", 1879 __func__, i); 1880 (void) rsu_delete_key(sc, i); 1881 RSU_UNLOCK(sc); 1882 1883 RSU_DELKEY_BMAP_LOCK(sc); 1884 clrbit(sc->free_keys_bmap, i); 1885 1886 /* bmap can be changed */ 1887 i = IEEE80211_WEP_NKID - 1; 1888 continue; 1889 } 1890 } 1891 RSU_DELKEY_BMAP_UNLOCK(sc); 1892 } 1893 1894 static int 1895 rsu_site_survey(struct rsu_softc *sc, struct ieee80211_scan_ssid *ssid) 1896 { 1897 struct r92s_fw_cmd_sitesurvey cmd; 1898 1899 RSU_ASSERT_LOCKED(sc); 1900 1901 memset(&cmd, 0, sizeof(cmd)); 1902 /* TODO: passive channels? */ 1903 if (sc->sc_active_scan) 1904 cmd.active = htole32(1); 1905 cmd.limit = htole32(48); 1906 1907 if (ssid != NULL) { 1908 sc->sc_extra_scan = 1; 1909 cmd.ssidlen = htole32(ssid->len); 1910 memcpy(cmd.ssid, ssid->ssid, ssid->len); 1911 } 1912 #ifdef USB_DEBUG 1913 if (rsu_debug & (RSU_DEBUG_SCAN | RSU_DEBUG_FWCMD)) { 1914 device_printf(sc->sc_dev, 1915 "sending site survey command, active %d", 1916 le32toh(cmd.active)); 1917 if (ssid != NULL) { 1918 printf(", ssid: "); 1919 ieee80211_print_essid(cmd.ssid, le32toh(cmd.ssidlen)); 1920 } 1921 printf("\n"); 1922 } 1923 #endif 1924 return (rsu_fw_cmd(sc, R92S_CMD_SITE_SURVEY, &cmd, sizeof(cmd))); 1925 } 1926 1927 static int 1928 rsu_join_bss(struct rsu_softc *sc, struct ieee80211_node *ni) 1929 { 1930 struct ieee80211com *ic = &sc->sc_ic; 1931 struct ieee80211vap *vap = ni->ni_vap; 1932 struct ndis_wlan_bssid_ex *bss; 1933 struct ndis_802_11_fixed_ies *fixed; 1934 struct r92s_fw_cmd_auth auth; 1935 uint8_t buf[sizeof(*bss) + 128] __aligned(4); 1936 uint8_t *frm; 1937 uint8_t opmode; 1938 int error; 1939 1940 RSU_ASSERT_LOCKED(sc); 1941 1942 /* Let the FW decide the opmode based on the capinfo field. */ 1943 opmode = NDIS802_11AUTOUNKNOWN; 1944 RSU_DPRINTF(sc, RSU_DEBUG_RESET, 1945 "%s: setting operating mode to %d\n", 1946 __func__, opmode); 1947 error = rsu_fw_cmd(sc, R92S_CMD_SET_OPMODE, &opmode, sizeof(opmode)); 1948 if (error != 0) 1949 return (error); 1950 1951 memset(&auth, 0, sizeof(auth)); 1952 if (vap->iv_flags & IEEE80211_F_WPA) { 1953 auth.mode = R92S_AUTHMODE_WPA; 1954 auth.dot1x = (ni->ni_authmode == IEEE80211_AUTH_8021X); 1955 } else 1956 auth.mode = R92S_AUTHMODE_OPEN; 1957 RSU_DPRINTF(sc, RSU_DEBUG_RESET, 1958 "%s: setting auth mode to %d\n", 1959 __func__, auth.mode); 1960 error = rsu_fw_cmd(sc, R92S_CMD_SET_AUTH, &auth, sizeof(auth)); 1961 if (error != 0) 1962 return (error); 1963 1964 memset(buf, 0, sizeof(buf)); 1965 bss = (struct ndis_wlan_bssid_ex *)buf; 1966 IEEE80211_ADDR_COPY(bss->macaddr, ni->ni_bssid); 1967 bss->ssid.ssidlen = htole32(ni->ni_esslen); 1968 memcpy(bss->ssid.ssid, ni->ni_essid, ni->ni_esslen); 1969 if (vap->iv_flags & (IEEE80211_F_PRIVACY | IEEE80211_F_WPA)) 1970 bss->privacy = htole32(1); 1971 bss->rssi = htole32(ni->ni_avgrssi); 1972 if (ic->ic_curmode == IEEE80211_MODE_11B) 1973 bss->networktype = htole32(NDIS802_11DS); 1974 else 1975 bss->networktype = htole32(NDIS802_11OFDM24); 1976 bss->config.len = htole32(sizeof(bss->config)); 1977 bss->config.bintval = htole32(ni->ni_intval); 1978 bss->config.dsconfig = htole32(ieee80211_chan2ieee(ic, ni->ni_chan)); 1979 bss->inframode = htole32(NDIS802_11INFRASTRUCTURE); 1980 /* XXX verify how this is supposed to look! */ 1981 memcpy(bss->supprates, ni->ni_rates.rs_rates, 1982 ni->ni_rates.rs_nrates); 1983 /* Write the fixed fields of the beacon frame. */ 1984 fixed = (struct ndis_802_11_fixed_ies *)&bss[1]; 1985 memcpy(&fixed->tstamp, ni->ni_tstamp.data, 8); 1986 fixed->bintval = htole16(ni->ni_intval); 1987 fixed->capabilities = htole16(ni->ni_capinfo); 1988 /* Write IEs to be included in the association request. */ 1989 frm = (uint8_t *)&fixed[1]; 1990 frm = ieee80211_add_rsn(frm, vap); 1991 frm = ieee80211_add_wpa(frm, vap); 1992 frm = ieee80211_add_qos(frm, ni); 1993 if ((ic->ic_flags & IEEE80211_F_WME) && 1994 (ni->ni_ies.wme_ie != NULL)) 1995 frm = ieee80211_add_wme_info(frm, &ic->ic_wme); 1996 if (ni->ni_flags & IEEE80211_NODE_HT) { 1997 frm = ieee80211_add_htcap(frm, ni); 1998 frm = ieee80211_add_htinfo(frm, ni); 1999 } 2000 bss->ieslen = htole32(frm - (uint8_t *)fixed); 2001 bss->len = htole32(((frm - buf) + 3) & ~3); 2002 RSU_DPRINTF(sc, RSU_DEBUG_RESET | RSU_DEBUG_FWCMD, 2003 "%s: sending join bss command to %s chan %d\n", 2004 __func__, 2005 ether_sprintf(bss->macaddr), le32toh(bss->config.dsconfig)); 2006 return (rsu_fw_cmd(sc, R92S_CMD_JOIN_BSS, buf, sizeof(buf))); 2007 } 2008 2009 static int 2010 rsu_disconnect(struct rsu_softc *sc) 2011 { 2012 uint32_t zero = 0; /* :-) */ 2013 2014 /* Disassociate from our current BSS. */ 2015 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD, 2016 "%s: sending disconnect command\n", __func__); 2017 return (rsu_fw_cmd(sc, R92S_CMD_DISCONNECT, &zero, sizeof(zero))); 2018 } 2019 2020 /* 2021 * Map the hardware provided RSSI value to a signal level. 2022 * For the most part it's just something we divide by and cap 2023 * so it doesn't overflow the representation by net80211. 2024 */ 2025 static int 2026 rsu_hwrssi_to_rssi(struct rsu_softc *sc, int hw_rssi) 2027 { 2028 int v; 2029 2030 if (hw_rssi == 0) 2031 return (0); 2032 v = hw_rssi >> 4; 2033 if (v > 80) 2034 v = 80; 2035 return (v); 2036 } 2037 2038 CTASSERT(MCLBYTES > sizeof(struct ieee80211_frame)); 2039 2040 static void 2041 rsu_event_survey(struct rsu_softc *sc, uint8_t *buf, int len) 2042 { 2043 struct ieee80211com *ic = &sc->sc_ic; 2044 struct ieee80211_frame *wh; 2045 struct ndis_wlan_bssid_ex *bss; 2046 struct ieee80211_rx_stats rxs; 2047 struct mbuf *m; 2048 uint32_t ieslen; 2049 uint32_t pktlen; 2050 2051 if (__predict_false(len < sizeof(*bss))) 2052 return; 2053 bss = (struct ndis_wlan_bssid_ex *)buf; 2054 ieslen = le32toh(bss->ieslen); 2055 /* range check length of information element */ 2056 if (__predict_false(ieslen > (uint32_t)(len - sizeof(*bss)))) 2057 return; 2058 2059 RSU_DPRINTF(sc, RSU_DEBUG_SCAN, 2060 "%s: found BSS %s: len=%d chan=%d inframode=%d " 2061 "networktype=%d privacy=%d, RSSI=%d\n", 2062 __func__, 2063 ether_sprintf(bss->macaddr), ieslen, 2064 le32toh(bss->config.dsconfig), le32toh(bss->inframode), 2065 le32toh(bss->networktype), le32toh(bss->privacy), 2066 le32toh(bss->rssi)); 2067 2068 /* Build a fake beacon frame to let net80211 do all the parsing. */ 2069 /* XXX TODO: just call the new scan API methods! */ 2070 if (__predict_false(ieslen > (size_t)(MCLBYTES - sizeof(*wh)))) 2071 return; 2072 pktlen = sizeof(*wh) + ieslen; 2073 m = m_get2(pktlen, M_NOWAIT, MT_DATA, M_PKTHDR); 2074 if (__predict_false(m == NULL)) 2075 return; 2076 wh = mtod(m, struct ieee80211_frame *); 2077 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 2078 IEEE80211_FC0_SUBTYPE_BEACON; 2079 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2080 USETW(wh->i_dur, 0); 2081 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr); 2082 IEEE80211_ADDR_COPY(wh->i_addr2, bss->macaddr); 2083 IEEE80211_ADDR_COPY(wh->i_addr3, bss->macaddr); 2084 *(uint16_t *)wh->i_seq = 0; 2085 memcpy(&wh[1], (uint8_t *)&bss[1], ieslen); 2086 2087 /* Finalize mbuf. */ 2088 m->m_pkthdr.len = m->m_len = pktlen; 2089 2090 /* Set channel flags for input path */ 2091 bzero(&rxs, sizeof(rxs)); 2092 rxs.r_flags |= IEEE80211_R_IEEE | IEEE80211_R_FREQ; 2093 rxs.r_flags |= IEEE80211_R_NF | IEEE80211_R_RSSI; 2094 rxs.c_ieee = le32toh(bss->config.dsconfig); 2095 rxs.c_freq = ieee80211_ieee2mhz(rxs.c_ieee, IEEE80211_CHAN_2GHZ); 2096 /* This is a number from 0..100; so let's just divide it down a bit */ 2097 rxs.c_rssi = le32toh(bss->rssi) / 2; 2098 rxs.c_nf = -96; 2099 if (ieee80211_add_rx_params(m, &rxs) == 0) 2100 return; 2101 2102 /* XXX avoid a LOR */ 2103 RSU_UNLOCK(sc); 2104 ieee80211_input_mimo_all(ic, m); 2105 RSU_LOCK(sc); 2106 } 2107 2108 static void 2109 rsu_event_join_bss(struct rsu_softc *sc, uint8_t *buf, int len) 2110 { 2111 struct ieee80211com *ic = &sc->sc_ic; 2112 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2113 struct ieee80211_node *ni = vap->iv_bss; 2114 struct r92s_event_join_bss *rsp; 2115 uint32_t tmp; 2116 int res; 2117 2118 if (__predict_false(len < sizeof(*rsp))) 2119 return; 2120 rsp = (struct r92s_event_join_bss *)buf; 2121 res = (int)le32toh(rsp->join_res); 2122 2123 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD, 2124 "%s: Rx join BSS event len=%d res=%d\n", 2125 __func__, len, res); 2126 2127 /* 2128 * XXX Don't do this; there's likely a better way to tell 2129 * the caller we failed. 2130 */ 2131 if (res <= 0) { 2132 RSU_UNLOCK(sc); 2133 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1); 2134 RSU_LOCK(sc); 2135 return; 2136 } 2137 2138 tmp = le32toh(rsp->associd); 2139 if (tmp >= vap->iv_max_aid) { 2140 RSU_DPRINTF(sc, RSU_DEBUG_ANY, "Assoc ID overflow\n"); 2141 tmp = 1; 2142 } 2143 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD, 2144 "%s: associated with %s associd=%d\n", 2145 __func__, ether_sprintf(rsp->bss.macaddr), tmp); 2146 /* XXX is this required? What's the top two bits for again? */ 2147 ni->ni_associd = tmp | 0xc000; 2148 2149 /* Refresh Rx filter (was changed by firmware). */ 2150 sc->sc_vap_is_running = 1; 2151 rsu_rxfilter_refresh(sc); 2152 2153 RSU_UNLOCK(sc); 2154 ieee80211_new_state(vap, IEEE80211_S_RUN, 2155 IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 2156 RSU_LOCK(sc); 2157 } 2158 2159 static void 2160 rsu_event_addba_req_report(struct rsu_softc *sc, uint8_t *buf, int len) 2161 { 2162 struct ieee80211com *ic = &sc->sc_ic; 2163 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2164 struct r92s_add_ba_event *ba = (void *) buf; 2165 struct ieee80211_node *ni; 2166 2167 if (len < sizeof(*ba)) { 2168 device_printf(sc->sc_dev, "%s: short read (%d)\n", __func__, len); 2169 return; 2170 } 2171 2172 if (vap == NULL) 2173 return; 2174 2175 RSU_DPRINTF(sc, RSU_DEBUG_AMPDU, "%s: mac=%s, tid=%d, ssn=%d\n", 2176 __func__, 2177 ether_sprintf(ba->mac_addr), 2178 (int) ba->tid, 2179 (int) le16toh(ba->ssn)); 2180 2181 /* XXX do node lookup; this is STA specific */ 2182 2183 ni = ieee80211_ref_node(vap->iv_bss); 2184 ieee80211_ampdu_rx_start_ext(ni, ba->tid, le16toh(ba->ssn) >> 4, 32); 2185 ieee80211_free_node(ni); 2186 } 2187 2188 static void 2189 rsu_rx_event(struct rsu_softc *sc, uint8_t code, uint8_t *buf, int len) 2190 { 2191 struct ieee80211com *ic = &sc->sc_ic; 2192 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2193 2194 RSU_DPRINTF(sc, RSU_DEBUG_RX | RSU_DEBUG_FWCMD, 2195 "%s: Rx event code=%d len=%d\n", __func__, code, len); 2196 switch (code) { 2197 case R92S_EVT_SURVEY: 2198 rsu_event_survey(sc, buf, len); 2199 break; 2200 case R92S_EVT_SURVEY_DONE: 2201 RSU_DPRINTF(sc, RSU_DEBUG_SCAN, 2202 "%s: %s scan done, found %d BSS\n", 2203 __func__, sc->sc_extra_scan ? "direct" : "broadcast", 2204 le32toh(*(uint32_t *)buf)); 2205 if (sc->sc_extra_scan == 1) { 2206 /* Send broadcast probe request. */ 2207 sc->sc_extra_scan = 0; 2208 if (vap != NULL && rsu_site_survey(sc, NULL) != 0) { 2209 RSU_UNLOCK(sc); 2210 ieee80211_cancel_scan(vap); 2211 RSU_LOCK(sc); 2212 } 2213 break; 2214 } 2215 if (vap != NULL) { 2216 RSU_UNLOCK(sc); 2217 ieee80211_scan_done(vap); 2218 RSU_LOCK(sc); 2219 } 2220 break; 2221 case R92S_EVT_JOIN_BSS: 2222 if (vap->iv_state == IEEE80211_S_AUTH) 2223 rsu_event_join_bss(sc, buf, len); 2224 break; 2225 case R92S_EVT_DEL_STA: 2226 RSU_DPRINTF(sc, RSU_DEBUG_FWCMD | RSU_DEBUG_STATE, 2227 "%s: disassociated from %s\n", __func__, 2228 ether_sprintf(buf)); 2229 if (vap->iv_state == IEEE80211_S_RUN && 2230 IEEE80211_ADDR_EQ(vap->iv_bss->ni_bssid, buf)) { 2231 RSU_UNLOCK(sc); 2232 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1); 2233 RSU_LOCK(sc); 2234 } 2235 break; 2236 case R92S_EVT_WPS_PBC: 2237 RSU_DPRINTF(sc, RSU_DEBUG_RX | RSU_DEBUG_FWCMD, 2238 "%s: WPS PBC pushed.\n", __func__); 2239 break; 2240 case R92S_EVT_FWDBG: 2241 buf[60] = '\0'; 2242 RSU_DPRINTF(sc, RSU_DEBUG_FWDBG, "FWDBG: %s\n", (char *)buf); 2243 break; 2244 case R92S_EVT_ADDBA_REQ_REPORT: 2245 rsu_event_addba_req_report(sc, buf, len); 2246 break; 2247 default: 2248 device_printf(sc->sc_dev, "%s: unhandled code (%d)\n", __func__, code); 2249 break; 2250 } 2251 } 2252 2253 static void 2254 rsu_rx_multi_event(struct rsu_softc *sc, uint8_t *buf, int len) 2255 { 2256 struct r92s_fw_cmd_hdr *cmd; 2257 int cmdsz; 2258 2259 RSU_DPRINTF(sc, RSU_DEBUG_RX, "%s: Rx events len=%d\n", __func__, len); 2260 2261 /* Skip Rx status. */ 2262 buf += sizeof(struct r92s_rx_stat); 2263 len -= sizeof(struct r92s_rx_stat); 2264 2265 /* Process all events. */ 2266 for (;;) { 2267 /* Check that command header fits. */ 2268 if (__predict_false(len < sizeof(*cmd))) 2269 break; 2270 cmd = (struct r92s_fw_cmd_hdr *)buf; 2271 /* Check that command payload fits. */ 2272 cmdsz = le16toh(cmd->len); 2273 if (__predict_false(len < sizeof(*cmd) + cmdsz)) 2274 break; 2275 2276 /* Process firmware event. */ 2277 rsu_rx_event(sc, cmd->code, (uint8_t *)&cmd[1], cmdsz); 2278 2279 if (!(cmd->seq & R92S_FW_CMD_MORE)) 2280 break; 2281 buf += sizeof(*cmd) + cmdsz; 2282 len -= sizeof(*cmd) + cmdsz; 2283 } 2284 } 2285 2286 static int8_t 2287 rsu_get_rssi(struct rsu_softc *sc, int rate, void *physt) 2288 { 2289 static const int8_t cckoff[] = { 14, -2, -20, -40 }; 2290 struct r92s_rx_phystat *phy; 2291 struct r92s_rx_cck *cck; 2292 uint8_t rpt; 2293 int8_t rssi; 2294 2295 if (rate <= 3) { 2296 cck = (struct r92s_rx_cck *)physt; 2297 rpt = (cck->agc_rpt >> 6) & 0x3; 2298 rssi = cck->agc_rpt & 0x3e; 2299 rssi = cckoff[rpt] - rssi; 2300 } else { /* OFDM/HT. */ 2301 phy = (struct r92s_rx_phystat *)physt; 2302 rssi = ((le32toh(phy->phydw1) >> 1) & 0x7f) - 106; 2303 } 2304 return (rssi); 2305 } 2306 2307 static struct mbuf * 2308 rsu_rx_copy_to_mbuf(struct rsu_softc *sc, struct r92s_rx_stat *stat, 2309 int totlen) 2310 { 2311 struct ieee80211com *ic = &sc->sc_ic; 2312 struct mbuf *m; 2313 uint32_t rxdw0; 2314 int pktlen; 2315 2316 rxdw0 = le32toh(stat->rxdw0); 2317 if (__predict_false(rxdw0 & (R92S_RXDW0_CRCERR | R92S_RXDW0_ICVERR))) { 2318 RSU_DPRINTF(sc, RSU_DEBUG_RX, 2319 "%s: RX flags error (%s)\n", __func__, 2320 rxdw0 & R92S_RXDW0_CRCERR ? "CRC" : "ICV"); 2321 goto fail; 2322 } 2323 2324 pktlen = MS(rxdw0, R92S_RXDW0_PKTLEN); 2325 if (__predict_false(pktlen < sizeof (struct ieee80211_frame_ack))) { 2326 RSU_DPRINTF(sc, RSU_DEBUG_RX, 2327 "%s: frame is too short: %d\n", __func__, pktlen); 2328 goto fail; 2329 } 2330 2331 m = m_get2(totlen, M_NOWAIT, MT_DATA, M_PKTHDR); 2332 if (__predict_false(m == NULL)) { 2333 device_printf(sc->sc_dev, 2334 "%s: could not allocate RX mbuf, totlen %d\n", 2335 __func__, totlen); 2336 goto fail; 2337 } 2338 2339 /* Finalize mbuf. */ 2340 memcpy(mtod(m, uint8_t *), (uint8_t *)stat, totlen); 2341 m->m_pkthdr.len = m->m_len = totlen; 2342 2343 return (m); 2344 fail: 2345 counter_u64_add(ic->ic_ierrors, 1); 2346 return (NULL); 2347 } 2348 2349 static uint32_t 2350 rsu_get_tsf_low(struct rsu_softc *sc) 2351 { 2352 return (rsu_read_4(sc, R92S_TSFTR)); 2353 } 2354 2355 static uint32_t 2356 rsu_get_tsf_high(struct rsu_softc *sc) 2357 { 2358 return (rsu_read_4(sc, R92S_TSFTR + 4)); 2359 } 2360 2361 static struct ieee80211_node * 2362 rsu_rx_frame(struct rsu_softc *sc, struct mbuf *m) 2363 { 2364 struct ieee80211com *ic = &sc->sc_ic; 2365 struct ieee80211_frame_min *wh; 2366 struct ieee80211_rx_stats rxs; 2367 struct r92s_rx_stat *stat; 2368 uint32_t rxdw0, rxdw3; 2369 uint8_t cipher, rate; 2370 int infosz; 2371 int rssi; 2372 2373 stat = mtod(m, struct r92s_rx_stat *); 2374 rxdw0 = le32toh(stat->rxdw0); 2375 rxdw3 = le32toh(stat->rxdw3); 2376 2377 rate = MS(rxdw3, R92S_RXDW3_RATE); 2378 cipher = MS(rxdw0, R92S_RXDW0_CIPHER); 2379 infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8; 2380 2381 /* Get RSSI from PHY status descriptor if present. */ 2382 if (infosz != 0 && (rxdw0 & R92S_RXDW0_PHYST)) 2383 rssi = rsu_get_rssi(sc, rate, &stat[1]); 2384 else { 2385 /* Cheat and get the last calibrated RSSI */ 2386 rssi = rsu_hwrssi_to_rssi(sc, sc->sc_currssi); 2387 } 2388 2389 /* Hardware does Rx TCP checksum offload. */ 2390 /* 2391 * This flag can be set for some other 2392 * (e.g., EAPOL) frame types, so don't rely on it. 2393 */ 2394 if (rxdw3 & R92S_RXDW3_TCPCHKVALID) { 2395 RSU_DPRINTF(sc, RSU_DEBUG_RX, 2396 "%s: TCP/IP checksums: %schecked / %schecked\n", 2397 __func__, 2398 (rxdw3 & R92S_RXDW3_TCPCHKRPT) ? "" : "not ", 2399 (rxdw3 & R92S_RXDW3_IPCHKRPT) ? "" : "not "); 2400 2401 /* 2402 * 'IP header checksum valid' bit will not be set if 2403 * the frame was not checked / has incorrect checksum / 2404 * does not have checksum (IPv6). 2405 * 2406 * NB: if DF bit is not set then frame will not be checked. 2407 */ 2408 if (rxdw3 & R92S_RXDW3_IPCHKRPT) { 2409 m->m_pkthdr.csum_flags = CSUM_IP_CHECKED; 2410 m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 2411 } 2412 2413 /* 2414 * This is independent of the above check. 2415 */ 2416 if (rxdw3 & R92S_RXDW3_TCPCHKRPT) { 2417 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID; 2418 m->m_pkthdr.csum_flags |= CSUM_PSEUDO_HDR; 2419 m->m_pkthdr.csum_data = 0xffff; 2420 } 2421 } 2422 2423 /* RX flags */ 2424 2425 /* Set channel flags for input path */ 2426 bzero(&rxs, sizeof(rxs)); 2427 2428 /* normal RSSI */ 2429 rxs.r_flags |= IEEE80211_R_NF | IEEE80211_R_RSSI; 2430 rxs.c_rssi = rssi; 2431 rxs.c_nf = -96; 2432 2433 /* Rate */ 2434 if (rate < 12) { 2435 rxs.c_rate = ridx2rate[rate]; 2436 if (RSU_RATE_IS_CCK(rate)) 2437 rxs.c_pktflags |= IEEE80211_RX_F_CCK; 2438 else 2439 rxs.c_pktflags |= IEEE80211_RX_F_OFDM; 2440 } else { 2441 rxs.c_rate = IEEE80211_RATE_MCS | (rate - 12); 2442 rxs.c_pktflags |= IEEE80211_RX_F_HT; 2443 } 2444 2445 if (ieee80211_radiotap_active(ic)) { 2446 struct rsu_rx_radiotap_header *tap = &sc->sc_rxtap; 2447 2448 /* Map HW rate index to 802.11 rate. */ 2449 tap->wr_flags = 0; /* TODO */ 2450 tap->wr_tsft = rsu_get_tsf_high(sc); 2451 if (le32toh(stat->tsf_low) > rsu_get_tsf_low(sc)) 2452 tap->wr_tsft--; 2453 tap->wr_tsft = (uint64_t)htole32(tap->wr_tsft) << 32; 2454 tap->wr_tsft += stat->tsf_low; 2455 2456 tap->wr_rate = rxs.c_rate; 2457 tap->wr_dbm_antsignal = rssi; 2458 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); 2459 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 2460 }; 2461 2462 (void) ieee80211_add_rx_params(m, &rxs); 2463 2464 /* Drop descriptor. */ 2465 m_adj(m, sizeof(*stat) + infosz); 2466 wh = mtod(m, struct ieee80211_frame_min *); 2467 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) && 2468 cipher != R92S_KEY_ALGO_NONE) { 2469 m->m_flags |= M_WEP; 2470 } 2471 2472 RSU_DPRINTF(sc, RSU_DEBUG_RX, 2473 "%s: Rx frame len %d, rate %d, infosz %d\n", 2474 __func__, m->m_len, rate, infosz); 2475 2476 if (m->m_len >= sizeof(*wh)) 2477 return (ieee80211_find_rxnode(ic, wh)); 2478 2479 return (NULL); 2480 } 2481 2482 static struct mbuf * 2483 rsu_rx_multi_frame(struct rsu_softc *sc, uint8_t *buf, int len) 2484 { 2485 struct r92s_rx_stat *stat; 2486 uint32_t rxdw0; 2487 int totlen, pktlen, infosz, npkts; 2488 struct mbuf *m, *m0 = NULL, *prevm = NULL; 2489 2490 /* 2491 * don't pass packets to the ieee80211 framework if the driver isn't 2492 * RUNNING. 2493 */ 2494 if (!sc->sc_running) 2495 return (NULL); 2496 2497 /* Get the number of encapsulated frames. */ 2498 stat = (struct r92s_rx_stat *)buf; 2499 npkts = MS(le32toh(stat->rxdw2), R92S_RXDW2_PKTCNT); 2500 RSU_DPRINTF(sc, RSU_DEBUG_RX, 2501 "%s: Rx %d frames in one chunk\n", __func__, npkts); 2502 2503 /* Process all of them. */ 2504 while (npkts-- > 0) { 2505 if (__predict_false(len < sizeof(*stat))) 2506 break; 2507 stat = (struct r92s_rx_stat *)buf; 2508 rxdw0 = le32toh(stat->rxdw0); 2509 2510 pktlen = MS(rxdw0, R92S_RXDW0_PKTLEN); 2511 if (__predict_false(pktlen == 0)) 2512 break; 2513 2514 infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8; 2515 2516 /* Make sure everything fits in xfer. */ 2517 totlen = sizeof(*stat) + infosz + pktlen; 2518 if (__predict_false(totlen > len)) 2519 break; 2520 2521 /* Process 802.11 frame. */ 2522 m = rsu_rx_copy_to_mbuf(sc, stat, totlen); 2523 if (m0 == NULL) 2524 m0 = m; 2525 if (prevm == NULL) 2526 prevm = m; 2527 else { 2528 prevm->m_next = m; 2529 prevm = m; 2530 } 2531 /* Next chunk is 128-byte aligned. */ 2532 totlen = (totlen + 127) & ~127; 2533 buf += totlen; 2534 len -= totlen; 2535 } 2536 2537 return (m0); 2538 } 2539 2540 static struct mbuf * 2541 rsu_rxeof(struct usb_xfer *xfer, struct rsu_data *data) 2542 { 2543 struct rsu_softc *sc = data->sc; 2544 struct ieee80211com *ic = &sc->sc_ic; 2545 struct r92s_rx_stat *stat; 2546 int len; 2547 2548 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 2549 2550 if (__predict_false(len < sizeof(*stat))) { 2551 RSU_DPRINTF(sc, RSU_DEBUG_RX, "xfer too short %d\n", len); 2552 counter_u64_add(ic->ic_ierrors, 1); 2553 return (NULL); 2554 } 2555 /* Determine if it is a firmware C2H event or an 802.11 frame. */ 2556 stat = (struct r92s_rx_stat *)data->buf; 2557 if ((le32toh(stat->rxdw1) & 0x1ff) == 0x1ff) { 2558 rsu_rx_multi_event(sc, data->buf, len); 2559 /* No packets to process. */ 2560 return (NULL); 2561 } else 2562 return (rsu_rx_multi_frame(sc, data->buf, len)); 2563 } 2564 2565 static void 2566 rsu_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error) 2567 { 2568 struct rsu_softc *sc = usbd_xfer_softc(xfer); 2569 struct ieee80211com *ic = &sc->sc_ic; 2570 struct ieee80211_node *ni; 2571 struct mbuf *m = NULL, *next; 2572 struct rsu_data *data; 2573 2574 RSU_ASSERT_LOCKED(sc); 2575 2576 switch (USB_GET_STATE(xfer)) { 2577 case USB_ST_TRANSFERRED: 2578 data = STAILQ_FIRST(&sc->sc_rx_active); 2579 if (data == NULL) 2580 goto tr_setup; 2581 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next); 2582 m = rsu_rxeof(xfer, data); 2583 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 2584 /* FALLTHROUGH */ 2585 case USB_ST_SETUP: 2586 tr_setup: 2587 data = STAILQ_FIRST(&sc->sc_rx_inactive); 2588 if (data == NULL) { 2589 KASSERT(m == NULL, ("mbuf isn't NULL")); 2590 return; 2591 } 2592 STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next); 2593 STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next); 2594 usbd_xfer_set_frame_data(xfer, 0, data->buf, 2595 usbd_xfer_max_len(xfer)); 2596 usbd_transfer_submit(xfer); 2597 /* 2598 * To avoid LOR we should unlock our private mutex here to call 2599 * ieee80211_input() because here is at the end of a USB 2600 * callback and safe to unlock. 2601 */ 2602 while (m != NULL) { 2603 next = m->m_next; 2604 m->m_next = NULL; 2605 2606 ni = rsu_rx_frame(sc, m); 2607 RSU_UNLOCK(sc); 2608 2609 if (ni != NULL) { 2610 if (ni->ni_flags & IEEE80211_NODE_HT) 2611 m->m_flags |= M_AMPDU; 2612 (void)ieee80211_input_mimo(ni, m); 2613 ieee80211_free_node(ni); 2614 } else 2615 (void)ieee80211_input_mimo_all(ic, m); 2616 2617 RSU_LOCK(sc); 2618 m = next; 2619 } 2620 break; 2621 default: 2622 /* needs it to the inactive queue due to a error. */ 2623 data = STAILQ_FIRST(&sc->sc_rx_active); 2624 if (data != NULL) { 2625 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next); 2626 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 2627 } 2628 if (error != USB_ERR_CANCELLED) { 2629 usbd_xfer_set_stall(xfer); 2630 counter_u64_add(ic->ic_ierrors, 1); 2631 goto tr_setup; 2632 } 2633 break; 2634 } 2635 2636 } 2637 2638 static void 2639 rsu_txeof(struct usb_xfer *xfer, struct rsu_data *data) 2640 { 2641 #ifdef USB_DEBUG 2642 struct rsu_softc *sc = usbd_xfer_softc(xfer); 2643 #endif 2644 2645 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, "%s: called; data=%p\n", 2646 __func__, 2647 data); 2648 2649 if (data->m) { 2650 /* XXX status? */ 2651 ieee80211_tx_complete(data->ni, data->m, 0); 2652 data->m = NULL; 2653 data->ni = NULL; 2654 } 2655 } 2656 2657 static void 2658 rsu_bulk_tx_callback_sub(struct usb_xfer *xfer, usb_error_t error, 2659 uint8_t which) 2660 { 2661 struct rsu_softc *sc = usbd_xfer_softc(xfer); 2662 struct ieee80211com *ic = &sc->sc_ic; 2663 struct rsu_data *data; 2664 2665 RSU_ASSERT_LOCKED(sc); 2666 2667 switch (USB_GET_STATE(xfer)) { 2668 case USB_ST_TRANSFERRED: 2669 data = STAILQ_FIRST(&sc->sc_tx_active[which]); 2670 if (data == NULL) 2671 goto tr_setup; 2672 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, "%s: transfer done %p\n", 2673 __func__, data); 2674 STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next); 2675 rsu_txeof(xfer, data); 2676 rsu_freebuf(sc, data); 2677 /* FALLTHROUGH */ 2678 case USB_ST_SETUP: 2679 tr_setup: 2680 data = STAILQ_FIRST(&sc->sc_tx_pending[which]); 2681 if (data == NULL) { 2682 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, 2683 "%s: empty pending queue sc %p\n", __func__, sc); 2684 return; 2685 } 2686 STAILQ_REMOVE_HEAD(&sc->sc_tx_pending[which], next); 2687 STAILQ_INSERT_TAIL(&sc->sc_tx_active[which], data, next); 2688 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen); 2689 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, 2690 "%s: submitting transfer %p\n", 2691 __func__, 2692 data); 2693 usbd_transfer_submit(xfer); 2694 break; 2695 default: 2696 data = STAILQ_FIRST(&sc->sc_tx_active[which]); 2697 if (data != NULL) { 2698 STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next); 2699 rsu_txeof(xfer, data); 2700 rsu_freebuf(sc, data); 2701 } 2702 counter_u64_add(ic->ic_oerrors, 1); 2703 2704 if (error != USB_ERR_CANCELLED) { 2705 usbd_xfer_set_stall(xfer); 2706 goto tr_setup; 2707 } 2708 break; 2709 } 2710 2711 /* 2712 * XXX TODO: if the queue is low, flush out FF TX frames. 2713 * Remember to unlock the driver for now; net80211 doesn't 2714 * defer it for us. 2715 */ 2716 } 2717 2718 static void 2719 rsu_bulk_tx_callback_be_bk(struct usb_xfer *xfer, usb_error_t error) 2720 { 2721 struct rsu_softc *sc = usbd_xfer_softc(xfer); 2722 2723 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_BE_BK); 2724 2725 /* This kicks the TX taskqueue */ 2726 rsu_start(sc); 2727 } 2728 2729 static void 2730 rsu_bulk_tx_callback_vi_vo(struct usb_xfer *xfer, usb_error_t error) 2731 { 2732 struct rsu_softc *sc = usbd_xfer_softc(xfer); 2733 2734 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_VI_VO); 2735 2736 /* This kicks the TX taskqueue */ 2737 rsu_start(sc); 2738 } 2739 2740 static void 2741 rsu_bulk_tx_callback_h2c(struct usb_xfer *xfer, usb_error_t error) 2742 { 2743 struct rsu_softc *sc = usbd_xfer_softc(xfer); 2744 2745 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_H2C); 2746 2747 /* This kicks the TX taskqueue */ 2748 rsu_start(sc); 2749 } 2750 2751 /* 2752 * Transmit the given frame. 2753 * 2754 * This doesn't free the node or mbuf upon failure. 2755 */ 2756 static int 2757 rsu_tx_start(struct rsu_softc *sc, struct ieee80211_node *ni, 2758 struct mbuf *m0, struct rsu_data *data) 2759 { 2760 struct ieee80211com *ic = &sc->sc_ic; 2761 struct ieee80211vap *vap = ni->ni_vap; 2762 struct ieee80211_frame *wh; 2763 struct ieee80211_key *k = NULL; 2764 struct r92s_tx_desc *txd; 2765 uint8_t type, cipher; 2766 int prio = 0; 2767 uint8_t which; 2768 int hasqos; 2769 int xferlen; 2770 int qid; 2771 2772 RSU_ASSERT_LOCKED(sc); 2773 2774 wh = mtod(m0, struct ieee80211_frame *); 2775 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 2776 2777 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: data=%p, m=%p\n", 2778 __func__, data, m0); 2779 2780 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 2781 k = ieee80211_crypto_encap(ni, m0); 2782 if (k == NULL) { 2783 device_printf(sc->sc_dev, 2784 "ieee80211_crypto_encap returns NULL.\n"); 2785 /* XXX we don't expect the fragmented frames */ 2786 return (ENOBUFS); 2787 } 2788 wh = mtod(m0, struct ieee80211_frame *); 2789 } 2790 /* If we have QoS then use it */ 2791 /* XXX TODO: mbuf WME/PRI versus TID? */ 2792 if (IEEE80211_QOS_HAS_SEQ(wh)) { 2793 /* Has QoS */ 2794 prio = M_WME_GETAC(m0); 2795 which = rsu_wme_ac_xfer_map[prio]; 2796 hasqos = 1; 2797 } else { 2798 /* Non-QoS TID */ 2799 /* XXX TODO: tid=0 for non-qos TID? */ 2800 which = rsu_wme_ac_xfer_map[WME_AC_BE]; 2801 hasqos = 0; 2802 prio = 0; 2803 } 2804 2805 qid = rsu_ac2qid[prio]; 2806 #if 0 2807 switch (type) { 2808 case IEEE80211_FC0_TYPE_CTL: 2809 case IEEE80211_FC0_TYPE_MGT: 2810 which = rsu_wme_ac_xfer_map[WME_AC_VO]; 2811 break; 2812 default: 2813 which = rsu_wme_ac_xfer_map[M_WME_GETAC(m0)]; 2814 break; 2815 } 2816 hasqos = 0; 2817 #endif 2818 2819 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: pri=%d, which=%d, hasqos=%d\n", 2820 __func__, 2821 prio, 2822 which, 2823 hasqos); 2824 2825 /* Fill Tx descriptor. */ 2826 txd = (struct r92s_tx_desc *)data->buf; 2827 memset(txd, 0, sizeof(*txd)); 2828 2829 txd->txdw0 |= htole32( 2830 SM(R92S_TXDW0_PKTLEN, m0->m_pkthdr.len) | 2831 SM(R92S_TXDW0_OFFSET, sizeof(*txd)) | 2832 R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG); 2833 2834 txd->txdw1 |= htole32( 2835 SM(R92S_TXDW1_MACID, R92S_MACID_BSS) | SM(R92S_TXDW1_QSEL, qid)); 2836 if (!hasqos) 2837 txd->txdw1 |= htole32(R92S_TXDW1_NONQOS); 2838 if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWENCRYPT)) { 2839 switch (k->wk_cipher->ic_cipher) { 2840 case IEEE80211_CIPHER_WEP: 2841 cipher = R92S_TXDW1_CIPHER_WEP; 2842 break; 2843 case IEEE80211_CIPHER_TKIP: 2844 cipher = R92S_TXDW1_CIPHER_TKIP; 2845 break; 2846 case IEEE80211_CIPHER_AES_CCM: 2847 cipher = R92S_TXDW1_CIPHER_AES; 2848 break; 2849 default: 2850 cipher = R92S_TXDW1_CIPHER_NONE; 2851 } 2852 txd->txdw1 |= htole32( 2853 SM(R92S_TXDW1_CIPHER, cipher) | 2854 SM(R92S_TXDW1_KEYIDX, k->wk_keyix)); 2855 } 2856 /* XXX todo: set AGGEN bit if appropriate? */ 2857 txd->txdw2 |= htole32(R92S_TXDW2_BK); 2858 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 2859 txd->txdw2 |= htole32(R92S_TXDW2_BMCAST); 2860 /* 2861 * Firmware will use and increment the sequence number for the 2862 * specified priority. 2863 */ 2864 txd->txdw3 |= htole32(SM(R92S_TXDW3_SEQ, prio)); 2865 2866 if (ieee80211_radiotap_active_vap(vap)) { 2867 struct rsu_tx_radiotap_header *tap = &sc->sc_txtap; 2868 2869 tap->wt_flags = 0; 2870 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 2871 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 2872 ieee80211_radiotap_tx(vap, m0); 2873 } 2874 2875 xferlen = sizeof(*txd) + m0->m_pkthdr.len; 2876 m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&txd[1]); 2877 2878 data->buflen = xferlen; 2879 data->ni = ni; 2880 data->m = m0; 2881 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next); 2882 2883 /* start transfer, if any */ 2884 usbd_transfer_start(sc->sc_xfer[which]); 2885 return (0); 2886 } 2887 2888 static int 2889 rsu_transmit(struct ieee80211com *ic, struct mbuf *m) 2890 { 2891 struct rsu_softc *sc = ic->ic_softc; 2892 int error; 2893 2894 RSU_LOCK(sc); 2895 if (!sc->sc_running) { 2896 RSU_UNLOCK(sc); 2897 return (ENXIO); 2898 } 2899 2900 /* 2901 * XXX TODO: ensure that we treat 'm' as a list of frames 2902 * to transmit! 2903 */ 2904 error = mbufq_enqueue(&sc->sc_snd, m); 2905 if (error) { 2906 RSU_DPRINTF(sc, RSU_DEBUG_TX, 2907 "%s: mbufq_enable: failed (%d)\n", 2908 __func__, 2909 error); 2910 RSU_UNLOCK(sc); 2911 return (error); 2912 } 2913 RSU_UNLOCK(sc); 2914 2915 /* This kicks the TX taskqueue */ 2916 rsu_start(sc); 2917 2918 return (0); 2919 } 2920 2921 static void 2922 rsu_drain_mbufq(struct rsu_softc *sc) 2923 { 2924 struct mbuf *m; 2925 struct ieee80211_node *ni; 2926 2927 RSU_ASSERT_LOCKED(sc); 2928 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 2929 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 2930 m->m_pkthdr.rcvif = NULL; 2931 ieee80211_free_node(ni); 2932 m_freem(m); 2933 } 2934 } 2935 2936 static void 2937 _rsu_start(struct rsu_softc *sc) 2938 { 2939 struct ieee80211_node *ni; 2940 struct rsu_data *bf; 2941 struct mbuf *m; 2942 2943 RSU_ASSERT_LOCKED(sc); 2944 2945 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 2946 bf = rsu_getbuf(sc); 2947 if (bf == NULL) { 2948 RSU_DPRINTF(sc, RSU_DEBUG_TX, 2949 "%s: failed to get buffer\n", __func__); 2950 mbufq_prepend(&sc->sc_snd, m); 2951 break; 2952 } 2953 2954 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 2955 m->m_pkthdr.rcvif = NULL; 2956 2957 if (rsu_tx_start(sc, ni, m, bf) != 0) { 2958 RSU_DPRINTF(sc, RSU_DEBUG_TX, 2959 "%s: failed to transmit\n", __func__); 2960 if_inc_counter(ni->ni_vap->iv_ifp, 2961 IFCOUNTER_OERRORS, 1); 2962 rsu_freebuf(sc, bf); 2963 ieee80211_free_node(ni); 2964 m_freem(m); 2965 break; 2966 } 2967 } 2968 } 2969 2970 static void 2971 rsu_start(struct rsu_softc *sc) 2972 { 2973 2974 taskqueue_enqueue(taskqueue_thread, &sc->tx_task); 2975 } 2976 2977 static int 2978 rsu_ioctl_net(struct ieee80211com *ic, u_long cmd, void *data) 2979 { 2980 struct rsu_softc *sc = ic->ic_softc; 2981 struct ifreq *ifr = (struct ifreq *)data; 2982 int error; 2983 2984 error = 0; 2985 switch (cmd) { 2986 case SIOCSIFCAP: 2987 { 2988 struct ieee80211vap *vap; 2989 int rxmask; 2990 2991 rxmask = ifr->ifr_reqcap & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6); 2992 2993 RSU_LOCK(sc); 2994 /* Both RXCSUM bits must be set (or unset). */ 2995 if (sc->sc_rx_checksum_enable && 2996 rxmask != (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) { 2997 rxmask = 0; 2998 sc->sc_rx_checksum_enable = 0; 2999 rsu_rxfilter_set(sc, R92S_RCR_TCP_OFFLD_EN, 0); 3000 } else if (!sc->sc_rx_checksum_enable && rxmask != 0) { 3001 rxmask = IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6; 3002 sc->sc_rx_checksum_enable = 1; 3003 rsu_rxfilter_set(sc, 0, R92S_RCR_TCP_OFFLD_EN); 3004 } else { 3005 /* Nothing to do. */ 3006 RSU_UNLOCK(sc); 3007 break; 3008 } 3009 RSU_UNLOCK(sc); 3010 3011 IEEE80211_LOCK(ic); /* XXX */ 3012 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 3013 struct ifnet *ifp = vap->iv_ifp; 3014 3015 ifp->if_capenable &= 3016 ~(IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6); 3017 ifp->if_capenable |= rxmask; 3018 } 3019 IEEE80211_UNLOCK(ic); 3020 break; 3021 } 3022 default: 3023 error = ENOTTY; /* for net80211 */ 3024 break; 3025 } 3026 3027 return (error); 3028 } 3029 3030 static void 3031 rsu_parent(struct ieee80211com *ic) 3032 { 3033 struct rsu_softc *sc = ic->ic_softc; 3034 3035 if (ic->ic_nrunning > 0) { 3036 if (rsu_init(sc) == 0) 3037 ieee80211_start_all(ic); 3038 else { 3039 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 3040 if (vap != NULL) 3041 ieee80211_stop(vap); 3042 } 3043 } else 3044 rsu_stop(sc); 3045 } 3046 3047 /* 3048 * Power on sequence for A-cut adapters. 3049 */ 3050 static void 3051 rsu_power_on_acut(struct rsu_softc *sc) 3052 { 3053 uint32_t reg; 3054 3055 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53); 3056 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57); 3057 3058 /* Enable AFE macro block's bandgap and Mbias. */ 3059 rsu_write_1(sc, R92S_AFE_MISC, 3060 rsu_read_1(sc, R92S_AFE_MISC) | 3061 R92S_AFE_MISC_BGEN | R92S_AFE_MISC_MBEN); 3062 /* Enable LDOA15 block. */ 3063 rsu_write_1(sc, R92S_LDOA15_CTRL, 3064 rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN); 3065 3066 rsu_write_1(sc, R92S_SPS1_CTRL, 3067 rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_LDEN); 3068 rsu_ms_delay(sc, 2000); 3069 /* Enable switch regulator block. */ 3070 rsu_write_1(sc, R92S_SPS1_CTRL, 3071 rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_SWEN); 3072 3073 rsu_write_4(sc, R92S_SPS1_CTRL, 0x00a7b267); 3074 3075 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 3076 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08); 3077 3078 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 3079 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20); 3080 3081 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 3082 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x90); 3083 3084 /* Enable AFE clock. */ 3085 rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1, 3086 rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04); 3087 /* Enable AFE PLL macro block. */ 3088 rsu_write_1(sc, R92S_AFE_PLL_CTRL, 3089 rsu_read_1(sc, R92S_AFE_PLL_CTRL) | 0x11); 3090 /* Attach AFE PLL to MACTOP/BB. */ 3091 rsu_write_1(sc, R92S_SYS_ISO_CTRL, 3092 rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11); 3093 3094 /* Switch to 40MHz clock instead of 80MHz. */ 3095 rsu_write_2(sc, R92S_SYS_CLKR, 3096 rsu_read_2(sc, R92S_SYS_CLKR) & ~R92S_SYS_CLKSEL); 3097 3098 /* Enable MAC clock. */ 3099 rsu_write_2(sc, R92S_SYS_CLKR, 3100 rsu_read_2(sc, R92S_SYS_CLKR) | 3101 R92S_MAC_CLK_EN | R92S_SYS_CLK_EN); 3102 3103 rsu_write_1(sc, R92S_PMC_FSM, 0x02); 3104 3105 /* Enable digital core and IOREG R/W. */ 3106 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 3107 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08); 3108 3109 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 3110 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80); 3111 3112 /* Switch the control path to firmware. */ 3113 reg = rsu_read_2(sc, R92S_SYS_CLKR); 3114 reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL; 3115 rsu_write_2(sc, R92S_SYS_CLKR, reg); 3116 3117 rsu_write_2(sc, R92S_CR, 0x37fc); 3118 3119 /* Fix USB RX FIFO issue. */ 3120 rsu_write_1(sc, 0xfe5c, 3121 rsu_read_1(sc, 0xfe5c) | 0x80); 3122 rsu_write_1(sc, 0x00ab, 3123 rsu_read_1(sc, 0x00ab) | 0xc0); 3124 3125 rsu_write_1(sc, R92S_SYS_CLKR, 3126 rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL); 3127 } 3128 3129 /* 3130 * Power on sequence for B-cut and C-cut adapters. 3131 */ 3132 static void 3133 rsu_power_on_bcut(struct rsu_softc *sc) 3134 { 3135 uint32_t reg; 3136 int ntries; 3137 3138 /* Prevent eFuse leakage. */ 3139 rsu_write_1(sc, 0x37, 0xb0); 3140 rsu_ms_delay(sc, 10); 3141 rsu_write_1(sc, 0x37, 0x30); 3142 3143 /* Switch the control path to hardware. */ 3144 reg = rsu_read_2(sc, R92S_SYS_CLKR); 3145 if (reg & R92S_FWHW_SEL) { 3146 rsu_write_2(sc, R92S_SYS_CLKR, 3147 reg & ~(R92S_SWHW_SEL | R92S_FWHW_SEL)); 3148 } 3149 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 3150 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) & ~0x8c); 3151 rsu_ms_delay(sc, 1); 3152 3153 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53); 3154 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57); 3155 3156 reg = rsu_read_1(sc, R92S_AFE_MISC); 3157 rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN); 3158 rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN | 3159 R92S_AFE_MISC_MBEN | R92S_AFE_MISC_I32_EN); 3160 3161 /* Enable PLL. */ 3162 rsu_write_1(sc, R92S_LDOA15_CTRL, 3163 rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN); 3164 3165 rsu_write_1(sc, R92S_LDOV12D_CTRL, 3166 rsu_read_1(sc, R92S_LDOV12D_CTRL) | R92S_LDV12_EN); 3167 3168 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 3169 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08); 3170 3171 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 3172 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20); 3173 3174 /* Support 64KB IMEM. */ 3175 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 3176 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x97); 3177 3178 /* Enable AFE clock. */ 3179 rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1, 3180 rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04); 3181 /* Enable AFE PLL macro block. */ 3182 reg = rsu_read_1(sc, R92S_AFE_PLL_CTRL); 3183 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11); 3184 rsu_ms_delay(sc, 1); 3185 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x51); 3186 rsu_ms_delay(sc, 1); 3187 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11); 3188 rsu_ms_delay(sc, 1); 3189 3190 /* Attach AFE PLL to MACTOP/BB. */ 3191 rsu_write_1(sc, R92S_SYS_ISO_CTRL, 3192 rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11); 3193 3194 /* Switch to 40MHz clock. */ 3195 rsu_write_1(sc, R92S_SYS_CLKR, 0x00); 3196 /* Disable CPU clock and 80MHz SSC. */ 3197 rsu_write_1(sc, R92S_SYS_CLKR, 3198 rsu_read_1(sc, R92S_SYS_CLKR) | 0xa0); 3199 /* Enable MAC clock. */ 3200 rsu_write_2(sc, R92S_SYS_CLKR, 3201 rsu_read_2(sc, R92S_SYS_CLKR) | 3202 R92S_MAC_CLK_EN | R92S_SYS_CLK_EN); 3203 3204 rsu_write_1(sc, R92S_PMC_FSM, 0x02); 3205 3206 /* Enable digital core and IOREG R/W. */ 3207 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 3208 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08); 3209 3210 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 3211 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80); 3212 3213 /* Switch the control path to firmware. */ 3214 reg = rsu_read_2(sc, R92S_SYS_CLKR); 3215 reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL; 3216 rsu_write_2(sc, R92S_SYS_CLKR, reg); 3217 3218 rsu_write_2(sc, R92S_CR, 0x37fc); 3219 3220 /* Fix USB RX FIFO issue. */ 3221 rsu_write_1(sc, 0xfe5c, 3222 rsu_read_1(sc, 0xfe5c) | 0x80); 3223 3224 rsu_write_1(sc, R92S_SYS_CLKR, 3225 rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL); 3226 3227 rsu_write_1(sc, 0xfe1c, 0x80); 3228 3229 /* Make sure TxDMA is ready to download firmware. */ 3230 for (ntries = 0; ntries < 20; ntries++) { 3231 reg = rsu_read_1(sc, R92S_TCR); 3232 if ((reg & (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT)) == 3233 (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT)) 3234 break; 3235 rsu_ms_delay(sc, 1); 3236 } 3237 if (ntries == 20) { 3238 RSU_DPRINTF(sc, RSU_DEBUG_RESET | RSU_DEBUG_TX, 3239 "%s: TxDMA is not ready\n", 3240 __func__); 3241 /* Reset TxDMA. */ 3242 reg = rsu_read_1(sc, R92S_CR); 3243 rsu_write_1(sc, R92S_CR, reg & ~R92S_CR_TXDMA_EN); 3244 rsu_ms_delay(sc, 1); 3245 rsu_write_1(sc, R92S_CR, reg | R92S_CR_TXDMA_EN); 3246 } 3247 } 3248 3249 static void 3250 rsu_power_off(struct rsu_softc *sc) 3251 { 3252 /* Turn RF off. */ 3253 rsu_write_1(sc, R92S_RF_CTRL, 0x00); 3254 rsu_ms_delay(sc, 5); 3255 3256 /* Turn MAC off. */ 3257 /* Switch control path. */ 3258 rsu_write_1(sc, R92S_SYS_CLKR + 1, 0x38); 3259 /* Reset MACTOP. */ 3260 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x70); 3261 rsu_write_1(sc, R92S_PMC_FSM, 0x06); 3262 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 0, 0xf9); 3263 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 0xe8); 3264 3265 /* Disable AFE PLL. */ 3266 rsu_write_1(sc, R92S_AFE_PLL_CTRL, 0x00); 3267 /* Disable A15V. */ 3268 rsu_write_1(sc, R92S_LDOA15_CTRL, 0x54); 3269 /* Disable eFuse 1.2V. */ 3270 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x50); 3271 rsu_write_1(sc, R92S_LDOV12D_CTRL, 0x24); 3272 /* Enable AFE macro block's bandgap and Mbias. */ 3273 rsu_write_1(sc, R92S_AFE_MISC, 0x30); 3274 /* Disable 1.6V LDO. */ 3275 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x56); 3276 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x43); 3277 3278 /* Firmware - tell it to switch things off */ 3279 (void) rsu_set_fw_power_state(sc, RSU_PWR_OFF); 3280 } 3281 3282 static int 3283 rsu_fw_loadsection(struct rsu_softc *sc, const uint8_t *buf, int len) 3284 { 3285 const uint8_t which = rsu_wme_ac_xfer_map[WME_AC_VO]; 3286 struct rsu_data *data; 3287 struct r92s_tx_desc *txd; 3288 int mlen; 3289 3290 while (len > 0) { 3291 data = rsu_getbuf(sc); 3292 if (data == NULL) 3293 return (ENOMEM); 3294 txd = (struct r92s_tx_desc *)data->buf; 3295 memset(txd, 0, sizeof(*txd)); 3296 if (len <= RSU_TXBUFSZ - sizeof(*txd)) { 3297 /* Last chunk. */ 3298 txd->txdw0 |= htole32(R92S_TXDW0_LINIP); 3299 mlen = len; 3300 } else 3301 mlen = RSU_TXBUFSZ - sizeof(*txd); 3302 txd->txdw0 |= htole32(SM(R92S_TXDW0_PKTLEN, mlen)); 3303 memcpy(&txd[1], buf, mlen); 3304 data->buflen = sizeof(*txd) + mlen; 3305 RSU_DPRINTF(sc, RSU_DEBUG_TX | RSU_DEBUG_FW | RSU_DEBUG_RESET, 3306 "%s: starting transfer %p\n", 3307 __func__, data); 3308 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next); 3309 buf += mlen; 3310 len -= mlen; 3311 } 3312 usbd_transfer_start(sc->sc_xfer[which]); 3313 return (0); 3314 } 3315 3316 CTASSERT(sizeof(size_t) >= sizeof(uint32_t)); 3317 3318 static int 3319 rsu_load_firmware(struct rsu_softc *sc) 3320 { 3321 const struct r92s_fw_hdr *hdr; 3322 struct r92s_fw_priv *dmem; 3323 struct ieee80211com *ic = &sc->sc_ic; 3324 const uint8_t *imem, *emem; 3325 uint32_t imemsz, ememsz; 3326 const struct firmware *fw; 3327 size_t size; 3328 uint32_t reg; 3329 int ntries, error; 3330 3331 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY) { 3332 RSU_DPRINTF(sc, RSU_DEBUG_ANY, 3333 "%s: Firmware already loaded\n", 3334 __func__); 3335 return (0); 3336 } 3337 3338 RSU_UNLOCK(sc); 3339 /* Read firmware image from the filesystem. */ 3340 if ((fw = firmware_get("rsu-rtl8712fw")) == NULL) { 3341 device_printf(sc->sc_dev, 3342 "%s: failed load firmware of file rsu-rtl8712fw\n", 3343 __func__); 3344 RSU_LOCK(sc); 3345 return (ENXIO); 3346 } 3347 RSU_LOCK(sc); 3348 size = fw->datasize; 3349 if (size < sizeof(*hdr)) { 3350 device_printf(sc->sc_dev, "firmware too short\n"); 3351 error = EINVAL; 3352 goto fail; 3353 } 3354 hdr = (const struct r92s_fw_hdr *)fw->data; 3355 if (hdr->signature != htole16(0x8712) && 3356 hdr->signature != htole16(0x8192)) { 3357 device_printf(sc->sc_dev, 3358 "invalid firmware signature 0x%x\n", 3359 le16toh(hdr->signature)); 3360 error = EINVAL; 3361 goto fail; 3362 } 3363 RSU_DPRINTF(sc, RSU_DEBUG_FW, "FW V%d %02x-%02x %02x:%02x\n", 3364 le16toh(hdr->version), hdr->month, hdr->day, hdr->hour, 3365 hdr->minute); 3366 3367 /* Make sure that driver and firmware are in sync. */ 3368 if (hdr->privsz != htole32(sizeof(*dmem))) { 3369 device_printf(sc->sc_dev, "unsupported firmware image\n"); 3370 error = EINVAL; 3371 goto fail; 3372 } 3373 /* Get FW sections sizes. */ 3374 imemsz = le32toh(hdr->imemsz); 3375 ememsz = le32toh(hdr->sramsz); 3376 /* Check that all FW sections fit in image. */ 3377 if (imemsz > (size_t)(size - sizeof(*hdr)) || 3378 ememsz > (size_t)(size - sizeof(*hdr) - imemsz)) { 3379 device_printf(sc->sc_dev, "firmware too short\n"); 3380 error = EINVAL; 3381 goto fail; 3382 } 3383 imem = (const uint8_t *)&hdr[1]; 3384 emem = imem + imemsz; 3385 3386 /* Load IMEM section. */ 3387 error = rsu_fw_loadsection(sc, imem, imemsz); 3388 if (error != 0) { 3389 device_printf(sc->sc_dev, 3390 "could not load firmware section %s\n", "IMEM"); 3391 goto fail; 3392 } 3393 /* Wait for load to complete. */ 3394 for (ntries = 0; ntries != 50; ntries++) { 3395 rsu_ms_delay(sc, 10); 3396 reg = rsu_read_1(sc, R92S_TCR); 3397 if (reg & R92S_TCR_IMEM_CODE_DONE) 3398 break; 3399 } 3400 if (ntries == 50) { 3401 device_printf(sc->sc_dev, "timeout waiting for IMEM transfer\n"); 3402 error = ETIMEDOUT; 3403 goto fail; 3404 } 3405 /* Load EMEM section. */ 3406 error = rsu_fw_loadsection(sc, emem, ememsz); 3407 if (error != 0) { 3408 device_printf(sc->sc_dev, 3409 "could not load firmware section %s\n", "EMEM"); 3410 goto fail; 3411 } 3412 /* Wait for load to complete. */ 3413 for (ntries = 0; ntries != 50; ntries++) { 3414 rsu_ms_delay(sc, 10); 3415 reg = rsu_read_2(sc, R92S_TCR); 3416 if (reg & R92S_TCR_EMEM_CODE_DONE) 3417 break; 3418 } 3419 if (ntries == 50) { 3420 device_printf(sc->sc_dev, "timeout waiting for EMEM transfer\n"); 3421 error = ETIMEDOUT; 3422 goto fail; 3423 } 3424 /* Enable CPU. */ 3425 rsu_write_1(sc, R92S_SYS_CLKR, 3426 rsu_read_1(sc, R92S_SYS_CLKR) | R92S_SYS_CPU_CLKSEL); 3427 if (!(rsu_read_1(sc, R92S_SYS_CLKR) & R92S_SYS_CPU_CLKSEL)) { 3428 device_printf(sc->sc_dev, "could not enable system clock\n"); 3429 error = EIO; 3430 goto fail; 3431 } 3432 rsu_write_2(sc, R92S_SYS_FUNC_EN, 3433 rsu_read_2(sc, R92S_SYS_FUNC_EN) | R92S_FEN_CPUEN); 3434 if (!(rsu_read_2(sc, R92S_SYS_FUNC_EN) & R92S_FEN_CPUEN)) { 3435 device_printf(sc->sc_dev, 3436 "could not enable microcontroller\n"); 3437 error = EIO; 3438 goto fail; 3439 } 3440 /* Wait for CPU to initialize. */ 3441 for (ntries = 0; ntries < 100; ntries++) { 3442 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_IMEM_RDY) 3443 break; 3444 rsu_ms_delay(sc, 1); 3445 } 3446 if (ntries == 100) { 3447 device_printf(sc->sc_dev, 3448 "timeout waiting for microcontroller\n"); 3449 error = ETIMEDOUT; 3450 goto fail; 3451 } 3452 3453 /* Update DMEM section before loading. */ 3454 dmem = __DECONST(struct r92s_fw_priv *, &hdr->priv); 3455 memset(dmem, 0, sizeof(*dmem)); 3456 dmem->hci_sel = R92S_HCI_SEL_USB | R92S_HCI_SEL_8172; 3457 dmem->nendpoints = sc->sc_nendpoints; 3458 dmem->chip_version = sc->cut; 3459 dmem->rf_config = sc->sc_rftype; 3460 dmem->vcs_type = R92S_VCS_TYPE_AUTO; 3461 dmem->vcs_mode = R92S_VCS_MODE_RTS_CTS; 3462 dmem->turbo_mode = 0; 3463 dmem->bw40_en = !! (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40); 3464 dmem->amsdu2ampdu_en = !! (sc->sc_ht); 3465 dmem->ampdu_en = !! (sc->sc_ht); 3466 dmem->agg_offload = !! (sc->sc_ht); 3467 dmem->qos_en = 1; 3468 dmem->ps_offload = 1; 3469 dmem->lowpower_mode = 1; /* XXX TODO: configurable? */ 3470 /* Load DMEM section. */ 3471 error = rsu_fw_loadsection(sc, (uint8_t *)dmem, sizeof(*dmem)); 3472 if (error != 0) { 3473 device_printf(sc->sc_dev, 3474 "could not load firmware section %s\n", "DMEM"); 3475 goto fail; 3476 } 3477 /* Wait for load to complete. */ 3478 for (ntries = 0; ntries < 100; ntries++) { 3479 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_DMEM_CODE_DONE) 3480 break; 3481 rsu_ms_delay(sc, 1); 3482 } 3483 if (ntries == 100) { 3484 device_printf(sc->sc_dev, "timeout waiting for %s transfer\n", 3485 "DMEM"); 3486 error = ETIMEDOUT; 3487 goto fail; 3488 } 3489 /* Wait for firmware readiness. */ 3490 for (ntries = 0; ntries < 60; ntries++) { 3491 if (!(rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY)) 3492 break; 3493 rsu_ms_delay(sc, 1); 3494 } 3495 if (ntries == 60) { 3496 device_printf(sc->sc_dev, 3497 "timeout waiting for firmware readiness\n"); 3498 error = ETIMEDOUT; 3499 goto fail; 3500 } 3501 fail: 3502 firmware_put(fw, FIRMWARE_UNLOAD); 3503 return (error); 3504 } 3505 3506 3507 static int 3508 rsu_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 3509 const struct ieee80211_bpf_params *params) 3510 { 3511 struct ieee80211com *ic = ni->ni_ic; 3512 struct rsu_softc *sc = ic->ic_softc; 3513 struct rsu_data *bf; 3514 3515 /* prevent management frames from being sent if we're not ready */ 3516 if (!sc->sc_running) { 3517 m_freem(m); 3518 return (ENETDOWN); 3519 } 3520 RSU_LOCK(sc); 3521 bf = rsu_getbuf(sc); 3522 if (bf == NULL) { 3523 m_freem(m); 3524 RSU_UNLOCK(sc); 3525 return (ENOBUFS); 3526 } 3527 if (rsu_tx_start(sc, ni, m, bf) != 0) { 3528 m_freem(m); 3529 rsu_freebuf(sc, bf); 3530 RSU_UNLOCK(sc); 3531 return (EIO); 3532 } 3533 RSU_UNLOCK(sc); 3534 3535 return (0); 3536 } 3537 3538 static void 3539 rsu_rxfilter_init(struct rsu_softc *sc) 3540 { 3541 uint32_t reg; 3542 3543 RSU_ASSERT_LOCKED(sc); 3544 3545 /* Setup multicast filter. */ 3546 rsu_set_multi(sc); 3547 3548 /* Adjust Rx filter. */ 3549 reg = rsu_read_4(sc, R92S_RCR); 3550 reg &= ~R92S_RCR_AICV; 3551 reg |= R92S_RCR_APP_PHYSTS; 3552 if (sc->sc_rx_checksum_enable) 3553 reg |= R92S_RCR_TCP_OFFLD_EN; 3554 rsu_write_4(sc, R92S_RCR, reg); 3555 3556 /* Update dynamic Rx filter parts. */ 3557 rsu_rxfilter_refresh(sc); 3558 } 3559 3560 static void 3561 rsu_rxfilter_set(struct rsu_softc *sc, uint32_t clear, uint32_t set) 3562 { 3563 /* NB: firmware can touch this register too. */ 3564 rsu_write_4(sc, R92S_RCR, 3565 (rsu_read_4(sc, R92S_RCR) & ~clear) | set); 3566 } 3567 3568 static void 3569 rsu_rxfilter_refresh(struct rsu_softc *sc) 3570 { 3571 struct ieee80211com *ic = &sc->sc_ic; 3572 uint32_t mask_all, mask_min; 3573 3574 RSU_ASSERT_LOCKED(sc); 3575 3576 /* NB: RCR_AMF / RXFLTMAP_MGT are used by firmware. */ 3577 mask_all = R92S_RCR_ACF | R92S_RCR_AAP; 3578 mask_min = R92S_RCR_APM; 3579 if (sc->sc_vap_is_running) 3580 mask_min |= R92S_RCR_CBSSID; 3581 else 3582 mask_all |= R92S_RCR_ADF; 3583 3584 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 3585 uint16_t rxfltmap; 3586 if (sc->sc_vap_is_running) 3587 rxfltmap = 0; 3588 else 3589 rxfltmap = R92S_RXFLTMAP_MGT_DEF; 3590 rsu_write_2(sc, R92S_RXFLTMAP_MGT, rxfltmap); 3591 } 3592 3593 if (ic->ic_promisc == 0 && ic->ic_opmode != IEEE80211_M_MONITOR) 3594 rsu_rxfilter_set(sc, mask_all, mask_min); 3595 else 3596 rsu_rxfilter_set(sc, mask_min, mask_all); 3597 } 3598 3599 static int 3600 rsu_init(struct rsu_softc *sc) 3601 { 3602 struct ieee80211com *ic = &sc->sc_ic; 3603 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 3604 uint8_t macaddr[IEEE80211_ADDR_LEN]; 3605 int error; 3606 int i; 3607 3608 RSU_LOCK(sc); 3609 3610 if (sc->sc_running) { 3611 RSU_UNLOCK(sc); 3612 return (0); 3613 } 3614 3615 /* Ensure the mbuf queue is drained */ 3616 rsu_drain_mbufq(sc); 3617 3618 /* Reset power management state. */ 3619 rsu_write_1(sc, R92S_USB_HRPWM, 0); 3620 3621 /* Power on adapter. */ 3622 if (sc->cut == 1) 3623 rsu_power_on_acut(sc); 3624 else 3625 rsu_power_on_bcut(sc); 3626 3627 /* Load firmware. */ 3628 error = rsu_load_firmware(sc); 3629 if (error != 0) 3630 goto fail; 3631 3632 rsu_write_4(sc, R92S_CR, 3633 rsu_read_4(sc, R92S_CR) & ~0xff000000); 3634 3635 /* Use 128 bytes pages. */ 3636 rsu_write_1(sc, 0x00b5, 3637 rsu_read_1(sc, 0x00b5) | 0x01); 3638 /* Enable USB Rx aggregation. */ 3639 rsu_write_1(sc, 0x00bd, 3640 rsu_read_1(sc, 0x00bd) | 0x80); 3641 /* Set USB Rx aggregation threshold. */ 3642 rsu_write_1(sc, 0x00d9, 0x01); 3643 /* Set USB Rx aggregation timeout (1.7ms/4). */ 3644 rsu_write_1(sc, 0xfe5b, 0x04); 3645 /* Fix USB Rx FIFO issue. */ 3646 rsu_write_1(sc, 0xfe5c, 3647 rsu_read_1(sc, 0xfe5c) | 0x80); 3648 3649 /* Set MAC address. */ 3650 IEEE80211_ADDR_COPY(macaddr, vap ? vap->iv_myaddr : ic->ic_macaddr); 3651 rsu_write_region_1(sc, R92S_MACID, macaddr, IEEE80211_ADDR_LEN); 3652 3653 /* It really takes 1.5 seconds for the firmware to boot: */ 3654 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(2000)); 3655 3656 RSU_DPRINTF(sc, RSU_DEBUG_RESET, "%s: setting MAC address to %s\n", 3657 __func__, 3658 ether_sprintf(macaddr)); 3659 error = rsu_fw_cmd(sc, R92S_CMD_SET_MAC_ADDRESS, macaddr, 3660 IEEE80211_ADDR_LEN); 3661 if (error != 0) { 3662 device_printf(sc->sc_dev, "could not set MAC address\n"); 3663 goto fail; 3664 } 3665 3666 /* Initialize Rx filter. */ 3667 rsu_rxfilter_init(sc); 3668 3669 /* Set PS mode fully active */ 3670 error = rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE); 3671 if (error != 0) { 3672 device_printf(sc->sc_dev, "could not set PS mode\n"); 3673 goto fail; 3674 } 3675 3676 /* Install static keys (if any). */ 3677 error = rsu_reinit_static_keys(sc); 3678 if (error != 0) 3679 goto fail; 3680 3681 sc->sc_extra_scan = 0; 3682 usbd_transfer_start(sc->sc_xfer[RSU_BULK_RX]); 3683 3684 /* We're ready to go. */ 3685 sc->sc_running = 1; 3686 RSU_UNLOCK(sc); 3687 3688 return (0); 3689 fail: 3690 /* Need to stop all failed transfers, if any */ 3691 for (i = 0; i != RSU_N_TRANSFER; i++) 3692 usbd_transfer_stop(sc->sc_xfer[i]); 3693 RSU_UNLOCK(sc); 3694 3695 return (error); 3696 } 3697 3698 static void 3699 rsu_stop(struct rsu_softc *sc) 3700 { 3701 int i; 3702 3703 RSU_LOCK(sc); 3704 if (!sc->sc_running) { 3705 RSU_UNLOCK(sc); 3706 return; 3707 } 3708 3709 sc->sc_running = 0; 3710 sc->sc_vap_is_running = 0; 3711 sc->sc_calibrating = 0; 3712 taskqueue_cancel_timeout(taskqueue_thread, &sc->calib_task, NULL); 3713 taskqueue_cancel(taskqueue_thread, &sc->tx_task, NULL); 3714 3715 /* Power off adapter. */ 3716 rsu_power_off(sc); 3717 3718 /* 3719 * CAM is not accessible after shutdown; 3720 * all entries are marked (by firmware?) as invalid. 3721 */ 3722 memset(sc->free_keys_bmap, 0, sizeof(sc->free_keys_bmap)); 3723 memset(sc->keys_bmap, 0, sizeof(sc->keys_bmap)); 3724 3725 for (i = 0; i < RSU_N_TRANSFER; i++) 3726 usbd_transfer_stop(sc->sc_xfer[i]); 3727 3728 /* Ensure the mbuf queue is drained */ 3729 rsu_drain_mbufq(sc); 3730 RSU_UNLOCK(sc); 3731 } 3732 3733 /* 3734 * Note: usb_pause_mtx() actually releases the mutex before calling pause(), 3735 * which breaks any kind of driver serialisation. 3736 */ 3737 static void 3738 rsu_ms_delay(struct rsu_softc *sc, int ms) 3739 { 3740 3741 //usb_pause_mtx(&sc->sc_mtx, hz / 1000); 3742 DELAY(ms * 1000); 3743 } 3744