v4l2-async.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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.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.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. static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier,
  109. struct v4l2_subdev *sd,
  110. struct v4l2_async_subdev *asd)
  111. {
  112. int ret;
  113. ret = v4l2_device_register_subdev(notifier->v4l2_dev, sd);
  114. if (ret < 0)
  115. return ret;
  116. ret = v4l2_async_notifier_call_bound(notifier, sd, asd);
  117. if (ret < 0) {
  118. v4l2_device_unregister_subdev(sd);
  119. return ret;
  120. }
  121. /* Remove from the waiting list */
  122. list_del(&asd->list);
  123. sd->asd = asd;
  124. sd->notifier = notifier;
  125. /* Move from the global subdevice list to notifier's done */
  126. list_move(&sd->async_list, &notifier->done);
  127. return 0;
  128. }
  129. static void v4l2_async_cleanup(struct v4l2_subdev *sd)
  130. {
  131. v4l2_device_unregister_subdev(sd);
  132. /* Subdevice driver will reprobe and put the subdev back onto the list */
  133. list_del_init(&sd->async_list);
  134. sd->asd = NULL;
  135. }
  136. static void v4l2_async_notifier_unbind_all_subdevs(
  137. struct v4l2_async_notifier *notifier)
  138. {
  139. struct v4l2_subdev *sd, *tmp;
  140. list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
  141. v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
  142. v4l2_async_cleanup(sd);
  143. list_move(&sd->async_list, &subdev_list);
  144. }
  145. }
  146. int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
  147. struct v4l2_async_notifier *notifier)
  148. {
  149. struct v4l2_subdev *sd, *tmp;
  150. struct v4l2_async_subdev *asd;
  151. int ret;
  152. int i;
  153. if (!v4l2_dev || !notifier->num_subdevs ||
  154. notifier->num_subdevs > V4L2_MAX_SUBDEVS)
  155. return -EINVAL;
  156. notifier->v4l2_dev = v4l2_dev;
  157. INIT_LIST_HEAD(&notifier->waiting);
  158. INIT_LIST_HEAD(&notifier->done);
  159. for (i = 0; i < notifier->num_subdevs; i++) {
  160. asd = notifier->subdevs[i];
  161. switch (asd->match_type) {
  162. case V4L2_ASYNC_MATCH_CUSTOM:
  163. case V4L2_ASYNC_MATCH_DEVNAME:
  164. case V4L2_ASYNC_MATCH_I2C:
  165. case V4L2_ASYNC_MATCH_FWNODE:
  166. break;
  167. default:
  168. dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL,
  169. "Invalid match type %u on %p\n",
  170. asd->match_type, asd);
  171. return -EINVAL;
  172. }
  173. list_add_tail(&asd->list, &notifier->waiting);
  174. }
  175. mutex_lock(&list_lock);
  176. list_for_each_entry_safe(sd, tmp, &subdev_list, async_list) {
  177. int ret;
  178. asd = v4l2_async_find_match(notifier, sd);
  179. if (!asd)
  180. continue;
  181. ret = v4l2_async_match_notify(notifier, sd, asd);
  182. if (ret < 0) {
  183. mutex_unlock(&list_lock);
  184. return ret;
  185. }
  186. }
  187. if (list_empty(&notifier->waiting)) {
  188. ret = v4l2_async_notifier_call_complete(notifier);
  189. if (ret)
  190. goto err_complete;
  191. }
  192. /* Keep also completed notifiers on the list */
  193. list_add(&notifier->list, &notifier_list);
  194. mutex_unlock(&list_lock);
  195. return 0;
  196. err_complete:
  197. v4l2_async_notifier_unbind_all_subdevs(notifier);
  198. mutex_unlock(&list_lock);
  199. return ret;
  200. }
  201. EXPORT_SYMBOL(v4l2_async_notifier_register);
  202. void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
  203. {
  204. if (!notifier->v4l2_dev)
  205. return;
  206. mutex_lock(&list_lock);
  207. list_del(&notifier->list);
  208. v4l2_async_notifier_unbind_all_subdevs(notifier);
  209. mutex_unlock(&list_lock);
  210. notifier->v4l2_dev = NULL;
  211. }
  212. EXPORT_SYMBOL(v4l2_async_notifier_unregister);
  213. void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier)
  214. {
  215. unsigned int i;
  216. if (!notifier->max_subdevs)
  217. return;
  218. for (i = 0; i < notifier->num_subdevs; i++) {
  219. struct v4l2_async_subdev *asd = notifier->subdevs[i];
  220. switch (asd->match_type) {
  221. case V4L2_ASYNC_MATCH_FWNODE:
  222. fwnode_handle_put(asd->match.fwnode.fwnode);
  223. break;
  224. default:
  225. WARN_ON_ONCE(true);
  226. break;
  227. }
  228. kfree(asd);
  229. }
  230. notifier->max_subdevs = 0;
  231. notifier->num_subdevs = 0;
  232. kvfree(notifier->subdevs);
  233. notifier->subdevs = NULL;
  234. }
  235. EXPORT_SYMBOL_GPL(v4l2_async_notifier_cleanup);
  236. int v4l2_async_register_subdev(struct v4l2_subdev *sd)
  237. {
  238. struct v4l2_async_notifier *notifier;
  239. int ret;
  240. /*
  241. * No reference taken. The reference is held by the device
  242. * (struct v4l2_subdev.dev), and async sub-device does not
  243. * exist independently of the device at any point of time.
  244. */
  245. if (!sd->fwnode && sd->dev)
  246. sd->fwnode = dev_fwnode(sd->dev);
  247. mutex_lock(&list_lock);
  248. INIT_LIST_HEAD(&sd->async_list);
  249. list_for_each_entry(notifier, &notifier_list, list) {
  250. struct v4l2_async_subdev *asd = v4l2_async_find_match(notifier,
  251. sd);
  252. int ret;
  253. if (!asd)
  254. continue;
  255. ret = v4l2_async_match_notify(notifier, sd, asd);
  256. if (ret)
  257. goto err_unlock;
  258. if (!list_empty(&notifier->waiting))
  259. goto out_unlock;
  260. ret = v4l2_async_notifier_call_complete(notifier);
  261. if (ret)
  262. goto err_cleanup;
  263. goto out_unlock;
  264. }
  265. /* None matched, wait for hot-plugging */
  266. list_add(&sd->async_list, &subdev_list);
  267. out_unlock:
  268. mutex_unlock(&list_lock);
  269. return 0;
  270. err_cleanup:
  271. v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
  272. v4l2_async_cleanup(sd);
  273. err_unlock:
  274. mutex_unlock(&list_lock);
  275. return ret;
  276. }
  277. EXPORT_SYMBOL(v4l2_async_register_subdev);
  278. void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
  279. {
  280. mutex_lock(&list_lock);
  281. if (sd->asd) {
  282. struct v4l2_async_notifier *notifier = sd->notifier;
  283. list_add(&sd->asd->list, &notifier->waiting);
  284. v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
  285. }
  286. v4l2_async_cleanup(sd);
  287. mutex_unlock(&list_lock);
  288. }
  289. EXPORT_SYMBOL(v4l2_async_unregister_subdev);