indydog.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * IndyDog 0.3 A Hardware Watchdog Device for SGI IP22
  3. *
  4. * (c) Copyright 2002 Guido Guenther <agx@sigxcpu.org>, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * based on softdog.c by Alan Cox <alan@redhat.com>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/fs.h>
  18. #include <linux/mm.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/watchdog.h>
  21. #include <linux/notifier.h>
  22. #include <linux/reboot.h>
  23. #include <linux/init.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/sgi/mc.h>
  26. #define PFX "indydog: "
  27. static int indydog_alive;
  28. #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
  29. static int nowayout = WATCHDOG_NOWAYOUT;
  30. module_param(nowayout, int, 0);
  31. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  32. static void indydog_start(void)
  33. {
  34. u32 mc_ctrl0 = sgimc->cpuctrl0;
  35. mc_ctrl0 = sgimc->cpuctrl0 | SGIMC_CCTRL0_WDOG;
  36. sgimc->cpuctrl0 = mc_ctrl0;
  37. }
  38. static void indydog_stop(void)
  39. {
  40. u32 mc_ctrl0 = sgimc->cpuctrl0;
  41. mc_ctrl0 &= ~SGIMC_CCTRL0_WDOG;
  42. sgimc->cpuctrl0 = mc_ctrl0;
  43. printk(KERN_INFO PFX "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 (indydog_alive)
  55. return -EBUSY;
  56. if (nowayout)
  57. __module_get(THIS_MODULE);
  58. /* Activate timer */
  59. indydog_start();
  60. indydog_ping();
  61. indydog_alive = 1;
  62. printk(KERN_INFO "Started watchdog timer.\n");
  63. return nonseekable_open(inode, file);
  64. }
  65. static int indydog_release(struct inode *inode, struct file *file)
  66. {
  67. /* Shut off the timer.
  68. * Lock it in if it's a module and we defined ...NOWAYOUT */
  69. if (!nowayout)
  70. indydog_stop(); /* Turn the WDT off */
  71. indydog_alive = 0;
  72. return 0;
  73. }
  74. static ssize_t indydog_write(struct file *file, const char *data, size_t len, loff_t *ppos)
  75. {
  76. /* Refresh the timer. */
  77. if (len) {
  78. indydog_ping();
  79. }
  80. return len;
  81. }
  82. static int indydog_ioctl(struct inode *inode, struct file *file,
  83. unsigned int cmd, unsigned long arg)
  84. {
  85. int options, retval = -EINVAL;
  86. static struct watchdog_info ident = {
  87. .options = WDIOF_KEEPALIVEPING |
  88. WDIOF_MAGICCLOSE,
  89. .firmware_version = 0,
  90. .identity = "Hardware Watchdog for SGI IP22",
  91. };
  92. switch (cmd) {
  93. default:
  94. return -ENOTTY;
  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_KEEPALIVE:
  104. indydog_ping();
  105. return 0;
  106. case WDIOC_GETTIMEOUT:
  107. return put_user(WATCHDOG_TIMEOUT,(int *)arg);
  108. case WDIOC_SETOPTIONS:
  109. {
  110. if (get_user(options, (int *)arg))
  111. return -EFAULT;
  112. if (options & WDIOS_DISABLECARD) {
  113. indydog_stop();
  114. retval = 0;
  115. }
  116. if (options & WDIOS_ENABLECARD) {
  117. indydog_start();
  118. retval = 0;
  119. }
  120. return retval;
  121. }
  122. }
  123. }
  124. static int indydog_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
  125. {
  126. if (code == SYS_DOWN || code == SYS_HALT)
  127. indydog_stop(); /* Turn the WDT off */
  128. return NOTIFY_DONE;
  129. }
  130. static const struct file_operations indydog_fops = {
  131. .owner = THIS_MODULE,
  132. .llseek = no_llseek,
  133. .write = indydog_write,
  134. .ioctl = indydog_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 char banner[] __initdata =
  147. KERN_INFO PFX "Hardware Watchdog Timer for SGI IP22: 0.3\n";
  148. static int __init watchdog_init(void)
  149. {
  150. int ret;
  151. ret = register_reboot_notifier(&indydog_notifier);
  152. if (ret) {
  153. printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  154. ret);
  155. return ret;
  156. }
  157. ret = misc_register(&indydog_miscdev);
  158. if (ret) {
  159. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  160. WATCHDOG_MINOR, ret);
  161. unregister_reboot_notifier(&indydog_notifier);
  162. return ret;
  163. }
  164. printk(banner);
  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");
  177. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);