uvc_entity.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * uvc_entity.c -- USB Video Class driver
  3. *
  4. * Copyright (C) 2005-2011
  5. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/list.h>
  15. #include <linux/videodev2.h>
  16. #include <media/v4l2-common.h>
  17. #include "uvcvideo.h"
  18. /* ------------------------------------------------------------------------
  19. * Video subdevices registration and unregistration
  20. */
  21. static int uvc_mc_register_entity(struct uvc_video_chain *chain,
  22. struct uvc_entity *entity)
  23. {
  24. const u32 flags = MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE;
  25. struct media_entity *sink;
  26. unsigned int i;
  27. int ret;
  28. sink = (UVC_ENTITY_TYPE(entity) == UVC_TT_STREAMING)
  29. ? (entity->vdev ? &entity->vdev->entity : NULL)
  30. : &entity->subdev.entity;
  31. if (sink == NULL)
  32. return 0;
  33. for (i = 0; i < entity->num_pads; ++i) {
  34. struct media_entity *source;
  35. struct uvc_entity *remote;
  36. u8 remote_pad;
  37. if (!(entity->pads[i].flags & MEDIA_PAD_FL_SINK))
  38. continue;
  39. remote = uvc_entity_by_id(chain->dev, entity->baSourceID[i]);
  40. if (remote == NULL)
  41. return -EINVAL;
  42. source = (UVC_ENTITY_TYPE(remote) == UVC_TT_STREAMING)
  43. ? (remote->vdev ? &remote->vdev->entity : NULL)
  44. : &remote->subdev.entity;
  45. if (source == NULL)
  46. continue;
  47. remote_pad = remote->num_pads - 1;
  48. ret = media_entity_create_link(source, remote_pad,
  49. sink, i, flags);
  50. if (ret < 0)
  51. return ret;
  52. }
  53. if (UVC_ENTITY_TYPE(entity) == UVC_TT_STREAMING)
  54. return 0;
  55. return v4l2_device_register_subdev(&chain->dev->vdev, &entity->subdev);
  56. }
  57. static struct v4l2_subdev_ops uvc_subdev_ops = {
  58. };
  59. void uvc_mc_cleanup_entity(struct uvc_entity *entity)
  60. {
  61. if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING)
  62. media_entity_cleanup(&entity->subdev.entity);
  63. else if (entity->vdev != NULL)
  64. media_entity_cleanup(&entity->vdev->entity);
  65. }
  66. static int uvc_mc_init_entity(struct uvc_entity *entity)
  67. {
  68. int ret;
  69. if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING) {
  70. v4l2_subdev_init(&entity->subdev, &uvc_subdev_ops);
  71. strlcpy(entity->subdev.name, entity->name,
  72. sizeof(entity->subdev.name));
  73. ret = media_entity_init(&entity->subdev.entity,
  74. entity->num_pads, entity->pads, 0);
  75. } else if (entity->vdev != NULL) {
  76. ret = media_entity_init(&entity->vdev->entity,
  77. entity->num_pads, entity->pads, 0);
  78. if (entity->flags & UVC_ENTITY_FLAG_DEFAULT)
  79. entity->vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
  80. } else
  81. ret = 0;
  82. return ret;
  83. }
  84. int uvc_mc_register_entities(struct uvc_video_chain *chain)
  85. {
  86. struct uvc_entity *entity;
  87. int ret;
  88. list_for_each_entry(entity, &chain->entities, chain) {
  89. ret = uvc_mc_init_entity(entity);
  90. if (ret < 0) {
  91. uvc_printk(KERN_INFO, "Failed to initialize entity for "
  92. "entity %u\n", entity->id);
  93. return ret;
  94. }
  95. }
  96. list_for_each_entry(entity, &chain->entities, chain) {
  97. ret = uvc_mc_register_entity(chain, entity);
  98. if (ret < 0) {
  99. uvc_printk(KERN_INFO, "Failed to register entity for "
  100. "entity %u\n", entity->id);
  101. return ret;
  102. }
  103. }
  104. return 0;
  105. }