watchdog_core.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * watchdog_core.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. * This source code is part of the generic code that can be used
  10. * by all the watchdog timer drivers.
  11. *
  12. * Based on source code of the following authors:
  13. * Matt Domsch <Matt_Domsch@dell.com>,
  14. * Rob Radez <rob@osinvestor.com>,
  15. * Rusty Lynch <rusty@linux.co.intel.com>
  16. * Satyam Sharma <satyam@infradead.org>
  17. * Randy Dunlap <randy.dunlap@oracle.com>
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License
  21. * as published by the Free Software Foundation; either version
  22. * 2 of the License, or (at your option) any later version.
  23. *
  24. * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
  25. * admit liability nor provide warranty for any of this software.
  26. * This material is provided "AS-IS" and at no charge.
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/module.h> /* For EXPORT_SYMBOL/module stuff/... */
  30. #include <linux/types.h> /* For standard types */
  31. #include <linux/errno.h> /* For the -ENODEV/... values */
  32. #include <linux/kernel.h> /* For printk/panic/... */
  33. #include <linux/watchdog.h> /* For watchdog specific items */
  34. #include <linux/init.h> /* For __init/__exit/... */
  35. #include <linux/idr.h> /* For ida_* macros */
  36. #include <linux/err.h> /* For IS_ERR macros */
  37. #include <linux/of.h> /* For of_get_timeout_sec */
  38. #include "watchdog_core.h" /* For watchdog_dev_register/... */
  39. static DEFINE_IDA(watchdog_ida);
  40. static struct class *watchdog_class;
  41. static void watchdog_check_min_max_timeout(struct watchdog_device *wdd)
  42. {
  43. /*
  44. * Check that we have valid min and max timeout values, if
  45. * not reset them both to 0 (=not used or unknown)
  46. */
  47. if (wdd->min_timeout > wdd->max_timeout) {
  48. pr_info("Invalid min and max timeout values, resetting to 0!\n");
  49. wdd->min_timeout = 0;
  50. wdd->max_timeout = 0;
  51. }
  52. }
  53. /**
  54. * watchdog_init_timeout() - initialize the timeout field
  55. * @timeout_parm: timeout module parameter
  56. * @dev: Device that stores the timeout-sec property
  57. *
  58. * Initialize the timeout field of the watchdog_device struct with either the
  59. * timeout module parameter (if it is valid value) or the timeout-sec property
  60. * (only if it is a valid value and the timeout_parm is out of bounds).
  61. * If none of them are valid then we keep the old value (which should normally
  62. * be the default timeout value.
  63. *
  64. * A zero is returned on success and -EINVAL for failure.
  65. */
  66. int watchdog_init_timeout(struct watchdog_device *wdd,
  67. unsigned int timeout_parm, struct device *dev)
  68. {
  69. unsigned int t = 0;
  70. int ret = 0;
  71. watchdog_check_min_max_timeout(wdd);
  72. /* try to get the tiemout module parameter first */
  73. if (!watchdog_timeout_invalid(wdd, timeout_parm)) {
  74. wdd->timeout = timeout_parm;
  75. return ret;
  76. }
  77. if (timeout_parm)
  78. ret = -EINVAL;
  79. /* try to get the timeout_sec property */
  80. if (dev == NULL || dev->of_node == NULL)
  81. return ret;
  82. of_property_read_u32(dev->of_node, "timeout-sec", &t);
  83. if (!watchdog_timeout_invalid(wdd, t))
  84. wdd->timeout = t;
  85. else
  86. ret = -EINVAL;
  87. return ret;
  88. }
  89. EXPORT_SYMBOL_GPL(watchdog_init_timeout);
  90. /**
  91. * watchdog_register_device() - register a watchdog device
  92. * @wdd: watchdog device
  93. *
  94. * Register a watchdog device with the kernel so that the
  95. * watchdog timer can be accessed from userspace.
  96. *
  97. * A zero is returned on success and a negative errno code for
  98. * failure.
  99. */
  100. int watchdog_register_device(struct watchdog_device *wdd)
  101. {
  102. int ret, id, devno;
  103. if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
  104. return -EINVAL;
  105. /* Mandatory operations need to be supported */
  106. if (wdd->ops->start == NULL || wdd->ops->stop == NULL)
  107. return -EINVAL;
  108. watchdog_check_min_max_timeout(wdd);
  109. /*
  110. * Note: now that all watchdog_device data has been verified, we
  111. * will not check this anymore in other functions. If data gets
  112. * corrupted in a later stage then we expect a kernel panic!
  113. */
  114. mutex_init(&wdd->lock);
  115. id = ida_simple_get(&watchdog_ida, 0, MAX_DOGS, GFP_KERNEL);
  116. if (id < 0)
  117. return id;
  118. wdd->id = id;
  119. ret = watchdog_dev_register(wdd);
  120. if (ret) {
  121. ida_simple_remove(&watchdog_ida, id);
  122. if (!(id == 0 && ret == -EBUSY))
  123. return ret;
  124. /* Retry in case a legacy watchdog module exists */
  125. id = ida_simple_get(&watchdog_ida, 1, MAX_DOGS, GFP_KERNEL);
  126. if (id < 0)
  127. return id;
  128. wdd->id = id;
  129. ret = watchdog_dev_register(wdd);
  130. if (ret) {
  131. ida_simple_remove(&watchdog_ida, id);
  132. return ret;
  133. }
  134. }
  135. devno = wdd->cdev.dev;
  136. wdd->dev = device_create(watchdog_class, wdd->parent, devno,
  137. NULL, "watchdog%d", wdd->id);
  138. if (IS_ERR(wdd->dev)) {
  139. watchdog_dev_unregister(wdd);
  140. ida_simple_remove(&watchdog_ida, id);
  141. ret = PTR_ERR(wdd->dev);
  142. return ret;
  143. }
  144. return 0;
  145. }
  146. EXPORT_SYMBOL_GPL(watchdog_register_device);
  147. /**
  148. * watchdog_unregister_device() - unregister a watchdog device
  149. * @wdd: watchdog device to unregister
  150. *
  151. * Unregister a watchdog device that was previously successfully
  152. * registered with watchdog_register_device().
  153. */
  154. void watchdog_unregister_device(struct watchdog_device *wdd)
  155. {
  156. int ret;
  157. int devno;
  158. if (wdd == NULL)
  159. return;
  160. devno = wdd->cdev.dev;
  161. ret = watchdog_dev_unregister(wdd);
  162. if (ret)
  163. pr_err("error unregistering /dev/watchdog (err=%d)\n", ret);
  164. device_destroy(watchdog_class, devno);
  165. ida_simple_remove(&watchdog_ida, wdd->id);
  166. wdd->dev = NULL;
  167. }
  168. EXPORT_SYMBOL_GPL(watchdog_unregister_device);
  169. static int __init watchdog_init(void)
  170. {
  171. int err;
  172. watchdog_class = class_create(THIS_MODULE, "watchdog");
  173. if (IS_ERR(watchdog_class)) {
  174. pr_err("couldn't create class\n");
  175. return PTR_ERR(watchdog_class);
  176. }
  177. err = watchdog_dev_init();
  178. if (err < 0) {
  179. class_destroy(watchdog_class);
  180. return err;
  181. }
  182. return 0;
  183. }
  184. static void __exit watchdog_exit(void)
  185. {
  186. watchdog_dev_exit();
  187. class_destroy(watchdog_class);
  188. ida_destroy(&watchdog_ida);
  189. }
  190. subsys_initcall(watchdog_init);
  191. module_exit(watchdog_exit);
  192. MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
  193. MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
  194. MODULE_DESCRIPTION("WatchDog Timer Driver Core");
  195. MODULE_LICENSE("GPL");