]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - kernel/power/user.c
Hibernation: Mark SNAPSHOT_SET_SWAP_FILE ioctl as deprecated (rev. 2)
[linux-2.6-omap-h63xx.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/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>
23 #include <linux/console.h>
24 #include <linux/cpu.h>
25 #include <linux/freezer.h>
26
27 #include <asm/uaccess.h>
28
29 #include "power.h"
30
31 /*
32  * NOTE: The SNAPSHOT_SET_SWAP_FILE and SNAPSHOT_PMOPS ioctls are obsolete and
33  * will be removed in the future.  They are only preserved here for
34  * compatibility with existing userland utilities.
35  */
36 #define SNAPSHOT_SET_SWAP_FILE  _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int)
37 #define SNAPSHOT_PMOPS          _IOW(SNAPSHOT_IOC_MAGIC, 12, unsigned int)
38
39 #define PMOPS_PREPARE   1
40 #define PMOPS_ENTER     2
41 #define PMOPS_FINISH    3
42
43
44 #define SNAPSHOT_MINOR  231
45
46 static struct snapshot_data {
47         struct snapshot_handle handle;
48         int swap;
49         int mode;
50         char frozen;
51         char ready;
52         char platform_support;
53 } snapshot_state;
54
55 atomic_t snapshot_device_available = ATOMIC_INIT(1);
56
57 static int snapshot_open(struct inode *inode, struct file *filp)
58 {
59         struct snapshot_data *data;
60
61         if (!atomic_add_unless(&snapshot_device_available, -1, 0))
62                 return -EBUSY;
63
64         if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
65                 atomic_inc(&snapshot_device_available);
66                 return -ENOSYS;
67         }
68         if(create_basic_memory_bitmaps()) {
69                 atomic_inc(&snapshot_device_available);
70                 return -ENOMEM;
71         }
72         nonseekable_open(inode, filp);
73         data = &snapshot_state;
74         filp->private_data = data;
75         memset(&data->handle, 0, sizeof(struct snapshot_handle));
76         if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
77                 data->swap = swsusp_resume_device ?
78                         swap_type_of(swsusp_resume_device, 0, NULL) : -1;
79                 data->mode = O_RDONLY;
80         } else {
81                 data->swap = -1;
82                 data->mode = O_WRONLY;
83         }
84         data->frozen = 0;
85         data->ready = 0;
86         data->platform_support = 0;
87
88         return 0;
89 }
90
91 static int snapshot_release(struct inode *inode, struct file *filp)
92 {
93         struct snapshot_data *data;
94
95         swsusp_free();
96         free_basic_memory_bitmaps();
97         data = filp->private_data;
98         free_all_swap_pages(data->swap);
99         if (data->frozen) {
100                 mutex_lock(&pm_mutex);
101                 thaw_processes();
102                 mutex_unlock(&pm_mutex);
103         }
104         atomic_inc(&snapshot_device_available);
105         return 0;
106 }
107
108 static ssize_t snapshot_read(struct file *filp, char __user *buf,
109                              size_t count, loff_t *offp)
110 {
111         struct snapshot_data *data;
112         ssize_t res;
113
114         data = filp->private_data;
115         if (!data->ready)
116                 return -ENODATA;
117         res = snapshot_read_next(&data->handle, count);
118         if (res > 0) {
119                 if (copy_to_user(buf, data_of(data->handle), res))
120                         res = -EFAULT;
121                 else
122                         *offp = data->handle.offset;
123         }
124         return res;
125 }
126
127 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
128                               size_t count, loff_t *offp)
129 {
130         struct snapshot_data *data;
131         ssize_t res;
132
133         data = filp->private_data;
134         res = snapshot_write_next(&data->handle, count);
135         if (res > 0) {
136                 if (copy_from_user(data_of(data->handle), buf, res))
137                         res = -EFAULT;
138                 else
139                         *offp = data->handle.offset;
140         }
141         return res;
142 }
143
144 static int snapshot_ioctl(struct inode *inode, struct file *filp,
145                           unsigned int cmd, unsigned long arg)
146 {
147         int error = 0;
148         struct snapshot_data *data;
149         loff_t size;
150         sector_t offset;
151
152         if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
153                 return -ENOTTY;
154         if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
155                 return -ENOTTY;
156         if (!capable(CAP_SYS_ADMIN))
157                 return -EPERM;
158
159         data = filp->private_data;
160
161         switch (cmd) {
162
163         case SNAPSHOT_FREEZE:
164                 if (data->frozen)
165                         break;
166                 mutex_lock(&pm_mutex);
167                 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
168                 if (!error) {
169                         printk("Syncing filesystems ... ");
170                         sys_sync();
171                         printk("done.\n");
172
173                         error = freeze_processes();
174                         if (error)
175                                 thaw_processes();
176                 }
177                 if (error)
178                         pm_notifier_call_chain(PM_POST_HIBERNATION);
179                 mutex_unlock(&pm_mutex);
180                 if (!error)
181                         data->frozen = 1;
182                 break;
183
184         case SNAPSHOT_UNFREEZE:
185                 if (!data->frozen || data->ready)
186                         break;
187                 mutex_lock(&pm_mutex);
188                 thaw_processes();
189                 pm_notifier_call_chain(PM_POST_HIBERNATION);
190                 mutex_unlock(&pm_mutex);
191                 data->frozen = 0;
192                 break;
193
194         case SNAPSHOT_ATOMIC_SNAPSHOT:
195                 if (data->mode != O_RDONLY || !data->frozen  || data->ready) {
196                         error = -EPERM;
197                         break;
198                 }
199                 error = hibernation_snapshot(data->platform_support);
200                 if (!error)
201                         error = put_user(in_suspend, (unsigned int __user *)arg);
202                 if (!error)
203                         data->ready = 1;
204                 break;
205
206         case SNAPSHOT_ATOMIC_RESTORE:
207                 snapshot_write_finalize(&data->handle);
208                 if (data->mode != O_WRONLY || !data->frozen ||
209                     !snapshot_image_loaded(&data->handle)) {
210                         error = -EPERM;
211                         break;
212                 }
213                 error = hibernation_restore(data->platform_support);
214                 break;
215
216         case SNAPSHOT_FREE:
217                 swsusp_free();
218                 memset(&data->handle, 0, sizeof(struct snapshot_handle));
219                 data->ready = 0;
220                 break;
221
222         case SNAPSHOT_SET_IMAGE_SIZE:
223                 image_size = arg;
224                 break;
225
226         case SNAPSHOT_GET_IMAGE_SIZE:
227                 if (!data->ready) {
228                         error = -ENODATA;
229                         break;
230                 }
231                 size = snapshot_get_image_size();
232                 size <<= PAGE_SHIFT;
233                 error = put_user(size, (loff_t __user *)arg);
234                 break;
235
236         case SNAPSHOT_AVAIL_SWAP:
237                 size = count_swap_pages(data->swap, 1);
238                 size <<= PAGE_SHIFT;
239                 error = put_user(size, (loff_t __user *)arg);
240                 break;
241
242         case SNAPSHOT_GET_SWAP_PAGE:
243                 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
244                         error = -ENODEV;
245                         break;
246                 }
247                 offset = alloc_swapdev_block(data->swap);
248                 if (offset) {
249                         offset <<= PAGE_SHIFT;
250                         error = put_user(offset, (sector_t __user *)arg);
251                 } else {
252                         error = -ENOSPC;
253                 }
254                 break;
255
256         case SNAPSHOT_FREE_SWAP_PAGES:
257                 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
258                         error = -ENODEV;
259                         break;
260                 }
261                 free_all_swap_pages(data->swap);
262                 break;
263
264         case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
265                 if (!swsusp_swap_in_use()) {
266                         /*
267                          * User space encodes device types as two-byte values,
268                          * so we need to recode them
269                          */
270                         if (old_decode_dev(arg)) {
271                                 data->swap = swap_type_of(old_decode_dev(arg),
272                                                         0, NULL);
273                                 if (data->swap < 0)
274                                         error = -ENODEV;
275                         } else {
276                                 data->swap = -1;
277                                 error = -EINVAL;
278                         }
279                 } else {
280                         error = -EPERM;
281                 }
282                 break;
283
284         case SNAPSHOT_S2RAM:
285                 if (!data->frozen) {
286                         error = -EPERM;
287                         break;
288                 }
289                 if (!mutex_trylock(&pm_mutex)) {
290                         error = -EBUSY;
291                         break;
292                 }
293                 /*
294                  * Tasks are frozen and the notifiers have been called with
295                  * PM_HIBERNATION_PREPARE
296                  */
297                 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
298                 mutex_unlock(&pm_mutex);
299                 break;
300
301         case SNAPSHOT_PLATFORM_SUPPORT:
302                 data->platform_support = !!arg;
303                 break;
304
305         case SNAPSHOT_POWER_OFF:
306                 if (data->platform_support)
307                         error = hibernation_platform_enter();
308                 break;
309
310         case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
311                 error = -EINVAL;
312
313                 switch (arg) {
314
315                 case PMOPS_PREPARE:
316                         data->platform_support = 1;
317                         error = 0;
318                         break;
319
320                 case PMOPS_ENTER:
321                         if (data->platform_support)
322                                 error = hibernation_platform_enter();
323                         break;
324
325                 case PMOPS_FINISH:
326                         if (data->platform_support)
327                                 error = 0;
328                         break;
329
330                 default:
331                         printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
332
333                 }
334                 break;
335
336         case SNAPSHOT_SET_SWAP_AREA:
337                 if (swsusp_swap_in_use()) {
338                         error = -EPERM;
339                 } else {
340                         struct resume_swap_area swap_area;
341                         dev_t swdev;
342
343                         error = copy_from_user(&swap_area, (void __user *)arg,
344                                         sizeof(struct resume_swap_area));
345                         if (error) {
346                                 error = -EFAULT;
347                                 break;
348                         }
349
350                         /*
351                          * User space encodes device types as two-byte values,
352                          * so we need to recode them
353                          */
354                         swdev = old_decode_dev(swap_area.dev);
355                         if (swdev) {
356                                 offset = swap_area.offset;
357                                 data->swap = swap_type_of(swdev, offset, NULL);
358                                 if (data->swap < 0)
359                                         error = -ENODEV;
360                         } else {
361                                 data->swap = -1;
362                                 error = -EINVAL;
363                         }
364                 }
365                 break;
366
367         default:
368                 error = -ENOTTY;
369
370         }
371
372         return error;
373 }
374
375 static const struct file_operations snapshot_fops = {
376         .open = snapshot_open,
377         .release = snapshot_release,
378         .read = snapshot_read,
379         .write = snapshot_write,
380         .llseek = no_llseek,
381         .ioctl = snapshot_ioctl,
382 };
383
384 static struct miscdevice snapshot_device = {
385         .minor = SNAPSHOT_MINOR,
386         .name = "snapshot",
387         .fops = &snapshot_fops,
388 };
389
390 static int __init snapshot_device_init(void)
391 {
392         return misc_register(&snapshot_device);
393 };
394
395 device_initcall(snapshot_device_init);