indydog.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/config.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 <asm/uaccess.h>
  26. #include <asm/sgi/mc.h>
  27. #define PFX "indydog: "
  28. static int indydog_alive;
  29. #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
  30. static int nowayout = WATCHDOG_NOWAYOUT;
  31. module_param(nowayout, int, 0);
  32. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  33. static void indydog_start(void)
  34. {
  35. u32 mc_ctrl0 = sgimc->cpuctrl0;
  36. mc_ctrl0 = sgimc->cpuctrl0 | SGIMC_CCTRL0_WDOG;
  37. sgimc->cpuctrl0 = mc_ctrl0;
  38. }
  39. static void indydog_stop(void)
  40. {
  41. u32 mc_ctrl0 = sgimc->cpuctrl0;
  42. mc_ctrl0 &= ~SGIMC_CCTRL0_WDOG;
  43. sgimc->cpuctrl0 = mc_ctrl0;
  44. printk(KERN_INFO PFX "Stopped watchdog timer.\n");
  45. }
  46. static void indydog_ping(void)
  47. {
  48. sgimc->watchdogt = 0;
  49. }
  50. /*
  51. * Allow only one person to hold it open
  52. */
  53. static int indydog_open(struct inode *inode, struct file *file)
  54. {
  55. if (indydog_alive)
  56. return -EBUSY;
  57. if (nowayout)
  58. __module_get(THIS_MODULE);
  59. /* Activate timer */
  60. indydog_start();
  61. indydog_ping();
  62. indydog_alive = 1;
  63. printk(KERN_INFO "Started watchdog timer.\n");
  64. return nonseekable_open(inode, file);
  65. }
  66. static int indydog_release(struct inode *inode, struct file *file)
  67. {
  68. /* Shut off the timer.
  69. * Lock it in if it's a module and we defined ...NOWAYOUT */
  70. if (!nowayout)
  71. indydog_stop(); /* Turn the WDT off */
  72. indydog_alive = 0;
  73. return 0;
  74. }
  75. static ssize_t indydog_write(struct file *file, const char *data, size_t len, loff_t *ppos)
  76. {
  77. /* Refresh the timer. */
  78. if (len) {
  79. indydog_ping();
  80. }
  81. return len;
  82. }
  83. static int indydog_ioctl(struct inode *inode, struct file *file,
  84. unsigned int cmd, unsigned long arg)
  85. {
  86. int options, retval = -EINVAL;
  87. static struct watchdog_info ident = {
  88. .options = WDIOF_KEEPALIVEPING |
  89. WDIOF_MAGICCLOSE,
  90. .firmware_version = 0,
  91. .identity = "Hardware Watchdog for SGI IP22",
  92. };
  93. switch (cmd) {
  94. default:
  95. return -ENOIOCTLCMD;
  96. case WDIOC_GETSUPPORT:
  97. if (copy_to_user((struct watchdog_info *)arg,
  98. &ident, sizeof(ident)))
  99. return -EFAULT;
  100. return 0;
  101. case WDIOC_GETSTATUS:
  102. case WDIOC_GETBOOTSTATUS:
  103. return put_user(0,(int *)arg);
  104. case WDIOC_KEEPALIVE:
  105. indydog_ping();
  106. return 0;
  107. case WDIOC_GETTIMEOUT:
  108. return put_user(WATCHDOG_TIMEOUT,(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. }
  124. }
  125. static int indydog_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
  126. {
  127. if (code == SYS_DOWN || code == SYS_HALT)
  128. indydog_stop(); /* Turn the WDT off */
  129. return NOTIFY_DONE;
  130. }
  131. static struct file_operations indydog_fops = {
  132. .owner = THIS_MODULE,
  133. .llseek = no_llseek,
  134. .write = indydog_write,
  135. .ioctl = indydog_ioctl,
  136. .open = indydog_open,
  137. .release = indydog_release,
  138. };
  139. static struct miscdevice indydog_miscdev = {
  140. .minor = WATCHDOG_MINOR,
  141. .name = "watchdog",
  142. .fops = &indydog_fops,
  143. };
  144. static struct notifier_block indydog_notifier = {
  145. .notifier_call = indydog_notify_sys,
  146. };
  147. static char banner[] __initdata =
  148. KERN_INFO PFX "Hardware Watchdog Timer for SGI IP22: 0.3\n";
  149. static int __init watchdog_init(void)
  150. {
  151. int ret;
  152. ret = register_reboot_notifier(&indydog_notifier);
  153. if (ret) {
  154. printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  155. ret);
  156. return ret;
  157. }
  158. ret = misc_register(&indydog_miscdev);
  159. if (ret) {
  160. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  161. WATCHDOG_MINOR, ret);
  162. unregister_reboot_notifier(&indydog_notifier);
  163. return ret;
  164. }
  165. printk(banner);
  166. return 0;
  167. }
  168. static void __exit watchdog_exit(void)
  169. {
  170. misc_deregister(&indydog_miscdev);
  171. unregister_reboot_notifier(&indydog_notifier);
  172. }
  173. module_init(watchdog_init);
  174. module_exit(watchdog_exit);
  175. MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
  176. MODULE_DESCRIPTION("Hardware Watchdog Device for SGI IP22");
  177. MODULE_LICENSE("GPL");
  178. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);