8859780817e11b5e3e5484815dfca88856d539a1
[linux-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 <linux/jiffies.h>
10 #include "power.h"
11
12 /*
13  *      control - Report/change current runtime PM setting of the device
14  *
15  *      Runtime power management of a device can be blocked with the help of
16  *      this attribute.  All devices have one of the following two values for
17  *      the power/control file:
18  *
19  *       + "auto\n" to allow the device to be power managed at run time;
20  *       + "on\n" to prevent the device from being power managed at run time;
21  *
22  *      The default for all devices is "auto", which means that devices may be
23  *      subject to automatic power management, depending on their drivers.
24  *      Changing this attribute to "on" prevents the driver from power managing
25  *      the device at run time.  Doing that while the device is suspended causes
26  *      it to be woken up.
27  *
28  *      wakeup - Report/change current wakeup option for device
29  *
30  *      Some devices support "wakeup" events, which are hardware signals
31  *      used to activate devices from suspended or low power states.  Such
32  *      devices have one of three values for the sysfs power/wakeup file:
33  *
34  *       + "enabled\n" to issue the events;
35  *       + "disabled\n" not to do so; or
36  *       + "\n" for temporary or permanent inability to issue wakeup.
37  *
38  *      (For example, unconfigured USB devices can't issue wakeups.)
39  *
40  *      Familiar examples of devices that can issue wakeup events include
41  *      keyboards and mice (both PS2 and USB styles), power buttons, modems,
42  *      "Wake-On-LAN" Ethernet links, GPIO lines, and more.  Some events
43  *      will wake the entire system from a suspend state; others may just
44  *      wake up the device (if the system as a whole is already active).
45  *      Some wakeup events use normal IRQ lines; other use special out
46  *      of band signaling.
47  *
48  *      It is the responsibility of device drivers to enable (or disable)
49  *      wakeup signaling as part of changing device power states, respecting
50  *      the policy choices provided through the driver model.
51  *
52  *      Devices may not be able to generate wakeup events from all power
53  *      states.  Also, the events may be ignored in some configurations;
54  *      for example, they might need help from other devices that aren't
55  *      active, or which may have wakeup disabled.  Some drivers rely on
56  *      wakeup events internally (unless they are disabled), keeping
57  *      their hardware in low power modes whenever they're unused.  This
58  *      saves runtime power, without requiring system-wide sleep states.
59  *
60  *      async - Report/change current async suspend setting for the device
61  *
62  *      Asynchronous suspend and resume of the device during system-wide power
63  *      state transitions can be enabled by writing "enabled" to this file.
64  *      Analogously, if "disabled" is written to this file, the device will be
65  *      suspended and resumed synchronously.
66  *
67  *      All devices have one of the following two values for power/async:
68  *
69  *       + "enabled\n" to permit the asynchronous suspend/resume of the device;
70  *       + "disabled\n" to forbid it;
71  *
72  *      NOTE: It generally is unsafe to permit the asynchronous suspend/resume
73  *      of a device unless it is certain that all of the PM dependencies of the
74  *      device are known to the PM core.  However, for some devices this
75  *      attribute is set to "enabled" by bus type code or device drivers and in
76  *      that cases it should be safe to leave the default value.
77  *
78  *      wakeup_count - Report the number of wakeup events related to the device
79  */
80
81 static const char enabled[] = "enabled";
82 static const char disabled[] = "disabled";
83
84 #ifdef CONFIG_PM_RUNTIME
85 static const char ctrl_auto[] = "auto";
86 static const char ctrl_on[] = "on";
87
88 static ssize_t control_show(struct device *dev, struct device_attribute *attr,
89                             char *buf)
90 {
91         return sprintf(buf, "%s\n",
92                                 dev->power.runtime_auto ? ctrl_auto : ctrl_on);
93 }
94
95 static ssize_t control_store(struct device * dev, struct device_attribute *attr,
96                              const char * buf, size_t n)
97 {
98         char *cp;
99         int len = n;
100
101         cp = memchr(buf, '\n', n);
102         if (cp)
103                 len = cp - buf;
104         if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
105                 pm_runtime_allow(dev);
106         else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
107                 pm_runtime_forbid(dev);
108         else
109                 return -EINVAL;
110         return n;
111 }
112
113 static DEVICE_ATTR(control, 0644, control_show, control_store);
114
115 static ssize_t rtpm_active_time_show(struct device *dev,
116                                 struct device_attribute *attr, char *buf)
117 {
118         int ret;
119         spin_lock_irq(&dev->power.lock);
120         update_pm_runtime_accounting(dev);
121         ret = sprintf(buf, "%i\n", jiffies_to_msecs(dev->power.active_jiffies));
122         spin_unlock_irq(&dev->power.lock);
123         return ret;
124 }
125
126 static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
127
128 static ssize_t rtpm_suspended_time_show(struct device *dev,
129                                 struct device_attribute *attr, char *buf)
130 {
131         int ret;
132         spin_lock_irq(&dev->power.lock);
133         update_pm_runtime_accounting(dev);
134         ret = sprintf(buf, "%i\n",
135                 jiffies_to_msecs(dev->power.suspended_jiffies));
136         spin_unlock_irq(&dev->power.lock);
137         return ret;
138 }
139
140 static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
141
142 static ssize_t rtpm_status_show(struct device *dev,
143                                 struct device_attribute *attr, char *buf)
144 {
145         const char *p;
146
147         if (dev->power.runtime_error) {
148                 p = "error\n";
149         } else if (dev->power.disable_depth) {
150                 p = "unsupported\n";
151         } else {
152                 switch (dev->power.runtime_status) {
153                 case RPM_SUSPENDED:
154                         p = "suspended\n";
155                         break;
156                 case RPM_SUSPENDING:
157                         p = "suspending\n";
158                         break;
159                 case RPM_RESUMING:
160                         p = "resuming\n";
161                         break;
162                 case RPM_ACTIVE:
163                         p = "active\n";
164                         break;
165                 default:
166                         return -EIO;
167                 }
168         }
169         return sprintf(buf, p);
170 }
171
172 static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
173 #endif
174
175 static ssize_t
176 wake_show(struct device * dev, struct device_attribute *attr, char * buf)
177 {
178         return sprintf(buf, "%s\n", device_can_wakeup(dev)
179                 ? (device_may_wakeup(dev) ? enabled : disabled)
180                 : "");
181 }
182
183 static ssize_t
184 wake_store(struct device * dev, struct device_attribute *attr,
185         const char * buf, size_t n)
186 {
187         char *cp;
188         int len = n;
189
190         if (!device_can_wakeup(dev))
191                 return -EINVAL;
192
193         cp = memchr(buf, '\n', n);
194         if (cp)
195                 len = cp - buf;
196         if (len == sizeof enabled - 1
197                         && strncmp(buf, enabled, sizeof enabled - 1) == 0)
198                 device_set_wakeup_enable(dev, 1);
199         else if (len == sizeof disabled - 1
200                         && strncmp(buf, disabled, sizeof disabled - 1) == 0)
201                 device_set_wakeup_enable(dev, 0);
202         else
203                 return -EINVAL;
204         return n;
205 }
206
207 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
208
209 #ifdef CONFIG_PM_SLEEP
210 static ssize_t wakeup_count_show(struct device *dev,
211                                 struct device_attribute *attr, char *buf)
212 {
213         unsigned long count = 0;
214         bool enabled = false;
215
216         spin_lock_irq(&dev->power.lock);
217         if (dev->power.wakeup) {
218                 count = dev->power.wakeup->event_count;
219                 enabled = true;
220         }
221         spin_unlock_irq(&dev->power.lock);
222         return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
223 }
224
225 static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
226
227 static ssize_t wakeup_active_count_show(struct device *dev,
228                                 struct device_attribute *attr, char *buf)
229 {
230         unsigned long count = 0;
231         bool enabled = false;
232
233         spin_lock_irq(&dev->power.lock);
234         if (dev->power.wakeup) {
235                 count = dev->power.wakeup->active_count;
236                 enabled = true;
237         }
238         spin_unlock_irq(&dev->power.lock);
239         return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
240 }
241
242 static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
243
244 static ssize_t wakeup_hit_count_show(struct device *dev,
245                                 struct device_attribute *attr, char *buf)
246 {
247         unsigned long count = 0;
248         bool enabled = false;
249
250         spin_lock_irq(&dev->power.lock);
251         if (dev->power.wakeup) {
252                 count = dev->power.wakeup->hit_count;
253                 enabled = true;
254         }
255         spin_unlock_irq(&dev->power.lock);
256         return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
257 }
258
259 static DEVICE_ATTR(wakeup_hit_count, 0444, wakeup_hit_count_show, NULL);
260
261 static ssize_t wakeup_active_show(struct device *dev,
262                                 struct device_attribute *attr, char *buf)
263 {
264         unsigned int active = 0;
265         bool enabled = false;
266
267         spin_lock_irq(&dev->power.lock);
268         if (dev->power.wakeup) {
269                 active = dev->power.wakeup->active;
270                 enabled = true;
271         }
272         spin_unlock_irq(&dev->power.lock);
273         return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
274 }
275
276 static DEVICE_ATTR(wakeup_active, 0444, wakeup_active_show, NULL);
277
278 static ssize_t wakeup_total_time_show(struct device *dev,
279                                 struct device_attribute *attr, char *buf)
280 {
281         s64 msec = 0;
282         bool enabled = false;
283
284         spin_lock_irq(&dev->power.lock);
285         if (dev->power.wakeup) {
286                 msec = ktime_to_ms(dev->power.wakeup->total_time);
287                 enabled = true;
288         }
289         spin_unlock_irq(&dev->power.lock);
290         return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
291 }
292
293 static DEVICE_ATTR(wakeup_total_time_ms, 0444, wakeup_total_time_show, NULL);
294
295 static ssize_t wakeup_max_time_show(struct device *dev,
296                                 struct device_attribute *attr, char *buf)
297 {
298         s64 msec = 0;
299         bool enabled = false;
300
301         spin_lock_irq(&dev->power.lock);
302         if (dev->power.wakeup) {
303                 msec = ktime_to_ms(dev->power.wakeup->max_time);
304                 enabled = true;
305         }
306         spin_unlock_irq(&dev->power.lock);
307         return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
308 }
309
310 static DEVICE_ATTR(wakeup_max_time_ms, 0444, wakeup_max_time_show, NULL);
311
312 static ssize_t wakeup_last_time_show(struct device *dev,
313                                 struct device_attribute *attr, char *buf)
314 {
315         s64 msec = 0;
316         bool enabled = false;
317
318         spin_lock_irq(&dev->power.lock);
319         if (dev->power.wakeup) {
320                 msec = ktime_to_ms(dev->power.wakeup->last_time);
321                 enabled = true;
322         }
323         spin_unlock_irq(&dev->power.lock);
324         return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
325 }
326
327 static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
328 #endif /* CONFIG_PM_SLEEP */
329
330 #ifdef CONFIG_PM_ADVANCED_DEBUG
331 #ifdef CONFIG_PM_RUNTIME
332
333 static ssize_t rtpm_usagecount_show(struct device *dev,
334                                     struct device_attribute *attr, char *buf)
335 {
336         return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
337 }
338
339 static ssize_t rtpm_children_show(struct device *dev,
340                                   struct device_attribute *attr, char *buf)
341 {
342         return sprintf(buf, "%d\n", dev->power.ignore_children ?
343                 0 : atomic_read(&dev->power.child_count));
344 }
345
346 static ssize_t rtpm_enabled_show(struct device *dev,
347                                  struct device_attribute *attr, char *buf)
348 {
349         if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
350                 return sprintf(buf, "disabled & forbidden\n");
351         else if (dev->power.disable_depth)
352                 return sprintf(buf, "disabled\n");
353         else if (dev->power.runtime_auto == false)
354                 return sprintf(buf, "forbidden\n");
355         return sprintf(buf, "enabled\n");
356 }
357
358 static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
359 static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
360 static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
361
362 #endif
363
364 static ssize_t async_show(struct device *dev, struct device_attribute *attr,
365                           char *buf)
366 {
367         return sprintf(buf, "%s\n",
368                         device_async_suspend_enabled(dev) ? enabled : disabled);
369 }
370
371 static ssize_t async_store(struct device *dev, struct device_attribute *attr,
372                            const char *buf, size_t n)
373 {
374         char *cp;
375         int len = n;
376
377         cp = memchr(buf, '\n', n);
378         if (cp)
379                 len = cp - buf;
380         if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
381                 device_enable_async_suspend(dev);
382         else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
383                 device_disable_async_suspend(dev);
384         else
385                 return -EINVAL;
386         return n;
387 }
388
389 static DEVICE_ATTR(async, 0644, async_show, async_store);
390 #endif /* CONFIG_PM_ADVANCED_DEBUG */
391
392 static struct attribute * power_attrs[] = {
393 #ifdef CONFIG_PM_RUNTIME
394         &dev_attr_control.attr,
395         &dev_attr_runtime_status.attr,
396         &dev_attr_runtime_suspended_time.attr,
397         &dev_attr_runtime_active_time.attr,
398 #endif
399         &dev_attr_wakeup.attr,
400 #ifdef CONFIG_PM_SLEEP
401         &dev_attr_wakeup_count.attr,
402         &dev_attr_wakeup_active_count.attr,
403         &dev_attr_wakeup_hit_count.attr,
404         &dev_attr_wakeup_active.attr,
405         &dev_attr_wakeup_total_time_ms.attr,
406         &dev_attr_wakeup_max_time_ms.attr,
407         &dev_attr_wakeup_last_time_ms.attr,
408 #endif
409 #ifdef CONFIG_PM_ADVANCED_DEBUG
410         &dev_attr_async.attr,
411 #ifdef CONFIG_PM_RUNTIME
412         &dev_attr_runtime_usage.attr,
413         &dev_attr_runtime_active_kids.attr,
414         &dev_attr_runtime_enabled.attr,
415 #endif
416 #endif
417         NULL,
418 };
419 static struct attribute_group pm_attr_group = {
420         .name   = "power",
421         .attrs  = power_attrs,
422 };
423
424 int dpm_sysfs_add(struct device * dev)
425 {
426         return sysfs_create_group(&dev->kobj, &pm_attr_group);
427 }
428
429 void dpm_sysfs_remove(struct device * dev)
430 {
431         sysfs_remove_group(&dev->kobj, &pm_attr_group);
432 }