component.c 14 KB

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