From: Mathias Nyman Date: Wed, 3 Jun 2026 09:11:25 +0000 (+0300) Subject: xhci: dbc: serialize enabling and disabling dbc X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=520058b73ba336380ecf7ea412263de9a7573df8;p=thirdparty%2Fkernel%2Flinux.git xhci: dbc: serialize enabling and disabling dbc DbC can be enabled and disabled via sysfs, serialize those with a mutex to make sure everything is done in the correct order. remove xhci_do_dbc_stop() and integrate the register write and dbc->state setting into xhci_do_stop() Signed-off-by: Mathias Nyman Link: https://patch.msgid.link/20260603091132.1110849-9-mathias.nyman@linux.intel.com Signed-off-by: Greg Kroah-Hartman --- diff --git a/drivers/usb/host/xhci-dbgcap.c b/drivers/usb/host/xhci-dbgcap.c index 6a9f73fecb73..49ae546c4103 100644 --- a/drivers/usb/host/xhci-dbgcap.c +++ b/drivers/usb/host/xhci-dbgcap.c @@ -661,17 +661,6 @@ static int xhci_do_dbc_start(struct xhci_dbc *dbc) return 0; } -static int xhci_do_dbc_stop(struct xhci_dbc *dbc) -{ - if (dbc->state == DS_DISABLED) - return -EINVAL; - - writel(0, &dbc->regs->control); - dbc->state = DS_DISABLED; - - return 0; -} - static int xhci_dbc_start(struct xhci_dbc *dbc) { int ret; @@ -683,29 +672,37 @@ static int xhci_dbc_start(struct xhci_dbc *dbc) spin_lock_irqsave(&dbc->lock, flags); ret = xhci_do_dbc_start(dbc); + if (ret) + goto err_unlock; + spin_unlock_irqrestore(&dbc->lock, flags); - if (ret) { - pm_runtime_put(dbc->dev); /* note this was self.controller */ - return ret; - } + mod_delayed_work(system_percpu_wq, &dbc->event_work, + msecs_to_jiffies(dbc->poll_interval)); - return mod_delayed_work(system_percpu_wq, &dbc->event_work, - msecs_to_jiffies(dbc->poll_interval)); + return 0; + +err_unlock: + + spin_unlock_irqrestore(&dbc->lock, flags); + pm_runtime_put(dbc->dev); /* note this was self.controller */ + + return ret; } static void xhci_dbc_stop(struct xhci_dbc *dbc) { - int ret; unsigned long flags; WARN_ON(!dbc); + spin_lock(&dbc->lock); + switch (dbc->state) { case DS_DISABLED: + spin_unlock(&dbc->lock); return; case DS_CONFIGURED: - spin_lock(&dbc->lock); xhci_dbc_flush_requests(dbc); spin_unlock(&dbc->lock); @@ -713,19 +710,20 @@ static void xhci_dbc_stop(struct xhci_dbc *dbc) dbc->driver->disconnect(dbc); break; default: + spin_unlock(&dbc->lock); break; } cancel_delayed_work_sync(&dbc->event_work); spin_lock_irqsave(&dbc->lock, flags); - ret = xhci_do_dbc_stop(dbc); + writel(0, &dbc->regs->control); + dbc->state = DS_DISABLED; spin_unlock_irqrestore(&dbc->lock, flags); - if (ret) - return; xhci_dbc_mem_cleanup(dbc); - pm_runtime_put_sync(dbc->dev); /* note, was self.controller */ + + pm_runtime_put(dbc->dev); /* note, was self.controller */ } static void @@ -1072,12 +1070,17 @@ static ssize_t dbc_store(struct device *dev, xhci = hcd_to_xhci(dev_get_drvdata(dev)); dbc = xhci->dbc; - if (sysfs_streq(buf, "enable")) + if (sysfs_streq(buf, "enable")) { + mutex_lock(&dbc->enable_mutex); xhci_dbc_start(dbc); - else if (sysfs_streq(buf, "disable")) + mutex_unlock(&dbc->enable_mutex); + } else if (sysfs_streq(buf, "disable")) { + mutex_lock(&dbc->enable_mutex); xhci_dbc_stop(dbc); - else + mutex_unlock(&dbc->enable_mutex); + } else { return -EINVAL; + } return count; } @@ -1443,6 +1446,7 @@ xhci_alloc_dbc(struct device *dev, void __iomem *base, const struct dbc_driver * INIT_DELAYED_WORK(&dbc->event_work, xhci_dbc_handle_events); spin_lock_init(&dbc->lock); + mutex_init(&dbc->enable_mutex); ret = sysfs_create_groups(&dev->kobj, dbc_dev_groups); if (ret) @@ -1460,8 +1464,9 @@ void xhci_dbc_remove(struct xhci_dbc *dbc) if (!dbc) return; /* stop hw, stop wq and call dbc->ops->stop() */ + mutex_lock(&dbc->enable_mutex); xhci_dbc_stop(dbc); - + mutex_unlock(&dbc->enable_mutex); /* remove sysfs files */ sysfs_remove_groups(&dbc->dev->kobj, dbc_dev_groups); @@ -1514,6 +1519,8 @@ int xhci_dbc_suspend(struct xhci_hcd *xhci) if (!dbc) return 0; + mutex_lock(&dbc->enable_mutex); + switch (dbc->state) { case DS_ENABLED: case DS_CONNECTED: @@ -1525,6 +1532,7 @@ int xhci_dbc_suspend(struct xhci_hcd *xhci) } xhci_dbc_stop(dbc); + mutex_unlock(&dbc->enable_mutex); return 0; } @@ -1537,11 +1545,15 @@ int xhci_dbc_resume(struct xhci_hcd *xhci) if (!dbc) return 0; + mutex_lock(&dbc->enable_mutex); + if (dbc->resume_required) { dbc->resume_required = 0; xhci_dbc_start(dbc); } + mutex_unlock(&dbc->enable_mutex); + return ret; } #endif /* CONFIG_PM */ diff --git a/drivers/usb/host/xhci-dbgcap.h b/drivers/usb/host/xhci-dbgcap.h index 20ae4e7617f2..f4c0169a1b4d 100644 --- a/drivers/usb/host/xhci-dbgcap.h +++ b/drivers/usb/host/xhci-dbgcap.h @@ -140,6 +140,7 @@ struct dbc_driver { struct xhci_dbc { spinlock_t lock; /* device access */ + struct mutex enable_mutex; struct device *dev; struct xhci_hcd *xhci; struct dbc_regs __iomem *regs;