component.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. kfree(match->compare);
  172. }
  173. static void devm_component_match_release(struct device *dev, void *res)
  174. {
  175. component_match_release(dev, res);
  176. }
  177. static int component_match_realloc(struct device *dev,
  178. struct component_match *match, size_t num)
  179. {
  180. struct component_match_array *new;
  181. if (match->alloc == num)
  182. return 0;
  183. new = kmalloc_array(num, sizeof(*new), GFP_KERNEL);
  184. if (!new)
  185. return -ENOMEM;
  186. if (match->compare) {
  187. memcpy(new, match->compare, sizeof(*new) *
  188. min(match->num, num));
  189. kfree(match->compare);
  190. }
  191. match->compare = new;
  192. match->alloc = num;
  193. return 0;
  194. }
  195. /*
  196. * Add a component to be matched, with a release function.
  197. *
  198. * The match array is first created or extended if necessary.
  199. */
  200. void component_match_add_release(struct device *master,
  201. struct component_match **matchptr,
  202. void (*release)(struct device *, void *),
  203. int (*compare)(struct device *, void *), void *compare_data)
  204. {
  205. struct component_match *match = *matchptr;
  206. if (IS_ERR(match))
  207. return;
  208. if (!match) {
  209. match = devres_alloc(devm_component_match_release,
  210. sizeof(*match), GFP_KERNEL);
  211. if (!match) {
  212. *matchptr = ERR_PTR(-ENOMEM);
  213. return;
  214. }
  215. devres_add(master, match);
  216. *matchptr = match;
  217. }
  218. if (match->num == match->alloc) {
  219. size_t new_size = match->alloc + 16;
  220. int ret;
  221. ret = component_match_realloc(master, match, new_size);
  222. if (ret) {
  223. *matchptr = ERR_PTR(ret);
  224. return;
  225. }
  226. }
  227. match->compare[match->num].compare = compare;
  228. match->compare[match->num].release = release;
  229. match->compare[match->num].data = compare_data;
  230. match->compare[match->num].component = NULL;
  231. match->num++;
  232. }
  233. EXPORT_SYMBOL(component_match_add_release);
  234. static void free_master(struct master *master)
  235. {
  236. struct component_match *match = master->match;
  237. int i;
  238. list_del(&master->node);
  239. if (match) {
  240. for (i = 0; i < match->num; i++) {
  241. struct component *c = match->compare[i].component;
  242. if (c)
  243. c->master = NULL;
  244. }
  245. }
  246. kfree(master);
  247. }
  248. int component_master_add_with_match(struct device *dev,
  249. const struct component_master_ops *ops,
  250. struct component_match *match)
  251. {
  252. struct master *master;
  253. int ret;
  254. /* Reallocate the match array for its true size */
  255. ret = component_match_realloc(dev, match, match->num);
  256. if (ret)
  257. return ret;
  258. master = kzalloc(sizeof(*master), GFP_KERNEL);
  259. if (!master)
  260. return -ENOMEM;
  261. master->dev = dev;
  262. master->ops = ops;
  263. master->match = match;
  264. /* Add to the list of available masters. */
  265. mutex_lock(&component_mutex);
  266. list_add(&master->node, &masters);
  267. ret = try_to_bring_up_master(master, NULL);
  268. if (ret < 0)
  269. free_master(master);
  270. mutex_unlock(&component_mutex);
  271. return ret < 0 ? ret : 0;
  272. }
  273. EXPORT_SYMBOL_GPL(component_master_add_with_match);
  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. free_master(master);
  283. }
  284. mutex_unlock(&component_mutex);
  285. }
  286. EXPORT_SYMBOL_GPL(component_master_del);
  287. static void component_unbind(struct component *component,
  288. struct master *master, void *data)
  289. {
  290. WARN_ON(!component->bound);
  291. component->ops->unbind(component->dev, master->dev, data);
  292. component->bound = false;
  293. /* Release all resources claimed in the binding of this component */
  294. devres_release_group(component->dev, component);
  295. }
  296. void component_unbind_all(struct device *master_dev, void *data)
  297. {
  298. struct master *master;
  299. struct component *c;
  300. size_t i;
  301. WARN_ON(!mutex_is_locked(&component_mutex));
  302. master = __master_find(master_dev, NULL);
  303. if (!master)
  304. return;
  305. /* Unbind components in reverse order */
  306. for (i = master->match->num; i--; )
  307. if (!master->match->compare[i].duplicate) {
  308. c = master->match->compare[i].component;
  309. component_unbind(c, master, data);
  310. }
  311. }
  312. EXPORT_SYMBOL_GPL(component_unbind_all);
  313. static int component_bind(struct component *component, struct master *master,
  314. void *data)
  315. {
  316. int ret;
  317. /*
  318. * Each component initialises inside its own devres group.
  319. * This allows us to roll-back a failed component without
  320. * affecting anything else.
  321. */
  322. if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
  323. return -ENOMEM;
  324. /*
  325. * Also open a group for the device itself: this allows us
  326. * to release the resources claimed against the sub-device
  327. * at the appropriate moment.
  328. */
  329. if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
  330. devres_release_group(master->dev, NULL);
  331. return -ENOMEM;
  332. }
  333. dev_dbg(master->dev, "binding %s (ops %ps)\n",
  334. dev_name(component->dev), component->ops);
  335. ret = component->ops->bind(component->dev, master->dev, data);
  336. if (!ret) {
  337. component->bound = true;
  338. /*
  339. * Close the component device's group so that resources
  340. * allocated in the binding are encapsulated for removal
  341. * at unbind. Remove the group on the DRM device as we
  342. * can clean those resources up independently.
  343. */
  344. devres_close_group(component->dev, NULL);
  345. devres_remove_group(master->dev, NULL);
  346. dev_info(master->dev, "bound %s (ops %ps)\n",
  347. dev_name(component->dev), component->ops);
  348. } else {
  349. devres_release_group(component->dev, NULL);
  350. devres_release_group(master->dev, NULL);
  351. dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
  352. dev_name(component->dev), component->ops, ret);
  353. }
  354. return ret;
  355. }
  356. int component_bind_all(struct device *master_dev, void *data)
  357. {
  358. struct master *master;
  359. struct component *c;
  360. size_t i;
  361. int ret = 0;
  362. WARN_ON(!mutex_is_locked(&component_mutex));
  363. master = __master_find(master_dev, NULL);
  364. if (!master)
  365. return -EINVAL;
  366. /* Bind components in match order */
  367. for (i = 0; i < master->match->num; i++)
  368. if (!master->match->compare[i].duplicate) {
  369. c = master->match->compare[i].component;
  370. ret = component_bind(c, master, data);
  371. if (ret)
  372. break;
  373. }
  374. if (ret != 0) {
  375. for (; i--; )
  376. if (!master->match->compare[i].duplicate) {
  377. c = master->match->compare[i].component;
  378. component_unbind(c, master, data);
  379. }
  380. }
  381. return ret;
  382. }
  383. EXPORT_SYMBOL_GPL(component_bind_all);
  384. int component_add(struct device *dev, const struct component_ops *ops)
  385. {
  386. struct component *component;
  387. int ret;
  388. component = kzalloc(sizeof(*component), GFP_KERNEL);
  389. if (!component)
  390. return -ENOMEM;
  391. component->ops = ops;
  392. component->dev = dev;
  393. dev_dbg(dev, "adding component (ops %ps)\n", ops);
  394. mutex_lock(&component_mutex);
  395. list_add_tail(&component->node, &component_list);
  396. ret = try_to_bring_up_masters(component);
  397. if (ret < 0) {
  398. if (component->master)
  399. remove_component(component->master, component);
  400. list_del(&component->node);
  401. kfree(component);
  402. }
  403. mutex_unlock(&component_mutex);
  404. return ret < 0 ? ret : 0;
  405. }
  406. EXPORT_SYMBOL_GPL(component_add);
  407. void component_del(struct device *dev, const struct component_ops *ops)
  408. {
  409. struct component *c, *component = NULL;
  410. mutex_lock(&component_mutex);
  411. list_for_each_entry(c, &component_list, node)
  412. if (c->dev == dev && c->ops == ops) {
  413. list_del(&c->node);
  414. component = c;
  415. break;
  416. }
  417. if (component && component->master) {
  418. take_down_master(component->master);
  419. remove_component(component->master, component);
  420. }
  421. mutex_unlock(&component_mutex);
  422. WARN_ON(!component);
  423. kfree(component);
  424. }
  425. EXPORT_SYMBOL_GPL(component_del);
  426. MODULE_LICENSE("GPL v2");