Skip to contentSkip to conversation

usb: udc: Add trace event for usb_gadget_set_state

PATCH In review Kuen-Han Tsai 2 participants 3 messages Aug 18, 2025 – Sep 11, 2025 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: udc: Add trace event for usb_gadget_set_state

While the userspace program can be notified of gadget state changes, timing issue can lead to missed transitions when reading the state value.

Introduce a trace event for usb_gadget_set_state to reliably track state transitions.

---
 drivers/usb/gadget/udc/core.c  | 1 +
 drivers/usb/gadget/udc/trace.h | 5 +++++
 2 files changed, 6 insertions(+)
drivers/usb/gadget/udc/core.c +1 -0
index d709e24c1fd4..e28fea614496 100644
@@ -1125,6 +1125,7 @@ void usb_gadget_set_state(struct usb_gadget *gadget,
 {
 	gadget->state = state;
 	schedule_work(&gadget->work);
+	trace_usb_gadget_set_state(gadget, 0);
 }
 EXPORT_SYMBOL_GPL(usb_gadget_set_state);
 
drivers/usb/gadget/udc/trace.h +5 -0
index 4e334298b0e8..fa3e6ddf0a12 100644
@@ -81,6 +81,11 @@ DECLARE_EVENT_CLASS(udc_log_gadget,
 		__entry->ret)
 );
 
+DEFINE_EVENT(udc_log_gadget, usb_gadget_set_state,
+	TP_PROTO(struct usb_gadget *g, int ret),
+	TP_ARGS(g, ret)
+);
+
 DEFINE_EVENT(udc_log_gadget, usb_gadget_frame_number,
 	TP_PROTO(struct usb_gadget *g, int ret),
 	TP_ARGS(g, ret)
Signature · 1 line
2.51.0.rc1.163.g2494970778-goog
maintainer
#1 lore raw
Quoted 18 lines · Kuen-Han Tsai

While the userspace program can be notified of gadget state changes, timing issue can lead to missed transitions when reading the state value.

Introduce a trace event for usb_gadget_set_state to reliably track state transitions.

---
 drivers/usb/gadget/udc/core.c  | 1 +
 drivers/usb/gadget/udc/trace.h | 5 +++++
 2 files changed, 6 insertions(+)
drivers/usb/gadget/udc/core.c +0 -0
index d709e24c1fd4..e28fea614496 100644

drivers/usb/gadget/udc/core.c void usb_gadget_set_state(struct usb_gadget *gadget, · line 1128

Quoted 4 lines
drivers/usb/gadget/udc/core.c +1 -0
@@ -1125,6 +1125,7 @@ void usb_gadget_set_state(struct usb_gadget *gadget,
 {
 	gadget->state = state;
 	schedule_work(&gadget->work);
+	trace_usb_gadget_set_state(gadget, 0);

Will this show the state the gadget has been set to? And why not just do that in the work callback, as that is when it really happens.

What is the output of this trace line?

thanks,

greg k-h

#2 lore raw

Hi Greg,

Quoted 15 lines · Greg KH
Quoted 13 lines · Kuen-Han Tsai

While the userspace program can be notified of gadget state changes, timing issue can lead to missed transitions when reading the state value.

Introduce a trace event for usb_gadget_set_state to reliably track state transitions.

---
 drivers/usb/gadget/udc/core.c  | 1 +
 drivers/usb/gadget/udc/trace.h | 5 +++++
 2 files changed, 6 insertions(+)
Quoted 12 lines

drivers/usb/gadget/udc/core.c void usb_gadget_set_state(struct usb_gadget *gadget, · line 1128

Quoted 9 lines
drivers/usb/gadget/udc/core.c +1 -0
index d709e24c1fd4..e28fea614496 100644
@@ -1125,6 +1125,7 @@ void usb_gadget_set_state(struct usb_gadget *gadget,
 {
      gadget->state = state;
      schedule_work(&gadget->work);
+     trace_usb_gadget_set_state(gadget, 0);

Will this show the state the gadget has been set to? And why not just do that in the work callback, as that is when it really happens.

Yes, it shows the new state by logging the state field from the gadget object.

The work callback is for handling asynchronous sysfs notifications that occur later. Placing the tracepoint here accurately reflects when the state change actually happens.

Quoted 3 lines

What is the output of this trace line?

The output looks like

usb_gadget_set_state: speed 5/5 state 5 100mA
[sg:self-powered:activated:connected] --> 0
usb_gadget_set_state: speed 5/5 state 6 100mA
[sg:self-powered:activated:connected] --> 0
usb_gadget_set_state: speed 5/5 state 7 100mA
[sg:self-powered:activated:connected] --> 0
usb_gadget_set_state: speed 5/5 state 7 900mA
[sg:bus-powered:activated:connected] --> 0

Regards,
Kuen-Han