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
;
104 res
= snapshot_read_next(&data
->handle
, count
);
106 if (copy_to_user(buf
, data_of(data
->handle
), res
))
109 *offp
= data
->handle
.offset
;
114 static ssize_t
snapshot_write(struct file
*filp
, const char __user
*buf
,
115 size_t count
, loff_t
*offp
)
117 struct snapshot_data
*data
;
120 data
= filp
->private_data
;
121 res
= snapshot_write_next(&data
->handle
, count
);
123 if (copy_from_user(data_of(data
->handle
), buf
, res
))
126 *offp
= data
->handle
.offset
;
131 static int snapshot_ioctl(struct inode
*inode
, struct file
*filp
,
132 unsigned int cmd
, unsigned long arg
)
135 struct snapshot_data
*data
;
139 if (_IOC_TYPE(cmd
) != SNAPSHOT_IOC_MAGIC
)
141 if (_IOC_NR(cmd
) > SNAPSHOT_IOC_MAXNR
)
143 if (!capable(CAP_SYS_ADMIN
))
146 data
= filp
->private_data
;
150 case SNAPSHOT_FREEZE
:
153 mutex_lock(&pm_mutex
);
154 error
= pm_notifier_call_chain(PM_HIBERNATION_PREPARE
);
156 error
= freeze_processes();
161 pm_notifier_call_chain(PM_POST_HIBERNATION
);
162 mutex_unlock(&pm_mutex
);
167 case SNAPSHOT_UNFREEZE
:
168 if (!data
->frozen
|| data
->ready
)
170 mutex_lock(&pm_mutex
);
172 pm_notifier_call_chain(PM_POST_HIBERNATION
);
173 mutex_unlock(&pm_mutex
);
177 case SNAPSHOT_ATOMIC_SNAPSHOT
:
178 if (data
->mode
!= O_RDONLY
|| !data
->frozen
|| data
->ready
) {
182 error
= hibernation_snapshot(data
->platform_suspend
);
184 error
= put_user(in_suspend
, (unsigned int __user
*)arg
);
189 case SNAPSHOT_ATOMIC_RESTORE
:
190 snapshot_write_finalize(&data
->handle
);
191 if (data
->mode
!= O_WRONLY
|| !data
->frozen
||
192 !snapshot_image_loaded(&data
->handle
)) {
196 error
= hibernation_restore(data
->platform_suspend
);
201 memset(&data
->handle
, 0, sizeof(struct snapshot_handle
));
205 case SNAPSHOT_SET_IMAGE_SIZE
:
209 case SNAPSHOT_AVAIL_SWAP
:
210 avail
= count_swap_pages(data
->swap
, 1);
211 avail
<<= PAGE_SHIFT
;
212 error
= put_user(avail
, (loff_t __user
*)arg
);
215 case SNAPSHOT_GET_SWAP_PAGE
:
216 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
220 offset
= alloc_swapdev_block(data
->swap
);
222 offset
<<= PAGE_SHIFT
;
223 error
= put_user(offset
, (sector_t __user
*)arg
);
229 case SNAPSHOT_FREE_SWAP_PAGES
:
230 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
234 free_all_swap_pages(data
->swap
);
237 case SNAPSHOT_SET_SWAP_FILE
:
238 if (!swsusp_swap_in_use()) {
240 * User space encodes device types as two-byte values,
241 * so we need to recode them
243 if (old_decode_dev(arg
)) {
244 data
->swap
= swap_type_of(old_decode_dev(arg
),
262 if (!mutex_trylock(&pm_mutex
)) {
267 * Tasks are frozen and the notifiers have been called with
268 * PM_HIBERNATION_PREPARE
270 error
= suspend_devices_and_enter(PM_SUSPEND_MEM
);
271 mutex_unlock(&pm_mutex
);
280 data
->platform_suspend
= 1;
285 if (data
->platform_suspend
)
286 error
= hibernation_platform_enter();
291 if (data
->platform_suspend
)
297 printk(KERN_ERR
"SNAPSHOT_PMOPS: invalid argument %ld\n", arg
);
302 case SNAPSHOT_SET_SWAP_AREA
:
303 if (swsusp_swap_in_use()) {
306 struct resume_swap_area swap_area
;
309 error
= copy_from_user(&swap_area
, (void __user
*)arg
,
310 sizeof(struct resume_swap_area
));
317 * User space encodes device types as two-byte values,
318 * so we need to recode them
320 swdev
= old_decode_dev(swap_area
.dev
);
322 offset
= swap_area
.offset
;
323 data
->swap
= swap_type_of(swdev
, offset
, NULL
);
341 static const struct file_operations snapshot_fops
= {
342 .open
= snapshot_open
,
343 .release
= snapshot_release
,
344 .read
= snapshot_read
,
345 .write
= snapshot_write
,
347 .ioctl
= snapshot_ioctl
,
350 static struct miscdevice snapshot_device
= {
351 .minor
= SNAPSHOT_MINOR
,
353 .fops
= &snapshot_fops
,
356 static int __init
snapshot_device_init(void)
358 return misc_register(&snapshot_device
);
361 device_initcall(snapshot_device_init
);