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;
36 struct bitmap_page *bitmap;
40 char platform_suspend;
43 static atomic_t device_available = ATOMIC_INIT(1);
45 static int snapshot_open(struct inode *inode, struct file *filp)
47 struct snapshot_data *data;
49 if (!atomic_add_unless(&device_available, -1, 0))
52 if ((filp->f_flags & O_ACCMODE) == O_RDWR)
55 nonseekable_open(inode, filp);
56 data = &snapshot_state;
57 filp->private_data = data;
58 memset(&data->handle, 0, sizeof(struct snapshot_handle));
59 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
60 data->swap = swsusp_resume_device ?
61 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
62 data->mode = O_RDONLY;
65 data->mode = O_WRONLY;
70 data->platform_suspend = 0;
75 static int snapshot_release(struct inode *inode, struct file *filp)
77 struct snapshot_data *data;
80 data = filp->private_data;
81 free_all_swap_pages(data->swap, data->bitmap);
82 free_bitmap(data->bitmap);
84 mutex_lock(&pm_mutex);
86 enable_nonboot_cpus();
87 mutex_unlock(&pm_mutex);
89 atomic_inc(&device_available);
93 static ssize_t snapshot_read(struct file *filp, char __user *buf,
94 size_t count, loff_t *offp)
96 struct snapshot_data *data;
99 data = filp->private_data;
100 res = snapshot_read_next(&data->handle, count);
102 if (copy_to_user(buf, data_of(data->handle), res))
105 *offp = data->handle.offset;
110 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
111 size_t count, loff_t *offp)
113 struct snapshot_data *data;
116 data = filp->private_data;
117 res = snapshot_write_next(&data->handle, count);
119 if (copy_from_user(data_of(data->handle), buf, res))
122 *offp = data->handle.offset;
127 static inline int platform_prepare(void)
131 if (pm_ops && pm_ops->prepare)
132 error = pm_ops->prepare(PM_SUSPEND_DISK);
137 static inline void platform_finish(void)
139 if (pm_ops && pm_ops->finish)
140 pm_ops->finish(PM_SUSPEND_DISK);
143 static inline int snapshot_suspend(int platform_suspend)
147 mutex_lock(&pm_mutex);
148 /* Free memory before shutting down devices. */
149 error = swsusp_shrink_memory();
153 if (platform_suspend) {
154 error = platform_prepare();
159 error = device_suspend(PMSG_FREEZE);
163 error = disable_nonboot_cpus();
166 error = swsusp_suspend();
168 enable_nonboot_cpus();
170 if (platform_suspend)
176 mutex_unlock(&pm_mutex);
180 static inline int snapshot_restore(int platform_suspend)
184 mutex_lock(&pm_mutex);
185 pm_prepare_console();
186 if (platform_suspend) {
187 error = platform_prepare();
192 error = device_suspend(PMSG_PRETHAW);
196 error = disable_nonboot_cpus();
198 error = swsusp_resume();
200 enable_nonboot_cpus();
202 if (platform_suspend)
208 pm_restore_console();
209 mutex_unlock(&pm_mutex);
213 static int snapshot_ioctl(struct inode *inode, struct file *filp,
214 unsigned int cmd, unsigned long arg)
217 struct snapshot_data *data;
221 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
223 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
225 if (!capable(CAP_SYS_ADMIN))
228 data = filp->private_data;
232 case SNAPSHOT_FREEZE:
235 mutex_lock(&pm_mutex);
236 if (freeze_processes()) {
240 mutex_unlock(&pm_mutex);
245 case SNAPSHOT_UNFREEZE:
248 mutex_lock(&pm_mutex);
250 mutex_unlock(&pm_mutex);
254 case SNAPSHOT_ATOMIC_SNAPSHOT:
255 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
259 error = snapshot_suspend(data->platform_suspend);
261 error = put_user(in_suspend, (unsigned int __user *)arg);
266 case SNAPSHOT_ATOMIC_RESTORE:
267 snapshot_write_finalize(&data->handle);
268 if (data->mode != O_WRONLY || !data->frozen ||
269 !snapshot_image_loaded(&data->handle)) {
273 error = snapshot_restore(data->platform_suspend);
278 memset(&data->handle, 0, sizeof(struct snapshot_handle));
282 case SNAPSHOT_SET_IMAGE_SIZE:
286 case SNAPSHOT_AVAIL_SWAP:
287 avail = count_swap_pages(data->swap, 1);
288 avail <<= PAGE_SHIFT;
289 error = put_user(avail, (loff_t __user *)arg);
292 case SNAPSHOT_GET_SWAP_PAGE:
293 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
298 data->bitmap = alloc_bitmap(count_swap_pages(data->swap, 0));
304 offset = alloc_swapdev_block(data->swap, data->bitmap);
306 offset <<= PAGE_SHIFT;
307 error = put_user(offset, (sector_t __user *)arg);
313 case SNAPSHOT_FREE_SWAP_PAGES:
314 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
318 free_all_swap_pages(data->swap, data->bitmap);
319 free_bitmap(data->bitmap);
323 case SNAPSHOT_SET_SWAP_FILE:
326 * User space encodes device types as two-byte values,
327 * so we need to recode them
329 if (old_decode_dev(arg)) {
330 data->swap = swap_type_of(old_decode_dev(arg),
354 if (!mutex_trylock(&pm_mutex)) {
359 if (pm_ops->prepare) {
360 error = pm_ops->prepare(PM_SUSPEND_MEM);
365 /* Put devices to sleep */
367 error = device_suspend(PMSG_SUSPEND);
369 printk(KERN_ERR "Failed to suspend some devices.\n");
371 /* Enter S3, system is already frozen */
372 suspend_enter(PM_SUSPEND_MEM);
374 /* Wake up devices */
379 pm_ops->finish(PM_SUSPEND_MEM);
382 mutex_unlock(&pm_mutex);
391 if (pm_ops && pm_ops->enter) {
392 data->platform_suspend = 1;
400 if (data->platform_suspend) {
401 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
402 error = pm_ops->enter(PM_SUSPEND_DISK);
408 if (data->platform_suspend)
414 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
419 case SNAPSHOT_SET_SWAP_AREA:
423 struct resume_swap_area swap_area;
426 error = copy_from_user(&swap_area, (void __user *)arg,
427 sizeof(struct resume_swap_area));
434 * User space encodes device types as two-byte values,
435 * so we need to recode them
437 swdev = old_decode_dev(swap_area.dev);
439 offset = swap_area.offset;
440 data->swap = swap_type_of(swdev, offset, NULL);
458 static const struct file_operations snapshot_fops = {
459 .open = snapshot_open,
460 .release = snapshot_release,
461 .read = snapshot_read,
462 .write = snapshot_write,
464 .ioctl = snapshot_ioctl,
467 static struct miscdevice snapshot_device = {
468 .minor = SNAPSHOT_MINOR,
470 .fops = &snapshot_fops,
473 static int __init snapshot_device_init(void)
475 return misc_register(&snapshot_device);
478 device_initcall(snapshot_device_init);