/* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */ /*- * Copyright (c) 2010 Damien Bergamini * Copyright (c) 2014 Kevin Lo * Copyright (c) 2015-2016 Andriy Voskoboinyk * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include __FBSDID("$FreeBSD$"); #include "opt_wlan.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static const struct usb_config rtwn_config_common[RTWN_N_TRANSFER] = { [RTWN_BULK_RX] = { .type = UE_BULK, .endpoint = UE_ADDR_ANY, .direction = UE_DIR_IN, .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, .callback = rtwn_bulk_rx_callback, }, [RTWN_BULK_TX_BE] = { .type = UE_BULK, .endpoint = UE_ADDR_ANY, .direction = UE_DIR_OUT, .bufsize = RTWN_USB_TXBUFSZ, .flags = { .ext_buffer = 1, .pipe_bof = 1, .force_short_xfer = 1, }, .callback = rtwn_bulk_tx_callback, .timeout = RTWN_TX_TIMEOUT, /* ms */ }, [RTWN_BULK_TX_BK] = { .type = UE_BULK, .endpoint = UE_ADDR_ANY, .direction = UE_DIR_OUT, .bufsize = RTWN_USB_TXBUFSZ, .flags = { .ext_buffer = 1, .pipe_bof = 1, .force_short_xfer = 1, }, .callback = rtwn_bulk_tx_callback, .timeout = RTWN_TX_TIMEOUT, /* ms */ }, [RTWN_BULK_TX_VI] = { .type = UE_BULK, .endpoint = UE_ADDR_ANY, .direction = UE_DIR_OUT, .bufsize = RTWN_USB_TXBUFSZ, .flags = { .ext_buffer = 1, .pipe_bof = 1, .force_short_xfer = 1 }, .callback = rtwn_bulk_tx_callback, .timeout = RTWN_TX_TIMEOUT, /* ms */ }, [RTWN_BULK_TX_VO] = { .type = UE_BULK, .endpoint = UE_ADDR_ANY, .direction = UE_DIR_OUT, .bufsize = RTWN_USB_TXBUFSZ, .flags = { .ext_buffer = 1, .pipe_bof = 1, .force_short_xfer = 1 }, .callback = rtwn_bulk_tx_callback, .timeout = RTWN_TX_TIMEOUT, /* ms */ }, }; static void rtwn_usb_setup_queues(struct rtwn_usb_softc *uc) { struct rtwn_softc *sc = &uc->uc_sc; int hasnq, haslq, nqueues, nqpages, nrempages; /* Get Tx queues to USB endpoints mapping. */ hasnq = haslq = 0; switch (uc->ntx) { case 4: case 3: haslq = 1; /* FALLTHROUGH */ case 2: hasnq = 1; /* FALLTHROUGH */ default: break; } nqueues = 1 + hasnq + haslq; /* Get the number of pages for each queue. */ nqpages = (sc->page_count - sc->npubqpages) / nqueues; /* * The remaining pages are assigned to the high priority * queue. */ nrempages = (sc->page_count - sc->npubqpages) % nqueues; sc->nhqpages = nqpages + nrempages; sc->nnqpages = (hasnq ? nqpages : 0); sc->nlqpages = (haslq ? nqpages : 0); } int rtwn_usb_setup_endpoints(struct rtwn_usb_softc *uc) { struct usb_config *rtwn_config; struct rtwn_softc *sc = &uc->uc_sc; const uint8_t iface_index = RTWN_IFACE_INDEX; struct usb_endpoint *ep, *ep_end; uint8_t addr[RTWN_MAX_EPOUT]; int error; /* Determine the number of bulk-out pipes. */ uc->ntx = 0; ep = uc->uc_udev->endpoints; ep_end = uc->uc_udev->endpoints + uc->uc_udev->endpoints_max; for (; ep != ep_end; ep++) { uint8_t eaddr; if ((ep->edesc == NULL) || (ep->iface_index != iface_index)) continue; eaddr = ep->edesc->bEndpointAddress; RTWN_DPRINTF(sc, RTWN_DEBUG_USB, "%s: endpoint: addr %u, direction %s\n", __func__, UE_GET_ADDR(eaddr), UE_GET_DIR(eaddr) == UE_DIR_OUT ? "output" : "input"); if (UE_GET_DIR(eaddr) == UE_DIR_OUT) { if (uc->ntx == RTWN_MAX_EPOUT) break; addr[uc->ntx++] = UE_GET_ADDR(eaddr); } } if (uc->ntx == 0 || uc->ntx > RTWN_MAX_EPOUT) { device_printf(sc->sc_dev, "%s: invalid number of Tx bulk pipes (%d)\n", __func__, uc->ntx); return (EINVAL); } rtwn_config = malloc(sizeof(rtwn_config_common), M_TEMP, M_WAITOK); memcpy(rtwn_config, rtwn_config_common, sizeof(rtwn_config_common)); /* NB: keep in sync with rtwn_dma_init(). */ rtwn_config[RTWN_BULK_TX_VO].endpoint = addr[0]; switch (uc->ntx) { case 4: case 3: rtwn_config[RTWN_BULK_TX_BE].endpoint = addr[2]; rtwn_config[RTWN_BULK_TX_BK].endpoint = addr[2]; rtwn_config[RTWN_BULK_TX_VI].endpoint = addr[1]; break; case 2: rtwn_config[RTWN_BULK_TX_BE].endpoint = addr[1]; rtwn_config[RTWN_BULK_TX_BK].endpoint = addr[1]; rtwn_config[RTWN_BULK_TX_VI].endpoint = addr[0]; break; case 1: rtwn_config[RTWN_BULK_TX_BE].endpoint = addr[0]; rtwn_config[RTWN_BULK_TX_BK].endpoint = addr[0]; rtwn_config[RTWN_BULK_TX_VI].endpoint = addr[0]; break; default: KASSERT(0, ("unhandled number of endpoints %d\n", uc->ntx)); break; } rtwn_config[RTWN_BULK_RX].bufsize = sc->rx_dma_size + 1024; error = usbd_transfer_setup(uc->uc_udev, &iface_index, uc->uc_xfer, rtwn_config, RTWN_N_TRANSFER, uc, &sc->sc_mtx); free(rtwn_config, M_TEMP); if (error) { device_printf(sc->sc_dev, "could not allocate USB transfers, " "err=%s\n", usbd_errstr(error)); return (error); } /* Assign pages for each queue (if not done). */ if (sc->nhqpages == 0 && sc->nnqpages == 0 && sc->nlqpages == 0) rtwn_usb_setup_queues(uc); return (0); } uint16_t rtwn_usb_get_qmap(struct rtwn_softc *sc) { struct rtwn_usb_softc *uc = RTWN_USB_SOFTC(sc); switch (uc->ntx) { case 1: return (R92C_TRXDMA_CTRL_QMAP_HQ); case 2: return (R92C_TRXDMA_CTRL_QMAP_HQ_NQ); default: return (R92C_TRXDMA_CTRL_QMAP_3EP); } }