v4l2-device.c 3.9 KB

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