Home | History | Annotate | Line # | Download | only in dist
dwc2_hcdqueue.c revision 1.8
      1  1.8  skrll /*	$NetBSD: dwc2_hcdqueue.c,v 1.8 2014/03/21 09:19:10 skrll Exp $	*/
      2  1.4  skrll 
      3  1.1  skrll /*
      4  1.1  skrll  * hcd_queue.c - DesignWare HS OTG Controller host queuing routines
      5  1.1  skrll  *
      6  1.1  skrll  * Copyright (C) 2004-2013 Synopsys, Inc.
      7  1.1  skrll  *
      8  1.1  skrll  * Redistribution and use in source and binary forms, with or without
      9  1.1  skrll  * modification, are permitted provided that the following conditions
     10  1.1  skrll  * are met:
     11  1.1  skrll  * 1. Redistributions of source code must retain the above copyright
     12  1.1  skrll  *    notice, this list of conditions, and the following disclaimer,
     13  1.1  skrll  *    without modification.
     14  1.1  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  skrll  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  skrll  *    documentation and/or other materials provided with the distribution.
     17  1.1  skrll  * 3. The names of the above-listed copyright holders may not be used
     18  1.1  skrll  *    to endorse or promote products derived from this software without
     19  1.1  skrll  *    specific prior written permission.
     20  1.1  skrll  *
     21  1.1  skrll  * ALTERNATIVELY, this software may be distributed under the terms of the
     22  1.1  skrll  * GNU General Public License ("GPL") as published by the Free Software
     23  1.1  skrll  * Foundation; either version 2 of the License, or (at your option) any
     24  1.1  skrll  * later version.
     25  1.1  skrll  *
     26  1.1  skrll  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     27  1.1  skrll  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     28  1.1  skrll  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  skrll  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     30  1.1  skrll  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     31  1.1  skrll  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     32  1.1  skrll  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     33  1.1  skrll  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     34  1.1  skrll  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     35  1.1  skrll  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     36  1.1  skrll  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     37  1.1  skrll  */
     38  1.1  skrll 
     39  1.1  skrll /*
     40  1.1  skrll  * This file contains the functions to manage Queue Heads and Queue
     41  1.1  skrll  * Transfer Descriptors for Host mode
     42  1.1  skrll  */
     43  1.2  skrll 
     44  1.2  skrll #include <sys/cdefs.h>
     45  1.8  skrll __KERNEL_RCSID(0, "$NetBSD: dwc2_hcdqueue.c,v 1.8 2014/03/21 09:19:10 skrll Exp $");
     46  1.2  skrll 
     47  1.2  skrll #include <sys/types.h>
     48  1.2  skrll #include <sys/kmem.h>
     49  1.2  skrll #include <sys/pool.h>
     50  1.2  skrll 
     51  1.2  skrll #include <dev/usb/usb.h>
     52  1.2  skrll #include <dev/usb/usbdi.h>
     53  1.2  skrll #include <dev/usb/usbdivar.h>
     54  1.2  skrll #include <dev/usb/usb_mem.h>
     55  1.2  skrll 
     56  1.2  skrll #include <machine/param.h>
     57  1.2  skrll 
     58  1.1  skrll #include <linux/kernel.h>
     59  1.1  skrll 
     60  1.2  skrll #include <dwc2/dwc2.h>
     61  1.2  skrll #include <dwc2/dwc2var.h>
     62  1.2  skrll 
     63  1.2  skrll #include "dwc2_core.h"
     64  1.2  skrll #include "dwc2_hcd.h"
     65  1.1  skrll 
     66  1.2  skrll static u32 dwc2_calc_bus_time(struct dwc2_hsotg *, int, int, int, int);
     67  1.1  skrll 
     68  1.1  skrll /**
     69  1.1  skrll  * dwc2_qh_init() - Initializes a QH structure
     70  1.1  skrll  *
     71  1.1  skrll  * @hsotg: The HCD state structure for the DWC OTG controller
     72  1.1  skrll  * @qh:    The QH to init
     73  1.1  skrll  * @urb:   Holds the information about the device/endpoint needed to initialize
     74  1.1  skrll  *         the QH
     75  1.1  skrll  */
     76  1.1  skrll #define SCHEDULE_SLOP 10
     77  1.1  skrll static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
     78  1.1  skrll 			 struct dwc2_hcd_urb *urb)
     79  1.1  skrll {
     80  1.1  skrll 	int dev_speed, hub_addr, hub_port;
     81  1.1  skrll 
     82  1.1  skrll 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
     83  1.1  skrll 
     84  1.1  skrll 	/* Initialize QH */
     85  1.1  skrll 	qh->ep_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
     86  1.1  skrll 	qh->ep_is_in = dwc2_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
     87  1.1  skrll 
     88  1.1  skrll 	qh->data_toggle = DWC2_HC_PID_DATA0;
     89  1.1  skrll 	qh->maxp = dwc2_hcd_get_mps(&urb->pipe_info);
     90  1.1  skrll 	INIT_LIST_HEAD(&qh->qtd_list);
     91  1.1  skrll 	INIT_LIST_HEAD(&qh->qh_list_entry);
     92  1.1  skrll 
     93  1.1  skrll 	/* FS/LS Endpoint on HS Hub, NOT virtual root hub */
     94  1.1  skrll 	dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
     95  1.1  skrll 
     96  1.1  skrll 	dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
     97  1.1  skrll 
     98  1.1  skrll 	if ((dev_speed == USB_SPEED_LOW || dev_speed == USB_SPEED_FULL) &&
     99  1.1  skrll 	    hub_addr != 0 && hub_addr != 1) {
    100  1.1  skrll 		dev_vdbg(hsotg->dev,
    101  1.1  skrll 			 "QH init: EP %d: TT found at hub addr %d, for port %d\n",
    102  1.1  skrll 			 dwc2_hcd_get_ep_num(&urb->pipe_info), hub_addr,
    103  1.1  skrll 			 hub_port);
    104  1.1  skrll 		qh->do_split = 1;
    105  1.1  skrll 	}
    106  1.1  skrll 
    107  1.1  skrll 	if (qh->ep_type == USB_ENDPOINT_XFER_INT ||
    108  1.1  skrll 	    qh->ep_type == USB_ENDPOINT_XFER_ISOC) {
    109  1.1  skrll 		/* Compute scheduling parameters once and save them */
    110  1.1  skrll 		u32 hprt, prtspd;
    111  1.1  skrll 
    112  1.1  skrll 		/* Todo: Account for split transfers in the bus time */
    113  1.1  skrll 		int bytecount =
    114  1.1  skrll 			dwc2_hb_mult(qh->maxp) * dwc2_max_packet(qh->maxp);
    115  1.1  skrll 
    116  1.2  skrll 		qh->usecs = dwc2_calc_bus_time(hsotg, qh->do_split ?
    117  1.1  skrll 				USB_SPEED_HIGH : dev_speed, qh->ep_is_in,
    118  1.1  skrll 				qh->ep_type == USB_ENDPOINT_XFER_ISOC,
    119  1.2  skrll 				bytecount);
    120  1.1  skrll 		/* Start in a slightly future (micro)frame */
    121  1.1  skrll 		qh->sched_frame = dwc2_frame_num_inc(hsotg->frame_number,
    122  1.1  skrll 						     SCHEDULE_SLOP);
    123  1.1  skrll 		qh->interval = urb->interval;
    124  1.1  skrll #if 0
    125  1.1  skrll 		/* Increase interrupt polling rate for debugging */
    126  1.1  skrll 		if (qh->ep_type == USB_ENDPOINT_XFER_INT)
    127  1.1  skrll 			qh->interval = 8;
    128  1.1  skrll #endif
    129  1.2  skrll 		hprt = DWC2_READ_4(hsotg, HPRT0);
    130  1.3  skrll 		prtspd = (hprt & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
    131  1.1  skrll 		if (prtspd == HPRT0_SPD_HIGH_SPEED &&
    132  1.1  skrll 		    (dev_speed == USB_SPEED_LOW ||
    133  1.1  skrll 		     dev_speed == USB_SPEED_FULL)) {
    134  1.1  skrll 			qh->interval *= 8;
    135  1.1  skrll 			qh->sched_frame |= 0x7;
    136  1.1  skrll 			qh->start_split_frame = qh->sched_frame;
    137  1.1  skrll 		}
    138  1.1  skrll 		dev_dbg(hsotg->dev, "interval=%d\n", qh->interval);
    139  1.1  skrll 	}
    140  1.1  skrll 
    141  1.1  skrll 	dev_vdbg(hsotg->dev, "DWC OTG HCD QH Initialized\n");
    142  1.1  skrll 	dev_vdbg(hsotg->dev, "DWC OTG HCD QH - qh = %p\n", qh);
    143  1.1  skrll 	dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Device Address = %d\n",
    144  1.1  skrll 		 dwc2_hcd_get_dev_addr(&urb->pipe_info));
    145  1.1  skrll 	dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Endpoint %d, %s\n",
    146  1.1  skrll 		 dwc2_hcd_get_ep_num(&urb->pipe_info),
    147  1.1  skrll 		 dwc2_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
    148  1.1  skrll 
    149  1.1  skrll 	qh->dev_speed = dev_speed;
    150  1.1  skrll 
    151  1.8  skrll #ifdef DWC2_DEBUG
    152  1.8  skrll 	const char *speed, *type;
    153  1.1  skrll 	switch (dev_speed) {
    154  1.1  skrll 	case USB_SPEED_LOW:
    155  1.1  skrll 		speed = "low";
    156  1.1  skrll 		break;
    157  1.1  skrll 	case USB_SPEED_FULL:
    158  1.1  skrll 		speed = "full";
    159  1.1  skrll 		break;
    160  1.1  skrll 	case USB_SPEED_HIGH:
    161  1.1  skrll 		speed = "high";
    162  1.1  skrll 		break;
    163  1.1  skrll 	default:
    164  1.1  skrll 		speed = "?";
    165  1.1  skrll 		break;
    166  1.1  skrll 	}
    167  1.1  skrll 	dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Speed = %s\n", speed);
    168  1.1  skrll 
    169  1.1  skrll 	switch (qh->ep_type) {
    170  1.1  skrll 	case USB_ENDPOINT_XFER_ISOC:
    171  1.1  skrll 		type = "isochronous";
    172  1.1  skrll 		break;
    173  1.1  skrll 	case USB_ENDPOINT_XFER_INT:
    174  1.1  skrll 		type = "interrupt";
    175  1.1  skrll 		break;
    176  1.1  skrll 	case USB_ENDPOINT_XFER_CONTROL:
    177  1.1  skrll 		type = "control";
    178  1.1  skrll 		break;
    179  1.1  skrll 	case USB_ENDPOINT_XFER_BULK:
    180  1.1  skrll 		type = "bulk";
    181  1.1  skrll 		break;
    182  1.1  skrll 	default:
    183  1.1  skrll 		type = "?";
    184  1.1  skrll 		break;
    185  1.1  skrll 	}
    186  1.1  skrll 
    187  1.1  skrll 	dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Type = %s\n", type);
    188  1.8  skrll #endif
    189  1.1  skrll 
    190  1.1  skrll 	if (qh->ep_type == USB_ENDPOINT_XFER_INT) {
    191  1.1  skrll 		dev_vdbg(hsotg->dev, "DWC OTG HCD QH - usecs = %d\n",
    192  1.1  skrll 			 qh->usecs);
    193  1.1  skrll 		dev_vdbg(hsotg->dev, "DWC OTG HCD QH - interval = %d\n",
    194  1.1  skrll 			 qh->interval);
    195  1.1  skrll 	}
    196  1.1  skrll }
    197  1.1  skrll 
    198  1.1  skrll /**
    199  1.1  skrll  * dwc2_hcd_qh_create() - Allocates and initializes a QH
    200  1.1  skrll  *
    201  1.3  skrll  * @hsotg:     The HCD state structure for the DWC OTG controller
    202  1.3  skrll  * @urb:       Holds the information about the device/endpoint needed
    203  1.3  skrll  *             to initialize the QH
    204  1.3  skrll  * @mem_flags: Flag to do atomic allocation if needed
    205  1.1  skrll  *
    206  1.1  skrll  * Return: Pointer to the newly allocated QH, or NULL on error
    207  1.1  skrll  */
    208  1.1  skrll static struct dwc2_qh *dwc2_hcd_qh_create(struct dwc2_hsotg *hsotg,
    209  1.1  skrll 					  struct dwc2_hcd_urb *urb,
    210  1.1  skrll 					  gfp_t mem_flags)
    211  1.1  skrll {
    212  1.2  skrll 	struct dwc2_softc *sc = hsotg->hsotg_sc;
    213  1.1  skrll 	struct dwc2_qh *qh;
    214  1.1  skrll 
    215  1.1  skrll 	if (!urb->priv)
    216  1.1  skrll 		return NULL;
    217  1.1  skrll 
    218  1.1  skrll 	/* Allocate memory */
    219  1.2  skrll 	qh = pool_cache_get(sc->sc_qhpool, PR_NOWAIT);
    220  1.1  skrll 	if (!qh)
    221  1.1  skrll 		return NULL;
    222  1.1  skrll 
    223  1.2  skrll 	memset(qh, 0, sizeof(*qh));
    224  1.1  skrll 	dwc2_qh_init(hsotg, qh, urb);
    225  1.1  skrll 
    226  1.1  skrll 	if (hsotg->core_params->dma_desc_enable > 0 &&
    227  1.1  skrll 	    dwc2_hcd_qh_init_ddma(hsotg, qh, mem_flags) < 0) {
    228  1.1  skrll 		dwc2_hcd_qh_free(hsotg, qh);
    229  1.1  skrll 		return NULL;
    230  1.1  skrll 	}
    231  1.1  skrll 
    232  1.1  skrll 	return qh;
    233  1.1  skrll }
    234  1.1  skrll 
    235  1.1  skrll /**
    236  1.1  skrll  * dwc2_hcd_qh_free() - Frees the QH
    237  1.1  skrll  *
    238  1.1  skrll  * @hsotg: HCD instance
    239  1.1  skrll  * @qh:    The QH to free
    240  1.1  skrll  *
    241  1.1  skrll  * QH should already be removed from the list. QTD list should already be empty
    242  1.1  skrll  * if called from URB Dequeue.
    243  1.1  skrll  *
    244  1.1  skrll  * Must NOT be called with interrupt disabled or spinlock held
    245  1.1  skrll  */
    246  1.1  skrll void dwc2_hcd_qh_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
    247  1.1  skrll {
    248  1.2  skrll 	struct dwc2_softc *sc = hsotg->hsotg_sc;
    249  1.1  skrll 
    250  1.1  skrll 	if (hsotg->core_params->dma_desc_enable > 0) {
    251  1.1  skrll 		dwc2_hcd_qh_free_ddma(hsotg, qh);
    252  1.1  skrll 	} else if (qh->dw_align_buf) {
    253  1.2  skrll 		/* XXXNH */
    254  1.2  skrll 		usb_freemem(&hsotg->hsotg_sc->sc_bus, &qh->dw_align_buf_usbdma);
    255  1.1  skrll 	}
    256  1.1  skrll 
    257  1.2  skrll 	pool_cache_put(sc->sc_qhpool, qh);
    258  1.1  skrll }
    259  1.1  skrll 
    260  1.1  skrll /**
    261  1.1  skrll  * dwc2_periodic_channel_available() - Checks that a channel is available for a
    262  1.1  skrll  * periodic transfer
    263  1.1  skrll  *
    264  1.1  skrll  * @hsotg: The HCD state structure for the DWC OTG controller
    265  1.1  skrll  *
    266  1.3  skrll  * Return: 0 if successful, negative error code otherwise
    267  1.1  skrll  */
    268  1.1  skrll static int dwc2_periodic_channel_available(struct dwc2_hsotg *hsotg)
    269  1.1  skrll {
    270  1.1  skrll 	/*
    271  1.3  skrll 	 * Currently assuming that there is a dedicated host channel for
    272  1.1  skrll 	 * each periodic transaction plus at least one host channel for
    273  1.1  skrll 	 * non-periodic transactions
    274  1.1  skrll 	 */
    275  1.1  skrll 	int status;
    276  1.1  skrll 	int num_channels;
    277  1.1  skrll 
    278  1.1  skrll 	num_channels = hsotg->core_params->host_channels;
    279  1.1  skrll 	if (hsotg->periodic_channels + hsotg->non_periodic_channels <
    280  1.1  skrll 								num_channels
    281  1.1  skrll 	    && hsotg->periodic_channels < num_channels - 1) {
    282  1.1  skrll 		status = 0;
    283  1.1  skrll 	} else {
    284  1.1  skrll 		dev_dbg(hsotg->dev,
    285  1.1  skrll 			"%s: Total channels: %d, Periodic: %d, "
    286  1.1  skrll 			"Non-periodic: %d\n", __func__, num_channels,
    287  1.1  skrll 			hsotg->periodic_channels, hsotg->non_periodic_channels);
    288  1.1  skrll 		status = -ENOSPC;
    289  1.1  skrll 	}
    290  1.1  skrll 
    291  1.1  skrll 	return status;
    292  1.1  skrll }
    293  1.1  skrll 
    294  1.1  skrll /**
    295  1.1  skrll  * dwc2_check_periodic_bandwidth() - Checks that there is sufficient bandwidth
    296  1.1  skrll  * for the specified QH in the periodic schedule
    297  1.1  skrll  *
    298  1.1  skrll  * @hsotg: The HCD state structure for the DWC OTG controller
    299  1.1  skrll  * @qh:    QH containing periodic bandwidth required
    300  1.1  skrll  *
    301  1.1  skrll  * Return: 0 if successful, negative error code otherwise
    302  1.1  skrll  *
    303  1.1  skrll  * For simplicity, this calculation assumes that all the transfers in the
    304  1.1  skrll  * periodic schedule may occur in the same (micro)frame
    305  1.1  skrll  */
    306  1.1  skrll static int dwc2_check_periodic_bandwidth(struct dwc2_hsotg *hsotg,
    307  1.1  skrll 					 struct dwc2_qh *qh)
    308  1.1  skrll {
    309  1.1  skrll 	int status;
    310  1.1  skrll 	s16 max_claimed_usecs;
    311  1.1  skrll 
    312  1.1  skrll 	status = 0;
    313  1.1  skrll 
    314  1.1  skrll 	if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
    315  1.1  skrll 		/*
    316  1.1  skrll 		 * High speed mode
    317  1.1  skrll 		 * Max periodic usecs is 80% x 125 usec = 100 usec
    318  1.1  skrll 		 */
    319  1.1  skrll 		max_claimed_usecs = 100 - qh->usecs;
    320  1.1  skrll 	} else {
    321  1.1  skrll 		/*
    322  1.1  skrll 		 * Full speed mode
    323  1.1  skrll 		 * Max periodic usecs is 90% x 1000 usec = 900 usec
    324  1.1  skrll 		 */
    325  1.1  skrll 		max_claimed_usecs = 900 - qh->usecs;
    326  1.1  skrll 	}
    327  1.1  skrll 
    328  1.1  skrll 	if (hsotg->periodic_usecs > max_claimed_usecs) {
    329  1.1  skrll 		dev_err(hsotg->dev,
    330  1.1  skrll 			"%s: already claimed usecs %d, required usecs %d\n",
    331  1.1  skrll 			__func__, hsotg->periodic_usecs, qh->usecs);
    332  1.1  skrll 		status = -ENOSPC;
    333  1.1  skrll 	}
    334  1.1  skrll 
    335  1.1  skrll 	return status;
    336  1.1  skrll }
    337  1.1  skrll 
    338  1.1  skrll /**
    339  1.1  skrll  * Microframe scheduler
    340  1.1  skrll  * track the total use in hsotg->frame_usecs
    341  1.1  skrll  * keep each qh use in qh->frame_usecs
    342  1.1  skrll  * when surrendering the qh then donate the time back
    343  1.1  skrll  */
    344  1.1  skrll static const unsigned short max_uframe_usecs[] = {
    345  1.1  skrll 	100, 100, 100, 100, 100, 100, 30, 0
    346  1.1  skrll };
    347  1.1  skrll 
    348  1.1  skrll void dwc2_hcd_init_usecs(struct dwc2_hsotg *hsotg)
    349  1.1  skrll {
    350  1.1  skrll 	int i;
    351  1.1  skrll 
    352  1.1  skrll 	for (i = 0; i < 8; i++)
    353  1.1  skrll 		hsotg->frame_usecs[i] = max_uframe_usecs[i];
    354  1.1  skrll }
    355  1.1  skrll 
    356  1.1  skrll static int dwc2_find_single_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
    357  1.1  skrll {
    358  1.1  skrll 	unsigned short utime = qh->usecs;
    359  1.6  skrll 	int i;
    360  1.1  skrll 
    361  1.6  skrll 	for (i = 0; i < 8; i++) {
    362  1.1  skrll 		/* At the start hsotg->frame_usecs[i] = max_uframe_usecs[i] */
    363  1.1  skrll 		if (utime <= hsotg->frame_usecs[i]) {
    364  1.1  skrll 			hsotg->frame_usecs[i] -= utime;
    365  1.1  skrll 			qh->frame_usecs[i] += utime;
    366  1.6  skrll 			return i;
    367  1.1  skrll 		}
    368  1.1  skrll 	}
    369  1.6  skrll 	return -1;
    370  1.1  skrll }
    371  1.1  skrll 
    372  1.1  skrll /*
    373  1.1  skrll  * use this for FS apps that can span multiple uframes
    374  1.1  skrll  */
    375  1.1  skrll static int dwc2_find_multi_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
    376  1.1  skrll {
    377  1.1  skrll 	unsigned short utime = qh->usecs;
    378  1.1  skrll 	unsigned short xtime;
    379  1.6  skrll 	int t_left;
    380  1.6  skrll 	int i;
    381  1.1  skrll 	int j;
    382  1.6  skrll 	int k;
    383  1.1  skrll 
    384  1.6  skrll 	for (i = 0; i < 8; i++) {
    385  1.6  skrll 		if (hsotg->frame_usecs[i] <= 0)
    386  1.1  skrll 			continue;
    387  1.1  skrll 
    388  1.1  skrll 		/*
    389  1.1  skrll 		 * we need n consecutive slots so use j as a start slot
    390  1.1  skrll 		 * j plus j+1 must be enough time (for now)
    391  1.1  skrll 		 */
    392  1.1  skrll 		xtime = hsotg->frame_usecs[i];
    393  1.1  skrll 		for (j = i + 1; j < 8; j++) {
    394  1.1  skrll 			/*
    395  1.1  skrll 			 * if we add this frame remaining time to xtime we may
    396  1.1  skrll 			 * be OK, if not we need to test j for a complete frame
    397  1.1  skrll 			 */
    398  1.1  skrll 			if (xtime + hsotg->frame_usecs[j] < utime) {
    399  1.1  skrll 				if (hsotg->frame_usecs[j] <
    400  1.6  skrll 							max_uframe_usecs[j])
    401  1.6  skrll 					continue;
    402  1.1  skrll 			}
    403  1.1  skrll 			if (xtime >= utime) {
    404  1.6  skrll 				t_left = utime;
    405  1.6  skrll 				for (k = i; k < 8; k++) {
    406  1.6  skrll 					t_left -= hsotg->frame_usecs[k];
    407  1.6  skrll 					if (t_left <= 0) {
    408  1.6  skrll 						qh->frame_usecs[k] +=
    409  1.6  skrll 							hsotg->frame_usecs[k]
    410  1.6  skrll 								+ t_left;
    411  1.6  skrll 						hsotg->frame_usecs[k] = -t_left;
    412  1.6  skrll 						return i;
    413  1.6  skrll 					} else {
    414  1.6  skrll 						qh->frame_usecs[k] +=
    415  1.6  skrll 							hsotg->frame_usecs[k];
    416  1.6  skrll 						hsotg->frame_usecs[k] = 0;
    417  1.6  skrll 					}
    418  1.6  skrll 				}
    419  1.1  skrll 			}
    420  1.1  skrll 			/* add the frame time to x time */
    421  1.1  skrll 			xtime += hsotg->frame_usecs[j];
    422  1.1  skrll 			/* we must have a fully available next frame or break */
    423  1.1  skrll 			if (xtime < utime &&
    424  1.6  skrll 			   hsotg->frame_usecs[j] == max_uframe_usecs[j])
    425  1.6  skrll 				continue;
    426  1.1  skrll 		}
    427  1.1  skrll 	}
    428  1.6  skrll 	return -1;
    429  1.1  skrll }
    430  1.1  skrll 
    431  1.1  skrll static int dwc2_find_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
    432  1.1  skrll {
    433  1.1  skrll 	int ret;
    434  1.1  skrll 
    435  1.1  skrll 	if (qh->dev_speed == USB_SPEED_HIGH) {
    436  1.1  skrll 		/* if this is a hs transaction we need a full frame */
    437  1.1  skrll 		ret = dwc2_find_single_uframe(hsotg, qh);
    438  1.1  skrll 	} else {
    439  1.1  skrll 		/*
    440  1.1  skrll 		 * if this is a fs transaction we may need a sequence
    441  1.1  skrll 		 * of frames
    442  1.1  skrll 		 */
    443  1.1  skrll 		ret = dwc2_find_multi_uframe(hsotg, qh);
    444  1.1  skrll 	}
    445  1.1  skrll 	return ret;
    446  1.1  skrll }
    447  1.1  skrll 
    448  1.1  skrll /**
    449  1.1  skrll  * dwc2_check_max_xfer_size() - Checks that the max transfer size allowed in a
    450  1.1  skrll  * host channel is large enough to handle the maximum data transfer in a single
    451  1.1  skrll  * (micro)frame for a periodic transfer
    452  1.1  skrll  *
    453  1.1  skrll  * @hsotg: The HCD state structure for the DWC OTG controller
    454  1.1  skrll  * @qh:    QH for a periodic endpoint
    455  1.1  skrll  *
    456  1.1  skrll  * Return: 0 if successful, negative error code otherwise
    457  1.1  skrll  */
    458  1.1  skrll static int dwc2_check_max_xfer_size(struct dwc2_hsotg *hsotg,
    459  1.1  skrll 				    struct dwc2_qh *qh)
    460  1.1  skrll {
    461  1.1  skrll 	u32 max_xfer_size;
    462  1.1  skrll 	u32 max_channel_xfer_size;
    463  1.1  skrll 	int status = 0;
    464  1.1  skrll 
    465  1.1  skrll 	max_xfer_size = dwc2_max_packet(qh->maxp) * dwc2_hb_mult(qh->maxp);
    466  1.1  skrll 	max_channel_xfer_size = hsotg->core_params->max_transfer_size;
    467  1.1  skrll 
    468  1.1  skrll 	if (max_xfer_size > max_channel_xfer_size) {
    469  1.1  skrll 		dev_err(hsotg->dev,
    470  1.1  skrll 			"%s: Periodic xfer length %d > max xfer length for channel %d\n",
    471  1.1  skrll 			__func__, max_xfer_size, max_channel_xfer_size);
    472  1.1  skrll 		status = -ENOSPC;
    473  1.1  skrll 	}
    474  1.1  skrll 
    475  1.1  skrll 	return status;
    476  1.1  skrll }
    477  1.1  skrll 
    478  1.1  skrll /**
    479  1.1  skrll  * dwc2_schedule_periodic() - Schedules an interrupt or isochronous transfer in
    480  1.1  skrll  * the periodic schedule
    481  1.1  skrll  *
    482  1.1  skrll  * @hsotg: The HCD state structure for the DWC OTG controller
    483  1.1  skrll  * @qh:    QH for the periodic transfer. The QH should already contain the
    484  1.1  skrll  *         scheduling information.
    485  1.1  skrll  *
    486  1.1  skrll  * Return: 0 if successful, negative error code otherwise
    487  1.1  skrll  */
    488  1.1  skrll static int dwc2_schedule_periodic(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
    489  1.1  skrll {
    490  1.1  skrll 	int status;
    491  1.1  skrll 
    492  1.1  skrll 	if (hsotg->core_params->uframe_sched > 0) {
    493  1.1  skrll 		int frame = -1;
    494  1.1  skrll 
    495  1.1  skrll 		status = dwc2_find_uframe(hsotg, qh);
    496  1.1  skrll 		if (status == 0)
    497  1.1  skrll 			frame = 7;
    498  1.1  skrll 		else if (status > 0)
    499  1.1  skrll 			frame = status - 1;
    500  1.1  skrll 
    501  1.1  skrll 		/* Set the new frame up */
    502  1.1  skrll 		if (frame > -1) {
    503  1.1  skrll 			qh->sched_frame &= ~0x7;
    504  1.1  skrll 			qh->sched_frame |= (frame & 7);
    505  1.1  skrll 		}
    506  1.1  skrll 
    507  1.1  skrll 		if (status != -1)
    508  1.1  skrll 			status = 0;
    509  1.1  skrll 	} else {
    510  1.1  skrll 		status = dwc2_periodic_channel_available(hsotg);
    511  1.1  skrll 		if (status) {
    512  1.1  skrll 			dev_info(hsotg->dev,
    513  1.1  skrll 				 "%s: No host channel available for periodic transfer\n",
    514  1.1  skrll 				 __func__);
    515  1.1  skrll 			return status;
    516  1.1  skrll 		}
    517  1.1  skrll 
    518  1.1  skrll 		status = dwc2_check_periodic_bandwidth(hsotg, qh);
    519  1.1  skrll 	}
    520  1.1  skrll 
    521  1.1  skrll 	if (status) {
    522  1.1  skrll 		dev_dbg(hsotg->dev,
    523  1.1  skrll 			"%s: Insufficient periodic bandwidth for periodic transfer\n",
    524  1.1  skrll 			__func__);
    525  1.1  skrll 		return status;
    526  1.1  skrll 	}
    527  1.1  skrll 
    528  1.1  skrll 	status = dwc2_check_max_xfer_size(hsotg, qh);
    529  1.1  skrll 	if (status) {
    530  1.1  skrll 		dev_dbg(hsotg->dev,
    531  1.1  skrll 			"%s: Channel max transfer size too small for periodic transfer\n",
    532  1.1  skrll 			__func__);
    533  1.1  skrll 		return status;
    534  1.1  skrll 	}
    535  1.1  skrll 
    536  1.3  skrll 	if (hsotg->core_params->dma_desc_enable > 0)
    537  1.1  skrll 		/* Don't rely on SOF and start in ready schedule */
    538  1.1  skrll 		list_add_tail(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
    539  1.3  skrll 	else
    540  1.1  skrll 		/* Always start in inactive schedule */
    541  1.1  skrll 		list_add_tail(&qh->qh_list_entry,
    542  1.1  skrll 			      &hsotg->periodic_sched_inactive);
    543  1.1  skrll 
    544  1.1  skrll 	if (hsotg->core_params->uframe_sched <= 0)
    545  1.1  skrll 		/* Reserve periodic channel */
    546  1.1  skrll 		hsotg->periodic_channels++;
    547  1.1  skrll 
    548  1.1  skrll 	/* Update claimed usecs per (micro)frame */
    549  1.1  skrll 	hsotg->periodic_usecs += qh->usecs;
    550  1.1  skrll 
    551  1.1  skrll 	return status;
    552  1.1  skrll }
    553  1.1  skrll 
    554  1.1  skrll /**
    555  1.1  skrll  * dwc2_deschedule_periodic() - Removes an interrupt or isochronous transfer
    556  1.1  skrll  * from the periodic schedule
    557  1.1  skrll  *
    558  1.1  skrll  * @hsotg: The HCD state structure for the DWC OTG controller
    559  1.1  skrll  * @qh:	   QH for the periodic transfer
    560  1.1  skrll  */
    561  1.1  skrll static void dwc2_deschedule_periodic(struct dwc2_hsotg *hsotg,
    562  1.1  skrll 				     struct dwc2_qh *qh)
    563  1.1  skrll {
    564  1.1  skrll 	int i;
    565  1.1  skrll 
    566  1.1  skrll 	list_del_init(&qh->qh_list_entry);
    567  1.1  skrll 
    568  1.1  skrll 	/* Update claimed usecs per (micro)frame */
    569  1.1  skrll 	hsotg->periodic_usecs -= qh->usecs;
    570  1.1  skrll 
    571  1.1  skrll 	if (hsotg->core_params->uframe_sched > 0) {
    572  1.1  skrll 		for (i = 0; i < 8; i++) {
    573  1.1  skrll 			hsotg->frame_usecs[i] += qh->frame_usecs[i];
    574  1.1  skrll 			qh->frame_usecs[i] = 0;
    575  1.1  skrll 		}
    576  1.1  skrll 	} else {
    577  1.1  skrll 		/* Release periodic channel reservation */
    578  1.1  skrll 		hsotg->periodic_channels--;
    579  1.1  skrll 	}
    580  1.1  skrll }
    581  1.1  skrll 
    582  1.1  skrll /**
    583  1.1  skrll  * dwc2_hcd_qh_add() - Adds a QH to either the non periodic or periodic
    584  1.1  skrll  * schedule if it is not already in the schedule. If the QH is already in
    585  1.1  skrll  * the schedule, no action is taken.
    586  1.1  skrll  *
    587  1.1  skrll  * @hsotg: The HCD state structure for the DWC OTG controller
    588  1.1  skrll  * @qh:    The QH to add
    589  1.1  skrll  *
    590  1.1  skrll  * Return: 0 if successful, negative error code otherwise
    591  1.1  skrll  */
    592  1.1  skrll int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
    593  1.1  skrll {
    594  1.1  skrll 	int status = 0;
    595  1.1  skrll 	u32 intr_mask;
    596  1.1  skrll 
    597  1.1  skrll 	if (dbg_qh(qh))
    598  1.1  skrll 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
    599  1.1  skrll 
    600  1.1  skrll 	if (!list_empty(&qh->qh_list_entry))
    601  1.1  skrll 		/* QH already in a schedule */
    602  1.1  skrll 		return status;
    603  1.1  skrll 
    604  1.1  skrll 	/* Add the new QH to the appropriate schedule */
    605  1.1  skrll 	if (dwc2_qh_is_non_per(qh)) {
    606  1.1  skrll 		/* Always start in inactive schedule */
    607  1.1  skrll 		list_add_tail(&qh->qh_list_entry,
    608  1.1  skrll 			      &hsotg->non_periodic_sched_inactive);
    609  1.1  skrll 	} else {
    610  1.1  skrll 		status = dwc2_schedule_periodic(hsotg, qh);
    611  1.1  skrll 		if (status == 0) {
    612  1.1  skrll 			if (!hsotg->periodic_qh_count) {
    613  1.2  skrll 				intr_mask = DWC2_READ_4(hsotg, GINTMSK);
    614  1.1  skrll 				intr_mask |= GINTSTS_SOF;
    615  1.2  skrll 				DWC2_WRITE_4(hsotg, GINTMSK, intr_mask);
    616  1.1  skrll 			}
    617  1.1  skrll 			hsotg->periodic_qh_count++;
    618  1.1  skrll 		}
    619  1.1  skrll 	}
    620  1.1  skrll 
    621  1.1  skrll 	return status;
    622  1.1  skrll }
    623  1.1  skrll 
    624  1.1  skrll /**
    625  1.1  skrll  * dwc2_hcd_qh_unlink() - Removes a QH from either the non-periodic or periodic
    626  1.1  skrll  * schedule. Memory is not freed.
    627  1.1  skrll  *
    628  1.1  skrll  * @hsotg: The HCD state structure
    629  1.1  skrll  * @qh:    QH to remove from schedule
    630  1.1  skrll  */
    631  1.1  skrll void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
    632  1.1  skrll {
    633  1.1  skrll 	u32 intr_mask;
    634  1.1  skrll 
    635  1.1  skrll 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
    636  1.1  skrll 
    637  1.1  skrll 	if (list_empty(&qh->qh_list_entry))
    638  1.1  skrll 		/* QH is not in a schedule */
    639  1.1  skrll 		return;
    640  1.1  skrll 
    641  1.1  skrll 	if (dwc2_qh_is_non_per(qh)) {
    642  1.1  skrll 		if (hsotg->non_periodic_qh_ptr == &qh->qh_list_entry)
    643  1.1  skrll 			hsotg->non_periodic_qh_ptr =
    644  1.1  skrll 					hsotg->non_periodic_qh_ptr->next;
    645  1.1  skrll 		list_del_init(&qh->qh_list_entry);
    646  1.1  skrll 	} else {
    647  1.1  skrll 		dwc2_deschedule_periodic(hsotg, qh);
    648  1.1  skrll 		hsotg->periodic_qh_count--;
    649  1.1  skrll 		if (!hsotg->periodic_qh_count) {
    650  1.2  skrll 			intr_mask = DWC2_READ_4(hsotg, GINTMSK);
    651  1.1  skrll 			intr_mask &= ~GINTSTS_SOF;
    652  1.2  skrll 			DWC2_WRITE_4(hsotg, GINTMSK, intr_mask);
    653  1.1  skrll 		}
    654  1.1  skrll 	}
    655  1.1  skrll }
    656  1.1  skrll 
    657  1.1  skrll /*
    658  1.1  skrll  * Schedule the next continuing periodic split transfer
    659  1.1  skrll  */
    660  1.1  skrll static void dwc2_sched_periodic_split(struct dwc2_hsotg *hsotg,
    661  1.1  skrll 				      struct dwc2_qh *qh, u16 frame_number,
    662  1.1  skrll 				      int sched_next_periodic_split)
    663  1.1  skrll {
    664  1.1  skrll 	u16 incr;
    665  1.1  skrll 
    666  1.1  skrll 	if (sched_next_periodic_split) {
    667  1.1  skrll 		qh->sched_frame = frame_number;
    668  1.1  skrll 		incr = dwc2_frame_num_inc(qh->start_split_frame, 1);
    669  1.1  skrll 		if (dwc2_frame_num_le(frame_number, incr)) {
    670  1.1  skrll 			/*
    671  1.1  skrll 			 * Allow one frame to elapse after start split
    672  1.1  skrll 			 * microframe before scheduling complete split, but
    673  1.1  skrll 			 * DON'T if we are doing the next start split in the
    674  1.1  skrll 			 * same frame for an ISOC out
    675  1.1  skrll 			 */
    676  1.1  skrll 			if (qh->ep_type != USB_ENDPOINT_XFER_ISOC ||
    677  1.1  skrll 			    qh->ep_is_in != 0) {
    678  1.1  skrll 				qh->sched_frame =
    679  1.1  skrll 					dwc2_frame_num_inc(qh->sched_frame, 1);
    680  1.1  skrll 			}
    681  1.1  skrll 		}
    682  1.1  skrll 	} else {
    683  1.1  skrll 		qh->sched_frame = dwc2_frame_num_inc(qh->start_split_frame,
    684  1.1  skrll 						     qh->interval);
    685  1.1  skrll 		if (dwc2_frame_num_le(qh->sched_frame, frame_number))
    686  1.1  skrll 			qh->sched_frame = frame_number;
    687  1.1  skrll 		qh->sched_frame |= 0x7;
    688  1.1  skrll 		qh->start_split_frame = qh->sched_frame;
    689  1.1  skrll 	}
    690  1.1  skrll }
    691  1.1  skrll 
    692  1.1  skrll /*
    693  1.1  skrll  * Deactivates a QH. For non-periodic QHs, removes the QH from the active
    694  1.1  skrll  * non-periodic schedule. The QH is added to the inactive non-periodic
    695  1.1  skrll  * schedule if any QTDs are still attached to the QH.
    696  1.1  skrll  *
    697  1.1  skrll  * For periodic QHs, the QH is removed from the periodic queued schedule. If
    698  1.1  skrll  * there are any QTDs still attached to the QH, the QH is added to either the
    699  1.1  skrll  * periodic inactive schedule or the periodic ready schedule and its next
    700  1.1  skrll  * scheduled frame is calculated. The QH is placed in the ready schedule if
    701  1.1  skrll  * the scheduled frame has been reached already. Otherwise it's placed in the
    702  1.1  skrll  * inactive schedule. If there are no QTDs attached to the QH, the QH is
    703  1.1  skrll  * completely removed from the periodic schedule.
    704  1.1  skrll  */
    705  1.1  skrll void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
    706  1.1  skrll 			    int sched_next_periodic_split)
    707  1.1  skrll {
    708  1.1  skrll 	if (dbg_qh(qh))
    709  1.1  skrll 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
    710  1.1  skrll 
    711  1.1  skrll 	if (dwc2_qh_is_non_per(qh)) {
    712  1.1  skrll 		dwc2_hcd_qh_unlink(hsotg, qh);
    713  1.1  skrll 		if (!list_empty(&qh->qtd_list))
    714  1.1  skrll 			/* Add back to inactive non-periodic schedule */
    715  1.1  skrll 			dwc2_hcd_qh_add(hsotg, qh);
    716  1.1  skrll 	} else {
    717  1.1  skrll 		u16 frame_number = dwc2_hcd_get_frame_number(hsotg);
    718  1.1  skrll 
    719  1.1  skrll 		if (qh->do_split) {
    720  1.1  skrll 			dwc2_sched_periodic_split(hsotg, qh, frame_number,
    721  1.1  skrll 						  sched_next_periodic_split);
    722  1.1  skrll 		} else {
    723  1.1  skrll 			qh->sched_frame = dwc2_frame_num_inc(qh->sched_frame,
    724  1.1  skrll 							     qh->interval);
    725  1.1  skrll 			if (dwc2_frame_num_le(qh->sched_frame, frame_number))
    726  1.1  skrll 				qh->sched_frame = frame_number;
    727  1.1  skrll 		}
    728  1.1  skrll 
    729  1.1  skrll 		if (list_empty(&qh->qtd_list)) {
    730  1.1  skrll 			dwc2_hcd_qh_unlink(hsotg, qh);
    731  1.1  skrll 		} else {
    732  1.1  skrll 			/*
    733  1.1  skrll 			 * Remove from periodic_sched_queued and move to
    734  1.1  skrll 			 * appropriate queue
    735  1.1  skrll 			 */
    736  1.1  skrll 			if ((hsotg->core_params->uframe_sched > 0 &&
    737  1.1  skrll 			     dwc2_frame_num_le(qh->sched_frame, frame_number))
    738  1.1  skrll 			 || (hsotg->core_params->uframe_sched <= 0 &&
    739  1.3  skrll 			     qh->sched_frame == frame_number))
    740  1.1  skrll 				list_move(&qh->qh_list_entry,
    741  1.1  skrll 					  &hsotg->periodic_sched_ready);
    742  1.3  skrll 			else
    743  1.1  skrll 				list_move(&qh->qh_list_entry,
    744  1.1  skrll 					  &hsotg->periodic_sched_inactive);
    745  1.1  skrll 		}
    746  1.1  skrll 	}
    747  1.1  skrll }
    748  1.1  skrll 
    749  1.1  skrll /**
    750  1.1  skrll  * dwc2_hcd_qtd_init() - Initializes a QTD structure
    751  1.1  skrll  *
    752  1.1  skrll  * @qtd: The QTD to initialize
    753  1.1  skrll  * @urb: The associated URB
    754  1.1  skrll  */
    755  1.1  skrll void dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
    756  1.1  skrll {
    757  1.1  skrll 	qtd->urb = urb;
    758  1.1  skrll 	if (dwc2_hcd_get_pipe_type(&urb->pipe_info) ==
    759  1.1  skrll 			USB_ENDPOINT_XFER_CONTROL) {
    760  1.1  skrll 		/*
    761  1.1  skrll 		 * The only time the QTD data toggle is used is on the data
    762  1.1  skrll 		 * phase of control transfers. This phase always starts with
    763  1.1  skrll 		 * DATA1.
    764  1.1  skrll 		 */
    765  1.1  skrll 		qtd->data_toggle = DWC2_HC_PID_DATA1;
    766  1.1  skrll 		qtd->control_phase = DWC2_CONTROL_SETUP;
    767  1.1  skrll 	}
    768  1.1  skrll 
    769  1.1  skrll 	/* Start split */
    770  1.1  skrll 	qtd->complete_split = 0;
    771  1.1  skrll 	qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
    772  1.1  skrll 	qtd->isoc_split_offset = 0;
    773  1.1  skrll 	qtd->in_process = 0;
    774  1.1  skrll 
    775  1.1  skrll 	/* Store the qtd ptr in the urb to reference the QTD */
    776  1.1  skrll 	urb->qtd = qtd;
    777  1.1  skrll }
    778  1.1  skrll 
    779  1.1  skrll /**
    780  1.1  skrll  * dwc2_hcd_qtd_add() - Adds a QTD to the QTD-list of a QH
    781  1.1  skrll  *
    782  1.3  skrll  * @hsotg:     The DWC HCD structure
    783  1.3  skrll  * @qtd:       The QTD to add
    784  1.3  skrll  * @qh:        Out parameter to return queue head
    785  1.3  skrll  * @mem_flags: Flag to do atomic alloc if needed
    786  1.1  skrll  *
    787  1.1  skrll  * Return: 0 if successful, negative error code otherwise
    788  1.1  skrll  *
    789  1.1  skrll  * Finds the correct QH to place the QTD into. If it does not find a QH, it
    790  1.1  skrll  * will create a new QH. If the QH to which the QTD is added is not currently
    791  1.1  skrll  * scheduled, it is placed into the proper schedule based on its EP type.
    792  1.7  skrll  *
    793  1.7  skrll  * HCD lock must be held and interrupts must be disabled on entry
    794  1.1  skrll  */
    795  1.1  skrll int dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
    796  1.1  skrll 		     struct dwc2_qh **qh, gfp_t mem_flags)
    797  1.1  skrll {
    798  1.1  skrll 	struct dwc2_hcd_urb *urb = qtd->urb;
    799  1.1  skrll 	int allocated = 0;
    800  1.1  skrll 	int retval;
    801  1.1  skrll 
    802  1.1  skrll 	/*
    803  1.1  skrll 	 * Get the QH which holds the QTD-list to insert to. Create QH if it
    804  1.1  skrll 	 * doesn't exist.
    805  1.1  skrll 	 */
    806  1.1  skrll 	if (*qh == NULL) {
    807  1.1  skrll 		*qh = dwc2_hcd_qh_create(hsotg, urb, mem_flags);
    808  1.1  skrll 		if (*qh == NULL)
    809  1.1  skrll 			return -ENOMEM;
    810  1.1  skrll 		allocated = 1;
    811  1.1  skrll 	}
    812  1.1  skrll 
    813  1.1  skrll 	retval = dwc2_hcd_qh_add(hsotg, *qh);
    814  1.1  skrll 	if (retval)
    815  1.1  skrll 		goto fail;
    816  1.1  skrll 
    817  1.1  skrll 	qtd->qh = *qh;
    818  1.1  skrll 	list_add_tail(&qtd->qtd_list_entry, &(*qh)->qtd_list);
    819  1.1  skrll 
    820  1.1  skrll 	return 0;
    821  1.1  skrll 
    822  1.1  skrll fail:
    823  1.1  skrll 	if (allocated) {
    824  1.1  skrll 		struct dwc2_qtd *qtd2, *qtd2_tmp;
    825  1.1  skrll 		struct dwc2_qh *qh_tmp = *qh;
    826  1.1  skrll 
    827  1.1  skrll 		*qh = NULL;
    828  1.1  skrll 		dwc2_hcd_qh_unlink(hsotg, qh_tmp);
    829  1.1  skrll 
    830  1.1  skrll 		/* Free each QTD in the QH's QTD list */
    831  1.1  skrll 		list_for_each_entry_safe(qtd2, qtd2_tmp, &qh_tmp->qtd_list,
    832  1.1  skrll 					 qtd_list_entry)
    833  1.1  skrll 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh_tmp);
    834  1.1  skrll 
    835  1.1  skrll 		dwc2_hcd_qh_free(hsotg, qh_tmp);
    836  1.1  skrll 	}
    837  1.1  skrll 
    838  1.1  skrll 	return retval;
    839  1.1  skrll }
    840  1.2  skrll 
    841  1.2  skrll void dwc2_hcd_qtd_unlink_and_free(struct dwc2_hsotg *hsotg,
    842  1.2  skrll 				  struct dwc2_qtd *qtd,
    843  1.2  skrll 				  struct dwc2_qh *qh)
    844  1.2  skrll {
    845  1.2  skrll 	struct dwc2_softc *sc = hsotg->hsotg_sc;
    846  1.2  skrll 
    847  1.2  skrll 	list_del_init(&qtd->qtd_list_entry);
    848  1.2  skrll  	pool_cache_put(sc->sc_qtdpool, qtd);
    849  1.2  skrll }
    850  1.2  skrll 
    851  1.2  skrll #define BITSTUFFTIME(bytecount)	((8 * 7 * (bytecount)) / 6)
    852  1.2  skrll #define HS_HOST_DELAY		5	/* nanoseconds */
    853  1.2  skrll #define FS_LS_HOST_DELAY	1000	/* nanoseconds */
    854  1.2  skrll #define HUB_LS_SETUP		333	/* nanoseconds */
    855  1.2  skrll 
    856  1.2  skrll static u32 dwc2_calc_bus_time(struct dwc2_hsotg *hsotg, int speed, int is_in,
    857  1.2  skrll 			      int is_isoc, int bytecount)
    858  1.2  skrll {
    859  1.2  skrll 	unsigned long retval;
    860  1.2  skrll 
    861  1.2  skrll 	switch (speed) {
    862  1.2  skrll 	case USB_SPEED_HIGH:
    863  1.2  skrll 		if (is_isoc)
    864  1.2  skrll 			retval =
    865  1.2  skrll 			    ((38 * 8 * 2083) +
    866  1.2  skrll 			     (2083 * (3 + BITSTUFFTIME(bytecount)))) / 1000 +
    867  1.2  skrll 			    HS_HOST_DELAY;
    868  1.2  skrll 		else
    869  1.2  skrll 			retval =
    870  1.2  skrll 			    ((55 * 8 * 2083) +
    871  1.2  skrll 			     (2083 * (3 + BITSTUFFTIME(bytecount)))) / 1000 +
    872  1.2  skrll 			    HS_HOST_DELAY;
    873  1.2  skrll 		break;
    874  1.2  skrll 	case USB_SPEED_FULL:
    875  1.2  skrll 		if (is_isoc) {
    876  1.2  skrll 			retval =
    877  1.2  skrll 			    (8354 * (31 + 10 * BITSTUFFTIME(bytecount))) / 1000;
    878  1.2  skrll 			if (is_in)
    879  1.2  skrll 				retval = 7268 + FS_LS_HOST_DELAY + retval;
    880  1.2  skrll 			else
    881  1.2  skrll 				retval = 6265 + FS_LS_HOST_DELAY + retval;
    882  1.2  skrll 		} else {
    883  1.2  skrll 			retval =
    884  1.2  skrll 			    (8354 * (31 + 10 * BITSTUFFTIME(bytecount))) / 1000;
    885  1.2  skrll 			retval = 9107 + FS_LS_HOST_DELAY + retval;
    886  1.2  skrll 		}
    887  1.2  skrll 		break;
    888  1.2  skrll 	case USB_SPEED_LOW:
    889  1.2  skrll 		if (is_in) {
    890  1.2  skrll 			retval =
    891  1.2  skrll 			    (67667 * (31 + 10 * BITSTUFFTIME(bytecount))) /
    892  1.2  skrll 			    1000;
    893  1.2  skrll 			retval =
    894  1.2  skrll 			    64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
    895  1.2  skrll 			    retval;
    896  1.2  skrll 		} else {
    897  1.2  skrll 			retval =
    898  1.2  skrll 			    (66700 * (31 + 10 * BITSTUFFTIME(bytecount))) /
    899  1.2  skrll 			    1000;
    900  1.2  skrll 			retval =
    901  1.2  skrll 			    64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
    902  1.2  skrll 			    retval;
    903  1.2  skrll 		}
    904  1.2  skrll 		break;
    905  1.2  skrll 	default:
    906  1.2  skrll 		dev_warn(hsotg->dev, "Unknown device speed\n");
    907  1.2  skrll 		retval = -1;
    908  1.2  skrll 	}
    909  1.2  skrll 
    910  1.2  skrll 	return NS_TO_US(retval);
    911  1.2  skrll }
    912