Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit 3852f56

Browse files
Badhri Jagan Sridharangregkh
authored andcommitted
usb: gadget: udc: core: Invoke usb_gadget_connect only when started
commit 0db213e upstream. usb_udc_connect_control does not check to see if the udc has already been started. This causes gadget->ops->pullup to be called through usb_gadget_connect when invoked from usb_udc_vbus_handler even before usb_gadget_udc_start is called. Guard this by checking for udc->started in usb_udc_connect_control before invoking usb_gadget_connect. Guarding udc->vbus, udc->started, gadget->connect, gadget->deactivate related functions with connect_lock. usb_gadget_connect_locked, usb_gadget_disconnect_locked, usb_udc_connect_control_locked, usb_gadget_udc_start_locked, usb_gadget_udc_stop_locked are called with this lock held as they can be simulataneously invoked from different code paths. Adding an additional check to make sure udc is started(udc->started) before pullup callback is invoked. Fixes: 628ef0d ("usb: udc: add usb_udc_vbus_handler") Cc: stable@vger.kernel.org Signed-off-by: Badhri Jagan Sridharan <badhri@google.com> Link: https://lore.kernel.org/r/20230407030741.3163220-1-badhri@google.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b252702 commit 3852f56

File tree

1 file changed

+104
-44
lines changed

1 file changed

+104
-44
lines changed

drivers/usb/gadget/udc/core.c

Lines changed: 104 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ static struct bus_type gadget_bus_type;
3737
* @vbus: for udcs who care about vbus status, this value is real vbus status;
3838
* for udcs who do not care about vbus status, this value is always true
3939
* @started: the UDC's started state. True if the UDC had started.
40+
* @connect_lock: protects udc->vbus, udc->started, gadget->connect, gadget->deactivate related
41+
* functions. usb_gadget_connect_locked, usb_gadget_disconnect_locked,
42+
* usb_udc_connect_control_locked, usb_gadget_udc_start_locked, usb_gadget_udc_stop_locked are
43+
* called with this lock held.
4044
*
4145
* This represents the internal data structure which is used by the UDC-class
4246
* to hold information about udc driver and gadget together.
@@ -48,6 +52,7 @@ struct usb_udc {
4852
struct list_head list;
4953
bool vbus;
5054
bool started;
55+
struct mutex connect_lock;
5156
};
5257

5358
static struct class *udc_class;
@@ -660,17 +665,9 @@ int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
660665
}
661666
EXPORT_SYMBOL_GPL(usb_gadget_vbus_disconnect);
662667

663-
/**
664-
* usb_gadget_connect - software-controlled connect to USB host
665-
* @gadget:the peripheral being connected
666-
*
667-
* Enables the D+ (or potentially D-) pullup. The host will start
668-
* enumerating this gadget when the pullup is active and a VBUS session
669-
* is active (the link is powered).
670-
*
671-
* Returns zero on success, else negative errno.
672-
*/
673-
int usb_gadget_connect(struct usb_gadget *gadget)
668+
/* Internal version of usb_gadget_connect needs to be called with connect_lock held. */
669+
static int usb_gadget_connect_locked(struct usb_gadget *gadget)
670+
__must_hold(&gadget->udc->connect_lock)
674671
{
675672
int ret = 0;
676673

@@ -679,10 +676,12 @@ int usb_gadget_connect(struct usb_gadget *gadget)
679676
goto out;
680677
}
681678

