v4l2-async.c 6.4 KB

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