v4l2-device.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. 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. void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
  133. {
  134. /* return if it isn't registered */
  135. if (sd == NULL || sd->v4l2_dev == NULL)
  136. return;
  137. spin_lock(&sd->v4l2_dev->lock);
  138. list_del(&sd->list);
  139. spin_unlock(&sd->v4l2_dev->lock);
  140. if (sd->internal_ops && sd->internal_ops->unregistered)
  141. sd->internal_ops->unregistered(sd);
  142. sd->v4l2_dev = NULL;
  143. module_put(sd->owner);
  144. }
  145. EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);