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