Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  *  pm_wakeup.h - Power management wakeup interface
0004  *
0005  *  Copyright (C) 2008 Alan Stern
0006  *  Copyright (C) 2010 Rafael J. Wysocki, Novell Inc.
0007  */
0008 
0009 #ifndef _LINUX_PM_WAKEUP_H
0010 #define _LINUX_PM_WAKEUP_H
0011 
0012 #ifndef _DEVICE_H_
0013 # error "please don't include this file directly"
0014 #endif
0015 
0016 #include <linux/types.h>
0017 
0018 struct wake_irq;
0019 
0020 /**
0021  * struct wakeup_source - Representation of wakeup sources
0022  *
0023  * @name: Name of the wakeup source
0024  * @id: Wakeup source id
0025  * @entry: Wakeup source list entry
0026  * @lock: Wakeup source lock
0027  * @wakeirq: Optional device specific wakeirq
0028  * @timer: Wakeup timer list
0029  * @timer_expires: Wakeup timer expiration
0030  * @total_time: Total time this wakeup source has been active.
0031  * @max_time: Maximum time this wakeup source has been continuously active.
0032  * @last_time: Monotonic clock when the wakeup source's was touched last time.
0033  * @prevent_sleep_time: Total time this source has been preventing autosleep.
0034  * @event_count: Number of signaled wakeup events.
0035  * @active_count: Number of times the wakeup source was activated.
0036  * @relax_count: Number of times the wakeup source was deactivated.
0037  * @expire_count: Number of times the wakeup source's timeout has expired.
0038  * @wakeup_count: Number of times the wakeup source might abort suspend.
0039  * @dev: Struct device for sysfs statistics about the wakeup source.
0040  * @active: Status of the wakeup source.
0041  * @autosleep_enabled: Autosleep is active, so update @prevent_sleep_time.
0042  */
0043 struct wakeup_source {
0044     const char      *name;
0045     int         id;
0046     struct list_head    entry;
0047     spinlock_t      lock;
0048     struct wake_irq     *wakeirq;
0049     struct timer_list   timer;
0050     unsigned long       timer_expires;
0051     ktime_t total_time;
0052     ktime_t max_time;
0053     ktime_t last_time;
0054     ktime_t start_prevent_time;
0055     ktime_t prevent_sleep_time;
0056     unsigned long       event_count;
0057     unsigned long       active_count;
0058     unsigned long       relax_count;
0059     unsigned long       expire_count;
0060     unsigned long       wakeup_count;
0061     struct device       *dev;
0062     bool            active:1;
0063     bool            autosleep_enabled:1;
0064 };
0065 
0066 #define for_each_wakeup_source(ws) \
0067     for ((ws) = wakeup_sources_walk_start();    \
0068          (ws);                  \
0069          (ws) = wakeup_sources_walk_next((ws)))
0070 
0071 #ifdef CONFIG_PM_SLEEP
0072 
0073 /*
0074  * Changes to device_may_wakeup take effect on the next pm state change.
0075  */
0076 
0077 static inline bool device_can_wakeup(struct device *dev)
0078 {
0079     return dev->power.can_wakeup;
0080 }
0081 
0082 static inline bool device_may_wakeup(struct device *dev)
0083 {
0084     return dev->power.can_wakeup && !!dev->power.wakeup;
0085 }
0086 
0087 static inline bool device_wakeup_path(struct device *dev)
0088 {
0089     return dev->power.wakeup_path;
0090 }
0091 
0092 static inline void device_set_wakeup_path(struct device *dev)
0093 {
0094     dev->power.wakeup_path = true;
0095 }
0096 
0097 /* drivers/base/power/wakeup.c */
0098 extern struct wakeup_source *wakeup_source_create(const char *name);
0099 extern void wakeup_source_destroy(struct wakeup_source *ws);
0100 extern void wakeup_source_add(struct wakeup_source *ws);
0101 extern void wakeup_source_remove(struct wakeup_source *ws);
0102 extern struct wakeup_source *wakeup_source_register(struct device *dev,
0103                             const char *name);
0104 extern void wakeup_source_unregister(struct wakeup_source *ws);
0105 extern int wakeup_sources_read_lock(void);
0106 extern void wakeup_sources_read_unlock(int idx);
0107 extern struct wakeup_source *wakeup_sources_walk_start(void);
0108 extern struct wakeup_source *wakeup_sources_walk_next(struct wakeup_source *ws);
0109 extern int device_wakeup_enable(struct device *dev);
0110 extern int device_wakeup_disable(struct device *dev);
0111 extern void device_set_wakeup_capable(struct device *dev, bool capable);
0112 extern int device_set_wakeup_enable(struct device *dev, bool enable);
0113 extern void __pm_stay_awake(struct wakeup_source *ws);
0114 extern void pm_stay_awake(struct device *dev);
0115 extern void __pm_relax(struct wakeup_source *ws);
0116 extern void pm_relax(struct device *dev);
0117 extern void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard);
0118 extern void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard);
0119 
0120 #else /* !CONFIG_PM_SLEEP */
0121 
0122 static inline void device_set_wakeup_capable(struct device *dev, bool capable)
0123 {
0124     dev->power.can_wakeup = capable;
0125 }
0126 
0127 static inline bool device_can_wakeup(struct device *dev)
0128 {
0129     return dev->power.can_wakeup;
0130 }
0131 
0132 static inline struct wakeup_source *wakeup_source_create(const char *name)
0133 {
0134     return NULL;
0135 }
0136 
0137 static inline void wakeup_source_destroy(struct wakeup_source *ws) {}
0138 
0139 static inline void wakeup_source_add(struct wakeup_source *ws) {}
0140 
0141 static inline void wakeup_source_remove(struct wakeup_source *ws) {}
0142 
0143 static inline struct wakeup_source *wakeup_source_register(struct device *dev,
0144                                const char *name)
0145 {
0146     return NULL;
0147 }
0148 
0149 static inline void wakeup_source_unregister(struct wakeup_source *ws) {}
0150 
0151 static inline int device_wakeup_enable(struct device *dev)
0152 {
0153     dev->power.should_wakeup = true;
0154     return 0;
0155 }
0156 
0157 static inline int device_wakeup_disable(struct device *dev)
0158 {
0159     dev->power.should_wakeup = false;
0160     return 0;
0161 }
0162 
0163 static inline int device_set_wakeup_enable(struct device *dev, bool enable)
0164 {
0165     dev->power.should_wakeup = enable;
0166     return 0;
0167 }
0168 
0169 static inline bool device_may_wakeup(struct device *dev)
0170 {
0171     return dev->power.can_wakeup && dev->power.should_wakeup;
0172 }
0173 
0174 static inline bool device_wakeup_path(struct device *dev)
0175 {
0176     return false;
0177 }
0178 
0179 static inline void device_set_wakeup_path(struct device *dev) {}
0180 
0181 static inline void __pm_stay_awake(struct wakeup_source *ws) {}
0182 
0183 static inline void pm_stay_awake(struct device *dev) {}
0184 
0185 static inline void __pm_relax(struct wakeup_source *ws) {}
0186 
0187 static inline void pm_relax(struct device *dev) {}
0188 
0189 static inline void pm_wakeup_ws_event(struct wakeup_source *ws,
0190                       unsigned int msec, bool hard) {}
0191 
0192 static inline void pm_wakeup_dev_event(struct device *dev, unsigned int msec,
0193                        bool hard) {}
0194 
0195 #endif /* !CONFIG_PM_SLEEP */
0196 
0197 static inline void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
0198 {
0199     return pm_wakeup_ws_event(ws, msec, false);
0200 }
0201 
0202 static inline void pm_wakeup_event(struct device *dev, unsigned int msec)
0203 {
0204     return pm_wakeup_dev_event(dev, msec, false);
0205 }
0206 
0207 static inline void pm_wakeup_hard_event(struct device *dev)
0208 {
0209     return pm_wakeup_dev_event(dev, 0, true);
0210 }
0211 
0212 /**
0213  * device_init_wakeup - Device wakeup initialization.
0214  * @dev: Device to handle.
0215  * @enable: Whether or not to enable @dev as a wakeup device.
0216  *
0217  * By default, most devices should leave wakeup disabled.  The exceptions are
0218  * devices that everyone expects to be wakeup sources: keyboards, power buttons,
0219  * possibly network interfaces, etc.  Also, devices that don't generate their
0220  * own wakeup requests but merely forward requests from one bus to another
0221  * (like PCI bridges) should have wakeup enabled by default.
0222  */
0223 static inline int device_init_wakeup(struct device *dev, bool enable)
0224 {
0225     if (enable) {
0226         device_set_wakeup_capable(dev, true);
0227         return device_wakeup_enable(dev);
0228     } else {
0229         device_wakeup_disable(dev);
0230         device_set_wakeup_capable(dev, false);
0231         return 0;
0232     }
0233 }
0234 
0235 #endif /* _LINUX_PM_WAKEUP_H */