watchdog_dev.c 14 KB

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