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