[adm5120] add copyright header to the USB driver's files
[openwrt.git] / target / linux / adm5120 / files / drivers / usb / host / adm5120-hcd.c
index fae5aa64fa3ee9cb95019bafe2ad2fdfba3ad22d..54185966145f45e810525752718efb6b6400a90f 100644 (file)
@@ -1,19 +1,16 @@
 /*
- * OHCI HCD (Host Controller Driver) for USB.
+ * ADM5120 HCD (Host Controller Driver) for USB
  *
- * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
- * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
+ * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
  *
- * [ Initialisation is based on Linus'  ]
- * [ uhci code and gregs ahcd fragments ]
- * [ (C) Copyright 1999 Linus Torvalds  ]
- * [ (C) Copyright 1999 Gregory P. Smith]
+ * This file was derived from: drivers/usb/host/ohci-hcd.c
+ *   (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
+ *   (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
  *
- *
- * OHCI is the main "non-Intel/VIA" standard for USB 1.1 host controller
- * interfaces (though some non-x86 Intel chips use it).  It supports
- * smarter hardware than UHCI.  A download link for the spec available
- * through the http://www.usb.org website.
+ *   [ Initialisation is based on Linus'  ]
+ *   [ uhci code and gregs ahcd fragments ]
+ *   [ (C) Copyright 1999 Linus Torvalds  ]
+ *   [ (C) Copyright 1999 Gregory P. Smith]
  *
  * This file is licenced under the GPL.
  */
 #include "../core/hcd.h"
 #include "../core/hub.h"
 
-#define DRIVER_VERSION "v0.02"
+#define DRIVER_VERSION "0.14.1"
 #define DRIVER_AUTHOR  "Gabor Juhos <juhosg at openwrt.org>"
 #define DRIVER_DESC    "ADMtek USB 1.1 Host Controller Driver"
 
 /*-------------------------------------------------------------------------*/
 
-#define ADMHC_VERBOSE_DEBUG    /* not always helpful */
-#undef LATE_ED_SCHEDULE
+#undef ADMHC_VERBOSE_DEBUG     /* not always helpful */
 
 /* For initializing controller (mask in an HCFS mode too) */
 #define        OHCI_CONTROL_INIT       OHCI_CTRL_CBSR
@@ -73,9 +69,10 @@ static void admhc_dump(struct admhcd *ahcd, int verbose);
 static int admhc_init(struct admhcd *ahcd);
 static void admhc_stop(struct usb_hcd *hcd);
 
-#include "adm5120-hub.c"
 #include "adm5120-dbg.c"
 #include "adm5120-mem.c"
+#include "adm5120-pm.c"
+#include "adm5120-hub.c"
 #include "adm5120-q.c"
 
 /*-------------------------------------------------------------------------*/
