blob: bc632a72f611185bbfebbf7aacc4ddc2add5507f [file] [log] [blame]
Paul Zimmerman7359d482013-03-11 17:47:59 -07001/*
2 * hcd_queue.c - DesignWare HS OTG Controller host queuing routines
3 *
4 * Copyright (C) 2004-2013 Synopsys, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the above-listed copyright holders may not be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * ALTERNATIVELY, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") as published by the Free Software
21 * Foundation; either version 2 of the License, or (at your option) any
22 * later version.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37/*
38 * This file contains the functions to manage Queue Heads and Queue
39 * Transfer Descriptors for Host mode
40 */
41#include <linux/kernel.h>
42#include <linux/module.h>
43#include <linux/spinlock.h>
44#include <linux/interrupt.h>
45#include <linux/dma-mapping.h>
46#include <linux/io.h>
47#include <linux/slab.h>
48#include <linux/usb.h>
49
50#include <linux/usb/hcd.h>
51#include <linux/usb/ch11.h>
52
53#include "core.h"
54#include "hcd.h"
55
56/**
57 * dwc2_qh_init() - Initializes a QH structure
58 *
59 * @hsotg: The HCD state structure for the DWC OTG controller
60 * @qh: The QH to init
61 * @urb: Holds the information about the device/endpoint needed to initialize
62 * the QH
63 */
64#define SCHEDULE_SLOP 10
65static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
66 struct dwc2_hcd_urb *urb)
67{
68 int dev_speed, hub_addr, hub_port;
69 char *speed, *type;
70
71 dev_vdbg(hsotg->dev, "%s()\n", __func__);
72
73 /* Initialize QH */
74 qh->ep_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
75 qh->ep_is_in = dwc2_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
76
77 qh->data_toggle = DWC2_HC_PID_DATA0;
78 qh->maxp = dwc2_hcd_get_mps(&urb->pipe_info);
79 INIT_LIST_HEAD(&qh->qtd_list);
80 INIT_LIST_HEAD(&qh->qh_list_entry);
81
82 /* FS/LS Endpoint on HS Hub, NOT virtual root hub */
83 dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
84
85 dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
86
87 if ((dev_speed == USB_SPEED_LOW || dev_speed == USB_SPEED_FULL) &&
88 hub_addr != 0 && hub_addr != 1) {
89 dev_vdbg(hsotg->dev,
90 "QH init: EP %d: TT found at hub addr %d, for port %d\n",
91 dwc2_hcd_get_ep_num(&urb->pipe_info), hub_addr,
92 hub_port);
93 qh->do_split = 1;
94 }
95
96 if (qh->ep_type == USB_ENDPOINT_XFER_INT ||
97 qh->ep_type == USB_ENDPOINT_XFER_ISOC) {
98 /* Compute scheduling parameters once and save them */
99 u32 hprt, prtspd;
100
101 /* Todo: Account for split transfers in the bus time */
102 int bytecount =
103 dwc2_hb_mult(qh->maxp) * dwc2_max_packet(qh->maxp);
104
105 qh->usecs = NS_TO_US(usb_calc_bus_time(qh->do_split ?
106 USB_SPEED_HIGH : dev_speed, qh->ep_is_in,
107 qh->ep_type == USB_ENDPOINT_XFER_ISOC,
108 bytecount));
Gregory Herrerodd81dd72015-09-22 15:16:52 +0200109
110 /* Ensure frame_number corresponds to the reality */
111 hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700112 /* Start in a slightly future (micro)frame */
113 qh->sched_frame = dwc2_frame_num_inc(hsotg->frame_number,
114 SCHEDULE_SLOP);
115 qh->interval = urb->interval;
116#if 0
117 /* Increase interrupt polling rate for debugging */
118 if (qh->ep_type == USB_ENDPOINT_XFER_INT)
119 qh->interval = 8;
120#endif
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300121 hprt = dwc2_readl(hsotg->regs + HPRT0);
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200122 prtspd = (hprt & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700123 if (prtspd == HPRT0_SPD_HIGH_SPEED &&
124 (dev_speed == USB_SPEED_LOW ||
125 dev_speed == USB_SPEED_FULL)) {
126 qh->interval *= 8;
127 qh->sched_frame |= 0x7;
128 qh->start_split_frame = qh->sched_frame;
129 }
130 dev_dbg(hsotg->dev, "interval=%d\n", qh->interval);
131 }
132
133 dev_vdbg(hsotg->dev, "DWC OTG HCD QH Initialized\n");
134 dev_vdbg(hsotg->dev, "DWC OTG HCD QH - qh = %p\n", qh);
135 dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Device Address = %d\n",
136 dwc2_hcd_get_dev_addr(&urb->pipe_info));
137 dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Endpoint %d, %s\n",
138 dwc2_hcd_get_ep_num(&urb->pipe_info),
139 dwc2_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
140
141 qh->dev_speed = dev_speed;
142
143 switch (dev_speed) {
144 case USB_SPEED_LOW:
145 speed = "low";
146 break;
147 case USB_SPEED_FULL:
148 speed = "full";
149 break;
150 case USB_SPEED_HIGH:
151 speed = "high";
152 break;
153 default:
154 speed = "?";
155 break;
156 }
157 dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Speed = %s\n", speed);
158
159 switch (qh->ep_type) {
160 case USB_ENDPOINT_XFER_ISOC:
161 type = "isochronous";
162 break;
163 case USB_ENDPOINT_XFER_INT:
164 type = "interrupt";
165 break;
166 case USB_ENDPOINT_XFER_CONTROL:
167 type = "control";
168 break;
169 case USB_ENDPOINT_XFER_BULK:
170 type = "bulk";
171 break;
172 default:
173 type = "?";
174 break;
175 }
176
177 dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Type = %s\n", type);
178
179 if (qh->ep_type == USB_ENDPOINT_XFER_INT) {
180 dev_vdbg(hsotg->dev, "DWC OTG HCD QH - usecs = %d\n",
181 qh->usecs);
182 dev_vdbg(hsotg->dev, "DWC OTG HCD QH - interval = %d\n",
183 qh->interval);
184 }
185}
186
187/**
188 * dwc2_hcd_qh_create() - Allocates and initializes a QH
189 *
190 * @hsotg: The HCD state structure for the DWC OTG controller
191 * @urb: Holds the information about the device/endpoint needed
192 * to initialize the QH
193 * @atomic_alloc: Flag to do atomic allocation if needed
194 *
195 * Return: Pointer to the newly allocated QH, or NULL on error
196 */
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +0200197struct dwc2_qh *dwc2_hcd_qh_create(struct dwc2_hsotg *hsotg,
Paul Zimmerman7359d482013-03-11 17:47:59 -0700198 struct dwc2_hcd_urb *urb,
199 gfp_t mem_flags)
200{
201 struct dwc2_qh *qh;
202
Paul Zimmermanb2d6cb52013-07-13 14:53:51 -0700203 if (!urb->priv)
204 return NULL;
205
Paul Zimmerman7359d482013-03-11 17:47:59 -0700206 /* Allocate memory */
207 qh = kzalloc(sizeof(*qh), mem_flags);
208 if (!qh)
209 return NULL;
210
211 dwc2_qh_init(hsotg, qh, urb);
212
213 if (hsotg->core_params->dma_desc_enable > 0 &&
214 dwc2_hcd_qh_init_ddma(hsotg, qh, mem_flags) < 0) {
215 dwc2_hcd_qh_free(hsotg, qh);
216 return NULL;
217 }
218
219 return qh;
220}
221
222/**
223 * dwc2_hcd_qh_free() - Frees the QH
224 *
225 * @hsotg: HCD instance
226 * @qh: The QH to free
227 *
228 * QH should already be removed from the list. QTD list should already be empty
229 * if called from URB Dequeue.
230 *
231 * Must NOT be called with interrupt disabled or spinlock held
232 */
233void dwc2_hcd_qh_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
234{
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800235 if (qh->desc_list)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700236 dwc2_hcd_qh_free_ddma(hsotg, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700237 kfree(qh);
238}
239
240/**
241 * dwc2_periodic_channel_available() - Checks that a channel is available for a
242 * periodic transfer
243 *
244 * @hsotg: The HCD state structure for the DWC OTG controller
245 *
Masanari Iida0dcde5082013-09-13 23:34:36 +0900246 * Return: 0 if successful, negative error code otherwise
Paul Zimmerman7359d482013-03-11 17:47:59 -0700247 */
248static int dwc2_periodic_channel_available(struct dwc2_hsotg *hsotg)
249{
250 /*
Masanari Iida0dcde5082013-09-13 23:34:36 +0900251 * Currently assuming that there is a dedicated host channel for
Paul Zimmerman7359d482013-03-11 17:47:59 -0700252 * each periodic transaction plus at least one host channel for
253 * non-periodic transactions
254 */
255 int status;
256 int num_channels;
257
258 num_channels = hsotg->core_params->host_channels;
259 if (hsotg->periodic_channels + hsotg->non_periodic_channels <
260 num_channels
261 && hsotg->periodic_channels < num_channels - 1) {
262 status = 0;
263 } else {
264 dev_dbg(hsotg->dev,
265 "%s: Total channels: %d, Periodic: %d, "
266 "Non-periodic: %d\n", __func__, num_channels,
267 hsotg->periodic_channels, hsotg->non_periodic_channels);
268 status = -ENOSPC;
269 }
270
271 return status;
272}
273
274/**
275 * dwc2_check_periodic_bandwidth() - Checks that there is sufficient bandwidth
276 * for the specified QH in the periodic schedule
277 *
278 * @hsotg: The HCD state structure for the DWC OTG controller
279 * @qh: QH containing periodic bandwidth required
280 *
281 * Return: 0 if successful, negative error code otherwise
282 *
283 * For simplicity, this calculation assumes that all the transfers in the
284 * periodic schedule may occur in the same (micro)frame
285 */
286static int dwc2_check_periodic_bandwidth(struct dwc2_hsotg *hsotg,
287 struct dwc2_qh *qh)
288{
289 int status;
290 s16 max_claimed_usecs;
291
292 status = 0;
293
294 if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
295 /*
296 * High speed mode
297 * Max periodic usecs is 80% x 125 usec = 100 usec
298 */
299 max_claimed_usecs = 100 - qh->usecs;
300 } else {
301 /*
302 * Full speed mode
303 * Max periodic usecs is 90% x 1000 usec = 900 usec
304 */
305 max_claimed_usecs = 900 - qh->usecs;
306 }
307
308 if (hsotg->periodic_usecs > max_claimed_usecs) {
309 dev_err(hsotg->dev,
310 "%s: already claimed usecs %d, required usecs %d\n",
311 __func__, hsotg->periodic_usecs, qh->usecs);
312 status = -ENOSPC;
313 }
314
315 return status;
316}
317
318/**
Dom Cobley20f2eb92013-09-23 14:23:34 -0700319 * Microframe scheduler
320 * track the total use in hsotg->frame_usecs
321 * keep each qh use in qh->frame_usecs
322 * when surrendering the qh then donate the time back
323 */
324static const unsigned short max_uframe_usecs[] = {
325 100, 100, 100, 100, 100, 100, 30, 0
326};
327
328void dwc2_hcd_init_usecs(struct dwc2_hsotg *hsotg)
329{
330 int i;
331
332 for (i = 0; i < 8; i++)
333 hsotg->frame_usecs[i] = max_uframe_usecs[i];
334}
335
336static int dwc2_find_single_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
337{
338 unsigned short utime = qh->usecs;
Himangi Saraogi86c17c02013-11-02 10:05:30 +0530339 int i;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700340
Himangi Saraogi86c17c02013-11-02 10:05:30 +0530341 for (i = 0; i < 8; i++) {
Dom Cobley20f2eb92013-09-23 14:23:34 -0700342 /* At the start hsotg->frame_usecs[i] = max_uframe_usecs[i] */
343 if (utime <= hsotg->frame_usecs[i]) {
344 hsotg->frame_usecs[i] -= utime;
345 qh->frame_usecs[i] += utime;
Himangi Saraogi86c17c02013-11-02 10:05:30 +0530346 return i;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700347 }
348 }
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -0800349 return -ENOSPC;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700350}
351
352/*
353 * use this for FS apps that can span multiple uframes
354 */
355static int dwc2_find_multi_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
356{
357 unsigned short utime = qh->usecs;
358 unsigned short xtime;
Himangi Saraogi86c17c02013-11-02 10:05:30 +0530359 int t_left;
360 int i;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700361 int j;
Himangi Saraogi86c17c02013-11-02 10:05:30 +0530362 int k;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700363
Himangi Saraogi86c17c02013-11-02 10:05:30 +0530364 for (i = 0; i < 8; i++) {
365 if (hsotg->frame_usecs[i] <= 0)
Dom Cobley20f2eb92013-09-23 14:23:34 -0700366 continue;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700367
368 /*
369 * we need n consecutive slots so use j as a start slot
370 * j plus j+1 must be enough time (for now)
371 */
372 xtime = hsotg->frame_usecs[i];
373 for (j = i + 1; j < 8; j++) {
374 /*
375 * if we add this frame remaining time to xtime we may
376 * be OK, if not we need to test j for a complete frame
377 */
378 if (xtime + hsotg->frame_usecs[j] < utime) {
379 if (hsotg->frame_usecs[j] <
Himangi Saraogi86c17c02013-11-02 10:05:30 +0530380 max_uframe_usecs[j])
381 continue;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700382 }
383 if (xtime >= utime) {
Himangi Saraogi86c17c02013-11-02 10:05:30 +0530384 t_left = utime;
385 for (k = i; k < 8; k++) {
386 t_left -= hsotg->frame_usecs[k];
387 if (t_left <= 0) {
388 qh->frame_usecs[k] +=
389 hsotg->frame_usecs[k]
390 + t_left;
391 hsotg->frame_usecs[k] = -t_left;
392 return i;
393 } else {
394 qh->frame_usecs[k] +=
395 hsotg->frame_usecs[k];
396 hsotg->frame_usecs[k] = 0;
397 }
398 }
Dom Cobley20f2eb92013-09-23 14:23:34 -0700399 }
400 /* add the frame time to x time */
401 xtime += hsotg->frame_usecs[j];
402 /* we must have a fully available next frame or break */
403 if (xtime < utime &&
Himangi Saraogi86c17c02013-11-02 10:05:30 +0530404 hsotg->frame_usecs[j] == max_uframe_usecs[j])
405 continue;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700406 }
407 }
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -0800408 return -ENOSPC;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700409}
410
411static int dwc2_find_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
412{
413 int ret;
414
415 if (qh->dev_speed == USB_SPEED_HIGH) {
416 /* if this is a hs transaction we need a full frame */
417 ret = dwc2_find_single_uframe(hsotg, qh);
418 } else {
419 /*
420 * if this is a fs transaction we may need a sequence
421 * of frames
422 */
423 ret = dwc2_find_multi_uframe(hsotg, qh);
424 }
425 return ret;
426}
427
428/**
Paul Zimmerman7359d482013-03-11 17:47:59 -0700429 * dwc2_check_max_xfer_size() - Checks that the max transfer size allowed in a
430 * host channel is large enough to handle the maximum data transfer in a single
431 * (micro)frame for a periodic transfer
432 *
433 * @hsotg: The HCD state structure for the DWC OTG controller
434 * @qh: QH for a periodic endpoint
435 *
436 * Return: 0 if successful, negative error code otherwise
437 */
438static int dwc2_check_max_xfer_size(struct dwc2_hsotg *hsotg,
439 struct dwc2_qh *qh)
440{
441 u32 max_xfer_size;
442 u32 max_channel_xfer_size;
443 int status = 0;
444
445 max_xfer_size = dwc2_max_packet(qh->maxp) * dwc2_hb_mult(qh->maxp);
446 max_channel_xfer_size = hsotg->core_params->max_transfer_size;
447
448 if (max_xfer_size > max_channel_xfer_size) {
449 dev_err(hsotg->dev,
450 "%s: Periodic xfer length %d > max xfer length for channel %d\n",
451 __func__, max_xfer_size, max_channel_xfer_size);
452 status = -ENOSPC;
453 }
454
455 return status;
456}
457
458/**
459 * dwc2_schedule_periodic() - Schedules an interrupt or isochronous transfer in
460 * the periodic schedule
461 *
462 * @hsotg: The HCD state structure for the DWC OTG controller
463 * @qh: QH for the periodic transfer. The QH should already contain the
464 * scheduling information.
465 *
466 * Return: 0 if successful, negative error code otherwise
467 */
468static int dwc2_schedule_periodic(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
469{
470 int status;
471
Dom Cobley20f2eb92013-09-23 14:23:34 -0700472 if (hsotg->core_params->uframe_sched > 0) {
473 int frame = -1;
474
475 status = dwc2_find_uframe(hsotg, qh);
476 if (status == 0)
477 frame = 7;
478 else if (status > 0)
479 frame = status - 1;
480
481 /* Set the new frame up */
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -0800482 if (frame >= 0) {
Dom Cobley20f2eb92013-09-23 14:23:34 -0700483 qh->sched_frame &= ~0x7;
484 qh->sched_frame |= (frame & 7);
485 }
486
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -0800487 if (status > 0)
Dom Cobley20f2eb92013-09-23 14:23:34 -0700488 status = 0;
489 } else {
490 status = dwc2_periodic_channel_available(hsotg);
491 if (status) {
492 dev_info(hsotg->dev,
493 "%s: No host channel available for periodic transfer\n",
494 __func__);
495 return status;
496 }
497
498 status = dwc2_check_periodic_bandwidth(hsotg, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700499 }
500
Paul Zimmerman7359d482013-03-11 17:47:59 -0700501 if (status) {
502 dev_dbg(hsotg->dev,
503 "%s: Insufficient periodic bandwidth for periodic transfer\n",
504 __func__);
505 return status;
506 }
507
508 status = dwc2_check_max_xfer_size(hsotg, qh);
509 if (status) {
510 dev_dbg(hsotg->dev,
511 "%s: Channel max transfer size too small for periodic transfer\n",
512 __func__);
513 return status;
514 }
515
516 if (hsotg->core_params->dma_desc_enable > 0)
517 /* Don't rely on SOF and start in ready schedule */
518 list_add_tail(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
519 else
520 /* Always start in inactive schedule */
521 list_add_tail(&qh->qh_list_entry,
522 &hsotg->periodic_sched_inactive);
523
Dom Cobley20f2eb92013-09-23 14:23:34 -0700524 if (hsotg->core_params->uframe_sched <= 0)
525 /* Reserve periodic channel */
526 hsotg->periodic_channels++;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700527
528 /* Update claimed usecs per (micro)frame */
529 hsotg->periodic_usecs += qh->usecs;
530
531 return status;
532}
533
534/**
535 * dwc2_deschedule_periodic() - Removes an interrupt or isochronous transfer
536 * from the periodic schedule
537 *
538 * @hsotg: The HCD state structure for the DWC OTG controller
539 * @qh: QH for the periodic transfer
540 */
541static void dwc2_deschedule_periodic(struct dwc2_hsotg *hsotg,
542 struct dwc2_qh *qh)
543{
Dom Cobley20f2eb92013-09-23 14:23:34 -0700544 int i;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700545
Dom Cobley20f2eb92013-09-23 14:23:34 -0700546 list_del_init(&qh->qh_list_entry);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700547
548 /* Update claimed usecs per (micro)frame */
549 hsotg->periodic_usecs -= qh->usecs;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700550
551 if (hsotg->core_params->uframe_sched > 0) {
552 for (i = 0; i < 8; i++) {
553 hsotg->frame_usecs[i] += qh->frame_usecs[i];
554 qh->frame_usecs[i] = 0;
555 }
556 } else {
557 /* Release periodic channel reservation */
558 hsotg->periodic_channels--;
559 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700560}
561
562/**
563 * dwc2_hcd_qh_add() - Adds a QH to either the non periodic or periodic
564 * schedule if it is not already in the schedule. If the QH is already in
565 * the schedule, no action is taken.
566 *
567 * @hsotg: The HCD state structure for the DWC OTG controller
568 * @qh: The QH to add
569 *
570 * Return: 0 if successful, negative error code otherwise
571 */
572int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
573{
Dan Carpenterd31e6ca2013-11-25 17:11:29 +0300574 int status;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700575 u32 intr_mask;
576
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200577 if (dbg_qh(qh))
578 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700579
580 if (!list_empty(&qh->qh_list_entry))
581 /* QH already in a schedule */
Dan Carpenterd31e6ca2013-11-25 17:11:29 +0300582 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700583
Gregory Herrero08c4ffc2015-09-22 15:16:45 +0200584 if (!dwc2_frame_num_le(qh->sched_frame, hsotg->frame_number) &&
585 !hsotg->frame_number) {
586 dev_dbg(hsotg->dev,
587 "reset frame number counter\n");
588 qh->sched_frame = dwc2_frame_num_inc(hsotg->frame_number,
589 SCHEDULE_SLOP);
590 }
591
Paul Zimmerman7359d482013-03-11 17:47:59 -0700592 /* Add the new QH to the appropriate schedule */
593 if (dwc2_qh_is_non_per(qh)) {
594 /* Always start in inactive schedule */
595 list_add_tail(&qh->qh_list_entry,
596 &hsotg->non_periodic_sched_inactive);
Dan Carpenter5e128472013-11-25 17:14:14 +0300597 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700598 }
599
Dan Carpenter5e128472013-11-25 17:14:14 +0300600 status = dwc2_schedule_periodic(hsotg, qh);
601 if (status)
602 return status;
603 if (!hsotg->periodic_qh_count) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300604 intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
Dan Carpenter5e128472013-11-25 17:14:14 +0300605 intr_mask |= GINTSTS_SOF;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300606 dwc2_writel(intr_mask, hsotg->regs + GINTMSK);
Dan Carpenter5e128472013-11-25 17:14:14 +0300607 }
608 hsotg->periodic_qh_count++;
609
Dan Carpenterd31e6ca2013-11-25 17:11:29 +0300610 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700611}
612
613/**
614 * dwc2_hcd_qh_unlink() - Removes a QH from either the non-periodic or periodic
615 * schedule. Memory is not freed.
616 *
617 * @hsotg: The HCD state structure
618 * @qh: QH to remove from schedule
619 */
620void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
621{
622 u32 intr_mask;
623
624 dev_vdbg(hsotg->dev, "%s()\n", __func__);
625
626 if (list_empty(&qh->qh_list_entry))
627 /* QH is not in a schedule */
628 return;
629
630 if (dwc2_qh_is_non_per(qh)) {
631 if (hsotg->non_periodic_qh_ptr == &qh->qh_list_entry)
632 hsotg->non_periodic_qh_ptr =
633 hsotg->non_periodic_qh_ptr->next;
634 list_del_init(&qh->qh_list_entry);
Dan Carpenter5e128472013-11-25 17:14:14 +0300635 return;
636 }
637
638 dwc2_deschedule_periodic(hsotg, qh);
639 hsotg->periodic_qh_count--;
640 if (!hsotg->periodic_qh_count) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300641 intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
Dan Carpenter5e128472013-11-25 17:14:14 +0300642 intr_mask &= ~GINTSTS_SOF;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300643 dwc2_writel(intr_mask, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700644 }
645}
646
647/*
648 * Schedule the next continuing periodic split transfer
649 */
650static void dwc2_sched_periodic_split(struct dwc2_hsotg *hsotg,
651 struct dwc2_qh *qh, u16 frame_number,
652 int sched_next_periodic_split)
653{
654 u16 incr;
655
656 if (sched_next_periodic_split) {
657 qh->sched_frame = frame_number;
658 incr = dwc2_frame_num_inc(qh->start_split_frame, 1);
659 if (dwc2_frame_num_le(frame_number, incr)) {
660 /*
661 * Allow one frame to elapse after start split
662 * microframe before scheduling complete split, but
663 * DON'T if we are doing the next start split in the
664 * same frame for an ISOC out
665 */
666 if (qh->ep_type != USB_ENDPOINT_XFER_ISOC ||
667 qh->ep_is_in != 0) {
668 qh->sched_frame =
669 dwc2_frame_num_inc(qh->sched_frame, 1);
670 }
671 }
672 } else {
673 qh->sched_frame = dwc2_frame_num_inc(qh->start_split_frame,
674 qh->interval);
675 if (dwc2_frame_num_le(qh->sched_frame, frame_number))
676 qh->sched_frame = frame_number;
677 qh->sched_frame |= 0x7;
678 qh->start_split_frame = qh->sched_frame;
679 }
680}
681
682/*
683 * Deactivates a QH. For non-periodic QHs, removes the QH from the active
684 * non-periodic schedule. The QH is added to the inactive non-periodic
685 * schedule if any QTDs are still attached to the QH.
686 *
687 * For periodic QHs, the QH is removed from the periodic queued schedule. If
688 * there are any QTDs still attached to the QH, the QH is added to either the
689 * periodic inactive schedule or the periodic ready schedule and its next
690 * scheduled frame is calculated. The QH is placed in the ready schedule if
691 * the scheduled frame has been reached already. Otherwise it's placed in the
692 * inactive schedule. If there are no QTDs attached to the QH, the QH is
693 * completely removed from the periodic schedule.
694 */
695void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
696 int sched_next_periodic_split)
697{
Dan Carpenter5e128472013-11-25 17:14:14 +0300698 u16 frame_number;
699
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200700 if (dbg_qh(qh))
701 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700702
703 if (dwc2_qh_is_non_per(qh)) {
704 dwc2_hcd_qh_unlink(hsotg, qh);
705 if (!list_empty(&qh->qtd_list))
706 /* Add back to inactive non-periodic schedule */
707 dwc2_hcd_qh_add(hsotg, qh);
Dan Carpenter5e128472013-11-25 17:14:14 +0300708 return;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700709 }
Dan Carpenter5e128472013-11-25 17:14:14 +0300710
711 frame_number = dwc2_hcd_get_frame_number(hsotg);
712
713 if (qh->do_split) {
714 dwc2_sched_periodic_split(hsotg, qh, frame_number,
715 sched_next_periodic_split);
716 } else {
717 qh->sched_frame = dwc2_frame_num_inc(qh->sched_frame,
718 qh->interval);
719 if (dwc2_frame_num_le(qh->sched_frame, frame_number))
720 qh->sched_frame = frame_number;
721 }
722
723 if (list_empty(&qh->qtd_list)) {
724 dwc2_hcd_qh_unlink(hsotg, qh);
725 return;
726 }
727 /*
728 * Remove from periodic_sched_queued and move to
729 * appropriate queue
730 */
731 if ((hsotg->core_params->uframe_sched > 0 &&
732 dwc2_frame_num_le(qh->sched_frame, frame_number)) ||
733 (hsotg->core_params->uframe_sched <= 0 &&
734 qh->sched_frame == frame_number))
Douglas Anderson94ef7ae2016-01-28 18:19:56 -0800735 list_move_tail(&qh->qh_list_entry,
736 &hsotg->periodic_sched_ready);
Dan Carpenter5e128472013-11-25 17:14:14 +0300737 else
Douglas Anderson94ef7ae2016-01-28 18:19:56 -0800738 list_move_tail(&qh->qh_list_entry,
739 &hsotg->periodic_sched_inactive);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700740}
741
742/**
743 * dwc2_hcd_qtd_init() - Initializes a QTD structure
744 *
745 * @qtd: The QTD to initialize
746 * @urb: The associated URB
747 */
748void dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
749{
750 qtd->urb = urb;
751 if (dwc2_hcd_get_pipe_type(&urb->pipe_info) ==
752 USB_ENDPOINT_XFER_CONTROL) {
753 /*
754 * The only time the QTD data toggle is used is on the data
755 * phase of control transfers. This phase always starts with
756 * DATA1.
757 */
758 qtd->data_toggle = DWC2_HC_PID_DATA1;
759 qtd->control_phase = DWC2_CONTROL_SETUP;
760 }
761
762 /* Start split */
763 qtd->complete_split = 0;
764 qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
765 qtd->isoc_split_offset = 0;
766 qtd->in_process = 0;
767
768 /* Store the qtd ptr in the urb to reference the QTD */
769 urb->qtd = qtd;
770}
771
772/**
773 * dwc2_hcd_qtd_add() - Adds a QTD to the QTD-list of a QH
Gregory Herrero33ad2612015-04-29 22:09:15 +0200774 * Caller must hold driver lock.
Paul Zimmerman7359d482013-03-11 17:47:59 -0700775 *
776 * @hsotg: The DWC HCD structure
777 * @qtd: The QTD to add
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +0200778 * @qh: Queue head to add qtd to
Paul Zimmerman7359d482013-03-11 17:47:59 -0700779 *
780 * Return: 0 if successful, negative error code otherwise
781 *
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +0200782 * If the QH to which the QTD is added is not currently scheduled, it is placed
783 * into the proper schedule based on its EP type.
Paul Zimmerman7359d482013-03-11 17:47:59 -0700784 */
785int dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +0200786 struct dwc2_qh *qh)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700787{
Paul Zimmermanb2d6cb52013-07-13 14:53:51 -0700788 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700789
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +0200790 if (unlikely(!qh)) {
791 dev_err(hsotg->dev, "%s: Invalid QH\n", __func__);
792 retval = -EINVAL;
793 goto fail;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700794 }
795
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +0200796 retval = dwc2_hcd_qh_add(hsotg, qh);
Paul Zimmermanb2d6cb52013-07-13 14:53:51 -0700797 if (retval)
798 goto fail;
799
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +0200800 qtd->qh = qh;
801 list_add_tail(&qtd->qtd_list_entry, &qh->qtd_list);
Paul Zimmermanb2d6cb52013-07-13 14:53:51 -0700802
803 return 0;
Paul Zimmermanb2d6cb52013-07-13 14:53:51 -0700804fail:
Paul Zimmerman7359d482013-03-11 17:47:59 -0700805 return retval;
806}