watchdog_dev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. #include "watchdog_dev.h"
  43. /* make sure we only register one /dev/watchdog device */
  44. static unsigned long watchdog_dev_busy;
  45. /* the watchdog device behind /dev/watchdog */
  46. static struct watchdog_device *wdd;
  47. /*
  48. * watchdog_ping: ping the watchdog.
  49. * @wddev: the watchdog device to ping
  50. *
  51. * If the watchdog has no own ping operation then it needs to be
  52. * restarted via the start operation. This wrapper function does
  53. * exactly that.
  54. * We only ping when the watchdog device is running.
  55. */
  56. static int watchdog_ping(struct watchdog_device *wddev)
  57. {
  58. if (watchdog_active(wddev)) {
  59. if (wddev->ops->ping)
  60. return wddev->ops->ping(wddev); /* ping the watchdog */
  61. else
  62. return wddev->ops->start(wddev); /* restart watchdog */
  63. }
  64. return 0;
  65. }
  66. /*
  67. * watchdog_start: wrapper to start the watchdog.
  68. * @wddev: the watchdog device to start
  69. *
  70. * Start the watchdog if it is not active and mark it active.
  71. * This function returns zero on success or a negative errno code for
  72. * failure.
  73. */
  74. static int watchdog_start(struct watchdog_device *wddev)
  75. {
  76. int err;
  77. if (!watchdog_active(wddev)) {
  78. err = wddev->ops->start(wddev);
  79. if (err < 0)
  80. return err;
  81. set_bit(WDOG_ACTIVE, &wddev->status);
  82. }
  83. return 0;
  84. }
  85. /*
  86. * watchdog_stop: wrapper to stop the watchdog.
  87. * @wddev: the watchdog device to stop
  88. *
  89. * Stop the watchdog if it is still active and unmark it active.
  90. * This function returns zero on success or a negative errno code for
  91. * failure.
  92. * If the 'nowayout' feature was set, the watchdog cannot be stopped.
  93. */
  94. static int watchdog_stop(struct watchdog_device *wddev)
  95. {
  96. int err = -EBUSY;
  97. if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) {
  98. pr_info("%s: nowayout prevents watchdog to be stopped!\n",
  99. wddev->info->identity);
  100. return err;
  101. }
  102. if (watchdog_active(wddev)) {
  103. err = wddev->ops->stop(wddev);
  104. if (err < 0)
  105. return err;
  106. clear_bit(WDOG_ACTIVE, &wddev->status);
  107. }
  108. return 0;
  109. }
  110. /*
  111. * watchdog_write: writes to the watchdog.
  112. * @file: file from VFS
  113. * @data: user address of data
  114. * @len: length of data
  115. * @ppos: pointer to the file offset
  116. *
  117. * A write to a watchdog device is defined as a keepalive ping.
  118. * Writing the magic 'V' sequence allows the next close to turn
  119. * off the watchdog (if 'nowayout' is not set).
  120. */
  121. static ssize_t watchdog_write(struct file *file, const char __user *data,
  122. size_t len, loff_t *ppos)
  123. {
  124. size_t i;
  125. char c;
  126. if (len == 0)
  127. return 0;
  128. /*
  129. * Note: just in case someone wrote the magic character
  130. * five months ago...
  131. */
  132. clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
  133. /* scan to see whether or not we got the magic character */
  134. for (i = 0; i != len; i++) {
  135. if (get_user(c, data + i))
  136. return -EFAULT;
  137. if (c == 'V')
  138. set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
  139. }
  140. /* someone wrote to us, so we send the watchdog a keepalive ping */
  141. watchdog_ping(wdd);
  142. return len;
  143. }
  144. /*
  145. * watchdog_ioctl: handle the different ioctl's for the watchdog device.
  146. * @file: file handle to the device
  147. * @cmd: watchdog command
  148. * @arg: argument pointer
  149. *
  150. * The watchdog API defines a common set of functions for all watchdogs
  151. * according to their available features.
  152. */
  153. static long watchdog_ioctl(struct file *file, unsigned int cmd,
  154. unsigned long arg)
  155. {
  156. void __user *argp = (void __user *)arg;
  157. int __user *p = argp;
  158. unsigned int val;
  159. int err;
  160. if (wdd->ops->ioctl) {
  161. err = wdd->ops->ioctl(wdd, cmd, arg);
  162. if (err != -ENOIOCTLCMD)
  163. return err;
  164. }
  165. switch (cmd) {
  166. case WDIOC_GETSUPPORT:
  167. return copy_to_user(argp, wdd->info,
  168. sizeof(struct watchdog_info)) ? -EFAULT : 0;
  169. case WDIOC_GETSTATUS:
  170. val = wdd->ops->status ? wdd->ops->status(wdd) : 0;
  171. return put_user(val, p);
  172. case WDIOC_GETBOOTSTATUS:
  173. return put_user(wdd->bootstatus, p);
  174. case WDIOC_SETOPTIONS:
  175. if (get_user(val, p))
  176. return -EFAULT;
  177. if (val & WDIOS_DISABLECARD) {
  178. err = watchdog_stop(wdd);
  179. if (err < 0)
  180. return err;
  181. }
  182. if (val & WDIOS_ENABLECARD) {
  183. err = watchdog_start(wdd);
  184. if (err < 0)
  185. return err;
  186. }
  187. return 0;
  188. case WDIOC_KEEPALIVE:
  189. if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
  190. return -EOPNOTSUPP;
  191. watchdog_ping(wdd);
  192. return 0;
  193. case WDIOC_SETTIMEOUT:
  194. if ((wdd->ops->set_timeout == NULL) ||
  195. !(wdd->info->options & WDIOF_SETTIMEOUT))
  196. return -EOPNOTSUPP;
  197. if (get_user(val, p))
  198. return -EFAULT;
  199. if ((wdd->max_timeout != 0) &&
  200. (val < wdd->min_timeout || val > wdd->max_timeout))
  201. return -EINVAL;
  202. err = wdd->ops->set_timeout(wdd, val);
  203. if (err < 0)
  204. return err;
  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. case WDIOC_GETTIMELEFT:
  216. if (!wdd->ops->get_timeleft)
  217. return -EOPNOTSUPP;
  218. return put_user(wdd->ops->get_timeleft(wdd), p);
  219. default:
  220. return -ENOTTY;
  221. }
  222. }
  223. /*
  224. * watchdog_open: open the /dev/watchdog device.
  225. * @inode: inode of device
  226. * @file: file handle to device
  227. *
  228. * When the /dev/watchdog device gets opened, we start the watchdog.
  229. * Watch out: the /dev/watchdog device is single open, so we make sure
  230. * it can only be opened once.
  231. */
  232. static int watchdog_open(struct inode *inode, struct file *file)
  233. {
  234. int err = -EBUSY;
  235. /* the watchdog is single open! */
  236. if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
  237. return -EBUSY;
  238. /*
  239. * If the /dev/watchdog device is open, we don't want the module
  240. * to be unloaded.
  241. */
  242. if (!try_module_get(wdd->ops->owner))
  243. goto out;
  244. err = watchdog_start(wdd);
  245. if (err < 0)
  246. goto out_mod;
  247. /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
  248. return nonseekable_open(inode, file);
  249. out_mod:
  250. module_put(wdd->ops->owner);
  251. out:
  252. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  253. return err;
  254. }
  255. /*
  256. * watchdog_release: release the /dev/watchdog device.
  257. * @inode: inode of device
  258. * @file: file handle to device
  259. *
  260. * This is the code for when /dev/watchdog gets closed. We will only
  261. * stop the watchdog when we have received the magic char (and nowayout
  262. * was not set), else the watchdog will keep running.
  263. */
  264. static int watchdog_release(struct inode *inode, struct file *file)
  265. {
  266. int err = -EBUSY;
  267. /*
  268. * We only stop the watchdog if we received the magic character
  269. * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
  270. * watchdog_stop will fail.
  271. */
  272. if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
  273. !(wdd->info->options & WDIOF_MAGICCLOSE))
  274. err = watchdog_stop(wdd);
  275. /* If the watchdog was not stopped, send a keepalive ping */
  276. if (err < 0) {
  277. pr_crit("%s: watchdog did not stop!\n", wdd->info->identity);
  278. watchdog_ping(wdd);
  279. }
  280. /* Allow the owner module to be unloaded again */
  281. module_put(wdd->ops->owner);
  282. /* make sure that /dev/watchdog can be re-opened */
  283. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  284. return 0;
  285. }
  286. static const struct file_operations watchdog_fops = {
  287. .owner = THIS_MODULE,
  288. .write = watchdog_write,
  289. .unlocked_ioctl = watchdog_ioctl,
  290. .open = watchdog_open,
  291. .release = watchdog_release,
  292. };
  293. static struct miscdevice watchdog_miscdev = {
  294. .minor = WATCHDOG_MINOR,
  295. .name = "watchdog",
  296. .fops = &watchdog_fops,
  297. };
  298. /*
  299. * watchdog_dev_register:
  300. * @watchdog: watchdog device
  301. *
  302. * Register a watchdog device as /dev/watchdog. /dev/watchdog
  303. * is actually a miscdevice and thus we set it up like that.
  304. */
  305. int watchdog_dev_register(struct watchdog_device *watchdog)
  306. {
  307. int err;
  308. /* Only one device can register for /dev/watchdog */
  309. if (test_and_set_bit(0, &watchdog_dev_busy)) {
  310. pr_err("only one watchdog can use /dev/watchdog\n");
  311. return -EBUSY;
  312. }
  313. wdd = watchdog;
  314. err = misc_register(&watchdog_miscdev);
  315. if (err != 0) {
  316. pr_err("%s: cannot register miscdev on minor=%d (err=%d)\n",
  317. watchdog->info->identity, WATCHDOG_MINOR, err);
  318. goto out;
  319. }
  320. return 0;
  321. out:
  322. wdd = NULL;
  323. clear_bit(0, &watchdog_dev_busy);
  324. return err;
  325. }
  326. /*
  327. * watchdog_dev_unregister:
  328. * @watchdog: watchdog device
  329. *
  330. * Deregister the /dev/watchdog device.
  331. */
  332. int watchdog_dev_unregister(struct watchdog_device *watchdog)
  333. {
  334. /* Check that a watchdog device was registered in the past */
  335. if (!test_bit(0, &watchdog_dev_busy) || !wdd)
  336. return -ENODEV;
  337. /* We can only unregister the watchdog device that was registered */
  338. if (watchdog != wdd) {
  339. pr_err("%s: watchdog was not registered as /dev/watchdog\n",
  340. watchdog->info->identity);
  341. return -ENODEV;
  342. }
  343. misc_deregister(&watchdog_miscdev);
  344. wdd = NULL;
  345. clear_bit(0, &watchdog_dev_busy);
  346. return 0;
  347. }