v4l2-device.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/i2c.h>
  19. #if defined(CONFIG_SPI)
  20. #include <linux/spi/spi.h>
  21. #endif
  22. #include <linux/videodev2.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-ctrls.h>
  25. int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
  26. {
  27. if (v4l2_dev == NULL)
  28. return -EINVAL;
  29. INIT_LIST_HEAD(&v4l2_dev->subdevs);
  30. spin_lock_init(&v4l2_dev->lock);
  31. mutex_init(&v4l2_dev->ioctl_lock);
  32. v4l2_dev->dev = dev;
  33. if (dev == NULL) {
  34. /* If dev == NULL, then name must be filled in by the caller */
  35. WARN_ON(!v4l2_dev->name[0]);
  36. return 0;
  37. }
  38. /* Set name to driver name + device name if it is empty. */
  39. if (!v4l2_dev->name[0])
  40. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
  41. dev->driver->name, dev_name(dev));
  42. if (!dev_get_drvdata(dev))
  43. dev_set_drvdata(dev, v4l2_dev);
  44. return 0;
  45. }
  46. EXPORT_SYMBOL_GPL(v4l2_device_register);
  47. int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
  48. atomic_t *instance)
  49. {
  50. int num = atomic_inc_return(instance) - 1;
  51. int len = strlen(basename);
  52. if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
  53. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  54. "%s-%d", basename, num);
  55. else
  56. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  57. "%s%d", basename, num);
  58. return num;
  59. }
  60. EXPORT_SYMBOL_GPL(v4l2_device_set_name);
  61. void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
  62. {
  63. if (v4l2_dev->dev == NULL)
  64. return;
  65. if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
  66. dev_set_drvdata(v4l2_dev->dev, NULL);
  67. v4l2_dev->dev = NULL;
  68. }
  69. EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
  70. void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
  71. {
  72. struct v4l2_subdev *sd, *next;
  73. if (v4l2_dev == NULL)
  74. return;
  75. v4l2_device_disconnect(v4l2_dev);
  76. /* Unregister subdevs */
  77. list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
  78. v4l2_device_unregister_subdev(sd);
  79. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  80. if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
  81. struct i2c_client *client = v4l2_get_subdevdata(sd);
  82. /* We need to unregister the i2c client explicitly.
  83. We cannot rely on i2c_del_adapter to always
  84. unregister clients for us, since if the i2c bus
  85. is a platform bus, then it is never deleted. */
  86. if (client)
  87. i2c_unregister_device(client);
  88. continue;
  89. }
  90. #endif
  91. #if defined(CONFIG_SPI)
  92. if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
  93. struct spi_device *spi = v4l2_get_subdevdata(sd);
  94. if (spi)
  95. spi_unregister_device(spi);
  96. continue;
  97. }
  98. #endif
  99. }
  100. }
  101. EXPORT_SYMBOL_GPL(v4l2_device_unregister);
  102. int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
  103. struct v4l2_subdev *sd)
  104. {
  105. int err;
  106. /* Check for valid input */
  107. if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
  108. return -EINVAL;
  109. /* Warn if we apparently re-register a subdev */
  110. WARN_ON(sd->v4l2_dev != NULL);
  111. if (!try_module_get(sd->owner))
  112. return -ENODEV;
  113. sd->v4l2_dev = v4l2_dev;
  114. if (sd->internal_ops && sd->internal_ops->registered) {
  115. err = sd->internal_ops->registered(sd);
  116. if (err)
  117. return err;
  118. }
  119. /* This just returns 0 if either of the two args is NULL */
  120. err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler);
  121. if (err) {
  122. if (sd->internal_ops && sd->internal_ops->unregistered)
  123. sd->internal_ops->unregistered(sd);
  124. return err;
  125. }
  126. spin_lock(&v4l2_dev->lock);
  127. list_add_tail(&sd->list, &v4l2_dev->subdevs);
  128. spin_unlock(&v4l2_dev->lock);
  129. return 0;
  130. }
  131. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
  132. int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
  133. {
  134. struct video_device *vdev;
  135. struct v4l2_subdev *sd;
  136. int err;
  137. /* Register a device node for every subdev marked with the
  138. * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
  139. */
  140. list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
  141. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
  142. continue;
  143. vdev = &sd->devnode;
  144. strlcpy(vdev->name, sd->name, sizeof(vdev->name));
  145. vdev->v4l2_dev = v4l2_dev;
  146. vdev->fops = &v4l2_subdev_fops;
  147. vdev->release = video_device_release_empty;
  148. err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
  149. sd->owner);
  150. if (err < 0)
  151. return err;
  152. }
  153. return 0;
  154. }
  155. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
  156. void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
  157. {
  158. /* return if it isn't registered */
  159. if (sd == NULL || sd->v4l2_dev == NULL)
  160. return;
  161. spin_lock(&sd->v4l2_dev->lock);
  162. list_del(&sd->list);
  163. spin_unlock(&sd->v4l2_dev->lock);
  164. if (sd->internal_ops && sd->internal_ops->unregistered)
  165. sd->internal_ops->unregistered(sd);
  166. sd->v4l2_dev = NULL;
  167. video_unregister_device(&sd->devnode);
  168. module_put(sd->owner);
  169. }
  170. EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);