1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2010 Nokia Corporation
7 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 * Sakari Ailus <sakari.ailus@iki.fi>
11 #include <linux/ioctl.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/videodev2.h>
17 #include <linux/export.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/v4l2-fh.h>
23 #include <media/v4l2-event.h>
25 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
27 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
28 if (sd->entity.num_pads) {
29 fh->pad = v4l2_subdev_alloc_pad_config(sd);
37 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
39 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
40 v4l2_subdev_free_pad_config(fh->pad);
45 static int subdev_open(struct file *file)
47 struct video_device *vdev = video_devdata(file);
48 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
49 struct v4l2_subdev_fh *subdev_fh;
52 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
53 if (subdev_fh == NULL)
56 ret = subdev_fh_init(subdev_fh, sd);
62 v4l2_fh_init(&subdev_fh->vfh, vdev);
63 v4l2_fh_add(&subdev_fh->vfh);
64 file->private_data = &subdev_fh->vfh;
65 #if defined(CONFIG_MEDIA_CONTROLLER)
66 if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
69 owner = sd->entity.graph_obj.mdev->dev->driver->owner;
70 if (!try_module_get(owner)) {
74 subdev_fh->owner = owner;
78 if (sd->internal_ops && sd->internal_ops->open) {
79 ret = sd->internal_ops->open(sd, subdev_fh);
87 module_put(subdev_fh->owner);
88 v4l2_fh_del(&subdev_fh->vfh);
89 v4l2_fh_exit(&subdev_fh->vfh);
90 subdev_fh_free(subdev_fh);
96 static int subdev_close(struct file *file)
98 struct video_device *vdev = video_devdata(file);
99 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
100 struct v4l2_fh *vfh = file->private_data;
101 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
103 if (sd->internal_ops && sd->internal_ops->close)
104 sd->internal_ops->close(sd, subdev_fh);
105 module_put(subdev_fh->owner);
108 subdev_fh_free(subdev_fh);
110 file->private_data = NULL;
115 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
116 static int check_format(struct v4l2_subdev *sd,
117 struct v4l2_subdev_format *format)
119 if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
120 format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
123 if (format->pad >= sd->entity.num_pads)
129 static int check_crop(struct v4l2_subdev *sd, struct v4l2_subdev_crop *crop)
131 if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
132 crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
135 if (crop->pad >= sd->entity.num_pads)
141 static int check_selection(struct v4l2_subdev *sd,
142 struct v4l2_subdev_selection *sel)
144 if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
145 sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
148 if (sel->pad >= sd->entity.num_pads)
154 static int check_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
156 if (edid->pad >= sd->entity.num_pads)
159 if (edid->blocks && edid->edid == NULL)
166 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
168 struct video_device *vdev = video_devdata(file);
169 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
170 struct v4l2_fh *vfh = file->private_data;
171 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
172 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
177 case VIDIOC_QUERYCTRL:
179 * TODO: this really should be folded into v4l2_queryctrl (this
180 * currently returns -EINVAL for NULL control handlers).
181 * However, v4l2_queryctrl() is still called directly by
182 * drivers as well and until that has been addressed I believe
183 * it is safer to do the check here. The same is true for the
184 * other control ioctls below.
186 if (!vfh->ctrl_handler)
188 return v4l2_queryctrl(vfh->ctrl_handler, arg);
190 case VIDIOC_QUERY_EXT_CTRL:
191 if (!vfh->ctrl_handler)
193 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
195 case VIDIOC_QUERYMENU:
196 if (!vfh->ctrl_handler)
198 return v4l2_querymenu(vfh->ctrl_handler, arg);
201 if (!vfh->ctrl_handler)
203 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
206 if (!vfh->ctrl_handler)
208 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
210 case VIDIOC_G_EXT_CTRLS:
211 if (!vfh->ctrl_handler)
213 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
214 sd->v4l2_dev->mdev, arg);
216 case VIDIOC_S_EXT_CTRLS:
217 if (!vfh->ctrl_handler)
219 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
220 sd->v4l2_dev->mdev, arg);
222 case VIDIOC_TRY_EXT_CTRLS:
223 if (!vfh->ctrl_handler)
225 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
226 sd->v4l2_dev->mdev, arg);
229 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
232 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
234 case VIDIOC_SUBSCRIBE_EVENT:
235 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
237 case VIDIOC_UNSUBSCRIBE_EVENT:
238 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
240 #ifdef CONFIG_VIDEO_ADV_DEBUG
241 case VIDIOC_DBG_G_REGISTER:
243 struct v4l2_dbg_register *p = arg;
245 if (!capable(CAP_SYS_ADMIN))
247 return v4l2_subdev_call(sd, core, g_register, p);
249 case VIDIOC_DBG_S_REGISTER:
251 struct v4l2_dbg_register *p = arg;
253 if (!capable(CAP_SYS_ADMIN))
255 return v4l2_subdev_call(sd, core, s_register, p);
257 case VIDIOC_DBG_G_CHIP_INFO:
259 struct v4l2_dbg_chip_info *p = arg;
261 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
263 if (sd->ops->core && sd->ops->core->s_register)
264 p->flags |= V4L2_CHIP_FL_WRITABLE;
265 if (sd->ops->core && sd->ops->core->g_register)
266 p->flags |= V4L2_CHIP_FL_READABLE;
267 strscpy(p->name, sd->name, sizeof(p->name));
272 case VIDIOC_LOG_STATUS: {
275 pr_info("%s: ================= START STATUS =================\n",
277 ret = v4l2_subdev_call(sd, core, log_status);
278 pr_info("%s: ================== END STATUS ==================\n",
283 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
284 case VIDIOC_SUBDEV_G_FMT: {
285 struct v4l2_subdev_format *format = arg;
287 rval = check_format(sd, format);
291 memset(format->reserved, 0, sizeof(format->reserved));
292 memset(format->format.reserved, 0, sizeof(format->format.reserved));
293 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->pad, format);
296 case VIDIOC_SUBDEV_S_FMT: {
297 struct v4l2_subdev_format *format = arg;
299 rval = check_format(sd, format);
303 memset(format->reserved, 0, sizeof(format->reserved));
304 memset(format->format.reserved, 0, sizeof(format->format.reserved));
305 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->pad, format);
308 case VIDIOC_SUBDEV_G_CROP: {
309 struct v4l2_subdev_crop *crop = arg;
310 struct v4l2_subdev_selection sel;
312 rval = check_crop(sd, crop);
316 memset(crop->reserved, 0, sizeof(crop->reserved));
317 memset(&sel, 0, sizeof(sel));
318 sel.which = crop->which;
320 sel.target = V4L2_SEL_TGT_CROP;
322 rval = v4l2_subdev_call(
323 sd, pad, get_selection, subdev_fh->pad, &sel);
330 case VIDIOC_SUBDEV_S_CROP: {
331 struct v4l2_subdev_crop *crop = arg;
332 struct v4l2_subdev_selection sel;
334 memset(crop->reserved, 0, sizeof(crop->reserved));
335 rval = check_crop(sd, crop);
339 memset(&sel, 0, sizeof(sel));
340 sel.which = crop->which;
342 sel.target = V4L2_SEL_TGT_CROP;
345 rval = v4l2_subdev_call(
346 sd, pad, set_selection, subdev_fh->pad, &sel);
353 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
354 struct v4l2_subdev_mbus_code_enum *code = arg;
356 if (code->which != V4L2_SUBDEV_FORMAT_TRY &&
357 code->which != V4L2_SUBDEV_FORMAT_ACTIVE)
360 if (code->pad >= sd->entity.num_pads)
363 memset(code->reserved, 0, sizeof(code->reserved));
364 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->pad,
368 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
369 struct v4l2_subdev_frame_size_enum *fse = arg;
371 if (fse->which != V4L2_SUBDEV_FORMAT_TRY &&
372 fse->which != V4L2_SUBDEV_FORMAT_ACTIVE)
375 if (fse->pad >= sd->entity.num_pads)
378 memset(fse->reserved, 0, sizeof(fse->reserved));
379 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->pad,
383 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
384 struct v4l2_subdev_frame_interval *fi = arg;
386 if (fi->pad >= sd->entity.num_pads)
389 memset(fi->reserved, 0, sizeof(fi->reserved));
390 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
393 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
394 struct v4l2_subdev_frame_interval *fi = arg;
396 if (fi->pad >= sd->entity.num_pads)
399 memset(fi->reserved, 0, sizeof(fi->reserved));
400 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
403 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
404 struct v4l2_subdev_frame_interval_enum *fie = arg;
406 if (fie->which != V4L2_SUBDEV_FORMAT_TRY &&
407 fie->which != V4L2_SUBDEV_FORMAT_ACTIVE)
410 if (fie->pad >= sd->entity.num_pads)
413 memset(fie->reserved, 0, sizeof(fie->reserved));
414 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->pad,
418 case VIDIOC_SUBDEV_G_SELECTION: {
419 struct v4l2_subdev_selection *sel = arg;
421 rval = check_selection(sd, sel);
425 memset(sel->reserved, 0, sizeof(sel->reserved));
426 return v4l2_subdev_call(
427 sd, pad, get_selection, subdev_fh->pad, sel);
430 case VIDIOC_SUBDEV_S_SELECTION: {
431 struct v4l2_subdev_selection *sel = arg;
433 rval = check_selection(sd, sel);
437 memset(sel->reserved, 0, sizeof(sel->reserved));
438 return v4l2_subdev_call(
439 sd, pad, set_selection, subdev_fh->pad, sel);
442 case VIDIOC_G_EDID: {
443 struct v4l2_subdev_edid *edid = arg;
445 rval = check_edid(sd, edid);
449 return v4l2_subdev_call(sd, pad, get_edid, edid);
452 case VIDIOC_S_EDID: {
453 struct v4l2_subdev_edid *edid = arg;
455 rval = check_edid(sd, edid);
459 return v4l2_subdev_call(sd, pad, set_edid, edid);
462 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
463 struct v4l2_dv_timings_cap *cap = arg;
465 if (cap->pad >= sd->entity.num_pads)
468 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
471 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
472 struct v4l2_enum_dv_timings *dvt = arg;
474 if (dvt->pad >= sd->entity.num_pads)
477 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
480 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
481 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
483 case VIDIOC_SUBDEV_G_DV_TIMINGS:
484 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
486 case VIDIOC_SUBDEV_S_DV_TIMINGS:
487 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
489 case VIDIOC_SUBDEV_G_STD:
490 return v4l2_subdev_call(sd, video, g_std, arg);
492 case VIDIOC_SUBDEV_S_STD: {
493 v4l2_std_id *std = arg;
495 return v4l2_subdev_call(sd, video, s_std, *std);
498 case VIDIOC_SUBDEV_ENUMSTD: {
499 struct v4l2_standard *p = arg;
502 if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
505 return v4l_video_std_enumstd(p, id);
508 case VIDIOC_SUBDEV_QUERYSTD:
509 return v4l2_subdev_call(sd, video, querystd, arg);
512 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
518 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
520 struct video_device *vdev = video_devdata(file);
521 struct mutex *lock = vdev->lock;
524 if (lock && mutex_lock_interruptible(lock))
526 if (video_is_registered(vdev))
527 ret = subdev_do_ioctl(file, cmd, arg);
533 static long subdev_ioctl(struct file *file, unsigned int cmd,
536 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
540 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
543 struct video_device *vdev = video_devdata(file);
544 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
546 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
550 static __poll_t subdev_poll(struct file *file, poll_table *wait)
552 struct video_device *vdev = video_devdata(file);
553 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
554 struct v4l2_fh *fh = file->private_data;
556 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
559 poll_wait(file, &fh->wait, wait);
561 if (v4l2_event_pending(fh))
567 const struct v4l2_file_operations v4l2_subdev_fops = {
568 .owner = THIS_MODULE,
570 .unlocked_ioctl = subdev_ioctl,
572 .compat_ioctl32 = subdev_compat_ioctl32,
574 .release = subdev_close,
578 #ifdef CONFIG_MEDIA_CONTROLLER
579 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
580 struct media_link *link,
581 struct v4l2_subdev_format *source_fmt,
582 struct v4l2_subdev_format *sink_fmt)
584 /* The width, height and code must match. */
585 if (source_fmt->format.width != sink_fmt->format.width
586 || source_fmt->format.height != sink_fmt->format.height
587 || source_fmt->format.code != sink_fmt->format.code)
590 /* The field order must match, or the sink field order must be NONE
591 * to support interlaced hardware connected to bridges that support
592 * progressive formats only.
594 if (source_fmt->format.field != sink_fmt->format.field &&
595 sink_fmt->format.field != V4L2_FIELD_NONE)
600 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
603 v4l2_subdev_link_validate_get_format(struct media_pad *pad,
604 struct v4l2_subdev_format *fmt)
606 if (is_media_entity_v4l2_subdev(pad->entity)) {
607 struct v4l2_subdev *sd =
608 media_entity_to_v4l2_subdev(pad->entity);
610 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
611 fmt->pad = pad->index;
612 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
615 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
616 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
617 pad->entity->function, pad->entity->name);
622 int v4l2_subdev_link_validate(struct media_link *link)
624 struct v4l2_subdev *sink;
625 struct v4l2_subdev_format sink_fmt, source_fmt;
628 rval = v4l2_subdev_link_validate_get_format(
629 link->source, &source_fmt);
633 rval = v4l2_subdev_link_validate_get_format(
634 link->sink, &sink_fmt);
638 sink = media_entity_to_v4l2_subdev(link->sink->entity);
640 rval = v4l2_subdev_call(sink, pad, link_validate, link,
641 &source_fmt, &sink_fmt);
642 if (rval != -ENOIOCTLCMD)
645 return v4l2_subdev_link_validate_default(
646 sink, link, &source_fmt, &sink_fmt);
648 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
650 struct v4l2_subdev_pad_config *
651 v4l2_subdev_alloc_pad_config(struct v4l2_subdev *sd)
653 struct v4l2_subdev_pad_config *cfg;
656 if (!sd->entity.num_pads)
659 cfg = kvmalloc_array(sd->entity.num_pads, sizeof(*cfg),
660 GFP_KERNEL | __GFP_ZERO);
664 ret = v4l2_subdev_call(sd, pad, init_cfg, cfg);
665 if (ret < 0 && ret != -ENOIOCTLCMD) {
672 EXPORT_SYMBOL_GPL(v4l2_subdev_alloc_pad_config);
674 void v4l2_subdev_free_pad_config(struct v4l2_subdev_pad_config *cfg)
678 EXPORT_SYMBOL_GPL(v4l2_subdev_free_pad_config);
679 #endif /* CONFIG_MEDIA_CONTROLLER */
681 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
683 INIT_LIST_HEAD(&sd->list);
691 sd->host_priv = NULL;
692 #if defined(CONFIG_MEDIA_CONTROLLER)
693 sd->entity.name = sd->name;
694 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
695 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
698 EXPORT_SYMBOL(v4l2_subdev_init);
700 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
701 const struct v4l2_event *ev)
703 v4l2_event_queue(sd->devnode, ev);
704 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
706 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);