dwc2_hcdqueue.c revision 1.16 1 1.16 skrll /* $NetBSD: dwc2_hcdqueue.c,v 1.16 2021/12/21 09:51:22 skrll Exp $ */
2 1.4 skrll
3 1.1 skrll /*
4 1.1 skrll * hcd_queue.c - DesignWare HS OTG Controller host queuing routines
5 1.1 skrll *
6 1.1 skrll * Copyright (C) 2004-2013 Synopsys, Inc.
7 1.1 skrll *
8 1.1 skrll * Redistribution and use in source and binary forms, with or without
9 1.1 skrll * modification, are permitted provided that the following conditions
10 1.1 skrll * are met:
11 1.1 skrll * 1. Redistributions of source code must retain the above copyright
12 1.1 skrll * notice, this list of conditions, and the following disclaimer,
13 1.1 skrll * without modification.
14 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 skrll * notice, this list of conditions and the following disclaimer in the
16 1.1 skrll * documentation and/or other materials provided with the distribution.
17 1.1 skrll * 3. The names of the above-listed copyright holders may not be used
18 1.1 skrll * to endorse or promote products derived from this software without
19 1.1 skrll * specific prior written permission.
20 1.1 skrll *
21 1.1 skrll * ALTERNATIVELY, this software may be distributed under the terms of the
22 1.1 skrll * GNU General Public License ("GPL") as published by the Free Software
23 1.1 skrll * Foundation; either version 2 of the License, or (at your option) any
24 1.1 skrll * later version.
25 1.1 skrll *
26 1.1 skrll * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 1.1 skrll * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 1.1 skrll * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 1.1 skrll * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 1.1 skrll * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 1.1 skrll * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 1.1 skrll * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 1.1 skrll * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 1.1 skrll * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 1.1 skrll * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 1.1 skrll */
38 1.1 skrll
39 1.1 skrll /*
40 1.1 skrll * This file contains the functions to manage Queue Heads and Queue
41 1.1 skrll * Transfer Descriptors for Host mode
42 1.1 skrll */
43 1.2 skrll
44 1.2 skrll #include <sys/cdefs.h>
45 1.16 skrll __KERNEL_RCSID(0, "$NetBSD: dwc2_hcdqueue.c,v 1.16 2021/12/21 09:51:22 skrll Exp $");
46 1.2 skrll
47 1.2 skrll #include <sys/types.h>
48 1.2 skrll #include <sys/kmem.h>
49 1.2 skrll #include <sys/pool.h>
50 1.2 skrll
51 1.2 skrll #include <dev/usb/usb.h>
52 1.2 skrll #include <dev/usb/usbdi.h>
53 1.2 skrll #include <dev/usb/usbdivar.h>
54 1.2 skrll #include <dev/usb/usb_mem.h>
55 1.2 skrll
56 1.2 skrll #include <machine/param.h>
57 1.2 skrll
58 1.1 skrll #include <linux/kernel.h>
59 1.1 skrll
60 1.2 skrll #include <dwc2/dwc2.h>
61 1.2 skrll #include <dwc2/dwc2var.h>
62 1.2 skrll
63 1.2 skrll #include "dwc2_core.h"
64 1.2 skrll #include "dwc2_hcd.h"
65 1.1 skrll
66 1.2 skrll static u32 dwc2_calc_bus_time(struct dwc2_hsotg *, int, int, int, int);
67 1.15 simonb static void dwc2_wait_timer_fn(void *);
68 1.15 simonb
69 1.15 simonb /* If we get a NAK, wait this long before retrying */
70 1.15 simonb #define DWC2_RETRY_WAIT_DELAY 1 /* msec */
71 1.1 skrll
72 1.1 skrll /**
73 1.1 skrll * dwc2_qh_init() - Initializes a QH structure
74 1.1 skrll *
75 1.1 skrll * @hsotg: The HCD state structure for the DWC OTG controller
76 1.1 skrll * @qh: The QH to init
77 1.1 skrll * @urb: Holds the information about the device/endpoint needed to initialize
78 1.1 skrll * the QH
79 1.1 skrll */
80 1.1 skrll #define SCHEDULE_SLOP 10
81 1.1 skrll static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
82 1.1 skrll struct dwc2_hcd_urb *urb)
83 1.1 skrll {
84 1.1 skrll int dev_speed, hub_addr, hub_port;
85 1.1 skrll
86 1.1 skrll dev_vdbg(hsotg->dev, "%s()\n", __func__);
87 1.1 skrll
88 1.1 skrll /* Initialize QH */
89 1.15 simonb qh->hsotg = hsotg;
90 1.15 simonb /* XXX timer_setup(&qh->wait_timer, dwc2_wait_timer_fn, 0); */
91 1.15 simonb callout_init(&qh->wait_timer, 0);
92 1.15 simonb callout_setfunc(&qh->wait_timer, dwc2_wait_timer_fn, qh);
93 1.1 skrll qh->ep_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
94 1.1 skrll qh->ep_is_in = dwc2_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
95 1.1 skrll
96 1.1 skrll qh->data_toggle = DWC2_HC_PID_DATA0;
97 1.1 skrll qh->maxp = dwc2_hcd_get_mps(&urb->pipe_info);
98 1.1 skrll INIT_LIST_HEAD(&qh->qtd_list);
99 1.1 skrll INIT_LIST_HEAD(&qh->qh_list_entry);
100 1.1 skrll
101 1.1 skrll /* FS/LS Endpoint on HS Hub, NOT virtual root hub */
102 1.1 skrll dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
103 1.1 skrll
104 1.1 skrll dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
105 1.11 skrll qh->nak_frame = 0xffff;
106 1.1 skrll
107 1.1 skrll if ((dev_speed == USB_SPEED_LOW || dev_speed == USB_SPEED_FULL) &&
108 1.1 skrll hub_addr != 0 && hub_addr != 1) {
109 1.1 skrll dev_vdbg(hsotg->dev,
110 1.1 skrll "QH init: EP %d: TT found at hub addr %d, for port %d\n",
111 1.1 skrll dwc2_hcd_get_ep_num(&urb->pipe_info), hub_addr,
112 1.1 skrll hub_port);
113 1.1 skrll qh->do_split = 1;
114 1.1 skrll }
115 1.1 skrll
116 1.1 skrll if (qh->ep_type == USB_ENDPOINT_XFER_INT ||
117 1.1 skrll qh->ep_type == USB_ENDPOINT_XFER_ISOC) {
118 1.1 skrll /* Compute scheduling parameters once and save them */
119 1.1 skrll u32 hprt, prtspd;
120 1.1 skrll
121 1.1 skrll /* Todo: Account for split transfers in the bus time */
122 1.1 skrll int bytecount =
123 1.1 skrll dwc2_hb_mult(qh->maxp) * dwc2_max_packet(qh->maxp);
124 1.1 skrll
125 1.2 skrll qh->usecs = dwc2_calc_bus_time(hsotg, qh->do_split ?
126 1.1 skrll USB_SPEED_HIGH : dev_speed, qh->ep_is_in,
127 1.1 skrll qh->ep_type == USB_ENDPOINT_XFER_ISOC,
128 1.2 skrll bytecount);
129 1.14 skrll
130 1.14 skrll /* Ensure frame_number corresponds to the reality */
131 1.14 skrll hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
132 1.1 skrll /* Start in a slightly future (micro)frame */
133 1.1 skrll qh->sched_frame = dwc2_frame_num_inc(hsotg->frame_number,
134 1.1 skrll SCHEDULE_SLOP);
135 1.1 skrll qh->interval = urb->interval;
136 1.1 skrll #if 0
137 1.1 skrll /* Increase interrupt polling rate for debugging */
138 1.1 skrll if (qh->ep_type == USB_ENDPOINT_XFER_INT)
139 1.1 skrll qh->interval = 8;
140 1.1 skrll #endif
141 1.2 skrll hprt = DWC2_READ_4(hsotg, HPRT0);
142 1.3 skrll prtspd = (hprt & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
143 1.1 skrll if (prtspd == HPRT0_SPD_HIGH_SPEED &&
144 1.1 skrll (dev_speed == USB_SPEED_LOW ||
145 1.1 skrll dev_speed == USB_SPEED_FULL)) {
146 1.1 skrll qh->interval *= 8;
147 1.1 skrll qh->sched_frame |= 0x7;
148 1.1 skrll qh->start_split_frame = qh->sched_frame;
149 1.1 skrll }
150 1.1 skrll dev_dbg(hsotg->dev, "interval=%d\n", qh->interval);
151 1.1 skrll }
152 1.1 skrll
153 1.1 skrll dev_vdbg(hsotg->dev, "DWC OTG HCD QH Initialized\n");
154 1.1 skrll dev_vdbg(hsotg->dev, "DWC OTG HCD QH - qh = %p\n", qh);
155 1.1 skrll dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Device Address = %d\n",
156 1.1 skrll dwc2_hcd_get_dev_addr(&urb->pipe_info));
157 1.1 skrll dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Endpoint %d, %s\n",
158 1.1 skrll dwc2_hcd_get_ep_num(&urb->pipe_info),
159 1.1 skrll dwc2_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
160 1.1 skrll
161 1.1 skrll qh->dev_speed = dev_speed;
162 1.1 skrll
163 1.8 skrll #ifdef DWC2_DEBUG
164 1.8 skrll const char *speed, *type;
165 1.1 skrll switch (dev_speed) {
166 1.1 skrll case USB_SPEED_LOW:
167 1.1 skrll speed = "low";
168 1.1 skrll break;
169 1.1 skrll case USB_SPEED_FULL:
170 1.1 skrll speed = "full";
171 1.1 skrll break;
172 1.1 skrll case USB_SPEED_HIGH:
173 1.1 skrll speed = "high";
174 1.1 skrll break;
175 1.1 skrll default:
176 1.1 skrll speed = "?";
177 1.1 skrll break;
178 1.1 skrll }
179 1.1 skrll dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Speed = %s\n", speed);
180 1.1 skrll
181 1.1 skrll switch (qh->ep_type) {
182 1.1 skrll case USB_ENDPOINT_XFER_ISOC:
183 1.1 skrll type = "isochronous";
184 1.1 skrll break;
185 1.1 skrll case USB_ENDPOINT_XFER_INT:
186 1.1 skrll type = "interrupt";
187 1.1 skrll break;
188 1.1 skrll case USB_ENDPOINT_XFER_CONTROL:
189 1.1 skrll type = "control";
190 1.1 skrll break;
191 1.1 skrll case USB_ENDPOINT_XFER_BULK:
192 1.1 skrll type = "bulk";
193 1.1 skrll break;
194 1.1 skrll default:
195 1.1 skrll type = "?";
196 1.1 skrll break;
197 1.1 skrll }
198 1.1 skrll
199 1.1 skrll dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Type = %s\n", type);
200 1.8 skrll #endif
201 1.1 skrll
202 1.1 skrll if (qh->ep_type == USB_ENDPOINT_XFER_INT) {
203 1.1 skrll dev_vdbg(hsotg->dev, "DWC OTG HCD QH - usecs = %d\n",
204 1.1 skrll qh->usecs);
205 1.1 skrll dev_vdbg(hsotg->dev, "DWC OTG HCD QH - interval = %d\n",
206 1.1 skrll qh->interval);
207 1.1 skrll }
208 1.1 skrll }
209 1.1 skrll
210 1.1 skrll /**
211 1.1 skrll * dwc2_hcd_qh_create() - Allocates and initializes a QH
212 1.1 skrll *
213 1.3 skrll * @hsotg: The HCD state structure for the DWC OTG controller
214 1.3 skrll * @urb: Holds the information about the device/endpoint needed
215 1.3 skrll * to initialize the QH
216 1.3 skrll * @mem_flags: Flag to do atomic allocation if needed
217 1.1 skrll *
218 1.1 skrll * Return: Pointer to the newly allocated QH, or NULL on error
219 1.1 skrll */
220 1.12 skrll struct dwc2_qh *dwc2_hcd_qh_create(struct dwc2_hsotg *hsotg,
221 1.1 skrll struct dwc2_hcd_urb *urb,
222 1.1 skrll gfp_t mem_flags)
223 1.1 skrll {
224 1.2 skrll struct dwc2_softc *sc = hsotg->hsotg_sc;
225 1.1 skrll struct dwc2_qh *qh;
226 1.1 skrll
227 1.1 skrll if (!urb->priv)
228 1.1 skrll return NULL;
229 1.1 skrll
230 1.1 skrll /* Allocate memory */
231 1.2 skrll qh = pool_cache_get(sc->sc_qhpool, PR_NOWAIT);
232 1.1 skrll if (!qh)
233 1.1 skrll return NULL;
234 1.1 skrll
235 1.2 skrll memset(qh, 0, sizeof(*qh));
236 1.1 skrll dwc2_qh_init(hsotg, qh, urb);
237 1.1 skrll
238 1.1 skrll if (hsotg->core_params->dma_desc_enable > 0 &&
239 1.1 skrll dwc2_hcd_qh_init_ddma(hsotg, qh, mem_flags) < 0) {
240 1.1 skrll dwc2_hcd_qh_free(hsotg, qh);
241 1.1 skrll return NULL;
242 1.1 skrll }
243 1.1 skrll
244 1.1 skrll return qh;
245 1.1 skrll }
246 1.1 skrll
247 1.1 skrll /**
248 1.1 skrll * dwc2_hcd_qh_free() - Frees the QH
249 1.1 skrll *
250 1.1 skrll * @hsotg: HCD instance
251 1.1 skrll * @qh: The QH to free
252 1.1 skrll *
253 1.1 skrll * QH should already be removed from the list. QTD list should already be empty
254 1.1 skrll * if called from URB Dequeue.
255 1.1 skrll *
256 1.1 skrll * Must NOT be called with interrupt disabled or spinlock held
257 1.1 skrll */
258 1.1 skrll void dwc2_hcd_qh_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
259 1.1 skrll {
260 1.2 skrll struct dwc2_softc *sc = hsotg->hsotg_sc;
261 1.15 simonb
262 1.15 simonb /*
263 1.15 simonb * We don't have the lock so we can safely wait until the wait timer
264 1.15 simonb * finishes. Of course, at this point in time we'd better have set
265 1.15 simonb * wait_timer_active to false so if this timer was still pending it
266 1.15 simonb * won't do anything anyway, but we want it to finish before we free
267 1.15 simonb * memory.
268 1.15 simonb */
269 1.15 simonb /* XXX del_timer_sync(&qh->wait_timer); */
270 1.15 simonb callout_destroy(&qh->wait_timer); /* XXX need to callout_halt() first? */
271 1.15 simonb
272 1.14 skrll if (qh->desc_list) {
273 1.1 skrll dwc2_hcd_qh_free_ddma(hsotg, qh);
274 1.1 skrll } else if (qh->dw_align_buf) {
275 1.16 skrll usb_freemem(&qh->dw_align_buf_usbdma);
276 1.12 skrll qh->dw_align_buf_dma = (dma_addr_t)0;
277 1.1 skrll }
278 1.1 skrll
279 1.2 skrll pool_cache_put(sc->sc_qhpool, qh);
280 1.1 skrll }
281 1.1 skrll
282 1.1 skrll /**
283 1.1 skrll * dwc2_periodic_channel_available() - Checks that a channel is available for a
284 1.1 skrll * periodic transfer
285 1.1 skrll *
286 1.1 skrll * @hsotg: The HCD state structure for the DWC OTG controller
287 1.1 skrll *
288 1.3 skrll * Return: 0 if successful, negative error code otherwise
289 1.1 skrll */
290 1.1 skrll static int dwc2_periodic_channel_available(struct dwc2_hsotg *hsotg)
291 1.1 skrll {
292 1.1 skrll /*
293 1.3 skrll * Currently assuming that there is a dedicated host channel for
294 1.1 skrll * each periodic transaction plus at least one host channel for
295 1.1 skrll * non-periodic transactions
296 1.1 skrll */
297 1.1 skrll int status;
298 1.1 skrll int num_channels;
299 1.1 skrll
300 1.1 skrll num_channels = hsotg->core_params->host_channels;
301 1.1 skrll if (hsotg->periodic_channels + hsotg->non_periodic_channels <
302 1.1 skrll num_channels
303 1.1 skrll && hsotg->periodic_channels < num_channels - 1) {
304 1.1 skrll status = 0;
305 1.1 skrll } else {
306 1.1 skrll dev_dbg(hsotg->dev,
307 1.1 skrll "%s: Total channels: %d, Periodic: %d, "
308 1.1 skrll "Non-periodic: %d\n", __func__, num_channels,
309 1.1 skrll hsotg->periodic_channels, hsotg->non_periodic_channels);
310 1.1 skrll status = -ENOSPC;
311 1.1 skrll }
312 1.1 skrll
313 1.1 skrll return status;
314 1.1 skrll }
315 1.1 skrll
316 1.1 skrll /**
317 1.1 skrll * dwc2_check_periodic_bandwidth() - Checks that there is sufficient bandwidth
318 1.1 skrll * for the specified QH in the periodic schedule
319 1.1 skrll *
320 1.1 skrll * @hsotg: The HCD state structure for the DWC OTG controller
321 1.1 skrll * @qh: QH containing periodic bandwidth required
322 1.1 skrll *
323 1.1 skrll * Return: 0 if successful, negative error code otherwise
324 1.1 skrll *
325 1.1 skrll * For simplicity, this calculation assumes that all the transfers in the
326 1.1 skrll * periodic schedule may occur in the same (micro)frame
327 1.1 skrll */
328 1.1 skrll static int dwc2_check_periodic_bandwidth(struct dwc2_hsotg *hsotg,
329 1.1 skrll struct dwc2_qh *qh)
330 1.1 skrll {
331 1.1 skrll int status;
332 1.1 skrll s16 max_claimed_usecs;
333 1.1 skrll
334 1.1 skrll status = 0;
335 1.1 skrll
336 1.1 skrll if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
337 1.1 skrll /*
338 1.1 skrll * High speed mode
339 1.1 skrll * Max periodic usecs is 80% x 125 usec = 100 usec
340 1.1 skrll */
341 1.1 skrll max_claimed_usecs = 100 - qh->usecs;
342 1.1 skrll } else {
343 1.1 skrll /*
344 1.1 skrll * Full speed mode
345 1.1 skrll * Max periodic usecs is 90% x 1000 usec = 900 usec
346 1.1 skrll */
347 1.1 skrll max_claimed_usecs = 900 - qh->usecs;
348 1.1 skrll }
349 1.1 skrll
350 1.1 skrll if (hsotg->periodic_usecs > max_claimed_usecs) {
351 1.1 skrll dev_err(hsotg->dev,
352 1.1 skrll "%s: already claimed usecs %d, required usecs %d\n",
353 1.1 skrll __func__, hsotg->periodic_usecs, qh->usecs);
354 1.1 skrll status = -ENOSPC;
355 1.1 skrll }
356 1.1 skrll
357 1.1 skrll return status;
358 1.1 skrll }
359 1.1 skrll
360 1.1 skrll /**
361 1.1 skrll * Microframe scheduler
362 1.1 skrll * track the total use in hsotg->frame_usecs
363 1.1 skrll * keep each qh use in qh->frame_usecs
364 1.1 skrll * when surrendering the qh then donate the time back
365 1.1 skrll */
366 1.1 skrll static const unsigned short max_uframe_usecs[] = {
367 1.1 skrll 100, 100, 100, 100, 100, 100, 30, 0
368 1.1 skrll };
369 1.1 skrll
370 1.1 skrll void dwc2_hcd_init_usecs(struct dwc2_hsotg *hsotg)
371 1.1 skrll {
372 1.1 skrll int i;
373 1.1 skrll
374 1.1 skrll for (i = 0; i < 8; i++)
375 1.1 skrll hsotg->frame_usecs[i] = max_uframe_usecs[i];
376 1.1 skrll }
377 1.1 skrll
378 1.1 skrll static int dwc2_find_single_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
379 1.1 skrll {
380 1.1 skrll unsigned short utime = qh->usecs;
381 1.6 skrll int i;
382 1.1 skrll
383 1.6 skrll for (i = 0; i < 8; i++) {
384 1.1 skrll /* At the start hsotg->frame_usecs[i] = max_uframe_usecs[i] */
385 1.1 skrll if (utime <= hsotg->frame_usecs[i]) {
386 1.1 skrll hsotg->frame_usecs[i] -= utime;
387 1.1 skrll qh->frame_usecs[i] += utime;
388 1.6 skrll return i;
389 1.1 skrll }
390 1.1 skrll }
391 1.9 skrll return -ENOSPC;
392 1.1 skrll }
393 1.1 skrll
394 1.1 skrll /*
395 1.1 skrll * use this for FS apps that can span multiple uframes
396 1.1 skrll */
397 1.1 skrll static int dwc2_find_multi_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
398 1.1 skrll {
399 1.1 skrll unsigned short utime = qh->usecs;
400 1.1 skrll unsigned short xtime;
401 1.6 skrll int t_left;
402 1.6 skrll int i;
403 1.1 skrll int j;
404 1.6 skrll int k;
405 1.1 skrll
406 1.6 skrll for (i = 0; i < 8; i++) {
407 1.6 skrll if (hsotg->frame_usecs[i] <= 0)
408 1.1 skrll continue;
409 1.1 skrll
410 1.1 skrll /*
411 1.1 skrll * we need n consecutive slots so use j as a start slot
412 1.1 skrll * j plus j+1 must be enough time (for now)
413 1.1 skrll */
414 1.1 skrll xtime = hsotg->frame_usecs[i];
415 1.1 skrll for (j = i + 1; j < 8; j++) {
416 1.1 skrll /*
417 1.1 skrll * if we add this frame remaining time to xtime we may
418 1.1 skrll * be OK, if not we need to test j for a complete frame
419 1.1 skrll */
420 1.1 skrll if (xtime + hsotg->frame_usecs[j] < utime) {
421 1.1 skrll if (hsotg->frame_usecs[j] <
422 1.6 skrll max_uframe_usecs[j])
423 1.6 skrll continue;
424 1.1 skrll }
425 1.1 skrll if (xtime >= utime) {
426 1.6 skrll t_left = utime;
427 1.6 skrll for (k = i; k < 8; k++) {
428 1.6 skrll t_left -= hsotg->frame_usecs[k];
429 1.6 skrll if (t_left <= 0) {
430 1.6 skrll qh->frame_usecs[k] +=
431 1.6 skrll hsotg->frame_usecs[k]
432 1.6 skrll + t_left;
433 1.6 skrll hsotg->frame_usecs[k] = -t_left;
434 1.6 skrll return i;
435 1.6 skrll } else {
436 1.6 skrll qh->frame_usecs[k] +=
437 1.6 skrll hsotg->frame_usecs[k];
438 1.6 skrll hsotg->frame_usecs[k] = 0;
439 1.6 skrll }
440 1.6 skrll }
441 1.1 skrll }
442 1.1 skrll /* add the frame time to x time */
443 1.1 skrll xtime += hsotg->frame_usecs[j];
444 1.1 skrll /* we must have a fully available next frame or break */
445 1.1 skrll if (xtime < utime &&
446 1.6 skrll hsotg->frame_usecs[j] == max_uframe_usecs[j])
447 1.6 skrll continue;
448 1.1 skrll }
449 1.1 skrll }
450 1.9 skrll return -ENOSPC;
451 1.1 skrll }
452 1.1 skrll
453 1.1 skrll static int dwc2_find_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
454 1.1 skrll {
455 1.1 skrll int ret;
456 1.1 skrll
457 1.1 skrll if (qh->dev_speed == USB_SPEED_HIGH) {
458 1.1 skrll /* if this is a hs transaction we need a full frame */
459 1.1 skrll ret = dwc2_find_single_uframe(hsotg, qh);
460 1.1 skrll } else {
461 1.1 skrll /*
462 1.1 skrll * if this is a fs transaction we may need a sequence
463 1.1 skrll * of frames
464 1.1 skrll */
465 1.1 skrll ret = dwc2_find_multi_uframe(hsotg, qh);
466 1.1 skrll }
467 1.1 skrll return ret;
468 1.1 skrll }
469 1.1 skrll
470 1.1 skrll /**
471 1.1 skrll * dwc2_check_max_xfer_size() - Checks that the max transfer size allowed in a
472 1.1 skrll * host channel is large enough to handle the maximum data transfer in a single
473 1.1 skrll * (micro)frame for a periodic transfer
474 1.1 skrll *
475 1.1 skrll * @hsotg: The HCD state structure for the DWC OTG controller
476 1.1 skrll * @qh: QH for a periodic endpoint
477 1.1 skrll *
478 1.1 skrll * Return: 0 if successful, negative error code otherwise
479 1.1 skrll */
480 1.1 skrll static int dwc2_check_max_xfer_size(struct dwc2_hsotg *hsotg,
481 1.1 skrll struct dwc2_qh *qh)
482 1.1 skrll {
483 1.1 skrll u32 max_xfer_size;
484 1.1 skrll u32 max_channel_xfer_size;
485 1.1 skrll int status = 0;
486 1.1 skrll
487 1.1 skrll max_xfer_size = dwc2_max_packet(qh->maxp) * dwc2_hb_mult(qh->maxp);
488 1.1 skrll max_channel_xfer_size = hsotg->core_params->max_transfer_size;
489 1.1 skrll
490 1.1 skrll if (max_xfer_size > max_channel_xfer_size) {
491 1.1 skrll dev_err(hsotg->dev,
492 1.1 skrll "%s: Periodic xfer length %d > max xfer length for channel %d\n",
493 1.1 skrll __func__, max_xfer_size, max_channel_xfer_size);
494 1.1 skrll status = -ENOSPC;
495 1.1 skrll }
496 1.1 skrll
497 1.1 skrll return status;
498 1.1 skrll }
499 1.1 skrll
500 1.1 skrll /**
501 1.1 skrll * dwc2_schedule_periodic() - Schedules an interrupt or isochronous transfer in
502 1.1 skrll * the periodic schedule
503 1.1 skrll *
504 1.1 skrll * @hsotg: The HCD state structure for the DWC OTG controller
505 1.1 skrll * @qh: QH for the periodic transfer. The QH should already contain the
506 1.1 skrll * scheduling information.
507 1.1 skrll *
508 1.1 skrll * Return: 0 if successful, negative error code otherwise
509 1.1 skrll */
510 1.1 skrll static int dwc2_schedule_periodic(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
511 1.1 skrll {
512 1.1 skrll int status;
513 1.1 skrll
514 1.1 skrll if (hsotg->core_params->uframe_sched > 0) {
515 1.1 skrll int frame = -1;
516 1.1 skrll
517 1.1 skrll status = dwc2_find_uframe(hsotg, qh);
518 1.1 skrll if (status == 0)
519 1.1 skrll frame = 7;
520 1.1 skrll else if (status > 0)
521 1.1 skrll frame = status - 1;
522 1.1 skrll
523 1.1 skrll /* Set the new frame up */
524 1.9 skrll if (frame >= 0) {
525 1.1 skrll qh->sched_frame &= ~0x7;
526 1.1 skrll qh->sched_frame |= (frame & 7);
527 1.1 skrll }
528 1.1 skrll
529 1.9 skrll if (status > 0)
530 1.1 skrll status = 0;
531 1.1 skrll } else {
532 1.1 skrll status = dwc2_periodic_channel_available(hsotg);
533 1.1 skrll if (status) {
534 1.1 skrll dev_info(hsotg->dev,
535 1.1 skrll "%s: No host channel available for periodic transfer\n",
536 1.1 skrll __func__);
537 1.1 skrll return status;
538 1.1 skrll }
539 1.1 skrll
540 1.1 skrll status = dwc2_check_periodic_bandwidth(hsotg, qh);
541 1.1 skrll }
542 1.1 skrll
543 1.1 skrll if (status) {
544 1.1 skrll dev_dbg(hsotg->dev,
545 1.1 skrll "%s: Insufficient periodic bandwidth for periodic transfer\n",
546 1.1 skrll __func__);
547 1.1 skrll return status;
548 1.1 skrll }
549 1.1 skrll
550 1.1 skrll status = dwc2_check_max_xfer_size(hsotg, qh);
551 1.1 skrll if (status) {
552 1.1 skrll dev_dbg(hsotg->dev,
553 1.1 skrll "%s: Channel max transfer size too small for periodic transfer\n",
554 1.1 skrll __func__);
555 1.1 skrll return status;
556 1.1 skrll }
557 1.1 skrll
558 1.3 skrll if (hsotg->core_params->dma_desc_enable > 0)
559 1.1 skrll /* Don't rely on SOF and start in ready schedule */
560 1.1 skrll list_add_tail(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
561 1.3 skrll else
562 1.1 skrll /* Always start in inactive schedule */
563 1.1 skrll list_add_tail(&qh->qh_list_entry,
564 1.1 skrll &hsotg->periodic_sched_inactive);
565 1.1 skrll
566 1.1 skrll if (hsotg->core_params->uframe_sched <= 0)
567 1.1 skrll /* Reserve periodic channel */
568 1.1 skrll hsotg->periodic_channels++;
569 1.1 skrll
570 1.1 skrll /* Update claimed usecs per (micro)frame */
571 1.1 skrll hsotg->periodic_usecs += qh->usecs;
572 1.1 skrll
573 1.1 skrll return status;
574 1.1 skrll }
575 1.1 skrll
576 1.1 skrll /**
577 1.1 skrll * dwc2_deschedule_periodic() - Removes an interrupt or isochronous transfer
578 1.1 skrll * from the periodic schedule
579 1.1 skrll *
580 1.1 skrll * @hsotg: The HCD state structure for the DWC OTG controller
581 1.1 skrll * @qh: QH for the periodic transfer
582 1.1 skrll */
583 1.1 skrll static void dwc2_deschedule_periodic(struct dwc2_hsotg *hsotg,
584 1.1 skrll struct dwc2_qh *qh)
585 1.1 skrll {
586 1.1 skrll int i;
587 1.1 skrll
588 1.1 skrll list_del_init(&qh->qh_list_entry);
589 1.1 skrll
590 1.1 skrll /* Update claimed usecs per (micro)frame */
591 1.1 skrll hsotg->periodic_usecs -= qh->usecs;
592 1.1 skrll
593 1.1 skrll if (hsotg->core_params->uframe_sched > 0) {
594 1.1 skrll for (i = 0; i < 8; i++) {
595 1.1 skrll hsotg->frame_usecs[i] += qh->frame_usecs[i];
596 1.1 skrll qh->frame_usecs[i] = 0;
597 1.1 skrll }
598 1.1 skrll } else {
599 1.1 skrll /* Release periodic channel reservation */
600 1.1 skrll hsotg->periodic_channels--;
601 1.1 skrll }
602 1.1 skrll }
603 1.1 skrll
604 1.1 skrll /**
605 1.15 simonb * dwc2_wait_timer_fn() - Timer function to re-queue after waiting
606 1.15 simonb *
607 1.15 simonb * As per the spec, a NAK indicates that "a function is temporarily unable to
608 1.15 simonb * transmit or receive data, but will eventually be able to do so without need
609 1.15 simonb * of host intervention".
610 1.15 simonb *
611 1.15 simonb * That means that when we encounter a NAK we're supposed to retry.
612 1.15 simonb *
613 1.15 simonb * ...but if we retry right away (from the interrupt handler that saw the NAK)
614 1.15 simonb * then we can end up with an interrupt storm (if the other side keeps NAKing
615 1.15 simonb * us) because on slow enough CPUs it could take us longer to get out of the
616 1.15 simonb * interrupt routine than it takes for the device to send another NAK. That
617 1.15 simonb * leads to a constant stream of NAK interrupts and the CPU locks.
618 1.15 simonb *
619 1.15 simonb * ...so instead of retrying right away in the case of a NAK we'll set a timer
620 1.15 simonb * to retry some time later. This function handles that timer and moves the
621 1.15 simonb * qh back to the "inactive" list, then queues transactions.
622 1.15 simonb *
623 1.15 simonb * @t: Pointer to wait_timer in a qh.
624 1.15 simonb */
625 1.15 simonb static void dwc2_wait_timer_fn(void *arg)
626 1.15 simonb {
627 1.15 simonb struct dwc2_qh *qh = arg;
628 1.15 simonb struct dwc2_hsotg *hsotg = qh->hsotg;
629 1.15 simonb unsigned long flags;
630 1.15 simonb
631 1.15 simonb spin_lock_irqsave(&hsotg->lock, flags);
632 1.15 simonb
633 1.15 simonb /*
634 1.15 simonb * We'll set wait_timer_cancel to true if we want to cancel this
635 1.15 simonb * operation in dwc2_hcd_qh_unlink().
636 1.15 simonb */
637 1.15 simonb if (!qh->wait_timer_cancel) {
638 1.15 simonb enum dwc2_transaction_type tr_type;
639 1.15 simonb
640 1.15 simonb qh->want_wait = false;
641 1.15 simonb
642 1.15 simonb list_move(&qh->qh_list_entry,
643 1.15 simonb &hsotg->non_periodic_sched_inactive);
644 1.15 simonb
645 1.15 simonb tr_type = dwc2_hcd_select_transactions(hsotg);
646 1.15 simonb if (tr_type != DWC2_TRANSACTION_NONE)
647 1.15 simonb dwc2_hcd_queue_transactions(hsotg, tr_type);
648 1.15 simonb }
649 1.15 simonb
650 1.15 simonb spin_unlock_irqrestore(&hsotg->lock, flags);
651 1.15 simonb }
652 1.15 simonb
653 1.15 simonb /**
654 1.1 skrll * dwc2_hcd_qh_add() - Adds a QH to either the non periodic or periodic
655 1.1 skrll * schedule if it is not already in the schedule. If the QH is already in
656 1.1 skrll * the schedule, no action is taken.
657 1.1 skrll *
658 1.1 skrll * @hsotg: The HCD state structure for the DWC OTG controller
659 1.1 skrll * @qh: The QH to add
660 1.1 skrll *
661 1.1 skrll * Return: 0 if successful, negative error code otherwise
662 1.1 skrll */
663 1.1 skrll int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
664 1.1 skrll {
665 1.9 skrll int status;
666 1.1 skrll u32 intr_mask;
667 1.1 skrll
668 1.1 skrll if (dbg_qh(qh))
669 1.1 skrll dev_vdbg(hsotg->dev, "%s()\n", __func__);
670 1.1 skrll
671 1.1 skrll if (!list_empty(&qh->qh_list_entry))
672 1.1 skrll /* QH already in a schedule */
673 1.9 skrll return 0;
674 1.1 skrll
675 1.14 skrll if (!dwc2_frame_num_le(qh->sched_frame, hsotg->frame_number) &&
676 1.14 skrll !hsotg->frame_number) {
677 1.14 skrll dev_dbg(hsotg->dev,
678 1.14 skrll "reset frame number counter\n");
679 1.14 skrll qh->sched_frame = dwc2_frame_num_inc(hsotg->frame_number,
680 1.14 skrll SCHEDULE_SLOP);
681 1.14 skrll }
682 1.14 skrll
683 1.1 skrll /* Add the new QH to the appropriate schedule */
684 1.1 skrll if (dwc2_qh_is_non_per(qh)) {
685 1.15 simonb if (qh->want_wait) {
686 1.15 simonb list_add_tail(&qh->qh_list_entry,
687 1.15 simonb &hsotg->non_periodic_sched_waiting);
688 1.15 simonb qh->wait_timer_cancel = false;
689 1.15 simonb /* XXX mod_timer(&qh->wait_timer,
690 1.15 simonb jiffies + DWC2_RETRY_WAIT_DELAY + 1); */
691 1.15 simonb callout_schedule(&qh->wait_timer,
692 1.15 simonb mstohz(DWC2_RETRY_WAIT_DELAY));
693 1.15 simonb } else {
694 1.15 simonb list_add_tail(&qh->qh_list_entry,
695 1.15 simonb &hsotg->non_periodic_sched_inactive);
696 1.15 simonb }
697 1.9 skrll return 0;
698 1.9 skrll }
699 1.12 skrll
700 1.9 skrll status = dwc2_schedule_periodic(hsotg, qh);
701 1.9 skrll if (status)
702 1.9 skrll return status;
703 1.9 skrll if (!hsotg->periodic_qh_count) {
704 1.9 skrll intr_mask = DWC2_READ_4(hsotg, GINTMSK);
705 1.9 skrll intr_mask |= GINTSTS_SOF;
706 1.9 skrll DWC2_WRITE_4(hsotg, GINTMSK, intr_mask);
707 1.1 skrll }
708 1.9 skrll hsotg->periodic_qh_count++;
709 1.1 skrll
710 1.9 skrll return 0;
711 1.1 skrll }
712 1.1 skrll
713 1.1 skrll /**
714 1.1 skrll * dwc2_hcd_qh_unlink() - Removes a QH from either the non-periodic or periodic
715 1.1 skrll * schedule. Memory is not freed.
716 1.1 skrll *
717 1.1 skrll * @hsotg: The HCD state structure
718 1.1 skrll * @qh: QH to remove from schedule
719 1.1 skrll */
720 1.1 skrll void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
721 1.1 skrll {
722 1.1 skrll u32 intr_mask;
723 1.1 skrll
724 1.1 skrll dev_vdbg(hsotg->dev, "%s()\n", __func__);
725 1.1 skrll
726 1.15 simonb /* If the wait_timer is pending, this will stop it from acting */
727 1.15 simonb qh->wait_timer_cancel = true;
728 1.15 simonb
729 1.1 skrll if (list_empty(&qh->qh_list_entry))
730 1.1 skrll /* QH is not in a schedule */
731 1.1 skrll return;
732 1.1 skrll
733 1.1 skrll if (dwc2_qh_is_non_per(qh)) {
734 1.1 skrll if (hsotg->non_periodic_qh_ptr == &qh->qh_list_entry)
735 1.1 skrll hsotg->non_periodic_qh_ptr =
736 1.1 skrll hsotg->non_periodic_qh_ptr->next;
737 1.1 skrll list_del_init(&qh->qh_list_entry);
738 1.9 skrll return;
739 1.9 skrll }
740 1.12 skrll
741 1.9 skrll dwc2_deschedule_periodic(hsotg, qh);
742 1.9 skrll hsotg->periodic_qh_count--;
743 1.9 skrll if (!hsotg->periodic_qh_count) {
744 1.9 skrll intr_mask = DWC2_READ_4(hsotg, GINTMSK);
745 1.9 skrll intr_mask &= ~GINTSTS_SOF;
746 1.9 skrll DWC2_WRITE_4(hsotg, GINTMSK, intr_mask);
747 1.1 skrll }
748 1.1 skrll }
749 1.1 skrll
750 1.1 skrll /*
751 1.1 skrll * Schedule the next continuing periodic split transfer
752 1.1 skrll */
753 1.1 skrll static void dwc2_sched_periodic_split(struct dwc2_hsotg *hsotg,
754 1.1 skrll struct dwc2_qh *qh, u16 frame_number,
755 1.1 skrll int sched_next_periodic_split)
756 1.1 skrll {
757 1.1 skrll u16 incr;
758 1.1 skrll
759 1.1 skrll if (sched_next_periodic_split) {
760 1.1 skrll qh->sched_frame = frame_number;
761 1.1 skrll incr = dwc2_frame_num_inc(qh->start_split_frame, 1);
762 1.1 skrll if (dwc2_frame_num_le(frame_number, incr)) {
763 1.1 skrll /*
764 1.1 skrll * Allow one frame to elapse after start split
765 1.1 skrll * microframe before scheduling complete split, but
766 1.1 skrll * DON'T if we are doing the next start split in the
767 1.1 skrll * same frame for an ISOC out
768 1.1 skrll */
769 1.1 skrll if (qh->ep_type != USB_ENDPOINT_XFER_ISOC ||
770 1.1 skrll qh->ep_is_in != 0) {
771 1.1 skrll qh->sched_frame =
772 1.1 skrll dwc2_frame_num_inc(qh->sched_frame, 1);
773 1.1 skrll }
774 1.1 skrll }
775 1.1 skrll } else {
776 1.1 skrll qh->sched_frame = dwc2_frame_num_inc(qh->start_split_frame,
777 1.1 skrll qh->interval);
778 1.1 skrll if (dwc2_frame_num_le(qh->sched_frame, frame_number))
779 1.1 skrll qh->sched_frame = frame_number;
780 1.1 skrll qh->sched_frame |= 0x7;
781 1.1 skrll qh->start_split_frame = qh->sched_frame;
782 1.1 skrll }
783 1.1 skrll }
784 1.1 skrll
785 1.1 skrll /*
786 1.1 skrll * Deactivates a QH. For non-periodic QHs, removes the QH from the active
787 1.1 skrll * non-periodic schedule. The QH is added to the inactive non-periodic
788 1.1 skrll * schedule if any QTDs are still attached to the QH.
789 1.1 skrll *
790 1.1 skrll * For periodic QHs, the QH is removed from the periodic queued schedule. If
791 1.1 skrll * there are any QTDs still attached to the QH, the QH is added to either the
792 1.1 skrll * periodic inactive schedule or the periodic ready schedule and its next
793 1.1 skrll * scheduled frame is calculated. The QH is placed in the ready schedule if
794 1.1 skrll * the scheduled frame has been reached already. Otherwise it's placed in the
795 1.1 skrll * inactive schedule. If there are no QTDs attached to the QH, the QH is
796 1.1 skrll * completely removed from the periodic schedule.
797 1.1 skrll */
798 1.1 skrll void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
799 1.1 skrll int sched_next_periodic_split)
800 1.1 skrll {
801 1.9 skrll u16 frame_number;
802 1.9 skrll
803 1.1 skrll if (dbg_qh(qh))
804 1.1 skrll dev_vdbg(hsotg->dev, "%s()\n", __func__);
805 1.1 skrll
806 1.1 skrll if (dwc2_qh_is_non_per(qh)) {
807 1.1 skrll dwc2_hcd_qh_unlink(hsotg, qh);
808 1.1 skrll if (!list_empty(&qh->qtd_list))
809 1.15 simonb /* Add back to inactive/waiting non-periodic schedule */
810 1.1 skrll dwc2_hcd_qh_add(hsotg, qh);
811 1.9 skrll return;
812 1.9 skrll }
813 1.9 skrll
814 1.9 skrll frame_number = dwc2_hcd_get_frame_number(hsotg);
815 1.9 skrll
816 1.9 skrll if (qh->do_split) {
817 1.9 skrll dwc2_sched_periodic_split(hsotg, qh, frame_number,
818 1.9 skrll sched_next_periodic_split);
819 1.1 skrll } else {
820 1.9 skrll qh->sched_frame = dwc2_frame_num_inc(qh->sched_frame,
821 1.9 skrll qh->interval);
822 1.9 skrll if (dwc2_frame_num_le(qh->sched_frame, frame_number))
823 1.9 skrll qh->sched_frame = frame_number;
824 1.9 skrll }
825 1.1 skrll
826 1.9 skrll if (list_empty(&qh->qtd_list)) {
827 1.9 skrll dwc2_hcd_qh_unlink(hsotg, qh);
828 1.9 skrll return;
829 1.1 skrll }
830 1.9 skrll /*
831 1.9 skrll * Remove from periodic_sched_queued and move to
832 1.9 skrll * appropriate queue
833 1.9 skrll */
834 1.9 skrll if ((hsotg->core_params->uframe_sched > 0 &&
835 1.9 skrll dwc2_frame_num_le(qh->sched_frame, frame_number)) ||
836 1.9 skrll (hsotg->core_params->uframe_sched <= 0 &&
837 1.9 skrll qh->sched_frame == frame_number))
838 1.9 skrll list_move(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
839 1.9 skrll else
840 1.9 skrll list_move(&qh->qh_list_entry, &hsotg->periodic_sched_inactive);
841 1.1 skrll }
842 1.1 skrll
843 1.1 skrll /**
844 1.1 skrll * dwc2_hcd_qtd_init() - Initializes a QTD structure
845 1.1 skrll *
846 1.1 skrll * @qtd: The QTD to initialize
847 1.1 skrll * @urb: The associated URB
848 1.1 skrll */
849 1.1 skrll void dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
850 1.1 skrll {
851 1.1 skrll qtd->urb = urb;
852 1.1 skrll if (dwc2_hcd_get_pipe_type(&urb->pipe_info) ==
853 1.1 skrll USB_ENDPOINT_XFER_CONTROL) {
854 1.1 skrll /*
855 1.1 skrll * The only time the QTD data toggle is used is on the data
856 1.1 skrll * phase of control transfers. This phase always starts with
857 1.1 skrll * DATA1.
858 1.1 skrll */
859 1.1 skrll qtd->data_toggle = DWC2_HC_PID_DATA1;
860 1.1 skrll qtd->control_phase = DWC2_CONTROL_SETUP;
861 1.1 skrll }
862 1.1 skrll
863 1.1 skrll /* Start split */
864 1.1 skrll qtd->complete_split = 0;
865 1.1 skrll qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
866 1.1 skrll qtd->isoc_split_offset = 0;
867 1.1 skrll qtd->in_process = 0;
868 1.1 skrll
869 1.1 skrll /* Store the qtd ptr in the urb to reference the QTD */
870 1.1 skrll urb->qtd = qtd;
871 1.1 skrll }
872 1.1 skrll
873 1.1 skrll /**
874 1.1 skrll * dwc2_hcd_qtd_add() - Adds a QTD to the QTD-list of a QH
875 1.12 skrll * Caller must hold driver lock.
876 1.1 skrll *
877 1.12 skrll * @hsotg: The DWC HCD structure
878 1.12 skrll * @qtd: The QTD to add
879 1.12 skrll * @qh: Queue head to add qtd to
880 1.1 skrll *
881 1.1 skrll * Return: 0 if successful, negative error code otherwise
882 1.1 skrll *
883 1.12 skrll * If the QH to which the QTD is added is not currently scheduled, it is placed
884 1.12 skrll * into the proper schedule based on its EP type.
885 1.1 skrll */
886 1.1 skrll int dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
887 1.12 skrll struct dwc2_qh *qh)
888 1.1 skrll {
889 1.12 skrll
890 1.13 uebayasi KASSERT(mutex_owned(&hsotg->lock));
891 1.1 skrll int retval;
892 1.1 skrll
893 1.12 skrll if (unlikely(!qh)) {
894 1.12 skrll dev_err(hsotg->dev, "%s: Invalid QH\n", __func__);
895 1.12 skrll retval = -EINVAL;
896 1.12 skrll goto fail;
897 1.1 skrll }
898 1.1 skrll
899 1.12 skrll retval = dwc2_hcd_qh_add(hsotg, qh);
900 1.1 skrll if (retval)
901 1.1 skrll goto fail;
902 1.1 skrll
903 1.12 skrll qtd->qh = qh;
904 1.12 skrll list_add_tail(&qtd->qtd_list_entry, &qh->qtd_list);
905 1.1 skrll
906 1.1 skrll return 0;
907 1.1 skrll fail:
908 1.1 skrll return retval;
909 1.1 skrll }
910 1.2 skrll
911 1.2 skrll void dwc2_hcd_qtd_unlink_and_free(struct dwc2_hsotg *hsotg,
912 1.2 skrll struct dwc2_qtd *qtd,
913 1.2 skrll struct dwc2_qh *qh)
914 1.2 skrll {
915 1.2 skrll struct dwc2_softc *sc = hsotg->hsotg_sc;
916 1.10 skrll
917 1.2 skrll list_del_init(&qtd->qtd_list_entry);
918 1.2 skrll pool_cache_put(sc->sc_qtdpool, qtd);
919 1.2 skrll }
920 1.2 skrll
921 1.2 skrll #define BITSTUFFTIME(bytecount) ((8 * 7 * (bytecount)) / 6)
922 1.2 skrll #define HS_HOST_DELAY 5 /* nanoseconds */
923 1.2 skrll #define FS_LS_HOST_DELAY 1000 /* nanoseconds */
924 1.2 skrll #define HUB_LS_SETUP 333 /* nanoseconds */
925 1.2 skrll
926 1.2 skrll static u32 dwc2_calc_bus_time(struct dwc2_hsotg *hsotg, int speed, int is_in,
927 1.2 skrll int is_isoc, int bytecount)
928 1.2 skrll {
929 1.2 skrll unsigned long retval;
930 1.2 skrll
931 1.2 skrll switch (speed) {
932 1.2 skrll case USB_SPEED_HIGH:
933 1.2 skrll if (is_isoc)
934 1.2 skrll retval =
935 1.2 skrll ((38 * 8 * 2083) +
936 1.2 skrll (2083 * (3 + BITSTUFFTIME(bytecount)))) / 1000 +
937 1.2 skrll HS_HOST_DELAY;
938 1.2 skrll else
939 1.2 skrll retval =
940 1.2 skrll ((55 * 8 * 2083) +
941 1.2 skrll (2083 * (3 + BITSTUFFTIME(bytecount)))) / 1000 +
942 1.2 skrll HS_HOST_DELAY;
943 1.2 skrll break;
944 1.2 skrll case USB_SPEED_FULL:
945 1.2 skrll if (is_isoc) {
946 1.2 skrll retval =
947 1.2 skrll (8354 * (31 + 10 * BITSTUFFTIME(bytecount))) / 1000;
948 1.2 skrll if (is_in)
949 1.2 skrll retval = 7268 + FS_LS_HOST_DELAY + retval;
950 1.2 skrll else
951 1.2 skrll retval = 6265 + FS_LS_HOST_DELAY + retval;
952 1.2 skrll } else {
953 1.2 skrll retval =
954 1.2 skrll (8354 * (31 + 10 * BITSTUFFTIME(bytecount))) / 1000;
955 1.2 skrll retval = 9107 + FS_LS_HOST_DELAY + retval;
956 1.2 skrll }
957 1.2 skrll break;
958 1.2 skrll case USB_SPEED_LOW:
959 1.2 skrll if (is_in) {
960 1.2 skrll retval =
961 1.2 skrll (67667 * (31 + 10 * BITSTUFFTIME(bytecount))) /
962 1.2 skrll 1000;
963 1.2 skrll retval =
964 1.2 skrll 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
965 1.2 skrll retval;
966 1.2 skrll } else {
967 1.2 skrll retval =
968 1.2 skrll (66700 * (31 + 10 * BITSTUFFTIME(bytecount))) /
969 1.2 skrll 1000;
970 1.2 skrll retval =
971 1.2 skrll 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
972 1.2 skrll retval;
973 1.2 skrll }
974 1.2 skrll break;
975 1.2 skrll default:
976 1.2 skrll dev_warn(hsotg->dev, "Unknown device speed\n");
977 1.2 skrll retval = -1;
978 1.2 skrll }
979 1.2 skrll
980 1.2 skrll return NS_TO_US(retval);
981 1.2 skrll }
982