component.c 12 KB

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