watchdog_dev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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_core.h"
  43. /* the dev_t structure to store the dynamically allocated watchdog devices */
  44. static dev_t watchdog_devt;
  45. /* the watchdog device behind /dev/watchdog */
  46. static struct watchdog_device *old_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. dev_info(wddev->dev, "nowayout prevents watchdog being stopped!\n");
  99. return err;
  100. }
  101. if (watchdog_active(wddev)) {
  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. struct watchdog_device *wdd = file->private_data;
  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. struct watchdog_device *wdd = file->private_data;
  157. void __user *argp = (void __user *)arg;
  158. int __user *p = argp;
  159. unsigned int val;
  160. int err;
  161. if (wdd->ops->ioctl) {
  162. err = wdd->ops->ioctl(wdd, cmd, arg);
  163. if (err != -ENOIOCTLCMD)
  164. return err;
  165. }
  166. switch (cmd) {
  167. case WDIOC_GETSUPPORT:
  168. return copy_to_user(argp, wdd->info,
  169. sizeof(struct watchdog_info)) ? -EFAULT : 0;
  170. case WDIOC_GETSTATUS:
  171. val = wdd->ops->status ? wdd->ops->status(wdd) : 0;
  172. return put_user(val, p);
  173. case WDIOC_GETBOOTSTATUS:
  174. return put_user(wdd->bootstatus, p);
  175. case WDIOC_SETOPTIONS:
  176. if (get_user(val, p))
  177. return -EFAULT;
  178. if (val & WDIOS_DISABLECARD) {
  179. err = watchdog_stop(wdd);
  180. if (err < 0)
  181. return err;
  182. }
  183. if (val & WDIOS_ENABLECARD) {
  184. err = watchdog_start(wdd);
  185. if (err < 0)
  186. return err;
  187. }
  188. return 0;
  189. case WDIOC_KEEPALIVE:
  190. if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
  191. return -EOPNOTSUPP;
  192. watchdog_ping(wdd);
  193. return 0;
  194. case WDIOC_SETTIMEOUT:
  195. if ((wdd->ops->set_timeout == NULL) ||
  196. !(wdd->info->options & WDIOF_SETTIMEOUT))
  197. return -EOPNOTSUPP;
  198. if (get_user(val, p))
  199. return -EFAULT;
  200. if ((wdd->max_timeout != 0) &&
  201. (val < wdd->min_timeout || val > wdd->max_timeout))
  202. return -EINVAL;
  203. err = wdd->ops->set_timeout(wdd, val);
  204. if (err < 0)
  205. return err;
  206. /* If the watchdog is active then we send a keepalive ping
  207. * to make sure that the watchdog keep's running (and if
  208. * possible that it takes the new timeout) */
  209. watchdog_ping(wdd);
  210. /* Fall */
  211. case WDIOC_GETTIMEOUT:
  212. /* timeout == 0 means that we don't know the timeout */
  213. if (wdd->timeout == 0)
  214. return -EOPNOTSUPP;
  215. return put_user(wdd->timeout, p);
  216. case WDIOC_GETTIMELEFT:
  217. if (!wdd->ops->get_timeleft)
  218. return -EOPNOTSUPP;
  219. return put_user(wdd->ops->get_timeleft(wdd), p);
  220. default:
  221. return -ENOTTY;
  222. }
  223. }
  224. /*
  225. * watchdog_open: open the /dev/watchdog* devices.
  226. * @inode: inode of device
  227. * @file: file handle to device
  228. *
  229. * When the /dev/watchdog* device gets opened, we start the watchdog.
  230. * Watch out: the /dev/watchdog device is single open, so we make sure
  231. * it can only be opened once.
  232. */
  233. static int watchdog_open(struct inode *inode, struct file *file)
  234. {
  235. int err = -EBUSY;
  236. struct watchdog_device *wdd;
  237. /* Get the corresponding watchdog device */
  238. if (imajor(inode) == MISC_MAJOR)
  239. wdd = old_wdd;
  240. else
  241. wdd = container_of(inode->i_cdev, struct watchdog_device, cdev);
  242. /* the watchdog is single open! */
  243. if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
  244. return -EBUSY;
  245. /*
  246. * If the /dev/watchdog device is open, we don't want the module
  247. * to be unloaded.
  248. */
  249. if (!try_module_get(wdd->ops->owner))
  250. goto out;
  251. err = watchdog_start(wdd);
  252. if (err < 0)
  253. goto out_mod;
  254. file->private_data = wdd;
  255. /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
  256. return nonseekable_open(inode, file);
  257. out_mod:
  258. module_put(wdd->ops->owner);
  259. out:
  260. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  261. return err;
  262. }
  263. /*
  264. * watchdog_release: release the watchdog device.
  265. * @inode: inode of device
  266. * @file: file handle to device
  267. *
  268. * This is the code for when /dev/watchdog gets closed. We will only
  269. * stop the watchdog when we have received the magic char (and nowayout
  270. * was not set), else the watchdog will keep running.
  271. */
  272. static int watchdog_release(struct inode *inode, struct file *file)
  273. {
  274. struct watchdog_device *wdd = file->private_data;
  275. int err = -EBUSY;
  276. /*
  277. * We only stop the watchdog if we received the magic character
  278. * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
  279. * watchdog_stop will fail.
  280. */
  281. if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
  282. !(wdd->info->options & WDIOF_MAGICCLOSE))
  283. err = watchdog_stop(wdd);
  284. /* If the watchdog was not stopped, send a keepalive ping */
  285. if (err < 0) {
  286. dev_crit(wdd->dev, "watchdog did not stop!\n");
  287. watchdog_ping(wdd);
  288. }
  289. /* Allow the owner module to be unloaded again */
  290. module_put(wdd->ops->owner);
  291. /* make sure that /dev/watchdog can be re-opened */
  292. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  293. return 0;
  294. }
  295. static const struct file_operations watchdog_fops = {
  296. .owner = THIS_MODULE,
  297. .write = watchdog_write,
  298. .unlocked_ioctl = watchdog_ioctl,
  299. .open = watchdog_open,
  300. .release = watchdog_release,
  301. };
  302. static struct miscdevice watchdog_miscdev = {
  303. .minor = WATCHDOG_MINOR,
  304. .name = "watchdog",
  305. .fops = &watchdog_fops,
  306. };
  307. /*
  308. * watchdog_dev_register: register a watchdog device
  309. * @watchdog: watchdog device
  310. *
  311. * Register a watchdog device including handling the legacy
  312. * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
  313. * thus we set it up like that.
  314. */
  315. int watchdog_dev_register(struct watchdog_device *watchdog)
  316. {
  317. int err, devno;
  318. if (watchdog->id == 0) {
  319. watchdog_miscdev.parent = watchdog->parent;
  320. err = misc_register(&watchdog_miscdev);
  321. if (err != 0) {
  322. pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
  323. watchdog->info->identity, WATCHDOG_MINOR, err);
  324. if (err == -EBUSY)
  325. pr_err("%s: a legacy watchdog module is probably present.\n",
  326. watchdog->info->identity);
  327. return err;
  328. }
  329. old_wdd = watchdog;
  330. }
  331. /* Fill in the data structures */
  332. devno = MKDEV(MAJOR(watchdog_devt), watchdog->id);
  333. cdev_init(&watchdog->cdev, &watchdog_fops);
  334. watchdog->cdev.owner = watchdog->ops->owner;
  335. /* Add the device */
  336. err = cdev_add(&watchdog->cdev, devno, 1);
  337. if (err) {
  338. pr_err("watchdog%d unable to add device %d:%d\n",
  339. watchdog->id, MAJOR(watchdog_devt), watchdog->id);
  340. if (watchdog->id == 0) {
  341. misc_deregister(&watchdog_miscdev);
  342. old_wdd = NULL;
  343. }
  344. }
  345. return err;
  346. }
  347. /*
  348. * watchdog_dev_unregister: unregister a watchdog device
  349. * @watchdog: watchdog device
  350. *
  351. * Unregister the watchdog and if needed the legacy /dev/watchdog device.
  352. */
  353. int watchdog_dev_unregister(struct watchdog_device *watchdog)
  354. {
  355. cdev_del(&watchdog->cdev);
  356. if (watchdog->id == 0) {
  357. misc_deregister(&watchdog_miscdev);
  358. old_wdd = NULL;
  359. }
  360. return 0;
  361. }
  362. /*
  363. * watchdog_dev_init: init dev part of watchdog core
  364. *
  365. * Allocate a range of chardev nodes to use for watchdog devices
  366. */
  367. int __init watchdog_dev_init(void)
  368. {
  369. int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
  370. if (err < 0)
  371. pr_err("watchdog: unable to allocate char dev region\n");
  372. return err;
  373. }
  374. /*
  375. * watchdog_dev_exit: exit dev part of watchdog core
  376. *
  377. * Release the range of chardev nodes used for watchdog devices
  378. */
  379. void __exit watchdog_dev_exit(void)
  380. {
  381. unregister_chrdev_region(watchdog_devt, MAX_DOGS);
  382. }