Am Dienstag, den 25.06.2019, 10:08 -0400 schrieb Alan Stern: > On Tue, 25 Jun 2019, Mayuresh Kulkarni wrote: > > Right, so user space should do the following when it determines the > > device is idle from its point of view - > > > > 1. Call ALLOW_SUSPEND ioctl That is a race in principle. You should reverse steps 1 and 2 > > 2. Queue an URB and wait for its REAP. When the wait returns -EFAIL (or > > something similar), that is the indication that the device is no longer > > active (or suspended) > > 3. Call WAIT_FOR_RESUME ioctl It seems to me that there ought to be one API for that. Either an ioctl or poll. > > 4. When WAIT_FOR_RESUME ioctl returns, it is guaranteed that device is > > active. > > 5. Call FORBID_SUSPEND ioctl and read the cause of resume. > > 6. Go to (1) when appropriate > > > > Have I summarized this approach correctly from user-space point of view? > > Yes, except for one thing: In step 4, it is _not_ guaranteed that the > device is active when WAIT_FOR_RESUME returns. The only guarantee is > that a resume did occur sometime after step 1, but the device might > have gone back into suspend after that occurred. Now is that a good API? Shouldn't we rather have an API for either * resume the device now and bump the counter * bump the counter when te device resumes I don't see a value in not having a defined power state after resume. > > 3. Extend usbdev_poll() to wait for udev->state == USB_STATE_SUSPENDED > > when events == POLLPRI. Return POLLPRI when state = USB_STATE_SUSPENDED. > > 4. As before, any ioctl != (ALLOW_SUSPEND or WAIT_FOR_RESUME) > > calls usb_autoresume_device(). > > 3 sounds reasonable at first, but I'm not sure it would work. > Consider what would happen if the device is suspended very briefly and > then wakes up. The usbdev_poll() call might not return, because by the > time it checks udev->state, the state has already changed back to > USB_STATE_CONFIGURED. Indeed. It seems to me that any power transition should be reported back. > In any case, we shouldn't do 4. It would prevent the device from ever > going into suspend, because the program would want to continue making > usbfs ioctl calls while waiting for the suspend to occur. Exactly. > > The corresponding user-space calls would be - > > A. When determined device is idle, call ALLOW_SUSPEND ioctl. > > B. Call poll(device_fd, POLLPRI). When poll returns check revents > > == POLLPRI. > > What if the device never does go into suspend? The poll() call > wouldn't return and the program would just stop working. Well, that is why you can poll for multiple events at the same time and the syscall has a timeout. > > 2. Is it safe to wait for udev->state to be of a particular value? > > No, not really, because the state can change. That is a general issue with poll. You cannot be sure that there is still data to be read when you are ready to read. Regards Oliver