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