blob: 98ade217da6c2c8d032c78a56d5358170d12819d [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>
Paul Gortmaker74da1ff2011-05-26 12:48:41 -040015#include <linux/kmod.h>
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080016#include <linux/string.h>
17#include <linux/device.h>
18#include <linux/miscdevice.h>
19#include <linux/mm.h>
20#include <linux/swap.h>
21#include <linux/swapops.h>
22#include <linux/pm.h>
23#include <linux/fs.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070024#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070025#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080026#include <linux/freezer.h>
Rafael J. Wysockic7510852009-04-12 20:06:56 +020027#include <scsi/scsi_scan.h>
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080028
29#include <asm/uaccess.h>
30
31#include "power.h"
32
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +020033/*
Rafael J. Wysocki96f73742007-10-26 01:02:15 +020034 * NOTE: The SNAPSHOT_SET_SWAP_FILE and SNAPSHOT_PMOPS ioctls are obsolete and
35 * will be removed in the future. They are only preserved here for
36 * compatibility with existing userland utilities.
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +020037 */
Rafael J. Wysocki96f73742007-10-26 01:02:15 +020038#define SNAPSHOT_SET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int)
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +020039#define SNAPSHOT_PMOPS _IOW(SNAPSHOT_IOC_MAGIC, 12, unsigned int)
40
41#define PMOPS_PREPARE 1
42#define PMOPS_ENTER 2
43#define PMOPS_FINISH 3
44
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +020045/*
46 * NOTE: The following ioctl definitions are wrong and have been replaced with
47 * correct ones. They are only preserved here for compatibility with existing
48 * userland utilities and will be removed in the future.
49 */
50#define SNAPSHOT_ATOMIC_SNAPSHOT _IOW(SNAPSHOT_IOC_MAGIC, 3, void *)
51#define SNAPSHOT_SET_IMAGE_SIZE _IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long)
52#define SNAPSHOT_AVAIL_SWAP _IOR(SNAPSHOT_IOC_MAGIC, 7, void *)
53#define SNAPSHOT_GET_SWAP_PAGE _IOR(SNAPSHOT_IOC_MAGIC, 8, void *)
54
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +020055
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080056#define SNAPSHOT_MINOR 231
57
58static struct snapshot_data {
59 struct snapshot_handle handle;
60 int swap;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080061 int mode;
62 char frozen;
63 char ready;
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +020064 char platform_support;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080065} snapshot_state;
66
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070067atomic_t snapshot_device_available = ATOMIC_INIT(1);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080068
69static int snapshot_open(struct inode *inode, struct file *filp)
70{
71 struct snapshot_data *data;
Alan Sternc3e94d82007-11-19 23:38:25 +010072 int error;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080073
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010074 lock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +020075
76 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
77 error = -EBUSY;
78 goto Unlock;
79 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080080
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070081 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070082 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +020083 error = -ENOSYS;
84 goto Unlock;
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070085 }
86 if(create_basic_memory_bitmaps()) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070087 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +020088 error = -ENOMEM;
89 goto Unlock;
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070090 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080091 nonseekable_open(inode, filp);
92 data = &snapshot_state;
93 filp->private_data = data;
94 memset(&data->handle, 0, sizeof(struct snapshot_handle));
95 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
Rafael J. Wysockic7510852009-04-12 20:06:56 +020096 /* Hibernating. The image device should be accessible. */
Rafael J. Wysocki915bae92006-12-06 20:34:07 -080097 data->swap = swsusp_resume_device ?
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -080098 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080099 data->mode = O_RDONLY;
Alan Sternc3e94d82007-11-19 23:38:25 +0100100 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
101 if (error)
102 pm_notifier_call_chain(PM_POST_HIBERNATION);
Andrey Borzenkovebae2602009-02-14 02:05:14 +0100103 } else {
Rafael J. Wysockic7510852009-04-12 20:06:56 +0200104 /*
105 * Resuming. We may need to wait for the image device to
106 * appear.
107 */
108 wait_for_device_probe();
109 scsi_complete_async_scans();
110
Andrey Borzenkovebae2602009-02-14 02:05:14 +0100111 data->swap = -1;
112 data->mode = O_WRONLY;
113 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
114 if (error)
115 pm_notifier_call_chain(PM_POST_RESTORE);
Alan Sternc3e94d82007-11-19 23:38:25 +0100116 }
Michal Kubecek8440f4b2011-06-18 20:34:01 +0200117 if (error) {
118 free_basic_memory_bitmaps();
Alan Sternc3e94d82007-11-19 23:38:25 +0100119 atomic_inc(&snapshot_device_available);
Michal Kubecek8440f4b2011-06-18 20:34:01 +0200120 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800121 data->frozen = 0;
122 data->ready = 0;
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200123 data->platform_support = 0;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800124
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200125 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100126 unlock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200127
128 return error;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800129}
130
131static int snapshot_release(struct inode *inode, struct file *filp)
132{
133 struct snapshot_data *data;
134
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100135 lock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200136
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800137 swsusp_free();
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700138 free_basic_memory_bitmaps();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800139 data = filp->private_data;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700140 free_all_swap_pages(data->swap);
Rafael J. Wysocki97449972011-05-10 21:10:01 +0200141 if (data->frozen) {
142 pm_restore_gfp_mask();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800143 thaw_processes();
Rafael J. Wysocki97449972011-05-10 21:10:01 +0200144 }
Takashi Iwai1497dd12010-12-10 00:16:39 +0100145 pm_notifier_call_chain(data->mode == O_RDONLY ?
Alan Sternc3e94d82007-11-19 23:38:25 +0100146 PM_POST_HIBERNATION : PM_POST_RESTORE);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700147 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200148
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100149 unlock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200150
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800151 return 0;
152}
153
154static ssize_t snapshot_read(struct file *filp, char __user *buf,
155 size_t count, loff_t *offp)
156{
157 struct snapshot_data *data;
158 ssize_t res;
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200159 loff_t pg_offp = *offp & ~PAGE_MASK;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800160
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100161 lock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200162
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800163 data = filp->private_data;
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200164 if (!data->ready) {
165 res = -ENODATA;
166 goto Unlock;
167 }
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200168 if (!pg_offp) { /* on page boundary? */
169 res = snapshot_read_next(&data->handle);
170 if (res <= 0)
171 goto Unlock;
172 } else {
173 res = PAGE_SIZE - pg_offp;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800174 }
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200175
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200176 res = simple_read_from_buffer(buf, count, &pg_offp,
177 data_of(data->handle), res);
178 if (res > 0)
179 *offp += res;
180
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200181 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100182 unlock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200183
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800184 return res;
185}
186
187static ssize_t snapshot_write(struct file *filp, const char __user *buf,
188 size_t count, loff_t *offp)
189{
190 struct snapshot_data *data;
191 ssize_t res;
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200192 loff_t pg_offp = *offp & ~PAGE_MASK;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800193
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100194 lock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200195
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800196 data = filp->private_data;
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200197
198 if (!pg_offp) {
199 res = snapshot_write_next(&data->handle);
200 if (res <= 0)
201 goto unlock;
202 } else {
203 res = PAGE_SIZE - pg_offp;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800204 }
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200205
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200206 res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
207 buf, count);
208 if (res > 0)
209 *offp += res;
210unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100211 unlock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200212
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800213 return res;
214}
215
Jiri Slabyb694e522010-01-27 23:47:50 +0100216static void snapshot_deprecated_ioctl(unsigned int cmd)
217{
218 if (printk_ratelimit())
219 printk(KERN_NOTICE "%pf: ioctl '%.8x' is deprecated and will "
220 "be removed soon, update your suspend-to-disk "
221 "utilities\n",
222 __builtin_return_address(0), cmd);
223}
224
Alan Cox52d11022008-06-11 22:07:52 +0200225static long snapshot_ioctl(struct file *filp, unsigned int cmd,
226 unsigned long arg)
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800227{
228 int error = 0;
229 struct snapshot_data *data;
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +0200230 loff_t size;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800231 sector_t offset;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800232
233 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
234 return -ENOTTY;
235 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
236 return -ENOTTY;
237 if (!capable(CAP_SYS_ADMIN))
238 return -EPERM;
239
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200240 if (!mutex_trylock(&pm_mutex))
241 return -EBUSY;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800242
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200243 data = filp->private_data;
Alan Cox52d11022008-06-11 22:07:52 +0200244
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800245 switch (cmd) {
246
247 case SNAPSHOT_FREEZE:
248 if (data->frozen)
249 break;
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700250
Alan Sternc3e94d82007-11-19 23:38:25 +0100251 printk("Syncing filesystems ... ");
252 sys_sync();
253 printk("done.\n");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700254
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700255 error = usermodehelper_disable();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700256 if (error)
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700257 break;
258
259 error = freeze_processes();
Tejun Heo03afed82011-11-21 12:32:24 -0800260 if (error)
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700261 usermodehelper_enable();
Srivatsa S. Bhate5b16742011-12-03 00:20:30 +0100262 else
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800263 data->frozen = 1;
264 break;
265
266 case SNAPSHOT_UNFREEZE:
Rafael J. Wysocki2f41ddd2007-06-16 10:16:03 -0700267 if (!data->frozen || data->ready)
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800268 break;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100269 pm_restore_gfp_mask();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800270 thaw_processes();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700271 usermodehelper_enable();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800272 data->frozen = 0;
273 break;
274
275 case SNAPSHOT_ATOMIC_SNAPSHOT:
Jiri Slabyb694e522010-01-27 23:47:50 +0100276 snapshot_deprecated_ioctl(cmd);
277 case SNAPSHOT_CREATE_IMAGE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800278 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
279 error = -EPERM;
280 break;
281 }
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100282 pm_restore_gfp_mask();
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200283 error = hibernation_snapshot(data->platform_support);
Srivatsa S. Bhat97819a22011-12-01 22:33:10 +0100284 if (!error) {
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200285 error = put_user(in_suspend, (int __user *)arg);
Srivatsa S. Bhat97819a22011-12-01 22:33:10 +0100286 if (!error && !freezer_test_done)
287 data->ready = 1;
288 if (freezer_test_done) {
289 freezer_test_done = false;
290 thaw_processes();
291 }
292 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800293 break;
294
295 case SNAPSHOT_ATOMIC_RESTORE:
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800296 snapshot_write_finalize(&data->handle);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800297 if (data->mode != O_WRONLY || !data->frozen ||
298 !snapshot_image_loaded(&data->handle)) {
299 error = -EPERM;
300 break;
301 }
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200302 error = hibernation_restore(data->platform_support);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800303 break;
304
305 case SNAPSHOT_FREE:
306 swsusp_free();
307 memset(&data->handle, 0, sizeof(struct snapshot_handle));
308 data->ready = 0;
309 break;
310
311 case SNAPSHOT_SET_IMAGE_SIZE:
Jiri Slabyb694e522010-01-27 23:47:50 +0100312 snapshot_deprecated_ioctl(cmd);
313 case SNAPSHOT_PREF_IMAGE_SIZE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800314 image_size = arg;
315 break;
316
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +0200317 case SNAPSHOT_GET_IMAGE_SIZE:
318 if (!data->ready) {
319 error = -ENODATA;
320 break;
321 }
322 size = snapshot_get_image_size();
323 size <<= PAGE_SHIFT;
324 error = put_user(size, (loff_t __user *)arg);
325 break;
326
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800327 case SNAPSHOT_AVAIL_SWAP:
Jiri Slabyb694e522010-01-27 23:47:50 +0100328 snapshot_deprecated_ioctl(cmd);
329 case SNAPSHOT_AVAIL_SWAP_SIZE:
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +0200330 size = count_swap_pages(data->swap, 1);
331 size <<= PAGE_SHIFT;
332 error = put_user(size, (loff_t __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800333 break;
334
335 case SNAPSHOT_GET_SWAP_PAGE:
Jiri Slabyb694e522010-01-27 23:47:50 +0100336 snapshot_deprecated_ioctl(cmd);
337 case SNAPSHOT_ALLOC_SWAP_PAGE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800338 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
339 error = -ENODEV;
340 break;
341 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700342 offset = alloc_swapdev_block(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800343 if (offset) {
344 offset <<= PAGE_SHIFT;
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200345 error = put_user(offset, (loff_t __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800346 } else {
347 error = -ENOSPC;
348 }
349 break;
350
351 case SNAPSHOT_FREE_SWAP_PAGES:
352 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
353 error = -ENODEV;
354 break;
355 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700356 free_all_swap_pages(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800357 break;
358
Rafael J. Wysocki96f73742007-10-26 01:02:15 +0200359 case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
Jiri Slabyb694e522010-01-27 23:47:50 +0100360 snapshot_deprecated_ioctl(cmd);
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700361 if (!swsusp_swap_in_use()) {
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800362 /*
363 * User space encodes device types as two-byte values,
364 * so we need to recode them
365 */
366 if (old_decode_dev(arg)) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800367 data->swap = swap_type_of(old_decode_dev(arg),
368 0, NULL);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800369 if (data->swap < 0)
370 error = -ENODEV;
371 } else {
372 data->swap = -1;
373 error = -EINVAL;
374 }
375 } else {
376 error = -EPERM;
377 }
378 break;
379
Luca Tettamanti9b238202006-03-23 03:00:09 -0800380 case SNAPSHOT_S2RAM:
381 if (!data->frozen) {
382 error = -EPERM;
383 break;
384 }
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700385 /*
386 * Tasks are frozen and the notifiers have been called with
387 * PM_HIBERNATION_PREPARE
388 */
389 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
Rafael J. Wysocki36cb7032011-05-10 21:10:13 +0200390 data->ready = 0;
Luca Tettamanti9b238202006-03-23 03:00:09 -0800391 break;
392
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200393 case SNAPSHOT_PLATFORM_SUPPORT:
394 data->platform_support = !!arg;
395 break;
396
397 case SNAPSHOT_POWER_OFF:
398 if (data->platform_support)
399 error = hibernation_platform_enter();
400 break;
401
402 case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
Jiri Slabyb694e522010-01-27 23:47:50 +0100403 snapshot_deprecated_ioctl(cmd);
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800404 error = -EINVAL;
405
Stefan Seyfried35926952006-12-06 20:34:06 -0800406 switch (arg) {
407
408 case PMOPS_PREPARE:
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200409 data->platform_support = 1;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700410 error = 0;
Stefan Seyfried35926952006-12-06 20:34:06 -0800411 break;
412
413 case PMOPS_ENTER:
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200414 if (data->platform_support)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700415 error = hibernation_platform_enter();
Stefan Seyfried35926952006-12-06 20:34:06 -0800416 break;
417
418 case PMOPS_FINISH:
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200419 if (data->platform_support)
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800420 error = 0;
Stefan Seyfried35926952006-12-06 20:34:06 -0800421 break;
422
423 default:
424 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
Stefan Seyfried35926952006-12-06 20:34:06 -0800425
426 }
427 break;
428
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800429 case SNAPSHOT_SET_SWAP_AREA:
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700430 if (swsusp_swap_in_use()) {
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800431 error = -EPERM;
432 } else {
433 struct resume_swap_area swap_area;
434 dev_t swdev;
435
436 error = copy_from_user(&swap_area, (void __user *)arg,
437 sizeof(struct resume_swap_area));
438 if (error) {
439 error = -EFAULT;
440 break;
441 }
442
443 /*
444 * User space encodes device types as two-byte values,
445 * so we need to recode them
446 */
Jiri Slabyd88d4052010-04-10 22:28:56 +0200447 swdev = new_decode_dev(swap_area.dev);
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800448 if (swdev) {
449 offset = swap_area.offset;
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800450 data->swap = swap_type_of(swdev, offset, NULL);
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800451 if (data->swap < 0)
452 error = -ENODEV;
453 } else {
454 data->swap = -1;
455 error = -EINVAL;
456 }
457 }
458 break;
459
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800460 default:
461 error = -ENOTTY;
462
463 }
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200464
465 mutex_unlock(&pm_mutex);
466
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800467 return error;
468}
469
Helge Deller15ad7cd2006-12-06 20:40:36 -0800470static const struct file_operations snapshot_fops = {
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800471 .open = snapshot_open,
472 .release = snapshot_release,
473 .read = snapshot_read,
474 .write = snapshot_write,
475 .llseek = no_llseek,
Alan Cox52d11022008-06-11 22:07:52 +0200476 .unlocked_ioctl = snapshot_ioctl,
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800477};
478
479static struct miscdevice snapshot_device = {
480 .minor = SNAPSHOT_MINOR,
481 .name = "snapshot",
482 .fops = &snapshot_fops,
483};
484
485static int __init snapshot_device_init(void)
486{
487 return misc_register(&snapshot_device);
488};
489
490device_initcall(snapshot_device_init);