xref: /freebsd/sys/dev/rtwn/usb/rtwn_usb_ep.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
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 struct usb_config rtwn_config[RTWN_N_TRANSFER] = {
62 	[RTWN_BULK_RX] = {
63 		.type = UE_BULK,
64 		.endpoint = UE_ADDR_ANY,
65 		.direction = UE_DIR_IN,
66 		.bufsize = RTWN_RXBUFSZ,
67 		.flags = {
68 			.pipe_bof = 1,
69 			.short_xfer_ok = 1
70 		},
71 		.callback = rtwn_bulk_rx_callback,
72 	},
73 	[RTWN_BULK_TX_BE] = {
74 		.type = UE_BULK,
75 		.endpoint = UE_ADDR_ANY,
76 		.direction = UE_DIR_OUT,
77 		.bufsize = RTWN_TXBUFSZ,
78 		.flags = {
79 			.ext_buffer = 1,
80 			.pipe_bof = 1,
81 			.force_short_xfer = 1,
82 		},
83 		.callback = rtwn_bulk_tx_callback,
84 		.timeout = RTWN_TX_TIMEOUT,	/* ms */
85 	},
86 	[RTWN_BULK_TX_BK] = {
87 		.type = UE_BULK,
88 		.endpoint = UE_ADDR_ANY,
89 		.direction = UE_DIR_OUT,
90 		.bufsize = RTWN_TXBUFSZ,
91 		.flags = {
92 			.ext_buffer = 1,
93 			.pipe_bof = 1,
94 			.force_short_xfer = 1,
95 		},
96 		.callback = rtwn_bulk_tx_callback,
97 		.timeout = RTWN_TX_TIMEOUT,	/* ms */
98 	},
99 	[RTWN_BULK_TX_VI] = {
100 		.type = UE_BULK,
101 		.endpoint = UE_ADDR_ANY,
102 		.direction = UE_DIR_OUT,
103 		.bufsize = RTWN_TXBUFSZ,
104 		.flags = {
105 			.ext_buffer = 1,
106 			.pipe_bof = 1,
107 			.force_short_xfer = 1
108 		},
109 		.callback = rtwn_bulk_tx_callback,
110 		.timeout = RTWN_TX_TIMEOUT,	/* ms */
111 	},
112 	[RTWN_BULK_TX_VO] = {
113 		.type = UE_BULK,
114 		.endpoint = UE_ADDR_ANY,
115 		.direction = UE_DIR_OUT,
116 		.bufsize = RTWN_TXBUFSZ,
117 		.flags = {
118 			.ext_buffer = 1,
119 			.pipe_bof = 1,
120 			.force_short_xfer = 1
121 		},
122 		.callback = rtwn_bulk_tx_callback,
123 		.timeout = RTWN_TX_TIMEOUT,	/* ms */
124 	},
125 };
126 
127 static void
128 rtwn_usb_setup_queues(struct rtwn_usb_softc *uc)
129 {
130 	struct rtwn_softc *sc = &uc->uc_sc;
131 	int hasnq, haslq, nqueues, nqpages, nrempages;
132 
133 	/* Get Tx queues to USB endpoints mapping. */
134 	hasnq = haslq = 0;
135 	switch (uc->ntx) {
136 	case 4:
137 	case 3:
138 		haslq = 1;
139 		/* FALLTHROUGH */
140 	case 2:
141 		hasnq = 1;
142 		/* FALLTHROUGH */
143 	default:
144 		break;
145 	}
146 	nqueues = 1 + hasnq + haslq;
147 
148 	/* Get the number of pages for each queue. */
149 	nqpages = (sc->page_count - sc->npubqpages) / nqueues;
150 
151 	/*
152 	 * The remaining pages are assigned to the high priority
153 	 * queue.
154 	 */
155 	nrempages = (sc->page_count - sc->npubqpages) % nqueues;
156 
157 	sc->nhqpages = nqpages + nrempages;
158 	sc->nnqpages = (hasnq ? nqpages : 0);
159 	sc->nlqpages = (haslq ? nqpages : 0);
160 }
161 
162 int
163 rtwn_usb_setup_endpoints(struct rtwn_usb_softc *uc)
164 {
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 	/* 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 	error = usbd_transfer_setup(uc->uc_udev, &iface_index,
226 	    uc->uc_xfer, rtwn_config, RTWN_N_TRANSFER, uc, &sc->sc_mtx);
227 	if (error) {
228 		device_printf(sc->sc_dev, "could not allocate USB transfers, "
229 		    "err=%s\n", usbd_errstr(error));
230 		return (error);
231 	}
232 
233 	/* Assign pages for each queue (if not done). */
234 	if (sc->nhqpages == 0 && sc->nnqpages == 0 && sc->nlqpages == 0)
235 		rtwn_usb_setup_queues(uc);
236 
237 	return (0);
238 }
239 
240 uint16_t
241 rtwn_usb_get_qmap(struct rtwn_softc *sc)
242 {
243 	struct rtwn_usb_softc *uc = RTWN_USB_SOFTC(sc);
244 
245 	switch (uc->ntx) {
246 	case 1:
247 		return (R92C_TRXDMA_CTRL_QMAP_HQ);
248 	case 2:
249 		return (R92C_TRXDMA_CTRL_QMAP_HQ_NQ);
250 	default:
251 		return (R92C_TRXDMA_CTRL_QMAP_3EP);
252 	}
253 }
254