v4l2-device.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. V4L2 device support.
  3. Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <linux/types.h>
  17. #include <linux/ioctl.h>
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/slab.h>
  21. #if defined(CONFIG_SPI)
  22. #include <linux/spi/spi.h>
  23. #endif
  24. #include <linux/videodev2.h>
  25. #include <media/v4l2-device.h>
  26. #include <media/v4l2-ctrls.h>
  27. int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
  28. {
  29. if (v4l2_dev == NULL)
  30. return -EINVAL;
  31. INIT_LIST_HEAD(&v4l2_dev->subdevs);
  32. spin_lock_init(&v4l2_dev->lock);
  33. mutex_init(&v4l2_dev->ioctl_lock);
  34. v4l2_prio_init(&v4l2_dev->prio);
  35. kref_init(&v4l2_dev->ref);
  36. get_device(dev);
  37. v4l2_dev->dev = dev;
  38. if (dev == NULL) {
  39. /* If dev == NULL, then name must be filled in by the caller */
  40. if (WARN_ON(!v4l2_dev->name[0]))
  41. return -EINVAL;
  42. return 0;
  43. }
  44. /* Set name to driver name + device name if it is empty. */
  45. if (!v4l2_dev->name[0])
  46. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
  47. dev->driver->name, dev_name(dev));
  48. if (!dev_get_drvdata(dev))
  49. dev_set_drvdata(dev, v4l2_dev);
  50. return 0;
  51. }
  52. EXPORT_SYMBOL_GPL(v4l2_device_register);
  53. static void v4l2_device_release(struct kref *ref)
  54. {
  55. struct v4l2_device *v4l2_dev =
  56. container_of(ref, struct v4l2_device, ref);
  57. if (v4l2_dev->release)
  58. v4l2_dev->release(v4l2_dev);
  59. }
  60. int v4l2_device_put(struct v4l2_device *v4l2_dev)
  61. {
  62. return kref_put(&v4l2_dev->ref, v4l2_device_release);
  63. }
  64. EXPORT_SYMBOL_GPL(v4l2_device_put);
  65. int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
  66. atomic_t *instance)
  67. {
  68. int num = atomic_inc_return(instance) - 1;
  69. int len = strlen(basename);
  70. if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
  71. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  72. "%s-%d", basename, num);
  73. else
  74. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  75. "%s%d", basename, num);
  76. return num;
  77. }
  78. EXPORT_SYMBOL_GPL(v4l2_device_set_name);
  79. void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
  80. {
  81. if (v4l2_dev->dev == NULL)
  82. return;
  83. if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
  84. dev_set_drvdata(v4l2_dev->dev, NULL);
  85. put_device(v4l2_dev->dev);
  86. v4l2_dev->dev = NULL;
  87. }
  88. EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
  89. void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
  90. {
  91. struct v4l2_subdev *sd, *next;
  92. /* Just return if v4l2_dev is NULL or if it was already
  93. * unregistered before. */
  94. if (v4l2_dev == NULL || !v4l2_dev->name[0])
  95. return;
  96. v4l2_device_disconnect(v4l2_dev);
  97. /* Unregister subdevs */
  98. list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
  99. v4l2_device_unregister_subdev(sd);
  100. #if IS_ENABLED(CONFIG_I2C)
  101. if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
  102. struct i2c_client *client = v4l2_get_subdevdata(sd);
  103. /* We need to unregister the i2c client explicitly.
  104. We cannot rely on i2c_del_adapter to always
  105. unregister clients for us, since if the i2c bus
  106. is a platform bus, then it is never deleted. */
  107. if (client)
  108. i2c_unregister_device(client);
  109. continue;
  110. }
  111. #endif
  112. #if defined(CONFIG_SPI)
  113. if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
  114. struct spi_device *spi = v4l2_get_subdevdata(sd);
  115. if (spi)
  116. spi_unregister_device(spi);
  117. continue;
  118. }
  119. #endif
  120. }
  121. /* Mark as unregistered, thus preventing duplicate unregistrations */
  122. v4l2_dev->name[0] = '\0';
  123. }
  124. EXPORT_SYMBOL_GPL(v4l2_device_unregister);
  125. int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
  126. struct v4l2_subdev *sd)
  127. {
  128. #if defined(CONFIG_MEDIA_CONTROLLER)
  129. struct media_entity *entity = &sd->entity;
  130. #endif
  131. int err;
  132. /* Check for valid input */
  133. if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
  134. return -EINVAL;
  135. /* Warn if we apparently re-register a subdev */
  136. WARN_ON(sd->v4l2_dev != NULL);
  137. /*
  138. * The reason to acquire the module here is to avoid unloading
  139. * a module of sub-device which is registered to a media
  140. * device. To make it possible to unload modules for media
  141. * devices that also register sub-devices, do not
  142. * try_module_get() such sub-device owners.
  143. */
  144. sd->owner_v4l2_dev = v4l2_dev->dev && v4l2_dev->dev->driver &&
  145. sd->owner == v4l2_dev->dev->driver->owner;
  146. if (!sd->owner_v4l2_dev && !try_module_get(sd->owner))
  147. return -ENODEV;
  148. sd->v4l2_dev = v4l2_dev;
  149. if (sd->internal_ops && sd->internal_ops->registered) {
  150. err = sd->internal_ops->registered(sd);
  151. if (err)
  152. goto error_module;
  153. }
  154. /* This just returns 0 if either of the two args is NULL */
  155. err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler, NULL);
  156. if (err)
  157. goto error_unregister;
  158. #if defined(CONFIG_MEDIA_CONTROLLER)
  159. /* Register the entity. */
  160. if (v4l2_dev->mdev) {
  161. err = media_device_register_entity(v4l2_dev->mdev, entity);
  162. if (err < 0)
  163. goto error_unregister;
  164. }
  165. #endif
  166. spin_lock(&v4l2_dev->lock);
  167. list_add_tail(&sd->list, &v4l2_dev->subdevs);
  168. spin_unlock(&v4l2_dev->lock);
  169. return 0;
  170. error_unregister:
  171. if (sd->internal_ops && sd->internal_ops->unregistered)
  172. sd->internal_ops->unregistered(sd);
  173. error_module:
  174. if (!sd->owner_v4l2_dev)
  175. module_put(sd->owner);
  176. sd->v4l2_dev = NULL;
  177. return err;
  178. }
  179. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
  180. static void v4l2_device_release_subdev_node(struct video_device *vdev)
  181. {
  182. struct v4l2_subdev *sd = video_get_drvdata(vdev);
  183. sd->devnode = NULL;
  184. kfree(vdev);
  185. }
  186. int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
  187. {
  188. struct video_device *vdev;
  189. struct v4l2_subdev *sd;
  190. int err;
  191. /* Register a device node for every subdev marked with the
  192. * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
  193. */
  194. list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
  195. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
  196. continue;
  197. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  198. if (!vdev) {
  199. err = -ENOMEM;
  200. goto clean_up;
  201. }
  202. video_set_drvdata(vdev, sd);
  203. strlcpy(vdev->name, sd->name, sizeof(vdev->name));
  204. vdev->v4l2_dev = v4l2_dev;
  205. vdev->fops = &v4l2_subdev_fops;
  206. vdev->release = v4l2_device_release_subdev_node;
  207. vdev->ctrl_handler = sd->ctrl_handler;
  208. err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
  209. sd->owner);
  210. if (err < 0) {
  211. kfree(vdev);
  212. goto clean_up;
  213. }
  214. #if defined(CONFIG_MEDIA_CONTROLLER)
  215. sd->entity.info.v4l.major = VIDEO_MAJOR;
  216. sd->entity.info.v4l.minor = vdev->minor;
  217. #endif
  218. sd->devnode = vdev;
  219. }
  220. return 0;
  221. clean_up:
  222. list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
  223. if (!sd->devnode)
  224. break;
  225. video_unregister_device(sd->devnode);
  226. }
  227. return err;
  228. }
  229. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
  230. void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
  231. {
  232. struct v4l2_device *v4l2_dev;
  233. /* return if it isn't registered */
  234. if (sd == NULL || sd->v4l2_dev == NULL)
  235. return;
  236. v4l2_dev = sd->v4l2_dev;
  237. spin_lock(&v4l2_dev->lock);
  238. list_del(&sd->list);
  239. spin_unlock(&v4l2_dev->lock);
  240. if (sd->internal_ops && sd->internal_ops->unregistered)
  241. sd->internal_ops->unregistered(sd);
  242. sd->v4l2_dev = NULL;
  243. #if defined(CONFIG_MEDIA_CONTROLLER)
  244. if (v4l2_dev->mdev) {
  245. media_entity_remove_links(&sd->entity);
  246. media_device_unregister_entity(&sd->entity);
  247. }
  248. #endif
  249. video_unregister_device(sd->devnode);
  250. if (!sd->owner_v4l2_dev)
  251. module_put(sd->owner);
  252. }
  253. EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);