1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Wakeup statistics in sysfs
4  *
5  * Copyright (c) 2019 Linux Foundation
6  * Copyright (c) 2019 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
7  * Copyright (c) 2019 Google Inc.
8  */
9 
10 #include <linux/device.h>
11 #include <linux/idr.h>
12 #include <linux/init.h>
13 #include <linux/kdev_t.h>
14 #include <linux/kernel.h>
15 #include <linux/kobject.h>
16 #include <linux/slab.h>
17 #include <linux/timekeeping.h>
18 
19 #include "power.h"
20 
21 static struct class *wakeup_class;
22 
23 #define wakeup_attr(_name)						\
24 static ssize_t _name##_show(struct device *dev,				\
25 			    struct device_attribute *attr, char *buf)	\
26 {									\
27 	struct wakeup_source *ws = dev_get_drvdata(dev);		\
28 									\
29 	return sprintf(buf, "%lu\n", ws->_name);			\
30 }									\
31 static DEVICE_ATTR_RO(_name)
32 
33 wakeup_attr(active_count);
34 wakeup_attr(event_count);
35 wakeup_attr(wakeup_count);
36 wakeup_attr(expire_count);
37 
active_time_ms_show(struct device * dev,struct device_attribute * attr,char * buf)38 static ssize_t active_time_ms_show(struct device *dev,
39 				   struct device_attribute *attr, char *buf)
40 {
41 	struct wakeup_source *ws = dev_get_drvdata(dev);
42 	ktime_t active_time =
43 		ws->active ? ktime_sub(ktime_get(), ws->last_time) : 0;
44 
45 	return sprintf(buf, "%lld\n", ktime_to_ms(active_time));
46 }
47 static DEVICE_ATTR_RO(active_time_ms);
48 
total_time_ms_show(struct device * dev,struct device_attribute * attr,char * buf)49 static ssize_t total_time_ms_show(struct device *dev,
50 				  struct device_attribute *attr, char *buf)
51 {
52 	struct wakeup_source *ws = dev_get_drvdata(dev);
53 	ktime_t active_time;
54 	ktime_t total_time = ws->total_time;
55 
56 	if (ws->active) {
57 		active_time = ktime_sub(ktime_get(), ws->last_time);
58 		total_time = ktime_add(total_time, active_time);
59 	}
60 	return sprintf(buf, "%lld\n", ktime_to_ms(total_time));
61 }
62 static DEVICE_ATTR_RO(total_time_ms);
63 
max_time_ms_show(struct device * dev,struct device_attribute * attr,char * buf)64 static ssize_t max_time_ms_show(struct device *dev,
65 				struct device_attribute *attr, char *buf)
66 {
67 	struct wakeup_source *ws = dev_get_drvdata(dev);
68 	ktime_t active_time;
69 	ktime_t max_time = ws->max_time;
70 
71 	if (ws->active) {
72 		active_time = ktime_sub(ktime_get(), ws->last_time);
73 		if (active_time > max_time)
74 			max_time = active_time;
75 	}
76 	return sprintf(buf, "%lld\n", ktime_to_ms(max_time));
77 }
78 static DEVICE_ATTR_RO(max_time_ms);
79 
last_change_ms_show(struct device * dev,struct device_attribute * attr,char * buf)80 static ssize_t last_change_ms_show(struct device *dev,
81 				   struct device_attribute *attr, char *buf)
82 {
83 	struct wakeup_source *ws = dev_get_drvdata(dev);
84 
85 	return sprintf(buf, "%lld\n", ktime_to_ms(ws->last_time));
86 }
87 static DEVICE_ATTR_RO(last_change_ms);
88 
name_show(struct device * dev,struct device_attribute * attr,char * buf)89 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
90 			 char *buf)
91 {
92 	struct wakeup_source *ws = dev_get_drvdata(dev);
93 
94 	return sprintf(buf, "%s\n", ws->name);
95 }
96 static DEVICE_ATTR_RO(name);
97 
prevent_suspend_time_ms_show(struct device * dev,struct device_attribute * attr,char * buf)98 static ssize_t prevent_suspend_time_ms_show(struct device *dev,
99 					    struct device_attribute *attr,
100 					    char *buf)
101 {
102 	struct wakeup_source *ws = dev_get_drvdata(dev);
103 	ktime_t prevent_sleep_time = ws->prevent_sleep_time;
104 
105 	if (ws->active && ws->autosleep_enabled) {
106 		prevent_sleep_time = ktime_add(prevent_sleep_time,
107 			ktime_sub(ktime_get(), ws->start_prevent_time));
108 	}
109 	return sprintf(buf, "%lld\n", ktime_to_ms(prevent_sleep_time));
110 }
111 static DEVICE_ATTR_RO(prevent_suspend_time_ms);
112 
113 static struct attribute *wakeup_source_attrs[] = {
114 	&dev_attr_name.attr,
115 	&dev_attr_active_count.attr,
116 	&dev_attr_event_count.attr,
117 	&dev_attr_wakeup_count.attr,
118 	&dev_attr_expire_count.attr,
119 	&dev_attr_active_time_ms.attr,
120 	&dev_attr_total_time_ms.attr,
121 	&dev_attr_max_time_ms.attr,
122 	&dev_attr_last_change_ms.attr,
123 	&dev_attr_prevent_suspend_time_ms.attr,
124 	NULL,
125 };
126 ATTRIBUTE_GROUPS(wakeup_source);
127 
device_create_release(struct device * dev)128 static void device_create_release(struct device *dev)
129 {
130 	kfree(dev);
131 }
132 
wakeup_source_device_create(struct device * parent,struct wakeup_source * ws)133 static struct device *wakeup_source_device_create(struct device *parent,
134 						  struct wakeup_source *ws)
135 {
136 	struct device *dev = NULL;
137 	int retval = -ENODEV;
138 
139 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
140 	if (!dev) {
141 		retval = -ENOMEM;
142 		goto error;
143 	}
144 
145 	device_initialize(dev);
146 	dev->devt = MKDEV(0, 0);
147 	dev->class = wakeup_class;
148 	dev->parent = parent;
149 	dev->groups = wakeup_source_groups;
150 	dev->release = device_create_release;
151 	dev_set_drvdata(dev, ws);
152 	device_set_pm_not_required(dev);
153 
154 	retval = kobject_set_name(&dev->kobj, "wakeup%d", ws->id);
155 	if (retval)
156 		goto error;
157 
158 	retval = device_add(dev);
159 	if (retval)
160 		goto error;
161 
162 	return dev;
163 
164 error:
165 	put_device(dev);
166 	return ERR_PTR(retval);
167 }
168 
169 /**
170  * wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs.
171  * @parent: Device given wakeup source is associated with (or NULL if virtual).
172  * @ws: Wakeup source to be added in sysfs.
173  */
wakeup_source_sysfs_add(struct device * parent,struct wakeup_source * ws)174 int wakeup_source_sysfs_add(struct device *parent, struct wakeup_source *ws)
175 {
176 	struct device *dev;
177 
178 	dev = wakeup_source_device_create(parent, ws);
179 	if (IS_ERR(dev))
180 		return PTR_ERR(dev);
181 	ws->dev = dev;
182 
183 	return 0;
184 }
185 
186 /**
187  * pm_wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs
188  * for a device if they're missing.
189  * @parent: Device given wakeup source is associated with
190  */
pm_wakeup_source_sysfs_add(struct device * parent)191 int pm_wakeup_source_sysfs_add(struct device *parent)
192 {
193 	if (!parent->power.wakeup || parent->power.wakeup->dev)
194 		return 0;
195 
196 	return wakeup_source_sysfs_add(parent, parent->power.wakeup);
197 }
198 
199 /**
200  * wakeup_source_sysfs_remove - Remove wakeup_source attributes from sysfs.
201  * @ws: Wakeup source to be removed from sysfs.
202  */
wakeup_source_sysfs_remove(struct wakeup_source * ws)203 void wakeup_source_sysfs_remove(struct wakeup_source *ws)
204 {
205 	device_unregister(ws->dev);
206 }
207 
wakeup_sources_sysfs_init(void)208 static int __init wakeup_sources_sysfs_init(void)
209 {
210 	wakeup_class = class_create(THIS_MODULE, "wakeup");
211 
212 	return PTR_ERR_OR_ZERO(wakeup_class);
213 }
214 postcore_initcall(wakeup_sources_sysfs_init);
215