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