[media] uvcvideo: Register subdevices for each entity
[pandora-kernel.git] / drivers / media / video / uvc / uvc_entity.c
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
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
26 static 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;
33         int ret;
34
35         for (i = 0; i < entity->num_pads; ++i) {
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                 remote_pad = remote->num_pads - 1;
44                 ret = media_entity_create_link(&remote->subdev.entity,
45                                 remote_pad, &entity->subdev.entity, i, flags);
46                 if (ret < 0)
47                         return ret;
48         }
49
50         return v4l2_device_register_subdev(&chain->dev->vdev, &entity->subdev);
51 }
52
53 static struct v4l2_subdev_ops uvc_subdev_ops = {
54 };
55
56 void uvc_mc_cleanup_entity(struct uvc_entity *entity)
57 {
58         media_entity_cleanup(&entity->subdev.entity);
59 }
60
61 static int uvc_mc_init_entity(struct uvc_entity *entity)
62 {
63         v4l2_subdev_init(&entity->subdev, &uvc_subdev_ops);
64         strlcpy(entity->subdev.name, entity->name, sizeof(entity->subdev.name));
65
66         return media_entity_init(&entity->subdev.entity, entity->num_pads,
67                                  entity->pads, 0);
68 }
69
70 int uvc_mc_register_entities(struct uvc_video_chain *chain)
71 {
72         struct uvc_entity *entity;
73         int ret;
74
75         list_for_each_entry(entity, &chain->entities, chain) {
76                 ret = uvc_mc_init_entity(entity);
77                 if (ret < 0) {
78                         uvc_printk(KERN_INFO, "Failed to initialize entity for "
79                                    "entity %u\n", entity->id);
80                         return ret;
81                 }
82         }
83
84         list_for_each_entry(entity, &chain->entities, chain) {
85                 ret = uvc_mc_register_entity(chain, entity);
86                 if (ret < 0) {
87                         uvc_printk(KERN_INFO, "Failed to register entity for "
88                                    "entity %u\n", entity->id);
89                         return ret;
90                 }
91         }
92
93         return 0;
94 }