indydog.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. printk(KERN_INFO "Started watchdog timer.\n");
  72. return nonseekable_open(inode, file);
  73. }
  74. static int indydog_release(struct inode *inode, struct file *file)
  75. {
  76. /* Shut off the timer.
  77. * Lock it in if it's a module and we defined ...NOWAYOUT */
  78. if (!nowayout)
  79. indydog_stop(); /* Turn the WDT off */
  80. clear_bit(0, &indydog_alive);
  81. return 0;
  82. }
  83. static ssize_t indydog_write(struct file *file, const char *data,
  84. size_t len, loff_t *ppos)
  85. {
  86. /* Refresh the timer. */
  87. if (len)
  88. indydog_ping();
  89. return len;
  90. }
  91. static long indydog_ioctl(struct file *file, unsigned int cmd,
  92. unsigned long arg)
  93. {
  94. int options, retval = -EINVAL;
  95. static const struct watchdog_info ident = {
  96. .options = WDIOF_KEEPALIVEPING,
  97. .firmware_version = 0,
  98. .identity = "Hardware Watchdog for SGI IP22",
  99. };
  100. switch (cmd) {
  101. case WDIOC_GETSUPPORT:
  102. if (copy_to_user((struct watchdog_info *)arg,
  103. &ident, sizeof(ident)))
  104. return -EFAULT;
  105. return 0;
  106. case WDIOC_GETSTATUS:
  107. case WDIOC_GETBOOTSTATUS:
  108. return put_user(0, (int *)arg);
  109. case WDIOC_SETOPTIONS:
  110. {
  111. if (get_user(options, (int *)arg))
  112. return -EFAULT;
  113. if (options & WDIOS_DISABLECARD) {
  114. indydog_stop();
  115. retval = 0;
  116. }
  117. if (options & WDIOS_ENABLECARD) {
  118. indydog_start();
  119. retval = 0;
  120. }
  121. return retval;
  122. }
  123. case WDIOC_KEEPALIVE:
  124. indydog_ping();
  125. return 0;
  126. case WDIOC_GETTIMEOUT:
  127. return put_user(WATCHDOG_TIMEOUT, (int *)arg);
  128. default:
  129. return -ENOTTY;
  130. }
  131. }
  132. static int indydog_notify_sys(struct notifier_block *this,
  133. unsigned long code, void *unused)
  134. {
  135. if (code == SYS_DOWN || code == SYS_HALT)
  136. indydog_stop(); /* Turn the WDT off */
  137. return NOTIFY_DONE;
  138. }
  139. static const struct file_operations indydog_fops = {
  140. .owner = THIS_MODULE,
  141. .llseek = no_llseek,
  142. .write = indydog_write,
  143. .unlocked_ioctl = indydog_ioctl,
  144. .open = indydog_open,
  145. .release = indydog_release,
  146. };
  147. static struct miscdevice indydog_miscdev = {
  148. .minor = WATCHDOG_MINOR,
  149. .name = "watchdog",
  150. .fops = &indydog_fops,
  151. };
  152. static struct notifier_block indydog_notifier = {
  153. .notifier_call = indydog_notify_sys,
  154. };
  155. static char banner[] __initdata =
  156. KERN_INFO PFX "Hardware Watchdog Timer for SGI IP22: 0.3\n";
  157. static int __init watchdog_init(void)
  158. {
  159. int ret;
  160. spin_lock_init(&indydog_lock);
  161. ret = register_reboot_notifier(&indydog_notifier);
  162. if (ret) {
  163. printk(KERN_ERR PFX
  164. "cannot register reboot notifier (err=%d)\n", ret);
  165. return ret;
  166. }
  167. ret = misc_register(&indydog_miscdev);
  168. if (ret) {
  169. printk(KERN_ERR PFX
  170. "cannot register miscdev on minor=%d (err=%d)\n",
  171. WATCHDOG_MINOR, ret);
  172. unregister_reboot_notifier(&indydog_notifier);
  173. return ret;
  174. }
  175. printk(banner);
  176. return 0;
  177. }
  178. static void __exit watchdog_exit(void)
  179. {
  180. misc_deregister(&indydog_miscdev);
  181. unregister_reboot_notifier(&indydog_notifier);
  182. }
  183. module_init(watchdog_init);
  184. module_exit(watchdog_exit);
  185. MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
  186. MODULE_DESCRIPTION("Hardware Watchdog Device for SGI IP22");
  187. MODULE_LICENSE("GPL");
  188. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);