watchdog_dev.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. * Writing the magic 'V' sequence allows the next close to turn
  112. * off the watchdog.
  113. */
  114. static ssize_t watchdog_write(struct file *file, const char __user *data,
  115. size_t len, loff_t *ppos)
  116. {
  117. size_t i;
  118. char c;
  119. if (len == 0)
  120. return 0;
  121. /*
  122. * Note: just in case someone wrote the magic character
  123. * five months ago...
  124. */
  125. clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
  126. /* scan to see whether or not we got the magic character */
  127. for (i = 0; i != len; i++) {
  128. if (get_user(c, data + i))
  129. return -EFAULT;
  130. if (c == 'V')
  131. set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
  132. }
  133. /* someone wrote to us, so we send the watchdog a keepalive ping */
  134. watchdog_ping(wdd);
  135. return len;
  136. }
  137. /*
  138. * watchdog_ioctl: handle the different ioctl's for the watchdog device.
  139. * @file: file handle to the device
  140. * @cmd: watchdog command
  141. * @arg: argument pointer
  142. *
  143. * The watchdog API defines a common set of functions for all watchdogs
  144. * according to their available features.
  145. */
  146. static long watchdog_ioctl(struct file *file, unsigned int cmd,
  147. unsigned long arg)
  148. {
  149. void __user *argp = (void __user *)arg;
  150. int __user *p = argp;
  151. unsigned int val;
  152. int err;
  153. switch (cmd) {
  154. case WDIOC_GETSUPPORT:
  155. return copy_to_user(argp, wdd->info,
  156. sizeof(struct watchdog_info)) ? -EFAULT : 0;
  157. case WDIOC_GETSTATUS:
  158. val = wdd->ops->status ? wdd->ops->status(wdd) : 0;
  159. return put_user(val, p);
  160. case WDIOC_GETBOOTSTATUS:
  161. return put_user(wdd->bootstatus, p);
  162. case WDIOC_SETOPTIONS:
  163. if (get_user(val, p))
  164. return -EFAULT;
  165. if (val & WDIOS_DISABLECARD) {
  166. err = watchdog_stop(wdd);
  167. if (err < 0)
  168. return err;
  169. }
  170. if (val & WDIOS_ENABLECARD) {
  171. err = watchdog_start(wdd);
  172. if (err < 0)
  173. return err;
  174. }
  175. return 0;
  176. case WDIOC_KEEPALIVE:
  177. if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
  178. return -EOPNOTSUPP;
  179. watchdog_ping(wdd);
  180. return 0;
  181. case WDIOC_SETTIMEOUT:
  182. if ((wdd->ops->set_timeout == NULL) ||
  183. !(wdd->info->options & WDIOF_SETTIMEOUT))
  184. return -EOPNOTSUPP;
  185. if (get_user(val, p))
  186. return -EFAULT;
  187. err = wdd->ops->set_timeout(wdd, val);
  188. if (err < 0)
  189. return err;
  190. wdd->timeout = val;
  191. /* If the watchdog is active then we send a keepalive ping
  192. * to make sure that the watchdog keep's running (and if
  193. * possible that it takes the new timeout) */
  194. watchdog_ping(wdd);
  195. /* Fall */
  196. case WDIOC_GETTIMEOUT:
  197. /* timeout == 0 means that we don't know the timeout */
  198. if (wdd->timeout == 0)
  199. return -EOPNOTSUPP;
  200. return put_user(wdd->timeout, p);
  201. default:
  202. return -ENOTTY;
  203. }
  204. }
  205. /*
  206. * watchdog_open: open the /dev/watchdog device.
  207. * @inode: inode of device
  208. * @file: file handle to device
  209. *
  210. * When the /dev/watchdog device gets opened, we start the watchdog.
  211. * Watch out: the /dev/watchdog device is single open, so we make sure
  212. * it can only be opened once.
  213. */
  214. static int watchdog_open(struct inode *inode, struct file *file)
  215. {
  216. int err = -EBUSY;
  217. /* the watchdog is single open! */
  218. if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
  219. return -EBUSY;
  220. /*
  221. * If the /dev/watchdog device is open, we don't want the module
  222. * to be unloaded.
  223. */
  224. if (!try_module_get(wdd->ops->owner))
  225. goto out;
  226. err = watchdog_start(wdd);
  227. if (err < 0)
  228. goto out_mod;
  229. /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
  230. return nonseekable_open(inode, file);
  231. out_mod:
  232. module_put(wdd->ops->owner);
  233. out:
  234. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  235. return err;
  236. }
  237. /*
  238. * watchdog_release: release the /dev/watchdog device.
  239. * @inode: inode of device
  240. * @file: file handle to device
  241. *
  242. * This is the code for when /dev/watchdog gets closed. We will only
  243. * stop the watchdog when we have received the magic char, else the
  244. * watchdog will keep running.
  245. */
  246. static int watchdog_release(struct inode *inode, struct file *file)
  247. {
  248. int err = -EBUSY;
  249. /*
  250. * We only stop the watchdog if we received the magic character
  251. * or if WDIOF_MAGICCLOSE is not set
  252. */
  253. if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
  254. !(wdd->info->options & WDIOF_MAGICCLOSE))
  255. err = watchdog_stop(wdd);
  256. /* If the watchdog was not stopped, send a keepalive ping */
  257. if (err < 0) {
  258. pr_crit("%s: watchdog did not stop!\n", wdd->info->identity);
  259. watchdog_ping(wdd);
  260. }
  261. /* Allow the owner module to be unloaded again */
  262. module_put(wdd->ops->owner);
  263. /* make sure that /dev/watchdog can be re-opened */
  264. clear_bit(WDOG_DEV_OPEN, &wdd->status);
  265. return 0;
  266. }
  267. static const struct file_operations watchdog_fops = {
  268. .owner = THIS_MODULE,
  269. .write = watchdog_write,
  270. .unlocked_ioctl = watchdog_ioctl,
  271. .open = watchdog_open,
  272. .release = watchdog_release,
  273. };
  274. static struct miscdevice watchdog_miscdev = {
  275. .minor = WATCHDOG_MINOR,
  276. .name = "watchdog",
  277. .fops = &watchdog_fops,
  278. };
  279. /*
  280. * watchdog_dev_register:
  281. * @watchdog: watchdog device
  282. *
  283. * Register a watchdog device as /dev/watchdog. /dev/watchdog
  284. * is actually a miscdevice and thus we set it up like that.
  285. */
  286. int watchdog_dev_register(struct watchdog_device *watchdog)
  287. {
  288. int err;
  289. /* Only one device can register for /dev/watchdog */
  290. if (test_and_set_bit(0, &watchdog_dev_busy)) {
  291. pr_err("only one watchdog can use /dev/watchdog.\n");
  292. return -EBUSY;
  293. }
  294. wdd = watchdog;
  295. err = misc_register(&watchdog_miscdev);
  296. if (err != 0) {
  297. pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
  298. watchdog->info->identity, WATCHDOG_MINOR, err);
  299. goto out;
  300. }
  301. return 0;
  302. out:
  303. wdd = NULL;
  304. clear_bit(0, &watchdog_dev_busy);
  305. return err;
  306. }
  307. /*
  308. * watchdog_dev_unregister:
  309. * @watchdog: watchdog device
  310. *
  311. * Deregister the /dev/watchdog device.
  312. */
  313. int watchdog_dev_unregister(struct watchdog_device *watchdog)
  314. {
  315. /* Check that a watchdog device was registered in the past */
  316. if (!test_bit(0, &watchdog_dev_busy) || !wdd)
  317. return -ENODEV;
  318. /* We can only unregister the watchdog device that was registered */
  319. if (watchdog != wdd) {
  320. pr_err("%s: watchdog was not registered as /dev/watchdog.\n",
  321. watchdog->info->identity);
  322. return -ENODEV;
  323. }
  324. misc_deregister(&watchdog_miscdev);
  325. wdd = NULL;
  326. clear_bit(0, &watchdog_dev_busy);
  327. return 0;
  328. }