Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * bios-less APM driver for ARM Linux |
| 3 | * Jamey Hicks <jamey@crl.dec.com> |
| 4 | * adapted from the APM BIOS driver for Linux by Stephen Rothwell (sfr@linuxcare.com) |
| 5 | * |
| 6 | * APM 1.2 Reference: |
| 7 | * Intel Corporation, Microsoft Corporation. Advanced Power Management |
| 8 | * (APM) BIOS Interface Specification, Revision 1.2, February 1996. |
| 9 | * |
| 10 | * [This document is available from Microsoft at: |
| 11 | * http://www.microsoft.com/hwdev/busbios/amp_12.htm] |
| 12 | */ |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/poll.h> |
| 15 | #include <linux/slab.h> |
Arnd Bergmann | 2861ead | 2008-05-20 19:15:33 +0200 | [diff] [blame] | 16 | #include <linux/smp_lock.h> |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 17 | #include <linux/proc_fs.h> |
Alexey Dobriyan | 647634d | 2008-04-29 01:01:46 -0700 | [diff] [blame] | 18 | #include <linux/seq_file.h> |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 19 | #include <linux/miscdevice.h> |
| 20 | #include <linux/apm_bios.h> |
| 21 | #include <linux/capability.h> |
| 22 | #include <linux/sched.h> |
Rafael J. Wysocki | 95d9ffb | 2007-10-18 03:04:39 -0700 | [diff] [blame] | 23 | #include <linux/suspend.h> |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 24 | #include <linux/apm-emulation.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 25 | #include <linux/freezer.h> |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 26 | #include <linux/device.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/list.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/completion.h> |
| 31 | #include <linux/kthread.h> |
| 32 | #include <linux/delay.h> |
| 33 | |
| 34 | #include <asm/system.h> |
| 35 | |
| 36 | /* |
| 37 | * The apm_bios device is one of the misc char devices. |
| 38 | * This is its minor number. |
| 39 | */ |
| 40 | #define APM_MINOR_DEV 134 |
| 41 | |
| 42 | /* |
| 43 | * See Documentation/Config.help for the configuration options. |
| 44 | * |
| 45 | * Various options can be changed at boot time as follows: |
| 46 | * (We allow underscores for compatibility with the modules code) |
| 47 | * apm=on/off enable/disable APM |
| 48 | */ |
| 49 | |
| 50 | /* |
| 51 | * Maximum number of events stored |
| 52 | */ |
| 53 | #define APM_MAX_EVENTS 16 |
| 54 | |
| 55 | struct apm_queue { |
| 56 | unsigned int event_head; |
| 57 | unsigned int event_tail; |
| 58 | apm_event_t events[APM_MAX_EVENTS]; |
| 59 | }; |
| 60 | |
| 61 | /* |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 62 | * thread states (for threads using a writable /dev/apm_bios fd): |
| 63 | * |
| 64 | * SUSPEND_NONE: nothing happening |
| 65 | * SUSPEND_PENDING: suspend event queued for thread and pending to be read |
| 66 | * SUSPEND_READ: suspend event read, pending acknowledgement |
| 67 | * SUSPEND_ACKED: acknowledgement received from thread (via ioctl), |
| 68 | * waiting for resume |
| 69 | * SUSPEND_ACKTO: acknowledgement timeout |
| 70 | * SUSPEND_DONE: thread had acked suspend and is now notified of |
| 71 | * resume |
| 72 | * |
| 73 | * SUSPEND_WAIT: this thread invoked suspend and is waiting for resume |
| 74 | * |
| 75 | * A thread migrates in one of three paths: |
| 76 | * NONE -1-> PENDING -2-> READ -3-> ACKED -4-> DONE -5-> NONE |
| 77 | * -6-> ACKTO -7-> NONE |
| 78 | * NONE -8-> WAIT -9-> NONE |
| 79 | * |
| 80 | * While in PENDING or READ, the thread is accounted for in the |
| 81 | * suspend_acks_pending counter. |
| 82 | * |
| 83 | * The transitions are invoked as follows: |
| 84 | * 1: suspend event is signalled from the core PM code |
| 85 | * 2: the suspend event is read from the fd by the userspace thread |
| 86 | * 3: userspace thread issues the APM_IOC_SUSPEND ioctl (as ack) |
| 87 | * 4: core PM code signals that we have resumed |
| 88 | * 5: APM_IOC_SUSPEND ioctl returns |
| 89 | * |
| 90 | * 6: the notifier invoked from the core PM code timed out waiting |
| 91 | * for all relevant threds to enter ACKED state and puts those |
| 92 | * that haven't into ACKTO |
| 93 | * 7: those threads issue APM_IOC_SUSPEND ioctl too late, |
| 94 | * get an error |
| 95 | * |
| 96 | * 8: userspace thread issues the APM_IOC_SUSPEND ioctl (to suspend), |
| 97 | * ioctl code invokes pm_suspend() |
| 98 | * 9: pm_suspend() returns indicating resume |
| 99 | */ |
| 100 | enum apm_suspend_state { |
| 101 | SUSPEND_NONE, |
| 102 | SUSPEND_PENDING, |
| 103 | SUSPEND_READ, |
| 104 | SUSPEND_ACKED, |
| 105 | SUSPEND_ACKTO, |
| 106 | SUSPEND_WAIT, |
| 107 | SUSPEND_DONE, |
| 108 | }; |
| 109 | |
| 110 | /* |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 111 | * The per-file APM data |
| 112 | */ |
| 113 | struct apm_user { |
| 114 | struct list_head list; |
| 115 | |
| 116 | unsigned int suser: 1; |
| 117 | unsigned int writer: 1; |
| 118 | unsigned int reader: 1; |
| 119 | |
| 120 | int suspend_result; |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 121 | enum apm_suspend_state suspend_state; |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 122 | |
| 123 | struct apm_queue queue; |
| 124 | }; |
| 125 | |
| 126 | /* |
| 127 | * Local variables |
| 128 | */ |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 129 | static atomic_t suspend_acks_pending = ATOMIC_INIT(0); |
| 130 | static atomic_t userspace_notification_inhibit = ATOMIC_INIT(0); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 131 | static int apm_disabled; |
| 132 | static struct task_struct *kapmd_tsk; |
| 133 | |
| 134 | static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue); |
| 135 | static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue); |
| 136 | |
| 137 | /* |
| 138 | * This is a list of everyone who has opened /dev/apm_bios |
| 139 | */ |
| 140 | static DECLARE_RWSEM(user_list_lock); |
| 141 | static LIST_HEAD(apm_user_list); |
| 142 | |
| 143 | /* |
| 144 | * kapmd info. kapmd provides us a process context to handle |
| 145 | * "APM" events within - specifically necessary if we're going |
| 146 | * to be suspending the system. |
| 147 | */ |
| 148 | static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait); |
| 149 | static DEFINE_SPINLOCK(kapmd_queue_lock); |
| 150 | static struct apm_queue kapmd_queue; |
| 151 | |
| 152 | static DEFINE_MUTEX(state_lock); |
| 153 | |
| 154 | static const char driver_version[] = "1.13"; /* no spaces */ |
| 155 | |
| 156 | |
| 157 | |
| 158 | /* |
| 159 | * Compatibility cruft until the IPAQ people move over to the new |
| 160 | * interface. |
| 161 | */ |
| 162 | static void __apm_get_power_status(struct apm_power_info *info) |
| 163 | { |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * This allows machines to provide their own "apm get power status" function. |
| 168 | */ |
| 169 | void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status; |
| 170 | EXPORT_SYMBOL(apm_get_power_status); |
| 171 | |
| 172 | |
| 173 | /* |
| 174 | * APM event queue management. |
| 175 | */ |
| 176 | static inline int queue_empty(struct apm_queue *q) |
| 177 | { |
| 178 | return q->event_head == q->event_tail; |
| 179 | } |
| 180 | |
| 181 | static inline apm_event_t queue_get_event(struct apm_queue *q) |
| 182 | { |
| 183 | q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS; |
| 184 | return q->events[q->event_tail]; |
| 185 | } |
| 186 | |
| 187 | static void queue_add_event(struct apm_queue *q, apm_event_t event) |
| 188 | { |
| 189 | q->event_head = (q->event_head + 1) % APM_MAX_EVENTS; |
| 190 | if (q->event_head == q->event_tail) { |
| 191 | static int notified; |
| 192 | |
| 193 | if (notified++ == 0) |
| 194 | printk(KERN_ERR "apm: an event queue overflowed\n"); |
| 195 | q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS; |
| 196 | } |
| 197 | q->events[q->event_head] = event; |
| 198 | } |
| 199 | |
| 200 | static void queue_event(apm_event_t event) |
| 201 | { |
| 202 | struct apm_user *as; |
| 203 | |
| 204 | down_read(&user_list_lock); |
| 205 | list_for_each_entry(as, &apm_user_list, list) { |
| 206 | if (as->reader) |
| 207 | queue_add_event(&as->queue, event); |
| 208 | } |
| 209 | up_read(&user_list_lock); |
| 210 | wake_up_interruptible(&apm_waitqueue); |
| 211 | } |
| 212 | |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 213 | static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos) |
| 214 | { |
| 215 | struct apm_user *as = fp->private_data; |
| 216 | apm_event_t event; |
| 217 | int i = count, ret = 0; |
| 218 | |
| 219 | if (count < sizeof(apm_event_t)) |
| 220 | return -EINVAL; |
| 221 | |
| 222 | if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK) |
| 223 | return -EAGAIN; |
| 224 | |
| 225 | wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue)); |
| 226 | |
| 227 | while ((i >= sizeof(event)) && !queue_empty(&as->queue)) { |
| 228 | event = queue_get_event(&as->queue); |
| 229 | |
| 230 | ret = -EFAULT; |
| 231 | if (copy_to_user(buf, &event, sizeof(event))) |
| 232 | break; |
| 233 | |
| 234 | mutex_lock(&state_lock); |
| 235 | if (as->suspend_state == SUSPEND_PENDING && |
| 236 | (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND)) |
| 237 | as->suspend_state = SUSPEND_READ; |
| 238 | mutex_unlock(&state_lock); |
| 239 | |
| 240 | buf += sizeof(event); |
| 241 | i -= sizeof(event); |
| 242 | } |
| 243 | |
| 244 | if (i < count) |
| 245 | ret = count - i; |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | static unsigned int apm_poll(struct file *fp, poll_table * wait) |
| 251 | { |
| 252 | struct apm_user *as = fp->private_data; |
| 253 | |
| 254 | poll_wait(fp, &apm_waitqueue, wait); |
| 255 | return queue_empty(&as->queue) ? 0 : POLLIN | POLLRDNORM; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * apm_ioctl - handle APM ioctl |
| 260 | * |
| 261 | * APM_IOC_SUSPEND |
| 262 | * This IOCTL is overloaded, and performs two functions. It is used to: |
| 263 | * - initiate a suspend |
| 264 | * - acknowledge a suspend read from /dev/apm_bios. |
| 265 | * Only when everyone who has opened /dev/apm_bios with write permission |
| 266 | * has acknowledge does the actual suspend happen. |
| 267 | */ |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 268 | static long |
| 269 | apm_ioctl(struct file *filp, u_int cmd, u_long arg) |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 270 | { |
| 271 | struct apm_user *as = filp->private_data; |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 272 | int err = -EINVAL; |
| 273 | |
| 274 | if (!as->suser || !as->writer) |
| 275 | return -EPERM; |
| 276 | |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 277 | lock_kernel(); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 278 | switch (cmd) { |
| 279 | case APM_IOC_SUSPEND: |
| 280 | mutex_lock(&state_lock); |
| 281 | |
| 282 | as->suspend_result = -EINTR; |
| 283 | |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 284 | switch (as->suspend_state) { |
| 285 | case SUSPEND_READ: |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 286 | /* |
| 287 | * If we read a suspend command from /dev/apm_bios, |
| 288 | * then the corresponding APM_IOC_SUSPEND ioctl is |
| 289 | * interpreted as an acknowledge. |
| 290 | */ |
| 291 | as->suspend_state = SUSPEND_ACKED; |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 292 | atomic_dec(&suspend_acks_pending); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 293 | mutex_unlock(&state_lock); |
| 294 | |
| 295 | /* |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 296 | * suspend_acks_pending changed, the notifier needs to |
| 297 | * be woken up for this |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 298 | */ |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 299 | wake_up(&apm_suspend_waitqueue); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 300 | |
| 301 | /* |
| 302 | * Wait for the suspend/resume to complete. If there |
| 303 | * are pending acknowledges, we wait here for them. |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 304 | */ |
Rafael J. Wysocki | cb43c54 | 2007-11-21 02:53:14 +0100 | [diff] [blame] | 305 | freezer_do_not_count(); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 306 | |
| 307 | wait_event(apm_suspend_waitqueue, |
| 308 | as->suspend_state == SUSPEND_DONE); |
Rafael J. Wysocki | cb43c54 | 2007-11-21 02:53:14 +0100 | [diff] [blame] | 309 | |
| 310 | /* |
| 311 | * Since we are waiting until the suspend is done, the |
| 312 | * try_to_freeze() in freezer_count() will not trigger |
| 313 | */ |
| 314 | freezer_count(); |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 315 | break; |
| 316 | case SUSPEND_ACKTO: |
| 317 | as->suspend_result = -ETIMEDOUT; |
| 318 | mutex_unlock(&state_lock); |
| 319 | break; |
| 320 | default: |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 321 | as->suspend_state = SUSPEND_WAIT; |
| 322 | mutex_unlock(&state_lock); |
| 323 | |
| 324 | /* |
| 325 | * Otherwise it is a request to suspend the system. |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 326 | * Just invoke pm_suspend(), we'll handle it from |
| 327 | * there via the notifier. |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 328 | */ |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 329 | as->suspend_result = pm_suspend(PM_SUSPEND_MEM); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 332 | mutex_lock(&state_lock); |
| 333 | err = as->suspend_result; |
| 334 | as->suspend_state = SUSPEND_NONE; |
| 335 | mutex_unlock(&state_lock); |
| 336 | break; |
| 337 | } |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 338 | unlock_kernel(); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 339 | |
| 340 | return err; |
| 341 | } |
| 342 | |
| 343 | static int apm_release(struct inode * inode, struct file * filp) |
| 344 | { |
| 345 | struct apm_user *as = filp->private_data; |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 346 | |
| 347 | filp->private_data = NULL; |
| 348 | |
| 349 | down_write(&user_list_lock); |
| 350 | list_del(&as->list); |
| 351 | up_write(&user_list_lock); |
| 352 | |
| 353 | /* |
| 354 | * We are now unhooked from the chain. As far as new |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 355 | * events are concerned, we no longer exist. |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 356 | */ |
| 357 | mutex_lock(&state_lock); |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 358 | if (as->suspend_state == SUSPEND_PENDING || |
| 359 | as->suspend_state == SUSPEND_READ) |
| 360 | atomic_dec(&suspend_acks_pending); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 361 | mutex_unlock(&state_lock); |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 362 | |
| 363 | wake_up(&apm_suspend_waitqueue); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 364 | |
| 365 | kfree(as); |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static int apm_open(struct inode * inode, struct file * filp) |
| 370 | { |
| 371 | struct apm_user *as; |
| 372 | |
Arnd Bergmann | 2861ead | 2008-05-20 19:15:33 +0200 | [diff] [blame] | 373 | lock_kernel(); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 374 | as = kzalloc(sizeof(*as), GFP_KERNEL); |
| 375 | if (as) { |
| 376 | /* |
| 377 | * XXX - this is a tiny bit broken, when we consider BSD |
| 378 | * process accounting. If the device is opened by root, we |
| 379 | * instantly flag that we used superuser privs. Who knows, |
| 380 | * we might close the device immediately without doing a |
| 381 | * privileged operation -- cevans |
| 382 | */ |
| 383 | as->suser = capable(CAP_SYS_ADMIN); |
| 384 | as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE; |
| 385 | as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ; |
| 386 | |
| 387 | down_write(&user_list_lock); |
| 388 | list_add(&as->list, &apm_user_list); |
| 389 | up_write(&user_list_lock); |
| 390 | |
| 391 | filp->private_data = as; |
| 392 | } |
Arnd Bergmann | 2861ead | 2008-05-20 19:15:33 +0200 | [diff] [blame] | 393 | unlock_kernel(); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 394 | |
| 395 | return as ? 0 : -ENOMEM; |
| 396 | } |
| 397 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 398 | static const struct file_operations apm_bios_fops = { |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 399 | .owner = THIS_MODULE, |
| 400 | .read = apm_read, |
| 401 | .poll = apm_poll, |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 402 | .unlocked_ioctl = apm_ioctl, |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 403 | .open = apm_open, |
| 404 | .release = apm_release, |
| 405 | }; |
| 406 | |
| 407 | static struct miscdevice apm_device = { |
| 408 | .minor = APM_MINOR_DEV, |
| 409 | .name = "apm_bios", |
| 410 | .fops = &apm_bios_fops |
| 411 | }; |
| 412 | |
| 413 | |
| 414 | #ifdef CONFIG_PROC_FS |
| 415 | /* |
| 416 | * Arguments, with symbols from linux/apm_bios.h. |
| 417 | * |
| 418 | * 0) Linux driver version (this will change if format changes) |
| 419 | * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2. |
| 420 | * 2) APM flags from APM Installation Check (0x00): |
| 421 | * bit 0: APM_16_BIT_SUPPORT |
| 422 | * bit 1: APM_32_BIT_SUPPORT |
| 423 | * bit 2: APM_IDLE_SLOWS_CLOCK |
| 424 | * bit 3: APM_BIOS_DISABLED |
| 425 | * bit 4: APM_BIOS_DISENGAGED |
| 426 | * 3) AC line status |
| 427 | * 0x00: Off-line |
| 428 | * 0x01: On-line |
| 429 | * 0x02: On backup power (BIOS >= 1.1 only) |
| 430 | * 0xff: Unknown |
| 431 | * 4) Battery status |
| 432 | * 0x00: High |
| 433 | * 0x01: Low |
| 434 | * 0x02: Critical |
| 435 | * 0x03: Charging |
| 436 | * 0x04: Selected battery not present (BIOS >= 1.2 only) |
| 437 | * 0xff: Unknown |
| 438 | * 5) Battery flag |
| 439 | * bit 0: High |
| 440 | * bit 1: Low |
| 441 | * bit 2: Critical |
| 442 | * bit 3: Charging |
| 443 | * bit 7: No system battery |
| 444 | * 0xff: Unknown |
| 445 | * 6) Remaining battery life (percentage of charge): |
| 446 | * 0-100: valid |
| 447 | * -1: Unknown |
| 448 | * 7) Remaining battery life (time units): |
| 449 | * Number of remaining minutes or seconds |
| 450 | * -1: Unknown |
| 451 | * 8) min = minutes; sec = seconds |
| 452 | */ |
Alexey Dobriyan | 647634d | 2008-04-29 01:01:46 -0700 | [diff] [blame] | 453 | static int proc_apm_show(struct seq_file *m, void *v) |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 454 | { |
| 455 | struct apm_power_info info; |
| 456 | char *units; |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 457 | |
| 458 | info.ac_line_status = 0xff; |
| 459 | info.battery_status = 0xff; |
| 460 | info.battery_flag = 0xff; |
| 461 | info.battery_life = -1; |
| 462 | info.time = -1; |
| 463 | info.units = -1; |
| 464 | |
| 465 | if (apm_get_power_status) |
| 466 | apm_get_power_status(&info); |
| 467 | |
| 468 | switch (info.units) { |
| 469 | default: units = "?"; break; |
| 470 | case 0: units = "min"; break; |
| 471 | case 1: units = "sec"; break; |
| 472 | } |
| 473 | |
Alexey Dobriyan | 647634d | 2008-04-29 01:01:46 -0700 | [diff] [blame] | 474 | seq_printf(m, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n", |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 475 | driver_version, APM_32_BIT_SUPPORT, |
| 476 | info.ac_line_status, info.battery_status, |
| 477 | info.battery_flag, info.battery_life, |
| 478 | info.time, units); |
| 479 | |
Alexey Dobriyan | 647634d | 2008-04-29 01:01:46 -0700 | [diff] [blame] | 480 | return 0; |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 481 | } |
Alexey Dobriyan | 647634d | 2008-04-29 01:01:46 -0700 | [diff] [blame] | 482 | |
| 483 | static int proc_apm_open(struct inode *inode, struct file *file) |
| 484 | { |
| 485 | return single_open(file, proc_apm_show, NULL); |
| 486 | } |
| 487 | |
| 488 | static const struct file_operations apm_proc_fops = { |
| 489 | .owner = THIS_MODULE, |
| 490 | .open = proc_apm_open, |
| 491 | .read = seq_read, |
| 492 | .llseek = seq_lseek, |
| 493 | .release = single_release, |
| 494 | }; |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 495 | #endif |
| 496 | |
| 497 | static int kapmd(void *arg) |
| 498 | { |
| 499 | do { |
| 500 | apm_event_t event; |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 501 | |
| 502 | wait_event_interruptible(kapmd_wait, |
| 503 | !queue_empty(&kapmd_queue) || kthread_should_stop()); |
| 504 | |
| 505 | if (kthread_should_stop()) |
| 506 | break; |
| 507 | |
| 508 | spin_lock_irq(&kapmd_queue_lock); |
| 509 | event = 0; |
| 510 | if (!queue_empty(&kapmd_queue)) |
| 511 | event = queue_get_event(&kapmd_queue); |
| 512 | spin_unlock_irq(&kapmd_queue_lock); |
| 513 | |
| 514 | switch (event) { |
| 515 | case 0: |
| 516 | break; |
| 517 | |
| 518 | case APM_LOW_BATTERY: |
| 519 | case APM_POWER_STATUS_CHANGE: |
| 520 | queue_event(event); |
| 521 | break; |
| 522 | |
| 523 | case APM_USER_SUSPEND: |
| 524 | case APM_SYS_SUSPEND: |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 525 | pm_suspend(PM_SUSPEND_MEM); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 526 | break; |
| 527 | |
| 528 | case APM_CRITICAL_SUSPEND: |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 529 | atomic_inc(&userspace_notification_inhibit); |
| 530 | pm_suspend(PM_SUSPEND_MEM); |
| 531 | atomic_dec(&userspace_notification_inhibit); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 532 | break; |
| 533 | } |
| 534 | } while (1); |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 539 | static int apm_suspend_notifier(struct notifier_block *nb, |
| 540 | unsigned long event, |
| 541 | void *dummy) |
| 542 | { |
| 543 | struct apm_user *as; |
| 544 | int err; |
| 545 | |
| 546 | /* short-cut emergency suspends */ |
| 547 | if (atomic_read(&userspace_notification_inhibit)) |
| 548 | return NOTIFY_DONE; |
| 549 | |
| 550 | switch (event) { |
| 551 | case PM_SUSPEND_PREPARE: |
| 552 | /* |
| 553 | * Queue an event to all "writer" users that we want |
| 554 | * to suspend and need their ack. |
| 555 | */ |
| 556 | mutex_lock(&state_lock); |
| 557 | down_read(&user_list_lock); |
| 558 | |
| 559 | list_for_each_entry(as, &apm_user_list, list) { |
| 560 | if (as->suspend_state != SUSPEND_WAIT && as->reader && |
| 561 | as->writer && as->suser) { |
| 562 | as->suspend_state = SUSPEND_PENDING; |
| 563 | atomic_inc(&suspend_acks_pending); |
| 564 | queue_add_event(&as->queue, APM_USER_SUSPEND); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | up_read(&user_list_lock); |
| 569 | mutex_unlock(&state_lock); |
| 570 | wake_up_interruptible(&apm_waitqueue); |
| 571 | |
| 572 | /* |
| 573 | * Wait for the the suspend_acks_pending variable to drop to |
| 574 | * zero, meaning everybody acked the suspend event (or the |
| 575 | * process was killed.) |
| 576 | * |
| 577 | * If the app won't answer within a short while we assume it |
| 578 | * locked up and ignore it. |
| 579 | */ |
| 580 | err = wait_event_interruptible_timeout( |
| 581 | apm_suspend_waitqueue, |
| 582 | atomic_read(&suspend_acks_pending) == 0, |
| 583 | 5*HZ); |
| 584 | |
| 585 | /* timed out */ |
| 586 | if (err == 0) { |
| 587 | /* |
| 588 | * Move anybody who timed out to "ack timeout" state. |
| 589 | * |
| 590 | * We could time out and the userspace does the ACK |
| 591 | * right after we time out but before we enter the |
| 592 | * locked section here, but that's fine. |
| 593 | */ |
| 594 | mutex_lock(&state_lock); |
| 595 | down_read(&user_list_lock); |
| 596 | list_for_each_entry(as, &apm_user_list, list) { |
| 597 | if (as->suspend_state == SUSPEND_PENDING || |
| 598 | as->suspend_state == SUSPEND_READ) { |
| 599 | as->suspend_state = SUSPEND_ACKTO; |
| 600 | atomic_dec(&suspend_acks_pending); |
| 601 | } |
| 602 | } |
| 603 | up_read(&user_list_lock); |
| 604 | mutex_unlock(&state_lock); |
| 605 | } |
| 606 | |
| 607 | /* let suspend proceed */ |
| 608 | if (err >= 0) |
| 609 | return NOTIFY_OK; |
| 610 | |
| 611 | /* interrupted by signal */ |
| 612 | return NOTIFY_BAD; |
| 613 | |
| 614 | case PM_POST_SUSPEND: |
| 615 | /* |
| 616 | * Anyone on the APM queues will think we're still suspended. |
| 617 | * Send a message so everyone knows we're now awake again. |
| 618 | */ |
| 619 | queue_event(APM_NORMAL_RESUME); |
| 620 | |
| 621 | /* |
| 622 | * Finally, wake up anyone who is sleeping on the suspend. |
| 623 | */ |
| 624 | mutex_lock(&state_lock); |
| 625 | down_read(&user_list_lock); |
| 626 | list_for_each_entry(as, &apm_user_list, list) { |
| 627 | if (as->suspend_state == SUSPEND_ACKED) { |
| 628 | /* |
| 629 | * TODO: maybe grab error code, needs core |
| 630 | * changes to push the error to the notifier |
| 631 | * chain (could use the second parameter if |
| 632 | * implemented) |
| 633 | */ |
| 634 | as->suspend_result = 0; |
| 635 | as->suspend_state = SUSPEND_DONE; |
| 636 | } |
| 637 | } |
| 638 | up_read(&user_list_lock); |
| 639 | mutex_unlock(&state_lock); |
| 640 | |
| 641 | wake_up(&apm_suspend_waitqueue); |
| 642 | return NOTIFY_OK; |
| 643 | |
| 644 | default: |
| 645 | return NOTIFY_DONE; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | static struct notifier_block apm_notif_block = { |
| 650 | .notifier_call = apm_suspend_notifier, |
| 651 | }; |
| 652 | |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 653 | static int __init apm_init(void) |
| 654 | { |
| 655 | int ret; |
| 656 | |
| 657 | if (apm_disabled) { |
| 658 | printk(KERN_NOTICE "apm: disabled on user request.\n"); |
| 659 | return -ENODEV; |
| 660 | } |
| 661 | |
| 662 | kapmd_tsk = kthread_create(kapmd, NULL, "kapmd"); |
| 663 | if (IS_ERR(kapmd_tsk)) { |
| 664 | ret = PTR_ERR(kapmd_tsk); |
| 665 | kapmd_tsk = NULL; |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 666 | goto out; |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 667 | } |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 668 | wake_up_process(kapmd_tsk); |
| 669 | |
| 670 | #ifdef CONFIG_PROC_FS |
Alexey Dobriyan | 647634d | 2008-04-29 01:01:46 -0700 | [diff] [blame] | 671 | proc_create("apm", 0, NULL, &apm_proc_fops); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 672 | #endif |
| 673 | |
| 674 | ret = misc_register(&apm_device); |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 675 | if (ret) |
| 676 | goto out_stop; |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 677 | |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 678 | ret = register_pm_notifier(&apm_notif_block); |
| 679 | if (ret) |
| 680 | goto out_unregister; |
| 681 | |
| 682 | return 0; |
| 683 | |
| 684 | out_unregister: |
| 685 | misc_deregister(&apm_device); |
| 686 | out_stop: |
| 687 | remove_proc_entry("apm", NULL); |
| 688 | kthread_stop(kapmd_tsk); |
| 689 | out: |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | static void __exit apm_exit(void) |
| 694 | { |
Johannes Berg | d20a4dc | 2008-06-11 22:03:10 +0200 | [diff] [blame] | 695 | unregister_pm_notifier(&apm_notif_block); |
Ralf Baechle | 7726942 | 2007-02-09 17:08:57 +0000 | [diff] [blame] | 696 | misc_deregister(&apm_device); |
| 697 | remove_proc_entry("apm", NULL); |
| 698 | |
| 699 | kthread_stop(kapmd_tsk); |
| 700 | } |
| 701 | |
| 702 | module_init(apm_init); |
| 703 | module_exit(apm_exit); |
| 704 | |
| 705 | MODULE_AUTHOR("Stephen Rothwell"); |
| 706 | MODULE_DESCRIPTION("Advanced Power Management"); |
| 707 | MODULE_LICENSE("GPL"); |
| 708 | |
| 709 | #ifndef MODULE |
| 710 | static int __init apm_setup(char *str) |
| 711 | { |
| 712 | while ((str != NULL) && (*str != '\0')) { |
| 713 | if (strncmp(str, "off", 3) == 0) |
| 714 | apm_disabled = 1; |
| 715 | if (strncmp(str, "on", 2) == 0) |
| 716 | apm_disabled = 0; |
| 717 | str = strchr(str, ','); |
| 718 | if (str != NULL) |
| 719 | str += strspn(str, ", \t"); |
| 720 | } |
| 721 | return 1; |
| 722 | } |
| 723 | |
| 724 | __setup("apm=", apm_setup); |
| 725 | #endif |
| 726 | |
| 727 | /** |
| 728 | * apm_queue_event - queue an APM event for kapmd |
| 729 | * @event: APM event |
| 730 | * |
| 731 | * Queue an APM event for kapmd to process and ultimately take the |
| 732 | * appropriate action. Only a subset of events are handled: |
| 733 | * %APM_LOW_BATTERY |
| 734 | * %APM_POWER_STATUS_CHANGE |
| 735 | * %APM_USER_SUSPEND |
| 736 | * %APM_SYS_SUSPEND |
| 737 | * %APM_CRITICAL_SUSPEND |
| 738 | */ |
| 739 | void apm_queue_event(apm_event_t event) |
| 740 | { |
| 741 | unsigned long flags; |
| 742 | |
| 743 | spin_lock_irqsave(&kapmd_queue_lock, flags); |
| 744 | queue_add_event(&kapmd_queue, event); |
| 745 | spin_unlock_irqrestore(&kapmd_queue_lock, flags); |
| 746 | |
| 747 | wake_up_interruptible(&kapmd_wait); |
| 748 | } |
| 749 | EXPORT_SYMBOL(apm_queue_event); |