aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Stern <stern@rowland.harvard.edu>2010-01-08 12:57:28 -0500
committerGreg Kroah-Hartman <gregkh@suse.de>2010-03-02 17:54:12 -0500
commit9bbdf1e0afe771ca7650f9f476769310bee9d8f3 (patch)
tree22852edde0165523d37f045575512f5759040dba
parent0c590e2361511997430130e10e372217c1128da6 (diff)
USB: convert to the runtime PM framework
This patch (as1329) converts the USB stack over to the PM core's runtime PM framework. This involves numerous changes throughout usbcore, especially to hub.c and driver.c. Perhaps the most notable change is that CONFIG_USB_SUSPEND now depends on CONFIG_PM_RUNTIME instead of CONFIG_PM. Several fields in the usb_device and usb_interface structures are no longer needed. Some code which used to depend on CONFIG_USB_PM now depends on CONFIG_USB_SUSPEND (requiring some rearrangement of header files). The only visible change in behavior should be that following a system sleep (resume from RAM or resume from hibernation), autosuspended USB devices will be resumed just like everything else. They won't remain suspended. But if they aren't in use then they will naturally autosuspend again in a few seconds. Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
-rw-r--r--Documentation/usb/power-management.txt217
-rw-r--r--drivers/usb/core/Kconfig4
-rw-r--r--drivers/usb/core/driver.c845
-rw-r--r--drivers/usb/core/hcd.c13
-rw-r--r--drivers/usb/core/hcd.h10
-rw-r--r--drivers/usb/core/hub.c65
-rw-r--r--drivers/usb/core/message.c1
-rw-r--r--drivers/usb/core/usb.c35
-rw-r--r--drivers/usb/core/usb.h49
-rw-r--r--drivers/usb/misc/usbtest.c4
-rw-r--r--include/linux/usb.h31
11 files changed, 490 insertions, 784 deletions
diff --git a/Documentation/usb/power-management.txt b/Documentation/usb/power-management.txt
index e3fa189c257a..2790ad48cfc2 100644
--- a/Documentation/usb/power-management.txt
+++ b/Documentation/usb/power-management.txt
@@ -2,7 +2,7 @@
2 2
3 Alan Stern <stern@rowland.harvard.edu> 3 Alan Stern <stern@rowland.harvard.edu>
4 4
5 November 10, 2009 5 December 11, 2009
6 6
7 7
8 8
@@ -29,9 +29,9 @@ covered to some extent (see Documentation/power/*.txt for more
29information about system PM). 29information about system PM).
30 30
31Note: Dynamic PM support for USB is present only if the kernel was 31Note: Dynamic PM support for USB is present only if the kernel was
32built with CONFIG_USB_SUSPEND enabled. System PM support is present 32built with CONFIG_USB_SUSPEND enabled (which depends on
33only if the kernel was built with CONFIG_SUSPEND or CONFIG_HIBERNATION 33CONFIG_PM_RUNTIME). System PM support is present only if the kernel
34enabled. 34was built with CONFIG_SUSPEND or CONFIG_HIBERNATION enabled.
35 35
36 36
37 What is Remote Wakeup? 37 What is Remote Wakeup?
@@ -326,64 +326,63 @@ driver does so by calling these six functions:
326 void usb_autopm_get_interface_no_resume(struct usb_interface *intf); 326 void usb_autopm_get_interface_no_resume(struct usb_interface *intf);
327 void usb_autopm_put_interface_no_suspend(struct usb_interface *intf); 327 void usb_autopm_put_interface_no_suspend(struct usb_interface *intf);
328 328
329The functions work by maintaining a counter in the usb_interface 329The functions work by maintaining a usage counter in the
330structure. When intf->pm_usage_count is > 0 then the interface is 330usb_interface's embedded device structure. When the counter is > 0
331deemed to be busy, and the kernel will not autosuspend the interface's 331then the interface is deemed to be busy, and the kernel will not
332device. When intf->pm_usage_count is <= 0 then the interface is 332autosuspend the interface's device. When the usage counter is = 0
333considered to be idle, and the kernel may autosuspend the device. 333then the interface is considered to be idle, and the kernel may
334autosuspend the device.
334 335
335(There is a similar pm_usage_count field in struct usb_device, 336(There is a similar usage counter field in struct usb_device,
336associated with the device itself rather than any of its interfaces. 337associated with the device itself rather than any of its interfaces.
337This field is used only by the USB core.) 338This counter is used only by the USB core.)
338 339
339Drivers must not modify intf->pm_usage_count directly; its value 340Drivers need not be concerned about balancing changes to the usage
340should be changed only be using the functions listed above. Drivers 341counter; the USB core will undo any remaining "get"s when a driver
341are responsible for insuring that the overall change to pm_usage_count 342is unbound from its interface. As a corollary, drivers must not call
342during their lifetime balances out to 0 (it may be necessary for the 343any of the usb_autopm_* functions after their diconnect() routine has
343disconnect method to call usb_autopm_put_interface() one or more times 344returned.
344to fulfill this requirement). The first two routines use the PM mutex 345
345in struct usb_device for mutual exclusion; drivers using the async 346Drivers using the async routines are responsible for their own
346routines are responsible for their own synchronization and mutual 347synchronization and mutual exclusion.
347exclusion. 348
348 349 usb_autopm_get_interface() increments the usage counter and
349 usb_autopm_get_interface() increments pm_usage_count and 350 does an autoresume if the device is suspended. If the
350 attempts an autoresume if the new value is > 0 and the 351 autoresume fails, the counter is decremented back.
351 device is suspended. 352
352 353 usb_autopm_put_interface() decrements the usage counter and
353 usb_autopm_put_interface() decrements pm_usage_count and 354 attempts an autosuspend if the new value is = 0.
354 attempts an autosuspend if the new value is <= 0 and the
355 device isn't suspended.
356 355
357 usb_autopm_get_interface_async() and 356 usb_autopm_get_interface_async() and
358 usb_autopm_put_interface_async() do almost the same things as 357 usb_autopm_put_interface_async() do almost the same things as
359 their non-async counterparts. The differences are: they do 358 their non-async counterparts. The big difference is that they
360 not acquire the PM mutex, and they use a workqueue to do their 359 use a workqueue to do the resume or suspend part of their
361 jobs. As a result they can be called in an atomic context, 360 jobs. As a result they can be called in an atomic context,
362 such as an URB's completion handler, but when they return the 361 such as an URB's completion handler, but when they return the
363 device will not generally not yet be in the desired state. 362 device will generally not yet be in the desired state.
364 363
365 usb_autopm_get_interface_no_resume() and 364 usb_autopm_get_interface_no_resume() and
366 usb_autopm_put_interface_no_suspend() merely increment or 365 usb_autopm_put_interface_no_suspend() merely increment or
367 decrement the pm_usage_count value; they do not attempt to 366 decrement the usage counter; they do not attempt to carry out
368 carry out an autoresume or an autosuspend. Hence they can be 367 an autoresume or an autosuspend. Hence they can be called in
369 called in an atomic context. 368 an atomic context.
370 369
371The conventional usage pattern is that a driver calls 370The simplest usage pattern is that a driver calls
372usb_autopm_get_interface() in its open routine and 371usb_autopm_get_interface() in its open routine and
373usb_autopm_put_interface() in its close or release routine. But 372usb_autopm_put_interface() in its close or release routine. But other
374other patterns are possible. 373patterns are possible.
375 374
376The autosuspend attempts mentioned above will often fail for one 375The autosuspend attempts mentioned above will often fail for one
377reason or another. For example, the power/level attribute might be 376reason or another. For example, the power/level attribute might be
378set to "on", or another interface in the same device might not be 377set to "on", or another interface in the same device might not be
379idle. This is perfectly normal. If the reason for failure was that 378idle. This is perfectly normal. If the reason for failure was that
380the device hasn't been idle for long enough, a delayed workqueue 379the device hasn't been idle for long enough, a timer is scheduled to
381routine is automatically set up to carry out the operation when the 380carry out the operation automatically when the autosuspend idle-delay
382autosuspend idle-delay has expired. 381has expired.
383 382
384Autoresume attempts also can fail, although failure would mean that 383Autoresume attempts also can fail, although failure would mean that
385the device is no longer present or operating properly. Unlike 384the device is no longer present or operating properly. Unlike
386autosuspend, there's no delay for an autoresume. 385autosuspend, there's no idle-delay for an autoresume.
387 386
388 387
389 Other parts of the driver interface 388 Other parts of the driver interface
@@ -413,26 +412,27 @@ though, setting this flag won't cause the kernel to autoresume it.
413Normally a driver would set this flag in its probe method, at which 412Normally a driver would set this flag in its probe method, at which
414time the device is guaranteed not to be autosuspended.) 413time the device is guaranteed not to be autosuspended.)
415 414
416The synchronous usb_autopm_* routines have to run in a sleepable 415If a driver does its I/O asynchronously in interrupt context, it
417process context; they must not be called from an interrupt handler or 416should call usb_autopm_get_interface_async() before starting output and
418while holding a spinlock. In fact, the entire autosuspend mechanism 417usb_autopm_put_interface_async() when the output queue drains. When
419is not well geared toward interrupt-driven operation. However there 418it receives an input event, it should call
420is one thing a driver can do in an interrupt handler:
421 419
422 usb_mark_last_busy(struct usb_device *udev); 420 usb_mark_last_busy(struct usb_device *udev);
423 421
424This sets udev->last_busy to the current time. udev->last_busy is the 422in the event handler. This sets udev->last_busy to the current time.
425field used for idle-delay calculations; updating it will cause any 423udev->last_busy is the field used for idle-delay calculations;
426pending autosuspend to be moved back. The usb_autopm_* routines will 424updating it will cause any pending autosuspend to be moved back. Most
427also set the last_busy field to the current time. 425of the usb_autopm_* routines will also set the last_busy field to the
428 426current time.
429Calling urb_mark_last_busy() from within an URB completion handler is 427
430subject to races: The kernel may have just finished deciding the 428Asynchronous operation is always subject to races. For example, a
431device has been idle for long enough but not yet gotten around to 429driver may call one of the usb_autopm_*_interface_async() routines at
432calling the driver's suspend method. The driver would have to be 430a time when the core has just finished deciding the device has been
433responsible for synchronizing its suspend method with its URB 431idle for long enough but not yet gotten around to calling the driver's
434completion handler and causing the autosuspend to fail with -EBUSY if 432suspend method. The suspend method must be responsible for
435an URB had completed too recently. 433synchronizing with the output request routine and the URB completion
434handler; it should cause autosuspends to fail with -EBUSY if the
435driver needs to use the device.
436 436
437External suspend calls should never be allowed to fail in this way, 437External suspend calls should never be allowed to fail in this way,
438only autosuspend calls. The driver can tell them apart by checking 438only autosuspend calls. The driver can tell them apart by checking
@@ -440,75 +440,23 @@ the PM_EVENT_AUTO bit in the message.event argument to the suspend
440method; this bit will be set for internal PM events (autosuspend) and 440method; this bit will be set for internal PM events (autosuspend) and
441clear for external PM events. 441clear for external PM events.
442 442
443Many of the ingredients in the autosuspend framework are oriented
444towards interfaces: The usb_interface structure contains the
445pm_usage_cnt field, and the usb_autopm_* routines take an interface
446pointer as their argument. But somewhat confusingly, a few of the
447pieces (i.e., usb_mark_last_busy()) use the usb_device structure
448instead. Drivers need to keep this straight; they can call
449interface_to_usbdev() to find the device structure for a given
450interface.
451 443
444 Mutual exclusion
445 ----------------
452 446
453 Locking requirements 447For external events -- but not necessarily for autosuspend or
454 -------------------- 448autoresume -- the device semaphore (udev->dev.sem) will be held when a
455 449suspend or resume method is called. This implies that external
456All three suspend/resume methods are always called while holding the 450suspend/resume events are mutually exclusive with calls to probe,
457usb_device's PM mutex. For external events -- but not necessarily for 451disconnect, pre_reset, and post_reset; the USB core guarantees that
458autosuspend or autoresume -- the device semaphore (udev->dev.sem) will 452this is true of autosuspend/autoresume events as well.
459also be held. This implies that external suspend/resume events are
460mutually exclusive with calls to probe, disconnect, pre_reset, and
461post_reset; the USB core guarantees that this is true of internal
462suspend/resume events as well.
463 453
464If a driver wants to block all suspend/resume calls during some 454If a driver wants to block all suspend/resume calls during some
465critical section, it can simply acquire udev->pm_mutex. Note that 455critical section, the best way is to lock the device and call
466calls to resume may be triggered indirectly. Block IO due to memory 456usb_autopm_get_interface() (and do the reverse at the end of the
467allocations can make the vm subsystem resume a device. Thus while 457critical section). Holding the device semaphore will block all
468holding this lock you must not allocate memory with GFP_KERNEL or 458external PM calls, and the usb_autopm_get_interface() will prevent any
469GFP_NOFS. 459internal PM calls, even if it fails. (Exercise: Why?)
470
471Alternatively, if the critical section might call some of the
472usb_autopm_* routines, the driver can avoid deadlock by doing:
473
474 down(&udev->dev.sem);
475 rc = usb_autopm_get_interface(intf);
476
477and at the end of the critical section:
478
479 if (!rc)
480 usb_autopm_put_interface(intf);
481 up(&udev->dev.sem);
482
483Holding the device semaphore will block all external PM calls, and the
484usb_autopm_get_interface() will prevent any internal PM calls, even if
485it fails. (Exercise: Why?)
486
487The rules for locking order are:
488
489 Never acquire any device semaphore while holding any PM mutex.
490
491 Never acquire udev->pm_mutex while holding the PM mutex for
492 a device that isn't a descendant of udev.
493
494In other words, PM mutexes should only be acquired going up the device
495tree, and they should be acquired only after locking all the device
496semaphores you need to hold. These rules don't matter to drivers very
497much; they usually affect just the USB core.
498
499Still, drivers do need to be careful. For example, many drivers use a
500private mutex to synchronize their normal I/O activities with their
501disconnect method. Now if the driver supports autosuspend then it
502must call usb_autopm_put_interface() from somewhere -- maybe from its
503close method. It should make the call while holding the private mutex,
504since a driver shouldn't call any of the usb_autopm_* functions for an
505interface from which it has been unbound.
506
507But the usb_autpm_* routines always acquire the device's PM mutex, and
508consequently the locking order has to be: private mutex first, PM
509mutex second. Since the suspend method is always called with the PM
510mutex held, it mustn't try to acquire the private mutex. It has to
511synchronize with the driver's I/O activities in some other way.
512 460
513 461
514 Interaction between dynamic PM and system PM 462 Interaction between dynamic PM and system PM
@@ -517,22 +465,11 @@ synchronize with the driver's I/O activities in some other way.
517Dynamic power management and system power management can interact in 465Dynamic power management and system power management can interact in
518a couple of ways. 466a couple of ways.
519 467
520Firstly, a device may already be manually suspended or autosuspended 468Firstly, a device may already be autosuspended when a system suspend
521when a system suspend occurs. Since system suspends are supposed to 469occurs. Since system suspends are supposed to be as transparent as
522be as transparent as possible, the device should remain suspended 470possible, the device should remain suspended following the system
523following the system resume. The 2.6.23 kernel obeys this principle 471resume. But this theory may not work out well in practice; over time
524for manually suspended devices but not for autosuspended devices; they 472the kernel's behavior in this regard has changed.
525do get resumed when the system wakes up. (Presumably they will be
526autosuspended again after their idle-delay time expires.) In later
527kernels this behavior will be fixed.
528
529(There is an exception. If a device would undergo a reset-resume
530instead of a normal resume, and the device is enabled for remote
531wakeup, then the reset-resume takes place even if the device was
532already suspended when the system suspend began. The justification is
533that a reset-resume is a kind of remote-wakeup event. Or to put it
534another way, a device which needs a reset won't be able to generate
535normal remote-wakeup signals, so it ought to be resumed immediately.)
536 473
537Secondly, a dynamic power-management event may occur as a system 474Secondly, a dynamic power-management event may occur as a system
538suspend is underway. The window for this is short, since system 475suspend is underway. The window for this is short, since system
diff --git a/drivers/usb/core/Kconfig b/drivers/usb/core/Kconfig
index ad925946f869..97a819c23ef3 100644
--- a/drivers/usb/core/Kconfig
+++ b/drivers/usb/core/Kconfig
@@ -91,8 +91,8 @@ config USB_DYNAMIC_MINORS
91 If you are unsure about this, say N here. 91 If you are unsure about this, say N here.
92 92
93config USB_SUSPEND 93config USB_SUSPEND
94 bool "USB selective suspend/resume and wakeup" 94 bool "USB runtime power management (suspend/resume and wakeup)"
95 depends on USB && PM 95 depends on USB && PM_RUNTIME
96 help 96 help
97 If you say Y here, you can use driver calls or the sysfs 97 If you say Y here, you can use driver calls or the sysfs
98 "power/level" file to suspend or resume individual USB 98 "power/level" file to suspend or resume individual USB
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 638d54693a1c..6850ec6576f8 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -25,7 +25,7 @@
25#include <linux/device.h> 25#include <linux/device.h>
26#include <linux/usb.h> 26#include <linux/usb.h>
27#include <linux/usb/quirks.h> 27#include <linux/usb/quirks.h>
28#include <linux/workqueue.h> 28#include <linux/pm_runtime.h>
29#include "hcd.h" 29#include "hcd.h"
30#include "usb.h" 30#include "usb.h"
31 31
@@ -221,7 +221,7 @@ static int usb_probe_device(struct device *dev)
221{ 221{
222 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); 222 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
223 struct usb_device *udev = to_usb_device(dev); 223 struct usb_device *udev = to_usb_device(dev);
224 int error = -ENODEV; 224 int error = 0;
225 225
226 dev_dbg(dev, "%s\n", __func__); 226 dev_dbg(dev, "%s\n", __func__);
227 227
@@ -230,18 +230,23 @@ static int usb_probe_device(struct device *dev)
230 /* The device should always appear to be in use 230 /* The device should always appear to be in use
231 * unless the driver suports autosuspend. 231 * unless the driver suports autosuspend.
232 */ 232 */
233 udev->pm_usage_cnt = !(udriver->supports_autosuspend); 233 if (!udriver->supports_autosuspend)
234 error = usb_autoresume_device(udev);
234 235
235 error = udriver->probe(udev); 236 if (!error)
237 error = udriver->probe(udev);
236 return error; 238 return error;
237} 239}
238 240
239/* called from driver core with dev locked */ 241/* called from driver core with dev locked */
240static int usb_unbind_device(struct device *dev) 242static int usb_unbind_device(struct device *dev)
241{ 243{
244 struct usb_device *udev = to_usb_device(dev);
242 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); 245 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
243 246
244 udriver->disconnect(to_usb_device(dev)); 247 udriver->disconnect(udev);
248 if (!udriver->supports_autosuspend)
249 usb_autosuspend_device(udev);
245 return 0; 250 return 0;
246} 251}
247 252
@@ -293,17 +298,16 @@ static int usb_probe_interface(struct device *dev)
293 if (error) 298 if (error)
294 return error; 299 return error;
295 300
296 /* Interface "power state" doesn't correspond to any hardware
297 * state whatsoever. We use it to record when it's bound to
298 * a driver that may start I/0: it's not frozen/quiesced.
299 */
300 mark_active(intf);
301 intf->condition = USB_INTERFACE_BINDING; 301 intf->condition = USB_INTERFACE_BINDING;
302 302
303 /* The interface should always appear to be in use 303 /* Bound interfaces are initially active. They are
304 * unless the driver suports autosuspend. 304 * runtime-PM-enabled only if the driver has autosuspend support.
305 * They are sensitive to their children's power states.
305 */ 306 */
306 atomic_set(&intf->pm_usage_cnt, !driver->supports_autosuspend); 307 pm_runtime_set_active(dev);
308 pm_suspend_ignore_children(dev, false);
309 if (driver->supports_autosuspend)
310 pm_runtime_enable(dev);
307 311
308 /* Carry out a deferred switch to altsetting 0 */ 312 /* Carry out a deferred switch to altsetting 0 */
309 if (intf->needs_altsetting0) { 313 if (intf->needs_altsetting0) {
@@ -323,10 +327,14 @@ static int usb_probe_interface(struct device *dev)
323 return error; 327 return error;
324 328
325 err: 329 err:
326 mark_quiesced(intf);
327 intf->needs_remote_wakeup = 0; 330 intf->needs_remote_wakeup = 0;
328 intf->condition = USB_INTERFACE_UNBOUND; 331 intf->condition = USB_INTERFACE_UNBOUND;
329 usb_cancel_queued_reset(intf); 332 usb_cancel_queued_reset(intf);
333
334 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
335 pm_runtime_disable(dev);
336 pm_runtime_set_suspended(dev);
337
330 usb_autosuspend_device(udev); 338 usb_autosuspend_device(udev);
331 return error; 339 return error;
332} 340}
@@ -376,9 +384,17 @@ static int usb_unbind_interface(struct device *dev)
376 usb_set_intfdata(intf, NULL); 384 usb_set_intfdata(intf, NULL);
377 385
378 intf->condition = USB_INTERFACE_UNBOUND; 386 intf->condition = USB_INTERFACE_UNBOUND;
379 mark_quiesced(intf);
380 intf->needs_remote_wakeup = 0; 387 intf->needs_remote_wakeup = 0;
381 388
389 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
390 pm_runtime_disable(dev);
391 pm_runtime_set_suspended(dev);
392
393 /* Undo any residual pm_autopm_get_interface_* calls */
394 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
395 usb_autopm_put_interface_no_suspend(intf);
396 atomic_set(&intf->pm_usage_cnt, 0);
397
382 if (!error) 398 if (!error)
383 usb_autosuspend_device(udev); 399 usb_autosuspend_device(udev);
384 400
@@ -409,7 +425,6 @@ int usb_driver_claim_interface(struct usb_driver *driver,
409 struct usb_interface *iface, void *priv) 425 struct usb_interface *iface, void *priv)
410{ 426{
411 struct device *dev = &iface->dev; 427 struct device *dev = &iface->dev;
412 struct usb_device *udev = interface_to_usbdev(iface);
413 int retval = 0; 428 int retval = 0;
414 429
415 if (dev->driver) 430 if (dev->driver)
@@ -419,11 +434,16 @@ int usb_driver_claim_interface(struct usb_driver *driver,
419 usb_set_intfdata(iface, priv); 434 usb_set_intfdata(iface, priv);
420 iface->needs_binding = 0; 435 iface->needs_binding = 0;
421 436
422 usb_pm_lock(udev);
423 iface->condition = USB_INTERFACE_BOUND; 437 iface->condition = USB_INTERFACE_BOUND;
424 mark_active(iface); 438
425 atomic_set(&iface->pm_usage_cnt, !driver->supports_autosuspend); 439 /* Bound interfaces are initially active. They are
426 usb_pm_unlock(udev); 440 * runtime-PM-enabled only if the driver has autosuspend support.
441 * They are sensitive to their children's power states.
442 */
443 pm_runtime_set_active(dev);
444 pm_suspend_ignore_children(dev, false);
445 if (driver->supports_autosuspend)
446 pm_runtime_enable(dev);
427 447
428 /* if interface was already added, bind now; else let 448 /* if interface was already added, bind now; else let
429 * the future device_add() bind it, bypassing probe() 449 * the future device_add() bind it, bypassing probe()
@@ -982,7 +1002,6 @@ static void do_unbind_rebind(struct usb_device *udev, int action)
982 } 1002 }
983} 1003}
984 1004
985/* Caller has locked udev's pm_mutex */
986static int usb_suspend_device(struct usb_device *udev, pm_message_t msg) 1005static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
987{ 1006{
988 struct usb_device_driver *udriver; 1007 struct usb_device_driver *udriver;
@@ -1006,7 +1025,6 @@ static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
1006 return status; 1025 return status;
1007} 1026}
1008 1027
1009/* Caller has locked udev's pm_mutex */
1010static int usb_resume_device(struct usb_device *udev, pm_message_t msg) 1028static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1011{ 1029{
1012 struct usb_device_driver *udriver; 1030 struct usb_device_driver *udriver;
@@ -1040,27 +1058,20 @@ static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1040 return status; 1058 return status;
1041} 1059}
1042 1060
1043/* Caller has locked intf's usb_device's pm mutex */
1044static int usb_suspend_interface(struct usb_device *udev, 1061static int usb_suspend_interface(struct usb_device *udev,
1045 struct usb_interface *intf, pm_message_t msg) 1062 struct usb_interface *intf, pm_message_t msg)
1046{ 1063{
1047 struct usb_driver *driver; 1064 struct usb_driver *driver;
1048 int status = 0; 1065 int status = 0;
1049 1066
1050 /* with no hardware, USB interfaces only use FREEZE and ON states */ 1067 if (udev->state == USB_STATE_NOTATTACHED ||
1051 if (udev->state == USB_STATE_NOTATTACHED || !is_active(intf)) 1068 intf->condition == USB_INTERFACE_UNBOUND)
1052 goto done;
1053
1054 /* This can happen; see usb_driver_release_interface() */
1055 if (intf->condition == USB_INTERFACE_UNBOUND)
1056 goto done; 1069 goto done;
1057 driver = to_usb_driver(intf->dev.driver); 1070 driver = to_usb_driver(intf->dev.driver);
1058 1071
1059 if (driver->suspend) { 1072 if (driver->suspend) {
1060 status = driver->suspend(intf, msg); 1073 status = driver->suspend(intf, msg);
1061 if (status == 0) 1074 if (status && !(msg.event & PM_EVENT_AUTO))
1062 mark_quiesced(intf);
1063 else if (!(msg.event & PM_EVENT_AUTO))
1064 dev_err(&intf->dev, "%s error %d\n", 1075 dev_err(&intf->dev, "%s error %d\n",
1065 "suspend", status); 1076 "suspend", status);
1066 } else { 1077 } else {
@@ -1068,7 +1079,6 @@ static int usb_suspend_interface(struct usb_device *udev,
1068 intf->needs_binding = 1; 1079 intf->needs_binding = 1;
1069 dev_warn(&intf->dev, "no %s for driver %s?\n", 1080 dev_warn(&intf->dev, "no %s for driver %s?\n",
1070 "suspend", driver->name); 1081 "suspend", driver->name);
1071 mark_quiesced(intf);
1072 } 1082 }
1073 1083
1074 done: 1084 done:
@@ -1076,14 +1086,13 @@ static int usb_suspend_interface(struct usb_device *udev,
1076 return status; 1086 return status;
1077} 1087}
1078 1088
1079/* Caller has locked intf's usb_device's pm_mutex */
1080static int usb_resume_interface(struct usb_device *udev, 1089static int usb_resume_interface(struct usb_device *udev,
1081 struct usb_interface *intf, pm_message_t msg, int reset_resume) 1090 struct usb_interface *intf, pm_message_t msg, int reset_resume)
1082{ 1091{
1083 struct usb_driver *driver; 1092 struct usb_driver *driver;
1084 int status = 0; 1093 int status = 0;
1085 1094
1086 if (udev->state == USB_STATE_NOTATTACHED || is_active(intf)) 1095 if (udev->state == USB_STATE_NOTATTACHED)
1087 goto done; 1096 goto done;
1088 1097
1089 /* Don't let autoresume interfere with unbinding */ 1098 /* Don't let autoresume interfere with unbinding */
@@ -1134,90 +1143,11 @@ static int usb_resume_interface(struct usb_device *udev,
1134 1143
1135done: 1144done:
1136 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); 1145 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1137 if (status == 0 && intf->condition == USB_INTERFACE_BOUND)
1138 mark_active(intf);
1139 1146
1140 /* Later we will unbind the driver and/or reprobe, if necessary */ 1147 /* Later we will unbind the driver and/or reprobe, if necessary */
1141 return status; 1148 return status;
1142} 1149}
1143 1150
1144#ifdef CONFIG_USB_SUSPEND
1145
1146/* Internal routine to check whether we may autosuspend a device. */
1147static int autosuspend_check(struct usb_device *udev, int reschedule)
1148{
1149 int i;
1150 struct usb_interface *intf;
1151 unsigned long suspend_time, j;
1152
1153 /* For autosuspend, fail fast if anything is in use or autosuspend
1154 * is disabled. Also fail if any interfaces require remote wakeup
1155 * but it isn't available.
1156 */
1157 if (udev->pm_usage_cnt > 0)
1158 return -EBUSY;
1159 if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled)
1160 return -EPERM;
1161
1162 suspend_time = udev->last_busy + udev->autosuspend_delay;
1163 if (udev->actconfig) {
1164 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1165 intf = udev->actconfig->interface[i];
1166 if (!is_active(intf))
1167 continue;
1168 if (atomic_read(&intf->pm_usage_cnt) > 0)
1169 return -EBUSY;
1170 if (intf->needs_remote_wakeup &&
1171 !udev->do_remote_wakeup) {
1172 dev_dbg(&udev->dev, "remote wakeup needed "
1173 "for autosuspend\n");
1174 return -EOPNOTSUPP;
1175 }
1176
1177 /* Don't allow autosuspend if the device will need
1178 * a reset-resume and any of its interface drivers
1179 * doesn't include support.
1180 */
1181 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1182 struct usb_driver *driver;
1183
1184 driver = to_usb_driver(intf->dev.driver);
1185 if (!driver->reset_resume ||
1186 intf->needs_remote_wakeup)
1187 return -EOPNOTSUPP;
1188 }
1189 }
1190 }
1191
1192 /* If everything is okay but the device hasn't been idle for long
1193 * enough, queue a delayed autosuspend request. If the device
1194 * _has_ been idle for long enough and the reschedule flag is set,
1195 * likewise queue a delayed (1 second) autosuspend request.
1196 */
1197 j = jiffies;
1198 if (time_before(j, suspend_time))
1199 reschedule = 1;
1200 else
1201 suspend_time = j + HZ;
1202 if (reschedule) {
1203 if (!timer_pending(&udev->autosuspend.timer)) {
1204 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
1205 round_jiffies_up_relative(suspend_time - j));
1206 }
1207 return -EAGAIN;
1208 }
1209 return 0;
1210}
1211
1212#else
1213
1214static inline int autosuspend_check(struct usb_device *udev, int reschedule)
1215{
1216 return 0;
1217}
1218
1219#endif /* CONFIG_USB_SUSPEND */
1220
1221/** 1151/**
1222 * usb_suspend_both - suspend a USB device and its interfaces 1152 * usb_suspend_both - suspend a USB device and its interfaces
1223 * @udev: the usb_device to suspend 1153 * @udev: the usb_device to suspend
@@ -1229,27 +1159,12 @@ static inline int autosuspend_check(struct usb_device *udev, int reschedule)
1229 * all the interfaces which were suspended are resumed so that they remain 1159 * all the interfaces which were suspended are resumed so that they remain
1230 * in the same state as the device. 1160 * in the same state as the device.
1231 * 1161 *
1232 * If an autosuspend is in progress the routine checks first to make sure 1162 * Autosuspend requests originating from a child device or an interface
1233 * that neither the device itself or any of its active interfaces is in use 1163 * driver may be made without the protection of @udev's device lock, but
1234 * (pm_usage_cnt is greater than 0). If they are, the autosuspend fails. 1164 * all other suspend calls will hold the lock. Usbcore will insure that
1235 * 1165 * method calls do not arrive during bind, unbind, or reset operations.
1236 * If the suspend succeeds, the routine recursively queues an autosuspend 1166 * However drivers must be prepared to handle suspend calls arriving at
1237 * request for @udev's parent device, thereby propagating the change up 1167 * unpredictable times.
1238 * the device tree. If all of the parent's children are now suspended,
1239 * the parent will autosuspend in turn.
1240 *
1241 * The suspend method calls are subject to mutual exclusion under control
1242 * of @udev's pm_mutex. Many of these calls are also under the protection
1243 * of @udev's device lock (including all requests originating outside the
1244 * USB subsystem), but autosuspend requests generated by a child device or
1245 * interface driver may not be. Usbcore will insure that the method calls
1246 * do not arrive during bind, unbind, or reset operations. However, drivers
1247 * must be prepared to handle suspend calls arriving at unpredictable times.
1248 * The only way to block such calls is to do an autoresume (preventing
1249 * autosuspends) while holding @udev's device lock (preventing outside
1250 * suspends).
1251 *
1252 * The caller must hold @udev->pm_mutex.
1253 * 1168 *
1254 * This routine can run only in process context. 1169 * This routine can run only in process context.
1255 */ 1170 */
@@ -1258,20 +1173,11 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1258 int status = 0; 1173 int status = 0;
1259 int i = 0; 1174 int i = 0;
1260 struct usb_interface *intf; 1175 struct usb_interface *intf;
1261 struct usb_device *parent = udev->parent;
1262 1176
1263 if (udev->state == USB_STATE_NOTATTACHED || 1177 if (udev->state == USB_STATE_NOTATTACHED ||
1264 udev->state == USB_STATE_SUSPENDED) 1178 udev->state == USB_STATE_SUSPENDED)
1265 goto done; 1179 goto done;
1266 1180
1267 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1268
1269 if (msg.event & PM_EVENT_AUTO) {
1270 status = autosuspend_check(udev, 0);
1271 if (status < 0)
1272 goto done;
1273 }
1274
1275 /* Suspend all the interfaces and then udev itself */ 1181 /* Suspend all the interfaces and then udev itself */
1276 if (udev->actconfig) { 1182 if (udev->actconfig) {
1277 for (; i < udev->actconfig->desc.bNumInterfaces; i++) { 1183 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
@@ -1286,35 +1192,21 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1286 1192
1287 /* If the suspend failed, resume interfaces that did get suspended */ 1193 /* If the suspend failed, resume interfaces that did get suspended */
1288 if (status != 0) { 1194 if (status != 0) {
1289 pm_message_t msg2; 1195 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1290
1291 msg2.event = msg.event ^ (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1292 while (--i >= 0) { 1196 while (--i >= 0) {
1293 intf = udev->actconfig->interface[i]; 1197 intf = udev->actconfig->interface[i];
1294 usb_resume_interface(udev, intf, msg2, 0); 1198 usb_resume_interface(udev, intf, msg, 0);
1295 } 1199 }
1296 1200
1297 /* Try another autosuspend when the interfaces aren't busy */ 1201 /* If the suspend succeeded then prevent any more URB submissions
1298 if (msg.event & PM_EVENT_AUTO) 1202 * and flush any outstanding URBs.
1299 autosuspend_check(udev, status == -EBUSY);
1300
1301 /* If the suspend succeeded then prevent any more URB submissions,
1302 * flush any outstanding URBs, and propagate the suspend up the tree.
1303 */ 1203 */
1304 } else { 1204 } else {
1305 cancel_delayed_work(&udev->autosuspend);
1306 udev->can_submit = 0; 1205 udev->can_submit = 0;
1307 for (i = 0; i < 16; ++i) { 1206 for (i = 0; i < 16; ++i) {
1308 usb_hcd_flush_endpoint(udev, udev->ep_out[i]); 1207 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1309 usb_hcd_flush_endpoint(udev, udev->ep_in[i]); 1208 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1310 } 1209 }
1311
1312 /* If this is just a FREEZE or a PRETHAW, udev might
1313 * not really be suspended. Only true suspends get
1314 * propagated up the device tree.
1315 */
1316 if (parent && udev->state == USB_STATE_SUSPENDED)
1317 usb_autosuspend_device(parent);
1318 } 1210 }
1319 1211
1320 done: 1212 done:
@@ -1331,23 +1223,12 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1331 * the resume method for @udev and then calls the resume methods for all 1223 * the resume method for @udev and then calls the resume methods for all
1332 * the interface drivers in @udev. 1224 * the interface drivers in @udev.
1333 * 1225 *
1334 * Before starting the resume, the routine calls itself recursively for 1226 * Autoresume requests originating from a child device or an interface
1335 * the parent device of @udev, thereby propagating the change up the device 1227 * driver may be made without the protection of @udev's device lock, but
1336 * tree and assuring that @udev will be able to resume. If the parent is 1228 * all other resume calls will hold the lock. Usbcore will insure that
1337 * unable to resume successfully, the routine fails. 1229 * method calls do not arrive during bind, unbind, or reset operations.
1338 * 1230 * However drivers must be prepared to handle resume calls arriving at
1339 * The resume method calls are subject to mutual exclusion under control 1231 * unpredictable times.
1340 * of @udev's pm_mutex. Many of these calls are also under the protection
1341 * of @udev's device lock (including all requests originating outside the
1342 * USB subsystem), but autoresume requests generated by a child device or
1343 * interface driver may not be. Usbcore will insure that the method calls
1344 * do not arrive during bind, unbind, or reset operations. However, drivers
1345 * must be prepared to handle resume calls arriving at unpredictable times.
1346 * The only way to block such calls is to do an autoresume (preventing
1347 * other autoresumes) while holding @udev's device lock (preventing outside
1348 * resumes).
1349 *
1350 * The caller must hold @udev->pm_mutex.
1351 * 1232 *
1352 * This routine can run only in process context. 1233 * This routine can run only in process context.
1353 */ 1234 */
@@ -1356,48 +1237,18 @@ static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1356 int status = 0; 1237 int status = 0;
1357 int i; 1238 int i;
1358 struct usb_interface *intf; 1239 struct usb_interface *intf;
1359 struct usb_device *parent = udev->parent;
1360 1240
1361 cancel_delayed_work(&udev->autosuspend);
1362 if (udev->state == USB_STATE_NOTATTACHED) { 1241 if (udev->state == USB_STATE_NOTATTACHED) {
1363 status = -ENODEV; 1242 status = -ENODEV;
1364 goto done; 1243 goto done;
1365 } 1244 }
1366 udev->can_submit = 1; 1245 udev->can_submit = 1;
1367 1246
1368 /* Propagate the resume up the tree, if necessary */ 1247 /* Resume the device */
1369 if (udev->state == USB_STATE_SUSPENDED) { 1248 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
1370 if (parent) {
1371 status = usb_autoresume_device(parent);
1372 if (status == 0) {
1373 status = usb_resume_device(udev, msg);
1374 if (status || udev->state ==
1375 USB_STATE_NOTATTACHED) {
1376 usb_autosuspend_device(parent);
1377
1378 /* It's possible usb_resume_device()
1379 * failed after the port was
1380 * unsuspended, causing udev to be
1381 * logically disconnected. We don't
1382 * want usb_disconnect() to autosuspend
1383 * the parent again, so tell it that
1384 * udev disconnected while still
1385 * suspended. */
1386 if (udev->state ==
1387 USB_STATE_NOTATTACHED)
1388 udev->discon_suspended = 1;
1389 }
1390 }
1391 } else {
1392
1393 /* We can't progagate beyond the USB subsystem,
1394 * so if a root hub's controller is suspended
1395 * then we're stuck. */
1396 status = usb_resume_device(udev, msg);
1397 }
1398 } else if (udev->reset_resume)
1399 status = usb_resume_device(udev, msg); 1249 status = usb_resume_device(udev, msg);
1400 1250
1251 /* Resume the interfaces */
1401 if (status == 0 && udev->actconfig) { 1252 if (status == 0 && udev->actconfig) {
1402 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1253 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1403 intf = udev->actconfig->interface[i]; 1254 intf = udev->actconfig->interface[i];
@@ -1413,104 +1264,46 @@ static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1413 return status; 1264 return status;
1414} 1265}
1415 1266
1416/** 1267/* The device lock is held by the PM core */
1417 * usb_external_suspend_device - external suspend of a USB device and its interfaces
1418 * @udev: the usb_device to suspend
1419 * @msg: Power Management message describing this state transition
1420 *
1421 * This routine handles external suspend requests: ones not generated
1422 * internally by a USB driver (autosuspend) but rather coming from the user
1423 * (via sysfs) or the PM core (system sleep). The suspend will be carried
1424 * out regardless of @udev's usage counter or those of its interfaces,
1425 * and regardless of whether or not remote wakeup is enabled. Of course,
1426 * interface drivers still have the option of failing the suspend (if
1427 * there are unsuspended children, for example).
1428 *
1429 * The caller must hold @udev's device lock.
1430 */
1431int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
1432{
1433 int status;
1434
1435 do_unbind_rebind(udev, DO_UNBIND);
1436 usb_pm_lock(udev);
1437 status = usb_suspend_both(udev, msg);
1438 usb_pm_unlock(udev);
1439 return status;
1440}
1441
1442/**
1443 * usb_external_resume_device - external resume of a USB device and its interfaces
1444 * @udev: the usb_device to resume
1445 * @msg: Power Management message describing this state transition
1446 *
1447 * This routine handles external resume requests: ones not generated
1448 * internally by a USB driver (autoresume) but rather coming from the user
1449 * (via sysfs), the PM core (system resume), or the device itself (remote
1450 * wakeup). @udev's usage counter is unaffected.
1451 *
1452 * The caller must hold @udev's device lock.
1453 */
1454int usb_external_resume_device(struct usb_device *udev, pm_message_t msg)
1455{
1456 int status;
1457
1458 usb_pm_lock(udev);
1459 status = usb_resume_both(udev, msg);
1460 udev->last_busy = jiffies;
1461 usb_pm_unlock(udev);
1462 if (status == 0)
1463 do_unbind_rebind(udev, DO_REBIND);
1464
1465 /* Now that the device is awake, we can start trying to autosuspend
1466 * it again. */
1467 if (status == 0)
1468 usb_try_autosuspend_device(udev);
1469 return status;
1470}
1471
1472int usb_suspend(struct device *dev, pm_message_t msg) 1268int usb_suspend(struct device *dev, pm_message_t msg)
1473{ 1269{
1474 struct usb_device *udev; 1270 struct usb_device *udev = to_usb_device(dev);
1475
1476 udev = to_usb_device(dev);
1477 1271
1478 /* If udev is already suspended, we can skip this suspend and 1272 do_unbind_rebind(udev, DO_UNBIND);
1479 * we should also skip the upcoming system resume. High-speed 1273 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1480 * root hubs are an exception; they need to resume whenever the 1274 return usb_suspend_both(udev, msg);
1481 * system wakes up in order for USB-PERSIST port handover to work
1482 * properly.
1483 */
1484 if (udev->state == USB_STATE_SUSPENDED) {
1485 if (udev->parent || udev->speed != USB_SPEED_HIGH)
1486 udev->skip_sys_resume = 1;
1487 return 0;
1488 }
1489
1490 udev->skip_sys_resume = 0;
1491 return usb_external_suspend_device(udev, msg);
1492} 1275}
1493 1276
1277/* The device lock is held by the PM core */
1494int usb_resume(struct device *dev, pm_message_t msg) 1278int usb_resume(struct device *dev, pm_message_t msg)
1495{ 1279{
1496 struct usb_device *udev; 1280 struct usb_device *udev = to_usb_device(dev);
1497 int status; 1281 int status;
1498 1282
1499 udev = to_usb_device(dev); 1283 /* For PM complete calls, all we do is rebind interfaces */
1284 if (msg.event == PM_EVENT_ON) {
1285 if (udev->state != USB_STATE_NOTATTACHED)
1286 do_unbind_rebind(udev, DO_REBIND);
1287 status = 0;
1500 1288
1501 /* If udev->skip_sys_resume is set then udev was already suspended 1289 /* For all other calls, take the device back to full power and
1502 * when the system sleep started, so we don't want to resume it 1290 * tell the PM core in case it was autosuspended previously.
1503 * during this system wakeup.
1504 */ 1291 */
1505 if (udev->skip_sys_resume) 1292 } else {
1506 return 0; 1293 status = usb_resume_both(udev, msg);
1507 status = usb_external_resume_device(udev, msg); 1294 if (status == 0) {
1295 pm_runtime_disable(dev);
1296 pm_runtime_set_active(dev);
1297 pm_runtime_enable(dev);
1298 udev->last_busy = jiffies;
1299 }
1300 }
1508 1301
1509 /* Avoid PM error messages for devices disconnected while suspended 1302 /* Avoid PM error messages for devices disconnected while suspended
1510 * as we'll display regular disconnect messages just a bit later. 1303 * as we'll display regular disconnect messages just a bit later.
1511 */ 1304 */
1512 if (status == -ENODEV) 1305 if (status == -ENODEV)
1513 return 0; 1306 status = 0;
1514 return status; 1307 return status;
1515} 1308}
1516 1309
@@ -1560,54 +1353,6 @@ int usb_disable_autosuspend(struct usb_device *udev)
1560} 1353}
1561EXPORT_SYMBOL_GPL(usb_disable_autosuspend); 1354EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1562 1355
1563/* Internal routine to adjust a device's usage counter and change
1564 * its autosuspend state.
1565 */
1566static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1567{
1568 int status = 0;
1569
1570 usb_pm_lock(udev);
1571 udev->pm_usage_cnt += inc_usage_cnt;
1572 WARN_ON(udev->pm_usage_cnt < 0);
1573 if (inc_usage_cnt)
1574 udev->last_busy = jiffies;
1575 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
1576 if (udev->state == USB_STATE_SUSPENDED)
1577 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1578 if (status != 0)
1579 udev->pm_usage_cnt -= inc_usage_cnt;
1580 else if (inc_usage_cnt)
1581 udev->last_busy = jiffies;
1582 } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) {
1583 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1584 }
1585 usb_pm_unlock(udev);
1586 return status;
1587}
1588
1589/* usb_autosuspend_work - callback routine to autosuspend a USB device */
1590void usb_autosuspend_work(struct work_struct *work)
1591{
1592 struct usb_device *udev =
1593 container_of(work, struct usb_device, autosuspend.work);
1594
1595 usb_autopm_do_device(udev, 0);
1596}
1597
1598/* usb_autoresume_work - callback routine to autoresume a USB device */
1599void usb_autoresume_work(struct work_struct *work)
1600{
1601 struct usb_device *udev =
1602 container_of(work, struct usb_device, autoresume);
1603
1604 /* Wake it up, let the drivers do their thing, and then put it
1605 * back to sleep.
1606 */
1607 if (usb_autopm_do_device(udev, 1) == 0)
1608 usb_autopm_do_device(udev, -1);
1609}
1610
1611/** 1356/**
1612 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces 1357 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1613 * @udev: the usb_device to autosuspend 1358 * @udev: the usb_device to autosuspend
@@ -1616,12 +1361,9 @@ void usb_autoresume_work(struct work_struct *work)
1616 * @udev and wants to allow it to autosuspend. Examples would be when 1361 * @udev and wants to allow it to autosuspend. Examples would be when
1617 * @udev's device file in usbfs is closed or after a configuration change. 1362 * @udev's device file in usbfs is closed or after a configuration change.
1618 * 1363 *
1619 * @udev's usage counter is decremented. If it or any of the usage counters 1364 * @udev's usage counter is decremented; if it drops to 0 and all the
1620 * for an active interface is greater than 0, no autosuspend request will be 1365 * interfaces are inactive then a delayed autosuspend will be attempted.
1621 * queued. (If an interface driver does not support autosuspend then its 1366 * The attempt may fail (see autosuspend_check()).
1622 * usage counter is permanently positive.) Furthermore, if an interface
1623 * driver requires remote-wakeup capability during autosuspend but remote
1624 * wakeup is disabled, the autosuspend will fail.
1625 * 1367 *
1626 * The caller must hold @udev's device lock. 1368 * The caller must hold @udev's device lock.
1627 * 1369 *
@@ -1631,9 +1373,11 @@ void usb_autosuspend_device(struct usb_device *udev)
1631{ 1373{
1632 int status; 1374 int status;
1633 1375
1634 status = usb_autopm_do_device(udev, -1); 1376 udev->last_busy = jiffies;
1635 dev_vdbg(&udev->dev, "%s: cnt %d\n", 1377 status = pm_runtime_put_sync(&udev->dev);
1636 __func__, udev->pm_usage_cnt); 1378 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1379 __func__, atomic_read(&udev->dev.power.usage_count),
1380 status);
1637} 1381}
1638 1382
1639/** 1383/**
@@ -1643,9 +1387,9 @@ void usb_autosuspend_device(struct usb_device *udev)
1643 * This routine should be called when a core subsystem thinks @udev may 1387 * This routine should be called when a core subsystem thinks @udev may
1644 * be ready to autosuspend. 1388 * be ready to autosuspend.
1645 * 1389 *
1646 * @udev's usage counter left unchanged. If it or any of the usage counters 1390 * @udev's usage counter left unchanged. If it is 0 and all the interfaces
1647 * for an active interface is greater than 0, or autosuspend is not allowed 1391 * are inactive then an autosuspend will be attempted. The attempt may
1648 * for any other reason, no autosuspend request will be queued. 1392 * fail or be delayed.
1649 * 1393 *
1650 * The caller must hold @udev's device lock. 1394 * The caller must hold @udev's device lock.
1651 * 1395 *
@@ -1653,9 +1397,12 @@ void usb_autosuspend_device(struct usb_device *udev)
1653 */ 1397 */
1654void usb_try_autosuspend_device(struct usb_device *udev) 1398void usb_try_autosuspend_device(struct usb_device *udev)
1655{ 1399{
1656 usb_autopm_do_device(udev, 0); 1400 int status;
1657 dev_vdbg(&udev->dev, "%s: cnt %d\n", 1401
1658 __func__, udev->pm_usage_cnt); 1402 status = pm_runtime_idle(&udev->dev);
1403 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1404 __func__, atomic_read(&udev->dev.power.usage_count),
1405 status);
1659} 1406}
1660 1407
1661/** 1408/**
@@ -1664,9 +1411,9 @@ void usb_try_autosuspend_device(struct usb_device *udev)
1664 * 1411 *
1665 * This routine should be called when a core subsystem wants to use @udev 1412 * This routine should be called when a core subsystem wants to use @udev
1666 * and needs to guarantee that it is not suspended. No autosuspend will 1413 * and needs to guarantee that it is not suspended. No autosuspend will
1667 * occur until usb_autosuspend_device is called. (Note that this will not 1414 * occur until usb_autosuspend_device() is called. (Note that this will
1668 * prevent suspend events originating in the PM core.) Examples would be 1415 * not prevent suspend events originating in the PM core.) Examples would
1669 * when @udev's device file in usbfs is opened or when a remote-wakeup 1416 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1670 * request is received. 1417 * request is received.
1671 * 1418 *
1672 * @udev's usage counter is incremented to prevent subsequent autosuspends. 1419 * @udev's usage counter is incremented to prevent subsequent autosuspends.
@@ -1680,42 +1427,14 @@ int usb_autoresume_device(struct usb_device *udev)
1680{ 1427{
1681 int status; 1428 int status;
1682 1429
1683 status = usb_autopm_do_device(udev, 1); 1430 status = pm_runtime_get_sync(&udev->dev);
1684 dev_vdbg(&udev->dev, "%s: status %d cnt %d\n", 1431 if (status < 0)
1685 __func__, status, udev->pm_usage_cnt); 1432 pm_runtime_put_sync(&udev->dev);
1686 return status; 1433 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1687} 1434 __func__, atomic_read(&udev->dev.power.usage_count),
1688 1435 status);
1689/* Internal routine to adjust an interface's usage counter and change 1436 if (status > 0)
1690 * its device's autosuspend state. 1437 status = 0;
1691 */
1692static int usb_autopm_do_interface(struct usb_interface *intf,
1693 int inc_usage_cnt)
1694{
1695 struct usb_device *udev = interface_to_usbdev(intf);
1696 int status = 0;
1697
1698 usb_pm_lock(udev);
1699 if (intf->condition == USB_INTERFACE_UNBOUND)
1700 status = -ENODEV;
1701 else {
1702 atomic_add(inc_usage_cnt, &intf->pm_usage_cnt);
1703 udev->last_busy = jiffies;
1704 if (inc_usage_cnt >= 0 &&
1705 atomic_read(&intf->pm_usage_cnt) > 0) {
1706 if (udev->state == USB_STATE_SUSPENDED)
1707 status = usb_resume_both(udev,
1708 PMSG_AUTO_RESUME);
1709 if (status != 0)
1710 atomic_sub(inc_usage_cnt, &intf->pm_usage_cnt);
1711 else
1712 udev->last_busy = jiffies;
1713 } else if (inc_usage_cnt <= 0 &&
1714 atomic_read(&intf->pm_usage_cnt) <= 0) {
1715 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1716 }
1717 }
1718 usb_pm_unlock(udev);
1719 return status; 1438 return status;
1720} 1439}
1721 1440
@@ -1729,34 +1448,25 @@ static int usb_autopm_do_interface(struct usb_interface *intf,
1729 * closed. 1448 * closed.
1730 * 1449 *
1731 * The routine decrements @intf's usage counter. When the counter reaches 1450 * The routine decrements @intf's usage counter. When the counter reaches
1732 * 0, a delayed autosuspend request for @intf's device is queued. When 1451 * 0, a delayed autosuspend request for @intf's device is attempted. The
1733 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all 1452 * attempt may fail (see autosuspend_check()).
1734 * the other usage counters for the sibling interfaces and @intf's
1735 * usb_device, the device and all its interfaces will be autosuspended.
1736 *
1737 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1738 * core will not change its value other than the increment and decrement
1739 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1740 * may use this simple counter-oriented discipline or may set the value
1741 * any way it likes.
1742 * 1453 *
1743 * If the driver has set @intf->needs_remote_wakeup then autosuspend will 1454 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1744 * take place only if the device's remote-wakeup facility is enabled. 1455 * take place only if the device's remote-wakeup facility is enabled.
1745 * 1456 *
1746 * Suspend method calls queued by this routine can arrive at any time
1747 * while @intf is resumed and its usage counter is equal to 0. They are
1748 * not protected by the usb_device's lock but only by its pm_mutex.
1749 * Drivers must provide their own synchronization.
1750 *
1751 * This routine can run only in process context. 1457 * This routine can run only in process context.
1752 */ 1458 */
1753void usb_autopm_put_interface(struct usb_interface *intf) 1459void usb_autopm_put_interface(struct usb_interface *intf)
1754{ 1460{
1755 int status; 1461 struct usb_device *udev = interface_to_usbdev(intf);
1462 int status;
1756 1463
1757 status = usb_autopm_do_interface(intf, -1); 1464 udev->last_busy = jiffies;
1758 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1465 atomic_dec(&intf->pm_usage_cnt);
1759 __func__, status, atomic_read(&intf->pm_usage_cnt)); 1466 status = pm_runtime_put_sync(&intf->dev);
1467 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1468 __func__, atomic_read(&intf->dev.power.usage_count),
1469 status);
1760} 1470}
1761EXPORT_SYMBOL_GPL(usb_autopm_put_interface); 1471EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1762 1472
@@ -1764,11 +1474,11 @@ EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1764 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter 1474 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1765 * @intf: the usb_interface whose counter should be decremented 1475 * @intf: the usb_interface whose counter should be decremented
1766 * 1476 *
1767 * This routine does essentially the same thing as 1477 * This routine does much the same thing as usb_autopm_put_interface():
1768 * usb_autopm_put_interface(): it decrements @intf's usage counter and 1478 * It decrements @intf's usage counter and schedules a delayed
1769 * queues a delayed autosuspend request if the counter is <= 0. The 1479 * autosuspend request if the counter is <= 0. The difference is that it
1770 * difference is that it does not acquire the device's pm_mutex; 1480 * does not perform any synchronization; callers should hold a private
1771 * callers must handle all synchronization issues themselves. 1481 * lock and handle all synchronization issues themselves.
1772 * 1482 *
1773 * Typically a driver would call this routine during an URB's completion 1483 * Typically a driver would call this routine during an URB's completion
1774 * handler, if no more URBs were pending. 1484 * handler, if no more URBs were pending.
@@ -1778,28 +1488,58 @@ EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1778void usb_autopm_put_interface_async(struct usb_interface *intf) 1488void usb_autopm_put_interface_async(struct usb_interface *intf)
1779{ 1489{
1780 struct usb_device *udev = interface_to_usbdev(intf); 1490 struct usb_device *udev = interface_to_usbdev(intf);
1491 unsigned long last_busy;
1781 int status = 0; 1492 int status = 0;
1782 1493
1783 if (intf->condition == USB_INTERFACE_UNBOUND) { 1494 last_busy = udev->last_busy;
1784 status = -ENODEV; 1495 udev->last_busy = jiffies;
1785 } else { 1496 atomic_dec(&intf->pm_usage_cnt);
1786 udev->last_busy = jiffies; 1497 pm_runtime_put_noidle(&intf->dev);
1787 atomic_dec(&intf->pm_usage_cnt); 1498
1788 if (udev->autosuspend_disabled || udev->autosuspend_delay < 0) 1499 if (!udev->autosuspend_disabled) {
1789 status = -EPERM; 1500 /* Optimization: Don't schedule a delayed autosuspend if
1790 else if (atomic_read(&intf->pm_usage_cnt) <= 0 && 1501 * the timer is already running and the expiration time
1791 !timer_pending(&udev->autosuspend.timer)) { 1502 * wouldn't change.
1792 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend, 1503 *
1504 * We have to use the interface's timer. Attempts to
1505 * schedule a suspend for the device would fail because
1506 * the interface is still active.
1507 */
1508 if (intf->dev.power.timer_expires == 0 ||
1509 round_jiffies_up(last_busy) !=
1510 round_jiffies_up(jiffies)) {
1511 status = pm_schedule_suspend(&intf->dev,
1512 jiffies_to_msecs(
1793 round_jiffies_up_relative( 1513 round_jiffies_up_relative(
1794 udev->autosuspend_delay)); 1514 udev->autosuspend_delay)));
1795 } 1515 }
1796 } 1516 }
1797 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1517 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1798 __func__, status, atomic_read(&intf->pm_usage_cnt)); 1518 __func__, atomic_read(&intf->dev.power.usage_count),
1519 status);
1799} 1520}
1800EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async); 1521EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1801 1522
1802/** 1523/**
1524 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1525 * @intf: the usb_interface whose counter should be decremented
1526 *
1527 * This routine decrements @intf's usage counter but does not carry out an
1528 * autosuspend.
1529 *
1530 * This routine can run in atomic context.
1531 */
1532void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1533{
1534 struct usb_device *udev = interface_to_usbdev(intf);
1535
1536 udev->last_busy = jiffies;
1537 atomic_dec(&intf->pm_usage_cnt);
1538 pm_runtime_put_noidle(&intf->dev);
1539}
1540EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1541
1542/**
1803 * usb_autopm_get_interface - increment a USB interface's PM-usage counter 1543 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1804 * @intf: the usb_interface whose counter should be incremented 1544 * @intf: the usb_interface whose counter should be incremented
1805 * 1545 *
@@ -1811,25 +1551,8 @@ EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1811 * or @intf is unbound. A typical example would be a character-device 1551 * or @intf is unbound. A typical example would be a character-device
1812 * driver when its device file is opened. 1552 * driver when its device file is opened.
1813 * 1553 *
1814 * 1554 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1815 * The routine increments @intf's usage counter. (However if the 1555 * However if the autoresume fails then the counter is re-decremented.
1816 * autoresume fails then the counter is re-decremented.) So long as the
1817 * counter is greater than 0, autosuspend will not be allowed for @intf
1818 * or its usb_device. When the driver is finished using @intf it should
1819 * call usb_autopm_put_interface() to decrement the usage counter and
1820 * queue a delayed autosuspend request (if the counter is <= 0).
1821 *
1822 *
1823 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1824 * core will not change its value other than the increment and decrement
1825 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1826 * may use this simple counter-oriented discipline or may set the value
1827 * any way it likes.
1828 *
1829 * Resume method calls generated by this routine can arrive at any time
1830 * while @intf is suspended. They are not protected by the usb_device's
1831 * lock but only by its pm_mutex. Drivers must provide their own
1832 * synchronization.
1833 * 1556 *
1834 * This routine can run only in process context. 1557 * This routine can run only in process context.
1835 */ 1558 */
@@ -1837,9 +1560,16 @@ int usb_autopm_get_interface(struct usb_interface *intf)
1837{ 1560{
1838 int status; 1561 int status;
1839 1562
1840 status = usb_autopm_do_interface(intf, 1); 1563 status = pm_runtime_get_sync(&intf->dev);
1841 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1564 if (status < 0)
1842 __func__, status, atomic_read(&intf->pm_usage_cnt)); 1565 pm_runtime_put_sync(&intf->dev);
1566 else
1567 atomic_inc(&intf->pm_usage_cnt);
1568 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1569 __func__, atomic_read(&intf->dev.power.usage_count),
1570 status);
1571 if (status > 0)
1572 status = 0;
1843 return status; 1573 return status;
1844} 1574}
1845EXPORT_SYMBOL_GPL(usb_autopm_get_interface); 1575EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
@@ -1849,41 +1579,201 @@ EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1849 * @intf: the usb_interface whose counter should be incremented 1579 * @intf: the usb_interface whose counter should be incremented
1850 * 1580 *
1851 * This routine does much the same thing as 1581 * This routine does much the same thing as
1852 * usb_autopm_get_interface(): it increments @intf's usage counter and 1582 * usb_autopm_get_interface(): It increments @intf's usage counter and
1853 * queues an autoresume request if the result is > 0. The differences 1583 * queues an autoresume request if the device is suspended. The
1854 * are that it does not acquire the device's pm_mutex (callers must 1584 * differences are that it does not perform any synchronization (callers
1855 * handle all synchronization issues themselves), and it does not 1585 * should hold a private lock and handle all synchronization issues
1856 * autoresume the device directly (it only queues a request). After a 1586 * themselves), and it does not autoresume the device directly (it only
1857 * successful call, the device will generally not yet be resumed. 1587 * queues a request). After a successful call, the device may not yet be
1588 * resumed.
1858 * 1589 *
1859 * This routine can run in atomic context. 1590 * This routine can run in atomic context.
1860 */ 1591 */
1861int usb_autopm_get_interface_async(struct usb_interface *intf) 1592int usb_autopm_get_interface_async(struct usb_interface *intf)
1862{ 1593{
1863 struct usb_device *udev = interface_to_usbdev(intf); 1594 int status = 0;
1864 int status = 0; 1595 enum rpm_status s;
1865 1596
1866 if (intf->condition == USB_INTERFACE_UNBOUND) 1597 /* Don't request a resume unless the interface is already suspending
1867 status = -ENODEV; 1598 * or suspended. Doing so would force a running suspend timer to be
1868 else { 1599 * cancelled.
1600 */
1601 pm_runtime_get_noresume(&intf->dev);
1602 s = ACCESS_ONCE(intf->dev.power.runtime_status);
1603 if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
1604 status = pm_request_resume(&intf->dev);
1605
1606 if (status < 0 && status != -EINPROGRESS)
1607 pm_runtime_put_noidle(&intf->dev);
1608 else
1869 atomic_inc(&intf->pm_usage_cnt); 1609 atomic_inc(&intf->pm_usage_cnt);
1870 if (atomic_read(&intf->pm_usage_cnt) > 0 && 1610 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1871 udev->state == USB_STATE_SUSPENDED) 1611 __func__, atomic_read(&intf->dev.power.usage_count),
1872 queue_work(ksuspend_usb_wq, &udev->autoresume); 1612 status);
1873 } 1613 if (status > 0)
1874 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1614 status = 0;
1875 __func__, status, atomic_read(&intf->pm_usage_cnt));
1876 return status; 1615 return status;
1877} 1616}
1878EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async); 1617EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1879 1618
1880#else 1619/**
1620 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1621 * @intf: the usb_interface whose counter should be incremented
1622 *
1623 * This routine increments @intf's usage counter but does not carry out an
1624 * autoresume.
1625 *
1626 * This routine can run in atomic context.
1627 */
1628void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1629{
1630 struct usb_device *udev = interface_to_usbdev(intf);
1631
1632 udev->last_busy = jiffies;
1633 atomic_inc(&intf->pm_usage_cnt);
1634 pm_runtime_get_noresume(&intf->dev);
1635}
1636EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1637
1638/* Internal routine to check whether we may autosuspend a device. */
1639static int autosuspend_check(struct usb_device *udev)
1640{
1641 int i;
1642 struct usb_interface *intf;
1643 unsigned long suspend_time, j;
1644
1645 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1646 * any interface drivers require remote wakeup but it isn't available.
1647 */
1648 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1649 if (udev->actconfig) {
1650 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1651 intf = udev->actconfig->interface[i];
1652
1653 /* We don't need to check interfaces that are
1654 * disabled for runtime PM. Either they are unbound
1655 * or else their drivers don't support autosuspend
1656 * and so they are permanently active.
1657 */
1658 if (intf->dev.power.disable_depth)
1659 continue;
1660 if (atomic_read(&intf->dev.power.usage_count) > 0)
1661 return -EBUSY;
1662 if (intf->needs_remote_wakeup &&
1663 !udev->do_remote_wakeup) {
1664 dev_dbg(&udev->dev, "remote wakeup needed "
1665 "for autosuspend\n");
1666 return -EOPNOTSUPP;
1667 }
1668
1669 /* Don't allow autosuspend if the device will need
1670 * a reset-resume and any of its interface drivers
1671 * doesn't include support or needs remote wakeup.
1672 */
1673 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1674 struct usb_driver *driver;
1675
1676 driver = to_usb_driver(intf->dev.driver);
1677 if (!driver->reset_resume ||
1678 intf->needs_remote_wakeup)
1679 return -EOPNOTSUPP;
1680 }
1681 }
1682 }
1683
1684 /* If everything is okay but the device hasn't been idle for long
1685 * enough, queue a delayed autosuspend request.
1686 */
1687 j = ACCESS_ONCE(jiffies);
1688 suspend_time = udev->last_busy + udev->autosuspend_delay;
1689 if (time_before(j, suspend_time)) {
1690 pm_schedule_suspend(&udev->dev, jiffies_to_msecs(
1691 round_jiffies_up_relative(suspend_time - j)));
1692 return -EAGAIN;
1693 }
1694 return 0;
1695}
1696
1697static int usb_runtime_suspend(struct device *dev)
1698{
1699 int status = 0;
1881 1700
1882void usb_autosuspend_work(struct work_struct *work) 1701 /* A USB device can be suspended if it passes the various autosuspend
1883{} 1702 * checks. Runtime suspend for a USB device means suspending all the
1703 * interfaces and then the device itself.
1704 */
1705 if (is_usb_device(dev)) {
1706 struct usb_device *udev = to_usb_device(dev);
1707
1708 if (autosuspend_check(udev) != 0)
1709 return -EAGAIN;
1710
1711 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1712
1713 /* If an interface fails the suspend, adjust the last_busy
1714 * time so that we don't get another suspend attempt right
1715 * away.
1716 */
1717 if (status) {
1718 udev->last_busy = jiffies +
1719 (udev->autosuspend_delay == 0 ?
1720 HZ/2 : 0);
1721 }
1722
1723 /* Prevent the parent from suspending immediately after */
1724 else if (udev->parent) {
1725 udev->parent->last_busy = jiffies;
1726 }
1727 }
1728
1729 /* Runtime suspend for a USB interface doesn't mean anything. */
1730 return status;
1731}
1732
1733static int usb_runtime_resume(struct device *dev)
1734{
1735 /* Runtime resume for a USB device means resuming both the device
1736 * and all its interfaces.
1737 */
1738 if (is_usb_device(dev)) {
1739 struct usb_device *udev = to_usb_device(dev);
1740 int status;
1741
1742 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1743 udev->last_busy = jiffies;
1744 return status;
1745 }
1746
1747 /* Runtime resume for a USB interface doesn't mean anything. */
1748 return 0;
1749}
1750
1751static int usb_runtime_idle(struct device *dev)
1752{
1753 /* An idle USB device can be suspended if it passes the various
1754 * autosuspend checks. An idle interface can be suspended at
1755 * any time.
1756 */
1757 if (is_usb_device(dev)) {
1758 struct usb_device *udev = to_usb_device(dev);
1759
1760 if (autosuspend_check(udev) != 0)
1761 return 0;
1762 }
1763
1764 pm_runtime_suspend(dev);
1765 return 0;
1766}
1767
1768static struct dev_pm_ops usb_bus_pm_ops = {
1769 .runtime_suspend = usb_runtime_suspend,
1770 .runtime_resume = usb_runtime_resume,
1771 .runtime_idle = usb_runtime_idle,
1772};
1773
1774#else
1884 1775
1885void usb_autoresume_work(struct work_struct *work) 1776#define usb_bus_pm_ops (*(struct dev_pm_ops *) NULL)
1886{}
1887 1777
1888#endif /* CONFIG_USB_SUSPEND */ 1778#endif /* CONFIG_USB_SUSPEND */
1889 1779
@@ -1891,4 +1781,5 @@ struct bus_type usb_bus_type = {
1891 .name = "usb", 1781 .name = "usb",
1892 .match = usb_device_match, 1782 .match = usb_device_match,
1893 .uevent = usb_uevent, 1783 .uevent = usb_uevent,
1784 .pm = &usb_bus_pm_ops,
1894}; 1785};
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index fc4290b6691c..b07ba051118d 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -39,6 +39,7 @@
39#include <linux/platform_device.h> 39#include <linux/platform_device.h>
40#include <linux/workqueue.h> 40#include <linux/workqueue.h>
41#include <linux/mutex.h> 41#include <linux/mutex.h>
42#include <linux/pm_runtime.h>
42 43
43#include <linux/usb.h> 44#include <linux/usb.h>
44 45
@@ -1858,6 +1859,10 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
1858 return status; 1859 return status;
1859} 1860}
1860 1861
1862#endif /* CONFIG_PM */
1863
1864#ifdef CONFIG_USB_SUSPEND
1865
1861/* Workqueue routine for root-hub remote wakeup */ 1866/* Workqueue routine for root-hub remote wakeup */
1862static void hcd_resume_work(struct work_struct *work) 1867static void hcd_resume_work(struct work_struct *work)
1863{ 1868{
@@ -1884,12 +1889,12 @@ void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
1884 1889
1885 spin_lock_irqsave (&hcd_root_hub_lock, flags); 1890 spin_lock_irqsave (&hcd_root_hub_lock, flags);
1886 if (hcd->rh_registered) 1891 if (hcd->rh_registered)
1887 queue_work(ksuspend_usb_wq, &hcd->wakeup_work); 1892 queue_work(pm_wq, &hcd->wakeup_work);
1888 spin_unlock_irqrestore (&hcd_root_hub_lock, flags); 1893 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
1889} 1894}
1890EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub); 1895EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
1891 1896
1892#endif 1897#endif /* CONFIG_USB_SUSPEND */
1893 1898
1894/*-------------------------------------------------------------------------*/ 1899/*-------------------------------------------------------------------------*/
1895 1900
@@ -2034,7 +2039,7 @@ struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
2034 init_timer(&hcd->rh_timer); 2039 init_timer(&hcd->rh_timer);
2035 hcd->rh_timer.function = rh_timer_func; 2040 hcd->rh_timer.function = rh_timer_func;
2036 hcd->rh_timer.data = (unsigned long) hcd; 2041 hcd->rh_timer.data = (unsigned long) hcd;
2037#ifdef CONFIG_PM 2042#ifdef CONFIG_USB_SUSPEND
2038 INIT_WORK(&hcd->wakeup_work, hcd_resume_work); 2043 INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
2039#endif 2044#endif
2040 mutex_init(&hcd->bandwidth_mutex); 2045 mutex_init(&hcd->bandwidth_mutex);
@@ -2234,7 +2239,7 @@ void usb_remove_hcd(struct usb_hcd *hcd)
2234 hcd->rh_registered = 0; 2239 hcd->rh_registered = 0;
2235 spin_unlock_irq (&hcd_root_hub_lock); 2240 spin_unlock_irq (&hcd_root_hub_lock);
2236 2241
2237#ifdef CONFIG_PM 2242#ifdef CONFIG_USB_SUSPEND
2238 cancel_work_sync(&hcd->wakeup_work); 2243 cancel_work_sync(&hcd->wakeup_work);
2239#endif 2244#endif
2240 2245
diff --git a/drivers/usb/core/hcd.h b/drivers/usb/core/hcd.h
index 70a7e490f81b..8953ded69541 100644
--- a/drivers/usb/core/hcd.h
+++ b/drivers/usb/core/hcd.h
@@ -80,7 +80,7 @@ struct usb_hcd {
80 80
81 struct timer_list rh_timer; /* drives root-hub polling */ 81 struct timer_list rh_timer; /* drives root-hub polling */
82 struct urb *status_urb; /* the current status urb */ 82 struct urb *status_urb; /* the current status urb */
83#ifdef CONFIG_PM 83#ifdef CONFIG_USB_SUSPEND
84 struct work_struct wakeup_work; /* for remote wakeup */ 84 struct work_struct wakeup_work; /* for remote wakeup */
85#endif 85#endif
86 86
@@ -464,16 +464,20 @@ extern int usb_find_interface_driver(struct usb_device *dev,
464#define usb_endpoint_out(ep_dir) (!((ep_dir) & USB_DIR_IN)) 464#define usb_endpoint_out(ep_dir) (!((ep_dir) & USB_DIR_IN))
465 465
466#ifdef CONFIG_PM 466#ifdef CONFIG_PM
467extern void usb_hcd_resume_root_hub(struct usb_hcd *hcd);
468extern void usb_root_hub_lost_power(struct usb_device *rhdev); 467extern void usb_root_hub_lost_power(struct usb_device *rhdev);
469extern int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg); 468extern int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg);
470extern int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg); 469extern int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg);
470#endif /* CONFIG_PM */
471
472#ifdef CONFIG_USB_SUSPEND
473extern void usb_hcd_resume_root_hub(struct usb_hcd *hcd);
471#else 474#else
472static inline void usb_hcd_resume_root_hub(struct usb_hcd *hcd) 475static inline void usb_hcd_resume_root_hub(struct usb_hcd *hcd)
473{ 476{
474 return; 477 return;
475} 478}
476#endif /* CONFIG_PM */ 479#endif /* CONFIG_USB_SUSPEND */
480
477 481
478/* 482/*
479 * USB device fs stuff 483 * USB device fs stuff
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 746f26f222ab..0e0a190bbd00 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -22,6 +22,7 @@
22#include <linux/kthread.h> 22#include <linux/kthread.h>
23#include <linux/mutex.h> 23#include <linux/mutex.h>
24#include <linux/freezer.h> 24#include <linux/freezer.h>
25#include <linux/pm_runtime.h>
25 26
26#include <asm/uaccess.h> 27#include <asm/uaccess.h>
27#include <asm/byteorder.h> 28#include <asm/byteorder.h>
@@ -71,7 +72,6 @@ struct usb_hub {
71 72
72 unsigned mA_per_port; /* current for each child */ 73 unsigned mA_per_port; /* current for each child */
73 74
74 unsigned init_done:1;
75 unsigned limited_power:1; 75 unsigned limited_power:1;
76 unsigned quiescing:1; 76 unsigned quiescing:1;
77 unsigned disconnected:1; 77 unsigned disconnected:1;
@@ -820,7 +820,6 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
820 } 820 }
821 init3: 821 init3:
822 hub->quiescing = 0; 822 hub->quiescing = 0;
823 hub->init_done = 1;
824 823
825 status = usb_submit_urb(hub->urb, GFP_NOIO); 824 status = usb_submit_urb(hub->urb, GFP_NOIO);
826 if (status < 0) 825 if (status < 0)
@@ -861,11 +860,6 @@ static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
861 int i; 860 int i;
862 861
863 cancel_delayed_work_sync(&hub->init_work); 862 cancel_delayed_work_sync(&hub->init_work);
864 if (!hub->init_done) {
865 hub->init_done = 1;
866 usb_autopm_put_interface_no_suspend(
867 to_usb_interface(hub->intfdev));
868 }
869 863
870 /* khubd and related activity won't re-trigger */ 864 /* khubd and related activity won't re-trigger */
871 hub->quiescing = 1; 865 hub->quiescing = 1;
@@ -1405,10 +1399,8 @@ static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1405 if (udev->children[i]) 1399 if (udev->children[i])
1406 recursively_mark_NOTATTACHED(udev->children[i]); 1400 recursively_mark_NOTATTACHED(udev->children[i]);
1407 } 1401 }
1408 if (udev->state == USB_STATE_SUSPENDED) { 1402 if (udev->state == USB_STATE_SUSPENDED)
1409 udev->discon_suspended = 1;
1410 udev->active_duration -= jiffies; 1403 udev->active_duration -= jiffies;
1411 }
1412 udev->state = USB_STATE_NOTATTACHED; 1404 udev->state = USB_STATE_NOTATTACHED;
1413} 1405}
1414 1406
@@ -1532,31 +1524,6 @@ static void update_address(struct usb_device *udev, int devnum)
1532 udev->devnum = devnum; 1524 udev->devnum = devnum;
1533} 1525}
1534 1526
1535#ifdef CONFIG_USB_SUSPEND
1536
1537static void usb_stop_pm(struct usb_device *udev)
1538{
1539 /* Synchronize with the ksuspend thread to prevent any more
1540 * autosuspend requests from being submitted, and decrement
1541 * the parent's count of unsuspended children.
1542 */
1543 usb_pm_lock(udev);
1544 if (udev->parent && !udev->discon_suspended)
1545 usb_autosuspend_device(udev->parent);
1546 usb_pm_unlock(udev);
1547
1548 /* Stop any autosuspend or autoresume requests already submitted */
1549 cancel_delayed_work_sync(&udev->autosuspend);
1550 cancel_work_sync(&udev->autoresume);
1551}
1552
1553#else
1554
1555static inline void usb_stop_pm(struct usb_device *udev)
1556{ }
1557
1558#endif
1559
1560/** 1527/**
1561 * usb_disconnect - disconnect a device (usbcore-internal) 1528 * usb_disconnect - disconnect a device (usbcore-internal)
1562 * @pdev: pointer to device being disconnected 1529 * @pdev: pointer to device being disconnected
@@ -1625,8 +1592,6 @@ void usb_disconnect(struct usb_device **pdev)
1625 *pdev = NULL; 1592 *pdev = NULL;
1626 spin_unlock_irq(&device_state_lock); 1593 spin_unlock_irq(&device_state_lock);
1627 1594
1628 usb_stop_pm(udev);
1629
1630 put_device(&udev->dev); 1595 put_device(&udev->dev);
1631} 1596}
1632 1597
@@ -1803,9 +1768,6 @@ int usb_new_device(struct usb_device *udev)
1803 int err; 1768 int err;
1804 1769
1805 if (udev->parent) { 1770 if (udev->parent) {
1806 /* Increment the parent's count of unsuspended children */
1807 usb_autoresume_device(udev->parent);
1808
1809 /* Initialize non-root-hub device wakeup to disabled; 1771 /* Initialize non-root-hub device wakeup to disabled;
1810 * device (un)configuration controls wakeup capable 1772 * device (un)configuration controls wakeup capable
1811 * sysfs power/wakeup controls wakeup enabled/disabled 1773 * sysfs power/wakeup controls wakeup enabled/disabled
@@ -1814,6 +1776,10 @@ int usb_new_device(struct usb_device *udev)
1814 device_set_wakeup_enable(&udev->dev, 1); 1776 device_set_wakeup_enable(&udev->dev, 1);
1815 } 1777 }
1816 1778
1779 /* Tell the runtime-PM framework the device is active */
1780 pm_runtime_set_active(&udev->dev);
1781 pm_runtime_enable(&udev->dev);
1782
1817 usb_detect_quirks(udev); 1783 usb_detect_quirks(udev);
1818 err = usb_enumerate_device(udev); /* Read descriptors */ 1784 err = usb_enumerate_device(udev); /* Read descriptors */
1819 if (err < 0) 1785 if (err < 0)
@@ -1844,7 +1810,8 @@ int usb_new_device(struct usb_device *udev)
1844 1810
1845fail: 1811fail:
1846 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 1812 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1847 usb_stop_pm(udev); 1813 pm_runtime_disable(&udev->dev);
1814 pm_runtime_set_suspended(&udev->dev);
1848 return err; 1815 return err;
1849} 1816}
1850 1817
@@ -2408,8 +2375,11 @@ int usb_remote_wakeup(struct usb_device *udev)
2408 2375
2409 if (udev->state == USB_STATE_SUSPENDED) { 2376 if (udev->state == USB_STATE_SUSPENDED) {
2410 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 2377 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
2411 usb_mark_last_busy(udev); 2378 status = usb_autoresume_device(udev);
2412 status = usb_external_resume_device(udev, PMSG_REMOTE_RESUME); 2379 if (status == 0) {
2380 /* Let the drivers do their thing, then... */
2381 usb_autosuspend_device(udev);
2382 }
2413 } 2383 }
2414 return status; 2384 return status;
2415} 2385}
@@ -2446,11 +2416,6 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
2446 return status; 2416 return status;
2447} 2417}
2448 2418
2449int usb_remote_wakeup(struct usb_device *udev)
2450{
2451 return 0;
2452}
2453
2454#endif 2419#endif
2455 2420
2456static int hub_suspend(struct usb_interface *intf, pm_message_t msg) 2421static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
@@ -3268,7 +3233,7 @@ static void hub_events(void)
3268 * disconnected while waiting for the lock to succeed. */ 3233 * disconnected while waiting for the lock to succeed. */
3269 usb_lock_device(hdev); 3234 usb_lock_device(hdev);
3270 if (unlikely(hub->disconnected)) 3235 if (unlikely(hub->disconnected))
3271 goto loop2; 3236 goto loop_disconnected;
3272 3237
3273 /* If the hub has died, clean up after it */ 3238 /* If the hub has died, clean up after it */
3274 if (hdev->state == USB_STATE_NOTATTACHED) { 3239 if (hdev->state == USB_STATE_NOTATTACHED) {
@@ -3428,7 +3393,7 @@ static void hub_events(void)
3428 * kick_khubd() and allow autosuspend. 3393 * kick_khubd() and allow autosuspend.
3429 */ 3394 */
3430 usb_autopm_put_interface(intf); 3395 usb_autopm_put_interface(intf);
3431 loop2: 3396 loop_disconnected:
3432 usb_unlock_device(hdev); 3397 usb_unlock_device(hdev);
3433 kref_put(&hub->kref, hub_release); 3398 kref_put(&hub->kref, hub_release);
3434 3399
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index df73574a9cc9..73de41bb2546 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1843,7 +1843,6 @@ free_interfaces:
1843 intf->dev.dma_mask = dev->dev.dma_mask; 1843 intf->dev.dma_mask = dev->dev.dma_mask;
1844 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device); 1844 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
1845 device_initialize(&intf->dev); 1845 device_initialize(&intf->dev);
1846 mark_quiesced(intf);
1847 dev_set_name(&intf->dev, "%d-%s:%d.%d", 1846 dev_set_name(&intf->dev, "%d-%s:%d.%d",
1848 dev->bus->busnum, dev->devpath, 1847 dev->bus->busnum, dev->devpath,
1849 configuration, alt->desc.bInterfaceNumber); 1848 configuration, alt->desc.bInterfaceNumber);
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 0daff0d968ba..32966ccdff63 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -49,9 +49,6 @@ const char *usbcore_name = "usbcore";
49 49
50static int nousb; /* Disable USB when built into kernel image */ 50static int nousb; /* Disable USB when built into kernel image */
51 51
52/* Workqueue for autosuspend and for remote wakeup of root hubs */
53struct workqueue_struct *ksuspend_usb_wq;
54
55#ifdef CONFIG_USB_SUSPEND 52#ifdef CONFIG_USB_SUSPEND
56static int usb_autosuspend_delay = 2; /* Default delay value, 53static int usb_autosuspend_delay = 2; /* Default delay value,
57 * in seconds */ 54 * in seconds */
@@ -264,23 +261,6 @@ static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
264 261
265#ifdef CONFIG_PM 262#ifdef CONFIG_PM
266 263
267static int ksuspend_usb_init(void)
268{
269 /* This workqueue is supposed to be both freezable and
270 * singlethreaded. Its job doesn't justify running on more
271 * than one CPU.
272 */
273 ksuspend_usb_wq = create_freezeable_workqueue("ksuspend_usbd");
274 if (!ksuspend_usb_wq)
275 return -ENOMEM;
276 return 0;
277}
278
279static void ksuspend_usb_cleanup(void)
280{
281 destroy_workqueue(ksuspend_usb_wq);
282}
283
284/* USB device Power-Management thunks. 264/* USB device Power-Management thunks.
285 * There's no need to distinguish here between quiescing a USB device 265 * There's no need to distinguish here between quiescing a USB device
286 * and powering it down; the generic_suspend() routine takes care of 266 * and powering it down; the generic_suspend() routine takes care of
@@ -296,7 +276,7 @@ static int usb_dev_prepare(struct device *dev)
296static void usb_dev_complete(struct device *dev) 276static void usb_dev_complete(struct device *dev)
297{ 277{
298 /* Currently used only for rebinding interfaces */ 278 /* Currently used only for rebinding interfaces */
299 usb_resume(dev, PMSG_RESUME); /* Message event is meaningless */ 279 usb_resume(dev, PMSG_ON); /* FIXME: change to PMSG_COMPLETE */
300} 280}
301 281
302static int usb_dev_suspend(struct device *dev) 282static int usb_dev_suspend(struct device *dev)
@@ -342,9 +322,7 @@ static const struct dev_pm_ops usb_device_pm_ops = {
342 322
343#else 323#else
344 324
345#define ksuspend_usb_init() 0 325#define usb_device_pm_ops (*(struct dev_pm_ops *) NULL)
346#define ksuspend_usb_cleanup() do {} while (0)
347#define usb_device_pm_ops (*(struct dev_pm_ops *)0)
348 326
349#endif /* CONFIG_PM */ 327#endif /* CONFIG_PM */
350 328
@@ -472,9 +450,6 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
472 INIT_LIST_HEAD(&dev->filelist); 450 INIT_LIST_HEAD(&dev->filelist);
473 451
474#ifdef CONFIG_PM 452#ifdef CONFIG_PM
475 mutex_init(&dev->pm_mutex);
476 INIT_DELAYED_WORK(&dev->autosuspend, usb_autosuspend_work);
477 INIT_WORK(&dev->autoresume, usb_autoresume_work);
478 dev->autosuspend_delay = usb_autosuspend_delay * HZ; 453 dev->autosuspend_delay = usb_autosuspend_delay * HZ;
479 dev->connect_time = jiffies; 454 dev->connect_time = jiffies;
480 dev->active_duration = -jiffies; 455 dev->active_duration = -jiffies;
@@ -1117,9 +1092,6 @@ static int __init usb_init(void)
1117 if (retval) 1092 if (retval)
1118 goto out; 1093 goto out;
1119 1094
1120 retval = ksuspend_usb_init();
1121 if (retval)
1122 goto out;
1123 retval = bus_register(&usb_bus_type); 1095 retval = bus_register(&usb_bus_type);
1124 if (retval) 1096 if (retval)
1125 goto bus_register_failed; 1097 goto bus_register_failed;
@@ -1159,7 +1131,7 @@ major_init_failed:
1159bus_notifier_failed: 1131bus_notifier_failed:
1160 bus_unregister(&usb_bus_type); 1132 bus_unregister(&usb_bus_type);
1161bus_register_failed: 1133bus_register_failed:
1162 ksuspend_usb_cleanup(); 1134 usb_debugfs_cleanup();
1163out: 1135out:
1164 return retval; 1136 return retval;
1165} 1137}
@@ -1181,7 +1153,6 @@ static void __exit usb_exit(void)
1181 usb_hub_cleanup(); 1153 usb_hub_cleanup();
1182 bus_unregister_notifier(&usb_bus_type, &usb_bus_nb); 1154 bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
1183 bus_unregister(&usb_bus_type); 1155 bus_unregister(&usb_bus_type);
1184 ksuspend_usb_cleanup();
1185 usb_debugfs_cleanup(); 1156 usb_debugfs_cleanup();
1186} 1157}
1187 1158
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index 2b74a7f99c41..cd882203ad34 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -55,25 +55,8 @@ extern void usb_major_cleanup(void);
55extern int usb_suspend(struct device *dev, pm_message_t msg); 55extern int usb_suspend(struct device *dev, pm_message_t msg);
56extern int usb_resume(struct device *dev, pm_message_t msg); 56extern int usb_resume(struct device *dev, pm_message_t msg);
57 57
58extern void usb_autosuspend_work(struct work_struct *work);
59extern void usb_autoresume_work(struct work_struct *work);
60extern int usb_port_suspend(struct usb_device *dev, pm_message_t msg); 58extern int usb_port_suspend(struct usb_device *dev, pm_message_t msg);
61extern int usb_port_resume(struct usb_device *dev, pm_message_t msg); 59extern int usb_port_resume(struct usb_device *dev, pm_message_t msg);
62extern int usb_external_suspend_device(struct usb_device *udev,
63 pm_message_t msg);
64extern int usb_external_resume_device(struct usb_device *udev,
65 pm_message_t msg);
66extern int usb_remote_wakeup(struct usb_device *dev);
67
68static inline void usb_pm_lock(struct usb_device *udev)
69{
70 mutex_lock_nested(&udev->pm_mutex, udev->level);
71}
72
73static inline void usb_pm_unlock(struct usb_device *udev)
74{
75 mutex_unlock(&udev->pm_mutex);
76}
77 60
78#else 61#else
79 62
@@ -87,14 +70,6 @@ static inline int usb_port_resume(struct usb_device *udev, pm_message_t msg)
87 return 0; 70 return 0;
88} 71}
89 72
90static inline int usb_remote_wakeup(struct usb_device *udev)
91{
92 return 0;
93}
94
95static inline void usb_pm_lock(struct usb_device *udev) {}
96static inline void usb_pm_unlock(struct usb_device *udev) {}
97
98#endif 73#endif
99 74
100#ifdef CONFIG_USB_SUSPEND 75#ifdef CONFIG_USB_SUSPEND
@@ -102,6 +77,7 @@ static inline void usb_pm_unlock(struct usb_device *udev) {}
102extern void usb_autosuspend_device(struct usb_device *udev); 77extern void usb_autosuspend_device(struct usb_device *udev);
103extern void usb_try_autosuspend_device(struct usb_device *udev); 78extern void usb_try_autosuspend_device(struct usb_device *udev);
104extern int usb_autoresume_device(struct usb_device *udev); 79extern int usb_autoresume_device(struct usb_device *udev);
80extern int usb_remote_wakeup(struct usb_device *dev);
105 81
106#else 82#else
107 83
@@ -112,9 +88,13 @@ static inline int usb_autoresume_device(struct usb_device *udev)
112 return 0; 88 return 0;
113} 89}
114 90
91static inline int usb_remote_wakeup(struct usb_device *udev)
92{
93 return 0;
94}
95
115#endif 96#endif
116 97
117extern struct workqueue_struct *ksuspend_usb_wq;
118extern struct bus_type usb_bus_type; 98extern struct bus_type usb_bus_type;
119extern struct device_type usb_device_type; 99extern struct device_type usb_device_type;
120extern struct device_type usb_if_device_type; 100extern struct device_type usb_if_device_type;
@@ -144,23 +124,6 @@ static inline int is_usb_device_driver(struct device_driver *drv)
144 for_devices; 124 for_devices;
145} 125}
146 126
147/* Interfaces and their "power state" are owned by usbcore */
148
149static inline void mark_active(struct usb_interface *f)
150{
151 f->is_active = 1;
152}
153
154static inline void mark_quiesced(struct usb_interface *f)
155{
156 f->is_active = 0;
157}
158
159static inline int is_active(const struct usb_interface *f)
160{
161 return f->is_active;
162}
163
164 127
165/* for labeling diagnostics */ 128/* for labeling diagnostics */
166extern const char *usbcore_name; 129extern const char *usbcore_name;
diff --git a/drivers/usb/misc/usbtest.c b/drivers/usb/misc/usbtest.c
index 3dab0c0b196f..707a87da77f8 100644
--- a/drivers/usb/misc/usbtest.c
+++ b/drivers/usb/misc/usbtest.c
@@ -1580,10 +1580,6 @@ usbtest_ioctl (struct usb_interface *intf, unsigned int code, void *buf)
1580 return -ERESTARTSYS; 1580 return -ERESTARTSYS;
1581 1581
1582 /* FIXME: What if a system sleep starts while a test is running? */ 1582 /* FIXME: What if a system sleep starts while a test is running? */
1583 if (!intf->is_active) {
1584 mutex_unlock(&dev->lock);
1585 return -EHOSTUNREACH;
1586 }
1587 1583
1588 /* some devices, like ez-usb default devices, need a non-default 1584 /* some devices, like ez-usb default devices, need a non-default
1589 * altsetting to have any active endpoints. some tests change 1585 * altsetting to have any active endpoints. some tests change
diff --git a/include/linux/usb.h b/include/linux/usb.h
index e6419ac89ea2..ad50fc8a7ad3 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -122,7 +122,6 @@ enum usb_interface_condition {
122 * number from the USB core by calling usb_register_dev(). 122 * number from the USB core by calling usb_register_dev().
123 * @condition: binding state of the interface: not bound, binding 123 * @condition: binding state of the interface: not bound, binding
124 * (in probe()), bound to a driver, or unbinding (in disconnect()) 124 * (in probe()), bound to a driver, or unbinding (in disconnect())
125 * @is_active: flag set when the interface is bound and not suspended.
126 * @sysfs_files_created: sysfs attributes exist 125 * @sysfs_files_created: sysfs attributes exist
127 * @ep_devs_created: endpoint child pseudo-devices exist 126 * @ep_devs_created: endpoint child pseudo-devices exist
128 * @unregistering: flag set when the interface is being unregistered 127 * @unregistering: flag set when the interface is being unregistered
@@ -135,8 +134,7 @@ enum usb_interface_condition {
135 * @dev: driver model's view of this device 134 * @dev: driver model's view of this device
136 * @usb_dev: if an interface is bound to the USB major, this will point 135 * @usb_dev: if an interface is bound to the USB major, this will point
137 * to the sysfs representation for that device. 136 * to the sysfs representation for that device.
138 * @pm_usage_cnt: PM usage counter for this interface; autosuspend is not 137 * @pm_usage_cnt: PM usage counter for this interface
139 * allowed unless the counter is 0.
140 * @reset_ws: Used for scheduling resets from atomic context. 138 * @reset_ws: Used for scheduling resets from atomic context.
141 * @reset_running: set to 1 if the interface is currently running a 139 * @reset_running: set to 1 if the interface is currently running a
142 * queued reset so that usb_cancel_queued_reset() doesn't try to 140 * queued reset so that usb_cancel_queued_reset() doesn't try to
@@ -184,7 +182,6 @@ struct usb_interface {
184 int minor; /* minor number this interface is 182 int minor; /* minor number this interface is
185 * bound to */ 183 * bound to */
186 enum usb_interface_condition condition; /* state of binding */ 184 enum usb_interface_condition condition; /* state of binding */
187 unsigned is_active:1; /* the interface is not suspended */
188 unsigned sysfs_files_created:1; /* the sysfs attributes exist */ 185 unsigned sysfs_files_created:1; /* the sysfs attributes exist */
189 unsigned ep_devs_created:1; /* endpoint "devices" exist */ 186 unsigned ep_devs_created:1; /* endpoint "devices" exist */
190 unsigned unregistering:1; /* unregistration is in progress */ 187 unsigned unregistering:1; /* unregistration is in progress */
@@ -401,7 +398,6 @@ struct usb_tt;
401 * @portnum: parent port number (origin 1) 398 * @portnum: parent port number (origin 1)
402 * @level: number of USB hub ancestors 399 * @level: number of USB hub ancestors
403 * @can_submit: URBs may be submitted 400 * @can_submit: URBs may be submitted
404 * @discon_suspended: disconnected while suspended
405 * @persist_enabled: USB_PERSIST enabled for this device 401 * @persist_enabled: USB_PERSIST enabled for this device
406 * @have_langid: whether string_langid is valid 402 * @have_langid: whether string_langid is valid
407 * @authorized: policy has said we can use it; 403 * @authorized: policy has said we can use it;
@@ -421,20 +417,15 @@ struct usb_tt;
421 * @usbfs_dentry: usbfs dentry entry for the device 417 * @usbfs_dentry: usbfs dentry entry for the device
422 * @maxchild: number of ports if hub 418 * @maxchild: number of ports if hub
423 * @children: child devices - USB devices that are attached to this hub 419 * @children: child devices - USB devices that are attached to this hub
424 * @pm_usage_cnt: usage counter for autosuspend
425 * @quirks: quirks of the whole device 420 * @quirks: quirks of the whole device
426 * @urbnum: number of URBs submitted for the whole device 421 * @urbnum: number of URBs submitted for the whole device
427 * @active_duration: total time device is not suspended 422 * @active_duration: total time device is not suspended
428 * @autosuspend: for delayed autosuspends
429 * @autoresume: for autoresumes requested while in_interrupt
430 * @pm_mutex: protects PM operations
431 * @last_busy: time of last use 423 * @last_busy: time of last use
432 * @autosuspend_delay: in jiffies 424 * @autosuspend_delay: in jiffies
433 * @connect_time: time device was first connected 425 * @connect_time: time device was first connected
434 * @do_remote_wakeup: remote wakeup should be enabled 426 * @do_remote_wakeup: remote wakeup should be enabled
435 * @reset_resume: needs reset instead of resume 427 * @reset_resume: needs reset instead of resume
436 * @autosuspend_disabled: autosuspend disabled by the user 428 * @autosuspend_disabled: autosuspend disabled by the user
437 * @skip_sys_resume: skip the next system resume
438 * @wusb_dev: if this is a Wireless USB device, link to the WUSB 429 * @wusb_dev: if this is a Wireless USB device, link to the WUSB
439 * specific data for the device. 430 * specific data for the device.
440 * @slot_id: Slot ID assigned by xHCI 431 * @slot_id: Slot ID assigned by xHCI
@@ -475,7 +466,6 @@ struct usb_device {
475 u8 level; 466 u8 level;
476 467
477 unsigned can_submit:1; 468 unsigned can_submit:1;
478 unsigned discon_suspended:1;
479 unsigned persist_enabled:1; 469 unsigned persist_enabled:1;
480 unsigned have_langid:1; 470 unsigned have_langid:1;
481 unsigned authorized:1; 471 unsigned authorized:1;
@@ -499,17 +489,12 @@ struct usb_device {
499 int maxchild; 489 int maxchild;
500 struct usb_device *children[USB_MAXCHILDREN]; 490 struct usb_device *children[USB_MAXCHILDREN];
501 491
502 int pm_usage_cnt;
503 u32 quirks; 492 u32 quirks;
504 atomic_t urbnum; 493 atomic_t urbnum;
505 494
506 unsigned long active_duration; 495 unsigned long active_duration;
507 496
508#ifdef CONFIG_PM 497#ifdef CONFIG_PM
509 struct delayed_work autosuspend;
510 struct work_struct autoresume;
511 struct mutex pm_mutex;
512
513 unsigned long last_busy; 498 unsigned long last_busy;
514 int autosuspend_delay; 499 int autosuspend_delay;
515 unsigned long connect_time; 500 unsigned long connect_time;
@@ -517,7 +502,6 @@ struct usb_device {
517 unsigned do_remote_wakeup:1; 502 unsigned do_remote_wakeup:1;
518 unsigned reset_resume:1; 503 unsigned reset_resume:1;
519 unsigned autosuspend_disabled:1; 504 unsigned autosuspend_disabled:1;
520 unsigned skip_sys_resume:1;
521#endif 505#endif
522 struct wusb_dev *wusb_dev; 506 struct wusb_dev *wusb_dev;
523 int slot_id; 507 int slot_id;
@@ -549,17 +533,8 @@ extern int usb_autopm_get_interface(struct usb_interface *intf);
549extern void usb_autopm_put_interface(struct usb_interface *intf); 533extern void usb_autopm_put_interface(struct usb_interface *intf);
550extern int usb_autopm_get_interface_async(struct usb_interface *intf); 534extern int usb_autopm_get_interface_async(struct usb_interface *intf);
551extern void usb_autopm_put_interface_async(struct usb_interface *intf); 535extern void usb_autopm_put_interface_async(struct usb_interface *intf);
552 536extern void usb_autopm_get_interface_no_resume(struct usb_interface *intf);
553static inline void usb_autopm_get_interface_no_resume( 537extern void usb_autopm_put_interface_no_suspend(struct usb_interface *intf);
554 struct usb_interface *intf)
555{
556 atomic_inc(&intf->pm_usage_cnt);
557}
558static inline void usb_autopm_put_interface_no_suspend(
559 struct usb_interface *intf)
560{
561 atomic_dec(&intf->pm_usage_cnt);
562}
563 538
564static inline void usb_mark_last_busy(struct usb_device *udev) 539static inline void usb_mark_last_busy(struct usb_device *udev)
565{ 540{