xref: /freebsd/sys/dev/rtwn/usb/rtwn_usb_ep.c (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 /*	$OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr 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) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
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 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23 
24 #include "opt_wlan.h"
25 
26 #include <sys/param.h>
27 #include <sys/lock.h>
28 #include <sys/mutex.h>
29 #include <sys/condvar.h>
30 #include <sys/mbuf.h>
31 #include <sys/kernel.h>
32 #include <sys/socket.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/queue.h>
36 #include <sys/taskqueue.h>
37 #include <sys/bus.h>
38 #include <sys/endian.h>
39 
40 #include <dev/usb/usb.h>
41 #include <dev/usb/usbdi.h>
42 #include <dev/usb/usb_device.h>
43 
44 #include <net/if.h>
45 #include <net/ethernet.h>
46 #include <net/if_media.h>
47 
48 #include <net80211/ieee80211_var.h>
49 
50 #include <dev/rtwn/if_rtwnvar.h>
51 #include <dev/rtwn/if_rtwn_debug.h>
52 
53 #include <dev/rtwn/usb/rtwn_usb_var.h>
54 #include <dev/rtwn/usb/rtwn_usb_ep.h>
55 #include <dev/rtwn/usb/rtwn_usb_rx.h>
56 #include <dev/rtwn/usb/rtwn_usb_tx.h>
57 
58 #include <dev/rtwn/rtl8192c/usb/r92cu_reg.h>
59 
60 static const struct usb_config rtwn_config_common[RTWN_N_TRANSFER] = {
61 	[RTWN_BULK_RX] = {
62 		.type = UE_BULK,
63 		.endpoint = UE_ADDR_ANY,
64 		.direction = UE_DIR_IN,
65 		.flags = {
66 			.pipe_bof = 1,
67 			.short_xfer_ok = 1
68 		},
69 		.callback = rtwn_bulk_rx_callback,
70 	},
71 	[RTWN_BULK_TX_BE] = {
72 		.type = UE_BULK,
73 		.endpoint = UE_ADDR_ANY,
74 		.direction = UE_DIR_OUT,
75 		.bufsize = RTWN_USB_TXBUFSZ,
76 		.flags = {
77 			.ext_buffer = 1,
78 			.pipe_bof = 1,
79 			.force_short_xfer = 1,
80 		},
81 		.callback = rtwn_bulk_tx_callback,
82 		.timeout = RTWN_TX_TIMEOUT,	/* ms */
83 	},
84 	[RTWN_BULK_TX_BK] = {
85 		.type = UE_BULK,
86 		.endpoint = UE_ADDR_ANY,
87 		.direction = UE_DIR_OUT,
88 		.bufsize = RTWN_USB_TXBUFSZ,
89 		.flags = {
90 			.ext_buffer = 1,
91 			.pipe_bof = 1,
92 			.force_short_xfer = 1,
93 		},
94 		.callback = rtwn_bulk_tx_callback,
95 		.timeout = RTWN_TX_TIMEOUT,	/* ms */
96 	},
97 	[RTWN_BULK_TX_VI] = {
98 		.type = UE_BULK,
99 		.endpoint = UE_ADDR_ANY,
100 		.direction = UE_DIR_OUT,
101 		.bufsize = RTWN_USB_TXBUFSZ,
102 		.flags = {
103 			.ext_buffer = 1,
104 			.pipe_bof = 1,
105 			.force_short_xfer = 1
106 		},
107 		.callback = rtwn_bulk_tx_callback,
108 		.timeout = RTWN_TX_TIMEOUT,	/* ms */
109 	},
110 	[RTWN_BULK_TX_VO] = {
111 		.type = UE_BULK,
112 		.endpoint = UE_ADDR_ANY,
113 		.direction = UE_DIR_OUT,
114 		.bufsize = RTWN_USB_TXBUFSZ,
115 		.flags = {
116 			.ext_buffer = 1,
117 			.pipe_bof = 1,
118 			.force_short_xfer = 1
119 		},
120 		.callback = rtwn_bulk_tx_callback,
121 		.timeout = RTWN_TX_TIMEOUT,	/* ms */
122 	},
123 };
124 
125 static void
126 rtwn_usb_setup_queues(struct rtwn_usb_softc *uc)
127 {
128 	struct rtwn_softc *sc = &uc->uc_sc;
129 	int hasnq, haslq, nqueues, nqpages, nrempages;
130 
131 	/* Get Tx queues to USB endpoints mapping. */
132 	hasnq = haslq = 0;
133 	switch (uc->ntx) {
134 	case 4:
135 	case 3:
136 		haslq = 1;
137 		/* FALLTHROUGH */
138 	case 2:
139 		hasnq = 1;
140 		/* FALLTHROUGH */
141 	default:
142 		break;
143 	}
144 	nqueues = 1 + hasnq + haslq;
145 
146 	/* Get the number of pages for each queue. */
147 	nqpages = (sc->page_count - sc->npubqpages) / nqueues;
148 
149 	/*
150 	 * The remaining pages are assigned to the high priority
151 	 * queue.
152 	 */
153 	nrempages = (sc->page_count - sc->npubqpages) % nqueues;
154 
155 	sc->nhqpages = nqpages + nrempages;
156 	sc->nnqpages = (hasnq ? nqpages : 0);
157 	sc->nlqpages = (haslq ? nqpages : 0);
158 }
159 
160 int
161 rtwn_usb_setup_endpoints(struct rtwn_usb_softc *uc)
162 {
163 	struct usb_config *rtwn_config;
164 	struct rtwn_softc *sc = &uc->uc_sc;
165 	const uint8_t iface_index = RTWN_IFACE_INDEX;
166 	struct usb_endpoint *ep, *ep_end;
167 	uint8_t addr[RTWN_MAX_EPOUT];
168 	int error;
169 
170 	/* Determine the number of bulk-out pipes. */
171 	uc->ntx = 0;
172 	ep = uc->uc_udev->endpoints;
173 	ep_end = uc->uc_udev->endpoints + uc->uc_udev->endpoints_max;
174 	for (; ep != ep_end; ep++) {
175 		uint8_t eaddr;
176 
177 		if ((ep->edesc == NULL) || (ep->iface_index != iface_index))
178 			continue;
179 
180 		eaddr = ep->edesc->bEndpointAddress;
181 		RTWN_DPRINTF(sc, RTWN_DEBUG_USB,
182 		    "%s: endpoint: addr %u, direction %s\n", __func__,
183 		    UE_GET_ADDR(eaddr), UE_GET_DIR(eaddr) == UE_DIR_OUT ?
184 		    "output" : "input");
185 
186 		if (UE_GET_DIR(eaddr) == UE_DIR_OUT) {
187 			if (uc->ntx == RTWN_MAX_EPOUT)
188 				break;
189 
190 			addr[uc->ntx++] = UE_GET_ADDR(eaddr);
191 		}
192 	}
193 	if (uc->ntx == 0 || uc->ntx > RTWN_MAX_EPOUT) {
194 		device_printf(sc->sc_dev,
195 		    "%s: invalid number of Tx bulk pipes (%d)\n", __func__,
196 		    uc->ntx);
197 		return (EINVAL);
198 	}
199 
200 	rtwn_config = malloc(sizeof(rtwn_config_common), M_TEMP, M_WAITOK);
201 	memcpy(rtwn_config, rtwn_config_common, sizeof(rtwn_config_common));
202 
203 	/* NB: keep in sync with rtwn_dma_init(). */
204 	rtwn_config[RTWN_BULK_TX_VO].endpoint = addr[0];
205 	switch (uc->ntx) {
206 	case 4:
207 	case 3:
208 		rtwn_config[RTWN_BULK_TX_BE].endpoint = addr[2];
209 		rtwn_config[RTWN_BULK_TX_BK].endpoint = addr[2];
210 		rtwn_config[RTWN_BULK_TX_VI].endpoint = addr[1];
211 		break;
212 	case 2:
213 		rtwn_config[RTWN_BULK_TX_BE].endpoint = addr[1];
214 		rtwn_config[RTWN_BULK_TX_BK].endpoint = addr[1];
215 		rtwn_config[RTWN_BULK_TX_VI].endpoint = addr[0];
216 		break;
217 	case 1:
218 		rtwn_config[RTWN_BULK_TX_BE].endpoint = addr[0];
219 		rtwn_config[RTWN_BULK_TX_BK].endpoint = addr[0];
220 		rtwn_config[RTWN_BULK_TX_VI].endpoint = addr[0];
221 		break;
222 	default:
223 		KASSERT(0, ("unhandled number of endpoints %d\n", uc->ntx));
224 		break;
225 	}
226 
227 	rtwn_config[RTWN_BULK_RX].bufsize =
228 	    uc->uc_rx_buf_size * RTWN_USB_RXBUFSZ_UNIT;
229 	error = usbd_transfer_setup(uc->uc_udev, &iface_index,
230 	    uc->uc_xfer, rtwn_config, RTWN_N_TRANSFER, uc, &sc->sc_mtx);
231 	free(rtwn_config, M_TEMP);
232 
233 	if (error) {
234 		device_printf(sc->sc_dev, "could not allocate USB transfers, "
235 		    "err=%s\n", usbd_errstr(error));
236 		return (error);
237 	}
238 
239 	/* Assign pages for each queue (if not done). */
240 	if (sc->nhqpages == 0 && sc->nnqpages == 0 && sc->nlqpages == 0)
241 		rtwn_usb_setup_queues(uc);
242 
243 	return (0);
244 }
245 
246 uint16_t
247 rtwn_usb_get_qmap(struct rtwn_softc *sc)
248 {
249 	struct rtwn_usb_softc *uc = RTWN_USB_SOFTC(sc);
250 
251 	switch (uc->ntx) {
252 	case 1:
253 		return (R92C_TRXDMA_CTRL_QMAP_HQ);
254 	case 2:
255 		return (R92C_TRXDMA_CTRL_QMAP_HQ_NQ);
256 	default:
257 		return (R92C_TRXDMA_CTRL_QMAP_3EP);
258 	}
259 }
260