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