1 /*****************************************************************************
2 ** FILE NAME : ifxhcd_queue.c
3 ** PROJECT : IFX USB sub-system V3
4 ** MODULES : IFX USB sub-system Host and Device driver
7 ** AUTHOR : Chen, Howard
8 ** DESCRIPTION : This file contains the functions to manage Queue Heads and Queue
9 ** Transfer Descriptors.
12 ** REFERENCE : Synopsys DWC-OTG Driver 2.7
13 ** COPYRIGHT : Copyright (c) 2010
14 ** LANTIQ DEUTSCHLAND GMBH,
15 ** Am Campeon 3, 85579 Neubiberg, Germany
17 ** This program is free software; you can redistribute it and/or modify
18 ** it under the terms of the GNU General Public License as published by
19 ** the Free Software Foundation; either version 2 of the License, or
20 ** (at your option) any later version.
22 ** Version Control Section **
26 ** $Log$ Revision history
27 *****************************************************************************/
30 * This file contains code fragments from Synopsys HS OTG Linux Software Driver.
31 * For this code the following notice is applicable:
33 * ==========================================================================
35 * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
36 * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
37 * otherwise expressly agreed to in writing between Synopsys and you.
39 * The Software IS NOT an item of Licensed Software or Licensed Product under
40 * any End User Software License Agreement or Agreement for Licensed Product
41 * with Synopsys or any supplement thereto. You are permitted to use and
42 * redistribute this Software in source and binary forms, with or without
43 * modification, provided that redistributions of source code must retain this
44 * notice. You may not view, use, disclose, copy or distribute this file or
45 * any information contained herein except pursuant to this license grant from
46 * Synopsys. If you do not agree with this notice, including the disclaimer
47 * below, then you are not authorized to use the Software.
49 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
50 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
53 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
60 * ========================================================================== */
64 \ingroup IFXUSB_DRIVER_V3
65 \brief This file contains the functions to manage Queue Heads and Queue
68 #include <linux/version.h>
69 #include "ifxusb_version.h"
71 #include <linux/kernel.h>
72 #include <linux/module.h>
73 #include <linux/moduleparam.h>
74 #include <linux/init.h>
75 #include <linux/device.h>
76 #include <linux/errno.h>
77 #include <linux/list.h>
78 #include <linux/interrupt.h>
79 #include <linux/string.h>
81 #include "ifxusb_plat.h"
82 #include "ifxusb_regs.h"
83 #include "ifxusb_cif.h"
86 #ifdef __EPQD_DESTROY_TIMEOUT__
87 #define epqh_self_destroy_timeout 300
88 static void eqph_destroy_func(unsigned long _ptr)
90 ifxhcd_epqh_t *epqh=(ifxhcd_epqh_t *)_ptr;
95 epqh->sysep->hcpriv=NULL;
97 ifxhcd_epqh_free (epqh);
103 \brief This function allocates and initializes a EPQH.
105 \param _ifxhcd The HCD state structure for the USB Host controller.
106 \param[in] _urb Holds the information about the device/endpoint that we need
107 to initialize the EPQH.
109 \return Returns pointer to the newly allocated EPQH, or NULL on error.
111 static ifxhcd_epqh_t *ifxhcd_epqh_create (ifxhcd_hcd_t *_ifxhcd, struct urb *_urb)
116 struct usb_host_endpoint *sysep = ifxhcd_urb_to_endpoint(_urb);
118 /* Allocate memory */
119 // epqh=(ifxhcd_epqh_t *) kmalloc (sizeof(ifxhcd_epqh_t), GFP_KERNEL);
120 epqh=(ifxhcd_epqh_t *) kmalloc (sizeof(ifxhcd_epqh_t), GFP_ATOMIC);
125 memset (epqh, 0, sizeof (ifxhcd_epqh_t));
129 epqh->devno=_urb->dev->devnum;
131 epqh->ifxhcd=_ifxhcd;
132 epqh->phase=EPQH_IDLE;
134 /* Initialize EPQH */
135 switch (usb_pipetype(_urb->pipe))
137 case PIPE_CONTROL : epqh->ep_type = IFXUSB_EP_TYPE_CTRL; break;
138 case PIPE_BULK : epqh->ep_type = IFXUSB_EP_TYPE_BULK; break;
139 case PIPE_ISOCHRONOUS: epqh->ep_type = IFXUSB_EP_TYPE_ISOC; break;
140 case PIPE_INTERRUPT : epqh->ep_type = IFXUSB_EP_TYPE_INTR; break;
143 usb_settoggle(_urb->dev, usb_pipeendpoint (_urb->pipe), !usb_pipein(_urb->pipe), IFXUSB_HC_PID_DATA0);
144 epqh->mps = usb_maxpacket(_urb->dev, _urb->pipe, !(usb_pipein(_urb->pipe)));
146 INIT_LIST_HEAD(&epqh->urbd_list);
147 #ifdef __STRICT_ORDER__
148 INIT_LIST_HEAD(&epqh->release_list);
150 INIT_LIST_HEAD(&epqh->ql);
151 INIT_LIST_HEAD(&epqh->ql_all);
152 INIT_URBD_LIST(epqh);
156 /* FS/LS Enpoint on HS Hub
157 * NOT virtual root hub */
158 epqh->need_split = 0;
159 hprt0.d32 = ifxusb_read_hprt0 (&_ifxhcd->core_if);
160 if (hprt0.b.prtspd == IFXUSB_HPRT0_PRTSPD_HIGH_SPEED &&
161 ((_urb->dev->speed == USB_SPEED_LOW) ||
162 (_urb->dev->speed == USB_SPEED_FULL)) &&
163 (_urb->dev->tt) && (_urb->dev->tt->hub) && (_urb->dev->tt->hub->devnum != 1))
165 IFX_DEBUGPL(DBG_HCD, "QH init: EP %d: TT found at hub addr %d, for port %d\n",
166 usb_pipeendpoint(_urb->pipe), _urb->dev->tt->hub->devnum,
168 epqh->need_split = 1;
171 if (epqh->ep_type == IFXUSB_EP_TYPE_INTR ||
172 epqh->ep_type == IFXUSB_EP_TYPE_ISOC)
174 /* Compute scheduling parameters once and save them. */
175 epqh->interval = _urb->interval;
181 if (epqh->ep_type == IFXUSB_EP_TYPE_ISOC)
182 _ifxhcd->isoc_ep_count++;
185 epqh->period_counter=0;
187 #ifdef __EPQD_DESTROY_TIMEOUT__
188 /* Start a timer for this transfer. */
189 init_timer(&epqh->destroy_timer);
190 epqh->destroy_timer.function = eqph_destroy_func;
191 epqh->destroy_timer.data = (unsigned long)(epqh);
195 IFX_DEBUGPL(DBG_HCD , "IFXUSB HCD EPQH Initialized\n");
196 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH - epqh = %p\n", epqh);
197 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH - Device Address = %d EP %d, %s\n",
199 usb_pipeendpoint(_urb->pipe),
200 usb_pipein(_urb->pipe) == USB_DIR_IN ? "IN" : "OUT");
201 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH - Speed = %s\n",
202 ({ char *speed; switch (_urb->dev->speed) {
203 case USB_SPEED_LOW: speed = "low" ; break;
204 case USB_SPEED_FULL: speed = "full"; break;
205 case USB_SPEED_HIGH: speed = "high"; break;
206 default: speed = "?"; break;
208 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH - Type = %s\n",
210 char *type; switch (epqh->ep_type)
212 case IFXUSB_EP_TYPE_ISOC: type = "isochronous"; break;
213 case IFXUSB_EP_TYPE_INTR: type = "interrupt" ; break;
214 case IFXUSB_EP_TYPE_CTRL: type = "control" ; break;
215 case IFXUSB_EP_TYPE_BULK: type = "bulk" ; break;
216 default: type = "?"; break;
220 if (epqh->ep_type == IFXUSB_EP_TYPE_INTR)
221 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH - interval = %d\n", epqh->interval);
224 LOCK_EPQH_LIST_ALL(_ifxhcd);
225 list_add_tail(&epqh->ql_all, &_ifxhcd->epqh_list_all);
226 UNLOCK_EPQH_LIST_ALL(_ifxhcd);
228 LOCK_EPQH_LIST(_ifxhcd);
229 switch (epqh->ep_type)
231 case IFXUSB_EP_TYPE_CTRL:
232 case IFXUSB_EP_TYPE_BULK:
234 list_add_tail(&epqh->ql, &_ifxhcd->epqh_list_np);
236 case IFXUSB_EP_TYPE_INTR:
237 list_add_tail(&epqh->ql, &_ifxhcd->epqh_list_intr);
240 case IFXUSB_EP_TYPE_ISOC:
241 list_add_tail(&epqh->ql, &_ifxhcd->epqh_list_isoc);
246 UNLOCK_EPQH_LIST(_ifxhcd);
256 \brief Free the EPQH. EPQH should already be removed from a list.
257 URBD list should already be empty if called from URB Dequeue.
259 \param[in] _epqh The EPQH to free.
261 void ifxhcd_epqh_free (ifxhcd_epqh_t *_epqh)
267 if(_epqh->sysep) _epqh->sysep->hcpriv=NULL;
270 local_irq_save (flags);
271 if (!list_empty(&_epqh->urbd_list))
272 IFX_WARN("%s() invalid epqh state\n",__func__);
275 LOCK_EPQH_LIST_ALL(_epqh->ifxhcd);
276 if (!list_empty(&_epqh->ql_all))
277 list_del_init (&_epqh->ql_all);
278 UNLOCK_EPQH_LIST_ALL(_epqh->ifxhcd);
280 LOCK_EPQH_LIST(_epqh->ifxhcd);
281 if (!list_empty(&_epqh->ql))
282 list_del_init (&_epqh->ql);
283 UNLOCK_EPQH_LIST(_epqh->ifxhcd);
285 #ifdef __EPQD_DESTROY_TIMEOUT__
286 del_timer(&_epqh->destroy_timer);
290 local_irq_restore (flags);
294 void ifxhcd_epqh_idle(ifxhcd_epqh_t *_epqh)
297 local_irq_save(flags);
298 LOCK_URBD_LIST(_epqh);
299 if (list_empty(&_epqh->urbd_list))
301 if(_epqh->ep_type == IFXUSB_EP_TYPE_ISOC || _epqh->ep_type == IFXUSB_EP_TYPE_INTR)
302 _epqh->phase=EPQH_STDBY;
305 _epqh->phase=EPQH_IDLE;
306 #ifdef __EPQD_DESTROY_TIMEOUT__
307 del_timer(&_epqh->destroy_timer);
308 _epqh->destroy_timer.expires = jiffies + (HZ*epqh_self_destroy_timeout);
309 add_timer(&_epqh->destroy_timer );
315 _epqh->phase=EPQH_READY;
316 #ifdef __EPQD_DESTROY_TIMEOUT__
317 del_timer(&_epqh->destroy_timer);
320 UNLOCK_URBD_LIST(_epqh);
321 local_irq_restore(flags);
325 void ifxhcd_epqh_idle_periodic(ifxhcd_epqh_t *_epqh)
328 if(_epqh->ep_type != IFXUSB_EP_TYPE_ISOC && _epqh->ep_type != IFXUSB_EP_TYPE_INTR && _epqh->phase!=EPQH_STDBY)
331 local_irq_save(flags);
332 LOCK_URBD_LIST(_epqh);
333 if (!list_empty(&_epqh->urbd_list))
334 IFX_WARN("%s() invalid epqh state(not empty)\n",__func__);
336 _epqh->phase=EPQH_IDLE;
338 #ifdef __EPQD_DESTROY_TIMEOUT__
339 del_timer(&_epqh->destroy_timer);
340 _epqh->destroy_timer.expires = jiffies + (HZ*epqh_self_destroy_timeout);
341 add_timer(&_epqh->destroy_timer );
345 if (_epqh->ep_type == IFXUSB_EP_TYPE_ISOC)
346 _epqh->ifxhcd->isoc_ep_count--;
348 UNLOCK_URBD_LIST(_epqh);
349 local_irq_restore(flags);
353 ifxhcd_epqh_t *ifxhcd_urbd_create (ifxhcd_hcd_t *_ifxhcd,struct urb *_urb)
356 struct usb_host_endpoint *sysep;
357 ifxhcd_epqh_t *epqh=NULL;
360 local_irq_save(flags);
362 sysep = ifxhcd_urb_to_endpoint(_urb);
364 LOCK_EPQH_LIST_ALL(_ifxhcd);
365 epqh = sysep_to_epqh(_ifxhcd, sysep);
369 sysep->hcpriv = NULL;
370 epqh = ifxhcd_epqh_create (_ifxhcd, _urb);
372 UNLOCK_EPQH_LIST_ALL(_ifxhcd);
376 IFX_ERROR("EPQH Error alloc\n");
377 local_irq_restore (flags);
378 return (ifxhcd_epqh_t *)NULL;
380 if(epqh->phase==EPQH_DISABLING)
382 IFX_ERROR("EPQH Error alloc while disabling\n");
383 local_irq_restore (flags);
384 return (ifxhcd_epqh_t *)NULL;
386 sysep->hcpriv = epqh;
390 IFX_WARN("%s() Previous urb->hcpriv exist %p\n",__func__,_urb->hcpriv);
392 local_irq_restore (flags);
393 return (ifxhcd_epqh_t *)NULL;
397 IFX_WARN("%s() Previous urb->hcpriv exist %p and epqh not the same %p %p\n",__func__,_urb->hcpriv,urbd->epqh,epqh);
402 urbd = (ifxhcd_urbd_t *) kmalloc (sizeof(ifxhcd_urbd_t), GFP_ATOMIC);
405 local_irq_restore (flags);
406 return (ifxhcd_epqh_t *)NULL;
408 memset (urbd, 0, sizeof (ifxhcd_urbd_t));
409 INIT_LIST_HEAD(&urbd->ql);
415 urbd->status= -EINPROGRESS;
417 urbd->is_in=usb_pipein(_urb->pipe) ? 1 : 0;
418 #define URB_NO_SETUP_DMA_MAP 0
420 if(epqh->ep_type == IFXUSB_EP_TYPE_ISOC)
422 if(_urb->transfer_flags && URB_NO_TRANSFER_DMA_MAP)
423 urbd->xfer_buff = (uint8_t *) (KSEG1ADDR((uint32_t *)_urb->transfer_dma));
425 urbd->xfer_buff = (uint8_t *) _urb->transfer_buffer;
430 urbd->xfer_len=_urb->transfer_buffer_length;
433 if(_urb->transfer_flags && URB_NO_TRANSFER_DMA_MAP)
434 urbd->xfer_buff = (uint8_t *) (KSEG1ADDR((uint32_t *)_urb->transfer_dma));
436 urbd->xfer_buff = (uint8_t *) _urb->transfer_buffer;
440 #if 1 // cache write-back, so DMA engine can get correct content. Precaution
442 dma_cache_wback_inv((unsigned long)urbd->xfer_buff, urbd->xfer_len);
445 if(epqh->ep_type == IFXUSB_EP_TYPE_CTRL)
447 if(_urb->transfer_flags && URB_NO_SETUP_DMA_MAP)
448 urbd->setup_buff = (uint8_t *) (KSEG1ADDR((uint32_t *)_urb->setup_dma));
450 urbd->setup_buff = (uint8_t *) _urb->setup_packet;
451 #if 1 // cache write-back, so DMA engine can get correct content. Precaution
452 dma_cache_wback_inv((unsigned long)urbd->setup_buff, 16);
456 LOCK_URBD_LIST(epqh);
457 if (!list_empty(&urbd->ql))
458 list_del_init(&urbd->ql);
459 list_add_tail(&urbd->ql, &epqh->urbd_list);
461 UNLOCK_URBD_LIST(epqh);
463 local_irq_restore (flags);
469 ifxhcd_epqh_t * sysep_to_epqh(ifxhcd_hcd_t *_ifxhcd, struct usb_host_endpoint *_sysep)
473 LOCK_EPQH_LIST_ALL(_ifxhcd);
474 list_for_each_entry( epqh, &_ifxhcd->epqh_list_all, ql_all)
476 if(epqh->sysep==_sysep)
478 UNLOCK_EPQH_LIST_ALL(_ifxhcd);
482 UNLOCK_EPQH_LIST_ALL(_ifxhcd);