aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/usb
diff options
context:
space:
mode:
Diffstat (limited to 'Documentation/usb')
-rw-r--r--Documentation/usb/error-codes.txt6
-rw-r--r--Documentation/usb/power-management.txt301
2 files changed, 125 insertions, 182 deletions
diff --git a/Documentation/usb/error-codes.txt b/Documentation/usb/error-codes.txt
index 9cf83e8c27b8..d83703ea74b2 100644
--- a/Documentation/usb/error-codes.txt
+++ b/Documentation/usb/error-codes.txt
@@ -41,8 +41,8 @@ USB-specific:
41 41
42-EFBIG Host controller driver can't schedule that many ISO frames. 42-EFBIG Host controller driver can't schedule that many ISO frames.
43 43
44-EPIPE Specified endpoint is stalled. For non-control endpoints, 44-EPIPE The pipe type specified in the URB doesn't match the
45 reset this status with usb_clear_halt(). 45 endpoint's actual type.
46 46
47-EMSGSIZE (a) endpoint maxpacket size is zero; it is not usable 47-EMSGSIZE (a) endpoint maxpacket size is zero; it is not usable
48 in the current interface altsetting. 48 in the current interface altsetting.
@@ -60,6 +60,8 @@ USB-specific:
60 60
61-EHOSTUNREACH URB was rejected because the device is suspended. 61-EHOSTUNREACH URB was rejected because the device is suspended.
62 62
63-ENOEXEC A control URB doesn't contain a Setup packet.
64
63 65
64************************************************************************** 66**************************************************************************
65* Error codes returned by in urb->status * 67* Error codes returned by in urb->status *
diff --git a/Documentation/usb/power-management.txt b/Documentation/usb/power-management.txt
index ad642615ad4c..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 October 5, 2007 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?
@@ -71,12 +71,10 @@ being accessed through sysfs, then it definitely is idle.
71 Forms of dynamic PM 71 Forms of dynamic PM
72 ------------------- 72 -------------------
73 73
74Dynamic suspends can occur in two ways: manual and automatic. 74Dynamic suspends occur when the kernel decides to suspend an idle
75"Manual" means that the user has told the kernel to suspend a device, 75device. This is called "autosuspend" for short. In general, a device
76whereas "automatic" means that the kernel has decided all by itself to 76won't be autosuspended unless it has been idle for some minimum period
77suspend a device. Automatic suspend is called "autosuspend" for 77of time, the so-called idle-delay time.
78short. In general, a device won't be autosuspended unless it has been
79idle for some minimum period of time, the so-called idle-delay time.
80 78
81Of course, nothing the kernel does on its own initiative should 79Of course, nothing the kernel does on its own initiative should
82prevent the computer or its devices from working properly. If a 80prevent the computer or its devices from working properly. If a
@@ -96,10 +94,11 @@ idle.
96We can categorize power management events in two broad classes: 94We can categorize power management events in two broad classes:
97external and internal. External events are those triggered by some 95external and internal. External events are those triggered by some
98agent outside the USB stack: system suspend/resume (triggered by 96agent outside the USB stack: system suspend/resume (triggered by
99userspace), manual dynamic suspend/resume (also triggered by 97userspace), manual dynamic resume (also triggered by userspace), and
100userspace), and remote wakeup (triggered by the device). Internal 98remote wakeup (triggered by the device). Internal events are those
101events are those triggered within the USB stack: autosuspend and 99triggered within the USB stack: autosuspend and autoresume. Note that
102autoresume. 100all dynamic suspend events are internal; external agents are not
101allowed to issue dynamic suspends.
103 102
104 103
105 The user interface for dynamic PM 104 The user interface for dynamic PM
@@ -123,9 +122,9 @@ relevant attribute files are: wakeup, level, and autosuspend.
123 122
124 power/level 123 power/level
125 124
126 This file contains one of three words: "on", "auto", 125 This file contains one of two words: "on" or "auto".
127 or "suspend". You can write those words to the file 126 You can write those words to the file to change the
128 to change the device's setting. 127 device's setting.
129 128
130 "on" means that the device should be resumed and 129 "on" means that the device should be resumed and
131 autosuspend is not allowed. (Of course, system 130 autosuspend is not allowed. (Of course, system
@@ -134,10 +133,10 @@ relevant attribute files are: wakeup, level, and autosuspend.
134 "auto" is the normal state in which the kernel is 133 "auto" is the normal state in which the kernel is
135 allowed to autosuspend and autoresume the device. 134 allowed to autosuspend and autoresume the device.
136 135
137 "suspend" means that the device should remain 136 (In kernels up to 2.6.32, you could also specify
138 suspended, and autoresume is not allowed. (But remote 137 "suspend", meaning that the device should remain
139 wakeup may still be allowed, since it is controlled 138 suspended and autoresume was not allowed. This
140 separately by the power/wakeup attribute.) 139 setting is no longer supported.)
141 140
142 power/autosuspend 141 power/autosuspend
143 142
@@ -145,9 +144,9 @@ relevant attribute files are: wakeup, level, and autosuspend.
145 number of seconds the device should remain idle before 144 number of seconds the device should remain idle before
146 the kernel will autosuspend it (the idle-delay time). 145 the kernel will autosuspend it (the idle-delay time).
147 The default is 2. 0 means to autosuspend as soon as 146 The default is 2. 0 means to autosuspend as soon as
148 the device becomes idle, and -1 means never to 147 the device becomes idle, and negative values mean
149 autosuspend. You can write a number to the file to 148 never to autosuspend. You can write a number to the
150 change the autosuspend idle-delay time. 149 file to change the autosuspend idle-delay time.
151 150
152Writing "-1" to power/autosuspend and writing "on" to power/level do 151Writing "-1" to power/autosuspend and writing "on" to power/level do
153essentially the same thing -- they both prevent the device from being 152essentially the same thing -- they both prevent the device from being
@@ -230,6 +229,11 @@ necessary operations by hand or add them to a udev script. You can
230also change the idle-delay time; 2 seconds is not the best choice for 229also change the idle-delay time; 2 seconds is not the best choice for
231every device. 230every device.
232 231
232If a driver knows that its device has proper suspend/resume support,
233it can enable autosuspend all by itself. For example, the video
234driver for a laptop's webcam might do this, since these devices are
235rarely used and so should normally be autosuspended.
236
233Sometimes it turns out that even when a device does work okay with 237Sometimes it turns out that even when a device does work okay with
234autosuspend there are still problems. For example, there are 238autosuspend there are still problems. For example, there are
235experimental patches adding autosuspend support to the usbhid driver, 239experimental patches adding autosuspend support to the usbhid driver,
@@ -313,82 +317,90 @@ three of the methods listed above. In addition, a driver indicates
313that it supports autosuspend by setting the .supports_autosuspend flag 317that it supports autosuspend by setting the .supports_autosuspend flag
314in its usb_driver structure. It is then responsible for informing the 318in its usb_driver structure. It is then responsible for informing the
315USB core whenever one of its interfaces becomes busy or idle. The 319USB core whenever one of its interfaces becomes busy or idle. The
316driver does so by calling these five functions: 320driver does so by calling these six functions:
317 321
318 int usb_autopm_get_interface(struct usb_interface *intf); 322 int usb_autopm_get_interface(struct usb_interface *intf);
319 void usb_autopm_put_interface(struct usb_interface *intf); 323 void usb_autopm_put_interface(struct usb_interface *intf);
320 int usb_autopm_set_interface(struct usb_interface *intf);
321 int usb_autopm_get_interface_async(struct usb_interface *intf); 324 int usb_autopm_get_interface_async(struct usb_interface *intf);
322 void usb_autopm_put_interface_async(struct usb_interface *intf); 325 void usb_autopm_put_interface_async(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);
323 328
324The functions work by maintaining a counter in the usb_interface 329The functions work by maintaining a usage counter in the
325structure. When intf->pm_usage_count is > 0 then the interface is 330usb_interface's embedded device structure. When the counter is > 0
326deemed 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
327device. When intf->pm_usage_count is <= 0 then the interface is 332autosuspend the interface's device. When the usage counter is = 0
328considered 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.
329 335
330(There is a similar pm_usage_count field in struct usb_device, 336(There is a similar usage counter field in struct usb_device,
331associated with the device itself rather than any of its interfaces. 337associated with the device itself rather than any of its interfaces.
332This field is used only by the USB core.) 338This counter is used only by the USB core.)
333 339
334The driver owns intf->pm_usage_count; it can modify the value however 340Drivers need not be concerned about balancing changes to the usage
335and whenever it likes. A nice aspect of the non-async usb_autopm_* 341counter; the USB core will undo any remaining "get"s when a driver
336routines is that the changes they make are protected by the usb_device 342is unbound from its interface. As a corollary, drivers must not call
337structure's PM mutex (udev->pm_mutex); however drivers may change 343any of the usb_autopm_* functions after their diconnect() routine has
338pm_usage_count without holding the mutex. Drivers using the async 344returned.
339routines are responsible for their own synchronization and mutual
340exclusion.
341 345
342 usb_autopm_get_interface() increments pm_usage_count and 346Drivers using the async routines are responsible for their own
343 attempts an autoresume if the new value is > 0 and the 347synchronization and mutual exclusion.
344 device is suspended.
345 348
346 usb_autopm_put_interface() decrements pm_usage_count and 349 usb_autopm_get_interface() increments the usage counter and
347 attempts an autosuspend if the new value is <= 0 and the 350 does an autoresume if the device is suspended. If the
348 device isn't suspended. 351 autoresume fails, the counter is decremented back.
349 352
350 usb_autopm_set_interface() leaves pm_usage_count alone. 353 usb_autopm_put_interface() decrements the usage counter and
351 It attempts an autoresume if the value is > 0 and the device 354 attempts an autosuspend if the new value is = 0.
352 is suspended, and it attempts an autosuspend if the value is
353 <= 0 and the device isn't suspended.
354 355
355 usb_autopm_get_interface_async() and 356 usb_autopm_get_interface_async() and
356 usb_autopm_put_interface_async() do almost the same things as 357 usb_autopm_put_interface_async() do almost the same things as
357 their non-async counterparts. The differences are: they do 358 their non-async counterparts. The big difference is that they
358 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
359 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,
360 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
361 device will not generally not yet be in the desired state. 362 device will generally not yet be in the desired state.
362
363There also are a couple of utility routines drivers can use:
364
365 usb_autopm_enable() sets pm_usage_cnt to 0 and then calls
366 usb_autopm_set_interface(), which will attempt an autosuspend.
367 363
368 usb_autopm_disable() sets pm_usage_cnt to 1 and then calls 364 usb_autopm_get_interface_no_resume() and
369 usb_autopm_set_interface(), which will attempt an autoresume. 365 usb_autopm_put_interface_no_suspend() merely increment or
366 decrement the usage counter; they do not attempt to carry out
367 an autoresume or an autosuspend. Hence they can be called in
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. This will happen if power/level is 383Autoresume attempts also can fail, although failure would mean that
385set to "suspend" or if the device doesn't manage to resume properly. 384the device is no longer present or operating properly. Unlike
386Unlike autosuspend, 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
390 ----------------------------------- 389 -----------------------------------
391 390
391Drivers can enable autosuspend for their devices by calling
392
393 usb_enable_autosuspend(struct usb_device *udev);
394
395in their probe() routine, if they know that the device is capable of
396suspending and resuming correctly. This is exactly equivalent to
397writing "auto" to the device's power/level attribute. Likewise,
398drivers can disable autosuspend by calling
399
400 usb_disable_autosuspend(struct usb_device *udev);
401
402This is exactly the same as writing "on" to the power/level attribute.
403
392Sometimes a driver needs to make sure that remote wakeup is enabled 404Sometimes a driver needs to make sure that remote wakeup is enabled
393during autosuspend. For example, there's not much point 405during autosuspend. For example, there's not much point
394autosuspending a keyboard if the user can't cause the keyboard to do a 406autosuspending a keyboard if the user can't cause the keyboard to do a
@@ -400,101 +412,51 @@ though, setting this flag won't cause the kernel to autoresume it.
400Normally 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
401time the device is guaranteed not to be autosuspended.) 413time the device is guaranteed not to be autosuspended.)
402 414
403The usb_autopm_* routines have to run in a sleepable process context; 415If a driver does its I/O asynchronously in interrupt context, it
404they must not be called from an interrupt handler or while holding a 416should call usb_autopm_get_interface_async() before starting output and
405spinlock. In fact, the entire autosuspend mechanism is not well geared 417usb_autopm_put_interface_async() when the output queue drains. When
406toward interrupt-driven operation. However there is one thing a 418it receives an input event, it should call
407driver can do in an interrupt handler:
408 419
409 usb_mark_last_busy(struct usb_device *udev); 420 usb_mark_last_busy(struct usb_device *udev);
410 421
411This 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.
412field used for idle-delay calculations; updating it will cause any 423udev->last_busy is the field used for idle-delay calculations;
413pending autosuspend to be moved back. The usb_autopm_* routines will 424updating it will cause any pending autosuspend to be moved back. Most
414also set the last_busy field to the current time. 425of the usb_autopm_* routines will also set the last_busy field to the
415 426current time.
416Calling urb_mark_last_busy() from within an URB completion handler is 427
417subject to races: The kernel may have just finished deciding the 428Asynchronous operation is always subject to races. For example, a
418device has been idle for long enough but not yet gotten around to 429driver may call one of the usb_autopm_*_interface_async() routines at
419calling the driver's suspend method. The driver would have to be 430a time when the core has just finished deciding the device has been
420responsible for synchronizing its suspend method with its URB 431idle for long enough but not yet gotten around to calling the driver's
421completion handler and causing the autosuspend to fail with -EBUSY if 432suspend method. The suspend method must be responsible for
422an 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.
423 436
424External suspend calls should never be allowed to fail in this way, 437External suspend calls should never be allowed to fail in this way,
425only autosuspend calls. The driver can tell them apart by checking 438only autosuspend calls. The driver can tell them apart by checking
426udev->auto_pm; this flag will be set to 1 for internal PM events 439the PM_EVENT_AUTO bit in the message.event argument to the suspend
427(autosuspend or autoresume) and 0 for external PM events. 440method; this bit will be set for internal PM events (autosuspend) and
428 441clear for external PM events.
429Many of the ingredients in the autosuspend framework are oriented
430towards interfaces: The usb_interface structure contains the
431pm_usage_cnt field, and the usb_autopm_* routines take an interface
432pointer as their argument. But somewhat confusingly, a few of the
433pieces (usb_mark_last_busy() and udev->auto_pm) use the usb_device
434structure instead. Drivers need to keep this straight; they can call
435interface_to_usbdev() to find the device structure for a given
436interface.
437
438
439 Locking requirements
440 --------------------
441
442All three suspend/resume methods are always called while holding the
443usb_device's PM mutex. For external events -- but not necessarily for
444autosuspend or autoresume -- the device semaphore (udev->dev.sem) will
445also be held. This implies that external suspend/resume events are
446mutually exclusive with calls to probe, disconnect, pre_reset, and
447post_reset; the USB core guarantees that this is true of internal
448suspend/resume events as well.
449
450If a driver wants to block all suspend/resume calls during some
451critical section, it can simply acquire udev->pm_mutex. Note that
452calls to resume may be triggered indirectly. Block IO due to memory
453allocations can make the vm subsystem resume a device. Thus while
454holding this lock you must not allocate memory with GFP_KERNEL or
455GFP_NOFS.
456
457Alternatively, if the critical section might call some of the
458usb_autopm_* routines, the driver can avoid deadlock by doing:
459
460 down(&udev->dev.sem);
461 rc = usb_autopm_get_interface(intf);
462
463and at the end of the critical section:
464
465 if (!rc)
466 usb_autopm_put_interface(intf);
467 up(&udev->dev.sem);
468 442
469Holding the device semaphore will block all external PM calls, and the
470usb_autopm_get_interface() will prevent any internal PM calls, even if
471it fails. (Exercise: Why?)
472 443
473The rules for locking order are: 444 Mutual exclusion
445 ----------------
474 446
475 Never acquire any device semaphore while holding any PM mutex. 447For external events -- but not necessarily for autosuspend or
448autoresume -- the device semaphore (udev->dev.sem) will be held when a
449suspend or resume method is called. This implies that external
450suspend/resume events are mutually exclusive with calls to probe,
451disconnect, pre_reset, and post_reset; the USB core guarantees that
452this is true of autosuspend/autoresume events as well.
476 453
477 Never acquire udev->pm_mutex while holding the PM mutex for 454If a driver wants to block all suspend/resume calls during some
478 a device that isn't a descendant of udev. 455critical section, the best way is to lock the device and call
479 456usb_autopm_get_interface() (and do the reverse at the end of the
480In other words, PM mutexes should only be acquired going up the device 457critical section). Holding the device semaphore will block all
481tree, and they should be acquired only after locking all the device 458external PM calls, and the usb_autopm_get_interface() will prevent any
482semaphores you need to hold. These rules don't matter to drivers very 459internal PM calls, even if it fails. (Exercise: Why?)
483much; they usually affect just the USB core.
484
485Still, drivers do need to be careful. For example, many drivers use a
486private mutex to synchronize their normal I/O activities with their
487disconnect method. Now if the driver supports autosuspend then it
488must call usb_autopm_put_interface() from somewhere -- maybe from its
489close method. It should make the call while holding the private mutex,
490since a driver shouldn't call any of the usb_autopm_* functions for an
491interface from which it has been unbound.
492
493But the usb_autpm_* routines always acquire the device's PM mutex, and
494consequently the locking order has to be: private mutex first, PM
495mutex second. Since the suspend method is always called with the PM
496mutex held, it mustn't try to acquire the private mutex. It has to
497synchronize with the driver's I/O activities in some other way.
498 460
499 461
500 Interaction between dynamic PM and system PM 462 Interaction between dynamic PM and system PM
@@ -503,22 +465,11 @@ synchronize with the driver's I/O activities in some other way.
503Dynamic power management and system power management can interact in 465Dynamic power management and system power management can interact in
504a couple of ways. 466a couple of ways.
505 467
506Firstly, a device may already be manually suspended or autosuspended 468Firstly, a device may already be autosuspended when a system suspend
507when a system suspend occurs. Since system suspends are supposed to 469occurs. Since system suspends are supposed to be as transparent as
508be as transparent as possible, the device should remain suspended 470possible, the device should remain suspended following the system
509following the system resume. The 2.6.23 kernel obeys this principle 471resume. But this theory may not work out well in practice; over time
510for manually suspended devices but not for autosuspended devices; they 472the kernel's behavior in this regard has changed.
511do get resumed when the system wakes up. (Presumably they will be
512autosuspended again after their idle-delay time expires.) In later
513kernels this behavior will be fixed.
514
515(There is an exception. If a device would undergo a reset-resume
516instead of a normal resume, and the device is enabled for remote
517wakeup, then the reset-resume takes place even if the device was
518already suspended when the system suspend began. The justification is
519that a reset-resume is a kind of remote-wakeup event. Or to put it
520another way, a device which needs a reset won't be able to generate
521normal remote-wakeup signals, so it ought to be resumed immediately.)
522 473
523Secondly, a dynamic power-management event may occur as a system 474Secondly, a dynamic power-management event may occur as a system
524suspend is underway. The window for this is short, since system 475suspend is underway. The window for this is short, since system
@@ -530,13 +481,3 @@ succeed, it may still remain active and thus cause the system to
530resume as soon as the system suspend is complete. Or the remote 481resume as soon as the system suspend is complete. Or the remote
531wakeup may fail and get lost. Which outcome occurs depends on timing 482wakeup may fail and get lost. Which outcome occurs depends on timing
532and on the hardware and firmware design. 483and on the hardware and firmware design.
533
534More interestingly, a device might undergo a manual resume or
535autoresume during system suspend. With current kernels this shouldn't
536happen, because manual resumes must be initiated by userspace and
537autoresumes happen in response to I/O requests, but all user processes
538and I/O should be quiescent during a system suspend -- thanks to the
539freezer. However there are plans to do away with the freezer, which
540would mean these things would become possible. If and when this comes
541about, the USB core will carefully arrange matters so that either type
542of resume will block until the entire system has resumed.