From: Heikki Krogerus <heikki.krogerus@linux.intel.com>
To: Linyu Yuan <quic_linyyuan@quicinc.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-usb@vger.kernel.org, Jack Pham <quic_jackp@quicinc.com>
Subject: Re: [PATCH v5 3/3] usb: typec: ucsi: Wait for the USB role switches
Date: Mon, 25 Apr 2022 11:14:24 +0300 [thread overview]
Message-ID: <YmZYYCorXsB3fM6T@kuha.fi.intel.com> (raw)
In-Reply-To: <1650597022-19793-4-git-send-email-quic_linyyuan@quicinc.com>
On Fri, Apr 22, 2022 at 11:10:22AM +0800, Linyu Yuan wrote:
> When role switch module probe late than ucsi module,
> fwnode_usb_role_switch_get() will return -EPROBE_DEFER,
> it is better to restart ucsi init work to find
> it again every 100ms, total wait time is 10 second.
>
> It also means change ucsi init work to delayed_work.
>
> Signed-off-by: Linyu Yuan <quic_linyyuan@quicinc.com>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> ---
> v2: keep original con->num in debug log
> v3: change return value from -EAGAIN to PTR_ERR()
> v4: change subject line,
> add counter for retry limit,
> correct commit descripton to match change in V3
> v5: small update of commit description
>
> drivers/usb/typec/ucsi/ucsi.c | 32 ++++++++++++++++++++------------
> drivers/usb/typec/ucsi/ucsi.h | 6 +++++-
> 2 files changed, 25 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
> index ce9192e..11f8808 100644
> --- a/drivers/usb/typec/ucsi/ucsi.c
> +++ b/drivers/usb/typec/ucsi/ucsi.c
> @@ -1053,6 +1053,14 @@ static int ucsi_register_port(struct ucsi *ucsi, int index)
> con->num = index + 1;
> con->ucsi = ucsi;
>
> + cap->fwnode = ucsi_find_fwnode(con);
> + con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
> + if (IS_ERR(con->usb_role_sw)) {
> + dev_err(ucsi->dev, "con%d: failed to get usb role switch\n",
> + con->num);
> + return PTR_ERR(con->usb_role_sw);
> + }
> +
> /* Delay other interactions with the con until registration is complete */
> mutex_lock(&con->lock);
>
> @@ -1088,7 +1096,6 @@ static int ucsi_register_port(struct ucsi *ucsi, int index)
> if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
> *accessory = TYPEC_ACCESSORY_DEBUG;
>
> - cap->fwnode = ucsi_find_fwnode(con);
> cap->driver_data = con;
> cap->ops = &ucsi_ops;
>
> @@ -1147,13 +1154,6 @@ static int ucsi_register_port(struct ucsi *ucsi, int index)
> ucsi_port_psy_changed(con);
> }
>
> - con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
> - if (IS_ERR(con->usb_role_sw)) {
> - dev_err(ucsi->dev, "con%d: failed to get usb role switch\n",
> - con->num);
> - con->usb_role_sw = NULL;
> - }
> -
> /* Only notify USB controller if partner supports USB data */
> if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
> u_role = USB_ROLE_NONE;
> @@ -1286,12 +1286,20 @@ static int ucsi_init(struct ucsi *ucsi)
>
> static void ucsi_init_work(struct work_struct *work)
> {
> - struct ucsi *ucsi = container_of(work, struct ucsi, work);
> + struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
> int ret;
>
> ret = ucsi_init(ucsi);
> if (ret)
> dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
> +
> + if (ret == -EPROBE_DEFER) {
> + if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT)
> + return;
> +
> + queue_delayed_work(system_long_wq, &ucsi->work,
> + UCSI_ROLE_SWITCH_INTERVAL);
> + }
> }
>
> /**
> @@ -1331,7 +1339,7 @@ struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
> if (!ucsi)
> return ERR_PTR(-ENOMEM);
>
> - INIT_WORK(&ucsi->work, ucsi_init_work);
> + INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
> mutex_init(&ucsi->ppm_lock);
> ucsi->dev = dev;
> ucsi->ops = ops;
> @@ -1366,7 +1374,7 @@ int ucsi_register(struct ucsi *ucsi)
> if (!ucsi->version)
> return -ENODEV;
>
> - queue_work(system_long_wq, &ucsi->work);
> + queue_delayed_work(system_long_wq, &ucsi->work, 0);
>
> return 0;
> }
> @@ -1383,7 +1391,7 @@ void ucsi_unregister(struct ucsi *ucsi)
> u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
>
> /* Make sure that we are not in the middle of driver initialization */
> - cancel_work_sync(&ucsi->work);
> + cancel_delayed_work_sync(&ucsi->work);
>
> /* Disable notifications */
> ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
> diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h
> index 280f1e1..8eb391e 100644
> --- a/drivers/usb/typec/ucsi/ucsi.h
> +++ b/drivers/usb/typec/ucsi/ucsi.h
> @@ -287,7 +287,11 @@ struct ucsi {
> struct ucsi_capability cap;
> struct ucsi_connector *connector;
>
> - struct work_struct work;
> + struct delayed_work work;
> + int work_count;
> +#define UCSI_ROLE_SWITCH_RETRY_PER_HZ 10
> +#define UCSI_ROLE_SWITCH_INTERVAL (HZ / UCSI_ROLE_SWITCH_RETRY_PER_HZ)
> +#define UCSI_ROLE_SWITCH_WAIT_COUNT (10 * UCSI_ROLE_SWITCH_RETRY_PER_HZ)
>
> /* PPM Communication lock */
> struct mutex ppm_lock;
> --
> 2.7.4
--
heikki
prev parent reply other threads:[~2022-04-25 8:15 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-22 3:10 [PATCH v5 0/3] usb: typec: ucsi: allow retry to find role switch Linyu Yuan
2022-04-22 3:10 ` [PATCH v5 1/3] usb: typec: ucsi: add a common function ucsi_unregister_connectors() Linyu Yuan
2022-04-25 8:09 ` Heikki Krogerus
2022-04-25 8:23 ` Linyu Yuan (QUIC)
2022-04-22 3:10 ` [PATCH v5 2/3] usb: typec: ucsi: do not allocate one extra unused connector Linyu Yuan
2022-04-25 8:13 ` Heikki Krogerus
2022-04-25 8:23 ` Linyu Yuan (QUIC)
2022-04-22 3:10 ` [PATCH v5 3/3] usb: typec: ucsi: Wait for the USB role switches Linyu Yuan
2022-04-25 8:14 ` Heikki Krogerus [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=YmZYYCorXsB3fM6T@kuha.fi.intel.com \
--to=heikki.krogerus@linux.intel.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-usb@vger.kernel.org \
--cc=quic_jackp@quicinc.com \
--cc=quic_linyyuan@quicinc.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