/*1* drivers/base/power/wakeup.c - System wakeup events framework2*3* Copyright (c) 2010 Rafael J. Wysocki <[email protected]>, Novell Inc.4*5* This file is released under the GPLv2.6*/78#include <linux/device.h>9#include <linux/slab.h>10#include <linux/sched.h>11#include <linux/capability.h>12#include <linux/suspend.h>13#include <linux/seq_file.h>14#include <linux/debugfs.h>1516#include "power.h"1718#define TIMEOUT 1001920/*21* If set, the suspend/hibernate code will abort transitions to a sleep state22* if wakeup events are registered during or immediately before the transition.23*/24bool events_check_enabled;2526/*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 one29* atomic variable to hold them both.30*/31static atomic_t combined_event_count = ATOMIC_INIT(0);3233#define IN_PROGRESS_BITS (sizeof(int) * 4)34#define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)3536static void split_counters(unsigned int *cnt, unsigned int *inpr)37{38unsigned int comb = atomic_read(&combined_event_count);3940*cnt = (comb >> IN_PROGRESS_BITS);41*inpr = comb & MAX_IN_PROGRESS;42}4344/* A preserved old value of the events counter. */45static unsigned int saved_count;4647static DEFINE_SPINLOCK(events_lock);4849static void pm_wakeup_timer_fn(unsigned long data);5051static LIST_HEAD(wakeup_sources);5253/**54* wakeup_source_create - Create a struct wakeup_source object.55* @name: Name of the new wakeup source.56*/57struct wakeup_source *wakeup_source_create(const char *name)58{59struct wakeup_source *ws;6061ws = kzalloc(sizeof(*ws), GFP_KERNEL);62if (!ws)63return NULL;6465spin_lock_init(&ws->lock);66if (name)67ws->name = kstrdup(name, GFP_KERNEL);6869return ws;70}71EXPORT_SYMBOL_GPL(wakeup_source_create);7273/**74* wakeup_source_destroy - Destroy a struct wakeup_source object.75* @ws: Wakeup source to destroy.76*/77void wakeup_source_destroy(struct wakeup_source *ws)78{79if (!ws)80return;8182spin_lock_irq(&ws->lock);83while (ws->active) {84spin_unlock_irq(&ws->lock);8586schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT));8788spin_lock_irq(&ws->lock);89}90spin_unlock_irq(&ws->lock);9192kfree(ws->name);93kfree(ws);94}95EXPORT_SYMBOL_GPL(wakeup_source_destroy);9697/**98* wakeup_source_add - Add given object to the list of wakeup sources.99* @ws: Wakeup source object to add to the list.100*/101void wakeup_source_add(struct wakeup_source *ws)102{103if (WARN_ON(!ws))104return;105106setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);107ws->active = false;108109spin_lock_irq(&events_lock);110list_add_rcu(&ws->entry, &wakeup_sources);111spin_unlock_irq(&events_lock);112}113EXPORT_SYMBOL_GPL(wakeup_source_add);114115/**116* wakeup_source_remove - Remove given object from the wakeup sources list.117* @ws: Wakeup source object to remove from the list.118*/119void wakeup_source_remove(struct wakeup_source *ws)120{121if (WARN_ON(!ws))122return;123124spin_lock_irq(&events_lock);125list_del_rcu(&ws->entry);126spin_unlock_irq(&events_lock);127synchronize_rcu();128}129EXPORT_SYMBOL_GPL(wakeup_source_remove);130131/**132* wakeup_source_register - Create wakeup source and add it to the list.133* @name: Name of the wakeup source to register.134*/135struct wakeup_source *wakeup_source_register(const char *name)136{137struct wakeup_source *ws;138139ws = wakeup_source_create(name);140if (ws)141wakeup_source_add(ws);142143return ws;144}145EXPORT_SYMBOL_GPL(wakeup_source_register);146147/**148* wakeup_source_unregister - Remove wakeup source from the list and remove it.149* @ws: Wakeup source object to unregister.150*/151void wakeup_source_unregister(struct wakeup_source *ws)152{153wakeup_source_remove(ws);154wakeup_source_destroy(ws);155}156EXPORT_SYMBOL_GPL(wakeup_source_unregister);157158/**159* device_wakeup_attach - Attach a wakeup source object to a device object.160* @dev: Device to handle.161* @ws: Wakeup source object to attach to @dev.162*163* This causes @dev to be treated as a wakeup device.164*/165static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)166{167spin_lock_irq(&dev->power.lock);168if (dev->power.wakeup) {169spin_unlock_irq(&dev->power.lock);170return -EEXIST;171}172dev->power.wakeup = ws;173spin_unlock_irq(&dev->power.lock);174return 0;175}176177/**178* device_wakeup_enable - Enable given device to be a wakeup source.179* @dev: Device to handle.180*181* Create a wakeup source object, register it and attach it to @dev.182*/183int device_wakeup_enable(struct device *dev)184{185struct wakeup_source *ws;186int ret;187188if (!dev || !dev->power.can_wakeup)189return -EINVAL;190191ws = wakeup_source_register(dev_name(dev));192if (!ws)193return -ENOMEM;194195ret = device_wakeup_attach(dev, ws);196if (ret)197wakeup_source_unregister(ws);198199return ret;200}201EXPORT_SYMBOL_GPL(device_wakeup_enable);202203/**204* device_wakeup_detach - Detach a device's wakeup source object from it.205* @dev: Device to detach the wakeup source object from.206*207* After it returns, @dev will not be treated as a wakeup device any more.208*/209static struct wakeup_source *device_wakeup_detach(struct device *dev)210{211struct wakeup_source *ws;212213spin_lock_irq(&dev->power.lock);214ws = dev->power.wakeup;215dev->power.wakeup = NULL;216spin_unlock_irq(&dev->power.lock);217return ws;218}219220/**221* device_wakeup_disable - Do not regard a device as a wakeup source any more.222* @dev: Device to handle.223*224* Detach the @dev's wakeup source object from it, unregister this wakeup source225* object and destroy it.226*/227int device_wakeup_disable(struct device *dev)228{229struct wakeup_source *ws;230231if (!dev || !dev->power.can_wakeup)232return -EINVAL;233234ws = device_wakeup_detach(dev);235if (ws)236wakeup_source_unregister(ws);237238return 0;239}240EXPORT_SYMBOL_GPL(device_wakeup_disable);241242/**243* device_set_wakeup_capable - Set/reset device wakeup capability flag.244* @dev: Device to handle.245* @capable: Whether or not @dev is capable of waking up the system from sleep.246*247* If @capable is set, set the @dev's power.can_wakeup flag and add its248* wakeup-related attributes to sysfs. Otherwise, unset the @dev's249* power.can_wakeup flag and remove its wakeup-related attributes from sysfs.250*251* This function may sleep and it can't be called from any context where252* sleeping is not allowed.253*/254void device_set_wakeup_capable(struct device *dev, bool capable)255{256if (!!dev->power.can_wakeup == !!capable)257return;258259if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {260if (capable) {261if (wakeup_sysfs_add(dev))262return;263} else {264wakeup_sysfs_remove(dev);265}266}267dev->power.can_wakeup = capable;268}269EXPORT_SYMBOL_GPL(device_set_wakeup_capable);270271/**272* device_init_wakeup - Device wakeup initialization.273* @dev: Device to handle.274* @enable: Whether or not to enable @dev as a wakeup device.275*276* By default, most devices should leave wakeup disabled. The exceptions are277* devices that everyone expects to be wakeup sources: keyboards, power buttons,278* possibly network interfaces, etc.279*/280int device_init_wakeup(struct device *dev, bool enable)281{282int ret = 0;283284if (enable) {285device_set_wakeup_capable(dev, true);286ret = device_wakeup_enable(dev);287} else {288device_set_wakeup_capable(dev, false);289}290291return ret;292}293EXPORT_SYMBOL_GPL(device_init_wakeup);294295/**296* device_set_wakeup_enable - Enable or disable a device to wake up the system.297* @dev: Device to handle.298*/299int device_set_wakeup_enable(struct device *dev, bool enable)300{301if (!dev || !dev->power.can_wakeup)302return -EINVAL;303304return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);305}306EXPORT_SYMBOL_GPL(device_set_wakeup_enable);307308/*309* The functions below use the observation that each wakeup event starts a310* period in which the system should not be suspended. The moment this period311* will end depends on how the wakeup event is going to be processed after being312* detected and all of the possible cases can be divided into two distinct313* groups.314*315* First, a wakeup event may be detected by the same functional unit that will316* carry out the entire processing of it and possibly will pass it to user space317* for further processing. In that case the functional unit that has detected318* the event may later "close" the "no suspend" period associated with it319* directly as soon as it has been dealt with. The pair of pm_stay_awake() and320* pm_relax(), balanced with each other, is supposed to be used in such321* situations.322*323* Second, a wakeup event may be detected by one functional unit and processed324* by another one. In that case the unit that has detected it cannot really325* "close" the "no suspend" period associated with it, unless it knows in326* advance what's going to happen to the event during processing. This327* knowledge, however, may not be available to it, so it can simply specify time328* to wait before the system can be suspended and pass it as the second329* argument of pm_wakeup_event().330*331* It is valid to call pm_relax() after pm_wakeup_event(), in which case the332* "no suspend" period will be ended either by the pm_relax(), or by the timer333* function executed when the timer expires, whichever comes first.334*/335336/**337* wakup_source_activate - Mark given wakeup source as active.338* @ws: Wakeup source to handle.339*340* Update the @ws' statistics and, if @ws has just been activated, notify the PM341* core of the event by incrementing the counter of of wakeup events being342* processed.343*/344static void wakeup_source_activate(struct wakeup_source *ws)345{346ws->active = true;347ws->active_count++;348ws->timer_expires = jiffies;349ws->last_time = ktime_get();350351/* Increment the counter of events in progress. */352atomic_inc(&combined_event_count);353}354355/**356* __pm_stay_awake - Notify the PM core of a wakeup event.357* @ws: Wakeup source object associated with the source of the event.358*359* It is safe to call this function from interrupt context.360*/361void __pm_stay_awake(struct wakeup_source *ws)362{363unsigned long flags;364365if (!ws)366return;367368spin_lock_irqsave(&ws->lock, flags);369ws->event_count++;370if (!ws->active)371wakeup_source_activate(ws);372spin_unlock_irqrestore(&ws->lock, flags);373}374EXPORT_SYMBOL_GPL(__pm_stay_awake);375376/**377* pm_stay_awake - Notify the PM core that a wakeup event is being processed.378* @dev: Device the wakeup event is related to.379*380* Notify the PM core of a wakeup event (signaled by @dev) by calling381* __pm_stay_awake for the @dev's wakeup source object.382*383* Call this function after detecting of a wakeup event if pm_relax() is going384* to be called directly after processing the event (and possibly passing it to385* user space for further processing).386*/387void pm_stay_awake(struct device *dev)388{389unsigned long flags;390391if (!dev)392return;393394spin_lock_irqsave(&dev->power.lock, flags);395__pm_stay_awake(dev->power.wakeup);396spin_unlock_irqrestore(&dev->power.lock, flags);397}398EXPORT_SYMBOL_GPL(pm_stay_awake);399400/**401* wakup_source_deactivate - Mark given wakeup source as inactive.402* @ws: Wakeup source to handle.403*404* Update the @ws' statistics and notify the PM core that the wakeup source has405* become inactive by decrementing the counter of wakeup events being processed406* and incrementing the counter of registered wakeup events.407*/408static void wakeup_source_deactivate(struct wakeup_source *ws)409{410ktime_t duration;411ktime_t now;412413ws->relax_count++;414/*415* __pm_relax() may be called directly or from a timer function.416* If it is called directly right after the timer function has been417* started, but before the timer function calls __pm_relax(), it is418* possible that __pm_stay_awake() will be called in the meantime and419* will set ws->active. Then, ws->active may be cleared immediately420* by the __pm_relax() called from the timer function, but in such a421* case ws->relax_count will be different from ws->active_count.422*/423if (ws->relax_count != ws->active_count) {424ws->relax_count--;425return;426}427428ws->active = false;429430now = ktime_get();431duration = ktime_sub(now, ws->last_time);432ws->total_time = ktime_add(ws->total_time, duration);433if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))434ws->max_time = duration;435436del_timer(&ws->timer);437438/*439* Increment the counter of registered wakeup events and decrement the440* couter of wakeup events in progress simultaneously.441*/442atomic_add(MAX_IN_PROGRESS, &combined_event_count);443}444445/**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.448*449* Call this function for wakeup events whose processing started with calling450* __pm_stay_awake().451*452* It is safe to call it from interrupt context.453*/454void __pm_relax(struct wakeup_source *ws)455{456unsigned long flags;457458if (!ws)459return;460461spin_lock_irqsave(&ws->lock, flags);462if (ws->active)463wakeup_source_deactivate(ws);464spin_unlock_irqrestore(&ws->lock, flags);465}466EXPORT_SYMBOL_GPL(__pm_relax);467468/**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*/474void pm_relax(struct device *dev)475{476unsigned long flags;477478if (!dev)479return;480481spin_lock_irqsave(&dev->power.lock, flags);482__pm_relax(dev->power.wakeup);483spin_unlock_irqrestore(&dev->power.lock, flags);484}485EXPORT_SYMBOL_GPL(pm_relax);486487/**488* pm_wakeup_timer_fn - Delayed finalization of a wakeup event.489* @data: Address of the wakeup source object associated with the event source.490*491* Call __pm_relax() for the wakeup source whose address is stored in @data.492*/493static void pm_wakeup_timer_fn(unsigned long data)494{495__pm_relax((struct wakeup_source *)data);496}497498/**499* __pm_wakeup_event - Notify the PM core of a wakeup event.500* @ws: Wakeup source object associated with the event source.501* @msec: Anticipated event processing time (in milliseconds).502*503* Notify the PM core of a wakeup event whose source is @ws that will take504* approximately @msec milliseconds to be processed by the kernel. If @ws is505* not active, activate it. If @msec is nonzero, set up the @ws' timer to506* execute pm_wakeup_timer_fn() in future.507*508* It is safe to call this function from interrupt context.509*/510void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)511{512unsigned long flags;513unsigned long expires;514515if (!ws)516return;517518spin_lock_irqsave(&ws->lock, flags);519520ws->event_count++;521if (!ws->active)522wakeup_source_activate(ws);523524if (!msec) {525wakeup_source_deactivate(ws);526goto unlock;527}528529expires = jiffies + msecs_to_jiffies(msec);530if (!expires)531expires = 1;532533if (time_after(expires, ws->timer_expires)) {534mod_timer(&ws->timer, expires);535ws->timer_expires = expires;536}537538unlock:539spin_unlock_irqrestore(&ws->lock, flags);540}541EXPORT_SYMBOL_GPL(__pm_wakeup_event);542543544/**545* pm_wakeup_event - Notify the PM core of a wakeup event.546* @dev: Device the wakeup event is related to.547* @msec: Anticipated event processing time (in milliseconds).548*549* Call __pm_wakeup_event() for the @dev's wakeup source object.550*/551void pm_wakeup_event(struct device *dev, unsigned int msec)552{553unsigned long flags;554555if (!dev)556return;557558spin_lock_irqsave(&dev->power.lock, flags);559__pm_wakeup_event(dev->power.wakeup, msec);560spin_unlock_irqrestore(&dev->power.lock, flags);561}562EXPORT_SYMBOL_GPL(pm_wakeup_event);563564/**565* pm_wakeup_update_hit_counts - Update hit counts of all active wakeup sources.566*/567static void pm_wakeup_update_hit_counts(void)568{569unsigned long flags;570struct wakeup_source *ws;571572rcu_read_lock();573list_for_each_entry_rcu(ws, &wakeup_sources, entry) {574spin_lock_irqsave(&ws->lock, flags);575if (ws->active)576ws->hit_count++;577spin_unlock_irqrestore(&ws->lock, flags);578}579rcu_read_unlock();580}581582/**583* pm_wakeup_pending - Check if power transition in progress should be aborted.584*585* Compare the current number of registered wakeup events with its preserved586* value from the past and return true if new wakeup events have been registered587* since the old value was stored. Also return true if the current number of588* wakeup events being processed is different from zero.589*/590bool pm_wakeup_pending(void)591{592unsigned long flags;593bool ret = false;594595spin_lock_irqsave(&events_lock, flags);596if (events_check_enabled) {597unsigned int cnt, inpr;598599split_counters(&cnt, &inpr);600ret = (cnt != saved_count || inpr > 0);601events_check_enabled = !ret;602}603spin_unlock_irqrestore(&events_lock, flags);604if (ret)605pm_wakeup_update_hit_counts();606return ret;607}608609/**610* pm_get_wakeup_count - Read the number of registered wakeup events.611* @count: Address to store the value at.612*613* Store the number of registered wakeup events at the address in @count. Block614* if the current number of wakeup events being processed is nonzero.615*616* Return 'false' if the wait for the number of wakeup events being processed to617* drop down to zero has been interrupted by a signal (and the current number618* of wakeup events being processed is still nonzero). Otherwise return 'true'.619*/620bool pm_get_wakeup_count(unsigned int *count)621{622unsigned int cnt, inpr;623624for (;;) {625split_counters(&cnt, &inpr);626if (inpr == 0 || signal_pending(current))627break;628pm_wakeup_update_hit_counts();629schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT));630}631632split_counters(&cnt, &inpr);633*count = cnt;634return !inpr;635}636637/**638* pm_save_wakeup_count - Save the current number of registered wakeup events.639* @count: Value to compare with the current number of registered wakeup events.640*641* If @count is equal to the current number of registered wakeup events and the642* current number of wakeup events being processed is zero, store @count as the643* old number of registered wakeup events for pm_check_wakeup_events(), enable644* wakeup events detection and return 'true'. Otherwise disable wakeup events645* detection and return 'false'.646*/647bool pm_save_wakeup_count(unsigned int count)648{649unsigned int cnt, inpr;650651events_check_enabled = false;652spin_lock_irq(&events_lock);653split_counters(&cnt, &inpr);654if (cnt == count && inpr == 0) {655saved_count = count;656events_check_enabled = true;657}658spin_unlock_irq(&events_lock);659if (!events_check_enabled)660pm_wakeup_update_hit_counts();661return events_check_enabled;662}663664static struct dentry *wakeup_sources_stats_dentry;665666/**667* print_wakeup_source_stats - Print wakeup source statistics information.668* @m: seq_file to print the statistics into.669* @ws: Wakeup source object to print the statistics for.670*/671static int print_wakeup_source_stats(struct seq_file *m,672struct wakeup_source *ws)673{674unsigned long flags;675ktime_t total_time;676ktime_t max_time;677unsigned long active_count;678ktime_t active_time;679int ret;680681spin_lock_irqsave(&ws->lock, flags);682683total_time = ws->total_time;684max_time = ws->max_time;685active_count = ws->active_count;686if (ws->active) {687active_time = ktime_sub(ktime_get(), ws->last_time);688total_time = ktime_add(total_time, active_time);689if (active_time.tv64 > max_time.tv64)690max_time = active_time;691} else {692active_time = ktime_set(0, 0);693}694695ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t"696"%lld\t\t%lld\t\t%lld\t\t%lld\n",697ws->name, active_count, ws->event_count, ws->hit_count,698ktime_to_ms(active_time), ktime_to_ms(total_time),699ktime_to_ms(max_time), ktime_to_ms(ws->last_time));700701spin_unlock_irqrestore(&ws->lock, flags);702703return ret;704}705706/**707* wakeup_sources_stats_show - Print wakeup sources statistics information.708* @m: seq_file to print the statistics into.709*/710static int wakeup_sources_stats_show(struct seq_file *m, void *unused)711{712struct wakeup_source *ws;713714seq_puts(m, "name\t\tactive_count\tevent_count\thit_count\t"715"active_since\ttotal_time\tmax_time\tlast_change\n");716717rcu_read_lock();718list_for_each_entry_rcu(ws, &wakeup_sources, entry)719print_wakeup_source_stats(m, ws);720rcu_read_unlock();721722return 0;723}724725static int wakeup_sources_stats_open(struct inode *inode, struct file *file)726{727return single_open(file, wakeup_sources_stats_show, NULL);728}729730static const struct file_operations wakeup_sources_stats_fops = {731.owner = THIS_MODULE,732.open = wakeup_sources_stats_open,733.read = seq_read,734.llseek = seq_lseek,735.release = single_release,736};737738static int __init wakeup_sources_debugfs_init(void)739{740wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",741S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);742return 0;743}744745postcore_initcall(wakeup_sources_debugfs_init);746747748