v4l2-async.c 6.7 KB

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