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