From: Badhri Jagan Sridharan <badhri@google.com>
To: gregkh@linuxfoundation.org, stern@rowland.harvard.edu,
colin.i.king@gmail.com, xuetao09@huawei.com,
quic_eserrao@quicinc.com, water.zhangjiantao@huawei.com,
peter.chen@freescale.com, balbi@ti.com, francesco@dolcini.it,
alistair@alistair23.me, stephan@gerhold.net,
bagasdotme@gmail.com, luca@z3ntu.xyz
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org,
Badhri Jagan Sridharan <badhri@google.com>,
stable <stable@kernel.org>
Subject: [PATCH v5 3/3] usb: gadget: udc: core: Prevent UDC from starting when unbound
Date: Wed, 31 May 2023 04:02:03 +0000 [thread overview]
Message-ID: <20230531040203.19295-3-badhri@google.com> (raw)
In-Reply-To: <20230531040203.19295-1-badhri@google.com>
UDC should neither be started nor pulled up unless the gadget driver is
bound. The new flag "allow_start" is now set by gadget_bind_driver()
and cleared by gadget_unbind_driver(). usb_gadget_udc_start_locked()
now checks whether allow_start is set before starting the UDC by
invoking the ->udc_start() callback.
Fixes: fc274c1e9973 ("USB: gadget: Add a new bus for gadgets")
Cc: stable <stable@kernel.org>
Signed-off-by: Badhri Jagan Sridharan <badhri@google.com>
---
v5 is the first version in this series.
---
drivers/usb/gadget/udc/core.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index 6ffe5fda8bb7..ac9d6186815d 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -37,6 +37,8 @@ static const struct bus_type gadget_bus_type;
* @vbus: for udcs who care about vbus status, this value is real vbus status;
* for udcs who do not care about vbus status, this value is always true
* @started: the UDC's started state. True if the UDC had started.
+ * @allow_start: Indicates whether UDC is allowed to start. Set/cleared by gadget_(un)bind_driver()
+ * after gadget driver is bound or unbound.
* @connect_lock: protects udc->vbus, udc->started, gadget->connect, gadget->deactivate related
* functions. usb_gadget_connect_locked, usb_gadget_disconnect_locked,
* usb_udc_connect_control_locked, usb_gadget_udc_start_locked, usb_gadget_udc_stop_locked are
@@ -52,6 +54,7 @@ struct usb_udc {
struct list_head list;
bool vbus;
bool started;
+ bool allow_start;
struct work_struct vbus_work;
struct mutex connect_lock;
};
@@ -1204,6 +1207,9 @@ static inline int usb_gadget_udc_start_locked(struct usb_udc *udc)
if (udc->started) {
dev_err(&udc->dev, "UDC had already started\n");
return -EBUSY;
+ } else if (!udc->allow_start) {
+ dev_err(&udc->dev, "UDC not allowed to start. Is gadget driver bound ?\n");
+ return -EIO;
}
ret = udc->gadget->ops->udc_start(udc->gadget, udc->driver);
@@ -1590,6 +1596,7 @@ static int gadget_bind_driver(struct device *dev)
goto err_bind;
mutex_lock(&udc->connect_lock);
+ udc->allow_start = true;
ret = usb_gadget_udc_start_locked(udc);
if (ret) {
mutex_unlock(&udc->connect_lock);
@@ -1630,6 +1637,7 @@ static void gadget_unbind_driver(struct device *dev)
cancel_work_sync(&udc->vbus_work);
mutex_lock(&udc->connect_lock);
+ udc->allow_start = false;
usb_gadget_disconnect_locked(gadget);
usb_gadget_disable_async_callbacks(udc);
if (gadget->irq)
--
2.41.0.rc0.172.g3f132b7071-goog
next prev parent reply other threads:[~2023-05-31 4:02 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-31 4:02 [PATCH v5 1/3] usb: gadget: udc: core: Offload usb_udc_vbus_handler processing Badhri Jagan Sridharan
2023-05-31 4:02 ` [PATCH v5 2/3] usb: gadget: udc: core: Invoke usb_gadget_connect only when started Badhri Jagan Sridharan
[not found] ` <618e4f17-2799-4838-a21c-184c9303bef6@rowland.harvard.edu>
2023-05-31 22:40 ` Badhri Jagan Sridharan
2023-05-31 22:55 ` Alan Stern
2023-06-02 17:34 ` Badhri Jagan Sridharan
2023-05-31 4:02 ` Badhri Jagan Sridharan [this message]
2023-05-31 17:40 ` [PATCH v5 3/3] usb: gadget: udc: core: Prevent UDC from starting when unbound Alan Stern
2023-06-02 17:26 ` Badhri Jagan Sridharan
2023-05-31 17:29 ` [PATCH v5 1/3] usb: gadget: udc: core: Offload usb_udc_vbus_handler processing Alan Stern
2023-06-02 17:23 ` Badhri Jagan Sridharan
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=20230531040203.19295-3-badhri@google.com \
--to=badhri@google.com \
--cc=alistair@alistair23.me \
--cc=bagasdotme@gmail.com \
--cc=balbi@ti.com \
--cc=colin.i.king@gmail.com \
--cc=francesco@dolcini.it \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=luca@z3ntu.xyz \
--cc=peter.chen@freescale.com \
--cc=quic_eserrao@quicinc.com \
--cc=stable@kernel.org \
--cc=stable@vger.kernel.org \
--cc=stephan@gerhold.net \
--cc=stern@rowland.harvard.edu \
--cc=water.zhangjiantao@huawei.com \
--cc=xuetao09@huawei.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
all inboxes | Powered by JetHome®