uvc_entity.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. u32 function;
  67. v4l2_subdev_init(&entity->subdev, &uvc_subdev_ops);
  68. strlcpy(entity->subdev.name, entity->name,
  69. sizeof(entity->subdev.name));
  70. switch (UVC_ENTITY_TYPE(entity)) {
  71. case UVC_VC_SELECTOR_UNIT:
  72. function = MEDIA_ENT_F_VID_MUX;
  73. break;
  74. case UVC_VC_PROCESSING_UNIT:
  75. case UVC_VC_EXTENSION_UNIT:
  76. /* For lack of a better option. */
  77. function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
  78. break;
  79. case UVC_COMPOSITE_CONNECTOR:
  80. case UVC_COMPONENT_CONNECTOR:
  81. function = MEDIA_ENT_F_CONN_COMPOSITE;
  82. break;
  83. case UVC_SVIDEO_CONNECTOR:
  84. function = MEDIA_ENT_F_CONN_SVIDEO;
  85. break;
  86. case UVC_ITT_CAMERA:
  87. function = MEDIA_ENT_F_CAM_SENSOR;
  88. break;
  89. case UVC_TT_VENDOR_SPECIFIC:
  90. case UVC_ITT_VENDOR_SPECIFIC:
  91. case UVC_ITT_MEDIA_TRANSPORT_INPUT:
  92. case UVC_OTT_VENDOR_SPECIFIC:
  93. case UVC_OTT_DISPLAY:
  94. case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
  95. case UVC_EXTERNAL_VENDOR_SPECIFIC:
  96. default:
  97. function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
  98. break;
  99. }
  100. entity->subdev.entity.function = function;
  101. ret = media_entity_pads_init(&entity->subdev.entity,
  102. entity->num_pads, entity->pads);
  103. if (ret < 0)
  104. return ret;
  105. ret = v4l2_device_register_subdev(&chain->dev->vdev,
  106. &entity->subdev);
  107. } else if (entity->vdev != NULL) {
  108. ret = media_entity_pads_init(&entity->vdev->entity,
  109. entity->num_pads, entity->pads);
  110. if (entity->flags & UVC_ENTITY_FLAG_DEFAULT)
  111. entity->vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
  112. } else
  113. ret = 0;
  114. return ret;
  115. }
  116. int uvc_mc_register_entities(struct uvc_video_chain *chain)
  117. {
  118. struct uvc_entity *entity;
  119. int ret;
  120. list_for_each_entry(entity, &chain->entities, chain) {
  121. ret = uvc_mc_init_entity(chain, entity);
  122. if (ret < 0) {
  123. uvc_printk(KERN_INFO, "Failed to initialize entity for "
  124. "entity %u\n", entity->id);
  125. return ret;
  126. }
  127. }
  128. list_for_each_entry(entity, &chain->entities, chain) {
  129. ret = uvc_mc_create_links(chain, entity);
  130. if (ret < 0) {
  131. uvc_printk(KERN_INFO, "Failed to create links for "
  132. "entity %u\n", entity->id);
  133. return ret;
  134. }
  135. }
  136. return 0;
  137. }