| 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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include "power.h" | 
 | 8 |  | 
 | 9 |  | 
| David Brownell | 2bca293 | 2006-08-30 13:54:36 -0700 | [diff] [blame] | 10 | #ifdef	CONFIG_PM_SYSFS_DEPRECATED | 
 | 11 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | /** | 
 | 13 |  *	state - Control current power state of device | 
 | 14 |  * | 
 | 15 |  *	show() returns the current power state of the device. '0' indicates | 
| David Brownell | 047bda3 | 2006-08-30 14:12:48 -0700 | [diff] [blame] | 16 |  *	the device is on. Other values (2) indicate the device is in some low | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 |  *	power state. | 
 | 18 |  * | 
| David Brownell | 047bda3 | 2006-08-30 14:12:48 -0700 | [diff] [blame] | 19 |  *	store() sets the current power state, which is an integer valued | 
 | 20 |  *	0, 2, or 3.  Devices with bus.suspend_late(), or bus.resume_early() | 
 | 21 |  *	methods fail this operation; those methods couldn't be called. | 
 | 22 |  *	Otherwise, | 
 | 23 |  * | 
 | 24 |  *	- If the recorded dev->power.power_state.event matches the | 
 | 25 |  *	  target value, nothing is done. | 
 | 26 |  *	- If the recorded event code is nonzero, the device is reactivated | 
 | 27 |  *	  by calling bus.resume() and/or class.resume(). | 
 | 28 |  *	- If the target value is nonzero, the device is suspended by | 
 | 29 |  *	  calling class.suspend() and/or bus.suspend() with event code | 
 | 30 |  *	  PM_EVENT_SUSPEND. | 
 | 31 |  * | 
 | 32 |  *	This mechanism is DEPRECATED and should only be used for testing. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 |  */ | 
 | 34 |  | 
