xref: /openbmc/linux/drivers/base/power/sysfs.c (revision c21b37f6)
1 /*
2  * drivers/base/power/sysfs.c - sysfs entries for device PM
3  */
4 
5 #include <linux/device.h>
6 #include <linux/string.h>
7 #include "power.h"
8 
9 
10 /*
11  *	wakeup - Report/change current wakeup option for device
12  *
13  *	Some devices support "wakeup" events, which are hardware signals
14  *	used to activate devices from suspended or low power states.  Such
15  *	devices have one of three values for the sysfs power/wakeup file:
16  *
17  *	 + "enabled\n" to issue the events;
18  *	 + "disabled\n" not to do so; or
19  *	 + "\n" for temporary or permanent inability to issue wakeup.
20  *
21  *	(For example, unconfigured USB devices can't issue wakeups.)
22  *
23  *	Familiar examples of devices that can issue wakeup events include
24  *	keyboards and mice (both PS2 and USB styles), power buttons, modems,
25  *	"Wake-On-LAN" Ethernet links, GPIO lines, and more.  Some events
26  *	will wake the entire system from a suspend state; others may just
27  *	wake up the device (if the system as a whole is already active).
28  *	Some wakeup events use normal IRQ lines; other use special out
29  *	of band signaling.
30  *
31  *	It is the responsibility of device drivers to enable (or disable)
32  *	wakeup signaling as part of changing device power states, respecting
33  *	the policy choices provided through the driver model.
34  *
35  *	Devices may not be able to generate wakeup events from all power
36  *	states.  Also, the events may be ignored in some configurations;
37  *	for example, they might need help from other devices that aren't
38  *	active, or which may have wakeup disabled.  Some drivers rely on
39  *	wakeup events internally (unless they are disabled), keeping
40  *	their hardware in low power modes whenever they're unused.  This
41  *	saves runtime power, without requiring system-wide sleep states.
42  */
43 
44 static const char enabled[] = "enabled";
45 static const char disabled[] = "disabled";
46 
47 static ssize_t
48 wake_show(struct device * dev, struct device_attribute *attr, char * buf)
49 {
50 	return sprintf(buf, "%s\n", device_can_wakeup(dev)
51 		? (device_may_wakeup(dev) ? enabled : disabled)
52 		: "");
53 }
54 
55 static ssize_t
56 wake_store(struct device * dev, struct device_attribute *attr,
57 	const char * buf, size_t n)
58 {
59 	char *cp;
60 	int len = n;
61 
62 	if (!device_can_wakeup(dev))
63 		return -EINVAL;
64 
65 	cp = memchr(buf, '\n', n);
66 	if (cp)
67 		len = cp - buf;
68 	if (len == sizeof enabled - 1
69 			&& strncmp(buf, enabled, sizeof enabled - 1) == 0)
70 		device_set_wakeup_enable(dev, 1);
71 	else if (len == sizeof disabled - 1
72 			&& strncmp(buf, disabled, sizeof disabled - 1) == 0)
73 		device_set_wakeup_enable(dev, 0);
74 	else
75 		return -EINVAL;
76 	return n;
77 }
78 
79 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
80 
81 
82 static struct attribute * power_attrs[] = {
83 	&dev_attr_wakeup.attr,
84 	NULL,
85 };
86 static struct attribute_group pm_attr_group = {
87 	.name	= "power",
88 	.attrs	= power_attrs,
89 };
90 
91 int dpm_sysfs_add(struct device * dev)
92 {
93 	return sysfs_create_group(&dev->kobj, &pm_attr_group);
94 }
95 
96 void dpm_sysfs_remove(struct device * dev)
97 {
98 	sysfs_remove_group(&dev->kobj, &pm_attr_group);
99 }
100