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