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#ifdef	CONFIG_PM_SYSFS_DEPRECATED
11
12/**
13 *	state - Control current power state of device
14 *
15 *	show() returns the current power state of the device. '0' indicates
16 *	the device is on. Other values (2) indicate the device is in some low
17 *	power state.
18 *
19 *	store() sets the current power state, which is an integer valued
20 *	0, 2, or 3.  Devices with bus.suspend_late(), or bus.resume_early()
21 *	methods fail this operation; those methods couldn't be called.
22 *	Otherwise,
23 *
24 *	- If the recorded dev->power.power_state.event matches the
25 *	  target value, nothing is done.
26 *	- If the recorded event code is nonzero, the device is reactivated
27 *	  by calling bus.resume() and/or class.resume().
28 *	- If the target value is nonzero, the device is suspended by
29 *	  calling class.suspend() and/or bus.suspend() with event code
30 *	  PM_EVENT_SUSPEND.
31 *
32 *	This mechanism is DEPRECATED and should only be used for testing.
33 */
34
35static ssize_t state_show(struct device * dev, struct device_attribute *attr, char * buf)
36{
37	if (dev->power.power_state.event)
38		return sprintf(buf, "2\n");
39	else
40		return sprintf(buf, "0\n");
41}
42
43static ssize_t state_store(struct device * dev, struct device_attribute *attr, const char * buf, size_t n)
44{
45	pm_message_t state;
46	int error = -EINVAL;
47
48	/* disallow incomplete suspend sequences */
49	if (dev->bus && (dev->bus->suspend_late || dev->bus->resume_early))
50		return error;
51
52	state.event = PM_EVENT_SUSPEND;
53	/* Older apps expected to write "3" here - confused with PCI D3 */
54	if ((n == 1) && !strcmp(buf, "3"))
55		error = dpm_runtime_suspend(dev, state);
56
57	if ((n == 1) && !strcmp(buf, "2"))
58		error = dpm_runtime_suspend(dev, state);
59
60	if ((n == 1) && !strcmp(buf, "0")) {
61		dpm_runtime_resume(dev);
62		error = 0;
63	}
64
65	return error ? error : n;
66}
67
68static DEVICE_ATTR(state, 0644, state_show, state_store);
69
70
71#endif	/* CONFIG_PM_SYSFS_DEPRECATED */
72
73/*
74 *	wakeup - Report/change current wakeup option for device
75 *
76 *	Some devices support "wakeup" events, which are hardware signals
77 *	used to activate devices from suspended or low power states.  Such
78 *	devices have one of three values for the sysfs power/wakeup file:
79 *
80 *	 + "enabled\n" to issue the events;
81 *	 + "disabled\n" not to do so; or
82 *	 + "\n" for temporary or permanent inability to issue wakeup.
83 *
84 *	(For example, unconfigured USB devices can't issue wakeups.)
85 *
86 *	Familiar examples of devices that can issue wakeup events include
87 *	keyboards and mice (both PS2 and USB styles), power buttons, modems,
88 *	"Wake-On-LAN" Ethernet links, GPIO lines, and more.  Some events
89 *	will wake the entire system from a suspend state; others may just
90 *	wake up the device (if the system as a whole is already active).
91 *	Some wakeup events use normal IRQ lines; other use special out
92 *	of band signaling.
93 *
94 *	It is the responsibility of device drivers to enable (or disable)
95 *	wakeup signaling as part of changing device power states, respecting
96 *	the policy choices provided through the driver model.
97 *
98 *	Devices may not be able to generate wakeup events from all power
99 *	states.  Also, the events may be ignored in some configurations;
100 *	for example, they might need help from other devices that aren't
101 *	active, or which may have wakeup disabled.  Some drivers rely on
102 *	wakeup events internally (unless they are disabled), keeping
103 *	their hardware in low power modes whenever they're unused.  This
104 *	saves runtime power, without requiring system-wide sleep states.
105 */
106
107static const char enabled[] = "enabled";
108static const char disabled[] = "disabled";
109
110static ssize_t
111wake_show(struct device * dev, struct device_attribute *attr, char * buf)
112{
113	return sprintf(buf, "%s\n", device_can_wakeup(dev)
114		? (device_may_wakeup(dev) ? enabled : disabled)
115		: "");
116}
117
118static ssize_t
119wake_store(struct device * dev, struct device_attribute *attr,
120	const char * buf, size_t n)
121{
122	char *cp;
123	int len = n;
124
125	if (!device_can_wakeup(dev))
126		return -EINVAL;
127
128	cp = memchr(buf, '\n', n);
129	if (cp)
130		len = cp - buf;
131	if (len == sizeof enabled - 1
132			&& strncmp(buf, enabled, sizeof enabled - 1) == 0)
133		device_set_wakeup_enable(dev, 1);
134	else if (len == sizeof disabled - 1
135			&& strncmp(buf, disabled, sizeof disabled - 1) == 0)
136		device_set_wakeup_enable(dev, 0);
137	else
138		return -EINVAL;
139	return n;
140}
141
142static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
143
144
145static struct attribute * power_attrs[] = {
146#ifdef	CONFIG_PM_SYSFS_DEPRECATED
147	&dev_attr_state.attr,
148#endif
149	&dev_attr_wakeup.attr,
150	NULL,
151};
152static struct attribute_group pm_attr_group = {
153	.name	= "power",
154	.attrs	= power_attrs,
155};
156
157int dpm_sysfs_add(struct device * dev)
158{
159	return sysfs_create_group(&dev->kobj, &pm_attr_group);
160}
161
162void dpm_sysfs_remove(struct device * dev)
163{
164	sysfs_remove_group(&dev->kobj, &pm_attr_group);
165}
166