watchdog_core.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 = -1, 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. /* Use alias for watchdog id if possible */
  140. if (wdd->parent) {
  141. ret = of_alias_get_id(wdd->parent->of_node, "watchdog");
  142. if (ret >= 0)
  143. id = ida_simple_get(&watchdog_ida, ret,
  144. ret + 1, GFP_KERNEL);
  145. }
  146. if (id < 0)
  147. id = ida_simple_get(&watchdog_ida, 0, MAX_DOGS, GFP_KERNEL);
  148. if (id < 0)
  149. return id;
  150. wdd->id = id;
  151. ret = watchdog_dev_register(wdd);
  152. if (ret) {
  153. ida_simple_remove(&watchdog_ida, id);
  154. if (!(id == 0 && ret == -EBUSY))
  155. return ret;
  156. /* Retry in case a legacy watchdog module exists */
  157. id = ida_simple_get(&watchdog_ida, 1, MAX_DOGS, GFP_KERNEL);
  158. if (id < 0)
  159. return id;
  160. wdd->id = id;
  161. ret = watchdog_dev_register(wdd);
  162. if (ret) {
  163. ida_simple_remove(&watchdog_ida, id);
  164. return ret;
  165. }
  166. }
  167. devno = wdd->cdev.dev;
  168. wdd->dev = device_create(watchdog_class, wdd->parent, devno,
  169. NULL, "watchdog%d", wdd->id);
  170. if (IS_ERR(wdd->dev)) {
  171. watchdog_dev_unregister(wdd);
  172. ida_simple_remove(&watchdog_ida, id);
  173. ret = PTR_ERR(wdd->dev);
  174. return ret;
  175. }
  176. return 0;
  177. }
  178. /**
  179. * watchdog_register_device() - register a watchdog device
  180. * @wdd: watchdog device
  181. *
  182. * Register a watchdog device with the kernel so that the
  183. * watchdog timer can be accessed from userspace.
  184. *
  185. * A zero is returned on success and a negative errno code for
  186. * failure.
  187. */
  188. int watchdog_register_device(struct watchdog_device *wdd)
  189. {
  190. int ret;
  191. mutex_lock(&wtd_deferred_reg_mutex);
  192. if (wtd_deferred_reg_done)
  193. ret = __watchdog_register_device(wdd);
  194. else
  195. ret = watchdog_deferred_registration_add(wdd);
  196. mutex_unlock(&wtd_deferred_reg_mutex);
  197. return ret;
  198. }
  199. EXPORT_SYMBOL_GPL(watchdog_register_device);
  200. static void __watchdog_unregister_device(struct watchdog_device *wdd)
  201. {
  202. int ret;
  203. int devno;
  204. if (wdd == NULL)
  205. return;
  206. devno = wdd->cdev.dev;
  207. ret = watchdog_dev_unregister(wdd);
  208. if (ret)
  209. pr_err("error unregistering /dev/watchdog (err=%d)\n", ret);
  210. device_destroy(watchdog_class, devno);
  211. ida_simple_remove(&watchdog_ida, wdd->id);
  212. wdd->dev = NULL;
  213. }
  214. /**
  215. * watchdog_unregister_device() - unregister a watchdog device
  216. * @wdd: watchdog device to unregister
  217. *
  218. * Unregister a watchdog device that was previously successfully
  219. * registered with watchdog_register_device().
  220. */
  221. void watchdog_unregister_device(struct watchdog_device *wdd)
  222. {
  223. mutex_lock(&wtd_deferred_reg_mutex);
  224. if (wtd_deferred_reg_done)
  225. __watchdog_unregister_device(wdd);
  226. else
  227. watchdog_deferred_registration_del(wdd);
  228. mutex_unlock(&wtd_deferred_reg_mutex);
  229. }
  230. EXPORT_SYMBOL_GPL(watchdog_unregister_device);
  231. static int __init watchdog_deferred_registration(void)
  232. {
  233. mutex_lock(&wtd_deferred_reg_mutex);
  234. wtd_deferred_reg_done = true;
  235. while (!list_empty(&wtd_deferred_reg_list)) {
  236. struct watchdog_device *wdd;
  237. wdd = list_first_entry(&wtd_deferred_reg_list,
  238. struct watchdog_device, deferred);
  239. list_del(&wdd->deferred);
  240. __watchdog_register_device(wdd);
  241. }
  242. mutex_unlock(&wtd_deferred_reg_mutex);
  243. return 0;
  244. }
  245. static int __init watchdog_init(void)
  246. {
  247. int err;
  248. watchdog_class = class_create(THIS_MODULE, "watchdog");
  249. if (IS_ERR(watchdog_class)) {
  250. pr_err("couldn't create class\n");
  251. return PTR_ERR(watchdog_class);
  252. }
  253. err = watchdog_dev_init();
  254. if (err < 0) {
  255. class_destroy(watchdog_class);
  256. return err;
  257. }
  258. watchdog_deferred_registration();
  259. return 0;
  260. }
  261. static void __exit watchdog_exit(void)
  262. {
  263. watchdog_dev_exit();
  264. class_destroy(watchdog_class);
  265. ida_destroy(&watchdog_ida);
  266. }
  267. subsys_initcall_sync(watchdog_init);
  268. module_exit(watchdog_exit);
  269. MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
  270. MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
  271. MODULE_DESCRIPTION("WatchDog Timer Driver Core");
  272. MODULE_LICENSE("GPL");