| Yani Ioannou | 74880c0 | 2005-05-17 06:41:12 -0400 | [diff] [blame] | 35 | static ssize_t state_show(struct device * dev, struct device_attribute *attr, char * buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { | 
| Pavel Machek | 022f7b0 | 2006-01-22 22:38:52 +0100 | [diff] [blame] | 37 | 	if (dev->power.power_state.event) | 
 | 38 | 		return sprintf(buf, "2\n"); | 
 | 39 | 	else | 
 | 40 | 		return sprintf(buf, "0\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } | 
 | 42 |  | 
| Yani Ioannou | 74880c0 | 2005-05-17 06:41:12 -0400 | [diff] [blame] | 43 | static ssize_t state_store(struct device * dev, struct device_attribute *attr, const char * buf, size_t n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { | 
| Pavel Machek | ca078ba | 2005-09-03 15:56:57 -0700 | [diff] [blame] | 45 | 	pm_message_t state; | 
| Pavel Machek | 022f7b0 | 2006-01-22 22:38:52 +0100 | [diff] [blame] | 46 | 	int error = -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 |  | 
| David Brownell | 047bda3 | 2006-08-30 14:12:48 -0700 | [diff] [blame] | 48 | 	/* disallow incomplete suspend sequences */ | 
 | 49 | 	if (dev->bus && (dev->bus->suspend_late || dev->bus->resume_early)) | 
 | 50 | 		return error; | 
 | 51 |  | 
| Pavel Machek | 022f7b0 | 2006-01-22 22:38:52 +0100 | [diff] [blame] | 52 | 	state.event = PM_EVENT_SUSPEND; | 
 | 53 | 	/* Older apps expected to write "3" here - confused with PCI D3 */ | 
 | 54 | 	if ((n == 1) && !strcmp(buf, "3")) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | 		error = dpm_runtime_suspend(dev, state); | 
| Pavel Machek | 022f7b0 | 2006-01-22 22:38:52 +0100 | [diff] [blame] | 56 |  | 
 | 57 | 	if ((n == 1) && !strcmp(buf, "2")) | 
 | 58 | 		error = dpm_runtime_suspend(dev, state); | 
 | 59 |  | 
 | 60 | 	if ((n == 1) && !strcmp(buf, "0")) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | 		dpm_runtime_resume(dev); | 
| Pavel Machek | 022f7b0 | 2006-01-22 22:38:52 +0100 | [diff] [blame] | 62 | 		error = 0; | 
 | 63 | 	} | 
 | 64 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | 	return error ? error : n; | 
 | 66 | } | 
 | 67 |  | 
 | 68 | static DEVICE_ATTR(state, 0644, state_show, state_store); | 
 | 69 |  | 
 | 70 |  | 
| David Brownell | 2bca293 | 2006-08-30 13:54:36 -0700 | [diff] [blame] | 71 | #endif	/* CONFIG_PM_SYSFS_DEPRECATED */ | 
 | 72 |  | 
| David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 73 | /* | 
 | 74 |  *	wakeup - Report/change current wakeup option for device | 
 | 75 |  * | 
 | 76 |  *	Some devices support "wakeup" events, which are hardware signals | 
 | 77 |  *	used to activate devices from suspended or low power states.  Such | 
 | 78 |  *	devices have one of three values for the sysfs power/wakeup file: | 
 | 79 |  * | 
 | 80 |  *	 + "enabled\n" to issue the events; | 
 | 81 |  *	 + "disabled\n" not to do so; or | 
 | 82 |  *	 + "\n" for temporary or permanent inability to issue wakeup. | 
 | 83 |  * | 
 | 84 |  *	(For example, unconfigured USB devices can't issue wakeups.) | 
 | 85 |  * | 
 | 86 |  *	Familiar examples of devices that can issue wakeup events include | 
 | 87 |  *	keyboards and mice (both PS2 and USB styles), power buttons, modems, | 
 | 88 |  *	"Wake-On-LAN" Ethernet links, GPIO lines, and more.  Some events | 
 | 89 |  *	will wake the entire system from a suspend state; others may just | 
 | 90 |  *	wake up the device (if the system as a whole is already active). | 
 | 91 |  *	Some wakeup events use normal IRQ lines; other use special out | 
 | 92 |  *	of band signaling. | 
 | 93 |  * | 
 | 94 |  *	It is the responsibility of device drivers to enable (or disable) | 
 | 95 |  *	wakeup signaling as part of changing device power states, respecting | 
 | 96 |  *	the policy choices provided through the driver model. | 
 | 97 |  * | 
 | 98 |  *	Devices may not be able to generate wakeup events from all power | 
 | 99 |  *	states.  Also, the events may be ignored in some configurations; | 
 | 100 |  *	for example, they might need help from other devices that aren't | 
 | 101 |  *	active, or which may have wakeup disabled.  Some drivers rely on | 
 | 102 |  *	wakeup events internally (unless they are disabled), keeping | 
 | 103 |  *	their hardware in low power modes whenever they're unused.  This | 
 | 104 |  *	saves runtime power, without requiring system-wide sleep states. | 
 | 105 |  */ | 
 | 106 |  | 
 | 107 | static const char enabled[] = "enabled"; | 
 | 108 | static const char disabled[] = "disabled"; | 
 | 109 |  | 
 | 110 | static ssize_t | 
 | 111 | wake_show(struct device * dev, struct device_attribute *attr, char * buf) | 
 | 112 | { | 
 | 113 | 	return sprintf(buf, "%s\n", device_can_wakeup(dev) | 
 | 114 | 		? (device_may_wakeup(dev) ? enabled : disabled) | 
 | 115 | 		: ""); | 
 | 116 | } | 
 | 117 |  | 
 | 118 | static ssize_t | 
 | 119 | wake_store(struct device * dev, struct device_attribute *attr, | 
 | 120 | 	const char * buf, size_t n) | 
 | 121 | { | 
 | 122 | 	char *cp; | 
 | 123 | 	int len = n; | 
 | 124 |  | 
 | 125 | 	if (!device_can_wakeup(dev)) | 
 | 126 | 		return -EINVAL; | 
 | 127 |  | 
 | 128 | 	cp = memchr(buf, '\n', n); | 
 | 129 | 	if (cp) | 
 | 130 | 		len = cp - buf; | 
 | 131 | 	if (len == sizeof enabled - 1 | 
 | 132 | 			&& strncmp(buf, enabled, sizeof enabled - 1) == 0) | 
 | 133 | 		device_set_wakeup_enable(dev, 1); | 
 | 134 | 	else if (len == sizeof disabled - 1 | 
 | 135 | 			&& strncmp(buf, disabled, sizeof disabled - 1) == 0) | 
 | 136 | 		device_set_wakeup_enable(dev, 0); | 
 | 137 | 	else | 
 | 138 | 		return -EINVAL; | 
 | 139 | 	return n; | 
 | 140 | } | 
 | 141 |  | 
 | 142 | static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store); | 
 | 143 |  | 
 | 144 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | static struct attribute * power_attrs[] = { | 
| David Brownell | 2bca293 | 2006-08-30 13:54:36 -0700 | [diff] [blame] | 146 | #ifdef	CONFIG_PM_SYSFS_DEPRECATED | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | 	&dev_attr_state.attr, | 
| David Brownell | 2bca293 | 2006-08-30 13:54:36 -0700 | [diff] [blame] | 148 | #endif | 
| David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 149 | 	&dev_attr_wakeup.attr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | 	NULL, | 
 | 151 | }; | 
 | 152 | static struct attribute_group pm_attr_group = { | 
 | 153 | 	.name	= "power", | 
 | 154 | 	.attrs	= power_attrs, | 
 | 155 | }; | 
 | 156 |  | 
 | 157 | int dpm_sysfs_add(struct device * dev) | 
 | 158 | { | 
 | 159 | 	return sysfs_create_group(&dev->kobj, &pm_attr_group); | 
 | 160 | } | 
 | 161 |  | 
 | 162 | void dpm_sysfs_remove(struct device * dev) | 
 | 163 | { | 
 | 164 | 	sysfs_remove_group(&dev->kobj, &pm_attr_group); | 
 | 165 | } |