linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: usbfs: Add reset_resume callback to usbfs.
@ 2025-08-22  2:46 ccc194101
  2025-08-22  4:49 ` Greg KH
  2025-08-22 20:30 ` Alan Stern
  0 siblings, 2 replies; 8+ messages in thread
From: ccc194101 @ 2025-08-22  2:46 UTC (permalink / raw)
  To: gregkh; +Cc: jannh, stern, rex.nie, linux-usb, linux-kernel, chenchangcheng

From: chenchangcheng <chenchangcheng@kylinos.cn>

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.

Signed-off-by: chenchangcheng <chenchangcheng@kylinos.cn>
---
 drivers/usb/core/devio.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index f6ce6e26e0d4..358850596b0d 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -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
-- 
2.25.1


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

* Re: [PATCH] usb: usbfs: Add reset_resume callback to usbfs.
  2025-08-22  2:46 [PATCH] usb: usbfs: Add reset_resume callback to usbfs ccc194101
@ 2025-08-22  4:49 ` Greg KH
  2025-08-22 20:30 ` Alan Stern
  1 sibling, 0 replies; 8+ messages in thread
From: Greg KH @ 2025-08-22  4:49 UTC (permalink / raw)
  To: ccc194101; +Cc: jannh, stern, rex.nie, linux-usb, linux-kernel, chenchangcheng

On Fri, Aug 22, 2025 at 10:46:02AM +0800, ccc194101@163.com wrote:
> From: chenchangcheng <chenchangcheng@kylinos.cn>
> 
> 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.
> 
> Signed-off-by: chenchangcheng <chenchangcheng@kylinos.cn>

Nit, we need a "real name", see the kernel documentation for details.

> ---
>  drivers/usb/core/devio.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
> index f6ce6e26e0d4..358850596b0d 100644
> --- a/drivers/usb/core/devio.c
> +++ b/drivers/usb/core/devio.c
> @@ -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

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

* Re: [PATCH] usb: usbfs: Add reset_resume callback to usbfs.
  2025-08-22  2:46 [PATCH] usb: usbfs: Add reset_resume callback to usbfs ccc194101
  2025-08-22  4:49 ` Greg KH
@ 2025-08-22 20:30 ` Alan Stern
  2025-08-25  1:36   ` 自己
  1 sibling, 1 reply; 8+ messages in thread
From: Alan Stern @ 2025-08-22 20:30 UTC (permalink / raw)
  To: ccc194101; +Cc: gregkh, jannh, rex.nie, linux-usb, linux-kernel, chenchangcheng

On Fri, Aug 22, 2025 at 10:46:02AM +0800, ccc194101@163.com wrote:
> From: chenchangcheng <chenchangcheng@kylinos.cn>
> 
> 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.

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

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

* Re:Re: [PATCH] usb: usbfs: Add reset_resume callback to usbfs.
  2025-08-22 20:30 ` Alan Stern
@ 2025-08-25  1:36   ` 自己
  2025-08-25  2:01     ` Alan Stern
  0 siblings, 1 reply; 8+ messages in thread
From: 自己 @ 2025-08-25  1:36 UTC (permalink / raw)
  To: Alan Stern, gregkh
  Cc: jannh, rex.nie, linux-usb, linux-kernel, chenchangcheng

At 2025-08-23 04:30:18, "Alan Stern" <stern@rowland.harvard.edu> wrote:
>On Fri, Aug 22, 2025 at 10:46:02AM +0800, ccc194101@163.com wrote:
>> From: chenchangcheng <chenchangcheng@kylinos.cn>
>> 
>> 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.
>
>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

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

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

* Re: Re: [PATCH] usb: usbfs: Add reset_resume callback to usbfs.
  2025-08-25  1:36   ` 自己
@ 2025-08-25  2:01     ` Alan Stern
  2025-08-25  2:19       ` 自己
  0 siblings, 1 reply; 8+ messages in thread
From: Alan Stern @ 2025-08-25  2:01 UTC (permalink / raw)
  To: 自己
  Cc: gregkh, jannh, rex.nie, linux-usb, linux-kernel, chenchangcheng

On Mon, Aug 25, 2025 at 09:36:49AM +0800, 自己 wrote:
> At 2025-08-23 04:30:18, "Alan Stern" <stern@rowland.harvard.edu> wrote:
> >On Fri, Aug 22, 2025 at 10:46:02AM +0800, ccc194101@163.com wrote:
> >> From: chenchangcheng <chenchangcheng@kylinos.cn>
> >> 
> >> 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.
> >
> >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
> 
> 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.

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

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

* Re:Re: Re: [PATCH] usb: usbfs: Add reset_resume callback to usbfs.
  2025-08-25  2:01     ` Alan Stern
@ 2025-08-25  2:19       ` 自己
  2025-08-25  9:50         ` Oliver Neukum
  2025-08-25 14:04         ` Re: " Alan Stern
  0 siblings, 2 replies; 8+ messages in thread
From: 自己 @ 2025-08-25  2:19 UTC (permalink / raw)
  To: Alan Stern
  Cc: gregkh, jannh, rex.nie, linux-usb, linux-kernel, chenchangcheng

At 2025-08-25 10:01:44, "Alan Stern" <stern@rowland.harvard.edu> wrote:
>On Mon, Aug 25, 2025 at 09:36:49AM +0800, 自己 wrote:
>> At 2025-08-23 04:30:18, "Alan Stern" <stern@rowland.harvard.edu> wrote:
>> >On Fri, Aug 22, 2025 at 10:46:02AM +0800, ccc194101@163.com wrote:
>> >> From: chenchangcheng <chenchangcheng@kylinos.cn>
>> >> 
>> >> 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.
>> >
>> >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
>> 
>> 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.
>
>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

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

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

* Re: [PATCH] usb: usbfs: Add reset_resume callback to usbfs.
  2025-08-25  2:19       ` 自己
@ 2025-08-25  9:50         ` Oliver Neukum
  2025-08-25 14:04         ` Re: " Alan Stern
  1 sibling, 0 replies; 8+ messages in thread
From: Oliver Neukum @ 2025-08-25  9:50 UTC (permalink / raw)
  To: 自己, Alan Stern
  Cc: gregkh, jannh, rex.nie, linux-usb, linux-kernel, chenchangcheng

On 8/25/25 04:19, 自己 wrote:

> 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


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

* Re: Re: Re: [PATCH] usb: usbfs: Add reset_resume callback to usbfs.
  2025-08-25  2:19       ` 自己
  2025-08-25  9:50         ` Oliver Neukum
@ 2025-08-25 14:04         ` Alan Stern
  1 sibling, 0 replies; 8+ messages in thread
From: Alan Stern @ 2025-08-25 14:04 UTC (permalink / raw)
  To: 自己
  Cc: gregkh, jannh, rex.nie, linux-usb, linux-kernel, chenchangcheng

On Mon, Aug 25, 2025 at 10:19:56AM +0800, 自己 wrote:
> 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

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

end of thread, other threads:[~2025-08-25 14:04 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-22  2:46 [PATCH] usb: usbfs: Add reset_resume callback to usbfs ccc194101
2025-08-22  4:49 ` Greg KH
2025-08-22 20:30 ` Alan Stern
2025-08-25  1:36   ` 自己
2025-08-25  2:01     ` Alan Stern
2025-08-25  2:19       ` 自己
2025-08-25  9:50         ` Oliver Neukum
2025-08-25 14:04         ` Re: " Alan Stern

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).