⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 power-management.txt

📁 linux 内核源代码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
	The suspend method is called to warn the driver that the	device is going to be suspended.  If the driver returns a	negative error code, the suspend will be aborted.  Normally	the driver will return 0, in which case it must cancel all	outstanding URBs (usb_kill_urb()) and not submit any more.	The resume method is called to tell the driver that the	device has been resumed and the driver can return to normal	operation.  URBs may once more be submitted.	The reset_resume method is called to tell the driver that	the device has been resumed and it also has been reset.	The driver should redo any necessary device initialization,	since the device has probably lost most or all of its state	(although the interfaces will be in the same altsettings as	before the suspend).If the device is disconnected or powered down while it is suspended,the disconnect method will be called instead of the resume orreset_resume method.  This is also quite likely to happen whenwaking up from hibernation, as many systems do not maintain suspendcurrent to the USB host controllers during hibernation.  (It'spossible to work around the hibernation-forces-disconnect problem byusing the USB Persist facility.)The reset_resume method is used by the USB Persist facility (seeDocumentation/usb/persist.txt) and it can also be used under certaincircumstances when CONFIG_USB_PERSIST is not enabled.  Currently, if adevice is reset during a resume and the driver does not have areset_resume method, the driver won't receive any notification aboutthe resume.  Later kernels will call the driver's disconnect method;2.6.23 doesn't do this.USB drivers are bound to interfaces, so their suspend and resumemethods get called when the interfaces are suspended or resumed.  Inprinciple one might want to suspend some interfaces on a device (i.e.,force the drivers for those interface to stop all activity) withoutsuspending the other interfaces.  The USB core doesn't allow this; allinterfaces are suspended when the device itself is suspended and allinterfaces are resumed when the device is resumed.  It isn't possibleto suspend or resume some but not all of a device's interfaces.  Theclosest you can come is to unbind the interfaces' drivers.	The driver interface for autosuspend and autoresume	---------------------------------------------------To support autosuspend and autoresume, a driver should implement allthree of the methods listed above.  In addition, a driver indicatesthat it supports autosuspend by setting the .supports_autosuspend flagin its usb_driver structure.  It is then responsible for informing theUSB core whenever one of its interfaces becomes busy or idle.  Thedriver does so by calling these three functions:	int  usb_autopm_get_interface(struct usb_interface *intf);	void usb_autopm_put_interface(struct usb_interface *intf);	int  usb_autopm_set_interface(struct usb_interface *intf);The functions work by maintaining a counter in the usb_interfacestructure.  When intf->pm_usage_count is > 0 then the interface isdeemed to be busy, and the kernel will not autosuspend the interface'sdevice.  When intf->pm_usage_count is <= 0 then the interface isconsidered to be idle, and the kernel may autosuspend the device.(There is a similar pm_usage_count field in struct usb_device,associated with the device itself rather than any of its interfaces.This field is used only by the USB core.)The driver owns intf->pm_usage_count; it can modify the value howeverand whenever it likes.  A nice aspect of the usb_autopm_* routines isthat the changes they make are protected by the usb_device structure'sPM mutex (udev->pm_mutex); however drivers may change pm_usage_countwithout holding the mutex.	usb_autopm_get_interface() increments pm_usage_count and	attempts an autoresume if the new value is > 0 and the	device is suspended.	usb_autopm_put_interface() decrements pm_usage_count and	attempts an autosuspend if the new value is <= 0 and the	device isn't suspended.	usb_autopm_set_interface() leaves pm_usage_count alone.	It attempts an autoresume if the value is > 0 and the device	is suspended, and it attempts an autosuspend if the value is	<= 0 and the device isn't suspended.There also are a couple of utility routines drivers can use:	usb_autopm_enable() sets pm_usage_cnt to 1 and then calls	usb_autopm_set_interface(), which will attempt an autoresume.	usb_autopm_disable() sets pm_usage_cnt to 0 and then calls	usb_autopm_set_interface(), which will attempt an autosuspend.The conventional usage pattern is that a driver callsusb_autopm_get_interface() in its open routine andusb_autopm_put_interface() in its close or release routine.  Butother patterns are possible.The autosuspend attempts mentioned above will often fail for onereason or another.  For example, the power/level attribute might beset to "on", or another interface in the same device might not beidle.  This is perfectly normal.  If the reason for failure was thatthe device hasn't been idle for long enough, a delayed workqueueroutine is automatically set up to carry out the operation when theautosuspend idle-delay has expired.Autoresume attempts also can fail.  This will happen if power/level isset to "suspend" or if the device doesn't manage to resume properly.Unlike autosuspend, there's no delay for an autoresume.	Other parts of the driver interface	-----------------------------------Sometimes a driver needs to make sure that remote wakeup is enabledduring autosuspend.  For example, there's not much pointautosuspending a keyboard if the user can't cause the keyboard to do aremote wakeup by typing on it.  If the driver setsintf->needs_remote_wakeup to 1, the kernel won't autosuspend thedevice if remote wakeup isn't available or has been disabled throughthe power/wakeup attribute.  (If the device is already autosuspended,though, setting this flag won't cause the kernel to autoresume it.Normally a driver would set this flag in its probe method, at whichtime the device is guaranteed not to be autosuspended.)The usb_autopm_* routines have to run in a sleepable process context;they must not be called from an interrupt handler or while holding aspinlock.  In fact, the entire autosuspend mechanism is not well gearedtoward interrupt-driven operation.  However there is one thing adriver can do in an interrupt handler:	usb_mark_last_busy(struct usb_device *udev);This sets udev->last_busy to the current time.  udev->last_busy is thefield used for idle-delay calculations; updating it will cause anypending autosuspend to be moved back.  The usb_autopm_* routines willalso set the last_busy field to the current time.Calling urb_mark_last_busy() from within an URB completion handler issubject to races: The kernel may have just finished deciding thedevice has been idle for long enough but not yet gotten around tocalling the driver's suspend method.  The driver would have to beresponsible for synchronizing its suspend method with its URBcompletion handler and causing the autosuspend to fail with -EBUSY ifan URB had completed too recently.External suspend calls should never be allowed to fail in this way,only autosuspend calls.  The driver can tell them apart by checkingudev->auto_pm; this flag will be set to 1 for internal PM events(autosuspend or autoresume) and 0 for external PM events.Many of the ingredients in the autosuspend framework are orientedtowards interfaces: The usb_interface structure contains thepm_usage_cnt field, and the usb_autopm_* routines take an interfacepointer as their argument.  But somewhat confusingly, a few of thepieces (usb_mark_last_busy() and udev->auto_pm) use the usb_devicestructure instead.  Drivers need to keep this straight; they can callinterface_to_usbdev() to find the device structure for a giveninterface.	Locking requirements	--------------------All three suspend/resume methods are always called while holding theusb_device's PM mutex.  For external events -- but not necessarily forautosuspend or autoresume -- the device semaphore (udev->dev.sem) willalso be held.  This implies that external suspend/resume events aremutually exclusive with calls to probe, disconnect, pre_reset, andpost_reset; the USB core guarantees that this is true of internalsuspend/resume events as well.If a driver wants to block all suspend/resume calls during somecritical section, it can simply acquire udev->pm_mutex.Alternatively, if the critical section might call some of theusb_autopm_* routines, the driver can avoid deadlock by doing:	down(&udev->dev.sem);	rc = usb_autopm_get_interface(intf);and at the end of the critical section:	if (!rc)		usb_autopm_put_interface(intf);	up(&udev->dev.sem);Holding the device semaphore will block all external PM calls, and theusb_autopm_get_interface() will prevent any internal PM calls, even ifit fails.  (Exercise: Why?)The rules for locking order are:	Never acquire any device semaphore while holding any PM mutex.	Never acquire udev->pm_mutex while holding the PM mutex for	a device that isn't a descendant of udev.In other words, PM mutexes should only be acquired going up the devicetree, and they should be acquired only after locking all the devicesemaphores you need to hold.  These rules don't matter to drivers verymuch; they usually affect just the USB core.Still, drivers do need to be careful.  For example, many drivers use aprivate mutex to synchronize their normal I/O activities with theirdisconnect method.  Now if the driver supports autosuspend then itmust call usb_autopm_put_interface() from somewhere -- maybe from itsclose method.  It should make the call while holding the private mutex,since a driver shouldn't call any of the usb_autopm_* functions for aninterface from which it has been unbound.But the usb_autpm_* routines always acquire the device's PM mutex, andconsequently the locking order has to be: private mutex first, PMmutex second.  Since the suspend method is always called with the PMmutex held, it mustn't try to acquire the private mutex.  It has tosynchronize with the driver's I/O activities in some other way.	Interaction between dynamic PM and system PM	--------------------------------------------Dynamic power management and system power management can interact ina couple of ways.Firstly, a device may already be manually suspended or autosuspendedwhen a system suspend occurs.  Since system suspends are supposed tobe as transparent as possible, the device should remain suspendedfollowing the system resume.  The 2.6.23 kernel obeys this principlefor manually suspended devices but not for autosuspended devices; theydo get resumed when the system wakes up.  (Presumably they will beautosuspended again after their idle-delay time expires.)  In laterkernels this behavior will be fixed.(There is an exception.  If a device would undergo a reset-resumeinstead of a normal resume, and the device is enabled for remotewakeup, then the reset-resume takes place even if the device wasalready suspended when the system suspend began.  The justification isthat a reset-resume is a kind of remote-wakeup event.  Or to put itanother way, a device which needs a reset won't be able to generatenormal remote-wakeup signals, so it ought to be resumed immediately.)Secondly, a dynamic power-management event may occur as a systemsuspend is underway.  The window for this is short, since systemsuspends don't take long (a few seconds usually), but it can happen.For example, a suspended device may send a remote-wakeup signal whilethe system is suspending.  The remote wakeup may succeed, which wouldcause the system suspend to abort.  If the remote wakeup doesn'tsucceed, it may still remain active and thus cause the system toresume as soon as the system suspend is complete.  Or the remotewakeup may fail and get lost.  Which outcome occurs depends on timingand on the hardware and firmware design.More interestingly, a device might undergo a manual resume orautoresume during system suspend.  With current kernels this shouldn'thappen, because manual resumes must be initiated by userspace andautoresumes happen in response to I/O requests, but all user processesand I/O should be quiescent during a system suspend -- thanks to thefreezer.  However there are plans to do away with the freezer, whichwould mean these things would become possible.  If and when this comesabout, the USB core will carefully arrange matters so that either typeof resume will block until the entire system has resumed.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -