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 78 static const char enabled[] = "enabled"; 79 static const char disabled[] = "disabled"; 80 81 #ifdef CONFIG_PM_RUNTIME 82 static const char ctrl_auto[] = "auto"; 83 static const char ctrl_on[] = "on"; 84 85 static ssize_t control_show(struct device *dev, struct device_attribute *attr, 86 char *buf) 87 { 88 return sprintf(buf, "%s\n", 89 dev->power.runtime_auto ? ctrl_auto : ctrl_on); 90 } 91 92 static ssize_t control_store(struct device * dev, struct device_attribute *attr, 93 const char * buf, size_t n) 94 { 95 char *cp; 96 int len = n; 97 98 cp = memchr(buf, '\n', n); 99 if (cp) 100 len = cp - buf; 101 if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0) 102 pm_runtime_allow(dev); 103 else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0) 104 pm_runtime_forbid(dev); 105 else 106 return -EINVAL; 107 return n; 108 } 109 110 static DEVICE_ATTR(control, 0644, control_show, control_store); 111 #endif 112 113 static ssize_t 114 wake_show(struct device * dev, struct device_attribute *attr, char * buf) 115 { 116 return sprintf(buf, "%s\n", device_can_wakeup(dev) 117 ? (device_may_wakeup(dev) ? enabled : disabled) 118 : ""); 119 } 120 121 static ssize_t 122 wake_store(struct device * dev, struct device_attribute *attr, 123 const char * buf, size_t n) 124 { 125 char *cp; 126 int len = n; 127 128 if (!device_can_wakeup(dev)) 129 return -EINVAL; 130 131 cp = memchr(buf, '\n', n); 132 if (cp) 133 len = cp - buf; 134 if (len == sizeof enabled - 1 135 && strncmp(buf, enabled, sizeof enabled - 1) == 0) 136 device_set_wakeup_enable(dev, 1); 137 else if (len == sizeof disabled - 1 138 && strncmp(buf, disabled, sizeof disabled - 1) == 0) 139 device_set_wakeup_enable(dev, 0); 140 else 141 return -EINVAL; 142 return n; 143 } 144 145 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store); 146 147 #ifdef CONFIG_PM_ADVANCED_DEBUG 148 #ifdef CONFIG_PM_RUNTIME 149 150 static ssize_t rtpm_usagecount_show(struct device *dev, 151 struct device_attribute *attr, char *buf) 152 { 153 return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count)); 154 } 155 156 static ssize_t rtpm_children_show(struct device *dev, 157 struct device_attribute *attr, char *buf) 158 { 159 return sprintf(buf, "%d\n", dev->power.ignore_children ? 160 0 : atomic_read(&dev->power.child_count)); 161 } 162 163 static ssize_t rtpm_enabled_show(struct device *dev, 164 struct device_attribute *attr, char *buf) 165 { 166 if ((dev->power.disable_depth) && (dev->power.runtime_auto == false)) 167 return sprintf(buf, "disabled & forbidden\n"); 168 else if (dev->power.disable_depth) 169 return sprintf(buf, "disabled\n"); 170 else if (dev->power.runtime_auto == false) 171 return sprintf(buf, "forbidden\n"); 172 return sprintf(buf, "enabled\n"); 173 } 174 175 static ssize_t rtpm_status_show(struct device *dev, 176 struct device_attribute *attr, char *buf) 177 { 178 if (dev->power.runtime_error) 179 return sprintf(buf, "error\n"); 180 switch (dev->power.runtime_status) { 181 case RPM_SUSPENDED: 182 return sprintf(buf, "suspended\n"); 183 case RPM_SUSPENDING: 184 return sprintf(buf, "suspending\n"); 185 case RPM_RESUMING: 186 return sprintf(buf, "resuming\n"); 187 case RPM_ACTIVE: 188 return sprintf(buf, "active\n"); 189 } 190 return -EIO; 191 } 192 193 static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL); 194 static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL); 195 static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL); 196 static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL); 197 198 #endif 199 200 static ssize_t async_show(struct device *dev, struct device_attribute *attr, 201 char *buf) 202 { 203 return sprintf(buf, "%s\n", 204 device_async_suspend_enabled(dev) ? enabled : disabled); 205 } 206 207 static ssize_t async_store(struct device *dev, struct device_attribute *attr, 208 const char *buf, size_t n) 209 { 210 char *cp; 211 int len = n; 212 213 cp = memchr(buf, '\n', n); 214 if (cp) 215 len = cp - buf; 216 if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0) 217 device_enable_async_suspend(dev); 218 else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0) 219 device_disable_async_suspend(dev); 220 else 221 return -EINVAL; 222 return n; 223 } 224 225 static DEVICE_ATTR(async, 0644, async_show, async_store); 226 #endif /* CONFIG_PM_ADVANCED_DEBUG */ 227 228 static struct attribute * power_attrs[] = { 229 #ifdef CONFIG_PM_RUNTIME 230 &dev_attr_control.attr, 231 #endif 232 &dev_attr_wakeup.attr, 233 #ifdef CONFIG_PM_ADVANCED_DEBUG 234 &dev_attr_async.attr, 235 #ifdef CONFIG_PM_RUNTIME 236 &dev_attr_runtime_usage.attr, 237 &dev_attr_runtime_active_kids.attr, 238 &dev_attr_runtime_status.attr, 239 &dev_attr_runtime_enabled.attr, 240 #endif 241 #endif 242 NULL, 243 }; 244 static struct attribute_group pm_attr_group = { 245 .name = "power", 246 .attrs = power_attrs, 247 }; 248 249 int dpm_sysfs_add(struct device * dev) 250 { 251 return sysfs_create_group(&dev->kobj, &pm_attr_group); 252 } 253 254 void dpm_sysfs_remove(struct device * dev) 255 { 256 sysfs_remove_group(&dev->kobj, &pm_attr_group); 257 } 258