]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - arch/sh/kernel/apm.c
Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[mirror_ubuntu-hirsute-kernel.git] / arch / sh / kernel / apm.c
1 /*
2 * bios-less APM driver for hp680
3 *
4 * Copyright 2005 (c) Andriy Skulysh <askulysh@gmail.com>
5 *
6 * based on ARM APM driver by
7 * Jamey Hicks <jamey@crl.dec.com>
8 *
9 * adapted from the APM BIOS driver for Linux by
10 * Stephen Rothwell (sfr@linuxcare.com)
11 *
12 * APM 1.2 Reference:
13 * Intel Corporation, Microsoft Corporation. Advanced Power Management
14 * (APM) BIOS Interface Specification, Revision 1.2, February 1996.
15 *
16 * [This document is available from Microsoft at:
17 * http://www.microsoft.com/hwdev/busbios/amp_12.htm]
18 */
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/poll.h>
22 #include <linux/timer.h>
23 #include <linux/slab.h>
24 #include <linux/proc_fs.h>
25 #include <linux/miscdevice.h>
26 #include <linux/apm_bios.h>
27 #include <linux/pm.h>
28 #include <linux/pm_legacy.h>
29 #include <asm/apm.h>
30
31 #define MODNAME "apm"
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 * Maximum number of events stored
41 */
42 #define APM_MAX_EVENTS 16
43
44 struct apm_queue {
45 unsigned int event_head;
46 unsigned int event_tail;
47 apm_event_t events[APM_MAX_EVENTS];
48 };
49
50 /*
51 * The per-file APM data
52 */
53 struct apm_user {
54 struct list_head list;
55
56 unsigned int suser: 1;
57 unsigned int writer: 1;
58 unsigned int reader: 1;
59
60 int suspend_result;
61 unsigned int suspend_state;
62 #define SUSPEND_NONE 0 /* no suspend pending */
63 #define SUSPEND_PENDING 1 /* suspend pending read */
64 #define SUSPEND_READ 2 /* suspend read, pending ack */
65 #define SUSPEND_ACKED 3 /* suspend acked */
66 #define SUSPEND_DONE 4 /* suspend completed */
67
68 struct apm_queue queue;
69 };
70
71 /*
72 * Local variables
73 */
74 static int suspends_pending;
75
76 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
77 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
78
79 /*
80 * This is a list of everyone who has opened /dev/apm_bios
81 */
82 static DECLARE_RWSEM(user_list_lock);
83 static LIST_HEAD(apm_user_list);
84
85 /*
86 * kapmd info. kapmd provides us a process context to handle
87 * "APM" events within - specifically necessary if we're going
88 * to be suspending the system.
89 */
90 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
91 static DECLARE_COMPLETION(kapmd_exit);
92 static DEFINE_SPINLOCK(kapmd_queue_lock);
93 static struct apm_queue kapmd_queue;
94
95 int apm_suspended;
96 EXPORT_SYMBOL(apm_suspended);
97
98 /* Platform-specific apm_read_proc(). */
99 int (*apm_get_info)(char *buf, char **start, off_t fpos, int length);
100 EXPORT_SYMBOL(apm_get_info);
101
102 /*
103 * APM event queue management.
104 */
105 static inline int queue_empty(struct apm_queue *q)
106 {
107 return q->event_head == q->event_tail;
108 }
109
110 static inline apm_event_t queue_get_event(struct apm_queue *q)
111 {
112 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
113 return q->events[q->event_tail];
114 }
115
116 static void queue_add_event(struct apm_queue *q, apm_event_t event)
117 {
118 q->event_head = (q->event_head + 1) % APM_MAX_EVENTS;
119 if (q->event_head == q->event_tail) {
120 static int notified;
121
122 if (notified++ == 0)
123 printk(KERN_ERR "apm: an event queue overflowed\n");
124
125 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
126 }
127 q->events[q->event_head] = event;
128 }
129
130 static void queue_event_one_user(struct apm_user *as, apm_event_t event)
131 {
132 if (as->suser && as->writer) {
133 switch (event) {
134 case APM_SYS_SUSPEND:
135 case APM_USER_SUSPEND:
136 /*
137 * If this user already has a suspend pending,
138 * don't queue another one.
139 */
140 if (as->suspend_state != SUSPEND_NONE)
141 return;
142
143 as->suspend_state = SUSPEND_PENDING;
144 suspends_pending++;
145 break;
146 }
147 }
148 queue_add_event(&as->queue, event);
149 }
150
151 static void queue_event(apm_event_t event, struct apm_user *sender)
152 {
153 struct apm_user *as;
154
155 down_read(&user_list_lock);
156
157 list_for_each_entry(as, &apm_user_list, list)
158 if (as != sender && as->reader)
159 queue_event_one_user(as, event);
160
161 up_read(&user_list_lock);
162 wake_up_interruptible(&apm_waitqueue);
163 }
164
165 /**
166 * apm_queue_event - queue an APM event for kapmd
167 * @event: APM event
168 *
169 * Queue an APM event for kapmd to process and ultimately take the
170 * appropriate action. Only a subset of events are handled:
171 * %APM_LOW_BATTERY
172 * %APM_POWER_STATUS_CHANGE
173 * %APM_USER_SUSPEND
174 * %APM_SYS_SUSPEND
175 * %APM_CRITICAL_SUSPEND
176 */
177 void apm_queue_event(apm_event_t event)
178 {
179 spin_lock_irq(&kapmd_queue_lock);
180 queue_add_event(&kapmd_queue, event);
181 spin_unlock_irq(&kapmd_queue_lock);
182
183 wake_up_interruptible(&kapmd_wait);
184 }
185 EXPORT_SYMBOL(apm_queue_event);
186
187 static void apm_suspend(void)
188 {
189 struct apm_user *as;
190 int err;
191
192 apm_suspended = 1;
193 err = pm_suspend(PM_SUSPEND_MEM);
194
195 /*
196 * Anyone on the APM queues will think we're still suspended.
197 * Send a message so everyone knows we're now awake again.
198 */
199 queue_event(APM_NORMAL_RESUME, NULL);
200
201 /*
202 * Finally, wake up anyone who is sleeping on the suspend.
203 */
204 down_read(&user_list_lock);
205 list_for_each_entry(as, &apm_user_list, list) {
206 as->suspend_result = err;
207 as->suspend_state = SUSPEND_DONE;
208 }
209 up_read(&user_list_lock);
210
211 wake_up(&apm_suspend_waitqueue);
212 apm_suspended = 0;
213 }
214
215 static ssize_t apm_read(struct file *fp, char __user *buf,
216 size_t count, loff_t *ppos)
217 {
218 struct apm_user *as = fp->private_data;
219 apm_event_t event;
220 int i = count, ret = 0;
221
222 if (count < sizeof(apm_event_t))
223 return -EINVAL;
224
225 if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
226 return -EAGAIN;
227
228 wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue));
229
230 while ((i >= sizeof(event)) && !queue_empty(&as->queue)) {
231 event = queue_get_event(&as->queue);
232
233 ret = -EFAULT;
234 if (copy_to_user(buf, &event, sizeof(event)))
235 break;
236
237 if (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND)
238 as->suspend_state = SUSPEND_READ;
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 */
268 static int
269 apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
270 {
271 struct apm_user *as = filp->private_data;
272 unsigned long flags;
273 int err = -EINVAL;
274
275 if (!as->suser || !as->writer)
276 return -EPERM;
277
278 switch (cmd) {
279 case APM_IOC_SUSPEND:
280 as->suspend_result = -EINTR;
281
282 if (as->suspend_state == SUSPEND_READ) {
283 /*
284 * If we read a suspend command from /dev/apm_bios,
285 * then the corresponding APM_IOC_SUSPEND ioctl is
286 * interpreted as an acknowledge.
287 */
288 as->suspend_state = SUSPEND_ACKED;
289 suspends_pending--;
290 } else {
291 /*
292 * Otherwise it is a request to suspend the system.
293 * Queue an event for all readers, and expect an
294 * acknowledge from all writers who haven't already
295 * acknowledged.
296 */
297 queue_event(APM_USER_SUSPEND, as);
298 }
299
300 /*
301 * If there are no further acknowledges required, suspend
302 * the system.
303 */
304 if (suspends_pending == 0)
305 apm_suspend();
306
307 /*
308 * Wait for the suspend/resume to complete. If there are
309 * pending acknowledges, we wait here for them.
310 *
311 * Note that we need to ensure that the PM subsystem does
312 * not kick us out of the wait when it suspends the threads.
313 */
314 flags = current->flags;
315 current->flags |= PF_NOFREEZE;
316
317 /*
318 * Note: do not allow a thread which is acking the suspend
319 * to escape until the resume is complete.
320 */
321 if (as->suspend_state == SUSPEND_ACKED)
322 wait_event(apm_suspend_waitqueue,
323 as->suspend_state == SUSPEND_DONE);
324 else
325 wait_event_interruptible(apm_suspend_waitqueue,
326 as->suspend_state == SUSPEND_DONE);
327
328 current->flags = flags;
329 err = as->suspend_result;
330 as->suspend_state = SUSPEND_NONE;
331 break;
332 }
333
334 return err;
335 }
336
337 static int apm_release(struct inode * inode, struct file * filp)
338 {
339 struct apm_user *as = filp->private_data;
340 filp->private_data = NULL;
341
342 down_write(&user_list_lock);
343 list_del(&as->list);
344 up_write(&user_list_lock);
345
346 /*
347 * We are now unhooked from the chain. As far as new
348 * events are concerned, we no longer exist. However, we
349 * need to balance suspends_pending, which means the
350 * possibility of sleeping.
351 */
352 if (as->suspend_state != SUSPEND_NONE) {
353 suspends_pending -= 1;
354 if (suspends_pending == 0)
355 apm_suspend();
356 }
357
358 kfree(as);
359 return 0;
360 }
361
362 static int apm_open(struct inode * inode, struct file * filp)
363 {
364 struct apm_user *as;
365
366 as = kzalloc(sizeof(*as), GFP_KERNEL);
367 if (as) {
368 /*
369 * XXX - this is a tiny bit broken, when we consider BSD
370 * process accounting. If the device is opened by root, we
371 * instantly flag that we used superuser privs. Who knows,
372 * we might close the device immediately without doing a
373 * privileged operation -- cevans
374 */
375 as->suser = capable(CAP_SYS_ADMIN);
376 as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
377 as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
378
379 down_write(&user_list_lock);
380 list_add(&as->list, &apm_user_list);
381 up_write(&user_list_lock);
382
383 filp->private_data = as;
384 }
385
386 return as ? 0 : -ENOMEM;
387 }
388
389 static struct file_operations apm_bios_fops = {
390 .owner = THIS_MODULE,
391 .read = apm_read,
392 .poll = apm_poll,
393 .ioctl = apm_ioctl,
394 .open = apm_open,
395 .release = apm_release,
396 };
397
398 static struct miscdevice apm_device = {
399 .minor = APM_MINOR_DEV,
400 .name = "apm_bios",
401 .fops = &apm_bios_fops
402 };
403
404
405 #ifdef CONFIG_PROC_FS
406 /*
407 * Arguments, with symbols from linux/apm_bios.h.
408 *
409 * 0) Linux driver version (this will change if format changes)
410 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
411 * 2) APM flags from APM Installation Check (0x00):
412 * bit 0: APM_16_BIT_SUPPORT
413 * bit 1: APM_32_BIT_SUPPORT
414 * bit 2: APM_IDLE_SLOWS_CLOCK
415 * bit 3: APM_BIOS_DISABLED
416 * bit 4: APM_BIOS_DISENGAGED
417 * 3) AC line status
418 * 0x00: Off-line
419 * 0x01: On-line
420 * 0x02: On backup power (BIOS >= 1.1 only)
421 * 0xff: Unknown
422 * 4) Battery status
423 * 0x00: High
424 * 0x01: Low
425 * 0x02: Critical
426 * 0x03: Charging
427 * 0x04: Selected battery not present (BIOS >= 1.2 only)
428 * 0xff: Unknown
429 * 5) Battery flag
430 * bit 0: High
431 * bit 1: Low
432 * bit 2: Critical
433 * bit 3: Charging
434 * bit 7: No system battery
435 * 0xff: Unknown
436 * 6) Remaining battery life (percentage of charge):
437 * 0-100: valid
438 * -1: Unknown
439 * 7) Remaining battery life (time units):
440 * Number of remaining minutes or seconds
441 * -1: Unknown
442 * 8) min = minutes; sec = seconds
443 */
444 static int apm_read_proc(char *buf, char **start, off_t fpos, int length)
445 {
446 if (likely(apm_get_info))
447 return apm_get_info(buf, start, fpos, length);
448
449 return -EINVAL;
450 }
451 #endif
452
453 static int kapmd(void *arg)
454 {
455 daemonize("kapmd");
456 current->flags |= PF_NOFREEZE;
457
458 do {
459 apm_event_t event;
460
461 wait_event_interruptible(kapmd_wait,
462 !queue_empty(&kapmd_queue) || !pm_active);
463
464 if (!pm_active)
465 break;
466
467 spin_lock_irq(&kapmd_queue_lock);
468 event = 0;
469 if (!queue_empty(&kapmd_queue))
470 event = queue_get_event(&kapmd_queue);
471 spin_unlock_irq(&kapmd_queue_lock);
472
473 switch (event) {
474 case 0:
475 break;
476
477 case APM_LOW_BATTERY:
478 case APM_POWER_STATUS_CHANGE:
479 queue_event(event, NULL);
480 break;
481
482 case APM_USER_SUSPEND:
483 case APM_SYS_SUSPEND:
484 queue_event(event, NULL);
485 if (suspends_pending == 0)
486 apm_suspend();
487 break;
488
489 case APM_CRITICAL_SUSPEND:
490 apm_suspend();
491 break;
492 }
493 } while (1);
494
495 complete_and_exit(&kapmd_exit, 0);
496 }
497
498 static int __init apm_init(void)
499 {
500 int ret;
501
502 pm_active = 1;
503
504 ret = kernel_thread(kapmd, NULL, CLONE_KERNEL);
505 if (unlikely(ret < 0)) {
506 pm_active = 0;
507 return ret;
508 }
509
510 create_proc_info_entry("apm", 0, NULL, apm_read_proc);
511
512 ret = misc_register(&apm_device);
513 if (unlikely(ret != 0)) {
514 remove_proc_entry("apm", NULL);
515
516 pm_active = 0;
517 wake_up(&kapmd_wait);
518 wait_for_completion(&kapmd_exit);
519 }
520
521 return ret;
522 }
523
524 static void __exit apm_exit(void)
525 {
526 misc_deregister(&apm_device);
527 remove_proc_entry("apm", NULL);
528
529 pm_active = 0;
530 wake_up(&kapmd_wait);
531 wait_for_completion(&kapmd_exit);
532 }
533
534 module_init(apm_init);
535 module_exit(apm_exit);
536
537 MODULE_AUTHOR("Stephen Rothwell, Andriy Skulysh");
538 MODULE_DESCRIPTION("Advanced Power Management");
539 MODULE_LICENSE("GPL");