linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alan Stern <stern@rowland.harvard.edu>
To: Pavan Kondeti <quic_pkondeti@quicinc.com>
Cc: Felipe Balbi <balbi@kernel.org>,
	Greg KH <gregkh@linuxfoundation.org>,
	USB mailing list <linux-usb@vger.kernel.org>
Subject: Re: [RFC PATCH 4/4] USB: gadget: Add a new bus for gadgets
Date: Wed, 23 Mar 2022 09:14:57 -0400	[thread overview]
Message-ID: <YjsdUTQsuWwYT0AX@rowland.harvard.edu> (raw)
In-Reply-To: <20220323065528.GA32029@hu-pkondeti-hyd.qualcomm.com>

On Wed, Mar 23, 2022 at 12:25:28PM +0530, Pavan Kondeti wrote:
> Hi Alan,
> 
> On Sun, Mar 20, 2022 at 03:51:04PM -0400, Alan Stern wrote:
> > This patch adds a "gadget" bus and uses it for registering gadgets and
> > their drivers.  From now on, bindings will be managed by the driver
> > core rather than through ad-hoc manipulations in the UDC core.
> > 
> > As part of this change, the driver_pending_list is removed.  The UDC
> > core won't need to keep track of unbound drivers for later binding,
> > because the driver core handles all of that for us.
> > 
> > However, we do need one new feature: a way to prevent gadget drivers
> > from being bound to more than one gadget at a time.  The existing code
> > does this automatically, but the driver core doesn't -- it's perfectly
> > happy to bind a single driver to all the matching devices on the bus.
> > The patch adds a new bitflag to the usb_gadget_driver structure for
> > this purpose.
> > 
> > A nice side effect of this change is a reduction in the total lines of
> > code, since now the driver core will do part of the work that the UDC
> > used to do.
> > 
> > A possible future patch could add udc devices to the gadget bus, say
> > as a separate device type.
> 
> Can you please elaborate on this? This UDC device will be added to gadget bus
> but not bound to any driver, correct?

The UDC/gadget subsystem is designed a little strangely.  For each UDC 
hardware device, the UDC core creates _two_ software representations: a 
struct usb_udc and a struct usb_gadget.  Both of these contain an 
embedded struct device, so the physical UDC hardware corresponds to two 
software "devices".

Currently neither of these devices gets registered on any bus.  There is 
a driver associated with the usb_gadget device, but not in the usual way 
(that is, it doesn't use the normal driver-core binding mechanism).

My patch keeps both of these device structures, but it registers the 
usb_gadget device on the new gadget bus and uses the driver core to do 
normal binding.  The usb_udc device still is not registered on any bus 
and does not have a driver.

> > Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
> > 
> > ---
> > 
> >  drivers/usb/gadget/udc/core.c |  248 +++++++++++++++++++-----------------------
> >  include/linux/usb/gadget.h    |   26 ++--
> >  2 files changed, 135 insertions(+), 139 deletions(-)
> > 
> 
> <snip>
> 
> >  
> >  /* ------------------------------------------------------------------------- */
> >  
> > -static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
> > +static int gadget_match_driver(struct device *dev, struct device_driver *drv)
> >  {
> > -	int ret;
> > +	struct usb_gadget *gadget = dev_to_usb_gadget(dev);
> > +	struct usb_udc *udc = gadget->udc;
> > +	struct usb_gadget_driver *driver = container_of(drv,
> > +			struct usb_gadget_driver, driver);
> >  
> > -	dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
> > -			driver->function);
> > +	/* If the driver specifies a udc_name, it must match the UDC's name */
> > +	if (driver->udc_name &&
> > +			strcmp(driver->udc_name, dev_name(&udc->dev)) != 0)
> > +		return 0;
> >  
> > +	/* Otherwise any gadget driver matches any UDC */
> > +	return 1;
> > +}
> > +
> 
> Would it be better if we add the driver->is_bound check here so that probe is
> not invoked? your patch checks it later at the probe.

Yes, you're right; the check could be moved here.  But this is only 
because the driver core holds the device lock the whole time while it 
does matching and probing.  If the lock were held during probing but not 
matching, it would then be possible for two processes to concurrently 
bind the same driver to two gadgets.

As far as I know, the driver core does not promise to hold the device 
lock during both matching and probing, so it may not be safe to depend 
on this behavior.  Maybe I'm wrong about this...

On the other hand, it wouldn't hurt to do the is_bound check in both 
places; it's a very cheap operation.  Thanks for the suggestion.

Alan Stern

  reply	other threads:[~2022-03-23 13:15 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-20 19:45 [RFC PATCH 0/4] USB: gadget: Create a bus for gadgets Alan Stern
2022-03-20 19:47 ` [RFC PATCH 1/4] USB: gadget: Rename usb_gadget_probe_driver() Alan Stern
2022-03-20 19:48   ` [RFC PATCH 2/4] USB: gadget: Register udc before gadget Alan Stern
2022-03-20 19:50     ` [RFC PATCH 3/4] USB: gadget: Fix mistakes in UDC core kerneldoc Alan Stern
2022-03-20 19:51       ` [RFC PATCH 4/4] USB: gadget: Add a new bus for gadgets Alan Stern
2022-03-23  6:55         ` Pavan Kondeti
2022-03-23 13:14           ` Alan Stern [this message]
2022-03-22 12:57   ` [RFC PATCH 1/4] USB: gadget: Rename usb_gadget_probe_driver() Jun Li
2022-03-22 14:37     ` Alan Stern
2022-04-22 13:30 ` [RFC PATCH 0/4] USB: gadget: Create a bus for gadgets Greg KH
2022-04-24  0:42   ` [PATCH 1/4] USB: gadget: Rename usb_gadget_probe_driver() Alan Stern
2022-04-24  1:33     ` [PATCH 2/4] USB: gadget: Register udc before gadget Alan Stern
2022-04-24  1:34       ` [PATCH 3/4] USB: gadget: Fix mistakes in UDC core kerneldoc Alan Stern
2022-04-24  1:35         ` [PATCH 4/4] USB: gadget: Add a new bus for gadgets Alan Stern
2022-05-03 10:14           ` Geert Uytterhoeven
2022-05-03 14:54             ` Alan Stern
2022-05-03 15:27               ` Geert Uytterhoeven
2022-05-03 15:48                 ` Alan Stern
2022-05-04 14:40                   ` Greg KH
2022-05-07 15:36                   ` Alan Stern
2022-05-09  7:46                     ` Geert Uytterhoeven
2022-05-09 14:15                       ` Alan Stern
2022-05-09 14:42                         ` Geert Uytterhoeven
2022-05-09 15:05                           ` Alan Stern
2022-05-09 16:23                             ` Greg KH
2022-05-09 16:47                               ` Alan Stern
2022-05-10  7:52                                 ` Greg KH
2022-05-10 15:51                                   ` [PATCH] USB: gadget: Add ID numbers to gadget names Alan Stern
2022-05-11 15:17                                     ` Greg KH
2022-05-11 16:58                                       ` Greg KH
2022-04-24  1:40   ` [RFC PATCH 0/4] USB: gadget: Create a bus for gadgets Alan Stern

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=YjsdUTQsuWwYT0AX@rowland.harvard.edu \
    --to=stern@rowland.harvard.edu \
    --cc=balbi@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=quic_pkondeti@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;
as well as URLs for NNTP newsgroup(s).