component.c 8.5 KB

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