sbc60xxwdt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * 60xx Single Board Computer Watchdog Timer driver for Linux 2.2.x
  3. *
  4. * Based on acquirewdt.c by Alan Cox.
  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. * The author does NOT admit liability nor provide warranty for
  12. * any of this software. This material is provided "AS-IS" in
  13. * the hope that it may be useful for others.
  14. *
  15. * (c) Copyright 2000 Jakob Oestergaard <jakob@unthought.net>
  16. *
  17. * 12/4 - 2000 [Initial revision]
  18. * 25/4 - 2000 Added /dev/watchdog support
  19. * 09/5 - 2001 [smj@oro.net] fixed fop_write to "return 1"
  20. * on success
  21. * 12/4 - 2002 [rob@osinvestor.com] eliminate fop_read
  22. * fix possible wdt_is_open race
  23. * add CONFIG_WATCHDOG_NOWAYOUT support
  24. * remove lock_kernel/unlock_kernel pairs
  25. * added KERN_* to printk's
  26. * got rid of extraneous comments
  27. * changed watchdog_info to correctly reflect what
  28. * the driver offers
  29. * added WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS,
  30. * WDIOC_SETTIMEOUT, WDIOC_GETTIMEOUT, and
  31. * WDIOC_SETOPTIONS ioctls
  32. * 09/8 - 2003 [wim@iguana.be] cleanup of trailing spaces
  33. * use module_param
  34. * made timeout (the emulated heartbeat) a
  35. * module_param
  36. * made the keepalive ping an internal subroutine
  37. * made wdt_stop and wdt_start module params
  38. * added extra printk's for startup problems
  39. * added MODULE_AUTHOR and MODULE_DESCRIPTION info
  40. *
  41. *
  42. * This WDT driver is different from the other Linux WDT
  43. * drivers in the following ways:
  44. * *) The driver will ping the watchdog by itself, because this
  45. * particular WDT has a very short timeout (one second) and it
  46. * would be insane to count on any userspace daemon always
  47. * getting scheduled within that time frame.
  48. *
  49. */
  50. #include <linux/module.h>
  51. #include <linux/moduleparam.h>
  52. #include <linux/types.h>
  53. #include <linux/timer.h>
  54. #include <linux/jiffies.h>
  55. #include <linux/miscdevice.h>
  56. #include <linux/watchdog.h>
  57. #include <linux/fs.h>
  58. #include <linux/ioport.h>
  59. #include <linux/notifier.h>
  60. #include <linux/reboot.h>
  61. #include <linux/init.h>
  62. #include <linux/io.h>
  63. #include <linux/uaccess.h>
  64. #include <asm/system.h>
  65. #define OUR_NAME "sbc60xxwdt"
  66. #define PFX OUR_NAME ": "
  67. /*
  68. * You must set these - The driver cannot probe for the settings
  69. */
  70. static int wdt_stop = 0x45;
  71. module_param(wdt_stop, int, 0);
  72. MODULE_PARM_DESC(wdt_stop, "SBC60xx WDT 'stop' io port (default 0x45)");
  73. static int wdt_start = 0x443;
  74. module_param(wdt_start, int, 0);
  75. MODULE_PARM_DESC(wdt_start, "SBC60xx WDT 'start' io port (default 0x443)");
  76. /*
  77. * The 60xx board can use watchdog timeout values from one second
  78. * to several minutes. The default is one second, so if we reset
  79. * the watchdog every ~250ms we should be safe.
  80. */
  81. #define WDT_INTERVAL (HZ/4+1)
  82. /*
  83. * We must not require too good response from the userspace daemon.
  84. * Here we require the userspace daemon to send us a heartbeat
  85. * char to /dev/watchdog every 30 seconds.
  86. * If the daemon pulses us every 25 seconds, we can still afford
  87. * a 5 second scheduling delay on the (high priority) daemon. That
  88. * should be sufficient for a box under any load.
  89. */
  90. #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
  91. static int timeout = WATCHDOG_TIMEOUT; /* in seconds, multiplied by HZ to
  92. get seconds to wait for a ping */
  93. module_param(timeout, int, 0);
  94. MODULE_PARM_DESC(timeout,
  95. "Watchdog timeout in seconds. (1<=timeout<=3600, default="
  96. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  97. static int nowayout = WATCHDOG_NOWAYOUT;
  98. module_param(nowayout, int, 0);
  99. MODULE_PARM_DESC(nowayout,
  100. "Watchdog cannot be stopped once started (default="
  101. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  102. static void wdt_timer_ping(unsigned long);
  103. static DEFINE_TIMER(timer, wdt_timer_ping, 0, 0);
  104. static unsigned long next_heartbeat;
  105. static unsigned long wdt_is_open;
  106. static char wdt_expect_close;
  107. /*
  108. * Whack the dog
  109. */
  110. static void wdt_timer_ping(unsigned long data)
  111. {
  112. /* If we got a heartbeat pulse within the WDT_US_INTERVAL
  113. * we agree to ping the WDT
  114. */
  115. if (time_before(jiffies, next_heartbeat)) {
  116. /* Ping the WDT by reading from wdt_start */
  117. inb_p(wdt_start);
  118. /* Re-set the timer interval */
  119. mod_timer(&timer, jiffies + WDT_INTERVAL);
  120. } else
  121. printk(KERN_WARNING PFX
  122. "Heartbeat lost! Will not ping the watchdog\n");
  123. }
  124. /*
  125. * Utility routines
  126. */
  127. static void wdt_startup(void)
  128. {
  129. next_heartbeat = jiffies + (timeout * HZ);
  130. /* Start the timer */
  131. mod_timer(&timer, jiffies + WDT_INTERVAL);
  132. printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
  133. }
  134. static void wdt_turnoff(void)
  135. {
  136. /* Stop the timer */
  137. del_timer(&timer);
  138. inb_p(wdt_stop);
  139. printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
  140. }
  141. static void wdt_keepalive(void)
  142. {
  143. /* user land ping */
  144. next_heartbeat = jiffies + (timeout * HZ);
  145. }
  146. /*
  147. * /dev/watchdog handling
  148. */
  149. static ssize_t fop_write(struct file *file, const char __user *buf,
  150. size_t count, loff_t *ppos)
  151. {
  152. /* See if we got the magic character 'V' and reload the timer */
  153. if (count) {
  154. if (!nowayout) {
  155. size_t ofs;
  156. /* note: just in case someone wrote the
  157. magic character five months ago... */
  158. wdt_expect_close = 0;
  159. /* scan to see whether or not we got the
  160. magic character */
  161. for (ofs = 0; ofs != count; ofs++) {
  162. char c;
  163. if (get_user(c, buf + ofs))
  164. return -EFAULT;
  165. if (c == 'V')
  166. wdt_expect_close = 42;
  167. }
  168. }
  169. /* Well, anyhow someone wrote to us, we should
  170. return that favour */
  171. wdt_keepalive();
  172. }
  173. return count;
  174. }
  175. static int fop_open(struct inode *inode, struct file *file)
  176. {
  177. /* Just in case we're already talking to someone... */
  178. if (test_and_set_bit(0, &wdt_is_open))
  179. return -EBUSY;
  180. if (nowayout)
  181. __module_get(THIS_MODULE);
  182. /* Good, fire up the show */
  183. wdt_startup();
  184. return nonseekable_open(inode, file);
  185. }
  186. static int fop_close(struct inode *inode, struct file *file)
  187. {
  188. if (wdt_expect_close == 42)
  189. wdt_turnoff();
  190. else {
  191. del_timer(&timer);
  192. printk(KERN_CRIT PFX
  193. "device file closed unexpectedly. Will not stop the WDT!\n");
  194. }
  195. clear_bit(0, &wdt_is_open);
  196. wdt_expect_close = 0;
  197. return 0;
  198. }
  199. static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  200. {
  201. void __user *argp = (void __user *)arg;
  202. int __user *p = argp;
  203. static const struct watchdog_info ident = {
  204. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  205. WDIOF_MAGICCLOSE,
  206. .firmware_version = 1,
  207. .identity = "SBC60xx",
  208. };
  209. switch (cmd) {
  210. case WDIOC_GETSUPPORT:
  211. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  212. case WDIOC_GETSTATUS:
  213. case WDIOC_GETBOOTSTATUS:
  214. return put_user(0, p);
  215. case WDIOC_SETOPTIONS:
  216. {
  217. int new_options, retval = -EINVAL;
  218. if (get_user(new_options, p))
  219. return -EFAULT;
  220. if (new_options & WDIOS_DISABLECARD) {
  221. wdt_turnoff();
  222. retval = 0;
  223. }
  224. if (new_options & WDIOS_ENABLECARD) {
  225. wdt_startup();
  226. retval = 0;
  227. }
  228. return retval;
  229. }
  230. case WDIOC_KEEPALIVE:
  231. wdt_keepalive();
  232. return 0;
  233. case WDIOC_SETTIMEOUT:
  234. {
  235. int new_timeout;
  236. if (get_user(new_timeout, p))
  237. return -EFAULT;
  238. /* arbitrary upper limit */
  239. if (new_timeout < 1 || new_timeout > 3600)
  240. return -EINVAL;
  241. timeout = new_timeout;
  242. wdt_keepalive();
  243. /* Fall through */
  244. }
  245. case WDIOC_GETTIMEOUT:
  246. return put_user(timeout, p);
  247. default:
  248. return -ENOTTY;
  249. }
  250. }
  251. static const struct file_operations wdt_fops = {
  252. .owner = THIS_MODULE,
  253. .llseek = no_llseek,
  254. .write = fop_write,
  255. .open = fop_open,
  256. .release = fop_close,
  257. .unlocked_ioctl = fop_ioctl,
  258. };
  259. static struct miscdevice wdt_miscdev = {
  260. .minor = WATCHDOG_MINOR,
  261. .name = "watchdog",
  262. .fops = &wdt_fops,
  263. };
  264. /*
  265. * Notifier for system down
  266. */
  267. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  268. void *unused)
  269. {
  270. if (code == SYS_DOWN || code == SYS_HALT)
  271. wdt_turnoff();
  272. return NOTIFY_DONE;
  273. }
  274. /*
  275. * The WDT needs to learn about soft shutdowns in order to
  276. * turn the timebomb registers off.
  277. */
  278. static struct notifier_block wdt_notifier = {
  279. .notifier_call = wdt_notify_sys,
  280. };
  281. static void __exit sbc60xxwdt_unload(void)
  282. {
  283. wdt_turnoff();
  284. /* Deregister */
  285. misc_deregister(&wdt_miscdev);
  286. unregister_reboot_notifier(&wdt_notifier);
  287. if ((wdt_stop != 0x45) && (wdt_stop != wdt_start))
  288. release_region(wdt_stop, 1);
  289. release_region(wdt_start, 1);
  290. }
  291. static int __init sbc60xxwdt_init(void)
  292. {
  293. int rc = -EBUSY;
  294. if (timeout < 1 || timeout > 3600) { /* arbitrary upper limit */
  295. timeout = WATCHDOG_TIMEOUT;
  296. printk(KERN_INFO PFX
  297. "timeout value must be 1 <= x <= 3600, using %d\n",
  298. timeout);
  299. }
  300. if (!request_region(wdt_start, 1, "SBC 60XX WDT")) {
  301. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  302. wdt_start);
  303. rc = -EIO;
  304. goto err_out;
  305. }
  306. /* We cannot reserve 0x45 - the kernel already has! */
  307. if (wdt_stop != 0x45 && wdt_stop != wdt_start) {
  308. if (!request_region(wdt_stop, 1, "SBC 60XX WDT")) {
  309. printk(KERN_ERR PFX
  310. "I/O address 0x%04x already in use\n",
  311. wdt_stop);
  312. rc = -EIO;
  313. goto err_out_region1;
  314. }
  315. }
  316. rc = register_reboot_notifier(&wdt_notifier);
  317. if (rc) {
  318. printk(KERN_ERR PFX
  319. "cannot register reboot notifier (err=%d)\n", rc);
  320. goto err_out_region2;
  321. }
  322. rc = misc_register(&wdt_miscdev);
  323. if (rc) {
  324. printk(KERN_ERR PFX
  325. "cannot register miscdev on minor=%d (err=%d)\n",
  326. wdt_miscdev.minor, rc);
  327. goto err_out_reboot;
  328. }
  329. printk(KERN_INFO PFX "WDT driver for 60XX single board computer initialised. timeout=%d sec (nowayout=%d)\n",
  330. timeout, nowayout);
  331. return 0;
  332. err_out_reboot:
  333. unregister_reboot_notifier(&wdt_notifier);
  334. err_out_region2:
  335. if (wdt_stop != 0x45 && wdt_stop != wdt_start)
  336. release_region(wdt_stop, 1);
  337. err_out_region1:
  338. release_region(wdt_start, 1);
  339. err_out:
  340. return rc;
  341. }
  342. module_init(sbc60xxwdt_init);
  343. module_exit(sbc60xxwdt_unload);
  344. MODULE_AUTHOR("Jakob Oestergaard <jakob@unthought.net>");
  345. MODULE_DESCRIPTION("60xx Single Board Computer Watchdog Timer driver");
  346. MODULE_LICENSE("GPL");
  347. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);