Linux 2.6.19-rc6
[cris-mirror.git] / kernel / power / user.c
blobd991d3b0e5a4e326ea29b6b7fc50c4a926dd5e3e
1 /*
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/string.h>
15 #include <linux/device.h>
16 #include <linux/miscdevice.h>
17 #include <linux/mm.h>
18 #include <linux/swap.h>
19 #include <linux/swapops.h>
20 #include <linux/pm.h>
21 #include <linux/fs.h>
22 #include <linux/console.h>
23 #include <linux/cpu.h>
25 #include <asm/uaccess.h>
27 #include "power.h"
29 #define SNAPSHOT_MINOR 231
31 static struct snapshot_data {
32 struct snapshot_handle handle;
33 int swap;
34 struct bitmap_page *bitmap;
35 int mode;
36 char frozen;
37 char ready;
38 } snapshot_state;
40 static atomic_t device_available = ATOMIC_INIT(1);
42 static int snapshot_open(struct inode *inode, struct file *filp)
44 struct snapshot_data *data;
46 if (!atomic_add_unless(&device_available, -1, 0))
47 return -EBUSY;
49 if ((filp->f_flags & O_ACCMODE) == O_RDWR)
50 return -ENOSYS;
52 nonseekable_open(inode, filp);
53 data = &snapshot_state;
54 filp->private_data = data;
55 memset(&data->handle, 0, sizeof(struct snapshot_handle));
56 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
57 data->swap = swsusp_resume_device ? swap_type_of(swsusp_resume_device) : -1;
58 data->mode = O_RDONLY;
59 } else {
60 data->swap = -1;
61 data->mode = O_WRONLY;
63 data->bitmap = NULL;
64 data->frozen = 0;
65 data->ready = 0;
67 return 0;
70 static int snapshot_release(struct inode *inode, struct file *filp)
72 struct snapshot_data *data;
74 swsusp_free();
75 data = filp->private_data;
76 free_all_swap_pages(data->swap, data->bitmap);
77 free_bitmap(data->bitmap);
78 if (data->frozen) {
79 down(&pm_sem);
80 thaw_processes();
81 enable_nonboot_cpus();
82 up(&pm_sem);
84 atomic_inc(&device_available);
85 return 0;
88 static ssize_t snapshot_read(struct file *filp, char __user *buf,
89 size_t count, loff_t *offp)
91 struct snapshot_data *data;
92 ssize_t res;
94 data = filp->private_data;
95 res = snapshot_read_next(&data->handle, count);
96 if (res > 0) {
97 if (copy_to_user(buf, data_of(data->handle), res))
98 res = -EFAULT;
99 else
100 *offp = data->handle.offset;
102 return res;
105 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
106 size_t count, loff_t *offp)
108 struct snapshot_data *data;
109 ssize_t res;
111 data = filp->private_data;
112 res = snapshot_write_next(&data->handle, count);
113 if (res > 0) {
114 if (copy_from_user(data_of(data->handle), buf, res))
115 res = -EFAULT;
116 else
117 *offp = data->handle.offset;
119 return res;
122 static int snapshot_ioctl(struct inode *inode, struct file *filp,
123 unsigned int cmd, unsigned long arg)
125 int error = 0;
126 struct snapshot_data *data;
127 loff_t offset, avail;
129 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
130 return -ENOTTY;
131 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
132 return -ENOTTY;
133 if (!capable(CAP_SYS_ADMIN))
134 return -EPERM;
136 data = filp->private_data;
138 switch (cmd) {
140 case SNAPSHOT_FREEZE:
141 if (data->frozen)
142 break;
143 down(&pm_sem);
144 error = disable_nonboot_cpus();
145 if (!error) {
146 error = freeze_processes();
147 if (error) {
148 thaw_processes();
149 enable_nonboot_cpus();
150 error = -EBUSY;
153 up(&pm_sem);
154 if (!error)
155 data->frozen = 1;
156 break;
158 case SNAPSHOT_UNFREEZE:
159 if (!data->frozen)
160 break;
161 down(&pm_sem);
162 thaw_processes();
163 enable_nonboot_cpus();
164 up(&pm_sem);
165 data->frozen = 0;
166 break;
168 case SNAPSHOT_ATOMIC_SNAPSHOT:
169 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
170 error = -EPERM;
171 break;
173 down(&pm_sem);
174 /* Free memory before shutting down devices. */
175 error = swsusp_shrink_memory();
176 if (!error) {
177 suspend_console();
178 error = device_suspend(PMSG_FREEZE);
179 if (!error) {
180 in_suspend = 1;
181 error = swsusp_suspend();
182 device_resume();
184 resume_console();
186 up(&pm_sem);
187 if (!error)
188 error = put_user(in_suspend, (unsigned int __user *)arg);
189 if (!error)
190 data->ready = 1;
191 break;
193 case SNAPSHOT_ATOMIC_RESTORE:
194 if (data->mode != O_WRONLY || !data->frozen ||
195 !snapshot_image_loaded(&data->handle)) {
196 error = -EPERM;
197 break;
199 snapshot_free_unused_memory(&data->handle);
200 down(&pm_sem);
201 pm_prepare_console();
202 suspend_console();
203 error = device_suspend(PMSG_PRETHAW);
204 if (!error) {
205 error = swsusp_resume();
206 device_resume();
208 resume_console();
209 pm_restore_console();
210 up(&pm_sem);
211 break;
213 case SNAPSHOT_FREE:
214 swsusp_free();
215 memset(&data->handle, 0, sizeof(struct snapshot_handle));
216 data->ready = 0;
217 break;
219 case SNAPSHOT_SET_IMAGE_SIZE:
220 image_size = arg;
221 break;
223 case SNAPSHOT_AVAIL_SWAP:
224 avail = count_swap_pages(data->swap, 1);
225 avail <<= PAGE_SHIFT;
226 error = put_user(avail, (loff_t __user *)arg);
227 break;
229 case SNAPSHOT_GET_SWAP_PAGE:
230 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
231 error = -ENODEV;
232 break;
234 if (!data->bitmap) {
235 data->bitmap = alloc_bitmap(count_swap_pages(data->swap, 0));
236 if (!data->bitmap) {
237 error = -ENOMEM;
238 break;
241 offset = alloc_swap_page(data->swap, data->bitmap);
242 if (offset) {
243 offset <<= PAGE_SHIFT;
244 error = put_user(offset, (loff_t __user *)arg);
245 } else {
246 error = -ENOSPC;
248 break;
250 case SNAPSHOT_FREE_SWAP_PAGES:
251 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
252 error = -ENODEV;
253 break;
255 free_all_swap_pages(data->swap, data->bitmap);
256 free_bitmap(data->bitmap);
257 data->bitmap = NULL;
258 break;
260 case SNAPSHOT_SET_SWAP_FILE:
261 if (!data->bitmap) {
263 * User space encodes device types as two-byte values,
264 * so we need to recode them
266 if (old_decode_dev(arg)) {
267 data->swap = swap_type_of(old_decode_dev(arg));
268 if (data->swap < 0)
269 error = -ENODEV;
270 } else {
271 data->swap = -1;
272 error = -EINVAL;
274 } else {
275 error = -EPERM;
277 break;
279 case SNAPSHOT_S2RAM:
280 if (!data->frozen) {
281 error = -EPERM;
282 break;
285 if (down_trylock(&pm_sem)) {
286 error = -EBUSY;
287 break;
290 if (pm_ops->prepare) {
291 error = pm_ops->prepare(PM_SUSPEND_MEM);
292 if (error)
293 goto OutS3;
296 /* Put devices to sleep */
297 suspend_console();
298 error = device_suspend(PMSG_SUSPEND);
299 if (error) {
300 printk(KERN_ERR "Failed to suspend some devices.\n");
301 } else {
302 /* Enter S3, system is already frozen */
303 suspend_enter(PM_SUSPEND_MEM);
305 /* Wake up devices */
306 device_resume();
308 resume_console();
309 if (pm_ops->finish)
310 pm_ops->finish(PM_SUSPEND_MEM);
312 OutS3:
313 up(&pm_sem);
314 break;
316 default:
317 error = -ENOTTY;
321 return error;
324 static struct file_operations snapshot_fops = {
325 .open = snapshot_open,
326 .release = snapshot_release,
327 .read = snapshot_read,
328 .write = snapshot_write,
329 .llseek = no_llseek,
330 .ioctl = snapshot_ioctl,
333 static struct miscdevice snapshot_device = {
334 .minor = SNAPSHOT_MINOR,
335 .name = "snapshot",
336 .fops = &snapshot_fops,
339 static int __init snapshot_device_init(void)
341 return misc_register(&snapshot_device);
344 device_initcall(snapshot_device_init);