blob: fdc1d926fb2a2dccf3ab0127b738db6997665470 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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>
Christoph Hellwigf3402a42007-04-22 20:30:43 +010027#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/desc.h>
29#include <asm/voyager.h>
30#include <asm/vic.h>
31#include <asm/mtrr.h>
32#include <asm/msr.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Christoph Hellwigf3402a42007-04-22 20:30:43 +010035struct task_struct *voyager_thread;
36static __u8 set_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38static int
39execute(const char *string)
40{
41 int ret;
42
43 char *envp[] = {
44 "HOME=/",
45 "TERM=linux",
46 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
47 NULL,
48 };
49 char *argv[] = {
50 "/bin/bash",
51 "-c",
52 (char *)string,
53 NULL,
54 };
55
56 if ((ret = call_usermodehelper(argv[0], argv, envp, 1)) != 0) {
57 printk(KERN_ERR "Voyager failed to run \"%s\": %i\n",
58 string, ret);
59 }
60 return ret;
61}
62
63static void
64check_from_kernel(void)
65{
66 if(voyager_status.switch_off) {
67
68 /* FIXME: This should be configureable via proc */
69 execute("umask 600; echo 0 > /etc/initrunlvl; kill -HUP 1");
70 } else if(voyager_status.power_fail) {
71 VDEBUG(("Voyager daemon detected AC power failure\n"));
72
73 /* FIXME: This should be configureable via proc */
74 execute("umask 600; echo F > /etc/powerstatus; kill -PWR 1");
75 set_timeout = 1;
76 }
77}
78
79static void
80check_continuing_condition(void)
81{
82 if(voyager_status.power_fail) {
83 __u8 data;
84 voyager_cat_psi(VOYAGER_PSI_SUBREAD,
85 VOYAGER_PSI_AC_FAIL_REG, &data);
86 if((data & 0x1f) == 0) {
87 /* all power restored */
88 printk(KERN_NOTICE "VOYAGER AC power restored, cancelling shutdown\n");
89 /* FIXME: should be user configureable */
90 execute("umask 600; echo O > /etc/powerstatus; kill -PWR 1");
91 set_timeout = 0;
92 }
93 }
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int
97thread(void *unused)
98{
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 printk(KERN_NOTICE "Voyager starting monitor thread\n");
100
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100101 for (;;) {
102 set_current_state(TASK_INTERRUPTIBLE);
103 schedule_timeout(set_timeout ? HZ : MAX_SCHEDULE_TIMEOUT);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 VDEBUG(("Voyager Daemon awoken\n"));
106 if(voyager_status.request_from_kernel == 0) {
107 /* probably awoken from timeout */
108 check_continuing_condition();
109 } else {
110 check_from_kernel();
111 voyager_status.request_from_kernel = 0;
112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114}
115
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100116static int __init
117voyager_thread_start(void)
118{
119 voyager_thread = kthread_run(thread, NULL, "kvoyagerd");
120 if (IS_ERR(voyager_thread)) {
121 printk(KERN_ERR "Voyager: Failed to create system monitor thread.\n");
122 return PTR_ERR(voyager_thread);
123 }
124 return 0;
125}
126
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static void __exit
129voyager_thread_stop(void)
130{
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100131 kthread_stop(voyager_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134module_init(voyager_thread_start);
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100135module_exit(voyager_thread_stop);