voyager_thread.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* -*- mode: c; c-basic-offset: 8 -*- */
  2. /* Copyright (C) 2001
  3. *
  4. * Author: J.E.J.Bottomley@HansenPartnership.com
  5. *
  6. * This module provides the machine status monitor thread for the
  7. * voyager architecture. This allows us to monitor the machine
  8. * environment (temp, voltage, fan function) and the front panel and
  9. * internal UPS. If a fault is detected, this thread takes corrective
  10. * action (usually just informing init)
  11. * */
  12. #include <linux/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/kernel_stat.h>
  15. #include <linux/delay.h>
  16. #include <linux/mc146818rtc.h>
  17. #include <linux/init.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/kmod.h>
  20. #include <linux/completion.h>
  21. #include <linux/sched.h>
  22. #include <linux/kthread.h>
  23. #include <asm/desc.h>
  24. #include <asm/voyager.h>
  25. #include <asm/vic.h>
  26. #include <asm/mtrr.h>
  27. #include <asm/msr.h>
  28. struct task_struct *voyager_thread;
  29. static __u8 set_timeout;
  30. static int execute(const char *string)
  31. {
  32. int ret;
  33. char *envp[] = {
  34. "HOME=/",
  35. "TERM=linux",
  36. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  37. NULL,
  38. };
  39. char *argv[] = {
  40. "/bin/bash",
  41. "-c",
  42. (char *)string,
  43. NULL,
  44. };
  45. if ((ret =
  46. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC)) != 0) {
  47. printk(KERN_ERR "Voyager failed to run \"%s\": %i\n", string,
  48. ret);
  49. }
  50. return ret;
  51. }
  52. static void check_from_kernel(void)
  53. {
  54. if (voyager_status.switch_off) {
  55. /* FIXME: This should be configurable via proc */
  56. execute("umask 600; echo 0 > /etc/initrunlvl; kill -HUP 1");
  57. } else if (voyager_status.power_fail) {
  58. VDEBUG(("Voyager daemon detected AC power failure\n"));
  59. /* FIXME: This should be configureable via proc */
  60. execute("umask 600; echo F > /etc/powerstatus; kill -PWR 1");
  61. set_timeout = 1;
  62. }
  63. }
  64. static void check_continuing_condition(void)
  65. {
  66. if (voyager_status.power_fail) {
  67. __u8 data;
  68. voyager_cat_psi(VOYAGER_PSI_SUBREAD,
  69. VOYAGER_PSI_AC_FAIL_REG, &data);
  70. if ((data & 0x1f) == 0) {
  71. /* all power restored */
  72. printk(KERN_NOTICE
  73. "VOYAGER AC power restored, cancelling shutdown\n");
  74. /* FIXME: should be user configureable */
  75. execute
  76. ("umask 600; echo O > /etc/powerstatus; kill -PWR 1");
  77. set_timeout = 0;
  78. }
  79. }
  80. }
  81. static int thread(void *unused)
  82. {
  83. printk(KERN_NOTICE "Voyager starting monitor thread\n");
  84. for (;;) {
  85. set_current_state(TASK_INTERRUPTIBLE);
  86. schedule_timeout(set_timeout ? HZ : MAX_SCHEDULE_TIMEOUT);
  87. VDEBUG(("Voyager Daemon awoken\n"));
  88. if (voyager_status.request_from_kernel == 0) {
  89. /* probably awoken from timeout */
  90. check_continuing_condition();
  91. } else {
  92. check_from_kernel();
  93. voyager_status.request_from_kernel = 0;
  94. }
  95. }
  96. }
  97. static int __init voyager_thread_start(void)
  98. {
  99. voyager_thread = kthread_run(thread, NULL, "kvoyagerd");
  100. if (IS_ERR(voyager_thread)) {
  101. printk(KERN_ERR
  102. "Voyager: Failed to create system monitor thread.\n");
  103. return PTR_ERR(voyager_thread);
  104. }
  105. return 0;
  106. }
  107. static void __exit voyager_thread_stop(void)
  108. {
  109. kthread_stop(voyager_thread);
  110. }
  111. module_init(voyager_thread_start);
  112. module_exit(voyager_thread_stop);