blob: 60d2550c9ac8707db83ee33baf76ebbae89a2c8e [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
129static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
130{
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300131 struct video_device *vdev = video_devdata(file);
132 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300133 struct v4l2_fh *vfh = file->private_data;
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300134#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
135 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
136#endif
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300137
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300138 switch (cmd) {
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300139 case VIDIOC_QUERYCTRL:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300140 return v4l2_queryctrl(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300141
142 case VIDIOC_QUERYMENU:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300143 return v4l2_querymenu(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300144
145 case VIDIOC_G_CTRL:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300146 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300147
148 case VIDIOC_S_CTRL:
Hans Verkuilab892ba2011-06-07 06:47:18 -0300149 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300150
151 case VIDIOC_G_EXT_CTRLS:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300152 return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300153
154 case VIDIOC_S_EXT_CTRLS:
Hans Verkuilab892ba2011-06-07 06:47:18 -0300155 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300156
157 case VIDIOC_TRY_EXT_CTRLS:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300158 return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300159
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300160 case VIDIOC_DQEVENT:
161 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
162 return -ENOIOCTLCMD;
163
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300164 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300165
166 case VIDIOC_SUBSCRIBE_EVENT:
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300167 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300168
169 case VIDIOC_UNSUBSCRIBE_EVENT:
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300170 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
Martin Hostettler69967a72011-09-19 02:04:56 -0300171
172#ifdef CONFIG_VIDEO_ADV_DEBUG
173 case VIDIOC_DBG_G_REGISTER:
174 {
175 struct v4l2_dbg_register *p = arg;
176
177 if (!capable(CAP_SYS_ADMIN))
178 return -EPERM;
179 return v4l2_subdev_call(sd, core, g_register, p);
180 }
181 case VIDIOC_DBG_S_REGISTER:
182 {
183 struct v4l2_dbg_register *p = arg;
184
185 if (!capable(CAP_SYS_ADMIN))
186 return -EPERM;
187 return v4l2_subdev_call(sd, core, s_register, p);
188 }
189#endif
Sylwester Nawrockibcd158d2011-10-01 11:13:59 -0300190
Hans Verkuil42194e72012-02-02 08:26:20 -0300191 case VIDIOC_LOG_STATUS: {
192 int ret;
193
194 pr_info("%s: ================= START STATUS =================\n",
195 sd->name);
196 ret = v4l2_subdev_call(sd, core, log_status);
197 pr_info("%s: ================== END STATUS ==================\n",
198 sd->name);
199 return ret;
200 }
Sylwester Nawrockibcd158d2011-10-01 11:13:59 -0300201
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300202#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
203 case VIDIOC_SUBDEV_G_FMT: {
204 struct v4l2_subdev_format *format = arg;
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300205
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300206 if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
207 format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
208 return -EINVAL;
209
210 if (format->pad >= sd->entity.num_pads)
211 return -EINVAL;
212
213 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh, format);
214 }
215
216 case VIDIOC_SUBDEV_S_FMT: {
217 struct v4l2_subdev_format *format = arg;
218
219 if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
220 format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
221 return -EINVAL;
222
223 if (format->pad >= sd->entity.num_pads)
224 return -EINVAL;
225
226 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format);
227 }
228
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300229 case VIDIOC_SUBDEV_G_CROP: {
230 struct v4l2_subdev_crop *crop = arg;
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300231 struct v4l2_subdev_selection sel;
232 int rval;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300233
234 if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
235 crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
236 return -EINVAL;
237
238 if (crop->pad >= sd->entity.num_pads)
239 return -EINVAL;
240
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300241 rval = v4l2_subdev_call(sd, pad, get_crop, subdev_fh, crop);
242 if (rval != -ENOIOCTLCMD)
243 return rval;
244
245 memset(&sel, 0, sizeof(sel));
246 sel.which = crop->which;
247 sel.pad = crop->pad;
Sakari Ailus5689b282012-05-18 09:31:18 -0300248 sel.target = V4L2_SEL_TGT_CROP;
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300249
250 rval = v4l2_subdev_call(
251 sd, pad, get_selection, subdev_fh, &sel);
252
253 crop->rect = sel.r;
254
255 return rval;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300256 }
257
258 case VIDIOC_SUBDEV_S_CROP: {
259 struct v4l2_subdev_crop *crop = arg;
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300260 struct v4l2_subdev_selection sel;
261 int rval;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300262
263 if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
264 crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
265 return -EINVAL;
266
267 if (crop->pad >= sd->entity.num_pads)
268 return -EINVAL;
269
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300270 rval = v4l2_subdev_call(sd, pad, set_crop, subdev_fh, crop);
271 if (rval != -ENOIOCTLCMD)
272 return rval;
273
274 memset(&sel, 0, sizeof(sel));
275 sel.which = crop->which;
276 sel.pad = crop->pad;
Sakari Ailus5689b282012-05-18 09:31:18 -0300277 sel.target = V4L2_SEL_TGT_CROP;
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300278 sel.r = crop->rect;
279
280 rval = v4l2_subdev_call(
281 sd, pad, set_selection, subdev_fh, &sel);
282
283 crop->rect = sel.r;
284
285 return rval;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300286 }
287
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300288 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
289 struct v4l2_subdev_mbus_code_enum *code = arg;
290
291 if (code->pad >= sd->entity.num_pads)
292 return -EINVAL;
293
294 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh,
295 code);
296 }
297
298 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
299 struct v4l2_subdev_frame_size_enum *fse = arg;
300
301 if (fse->pad >= sd->entity.num_pads)
302 return -EINVAL;
303
304 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh,
305 fse);
306 }
Laurent Pinchart35c30172010-05-05 11:38:35 -0300307
308 case VIDIOC_SUBDEV_G_FRAME_INTERVAL:
309 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
310
311 case VIDIOC_SUBDEV_S_FRAME_INTERVAL:
312 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
313
314 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
315 struct v4l2_subdev_frame_interval_enum *fie = arg;
316
317 if (fie->pad >= sd->entity.num_pads)
318 return -EINVAL;
319
320 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh,
321 fie);
322 }
Sakari Ailusae184cd2011-10-14 14:14:26 -0300323
324 case VIDIOC_SUBDEV_G_SELECTION: {
325 struct v4l2_subdev_selection *sel = arg;
326
327 if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
328 sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
329 return -EINVAL;
330
331 if (sel->pad >= sd->entity.num_pads)
332 return -EINVAL;
333
334 return v4l2_subdev_call(
335 sd, pad, get_selection, subdev_fh, sel);
336 }
337
338 case VIDIOC_SUBDEV_S_SELECTION: {
339 struct v4l2_subdev_selection *sel = arg;
340
341 if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
342 sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
343 return -EINVAL;
344
345 if (sel->pad >= sd->entity.num_pads)
346 return -EINVAL;
347
348 return v4l2_subdev_call(
349 sd, pad, set_selection, subdev_fh, sel);
350 }
Hans Verkuiled45ce22012-08-10 06:07:12 -0300351
352 case VIDIOC_SUBDEV_G_EDID:
353 return v4l2_subdev_call(sd, pad, get_edid, arg);
354
355 case VIDIOC_SUBDEV_S_EDID:
356 return v4l2_subdev_call(sd, pad, set_edid, arg);
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300357#endif
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300358 default:
Laurent Pinchartc30b46e2010-02-26 12:23:10 -0300359 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300360 }
361
362 return 0;
363}
364
365static long subdev_ioctl(struct file *file, unsigned int cmd,
366 unsigned long arg)
367{
368 return video_usercopy(file, cmd, arg, subdev_do_ioctl);
369}
370
Hans Verkuilab58a302014-02-10 08:08:44 -0300371#ifdef CONFIG_COMPAT
372static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
373 unsigned long arg)
374{
375 struct video_device *vdev = video_devdata(file);
376 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
377
378 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
379}
380#endif
381
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300382static unsigned int subdev_poll(struct file *file, poll_table *wait)
383{
384 struct video_device *vdev = video_devdata(file);
385 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
386 struct v4l2_fh *fh = file->private_data;
387
388 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
389 return POLLERR;
390
Hans Verkuil523f46d2011-06-13 17:44:42 -0300391 poll_wait(file, &fh->wait, wait);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300392
393 if (v4l2_event_pending(fh))
394 return POLLPRI;
395
396 return 0;
397}
398
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300399const struct v4l2_file_operations v4l2_subdev_fops = {
400 .owner = THIS_MODULE,
401 .open = subdev_open,
402 .unlocked_ioctl = subdev_ioctl,
Hans Verkuilab58a302014-02-10 08:08:44 -0300403#ifdef CONFIG_COMPAT
404 .compat_ioctl32 = subdev_compat_ioctl32,
405#endif
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300406 .release = subdev_close,
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300407 .poll = subdev_poll,
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300408};
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300409
Sakari Ailus8227c922011-10-10 17:01:25 -0300410#ifdef CONFIG_MEDIA_CONTROLLER
411int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
412 struct media_link *link,
413 struct v4l2_subdev_format *source_fmt,
414 struct v4l2_subdev_format *sink_fmt)
415{
416 if (source_fmt->format.width != sink_fmt->format.width
417 || source_fmt->format.height != sink_fmt->format.height
418 || source_fmt->format.code != sink_fmt->format.code)
419 return -EINVAL;
420
421 return 0;
422}
423EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
424
425static int
426v4l2_subdev_link_validate_get_format(struct media_pad *pad,
427 struct v4l2_subdev_format *fmt)
428{
Laurent Pinchart864a1212012-10-20 17:47:22 -0300429 if (media_entity_type(pad->entity) == MEDIA_ENT_T_V4L2_SUBDEV) {
430 struct v4l2_subdev *sd =
431 media_entity_to_v4l2_subdev(pad->entity);
432
Sakari Ailus8227c922011-10-10 17:01:25 -0300433 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
434 fmt->pad = pad->index;
Laurent Pinchart864a1212012-10-20 17:47:22 -0300435 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
Sakari Ailus8227c922011-10-10 17:01:25 -0300436 }
Laurent Pinchart864a1212012-10-20 17:47:22 -0300437
438 WARN(pad->entity->type != MEDIA_ENT_T_DEVNODE_V4L,
439 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
440 pad->entity->type, pad->entity->name);
441
442 return -EINVAL;
Sakari Ailus8227c922011-10-10 17:01:25 -0300443}
444
445int v4l2_subdev_link_validate(struct media_link *link)
446{
447 struct v4l2_subdev *sink;
448 struct v4l2_subdev_format sink_fmt, source_fmt;
449 int rval;
450
451 rval = v4l2_subdev_link_validate_get_format(
452 link->source, &source_fmt);
453 if (rval < 0)
454 return 0;
455
456 rval = v4l2_subdev_link_validate_get_format(
457 link->sink, &sink_fmt);
458 if (rval < 0)
459 return 0;
460
461 sink = media_entity_to_v4l2_subdev(link->sink->entity);
462
463 rval = v4l2_subdev_call(sink, pad, link_validate, link,
464 &source_fmt, &sink_fmt);
465 if (rval != -ENOIOCTLCMD)
466 return rval;
467
468 return v4l2_subdev_link_validate_default(
469 sink, link, &source_fmt, &sink_fmt);
470}
471EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
472#endif /* CONFIG_MEDIA_CONTROLLER */
473
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300474void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
475{
476 INIT_LIST_HEAD(&sd->list);
477 BUG_ON(!ops);
478 sd->ops = ops;
479 sd->v4l2_dev = NULL;
480 sd->flags = 0;
481 sd->name[0] = '\0';
482 sd->grp_id = 0;
483 sd->dev_priv = NULL;
484 sd->host_priv = NULL;
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300485#if defined(CONFIG_MEDIA_CONTROLLER)
486 sd->entity.name = sd->name;
487 sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
488#endif
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300489}
490EXPORT_SYMBOL(v4l2_subdev_init);