Merge tag 'v3.10.85' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.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/export.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/compat.h>
25 #include <linux/console.h>
26 #include <linux/cpu.h>
27 #include <linux/freezer.h>
28
29 #include <asm/uaccess.h>
30
31 #include "power.h"
32
33
34 #define SNAPSHOT_MINOR 231
35
36 static struct snapshot_data {
37 struct snapshot_handle handle;
38 int swap;
39 int mode;
40 char frozen;
41 char ready;
42 char platform_support;
43 } snapshot_state;
44
45 atomic_t snapshot_device_available = ATOMIC_INIT(1);
46 EXPORT_SYMBOL_GPL(snapshot_device_available);
47
48 static int snapshot_open(struct inode *inode, struct file *filp)
49 {
50 struct snapshot_data *data;
51 int error;
52
53 lock_system_sleep();
54
55 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
56 error = -EBUSY;
57 goto Unlock;
58 }
59
60 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
61 atomic_inc(&snapshot_device_available);
62 error = -ENOSYS;
63 goto Unlock;
64 }
65 if(create_basic_memory_bitmaps()) {
66 atomic_inc(&snapshot_device_available);
67 error = -ENOMEM;
68 goto Unlock;
69 }
70 nonseekable_open(inode, filp);
71 data = &snapshot_state;
72 filp->private_data = data;
73 memset(&data->handle, 0, sizeof(struct snapshot_handle));
74 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
75 /* Hibernating. The image device should be accessible. */
76 data->swap = swsusp_resume_device ?
77 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
78 data->mode = O_RDONLY;
79 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
80 if (error)
81 pm_notifier_call_chain(PM_POST_HIBERNATION);
82 } else {
83 /*
84 * Resuming. We may need to wait for the image device to
85 * appear.
86 */
87 wait_for_device_probe();
88
89 data->swap = -1;
90 data->mode = O_WRONLY;
91 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
92 if (error)
93 pm_notifier_call_chain(PM_POST_RESTORE);
94 }
95 if (error) {
96 free_basic_memory_bitmaps();
97 atomic_inc(&snapshot_device_available);
98 }
99 data->frozen = 0;
100 data->ready = 0;
101 data->platform_support = 0;
102
103 Unlock:
104 unlock_system_sleep();
105
106 return error;
107 }
108
109 static int snapshot_release(struct inode *inode, struct file *filp)
110 {
111 struct snapshot_data *data;
112
113 lock_system_sleep();
114
115 swsusp_free();
116 free_basic_memory_bitmaps();
117 data = filp->private_data;
118 free_all_swap_pages(data->swap);
119 if (data->frozen) {
120 pm_restore_gfp_mask();
121 thaw_processes();
122 }
123 pm_notifier_call_chain(data->mode == O_RDONLY ?
124 PM_POST_HIBERNATION : PM_POST_RESTORE);
125 atomic_inc(&snapshot_device_available);
126
127 unlock_system_sleep();
128
129 return 0;
130 }
131
132 static ssize_t snapshot_read(struct file *filp, char __user *buf,
133 size_t count, loff_t *offp)
134 {
135 struct snapshot_data *data;
136 ssize_t res;
137 loff_t pg_offp = *offp & ~PAGE_MASK;
138
139 lock_system_sleep();
140
141 data = filp->private_data;
142 if (!data->ready) {
143 res = -ENODATA;
144 goto Unlock;
145 }
146 if (!pg_offp) { /* on page boundary? */
147 res = snapshot_read_next(&data->handle);
148 if (res <= 0)
149 goto Unlock;
150 } else {
151 res = PAGE_SIZE - pg_offp;
152 }
153
154 res = simple_read_from_buffer(buf, count, &pg_offp,
155 data_of(data->handle), res);
156 if (res > 0)
157 *offp += res;
158
159 Unlock:
160 unlock_system_sleep();
161
162 return res;
163 }
164
165 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
166 size_t count, loff_t *offp)
167 {
168 struct snapshot_data *data;
169 ssize_t res;
170 loff_t pg_offp = *offp & ~PAGE_MASK;
171
172 lock_system_sleep();
173
174 data = filp->private_data;
175
176 if (!pg_offp) {
177 res = snapshot_write_next(&data->handle);
178 if (res <= 0)
179 goto unlock;
180 } else {
181 res = PAGE_SIZE - pg_offp;
182 }
183
184 res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
185 buf, count);
186 if (res > 0)
187 *offp += res;
188 unlock:
189 unlock_system_sleep();
190
191 return res;
192 }
193
194 static long snapshot_ioctl(struct file *filp, unsigned int cmd,
195 unsigned long arg)
196 {
197 int error = 0;
198 struct snapshot_data *data;
199 loff_t size;
200 sector_t offset;
201
202 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
203 return -ENOTTY;
204 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
205 return -ENOTTY;
206 if (!capable(CAP_SYS_ADMIN))
207 return -EPERM;
208
209 if (!mutex_trylock(&pm_mutex))
210 return -EBUSY;
211
212 data = filp->private_data;
213
214 switch (cmd) {
215
216 case SNAPSHOT_FREEZE:
217 if (data->frozen)
218 break;
219
220 printk("Syncing filesystems ... ");
221 sys_sync();
222 printk("done.\n");
223
224 error = freeze_processes();
225 if (!error)
226 data->frozen = 1;
227 break;
228
229 case SNAPSHOT_UNFREEZE:
230 if (!data->frozen || data->ready)
231 break;
232 pm_restore_gfp_mask();
233 thaw_processes();
234 data->frozen = 0;
235 break;
236
237 case SNAPSHOT_CREATE_IMAGE:
238 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
239 error = -EPERM;
240 break;
241 }
242 pm_restore_gfp_mask();
243 error = hibernation_snapshot(data->platform_support);
244 if (!error) {
245 error = put_user(in_suspend, (int __user *)arg);
246 data->ready = !freezer_test_done && !error;
247 freezer_test_done = false;
248 }
249 break;
250
251 case SNAPSHOT_ATOMIC_RESTORE:
252 snapshot_write_finalize(&data->handle);
253 if (data->mode != O_WRONLY || !data->frozen ||
254 !snapshot_image_loaded(&data->handle)) {
255 error = -EPERM;
256 break;
257 }
258 error = hibernation_restore(data->platform_support);
259 break;
260
261 case SNAPSHOT_FREE:
262 swsusp_free();
263 memset(&data->handle, 0, sizeof(struct snapshot_handle));
264 data->ready = 0;
265 /*
266 * It is necessary to thaw kernel threads here, because
267 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
268 * SNAPSHOT_FREE. In that case, if kernel threads were not
269 * thawed, the preallocation of memory carried out by
270 * hibernation_snapshot() might run into problems (i.e. it
271 * might fail or even deadlock).
272 */
273 thaw_kernel_threads();
274 break;
275
276 case SNAPSHOT_PREF_IMAGE_SIZE:
277 image_size = arg;
278 break;
279
280 case SNAPSHOT_GET_IMAGE_SIZE:
281 if (!data->ready) {
282 error = -ENODATA;
283 break;
284 }
285 size = snapshot_get_image_size();
286 size <<= PAGE_SHIFT;
287 error = put_user(size, (loff_t __user *)arg);
288 break;
289
290 case SNAPSHOT_AVAIL_SWAP_SIZE:
291 size = count_swap_pages(data->swap, 1);
292 size <<= PAGE_SHIFT;
293 error = put_user(size, (loff_t __user *)arg);
294 break;
295
296 case SNAPSHOT_ALLOC_SWAP_PAGE:
297 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
298 error = -ENODEV;
299 break;
300 }
301 offset = alloc_swapdev_block(data->swap);
302 if (offset) {
303 offset <<= PAGE_SHIFT;
304 error = put_user(offset, (loff_t __user *)arg);
305 } else {
306 error = -ENOSPC;
307 }
308 break;
309
310 case SNAPSHOT_FREE_SWAP_PAGES:
311 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
312 error = -ENODEV;
313 break;
314 }
315 free_all_swap_pages(data->swap);
316 break;
317
318 case SNAPSHOT_S2RAM:
319 if (!data->frozen) {
320 error = -EPERM;
321 break;
322 }
323 /*
324 * Tasks are frozen and the notifiers have been called with
325 * PM_HIBERNATION_PREPARE
326 */
327 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
328 data->ready = 0;
329 break;
330
331 case SNAPSHOT_PLATFORM_SUPPORT:
332 data->platform_support = !!arg;
333 break;
334
335 case SNAPSHOT_POWER_OFF:
336 if (data->platform_support)
337 error = hibernation_platform_enter();
338 break;
339
340 case SNAPSHOT_SET_SWAP_AREA:
341 if (swsusp_swap_in_use()) {
342 error = -EPERM;
343 } else {
344 struct resume_swap_area swap_area;
345 dev_t swdev;
346
347 error = copy_from_user(&swap_area, (void __user *)arg,
348 sizeof(struct resume_swap_area));
349 if (error) {
350 error = -EFAULT;
351 break;
352 }
353
354 /*
355 * User space encodes device types as two-byte values,
356 * so we need to recode them
357 */
358 swdev = new_decode_dev(swap_area.dev);
359 if (swdev) {
360 offset = swap_area.offset;
361 data->swap = swap_type_of(swdev, offset, NULL);
362 if (data->swap < 0)
363 error = -ENODEV;
364 } else {
365 data->swap = -1;
366 error = -EINVAL;
367 }
368 }
369 break;
370
371 default:
372 error = -ENOTTY;
373
374 }
375
376 mutex_unlock(&pm_mutex);
377
378 return error;
379 }
380
381 #ifdef CONFIG_COMPAT
382
383 struct compat_resume_swap_area {
384 compat_loff_t offset;
385 u32 dev;
386 } __packed;
387
388 static long
389 snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
390 {
391 BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t));
392
393 switch (cmd) {
394 case SNAPSHOT_GET_IMAGE_SIZE:
395 case SNAPSHOT_AVAIL_SWAP_SIZE:
396 case SNAPSHOT_ALLOC_SWAP_PAGE: {
397 compat_loff_t __user *uoffset = compat_ptr(arg);
398 loff_t offset;
399 mm_segment_t old_fs;
400 int err;
401
402 old_fs = get_fs();
403 set_fs(KERNEL_DS);
404 err = snapshot_ioctl(file, cmd, (unsigned long) &offset);
405 set_fs(old_fs);
406 if (!err && put_user(offset, uoffset))
407 err = -EFAULT;
408 return err;
409 }
410
411 case SNAPSHOT_CREATE_IMAGE:
412 return snapshot_ioctl(file, cmd,
413 (unsigned long) compat_ptr(arg));
414
415 case SNAPSHOT_SET_SWAP_AREA: {
416 struct compat_resume_swap_area __user *u_swap_area =
417 compat_ptr(arg);
418 struct resume_swap_area swap_area;
419 mm_segment_t old_fs;
420 int err;
421
422 err = get_user(swap_area.offset, &u_swap_area->offset);
423 err |= get_user(swap_area.dev, &u_swap_area->dev);
424 if (err)
425 return -EFAULT;
426 old_fs = get_fs();
427 set_fs(KERNEL_DS);
428 err = snapshot_ioctl(file, SNAPSHOT_SET_SWAP_AREA,
429 (unsigned long) &swap_area);
430 set_fs(old_fs);
431 return err;
432 }
433
434 default:
435 return snapshot_ioctl(file, cmd, arg);
436 }
437 }
438
439 #endif /* CONFIG_COMPAT */
440
441 static const struct file_operations snapshot_fops = {
442 .open = snapshot_open,
443 .release = snapshot_release,
444 .read = snapshot_read,
445 .write = snapshot_write,
446 .llseek = no_llseek,
447 .unlocked_ioctl = snapshot_ioctl,
448 #ifdef CONFIG_COMPAT
449 .compat_ioctl = snapshot_compat_ioctl,
450 #endif
451 };
452
453 static struct miscdevice snapshot_device = {
454 .minor = SNAPSHOT_MINOR,
455 .name = "snapshot",
456 .fops = &snapshot_fops,
457 };
458
459 static int __init snapshot_device_init(void)
460 {
461 return misc_register(&snapshot_device);
462 };
463
464 device_initcall(snapshot_device_init);