component.c 11 KB

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