indydog.c 4.4 KB

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