1 /* ==========================================================================
2 * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
7 * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
8 * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
9 * otherwise expressly agreed to in writing between Synopsys and you.
11 * The Software IS NOT an item of Licensed Software or Licensed Product under
12 * any End User Software License Agreement or Agreement for Licensed Product
13 * with Synopsys or any supplement thereto. You are permitted to use and
14 * redistribute this Software in source and binary forms, with or without
15 * modification, provided that redistributions of source code must retain this
16 * notice. You may not view, use, disclose, copy or distribute this file or
17 * any information contained herein except pursuant to this license grant from
18 * Synopsys. If you do not agree with this notice, including the disclaimer
19 * below, then you are not authorized to use the Software.
21 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32 * ========================================================================== */
33 #ifndef DWC_DEVICE_ONLY
38 * This file contains the functions to manage Queue Heads and Queue
39 * Transfer Descriptors.
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
44 #include <linux/init.h>
45 #include <linux/device.h>
46 #include <linux/errno.h>
47 #include <linux/list.h>
48 #include <linux/interrupt.h>
49 #include <linux/string.h>
50 #include <linux/version.h>
52 #include <mach/irqs.h>
54 #include "otg_driver.h"
59 * This function allocates and initializes a QH.
61 * @param hcd The HCD state structure for the DWC OTG controller.
62 * @param[in] urb Holds the information about the device/endpoint that we need
63 * to initialize the QH.
65 * @return Returns pointer to the newly allocated QH, or NULL on error. */
66 dwc_otg_qh_t
*dwc_otg_hcd_qh_create (dwc_otg_hcd_t
*hcd
, struct urb
*urb
)
71 /** @todo add memflags argument */
72 qh
= dwc_otg_hcd_qh_alloc ();
77 dwc_otg_hcd_qh_init (hcd
, qh
, urb
);
81 /** Free each QTD in the QH's QTD-list then free the QH. QH should already be
82 * removed from a list. QTD list should already be empty if called from URB
85 * @param[in] hcd HCD instance.
86 * @param[in] qh The QH to free.
88 void dwc_otg_hcd_qh_free (dwc_otg_hcd_t
*hcd
, dwc_otg_qh_t
*qh
)
91 struct list_head
*pos
;
92 //unsigned long flags;
94 /* Free each QTD in the QTD list */
97 //the spinlock is locked before this function get called,
98 //but in case the lock is needed, the check function is preserved
100 //but in non-SMP mode, all spinlock is lockable.
101 //don't do the test in non-SMP mode
103 if(spin_trylock(&hcd
->lock
)) {
104 printk("%s: It is not supposed to be lockable!!\n",__func__
);
108 // SPIN_LOCK_IRQSAVE(&hcd->lock, flags)
109 for (pos
= qh
->qtd_list
.next
;
110 pos
!= &qh
->qtd_list
;
111 pos
= qh
->qtd_list
.next
)
114 qtd
= dwc_list_to_qtd (pos
);
115 dwc_otg_hcd_qtd_free (qtd
);
117 // SPIN_UNLOCK_IRQRESTORE(&hcd->lock, flags)
123 /** Initializes a QH structure.
125 * @param[in] hcd The HCD state structure for the DWC OTG controller.
126 * @param[in] qh The QH to init.
127 * @param[in] urb Holds the information about the device/endpoint that we need
128 * to initialize the QH. */
129 #define SCHEDULE_SLOP 10
130 void dwc_otg_hcd_qh_init(dwc_otg_hcd_t
*hcd
, dwc_otg_qh_t
*qh
, struct urb
*urb
)
133 memset (qh
, 0, sizeof (dwc_otg_qh_t
));
136 switch (usb_pipetype(urb
->pipe
)) {
138 qh
->ep_type
= USB_ENDPOINT_XFER_CONTROL
;
141 qh
->ep_type
= USB_ENDPOINT_XFER_BULK
;
143 case PIPE_ISOCHRONOUS
:
144 qh
->ep_type
= USB_ENDPOINT_XFER_ISOC
;
147 qh
->ep_type
= USB_ENDPOINT_XFER_INT
;
151 qh
->ep_is_in
= usb_pipein(urb
->pipe
) ? 1 : 0;
153 qh
->data_toggle
= DWC_OTG_HC_PID_DATA0
;
154 qh
->maxp
= usb_maxpacket(urb
->dev
, urb
->pipe
, !(usb_pipein(urb
->pipe
)));
155 INIT_LIST_HEAD(&qh
->qtd_list
);
156 INIT_LIST_HEAD(&qh
->qh_list_entry
);
158 qh
->speed
= urb
->dev
->speed
;
160 /* FS/LS Enpoint on HS Hub
161 * NOT virtual root hub */
163 if (((urb
->dev
->speed
== USB_SPEED_LOW
) ||
164 (urb
->dev
->speed
== USB_SPEED_FULL
)) &&
165 (urb
->dev
->tt
) && (urb
->dev
->tt
->hub
) && (urb
->dev
->tt
->hub
->devnum
!= 1))
167 DWC_DEBUGPL(DBG_HCD
, "QH init: EP %d: TT found at hub addr %d, for port %d\n",
168 usb_pipeendpoint(urb
->pipe
), urb
->dev
->tt
->hub
->devnum
,
173 if (qh
->ep_type
== USB_ENDPOINT_XFER_INT
||
174 qh
->ep_type
== USB_ENDPOINT_XFER_ISOC
) {
175 /* Compute scheduling parameters once and save them. */
178 /** @todo Account for split transfers in the bus time. */
179 int bytecount
= dwc_hb_mult(qh
->maxp
) * dwc_max_packet(qh
->maxp
);
180 qh
->usecs
= NS_TO_US(usb_calc_bus_time(urb
->dev
->speed
,
181 usb_pipein(urb
->pipe
),
182 (qh
->ep_type
== USB_ENDPOINT_XFER_ISOC
),
185 /* Start in a slightly future (micro)frame. */
186 qh
->sched_frame
= dwc_frame_num_inc(hcd
->frame_number
,
188 qh
->interval
= urb
->interval
;
190 /* Increase interrupt polling rate for debugging. */
191 if (qh
->ep_type
== USB_ENDPOINT_XFER_INT
) {
195 hprt
.d32
= dwc_read_reg32(hcd
->core_if
->host_if
->hprt0
);
196 if ((hprt
.b
.prtspd
== DWC_HPRT0_PRTSPD_HIGH_SPEED
) &&
197 ((urb
->dev
->speed
== USB_SPEED_LOW
) ||
198 (urb
->dev
->speed
== USB_SPEED_FULL
))) {
200 qh
->sched_frame
|= 0x7;
201 qh
->start_split_frame
= qh
->sched_frame
;
206 DWC_DEBUGPL(DBG_HCD
, "DWC OTG HCD QH Initialized\n");
207 DWC_DEBUGPL(DBG_HCDV
, "DWC OTG HCD QH - qh = %p\n", qh
);
208 DWC_DEBUGPL(DBG_HCDV
, "DWC OTG HCD QH - Device Address = %d\n",
210 DWC_DEBUGPL(DBG_HCDV
, "DWC OTG HCD QH - Endpoint %d, %s\n",
211 usb_pipeendpoint(urb
->pipe
),
212 usb_pipein(urb
->pipe
) == USB_DIR_IN
? "IN" : "OUT");
214 switch(urb
->dev
->speed
) {
228 DWC_DEBUGPL(DBG_HCDV
, "DWC OTG HCD QH - Speed = %s\n", speed
);
230 switch (qh
->ep_type
) {
231 case USB_ENDPOINT_XFER_ISOC
:
232 type
= "isochronous";
234 case USB_ENDPOINT_XFER_INT
:
237 case USB_ENDPOINT_XFER_CONTROL
:
240 case USB_ENDPOINT_XFER_BULK
:
247 DWC_DEBUGPL(DBG_HCDV
, "DWC OTG HCD QH - Type = %s\n",type
);
250 if (qh
->ep_type
== USB_ENDPOINT_XFER_INT
) {
251 DWC_DEBUGPL(DBG_HCDV
, "DWC OTG HCD QH - usecs = %d\n",
253 DWC_DEBUGPL(DBG_HCDV
, "DWC OTG HCD QH - interval = %d\n",
262 * Microframe scheduler
263 * track the total use in hcd->frame_usecs
264 * keep each qh use in qh->frame_usecs
265 * when surrendering the qh then donate the time back
267 static const u16 max_uframe_usecs
[] = { 100, 100, 100, 100, 100, 100, 30, 0 };
270 * called from dwc_otg_hcd.c:dwc_otg_hcd_init
272 int init_hcd_usecs(dwc_otg_hcd_t
*hcd
)
276 for (i
= 0; i
< 8; i
++)
277 hcd
->frame_usecs
[i
] = max_uframe_usecs
[i
];
282 static int find_single_uframe(dwc_otg_hcd_t
*hcd
, dwc_otg_qh_t
*qh
)
296 /* At the start hcd->frame_usecs[i] = max_uframe_usecs[i]; */
297 if (utime
<= hcd
->frame_usecs
[i
]) {
298 hcd
->frame_usecs
[i
] -= utime
;
299 qh
->frame_usecs
[i
] += utime
;
316 * use this for FS apps that can span multiple uframes
318 static int find_multi_uframe(dwc_otg_hcd_t
*hcd
, dwc_otg_qh_t
*qh
)
335 if (hcd
->frame_usecs
[i
] <= 0) {
345 * We need n consequtive slots so use j as a start slot.
346 * j plus j+1 must be enough time (for now)
348 xtime
= hcd
->frame_usecs
[i
];
349 for (j
= i
+ 1; j
< 8; j
++) {
351 * if we add this frame remaining time to xtime we may
352 * be OK, if not we need to test j for a complete frame.
354 if ((xtime
+ hcd
->frame_usecs
[j
]) < utime
) {
355 if (hcd
->frame_usecs
[j
] < max_uframe_usecs
[j
]) {
361 if (xtime
>= utime
) {
363 j
= 8; /* stop loop with a good value ret */
366 /* add the frame time to x time */
367 xtime
+= hcd
->frame_usecs
[j
];
368 /* we must have a fully available next frame or break */
369 if ((xtime
< utime
) &&
370 (hcd
->frame_usecs
[j
] == max_uframe_usecs
[j
])) {
372 j
= 8; /* stop loop with a bad value ret */
378 for (j
= i
; (t_left
> 0) && (j
< 8); j
++) {
379 t_left
-= hcd
->frame_usecs
[j
];
381 qh
->frame_usecs
[j
] +=
382 hcd
->frame_usecs
[j
] + t_left
;
383 hcd
->frame_usecs
[j
] = -t_left
;
387 qh
->frame_usecs
[j
] +=
389 hcd
->frame_usecs
[j
] = 0;
403 static int find_uframe(dwc_otg_hcd_t
*hcd
, dwc_otg_qh_t
*qh
)
407 if (qh
->speed
== USB_SPEED_HIGH
)
408 /* if this is a hs transaction we need a full frame */
409 ret
= find_single_uframe(hcd
, qh
);
411 /* FS transaction may need a sequence of frames */
412 ret
= find_multi_uframe(hcd
, qh
);
418 * Checks that the max transfer size allowed in a host channel is large enough
419 * to handle the maximum data transfer in a single (micro)frame for a periodic
422 * @param hcd The HCD state structure for the DWC OTG controller.
423 * @param qh QH for a periodic endpoint.
425 * @return 0 if successful, negative error code otherwise.
427 static int check_max_xfer_size(dwc_otg_hcd_t
*hcd
, dwc_otg_qh_t
*qh
)
430 uint32_t max_xfer_size
;
431 uint32_t max_channel_xfer_size
;
435 max_xfer_size
= dwc_max_packet(qh
->maxp
) * dwc_hb_mult(qh
->maxp
);
436 max_channel_xfer_size
= hcd
->core_if
->core_params
->max_transfer_size
;
438 if (max_xfer_size
> max_channel_xfer_size
) {
439 DWC_NOTICE("%s: Periodic xfer length %d > "
440 "max xfer length for channel %d\n",
441 __func__
, max_xfer_size
, max_channel_xfer_size
);
449 * Schedules an interrupt or isochronous transfer in the periodic schedule.
451 static int schedule_periodic(dwc_otg_hcd_t
*hcd
, dwc_otg_qh_t
*qh
)
454 struct usb_bus
*bus
= hcd_to_bus(dwc_otg_hcd_to_hcd(hcd
));
457 status
= find_uframe(hcd
, qh
);
465 /* Set the new frame up */
467 qh
->sched_frame
&= ~0x7;
468 qh
->sched_frame
|= (frame
& 7);
473 pr_notice("%s: Insufficient periodic bandwidth for "
474 "periodic transfer.\n", __func__
);
477 status
= check_max_xfer_size(hcd
, qh
);
479 pr_notice("%s: Channel max transfer size too small "
480 "for periodic transfer.\n", __func__
);
483 /* Always start in the inactive schedule. */
484 list_add_tail(&qh
->qh_list_entry
, &hcd
->periodic_sched_inactive
);
486 /* Update claimed usecs per (micro)frame. */
487 hcd
->periodic_usecs
+= qh
->usecs
;
490 * Update average periodic bandwidth claimed and # periodic reqs for
493 bus
->bandwidth_allocated
+= qh
->usecs
/ qh
->interval
;
495 if (qh
->ep_type
== USB_ENDPOINT_XFER_INT
)
496 bus
->bandwidth_int_reqs
++;
498 bus
->bandwidth_isoc_reqs
++;
504 * This function adds a QH to either the non periodic or periodic schedule if
505 * it is not already in the schedule. If the QH is already in the schedule, no
508 * @return 0 if successful, negative error code otherwise.
510 int dwc_otg_hcd_qh_add (dwc_otg_hcd_t
*hcd
, dwc_otg_qh_t
*qh
)
512 //unsigned long flags;
516 //the spinlock is locked before this function get called,
517 //but in case the lock is needed, the check function is preserved
518 //but in non-SMP mode, all spinlock is lockable.
519 //don't do the test in non-SMP mode
521 if(spin_trylock(&hcd
->lock
)) {
522 printk("%s: It is not supposed to be lockable!!\n",__func__
);
526 // SPIN_LOCK_IRQSAVE(&hcd->lock, flags)
528 if (!list_empty(&qh
->qh_list_entry
)) {
529 /* QH already in a schedule. */
533 /* Add the new QH to the appropriate schedule */
534 if (dwc_qh_is_non_per(qh
)) {
535 /* Always start in the inactive schedule. */
536 list_add_tail(&qh
->qh_list_entry
, &hcd
->non_periodic_sched_inactive
);
538 status
= schedule_periodic(hcd
, qh
);
542 // SPIN_UNLOCK_IRQRESTORE(&hcd->lock, flags)
548 * Removes an interrupt or isochronous transfer from the periodic schedule.
550 static void deschedule_periodic(dwc_otg_hcd_t
*hcd
, dwc_otg_qh_t
*qh
)
552 struct usb_bus
*bus
= hcd_to_bus(dwc_otg_hcd_to_hcd(hcd
));
555 list_del_init(&qh
->qh_list_entry
);
556 /* Update claimed usecs per (micro)frame. */
557 hcd
->periodic_usecs
-= qh
->usecs
;
558 for (i
= 0; i
< 8; i
++) {
559 hcd
->frame_usecs
[i
] += qh
->frame_usecs
[i
];
560 qh
->frame_usecs
[i
] = 0;
563 * Update average periodic bandwidth claimed and # periodic reqs for
566 bus
->bandwidth_allocated
-= qh
->usecs
/ qh
->interval
;
568 if (qh
->ep_type
== USB_ENDPOINT_XFER_INT
)
569 bus
->bandwidth_int_reqs
--;
571 bus
->bandwidth_isoc_reqs
--;
575 * Removes a QH from either the non-periodic or periodic schedule. Memory is
578 * @param[in] hcd The HCD state structure.
579 * @param[in] qh QH to remove from schedule. */
580 void dwc_otg_hcd_qh_remove (dwc_otg_hcd_t
*hcd
, dwc_otg_qh_t
*qh
)
582 //unsigned long flags;
585 //the spinlock is locked before this function get called,
586 //but in case the lock is needed, the check function is preserved
587 //but in non-SMP mode, all spinlock is lockable.
588 //don't do the test in non-SMP mode
590 if(spin_trylock(&hcd
->lock
)) {
591 printk("%s: It is not supposed to be lockable!!\n",__func__
);
595 // SPIN_LOCK_IRQSAVE(&hcd->lock, flags);
597 if (list_empty(&qh
->qh_list_entry
)) {
598 /* QH is not in a schedule. */
602 if (dwc_qh_is_non_per(qh
)) {
603 if (hcd
->non_periodic_qh_ptr
== &qh
->qh_list_entry
) {
604 hcd
->non_periodic_qh_ptr
= hcd
->non_periodic_qh_ptr
->next
;
606 list_del_init(&qh
->qh_list_entry
);
608 deschedule_periodic(hcd
, qh
);
612 // SPIN_UNLOCK_IRQRESTORE(&hcd->lock, flags);
617 * Deactivates a QH. For non-periodic QHs, removes the QH from the active
618 * non-periodic schedule. The QH is added to the inactive non-periodic
619 * schedule if any QTDs are still attached to the QH.
621 * For periodic QHs, the QH is removed from the periodic queued schedule. If
622 * there are any QTDs still attached to the QH, the QH is added to either the
623 * periodic inactive schedule or the periodic ready schedule and its next
624 * scheduled frame is calculated. The QH is placed in the ready schedule if
625 * the scheduled frame has been reached already. Otherwise it's placed in the
626 * inactive schedule. If there are no QTDs attached to the QH, the QH is
627 * completely removed from the periodic schedule.
629 void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t
*hcd
, dwc_otg_qh_t
*qh
, int sched_next_periodic_split
)
632 SPIN_LOCK_IRQSAVE(&hcd
->lock
, flags
);
634 if (dwc_qh_is_non_per(qh
)) {
635 dwc_otg_hcd_qh_remove(hcd
, qh
);
636 if (!list_empty(&qh
->qtd_list
)) {
637 /* Add back to inactive non-periodic schedule. */
638 dwc_otg_hcd_qh_add(hcd
, qh
);
641 uint16_t frame_number
= dwc_otg_hcd_get_frame_number(dwc_otg_hcd_to_hcd(hcd
));
644 /* Schedule the next continuing periodic split transfer */
645 if (sched_next_periodic_split
) {
647 qh
->sched_frame
= frame_number
;
648 if (dwc_frame_num_le(frame_number
,
649 dwc_frame_num_inc(qh
->start_split_frame
, 1))) {
651 * Allow one frame to elapse after start
652 * split microframe before scheduling
653 * complete split, but DONT if we are
654 * doing the next start split in the
655 * same frame for an ISOC out.
657 if ((qh
->ep_type
!= USB_ENDPOINT_XFER_ISOC
) || (qh
->ep_is_in
!= 0)) {
658 qh
->sched_frame
= dwc_frame_num_inc(qh
->sched_frame
, 1);
662 qh
->sched_frame
= dwc_frame_num_inc(qh
->start_split_frame
,
664 if (dwc_frame_num_le(qh
->sched_frame
, frame_number
)) {
665 qh
->sched_frame
= frame_number
;
667 qh
->sched_frame
|= 0x7;
668 qh
->start_split_frame
= qh
->sched_frame
;
671 qh
->sched_frame
= dwc_frame_num_inc(qh
->sched_frame
, qh
->interval
);
672 if (dwc_frame_num_le(qh
->sched_frame
, frame_number
)) {
673 qh
->sched_frame
= frame_number
;
677 if (list_empty(&qh
->qtd_list
)) {
678 dwc_otg_hcd_qh_remove(hcd
, qh
);
681 * Remove from periodic_sched_queued and move to
684 if (qh
->sched_frame
== frame_number
) {
685 list_move(&qh
->qh_list_entry
,
686 &hcd
->periodic_sched_ready
);
688 list_move(&qh
->qh_list_entry
,
689 &hcd
->periodic_sched_inactive
);
694 SPIN_UNLOCK_IRQRESTORE(&hcd
->lock
, flags
);
698 * This function allocates and initializes a QTD.
700 * @param[in] urb The URB to create a QTD from. Each URB-QTD pair will end up
701 * pointing to each other so each pair should have a unique correlation.
703 * @return Returns pointer to the newly allocated QTD, or NULL on error. */
704 dwc_otg_qtd_t
*dwc_otg_hcd_qtd_create (struct urb
*urb
)
708 qtd
= dwc_otg_hcd_qtd_alloc ();
713 dwc_otg_hcd_qtd_init (qtd
, urb
);
718 * Initializes a QTD structure.
720 * @param[in] qtd The QTD to initialize.
721 * @param[in] urb The URB to use for initialization. */
722 void dwc_otg_hcd_qtd_init (dwc_otg_qtd_t
*qtd
, struct urb
*urb
)
724 memset (qtd
, 0, sizeof (dwc_otg_qtd_t
));
726 if (usb_pipecontrol(urb
->pipe
)) {
728 * The only time the QTD data toggle is used is on the data
729 * phase of control transfers. This phase always starts with
732 qtd
->data_toggle
= DWC_OTG_HC_PID_DATA1
;
733 qtd
->control_phase
= DWC_OTG_CONTROL_SETUP
;
737 qtd
->complete_split
= 0;
738 qtd
->isoc_split_pos
= DWC_HCSPLIT_XACTPOS_ALL
;
739 qtd
->isoc_split_offset
= 0;
741 /* Store the qtd ptr in the urb to reference what QTD. */
747 * This function adds a QTD to the QTD-list of a QH. It will find the correct
748 * QH to place the QTD into. If it does not find a QH, then it will create a
749 * new QH. If the QH to which the QTD is added is not currently scheduled, it
750 * is placed into the proper schedule based on its EP type.
752 * @param[in] qtd The QTD to add
753 * @param[in] dwc_otg_hcd The DWC HCD structure
755 * @return 0 if successful, negative error code otherwise.
757 int dwc_otg_hcd_qtd_add (dwc_otg_qtd_t
*qtd
,
758 dwc_otg_hcd_t
*dwc_otg_hcd
)
760 struct usb_host_endpoint
*ep
;
765 struct urb
*urb
= qtd
->urb
;
767 SPIN_LOCK_IRQSAVE(&dwc_otg_hcd
->lock
, flags
);
770 * Get the QH which holds the QTD-list to insert to. Create QH if it
773 ep
= dwc_urb_to_endpoint(urb
);
774 qh
= (dwc_otg_qh_t
*)ep
->hcpriv
;
776 qh
= dwc_otg_hcd_qh_create (dwc_otg_hcd
, urb
);
783 retval
= dwc_otg_hcd_qh_add(dwc_otg_hcd
, qh
);
785 list_add_tail(&qtd
->qtd_list_entry
, &qh
->qtd_list
);
789 SPIN_UNLOCK_IRQRESTORE(&dwc_otg_hcd
->lock
, flags
);
794 #endif /* DWC_DEVICE_ONLY */