[media] v4l2-events/fh: merge v4l2_events into v4l2_fh
[pandora-kernel.git] / drivers / media / video / v4l2-subdev.c
1 /*
2  * V4L2 sub-device
3  *
4  * Copyright (C) 2010 Nokia Corporation
5  *
6  * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7  *          Sakari Ailus <sakari.ailus@iki.fi>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <linux/ioctl.h>
24 #include <linux/slab.h>
25 #include <linux/types.h>
26 #include <linux/videodev2.h>
27
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-fh.h>
32 #include <media/v4l2-event.h>
33
34 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
35 {
36 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
37         /* Allocate try format and crop in the same memory block */
38         fh->try_fmt = kzalloc((sizeof(*fh->try_fmt) + sizeof(*fh->try_crop))
39                               * sd->entity.num_pads, GFP_KERNEL);
40         if (fh->try_fmt == NULL)
41                 return -ENOMEM;
42
43         fh->try_crop = (struct v4l2_rect *)
44                 (fh->try_fmt + sd->entity.num_pads);
45 #endif
46         return 0;
47 }
48
49 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
50 {
51 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
52         kfree(fh->try_fmt);
53         fh->try_fmt = NULL;
54         fh->try_crop = NULL;
55 #endif
56 }
57
58 static int subdev_open(struct file *file)
59 {
60         struct video_device *vdev = video_devdata(file);
61         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
62         struct v4l2_subdev_fh *subdev_fh;
63 #if defined(CONFIG_MEDIA_CONTROLLER)
64         struct media_entity *entity = NULL;
65 #endif
66         int ret;
67
68         subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
69         if (subdev_fh == NULL)
70                 return -ENOMEM;
71
72         ret = subdev_fh_init(subdev_fh, sd);
73         if (ret) {
74                 kfree(subdev_fh);
75                 return ret;
76         }
77
78         v4l2_fh_init(&subdev_fh->vfh, vdev);
79
80         if (sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) {
81                 ret = v4l2_event_alloc(&subdev_fh->vfh, sd->nevents);
82                 if (ret)
83                         goto err;
84         }
85
86         v4l2_fh_add(&subdev_fh->vfh);
87         file->private_data = &subdev_fh->vfh;
88 #if defined(CONFIG_MEDIA_CONTROLLER)
89         if (sd->v4l2_dev->mdev) {
90                 entity = media_entity_get(&sd->entity);
91                 if (!entity) {
92                         ret = -EBUSY;
93                         goto err;
94                 }
95         }
96 #endif
97
98         if (sd->internal_ops && sd->internal_ops->open) {
99                 ret = sd->internal_ops->open(sd, subdev_fh);
100                 if (ret < 0)
101                         goto err;
102         }
103
104         return 0;
105
106 err:
107 #if defined(CONFIG_MEDIA_CONTROLLER)
108         if (entity)
109                 media_entity_put(entity);
110 #endif
111         v4l2_fh_del(&subdev_fh->vfh);
112         v4l2_fh_exit(&subdev_fh->vfh);
113         subdev_fh_free(subdev_fh);
114         kfree(subdev_fh);
115
116         return ret;
117 }
118
119 static int subdev_close(struct file *file)
120 {
121         struct video_device *vdev = video_devdata(file);
122         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
123         struct v4l2_fh *vfh = file->private_data;
124         struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
125
126         if (sd->internal_ops && sd->internal_ops->close)
127                 sd->internal_ops->close(sd, subdev_fh);
128 #if defined(CONFIG_MEDIA_CONTROLLER)
129         if (sd->v4l2_dev->mdev)
130                 media_entity_put(&sd->entity);
131 #endif
132         v4l2_fh_del(vfh);
133         v4l2_fh_exit(vfh);
134         subdev_fh_free(subdev_fh);
135         kfree(subdev_fh);
136         file->private_data = NULL;
137
138         return 0;
139 }
140
141 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
142 {
143         struct video_device *vdev = video_devdata(file);
144         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
145         struct v4l2_fh *vfh = file->private_data;
146 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
147         struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
148 #endif
149
150         switch (cmd) {
151         case VIDIOC_QUERYCTRL:
152                 return v4l2_queryctrl(vfh->ctrl_handler, arg);
153
154         case VIDIOC_QUERYMENU:
155                 return v4l2_querymenu(vfh->ctrl_handler, arg);
156
157         case VIDIOC_G_CTRL:
158                 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
159
160         case VIDIOC_S_CTRL:
161                 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
162
163         case VIDIOC_G_EXT_CTRLS:
164                 return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg);
165
166         case VIDIOC_S_EXT_CTRLS:
167                 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg);
168
169         case VIDIOC_TRY_EXT_CTRLS:
170                 return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg);
171
172         case VIDIOC_DQEVENT:
173                 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
174                         return -ENOIOCTLCMD;
175
176                 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
177
178         case VIDIOC_SUBSCRIBE_EVENT:
179                 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
180
181         case VIDIOC_UNSUBSCRIBE_EVENT:
182                 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
183 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
184         case VIDIOC_SUBDEV_G_FMT: {
185                 struct v4l2_subdev_format *format = arg;
186
187                 if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
188                     format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
189                         return -EINVAL;
190
191                 if (format->pad >= sd->entity.num_pads)
192                         return -EINVAL;
193
194                 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh, format);
195         }
196
197         case VIDIOC_SUBDEV_S_FMT: {
198                 struct v4l2_subdev_format *format = arg;
199
200                 if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
201                     format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
202                         return -EINVAL;
203
204                 if (format->pad >= sd->entity.num_pads)
205                         return -EINVAL;
206
207                 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format);
208         }
209
210         case VIDIOC_SUBDEV_G_CROP: {
211                 struct v4l2_subdev_crop *crop = arg;
212
213                 if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
214                     crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
215                         return -EINVAL;
216
217                 if (crop->pad >= sd->entity.num_pads)
218                         return -EINVAL;
219
220                 return v4l2_subdev_call(sd, pad, get_crop, subdev_fh, crop);
221         }
222
223         case VIDIOC_SUBDEV_S_CROP: {
224                 struct v4l2_subdev_crop *crop = arg;
225
226                 if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
227                     crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
228                         return -EINVAL;
229
230                 if (crop->pad >= sd->entity.num_pads)
231                         return -EINVAL;
232
233                 return v4l2_subdev_call(sd, pad, set_crop, subdev_fh, crop);
234         }
235
236         case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
237                 struct v4l2_subdev_mbus_code_enum *code = arg;
238
239                 if (code->pad >= sd->entity.num_pads)
240                         return -EINVAL;
241
242                 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh,
243                                         code);
244         }
245
246         case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
247                 struct v4l2_subdev_frame_size_enum *fse = arg;
248
249                 if (fse->pad >= sd->entity.num_pads)
250                         return -EINVAL;
251
252                 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh,
253                                         fse);
254         }
255
256         case VIDIOC_SUBDEV_G_FRAME_INTERVAL:
257                 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
258
259         case VIDIOC_SUBDEV_S_FRAME_INTERVAL:
260                 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
261
262         case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
263                 struct v4l2_subdev_frame_interval_enum *fie = arg;
264
265                 if (fie->pad >= sd->entity.num_pads)
266                         return -EINVAL;
267
268                 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh,
269                                         fie);
270         }
271 #endif
272         default:
273                 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
274         }
275
276         return 0;
277 }
278
279 static long subdev_ioctl(struct file *file, unsigned int cmd,
280         unsigned long arg)
281 {
282         return video_usercopy(file, cmd, arg, subdev_do_ioctl);
283 }
284
285 static unsigned int subdev_poll(struct file *file, poll_table *wait)
286 {
287         struct video_device *vdev = video_devdata(file);
288         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
289         struct v4l2_fh *fh = file->private_data;
290
291         if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
292                 return POLLERR;
293
294         poll_wait(file, &fh->wait, wait);
295
296         if (v4l2_event_pending(fh))
297                 return POLLPRI;
298
299         return 0;
300 }
301
302 const struct v4l2_file_operations v4l2_subdev_fops = {
303         .owner = THIS_MODULE,
304         .open = subdev_open,
305         .unlocked_ioctl = subdev_ioctl,
306         .release = subdev_close,
307         .poll = subdev_poll,
308 };
309
310 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
311 {
312         INIT_LIST_HEAD(&sd->list);
313         BUG_ON(!ops);
314         sd->ops = ops;
315         sd->v4l2_dev = NULL;
316         sd->flags = 0;
317         sd->name[0] = '\0';
318         sd->grp_id = 0;
319         sd->dev_priv = NULL;
320         sd->host_priv = NULL;
321 #if defined(CONFIG_MEDIA_CONTROLLER)
322         sd->entity.name = sd->name;
323         sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
324 #endif
325 }
326 EXPORT_SYMBOL(v4l2_subdev_init);