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