v4l2-async.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 *v4l2_async_find_match(
  78. struct v4l2_async_notifier *notifier, struct v4l2_subdev *sd)
  79. {
  80. bool (*match)(struct v4l2_subdev *, struct v4l2_async_subdev *);
  81. struct v4l2_async_subdev *asd;
  82. list_for_each_entry(asd, &notifier->waiting, list) {
  83. /* bus_type has been verified valid before */
  84. switch (asd->match_type) {
  85. case V4L2_ASYNC_MATCH_CUSTOM:
  86. match = match_custom;
  87. break;
  88. case V4L2_ASYNC_MATCH_DEVNAME:
  89. match = match_devname;
  90. break;
  91. case V4L2_ASYNC_MATCH_I2C:
  92. match = match_i2c;
  93. break;
  94. case V4L2_ASYNC_MATCH_FWNODE:
  95. match = match_fwnode;
  96. break;
  97. default:
  98. /* Cannot happen, unless someone breaks us */
  99. WARN_ON(true);
  100. return NULL;
  101. }
  102. /* match cannot be NULL here */
  103. if (match(sd, asd))
  104. return asd;
  105. }
  106. return NULL;
  107. }
  108. /* Find the sub-device notifier registered by a sub-device driver. */
  109. static struct v4l2_async_notifier *v4l2_async_find_subdev_notifier(
  110. struct v4l2_subdev *sd)
  111. {
  112. struct v4l2_async_notifier *n;
  113. list_for_each_entry(n, &notifier_list, list)
  114. if (n->sd == sd)
  115. return n;
  116. return NULL;
  117. }
  118. /* Get v4l2_device related to the notifier if one can be found. */
  119. static struct v4l2_device *v4l2_async_notifier_find_v4l2_dev(
  120. struct v4l2_async_notifier *notifier)
  121. {
  122. while (notifier->parent)
  123. notifier = notifier->parent;
  124. return notifier->v4l2_dev;
  125. }
  126. /*
  127. * Return true if all child sub-device notifiers are complete, false otherwise.
  128. */
  129. static bool v4l2_async_notifier_can_complete(
  130. struct v4l2_async_notifier *notifier)
  131. {
  132. struct v4l2_subdev *sd;
  133. if (!list_empty(&notifier->waiting))
  134. return false;
  135. list_for_each_entry(sd, &notifier->done, async_list) {
  136. struct v4l2_async_notifier *subdev_notifier =
  137. v4l2_async_find_subdev_notifier(sd);
  138. if (subdev_notifier &&
  139. !v4l2_async_notifier_can_complete(subdev_notifier))
  140. return false;
  141. }
  142. return true;
  143. }
  144. /*
  145. * Complete the master notifier if possible. This is done when all async
  146. * sub-devices have been bound; v4l2_device is also available then.
  147. */
  148. static int v4l2_async_notifier_try_complete(
  149. struct v4l2_async_notifier *notifier)
  150. {
  151. /* Quick check whether there are still more sub-devices here. */
  152. if (!list_empty(&notifier->waiting))
  153. return 0;
  154. /* Check the entire notifier tree; find the root notifier first. */
  155. while (notifier->parent)
  156. notifier = notifier->parent;
  157. /* This is root if it has v4l2_dev. */
  158. if (!notifier->v4l2_dev)
  159. return 0;
  160. /* Is everything ready? */
  161. if (!v4l2_async_notifier_can_complete(notifier))
  162. return 0;
  163. return v4l2_async_notifier_call_complete(notifier);
  164. }
  165. static int v4l2_async_notifier_try_all_subdevs(
  166. struct v4l2_async_notifier *notifier);
  167. static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier,
  168. struct v4l2_device *v4l2_dev,
  169. struct v4l2_subdev *sd,
  170. struct v4l2_async_subdev *asd)
  171. {
  172. struct v4l2_async_notifier *subdev_notifier;
  173. int ret;
  174. ret = v4l2_device_register_subdev(v4l2_dev, sd);
  175. if (ret < 0)
  176. return ret;
  177. ret = v4l2_async_notifier_call_bound(notifier, sd, asd);
  178. if (ret < 0) {
  179. v4l2_device_unregister_subdev(sd);
  180. return ret;
  181. }
  182. /* Remove from the waiting list */
  183. list_del(&asd->list);
  184. sd->asd = asd;
  185. sd->notifier = notifier;
  186. /* Move from the global subdevice list to notifier's done */
  187. list_move(&sd->async_list, &notifier->done);
  188. /*
  189. * See if the sub-device has a notifier. If not, return here.
  190. */
  191. subdev_notifier = v4l2_async_find_subdev_notifier(sd);
  192. if (!subdev_notifier || subdev_notifier->parent)
  193. return 0;
  194. /*
  195. * Proceed with checking for the sub-device notifier's async
  196. * sub-devices, and return the result. The error will be handled by the
  197. * caller.
  198. */
  199. subdev_notifier->parent = notifier;
  200. return v4l2_async_notifier_try_all_subdevs(subdev_notifier);
  201. }
  202. /* Test all async sub-devices in a notifier for a match. */
  203. static int v4l2_async_notifier_try_all_subdevs(
  204. struct v4l2_async_notifier *notifier)
  205. {
  206. struct v4l2_device *v4l2_dev =
  207. v4l2_async_notifier_find_v4l2_dev(notifier);
  208. struct v4l2_subdev *sd;
  209. if (!v4l2_dev)
  210. return 0;
  211. again:
  212. list_for_each_entry(sd, &subdev_list, async_list) {
  213. struct v4l2_async_subdev *asd;
  214. int ret;
  215. asd = v4l2_async_find_match(notifier, sd);
  216. if (!asd)
  217. continue;
  218. ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
  219. if (ret < 0)
  220. return ret;
  221. /*
  222. * v4l2_async_match_notify() may lead to registering a
  223. * new notifier and thus changing the async subdevs
  224. * list. In order to proceed safely from here, restart
  225. * parsing the list from the beginning.
  226. */
  227. goto again;
  228. }
  229. return 0;
  230. }
  231. static void v4l2_async_cleanup(struct v4l2_subdev *sd)
  232. {
  233. v4l2_device_unregister_subdev(sd);
  234. /* Subdevice driver will reprobe and put the subdev back onto the list */
  235. list_del_init(&sd->async_list);
  236. sd->asd = NULL;
  237. }
  238. /* Unbind all sub-devices in the notifier tree. */
  239. static void v4l2_async_notifier_unbind_all_subdevs(
  240. struct v4l2_async_notifier *notifier)
  241. {
  242. struct v4l2_subdev *sd, *tmp;
  243. list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
  244. struct v4l2_async_notifier *subdev_notifier =
  245. v4l2_async_find_subdev_notifier(sd);
  246. if (subdev_notifier)
  247. v4l2_async_notifier_unbind_all_subdevs(subdev_notifier);
  248. v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
  249. v4l2_async_cleanup(sd);
  250. list_move(&sd->async_list, &subdev_list);
  251. }
  252. notifier->parent = NULL;
  253. }
  254. /* See if an fwnode can be found in a notifier's lists. */
  255. static bool __v4l2_async_notifier_fwnode_has_async_subdev(
  256. struct v4l2_async_notifier *notifier, struct fwnode_handle *fwnode)
  257. {
  258. struct v4l2_async_subdev *asd;
  259. struct v4l2_subdev *sd;
  260. list_for_each_entry(asd, &notifier->waiting, list) {
  261. if (asd->match_type != V4L2_ASYNC_MATCH_FWNODE)
  262. continue;
  263. if (asd->match.fwnode == fwnode)
  264. return true;
  265. }
  266. list_for_each_entry(sd, &notifier->done, async_list) {
  267. if (WARN_ON(!sd->asd))
  268. continue;
  269. if (sd->asd->match_type != V4L2_ASYNC_MATCH_FWNODE)
  270. continue;
  271. if (sd->asd->match.fwnode == fwnode)
  272. return true;
  273. }
  274. return false;
  275. }
  276. /*
  277. * Find out whether an async sub-device was set up for an fwnode already or
  278. * whether it exists in a given notifier before @this_index.
  279. */
  280. static bool v4l2_async_notifier_fwnode_has_async_subdev(
  281. struct v4l2_async_notifier *notifier, struct fwnode_handle *fwnode,
  282. unsigned int this_index)
  283. {
  284. unsigned int j;
  285. lockdep_assert_held(&list_lock);
  286. /* Check that an fwnode is not being added more than once. */
  287. for (j = 0; j < this_index; j++) {
  288. struct v4l2_async_subdev *asd = notifier->subdevs[this_index];
  289. struct v4l2_async_subdev *other_asd = notifier->subdevs[j];
  290. if (other_asd->match_type == V4L2_ASYNC_MATCH_FWNODE &&
  291. asd->match.fwnode ==
  292. other_asd->match.fwnode)
  293. return true;
  294. }
  295. /* Check than an fwnode did not exist in other notifiers. */
  296. list_for_each_entry(notifier, &notifier_list, list)
  297. if (__v4l2_async_notifier_fwnode_has_async_subdev(
  298. notifier, fwnode))
  299. return true;
  300. return false;
  301. }
  302. static int __v4l2_async_notifier_register(struct v4l2_async_notifier *notifier)
  303. {
  304. struct device *dev =
  305. notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL;
  306. struct v4l2_async_subdev *asd;
  307. int ret;
  308. int i;
  309. if (notifier->num_subdevs > V4L2_MAX_SUBDEVS)
  310. return -EINVAL;
  311. INIT_LIST_HEAD(&notifier->waiting);
  312. INIT_LIST_HEAD(&notifier->done);
  313. mutex_lock(&list_lock);
  314. for (i = 0; i < notifier->num_subdevs; i++) {
  315. asd = notifier->subdevs[i];
  316. switch (asd->match_type) {
  317. case V4L2_ASYNC_MATCH_CUSTOM:
  318. case V4L2_ASYNC_MATCH_DEVNAME:
  319. case V4L2_ASYNC_MATCH_I2C:
  320. break;
  321. case V4L2_ASYNC_MATCH_FWNODE:
  322. if (v4l2_async_notifier_fwnode_has_async_subdev(
  323. notifier, asd->match.fwnode, i)) {
  324. dev_err(dev,
  325. "fwnode has already been registered or in notifier's subdev list\n");
  326. ret = -EEXIST;
  327. goto err_unlock;
  328. }
  329. break;
  330. default:
  331. dev_err(dev, "Invalid match type %u on %p\n",
  332. asd->match_type, asd);
  333. ret = -EINVAL;
  334. goto err_unlock;
  335. }
  336. list_add_tail(&asd->list, &notifier->waiting);
  337. }
  338. ret = v4l2_async_notifier_try_all_subdevs(notifier);
  339. if (ret < 0)
  340. goto err_unbind;
  341. ret = v4l2_async_notifier_try_complete(notifier);
  342. if (ret < 0)
  343. goto err_unbind;
  344. /* Keep also completed notifiers on the list */
  345. list_add(&notifier->list, &notifier_list);
  346. mutex_unlock(&list_lock);
  347. return 0;
  348. err_unbind:
  349. /*
  350. * On failure, unbind all sub-devices registered through this notifier.
  351. */
  352. v4l2_async_notifier_unbind_all_subdevs(notifier);
  353. err_unlock:
  354. mutex_unlock(&list_lock);
  355. return ret;
  356. }
  357. int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
  358. struct v4l2_async_notifier *notifier)
  359. {
  360. int ret;
  361. if (WARN_ON(!v4l2_dev || notifier->sd))
  362. return -EINVAL;
  363. notifier->v4l2_dev = v4l2_dev;
  364. ret = __v4l2_async_notifier_register(notifier);
  365. if (ret)
  366. notifier->v4l2_dev = NULL;
  367. return ret;
  368. }
  369. EXPORT_SYMBOL(v4l2_async_notifier_register);
  370. int v4l2_async_subdev_notifier_register(struct v4l2_subdev *sd,
  371. struct v4l2_async_notifier *notifier)
  372. {
  373. int ret;
  374. if (WARN_ON(!sd || notifier->v4l2_dev))
  375. return -EINVAL;
  376. notifier->sd = sd;
  377. ret = __v4l2_async_notifier_register(notifier);
  378. if (ret)
  379. notifier->sd = NULL;
  380. return ret;
  381. }
  382. EXPORT_SYMBOL(v4l2_async_subdev_notifier_register);
  383. static void __v4l2_async_notifier_unregister(
  384. struct v4l2_async_notifier *notifier)
  385. {
  386. if (!notifier || (!notifier->v4l2_dev && !notifier->sd))
  387. return;
  388. v4l2_async_notifier_unbind_all_subdevs(notifier);
  389. notifier->sd = NULL;
  390. notifier->v4l2_dev = NULL;
  391. list_del(&notifier->list);
  392. }
  393. void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
  394. {
  395. mutex_lock(&list_lock);
  396. __v4l2_async_notifier_unregister(notifier);
  397. mutex_unlock(&list_lock);
  398. }
  399. EXPORT_SYMBOL(v4l2_async_notifier_unregister);
  400. void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier)
  401. {
  402. unsigned int i;
  403. if (!notifier || !notifier->max_subdevs)
  404. return;
  405. for (i = 0; i < notifier->num_subdevs; i++) {
  406. struct v4l2_async_subdev *asd = notifier->subdevs[i];
  407. switch (asd->match_type) {
  408. case V4L2_ASYNC_MATCH_FWNODE:
  409. fwnode_handle_put(asd->match.fwnode);
  410. break;
  411. default:
  412. WARN_ON_ONCE(true);
  413. break;
  414. }
  415. kfree(asd);
  416. }
  417. notifier->max_subdevs = 0;
  418. notifier->num_subdevs = 0;
  419. kvfree(notifier->subdevs);
  420. notifier->subdevs = NULL;
  421. }
  422. EXPORT_SYMBOL_GPL(v4l2_async_notifier_cleanup);
  423. int v4l2_async_register_subdev(struct v4l2_subdev *sd)
  424. {
  425. struct v4l2_async_notifier *subdev_notifier;
  426. struct v4l2_async_notifier *notifier;
  427. int ret;
  428. /*
  429. * No reference taken. The reference is held by the device
  430. * (struct v4l2_subdev.dev), and async sub-device does not
  431. * exist independently of the device at any point of time.
  432. */
  433. if (!sd->fwnode && sd->dev)
  434. sd->fwnode = dev_fwnode(sd->dev);
  435. mutex_lock(&list_lock);
  436. INIT_LIST_HEAD(&sd->async_list);
  437. list_for_each_entry(notifier, &notifier_list, list) {
  438. struct v4l2_device *v4l2_dev =
  439. v4l2_async_notifier_find_v4l2_dev(notifier);
  440. struct v4l2_async_subdev *asd;
  441. if (!v4l2_dev)
  442. continue;
  443. asd = v4l2_async_find_match(notifier, sd);
  444. if (!asd)
  445. continue;
  446. ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
  447. if (ret)
  448. goto err_unbind;
  449. ret = v4l2_async_notifier_try_complete(notifier);
  450. if (ret)
  451. goto err_unbind;
  452. goto out_unlock;
  453. }
  454. /* None matched, wait for hot-plugging */
  455. list_add(&sd->async_list, &subdev_list);
  456. out_unlock:
  457. mutex_unlock(&list_lock);
  458. return 0;
  459. err_unbind:
  460. /*
  461. * Complete failed. Unbind the sub-devices bound through registering
  462. * this async sub-device.
  463. */
  464. subdev_notifier = v4l2_async_find_subdev_notifier(sd);
  465. if (subdev_notifier)
  466. v4l2_async_notifier_unbind_all_subdevs(subdev_notifier);
  467. if (sd->asd)
  468. v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
  469. v4l2_async_cleanup(sd);
  470. mutex_unlock(&list_lock);
  471. return ret;
  472. }
  473. EXPORT_SYMBOL(v4l2_async_register_subdev);
  474. void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
  475. {
  476. mutex_lock(&list_lock);
  477. __v4l2_async_notifier_unregister(sd->subdev_notifier);
  478. v4l2_async_notifier_cleanup(sd->subdev_notifier);
  479. kfree(sd->subdev_notifier);
  480. sd->subdev_notifier = NULL;
  481. if (sd->asd) {
  482. struct v4l2_async_notifier *notifier = sd->notifier;
  483. list_add(&sd->asd->list, &notifier->waiting);
  484. v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
  485. }
  486. v4l2_async_cleanup(sd);
  487. mutex_unlock(&list_lock);
  488. }
  489. EXPORT_SYMBOL(v4l2_async_unregister_subdev);