| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * kernel/power/main.c - PM subsystem core functionality. | 
 | 3 |  * | 
 | 4 |  * Copyright (c) 2003 Patrick Mochel | 
 | 5 |  * Copyright (c) 2003 Open Source Development Lab | 
 | 6 |  *  | 
 | 7 |  * This file is released under the GPLv2 | 
 | 8 |  * | 
 | 9 |  */ | 
 | 10 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/kobject.h> | 
 | 12 | #include <linux/string.h> | 
| Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 13 | #include <linux/resume-trace.h> | 
| Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 14 | #include <linux/workqueue.h> | 
| Amar Singhal | 97d68a8 | 2012-03-20 19:03:04 -0700 | [diff] [blame] | 15 | #include <linux/hrtimer.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 |  | 
 | 17 | #include "power.h" | 
 | 18 |  | 
| Amar Singhal | ac2a6d6 | 2012-02-27 17:40:31 -0800 | [diff] [blame] | 19 | #define MAX_BUF 100 | 
 | 20 |  | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 21 | DEFINE_MUTEX(pm_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 |  | 
| Rafael J. Wysocki | cd51e61 | 2011-02-11 00:04:52 +0100 | [diff] [blame] | 23 | #ifdef CONFIG_PM_SLEEP | 
 | 24 |  | 
| Alan Stern | 8252575 | 2007-11-19 23:49:18 +0100 | [diff] [blame] | 25 | /* Routines for PM-transition notifications */ | 
 | 26 |  | 
 | 27 | static BLOCKING_NOTIFIER_HEAD(pm_chain_head); | 
 | 28 |  | 
| Amar Singhal | 97d68a8 | 2012-03-20 19:03:04 -0700 | [diff] [blame] | 29 | static void touch_event_fn(struct work_struct *work); | 
 | 30 | static DECLARE_WORK(touch_event_struct, touch_event_fn); | 
 | 31 |  | 
 | 32 | static struct hrtimer tc_ev_timer; | 
 | 33 | static int tc_ev_processed; | 
| Amar Singhal | ac2a6d6 | 2012-02-27 17:40:31 -0800 | [diff] [blame] | 34 | static ktime_t touch_evt_timer_val; | 
 | 35 |  | 
| Alan Stern | 8252575 | 2007-11-19 23:49:18 +0100 | [diff] [blame] | 36 | int register_pm_notifier(struct notifier_block *nb) | 
 | 37 | { | 
 | 38 | 	return blocking_notifier_chain_register(&pm_chain_head, nb); | 
 | 39 | } | 
 | 40 | EXPORT_SYMBOL_GPL(register_pm_notifier); | 
 | 41 |  | 
 | 42 | int unregister_pm_notifier(struct notifier_block *nb) | 
 | 43 | { | 
 | 44 | 	return blocking_notifier_chain_unregister(&pm_chain_head, nb); | 
 | 45 | } | 
 | 46 | EXPORT_SYMBOL_GPL(unregister_pm_notifier); | 
 | 47 |  | 
 | 48 | int pm_notifier_call_chain(unsigned long val) | 
 | 49 | { | 
 | 50 | 	return (blocking_notifier_call_chain(&pm_chain_head, val, NULL) | 
 | 51 | 			== NOTIFY_BAD) ? -EINVAL : 0; | 
 | 52 | } | 
 | 53 |  | 
| Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 54 | /* If set, devices may be suspended and resumed asynchronously. */ | 
 | 55 | int pm_async_enabled = 1; | 
 | 56 |  | 
 | 57 | static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 58 | 			     char *buf) | 
 | 59 | { | 
 | 60 | 	return sprintf(buf, "%d\n", pm_async_enabled); | 
 | 61 | } | 
 | 62 |  | 
 | 63 | static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 64 | 			      const char *buf, size_t n) | 
 | 65 | { | 
 | 66 | 	unsigned long val; | 
 | 67 |  | 
 | 68 | 	if (strict_strtoul(buf, 10, &val)) | 
 | 69 | 		return -EINVAL; | 
 | 70 |  | 
 | 71 | 	if (val > 1) | 
 | 72 | 		return -EINVAL; | 
 | 73 |  | 
 | 74 | 	pm_async_enabled = val; | 
 | 75 | 	return n; | 
 | 76 | } | 
 | 77 |  | 
 | 78 | power_attr(pm_async); | 
 | 79 |  | 
