Linux USB
 help / color / mirror / Atom feed
* dwc3: unusual handling of setup requests with wLength == 0
@ 2023-08-18  0:15 Andrey Konovalov
  2023-08-18  1:08 ` Thinh Nguyen
  0 siblings, 1 reply; 37+ messages in thread
From: Andrey Konovalov @ 2023-08-18  0:15 UTC (permalink / raw)
  To: Alan Stern, Thinh Nguyen; +Cc: Felipe Balbi, Greg Kroah-Hartman, USB list, LKML

Hi Alan and Thinh,

I have been testing Raw Gadget with the dwc3 UDC driver and stumbled
upon an issue related to how dwc3 handles setup requests with wLength
== 0.

When running a simple Raw Gadget-based keyboard emulator [1],
everything works as expected until the point when the host sends a
SET_CONFIGURATION request, which has wLength == 0.

For setup requests with wLength != 0, just like the other UDC drivers
I tested, dwc3 calls the gadget driver's ->setup() callback and then
waits until the gadget driver queues an URB to EP0 as a response.

However, for a setup request with wLength == 0, dwc3 does not wait
until the gadget driver queues an URB to ack the transfer. It appears
that dwc3 just acks the request internally and then proceeds with
calling the ->setup() callback for the next request received from the
host. This confuses Raw Gadget, as it does not expect to get a new
->setup() call before it explicitly acks the previous one by queuing
an URB. As a result, the emulation fails.

I suspect this issue has not been observed with other gadget drivers,
as they queue an URB immediately after receiving a ->setup() call:
dwc3 appears to somehow correctly handle this internally even though
it acks the transfer by itself. But the timings with Raw Gadget are
different, as it requires userspace to ack the transfer. Sometimes
though, the Raw Gadget-based emulator also manages to queue an URB
before the next request is received from the host and the enumeration
continues properly (until the next request with wLength == 0).

What do you think would be the best approach to deal with this?

Can this be considered a bug in dwc3 that should be fixed? There's a
seemingly related comment in dwc3 code [2], but I'm not familiar
enough with its internals to understand whether this is what leads to
the issue I'm seeing.

Or should I adapt Raw Gadget to handle this unusual dwc3 behavior?
This might be tricky to do, as I cannot change the existing userspace
API.

On a side note, as an experiment, I tried returning
USB_GADGET_DELAYED_STATUS from the Raw Gadget's ->setup() callback if
the UDC driver calls it too early: some UDC drivers, including dwc3,
appear to contain a special handling for this return value. However,
that didn't work out. Perhaps, I misunderstand the meaning of this
value.

Thank you!

[1] https://github.com/xairy/raw-gadget/blob/master/examples/keyboard.c
[2] https://elixir.bootlin.com/linux/v6.5-rc6/source/drivers/usb/dwc3/ep0.c#L145

^ permalink raw reply	[flat|nested] 37+ messages in thread

end of thread, other threads:[~2023-09-05 22:53 UTC | newest]

Thread overview: 37+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-18  0:15 dwc3: unusual handling of setup requests with wLength == 0 Andrey Konovalov
2023-08-18  1:08 ` Thinh Nguyen
2023-08-18  2:37   ` Alan Stern
2023-08-18  3:10     ` Thinh Nguyen
2023-08-18  3:26       ` Thinh Nguyen
2023-08-18  3:42       ` Alan Stern
2023-08-18 19:49         ` Thinh Nguyen
2023-08-18 20:46           ` Thinh Nguyen
2023-08-18 23:06           ` Alan Stern
2023-08-19  0:06             ` Thinh Nguyen
2023-08-19  1:54               ` Andrey Konovalov
2023-08-20 14:20               ` Alan Stern
2023-08-21 16:13                 ` Andrey Konovalov
2023-08-21 17:25                   ` Alan Stern
2023-08-23  2:05                     ` Thinh Nguyen
2023-08-23  2:30                     ` Andrey Konovalov
2023-08-23 15:48                       ` Alan Stern
2023-08-23 17:18                         ` Thinh Nguyen
2023-08-25  1:36                           ` Andrey Konovalov
2023-08-25  2:08                             ` Alan Stern
2023-08-23  2:14                 ` Thinh Nguyen
2023-08-23 15:17                   ` Alan Stern
2023-08-23 17:59                     ` Thinh Nguyen
2023-08-23 19:19                       ` Alan Stern
2023-08-23 22:22                         ` Thinh Nguyen
2023-08-24  2:21                           ` Alan Stern
2023-08-26  1:20                             ` Thinh Nguyen
2023-08-26  3:10                               ` Alan Stern
2023-08-30  1:32                                 ` Thinh Nguyen
2023-08-30 14:48                                   ` Alan Stern
2023-08-31  2:43                                     ` Thinh Nguyen
2023-08-31 15:40                                       ` Alan Stern
2023-09-01  1:27                                         ` Thinh Nguyen
2023-09-01 17:37                                           ` Alan Stern
2023-09-01 21:14                                             ` Thinh Nguyen
2023-09-02 15:15                                               ` Alan Stern
2023-09-05 22:53                                                 ` Thinh Nguyen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox