2 * hcd_queue.c - DesignWare HS OTG Controller host queuing routines
4 * Copyright (C) 2004-2013 Synopsys, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
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.
38 * This file contains the functions to manage Queue Heads and Queue
39 * Transfer Descriptors for Host mode
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>
47 #include <linux/slab.h>
48 #include <linux/usb.h>
50 #include <linux/usb/hcd.h>
51 #include <linux/usb/ch11.h>
57 * dwc2_qh_init() - Initializes a QH structure
59 * @hsotg: The HCD state structure for the DWC OTG controller
61 * @urb: Holds the information about the device/endpoint needed to initialize
64 #define SCHEDULE_SLOP 10
65 static void dwc2_qh_init(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
,
66 struct dwc2_hcd_urb
*urb
)
68 int dev_speed
, hub_addr
, hub_port
;
71 dev_vdbg(hsotg
->dev
, "%s()\n", __func__
);
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;
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
);
82 /* FS/LS Endpoint on HS Hub, NOT virtual root hub */
83 dev_speed
= dwc2_host_get_speed(hsotg
, urb
->priv
);
85 dwc2_host_hub_info(hsotg
, urb
->priv
, &hub_addr
, &hub_port
);
87 if ((dev_speed
== USB_SPEED_LOW
|| dev_speed
== USB_SPEED_FULL
) &&
88 hub_addr
!= 0 && hub_addr
!= 1) {
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
,
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 */
101 /* Todo: Account for split transfers in the bus time */
103 dwc2_hb_mult(qh
->maxp
) * dwc2_max_packet(qh
->maxp
);
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
,
109 /* Start in a slightly future (micro)frame */
110 qh
->sched_frame
= dwc2_frame_num_inc(hsotg
->frame_number
,
112 qh
->interval
= urb
->interval
;
114 /* Increase interrupt polling rate for debugging */
115 if (qh
->ep_type
== USB_ENDPOINT_XFER_INT
)
118 hprt
= readl(hsotg
->regs
+ HPRT0
);
119 prtspd
= (hprt
& HPRT0_SPD_MASK
) >> HPRT0_SPD_SHIFT
;
120 if (prtspd
== HPRT0_SPD_HIGH_SPEED
&&
121 (dev_speed
== USB_SPEED_LOW
||
122 dev_speed
== USB_SPEED_FULL
)) {
124 qh
->sched_frame
|= 0x7;
125 qh
->start_split_frame
= qh
->sched_frame
;
127 dev_dbg(hsotg
->dev
, "interval=%d\n", qh
->interval
);
130 dev_vdbg(hsotg
->dev
, "DWC OTG HCD QH Initialized\n");
131 dev_vdbg(hsotg
->dev
, "DWC OTG HCD QH - qh = %p\n", qh
);
132 dev_vdbg(hsotg
->dev
, "DWC OTG HCD QH - Device Address = %d\n",
133 dwc2_hcd_get_dev_addr(&urb
->pipe_info
));
134 dev_vdbg(hsotg
->dev
, "DWC OTG HCD QH - Endpoint %d, %s\n",
135 dwc2_hcd_get_ep_num(&urb
->pipe_info
),
136 dwc2_hcd_is_pipe_in(&urb
->pipe_info
) ? "IN" : "OUT");
138 qh
->dev_speed
= dev_speed
;
154 dev_vdbg(hsotg
->dev
, "DWC OTG HCD QH - Speed = %s\n", speed
);
156 switch (qh
->ep_type
) {
157 case USB_ENDPOINT_XFER_ISOC
:
158 type
= "isochronous";
160 case USB_ENDPOINT_XFER_INT
:
163 case USB_ENDPOINT_XFER_CONTROL
:
166 case USB_ENDPOINT_XFER_BULK
:
174 dev_vdbg(hsotg
->dev
, "DWC OTG HCD QH - Type = %s\n", type
);
176 if (qh
->ep_type
== USB_ENDPOINT_XFER_INT
) {
177 dev_vdbg(hsotg
->dev
, "DWC OTG HCD QH - usecs = %d\n",
179 dev_vdbg(hsotg
->dev
, "DWC OTG HCD QH - interval = %d\n",
185 * dwc2_hcd_qh_create() - Allocates and initializes a QH
187 * @hsotg: The HCD state structure for the DWC OTG controller
188 * @urb: Holds the information about the device/endpoint needed
189 * to initialize the QH
190 * @atomic_alloc: Flag to do atomic allocation if needed
192 * Return: Pointer to the newly allocated QH, or NULL on error
194 static struct dwc2_qh
*dwc2_hcd_qh_create(struct dwc2_hsotg
*hsotg
,
195 struct dwc2_hcd_urb
*urb
,
203 /* Allocate memory */
204 qh
= kzalloc(sizeof(*qh
), mem_flags
);
208 dwc2_qh_init(hsotg
, qh
, urb
);
210 if (hsotg
->core_params
->dma_desc_enable
> 0 &&
211 dwc2_hcd_qh_init_ddma(hsotg
, qh
, mem_flags
) < 0) {
212 dwc2_hcd_qh_free(hsotg
, qh
);
220 * dwc2_hcd_qh_free() - Frees the QH
222 * @hsotg: HCD instance
223 * @qh: The QH to free
225 * QH should already be removed from the list. QTD list should already be empty
226 * if called from URB Dequeue.
228 * Must NOT be called with interrupt disabled or spinlock held
230 void dwc2_hcd_qh_free(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
)
232 if (hsotg
->core_params
->dma_desc_enable
> 0)
233 dwc2_hcd_qh_free_ddma(hsotg
, qh
);
234 else if (qh
->dw_align_buf
)
235 dma_free_coherent(hsotg
->dev
, qh
->dw_align_buf_size
,
236 qh
->dw_align_buf
, qh
->dw_align_buf_dma
);
241 * dwc2_periodic_channel_available() - Checks that a channel is available for a
244 * @hsotg: The HCD state structure for the DWC OTG controller
246 * Return: 0 if successful, negative error code otherwise
248 static int dwc2_periodic_channel_available(struct dwc2_hsotg
*hsotg
)
251 * Currently assuming that there is a dedicated host channel for
252 * each periodic transaction plus at least one host channel for
253 * non-periodic transactions
258 num_channels
= hsotg
->core_params
->host_channels
;
259 if (hsotg
->periodic_channels
+ hsotg
->non_periodic_channels
<
261 && hsotg
->periodic_channels
< num_channels
- 1) {
265 "%s: Total channels: %d, Periodic: %d, "
266 "Non-periodic: %d\n", __func__
, num_channels
,
267 hsotg
->periodic_channels
, hsotg
->non_periodic_channels
);
275 * dwc2_check_periodic_bandwidth() - Checks that there is sufficient bandwidth
276 * for the specified QH in the periodic schedule
278 * @hsotg: The HCD state structure for the DWC OTG controller
279 * @qh: QH containing periodic bandwidth required
281 * Return: 0 if successful, negative error code otherwise
283 * For simplicity, this calculation assumes that all the transfers in the
284 * periodic schedule may occur in the same (micro)frame
286 static int dwc2_check_periodic_bandwidth(struct dwc2_hsotg
*hsotg
,
290 s16 max_claimed_usecs
;
294 if (qh
->dev_speed
== USB_SPEED_HIGH
|| qh
->do_split
) {
297 * Max periodic usecs is 80% x 125 usec = 100 usec
299 max_claimed_usecs
= 100 - qh
->usecs
;
303 * Max periodic usecs is 90% x 1000 usec = 900 usec
305 max_claimed_usecs
= 900 - qh
->usecs
;
308 if (hsotg
->periodic_usecs
> max_claimed_usecs
) {
310 "%s: already claimed usecs %d, required usecs %d\n",
311 __func__
, hsotg
->periodic_usecs
, qh
->usecs
);
319 * 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
324 static const unsigned short max_uframe_usecs
[] = {
325 100, 100, 100, 100, 100, 100, 30, 0
328 void dwc2_hcd_init_usecs(struct dwc2_hsotg
*hsotg
)
332 for (i
= 0; i
< 8; i
++)
333 hsotg
->frame_usecs
[i
] = max_uframe_usecs
[i
];
336 static int dwc2_find_single_uframe(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
)
338 unsigned short utime
= qh
->usecs
;
341 for (i
= 0; i
< 8; i
++) {
342 /* 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
;
353 * use this for FS apps that can span multiple uframes
355 static int dwc2_find_multi_uframe(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
)
357 unsigned short utime
= qh
->usecs
;
358 unsigned short xtime
;
364 for (i
= 0; i
< 8; i
++) {
365 if (hsotg
->frame_usecs
[i
] <= 0)
369 * we need n consecutive slots so use j as a start slot
370 * j plus j+1 must be enough time (for now)
372 xtime
= hsotg
->frame_usecs
[i
];
373 for (j
= i
+ 1; j
< 8; j
++) {
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
378 if (xtime
+ hsotg
->frame_usecs
[j
] < utime
) {
379 if (hsotg
->frame_usecs
[j
] <
383 if (xtime
>= utime
) {
385 for (k
= i
; k
< 8; k
++) {
386 t_left
-= hsotg
->frame_usecs
[k
];
388 qh
->frame_usecs
[k
] +=
389 hsotg
->frame_usecs
[k
]
391 hsotg
->frame_usecs
[k
] = -t_left
;
394 qh
->frame_usecs
[k
] +=
395 hsotg
->frame_usecs
[k
];
396 hsotg
->frame_usecs
[k
] = 0;
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 */
404 hsotg
->frame_usecs
[j
] == max_uframe_usecs
[j
])
411 static int dwc2_find_uframe(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
)
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
);
420 * if this is a fs transaction we may need a sequence
423 ret
= dwc2_find_multi_uframe(hsotg
, qh
);
429 * 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
433 * @hsotg: The HCD state structure for the DWC OTG controller
434 * @qh: QH for a periodic endpoint
436 * Return: 0 if successful, negative error code otherwise
438 static int dwc2_check_max_xfer_size(struct dwc2_hsotg
*hsotg
,
442 u32 max_channel_xfer_size
;
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
;
448 if (max_xfer_size
> max_channel_xfer_size
) {
450 "%s: Periodic xfer length %d > max xfer length for channel %d\n",
451 __func__
, max_xfer_size
, max_channel_xfer_size
);
459 * dwc2_schedule_periodic() - Schedules an interrupt or isochronous transfer in
460 * the periodic schedule
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.
466 * Return: 0 if successful, negative error code otherwise
468 static int dwc2_schedule_periodic(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
)
472 if (hsotg
->core_params
->uframe_sched
> 0) {
475 status
= dwc2_find_uframe(hsotg
, qh
);
481 /* Set the new frame up */
483 qh
->sched_frame
&= ~0x7;
484 qh
->sched_frame
|= (frame
& 7);
490 status
= dwc2_periodic_channel_available(hsotg
);
493 "%s: No host channel available for periodic transfer\n",
498 status
= dwc2_check_periodic_bandwidth(hsotg
, qh
);
503 "%s: Insufficient periodic bandwidth for periodic transfer\n",
508 status
= dwc2_check_max_xfer_size(hsotg
, qh
);
511 "%s: Channel max transfer size too small for periodic transfer\n",
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
);
520 /* Always start in inactive schedule */
521 list_add_tail(&qh
->qh_list_entry
,
522 &hsotg
->periodic_sched_inactive
);
524 if (hsotg
->core_params
->uframe_sched
<= 0)
525 /* Reserve periodic channel */
526 hsotg
->periodic_channels
++;
528 /* Update claimed usecs per (micro)frame */
529 hsotg
->periodic_usecs
+= qh
->usecs
;
535 * dwc2_deschedule_periodic() - Removes an interrupt or isochronous transfer
536 * from the periodic schedule
538 * @hsotg: The HCD state structure for the DWC OTG controller
539 * @qh: QH for the periodic transfer
541 static void dwc2_deschedule_periodic(struct dwc2_hsotg
*hsotg
,
546 list_del_init(&qh
->qh_list_entry
);
548 /* Update claimed usecs per (micro)frame */
549 hsotg
->periodic_usecs
-= qh
->usecs
;
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;
557 /* Release periodic channel reservation */
558 hsotg
->periodic_channels
--;
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.
567 * @hsotg: The HCD state structure for the DWC OTG controller
570 * Return: 0 if successful, negative error code otherwise
572 int dwc2_hcd_qh_add(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
)
578 dev_vdbg(hsotg
->dev
, "%s()\n", __func__
);
580 if (!list_empty(&qh
->qh_list_entry
))
581 /* QH already in a schedule */
584 /* Add the new QH to the appropriate schedule */
585 if (dwc2_qh_is_non_per(qh
)) {
586 /* Always start in inactive schedule */
587 list_add_tail(&qh
->qh_list_entry
,
588 &hsotg
->non_periodic_sched_inactive
);
592 status
= dwc2_schedule_periodic(hsotg
, qh
);
595 if (!hsotg
->periodic_qh_count
) {
596 intr_mask
= readl(hsotg
->regs
+ GINTMSK
);
597 intr_mask
|= GINTSTS_SOF
;
598 writel(intr_mask
, hsotg
->regs
+ GINTMSK
);
600 hsotg
->periodic_qh_count
++;
606 * dwc2_hcd_qh_unlink() - Removes a QH from either the non-periodic or periodic
607 * schedule. Memory is not freed.
609 * @hsotg: The HCD state structure
610 * @qh: QH to remove from schedule
612 void dwc2_hcd_qh_unlink(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
)
616 dev_vdbg(hsotg
->dev
, "%s()\n", __func__
);
618 if (list_empty(&qh
->qh_list_entry
))
619 /* QH is not in a schedule */
622 if (dwc2_qh_is_non_per(qh
)) {
623 if (hsotg
->non_periodic_qh_ptr
== &qh
->qh_list_entry
)
624 hsotg
->non_periodic_qh_ptr
=
625 hsotg
->non_periodic_qh_ptr
->next
;
626 list_del_init(&qh
->qh_list_entry
);
630 dwc2_deschedule_periodic(hsotg
, qh
);
631 hsotg
->periodic_qh_count
--;
632 if (!hsotg
->periodic_qh_count
) {
633 intr_mask
= readl(hsotg
->regs
+ GINTMSK
);
634 intr_mask
&= ~GINTSTS_SOF
;
635 writel(intr_mask
, hsotg
->regs
+ GINTMSK
);
640 * Schedule the next continuing periodic split transfer
642 static void dwc2_sched_periodic_split(struct dwc2_hsotg
*hsotg
,
643 struct dwc2_qh
*qh
, u16 frame_number
,
644 int sched_next_periodic_split
)
648 if (sched_next_periodic_split
) {
649 qh
->sched_frame
= frame_number
;
650 incr
= dwc2_frame_num_inc(qh
->start_split_frame
, 1);
651 if (dwc2_frame_num_le(frame_number
, incr
)) {
653 * Allow one frame to elapse after start split
654 * microframe before scheduling complete split, but
655 * DON'T if we are doing the next start split in the
656 * same frame for an ISOC out
658 if (qh
->ep_type
!= USB_ENDPOINT_XFER_ISOC
||
661 dwc2_frame_num_inc(qh
->sched_frame
, 1);
665 qh
->sched_frame
= dwc2_frame_num_inc(qh
->start_split_frame
,
667 if (dwc2_frame_num_le(qh
->sched_frame
, frame_number
))
668 qh
->sched_frame
= frame_number
;
669 qh
->sched_frame
|= 0x7;
670 qh
->start_split_frame
= qh
->sched_frame
;
675 * Deactivates a QH. For non-periodic QHs, removes the QH from the active
676 * non-periodic schedule. The QH is added to the inactive non-periodic
677 * schedule if any QTDs are still attached to the QH.
679 * For periodic QHs, the QH is removed from the periodic queued schedule. If
680 * there are any QTDs still attached to the QH, the QH is added to either the
681 * periodic inactive schedule or the periodic ready schedule and its next
682 * scheduled frame is calculated. The QH is placed in the ready schedule if
683 * the scheduled frame has been reached already. Otherwise it's placed in the
684 * inactive schedule. If there are no QTDs attached to the QH, the QH is
685 * completely removed from the periodic schedule.
687 void dwc2_hcd_qh_deactivate(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
,
688 int sched_next_periodic_split
)
693 dev_vdbg(hsotg
->dev
, "%s()\n", __func__
);
695 if (dwc2_qh_is_non_per(qh
)) {
696 dwc2_hcd_qh_unlink(hsotg
, qh
);
697 if (!list_empty(&qh
->qtd_list
))
698 /* Add back to inactive non-periodic schedule */
699 dwc2_hcd_qh_add(hsotg
, qh
);
703 frame_number
= dwc2_hcd_get_frame_number(hsotg
);
706 dwc2_sched_periodic_split(hsotg
, qh
, frame_number
,
707 sched_next_periodic_split
);
709 qh
->sched_frame
= dwc2_frame_num_inc(qh
->sched_frame
,
711 if (dwc2_frame_num_le(qh
->sched_frame
, frame_number
))
712 qh
->sched_frame
= frame_number
;
715 if (list_empty(&qh
->qtd_list
)) {
716 dwc2_hcd_qh_unlink(hsotg
, qh
);
720 * Remove from periodic_sched_queued and move to
723 if ((hsotg
->core_params
->uframe_sched
> 0 &&
724 dwc2_frame_num_le(qh
->sched_frame
, frame_number
)) ||
725 (hsotg
->core_params
->uframe_sched
<= 0 &&
726 qh
->sched_frame
== frame_number
))
727 list_move(&qh
->qh_list_entry
, &hsotg
->periodic_sched_ready
);
729 list_move(&qh
->qh_list_entry
, &hsotg
->periodic_sched_inactive
);
733 * dwc2_hcd_qtd_init() - Initializes a QTD structure
735 * @qtd: The QTD to initialize
736 * @urb: The associated URB
738 void dwc2_hcd_qtd_init(struct dwc2_qtd
*qtd
, struct dwc2_hcd_urb
*urb
)
741 if (dwc2_hcd_get_pipe_type(&urb
->pipe_info
) ==
742 USB_ENDPOINT_XFER_CONTROL
) {
744 * The only time the QTD data toggle is used is on the data
745 * phase of control transfers. This phase always starts with
748 qtd
->data_toggle
= DWC2_HC_PID_DATA1
;
749 qtd
->control_phase
= DWC2_CONTROL_SETUP
;
753 qtd
->complete_split
= 0;
754 qtd
->isoc_split_pos
= DWC2_HCSPLT_XACTPOS_ALL
;
755 qtd
->isoc_split_offset
= 0;
758 /* Store the qtd ptr in the urb to reference the QTD */
763 * dwc2_hcd_qtd_add() - Adds a QTD to the QTD-list of a QH
765 * @hsotg: The DWC HCD structure
766 * @qtd: The QTD to add
767 * @qh: Out parameter to return queue head
768 * @atomic_alloc: Flag to do atomic alloc if needed
770 * Return: 0 if successful, negative error code otherwise
772 * Finds the correct QH to place the QTD into. If it does not find a QH, it
773 * will create a new QH. If the QH to which the QTD is added is not currently
774 * scheduled, it is placed into the proper schedule based on its EP type.
776 int dwc2_hcd_qtd_add(struct dwc2_hsotg
*hsotg
, struct dwc2_qtd
*qtd
,
777 struct dwc2_qh
**qh
, gfp_t mem_flags
)
779 struct dwc2_hcd_urb
*urb
= qtd
->urb
;
785 * Get the QH which holds the QTD-list to insert to. Create QH if it
789 *qh
= dwc2_hcd_qh_create(hsotg
, urb
, mem_flags
);
795 spin_lock_irqsave(&hsotg
->lock
, flags
);
797 retval
= dwc2_hcd_qh_add(hsotg
, *qh
);
802 list_add_tail(&qtd
->qtd_list_entry
, &(*qh
)->qtd_list
);
803 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
809 struct dwc2_qtd
*qtd2
, *qtd2_tmp
;
810 struct dwc2_qh
*qh_tmp
= *qh
;
813 dwc2_hcd_qh_unlink(hsotg
, qh_tmp
);
815 /* Free each QTD in the QH's QTD list */
816 list_for_each_entry_safe(qtd2
, qtd2_tmp
, &qh_tmp
->qtd_list
,
818 dwc2_hcd_qtd_unlink_and_free(hsotg
, qtd2
, qh_tmp
);
820 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
821 dwc2_hcd_qh_free(hsotg
, qh_tmp
);
823 spin_unlock_irqrestore(&hsotg
->lock
, flags
);