v4l2-async.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * V4L2 asynchronous subdevice registration API
  3. *
  4. * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/i2c.h>
  13. #include <linux/list.h>
  14. #include <linux/mm.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/types.h>
  21. #include <media/v4l2-async.h>
  22. #include <media/v4l2-device.h>
  23. #include <media/v4l2-fwnode.h>
  24. #include <media/v4l2-subdev.h>
  25. static int v4l2_async_notifier_call_bound(struct v4l2_async_notifier *n,
  26. struct v4l2_subdev *subdev,
  27. struct v4l2_async_subdev *asd)
  28. {
  29. if (!n->ops || !n->ops->bound)
  30. return 0;
  31. return n->ops->bound(n, subdev, asd);
  32. }
  33. static void v4l2_async_notifier_call_unbind(struct v4l2_async_notifier *n,
  34. struct v4l2_subdev *subdev,
  35. struct v4l2_async_subdev *asd)
  36. {
  37. if (!n->ops || !n->ops->unbind)
  38. return;
  39. n->ops->unbind(n, subdev, asd);
  40. }
  41. static int v4l2_async_notifier_call_complete(struct v4l2_async_notifier *n)
  42. {
  43. if (!n->ops || !n->ops->complete)
  44. return 0;
  45. return n->ops->complete(n);
  46. }
  47. static bool match_i2c(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
  48. {
  49. #if IS_ENABLED(CONFIG_I2C)
  50. struct i2c_client *client = i2c_verify_client(sd->dev);
  51. return client &&
  52. asd->match.i2c.adapter_id == client->adapter->nr &&
  53. asd->match.i2c.address == client->addr;
  54. #else
  55. return false;
  56. #endif
  57. }
  58. static bool match_devname(struct v4l2_subdev *sd,
  59. struct v4l2_async_subdev *asd)
  60. {
  61. return !strcmp(asd->match.device_name, dev_name(sd->dev));
  62. }
  63. static bool match_fwnode(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
  64. {
  65. return sd->fwnode == asd->match.fwnode;
  66. }
  67. static bool match_custom(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
  68. {
  69. if (!asd->match.custom.match)
  70. /* Match always */
  71. return true;
  72. return asd->match.custom.match(sd->dev, asd);
  73. }
  74. static LIST_HEAD(subdev_list);
  75. static LIST_HEAD(notifier_list);
  76. static DEFINE_MUTEX(list_lock);
  77. static struct v4l2_async_subdev *
  78. v4l2_async_find_match(struct v4l2_async_notifier *notifier,
  79. struct v4l2_subdev *sd)
  80. {
  81. bool (*match)(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd);
  82. struct v4l2_async_subdev *asd;
  83. list_for_each_entry(asd, &notifier->waiting, list) {
  84. /* bus_type has been verified valid before */
  85. switch (asd->match_type) {
  86. case V4L2_ASYNC_MATCH_CUSTOM:
  87. match = match_custom;
  88. break;
  89. case V4L2_ASYNC_MATCH_DEVNAME:
  90. match = match_devname;
  91. break;
  92. case V4L2_ASYNC_MATCH_I2C:
  93. match = match_i2c;
  94. break;
  95. case V4L2_ASYNC_MATCH_FWNODE:
  96. match = match_fwnode;
  97. break;
  98. default:
  99. /* Cannot happen, unless someone breaks us */
  100. WARN_ON(true);
  101. return NULL;
  102. }
  103. /* match cannot be NULL here */
  104. if (match(sd, asd))
  105. return asd;
  106. }
  107. return NULL;
  108. }
  109. /* Compare two async sub-device descriptors for equivalence */
  110. static bool asd_equal(struct v4l2_async_subdev *asd_x,
  111. struct v4l2_async_subdev *asd_y)
  112. {
  113. if (asd_x->match_type != asd_y->match_type)
  114. return false;
  115. switch (asd_x->match_type) {
  116. case V4L2_ASYNC_MATCH_DEVNAME:
  117. return strcmp(asd_x->match.device_name,
  118. asd_y->match.device_name) == 0;
  119. case V4L2_ASYNC_MATCH_I2C:
  120. return asd_x->match.i2c.adapter_id ==
  121. asd_y->match.i2c.adapter_id &&
  122. asd_x->match.i2c.address ==
  123. asd_y->match.i2c.address;
  124. case V4L2_ASYNC_MATCH_FWNODE:
  125. return asd_x->match.fwnode == asd_y->match.fwnode;
  126. default:
  127. break;
  128. }
  129. return false;
  130. }
  131. /* Find the sub-device notifier registered by a sub-device driver. */
  132. static struct v4l2_async_notifier *
  133. v4l2_async_find_subdev_notifier(struct v4l2_subdev *sd)
  134. {
  135. struct v4l2_async_notifier *n;
  136. list_for_each_entry(n, &notifier_list, list)
  137. if (n->sd == sd)
  138. return n;
  139. return NULL;
  140. }
  141. /* Get v4l2_device related to the notifier if one can be found. */
  142. static struct v4l2_device *
  143. v4l2_async_notifier_find_v4l2_dev(struct v4l2_async_notifier *notifier)
  144. {
  145. while (notifier->parent)
  146. notifier = notifier->parent;
  147. return notifier->v4l2_dev;
  148. }
  149. /*
  150. * Return true if all child sub-device notifiers are complete, false otherwise.
  151. */
  152. static bool
  153. v4l2_async_notifier_can_complete(struct v4l2_async_notifier *notifier)
  154. {
  155. struct v4l2_subdev *sd;
  156. if (!list_empty(&notifier->waiting))
  157. return false;
  158. list_for_each_entry(sd, &notifier->done, async_list) {
  159. struct v4l2_async_notifier *subdev_notifier =
  160. v4l2_async_find_subdev_notifier(sd);
  161. if (subdev_notifier &&
  162. !v4l2_async_notifier_can_complete(subdev_notifier))
  163. return false;
  164. }
  165. return true;
  166. }
  167. /*
  168. * Complete the master notifier if possible. This is done when all async
  169. * sub-devices have been bound; v4l2_device is also available then.
  170. */
  171. static int
  172. v4l2_async_notifier_try_complete(struct v4l2_async_notifier *notifier)
  173. {
  174. /* Quick check whether there are still more sub-devices here. */
  175. if (!list_empty(&notifier->waiting))
  176. return 0;
  177. /* Check the entire notifier tree; find the root notifier first. */
  178. while (notifier->parent)
  179. notifier = notifier->parent;
  180. /* This is root if it has v4l2_dev. */
  181. if (!notifier->v4l2_dev)
  182. return 0;
  183. /* Is everything ready? */
  184. if (!v4l2_async_notifier_can_complete(notifier))
  185. return 0;
  186. return v4l2_async_notifier_call_complete(notifier);
  187. }
  188. static int
  189. v4l2_async_notifier_try_all_subdevs(struct v4l2_async_notifier *notifier);
  190. static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier,
  191. struct v4l2_device *v4l2_dev,
  192. struct v4l2_subdev *sd,
  193. struct v4l2_async_subdev *asd)
  194. {
  195. struct v4l2_async_notifier *subdev_notifier;
  196. int ret;
  197. ret = v4l2_device_register_subdev(v4l2_dev, sd);
  198. if (ret < 0)
  199. return ret;
  200. ret = v4l2_async_notifier_call_bound(notifier, sd, asd);
  201. if (ret < 0) {
  202. v4l2_device_unregister_subdev(sd);
  203. return ret;
  204. }
  205. /* Remove from the waiting list */
  206. list_del(&asd->list);
  207. sd->asd = asd;
  208. sd->notifier = notifier;
  209. /* Move from the global subdevice list to notifier's done */
  210. list_move(&sd->async_list, &notifier->done);
  211. /*
  212. * See if the sub-device has a notifier. If not, return here.
  213. */
  214. subdev_notifier = v4l2_async_find_subdev_notifier(sd);
  215. if (!subdev_notifier || subdev_notifier->parent)
  216. return 0;
  217. /*
  218. * Proceed with checking for the sub-device notifier's async
  219. * sub-devices, and return the result. The error will be handled by the
  220. * caller.
  221. */
  222. subdev_notifier->parent = notifier;
  223. return v4l2_async_notifier_try_all_subdevs(subdev_notifier);
  224. }
  225. /* Test all async sub-devices in a notifier for a match. */
  226. static int
  227. v4l2_async_notifier_try_all_subdevs(struct v4l2_async_notifier *notifier)
  228. {
  229. struct v4l2_device *v4l2_dev =
  230. v4l2_async_notifier_find_v4l2_dev(notifier);
  231. struct v4l2_subdev *sd;
  232. if (!v4l2_dev)
  233. return 0;
  234. again:
  235. list_for_each_entry(sd, &subdev_list, async_list) {
  236. struct v4l2_async_subdev *asd;
  237. int ret;
  238. asd = v4l2_async_find_match(notifier, sd);
  239. if (!asd)
  240. continue;
  241. ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
  242. if (ret < 0)
  243. return ret;
  244. /*
  245. * v4l2_async_match_notify() may lead to registering a
  246. * new notifier and thus changing the async subdevs
  247. * list. In order to proceed safely from here, restart
  248. * parsing the list from the beginning.
  249. */
  250. goto again;
  251. }
  252. return 0;
  253. }
  254. static void v4l2_async_cleanup(struct v4l2_subdev *sd)
  255. {
  256. v4l2_device_unregister_subdev(sd);
  257. /*
  258. * Subdevice driver will reprobe and put the subdev back
  259. * onto the list
  260. */
  261. list_del_init(&sd->async_list);
  262. sd->asd = NULL;
  263. }
  264. /* Unbind all sub-devices in the notifier tree. */
  265. static void
  266. v4l2_async_notifier_unbind_all_subdevs(struct v4l2_async_notifier *notifier)
  267. {
  268. struct v4l2_subdev *sd, *tmp;
  269. list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
  270. struct v4l2_async_notifier *subdev_notifier =
  271. v4l2_async_find_subdev_notifier(sd);
  272. if (subdev_notifier)
  273. v4l2_async_notifier_unbind_all_subdevs(subdev_notifier);
  274. v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
  275. v4l2_async_cleanup(sd);
  276. list_move(&sd->async_list, &subdev_list);
  277. }
  278. notifier->parent = NULL;
  279. }
  280. /* See if an async sub-device can be found in a notifier's lists. */
  281. static bool
  282. __v4l2_async_notifier_has_async_subdev(struct v4l2_async_notifier *notifier,
  283. struct v4l2_async_subdev *asd)
  284. {
  285. struct v4l2_async_subdev *asd_y;
  286. struct v4l2_subdev *sd;
  287. list_for_each_entry(asd_y, &notifier->waiting, list)
  288. if (asd_equal(asd, asd_y))
  289. return true;
  290. list_for_each_entry(sd, &notifier->done, async_list) {
  291. if (WARN_ON(!sd->asd))
  292. continue;
  293. if (asd_equal(asd, sd->asd))
  294. return true;
  295. }
  296. return false;
  297. }
  298. /*
  299. * Find out whether an async sub-device was set up already or
  300. * whether it exists in a given notifier before @this_index.
  301. * If @this_index < 0, search the notifier's entire @asd_list.
  302. */
  303. static bool
  304. v4l2_async_notifier_has_async_subdev(struct v4l2_async_notifier *notifier,
  305. struct v4l2_async_subdev *asd,
  306. int this_index)
  307. {
  308. struct v4l2_async_subdev *asd_y;
  309. int j = 0;
  310. lockdep_assert_held(&list_lock);
  311. /* Check that an asd is not being added more than once. */
  312. list_for_each_entry(asd_y, &notifier->asd_list, asd_list) {
  313. if (this_index >= 0 && j++ >= this_index)
  314. break;
  315. if (asd_equal(asd, asd_y))
  316. return true;
  317. }
  318. /* Check that an asd does not exist in other notifiers. */
  319. list_for_each_entry(notifier, &notifier_list, list)
  320. if (__v4l2_async_notifier_has_async_subdev(notifier, asd))
  321. return true;
  322. return false;
  323. }
  324. static int v4l2_async_notifier_asd_valid(struct v4l2_async_notifier *notifier,
  325. struct v4l2_async_subdev *asd,
  326. int this_index)
  327. {
  328. struct device *dev =
  329. notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL;
  330. if (!asd)
  331. return -EINVAL;
  332. switch (asd->match_type) {
  333. case V4L2_ASYNC_MATCH_CUSTOM:
  334. case V4L2_ASYNC_MATCH_DEVNAME:
  335. case V4L2_ASYNC_MATCH_I2C:
  336. case V4L2_ASYNC_MATCH_FWNODE:
  337. if (v4l2_async_notifier_has_async_subdev(notifier, asd,
  338. this_index)) {
  339. dev_dbg(dev, "subdev descriptor already listed in this or other notifiers\n");
  340. return -EEXIST;
  341. }
  342. break;
  343. default:
  344. dev_err(dev, "Invalid match type %u on %p\n",
  345. asd->match_type, asd);
  346. return -EINVAL;
  347. }
  348. return 0;
  349. }
  350. void v4l2_async_notifier_init(struct v4l2_async_notifier *notifier)
  351. {
  352. mutex_lock(&list_lock);
  353. INIT_LIST_HEAD(&notifier->asd_list);
  354. mutex_unlock(&list_lock);
  355. }
  356. EXPORT_SYMBOL(v4l2_async_notifier_init);
  357. static int __v4l2_async_notifier_register(struct v4l2_async_notifier *notifier)
  358. {
  359. struct v4l2_async_subdev *asd;
  360. int ret, i = 0;
  361. INIT_LIST_HEAD(&notifier->waiting);
  362. INIT_LIST_HEAD(&notifier->done);
  363. mutex_lock(&list_lock);
  364. list_for_each_entry(asd, &notifier->asd_list, asd_list) {
  365. ret = v4l2_async_notifier_asd_valid(notifier, asd, i++);
  366. if (ret)
  367. goto err_unlock;
  368. list_add_tail(&asd->list, &notifier->waiting);
  369. }
  370. ret = v4l2_async_notifier_try_all_subdevs(notifier);
  371. if (ret < 0)
  372. goto err_unbind;
  373. ret = v4l2_async_notifier_try_complete(notifier);
  374. if (ret < 0)
  375. goto err_unbind;
  376. /* Keep also completed notifiers on the list */
  377. list_add(&notifier->list, &notifier_list);
  378. mutex_unlock(&list_lock);
  379. return 0;
  380. err_unbind:
  381. /*
  382. * On failure, unbind all sub-devices registered through this notifier.
  383. */
  384. v4l2_async_notifier_unbind_all_subdevs(notifier);
  385. err_unlock:
  386. mutex_unlock(&list_lock);
  387. return ret;
  388. }
  389. int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
  390. struct v4l2_async_notifier *notifier)
  391. {
  392. int ret;
  393. if (WARN_ON(!v4l2_dev || notifier->sd))
  394. return -EINVAL;
  395. notifier->v4l2_dev = v4l2_dev;
  396. ret = __v4l2_async_notifier_register(notifier);
  397. if (ret)
  398. notifier->v4l2_dev = NULL;
  399. return ret;
  400. }
  401. EXPORT_SYMBOL(v4l2_async_notifier_register);
  402. int v4l2_async_subdev_notifier_register(struct v4l2_subdev *sd,
  403. struct v4l2_async_notifier *notifier)
  404. {
  405. int ret;
  406. if (WARN_ON(!sd || notifier->v4l2_dev))
  407. return -EINVAL;
  408. notifier->sd = sd;
  409. ret = __v4l2_async_notifier_register(notifier);
  410. if (ret)
  411. notifier->sd = NULL;
  412. return ret;
  413. }
  414. EXPORT_SYMBOL(v4l2_async_subdev_notifier_register);
  415. static void
  416. __v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
  417. {
  418. if (!notifier || (!notifier->v4l2_dev && !notifier->sd))
  419. return;
  420. v4l2_async_notifier_unbind_all_subdevs(notifier);
  421. notifier->sd = NULL;
  422. notifier->v4l2_dev = NULL;
  423. list_del(&notifier->list);
  424. }
  425. void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
  426. {
  427. mutex_lock(&list_lock);
  428. __v4l2_async_notifier_unregister(notifier);
  429. mutex_unlock(&list_lock);
  430. }
  431. EXPORT_SYMBOL(v4l2_async_notifier_unregister);
  432. static void __v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier)
  433. {
  434. struct v4l2_async_subdev *asd, *tmp;
  435. if (!notifier)
  436. return;
  437. list_for_each_entry_safe(asd, tmp, &notifier->asd_list, asd_list) {
  438. switch (asd->match_type) {
  439. case V4L2_ASYNC_MATCH_FWNODE:
  440. fwnode_handle_put(asd->match.fwnode);
  441. break;
  442. default:
  443. break;
  444. }
  445. list_del(&asd->asd_list);
  446. kfree(asd);
  447. }
  448. }
  449. void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier)
  450. {
  451. mutex_lock(&list_lock);
  452. __v4l2_async_notifier_cleanup(notifier);
  453. mutex_unlock(&list_lock);
  454. }
  455. EXPORT_SYMBOL_GPL(v4l2_async_notifier_cleanup);
  456. int v4l2_async_notifier_add_subdev(struct v4l2_async_notifier *notifier,
  457. struct v4l2_async_subdev *asd)
  458. {
  459. int ret;
  460. mutex_lock(&list_lock);
  461. ret = v4l2_async_notifier_asd_valid(notifier, asd, -1);
  462. if (ret)
  463. goto unlock;
  464. list_add_tail(&asd->asd_list, &notifier->asd_list);
  465. unlock:
  466. mutex_unlock(&list_lock);
  467. return ret;
  468. }
  469. EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_subdev);
  470. struct v4l2_async_subdev *
  471. v4l2_async_notifier_add_fwnode_subdev(struct v4l2_async_notifier *notifier,
  472. struct fwnode_handle *fwnode,
  473. unsigned int asd_struct_size)
  474. {
  475. struct v4l2_async_subdev *asd;
  476. int ret;
  477. asd = kzalloc(asd_struct_size, GFP_KERNEL);
  478. if (!asd)
  479. return ERR_PTR(-ENOMEM);
  480. asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
  481. asd->match.fwnode = fwnode;
  482. ret = v4l2_async_notifier_add_subdev(notifier, asd);
  483. if (ret) {
  484. kfree(asd);
  485. return ERR_PTR(ret);
  486. }
  487. return asd;
  488. }
  489. EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_fwnode_subdev);
  490. struct v4l2_async_subdev *
  491. v4l2_async_notifier_add_i2c_subdev(struct v4l2_async_notifier *notifier,
  492. int adapter_id, unsigned short address,
  493. unsigned int asd_struct_size)
  494. {
  495. struct v4l2_async_subdev *asd;
  496. int ret;
  497. asd = kzalloc(asd_struct_size, GFP_KERNEL);
  498. if (!asd)
  499. return ERR_PTR(-ENOMEM);
  500. asd->match_type = V4L2_ASYNC_MATCH_I2C;
  501. asd->match.i2c.adapter_id = adapter_id;
  502. asd->match.i2c.address = address;
  503. ret = v4l2_async_notifier_add_subdev(notifier, asd);
  504. if (ret) {
  505. kfree(asd);
  506. return ERR_PTR(ret);
  507. }
  508. return asd;
  509. }
  510. EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_i2c_subdev);
  511. struct v4l2_async_subdev *
  512. v4l2_async_notifier_add_devname_subdev(struct v4l2_async_notifier *notifier,
  513. const char *device_name,
  514. unsigned int asd_struct_size)
  515. {
  516. struct v4l2_async_subdev *asd;
  517. int ret;
  518. asd = kzalloc(asd_struct_size, GFP_KERNEL);
  519. if (!asd)
  520. return ERR_PTR(-ENOMEM);
  521. asd->match_type = V4L2_ASYNC_MATCH_DEVNAME;
  522. asd->match.device_name = device_name;
  523. ret = v4l2_async_notifier_add_subdev(notifier, asd);
  524. if (ret) {
  525. kfree(asd);
  526. return ERR_PTR(ret);
  527. }
  528. return asd;
  529. }
  530. EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_devname_subdev);
  531. int v4l2_async_register_subdev(struct v4l2_subdev *sd)
  532. {
  533. struct v4l2_async_notifier *subdev_notifier;
  534. struct v4l2_async_notifier *notifier;
  535. int ret;
  536. /*
  537. * No reference taken. The reference is held by the device
  538. * (struct v4l2_subdev.dev), and async sub-device does not
  539. * exist independently of the device at any point of time.
  540. */
  541. if (!sd->fwnode && sd->dev)
  542. sd->fwnode = dev_fwnode(sd->dev);
  543. mutex_lock(&list_lock);
  544. INIT_LIST_HEAD(&sd->async_list);
  545. list_for_each_entry(notifier, &notifier_list, list) {
  546. struct v4l2_device *v4l2_dev =
  547. v4l2_async_notifier_find_v4l2_dev(notifier);
  548. struct v4l2_async_subdev *asd;
  549. if (!v4l2_dev)
  550. continue;
  551. asd = v4l2_async_find_match(notifier, sd);
  552. if (!asd)
  553. continue;
  554. ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
  555. if (ret)
  556. goto err_unbind;
  557. ret = v4l2_async_notifier_try_complete(notifier);
  558. if (ret)
  559. goto err_unbind;
  560. goto out_unlock;
  561. }
  562. /* None matched, wait for hot-plugging */
  563. list_add(&sd->async_list, &subdev_list);
  564. out_unlock:
  565. mutex_unlock(&list_lock);
  566. return 0;
  567. err_unbind:
  568. /*
  569. * Complete failed. Unbind the sub-devices bound through registering
  570. * this async sub-device.
  571. */
  572. subdev_notifier = v4l2_async_find_subdev_notifier(sd);
  573. if (subdev_notifier)
  574. v4l2_async_notifier_unbind_all_subdevs(subdev_notifier);
  575. if (sd->asd)
  576. v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
  577. v4l2_async_cleanup(sd);
  578. mutex_unlock(&list_lock);
  579. return ret;
  580. }
  581. EXPORT_SYMBOL(v4l2_async_register_subdev);
  582. void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
  583. {
  584. mutex_lock(&list_lock);
  585. __v4l2_async_notifier_unregister(sd->subdev_notifier);
  586. __v4l2_async_notifier_cleanup(sd->subdev_notifier);
  587. kfree(sd->subdev_notifier);
  588. sd->subdev_notifier = NULL;
  589. if (sd->asd) {
  590. struct v4l2_async_notifier *notifier = sd->notifier;
  591. list_add(&sd->asd->list, &notifier->waiting);
  592. v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
  593. }
  594. v4l2_async_cleanup(sd);
  595. mutex_unlock(&list_lock);
  596. }
  597. EXPORT_SYMBOL(v4l2_async_unregister_subdev);