0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/device.h>
0011 #include <linux/idr.h>
0012 #include <linux/init.h>
0013 #include <linux/kdev_t.h>
0014 #include <linux/kernel.h>
0015 #include <linux/kobject.h>
0016 #include <linux/slab.h>
0017 #include <linux/timekeeping.h>
0018
0019 #include "power.h"
0020
0021 static struct class *wakeup_class;
0022
0023 #define wakeup_attr(_name) \
0024 static ssize_t _name##_show(struct device *dev, \
0025 struct device_attribute *attr, char *buf) \
0026 { \
0027 struct wakeup_source *ws = dev_get_drvdata(dev); \
0028 \
0029 return sysfs_emit(buf, "%lu\n", ws->_name); \
0030 } \
0031 static DEVICE_ATTR_RO(_name)
0032
0033 wakeup_attr(active_count);
0034 wakeup_attr(event_count);
0035 wakeup_attr(wakeup_count);
0036 wakeup_attr(expire_count);
0037
0038 static ssize_t active_time_ms_show(struct device *dev,
0039 struct device_attribute *attr, char *buf)
0040 {
0041 struct wakeup_source *ws = dev_get_drvdata(dev);
0042 ktime_t active_time =
0043 ws->active ? ktime_sub(ktime_get(), ws->last_time) : 0;
0044
0045 return sysfs_emit(buf, "%lld\n", ktime_to_ms(active_time));
0046 }
0047 static DEVICE_ATTR_RO(active_time_ms);
0048
0049 static ssize_t total_time_ms_show(struct device *dev,
0050 struct device_attribute *attr, char *buf)
0051 {
0052 struct wakeup_source *ws = dev_get_drvdata(dev);
0053 ktime_t active_time;
0054 ktime_t total_time = ws->total_time;
0055
0056 if (ws->active) {
0057 active_time = ktime_sub(ktime_get(), ws->last_time);
0058 total_time = ktime_add(total_time, active_time);
0059 }
0060
0061 return sysfs_emit(buf, "%lld\n", ktime_to_ms(total_time));
0062 }
0063 static DEVICE_ATTR_RO(total_time_ms);
0064
0065 static ssize_t max_time_ms_show(struct device *dev,
0066 struct device_attribute *attr, char *buf)
0067 {
0068 struct wakeup_source *ws = dev_get_drvdata(dev);
0069 ktime_t active_time;
0070 ktime_t max_time = ws->max_time;
0071
0072 if (ws->active) {
0073 active_time = ktime_sub(ktime_get(), ws->last_time);
0074 if (active_time > max_time)
0075 max_time = active_time;
0076 }
0077
0078 return sysfs_emit(buf, "%lld\n", ktime_to_ms(max_time));
0079 }
0080 static DEVICE_ATTR_RO(max_time_ms);
0081
0082 static ssize_t last_change_ms_show(struct device *dev,
0083 struct device_attribute *attr, char *buf)
0084 {
0085 struct wakeup_source *ws = dev_get_drvdata(dev);
0086
0087 return sysfs_emit(buf, "%lld\n", ktime_to_ms(ws->last_time));
0088 }
0089 static DEVICE_ATTR_RO(last_change_ms);
0090
0091 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
0092 char *buf)
0093 {
0094 struct wakeup_source *ws = dev_get_drvdata(dev);
0095
0096 return sysfs_emit(buf, "%s\n", ws->name);
0097 }
0098 static DEVICE_ATTR_RO(name);
0099
0100 static ssize_t prevent_suspend_time_ms_show(struct device *dev,
0101 struct device_attribute *attr,
0102 char *buf)
0103 {
0104 struct wakeup_source *ws = dev_get_drvdata(dev);
0105 ktime_t prevent_sleep_time = ws->prevent_sleep_time;
0106
0107 if (ws->active && ws->autosleep_enabled) {
0108 prevent_sleep_time = ktime_add(prevent_sleep_time,
0109 ktime_sub(ktime_get(), ws->start_prevent_time));
0110 }
0111
0112 return sysfs_emit(buf, "%lld\n", ktime_to_ms(prevent_sleep_time));
0113 }
0114 static DEVICE_ATTR_RO(prevent_suspend_time_ms);
0115
0116 static struct attribute *wakeup_source_attrs[] = {
0117 &dev_attr_name.attr,
0118 &dev_attr_active_count.attr,
0119 &dev_attr_event_count.attr,
0120 &dev_attr_wakeup_count.attr,
0121 &dev_attr_expire_count.attr,
0122 &dev_attr_active_time_ms.attr,
0123 &dev_attr_total_time_ms.attr,
0124 &dev_attr_max_time_ms.attr,
0125 &dev_attr_last_change_ms.attr,
0126 &dev_attr_prevent_suspend_time_ms.attr,
0127 NULL,
0128 };
0129 ATTRIBUTE_GROUPS(wakeup_source);
0130
0131 static void device_create_release(struct device *dev)
0132 {
0133 kfree(dev);
0134 }
0135
0136 static struct device *wakeup_source_device_create(struct device *parent,
0137 struct wakeup_source *ws)
0138 {
0139 struct device *dev = NULL;
0140 int retval;
0141
0142 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
0143 if (!dev) {
0144 retval = -ENOMEM;
0145 goto error;
0146 }
0147
0148 device_initialize(dev);
0149 dev->devt = MKDEV(0, 0);
0150 dev->class = wakeup_class;
0151 dev->parent = parent;
0152 dev->groups = wakeup_source_groups;
0153 dev->release = device_create_release;
0154 dev_set_drvdata(dev, ws);
0155 device_set_pm_not_required(dev);
0156
0157 retval = dev_set_name(dev, "wakeup%d", ws->id);
0158 if (retval)
0159 goto error;
0160
0161 retval = device_add(dev);
0162 if (retval)
0163 goto error;
0164
0165 return dev;
0166
0167 error:
0168 put_device(dev);
0169 return ERR_PTR(retval);
0170 }
0171
0172
0173
0174
0175
0176
0177 int wakeup_source_sysfs_add(struct device *parent, struct wakeup_source *ws)
0178 {
0179 struct device *dev;
0180
0181 dev = wakeup_source_device_create(parent, ws);
0182 if (IS_ERR(dev))
0183 return PTR_ERR(dev);
0184 ws->dev = dev;
0185
0186 return 0;
0187 }
0188
0189
0190
0191
0192
0193
0194 int pm_wakeup_source_sysfs_add(struct device *parent)
0195 {
0196 if (!parent->power.wakeup || parent->power.wakeup->dev)
0197 return 0;
0198
0199 return wakeup_source_sysfs_add(parent, parent->power.wakeup);
0200 }
0201
0202
0203
0204
0205
0206 void wakeup_source_sysfs_remove(struct wakeup_source *ws)
0207 {
0208 device_unregister(ws->dev);
0209 }
0210
0211 static int __init wakeup_sources_sysfs_init(void)
0212 {
0213 wakeup_class = class_create(THIS_MODULE, "wakeup");
0214
0215 return PTR_ERR_OR_ZERO(wakeup_class);
0216 }
0217 postcore_initcall(wakeup_sources_sysfs_init);