v4l2-device.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
  25. {
  26. if (v4l2_dev == NULL)
  27. return -EINVAL;
  28. INIT_LIST_HEAD(&v4l2_dev->subdevs);
  29. spin_lock_init(&v4l2_dev->lock);
  30. v4l2_dev->dev = dev;
  31. if (dev == NULL) {
  32. /* If dev == NULL, then name must be filled in by the caller */
  33. WARN_ON(!v4l2_dev->name[0]);
  34. return 0;
  35. }
  36. /* Set name to driver name + device name if it is empty. */
  37. if (!v4l2_dev->name[0])
  38. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
  39. dev->driver->name, dev_name(dev));
  40. if (dev_get_drvdata(dev))
  41. v4l2_warn(v4l2_dev, "Non-NULL drvdata on register\n");
  42. dev_set_drvdata(dev, v4l2_dev);
  43. return 0;
  44. }
  45. EXPORT_SYMBOL_GPL(v4l2_device_register);
  46. int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
  47. atomic_t *instance)
  48. {
  49. int num = atomic_inc_return(instance) - 1;
  50. int len = strlen(basename);
  51. if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
  52. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  53. "%s-%d", basename, num);
  54. else
  55. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  56. "%s%d", basename, num);
  57. return num;
  58. }
  59. EXPORT_SYMBOL_GPL(v4l2_device_set_name);
  60. void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
  61. {
  62. if (v4l2_dev->dev) {
  63. dev_set_drvdata(v4l2_dev->dev, NULL);
  64. v4l2_dev->dev = NULL;
  65. }
  66. }
  67. EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
  68. void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
  69. {
  70. struct v4l2_subdev *sd, *next;
  71. if (v4l2_dev == NULL)
  72. return;
  73. v4l2_device_disconnect(v4l2_dev);
  74. /* Unregister subdevs */
  75. list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
  76. v4l2_device_unregister_subdev(sd);
  77. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  78. if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
  79. struct i2c_client *client = v4l2_get_subdevdata(sd);
  80. /* We need to unregister the i2c client explicitly.
  81. We cannot rely on i2c_del_adapter to always
  82. unregister clients for us, since if the i2c bus
  83. is a platform bus, then it is never deleted. */
  84. if (client)
  85. i2c_unregister_device(client);
  86. }
  87. #endif
  88. #if defined(CONFIG_SPI)
  89. if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
  90. struct spi_device *spi = v4l2_get_subdevdata(sd);
  91. if (spi)
  92. spi_unregister_device(spi);
  93. }
  94. #endif
  95. }
  96. }
  97. EXPORT_SYMBOL_GPL(v4l2_device_unregister);
  98. int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
  99. struct v4l2_subdev *sd)
  100. {
  101. /* Check for valid input */
  102. if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
  103. return -EINVAL;
  104. /* Warn if we apparently re-register a subdev */
  105. WARN_ON(sd->v4l2_dev != NULL);
  106. if (!try_module_get(sd->owner))
  107. return -ENODEV;
  108. sd->v4l2_dev = v4l2_dev;
  109. spin_lock(&v4l2_dev->lock);
  110. list_add_tail(&sd->list, &v4l2_dev->subdevs);
  111. spin_unlock(&v4l2_dev->lock);
  112. return 0;
  113. }
  114. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
  115. void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
  116. {
  117. /* return if it isn't registered */
  118. if (sd == NULL || sd->v4l2_dev == NULL)
  119. return;
  120. spin_lock(&sd->v4l2_dev->lock);
  121. list_del(&sd->list);
  122. spin_unlock(&sd->v4l2_dev->lock);
  123. sd->v4l2_dev = NULL;
  124. module_put(sd->owner);
  125. }
  126. EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);