Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/power/sysfs.c - sysfs entries for device PM |
| 3 | */ |
| 4 | |
| 5 | #include <linux/device.h> |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 6 | #include <linux/string.h> |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 7 | #include <linux/pm_runtime.h> |
Dominik Brodowski | c92445f | 2010-04-23 20:32:23 +0200 | [diff] [blame] | 8 | #include <asm/atomic.h> |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 9 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include "power.h" |
| 11 | |
David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 12 | /* |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 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 | * |
David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 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. |
Rafael J. Wysocki | 5a2eb85 | 2010-01-23 22:25:23 +0100 | [diff] [blame] | 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. |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 77 | * |
| 78 | * wakeup_count - Report the number of wakeup events related to the device |
David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 79 | */ |
| 80 | |
| 81 | static const char enabled[] = "enabled"; |
| 82 | static const char disabled[] = "disabled"; |
| 83 | |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 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); |
Alan Stern | 0fcb4ee | 2010-07-08 00:05:37 +0200 | [diff] [blame] | 114 | |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 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 | |
Alan Stern | 0fcb4ee | 2010-07-08 00:05:37 +0200 | [diff] [blame] | 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); |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 173 | #endif |
| 174 | |
David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 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 | |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 209 | #ifdef CONFIG_PM_SLEEP |
| 210 | static ssize_t wakeup_count_show(struct device *dev, |
| 211 | struct device_attribute *attr, char *buf) |
| 212 | { |
| 213 | return sprintf(buf, "%lu\n", dev->power.wakeup_count); |
| 214 | } |
| 215 | |
| 216 | static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL); |
| 217 | #endif |
| 218 | |
Dominik Brodowski | c92445f | 2010-04-23 20:32:23 +0200 | [diff] [blame] | 219 | #ifdef CONFIG_PM_ADVANCED_DEBUG |
| 220 | #ifdef CONFIG_PM_RUNTIME |
| 221 | |
| 222 | static ssize_t rtpm_usagecount_show(struct device *dev, |
| 223 | struct device_attribute *attr, char *buf) |
| 224 | { |
| 225 | return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count)); |
| 226 | } |
| 227 | |
| 228 | static ssize_t rtpm_children_show(struct device *dev, |
| 229 | struct device_attribute *attr, char *buf) |
| 230 | { |
| 231 | return sprintf(buf, "%d\n", dev->power.ignore_children ? |
| 232 | 0 : atomic_read(&dev->power.child_count)); |
| 233 | } |
| 234 | |
| 235 | static ssize_t rtpm_enabled_show(struct device *dev, |
| 236 | struct device_attribute *attr, char *buf) |
| 237 | { |
| 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"); |
| 245 | } |
| 246 | |
Dominik Brodowski | c92445f | 2010-04-23 20:32:23 +0200 | [diff] [blame] | 247 | static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL); |
| 248 | static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL); |
Dominik Brodowski | c92445f | 2010-04-23 20:32:23 +0200 | [diff] [blame] | 249 | static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL); |
| 250 | |
| 251 | #endif |
| 252 | |
Rafael J. Wysocki | 5a2eb85 | 2010-01-23 22:25:23 +0100 | [diff] [blame] | 253 | static ssize_t async_show(struct device *dev, struct device_attribute *attr, |
| 254 | char *buf) |
| 255 | { |
| 256 | return sprintf(buf, "%s\n", |
| 257 | device_async_suspend_enabled(dev) ? enabled : disabled); |
| 258 | } |
| 259 | |
| 260 | static ssize_t async_store(struct device *dev, struct device_attribute *attr, |
| 261 | const char *buf, size_t n) |
| 262 | { |
| 263 | char *cp; |
| 264 | int len = n; |
| 265 | |
| 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; |
| 276 | } |
| 277 | |
| 278 | static DEVICE_ATTR(async, 0644, async_show, async_store); |
Dominik Brodowski | c92445f | 2010-04-23 20:32:23 +0200 | [diff] [blame] | 279 | #endif /* CONFIG_PM_ADVANCED_DEBUG */ |
David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | static struct attribute * power_attrs[] = { |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 282 | #ifdef CONFIG_PM_RUNTIME |
| 283 | &dev_attr_control.attr, |
Alan Stern | 0fcb4ee | 2010-07-08 00:05:37 +0200 | [diff] [blame] | 284 | &dev_attr_runtime_status.attr, |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 285 | &dev_attr_runtime_suspended_time.attr, |
| 286 | &dev_attr_runtime_active_time.attr, |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 287 | #endif |
David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 288 | &dev_attr_wakeup.attr, |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 289 | #ifdef CONFIG_PM_SLEEP |
| 290 | &dev_attr_wakeup_count.attr, |
| 291 | #endif |
Dominik Brodowski | c92445f | 2010-04-23 20:32:23 +0200 | [diff] [blame] | 292 | #ifdef CONFIG_PM_ADVANCED_DEBUG |
Rafael J. Wysocki | 5a2eb85 | 2010-01-23 22:25:23 +0100 | [diff] [blame] | 293 | &dev_attr_async.attr, |
Dominik Brodowski | c92445f | 2010-04-23 20:32:23 +0200 | [diff] [blame] | 294 | #ifdef CONFIG_PM_RUNTIME |
| 295 | &dev_attr_runtime_usage.attr, |
| 296 | &dev_attr_runtime_active_kids.attr, |
Dominik Brodowski | c92445f | 2010-04-23 20:32:23 +0200 | [diff] [blame] | 297 | &dev_attr_runtime_enabled.attr, |
| 298 | #endif |
Rafael J. Wysocki | 5a2eb85 | 2010-01-23 22:25:23 +0100 | [diff] [blame] | 299 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | NULL, |
| 301 | }; |
| 302 | static struct attribute_group pm_attr_group = { |
| 303 | .name = "power", |
| 304 | .attrs = power_attrs, |
| 305 | }; |
| 306 | |
| 307 | int dpm_sysfs_add(struct device * dev) |
| 308 | { |
| 309 | return sysfs_create_group(&dev->kobj, &pm_attr_group); |
| 310 | } |
| 311 | |
| 312 | void dpm_sysfs_remove(struct device * dev) |
| 313 | { |
| 314 | sysfs_remove_group(&dev->kobj, &pm_attr_group); |
| 315 | } |