Merge branch 'generic-ipi' into generic-ipi-for-linus
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / char / apm-emulation.c
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>
16 #include <linux/smp_lock.h>
17 #include <linux/proc_fs.h>
18 #include <linux/seq_file.h>
19 #include <linux/miscdevice.h>
20 #include <linux/apm_bios.h>
21 #include <linux/capability.h>
22 #include <linux/sched.h>
23 #include <linux/suspend.h>
24 #include <linux/apm-emulation.h>
25 #include <linux/freezer.h>
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 /*
62 * The per-file APM data
63 */
64 struct apm_user {
65 struct list_head list;
66
67 unsigned int suser: 1;
68 unsigned int writer: 1;
69 unsigned int reader: 1;
70
71 int suspend_result;
72 unsigned int suspend_state;
73 #define SUSPEND_NONE 0 /* no suspend pending */
74 #define SUSPEND_PENDING 1 /* suspend pending read */
75 #define SUSPEND_READ 2 /* suspend read, pending ack */
76 #define SUSPEND_ACKED 3 /* suspend acked */
77 #define SUSPEND_WAIT 4 /* waiting for suspend */
78 #define SUSPEND_DONE 5 /* suspend completed */
79
80 struct apm_queue queue;
81 };
82
83 /*
84 * Local variables
85 */
86 static int suspends_pending;
87 static int apm_disabled;
88 static struct task_struct *kapmd_tsk;
89
90 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
91 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
92
93 /*
94 * This is a list of everyone who has opened /dev/apm_bios
95 */
96 static DECLARE_RWSEM(user_list_lock);
97 static LIST_HEAD(apm_user_list);
98
99 /*
100 * kapmd info. kapmd provides us a process context to handle
101 * "APM" events within - specifically necessary if we're going
102 * to be suspending the system.
103 */
104 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
105 static DEFINE_SPINLOCK(kapmd_queue_lock);
106 static struct apm_queue kapmd_queue;
107
108 static DEFINE_MUTEX(state_lock);
109
110 static const char driver_version[] = "1.13"; /* no spaces */
111
112
113
114 /*
115 * Compatibility cruft until the IPAQ people move over to the new
116 * interface.
117 */
118 static void __apm_get_power_status(struct apm_power_info *info)
119 {
120 }
121
122 /*
123 * This allows machines to provide their own "apm get power status" function.
124 */
125 void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status;
126 EXPORT_SYMBOL(apm_get_power_status);
127
128
129 /*
130 * APM event queue management.
131 */
132 static inline int queue_empty(struct apm_queue *q)
133 {
134 return q->event_head == q->event_tail;
135 }
136
137 static inline apm_event_t queue_get_event(struct apm_queue *q)
138 {
139 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
140 return q->events[q->event_tail];
141 }
142
143 static void queue_add_event(struct apm_queue *q, apm_event_t event)
144 {
145 q->event_head = (q->event_head + 1) % APM_MAX_EVENTS;
146 if (q->event_head == q->event_tail) {
147 static int notified;
148
149 if (notified++ == 0)
150 printk(KERN_ERR "apm: an event queue overflowed\n");
151 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
152 }
153 q->events[q->event_head] = event;
154 }
155
156 static void queue_event(apm_event_t event)
157 {
158 struct apm_user *as;
159
160 down_read(&user_list_lock);
161 list_for_each_entry(as, &apm_user_list, list) {
162 if (as->reader)
163 queue_add_event(&as->queue, event);
164 }
165 up_read(&user_list_lock);
166 wake_up_interruptible(&apm_waitqueue);
167 }
168
169 /*
170 * queue_suspend_event - queue an APM suspend event.
171 *
172 * Check that we're in a state where we can suspend. If not,
173 * return -EBUSY. Otherwise, queue an event to all "writer"
174 * users. If there are no "writer" users, return '1' to
175 * indicate that we can immediately suspend.
176 */
177 static int queue_suspend_event(apm_event_t event, struct apm_user *sender)
178 {
179 struct apm_user *as;
180 int ret = 1;
181
182 mutex_lock(&state_lock);
183 down_read(&user_list_lock);
184
185 /*
186 * If a thread is still processing, we can't suspend, so reject
187 * the request.
188 */
189 list_for_each_entry(as, &apm_user_list, list) {
190 if (as != sender && as->reader && as->writer && as->suser &&
191 as->suspend_state != SUSPEND_NONE) {
192 ret = -EBUSY;
193 goto out;
194 }
195 }
196
197 list_for_each_entry(as, &apm_user_list, list) {
198 if (as != sender && as->reader && as->writer && as->suser) {
199 as->suspend_state = SUSPEND_PENDING;
200 suspends_pending++;
201 queue_add_event(&as->queue, event);
202 ret = 0;
203 }
204 }
205 out:
206 up_read(&user_list_lock);
207 mutex_unlock(&state_lock);
208 wake_up_interruptible(&apm_waitqueue);
209 return ret;
210 }
211
212 static void apm_suspend(void)
213 {
214 struct apm_user *as;
215 int err = pm_suspend(PM_SUSPEND_MEM);
216
217 /*
218 * Anyone on the APM queues will think we're still suspended.
219 * Send a message so everyone knows we're now awake again.
220 */
221 queue_event(APM_NORMAL_RESUME);
222
223 /*
224 * Finally, wake up anyone who is sleeping on the suspend.
225 */
226 mutex_lock(&state_lock);
227 down_read(&user_list_lock);
228 list_for_each_entry(as, &apm_user_list, list) {
229 if (as->suspend_state == SUSPEND_WAIT ||
230 as->suspend_state == SUSPEND_ACKED) {
231 as->suspend_result = err;
232 as->suspend_state = SUSPEND_DONE;
233 }
234 }
235 up_read(&user_list_lock);
236 mutex_unlock(&state_lock);
237
238 wake_up(&apm_suspend_waitqueue);
239 }
240
241 static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
242 {
243 struct apm_user *as = fp->private_data;
244 apm_event_t event;
245 int i = count, ret = 0;
246
247 if (count < sizeof(apm_event_t))
248 return -EINVAL;
249
250 if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
251 return -EAGAIN;
252
253 wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue));
254
255 while ((i >= sizeof(event)) && !queue_empty(&as->queue)) {
256 event = queue_get_event(&as->queue);
257
258 ret = -EFAULT;
259 if (copy_to_user(buf, &event, sizeof(event)))
260 break;
261
262 mutex_lock(&state_lock);
263 if (as->suspend_state == SUSPEND_PENDING &&
264 (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND))
265 as->suspend_state = SUSPEND_READ;
266 mutex_unlock(&state_lock);
267
268 buf += sizeof(event);
269 i -= sizeof(event);
270 }
271
272 if (i < count)
273 ret = count - i;
274
275 return ret;
276 }
277
278 static unsigned int apm_poll(struct file *fp, poll_table * wait)
279 {
280 struct apm_user *as = fp->private_data;
281
282 poll_wait(fp, &apm_waitqueue, wait);
283 return queue_empty(&as->queue) ? 0 : POLLIN | POLLRDNORM;
284 }
285
286 /*
287 * apm_ioctl - handle APM ioctl
288 *
289 * APM_IOC_SUSPEND
290 * This IOCTL is overloaded, and performs two functions. It is used to:
291 * - initiate a suspend
292 * - acknowledge a suspend read from /dev/apm_bios.
293 * Only when everyone who has opened /dev/apm_bios with write permission
294 * has acknowledge does the actual suspend happen.
295 */
296 static int
297 apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
298 {
299 struct apm_user *as = filp->private_data;
300 int err = -EINVAL;
301
302 if (!as->suser || !as->writer)
303 return -EPERM;
304
305 switch (cmd) {
306 case APM_IOC_SUSPEND:
307 mutex_lock(&state_lock);
308
309 as->suspend_result = -EINTR;
310
311 if (as->suspend_state == SUSPEND_READ) {
312 int pending;
313
314 /*
315 * If we read a suspend command from /dev/apm_bios,
316 * then the corresponding APM_IOC_SUSPEND ioctl is
317 * interpreted as an acknowledge.
318 */
319 as->suspend_state = SUSPEND_ACKED;
320 suspends_pending--;
321 pending = suspends_pending == 0;
322 mutex_unlock(&state_lock);
323
324 /*
325 * If there are no further acknowledges required,
326 * suspend the system.
327 */
328 if (pending)
329 apm_suspend();
330
331 /*
332 * Wait for the suspend/resume to complete. If there
333 * are pending acknowledges, we wait here for them.
334 */
335 freezer_do_not_count();
336
337 wait_event(apm_suspend_waitqueue,
338 as->suspend_state == SUSPEND_DONE);
339
340 /*
341 * Since we are waiting until the suspend is done, the
342 * try_to_freeze() in freezer_count() will not trigger
343 */
344 freezer_count();
345 } else {
346 as->suspend_state = SUSPEND_WAIT;
347 mutex_unlock(&state_lock);
348
349 /*
350 * Otherwise it is a request to suspend the system.
351 * Queue an event for all readers, and expect an
352 * acknowledge from all writers who haven't already
353 * acknowledged.
354 */
355 err = queue_suspend_event(APM_USER_SUSPEND, as);
356 if (err < 0) {
357 /*
358 * Avoid taking the lock here - this
359 * should be fine.
360 */
361 as->suspend_state = SUSPEND_NONE;
362 break;
363 }
364
365 if (err > 0)
366 apm_suspend();
367
368 /*
369 * Wait for the suspend/resume to complete. If there
370 * are pending acknowledges, we wait here for them.
371 */
372 wait_event_freezable(apm_suspend_waitqueue,
373 as->suspend_state == SUSPEND_DONE);
374 }
375
376 mutex_lock(&state_lock);
377 err = as->suspend_result;
378 as->suspend_state = SUSPEND_NONE;
379 mutex_unlock(&state_lock);
380 break;
381 }
382
383 return err;
384 }
385
386 static int apm_release(struct inode * inode, struct file * filp)
387 {
388 struct apm_user *as = filp->private_data;
389 int pending = 0;
390
391 filp->private_data = NULL;
392
393 down_write(&user_list_lock);
394 list_del(&as->list);
395 up_write(&user_list_lock);
396
397 /*
398 * We are now unhooked from the chain. As far as new
399 * events are concerned, we no longer exist. However, we
400 * need to balance suspends_pending, which means the
401 * possibility of sleeping.
402 */
403 mutex_lock(&state_lock);
404 if (as->suspend_state != SUSPEND_NONE) {
405 suspends_pending -= 1;
406 pending = suspends_pending == 0;
407 }
408 mutex_unlock(&state_lock);
409 if (pending)
410 apm_suspend();
411
412 kfree(as);
413 return 0;
414 }
415
416 static int apm_open(struct inode * inode, struct file * filp)
417 {
418 struct apm_user *as;
419
420 lock_kernel();
421 as = kzalloc(sizeof(*as), GFP_KERNEL);
422 if (as) {
423 /*
424 * XXX - this is a tiny bit broken, when we consider BSD
425 * process accounting. If the device is opened by root, we
426 * instantly flag that we used superuser privs. Who knows,
427 * we might close the device immediately without doing a
428 * privileged operation -- cevans
429 */
430 as->suser = capable(CAP_SYS_ADMIN);
431 as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
432 as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
433
434 down_write(&user_list_lock);
435 list_add(&as->list, &apm_user_list);
436 up_write(&user_list_lock);
437
438 filp->private_data = as;
439 }
440 unlock_kernel();
441
442 return as ? 0 : -ENOMEM;
443 }
444
445 static struct file_operations apm_bios_fops = {
446 .owner = THIS_MODULE,
447 .read = apm_read,
448 .poll = apm_poll,
449 .ioctl = apm_ioctl,
450 .open = apm_open,
451 .release = apm_release,
452 };
453
454 static struct miscdevice apm_device = {
455 .minor = APM_MINOR_DEV,
456 .name = "apm_bios",
457 .fops = &apm_bios_fops
458 };
459
460
461 #ifdef CONFIG_PROC_FS
462 /*
463 * Arguments, with symbols from linux/apm_bios.h.
464 *
465 * 0) Linux driver version (this will change if format changes)
466 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
467 * 2) APM flags from APM Installation Check (0x00):
468 * bit 0: APM_16_BIT_SUPPORT
469 * bit 1: APM_32_BIT_SUPPORT
470 * bit 2: APM_IDLE_SLOWS_CLOCK
471 * bit 3: APM_BIOS_DISABLED
472 * bit 4: APM_BIOS_DISENGAGED
473 * 3) AC line status
474 * 0x00: Off-line
475 * 0x01: On-line
476 * 0x02: On backup power (BIOS >= 1.1 only)
477 * 0xff: Unknown
478 * 4) Battery status
479 * 0x00: High
480 * 0x01: Low
481 * 0x02: Critical
482 * 0x03: Charging
483 * 0x04: Selected battery not present (BIOS >= 1.2 only)
484 * 0xff: Unknown
485 * 5) Battery flag
486 * bit 0: High
487 * bit 1: Low
488 * bit 2: Critical
489 * bit 3: Charging
490 * bit 7: No system battery
491 * 0xff: Unknown
492 * 6) Remaining battery life (percentage of charge):
493 * 0-100: valid
494 * -1: Unknown
495 * 7) Remaining battery life (time units):
496 * Number of remaining minutes or seconds
497 * -1: Unknown
498 * 8) min = minutes; sec = seconds
499 */
500 static int proc_apm_show(struct seq_file *m, void *v)
501 {
502 struct apm_power_info info;
503 char *units;
504
505 info.ac_line_status = 0xff;
506 info.battery_status = 0xff;
507 info.battery_flag = 0xff;
508 info.battery_life = -1;
509 info.time = -1;
510 info.units = -1;
511
512 if (apm_get_power_status)
513 apm_get_power_status(&info);
514
515 switch (info.units) {
516 default: units = "?"; break;
517 case 0: units = "min"; break;
518 case 1: units = "sec"; break;
519 }
520
521 seq_printf(m, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
522 driver_version, APM_32_BIT_SUPPORT,
523 info.ac_line_status, info.battery_status,
524 info.battery_flag, info.battery_life,
525 info.time, units);
526
527 return 0;
528 }
529
530 static int proc_apm_open(struct inode *inode, struct file *file)
531 {
532 return single_open(file, proc_apm_show, NULL);
533 }
534
535 static const struct file_operations apm_proc_fops = {
536 .owner = THIS_MODULE,
537 .open = proc_apm_open,
538 .read = seq_read,
539 .llseek = seq_lseek,
540 .release = single_release,
541 };
542 #endif
543
544 static int kapmd(void *arg)
545 {
546 do {
547 apm_event_t event;
548 int ret;
549
550 wait_event_interruptible(kapmd_wait,
551 !queue_empty(&kapmd_queue) || kthread_should_stop());
552
553 if (kthread_should_stop())
554 break;
555
556 spin_lock_irq(&kapmd_queue_lock);
557 event = 0;
558 if (!queue_empty(&kapmd_queue))
559 event = queue_get_event(&kapmd_queue);
560 spin_unlock_irq(&kapmd_queue_lock);
561
562 switch (event) {
563 case 0:
564 break;
565
566 case APM_LOW_BATTERY:
567 case APM_POWER_STATUS_CHANGE:
568 queue_event(event);
569 break;
570
571 case APM_USER_SUSPEND:
572 case APM_SYS_SUSPEND:
573 ret = queue_suspend_event(event, NULL);
574 if (ret < 0) {
575 /*
576 * We were busy. Try again in 50ms.
577 */
578 queue_add_event(&kapmd_queue, event);
579 msleep(50);
580 }
581 if (ret > 0)
582 apm_suspend();
583 break;
584
585 case APM_CRITICAL_SUSPEND:
586 apm_suspend();
587 break;
588 }
589 } while (1);
590
591 return 0;
592 }
593
594 static int __init apm_init(void)
595 {
596 int ret;
597
598 if (apm_disabled) {
599 printk(KERN_NOTICE "apm: disabled on user request.\n");
600 return -ENODEV;
601 }
602
603 kapmd_tsk = kthread_create(kapmd, NULL, "kapmd");
604 if (IS_ERR(kapmd_tsk)) {
605 ret = PTR_ERR(kapmd_tsk);
606 kapmd_tsk = NULL;
607 return ret;
608 }
609 wake_up_process(kapmd_tsk);
610
611 #ifdef CONFIG_PROC_FS
612 proc_create("apm", 0, NULL, &apm_proc_fops);
613 #endif
614
615 ret = misc_register(&apm_device);
616 if (ret != 0) {
617 remove_proc_entry("apm", NULL);
618 kthread_stop(kapmd_tsk);
619 }
620
621 return ret;
622 }
623
624 static void __exit apm_exit(void)
625 {
626 misc_deregister(&apm_device);
627 remove_proc_entry("apm", NULL);
628
629 kthread_stop(kapmd_tsk);
630 }
631
632 module_init(apm_init);
633 module_exit(apm_exit);
634
635 MODULE_AUTHOR("Stephen Rothwell");
636 MODULE_DESCRIPTION("Advanced Power Management");
637 MODULE_LICENSE("GPL");
638
639 #ifndef MODULE
640 static int __init apm_setup(char *str)
641 {
642 while ((str != NULL) && (*str != '\0')) {
643 if (strncmp(str, "off", 3) == 0)
644 apm_disabled = 1;
645 if (strncmp(str, "on", 2) == 0)
646 apm_disabled = 0;
647 str = strchr(str, ',');
648 if (str != NULL)
649 str += strspn(str, ", \t");
650 }
651 return 1;
652 }
653
654 __setup("apm=", apm_setup);
655 #endif
656
657 /**
658 * apm_queue_event - queue an APM event for kapmd
659 * @event: APM event
660 *
661 * Queue an APM event for kapmd to process and ultimately take the
662 * appropriate action. Only a subset of events are handled:
663 * %APM_LOW_BATTERY
664 * %APM_POWER_STATUS_CHANGE
665 * %APM_USER_SUSPEND
666 * %APM_SYS_SUSPEND
667 * %APM_CRITICAL_SUSPEND
668 */
669 void apm_queue_event(apm_event_t event)
670 {
671 unsigned long flags;
672
673 spin_lock_irqsave(&kapmd_queue_lock, flags);
674 queue_add_event(&kapmd_queue, event);
675 spin_unlock_irqrestore(&kapmd_queue_lock, flags);
676
677 wake_up_interruptible(&kapmd_wait);
678 }
679 EXPORT_SYMBOL(apm_queue_event);