v4l2-async.c 7.4 KB

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