v4l2-async.c 7.5 KB

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