v4l2-async.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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;
  163. int i = 0;
  164. if (!notifier->v4l2_dev)
  165. return;
  166. dev = kmalloc(n_subdev * sizeof(*dev), GFP_KERNEL);
  167. if (!dev) {
  168. dev_err(notifier->v4l2_dev->dev,
  169. "Failed to allocate device cache!\n");
  170. }
  171. mutex_lock(&list_lock);
  172. list_del(&notifier->list);
  173. list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
  174. struct device *d;
  175. d = get_device(sd->dev);
  176. v4l2_async_cleanup(sd);
  177. /* If we handled USB devices, we'd have to lock the parent too */
  178. device_release_driver(d);
  179. if (notifier->unbind)
  180. notifier->unbind(notifier, sd, sd->asd);
  181. /*
  182. * Store device at the device cache, in order to call
  183. * put_device() on the final step
  184. */
  185. if (dev)
  186. dev[i++] = d;
  187. else
  188. put_device(d);
  189. }
  190. mutex_unlock(&list_lock);
  191. /*
  192. * Call device_attach() to reprobe devices
  193. *
  194. * NOTE: If dev allocation fails, i is 0, and the whole loop won't be
  195. * executed.
  196. */
  197. while (i--) {
  198. struct device *d = dev[i];
  199. if (d && device_attach(d) < 0) {
  200. const char *name = "(none)";
  201. int lock = device_trylock(d);
  202. if (lock && d->driver)
  203. name = d->driver->name;
  204. dev_err(d, "Failed to re-probe to %s\n", name);
  205. if (lock)
  206. device_unlock(d);
  207. }
  208. put_device(d);
  209. }
  210. kfree(dev);
  211. notifier->v4l2_dev = NULL;
  212. /*
  213. * Don't care about the waiting list, it is initialised and populated
  214. * upon notifier registration.
  215. */
  216. }
  217. EXPORT_SYMBOL(v4l2_async_notifier_unregister);
  218. int v4l2_async_register_subdev(struct v4l2_subdev *sd)
  219. {
  220. struct v4l2_async_notifier *notifier;
  221. mutex_lock(&list_lock);
  222. INIT_LIST_HEAD(&sd->async_list);
  223. list_for_each_entry(notifier, &notifier_list, list) {
  224. struct v4l2_async_subdev *asd = v4l2_async_belongs(notifier, sd);
  225. if (asd) {
  226. int ret = v4l2_async_test_notify(notifier, sd, asd);
  227. mutex_unlock(&list_lock);
  228. return ret;
  229. }
  230. }
  231. /* None matched, wait for hot-plugging */
  232. list_add(&sd->async_list, &subdev_list);
  233. mutex_unlock(&list_lock);
  234. return 0;
  235. }
  236. EXPORT_SYMBOL(v4l2_async_register_subdev);
  237. void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
  238. {
  239. struct v4l2_async_notifier *notifier = sd->notifier;
  240. if (!sd->asd) {
  241. if (!list_empty(&sd->async_list))
  242. v4l2_async_cleanup(sd);
  243. return;
  244. }
  245. mutex_lock(&list_lock);
  246. list_add(&sd->asd->list, &notifier->waiting);
  247. v4l2_async_cleanup(sd);
  248. if (notifier->unbind)
  249. notifier->unbind(notifier, sd, sd->asd);
  250. mutex_unlock(&list_lock);
  251. }
  252. EXPORT_SYMBOL(v4l2_async_unregister_subdev);