blob: 2d47517dbe323ea8d498be0babb4c5ddb6649219 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/power/sysfs.c - sysfs entries for device PM
3 */
4
5#include <linux/device.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -08006#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "power.h"
8
9
David Brownell2bca2932006-08-30 13:54:36 -070010#ifdef CONFIG_PM_SYSFS_DEPRECATED
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012/**
13 * state - Control current power state of device
14 *
15 * show() returns the current power state of the device. '0' indicates
David Brownell047bda32006-08-30 14:12:48 -070016 * the device is on. Other values (2) indicate the device is in some low
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * power state.
18 *
David Brownell047bda32006-08-30 14:12:48 -070019 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
Yani Ioannou74880c02005-05-17 06:41:12 -040035static ssize_t state_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Pavel Machek022f7b02006-01-22 22:38:52 +010037 if (dev->power.power_state.event)
38 return sprintf(buf, "2\n");
39 else
40 return sprintf(buf, "0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Yani Ioannou74880c02005-05-17 06:41:12 -040043static ssize_t state_store(struct device * dev, struct device_attribute *attr, const char * buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Pavel Machekca078ba2005-09-03 15:56:57 -070045 pm_message_t state;
Pavel Machek022f7b02006-01-22 22:38:52 +010046 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
David Brownell047bda32006-08-30 14:12:48 -070048 /* disallow incomplete suspend sequences */
49 if (dev->bus && (dev->bus->suspend_late || dev->bus->resume_early))
50 return error;
51
Pavel Machek022f7b02006-01-22 22:38:52 +010052 state.event = PM_EVENT_SUSPEND;
53 /* Older apps expected to write "3" here - confused with PCI D3 */
54 if ((n == 1) && !strcmp(buf, "3"))
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 error = dpm_runtime_suspend(dev, state);
Pavel Machek022f7b02006-01-22 22:38:52 +010056
57 if ((n == 1) && !strcmp(buf, "2"))
58 error = dpm_runtime_suspend(dev, state);
59
60 if ((n == 1) && !strcmp(buf, "0")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 dpm_runtime_resume(dev);
Pavel Machek022f7b02006-01-22 22:38:52 +010062 error = 0;
63 }
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return error ? error : n;
66}
67
68static DEVICE_ATTR(state, 0644, state_show, state_store);
69
70
David Brownell2bca2932006-08-30 13:54:36 -070071#endif /* CONFIG_PM_SYSFS_DEPRECATED */
72
David Brownell0ac85242005-09-12 19:39:34 -070073/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static struct attribute * power_attrs[] = {
David Brownell2bca2932006-08-30 13:54:36 -0700146#ifdef CONFIG_PM_SYSFS_DEPRECATED
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 &dev_attr_state.attr,
David Brownell2bca2932006-08-30 13:54:36 -0700148#endif
David Brownell0ac85242005-09-12 19:39:34 -0700149 &dev_attr_wakeup.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 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}