watchdog_dev.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. * We only ping when the watchdog device is running.
  54. */
  55. static int watchdog_ping(struct watchdog_device *wddev)
  56. {
  57. if (test_bit(WDOG_ACTIVE, &wdd->status)) {
  58. if (wddev->ops->ping)
  59. return wddev->ops->ping(wddev); /* ping the watchdog */
  60. else
  61. return wddev->ops->start(wddev); /* restart watchdog */
  62. }
  63. return 0;
  64. }
  65. /*
  66. * watchdog_start: wrapper to start the watchdog.
  67. * @wddev: the watchdog device to start
  68. *
  69. * Start the watchdog if it is not active and mark it active.
  70. * This function returns zero on success or a negative errno code for
  71. * failure.
  72. */
  73. static int watchdog_start(struct watchdog_device *wddev)
  74. {
  75. int err;
  76. if (!test_bit(WDOG_ACTIVE, &wdd->status)) {
  77. err = wddev->ops->start(wddev);
  78. if (err < 0)
  79. return err;
  80. set_bit(WDOG_ACTIVE, &wdd->status);
  81. }
  82. return 0;
  83. }
  84. /*
  85. * watchdog_stop: wrapper to stop the watchdog.
  86. * @wddev: the watchdog device to stop
  87. *
  88. * Stop the watchdog if it is still active and unmark it active.
  89. * This function returns zero on success or a negative errno code for
  90. * failure.
  91. */
  92. static int watchdog_stop(struct watchdog_device *wddev)
  93. {
  94. int err;
  95. if (test_bit(WDOG_ACTIVE, &wdd->status)) {
  96. err = wddev->ops->stop(wddev);
  97. if (err < 0)
  98. return err;
  99. clear_bit(WDOG_ACTIVE, &wdd->status);
  100. }
  101. return 0;
  102. }
  103. /*
  104. * watchdog_write: writes to the watchdog.
  105. * @file: file from VFS
  106. * @data: user address of data
  107. * @len: length of data
  108. * @ppos: pointer to the file offset
  109. *
  110. * A write to a watchdog device is defined as a keepalive ping.
  111. */
  112. static ssize_t watchdog_write(struct file *file, const char __user *data,
  113. size_t len, loff_t *ppos)
  114. {
  115. size_t i;
  116. char c;
  117. if (len == 0)
  118. return 0;
  119. for (i = 0; i != len; i++) {
  120. if (get_user(c, data + i))
  121. return -EFAULT;
  122. }
  123. /* someone wrote to us, so we send the watchdog a keepalive ping */
  124. watchdog_ping(wdd);
  125. return len;
  126. }
  127. /*
  128. * watchdog_ioctl: handle the different ioctl's for the watchdog device.
  129. * @file: file handle to the device
  130. * @cmd: watchdog command
  131. * @arg: argument pointer
  132. *
  133. * The watchdog API defines a common set of functions for all watchdogs
  134. * according to their available features.
  135. */
  136. static long watchdog_ioctl(struct file *file, unsigned int cmd,
  137. unsigned long arg)
  138. {
  139. void __user *argp = (void __user *)arg;
  140. int __user *p = argp;
  141. unsigned int val;
  142. int err;
  143. switch (cmd) {
  144. case WDIOC_GETSUPPORT:
  145. return copy_to_user(argp, wdd->info,
  146. sizeof(struct watchdog_info)) ? -EFAULT : 0;
  147. case WDIOC_GETSTATUS:
  148. val = wdd->ops->status ? wdd->ops->status(wdd) : 0;
  149. return put_user(val, p);
  150. case WDIOC_GETBOOTSTATUS:
  151. return put_user(wdd->bootstatus, p);
  152. case WDIOC_SETOPTIONS:
  153. if (get_user(val, p))
  154. return -EFAULT;
  155. if (val & WDIOS_DISABLECARD) {
  156. err = watchdog_stop(wdd);
  157. if (err < 0)
  158. return err;
  159. }
  160. if (val & WDIOS_ENABLECARD) {
  161. err = watchdog_start(wdd);
  162. if (err < 0)
  163. return err;
  164. }
  165. return 0;
  166. case WDIOC_KEEPALIVE:
  167. if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
  168. return -EOPNOTSUPP;
  169. watchdog_ping(wdd);
  170. return 0;
  171. default:
  172. return -ENOTTY;
  173. }
  174. }
  175. /*
  176. * watchdog_open: open the /dev/watchdog device.
  177. * @inode: inode of device
  178. * @file: file handle to device
  179. *
  180. * When the /dev/watchdog device gets opened, we start the watchdog.
  181. * Watch out: the /dev/watchdog device is single open, so we make sure
  182. * it can only be opened once.
  183. */
  184. static int watchdog_open(struct inode *inode, struct file *file)
  185. {
  186. int err = -EBUSY;
  187. /* the watchdog is single open! */
  188. if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
  189. return -EBUSY;
  190. /*
  191. * If the /dev/watchdog device is open, we don't want the module
  192. * to be unloaded.
  193. */
  194. if (!try_module_get(wdd->ops->owner))
  195. goto out;
  196. err = watchdog_start(wdd);
  197. if (err < 0)
  198. goto out_mod;
  199. /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
  200. return nonseekable_open(inode, file);
  201. out_mod:
  202. module_put(wdd->ops->owner);
  203. out:
  204. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  205. return err;
  206. }
  207. /*
  208. * watchdog_release: release the /dev/watchdog device.
  209. * @inode: inode of device
  210. * @file: file handle to device
  211. *
  212. * This is the code for when /dev/watchdog gets closed.
  213. */
  214. static int watchdog_release(struct inode *inode, struct file *file)
  215. {
  216. int err;
  217. err = watchdog_stop(wdd);
  218. if (err < 0) {
  219. pr_crit("%s: watchdog did not stop!\n", wdd->info->identity);
  220. watchdog_ping(wdd);
  221. }
  222. /* Allow the owner module to be unloaded again */
  223. module_put(wdd->ops->owner);
  224. /* make sure that /dev/watchdog can be re-opened */
  225. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  226. return 0;
  227. }
  228. static const struct file_operations watchdog_fops = {
  229. .owner = THIS_MODULE,
  230. .write = watchdog_write,
  231. .unlocked_ioctl = watchdog_ioctl,
  232. .open = watchdog_open,
  233. .release = watchdog_release,
  234. };
  235. static struct miscdevice watchdog_miscdev = {
  236. .minor = WATCHDOG_MINOR,
  237. .name = "watchdog",
  238. .fops = &watchdog_fops,
  239. };
  240. /*
  241. * watchdog_dev_register:
  242. * @watchdog: watchdog device
  243. *
  244. * Register a watchdog device as /dev/watchdog. /dev/watchdog
  245. * is actually a miscdevice and thus we set it up like that.
  246. */
  247. int watchdog_dev_register(struct watchdog_device *watchdog)
  248. {
  249. int err;
  250. /* Only one device can register for /dev/watchdog */
  251. if (test_and_set_bit(0, &watchdog_dev_busy)) {
  252. pr_err("only one watchdog can use /dev/watchdog.\n");
  253. return -EBUSY;
  254. }
  255. wdd = watchdog;
  256. err = misc_register(&watchdog_miscdev);
  257. if (err != 0) {
  258. pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
  259. watchdog->info->identity, WATCHDOG_MINOR, err);
  260. goto out;
  261. }
  262. return 0;
  263. out:
  264. wdd = NULL;
  265. clear_bit(0, &watchdog_dev_busy);
  266. return err;
  267. }
  268. /*
  269. * watchdog_dev_unregister:
  270. * @watchdog: watchdog device
  271. *
  272. * Deregister the /dev/watchdog device.
  273. */
  274. int watchdog_dev_unregister(struct watchdog_device *watchdog)
  275. {
  276. /* Check that a watchdog device was registered in the past */
  277. if (!test_bit(0, &watchdog_dev_busy) || !wdd)
  278. return -ENODEV;
  279. /* We can only unregister the watchdog device that was registered */
  280. if (watchdog != wdd) {
  281. pr_err("%s: watchdog was not registered as /dev/watchdog.\n",
  282. watchdog->info->identity);
  283. return -ENODEV;
  284. }
  285. misc_deregister(&watchdog_miscdev);
  286. wdd = NULL;
  287. clear_bit(0, &watchdog_dev_busy);
  288. return 0;
  289. }