| Amar Singhal | ac2a6d6 | 2012-02-27 17:40:31 -0800 | [diff] [blame] | 80 | static ssize_t | 
 | 81 | touch_event_show(struct kobject *kobj, | 
 | 82 | 		 struct kobj_attribute *attr, char *buf) | 
 | 83 | { | 
| Amar Singhal | 97d68a8 | 2012-03-20 19:03:04 -0700 | [diff] [blame] | 84 | 	if (tc_ev_processed == 0) | 
| Amar Singhal | ac2a6d6 | 2012-02-27 17:40:31 -0800 | [diff] [blame] | 85 | 		return snprintf(buf, strnlen("touch_event", MAX_BUF) + 1, | 
 | 86 | 				"touch_event"); | 
 | 87 | 	else | 
 | 88 | 		return snprintf(buf, strnlen("null", MAX_BUF) + 1, | 
 | 89 | 				"null"); | 
 | 90 | } | 
 | 91 |  | 
 | 92 | static ssize_t | 
 | 93 | touch_event_store(struct kobject *kobj, | 
 | 94 | 		  struct kobj_attribute *attr, | 
 | 95 | 		  const char *buf, size_t n) | 
 | 96 | { | 
 | 97 |  | 
| Amar Singhal | 97d68a8 | 2012-03-20 19:03:04 -0700 | [diff] [blame] | 98 | 	hrtimer_cancel(&tc_ev_timer); | 
 | 99 | 	tc_ev_processed = 0; | 
| Amar Singhal | ac2a6d6 | 2012-02-27 17:40:31 -0800 | [diff] [blame] | 100 |  | 
 | 101 | 	/* set a timer to notify the userspace to stop processing | 
 | 102 | 	 * touch event | 
 | 103 | 	 */ | 
| Amar Singhal | 97d68a8 | 2012-03-20 19:03:04 -0700 | [diff] [blame] | 104 | 	hrtimer_start(&tc_ev_timer, touch_evt_timer_val, HRTIMER_MODE_REL); | 
| Amar Singhal | ac2a6d6 | 2012-02-27 17:40:31 -0800 | [diff] [blame] | 105 |  | 
 | 106 | 	/* wakeup the userspace poll */ | 
 | 107 | 	sysfs_notify(kobj, NULL, "touch_event"); | 
 | 108 |  | 
 | 109 | 	return n; | 
 | 110 | } | 
 | 111 |  | 
 | 112 | power_attr(touch_event); | 
 | 113 |  | 
 | 114 | static ssize_t | 
 | 115 | touch_event_timer_show(struct kobject *kobj, | 
 | 116 | 		 struct kobj_attribute *attr, char *buf) | 
 | 117 | { | 
 | 118 | 	return snprintf(buf, MAX_BUF, "%lld", touch_evt_timer_val.tv64); | 
 | 119 | } | 
 | 120 |  | 
 | 121 | static ssize_t | 
 | 122 | touch_event_timer_store(struct kobject *kobj, | 
 | 123 | 			struct kobj_attribute *attr, | 
 | 124 | 			const char *buf, size_t n) | 
 | 125 | { | 
 | 126 | 	unsigned long val; | 
 | 127 |  | 
 | 128 | 	if (strict_strtoul(buf, 10, &val)) | 
 | 129 | 		return -EINVAL; | 
 | 130 |  | 
 | 131 | 	touch_evt_timer_val = ktime_set(0, val*1000); | 
 | 132 |  | 
 | 133 | 	return n; | 
 | 134 | } | 
 | 135 |  | 
 | 136 | power_attr(touch_event_timer); | 
 | 137 |  | 
| Amar Singhal | 97d68a8 | 2012-03-20 19:03:04 -0700 | [diff] [blame] | 138 | static void touch_event_fn(struct work_struct *work) | 
| Amar Singhal | ac2a6d6 | 2012-02-27 17:40:31 -0800 | [diff] [blame] | 139 | { | 
 | 140 | 	/* wakeup the userspace poll */ | 
| Amar Singhal | 97d68a8 | 2012-03-20 19:03:04 -0700 | [diff] [blame] | 141 | 	tc_ev_processed = 1; | 
| Amar Singhal | ac2a6d6 | 2012-02-27 17:40:31 -0800 | [diff] [blame] | 142 | 	sysfs_notify(power_kobj, NULL, "touch_event"); | 
| Amar Singhal | 97d68a8 | 2012-03-20 19:03:04 -0700 | [diff] [blame] | 143 |  | 
 | 144 | 	return; | 
 | 145 | } | 
 | 146 |  | 
 | 147 | static enum hrtimer_restart tc_ev_stop(struct hrtimer *hrtimer) | 
 | 148 | { | 
 | 149 |  | 
 | 150 | 	schedule_work(&touch_event_struct); | 
 | 151 |  | 
| Amar Singhal | ac2a6d6 | 2012-02-27 17:40:31 -0800 | [diff] [blame] | 152 | 	return HRTIMER_NORESTART; | 
 | 153 | } | 
 | 154 |  | 
| Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 155 | #ifdef CONFIG_PM_DEBUG | 
 | 156 | int pm_test_level = TEST_NONE; | 
 | 157 |  | 
| Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 158 | static const char * const pm_tests[__TEST_AFTER_LAST] = { | 
 | 159 | 	[TEST_NONE] = "none", | 
 | 160 | 	[TEST_CORE] = "core", | 
 | 161 | 	[TEST_CPUS] = "processors", | 
 | 162 | 	[TEST_PLATFORM] = "platform", | 
 | 163 | 	[TEST_DEVICES] = "devices", | 
 | 164 | 	[TEST_FREEZER] = "freezer", | 
 | 165 | }; | 
 | 166 |  | 
| Rafael J. Wysocki | 039a75c | 2008-01-29 00:29:06 +0100 | [diff] [blame] | 167 | static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 168 | 				char *buf) | 
| Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 169 | { | 
 | 170 | 	char *s = buf; | 
 | 171 | 	int level; | 
 | 172 |  | 
 | 173 | 	for (level = TEST_FIRST; level <= TEST_MAX; level++) | 
 | 174 | 		if (pm_tests[level]) { | 
 | 175 | 			if (level == pm_test_level) | 
 | 176 | 				s += sprintf(s, "[%s] ", pm_tests[level]); | 
 | 177 | 			else | 
 | 178 | 				s += sprintf(s, "%s ", pm_tests[level]); | 
 | 179 | 		} | 
 | 180 |  | 
 | 181 | 	if (s != buf) | 
 | 182 | 		/* convert the last space to a newline */ | 
 | 183 | 		*(s-1) = '\n'; | 
 | 184 |  | 
 | 185 | 	return (s - buf); | 
 | 186 | } | 
 | 187 |  | 
| Rafael J. Wysocki | 039a75c | 2008-01-29 00:29:06 +0100 | [diff] [blame] | 188 | static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 189 | 				const char *buf, size_t n) | 
| Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 190 | { | 
 | 191 | 	const char * const *s; | 
 | 192 | 	int level; | 
 | 193 | 	char *p; | 
 | 194 | 	int len; | 
 | 195 | 	int error = -EINVAL; | 
 | 196 |  | 
 | 197 | 	p = memchr(buf, '\n', n); | 
 | 198 | 	len = p ? p - buf : n; | 
 | 199 |  | 
 | 200 | 	mutex_lock(&pm_mutex); | 
 | 201 |  | 
 | 202 | 	level = TEST_FIRST; | 
 | 203 | 	for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++) | 
 | 204 | 		if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) { | 
 | 205 | 			pm_test_level = level; | 
 | 206 | 			error = 0; | 
 | 207 | 			break; | 
 | 208 | 		} | 
 | 209 |  | 
 | 210 | 	mutex_unlock(&pm_mutex); | 
 | 211 |  | 
 | 212 | 	return error ? error : n; | 
 | 213 | } | 
 | 214 |  | 
 | 215 | power_attr(pm_test); | 
