]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/meta-ti-glsdk.git/blob - recipes-bsp/linux/linux-omap/media/0018-v4l-Make-video_device-inherit-from-media_entity.patch
linux-omap 2.6.37: sync with OE .dev
[glsdk/meta-ti-glsdk.git] / recipes-bsp / linux / linux-omap / media / 0018-v4l-Make-video_device-inherit-from-media_entity.patch
1 From e31cb57c733341b49256a47f086fa4cc1c1c56ac Mon Sep 17 00:00:00 2001
2 From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
3 Date: Wed, 9 Dec 2009 12:40:10 +0100
4 Subject: [PATCH 18/43] v4l: Make video_device inherit from media_entity
6 V4L2 devices are media entities. As such they need to inherit from
7 (include) the media_entity structure.
9 When registering/unregistering the device, the media entity is
10 automatically registered/unregistered. The entity is acquired on device
11 open and released on device close.
13 Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
14 Signed-off-by: Sakari Ailus <sakari.ailus@maxwell.research.nokia.com>
15 ---
16  Documentation/video4linux/v4l2-framework.txt |   38 ++++++++++++++++++--
17  drivers/media/video/v4l2-dev.c               |   49 +++++++++++++++++++++++--
18  include/media/v4l2-dev.h                     |    7 ++++
19  3 files changed, 87 insertions(+), 7 deletions(-)
21 diff --git a/Documentation/video4linux/v4l2-framework.txt b/Documentation/video4linux/v4l2-framework.txt
22 index aeb2a22..f231bc2 100644
23 --- a/Documentation/video4linux/v4l2-framework.txt
24 +++ b/Documentation/video4linux/v4l2-framework.txt
25 @@ -71,6 +71,10 @@ sub-device instances, the video_device struct stores V4L2 device node data
26  and in the future a v4l2_fh struct will keep track of filehandle instances
27  (this is not yet implemented).
28  
29 +The V4L2 framework also optionally integrates with the media framework. If a
30 +driver sets the struct v4l2_device mdev field, sub-devices and video nodes
31 +will automatically appear in the media framework as entities.
32 +
33  
34  struct v4l2_device
35  ------------------
36 @@ -84,11 +88,14 @@ You must register the device instance:
37         v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev);
38  
39  Registration will initialize the v4l2_device struct. If the dev->driver_data
40 -field is NULL, it will be linked to v4l2_dev. Drivers that use the media
41 -device framework in addition to the V4L2 framework need to set
42 +field is NULL, it will be linked to v4l2_dev.
43 +
44 +Drivers that want integration with the media device framework need to set
45  dev->driver_data manually to point to the driver-specific device structure
46  that embed the struct v4l2_device instance. This is achieved by a
47 -dev_set_drvdata() call before registering the V4L2 device instance.
48 +dev_set_drvdata() call before registering the V4L2 device instance. They must
49 +also set the struct v4l2_device mdev field to point to a properly initialized
50 +and registered media_device instance.
51  
52  If v4l2_dev->name is empty then it will be set to a value derived from dev
53  (driver name followed by the bus_id, to be precise). If you set it up before
54 @@ -532,6 +539,21 @@ If you use v4l2_ioctl_ops, then you should set either .unlocked_ioctl or
55  The v4l2_file_operations struct is a subset of file_operations. The main
56  difference is that the inode argument is omitted since it is never used.
57  
58 +If integration with the media framework is needed, you must initialize the
59 +media_entity struct embedded in the video_device struct (entity field) by
60 +calling media_entity_init():
61 +
62 +       struct media_pad *pad = &my_vdev->pad;
63 +       int err;
64 +
65 +       err = media_entity_init(&vdev->entity, 1, pad, 0);
66 +
67 +The pads array must have been previously initialized. There is no need to
68 +manually set the struct media_entity type and name fields.
69 +
70 +A reference to the entity will be automatically acquired/released when the
71 +video device is opened/closed.
72 +
73  v4l2_file_operations and locking
74  --------------------------------
75  
76 @@ -561,6 +583,9 @@ for you.
77                 return err;
78         }
79  
80 +If the v4l2_device parent device has a non-NULL mdev field, the video device
81 +entity will be automatically registered with the media device.
82 +
83  Which device is registered depends on the type argument. The following
84  types exist:
85  
86 @@ -636,6 +661,13 @@ release, of course) will return an error as well.
87  When the last user of the video device node exits, then the vdev->release()
88  callback is called and you can do the final cleanup there.
89  
90 +Don't forget to cleanup the media entity associated with the video device if
91 +it has been initialized:
92 +
93 +       media_entity_cleanup(&vdev->entity);
94 +
95 +This can be done from the release callback.
96 +
97  
98  video_device helper functions
99  -----------------------------
100 diff --git a/drivers/media/video/v4l2-dev.c b/drivers/media/video/v4l2-dev.c
101 index f22bd41..f91348f 100644
102 --- a/drivers/media/video/v4l2-dev.c
103 +++ b/drivers/media/video/v4l2-dev.c
104 @@ -303,6 +303,9 @@ static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
105  static int v4l2_open(struct inode *inode, struct file *filp)
106  {
107         struct video_device *vdev;
108 +#if defined(CONFIG_MEDIA_CONTROLLER)
109 +       struct media_entity *entity = NULL;
110 +#endif
111         int ret = 0;
112  
113         /* Check if the video device is available */
114 @@ -316,6 +319,16 @@ static int v4l2_open(struct inode *inode, struct file *filp)
115         /* and increase the device refcount */
116         video_get(vdev);
117         mutex_unlock(&videodev_lock);
118 +#if defined(CONFIG_MEDIA_CONTROLLER)
119 +       if (vdev->v4l2_dev && vdev->v4l2_dev->mdev) {
120 +               entity = media_entity_get(&vdev->entity);
121 +               if (!entity) {
122 +                       ret = -EBUSY;
123 +                       video_put(vdev);
124 +                       return ret;
125 +               }
126 +       }
127 +#endif
128         if (vdev->fops->open) {
129                 if (vdev->lock && mutex_lock_interruptible(vdev->lock)) {
130                         ret = -ERESTARTSYS;
131 @@ -331,8 +344,13 @@ static int v4l2_open(struct inode *inode, struct file *filp)
132  
133  err:
134         /* decrease the refcount in case of an error */
135 -       if (ret)
136 +       if (ret) {
137 +#if defined(CONFIG_MEDIA_CONTROLLER)
138 +               if (vdev->v4l2_dev && vdev->v4l2_dev->mdev)
139 +                       media_entity_put(entity);
140 +#endif
141                 video_put(vdev);
142 +       }
143         return ret;
144  }
145  
146 @@ -349,7 +367,10 @@ static int v4l2_release(struct inode *inode, struct file *filp)
147                 if (vdev->lock)
148                         mutex_unlock(vdev->lock);
149         }
151 +#if defined(CONFIG_MEDIA_CONTROLLER)
152 +       if (vdev->v4l2_dev && vdev->v4l2_dev->mdev)
153 +               media_entity_put(&vdev->entity);
154 +#endif
155         /* decrease the refcount unconditionally since the release()
156            return value is ignored. */
157         video_put(vdev);
158 @@ -586,12 +607,27 @@ int __video_register_device(struct video_device *vdev, int type, int nr,
159         if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
160                 printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__,
161                         name_base, nr, video_device_node_name(vdev));
163 -       /* Part 5: Activate this minor. The char device can now be used. */
164 +#if defined(CONFIG_MEDIA_CONTROLLER)
165 +       /* Part 5: Register the entity. */
166 +       if (vdev->v4l2_dev && vdev->v4l2_dev->mdev) {
167 +               vdev->entity.type = MEDIA_ENT_T_DEVNODE_V4L;
168 +               vdev->entity.name = vdev->name;
169 +               vdev->entity.v4l.major = VIDEO_MAJOR;
170 +               vdev->entity.v4l.minor = vdev->minor;
171 +               ret = media_device_register_entity(vdev->v4l2_dev->mdev,
172 +                       &vdev->entity);
173 +               if (ret < 0)
174 +                       printk(KERN_WARNING
175 +                              "%s: media_device_register_entity failed\n",
176 +                              __func__);
177 +       }
178 +#endif
179 +       /* Part 6: Activate this minor. The char device can now be used. */
180         set_bit(V4L2_FL_REGISTERED, &vdev->flags);
181         mutex_lock(&videodev_lock);
182         video_device[vdev->minor] = vdev;
183         mutex_unlock(&videodev_lock);
185         return 0;
186  
187  cleanup:
188 @@ -619,6 +655,11 @@ void video_unregister_device(struct video_device *vdev)
189         if (!vdev || !video_is_registered(vdev))
190                 return;
191  
192 +#if defined(CONFIG_MEDIA_CONTROLLER)
193 +       if (vdev->v4l2_dev && vdev->v4l2_dev->mdev)
194 +               media_device_unregister_entity(&vdev->entity);
195 +#endif
197         mutex_lock(&videodev_lock);
198         /* This must be in a critical section to prevent a race with v4l2_open.
199          * Once this bit has been cleared video_get may never be called again.
200 diff --git a/include/media/v4l2-dev.h b/include/media/v4l2-dev.h
201 index 4fe6831..51b2c51 100644
202 --- a/include/media/v4l2-dev.h
203 +++ b/include/media/v4l2-dev.h
204 @@ -16,6 +16,8 @@
205  #include <linux/mutex.h>
206  #include <linux/videodev2.h>
207  
208 +#include <media/media-entity.h>
210  #define VIDEO_MAJOR    81
211  
212  #define VFL_TYPE_GRABBER       0
213 @@ -55,6 +57,9 @@ struct v4l2_file_operations {
214  
215  struct video_device
216  {
217 +#if defined(CONFIG_MEDIA_CONTROLLER)
218 +       struct media_entity entity;
219 +#endif
220         /* device ops */
221         const struct v4l2_file_operations *fops;
222  
223 @@ -100,6 +105,8 @@ struct video_device
224         struct mutex *lock;
225  };
226  
227 +#define media_entity_to_video_device(entity) \
228 +       container_of(entity, struct video_device, entity)
229  /* dev to video-device */
230  #define to_video_device(cd) container_of(cd, struct video_device, dev)
231  
232 -- 
233 1.6.6.1