component.c 12 KB

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