v4l2-async.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 bool match_i2c(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
  26. {
  27. #if IS_ENABLED(CONFIG_I2C)
  28. struct i2c_client *client = i2c_verify_client(sd->dev);
  29. return client &&
  30. asd->match.i2c.adapter_id == client->adapter->nr &&
  31. asd->match.i2c.address == client->addr;
  32. #else
  33. return false;
  34. #endif
  35. }
  36. static bool match_devname(struct v4l2_subdev *sd,
  37. struct v4l2_async_subdev *asd)
  38. {
  39. return !strcmp(asd->match.device_name.name, dev_name(sd->dev));
  40. }
  41. static bool match_fwnode(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
  42. {
  43. return sd->fwnode == asd->match.fwnode.fwnode;
  44. }
  45. static bool match_custom(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
  46. {
  47. if (!asd->match.custom.match)
  48. /* Match always */
  49. return true;
  50. return asd->match.custom.match(sd->dev, asd);
  51. }
  52. static LIST_HEAD(subdev_list);
  53. static LIST_HEAD(notifier_list);
  54. static DEFINE_MUTEX(list_lock);
  55. static struct v4l2_async_subdev *v4l2_async_find_match(
  56. struct v4l2_async_notifier *notifier, struct v4l2_subdev *sd)
  57. {
  58. bool (*match)(struct v4l2_subdev *, struct v4l2_async_subdev *);
  59. struct v4l2_async_subdev *asd;
  60. list_for_each_entry(asd, &notifier->waiting, list) {
  61. /* bus_type has been verified valid before */
  62. switch (asd->match_type) {
  63. case V4L2_ASYNC_MATCH_CUSTOM:
  64. match = match_custom;
  65. break;
  66. case V4L2_ASYNC_MATCH_DEVNAME:
  67. match = match_devname;
  68. break;
  69. case V4L2_ASYNC_MATCH_I2C:
  70. match = match_i2c;
  71. break;
  72. case V4L2_ASYNC_MATCH_FWNODE:
  73. match = match_fwnode;
  74. break;
  75. default:
  76. /* Cannot happen, unless someone breaks us */
  77. WARN_ON(true);
  78. return NULL;
  79. }
  80. /* match cannot be NULL here */
  81. if (match(sd, asd))
  82. return asd;
  83. }
  84. return NULL;
  85. }
  86. static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier,
  87. struct v4l2_subdev *sd,
  88. struct v4l2_async_subdev *asd)
  89. {
  90. int ret;
  91. if (notifier->ops->bound) {
  92. ret = notifier->ops->bound(notifier, sd, asd);
  93. if (ret < 0)
  94. return ret;
  95. }
  96. ret = v4l2_device_register_subdev(notifier->v4l2_dev, sd);
  97. if (ret < 0) {
  98. if (notifier->ops->unbind)
  99. notifier->ops->unbind(notifier, sd, asd);
  100. return ret;
  101. }
  102. /* Remove from the waiting list */
  103. list_del(&asd->list);
  104. sd->asd = asd;
  105. sd->notifier = notifier;
  106. /* Move from the global subdevice list to notifier's done */
  107. list_move(&sd->async_list, &notifier->done);
  108. return 0;
  109. }
  110. static void v4l2_async_cleanup(struct v4l2_subdev *sd)
  111. {
  112. v4l2_device_unregister_subdev(sd);
  113. /* Subdevice driver will reprobe and put the subdev back onto the list */
  114. list_del_init(&sd->async_list);
  115. sd->asd = NULL;
  116. }
  117. static void v4l2_async_notifier_unbind_all_subdevs(
  118. struct v4l2_async_notifier *notifier)
  119. {
  120. struct v4l2_subdev *sd, *tmp;
  121. list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
  122. if (notifier->ops->unbind)
  123. notifier->ops->unbind(notifier, sd, sd->asd);
  124. v4l2_async_cleanup(sd);
  125. list_move(&sd->async_list, &subdev_list);
  126. }
  127. }
  128. int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
  129. struct v4l2_async_notifier *notifier)
  130. {
  131. struct v4l2_subdev *sd, *tmp;
  132. struct v4l2_async_subdev *asd;
  133. int ret;
  134. int i;
  135. if (!v4l2_dev || !notifier->num_subdevs ||
  136. notifier->num_subdevs > V4L2_MAX_SUBDEVS)
  137. return -EINVAL;
  138. notifier->v4l2_dev = v4l2_dev;
  139. INIT_LIST_HEAD(&notifier->waiting);
  140. INIT_LIST_HEAD(&notifier->done);
  141. for (i = 0; i < notifier->num_subdevs; i++) {
  142. asd = notifier->subdevs[i];
  143. switch (asd->match_type) {
  144. case V4L2_ASYNC_MATCH_CUSTOM:
  145. case V4L2_ASYNC_MATCH_DEVNAME:
  146. case V4L2_ASYNC_MATCH_I2C:
  147. case V4L2_ASYNC_MATCH_FWNODE:
  148. break;
  149. default:
  150. dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL,
  151. "Invalid match type %u on %p\n",
  152. asd->match_type, asd);
  153. return -EINVAL;
  154. }
  155. list_add_tail(&asd->list, &notifier->waiting);
  156. }
  157. mutex_lock(&list_lock);
  158. list_for_each_entry_safe(sd, tmp, &subdev_list, async_list) {
  159. int ret;
  160. asd = v4l2_async_find_match(notifier, sd);
  161. if (!asd)
  162. continue;
  163. ret = v4l2_async_match_notify(notifier, sd, asd);
  164. if (ret < 0) {
  165. mutex_unlock(&list_lock);
  166. return ret;
  167. }
  168. }
  169. if (list_empty(&notifier->waiting) && notifier->ops->complete) {
  170. ret = notifier->ops->complete(notifier);
  171. if (ret)
  172. goto err_complete;
  173. }
  174. /* Keep also completed notifiers on the list */
  175. list_add(&notifier->list, &notifier_list);
  176. mutex_unlock(&list_lock);
  177. return 0;
  178. err_complete:
  179. v4l2_async_notifier_unbind_all_subdevs(notifier);
  180. mutex_unlock(&list_lock);
  181. return ret;
  182. }
  183. EXPORT_SYMBOL(v4l2_async_notifier_register);
  184. void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
  185. {
  186. if (!notifier->v4l2_dev)
  187. return;
  188. mutex_lock(&list_lock);
  189. list_del(&notifier->list);
  190. v4l2_async_notifier_unbind_all_subdevs(notifier);
  191. mutex_unlock(&list_lock);
  192. notifier->v4l2_dev = NULL;
  193. }
  194. EXPORT_SYMBOL(v4l2_async_notifier_unregister);
  195. void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier)
  196. {
  197. unsigned int i;
  198. if (!notifier->max_subdevs)
  199. return;
  200. for (i = 0; i < notifier->num_subdevs; i++) {
  201. struct v4l2_async_subdev *asd = notifier->subdevs[i];
  202. switch (asd->match_type) {
  203. case V4L2_ASYNC_MATCH_FWNODE:
  204. fwnode_handle_put(asd->match.fwnode.fwnode);
  205. break;
  206. default:
  207. WARN_ON_ONCE(true);
  208. break;
  209. }
  210. kfree(asd);
  211. }
  212. notifier->max_subdevs = 0;
  213. notifier->num_subdevs = 0;
  214. kvfree(notifier->subdevs);
  215. notifier->subdevs = NULL;
  216. }
  217. EXPORT_SYMBOL_GPL(v4l2_async_notifier_cleanup);
  218. int v4l2_async_register_subdev(struct v4l2_subdev *sd)
  219. {
  220. struct v4l2_async_notifier *notifier;
  221. int ret;
  222. /*
  223. * No reference taken. The reference is held by the device
  224. * (struct v4l2_subdev.dev), and async sub-device does not
  225. * exist independently of the device at any point of time.
  226. */
  227. if (!sd->fwnode && sd->dev)
  228. sd->fwnode = dev_fwnode(sd->dev);
  229. mutex_lock(&list_lock);
  230. INIT_LIST_HEAD(&sd->async_list);
  231. list_for_each_entry(notifier, &notifier_list, list) {
  232. struct v4l2_async_subdev *asd = v4l2_async_find_match(notifier,
  233. sd);
  234. int ret;
  235. if (!asd)
  236. continue;
  237. ret = v4l2_async_match_notify(notifier, sd, asd);
  238. if (ret)
  239. goto err_unlock;
  240. if (!list_empty(&notifier->waiting) || !notifier->ops->complete)
  241. goto out_unlock;
  242. ret = notifier->ops->complete(notifier);
  243. if (ret)
  244. goto err_cleanup;
  245. goto out_unlock;
  246. }
  247. /* None matched, wait for hot-plugging */
  248. list_add(&sd->async_list, &subdev_list);
  249. out_unlock:
  250. mutex_unlock(&list_lock);
  251. return 0;
  252. err_cleanup:
  253. if (notifier->ops->unbind)
  254. notifier->ops->unbind(notifier, sd, sd->asd);
  255. v4l2_async_cleanup(sd);
  256. err_unlock:
  257. mutex_unlock(&list_lock);
  258. return ret;
  259. }
  260. EXPORT_SYMBOL(v4l2_async_register_subdev);
  261. void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
  262. {
  263. mutex_lock(&list_lock);
  264. if (sd->asd) {
  265. struct v4l2_async_notifier *notifier = sd->notifier;
  266. list_add(&sd->asd->list, &notifier->waiting);
  267. if (notifier->ops->unbind)
  268. notifier->ops->unbind(notifier, sd, sd->asd);
  269. }
  270. v4l2_async_cleanup(sd);
  271. mutex_unlock(&list_lock);
  272. }
  273. EXPORT_SYMBOL(v4l2_async_unregister_subdev);