watchdog_dev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * watchdog_dev.c
  3. *
  4. * (c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  5. * All Rights Reserved.
  6. *
  7. * (c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>.
  8. *
  9. *
  10. * This source code is part of the generic code that can be used
  11. * by all the watchdog timer drivers.
  12. *
  13. * This part of the generic code takes care of the following
  14. * misc device: /dev/watchdog.
  15. *
  16. * Based on source code of the following authors:
  17. * Matt Domsch <Matt_Domsch@dell.com>,
  18. * Rob Radez <rob@osinvestor.com>,
  19. * Rusty Lynch <rusty@linux.co.intel.com>
  20. * Satyam Sharma <satyam@infradead.org>
  21. * Randy Dunlap <randy.dunlap@oracle.com>
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public License
  25. * as published by the Free Software Foundation; either version
  26. * 2 of the License, or (at your option) any later version.
  27. *
  28. * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
  29. * admit liability nor provide warranty for any of this software.
  30. * This material is provided "AS-IS" and at no charge.
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include <linux/module.h> /* For module stuff/... */
  34. #include <linux/types.h> /* For standard types (like size_t) */
  35. #include <linux/errno.h> /* For the -ENODEV/... values */
  36. #include <linux/kernel.h> /* For printk/panic/... */
  37. #include <linux/fs.h> /* For file operations */
  38. #include <linux/watchdog.h> /* For watchdog specific items */
  39. #include <linux/miscdevice.h> /* For handling misc devices */
  40. #include <linux/init.h> /* For __init/__exit/... */
  41. #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
  42. /* make sure we only register one /dev/watchdog device */
  43. static unsigned long watchdog_dev_busy;
  44. /* the watchdog device behind /dev/watchdog */
  45. static struct watchdog_device *wdd;
  46. /*
  47. * watchdog_ping: ping the watchdog.
  48. * @wddev: the watchdog device to ping
  49. *
  50. * If the watchdog has no own ping operation then it needs to be
  51. * restarted via the start operation. This wrapper function does
  52. * exactly that.
  53. * We only ping when the watchdog device is running.
  54. */
  55. static int watchdog_ping(struct watchdog_device *wddev)
  56. {
  57. if (test_bit(WDOG_ACTIVE, &wdd->status)) {
  58. if (wddev->ops->ping)
  59. return wddev->ops->ping(wddev); /* ping the watchdog */
  60. else
  61. return wddev->ops->start(wddev); /* restart watchdog */
  62. }
  63. return 0;
  64. }
  65. /*
  66. * watchdog_start: wrapper to start the watchdog.
  67. * @wddev: the watchdog device to start
  68. *
  69. * Start the watchdog if it is not active and mark it active.
  70. * This function returns zero on success or a negative errno code for
  71. * failure.
  72. */
  73. static int watchdog_start(struct watchdog_device *wddev)
  74. {
  75. int err;
  76. if (!test_bit(WDOG_ACTIVE, &wdd->status)) {
  77. err = wddev->ops->start(wddev);
  78. if (err < 0)
  79. return err;
  80. set_bit(WDOG_ACTIVE, &wdd->status);
  81. }
  82. return 0;
  83. }
  84. /*
  85. * watchdog_stop: wrapper to stop the watchdog.
  86. * @wddev: the watchdog device to stop
  87. *
  88. * Stop the watchdog if it is still active and unmark it active.
  89. * This function returns zero on success or a negative errno code for
  90. * failure.
  91. * If the 'nowayout' feature was set, the watchdog cannot be stopped.
  92. */
  93. static int watchdog_stop(struct watchdog_device *wddev)
  94. {
  95. int err = -EBUSY;
  96. if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
  97. pr_info("%s: nowayout prevents watchdog to be stopped!\n",
  98. wdd->info->identity);
  99. return err;
  100. }
  101. if (test_bit(WDOG_ACTIVE, &wdd->status)) {
  102. err = wddev->ops->stop(wddev);
  103. if (err < 0)
  104. return err;
  105. clear_bit(WDOG_ACTIVE, &wdd->status);
  106. }
  107. return 0;
  108. }
  109. /*
  110. * watchdog_write: writes to the watchdog.
  111. * @file: file from VFS
  112. * @data: user address of data
  113. * @len: length of data
  114. * @ppos: pointer to the file offset
  115. *
  116. * A write to a watchdog device is defined as a keepalive ping.
  117. * Writing the magic 'V' sequence allows the next close to turn
  118. * off the watchdog (if 'nowayout' is not set).
  119. */
  120. static ssize_t watchdog_write(struct file *file, const char __user *data,
  121. size_t len, loff_t *ppos)
  122. {
  123. size_t i;
  124. char c;
  125. if (len == 0)
  126. return 0;
  127. /*
  128. * Note: just in case someone wrote the magic character
  129. * five months ago...
  130. */
  131. clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
  132. /* scan to see whether or not we got the magic character */
  133. for (i = 0; i != len; i++) {
  134. if (get_user(c, data + i))
  135. return -EFAULT;
  136. if (c == 'V')
  137. set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
  138. }
  139. /* someone wrote to us, so we send the watchdog a keepalive ping */
  140. watchdog_ping(wdd);
  141. return len;
  142. }
  143. /*
  144. * watchdog_ioctl: handle the different ioctl's for the watchdog device.
  145. * @file: file handle to the device
  146. * @cmd: watchdog command
  147. * @arg: argument pointer
  148. *
  149. * The watchdog API defines a common set of functions for all watchdogs
  150. * according to their available features.
  151. */
  152. static long watchdog_ioctl(struct file *file, unsigned int cmd,
  153. unsigned long arg)
  154. {
  155. void __user *argp = (void __user *)arg;
  156. int __user *p = argp;
  157. unsigned int val;
  158. int err;
  159. if (wdd->ops->ioctl) {
  160. err = wdd->ops->ioctl(wdd, cmd, arg);
  161. if (err != -ENOIOCTLCMD)
  162. return err;
  163. }
  164. switch (cmd) {
  165. case WDIOC_GETSUPPORT:
  166. return copy_to_user(argp, wdd->info,
  167. sizeof(struct watchdog_info)) ? -EFAULT : 0;
  168. case WDIOC_GETSTATUS:
  169. val = wdd->ops->status ? wdd->ops->status(wdd) : 0;
  170. return put_user(val, p);
  171. case WDIOC_GETBOOTSTATUS:
  172. return put_user(wdd->bootstatus, p);
  173. case WDIOC_SETOPTIONS:
  174. if (get_user(val, p))
  175. return -EFAULT;
  176. if (val & WDIOS_DISABLECARD) {
  177. err = watchdog_stop(wdd);
  178. if (err < 0)
  179. return err;
  180. }
  181. if (val & WDIOS_ENABLECARD) {
  182. err = watchdog_start(wdd);
  183. if (err < 0)
  184. return err;
  185. }
  186. return 0;
  187. case WDIOC_KEEPALIVE:
  188. if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
  189. return -EOPNOTSUPP;
  190. watchdog_ping(wdd);
  191. return 0;
  192. case WDIOC_SETTIMEOUT:
  193. if ((wdd->ops->set_timeout == NULL) ||
  194. !(wdd->info->options & WDIOF_SETTIMEOUT))
  195. return -EOPNOTSUPP;
  196. if (get_user(val, p))
  197. return -EFAULT;
  198. if ((wdd->max_timeout != 0) &&
  199. (val < wdd->min_timeout || val > wdd->max_timeout))
  200. return -EINVAL;
  201. err = wdd->ops->set_timeout(wdd, val);
  202. if (err < 0)
  203. return err;
  204. wdd->timeout = val;
  205. /* If the watchdog is active then we send a keepalive ping
  206. * to make sure that the watchdog keep's running (and if
  207. * possible that it takes the new timeout) */
  208. watchdog_ping(wdd);
  209. /* Fall */
  210. case WDIOC_GETTIMEOUT:
  211. /* timeout == 0 means that we don't know the timeout */
  212. if (wdd->timeout == 0)
  213. return -EOPNOTSUPP;
  214. return put_user(wdd->timeout, p);
  215. default:
  216. return -ENOTTY;
  217. }
  218. }
  219. /*
  220. * watchdog_open: open the /dev/watchdog device.
  221. * @inode: inode of device
  222. * @file: file handle to device
  223. *
  224. * When the /dev/watchdog device gets opened, we start the watchdog.
  225. * Watch out: the /dev/watchdog device is single open, so we make sure
  226. * it can only be opened once.
  227. */
  228. static int watchdog_open(struct inode *inode, struct file *file)
  229. {
  230. int err = -EBUSY;
  231. /* the watchdog is single open! */
  232. if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
  233. return -EBUSY;
  234. /*
  235. * If the /dev/watchdog device is open, we don't want the module
  236. * to be unloaded.
  237. */
  238. if (!try_module_get(wdd->ops->owner))
  239. goto out;
  240. err = watchdog_start(wdd);
  241. if (err < 0)
  242. goto out_mod;
  243. /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
  244. return nonseekable_open(inode, file);
  245. out_mod:
  246. module_put(wdd->ops->owner);
  247. out:
  248. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  249. return err;
  250. }
  251. /*
  252. * watchdog_release: release the /dev/watchdog device.
  253. * @inode: inode of device
  254. * @file: file handle to device
  255. *
  256. * This is the code for when /dev/watchdog gets closed. We will only
  257. * stop the watchdog when we have received the magic char (and nowayout
  258. * was not set), else the watchdog will keep running.
  259. */
  260. static int watchdog_release(struct inode *inode, struct file *file)
  261. {
  262. int err = -EBUSY;
  263. /*
  264. * We only stop the watchdog if we received the magic character
  265. * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
  266. * watchdog_stop will fail.
  267. */
  268. if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
  269. !(wdd->info->options & WDIOF_MAGICCLOSE))
  270. err = watchdog_stop(wdd);
  271. /* If the watchdog was not stopped, send a keepalive ping */
  272. if (err < 0) {
  273. pr_crit("%s: watchdog did not stop!\n", wdd->info->identity);
  274. watchdog_ping(wdd);
  275. }
  276. /* Allow the owner module to be unloaded again */
  277. module_put(wdd->ops->owner);
  278. /* make sure that /dev/watchdog can be re-opened */
  279. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  280. return 0;
  281. }
  282. static const struct file_operations watchdog_fops = {
  283. .owner = THIS_MODULE,
  284. .write = watchdog_write,
  285. .unlocked_ioctl = watchdog_ioctl,
  286. .open = watchdog_open,
  287. .release = watchdog_release,
  288. };
  289. static struct miscdevice watchdog_miscdev = {
  290. .minor = WATCHDOG_MINOR,
  291. .name = "watchdog",
  292. .fops = &watchdog_fops,
  293. };
  294. /*
  295. * watchdog_dev_register:
  296. * @watchdog: watchdog device
  297. *
  298. * Register a watchdog device as /dev/watchdog. /dev/watchdog
  299. * is actually a miscdevice and thus we set it up like that.
  300. */
  301. int watchdog_dev_register(struct watchdog_device *watchdog)
  302. {
  303. int err;
  304. /* Only one device can register for /dev/watchdog */
  305. if (test_and_set_bit(0, &watchdog_dev_busy)) {
  306. pr_err("only one watchdog can use /dev/watchdog.\n");
  307. return -EBUSY;
  308. }
  309. wdd = watchdog;
  310. err = misc_register(&watchdog_miscdev);
  311. if (err != 0) {
  312. pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
  313. watchdog->info->identity, WATCHDOG_MINOR, err);
  314. goto out;
  315. }
  316. return 0;
  317. out:
  318. wdd = NULL;
  319. clear_bit(0, &watchdog_dev_busy);
  320. return err;
  321. }
  322. /*
  323. * watchdog_dev_unregister:
  324. * @watchdog: watchdog device
  325. *
  326. * Deregister the /dev/watchdog device.
  327. */
  328. int watchdog_dev_unregister(struct watchdog_device *watchdog)
  329. {
  330. /* Check that a watchdog device was registered in the past */
  331. if (!test_bit(0, &watchdog_dev_busy) || !wdd)
  332. return -ENODEV;
  333. /* We can only unregister the watchdog device that was registered */
  334. if (watchdog != wdd) {
  335. pr_err("%s: watchdog was not registered as /dev/watchdog.\n",
  336. watchdog->info->identity);
  337. return -ENODEV;
  338. }
  339. misc_deregister(&watchdog_miscdev);
  340. wdd = NULL;
  341. clear_bit(0, &watchdog_dev_busy);
  342. return 0;
  343. }