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;
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) {
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);
260 urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
261 /* Convert status value. */
263 case -DWC_E_PROTOCOL:
266 case -DWC_E_IN_PROGRESS:
267 status = -EINPROGRESS;
278 case -DWC_E_OVERFLOW:
283 DWC_PRINTF("Uknown urb status %d\n", status);
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,
295 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
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
303 urb->iso_frame_desc[i].status =
304 dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
308 urb->status = status;
311 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
312 (urb->actual_length < urb->transfer_buffer_length)) {
313 urb->status = -EREMOTEIO;
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);
321 free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
322 dwc_otg_hcd_get_ep_bandwidth(hcd,
329 DWC_FREE(dwc_otg_urb);
331 usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
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);
337 usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, status);
339 DWC_SPINLOCK(hcd->lock);
344 void dwc_otg_clear_halt(struct urb *_urb)
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;
354 static struct dwc_otg_hcd_function_ops hcd_fops = {
356 .disconnect = _disconnect,
357 .hub_info = _hub_info,
359 .complete = _complete,
360 .get_b_hnp_enable = _get_b_hnp_enable,
363 static void dwc_otg_hcd_enable(struct work_struct *work)
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); */
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;
382 DWC_PRINTF("%s, disable host controller\n", __func__);
384 if (_core_if->hcd_cb && _core_if->hcd_cb->disconnect) {
385 _core_if->hcd_cb->disconnect(_core_if->hcd_cb->p);
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);
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);
400 dwc_otg_core_init(core_if);
401 dwc_otg_enable_global_interrupts(core_if);
402 cil_hcd_start(core_if);
404 dwc_otg_hcd->host_enabled = dwc_otg_hcd->host_setenable;
409 static void dwc_otg_hcd_connect_detect(unsigned long pdata)
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;
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;
421 /* no device, suspend host */
422 if ((dwc_otg_read_hprt0(core_if) & 1) == 0)
423 dwc_otg_hcd->host_setenable = 2;
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);
429 mod_timer(&dwc_otg_hcd->connect_detect_timer, jiffies + (HZ << 1));
430 local_irq_restore(flags);
434 static void otg20_hcd_connect_detect(struct work_struct *work)
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;
442 if (pldata->phy_status == USB_PHY_SUSPEND) {
443 pldata->clock_enable(pldata, 1);
444 pldata->phy_suspend(pldata, USB_PHY_ENABLED);
446 dwc_otg_core_init(core_if);
447 dwc_otg_enable_global_interrupts(core_if);
448 cil_hcd_start(core_if);
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.
457 int otg20_hcd_init(struct platform_device *_dev)
459 struct usb_hcd *hcd = NULL;
460 dwc_otg_hcd_t *dwc_otg_hcd = NULL;
462 dwc_otg_device_t *otg_dev = dwc_get_device_platform_data(_dev);
465 static u64 usb_dmamask = 0xffffffffUL;
467 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT\n");
469 /* Set device flags indicating whether the HCD supports DMA. */
470 if (dwc_otg_is_dma_enable(otg_dev->core_if)) {
472 _dev->dev.dma_mask = &usb_dmamask;
473 _dev->dev.coherent_dma_mask = ~0;
476 _dev->dev.dma_mask = (void *)0;
477 _dev->dev.coherent_dma_mask = 0;
481 * Allocate memory for the base HCD plus the DWC OTG HCD.
482 * Initialize the base HCD.
484 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
485 hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
488 usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev,
489 dev_name(&_dev->dev));
491 /* hcd->uses_new_polling = 1; */
492 /* hcd->poll_rh = 0; */
499 hcd->regs = otg_dev->os_dep.base;
501 /* Initialize the DWC OTG HCD. */
502 dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
506 ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
508 otg_dev->hcd = dwc_otg_hcd;
510 if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
514 otg_dev->hcd->otg_dev = otg_dev;
515 hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
517 /* #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33) */
518 /* don't support for LM(with 2.6.20.1 kernel) */
519 hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
520 /* Don't support SG list at this point */
521 hcd->self.sg_tablesize = 0;
524 /* LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0) */
525 /* Do not to do HNP polling if not capable */
526 /* if (otg_dev->core_if->otg_ver) */
527 /* hcd->self.is_hnp_cap = dwc_otg_get_hnpcapable(otg_dev->core_if); */
530 * Finish generic HCD initialization and start the HCD. This function
531 * allocates the DMA buffer pool, registers the USB bus, requests the
532 * IRQ line, and calls hcd_start method.
534 irq = platform_get_irq(_dev, 0);
535 retval = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_DISABLED);
540 dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
541 dwc_otg_hcd->host_enabled = 1;
542 if (dwc_otg_is_host_mode(otg_dev->core_if) ||
543 (otg_dev->core_if->usb_mode == USB_MODE_FORCE_HOST)) {
544 INIT_DELAYED_WORK(&dwc_otg_hcd->host_enable_work,
545 otg20_hcd_connect_detect);
546 schedule_delayed_work(&dwc_otg_hcd->host_enable_work, 0);
557 * Initializes the HCD. This function allocates memory for and initializes the
558 * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
559 * USB bus with the core and calls the hc_driver->start() function. It returns
560 * a negative error on failure.
562 int host20_hcd_init(struct platform_device *_dev)
564 struct usb_hcd *hcd = NULL;
565 dwc_otg_hcd_t *dwc_otg_hcd = NULL;
567 dwc_otg_device_t *otg_dev = dwc_get_device_platform_data(_dev);
570 static u64 usb_dmamask = 0xffffffffUL;
571 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT\n");
573 /* Set device flags indicating whether the HCD supports DMA. */
574 if (dwc_otg_is_dma_enable(otg_dev->core_if)) {
576 _dev->dev.dma_mask = &usb_dmamask;
577 _dev->dev.coherent_dma_mask = ~0;
580 _dev->dev.dma_mask = (void *)0;
581 _dev->dev.coherent_dma_mask = 0;
585 * Allocate memory for the base HCD plus the DWC OTG HCD.
586 * Initialize the base HCD.
588 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
589 hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
592 usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev,
593 dev_name(&_dev->dev));
595 /* hcd->uses_new_polling = 1; */
596 /* hcd->poll_rh = 0; */
603 hcd->regs = otg_dev->os_dep.base;
605 /* Initialize the DWC OTG HCD. */
606 dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
610 ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
612 otg_dev->hcd = dwc_otg_hcd;
614 if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
618 otg_dev->hcd->otg_dev = otg_dev;
619 hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
621 /* #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33) */
622 /* don't support for LM(with 2.6.20.1 kernel) */
623 hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
624 /* Don't support SG list at this point */
625 hcd->self.sg_tablesize = 0;
628 /* LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0) */
629 /* Do not to do HNP polling if not capable */
630 /* if (otg_dev->core_if->otg_ver) */
631 /* hcd->self.is_hnp_cap = dwc_otg_get_hnpcapable(otg_dev->core_if);*/
634 * Finish generic HCD initialization and start the HCD. This function
635 * allocates the DMA buffer pool, registers the USB bus, requests the
636 * IRQ line, and calls hcd_start method.
638 irq = platform_get_irq(_dev, 0);
639 retval = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_DISABLED);
644 dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
646 dwc_otg_hcd->host_enabled = 2;
647 dwc_otg_hcd->host_setenable = 2;
648 dwc_otg_hcd->connect_detect_timer.function = dwc_otg_hcd_connect_detect;
649 dwc_otg_hcd->connect_detect_timer.data = (unsigned long)(dwc_otg_hcd);
650 init_timer(&dwc_otg_hcd->connect_detect_timer);
651 mod_timer(&dwc_otg_hcd->connect_detect_timer, jiffies + (HZ << 1));
653 INIT_DELAYED_WORK(&dwc_otg_hcd->host_enable_work, dwc_otg_hcd_enable);
664 * Frees memory and resources associated with the HCD and deregisters the bus.
666 void hcd_remove(struct platform_device *_dev)
669 dwc_otg_device_t *otg_dev = dwc_get_device_platform_data(_dev);
670 dwc_otg_hcd_t *dwc_otg_hcd;
673 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE\n");
676 DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
680 dwc_otg_hcd = otg_dev->hcd;
683 DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
687 hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
691 "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
696 dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
697 dwc_otg_hcd_remove(dwc_otg_hcd);
701 /* =========================================================================
702 * Linux HC Driver Functions
703 * ========================================================================= */
705 /** Initializes the DWC_otg controller and its root hub and prepares it for host
706 * mode operation. Activates the root port. Returns 0 on success and a negative
707 * error code on failure. */
708 int hcd_start(struct usb_hcd *hcd)
710 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
713 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
714 bus = hcd_to_bus(hcd);
716 hcd->state = HC_STATE_RUNNING;
717 if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
718 if (dwc_otg_hcd->core_if->otg_ver)
719 dwc_otg_hcd->core_if->op_state = B_PERIPHERAL;
723 /* Initialize and connect root hub if one is not already attached */
725 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
726 /* Inform the HUB driver to resume. */
727 usb_hcd_resume_root_hub(hcd);
734 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
737 void hcd_stop(struct usb_hcd *hcd)
739 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
741 dwc_otg_hcd_stop(dwc_otg_hcd);
744 static int dwc_otg_hcd_suspend(struct usb_hcd *hcd)
746 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
747 dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
749 pcgcctl_data_t pcgcctl;
750 struct dwc_otg_platform_data *pldata;
751 pldata = core_if->otg_dev->pldata;
753 if (core_if->op_state == B_PERIPHERAL) {
754 DWC_PRINTF("%s, usb device mode\n", __func__);
758 if (!(dwc_otg_hcd->host_enabled & 1))
761 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
762 #ifdef CONFIG_PM_RUNTIME
763 if ((!hprt0.b.prtena) && (!hprt0.b.prtpwr))
766 DWC_PRINTF("%s suspend, HPRT0:0x%x\n", hcd->self.bus_name, hprt0.d32);
768 if (hprt0.b.prtconnsts) { /* usb device connected */
769 if (!hprt0.b.prtsusp) {
772 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
775 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
777 if (!hprt0.b.prtsusp) {
780 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
784 pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
785 /* Partial Power-Down mode not enable */
786 pcgcctl.b.pwrclmp = 0;
787 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
790 /* pcgcctl.b.rstpdwnmodule = 1; */
791 pcgcctl.b.stoppclk = 1; /* stop phy clk */
792 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
793 } else {/* no device connect */
794 if (!pldata->get_status(USB_REMOTE_WAKEUP)) {
795 if (pldata->phy_suspend)
796 pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
798 if (pldata->clock_enable)
799 pldata->clock_enable(pldata, 0);
806 static int dwc_otg_hcd_resume(struct usb_hcd *hcd)
808 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
809 dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
811 pcgcctl_data_t pcgcctl;
812 gintmsk_data_t gintmsk;
813 struct dwc_otg_platform_data *pldata;
814 pldata = core_if->otg_dev->pldata;
816 if (core_if->op_state == B_PERIPHERAL) {
817 DWC_PRINTF("%s, usb device mode\n", __func__);
820 /* #ifdef CONFIG_PM_RUNTIME */
821 if (!(dwc_otg_hcd->host_enabled & 1))
825 if (!pldata->get_status(USB_REMOTE_WAKEUP)) {
826 if (pldata->clock_enable)
827 pldata->clock_enable(pldata, 1);
830 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
831 #ifdef CONFIG_PM_RUNTIME
832 /* USB HCD already resumed by remote wakeup, return now */
833 if ((!hprt0.b.prtsusp) && (hprt0.b.prtena))
838 pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);;
839 pcgcctl.b.stoppclk = 0; /* restart phy clk */
840 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
842 pcgcctl.b.pwrclmp = 0; /* power clamp */
843 DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
846 gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
847 gintmsk.b.portintr = 0;
848 DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
850 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
852 #ifdef CONFIG_PM_RUNTIME
853 if ((!hprt0.b.prtena) && (!hprt0.b.prtpwr))
856 DWC_PRINTF("%s resume, HPRT0:0x%x\n", hcd->self.bus_name, hprt0.d32);
858 if (hprt0.b.prtconnsts) {
859 /* hprt0.d32 = dwc_read_reg32(core_if->host_if->hprt0); */
860 /* DWC_PRINT("%s, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32); */
864 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
866 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
867 /* DWC_PRINT("%s, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32); */
872 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
876 hprt0.b.prtconndet = 1;
877 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
879 /* hprt0.d32 = dwc_read_reg32(core_if->host_if->hprt0); */
880 /* DWC_PRINT("%s, HPRT0:0x%x\n",hcd->self.bus_name,hprt0.d32); */
884 if (!pldata->get_status(USB_REMOTE_WAKEUP)) {
885 if (pldata->phy_suspend)
886 pldata->phy_suspend(pldata, USB_PHY_ENABLED);
889 gintmsk.b.portintr = 1;
890 DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
896 int hcd_suspend(struct usb_hcd *hcd)
898 /* dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd); */
900 DWC_DEBUGPL(DBG_HCD, "HCD SUSPEND\n");
902 dwc_otg_hcd_suspend(hcd);
908 int hcd_resume(struct usb_hcd *hcd)
910 /* dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd); */
912 DWC_DEBUGPL(DBG_HCD, "HCD RESUME\n");
914 dwc_otg_hcd_resume(hcd);
919 /** Returns the current frame number. */
920 static int get_frame_number(struct usb_hcd *hcd)
922 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
924 return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
928 static void dump_urb_info(struct urb *urb, char *fn_name)
930 DWC_PRINTF("%s, urb %p\n", fn_name, urb);
931 DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
932 DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
933 (usb_pipein(urb->pipe) ? "IN" : "OUT"));
934 DWC_PRINTF(" Endpoint type: %s\n", ({
936 switch (usb_pipetype(urb->pipe)) {
938 pipetype = "CONTROL";
944 pipetype = "INTERRUPT";
946 case PIPE_ISOCHRONOUS:
947 pipetype = "ISOCHRONOUS";
950 pipetype = "UNKNOWN";
954 DWC_PRINTF(" Speed: %s\n", ({
956 switch (urb->dev->speed) {
971 DWC_PRINTF(" Max packet size: %d\n",
972 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
973 DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
974 DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
975 urb->transfer_buffer, (void *)urb->transfer_dma);
976 DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
977 urb->setup_packet, (void *)urb->setup_dma);
978 DWC_PRINTF(" Interval: %d\n", urb->interval);
979 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
981 for (i = 0; i < urb->number_of_packets; i++) {
982 DWC_PRINTF(" ISO Desc %d:\n", i);
983 DWC_PRINTF(" offset: %d, length %d\n",
984 urb->iso_frame_desc[i].offset,
985 urb->iso_frame_desc[i].length);
992 /** Starts processing a USB transfer request specified by a USB Request Block
993 * (URB). mem_flags indicates the type of memory allocation to use while
994 * processing this URB. */
995 static int urb_enqueue(struct usb_hcd *hcd,
996 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
997 struct usb_host_endpoint *ep,
999 struct urb *urb, gfp_t mem_flags)
1002 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1003 struct usb_host_endpoint *ep = urb->ep;
1005 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1006 dwc_otg_hcd_urb_t *dwc_otg_urb;
1008 int alloc_bandwidth = 0;
1009 uint8_t ep_type = 0;
1011 dwc_irqflags_t irq_flags;
1015 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
1016 dump_urb_info(urb, "urb_enqueue");
1020 if (unlikely(atomic_read(&urb->use_count) > 1)) {
1022 printk("%s urb %p already in queue, qtd %p, use_count %d\n",
1023 __func__, urb, urb->hcpriv,
1024 atomic_read(&urb->use_count));
1028 if (unlikely(atomic_read(&urb->reject))) {
1030 DWC_DEBUGPL(DBG_HCD,
1031 "%s urb %p submissions will fail,reject %d,count %d\n",
1032 __func__, urb, atomic_read(&urb->reject),
1033 atomic_read(&urb->use_count));
1037 if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
1038 || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
1039 if (!dwc_otg_hcd_is_bandwidth_allocated
1040 (dwc_otg_hcd, &ep->hcpriv)) {
1041 alloc_bandwidth = 1;
1045 switch (usb_pipetype(urb->pipe)) {
1047 ep_type = USB_ENDPOINT_XFER_CONTROL;
1049 case PIPE_ISOCHRONOUS:
1050 ep_type = USB_ENDPOINT_XFER_ISOC;
1053 ep_type = USB_ENDPOINT_XFER_BULK;
1055 case PIPE_INTERRUPT:
1056 ep_type = USB_ENDPOINT_XFER_INT;
1059 DWC_WARN("Wrong ep type\n");
1062 dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
1063 urb->number_of_packets,
1064 mem_flags == GFP_ATOMIC ? 1 : 0);
1066 dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
1067 usb_pipeendpoint(urb->pipe), ep_type,
1068 usb_pipein(urb->pipe),
1069 usb_maxpacket(urb->dev, urb->pipe,
1070 !(usb_pipein(urb->pipe))));
1073 if ((uint32_t) urb->transfer_buffer & 3) {
1075 ("%s urb->transfer_buffer address not align to 4-byte 0x%x\n",
1076 __func__, (uint32_t) urb->transfer_buffer);
1080 buf = urb->transfer_buffer;
1082 if (hcd->self.uses_dma) {
1084 * Calculate virtual address from physical address,
1085 * because some class driver may not fill transfer_buffer.
1086 * In Buffer DMA mode virual address is used,
1087 * when handling non DWORD aligned buffers.
1089 buf = phys_to_virt(urb->transfer_dma);
1092 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
1093 flags |= URB_GIVEBACK_ASAP;
1094 if (urb->transfer_flags & URB_ZERO_PACKET)
1095 flags |= URB_SEND_ZERO_PACKET;
1097 dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
1099 urb->transfer_buffer_length,
1101 urb->setup_dma, flags, urb->interval);
1103 for (i = 0; i < urb->number_of_packets; ++i) {
1104 dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
1105 urb->iso_frame_desc[i].
1107 urb->iso_frame_desc[i].
1111 urb->hcpriv = dwc_otg_urb;
1113 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irq_flags);
1114 retval = usb_hcd_link_urb_to_ep(hcd, urb);
1115 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irq_flags);
1119 retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb, &ep->hcpriv,
1120 mem_flags == GFP_ATOMIC ? 1 : 0);
1122 if (retval == -DWC_E_NO_DEVICE)
1127 if (alloc_bandwidth) {
1128 allocate_bus_bandwidth(hcd, dwc_otg_hcd_get_ep_bandwidth
1129 (dwc_otg_hcd, ep->hcpriv), urb);
1135 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irq_flags);
1136 dwc_otg_urb->priv = NULL;
1137 usb_hcd_unlink_urb_from_ep(hcd, urb);
1138 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irq_flags);
1141 DWC_FREE(dwc_otg_urb);
1145 /** Aborts/cancels a USB transfer request. Always returns 0 to indicate
1147 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
1148 static int urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1150 static int urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1154 dwc_irqflags_t flags;
1155 dwc_otg_hcd_t *dwc_otg_hcd;
1156 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
1158 dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1161 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
1162 dump_urb_info(urb, "urb_dequeue");
1167 DWC_PRINTF("%s error: urb is %p!!!\n", __func__, urb);
1171 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
1172 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1174 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
1178 dwc_otg_hcd_urb_dequeue(dwc_otg_hcd, urb->hcpriv);
1179 DWC_FREE(urb->hcpriv);
1181 usb_hcd_unlink_urb_from_ep(hcd, urb);
1182 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
1184 /* Higher layer software sets URB status. */
1185 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
1186 usb_hcd_giveback_urb(hcd, urb);
1188 usb_hcd_giveback_urb(hcd, urb, status);
1190 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
1191 DWC_PRINTF("Called usb_hcd_giveback_urb()\n");
1192 DWC_PRINTF(" urb->status = %d\n", urb->status);
1198 /* Frees resources in the DWC_otg controller related to a given endpoint. Also
1199 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
1200 * must already be dequeued. */
1201 static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1203 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1205 DWC_DEBUGPL(DBG_HCD,
1206 "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
1207 "endpoint=%d\n", ep->desc.bEndpointAddress,
1208 dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
1209 dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
1213 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
1214 /* Resets endpoint specific parameter values, in current version used to reset
1215 * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
1216 static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1218 dwc_irqflags_t flags;
1219 struct usb_device *udev = NULL;
1220 int epnum = usb_endpoint_num(&ep->desc);
1221 int is_out = usb_endpoint_dir_out(&ep->desc);
1222 int is_control = usb_endpoint_xfer_control(&ep->desc);
1223 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1225 struct platform_device *_dev = dwc_otg_hcd->otg_dev->os_dep.pdev;
1227 udev = to_usb_device(&_dev->dev);
1231 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n",
1234 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
1235 usb_settoggle(udev, epnum, is_out, 0);
1237 usb_settoggle(udev, epnum, !is_out, 0);
1240 dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
1242 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
1246 /** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
1247 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
1250 * This function is called by the USB core when an interrupt occurs */
1251 static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
1253 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1254 int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
1256 /* S3C2410X_CLEAR_EINTPEND(); */
1258 return IRQ_RETVAL(retval);
1261 /** Creates Status Change bitmap for the root hub and root port. The bitmap is
1262 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
1263 * is the status change indicator for the single root port. Returns 1 if either
1264 * change indicator is 1, otherwise returns 0. */
1265 int hub_status_data(struct usb_hcd *hcd, char *buf)
1267 dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
1270 buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
1272 return (buf[0] != 0);
1275 /** Handles hub class-specific requests. */
1276 int hub_control(struct usb_hcd *hcd,
1277 u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
1281 retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
1282 typeReq, wValue, wIndex, buf, wLength);
1285 case -DWC_E_INVALID:
1293 #endif /* DWC_DEVICE_ONLY */