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