]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/base/power/sysfs.c
1eca50c8e7ca36dab412e1c455c245022d1ee1da
[net-next-2.6.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 <linux/pm_runtime.h>
8 #include <asm/atomic.h>
9 #include "power.h"
10
11 /*
12  *      control - Report/change current runtime PM setting of the device
13  *
14  *      Runtime power management of a device can be blocked with the help of
15  *      this attribute.  All devices have one of the following two values for
16  *      the power/control file:
17  *
18  *       + "auto\n" to allow the device to be power managed at run time;
19  *       + "on\n" to prevent the device from being power managed at run time;
20  *
21  *      The default for all devices is "auto", which means that devices may be
22  *      subject to automatic power management, depending on their drivers.
23  *      Changing this attribute to "on" prevents the driver from power managing
24  *      the device at run time.  Doing that while the device is suspended causes
25  *      it to be woken up.
26  *
27  *      wakeup - Report/change current wakeup option for device
28  *
29  *      Some devices support "wakeup" events, which are hardware signals
30  *      used to activate devices from suspended or low power states.  Such
31  *      devices have one of three values for the sysfs power/wakeup file:
32  *
33  *       + "enabled\n" to issue the events;
34  *       + "disabled\n" not to do so; or
35  *       + "\n" for temporary or permanent inability to issue wakeup.
36  *
37  *      (For example, unconfigured USB devices can't issue wakeups.)
38  *
39  *      Familiar examples of devices that can issue wakeup events include
40  *      keyboards and mice (both PS2 and USB styles), power buttons, modems,
41  *      "Wake-On-LAN" Ethernet links, GPIO lines, and more.  Some events
42  *      will wake the entire system from a suspend state; others may just
43  *      wake up the device (if the system as a whole is already active).
44  *      Some wakeup events use normal IRQ lines; other use special out
45  *      of band signaling.
46  *
47  *      It is the responsibility of device drivers to enable (or disable)
48  *      wakeup signaling as part of changing device power states, respecting
49  *      the policy choices provided through the driver model.
50  *
51  *      Devices may not be able to generate wakeup events from all power
52  *      states.  Also, the events may be ignored in some configurations;
53  *      for example, they might need help from other devices that aren't
54  *      active, or which may have wakeup disabled.  Some drivers rely on
55  *      wakeup events internally (unless they are disabled), keeping
56  *      their hardware in low power modes whenever they're unused.  This
57  *      saves runtime power, without requiring system-wide sleep states.
58  *
59  *      async - Report/change current async suspend setting for the device
60  *
61  *      Asynchronous suspend and resume of the device during system-wide power
62  *      state transitions can be enabled by writing "enabled" to this file.
63  *      Analogously, if "disabled" is written to this file, the device will be
64  *      suspended and resumed synchronously.
65  *
66  *      All devices have one of the following two values for power/async:
67  *
68  *       + "enabled\n" to permit the asynchronous suspend/resume of the device;
69  *       + "disabled\n" to forbid it;
70  *
71  *      NOTE: It generally is unsafe to permit the asynchronous suspend/resume
72  *      of a device unless it is certain that all of the PM dependencies of the
73  *      device are known to the PM core.  However, for some devices this
74  *      attribute is set to "enabled" by bus type code or device drivers and in
75  *      that cases it should be safe to leave the default value.
76  *
77  *      wakeup_count - Report the number of wakeup events related to the device
78  */
79
80 static const char enabled[] = "enabled";
81 static const char disabled[] = "disabled";
82
83 #ifdef CONFIG_PM_RUNTIME
84 static const char ctrl_auto[] = "auto";
85 static const char ctrl_on[] = "on";
86
87 static ssize_t control_show(struct device *dev, struct device_attribute *attr,
88                             char *buf)
89 {
90         return sprintf(buf, "%s\n",
91                                 dev->power.runtime_auto ? ctrl_auto : ctrl_on);
92 }
93
94 static ssize_t control_store(struct device * dev, struct device_attribute *attr,
95                              const char * buf, size_t n)
96 {
97         char *cp;
98         int len = n;
99
100         cp = memchr(buf, '\n', n);
101         if (cp)
102                 len = cp - buf;
103         if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
104                 pm_runtime_allow(dev);
105         else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
106                 pm_runtime_forbid(dev);
107         else
108                 return -EINVAL;
109         return n;
110 }
111
112 static DEVICE_ATTR(control, 0644, control_show, control_store);
113
114 static ssize_t rtpm_status_show(struct device *dev,
115                                 struct device_attribute *attr, char *buf)
116 {
117         const char *p;
118
119         if (dev->power.runtime_error) {
120                 p = "error\n";
121         } else if (dev->power.disable_depth) {
122                 p = "unsupported\n";
123         } else {
124                 switch (dev->power.runtime_status) {
125                 case RPM_SUSPENDED:
126                         p = "suspended\n";
127                         break;
128                 case RPM_SUSPENDING:
129                         p = "suspending\n";
130                         break;
131                 case RPM_RESUMING:
132                         p = "resuming\n";
133                         break;
134                 case RPM_ACTIVE:
135                         p = "active\n";
136                         break;
137                 default:
138                         return -EIO;
139                 }
140         }
141         return sprintf(buf, p);
142 }
143
144 static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
145 #endif
146
147 static ssize_t
148 wake_show(struct device * dev, struct device_attribute *attr, char * buf)
149 {
150         return sprintf(buf, "%s\n", device_can_wakeup(dev)
151                 ? (device_may_wakeup(dev) ? enabled : disabled)
152                 : "");
153 }
154
155 static ssize_t
156 wake_store(struct device * dev, struct device_attribute *attr,
157         const char * buf, size_t n)
158 {
159         char *cp;
160         int len = n;
161
162         if (!device_can_wakeup(dev))
163                 return -EINVAL;
164
165         cp = memchr(buf, '\n', n);
166         if (cp)
167                 len = cp - buf;
168         if (len == sizeof enabled - 1
169                         && strncmp(buf, enabled, sizeof enabled - 1) == 0)
170                 device_set_wakeup_enable(dev, 1);
171         else if (len == sizeof disabled - 1
172                         && strncmp(buf, disabled, sizeof disabled - 1) == 0)
173                 device_set_wakeup_enable(dev, 0);
174         else
175                 return -EINVAL;
176         return n;
177 }
178
179 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
180
181 #ifdef CONFIG_PM_SLEEP
182 static ssize_t wakeup_count_show(struct device *dev,
183                                 struct device_attribute *attr, char *buf)
184 {
185         return sprintf(buf, "%lu\n", dev->power.wakeup_count);
186 }
187
188 static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
189 #endif
190
191 #ifdef CONFIG_PM_ADVANCED_DEBUG
192 #ifdef CONFIG_PM_RUNTIME
193
194 static ssize_t rtpm_usagecount_show(struct device *dev,
195                                     struct device_attribute *attr, char *buf)
196 {
197         return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
198 }
199
200 static ssize_t rtpm_children_show(struct device *dev,
201                                   struct device_attribute *attr, char *buf)
202 {
203         return sprintf(buf, "%d\n", dev->power.ignore_children ?
204                 0 : atomic_read(&dev->power.child_count));
205 }
206
207 static ssize_t rtpm_enabled_show(struct device *dev,
208                                  struct device_attribute *attr, char *buf)
209 {
210         if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
211                 return sprintf(buf, "disabled & forbidden\n");
212         else if (dev->power.disable_depth)
213                 return sprintf(buf, "disabled\n");
214         else if (dev->power.runtime_auto == false)
215                 return sprintf(buf, "forbidden\n");
216         return sprintf(buf, "enabled\n");
217 }
218
219 static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
220 static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
221 static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
222
223 #endif
224
225 static ssize_t async_show(struct device *dev, struct device_attribute *attr,
226                           char *buf)
227 {
228         return sprintf(buf, "%s\n",
229                         device_async_suspend_enabled(dev) ? enabled : disabled);
230 }
231
232 static ssize_t async_store(struct device *dev, struct device_attribute *attr,
233                            const char *buf, size_t n)
234 {
235         char *cp;
236         int len = n;
237
238         cp = memchr(buf, '\n', n);
239         if (cp)
240                 len = cp - buf;
241         if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
242                 device_enable_async_suspend(dev);
243         else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
244                 device_disable_async_suspend(dev);
245         else
246                 return -EINVAL;
247         return n;
248 }
249
250 static DEVICE_ATTR(async, 0644, async_show, async_store);
251 #endif /* CONFIG_PM_ADVANCED_DEBUG */
252
253 static struct attribute * power_attrs[] = {
254 #ifdef CONFIG_PM_RUNTIME
255         &dev_attr_control.attr,
256         &dev_attr_runtime_status.attr,
257 #endif
258         &dev_attr_wakeup.attr,
259 #ifdef CONFIG_PM_SLEEP
260         &dev_attr_wakeup_count.attr,
261 #endif
262 #ifdef CONFIG_PM_ADVANCED_DEBUG
263         &dev_attr_async.attr,
264 #ifdef CONFIG_PM_RUNTIME
265         &dev_attr_runtime_usage.attr,
266         &dev_attr_runtime_active_kids.attr,
267         &dev_attr_runtime_enabled.attr,
268 #endif
269 #endif
270         NULL,
271 };
272 static struct attribute_group pm_attr_group = {
273         .name   = "power",
274         .attrs  = power_attrs,
275 };
276
277 int dpm_sysfs_add(struct device * dev)
278 {
279         return sysfs_create_group(&dev->kobj, &pm_attr_group);
280 }
281
282 void dpm_sysfs_remove(struct device * dev)
283 {
284         sysfs_remove_group(&dev->kobj, &pm_attr_group);
285 }