682-
if (gadget->deactivated) {
679+
if (gadget->deactivated || !gadget->udc->started) {
683680
/*
684681
* If gadget is deactivated we only save new state.
685682
* Gadget will be connected automatically after activation.
683+
*
684+
* udc first needs to be started before gadget can be pulled up.
686685
*/
687686
gadget->connected = true;
688687
goto out;
@@ -697,22 +696,32 @@ int usb_gadget_connect(struct usb_gadget *gadget)
697696

698697
return ret;
699698
}
700-
EXPORT_SYMBOL_GPL(usb_gadget_connect);
701699

702700
/**
703-
* usb_gadget_disconnect - software-controlled disconnect from USB host
704-
* @gadget:the peripheral being disconnected
705-
*
706-
* Disables the D+ (or potentially D-) pullup, which the host may see
707-
* as a disconnect (when a VBUS session is active). Not all systems
708-
* support software pullup controls.
701+
* usb_gadget_connect - software-controlled connect to USB host
702+
* @gadget:the peripheral being connected
709703
*
710-
* Following a successful disconnect, invoke the ->disconnect() callback
711-
* for the current gadget driver so that UDC drivers don't need to.
704+
* Enables the D+ (or potentially D-) pullup. The host will start
705+
* enumerating this gadget when the pullup is active and a VBUS session
706+
* is active (the link is powered).
712707
*
713708
* Returns zero on success, else negative errno.
714709
*/
715-
int usb_gadget_disconnect(struct usb_gadget *gadget)
710+
int usb_gadget_connect(struct usb_gadget *gadget)
711+
{
712+
int ret;
713+
714+
mutex_lock(&gadget->udc->connect_lock);
715+
ret = usb_gadget_connect_locked(gadget);
716+
mutex_unlock(&gadget->udc->connect_lock);
717+
718+
return ret;
719+
}
720+
EXPORT_SYMBOL_GPL(usb_gadget_connect);
721+
722+
/* Internal version of usb_gadget_disconnect needs to be called with connect_lock held. */
723+
static int usb_gadget_disconnect_locked(struct usb_gadget *gadget)
724+
__must_hold(&gadget->udc->connect_lock)
716725
{
717726
int ret = 0;
718727

@@ -724,10 +733,12 @@ int usb_gadget_disconnect(struct usb_gadget *gadget)
724733
if (!gadget->connected)
725734
goto out;
726735

727-
if (gadget->deactivated) {
736+
if (gadget->deactivated || !gadget->udc->started) {
728737
/*
729738
* If gadget is deactivated we only save new state.
730739
* Gadget will stay disconnected after activation.
740+
*
741+
* udc should have been started before gadget being pulled down.
731742
*/
732743
gadget->connected = false;
733744
goto out;
@@ -747,6 +758,30 @@ int usb_gadget_disconnect(struct usb_gadget *gadget)
747758

748759
return ret;
749760
}
761+
762+
/**
763+
* usb_gadget_disconnect - software-controlled disconnect from USB host
764+
* @gadget:the peripheral being disconnected
765+
*
766+
* Disables the D+ (or potentially D-) pullup, which the host may see
767+
* as a disconnect (when a VBUS session is active). Not all systems
768+
* support software pullup controls.
769+
*
770+
* Following a successful disconnect, invoke the ->disconnect() callback
771+
* for the current gadget driver so that UDC drivers don't need to.
772+
*
773+
* Returns zero on success, else negative errno.
774+
*/
775+
int usb_gadget_disconnect(struct usb_gadget *gadget)
776+
{
777+
int ret;
778+
779+
mutex_lock(&gadget->udc->connect_lock);
780+
ret = usb_gadget_disconnect_locked(gadget);
781+
mutex_unlock(&gadget->udc->connect_lock);
782+
783+
return ret;
784+
}
750785
EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
751786

752787
/**
@@ -767,10 +802,11 @@ int usb_gadget_deactivate(struct usb_gadget *gadget)
767802
if (gadget->deactivated)
768803
goto out;
769804

805+
mutex_lock(&gadget->udc->connect_lock);
770806
if (gadget->connected) {
771-
ret = usb_gadget_disconnect(gadget);
807+
ret = usb_gadget_disconnect_locked(gadget);
772808
if (ret)
773-
goto out;
809+
goto unlock;
774810

775811
/*
776812
* If gadget was being connected before deactivation, we want
@@ -780,6 +816,8 @@ int usb_gadget_deactivate(struct usb_gadget *gadget)
780816
}
781817
gadget->deactivated = true;
782818

819+
unlock:
820+
mutex_unlock(&gadget->udc->connect_lock);
783821
out:
784822
trace_usb_gadget_deactivate(gadget, ret);
785823

@@ -803,14 +841,16 @@ int usb_gadget_activate(struct usb_gadget *gadget)
803841
if (!gadget->deactivated)
804842
goto out;
805843

844+
mutex_lock(&gadget->udc->connect_lock);
806845
gadget->deactivated = false;
807846

808847
/*
809848
* If gadget has been connected before deactivation, or became connected
810849
* while it was being deactivated, we call usb_gadget_connect().
811850
*/
812851
if (gadget->connected)
813-
ret = usb_gadget_connect(gadget);
852+
ret = usb_gadget_connect_locked(gadget);
853+
mutex_unlock(&gadget->udc->connect_lock);
814854

815855
out:
816856
trace_usb_gadget_activate(gadget, ret);
@@ -1051,12 +1091,13 @@ EXPORT_SYMBOL_GPL(usb_gadget_set_state);
10511091

10521092
/* ------------------------------------------------------------------------- */
10531093

1054-
static void usb_udc_connect_control(struct usb_udc *udc)
1094+
/* Acquire connect_lock before calling this function. */
1095+
static void usb_udc_connect_control_locked(struct usb_udc *udc) __must_hold(&udc->connect_lock)
10551096
{
1056-
if (udc->vbus)
1057-
usb_gadget_connect(udc->gadget);
1097+
if (udc->vbus && udc->started)
1098+
usb_gadget_connect_locked(udc->gadget);
10581099
else
1059-
usb_gadget_disconnect(udc->gadget);
1100+
usb_gadget_disconnect_locked(udc->gadget);
10601101
}
10611102

10621103
/**
@@ -1072,10 +1113,12 @@ void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
10721113
{
10731114
struct usb_udc *udc = gadget->udc;
10741115

1116+
mutex_lock(&udc->connect_lock);
10751117
if (udc) {
10761118
udc->vbus = status;
1077-
usb_udc_connect_control(udc);
1119+
usb_udc_connect_control_locked(udc);
10781120
}
1121+
mutex_unlock(&udc->connect_lock);
10791122
}
10801123
EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
10811124

@@ -1097,7 +1140,7 @@ void usb_gadget_udc_reset(struct usb_gadget *gadget,
10971140
EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
10981141

10991142
/**
1100-
* usb_gadget_udc_start - tells usb device controller to start up
1143+
* usb_gadget_udc_start_locked - tells usb device controller to start up
11011144
* @udc: The UDC to be started
11021145
*
11031146
* This call is issued by the UDC Class driver when it's about
@@ -1108,8 +1151,11 @@ EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
11081151
* necessary to have it powered on.
11091152
*
11101153
* Returns zero on success, else negative errno.
1154+
*
1155+
* Caller should acquire connect_lock before invoking this function.
11111156
*/
1112-
static inline int usb_gadget_udc_start(struct usb_udc *udc)
1157+
static inline int usb_gadget_udc_start_locked(struct usb_udc *udc)
1158+
__must_hold(&udc->connect_lock)
11131159
{
11141160
int ret;
11151161

@@ -1126,7 +1172,7 @@ static inline int usb_gadget_udc_start(struct usb_udc *udc)
11261172
}
11271173

11281174
/**
1129-
* usb_gadget_udc_stop - tells usb device controller we don't need it anymore
1175+
* usb_gadget_udc_stop_locked - tells usb device controller we don't need it anymore
11301176
* @udc: The UDC to be stopped
11311177
*
11321178
* This call is issued by the UDC Class driver after calling
@@ -1135,8 +1181,11 @@ static inline int usb_gadget_udc_start(struct usb_udc *udc)
11351181
* The details are implementation specific, but it can go as
11361182
* far as powering off UDC completely and disable its data
11371183
* line pullups.
1184+
*
1185+
* Caller should acquire connect lock before invoking this function.
11381186
*/
1139-
static inline void usb_gadget_udc_stop(struct usb_udc *udc)
1187+
static inline void usb_gadget_udc_stop_locked(struct usb_udc *udc)
1188+
__must_hold(&udc->connect_lock)
11401189
{
11411190
if (!udc->started) {
11421191
dev_err(&udc->dev, "UDC had already stopped\n");
@@ -1295,6 +1344,7 @@ int usb_add_gadget(struct usb_gadget *gadget)
12951344

12961345
udc->gadget = gadget;
12971346
gadget->udc = udc;
1347+
mutex_init(&udc->connect_lock);
12981348

12991349
udc->started = false;
13001350

@@ -1496,11 +1546,15 @@ static int gadget_bind_driver(struct device *dev)
14961546
if (ret)
14971547
goto err_bind;
14981548

1499-
ret = usb_gadget_udc_start(udc);
1500-
if (ret)
1549+
mutex_lock(&udc->connect_lock);
1550+
ret = usb_gadget_udc_start_locked(udc);
1551+
if (ret) {
1552+
mutex_unlock(&udc->connect_lock);
15011553
goto err_start;
1554+
}
15021555
usb_gadget_enable_async_callbacks(udc);
1503-
usb_udc_connect_control(udc);
1556+
usb_udc_connect_control_locked(udc);
1557+
mutex_unlock(&udc->connect_lock);
15041558

15051559
kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
15061560
return 0;
@@ -1531,12 +1585,14 @@ static void gadget_unbind_driver(struct device *dev)
15311585

15321586
kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
15331587

1534-
usb_gadget_disconnect(gadget);
1588+
mutex_lock(&udc->connect_lock);
1589+
usb_gadget_disconnect_locked(gadget);
15351590
usb_gadget_disable_async_callbacks(udc);
15361591
if (gadget->irq)
15371592
synchronize_irq(gadget->irq);
15381593
udc->driver->unbind(gadget);
1539-
usb_gadget_udc_stop(udc);
1594+
usb_gadget_udc_stop_locked(udc);
1595+
mutex_unlock(&udc->connect_lock);
15401596

15411597
mutex_lock(&udc_lock);
15421598
driver->is_bound = false;
@@ -1622,11 +1678,15 @@ static ssize_t soft_connect_store(struct device *dev,
16221678
}
16231679

16241680
if (sysfs_streq(buf, "connect")) {
1625-
usb_gadget_udc_start(udc);
1626-
usb_gadget_connect(udc->gadget);
1681+
mutex_lock(&udc->connect_lock);
1682+
usb_gadget_udc_start_locked(udc);
1683+
usb_gadget_connect_locked(udc->gadget);
1684+
mutex_unlock(&udc->connect_lock);
16271685
} else if (sysfs_streq(buf, "disconnect")) {
1628-
usb_gadget_disconnect(udc->gadget);
1629-
usb_gadget_udc_stop(udc);
1686+
mutex_lock(&udc->connect_lock);
1687+
usb_gadget_disconnect_locked(udc->gadget);
1688+
usb_gadget_udc_stop_locked(udc);
1689+
mutex_unlock(&udc->connect_lock);
16301690
} else {
16311691
dev_err(dev, "unsupported command '%s'\n", buf);
16321692
ret = -EINVAL;

0 commit comments

Comments
 (0)