v4l2-device.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. v4l2_warn(v4l2_dev, "Non-NULL drvdata on register\n");
  44. dev_set_drvdata(dev, v4l2_dev);
  45. return 0;
  46. }
  47. EXPORT_SYMBOL_GPL(v4l2_device_register);
  48. int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
  49. atomic_t *instance)
  50. {
  51. int num = atomic_inc_return(instance) - 1;
  52. int len = strlen(basename);
  53. if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
  54. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  55. "%s-%d", basename, num);
  56. else
  57. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  58. "%s%d", basename, num);
  59. return num;
  60. }
  61. EXPORT_SYMBOL_GPL(v4l2_device_set_name);
  62. void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
  63. {
  64. if (v4l2_dev->dev) {
  65. dev_set_drvdata(v4l2_dev->dev, NULL);
  66. v4l2_dev->dev = NULL;
  67. }
  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. }
  89. #endif
  90. #if defined(CONFIG_SPI)
  91. if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
  92. struct spi_device *spi = v4l2_get_subdevdata(sd);
  93. if (spi)
  94. spi_unregister_device(spi);
  95. }
  96. #endif
  97. }
  98. }
  99. EXPORT_SYMBOL_GPL(v4l2_device_unregister);
  100. int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
  101. struct v4l2_subdev *sd)
  102. {
  103. int err;
  104. /* Check for valid input */
  105. if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
  106. return -EINVAL;
  107. /* Warn if we apparently re-register a subdev */
  108. WARN_ON(sd->v4l2_dev != NULL);
  109. if (!try_module_get(sd->owner))
  110. return -ENODEV;
  111. sd->v4l2_dev = v4l2_dev;
  112. if (sd->internal_ops && sd->internal_ops->registered) {
  113. err = sd->internal_ops->registered(sd);
  114. if (err)
  115. return err;
  116. }
  117. /* This just returns 0 if either of the two args is NULL */
  118. err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler);
  119. if (err) {
  120. if (sd->internal_ops && sd->internal_ops->unregistered)
  121. sd->internal_ops->unregistered(sd);
  122. return err;
  123. }
  124. spin_lock(&v4l2_dev->lock);
  125. list_add_tail(&sd->list, &v4l2_dev->subdevs);
  126. spin_unlock(&v4l2_dev->lock);
  127. return 0;
  128. }
  129. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
  130. void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
  131. {
  132. /* return if it isn't registered */
  133. if (sd == NULL || sd->v4l2_dev == NULL)
  134. return;
  135. spin_lock(&sd->v4l2_dev->lock);
  136. list_del(&sd->list);
  137. spin_unlock(&sd->v4l2_dev->lock);
  138. if (sd->internal_ops && sd->internal_ops->unregistered)
  139. sd->internal_ops->unregistered(sd);
  140. sd->v4l2_dev = NULL;
  141. module_put(sd->owner);
  142. }
  143. EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);