2 * drivers/base/power/sysfs.c - sysfs entries for device PM
5 #include <linux/device.h>
6 #include <linux/string.h>
11 * state - Control current power state of device
13 * show() returns the current power state of the device. '0' indicates
14 * the device is on. Other values (2) indicate the device is in some low
17 * store() sets the current power state, which is an integer valued
18 * 0, 2, or 3. Devices with bus.suspend_late(), or bus.resume_early()
19 * methods fail this operation; those methods couldn't be called.
22 * - If the recorded dev->power.power_state.event matches the
23 * target value, nothing is done.
24 * - If the recorded event code is nonzero, the device is reactivated
25 * by calling bus.resume() and/or class.resume().
26 * - If the target value is nonzero, the device is suspended by
27 * calling class.suspend() and/or bus.suspend() with event code
30 * This mechanism is DEPRECATED and should only be used for testing.
33 static ssize_t state_show(struct device * dev, struct device_attribute *attr, char * buf)
35 if (dev->power.power_state.event)
36 return sprintf(buf, "2\n");
38 return sprintf(buf, "0\n");
41 static ssize_t state_store(struct device * dev, struct device_attribute *attr, const char * buf, size_t n)
46 /* disallow incomplete suspend sequences */
47 if (dev->bus && (dev->bus->suspend_late || dev->bus->resume_early))
50 state.event = PM_EVENT_SUSPEND;
51 /* Older apps expected to write "3" here - confused with PCI D3 */
52 if ((n == 1) && !strcmp(buf, "3"))
53 error = dpm_runtime_suspend(dev, state);
55 if ((n == 1) && !strcmp(buf, "2"))
56 error = dpm_runtime_suspend(dev, state);
58 if ((n == 1) && !strcmp(buf, "0")) {
59 dpm_runtime_resume(dev);
63 return error ? error : n;
66 static DEVICE_ATTR(state, 0644, state_show, state_store);
70 * wakeup - Report/change current wakeup option for device
72 * Some devices support "wakeup" events, which are hardware signals
73 * used to activate devices from suspended or low power states. Such
74 * devices have one of three values for the sysfs power/wakeup file:
76 * + "enabled\n" to issue the events;
77 * + "disabled\n" not to do so; or
78 * + "\n" for temporary or permanent inability to issue wakeup.
80 * (For example, unconfigured USB devices can't issue wakeups.)
82 * Familiar examples of devices that can issue wakeup events include
83 * keyboards and mice (both PS2 and USB styles), power buttons, modems,
84 * "Wake-On-LAN" Ethernet links, GPIO lines, and more. Some events
85 * will wake the entire system from a suspend state; others may just
86 * wake up the device (if the system as a whole is already active).
87 * Some wakeup events use normal IRQ lines; other use special out
90 * It is the responsibility of device drivers to enable (or disable)
91 * wakeup signaling as part of changing device power states, respecting
92 * the policy choices provided through the driver model.
94 * Devices may not be able to generate wakeup events from all power
95 * states. Also, the events may be ignored in some configurations;
96 * for example, they might need help from other devices that aren't
97 * active, or which may have wakeup disabled. Some drivers rely on
98 * wakeup events internally (unless they are disabled), keeping
99 * their hardware in low power modes whenever they're unused. This
100 * saves runtime power, without requiring system-wide sleep states.
103 static const char enabled[] = "enabled";
104 static const char disabled[] = "disabled";
107 wake_show(struct device * dev, struct device_attribute *attr, char * buf)
109 return sprintf(buf, "%s\n", device_can_wakeup(dev)
110 ? (device_may_wakeup(dev) ? enabled : disabled)
115 wake_store(struct device * dev, struct device_attribute *attr,
116 const char * buf, size_t n)
121 if (!device_can_wakeup(dev))
124 cp = memchr(buf, '\n', n);
127 if (len == sizeof enabled - 1
128 && strncmp(buf, enabled, sizeof enabled - 1) == 0)
129 device_set_wakeup_enable(dev, 1);
130 else if (len == sizeof disabled - 1
131 && strncmp(buf, disabled, sizeof disabled - 1) == 0)
132 device_set_wakeup_enable(dev, 0);
138 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
141 static struct attribute * power_attrs[] = {
142 &dev_attr_state.attr,
143 &dev_attr_wakeup.attr,
146 static struct attribute_group pm_attr_group = {
148 .attrs = power_attrs,
151 int dpm_sysfs_add(struct device * dev)
153 return sysfs_create_group(&dev->kobj, &pm_attr_group);
156 void dpm_sysfs_remove(struct device * dev)
158 sysfs_remove_group(&dev->kobj, &pm_attr_group);