cpu5wdt.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * sma cpu5 watchdog driver
  3. *
  4. * Copyright (C) 2003 Heiko Ronsdorf <hero@ihg.uni-duisburg.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/types.h>
  25. #include <linux/errno.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/fs.h>
  28. #include <linux/ioport.h>
  29. #include <linux/timer.h>
  30. #include <linux/completion.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/io.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/watchdog.h>
  35. /* adjustable parameters */
  36. static int verbose;
  37. static int port = 0x91;
  38. static int ticks = 10000;
  39. static DEFINE_SPINLOCK(cpu5wdt_lock);
  40. #define PFX "cpu5wdt: "
  41. #define CPU5WDT_EXTENT 0x0A
  42. #define CPU5WDT_STATUS_REG 0x00
  43. #define CPU5WDT_TIME_A_REG 0x02
  44. #define CPU5WDT_TIME_B_REG 0x03
  45. #define CPU5WDT_MODE_REG 0x04
  46. #define CPU5WDT_TRIGGER_REG 0x07
  47. #define CPU5WDT_ENABLE_REG 0x08
  48. #define CPU5WDT_RESET_REG 0x09
  49. #define CPU5WDT_INTERVAL (HZ/10+1)
  50. /* some device data */
  51. static struct {
  52. struct completion stop;
  53. int running;
  54. struct timer_list timer;
  55. int queue;
  56. int default_ticks;
  57. unsigned long inuse;
  58. } cpu5wdt_device;
  59. /* generic helper functions */
  60. static void cpu5wdt_trigger(unsigned long unused)
  61. {
  62. if (verbose > 2)
  63. pr_debug("trigger at %i ticks\n", ticks);
  64. if (cpu5wdt_device.running)
  65. ticks--;
  66. spin_lock(&cpu5wdt_lock);
  67. /* keep watchdog alive */
  68. outb(1, port + CPU5WDT_TRIGGER_REG);
  69. /* requeue?? */
  70. if (cpu5wdt_device.queue && ticks)
  71. mod_timer(&cpu5wdt_device.timer, jiffies + CPU5WDT_INTERVAL);
  72. else {
  73. /* ticks doesn't matter anyway */
  74. complete(&cpu5wdt_device.stop);
  75. }
  76. spin_unlock(&cpu5wdt_lock);
  77. }
  78. static void cpu5wdt_reset(void)
  79. {
  80. ticks = cpu5wdt_device.default_ticks;
  81. if (verbose)
  82. pr_debug("reset (%i ticks)\n", (int) ticks);
  83. }
  84. static void cpu5wdt_start(void)
  85. {
  86. unsigned long flags;
  87. spin_lock_irqsave(&cpu5wdt_lock, flags);
  88. if (!cpu5wdt_device.queue) {
  89. cpu5wdt_device.queue = 1;
  90. outb(0, port + CPU5WDT_TIME_A_REG);
  91. outb(0, port + CPU5WDT_TIME_B_REG);
  92. outb(1, port + CPU5WDT_MODE_REG);
  93. outb(0, port + CPU5WDT_RESET_REG);
  94. outb(0, port + CPU5WDT_ENABLE_REG);
  95. mod_timer(&cpu5wdt_device.timer, jiffies + CPU5WDT_INTERVAL);
  96. }
  97. /* if process dies, counter is not decremented */
  98. cpu5wdt_device.running++;
  99. spin_unlock_irqrestore(&cpu5wdt_lock, flags);
  100. }
  101. static int cpu5wdt_stop(void)
  102. {
  103. unsigned long flags;
  104. spin_lock_irqsave(&cpu5wdt_lock, flags);
  105. if (cpu5wdt_device.running)
  106. cpu5wdt_device.running = 0;
  107. ticks = cpu5wdt_device.default_ticks;
  108. spin_unlock_irqrestore(&cpu5wdt_lock, flags);
  109. if (verbose)
  110. pr_crit("stop not possible\n");
  111. return -EIO;
  112. }
  113. /* filesystem operations */
  114. static int cpu5wdt_open(struct inode *inode, struct file *file)
  115. {
  116. if (test_and_set_bit(0, &cpu5wdt_device.inuse))
  117. return -EBUSY;
  118. return nonseekable_open(inode, file);
  119. }
  120. static int cpu5wdt_release(struct inode *inode, struct file *file)
  121. {
  122. clear_bit(0, &cpu5wdt_device.inuse);
  123. return 0;
  124. }
  125. static long cpu5wdt_ioctl(struct file *file, unsigned int cmd,
  126. unsigned long arg)
  127. {
  128. void __user *argp = (void __user *)arg;
  129. int __user *p = argp;
  130. unsigned int value;
  131. static const struct watchdog_info ident = {
  132. .options = WDIOF_CARDRESET,
  133. .identity = "CPU5 WDT",
  134. };
  135. switch (cmd) {
  136. case WDIOC_GETSUPPORT:
  137. if (copy_to_user(argp, &ident, sizeof(ident)))
  138. return -EFAULT;
  139. break;
  140. case WDIOC_GETSTATUS:
  141. value = inb(port + CPU5WDT_STATUS_REG);
  142. value = (value >> 2) & 1;
  143. return put_user(value, p);
  144. case WDIOC_GETBOOTSTATUS:
  145. return put_user(0, p);
  146. case WDIOC_SETOPTIONS:
  147. if (get_user(value, p))
  148. return -EFAULT;
  149. if (value & WDIOS_ENABLECARD)
  150. cpu5wdt_start();
  151. if (value & WDIOS_DISABLECARD)
  152. cpu5wdt_stop();
  153. break;
  154. case WDIOC_KEEPALIVE:
  155. cpu5wdt_reset();
  156. break;
  157. default:
  158. return -ENOTTY;
  159. }
  160. return 0;
  161. }
  162. static ssize_t cpu5wdt_write(struct file *file, const char __user *buf,
  163. size_t count, loff_t *ppos)
  164. {
  165. if (!count)
  166. return -EIO;
  167. cpu5wdt_reset();
  168. return count;
  169. }
  170. static const struct file_operations cpu5wdt_fops = {
  171. .owner = THIS_MODULE,
  172. .llseek = no_llseek,
  173. .unlocked_ioctl = cpu5wdt_ioctl,
  174. .open = cpu5wdt_open,
  175. .write = cpu5wdt_write,
  176. .release = cpu5wdt_release,
  177. };
  178. static struct miscdevice cpu5wdt_misc = {
  179. .minor = WATCHDOG_MINOR,
  180. .name = "watchdog",
  181. .fops = &cpu5wdt_fops,
  182. };
  183. /* init/exit function */
  184. static int cpu5wdt_init(void)
  185. {
  186. unsigned int val;
  187. int err;
  188. if (verbose)
  189. pr_debug("port=0x%x, verbose=%i\n", port, verbose);
  190. init_completion(&cpu5wdt_device.stop);
  191. cpu5wdt_device.queue = 0;
  192. setup_timer(&cpu5wdt_device.timer, cpu5wdt_trigger, 0);
  193. cpu5wdt_device.default_ticks = ticks;
  194. if (!request_region(port, CPU5WDT_EXTENT, PFX)) {
  195. pr_err("request_region failed\n");
  196. err = -EBUSY;
  197. goto no_port;
  198. }
  199. /* watchdog reboot? */
  200. val = inb(port + CPU5WDT_STATUS_REG);
  201. val = (val >> 2) & 1;
  202. if (!val)
  203. pr_info("sorry, was my fault\n");
  204. err = misc_register(&cpu5wdt_misc);
  205. if (err < 0) {
  206. pr_err("misc_register failed\n");
  207. goto no_misc;
  208. }
  209. pr_info("init success\n");
  210. return 0;
  211. no_misc:
  212. release_region(port, CPU5WDT_EXTENT);
  213. no_port:
  214. return err;
  215. }
  216. static int cpu5wdt_init_module(void)
  217. {
  218. return cpu5wdt_init();
  219. }
  220. static void cpu5wdt_exit(void)
  221. {
  222. if (cpu5wdt_device.queue) {
  223. cpu5wdt_device.queue = 0;
  224. wait_for_completion(&cpu5wdt_device.stop);
  225. del_timer(&cpu5wdt_device.timer);
  226. }
  227. misc_deregister(&cpu5wdt_misc);
  228. release_region(port, CPU5WDT_EXTENT);
  229. }
  230. static void cpu5wdt_exit_module(void)
  231. {
  232. cpu5wdt_exit();
  233. }
  234. /* module entry points */
  235. module_init(cpu5wdt_init_module);
  236. module_exit(cpu5wdt_exit_module);
  237. MODULE_AUTHOR("Heiko Ronsdorf <hero@ihg.uni-duisburg.de>");
  238. MODULE_DESCRIPTION("sma cpu5 watchdog driver");
  239. MODULE_SUPPORTED_DEVICE("sma cpu5 watchdog");
  240. MODULE_LICENSE("GPL");
  241. module_param(port, int, 0);
  242. MODULE_PARM_DESC(port, "base address of watchdog card, default is 0x91");
  243. module_param(verbose, int, 0);
  244. MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
  245. module_param(ticks, int, 0);
  246. MODULE_PARM_DESC(ticks, "count down ticks, default is 10000");