indydog.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/watchdog.h>
  23. #include <linux/notifier.h>
  24. #include <linux/reboot.h>
  25. #include <linux/init.h>
  26. #include <linux/uaccess.h>
  27. #include <asm/sgi/mc.h>
  28. static unsigned long indydog_alive;
  29. static DEFINE_SPINLOCK(indydog_lock);
  30. #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
  31. static bool nowayout = WATCHDOG_NOWAYOUT;
  32. module_param(nowayout, bool, 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. spin_lock(&indydog_lock);
  39. sgimc->cpuctrl0 |= SGIMC_CCTRL0_WDOG;
  40. spin_unlock(&indydog_lock);
  41. }
  42. static void indydog_stop(void)
  43. {
  44. spin_lock(&indydog_lock);
  45. sgimc->cpuctrl0 &= ~SGIMC_CCTRL0_WDOG;
  46. spin_unlock(&indydog_lock);
  47. pr_info("Stopped watchdog timer\n");
  48. }
  49. static void indydog_ping(void)
  50. {
  51. sgimc->watchdogt = 0;
  52. }
  53. /*
  54. * Allow only one person to hold it open
  55. */
  56. static int indydog_open(struct inode *inode, struct file *file)
  57. {
  58. if (test_and_set_bit(0, &indydog_alive))
  59. return -EBUSY;
  60. if (nowayout)
  61. __module_get(THIS_MODULE);
  62. /* Activate timer */
  63. indydog_start();
  64. indydog_ping();
  65. pr_info("Started watchdog timer\n");
  66. return nonseekable_open(inode, file);
  67. }
  68. static int indydog_release(struct inode *inode, struct file *file)
  69. {
  70. /* Shut off the timer.
  71. * Lock it in if it's a module and we defined ...NOWAYOUT */
  72. if (!nowayout)
  73. indydog_stop(); /* Turn the WDT off */
  74. clear_bit(0, &indydog_alive);
  75. return 0;
  76. }
  77. static ssize_t indydog_write(struct file *file, const char *data,
  78. size_t len, loff_t *ppos)
  79. {
  80. /* Refresh the timer. */
  81. if (len)
  82. indydog_ping();
  83. return len;
  84. }
  85. static long indydog_ioctl(struct file *file, unsigned int cmd,
  86. unsigned long arg)
  87. {
  88. int options, retval = -EINVAL;
  89. static const struct watchdog_info ident = {
  90. .options = WDIOF_KEEPALIVEPING,
  91. .firmware_version = 0,
  92. .identity = "Hardware Watchdog for SGI IP22",
  93. };
  94. switch (cmd) {
  95. case WDIOC_GETSUPPORT:
  96. if (copy_to_user((struct watchdog_info *)arg,
  97. &ident, sizeof(ident)))
  98. return -EFAULT;
  99. return 0;
  100. case WDIOC_GETSTATUS:
  101. case WDIOC_GETBOOTSTATUS:
  102. return put_user(0, (int *)arg);
  103. case WDIOC_SETOPTIONS:
  104. {
  105. if (get_user(options, (int *)arg))
  106. return -EFAULT;
  107. if (options & WDIOS_DISABLECARD) {
  108. indydog_stop();
  109. retval = 0;
  110. }
  111. if (options & WDIOS_ENABLECARD) {
  112. indydog_start();
  113. retval = 0;
  114. }
  115. return retval;
  116. }
  117. case WDIOC_KEEPALIVE:
  118. indydog_ping();
  119. return 0;
  120. case WDIOC_GETTIMEOUT:
  121. return put_user(WATCHDOG_TIMEOUT, (int *)arg);
  122. default:
  123. return -ENOTTY;
  124. }
  125. }
  126. static int indydog_notify_sys(struct notifier_block *this,
  127. unsigned long code, void *unused)
  128. {
  129. if (code == SYS_DOWN || code == SYS_HALT)
  130. indydog_stop(); /* Turn the WDT off */
  131. return NOTIFY_DONE;
  132. }
  133. static const struct file_operations indydog_fops = {
  134. .owner = THIS_MODULE,
  135. .llseek = no_llseek,
  136. .write = indydog_write,
  137. .unlocked_ioctl = indydog_ioctl,
  138. .open = indydog_open,
  139. .release = indydog_release,
  140. };
  141. static struct miscdevice indydog_miscdev = {
  142. .minor = WATCHDOG_MINOR,
  143. .name = "watchdog",
  144. .fops = &indydog_fops,
  145. };
  146. static struct notifier_block indydog_notifier = {
  147. .notifier_call = indydog_notify_sys,
  148. };
  149. static int __init watchdog_init(void)
  150. {
  151. int ret;
  152. ret = register_reboot_notifier(&indydog_notifier);
  153. if (ret) {
  154. pr_err("cannot register reboot notifier (err=%d)\n", ret);
  155. return ret;
  156. }
  157. ret = misc_register(&indydog_miscdev);
  158. if (ret) {
  159. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  160. WATCHDOG_MINOR, ret);
  161. unregister_reboot_notifier(&indydog_notifier);
  162. return ret;
  163. }
  164. pr_info("Hardware Watchdog Timer for SGI IP22: 0.3\n");
  165. return 0;
  166. }
  167. static void __exit watchdog_exit(void)
  168. {
  169. misc_deregister(&indydog_miscdev);
  170. unregister_reboot_notifier(&indydog_notifier);
  171. }
  172. module_init(watchdog_init);
  173. module_exit(watchdog_exit);
  174. MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
  175. MODULE_DESCRIPTION("Hardware Watchdog Device for SGI IP22");
  176. MODULE_LICENSE("GPL");