Linux-libre 5.4.39-gnu
[librecmc/linux-libre.git] / drivers / media / usb / uvc / uvc_entity.c
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 (laurent.pinchart@ideasonboard.com)
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/list.h>
11 #include <linux/videodev2.h>
12
13 #include <media/v4l2-common.h>
14
15 #include "uvcvideo.h"
16
17 static int uvc_mc_create_links(struct uvc_video_chain *chain,
18                                     struct uvc_entity *entity)
19 {
20         const u32 flags = MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE;
21         struct media_entity *sink;
22         unsigned int i;
23         int ret;
24
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
31         for (i = 0; i < entity->num_pads; ++i) {
32                 struct media_entity *source;
33                 struct uvc_entity *remote;
34                 u8 remote_pad;
35
36                 if (!(entity->pads[i].flags & MEDIA_PAD_FL_SINK))
37                         continue;
38
39                 remote = uvc_entity_by_id(chain->dev, entity->baSourceID[i]);
40                 if (remote == NULL)
41                         return -EINVAL;
42
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;
48
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;
54         }
55
56         return 0;
57 }
58
59 static const struct v4l2_subdev_ops uvc_subdev_ops = {
60 };
61
62 void uvc_mc_cleanup_entity(struct uvc_entity *entity)
63 {
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);
68 }
69
70 static int uvc_mc_init_entity(struct uvc_video_chain *chain,
71                               struct uvc_entity *entity)
72 {
73         int ret;
74
75         if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING) {
76                 v4l2_subdev_init(&entity->subdev, &uvc_subdev_ops);
77                 strscpy(entity->subdev.name, entity->name,
78                         sizeof(entity->subdev.name));
79
80                 ret = media_entity_pads_init(&entity->subdev.entity,
81                                         entity->num_pads, entity->pads);
82
83                 if (ret < 0)
84                         return ret;
85
86                 ret = v4l2_device_register_subdev(&chain->dev->vdev,
87                                                   &entity->subdev);
88         } else if (entity->vdev != NULL) {
89                 ret = media_entity_pads_init(&entity->vdev->entity,
90                                         entity->num_pads, entity->pads);
91                 if (entity->flags & UVC_ENTITY_FLAG_DEFAULT)
92                         entity->vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
93         } else
94                 ret = 0;
95
96         return ret;
97 }
98
99 int uvc_mc_register_entities(struct uvc_video_chain *chain)
100 {
101         struct uvc_entity *entity;
102         int ret;
103
104         list_for_each_entry(entity, &chain->entities, chain) {
105                 ret = uvc_mc_init_entity(chain, entity);
106                 if (ret < 0) {
107                         uvc_printk(KERN_INFO, "Failed to initialize entity for "
108                                    "entity %u\n", entity->id);
109                         return ret;
110                 }
111         }
112
113         list_for_each_entry(entity, &chain->entities, chain) {
114                 ret = uvc_mc_create_links(chain, entity);
115                 if (ret < 0) {
116                         uvc_printk(KERN_INFO, "Failed to create links for "
117                                    "entity %u\n", entity->id);
118                         return ret;
119                 }
120         }
121
122         return 0;
123 }