v4l2-async.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. ret = v4l2_subdev_call(sd, core, registered_async);
  106. if (ret < 0 && ret != -ENOIOCTLCMD) {
  107. if (notifier->unbind)
  108. notifier->unbind(notifier, sd, asd);
  109. return ret;
  110. }
  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 (!notifier->num_subdevs || notifier->num_subdevs > V4L2_MAX_SUBDEVS)
  130. return -EINVAL;
  131. notifier->v4l2_dev = v4l2_dev;
  132. INIT_LIST_HEAD(&notifier->waiting);
  133. INIT_LIST_HEAD(&notifier->done);
  134. for (i = 0; i < notifier->num_subdevs; i++) {
  135. asd = notifier->subdevs[i];
  136. switch (asd->match_type) {
  137. case V4L2_ASYNC_MATCH_CUSTOM:
  138. case V4L2_ASYNC_MATCH_DEVNAME:
  139. case V4L2_ASYNC_MATCH_I2C:
  140. case V4L2_ASYNC_MATCH_OF:
  141. break;
  142. default:
  143. dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL,
  144. "Invalid match type %u on %p\n",
  145. asd->match_type, asd);
  146. return -EINVAL;
  147. }
  148. list_add_tail(&asd->list, &notifier->waiting);
  149. }
  150. mutex_lock(&list_lock);
  151. /* Keep also completed notifiers on the list */
  152. list_add(&notifier->list, &notifier_list);
  153. list_for_each_entry_safe(sd, tmp, &subdev_list, async_list) {
  154. int ret;
  155. asd = v4l2_async_belongs(notifier, sd);
  156. if (!asd)
  157. continue;
  158. ret = v4l2_async_test_notify(notifier, sd, asd);
  159. if (ret < 0) {
  160. mutex_unlock(&list_lock);
  161. return ret;
  162. }
  163. }
  164. mutex_unlock(&list_lock);
  165. return 0;
  166. }
  167. EXPORT_SYMBOL(v4l2_async_notifier_register);
  168. void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
  169. {
  170. struct v4l2_subdev *sd, *tmp;
  171. unsigned int notif_n_subdev = notifier->num_subdevs;
  172. unsigned int n_subdev = min(notif_n_subdev, V4L2_MAX_SUBDEVS);
  173. struct device **dev;
  174. int i = 0;
  175. if (!notifier->v4l2_dev)
  176. return;
  177. dev = kmalloc(n_subdev * sizeof(*dev), GFP_KERNEL);
  178. if (!dev) {
  179. dev_err(notifier->v4l2_dev->dev,
  180. "Failed to allocate device cache!\n");
  181. }
  182. mutex_lock(&list_lock);
  183. list_del(&notifier->list);
  184. list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
  185. struct device *d;
  186. d = get_device(sd->dev);
  187. v4l2_async_cleanup(sd);
  188. /* If we handled USB devices, we'd have to lock the parent too */
  189. device_release_driver(d);
  190. if (notifier->unbind)
  191. notifier->unbind(notifier, sd, sd->asd);
  192. /*
  193. * Store device at the device cache, in order to call
  194. * put_device() on the final step
  195. */
  196. if (dev)
  197. dev[i++] = d;
  198. else
  199. put_device(d);
  200. }
  201. mutex_unlock(&list_lock);
  202. /*
  203. * Call device_attach() to reprobe devices
  204. *
  205. * NOTE: If dev allocation fails, i is 0, and the whole loop won't be
  206. * executed.
  207. */
  208. while (i--) {
  209. struct device *d = dev[i];
  210. if (d && device_attach(d) < 0) {
  211. const char *name = "(none)";
  212. int lock = device_trylock(d);
  213. if (lock && d->driver)
  214. name = d->driver->name;
  215. dev_err(d, "Failed to re-probe to %s\n", name);
  216. if (lock)
  217. device_unlock(d);
  218. }
  219. put_device(d);
  220. }
  221. kfree(dev);
  222. notifier->v4l2_dev = NULL;
  223. /*
  224. * Don't care about the waiting list, it is initialised and populated
  225. * upon notifier registration.
  226. */
  227. }
  228. EXPORT_SYMBOL(v4l2_async_notifier_unregister);
  229. int v4l2_async_register_subdev(struct v4l2_subdev *sd)
  230. {
  231. struct v4l2_async_notifier *notifier;
  232. /*
  233. * No reference taken. The reference is held by the device
  234. * (struct v4l2_subdev.dev), and async sub-device does not
  235. * exist independently of the device at any point of time.
  236. */
  237. if (!sd->of_node && sd->dev)
  238. sd->of_node = sd->dev->of_node;
  239. mutex_lock(&list_lock);
  240. INIT_LIST_HEAD(&sd->async_list);
  241. list_for_each_entry(notifier, &notifier_list, list) {
  242. struct v4l2_async_subdev *asd = v4l2_async_belongs(notifier, sd);
  243. if (asd) {
  244. int ret = v4l2_async_test_notify(notifier, sd, asd);
  245. mutex_unlock(&list_lock);
  246. return ret;
  247. }
  248. }
  249. /* None matched, wait for hot-plugging */
  250. list_add(&sd->async_list, &subdev_list);
  251. mutex_unlock(&list_lock);
  252. return 0;
  253. }
  254. EXPORT_SYMBOL(v4l2_async_register_subdev);
  255. void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
  256. {
  257. struct v4l2_async_notifier *notifier = sd->notifier;
  258. if (!sd->asd) {
  259. if (!list_empty(&sd->async_list))
  260. v4l2_async_cleanup(sd);
  261. return;
  262. }
  263. mutex_lock(&list_lock);
  264. list_add(&sd->asd->list, &notifier->waiting);
  265. v4l2_async_cleanup(sd);
  266. if (notifier->unbind)
  267. notifier->unbind(notifier, sd, sd->asd);
  268. mutex_unlock(&list_lock);
  269. }
  270. EXPORT_SYMBOL(v4l2_async_unregister_subdev);