watchdog_dev.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /* make sure we only register one /dev/watchdog device */
  43. static unsigned long watchdog_dev_busy;
  44. /* the watchdog device behind /dev/watchdog */
  45. static struct watchdog_device *wdd;
  46. /*
  47. * watchdog_ping: ping the watchdog.
  48. * @wddev: the watchdog device to ping
  49. *
  50. * If the watchdog has no own ping operation then it needs to be
  51. * restarted via the start operation. This wrapper function does
  52. * exactly that.
  53. */
  54. static int watchdog_ping(struct watchdog_device *wddev)
  55. {
  56. if (wddev->ops->ping)
  57. return wddev->ops->ping(wddev); /* ping the watchdog */
  58. else
  59. return wddev->ops->start(wddev); /* restart the watchdog */
  60. }
  61. /*
  62. * watchdog_write: writes to the watchdog.
  63. * @file: file from VFS
  64. * @data: user address of data
  65. * @len: length of data
  66. * @ppos: pointer to the file offset
  67. *
  68. * A write to a watchdog device is defined as a keepalive ping.
  69. */
  70. static ssize_t watchdog_write(struct file *file, const char __user *data,
  71. size_t len, loff_t *ppos)
  72. {
  73. size_t i;
  74. char c;
  75. if (len == 0)
  76. return 0;
  77. for (i = 0; i != len; i++) {
  78. if (get_user(c, data + i))
  79. return -EFAULT;
  80. }
  81. /* someone wrote to us, so we send the watchdog a keepalive ping */
  82. watchdog_ping(wdd);
  83. return len;
  84. }
  85. /*
  86. * watchdog_open: open the /dev/watchdog device.
  87. * @inode: inode of device
  88. * @file: file handle to device
  89. *
  90. * When the /dev/watchdog device gets opened, we start the watchdog.
  91. * Watch out: the /dev/watchdog device is single open, so we make sure
  92. * it can only be opened once.
  93. */
  94. static int watchdog_open(struct inode *inode, struct file *file)
  95. {
  96. int err = -EBUSY;
  97. /* the watchdog is single open! */
  98. if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
  99. return -EBUSY;
  100. /*
  101. * If the /dev/watchdog device is open, we don't want the module
  102. * to be unloaded.
  103. */
  104. if (!try_module_get(wdd->ops->owner))
  105. goto out;
  106. err = wdd->ops->start(wdd);
  107. if (err < 0)
  108. goto out_mod;
  109. /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
  110. return nonseekable_open(inode, file);
  111. out_mod:
  112. module_put(wdd->ops->owner);
  113. out:
  114. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  115. return err;
  116. }
  117. /*
  118. * watchdog_release: release the /dev/watchdog device.
  119. * @inode: inode of device
  120. * @file: file handle to device
  121. *
  122. * This is the code for when /dev/watchdog gets closed.
  123. */
  124. static int watchdog_release(struct inode *inode, struct file *file)
  125. {
  126. int err;
  127. err = wdd->ops->stop(wdd);
  128. if (err != 0) {
  129. pr_crit("%s: watchdog did not stop!\n", wdd->info->identity);
  130. watchdog_ping(wdd);
  131. }
  132. /* Allow the owner module to be unloaded again */
  133. module_put(wdd->ops->owner);
  134. /* make sure that /dev/watchdog can be re-opened */
  135. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  136. return 0;
  137. }
  138. static const struct file_operations watchdog_fops = {
  139. .owner = THIS_MODULE,
  140. .write = watchdog_write,
  141. .open = watchdog_open,
  142. .release = watchdog_release,
  143. };
  144. static struct miscdevice watchdog_miscdev = {
  145. .minor = WATCHDOG_MINOR,
  146. .name = "watchdog",
  147. .fops = &watchdog_fops,
  148. };
  149. /*
  150. * watchdog_dev_register:
  151. * @watchdog: watchdog device
  152. *
  153. * Register a watchdog device as /dev/watchdog. /dev/watchdog
  154. * is actually a miscdevice and thus we set it up like that.
  155. */
  156. int watchdog_dev_register(struct watchdog_device *watchdog)
  157. {
  158. int err;
  159. /* Only one device can register for /dev/watchdog */
  160. if (test_and_set_bit(0, &watchdog_dev_busy)) {
  161. pr_err("only one watchdog can use /dev/watchdog.\n");
  162. return -EBUSY;
  163. }
  164. wdd = watchdog;
  165. err = misc_register(&watchdog_miscdev);
  166. if (err != 0) {
  167. pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
  168. watchdog->info->identity, WATCHDOG_MINOR, err);
  169. goto out;
  170. }
  171. return 0;
  172. out:
  173. wdd = NULL;
  174. clear_bit(0, &watchdog_dev_busy);
  175. return err;
  176. }
  177. /*
  178. * watchdog_dev_unregister:
  179. * @watchdog: watchdog device
  180. *
  181. * Deregister the /dev/watchdog device.
  182. */
  183. int watchdog_dev_unregister(struct watchdog_device *watchdog)
  184. {
  185. /* Check that a watchdog device was registered in the past */
  186. if (!test_bit(0, &watchdog_dev_busy) || !wdd)
  187. return -ENODEV;
  188. /* We can only unregister the watchdog device that was registered */
  189. if (watchdog != wdd) {
  190. pr_err("%s: watchdog was not registered as /dev/watchdog.\n",
  191. watchdog->info->identity);
  192. return -ENODEV;
  193. }
  194. misc_deregister(&watchdog_miscdev);
  195. wdd = NULL;
  196. clear_bit(0, &watchdog_dev_busy);
  197. return 0;
  198. }