component.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Componentized device handling.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This is work in progress. We gather up the component devices into a list,
  9. * and bind them when instructed. At the moment, we're specific to the DRM
  10. * subsystem, and only handles one master device, but this doesn't have to be
  11. * the case.
  12. */
  13. #include <linux/component.h>
  14. #include <linux/device.h>
  15. #include <linux/kref.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/slab.h>
  20. struct master {
  21. struct list_head node;
  22. struct list_head components;
  23. bool bound;
  24. const struct component_master_ops *ops;
  25. struct device *dev;
  26. };
  27. struct component {
  28. struct list_head node;
  29. struct list_head master_node;
  30. struct master *master;
  31. bool bound;
  32. const struct component_ops *ops;
  33. struct device *dev;
  34. };
  35. static DEFINE_MUTEX(component_mutex);
  36. static LIST_HEAD(component_list);
  37. static LIST_HEAD(masters);
  38. static struct master *__master_find(struct device *dev,
  39. const struct component_master_ops *ops)
  40. {
  41. struct master *m;
  42. list_for_each_entry(m, &masters, node)
  43. if (m->dev == dev && (!ops || m->ops == ops))
  44. return m;
  45. return NULL;
  46. }
  47. /* Attach an unattached component to a master. */
  48. static void component_attach_master(struct master *master, struct component *c)
  49. {
  50. c->master = master;
  51. list_add_tail(&c->master_node, &master->components);
  52. }
  53. /* Detach a component from a master. */
  54. static void component_detach_master(struct master *master, struct component *c)
  55. {
  56. list_del(&c->master_node);
  57. c->master = NULL;
  58. }
  59. int component_master_add_child(struct master *master,
  60. int (*compare)(struct device *, void *), void *compare_data)
  61. {
  62. struct component *c;
  63. int ret = -ENXIO;
  64. list_for_each_entry(c, &component_list, node) {
  65. if (c->master)
  66. continue;
  67. if (compare(c->dev, compare_data)) {
  68. component_attach_master(master, c);
  69. ret = 0;
  70. break;
  71. }
  72. }
  73. return ret;
  74. }
  75. EXPORT_SYMBOL_GPL(component_master_add_child);
  76. /* Detach all attached components from this master */
  77. static void master_remove_components(struct master *master)
  78. {
  79. while (!list_empty(&master->components)) {
  80. struct component *c = list_first_entry(&master->components,
  81. struct component, master_node);
  82. WARN_ON(c->master != master);
  83. component_detach_master(master, c);
  84. }
  85. }
  86. /*
  87. * Try to bring up a master. If component is NULL, we're interested in
  88. * this master, otherwise it's a component which must be present to try
  89. * and bring up the master.
  90. *
  91. * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
  92. */
  93. static int try_to_bring_up_master(struct master *master,
  94. struct component *component)
  95. {
  96. int ret = 0;
  97. if (!master->bound) {
  98. /*
  99. * Search the list of components, looking for components that
  100. * belong to this master, and attach them to the master.
  101. */
  102. if (master->ops->add_components(master->dev, master)) {
  103. /* Failed to find all components */
  104. master_remove_components(master);
  105. ret = 0;
  106. goto out;
  107. }
  108. if (component && component->master != master) {
  109. master_remove_components(master);
  110. ret = 0;
  111. goto out;
  112. }
  113. if (!devres_open_group(master->dev, NULL, GFP_KERNEL)) {
  114. ret = -ENOMEM;
  115. goto out;
  116. }
  117. /* Found all components */
  118. ret = master->ops->bind(master->dev);
  119. if (ret < 0) {
  120. devres_release_group(master->dev, NULL);
  121. dev_info(master->dev, "master bind failed: %d\n", ret);
  122. master_remove_components(master);
  123. goto out;
  124. }
  125. master->bound = true;
  126. ret = 1;
  127. }
  128. out:
  129. return ret;
  130. }
  131. static int try_to_bring_up_masters(struct component *component)
  132. {
  133. struct master *m;
  134. int ret = 0;
  135. list_for_each_entry(m, &masters, node) {
  136. ret = try_to_bring_up_master(m, component);
  137. if (ret != 0)
  138. break;
  139. }
  140. return ret;
  141. }
  142. static void take_down_master(struct master *master)
  143. {
  144. if (master->bound) {
  145. master->ops->unbind(master->dev);
  146. devres_release_group(master->dev, NULL);
  147. master->bound = false;
  148. }
  149. master_remove_components(master);
  150. }
  151. int component_master_add(struct device *dev,
  152. const struct component_master_ops *ops)
  153. {
  154. struct master *master;
  155. int ret;
  156. master = kzalloc(sizeof(*master), GFP_KERNEL);
  157. if (!master)
  158. return -ENOMEM;
  159. master->dev = dev;
  160. master->ops = ops;
  161. INIT_LIST_HEAD(&master->components);
  162. /* Add to the list of available masters. */
  163. mutex_lock(&component_mutex);
  164. list_add(&master->node, &masters);
  165. ret = try_to_bring_up_master(master, NULL);
  166. if (ret < 0) {
  167. /* Delete off the list if we weren't successful */
  168. list_del(&master->node);
  169. kfree(master);
  170. }
  171. mutex_unlock(&component_mutex);
  172. return ret < 0 ? ret : 0;
  173. }
  174. EXPORT_SYMBOL_GPL(component_master_add);
  175. void component_master_del(struct device *dev,
  176. const struct component_master_ops *ops)
  177. {
  178. struct master *master;
  179. mutex_lock(&component_mutex);
  180. master = __master_find(dev, ops);
  181. if (master) {
  182. take_down_master(master);
  183. list_del(&master->node);
  184. kfree(master);
  185. }
  186. mutex_unlock(&component_mutex);
  187. }
  188. EXPORT_SYMBOL_GPL(component_master_del);
  189. static void component_unbind(struct component *component,
  190. struct master *master, void *data)
  191. {
  192. WARN_ON(!component->bound);
  193. component->ops->unbind(component->dev, master->dev, data);
  194. component->bound = false;
  195. /* Release all resources claimed in the binding of this component */
  196. devres_release_group(component->dev, component);
  197. }
  198. void component_unbind_all(struct device *master_dev, void *data)
  199. {
  200. struct master *master;
  201. struct component *c;
  202. WARN_ON(!mutex_is_locked(&component_mutex));
  203. master = __master_find(master_dev, NULL);
  204. if (!master)
  205. return;
  206. list_for_each_entry_reverse(c, &master->components, master_node)
  207. component_unbind(c, master, data);
  208. }
  209. EXPORT_SYMBOL_GPL(component_unbind_all);
  210. static int component_bind(struct component *component, struct master *master,
  211. void *data)
  212. {
  213. int ret;
  214. /*
  215. * Each component initialises inside its own devres group.
  216. * This allows us to roll-back a failed component without
  217. * affecting anything else.
  218. */
  219. if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
  220. return -ENOMEM;
  221. /*
  222. * Also open a group for the device itself: this allows us
  223. * to release the resources claimed against the sub-device
  224. * at the appropriate moment.
  225. */
  226. if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
  227. devres_release_group(master->dev, NULL);
  228. return -ENOMEM;
  229. }
  230. dev_dbg(master->dev, "binding %s (ops %ps)\n",
  231. dev_name(component->dev), component->ops);
  232. ret = component->ops->bind(component->dev, master->dev, data);
  233. if (!ret) {
  234. component->bound = true;
  235. /*
  236. * Close the component device's group so that resources
  237. * allocated in the binding are encapsulated for removal
  238. * at unbind. Remove the group on the DRM device as we
  239. * can clean those resources up independently.
  240. */
  241. devres_close_group(component->dev, NULL);
  242. devres_remove_group(master->dev, NULL);
  243. dev_info(master->dev, "bound %s (ops %ps)\n",
  244. dev_name(component->dev), component->ops);
  245. } else {
  246. devres_release_group(component->dev, NULL);
  247. devres_release_group(master->dev, NULL);
  248. dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
  249. dev_name(component->dev), component->ops, ret);
  250. }
  251. return ret;
  252. }
  253. int component_bind_all(struct device *master_dev, void *data)
  254. {
  255. struct master *master;
  256. struct component *c;
  257. int ret = 0;
  258. WARN_ON(!mutex_is_locked(&component_mutex));
  259. master = __master_find(master_dev, NULL);
  260. if (!master)
  261. return -EINVAL;
  262. list_for_each_entry(c, &master->components, master_node) {
  263. ret = component_bind(c, master, data);
  264. if (ret)
  265. break;
  266. }
  267. if (ret != 0) {
  268. list_for_each_entry_continue_reverse(c, &master->components,
  269. master_node)
  270. component_unbind(c, master, data);
  271. }
  272. return ret;
  273. }
  274. EXPORT_SYMBOL_GPL(component_bind_all);
  275. int component_add(struct device *dev, const struct component_ops *ops)
  276. {
  277. struct component *component;
  278. int ret;
  279. component = kzalloc(sizeof(*component), GFP_KERNEL);
  280. if (!component)
  281. return -ENOMEM;
  282. component->ops = ops;
  283. component->dev = dev;
  284. dev_dbg(dev, "adding component (ops %ps)\n", ops);
  285. mutex_lock(&component_mutex);
  286. list_add_tail(&component->node, &component_list);
  287. ret = try_to_bring_up_masters(component);
  288. if (ret < 0) {
  289. list_del(&component->node);
  290. kfree(component);
  291. }
  292. mutex_unlock(&component_mutex);
  293. return ret < 0 ? ret : 0;
  294. }
  295. EXPORT_SYMBOL_GPL(component_add);
  296. void component_del(struct device *dev, const struct component_ops *ops)
  297. {
  298. struct component *c, *component = NULL;
  299. mutex_lock(&component_mutex);
  300. list_for_each_entry(c, &component_list, node)
  301. if (c->dev == dev && c->ops == ops) {
  302. list_del(&c->node);
  303. component = c;
  304. break;
  305. }
  306. if (component && component->master)
  307. take_down_master(component->master);
  308. mutex_unlock(&component_mutex);
  309. WARN_ON(!component);
  310. kfree(component);
  311. }
  312. EXPORT_SYMBOL_GPL(component_del);
  313. MODULE_LICENSE("GPL v2");