blob: c3ab0c813be249d4957fa93e83cfac0df567b703 [file] [log] [blame]
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -03001/*
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
14#include <linux/kernel.h>
15#include <linux/list.h>
16#include <linux/videodev2.h>
17
18#include <media/v4l2-common.h>
19
20#include "uvcvideo.h"
21
22/* ------------------------------------------------------------------------
23 * Video subdevices registration and unregistration
24 */
25
26static int uvc_mc_register_entity(struct uvc_video_chain *chain,
27 struct uvc_entity *entity)
28{
29 const u32 flags = MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE;
30 struct uvc_entity *remote;
31 unsigned int i;
32 u8 remote_pad;
Mauro Carvalho Chehab76df01e2011-06-01 14:44:41 -030033 int ret = 0;
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -030034
35 for (i = 0; i < entity->num_pads; ++i) {
Laurent Pinchart8a65a942010-01-21 08:56:19 -030036 struct media_entity *source;
37 struct media_entity *sink;
38
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -030039 if (!(entity->pads[i].flags & MEDIA_PAD_FL_SINK))
40 continue;
41
42 remote = uvc_entity_by_id(chain->dev, entity->baSourceID[i]);
43 if (remote == NULL)
44 return -EINVAL;
45
Laurent Pinchart8a65a942010-01-21 08:56:19 -030046 source = (UVC_ENTITY_TYPE(remote) == UVC_TT_STREAMING)
47 ? &remote->vdev->entity : &remote->subdev.entity;
48 sink = (UVC_ENTITY_TYPE(entity) == UVC_TT_STREAMING)
49 ? &entity->vdev->entity : &entity->subdev.entity;
50
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -030051 remote_pad = remote->num_pads - 1;
Laurent Pinchart8a65a942010-01-21 08:56:19 -030052 ret = media_entity_create_link(source, remote_pad,
53 sink, i, flags);
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -030054 if (ret < 0)
55 return ret;
56 }
57
Laurent Pinchart8a65a942010-01-21 08:56:19 -030058 if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING)
59 ret = v4l2_device_register_subdev(&chain->dev->vdev,
60 &entity->subdev);
61
62 return ret;
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -030063}
64
65static struct v4l2_subdev_ops uvc_subdev_ops = {
66};
67
68void uvc_mc_cleanup_entity(struct uvc_entity *entity)
69{
Laurent Pinchart8a65a942010-01-21 08:56:19 -030070 if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING)
71 media_entity_cleanup(&entity->subdev.entity);
72 else if (entity->vdev != NULL)
73 media_entity_cleanup(&entity->vdev->entity);
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -030074}
75
76static int uvc_mc_init_entity(struct uvc_entity *entity)
77{
Laurent Pinchart8a65a942010-01-21 08:56:19 -030078 int ret;
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -030079
Laurent Pinchart8a65a942010-01-21 08:56:19 -030080 if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING) {
81 v4l2_subdev_init(&entity->subdev, &uvc_subdev_ops);
82 strlcpy(entity->subdev.name, entity->name,
83 sizeof(entity->subdev.name));
84
85 ret = media_entity_init(&entity->subdev.entity,
86 entity->num_pads, entity->pads, 0);
87 } else
88 ret = media_entity_init(&entity->vdev->entity,
89 entity->num_pads, entity->pads, 0);
90
91 return ret;
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -030092}
93
94int uvc_mc_register_entities(struct uvc_video_chain *chain)
95{
96 struct uvc_entity *entity;
97 int ret;
98
99 list_for_each_entry(entity, &chain->entities, chain) {
100 ret = uvc_mc_init_entity(entity);
101 if (ret < 0) {
102 uvc_printk(KERN_INFO, "Failed to initialize entity for "
103 "entity %u\n", entity->id);
104 return ret;
105 }
106 }
107
108 list_for_each_entry(entity, &chain->entities, chain) {
109 ret = uvc_mc_register_entity(chain, entity);
110 if (ret < 0) {
111 uvc_printk(KERN_INFO, "Failed to register entity for "
112 "entity %u\n", entity->id);
113 return ret;
114 }
115 }
116
117 return 0;
118}