2017-11-03 13:28:30 +03:00
|
|
|
// SPDX-License-Identifier: GPL-1.0+
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* OHCI HCD (Host Controller Driver) for USB.
|
2006-12-05 14:18:31 +03:00
|
|
|
*
|
2005-04-17 02:20:36 +04:00
|
|
|
* (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
|
|
|
|
* (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
|
2006-12-05 14:18:31 +03:00
|
|
|
*
|
2005-04-17 02:20:36 +04:00
|
|
|
* This file is licenced under GPL
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OHCI Root Hub ... the nonsharable stuff
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define dbg_port(hc,label,num,value) \
|
|
|
|
ohci_dbg (hc, \
|
|
|
|
"%s roothub.portstatus [%d] " \
|
|
|
|
"= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
|
2017-02-08 18:53:08 +03:00
|
|
|
label, num, value, \
|
|
|
|
(value & RH_PS_PRSC) ? " PRSC" : "", \
|
|
|
|
(value & RH_PS_OCIC) ? " OCIC" : "", \
|
|
|
|
(value & RH_PS_PSSC) ? " PSSC" : "", \
|
|
|
|
(value & RH_PS_PESC) ? " PESC" : "", \
|
|
|
|
(value & RH_PS_CSC) ? " CSC" : "", \
|
2006-12-05 14:18:31 +03:00
|
|
|
\
|
2017-02-08 18:53:08 +03:00
|
|
|
(value & RH_PS_LSDA) ? " LSDA" : "", \
|
|
|
|
(value & RH_PS_PPS) ? " PPS" : "", \
|
|
|
|
(value & RH_PS_PRS) ? " PRS" : "", \
|
|
|
|
(value & RH_PS_POCI) ? " POCI" : "", \
|
|
|
|
(value & RH_PS_PSS) ? " PSS" : "", \
|
2006-12-05 14:18:31 +03:00
|
|
|
\
|
2017-02-08 18:53:08 +03:00
|
|
|
(value & RH_PS_PES) ? " PES" : "", \
|
|
|
|
(value & RH_PS_CCS) ? " CCS" : "" \
|
2005-04-17 02:20:36 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#define OHCI_SCHED_ENABLES \
|
|
|
|
(OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
|
|
|
|
|
2014-07-19 00:25:59 +04:00
|
|
|
static void update_done_list(struct ohci_hcd *);
|
2014-07-19 00:26:07 +04:00
|
|
|
static void ohci_work(struct ohci_hcd *);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2006-10-24 20:02:31 +04:00
|
|
|
#ifdef CONFIG_PM
|
2006-09-26 22:46:16 +04:00
|
|
|
static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
|
|
|
|
__releases(ohci->lock)
|
|
|
|
__acquires(ohci->lock)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
int status = 0;
|
[PATCH] USB: Fix USB suspend/resume crasher (#2)
This patch closes the IRQ race and makes various other OHCI & EHCI code
path safer vs. suspend/resume.
I've been able to (finally !) successfully suspend and resume various
Mac models, with or without USB mouse plugged, or plugging while asleep,
or unplugging while asleep etc... all without a crash.
Alan, please verify the UHCI bit I did, I only verified that it builds.
It's very simple so I wouldn't expect any issue there. If you aren't
confident, then just drop the hunks that change uhci-hcd.c
I also made the patch a little bit more "safer" by making sure the store
to the interrupt register that disables interrupts is not posted before
I set the flag and drop the spinlock.
Without this patch, you cannot reliably sleep/wakeup any recent Mac, and
I suspect PCs have some more sneaky issues too (they don't frankly crash
with machine checks because x86 tend to silently swallow PCI errors but
that won't last afaik, at least PCI Express will blow up in those
situations, but the USB code may still misbehave).
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2005-11-25 01:59:46 +03:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
|
|
|
|
switch (ohci->hc_control & OHCI_CTRL_HCFS) {
|
|
|
|
case OHCI_USB_RESUME:
|
|
|
|
ohci_dbg (ohci, "resume/suspend?\n");
|
|
|
|
ohci->hc_control &= ~OHCI_CTRL_HCFS;
|
|
|
|
ohci->hc_control |= OHCI_USB_RESET;
|
|
|
|
ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
|
|
|
|
(void) ohci_readl (ohci, &ohci->regs->control);
|
|
|
|
/* FALL THROUGH */
|
|
|
|
case OHCI_USB_RESET:
|
|
|
|
status = -EBUSY;
|
|
|
|
ohci_dbg (ohci, "needs reinit!\n");
|
|
|
|
goto done;
|
|
|
|
case OHCI_USB_SUSPEND:
|
2006-09-26 22:46:16 +04:00
|
|
|
if (!ohci->autostop) {
|
|
|
|
ohci_dbg (ohci, "already suspended\n");
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2006-09-26 22:46:16 +04:00
|
|
|
ohci_dbg (ohci, "%s root hub\n",
|
|
|
|
autostop ? "auto-stop" : "suspend");
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/* First stop any processing */
|
2006-09-26 22:46:16 +04:00
|
|
|
if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
ohci->hc_control &= ~OHCI_SCHED_ENABLES;
|
|
|
|
ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
|
|
|
|
ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
|
|
|
|
ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
|
|
|
|
|
|
|
|
/* sched disables take effect on the next frame,
|
|
|
|
* then the last WDH could take 6+ msec
|
|
|
|
*/
|
|
|
|
ohci_dbg (ohci, "stopping schedules ...\n");
|
2006-09-26 22:46:16 +04:00
|
|
|
ohci->autostop = 0;
|
|
|
|
spin_unlock_irq (&ohci->lock);
|
|
|
|
msleep (8);
|
|
|
|
spin_lock_irq (&ohci->lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2014-07-19 00:25:59 +04:00
|
|
|
update_done_list(ohci);
|
2014-07-19 00:26:07 +04:00
|
|
|
ohci_work(ohci);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2014-05-01 23:21:42 +04:00
|
|
|
/*
|
|
|
|
* Some controllers don't handle "global" suspend properly if
|
|
|
|
* there are unsuspended ports. For these controllers, put all
|
|
|
|
* the enabled ports into suspend before suspending the root hub.
|
|
|
|
*/
|
|
|
|
if (ohci->flags & OHCI_QUIRK_GLOBAL_SUSPEND) {
|
|
|
|
__hc32 __iomem *portstat = ohci->regs->roothub.portstatus;
|
|
|
|
int i;
|
|
|
|
unsigned temp;
|
|
|
|
|
|
|
|
for (i = 0; i < ohci->num_ports; (++i, ++portstat)) {
|
|
|
|
temp = ohci_readl(ohci, portstat);
|
|
|
|
if ((temp & (RH_PS_PES | RH_PS_PSS)) ==
|
|
|
|
RH_PS_PES)
|
|
|
|
ohci_writel(ohci, RH_PS_PSS, portstat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/* maybe resume can wake root hub */
|
2008-04-14 20:17:10 +04:00
|
|
|
if (ohci_to_hcd(ohci)->self.root_hub->do_remote_wakeup || autostop) {
|
2005-04-17 02:20:36 +04:00
|
|
|
ohci->hc_control |= OHCI_CTRL_RWE;
|
2008-04-14 20:17:10 +04:00
|
|
|
} else {
|
2008-04-14 20:17:49 +04:00
|
|
|
ohci_writel(ohci, OHCI_INTR_RHSC | OHCI_INTR_RD,
|
|
|
|
&ohci->regs->intrdisable);
|
2005-04-17 02:20:36 +04:00
|
|
|
ohci->hc_control &= ~OHCI_CTRL_RWE;
|
2006-09-25 23:41:21 +04:00
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2005-09-23 09:42:53 +04:00
|
|
|
/* Suspend hub ... this is the "global (to this bus) suspend" mode,
|
|
|
|
* which doesn't imply ports will first be individually suspended.
|
|
|
|
*/
|
2005-04-17 02:20:36 +04:00
|
|
|
ohci->hc_control &= ~OHCI_CTRL_HCFS;
|
|
|
|
ohci->hc_control |= OHCI_USB_SUSPEND;
|
|
|
|
ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
|
|
|
|
(void) ohci_readl (ohci, &ohci->regs->control);
|
|
|
|
|
|
|
|
/* no resumes until devices finish suspending */
|
2006-09-26 22:46:16 +04:00
|
|
|
if (!autostop) {
|
|
|
|
ohci->next_statechange = jiffies + msecs_to_jiffies (5);
|
|
|
|
ohci->autostop = 0;
|
2011-11-18 01:41:56 +04:00
|
|
|
ohci->rh_state = OHCI_RH_SUSPENDED;
|
2006-09-26 22:46:16 +04:00
|
|
|
}
|
2006-08-04 22:31:55 +04:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
done:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct ed *find_head (struct ed *ed)
|
|
|
|
{
|
|
|
|
/* for bulk and control lists */
|
|
|
|
while (ed->ed_prev)
|
|
|
|
ed = ed->ed_prev;
|
|
|
|
return ed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* caller has locked the root hub */
|
2006-09-26 22:46:16 +04:00
|
|
|
static int ohci_rh_resume (struct ohci_hcd *ohci)
|
|
|
|
__releases(ohci->lock)
|
|
|
|
__acquires(ohci->lock)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2006-09-26 22:46:16 +04:00
|
|
|
struct usb_hcd *hcd = ohci_to_hcd (ohci);
|
2005-04-17 02:20:36 +04:00
|
|
|
u32 temp, enables;
|
|
|
|
int status = -EINPROGRESS;
|
2006-09-26 22:46:16 +04:00
|
|
|
int autostopped = ohci->autostop;
|
[PATCH] USB: Fix USB suspend/resume crasher (#2)
This patch closes the IRQ race and makes various other OHCI & EHCI code
path safer vs. suspend/resume.
I've been able to (finally !) successfully suspend and resume various
Mac models, with or without USB mouse plugged, or plugging while asleep,
or unplugging while asleep etc... all without a crash.
Alan, please verify the UHCI bit I did, I only verified that it builds.
It's very simple so I wouldn't expect any issue there. If you aren't
confident, then just drop the hunks that change uhci-hcd.c
I also made the patch a little bit more "safer" by making sure the store
to the interrupt register that disables interrupts is not posted before
I set the flag and drop the spinlock.
Without this patch, you cannot reliably sleep/wakeup any recent Mac, and
I suspect PCs have some more sneaky issues too (they don't frankly crash
with machine checks because x86 tend to silently swallow PCI errors but
that won't last afaik, at least PCI Express will blow up in those
situations, but the USB code may still misbehave).
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2005-11-25 01:59:46 +03:00
|
|
|
|
2006-09-26 22:46:16 +04:00
|
|
|
ohci->autostop = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
|
|
|
|
|
|
|
|
if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
|
2005-09-23 09:42:53 +04:00
|
|
|
/* this can happen after resuming a swsusp snapshot */
|
2011-11-18 01:41:56 +04:00
|
|
|
if (ohci->rh_state != OHCI_RH_RUNNING) {
|
2005-04-17 02:20:36 +04:00
|
|
|
ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
|
|
|
|
ohci->hc_control);
|
|
|
|
status = -EBUSY;
|
|
|
|
/* this happens when pmcore resumes HC then root */
|
|
|
|
} else {
|
|
|
|
ohci_dbg (ohci, "duplicate resume\n");
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
} else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
|
|
|
|
case OHCI_USB_SUSPEND:
|
|
|
|
ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
|
|
|
|
ohci->hc_control |= OHCI_USB_RESUME;
|
|
|
|
ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
|
|
|
|
(void) ohci_readl (ohci, &ohci->regs->control);
|
2006-09-26 22:46:16 +04:00
|
|
|
ohci_dbg (ohci, "%s root hub\n",
|
|
|
|
autostopped ? "auto-start" : "resume");
|
2005-04-17 02:20:36 +04:00
|
|
|
break;
|
|
|
|
case OHCI_USB_RESUME:
|
|
|
|
/* HCFS changes sometime after INTR_RD */
|
2006-11-06 20:05:00 +03:00
|
|
|
ohci_dbg(ohci, "%swakeup root hub\n",
|
2006-10-24 20:04:22 +04:00
|
|
|
autostopped ? "auto-" : "");
|
2005-04-17 02:20:36 +04:00
|
|
|
break;
|
|
|
|
case OHCI_USB_OPER:
|
2005-09-23 09:42:53 +04:00
|
|
|
/* this can happen after resuming a swsusp snapshot */
|
|
|
|
ohci_dbg (ohci, "snapshot resume? reinit\n");
|
|
|
|
status = -EBUSY;
|
2005-04-17 02:20:36 +04:00
|
|
|
break;
|
|
|
|
default: /* RESET, we lost power */
|
2005-09-23 09:42:53 +04:00
|
|
|
ohci_dbg (ohci, "lost power\n");
|
2005-04-17 02:20:36 +04:00
|
|
|
status = -EBUSY;
|
|
|
|
}
|
|
|
|
if (status == -EBUSY) {
|
2006-09-26 22:46:16 +04:00
|
|
|
if (!autostopped) {
|
|
|
|
spin_unlock_irq (&ohci->lock);
|
|
|
|
status = ohci_restart (ohci);
|
2007-06-01 01:34:27 +04:00
|
|
|
|
|
|
|
usb_root_hub_lost_power(hcd->self.root_hub);
|
|
|
|
|
2006-09-26 22:46:16 +04:00
|
|
|
spin_lock_irq (&ohci->lock);
|
|
|
|
}
|
|
|
|
return status;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
if (status != -EINPROGRESS)
|
|
|
|
return status;
|
2006-09-26 22:46:16 +04:00
|
|
|
if (autostopped)
|
|
|
|
goto skip_resume;
|
|
|
|
spin_unlock_irq (&ohci->lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/* Some controllers (lucent erratum) need extra-long delays */
|
2005-09-23 09:42:53 +04:00
|
|
|
msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
temp = ohci_readl (ohci, &ohci->regs->control);
|
|
|
|
temp &= OHCI_CTRL_HCFS;
|
|
|
|
if (temp != OHCI_USB_RESUME) {
|
|
|
|
ohci_err (ohci, "controller won't resume\n");
|
2006-10-27 18:35:01 +04:00
|
|
|
spin_lock_irq(&ohci->lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* disable old schedule state, reinit from scratch */
|
|
|
|
ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
|
|
|
|
ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
|
|
|
|
ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
|
|
|
|
ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
|
|
|
|
ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
|
|
|
|
ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
|
|
|
|
|
|
|
|
/* Sometimes PCI D3 suspend trashes frame timings ... */
|
|
|
|
periodic_reinit (ohci);
|
|
|
|
|
2013-10-18 19:16:07 +04:00
|
|
|
/*
|
|
|
|
* The following code is executed with ohci->lock held and
|
|
|
|
* irqs disabled if and only if autostopped is true. This
|
|
|
|
* will cause sparse to warn about a "context imbalance".
|
2006-09-26 22:46:16 +04:00
|
|
|
*/
|
|
|
|
skip_resume:
|
2005-04-17 02:20:36 +04:00
|
|
|
/* interrupts might have been disabled */
|
|
|
|
ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
|
|
|
|
if (ohci->ed_rm_list)
|
|
|
|
ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
|
|
|
|
|
|
|
|
/* Then re-enable operations */
|
|
|
|
ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
|
|
|
|
(void) ohci_readl (ohci, &ohci->regs->control);
|
2006-09-26 22:46:16 +04:00
|
|
|
if (!autostopped)
|
|
|
|
msleep (3);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2006-01-24 02:28:07 +03:00
|
|
|
temp = ohci->hc_control;
|
|
|
|
temp &= OHCI_CTRL_RWC;
|
|
|
|
temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
|
2005-04-17 02:20:36 +04:00
|
|
|
ohci->hc_control = temp;
|
|
|
|
ohci_writel (ohci, temp, &ohci->regs->control);
|
|
|
|
(void) ohci_readl (ohci, &ohci->regs->control);
|
|
|
|
|
|
|
|
/* TRSMRCY */
|
2006-09-26 22:46:16 +04:00
|
|
|
if (!autostopped) {
|
|
|
|
msleep (10);
|
|
|
|
spin_lock_irq (&ohci->lock);
|
|
|
|
}
|
|
|
|
/* now ohci->lock is always held and irqs are always disabled */
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2006-08-04 22:31:55 +04:00
|
|
|
/* keep it alive for more than ~5x suspend + resume costs */
|
|
|
|
ohci->next_statechange = jiffies + STATECHANGE_DELAY;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/* maybe turn schedules back on */
|
|
|
|
enables = 0;
|
|
|
|
temp = 0;
|
|
|
|
if (!ohci->ed_rm_list) {
|
|
|
|
if (ohci->ed_controltail) {
|
|
|
|
ohci_writel (ohci,
|
|
|
|
find_head (ohci->ed_controltail)->dma,
|
|
|
|
&ohci->regs->ed_controlhead);
|
|
|
|
enables |= OHCI_CTRL_CLE;
|
|
|
|
temp |= OHCI_CLF;
|
|
|
|
}
|
|
|
|
if (ohci->ed_bulktail) {
|
|
|
|
ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
|
|
|
|
&ohci->regs->ed_bulkhead);
|
|
|
|
enables |= OHCI_CTRL_BLE;
|
|
|
|
temp |= OHCI_BLF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
|
|
|
|
enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
|
|
|
|
if (enables) {
|
|
|
|
ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
|
|
|
|
ohci->hc_control |= enables;
|
|
|
|
ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
|
|
|
|
if (temp)
|
|
|
|
ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
|
|
|
|
(void) ohci_readl (ohci, &ohci->regs->control);
|
|
|
|
}
|
|
|
|
|
2011-11-18 01:41:56 +04:00
|
|
|
ohci->rh_state = OHCI_RH_RUNNING;
|
2005-04-17 02:20:36 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-26 22:46:16 +04:00
|
|
|
static int ohci_bus_suspend (struct usb_hcd *hcd)
|
|
|
|
{
|
|
|
|
struct ohci_hcd *ohci = hcd_to_ohci (hcd);
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
spin_lock_irq (&ohci->lock);
|
|
|
|
|
2010-06-23 00:39:10 +04:00
|
|
|
if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
|
2006-09-26 22:46:16 +04:00
|
|
|
rc = -ESHUTDOWN;
|
|
|
|
else
|
|
|
|
rc = ohci_rh_suspend (ohci, 0);
|
|
|
|
spin_unlock_irq (&ohci->lock);
|
2014-07-19 00:26:12 +04:00
|
|
|
|
ohci-hcd: Fix race condition caused by ohci_urb_enqueue() and io_watchdog_func()
Running io_watchdog_func() while ohci_urb_enqueue() is running can
cause a race condition where ohci->prev_frame_no is corrupted and the
watchdog can mis-detect following error:
ohci-platform 664a0800.usb: frame counter not updating; disabled
ohci-platform 664a0800.usb: HC died; cleaning up
Specifically, following scenario causes a race condition:
1. ohci_urb_enqueue() calls spin_lock_irqsave(&ohci->lock, flags)
and enters the critical section
2. ohci_urb_enqueue() calls timer_pending(&ohci->io_watchdog) and it
returns false
3. ohci_urb_enqueue() sets ohci->prev_frame_no to a frame number
read by ohci_frame_no(ohci)
4. ohci_urb_enqueue() schedules io_watchdog_func() with mod_timer()
5. ohci_urb_enqueue() calls spin_unlock_irqrestore(&ohci->lock,
flags) and exits the critical section
6. Later, ohci_urb_enqueue() is called
7. ohci_urb_enqueue() calls spin_lock_irqsave(&ohci->lock, flags)
and enters the critical section
8. The timer scheduled on step 4 expires and io_watchdog_func() runs
9. io_watchdog_func() calls spin_lock_irqsave(&ohci->lock, flags)
and waits on it because ohci_urb_enqueue() is already in the
critical section on step 7
10. ohci_urb_enqueue() calls timer_pending(&ohci->io_watchdog) and it
returns false
11. ohci_urb_enqueue() sets ohci->prev_frame_no to new frame number
read by ohci_frame_no(ohci) because the frame number proceeded
between step 3 and 6
12. ohci_urb_enqueue() schedules io_watchdog_func() with mod_timer()
13. ohci_urb_enqueue() calls spin_unlock_irqrestore(&ohci->lock,
flags) and exits the critical section, then wake up
io_watchdog_func() which is waiting on step 9
14. io_watchdog_func() enters the critical section
15. io_watchdog_func() calls ohci_frame_no(ohci) and set frame_no
variable to the frame number
16. io_watchdog_func() compares frame_no and ohci->prev_frame_no
On step 16, because this calling of io_watchdog_func() is scheduled on
step 4, the frame number set in ohci->prev_frame_no is expected to the
number set on step 3. However, ohci->prev_frame_no is overwritten on
step 11. Because step 16 is executed soon after step 11, the frame
number might not proceed, so ohci->prev_frame_no must equals to
frame_no.
To address above scenario, this patch introduces a special sentinel
value IO_WATCHDOG_OFF and set this value to ohci->prev_frame_no when
the watchdog is not pending or running. When ohci_urb_enqueue()
schedules the watchdog (step 4 and 12 above), it compares
ohci->prev_frame_no to IO_WATCHDOG_OFF so that ohci->prev_frame_no is
not overwritten while io_watchdog_func() is running.
Signed-off-by: Shigeru Yoshida <Shigeru.Yoshida@windriver.com>
Signed-off-by: Haiqing Bai <Haiqing.Bai@windriver.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Cc: stable <stable@vger.kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-02-02 08:51:39 +03:00
|
|
|
if (rc == 0) {
|
2014-07-19 00:26:12 +04:00
|
|
|
del_timer_sync(&ohci->io_watchdog);
|
ohci-hcd: Fix race condition caused by ohci_urb_enqueue() and io_watchdog_func()
Running io_watchdog_func() while ohci_urb_enqueue() is running can
cause a race condition where ohci->prev_frame_no is corrupted and the
watchdog can mis-detect following error:
ohci-platform 664a0800.usb: frame counter not updating; disabled
ohci-platform 664a0800.usb: HC died; cleaning up
Specifically, following scenario causes a race condition:
1. ohci_urb_enqueue() calls spin_lock_irqsave(&ohci->lock, flags)
and enters the critical section
2. ohci_urb_enqueue() calls timer_pending(&ohci->io_watchdog) and it
returns false
3. ohci_urb_enqueue() sets ohci->prev_frame_no to a frame number
read by ohci_frame_no(ohci)
4. ohci_urb_enqueue() schedules io_watchdog_func() with mod_timer()
5. ohci_urb_enqueue() calls spin_unlock_irqrestore(&ohci->lock,
flags) and exits the critical section
6. Later, ohci_urb_enqueue() is called
7. ohci_urb_enqueue() calls spin_lock_irqsave(&ohci->lock, flags)
and enters the critical section
8. The timer scheduled on step 4 expires and io_watchdog_func() runs
9. io_watchdog_func() calls spin_lock_irqsave(&ohci->lock, flags)
and waits on it because ohci_urb_enqueue() is already in the
critical section on step 7
10. ohci_urb_enqueue() calls timer_pending(&ohci->io_watchdog) and it
returns false
11. ohci_urb_enqueue() sets ohci->prev_frame_no to new frame number
read by ohci_frame_no(ohci) because the frame number proceeded
between step 3 and 6
12. ohci_urb_enqueue() schedules io_watchdog_func() with mod_timer()
13. ohci_urb_enqueue() calls spin_unlock_irqrestore(&ohci->lock,
flags) and exits the critical section, then wake up
io_watchdog_func() which is waiting on step 9
14. io_watchdog_func() enters the critical section
15. io_watchdog_func() calls ohci_frame_no(ohci) and set frame_no
variable to the frame number
16. io_watchdog_func() compares frame_no and ohci->prev_frame_no
On step 16, because this calling of io_watchdog_func() is scheduled on
step 4, the frame number set in ohci->prev_frame_no is expected to the
number set on step 3. However, ohci->prev_frame_no is overwritten on
step 11. Because step 16 is executed soon after step 11, the frame
number might not proceed, so ohci->prev_frame_no must equals to
frame_no.
To address above scenario, this patch introduces a special sentinel
value IO_WATCHDOG_OFF and set this value to ohci->prev_frame_no when
the watchdog is not pending or running. When ohci_urb_enqueue()
schedules the watchdog (step 4 and 12 above), it compares
ohci->prev_frame_no to IO_WATCHDOG_OFF so that ohci->prev_frame_no is
not overwritten while io_watchdog_func() is running.
Signed-off-by: Shigeru Yoshida <Shigeru.Yoshida@windriver.com>
Signed-off-by: Haiqing Bai <Haiqing.Bai@windriver.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Cc: stable <stable@vger.kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-02-02 08:51:39 +03:00
|
|
|
ohci->prev_frame_no = IO_WATCHDOG_OFF;
|
|
|
|
}
|
2006-09-26 22:46:16 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ohci_bus_resume (struct usb_hcd *hcd)
|
|
|
|
{
|
|
|
|
struct ohci_hcd *ohci = hcd_to_ohci (hcd);
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (time_before (jiffies, ohci->next_statechange))
|
|
|
|
msleep(5);
|
|
|
|
|
|
|
|
spin_lock_irq (&ohci->lock);
|
|
|
|
|
2010-06-23 00:39:10 +04:00
|
|
|
if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
|
2006-09-26 22:46:16 +04:00
|
|
|
rc = -ESHUTDOWN;
|
|
|
|
else
|
|
|
|
rc = ohci_rh_resume (ohci);
|
|
|
|
spin_unlock_irq (&ohci->lock);
|
|
|
|
|
|
|
|
/* poll until we know a device is connected or we autostop */
|
|
|
|
if (rc == 0)
|
|
|
|
usb_hcd_poll_rh_status(hcd);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2006-11-20 19:06:59 +03:00
|
|
|
/* Carry out polling-, autostop-, and autoresume-related state changes */
|
|
|
|
static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
|
2008-10-09 23:40:23 +04:00
|
|
|
int any_connected, int rhsc_status)
|
2006-11-20 19:06:59 +03:00
|
|
|
{
|
|
|
|
int poll_rh = 1;
|
2008-10-09 23:40:23 +04:00
|
|
|
int rhsc_enable;
|
2006-11-20 19:06:59 +03:00
|
|
|
|
2010-06-25 22:02:24 +04:00
|
|
|
/* Some broken controllers never turn off RHSC in the interrupt
|
2008-09-04 00:38:32 +04:00
|
|
|
* status register. For their sake we won't re-enable RHSC
|
|
|
|
* interrupts if the interrupt bit is already active.
|
|
|
|
*/
|
|
|
|
rhsc_enable = ohci_readl(ohci, &ohci->regs->intrenable) &
|
|
|
|
OHCI_INTR_RHSC;
|
2006-11-20 19:06:59 +03:00
|
|
|
|
2008-09-04 00:38:32 +04:00
|
|
|
switch (ohci->hc_control & OHCI_CTRL_HCFS) {
|
2006-11-20 19:06:59 +03:00
|
|
|
case OHCI_USB_OPER:
|
2008-09-04 00:38:32 +04:00
|
|
|
/* If no status changes are pending, enable RHSC interrupts. */
|
|
|
|
if (!rhsc_enable && !rhsc_status && !changed) {
|
|
|
|
rhsc_enable = OHCI_INTR_RHSC;
|
|
|
|
ohci_writel(ohci, rhsc_enable, &ohci->regs->intrenable);
|
2008-08-21 01:22:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep on polling until we know a device is connected
|
|
|
|
* and RHSC is enabled, or until we autostop.
|
|
|
|
*/
|
2006-11-20 19:06:59 +03:00
|
|
|
if (!ohci->autostop) {
|
|
|
|
if (any_connected ||
|
|
|
|
!device_may_wakeup(&ohci_to_hcd(ohci)
|
|
|
|
->self.root_hub->dev)) {
|
2008-09-04 00:38:32 +04:00
|
|
|
if (rhsc_enable)
|
2006-11-20 19:06:59 +03:00
|
|
|
poll_rh = 0;
|
|
|
|
} else {
|
|
|
|
ohci->autostop = 1;
|
|
|
|
ohci->next_statechange = jiffies + HZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if no devices have been attached for one second, autostop */
|
|
|
|
} else {
|
|
|
|
if (changed || any_connected) {
|
|
|
|
ohci->autostop = 0;
|
|
|
|
ohci->next_statechange = jiffies +
|
|
|
|
STATECHANGE_DELAY;
|
2008-09-04 00:38:32 +04:00
|
|
|
} else if (time_after_eq(jiffies,
|
2006-11-20 19:06:59 +03:00
|
|
|
ohci->next_statechange)
|
|
|
|
&& !ohci->ed_rm_list
|
|
|
|
&& !(ohci->hc_control &
|
|
|
|
OHCI_SCHED_ENABLES)) {
|
|
|
|
ohci_rh_suspend(ohci, 1);
|
2008-09-04 00:38:32 +04:00
|
|
|
if (rhsc_enable)
|
|
|
|
poll_rh = 0;
|
2006-11-20 19:06:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OHCI_USB_SUSPEND:
|
|
|
|
case OHCI_USB_RESUME:
|
2008-09-04 00:38:32 +04:00
|
|
|
/* if there is a port change, autostart or ask to be resumed */
|
2006-11-20 19:06:59 +03:00
|
|
|
if (changed) {
|
|
|
|
if (ohci->autostop)
|
|
|
|
ohci_rh_resume(ohci);
|
|
|
|
else
|
|
|
|
usb_hcd_resume_root_hub(ohci_to_hcd(ohci));
|
2008-10-09 23:40:23 +04:00
|
|
|
|
|
|
|
/* If remote wakeup is disabled, stop polling */
|
|
|
|
} else if (!ohci->autostop &&
|
|
|
|
!ohci_to_hcd(ohci)->self.root_hub->
|
|
|
|
do_remote_wakeup) {
|
|
|
|
poll_rh = 0;
|
|
|
|
|
2006-11-20 19:06:59 +03:00
|
|
|
} else {
|
2008-10-09 23:40:23 +04:00
|
|
|
/* If no status changes are pending,
|
|
|
|
* enable RHSC interrupts
|
|
|
|
*/
|
|
|
|
if (!rhsc_enable && !rhsc_status) {
|
2008-09-04 00:38:32 +04:00
|
|
|
rhsc_enable = OHCI_INTR_RHSC;
|
|
|
|
ohci_writel(ohci, rhsc_enable,
|
2008-08-21 01:22:05 +04:00
|
|
|
&ohci->regs->intrenable);
|
2008-09-04 00:38:32 +04:00
|
|
|
}
|
2008-10-09 23:40:23 +04:00
|
|
|
/* Keep polling until RHSC is enabled */
|
2008-09-04 00:38:32 +04:00
|
|
|
if (rhsc_enable)
|
|
|
|
poll_rh = 0;
|
2006-11-20 19:06:59 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return poll_rh;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* CONFIG_PM */
|
|
|
|
|
|
|
|
static inline int ohci_rh_resume(struct ohci_hcd *ohci)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Carry out polling-related state changes.
|
|
|
|
* autostop isn't used when CONFIG_PM is turned off.
|
|
|
|
*/
|
|
|
|
static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
|
2008-10-09 23:40:23 +04:00
|
|
|
int any_connected, int rhsc_status)
|
2006-11-20 19:06:59 +03:00
|
|
|
{
|
2008-08-21 01:22:05 +04:00
|
|
|
/* If RHSC is enabled, don't poll */
|
2008-07-06 21:27:25 +04:00
|
|
|
if (ohci_readl(ohci, &ohci->regs->intrenable) & OHCI_INTR_RHSC)
|
2008-08-21 01:22:05 +04:00
|
|
|
return 0;
|
|
|
|
|
2008-10-09 23:40:23 +04:00
|
|
|
/* If status changes are pending, continue polling.
|
|
|
|
* Conversely, if no status changes are pending but the RHSC
|
|
|
|
* status bit was set, then RHSC may be broken so continue polling.
|
|
|
|
*/
|
|
|
|
if (changed || rhsc_status)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* It's safe to re-enable RHSC interrupts */
|
|
|
|
ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
|
|
|
|
return 0;
|
2006-11-20 19:06:59 +03:00
|
|
|
}
|
|
|
|
|
2005-09-14 06:59:11 +04:00
|
|
|
#endif /* CONFIG_PM */
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* build "status change" packet (one or two bytes) from HC registers */
|
|
|
|
|
2014-04-16 20:00:09 +04:00
|
|
|
int ohci_hub_status_data(struct usb_hcd *hcd, char *buf)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct ohci_hcd *ohci = hcd_to_ohci (hcd);
|
2005-08-31 22:52:57 +04:00
|
|
|
int i, changed = 0, length = 1;
|
2006-10-27 18:33:11 +04:00
|
|
|
int any_connected = 0;
|
2008-10-09 23:40:23 +04:00
|
|
|
int rhsc_status;
|
2005-04-17 02:20:36 +04:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave (&ohci->lock, flags);
|
2010-06-23 00:39:10 +04:00
|
|
|
if (!HCD_HW_ACCESSIBLE(hcd))
|
2007-05-04 19:57:00 +04:00
|
|
|
goto done;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2005-08-31 22:52:57 +04:00
|
|
|
/* undocumented erratum seen on at least rev D */
|
|
|
|
if ((ohci->flags & OHCI_QUIRK_AMD756)
|
|
|
|
&& (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
|
|
|
|
ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
|
2005-04-17 02:20:36 +04:00
|
|
|
ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
|
|
|
|
/* retry later; "should not happen" */
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init status */
|
|
|
|
if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
|
|
|
|
buf [0] = changed = 1;
|
|
|
|
else
|
|
|
|
buf [0] = 0;
|
2005-08-31 22:52:57 +04:00
|
|
|
if (ohci->num_ports > 7) {
|
2005-04-17 02:20:36 +04:00
|
|
|
buf [1] = 0;
|
|
|
|
length++;
|
|
|
|
}
|
|
|
|
|
2008-10-09 23:40:23 +04:00
|
|
|
/* Clear the RHSC status flag before reading the port statuses */
|
|
|
|
ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrstatus);
|
|
|
|
rhsc_status = ohci_readl(ohci, &ohci->regs->intrstatus) &
|
|
|
|
OHCI_INTR_RHSC;
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/* look at each port */
|
2005-08-31 22:52:57 +04:00
|
|
|
for (i = 0; i < ohci->num_ports; i++) {
|
2005-04-17 02:20:36 +04:00
|
|
|
u32 status = roothub_portstatus (ohci, i);
|
|
|
|
|
2006-09-26 22:46:16 +04:00
|
|
|
/* can't autostop if ports are connected */
|
|
|
|
any_connected |= (status & RH_PS_CCS);
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
|
|
|
|
| RH_PS_OCIC | RH_PS_PRSC)) {
|
|
|
|
changed = 1;
|
|
|
|
if (i < 7)
|
|
|
|
buf [0] |= 1 << (i + 1);
|
|
|
|
else
|
|
|
|
buf [1] |= 1 << (i - 7);
|
|
|
|
}
|
2006-08-04 22:31:55 +04:00
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2010-06-23 00:39:10 +04:00
|
|
|
if (ohci_root_hub_state_changes(ohci, changed,
|
|
|
|
any_connected, rhsc_status))
|
|
|
|
set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
|
|
|
|
else
|
|
|
|
clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
|
|
|
|
|
2006-08-04 22:31:55 +04:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
done:
|
|
|
|
spin_unlock_irqrestore (&ohci->lock, flags);
|
|
|
|
|
|
|
|
return changed ? length : 0;
|
|
|
|
}
|
2014-04-16 20:00:09 +04:00
|
|
|
EXPORT_SYMBOL_GPL(ohci_hub_status_data);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
ohci_hub_descriptor (
|
|
|
|
struct ohci_hcd *ohci,
|
|
|
|
struct usb_hub_descriptor *desc
|
|
|
|
) {
|
|
|
|
u32 rh = roothub_a (ohci);
|
|
|
|
u16 temp;
|
|
|
|
|
2015-03-29 01:19:48 +03:00
|
|
|
desc->bDescriptorType = USB_DT_HUB;
|
2005-04-17 02:20:36 +04:00
|
|
|
desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
|
|
|
|
desc->bHubContrCurrent = 0;
|
|
|
|
|
2005-08-31 22:52:57 +04:00
|
|
|
desc->bNbrPorts = ohci->num_ports;
|
|
|
|
temp = 1 + (ohci->num_ports / 8);
|
2005-04-17 02:20:36 +04:00
|
|
|
desc->bDescLength = 7 + 2 * temp;
|
|
|
|
|
2015-01-19 01:41:37 +03:00
|
|
|
temp = HUB_CHAR_COMMON_LPSM | HUB_CHAR_COMMON_OCPM;
|
2005-04-17 02:20:36 +04:00
|
|
|
if (rh & RH_A_NPS) /* no power switching? */
|
2015-01-19 01:41:37 +03:00
|
|
|
temp |= HUB_CHAR_NO_LPSM;
|
2006-12-05 14:18:31 +03:00
|
|
|
if (rh & RH_A_PSM) /* per-port power switching? */
|
2015-01-19 01:41:37 +03:00
|
|
|
temp |= HUB_CHAR_INDV_PORT_LPSM;
|
2005-04-17 02:20:36 +04:00
|
|
|
if (rh & RH_A_NOCP) /* no overcurrent reporting? */
|
2015-01-19 01:41:37 +03:00
|
|
|
temp |= HUB_CHAR_NO_OCPM;
|
2005-04-17 02:20:36 +04:00
|
|
|
else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
|
2015-01-19 01:41:37 +03:00
|
|
|
temp |= HUB_CHAR_INDV_PORT_OCPM;
|
2013-10-18 19:16:07 +04:00
|
|
|
desc->wHubCharacteristics = cpu_to_le16(temp);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2010-12-01 02:55:51 +03:00
|
|
|
/* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
|
2005-04-17 02:20:36 +04:00
|
|
|
rh = roothub_b (ohci);
|
2001-09-17 11:00:00 +04:00
|
|
|
memset(desc->u.hs.DeviceRemovable, 0xff,
|
|
|
|
sizeof(desc->u.hs.DeviceRemovable));
|
|
|
|
desc->u.hs.DeviceRemovable[0] = rh & RH_B_DR;
|
2005-08-31 22:52:57 +04:00
|
|
|
if (ohci->num_ports > 7) {
|
2001-09-17 11:00:00 +04:00
|
|
|
desc->u.hs.DeviceRemovable[1] = (rh & RH_B_DR) >> 8;
|
|
|
|
desc->u.hs.DeviceRemovable[2] = 0xff;
|
2005-04-17 02:20:36 +04:00
|
|
|
} else
|
2001-09-17 11:00:00 +04:00
|
|
|
desc->u.hs.DeviceRemovable[1] = 0xff;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_USB_OTG
|
|
|
|
|
|
|
|
static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
|
|
|
|
{
|
|
|
|
struct ohci_hcd *ohci = hcd_to_ohci (hcd);
|
|
|
|
u32 status;
|
|
|
|
|
|
|
|
if (!port)
|
|
|
|
return -EINVAL;
|
|
|
|
port--;
|
|
|
|
|
|
|
|
/* start port reset before HNP protocol times out */
|
|
|
|
status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
|
|
|
|
if (!(status & RH_PS_CCS))
|
|
|
|
return -ENODEV;
|
|
|
|
|
2014-09-19 19:32:23 +04:00
|
|
|
/* hub_wq will finish the reset later */
|
2005-04-17 02:20:36 +04:00
|
|
|
ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define ohci_start_port_reset NULL
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
/* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
|
|
|
|
* not necessarily continuous ... to guard against resume signaling.
|
|
|
|
*/
|
|
|
|
#define PORT_RESET_MSEC 50
|
|
|
|
|
|
|
|
/* this timer value might be vendor-specific ... */
|
|
|
|
#define PORT_RESET_HW_MSEC 10
|
|
|
|
|
|
|
|
/* wrap-aware logic morphed from <linux/jiffies.h> */
|
|
|
|
#define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
|
|
|
|
|
2014-09-19 19:32:23 +04:00
|
|
|
/* called from some task, normally hub_wq */
|
2006-12-07 04:04:15 +03:00
|
|
|
static inline int root_port_reset (struct ohci_hcd *ohci, unsigned port)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
__hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
|
2008-04-22 18:50:18 +04:00
|
|
|
u32 temp = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
|
|
|
|
u16 reset_done = now + PORT_RESET_MSEC;
|
2008-02-02 13:42:52 +03:00
|
|
|
int limit_1 = DIV_ROUND_UP(PORT_RESET_MSEC, PORT_RESET_HW_MSEC);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/* build a "continuous enough" reset signal, with up to
|
|
|
|
* 3msec gap between pulses. scheduler HZ==100 must work;
|
|
|
|
* this might need to be deadline-scheduled.
|
|
|
|
*/
|
|
|
|
do {
|
2008-02-02 13:42:52 +03:00
|
|
|
int limit_2;
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/* spin until any current reset finishes */
|
2008-02-02 13:42:52 +03:00
|
|
|
limit_2 = PORT_RESET_HW_MSEC * 2;
|
|
|
|
while (--limit_2 >= 0) {
|
2005-04-17 02:20:36 +04:00
|
|
|
temp = ohci_readl (ohci, portstat);
|
2006-12-07 04:04:15 +03:00
|
|
|
/* handle e.g. CardBus eject */
|
|
|
|
if (temp == ~(u32)0)
|
|
|
|
return -ESHUTDOWN;
|
2005-04-17 02:20:36 +04:00
|
|
|
if (!(temp & RH_PS_PRS))
|
|
|
|
break;
|
|
|
|
udelay (500);
|
2006-12-05 14:18:31 +03:00
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2008-02-02 13:42:52 +03:00
|
|
|
/* timeout (a hardware error) has been observed when
|
|
|
|
* EHCI sets CF while this driver is resetting a port;
|
|
|
|
* presumably other disconnect paths might do it too.
|
|
|
|
*/
|
|
|
|
if (limit_2 < 0) {
|
|
|
|
ohci_dbg(ohci,
|
|
|
|
"port[%d] reset timeout, stat %08x\n",
|
|
|
|
port, temp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
if (!(temp & RH_PS_CCS))
|
|
|
|
break;
|
|
|
|
if (temp & RH_PS_PRSC)
|
|
|
|
ohci_writel (ohci, RH_PS_PRSC, portstat);
|
|
|
|
|
|
|
|
/* start the next reset, sleep till it's probably done */
|
|
|
|
ohci_writel (ohci, RH_PS_PRS, portstat);
|
|
|
|
msleep(PORT_RESET_HW_MSEC);
|
|
|
|
now = ohci_readl(ohci, &ohci->regs->fmnumber);
|
2008-02-02 13:42:52 +03:00
|
|
|
} while (tick_before(now, reset_done) && --limit_1 >= 0);
|
|
|
|
|
|
|
|
/* caller synchronizes using PRSC ... and handles PRS
|
|
|
|
* still being set when this returns.
|
|
|
|
*/
|
2006-12-07 04:04:15 +03:00
|
|
|
|
|
|
|
return 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2014-04-16 20:00:09 +04:00
|
|
|
int ohci_hub_control(
|
2005-04-17 02:20:36 +04:00
|
|
|
struct usb_hcd *hcd,
|
|
|
|
u16 typeReq,
|
|
|
|
u16 wValue,
|
|
|
|
u16 wIndex,
|
|
|
|
char *buf,
|
|
|
|
u16 wLength
|
|
|
|
) {
|
|
|
|
struct ohci_hcd *ohci = hcd_to_ohci (hcd);
|
2010-04-20 18:37:57 +04:00
|
|
|
int ports = ohci->num_ports;
|
2005-04-17 02:20:36 +04:00
|
|
|
u32 temp;
|
|
|
|
int retval = 0;
|
|
|
|
|
2010-06-23 00:39:10 +04:00
|
|
|
if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
|
[PATCH] USB: Fix USB suspend/resume crasher (#2)
This patch closes the IRQ race and makes various other OHCI & EHCI code
path safer vs. suspend/resume.
I've been able to (finally !) successfully suspend and resume various
Mac models, with or without USB mouse plugged, or plugging while asleep,
or unplugging while asleep etc... all without a crash.
Alan, please verify the UHCI bit I did, I only verified that it builds.
It's very simple so I wouldn't expect any issue there. If you aren't
confident, then just drop the hunks that change uhci-hcd.c
I also made the patch a little bit more "safer" by making sure the store
to the interrupt register that disables interrupts is not posted before
I set the flag and drop the spinlock.
Without this patch, you cannot reliably sleep/wakeup any recent Mac, and
I suspect PCs have some more sneaky issues too (they don't frankly crash
with machine checks because x86 tend to silently swallow PCI errors but
that won't last afaik, at least PCI Express will blow up in those
situations, but the USB code may still misbehave).
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2005-11-25 01:59:46 +03:00
|
|
|
return -ESHUTDOWN;
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
switch (typeReq) {
|
|
|
|
case ClearHubFeature:
|
|
|
|
switch (wValue) {
|
|
|
|
case C_HUB_OVER_CURRENT:
|
|
|
|
ohci_writel (ohci, RH_HS_OCIC,
|
|
|
|
&ohci->regs->roothub.status);
|
|
|
|
case C_HUB_LOCAL_POWER:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ClearPortFeature:
|
|
|
|
if (!wIndex || wIndex > ports)
|
|
|
|
goto error;
|
|
|
|
wIndex--;
|
|
|
|
|
|
|
|
switch (wValue) {
|
|
|
|
case USB_PORT_FEAT_ENABLE:
|
|
|
|
temp = RH_PS_CCS;
|
|
|
|
break;
|
|
|
|
case USB_PORT_FEAT_C_ENABLE:
|
|
|
|
temp = RH_PS_PESC;
|
|
|
|
break;
|
|
|
|
case USB_PORT_FEAT_SUSPEND:
|
|
|
|
temp = RH_PS_POCI;
|
|
|
|
break;
|
|
|
|
case USB_PORT_FEAT_C_SUSPEND:
|
|
|
|
temp = RH_PS_PSSC;
|
|
|
|
break;
|
|
|
|
case USB_PORT_FEAT_POWER:
|
|
|
|
temp = RH_PS_LSDA;
|
|
|
|
break;
|
|
|
|
case USB_PORT_FEAT_C_CONNECTION:
|
|
|
|
temp = RH_PS_CSC;
|
|
|
|
break;
|
|
|
|
case USB_PORT_FEAT_C_OVER_CURRENT:
|
|
|
|
temp = RH_PS_OCIC;
|
|
|
|
break;
|
|
|
|
case USB_PORT_FEAT_C_RESET:
|
|
|
|
temp = RH_PS_PRSC;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
ohci_writel (ohci, temp,
|
|
|
|
&ohci->regs->roothub.portstatus [wIndex]);
|
|
|
|
// ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
|
|
|
|
break;
|
|
|
|
case GetHubDescriptor:
|
|
|
|
ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
|
|
|
|
break;
|
|
|
|
case GetHubStatus:
|
|
|
|
temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
|
2008-04-29 12:03:40 +04:00
|
|
|
put_unaligned_le32(temp, buf);
|
2005-04-17 02:20:36 +04:00
|
|
|
break;
|
|
|
|
case GetPortStatus:
|
|
|
|
if (!wIndex || wIndex > ports)
|
|
|
|
goto error;
|
|
|
|
wIndex--;
|
|
|
|
temp = roothub_portstatus (ohci, wIndex);
|
2008-04-29 12:03:40 +04:00
|
|
|
put_unaligned_le32(temp, buf);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-11-18 16:23:00 +04:00
|
|
|
if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
|
|
|
|
dbg_port(ohci, "GetStatus", wIndex, temp);
|
2005-04-17 02:20:36 +04:00
|
|
|
break;
|
|
|
|
case SetHubFeature:
|
|
|
|
switch (wValue) {
|
|
|
|
case C_HUB_OVER_CURRENT:
|
|
|
|
// FIXME: this can be cleared, yes?
|
|
|
|
case C_HUB_LOCAL_POWER:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SetPortFeature:
|
|
|
|
if (!wIndex || wIndex > ports)
|
|
|
|
goto error;
|
|
|
|
wIndex--;
|
|
|
|
switch (wValue) {
|
|
|
|
case USB_PORT_FEAT_SUSPEND:
|
|
|
|
#ifdef CONFIG_USB_OTG
|
|
|
|
if (hcd->self.otg_port == (wIndex + 1)
|
|
|
|
&& hcd->self.b_hnp_enable)
|
2008-07-06 14:26:30 +04:00
|
|
|
ohci->start_hnp(ohci);
|
2005-04-17 02:20:36 +04:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
ohci_writel (ohci, RH_PS_PSS,
|
|
|
|
&ohci->regs->roothub.portstatus [wIndex]);
|
|
|
|
break;
|
|
|
|
case USB_PORT_FEAT_POWER:
|
|
|
|
ohci_writel (ohci, RH_PS_PPS,
|
|
|
|
&ohci->regs->roothub.portstatus [wIndex]);
|
|
|
|
break;
|
|
|
|
case USB_PORT_FEAT_RESET:
|
2006-12-07 04:04:15 +03:00
|
|
|
retval = root_port_reset (ohci, wIndex);
|
2005-04-17 02:20:36 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error:
|
|
|
|
/* "protocol stall" on error */
|
|
|
|
retval = -EPIPE;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
2014-04-16 20:00:09 +04:00
|
|
|
EXPORT_SYMBOL_GPL(ohci_hub_control);
|