v4l2-async.c 6.6 KB

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