1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2010 Nokia Corporation
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>
18 #include <linux/version.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-event.h>
26 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
27 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
29 struct v4l2_subdev_state *state;
31 state = v4l2_subdev_alloc_state(sd);
33 return PTR_ERR(state);
40 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
42 v4l2_subdev_free_state(fh->state);
46 static int subdev_open(struct file *file)
48 struct video_device *vdev = video_devdata(file);
49 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
50 struct v4l2_subdev_fh *subdev_fh;
53 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
54 if (subdev_fh == NULL)
57 ret = subdev_fh_init(subdev_fh, sd);
63 v4l2_fh_init(&subdev_fh->vfh, vdev);
64 v4l2_fh_add(&subdev_fh->vfh);
65 file->private_data = &subdev_fh->vfh;
66 #if defined(CONFIG_MEDIA_CONTROLLER)
67 if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
70 owner = sd->entity.graph_obj.mdev->dev->driver->owner;
71 if (!try_module_get(owner)) {
75 subdev_fh->owner = owner;
79 if (sd->internal_ops && sd->internal_ops->open) {
80 ret = sd->internal_ops->open(sd, subdev_fh);
88 module_put(subdev_fh->owner);
89 v4l2_fh_del(&subdev_fh->vfh);
90 v4l2_fh_exit(&subdev_fh->vfh);
91 subdev_fh_free(subdev_fh);
97 static int subdev_close(struct file *file)
99 struct video_device *vdev = video_devdata(file);
100 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
101 struct v4l2_fh *vfh = file->private_data;
102 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
104 if (sd->internal_ops && sd->internal_ops->close)
105 sd->internal_ops->close(sd, subdev_fh);
106 module_put(subdev_fh->owner);
109 subdev_fh_free(subdev_fh);
111 file->private_data = NULL;
115 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
116 static int subdev_open(struct file *file)
121 static int subdev_close(struct file *file)
125 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
127 static inline int check_which(u32 which)
129 if (which != V4L2_SUBDEV_FORMAT_TRY &&
130 which != V4L2_SUBDEV_FORMAT_ACTIVE)
136 static inline int check_pad(struct v4l2_subdev *sd, u32 pad)
138 #if defined(CONFIG_MEDIA_CONTROLLER)
139 if (sd->entity.num_pads) {
140 if (pad >= sd->entity.num_pads)
145 /* allow pad 0 on subdevices not registered as media entities */
151 static int check_state_pads(u32 which, struct v4l2_subdev_state *state)
153 if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads))
159 static inline int check_format(struct v4l2_subdev *sd,
160 struct v4l2_subdev_state *state,
161 struct v4l2_subdev_format *format)
166 return check_which(format->which) ? : check_pad(sd, format->pad) ? :
167 check_state_pads(format->which, state);
170 static int call_get_fmt(struct v4l2_subdev *sd,
171 struct v4l2_subdev_state *state,
172 struct v4l2_subdev_format *format)
174 return check_format(sd, state, format) ? :
175 sd->ops->pad->get_fmt(sd, state, format);
178 static int call_set_fmt(struct v4l2_subdev *sd,
179 struct v4l2_subdev_state *state,
180 struct v4l2_subdev_format *format)
182 return check_format(sd, state, format) ? :
183 sd->ops->pad->set_fmt(sd, state, format);
186 static int call_enum_mbus_code(struct v4l2_subdev *sd,
187 struct v4l2_subdev_state *state,
188 struct v4l2_subdev_mbus_code_enum *code)
193 return check_which(code->which) ? : check_pad(sd, code->pad) ? :
194 check_state_pads(code->which, state) ? :
195 sd->ops->pad->enum_mbus_code(sd, state, code);
198 static int call_enum_frame_size(struct v4l2_subdev *sd,
199 struct v4l2_subdev_state *state,
200 struct v4l2_subdev_frame_size_enum *fse)
205 return check_which(fse->which) ? : check_pad(sd, fse->pad) ? :
206 check_state_pads(fse->which, state) ? :
207 sd->ops->pad->enum_frame_size(sd, state, fse);
210 static inline int check_frame_interval(struct v4l2_subdev *sd,
211 struct v4l2_subdev_frame_interval *fi)
216 return check_pad(sd, fi->pad);
219 static int call_g_frame_interval(struct v4l2_subdev *sd,
220 struct v4l2_subdev_frame_interval *fi)
222 return check_frame_interval(sd, fi) ? :
223 sd->ops->video->g_frame_interval(sd, fi);
226 static int call_s_frame_interval(struct v4l2_subdev *sd,
227 struct v4l2_subdev_frame_interval *fi)
229 return check_frame_interval(sd, fi) ? :
230 sd->ops->video->s_frame_interval(sd, fi);
233 static int call_enum_frame_interval(struct v4l2_subdev *sd,
234 struct v4l2_subdev_state *state,
235 struct v4l2_subdev_frame_interval_enum *fie)
240 return check_which(fie->which) ? : check_pad(sd, fie->pad) ? :
241 check_state_pads(fie->which, state) ? :
242 sd->ops->pad->enum_frame_interval(sd, state, fie);
245 static inline int check_selection(struct v4l2_subdev *sd,
246 struct v4l2_subdev_state *state,
247 struct v4l2_subdev_selection *sel)
252 return check_which(sel->which) ? : check_pad(sd, sel->pad) ? :
253 check_state_pads(sel->which, state);
256 static int call_get_selection(struct v4l2_subdev *sd,
257 struct v4l2_subdev_state *state,
258 struct v4l2_subdev_selection *sel)
260 return check_selection(sd, state, sel) ? :
261 sd->ops->pad->get_selection(sd, state, sel);
264 static int call_set_selection(struct v4l2_subdev *sd,
265 struct v4l2_subdev_state *state,
266 struct v4l2_subdev_selection *sel)
268 return check_selection(sd, state, sel) ? :
269 sd->ops->pad->set_selection(sd, state, sel);
272 static inline int check_edid(struct v4l2_subdev *sd,
273 struct v4l2_subdev_edid *edid)
278 if (edid->blocks && edid->edid == NULL)
281 return check_pad(sd, edid->pad);
284 static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
286 return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid);
289 static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
291 return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid);
294 static int call_dv_timings_cap(struct v4l2_subdev *sd,
295 struct v4l2_dv_timings_cap *cap)
300 return check_pad(sd, cap->pad) ? :
301 sd->ops->pad->dv_timings_cap(sd, cap);
304 static int call_enum_dv_timings(struct v4l2_subdev *sd,
305 struct v4l2_enum_dv_timings *dvt)
310 return check_pad(sd, dvt->pad) ? :
311 sd->ops->pad->enum_dv_timings(sd, dvt);
314 static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
315 struct v4l2_mbus_config *config)
317 return check_pad(sd, pad) ? :
318 sd->ops->pad->get_mbus_config(sd, pad, config);
321 static int call_set_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
322 struct v4l2_mbus_config *config)
324 return check_pad(sd, pad) ? :
325 sd->ops->pad->get_mbus_config(sd, pad, config);
328 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = {
329 .get_fmt = call_get_fmt,
330 .set_fmt = call_set_fmt,
331 .enum_mbus_code = call_enum_mbus_code,
332 .enum_frame_size = call_enum_frame_size,
333 .enum_frame_interval = call_enum_frame_interval,
334 .get_selection = call_get_selection,
335 .set_selection = call_set_selection,
336 .get_edid = call_get_edid,
337 .set_edid = call_set_edid,
338 .dv_timings_cap = call_dv_timings_cap,
339 .enum_dv_timings = call_enum_dv_timings,
340 .get_mbus_config = call_get_mbus_config,
341 .set_mbus_config = call_set_mbus_config,
344 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = {
345 .g_frame_interval = call_g_frame_interval,
346 .s_frame_interval = call_s_frame_interval,
349 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = {
350 .pad = &v4l2_subdev_call_pad_wrappers,
351 .video = &v4l2_subdev_call_video_wrappers,
353 EXPORT_SYMBOL(v4l2_subdev_call_wrappers);
355 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
356 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
358 struct video_device *vdev = video_devdata(file);
359 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
360 struct v4l2_fh *vfh = file->private_data;
361 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
362 bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
366 case VIDIOC_SUBDEV_QUERYCAP: {
367 struct v4l2_subdev_capability *cap = arg;
369 memset(cap->reserved, 0, sizeof(cap->reserved));
370 cap->version = LINUX_VERSION_CODE;
371 cap->capabilities = ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0;
376 case VIDIOC_QUERYCTRL:
378 * TODO: this really should be folded into v4l2_queryctrl (this
379 * currently returns -EINVAL for NULL control handlers).
380 * However, v4l2_queryctrl() is still called directly by
381 * drivers as well and until that has been addressed I believe
382 * it is safer to do the check here. The same is true for the
383 * other control ioctls below.
385 if (!vfh->ctrl_handler)
387 return v4l2_queryctrl(vfh->ctrl_handler, arg);
389 case VIDIOC_QUERY_EXT_CTRL:
390 if (!vfh->ctrl_handler)
392 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
394 case VIDIOC_QUERYMENU:
395 if (!vfh->ctrl_handler)
397 return v4l2_querymenu(vfh->ctrl_handler, arg);
400 if (!vfh->ctrl_handler)
402 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
405 if (!vfh->ctrl_handler)
407 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
409 case VIDIOC_G_EXT_CTRLS:
410 if (!vfh->ctrl_handler)
412 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
413 vdev, sd->v4l2_dev->mdev, arg);
415 case VIDIOC_S_EXT_CTRLS:
416 if (!vfh->ctrl_handler)
418 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
419 vdev, sd->v4l2_dev->mdev, arg);
421 case VIDIOC_TRY_EXT_CTRLS:
422 if (!vfh->ctrl_handler)
424 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
425 vdev, sd->v4l2_dev->mdev, arg);
428 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
431 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
433 case VIDIOC_SUBSCRIBE_EVENT:
434 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
436 case VIDIOC_UNSUBSCRIBE_EVENT:
437 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
439 #ifdef CONFIG_VIDEO_ADV_DEBUG
440 case VIDIOC_DBG_G_REGISTER:
442 struct v4l2_dbg_register *p = arg;
444 if (!capable(CAP_SYS_ADMIN))
446 return v4l2_subdev_call(sd, core, g_register, p);
448 case VIDIOC_DBG_S_REGISTER:
450 struct v4l2_dbg_register *p = arg;
452 if (!capable(CAP_SYS_ADMIN))
454 return v4l2_subdev_call(sd, core, s_register, p);
456 case VIDIOC_DBG_G_CHIP_INFO:
458 struct v4l2_dbg_chip_info *p = arg;
460 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
462 if (sd->ops->core && sd->ops->core->s_register)
463 p->flags |= V4L2_CHIP_FL_WRITABLE;
464 if (sd->ops->core && sd->ops->core->g_register)
465 p->flags |= V4L2_CHIP_FL_READABLE;
466 strscpy(p->name, sd->name, sizeof(p->name));
471 case VIDIOC_LOG_STATUS: {
474 pr_info("%s: ================= START STATUS =================\n",
476 ret = v4l2_subdev_call(sd, core, log_status);
477 pr_info("%s: ================== END STATUS ==================\n",
482 case VIDIOC_SUBDEV_G_FMT: {
483 struct v4l2_subdev_format *format = arg;
485 memset(format->reserved, 0, sizeof(format->reserved));
486 memset(format->format.reserved, 0, sizeof(format->format.reserved));
487 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->state, format);
490 case VIDIOC_SUBDEV_S_FMT: {
491 struct v4l2_subdev_format *format = arg;
493 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
496 memset(format->reserved, 0, sizeof(format->reserved));
497 memset(format->format.reserved, 0, sizeof(format->format.reserved));
498 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->state, format);
501 case VIDIOC_SUBDEV_G_CROP: {
502 struct v4l2_subdev_crop *crop = arg;
503 struct v4l2_subdev_selection sel;
505 memset(crop->reserved, 0, sizeof(crop->reserved));
506 memset(&sel, 0, sizeof(sel));
507 sel.which = crop->which;
509 sel.target = V4L2_SEL_TGT_CROP;
511 rval = v4l2_subdev_call(
512 sd, pad, get_selection, subdev_fh->state, &sel);
519 case VIDIOC_SUBDEV_S_CROP: {
520 struct v4l2_subdev_crop *crop = arg;
521 struct v4l2_subdev_selection sel;
523 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
526 memset(crop->reserved, 0, sizeof(crop->reserved));
527 memset(&sel, 0, sizeof(sel));
528 sel.which = crop->which;
530 sel.target = V4L2_SEL_TGT_CROP;
533 rval = v4l2_subdev_call(
534 sd, pad, set_selection, subdev_fh->state, &sel);
541 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
542 struct v4l2_subdev_mbus_code_enum *code = arg;
544 memset(code->reserved, 0, sizeof(code->reserved));
545 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->state,
549 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
550 struct v4l2_subdev_frame_size_enum *fse = arg;
552 memset(fse->reserved, 0, sizeof(fse->reserved));
553 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->state,
557 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
558 struct v4l2_subdev_frame_interval *fi = arg;
560 memset(fi->reserved, 0, sizeof(fi->reserved));
561 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
564 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
565 struct v4l2_subdev_frame_interval *fi = arg;
570 memset(fi->reserved, 0, sizeof(fi->reserved));
571 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
574 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
575 struct v4l2_subdev_frame_interval_enum *fie = arg;
577 memset(fie->reserved, 0, sizeof(fie->reserved));
578 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->state,
582 case VIDIOC_SUBDEV_G_SELECTION: {
583 struct v4l2_subdev_selection *sel = arg;
585 memset(sel->reserved, 0, sizeof(sel->reserved));
586 return v4l2_subdev_call(
587 sd, pad, get_selection, subdev_fh->state, sel);
590 case VIDIOC_SUBDEV_S_SELECTION: {
591 struct v4l2_subdev_selection *sel = arg;
593 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
596 memset(sel->reserved, 0, sizeof(sel->reserved));
597 return v4l2_subdev_call(
598 sd, pad, set_selection, subdev_fh->state, sel);
601 case VIDIOC_G_EDID: {
602 struct v4l2_subdev_edid *edid = arg;
604 return v4l2_subdev_call(sd, pad, get_edid, edid);
607 case VIDIOC_S_EDID: {
608 struct v4l2_subdev_edid *edid = arg;
610 return v4l2_subdev_call(sd, pad, set_edid, edid);
613 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
614 struct v4l2_dv_timings_cap *cap = arg;
616 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
619 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
620 struct v4l2_enum_dv_timings *dvt = arg;
622 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
625 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
626 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
628 case VIDIOC_SUBDEV_G_DV_TIMINGS:
629 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
631 case VIDIOC_SUBDEV_S_DV_TIMINGS:
635 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
637 case VIDIOC_SUBDEV_G_STD:
638 return v4l2_subdev_call(sd, video, g_std, arg);
640 case VIDIOC_SUBDEV_S_STD: {
641 v4l2_std_id *std = arg;
646 return v4l2_subdev_call(sd, video, s_std, *std);
649 case VIDIOC_SUBDEV_ENUMSTD: {
650 struct v4l2_standard *p = arg;
653 if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
656 return v4l_video_std_enumstd(p, id);
659 case VIDIOC_SUBDEV_QUERYSTD:
660 return v4l2_subdev_call(sd, video, querystd, arg);
663 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
669 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
671 struct video_device *vdev = video_devdata(file);
672 struct mutex *lock = vdev->lock;
675 if (lock && mutex_lock_interruptible(lock))
677 if (video_is_registered(vdev))
678 ret = subdev_do_ioctl(file, cmd, arg);
684 static long subdev_ioctl(struct file *file, unsigned int cmd,
687 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
691 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
694 struct video_device *vdev = video_devdata(file);
695 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
697 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
701 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
702 static long subdev_ioctl(struct file *file, unsigned int cmd,
709 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
715 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
717 static __poll_t subdev_poll(struct file *file, poll_table *wait)
719 struct video_device *vdev = video_devdata(file);
720 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
721 struct v4l2_fh *fh = file->private_data;
723 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
726 poll_wait(file, &fh->wait, wait);
728 if (v4l2_event_pending(fh))
734 const struct v4l2_file_operations v4l2_subdev_fops = {
735 .owner = THIS_MODULE,
737 .unlocked_ioctl = subdev_ioctl,
739 .compat_ioctl32 = subdev_compat_ioctl32,
741 .release = subdev_close,
745 #ifdef CONFIG_MEDIA_CONTROLLER
747 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
748 struct fwnode_endpoint *endpoint)
750 struct fwnode_handle *fwnode;
751 struct v4l2_subdev *sd;
753 if (!is_media_entity_v4l2_subdev(entity))
756 sd = media_entity_to_v4l2_subdev(entity);
758 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
759 fwnode_handle_put(fwnode);
761 if (dev_fwnode(sd->dev) == fwnode)
762 return endpoint->port;
766 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
768 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
769 struct media_link *link,
770 struct v4l2_subdev_format *source_fmt,
771 struct v4l2_subdev_format *sink_fmt)
775 /* The width, height and code must match. */
776 if (source_fmt->format.width != sink_fmt->format.width) {
777 dev_dbg(sd->entity.graph_obj.mdev->dev,
778 "%s: width does not match (source %u, sink %u)\n",
780 source_fmt->format.width, sink_fmt->format.width);
784 if (source_fmt->format.height != sink_fmt->format.height) {
785 dev_dbg(sd->entity.graph_obj.mdev->dev,
786 "%s: height does not match (source %u, sink %u)\n",
788 source_fmt->format.height, sink_fmt->format.height);
792 if (source_fmt->format.code != sink_fmt->format.code) {
793 dev_dbg(sd->entity.graph_obj.mdev->dev,
794 "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
796 source_fmt->format.code, sink_fmt->format.code);
800 /* The field order must match, or the sink field order must be NONE
801 * to support interlaced hardware connected to bridges that support
802 * progressive formats only.
804 if (source_fmt->format.field != sink_fmt->format.field &&
805 sink_fmt->format.field != V4L2_FIELD_NONE) {
806 dev_dbg(sd->entity.graph_obj.mdev->dev,
807 "%s: field does not match (source %u, sink %u)\n",
809 source_fmt->format.field, sink_fmt->format.field);
816 dev_dbg(sd->entity.graph_obj.mdev->dev,
817 "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__,
818 link->source->entity->name, link->source->index,
819 link->sink->entity->name, link->sink->index);
823 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
826 v4l2_subdev_link_validate_get_format(struct media_pad *pad,
827 struct v4l2_subdev_format *fmt)
829 if (is_media_entity_v4l2_subdev(pad->entity)) {
830 struct v4l2_subdev *sd =
831 media_entity_to_v4l2_subdev(pad->entity);
833 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
834 fmt->pad = pad->index;
835 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
838 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
839 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
840 pad->entity->function, pad->entity->name);
845 int v4l2_subdev_link_validate(struct media_link *link)
847 struct v4l2_subdev *sink;
848 struct v4l2_subdev_format sink_fmt, source_fmt;
851 rval = v4l2_subdev_link_validate_get_format(
852 link->source, &source_fmt);
856 rval = v4l2_subdev_link_validate_get_format(
857 link->sink, &sink_fmt);
861 sink = media_entity_to_v4l2_subdev(link->sink->entity);
863 rval = v4l2_subdev_call(sink, pad, link_validate, link,
864 &source_fmt, &sink_fmt);
865 if (rval != -ENOIOCTLCMD)
868 return v4l2_subdev_link_validate_default(
869 sink, link, &source_fmt, &sink_fmt);
871 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
873 struct v4l2_subdev_state *v4l2_subdev_alloc_state(struct v4l2_subdev *sd)
875 struct v4l2_subdev_state *state;
878 state = kzalloc(sizeof(*state), GFP_KERNEL);
880 return ERR_PTR(-ENOMEM);
882 if (sd->entity.num_pads) {
883 state->pads = kvmalloc_array(sd->entity.num_pads,
884 sizeof(*state->pads),
885 GFP_KERNEL | __GFP_ZERO);
892 ret = v4l2_subdev_call(sd, pad, init_cfg, state);
893 if (ret < 0 && ret != -ENOIOCTLCMD)
899 if (state && state->pads)
906 EXPORT_SYMBOL_GPL(v4l2_subdev_alloc_state);
908 void v4l2_subdev_free_state(struct v4l2_subdev_state *state)
916 EXPORT_SYMBOL_GPL(v4l2_subdev_free_state);
918 #endif /* CONFIG_MEDIA_CONTROLLER */
920 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
922 INIT_LIST_HEAD(&sd->list);
930 sd->host_priv = NULL;
931 #if defined(CONFIG_MEDIA_CONTROLLER)
932 sd->entity.name = sd->name;
933 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
934 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
937 EXPORT_SYMBOL(v4l2_subdev_init);
939 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
940 const struct v4l2_event *ev)
942 v4l2_event_queue(sd->devnode, ev);
943 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
945 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);