From: Peter Chen <peter.chen@kernel.org>
To: Xu Yang <xu.yang_2@nxp.com>
Cc: gregkh@linuxfoundation.org, linux-usb@vger.kernel.org,
jun.li@nxp.com, linux-imx@nxp.com
Subject: Re: [PATCH] usb: chipidea: core: handle power lost in workqueue
Date: Mon, 5 Feb 2024 12:40:37 +0800 [thread overview]
Message-ID: <20240205044037.GB1200221@nchen-desktop> (raw)
In-Reply-To: <20240119123537.3614838-1-xu.yang_2@nxp.com>
On 24-01-19 20:35:37, Xu Yang wrote:
> When power is recycled in usb controller during system power management,
> the controller will recognize it and switch role if role has been changed
> during power lost. In current design, it will be completed in resume()
> function. However, this may bring issues since usb class devices have
> their pm operations too and these device's resume() functions are still
> not being called at this point. When usb controller recognized host role
> should be stopped, these usb class devices will be removed at this point.
> But these usb class devices can't be removed in some cases, such as scsi
> devices. Since scsi driver may sync data to U-disk, however it will block
> there because scsi drvier can only handle pm request when is in suspended
> state. Therefore, there may exist a dependency between ci_resume() and usb
> class device's resume(). To break this potential dependency, we need to
> handle power lost work in a workqueue.
>
> Fixes: 74494b33211d ("usb: chipidea: core: add controller resume support when controller is powered off")
> cc: <stable@vger.kernel.org>
> Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
Acked-by: Peter Chen <peter.chen@kernel.org>
> ---
> drivers/usb/chipidea/ci.h | 2 ++
> drivers/usb/chipidea/core.c | 44 ++++++++++++++++++++-----------------
> 2 files changed, 26 insertions(+), 20 deletions(-)
>
> diff --git a/drivers/usb/chipidea/ci.h b/drivers/usb/chipidea/ci.h
> index d9bb3d3f026e..2a38e1eb6546 100644
> --- a/drivers/usb/chipidea/ci.h
> +++ b/drivers/usb/chipidea/ci.h
> @@ -176,6 +176,7 @@ struct hw_bank {
> * @enabled_otg_timer_bits: bits of enabled otg timers
> * @next_otg_timer: next nearest enabled timer to be expired
> * @work: work for role changing
> + * @power_lost_work: work for power lost handling
> * @wq: workqueue thread
> * @qh_pool: allocation pool for queue heads
> * @td_pool: allocation pool for transfer descriptors
> @@ -226,6 +227,7 @@ struct ci_hdrc {
> enum otg_fsm_timer next_otg_timer;
> struct usb_role_switch *role_switch;
> struct work_struct work;
> + struct work_struct power_lost_work;
> struct workqueue_struct *wq;
>
> struct dma_pool *qh_pool;
> diff --git a/drivers/usb/chipidea/core.c b/drivers/usb/chipidea/core.c
> index 41014f93cfdf..835bf2428dc6 100644
> --- a/drivers/usb/chipidea/core.c
> +++ b/drivers/usb/chipidea/core.c
> @@ -856,6 +856,27 @@ static int ci_extcon_register(struct ci_hdrc *ci)
> return 0;
> }
>
> +static void ci_power_lost_work(struct work_struct *work)
> +{
> + struct ci_hdrc *ci = container_of(work, struct ci_hdrc, power_lost_work);
> + enum ci_role role;
> +
> + disable_irq_nosync(ci->irq);
> + pm_runtime_get_sync(ci->dev);
> + if (!ci_otg_is_fsm_mode(ci)) {
> + role = ci_get_role(ci);
> +
> + if (ci->role != role) {
> + ci_handle_id_switch(ci);
> + } else if (role == CI_ROLE_GADGET) {
> + if (ci->is_otg && hw_read_otgsc(ci, OTGSC_BSV))
> + usb_gadget_vbus_connect(&ci->gadget);
> + }
> + }
> + pm_runtime_put_sync(ci->dev);
> + enable_irq(ci->irq);
> +}
> +
> static DEFINE_IDA(ci_ida);
>
> struct platform_device *ci_hdrc_add_device(struct device *dev,
> @@ -1045,6 +1066,8 @@ static int ci_hdrc_probe(struct platform_device *pdev)
>
> spin_lock_init(&ci->lock);
> mutex_init(&ci->mutex);
> + INIT_WORK(&ci->power_lost_work, ci_power_lost_work);
> +
> ci->dev = dev;
> ci->platdata = dev_get_platdata(dev);
> ci->imx28_write_fix = !!(ci->platdata->flags &
> @@ -1396,25 +1419,6 @@ static int ci_suspend(struct device *dev)
> return 0;
> }
>
> -static void ci_handle_power_lost(struct ci_hdrc *ci)
> -{
> - enum ci_role role;
> -
> - disable_irq_nosync(ci->irq);
> - if (!ci_otg_is_fsm_mode(ci)) {
> - role = ci_get_role(ci);
> -
> - if (ci->role != role) {
> - ci_handle_id_switch(ci);
> - } else if (role == CI_ROLE_GADGET) {
> - if (ci->is_otg && hw_read_otgsc(ci, OTGSC_BSV))
> - usb_gadget_vbus_connect(&ci->gadget);
> - }
> - }
> -
> - enable_irq(ci->irq);
> -}
> -
> static int ci_resume(struct device *dev)
> {
> struct ci_hdrc *ci = dev_get_drvdata(dev);
> @@ -1446,7 +1450,7 @@ static int ci_resume(struct device *dev)
> ci_role(ci)->resume(ci, power_lost);
>
> if (power_lost)
> - ci_handle_power_lost(ci);
> + queue_work(system_freezable_wq, &ci->power_lost_work);
>
> if (ci->supports_runtime_pm) {
> pm_runtime_disable(dev);
> --
> 2.34.1
>
>
--
Thanks,
Peter Chen
prev parent reply other threads:[~2024-02-05 4:40 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-19 12:35 [PATCH] usb: chipidea: core: handle power lost in workqueue Xu Yang
2024-02-05 4:40 ` Peter Chen [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240205044037.GB1200221@nchen-desktop \
--to=peter.chen@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=jun.li@nxp.com \
--cc=linux-imx@nxp.com \
--cc=linux-usb@vger.kernel.org \
--cc=xu.yang_2@nxp.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox