v4l2-device.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
  44. {
  45. if (v4l2_dev->dev) {
  46. dev_set_drvdata(v4l2_dev->dev, NULL);
  47. v4l2_dev->dev = NULL;
  48. }
  49. }
  50. EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
  51. void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
  52. {
  53. struct v4l2_subdev *sd, *next;
  54. if (v4l2_dev == NULL)
  55. return;
  56. v4l2_device_disconnect(v4l2_dev);
  57. /* Unregister subdevs */
  58. list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list)
  59. v4l2_device_unregister_subdev(sd);
  60. }
  61. EXPORT_SYMBOL_GPL(v4l2_device_unregister);
  62. int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
  63. struct v4l2_subdev *sd)
  64. {
  65. /* Check for valid input */
  66. if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
  67. return -EINVAL;
  68. /* Warn if we apparently re-register a subdev */
  69. WARN_ON(sd->v4l2_dev != NULL);
  70. if (!try_module_get(sd->owner))
  71. return -ENODEV;
  72. sd->v4l2_dev = v4l2_dev;
  73. spin_lock(&v4l2_dev->lock);
  74. list_add_tail(&sd->list, &v4l2_dev->subdevs);
  75. spin_unlock(&v4l2_dev->lock);
  76. return 0;
  77. }
  78. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
  79. void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
  80. {
  81. /* return if it isn't registered */
  82. if (sd == NULL || sd->v4l2_dev == NULL)
  83. return;
  84. spin_lock(&sd->v4l2_dev->lock);
  85. list_del(&sd->list);
  86. spin_unlock(&sd->v4l2_dev->lock);
  87. sd->v4l2_dev = NULL;
  88. module_put(sd->owner);
  89. }
  90. EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);