component.c 14 KB

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