um: call free_irq() only on enabled channels
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / base / power / sysfs.c
blobe56b4388fe61004a1f85e0209cbc59bd60f4d1fb
1 /*
2 * drivers/base/power/sysfs.c - sysfs entries for device PM
3 */
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"
13 * control - Report/change current runtime PM setting of the device
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:
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;
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.
28 * wakeup - Report/change current wakeup option for device
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:
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.
38 * (For example, unconfigured USB devices can't issue wakeups.)
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.
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.
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.
60 * async - Report/change current async suspend setting for the device
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.
67 * All devices have one of the following two values for power/async:
69 * + "enabled\n" to permit the asynchronous suspend/resume of the device;
70 * + "disabled\n" to forbid it;
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.
78 * wakeup_count - Report the number of wakeup events related to the device
81 static const char enabled[] = "enabled";
82 static const char disabled[] = "disabled";
84 #ifdef CONFIG_PM_RUNTIME
85 static const char ctrl_auto[] = "auto";
86 static const char ctrl_on[] = "on";
88 static ssize_t control_show(struct device *dev, struct device_attribute *attr,
89 char *buf)
91 return sprintf(buf, "%s\n",
92 dev->power.runtime_auto ? ctrl_auto : ctrl_on);
95 static ssize_t control_store(struct device * dev, struct device_attribute *attr,
96 const char * buf, size_t n)
98 char *cp;
99 int len = n;
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;
113 static DEVICE_ATTR(control, 0644, control_show, control_store);
115 static ssize_t rtpm_active_time_show(struct device *dev,
116 struct device_attribute *attr, char *buf)
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;
126 static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
128 static ssize_t rtpm_suspended_time_show(struct device *dev,
129 struct device_attribute *attr, char *buf)
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;
140 static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
142 static ssize_t rtpm_status_show(struct device *dev,
143 struct device_attribute *attr, char *buf)
145 const char *p;
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;
169 return sprintf(buf, p);
172 static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
173 #endif
175 static ssize_t
176 wake_show(struct device * dev, struct device_attribute *attr, char * buf)
178 return sprintf(buf, "%s\n", device_can_wakeup(dev)
179 ? (device_may_wakeup(dev) ? enabled : disabled)
180 : "");
183 static ssize_t
184 wake_store(struct device * dev, struct device_attribute *attr,
185 const char * buf, size_t n)
187 char *cp;
188 int len = n;
190 if (!device_can_wakeup(dev))
191 return -EINVAL;
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;
207 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
209 #ifdef CONFIG_PM_SLEEP
210 static ssize_t wakeup_count_show(struct device *dev,
211 struct device_attribute *attr, char *buf)
213 return sprintf(buf, "%lu\n", dev->power.wakeup_count);
216 static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
217 #endif
219 #ifdef CONFIG_PM_ADVANCED_DEBUG
220 #ifdef CONFIG_PM_RUNTIME
222 static ssize_t rtpm_usagecount_show(struct device *dev,
223 struct device_attribute *attr, char *buf)
225 return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
228 static ssize_t rtpm_children_show(struct device *dev,
229 struct device_attribute *attr, char *buf)
231 return sprintf(buf, "%d\n", dev->power.ignore_children ?
232 0 : atomic_read(&dev->power.child_count));
235 static ssize_t rtpm_enabled_show(struct device *dev,
236 struct device_attribute *attr, char *buf)
238 if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
239 return sprintf(buf, "disabled & forbidden\n");
240 else if (dev->power.disable_depth)
241 return sprintf(buf, "disabled\n");
242 else if (dev->power.runtime_auto == false)
243 return sprintf(buf, "forbidden\n");
244 return sprintf(buf, "enabled\n");
247 static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
248 static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
249 static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
251 #endif
253 static ssize_t async_show(struct device *dev, struct device_attribute *attr,
254 char *buf)
256 return sprintf(buf, "%s\n",
257 device_async_suspend_enabled(dev) ? enabled : disabled);
260 static ssize_t async_store(struct device *dev, struct device_attribute *attr,
261 const char *buf, size_t n)
263 char *cp;
264 int len = n;
266 cp = memchr(buf, '\n', n);
267 if (cp)
268 len = cp - buf;
269 if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
270 device_enable_async_suspend(dev);
271 else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
272 device_disable_async_suspend(dev);
273 else
274 return -EINVAL;
275 return n;
278 static DEVICE_ATTR(async, 0644, async_show, async_store);
279 #endif /* CONFIG_PM_ADVANCED_DEBUG */
281 static struct attribute * power_attrs[] = {
282 #ifdef CONFIG_PM_RUNTIME
283 &dev_attr_control.attr,
284 &dev_attr_runtime_status.attr,
285 &dev_attr_runtime_suspended_time.attr,
286 &dev_attr_runtime_active_time.attr,
287 #endif
288 &dev_attr_wakeup.attr,
289 #ifdef CONFIG_PM_SLEEP
290 &dev_attr_wakeup_count.attr,
291 #endif
292 #ifdef CONFIG_PM_ADVANCED_DEBUG
293 &dev_attr_async.attr,
294 #ifdef CONFIG_PM_RUNTIME
295 &dev_attr_runtime_usage.attr,
296 &dev_attr_runtime_active_kids.attr,
297 &dev_attr_runtime_enabled.attr,
298 #endif
299 #endif
300 NULL,
302 static struct attribute_group pm_attr_group = {
303 .name = "power",
304 .attrs = power_attrs,
307 int dpm_sysfs_add(struct device * dev)
309 return sysfs_create_group(&dev->kobj, &pm_attr_group);
312 void dpm_sysfs_remove(struct device * dev)
314 sysfs_remove_group(&dev->kobj, &pm_attr_group);