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 11 #include <linux/kobject.h> 12 #include <linux/string.h> 13 #include <linux/resume-trace.h> 14 15 #include "power.h" 16 17 DEFINE_MUTEX(pm_mutex); 18 19 unsigned int pm_flags; 20 EXPORT_SYMBOL(pm_flags); 21 22 #ifdef CONFIG_PM_SLEEP 23 24 /* Routines for PM-transition notifications */ 25 26 static BLOCKING_NOTIFIER_HEAD(pm_chain_head); 27 28 int register_pm_notifier(struct notifier_block *nb) 29 { 30 return blocking_notifier_chain_register(&pm_chain_head, nb); 31 } 32 EXPORT_SYMBOL_GPL(register_pm_notifier); 33 34 int unregister_pm_notifier(struct notifier_block *nb) 35 { 36 return blocking_notifier_chain_unregister(&pm_chain_head, nb); 37 } 38 EXPORT_SYMBOL_GPL(unregister_pm_notifier); 39 40 int pm_notifier_call_chain(unsigned long val) 41 { 42 return (blocking_notifier_call_chain(&pm_chain_head, val, NULL) 43 == NOTIFY_BAD) ? -EINVAL : 0; 44 } 45 46 #ifdef CONFIG_PM_DEBUG 47 int pm_test_level = TEST_NONE; 48 49 static const char * const pm_tests[__TEST_AFTER_LAST] = { 50 [TEST_NONE] = "none", 51 [TEST_CORE] = "core", 52 [TEST_CPUS] = "processors", 53 [TEST_PLATFORM] = "platform", 54 [TEST_DEVICES] = "devices", 55 [TEST_FREEZER] = "freezer", 56 }; 57 58 static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr, 59 char *buf) 60 { 61 char *s = buf; 62 int level; 63 64 for (level = TEST_FIRST; level <= TEST_MAX; level++) 65 if (pm_tests[level]) { 66 if (level == pm_test_level) 67 s += sprintf(s, "[%s] ", pm_tests[level]); 68 else 69 s += sprintf(s, "%s ", pm_tests[level]); 70 } 71 72 if (s != buf) 73 /* convert the last space to a newline */ 74 *(s-1) = '\n'; 75 76 return (s - buf); 77 } 78 79 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr, 80 const char *buf, size_t n) 81 { 82 const char * const *s; 83 int level; 84 char *p; 85 int len; 86 int error = -EINVAL; 87 88 p = memchr(buf, '\n', n); 89 len = p ? p - buf : n; 90 91 mutex_lock(&pm_mutex); 92 93 level = TEST_FIRST; 94 for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++) 95 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) { 96 pm_test_level = level; 97 error = 0; 98 break; 99 } 100 101 mutex_unlock(&pm_mutex); 102 103 return error ? error : n; 104 } 105 106 power_attr(pm_test); 107 #endif /* CONFIG_PM_DEBUG */ 108 109 #endif /* CONFIG_PM_SLEEP */ 110 111 struct kobject *power_kobj; 112 113 /** 114 * state - control system power state. 115 * 116 * show() returns what states are supported, which is hard-coded to 117 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and 118 * 'disk' (Suspend-to-Disk). 119 * 120 * store() accepts one of those strings, translates it into the 121 * proper enumerated value, and initiates a suspend transition. 122 */ 123 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr, 124 char *buf) 125 { 126 char *s = buf; 127 #ifdef CONFIG_SUSPEND 128 int i; 129 130 for (i = 0; i < PM_SUSPEND_MAX; i++) { 131 if (pm_states[i] && valid_state(i)) 132 s += sprintf(s,"%s ", pm_states[i]); 133 } 134 #endif 135 #ifdef CONFIG_HIBERNATION 136 s += sprintf(s, "%s\n", "disk"); 137 #else 138 if (s != buf) 139 /* convert the last space to a newline */ 140 *(s-1) = '\n'; 141 #endif 142 return (s - buf); 143 } 144 145 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, 146 const char *buf, size_t n) 147 { 148 #ifdef CONFIG_SUSPEND 149 suspend_state_t state = PM_SUSPEND_STANDBY; 150 const char * const *s; 151 #endif 152 char *p; 153 int len; 154 int error = -EINVAL; 155 156 p = memchr(buf, '\n', n); 157 len = p ? p - buf : n; 158 159 /* First, check if we are requested to hibernate */ 160 if (len == 4 && !strncmp(buf, "disk", len)) { 161 error = hibernate(); 162 goto Exit; 163 } 164 165 #ifdef CONFIG_SUSPEND 166 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) { 167 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) 168 break; 169 } 170 if (state < PM_SUSPEND_MAX && *s) 171 error = enter_state(state); 172 #endif 173 174 Exit: 175 return error ? error : n; 176 } 177 178 power_attr(state); 179 180 #ifdef CONFIG_PM_TRACE 181 int pm_trace_enabled; 182 183 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr, 184 char *buf) 185 { 186 return sprintf(buf, "%d\n", pm_trace_enabled); 187 } 188 189 static ssize_t 190 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr, 191 const char *buf, size_t n) 192 { 193 int val; 194 195 if (sscanf(buf, "%d", &val) == 1) { 196 pm_trace_enabled = !!val; 197 return n; 198 } 199 return -EINVAL; 200 } 201 202 power_attr(pm_trace); 203 #endif /* CONFIG_PM_TRACE */ 204 205 static struct attribute * g[] = { 206 &state_attr.attr, 207 #ifdef CONFIG_PM_TRACE 208 &pm_trace_attr.attr, 209 #endif 210 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG) 211 &pm_test_attr.attr, 212 #endif 213 NULL, 214 }; 215 216 static struct attribute_group attr_group = { 217 .attrs = g, 218 }; 219 220 static int __init pm_init(void) 221 { 222 power_kobj = kobject_create_and_add("power", NULL); 223 if (!power_kobj) 224 return -ENOMEM; 225 return sysfs_create_group(power_kobj, &attr_group); 226 } 227 228 core_initcall(pm_init); 229