blob: f9d5953381595bdef0df4671d1e8cd4cbdc05bbe [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/init.h>
22#include <linux/bootmem.h>
23#include <linux/kmod.h>
24#include <linux/completion.h>
25#include <linux/sched.h>
Christoph Hellwigf3402a42007-04-22 20:30:43 +010026#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Christoph Hellwigf3402a42007-04-22 20:30:43 +010034struct task_struct *voyager_thread;
35static __u8 set_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37static int
38execute(const char *string)
39{
40 int ret;
41
42 char *envp[] = {
43 "HOME=/",
44 "TERM=linux",
45 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
46 NULL,
47 };
48 char *argv[] = {
49 "/bin/bash",
50 "-c",
51 (char *)string,
52 NULL,
53 };
54
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -070055 if ((ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 printk(KERN_ERR "Voyager failed to run \"%s\": %i\n",
57 string, ret);
58 }
59 return ret;
60}
61
62static void
63check_from_kernel(void)
64{
65 if(voyager_status.switch_off) {
66
67 /* FIXME: This should be configureable via proc */
68 execute("umask 600; echo 0 > /etc/initrunlvl; kill -HUP 1");
69 } else if(voyager_status.power_fail) {
70 VDEBUG(("Voyager daemon detected AC power failure\n"));
71
72 /* FIXME: This should be configureable via proc */
73 execute("umask 600; echo F > /etc/powerstatus; kill -PWR 1");
74 set_timeout = 1;
75 }
76}
77
78static void
79check_continuing_condition(void)
80{
81 if(voyager_status.power_fail) {
82 __u8 data;
83 voyager_cat_psi(VOYAGER_PSI_SUBREAD,
84 VOYAGER_PSI_AC_FAIL_REG, &data);
85 if((data & 0x1f) == 0) {
86 /* all power restored */
87 printk(KERN_NOTICE "VOYAGER AC power restored, cancelling shutdown\n");
88 /* FIXME: should be user configureable */
89 execute("umask 600; echo O > /etc/powerstatus; kill -PWR 1");
90 set_timeout = 0;
91 }
92 }
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int
96thread(void *unused)
97{
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 printk(KERN_NOTICE "Voyager starting monitor thread\n");
99
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100100 for (;;) {
101 set_current_state(TASK_INTERRUPTIBLE);
102 schedule_timeout(set_timeout ? HZ : MAX_SCHEDULE_TIMEOUT);
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 VDEBUG(("Voyager Daemon awoken\n"));
105 if(voyager_status.request_from_kernel == 0) {
106 /* probably awoken from timeout */
107 check_continuing_condition();
108 } else {
109 check_from_kernel();
110 voyager_status.request_from_kernel = 0;
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113}
114
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100115static int __init
116voyager_thread_start(void)
117{
118 voyager_thread = kthread_run(thread, NULL, "kvoyagerd");
119 if (IS_ERR(voyager_thread)) {
120 printk(KERN_ERR "Voyager: Failed to create system monitor thread.\n");
121 return PTR_ERR(voyager_thread);
122 }
123 return 0;
124}
125
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static void __exit
128voyager_thread_stop(void)
129{
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100130 kthread_stop(voyager_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133module_init(voyager_thread_start);
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100134module_exit(voyager_thread_stop);