1 /* $OpenBSD: if_urtwn.c,v 1.100 2022/04/21 21:03:03 stsp Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr> 5 * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org> 6 * Copyright (c) 2016 Nathanial Sloss <nathanialsloss@yahoo.com.au> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 /* 22 * Driver for Realtek RTL8188CE-VAU/RTL8188CUS/RTL8188EU/RTL8188RU/RTL8192CU/ 23 * RTL8192EU. 24 */ 25 26 #include "bpfilter.h" 27 28 #include <sys/param.h> 29 #include <sys/sockio.h> 30 #include <sys/mbuf.h> 31 #include <sys/kernel.h> 32 #include <sys/socket.h> 33 #include <sys/systm.h> 34 #include <sys/timeout.h> 35 #include <sys/conf.h> 36 #include <sys/device.h> 37 #include <sys/endian.h> 38 39 #include <machine/bus.h> 40 #include <machine/intr.h> 41 42 #if NBPFILTER > 0 43 #include <net/bpf.h> 44 #endif 45 #include <net/if.h> 46 #include <net/if_dl.h> 47 #include <net/if_media.h> 48 49 #include <netinet/in.h> 50 #include <netinet/if_ether.h> 51 52 #include <net80211/ieee80211_var.h> 53 #include <net80211/ieee80211_amrr.h> 54 #include <net80211/ieee80211_radiotap.h> 55 56 #include <dev/usb/usb.h> 57 #include <dev/usb/usbdi.h> 58 #include <dev/usb/usbdivar.h> 59 #include <dev/usb/usbdi_util.h> 60 #include <dev/usb/usbdevs.h> 61 62 #include <dev/ic/r92creg.h> 63 #include <dev/ic/rtwnvar.h> 64 65 /* Maximum number of output pipes is 3. */ 66 #define R92C_MAX_EPOUT 3 67 68 #define R92C_HQ_NPAGES 12 69 #define R92C_LQ_NPAGES 2 70 #define R92C_NQ_NPAGES 2 71 #define R92C_TXPKTBUF_COUNT 256 72 #define R92C_TX_PAGE_COUNT 248 73 #define R92C_TX_PAGE_BOUNDARY (R92C_TX_PAGE_COUNT + 1) 74 #define R92C_MAX_RX_DMA_SIZE 0x2800 75 76 #define R88E_HQ_NPAGES 0 77 #define R88E_LQ_NPAGES 9 78 #define R88E_NQ_NPAGES 0 79 #define R88E_TXPKTBUF_COUNT 177 80 #define R88E_TX_PAGE_COUNT 168 81 #define R88E_TX_PAGE_BOUNDARY (R88E_TX_PAGE_COUNT + 1) 82 #define R88E_MAX_RX_DMA_SIZE 0x2400 83 84 #define R92E_HQ_NPAGES 16 85 #define R92E_LQ_NPAGES 16 86 #define R92E_NQ_NPAGES 16 87 #define R92E_TX_PAGE_COUNT 248 88 #define R92E_TX_PAGE_BOUNDARY (R92E_TX_PAGE_COUNT + 1) 89 #define R92E_MAX_RX_DMA_SIZE 0x3fc0 90 91 #define R92C_TXDESC_SUMSIZE 32 92 #define R92C_TXDESC_SUMOFFSET 14 93 94 /* USB Requests. */ 95 #define R92C_REQ_REGS 0x05 96 97 /* 98 * Driver definitions. 99 */ 100 #define URTWN_RX_LIST_COUNT 1 101 #define URTWN_TX_LIST_COUNT 8 102 #define URTWN_HOST_CMD_RING_COUNT 32 103 104 #define URTWN_RXBUFSZ (16 * 1024) 105 #define URTWN_TXBUFSZ (sizeof(struct r92e_tx_desc_usb) + IEEE80211_MAX_LEN) 106 107 #define URTWN_RIDX_COUNT 28 108 109 #define URTWN_TX_TIMEOUT 5000 /* ms */ 110 111 #define URTWN_LED_LINK 0 112 #define URTWN_LED_DATA 1 113 114 struct urtwn_rx_radiotap_header { 115 struct ieee80211_radiotap_header wr_ihdr; 116 uint8_t wr_flags; 117 uint8_t wr_rate; 118 uint16_t wr_chan_freq; 119 uint16_t wr_chan_flags; 120 uint8_t wr_dbm_antsignal; 121 } __packed; 122 123 #define URTWN_RX_RADIOTAP_PRESENT \ 124 (1 << IEEE80211_RADIOTAP_FLAGS | \ 125 1 << IEEE80211_RADIOTAP_RATE | \ 126 1 << IEEE80211_RADIOTAP_CHANNEL | \ 127 1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) 128 129 struct urtwn_tx_radiotap_header { 130 struct ieee80211_radiotap_header wt_ihdr; 131 uint8_t wt_flags; 132 uint16_t wt_chan_freq; 133 uint16_t wt_chan_flags; 134 } __packed; 135 136 #define URTWN_TX_RADIOTAP_PRESENT \ 137 (1 << IEEE80211_RADIOTAP_FLAGS | \ 138 1 << IEEE80211_RADIOTAP_CHANNEL) 139 140 struct urtwn_softc; 141 142 struct urtwn_rx_data { 143 struct urtwn_softc *sc; 144 struct usbd_xfer *xfer; 145 uint8_t *buf; 146 }; 147 148 struct urtwn_tx_data { 149 struct urtwn_softc *sc; 150 struct usbd_pipe *pipe; 151 struct usbd_xfer *xfer; 152 uint8_t *buf; 153 TAILQ_ENTRY(urtwn_tx_data) next; 154 }; 155 156 struct urtwn_host_cmd { 157 void (*cb)(struct urtwn_softc *, void *); 158 uint8_t data[256]; 159 }; 160 161 struct urtwn_cmd_newstate { 162 enum ieee80211_state state; 163 int arg; 164 }; 165 166 struct urtwn_cmd_key { 167 struct ieee80211_key key; 168 struct ieee80211_node *ni; 169 }; 170 171 struct urtwn_host_cmd_ring { 172 struct urtwn_host_cmd cmd[URTWN_HOST_CMD_RING_COUNT]; 173 int cur; 174 int next; 175 int queued; 176 }; 177 178 struct urtwn_softc { 179 struct device sc_dev; 180 struct rtwn_softc sc_sc; 181 182 struct usbd_device *sc_udev; 183 struct usbd_interface *sc_iface; 184 struct usb_task sc_task; 185 186 struct timeout scan_to; 187 struct timeout calib_to; 188 189 int ntx; 190 struct usbd_pipe *rx_pipe; 191 struct usbd_pipe *tx_pipe[R92C_MAX_EPOUT]; 192 int ac2idx[EDCA_NUM_AC]; 193 194 struct urtwn_host_cmd_ring cmdq; 195 struct urtwn_rx_data rx_data[URTWN_RX_LIST_COUNT]; 196 struct urtwn_tx_data tx_data[URTWN_TX_LIST_COUNT]; 197 TAILQ_HEAD(, urtwn_tx_data) tx_free_list; 198 199 struct ieee80211_amrr amrr; 200 struct ieee80211_amrr_node amn; 201 202 #if NBPFILTER > 0 203 caddr_t sc_drvbpf; 204 205 union { 206 struct urtwn_rx_radiotap_header th; 207 uint8_t pad[64]; 208 } sc_rxtapu; 209 #define sc_rxtap sc_rxtapu.th 210 int sc_rxtap_len; 211 212 union { 213 struct urtwn_tx_radiotap_header th; 214 uint8_t pad[64]; 215 } sc_txtapu; 216 #define sc_txtap sc_txtapu.th 217 int sc_txtap_len; 218 #endif 219 int sc_key_tasks; 220 }; 221 222 #ifdef URTWN_DEBUG 223 #define DPRINTF(x) do { if (urtwn_debug) printf x; } while (0) 224 #define DPRINTFN(n, x) do { if (urtwn_debug >= (n)) printf x; } while (0) 225 int urtwn_debug = 4; 226 #else 227 #define DPRINTF(x) 228 #define DPRINTFN(n, x) 229 #endif 230 231 /* 232 * Various supported device vendors/products. 233 */ 234 #define URTWN_DEV(v, p, f) \ 235 { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, (f) | RTWN_CHIP_USB } 236 #define URTWN_DEV_8192CU(v, p) URTWN_DEV(v, p, RTWN_CHIP_92C | RTWN_CHIP_88C) 237 #define URTWN_DEV_8188EU(v, p) URTWN_DEV(v, p, RTWN_CHIP_88E) 238 #define URTWN_DEV_8192EU(v, p) URTWN_DEV(v, p, RTWN_CHIP_92E) 239 static const struct urtwn_type { 240 struct usb_devno dev; 241 uint32_t chip; 242 } urtwn_devs[] = { 243 URTWN_DEV_8192CU(ABOCOM, RTL8188CU_1), 244 URTWN_DEV_8192CU(ABOCOM, RTL8188CU_1), 245 URTWN_DEV_8192CU(ABOCOM, RTL8188CU_2), 246 URTWN_DEV_8192CU(ABOCOM, RTL8192CU), 247 URTWN_DEV_8192CU(ASUS, RTL8192CU), 248 URTWN_DEV_8192CU(ASUS, RTL8192CU_2), 249 URTWN_DEV_8192CU(ASUS, RTL8192CU_3), 250 URTWN_DEV_8192CU(AZUREWAVE, RTL8188CE_1), 251 URTWN_DEV_8192CU(AZUREWAVE, RTL8188CE_2), 252 URTWN_DEV_8192CU(AZUREWAVE, RTL8188CU), 253 URTWN_DEV_8192CU(BELKIN, F7D2102), 254 URTWN_DEV_8192CU(BELKIN, F9L1004V1), 255 URTWN_DEV_8192CU(BELKIN, RTL8188CU), 256 URTWN_DEV_8192CU(BELKIN, RTL8188CUS), 257 URTWN_DEV_8192CU(BELKIN, RTL8192CU), 258 URTWN_DEV_8192CU(BELKIN, RTL8192CU_1), 259 URTWN_DEV_8192CU(CHICONY, RTL8188CUS_1), 260 URTWN_DEV_8192CU(CHICONY, RTL8188CUS_2), 261 URTWN_DEV_8192CU(CHICONY, RTL8188CUS_3), 262 URTWN_DEV_8192CU(CHICONY, RTL8188CUS_4), 263 URTWN_DEV_8192CU(CHICONY, RTL8188CUS_5), 264 URTWN_DEV_8192CU(CHICONY, RTL8188CUS_6), 265 URTWN_DEV_8192CU(COMPARE, RTL8192CU), 266 URTWN_DEV_8192CU(COREGA, RTL8192CU), 267 URTWN_DEV_8192CU(DLINK, DWA131B), 268 URTWN_DEV_8192CU(DLINK, RTL8188CU), 269 URTWN_DEV_8192CU(DLINK, RTL8192CU_1), 270 URTWN_DEV_8192CU(DLINK, RTL8192CU_2), 271 URTWN_DEV_8192CU(DLINK, RTL8192CU_3), 272 URTWN_DEV_8192CU(DLINK, RTL8192CU_4), 273 URTWN_DEV_8192CU(EDIMAX, EW7811UN), 274 URTWN_DEV_8192CU(EDIMAX, RTL8192CU), 275 URTWN_DEV_8192CU(FEIXUN, RTL8188CU), 276 URTWN_DEV_8192CU(FEIXUN, RTL8192CU), 277 URTWN_DEV_8192CU(GUILLEMOT, HWNUP150), 278 URTWN_DEV_8192CU(GUILLEMOT, RTL8192CU), 279 URTWN_DEV_8192CU(HAWKING, RTL8192CU), 280 URTWN_DEV_8192CU(HAWKING, RTL8192CU_2), 281 URTWN_DEV_8192CU(HP3, RTL8188CU), 282 URTWN_DEV_8192CU(IODATA, WNG150UM), 283 URTWN_DEV_8192CU(IODATA, RTL8192CU), 284 URTWN_DEV_8192CU(NETGEAR, N300MA), 285 URTWN_DEV_8192CU(NETGEAR, WNA1000M), 286 URTWN_DEV_8192CU(NETGEAR, WNA1000MV2), 287 URTWN_DEV_8192CU(NETGEAR, RTL8192CU), 288 URTWN_DEV_8192CU(NETGEAR4, RTL8188CU), 289 URTWN_DEV_8192CU(NETWEEN, RTL8192CU), 290 URTWN_DEV_8192CU(NOVATECH, RTL8188CU), 291 URTWN_DEV_8192CU(PLANEX2, RTL8188CU_1), 292 URTWN_DEV_8192CU(PLANEX2, RTL8188CU_2), 293 URTWN_DEV_8192CU(PLANEX2, RTL8188CU_3), 294 URTWN_DEV_8192CU(PLANEX2, RTL8188CU_4), 295 URTWN_DEV_8192CU(PLANEX2, RTL8188CUS), 296 URTWN_DEV_8192CU(PLANEX2, RTL8192CU), 297 URTWN_DEV_8192CU(REALTEK, RTL8188CE_0), 298 URTWN_DEV_8192CU(REALTEK, RTL8188CE_1), 299 URTWN_DEV_8192CU(REALTEK, RTL8188CTV), 300 URTWN_DEV_8192CU(REALTEK, RTL8188CU_0), 301 URTWN_DEV_8192CU(REALTEK, RTL8188CU_1), 302 URTWN_DEV_8192CU(REALTEK, RTL8188CU_2), 303 URTWN_DEV_8192CU(REALTEK, RTL8188CU_3), 304 URTWN_DEV_8192CU(REALTEK, RTL8188CU_4), 305 URTWN_DEV_8192CU(REALTEK, RTL8188CU_5), 306 URTWN_DEV_8192CU(REALTEK, RTL8188CU_COMBO), 307 URTWN_DEV_8192CU(REALTEK, RTL8188CUS), 308 URTWN_DEV_8192CU(REALTEK, RTL8188RU), 309 URTWN_DEV_8192CU(REALTEK, RTL8188RU_2), 310 URTWN_DEV_8192CU(REALTEK, RTL8188RU_3), 311 URTWN_DEV_8192CU(REALTEK, RTL8191CU), 312 URTWN_DEV_8192CU(REALTEK, RTL8192CE), 313 URTWN_DEV_8192CU(REALTEK, RTL8192CE_VAU), 314 URTWN_DEV_8192CU(REALTEK, RTL8192CU), 315 URTWN_DEV_8192CU(SITECOMEU, RTL8188CU), 316 URTWN_DEV_8192CU(SITECOMEU, RTL8188CU_2), 317 URTWN_DEV_8192CU(SITECOMEU, RTL8192CU), 318 URTWN_DEV_8192CU(SITECOMEU, RTL8192CU_2), 319 URTWN_DEV_8192CU(SITECOMEU, WLA2100V2), 320 URTWN_DEV_8192CU(TPLINK, RTL8192CU), 321 URTWN_DEV_8192CU(TRENDNET, RTL8188CU), 322 URTWN_DEV_8192CU(TRENDNET, RTL8192CU), 323 URTWN_DEV_8192CU(ZYXEL, RTL8192CU), 324 /* URTWN_RTL8188E */ 325 URTWN_DEV_8188EU(ABOCOM, RTL8188EU), 326 URTWN_DEV_8188EU(DLINK, DWA121B1), 327 URTWN_DEV_8188EU(DLINK, DWA123D1), 328 URTWN_DEV_8188EU(DLINK, DWA125D1), 329 URTWN_DEV_8188EU(EDIMAX, EW7811UNV2), 330 URTWN_DEV_8188EU(ELECOM, WDC150SU2M), 331 URTWN_DEV_8188EU(REALTEK, RTL8188ETV), 332 URTWN_DEV_8188EU(REALTEK, RTL8188EU), 333 URTWN_DEV_8188EU(TPLINK, RTL8188EUS), 334 URTWN_DEV_8188EU(ASUS, RTL8188EUS), 335 336 /* URTWN_RTL8192EU */ 337 URTWN_DEV_8192EU(DLINK, DWA131E1), 338 URTWN_DEV_8192EU(REALTEK, RTL8192EU), 339 URTWN_DEV_8192EU(TPLINK, RTL8192EU), 340 URTWN_DEV_8192EU(TPLINK, RTL8192EU_2), 341 URTWN_DEV_8192EU(TPLINK, RTL8192EU_3) 342 }; 343 344 #define urtwn_lookup(v, p) \ 345 ((const struct urtwn_type *)usb_lookup(urtwn_devs, v, p)) 346 347 int urtwn_match(struct device *, void *, void *); 348 void urtwn_attach(struct device *, struct device *, void *); 349 int urtwn_detach(struct device *, int); 350 int urtwn_open_pipes(struct urtwn_softc *); 351 void urtwn_close_pipes(struct urtwn_softc *); 352 int urtwn_alloc_rx_list(struct urtwn_softc *); 353 void urtwn_free_rx_list(struct urtwn_softc *); 354 int urtwn_alloc_tx_list(struct urtwn_softc *); 355 void urtwn_free_tx_list(struct urtwn_softc *); 356 void urtwn_task(void *); 357 void urtwn_do_async(struct urtwn_softc *, 358 void (*)(struct urtwn_softc *, void *), void *, int); 359 void urtwn_wait_async(void *); 360 int urtwn_write_region_1(struct urtwn_softc *, uint16_t, uint8_t *, 361 int); 362 void urtwn_write_1(void *, uint16_t, uint8_t); 363 void urtwn_write_2(void *, uint16_t, uint16_t); 364 void urtwn_write_4(void *, uint16_t, uint32_t); 365 int urtwn_read_region_1(struct urtwn_softc *, uint16_t, uint8_t *, 366 int); 367 uint8_t urtwn_read_1(void *, uint16_t); 368 uint16_t urtwn_read_2(void *, uint16_t); 369 uint32_t urtwn_read_4(void *, uint16_t); 370 int urtwn_llt_write(struct urtwn_softc *, uint32_t, uint32_t); 371 void urtwn_calib_to(void *); 372 void urtwn_calib_cb(struct urtwn_softc *, void *); 373 void urtwn_scan_to(void *); 374 void urtwn_next_scan(void *); 375 void urtwn_cancel_scan(void *); 376 int urtwn_newstate(struct ieee80211com *, enum ieee80211_state, 377 int); 378 void urtwn_newstate_cb(struct urtwn_softc *, void *); 379 void urtwn_updateslot(struct ieee80211com *); 380 void urtwn_updateslot_cb(struct urtwn_softc *, void *); 381 void urtwn_updateedca(struct ieee80211com *); 382 void urtwn_updateedca_cb(struct urtwn_softc *, void *); 383 int urtwn_set_key(struct ieee80211com *, struct ieee80211_node *, 384 struct ieee80211_key *); 385 void urtwn_set_key_cb(struct urtwn_softc *, void *); 386 void urtwn_delete_key(struct ieee80211com *, 387 struct ieee80211_node *, struct ieee80211_key *); 388 void urtwn_delete_key_cb(struct urtwn_softc *, void *); 389 void urtwn_rx_frame(struct urtwn_softc *, uint8_t *, int, 390 struct mbuf_list *); 391 void urtwn_rxeof(struct usbd_xfer *, void *, 392 usbd_status); 393 void urtwn_txeof(struct usbd_xfer *, void *, 394 usbd_status); 395 int urtwn_tx(void *, struct mbuf *, struct ieee80211_node *); 396 int urtwn_ioctl(struct ifnet *, u_long, caddr_t); 397 int urtwn_power_on(void *); 398 int urtwn_alloc_buffers(void *); 399 int urtwn_r92c_power_on(struct urtwn_softc *); 400 int urtwn_r92e_power_on(struct urtwn_softc *); 401 int urtwn_r88e_power_on(struct urtwn_softc *); 402 int urtwn_llt_init(struct urtwn_softc *, int); 403 int urtwn_fw_loadpage(void *, int, uint8_t *, int); 404 int urtwn_load_firmware(void *, u_char **, size_t *); 405 int urtwn_dma_init(void *); 406 void urtwn_aggr_init(void *); 407 void urtwn_mac_init(void *); 408 void urtwn_bb_init(void *); 409 void urtwn_burstlen_init(struct urtwn_softc *); 410 int urtwn_init(void *); 411 void urtwn_stop(void *); 412 int urtwn_is_oactive(void *); 413 void urtwn_next_calib(void *); 414 void urtwn_cancel_calib(void *); 415 416 /* Aliases. */ 417 #define urtwn_bb_write urtwn_write_4 418 #define urtwn_bb_read urtwn_read_4 419 420 struct cfdriver urtwn_cd = { 421 NULL, "urtwn", DV_IFNET 422 }; 423 424 const struct cfattach urtwn_ca = { 425 sizeof(struct urtwn_softc), urtwn_match, urtwn_attach, urtwn_detach 426 }; 427 428 int 429 urtwn_match(struct device *parent, void *match, void *aux) 430 { 431 struct usb_attach_arg *uaa = aux; 432 433 if (uaa->iface == NULL || uaa->configno != 1) 434 return (UMATCH_NONE); 435 436 return ((urtwn_lookup(uaa->vendor, uaa->product) != NULL) ? 437 UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE); 438 } 439 440 void 441 urtwn_attach(struct device *parent, struct device *self, void *aux) 442 { 443 struct urtwn_softc *sc = (struct urtwn_softc *)self; 444 struct usb_attach_arg *uaa = aux; 445 struct ifnet *ifp; 446 struct ieee80211com *ic = &sc->sc_sc.sc_ic; 447 448 sc->sc_udev = uaa->device; 449 sc->sc_iface = uaa->iface; 450 451 sc->sc_sc.chip = urtwn_lookup(uaa->vendor, uaa->product)->chip; 452 453 usb_init_task(&sc->sc_task, urtwn_task, sc, USB_TASK_TYPE_GENERIC); 454 timeout_set(&sc->scan_to, urtwn_scan_to, sc); 455 timeout_set(&sc->calib_to, urtwn_calib_to, sc); 456 if (urtwn_open_pipes(sc) != 0) 457 return; 458 459 sc->amrr.amrr_min_success_threshold = 1; 460 sc->amrr.amrr_max_success_threshold = 10; 461 462 /* Attach the bus-agnostic driver. */ 463 sc->sc_sc.sc_ops.cookie = sc; 464 sc->sc_sc.sc_ops.write_1 = urtwn_write_1; 465 sc->sc_sc.sc_ops.write_2 = urtwn_write_2; 466 sc->sc_sc.sc_ops.write_4 = urtwn_write_4; 467 sc->sc_sc.sc_ops.read_1 = urtwn_read_1; 468 sc->sc_sc.sc_ops.read_2 = urtwn_read_2; 469 sc->sc_sc.sc_ops.read_4 = urtwn_read_4; 470 sc->sc_sc.sc_ops.tx = urtwn_tx; 471 sc->sc_sc.sc_ops.power_on = urtwn_power_on; 472 sc->sc_sc.sc_ops.dma_init = urtwn_dma_init; 473 sc->sc_sc.sc_ops.fw_loadpage = urtwn_fw_loadpage; 474 sc->sc_sc.sc_ops.load_firmware = urtwn_load_firmware; 475 sc->sc_sc.sc_ops.aggr_init = urtwn_aggr_init; 476 sc->sc_sc.sc_ops.mac_init = urtwn_mac_init; 477 sc->sc_sc.sc_ops.bb_init = urtwn_bb_init; 478 sc->sc_sc.sc_ops.alloc_buffers = urtwn_alloc_buffers; 479 sc->sc_sc.sc_ops.init = urtwn_init; 480 sc->sc_sc.sc_ops.stop = urtwn_stop; 481 sc->sc_sc.sc_ops.is_oactive = urtwn_is_oactive; 482 sc->sc_sc.sc_ops.next_calib = urtwn_next_calib; 483 sc->sc_sc.sc_ops.cancel_calib = urtwn_cancel_calib; 484 sc->sc_sc.sc_ops.next_scan = urtwn_next_scan; 485 sc->sc_sc.sc_ops.cancel_scan = urtwn_cancel_scan; 486 sc->sc_sc.sc_ops.wait_async = urtwn_wait_async; 487 if (rtwn_attach(&sc->sc_dev, &sc->sc_sc) != 0) { 488 urtwn_close_pipes(sc); 489 return; 490 } 491 492 /* ifp is now valid */ 493 ifp = &sc->sc_sc.sc_ic.ic_if; 494 ifp->if_ioctl = urtwn_ioctl; 495 496 ic->ic_updateslot = urtwn_updateslot; 497 ic->ic_updateedca = urtwn_updateedca; 498 ic->ic_set_key = urtwn_set_key; 499 ic->ic_delete_key = urtwn_delete_key; 500 /* Override state transition machine. */ 501 ic->ic_newstate = urtwn_newstate; 502 503 #if NBPFILTER > 0 504 bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO, 505 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN); 506 507 sc->sc_rxtap_len = sizeof(sc->sc_rxtapu); 508 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 509 sc->sc_rxtap.wr_ihdr.it_present = htole32(URTWN_RX_RADIOTAP_PRESENT); 510 511 sc->sc_txtap_len = sizeof(sc->sc_txtapu); 512 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 513 sc->sc_txtap.wt_ihdr.it_present = htole32(URTWN_TX_RADIOTAP_PRESENT); 514 #endif 515 } 516 517 int 518 urtwn_detach(struct device *self, int flags) 519 { 520 struct urtwn_softc *sc = (struct urtwn_softc *)self; 521 int s; 522 523 s = splusb(); 524 525 if (timeout_initialized(&sc->scan_to)) 526 timeout_del(&sc->scan_to); 527 if (timeout_initialized(&sc->calib_to)) 528 timeout_del(&sc->calib_to); 529 530 /* Wait for all async commands to complete. */ 531 usb_rem_wait_task(sc->sc_udev, &sc->sc_task); 532 533 usbd_ref_wait(sc->sc_udev); 534 535 rtwn_detach(&sc->sc_sc, flags); 536 537 /* Abort and close Tx/Rx pipes. */ 538 urtwn_close_pipes(sc); 539 540 /* Free Tx/Rx buffers. */ 541 urtwn_free_tx_list(sc); 542 urtwn_free_rx_list(sc); 543 splx(s); 544 545 return (0); 546 } 547 548 int 549 urtwn_open_pipes(struct urtwn_softc *sc) 550 { 551 /* Bulk-out endpoints addresses (from highest to lowest prio). */ 552 uint8_t epaddr[R92C_MAX_EPOUT] = { 0, 0, 0 }; 553 uint8_t rx_no; 554 usb_interface_descriptor_t *id; 555 usb_endpoint_descriptor_t *ed; 556 int i, error, nrx = 0; 557 558 /* Find all bulk endpoints. */ 559 id = usbd_get_interface_descriptor(sc->sc_iface); 560 for (i = 0; i < id->bNumEndpoints; i++) { 561 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 562 if (ed == NULL || UE_GET_XFERTYPE(ed->bmAttributes) != UE_BULK) 563 continue; 564 565 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) { 566 rx_no = ed->bEndpointAddress; 567 nrx++; 568 } else { 569 if (sc->ntx < R92C_MAX_EPOUT) 570 epaddr[sc->ntx] = ed->bEndpointAddress; 571 sc->ntx++; 572 } 573 } 574 if (nrx == 0) { 575 printf("%s: %d: invalid number of Rx bulk pipes\n", 576 sc->sc_dev.dv_xname, nrx); 577 return (EIO); 578 } 579 DPRINTF(("found %d bulk-out pipes\n", sc->ntx)); 580 if (sc->ntx == 0 || sc->ntx > R92C_MAX_EPOUT) { 581 printf("%s: %d: invalid number of Tx bulk pipes\n", 582 sc->sc_dev.dv_xname, sc->ntx); 583 return (EIO); 584 } 585 586 /* Open bulk-in pipe. */ 587 error = usbd_open_pipe(sc->sc_iface, rx_no, 0, &sc->rx_pipe); 588 if (error != 0) { 589 printf("%s: could not open Rx bulk pipe\n", 590 sc->sc_dev.dv_xname); 591 goto fail; 592 } 593 594 /* Open bulk-out pipes (up to 3). */ 595 for (i = 0; i < sc->ntx; i++) { 596 error = usbd_open_pipe(sc->sc_iface, epaddr[i], 0, 597 &sc->tx_pipe[i]); 598 if (error != 0) { 599 printf("%s: could not open Tx bulk pipe 0x%02x\n", 600 sc->sc_dev.dv_xname, epaddr[i]); 601 goto fail; 602 } 603 } 604 605 /* Map 802.11 access categories to USB pipes. */ 606 sc->ac2idx[EDCA_AC_BK] = 607 sc->ac2idx[EDCA_AC_BE] = (sc->ntx == 3) ? 2 : ((sc->ntx == 2) ? 1 : 0); 608 sc->ac2idx[EDCA_AC_VI] = (sc->ntx == 3) ? 1 : 0; 609 sc->ac2idx[EDCA_AC_VO] = 0; /* Always use highest prio. */ 610 611 if (error != 0) 612 fail: urtwn_close_pipes(sc); 613 return (error); 614 } 615 616 void 617 urtwn_close_pipes(struct urtwn_softc *sc) 618 { 619 int i; 620 621 /* Close Rx pipe. */ 622 if (sc->rx_pipe != NULL) 623 usbd_close_pipe(sc->rx_pipe); 624 /* Close Tx pipes. */ 625 for (i = 0; i < R92C_MAX_EPOUT; i++) { 626 if (sc->tx_pipe[i] == NULL) 627 continue; 628 usbd_close_pipe(sc->tx_pipe[i]); 629 } 630 } 631 632 int 633 urtwn_alloc_rx_list(struct urtwn_softc *sc) 634 { 635 struct urtwn_rx_data *data; 636 int i, error = 0; 637 638 for (i = 0; i < URTWN_RX_LIST_COUNT; i++) { 639 data = &sc->rx_data[i]; 640 641 data->sc = sc; /* Backpointer for callbacks. */ 642 643 data->xfer = usbd_alloc_xfer(sc->sc_udev); 644 if (data->xfer == NULL) { 645 printf("%s: could not allocate xfer\n", 646 sc->sc_dev.dv_xname); 647 error = ENOMEM; 648 break; 649 } 650 data->buf = usbd_alloc_buffer(data->xfer, URTWN_RXBUFSZ); 651 if (data->buf == NULL) { 652 printf("%s: could not allocate xfer buffer\n", 653 sc->sc_dev.dv_xname); 654 error = ENOMEM; 655 break; 656 } 657 } 658 if (error != 0) 659 urtwn_free_rx_list(sc); 660 return (error); 661 } 662 663 void 664 urtwn_free_rx_list(struct urtwn_softc *sc) 665 { 666 int i; 667 668 /* NB: Caller must abort pipe first. */ 669 for (i = 0; i < URTWN_RX_LIST_COUNT; i++) { 670 if (sc->rx_data[i].xfer != NULL) 671 usbd_free_xfer(sc->rx_data[i].xfer); 672 sc->rx_data[i].xfer = NULL; 673 } 674 } 675 676 int 677 urtwn_alloc_tx_list(struct urtwn_softc *sc) 678 { 679 struct urtwn_tx_data *data; 680 int i, error = 0; 681 682 TAILQ_INIT(&sc->tx_free_list); 683 for (i = 0; i < URTWN_TX_LIST_COUNT; i++) { 684 data = &sc->tx_data[i]; 685 686 data->sc = sc; /* Backpointer for callbacks. */ 687 688 data->xfer = usbd_alloc_xfer(sc->sc_udev); 689 if (data->xfer == NULL) { 690 printf("%s: could not allocate xfer\n", 691 sc->sc_dev.dv_xname); 692 error = ENOMEM; 693 break; 694 } 695 data->buf = usbd_alloc_buffer(data->xfer, URTWN_TXBUFSZ); 696 if (data->buf == NULL) { 697 printf("%s: could not allocate xfer buffer\n", 698 sc->sc_dev.dv_xname); 699 error = ENOMEM; 700 break; 701 } 702 /* Append this Tx buffer to our free list. */ 703 TAILQ_INSERT_TAIL(&sc->tx_free_list, data, next); 704 } 705 if (error != 0) 706 urtwn_free_tx_list(sc); 707 return (error); 708 } 709 710 void 711 urtwn_free_tx_list(struct urtwn_softc *sc) 712 { 713 int i; 714 715 /* NB: Caller must abort pipe first. */ 716 for (i = 0; i < URTWN_TX_LIST_COUNT; i++) { 717 if (sc->tx_data[i].xfer != NULL) 718 usbd_free_xfer(sc->tx_data[i].xfer); 719 sc->tx_data[i].xfer = NULL; 720 } 721 } 722 723 void 724 urtwn_task(void *arg) 725 { 726 struct urtwn_softc *sc = arg; 727 struct urtwn_host_cmd_ring *ring = &sc->cmdq; 728 struct urtwn_host_cmd *cmd; 729 int s; 730 731 /* Process host commands. */ 732 s = splusb(); 733 while (ring->next != ring->cur) { 734 cmd = &ring->cmd[ring->next]; 735 splx(s); 736 /* Invoke callback. */ 737 cmd->cb(sc, cmd->data); 738 s = splusb(); 739 ring->queued--; 740 ring->next = (ring->next + 1) % URTWN_HOST_CMD_RING_COUNT; 741 } 742 splx(s); 743 } 744 745 void 746 urtwn_do_async(struct urtwn_softc *sc, 747 void (*cb)(struct urtwn_softc *, void *), void *arg, int len) 748 { 749 struct urtwn_host_cmd_ring *ring = &sc->cmdq; 750 struct urtwn_host_cmd *cmd; 751 int s; 752 753 s = splusb(); 754 cmd = &ring->cmd[ring->cur]; 755 cmd->cb = cb; 756 KASSERT(len <= sizeof(cmd->data)); 757 memcpy(cmd->data, arg, len); 758 ring->cur = (ring->cur + 1) % URTWN_HOST_CMD_RING_COUNT; 759 760 /* If there is no pending command already, schedule a task. */ 761 if (++ring->queued == 1) 762 usb_add_task(sc->sc_udev, &sc->sc_task); 763 splx(s); 764 } 765 766 void 767 urtwn_wait_async(void *cookie) 768 { 769 struct urtwn_softc *sc = cookie; 770 int s; 771 772 s = splusb(); 773 /* Wait for all queued asynchronous commands to complete. */ 774 usb_wait_task(sc->sc_udev, &sc->sc_task); 775 splx(s); 776 } 777 778 int 779 urtwn_write_region_1(struct urtwn_softc *sc, uint16_t addr, uint8_t *buf, 780 int len) 781 { 782 usb_device_request_t req; 783 784 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 785 req.bRequest = R92C_REQ_REGS; 786 USETW(req.wValue, addr); 787 USETW(req.wIndex, 0); 788 USETW(req.wLength, len); 789 return (usbd_do_request(sc->sc_udev, &req, buf)); 790 } 791 792 void 793 urtwn_write_1(void *cookie, uint16_t addr, uint8_t val) 794 { 795 struct urtwn_softc *sc = cookie; 796 797 urtwn_write_region_1(sc, addr, &val, 1); 798 } 799 800 void 801 urtwn_write_2(void *cookie, uint16_t addr, uint16_t val) 802 { 803 struct urtwn_softc *sc = cookie; 804 805 val = htole16(val); 806 urtwn_write_region_1(sc, addr, (uint8_t *)&val, 2); 807 } 808 809 void 810 urtwn_write_4(void *cookie, uint16_t addr, uint32_t val) 811 { 812 struct urtwn_softc *sc = cookie; 813 814 val = htole32(val); 815 urtwn_write_region_1(sc, addr, (uint8_t *)&val, 4); 816 } 817 818 int 819 urtwn_read_region_1(struct urtwn_softc *sc, uint16_t addr, uint8_t *buf, 820 int len) 821 { 822 usb_device_request_t req; 823 824 req.bmRequestType = UT_READ_VENDOR_DEVICE; 825 req.bRequest = R92C_REQ_REGS; 826 USETW(req.wValue, addr); 827 USETW(req.wIndex, 0); 828 USETW(req.wLength, len); 829 return (usbd_do_request(sc->sc_udev, &req, buf)); 830 } 831 832 uint8_t 833 urtwn_read_1(void *cookie, uint16_t addr) 834 { 835 struct urtwn_softc *sc = cookie; 836 uint8_t val; 837 838 if (urtwn_read_region_1(sc, addr, &val, 1) != 0) 839 return (0xff); 840 return (val); 841 } 842 843 uint16_t 844 urtwn_read_2(void *cookie, uint16_t addr) 845 { 846 struct urtwn_softc *sc = cookie; 847 uint16_t val; 848 849 if (urtwn_read_region_1(sc, addr, (uint8_t *)&val, 2) != 0) 850 return (0xffff); 851 return (letoh16(val)); 852 } 853 854 uint32_t 855 urtwn_read_4(void *cookie, uint16_t addr) 856 { 857 struct urtwn_softc *sc = cookie; 858 uint32_t val; 859 860 if (urtwn_read_region_1(sc, addr, (uint8_t *)&val, 4) != 0) 861 return (0xffffffff); 862 return (letoh32(val)); 863 } 864 865 int 866 urtwn_llt_write(struct urtwn_softc *sc, uint32_t addr, uint32_t data) 867 { 868 int ntries; 869 870 urtwn_write_4(sc, R92C_LLT_INIT, 871 SM(R92C_LLT_INIT_OP, R92C_LLT_INIT_OP_WRITE) | 872 SM(R92C_LLT_INIT_ADDR, addr) | 873 SM(R92C_LLT_INIT_DATA, data)); 874 /* Wait for write operation to complete. */ 875 for (ntries = 0; ntries < 20; ntries++) { 876 if (MS(urtwn_read_4(sc, R92C_LLT_INIT), R92C_LLT_INIT_OP) == 877 R92C_LLT_INIT_OP_NO_ACTIVE) 878 return (0); 879 DELAY(5); 880 } 881 return (ETIMEDOUT); 882 } 883 884 void 885 urtwn_calib_to(void *arg) 886 { 887 struct urtwn_softc *sc = arg; 888 889 if (usbd_is_dying(sc->sc_udev)) 890 return; 891 892 usbd_ref_incr(sc->sc_udev); 893 894 /* Do it in a process context. */ 895 urtwn_do_async(sc, urtwn_calib_cb, NULL, 0); 896 897 usbd_ref_decr(sc->sc_udev); 898 } 899 900 /* ARGSUSED */ 901 void 902 urtwn_calib_cb(struct urtwn_softc *sc, void *arg) 903 { 904 struct ieee80211com *ic = &sc->sc_sc.sc_ic; 905 int s; 906 907 s = splnet(); 908 if (ic->ic_opmode == IEEE80211_M_STA) { 909 ieee80211_amrr_choose(&sc->amrr, ic->ic_bss, &sc->amn); 910 } 911 splx(s); 912 913 rtwn_calib(&sc->sc_sc); 914 } 915 916 void 917 urtwn_next_calib(void *cookie) 918 { 919 struct urtwn_softc *sc = cookie; 920 921 if (!usbd_is_dying(sc->sc_udev)) 922 timeout_add_sec(&sc->calib_to, 2); 923 } 924 925 void 926 urtwn_cancel_calib(void *cookie) 927 { 928 struct urtwn_softc *sc = cookie; 929 930 if (timeout_initialized(&sc->calib_to)) 931 timeout_del(&sc->calib_to); 932 } 933 934 void 935 urtwn_scan_to(void *arg) 936 { 937 struct urtwn_softc *sc = arg; 938 939 if (usbd_is_dying(sc->sc_udev)) 940 return; 941 942 usbd_ref_incr(sc->sc_udev); 943 rtwn_next_scan(&sc->sc_sc); 944 usbd_ref_decr(sc->sc_udev); 945 } 946 947 void 948 urtwn_next_scan(void *arg) 949 { 950 struct urtwn_softc *sc = arg; 951 952 if (!usbd_is_dying(sc->sc_udev)) 953 timeout_add_msec(&sc->scan_to, 200); 954 } 955 956 void 957 urtwn_cancel_scan(void *cookie) 958 { 959 struct urtwn_softc *sc = cookie; 960 961 if (timeout_initialized(&sc->scan_to)) 962 timeout_del(&sc->scan_to); 963 } 964 965 int 966 urtwn_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 967 { 968 struct rtwn_softc *sc_sc = ic->ic_softc; 969 struct device *self = sc_sc->sc_pdev; 970 struct urtwn_softc *sc = (struct urtwn_softc *)self; 971 struct urtwn_cmd_newstate cmd; 972 973 /* Do it in a process context. */ 974 cmd.state = nstate; 975 cmd.arg = arg; 976 urtwn_do_async(sc, urtwn_newstate_cb, &cmd, sizeof(cmd)); 977 return (0); 978 } 979 980 void 981 urtwn_newstate_cb(struct urtwn_softc *sc, void *arg) 982 { 983 struct urtwn_cmd_newstate *cmd = arg; 984 struct ieee80211com *ic = &sc->sc_sc.sc_ic; 985 986 rtwn_newstate(ic, cmd->state, cmd->arg); 987 } 988 989 void 990 urtwn_updateslot(struct ieee80211com *ic) 991 { 992 struct rtwn_softc *sc_sc = ic->ic_softc; 993 struct device *self = sc_sc->sc_pdev; 994 struct urtwn_softc *sc = (struct urtwn_softc *)self; 995 996 /* Do it in a process context. */ 997 urtwn_do_async(sc, urtwn_updateslot_cb, NULL, 0); 998 } 999 1000 /* ARGSUSED */ 1001 void 1002 urtwn_updateslot_cb(struct urtwn_softc *sc, void *arg) 1003 { 1004 struct ieee80211com *ic = &sc->sc_sc.sc_ic; 1005 1006 rtwn_updateslot(ic); 1007 } 1008 1009 void 1010 urtwn_updateedca(struct ieee80211com *ic) 1011 { 1012 struct rtwn_softc *sc_sc = ic->ic_softc; 1013 struct device *self = sc_sc->sc_pdev; 1014 struct urtwn_softc *sc = (struct urtwn_softc *)self; 1015 1016 /* Do it in a process context. */ 1017 urtwn_do_async(sc, urtwn_updateedca_cb, NULL, 0); 1018 } 1019 1020 /* ARGSUSED */ 1021 void 1022 urtwn_updateedca_cb(struct urtwn_softc *sc, void *arg) 1023 { 1024 struct ieee80211com *ic = &sc->sc_sc.sc_ic; 1025 1026 rtwn_updateedca(ic); 1027 } 1028 1029 int 1030 urtwn_set_key(struct ieee80211com *ic, struct ieee80211_node *ni, 1031 struct ieee80211_key *k) 1032 { 1033 struct rtwn_softc *sc_sc = ic->ic_softc; 1034 struct device *self = sc_sc->sc_pdev; 1035 struct urtwn_softc *sc = (struct urtwn_softc *)self; 1036 struct urtwn_cmd_key cmd; 1037 1038 /* Only handle keys for CCMP */ 1039 if (k->k_cipher != IEEE80211_CIPHER_CCMP) 1040 return ieee80211_set_key(ic, ni, k); 1041 1042 /* Defer setting of WEP keys until interface is brought up. */ 1043 if ((ic->ic_if.if_flags & (IFF_UP | IFF_RUNNING)) != 1044 (IFF_UP | IFF_RUNNING)) 1045 return (0); 1046 1047 /* Do it in a process context. */ 1048 cmd.key = *k; 1049 cmd.ni = ni; 1050 urtwn_do_async(sc, urtwn_set_key_cb, &cmd, sizeof(cmd)); 1051 sc->sc_key_tasks++; 1052 1053 return (EBUSY); 1054 } 1055 1056 void 1057 urtwn_set_key_cb(struct urtwn_softc *sc, void *arg) 1058 { 1059 struct ieee80211com *ic = &sc->sc_sc.sc_ic; 1060 struct urtwn_cmd_key *cmd = arg; 1061 1062 sc->sc_key_tasks--; 1063 1064 if (rtwn_set_key(ic, cmd->ni, &cmd->key) == 0) { 1065 if (sc->sc_key_tasks == 0) { 1066 DPRINTF(("marking port %s valid\n", 1067 ether_sprintf(cmd->ni->ni_macaddr))); 1068 cmd->ni->ni_port_valid = 1; 1069 ieee80211_set_link_state(ic, LINK_STATE_UP); 1070 } 1071 } else { 1072 IEEE80211_SEND_MGMT(ic, cmd->ni, IEEE80211_FC0_SUBTYPE_DEAUTH, 1073 IEEE80211_REASON_AUTH_LEAVE); 1074 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 1075 } 1076 } 1077 1078 void 1079 urtwn_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni, 1080 struct ieee80211_key *k) 1081 { 1082 struct rtwn_softc *sc_sc = ic->ic_softc; 1083 struct device *self = sc_sc->sc_pdev; 1084 struct urtwn_softc *sc = (struct urtwn_softc *)self; 1085 struct urtwn_cmd_key cmd; 1086 1087 /* Only handle keys for CCMP */ 1088 if (k->k_cipher != IEEE80211_CIPHER_CCMP) { 1089 ieee80211_delete_key(ic, ni, k); 1090 return; 1091 } 1092 1093 if (!(ic->ic_if.if_flags & IFF_RUNNING) || 1094 ic->ic_state != IEEE80211_S_RUN) 1095 return; /* Nothing to do. */ 1096 1097 /* Do it in a process context. */ 1098 cmd.key = *k; 1099 cmd.ni = ni; 1100 urtwn_do_async(sc, urtwn_delete_key_cb, &cmd, sizeof(cmd)); 1101 } 1102 1103 void 1104 urtwn_delete_key_cb(struct urtwn_softc *sc, void *arg) 1105 { 1106 struct ieee80211com *ic = &sc->sc_sc.sc_ic; 1107 struct urtwn_cmd_key *cmd = arg; 1108 1109 rtwn_delete_key(ic, cmd->ni, &cmd->key); 1110 } 1111 1112 int 1113 urtwn_ccmp_decap(struct urtwn_softc *sc, struct mbuf *m, 1114 struct ieee80211_node *ni) 1115 { 1116 struct ieee80211com *ic = &sc->sc_sc.sc_ic; 1117 struct ieee80211_key *k; 1118 struct ieee80211_frame *wh; 1119 uint64_t pn, *prsc; 1120 uint8_t *ivp; 1121 uint8_t tid; 1122 int hdrlen, hasqos; 1123 1124 k = ieee80211_get_rxkey(ic, m, ni); 1125 if (k == NULL) 1126 return 1; 1127 1128 wh = mtod(m, struct ieee80211_frame *); 1129 hdrlen = ieee80211_get_hdrlen(wh); 1130 ivp = (uint8_t *)wh + hdrlen; 1131 1132 /* Check that ExtIV bit is set. */ 1133 if (!(ivp[3] & IEEE80211_WEP_EXTIV)) 1134 return 1; 1135 1136 hasqos = ieee80211_has_qos(wh); 1137 tid = hasqos ? ieee80211_get_qos(wh) & IEEE80211_QOS_TID : 0; 1138 prsc = &k->k_rsc[tid]; 1139 1140 /* Extract the 48-bit PN from the CCMP header. */ 1141 pn = (uint64_t)ivp[0] | 1142 (uint64_t)ivp[1] << 8 | 1143 (uint64_t)ivp[4] << 16 | 1144 (uint64_t)ivp[5] << 24 | 1145 (uint64_t)ivp[6] << 32 | 1146 (uint64_t)ivp[7] << 40; 1147 if (pn <= *prsc) { 1148 ic->ic_stats.is_ccmp_replays++; 1149 return 1; 1150 } 1151 /* Last seen packet number is updated in ieee80211_inputm(). */ 1152 1153 /* Strip MIC. IV will be stripped by ieee80211_inputm(). */ 1154 m_adj(m, -IEEE80211_CCMP_MICLEN); 1155 return 0; 1156 } 1157 1158 void 1159 urtwn_rx_frame(struct urtwn_softc *sc, uint8_t *buf, int pktlen, 1160 struct mbuf_list *ml) 1161 { 1162 struct ieee80211com *ic = &sc->sc_sc.sc_ic; 1163 struct ifnet *ifp = &ic->ic_if; 1164 struct ieee80211_rxinfo rxi; 1165 struct ieee80211_frame *wh; 1166 struct ieee80211_node *ni; 1167 struct r92c_rx_desc_usb *rxd; 1168 uint32_t rxdw0, rxdw3; 1169 struct mbuf *m; 1170 uint8_t rate; 1171 int8_t rssi = 0; 1172 int s, infosz; 1173 1174 rxd = (struct r92c_rx_desc_usb *)buf; 1175 rxdw0 = letoh32(rxd->rxdw0); 1176 rxdw3 = letoh32(rxd->rxdw3); 1177 1178 if (__predict_false(rxdw0 & (R92C_RXDW0_CRCERR | R92C_RXDW0_ICVERR))) { 1179 /* 1180 * This should not happen since we setup our Rx filter 1181 * to not receive these frames. 1182 */ 1183 ifp->if_ierrors++; 1184 return; 1185 } 1186 if (__predict_false(pktlen < sizeof(*wh) || pktlen > MCLBYTES)) { 1187 ifp->if_ierrors++; 1188 return; 1189 } 1190 1191 rate = MS(rxdw3, R92C_RXDW3_RATE); 1192 infosz = MS(rxdw0, R92C_RXDW0_INFOSZ) * 8; 1193 1194 /* Get RSSI from PHY status descriptor if present. */ 1195 if (infosz != 0 && (rxdw0 & R92C_RXDW0_PHYST)) { 1196 rssi = rtwn_get_rssi(&sc->sc_sc, rate, &rxd[1]); 1197 /* Update our average RSSI. */ 1198 rtwn_update_avgrssi(&sc->sc_sc, rate, rssi); 1199 } 1200 1201 DPRINTFN(5, ("Rx frame len=%d rate=%d infosz=%d rssi=%d\n", 1202 pktlen, rate, infosz, rssi)); 1203 1204 MGETHDR(m, M_DONTWAIT, MT_DATA); 1205 if (__predict_false(m == NULL)) { 1206 ifp->if_ierrors++; 1207 return; 1208 } 1209 if (pktlen > MHLEN) { 1210 MCLGET(m, M_DONTWAIT); 1211 if (__predict_false(!(m->m_flags & M_EXT))) { 1212 ifp->if_ierrors++; 1213 m_freem(m); 1214 return; 1215 } 1216 } 1217 /* Finalize mbuf. */ 1218 wh = (struct ieee80211_frame *)((uint8_t *)&rxd[1] + infosz); 1219 memcpy(mtod(m, uint8_t *), wh, pktlen); 1220 m->m_pkthdr.len = m->m_len = pktlen; 1221 1222 s = splnet(); 1223 #if NBPFILTER > 0 1224 if (__predict_false(sc->sc_drvbpf != NULL)) { 1225 struct urtwn_rx_radiotap_header *tap = &sc->sc_rxtap; 1226 struct mbuf mb; 1227 1228 tap->wr_flags = 0; 1229 /* Map HW rate index to 802.11 rate. */ 1230 if (!(rxdw3 & R92C_RXDW3_HT)) { 1231 switch (rate) { 1232 /* CCK. */ 1233 case 0: tap->wr_rate = 2; break; 1234 case 1: tap->wr_rate = 4; break; 1235 case 2: tap->wr_rate = 11; break; 1236 case 3: tap->wr_rate = 22; break; 1237 /* OFDM. */ 1238 case 4: tap->wr_rate = 12; break; 1239 case 5: tap->wr_rate = 18; break; 1240 case 6: tap->wr_rate = 24; break; 1241 case 7: tap->wr_rate = 36; break; 1242 case 8: tap->wr_rate = 48; break; 1243 case 9: tap->wr_rate = 72; break; 1244 case 10: tap->wr_rate = 96; break; 1245 case 11: tap->wr_rate = 108; break; 1246 } 1247 if (rate <= 3) 1248 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 1249 } else if (rate >= 12) { /* MCS0~15. */ 1250 /* Bit 7 set means HT MCS instead of rate. */ 1251 tap->wr_rate = 0x80 | (rate - 12); 1252 } 1253 tap->wr_dbm_antsignal = rssi; 1254 tap->wr_chan_freq = htole16(ic->ic_ibss_chan->ic_freq); 1255 tap->wr_chan_flags = htole16(ic->ic_ibss_chan->ic_flags); 1256 1257 mb.m_data = (caddr_t)tap; 1258 mb.m_len = sc->sc_rxtap_len; 1259 mb.m_next = m; 1260 mb.m_nextpkt = NULL; 1261 mb.m_type = 0; 1262 mb.m_flags = 0; 1263 bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN); 1264 } 1265 #endif 1266 1267 ni = ieee80211_find_rxnode(ic, wh); 1268 memset(&rxi, 0, sizeof(rxi)); 1269 rxi.rxi_rssi = rssi; 1270 1271 /* Handle hardware decryption. */ 1272 if (((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) 1273 && (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) && 1274 (ni->ni_flags & IEEE80211_NODE_RXPROT) && 1275 ((!IEEE80211_IS_MULTICAST(wh->i_addr1) && 1276 ni->ni_pairwise_key.k_cipher == IEEE80211_CIPHER_CCMP) || 1277 (IEEE80211_IS_MULTICAST(wh->i_addr1) && 1278 ni->ni_rsngroupcipher == IEEE80211_CIPHER_CCMP))) { 1279 if (urtwn_ccmp_decap(sc, m, ni) != 0) { 1280 ifp->if_ierrors++; 1281 m_freem(m); 1282 ieee80211_release_node(ic, ni); 1283 return; 1284 } 1285 rxi.rxi_flags |= IEEE80211_RXI_HWDEC; 1286 } 1287 1288 ieee80211_inputm(ifp, m, ni, &rxi, ml); 1289 /* Node is no longer needed. */ 1290 ieee80211_release_node(ic, ni); 1291 splx(s); 1292 } 1293 1294 void 1295 urtwn_rxeof(struct usbd_xfer *xfer, void *priv, 1296 usbd_status status) 1297 { 1298 struct mbuf_list ml = MBUF_LIST_INITIALIZER(); 1299 struct urtwn_rx_data *data = priv; 1300 struct urtwn_softc *sc = data->sc; 1301 struct ieee80211com *ic = &sc->sc_sc.sc_ic; 1302 struct r92c_rx_desc_usb *rxd; 1303 uint32_t rxdw0; 1304 uint8_t *buf; 1305 int len, totlen, pktlen, infosz, npkts, error, align; 1306 1307 if (__predict_false(status != USBD_NORMAL_COMPLETION)) { 1308 DPRINTF(("RX status=%d\n", status)); 1309 if (status == USBD_STALLED) 1310 usbd_clear_endpoint_stall_async(sc->rx_pipe); 1311 if (status != USBD_CANCELLED) 1312 goto resubmit; 1313 return; 1314 } 1315 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 1316 1317 if (__predict_false(len < sizeof(*rxd))) { 1318 DPRINTF(("xfer too short %d\n", len)); 1319 goto resubmit; 1320 } 1321 buf = data->buf; 1322 1323 /* Get the number of encapsulated frames. */ 1324 rxd = (struct r92c_rx_desc_usb *)buf; 1325 npkts = MS(letoh32(rxd->rxdw2), R92C_RXDW2_PKTCNT); 1326 DPRINTFN(4, ("Rx %d frames in one chunk\n", npkts)); 1327 1328 if (sc->sc_sc.chip & RTWN_CHIP_88E) { 1329 int ntries, type; 1330 struct r88e_tx_rpt_ccx *rxstat; 1331 1332 type = MS(letoh32(rxd->rxdw3), R88E_RXDW3_RPT); 1333 1334 if (type == R88E_RXDW3_RPT_TX1) { 1335 buf += sizeof(struct r92c_rx_desc_usb); 1336 rxstat = (struct r88e_tx_rpt_ccx *)buf; 1337 ntries = MS(letoh32(rxstat->rptb2), 1338 R88E_RPTB2_RETRY_CNT); 1339 1340 if (rxstat->rptb1 & R88E_RPTB1_PKT_OK) 1341 sc->amn.amn_txcnt++; 1342 if (ntries > 0) 1343 sc->amn.amn_retrycnt++; 1344 1345 goto resubmit; 1346 } 1347 } else if (sc->sc_sc.chip & RTWN_CHIP_92E) { 1348 int type; 1349 struct r92e_c2h_tx_rpt *txrpt; 1350 1351 if (letoh32(rxd->rxdw2) & R92E_RXDW2_RPT_C2H) { 1352 if (len < sizeof(struct r92c_rx_desc_usb) + 2) 1353 goto resubmit; 1354 1355 type = buf[sizeof(struct r92c_rx_desc_usb)]; 1356 switch (type) { 1357 case R92C_C2HEVT_TX_REPORT: 1358 buf += sizeof(struct r92c_rx_desc_usb) + 2; 1359 txrpt = (struct r92e_c2h_tx_rpt *)buf; 1360 if (MS(txrpt->rptb2, R92E_RPTB2_RETRY_CNT) > 0) 1361 sc->amn.amn_retrycnt++; 1362 if ((txrpt->rptb0 & (R92E_RPTB0_RETRY_OVER | 1363 R92E_RPTB0_LIFE_EXPIRE)) == 0) 1364 sc->amn.amn_txcnt++; 1365 break; 1366 default: 1367 break; 1368 } 1369 goto resubmit; 1370 } 1371 } 1372 1373 align = (sc->sc_sc.chip & RTWN_CHIP_92E ? 7 : 127); 1374 1375 /* Process all of them. */ 1376 while (npkts-- > 0) { 1377 if (__predict_false(len < sizeof(*rxd))) 1378 break; 1379 rxd = (struct r92c_rx_desc_usb *)buf; 1380 rxdw0 = letoh32(rxd->rxdw0); 1381 1382 pktlen = MS(rxdw0, R92C_RXDW0_PKTLEN); 1383 if (__predict_false(pktlen == 0)) 1384 break; 1385 1386 infosz = MS(rxdw0, R92C_RXDW0_INFOSZ) * 8; 1387 1388 /* Make sure everything fits in xfer. */ 1389 totlen = sizeof(*rxd) + infosz + pktlen; 1390 if (__predict_false(totlen > len)) 1391 break; 1392 1393 /* Process 802.11 frame. */ 1394 urtwn_rx_frame(sc, buf, pktlen, &ml); 1395 1396 /* Handle chunk alignment. */ 1397 totlen = (totlen + align) & ~align; 1398 buf += totlen; 1399 len -= totlen; 1400 } 1401 if_input(&ic->ic_if, &ml); 1402 1403 resubmit: 1404 /* Setup a new transfer. */ 1405 usbd_setup_xfer(xfer, sc->rx_pipe, data, data->buf, URTWN_RXBUFSZ, 1406 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, urtwn_rxeof); 1407 error = usbd_transfer(data->xfer); 1408 if (error != 0 && error != USBD_IN_PROGRESS) 1409 DPRINTF(("could not set up new transfer: %d\n", error)); 1410 } 1411 1412 void 1413 urtwn_txeof(struct usbd_xfer *xfer, void *priv, 1414 usbd_status status) 1415 { 1416 struct urtwn_tx_data *data = priv; 1417 struct urtwn_softc *sc = data->sc; 1418 struct ifnet *ifp = &sc->sc_sc.sc_ic.ic_if; 1419 int s; 1420 1421 s = splnet(); 1422 /* Put this Tx buffer back to our free list. */ 1423 TAILQ_INSERT_TAIL(&sc->tx_free_list, data, next); 1424 1425 if (__predict_false(status != USBD_NORMAL_COMPLETION)) { 1426 DPRINTF(("TX status=%d\n", status)); 1427 if (status == USBD_STALLED) 1428 usbd_clear_endpoint_stall_async(data->pipe); 1429 ifp->if_oerrors++; 1430 splx(s); 1431 return; 1432 } 1433 sc->sc_sc.sc_tx_timer = 0; 1434 1435 /* We just released a Tx buffer, notify Tx. */ 1436 if (ifq_is_oactive(&ifp->if_snd)) { 1437 ifq_clr_oactive(&ifp->if_snd); 1438 rtwn_start(ifp); 1439 } 1440 splx(s); 1441 } 1442 1443 void 1444 urtwn_tx_fill_desc(struct urtwn_softc *sc, uint8_t **txdp, struct mbuf *m, 1445 struct ieee80211_frame *wh, struct ieee80211_key *k, 1446 struct ieee80211_node *ni) 1447 { 1448 struct r92c_tx_desc_usb *txd; 1449 struct ieee80211com *ic = &sc->sc_sc.sc_ic; 1450 uint8_t raid, type, rtsrate; 1451 uint32_t pktlen; 1452 1453 txd = (struct r92c_tx_desc_usb *)*txdp; 1454 (*txdp) += sizeof(*txd); 1455 memset(txd, 0, sizeof(*txd)); 1456 1457 pktlen = m->m_pkthdr.len; 1458 if (k != NULL && k->k_cipher == IEEE80211_CIPHER_CCMP) { 1459 txd->txdw1 |= htole32(SM(R92C_TXDW1_CIPHER, 1460 R92C_TXDW1_CIPHER_AES)); 1461 pktlen += IEEE80211_CCMP_HDRLEN; 1462 } 1463 1464 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 1465 1466 txd->txdw0 |= htole32( 1467 SM(R92C_TXDW0_PKTLEN, pktlen) | 1468 SM(R92C_TXDW0_OFFSET, sizeof(*txd)) | 1469 R92C_TXDW0_OWN | R92C_TXDW0_FSG | R92C_TXDW0_LSG); 1470 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1471 txd->txdw0 |= htole32(R92C_TXDW0_BMCAST); 1472 1473 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) && 1474 type == IEEE80211_FC0_TYPE_DATA) { 1475 if (ic->ic_curmode == IEEE80211_MODE_11B || 1476 (sc->sc_sc.sc_flags & RTWN_FLAG_FORCE_RAID_11B)) 1477 raid = R92C_RAID_11B; 1478 else 1479 raid = R92C_RAID_11BG; 1480 if (sc->sc_sc.chip & RTWN_CHIP_88E) { 1481 txd->txdw1 |= htole32( 1482 SM(R88E_TXDW1_MACID, R92C_MACID_BSS) | 1483 SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_BE) | 1484 SM(R92C_TXDW1_RAID, raid)); 1485 txd->txdw2 |= htole32(R88E_TXDW2_AGGBK); 1486 /* Request TX status report for AMRR */ 1487 txd->txdw2 |= htole32(R92C_TXDW2_CCX_RPT); 1488 } else { 1489 txd->txdw1 |= htole32( 1490 SM(R92C_TXDW1_MACID, R92C_MACID_BSS) | 1491 SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_BE) | 1492 SM(R92C_TXDW1_RAID, raid) | R92C_TXDW1_AGGBK); 1493 } 1494 1495 if (pktlen + IEEE80211_CRC_LEN > ic->ic_rtsthreshold) { 1496 txd->txdw4 |= htole32(R92C_TXDW4_RTSEN | 1497 R92C_TXDW4_HWRTSEN); 1498 } else if (ic->ic_flags & IEEE80211_F_USEPROT) { 1499 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) { 1500 txd->txdw4 |= htole32(R92C_TXDW4_CTS2SELF | 1501 R92C_TXDW4_HWRTSEN); 1502 } else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) { 1503 txd->txdw4 |= htole32(R92C_TXDW4_RTSEN | 1504 R92C_TXDW4_HWRTSEN); 1505 } 1506 } 1507 txd->txdw5 |= htole32(0x0001ff00); 1508 1509 if (ic->ic_curmode == IEEE80211_MODE_11B) 1510 rtsrate = 0; /* CCK1 */ 1511 else 1512 rtsrate = 8; /* OFDM24 */ 1513 1514 if (sc->sc_sc.chip & RTWN_CHIP_88E) { 1515 /* Use AMRR */ 1516 txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE); 1517 txd->txdw4 |= htole32(SM(R92C_TXDW4_RTSRATE, rtsrate)); 1518 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, 1519 ni->ni_txrate)); 1520 } else { 1521 /* Send data at OFDM54. */ 1522 txd->txdw4 |= htole32(SM(R92C_TXDW4_RTSRATE, rtsrate)); 1523 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, 11)); 1524 } 1525 } else { 1526 txd->txdw1 |= htole32( 1527 SM(R92C_TXDW1_MACID, 0) | 1528 SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT) | 1529 SM(R92C_TXDW1_RAID, R92C_RAID_11B)); 1530 1531 /* Force CCK1. */ 1532 txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE); 1533 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, 0)); 1534 } 1535 /* Set sequence number (already little endian). */ 1536 txd->txdseq |= (*(uint16_t *)wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT; 1537 1538 if (!ieee80211_has_qos(wh)) { 1539 /* Use HW sequence numbering for non-QoS frames. */ 1540 txd->txdw4 |= htole32(R92C_TXDW4_HWSEQ); 1541 txd->txdseq |= htole16(R92C_TXDW3_HWSEQEN); 1542 } else 1543 txd->txdw4 |= htole32(R92C_TXDW4_QOS); 1544 } 1545 1546 void 1547 urtwn_tx_fill_desc_gen2(struct urtwn_softc *sc, uint8_t **txdp, struct mbuf *m, 1548 struct ieee80211_frame *wh, struct ieee80211_key *k, 1549 struct ieee80211_node *ni) 1550 { 1551 struct r92e_tx_desc_usb *txd; 1552 struct ieee80211com *ic = &sc->sc_sc.sc_ic; 1553 uint8_t raid, type; 1554 uint32_t pktlen; 1555 1556 txd = (struct r92e_tx_desc_usb *)*txdp; 1557 (*txdp) += sizeof(*txd); 1558 memset(txd, 0, sizeof(*txd)); 1559 1560 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 1561 1562 pktlen = m->m_pkthdr.len; 1563 if (k != NULL && k->k_cipher == IEEE80211_CIPHER_CCMP) { 1564 txd->txdw1 |= htole32(SM(R92C_TXDW1_CIPHER, 1565 R92C_TXDW1_CIPHER_AES)); 1566 pktlen += IEEE80211_CCMP_HDRLEN; 1567 } 1568 1569 txd->txdw0 |= htole32( 1570 SM(R92C_TXDW0_PKTLEN, pktlen) | 1571 SM(R92C_TXDW0_OFFSET, sizeof(*txd)) | 1572 R92C_TXDW0_OWN | R92C_TXDW0_FSG | R92C_TXDW0_LSG); 1573 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1574 txd->txdw0 |= htole32(R92C_TXDW0_BMCAST); 1575 1576 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) && 1577 type == IEEE80211_FC0_TYPE_DATA) { 1578 if (ic->ic_curmode == IEEE80211_MODE_11B || 1579 (sc->sc_sc.sc_flags & RTWN_FLAG_FORCE_RAID_11B)) 1580 raid = R92E_RAID_11B; 1581 else 1582 raid = R92E_RAID_11BG; 1583 txd->txdw1 |= htole32( 1584 SM(R92E_TXDW1_MACID, R92C_MACID_BSS) | 1585 SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_BE) | 1586 SM(R92C_TXDW1_RAID, raid)); 1587 /* Request TX status report for AMRR */ 1588 txd->txdw2 |= htole32(R92C_TXDW2_CCX_RPT | R88E_TXDW2_AGGBK); 1589 1590 if (pktlen + IEEE80211_CRC_LEN > ic->ic_rtsthreshold) { 1591 txd->txdw4 |= htole32(R92C_TXDW4_RTSEN | 1592 R92C_TXDW4_HWRTSEN); 1593 } else if (ic->ic_flags & IEEE80211_F_USEPROT) { 1594 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) { 1595 txd->txdw4 |= htole32(R92C_TXDW4_CTS2SELF | 1596 R92C_TXDW4_HWRTSEN); 1597 } else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) { 1598 txd->txdw4 |= htole32(R92C_TXDW4_RTSEN | 1599 R92C_TXDW4_HWRTSEN); 1600 } 1601 } 1602 txd->txdw5 |= htole32(0x0001ff00); 1603 1604 /* Use AMRR */ 1605 txd->txdw3 |= htole32(R92E_TXDW3_DRVRATE); 1606 txd->txdw4 |= htole32(SM(R92E_TXDW4_RTSRATE, 8)); 1607 txd->txdw4 |= htole32(SM(R92E_TXDW4_DATARATE, ni->ni_txrate)); 1608 } else { 1609 txd->txdw1 |= htole32( 1610 SM(R92E_TXDW1_MACID, 0) | 1611 SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT) | 1612 SM(R92C_TXDW1_RAID, R92E_RAID_11B)); 1613 1614 /* Force CCK1. */ 1615 txd->txdw3 |= htole32(R92E_TXDW3_DRVRATE); 1616 txd->txdw4 |= htole32(SM(R92E_TXDW4_DATARATE, 0)); 1617 } 1618 txd->txdw4 |= htole32(SM(R92E_TXDW4_DATARATEFB, 0x1f)); 1619 1620 txd->txdseq2 |= htole16(SM(R92E_TXDSEQ2_HWSEQ, *(uint16_t *)wh->i_seq)); 1621 1622 if (!ieee80211_has_qos(wh)) { 1623 /* Use HW sequence numbering for non-QoS frames. */ 1624 txd->txdw7 |= htole16(R92C_TXDW3_HWSEQEN); 1625 } 1626 } 1627 1628 int 1629 urtwn_tx(void *cookie, struct mbuf *m, struct ieee80211_node *ni) 1630 { 1631 struct urtwn_softc *sc = cookie; 1632 struct ieee80211com *ic = &sc->sc_sc.sc_ic; 1633 struct ieee80211_frame *wh; 1634 struct ieee80211_key *k = NULL; 1635 struct urtwn_tx_data *data; 1636 struct usbd_pipe *pipe; 1637 uint16_t qos, sum; 1638 uint8_t tid, qid; 1639 int i, xferlen, error, headerlen; 1640 uint8_t *txdp; 1641 1642 wh = mtod(m, struct ieee80211_frame *); 1643 1644 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1645 k = ieee80211_get_txkey(ic, wh, ni); 1646 if (k->k_cipher != IEEE80211_CIPHER_CCMP) { 1647 if ((m = ieee80211_encrypt(ic, m, k)) == NULL) 1648 return (ENOBUFS); 1649 wh = mtod(m, struct ieee80211_frame *); 1650 } 1651 } 1652 1653 if (ieee80211_has_qos(wh)) { 1654 qos = ieee80211_get_qos(wh); 1655 tid = qos & IEEE80211_QOS_TID; 1656 qid = ieee80211_up_to_ac(ic, tid); 1657 } else if ((wh->i_fc[1] & IEEE80211_FC0_TYPE_MASK) 1658 != IEEE80211_FC0_TYPE_DATA) { 1659 /* Use AC VO for management frames. */ 1660 qid = EDCA_AC_VO; 1661 } else 1662 qid = EDCA_AC_BE; 1663 1664 /* Get the USB pipe to use for this AC. */ 1665 pipe = sc->tx_pipe[sc->ac2idx[qid]]; 1666 1667 /* Grab a Tx buffer from our free list. */ 1668 data = TAILQ_FIRST(&sc->tx_free_list); 1669 TAILQ_REMOVE(&sc->tx_free_list, data, next); 1670 1671 /* Fill Tx descriptor. */ 1672 txdp = data->buf; 1673 if (sc->sc_sc.chip & RTWN_CHIP_92E) 1674 urtwn_tx_fill_desc_gen2(sc, &txdp, m, wh, k, ni); 1675 else 1676 urtwn_tx_fill_desc(sc, &txdp, m, wh, k, ni); 1677 1678 /* Compute Tx descriptor checksum. */ 1679 sum = 0; 1680 for (i = 0; i < R92C_TXDESC_SUMSIZE / 2; i++) 1681 sum ^= ((uint16_t *)data->buf)[i]; 1682 ((uint16_t *)data->buf)[R92C_TXDESC_SUMOFFSET] = sum; 1683 1684 #if NBPFILTER > 0 1685 if (__predict_false(sc->sc_drvbpf != NULL)) { 1686 struct urtwn_tx_radiotap_header *tap = &sc->sc_txtap; 1687 struct mbuf mb; 1688 1689 tap->wt_flags = 0; 1690 tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq); 1691 tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags); 1692 1693 mb.m_data = (caddr_t)tap; 1694 mb.m_len = sc->sc_txtap_len; 1695 mb.m_next = m; 1696 mb.m_nextpkt = NULL; 1697 mb.m_type = 0; 1698 mb.m_flags = 0; 1699 bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT); 1700 } 1701 #endif 1702 1703 if (k != NULL && k->k_cipher == IEEE80211_CIPHER_CCMP) { 1704 xferlen = (txdp - data->buf) + m->m_pkthdr.len + 1705 IEEE80211_CCMP_HDRLEN; 1706 headerlen = ieee80211_get_hdrlen(wh); 1707 1708 m_copydata(m, 0, headerlen, txdp); 1709 txdp += headerlen; 1710 1711 k->k_tsc++; 1712 txdp[0] = k->k_tsc; 1713 txdp[1] = k->k_tsc >> 8; 1714 txdp[2] = 0; 1715 txdp[3] = k->k_id | IEEE80211_WEP_EXTIV; 1716 txdp[4] = k->k_tsc >> 16; 1717 txdp[5] = k->k_tsc >> 24; 1718 txdp[6] = k->k_tsc >> 32; 1719 txdp[7] = k->k_tsc >> 40; 1720 txdp += IEEE80211_CCMP_HDRLEN; 1721 1722 m_copydata(m, headerlen, m->m_pkthdr.len - headerlen, txdp); 1723 m_freem(m); 1724 } else { 1725 xferlen = (txdp - data->buf) + m->m_pkthdr.len; 1726 m_copydata(m, 0, m->m_pkthdr.len, txdp); 1727 m_freem(m); 1728 } 1729 1730 data->pipe = pipe; 1731 usbd_setup_xfer(data->xfer, pipe, data, data->buf, xferlen, 1732 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, URTWN_TX_TIMEOUT, 1733 urtwn_txeof); 1734 error = usbd_transfer(data->xfer); 1735 if (__predict_false(error != USBD_IN_PROGRESS && error != 0)) { 1736 /* Put this Tx buffer back to our free list. */ 1737 TAILQ_INSERT_TAIL(&sc->tx_free_list, data, next); 1738 return (error); 1739 } 1740 ieee80211_release_node(ic, ni); 1741 return (0); 1742 } 1743 1744 int 1745 urtwn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1746 { 1747 struct rtwn_softc *sc_sc = ifp->if_softc; 1748 struct device *self = sc_sc->sc_pdev; 1749 struct urtwn_softc *sc = (struct urtwn_softc *)self; 1750 int error; 1751 1752 if (usbd_is_dying(sc->sc_udev)) 1753 return ENXIO; 1754 1755 usbd_ref_incr(sc->sc_udev); 1756 error = rtwn_ioctl(ifp, cmd, data); 1757 usbd_ref_decr(sc->sc_udev); 1758 1759 return (error); 1760 } 1761 1762 int 1763 urtwn_r92c_power_on(struct urtwn_softc *sc) 1764 { 1765 uint32_t reg; 1766 int ntries; 1767 1768 /* Wait for autoload done bit. */ 1769 for (ntries = 0; ntries < 1000; ntries++) { 1770 if (urtwn_read_1(sc, R92C_APS_FSMCO) & R92C_APS_FSMCO_PFM_ALDN) 1771 break; 1772 DELAY(5); 1773 } 1774 if (ntries == 1000) { 1775 printf("%s: timeout waiting for chip autoload\n", 1776 sc->sc_dev.dv_xname); 1777 return (ETIMEDOUT); 1778 } 1779 1780 /* Unlock ISO/CLK/Power control register. */ 1781 urtwn_write_1(sc, R92C_RSV_CTRL, 0); 1782 /* Move SPS into PWM mode. */ 1783 urtwn_write_1(sc, R92C_SPS0_CTRL, 0x2b); 1784 DELAY(100); 1785 1786 reg = urtwn_read_1(sc, R92C_LDOV12D_CTRL); 1787 if (!(reg & R92C_LDOV12D_CTRL_LDV12_EN)) { 1788 urtwn_write_1(sc, R92C_LDOV12D_CTRL, 1789 reg | R92C_LDOV12D_CTRL_LDV12_EN); 1790 DELAY(100); 1791 urtwn_write_1(sc, R92C_SYS_ISO_CTRL, 1792 urtwn_read_1(sc, R92C_SYS_ISO_CTRL) & 1793 ~R92C_SYS_ISO_CTRL_MD2PP); 1794 } 1795 1796 /* Auto enable WLAN. */ 1797 urtwn_write_2(sc, R92C_APS_FSMCO, 1798 urtwn_read_2(sc, R92C_APS_FSMCO) | R92C_APS_FSMCO_APFM_ONMAC); 1799 for (ntries = 0; ntries < 1000; ntries++) { 1800 if (!(urtwn_read_2(sc, R92C_APS_FSMCO) & 1801 R92C_APS_FSMCO_APFM_ONMAC)) 1802 break; 1803 DELAY(5); 1804 } 1805 if (ntries == 1000) { 1806 printf("%s: timeout waiting for MAC auto ON\n", 1807 sc->sc_dev.dv_xname); 1808 return (ETIMEDOUT); 1809 } 1810 1811 /* Enable radio, GPIO and LED functions. */ 1812 urtwn_write_2(sc, R92C_APS_FSMCO, 1813 R92C_APS_FSMCO_AFSM_HSUS | 1814 R92C_APS_FSMCO_PDN_EN | 1815 R92C_APS_FSMCO_PFM_ALDN); 1816 /* Release RF digital isolation. */ 1817 urtwn_write_2(sc, R92C_SYS_ISO_CTRL, 1818 urtwn_read_2(sc, R92C_SYS_ISO_CTRL) & ~R92C_SYS_ISO_CTRL_DIOR); 1819 1820 /* Enable MAC DMA/WMAC/SCHEDULE/SEC blocks. */ 1821 reg = urtwn_read_2(sc, R92C_CR); 1822 reg |= R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN | 1823 R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN | 1824 R92C_CR_SCHEDULE_EN | R92C_CR_MACTXEN | R92C_CR_MACRXEN | 1825 R92C_CR_ENSEC; 1826 urtwn_write_2(sc, R92C_CR, reg); 1827 1828 urtwn_write_1(sc, 0xfe10, 0x19); 1829 return (0); 1830 } 1831 1832 int 1833 urtwn_r92e_power_on(struct urtwn_softc *sc) 1834 { 1835 uint32_t reg; 1836 int ntries; 1837 1838 if (urtwn_read_4(sc, R92C_SYS_CFG) & R92E_SYS_CFG_SPSLDO_SEL) { 1839 /* LDO. */ 1840 urtwn_write_1(sc, R92E_LDO_SWR_CTRL, 0xc3); 1841 } else { 1842 reg = urtwn_read_4(sc, R92C_SYS_SWR_CTRL2); 1843 reg &= 0xff0fffff; 1844 reg |= 0x00500000; 1845 urtwn_write_4(sc, R92C_SYS_SWR_CTRL2, reg); 1846 urtwn_write_1(sc, R92E_LDO_SWR_CTRL, 0x83); 1847 } 1848 1849 /* 40MHz crystal source */ 1850 urtwn_write_1(sc, R92C_AFE_PLL_CTRL, 1851 urtwn_read_1(sc, R92C_AFE_PLL_CTRL) & 0xfb); 1852 urtwn_write_4(sc, R92C_AFE_XTAL_CTRL_EXT, 1853 urtwn_read_4(sc, R92C_AFE_XTAL_CTRL_EXT) & 0xfffffc7f); 1854 1855 urtwn_write_1(sc, R92C_AFE_PLL_CTRL, 1856 urtwn_read_1(sc, R92C_AFE_PLL_CTRL) & 0xbf); 1857 urtwn_write_4(sc, R92C_AFE_XTAL_CTRL_EXT, 1858 urtwn_read_4(sc, R92C_AFE_XTAL_CTRL_EXT) & 0xffdfffff); 1859 1860 /* Disable HWPDN. */ 1861 urtwn_write_2(sc, R92C_APS_FSMCO, 1862 urtwn_read_2(sc, R92C_APS_FSMCO) & ~R92C_APS_FSMCO_APDM_HPDN); 1863 for (ntries = 0; ntries < 5000; ntries++) { 1864 if (urtwn_read_4(sc, R92C_APS_FSMCO) & R92C_APS_FSMCO_SUS_HOST) 1865 break; 1866 DELAY(10); 1867 } 1868 if (ntries == 5000) { 1869 printf("%s: timeout waiting for chip power up\n", 1870 sc->sc_dev.dv_xname); 1871 return (ETIMEDOUT); 1872 } 1873 1874 /* Disable WL suspend. */ 1875 urtwn_write_2(sc, R92C_APS_FSMCO, 1876 urtwn_read_2(sc, R92C_APS_FSMCO) & 1877 ~(R92C_APS_FSMCO_AFSM_HSUS | R92C_APS_FSMCO_AFSM_PCIE)); 1878 1879 /* Auto enable WLAN. */ 1880 urtwn_write_4(sc, R92C_APS_FSMCO, 1881 urtwn_read_4(sc, R92C_APS_FSMCO) | R92C_APS_FSMCO_RDY_MACON); 1882 urtwn_write_2(sc, R92C_APS_FSMCO, 1883 urtwn_read_2(sc, R92C_APS_FSMCO) | R92C_APS_FSMCO_APFM_ONMAC); 1884 for (ntries = 0; ntries < 5000; ntries++) { 1885 if (!(urtwn_read_2(sc, R92C_APS_FSMCO) & 1886 R92C_APS_FSMCO_APFM_ONMAC)) 1887 break; 1888 DELAY(10); 1889 } 1890 if (ntries == 5000) { 1891 printf("%s: timeout waiting for MAC auto ON\n", 1892 sc->sc_dev.dv_xname); 1893 return (ETIMEDOUT); 1894 } 1895 1896 /* Enable MAC DMA/WMAC/SCHEDULE/SEC blocks. */ 1897 urtwn_write_2(sc, R92C_CR, 0); 1898 reg = urtwn_read_2(sc, R92C_CR); 1899 reg |= R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN | 1900 R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN | 1901 R92C_CR_SCHEDULE_EN | R92C_CR_ENSEC | R92C_CR_CALTMR_EN; 1902 urtwn_write_2(sc, R92C_CR, reg); 1903 return (0); 1904 } 1905 1906 int 1907 urtwn_r88e_power_on(struct urtwn_softc *sc) 1908 { 1909 uint32_t reg; 1910 int ntries; 1911 1912 /* Wait for power ready bit. */ 1913 for (ntries = 0; ntries < 5000; ntries++) { 1914 if (urtwn_read_4(sc, R92C_APS_FSMCO) & R92C_APS_FSMCO_SUS_HOST) 1915 break; 1916 DELAY(10); 1917 } 1918 if (ntries == 5000) { 1919 printf("%s: timeout waiting for chip power up\n", 1920 sc->sc_dev.dv_xname); 1921 return (ETIMEDOUT); 1922 } 1923 1924 /* Reset BB. */ 1925 urtwn_write_1(sc, R92C_SYS_FUNC_EN, 1926 urtwn_read_1(sc, R92C_SYS_FUNC_EN) & ~(R92C_SYS_FUNC_EN_BBRSTB | 1927 R92C_SYS_FUNC_EN_BB_GLB_RST)); 1928 1929 urtwn_write_1(sc, R92C_AFE_XTAL_CTRL + 2, 1930 urtwn_read_1(sc, R92C_AFE_XTAL_CTRL + 2) | 0x80); 1931 1932 /* Disable HWPDN. */ 1933 urtwn_write_2(sc, R92C_APS_FSMCO, 1934 urtwn_read_2(sc, R92C_APS_FSMCO) & ~R92C_APS_FSMCO_APDM_HPDN); 1935 /* Disable WL suspend. */ 1936 urtwn_write_2(sc, R92C_APS_FSMCO, 1937 urtwn_read_2(sc, R92C_APS_FSMCO) & 1938 ~(R92C_APS_FSMCO_AFSM_HSUS | R92C_APS_FSMCO_AFSM_PCIE)); 1939 1940 /* Auto enable WLAN. */ 1941 urtwn_write_2(sc, R92C_APS_FSMCO, 1942 urtwn_read_2(sc, R92C_APS_FSMCO) | R92C_APS_FSMCO_APFM_ONMAC); 1943 for (ntries = 0; ntries < 5000; ntries++) { 1944 if (!(urtwn_read_2(sc, R92C_APS_FSMCO) & 1945 R92C_APS_FSMCO_APFM_ONMAC)) 1946 break; 1947 DELAY(10); 1948 } 1949 if (ntries == 5000) { 1950 printf("%s: timeout waiting for MAC auto ON\n", 1951 sc->sc_dev.dv_xname); 1952 return (ETIMEDOUT); 1953 } 1954 1955 /* Enable LDO normal mode. */ 1956 urtwn_write_1(sc, R92C_LPLDO_CTRL, 1957 urtwn_read_1(sc, R92C_LPLDO_CTRL) & ~0x10); 1958 1959 /* Enable MAC DMA/WMAC/SCHEDULE/SEC blocks. */ 1960 urtwn_write_2(sc, R92C_CR, 0); 1961 reg = urtwn_read_2(sc, R92C_CR); 1962 reg |= R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN | 1963 R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN | 1964 R92C_CR_SCHEDULE_EN | R92C_CR_ENSEC | R92C_CR_CALTMR_EN; 1965 urtwn_write_2(sc, R92C_CR, reg); 1966 return (0); 1967 } 1968 1969 int 1970 urtwn_llt_init(struct urtwn_softc *sc, int page_count) 1971 { 1972 int i, error, pktbuf_count; 1973 1974 pktbuf_count = (sc->sc_sc.chip & RTWN_CHIP_88E) ? 1975 R88E_TXPKTBUF_COUNT : R92C_TXPKTBUF_COUNT; 1976 1977 /* Reserve pages [0; page_count]. */ 1978 for (i = 0; i < page_count; i++) { 1979 if ((error = urtwn_llt_write(sc, i, i + 1)) != 0) 1980 return (error); 1981 } 1982 /* NB: 0xff indicates end-of-list. */ 1983 if ((error = urtwn_llt_write(sc, i, 0xff)) != 0) 1984 return (error); 1985 /* 1986 * Use pages [page_count + 1; pktbuf_count - 1] 1987 * as ring buffer. 1988 */ 1989 for (++i; i < pktbuf_count - 1; i++) { 1990 if ((error = urtwn_llt_write(sc, i, i + 1)) != 0) 1991 return (error); 1992 } 1993 /* Make the last page point to the beginning of the ring buffer. */ 1994 error = urtwn_llt_write(sc, i, page_count + 1); 1995 return (error); 1996 } 1997 1998 int 1999 urtwn_auto_llt_init(struct urtwn_softc *sc) 2000 { 2001 int ntries; 2002 2003 urtwn_write_4(sc, R92E_AUTO_LLT, urtwn_read_4(sc, 2004 R92E_AUTO_LLT) | R92E_AUTO_LLT_EN); 2005 for (ntries = 0; ntries < 1000; ntries++) { 2006 if (!(urtwn_read_4(sc, R92E_AUTO_LLT) & R92E_AUTO_LLT_EN)) 2007 return (0); 2008 DELAY(2); 2009 } 2010 2011 return (ETIMEDOUT); 2012 } 2013 2014 int 2015 urtwn_fw_loadpage(void *cookie, int page, uint8_t *buf, int len) 2016 { 2017 struct urtwn_softc *sc = cookie; 2018 uint32_t reg; 2019 int off, mlen, error = 0; 2020 2021 reg = urtwn_read_4(sc, R92C_MCUFWDL); 2022 reg = RW(reg, R92C_MCUFWDL_PAGE, page); 2023 urtwn_write_4(sc, R92C_MCUFWDL, reg); 2024 2025 off = R92C_FW_START_ADDR; 2026 while (len > 0) { 2027 if (len > 196) 2028 mlen = 196; 2029 else if (len > 4) 2030 mlen = 4; 2031 else 2032 mlen = 1; 2033 error = urtwn_write_region_1(sc, off, buf, mlen); 2034 if (error != 0) 2035 break; 2036 off += mlen; 2037 buf += mlen; 2038 len -= mlen; 2039 } 2040 return (error); 2041 } 2042 2043 int 2044 urtwn_load_firmware(void *cookie, u_char **fw, size_t *len) 2045 { 2046 struct urtwn_softc *sc = cookie; 2047 const char *name; 2048 int error; 2049 2050 if (sc->sc_sc.chip & RTWN_CHIP_92E) 2051 name = "urtwn-rtl8192eu"; 2052 else if (sc->sc_sc.chip & RTWN_CHIP_88E) 2053 name = "urtwn-rtl8188eu"; 2054 else if ((sc->sc_sc.chip & (RTWN_CHIP_UMC_A_CUT | RTWN_CHIP_92C)) == 2055 RTWN_CHIP_UMC_A_CUT) 2056 name = "urtwn-rtl8192cU"; 2057 else 2058 name = "urtwn-rtl8192cT"; 2059 2060 error = loadfirmware(name, fw, len); 2061 if (error) 2062 printf("%s: could not read firmware %s (error %d)\n", 2063 sc->sc_dev.dv_xname, name, error); 2064 return (error); 2065 } 2066 2067 int 2068 urtwn_dma_init(void *cookie) 2069 { 2070 struct urtwn_softc *sc = cookie; 2071 uint32_t reg; 2072 uint16_t dmasize; 2073 int hqpages, lqpages, nqpages, pagecnt, boundary; 2074 int error, hashq, haslq, hasnq; 2075 2076 /* Default initialization of chipset values. */ 2077 if (sc->sc_sc.chip & RTWN_CHIP_88E) { 2078 hqpages = R88E_HQ_NPAGES; 2079 lqpages = R88E_LQ_NPAGES; 2080 nqpages = R88E_NQ_NPAGES; 2081 pagecnt = R88E_TX_PAGE_COUNT; 2082 boundary = R88E_TX_PAGE_BOUNDARY; 2083 dmasize = R88E_MAX_RX_DMA_SIZE; 2084 } else if (sc->sc_sc.chip & RTWN_CHIP_92E) { 2085 hqpages = R92E_HQ_NPAGES; 2086 lqpages = R92E_LQ_NPAGES; 2087 nqpages = R92E_NQ_NPAGES; 2088 pagecnt = R92E_TX_PAGE_COUNT; 2089 boundary = R92E_TX_PAGE_BOUNDARY; 2090 dmasize = R92E_MAX_RX_DMA_SIZE; 2091 } else { 2092 hqpages = R92C_HQ_NPAGES; 2093 lqpages = R92C_LQ_NPAGES; 2094 nqpages = R92C_NQ_NPAGES; 2095 pagecnt = R92C_TX_PAGE_COUNT; 2096 boundary = R92C_TX_PAGE_BOUNDARY; 2097 dmasize = R92C_MAX_RX_DMA_SIZE; 2098 } 2099 2100 /* Initialize LLT table. */ 2101 if (sc->sc_sc.chip & RTWN_CHIP_92E) { 2102 error = urtwn_auto_llt_init(sc); 2103 } else { 2104 error = urtwn_llt_init(sc, pagecnt); 2105 } 2106 if (error != 0) 2107 return (error); 2108 2109 /* Get Tx queues to USB endpoints mapping. */ 2110 hashq = hasnq = haslq = 0; 2111 switch (sc->ntx) { 2112 case 3: 2113 haslq = 1; 2114 pagecnt -= lqpages; 2115 /* FALLTHROUGH */ 2116 case 2: 2117 hasnq = 1; 2118 pagecnt -= nqpages; 2119 /* FALLTHROUGH */ 2120 case 1: 2121 hashq = 1; 2122 pagecnt -= hqpages; 2123 break; 2124 } 2125 2126 /* Set number of pages for normal priority queue. */ 2127 urtwn_write_1(sc, R92C_RQPN_NPQ, hasnq ? nqpages : 0); 2128 urtwn_write_4(sc, R92C_RQPN, 2129 /* Set number of pages for public queue. */ 2130 SM(R92C_RQPN_PUBQ, pagecnt) | 2131 /* Set number of pages for high priority queue. */ 2132 SM(R92C_RQPN_HPQ, hashq ? hqpages : 0) | 2133 /* Set number of pages for low priority queue. */ 2134 SM(R92C_RQPN_LPQ, haslq ? lqpages : 0) | 2135 /* Load values. */ 2136 R92C_RQPN_LD); 2137 2138 urtwn_write_1(sc, R92C_TXPKTBUF_BCNQ_BDNY, boundary); 2139 urtwn_write_1(sc, R92C_TXPKTBUF_MGQ_BDNY, boundary); 2140 urtwn_write_1(sc, R92C_TXPKTBUF_WMAC_LBK_BF_HD, boundary); 2141 urtwn_write_1(sc, R92C_TRXFF_BNDY, boundary); 2142 urtwn_write_1(sc, R92C_TDECTRL + 1, boundary); 2143 2144 /* Set queue to USB pipe mapping. */ 2145 reg = urtwn_read_2(sc, R92C_TRXDMA_CTRL); 2146 reg &= ~R92C_TRXDMA_CTRL_QMAP_M; 2147 if (haslq) 2148 reg |= R92C_TRXDMA_CTRL_QMAP_3EP; 2149 else if (hashq) { 2150 if (!hasnq) 2151 reg |= R92C_TRXDMA_CTRL_QMAP_HQ; 2152 else 2153 reg |= R92C_TRXDMA_CTRL_QMAP_HQ_NQ; 2154 } 2155 urtwn_write_2(sc, R92C_TRXDMA_CTRL, reg); 2156 2157 /* Set Tx/Rx transfer page boundary. */ 2158 urtwn_write_2(sc, R92C_TRXFF_BNDY + 2, dmasize - 1); 2159 2160 if (!(sc->sc_sc.chip & RTWN_CHIP_92E)) { 2161 /* Set Tx/Rx transfer page size. */ 2162 urtwn_write_1(sc, R92C_PBP, 2163 SM(R92C_PBP_PSRX, R92C_PBP_128) | 2164 SM(R92C_PBP_PSTX, R92C_PBP_128)); 2165 } 2166 return (error); 2167 } 2168 2169 void 2170 urtwn_aggr_init(void *cookie) 2171 { 2172 struct urtwn_softc *sc = cookie; 2173 uint32_t reg = 0; 2174 int dmasize, dmatiming, ndesc; 2175 2176 /* Set burst packet length. */ 2177 if (sc->sc_sc.chip & RTWN_CHIP_92E) 2178 urtwn_burstlen_init(sc); 2179 2180 if (sc->sc_sc.chip & RTWN_CHIP_92E) { 2181 dmasize = 6; 2182 dmatiming = 32; 2183 ndesc = 3; 2184 } else { 2185 dmasize = 48; 2186 dmatiming = 4; 2187 ndesc = (sc->sc_sc.chip & RTWN_CHIP_88E) ? 1 : 6; 2188 } 2189 2190 /* Tx aggregation setting. */ 2191 if (sc->sc_sc.chip & RTWN_CHIP_92E) { 2192 urtwn_write_1(sc, R92E_DWBCN1_CTRL, ndesc << 1); 2193 } else { 2194 reg = urtwn_read_4(sc, R92C_TDECTRL); 2195 reg = RW(reg, R92C_TDECTRL_BLK_DESC_NUM, ndesc); 2196 urtwn_write_4(sc, R92C_TDECTRL, reg); 2197 } 2198 2199 /* Rx aggregation setting. */ 2200 if (!(sc->sc_sc.chip & RTWN_CHIP_92E)) { 2201 urtwn_write_1(sc, R92C_TRXDMA_CTRL, 2202 urtwn_read_1(sc, R92C_TRXDMA_CTRL) | 2203 R92C_TRXDMA_CTRL_RXDMA_AGG_EN); 2204 } 2205 2206 urtwn_write_1(sc, R92C_RXDMA_AGG_PG_TH, dmasize); 2207 if (sc->sc_sc.chip & (RTWN_CHIP_92C | RTWN_CHIP_88C)) 2208 urtwn_write_1(sc, R92C_USB_DMA_AGG_TO, dmatiming); 2209 else 2210 urtwn_write_1(sc, R92C_RXDMA_AGG_PG_TH + 1, dmatiming); 2211 2212 /* Drop incorrect bulk out. */ 2213 urtwn_write_4(sc, R92C_TXDMA_OFFSET_CHK, 2214 urtwn_read_4(sc, R92C_TXDMA_OFFSET_CHK) | 2215 R92C_TXDMA_OFFSET_CHK_DROP_DATA_EN); 2216 } 2217 2218 void 2219 urtwn_mac_init(void *cookie) 2220 { 2221 struct urtwn_softc *sc = cookie; 2222 int i; 2223 2224 /* Write MAC initialization values. */ 2225 if (sc->sc_sc.chip & RTWN_CHIP_88E) { 2226 for (i = 0; i < nitems(rtl8188eu_mac); i++) { 2227 urtwn_write_1(sc, rtl8188eu_mac[i].reg, 2228 rtl8188eu_mac[i].val); 2229 } 2230 urtwn_write_1(sc, R92C_MAX_AGGR_NUM, 0x07); 2231 } else if (sc->sc_sc.chip & RTWN_CHIP_92E) { 2232 for (i = 0; i < nitems(rtl8192eu_mac); i++) { 2233 urtwn_write_1(sc, rtl8192eu_mac[i].reg, 2234 rtl8192eu_mac[i].val); 2235 } 2236 } else { 2237 for (i = 0; i < nitems(rtl8192cu_mac); i++) 2238 urtwn_write_1(sc, rtl8192cu_mac[i].reg, 2239 rtl8192cu_mac[i].val); 2240 } 2241 } 2242 2243 void 2244 urtwn_bb_init(void *cookie) 2245 { 2246 struct urtwn_softc *sc = cookie; 2247 const struct r92c_bb_prog *prog; 2248 uint32_t reg; 2249 uint8_t xtal; 2250 int i; 2251 2252 /* Enable BB and RF. */ 2253 urtwn_write_2(sc, R92C_SYS_FUNC_EN, 2254 urtwn_read_2(sc, R92C_SYS_FUNC_EN) | 2255 R92C_SYS_FUNC_EN_BBRSTB | R92C_SYS_FUNC_EN_BB_GLB_RST | 2256 R92C_SYS_FUNC_EN_DIO_RF); 2257 2258 if (!(sc->sc_sc.chip & (RTWN_CHIP_88E | RTWN_CHIP_92E))) 2259 urtwn_write_2(sc, R92C_AFE_PLL_CTRL, 0xdb83); 2260 2261 urtwn_write_1(sc, R92C_RF_CTRL, 2262 R92C_RF_CTRL_EN | R92C_RF_CTRL_RSTB | R92C_RF_CTRL_SDMRSTB); 2263 urtwn_write_1(sc, R92C_SYS_FUNC_EN, 2264 R92C_SYS_FUNC_EN_USBA | R92C_SYS_FUNC_EN_USBD | 2265 R92C_SYS_FUNC_EN_BB_GLB_RST | R92C_SYS_FUNC_EN_BBRSTB); 2266 2267 if (!(sc->sc_sc.chip & (RTWN_CHIP_88E | RTWN_CHIP_92E))) { 2268 urtwn_write_1(sc, R92C_LDOHCI12_CTRL, 0x0f); 2269 urtwn_write_1(sc, 0x15, 0xe9); 2270 urtwn_write_1(sc, R92C_AFE_XTAL_CTRL + 1, 0x80); 2271 } 2272 2273 /* Select BB programming based on board type. */ 2274 if (sc->sc_sc.chip & RTWN_CHIP_88E) 2275 prog = &rtl8188eu_bb_prog; 2276 else if (sc->sc_sc.chip & RTWN_CHIP_92E) 2277 prog = &rtl8192eu_bb_prog; 2278 else if (!(sc->sc_sc.chip & RTWN_CHIP_92C)) { 2279 if (sc->sc_sc.board_type == R92C_BOARD_TYPE_MINICARD) 2280 prog = &rtl8188ce_bb_prog; 2281 else if (sc->sc_sc.board_type == R92C_BOARD_TYPE_HIGHPA) 2282 prog = &rtl8188ru_bb_prog; 2283 else 2284 prog = &rtl8188cu_bb_prog; 2285 } else { 2286 if (sc->sc_sc.board_type == R92C_BOARD_TYPE_MINICARD) 2287 prog = &rtl8192ce_bb_prog; 2288 else 2289 prog = &rtl8192cu_bb_prog; 2290 } 2291 /* Write BB initialization values. */ 2292 for (i = 0; i < prog->count; i++) { 2293 urtwn_bb_write(sc, prog->regs[i], prog->vals[i]); 2294 DELAY(1); 2295 } 2296 2297 if (sc->sc_sc.chip & RTWN_CHIP_92C_1T2R) { 2298 /* 8192C 1T only configuration. */ 2299 reg = urtwn_bb_read(sc, R92C_FPGA0_TXINFO); 2300 reg = (reg & ~0x00000003) | 0x2; 2301 urtwn_bb_write(sc, R92C_FPGA0_TXINFO, reg); 2302 2303 reg = urtwn_bb_read(sc, R92C_FPGA1_TXINFO); 2304 reg = (reg & ~0x00300033) | 0x00200022; 2305 urtwn_bb_write(sc, R92C_FPGA1_TXINFO, reg); 2306 2307 reg = urtwn_bb_read(sc, R92C_CCK0_AFESETTING); 2308 reg = (reg & ~0xff000000) | 0x45 << 24; 2309 urtwn_bb_write(sc, R92C_CCK0_AFESETTING, reg); 2310 2311 reg = urtwn_bb_read(sc, R92C_OFDM0_TRXPATHENA); 2312 reg = (reg & ~0x000000ff) | 0x23; 2313 urtwn_bb_write(sc, R92C_OFDM0_TRXPATHENA, reg); 2314 2315 reg = urtwn_bb_read(sc, R92C_OFDM0_AGCPARAM1); 2316 reg = (reg & ~0x00000030) | 1 << 4; 2317 urtwn_bb_write(sc, R92C_OFDM0_AGCPARAM1, reg); 2318 2319 reg = urtwn_bb_read(sc, 0xe74); 2320 reg = (reg & ~0x0c000000) | 2 << 26; 2321 urtwn_bb_write(sc, 0xe74, reg); 2322 reg = urtwn_bb_read(sc, 0xe78); 2323 reg = (reg & ~0x0c000000) | 2 << 26; 2324 urtwn_bb_write(sc, 0xe78, reg); 2325 reg = urtwn_bb_read(sc, 0xe7c); 2326 reg = (reg & ~0x0c000000) | 2 << 26; 2327 urtwn_bb_write(sc, 0xe7c, reg); 2328 reg = urtwn_bb_read(sc, 0xe80); 2329 reg = (reg & ~0x0c000000) | 2 << 26; 2330 urtwn_bb_write(sc, 0xe80, reg); 2331 reg = urtwn_bb_read(sc, 0xe88); 2332 reg = (reg & ~0x0c000000) | 2 << 26; 2333 urtwn_bb_write(sc, 0xe88, reg); 2334 } 2335 2336 /* Write AGC values. */ 2337 for (i = 0; i < prog->agccount; i++) { 2338 urtwn_bb_write(sc, R92C_OFDM0_AGCRSSITABLE, 2339 prog->agcvals[i]); 2340 DELAY(1); 2341 } 2342 2343 if (sc->sc_sc.chip & RTWN_CHIP_88E) { 2344 urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), 0x69553422); 2345 DELAY(1); 2346 urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), 0x69553420); 2347 DELAY(1); 2348 } else if (sc->sc_sc.chip & RTWN_CHIP_92E) { 2349 urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), 0x00040022); 2350 DELAY(1); 2351 urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), 0x00040020); 2352 DELAY(1); 2353 } 2354 2355 if (sc->sc_sc.chip & RTWN_CHIP_88E) { 2356 xtal = sc->sc_sc.crystal_cap & 0x3f; 2357 reg = urtwn_bb_read(sc, R92C_AFE_XTAL_CTRL); 2358 urtwn_bb_write(sc, R92C_AFE_XTAL_CTRL, 2359 RW(reg, R92C_AFE_XTAL_CTRL_ADDR, xtal | xtal << 6)); 2360 } else if (sc->sc_sc.chip & RTWN_CHIP_92E) { 2361 xtal = sc->sc_sc.crystal_cap & 0x3f; 2362 reg = urtwn_read_4(sc, R92C_AFE_CTRL3); 2363 reg &= 0xff000fff; 2364 reg |= (xtal | (xtal << 6)) << 12; 2365 urtwn_write_4(sc, R92C_AFE_CTRL3, reg); 2366 2367 urtwn_write_4(sc, R92C_AFE_XTAL_CTRL, 0x000f81fb); 2368 } 2369 2370 if (urtwn_bb_read(sc, R92C_HSSI_PARAM2(0)) & R92C_HSSI_PARAM2_CCK_HIPWR) 2371 sc->sc_sc.sc_flags |= RTWN_FLAG_CCK_HIPWR; 2372 } 2373 2374 void 2375 urtwn_burstlen_init(struct urtwn_softc *sc) 2376 { 2377 uint8_t reg; 2378 2379 reg = urtwn_read_1(sc, R92E_RXDMA_PRO); 2380 reg &= ~0x30; 2381 switch (sc->sc_udev->speed) { 2382 case USB_SPEED_HIGH: 2383 urtwn_write_1(sc, R92E_RXDMA_PRO, reg | 0x1e); 2384 break; 2385 default: 2386 urtwn_write_1(sc, R92E_RXDMA_PRO, reg | 0x2e); 2387 break; 2388 } 2389 } 2390 2391 int 2392 urtwn_power_on(void *cookie) 2393 { 2394 struct urtwn_softc *sc = cookie; 2395 2396 if (sc->sc_sc.chip & RTWN_CHIP_88E) 2397 return (urtwn_r88e_power_on(sc)); 2398 else if (sc->sc_sc.chip & RTWN_CHIP_92E) 2399 return (urtwn_r92e_power_on(sc)); 2400 2401 return (urtwn_r92c_power_on(sc)); 2402 } 2403 2404 int 2405 urtwn_alloc_buffers(void *cookie) 2406 { 2407 struct urtwn_softc *sc = cookie; 2408 int error; 2409 2410 /* Init host async commands ring. */ 2411 sc->cmdq.cur = sc->cmdq.next = sc->cmdq.queued = 0; 2412 2413 /* Allocate Tx/Rx buffers. */ 2414 error = urtwn_alloc_rx_list(sc); 2415 if (error != 0) { 2416 printf("%s: could not allocate Rx buffers\n", 2417 sc->sc_dev.dv_xname); 2418 return (error); 2419 } 2420 error = urtwn_alloc_tx_list(sc); 2421 if (error != 0) { 2422 printf("%s: could not allocate Tx buffers\n", 2423 sc->sc_dev.dv_xname); 2424 return (error); 2425 } 2426 2427 return (0); 2428 } 2429 2430 int 2431 urtwn_init(void *cookie) 2432 { 2433 struct urtwn_softc *sc = cookie; 2434 int i, error; 2435 2436 if (sc->sc_sc.chip & RTWN_CHIP_92E) 2437 urtwn_write_1(sc, R92C_ACLK_MON, 0); 2438 2439 /* Queue Rx xfers. */ 2440 for (i = 0; i < URTWN_RX_LIST_COUNT; i++) { 2441 struct urtwn_rx_data *data = &sc->rx_data[i]; 2442 2443 usbd_setup_xfer(data->xfer, sc->rx_pipe, data, data->buf, 2444 URTWN_RXBUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, 2445 USBD_NO_TIMEOUT, urtwn_rxeof); 2446 error = usbd_transfer(data->xfer); 2447 if (error != 0 && error != USBD_IN_PROGRESS) 2448 return (error); 2449 } 2450 2451 ieee80211_amrr_node_init(&sc->amrr, &sc->amn); 2452 2453 /* 2454 * Enable TX reports for AMRR. 2455 * In order to get reports we need to explicitly reset the register. 2456 */ 2457 if (sc->sc_sc.chip & RTWN_CHIP_88E) 2458 urtwn_write_1(sc, R88E_TX_RPT_CTRL, (urtwn_read_1(sc, 2459 R88E_TX_RPT_CTRL) & ~0) | R88E_TX_RPT_CTRL_EN); 2460 2461 return (0); 2462 } 2463 2464 void 2465 urtwn_stop(void *cookie) 2466 { 2467 struct urtwn_softc *sc = cookie; 2468 int i; 2469 2470 /* Abort Tx. */ 2471 for (i = 0; i < R92C_MAX_EPOUT; i++) { 2472 if (sc->tx_pipe[i] != NULL) 2473 usbd_abort_pipe(sc->tx_pipe[i]); 2474 } 2475 /* Stop Rx pipe. */ 2476 usbd_abort_pipe(sc->rx_pipe); 2477 /* Free Tx/Rx buffers. */ 2478 urtwn_free_tx_list(sc); 2479 urtwn_free_rx_list(sc); 2480 } 2481 2482 int 2483 urtwn_is_oactive(void *cookie) 2484 { 2485 struct urtwn_softc *sc = cookie; 2486 2487 return (TAILQ_EMPTY(&sc->tx_free_list)); 2488 } 2489