watchdog_dev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. int err = 0;
  59. if (!watchdog_active(wddev))
  60. goto out_ping;
  61. if (wddev->ops->ping)
  62. err = wddev->ops->ping(wddev); /* ping the watchdog */
  63. else
  64. err = wddev->ops->start(wddev); /* restart watchdog */
  65. out_ping:
  66. return err;
  67. }
  68. /*
  69. * watchdog_start: wrapper to start the watchdog.
  70. * @wddev: the watchdog device to start
  71. *
  72. * Start the watchdog if it is not active and mark it active.
  73. * This function returns zero on success or a negative errno code for
  74. * failure.
  75. */
  76. static int watchdog_start(struct watchdog_device *wddev)
  77. {
  78. int err = 0;
  79. if (watchdog_active(wddev))
  80. goto out_start;
  81. err = wddev->ops->start(wddev);
  82. if (err == 0)
  83. set_bit(WDOG_ACTIVE, &wddev->status);
  84. out_start:
  85. return err;
  86. }
  87. /*
  88. * watchdog_stop: wrapper to stop the watchdog.
  89. * @wddev: the watchdog device to stop
  90. *
  91. * Stop the watchdog if it is still active and unmark it active.
  92. * This function returns zero on success or a negative errno code for
  93. * failure.
  94. * If the 'nowayout' feature was set, the watchdog cannot be stopped.
  95. */
  96. static int watchdog_stop(struct watchdog_device *wddev)
  97. {
  98. int err = 0;
  99. if (!watchdog_active(wddev))
  100. goto out_stop;
  101. if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) {
  102. dev_info(wddev->dev, "nowayout prevents watchdog being stopped!\n");
  103. err = -EBUSY;
  104. goto out_stop;
  105. }
  106. err = wddev->ops->stop(wddev);
  107. if (err == 0)
  108. clear_bit(WDOG_ACTIVE, &wddev->status);
  109. out_stop:
  110. return err;
  111. }
  112. /*
  113. * watchdog_get_status: wrapper to get the watchdog status
  114. * @wddev: the watchdog device to get the status from
  115. * @status: the status of the watchdog device
  116. *
  117. * Get the watchdog's status flags.
  118. */
  119. static int watchdog_get_status(struct watchdog_device *wddev,
  120. unsigned int *status)
  121. {
  122. int err = 0;
  123. *status = 0;
  124. if (!wddev->ops->status)
  125. return -EOPNOTSUPP;
  126. *status = wddev->ops->status(wddev);
  127. return err;
  128. }
  129. /*
  130. * watchdog_set_timeout: set the watchdog timer timeout
  131. * @wddev: the watchdog device to set the timeout for
  132. * @timeout: timeout to set in seconds
  133. */
  134. static int watchdog_set_timeout(struct watchdog_device *wddev,
  135. unsigned int timeout)
  136. {
  137. int err;
  138. if ((wddev->ops->set_timeout == NULL) ||
  139. !(wddev->info->options & WDIOF_SETTIMEOUT))
  140. return -EOPNOTSUPP;
  141. if ((wddev->max_timeout != 0) &&
  142. (timeout < wddev->min_timeout || timeout > wddev->max_timeout))
  143. return -EINVAL;
  144. err = wddev->ops->set_timeout(wddev, timeout);
  145. return err;
  146. }
  147. /*
  148. * watchdog_get_timeleft: wrapper to get the time left before a reboot
  149. * @wddev: the watchdog device to get the remaining time from
  150. * @timeleft: the time that's left
  151. *
  152. * Get the time before a watchdog will reboot (if not pinged).
  153. */
  154. static int watchdog_get_timeleft(struct watchdog_device *wddev,
  155. unsigned int *timeleft)
  156. {
  157. int err = 0;
  158. *timeleft = 0;
  159. if (!wddev->ops->get_timeleft)
  160. return -EOPNOTSUPP;
  161. *timeleft = wddev->ops->get_timeleft(wddev);
  162. return err;
  163. }
  164. /*
  165. * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
  166. * @wddev: the watchdog device to do the ioctl on
  167. * @cmd: watchdog command
  168. * @arg: argument pointer
  169. */
  170. static int watchdog_ioctl_op(struct watchdog_device *wddev, unsigned int cmd,
  171. unsigned long arg)
  172. {
  173. int err;
  174. if (!wddev->ops->ioctl)
  175. return -ENOIOCTLCMD;
  176. err = wddev->ops->ioctl(wddev, cmd, arg);
  177. return err;
  178. }
  179. /*
  180. * watchdog_write: writes to the watchdog.
  181. * @file: file from VFS
  182. * @data: user address of data
  183. * @len: length of data
  184. * @ppos: pointer to the file offset
  185. *
  186. * A write to a watchdog device is defined as a keepalive ping.
  187. * Writing the magic 'V' sequence allows the next close to turn
  188. * off the watchdog (if 'nowayout' is not set).
  189. */
  190. static ssize_t watchdog_write(struct file *file, const char __user *data,
  191. size_t len, loff_t *ppos)
  192. {
  193. struct watchdog_device *wdd = file->private_data;
  194. size_t i;
  195. char c;
  196. if (len == 0)
  197. return 0;
  198. /*
  199. * Note: just in case someone wrote the magic character
  200. * five months ago...
  201. */
  202. clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
  203. /* scan to see whether or not we got the magic character */
  204. for (i = 0; i != len; i++) {
  205. if (get_user(c, data + i))
  206. return -EFAULT;
  207. if (c == 'V')
  208. set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
  209. }
  210. /* someone wrote to us, so we send the watchdog a keepalive ping */
  211. watchdog_ping(wdd);
  212. return len;
  213. }
  214. /*
  215. * watchdog_ioctl: handle the different ioctl's for the watchdog device.
  216. * @file: file handle to the device
  217. * @cmd: watchdog command
  218. * @arg: argument pointer
  219. *
  220. * The watchdog API defines a common set of functions for all watchdogs
  221. * according to their available features.
  222. */
  223. static long watchdog_ioctl(struct file *file, unsigned int cmd,
  224. unsigned long arg)
  225. {
  226. struct watchdog_device *wdd = file->private_data;
  227. void __user *argp = (void __user *)arg;
  228. int __user *p = argp;
  229. unsigned int val;
  230. int err;
  231. err = watchdog_ioctl_op(wdd, cmd, arg);
  232. if (err != -ENOIOCTLCMD)
  233. return err;
  234. switch (cmd) {
  235. case WDIOC_GETSUPPORT:
  236. return copy_to_user(argp, wdd->info,
  237. sizeof(struct watchdog_info)) ? -EFAULT : 0;
  238. case WDIOC_GETSTATUS:
  239. err = watchdog_get_status(wdd, &val);
  240. if (err)
  241. return err;
  242. return put_user(val, p);
  243. case WDIOC_GETBOOTSTATUS:
  244. return put_user(wdd->bootstatus, p);
  245. case WDIOC_SETOPTIONS:
  246. if (get_user(val, p))
  247. return -EFAULT;
  248. if (val & WDIOS_DISABLECARD) {
  249. err = watchdog_stop(wdd);
  250. if (err < 0)
  251. return err;
  252. }
  253. if (val & WDIOS_ENABLECARD) {
  254. err = watchdog_start(wdd);
  255. if (err < 0)
  256. return err;
  257. }
  258. return 0;
  259. case WDIOC_KEEPALIVE:
  260. if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
  261. return -EOPNOTSUPP;
  262. watchdog_ping(wdd);
  263. return 0;
  264. case WDIOC_SETTIMEOUT:
  265. if (get_user(val, p))
  266. return -EFAULT;
  267. err = watchdog_set_timeout(wdd, val);
  268. if (err < 0)
  269. return err;
  270. /* If the watchdog is active then we send a keepalive ping
  271. * to make sure that the watchdog keep's running (and if
  272. * possible that it takes the new timeout) */
  273. watchdog_ping(wdd);
  274. /* Fall */
  275. case WDIOC_GETTIMEOUT:
  276. /* timeout == 0 means that we don't know the timeout */
  277. if (wdd->timeout == 0)
  278. return -EOPNOTSUPP;
  279. return put_user(wdd->timeout, p);
  280. case WDIOC_GETTIMELEFT:
  281. err = watchdog_get_timeleft(wdd, &val);
  282. if (err)
  283. return err;
  284. return put_user(val, p);
  285. default:
  286. return -ENOTTY;
  287. }
  288. }
  289. /*
  290. * watchdog_open: open the /dev/watchdog* devices.
  291. * @inode: inode of device
  292. * @file: file handle to device
  293. *
  294. * When the /dev/watchdog* device gets opened, we start the watchdog.
  295. * Watch out: the /dev/watchdog device is single open, so we make sure
  296. * it can only be opened once.
  297. */
  298. static int watchdog_open(struct inode *inode, struct file *file)
  299. {
  300. int err = -EBUSY;
  301. struct watchdog_device *wdd;
  302. /* Get the corresponding watchdog device */
  303. if (imajor(inode) == MISC_MAJOR)
  304. wdd = old_wdd;
  305. else
  306. wdd = container_of(inode->i_cdev, struct watchdog_device, cdev);
  307. /* the watchdog is single open! */
  308. if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
  309. return -EBUSY;
  310. /*
  311. * If the /dev/watchdog device is open, we don't want the module
  312. * to be unloaded.
  313. */
  314. if (!try_module_get(wdd->ops->owner))
  315. goto out;
  316. err = watchdog_start(wdd);
  317. if (err < 0)
  318. goto out_mod;
  319. file->private_data = wdd;
  320. /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
  321. return nonseekable_open(inode, file);
  322. out_mod:
  323. module_put(wdd->ops->owner);
  324. out:
  325. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  326. return err;
  327. }
  328. /*
  329. * watchdog_release: release the watchdog device.
  330. * @inode: inode of device
  331. * @file: file handle to device
  332. *
  333. * This is the code for when /dev/watchdog gets closed. We will only
  334. * stop the watchdog when we have received the magic char (and nowayout
  335. * was not set), else the watchdog will keep running.
  336. */
  337. static int watchdog_release(struct inode *inode, struct file *file)
  338. {
  339. struct watchdog_device *wdd = file->private_data;
  340. int err = -EBUSY;
  341. /*
  342. * We only stop the watchdog if we received the magic character
  343. * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
  344. * watchdog_stop will fail.
  345. */
  346. if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
  347. !(wdd->info->options & WDIOF_MAGICCLOSE))
  348. err = watchdog_stop(wdd);
  349. /* If the watchdog was not stopped, send a keepalive ping */
  350. if (err < 0) {
  351. dev_crit(wdd->dev, "watchdog did not stop!\n");
  352. watchdog_ping(wdd);
  353. }
  354. /* Allow the owner module to be unloaded again */
  355. module_put(wdd->ops->owner);
  356. /* make sure that /dev/watchdog can be re-opened */
  357. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  358. return 0;
  359. }
  360. static const struct file_operations watchdog_fops = {
  361. .owner = THIS_MODULE,
  362. .write = watchdog_write,
  363. .unlocked_ioctl = watchdog_ioctl,
  364. .open = watchdog_open,
  365. .release = watchdog_release,
  366. };
  367. static struct miscdevice watchdog_miscdev = {
  368. .minor = WATCHDOG_MINOR,
  369. .name = "watchdog",
  370. .fops = &watchdog_fops,
  371. };
  372. /*
  373. * watchdog_dev_register: register a watchdog device
  374. * @watchdog: watchdog device
  375. *
  376. * Register a watchdog device including handling the legacy
  377. * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
  378. * thus we set it up like that.
  379. */
  380. int watchdog_dev_register(struct watchdog_device *watchdog)
  381. {
  382. int err, devno;
  383. if (watchdog->id == 0) {
  384. watchdog_miscdev.parent = watchdog->parent;
  385. err = misc_register(&watchdog_miscdev);
  386. if (err != 0) {
  387. pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
  388. watchdog->info->identity, WATCHDOG_MINOR, err);
  389. if (err == -EBUSY)
  390. pr_err("%s: a legacy watchdog module is probably present.\n",
  391. watchdog->info->identity);
  392. return err;
  393. }
  394. old_wdd = watchdog;
  395. }
  396. /* Fill in the data structures */
  397. devno = MKDEV(MAJOR(watchdog_devt), watchdog->id);
  398. cdev_init(&watchdog->cdev, &watchdog_fops);
  399. watchdog->cdev.owner = watchdog->ops->owner;
  400. /* Add the device */
  401. err = cdev_add(&watchdog->cdev, devno, 1);
  402. if (err) {
  403. pr_err("watchdog%d unable to add device %d:%d\n",
  404. watchdog->id, MAJOR(watchdog_devt), watchdog->id);
  405. if (watchdog->id == 0) {
  406. misc_deregister(&watchdog_miscdev);
  407. old_wdd = NULL;
  408. }
  409. }
  410. return err;
  411. }
  412. /*
  413. * watchdog_dev_unregister: unregister a watchdog device
  414. * @watchdog: watchdog device
  415. *
  416. * Unregister the watchdog and if needed the legacy /dev/watchdog device.
  417. */
  418. int watchdog_dev_unregister(struct watchdog_device *watchdog)
  419. {
  420. cdev_del(&watchdog->cdev);
  421. if (watchdog->id == 0) {
  422. misc_deregister(&watchdog_miscdev);
  423. old_wdd = NULL;
  424. }
  425. return 0;
  426. }
  427. /*
  428. * watchdog_dev_init: init dev part of watchdog core
  429. *
  430. * Allocate a range of chardev nodes to use for watchdog devices
  431. */
  432. int __init watchdog_dev_init(void)
  433. {
  434. int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
  435. if (err < 0)
  436. pr_err("watchdog: unable to allocate char dev region\n");
  437. return err;
  438. }
  439. /*
  440. * watchdog_dev_exit: exit dev part of watchdog core
  441. *
  442. * Release the range of chardev nodes used for watchdog devices
  443. */
  444. void __exit watchdog_dev_exit(void)
  445. {
  446. unregister_chrdev_region(watchdog_devt, MAX_DOGS);
  447. }