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