blob: bc763db385df4af8d2bea5f5561a13cb0883b56d [file] [log] [blame]
Laurent Pinchart2096a5d2009-12-09 08:38:49 -03001/*
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -03002 * V4L2 sub-device
Laurent Pinchart2096a5d2009-12-09 08:38:49 -03003 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -03004 * Copyright (C) 2010 Nokia Corporation
Laurent Pinchart2096a5d2009-12-09 08:38:49 -03005 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -03006 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -03008 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -03009 * 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.
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030012 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -030013 * 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.
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030017 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -030018 * 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
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030021 */
22
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030023#include <linux/ioctl.h>
Sakari Ailus02adb1c2010-03-03 12:49:38 -030024#include <linux/slab.h>
25#include <linux/types.h>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030026#include <linux/videodev2.h>
27
Laurent Pinchartea8aa432009-12-09 08:39:54 -030028#include <media/v4l2-ctrls.h>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030029#include <media/v4l2-device.h>
30#include <media/v4l2-ioctl.h>
Sakari Ailus02adb1c2010-03-03 12:49:38 -030031#include <media/v4l2-fh.h>
32#include <media/v4l2-event.h>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030033
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030034static 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
49static 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
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030058static int subdev_open(struct file *file)
59{
Sakari Ailus02adb1c2010-03-03 12:49:38 -030060 struct video_device *vdev = video_devdata(file);
61 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030062 struct v4l2_subdev_fh *subdev_fh;
Laurent Pinchart61f5db52009-12-09 08:40:08 -030063#if defined(CONFIG_MEDIA_CONTROLLER)
Laurent Pinchartf0beea82010-08-01 19:05:09 -030064 struct media_entity *entity = NULL;
Laurent Pinchart61f5db52009-12-09 08:40:08 -030065#endif
Sakari Ailus02adb1c2010-03-03 12:49:38 -030066 int ret;
67
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030068 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
69 if (subdev_fh == NULL)
70 return -ENOMEM;
Sakari Ailus02adb1c2010-03-03 12:49:38 -030071
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030072 ret = subdev_fh_init(subdev_fh, sd);
73 if (ret) {
74 kfree(subdev_fh);
75 return ret;
Sakari Ailus02adb1c2010-03-03 12:49:38 -030076 }
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030077
78 ret = v4l2_fh_init(&subdev_fh->vfh, vdev);
79 if (ret)
80 goto err;
81
82 if (sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) {
83 ret = v4l2_event_init(&subdev_fh->vfh);
84 if (ret)
85 goto err;
86
87 ret = v4l2_event_alloc(&subdev_fh->vfh, sd->nevents);
88 if (ret)
89 goto err;
90 }
91
92 v4l2_fh_add(&subdev_fh->vfh);
93 file->private_data = &subdev_fh->vfh;
Laurent Pinchart61f5db52009-12-09 08:40:08 -030094#if defined(CONFIG_MEDIA_CONTROLLER)
95 if (sd->v4l2_dev->mdev) {
96 entity = media_entity_get(&sd->entity);
97 if (!entity) {
98 ret = -EBUSY;
99 goto err;
100 }
101 }
102#endif
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300103
Laurent Pinchartf0beea82010-08-01 19:05:09 -0300104 if (sd->internal_ops && sd->internal_ops->open) {
105 ret = sd->internal_ops->open(sd, subdev_fh);
106 if (ret < 0)
107 goto err;
108 }
109
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300110 return 0;
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300111
112err:
Laurent Pinchartf0beea82010-08-01 19:05:09 -0300113#if defined(CONFIG_MEDIA_CONTROLLER)
114 if (entity)
115 media_entity_put(entity);
116#endif
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300117 v4l2_fh_del(&subdev_fh->vfh);
118 v4l2_fh_exit(&subdev_fh->vfh);
119 subdev_fh_free(subdev_fh);
120 kfree(subdev_fh);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300121
122 return ret;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300123}
124
125static int subdev_close(struct file *file)
126{
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300127 struct video_device *vdev = video_devdata(file);
128 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300129 struct v4l2_fh *vfh = file->private_data;
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300130 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300131
Laurent Pinchartf0beea82010-08-01 19:05:09 -0300132 if (sd->internal_ops && sd->internal_ops->close)
133 sd->internal_ops->close(sd, subdev_fh);
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300134#if defined(CONFIG_MEDIA_CONTROLLER)
135 if (sd->v4l2_dev->mdev)
136 media_entity_put(&sd->entity);
137#endif
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300138 v4l2_fh_del(vfh);
139 v4l2_fh_exit(vfh);
140 subdev_fh_free(subdev_fh);
141 kfree(subdev_fh);
142 file->private_data = NULL;
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300143
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300144 return 0;
145}
146
147static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
148{
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300149 struct video_device *vdev = video_devdata(file);
150 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300151 struct v4l2_fh *vfh = file->private_data;
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300152
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300153 switch (cmd) {
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300154 case VIDIOC_QUERYCTRL:
155 return v4l2_subdev_queryctrl(sd, arg);
156
157 case VIDIOC_QUERYMENU:
158 return v4l2_subdev_querymenu(sd, arg);
159
160 case VIDIOC_G_CTRL:
161 return v4l2_subdev_g_ctrl(sd, arg);
162
163 case VIDIOC_S_CTRL:
164 return v4l2_subdev_s_ctrl(sd, arg);
165
166 case VIDIOC_G_EXT_CTRLS:
167 return v4l2_subdev_g_ext_ctrls(sd, arg);
168
169 case VIDIOC_S_EXT_CTRLS:
170 return v4l2_subdev_s_ext_ctrls(sd, arg);
171
172 case VIDIOC_TRY_EXT_CTRLS:
173 return v4l2_subdev_try_ext_ctrls(sd, arg);
174
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300175 case VIDIOC_DQEVENT:
176 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
177 return -ENOIOCTLCMD;
178
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300179 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300180
181 case VIDIOC_SUBSCRIBE_EVENT:
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300182 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300183
184 case VIDIOC_UNSUBSCRIBE_EVENT:
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300185 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300186
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300187 default:
188 return -ENOIOCTLCMD;
189 }
190
191 return 0;
192}
193
194static long subdev_ioctl(struct file *file, unsigned int cmd,
195 unsigned long arg)
196{
197 return video_usercopy(file, cmd, arg, subdev_do_ioctl);
198}
199
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300200static unsigned int subdev_poll(struct file *file, poll_table *wait)
201{
202 struct video_device *vdev = video_devdata(file);
203 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
204 struct v4l2_fh *fh = file->private_data;
205
206 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
207 return POLLERR;
208
209 poll_wait(file, &fh->events->wait, wait);
210
211 if (v4l2_event_pending(fh))
212 return POLLPRI;
213
214 return 0;
215}
216
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300217const struct v4l2_file_operations v4l2_subdev_fops = {
218 .owner = THIS_MODULE,
219 .open = subdev_open,
220 .unlocked_ioctl = subdev_ioctl,
221 .release = subdev_close,
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300222 .poll = subdev_poll,
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300223};
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300224
225void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
226{
227 INIT_LIST_HEAD(&sd->list);
228 BUG_ON(!ops);
229 sd->ops = ops;
230 sd->v4l2_dev = NULL;
231 sd->flags = 0;
232 sd->name[0] = '\0';
233 sd->grp_id = 0;
234 sd->dev_priv = NULL;
235 sd->host_priv = NULL;
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300236#if defined(CONFIG_MEDIA_CONTROLLER)
237 sd->entity.name = sd->name;
238 sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
239#endif
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300240}
241EXPORT_SYMBOL(v4l2_subdev_init);