watchdog_dev.c 14 KB

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