4 * Copyright (C) 2010 Nokia Corporation
6 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
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.
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.
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
23 #include <linux/ioctl.h>
24 #include <linux/slab.h>
25 #include <linux/types.h>
26 #include <linux/videodev2.h>
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>
34 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
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)
43 fh->try_crop = (struct v4l2_rect *)
44 (fh->try_fmt + sd->entity.num_pads);
49 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
51 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
58 static int subdev_open(struct file *file)
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;
68 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
69 if (subdev_fh == NULL)
72 ret = subdev_fh_init(subdev_fh, sd);
78 ret = v4l2_fh_init(&subdev_fh->vfh, vdev);
82 if (sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) {
83 ret = v4l2_event_init(&subdev_fh->vfh);
87 ret = v4l2_event_alloc(&subdev_fh->vfh, sd->nevents);
92 v4l2_fh_add(&subdev_fh->vfh);
93 file->private_data = &subdev_fh->vfh;
94 #if defined(CONFIG_MEDIA_CONTROLLER)
95 if (sd->v4l2_dev->mdev) {
96 entity = media_entity_get(&sd->entity);
107 v4l2_fh_del(&subdev_fh->vfh);
108 v4l2_fh_exit(&subdev_fh->vfh);
109 subdev_fh_free(subdev_fh);
115 static int subdev_close(struct file *file)
117 #if defined(CONFIG_MEDIA_CONTROLLER)
118 struct video_device *vdev = video_devdata(file);
119 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
121 struct v4l2_fh *vfh = file->private_data;
122 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
124 #if defined(CONFIG_MEDIA_CONTROLLER)
125 if (sd->v4l2_dev->mdev)
126 media_entity_put(&sd->entity);
130 subdev_fh_free(subdev_fh);
132 file->private_data = NULL;
137 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
139 struct video_device *vdev = video_devdata(file);
140 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
141 struct v4l2_fh *vfh = file->private_data;
144 case VIDIOC_QUERYCTRL:
145 return v4l2_subdev_queryctrl(sd, arg);
147 case VIDIOC_QUERYMENU:
148 return v4l2_subdev_querymenu(sd, arg);
151 return v4l2_subdev_g_ctrl(sd, arg);
154 return v4l2_subdev_s_ctrl(sd, arg);
156 case VIDIOC_G_EXT_CTRLS:
157 return v4l2_subdev_g_ext_ctrls(sd, arg);
159 case VIDIOC_S_EXT_CTRLS:
160 return v4l2_subdev_s_ext_ctrls(sd, arg);
162 case VIDIOC_TRY_EXT_CTRLS:
163 return v4l2_subdev_try_ext_ctrls(sd, arg);
166 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
169 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
171 case VIDIOC_SUBSCRIBE_EVENT:
172 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
174 case VIDIOC_UNSUBSCRIBE_EVENT:
175 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
184 static long subdev_ioctl(struct file *file, unsigned int cmd,
187 return video_usercopy(file, cmd, arg, subdev_do_ioctl);
190 static unsigned int subdev_poll(struct file *file, poll_table *wait)
192 struct video_device *vdev = video_devdata(file);
193 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
194 struct v4l2_fh *fh = file->private_data;
196 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
199 poll_wait(file, &fh->events->wait, wait);
201 if (v4l2_event_pending(fh))
207 const struct v4l2_file_operations v4l2_subdev_fops = {
208 .owner = THIS_MODULE,
210 .unlocked_ioctl = subdev_ioctl,
211 .release = subdev_close,
215 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
217 INIT_LIST_HEAD(&sd->list);
225 sd->host_priv = NULL;
226 #if defined(CONFIG_MEDIA_CONTROLLER)
227 sd->entity.name = sd->name;
228 sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
231 EXPORT_SYMBOL(v4l2_subdev_init);