usb: usbfs: Add reset_resume callback to usbfs.
PATCH 1/1 usb: usbfs: Add reset_resume callback to usbfs.
From: chenchangcheng <[email protected]>
When an Apple device is inserted into the host, and the host wakes up from S3/S4 power states, if the reset_resume process is triggered, the absence of a reset_resume callback in usbfs will cause the device to unbind.
By adding a reset_resume callback to usbfs and reporting REMOVE and ADD uevents in reset_resume, the userspace is prompted to reissue commands to re-establish the binding with usbfs.
--- drivers/usb/core/devio.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/usb/core/devio.c +9 -0
@@ -749,6 +749,14 @@ static int driver_resume(struct usb_interface *intf)return 0 ; } +static int driver_reset_resume (struct usb_interface *intf) +{ +struct usb_device *udev =interface_to_usbdev (intf); + +kobject_uevent (&udev->dev.kobj,KOBJ_REMOVE ); +kobject_uevent (&udev->dev.kobj,KOBJ_ADD ); +return 0 ; +}#ifdef CONFIG_PM /* The following routines apply to the entire device, not interfaces */ void usbfs_notify_suspend (struct usb_device *udev)
@@ -776,6 +784,7 @@ struct usb_driver usbfs_driver = { .disconnect = driver_disconnect, .suspend = driver_suspend, .resume = driver_resume, + .reset_resume = driver_reset_resume, .supports_autosuspend = 1, };
base-commit: b19a97d57c15643494ac8bfaaa35e3ee472d41da
Signature · 1 line
2.25.1
Greg KH
maintainerQuoted 11 lines · On Fri, Aug 22, 2025 at 10:46:02AM +0800, [email protected] wrote:
From: chenchangcheng <[email protected]>
When an Apple device is inserted into the host, and the host wakes up from S3/S4 power states, if the
reset_resumeprocess is triggered, the absence of areset_resumecallback in usbfs will cause the device to unbind.
By adding areset_resumecallback to usbfs and reporting REMOVE and ADD uevents inreset_resume, the userspace is prompted to reissue commands to re-establish the binding with usbfs.Signed-off-by chenchangcheng <[email protected]>
Nit, we need a "real name", see the kernel documentation for details.
Quoted 9 lines
--- drivers/usb/core/devio.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/usb/core/devio.c +0
-0
drivers/usb/core/devio.c static int driver_resume(struct usb_interface *intf) · line 757
Quoted 9 lines
drivers/usb/core/devio.c +6
-0@@ -749,6 +749,14 @@ static int driver_resume(struct usb_interface *intf)return 0 ; } +static int driver_reset_resume (struct usb_interface *intf) +{ +struct usb_device *udev =interface_to_usbdev (intf); + +kobject_uevent (&udev->dev.kobj,KOBJ_REMOVE ); +kobject_uevent (&udev->dev.kobj,KOBJ_ADD );
But the object is not being removed and added. So why lie like this? How does userspace now handle this as the device did not go away?
This feels odd, what changed to require this kernel change to be added?
thanks,
greg k-h
Quoted 9 lines · On Fri, Aug 22, 2025 at 10:46:02AM +0800, [email protected] wrote:
From: chenchangcheng <[email protected]>
When an Apple device is inserted into the host, and the host wakes up from S3/S4 power states, if the
reset_resumeprocess is triggered, the absence of areset_resumecallback in usbfs will cause the device to unbind.
By adding areset_resumecallback to usbfs and reporting REMOVE and ADD uevents inreset_resume, the userspace is prompted to reissue commands to re-establish the binding with usbfs.
usbfs has no way to inform userspace when the device is reset. This is true for normal resets as well as for reset-resumes (no pre_reset, post_reset, or reset_resume callbacks). I don't see any point in trying to add support for the latter but not the former.
Unbinding the device forces userspace to re-open the device file and establish a new binding. How does adding REMOVE and ADD uevents make the situation any better than it already is?
Alan Stern
Quoted 16 lines · Alan Stern
Quoted 9 lines · On Fri, Aug 22, 2025 at 10:46:02AM +0800, [email protected] wrote:
From: chenchangcheng <[email protected]>
When an Apple device is inserted into the host, and the host wakes up from S3/S4 power states, if the
reset_resumeprocess is triggered, the absence of areset_resumecallback in usbfs will cause the device to unbind.
By adding areset_resumecallback to usbfs and reporting REMOVE and ADD uevents inreset_resume, the userspace is prompted to reissue commands to re-establish the binding with usbfs.usbfs has no way to inform userspace when the device is reset. This is true for normal resets as well as for reset-resumes (no
pre_reset,post_reset, orreset_resumecallbacks). I don't see any point in trying to add support for the latter but not the former.
Quoted 6 lines
Unbinding the device forces userspace to re-open the device file and establish a new binding. How does adding REMOVE and ADD uevents make the situation any better than it already is?
Alan Stern
Here is my reasoning:
Currently, for Apple devices after S3/S4 states, since the USB hub loses power, the reset-resume process is triggered during resume. If the original reset_resume process is followed, the device would be forcibly unbound, and the device_attach function would be used to rebind the driver. However, usbfs is different in that it cannot automatically rebind after unbinding and requires a userspace ioctl to re-establish the binding.
If we assume that the reset_resume callback of usbfs does nothing
and simply returns 0, the USB device would still be reset. When userspace uses the previous file descriptor handle to issue a command, it would result in an error: "PTP_OC 0x1007 receiving resp failed: PTP Session Not Open (0x2003)."
Therefore, by adding REMOVE and ADD uevents in the reset_resume process,
userspace is notified to first unbind and then rebind. This approach avoids the aforementioned issue.
Chen Changcheng
Quoted 32 lines · 自己
Quoted 16 lines · Alan Stern
Quoted 9 lines · On Fri, Aug 22, 2025 at 10:46:02AM +0800, [email protected] wrote:
From: chenchangcheng <[email protected]>
When an Apple device is inserted into the host, and the host wakes up from S3/S4 power states, if the
reset_resumeprocess is triggered, the absence of areset_resumecallback in usbfs will cause the device to unbind.
By adding areset_resumecallback to usbfs and reporting REMOVE and ADD uevents inreset_resume, the userspace is prompted to reissue commands to re-establish the binding with usbfs.usbfs has no way to inform userspace when the device is reset. This is true for normal resets as well as for reset-resumes (no
pre_reset,post_reset, orreset_resumecallbacks). I don't see any point in trying to add support for the latter but not the former.Quoted 6 lines
Unbinding the device forces userspace to re-open the device file and establish a new binding. How does adding REMOVE and ADD uevents make the situation any better than it already is?
Alan Stern
Here is my reasoning:
Currently, for Apple devices after S3/S4 states, since the USB hub loses power, the reset-resume process is triggered during resume. If the originalreset_resumeprocess is followed, the device would be forcibly unbound, and thedevice_attachfunction would be used to rebind the driver. However, usbfs is different in that it cannot automatically rebind after unbinding and requires a userspace ioctl to re-establish the binding.
Quoted 7 lines
If we assume that the
reset_resumecallback of usbfs does nothing
and simply returns 0, the USB device would still be reset. When userspace uses the previous file descriptor handle to issue a command, it would result in an error: "PTP_OC0x1007receiving resp failed: PTP Session Not Open (0x2003)."Therefore, by adding REMOVE and ADD uevents in the
reset_resumeprocess,
userspace is notified to first unbind and then rebind. This approach avoids the aforementioned issue.
Doesn't the "PTP Session Not Open" error notify userspace to unbind and rebind? Why is adding REMOVE and ADD uevents any better?
In the current kernel there is no reset_resume callback for usbfs. Consequently, when userspace uses the previous file descriptor handle to issue an ioctl command after a resume, it gets a -ENODEV error. Doesn't this also notify userspace that it should unbind and rebind? Why is adding a reset_resume callback any better?
Alan Stern
Quoted 44 lines · Alan Stern
Quoted 32 lines · 自己
Quoted 16 lines · Alan Stern
Quoted 9 lines · On Fri, Aug 22, 2025 at 10:46:02AM +0800, [email protected] wrote:
From: chenchangcheng <[email protected]>
When an Apple device is inserted into the host, and the host wakes up from S3/S4 power states, if the
reset_resumeprocess is triggered, the absence of areset_resumecallback in usbfs will cause the device to unbind.
By adding areset_resumecallback to usbfs and reporting REMOVE and ADD uevents inreset_resume, the userspace is prompted to reissue commands to re-establish the binding with usbfs.usbfs has no way to inform userspace when the device is reset. This is true for normal resets as well as for reset-resumes (no
pre_reset,post_reset, orreset_resumecallbacks). I don't see any point in trying to add support for the latter but not the former.Quoted 6 lines
Unbinding the device forces userspace to re-open the device file and establish a new binding. How does adding REMOVE and ADD uevents make the situation any better than it already is?
Alan Stern
Here is my reasoning:
Currently, for Apple devices after S3/S4 states, since the USB hub loses power, the reset-resume process is triggered during resume. If the originalreset_resumeprocess is followed, the device would be forcibly unbound, and thedevice_attachfunction would be used to rebind the driver. However, usbfs is different in that it cannot automatically rebind after unbinding and requires a userspace ioctl to re-establish the binding.Quoted 7 lines
If we assume that the
reset_resumecallback of usbfs does nothing
and simply returns 0, the USB device would still be reset. When userspace uses the previous file descriptor handle to issue a command, it would result in an error: "PTP_OC0x1007receiving resp failed: PTP Session Not Open (0x2003)."Therefore, by adding REMOVE and ADD uevents in the
reset_resumeprocess,
userspace is notified to first unbind and then rebind. This approach avoids the aforementioned issue.Doesn't the "PTP Session Not Open" error notify userspace to unbind and rebind? Why is adding REMOVE and ADD uevents any better?
Quoted 7 lines
In the current kernel there is no
reset_resumecallback for usbfs. Consequently, when userspace uses the previous file descriptor handle to issue an ioctl command after a resume, it gets a-ENODEVerror. Doesn't this also notify userspace that it should unbind and rebind? Why is adding areset_resumecallback any better?Alan Stern
According to the current experimental findings, when userspace encounters
an error while using the previous file descriptor (fd), it does not proceed to unbind and rebind automatically. Therefore, the two uevents were added in the kernel to explicitly notify userspace to unbind and rebind.
Chen Changcheng
Quoted 4 lines · 自己
According to the current experimental findings, when userspace encounters
an error while using the previous file descriptor (fd), it does not proceed to unbind and rebind automatically. Therefore, the two uevents were added in the kernel to explicitly notify userspace to unbind and rebind.
Sure, but that means that user space has a bug. That race is present in every case. Even if the kernel were to notify user space by additional channels, user space could already be in the process of calling into the kernel.
There is no way of avoiding the need for user space to handle this error return correctly.
Regards
Oliver
Quoted 4 lines · 自己
According to the current experimental findings, when userspace encounters
an error while using the previous file descriptor (fd), it does not proceed to unbind and rebind automatically. Therefore, the two uevents were added in the kernel to explicitly notify userspace to unbind and rebind.
Like Oliver said, this means that userspace needs to be fixed. Not the kernel.
Alan Stern