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