2 * linux/kernel/power/user.c
4 * This file provides the user space interface for software suspend/resume.
6 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
8 * This file is released under the GPLv2.
12 #include <linux/suspend.h>
13 #include <linux/syscalls.h>
14 #include <linux/reboot.h>
15 #include <linux/string.h>
16 #include <linux/device.h>
17 #include <linux/miscdevice.h>
19 #include <linux/swap.h>
20 #include <linux/swapops.h>
23 #include <linux/console.h>
24 #include <linux/cpu.h>
25 #include <linux/freezer.h>
27 #include <asm/uaccess.h>
31 #define SNAPSHOT_MINOR 231
33 static struct snapshot_data {
34 struct snapshot_handle handle;
39 char platform_suspend;
42 atomic_t snapshot_device_available = ATOMIC_INIT(1);
44 static int snapshot_open(struct inode *inode, struct file *filp)
46 struct snapshot_data *data;
48 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
51 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
52 atomic_inc(&snapshot_device_available);
55 if(create_basic_memory_bitmaps()) {
56 atomic_inc(&snapshot_device_available);
59 nonseekable_open(inode, filp);
60 data = &snapshot_state;
61 filp->private_data = data;
62 memset(&data->handle, 0, sizeof(struct snapshot_handle));
63 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
64 data->swap = swsusp_resume_device ?
65 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
66 data->mode = O_RDONLY;
69 data->mode = O_WRONLY;
73 data->platform_suspend = 0;
78 static int snapshot_release(struct inode *inode, struct file *filp)
80 struct snapshot_data *data;
83 free_basic_memory_bitmaps();
84 data = filp->private_data;
85 free_all_swap_pages(data->swap);
87 mutex_lock(&pm_mutex);
89 mutex_unlock(&pm_mutex);
91 atomic_inc(&snapshot_device_available);
95 static ssize_t snapshot_read(struct file *filp, char __user *buf,
96 size_t count, loff_t *offp)
98 struct snapshot_data *data;
101 data = filp->private_data;
102 res = snapshot_read_next(&data->handle, count);
104 if (copy_to_user(buf, data_of(data->handle), res))
107 *offp = data->handle.offset;
112 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
113 size_t count, loff_t *offp)
115 struct snapshot_data *data;
118 data = filp->private_data;
119 res = snapshot_write_next(&data->handle, count);
121 if (copy_from_user(data_of(data->handle), buf, res))
124 *offp = data->handle.offset;
129 static inline int platform_prepare(void)
134 error = hibernation_ops->prepare();
139 static inline void platform_finish(void)
142 hibernation_ops->finish();
145 static inline int snapshot_suspend(int platform_suspend)
149 mutex_lock(&pm_mutex);
150 /* Free memory before shutting down devices. */
151 error = swsusp_shrink_memory();
155 if (platform_suspend) {
156 error = platform_prepare();
161 error = device_suspend(PMSG_FREEZE);
165 error = disable_nonboot_cpus();
168 error = swsusp_suspend();
170 enable_nonboot_cpus();
172 if (platform_suspend)
178 mutex_unlock(&pm_mutex);
182 static inline int snapshot_restore(int platform_suspend)
186 mutex_lock(&pm_mutex);
187 pm_prepare_console();
188 if (platform_suspend) {
189 error = platform_prepare();
194 error = device_suspend(PMSG_PRETHAW);
198 error = disable_nonboot_cpus();
200 error = swsusp_resume();
202 enable_nonboot_cpus();
204 if (platform_suspend)
210 pm_restore_console();
211 mutex_unlock(&pm_mutex);
215 static int snapshot_ioctl(struct inode *inode, struct file *filp,
216 unsigned int cmd, unsigned long arg)
219 struct snapshot_data *data;
223 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
225 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
227 if (!capable(CAP_SYS_ADMIN))
230 data = filp->private_data;
234 case SNAPSHOT_FREEZE:
237 mutex_lock(&pm_mutex);
238 if (freeze_processes()) {
242 mutex_unlock(&pm_mutex);
247 case SNAPSHOT_UNFREEZE:
250 mutex_lock(&pm_mutex);
252 mutex_unlock(&pm_mutex);
256 case SNAPSHOT_ATOMIC_SNAPSHOT:
257 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
261 error = snapshot_suspend(data->platform_suspend);
263 error = put_user(in_suspend, (unsigned int __user *)arg);
268 case SNAPSHOT_ATOMIC_RESTORE:
269 snapshot_write_finalize(&data->handle);
270 if (data->mode != O_WRONLY || !data->frozen ||
271 !snapshot_image_loaded(&data->handle)) {
275 error = snapshot_restore(data->platform_suspend);
280 memset(&data->handle, 0, sizeof(struct snapshot_handle));
284 case SNAPSHOT_SET_IMAGE_SIZE:
288 case SNAPSHOT_AVAIL_SWAP:
289 avail = count_swap_pages(data->swap, 1);
290 avail <<= PAGE_SHIFT;
291 error = put_user(avail, (loff_t __user *)arg);
294 case SNAPSHOT_GET_SWAP_PAGE:
295 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
299 offset = alloc_swapdev_block(data->swap);
301 offset <<= PAGE_SHIFT;
302 error = put_user(offset, (sector_t __user *)arg);
308 case SNAPSHOT_FREE_SWAP_PAGES:
309 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
313 free_all_swap_pages(data->swap);
316 case SNAPSHOT_SET_SWAP_FILE:
317 if (!swsusp_swap_in_use()) {
319 * User space encodes device types as two-byte values,
320 * so we need to recode them
322 if (old_decode_dev(arg)) {
323 data->swap = swap_type_of(old_decode_dev(arg),
347 if (!mutex_trylock(&pm_mutex)) {
352 if (pm_ops->prepare) {
353 error = pm_ops->prepare(PM_SUSPEND_MEM);
358 /* Put devices to sleep */
360 error = device_suspend(PMSG_SUSPEND);
362 printk(KERN_ERR "Failed to suspend some devices.\n");
364 error = disable_nonboot_cpus();
366 /* Enter S3, system is already frozen */
367 suspend_enter(PM_SUSPEND_MEM);
368 enable_nonboot_cpus();
370 /* Wake up devices */
375 pm_ops->finish(PM_SUSPEND_MEM);
378 mutex_unlock(&pm_mutex);
387 if (hibernation_ops) {
388 data->platform_suspend = 1;
396 if (data->platform_suspend) {
397 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
398 error = hibernation_ops->enter();
403 if (data->platform_suspend)
409 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
414 case SNAPSHOT_SET_SWAP_AREA:
415 if (swsusp_swap_in_use()) {
418 struct resume_swap_area swap_area;
421 error = copy_from_user(&swap_area, (void __user *)arg,
422 sizeof(struct resume_swap_area));
429 * User space encodes device types as two-byte values,
430 * so we need to recode them
432 swdev = old_decode_dev(swap_area.dev);
434 offset = swap_area.offset;
435 data->swap = swap_type_of(swdev, offset, NULL);
453 static const struct file_operations snapshot_fops = {
454 .open = snapshot_open,
455 .release = snapshot_release,
456 .read = snapshot_read,
457 .write = snapshot_write,
459 .ioctl = snapshot_ioctl,
462 static struct miscdevice snapshot_device = {
463 .minor = SNAPSHOT_MINOR,
465 .fops = &snapshot_fops,
468 static int __init snapshot_device_init(void)
470 return misc_register(&snapshot_device);
473 device_initcall(snapshot_device_init);