📄 main.c
字号:
Recover_platform: if (suspend_ops->recover) suspend_ops->recover(); goto Resume_devices;}/** * suspend_finish - Do final work before exiting suspend sequence. * * Call platform code to clean up, restart processes, and free the * console that we've allocated. This is not called for suspend-to-disk. */static void suspend_finish(void){ suspend_thaw_processes(); pm_notifier_call_chain(PM_POST_SUSPEND); pm_restore_console();}static const char * const pm_states[PM_SUSPEND_MAX] = { [PM_SUSPEND_STANDBY] = "standby", [PM_SUSPEND_MEM] = "mem",};static inline int valid_state(suspend_state_t state){ /* All states need lowlevel support and need to be valid * to the lowlevel implementation, no valid callback * implies that none are valid. */ if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state)) return 0; return 1;}/** * enter_state - Do common work of entering low-power state. * @state: pm_state structure for state we're entering. * * Make sure we're the only ones trying to enter a sleep state. Fail * if someone has beat us to it, since we don't want anything weird to * happen when we wake up. * Then, do the setup for suspend, enter the state, and cleaup (after * we've woken up). */static int enter_state(suspend_state_t state){ int error; if (!valid_state(state)) return -ENODEV; if (!mutex_trylock(&pm_mutex)) return -EBUSY; printk(KERN_INFO "PM: Syncing filesystems ... "); sys_sync(); printk("done.\n"); pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]); error = suspend_prepare(); if (error) goto Unlock; if (suspend_test(TEST_FREEZER)) goto Finish; pr_debug("PM: Entering %s sleep\n", pm_states[state]); error = suspend_devices_and_enter(state); Finish: pr_debug("PM: Finishing wakeup.\n"); suspend_finish(); Unlock: mutex_unlock(&pm_mutex); return error;}/** * pm_suspend - Externally visible function for suspending system. * @state: Enumerated value of state to enter. * * Determine whether or not value is within range, get state * structure, and enter (above). */int pm_suspend(suspend_state_t state){ if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX) return enter_state(state); return -EINVAL;}EXPORT_SYMBOL(pm_suspend);#endif /* CONFIG_SUSPEND */struct kobject *power_kobj;/** * state - control system power state. * * show() returns what states are supported, which is hard-coded to * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and * 'disk' (Suspend-to-Disk). * * store() accepts one of those strings, translates it into the * proper enumerated value, and initiates a suspend transition. */static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf){ char *s = buf;#ifdef CONFIG_SUSPEND int i; for (i = 0; i < PM_SUSPEND_MAX; i++) { if (pm_states[i] && valid_state(i)) s += sprintf(s,"%s ", pm_states[i]); }#endif#ifdef CONFIG_HIBERNATION s += sprintf(s, "%s\n", "disk");#else if (s != buf) /* convert the last space to a newline */ *(s-1) = '\n';#endif return (s - buf);}static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t n){#ifdef CONFIG_SUSPEND suspend_state_t state = PM_SUSPEND_STANDBY; const char * const *s;#endif char *p; int len; int error = -EINVAL; p = memchr(buf, '\n', n); len = p ? p - buf : n; /* First, check if we are requested to hibernate */ if (len == 4 && !strncmp(buf, "disk", len)) { error = hibernate(); goto Exit; }#ifdef CONFIG_SUSPEND for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) { if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) break; } if (state < PM_SUSPEND_MAX && *s) error = enter_state(state);#endif Exit: return error ? error : n;}power_attr(state);#ifdef CONFIG_PM_TRACEint pm_trace_enabled;static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf){ return sprintf(buf, "%d\n", pm_trace_enabled);}static ssize_tpm_trace_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t n){ int val; if (sscanf(buf, "%d", &val) == 1) { pm_trace_enabled = !!val; return n; } return -EINVAL;}power_attr(pm_trace);#endif /* CONFIG_PM_TRACE */static struct attribute * g[] = { &state_attr.attr,#ifdef CONFIG_PM_TRACE &pm_trace_attr.attr,#endif#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG) &pm_test_attr.attr,#endif NULL,};static struct attribute_group attr_group = { .attrs = g,};static int __init pm_init(void){ power_kobj = kobject_create_and_add("power", NULL); if (!power_kobj) return -ENOMEM; return sysfs_create_group(power_kobj, &attr_group);}core_initcall(pm_init);#ifdef CONFIG_PM_TEST_SUSPEND#include <linux/rtc.h>/* * To test system suspend, we need a hands-off mechanism to resume the * system. RTCs wake alarms are a common self-contained mechanism. */static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state){ static char err_readtime[] __initdata = KERN_ERR "PM: can't read %s time, err %d\n"; static char err_wakealarm [] __initdata = KERN_ERR "PM: can't set %s wakealarm, err %d\n"; static char err_suspend[] __initdata = KERN_ERR "PM: suspend test failed, error %d\n"; static char info_test[] __initdata = KERN_INFO "PM: test RTC wakeup from '%s' suspend\n"; unsigned long now; struct rtc_wkalrm alm; int status; /* this may fail if the RTC hasn't been initialized */ status = rtc_read_time(rtc, &alm.time); if (status < 0) { printk(err_readtime, rtc->dev.bus_id, status); return; } rtc_tm_to_time(&alm.time, &now); memset(&alm, 0, sizeof alm); rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time); alm.enabled = true; status = rtc_set_alarm(rtc, &alm); if (status < 0) { printk(err_wakealarm, rtc->dev.bus_id, status); return; } if (state == PM_SUSPEND_MEM) { printk(info_test, pm_states[state]); status = pm_suspend(state); if (status == -ENODEV) state = PM_SUSPEND_STANDBY; } if (state == PM_SUSPEND_STANDBY) { printk(info_test, pm_states[state]); status = pm_suspend(state); } if (status < 0) printk(err_suspend, status); /* Some platforms can't detect that the alarm triggered the * wakeup, or (accordingly) disable it after it afterwards. * It's supposed to give oneshot behavior; cope. */ alm.enabled = false; rtc_set_alarm(rtc, &alm);}static int __init has_wakealarm(struct device *dev, void *name_ptr){ struct rtc_device *candidate = to_rtc_device(dev); if (!candidate->ops->set_alarm) return 0; if (!device_may_wakeup(candidate->dev.parent)) return 0; *(char **)name_ptr = dev->bus_id; return 1;}/* * Kernel options like "test_suspend=mem" force suspend/resume sanity tests * at startup time. They're normally disabled, for faster boot and because * we can't know which states really work on this particular system. */static suspend_state_t test_state __initdata = PM_SUSPEND_ON;static char warn_bad_state[] __initdata = KERN_WARNING "PM: can't test '%s' suspend state\n";static int __init setup_test_suspend(char *value){ unsigned i; /* "=mem" ==> "mem" */ value++; for (i = 0; i < PM_SUSPEND_MAX; i++) { if (!pm_states[i]) continue; if (strcmp(pm_states[i], value) != 0) continue; test_state = (__force suspend_state_t) i; return 0; } printk(warn_bad_state, value); return 0;}__setup("test_suspend", setup_test_suspend);static int __init test_suspend(void){ static char warn_no_rtc[] __initdata = KERN_WARNING "PM: no wakealarm-capable RTC driver is ready\n"; char *pony = NULL; struct rtc_device *rtc = NULL; /* PM is initialized by now; is that state testable? */ if (test_state == PM_SUSPEND_ON) goto done; if (!valid_state(test_state)) { printk(warn_bad_state, pm_states[test_state]); goto done; } /* RTCs have initialized by now too ... can we use one? */ class_find_device(rtc_class, NULL, &pony, has_wakealarm); if (pony) rtc = rtc_class_open(pony); if (!rtc) { printk(warn_no_rtc); goto done; } /* go for it */ test_wakealarm(rtc, test_state); rtc_class_close(rtc);done: return 0;}late_initcall(test_suspend);#endif /* CONFIG_PM_TEST_SUSPEND */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -