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