USB: Fix usb debug log.
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc_otg_310 / dwc_otg_hcd_linux.c
1 /* ==========================================================================
2  * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
3  * $Revision: #22 $
4  * $Date: 2012/12/21 $
5  * $Change: 2131568 $
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
35 /**
36  * @file
37  *
38  * This file contains the implementation of the HCD. In Linux, the HCD
39  * implements the hc_driver API.
40  */
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
44 #include <linux/init.h>
45 #include <linux/device.h>
46 #include <linux/errno.h>
47 #include <linux/list.h>
48 #include <linux/interrupt.h>
49 #include <linux/string.h>
50 #include <linux/dma-mapping.h>
51 #include <linux/version.h>
52 #include <asm/io.h>
53 #include <linux/usb.h>
54 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
55 #include <../drivers/usb/core/hcd.h>
56 #else
57 #include <linux/usb/hcd.h>
58 #endif
59
60 #include "dwc_otg_hcd_if.h"
61 #include "dwc_otg_dbg.h"
62 #include "dwc_otg_driver.h"
63 #include "dwc_otg_hcd.h"
64 #include "dwc_otg_attr.h"
65 #include "usbdev_rk.h"
66
67 /**
68  * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
69  * qualified with its direction (possible 32 endpoints per device).
70  */
71 #define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
72                                                      ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
73
74 static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
75
76 /** @name Linux HC Driver API Functions */
77 /** @{ */
78 static int urb_enqueue(struct usb_hcd *hcd,
79 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
80                        struct usb_host_endpoint *ep,
81 #endif
82                        struct urb *urb, gfp_t mem_flags);
83 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
84 static int urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
85 #else
86 static int urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
87 #endif
88
89 static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
90 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
91 static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
92 #endif
93 static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
94 extern int hcd_start(struct usb_hcd *hcd);
95 extern void hcd_stop(struct usb_hcd *hcd);
96 extern int hcd_suspend(struct usb_hcd *hcd);
97 extern int hcd_resume(struct usb_hcd *hcd);
98 static int get_frame_number(struct usb_hcd *hcd);
99 extern int hub_status_data(struct usb_hcd *hcd, char *buf);
100 extern int hub_control(struct usb_hcd *hcd,
101                        u16 typeReq,
102                        u16 wValue, u16 wIndex, char *buf, u16 wLength);
103
104 struct wrapper_priv_data {
105         dwc_otg_hcd_t *dwc_otg_hcd;
106 };
107
108 /** @} */
109
110 static struct hc_driver dwc_otg_hc_driver = {
111
112         .description = dwc_otg_hcd_name,
113         .product_desc = "DWC OTG Controller",
114         .hcd_priv_size = sizeof(struct wrapper_priv_data),
115
116         .irq = dwc_otg_hcd_irq,
117
118         .flags = HCD_MEMORY | HCD_USB2,
119
120         //.reset =              
121         .start = hcd_start,
122         //.suspend =            
123         //.resume =             
124         .stop = hcd_stop,
125
126         .urb_enqueue = urb_enqueue,
127         .urb_dequeue = urb_dequeue,
128         .endpoint_disable = endpoint_disable,
129 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
130         .endpoint_reset = endpoint_reset,
131 #endif
132         .get_frame_number = get_frame_number,
133
134         .hub_status_data = hub_status_data,
135         .hub_control = hub_control,
136         .bus_suspend = hcd_suspend ,              
137         .bus_resume = hcd_resume,       
138 };
139
140 /** Gets the dwc_otg_hcd from a struct usb_hcd */
141 static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
142 {
143         struct wrapper_priv_data *p;
144         p = (struct wrapper_priv_data *)(hcd->hcd_priv);
145         return p->dwc_otg_hcd;
146 }
147
148 /** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
149 inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
150 {
151         return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
152 }
153 EXPORT_SYMBOL(dwc_otg_hcd_to_hcd);
154
155 /** Gets the usb_host_endpoint associated with an URB. */
156 inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
157 {
158         struct usb_device *dev = urb->dev;
159         int ep_num = usb_pipeendpoint(urb->pipe);
160
161         if (usb_pipein(urb->pipe))
162                 return dev->ep_in[ep_num];
163         else
164                 return dev->ep_out[ep_num];
165 }
166
167 static int _disconnect(dwc_otg_hcd_t * hcd)
168 {
169         struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
170
171         usb_hcd->self.is_b_host = 0;
172         return 0;
173 }
174
175 static int _start(dwc_otg_hcd_t * hcd)
176 {
177         struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
178
179         usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
180         hcd_start(usb_hcd);
181
182         return 0;
183 }
184
185 static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
186                      uint32_t * port_addr)
187 {
188         struct urb *urb = (struct urb *)urb_handle;
189         if (urb->dev->tt) {
190                 *hub_addr = urb->dev->tt->hub->devnum;
191         } else {
192                 *hub_addr = 0;
193         }
194         *port_addr = urb->dev->ttport;
195         return 0;
196 }
197
198 static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
199 {
200         struct urb *urb = (struct urb *)urb_handle;
201         return urb->dev->speed;
202 }
203
204 static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
205 {
206         struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
207         return usb_hcd->self.b_hnp_enable;
208 }
209
210 static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
211                                    struct urb *urb)
212 {
213         hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
214         if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
215                 hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
216         } else {
217                 hcd_to_bus(hcd)->bandwidth_int_reqs++;
218         }
219 }
220
221 static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
222                                struct urb *urb)
223 {
224         hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
225         if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
226                 hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
227         } else {
228                 hcd_to_bus(hcd)->bandwidth_int_reqs--;
229         }
230 }
231
232 /**
233  * Sets the final status of an URB and returns it to the device driver. Any
234  * required cleanup of the URB is performed.
235  */
236 static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
237                      dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
238 {
239         struct urb *urb = (struct urb *)urb_handle;
240 #ifdef DEBUG
241         if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
242                 DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
243                            __func__, urb, usb_pipedevice(urb->pipe),
244                            usb_pipeendpoint(urb->pipe),
245                            usb_pipein(urb->pipe) ? "IN" : "OUT", status);
246                 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
247                         int i;
248                         for (i = 0; i < urb->number_of_packets; i++) {
249                                 DWC_PRINTF("  ISO Desc %d status: %d\n",
250                                            i, urb->iso_frame_desc[i].status);
251                         }
252                 }
253         }
254 #endif
255
256         urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
257         /* Convert status value. */
258         switch (status) {
259         case -DWC_E_PROTOCOL:
260                 status = -EPROTO;
261                 break;
262         case -DWC_E_IN_PROGRESS:
263                 status = -EINPROGRESS;
264                 break;
265         case -DWC_E_PIPE:
266                 status = -EPIPE;
267                 break;
268         case -DWC_E_IO:
269                 status = -EIO;
270                 break;
271         case -DWC_E_TIMEOUT:
272                 status = -ETIMEDOUT;
273                 break;
274         case -DWC_E_OVERFLOW:
275                 status = -EOVERFLOW;
276                 break;
277         default:
278                 if (status) {
279                         DWC_PRINTF("Uknown urb status %d\n", status);
280
281                 }
282         }
283
284         if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
285                 int i;
286
287                 urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
288                 for (i = 0; i < urb->number_of_packets; ++i) {
289                         urb->iso_frame_desc[i].actual_length =
290                             dwc_otg_hcd_urb_get_iso_desc_actual_length
291                             (dwc_otg_urb, i);
292                         urb->iso_frame_desc[i].status =
293                             dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
294                 }
295         }
296
297         urb->status = status;
298         urb->hcpriv = NULL;
299         if (!status) {
300                 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
301                     (urb->actual_length < urb->transfer_buffer_length)) {
302                         urb->status = -EREMOTEIO;
303                 }
304         }
305
306         if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
307             (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
308                 struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
309                 if (ep) {
310                         free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
311                                            dwc_otg_hcd_get_ep_bandwidth(hcd,
312                                                                         ep->hcpriv),
313                                            urb);
314                 }
315         }
316
317         DWC_FREE(dwc_otg_urb);
318
319         DWC_SPINUNLOCK(hcd->lock);
320 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
321         usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
322 #else
323         usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, status);
324 #endif
325         DWC_SPINLOCK(hcd->lock);
326
327         return 0;
328 }
329
330 void dwc_otg_clear_halt(struct urb *_urb)
331 {
332         struct dwc_otg_qh *_qh;
333         struct usb_host_endpoint *ep = dwc_urb_to_endpoint(_urb);
334         if((ep)&&(ep->hcpriv))
335         {
336                 _qh =  (dwc_otg_qh_t *) ep->hcpriv;
337                 _qh->data_toggle = 0;
338         }
339 }
340
341 static struct dwc_otg_hcd_function_ops hcd_fops = {
342         .start = _start,
343         .disconnect = _disconnect,
344         .hub_info = _hub_info,
345         .speed = _speed,
346         .complete = _complete,
347         .get_b_hnp_enable = _get_b_hnp_enable,
348 };
349 static void dwc_otg_hcd_enable(struct work_struct *work)
350 {
351         dwc_otg_hcd_t *dwc_otg_hcd;
352         dwc_otg_core_if_t *core_if;
353         struct dwc_otg_platform_data *pldata;
354         dwc_otg_hcd = container_of(work, dwc_otg_hcd_t, host_enable_work.work);
355         core_if = dwc_otg_hcd->core_if;
356         pldata = core_if->otg_dev->pldata;
357         if(dwc_otg_hcd->host_enabled == dwc_otg_hcd->host_setenable){
358 //      DWC_PRINT("%s, enable flag %d\n", __func__, dwc_otg_hcd->host_setenable);
359                 goto out;
360         }
361             
362         if(dwc_otg_hcd->host_setenable == 2){// enable -> disable
363                 if(pldata->get_status(USB_STATUS_DPDM)){// usb device connected
364                         dwc_otg_hcd->host_setenable = 1;
365                         goto out;
366                 }
367                 DWC_PRINTF("%s, disable host controller\n", __func__);
368 #if 0
369                 if (_core_if->hcd_cb && _core_if->hcd_cb->disconnect) {
370                 _core_if->hcd_cb->disconnect( _core_if->hcd_cb->p );
371                 }
372 #endif
373                 pldata->soft_reset();
374                 dwc_otg_disable_host_interrupts( core_if );
375                 if(pldata->phy_suspend) 
376                         pldata->phy_suspend( pldata, USB_PHY_SUSPEND);
377                 udelay(3);
378                 pldata->clock_enable( pldata, 0);
379         }else if(dwc_otg_hcd->host_setenable == 1){
380                 DWC_PRINTF("%s, enable host controller\n", __func__);
381                 pldata->clock_enable( pldata, 1);
382                 if(pldata->phy_suspend) 
383                         pldata->phy_suspend( pldata, USB_PHY_ENABLED);
384                 mdelay(5);
385                 dwc_otg_core_init(core_if);
386                 dwc_otg_enable_global_interrupts(core_if);
387                 cil_hcd_start(core_if);
388         }
389         dwc_otg_hcd->host_enabled = dwc_otg_hcd->host_setenable;
390 out:
391         return;
392 }
393 static void dwc_otg_hcd_connect_detect(unsigned long pdata)
394 {
395         dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *)pdata;
396         dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
397         unsigned long flags;
398         struct dwc_otg_platform_data *pldata;
399         pldata = core_if->otg_dev->pldata;
400         local_irq_save(flags);
401         if(pldata->get_status(USB_STATUS_DPDM)) // usb device connected   
402         {
403                 dwc_otg_hcd->host_setenable = 1;
404         }
405         else
406         {                                   // no device, suspend host    
407         if((dwc_otg_read_hprt0(core_if) & 1) == 0)
408                 dwc_otg_hcd->host_setenable = 2;
409         }
410         if((dwc_otg_hcd->host_enabled) && (dwc_otg_hcd->host_setenable != dwc_otg_hcd->host_enabled)){
411                 schedule_delayed_work(&dwc_otg_hcd->host_enable_work, 1);
412         }
413         mod_timer(&dwc_otg_hcd->connect_detect_timer,jiffies + (HZ<<1)); 
414         local_irq_restore(flags);
415         return;
416 }
417
418 static void otg20_hcd_connect_detect(struct work_struct *work)
419 {
420         dwc_otg_hcd_t *dwc_otg_hcd = container_of(work, dwc_otg_hcd_t, host_enable_work.work);
421         dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
422         struct dwc_otg_platform_data *pldata;
423         pldata = core_if->otg_dev->pldata;
424
425         if( pldata->phy_status == USB_PHY_SUSPEND){
426                 pldata->clock_enable(pldata, 1);
427                 pldata->phy_suspend(pldata, USB_PHY_ENABLED);
428         }
429         dwc_otg_core_init(core_if);
430         dwc_otg_enable_global_interrupts(core_if);
431         cil_hcd_start(core_if);
432 }
433
434 /**
435  * Initializes the HCD. This function allocates memory for and initializes the
436  * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
437  * USB bus with the core and calls the hc_driver->start() function. It returns
438  * a negative error on failure.
439  */
440 int otg20_hcd_init( struct platform_device *_dev )
441 {
442         struct usb_hcd *hcd = NULL;
443         dwc_otg_hcd_t *dwc_otg_hcd = NULL;
444
445         dwc_otg_device_t *otg_dev = dwc_get_device_platform_data(_dev);
446         int retval = 0;
447         int irq;
448         static u64 usb_dmamask = 0xffffffffUL;
449
450         DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT\n");
451
452         /* Set device flags indicating whether the HCD supports DMA. */
453         if (dwc_otg_is_dma_enable(otg_dev->core_if)) {
454
455                 _dev->dev.dma_mask = &usb_dmamask;
456                 _dev->dev.coherent_dma_mask = ~0;
457         } else {
458
459                 _dev->dev.dma_mask = (void *)0;
460                 _dev->dev.coherent_dma_mask = 0;
461         }
462
463         /*
464          * Allocate memory for the base HCD plus the DWC OTG HCD.
465          * Initialize the base HCD.
466          */
467 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
468         hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
469 #else
470         hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
471         hcd->has_tt = 1;
472 //      hcd->uses_new_polling = 1;
473 //      hcd->poll_rh = 0;
474 #endif
475         if (!hcd) {
476                 retval = -ENOMEM;
477                 goto error1;
478         }
479
480         hcd->regs = otg_dev->os_dep.base;
481
482         /* Initialize the DWC OTG HCD. */
483         dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
484         if (!dwc_otg_hcd) {
485                 goto error2;
486         }
487         ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
488             dwc_otg_hcd;
489         otg_dev->hcd = dwc_otg_hcd;
490
491         if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
492                 goto error2;
493         }
494
495         otg_dev->hcd->otg_dev = otg_dev;
496         hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
497 #if 0//#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
498         hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
499         /* Don't support SG list at this point */
500         hcd->self.sg_tablesize = 0;
501 #endif
502 #if 0//LINUX_VERSION_CODE >= KERNEL_VERSION(3,6,0)
503         /* Do not to do HNP polling if not capable */
504         //if (otg_dev->core_if->otg_ver)
505         //      hcd->self.is_hnp_cap = dwc_otg_get_hnpcapable(otg_dev->core_if);
506 #endif
507         /*
508          * Finish generic HCD initialization and start the HCD. This function
509          * allocates the DMA buffer pool, registers the USB bus, requests the
510          * IRQ line, and calls hcd_start method.
511          */
512         irq = platform_get_irq(_dev,0);
513         retval = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_DISABLED);
514         if (retval < 0) {
515                 goto error2;
516         }
517
518         dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
519         dwc_otg_hcd->host_enabled = 1;
520         if(dwc_otg_is_host_mode(otg_dev->core_if) ||
521           (otg_dev->core_if->usb_mode == USB_MODE_FORCE_HOST)){
522                 INIT_DELAYED_WORK(&dwc_otg_hcd->host_enable_work, otg20_hcd_connect_detect);
523                 schedule_delayed_work(&dwc_otg_hcd->host_enable_work, HZ>>2);
524         }
525         return 0;
526
527 error2:
528         usb_put_hcd(hcd);
529 error1:
530         return retval;
531 }
532
533
534 /**
535  * Initializes the HCD. This function allocates memory for and initializes the
536  * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
537  * USB bus with the core and calls the hc_driver->start() function. It returns
538  * a negative error on failure.
539  */
540 int host20_hcd_init( struct platform_device *_dev )
541 {
542         struct usb_hcd *hcd = NULL;
543         dwc_otg_hcd_t *dwc_otg_hcd = NULL;
544
545         dwc_otg_device_t *otg_dev = dwc_get_device_platform_data(_dev);
546         int retval = 0;
547         int irq;
548         static u64 usb_dmamask = 0xffffffffUL; 
549         DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT\n");
550
551         /* Set device flags indicating whether the HCD supports DMA. */
552         if (dwc_otg_is_dma_enable(otg_dev->core_if)) {
553
554                 _dev->dev.dma_mask = &usb_dmamask;
555                 _dev->dev.coherent_dma_mask = ~0;
556         } else {
557
558                 _dev->dev.dma_mask = (void *)0;
559                 _dev->dev.coherent_dma_mask = 0;
560         }
561
562         /*
563          * Allocate memory for the base HCD plus the DWC OTG HCD.
564          * Initialize the base HCD.
565          */
566 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
567         hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
568 #else
569         hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
570         hcd->has_tt = 1;
571 //      hcd->uses_new_polling = 1;
572 //      hcd->poll_rh = 0;
573 #endif
574         if (!hcd) {
575                 retval = -ENOMEM;
576                 goto error1;
577         }
578
579         hcd->regs = otg_dev->os_dep.base;
580
581         /* Initialize the DWC OTG HCD. */
582         dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
583         if (!dwc_otg_hcd) {
584                 goto error2;
585         }
586         ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
587             dwc_otg_hcd;
588         otg_dev->hcd = dwc_otg_hcd;
589
590         if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
591                 goto error2;
592         }
593
594         otg_dev->hcd->otg_dev = otg_dev;
595         hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
596 #if 0//#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
597         hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
598         /* Don't support SG list at this point */
599         hcd->self.sg_tablesize = 0;
600 #endif
601 #if 0//LINUX_VERSION_CODE >= KERNEL_VERSION(3,6,0)
602         /* Do not to do HNP polling if not capable */
603         //if (otg_dev->core_if->otg_ver)
604         //      hcd->self.is_hnp_cap = dwc_otg_get_hnpcapable(otg_dev->core_if);
605 #endif
606         /*
607          * Finish generic HCD initialization and start the HCD. This function
608          * allocates the DMA buffer pool, registers the USB bus, requests the
609          * IRQ line, and calls hcd_start method.
610          */
611         irq = platform_get_irq(_dev,0);
612         retval = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_DISABLED);
613         if (retval < 0) {
614                 goto error2;
615         }
616
617         dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
618
619         dwc_otg_hcd->host_enabled = 2;
620         dwc_otg_hcd->host_setenable = 2;
621         dwc_otg_hcd->connect_detect_timer.function = dwc_otg_hcd_connect_detect;
622         dwc_otg_hcd->connect_detect_timer.data = (unsigned long)(dwc_otg_hcd);
623         init_timer( &dwc_otg_hcd->connect_detect_timer);
624         mod_timer(&dwc_otg_hcd->connect_detect_timer, jiffies+(HZ<<3)); 
625     
626         INIT_DELAYED_WORK(&dwc_otg_hcd->host_enable_work, dwc_otg_hcd_enable);
627         return 0;
628
629 error2:
630         usb_put_hcd(hcd);
631 error1:
632         return retval;
633 }
634
635 /**
636  * Removes the HCD.
637  * Frees memory and resources associated with the HCD and deregisters the bus.
638  */
639 void hcd_remove(
640
641                        struct platform_device *_dev
642     )
643 {
644
645         dwc_otg_device_t *otg_dev = dwc_get_device_platform_data(_dev);
646         dwc_otg_hcd_t *dwc_otg_hcd;
647         struct usb_hcd *hcd;
648
649         DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE\n");
650
651         if (!otg_dev) {
652                 DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
653                 return;
654         }
655
656         dwc_otg_hcd = otg_dev->hcd;
657
658         if (!dwc_otg_hcd) {
659                 DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
660                 return;
661         }
662
663         hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
664
665         if (!hcd) {
666                 DWC_DEBUGPL(DBG_ANY,
667                             "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
668                             __func__);
669                 return;
670         }
671         usb_remove_hcd(hcd);
672         dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
673         dwc_otg_hcd_remove(dwc_otg_hcd);
674         usb_put_hcd(hcd);
675 }
676
677 /* =========================================================================
678  *  Linux HC Driver Functions
679  * ========================================================================= */
680
681 /** Initializes the DWC_otg controller and its root hub and prepares it for host
682  * mode operation. Activates the root port. Returns 0 on success and a negative
683  * error code on failure. */
684 int hcd_start(struct usb_hcd *hcd)
685 {
686         dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
687         struct usb_bus *bus;
688
689         DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
690         bus = hcd_to_bus(hcd);
691
692         hcd->state = HC_STATE_RUNNING;
693         if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
694                 if (dwc_otg_hcd->core_if->otg_ver)
695                         dwc_otg_hcd->core_if->op_state = B_PERIPHERAL;
696                 return 0;
697         }
698
699         /* Initialize and connect root hub if one is not already attached */
700         if (bus->root_hub) {
701                 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
702                 /* Inform the HUB driver to resume. */
703                 usb_hcd_resume_root_hub(hcd);
704         }
705
706         return 0;
707 }
708
709 /**
710  * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
711  * stopped.
712  */
713 void hcd_stop(struct usb_hcd *hcd)
714 {
715         dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
716
717         dwc_otg_hcd_stop(dwc_otg_hcd);
718 }
719
720
721 static int dwc_otg_hcd_suspend(struct usb_hcd *hcd)
722 {
723         dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd (hcd);
724         dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
725         hprt0_data_t hprt0;
726         pcgcctl_data_t pcgcctl;
727         struct dwc_otg_platform_data *pldata;
728         pldata = core_if->otg_dev->pldata;
729
730         if(core_if->op_state == B_PERIPHERAL){
731                 DWC_PRINTF("%s, usb device mode\n", __func__);
732                 return 0;
733         }
734
735         if(!(dwc_otg_hcd->host_enabled&1))
736                 return 0;
737
738         hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
739 #ifdef CONFIG_PM_RUNTIME    
740         if((!hprt0.b.prtena) && (!hprt0.b.prtpwr))
741                 return 0;
742 #endif        
743         DWC_PRINTF("%s suspend, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32);
744
745         if(hprt0.b.prtconnsts){ // usb device connected
746                 if(!hprt0.b.prtsusp)
747                 {
748                         hprt0.b.prtsusp = 1;
749                         hprt0.b.prtena = 0;
750                         DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
751                 }
752                 udelay(10);
753                 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
754
755                 if(!hprt0.b.prtsusp)
756                 {
757                         hprt0.b.prtsusp = 1;
758                         hprt0.b.prtena = 0;
759                         DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
760                 }
761                 mdelay(5);
762
763                 pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
764                 //Partial Power-Down mode not enable
765                 pcgcctl.b.pwrclmp = 0;
766                 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
767                 udelay(1);
768                 //pcgcctl.b.rstpdwnmodule = 1;//reset PDM
769                 pcgcctl.b.stoppclk = 1;//stop phy clk
770                 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
771         }
772         else{ //no device connect
773                 if(!pldata->get_status(USB_REMOTE_WAKEUP)){
774                         if(pldata->phy_suspend)
775                                 pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
776                         udelay(3);
777                         if (pldata->clock_enable)
778                                         pldata->clock_enable(pldata, 0);
779                 }
780         }
781
782
783         return 0;
784 }
785
786
787 static int dwc_otg_hcd_resume(struct usb_hcd *hcd)
788 {
789         dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd (hcd);
790         dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
791         hprt0_data_t hprt0;
792         pcgcctl_data_t pcgcctl;
793         gintmsk_data_t gintmsk;
794         struct dwc_otg_platform_data *pldata;
795         pldata = core_if->otg_dev->pldata;
796         
797         if(core_if->op_state == B_PERIPHERAL){
798                 DWC_PRINTF("%s, usb device mode\n", __func__);
799                 return 0;
800         }
801
802 //#ifdef CONFIG_PM_RUNTIME    
803         if(!(dwc_otg_hcd->host_enabled&1))
804                 return 0;
805 //#endif
806
807         if(!pldata->get_status(USB_REMOTE_WAKEUP)){
808                 if (pldata->clock_enable)
809                         pldata->clock_enable( pldata, 1);
810         }
811
812         hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0); 
813 #ifdef CONFIG_PM_RUNTIME
814         //USB HCD already resumed by remote wakeup, return now
815         if((!hprt0.b.prtsusp) && (hprt0.b.prtena))
816                 return 0;
817 #endif
818
819         //power on
820         pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);;
821         pcgcctl.b.stoppclk = 0;//restart phy clk
822         DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
823         udelay(1);
824         pcgcctl.b.pwrclmp = 0;//power clamp
825         DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
826         udelay(2);
827
828         gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
829         gintmsk.b.portintr = 0;
830         DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
831         
832         hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
833
834 #ifdef CONFIG_PM_RUNTIME
835         if((!hprt0.b.prtena) && (!hprt0.b.prtpwr))
836                 return 0;
837 #endif
838         DWC_PRINTF("%s resume, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32);
839         
840         if(hprt0.b.prtconnsts){
841                 //hprt0.d32 = dwc_read_reg32(core_if->host_if->hprt0);
842                 //DWC_PRINT("%s, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32);
843                 hprt0.b.prtpwr = 1;    
844                 hprt0.b.prtres = 1;
845                 hprt0.b.prtena = 0;
846                 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
847                 mdelay(20);
848                 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);    
849                 //DWC_PRINT("%s, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32);
850                 //hprt0.d32 = 0;
851                 hprt0.b.prtpwr = 1;    
852                 hprt0.b.prtres = 0;
853                 hprt0.b.prtena = 0;
854                 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
855                 hprt0.d32 = 0;
856                 hprt0.b.prtpwr = 1;
857                 hprt0.b.prtena = 0;
858                 hprt0.b.prtconndet = 1;
859                 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
860
861                 //hprt0.d32 = dwc_read_reg32(core_if->host_if->hprt0);  
862                 //DWC_PRINT("%s, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32);
863         
864                 mdelay(10);
865         }
866         else{
867                 if(!pldata->get_status(USB_REMOTE_WAKEUP)){
868                         if(pldata->phy_suspend)
869                                 pldata->phy_suspend( pldata, USB_PHY_ENABLED);
870                 }
871         }
872         gintmsk.b.portintr = 1;
873         DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
874
875         return 0;
876 }
877
878 /** HCD Suspend */
879 int hcd_suspend(struct usb_hcd *hcd)
880 {
881         //dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
882
883         DWC_DEBUGPL(DBG_HCD, "HCD SUSPEND\n");
884
885         dwc_otg_hcd_suspend(hcd);
886
887         return 0;
888 }
889
890 /** HCD resume */
891 int hcd_resume(struct usb_hcd *hcd)
892 {
893         //dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
894
895         DWC_DEBUGPL(DBG_HCD, "HCD RESUME\n");
896         
897         dwc_otg_hcd_resume(hcd);
898
899         return 0;
900 }
901
902
903 /** Returns the current frame number. */
904 static int get_frame_number(struct usb_hcd *hcd)
905 {
906         dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
907
908         return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
909 }
910
911 #ifdef DEBUG
912 static void dump_urb_info(struct urb *urb, char *fn_name)
913 {
914         DWC_PRINTF("%s, urb %p\n", fn_name, urb);
915         DWC_PRINTF("  Device address: %d\n", usb_pipedevice(urb->pipe));
916         DWC_PRINTF("  Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
917                    (usb_pipein(urb->pipe) ? "IN" : "OUT"));
918         DWC_PRINTF("  Endpoint type: %s\n", ( {
919                                              char *pipetype;
920                                              switch (usb_pipetype(urb->pipe)) {
921 case PIPE_CONTROL:
922 pipetype = "CONTROL"; break; case PIPE_BULK:
923 pipetype = "BULK"; break; case PIPE_INTERRUPT:
924 pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
925 pipetype = "ISOCHRONOUS"; break; default:
926                                              pipetype = "UNKNOWN"; break;};
927                                              pipetype;}
928                    )) ;
929         DWC_PRINTF("  Speed: %s\n", ( {
930                                      char *speed; switch (urb->dev->speed) {
931 case USB_SPEED_HIGH:
932 speed = "HIGH"; break; case USB_SPEED_FULL:
933 speed = "FULL"; break; case USB_SPEED_LOW:
934 speed = "LOW"; break; default:
935                                      speed = "UNKNOWN"; break;};
936                                      speed;}
937                    )) ;
938         DWC_PRINTF("  Max packet size: %d\n",
939                    usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
940         DWC_PRINTF("  Data buffer length: %d\n", urb->transfer_buffer_length);
941         DWC_PRINTF("  Transfer buffer: %p, Transfer DMA: %p\n",
942                    urb->transfer_buffer, (void *)urb->transfer_dma);
943         DWC_PRINTF("  Setup buffer: %p, Setup DMA: %p\n",
944                    urb->setup_packet, (void *)urb->setup_dma);
945         DWC_PRINTF("  Interval: %d\n", urb->interval);
946         if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
947                 int i;
948                 for (i = 0; i < urb->number_of_packets; i++) {
949                         DWC_PRINTF("  ISO Desc %d:\n", i);
950                         DWC_PRINTF("    offset: %d, length %d\n",
951                                    urb->iso_frame_desc[i].offset,
952                                    urb->iso_frame_desc[i].length);
953                 }
954         }
955 }
956
957 #endif
958
959 /** Starts processing a USB transfer request specified by a USB Request Block
960  * (URB). mem_flags indicates the type of memory allocation to use while
961  * processing this URB. */
962 static int urb_enqueue(struct usb_hcd *hcd,
963 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
964                        struct usb_host_endpoint *ep,
965 #endif
966                        struct urb *urb, gfp_t mem_flags)
967 {
968         int retval = 0;
969 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
970         struct usb_host_endpoint *ep = urb->ep;
971 #endif
972         dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
973         dwc_otg_hcd_urb_t *dwc_otg_urb;
974         int i;
975         int alloc_bandwidth = 0;
976         uint8_t ep_type = 0;
977         uint32_t flags = 0;
978         void *buf;
979
980 #ifdef DEBUG
981         if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
982                 dump_urb_info(urb, "urb_enqueue");
983         }
984 #endif
985
986         if(atomic_read(&urb->use_count)>1){
987                 retval = -EPERM;
988                 printk("%s urb %p already in queue, qtd %p, count%d\n", 
989                         __func__, urb, urb->hcpriv, atomic_read(&urb->use_count));
990                  return retval;
991         }
992
993         if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
994             || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
995                 if (!dwc_otg_hcd_is_bandwidth_allocated
996                     (dwc_otg_hcd, &ep->hcpriv)) {
997                         alloc_bandwidth = 1;
998                 }
999         }
1000
1001         switch (usb_pipetype(urb->pipe)) {
1002         case PIPE_CONTROL:
1003                 ep_type = USB_ENDPOINT_XFER_CONTROL;
1004                 break;
1005         case PIPE_ISOCHRONOUS:
1006                 ep_type = USB_ENDPOINT_XFER_ISOC;
1007                 break;
1008         case PIPE_BULK:
1009                 ep_type = USB_ENDPOINT_XFER_BULK;
1010                 break;
1011         case PIPE_INTERRUPT:
1012                 ep_type = USB_ENDPOINT_XFER_INT;
1013                 break;
1014         default:
1015                 DWC_WARN("Wrong ep type\n");
1016         }
1017
1018         dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
1019                                             urb->number_of_packets,
1020                                             mem_flags == GFP_ATOMIC ? 1 : 0);
1021
1022         dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
1023                                      usb_pipeendpoint(urb->pipe), ep_type,
1024                                      usb_pipein(urb->pipe),
1025                                      usb_maxpacket(urb->dev, urb->pipe,
1026                                                    !(usb_pipein(urb->pipe))));
1027
1028 #ifdef DEBUG
1029         if((uint32_t)urb->transfer_buffer & 3){
1030                 DWC_PRINTF("%s urb->transfer_buffer address not align to 4-byte 0x%x\n", 
1031                             __func__, (uint32_t)urb->transfer_buffer);
1032         }
1033 #endif
1034
1035         buf = urb->transfer_buffer;
1036
1037         if (hcd->self.uses_dma) {
1038                 /*
1039                  * Calculate virtual address from physical address,
1040                  * because some class driver may not fill transfer_buffer.
1041                  * In Buffer DMA mode virual address is used,
1042                  * when handling non DWORD aligned buffers.
1043                  */
1044                 buf = phys_to_virt(urb->transfer_dma);
1045         }
1046
1047         if (!(urb->transfer_flags & URB_NO_INTERRUPT))
1048                 flags |= URB_GIVEBACK_ASAP;
1049         if (urb->transfer_flags & URB_ZERO_PACKET)
1050                 flags |= URB_SEND_ZERO_PACKET;
1051
1052         dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
1053                                    urb->transfer_dma,
1054                                    urb->transfer_buffer_length,
1055                                    urb->setup_packet,
1056                                    urb->setup_dma, flags, urb->interval);
1057
1058         for (i = 0; i < urb->number_of_packets; ++i) {
1059                 dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
1060                                                     urb->
1061                                                     iso_frame_desc[i].offset,
1062                                                     urb->
1063                                                     iso_frame_desc[i].length);
1064         }
1065
1066         urb->hcpriv = dwc_otg_urb;
1067         retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb, &ep->hcpriv,
1068                                          mem_flags == GFP_ATOMIC ? 1 : 0);
1069         if (!retval) {
1070                 if (alloc_bandwidth) {
1071                         allocate_bus_bandwidth(hcd,
1072                                                dwc_otg_hcd_get_ep_bandwidth
1073                                                (dwc_otg_hcd, ep->hcpriv), urb);
1074                 }
1075         } else {
1076                 if (retval == -DWC_E_NO_DEVICE) {
1077                         retval = -ENODEV;
1078                 }
1079         }
1080
1081         return retval;
1082 }
1083
1084 /** Aborts/cancels a USB transfer request. Always returns 0 to indicate
1085  * success.  */
1086 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
1087 static int urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1088 #else
1089 static int urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1090 #endif
1091 {
1092         dwc_irqflags_t flags;
1093         dwc_otg_hcd_t *dwc_otg_hcd;
1094         DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
1095
1096         dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1097
1098 #ifdef DEBUG
1099         if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
1100                 dump_urb_info(urb, "urb_dequeue");
1101         }
1102 #endif
1103
1104         DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
1105
1106         if(((uint32_t)urb&0xf0000000)==0){
1107                 DWC_PRINTF("%s error: urb is %p!!!\n", __func__, urb);
1108                 goto out1;
1109         }
1110         
1111         if(((uint32_t)urb->hcpriv&0xf0000000) == 0){
1112                 DWC_PRINTF("%s error: urb->hcpriv %p urb %p, count %d!!!\n",__func__, 
1113                            urb->hcpriv, urb, atomic_read(&urb->use_count));
1114                 if((atomic_read(&urb->use_count)) == 1)
1115                         goto out2;
1116                 else{
1117                         DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
1118                         return 0;
1119                 }
1120         }
1121         
1122         dwc_otg_hcd_urb_dequeue(dwc_otg_hcd, urb->hcpriv);
1123
1124 out2:
1125         DWC_FREE(urb->hcpriv);
1126         urb->hcpriv = NULL;
1127         DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
1128
1129         /* Higher layer software sets URB status. */
1130 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
1131         usb_hcd_giveback_urb(hcd, urb);
1132 #else
1133         usb_hcd_giveback_urb(hcd, urb, status);
1134 #endif
1135         if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
1136                 DWC_PRINTF("Called usb_hcd_giveback_urb()\n");
1137                 DWC_PRINTF("  urb->status = %d\n", urb->status);
1138         }
1139 out1:
1140         return 0;
1141 }
1142
1143 /* Frees resources in the DWC_otg controller related to a given endpoint. Also
1144  * clears state in the HCD related to the endpoint. Any URBs for the endpoint
1145  * must already be dequeued. */
1146 static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1147 {
1148         dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1149
1150         DWC_DEBUGPL(DBG_HCD,
1151                     "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
1152                     "endpoint=%d\n", ep->desc.bEndpointAddress,
1153                     dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
1154         dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
1155         ep->hcpriv = NULL;
1156 }
1157
1158 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
1159 /* Resets endpoint specific parameter values, in current version used to reset 
1160  * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
1161 static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1162 {
1163         dwc_irqflags_t flags;
1164         struct usb_device *udev = NULL;
1165         int epnum = usb_endpoint_num(&ep->desc);
1166         int is_out = usb_endpoint_dir_out(&ep->desc);
1167         int is_control = usb_endpoint_xfer_control(&ep->desc);
1168         dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1169
1170         struct platform_device *_dev = dwc_otg_hcd->otg_dev->os_dep.pdev;
1171         if (_dev)
1172                 udev = to_usb_device(&_dev->dev);
1173         else
1174                 return;
1175
1176         DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
1177
1178         DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
1179         usb_settoggle(udev, epnum, is_out, 0);
1180         if (is_control)
1181                 usb_settoggle(udev, epnum, !is_out, 0);
1182
1183         if (ep->hcpriv) {
1184                 dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
1185         }
1186         DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
1187 }
1188 #endif
1189
1190 /** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
1191  * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
1192  * interrupt.
1193  *
1194  * This function is called by the USB core when an interrupt occurs */
1195 static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
1196 {
1197         dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1198         int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
1199         if (retval != 0) {
1200                 //S3C2410X_CLEAR_EINTPEND();
1201         }
1202         return IRQ_RETVAL(retval);
1203 }
1204
1205 /** Creates Status Change bitmap for the root hub and root port. The bitmap is
1206  * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
1207  * is the status change indicator for the single root port. Returns 1 if either
1208  * change indicator is 1, otherwise returns 0. */
1209 int hub_status_data(struct usb_hcd *hcd, char *buf)
1210 {
1211         dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1212
1213         buf[0] = 0;
1214         buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
1215
1216         return (buf[0] != 0);
1217 }
1218
1219 /** Handles hub class-specific requests. */
1220 int hub_control(struct usb_hcd *hcd,
1221                 u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
1222 {
1223         int retval;
1224
1225         retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
1226                                          typeReq, wValue, wIndex, buf, wLength);
1227
1228         switch (retval) {
1229         case -DWC_E_INVALID:
1230                 retval = -EINVAL;
1231                 break;
1232         }
1233
1234         return retval;
1235 }
1236
1237 #endif /* DWC_DEVICE_ONLY */