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 error
= disable_nonboot_cpus();
373 /* Enter S3, system is already frozen */
374 suspend_enter(PM_SUSPEND_MEM
);
375 enable_nonboot_cpus();
377 /* Wake up devices */
382 pm_ops
->finish(PM_SUSPEND_MEM
);
385 mutex_unlock(&pm_mutex
);
394 if (pm_ops
&& pm_ops
->enter
) {
395 data
->platform_suspend
= 1;
403 if (data
->platform_suspend
) {
404 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK
);
405 error
= pm_ops
->enter(PM_SUSPEND_DISK
);
411 if (data
->platform_suspend
)
417 printk(KERN_ERR
"SNAPSHOT_PMOPS: invalid argument %ld\n", arg
);
422 case SNAPSHOT_SET_SWAP_AREA
:
426 struct resume_swap_area swap_area
;
429 error
= copy_from_user(&swap_area
, (void __user
*)arg
,
430 sizeof(struct resume_swap_area
));
437 * User space encodes device types as two-byte values,
438 * so we need to recode them
440 swdev
= old_decode_dev(swap_area
.dev
);
442 offset
= swap_area
.offset
;
443 data
->swap
= swap_type_of(swdev
, offset
, NULL
);
461 static const struct file_operations snapshot_fops
= {
462 .open
= snapshot_open
,
463 .release
= snapshot_release
,
464 .read
= snapshot_read
,
465 .write
= snapshot_write
,
467 .ioctl
= snapshot_ioctl
,
470 static struct miscdevice snapshot_device
= {
471 .minor
= SNAPSHOT_MINOR
,
473 .fops
= &snapshot_fops
,
476 static int __init
snapshot_device_init(void)
478 return misc_register(&snapshot_device
);
481 device_initcall(snapshot_device_init
);