component.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 component_match {
  21. size_t alloc;
  22. size_t num;
  23. struct {
  24. void *data;
  25. int (*fn)(struct device *, void *);
  26. } compare[0];
  27. };
  28. struct master {
  29. struct list_head node;
  30. struct list_head components;
  31. bool bound;
  32. const struct component_master_ops *ops;
  33. struct device *dev;
  34. struct component_match *match;
  35. };
  36. struct component {
  37. struct list_head node;
  38. struct list_head master_node;
  39. struct master *master;
  40. bool bound;
  41. const struct component_ops *ops;
  42. struct device *dev;
  43. };
  44. static DEFINE_MUTEX(component_mutex);
  45. static LIST_HEAD(component_list);
  46. static LIST_HEAD(masters);
  47. static struct master *__master_find(struct device *dev,
  48. const struct component_master_ops *ops)
  49. {
  50. struct master *m;
  51. list_for_each_entry(m, &masters, node)
  52. if (m->dev == dev && (!ops || m->ops == ops))
  53. return m;
  54. return NULL;
  55. }
  56. /* Attach an unattached component to a master. */
  57. static void component_attach_master(struct master *master, struct component *c)
  58. {
  59. c->master = master;
  60. list_add_tail(&c->master_node, &master->components);
  61. }
  62. /* Detach a component from a master. */
  63. static void component_detach_master(struct master *master, struct component *c)
  64. {
  65. list_del(&c->master_node);
  66. c->master = NULL;
  67. }
  68. /*
  69. * Add a component to a master, finding the component via the compare
  70. * function and compare data. This is safe to call for duplicate matches
  71. * and will not result in the same component being added multiple times.
  72. */
  73. int component_master_add_child(struct master *master,
  74. int (*compare)(struct device *, void *), void *compare_data)
  75. {
  76. struct component *c;
  77. int ret = -ENXIO;
  78. list_for_each_entry(c, &component_list, node) {
  79. if (c->master && c->master != master)
  80. continue;
  81. if (compare(c->dev, compare_data)) {
  82. if (!c->master)
  83. component_attach_master(master, c);
  84. ret = 0;
  85. break;
  86. }
  87. }
  88. return ret;
  89. }
  90. EXPORT_SYMBOL_GPL(component_master_add_child);
  91. static int find_components(struct master *master)
  92. {
  93. struct component_match *match = master->match;
  94. size_t i;
  95. int ret = 0;
  96. if (!match) {
  97. /*
  98. * Search the list of components, looking for components that
  99. * belong to this master, and attach them to the master.
  100. */
  101. return master->ops->add_components(master->dev, master);
  102. }
  103. /*
  104. * Scan the array of match functions and attach
  105. * any components which are found to this master.
  106. */
  107. for (i = 0; i < match->num; i++) {
  108. ret = component_master_add_child(master,
  109. match->compare[i].fn,
  110. match->compare[i].data);
  111. if (ret)
  112. break;
  113. }
  114. return ret;
  115. }
  116. /* Detach all attached components from this master */
  117. static void master_remove_components(struct master *master)
  118. {
  119. while (!list_empty(&master->components)) {
  120. struct component *c = list_first_entry(&master->components,
  121. struct component, master_node);
  122. WARN_ON(c->master != master);
  123. component_detach_master(master, c);
  124. }
  125. }
  126. /*
  127. * Try to bring up a master. If component is NULL, we're interested in
  128. * this master, otherwise it's a component which must be present to try
  129. * and bring up the master.
  130. *
  131. * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
  132. */
  133. static int try_to_bring_up_master(struct master *master,
  134. struct component *component)
  135. {
  136. int ret;
  137. if (master->bound)
  138. return 0;
  139. /*
  140. * Search the list of components, looking for components that
  141. * belong to this master, and attach them to the master.
  142. */
  143. if (find_components(master)) {
  144. /* Failed to find all components */
  145. ret = 0;
  146. goto out;
  147. }
  148. if (component && component->master != master) {
  149. ret = 0;
  150. goto out;
  151. }
  152. if (!devres_open_group(master->dev, NULL, GFP_KERNEL)) {
  153. ret = -ENOMEM;
  154. goto out;
  155. }
  156. /* Found all components */
  157. ret = master->ops->bind(master->dev);
  158. if (ret < 0) {
  159. devres_release_group(master->dev, NULL);
  160. dev_info(master->dev, "master bind failed: %d\n", ret);
  161. goto out;
  162. }
  163. master->bound = true;
  164. return 1;
  165. out:
  166. master_remove_components(master);
  167. return ret;
  168. }
  169. static int try_to_bring_up_masters(struct component *component)
  170. {
  171. struct master *m;
  172. int ret = 0;
  173. list_for_each_entry(m, &masters, node) {
  174. ret = try_to_bring_up_master(m, component);
  175. if (ret != 0)
  176. break;
  177. }
  178. return ret;
  179. }
  180. static void take_down_master(struct master *master)
  181. {
  182. if (master->bound) {
  183. master->ops->unbind(master->dev);
  184. devres_release_group(master->dev, NULL);
  185. master->bound = false;
  186. }
  187. master_remove_components(master);
  188. }
  189. static size_t component_match_size(size_t num)
  190. {
  191. return offsetof(struct component_match, compare[num]);
  192. }
  193. static struct component_match *component_match_realloc(struct device *dev,
  194. struct component_match *match, size_t num)
  195. {
  196. struct component_match *new;
  197. if (match && match->alloc == num)
  198. return match;
  199. new = devm_kmalloc(dev, component_match_size(num), GFP_KERNEL);
  200. if (!new)
  201. return ERR_PTR(-ENOMEM);
  202. if (match) {
  203. memcpy(new, match, component_match_size(min(match->num, num)));
  204. devm_kfree(dev, match);
  205. } else {
  206. new->num = 0;
  207. }
  208. new->alloc = num;
  209. return new;
  210. }
  211. /*
  212. * Add a component to be matched.
  213. *
  214. * The match array is first created or extended if necessary.
  215. */
  216. void component_match_add(struct device *dev, struct component_match **matchptr,
  217. int (*compare)(struct device *, void *), void *compare_data)
  218. {
  219. struct component_match *match = *matchptr;
  220. if (IS_ERR(match))
  221. return;
  222. if (!match || match->num == match->alloc) {
  223. size_t new_size = match ? match->alloc + 16 : 15;
  224. match = component_match_realloc(dev, match, new_size);
  225. *matchptr = match;
  226. if (IS_ERR(match))
  227. return;
  228. }
  229. match->compare[match->num].fn = compare;
  230. match->compare[match->num].data = compare_data;
  231. match->num++;
  232. }
  233. EXPORT_SYMBOL(component_match_add);
  234. int component_master_add_with_match(struct device *dev,
  235. const struct component_master_ops *ops,
  236. struct component_match *match)
  237. {
  238. struct master *master;
  239. int ret;
  240. if (ops->add_components && match)
  241. return -EINVAL;
  242. if (match) {
  243. /* Reallocate the match array for its true size */
  244. match = component_match_realloc(dev, match, match->num);
  245. if (IS_ERR(match))
  246. return PTR_ERR(match);
  247. }
  248. master = kzalloc(sizeof(*master), GFP_KERNEL);
  249. if (!master)
  250. return -ENOMEM;
  251. master->dev = dev;
  252. master->ops = ops;
  253. master->match = match;
  254. INIT_LIST_HEAD(&master->components);
  255. /* Add to the list of available masters. */
  256. mutex_lock(&component_mutex);
  257. list_add(&master->node, &masters);
  258. ret = try_to_bring_up_master(master, NULL);
  259. if (ret < 0) {
  260. /* Delete off the list if we weren't successful */
  261. list_del(&master->node);
  262. kfree(master);
  263. }
  264. mutex_unlock(&component_mutex);
  265. return ret < 0 ? ret : 0;
  266. }
  267. EXPORT_SYMBOL_GPL(component_master_add_with_match);
  268. int component_master_add(struct device *dev,
  269. const struct component_master_ops *ops)
  270. {
  271. return component_master_add_with_match(dev, ops, NULL);
  272. }
  273. EXPORT_SYMBOL_GPL(component_master_add);
  274. void component_master_del(struct device *dev,
  275. const struct component_master_ops *ops)
  276. {
  277. struct master *master;
  278. mutex_lock(&component_mutex);
  279. master = __master_find(dev, ops);
  280. if (master) {
  281. take_down_master(master);
  282. list_del(&master->node);
  283. kfree(master);
  284. }
  285. mutex_unlock(&component_mutex);
  286. }
  287. EXPORT_SYMBOL_GPL(component_master_del);
  288. static void component_unbind(struct component *component,
  289. struct master *master, void *data)
  290. {
  291. WARN_ON(!component->bound);
  292. component->ops->unbind(component->dev, master->dev, data);
  293. component->bound = false;
  294. /* Release all resources claimed in the binding of this component */
  295. devres_release_group(component->dev, component);
  296. }
  297. void component_unbind_all(struct device *master_dev, void *data)
  298. {
  299. struct master *master;
  300. struct component *c;
  301. WARN_ON(!mutex_is_locked(&component_mutex));
  302. master = __master_find(master_dev, NULL);
  303. if (!master)
  304. return;
  305. list_for_each_entry_reverse(c, &master->components, master_node)
  306. component_unbind(c, master, data);
  307. }
  308. EXPORT_SYMBOL_GPL(component_unbind_all);
  309. static int component_bind(struct component *component, struct master *master,
  310. void *data)
  311. {
  312. int ret;
  313. /*
  314. * Each component initialises inside its own devres group.
  315. * This allows us to roll-back a failed component without
  316. * affecting anything else.
  317. */
  318. if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
  319. return -ENOMEM;
  320. /*
  321. * Also open a group for the device itself: this allows us
  322. * to release the resources claimed against the sub-device
  323. * at the appropriate moment.
  324. */
  325. if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
  326. devres_release_group(master->dev, NULL);
  327. return -ENOMEM;
  328. }
  329. dev_dbg(master->dev, "binding %s (ops %ps)\n",
  330. dev_name(component->dev), component->ops);
  331. ret = component->ops->bind(component->dev, master->dev, data);
  332. if (!ret) {
  333. component->bound = true;
  334. /*
  335. * Close the component device's group so that resources
  336. * allocated in the binding are encapsulated for removal
  337. * at unbind. Remove the group on the DRM device as we
  338. * can clean those resources up independently.
  339. */
  340. devres_close_group(component->dev, NULL);
  341. devres_remove_group(master->dev, NULL);
  342. dev_info(master->dev, "bound %s (ops %ps)\n",
  343. dev_name(component->dev), component->ops);
  344. } else {
  345. devres_release_group(component->dev, NULL);
  346. devres_release_group(master->dev, NULL);
  347. dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
  348. dev_name(component->dev), component->ops, ret);
  349. }
  350. return ret;
  351. }
  352. int component_bind_all(struct device *master_dev, void *data)
  353. {
  354. struct master *master;
  355. struct component *c;
  356. int ret = 0;
  357. WARN_ON(!mutex_is_locked(&component_mutex));
  358. master = __master_find(master_dev, NULL);
  359. if (!master)
  360. return -EINVAL;
  361. list_for_each_entry(c, &master->components, master_node) {
  362. ret = component_bind(c, master, data);
  363. if (ret)
  364. break;
  365. }
  366. if (ret != 0) {
  367. list_for_each_entry_continue_reverse(c, &master->components,
  368. master_node)
  369. component_unbind(c, master, data);
  370. }
  371. return ret;
  372. }
  373. EXPORT_SYMBOL_GPL(component_bind_all);
  374. int component_add(struct device *dev, const struct component_ops *ops)
  375. {
  376. struct component *component;
  377. int ret;
  378. component = kzalloc(sizeof(*component), GFP_KERNEL);
  379. if (!component)
  380. return -ENOMEM;
  381. component->ops = ops;
  382. component->dev = dev;
  383. dev_dbg(dev, "adding component (ops %ps)\n", ops);
  384. mutex_lock(&component_mutex);
  385. list_add_tail(&component->node, &component_list);
  386. ret = try_to_bring_up_masters(component);
  387. if (ret < 0) {
  388. list_del(&component->node);
  389. kfree(component);
  390. }
  391. mutex_unlock(&component_mutex);
  392. return ret < 0 ? ret : 0;
  393. }
  394. EXPORT_SYMBOL_GPL(component_add);
  395. void component_del(struct device *dev, const struct component_ops *ops)
  396. {
  397. struct component *c, *component = NULL;
  398. mutex_lock(&component_mutex);
  399. list_for_each_entry(c, &component_list, node)
  400. if (c->dev == dev && c->ops == ops) {
  401. list_del(&c->node);
  402. component = c;
  403. break;
  404. }
  405. if (component && component->master)
  406. take_down_master(component->master);
  407. mutex_unlock(&component_mutex);
  408. WARN_ON(!component);
  409. kfree(component);
  410. }
  411. EXPORT_SYMBOL_GPL(component_del);
  412. MODULE_LICENSE("GPL v2");