drm/nouveau: consume the return of large GSP message
[drm/drm-misc.git] / drivers / media / usb / uvc / uvc_entity.c
blobcc68dd24eb42dce5b2846ca52a8dfa499c8aed96
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * uvc_entity.c -- USB Video Class driver
5 * Copyright (C) 2005-2011
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 */
9 #include <linux/kernel.h>
10 #include <linux/list.h>
11 #include <linux/videodev2.h>
13 #include <media/v4l2-common.h>
15 #include "uvcvideo.h"
17 static int uvc_mc_create_links(struct uvc_video_chain *chain,
18 struct uvc_entity *entity)
20 const u32 flags = MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE;
21 struct media_entity *sink;
22 unsigned int i;
23 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;
31 for (i = 0; i < entity->num_pads; ++i) {
32 struct media_entity *source;
33 struct uvc_entity *remote;
34 u8 remote_pad;
36 if (!(entity->pads[i].flags & MEDIA_PAD_FL_SINK))
37 continue;
39 remote = uvc_entity_by_id(chain->dev, entity->baSourceID[i]);
40 if (remote == NULL || remote->num_pads == 0)
41 return -EINVAL;
43 source = (UVC_ENTITY_TYPE(remote) == UVC_TT_STREAMING)
44 ? (remote->vdev ? &remote->vdev->entity : NULL)
45 : &remote->subdev.entity;
46 if (source == NULL)
47 continue;
49 remote_pad = remote->num_pads - 1;
50 ret = media_create_pad_link(source, remote_pad,
51 sink, i, flags);
52 if (ret < 0)
53 return ret;
56 return 0;
59 static const struct v4l2_subdev_ops uvc_subdev_ops = {
62 void uvc_mc_cleanup_entity(struct uvc_entity *entity)
64 if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING)
65 media_entity_cleanup(&entity->subdev.entity);
66 else if (entity->vdev != NULL)
67 media_entity_cleanup(&entity->vdev->entity);
70 static int uvc_mc_init_entity(struct uvc_video_chain *chain,
71 struct uvc_entity *entity)
73 int ret;
75 if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING) {
76 u32 function;
78 v4l2_subdev_init(&entity->subdev, &uvc_subdev_ops);
79 strscpy(entity->subdev.name, entity->name,
80 sizeof(entity->subdev.name));
82 switch (UVC_ENTITY_TYPE(entity)) {
83 case UVC_VC_SELECTOR_UNIT:
84 function = MEDIA_ENT_F_VID_MUX;
85 break;
86 case UVC_VC_PROCESSING_UNIT:
87 case UVC_VC_EXTENSION_UNIT:
88 /* For lack of a better option. */
89 function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
90 break;
91 case UVC_COMPOSITE_CONNECTOR:
92 case UVC_COMPONENT_CONNECTOR:
93 function = MEDIA_ENT_F_CONN_COMPOSITE;
94 break;
95 case UVC_SVIDEO_CONNECTOR:
96 function = MEDIA_ENT_F_CONN_SVIDEO;
97 break;
98 case UVC_ITT_CAMERA:
99 function = MEDIA_ENT_F_CAM_SENSOR;
100 break;
101 case UVC_TT_VENDOR_SPECIFIC:
102 case UVC_ITT_VENDOR_SPECIFIC:
103 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
104 case UVC_OTT_VENDOR_SPECIFIC:
105 case UVC_OTT_DISPLAY:
106 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
107 case UVC_EXTERNAL_VENDOR_SPECIFIC:
108 case UVC_EXT_GPIO_UNIT:
109 default:
110 function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
111 break;
114 entity->subdev.entity.function = function;
116 ret = media_entity_pads_init(&entity->subdev.entity,
117 entity->num_pads, entity->pads);
119 if (ret < 0)
120 return ret;
122 ret = v4l2_device_register_subdev(&chain->dev->vdev,
123 &entity->subdev);
124 } else if (entity->vdev != NULL) {
125 ret = media_entity_pads_init(&entity->vdev->entity,
126 entity->num_pads, entity->pads);
127 if (entity->flags & UVC_ENTITY_FLAG_DEFAULT)
128 entity->vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
129 } else
130 ret = 0;
132 return ret;
135 int uvc_mc_register_entities(struct uvc_video_chain *chain)
137 struct uvc_entity *entity;
138 int ret;
140 list_for_each_entry(entity, &chain->entities, chain) {
141 ret = uvc_mc_init_entity(chain, entity);
142 if (ret < 0) {
143 dev_info(&chain->dev->udev->dev,
144 "Failed to initialize entity for entity %u\n",
145 entity->id);
146 return ret;
150 list_for_each_entry(entity, &chain->entities, chain) {
151 ret = uvc_mc_create_links(chain, entity);
152 if (ret < 0) {
153 dev_info(&chain->dev->udev->dev,
154 "Failed to create links for entity %u\n",
155 entity->id);
156 return ret;
160 return 0;