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