aboutsummaryrefslogtreecommitdiffstats
path: root/arch
diff options
context:
space:
mode:
authorPaul Mundt <lethal@linux-sh.org>2007-01-24 07:56:20 -0500
committerRalf Baechle <ralf@linux-mips.org>2007-02-09 12:08:58 -0500
commit0a9b0db19262dbb09f3a34195e68cafd5dc3fa10 (patch)
tree4fc4cd76617f8899494ae1f2b58aac4afe114490 /arch
parent2116245ee121af820225834e9695005ab07d1e84 (diff)
[APM] SH: Convert to use shared APM emulation.
Signed-off-by: Paul Mundt <lethal@linux-sh.org> Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
Diffstat (limited to 'arch')
-rw-r--r--arch/sh/Kconfig7
-rw-r--r--arch/sh/boards/hp6xx/hp6xx_apm.c68
-rw-r--r--arch/sh/kernel/Makefile1
-rw-r--r--arch/sh/kernel/apm.c538
4 files changed, 28 insertions, 586 deletions
diff --git a/arch/sh/Kconfig b/arch/sh/Kconfig
index 3aa3b885ab36..4f3891215b87 100644
--- a/arch/sh/Kconfig
+++ b/arch/sh/Kconfig
@@ -48,6 +48,9 @@ config GENERIC_IOMAP
48config GENERIC_TIME 48config GENERIC_TIME
49 def_bool n 49 def_bool n
50 50
51config SYS_SUPPORTS_APM_EMULATION
52 bool
53
51config ARCH_MAY_HAVE_PC_FDC 54config ARCH_MAY_HAVE_PC_FDC
52 bool 55 bool
53 56
@@ -126,6 +129,7 @@ config SH_7751_SYSTEMH
126 129
127config SH_HP6XX 130config SH_HP6XX
128 bool "HP6XX" 131 bool "HP6XX"
132 select SYS_SUPPORTS_APM_EMULATION
129 help 133 help
130 Select HP6XX if configuring for a HP jornada HP6xx. 134 Select HP6XX if configuring for a HP jornada HP6xx.
131 More information (hardware only) at 135 More information (hardware only) at
@@ -694,9 +698,6 @@ depends on EXPERIMENTAL
694 698
695source kernel/power/Kconfig 699source kernel/power/Kconfig
696 700
697config APM
698 bool "Advanced Power Management Emulation"
699 depends on PM
700endmenu 701endmenu
701 702
702source "net/Kconfig" 703source "net/Kconfig"
diff --git a/arch/sh/boards/hp6xx/hp6xx_apm.c b/arch/sh/boards/hp6xx/hp6xx_apm.c
index d146cdaa0b8b..d1c1460c8a06 100644
--- a/arch/sh/boards/hp6xx/hp6xx_apm.c
+++ b/arch/sh/boards/hp6xx/hp6xx_apm.c
@@ -7,12 +7,11 @@
7 * modify it under the terms of the GNU General Public License. 7 * modify it under the terms of the GNU General Public License.
8 */ 8 */
9#include <linux/module.h> 9#include <linux/module.h>
10#include <linux/apm_bios.h>
11#include <linux/kernel.h> 10#include <linux/kernel.h>
12#include <linux/init.h> 11#include <linux/init.h>
13#include <linux/interrupt.h> 12#include <linux/interrupt.h>
14#include <asm/io.h> 13#include <linux/apm-emulation.h>
15#include <asm/apm.h> 14#include <linux/io.h>
16#include <asm/adc.h> 15#include <asm/adc.h>
17#include <asm/hp6xx.h> 16#include <asm/hp6xx.h>
18 17
@@ -27,60 +26,41 @@
27 26
28#define MODNAME "hp6x0_apm" 27#define MODNAME "hp6x0_apm"
29 28
30static int hp6x0_apm_get_info(char *buf, char **start, off_t fpos, int length) 29static void hp6x0_apm_get_power_status(struct apm_power_info *info)
31{ 30{
31 int battery, backup, charging, percentage;
32 u8 pgdr; 32 u8 pgdr;
33 char *p;
34 int battery_status;
35 int battery_flag;
36 int ac_line_status;
37 int time_units = APM_BATTERY_LIFE_UNKNOWN;
38 33
39 int battery = adc_single(ADC_CHANNEL_BATTERY); 34 battery = adc_single(ADC_CHANNEL_BATTERY);
40 int backup = adc_single(ADC_CHANNEL_BACKUP); 35 backup = adc_single(ADC_CHANNEL_BACKUP);
41 int charging = adc_single(ADC_CHANNEL_CHARGE); 36 charging = adc_single(ADC_CHANNEL_CHARGE);
42 int percentage;
43 37
44 percentage = 100 * (battery - HP680_BATTERY_MIN) / 38 percentage = 100 * (battery - HP680_BATTERY_MIN) /
45 (HP680_BATTERY_MAX - HP680_BATTERY_MIN); 39 (HP680_BATTERY_MAX - HP680_BATTERY_MIN);
46 40
47 ac_line_status = (battery > HP680_BATTERY_AC_ON) ? 41 info->ac_line_status = (battery > HP680_BATTERY_AC_ON) ?
48 APM_AC_ONLINE : APM_AC_OFFLINE; 42 APM_AC_ONLINE : APM_AC_OFFLINE;
49 43
50 p = buf;
51
52 pgdr = ctrl_inb(SH7709_PGDR); 44 pgdr = ctrl_inb(SH7709_PGDR);
53 if (pgdr & PGDR_MAIN_BATTERY_OUT) { 45 if (pgdr & PGDR_MAIN_BATTERY_OUT) {
54 battery_status = APM_BATTERY_STATUS_NOT_PRESENT; 46 info->battery_status = APM_BATTERY_STATUS_NOT_PRESENT;
55 battery_flag = 0x80; 47 info->battery_flag = 0x80;
56 percentage = -1; 48 } else if (charging < 8) {
57 } else if (charging < 8 ) { 49 info->battery_status = APM_BATTERY_STATUS_CHARGING;
58 battery_status = APM_BATTERY_STATUS_CHARGING; 50 info->battery_flag = 0x08;
59 battery_flag = 0x08; 51 info->ac_line_status = 0xff;
60 ac_line_status = 0xff;
61 } else if (percentage <= APM_CRITICAL) { 52 } else if (percentage <= APM_CRITICAL) {
62 battery_status = APM_BATTERY_STATUS_CRITICAL; 53 info->battery_status = APM_BATTERY_STATUS_CRITICAL;
63 battery_flag = 0x04; 54 info->battery_flag = 0x04;
64 } else if (percentage <= APM_LOW) { 55 } else if (percentage <= APM_LOW) {
65 battery_status = APM_BATTERY_STATUS_LOW; 56 info->battery_status = APM_BATTERY_STATUS_LOW;
66 battery_flag = 0x02; 57 info->battery_flag = 0x02;
67 } else { 58 } else {
68 battery_status = APM_BATTERY_STATUS_HIGH; 59 info->battery_status = APM_BATTERY_STATUS_HIGH;
69 battery_flag = 0x01; 60 info->battery_flag = 0x01;
70 } 61 }
71 62
72 p += sprintf(p, "1.0 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n", 63 info->units = 0;
73 APM_32_BIT_SUPPORT,
74 ac_line_status,
75 battery_status,
76 battery_flag,
77 percentage,
78 time_units,
79 "min");
80 p += sprintf(p, "bat=%d backup=%d charge=%d\n",
81 battery, backup, charging);
82
83 return p - buf;
84} 64}
85 65
86static irqreturn_t hp6x0_apm_interrupt(int irq, void *dev) 66static irqreturn_t hp6x0_apm_interrupt(int irq, void *dev)
@@ -96,14 +76,14 @@ static int __init hp6x0_apm_init(void)
96 int ret; 76 int ret;
97 77
98 ret = request_irq(HP680_BTN_IRQ, hp6x0_apm_interrupt, 78 ret = request_irq(HP680_BTN_IRQ, hp6x0_apm_interrupt,
99 IRQF_DISABLED, MODNAME, 0); 79 IRQF_DISABLED, MODNAME, NULL);
100 if (unlikely(ret < 0)) { 80 if (unlikely(ret < 0)) {
101 printk(KERN_ERR MODNAME ": IRQ %d request failed\n", 81 printk(KERN_ERR MODNAME ": IRQ %d request failed\n",
102 HP680_BTN_IRQ); 82 HP680_BTN_IRQ);
103 return ret; 83 return ret;
104 } 84 }
105 85
106 apm_get_info = hp6x0_apm_get_info; 86 apm_get_power_status = hp6x0_apm_get_power_status;
107 87
108 return ret; 88 return ret;
109} 89}
@@ -111,7 +91,7 @@ static int __init hp6x0_apm_init(void)
111static void __exit hp6x0_apm_exit(void) 91static void __exit hp6x0_apm_exit(void)
112{ 92{
113 free_irq(HP680_BTN_IRQ, 0); 93 free_irq(HP680_BTN_IRQ, 0);
114 apm_get_info = 0; 94 apm_get_info = NULL;
115} 95}
116 96
117module_init(hp6x0_apm_init); 97module_init(hp6x0_apm_init);
diff --git a/arch/sh/kernel/Makefile b/arch/sh/kernel/Makefile
index 99c7e5249f7a..2f6d2bcb1c93 100644
--- a/arch/sh/kernel/Makefile
+++ b/arch/sh/kernel/Makefile
@@ -19,6 +19,5 @@ obj-$(CONFIG_SH_CPU_FREQ) += cpufreq.o
19obj-$(CONFIG_MODULES) += module.o 19obj-$(CONFIG_MODULES) += module.o
20obj-$(CONFIG_EARLY_PRINTK) += early_printk.o 20obj-$(CONFIG_EARLY_PRINTK) += early_printk.o
21obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o 21obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
22obj-$(CONFIG_APM) += apm.o
23obj-$(CONFIG_PM) += pm.o 22obj-$(CONFIG_PM) += pm.o
24obj-$(CONFIG_STACKTRACE) += stacktrace.o 23obj-$(CONFIG_STACKTRACE) += stacktrace.o
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
43struct 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 */
52struct 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 */
73static int suspends_pending;
74
75static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
76static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
77
78/*
79 * This is a list of everyone who has opened /dev/apm_bios
80 */
81static DECLARE_RWSEM(user_list_lock);
82static 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 */
89static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
90static DECLARE_COMPLETION(kapmd_exit);
91static DEFINE_SPINLOCK(kapmd_queue_lock);
92static struct apm_queue kapmd_queue;
93
94int apm_suspended;
95EXPORT_SYMBOL(apm_suspended);
96
97/* Platform-specific apm_read_proc(). */
98int (*apm_get_info)(char *buf, char **start, off_t fpos, int length);
99EXPORT_SYMBOL(apm_get_info);
100
101/*
102 * APM event queue management.
103 */
104static inline int queue_empty(struct apm_queue *q)
105{
106 return q->event_head == q->event_tail;
107}
108
109static 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
115static 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
129static 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
150static 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 */
176void 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}
184EXPORT_SYMBOL(apm_queue_event);
185
186static 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
214static 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
249static 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 */
267static int
268apm_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
336static 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
361static 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
388static 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
397static 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 */
443static 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
452static 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
497static 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
523static 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
533module_init(apm_init);
534module_exit(apm_exit);
535
536MODULE_AUTHOR("Stephen Rothwell, Andriy Skulysh");
537MODULE_DESCRIPTION("Advanced Power Management");
538MODULE_LICENSE("GPL");