uvc_entity.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * uvc_entity.c -- USB Video Class driver
  4. *
  5. * Copyright (C) 2005-2011
  6. * Laurent Pinchart ([email protected])
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/list.h>
  10. #include <linux/videodev2.h>
  11. #include <media/v4l2-common.h>
  12. #include "uvcvideo.h"
  13. static int uvc_mc_create_links(struct uvc_video_chain *chain,
  14. struct uvc_entity *entity)
  15. {
  16. const u32 flags = MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE;
  17. struct media_entity *sink;
  18. unsigned int i;
  19. int ret;
  20. sink = (UVC_ENTITY_TYPE(entity) == UVC_TT_STREAMING)
  21. ? (entity->vdev ? &entity->vdev->entity : NULL)
  22. : &entity->subdev.entity;
  23. if (sink == NULL)
  24. return 0;
  25. for (i = 0; i < entity->num_pads; ++i) {
  26. struct media_entity *source;
  27. struct uvc_entity *remote;
  28. u8 remote_pad;
  29. if (!(entity->pads[i].flags & MEDIA_PAD_FL_SINK))
  30. continue;
  31. remote = uvc_entity_by_id(chain->dev, entity->baSourceID[i]);
  32. if (remote == NULL || remote->num_pads == 0)
  33. return -EINVAL;
  34. source = (UVC_ENTITY_TYPE(remote) == UVC_TT_STREAMING)
  35. ? (remote->vdev ? &remote->vdev->entity : NULL)
  36. : &remote->subdev.entity;
  37. if (source == NULL)
  38. continue;
  39. remote_pad = remote->num_pads - 1;
  40. ret = media_create_pad_link(source, remote_pad,
  41. sink, i, flags);
  42. if (ret < 0)
  43. return ret;
  44. }
  45. return 0;
  46. }
  47. static const struct v4l2_subdev_ops uvc_subdev_ops = {
  48. };
  49. void uvc_mc_cleanup_entity(struct uvc_entity *entity)
  50. {
  51. if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING)
  52. media_entity_cleanup(&entity->subdev.entity);
  53. else if (entity->vdev != NULL)
  54. media_entity_cleanup(&entity->vdev->entity);
  55. }
  56. static int uvc_mc_init_entity(struct uvc_video_chain *chain,
  57. struct uvc_entity *entity)
  58. {
  59. int ret;
  60. if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING) {
  61. u32 function;
  62. v4l2_subdev_init(&entity->subdev, &uvc_subdev_ops);
  63. strscpy(entity->subdev.name, entity->name,
  64. sizeof(entity->subdev.name));
  65. switch (UVC_ENTITY_TYPE(entity)) {
  66. case UVC_VC_SELECTOR_UNIT:
  67. function = MEDIA_ENT_F_VID_MUX;
  68. break;
  69. case UVC_VC_PROCESSING_UNIT:
  70. case UVC_VC_EXTENSION_UNIT:
  71. /* For lack of a better option. */
  72. function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
  73. break;
  74. case UVC_COMPOSITE_CONNECTOR:
  75. case UVC_COMPONENT_CONNECTOR:
  76. function = MEDIA_ENT_F_CONN_COMPOSITE;
  77. break;
  78. case UVC_SVIDEO_CONNECTOR:
  79. function = MEDIA_ENT_F_CONN_SVIDEO;
  80. break;
  81. case UVC_ITT_CAMERA:
  82. function = MEDIA_ENT_F_CAM_SENSOR;
  83. break;
  84. case UVC_TT_VENDOR_SPECIFIC:
  85. case UVC_ITT_VENDOR_SPECIFIC:
  86. case UVC_ITT_MEDIA_TRANSPORT_INPUT:
  87. case UVC_OTT_VENDOR_SPECIFIC:
  88. case UVC_OTT_DISPLAY:
  89. case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
  90. case UVC_EXTERNAL_VENDOR_SPECIFIC:
  91. case UVC_EXT_GPIO_UNIT:
  92. default:
  93. function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
  94. break;
  95. }
  96. entity->subdev.entity.function = function;
  97. ret = media_entity_pads_init(&entity->subdev.entity,
  98. entity->num_pads, entity->pads);
  99. if (ret < 0)
  100. return ret;
  101. ret = v4l2_device_register_subdev(&chain->dev->vdev,
  102. &entity->subdev);
  103. } else if (entity->vdev != NULL) {
  104. ret = media_entity_pads_init(&entity->vdev->entity,
  105. entity->num_pads, entity->pads);
  106. if (entity->flags & UVC_ENTITY_FLAG_DEFAULT)
  107. entity->vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
  108. } else
  109. ret = 0;
  110. return ret;
  111. }
  112. int uvc_mc_register_entities(struct uvc_video_chain *chain)
  113. {
  114. struct uvc_entity *entity;
  115. int ret;
  116. list_for_each_entry(entity, &chain->entities, chain) {
  117. ret = uvc_mc_init_entity(chain, entity);
  118. if (ret < 0) {
  119. dev_info(&chain->dev->udev->dev,
  120. "Failed to initialize entity for entity %u\n",
  121. entity->id);
  122. return ret;
  123. }
  124. }
  125. list_for_each_entry(entity, &chain->entities, chain) {
  126. ret = uvc_mc_create_links(chain, entity);
  127. if (ret < 0) {
  128. dev_info(&chain->dev->udev->dev,
  129. "Failed to create links for entity %u\n",
  130. entity->id);
  131. return ret;
  132. }
  133. }
  134. return 0;
  135. }