Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1 | /* |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 2 | * drivers/base/power/runtime.c - Helper functions for device runtime PM |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 5 | * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu> |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 6 | * |
| 7 | * This file is released under the GPLv2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/sched.h> |
Paul Gortmaker | 1b6bc32 | 2011-05-27 07:12:15 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 12 | #include <linux/pm_runtime.h> |
Ming Lei | c3dc2f1 | 2011-09-27 22:54:41 +0200 | [diff] [blame] | 13 | #include <trace/events/rpm.h> |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 14 | #include "power.h" |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 15 | |
Ulf Hansson | 5f59df7 | 2014-03-01 11:56:04 +0100 | [diff] [blame^] | 16 | #define RPM_GET_CALLBACK(dev, cb) \ |
| 17 | ({ \ |
| 18 | int (*__rpm_cb)(struct device *__d); \ |
| 19 | \ |
| 20 | if (dev->pm_domain) \ |
| 21 | __rpm_cb = dev->pm_domain->ops.cb; \ |
| 22 | else if (dev->type && dev->type->pm) \ |
| 23 | __rpm_cb = dev->type->pm->cb; \ |
| 24 | else if (dev->class && dev->class->pm) \ |
| 25 | __rpm_cb = dev->class->pm->cb; \ |
| 26 | else if (dev->bus && dev->bus->pm) \ |
| 27 | __rpm_cb = dev->bus->pm->cb; \ |
| 28 | else \ |
| 29 | __rpm_cb = NULL; \ |
| 30 | \ |
| 31 | if (!__rpm_cb && dev->driver && dev->driver->pm) \ |
| 32 | __rpm_cb = dev->driver->pm->cb; \ |
| 33 | \ |
| 34 | __rpm_cb; \ |
| 35 | }) |
| 36 | |
| 37 | static int (*rpm_get_suspend_cb(struct device *dev))(struct device *) |
| 38 | { |
| 39 | return RPM_GET_CALLBACK(dev, runtime_suspend); |
| 40 | } |
| 41 | |
| 42 | static int (*rpm_get_resume_cb(struct device *dev))(struct device *) |
| 43 | { |
| 44 | return RPM_GET_CALLBACK(dev, runtime_resume); |
| 45 | } |
| 46 | |
| 47 | static int (*rpm_get_idle_cb(struct device *dev))(struct device *) |
| 48 | { |
| 49 | return RPM_GET_CALLBACK(dev, runtime_idle); |
| 50 | } |
| 51 | |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 52 | static int rpm_resume(struct device *dev, int rpmflags); |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 53 | static int rpm_suspend(struct device *dev, int rpmflags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 54 | |
| 55 | /** |
Alan Stern | 4769373 | 2010-09-25 23:34:46 +0200 | [diff] [blame] | 56 | * update_pm_runtime_accounting - Update the time accounting of power states |
| 57 | * @dev: Device to update the accounting for |
| 58 | * |
| 59 | * In order to be able to have time accounting of the various power states |
| 60 | * (as used by programs such as PowerTOP to show the effectiveness of runtime |
| 61 | * PM), we need to track the time spent in each state. |
| 62 | * update_pm_runtime_accounting must be called each time before the |
| 63 | * runtime_status field is updated, to account the time in the old state |
| 64 | * correctly. |
| 65 | */ |
| 66 | void update_pm_runtime_accounting(struct device *dev) |
| 67 | { |
| 68 | unsigned long now = jiffies; |
venu byravarasu | def0c0a3 | 2011-11-03 10:12:14 +0100 | [diff] [blame] | 69 | unsigned long delta; |
Alan Stern | 4769373 | 2010-09-25 23:34:46 +0200 | [diff] [blame] | 70 | |
| 71 | delta = now - dev->power.accounting_timestamp; |
| 72 | |
Alan Stern | 4769373 | 2010-09-25 23:34:46 +0200 | [diff] [blame] | 73 | dev->power.accounting_timestamp = now; |
| 74 | |
| 75 | if (dev->power.disable_depth > 0) |
| 76 | return; |
| 77 | |
| 78 | if (dev->power.runtime_status == RPM_SUSPENDED) |
| 79 | dev->power.suspended_jiffies += delta; |
| 80 | else |
| 81 | dev->power.active_jiffies += delta; |
| 82 | } |
| 83 | |
| 84 | static void __update_runtime_status(struct device *dev, enum rpm_status status) |
| 85 | { |
| 86 | update_pm_runtime_accounting(dev); |
| 87 | dev->power.runtime_status = status; |
| 88 | } |
| 89 | |
| 90 | /** |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 91 | * pm_runtime_deactivate_timer - Deactivate given device's suspend timer. |
| 92 | * @dev: Device to handle. |
| 93 | */ |
| 94 | static void pm_runtime_deactivate_timer(struct device *dev) |
| 95 | { |
| 96 | if (dev->power.timer_expires > 0) { |
| 97 | del_timer(&dev->power.suspend_timer); |
| 98 | dev->power.timer_expires = 0; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests. |
| 104 | * @dev: Device to handle. |
| 105 | */ |
| 106 | static void pm_runtime_cancel_pending(struct device *dev) |
| 107 | { |
| 108 | pm_runtime_deactivate_timer(dev); |
| 109 | /* |
| 110 | * In case there's a request pending, make sure its work function will |
| 111 | * return without doing anything. |
| 112 | */ |
| 113 | dev->power.request = RPM_REQ_NONE; |
| 114 | } |
| 115 | |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 116 | /* |
| 117 | * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time. |
| 118 | * @dev: Device to handle. |
| 119 | * |
| 120 | * Compute the autosuspend-delay expiration time based on the device's |
| 121 | * power.last_busy time. If the delay has already expired or is disabled |
| 122 | * (negative) or the power.use_autosuspend flag isn't set, return 0. |
| 123 | * Otherwise return the expiration time in jiffies (adjusted to be nonzero). |
| 124 | * |
| 125 | * This function may be called either with or without dev->power.lock held. |
| 126 | * Either way it can be racy, since power.last_busy may be updated at any time. |
| 127 | */ |
| 128 | unsigned long pm_runtime_autosuspend_expiration(struct device *dev) |
| 129 | { |
| 130 | int autosuspend_delay; |
| 131 | long elapsed; |
| 132 | unsigned long last_busy; |
| 133 | unsigned long expires = 0; |
| 134 | |
| 135 | if (!dev->power.use_autosuspend) |
| 136 | goto out; |
| 137 | |
| 138 | autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay); |
| 139 | if (autosuspend_delay < 0) |
| 140 | goto out; |
| 141 | |
| 142 | last_busy = ACCESS_ONCE(dev->power.last_busy); |
| 143 | elapsed = jiffies - last_busy; |
| 144 | if (elapsed < 0) |
| 145 | goto out; /* jiffies has wrapped around. */ |
| 146 | |
| 147 | /* |
| 148 | * If the autosuspend_delay is >= 1 second, align the timer by rounding |
| 149 | * up to the nearest second. |
| 150 | */ |
| 151 | expires = last_busy + msecs_to_jiffies(autosuspend_delay); |
| 152 | if (autosuspend_delay >= 1000) |
| 153 | expires = round_jiffies(expires); |
| 154 | expires += !expires; |
| 155 | if (elapsed >= expires - last_busy) |
| 156 | expires = 0; /* Already expired. */ |
| 157 | |
| 158 | out: |
| 159 | return expires; |
| 160 | } |
| 161 | EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration); |
| 162 | |
Ming Lei | e823407 | 2013-02-22 16:34:11 -0800 | [diff] [blame] | 163 | static int dev_memalloc_noio(struct device *dev, void *data) |
| 164 | { |
| 165 | return dev->power.memalloc_noio; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag. |
| 170 | * @dev: Device to handle. |
| 171 | * @enable: True for setting the flag and False for clearing the flag. |
| 172 | * |
| 173 | * Set the flag for all devices in the path from the device to the |
| 174 | * root device in the device tree if @enable is true, otherwise clear |
| 175 | * the flag for devices in the path whose siblings don't set the flag. |
| 176 | * |
| 177 | * The function should only be called by block device, or network |
| 178 | * device driver for solving the deadlock problem during runtime |
| 179 | * resume/suspend: |
| 180 | * |
| 181 | * If memory allocation with GFP_KERNEL is called inside runtime |
| 182 | * resume/suspend callback of any one of its ancestors(or the |
| 183 | * block device itself), the deadlock may be triggered inside the |
| 184 | * memory allocation since it might not complete until the block |
| 185 | * device becomes active and the involed page I/O finishes. The |
| 186 | * situation is pointed out first by Alan Stern. Network device |
| 187 | * are involved in iSCSI kind of situation. |
| 188 | * |
| 189 | * The lock of dev_hotplug_mutex is held in the function for handling |
| 190 | * hotplug race because pm_runtime_set_memalloc_noio() may be called |
| 191 | * in async probe(). |
| 192 | * |
| 193 | * The function should be called between device_add() and device_del() |
| 194 | * on the affected device(block/network device). |
| 195 | */ |
| 196 | void pm_runtime_set_memalloc_noio(struct device *dev, bool enable) |
| 197 | { |
| 198 | static DEFINE_MUTEX(dev_hotplug_mutex); |
| 199 | |
| 200 | mutex_lock(&dev_hotplug_mutex); |
| 201 | for (;;) { |
| 202 | bool enabled; |
| 203 | |
| 204 | /* hold power lock since bitfield is not SMP-safe. */ |
| 205 | spin_lock_irq(&dev->power.lock); |
| 206 | enabled = dev->power.memalloc_noio; |
| 207 | dev->power.memalloc_noio = enable; |
| 208 | spin_unlock_irq(&dev->power.lock); |
| 209 | |
| 210 | /* |
| 211 | * not need to enable ancestors any more if the device |
| 212 | * has been enabled. |
| 213 | */ |
| 214 | if (enabled && enable) |
| 215 | break; |
| 216 | |
| 217 | dev = dev->parent; |
| 218 | |
| 219 | /* |
| 220 | * clear flag of the parent device only if all the |
| 221 | * children don't set the flag because ancestor's |
| 222 | * flag was set by any one of the descendants. |
| 223 | */ |
| 224 | if (!dev || (!enable && |
| 225 | device_for_each_child(dev, NULL, |
| 226 | dev_memalloc_noio))) |
| 227 | break; |
| 228 | } |
| 229 | mutex_unlock(&dev_hotplug_mutex); |
| 230 | } |
| 231 | EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio); |
| 232 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 233 | /** |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 234 | * rpm_check_suspend_allowed - Test whether a device may be suspended. |
| 235 | * @dev: Device to test. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 236 | */ |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 237 | static int rpm_check_suspend_allowed(struct device *dev) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 238 | { |
| 239 | int retval = 0; |
| 240 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 241 | if (dev->power.runtime_error) |
| 242 | retval = -EINVAL; |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 243 | else if (dev->power.disable_depth > 0) |
| 244 | retval = -EACCES; |
| 245 | else if (atomic_read(&dev->power.usage_count) > 0) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 246 | retval = -EAGAIN; |
| 247 | else if (!pm_children_suspended(dev)) |
| 248 | retval = -EBUSY; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 249 | |
| 250 | /* Pending resume requests take precedence over suspends. */ |
| 251 | else if ((dev->power.deferred_resume |
Kevin Winchester | 78ca7c3 | 2010-10-29 15:29:55 +0200 | [diff] [blame] | 252 | && dev->power.runtime_status == RPM_SUSPENDING) |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 253 | || (dev->power.request_pending |
| 254 | && dev->power.request == RPM_REQ_RESUME)) |
| 255 | retval = -EAGAIN; |
Rafael J. Wysocki | 55d7ec4 | 2012-08-15 21:32:04 +0200 | [diff] [blame] | 256 | else if (__dev_pm_qos_read_value(dev) < 0) |
| 257 | retval = -EPERM; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 258 | else if (dev->power.runtime_status == RPM_SUSPENDED) |
| 259 | retval = 1; |
| 260 | |
| 261 | return retval; |
| 262 | } |
| 263 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 264 | /** |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 265 | * __rpm_callback - Run a given runtime PM callback for a given device. |
| 266 | * @cb: Runtime PM callback to run. |
| 267 | * @dev: Device to run the callback for. |
| 268 | */ |
| 269 | static int __rpm_callback(int (*cb)(struct device *), struct device *dev) |
| 270 | __releases(&dev->power.lock) __acquires(&dev->power.lock) |
| 271 | { |
| 272 | int retval; |
| 273 | |
| 274 | if (dev->power.irq_safe) |
| 275 | spin_unlock(&dev->power.lock); |
| 276 | else |
| 277 | spin_unlock_irq(&dev->power.lock); |
| 278 | |
| 279 | retval = cb(dev); |
| 280 | |
| 281 | if (dev->power.irq_safe) |
| 282 | spin_lock(&dev->power.lock); |
| 283 | else |
| 284 | spin_lock_irq(&dev->power.lock); |
| 285 | |
| 286 | return retval; |
| 287 | } |
| 288 | |
| 289 | /** |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 290 | * rpm_idle - Notify device bus type if the device can be suspended. |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 291 | * @dev: Device to notify the bus type about. |
| 292 | * @rpmflags: Flag bits. |
| 293 | * |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 294 | * Check if the device's runtime PM status allows it to be suspended. If |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 295 | * another idle notification has been started earlier, return immediately. If |
| 296 | * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise |
Ulf Hansson | d66e6db | 2013-10-15 22:25:08 +0200 | [diff] [blame] | 297 | * run the ->runtime_idle() callback directly. If the ->runtime_idle callback |
| 298 | * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag. |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 299 | * |
| 300 | * This function must be called under dev->power.lock with interrupts disabled. |
| 301 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 302 | static int rpm_idle(struct device *dev, int rpmflags) |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 303 | { |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 304 | int (*callback)(struct device *); |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 305 | int retval; |
| 306 | |
Ming Lei | c3dc2f1 | 2011-09-27 22:54:41 +0200 | [diff] [blame] | 307 | trace_rpm_idle(dev, rpmflags); |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 308 | retval = rpm_check_suspend_allowed(dev); |
| 309 | if (retval < 0) |
| 310 | ; /* Conditions are wrong. */ |
| 311 | |
| 312 | /* Idle notifications are allowed only in the RPM_ACTIVE state. */ |
| 313 | else if (dev->power.runtime_status != RPM_ACTIVE) |
| 314 | retval = -EAGAIN; |
| 315 | |
| 316 | /* |
| 317 | * Any pending request other than an idle notification takes |
| 318 | * precedence over us, except that the timer may be running. |
| 319 | */ |
| 320 | else if (dev->power.request_pending && |
| 321 | dev->power.request > RPM_REQ_IDLE) |
| 322 | retval = -EAGAIN; |
| 323 | |
| 324 | /* Act as though RPM_NOWAIT is always set. */ |
| 325 | else if (dev->power.idle_notification) |
| 326 | retval = -EINPROGRESS; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 327 | if (retval) |
| 328 | goto out; |
| 329 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 330 | /* Pending requests need to be canceled. */ |
| 331 | dev->power.request = RPM_REQ_NONE; |
| 332 | |
Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 333 | if (dev->power.no_callbacks) |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 334 | goto out; |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 335 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 336 | /* Carry out an asynchronous or a synchronous idle notification. */ |
| 337 | if (rpmflags & RPM_ASYNC) { |
| 338 | dev->power.request = RPM_REQ_IDLE; |
| 339 | if (!dev->power.request_pending) { |
| 340 | dev->power.request_pending = true; |
| 341 | queue_work(pm_wq, &dev->power.work); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 342 | } |
Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 343 | trace_rpm_return_int(dev, _THIS_IP_, 0); |
| 344 | return 0; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | dev->power.idle_notification = true; |
| 348 | |
Ulf Hansson | 5f59df7 | 2014-03-01 11:56:04 +0100 | [diff] [blame^] | 349 | callback = rpm_get_idle_cb(dev); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 350 | |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 351 | if (callback) |
Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 352 | retval = __rpm_callback(callback, dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 353 | |
| 354 | dev->power.idle_notification = false; |
| 355 | wake_up_all(&dev->power.wait_queue); |
| 356 | |
| 357 | out: |
Ming Lei | c3dc2f1 | 2011-09-27 22:54:41 +0200 | [diff] [blame] | 358 | trace_rpm_return_int(dev, _THIS_IP_, retval); |
Ulf Hansson | d66e6db | 2013-10-15 22:25:08 +0200 | [diff] [blame] | 359 | return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /** |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 363 | * rpm_callback - Run a given runtime PM callback for a given device. |
| 364 | * @cb: Runtime PM callback to run. |
| 365 | * @dev: Device to run the callback for. |
| 366 | */ |
| 367 | static int rpm_callback(int (*cb)(struct device *), struct device *dev) |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 368 | { |
| 369 | int retval; |
| 370 | |
| 371 | if (!cb) |
| 372 | return -ENOSYS; |
| 373 | |
Ming Lei | db88175 | 2013-02-22 16:34:19 -0800 | [diff] [blame] | 374 | if (dev->power.memalloc_noio) { |
| 375 | unsigned int noio_flag; |
| 376 | |
| 377 | /* |
| 378 | * Deadlock might be caused if memory allocation with |
| 379 | * GFP_KERNEL happens inside runtime_suspend and |
| 380 | * runtime_resume callbacks of one block device's |
| 381 | * ancestor or the block device itself. Network |
| 382 | * device might be thought as part of iSCSI block |
| 383 | * device, so network device and its ancestor should |
| 384 | * be marked as memalloc_noio too. |
| 385 | */ |
| 386 | noio_flag = memalloc_noio_save(); |
| 387 | retval = __rpm_callback(cb, dev); |
| 388 | memalloc_noio_restore(noio_flag); |
| 389 | } else { |
| 390 | retval = __rpm_callback(cb, dev); |
| 391 | } |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 392 | |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 393 | dev->power.runtime_error = retval; |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 394 | return retval != -EACCES ? retval : -EIO; |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 398 | * rpm_suspend - Carry out runtime suspend of given device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 399 | * @dev: Device to suspend. |
Alan Stern | 3f9af051 | 2010-09-25 23:34:54 +0200 | [diff] [blame] | 400 | * @rpmflags: Flag bits. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 401 | * |
Ming Lei | 47d8f0b | 2011-10-12 11:53:32 +0800 | [diff] [blame] | 402 | * Check if the device's runtime PM status allows it to be suspended. |
| 403 | * Cancel a pending idle notification, autosuspend or suspend. If |
| 404 | * another suspend has been started earlier, either return immediately |
| 405 | * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC |
| 406 | * flags. If the RPM_ASYNC flag is set then queue a suspend request; |
Ming Lei | 857b36c | 2011-10-12 22:59:33 +0200 | [diff] [blame] | 407 | * otherwise run the ->runtime_suspend() callback directly. When |
| 408 | * ->runtime_suspend succeeded, if a deferred resume was requested while |
| 409 | * the callback was running then carry it out, otherwise send an idle |
| 410 | * notification for its parent (if the suspend succeeded and both |
| 411 | * ignore_children of parent->power and irq_safe of dev->power are not set). |
Alan Stern | 886486b | 2011-11-03 23:39:18 +0100 | [diff] [blame] | 412 | * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO |
| 413 | * flag is set and the next autosuspend-delay expiration time is in the |
| 414 | * future, schedule another autosuspend attempt. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 415 | * |
| 416 | * This function must be called under dev->power.lock with interrupts disabled. |
| 417 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 418 | static int rpm_suspend(struct device *dev, int rpmflags) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 419 | __releases(&dev->power.lock) __acquires(&dev->power.lock) |
| 420 | { |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 421 | int (*callback)(struct device *); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 422 | struct device *parent = NULL; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 423 | int retval; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 424 | |
Ming Lei | c3dc2f1 | 2011-09-27 22:54:41 +0200 | [diff] [blame] | 425 | trace_rpm_suspend(dev, rpmflags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 426 | |
| 427 | repeat: |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 428 | retval = rpm_check_suspend_allowed(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 429 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 430 | if (retval < 0) |
| 431 | ; /* Conditions are wrong. */ |
| 432 | |
| 433 | /* Synchronous suspends are not allowed in the RPM_RESUMING state. */ |
| 434 | else if (dev->power.runtime_status == RPM_RESUMING && |
| 435 | !(rpmflags & RPM_ASYNC)) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 436 | retval = -EAGAIN; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 437 | if (retval) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 438 | goto out; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 439 | |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 440 | /* If the autosuspend_delay time hasn't expired yet, reschedule. */ |
| 441 | if ((rpmflags & RPM_AUTO) |
| 442 | && dev->power.runtime_status != RPM_SUSPENDING) { |
| 443 | unsigned long expires = pm_runtime_autosuspend_expiration(dev); |
| 444 | |
| 445 | if (expires != 0) { |
| 446 | /* Pending requests need to be canceled. */ |
| 447 | dev->power.request = RPM_REQ_NONE; |
| 448 | |
| 449 | /* |
| 450 | * Optimization: If the timer is already running and is |
| 451 | * set to expire at or before the autosuspend delay, |
| 452 | * avoid the overhead of resetting it. Just let it |
| 453 | * expire; pm_suspend_timer_fn() will take care of the |
| 454 | * rest. |
| 455 | */ |
| 456 | if (!(dev->power.timer_expires && time_before_eq( |
| 457 | dev->power.timer_expires, expires))) { |
| 458 | dev->power.timer_expires = expires; |
| 459 | mod_timer(&dev->power.suspend_timer, expires); |
| 460 | } |
| 461 | dev->power.timer_autosuspends = 1; |
| 462 | goto out; |
| 463 | } |
| 464 | } |
| 465 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 466 | /* Other scheduled or pending requests need to be canceled. */ |
| 467 | pm_runtime_cancel_pending(dev); |
| 468 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 469 | if (dev->power.runtime_status == RPM_SUSPENDING) { |
| 470 | DEFINE_WAIT(wait); |
| 471 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 472 | if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 473 | retval = -EINPROGRESS; |
| 474 | goto out; |
| 475 | } |
| 476 | |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 477 | if (dev->power.irq_safe) { |
| 478 | spin_unlock(&dev->power.lock); |
| 479 | |
| 480 | cpu_relax(); |
| 481 | |
| 482 | spin_lock(&dev->power.lock); |
| 483 | goto repeat; |
| 484 | } |
| 485 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 486 | /* Wait for the other suspend running in parallel with us. */ |
| 487 | for (;;) { |
| 488 | prepare_to_wait(&dev->power.wait_queue, &wait, |
| 489 | TASK_UNINTERRUPTIBLE); |
| 490 | if (dev->power.runtime_status != RPM_SUSPENDING) |
| 491 | break; |
| 492 | |
| 493 | spin_unlock_irq(&dev->power.lock); |
| 494 | |
| 495 | schedule(); |
| 496 | |
| 497 | spin_lock_irq(&dev->power.lock); |
| 498 | } |
| 499 | finish_wait(&dev->power.wait_queue, &wait); |
| 500 | goto repeat; |
| 501 | } |
| 502 | |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 503 | if (dev->power.no_callbacks) |
| 504 | goto no_callback; /* Assume success. */ |
| 505 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 506 | /* Carry out an asynchronous or a synchronous suspend. */ |
| 507 | if (rpmflags & RPM_ASYNC) { |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 508 | dev->power.request = (rpmflags & RPM_AUTO) ? |
| 509 | RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 510 | if (!dev->power.request_pending) { |
| 511 | dev->power.request_pending = true; |
| 512 | queue_work(pm_wq, &dev->power.work); |
| 513 | } |
| 514 | goto out; |
| 515 | } |
| 516 | |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 517 | __update_runtime_status(dev, RPM_SUSPENDING); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 518 | |
Ulf Hansson | 5f59df7 | 2014-03-01 11:56:04 +0100 | [diff] [blame^] | 519 | callback = rpm_get_suspend_cb(dev); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 520 | |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 521 | retval = rpm_callback(callback, dev); |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 522 | if (retval) |
| 523 | goto fail; |
Alan Stern | 886486b | 2011-11-03 23:39:18 +0100 | [diff] [blame] | 524 | |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 525 | no_callback: |
Ming Lei | 857b36c | 2011-10-12 22:59:33 +0200 | [diff] [blame] | 526 | __update_runtime_status(dev, RPM_SUSPENDED); |
| 527 | pm_runtime_deactivate_timer(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 528 | |
Ming Lei | 857b36c | 2011-10-12 22:59:33 +0200 | [diff] [blame] | 529 | if (dev->parent) { |
| 530 | parent = dev->parent; |
| 531 | atomic_add_unless(&parent->power.child_count, -1, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 532 | } |
| 533 | wake_up_all(&dev->power.wait_queue); |
| 534 | |
| 535 | if (dev->power.deferred_resume) { |
Rafael J. Wysocki | 58a34de | 2012-08-15 21:31:55 +0200 | [diff] [blame] | 536 | dev->power.deferred_resume = false; |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 537 | rpm_resume(dev, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 538 | retval = -EAGAIN; |
| 539 | goto out; |
| 540 | } |
| 541 | |
Alan Stern | c3810c8 | 2011-01-25 20:50:07 +0100 | [diff] [blame] | 542 | /* Maybe the parent is now able to suspend. */ |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 543 | if (parent && !parent->power.ignore_children && !dev->power.irq_safe) { |
Alan Stern | c3810c8 | 2011-01-25 20:50:07 +0100 | [diff] [blame] | 544 | spin_unlock(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 545 | |
Alan Stern | c3810c8 | 2011-01-25 20:50:07 +0100 | [diff] [blame] | 546 | spin_lock(&parent->power.lock); |
| 547 | rpm_idle(parent, RPM_ASYNC); |
| 548 | spin_unlock(&parent->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 549 | |
Alan Stern | c3810c8 | 2011-01-25 20:50:07 +0100 | [diff] [blame] | 550 | spin_lock(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | out: |
Ming Lei | c3dc2f1 | 2011-09-27 22:54:41 +0200 | [diff] [blame] | 554 | trace_rpm_return_int(dev, _THIS_IP_, retval); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 555 | |
| 556 | return retval; |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 557 | |
| 558 | fail: |
| 559 | __update_runtime_status(dev, RPM_ACTIVE); |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 560 | dev->power.deferred_resume = false; |
Alan Stern | f2791d7 | 2012-03-26 22:46:52 +0200 | [diff] [blame] | 561 | wake_up_all(&dev->power.wait_queue); |
| 562 | |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 563 | if (retval == -EAGAIN || retval == -EBUSY) { |
| 564 | dev->power.runtime_error = 0; |
| 565 | |
| 566 | /* |
| 567 | * If the callback routine failed an autosuspend, and |
| 568 | * if the last_busy time has been updated so that there |
| 569 | * is a new autosuspend expiration time, automatically |
| 570 | * reschedule another autosuspend. |
| 571 | */ |
| 572 | if ((rpmflags & RPM_AUTO) && |
| 573 | pm_runtime_autosuspend_expiration(dev) != 0) |
| 574 | goto repeat; |
| 575 | } else { |
| 576 | pm_runtime_cancel_pending(dev); |
| 577 | } |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 578 | goto out; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 582 | * rpm_resume - Carry out runtime resume of given device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 583 | * @dev: Device to resume. |
Alan Stern | 3f9af051 | 2010-09-25 23:34:54 +0200 | [diff] [blame] | 584 | * @rpmflags: Flag bits. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 585 | * |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 586 | * Check if the device's runtime PM status allows it to be resumed. Cancel |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 587 | * any scheduled or pending requests. If another resume has been started |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 588 | * earlier, either return immediately or wait for it to finish, depending on the |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 589 | * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in |
| 590 | * parallel with this function, either tell the other process to resume after |
| 591 | * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC |
| 592 | * flag is set then queue a resume request; otherwise run the |
| 593 | * ->runtime_resume() callback directly. Queue an idle notification for the |
| 594 | * device if the resume succeeded. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 595 | * |
| 596 | * This function must be called under dev->power.lock with interrupts disabled. |
| 597 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 598 | static int rpm_resume(struct device *dev, int rpmflags) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 599 | __releases(&dev->power.lock) __acquires(&dev->power.lock) |
| 600 | { |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 601 | int (*callback)(struct device *); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 602 | struct device *parent = NULL; |
| 603 | int retval = 0; |
| 604 | |
Ming Lei | c3dc2f1 | 2011-09-27 22:54:41 +0200 | [diff] [blame] | 605 | trace_rpm_resume(dev, rpmflags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 606 | |
| 607 | repeat: |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 608 | if (dev->power.runtime_error) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 609 | retval = -EINVAL; |
Kevin Hilman | 6f3c77b | 2012-09-21 22:47:34 +0000 | [diff] [blame] | 610 | else if (dev->power.disable_depth == 1 && dev->power.is_suspended |
| 611 | && dev->power.runtime_status == RPM_ACTIVE) |
| 612 | retval = 1; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 613 | else if (dev->power.disable_depth > 0) |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 614 | retval = -EACCES; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 615 | if (retval) |
| 616 | goto out; |
| 617 | |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 618 | /* |
| 619 | * Other scheduled or pending requests need to be canceled. Small |
| 620 | * optimization: If an autosuspend timer is running, leave it running |
| 621 | * rather than cancelling it now only to restart it again in the near |
| 622 | * future. |
| 623 | */ |
| 624 | dev->power.request = RPM_REQ_NONE; |
| 625 | if (!dev->power.timer_autosuspends) |
| 626 | pm_runtime_deactivate_timer(dev); |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 627 | |
| 628 | if (dev->power.runtime_status == RPM_ACTIVE) { |
| 629 | retval = 1; |
| 630 | goto out; |
| 631 | } |
| 632 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 633 | if (dev->power.runtime_status == RPM_RESUMING |
| 634 | || dev->power.runtime_status == RPM_SUSPENDING) { |
| 635 | DEFINE_WAIT(wait); |
| 636 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 637 | if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 638 | if (dev->power.runtime_status == RPM_SUSPENDING) |
| 639 | dev->power.deferred_resume = true; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 640 | else |
| 641 | retval = -EINPROGRESS; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 642 | goto out; |
| 643 | } |
| 644 | |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 645 | if (dev->power.irq_safe) { |
| 646 | spin_unlock(&dev->power.lock); |
| 647 | |
| 648 | cpu_relax(); |
| 649 | |
| 650 | spin_lock(&dev->power.lock); |
| 651 | goto repeat; |
| 652 | } |
| 653 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 654 | /* Wait for the operation carried out in parallel with us. */ |
| 655 | for (;;) { |
| 656 | prepare_to_wait(&dev->power.wait_queue, &wait, |
| 657 | TASK_UNINTERRUPTIBLE); |
| 658 | if (dev->power.runtime_status != RPM_RESUMING |
| 659 | && dev->power.runtime_status != RPM_SUSPENDING) |
| 660 | break; |
| 661 | |
| 662 | spin_unlock_irq(&dev->power.lock); |
| 663 | |
| 664 | schedule(); |
| 665 | |
| 666 | spin_lock_irq(&dev->power.lock); |
| 667 | } |
| 668 | finish_wait(&dev->power.wait_queue, &wait); |
| 669 | goto repeat; |
| 670 | } |
| 671 | |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 672 | /* |
| 673 | * See if we can skip waking up the parent. This is safe only if |
| 674 | * power.no_callbacks is set, because otherwise we don't know whether |
| 675 | * the resume will actually succeed. |
| 676 | */ |
| 677 | if (dev->power.no_callbacks && !parent && dev->parent) { |
Ming Lei | d63be5f | 2010-10-22 23:48:14 +0200 | [diff] [blame] | 678 | spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING); |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 679 | if (dev->parent->power.disable_depth > 0 |
| 680 | || dev->parent->power.ignore_children |
| 681 | || dev->parent->power.runtime_status == RPM_ACTIVE) { |
| 682 | atomic_inc(&dev->parent->power.child_count); |
| 683 | spin_unlock(&dev->parent->power.lock); |
Rafael J. Wysocki | 7f321c2 | 2012-08-15 21:31:45 +0200 | [diff] [blame] | 684 | retval = 1; |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 685 | goto no_callback; /* Assume success. */ |
| 686 | } |
| 687 | spin_unlock(&dev->parent->power.lock); |
| 688 | } |
| 689 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 690 | /* Carry out an asynchronous or a synchronous resume. */ |
| 691 | if (rpmflags & RPM_ASYNC) { |
| 692 | dev->power.request = RPM_REQ_RESUME; |
| 693 | if (!dev->power.request_pending) { |
| 694 | dev->power.request_pending = true; |
| 695 | queue_work(pm_wq, &dev->power.work); |
| 696 | } |
| 697 | retval = 0; |
| 698 | goto out; |
| 699 | } |
| 700 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 701 | if (!parent && dev->parent) { |
| 702 | /* |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 703 | * Increment the parent's usage counter and resume it if |
| 704 | * necessary. Not needed if dev is irq-safe; then the |
| 705 | * parent is permanently resumed. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 706 | */ |
| 707 | parent = dev->parent; |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 708 | if (dev->power.irq_safe) |
| 709 | goto skip_parent; |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 710 | spin_unlock(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 711 | |
| 712 | pm_runtime_get_noresume(parent); |
| 713 | |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 714 | spin_lock(&parent->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 715 | /* |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 716 | * We can resume if the parent's runtime PM is disabled or it |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 717 | * is set to ignore children. |
| 718 | */ |
| 719 | if (!parent->power.disable_depth |
| 720 | && !parent->power.ignore_children) { |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 721 | rpm_resume(parent, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 722 | if (parent->power.runtime_status != RPM_ACTIVE) |
| 723 | retval = -EBUSY; |
| 724 | } |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 725 | spin_unlock(&parent->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 726 | |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 727 | spin_lock(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 728 | if (retval) |
| 729 | goto out; |
| 730 | goto repeat; |
| 731 | } |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 732 | skip_parent: |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 733 | |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 734 | if (dev->power.no_callbacks) |
| 735 | goto no_callback; /* Assume success. */ |
| 736 | |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 737 | __update_runtime_status(dev, RPM_RESUMING); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 738 | |
Ulf Hansson | 5f59df7 | 2014-03-01 11:56:04 +0100 | [diff] [blame^] | 739 | callback = rpm_get_resume_cb(dev); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 740 | |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 741 | retval = rpm_callback(callback, dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 742 | if (retval) { |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 743 | __update_runtime_status(dev, RPM_SUSPENDED); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 744 | pm_runtime_cancel_pending(dev); |
| 745 | } else { |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 746 | no_callback: |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 747 | __update_runtime_status(dev, RPM_ACTIVE); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 748 | if (parent) |
| 749 | atomic_inc(&parent->power.child_count); |
| 750 | } |
| 751 | wake_up_all(&dev->power.wait_queue); |
| 752 | |
Rafael J. Wysocki | 7f321c2 | 2012-08-15 21:31:45 +0200 | [diff] [blame] | 753 | if (retval >= 0) |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 754 | rpm_idle(dev, RPM_ASYNC); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 755 | |
| 756 | out: |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 757 | if (parent && !dev->power.irq_safe) { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 758 | spin_unlock_irq(&dev->power.lock); |
| 759 | |
| 760 | pm_runtime_put(parent); |
| 761 | |
| 762 | spin_lock_irq(&dev->power.lock); |
| 763 | } |
| 764 | |
Ming Lei | c3dc2f1 | 2011-09-27 22:54:41 +0200 | [diff] [blame] | 765 | trace_rpm_return_int(dev, _THIS_IP_, retval); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 766 | |
| 767 | return retval; |
| 768 | } |
| 769 | |
| 770 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 771 | * pm_runtime_work - Universal runtime PM work function. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 772 | * @work: Work structure used for scheduling the execution of this function. |
| 773 | * |
| 774 | * Use @work to get the device object the work is to be done for, determine what |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 775 | * is to be done and execute the appropriate runtime PM function. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 776 | */ |
| 777 | static void pm_runtime_work(struct work_struct *work) |
| 778 | { |
| 779 | struct device *dev = container_of(work, struct device, power.work); |
| 780 | enum rpm_request req; |
| 781 | |
| 782 | spin_lock_irq(&dev->power.lock); |
| 783 | |
| 784 | if (!dev->power.request_pending) |
| 785 | goto out; |
| 786 | |
| 787 | req = dev->power.request; |
| 788 | dev->power.request = RPM_REQ_NONE; |
| 789 | dev->power.request_pending = false; |
| 790 | |
| 791 | switch (req) { |
| 792 | case RPM_REQ_NONE: |
| 793 | break; |
| 794 | case RPM_REQ_IDLE: |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 795 | rpm_idle(dev, RPM_NOWAIT); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 796 | break; |
| 797 | case RPM_REQ_SUSPEND: |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 798 | rpm_suspend(dev, RPM_NOWAIT); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 799 | break; |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 800 | case RPM_REQ_AUTOSUSPEND: |
| 801 | rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO); |
| 802 | break; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 803 | case RPM_REQ_RESUME: |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 804 | rpm_resume(dev, RPM_NOWAIT); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 805 | break; |
| 806 | } |
| 807 | |
| 808 | out: |
| 809 | spin_unlock_irq(&dev->power.lock); |
| 810 | } |
| 811 | |
| 812 | /** |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 813 | * pm_suspend_timer_fn - Timer function for pm_schedule_suspend(). |
| 814 | * @data: Device pointer passed by pm_schedule_suspend(). |
| 815 | * |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 816 | * Check if the time is right and queue a suspend request. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 817 | */ |
| 818 | static void pm_suspend_timer_fn(unsigned long data) |
| 819 | { |
| 820 | struct device *dev = (struct device *)data; |
| 821 | unsigned long flags; |
| 822 | unsigned long expires; |
| 823 | |
| 824 | spin_lock_irqsave(&dev->power.lock, flags); |
| 825 | |
| 826 | expires = dev->power.timer_expires; |
| 827 | /* If 'expire' is after 'jiffies' we've been called too early. */ |
| 828 | if (expires > 0 && !time_after(expires, jiffies)) { |
| 829 | dev->power.timer_expires = 0; |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 830 | rpm_suspend(dev, dev->power.timer_autosuspends ? |
| 831 | (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * pm_schedule_suspend - Set up a timer to submit a suspend request in future. |
| 839 | * @dev: Device to suspend. |
| 840 | * @delay: Time to wait before submitting a suspend request, in milliseconds. |
| 841 | */ |
| 842 | int pm_schedule_suspend(struct device *dev, unsigned int delay) |
| 843 | { |
| 844 | unsigned long flags; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 845 | int retval; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 846 | |
| 847 | spin_lock_irqsave(&dev->power.lock, flags); |
| 848 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 849 | if (!delay) { |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 850 | retval = rpm_suspend(dev, RPM_ASYNC); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 851 | goto out; |
| 852 | } |
| 853 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 854 | retval = rpm_check_suspend_allowed(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 855 | if (retval) |
| 856 | goto out; |
| 857 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 858 | /* Other scheduled or pending requests need to be canceled. */ |
| 859 | pm_runtime_cancel_pending(dev); |
| 860 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 861 | dev->power.timer_expires = jiffies + msecs_to_jiffies(delay); |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 862 | dev->power.timer_expires += !dev->power.timer_expires; |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 863 | dev->power.timer_autosuspends = 0; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 864 | mod_timer(&dev->power.suspend_timer, dev->power.timer_expires); |
| 865 | |
| 866 | out: |
| 867 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 868 | |
| 869 | return retval; |
| 870 | } |
| 871 | EXPORT_SYMBOL_GPL(pm_schedule_suspend); |
| 872 | |
| 873 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 874 | * __pm_runtime_idle - Entry point for runtime idle operations. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 875 | * @dev: Device to send idle notification for. |
| 876 | * @rpmflags: Flag bits. |
| 877 | * |
| 878 | * If the RPM_GET_PUT flag is set, decrement the device's usage count and |
| 879 | * return immediately if it is larger than zero. Then carry out an idle |
| 880 | * notification, either synchronous or asynchronous. |
| 881 | * |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 882 | * This routine may be called in atomic context if the RPM_ASYNC flag is set, |
| 883 | * or if pm_runtime_irq_safe() has been called. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 884 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 885 | int __pm_runtime_idle(struct device *dev, int rpmflags) |
| 886 | { |
| 887 | unsigned long flags; |
| 888 | int retval; |
| 889 | |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 890 | might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); |
| 891 | |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 892 | if (rpmflags & RPM_GET_PUT) { |
| 893 | if (!atomic_dec_and_test(&dev->power.usage_count)) |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | spin_lock_irqsave(&dev->power.lock, flags); |
| 898 | retval = rpm_idle(dev, rpmflags); |
| 899 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 900 | |
| 901 | return retval; |
| 902 | } |
| 903 | EXPORT_SYMBOL_GPL(__pm_runtime_idle); |
| 904 | |
| 905 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 906 | * __pm_runtime_suspend - Entry point for runtime put/suspend operations. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 907 | * @dev: Device to suspend. |
| 908 | * @rpmflags: Flag bits. |
| 909 | * |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 910 | * If the RPM_GET_PUT flag is set, decrement the device's usage count and |
| 911 | * return immediately if it is larger than zero. Then carry out a suspend, |
| 912 | * either synchronous or asynchronous. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 913 | * |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 914 | * This routine may be called in atomic context if the RPM_ASYNC flag is set, |
| 915 | * or if pm_runtime_irq_safe() has been called. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 916 | */ |
| 917 | int __pm_runtime_suspend(struct device *dev, int rpmflags) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 918 | { |
| 919 | unsigned long flags; |
| 920 | int retval; |
| 921 | |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 922 | might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); |
| 923 | |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 924 | if (rpmflags & RPM_GET_PUT) { |
| 925 | if (!atomic_dec_and_test(&dev->power.usage_count)) |
| 926 | return 0; |
| 927 | } |
| 928 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 929 | spin_lock_irqsave(&dev->power.lock, flags); |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 930 | retval = rpm_suspend(dev, rpmflags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 931 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 932 | |
| 933 | return retval; |
| 934 | } |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 935 | EXPORT_SYMBOL_GPL(__pm_runtime_suspend); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 936 | |
| 937 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 938 | * __pm_runtime_resume - Entry point for runtime resume operations. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 939 | * @dev: Device to resume. |
Alan Stern | 3f9af051 | 2010-09-25 23:34:54 +0200 | [diff] [blame] | 940 | * @rpmflags: Flag bits. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 941 | * |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 942 | * If the RPM_GET_PUT flag is set, increment the device's usage count. Then |
| 943 | * carry out a resume, either synchronous or asynchronous. |
| 944 | * |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 945 | * This routine may be called in atomic context if the RPM_ASYNC flag is set, |
| 946 | * or if pm_runtime_irq_safe() has been called. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 947 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 948 | int __pm_runtime_resume(struct device *dev, int rpmflags) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 949 | { |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 950 | unsigned long flags; |
Alan Stern | 1d531c1 | 2009-12-13 20:28:30 +0100 | [diff] [blame] | 951 | int retval; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 952 | |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 953 | might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); |
| 954 | |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 955 | if (rpmflags & RPM_GET_PUT) |
| 956 | atomic_inc(&dev->power.usage_count); |
| 957 | |
| 958 | spin_lock_irqsave(&dev->power.lock, flags); |
| 959 | retval = rpm_resume(dev, rpmflags); |
| 960 | spin_unlock_irqrestore(&dev->power.lock, flags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 961 | |
| 962 | return retval; |
| 963 | } |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 964 | EXPORT_SYMBOL_GPL(__pm_runtime_resume); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 965 | |
| 966 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 967 | * __pm_runtime_set_status - Set runtime PM status of a device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 968 | * @dev: Device to handle. |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 969 | * @status: New runtime PM status of the device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 970 | * |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 971 | * If runtime PM of the device is disabled or its power.runtime_error field is |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 972 | * different from zero, the status may be changed either to RPM_ACTIVE, or to |
| 973 | * RPM_SUSPENDED, as long as that reflects the actual state of the device. |
| 974 | * However, if the device has a parent and the parent is not active, and the |
| 975 | * parent's power.ignore_children flag is unset, the device's status cannot be |
| 976 | * set to RPM_ACTIVE, so -EBUSY is returned in that case. |
| 977 | * |
| 978 | * If successful, __pm_runtime_set_status() clears the power.runtime_error field |
| 979 | * and the device parent's counter of unsuspended children is modified to |
| 980 | * reflect the new status. If the new status is RPM_SUSPENDED, an idle |
| 981 | * notification request for the parent is submitted. |
| 982 | */ |
| 983 | int __pm_runtime_set_status(struct device *dev, unsigned int status) |
| 984 | { |
| 985 | struct device *parent = dev->parent; |
| 986 | unsigned long flags; |
| 987 | bool notify_parent = false; |
| 988 | int error = 0; |
| 989 | |
| 990 | if (status != RPM_ACTIVE && status != RPM_SUSPENDED) |
| 991 | return -EINVAL; |
| 992 | |
| 993 | spin_lock_irqsave(&dev->power.lock, flags); |
| 994 | |
| 995 | if (!dev->power.runtime_error && !dev->power.disable_depth) { |
| 996 | error = -EAGAIN; |
| 997 | goto out; |
| 998 | } |
| 999 | |
| 1000 | if (dev->power.runtime_status == status) |
| 1001 | goto out_set; |
| 1002 | |
| 1003 | if (status == RPM_SUSPENDED) { |
| 1004 | /* It always is possible to set the status to 'suspended'. */ |
| 1005 | if (parent) { |
| 1006 | atomic_add_unless(&parent->power.child_count, -1, 0); |
| 1007 | notify_parent = !parent->power.ignore_children; |
| 1008 | } |
| 1009 | goto out_set; |
| 1010 | } |
| 1011 | |
| 1012 | if (parent) { |
Rafael J. Wysocki | bab636b | 2009-12-03 20:21:21 +0100 | [diff] [blame] | 1013 | spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1014 | |
| 1015 | /* |
| 1016 | * It is invalid to put an active child under a parent that is |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1017 | * not active, has runtime PM enabled and the |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1018 | * 'power.ignore_children' flag unset. |
| 1019 | */ |
| 1020 | if (!parent->power.disable_depth |
| 1021 | && !parent->power.ignore_children |
Rafael J. Wysocki | 965c4ac | 2009-12-03 21:04:41 +0100 | [diff] [blame] | 1022 | && parent->power.runtime_status != RPM_ACTIVE) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1023 | error = -EBUSY; |
Rafael J. Wysocki | 965c4ac | 2009-12-03 21:04:41 +0100 | [diff] [blame] | 1024 | else if (dev->power.runtime_status == RPM_SUSPENDED) |
| 1025 | atomic_inc(&parent->power.child_count); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1026 | |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 1027 | spin_unlock(&parent->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1028 | |
| 1029 | if (error) |
| 1030 | goto out; |
| 1031 | } |
| 1032 | |
| 1033 | out_set: |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 1034 | __update_runtime_status(dev, status); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1035 | dev->power.runtime_error = 0; |
| 1036 | out: |
| 1037 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 1038 | |
| 1039 | if (notify_parent) |
| 1040 | pm_request_idle(parent); |
| 1041 | |
| 1042 | return error; |
| 1043 | } |
| 1044 | EXPORT_SYMBOL_GPL(__pm_runtime_set_status); |
| 1045 | |
| 1046 | /** |
| 1047 | * __pm_runtime_barrier - Cancel pending requests and wait for completions. |
| 1048 | * @dev: Device to handle. |
| 1049 | * |
| 1050 | * Flush all pending requests for the device from pm_wq and wait for all |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1051 | * runtime PM operations involving the device in progress to complete. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1052 | * |
| 1053 | * Should be called under dev->power.lock with interrupts disabled. |
| 1054 | */ |
| 1055 | static void __pm_runtime_barrier(struct device *dev) |
| 1056 | { |
| 1057 | pm_runtime_deactivate_timer(dev); |
| 1058 | |
| 1059 | if (dev->power.request_pending) { |
| 1060 | dev->power.request = RPM_REQ_NONE; |
| 1061 | spin_unlock_irq(&dev->power.lock); |
| 1062 | |
| 1063 | cancel_work_sync(&dev->power.work); |
| 1064 | |
| 1065 | spin_lock_irq(&dev->power.lock); |
| 1066 | dev->power.request_pending = false; |
| 1067 | } |
| 1068 | |
| 1069 | if (dev->power.runtime_status == RPM_SUSPENDING |
| 1070 | || dev->power.runtime_status == RPM_RESUMING |
| 1071 | || dev->power.idle_notification) { |
| 1072 | DEFINE_WAIT(wait); |
| 1073 | |
| 1074 | /* Suspend, wake-up or idle notification in progress. */ |
| 1075 | for (;;) { |
| 1076 | prepare_to_wait(&dev->power.wait_queue, &wait, |
| 1077 | TASK_UNINTERRUPTIBLE); |
| 1078 | if (dev->power.runtime_status != RPM_SUSPENDING |
| 1079 | && dev->power.runtime_status != RPM_RESUMING |
| 1080 | && !dev->power.idle_notification) |
| 1081 | break; |
| 1082 | spin_unlock_irq(&dev->power.lock); |
| 1083 | |
| 1084 | schedule(); |
| 1085 | |
| 1086 | spin_lock_irq(&dev->power.lock); |
| 1087 | } |
| 1088 | finish_wait(&dev->power.wait_queue, &wait); |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | /** |
| 1093 | * pm_runtime_barrier - Flush pending requests and wait for completions. |
| 1094 | * @dev: Device to handle. |
| 1095 | * |
| 1096 | * Prevent the device from being suspended by incrementing its usage counter and |
| 1097 | * if there's a pending resume request for the device, wake the device up. |
| 1098 | * Next, make sure that all pending requests for the device have been flushed |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1099 | * from pm_wq and wait for all runtime PM operations involving the device in |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1100 | * progress to complete. |
| 1101 | * |
| 1102 | * Return value: |
| 1103 | * 1, if there was a resume request pending and the device had to be woken up, |
| 1104 | * 0, otherwise |
| 1105 | */ |
| 1106 | int pm_runtime_barrier(struct device *dev) |
| 1107 | { |
| 1108 | int retval = 0; |
| 1109 | |
| 1110 | pm_runtime_get_noresume(dev); |
| 1111 | spin_lock_irq(&dev->power.lock); |
| 1112 | |
| 1113 | if (dev->power.request_pending |
| 1114 | && dev->power.request == RPM_REQ_RESUME) { |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1115 | rpm_resume(dev, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1116 | retval = 1; |
| 1117 | } |
| 1118 | |
| 1119 | __pm_runtime_barrier(dev); |
| 1120 | |
| 1121 | spin_unlock_irq(&dev->power.lock); |
| 1122 | pm_runtime_put_noidle(dev); |
| 1123 | |
| 1124 | return retval; |
| 1125 | } |
| 1126 | EXPORT_SYMBOL_GPL(pm_runtime_barrier); |
| 1127 | |
| 1128 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1129 | * __pm_runtime_disable - Disable runtime PM of a device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1130 | * @dev: Device to handle. |
| 1131 | * @check_resume: If set, check if there's a resume request for the device. |
| 1132 | * |
| 1133 | * Increment power.disable_depth for the device and if was zero previously, |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1134 | * cancel all pending runtime PM requests for the device and wait for all |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1135 | * operations in progress to complete. The device can be either active or |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1136 | * suspended after its runtime PM has been disabled. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1137 | * |
| 1138 | * If @check_resume is set and there's a resume request pending when |
| 1139 | * __pm_runtime_disable() is called and power.disable_depth is zero, the |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1140 | * function will wake up the device before disabling its runtime PM. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1141 | */ |
| 1142 | void __pm_runtime_disable(struct device *dev, bool check_resume) |
| 1143 | { |
| 1144 | spin_lock_irq(&dev->power.lock); |
| 1145 | |
| 1146 | if (dev->power.disable_depth > 0) { |
| 1147 | dev->power.disable_depth++; |
| 1148 | goto out; |
| 1149 | } |
| 1150 | |
| 1151 | /* |
| 1152 | * Wake up the device if there's a resume request pending, because that |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1153 | * means there probably is some I/O to process and disabling runtime PM |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1154 | * shouldn't prevent the device from processing the I/O. |
| 1155 | */ |
| 1156 | if (check_resume && dev->power.request_pending |
| 1157 | && dev->power.request == RPM_REQ_RESUME) { |
| 1158 | /* |
| 1159 | * Prevent suspends and idle notifications from being carried |
| 1160 | * out after we have woken up the device. |
| 1161 | */ |
| 1162 | pm_runtime_get_noresume(dev); |
| 1163 | |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1164 | rpm_resume(dev, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1165 | |
| 1166 | pm_runtime_put_noidle(dev); |
| 1167 | } |
| 1168 | |
| 1169 | if (!dev->power.disable_depth++) |
| 1170 | __pm_runtime_barrier(dev); |
| 1171 | |
| 1172 | out: |
| 1173 | spin_unlock_irq(&dev->power.lock); |
| 1174 | } |
| 1175 | EXPORT_SYMBOL_GPL(__pm_runtime_disable); |
| 1176 | |
| 1177 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1178 | * pm_runtime_enable - Enable runtime PM of a device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1179 | * @dev: Device to handle. |
| 1180 | */ |
| 1181 | void pm_runtime_enable(struct device *dev) |
| 1182 | { |
| 1183 | unsigned long flags; |
| 1184 | |
| 1185 | spin_lock_irqsave(&dev->power.lock, flags); |
| 1186 | |
| 1187 | if (dev->power.disable_depth > 0) |
| 1188 | dev->power.disable_depth--; |
| 1189 | else |
| 1190 | dev_warn(dev, "Unbalanced %s!\n", __func__); |
| 1191 | |
| 1192 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 1193 | } |
| 1194 | EXPORT_SYMBOL_GPL(pm_runtime_enable); |
| 1195 | |
| 1196 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1197 | * pm_runtime_forbid - Block runtime PM of a device. |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1198 | * @dev: Device to handle. |
| 1199 | * |
| 1200 | * Increase the device's usage count and clear its power.runtime_auto flag, |
| 1201 | * so that it cannot be suspended at run time until pm_runtime_allow() is called |
| 1202 | * for it. |
| 1203 | */ |
| 1204 | void pm_runtime_forbid(struct device *dev) |
| 1205 | { |
| 1206 | spin_lock_irq(&dev->power.lock); |
| 1207 | if (!dev->power.runtime_auto) |
| 1208 | goto out; |
| 1209 | |
| 1210 | dev->power.runtime_auto = false; |
| 1211 | atomic_inc(&dev->power.usage_count); |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1212 | rpm_resume(dev, 0); |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1213 | |
| 1214 | out: |
| 1215 | spin_unlock_irq(&dev->power.lock); |
| 1216 | } |
| 1217 | EXPORT_SYMBOL_GPL(pm_runtime_forbid); |
| 1218 | |
| 1219 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1220 | * pm_runtime_allow - Unblock runtime PM of a device. |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1221 | * @dev: Device to handle. |
| 1222 | * |
| 1223 | * Decrease the device's usage count and set its power.runtime_auto flag. |
| 1224 | */ |
| 1225 | void pm_runtime_allow(struct device *dev) |
| 1226 | { |
| 1227 | spin_lock_irq(&dev->power.lock); |
| 1228 | if (dev->power.runtime_auto) |
| 1229 | goto out; |
| 1230 | |
| 1231 | dev->power.runtime_auto = true; |
| 1232 | if (atomic_dec_and_test(&dev->power.usage_count)) |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 1233 | rpm_idle(dev, RPM_AUTO); |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1234 | |
| 1235 | out: |
| 1236 | spin_unlock_irq(&dev->power.lock); |
| 1237 | } |
| 1238 | EXPORT_SYMBOL_GPL(pm_runtime_allow); |
| 1239 | |
| 1240 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1241 | * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device. |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 1242 | * @dev: Device to handle. |
| 1243 | * |
| 1244 | * Set the power.no_callbacks flag, which tells the PM core that this |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1245 | * device is power-managed through its parent and has no runtime PM |
| 1246 | * callbacks of its own. The runtime sysfs attributes will be removed. |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 1247 | */ |
| 1248 | void pm_runtime_no_callbacks(struct device *dev) |
| 1249 | { |
| 1250 | spin_lock_irq(&dev->power.lock); |
| 1251 | dev->power.no_callbacks = 1; |
| 1252 | spin_unlock_irq(&dev->power.lock); |
| 1253 | if (device_is_registered(dev)) |
| 1254 | rpm_sysfs_remove(dev); |
| 1255 | } |
| 1256 | EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks); |
| 1257 | |
| 1258 | /** |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 1259 | * pm_runtime_irq_safe - Leave interrupts disabled during callbacks. |
| 1260 | * @dev: Device to handle |
| 1261 | * |
| 1262 | * Set the power.irq_safe flag, which tells the PM core that the |
| 1263 | * ->runtime_suspend() and ->runtime_resume() callbacks for this device should |
| 1264 | * always be invoked with the spinlock held and interrupts disabled. It also |
| 1265 | * causes the parent's usage counter to be permanently incremented, preventing |
| 1266 | * the parent from runtime suspending -- otherwise an irq-safe child might have |
| 1267 | * to wait for a non-irq-safe parent. |
| 1268 | */ |
| 1269 | void pm_runtime_irq_safe(struct device *dev) |
| 1270 | { |
| 1271 | if (dev->parent) |
| 1272 | pm_runtime_get_sync(dev->parent); |
| 1273 | spin_lock_irq(&dev->power.lock); |
| 1274 | dev->power.irq_safe = 1; |
| 1275 | spin_unlock_irq(&dev->power.lock); |
| 1276 | } |
| 1277 | EXPORT_SYMBOL_GPL(pm_runtime_irq_safe); |
| 1278 | |
| 1279 | /** |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 1280 | * update_autosuspend - Handle a change to a device's autosuspend settings. |
| 1281 | * @dev: Device to handle. |
| 1282 | * @old_delay: The former autosuspend_delay value. |
| 1283 | * @old_use: The former use_autosuspend value. |
| 1284 | * |
| 1285 | * Prevent runtime suspend if the new delay is negative and use_autosuspend is |
| 1286 | * set; otherwise allow it. Send an idle notification if suspends are allowed. |
| 1287 | * |
| 1288 | * This function must be called under dev->power.lock with interrupts disabled. |
| 1289 | */ |
| 1290 | static void update_autosuspend(struct device *dev, int old_delay, int old_use) |
| 1291 | { |
| 1292 | int delay = dev->power.autosuspend_delay; |
| 1293 | |
| 1294 | /* Should runtime suspend be prevented now? */ |
| 1295 | if (dev->power.use_autosuspend && delay < 0) { |
| 1296 | |
| 1297 | /* If it used to be allowed then prevent it. */ |
| 1298 | if (!old_use || old_delay >= 0) { |
| 1299 | atomic_inc(&dev->power.usage_count); |
| 1300 | rpm_resume(dev, 0); |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | /* Runtime suspend should be allowed now. */ |
| 1305 | else { |
| 1306 | |
| 1307 | /* If it used to be prevented then allow it. */ |
| 1308 | if (old_use && old_delay < 0) |
| 1309 | atomic_dec(&dev->power.usage_count); |
| 1310 | |
| 1311 | /* Maybe we can autosuspend now. */ |
| 1312 | rpm_idle(dev, RPM_AUTO); |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /** |
| 1317 | * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value. |
| 1318 | * @dev: Device to handle. |
| 1319 | * @delay: Value of the new delay in milliseconds. |
| 1320 | * |
| 1321 | * Set the device's power.autosuspend_delay value. If it changes to negative |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1322 | * and the power.use_autosuspend flag is set, prevent runtime suspends. If it |
| 1323 | * changes the other way, allow runtime suspends. |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 1324 | */ |
| 1325 | void pm_runtime_set_autosuspend_delay(struct device *dev, int delay) |
| 1326 | { |
| 1327 | int old_delay, old_use; |
| 1328 | |
| 1329 | spin_lock_irq(&dev->power.lock); |
| 1330 | old_delay = dev->power.autosuspend_delay; |
| 1331 | old_use = dev->power.use_autosuspend; |
| 1332 | dev->power.autosuspend_delay = delay; |
| 1333 | update_autosuspend(dev, old_delay, old_use); |
| 1334 | spin_unlock_irq(&dev->power.lock); |
| 1335 | } |
| 1336 | EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay); |
| 1337 | |
| 1338 | /** |
| 1339 | * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag. |
| 1340 | * @dev: Device to handle. |
| 1341 | * @use: New value for use_autosuspend. |
| 1342 | * |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1343 | * Set the device's power.use_autosuspend flag, and allow or prevent runtime |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 1344 | * suspends as needed. |
| 1345 | */ |
| 1346 | void __pm_runtime_use_autosuspend(struct device *dev, bool use) |
| 1347 | { |
| 1348 | int old_delay, old_use; |
| 1349 | |
| 1350 | spin_lock_irq(&dev->power.lock); |
| 1351 | old_delay = dev->power.autosuspend_delay; |
| 1352 | old_use = dev->power.use_autosuspend; |
| 1353 | dev->power.use_autosuspend = use; |
| 1354 | update_autosuspend(dev, old_delay, old_use); |
| 1355 | spin_unlock_irq(&dev->power.lock); |
| 1356 | } |
| 1357 | EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend); |
| 1358 | |
| 1359 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1360 | * pm_runtime_init - Initialize runtime PM fields in given device object. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1361 | * @dev: Device object to initialize. |
| 1362 | */ |
| 1363 | void pm_runtime_init(struct device *dev) |
| 1364 | { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1365 | dev->power.runtime_status = RPM_SUSPENDED; |
| 1366 | dev->power.idle_notification = false; |
| 1367 | |
| 1368 | dev->power.disable_depth = 1; |
| 1369 | atomic_set(&dev->power.usage_count, 0); |
| 1370 | |
| 1371 | dev->power.runtime_error = 0; |
| 1372 | |
| 1373 | atomic_set(&dev->power.child_count, 0); |
| 1374 | pm_suspend_ignore_children(dev, false); |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1375 | dev->power.runtime_auto = true; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1376 | |
| 1377 | dev->power.request_pending = false; |
| 1378 | dev->power.request = RPM_REQ_NONE; |
| 1379 | dev->power.deferred_resume = false; |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 1380 | dev->power.accounting_timestamp = jiffies; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1381 | INIT_WORK(&dev->power.work, pm_runtime_work); |
| 1382 | |
| 1383 | dev->power.timer_expires = 0; |
| 1384 | setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn, |
| 1385 | (unsigned long)dev); |
| 1386 | |
| 1387 | init_waitqueue_head(&dev->power.wait_queue); |
| 1388 | } |
| 1389 | |
| 1390 | /** |
| 1391 | * pm_runtime_remove - Prepare for removing a device from device hierarchy. |
| 1392 | * @dev: Device object being removed from device hierarchy. |
| 1393 | */ |
| 1394 | void pm_runtime_remove(struct device *dev) |
| 1395 | { |
| 1396 | __pm_runtime_disable(dev, false); |
| 1397 | |
| 1398 | /* Change the status back to 'suspended' to match the initial status. */ |
| 1399 | if (dev->power.runtime_status == RPM_ACTIVE) |
| 1400 | pm_runtime_set_suspended(dev); |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 1401 | if (dev->power.irq_safe && dev->parent) |
Ulf Hansson | db28dfa | 2013-04-12 09:41:30 +0000 | [diff] [blame] | 1402 | pm_runtime_put(dev->parent); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1403 | } |