blob: 8bafb942552b5f25fda7837b8553118984d53dc0 [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>
Paul Gortmaker35a24632011-08-01 15:26:38 -040027#include <linux/export.h>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030028
Laurent Pinchartea8aa432009-12-09 08:39:54 -030029#include <media/v4l2-ctrls.h>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030030#include <media/v4l2-device.h>
31#include <media/v4l2-ioctl.h>
Sakari Ailus02adb1c2010-03-03 12:49:38 -030032#include <media/v4l2-fh.h>
33#include <media/v4l2-event.h>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030034
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030035static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
36{
37#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
Sakari Ailusae184cd2011-10-14 14:14:26 -030038 fh->pad = kzalloc(sizeof(*fh->pad) * sd->entity.num_pads, GFP_KERNEL);
39 if (fh->pad == NULL)
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030040 return -ENOMEM;
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030041#endif
42 return 0;
43}
44
45static void subdev_fh_free(struct v4l2_subdev_fh *fh)
46{
47#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
Sakari Ailusae184cd2011-10-14 14:14:26 -030048 kfree(fh->pad);
49 fh->pad = NULL;
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030050#endif
51}
52
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030053static int subdev_open(struct file *file)
54{
Sakari Ailus02adb1c2010-03-03 12:49:38 -030055 struct video_device *vdev = video_devdata(file);
56 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030057 struct v4l2_subdev_fh *subdev_fh;
Laurent Pinchart61f5db52009-12-09 08:40:08 -030058#if defined(CONFIG_MEDIA_CONTROLLER)
Laurent Pinchartf0beea82010-08-01 19:05:09 -030059 struct media_entity *entity = NULL;
Laurent Pinchart61f5db52009-12-09 08:40:08 -030060#endif
Sakari Ailus02adb1c2010-03-03 12:49:38 -030061 int ret;
62
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030063 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
64 if (subdev_fh == NULL)
65 return -ENOMEM;
Sakari Ailus02adb1c2010-03-03 12:49:38 -030066
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030067 ret = subdev_fh_init(subdev_fh, sd);
68 if (ret) {
69 kfree(subdev_fh);
70 return ret;
Sakari Ailus02adb1c2010-03-03 12:49:38 -030071 }
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030072
Hans Verkuil523f46d2011-06-13 17:44:42 -030073 v4l2_fh_init(&subdev_fh->vfh, vdev);
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030074 v4l2_fh_add(&subdev_fh->vfh);
75 file->private_data = &subdev_fh->vfh;
Laurent Pinchart61f5db52009-12-09 08:40:08 -030076#if defined(CONFIG_MEDIA_CONTROLLER)
77 if (sd->v4l2_dev->mdev) {
78 entity = media_entity_get(&sd->entity);
79 if (!entity) {
80 ret = -EBUSY;
81 goto err;
82 }
83 }
84#endif
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030085
Laurent Pinchartf0beea82010-08-01 19:05:09 -030086 if (sd->internal_ops && sd->internal_ops->open) {
87 ret = sd->internal_ops->open(sd, subdev_fh);
88 if (ret < 0)
89 goto err;
90 }
91
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030092 return 0;
Sakari Ailus02adb1c2010-03-03 12:49:38 -030093
94err:
Laurent Pinchartf0beea82010-08-01 19:05:09 -030095#if defined(CONFIG_MEDIA_CONTROLLER)
96 if (entity)
97 media_entity_put(entity);
98#endif
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030099 v4l2_fh_del(&subdev_fh->vfh);
100 v4l2_fh_exit(&subdev_fh->vfh);
101 subdev_fh_free(subdev_fh);
102 kfree(subdev_fh);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300103
104 return ret;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300105}
106
107static int subdev_close(struct file *file)
108{
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300109 struct video_device *vdev = video_devdata(file);
110 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300111 struct v4l2_fh *vfh = file->private_data;
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300112 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300113
Laurent Pinchartf0beea82010-08-01 19:05:09 -0300114 if (sd->internal_ops && sd->internal_ops->close)
115 sd->internal_ops->close(sd, subdev_fh);
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300116#if defined(CONFIG_MEDIA_CONTROLLER)
117 if (sd->v4l2_dev->mdev)
118 media_entity_put(&sd->entity);
119#endif
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300120 v4l2_fh_del(vfh);
121 v4l2_fh_exit(vfh);
122 subdev_fh_free(subdev_fh);
123 kfree(subdev_fh);
124 file->private_data = NULL;
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300125
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300126 return 0;
127}
128
Mauro Carvalho Chehabd352bcc92014-07-22 01:40:00 -0300129#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
Sakari Ailusb225e392014-06-04 08:22:03 -0300130static int check_format(struct v4l2_subdev *sd,
131 struct v4l2_subdev_format *format)
132{
133 if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
134 format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
135 return -EINVAL;
136
137 if (format->pad >= sd->entity.num_pads)
138 return -EINVAL;
139
140 return 0;
141}
142
143static int check_crop(struct v4l2_subdev *sd, struct v4l2_subdev_crop *crop)
144{
145 if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
146 crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
147 return -EINVAL;
148
149 if (crop->pad >= sd->entity.num_pads)
150 return -EINVAL;
151
152 return 0;
153}
154
155static int check_selection(struct v4l2_subdev *sd,
156 struct v4l2_subdev_selection *sel)
157{
158 if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
159 sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
160 return -EINVAL;
161
162 if (sel->pad >= sd->entity.num_pads)
163 return -EINVAL;
164
165 return 0;
166}
167
168static int check_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
169{
170 if (edid->pad >= sd->entity.num_pads)
171 return -EINVAL;
172
173 if (edid->blocks && edid->edid == NULL)
174 return -EINVAL;
175
176 return 0;
177}
Mauro Carvalho Chehabd352bcc92014-07-22 01:40:00 -0300178#endif
Sakari Ailusb225e392014-06-04 08:22:03 -0300179
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300180static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
181{
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300182 struct video_device *vdev = video_devdata(file);
183 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300184 struct v4l2_fh *vfh = file->private_data;
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300185#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
186 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
Sakari Ailusb225e392014-06-04 08:22:03 -0300187 int rval;
Mauro Carvalho Chehabd352bcc92014-07-22 01:40:00 -0300188#endif
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300189
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300190 switch (cmd) {
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300191 case VIDIOC_QUERYCTRL:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300192 return v4l2_queryctrl(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300193
Hans Verkuile6bee362014-06-10 04:22:06 -0300194 case VIDIOC_QUERY_EXT_CTRL:
195 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
196
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300197 case VIDIOC_QUERYMENU:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300198 return v4l2_querymenu(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300199
200 case VIDIOC_G_CTRL:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300201 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300202
203 case VIDIOC_S_CTRL:
Hans Verkuilab892ba2011-06-07 06:47:18 -0300204 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300205
206 case VIDIOC_G_EXT_CTRLS:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300207 return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300208
209 case VIDIOC_S_EXT_CTRLS:
Hans Verkuilab892ba2011-06-07 06:47:18 -0300210 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300211
212 case VIDIOC_TRY_EXT_CTRLS:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300213 return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300214
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300215 case VIDIOC_DQEVENT:
216 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
217 return -ENOIOCTLCMD;
218
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300219 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300220
221 case VIDIOC_SUBSCRIBE_EVENT:
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300222 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300223
224 case VIDIOC_UNSUBSCRIBE_EVENT:
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300225 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
Martin Hostettler69967a72011-09-19 02:04:56 -0300226
227#ifdef CONFIG_VIDEO_ADV_DEBUG
228 case VIDIOC_DBG_G_REGISTER:
229 {
230 struct v4l2_dbg_register *p = arg;
231
232 if (!capable(CAP_SYS_ADMIN))
233 return -EPERM;
234 return v4l2_subdev_call(sd, core, g_register, p);
235 }
236 case VIDIOC_DBG_S_REGISTER:
237 {
238 struct v4l2_dbg_register *p = arg;
239
240 if (!capable(CAP_SYS_ADMIN))
241 return -EPERM;
242 return v4l2_subdev_call(sd, core, s_register, p);
243 }
244#endif
Sylwester Nawrockibcd158d2011-10-01 11:13:59 -0300245
Hans Verkuil42194e72012-02-02 08:26:20 -0300246 case VIDIOC_LOG_STATUS: {
247 int ret;
248
249 pr_info("%s: ================= START STATUS =================\n",
250 sd->name);
251 ret = v4l2_subdev_call(sd, core, log_status);
252 pr_info("%s: ================== END STATUS ==================\n",
253 sd->name);
254 return ret;
255 }
Sylwester Nawrockibcd158d2011-10-01 11:13:59 -0300256
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300257#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
258 case VIDIOC_SUBDEV_G_FMT: {
259 struct v4l2_subdev_format *format = arg;
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300260
Sakari Ailusb225e392014-06-04 08:22:03 -0300261 rval = check_format(sd, format);
262 if (rval)
263 return rval;
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300264
Hans Verkuilf7234132015-03-04 01:47:54 -0800265 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->pad, format);
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300266 }
267
268 case VIDIOC_SUBDEV_S_FMT: {
269 struct v4l2_subdev_format *format = arg;
270
Sakari Ailusb225e392014-06-04 08:22:03 -0300271 rval = check_format(sd, format);
272 if (rval)
273 return rval;
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300274
Hans Verkuilf7234132015-03-04 01:47:54 -0800275 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->pad, format);
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300276 }
277
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300278 case VIDIOC_SUBDEV_G_CROP: {
279 struct v4l2_subdev_crop *crop = arg;
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300280 struct v4l2_subdev_selection sel;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300281
Sakari Ailusb225e392014-06-04 08:22:03 -0300282 rval = check_crop(sd, crop);
283 if (rval)
284 return rval;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300285
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300286 memset(&sel, 0, sizeof(sel));
287 sel.which = crop->which;
288 sel.pad = crop->pad;
Sakari Ailus5689b282012-05-18 09:31:18 -0300289 sel.target = V4L2_SEL_TGT_CROP;
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300290
291 rval = v4l2_subdev_call(
Hans Verkuilf7234132015-03-04 01:47:54 -0800292 sd, pad, get_selection, subdev_fh->pad, &sel);
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300293
294 crop->rect = sel.r;
295
296 return rval;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300297 }
298
299 case VIDIOC_SUBDEV_S_CROP: {
300 struct v4l2_subdev_crop *crop = arg;
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300301 struct v4l2_subdev_selection sel;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300302
Sakari Ailusb225e392014-06-04 08:22:03 -0300303 rval = check_crop(sd, crop);
304 if (rval)
305 return rval;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300306
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300307 memset(&sel, 0, sizeof(sel));
308 sel.which = crop->which;
309 sel.pad = crop->pad;
Sakari Ailus5689b282012-05-18 09:31:18 -0300310 sel.target = V4L2_SEL_TGT_CROP;
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300311 sel.r = crop->rect;
312
313 rval = v4l2_subdev_call(
Hans Verkuilf7234132015-03-04 01:47:54 -0800314 sd, pad, set_selection, subdev_fh->pad, &sel);
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300315
316 crop->rect = sel.r;
317
318 return rval;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300319 }
320
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300321 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
322 struct v4l2_subdev_mbus_code_enum *code = arg;
323
Hans Verkuil20058f92015-03-04 01:47:56 -0800324 if (code->which != V4L2_SUBDEV_FORMAT_TRY &&
325 code->which != V4L2_SUBDEV_FORMAT_ACTIVE)
326 return -EINVAL;
327
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300328 if (code->pad >= sd->entity.num_pads)
329 return -EINVAL;
330
Hans Verkuilf7234132015-03-04 01:47:54 -0800331 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->pad,
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300332 code);
333 }
334
335 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
336 struct v4l2_subdev_frame_size_enum *fse = arg;
337
Hans Verkuil20058f92015-03-04 01:47:56 -0800338 if (fse->which != V4L2_SUBDEV_FORMAT_TRY &&
339 fse->which != V4L2_SUBDEV_FORMAT_ACTIVE)
340 return -EINVAL;
341
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300342 if (fse->pad >= sd->entity.num_pads)
343 return -EINVAL;
344
Hans Verkuilf7234132015-03-04 01:47:54 -0800345 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->pad,
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300346 fse);
347 }
Laurent Pinchart35c30172010-05-05 11:38:35 -0300348
Sakari Ailus743e1832013-04-22 10:24:51 -0300349 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
350 struct v4l2_subdev_frame_interval *fi = arg;
Laurent Pinchart35c30172010-05-05 11:38:35 -0300351
Sakari Ailus743e1832013-04-22 10:24:51 -0300352 if (fi->pad >= sd->entity.num_pads)
353 return -EINVAL;
354
355 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
356 }
357
358 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
359 struct v4l2_subdev_frame_interval *fi = arg;
360
361 if (fi->pad >= sd->entity.num_pads)
362 return -EINVAL;
363
Laurent Pinchart35c30172010-05-05 11:38:35 -0300364 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
Sakari Ailus743e1832013-04-22 10:24:51 -0300365 }
Laurent Pinchart35c30172010-05-05 11:38:35 -0300366
367 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
368 struct v4l2_subdev_frame_interval_enum *fie = arg;
369
Hans Verkuil20058f92015-03-04 01:47:56 -0800370 if (fie->which != V4L2_SUBDEV_FORMAT_TRY &&
371 fie->which != V4L2_SUBDEV_FORMAT_ACTIVE)
372 return -EINVAL;
373
Laurent Pinchart35c30172010-05-05 11:38:35 -0300374 if (fie->pad >= sd->entity.num_pads)
375 return -EINVAL;
376
Hans Verkuilf7234132015-03-04 01:47:54 -0800377 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->pad,
Laurent Pinchart35c30172010-05-05 11:38:35 -0300378 fie);
379 }
Sakari Ailusae184cd2011-10-14 14:14:26 -0300380
381 case VIDIOC_SUBDEV_G_SELECTION: {
382 struct v4l2_subdev_selection *sel = arg;
383
Sakari Ailusb225e392014-06-04 08:22:03 -0300384 rval = check_selection(sd, sel);
385 if (rval)
386 return rval;
Sakari Ailusae184cd2011-10-14 14:14:26 -0300387
388 return v4l2_subdev_call(
Hans Verkuilf7234132015-03-04 01:47:54 -0800389 sd, pad, get_selection, subdev_fh->pad, sel);
Sakari Ailusae184cd2011-10-14 14:14:26 -0300390 }
391
392 case VIDIOC_SUBDEV_S_SELECTION: {
393 struct v4l2_subdev_selection *sel = arg;
394
Sakari Ailusb225e392014-06-04 08:22:03 -0300395 rval = check_selection(sd, sel);
396 if (rval)
397 return rval;
Sakari Ailusae184cd2011-10-14 14:14:26 -0300398
399 return v4l2_subdev_call(
Hans Verkuilf7234132015-03-04 01:47:54 -0800400 sd, pad, set_selection, subdev_fh->pad, sel);
Sakari Ailusae184cd2011-10-14 14:14:26 -0300401 }
Hans Verkuiled45ce22012-08-10 06:07:12 -0300402
Laurent Pinchartf2e90842014-01-29 10:07:13 -0300403 case VIDIOC_G_EDID: {
404 struct v4l2_subdev_edid *edid = arg;
Hans Verkuiled45ce22012-08-10 06:07:12 -0300405
Sakari Ailusb225e392014-06-04 08:22:03 -0300406 rval = check_edid(sd, edid);
407 if (rval)
408 return rval;
Laurent Pinchartf2e90842014-01-29 10:07:13 -0300409
410 return v4l2_subdev_call(sd, pad, get_edid, edid);
411 }
412
413 case VIDIOC_S_EDID: {
414 struct v4l2_subdev_edid *edid = arg;
415
Sakari Ailusb225e392014-06-04 08:22:03 -0300416 rval = check_edid(sd, edid);
417 if (rval)
418 return rval;
Laurent Pinchartf2e90842014-01-29 10:07:13 -0300419
420 return v4l2_subdev_call(sd, pad, set_edid, edid);
421 }
Laurent Pinchart9cfd65e2014-01-29 10:07:13 -0300422
423 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
424 struct v4l2_dv_timings_cap *cap = arg;
425
426 if (cap->pad >= sd->entity.num_pads)
427 return -EINVAL;
428
429 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
430 }
431
432 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
433 struct v4l2_enum_dv_timings *dvt = arg;
434
435 if (dvt->pad >= sd->entity.num_pads)
436 return -EINVAL;
437
438 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
439 }
440
441 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
442 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
443
444 case VIDIOC_SUBDEV_G_DV_TIMINGS:
445 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
446
447 case VIDIOC_SUBDEV_S_DV_TIMINGS:
448 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300449#endif
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300450 default:
Laurent Pinchartc30b46e2010-02-26 12:23:10 -0300451 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300452 }
453
454 return 0;
455}
456
457static long subdev_ioctl(struct file *file, unsigned int cmd,
458 unsigned long arg)
459{
460 return video_usercopy(file, cmd, arg, subdev_do_ioctl);
461}
462
Hans Verkuilab58a302014-02-10 08:08:44 -0300463#ifdef CONFIG_COMPAT
464static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
465 unsigned long arg)
466{
467 struct video_device *vdev = video_devdata(file);
468 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
469
470 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
471}
472#endif
473
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300474static unsigned int subdev_poll(struct file *file, poll_table *wait)
475{
476 struct video_device *vdev = video_devdata(file);
477 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
478 struct v4l2_fh *fh = file->private_data;
479
480 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
481 return POLLERR;
482
Hans Verkuil523f46d2011-06-13 17:44:42 -0300483 poll_wait(file, &fh->wait, wait);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300484
485 if (v4l2_event_pending(fh))
486 return POLLPRI;
487
488 return 0;
489}
490
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300491const struct v4l2_file_operations v4l2_subdev_fops = {
492 .owner = THIS_MODULE,
493 .open = subdev_open,
494 .unlocked_ioctl = subdev_ioctl,
Hans Verkuilab58a302014-02-10 08:08:44 -0300495#ifdef CONFIG_COMPAT
496 .compat_ioctl32 = subdev_compat_ioctl32,
497#endif
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300498 .release = subdev_close,
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300499 .poll = subdev_poll,
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300500};
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300501
Sakari Ailus8227c922011-10-10 17:01:25 -0300502#ifdef CONFIG_MEDIA_CONTROLLER
503int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
504 struct media_link *link,
505 struct v4l2_subdev_format *source_fmt,
506 struct v4l2_subdev_format *sink_fmt)
507{
Laurent Pinchart24acf8b2014-05-19 11:36:23 -0300508 /* The width, height and code must match. */
Sakari Ailus8227c922011-10-10 17:01:25 -0300509 if (source_fmt->format.width != sink_fmt->format.width
510 || source_fmt->format.height != sink_fmt->format.height
511 || source_fmt->format.code != sink_fmt->format.code)
512 return -EINVAL;
513
Laurent Pinchart24acf8b2014-05-19 11:36:23 -0300514 /* The field order must match, or the sink field order must be NONE
515 * to support interlaced hardware connected to bridges that support
516 * progressive formats only.
517 */
518 if (source_fmt->format.field != sink_fmt->format.field &&
519 sink_fmt->format.field != V4L2_FIELD_NONE)
520 return -EINVAL;
521
Sakari Ailus8227c922011-10-10 17:01:25 -0300522 return 0;
523}
524EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
525
526static int
527v4l2_subdev_link_validate_get_format(struct media_pad *pad,
528 struct v4l2_subdev_format *fmt)
529{
Laurent Pinchart864a1212012-10-20 17:47:22 -0300530 if (media_entity_type(pad->entity) == MEDIA_ENT_T_V4L2_SUBDEV) {
531 struct v4l2_subdev *sd =
532 media_entity_to_v4l2_subdev(pad->entity);
533
Sakari Ailus8227c922011-10-10 17:01:25 -0300534 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
535 fmt->pad = pad->index;
Laurent Pinchart864a1212012-10-20 17:47:22 -0300536 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
Sakari Ailus8227c922011-10-10 17:01:25 -0300537 }
Laurent Pinchart864a1212012-10-20 17:47:22 -0300538
539 WARN(pad->entity->type != MEDIA_ENT_T_DEVNODE_V4L,
540 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
541 pad->entity->type, pad->entity->name);
542
543 return -EINVAL;
Sakari Ailus8227c922011-10-10 17:01:25 -0300544}
545
546int v4l2_subdev_link_validate(struct media_link *link)
547{
548 struct v4l2_subdev *sink;
549 struct v4l2_subdev_format sink_fmt, source_fmt;
550 int rval;
551
552 rval = v4l2_subdev_link_validate_get_format(
553 link->source, &source_fmt);
554 if (rval < 0)
555 return 0;
556
557 rval = v4l2_subdev_link_validate_get_format(
558 link->sink, &sink_fmt);
559 if (rval < 0)
560 return 0;
561
562 sink = media_entity_to_v4l2_subdev(link->sink->entity);
563
564 rval = v4l2_subdev_call(sink, pad, link_validate, link,
565 &source_fmt, &sink_fmt);
566 if (rval != -ENOIOCTLCMD)
567 return rval;
568
569 return v4l2_subdev_link_validate_default(
570 sink, link, &source_fmt, &sink_fmt);
571}
572EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
573#endif /* CONFIG_MEDIA_CONTROLLER */
574
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300575void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
576{
577 INIT_LIST_HEAD(&sd->list);
578 BUG_ON(!ops);
579 sd->ops = ops;
580 sd->v4l2_dev = NULL;
581 sd->flags = 0;
582 sd->name[0] = '\0';
583 sd->grp_id = 0;
584 sd->dev_priv = NULL;
585 sd->host_priv = NULL;
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300586#if defined(CONFIG_MEDIA_CONTROLLER)
587 sd->entity.name = sd->name;
588 sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
589#endif
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300590}
591EXPORT_SYMBOL(v4l2_subdev_init);