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