uvc_entity.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. static int uvc_mc_create_links(struct uvc_video_chain *chain,
  19. struct uvc_entity *entity)
  20. {
  21. const u32 flags = MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE;
  22. struct media_entity *sink;
  23. unsigned int i;
  24. int ret;
  25. sink = (UVC_ENTITY_TYPE(entity) == UVC_TT_STREAMING)
  26. ? (entity->vdev ? &entity->vdev->entity : NULL)
  27. : &entity->subdev.entity;
  28. if (sink == NULL)
  29. return 0;
  30. for (i = 0; i < entity->num_pads; ++i) {
  31. struct media_entity *source;
  32. struct uvc_entity *remote;
  33. u8 remote_pad;
  34. if (!(entity->pads[i].flags & MEDIA_PAD_FL_SINK))
  35. continue;
  36. remote = uvc_entity_by_id(chain->dev, entity->baSourceID[i]);
  37. if (remote == NULL)
  38. return -EINVAL;
  39. source = (UVC_ENTITY_TYPE(remote) == UVC_TT_STREAMING)
  40. ? (remote->vdev ? &remote->vdev->entity : NULL)
  41. : &remote->subdev.entity;
  42. if (source == NULL)
  43. continue;
  44. remote_pad = remote->num_pads - 1;
  45. ret = media_create_pad_link(source, remote_pad,
  46. sink, i, flags);
  47. if (ret < 0)
  48. return ret;
  49. }
  50. return 0;
  51. }
  52. static const struct v4l2_subdev_ops uvc_subdev_ops = {
  53. };
  54. void uvc_mc_cleanup_entity(struct uvc_entity *entity)
  55. {
  56. if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING)
  57. media_entity_cleanup(&entity->subdev.entity);
  58. else if (entity->vdev != NULL)
  59. media_entity_cleanup(&entity->vdev->entity);
  60. }
  61. static int uvc_mc_init_entity(struct uvc_video_chain *chain,
  62. struct uvc_entity *entity)
  63. {
  64. int ret;
  65. if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING) {
  66. v4l2_subdev_init(&entity->subdev, &uvc_subdev_ops);
  67. strlcpy(entity->subdev.name, entity->name,
  68. sizeof(entity->subdev.name));
  69. ret = media_entity_pads_init(&entity->subdev.entity,
  70. entity->num_pads, entity->pads);
  71. if (ret < 0)
  72. return ret;
  73. ret = v4l2_device_register_subdev(&chain->dev->vdev,
  74. &entity->subdev);
  75. } else if (entity->vdev != NULL) {
  76. ret = media_entity_pads_init(&entity->vdev->entity,
  77. entity->num_pads, entity->pads);
  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(chain, 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_create_links(chain, entity);
  98. if (ret < 0) {
  99. uvc_printk(KERN_INFO, "Failed to create links for "
  100. "entity %u\n", entity->id);
  101. return ret;
  102. }
  103. }
  104. return 0;
  105. }