Rebecca Schultz | 4b0ea27 | 2008-07-17 18:14:55 -0700 | [diff] [blame^] | 1 | /* kernel/power/fbearlysuspend.c |
| 2 | * |
| 3 | * Copyright (C) 2005-2008 Google, Inc. |
| 4 | * |
| 5 | * This software is licensed under the terms of the GNU General Public |
| 6 | * License version 2, as published by the Free Software Foundation, and |
| 7 | * may be copied, distributed, and modified under those terms. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/earlysuspend.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/wait.h> |
| 19 | |
| 20 | #include "power.h" |
| 21 | |
| 22 | static wait_queue_head_t fb_state_wq; |
| 23 | static DEFINE_SPINLOCK(fb_state_lock); |
| 24 | static enum { |
| 25 | FB_STATE_STOPPED_DRAWING, |
| 26 | FB_STATE_REQUEST_STOP_DRAWING, |
| 27 | FB_STATE_DRAWING_OK, |
| 28 | } fb_state; |
| 29 | |
| 30 | /* tell userspace to stop drawing, wait for it to stop */ |
| 31 | static void stop_drawing_early_suspend(struct early_suspend *h) |
| 32 | { |
| 33 | int ret; |
| 34 | unsigned long irq_flags; |
| 35 | |
| 36 | spin_lock_irqsave(&fb_state_lock, irq_flags); |
| 37 | fb_state = FB_STATE_REQUEST_STOP_DRAWING; |
| 38 | spin_unlock_irqrestore(&fb_state_lock, irq_flags); |
| 39 | |
| 40 | wake_up_all(&fb_state_wq); |
| 41 | ret = wait_event_timeout(fb_state_wq, |
| 42 | fb_state == FB_STATE_STOPPED_DRAWING, |
| 43 | HZ); |
| 44 | if (unlikely(fb_state != FB_STATE_STOPPED_DRAWING)) |
| 45 | pr_warning("stop_drawing_early_suspend: timeout waiting for " |
| 46 | "userspace to stop drawing\n"); |
| 47 | } |
| 48 | |
| 49 | /* tell userspace to start drawing */ |
| 50 | static void start_drawing_late_resume(struct early_suspend *h) |
| 51 | { |
| 52 | unsigned long irq_flags; |
| 53 | |
| 54 | spin_lock_irqsave(&fb_state_lock, irq_flags); |
| 55 | fb_state = FB_STATE_DRAWING_OK; |
| 56 | spin_unlock_irqrestore(&fb_state_lock, irq_flags); |
| 57 | wake_up(&fb_state_wq); |
| 58 | } |
| 59 | |
| 60 | static struct early_suspend stop_drawing_early_suspend_desc = { |
| 61 | .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING, |
| 62 | .suspend = stop_drawing_early_suspend, |
| 63 | .resume = start_drawing_late_resume, |
| 64 | }; |
| 65 | |
| 66 | static ssize_t wait_for_fb_sleep_show(struct kobject *kobj, |
| 67 | struct kobj_attribute *attr, char *buf) |
| 68 | { |
| 69 | char *s = buf; |
| 70 | int ret; |
| 71 | |
| 72 | ret = wait_event_interruptible(fb_state_wq, |
| 73 | fb_state != FB_STATE_DRAWING_OK); |
| 74 | if (ret && fb_state == FB_STATE_DRAWING_OK) |
| 75 | return ret; |
| 76 | else |
| 77 | s += sprintf(buf, "sleeping"); |
| 78 | return s - buf; |
| 79 | } |
| 80 | |
| 81 | static ssize_t wait_for_fb_wake_show(struct kobject *kobj, |
| 82 | struct kobj_attribute *attr, char *buf) |
| 83 | { |
| 84 | char *s = buf; |
| 85 | int ret; |
| 86 | unsigned long irq_flags; |
| 87 | |
| 88 | spin_lock_irqsave(&fb_state_lock, irq_flags); |
| 89 | if (fb_state == FB_STATE_REQUEST_STOP_DRAWING) { |
| 90 | fb_state = FB_STATE_STOPPED_DRAWING; |
| 91 | wake_up(&fb_state_wq); |
| 92 | } |
| 93 | spin_unlock_irqrestore(&fb_state_lock, irq_flags); |
| 94 | |
| 95 | ret = wait_event_interruptible(fb_state_wq, |
| 96 | fb_state == FB_STATE_DRAWING_OK); |
| 97 | if (ret && fb_state != FB_STATE_DRAWING_OK) |
| 98 | return ret; |
| 99 | else |
| 100 | s += sprintf(buf, "awake"); |
| 101 | |
| 102 | return s - buf; |
| 103 | } |
| 104 | |
| 105 | #define power_ro_attr(_name) \ |
| 106 | static struct kobj_attribute _name##_attr = { \ |
| 107 | .attr = { \ |
| 108 | .name = __stringify(_name), \ |
| 109 | .mode = 0444, \ |
| 110 | }, \ |
| 111 | .show = _name##_show, \ |
| 112 | .store = NULL, \ |
| 113 | } |
| 114 | |
| 115 | power_ro_attr(wait_for_fb_sleep); |
| 116 | power_ro_attr(wait_for_fb_wake); |
| 117 | |
| 118 | static struct attribute *g[] = { |
| 119 | &wait_for_fb_sleep_attr.attr, |
| 120 | &wait_for_fb_wake_attr.attr, |
| 121 | NULL, |
| 122 | }; |
| 123 | |
| 124 | static struct attribute_group attr_group = { |
| 125 | .attrs = g, |
| 126 | }; |
| 127 | |
| 128 | static int __init android_power_init(void) |
| 129 | { |
| 130 | int ret; |
| 131 | |
| 132 | init_waitqueue_head(&fb_state_wq); |
| 133 | fb_state = FB_STATE_DRAWING_OK; |
| 134 | |
| 135 | ret = sysfs_create_group(power_kobj, &attr_group); |
| 136 | if (ret) { |
| 137 | pr_err("android_power_init: sysfs_create_group failed\n"); |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | register_early_suspend(&stop_drawing_early_suspend_desc); |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static void __exit android_power_exit(void) |
| 146 | { |
| 147 | unregister_early_suspend(&stop_drawing_early_suspend_desc); |
| 148 | sysfs_remove_group(power_kobj, &attr_group); |
| 149 | } |
| 150 | |
| 151 | module_init(android_power_init); |
| 152 | module_exit(android_power_exit); |
| 153 | |