Merge branch 'upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/vitb/linux...
[linux-2.6] / arch / i386 / mach-voyager / voyager_thread.c
1 /* -*- mode: c; c-basic-offset: 8 -*- */
2
3 /* Copyright (C) 2001
4  *
5  * Author: J.E.J.Bottomley@HansenPartnership.com
6  *
7  * linux/arch/i386/kernel/voyager_thread.c
8  *
9  * This module provides the machine status monitor thread for the
10  * voyager architecture.  This allows us to monitor the machine
11  * environment (temp, voltage, fan function) and the front panel and
12  * internal UPS.  If a fault is detected, this thread takes corrective
13  * action (usually just informing init)
14  * */
15
16 #include <linux/module.h>
17 #include <linux/mm.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/delay.h>
20 #include <linux/mc146818rtc.h>
21 #include <linux/smp_lock.h>
22 #include <linux/init.h>
23 #include <linux/bootmem.h>
24 #include <linux/kmod.h>
25 #include <linux/completion.h>
26 #include <linux/sched.h>
27 #include <asm/desc.h>
28 #include <asm/voyager.h>
29 #include <asm/vic.h>
30 #include <asm/mtrr.h>
31 #include <asm/msr.h>
32
33 #define THREAD_NAME "kvoyagerd"
34
35 /* external variables */
36 int kvoyagerd_running = 0;
37 DECLARE_MUTEX_LOCKED(kvoyagerd_sem);
38
39 static int thread(void *);
40
41 static __u8 set_timeout = 0;
42
43 /* Start the machine monitor thread.  Return 1 if OK, 0 if fail */
44 static int __init
45 voyager_thread_start(void)
46 {
47         if(kernel_thread(thread, NULL, CLONE_KERNEL) < 0) {
48                 /* This is serious, but not fatal */
49                 printk(KERN_ERR "Voyager: Failed to create system monitor thread!!!\n");
50                 return 1;
51         }
52         return 0;
53 }
54
55 static int
56 execute(const char *string)
57 {
58         int ret;
59
60         char *envp[] = {
61                 "HOME=/",
62                 "TERM=linux",
63                 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
64                 NULL,
65         };
66         char *argv[] = {
67                 "/bin/bash",
68                 "-c",
69                 (char *)string,
70                 NULL,
71         };
72
73         if ((ret = call_usermodehelper(argv[0], argv, envp, 1)) != 0) {
74                 printk(KERN_ERR "Voyager failed to run \"%s\": %i\n",
75                        string, ret);
76         }
77         return ret;
78 }
79
80 static void
81 check_from_kernel(void)
82 {
83         if(voyager_status.switch_off) {
84                 
85                 /* FIXME: This should be configureable via proc */
86                 execute("umask 600; echo 0 > /etc/initrunlvl; kill -HUP 1");
87         } else if(voyager_status.power_fail) {
88                 VDEBUG(("Voyager daemon detected AC power failure\n"));
89                 
90                 /* FIXME: This should be configureable via proc */
91                 execute("umask 600; echo F > /etc/powerstatus; kill -PWR 1");
92                 set_timeout = 1;
93         }
94 }
95
96 static void
97 check_continuing_condition(void)
98 {
99         if(voyager_status.power_fail) {
100                 __u8 data;
101                 voyager_cat_psi(VOYAGER_PSI_SUBREAD, 
102                                 VOYAGER_PSI_AC_FAIL_REG, &data);
103                 if((data & 0x1f) == 0) {
104                         /* all power restored */
105                         printk(KERN_NOTICE "VOYAGER AC power restored, cancelling shutdown\n");
106                         /* FIXME: should be user configureable */
107                         execute("umask 600; echo O > /etc/powerstatus; kill -PWR 1");
108                         set_timeout = 0;
109                 }
110         }
111 }
112
113 static void
114 wakeup(unsigned long unused)
115 {
116         up(&kvoyagerd_sem);
117 }
118
119 static int
120 thread(void *unused)
121 {
122         struct timer_list wakeup_timer;
123
124         kvoyagerd_running = 1;
125
126         daemonize(THREAD_NAME);
127
128         set_timeout = 0;
129
130         init_timer(&wakeup_timer);
131
132         sigfillset(&current->blocked);
133
134         printk(KERN_NOTICE "Voyager starting monitor thread\n");
135
136         for(;;) {
137                 down_interruptible(&kvoyagerd_sem);
138                 VDEBUG(("Voyager Daemon awoken\n"));
139                 if(voyager_status.request_from_kernel == 0) {
140                         /* probably awoken from timeout */
141                         check_continuing_condition();
142                 } else {
143                         check_from_kernel();
144                         voyager_status.request_from_kernel = 0;
145                 }
146                 if(set_timeout) {
147                         del_timer(&wakeup_timer);
148                         wakeup_timer.expires = HZ + jiffies;
149                         wakeup_timer.function = wakeup;
150                         add_timer(&wakeup_timer);
151                 }
152         }
153 }
154
155 static void __exit
156 voyager_thread_stop(void)
157 {
158         /* FIXME: do nothing at the moment */
159 }
160
161 module_init(voyager_thread_start);
162 //module_exit(voyager_thread_stop);