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 if(create_basic_memory_bitmaps())
58 nonseekable_open(inode
, filp
);
59 data
= &snapshot_state
;
60 filp
->private_data
= data
;
61 memset(&data
->handle
, 0, sizeof(struct snapshot_handle
));
62 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
63 data
->swap
= swsusp_resume_device
?
64 swap_type_of(swsusp_resume_device
, 0, NULL
) : -1;
65 data
->mode
= O_RDONLY
;
68 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
, data
->bitmap
);
86 free_bitmap(data
->bitmap
);
88 mutex_lock(&pm_mutex
);
90 enable_nonboot_cpus();
91 mutex_unlock(&pm_mutex
);
93 atomic_inc(&device_available
);
97 static ssize_t
snapshot_read(struct file
*filp
, char __user
*buf
,
98 size_t count
, loff_t
*offp
)
100 struct snapshot_data
*data
;
103 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 inline int platform_prepare(void)
135 if (pm_ops
&& pm_ops
->prepare
)
136 error
= pm_ops
->prepare(PM_SUSPEND_DISK
);
141 static inline void platform_finish(void)
143 if (pm_ops
&& pm_ops
->finish
)
144 pm_ops
->finish(PM_SUSPEND_DISK
);
147 static inline int snapshot_suspend(int platform_suspend
)
151 mutex_lock(&pm_mutex
);
152 /* Free memory before shutting down devices. */
153 error
= swsusp_shrink_memory();
157 if (platform_suspend
) {
158 error
= platform_prepare();
163 error
= device_suspend(PMSG_FREEZE
);
167 error
= disable_nonboot_cpus();
170 error
= swsusp_suspend();
172 enable_nonboot_cpus();
174 if (platform_suspend
)
180 mutex_unlock(&pm_mutex
);
184 static inline int snapshot_restore(int platform_suspend
)
188 mutex_lock(&pm_mutex
);
189 pm_prepare_console();
190 if (platform_suspend
) {
191 error
= platform_prepare();
196 error
= device_suspend(PMSG_PRETHAW
);
200 error
= disable_nonboot_cpus();
202 error
= swsusp_resume();
204 enable_nonboot_cpus();
206 if (platform_suspend
)
212 pm_restore_console();
213 mutex_unlock(&pm_mutex
);
217 static int snapshot_ioctl(struct inode
*inode
, struct file
*filp
,
218 unsigned int cmd
, unsigned long arg
)
221 struct snapshot_data
*data
;
225 if (_IOC_TYPE(cmd
) != SNAPSHOT_IOC_MAGIC
)
227 if (_IOC_NR(cmd
) > SNAPSHOT_IOC_MAXNR
)
229 if (!capable(CAP_SYS_ADMIN
))
232 data
= filp
->private_data
;
236 case SNAPSHOT_FREEZE
:
239 mutex_lock(&pm_mutex
);
240 if (freeze_processes()) {
244 mutex_unlock(&pm_mutex
);
249 case SNAPSHOT_UNFREEZE
:
252 mutex_lock(&pm_mutex
);
254 mutex_unlock(&pm_mutex
);
258 case SNAPSHOT_ATOMIC_SNAPSHOT
:
259 if (data
->mode
!= O_RDONLY
|| !data
->frozen
|| data
->ready
) {
263 error
= snapshot_suspend(data
->platform_suspend
);
265 error
= put_user(in_suspend
, (unsigned int __user
*)arg
);
270 case SNAPSHOT_ATOMIC_RESTORE
:
271 snapshot_write_finalize(&data
->handle
);
272 if (data
->mode
!= O_WRONLY
|| !data
->frozen
||
273 !snapshot_image_loaded(&data
->handle
)) {
277 error
= snapshot_restore(data
->platform_suspend
);
282 memset(&data
->handle
, 0, sizeof(struct snapshot_handle
));
286 case SNAPSHOT_SET_IMAGE_SIZE
:
290 case SNAPSHOT_AVAIL_SWAP
:
291 avail
= count_swap_pages(data
->swap
, 1);
292 avail
<<= PAGE_SHIFT
;
293 error
= put_user(avail
, (loff_t __user
*)arg
);
296 case SNAPSHOT_GET_SWAP_PAGE
:
297 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
302 data
->bitmap
= alloc_bitmap(count_swap_pages(data
->swap
, 0));
308 offset
= alloc_swapdev_block(data
->swap
, data
->bitmap
);
310 offset
<<= PAGE_SHIFT
;
311 error
= put_user(offset
, (sector_t __user
*)arg
);
317 case SNAPSHOT_FREE_SWAP_PAGES
:
318 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
322 free_all_swap_pages(data
->swap
, data
->bitmap
);
323 free_bitmap(data
->bitmap
);
327 case SNAPSHOT_SET_SWAP_FILE
:
330 * User space encodes device types as two-byte values,
331 * so we need to recode them
333 if (old_decode_dev(arg
)) {
334 data
->swap
= swap_type_of(old_decode_dev(arg
),
358 if (!mutex_trylock(&pm_mutex
)) {
363 if (pm_ops
->prepare
) {
364 error
= pm_ops
->prepare(PM_SUSPEND_MEM
);
369 /* Put devices to sleep */
371 error
= device_suspend(PMSG_SUSPEND
);
373 printk(KERN_ERR
"Failed to suspend some devices.\n");
375 error
= disable_nonboot_cpus();
377 /* Enter S3, system is already frozen */
378 suspend_enter(PM_SUSPEND_MEM
);
379 enable_nonboot_cpus();
381 /* Wake up devices */
386 pm_ops
->finish(PM_SUSPEND_MEM
);
389 mutex_unlock(&pm_mutex
);
398 if (pm_ops
&& pm_ops
->enter
) {
399 data
->platform_suspend
= 1;
407 if (data
->platform_suspend
) {
408 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK
);
409 error
= pm_ops
->enter(PM_SUSPEND_DISK
);
415 if (data
->platform_suspend
)
421 printk(KERN_ERR
"SNAPSHOT_PMOPS: invalid argument %ld\n", arg
);
426 case SNAPSHOT_SET_SWAP_AREA
:
430 struct resume_swap_area swap_area
;
433 error
= copy_from_user(&swap_area
, (void __user
*)arg
,
434 sizeof(struct resume_swap_area
));
441 * User space encodes device types as two-byte values,
442 * so we need to recode them
444 swdev
= old_decode_dev(swap_area
.dev
);
446 offset
= swap_area
.offset
;
447 data
->swap
= swap_type_of(swdev
, offset
, NULL
);
465 static const struct file_operations snapshot_fops
= {
466 .open
= snapshot_open
,
467 .release
= snapshot_release
,
468 .read
= snapshot_read
,
469 .write
= snapshot_write
,
471 .ioctl
= snapshot_ioctl
,
474 static struct miscdevice snapshot_device
= {
475 .minor
= SNAPSHOT_MINOR
,
477 .fops
= &snapshot_fops
,
480 static int __init
snapshot_device_init(void)
482 return misc_register(&snapshot_device
);
485 device_initcall(snapshot_device_init
);