OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / kernel / power / user.c
blob89dc7e4525440a3e3eef2481731aeb210fda97a4
1 /*
2 * linux/kernel/power/user.c
4 * This file provides the user space interface for software suspend/resume.
6 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
8 * This file is released under the GPLv2.
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/compat.h>
24 #include <linux/console.h>
25 #include <linux/cpu.h>
26 #include <linux/freezer.h>
27 #include <scsi/scsi_scan.h>
29 #include <asm/uaccess.h>
31 #include "power.h"
34 #define SNAPSHOT_MINOR 231
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;
45 atomic_t snapshot_device_available = ATOMIC_INIT(1);
47 static int snapshot_open(struct inode *inode, struct file *filp)
49 struct snapshot_data *data;
50 int error;
52 lock_system_sleep();
54 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
55 error = -EBUSY;
56 goto Unlock;
59 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
60 atomic_inc(&snapshot_device_available);
61 error = -ENOSYS;
62 goto Unlock;
64 if(create_basic_memory_bitmaps()) {
65 atomic_inc(&snapshot_device_available);
66 error = -ENOMEM;
67 goto Unlock;
69 nonseekable_open(inode, filp);
70 data = &snapshot_state;
71 filp->private_data = data;
72 memset(&data->handle, 0, sizeof(struct snapshot_handle));
73 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
74 /* Hibernating. The image device should be accessible. */
75 data->swap = swsusp_resume_device ?
76 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
77 data->mode = O_RDONLY;
78 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
79 if (error)
80 pm_notifier_call_chain(PM_POST_HIBERNATION);
81 } else {
83 * Resuming. We may need to wait for the image device to
84 * appear.
86 wait_for_device_probe();
87 scsi_complete_async_scans();
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);
95 if (error) {
96 free_basic_memory_bitmaps();
97 atomic_inc(&snapshot_device_available);
99 data->frozen = 0;
100 data->ready = 0;
101 data->platform_support = 0;
103 Unlock:
104 unlock_system_sleep();
106 return error;
109 static int snapshot_release(struct inode *inode, struct file *filp)
111 struct snapshot_data *data;
113 lock_system_sleep();
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();
123 pm_notifier_call_chain(data->mode == O_RDONLY ?
124 PM_POST_HIBERNATION : PM_POST_RESTORE);
125 atomic_inc(&snapshot_device_available);
127 unlock_system_sleep();
129 return 0;
132 static ssize_t snapshot_read(struct file *filp, char __user *buf,
133 size_t count, loff_t *offp)
135 struct snapshot_data *data;
136 ssize_t res;
137 loff_t pg_offp = *offp & ~PAGE_MASK;
139 lock_system_sleep();
141 data = filp->private_data;
142 if (!data->ready) {
143 res = -ENODATA;
144 goto Unlock;
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;
154 res = simple_read_from_buffer(buf, count, &pg_offp,
155 data_of(data->handle), res);
156 if (res > 0)
157 *offp += res;
159 Unlock:
160 unlock_system_sleep();
162 return res;
165 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
166 size_t count, loff_t *offp)
168 struct snapshot_data *data;
169 ssize_t res;
170 loff_t pg_offp = *offp & ~PAGE_MASK;
172 lock_system_sleep();
174 data = filp->private_data;
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;
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();
191 return res;
194 static long snapshot_ioctl(struct file *filp, unsigned int cmd,
195 unsigned long arg)
197 int error = 0;
198 struct snapshot_data *data;
199 loff_t size;
200 sector_t offset;
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;
209 if (!mutex_trylock(&pm_mutex))
210 return -EBUSY;
212 data = filp->private_data;
214 switch (cmd) {
216 case SNAPSHOT_FREEZE:
217 if (data->frozen)
218 break;
220 printk("Syncing filesystems ... ");
221 sys_sync();
222 printk("done.\n");
224 error = freeze_processes();
225 if (!error)
226 data->frozen = 1;
227 break;
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;
237 case SNAPSHOT_CREATE_IMAGE:
238 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
239 error = -EPERM;
240 break;
242 pm_restore_gfp_mask();
243 error = hibernation_snapshot(data->platform_support);
244 if (error) {
245 thaw_kernel_threads();
246 } else {
247 error = put_user(in_suspend, (int __user *)arg);
248 if (!error && !freezer_test_done)
249 data->ready = 1;
250 if (freezer_test_done) {
251 freezer_test_done = false;
252 thaw_kernel_threads();
255 break;
257 case SNAPSHOT_ATOMIC_RESTORE:
258 snapshot_write_finalize(&data->handle);
259 if (data->mode != O_WRONLY || !data->frozen ||
260 !snapshot_image_loaded(&data->handle)) {
261 error = -EPERM;
262 break;
264 error = hibernation_restore(data->platform_support);
265 break;
267 case SNAPSHOT_FREE:
268 swsusp_free();
269 memset(&data->handle, 0, sizeof(struct snapshot_handle));
270 data->ready = 0;
272 * It is necessary to thaw kernel threads here, because
273 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
274 * SNAPSHOT_FREE. In that case, if kernel threads were not
275 * thawed, the preallocation of memory carried out by
276 * hibernation_snapshot() might run into problems (i.e. it
277 * might fail or even deadlock).
279 thaw_kernel_threads();
280 break;
282 case SNAPSHOT_PREF_IMAGE_SIZE:
283 image_size = arg;
284 break;
286 case SNAPSHOT_GET_IMAGE_SIZE:
287 if (!data->ready) {
288 error = -ENODATA;
289 break;
291 size = snapshot_get_image_size();
292 size <<= PAGE_SHIFT;
293 error = put_user(size, (loff_t __user *)arg);
294 break;
296 case SNAPSHOT_AVAIL_SWAP_SIZE:
297 size = count_swap_pages(data->swap, 1);
298 size <<= PAGE_SHIFT;
299 error = put_user(size, (loff_t __user *)arg);
300 break;
302 case SNAPSHOT_ALLOC_SWAP_PAGE:
303 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
304 error = -ENODEV;
305 break;
307 offset = alloc_swapdev_block(data->swap);
308 if (offset) {
309 offset <<= PAGE_SHIFT;
310 error = put_user(offset, (loff_t __user *)arg);
311 } else {
312 error = -ENOSPC;
314 break;
316 case SNAPSHOT_FREE_SWAP_PAGES:
317 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
318 error = -ENODEV;
319 break;
321 free_all_swap_pages(data->swap);
322 break;
324 case SNAPSHOT_S2RAM:
325 if (!data->frozen) {
326 error = -EPERM;
327 break;
330 * Tasks are frozen and the notifiers have been called with
331 * PM_HIBERNATION_PREPARE
333 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
334 data->ready = 0;
335 break;
337 case SNAPSHOT_PLATFORM_SUPPORT:
338 data->platform_support = !!arg;
339 break;
341 case SNAPSHOT_POWER_OFF:
342 if (data->platform_support)
343 error = hibernation_platform_enter();
344 break;
346 case SNAPSHOT_SET_SWAP_AREA:
347 if (swsusp_swap_in_use()) {
348 error = -EPERM;
349 } else {
350 struct resume_swap_area swap_area;
351 dev_t swdev;
353 error = copy_from_user(&swap_area, (void __user *)arg,
354 sizeof(struct resume_swap_area));
355 if (error) {
356 error = -EFAULT;
357 break;
361 * User space encodes device types as two-byte values,
362 * so we need to recode them
364 swdev = new_decode_dev(swap_area.dev);
365 if (swdev) {
366 offset = swap_area.offset;
367 data->swap = swap_type_of(swdev, offset, NULL);
368 if (data->swap < 0)
369 error = -ENODEV;
370 } else {
371 data->swap = -1;
372 error = -EINVAL;
375 break;
377 default:
378 error = -ENOTTY;
382 mutex_unlock(&pm_mutex);
384 return error;
387 #ifdef CONFIG_COMPAT
389 struct compat_resume_swap_area {
390 compat_loff_t offset;
391 u32 dev;
392 } __packed;
394 static long
395 snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
397 BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t));
399 switch (cmd) {
400 case SNAPSHOT_GET_IMAGE_SIZE:
401 case SNAPSHOT_AVAIL_SWAP_SIZE:
402 case SNAPSHOT_ALLOC_SWAP_PAGE: {
403 compat_loff_t __user *uoffset = compat_ptr(arg);
404 loff_t offset;
405 mm_segment_t old_fs;
406 int err;
408 old_fs = get_fs();
409 set_fs(KERNEL_DS);
410 err = snapshot_ioctl(file, cmd, (unsigned long) &offset);
411 set_fs(old_fs);
412 if (!err && put_user(offset, uoffset))
413 err = -EFAULT;
414 return err;
417 case SNAPSHOT_CREATE_IMAGE:
418 return snapshot_ioctl(file, cmd,
419 (unsigned long) compat_ptr(arg));
421 case SNAPSHOT_SET_SWAP_AREA: {
422 struct compat_resume_swap_area __user *u_swap_area =
423 compat_ptr(arg);
424 struct resume_swap_area swap_area;
425 mm_segment_t old_fs;
426 int err;
428 err = get_user(swap_area.offset, &u_swap_area->offset);
429 err |= get_user(swap_area.dev, &u_swap_area->dev);
430 if (err)
431 return -EFAULT;
432 old_fs = get_fs();
433 set_fs(KERNEL_DS);
434 err = snapshot_ioctl(file, SNAPSHOT_SET_SWAP_AREA,
435 (unsigned long) &swap_area);
436 set_fs(old_fs);
437 return err;
440 default:
441 return snapshot_ioctl(file, cmd, arg);
445 #endif /* CONFIG_COMPAT */
447 static const struct file_operations snapshot_fops = {
448 .open = snapshot_open,
449 .release = snapshot_release,
450 .read = snapshot_read,
451 .write = snapshot_write,
452 .llseek = no_llseek,
453 .unlocked_ioctl = snapshot_ioctl,
454 #ifdef CONFIG_COMPAT
455 .compat_ioctl = snapshot_compat_ioctl,
456 #endif
459 static struct miscdevice snapshot_device = {
460 .minor = SNAPSHOT_MINOR,
461 .name = "snapshot",
462 .fops = &snapshot_fops,
465 static int __init snapshot_device_init(void)
467 return misc_register(&snapshot_device);
470 device_initcall(snapshot_device_init);