watchdog_dev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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, &wddev->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, &wddev->status)) {
  77. err = wddev->ops->start(wddev);
  78. if (err < 0)
  79. return err;
  80. set_bit(WDOG_ACTIVE, &wddev->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, &wddev->status)) {
  97. pr_info("%s: nowayout prevents watchdog to be stopped!\n",
  98. wddev->info->identity);
  99. return err;
  100. }
  101. if (test_bit(WDOG_ACTIVE, &wddev->status)) {
  102. err = wddev->ops->stop(wddev);
  103. if (err < 0)
  104. return err;
  105. clear_bit(WDOG_ACTIVE, &wddev->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. /* If the watchdog is active then we send a keepalive ping
  205. * to make sure that the watchdog keep's running (and if
  206. * possible that it takes the new timeout) */
  207. watchdog_ping(wdd);
  208. /* Fall */
  209. case WDIOC_GETTIMEOUT:
  210. /* timeout == 0 means that we don't know the timeout */
  211. if (wdd->timeout == 0)
  212. return -EOPNOTSUPP;
  213. return put_user(wdd->timeout, p);
  214. case WDIOC_GETTIMELEFT:
  215. if (!wdd->ops->get_timeleft)
  216. return -EOPNOTSUPP;
  217. return put_user(wdd->ops->get_timeleft(wdd), p);
  218. default:
  219. return -ENOTTY;
  220. }
  221. }
  222. /*
  223. * watchdog_open: open the /dev/watchdog device.
  224. * @inode: inode of device
  225. * @file: file handle to device
  226. *
  227. * When the /dev/watchdog device gets opened, we start the watchdog.
  228. * Watch out: the /dev/watchdog device is single open, so we make sure
  229. * it can only be opened once.
  230. */
  231. static int watchdog_open(struct inode *inode, struct file *file)
  232. {
  233. int err = -EBUSY;
  234. /* the watchdog is single open! */
  235. if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
  236. return -EBUSY;
  237. /*
  238. * If the /dev/watchdog device is open, we don't want the module
  239. * to be unloaded.
  240. */
  241. if (!try_module_get(wdd->ops->owner))
  242. goto out;
  243. err = watchdog_start(wdd);
  244. if (err < 0)
  245. goto out_mod;
  246. /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
  247. return nonseekable_open(inode, file);
  248. out_mod:
  249. module_put(wdd->ops->owner);
  250. out:
  251. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  252. return err;
  253. }
  254. /*
  255. * watchdog_release: release the /dev/watchdog device.
  256. * @inode: inode of device
  257. * @file: file handle to device
  258. *
  259. * This is the code for when /dev/watchdog gets closed. We will only
  260. * stop the watchdog when we have received the magic char (and nowayout
  261. * was not set), else the watchdog will keep running.
  262. */
  263. static int watchdog_release(struct inode *inode, struct file *file)
  264. {
  265. int err = -EBUSY;
  266. /*
  267. * We only stop the watchdog if we received the magic character
  268. * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
  269. * watchdog_stop will fail.
  270. */
  271. if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
  272. !(wdd->info->options & WDIOF_MAGICCLOSE))
  273. err = watchdog_stop(wdd);
  274. /* If the watchdog was not stopped, send a keepalive ping */
  275. if (err < 0) {
  276. pr_crit("%s: watchdog did not stop!\n", wdd->info->identity);
  277. watchdog_ping(wdd);
  278. }
  279. /* Allow the owner module to be unloaded again */
  280. module_put(wdd->ops->owner);
  281. /* make sure that /dev/watchdog can be re-opened */
  282. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  283. return 0;
  284. }
  285. static const struct file_operations watchdog_fops = {
  286. .owner = THIS_MODULE,
  287. .write = watchdog_write,
  288. .unlocked_ioctl = watchdog_ioctl,
  289. .open = watchdog_open,
  290. .release = watchdog_release,
  291. };
  292. static struct miscdevice watchdog_miscdev = {
  293. .minor = WATCHDOG_MINOR,
  294. .name = "watchdog",
  295. .fops = &watchdog_fops,
  296. };
  297. /*
  298. * watchdog_dev_register:
  299. * @watchdog: watchdog device
  300. *
  301. * Register a watchdog device as /dev/watchdog. /dev/watchdog
  302. * is actually a miscdevice and thus we set it up like that.
  303. */
  304. int watchdog_dev_register(struct watchdog_device *watchdog)
  305. {
  306. int err;
  307. /* Only one device can register for /dev/watchdog */
  308. if (test_and_set_bit(0, &watchdog_dev_busy)) {
  309. pr_err("only one watchdog can use /dev/watchdog\n");
  310. return -EBUSY;
  311. }
  312. wdd = watchdog;
  313. err = misc_register(&watchdog_miscdev);
  314. if (err != 0) {
  315. pr_err("%s: cannot register miscdev on minor=%d (err=%d)\n",
  316. watchdog->info->identity, WATCHDOG_MINOR, err);
  317. goto out;
  318. }
  319. return 0;
  320. out:
  321. wdd = NULL;
  322. clear_bit(0, &watchdog_dev_busy);
  323. return err;
  324. }
  325. /*
  326. * watchdog_dev_unregister:
  327. * @watchdog: watchdog device
  328. *
  329. * Deregister the /dev/watchdog device.
  330. */
  331. int watchdog_dev_unregister(struct watchdog_device *watchdog)
  332. {
  333. /* Check that a watchdog device was registered in the past */
  334. if (!test_bit(0, &watchdog_dev_busy) || !wdd)
  335. return -ENODEV;
  336. /* We can only unregister the watchdog device that was registered */
  337. if (watchdog != wdd) {
  338. pr_err("%s: watchdog was not registered as /dev/watchdog\n",
  339. watchdog->info->identity);
  340. return -ENODEV;
  341. }
  342. misc_deregister(&watchdog_miscdev);
  343. wdd = NULL;
  344. clear_bit(0, &watchdog_dev_busy);
  345. return 0;
  346. }