component.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Componentized device handling.
  4. */
  5. #include <linux/component.h>
  6. #include <linux/device.h>
  7. #include <linux/list.h>
  8. #include <linux/mutex.h>
  9. #include <linux/of.h>
  10. #include <linux/slab.h>
  11. #include <linux/debugfs.h>
  12. /**
  13. * DOC: overview
  14. *
  15. * The component helper allows drivers to collect a pile of sub-devices,
  16. * including their bound drivers, into an aggregate driver. Various subsystems
  17. * already provide functions to get hold of such components, e.g.
  18. * of_clk_get_by_name(). The component helper can be used when such a
  19. * subsystem-specific way to find a device is not available: The component
  20. * helper fills the niche of aggregate drivers for specific hardware, where
  21. * further standardization into a subsystem would not be practical. The common
  22. * example is when a logical device (e.g. a DRM display driver) is spread around
  23. * the SoC on various components (scanout engines, blending blocks, transcoders
  24. * for various outputs and so on).
  25. *
  26. * The component helper also doesn't solve runtime dependencies, e.g. for system
  27. * suspend and resume operations. See also :ref:`device links<device_link>`.
  28. *
  29. * Components are registered using component_add() and unregistered with
  30. * component_del(), usually from the driver's probe and disconnect functions.
  31. *
  32. * Aggregate drivers first assemble a component match list of what they need
  33. * using component_match_add(). This is then registered as an aggregate driver
  34. * using component_master_add_with_match(), and unregistered using
  35. * component_master_del().
  36. */
  37. struct component;
  38. struct component_match_array {
  39. void *data;
  40. int (*compare)(struct device *, void *);
  41. int (*compare_typed)(struct device *, int, void *);
  42. void (*release)(struct device *, void *);
  43. struct component *component;
  44. bool duplicate;
  45. };
  46. struct component_match {
  47. size_t alloc;
  48. size_t num;
  49. struct component_match_array *compare;
  50. };
  51. struct aggregate_device {
  52. struct list_head node;
  53. bool bound;
  54. const struct component_master_ops *ops;
  55. struct device *parent;
  56. struct component_match *match;
  57. };
  58. struct component {
  59. struct list_head node;
  60. struct aggregate_device *adev;
  61. bool bound;
  62. const struct component_ops *ops;
  63. int subcomponent;
  64. struct device *dev;
  65. };
  66. static DEFINE_MUTEX(component_mutex);
  67. static LIST_HEAD(component_list);
  68. static LIST_HEAD(aggregate_devices);
  69. #ifdef CONFIG_DEBUG_FS
  70. static struct dentry *component_debugfs_dir;
  71. static int component_devices_show(struct seq_file *s, void *data)
  72. {
  73. struct aggregate_device *m = s->private;
  74. struct component_match *match = m->match;
  75. size_t i;
  76. mutex_lock(&component_mutex);
  77. seq_printf(s, "%-40s %20s\n", "aggregate_device name", "status");
  78. seq_puts(s, "-------------------------------------------------------------\n");
  79. seq_printf(s, "%-40s %20s\n\n",
  80. dev_name(m->parent), m->bound ? "bound" : "not bound");
  81. seq_printf(s, "%-40s %20s\n", "device name", "status");
  82. seq_puts(s, "-------------------------------------------------------------\n");
  83. for (i = 0; i < match->num; i++) {
  84. struct component *component = match->compare[i].component;
  85. seq_printf(s, "%-40s %20s\n",
  86. component ? dev_name(component->dev) : "(unknown)",
  87. component ? (component->bound ? "bound" : "not bound") : "not registered");
  88. }
  89. mutex_unlock(&component_mutex);
  90. return 0;
  91. }
  92. DEFINE_SHOW_ATTRIBUTE(component_devices);
  93. static int __init component_debug_init(void)
  94. {
  95. component_debugfs_dir = debugfs_create_dir("device_component", NULL);
  96. return 0;
  97. }
  98. core_initcall(component_debug_init);
  99. static void component_debugfs_add(struct aggregate_device *m)
  100. {
  101. debugfs_create_file(dev_name(m->parent), 0444, component_debugfs_dir, m,
  102. &component_devices_fops);
  103. }
  104. static void component_debugfs_del(struct aggregate_device *m)
  105. {
  106. debugfs_lookup_and_remove(dev_name(m->parent), component_debugfs_dir);
  107. }
  108. #else
  109. static void component_debugfs_add(struct aggregate_device *m)
  110. { }
  111. static void component_debugfs_del(struct aggregate_device *m)
  112. { }
  113. #endif
  114. static struct aggregate_device *__aggregate_find(struct device *parent,
  115. const struct component_master_ops *ops)
  116. {
  117. struct aggregate_device *m;
  118. list_for_each_entry(m, &aggregate_devices, node)
  119. if (m->parent == parent && (!ops || m->ops == ops))
  120. return m;
  121. return NULL;
  122. }
  123. static struct component *find_component(struct aggregate_device *adev,
  124. struct component_match_array *mc)
  125. {
  126. struct component *c;
  127. list_for_each_entry(c, &component_list, node) {
  128. if (c->adev && c->adev != adev)
  129. continue;
  130. if (mc->compare && mc->compare(c->dev, mc->data))
  131. return c;
  132. if (mc->compare_typed &&
  133. mc->compare_typed(c->dev, c->subcomponent, mc->data))
  134. return c;
  135. }
  136. return NULL;
  137. }
  138. static int find_components(struct aggregate_device *adev)
  139. {
  140. struct component_match *match = adev->match;
  141. size_t i;
  142. int ret = 0;
  143. /*
  144. * Scan the array of match functions and attach
  145. * any components which are found to this adev.
  146. */
  147. for (i = 0; i < match->num; i++) {
  148. struct component_match_array *mc = &match->compare[i];
  149. struct component *c;
  150. dev_dbg(adev->parent, "Looking for component %zu\n", i);
  151. if (match->compare[i].component)
  152. continue;
  153. c = find_component(adev, mc);
  154. if (!c) {
  155. ret = -ENXIO;
  156. break;
  157. }
  158. dev_dbg(adev->parent, "found component %s, duplicate %u\n",
  159. dev_name(c->dev), !!c->adev);
  160. /* Attach this component to the adev */
  161. match->compare[i].duplicate = !!c->adev;
  162. match->compare[i].component = c;
  163. c->adev = adev;
  164. }
  165. return ret;
  166. }
  167. /* Detach component from associated aggregate_device */
  168. static void remove_component(struct aggregate_device *adev, struct component *c)
  169. {
  170. size_t i;
  171. /* Detach the component from this adev. */
  172. for (i = 0; i < adev->match->num; i++)
  173. if (adev->match->compare[i].component == c)
  174. adev->match->compare[i].component = NULL;
  175. }
  176. /*
  177. * Try to bring up an aggregate device. If component is NULL, we're interested
  178. * in this aggregate device, otherwise it's a component which must be present
  179. * to try and bring up the aggregate device.
  180. *
  181. * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
  182. */
  183. static int try_to_bring_up_aggregate_device(struct aggregate_device *adev,
  184. struct component *component)
  185. {
  186. int ret;
  187. dev_dbg(adev->parent, "trying to bring up adev\n");
  188. if (find_components(adev)) {
  189. dev_dbg(adev->parent, "master has incomplete components\n");
  190. return 0;
  191. }
  192. if (component && component->adev != adev) {
  193. dev_dbg(adev->parent, "master is not for this component (%s)\n",
  194. dev_name(component->dev));
  195. return 0;
  196. }
  197. if (!devres_open_group(adev->parent, adev, GFP_KERNEL))
  198. return -ENOMEM;
  199. /* Found all components */
  200. ret = adev->ops->bind(adev->parent);
  201. if (ret < 0) {
  202. devres_release_group(adev->parent, NULL);
  203. if (ret != -EPROBE_DEFER)
  204. dev_info(adev->parent, "adev bind failed: %d\n", ret);
  205. return ret;
  206. }
  207. devres_close_group(adev->parent, NULL);
  208. adev->bound = true;
  209. return 1;
  210. }
  211. static int try_to_bring_up_masters(struct component *component)
  212. {
  213. struct aggregate_device *adev;
  214. int ret = 0;
  215. list_for_each_entry(adev, &aggregate_devices, node) {
  216. if (!adev->bound) {
  217. ret = try_to_bring_up_aggregate_device(adev, component);
  218. if (ret != 0)
  219. break;
  220. }
  221. }
  222. return ret;
  223. }
  224. static void take_down_aggregate_device(struct aggregate_device *adev)
  225. {
  226. if (adev->bound) {
  227. adev->ops->unbind(adev->parent);
  228. devres_release_group(adev->parent, adev);
  229. adev->bound = false;
  230. }
  231. }
  232. /**
  233. * component_compare_of - A common component compare function for of_node
  234. * @dev: component device
  235. * @data: @compare_data from component_match_add_release()
  236. *
  237. * A common compare function when compare_data is device of_node. e.g.
  238. * component_match_add_release(masterdev, &match, component_release_of,
  239. * component_compare_of, component_dev_of_node)
  240. */
  241. int component_compare_of(struct device *dev, void *data)
  242. {
  243. return device_match_of_node(dev, data);
  244. }
  245. EXPORT_SYMBOL_GPL(component_compare_of);
  246. /**
  247. * component_release_of - A common component release function for of_node
  248. * @dev: component device
  249. * @data: @compare_data from component_match_add_release()
  250. *
  251. * About the example, Please see component_compare_of().
  252. */
  253. void component_release_of(struct device *dev, void *data)
  254. {
  255. of_node_put(data);
  256. }
  257. EXPORT_SYMBOL_GPL(component_release_of);
  258. /**
  259. * component_compare_dev - A common component compare function for dev
  260. * @dev: component device
  261. * @data: @compare_data from component_match_add_release()
  262. *
  263. * A common compare function when compare_data is struce device. e.g.
  264. * component_match_add(masterdev, &match, component_compare_dev, component_dev)
  265. */
  266. int component_compare_dev(struct device *dev, void *data)
  267. {
  268. return dev == data;
  269. }
  270. EXPORT_SYMBOL_GPL(component_compare_dev);
  271. /**
  272. * component_compare_dev_name - A common component compare function for device name
  273. * @dev: component device
  274. * @data: @compare_data from component_match_add_release()
  275. *
  276. * A common compare function when compare_data is device name string. e.g.
  277. * component_match_add(masterdev, &match, component_compare_dev_name,
  278. * "component_dev_name")
  279. */
  280. int component_compare_dev_name(struct device *dev, void *data)
  281. {
  282. return device_match_name(dev, data);
  283. }
  284. EXPORT_SYMBOL_GPL(component_compare_dev_name);
  285. static void devm_component_match_release(struct device *parent, void *res)
  286. {
  287. struct component_match *match = res;
  288. unsigned int i;
  289. for (i = 0; i < match->num; i++) {
  290. struct component_match_array *mc = &match->compare[i];
  291. if (mc->release)
  292. mc->release(parent, mc->data);
  293. }
  294. kfree(match->compare);
  295. }
  296. static int component_match_realloc(struct component_match *match, size_t num)
  297. {
  298. struct component_match_array *new;
  299. if (match->alloc == num)
  300. return 0;
  301. new = kmalloc_array(num, sizeof(*new), GFP_KERNEL);
  302. if (!new)
  303. return -ENOMEM;
  304. if (match->compare) {
  305. memcpy(new, match->compare, sizeof(*new) *
  306. min(match->num, num));
  307. kfree(match->compare);
  308. }
  309. match->compare = new;
  310. match->alloc = num;
  311. return 0;
  312. }
  313. static void __component_match_add(struct device *parent,
  314. struct component_match **matchptr,
  315. void (*release)(struct device *, void *),
  316. int (*compare)(struct device *, void *),
  317. int (*compare_typed)(struct device *, int, void *),
  318. void *compare_data)
  319. {
  320. struct component_match *match = *matchptr;
  321. if (IS_ERR(match))
  322. return;
  323. if (!match) {
  324. match = devres_alloc(devm_component_match_release,
  325. sizeof(*match), GFP_KERNEL);
  326. if (!match) {
  327. *matchptr = ERR_PTR(-ENOMEM);
  328. return;
  329. }
  330. devres_add(parent, match);
  331. *matchptr = match;
  332. }
  333. if (match->num == match->alloc) {
  334. size_t new_size = match->alloc + 16;
  335. int ret;
  336. ret = component_match_realloc(match, new_size);
  337. if (ret) {
  338. *matchptr = ERR_PTR(ret);
  339. return;
  340. }
  341. }
  342. match->compare[match->num].compare = compare;
  343. match->compare[match->num].compare_typed = compare_typed;
  344. match->compare[match->num].release = release;
  345. match->compare[match->num].data = compare_data;
  346. match->compare[match->num].component = NULL;
  347. match->num++;
  348. }
  349. /**
  350. * component_match_add_release - add a component match entry with release callback
  351. * @parent: parent device of the aggregate driver
  352. * @matchptr: pointer to the list of component matches
  353. * @release: release function for @compare_data
  354. * @compare: compare function to match against all components
  355. * @compare_data: opaque pointer passed to the @compare function
  356. *
  357. * Adds a new component match to the list stored in @matchptr, which the
  358. * aggregate driver needs to function. The list of component matches pointed to
  359. * by @matchptr must be initialized to NULL before adding the first match. This
  360. * only matches against components added with component_add().
  361. *
  362. * The allocated match list in @matchptr is automatically released using devm
  363. * actions, where upon @release will be called to free any references held by
  364. * @compare_data, e.g. when @compare_data is a &device_node that must be
  365. * released with of_node_put().
  366. *
  367. * See also component_match_add() and component_match_add_typed().
  368. */
  369. void component_match_add_release(struct device *parent,
  370. struct component_match **matchptr,
  371. void (*release)(struct device *, void *),
  372. int (*compare)(struct device *, void *), void *compare_data)
  373. {
  374. __component_match_add(parent, matchptr, release, compare, NULL,
  375. compare_data);
  376. }
  377. EXPORT_SYMBOL(component_match_add_release);
  378. /**
  379. * component_match_add_typed - add a component match entry for a typed component
  380. * @parent: parent device of the aggregate driver
  381. * @matchptr: pointer to the list of component matches
  382. * @compare_typed: compare function to match against all typed components
  383. * @compare_data: opaque pointer passed to the @compare function
  384. *
  385. * Adds a new component match to the list stored in @matchptr, which the
  386. * aggregate driver needs to function. The list of component matches pointed to
  387. * by @matchptr must be initialized to NULL before adding the first match. This
  388. * only matches against components added with component_add_typed().
  389. *
  390. * The allocated match list in @matchptr is automatically released using devm
  391. * actions.
  392. *
  393. * See also component_match_add_release() and component_match_add_typed().
  394. */
  395. void component_match_add_typed(struct device *parent,
  396. struct component_match **matchptr,
  397. int (*compare_typed)(struct device *, int, void *), void *compare_data)
  398. {
  399. __component_match_add(parent, matchptr, NULL, NULL, compare_typed,
  400. compare_data);
  401. }
  402. EXPORT_SYMBOL(component_match_add_typed);
  403. static void free_aggregate_device(struct aggregate_device *adev)
  404. {
  405. struct component_match *match = adev->match;
  406. int i;
  407. component_debugfs_del(adev);
  408. list_del(&adev->node);
  409. if (match) {
  410. for (i = 0; i < match->num; i++) {
  411. struct component *c = match->compare[i].component;
  412. if (c)
  413. c->adev = NULL;
  414. }
  415. }
  416. kfree(adev);
  417. }
  418. /**
  419. * component_master_add_with_match - register an aggregate driver
  420. * @parent: parent device of the aggregate driver
  421. * @ops: callbacks for the aggregate driver
  422. * @match: component match list for the aggregate driver
  423. *
  424. * Registers a new aggregate driver consisting of the components added to @match
  425. * by calling one of the component_match_add() functions. Once all components in
  426. * @match are available, it will be assembled by calling
  427. * &component_master_ops.bind from @ops. Must be unregistered by calling
  428. * component_master_del().
  429. */
  430. int component_master_add_with_match(struct device *parent,
  431. const struct component_master_ops *ops,
  432. struct component_match *match)
  433. {
  434. struct aggregate_device *adev;
  435. int ret;
  436. /* Reallocate the match array for its true size */
  437. ret = component_match_realloc(match, match->num);
  438. if (ret)
  439. return ret;
  440. adev = kzalloc(sizeof(*adev), GFP_KERNEL);
  441. if (!adev)
  442. return -ENOMEM;
  443. adev->parent = parent;
  444. adev->ops = ops;
  445. adev->match = match;
  446. component_debugfs_add(adev);
  447. /* Add to the list of available aggregate devices. */
  448. mutex_lock(&component_mutex);
  449. list_add(&adev->node, &aggregate_devices);
  450. ret = try_to_bring_up_aggregate_device(adev, NULL);
  451. if (ret < 0)
  452. free_aggregate_device(adev);
  453. mutex_unlock(&component_mutex);
  454. return ret < 0 ? ret : 0;
  455. }
  456. EXPORT_SYMBOL_GPL(component_master_add_with_match);
  457. /**
  458. * component_master_del - unregister an aggregate driver
  459. * @parent: parent device of the aggregate driver
  460. * @ops: callbacks for the aggregate driver
  461. *
  462. * Unregisters an aggregate driver registered with
  463. * component_master_add_with_match(). If necessary the aggregate driver is first
  464. * disassembled by calling &component_master_ops.unbind from @ops.
  465. */
  466. void component_master_del(struct device *parent,
  467. const struct component_master_ops *ops)
  468. {
  469. struct aggregate_device *adev;
  470. mutex_lock(&component_mutex);
  471. adev = __aggregate_find(parent, ops);
  472. if (adev) {
  473. take_down_aggregate_device(adev);
  474. free_aggregate_device(adev);
  475. }
  476. mutex_unlock(&component_mutex);
  477. }
  478. EXPORT_SYMBOL_GPL(component_master_del);
  479. static void component_unbind(struct component *component,
  480. struct aggregate_device *adev, void *data)
  481. {
  482. WARN_ON(!component->bound);
  483. if (component->ops && component->ops->unbind)
  484. component->ops->unbind(component->dev, adev->parent, data);
  485. component->bound = false;
  486. /* Release all resources claimed in the binding of this component */
  487. devres_release_group(component->dev, component);
  488. }
  489. /**
  490. * component_unbind_all - unbind all components of an aggregate driver
  491. * @parent: parent device of the aggregate driver
  492. * @data: opaque pointer, passed to all components
  493. *
  494. * Unbinds all components of the aggregate device by passing @data to their
  495. * &component_ops.unbind functions. Should be called from
  496. * &component_master_ops.unbind.
  497. */
  498. void component_unbind_all(struct device *parent, void *data)
  499. {
  500. struct aggregate_device *adev;
  501. struct component *c;
  502. size_t i;
  503. WARN_ON(!mutex_is_locked(&component_mutex));
  504. adev = __aggregate_find(parent, NULL);
  505. if (!adev)
  506. return;
  507. /* Unbind components in reverse order */
  508. for (i = adev->match->num; i--; )
  509. if (!adev->match->compare[i].duplicate) {
  510. c = adev->match->compare[i].component;
  511. component_unbind(c, adev, data);
  512. }
  513. }
  514. EXPORT_SYMBOL_GPL(component_unbind_all);
  515. static int component_bind(struct component *component, struct aggregate_device *adev,
  516. void *data)
  517. {
  518. int ret;
  519. /*
  520. * Each component initialises inside its own devres group.
  521. * This allows us to roll-back a failed component without
  522. * affecting anything else.
  523. */
  524. if (!devres_open_group(adev->parent, NULL, GFP_KERNEL))
  525. return -ENOMEM;
  526. /*
  527. * Also open a group for the device itself: this allows us
  528. * to release the resources claimed against the sub-device
  529. * at the appropriate moment.
  530. */
  531. if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
  532. devres_release_group(adev->parent, NULL);
  533. return -ENOMEM;
  534. }
  535. dev_dbg(adev->parent, "binding %s (ops %ps)\n",
  536. dev_name(component->dev), component->ops);
  537. ret = component->ops->bind(component->dev, adev->parent, data);
  538. if (!ret) {
  539. component->bound = true;
  540. /*
  541. * Close the component device's group so that resources
  542. * allocated in the binding are encapsulated for removal
  543. * at unbind. Remove the group on the DRM device as we
  544. * can clean those resources up independently.
  545. */
  546. devres_close_group(component->dev, NULL);
  547. devres_remove_group(adev->parent, NULL);
  548. dev_info(adev->parent, "bound %s (ops %ps)\n",
  549. dev_name(component->dev), component->ops);
  550. } else {
  551. devres_release_group(component->dev, NULL);
  552. devres_release_group(adev->parent, NULL);
  553. if (ret != -EPROBE_DEFER)
  554. dev_err(adev->parent, "failed to bind %s (ops %ps): %d\n",
  555. dev_name(component->dev), component->ops, ret);
  556. }
  557. return ret;
  558. }
  559. /**
  560. * component_bind_all - bind all components of an aggregate driver
  561. * @parent: parent device of the aggregate driver
  562. * @data: opaque pointer, passed to all components
  563. *
  564. * Binds all components of the aggregate @dev by passing @data to their
  565. * &component_ops.bind functions. Should be called from
  566. * &component_master_ops.bind.
  567. */
  568. int component_bind_all(struct device *parent, void *data)
  569. {
  570. struct aggregate_device *adev;
  571. struct component *c;
  572. size_t i;
  573. int ret = 0;
  574. WARN_ON(!mutex_is_locked(&component_mutex));
  575. adev = __aggregate_find(parent, NULL);
  576. if (!adev)
  577. return -EINVAL;
  578. /* Bind components in match order */
  579. for (i = 0; i < adev->match->num; i++)
  580. if (!adev->match->compare[i].duplicate) {
  581. c = adev->match->compare[i].component;
  582. ret = component_bind(c, adev, data);
  583. if (ret)
  584. break;
  585. }
  586. if (ret != 0) {
  587. for (; i > 0; i--)
  588. if (!adev->match->compare[i - 1].duplicate) {
  589. c = adev->match->compare[i - 1].component;
  590. component_unbind(c, adev, data);
  591. }
  592. }
  593. return ret;
  594. }
  595. EXPORT_SYMBOL_GPL(component_bind_all);
  596. static int __component_add(struct device *dev, const struct component_ops *ops,
  597. int subcomponent)
  598. {
  599. struct component *component;
  600. int ret;
  601. component = kzalloc(sizeof(*component), GFP_KERNEL);
  602. if (!component)
  603. return -ENOMEM;
  604. component->ops = ops;
  605. component->dev = dev;
  606. component->subcomponent = subcomponent;
  607. dev_dbg(dev, "adding component (ops %ps)\n", ops);
  608. mutex_lock(&component_mutex);
  609. list_add_tail(&component->node, &component_list);
  610. ret = try_to_bring_up_masters(component);
  611. if (ret < 0) {
  612. if (component->adev)
  613. remove_component(component->adev, component);
  614. list_del(&component->node);
  615. kfree(component);
  616. }
  617. mutex_unlock(&component_mutex);
  618. return ret < 0 ? ret : 0;
  619. }
  620. /**
  621. * component_add_typed - register a component
  622. * @dev: component device
  623. * @ops: component callbacks
  624. * @subcomponent: nonzero identifier for subcomponents
  625. *
  626. * Register a new component for @dev. Functions in @ops will be call when the
  627. * aggregate driver is ready to bind the overall driver by calling
  628. * component_bind_all(). See also &struct component_ops.
  629. *
  630. * @subcomponent must be nonzero and is used to differentiate between multiple
  631. * components registerd on the same device @dev. These components are match
  632. * using component_match_add_typed().
  633. *
  634. * The component needs to be unregistered at driver unload/disconnect by
  635. * calling component_del().
  636. *
  637. * See also component_add().
  638. */
  639. int component_add_typed(struct device *dev, const struct component_ops *ops,
  640. int subcomponent)
  641. {
  642. if (WARN_ON(subcomponent == 0))
  643. return -EINVAL;
  644. return __component_add(dev, ops, subcomponent);
  645. }
  646. EXPORT_SYMBOL_GPL(component_add_typed);
  647. /**
  648. * component_add - register a component
  649. * @dev: component device
  650. * @ops: component callbacks
  651. *
  652. * Register a new component for @dev. Functions in @ops will be called when the
  653. * aggregate driver is ready to bind the overall driver by calling
  654. * component_bind_all(). See also &struct component_ops.
  655. *
  656. * The component needs to be unregistered at driver unload/disconnect by
  657. * calling component_del().
  658. *
  659. * See also component_add_typed() for a variant that allows multipled different
  660. * components on the same device.
  661. */
  662. int component_add(struct device *dev, const struct component_ops *ops)
  663. {
  664. return __component_add(dev, ops, 0);
  665. }
  666. EXPORT_SYMBOL_GPL(component_add);
  667. /**
  668. * component_del - unregister a component
  669. * @dev: component device
  670. * @ops: component callbacks
  671. *
  672. * Unregister a component added with component_add(). If the component is bound
  673. * into an aggregate driver, this will force the entire aggregate driver, including
  674. * all its components, to be unbound.
  675. */
  676. void component_del(struct device *dev, const struct component_ops *ops)
  677. {
  678. struct component *c, *component = NULL;
  679. mutex_lock(&component_mutex);
  680. list_for_each_entry(c, &component_list, node)
  681. if (c->dev == dev && c->ops == ops) {
  682. list_del(&c->node);
  683. component = c;
  684. break;
  685. }
  686. if (component && component->adev) {
  687. take_down_aggregate_device(component->adev);
  688. remove_component(component->adev, component);
  689. }
  690. mutex_unlock(&component_mutex);
  691. WARN_ON(!component);
  692. kfree(component);
  693. }
  694. EXPORT_SYMBOL_GPL(component_del);