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