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>
32 * NOTE: The SNAPSHOT_PMOPS ioctl is obsolete and will be removed in the
33 * future. It is only preserved here for compatibility with existing userland
36 #define SNAPSHOT_PMOPS _IOW(SNAPSHOT_IOC_MAGIC, 12, unsigned int)
38 #define PMOPS_PREPARE 1
40 #define PMOPS_FINISH 3
43 #define SNAPSHOT_MINOR 231
45 static struct snapshot_data {
46 struct snapshot_handle handle;
51 char platform_support;
54 atomic_t snapshot_device_available = ATOMIC_INIT(1);
56 static int snapshot_open(struct inode *inode, struct file *filp)
58 struct snapshot_data *data;
60 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
63 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
64 atomic_inc(&snapshot_device_available);
67 if(create_basic_memory_bitmaps()) {
68 atomic_inc(&snapshot_device_available);
71 nonseekable_open(inode, filp);
72 data = &snapshot_state;
73 filp->private_data = data;
74 memset(&data->handle, 0, sizeof(struct snapshot_handle));
75 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
76 data->swap = swsusp_resume_device ?
77 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
78 data->mode = O_RDONLY;
81 data->mode = O_WRONLY;
85 data->platform_support = 0;
90 static int snapshot_release(struct inode *inode, struct file *filp)
92 struct snapshot_data *data;
95 free_basic_memory_bitmaps();
96 data = filp->private_data;
97 free_all_swap_pages(data->swap);
99 mutex_lock(&pm_mutex);
101 mutex_unlock(&pm_mutex);
103 atomic_inc(&snapshot_device_available);
107 static ssize_t snapshot_read(struct file *filp, char __user *buf,
108 size_t count, loff_t *offp)
110 struct snapshot_data *data;
113 data = filp->private_data;
116 res = snapshot_read_next(&data->handle, count);
118 if (copy_to_user(buf, data_of(data->handle), res))
121 *offp = data->handle.offset;
126 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
127 size_t count, loff_t *offp)
129 struct snapshot_data *data;
132 data = filp->private_data;
133 res = snapshot_write_next(&data->handle, count);
135 if (copy_from_user(data_of(data->handle), buf, res))
138 *offp = data->handle.offset;
143 static int snapshot_ioctl(struct inode *inode, struct file *filp,
144 unsigned int cmd, unsigned long arg)
147 struct snapshot_data *data;
151 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
153 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
155 if (!capable(CAP_SYS_ADMIN))
158 data = filp->private_data;
162 case SNAPSHOT_FREEZE:
165 mutex_lock(&pm_mutex);
166 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
168 printk("Syncing filesystems ... ");
172 error = freeze_processes();
177 pm_notifier_call_chain(PM_POST_HIBERNATION);
178 mutex_unlock(&pm_mutex);
183 case SNAPSHOT_UNFREEZE:
184 if (!data->frozen || data->ready)
186 mutex_lock(&pm_mutex);
188 pm_notifier_call_chain(PM_POST_HIBERNATION);
189 mutex_unlock(&pm_mutex);
193 case SNAPSHOT_ATOMIC_SNAPSHOT:
194 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
198 error = hibernation_snapshot(data->platform_support);
200 error = put_user(in_suspend, (unsigned int __user *)arg);
205 case SNAPSHOT_ATOMIC_RESTORE:
206 snapshot_write_finalize(&data->handle);
207 if (data->mode != O_WRONLY || !data->frozen ||
208 !snapshot_image_loaded(&data->handle)) {
212 error = hibernation_restore(data->platform_support);
217 memset(&data->handle, 0, sizeof(struct snapshot_handle));
221 case SNAPSHOT_SET_IMAGE_SIZE:
225 case SNAPSHOT_GET_IMAGE_SIZE:
230 size = snapshot_get_image_size();
232 error = put_user(size, (loff_t __user *)arg);
235 case SNAPSHOT_AVAIL_SWAP:
236 size = count_swap_pages(data->swap, 1);
238 error = put_user(size, (loff_t __user *)arg);
241 case SNAPSHOT_GET_SWAP_PAGE:
242 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
246 offset = alloc_swapdev_block(data->swap);
248 offset <<= PAGE_SHIFT;
249 error = put_user(offset, (sector_t __user *)arg);
255 case SNAPSHOT_FREE_SWAP_PAGES:
256 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
260 free_all_swap_pages(data->swap);
263 case SNAPSHOT_SET_SWAP_FILE:
264 if (!swsusp_swap_in_use()) {
266 * User space encodes device types as two-byte values,
267 * so we need to recode them
269 if (old_decode_dev(arg)) {
270 data->swap = swap_type_of(old_decode_dev(arg),
288 if (!mutex_trylock(&pm_mutex)) {
293 * Tasks are frozen and the notifiers have been called with
294 * PM_HIBERNATION_PREPARE
296 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
297 mutex_unlock(&pm_mutex);
300 case SNAPSHOT_PLATFORM_SUPPORT:
301 data->platform_support = !!arg;
304 case SNAPSHOT_POWER_OFF:
305 if (data->platform_support)
306 error = hibernation_platform_enter();
309 case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
315 data->platform_support = 1;
320 if (data->platform_support)
321 error = hibernation_platform_enter();
325 if (data->platform_support)
330 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
335 case SNAPSHOT_SET_SWAP_AREA:
336 if (swsusp_swap_in_use()) {
339 struct resume_swap_area swap_area;
342 error = copy_from_user(&swap_area, (void __user *)arg,
343 sizeof(struct resume_swap_area));
350 * User space encodes device types as two-byte values,
351 * so we need to recode them
353 swdev = old_decode_dev(swap_area.dev);
355 offset = swap_area.offset;
356 data->swap = swap_type_of(swdev, offset, NULL);
374 static const struct file_operations snapshot_fops = {
375 .open = snapshot_open,
376 .release = snapshot_release,
377 .read = snapshot_read,
378 .write = snapshot_write,
380 .ioctl = snapshot_ioctl,
383 static struct miscdevice snapshot_device = {
384 .minor = SNAPSHOT_MINOR,
386 .fops = &snapshot_fops,
389 static int __init snapshot_device_init(void)
391 return misc_register(&snapshot_device);
394 device_initcall(snapshot_device_init);