component.c 8.7 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;
  97. if (master->bound)
  98. return 0;
  99. /*
  100. * Search the list of components, looking for components that
  101. * belong to this master, and attach them to the master.
  102. */
  103. if (master->ops->add_components(master->dev, master)) {
  104. /* Failed to find all components */
  105. ret = 0;
  106. goto out;
  107. }
  108. if (component && component->master != master) {
  109. ret = 0;
  110. goto out;
  111. }
  112. if (!devres_open_group(master->dev, NULL, GFP_KERNEL)) {
  113. ret = -ENOMEM;
  114. goto out;
  115. }
  116. /* Found all components */
  117. ret = master->ops->bind(master->dev);
  118. if (ret < 0) {
  119. devres_release_group(master->dev, NULL);
  120. dev_info(master->dev, "master bind failed: %d\n", ret);
  121. goto out;
  122. }
  123. master->bound = true;
  124. return 1;
  125. out:
  126. master_remove_components(master);
  127. return ret;
  128. }
  129. static int try_to_bring_up_masters(struct component *component)
  130. {
  131. struct master *m;
  132. int ret = 0;
  133. list_for_each_entry(m, &masters, node) {
  134. ret = try_to_bring_up_master(m, component);
  135. if (ret != 0)
  136. break;
  137. }
  138. return ret;
  139. }
  140. static void take_down_master(struct master *master)
  141. {
  142. if (master->bound) {
  143. master->ops->unbind(master->dev);
  144. devres_release_group(master->dev, NULL);
  145. master->bound = false;
  146. }
  147. master_remove_components(master);
  148. }
  149. int component_master_add(struct device *dev,
  150. const struct component_master_ops *ops)
  151. {
  152. struct master *master;
  153. int ret;
  154. master = kzalloc(sizeof(*master), GFP_KERNEL);
  155. if (!master)
  156. return -ENOMEM;
  157. master->dev = dev;
  158. master->ops = ops;
  159. INIT_LIST_HEAD(&master->components);
  160. /* Add to the list of available masters. */
  161. mutex_lock(&component_mutex);
  162. list_add(&master->node, &masters);
  163. ret = try_to_bring_up_master(master, NULL);
  164. if (ret < 0) {
  165. /* Delete off the list if we weren't successful */
  166. list_del(&master->node);
  167. kfree(master);
  168. }
  169. mutex_unlock(&component_mutex);
  170. return ret < 0 ? ret : 0;
  171. }
  172. EXPORT_SYMBOL_GPL(component_master_add);
  173. void component_master_del(struct device *dev,
  174. const struct component_master_ops *ops)
  175. {
  176. struct master *master;
  177. mutex_lock(&component_mutex);
  178. master = __master_find(dev, ops);
  179. if (master) {
  180. take_down_master(master);
  181. list_del(&master->node);
  182. kfree(master);
  183. }
  184. mutex_unlock(&component_mutex);
  185. }
  186. EXPORT_SYMBOL_GPL(component_master_del);
  187. static void component_unbind(struct component *component,
  188. struct master *master, void *data)
  189. {
  190. WARN_ON(!component->bound);
  191. component->ops->unbind(component->dev, master->dev, data);
  192. component->bound = false;
  193. /* Release all resources claimed in the binding of this component */
  194. devres_release_group(component->dev, component);
  195. }
  196. void component_unbind_all(struct device *master_dev, void *data)
  197. {
  198. struct master *master;
  199. struct component *c;
  200. WARN_ON(!mutex_is_locked(&component_mutex));
  201. master = __master_find(master_dev, NULL);
  202. if (!master)
  203. return;
  204. list_for_each_entry_reverse(c, &master->components, master_node)
  205. component_unbind(c, master, data);
  206. }
  207. EXPORT_SYMBOL_GPL(component_unbind_all);
  208. static int component_bind(struct component *component, struct master *master,
  209. void *data)
  210. {
  211. int ret;
  212. /*
  213. * Each component initialises inside its own devres group.
  214. * This allows us to roll-back a failed component without
  215. * affecting anything else.
  216. */
  217. if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
  218. return -ENOMEM;
  219. /*
  220. * Also open a group for the device itself: this allows us
  221. * to release the resources claimed against the sub-device
  222. * at the appropriate moment.
  223. */
  224. if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
  225. devres_release_group(master->dev, NULL);
  226. return -ENOMEM;
  227. }
  228. dev_dbg(master->dev, "binding %s (ops %ps)\n",
  229. dev_name(component->dev), component->ops);
  230. ret = component->ops->bind(component->dev, master->dev, data);
  231. if (!ret) {
  232. component->bound = true;
  233. /*
  234. * Close the component device's group so that resources
  235. * allocated in the binding are encapsulated for removal
  236. * at unbind. Remove the group on the DRM device as we
  237. * can clean those resources up independently.
  238. */
  239. devres_close_group(component->dev, NULL);
  240. devres_remove_group(master->dev, NULL);
  241. dev_info(master->dev, "bound %s (ops %ps)\n",
  242. dev_name(component->dev), component->ops);
  243. } else {
  244. devres_release_group(component->dev, NULL);
  245. devres_release_group(master->dev, NULL);
  246. dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
  247. dev_name(component->dev), component->ops, ret);
  248. }
  249. return ret;
  250. }
  251. int component_bind_all(struct device *master_dev, void *data)
  252. {
  253. struct master *master;
  254. struct component *c;
  255. int ret = 0;
  256. WARN_ON(!mutex_is_locked(&component_mutex));
  257. master = __master_find(master_dev, NULL);
  258. if (!master)
  259. return -EINVAL;
  260. list_for_each_entry(c, &master->components, master_node) {
  261. ret = component_bind(c, master, data);
  262. if (ret)
  263. break;
  264. }
  265. if (ret != 0) {
  266. list_for_each_entry_continue_reverse(c, &master->components,
  267. master_node)
  268. component_unbind(c, master, data);
  269. }
  270. return ret;
  271. }
  272. EXPORT_SYMBOL_GPL(component_bind_all);
  273. int component_add(struct device *dev, const struct component_ops *ops)
  274. {
  275. struct component *component;
  276. int ret;
  277. component = kzalloc(sizeof(*component), GFP_KERNEL);
  278. if (!component)
  279. return -ENOMEM;
  280. component->ops = ops;
  281. component->dev = dev;
  282. dev_dbg(dev, "adding component (ops %ps)\n", ops);
  283. mutex_lock(&component_mutex);
  284. list_add_tail(&component->node, &component_list);
  285. ret = try_to_bring_up_masters(component);
  286. if (ret < 0) {
  287. list_del(&component->node);
  288. kfree(component);
  289. }
  290. mutex_unlock(&component_mutex);
  291. return ret < 0 ? ret : 0;
  292. }
  293. EXPORT_SYMBOL_GPL(component_add);
  294. void component_del(struct device *dev, const struct component_ops *ops)
  295. {
  296. struct component *c, *component = NULL;
  297. mutex_lock(&component_mutex);
  298. list_for_each_entry(c, &component_list, node)
  299. if (c->dev == dev && c->ops == ops) {
  300. list_del(&c->node);
  301. component = c;
  302. break;
  303. }
  304. if (component && component->master)
  305. take_down_master(component->master);
  306. mutex_unlock(&component_mutex);
  307. WARN_ON(!component);
  308. kfree(component);
  309. }
  310. EXPORT_SYMBOL_GPL(component_del);
  311. MODULE_LICENSE("GPL v2");