sbc60xxwdt.c 10 KB

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