v4l2-device.c 2.8 KB

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