indydog.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * IndyDog 0.3 A Hardware Watchdog Device for SGI IP22
  3. *
  4. * (c) Copyright 2002 Guido Guenther <agx@sigxcpu.org>,
  5. * All Rights Reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * based on softdog.c by Alan Cox <alan@lxorguk.ukuu.org.uk>
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/fs.h>
  19. #include <linux/mm.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/watchdog.h>
  22. #include <linux/notifier.h>
  23. #include <linux/reboot.h>
  24. #include <linux/init.h>
  25. #include <linux/uaccess.h>
  26. #include <asm/sgi/mc.h>
  27. #define PFX "indydog: "
  28. static unsigned long indydog_alive;
  29. static spinlock_t indydog_lock;
  30. #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
  31. static int nowayout = WATCHDOG_NOWAYOUT;
  32. module_param(nowayout, int, 0);
  33. MODULE_PARM_DESC(nowayout,
  34. "Watchdog cannot be stopped once started (default="
  35. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  36. static void indydog_start(void)
  37. {
  38. u32 mc_ctrl0;
  39. spin_lock(&indydog_lock);
  40. mc_ctrl0 = sgimc->cpuctrl0;
  41. mc_ctrl0 = sgimc->cpuctrl0 | SGIMC_CCTRL0_WDOG;
  42. sgimc->cpuctrl0 = mc_ctrl0;
  43. spin_unlock(&indydog_lock);
  44. }
  45. static void indydog_stop(void)
  46. {
  47. u32 mc_ctrl0;
  48. spin_lock(&indydog_lock);
  49. mc_ctrl0 = sgimc->cpuctrl0;
  50. mc_ctrl0 &= ~SGIMC_CCTRL0_WDOG;
  51. sgimc->cpuctrl0 = mc_ctrl0;
  52. spin_unlock(&indydog_lock);
  53. printk(KERN_INFO PFX "Stopped watchdog timer.\n");
  54. }
  55. static void indydog_ping(void)
  56. {
  57. sgimc->watchdogt = 0;
  58. }
  59. /*
  60. * Allow only one person to hold it open
  61. */
  62. static int indydog_open(struct inode *inode, struct file *file)
  63. {
  64. if (test_and_set_bit(0, &indydog_alive))
  65. return -EBUSY;
  66. if (nowayout)
  67. __module_get(THIS_MODULE);
  68. /* Activate timer */
  69. indydog_start();
  70. indydog_ping();
  71. indydog_alive = 1;
  72. printk(KERN_INFO "Started watchdog timer.\n");
  73. return nonseekable_open(inode, file);
  74. }
  75. static int indydog_release(struct inode *inode, struct file *file)
  76. {
  77. /* Shut off the timer.
  78. * Lock it in if it's a module and we defined ...NOWAYOUT */
  79. if (!nowayout)
  80. indydog_stop(); /* Turn the WDT off */
  81. clear_bit(0, &indydog_alive);
  82. return 0;
  83. }
  84. static ssize_t indydog_write(struct file *file, const char *data,
  85. size_t len, loff_t *ppos)
  86. {
  87. /* Refresh the timer. */
  88. if (len)
  89. indydog_ping();
  90. return len;
  91. }
  92. static long indydog_ioctl(struct file *file, unsigned int cmd,
  93. unsigned long arg)
  94. {
  95. int options, retval = -EINVAL;
  96. static struct watchdog_info ident = {
  97. .options = WDIOF_KEEPALIVEPING |
  98. WDIOF_MAGICCLOSE,
  99. .firmware_version = 0,
  100. .identity = "Hardware Watchdog for SGI IP22",
  101. };
  102. switch (cmd) {
  103. case WDIOC_GETSUPPORT:
  104. if (copy_to_user((struct watchdog_info *)arg,
  105. &ident, sizeof(ident)))
  106. return -EFAULT;
  107. return 0;
  108. case WDIOC_GETSTATUS:
  109. case WDIOC_GETBOOTSTATUS:
  110. return put_user(0, (int *)arg);
  111. case WDIOC_SETOPTIONS:
  112. {
  113. if (get_user(options, (int *)arg))
  114. return -EFAULT;
  115. if (options & WDIOS_DISABLECARD) {
  116. indydog_stop();
  117. retval = 0;
  118. }
  119. if (options & WDIOS_ENABLECARD) {
  120. indydog_start();
  121. retval = 0;
  122. }
  123. return retval;
  124. }
  125. case WDIOC_KEEPALIVE:
  126. indydog_ping();
  127. return 0;
  128. case WDIOC_GETTIMEOUT:
  129. return put_user(WATCHDOG_TIMEOUT, (int *)arg);
  130. default:
  131. return -ENOTTY;
  132. }
  133. }
  134. static int indydog_notify_sys(struct notifier_block *this,
  135. unsigned long code, void *unused)
  136. {
  137. if (code == SYS_DOWN || code == SYS_HALT)
  138. indydog_stop(); /* Turn the WDT off */
  139. return NOTIFY_DONE;
  140. }
  141. static const struct file_operations indydog_fops = {
  142. .owner = THIS_MODULE,
  143. .llseek = no_llseek,
  144. .write = indydog_write,
  145. .unlocked_ioctl = indydog_ioctl,
  146. .open = indydog_open,
  147. .release = indydog_release,
  148. };
  149. static struct miscdevice indydog_miscdev = {
  150. .minor = WATCHDOG_MINOR,
  151. .name = "watchdog",
  152. .fops = &indydog_fops,
  153. };
  154. static struct notifier_block indydog_notifier = {
  155. .notifier_call = indydog_notify_sys,
  156. };
  157. static char banner[] __initdata =
  158. KERN_INFO PFX "Hardware Watchdog Timer for SGI IP22: 0.3\n";
  159. static int __init watchdog_init(void)
  160. {
  161. int ret;
  162. spin_lock_init(&indydog_lock);
  163. ret = register_reboot_notifier(&indydog_notifier);
  164. if (ret) {
  165. printk(KERN_ERR PFX
  166. "cannot register reboot notifier (err=%d)\n", ret);
  167. return ret;
  168. }
  169. ret = misc_register(&indydog_miscdev);
  170. if (ret) {
  171. printk(KERN_ERR PFX
  172. "cannot register miscdev on minor=%d (err=%d)\n",
  173. WATCHDOG_MINOR, ret);
  174. unregister_reboot_notifier(&indydog_notifier);
  175. return ret;
  176. }
  177. printk(banner);
  178. return 0;
  179. }
  180. static void __exit watchdog_exit(void)
  181. {
  182. misc_deregister(&indydog_miscdev);
  183. unregister_reboot_notifier(&indydog_notifier);
  184. }
  185. module_init(watchdog_init);
  186. module_exit(watchdog_exit);
  187. MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
  188. MODULE_DESCRIPTION("Hardware Watchdog Device for SGI IP22");
  189. MODULE_LICENSE("GPL");
  190. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);