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