v4l2-async.c 7.7 KB

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