1 /* ==========================================================================
2 * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
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.
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.
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
32 * ========================================================================== */
33 #ifndef DWC_DEVICE_ONLY
38 * This file contains the implementation of the HCD. In Linux, the HCD
39 * implements the hc_driver API.
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>
53 #include <linux/usb.h>
54 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
55 #include <../drivers/usb/core/hcd.h>
57 #include <linux/usb/hcd.h>
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"
68 * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
69 * qualified with its direction (possible 32 endpoints per device).
71 #define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
72 ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
74 static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
76 /** @name Linux HC Driver API Functions */
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,
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);
86 static int urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
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);
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,
102 u16 wValue, u16 wIndex, char *buf, u16 wLength);
104 struct wrapper_priv_data {
105 dwc_otg_hcd_t *dwc_otg_hcd;
110 static struct hc_driver dwc_otg_hc_driver = {
112 .description = dwc_otg_hcd_name,
113 .product_desc = "DWC OTG Controller",
114 .hcd_priv_size = sizeof(struct wrapper_priv_data),
116 .irq = dwc_otg_hcd_irq,
118 .flags = HCD_MEMORY | HCD_USB2,
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,
132 .get_frame_number = get_frame_number,
134 .hub_status_data = hub_status_data,
135 .hub_control = hub_control,
136 .bus_suspend = hcd_suspend ,
137 .bus_resume = hcd_resume,
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)
143 struct wrapper_priv_data *p;
144 p = (struct wrapper_priv_data *)(hcd->hcd_priv);
145 return p->dwc_otg_hcd;
148 /** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
149 inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
151 return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
153 EXPORT_SYMBOL(dwc_otg_hcd_to_hcd);
155 /** Gets the usb_host_endpoint associated with an URB. */
156 inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
158 struct usb_device *dev = urb->dev;
159 int ep_num = usb_pipeendpoint(urb->pipe);
161 if (usb_pipein(urb->pipe))
162 return dev->ep_in[ep_num];
164 return dev->ep_out[ep_num];
167 static int _disconnect(dwc_otg_hcd_t * hcd)
169 struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
171 usb_hcd->self.is_b_host = 0;
175 static int _start(dwc_otg_hcd_t * hcd)
177 struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
179 usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
185 static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
186 uint32_t * port_addr)
188 struct urb *urb = (struct urb *)urb_handle;
190 *hub_addr = urb->dev->tt->hub->devnum;
194 *port_addr = urb->dev->ttport;
198 static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
200 struct urb *urb = (struct urb *)urb_handle;
201 return urb->dev->speed;
204 static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
206 struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
207 return usb_hcd->self.b_hnp_enable;
210 static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
213 hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
214 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
215 hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
217 hcd_to_bus(hcd)->bandwidth_int_reqs++;
221 static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
224 hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
225 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
226 hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
228 hcd_to_bus(hcd)->bandwidth_int_reqs--;
233 * Sets the final status of an URB and returns it to the device driver. Any
234 * required cleanup of the URB is performed.
236 static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
237 dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
239 struct urb *urb = (struct urb *)urb_handle;
241 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
242 DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
243 __func__, urb, usb_pipedevice(urb->pipe),
244 usb_pipeendpoint(urb->pipe),
245 usb_pipein(urb->pipe) ? "IN" : "OUT", status);
246 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
248 for (i = 0; i < urb->number_of_packets; i++) {
249 DWC_PRINTF(" ISO Desc %d status: %d\n",
250 i, urb->iso_frame_desc[i].status);
256 urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
257 /* Convert status value. */
259 case -DWC_E_PROTOCOL:
262 case -DWC_E_IN_PROGRESS:
263 status = -EINPROGRESS;
274 case -DWC_E_OVERFLOW:
279 DWC_PRINTF("Uknown urb status %d\n", status);
284 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
287 urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
288 for (i = 0; i < urb->number_of_packets; ++i) {
289 urb->iso_frame_desc[i].actual_length =
290 dwc_otg_hcd_urb_get_iso_desc_actual_length
292 urb->iso_frame_desc[i].status =
293 dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
297 urb->status = status;
300 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
301 (urb->actual_length < urb->transfer_buffer_length)) {
302 urb->status = -EREMOTEIO;
306 if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
307 (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
308 struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
310 free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
311 dwc_otg_hcd_get_ep_bandwidth(hcd,
317 DWC_FREE(dwc_otg_urb);
319 DWC_SPINUNLOCK(hcd->lock);
320 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
321 usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
323 usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, status);
325 DWC_SPINLOCK(hcd->lock);
330 void dwc_otg_clear_halt(struct urb *_urb)
332 struct dwc_otg_qh *_qh;
333 struct usb_host_endpoint *ep = dwc_urb_to_endpoint(_urb);
334 if((ep)&&(ep->hcpriv))
336 _qh = (dwc_otg_qh_t *) ep->hcpriv;
337 _qh->data_toggle = 0;
341 static struct dwc_otg_hcd_function_ops hcd_fops = {
343 .disconnect = _disconnect,
344 .hub_info = _hub_info,
346 .complete = _complete,
347 .get_b_hnp_enable = _get_b_hnp_enable,
349 static void dwc_otg_hcd_enable(struct work_struct *work)
351 dwc_otg_hcd_t *dwc_otg_hcd;
352 dwc_otg_core_if_t *core_if;
353 struct dwc_otg_platform_data *pldata;
354 dwc_otg_hcd = container_of(work, dwc_otg_hcd_t, host_enable_work.work);
355 core_if = dwc_otg_hcd->core_if;
356 pldata = core_if->otg_dev->pldata;
357 if(dwc_otg_hcd->host_enabled == dwc_otg_hcd->host_setenable){
358 // DWC_PRINT("%s, enable flag %d\n", __func__, dwc_otg_hcd->host_setenable);
362 if(dwc_otg_hcd->host_setenable == 2){// enable -> disable
363 if(pldata->get_status(USB_STATUS_DPDM)){// usb device connected
364 dwc_otg_hcd->host_setenable = 1;
367 DWC_PRINTF("%s, disable host controller\n", __func__);
369 if (_core_if->hcd_cb && _core_if->hcd_cb->disconnect) {
370 _core_if->hcd_cb->disconnect( _core_if->hcd_cb->p );
373 pldata->soft_reset();
374 dwc_otg_disable_host_interrupts( core_if );
375 if(pldata->phy_suspend)
376 pldata->phy_suspend( pldata, USB_PHY_SUSPEND);
378 pldata->clock_enable( pldata, 0);
379 }else if(dwc_otg_hcd->host_setenable == 1){
380 DWC_PRINTF("%s, enable host controller\n", __func__);
381 pldata->clock_enable( pldata, 1);
382 if(pldata->phy_suspend)
383 pldata->phy_suspend( pldata, USB_PHY_ENABLED);
385 dwc_otg_core_init(core_if);
386 dwc_otg_enable_global_interrupts(core_if);
387 cil_hcd_start(core_if);
389 dwc_otg_hcd->host_enabled = dwc_otg_hcd->host_setenable;
393 static void dwc_otg_hcd_connect_detect(unsigned long pdata)
395 dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *)pdata;
396 dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
398 struct dwc_otg_platform_data *pldata;
399 pldata = core_if->otg_dev->pldata;
400 local_irq_save(flags);
401 if(pldata->get_status(USB_STATUS_DPDM)) // usb device connected
403 dwc_otg_hcd->host_setenable = 1;
406 { // no device, suspend host
407 if((dwc_otg_read_hprt0(core_if) & 1) == 0)
408 dwc_otg_hcd->host_setenable = 2;
410 if((dwc_otg_hcd->host_enabled) && (dwc_otg_hcd->host_setenable != dwc_otg_hcd->host_enabled)){
411 schedule_delayed_work(&dwc_otg_hcd->host_enable_work, 1);
413 mod_timer(&dwc_otg_hcd->connect_detect_timer,jiffies + (HZ<<1));
414 local_irq_restore(flags);
419 * Initializes the HCD. This function allocates memory for and initializes the
420 * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
421 * USB bus with the core and calls the hc_driver->start() function. It returns
422 * a negative error on failure.
424 int otg20_hcd_init( struct platform_device *_dev )
426 struct usb_hcd *hcd = NULL;
427 dwc_otg_hcd_t *dwc_otg_hcd = NULL;
429 dwc_otg_device_t *otg_dev = dwc_get_device_platform_data(_dev);
432 static u64 usb_dmamask = 0xffffffffUL;
434 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT\n");
436 /* Set device flags indicating whether the HCD supports DMA. */
437 if (dwc_otg_is_dma_enable(otg_dev->core_if)) {
439 _dev->dev.dma_mask = &usb_dmamask;
440 _dev->dev.coherent_dma_mask = ~0;
443 _dev->dev.dma_mask = (void *)0;
444 _dev->dev.coherent_dma_mask = 0;
448 * Allocate memory for the base HCD plus the DWC OTG HCD.
449 * Initialize the base HCD.
451 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
452 hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
454 hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
456 // hcd->uses_new_polling = 1;
464 hcd->regs = otg_dev->os_dep.base;
466 /* Initialize the DWC OTG HCD. */
467 dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
471 ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
473 otg_dev->hcd = dwc_otg_hcd;
475 if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
479 otg_dev->hcd->otg_dev = otg_dev;
480 hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
481 #if 0//#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
482 hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
483 /* Don't support SG list at this point */
484 hcd->self.sg_tablesize = 0;
486 #if 0//LINUX_VERSION_CODE >= KERNEL_VERSION(3,6,0)
487 /* Do not to do HNP polling if not capable */
488 //if (otg_dev->core_if->otg_ver)
489 // hcd->self.is_hnp_cap = dwc_otg_get_hnpcapable(otg_dev->core_if);
492 * Finish generic HCD initialization and start the HCD. This function
493 * allocates the DMA buffer pool, registers the USB bus, requests the
494 * IRQ line, and calls hcd_start method.
496 irq = platform_get_irq(_dev,0);
497 retval = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_DISABLED);
502 dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
503 dwc_otg_hcd->host_enabled = 1;
515 * Initializes the HCD. This function allocates memory for and initializes the
516 * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
517 * USB bus with the core and calls the hc_driver->start() function. It returns
518 * a negative error on failure.
520 int host20_hcd_init( struct platform_device *_dev )
522 struct usb_hcd *hcd = NULL;
523 dwc_otg_hcd_t *dwc_otg_hcd = NULL;
525 dwc_otg_device_t *otg_dev = dwc_get_device_platform_data(_dev);
528 static u64 usb_dmamask = 0xffffffffUL;
529 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT\n");
531 /* Set device flags indicating whether the HCD supports DMA. */
532 if (dwc_otg_is_dma_enable(otg_dev->core_if)) {
534 _dev->dev.dma_mask = &usb_dmamask;
535 _dev->dev.coherent_dma_mask = ~0;
538 _dev->dev.dma_mask = (void *)0;
539 _dev->dev.coherent_dma_mask = 0;
543 * Allocate memory for the base HCD plus the DWC OTG HCD.
544 * Initialize the base HCD.
546 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
547 hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
549 hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
551 // hcd->uses_new_polling = 1;
559 hcd->regs = otg_dev->os_dep.base;
561 /* Initialize the DWC OTG HCD. */
562 dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
566 ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
568 otg_dev->hcd = dwc_otg_hcd;
570 if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
574 otg_dev->hcd->otg_dev = otg_dev;
575 hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
576 #if 0//#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
577 hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
578 /* Don't support SG list at this point */
579 hcd->self.sg_tablesize = 0;
581 #if 0//LINUX_VERSION_CODE >= KERNEL_VERSION(3,6,0)
582 /* Do not to do HNP polling if not capable */
583 //if (otg_dev->core_if->otg_ver)
584 // hcd->self.is_hnp_cap = dwc_otg_get_hnpcapable(otg_dev->core_if);
587 * Finish generic HCD initialization and start the HCD. This function
588 * allocates the DMA buffer pool, registers the USB bus, requests the
589 * IRQ line, and calls hcd_start method.
591 irq = platform_get_irq(_dev,0);
592 retval = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_DISABLED);
597 dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
599 dwc_otg_hcd->host_enabled = 1;
600 dwc_otg_hcd->host_setenable = 1;
601 dwc_otg_hcd->connect_detect_timer.function = dwc_otg_hcd_connect_detect;
602 dwc_otg_hcd->connect_detect_timer.data = (unsigned long)(dwc_otg_hcd);
603 init_timer( &dwc_otg_hcd->connect_detect_timer);
604 mod_timer(&dwc_otg_hcd->connect_detect_timer, jiffies+(HZ<<3));
606 INIT_DELAYED_WORK(&dwc_otg_hcd->host_enable_work, dwc_otg_hcd_enable);
617 * Frees memory and resources associated with the HCD and deregisters the bus.
621 struct platform_device *_dev
625 dwc_otg_device_t *otg_dev = dwc_get_device_platform_data(_dev);
626 dwc_otg_hcd_t *dwc_otg_hcd;
629 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE\n");
632 DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
636 dwc_otg_hcd = otg_dev->hcd;
639 DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
643 hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
647 "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
652 dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
653 dwc_otg_hcd_remove(dwc_otg_hcd);
657 /* =========================================================================
658 * Linux HC Driver Functions
659 * ========================================================================= */
661 /** Initializes the DWC_otg controller and its root hub and prepares it for host
662 * mode operation. Activates the root port. Returns 0 on success and a negative
663 * error code on failure. */
664 int hcd_start(struct usb_hcd *hcd)
666 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
669 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
670 bus = hcd_to_bus(hcd);
672 hcd->state = HC_STATE_RUNNING;
673 if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
674 if (dwc_otg_hcd->core_if->otg_ver)
675 dwc_otg_hcd->core_if->op_state = B_PERIPHERAL;
679 /* Initialize and connect root hub if one is not already attached */
681 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
682 /* Inform the HUB driver to resume. */
683 usb_hcd_resume_root_hub(hcd);
690 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
693 void hcd_stop(struct usb_hcd *hcd)
695 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
697 dwc_otg_hcd_stop(dwc_otg_hcd);
701 static int dwc_otg_hcd_suspend(struct usb_hcd *hcd)
703 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd (hcd);
704 dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
706 pcgcctl_data_t pcgcctl;
707 struct dwc_otg_platform_data *pldata;
708 pldata = core_if->otg_dev->pldata;
710 if(core_if->op_state == B_PERIPHERAL){
711 DWC_PRINTF("%s, usb device mode\n", __func__);
715 if(!(dwc_otg_hcd->host_enabled&1))
718 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
719 #ifdef CONFIG_PM_RUNTIME
720 if((!hprt0.b.prtena) && (!hprt0.b.prtpwr))
723 DWC_PRINTF("%s suspend, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32);
725 if(hprt0.b.prtconnsts){ // usb device connected
730 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
733 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
739 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
743 pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
744 //Partial Power-Down mode not enable
745 pcgcctl.b.pwrclmp = 0;
746 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
748 //pcgcctl.b.rstpdwnmodule = 1;//reset PDM
749 pcgcctl.b.stoppclk = 1;//stop phy clk
750 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
752 else{ //no device connect
753 if(!pldata->get_status(USB_REMOTE_WAKEUP)){
754 if(pldata->phy_suspend)
755 pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
756 if (pldata->clock_enable)
757 pldata->clock_enable(pldata, 0);
767 static int dwc_otg_hcd_resume(struct usb_hcd *hcd)
769 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd (hcd);
770 dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
772 pcgcctl_data_t pcgcctl;
773 gintmsk_data_t gintmsk;
774 struct dwc_otg_platform_data *pldata;
775 pldata = core_if->otg_dev->pldata;
777 if(core_if->op_state == B_PERIPHERAL){
778 DWC_PRINTF("%s, usb device mode\n", __func__);
782 //#ifdef CONFIG_PM_RUNTIME
783 if(!(dwc_otg_hcd->host_enabled&1))
787 if(!pldata->get_status(USB_REMOTE_WAKEUP)){
788 if (pldata->clock_enable)
789 pldata->clock_enable( pldata, 1);
792 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
793 #ifdef CONFIG_PM_RUNTIME
794 //USB HCD already resumed by remote wakeup, return now
795 if((!hprt0.b.prtsusp) && (hprt0.b.prtena))
800 pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);;
801 pcgcctl.b.stoppclk = 0;//restart phy clk
802 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
804 pcgcctl.b.pwrclmp = 0;//power clamp
805 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
808 gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
809 gintmsk.b.portintr = 0;
810 DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
812 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
814 #ifdef CONFIG_PM_RUNTIME
815 if((!hprt0.b.prtena) && (!hprt0.b.prtpwr))
818 DWC_PRINTF("%s resume, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32);
820 if(hprt0.b.prtconnsts){
821 //hprt0.d32 = dwc_read_reg32(core_if->host_if->hprt0);
822 //DWC_PRINT("%s, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32);
826 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
828 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
829 //DWC_PRINT("%s, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32);
834 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
838 hprt0.b.prtconndet = 1;
839 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
841 //hprt0.d32 = dwc_read_reg32(core_if->host_if->hprt0);
842 //DWC_PRINT("%s, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32);
847 if(!pldata->get_status(USB_REMOTE_WAKEUP)){
848 if(pldata->phy_suspend)
849 pldata->phy_suspend( pldata, USB_PHY_ENABLED);
852 gintmsk.b.portintr = 1;
853 DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
859 int hcd_suspend(struct usb_hcd *hcd)
861 //dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
863 DWC_DEBUGPL(DBG_HCD, "HCD SUSPEND\n");
865 dwc_otg_hcd_suspend(hcd);
871 int hcd_resume(struct usb_hcd *hcd)
873 //dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
875 DWC_DEBUGPL(DBG_HCD, "HCD RESUME\n");
877 dwc_otg_hcd_resume(hcd);
883 /** Returns the current frame number. */
884 static int get_frame_number(struct usb_hcd *hcd)
886 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
888 return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
892 static void dump_urb_info(struct urb *urb, char *fn_name)
894 DWC_PRINTF("%s, urb %p\n", fn_name, urb);
895 DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
896 DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
897 (usb_pipein(urb->pipe) ? "IN" : "OUT"));
898 DWC_PRINTF(" Endpoint type: %s\n", ( {
900 switch (usb_pipetype(urb->pipe)) {
902 pipetype = "CONTROL"; break; case PIPE_BULK:
903 pipetype = "BULK"; break; case PIPE_INTERRUPT:
904 pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
905 pipetype = "ISOCHRONOUS"; break; default:
906 pipetype = "UNKNOWN"; break;};
909 DWC_PRINTF(" Speed: %s\n", ( {
910 char *speed; switch (urb->dev->speed) {
912 speed = "HIGH"; break; case USB_SPEED_FULL:
913 speed = "FULL"; break; case USB_SPEED_LOW:
914 speed = "LOW"; break; default:
915 speed = "UNKNOWN"; break;};
918 DWC_PRINTF(" Max packet size: %d\n",
919 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
920 DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
921 DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
922 urb->transfer_buffer, (void *)urb->transfer_dma);
923 DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
924 urb->setup_packet, (void *)urb->setup_dma);
925 DWC_PRINTF(" Interval: %d\n", urb->interval);
926 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
928 for (i = 0; i < urb->number_of_packets; i++) {
929 DWC_PRINTF(" ISO Desc %d:\n", i);
930 DWC_PRINTF(" offset: %d, length %d\n",
931 urb->iso_frame_desc[i].offset,
932 urb->iso_frame_desc[i].length);
939 /** Starts processing a USB transfer request specified by a USB Request Block
940 * (URB). mem_flags indicates the type of memory allocation to use while
941 * processing this URB. */
942 static int urb_enqueue(struct usb_hcd *hcd,
943 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
944 struct usb_host_endpoint *ep,
946 struct urb *urb, gfp_t mem_flags)
949 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
950 struct usb_host_endpoint *ep = urb->ep;
952 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
953 dwc_otg_hcd_urb_t *dwc_otg_urb;
955 int alloc_bandwidth = 0;
961 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
962 dump_urb_info(urb, "urb_enqueue");
966 if(atomic_read(&urb->use_count)>1){
968 printk("%s urb %p already in queue, qtd %p, count%d\n",
969 __func__, urb, urb->hcpriv, atomic_read(&urb->use_count));
973 if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
974 || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
975 if (!dwc_otg_hcd_is_bandwidth_allocated
976 (dwc_otg_hcd, &ep->hcpriv)) {
981 switch (usb_pipetype(urb->pipe)) {
983 ep_type = USB_ENDPOINT_XFER_CONTROL;
985 case PIPE_ISOCHRONOUS:
986 ep_type = USB_ENDPOINT_XFER_ISOC;
989 ep_type = USB_ENDPOINT_XFER_BULK;
992 ep_type = USB_ENDPOINT_XFER_INT;
995 DWC_WARN("Wrong ep type\n");
998 dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
999 urb->number_of_packets,
1000 mem_flags == GFP_ATOMIC ? 1 : 0);
1002 dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
1003 usb_pipeendpoint(urb->pipe), ep_type,
1004 usb_pipein(urb->pipe),
1005 usb_maxpacket(urb->dev, urb->pipe,
1006 !(usb_pipein(urb->pipe))));
1008 if((uint32_t)urb->transfer_buffer & 3){
1009 DWC_PRINTF("%s urb->transfer_buffer address not align to 4-byte 0x%x\n",
1010 __func__, (uint32_t)urb->transfer_buffer);
1013 buf = urb->transfer_buffer;
1015 if (hcd->self.uses_dma) {
1017 * Calculate virtual address from physical address,
1018 * because some class driver may not fill transfer_buffer.
1019 * In Buffer DMA mode virual address is used,
1020 * when handling non DWORD aligned buffers.
1022 buf = phys_to_virt(urb->transfer_dma);
1025 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
1026 flags |= URB_GIVEBACK_ASAP;
1027 if (urb->transfer_flags & URB_ZERO_PACKET)
1028 flags |= URB_SEND_ZERO_PACKET;
1030 dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
1032 urb->transfer_buffer_length,
1034 urb->setup_dma, flags, urb->interval);
1036 for (i = 0; i < urb->number_of_packets; ++i) {
1037 dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
1039 iso_frame_desc[i].offset,
1041 iso_frame_desc[i].length);
1044 urb->hcpriv = dwc_otg_urb;
1045 retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb, &ep->hcpriv,
1046 mem_flags == GFP_ATOMIC ? 1 : 0);
1048 if (alloc_bandwidth) {
1049 allocate_bus_bandwidth(hcd,
1050 dwc_otg_hcd_get_ep_bandwidth
1051 (dwc_otg_hcd, ep->hcpriv), urb);
1054 if (retval == -DWC_E_NO_DEVICE) {
1062 /** Aborts/cancels a USB transfer request. Always returns 0 to indicate
1064 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
1065 static int urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1067 static int urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1070 dwc_irqflags_t flags;
1071 dwc_otg_hcd_t *dwc_otg_hcd;
1072 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
1074 dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1077 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
1078 dump_urb_info(urb, "urb_dequeue");
1082 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
1084 if(((uint32_t)urb&0xf0000000)==0){
1085 DWC_PRINTF("%s error: urb is %p!!!\n", __func__, urb);
1089 if(((uint32_t)urb->hcpriv&0xf0000000) == 0){
1090 DWC_PRINTF("%s error: urb->hcpriv %p urb %p, count %d!!!\n",__func__,
1091 urb->hcpriv, urb, atomic_read(&urb->use_count));
1092 if((atomic_read(&urb->use_count)) == 1)
1095 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
1100 dwc_otg_hcd_urb_dequeue(dwc_otg_hcd, urb->hcpriv);
1103 DWC_FREE(urb->hcpriv);
1105 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
1107 /* Higher layer software sets URB status. */
1108 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
1109 usb_hcd_giveback_urb(hcd, urb);
1111 usb_hcd_giveback_urb(hcd, urb, status);
1113 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
1114 DWC_PRINTF("Called usb_hcd_giveback_urb()\n");
1115 DWC_PRINTF(" urb->status = %d\n", urb->status);
1121 /* Frees resources in the DWC_otg controller related to a given endpoint. Also
1122 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
1123 * must already be dequeued. */
1124 static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1126 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1128 DWC_DEBUGPL(DBG_HCD,
1129 "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
1130 "endpoint=%d\n", ep->desc.bEndpointAddress,
1131 dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
1132 dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
1136 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
1137 /* Resets endpoint specific parameter values, in current version used to reset
1138 * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
1139 static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1141 dwc_irqflags_t flags;
1142 struct usb_device *udev = NULL;
1143 int epnum = usb_endpoint_num(&ep->desc);
1144 int is_out = usb_endpoint_dir_out(&ep->desc);
1145 int is_control = usb_endpoint_xfer_control(&ep->desc);
1146 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1148 struct platform_device *_dev = dwc_otg_hcd->otg_dev->os_dep.pdev;
1150 udev = to_usb_device(&_dev->dev);
1154 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
1156 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
1157 usb_settoggle(udev, epnum, is_out, 0);
1159 usb_settoggle(udev, epnum, !is_out, 0);
1162 dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
1164 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
1168 /** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
1169 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
1172 * This function is called by the USB core when an interrupt occurs */
1173 static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
1175 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1176 int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
1178 //S3C2410X_CLEAR_EINTPEND();
1180 return IRQ_RETVAL(retval);
1183 /** Creates Status Change bitmap for the root hub and root port. The bitmap is
1184 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
1185 * is the status change indicator for the single root port. Returns 1 if either
1186 * change indicator is 1, otherwise returns 0. */
1187 int hub_status_data(struct usb_hcd *hcd, char *buf)
1189 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1192 buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
1194 return (buf[0] != 0);
1197 /** Handles hub class-specific requests. */
1198 int hub_control(struct usb_hcd *hcd,
1199 u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
1203 retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
1204 typeReq, wValue, wIndex, buf, wLength);
1207 case -DWC_E_INVALID:
1215 #endif /* DWC_DEVICE_ONLY */