| Rafael J. Wysocki | 091d71e | 2009-01-17 00:10:45 +0100 | [diff] [blame] | 216 | #endif /* CONFIG_PM_DEBUG */ | 
| Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 217 |  | 
| Rafael J. Wysocki | 7671b8a | 2007-12-14 01:07:13 +0100 | [diff] [blame] | 218 | #endif /* CONFIG_PM_SLEEP */ | 
| Rafael J. Wysocki | 039a75c | 2008-01-29 00:29:06 +0100 | [diff] [blame] | 219 |  | 
| Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 220 | struct kobject *power_kobj; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 |  | 
 | 222 | /** | 
 | 223 |  *	state - control system power state. | 
 | 224 |  * | 
 | 225 |  *	show() returns what states are supported, which is hard-coded to | 
 | 226 |  *	'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and | 
 | 227 |  *	'disk' (Suspend-to-Disk). | 
 | 228 |  * | 
 | 229 |  *	store() accepts one of those strings, translates it into the  | 
 | 230 |  *	proper enumerated value, and initiates a suspend transition. | 
 | 231 |  */ | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 232 | static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 233 | 			  char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { | 
| Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 235 | 	char *s = buf; | 
 | 236 | #ifdef CONFIG_SUSPEND | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | 	int i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 |  | 
 | 239 | 	for (i = 0; i < PM_SUSPEND_MAX; i++) { | 
| Pavel Machek | 123d3c1 | 2005-11-29 19:34:37 -0800 | [diff] [blame] | 240 | 		if (pm_states[i] && valid_state(i)) | 
 | 241 | 			s += sprintf(s,"%s ", pm_states[i]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | 	} | 
| Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 243 | #endif | 
| Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 244 | #ifdef CONFIG_HIBERNATION | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 245 | 	s += sprintf(s, "%s\n", "disk"); | 
 | 246 | #else | 
 | 247 | 	if (s != buf) | 
 | 248 | 		/* convert the last space to a newline */ | 
 | 249 | 		*(s-1) = '\n'; | 
 | 250 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | 	return (s - buf); | 
 | 252 | } | 
 | 253 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 254 | static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 255 | 			   const char *buf, size_t n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { | 
| Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 257 | #ifdef CONFIG_SUSPEND | 
| Arve Hjønnevåg | b0dc343 | 2008-10-09 19:17:11 -0700 | [diff] [blame] | 258 | #ifdef CONFIG_EARLYSUSPEND | 
 | 259 | 	suspend_state_t state = PM_SUSPEND_ON; | 
 | 260 | #else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | 	suspend_state_t state = PM_SUSPEND_STANDBY; | 
| Arve Hjønnevåg | b0dc343 | 2008-10-09 19:17:11 -0700 | [diff] [blame] | 262 | #endif | 
| Andreas Mohr | 3b364b8 | 2006-06-25 05:47:56 -0700 | [diff] [blame] | 263 | 	const char * const *s; | 
| Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 264 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | 	char *p; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | 	int len; | 
| Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 267 | 	int error = -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 |  | 
 | 269 | 	p = memchr(buf, '\n', n); | 
 | 270 | 	len = p ? p - buf : n; | 
 | 271 |  | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 272 | 	/* First, check if we are requested to hibernate */ | 
| Rafael J. Wysocki | 8d98a69 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 273 | 	if (len == 4 && !strncmp(buf, "disk", len)) { | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 274 | 		error = hibernate(); | 
| Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 275 |   goto Exit; | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 276 | 	} | 
 | 277 |  | 
| Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 278 | #ifdef CONFIG_SUSPEND | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | 	for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) { | 
| Rafael J. Wysocki | 8d98a69 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 280 | 		if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | 			break; | 
 | 282 | 	} | 
| dean gaudet | 47bb789 | 2006-04-27 18:39:17 -0700 | [diff] [blame] | 283 | 	if (state < PM_SUSPEND_MAX && *s) | 
| Arve Hjønnevåg | b0dc343 | 2008-10-09 19:17:11 -0700 | [diff] [blame] | 284 | #ifdef CONFIG_EARLYSUSPEND | 
 | 285 | 		if (state == PM_SUSPEND_ON || valid_state(state)) { | 
 | 286 | 			error = 0; | 
 | 287 | 			request_suspend_state(state); | 
 | 288 | 		} | 
 | 289 | #else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 		error = enter_state(state); | 
| Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 291 | #endif | 
| Arve Hjønnevåg | b0dc343 | 2008-10-09 19:17:11 -0700 | [diff] [blame] | 292 | #endif | 
| Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 293 |  | 
 | 294 |  Exit: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | 	return error ? error : n; | 
 | 296 | } | 
 | 297 |  | 
 | 298 | power_attr(state); | 
 | 299 |  | 
| Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 300 | #ifdef CONFIG_PM_SLEEP | 
 | 301 | /* | 
 | 302 |  * The 'wakeup_count' attribute, along with the functions defined in | 
 | 303 |  * drivers/base/power/wakeup.c, provides a means by which wakeup events can be | 
 | 304 |  * handled in a non-racy way. | 
 | 305 |  * | 
 | 306 |  * If a wakeup event occurs when the system is in a sleep state, it simply is | 
 | 307 |  * woken up.  In turn, if an event that would wake the system up from a sleep | 
 | 308 |  * state occurs when it is undergoing a transition to that sleep state, the | 
 | 309 |  * transition should be aborted.  Moreover, if such an event occurs when the | 
 | 310 |  * system is in the working state, an attempt to start a transition to the | 
 | 311 |  * given sleep state should fail during certain period after the detection of | 
 | 312 |  * the event.  Using the 'state' attribute alone is not sufficient to satisfy | 
 | 313 |  * these requirements, because a wakeup event may occur exactly when 'state' | 
 | 314 |  * is being written to and may be delivered to user space right before it is | 
 | 315 |  * frozen, so the event will remain only partially processed until the system is | 
 | 316 |  * woken up by another event.  In particular, it won't cause the transition to | 
 | 317 |  * a sleep state to be aborted. | 
 | 318 |  * | 
 | 319 |  * This difficulty may be overcome if user space uses 'wakeup_count' before | 
 | 320 |  * writing to 'state'.  It first should read from 'wakeup_count' and store | 
 | 321 |  * the read value.  Then, after carrying out its own preparations for the system | 
 | 322 |  * transition to a sleep state, it should write the stored value to | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 323 |  * 'wakeup_count'.  If that fails, at least one wakeup event has occurred since | 
| Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 324 |  * 'wakeup_count' was read and 'state' should not be written to.  Otherwise, it | 
 | 325 |  * is allowed to write to 'state', but the transition will be aborted if there | 
 | 326 |  * are any wakeup events detected after 'wakeup_count' was written to. | 
 | 327 |  */ | 
 | 328 |  | 
 | 329 | static ssize_t wakeup_count_show(struct kobject *kobj, | 
 | 330 | 				struct kobj_attribute *attr, | 
 | 331 | 				char *buf) | 
 | 332 | { | 
| Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 333 | 	unsigned int val; | 
| Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 334 |  | 
| Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 335 | 	return pm_get_wakeup_count(&val) ? sprintf(buf, "%u\n", val) : -EINTR; | 
| Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 336 | } | 
 | 337 |  | 
 | 338 | static ssize_t wakeup_count_store(struct kobject *kobj, | 
 | 339 | 				struct kobj_attribute *attr, | 
 | 340 | 				const char *buf, size_t n) | 
 | 341 | { | 
| Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 342 | 	unsigned int val; | 
| Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 343 |  | 
| Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 344 | 	if (sscanf(buf, "%u", &val) == 1) { | 
| Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 345 | 		if (pm_save_wakeup_count(val)) | 
 | 346 | 			return n; | 
 | 347 | 	} | 
 | 348 | 	return -EINVAL; | 
 | 349 | } | 
 | 350 |  | 
 | 351 | power_attr(wakeup_count); | 
 | 352 | #endif /* CONFIG_PM_SLEEP */ | 
 | 353 |  | 
| Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 354 | #ifdef CONFIG_PM_TRACE | 
 | 355 | int pm_trace_enabled; | 
 | 356 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 357 | static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 358 | 			     char *buf) | 
| Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 359 | { | 
 | 360 | 	return sprintf(buf, "%d\n", pm_trace_enabled); | 
 | 361 | } | 
 | 362 |  | 
 | 363 | static ssize_t | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 364 | pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 365 | 	       const char *buf, size_t n) | 
| Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 366 | { | 
 | 367 | 	int val; | 
 | 368 |  | 
 | 369 | 	if (sscanf(buf, "%d", &val) == 1) { | 
 | 370 | 		pm_trace_enabled = !!val; | 
 | 371 | 		return n; | 
 | 372 | 	} | 
 | 373 | 	return -EINVAL; | 
 | 374 | } | 
 | 375 |  | 
 | 376 | power_attr(pm_trace); | 
| James Hogan | d33ac60 | 2010-10-12 00:00:25 +0200 | [diff] [blame] | 377 |  | 
 | 378 | static ssize_t pm_trace_dev_match_show(struct kobject *kobj, | 
 | 379 | 				       struct kobj_attribute *attr, | 
 | 380 | 				       char *buf) | 
 | 381 | { | 
 | 382 | 	return show_trace_dev_match(buf, PAGE_SIZE); | 
 | 383 | } | 
 | 384 |  | 
 | 385 | static ssize_t | 
 | 386 | pm_trace_dev_match_store(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 387 | 			 const char *buf, size_t n) | 
 | 388 | { | 
 | 389 | 	return -EINVAL; | 
 | 390 | } | 
 | 391 |  | 
 | 392 | power_attr(pm_trace_dev_match); | 
 | 393 |  | 
| Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 394 | #endif /* CONFIG_PM_TRACE */ | 
| Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 395 |  | 
| Arve Hjønnevåg | 47dfb46 | 2008-10-09 21:01:46 -0700 | [diff] [blame] | 396 | #ifdef CONFIG_USER_WAKELOCK | 
 | 397 | power_attr(wake_lock); | 
 | 398 | power_attr(wake_unlock); | 
 | 399 | #endif | 
 | 400 |  | 
| Amar Singhal | ac2a6d6 | 2012-02-27 17:40:31 -0800 | [diff] [blame] | 401 | static struct attribute *g[] = { | 
 | 402 | 	&touch_event_attr.attr, | 
 | 403 | 	&touch_event_timer_attr.attr, | 
| Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 404 | 	&state_attr.attr, | 
| Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 405 | #ifdef CONFIG_PM_TRACE | 
| Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 406 | 	&pm_trace_attr.attr, | 
| James Hogan | d33ac60 | 2010-10-12 00:00:25 +0200 | [diff] [blame] | 407 | 	&pm_trace_dev_match_attr.attr, | 
| Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 408 | #endif | 
| Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 409 | #ifdef CONFIG_PM_SLEEP | 
 | 410 | 	&pm_async_attr.attr, | 
| Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 411 | 	&wakeup_count_attr.attr, | 
| Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 412 | #ifdef CONFIG_PM_DEBUG | 
| Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 413 | 	&pm_test_attr.attr, | 
 | 414 | #endif | 
| Arve Hjønnevåg | 47dfb46 | 2008-10-09 21:01:46 -0700 | [diff] [blame] | 415 | #ifdef CONFIG_USER_WAKELOCK | 
 | 416 | 	&wake_lock_attr.attr, | 
 | 417 | 	&wake_unlock_attr.attr, | 
 | 418 | #endif | 
| Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 419 | #endif | 
| Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 420 | 	NULL, | 
 | 421 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 |  | 
 | 423 | static struct attribute_group attr_group = { | 
 | 424 | 	.attrs = g, | 
 | 425 | }; | 
 | 426 |  | 
| Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 427 | #ifdef CONFIG_PM_RUNTIME | 
 | 428 | struct workqueue_struct *pm_wq; | 
| Alan Stern | 7b199ca | 2009-12-03 20:22:21 +0100 | [diff] [blame] | 429 | EXPORT_SYMBOL_GPL(pm_wq); | 
| Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 430 |  | 
 | 431 | static int __init pm_start_workqueue(void) | 
 | 432 | { | 
| Tejun Heo | 58a69cb | 2011-02-16 09:25:31 +0100 | [diff] [blame] | 433 | 	pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0); | 
| Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 434 |  | 
 | 435 | 	return pm_wq ? 0 : -ENOMEM; | 
 | 436 | } | 
 | 437 | #else | 
 | 438 | static inline int pm_start_workqueue(void) { return 0; } | 
 | 439 | #endif | 
 | 440 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | static int __init pm_init(void) | 
 | 442 | { | 
| Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 443 | 	int error = pm_start_workqueue(); | 
 | 444 | 	if (error) | 
 | 445 | 		return error; | 
| Rafael J. Wysocki | ac5c24ec | 2010-09-20 19:44:56 +0200 | [diff] [blame] | 446 | 	hibernate_image_size_init(); | 
| Rafael J. Wysocki | ddeb648 | 2011-05-15 11:38:48 +0200 | [diff] [blame] | 447 | 	hibernate_reserved_size_init(); | 
| Amar Singhal | ac2a6d6 | 2012-02-27 17:40:31 -0800 | [diff] [blame] | 448 |  | 
 | 449 | 	touch_evt_timer_val = ktime_set(2, 0); | 
| Amar Singhal | 97d68a8 | 2012-03-20 19:03:04 -0700 | [diff] [blame] | 450 | 	hrtimer_init(&tc_ev_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | 
 | 451 | 	tc_ev_timer.function = &tc_ev_stop; | 
 | 452 | 	tc_ev_processed = 1; | 
| Amar Singhal | ac2a6d6 | 2012-02-27 17:40:31 -0800 | [diff] [blame] | 453 |  | 
| Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 454 | 	power_kobj = kobject_create_and_add("power", NULL); | 
 | 455 | 	if (!power_kobj) | 
| Greg Kroah-Hartman | 039a5dc | 2007-11-01 10:39:50 -0700 | [diff] [blame] | 456 | 		return -ENOMEM; | 
| Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 457 | 	return sysfs_create_group(power_kobj, &attr_group); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } | 
 | 459 |  | 
 | 460 | core_initcall(pm_init); |