v4l2-async.c 7.6 KB

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