v4l2-device.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 (dev == NULL || v4l2_dev == NULL)
  24. return -EINVAL;
  25. /* Warn if we apparently re-register a device */
  26. WARN_ON(dev_get_drvdata(dev) != NULL);
  27. INIT_LIST_HEAD(&v4l2_dev->subdevs);
  28. spin_lock_init(&v4l2_dev->lock);
  29. v4l2_dev->dev = dev;
  30. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
  31. dev->driver->name, dev->bus_id);
  32. dev_set_drvdata(dev, v4l2_dev);
  33. return 0;
  34. }
  35. EXPORT_SYMBOL_GPL(v4l2_device_register);
  36. void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
  37. {
  38. struct v4l2_subdev *sd, *next;
  39. if (v4l2_dev == NULL || v4l2_dev->dev == NULL)
  40. return;
  41. dev_set_drvdata(v4l2_dev->dev, NULL);
  42. /* unregister subdevs */
  43. list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list)
  44. v4l2_device_unregister_subdev(sd);
  45. v4l2_dev->dev = NULL;
  46. }
  47. EXPORT_SYMBOL_GPL(v4l2_device_unregister);
  48. int v4l2_device_register_subdev(struct v4l2_device *dev, struct v4l2_subdev *sd)
  49. {
  50. /* Check for valid input */
  51. if (dev == NULL || sd == NULL || !sd->name[0])
  52. return -EINVAL;
  53. /* Warn if we apparently re-register a subdev */
  54. WARN_ON(sd->dev != NULL);
  55. if (!try_module_get(sd->owner))
  56. return -ENODEV;
  57. sd->dev = dev;
  58. spin_lock(&dev->lock);
  59. list_add_tail(&sd->list, &dev->subdevs);
  60. spin_unlock(&dev->lock);
  61. return 0;
  62. }
  63. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
  64. void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
  65. {
  66. /* return if it isn't registered */
  67. if (sd == NULL || sd->dev == NULL)
  68. return;
  69. spin_lock(&sd->dev->lock);
  70. list_del(&sd->list);
  71. spin_unlock(&sd->dev->lock);
  72. sd->dev = NULL;
  73. module_put(sd->owner);
  74. }
  75. EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);