@@ -92,7 +89,7 @@ static int admhc_urb_enqueue(struct usb_hcd *hcd, struct usb_host_endpoint *ep,
        unsigned int    pipe = urb->pipe;
        int             td_cnt = 0;
        unsigned long   flags;
-       int             retval = 0;
+       int             ret = 0;
 
 #ifdef ADMHC_VERBOSE_DEBUG
        spin_lock_irqsave(&ahcd->lock, flags);
@@ -114,12 +111,10 @@ static int admhc_urb_enqueue(struct usb_hcd *hcd, struct usb_host_endpoint *ep,
 
                /* 1 TD for setup, 1 for ACK, plus ... */
                td_cnt = 2;
-               if (urb->transfer_buffer_length)
-                       td_cnt++;
-               break;
+               /* FALLTHROUGH */
        case PIPE_BULK:
                /* one TD for every 4096 Bytes (can be upto 8K) */
-               td_cnt = urb->transfer_buffer_length / TD_DATALEN_MAX;
+               td_cnt += urb->transfer_buffer_length / TD_DATALEN_MAX;
                /* ... and for any remaining bytes ... */
                if ((urb->transfer_buffer_length % TD_DATALEN_MAX) != 0)
                        td_cnt++;
@@ -144,10 +139,6 @@ static int admhc_urb_enqueue(struct usb_hcd *hcd, struct usb_host_endpoint *ep,
                /* number of packets from URB */
                td_cnt = urb->number_of_packets;
                break;
-       default:
-               /* paranoia */
-               admhc_err(ahcd, "bad EP type %d", ed->type);
-               return -EINVAL;
        }
 
        urb_priv = urb_priv_alloc(ahcd, td_cnt, mem_flags);
@@ -155,16 +146,15 @@ static int admhc_urb_enqueue(struct usb_hcd *hcd, struct usb_host_endpoint *ep,
                return -ENOMEM;
 
        urb_priv->ed = ed;
-       urb_priv->urb = urb;
 
        spin_lock_irqsave(&ahcd->lock, flags);
        /* don't submit to a dead HC */
        if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
-               retval = -ENODEV;
+               ret = -ENODEV;
                goto fail;
        }
        if (!HC_IS_RUNNING(hcd->state)) {
-               retval = -ENODEV;
+               ret = -ENODEV;
                goto fail;
        }
 
@@ -174,12 +164,17 @@ static int admhc_urb_enqueue(struct usb_hcd *hcd, struct usb_host_endpoint *ep,
                spin_unlock(&urb->lock);
                urb->hcpriv = urb_priv;
                finish_urb(ahcd, urb);
-               retval = 0;
+               ret = 0;
                goto fail;
        }
 
-       if (ed->type == PIPE_ISOCHRONOUS) {
-               if (ed->state == ED_NEW) {
+       /* schedule the ed if needed */
+       if (ed->state == ED_IDLE) {
+               ret = ed_schedule(ahcd, ed);
+               if (ret < 0)
+                       goto fail0;
+
+               if (ed->type == PIPE_ISOCHRONOUS) {
                        u16     frame = admhc_frame_no(ahcd);
 
                        /* delay a few frames before the first TD */
@@ -191,29 +186,29 @@ static int admhc_urb_enqueue(struct usb_hcd *hcd, struct usb_host_endpoint *ep,
                        /* yes, only URB_ISO_ASAP is supported, and
                         * urb->start_frame is never used as input.
                         */
-               } else
-                       urb->start_frame = ed->last_iso + ed->interval;
-       }
+               }
+       } else if (ed->type == PIPE_ISOCHRONOUS)
+               urb->start_frame = ed->last_iso + ed->interval;
 
+       /* fill the TDs and link them to the ed; and
+        * enable that part of the schedule, if needed
+        * and update count of queued periodic urbs
+        */
        urb->hcpriv = urb_priv;
-       td_submit_urb(ahcd, urb_priv->urb);
-
-       /* append it to the ED's queue */
-       list_add_tail(&urb_priv->pending, &ed->urb_pending);
+       td_submit_urb(ahcd, urb);
 
-       /* schedule the ED */
-       retval = ed_schedule(ahcd, ed);
+#ifdef ADMHC_VERBOSE_DEBUG
+       admhc_dump_ed(ahcd, "admhc_urb_enqueue", urb_priv->ed, 1);
+#endif
 
 fail0:
        spin_unlock(&urb->lock);
 fail:
-       if (retval) {
-               urb_priv = urb->hcpriv;
+       if (ret)
                urb_priv_free(ahcd, urb_priv);
-       }
 
        spin_unlock_irqrestore(&ahcd->lock, flags);
-       return retval;
+       return ret;
 }
 
 /*
@@ -225,43 +220,33 @@ fail:
 static int admhc_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
 {
        struct admhcd *ahcd = hcd_to_admhcd(hcd);
-       struct urb_priv *up;
        unsigned long flags;
 
-       up = urb->hcpriv;
-       if (!up)
-               return 0;
-
        spin_lock_irqsave(&ahcd->lock, flags);
 
 #ifdef ADMHC_VERBOSE_DEBUG
-       urb_print(ahcd, urb, "DEQEUE", 1);
+       urb_print(ahcd, urb, "DEQUEUE", 1);
 #endif
 
        if (HC_IS_RUNNING(hcd->state)) {
+               struct urb_priv *urb_priv;
+
                /* Unless an IRQ completed the unlink while it was being
                 * handed to us, flag it for unlink and giveback, and force
                 * some upcoming INTR_SF to call finish_unlinks()
                 */
-               if (up->ed->urb_active != up) {
-                       list_del(&up->pending);
-                       finish_urb(ahcd, urb);
-               } else {
-                       ed_start_deschedule(ahcd, up->ed);
+               urb_priv = urb->hcpriv;
+               if (urb_priv) {
+                       if (urb_priv->ed->state == ED_OPER)
+                               start_ed_unlink(ahcd, urb_priv->ed);
                }
        } else {
                /*
                 * with HC dead, we won't respect hc queue pointers
                 * any more ... just clean up every urb's memory.
                 */
-               if (up->ed->urb_active != up) {
-                       list_del(&up->pending);
+               if (urb->hcpriv)
                        finish_urb(ahcd, urb);
-               } else {
-                       finish_urb(ahcd, urb);
-                       up->ed->urb_active = NULL;
-                       up->ed->state = ED_IDLE;
-               }
        }
        spin_unlock_irqrestore(&ahcd->lock, flags);
 
@@ -273,6 +258,7 @@ static int admhc_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
 /* frees config/altsetting state for endpoints,
  * including ED memory, dummy TD, and bulk/intr data toggle
  */
+
 static void admhc_endpoint_disable(struct usb_hcd *hcd,
                struct usb_host_endpoint *ep)
 {
@@ -298,8 +284,8 @@ rescan:
 
        if (!HC_IS_RUNNING(hcd->state)) {
 sanitize:
-               ed->state = ED_UNLINK;
-               admhc_finish_unlinks(ahcd, 0);
+               ed->state = ED_IDLE;
+               finish_unlinks(ahcd, 0);
        }
 
        switch (ed->state) {
@@ -312,11 +298,10 @@ sanitize:
                spin_unlock_irqrestore(&ahcd->lock, flags);
                schedule_timeout_uninterruptible(1);
                goto rescan;
-       case ED_IDLE:
-       case ED_NEW:            /* fully unlinked */
-               if (list_empty(&ed->urb_pending)) {
-                       td_free(ahcd, ed->dummy);
-                       ed_free(ahcd, ed);
+       case ED_IDLE:           /* fully unlinked */
+               if (list_empty(&ed->td_list)) {
+                       td_free (ahcd, ed->dummy);
+                       ed_free (ahcd, ed);
                        break;
                }
                /* else FALL THROUGH */
@@ -324,11 +309,10 @@ sanitize:
                /* caller was supposed to have unlinked any requests;
                 * that's not our job.  can't recover; must leak ed.
                 */
-               admhc_err(ahcd, "leak ed %p (#%02x) %s act %p%s\n",
-                       ed, ep->desc.bEndpointAddress,
-                       ed_statestring(ed->state),
-                       ed->urb_active,
-                       list_empty(&ed->urb_pending) ? "" : " (has urbs)");
+               admhc_err(ahcd, "leak ed %p (#%02x) state %d%s\n",
+                       ed, ep->desc.bEndpointAddress, ed->state,
+                       list_empty(&ed->td_list) ? "" : " (has tds)");
+               td_free(ahcd, ed->dummy);
                break;
        }
 
@@ -338,7 +322,7 @@ sanitize:
        return;
 }
 
-static int admhc_get_frame(struct usb_hcd *hcd)
+static int admhc_get_frame_number(struct usb_hcd *hcd)
 {
        struct admhcd *ahcd = hcd_to_admhcd(hcd);
 
@@ -347,8 +331,15 @@ static int admhc_get_frame(struct usb_hcd *hcd)
 
 static void admhc_usb_reset(struct admhcd *ahcd)
 {
+#if 0
+       ahcd->hc_control = admhc_readl(ahcd, &ahcd->regs->control);
+       ahcd->hc_control &= OHCI_CTRL_RWC;
+       admhc_writel(ahcd, ahcd->hc_control, &ahcd->regs->control);
+#else
+       /* FIXME */
        ahcd->host_control = ADMHC_BUSS_RESET;
        admhc_writel(ahcd, ahcd->host_control ,&ahcd->regs->host_control);
+#endif
 }
 
 /* admhc_shutdown forcibly disables IRQs and DMA, helping kexec and
@@ -467,7 +458,7 @@ static int admhc_init(struct admhcd *ahcd)
 
        /* Read the number of ports unless overridden */
        if (ahcd->num_ports == 0)
-               ahcd->num_ports = admhc_get_rhdesc(ahcd) & ADMHC_RH_NUMP;
+               ahcd->num_ports = admhc_read_rhdesc(ahcd) & ADMHC_RH_NUMP;
 
        ret = admhc_mem_init(ahcd);
        if (ret)
@@ -509,10 +500,21 @@ static int admhc_run(struct admhcd *ahcd)
                        admhc_dbg(ahcd, "fminterval delta %d\n",
                                ahcd->fminterval - FI);
                ahcd->fminterval |=
-                       (FSLDP (ahcd->fminterval) << ADMHC_SFI_FSLDP_SHIFT);
+                       (FSLDP(ahcd->fminterval) << ADMHC_SFI_FSLDP_SHIFT);
                /* also: power/overcurrent flags in rhdesc */
        }
 
+#if 0  /* TODO: not applicable */
+       /* Reset USB nearly "by the book".  RemoteWakeupConnected was
+        * saved if boot firmware (BIOS/SMM/...) told us it's connected,
+        * or if bus glue did the same (e.g. for PCI add-in cards with
+        * PCI PM support).
+        */
+       if ((ahcd->hc_control & OHCI_CTRL_RWC) != 0
+                       && !device_may_wakeup(hcd->self.controller))
+               device_init_wakeup(hcd->self.controller, 1);
+#endif
+
        switch (ahcd->host_control & ADMHC_HC_BUSS) {
        case ADMHC_BUSS_OPER:
                temp = 0;
@@ -535,12 +537,11 @@ static int admhc_run(struct admhcd *ahcd)
        admhc_writel_flush(ahcd);
 
        msleep(temp);
-       temp = admhc_get_rhdesc(ahcd);
+       temp = admhc_read_rhdesc(ahcd);
        if (!(temp & ADMHC_RH_NPS)) {
                /* power down each port */
                for (temp = 0; temp < ahcd->num_ports; temp++)
-                       admhc_writel(ahcd, ADMHC_PS_CPP,
-                               &ahcd->regs->portstatus[temp]);
+                       admhc_write_portstatus(ahcd, temp, ADMHC_PS_CPP);
        }
        /* flush those writes */
        admhc_writel_flush(ahcd);
@@ -548,7 +549,6 @@ static int admhc_run(struct admhcd *ahcd)
        /* 2msec timelimit here means no irqs/preempt */
        spin_lock_irq(&ahcd->lock);
 
-retry:
        admhc_writel(ahcd, ADMHC_CTRL_SR,  &ahcd->regs->gencontrol);
        temp = 30;      /* ... allow extra time */
        while ((admhc_readl(ahcd, &ahcd->regs->gencontrol) & ADMHC_CTRL_SR) != 0) {
@@ -557,7 +557,7 @@ retry:
                        admhc_err(ahcd, "USB HC reset timed out!\n");
                        return -1;
                }
-               udelay (1);
+               udelay(1);
        }
 
        /* enable HOST mode, before access any host specific register */
@@ -650,7 +650,6 @@ static irqreturn_t admhc_irq(struct usb_hcd *hcd)
 
        ints &= admhc_readl(ahcd, &regs->int_enable);
 
-       spin_lock(&ahcd->lock);
        if (ints & ADMHC_INTR_FATI) {
                /* e.g. due to PCI Master/Target Abort */
                admhc_disable(ahcd);
@@ -660,10 +659,9 @@ static irqreturn_t admhc_irq(struct usb_hcd *hcd)
        }
 
        if (ints & ADMHC_INTR_BABI) {
-               admhc_intr_disable(ahcd, ADMHC_INTR_MIE);
+               admhc_intr_disable(ahcd, ADMHC_INTR_BABI);
+               admhc_intr_ack(ahcd, ADMHC_INTR_BABI);
                admhc_err(ahcd, "Babble Detected\n");
-               admhc_disable(ahcd);
-               admhc_usb_reset(ahcd);
        }
 
        if (ints & ADMHC_INTR_INSM) {
@@ -690,17 +688,21 @@ static irqreturn_t admhc_irq(struct usb_hcd *hcd)
                admhc_intr_ack(ahcd, ADMHC_INTR_RESI);
                hcd->poll_rh = 1;
                if (ahcd->autostop) {
+                       spin_lock(&ahcd->lock);
                        admhc_rh_resume(ahcd);
+                       spin_unlock(&ahcd->lock);
                } else
                        usb_hcd_resume_root_hub(hcd);
        }
 
        if (ints & ADMHC_INTR_TDC) {
+               admhc_vdbg(ahcd, "Transfer Descriptor Complete\n");
                admhc_intr_ack(ahcd, ADMHC_INTR_TDC);
                if (HC_IS_RUNNING(hcd->state))
                        admhc_intr_disable(ahcd, ADMHC_INTR_TDC);
-               admhc_vdbg(ahcd, "Transfer Descriptor Complete\n");
+               spin_lock(&ahcd->lock);
                admhc_td_complete(ahcd);
+               spin_unlock(&ahcd->lock);
                if (HC_IS_RUNNING(hcd->state))
                        admhc_intr_enable(ahcd, ADMHC_INTR_TDC);
        }
@@ -710,19 +712,43 @@ static irqreturn_t admhc_irq(struct usb_hcd *hcd)
                admhc_vdbg(ahcd, "Schedule Overrun\n");
        }
 
+#if 1
+       spin_lock(&ahcd->lock);
+       if (ahcd->ed_rm_list)
+               finish_unlinks(ahcd, admhc_frame_no(ahcd));
+
+       if ((ints & ADMHC_INTR_SOFI) != 0 && !ahcd->ed_rm_list
+                       && HC_IS_RUNNING(hcd->state))
+               admhc_intr_disable(ahcd, ADMHC_INTR_SOFI);
+       spin_unlock(&ahcd->lock);
+#else
        if (ints & ADMHC_INTR_SOFI) {
-               admhc_intr_ack(ahcd, ADMHC_INTR_SOFI);
+               admhc_vdbg(ahcd, "Start Of Frame\n");
+               spin_lock(&ahcd->lock);
+
                /* handle any pending ED removes */
-               admhc_finish_unlinks(ahcd, admhc_frame_no(ahcd));
-               admhc_sof_refill(ahcd);
+               finish_unlinks(ahcd, admhc_frameno(ahcd));
+
+               /* leaving INTR_SOFI enabled when there's still unlinking
+                * to be done in the (next frame).
+                */
+               if ((ahcd->ed_rm_list == NULL) ||
+                       HC_IS_RUNNING(hcd->state) == 0)
+                       /*
+                        * disable INTR_SOFI if there are no unlinking to be
+                        * done (in the next frame)
+                        */
+                       admhc_intr_disable(ahcd, ADMHC_INTR_SOFI);
+
+               spin_unlock(&ahcd->lock);
        }
+#endif
 
        if (HC_IS_RUNNING(hcd->state)) {
                admhc_intr_ack(ahcd, ints);
                admhc_intr_enable(ahcd, ADMHC_INTR_MIE);
                admhc_writel_flush(ahcd);
        }
-       spin_unlock(&ahcd->lock);
 
        return IRQ_HANDLED;
 }
@@ -750,87 +776,6 @@ static void admhc_stop(struct usb_hcd *hcd)
 
 /*-------------------------------------------------------------------------*/
 
-/* must not be called from interrupt context */
-
-#ifdef CONFIG_PM
-
-static int admhc_restart(struct admhcd *ahcd)
-{
-       int temp;
-       int i;
-       struct urb_priv *priv;
-
-       /* mark any devices gone, so they do nothing till khubd disconnects.
-        * recycle any "live" eds/tds (and urbs) right away.
-        * later, khubd disconnect processing will recycle the other state,
-        * (either as disconnect/reconnect, or maybe someday as a reset).
-        */
-       spin_lock_irq(&ahcd->lock);
-       admhc_disable(ahcd);
-       usb_root_hub_lost_power(admhcd_to_hcd(ahcd)->self.root_hub);
-       if (!list_empty(&ahcd->pending))
-               admhc_dbg(ahcd, "abort schedule...\n");
-               list_for_each_entry(priv, &ahcd->pending, pending) {
-               struct urb      *urb = priv->td[0]->urb;
-               struct ed       *ed = priv->ed;
-
-               switch (ed->state) {
-               case ED_OPER:
-                       ed->state = ED_UNLINK;
-                       ed->hwINFO |= cpu_to_hc32(ahcd, ED_DEQUEUE);
-                       ed_deschedule (ahcd, ed);
-
-                       ed->ed_next = ahcd->ed_rm_list;
-                       ed->ed_prev = NULL;
-                       ahcd->ed_rm_list = ed;
-                       /* FALLTHROUGH */
-               case ED_UNLINK:
-                       break;
-               default:
-                       admhc_dbg(ahcd, "bogus ed %p state %d\n",
-                                       ed, ed->state);
-               }
-
-               spin_lock(&urb->lock);
-               urb->status = -ESHUTDOWN;
-               spin_unlock(&urb->lock);
-       }
-       finish_unlinks(ahcd, 0);
-       spin_unlock_irq(&ahcd->lock);
-
-       /* paranoia, in case that didn't work: */
-
-       /* empty the interrupt branches */
-       for (i = 0; i < NUM_INTS; i++) ahcd->load[i] = 0;
-       for (i = 0; i < NUM_INTS; i++) ahcd->hcca->int_table[i] = 0;
-
-       /* no EDs to remove */
-       ahcd->ed_rm_list = NULL;
-
-       /* empty control and bulk lists */
-       ahcd->ed_controltail = NULL;
-       ahcd->ed_bulktail    = NULL;
-
-       if ((temp = admhc_run(ahcd)) < 0) {
-               admhc_err(ahcd, "can't restart, %d\n", temp);
-               return temp;
-       } else {
-               /* here we "know" root ports should always stay powered,
-                * and that if we try to turn them back on the root hub
-                * will respond to CSC processing.
-                */
-               i = ahcd->num_ports;
-               while (i--)
-                       admhc_writel(ahcd, RH_PS_PSS,
-                               &ahcd->regs->portstatus[i]);
-               admhc_dbg(ahcd, "restart complete\n");
-       }
-       return 0;
-}
-#endif
-
-/*-------------------------------------------------------------------------*/
-
 #ifdef CONFIG_MIPS_ADM5120
 #include "adm5120-drv.c"
 #define PLATFORM_DRIVER                usb_hcd_adm5120_driver
@@ -840,11 +785,11 @@ static int admhc_restart(struct admhcd *ahcd)
 #error "missing bus glue for admhc-hcd"
 #endif
 
-#define DRIVER_INFO DRIVER_DESC " " DRIVER_VERSION
+#define DRIVER_INFO DRIVER_DESC " version " DRIVER_VERSION
 
 static int __init admhc_hcd_mod_init(void)
 {
-       int retval = 0;
+       int ret = 0;
 
        if (usb_disabled())
                return -ENODEV;
@@ -854,18 +799,18 @@ static int __init admhc_hcd_mod_init(void)
                sizeof (struct ed), sizeof (struct td));
 
 #ifdef PLATFORM_DRIVER
-       retval = platform_driver_register(&PLATFORM_DRIVER);
-       if (retval < 0)
+       ret = platform_driver_register(&PLATFORM_DRIVER);
+       if (ret < 0)
                goto error_platform;
 #endif
 
-       return retval;
+       return ret;
 
 #ifdef PLATFORM_DRIVER
        platform_driver_unregister(&PLATFORM_DRIVER);
 error_platform:
 #endif
-       return retval;
+       return ret;
 }
 module_init(admhc_hcd_mod_init);
 
@@ -877,4 +822,5 @@ module_exit(admhc_hcd_mod_exit);
 
 MODULE_AUTHOR(DRIVER_AUTHOR);
 MODULE_DESCRIPTION(DRIVER_INFO);
+MODULE_VERSION(DRIVER_VERSION);
 MODULE_LICENSE("GPL");