v4l2-device.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. V4L2 device support.
  4. Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
  5. */
  6. #include <linux/types.h>
  7. #include <linux/ioctl.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/videodev2.h>
  11. #include <media/v4l2-device.h>
  12. #include <media/v4l2-ctrls.h>
  13. int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
  14. {
  15. if (v4l2_dev == NULL)
  16. return -EINVAL;
  17. INIT_LIST_HEAD(&v4l2_dev->subdevs);
  18. spin_lock_init(&v4l2_dev->lock);
  19. v4l2_prio_init(&v4l2_dev->prio);
  20. kref_init(&v4l2_dev->ref);
  21. get_device(dev);
  22. v4l2_dev->dev = dev;
  23. if (dev == NULL) {
  24. /* If dev == NULL, then name must be filled in by the caller */
  25. if (WARN_ON(!v4l2_dev->name[0]))
  26. return -EINVAL;
  27. return 0;
  28. }
  29. /* Set name to driver name + device name if it is empty. */
  30. if (!v4l2_dev->name[0])
  31. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
  32. dev->driver->name, dev_name(dev));
  33. if (!dev_get_drvdata(dev))
  34. dev_set_drvdata(dev, v4l2_dev);
  35. return 0;
  36. }
  37. EXPORT_SYMBOL_GPL(v4l2_device_register);
  38. static void v4l2_device_release(struct kref *ref)
  39. {
  40. struct v4l2_device *v4l2_dev =
  41. container_of(ref, struct v4l2_device, ref);
  42. if (v4l2_dev->release)
  43. v4l2_dev->release(v4l2_dev);
  44. }
  45. int v4l2_device_put(struct v4l2_device *v4l2_dev)
  46. {
  47. return kref_put(&v4l2_dev->ref, v4l2_device_release);
  48. }
  49. EXPORT_SYMBOL_GPL(v4l2_device_put);
  50. int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
  51. atomic_t *instance)
  52. {
  53. int num = atomic_inc_return(instance) - 1;
  54. int len = strlen(basename);
  55. if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
  56. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  57. "%s-%d", basename, num);
  58. else
  59. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  60. "%s%d", basename, num);
  61. return num;
  62. }
  63. EXPORT_SYMBOL_GPL(v4l2_device_set_name);
  64. void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
  65. {
  66. if (v4l2_dev->dev == NULL)
  67. return;
  68. if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
  69. dev_set_drvdata(v4l2_dev->dev, NULL);
  70. put_device(v4l2_dev->dev);
  71. v4l2_dev->dev = NULL;
  72. }
  73. EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
  74. void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
  75. {
  76. struct v4l2_subdev *sd, *next;
  77. /* Just return if v4l2_dev is NULL or if it was already
  78. * unregistered before. */
  79. if (v4l2_dev == NULL || !v4l2_dev->name[0])
  80. return;
  81. v4l2_device_disconnect(v4l2_dev);
  82. /* Unregister subdevs */
  83. list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
  84. v4l2_device_unregister_subdev(sd);
  85. if (sd->flags & V4L2_SUBDEV_FL_IS_I2C)
  86. v4l2_i2c_subdev_unregister(sd);
  87. else if (sd->flags & V4L2_SUBDEV_FL_IS_SPI)
  88. v4l2_spi_subdev_unregister(sd);
  89. }
  90. /* Mark as unregistered, thus preventing duplicate unregistrations */
  91. v4l2_dev->name[0] = '\0';
  92. }
  93. EXPORT_SYMBOL_GPL(v4l2_device_unregister);
  94. int __v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
  95. struct v4l2_subdev *sd, struct module *module)
  96. {
  97. int err;
  98. /* Check for valid input */
  99. if (!v4l2_dev || !sd || sd->v4l2_dev || !sd->name[0])
  100. return -EINVAL;
  101. /*
  102. * The reason to acquire the module here is to avoid unloading
  103. * a module of sub-device which is registered to a media
  104. * device. To make it possible to unload modules for media
  105. * devices that also register sub-devices, do not
  106. * try_module_get() such sub-device owners.
  107. */
  108. sd->owner_v4l2_dev = v4l2_dev->dev && v4l2_dev->dev->driver &&
  109. module == v4l2_dev->dev->driver->owner;
  110. if (!sd->owner_v4l2_dev && !try_module_get(module))
  111. return -ENODEV;
  112. sd->v4l2_dev = v4l2_dev;
  113. /* This just returns 0 if either of the two args is NULL */
  114. err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler,
  115. NULL, true);
  116. if (err)
  117. goto error_module;
  118. #if defined(CONFIG_MEDIA_CONTROLLER)
  119. /* Register the entity. */
  120. if (v4l2_dev->mdev) {
  121. err = media_device_register_entity(v4l2_dev->mdev, &sd->entity);
  122. if (err < 0)
  123. goto error_module;
  124. }
  125. #endif
  126. if (sd->internal_ops && sd->internal_ops->registered) {
  127. err = sd->internal_ops->registered(sd);
  128. if (err)
  129. goto error_unregister;
  130. }
  131. sd->owner = module;
  132. spin_lock(&v4l2_dev->lock);
  133. list_add_tail(&sd->list, &v4l2_dev->subdevs);
  134. spin_unlock(&v4l2_dev->lock);
  135. return 0;
  136. error_unregister:
  137. #if defined(CONFIG_MEDIA_CONTROLLER)
  138. media_device_unregister_entity(&sd->entity);
  139. #endif
  140. error_module:
  141. if (!sd->owner_v4l2_dev)
  142. module_put(sd->owner);
  143. sd->v4l2_dev = NULL;
  144. return err;
  145. }
  146. EXPORT_SYMBOL_GPL(__v4l2_device_register_subdev);
  147. static void v4l2_subdev_release(struct v4l2_subdev *sd)
  148. {
  149. struct module *owner = !sd->owner_v4l2_dev ? sd->owner : NULL;
  150. if (sd->internal_ops && sd->internal_ops->release)
  151. sd->internal_ops->release(sd);
  152. sd->devnode = NULL;
  153. module_put(owner);
  154. }
  155. static void v4l2_device_release_subdev_node(struct video_device *vdev)
  156. {
  157. v4l2_subdev_release(video_get_drvdata(vdev));
  158. kfree(vdev);
  159. }
  160. int __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
  161. bool read_only)
  162. {
  163. struct video_device *vdev;
  164. struct v4l2_subdev *sd;
  165. int err;
  166. /* Register a device node for every subdev marked with the
  167. * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
  168. */
  169. list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
  170. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
  171. continue;
  172. if (sd->devnode)
  173. continue;
  174. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  175. if (!vdev) {
  176. err = -ENOMEM;
  177. goto clean_up;
  178. }
  179. video_set_drvdata(vdev, sd);
  180. strscpy(vdev->name, sd->name, sizeof(vdev->name));
  181. vdev->dev_parent = sd->dev;
  182. vdev->v4l2_dev = v4l2_dev;
  183. vdev->fops = &v4l2_subdev_fops;
  184. vdev->release = v4l2_device_release_subdev_node;
  185. vdev->ctrl_handler = sd->ctrl_handler;
  186. if (read_only)
  187. set_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
  188. sd->devnode = vdev;
  189. err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
  190. sd->owner);
  191. if (err < 0) {
  192. sd->devnode = NULL;
  193. kfree(vdev);
  194. goto clean_up;
  195. }
  196. #if defined(CONFIG_MEDIA_CONTROLLER)
  197. sd->entity.info.dev.major = VIDEO_MAJOR;
  198. sd->entity.info.dev.minor = vdev->minor;
  199. /* Interface is created by __video_register_device() */
  200. if (vdev->v4l2_dev->mdev) {
  201. struct media_link *link;
  202. link = media_create_intf_link(&sd->entity,
  203. &vdev->intf_devnode->intf,
  204. MEDIA_LNK_FL_ENABLED |
  205. MEDIA_LNK_FL_IMMUTABLE);
  206. if (!link) {
  207. err = -ENOMEM;
  208. goto clean_up;
  209. }
  210. }
  211. #endif
  212. }
  213. return 0;
  214. clean_up:
  215. list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
  216. if (!sd->devnode)
  217. break;
  218. video_unregister_device(sd->devnode);
  219. }
  220. return err;
  221. }
  222. EXPORT_SYMBOL_GPL(__v4l2_device_register_subdev_nodes);
  223. void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
  224. {
  225. struct v4l2_device *v4l2_dev;
  226. /* return if it isn't registered */
  227. if (sd == NULL || sd->v4l2_dev == NULL)
  228. return;
  229. v4l2_dev = sd->v4l2_dev;
  230. spin_lock(&v4l2_dev->lock);
  231. list_del(&sd->list);
  232. spin_unlock(&v4l2_dev->lock);
  233. if (sd->internal_ops && sd->internal_ops->unregistered)
  234. sd->internal_ops->unregistered(sd);
  235. sd->v4l2_dev = NULL;
  236. #if defined(CONFIG_MEDIA_CONTROLLER)
  237. if (v4l2_dev->mdev) {
  238. /*
  239. * No need to explicitly remove links, as both pads and
  240. * links are removed by the function below, in the right order
  241. */
  242. media_device_unregister_entity(&sd->entity);
  243. }
  244. #endif
  245. if (sd->devnode)
  246. video_unregister_device(sd->devnode);
  247. else
  248. v4l2_subdev_release(sd);
  249. }
  250. EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);