blob: e55b3c2779e967fd5301e34fb120f27334c94adb [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
10/**
11 * state - Control current power state of device
12 *
13 * show() returns the current power state of the device. '0' indicates
David Brownell047bda32006-08-30 14:12:48 -070014 * the device is on. Other values (2) indicate the device is in some low
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * power state.
16 *
David Brownell047bda32006-08-30 14:12:48 -070017 * 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.
20 * Otherwise,
21 *
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
28 * PM_EVENT_SUSPEND.
29 *
30 * This mechanism is DEPRECATED and should only be used for testing.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32
Yani Ioannou74880c02005-05-17 06:41:12 -040033static ssize_t state_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Pavel Machek022f7b02006-01-22 22:38:52 +010035 if (dev->power.power_state.event)
36 return sprintf(buf, "2\n");
37 else
38 return sprintf(buf, "0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Yani Ioannou74880c02005-05-17 06:41:12 -040041static ssize_t state_store(struct device * dev, struct device_attribute *attr, const char * buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Pavel Machekca078ba2005-09-03 15:56:57 -070043 pm_message_t state;
Pavel Machek022f7b02006-01-22 22:38:52 +010044 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
David Brownell047bda32006-08-30 14:12:48 -070046 /* disallow incomplete suspend sequences */
47 if (dev->bus && (dev->bus->suspend_late || dev->bus->resume_early))
48 return error;
49
Pavel Machek022f7b02006-01-22 22:38:52 +010050 state.event = PM_EVENT_SUSPEND;
51 /* Older apps expected to write "3" here - confused with PCI D3 */
52 if ((n == 1) && !strcmp(buf, "3"))
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 error = dpm_runtime_suspend(dev, state);
Pavel Machek022f7b02006-01-22 22:38:52 +010054
55 if ((n == 1) && !strcmp(buf, "2"))
56 error = dpm_runtime_suspend(dev, state);
57
58 if ((n == 1) && !strcmp(buf, "0")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 dpm_runtime_resume(dev);
Pavel Machek022f7b02006-01-22 22:38:52 +010060 error = 0;
61 }
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return error ? error : n;
64}
65
66static DEVICE_ATTR(state, 0644, state_show, state_store);
67
68
David Brownell0ac85242005-09-12 19:39:34 -070069/*
70 * wakeup - Report/change current wakeup option for device
71 *
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:
75 *
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.
79 *
80 * (For example, unconfigured USB devices can't issue wakeups.)
81 *
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
88 * of band signaling.
89 *
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.
93 *
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.
101 */
102
103static const char enabled[] = "enabled";
104static const char disabled[] = "disabled";
105
106static ssize_t
107wake_show(struct device * dev, struct device_attribute *attr, char * buf)
108{
109 return sprintf(buf, "%s\n", device_can_wakeup(dev)
110 ? (device_may_wakeup(dev) ? enabled : disabled)
111 : "");
112}
113
114static ssize_t
115wake_store(struct device * dev, struct device_attribute *attr,
116 const char * buf, size_t n)
117{
118 char *cp;
119 int len = n;
120
121 if (!device_can_wakeup(dev))
122 return -EINVAL;
123
124 cp = memchr(buf, '\n', n);
125 if (cp)
126 len = cp - buf;
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);
133 else
134 return -EINVAL;
135 return n;
136}
137
138static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
139
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static struct attribute * power_attrs[] = {
142 &dev_attr_state.attr,
David Brownell0ac85242005-09-12 19:39:34 -0700143 &dev_attr_wakeup.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 NULL,
145};
146static struct attribute_group pm_attr_group = {
147 .name = "power",
148 .attrs = power_attrs,
149};
150
151int dpm_sysfs_add(struct device * dev)
152{
153 return sysfs_create_group(&dev->kobj, &pm_attr_group);
154}
155
156void dpm_sysfs_remove(struct device * dev)
157{
158 sysfs_remove_group(&dev->kobj, &pm_attr_group);
159}