Skip to content

feat(usb): Add suspend and resume support - #1138

Open
vesim987 wants to merge 1 commit into
mainfrom
usb-suspend
Open

vesim987 wants to merge 1 commit into
mainfrom
usb-suspend

Conversation

@vesim987

Copy link
Copy Markdown
Collaborator

Requires #1130

@vesim987
vesim987 force-pushed the usb-suspend branch 2 times, most recently from a536074 to c9aa875 Compare November 19, 2026 05:02
@vesim987
vesim987 marked this pull request as ready for review November 19, 2026 05:02

@mattnite mattnite left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What testing did you do?

Comment thread core/src/core/usb/HID_report.zig Resolved
Comment thread core/src/core/usb/descriptor.zig Resolved
Comment thread core/src/core/usb.zig Resolved
Comment thread core/src/core/usb.zig Resolved
@vesim987

Copy link
Copy Markdown
Collaborator Author

Compiling any USB example with debug logging is enough to validate that the events are fired when the host suspend/resumes.

For waking up I've added to one of the example small snippet of code that called remote_wakeup after 3s.
On my Linux it required

echo enabled | sudo tee /sys/bus/usb/devices/<device>/power/wakeup

for it to work, w/o this it properly returned an error.

I can add extra example for this.

@mattnite

Copy link
Copy Markdown
Contributor

Yes I agree. An example would be great, please include some comments documenting the behaviour, explain some potential applications of this to users.

var usb_device: USB_Device = undefined;

// Generate a device controller with descriptor and handlers setup for CDC (USB_Serial) and
// picotool-controlled reset (ResetDriver).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does picotool enter the picture? I thought resetting a device was a standard thing

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

picotool has reboot subcommnd which sends special USB packet, ResetDriver implements that. I just copied this from other examples.

wakeup_deadline = rp2xxx.time.deadline_in_ms(5 * std.time.ms_per_s);
}

fn on_resume() void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a context pointer to the callbacks please.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a point in doing that? both are registered in usb_controller which is global variable anyway.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a global variable in your example, but it may not be a global example in user code.

Comment thread core/src/core/usb.zig
default_vendor: ?Options.IdStringPair = null,
default_product: ?Options.IdStringPair = null,
/// Ports that enable this must also provide the `signal_resume` vtable function.
supports_remote_wakeup: bool = false,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not neccessary, you can just check if signal_resume is null

Comment thread core/src/core/usb.zig
_ = device_itf;
log.debug("on_suspend", .{});

if (!self.is_suspended) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log message if on_suspend was called while suspened would be nice

Comment thread core/src/core/usb.zig
.create(attr.self_powered, true),
};
};
return std.mem.asBytes(&S.responses[@intFromBool(self.remote_wakeup_enabled)]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does a device really need to be self-powered for remote wakeup to work?

Comment thread core/src/core/usb.zig
/// Only called when the bus actually enters suspend.
on_suspend: ?*const fn () void = null,
/// Only called when the bus actually leaves suspend.
on_resume: ?*const fn () void = null,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be handled by returning a value from poll rather than with callbacks

Also adds example for rp2xxx.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants