pandora: defconfig: update
[pandora-kernel.git] / kernel / power / user.c
1 /*
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>
14 #include <linux/reboot.h>
15 #include <linux/kmod.h>
16 #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>
24 #include <linux/console.h>
25 #include <linux/cpu.h>
26 #include <linux/freezer.h>
27 #include <scsi/scsi_scan.h>
28
29 #include <asm/uaccess.h>
30
31 #include "power.h"
32
33 /*
34  * 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.
37  */
38 #define SNAPSHOT_SET_SWAP_FILE  _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int)
39 #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
45 /*
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
55
56 #define SNAPSHOT_MINOR  231
57
58 static struct snapshot_data {
59         struct snapshot_handle handle;
60         int swap;
61         int mode;
62         char frozen;
63         char ready;
64         char platform_support;
65 } snapshot_state;
66
67 atomic_t snapshot_device_available = ATOMIC_INIT(1);
68
69 static int snapshot_open(struct inode *inode, struct file *filp)
70 {
71         struct snapshot_data *data;
72         int error;
73
74         mutex_lock(&pm_mutex);
75
76         if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
77                 error = -EBUSY;
78                 goto Unlock;
79         }
80
81         if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
82                 atomic_inc(&snapshot_device_available);
83                 error = -ENOSYS;
84                 goto Unlock;
85         }
86         if(create_basic_memory_bitmaps()) {
87                 atomic_inc(&snapshot_device_available);
88                 error = -ENOMEM;
89                 goto Unlock;
90         }
91         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) {
96                 /* Hibernating.  The image device should be accessible. */
97                 data->swap = swsusp_resume_device ?
98                         swap_type_of(swsusp_resume_device, 0, NULL) : -1;
99                 data->mode = O_RDONLY;
100                 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
101                 if (error)
102                         pm_notifier_call_chain(PM_POST_HIBERNATION);
103         } else {
104                 /*
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
111                 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);
116         }
117         if (error) {
118                 free_basic_memory_bitmaps();
119                 atomic_inc(&snapshot_device_available);
120         }
121         data->frozen = 0;
122         data->ready = 0;
123         data->platform_support = 0;
124
125  Unlock:
126         mutex_unlock(&pm_mutex);
127
128         return error;
129 }
130
131 static int snapshot_release(struct inode *inode, struct file *filp)
132 {
133         struct snapshot_data *data;
134
135         mutex_lock(&pm_mutex);
136
137         swsusp_free();
138         free_basic_memory_bitmaps();
139         data = filp->private_data;
140         free_all_swap_pages(data->swap);
141         if (data->frozen) {
142                 pm_restore_gfp_mask();
143                 thaw_processes();
144         }
145         pm_notifier_call_chain(data->mode == O_RDONLY ?
146                         PM_POST_HIBERNATION : PM_POST_RESTORE);
147         atomic_inc(&snapshot_device_available);
148
149         mutex_unlock(&pm_mutex);
150
151         return 0;
152 }
153
154 static 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;
159         loff_t pg_offp = *offp & ~PAGE_MASK;
160
161         mutex_lock(&pm_mutex);
162
163         data = filp->private_data;
164         if (!data->ready) {
165                 res = -ENODATA;
166                 goto Unlock;
167         }
168         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;
174         }
175
176         res = simple_read_from_buffer(buf, count, &pg_offp,
177                         data_of(data->handle), res);
178         if (res > 0)
179                 *offp += res;
180
181  Unlock:
182         mutex_unlock(&pm_mutex);
183
184         return res;
185 }
186
187 static 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;
192         loff_t pg_offp = *offp & ~PAGE_MASK;
193
194         mutex_lock(&pm_mutex);
195
196         data = filp->private_data;
197
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;
204         }
205
206         res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
207                         buf, count);
208         if (res > 0)
209                 *offp += res;
210 unlock:
211         mutex_unlock(&pm_mutex);
212
213         return res;
214 }
215
216 static 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
225 static long snapshot_ioctl(struct file *filp, unsigned int cmd,
226                                                         unsigned long arg)
227 {
228         int error = 0;
229         struct snapshot_data *data;
230         loff_t size;
231         sector_t offset;
232
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
240         if (!mutex_trylock(&pm_mutex))
241                 return -EBUSY;
242
243         data = filp->private_data;
244
245         switch (cmd) {
246
247         case SNAPSHOT_FREEZE:
248                 if (data->frozen)
249                         break;
250
251                 printk("Syncing filesystems ... ");
252                 sys_sync();
253                 printk("done.\n");
254
255                 error = usermodehelper_disable();
256                 if (error)
257                         break;
258
259                 error = freeze_processes();
260                 if (error) {
261                         thaw_processes();
262                         usermodehelper_enable();
263                 }
264                 if (!error)
265                         data->frozen = 1;
266                 break;
267
268         case SNAPSHOT_UNFREEZE:
269                 if (!data->frozen || data->ready)
270                         break;
271                 pm_restore_gfp_mask();
272                 thaw_processes();
273                 usermodehelper_enable();
274                 data->frozen = 0;
275                 break;
276
277         case SNAPSHOT_ATOMIC_SNAPSHOT:
278                 snapshot_deprecated_ioctl(cmd);
279         case SNAPSHOT_CREATE_IMAGE:
280                 if (data->mode != O_RDONLY || !data->frozen  || data->ready) {
281                         error = -EPERM;
282                         break;
283                 }
284                 pm_restore_gfp_mask();
285                 error = hibernation_snapshot(data->platform_support);
286                 if (error) {
287                         thaw_kernel_threads();
288                 } else {
289                         error = put_user(in_suspend, (int __user *)arg);
290                         if (!error && !freezer_test_done)
291                                 data->ready = 1;
292                         if (freezer_test_done) {
293                                 freezer_test_done = false;
294                                 thaw_kernel_threads();
295                         }
296                 }
297                 break;
298
299         case SNAPSHOT_ATOMIC_RESTORE:
300                 snapshot_write_finalize(&data->handle);
301                 if (data->mode != O_WRONLY || !data->frozen ||
302                     !snapshot_image_loaded(&data->handle)) {
303                         error = -EPERM;
304                         break;
305                 }
306                 error = hibernation_restore(data->platform_support);
307                 break;
308
309         case SNAPSHOT_FREE:
310                 swsusp_free();
311                 memset(&data->handle, 0, sizeof(struct snapshot_handle));
312                 data->ready = 0;
313                 /*
314                  * It is necessary to thaw kernel threads here, because
315                  * SNAPSHOT_CREATE_IMAGE may be invoked directly after
316                  * SNAPSHOT_FREE.  In that case, if kernel threads were not
317                  * thawed, the preallocation of memory carried out by
318                  * hibernation_snapshot() might run into problems (i.e. it
319                  * might fail or even deadlock).
320                  */
321                 thaw_kernel_threads();
322                 break;
323
324         case SNAPSHOT_SET_IMAGE_SIZE:
325                 snapshot_deprecated_ioctl(cmd);
326         case SNAPSHOT_PREF_IMAGE_SIZE:
327                 image_size = arg;
328                 break;
329
330         case SNAPSHOT_GET_IMAGE_SIZE:
331                 if (!data->ready) {
332                         error = -ENODATA;
333                         break;
334                 }
335                 size = snapshot_get_image_size();
336                 size <<= PAGE_SHIFT;
337                 error = put_user(size, (loff_t __user *)arg);
338                 break;
339
340         case SNAPSHOT_AVAIL_SWAP:
341                 snapshot_deprecated_ioctl(cmd);
342         case SNAPSHOT_AVAIL_SWAP_SIZE:
343                 size = count_swap_pages(data->swap, 1);
344                 size <<= PAGE_SHIFT;
345                 error = put_user(size, (loff_t __user *)arg);
346                 break;
347
348         case SNAPSHOT_GET_SWAP_PAGE:
349                 snapshot_deprecated_ioctl(cmd);
350         case SNAPSHOT_ALLOC_SWAP_PAGE:
351                 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
352                         error = -ENODEV;
353                         break;
354                 }
355                 offset = alloc_swapdev_block(data->swap);
356                 if (offset) {
357                         offset <<= PAGE_SHIFT;
358                         error = put_user(offset, (loff_t __user *)arg);
359                 } else {
360                         error = -ENOSPC;
361                 }
362                 break;
363
364         case SNAPSHOT_FREE_SWAP_PAGES:
365                 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
366                         error = -ENODEV;
367                         break;
368                 }
369                 free_all_swap_pages(data->swap);
370                 break;
371
372         case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
373                 snapshot_deprecated_ioctl(cmd);
374                 if (!swsusp_swap_in_use()) {
375                         /*
376                          * User space encodes device types as two-byte values,
377                          * so we need to recode them
378                          */
379                         if (old_decode_dev(arg)) {
380                                 data->swap = swap_type_of(old_decode_dev(arg),
381                                                         0, NULL);
382                                 if (data->swap < 0)
383                                         error = -ENODEV;
384                         } else {
385                                 data->swap = -1;
386                                 error = -EINVAL;
387                         }
388                 } else {
389                         error = -EPERM;
390                 }
391                 break;
392
393         case SNAPSHOT_S2RAM:
394                 if (!data->frozen) {
395                         error = -EPERM;
396                         break;
397                 }
398                 /*
399                  * Tasks are frozen and the notifiers have been called with
400                  * PM_HIBERNATION_PREPARE
401                  */
402                 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
403                 data->ready = 0;
404                 break;
405
406         case SNAPSHOT_PLATFORM_SUPPORT:
407                 data->platform_support = !!arg;
408                 break;
409
410         case SNAPSHOT_POWER_OFF:
411                 if (data->platform_support)
412                         error = hibernation_platform_enter();
413                 break;
414
415         case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
416                 snapshot_deprecated_ioctl(cmd);
417                 error = -EINVAL;
418
419                 switch (arg) {
420
421                 case PMOPS_PREPARE:
422                         data->platform_support = 1;
423                         error = 0;
424                         break;
425
426                 case PMOPS_ENTER:
427                         if (data->platform_support)
428                                 error = hibernation_platform_enter();
429                         break;
430
431                 case PMOPS_FINISH:
432                         if (data->platform_support)
433                                 error = 0;
434                         break;
435
436                 default:
437                         printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
438
439                 }
440                 break;
441
442         case SNAPSHOT_SET_SWAP_AREA:
443                 if (swsusp_swap_in_use()) {
444                         error = -EPERM;
445                 } else {
446                         struct resume_swap_area swap_area;
447                         dev_t swdev;
448
449                         error = copy_from_user(&swap_area, (void __user *)arg,
450                                         sizeof(struct resume_swap_area));
451                         if (error) {
452                                 error = -EFAULT;
453                                 break;
454                         }
455
456                         /*
457                          * User space encodes device types as two-byte values,
458                          * so we need to recode them
459                          */
460                         swdev = new_decode_dev(swap_area.dev);
461                         if (swdev) {
462                                 offset = swap_area.offset;
463                                 data->swap = swap_type_of(swdev, offset, NULL);
464                                 if (data->swap < 0)
465                                         error = -ENODEV;
466                         } else {
467                                 data->swap = -1;
468                                 error = -EINVAL;
469                         }
470                 }
471                 break;
472
473         default:
474                 error = -ENOTTY;
475
476         }
477
478         mutex_unlock(&pm_mutex);
479
480         return error;
481 }
482
483 static const struct file_operations snapshot_fops = {
484         .open = snapshot_open,
485         .release = snapshot_release,
486         .read = snapshot_read,
487         .write = snapshot_write,
488         .llseek = no_llseek,
489         .unlocked_ioctl = snapshot_ioctl,
490 };
491
492 static struct miscdevice snapshot_device = {
493         .minor = SNAPSHOT_MINOR,
494         .name = "snapshot",
495         .fops = &snapshot_fops,
496 };
497
498 static int __init snapshot_device_init(void)
499 {
500         return misc_register(&snapshot_device);
501 };
502
503 device_initcall(snapshot_device_init);