v4l2-device.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. v4l2_dev->dev = dev;
  32. if (dev == NULL) {
  33. /* If dev == NULL, then name must be filled in by the caller */
  34. WARN_ON(!v4l2_dev->name[0]);
  35. return 0;
  36. }
  37. /* Set name to driver name + device name if it is empty. */
  38. if (!v4l2_dev->name[0])
  39. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
  40. dev->driver->name, dev_name(dev));
  41. if (dev_get_drvdata(dev))
  42. v4l2_warn(v4l2_dev, "Non-NULL drvdata on register\n");
  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) {
  64. dev_set_drvdata(v4l2_dev->dev, NULL);
  65. v4l2_dev->dev = NULL;
  66. }
  67. }
  68. EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
  69. void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
  70. {
  71. struct v4l2_subdev *sd, *next;
  72. if (v4l2_dev == NULL)
  73. return;
  74. v4l2_device_disconnect(v4l2_dev);
  75. /* Unregister subdevs */
  76. list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
  77. v4l2_device_unregister_subdev(sd);
  78. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  79. if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
  80. struct i2c_client *client = v4l2_get_subdevdata(sd);
  81. /* We need to unregister the i2c client explicitly.
  82. We cannot rely on i2c_del_adapter to always
  83. unregister clients for us, since if the i2c bus
  84. is a platform bus, then it is never deleted. */
  85. if (client)
  86. i2c_unregister_device(client);
  87. }
  88. #endif
  89. #if defined(CONFIG_SPI)
  90. if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
  91. struct spi_device *spi = v4l2_get_subdevdata(sd);
  92. if (spi)
  93. spi_unregister_device(spi);
  94. }
  95. #endif
  96. }
  97. }
  98. EXPORT_SYMBOL_GPL(v4l2_device_unregister);
  99. int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
  100. struct v4l2_subdev *sd)
  101. {
  102. int err;
  103. /* Check for valid input */
  104. if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
  105. return -EINVAL;
  106. /* Warn if we apparently re-register a subdev */
  107. WARN_ON(sd->v4l2_dev != NULL);
  108. if (!try_module_get(sd->owner))
  109. return -ENODEV;
  110. /* This just returns 0 if either of the two args is NULL */
  111. err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler);
  112. if (err)
  113. return err;
  114. sd->v4l2_dev = v4l2_dev;
  115. spin_lock(&v4l2_dev->lock);
  116. list_add_tail(&sd->list, &v4l2_dev->subdevs);
  117. spin_unlock(&v4l2_dev->lock);
  118. return 0;
  119. }
  120. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
  121. void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
  122. {
  123. /* return if it isn't registered */
  124. if (sd == NULL || sd->v4l2_dev == NULL)
  125. return;
  126. spin_lock(&sd->v4l2_dev->lock);
  127. list_del(&sd->list);
  128. spin_unlock(&sd->v4l2_dev->lock);
  129. sd->v4l2_dev = NULL;
  130. module_put(sd->owner);
  131. }
  132. EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);