USB: support usb otg and host20 functions
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc_otg / dwc_otg_hcd.h
1 /* ==========================================================================
2  * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_hcd.h $
3  * $Revision: #6 $
4  * $Date: 2006/12/05 $
5  * $Change: 762293 $
6  *
7  * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
8  * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
9  * otherwise expressly agreed to in writing between Synopsys and you.
10  * 
11  * The Software IS NOT an item of Licensed Software or Licensed Product under
12  * any End User Software License Agreement or Agreement for Licensed Product
13  * with Synopsys or any supplement thereto. You are permitted to use and
14  * redistribute this Software in source and binary forms, with or without
15  * modification, provided that redistributions of source code must retain this
16  * notice. You may not view, use, disclose, copy or distribute this file or
17  * any information contained herein except pursuant to this license grant from
18  * Synopsys. If you do not agree with this notice, including the disclaimer
19  * below, then you are not authorized to use the Software.
20  * 
21  * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31  * DAMAGE.
32  * ========================================================================== */
33 #ifndef DWC_DEVICE_ONLY
34 #if !defined(__DWC_HCD_H__)
35 #define __DWC_HCD_H__
36
37 #include <linux/list.h>
38 #include <linux/usb.h>
39 #include <linux/version.h>
40 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
41 #include <linux/usb/hcd.h>
42 #else
43 #include <../drivers/usb/core/hcd.h>
44 #endif
45
46 struct dwc_otg_device;
47
48 #include "dwc_otg_cil.h"
49
50 /**
51  * @file
52  *
53  * This file contains the structures, constants, and interfaces for
54  * the Host Contoller Driver (HCD).
55  *
56  * The Host Controller Driver (HCD) is responsible for translating requests
57  * from the USB Driver into the appropriate actions on the DWC_otg controller.
58  * It isolates the USBD from the specifics of the controller by providing an
59  * API to the USBD.
60  */
61
62 /**
63  * Phases for control transfers.
64  */
65 typedef enum dwc_otg_control_phase {
66         DWC_OTG_CONTROL_SETUP,
67         DWC_OTG_CONTROL_DATA,
68         DWC_OTG_CONTROL_STATUS
69 } dwc_otg_control_phase_e;
70
71 /** Transaction types. */
72 typedef enum dwc_otg_transaction_type {
73         DWC_OTG_TRANSACTION_NONE,
74         DWC_OTG_TRANSACTION_PERIODIC,
75         DWC_OTG_TRANSACTION_NON_PERIODIC,
76         DWC_OTG_TRANSACTION_ALL
77 } dwc_otg_transaction_type_e;
78
79 /** Transaction types. */
80 typedef enum qh_status_type {
81         QH_INACTIVE,
82         QH_ACTIVE,
83         QH_READY,
84         QH_ASSIGNED,
85         QH_QUEUED
86 } qh_status_type_e;
87
88 /**
89  * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
90  * interrupt, or isochronous transfer. A single QTD is created for each URB
91  * (of one of these types) submitted to the HCD. The transfer associated with
92  * a QTD may require one or multiple transactions.
93  *
94  * A QTD is linked to a Queue Head, which is entered in either the
95  * non-periodic or periodic schedule for execution. When a QTD is chosen for
96  * execution, some or all of its transactions may be executed. After
97  * execution, the state of the QTD is updated. The QTD may be retired if all
98  * its transactions are complete or if an error occurred. Otherwise, it
99  * remains in the schedule so more transactions can be executed later.
100  */
101 typedef struct dwc_otg_qtd {
102         /**
103          * Determines the PID of the next data packet for the data phase of
104          * control transfers. Ignored for other transfer types.<br>
105          * One of the following values:
106          *      - DWC_OTG_HC_PID_DATA0
107          *      - DWC_OTG_HC_PID_DATA1
108          */
109         uint8_t                 data_toggle;
110
111         /** Current phase for control transfers (Setup, Data, or Status). */
112         dwc_otg_control_phase_e control_phase;
113
114         /** Keep track of the current split type
115          * for FS/LS endpoints on a HS Hub */
116         uint8_t                 complete_split;
117
118         /** How many bytes transferred during SSPLIT OUT */
119         uint32_t                ssplit_out_xfer_count;
120
121         /**
122          * Holds the number of bus errors that have occurred for a transaction
123          * within this transfer.
124          */
125         uint8_t                 error_count;
126
127         /**
128          * Index of the next frame descriptor for an isochronous transfer. A
129          * frame descriptor describes the buffer position and length of the
130          * data to be transferred in the next scheduled (micro)frame of an
131          * isochronous transfer. It also holds status for that transaction.
132          * The frame index starts at 0.
133          */
134         int                     isoc_frame_index;
135
136         /** Position of the ISOC split on full/low speed */
137         uint8_t                 isoc_split_pos;
138
139         /** Position of the ISOC split in the buffer for the current frame */
140         uint16_t                isoc_split_offset;
141
142         /** URB for this transfer */
143         struct urb              *urb;
144
145         /** This list of QTDs */
146         struct list_head        qtd_list_entry;
147
148 } dwc_otg_qtd_t;
149
150 /**
151  * A Queue Head (QH) holds the static characteristics of an endpoint and
152  * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
153  * be entered in either the non-periodic or periodic schedule.
154  */
155 typedef struct dwc_otg_qh {
156         /**
157          * Endpoint type.
158          * One of the following values:
159          *      - USB_ENDPOINT_XFER_CONTROL
160          *      - USB_ENDPOINT_XFER_ISOC
161          *      - USB_ENDPOINT_XFER_BULK
162          *      - USB_ENDPOINT_XFER_INT
163          */
164         uint8_t                 ep_type;
165         uint8_t                 ep_is_in;
166
167         /** wMaxPacketSize Field of Endpoint Descriptor. */
168         uint16_t                maxp;
169
170         /**
171          * Determines the PID of the next data packet for non-control
172          * transfers. Ignored for control transfers.<br>
173          * One of the following values:
174          *      - DWC_OTG_HC_PID_DATA0
175          *      - DWC_OTG_HC_PID_DATA1
176          */
177         uint8_t                 data_toggle;
178
179         /** Ping state if 1. */
180         uint8_t                 ping_state;
181         
182         /**
183          * 
184         ****/
185     uint16_t        qh_state;
186         /**
187          * List of QTDs for this QH.
188          */
189         struct list_head        qtd_list;
190
191         /** Host channel currently processing transfers for this QH. */
192         dwc_hc_t                *channel;
193
194         /** QTD currently assigned to a host channel for this QH. */
195         dwc_otg_qtd_t           *qtd_in_process;
196
197         /** Full/low speed endpoint on high-speed hub requires split. */
198         uint8_t                 do_split;
199
200         /** @name Periodic schedule information */
201         /** @{ */
202
203         /** Bandwidth in microseconds per (micro)frame. */
204         uint8_t                 usecs;
205
206         /** Interval between transfers in (micro)frames. */
207         uint16_t                interval;
208
209         /**
210          * (micro)frame to initialize a periodic transfer. The transfer
211          * executes in the following (micro)frame.
212          */
213         uint16_t                sched_frame;
214
215         /** (micro)frame at which last start split was initialized. */
216         uint16_t                start_split_frame;
217
218         /** @} */
219
220         /** Entry for QH in either the periodic or non-periodic schedule. */
221         struct list_head        qh_list_entry;
222 } dwc_otg_qh_t;
223
224 /**
225  * This structure holds the state of the HCD, including the non-periodic and
226  * periodic schedules.
227  */
228 typedef struct dwc_otg_hcd {
229
230         spinlock_t              lock;
231
232         /** DWC OTG Core Interface Layer */
233         dwc_otg_core_if_t       *core_if;
234
235         /** Internal DWC HCD Flags */   
236         volatile union dwc_otg_hcd_internal_flags {
237                 uint32_t d32;
238                 struct {
239                         unsigned port_connect_status_change : 1;
240                         unsigned port_connect_status : 1;
241                         unsigned port_reset_change : 1;
242                         unsigned port_enable_change : 1;
243                         unsigned port_suspend_change : 1;
244                         unsigned port_over_current_change : 1;
245                         unsigned reserved : 27;
246                 } b;
247         } flags;
248
249         /**
250          * Inactive items in the non-periodic schedule. This is a list of
251          * Queue Heads. Transfers associated with these Queue Heads are not
252          * currently assigned to a host channel.
253          */
254         struct list_head        non_periodic_sched_inactive;
255
256         /**
257          * Active items in the non-periodic schedule. This is a list of
258          * Queue Heads. Transfers associated with these Queue Heads are
259          * currently assigned to a host channel.
260          */
261         struct list_head        non_periodic_sched_active;
262
263         /**
264          * Pointer to the next Queue Head to process in the active
265          * non-periodic schedule.
266          */
267         struct list_head        *non_periodic_qh_ptr;
268
269         /**
270          * Inactive items in the periodic schedule. This is a list of QHs for
271          * periodic transfers that are _not_ scheduled for the next frame.
272          * Each QH in the list has an interval counter that determines when it
273          * needs to be scheduled for execution. This scheduling mechanism
274          * allows only a simple calculation for periodic bandwidth used (i.e.
275          * must assume that all periodic transfers may need to execute in the
276          * same frame). However, it greatly simplifies scheduling and should
277          * be sufficient for the vast majority of OTG hosts, which need to
278          * connect to a small number of peripherals at one time.
279          *
280          * Items move from this list to periodic_sched_ready when the QH
281          * interval counter is 0 at SOF.
282          */
283         struct list_head        periodic_sched_inactive;
284
285         /**
286          * List of periodic QHs that are ready for execution in the next
287          * frame, but have not yet been assigned to host channels.
288          *
289          * Items move from this list to periodic_sched_assigned as host
290          * channels become available during the current frame.
291          */
292         //struct list_head      periodic_sched_ready;
293
294         /**
295          * List of periodic QHs to be executed in the next frame that are
296          * assigned to host channels.
297          *
298          * Items move from this list to periodic_sched_queued as the
299          * transactions for the QH are queued to the DWC_otg controller.
300          */
301         //struct list_head      periodic_sched_assigned;
302
303         /**
304          * List of periodic QHs that have been queued for execution.
305          *
306          * Items move from this list to either periodic_sched_inactive or
307          * periodic_sched_ready when the channel associated with the transfer
308          * is released. If the interval for the QH is 1, the item moves to
309          * periodic_sched_ready because it must be rescheduled for the next
310          * frame. Otherwise, the item moves to periodic_sched_inactive.
311          */
312         //struct list_head      periodic_sched_queued;
313
314         /**
315          * Total bandwidth claimed so far for periodic transfers. This value
316          * is in microseconds per (micro)frame. The assumption is that all
317          * periodic transfers may occur in the same (micro)frame.
318          */
319         uint16_t                periodic_usecs;
320
321         /**
322          * Frame number read from the core at SOF. The value ranges from 0 to
323          * DWC_HFNUM_MAX_FRNUM.
324          */
325         uint16_t                frame_number;
326
327         /**
328          * Free host channels in the controller. This is a list of
329          * dwc_hc_t items.
330          */
331         struct list_head        free_hc_list;
332
333         /**
334          * Number of host channels assigned to periodic transfers. Currently
335          * assuming that there is a dedicated host channel for each periodic
336          * transaction and at least one host channel available for
337          * non-periodic transactions.
338          */
339         int                     periodic_channels;
340
341         /**
342          * Number of host channels assigned to non-periodic transfers.
343          */
344         int                     non_periodic_channels;
345
346         /**
347          * Array of pointers to the host channel descriptors. Allows accessing
348          * a host channel descriptor given the host channel number. This is
349          * useful in interrupt handlers.
350          */
351         dwc_hc_t                *hc_ptr_array[MAX_EPS_CHANNELS];
352
353         /**
354          * Buffer to use for any data received during the status phase of a
355          * control transfer. Normally no data is transferred during the status
356          * phase. This buffer is used as a bit bucket. 
357          */
358         uint8_t                 *status_buf;
359
360         /**
361          * DMA address for status_buf.
362          */
363         dma_addr_t              status_buf_dma;
364 #define DWC_OTG_HCD_STATUS_BUF_SIZE 64  
365
366         /**
367          * Structure to allow starting the HCD in a non-interrupt context
368          * during an OTG role change.
369          */
370         struct work_struct      start_work;
371
372         /**
373          * Connection timer. An OTG host must display a message if the device
374          * does not connect. Started when the VBus power is turned on via
375          * sysfs attribute "buspower".
376          */
377         struct timer_list       conn_timer;
378
379         /* Tasket to do a reset */
380         struct tasklet_struct   *reset_tasklet;
381
382     struct timer_list   connect_detect_timer;
383     struct delayed_work host_enable_work;
384     
385         spinlock_t global_lock;
386
387 #ifdef DEBUG
388         uint32_t                frrem_samples;
389         uint64_t                frrem_accum;
390
391         uint32_t                hfnum_7_samples_a;
392         uint64_t                hfnum_7_frrem_accum_a;
393         uint32_t                hfnum_0_samples_a;
394         uint64_t                hfnum_0_frrem_accum_a;
395         uint32_t                hfnum_other_samples_a;
396         uint64_t                hfnum_other_frrem_accum_a;
397
398         uint32_t                hfnum_7_samples_b;
399         uint64_t                hfnum_7_frrem_accum_b;
400         uint32_t                hfnum_0_samples_b;
401         uint64_t                hfnum_0_frrem_accum_b;
402         uint32_t                hfnum_other_samples_b;
403         uint64_t                hfnum_other_frrem_accum_b;
404 #endif  
405
406     /** Flag to indicate whether host controller is enabled. 
407      *  0: force disable by sysfs
408      *  1: enable
409      *  2: not enable
410      **/
411     uint8_t host_enabled;
412     uint8_t host_setenable;
413
414 } dwc_otg_hcd_t;
415
416 /** Gets the dwc_otg_hcd from a struct usb_hcd */
417 static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
418 {
419         return (dwc_otg_hcd_t *)(hcd->hcd_priv);
420 }
421
422 /** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
423 static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t *dwc_otg_hcd)
424 {
425         return container_of((void *)dwc_otg_hcd, struct usb_hcd, hcd_priv);
426 }
427
428 /** @name HCD Create/Destroy Functions */
429 /** @{ */
430 extern int dwc_otg_hcd_init(struct device *_dev);
431 extern void dwc_otg_hcd_remove(struct device *_dev);
432 /** @} */
433
434 /** @name Linux HC Driver API Functions */
435 /** @{ */
436
437 extern int dwc_otg_hcd_start(struct usb_hcd *hcd);
438 extern void dwc_otg_hcd_stop(struct usb_hcd *hcd);
439 extern int dwc_otg_hcd_get_frame_number(struct usb_hcd *hcd);
440 extern void dwc_otg_hcd_free(struct usb_hcd *hcd);
441 extern int dwc_otg_hcd_urb_enqueue(struct usb_hcd *_hcd,
442                             struct urb *_urb,
443                             gfp_t _mem_flags);
444 extern int dwc_otg_hcd_urb_dequeue(struct usb_hcd *hcd, 
445                                    struct urb *urb,
446                                    int status);
447 extern void dwc_otg_hcd_endpoint_disable(struct usb_hcd *hcd,
448                                          struct usb_host_endpoint *ep);
449 extern irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
450 extern int dwc_otg_hcd_hub_status_data(struct usb_hcd *hcd, 
451                                        char *buf);
452 extern int dwc_otg_hcd_hub_control(struct usb_hcd *hcd, 
453                                    u16 typeReq, 
454                                    u16 wValue, 
455                                    u16 wIndex, 
456                                    char *buf, 
457                                    u16 wLength);
458
459 /** @} */
460
461 /** @name Transaction Execution Functions */
462 /** @{ */
463 extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t *_hcd);
464 extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t *_hcd,
465                                            dwc_otg_transaction_type_e _tr_type);
466 extern void dwc_otg_hcd_complete_urb(dwc_otg_hcd_t *_hcd, struct urb *_urb,
467                                      int _status);
468 /** @} */
469
470 /** @name Interrupt Handler Functions */
471 /** @{ */
472 extern int32_t dwc_otg_hcd_handle_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
473 extern int32_t dwc_otg_hcd_handle_sof_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
474 extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
475 extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
476 extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
477 extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *_dwc_otg_hcd);
478 extern int32_t dwc_otg_hcd_handle_port_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
479 extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
480 extern int32_t dwc_otg_hcd_handle_disconnect_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
481 extern int32_t dwc_otg_hcd_handle_hc_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
482 extern int32_t dwc_otg_hcd_handle_hc_n_intr (dwc_otg_hcd_t *_dwc_otg_hcd, uint32_t _num);
483 extern int32_t dwc_otg_hcd_handle_session_req_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
484 extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr (dwc_otg_hcd_t *_dwc_otg_hcd);
485 /** @} */
486
487
488 /** @name Schedule Queue Functions */
489 /** @{ */
490
491 /* Implemented in dwc_otg_hcd_queue.c */
492 extern dwc_otg_qh_t *dwc_otg_hcd_qh_create (dwc_otg_hcd_t *_hcd, struct urb *_urb);
493 extern void dwc_otg_hcd_qh_init (dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh, struct urb *_urb);
494 extern void dwc_otg_hcd_qh_free (dwc_otg_qh_t *_qh);
495 extern int dwc_otg_hcd_qh_add (dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh);
496 extern void dwc_otg_hcd_qh_remove (dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh);
497 extern void dwc_otg_hcd_qh_deactivate (dwc_otg_hcd_t *_hcd, dwc_otg_qh_t *_qh, int sched_csplit);
498
499 /** Remove and free a QH */
500 static inline void dwc_otg_hcd_qh_remove_and_free (dwc_otg_hcd_t *_hcd,
501                                                    dwc_otg_qh_t *_qh)
502 {
503         dwc_otg_hcd_qh_remove (_hcd, _qh);
504         dwc_otg_hcd_qh_free (_qh);
505 }
506
507 /** Allocates memory for a QH structure.
508  * @return Returns the memory allocate or NULL on error. */
509 static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc (void)
510 {
511         return (dwc_otg_qh_t *) kmalloc (sizeof(dwc_otg_qh_t), GFP_ATOMIC);//GFP_KERNEL
512 }
513
514 extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create (struct urb *urb);
515 extern void dwc_otg_hcd_qtd_init (dwc_otg_qtd_t *qtd, struct urb *urb);
516 extern int dwc_otg_hcd_qtd_add (dwc_otg_qtd_t *qtd, dwc_otg_hcd_t *dwc_otg_hcd);
517
518 /** Allocates memory for a QTD structure.
519  * @return Returns the memory allocate or NULL on error. */
520 static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc (void)
521 {
522         return (dwc_otg_qtd_t *) kmalloc (sizeof(dwc_otg_qtd_t), GFP_ATOMIC);//GFP_KERNEL
523 }
524
525 /** Frees the memory for a QTD structure.  QTD should already be removed from
526  * list.
527  * @param[in] _qtd QTD to free.*/
528 static inline void dwc_otg_hcd_qtd_free (dwc_otg_qtd_t *_qtd)
529 {
530         kfree (_qtd);
531 }
532
533 /** Removes a QTD from list.
534  * @param[in] _qtd QTD to remove from list. */
535 static inline void dwc_otg_hcd_qtd_remove (dwc_otg_qtd_t *_qtd)
536 {
537         unsigned long flags;
538         local_irq_save (flags);
539         list_del (&_qtd->qtd_list_entry);
540         local_irq_restore (flags);
541 }
542
543 /** Remove and free a QTD */
544 static inline void dwc_otg_hcd_qtd_remove_and_free (dwc_otg_qtd_t *_qtd)
545 {
546         dwc_otg_hcd_qtd_remove (_qtd);
547         dwc_otg_hcd_qtd_free (_qtd);
548 }
549
550 /** @} */
551
552
553 /** @name Internal Functions */
554 /** @{ */
555 dwc_otg_qh_t *dwc_urb_to_qh(struct urb *_urb);
556 void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t *_hcd);
557 void dwc_otg_hcd_dump_state(dwc_otg_hcd_t *_hcd);
558 /** @} */
559
560 /** Gets the usb_host_endpoint associated with an URB. */
561 static inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *_urb)
562 {
563         struct usb_device *dev = _urb->dev;
564         int ep_num = usb_pipeendpoint(_urb->pipe);
565         
566         if (usb_pipein(_urb->pipe))
567                 return dev->ep_in[ep_num ];
568         else
569                 return dev->ep_out[ep_num ];
570 }
571
572 /**
573  * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
574  * qualified with its direction (possible 32 endpoints per device).
575  */
576 #define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
577                                                      ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
578
579 /** Gets the QH that contains the list_head */
580 #define dwc_list_to_qh(_list_head_ptr_) (container_of(_list_head_ptr_,dwc_otg_qh_t,qh_list_entry))
581
582 /** Gets the QTD that contains the list_head */
583 #define dwc_list_to_qtd(_list_head_ptr_) (container_of(_list_head_ptr_,dwc_otg_qtd_t,qtd_list_entry))
584
585 /** Check if QH is non-periodic  */
586 #define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == USB_ENDPOINT_XFER_BULK) || \
587                                      (_qh_ptr_->ep_type == USB_ENDPOINT_XFER_CONTROL))
588
589 /** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
590 #define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
591
592 /** Packet size for any kind of endpoint descriptor */
593 #define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
594
595 /**
596  * Returns true if _frame1 is less than or equal to _frame2. The comparison is
597  * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
598  * frame number when the max frame number is reached.
599  */
600 static inline int dwc_frame_num_le(uint16_t _frame1, uint16_t _frame2)
601 {
602         return ((_frame2 - _frame1) & DWC_HFNUM_MAX_FRNUM) <=
603                 (DWC_HFNUM_MAX_FRNUM >> 1);
604 }
605
606 /**
607  * Returns true if _frame1 is greater than _frame2. The comparison is done
608  * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
609  * number when the max frame number is reached.
610  */
611 static inline int dwc_frame_num_gt(uint16_t _frame1, uint16_t _frame2)
612 {
613         return (_frame1 != _frame2) &&
614                 (((_frame1 - _frame2) & DWC_HFNUM_MAX_FRNUM) <
615                  (DWC_HFNUM_MAX_FRNUM >> 1));
616 }
617
618 /**
619  * Increments _frame by the amount specified by _inc. The addition is done
620  * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
621  */
622 static inline uint16_t dwc_frame_num_inc(uint16_t _frame, uint16_t _inc)
623 {
624         return (_frame + _inc) & DWC_HFNUM_MAX_FRNUM;
625 }
626
627 static inline uint16_t dwc_full_frame_num (uint16_t _frame)
628 {
629         return ((_frame) & DWC_HFNUM_MAX_FRNUM) >> 3;
630 }
631
632 static inline uint16_t dwc_micro_frame_num (uint16_t _frame)
633 {
634         return (_frame) & 0x7;
635 }
636
637 #ifdef DEBUG
638 /**
639  * Macro to sample the remaining PHY clocks left in the current frame. This
640  * may be used during debugging to determine the average time it takes to
641  * execute sections of code. There are two possible sample points, "a" and
642  * "b", so the _letter argument must be one of these values.
643  *
644  * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
645  * example, "cat /sys/devices/lm0/hcd_frrem".
646  */
647 #define dwc_sample_frrem(_hcd, _qh, _letter) \
648 { \
649         hfnum_data_t hfnum; \
650         dwc_otg_qtd_t *qtd; \
651         qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
652         if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
653                 hfnum.d32 = dwc_read_reg32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
654                 switch (hfnum.b.frnum & 0x7) { \
655                 case 7: \
656                         _hcd->hfnum_7_samples_##_letter++; \
657                         _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
658                         break; \
659                 case 0: \
660                         _hcd->hfnum_0_samples_##_letter++; \
661                         _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
662                         break; \
663                 default: \
664                         _hcd->hfnum_other_samples_##_letter++; \
665                         _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
666                         break; \
667                 } \
668         } \
669 }
670 #else
671 #define dwc_sample_frrem(_hcd, _qh, _letter) 
672 #endif          
673 #endif
674 #endif /* DWC_DEVICE_ONLY */