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 static 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);
154 /** Gets the usb_host_endpoint associated with an URB. */
155 inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
157 struct usb_device *dev = urb->dev;
158 int ep_num = usb_pipeendpoint(urb->pipe);
163 if (usb_pipein(urb->pipe))
164 return dev->ep_in[ep_num];
166 return dev->ep_out[ep_num];
169 static int _disconnect(dwc_otg_hcd_t *hcd)
171 struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
173 usb_hcd->self.is_b_host = 0;
177 static int _start(dwc_otg_hcd_t *hcd)
179 struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
181 usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
187 static int _hub_info(dwc_otg_hcd_t *hcd, void *urb_handle, uint32_t *hub_addr,
190 struct urb *urb = (struct urb *)urb_handle;
192 *hub_addr = urb->dev->tt->hub->devnum;
196 *port_addr = urb->dev->ttport;
200 static int _speed(dwc_otg_hcd_t *hcd, void *urb_handle)
202 struct urb *urb = (struct urb *)urb_handle;
203 return urb->dev->speed;
206 static int _get_b_hnp_enable(dwc_otg_hcd_t *hcd)
208 struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
209 return usb_hcd->self.b_hnp_enable;
212 static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
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++;
219 hcd_to_bus(hcd)->bandwidth_int_reqs++;
223 static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
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--;
230 hcd_to_bus(hcd)->bandwidth_int_reqs--;
235 * Sets the final status of an URB and returns it to the device driver. Any
236 * required cleanup of the URB is performed.
238 static int _complete(dwc_otg_hcd_t *hcd, void *urb_handle,
239 dwc_otg_hcd_urb_t *dwc_otg_urb, int32_t status)
241 struct urb *urb = (struct urb *)urb_handle;
243 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
244 DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
245 __func__, urb, usb_pipedevice(urb->pipe),
246 usb_pipeendpoint(urb->pipe),
247 usb_pipein(urb->pipe) ? "IN" : "OUT", status);
248 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
250 for (i = 0; i < urb->number_of_packets; i++) {
251 DWC_PRINTF(" ISO Desc %d status: %d\n",
252 i, urb->iso_frame_desc[i].status);
258 urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
259 /* Convert status value. */
261 case -DWC_E_PROTOCOL:
264 case -DWC_E_IN_PROGRESS:
265 status = -EINPROGRESS;
276 case -DWC_E_OVERFLOW:
281 DWC_PRINTF("Uknown urb status %d\n", status);
286 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
289 urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
290 for (i = 0; i < urb->number_of_packets; ++i) {
291 urb->iso_frame_desc[i].actual_length =
292 dwc_otg_hcd_urb_get_iso_desc_actual_length
294 urb->iso_frame_desc[i].status =
295 dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
299 urb->status = status;
302 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
303 (urb->actual_length < urb->transfer_buffer_length)) {
304 urb->status = -EREMOTEIO;
308 if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
309 (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
310 struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
312 free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
313 dwc_otg_hcd_get_ep_bandwidth(hcd,
320 DWC_FREE(dwc_otg_urb);
322 DWC_SPINUNLOCK(hcd->lock);
323 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
324 usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
326 usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, status);
328 DWC_SPINLOCK(hcd->lock);
333 void dwc_otg_clear_halt(struct urb *_urb)
335 struct dwc_otg_qh *_qh;
336 struct usb_host_endpoint *ep = dwc_urb_to_endpoint(_urb);
337 if ((ep) && (ep->hcpriv)) {
338 _qh = (dwc_otg_qh_t *) ep->hcpriv;
339 _qh->data_toggle = 0;
343 static struct dwc_otg_hcd_function_ops hcd_fops = {
345 .disconnect = _disconnect,
346 .hub_info = _hub_info,
348 .complete = _complete,
349 .get_b_hnp_enable = _get_b_hnp_enable,
352 static void dwc_otg_hcd_enable(struct work_struct *work)
354 dwc_otg_hcd_t *dwc_otg_hcd;
355 dwc_otg_core_if_t *core_if;
356 struct dwc_otg_platform_data *pldata;
357 dwc_otg_hcd = container_of(work, dwc_otg_hcd_t, host_enable_work.work);
358 core_if = dwc_otg_hcd->core_if;
359 pldata = core_if->otg_dev->pldata;
360 if (dwc_otg_hcd->host_enabled == dwc_otg_hcd->host_setenable) {
361 /* DWC_PRINT("%s, enable flag %d\n",
362 * __func__, dwc_otg_hcd->host_setenable); */
366 if (dwc_otg_hcd->host_setenable == 2) {/* enable -> disable */
367 if (pldata->get_status(USB_STATUS_DPDM)) {/* usb device connected */
368 dwc_otg_hcd->host_setenable = 1;
371 DWC_PRINTF("%s, disable host controller\n", __func__);
373 if (_core_if->hcd_cb && _core_if->hcd_cb->disconnect) {
374 _core_if->hcd_cb->disconnect(_core_if->hcd_cb->p);
377 pldata->soft_reset(pldata, RST_RECNT);
378 dwc_otg_disable_host_interrupts(core_if);
379 if (pldata->phy_suspend)
380 pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
382 pldata->clock_enable(pldata, 0);
383 } else if (dwc_otg_hcd->host_setenable == 1) {
384 DWC_PRINTF("%s, enable host controller\n", __func__);
385 pldata->clock_enable(pldata, 1);
386 if (pldata->phy_suspend)
387 pldata->phy_suspend(pldata, USB_PHY_ENABLED);
389 dwc_otg_core_init(core_if);
390 dwc_otg_enable_global_interrupts(core_if);
391 cil_hcd_start(core_if);
393 dwc_otg_hcd->host_enabled = dwc_otg_hcd->host_setenable;
398 static void dwc_otg_hcd_connect_detect(unsigned long pdata)
400 dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) pdata;
401 dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
403 struct dwc_otg_platform_data *pldata;
404 pldata = core_if->otg_dev->pldata;
405 local_irq_save(flags);
406 if (pldata->get_status(USB_STATUS_DPDM)) {
407 /* usb device connected */
408 dwc_otg_hcd->host_setenable = 1;
410 /* no device, suspend host */
411 if ((dwc_otg_read_hprt0(core_if) & 1) == 0)
412 dwc_otg_hcd->host_setenable = 2;
414 if ((dwc_otg_hcd->host_enabled)
415 && (dwc_otg_hcd->host_setenable != dwc_otg_hcd->host_enabled)) {
416 schedule_delayed_work(&dwc_otg_hcd->host_enable_work, 1);
418 mod_timer(&dwc_otg_hcd->connect_detect_timer, jiffies + (HZ << 1));
419 local_irq_restore(flags);
423 static void otg20_hcd_connect_detect(struct work_struct *work)
425 dwc_otg_hcd_t *dwc_otg_hcd =
426 container_of(work, dwc_otg_hcd_t, host_enable_work.work);
427 dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
428 struct dwc_otg_platform_data *pldata;
429 pldata = core_if->otg_dev->pldata;
431 if (pldata->phy_status == USB_PHY_SUSPEND) {
432 pldata->clock_enable(pldata, 1);
433 pldata->phy_suspend(pldata, USB_PHY_ENABLED);
435 dwc_otg_core_init(core_if);
436 dwc_otg_enable_global_interrupts(core_if);
437 cil_hcd_start(core_if);
441 * Initializes the HCD. This function allocates memory for and initializes the
442 * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
443 * USB bus with the core and calls the hc_driver->start() function. It returns
444 * a negative error on failure.
446 int otg20_hcd_init(struct platform_device *_dev)
448 struct usb_hcd *hcd = NULL;
449 dwc_otg_hcd_t *dwc_otg_hcd = NULL;
451 dwc_otg_device_t *otg_dev = dwc_get_device_platform_data(_dev);
455 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT\n");
458 * Allocate memory for the base HCD plus the DWC OTG HCD.
459 * Initialize the base HCD.
461 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
462 hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
465 usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev,
466 dev_name(&_dev->dev));
468 /* hcd->uses_new_polling = 1; */
469 /* hcd->poll_rh = 0; */
476 hcd->regs = otg_dev->os_dep.base;
478 /* Initialize the DWC OTG HCD. */
479 dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
483 ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
485 otg_dev->hcd = dwc_otg_hcd;
487 if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
491 otg_dev->hcd->otg_dev = otg_dev;
492 hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
494 /* #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33) */
495 /* don't support for LM(with 2.6.20.1 kernel) */
496 hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
497 /* Don't support SG list at this point */
498 hcd->self.sg_tablesize = 0;
501 /* LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0) */
502 /* Do not to do HNP polling if not capable */
503 /* if (otg_dev->core_if->otg_ver) */
504 /* hcd->self.is_hnp_cap = dwc_otg_get_hnpcapable(otg_dev->core_if); */
507 * Finish generic HCD initialization and start the HCD. This function
508 * allocates the DMA buffer pool, registers the USB bus, requests the
509 * IRQ line, and calls hcd_start method.
511 irq = platform_get_irq(_dev, 0);
512 retval = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_DISABLED);
517 dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
518 dwc_otg_hcd->host_enabled = 1;
519 if (dwc_otg_is_host_mode(otg_dev->core_if) ||
520 (otg_dev->core_if->usb_mode == USB_MODE_FORCE_HOST)) {
521 INIT_DELAYED_WORK(&dwc_otg_hcd->host_enable_work,
522 otg20_hcd_connect_detect);
523 schedule_delayed_work(&dwc_otg_hcd->host_enable_work, 0);
534 * Initializes the HCD. This function allocates memory for and initializes the
535 * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
536 * USB bus with the core and calls the hc_driver->start() function. It returns
537 * a negative error on failure.
539 int host20_hcd_init(struct platform_device *_dev)
541 struct usb_hcd *hcd = NULL;
542 dwc_otg_hcd_t *dwc_otg_hcd = NULL;
544 dwc_otg_device_t *otg_dev = dwc_get_device_platform_data(_dev);
547 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT\n");
550 * Allocate memory for the base HCD plus the DWC OTG HCD.
551 * Initialize the base HCD.
553 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
554 hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
557 usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev,
558 dev_name(&_dev->dev));
560 /* hcd->uses_new_polling = 1; */
561 /* hcd->poll_rh = 0; */
568 hcd->regs = otg_dev->os_dep.base;
570 /* Initialize the DWC OTG HCD. */
571 dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
575 ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
577 otg_dev->hcd = dwc_otg_hcd;
579 if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
583 otg_dev->hcd->otg_dev = otg_dev;
584 hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
586 /* #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33) */
587 /* don't support for LM(with 2.6.20.1 kernel) */
588 hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
589 /* Don't support SG list at this point */
590 hcd->self.sg_tablesize = 0;
593 /* LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0) */
594 /* Do not to do HNP polling if not capable */
595 /* if (otg_dev->core_if->otg_ver) */
596 /* hcd->self.is_hnp_cap = dwc_otg_get_hnpcapable(otg_dev->core_if);*/
599 * Finish generic HCD initialization and start the HCD. This function
600 * allocates the DMA buffer pool, registers the USB bus, requests the
601 * IRQ line, and calls hcd_start method.
603 irq = platform_get_irq(_dev, 0);
604 retval = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_DISABLED);
609 dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
611 dwc_otg_hcd->host_enabled = 2;
612 dwc_otg_hcd->host_setenable = 2;
613 dwc_otg_hcd->connect_detect_timer.function = dwc_otg_hcd_connect_detect;
614 dwc_otg_hcd->connect_detect_timer.data = (unsigned long)(dwc_otg_hcd);
615 init_timer(&dwc_otg_hcd->connect_detect_timer);
616 mod_timer(&dwc_otg_hcd->connect_detect_timer, jiffies + (HZ << 1));
618 INIT_DELAYED_WORK(&dwc_otg_hcd->host_enable_work, dwc_otg_hcd_enable);
629 * Frees memory and resources associated with the HCD and deregisters the bus.
631 void hcd_remove(struct platform_device *_dev)
634 dwc_otg_device_t *otg_dev = dwc_get_device_platform_data(_dev);
635 dwc_otg_hcd_t *dwc_otg_hcd;
638 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE\n");
641 DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
645 dwc_otg_hcd = otg_dev->hcd;
648 DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
652 hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
656 "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
661 dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
662 dwc_otg_hcd_remove(dwc_otg_hcd);
666 /* =========================================================================
667 * Linux HC Driver Functions
668 * ========================================================================= */
670 /** Initializes the DWC_otg controller and its root hub and prepares it for host
671 * mode operation. Activates the root port. Returns 0 on success and a negative
672 * error code on failure. */
673 int hcd_start(struct usb_hcd *hcd)
675 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
678 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
679 bus = hcd_to_bus(hcd);
681 hcd->state = HC_STATE_RUNNING;
682 if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
683 if (dwc_otg_hcd->core_if->otg_ver)
684 dwc_otg_hcd->core_if->op_state = B_PERIPHERAL;
688 /* Initialize and connect root hub if one is not already attached */
690 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
691 /* Inform the HUB driver to resume. */
692 usb_hcd_resume_root_hub(hcd);
699 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
702 void hcd_stop(struct usb_hcd *hcd)
704 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
706 dwc_otg_hcd_stop(dwc_otg_hcd);
709 static int dwc_otg_hcd_suspend(struct usb_hcd *hcd)
711 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
712 dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
714 pcgcctl_data_t pcgcctl;
715 struct dwc_otg_platform_data *pldata;
716 pldata = core_if->otg_dev->pldata;
718 if (core_if->op_state == B_PERIPHERAL) {
719 DWC_PRINTF("%s, usb device mode\n", __func__);
723 if (!(dwc_otg_hcd->host_enabled & 1))
726 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
727 #ifdef CONFIG_PM_RUNTIME
728 if ((!hprt0.b.prtena) && (!hprt0.b.prtpwr))
731 DWC_PRINTF("%s suspend, HPRT0:0x%x\n", hcd->self.bus_name, hprt0.d32);
733 if (hprt0.b.prtconnsts) { /* usb device connected */
734 if (!hprt0.b.prtsusp) {
737 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
740 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
742 if (!hprt0.b.prtsusp) {
745 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
749 pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
750 /* Partial Power-Down mode not enable */
751 pcgcctl.b.pwrclmp = 0;
752 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
755 /* pcgcctl.b.rstpdwnmodule = 1; */
756 pcgcctl.b.stoppclk = 1; /* stop phy clk */
757 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
758 } else {/* no device connect */
759 if (!pldata->get_status(USB_REMOTE_WAKEUP)) {
760 if (pldata->phy_suspend)
761 pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
763 if (pldata->clock_enable)
764 pldata->clock_enable(pldata, 0);
771 static int dwc_otg_hcd_resume(struct usb_hcd *hcd)
773 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
774 dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
776 pcgcctl_data_t pcgcctl;
777 gintmsk_data_t gintmsk;
778 struct dwc_otg_platform_data *pldata;
779 pldata = core_if->otg_dev->pldata;
781 if (core_if->op_state == B_PERIPHERAL) {
782 DWC_PRINTF("%s, usb device mode\n", __func__);
785 /* #ifdef CONFIG_PM_RUNTIME */
786 if (!(dwc_otg_hcd->host_enabled & 1))
790 if (!pldata->get_status(USB_REMOTE_WAKEUP)) {
791 if (pldata->clock_enable)
792 pldata->clock_enable(pldata, 1);
795 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
796 #ifdef CONFIG_PM_RUNTIME
797 /* USB HCD already resumed by remote wakeup, return now */
798 if ((!hprt0.b.prtsusp) && (hprt0.b.prtena))
803 pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);;
804 pcgcctl.b.stoppclk = 0; /* restart phy clk */
805 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
807 pcgcctl.b.pwrclmp = 0; /* power clamp */
808 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
811 gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
812 gintmsk.b.portintr = 0;
813 DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
815 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
817 #ifdef CONFIG_PM_RUNTIME
818 if ((!hprt0.b.prtena) && (!hprt0.b.prtpwr))
821 DWC_PRINTF("%s resume, HPRT0:0x%x\n", hcd->self.bus_name, hprt0.d32);
823 if (hprt0.b.prtconnsts) {
824 /* hprt0.d32 = dwc_read_reg32(core_if->host_if->hprt0); */
825 /* DWC_PRINT("%s, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32); */
829 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
831 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
832 /* DWC_PRINT("%s, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32); */
837 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
841 hprt0.b.prtconndet = 1;
842 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
844 /* hprt0.d32 = dwc_read_reg32(core_if->host_if->hprt0); */
845 /* DWC_PRINT("%s, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32); */
849 if (!pldata->get_status(USB_REMOTE_WAKEUP)) {
850 if (pldata->phy_suspend)
851 pldata->phy_suspend(pldata, USB_PHY_ENABLED);
854 gintmsk.b.portintr = 1;
855 DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
861 int hcd_suspend(struct usb_hcd *hcd)
863 /* dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd); */
865 DWC_DEBUGPL(DBG_HCD, "HCD SUSPEND\n");
867 dwc_otg_hcd_suspend(hcd);
873 int hcd_resume(struct usb_hcd *hcd)
875 /* dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd); */
877 DWC_DEBUGPL(DBG_HCD, "HCD RESUME\n");
879 dwc_otg_hcd_resume(hcd);
884 /** Returns the current frame number. */
885 static int get_frame_number(struct usb_hcd *hcd)
887 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
889 return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
893 static void dump_urb_info(struct urb *urb, char *fn_name)
895 DWC_PRINTF("%s, urb %p\n", fn_name, urb);
896 DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
897 DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
898 (usb_pipein(urb->pipe) ? "IN" : "OUT"));
899 DWC_PRINTF(" Endpoint type: %s\n", ({
901 switch (usb_pipetype(urb->pipe)) {
903 pipetype = "CONTROL";
909 pipetype = "INTERRUPT";
911 case PIPE_ISOCHRONOUS:
912 pipetype = "ISOCHRONOUS";
915 pipetype = "UNKNOWN";
919 DWC_PRINTF(" Speed: %s\n", ({
921 switch (urb->dev->speed) {
936 DWC_PRINTF(" Max packet size: %d\n",
937 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
938 DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
939 DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
940 urb->transfer_buffer, (void *)urb->transfer_dma);
941 DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
942 urb->setup_packet, (void *)urb->setup_dma);
943 DWC_PRINTF(" Interval: %d\n", urb->interval);
944 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
946 for (i = 0; i < urb->number_of_packets; i++) {
947 DWC_PRINTF(" ISO Desc %d:\n", i);
948 DWC_PRINTF(" offset: %d, length %d\n",
949 urb->iso_frame_desc[i].offset,
950 urb->iso_frame_desc[i].length);
957 /** Starts processing a USB transfer request specified by a USB Request Block
958 * (URB). mem_flags indicates the type of memory allocation to use while
959 * processing this URB. */
960 static int urb_enqueue(struct usb_hcd *hcd,
961 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
962 struct usb_host_endpoint *ep,
964 struct urb *urb, gfp_t mem_flags)
967 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
968 struct usb_host_endpoint *ep = urb->ep;
970 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
971 dwc_otg_hcd_urb_t *dwc_otg_urb;
973 int alloc_bandwidth = 0;
979 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
980 dump_urb_info(urb, "urb_enqueue");
984 if (unlikely(atomic_read(&urb->use_count) > 1)) {
986 printk("%s urb %p already in queue, qtd %p, use_count %d\n",
987 __func__, urb, urb->hcpriv,
988 atomic_read(&urb->use_count));
992 if (unlikely(atomic_read(&urb->reject))) {
995 "%s urb %p submissions will fail,reject %d,count %d\n",
996 __func__, urb, atomic_read(&urb->reject),
997 atomic_read(&urb->use_count));
1001 if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
1002 || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
1003 if (!dwc_otg_hcd_is_bandwidth_allocated
1004 (dwc_otg_hcd, &ep->hcpriv)) {
1005 alloc_bandwidth = 1;
1009 switch (usb_pipetype(urb->pipe)) {
1011 ep_type = USB_ENDPOINT_XFER_CONTROL;
1013 case PIPE_ISOCHRONOUS:
1014 ep_type = USB_ENDPOINT_XFER_ISOC;
1017 ep_type = USB_ENDPOINT_XFER_BULK;
1019 case PIPE_INTERRUPT:
1020 ep_type = USB_ENDPOINT_XFER_INT;
1023 DWC_WARN("Wrong ep type\n");
1026 dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
1027 urb->number_of_packets,
1028 mem_flags == GFP_ATOMIC ? 1 : 0);
1030 dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
1031 usb_pipeendpoint(urb->pipe), ep_type,
1032 usb_pipein(urb->pipe),
1033 usb_maxpacket(urb->dev, urb->pipe,
1034 !(usb_pipein(urb->pipe))));
1037 if ((uint32_t) urb->transfer_buffer & 3) {
1039 ("%s urb->transfer_buffer address not align to 4-byte 0x%x\n",
1040 __func__, (uint32_t) urb->transfer_buffer);
1044 buf = urb->transfer_buffer;
1046 if (hcd->self.uses_dma) {
1048 * Calculate virtual address from physical address,
1049 * because some class driver may not fill transfer_buffer.
1050 * In Buffer DMA mode virual address is used,
1051 * when handling non DWORD aligned buffers.
1053 buf = phys_to_virt(urb->transfer_dma);
1056 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
1057 flags |= URB_GIVEBACK_ASAP;
1058 if (urb->transfer_flags & URB_ZERO_PACKET)
1059 flags |= URB_SEND_ZERO_PACKET;
1061 dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
1063 urb->transfer_buffer_length,
1065 urb->setup_dma, flags, urb->interval);
1067 for (i = 0; i < urb->number_of_packets; ++i) {
1068 dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
1069 urb->iso_frame_desc[i].
1071 urb->iso_frame_desc[i].
1075 urb->hcpriv = dwc_otg_urb;
1076 retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb, &ep->hcpriv,
1077 mem_flags == GFP_ATOMIC ? 1 : 0);
1079 if (alloc_bandwidth) {
1080 allocate_bus_bandwidth(hcd,
1081 dwc_otg_hcd_get_ep_bandwidth
1082 (dwc_otg_hcd, ep->hcpriv), urb);
1085 if (retval == -DWC_E_NO_DEVICE) {
1093 /** Aborts/cancels a USB transfer request. Always returns 0 to indicate
1095 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
1096 static int urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1098 static int urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1101 dwc_irqflags_t flags;
1102 dwc_otg_hcd_t *dwc_otg_hcd;
1103 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
1105 dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1108 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
1109 dump_urb_info(urb, "urb_dequeue");
1113 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
1115 if (((uint32_t) urb & 0xf0000000) == 0) {
1116 DWC_PRINTF("%s error: urb is %p!!!\n", __func__, urb);
1120 if (((uint32_t) urb->hcpriv & 0xf0000000) == 0) {
1121 DWC_PRINTF("%s error: urb->hcpriv %p urb %p, count %d!!!\n",
1122 __func__, urb->hcpriv, urb,
1123 atomic_read(&urb->use_count));
1124 if ((atomic_read(&urb->use_count)) == 1)
1127 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
1132 dwc_otg_hcd_urb_dequeue(dwc_otg_hcd, urb->hcpriv);
1135 DWC_FREE(urb->hcpriv);
1137 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
1139 /* Higher layer software sets URB status. */
1140 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
1141 usb_hcd_giveback_urb(hcd, urb);
1143 usb_hcd_giveback_urb(hcd, urb, status);
1145 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
1146 DWC_PRINTF("Called usb_hcd_giveback_urb()\n");
1147 DWC_PRINTF(" urb->status = %d\n", urb->status);
1153 /* Frees resources in the DWC_otg controller related to a given endpoint. Also
1154 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
1155 * must already be dequeued. */
1156 static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1158 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1160 DWC_DEBUGPL(DBG_HCD,
1161 "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
1162 "endpoint=%d\n", ep->desc.bEndpointAddress,
1163 dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
1164 dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
1168 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
1169 /* Resets endpoint specific parameter values, in current version used to reset
1170 * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
1171 static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1173 dwc_irqflags_t flags;
1174 struct usb_device *udev = NULL;
1175 int epnum = usb_endpoint_num(&ep->desc);
1176 int is_out = usb_endpoint_dir_out(&ep->desc);
1177 int is_control = usb_endpoint_xfer_control(&ep->desc);
1178 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1180 struct platform_device *_dev = dwc_otg_hcd->otg_dev->os_dep.pdev;
1182 udev = to_usb_device(&_dev->dev);
1186 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n",
1189 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
1190 usb_settoggle(udev, epnum, is_out, 0);
1192 usb_settoggle(udev, epnum, !is_out, 0);
1195 dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
1197 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
1201 /** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
1202 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
1205 * This function is called by the USB core when an interrupt occurs */
1206 static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
1208 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1209 int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
1211 /* S3C2410X_CLEAR_EINTPEND(); */
1213 return IRQ_RETVAL(retval);
1216 /** Creates Status Change bitmap for the root hub and root port. The bitmap is
1217 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
1218 * is the status change indicator for the single root port. Returns 1 if either
1219 * change indicator is 1, otherwise returns 0. */
1220 int hub_status_data(struct usb_hcd *hcd, char *buf)
1222 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1225 buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
1227 return (buf[0] != 0);
1230 /** Handles hub class-specific requests. */
1231 int hub_control(struct usb_hcd *hcd,
1232 u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
1236 retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
1237 typeReq, wValue, wIndex, buf, wLength);
1240 case -DWC_E_INVALID:
1248 #endif /* DWC_DEVICE_ONLY */