PM: update docs for writing .../power/state
[pandora-kernel.git] / drivers / base / power / sysfs.c
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 /**
11  *      state - Control current power state of device
12  *
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
15  *      power state.
16  *
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.
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.
31  */
32
33 static ssize_t state_show(struct device * dev, struct device_attribute *attr, char * buf)
34 {
35         if (dev->power.power_state.event)
36                 return sprintf(buf, "2\n");
37         else
38                 return sprintf(buf, "0\n");
39 }
40
41 static ssize_t state_store(struct device * dev, struct device_attribute *attr, const char * buf, size_t n)
42 {
43         pm_message_t state;
44         int error = -EINVAL;
45
46         /* disallow incomplete suspend sequences */
47         if (dev->bus && (dev->bus->suspend_late || dev->bus->resume_early))
48                 return error;
49
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);
54
55         if ((n == 1) && !strcmp(buf, "2"))
56                 error = dpm_runtime_suspend(dev, state);
57
58         if ((n == 1) && !strcmp(buf, "0")) {
59                 dpm_runtime_resume(dev);
60                 error = 0;
61         }
62
63         return error ? error : n;
64 }
65
66 static DEVICE_ATTR(state, 0644, state_show, state_store);
67
68
69 /*
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
103 static const char enabled[] = "enabled";
104 static const char disabled[] = "disabled";
105
106 static ssize_t
107 wake_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
114 static ssize_t
115 wake_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
138 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
139
140
141 static struct attribute * power_attrs[] = {
142         &dev_attr_state.attr,
143         &dev_attr_wakeup.attr,
144         NULL,
145 };
146 static struct attribute_group pm_attr_group = {
147         .name   = "power",
148         .attrs  = power_attrs,
149 };
150
151 int dpm_sysfs_add(struct device * dev)
152 {
153         return sysfs_create_group(&dev->kobj, &pm_attr_group);
154 }
155
156 void dpm_sysfs_remove(struct device * dev)
157 {
158         sysfs_remove_group(&dev->kobj, &pm_attr_group);
159 }