watchdog_core.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. /*
  42. * Deferred Registration infrastructure.
  43. *
  44. * Sometimes watchdog drivers needs to be loaded as soon as possible,
  45. * for example when it's impossible to disable it. To do so,
  46. * raising the initcall level of the watchdog driver is a solution.
  47. * But in such case, the miscdev is maybe not ready (subsys_initcall), and
  48. * watchdog_core need miscdev to register the watchdog as a char device.
  49. *
  50. * The deferred registration infrastructure offer a way for the watchdog
  51. * subsystem to register a watchdog properly, even before miscdev is ready.
  52. */
  53. static DEFINE_MUTEX(wtd_deferred_reg_mutex);
  54. static LIST_HEAD(wtd_deferred_reg_list);
  55. static bool wtd_deferred_reg_done;
  56. static int watchdog_deferred_registration_add(struct watchdog_device *wdd)
  57. {
  58. list_add_tail(&wdd->deferred,
  59. &wtd_deferred_reg_list);
  60. return 0;
  61. }
  62. static void watchdog_deferred_registration_del(struct watchdog_device *wdd)
  63. {
  64. struct list_head *p, *n;
  65. struct watchdog_device *wdd_tmp;
  66. list_for_each_safe(p, n, &wtd_deferred_reg_list) {
  67. wdd_tmp = list_entry(p, struct watchdog_device,
  68. deferred);
  69. if (wdd_tmp == wdd) {
  70. list_del(&wdd_tmp->deferred);
  71. break;
  72. }
  73. }
  74. }
  75. static void watchdog_check_min_max_timeout(struct watchdog_device *wdd)
  76. {
  77. /*
  78. * Check that we have valid min and max timeout values, if
  79. * not reset them both to 0 (=not used or unknown)
  80. */
  81. if (wdd->min_timeout > wdd->max_timeout) {
  82. pr_info("Invalid min and max timeout values, resetting to 0!\n");
  83. wdd->min_timeout = 0;
  84. wdd->max_timeout = 0;
  85. }
  86. }
  87. /**
  88. * watchdog_init_timeout() - initialize the timeout field
  89. * @timeout_parm: timeout module parameter
  90. * @dev: Device that stores the timeout-sec property
  91. *
  92. * Initialize the timeout field of the watchdog_device struct with either the
  93. * timeout module parameter (if it is valid value) or the timeout-sec property
  94. * (only if it is a valid value and the timeout_parm is out of bounds).
  95. * If none of them are valid then we keep the old value (which should normally
  96. * be the default timeout value.
  97. *
  98. * A zero is returned on success and -EINVAL for failure.
  99. */
  100. int watchdog_init_timeout(struct watchdog_device *wdd,
  101. unsigned int timeout_parm, struct device *dev)
  102. {
  103. unsigned int t = 0;
  104. int ret = 0;
  105. watchdog_check_min_max_timeout(wdd);
  106. /* try to get the timeout module parameter first */
  107. if (!watchdog_timeout_invalid(wdd, timeout_parm) && timeout_parm) {
  108. wdd->timeout = timeout_parm;
  109. return ret;
  110. }
  111. if (timeout_parm)
  112. ret = -EINVAL;
  113. /* try to get the timeout_sec property */
  114. if (dev == NULL || dev->of_node == NULL)
  115. return ret;
  116. of_property_read_u32(dev->of_node, "timeout-sec", &t);
  117. if (!watchdog_timeout_invalid(wdd, t) && t)
  118. wdd->timeout = t;
  119. else
  120. ret = -EINVAL;
  121. return ret;
  122. }
  123. EXPORT_SYMBOL_GPL(watchdog_init_timeout);
  124. static int __watchdog_register_device(struct watchdog_device *wdd)
  125. {
  126. int ret, id, devno;
  127. if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
  128. return -EINVAL;
  129. /* Mandatory operations need to be supported */
  130. if (wdd->ops->start == NULL || wdd->ops->stop == NULL)
  131. return -EINVAL;
  132. watchdog_check_min_max_timeout(wdd);
  133. /*
  134. * Note: now that all watchdog_device data has been verified, we
  135. * will not check this anymore in other functions. If data gets
  136. * corrupted in a later stage then we expect a kernel panic!
  137. */
  138. mutex_init(&wdd->lock);
  139. id = ida_simple_get(&watchdog_ida, 0, MAX_DOGS, GFP_KERNEL);
  140. if (id < 0)
  141. return id;
  142. wdd->id = id;
  143. ret = watchdog_dev_register(wdd);
  144. if (ret) {
  145. ida_simple_remove(&watchdog_ida, id);
  146. if (!(id == 0 && ret == -EBUSY))
  147. return ret;
  148. /* Retry in case a legacy watchdog module exists */
  149. id = ida_simple_get(&watchdog_ida, 1, MAX_DOGS, GFP_KERNEL);
  150. if (id < 0)
  151. return id;
  152. wdd->id = id;
  153. ret = watchdog_dev_register(wdd);
  154. if (ret) {
  155. ida_simple_remove(&watchdog_ida, id);
  156. return ret;
  157. }
  158. }
  159. devno = wdd->cdev.dev;
  160. wdd->dev = device_create(watchdog_class, wdd->parent, devno,
  161. NULL, "watchdog%d", wdd->id);
  162. if (IS_ERR(wdd->dev)) {
  163. watchdog_dev_unregister(wdd);
  164. ida_simple_remove(&watchdog_ida, id);
  165. ret = PTR_ERR(wdd->dev);
  166. return ret;
  167. }
  168. return 0;
  169. }
  170. /**
  171. * watchdog_register_device() - register a watchdog device
  172. * @wdd: watchdog device
  173. *
  174. * Register a watchdog device with the kernel so that the
  175. * watchdog timer can be accessed from userspace.
  176. *
  177. * A zero is returned on success and a negative errno code for
  178. * failure.
  179. */
  180. int watchdog_register_device(struct watchdog_device *wdd)
  181. {
  182. int ret;
  183. mutex_lock(&wtd_deferred_reg_mutex);
  184. if (wtd_deferred_reg_done)
  185. ret = __watchdog_register_device(wdd);
  186. else
  187. ret = watchdog_deferred_registration_add(wdd);
  188. mutex_unlock(&wtd_deferred_reg_mutex);
  189. return ret;
  190. }
  191. EXPORT_SYMBOL_GPL(watchdog_register_device);
  192. static void __watchdog_unregister_device(struct watchdog_device *wdd)
  193. {
  194. int ret;
  195. int devno;
  196. if (wdd == NULL)
  197. return;
  198. devno = wdd->cdev.dev;
  199. ret = watchdog_dev_unregister(wdd);
  200. if (ret)
  201. pr_err("error unregistering /dev/watchdog (err=%d)\n", ret);
  202. device_destroy(watchdog_class, devno);
  203. ida_simple_remove(&watchdog_ida, wdd->id);
  204. wdd->dev = NULL;
  205. }
  206. /**
  207. * watchdog_unregister_device() - unregister a watchdog device
  208. * @wdd: watchdog device to unregister
  209. *
  210. * Unregister a watchdog device that was previously successfully
  211. * registered with watchdog_register_device().
  212. */
  213. void watchdog_unregister_device(struct watchdog_device *wdd)
  214. {
  215. mutex_lock(&wtd_deferred_reg_mutex);
  216. if (wtd_deferred_reg_done)
  217. __watchdog_unregister_device(wdd);
  218. else
  219. watchdog_deferred_registration_del(wdd);
  220. mutex_unlock(&wtd_deferred_reg_mutex);
  221. }
  222. EXPORT_SYMBOL_GPL(watchdog_unregister_device);
  223. static int __init watchdog_deferred_registration(void)
  224. {
  225. mutex_lock(&wtd_deferred_reg_mutex);
  226. wtd_deferred_reg_done = true;
  227. while (!list_empty(&wtd_deferred_reg_list)) {
  228. struct watchdog_device *wdd;
  229. wdd = list_first_entry(&wtd_deferred_reg_list,
  230. struct watchdog_device, deferred);
  231. list_del(&wdd->deferred);
  232. __watchdog_register_device(wdd);
  233. }
  234. mutex_unlock(&wtd_deferred_reg_mutex);
  235. return 0;
  236. }
  237. static int __init watchdog_init(void)
  238. {
  239. int err;
  240. watchdog_class = class_create(THIS_MODULE, "watchdog");
  241. if (IS_ERR(watchdog_class)) {
  242. pr_err("couldn't create class\n");
  243. return PTR_ERR(watchdog_class);
  244. }
  245. err = watchdog_dev_init();
  246. if (err < 0) {
  247. class_destroy(watchdog_class);
  248. return err;
  249. }
  250. watchdog_deferred_registration();
  251. return 0;
  252. }
  253. static void __exit watchdog_exit(void)
  254. {
  255. watchdog_dev_exit();
  256. class_destroy(watchdog_class);
  257. ida_destroy(&watchdog_ida);
  258. }
  259. subsys_initcall_sync(watchdog_init);
  260. module_exit(watchdog_exit);
  261. MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
  262. MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
  263. MODULE_DESCRIPTION("WatchDog Timer Driver Core");
  264. MODULE_LICENSE("GPL");