Skip to content

Commit

Permalink
USB: gadget: Register udc before gadget
Browse files Browse the repository at this point in the history
In preparation for adding a "gadget" bus, this patch reverses the
order of registration of udc and gadget devices in usb_add_gadget().

The current code adds the gadget device first, probably because that
was more convenient at the time and the order didn't really matter.
But with the upcoming change, adding the gadget will cause driver
probing to occur.  Unwinding that on the error pathway will become
much more obtrusive, not to mention the fact that a gadget driver
might not work properly before the udc is registered.  It's better to
register the udc device first, particularly since that doesn't involve
a bus or driver binding and therefore is simpler to unwind.

For symmetry, the order of unregistration in usb_del_gadget() is
likewise reversed.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Link: https://lore.kernel.org/r/YmSo6fU1FlNq8cOZ@rowland.harvard.edu
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Alan Stern authored and Greg Kroah-Hartman committed Apr 26, 2022
1 parent af1969a commit 6ebb449
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions drivers/usb/gadget/udc/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1308,10 +1308,6 @@ int usb_add_gadget(struct usb_gadget *gadget)
if (ret)
goto err_put_udc;

ret = device_add(&gadget->dev);
if (ret)
goto err_put_udc;

udc->gadget = gadget;
gadget->udc = udc;

Expand All @@ -1327,15 +1323,22 @@ int usb_add_gadget(struct usb_gadget *gadget)
usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
udc->vbus = true;

ret = device_add(&gadget->dev);
if (ret)
goto err_del_udc;

/* pick up one of pending gadget drivers */
ret = check_pending_gadget_drivers(udc);
if (ret)
goto err_del_udc;
goto err_del_gadget;

mutex_unlock(&udc_lock);

return 0;

err_del_gadget:
device_del(&gadget->dev);

err_del_udc:
flush_work(&gadget->work);
device_del(&udc->dev);
Expand All @@ -1344,8 +1347,6 @@ int usb_add_gadget(struct usb_gadget *gadget)
list_del(&udc->list);
mutex_unlock(&udc_lock);

device_del(&gadget->dev);

err_put_udc:
put_device(&udc->dev);

Expand Down Expand Up @@ -1469,8 +1470,8 @@ void usb_del_gadget(struct usb_gadget *gadget)

kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
flush_work(&gadget->work);
device_unregister(&udc->dev);
device_del(&gadget->dev);
device_unregister(&udc->dev);
}
EXPORT_SYMBOL_GPL(usb_del_gadget);

Expand Down

0 comments on commit 6ebb449

Please sign in to comment.