Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/power/wakeup.c - System wakeup events framework |
| 3 | * |
| 4 | * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. |
| 5 | * |
| 6 | * This file is released under the GPLv2. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/capability.h> |
Paul Gortmaker | 1b6bc32 | 2011-05-27 07:12:15 -0400 | [diff] [blame] | 13 | #include <linux/export.h> |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 14 | #include <linux/suspend.h> |
Rafael J. Wysocki | 9c03439 | 2010-10-19 23:42:49 +0200 | [diff] [blame] | 15 | #include <linux/seq_file.h> |
| 16 | #include <linux/debugfs.h> |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 17 | |
| 18 | #include "power.h" |
| 19 | |
| 20 | #define TIMEOUT 100 |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * If set, the suspend/hibernate code will abort transitions to a sleep state |
| 24 | * if wakeup events are registered during or immediately before the transition. |
| 25 | */ |
| 26 | bool events_check_enabled; |
| 27 | |
Rafael J. Wysocki | 023d377 | 2011-01-31 11:06:39 +0100 | [diff] [blame] | 28 | /* |
| 29 | * Combined counters of registered wakeup events and wakeup events in progress. |
| 30 | * They need to be modified together atomically, so it's better to use one |
| 31 | * atomic variable to hold them both. |
| 32 | */ |
| 33 | static atomic_t combined_event_count = ATOMIC_INIT(0); |
| 34 | |
| 35 | #define IN_PROGRESS_BITS (sizeof(int) * 4) |
| 36 | #define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1) |
| 37 | |
| 38 | static void split_counters(unsigned int *cnt, unsigned int *inpr) |
| 39 | { |
| 40 | unsigned int comb = atomic_read(&combined_event_count); |
| 41 | |
| 42 | *cnt = (comb >> IN_PROGRESS_BITS); |
| 43 | *inpr = comb & MAX_IN_PROGRESS; |
| 44 | } |
| 45 | |
| 46 | /* A preserved old value of the events counter. */ |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 47 | static unsigned int saved_count; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 48 | |
| 49 | static DEFINE_SPINLOCK(events_lock); |
| 50 | |
Rafael J. Wysocki | 4eb241e | 2010-07-07 23:43:51 +0200 | [diff] [blame] | 51 | static void pm_wakeup_timer_fn(unsigned long data); |
| 52 | |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 53 | static LIST_HEAD(wakeup_sources); |
| 54 | |
| 55 | /** |
| 56 | * wakeup_source_create - Create a struct wakeup_source object. |
| 57 | * @name: Name of the new wakeup source. |
| 58 | */ |
| 59 | struct wakeup_source *wakeup_source_create(const char *name) |
| 60 | { |
| 61 | struct wakeup_source *ws; |
| 62 | |
| 63 | ws = kzalloc(sizeof(*ws), GFP_KERNEL); |
| 64 | if (!ws) |
| 65 | return NULL; |
| 66 | |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 67 | if (name) |
| 68 | ws->name = kstrdup(name, GFP_KERNEL); |
| 69 | |
| 70 | return ws; |
| 71 | } |
| 72 | EXPORT_SYMBOL_GPL(wakeup_source_create); |
| 73 | |
| 74 | /** |
| 75 | * wakeup_source_destroy - Destroy a struct wakeup_source object. |
| 76 | * @ws: Wakeup source to destroy. |
Rafael J. Wysocki | d94aff8 | 2012-02-17 23:39:20 +0100 | [diff] [blame] | 77 | * |
| 78 | * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never |
| 79 | * be run in parallel with this function for the same wakeup source object. |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 80 | */ |
| 81 | void wakeup_source_destroy(struct wakeup_source *ws) |
| 82 | { |
| 83 | if (!ws) |
| 84 | return; |
| 85 | |
Rafael J. Wysocki | d94aff8 | 2012-02-17 23:39:20 +0100 | [diff] [blame] | 86 | del_timer_sync(&ws->timer); |
| 87 | __pm_relax(ws); |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 88 | kfree(ws->name); |
| 89 | kfree(ws); |
| 90 | } |
| 91 | EXPORT_SYMBOL_GPL(wakeup_source_destroy); |
| 92 | |
| 93 | /** |
| 94 | * wakeup_source_add - Add given object to the list of wakeup sources. |
| 95 | * @ws: Wakeup source object to add to the list. |
| 96 | */ |
| 97 | void wakeup_source_add(struct wakeup_source *ws) |
| 98 | { |
| 99 | if (WARN_ON(!ws)) |
| 100 | return; |
| 101 | |
Rafael J. Wysocki | 7c95149 | 2012-02-11 00:00:11 +0100 | [diff] [blame] | 102 | spin_lock_init(&ws->lock); |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 103 | setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws); |
| 104 | ws->active = false; |
| 105 | |
| 106 | spin_lock_irq(&events_lock); |
| 107 | list_add_rcu(&ws->entry, &wakeup_sources); |
| 108 | spin_unlock_irq(&events_lock); |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 109 | } |
| 110 | EXPORT_SYMBOL_GPL(wakeup_source_add); |
| 111 | |
| 112 | /** |
| 113 | * wakeup_source_remove - Remove given object from the wakeup sources list. |
| 114 | * @ws: Wakeup source object to remove from the list. |
| 115 | */ |
| 116 | void wakeup_source_remove(struct wakeup_source *ws) |
| 117 | { |
| 118 | if (WARN_ON(!ws)) |
| 119 | return; |
| 120 | |
| 121 | spin_lock_irq(&events_lock); |
| 122 | list_del_rcu(&ws->entry); |
| 123 | spin_unlock_irq(&events_lock); |
| 124 | synchronize_rcu(); |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(wakeup_source_remove); |
| 127 | |
| 128 | /** |
| 129 | * wakeup_source_register - Create wakeup source and add it to the list. |
| 130 | * @name: Name of the wakeup source to register. |
| 131 | */ |
| 132 | struct wakeup_source *wakeup_source_register(const char *name) |
| 133 | { |
| 134 | struct wakeup_source *ws; |
| 135 | |
| 136 | ws = wakeup_source_create(name); |
| 137 | if (ws) |
| 138 | wakeup_source_add(ws); |
| 139 | |
| 140 | return ws; |
| 141 | } |
| 142 | EXPORT_SYMBOL_GPL(wakeup_source_register); |
| 143 | |
| 144 | /** |
| 145 | * wakeup_source_unregister - Remove wakeup source from the list and remove it. |
| 146 | * @ws: Wakeup source object to unregister. |
| 147 | */ |
| 148 | void wakeup_source_unregister(struct wakeup_source *ws) |
| 149 | { |
| 150 | wakeup_source_remove(ws); |
| 151 | wakeup_source_destroy(ws); |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(wakeup_source_unregister); |
| 154 | |
| 155 | /** |
| 156 | * device_wakeup_attach - Attach a wakeup source object to a device object. |
| 157 | * @dev: Device to handle. |
| 158 | * @ws: Wakeup source object to attach to @dev. |
| 159 | * |
| 160 | * This causes @dev to be treated as a wakeup device. |
| 161 | */ |
| 162 | static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws) |
| 163 | { |
| 164 | spin_lock_irq(&dev->power.lock); |
| 165 | if (dev->power.wakeup) { |
| 166 | spin_unlock_irq(&dev->power.lock); |
| 167 | return -EEXIST; |
| 168 | } |
| 169 | dev->power.wakeup = ws; |
| 170 | spin_unlock_irq(&dev->power.lock); |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * device_wakeup_enable - Enable given device to be a wakeup source. |
| 176 | * @dev: Device to handle. |
| 177 | * |
| 178 | * Create a wakeup source object, register it and attach it to @dev. |
| 179 | */ |
| 180 | int device_wakeup_enable(struct device *dev) |
| 181 | { |
| 182 | struct wakeup_source *ws; |
| 183 | int ret; |
| 184 | |
| 185 | if (!dev || !dev->power.can_wakeup) |
| 186 | return -EINVAL; |
| 187 | |
| 188 | ws = wakeup_source_register(dev_name(dev)); |
| 189 | if (!ws) |
| 190 | return -ENOMEM; |
| 191 | |
| 192 | ret = device_wakeup_attach(dev, ws); |
| 193 | if (ret) |
| 194 | wakeup_source_unregister(ws); |
| 195 | |
| 196 | return ret; |
| 197 | } |
| 198 | EXPORT_SYMBOL_GPL(device_wakeup_enable); |
| 199 | |
| 200 | /** |
| 201 | * device_wakeup_detach - Detach a device's wakeup source object from it. |
| 202 | * @dev: Device to detach the wakeup source object from. |
| 203 | * |
| 204 | * After it returns, @dev will not be treated as a wakeup device any more. |
| 205 | */ |
| 206 | static struct wakeup_source *device_wakeup_detach(struct device *dev) |
| 207 | { |
| 208 | struct wakeup_source *ws; |
| 209 | |
| 210 | spin_lock_irq(&dev->power.lock); |
| 211 | ws = dev->power.wakeup; |
| 212 | dev->power.wakeup = NULL; |
| 213 | spin_unlock_irq(&dev->power.lock); |
| 214 | return ws; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * device_wakeup_disable - Do not regard a device as a wakeup source any more. |
| 219 | * @dev: Device to handle. |
| 220 | * |
| 221 | * Detach the @dev's wakeup source object from it, unregister this wakeup source |
| 222 | * object and destroy it. |
| 223 | */ |
| 224 | int device_wakeup_disable(struct device *dev) |
| 225 | { |
| 226 | struct wakeup_source *ws; |
| 227 | |
| 228 | if (!dev || !dev->power.can_wakeup) |
| 229 | return -EINVAL; |
| 230 | |
| 231 | ws = device_wakeup_detach(dev); |
| 232 | if (ws) |
| 233 | wakeup_source_unregister(ws); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | EXPORT_SYMBOL_GPL(device_wakeup_disable); |
| 238 | |
| 239 | /** |
Rafael J. Wysocki | cb8f51b | 2011-02-08 23:26:02 +0100 | [diff] [blame] | 240 | * device_set_wakeup_capable - Set/reset device wakeup capability flag. |
| 241 | * @dev: Device to handle. |
| 242 | * @capable: Whether or not @dev is capable of waking up the system from sleep. |
| 243 | * |
| 244 | * If @capable is set, set the @dev's power.can_wakeup flag and add its |
| 245 | * wakeup-related attributes to sysfs. Otherwise, unset the @dev's |
| 246 | * power.can_wakeup flag and remove its wakeup-related attributes from sysfs. |
| 247 | * |
| 248 | * This function may sleep and it can't be called from any context where |
| 249 | * sleeping is not allowed. |
| 250 | */ |
| 251 | void device_set_wakeup_capable(struct device *dev, bool capable) |
| 252 | { |
| 253 | if (!!dev->power.can_wakeup == !!capable) |
| 254 | return; |
| 255 | |
Rafael J. Wysocki | 22110fa | 2011-04-26 11:33:09 +0200 | [diff] [blame] | 256 | if (device_is_registered(dev) && !list_empty(&dev->power.entry)) { |
Rafael J. Wysocki | cb8f51b | 2011-02-08 23:26:02 +0100 | [diff] [blame] | 257 | if (capable) { |
| 258 | if (wakeup_sysfs_add(dev)) |
| 259 | return; |
| 260 | } else { |
| 261 | wakeup_sysfs_remove(dev); |
| 262 | } |
| 263 | } |
| 264 | dev->power.can_wakeup = capable; |
| 265 | } |
| 266 | EXPORT_SYMBOL_GPL(device_set_wakeup_capable); |
| 267 | |
| 268 | /** |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 269 | * device_init_wakeup - Device wakeup initialization. |
| 270 | * @dev: Device to handle. |
| 271 | * @enable: Whether or not to enable @dev as a wakeup device. |
| 272 | * |
| 273 | * By default, most devices should leave wakeup disabled. The exceptions are |
| 274 | * devices that everyone expects to be wakeup sources: keyboards, power buttons, |
Alan Stern | 8f88893 | 2011-09-26 17:38:50 +0200 | [diff] [blame] | 275 | * possibly network interfaces, etc. Also, devices that don't generate their |
| 276 | * own wakeup requests but merely forward requests from one bus to another |
| 277 | * (like PCI bridges) should have wakeup enabled by default. |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 278 | */ |
| 279 | int device_init_wakeup(struct device *dev, bool enable) |
| 280 | { |
| 281 | int ret = 0; |
| 282 | |
| 283 | if (enable) { |
| 284 | device_set_wakeup_capable(dev, true); |
| 285 | ret = device_wakeup_enable(dev); |
| 286 | } else { |
| 287 | device_set_wakeup_capable(dev, false); |
| 288 | } |
| 289 | |
| 290 | return ret; |
| 291 | } |
| 292 | EXPORT_SYMBOL_GPL(device_init_wakeup); |
| 293 | |
| 294 | /** |
| 295 | * device_set_wakeup_enable - Enable or disable a device to wake up the system. |
| 296 | * @dev: Device to handle. |
| 297 | */ |
| 298 | int device_set_wakeup_enable(struct device *dev, bool enable) |
| 299 | { |
| 300 | if (!dev || !dev->power.can_wakeup) |
| 301 | return -EINVAL; |
| 302 | |
| 303 | return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev); |
| 304 | } |
| 305 | EXPORT_SYMBOL_GPL(device_set_wakeup_enable); |
Rafael J. Wysocki | 4eb241e | 2010-07-07 23:43:51 +0200 | [diff] [blame] | 306 | |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 307 | /* |
| 308 | * The functions below use the observation that each wakeup event starts a |
| 309 | * period in which the system should not be suspended. The moment this period |
| 310 | * will end depends on how the wakeup event is going to be processed after being |
| 311 | * detected and all of the possible cases can be divided into two distinct |
| 312 | * groups. |
| 313 | * |
| 314 | * First, a wakeup event may be detected by the same functional unit that will |
| 315 | * carry out the entire processing of it and possibly will pass it to user space |
| 316 | * for further processing. In that case the functional unit that has detected |
| 317 | * the event may later "close" the "no suspend" period associated with it |
| 318 | * directly as soon as it has been dealt with. The pair of pm_stay_awake() and |
| 319 | * pm_relax(), balanced with each other, is supposed to be used in such |
| 320 | * situations. |
| 321 | * |
| 322 | * Second, a wakeup event may be detected by one functional unit and processed |
| 323 | * by another one. In that case the unit that has detected it cannot really |
| 324 | * "close" the "no suspend" period associated with it, unless it knows in |
| 325 | * advance what's going to happen to the event during processing. This |
| 326 | * knowledge, however, may not be available to it, so it can simply specify time |
| 327 | * to wait before the system can be suspended and pass it as the second |
| 328 | * argument of pm_wakeup_event(). |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 329 | * |
| 330 | * It is valid to call pm_relax() after pm_wakeup_event(), in which case the |
| 331 | * "no suspend" period will be ended either by the pm_relax(), or by the timer |
| 332 | * function executed when the timer expires, whichever comes first. |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 333 | */ |
| 334 | |
| 335 | /** |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 336 | * wakup_source_activate - Mark given wakeup source as active. |
| 337 | * @ws: Wakeup source to handle. |
| 338 | * |
| 339 | * Update the @ws' statistics and, if @ws has just been activated, notify the PM |
| 340 | * core of the event by incrementing the counter of of wakeup events being |
| 341 | * processed. |
| 342 | */ |
| 343 | static void wakeup_source_activate(struct wakeup_source *ws) |
| 344 | { |
| 345 | ws->active = true; |
| 346 | ws->active_count++; |
| 347 | ws->timer_expires = jiffies; |
| 348 | ws->last_time = ktime_get(); |
| 349 | |
Rafael J. Wysocki | 023d377 | 2011-01-31 11:06:39 +0100 | [diff] [blame] | 350 | /* Increment the counter of events in progress. */ |
| 351 | atomic_inc(&combined_event_count); |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
| 355 | * __pm_stay_awake - Notify the PM core of a wakeup event. |
| 356 | * @ws: Wakeup source object associated with the source of the event. |
| 357 | * |
| 358 | * It is safe to call this function from interrupt context. |
| 359 | */ |
| 360 | void __pm_stay_awake(struct wakeup_source *ws) |
| 361 | { |
| 362 | unsigned long flags; |
| 363 | |
| 364 | if (!ws) |
| 365 | return; |
| 366 | |
| 367 | spin_lock_irqsave(&ws->lock, flags); |
| 368 | ws->event_count++; |
| 369 | if (!ws->active) |
| 370 | wakeup_source_activate(ws); |
| 371 | spin_unlock_irqrestore(&ws->lock, flags); |
| 372 | } |
| 373 | EXPORT_SYMBOL_GPL(__pm_stay_awake); |
| 374 | |
| 375 | /** |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 376 | * pm_stay_awake - Notify the PM core that a wakeup event is being processed. |
| 377 | * @dev: Device the wakeup event is related to. |
| 378 | * |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 379 | * Notify the PM core of a wakeup event (signaled by @dev) by calling |
| 380 | * __pm_stay_awake for the @dev's wakeup source object. |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 381 | * |
| 382 | * Call this function after detecting of a wakeup event if pm_relax() is going |
| 383 | * to be called directly after processing the event (and possibly passing it to |
| 384 | * user space for further processing). |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 385 | */ |
| 386 | void pm_stay_awake(struct device *dev) |
| 387 | { |
| 388 | unsigned long flags; |
| 389 | |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 390 | if (!dev) |
| 391 | return; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 392 | |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 393 | spin_lock_irqsave(&dev->power.lock, flags); |
| 394 | __pm_stay_awake(dev->power.wakeup); |
| 395 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 396 | } |
| 397 | EXPORT_SYMBOL_GPL(pm_stay_awake); |
| 398 | |
| 399 | /** |
| 400 | * wakup_source_deactivate - Mark given wakeup source as inactive. |
| 401 | * @ws: Wakeup source to handle. |
| 402 | * |
| 403 | * Update the @ws' statistics and notify the PM core that the wakeup source has |
| 404 | * become inactive by decrementing the counter of wakeup events being processed |
| 405 | * and incrementing the counter of registered wakeup events. |
| 406 | */ |
| 407 | static void wakeup_source_deactivate(struct wakeup_source *ws) |
| 408 | { |
| 409 | ktime_t duration; |
| 410 | ktime_t now; |
| 411 | |
| 412 | ws->relax_count++; |
| 413 | /* |
| 414 | * __pm_relax() may be called directly or from a timer function. |
| 415 | * If it is called directly right after the timer function has been |
| 416 | * started, but before the timer function calls __pm_relax(), it is |
| 417 | * possible that __pm_stay_awake() will be called in the meantime and |
| 418 | * will set ws->active. Then, ws->active may be cleared immediately |
| 419 | * by the __pm_relax() called from the timer function, but in such a |
| 420 | * case ws->relax_count will be different from ws->active_count. |
| 421 | */ |
| 422 | if (ws->relax_count != ws->active_count) { |
| 423 | ws->relax_count--; |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | ws->active = false; |
| 428 | |
| 429 | now = ktime_get(); |
| 430 | duration = ktime_sub(now, ws->last_time); |
| 431 | ws->total_time = ktime_add(ws->total_time, duration); |
| 432 | if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time)) |
| 433 | ws->max_time = duration; |
| 434 | |
| 435 | del_timer(&ws->timer); |
Rafael J. Wysocki | da863cd | 2012-02-17 23:39:33 +0100 | [diff] [blame^] | 436 | ws->timer_expires = 0; |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 437 | |
| 438 | /* |
Rafael J. Wysocki | 023d377 | 2011-01-31 11:06:39 +0100 | [diff] [blame] | 439 | * Increment the counter of registered wakeup events and decrement the |
| 440 | * couter of wakeup events in progress simultaneously. |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 441 | */ |
Rafael J. Wysocki | 023d377 | 2011-01-31 11:06:39 +0100 | [diff] [blame] | 442 | atomic_add(MAX_IN_PROGRESS, &combined_event_count); |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /** |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 446 | * __pm_relax - Notify the PM core that processing of a wakeup event has ended. |
| 447 | * @ws: Wakeup source object associated with the source of the event. |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 448 | * |
| 449 | * Call this function for wakeup events whose processing started with calling |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 450 | * __pm_stay_awake(). |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 451 | * |
| 452 | * It is safe to call it from interrupt context. |
| 453 | */ |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 454 | void __pm_relax(struct wakeup_source *ws) |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 455 | { |
| 456 | unsigned long flags; |
| 457 | |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 458 | if (!ws) |
| 459 | return; |
| 460 | |
| 461 | spin_lock_irqsave(&ws->lock, flags); |
| 462 | if (ws->active) |
| 463 | wakeup_source_deactivate(ws); |
| 464 | spin_unlock_irqrestore(&ws->lock, flags); |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 465 | } |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 466 | EXPORT_SYMBOL_GPL(__pm_relax); |
| 467 | |
| 468 | /** |
| 469 | * pm_relax - Notify the PM core that processing of a wakeup event has ended. |
| 470 | * @dev: Device that signaled the event. |
| 471 | * |
| 472 | * Execute __pm_relax() for the @dev's wakeup source object. |
| 473 | */ |
| 474 | void pm_relax(struct device *dev) |
| 475 | { |
| 476 | unsigned long flags; |
| 477 | |
| 478 | if (!dev) |
| 479 | return; |
| 480 | |
| 481 | spin_lock_irqsave(&dev->power.lock, flags); |
| 482 | __pm_relax(dev->power.wakeup); |
| 483 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 484 | } |
| 485 | EXPORT_SYMBOL_GPL(pm_relax); |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 486 | |
| 487 | /** |
Rafael J. Wysocki | 4eb241e | 2010-07-07 23:43:51 +0200 | [diff] [blame] | 488 | * pm_wakeup_timer_fn - Delayed finalization of a wakeup event. |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 489 | * @data: Address of the wakeup source object associated with the event source. |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 490 | * |
Rafael J. Wysocki | da863cd | 2012-02-17 23:39:33 +0100 | [diff] [blame^] | 491 | * Call wakeup_source_deactivate() for the wakeup source whose address is stored |
| 492 | * in @data if it is currently active and its timer has not been canceled and |
| 493 | * the expiration time of the timer is not in future. |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 494 | */ |
Rafael J. Wysocki | 4eb241e | 2010-07-07 23:43:51 +0200 | [diff] [blame] | 495 | static void pm_wakeup_timer_fn(unsigned long data) |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 496 | { |
Rafael J. Wysocki | da863cd | 2012-02-17 23:39:33 +0100 | [diff] [blame^] | 497 | struct wakeup_source *ws = (struct wakeup_source *)data; |
| 498 | unsigned long flags; |
| 499 | |
| 500 | spin_lock_irqsave(&ws->lock, flags); |
| 501 | |
| 502 | if (ws->active && ws->timer_expires |
| 503 | && time_after_eq(jiffies, ws->timer_expires)) |
| 504 | wakeup_source_deactivate(ws); |
| 505 | |
| 506 | spin_unlock_irqrestore(&ws->lock, flags); |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /** |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 510 | * __pm_wakeup_event - Notify the PM core of a wakeup event. |
| 511 | * @ws: Wakeup source object associated with the event source. |
| 512 | * @msec: Anticipated event processing time (in milliseconds). |
| 513 | * |
| 514 | * Notify the PM core of a wakeup event whose source is @ws that will take |
| 515 | * approximately @msec milliseconds to be processed by the kernel. If @ws is |
| 516 | * not active, activate it. If @msec is nonzero, set up the @ws' timer to |
| 517 | * execute pm_wakeup_timer_fn() in future. |
| 518 | * |
| 519 | * It is safe to call this function from interrupt context. |
| 520 | */ |
| 521 | void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec) |
| 522 | { |
| 523 | unsigned long flags; |
| 524 | unsigned long expires; |
| 525 | |
| 526 | if (!ws) |
| 527 | return; |
| 528 | |
| 529 | spin_lock_irqsave(&ws->lock, flags); |
| 530 | |
| 531 | ws->event_count++; |
| 532 | if (!ws->active) |
| 533 | wakeup_source_activate(ws); |
| 534 | |
| 535 | if (!msec) { |
| 536 | wakeup_source_deactivate(ws); |
| 537 | goto unlock; |
| 538 | } |
| 539 | |
| 540 | expires = jiffies + msecs_to_jiffies(msec); |
| 541 | if (!expires) |
| 542 | expires = 1; |
| 543 | |
| 544 | if (time_after(expires, ws->timer_expires)) { |
| 545 | mod_timer(&ws->timer, expires); |
| 546 | ws->timer_expires = expires; |
| 547 | } |
| 548 | |
| 549 | unlock: |
| 550 | spin_unlock_irqrestore(&ws->lock, flags); |
| 551 | } |
| 552 | EXPORT_SYMBOL_GPL(__pm_wakeup_event); |
| 553 | |
| 554 | |
| 555 | /** |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 556 | * pm_wakeup_event - Notify the PM core of a wakeup event. |
| 557 | * @dev: Device the wakeup event is related to. |
| 558 | * @msec: Anticipated event processing time (in milliseconds). |
| 559 | * |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 560 | * Call __pm_wakeup_event() for the @dev's wakeup source object. |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 561 | */ |
| 562 | void pm_wakeup_event(struct device *dev, unsigned int msec) |
| 563 | { |
| 564 | unsigned long flags; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 565 | |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 566 | if (!dev) |
| 567 | return; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 568 | |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 569 | spin_lock_irqsave(&dev->power.lock, flags); |
| 570 | __pm_wakeup_event(dev->power.wakeup, msec); |
| 571 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 572 | } |
| 573 | EXPORT_SYMBOL_GPL(pm_wakeup_event); |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 574 | |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 575 | /** |
| 576 | * pm_wakeup_update_hit_counts - Update hit counts of all active wakeup sources. |
| 577 | */ |
| 578 | static void pm_wakeup_update_hit_counts(void) |
| 579 | { |
| 580 | unsigned long flags; |
| 581 | struct wakeup_source *ws; |
Rafael J. Wysocki | 4eb241e | 2010-07-07 23:43:51 +0200 | [diff] [blame] | 582 | |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 583 | rcu_read_lock(); |
| 584 | list_for_each_entry_rcu(ws, &wakeup_sources, entry) { |
| 585 | spin_lock_irqsave(&ws->lock, flags); |
| 586 | if (ws->active) |
| 587 | ws->hit_count++; |
| 588 | spin_unlock_irqrestore(&ws->lock, flags); |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 589 | } |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 590 | rcu_read_unlock(); |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /** |
Rafael J. Wysocki | a2867e0 | 2010-12-03 22:58:31 +0100 | [diff] [blame] | 594 | * pm_wakeup_pending - Check if power transition in progress should be aborted. |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 595 | * |
| 596 | * Compare the current number of registered wakeup events with its preserved |
Rafael J. Wysocki | a2867e0 | 2010-12-03 22:58:31 +0100 | [diff] [blame] | 597 | * value from the past and return true if new wakeup events have been registered |
| 598 | * since the old value was stored. Also return true if the current number of |
| 599 | * wakeup events being processed is different from zero. |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 600 | */ |
Rafael J. Wysocki | a2867e0 | 2010-12-03 22:58:31 +0100 | [diff] [blame] | 601 | bool pm_wakeup_pending(void) |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 602 | { |
| 603 | unsigned long flags; |
Rafael J. Wysocki | a2867e0 | 2010-12-03 22:58:31 +0100 | [diff] [blame] | 604 | bool ret = false; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 605 | |
| 606 | spin_lock_irqsave(&events_lock, flags); |
| 607 | if (events_check_enabled) { |
Rafael J. Wysocki | 023d377 | 2011-01-31 11:06:39 +0100 | [diff] [blame] | 608 | unsigned int cnt, inpr; |
| 609 | |
| 610 | split_counters(&cnt, &inpr); |
| 611 | ret = (cnt != saved_count || inpr > 0); |
Rafael J. Wysocki | a2867e0 | 2010-12-03 22:58:31 +0100 | [diff] [blame] | 612 | events_check_enabled = !ret; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 613 | } |
| 614 | spin_unlock_irqrestore(&events_lock, flags); |
Rafael J. Wysocki | a2867e0 | 2010-12-03 22:58:31 +0100 | [diff] [blame] | 615 | if (ret) |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 616 | pm_wakeup_update_hit_counts(); |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * pm_get_wakeup_count - Read the number of registered wakeup events. |
| 622 | * @count: Address to store the value at. |
| 623 | * |
| 624 | * Store the number of registered wakeup events at the address in @count. Block |
| 625 | * if the current number of wakeup events being processed is nonzero. |
| 626 | * |
Rafael J. Wysocki | 790c788 | 2011-01-31 11:07:01 +0100 | [diff] [blame] | 627 | * Return 'false' if the wait for the number of wakeup events being processed to |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 628 | * drop down to zero has been interrupted by a signal (and the current number |
Rafael J. Wysocki | 790c788 | 2011-01-31 11:07:01 +0100 | [diff] [blame] | 629 | * of wakeup events being processed is still nonzero). Otherwise return 'true'. |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 630 | */ |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 631 | bool pm_get_wakeup_count(unsigned int *count) |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 632 | { |
Rafael J. Wysocki | 023d377 | 2011-01-31 11:06:39 +0100 | [diff] [blame] | 633 | unsigned int cnt, inpr; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 634 | |
Rafael J. Wysocki | 023d377 | 2011-01-31 11:06:39 +0100 | [diff] [blame] | 635 | for (;;) { |
| 636 | split_counters(&cnt, &inpr); |
| 637 | if (inpr == 0 || signal_pending(current)) |
| 638 | break; |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 639 | pm_wakeup_update_hit_counts(); |
| 640 | schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT)); |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 641 | } |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 642 | |
Rafael J. Wysocki | 023d377 | 2011-01-31 11:06:39 +0100 | [diff] [blame] | 643 | split_counters(&cnt, &inpr); |
| 644 | *count = cnt; |
| 645 | return !inpr; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /** |
| 649 | * pm_save_wakeup_count - Save the current number of registered wakeup events. |
| 650 | * @count: Value to compare with the current number of registered wakeup events. |
| 651 | * |
| 652 | * If @count is equal to the current number of registered wakeup events and the |
| 653 | * current number of wakeup events being processed is zero, store @count as the |
Rafael J. Wysocki | 378eef9 | 2011-01-31 11:06:50 +0100 | [diff] [blame] | 654 | * old number of registered wakeup events for pm_check_wakeup_events(), enable |
| 655 | * wakeup events detection and return 'true'. Otherwise disable wakeup events |
| 656 | * detection and return 'false'. |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 657 | */ |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 658 | bool pm_save_wakeup_count(unsigned int count) |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 659 | { |
Rafael J. Wysocki | 023d377 | 2011-01-31 11:06:39 +0100 | [diff] [blame] | 660 | unsigned int cnt, inpr; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 661 | |
Rafael J. Wysocki | 378eef9 | 2011-01-31 11:06:50 +0100 | [diff] [blame] | 662 | events_check_enabled = false; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 663 | spin_lock_irq(&events_lock); |
Rafael J. Wysocki | 023d377 | 2011-01-31 11:06:39 +0100 | [diff] [blame] | 664 | split_counters(&cnt, &inpr); |
| 665 | if (cnt == count && inpr == 0) { |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 666 | saved_count = count; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 667 | events_check_enabled = true; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 668 | } |
| 669 | spin_unlock_irq(&events_lock); |
Rafael J. Wysocki | 378eef9 | 2011-01-31 11:06:50 +0100 | [diff] [blame] | 670 | if (!events_check_enabled) |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 671 | pm_wakeup_update_hit_counts(); |
Rafael J. Wysocki | 378eef9 | 2011-01-31 11:06:50 +0100 | [diff] [blame] | 672 | return events_check_enabled; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 673 | } |
Rafael J. Wysocki | 9c03439 | 2010-10-19 23:42:49 +0200 | [diff] [blame] | 674 | |
| 675 | static struct dentry *wakeup_sources_stats_dentry; |
| 676 | |
| 677 | /** |
| 678 | * print_wakeup_source_stats - Print wakeup source statistics information. |
| 679 | * @m: seq_file to print the statistics into. |
| 680 | * @ws: Wakeup source object to print the statistics for. |
| 681 | */ |
| 682 | static int print_wakeup_source_stats(struct seq_file *m, |
| 683 | struct wakeup_source *ws) |
| 684 | { |
| 685 | unsigned long flags; |
| 686 | ktime_t total_time; |
| 687 | ktime_t max_time; |
| 688 | unsigned long active_count; |
| 689 | ktime_t active_time; |
| 690 | int ret; |
| 691 | |
| 692 | spin_lock_irqsave(&ws->lock, flags); |
| 693 | |
| 694 | total_time = ws->total_time; |
| 695 | max_time = ws->max_time; |
| 696 | active_count = ws->active_count; |
| 697 | if (ws->active) { |
| 698 | active_time = ktime_sub(ktime_get(), ws->last_time); |
| 699 | total_time = ktime_add(total_time, active_time); |
| 700 | if (active_time.tv64 > max_time.tv64) |
| 701 | max_time = active_time; |
| 702 | } else { |
| 703 | active_time = ktime_set(0, 0); |
| 704 | } |
| 705 | |
| 706 | ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t" |
| 707 | "%lld\t\t%lld\t\t%lld\t\t%lld\n", |
| 708 | ws->name, active_count, ws->event_count, ws->hit_count, |
| 709 | ktime_to_ms(active_time), ktime_to_ms(total_time), |
| 710 | ktime_to_ms(max_time), ktime_to_ms(ws->last_time)); |
| 711 | |
| 712 | spin_unlock_irqrestore(&ws->lock, flags); |
| 713 | |
| 714 | return ret; |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * wakeup_sources_stats_show - Print wakeup sources statistics information. |
| 719 | * @m: seq_file to print the statistics into. |
| 720 | */ |
| 721 | static int wakeup_sources_stats_show(struct seq_file *m, void *unused) |
| 722 | { |
| 723 | struct wakeup_source *ws; |
| 724 | |
| 725 | seq_puts(m, "name\t\tactive_count\tevent_count\thit_count\t" |
| 726 | "active_since\ttotal_time\tmax_time\tlast_change\n"); |
| 727 | |
| 728 | rcu_read_lock(); |
| 729 | list_for_each_entry_rcu(ws, &wakeup_sources, entry) |
| 730 | print_wakeup_source_stats(m, ws); |
| 731 | rcu_read_unlock(); |
| 732 | |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | static int wakeup_sources_stats_open(struct inode *inode, struct file *file) |
| 737 | { |
| 738 | return single_open(file, wakeup_sources_stats_show, NULL); |
| 739 | } |
| 740 | |
| 741 | static const struct file_operations wakeup_sources_stats_fops = { |
| 742 | .owner = THIS_MODULE, |
| 743 | .open = wakeup_sources_stats_open, |
| 744 | .read = seq_read, |
| 745 | .llseek = seq_lseek, |
| 746 | .release = single_release, |
| 747 | }; |
| 748 | |
| 749 | static int __init wakeup_sources_debugfs_init(void) |
| 750 | { |
| 751 | wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources", |
| 752 | S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops); |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | postcore_initcall(wakeup_sources_debugfs_init); |