xref: /freebsd/sys/dev/rtwn/usb/rtwn_usb_ep.c (revision 076ad2f836d5f49dc1375f1677335a48fe0d4b82)
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 
61 static const struct usb_config rtwn_config_common[RTWN_N_TRANSFER] = {
62 	[RTWN_BULK_RX] = {
63 		.type = UE_BULK,
64 		.endpoint = UE_ADDR_ANY,
65 		.direction = UE_DIR_IN,
66 		.flags = {
67 			.pipe_bof = 1,
68 			.short_xfer_ok = 1
69 		},
70 		.callback = rtwn_bulk_rx_callback,
71 	},
72 	[RTWN_BULK_TX_BE] = {
73 		.type = UE_BULK,
74 		.endpoint = UE_ADDR_ANY,
75 		.direction = UE_DIR_OUT,
76 		.bufsize = RTWN_TXBUFSZ,
77 		.flags = {
78 			.ext_buffer = 1,
79 			.pipe_bof = 1,
80 			.force_short_xfer = 1,
81 		},
82 		.callback = rtwn_bulk_tx_callback,
83 		.timeout = RTWN_TX_TIMEOUT,	/* ms */
84 	},
85 	[RTWN_BULK_TX_BK] = {
86 		.type = UE_BULK,
87 		.endpoint = UE_ADDR_ANY,
88 		.direction = UE_DIR_OUT,
89 		.bufsize = RTWN_TXBUFSZ,
90 		.flags = {
91 			.ext_buffer = 1,
92 			.pipe_bof = 1,
93 			.force_short_xfer = 1,
94 		},
95 		.callback = rtwn_bulk_tx_callback,
96 		.timeout = RTWN_TX_TIMEOUT,	/* ms */
97 	},
98 	[RTWN_BULK_TX_VI] = {
99 		.type = UE_BULK,
100 		.endpoint = UE_ADDR_ANY,
101 		.direction = UE_DIR_OUT,
102 		.bufsize = RTWN_TXBUFSZ,
103 		.flags = {
104 			.ext_buffer = 1,
105 			.pipe_bof = 1,
106 			.force_short_xfer = 1
107 		},
108 		.callback = rtwn_bulk_tx_callback,
109 		.timeout = RTWN_TX_TIMEOUT,	/* ms */
110 	},
111 	[RTWN_BULK_TX_VO] = {
112 		.type = UE_BULK,
113 		.endpoint = UE_ADDR_ANY,
114 		.direction = UE_DIR_OUT,
115 		.bufsize = RTWN_TXBUFSZ,
116 		.flags = {
117 			.ext_buffer = 1,
118 			.pipe_bof = 1,
119 			.force_short_xfer = 1
120 		},
121 		.callback = rtwn_bulk_tx_callback,
122 		.timeout = RTWN_TX_TIMEOUT,	/* ms */
123 	},
124 };
125 
126 static void
127 rtwn_usb_setup_queues(struct rtwn_usb_softc *uc)
128 {
129 	struct rtwn_softc *sc = &uc->uc_sc;
130 	int hasnq, haslq, nqueues, nqpages, nrempages;
131 
132 	/* Get Tx queues to USB endpoints mapping. */
133 	hasnq = haslq = 0;
134 	switch (uc->ntx) {
135 	case 4:
136 	case 3:
137 		haslq = 1;
138 		/* FALLTHROUGH */
139 	case 2:
140 		hasnq = 1;
141 		/* FALLTHROUGH */
142 	default:
143 		break;
144 	}
145 	nqueues = 1 + hasnq + haslq;
146 
147 	/* Get the number of pages for each queue. */
148 	nqpages = (sc->page_count - sc->npubqpages) / nqueues;
149 
150 	/*
151 	 * The remaining pages are assigned to the high priority
152 	 * queue.
153 	 */
154 	nrempages = (sc->page_count - sc->npubqpages) % nqueues;
155 
156 	sc->nhqpages = nqpages + nrempages;
157 	sc->nnqpages = (hasnq ? nqpages : 0);
158 	sc->nlqpages = (haslq ? nqpages : 0);
159 }
160 
161 int
162 rtwn_usb_setup_endpoints(struct rtwn_usb_softc *uc)
163 {
164 	struct usb_config *rtwn_config;
165 	struct rtwn_softc *sc = &uc->uc_sc;
166 	const uint8_t iface_index = RTWN_IFACE_INDEX;
167 	struct usb_endpoint *ep, *ep_end;
168 	uint8_t addr[RTWN_MAX_EPOUT];
169 	int error;
170 
171 	/* Determine the number of bulk-out pipes. */
172 	uc->ntx = 0;
173 	ep = uc->uc_udev->endpoints;
174 	ep_end = uc->uc_udev->endpoints + uc->uc_udev->endpoints_max;
175 	for (; ep != ep_end; ep++) {
176 		uint8_t eaddr;
177 
178 		if ((ep->edesc == NULL) || (ep->iface_index != iface_index))
179 			continue;
180 
181 		eaddr = ep->edesc->bEndpointAddress;
182 		RTWN_DPRINTF(sc, RTWN_DEBUG_USB,
183 		    "%s: endpoint: addr %u, direction %s\n", __func__,
184 		    UE_GET_ADDR(eaddr), UE_GET_DIR(eaddr) == UE_DIR_OUT ?
185 		    "output" : "input");
186 
187 		if (UE_GET_DIR(eaddr) == UE_DIR_OUT) {
188 			if (uc->ntx == RTWN_MAX_EPOUT)
189 				break;
190 
191 			addr[uc->ntx++] = UE_GET_ADDR(eaddr);
192 		}
193 	}
194 	if (uc->ntx == 0 || uc->ntx > RTWN_MAX_EPOUT) {
195 		device_printf(sc->sc_dev,
196 		    "%s: invalid number of Tx bulk pipes (%d)\n", __func__,
197 		    uc->ntx);
198 		return (EINVAL);
199 	}
200 
201 	rtwn_config = malloc(sizeof(rtwn_config_common), M_TEMP, M_WAITOK);
202 	memcpy(rtwn_config, rtwn_config_common, sizeof(rtwn_config_common));
203 
204 	/* NB: keep in sync with rtwn_dma_init(). */
205 	rtwn_config[RTWN_BULK_TX_VO].endpoint = addr[0];
206 	switch (uc->ntx) {
207 	case 4:
208 	case 3:
209 		rtwn_config[RTWN_BULK_TX_BE].endpoint = addr[2];
210 		rtwn_config[RTWN_BULK_TX_BK].endpoint = addr[2];
211 		rtwn_config[RTWN_BULK_TX_VI].endpoint = addr[1];
212 		break;
213 	case 2:
214 		rtwn_config[RTWN_BULK_TX_BE].endpoint = addr[1];
215 		rtwn_config[RTWN_BULK_TX_BK].endpoint = addr[1];
216 		rtwn_config[RTWN_BULK_TX_VI].endpoint = addr[0];
217 		break;
218 	case 1:
219 		rtwn_config[RTWN_BULK_TX_BE].endpoint = addr[0];
220 		rtwn_config[RTWN_BULK_TX_BK].endpoint = addr[0];
221 		rtwn_config[RTWN_BULK_TX_VI].endpoint = addr[0];
222 		break;
223 	default:
224 		KASSERT(0, ("unhandled number of endpoints %d\n", uc->ntx));
225 		break;
226 	}
227 
228 	rtwn_config[RTWN_BULK_RX].bufsize = sc->rx_dma_size + 1024;
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