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>
27 #include <linux/export.h>
29 #include <media/v4l2-ctrls.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-fh.h>
33 #include <media/v4l2-event.h>
35 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
37 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
38 fh->pad = kzalloc(sizeof(*fh->pad) * sd->entity.num_pads, GFP_KERNEL);
45 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
47 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
53 static int subdev_open(struct file *file)
55 struct video_device *vdev = video_devdata(file);
56 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
57 struct v4l2_subdev_fh *subdev_fh;
58 #if defined(CONFIG_MEDIA_CONTROLLER)
59 struct media_entity *entity = NULL;
63 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
64 if (subdev_fh == NULL)
67 ret = subdev_fh_init(subdev_fh, sd);
73 v4l2_fh_init(&subdev_fh->vfh, vdev);
74 v4l2_fh_add(&subdev_fh->vfh);
75 file->private_data = &subdev_fh->vfh;
76 #if defined(CONFIG_MEDIA_CONTROLLER)
77 if (sd->v4l2_dev->mdev) {
78 entity = media_entity_get(&sd->entity);
86 if (sd->internal_ops && sd->internal_ops->open) {
87 ret = sd->internal_ops->open(sd, subdev_fh);
95 #if defined(CONFIG_MEDIA_CONTROLLER)
97 media_entity_put(entity);
99 v4l2_fh_del(&subdev_fh->vfh);
100 v4l2_fh_exit(&subdev_fh->vfh);
101 subdev_fh_free(subdev_fh);
107 static int subdev_close(struct file *file)
109 struct video_device *vdev = video_devdata(file);
110 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
111 struct v4l2_fh *vfh = file->private_data;
112 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
114 if (sd->internal_ops && sd->internal_ops->close)
115 sd->internal_ops->close(sd, subdev_fh);
116 #if defined(CONFIG_MEDIA_CONTROLLER)
117 if (sd->v4l2_dev->mdev)
118 media_entity_put(&sd->entity);
122 subdev_fh_free(subdev_fh);
124 file->private_data = NULL;
129 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
130 static int check_format(struct v4l2_subdev *sd,
131 struct v4l2_subdev_format *format)
133 if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
134 format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
137 if (format->pad >= sd->entity.num_pads)
143 static int check_crop(struct v4l2_subdev *sd, struct v4l2_subdev_crop *crop)
145 if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
146 crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
149 if (crop->pad >= sd->entity.num_pads)
155 static int check_selection(struct v4l2_subdev *sd,
156 struct v4l2_subdev_selection *sel)
158 if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
159 sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
162 if (sel->pad >= sd->entity.num_pads)
168 static int check_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
170 if (edid->pad >= sd->entity.num_pads)
173 if (edid->blocks && edid->edid == NULL)
180 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
182 struct video_device *vdev = video_devdata(file);
183 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
184 struct v4l2_fh *vfh = file->private_data;
185 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
186 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
191 case VIDIOC_QUERYCTRL:
192 return v4l2_queryctrl(vfh->ctrl_handler, arg);
194 case VIDIOC_QUERY_EXT_CTRL:
195 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
197 case VIDIOC_QUERYMENU:
198 return v4l2_querymenu(vfh->ctrl_handler, arg);
201 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
204 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
206 case VIDIOC_G_EXT_CTRLS:
207 return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg);
209 case VIDIOC_S_EXT_CTRLS:
210 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg);
212 case VIDIOC_TRY_EXT_CTRLS:
213 return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg);
216 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
219 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
221 case VIDIOC_SUBSCRIBE_EVENT:
222 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
224 case VIDIOC_UNSUBSCRIBE_EVENT:
225 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
227 #ifdef CONFIG_VIDEO_ADV_DEBUG
228 case VIDIOC_DBG_G_REGISTER:
230 struct v4l2_dbg_register *p = arg;
232 if (!capable(CAP_SYS_ADMIN))
234 return v4l2_subdev_call(sd, core, g_register, p);
236 case VIDIOC_DBG_S_REGISTER:
238 struct v4l2_dbg_register *p = arg;
240 if (!capable(CAP_SYS_ADMIN))
242 return v4l2_subdev_call(sd, core, s_register, p);
246 case VIDIOC_LOG_STATUS: {
249 pr_info("%s: ================= START STATUS =================\n",
251 ret = v4l2_subdev_call(sd, core, log_status);
252 pr_info("%s: ================== END STATUS ==================\n",
257 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
258 case VIDIOC_SUBDEV_G_FMT: {
259 struct v4l2_subdev_format *format = arg;
261 rval = check_format(sd, format);
265 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh, format);
268 case VIDIOC_SUBDEV_S_FMT: {
269 struct v4l2_subdev_format *format = arg;
271 rval = check_format(sd, format);
275 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format);
278 case VIDIOC_SUBDEV_G_CROP: {
279 struct v4l2_subdev_crop *crop = arg;
280 struct v4l2_subdev_selection sel;
282 rval = check_crop(sd, crop);
286 rval = v4l2_subdev_call(sd, pad, get_crop, subdev_fh, crop);
287 if (rval != -ENOIOCTLCMD)
290 memset(&sel, 0, sizeof(sel));
291 sel.which = crop->which;
293 sel.target = V4L2_SEL_TGT_CROP;
295 rval = v4l2_subdev_call(
296 sd, pad, get_selection, subdev_fh, &sel);
303 case VIDIOC_SUBDEV_S_CROP: {
304 struct v4l2_subdev_crop *crop = arg;
305 struct v4l2_subdev_selection sel;
307 rval = check_crop(sd, crop);
311 rval = v4l2_subdev_call(sd, pad, set_crop, subdev_fh, crop);
312 if (rval != -ENOIOCTLCMD)
315 memset(&sel, 0, sizeof(sel));
316 sel.which = crop->which;
318 sel.target = V4L2_SEL_TGT_CROP;
321 rval = v4l2_subdev_call(
322 sd, pad, set_selection, subdev_fh, &sel);
329 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
330 struct v4l2_subdev_mbus_code_enum *code = arg;
332 if (code->pad >= sd->entity.num_pads)
335 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh,
339 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
340 struct v4l2_subdev_frame_size_enum *fse = arg;
342 if (fse->pad >= sd->entity.num_pads)
345 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh,
349 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
350 struct v4l2_subdev_frame_interval *fi = arg;
352 if (fi->pad >= sd->entity.num_pads)
355 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
358 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
359 struct v4l2_subdev_frame_interval *fi = arg;
361 if (fi->pad >= sd->entity.num_pads)
364 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
367 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
368 struct v4l2_subdev_frame_interval_enum *fie = arg;
370 if (fie->pad >= sd->entity.num_pads)
373 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh,
377 case VIDIOC_SUBDEV_G_SELECTION: {
378 struct v4l2_subdev_selection *sel = arg;
380 rval = check_selection(sd, sel);
384 return v4l2_subdev_call(
385 sd, pad, get_selection, subdev_fh, sel);
388 case VIDIOC_SUBDEV_S_SELECTION: {
389 struct v4l2_subdev_selection *sel = arg;
391 rval = check_selection(sd, sel);
395 return v4l2_subdev_call(
396 sd, pad, set_selection, subdev_fh, sel);
399 case VIDIOC_G_EDID: {
400 struct v4l2_subdev_edid *edid = arg;
402 rval = check_edid(sd, edid);
406 return v4l2_subdev_call(sd, pad, get_edid, edid);
409 case VIDIOC_S_EDID: {
410 struct v4l2_subdev_edid *edid = arg;
412 rval = check_edid(sd, edid);
416 return v4l2_subdev_call(sd, pad, set_edid, edid);
419 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
420 struct v4l2_dv_timings_cap *cap = arg;
422 if (cap->pad >= sd->entity.num_pads)
425 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
428 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
429 struct v4l2_enum_dv_timings *dvt = arg;
431 if (dvt->pad >= sd->entity.num_pads)
434 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
437 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
438 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
440 case VIDIOC_SUBDEV_G_DV_TIMINGS:
441 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
443 case VIDIOC_SUBDEV_S_DV_TIMINGS:
444 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
447 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
453 static long subdev_ioctl(struct file *file, unsigned int cmd,
456 return video_usercopy(file, cmd, arg, subdev_do_ioctl);
460 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
463 struct video_device *vdev = video_devdata(file);
464 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
466 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
470 static unsigned int subdev_poll(struct file *file, poll_table *wait)
472 struct video_device *vdev = video_devdata(file);
473 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
474 struct v4l2_fh *fh = file->private_data;
476 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
479 poll_wait(file, &fh->wait, wait);
481 if (v4l2_event_pending(fh))
487 const struct v4l2_file_operations v4l2_subdev_fops = {
488 .owner = THIS_MODULE,
490 .unlocked_ioctl = subdev_ioctl,
492 .compat_ioctl32 = subdev_compat_ioctl32,
494 .release = subdev_close,
498 #ifdef CONFIG_MEDIA_CONTROLLER
499 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
500 struct media_link *link,
501 struct v4l2_subdev_format *source_fmt,
502 struct v4l2_subdev_format *sink_fmt)
504 /* The width, height and code must match. */
505 if (source_fmt->format.width != sink_fmt->format.width
506 || source_fmt->format.height != sink_fmt->format.height
507 || source_fmt->format.code != sink_fmt->format.code)
510 /* The field order must match, or the sink field order must be NONE
511 * to support interlaced hardware connected to bridges that support
512 * progressive formats only.
514 if (source_fmt->format.field != sink_fmt->format.field &&
515 sink_fmt->format.field != V4L2_FIELD_NONE)
520 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
523 v4l2_subdev_link_validate_get_format(struct media_pad *pad,
524 struct v4l2_subdev_format *fmt)
526 if (media_entity_type(pad->entity) == MEDIA_ENT_T_V4L2_SUBDEV) {
527 struct v4l2_subdev *sd =
528 media_entity_to_v4l2_subdev(pad->entity);
530 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
531 fmt->pad = pad->index;
532 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
535 WARN(pad->entity->type != MEDIA_ENT_T_DEVNODE_V4L,
536 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
537 pad->entity->type, pad->entity->name);
542 int v4l2_subdev_link_validate(struct media_link *link)
544 struct v4l2_subdev *sink;
545 struct v4l2_subdev_format sink_fmt, source_fmt;
548 rval = v4l2_subdev_link_validate_get_format(
549 link->source, &source_fmt);
553 rval = v4l2_subdev_link_validate_get_format(
554 link->sink, &sink_fmt);
558 sink = media_entity_to_v4l2_subdev(link->sink->entity);
560 rval = v4l2_subdev_call(sink, pad, link_validate, link,
561 &source_fmt, &sink_fmt);
562 if (rval != -ENOIOCTLCMD)
565 return v4l2_subdev_link_validate_default(
566 sink, link, &source_fmt, &sink_fmt);
568 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
569 #endif /* CONFIG_MEDIA_CONTROLLER */
571 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
573 INIT_LIST_HEAD(&sd->list);
581 sd->host_priv = NULL;
582 #if defined(CONFIG_MEDIA_CONTROLLER)
583 sd->entity.name = sd->name;
584 sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
587 EXPORT_SYMBOL(v4l2_subdev_init);