v4l2-async.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 v4l2_subdev *sd, struct v4l2_async_subdev *asd)
  23. {
  24. #if IS_ENABLED(CONFIG_I2C)
  25. struct i2c_client *client = i2c_verify_client(sd->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 v4l2_subdev *sd,
  34. struct v4l2_async_subdev *asd)
  35. {
  36. return !strcmp(asd->match.device_name.name, dev_name(sd->dev));
  37. }
  38. static bool match_of(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
  39. {
  40. return !of_node_cmp(of_node_full_name(sd->of_node),
  41. of_node_full_name(asd->match.of.node));
  42. }
  43. static bool match_custom(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
  44. {
  45. if (!asd->match.custom.match)
  46. /* Match always */
  47. return true;
  48. return asd->match.custom.match(sd->dev, asd);
  49. }
  50. static LIST_HEAD(subdev_list);
  51. static LIST_HEAD(notifier_list);
  52. static DEFINE_MUTEX(list_lock);
  53. static struct v4l2_async_subdev *v4l2_async_belongs(struct v4l2_async_notifier *notifier,
  54. struct v4l2_subdev *sd)
  55. {
  56. bool (*match)(struct v4l2_subdev *, struct v4l2_async_subdev *);
  57. struct v4l2_async_subdev *asd;
  58. list_for_each_entry(asd, &notifier->waiting, list) {
  59. /* bus_type has been verified valid before */
  60. switch (asd->match_type) {
  61. case V4L2_ASYNC_MATCH_CUSTOM:
  62. match = match_custom;
  63. break;
  64. case V4L2_ASYNC_MATCH_DEVNAME:
  65. match = match_devname;
  66. break;
  67. case V4L2_ASYNC_MATCH_I2C:
  68. match = match_i2c;
  69. break;
  70. case V4L2_ASYNC_MATCH_OF:
  71. match = match_of;
  72. break;
  73. default:
  74. /* Cannot happen, unless someone breaks us */
  75. WARN_ON(true);
  76. return NULL;
  77. }
  78. /* match cannot be NULL here */
  79. if (match(sd, asd))
  80. return asd;
  81. }
  82. return NULL;
  83. }
  84. static int v4l2_async_test_notify(struct v4l2_async_notifier *notifier,
  85. struct v4l2_subdev *sd,
  86. struct v4l2_async_subdev *asd)
  87. {
  88. int ret;
  89. if (notifier->bound) {
  90. ret = notifier->bound(notifier, sd, asd);
  91. if (ret < 0)
  92. return ret;
  93. }
  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. /* Remove from the waiting list */
  101. list_del(&asd->list);
  102. sd->asd = asd;
  103. sd->notifier = notifier;
  104. /* Move from the global subdevice list to notifier's done */
  105. list_move(&sd->async_list, &notifier->done);
  106. if (list_empty(&notifier->waiting) && notifier->complete)
  107. return notifier->complete(notifier);
  108. return 0;
  109. }
  110. static void v4l2_async_cleanup(struct v4l2_subdev *sd)
  111. {
  112. v4l2_device_unregister_subdev(sd);
  113. /* Subdevice driver will reprobe and put the subdev back onto the list */
  114. list_del_init(&sd->async_list);
  115. sd->asd = NULL;
  116. sd->dev = NULL;
  117. }
  118. int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
  119. struct v4l2_async_notifier *notifier)
  120. {
  121. struct v4l2_subdev *sd, *tmp;
  122. struct v4l2_async_subdev *asd;
  123. int i;
  124. if (!notifier->num_subdevs || notifier->num_subdevs > V4L2_MAX_SUBDEVS)
  125. return -EINVAL;
  126. notifier->v4l2_dev = v4l2_dev;
  127. INIT_LIST_HEAD(&notifier->waiting);
  128. INIT_LIST_HEAD(&notifier->done);
  129. for (i = 0; i < notifier->num_subdevs; i++) {
  130. asd = notifier->subdevs[i];
  131. switch (asd->match_type) {
  132. case V4L2_ASYNC_MATCH_CUSTOM:
  133. case V4L2_ASYNC_MATCH_DEVNAME:
  134. case V4L2_ASYNC_MATCH_I2C:
  135. case V4L2_ASYNC_MATCH_OF:
  136. break;
  137. default:
  138. dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL,
  139. "Invalid match type %u on %p\n",
  140. asd->match_type, asd);
  141. return -EINVAL;
  142. }
  143. list_add_tail(&asd->list, &notifier->waiting);
  144. }
  145. mutex_lock(&list_lock);
  146. list_for_each_entry_safe(sd, tmp, &subdev_list, async_list) {
  147. int ret;
  148. asd = v4l2_async_belongs(notifier, sd);
  149. if (!asd)
  150. continue;
  151. ret = v4l2_async_test_notify(notifier, sd, asd);
  152. if (ret < 0) {
  153. mutex_unlock(&list_lock);
  154. return ret;
  155. }
  156. }
  157. /* Keep also completed notifiers on the list */
  158. list_add(&notifier->list, &notifier_list);
  159. mutex_unlock(&list_lock);
  160. return 0;
  161. }
  162. EXPORT_SYMBOL(v4l2_async_notifier_register);
  163. void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
  164. {
  165. struct v4l2_subdev *sd, *tmp;
  166. unsigned int notif_n_subdev = notifier->num_subdevs;
  167. unsigned int n_subdev = min(notif_n_subdev, V4L2_MAX_SUBDEVS);
  168. struct device **dev;
  169. int i = 0;
  170. if (!notifier->v4l2_dev)
  171. return;
  172. dev = kmalloc_array(n_subdev, sizeof(*dev), GFP_KERNEL);
  173. if (!dev) {
  174. dev_err(notifier->v4l2_dev->dev,
  175. "Failed to allocate device cache!\n");
  176. }
  177. mutex_lock(&list_lock);
  178. list_del(&notifier->list);
  179. list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
  180. struct device *d;
  181. d = get_device(sd->dev);
  182. v4l2_async_cleanup(sd);
  183. /* If we handled USB devices, we'd have to lock the parent too */
  184. device_release_driver(d);
  185. if (notifier->unbind)
  186. notifier->unbind(notifier, sd, sd->asd);
  187. /*
  188. * Store device at the device cache, in order to call
  189. * put_device() on the final step
  190. */
  191. if (dev)
  192. dev[i++] = d;
  193. else
  194. put_device(d);
  195. }
  196. mutex_unlock(&list_lock);
  197. /*
  198. * Call device_attach() to reprobe devices
  199. *
  200. * NOTE: If dev allocation fails, i is 0, and the whole loop won't be
  201. * executed.
  202. */
  203. while (i--) {
  204. struct device *d = dev[i];
  205. if (d && device_attach(d) < 0) {
  206. const char *name = "(none)";
  207. int lock = device_trylock(d);
  208. if (lock && d->driver)
  209. name = d->driver->name;
  210. dev_err(d, "Failed to re-probe to %s\n", name);
  211. if (lock)
  212. device_unlock(d);
  213. }
  214. put_device(d);
  215. }
  216. kfree(dev);
  217. notifier->v4l2_dev = NULL;
  218. /*
  219. * Don't care about the waiting list, it is initialised and populated
  220. * upon notifier registration.
  221. */
  222. }
  223. EXPORT_SYMBOL(v4l2_async_notifier_unregister);
  224. int v4l2_async_register_subdev(struct v4l2_subdev *sd)
  225. {
  226. struct v4l2_async_notifier *notifier;
  227. /*
  228. * No reference taken. The reference is held by the device
  229. * (struct v4l2_subdev.dev), and async sub-device does not
  230. * exist independently of the device at any point of time.
  231. */
  232. if (!sd->of_node && sd->dev)
  233. sd->of_node = sd->dev->of_node;
  234. mutex_lock(&list_lock);
  235. INIT_LIST_HEAD(&sd->async_list);
  236. list_for_each_entry(notifier, &notifier_list, list) {
  237. struct v4l2_async_subdev *asd = v4l2_async_belongs(notifier, sd);
  238. if (asd) {
  239. int ret = v4l2_async_test_notify(notifier, sd, asd);
  240. mutex_unlock(&list_lock);
  241. return ret;
  242. }
  243. }
  244. /* None matched, wait for hot-plugging */
  245. list_add(&sd->async_list, &subdev_list);
  246. mutex_unlock(&list_lock);
  247. return 0;
  248. }
  249. EXPORT_SYMBOL(v4l2_async_register_subdev);
  250. void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
  251. {
  252. struct v4l2_async_notifier *notifier = sd->notifier;
  253. if (!sd->asd) {
  254. if (!list_empty(&sd->async_list))
  255. v4l2_async_cleanup(sd);
  256. return;
  257. }
  258. mutex_lock(&list_lock);
  259. list_add(&sd->asd->list, &notifier->waiting);
  260. v4l2_async_cleanup(sd);
  261. if (notifier->unbind)
  262. notifier->unbind(notifier, sd, sd->asd);
  263. mutex_unlock(&list_lock);
  264. }
  265. EXPORT_SYMBOL(v4l2_async_unregister_subdev);