USB: support DWC_OTG Driver Version3.10 and used by default
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc_otg_310 / dwc_otg_hcd_queue.c
1 /* ==========================================================================
2  * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
3  * $Revision: #44 $
4  * $Date: 2011/10/26 $
5  * $Change: 1873028 $
6  *
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.
10  * 
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.
20  * 
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
31  * DAMAGE.
32  * ========================================================================== */
33 #ifndef DWC_DEVICE_ONLY
34
35 /**
36  * @file
37  *
38  * This file contains the functions to manage Queue Heads and Queue
39  * Transfer Descriptors.
40  */
41
42 #include "dwc_otg_hcd.h"
43 #include "dwc_otg_regs.h"
44
45 /** 
46  * Free each QTD in the QH's QTD-list then free the QH.  QH should already be
47  * removed from a list.  QTD list should already be empty if called from URB
48  * Dequeue.
49  *
50  * @param hcd HCD instance.
51  * @param qh The QH to free.
52  */
53 void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
54 {
55         dwc_otg_qtd_t *qtd, *qtd_tmp;
56         dwc_irqflags_t flags;
57
58         /* Free each QTD in the QTD list */
59         DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
60         DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
61                 DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
62                 dwc_otg_hcd_qtd_free(qtd);
63         }
64
65         if (hcd->core_if->dma_desc_enable) {
66                 dwc_otg_hcd_qh_free_ddma(hcd, qh);
67         } else if (qh->dw_align_buf) {
68                 uint32_t buf_size;
69                 if (qh->ep_type == UE_ISOCHRONOUS) {
70                         buf_size = 4096;
71                 } else {
72                         buf_size = hcd->core_if->core_params->max_transfer_size;
73                 }
74                 DWC_DMA_FREE(buf_size, qh->dw_align_buf, qh->dw_align_buf_dma);
75         }
76
77         DWC_FREE(qh);
78         DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
79         return;
80 }
81
82 #define BitStuffTime(bytecount)  ((8 * 7* bytecount) / 6)
83 #define HS_HOST_DELAY           5       /* nanoseconds */
84 #define FS_LS_HOST_DELAY        1000    /* nanoseconds */
85 #define HUB_LS_SETUP            333     /* nanoseconds */
86 #define NS_TO_US(ns)            ((ns + 500) / 1000)
87                                 /* convert & round nanoseconds to microseconds */
88
89 static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
90 {
91         unsigned long retval;
92
93         switch (speed) {
94         case USB_SPEED_HIGH:
95                 if (is_isoc) {
96                         retval =
97                             ((38 * 8 * 2083) +
98                              (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
99                             HS_HOST_DELAY;
100                 } else {
101                         retval =
102                             ((55 * 8 * 2083) +
103                              (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
104                             HS_HOST_DELAY;
105                 }
106                 break;
107         case USB_SPEED_FULL:
108                 if (is_isoc) {
109                         retval =
110                             (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
111                         if (is_in) {
112                                 retval = 7268 + FS_LS_HOST_DELAY + retval;
113                         } else {
114                                 retval = 6265 + FS_LS_HOST_DELAY + retval;
115                         }
116                 } else {
117                         retval =
118                             (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
119                         retval = 9107 + FS_LS_HOST_DELAY + retval;
120                 }
121                 break;
122         case USB_SPEED_LOW:
123                 if (is_in) {
124                         retval =
125                             (67667 * (31 + 10 * BitStuffTime(bytecount))) /
126                             1000;
127                         retval =
128                             64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
129                             retval;
130                 } else {
131                         retval =
132                             (66700 * (31 + 10 * BitStuffTime(bytecount))) /
133                             1000;
134                         retval =
135                             64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
136                             retval;
137                 }
138                 break;
139         default:
140                 DWC_WARN("Unknown device speed\n");
141                 retval = -1;
142         }
143
144         return NS_TO_US(retval);
145 }
146
147 /** 
148  * Initializes a QH structure.
149  *
150  * @param hcd The HCD state structure for the DWC OTG controller.
151  * @param qh  The QH to init.
152  * @param urb Holds the information about the device/endpoint that we need
153  *            to initialize the QH. 
154  */
155 #define SCHEDULE_SLOP 10
156 void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
157 {
158         char *speed, *type;
159         int dev_speed;
160         uint32_t hub_addr, hub_port;
161
162         dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
163
164         /* Initialize QH */
165         qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
166         qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
167
168         qh->data_toggle = DWC_OTG_HC_PID_DATA0;
169         qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
170         DWC_CIRCLEQ_INIT(&qh->qtd_list);
171         DWC_LIST_INIT(&qh->qh_list_entry);
172         qh->channel = NULL;
173
174         /* FS/LS Enpoint on HS Hub 
175          * NOT virtual root hub */
176         dev_speed = hcd->fops->speed(hcd, urb->priv);
177
178         hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
179         qh->do_split = 0;
180
181         if (((dev_speed == USB_SPEED_LOW) ||
182              (dev_speed == USB_SPEED_FULL)) &&
183             (hub_addr != 0 && hub_addr != 1)) {
184                 DWC_DEBUGPL(DBG_HCD,
185                             "QH init: EP %d: TT found at hub addr %d, for port %d\n",
186                             dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
187                             hub_port);
188                 qh->do_split = 1;
189         }
190
191         if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
192                 /* Compute scheduling parameters once and save them. */
193                 hprt0_data_t hprt;
194
195                 /** @todo Account for split transfers in the bus time. */
196                 int bytecount =
197                     dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
198
199                 qh->usecs =
200                     calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
201                                   qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
202                                   bytecount);
203                 /* Start in a slightly future (micro)frame. */
204                 qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
205                                                     SCHEDULE_SLOP);
206                 qh->interval = urb->interval;
207
208 #if 0
209                 /* Increase interrupt polling rate for debugging. */
210                 if (qh->ep_type == UE_INTERRUPT) {
211                         qh->interval = 8;
212                 }
213 #endif
214                 hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
215                 if ((hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) &&
216                     ((dev_speed == USB_SPEED_LOW) ||
217                      (dev_speed == USB_SPEED_FULL))) {
218                         qh->interval *= 8;
219                         qh->sched_frame |= 0x7;
220                         qh->start_split_frame = qh->sched_frame;
221                 }
222
223         }
224
225         DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
226         DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH  - qh = %p\n", qh);
227         DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH  - Device Address = %d\n",
228                     dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
229         DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH  - Endpoint %d, %s\n",
230                     dwc_otg_hcd_get_ep_num(&urb->pipe_info),
231                     dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
232         switch (dev_speed) {
233         case USB_SPEED_LOW:
234                 qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
235                 speed = "low";
236                 break;
237         case USB_SPEED_FULL:
238                 qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
239                 speed = "full";
240                 break;
241         case USB_SPEED_HIGH:
242                 qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
243                 speed = "high";
244                 break;
245         default:
246                 speed = "?";
247                 break;
248         }
249         DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH  - Speed = %s\n", speed);
250
251         switch (qh->ep_type) {
252         case UE_ISOCHRONOUS:
253                 type = "isochronous";
254                 break;
255         case UE_INTERRUPT:
256                 type = "interrupt";
257                 break;
258         case UE_CONTROL:
259                 type = "control";
260                 break;
261         case UE_BULK:
262                 type = "bulk";
263                 break;
264         default:
265                 type = "?";
266                 break;
267         }
268
269         DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH  - Type = %s\n", type);
270
271 #ifdef DEBUG
272         if (qh->ep_type == UE_INTERRUPT) {
273                 DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
274                             qh->usecs);
275                 DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
276                             qh->interval);
277         }
278 #endif
279
280 }
281
282 /**
283  * This function allocates and initializes a QH.
284  *
285  * @param hcd The HCD state structure for the DWC OTG controller.
286  * @param urb Holds the information about the device/endpoint that we need
287  *            to initialize the QH.
288  * @param atomic_alloc Flag to do atomic allocation if needed
289  *
290  * @return Returns pointer to the newly allocated QH, or NULL on error. */
291 dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
292                                     dwc_otg_hcd_urb_t * urb, int atomic_alloc)
293 {
294         dwc_otg_qh_t *qh;
295
296         /* Allocate memory */
297         /** @todo add memflags argument */
298         qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
299         if (qh == NULL) {
300                 DWC_ERROR("qh allocation failed");
301                 return NULL;
302         }
303
304         qh_init(hcd, qh, urb);
305
306         if (hcd->core_if->dma_desc_enable
307             && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
308                 dwc_otg_hcd_qh_free(hcd, qh);
309                 return NULL;
310         }
311
312         return qh;
313 }
314
315 /**
316  * Checks that a channel is available for a periodic transfer.
317  *
318  * @return 0 if successful, negative error code otherise.
319  */
320 static int periodic_channel_available(dwc_otg_hcd_t * hcd)
321 {
322         /*
323          * Currently assuming that there is a dedicated host channnel for each
324          * periodic transaction plus at least one host channel for
325          * non-periodic transactions.
326          */
327         int status;
328         int num_channels;
329
330         num_channels = hcd->core_if->core_params->host_channels;
331         if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
332             && (hcd->periodic_channels < num_channels - 1)) {
333                 status = 0;
334         } else {
335                 DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
336                         __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels);    //NOTICE
337                 status = -DWC_E_NO_SPACE;
338         }
339
340         return status;
341 }
342
343 /**
344  * Checks that there is sufficient bandwidth for the specified QH in the
345  * periodic schedule. For simplicity, this calculation assumes that all the
346  * transfers in the periodic schedule may occur in the same (micro)frame.
347  *
348  * @param hcd The HCD state structure for the DWC OTG controller.
349  * @param qh QH containing periodic bandwidth required.
350  *
351  * @return 0 if successful, negative error code otherwise.
352  */
353 static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
354 {
355         int status;
356         int16_t max_claimed_usecs;
357
358         status = 0;
359
360         if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
361                 /*
362                  * High speed mode.
363                  * Max periodic usecs is 80% x 125 usec = 100 usec.
364                  */
365
366                 max_claimed_usecs = 100 - qh->usecs;
367         } else {
368                 /*
369                  * Full speed mode.
370                  * Max periodic usecs is 90% x 1000 usec = 900 usec.
371                  */
372                 max_claimed_usecs = 900 - qh->usecs;
373         }
374
375         if (hcd->periodic_usecs > max_claimed_usecs) {
376                 DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs);        //NOTICE
377                 status = -DWC_E_NO_SPACE;
378         }
379
380         return status;
381 }
382
383 /**
384  * Checks that the max transfer size allowed in a host channel is large enough
385  * to handle the maximum data transfer in a single (micro)frame for a periodic
386  * transfer.
387  *
388  * @param hcd The HCD state structure for the DWC OTG controller.
389  * @param qh QH for a periodic endpoint.
390  *
391  * @return 0 if successful, negative error code otherwise.
392  */
393 static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
394 {
395         int status;
396         uint32_t max_xfer_size;
397         uint32_t max_channel_xfer_size;
398
399         status = 0;
400
401         max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
402         max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
403
404         if (max_xfer_size > max_channel_xfer_size) {
405                 DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
406                                 __func__, max_xfer_size, max_channel_xfer_size);        //NOTICE
407                 status = -DWC_E_NO_SPACE;
408         }
409
410         return status;
411 }
412
413 /**
414  * Schedules an interrupt or isochronous transfer in the periodic schedule.
415  *
416  * @param hcd The HCD state structure for the DWC OTG controller.
417  * @param qh QH for the periodic transfer. The QH should already contain the
418  * scheduling information.
419  *
420  * @return 0 if successful, negative error code otherwise.
421  */
422 static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
423 {
424         int status = 0;
425
426         status = periodic_channel_available(hcd);
427         if (status) {
428                 DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__);        //NOTICE
429                 return status;
430         }
431
432         status = check_periodic_bandwidth(hcd, qh);
433         if (status) {
434                 DWC_INFO("%s: Insufficient periodic bandwidth for " "periodic transfer.\n", __func__);  //NOTICE
435                 return status;
436         }
437
438         status = check_max_xfer_size(hcd, qh);
439         if (status) {
440                 DWC_INFO("%s: Channel max transfer size too small " "for periodic transfer.\n", __func__);      //NOTICE
441                 return status;
442         }
443
444         if (hcd->core_if->dma_desc_enable) {
445                 /* Don't rely on SOF and start in ready schedule */
446                 DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
447         }
448         else {
449         /* Always start in the inactive schedule. */
450         DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
451         }
452
453         /* Reserve the periodic channel. */
454         hcd->periodic_channels++;
455
456         /* Update claimed usecs per (micro)frame. */
457         hcd->periodic_usecs += qh->usecs;
458
459         return status;
460 }
461
462 /**
463  * This function adds a QH to either the non periodic or periodic schedule if
464  * it is not already in the schedule. If the QH is already in the schedule, no
465  * action is taken.
466  *
467  * @return 0 if successful, negative error code otherwise.
468  */
469 int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
470 {
471         int status = 0;
472         gintmsk_data_t intr_mask = {.d32 = 0 };
473
474         if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
475                 /* QH already in a schedule. */
476                 return status;
477         }
478
479         /* Add the new QH to the appropriate schedule */
480         if (dwc_qh_is_non_per(qh)) {
481                 /* Always start in the inactive schedule. */
482                 DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
483                                      &qh->qh_list_entry);
484         } else {
485                 status = schedule_periodic(hcd, qh);
486                 if ( !hcd->periodic_qh_count ) {
487                         intr_mask.b.sofintr = 1;
488                         DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
489                                                                 intr_mask.d32, intr_mask.d32);
490                 }
491                 hcd->periodic_qh_count++;
492         }
493
494         return status;
495 }
496
497 /**
498  * Removes an interrupt or isochronous transfer from the periodic schedule.
499  *
500  * @param hcd The HCD state structure for the DWC OTG controller.
501  * @param qh QH for the periodic transfer.
502  */
503 static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
504 {
505         DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
506
507         /* Release the periodic channel reservation. */
508         hcd->periodic_channels--;
509
510         /* Update claimed usecs per (micro)frame. */
511         hcd->periodic_usecs -= qh->usecs;
512 }
513
514 /** 
515  * Removes a QH from either the non-periodic or periodic schedule.  Memory is
516  * not freed.
517  *
518  * @param hcd The HCD state structure.
519  * @param qh QH to remove from schedule. */
520 void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
521 {
522         gintmsk_data_t intr_mask = {.d32 = 0 };
523
524         if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
525                 /* QH is not in a schedule. */
526                 return;
527         }
528
529         if (dwc_qh_is_non_per(qh)) {
530                 if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
531                         hcd->non_periodic_qh_ptr =
532                             hcd->non_periodic_qh_ptr->next;
533                 }
534                 DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
535         } else {
536                 deschedule_periodic(hcd, qh);
537                 hcd->periodic_qh_count--;
538                 if( !hcd->periodic_qh_count ) {
539                         intr_mask.b.sofintr = 1;
540                                 DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
541                                                                         intr_mask.d32, 0);
542                 }
543         }
544 }
545
546 /**
547  * Deactivates a QH. For non-periodic QHs, removes the QH from the active
548  * non-periodic schedule. The QH is added to the inactive non-periodic
549  * schedule if any QTDs are still attached to the QH.
550  *
551  * For periodic QHs, the QH is removed from the periodic queued schedule. If
552  * there are any QTDs still attached to the QH, the QH is added to either the
553  * periodic inactive schedule or the periodic ready schedule and its next
554  * scheduled frame is calculated. The QH is placed in the ready schedule if
555  * the scheduled frame has been reached already. Otherwise it's placed in the
556  * inactive schedule. If there are no QTDs attached to the QH, the QH is
557  * completely removed from the periodic schedule.
558  */
559 void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
560                                int sched_next_periodic_split)
561 {       
562         if (dwc_qh_is_non_per(qh)) {
563                 dwc_otg_hcd_qh_remove(hcd, qh);
564                 if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
565                         /* Add back to inactive non-periodic schedule. */
566                         dwc_otg_hcd_qh_add(hcd, qh);
567                 }
568         } else {
569                 uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
570
571                 if (qh->do_split) {
572                         /* Schedule the next continuing periodic split transfer */
573                         if (sched_next_periodic_split) {
574
575                                 qh->sched_frame = frame_number;
576                                 if (dwc_frame_num_le(frame_number,
577                                                      dwc_frame_num_inc
578                                                      (qh->start_split_frame,
579                                                       1))) {
580                                         /*
581                                          * Allow one frame to elapse after start
582                                          * split microframe before scheduling
583                                          * complete split, but DONT if we are
584                                          * doing the next start split in the
585                                          * same frame for an ISOC out.
586                                          */
587                                         if ((qh->ep_type != UE_ISOCHRONOUS) ||
588                                             (qh->ep_is_in != 0)) {
589                                                 qh->sched_frame =
590                                                     dwc_frame_num_inc(qh->sched_frame, 1);
591                                         }
592                                 }
593                         } else {
594                                 qh->sched_frame =
595                                     dwc_frame_num_inc(qh->start_split_frame,
596                                                       qh->interval);
597                                 if (dwc_frame_num_le
598                                     (qh->sched_frame, frame_number)) {
599                                         qh->sched_frame = frame_number;
600                                 }
601                                 qh->sched_frame |= 0x7;
602                                 qh->start_split_frame = qh->sched_frame;
603                         }
604                 } else {
605                         qh->sched_frame =
606                             dwc_frame_num_inc(qh->sched_frame, qh->interval);
607                         if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
608                                 qh->sched_frame = frame_number;
609                         }
610                 }
611
612                 if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
613                         dwc_otg_hcd_qh_remove(hcd, qh);
614                 } else {
615                         /*
616                          * Remove from periodic_sched_queued and move to
617                          * appropriate queue.
618                          */
619                         if (qh->sched_frame == frame_number) {
620                                 DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
621                                                    &qh->qh_list_entry);
622                         } else {
623                                 DWC_LIST_MOVE_HEAD
624                                     (&hcd->periodic_sched_inactive,
625                                      &qh->qh_list_entry);
626                         }
627                 }
628         }
629 }
630
631 /** 
632  * This function allocates and initializes a QTD. 
633  *
634  * @param urb The URB to create a QTD from.  Each URB-QTD pair will end up
635  *            pointing to each other so each pair should have a unique correlation.
636  * @param atomic_alloc Flag to do atomic alloc if needed
637  *
638  * @return Returns pointer to the newly allocated QTD, or NULL on error. */
639 dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
640 {
641         dwc_otg_qtd_t *qtd;
642
643         qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
644         if (qtd == NULL) {
645                 return NULL;
646         }
647
648         dwc_otg_hcd_qtd_init(qtd, urb);
649         return qtd;
650 }
651
652 /** 
653  * Initializes a QTD structure.
654  *
655  * @param qtd The QTD to initialize.
656  * @param urb The URB to use for initialization.  */
657 void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
658 {
659         dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
660         qtd->urb = urb;
661         if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
662                 /*
663                  * The only time the QTD data toggle is used is on the data
664                  * phase of control transfers. This phase always starts with
665                  * DATA1.
666                  */
667                 qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
668                 qtd->control_phase = DWC_OTG_CONTROL_SETUP;
669         }
670
671         /* start split */
672         qtd->complete_split = 0;
673         qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
674         qtd->isoc_split_offset = 0;
675         qtd->in_process = 0;
676
677         /* Store the qtd ptr in the urb to reference what QTD. */
678         urb->qtd = qtd;
679         return;
680 }
681
682 /**
683  * This function adds a QTD to the QTD-list of a QH.  It will find the correct
684  * QH to place the QTD into.  If it does not find a QH, then it will create a
685  * new QH. If the QH to which the QTD is added is not currently scheduled, it
686  * is placed into the proper schedule based on its EP type.
687  *
688  * @param[in] qtd The QTD to add
689  * @param[in] hcd The DWC HCD structure
690  * @param[out] qh out parameter to return queue head
691  * @param atomic_alloc Flag to do atomic alloc if needed
692  *
693  * @return 0 if successful, negative error code otherwise.
694  */
695 int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
696                         dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
697 {
698         int retval = 0;
699         dwc_irqflags_t flags;
700
701         dwc_otg_hcd_urb_t *urb = qtd->urb;
702
703         /*
704          * Get the QH which holds the QTD-list to insert to. Create QH if it
705          * doesn't exist.
706          */
707         if (*qh == NULL) {
708                 *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
709                 if (*qh == NULL) {
710                         retval = -1;
711                         goto done;
712                 }
713         }
714         DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
715         retval = dwc_otg_hcd_qh_add(hcd, *qh);
716         if (retval == 0) {
717                 DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
718                                         qtd_list_entry);
719         }
720         DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
721
722 done:
723
724         return retval;
725 }
726
727 #endif /* DWC_DEVICE_ONLY */