v4l2-async.c 7.9 KB

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