v4l2-async.c 6.8 KB

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