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.
19 #include <linux/ioctl.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
24 #include <linux/videodev2.h>
25 #include <linux/export.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/v4l2-fh.h>
31 #include <media/v4l2-event.h>
33 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
35 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
36 if (sd->entity.num_pads) {
37 fh->pad = v4l2_subdev_alloc_pad_config(sd);
45 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
47 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
48 v4l2_subdev_free_pad_config(fh->pad);
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;
60 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
61 if (subdev_fh == NULL)
64 ret = subdev_fh_init(subdev_fh, sd);
70 v4l2_fh_init(&subdev_fh->vfh, vdev);
71 v4l2_fh_add(&subdev_fh->vfh);
72 file->private_data = &subdev_fh->vfh;
73 #if defined(CONFIG_MEDIA_CONTROLLER)
74 if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
77 owner = sd->entity.graph_obj.mdev->dev->driver->owner;
78 if (!try_module_get(owner)) {
82 subdev_fh->owner = owner;
86 if (sd->internal_ops && sd->internal_ops->open) {
87 ret = sd->internal_ops->open(sd, subdev_fh);
95 module_put(subdev_fh->owner);
96 v4l2_fh_del(&subdev_fh->vfh);
97 v4l2_fh_exit(&subdev_fh->vfh);
98 subdev_fh_free(subdev_fh);
104 static int subdev_close(struct file *file)
106 struct video_device *vdev = video_devdata(file);
107 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
108 struct v4l2_fh *vfh = file->private_data;
109 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
111 if (sd->internal_ops && sd->internal_ops->close)
112 sd->internal_ops->close(sd, subdev_fh);
113 module_put(subdev_fh->owner);
116 subdev_fh_free(subdev_fh);
118 file->private_data = NULL;
123 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
124 static int check_format(struct v4l2_subdev *sd,
125 struct v4l2_subdev_format *format)
127 if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
128 format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
131 if (format->pad >= sd->entity.num_pads)
137 static int check_crop(struct v4l2_subdev *sd, struct v4l2_subdev_crop *crop)
139 if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
140 crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
143 if (crop->pad >= sd->entity.num_pads)
149 static int check_selection(struct v4l2_subdev *sd,
150 struct v4l2_subdev_selection *sel)
152 if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
153 sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
156 if (sel->pad >= sd->entity.num_pads)
162 static int check_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
164 if (edid->pad >= sd->entity.num_pads)
167 if (edid->blocks && edid->edid == NULL)
174 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
176 struct video_device *vdev = video_devdata(file);
177 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
178 struct v4l2_fh *vfh = file->private_data;
179 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
180 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
185 case VIDIOC_QUERYCTRL:
187 * TODO: this really should be folded into v4l2_queryctrl (this
188 * currently returns -EINVAL for NULL control handlers).
189 * However, v4l2_queryctrl() is still called directly by
190 * drivers as well and until that has been addressed I believe
191 * it is safer to do the check here. The same is true for the
192 * other control ioctls below.
194 if (!vfh->ctrl_handler)
196 return v4l2_queryctrl(vfh->ctrl_handler, arg);
198 case VIDIOC_QUERY_EXT_CTRL:
199 if (!vfh->ctrl_handler)
201 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
203 case VIDIOC_QUERYMENU:
204 if (!vfh->ctrl_handler)
206 return v4l2_querymenu(vfh->ctrl_handler, arg);
209 if (!vfh->ctrl_handler)
211 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
214 if (!vfh->ctrl_handler)
216 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
218 case VIDIOC_G_EXT_CTRLS:
219 if (!vfh->ctrl_handler)
221 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
222 sd->v4l2_dev->mdev, arg);
224 case VIDIOC_S_EXT_CTRLS:
225 if (!vfh->ctrl_handler)
227 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
228 sd->v4l2_dev->mdev, arg);
230 case VIDIOC_TRY_EXT_CTRLS:
231 if (!vfh->ctrl_handler)
233 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
234 sd->v4l2_dev->mdev, arg);
237 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
240 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
242 case VIDIOC_SUBSCRIBE_EVENT:
243 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
245 case VIDIOC_UNSUBSCRIBE_EVENT:
246 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
248 #ifdef CONFIG_VIDEO_ADV_DEBUG
249 case VIDIOC_DBG_G_REGISTER:
251 struct v4l2_dbg_register *p = arg;
253 if (!capable(CAP_SYS_ADMIN))
255 return v4l2_subdev_call(sd, core, g_register, p);
257 case VIDIOC_DBG_S_REGISTER:
259 struct v4l2_dbg_register *p = arg;
261 if (!capable(CAP_SYS_ADMIN))
263 return v4l2_subdev_call(sd, core, s_register, p);
265 case VIDIOC_DBG_G_CHIP_INFO:
267 struct v4l2_dbg_chip_info *p = arg;
269 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
271 if (sd->ops->core && sd->ops->core->s_register)
272 p->flags |= V4L2_CHIP_FL_WRITABLE;
273 if (sd->ops->core && sd->ops->core->g_register)
274 p->flags |= V4L2_CHIP_FL_READABLE;
275 strscpy(p->name, sd->name, sizeof(p->name));
280 case VIDIOC_LOG_STATUS: {
283 pr_info("%s: ================= START STATUS =================\n",
285 ret = v4l2_subdev_call(sd, core, log_status);
286 pr_info("%s: ================== END STATUS ==================\n",
291 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
292 case VIDIOC_SUBDEV_G_FMT: {
293 struct v4l2_subdev_format *format = arg;
295 rval = check_format(sd, format);
299 memset(format->reserved, 0, sizeof(format->reserved));
300 memset(format->format.reserved, 0, sizeof(format->format.reserved));
301 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->pad, format);
304 case VIDIOC_SUBDEV_S_FMT: {
305 struct v4l2_subdev_format *format = arg;
307 rval = check_format(sd, format);
311 memset(format->reserved, 0, sizeof(format->reserved));
312 memset(format->format.reserved, 0, sizeof(format->format.reserved));
313 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->pad, format);
316 case VIDIOC_SUBDEV_G_CROP: {
317 struct v4l2_subdev_crop *crop = arg;
318 struct v4l2_subdev_selection sel;
320 rval = check_crop(sd, crop);
324 memset(crop->reserved, 0, sizeof(crop->reserved));
325 memset(&sel, 0, sizeof(sel));
326 sel.which = crop->which;
328 sel.target = V4L2_SEL_TGT_CROP;
330 rval = v4l2_subdev_call(
331 sd, pad, get_selection, subdev_fh->pad, &sel);
338 case VIDIOC_SUBDEV_S_CROP: {
339 struct v4l2_subdev_crop *crop = arg;
340 struct v4l2_subdev_selection sel;
342 memset(crop->reserved, 0, sizeof(crop->reserved));
343 rval = check_crop(sd, crop);
347 memset(&sel, 0, sizeof(sel));
348 sel.which = crop->which;
350 sel.target = V4L2_SEL_TGT_CROP;
353 rval = v4l2_subdev_call(
354 sd, pad, set_selection, subdev_fh->pad, &sel);
361 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
362 struct v4l2_subdev_mbus_code_enum *code = arg;
364 if (code->which != V4L2_SUBDEV_FORMAT_TRY &&
365 code->which != V4L2_SUBDEV_FORMAT_ACTIVE)
368 if (code->pad >= sd->entity.num_pads)
371 memset(code->reserved, 0, sizeof(code->reserved));
372 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->pad,
376 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
377 struct v4l2_subdev_frame_size_enum *fse = arg;
379 if (fse->which != V4L2_SUBDEV_FORMAT_TRY &&
380 fse->which != V4L2_SUBDEV_FORMAT_ACTIVE)
383 if (fse->pad >= sd->entity.num_pads)
386 memset(fse->reserved, 0, sizeof(fse->reserved));
387 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->pad,
391 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
392 struct v4l2_subdev_frame_interval *fi = arg;
394 if (fi->pad >= sd->entity.num_pads)
397 memset(fi->reserved, 0, sizeof(fi->reserved));
398 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
401 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
402 struct v4l2_subdev_frame_interval *fi = arg;
404 if (fi->pad >= sd->entity.num_pads)
407 memset(fi->reserved, 0, sizeof(fi->reserved));
408 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
411 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
412 struct v4l2_subdev_frame_interval_enum *fie = arg;
414 if (fie->which != V4L2_SUBDEV_FORMAT_TRY &&
415 fie->which != V4L2_SUBDEV_FORMAT_ACTIVE)
418 if (fie->pad >= sd->entity.num_pads)
421 memset(fie->reserved, 0, sizeof(fie->reserved));
422 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->pad,
426 case VIDIOC_SUBDEV_G_SELECTION: {
427 struct v4l2_subdev_selection *sel = arg;
429 rval = check_selection(sd, sel);
433 memset(sel->reserved, 0, sizeof(sel->reserved));
434 return v4l2_subdev_call(
435 sd, pad, get_selection, subdev_fh->pad, sel);
438 case VIDIOC_SUBDEV_S_SELECTION: {
439 struct v4l2_subdev_selection *sel = arg;
441 rval = check_selection(sd, sel);
445 memset(sel->reserved, 0, sizeof(sel->reserved));
446 return v4l2_subdev_call(
447 sd, pad, set_selection, subdev_fh->pad, sel);
450 case VIDIOC_G_EDID: {
451 struct v4l2_subdev_edid *edid = arg;
453 rval = check_edid(sd, edid);
457 return v4l2_subdev_call(sd, pad, get_edid, edid);
460 case VIDIOC_S_EDID: {
461 struct v4l2_subdev_edid *edid = arg;
463 rval = check_edid(sd, edid);
467 return v4l2_subdev_call(sd, pad, set_edid, edid);
470 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
471 struct v4l2_dv_timings_cap *cap = arg;
473 if (cap->pad >= sd->entity.num_pads)
476 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
479 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
480 struct v4l2_enum_dv_timings *dvt = arg;
482 if (dvt->pad >= sd->entity.num_pads)
485 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
488 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
489 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
491 case VIDIOC_SUBDEV_G_DV_TIMINGS:
492 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
494 case VIDIOC_SUBDEV_S_DV_TIMINGS:
495 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
497 case VIDIOC_SUBDEV_G_STD:
498 return v4l2_subdev_call(sd, video, g_std, arg);
500 case VIDIOC_SUBDEV_S_STD: {
501 v4l2_std_id *std = arg;
503 return v4l2_subdev_call(sd, video, s_std, *std);
506 case VIDIOC_SUBDEV_ENUMSTD: {
507 struct v4l2_standard *p = arg;
510 if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
513 return v4l_video_std_enumstd(p, id);
516 case VIDIOC_SUBDEV_QUERYSTD:
517 return v4l2_subdev_call(sd, video, querystd, arg);
520 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
526 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
528 struct video_device *vdev = video_devdata(file);
529 struct mutex *lock = vdev->lock;
532 if (lock && mutex_lock_interruptible(lock))
534 if (video_is_registered(vdev))
535 ret = subdev_do_ioctl(file, cmd, arg);
541 static long subdev_ioctl(struct file *file, unsigned int cmd,
544 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
548 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
551 struct video_device *vdev = video_devdata(file);
552 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
554 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
558 static __poll_t subdev_poll(struct file *file, poll_table *wait)
560 struct video_device *vdev = video_devdata(file);
561 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
562 struct v4l2_fh *fh = file->private_data;
564 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
567 poll_wait(file, &fh->wait, wait);
569 if (v4l2_event_pending(fh))
575 const struct v4l2_file_operations v4l2_subdev_fops = {
576 .owner = THIS_MODULE,
578 .unlocked_ioctl = subdev_ioctl,
580 .compat_ioctl32 = subdev_compat_ioctl32,
582 .release = subdev_close,
586 #ifdef CONFIG_MEDIA_CONTROLLER
587 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
588 struct media_link *link,
589 struct v4l2_subdev_format *source_fmt,
590 struct v4l2_subdev_format *sink_fmt)
592 /* The width, height and code must match. */
593 if (source_fmt->format.width != sink_fmt->format.width
594 || source_fmt->format.height != sink_fmt->format.height
595 || source_fmt->format.code != sink_fmt->format.code)
598 /* The field order must match, or the sink field order must be NONE
599 * to support interlaced hardware connected to bridges that support
600 * progressive formats only.
602 if (source_fmt->format.field != sink_fmt->format.field &&
603 sink_fmt->format.field != V4L2_FIELD_NONE)
608 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
611 v4l2_subdev_link_validate_get_format(struct media_pad *pad,
612 struct v4l2_subdev_format *fmt)
614 if (is_media_entity_v4l2_subdev(pad->entity)) {
615 struct v4l2_subdev *sd =
616 media_entity_to_v4l2_subdev(pad->entity);
618 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
619 fmt->pad = pad->index;
620 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
623 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
624 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
625 pad->entity->function, pad->entity->name);
630 int v4l2_subdev_link_validate(struct media_link *link)
632 struct v4l2_subdev *sink;
633 struct v4l2_subdev_format sink_fmt, source_fmt;
636 rval = v4l2_subdev_link_validate_get_format(
637 link->source, &source_fmt);
641 rval = v4l2_subdev_link_validate_get_format(
642 link->sink, &sink_fmt);
646 sink = media_entity_to_v4l2_subdev(link->sink->entity);
648 rval = v4l2_subdev_call(sink, pad, link_validate, link,
649 &source_fmt, &sink_fmt);
650 if (rval != -ENOIOCTLCMD)
653 return v4l2_subdev_link_validate_default(
654 sink, link, &source_fmt, &sink_fmt);
656 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
658 struct v4l2_subdev_pad_config *
659 v4l2_subdev_alloc_pad_config(struct v4l2_subdev *sd)
661 struct v4l2_subdev_pad_config *cfg;
664 if (!sd->entity.num_pads)
667 cfg = kvmalloc_array(sd->entity.num_pads, sizeof(*cfg),
668 GFP_KERNEL | __GFP_ZERO);
672 ret = v4l2_subdev_call(sd, pad, init_cfg, cfg);
673 if (ret < 0 && ret != -ENOIOCTLCMD) {
680 EXPORT_SYMBOL_GPL(v4l2_subdev_alloc_pad_config);
682 void v4l2_subdev_free_pad_config(struct v4l2_subdev_pad_config *cfg)
686 EXPORT_SYMBOL_GPL(v4l2_subdev_free_pad_config);
687 #endif /* CONFIG_MEDIA_CONTROLLER */
689 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
691 INIT_LIST_HEAD(&sd->list);
699 sd->host_priv = NULL;
700 #if defined(CONFIG_MEDIA_CONTROLLER)
701 sd->entity.name = sd->name;
702 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
703 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
706 EXPORT_SYMBOL(v4l2_subdev_init);
708 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
709 const struct v4l2_event *ev)
711 v4l2_event_queue(sd->devnode, ev);
712 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
714 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);