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.
10 *****************************************************************************/
14 \ingroup IFXUSB_DRIVER_V3
15 \brief This file contains the functions to manage Queue Heads and Queue
18 #include <linux/version.h>
19 #include "ifxusb_version.h"
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/init.h>
25 #include <linux/device.h>
26 #include <linux/errno.h>
27 #include <linux/list.h>
28 #include <linux/interrupt.h>
29 #include <linux/string.h>
31 #include "ifxusb_plat.h"
32 #include "ifxusb_regs.h"
33 #include "ifxusb_cif.h"
36 #ifdef __EPQD_DESTROY_TIMEOUT__
37 #define epqh_self_destroy_timeout 5
38 static void eqph_destroy_func(unsigned long _ptr)
40 ifxhcd_epqh_t *epqh=(ifxhcd_epqh_t *)_ptr;
43 ifxhcd_epqh_free (epqh);
48 #define SCHEDULE_SLOP 10
51 \brief This function allocates and initializes a EPQH.
53 \param _ifxhcd The HCD state structure for the USB Host controller.
54 \param[in] _urb Holds the information about the device/endpoint that we need
55 to initialize the EPQH.
57 \return Returns pointer to the newly allocated EPQH, or NULL on error.
59 ifxhcd_epqh_t *ifxhcd_epqh_create (ifxhcd_hcd_t *_ifxhcd, struct urb *_urb)
64 struct usb_host_endpoint *sysep = ifxhcd_urb_to_endpoint(_urb);
67 // epqh=(ifxhcd_epqh_t *) kmalloc (sizeof(ifxhcd_epqh_t), GFP_KERNEL);
68 epqh=(ifxhcd_epqh_t *) kmalloc (sizeof(ifxhcd_epqh_t), GFP_ATOMIC);
73 memset (epqh, 0, sizeof (ifxhcd_epqh_t));
78 switch (usb_pipetype(_urb->pipe))
80 case PIPE_CONTROL : epqh->ep_type = IFXUSB_EP_TYPE_CTRL; break;
81 case PIPE_BULK : epqh->ep_type = IFXUSB_EP_TYPE_BULK; break;
82 case PIPE_ISOCHRONOUS: epqh->ep_type = IFXUSB_EP_TYPE_ISOC; break;
83 case PIPE_INTERRUPT : epqh->ep_type = IFXUSB_EP_TYPE_INTR; break;
86 //epqh->data_toggle = IFXUSB_HC_PID_DATA0;
88 epqh->mps = usb_maxpacket(_urb->dev, _urb->pipe, !(usb_pipein(_urb->pipe)));
90 hprt0.d32 = ifxusb_read_hprt0 (&_ifxhcd->core_if);
92 INIT_LIST_HEAD(&epqh->urbd_list);
93 INIT_LIST_HEAD(&epqh->epqh_list_entry);
96 epqh->dump_buf = ifxusb_alloc_buf(epqh->mps, 0);
98 /* FS/LS Enpoint on HS Hub
99 * NOT virtual root hub */
100 epqh->need_split = 0;
101 epqh->pkt_count_limit=0;
102 if(epqh->ep_type == IFXUSB_EP_TYPE_BULK && !(usb_pipein(_urb->pipe)) )
103 epqh->pkt_count_limit=4;
104 if (hprt0.b.prtspd == IFXUSB_HPRT0_PRTSPD_HIGH_SPEED &&
105 ((_urb->dev->speed == USB_SPEED_LOW) ||
106 (_urb->dev->speed == USB_SPEED_FULL)) &&
107 (_urb->dev->tt) && (_urb->dev->tt->hub->devnum != 1))
109 IFX_DEBUGPL(DBG_HCD, "QH init: EP %d: TT found at hub addr %d, for port %d\n",
110 usb_pipeendpoint(_urb->pipe), _urb->dev->tt->hub->devnum,
112 epqh->need_split = 1;
113 epqh->pkt_count_limit=1;
116 if (epqh->ep_type == IFXUSB_EP_TYPE_INTR ||
117 epqh->ep_type == IFXUSB_EP_TYPE_ISOC)
119 /* Compute scheduling parameters once and save them. */
120 epqh->interval = _urb->interval;
125 epqh->period_counter=0;
128 #ifdef __EPQD_DESTROY_TIMEOUT__
129 /* Start a timer for this transfer. */
130 init_timer(&epqh->destroy_timer);
131 epqh->destroy_timer.function = eqph_destroy_func;
132 epqh->destroy_timer.data = (unsigned long)(epqh);
136 IFX_DEBUGPL(DBG_HCD , "IFXUSB HCD EPQH Initialized\n");
137 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH - epqh = %p\n", epqh);
138 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH - Device Address = %d EP %d, %s\n",
140 usb_pipeendpoint(_urb->pipe),
141 usb_pipein(_urb->pipe) == USB_DIR_IN ? "IN" : "OUT");
142 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH - Speed = %s\n",
143 ({ char *speed; switch (_urb->dev->speed) {
144 case USB_SPEED_LOW: speed = "low" ; break;
145 case USB_SPEED_FULL: speed = "full"; break;
146 case USB_SPEED_HIGH: speed = "high"; break;
147 default: speed = "?"; break;
149 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH - Type = %s\n",
151 char *type; switch (epqh->ep_type)
153 case IFXUSB_EP_TYPE_ISOC: type = "isochronous"; break;
154 case IFXUSB_EP_TYPE_INTR: type = "interrupt" ; break;
155 case IFXUSB_EP_TYPE_CTRL: type = "control" ; break;
156 case IFXUSB_EP_TYPE_BULK: type = "bulk" ; break;
157 default: type = "?"; break;
161 if (epqh->ep_type == IFXUSB_EP_TYPE_INTR)
162 IFX_DEBUGPL(DBG_HCDV, "IFXUSB HCD EPQH - interval = %d\n", epqh->interval);
174 \brief Free the EPQH. EPQH should already be removed from a list.
175 URBD list should already be empty if called from URB Dequeue.
177 \param[in] _epqh The EPQH to free.
179 void ifxhcd_epqh_free (ifxhcd_epqh_t *_epqh)
183 if(_epqh->sysep) _epqh->sysep->hcpriv=NULL;
189 /* Free each QTD in the QTD list */
190 local_irq_save (flags);
191 if (!list_empty(&_epqh->urbd_list))
192 IFX_WARN("%s() invalid epqh state\n",__func__);
194 #if defined(__UNALIGNED_BUFFER_ADJ__)
195 if(_epqh->aligned_buf)
196 ifxusb_free_buf(_epqh->aligned_buf);
197 if(_epqh->aligned_setup)
198 ifxusb_free_buf(_epqh->aligned_setup);
201 if (!list_empty(&_epqh->epqh_list_entry))
202 list_del_init(&_epqh->epqh_list_entry);
204 #ifdef __EPQD_DESTROY_TIMEOUT__
205 del_timer(&_epqh->destroy_timer);
208 ifxusb_free_buf(_epqh->dump_buf);
213 local_irq_restore (flags);
217 \brief This function adds a EPQH to
219 \return 0 if successful, negative error code otherwise.
221 void ifxhcd_epqh_ready(ifxhcd_hcd_t *_ifxhcd, ifxhcd_epqh_t *_epqh)
224 local_irq_save(flags);
225 if (list_empty(&_epqh->epqh_list_entry))
228 if (_epqh->ep_type == IFXUSB_EP_TYPE_ISOC)
229 list_add_tail(&_epqh->epqh_list_entry, &_ifxhcd->epqh_isoc_ready);
232 if(_epqh->ep_type == IFXUSB_EP_TYPE_INTR)
233 list_add_tail(&_epqh->epqh_list_entry, &_ifxhcd->epqh_intr_ready);
235 list_add_tail(&_epqh->epqh_list_entry, &_ifxhcd->epqh_np_ready);
238 else if(!_epqh->is_active)
241 if (_epqh->ep_type == IFXUSB_EP_TYPE_ISOC)
242 list_move_tail(&_epqh->epqh_list_entry, &_ifxhcd->epqh_isoc_ready);
245 if(_epqh->ep_type == IFXUSB_EP_TYPE_INTR)
246 list_move_tail(&_epqh->epqh_list_entry, &_ifxhcd->epqh_intr_ready);
248 list_move_tail(&_epqh->epqh_list_entry, &_ifxhcd->epqh_np_ready);
250 #ifdef __EPQD_DESTROY_TIMEOUT__
251 del_timer(&_epqh->destroy_timer);
253 local_irq_restore(flags);
256 void ifxhcd_epqh_active(ifxhcd_hcd_t *_ifxhcd, ifxhcd_epqh_t *_epqh)
259 local_irq_save(flags);
260 if (list_empty(&_epqh->epqh_list_entry))
261 IFX_WARN("%s() invalid epqh state\n",__func__);
263 if (_epqh->ep_type == IFXUSB_EP_TYPE_ISOC)
264 list_move_tail(&_epqh->epqh_list_entry, &_ifxhcd->epqh_isoc_active);
267 if(_epqh->ep_type == IFXUSB_EP_TYPE_INTR)
268 list_move_tail(&_epqh->epqh_list_entry, &_ifxhcd->epqh_intr_active);
270 list_move_tail(&_epqh->epqh_list_entry, &_ifxhcd->epqh_np_active);
272 #ifdef __EPQD_DESTROY_TIMEOUT__
273 del_timer(&_epqh->destroy_timer);
275 local_irq_restore(flags);
278 void ifxhcd_epqh_idle(ifxhcd_hcd_t *_ifxhcd, ifxhcd_epqh_t *_epqh)
281 local_irq_save(flags);
283 if (list_empty(&_epqh->urbd_list))
285 if(_epqh->ep_type == IFXUSB_EP_TYPE_ISOC || _epqh->ep_type == IFXUSB_EP_TYPE_INTR)
287 list_move_tail(&_epqh->epqh_list_entry, &_ifxhcd->epqh_stdby);
291 list_del_init(&_epqh->epqh_list_entry);
292 #ifdef __EPQD_DESTROY_TIMEOUT__
293 del_timer(&_epqh->destroy_timer);
294 _epqh->destroy_timer.expires = jiffies + (HZ*epqh_self_destroy_timeout);
295 add_timer(&_epqh->destroy_timer );
302 if (_epqh->ep_type == IFXUSB_EP_TYPE_ISOC)
303 list_move_tail(&_epqh->epqh_list_entry, &_ifxhcd->epqh_isoc_ready);
306 if(_epqh->ep_type == IFXUSB_EP_TYPE_INTR)
307 list_move_tail(&_epqh->epqh_list_entry, &_ifxhcd->epqh_intr_ready);
309 list_move_tail(&_epqh->epqh_list_entry, &_ifxhcd->epqh_np_ready);
312 local_irq_restore(flags);
316 void ifxhcd_epqh_idle_periodic(ifxhcd_epqh_t *_epqh)
319 if(_epqh->ep_type != IFXUSB_EP_TYPE_ISOC && _epqh->ep_type != IFXUSB_EP_TYPE_INTR)
322 local_irq_save(flags);
324 if (list_empty(&_epqh->epqh_list_entry))
325 IFX_WARN("%s() invalid epqh state\n",__func__);
326 if (!list_empty(&_epqh->urbd_list))
327 IFX_WARN("%s() invalid epqh state(not empty)\n",__func__);
330 list_del_init(&_epqh->epqh_list_entry);
331 #ifdef __EPQD_DESTROY_TIMEOUT__
332 del_timer(&_epqh->destroy_timer);
333 _epqh->destroy_timer.expires = jiffies + (HZ*epqh_self_destroy_timeout);
334 add_timer(&_epqh->destroy_timer );
337 local_irq_restore(flags);
341 int ifxhcd_urbd_create (ifxhcd_hcd_t *_ifxhcd,struct urb *_urb)
344 struct usb_host_endpoint *sysep;
347 /* == AVM/WK 20100714 retval correctly initialized ==*/
348 int retval = -ENOMEM;
350 /*== AVM/BC 20100630 - Spinlock ==*/
351 //local_irq_save(flags);
352 SPIN_LOCK_IRQSAVE(&_ifxhcd->lock, flags);
354 // urbd = (ifxhcd_urbd_t *) kmalloc (sizeof(ifxhcd_urbd_t), GFP_KERNEL);
355 urbd = (ifxhcd_urbd_t *) kmalloc (sizeof(ifxhcd_urbd_t), GFP_ATOMIC);
356 if (urbd != NULL) /* Initializes a QTD structure.*/
359 memset (urbd, 0, sizeof (ifxhcd_urbd_t));
361 sysep = ifxhcd_urb_to_endpoint(_urb);
362 epqh = (ifxhcd_epqh_t *)sysep->hcpriv;
365 epqh = ifxhcd_epqh_create (_ifxhcd, _urb);
370 //local_irq_restore (flags);
371 SPIN_UNLOCK_IRQRESTORE(&_ifxhcd->lock, flags);
374 sysep->hcpriv = epqh;
377 INIT_LIST_HEAD(&urbd->urbd_list_entry);
379 /*== AVM/BC 20100630 - 2.6.28 needs HCD link/unlink URBs ==*/
380 retval = usb_hcd_link_urb_to_ep(ifxhcd_to_syshcd(_ifxhcd), _urb);
382 if (unlikely(retval)){
385 SPIN_UNLOCK_IRQRESTORE(&_ifxhcd->lock, flags);
389 list_add_tail(&urbd->urbd_list_entry, &epqh->urbd_list);
394 urbd->is_in=usb_pipein(_urb->pipe) ? 1 : 0;;
396 urbd->xfer_len=_urb->transfer_buffer_length;
397 #define URB_NO_SETUP_DMA_MAP 0
401 if(_urb->transfer_flags && URB_NO_TRANSFER_DMA_MAP)
402 urbd->xfer_buff = (uint8_t *) (KSEG1ADDR((uint32_t *)_urb->transfer_dma));
404 urbd->xfer_buff = (uint8_t *) _urb->transfer_buffer;
406 if(epqh->ep_type == IFXUSB_EP_TYPE_CTRL)
408 if(_urb->transfer_flags && URB_NO_SETUP_DMA_MAP)
409 urbd->setup_buff = (uint8_t *) (KSEG1ADDR((uint32_t *)_urb->setup_dma));
411 urbd->setup_buff = (uint8_t *) _urb->setup_packet;
414 //local_irq_restore (flags);
415 SPIN_UNLOCK_IRQRESTORE(&_ifxhcd->lock, flags);