usb: dwc_otg_310: support vbus controlled by both gpio and pmic
[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_DEV_DMA_FREE(buf_size, qh->dw_align_buf,
74                                  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,
337                          hcd->non_periodic_channels);
338                 status = -DWC_E_NO_SPACE;
339         }
340
341         return status;
342 }
343
344 /**
345  * Checks that there is sufficient bandwidth for the specified QH in the
346  * periodic schedule. For simplicity, this calculation assumes that all the
347  * transfers in the periodic schedule may occur in the same (micro)frame.
348  *
349  * @param hcd The HCD state structure for the DWC OTG controller.
350  * @param qh QH containing periodic bandwidth required.
351  *
352  * @return 0 if successful, negative error code otherwise.
353  */
354 static int check_periodic_bandwidth(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
355 {
356         int status;
357         int16_t max_claimed_usecs;
358
359         status = 0;
360
361         if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
362                 /*
363                  * High speed mode.
364                  * Max periodic usecs is 80% x 125 usec = 100 usec.
365                  */
366
367                 max_claimed_usecs = 100 - qh->usecs;
368         } else {
369                 /*
370                  * Full speed mode.
371                  * Max periodic usecs is 90% x 1000 usec = 900 usec.
372                  */
373                 max_claimed_usecs = 900 - qh->usecs;
374         }
375
376         if (hcd->periodic_usecs > max_claimed_usecs) {
377                 DWC_INFO("%s: already claimed usecs %d, required usecs %d\n",
378                          __func__, hcd->periodic_usecs, qh->usecs);
379                 status = -DWC_E_NO_SPACE;
380         }
381
382         return status;
383 }
384
385 /**
386  * Checks that the max transfer size allowed in a host channel is large enough
387  * to handle the maximum data transfer in a single (micro)frame for a periodic
388  * transfer.
389  *
390  * @param hcd The HCD state structure for the DWC OTG controller.
391  * @param qh QH for a periodic endpoint.
392  *
393  * @return 0 if successful, negative error code otherwise.
394  */
395 static int check_max_xfer_size(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
396 {
397         int status;
398         uint32_t max_xfer_size;
399         uint32_t max_channel_xfer_size;
400
401         status = 0;
402
403         max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
404         max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
405
406         if (max_xfer_size > max_channel_xfer_size) {
407                 DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
408                          __func__, max_xfer_size, max_channel_xfer_size);
409                 status = -DWC_E_NO_SPACE;
410         }
411
412         return status;
413 }
414
415 /**
416  * Schedules an interrupt or isochronous transfer in the periodic schedule.
417  *
418  * @param hcd The HCD state structure for the DWC OTG controller.
419  * @param qh QH for the periodic transfer. The QH should already contain the
420  * scheduling information.
421  *
422  * @return 0 if successful, negative error code otherwise.
423  */
424 static int schedule_periodic(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
425 {
426         int status = 0;
427
428         status = periodic_channel_available(hcd);
429         if (status) {
430                 DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__);
431                 return status;
432         }
433
434         status = check_periodic_bandwidth(hcd, qh);
435         if (status) {
436                 DWC_INFO("%s: Insufficient periodic bandwidth for " "periodic transfer.\n", __func__);
437                 return status;
438         }
439
440         status = check_max_xfer_size(hcd, qh);
441         if (status) {
442                 DWC_INFO("%s: Channel max transfer size too small " "for periodic transfer.\n", __func__);
443                 return status;
444         }
445
446         if (hcd->core_if->dma_desc_enable) {
447                 /* Don't rely on SOF and start in ready schedule */
448                 DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready,
449                                      &qh->qh_list_entry);
450         } else {
451                 /* Always start in the inactive schedule. */
452                 DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive,
453                                      &qh->qh_list_entry);
454         }
455
456         /* Reserve the periodic channel. */
457         hcd->periodic_channels++;
458
459         /* Update claimed usecs per (micro)frame. */
460         hcd->periodic_usecs += qh->usecs;
461
462         return status;
463 }
464
465 /**
466  * This function adds a QH to either the non periodic or periodic schedule if
467  * it is not already in the schedule. If the QH is already in the schedule, no
468  * action is taken.
469  *
470  * @return 0 if successful, negative error code otherwise.
471  */
472 int dwc_otg_hcd_qh_add(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
473 {
474         int status = 0;
475         gintmsk_data_t intr_mask = {.d32 = 0 };
476
477         if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
478                 /* QH already in a schedule. */
479                 return status;
480         }
481
482         /* Add the new QH to the appropriate schedule */
483         if (dwc_qh_is_non_per(qh)) {
484                 /* Always start in the inactive schedule. */
485                 DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
486                                      &qh->qh_list_entry);
487         } else {
488                 status = schedule_periodic(hcd, qh);
489                 if (!hcd->periodic_qh_count) {
490                         intr_mask.b.sofintr = 1;
491                         DWC_MODIFY_REG32(&hcd->core_if->
492                                          core_global_regs->gintmsk,
493                                          intr_mask.d32, intr_mask.d32);
494                 }
495                 hcd->periodic_qh_count++;
496         }
497
498         return status;
499 }
500
501 /**
502  * Removes an interrupt or isochronous transfer from the periodic schedule.
503  *
504  * @param hcd The HCD state structure for the DWC OTG controller.
505  * @param qh QH for the periodic transfer.
506  */
507 static void deschedule_periodic(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
508 {
509         DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
510
511         /* Release the periodic channel reservation. */
512         hcd->periodic_channels--;
513
514         /* Update claimed usecs per (micro)frame. */
515         hcd->periodic_usecs -= qh->usecs;
516 }
517
518 /**
519  * Removes a QH from either the non-periodic or periodic schedule.  Memory is
520  * not freed.
521  *
522  * @param hcd The HCD state structure.
523  * @param qh QH to remove from schedule. */
524 void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
525 {
526         gintmsk_data_t intr_mask = {.d32 = 0 };
527
528         if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
529                 /* QH is not in a schedule. */
530                 return;
531         }
532
533         if (dwc_qh_is_non_per(qh)) {
534                 if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
535                         hcd->non_periodic_qh_ptr =
536                             hcd->non_periodic_qh_ptr->next;
537                 }
538                 DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
539         } else {
540                 deschedule_periodic(hcd, qh);
541                 hcd->periodic_qh_count--;
542                 if (!hcd->periodic_qh_count) {
543                         intr_mask.b.sofintr = 1;
544                         DWC_MODIFY_REG32(&hcd->core_if->
545                                          core_global_regs->gintmsk,
546                                          intr_mask.d32, 0);
547                 }
548         }
549 }
550
551 /**
552  * Deactivates a QH. For non-periodic QHs, removes the QH from the active
553  * non-periodic schedule. The QH is added to the inactive non-periodic
554  * schedule if any QTDs are still attached to the QH.
555  *
556  * For periodic QHs, the QH is removed from the periodic queued schedule. If
557  * there are any QTDs still attached to the QH, the QH is added to either the
558  * periodic inactive schedule or the periodic ready schedule and its next
559  * scheduled frame is calculated. The QH is placed in the ready schedule if
560  * the scheduled frame has been reached already. Otherwise it's placed in the
561  * inactive schedule. If there are no QTDs attached to the QH, the QH is
562  * completely removed from the periodic schedule.
563  */
564 void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh,
565                                int sched_next_periodic_split)
566 {
567         if (dwc_qh_is_non_per(qh)) {
568                 dwc_otg_hcd_qh_remove(hcd, qh);
569                 if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
570                         /* Add back to inactive non-periodic schedule. */
571                         dwc_otg_hcd_qh_add(hcd, qh);
572                 }
573         } else {
574                 uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
575
576                 if (qh->do_split) {
577                         /* Schedule the next continuing periodic split transfer */
578                         if (sched_next_periodic_split) {
579
580                                 qh->sched_frame = frame_number;
581                                 if (dwc_frame_num_le(frame_number,
582                                                      dwc_frame_num_inc
583                                                      (qh->start_split_frame,
584                                                       1))) {
585                                         /*
586                                          * Allow one frame to elapse after start
587                                          * split microframe before scheduling
588                                          * complete split, but DONT if we are
589                                          * doing the next start split in the
590                                          * same frame for an ISOC out.
591                                          */
592                                         if ((qh->ep_type != UE_ISOCHRONOUS) ||
593                                             (qh->ep_is_in != 0)) {
594                                                 qh->sched_frame =
595                                                     dwc_frame_num_inc
596                                                     (qh->sched_frame, 1);
597                                         }
598                                 }
599                         } else {
600                                 qh->sched_frame =
601                                     dwc_frame_num_inc(qh->start_split_frame,
602                                                       qh->interval);
603                                 if (dwc_frame_num_le
604                                     (qh->sched_frame, frame_number)) {
605                                         qh->sched_frame = frame_number;
606                                 }
607                                 qh->sched_frame |= 0x7;
608                                 qh->start_split_frame = qh->sched_frame;
609                         }
610                 } else {
611                         qh->sched_frame =
612                             dwc_frame_num_inc(qh->sched_frame, qh->interval);
613                         if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
614                                 qh->sched_frame = frame_number;
615                         }
616                 }
617
618                 if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
619                         dwc_otg_hcd_qh_remove(hcd, qh);
620                 } else {
621                         /*
622                          * Remove from periodic_sched_queued and move to
623                          * appropriate queue.
624                          */
625                         if (qh->sched_frame == frame_number) {
626                                 DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
627                                                    &qh->qh_list_entry);
628                         } else {
629                                 DWC_LIST_MOVE_HEAD
630                                     (&hcd->periodic_sched_inactive,
631                                      &qh->qh_list_entry);
632                         }
633                 }
634         }
635 }
636
637 /**
638  * This function allocates and initializes a QTD.
639  *
640  * @param urb The URB to create a QTD from.  Each URB-QTD pair will end up
641  *            pointing to each other so each pair should have a unique correlation.
642  * @param atomic_alloc Flag to do atomic alloc if needed
643  *
644  * @return Returns pointer to the newly allocated QTD, or NULL on error. */
645 dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t *urb, int atomic_alloc)
646 {
647         dwc_otg_qtd_t *qtd;
648
649         qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
650         if (qtd == NULL) {
651                 return NULL;
652         }
653
654         dwc_otg_hcd_qtd_init(qtd, urb);
655         return qtd;
656 }
657
658 /**
659  * Initializes a QTD structure.
660  *
661  * @param qtd The QTD to initialize.
662  * @param urb The URB to use for initialization.  */
663 void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t *qtd, dwc_otg_hcd_urb_t *urb)
664 {
665         dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
666         qtd->urb = urb;
667         if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
668                 /*
669                  * The only time the QTD data toggle is used is on the data
670                  * phase of control transfers. This phase always starts with
671                  * DATA1.
672                  */
673                 qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
674                 qtd->control_phase = DWC_OTG_CONTROL_SETUP;
675         }
676
677         /* start split */
678         qtd->complete_split = 0;
679         qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
680         qtd->isoc_split_offset = 0;
681         qtd->in_process = 0;
682
683         /* Store the qtd ptr in the urb to reference what QTD. */
684         urb->qtd = qtd;
685         return;
686 }
687
688 /**
689  * This function adds a QTD to the QTD-list of a QH.  It will find the correct
690  * QH to place the QTD into.  If it does not find a QH, then it will create a
691  * new QH. If the QH to which the QTD is added is not currently scheduled, it
692  * is placed into the proper schedule based on its EP type.
693  *
694  * @param[in] qtd The QTD to add
695  * @param[in] hcd The DWC HCD structure
696  * @param[out] qh out parameter to return queue head
697  * @param atomic_alloc Flag to do atomic alloc if needed
698  *
699  * @return 0 if successful, negative error code otherwise.
700  */
701 int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t *qtd,
702                         dwc_otg_hcd_t *hcd, dwc_otg_qh_t **qh,
703                         int atomic_alloc)
704 {
705         int retval = 0;
706
707         dwc_otg_hcd_urb_t *urb = qtd->urb;
708
709         /*
710          * Get the QH which holds the QTD-list to insert to. Create QH if it
711          * doesn't exist.
712          */
713         if (*qh == NULL) {
714                 *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
715                 if (*qh == NULL) {
716                         retval = -1;
717                         goto done;
718                 }
719         }
720         qtd->qh = *qh;
721         retval = dwc_otg_hcd_qh_add(hcd, *qh);
722         if (retval == 0) {
723                 DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
724                                         qtd_list_entry);
725         }
726
727 done:
728
729         return retval;
730 }
731
732 #endif /* DWC_DEVICE_ONLY */