blob: 7f19afe01b48b332013fbc1757638829af6633e9 [file] [log] [blame]
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001/*
2 * linux/kernel/power/user.c
3 *
4 * This file provides the user space interface for software suspend/resume.
5 *
6 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
7 *
8 * This file is released under the GPLv2.
9 *
10 */
11
12#include <linux/suspend.h>
13#include <linux/syscalls.h>
Stefan Seyfried35926952006-12-06 20:34:06 -080014#include <linux/reboot.h>
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080015#include <linux/string.h>
16#include <linux/device.h>
17#include <linux/miscdevice.h>
18#include <linux/mm.h>
19#include <linux/swap.h>
20#include <linux/swapops.h>
21#include <linux/pm.h>
22#include <linux/fs.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070023#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070024#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080025#include <linux/freezer.h>
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080026
27#include <asm/uaccess.h>
28
29#include "power.h"
30
31#define SNAPSHOT_MINOR 231
32
33static struct snapshot_data {
34 struct snapshot_handle handle;
35 int swap;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080036 int mode;
37 char frozen;
38 char ready;
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -080039 char platform_suspend;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080040} snapshot_state;
41
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070042atomic_t snapshot_device_available = ATOMIC_INIT(1);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080043
44static int snapshot_open(struct inode *inode, struct file *filp)
45{
46 struct snapshot_data *data;
47
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070048 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080049 return -EBUSY;
50
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070051 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070052 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080053 return -ENOSYS;
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070054 }
55 if(create_basic_memory_bitmaps()) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070056 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -070057 return -ENOMEM;
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070058 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080059 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) {
Rafael J. Wysocki915bae92006-12-06 20:34:07 -080064 data->swap = swsusp_resume_device ?
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -080065 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080066 data->mode = O_RDONLY;
67 } else {
68 data->swap = -1;
69 data->mode = O_WRONLY;
70 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080071 data->frozen = 0;
72 data->ready = 0;
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -080073 data->platform_suspend = 0;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080074
75 return 0;
76}
77
78static int snapshot_release(struct inode *inode, struct file *filp)
79{
80 struct snapshot_data *data;
81
82 swsusp_free();
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -070083 free_basic_memory_bitmaps();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080084 data = filp->private_data;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -070085 free_all_swap_pages(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080086 if (data->frozen) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -080087 mutex_lock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080088 thaw_processes();
Stephen Hemmingera6d70982006-12-06 20:34:35 -080089 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080090 }
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070091 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080092 return 0;
93}
94
95static ssize_t snapshot_read(struct file *filp, char __user *buf,
96 size_t count, loff_t *offp)
97{
98 struct snapshot_data *data;
99 ssize_t res;
100
101 data = filp->private_data;
Rafael J. Wysocki2f41ddd2007-06-16 10:16:03 -0700102 if (!data->ready)
103 return -ENODATA;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800104 res = snapshot_read_next(&data->handle, count);
105 if (res > 0) {
106 if (copy_to_user(buf, data_of(data->handle), res))
107 res = -EFAULT;
108 else
109 *offp = data->handle.offset;
110 }
111 return res;
112}
113
114static ssize_t snapshot_write(struct file *filp, const char __user *buf,
115 size_t count, loff_t *offp)
116{
117 struct snapshot_data *data;
118 ssize_t res;
119
120 data = filp->private_data;
121 res = snapshot_write_next(&data->handle, count);
122 if (res > 0) {
123 if (copy_from_user(data_of(data->handle), buf, res))
124 res = -EFAULT;
125 else
126 *offp = data->handle.offset;
127 }
128 return res;
129}
130
131static int snapshot_ioctl(struct inode *inode, struct file *filp,
132 unsigned int cmd, unsigned long arg)
133{
134 int error = 0;
135 struct snapshot_data *data;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800136 loff_t avail;
137 sector_t offset;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800138
139 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
140 return -ENOTTY;
141 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
142 return -ENOTTY;
143 if (!capable(CAP_SYS_ADMIN))
144 return -EPERM;
145
146 data = filp->private_data;
147
148 switch (cmd) {
149
150 case SNAPSHOT_FREEZE:
151 if (data->frozen)
152 break;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800153 mutex_lock(&pm_mutex);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700154 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
155 if (!error) {
156 error = freeze_processes();
157 if (error)
158 thaw_processes();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800159 }
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700160 if (error)
161 pm_notifier_call_chain(PM_POST_HIBERNATION);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800162 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800163 if (!error)
164 data->frozen = 1;
165 break;
166
167 case SNAPSHOT_UNFREEZE:
Rafael J. Wysocki2f41ddd2007-06-16 10:16:03 -0700168 if (!data->frozen || data->ready)
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800169 break;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800170 mutex_lock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800171 thaw_processes();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700172 pm_notifier_call_chain(PM_POST_HIBERNATION);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800173 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800174 data->frozen = 0;
175 break;
176
177 case SNAPSHOT_ATOMIC_SNAPSHOT:
178 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
179 error = -EPERM;
180 break;
181 }
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700182 error = hibernation_snapshot(data->platform_suspend);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800183 if (!error)
184 error = put_user(in_suspend, (unsigned int __user *)arg);
185 if (!error)
186 data->ready = 1;
187 break;
188
189 case SNAPSHOT_ATOMIC_RESTORE:
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800190 snapshot_write_finalize(&data->handle);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800191 if (data->mode != O_WRONLY || !data->frozen ||
192 !snapshot_image_loaded(&data->handle)) {
193 error = -EPERM;
194 break;
195 }
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700196 error = hibernation_restore(data->platform_suspend);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800197 break;
198
199 case SNAPSHOT_FREE:
200 swsusp_free();
201 memset(&data->handle, 0, sizeof(struct snapshot_handle));
202 data->ready = 0;
203 break;
204
205 case SNAPSHOT_SET_IMAGE_SIZE:
206 image_size = arg;
207 break;
208
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);
213 break;
214
215 case SNAPSHOT_GET_SWAP_PAGE:
216 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
217 error = -ENODEV;
218 break;
219 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700220 offset = alloc_swapdev_block(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800221 if (offset) {
222 offset <<= PAGE_SHIFT;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800223 error = put_user(offset, (sector_t __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800224 } else {
225 error = -ENOSPC;
226 }
227 break;
228
229 case SNAPSHOT_FREE_SWAP_PAGES:
230 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
231 error = -ENODEV;
232 break;
233 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700234 free_all_swap_pages(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800235 break;
236
237 case SNAPSHOT_SET_SWAP_FILE:
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700238 if (!swsusp_swap_in_use()) {
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800239 /*
240 * User space encodes device types as two-byte values,
241 * so we need to recode them
242 */
243 if (old_decode_dev(arg)) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800244 data->swap = swap_type_of(old_decode_dev(arg),
245 0, NULL);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800246 if (data->swap < 0)
247 error = -ENODEV;
248 } else {
249 data->swap = -1;
250 error = -EINVAL;
251 }
252 } else {
253 error = -EPERM;
254 }
255 break;
256
Luca Tettamanti9b238202006-03-23 03:00:09 -0800257 case SNAPSHOT_S2RAM:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800258 if (!pm_ops) {
259 error = -ENOSYS;
260 break;
261 }
262
Luca Tettamanti9b238202006-03-23 03:00:09 -0800263 if (!data->frozen) {
264 error = -EPERM;
265 break;
266 }
267
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800268 if (!mutex_trylock(&pm_mutex)) {
Luca Tettamanti9b238202006-03-23 03:00:09 -0800269 error = -EBUSY;
270 break;
271 }
272
273 if (pm_ops->prepare) {
274 error = pm_ops->prepare(PM_SUSPEND_MEM);
275 if (error)
276 goto OutS3;
277 }
278
279 /* Put devices to sleep */
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700280 suspend_console();
Luca Tettamanti9b238202006-03-23 03:00:09 -0800281 error = device_suspend(PMSG_SUSPEND);
282 if (error) {
283 printk(KERN_ERR "Failed to suspend some devices.\n");
284 } else {
Rafael J. Wysocki93c9a7f2007-03-22 00:11:20 -0800285 error = disable_nonboot_cpus();
286 if (!error) {
287 /* Enter S3, system is already frozen */
288 suspend_enter(PM_SUSPEND_MEM);
289 enable_nonboot_cpus();
290 }
Luca Tettamanti9b238202006-03-23 03:00:09 -0800291 /* Wake up devices */
292 device_resume();
293 }
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700294 resume_console();
Luca Tettamanti9b238202006-03-23 03:00:09 -0800295 if (pm_ops->finish)
296 pm_ops->finish(PM_SUSPEND_MEM);
297
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800298 OutS3:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800299 mutex_unlock(&pm_mutex);
Luca Tettamanti9b238202006-03-23 03:00:09 -0800300 break;
301
Stefan Seyfried35926952006-12-06 20:34:06 -0800302 case SNAPSHOT_PMOPS:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800303 error = -EINVAL;
304
Stefan Seyfried35926952006-12-06 20:34:06 -0800305 switch (arg) {
306
307 case PMOPS_PREPARE:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700308 data->platform_suspend = 1;
309 error = 0;
Stefan Seyfried35926952006-12-06 20:34:06 -0800310 break;
311
312 case PMOPS_ENTER:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700313 if (data->platform_suspend)
314 error = hibernation_platform_enter();
315
Stefan Seyfried35926952006-12-06 20:34:06 -0800316 break;
317
318 case PMOPS_FINISH:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800319 if (data->platform_suspend)
320 error = 0;
321
Stefan Seyfried35926952006-12-06 20:34:06 -0800322 break;
323
324 default:
325 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
Stefan Seyfried35926952006-12-06 20:34:06 -0800326
327 }
328 break;
329
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800330 case SNAPSHOT_SET_SWAP_AREA:
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700331 if (swsusp_swap_in_use()) {
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800332 error = -EPERM;
333 } else {
334 struct resume_swap_area swap_area;
335 dev_t swdev;
336
337 error = copy_from_user(&swap_area, (void __user *)arg,
338 sizeof(struct resume_swap_area));
339 if (error) {
340 error = -EFAULT;
341 break;
342 }
343
344 /*
345 * User space encodes device types as two-byte values,
346 * so we need to recode them
347 */
348 swdev = old_decode_dev(swap_area.dev);
349 if (swdev) {
350 offset = swap_area.offset;
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800351 data->swap = swap_type_of(swdev, offset, NULL);
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800352 if (data->swap < 0)
353 error = -ENODEV;
354 } else {
355 data->swap = -1;
356 error = -EINVAL;
357 }
358 }
359 break;
360
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800361 default:
362 error = -ENOTTY;
363
364 }
365
366 return error;
367}
368
Helge Deller15ad7cd2006-12-06 20:40:36 -0800369static const struct file_operations snapshot_fops = {
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800370 .open = snapshot_open,
371 .release = snapshot_release,
372 .read = snapshot_read,
373 .write = snapshot_write,
374 .llseek = no_llseek,
375 .ioctl = snapshot_ioctl,
376};
377
378static struct miscdevice snapshot_device = {
379 .minor = SNAPSHOT_MINOR,
380 .name = "snapshot",
381 .fops = &snapshot_fops,
382};
383
384static int __init snapshot_device_init(void)
385{
386 return misc_register(&snapshot_device);
387};
388
389device_initcall(snapshot_device_init);