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