On Fri, May 19, 2023 at 10:49:49AM -0400, Alan Stern wrote:
On Fri, May 19, 2023 at 04:30:41AM +0000, Badhri Jagan Sridharan wrote:
chipidea udc calls usb_udc_vbus_handler from udc_start gadget ops causing a deadlock. Avoid this by offloading usb_udc_vbus_handler processing.
Look, this is way overkill.
usb_udc_vbus_handler() has only two jobs to do: set udc->vbus and call usb_udc_connect_control(). Furthermore, it gets called from only two drivers: chipidea and max3420.
Why not have the callers set udc->vbus themselves and then call usb_gadget_{dis}connect() directly? Then we could eliminate usb_udc_vbus_handler() entirely. And the unnecessary calls -- the ones causing deadlocks -- from within udc_start() and udc_stop() handlers can be removed with no further consequence.
This approach simplifies and removes code. Whereas your approach complicates and adds code for no good reason.
I changed my mind.
After looking more closely, I found the comment in gadget.h about ->disconnect() callbacks happening in interrupt context. This means we cannot use a mutex to protect the associated state, and therefore the connect_lock _must_ be a spinlock, not a mutex.
This also probably means that udc_start and udc_stop callbacks should not be invoked with the lock held. In fact, you might want to avoid using the lock at all with gadget_bind_driver() and gadget_unbind_driver() -- use it only in the functions that these routines call.
So it appears the whole connect_lock thing needs to be redesigned with these ideas in mind. However, it's still true that the UDC drivers shouldn't try to set the connection state from within their udc_start and udc_stop callbacks, because the core takes care of this automatically.
Alan Stern