watchdog_dev.c 14 KB

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