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)
96 media_entity_put(entity
);
98 v4l2_fh_del(&subdev_fh
->vfh
);
99 v4l2_fh_exit(&subdev_fh
->vfh
);
100 subdev_fh_free(subdev_fh
);
106 static int subdev_close(struct file
*file
)
108 struct video_device
*vdev
= video_devdata(file
);
109 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
110 struct v4l2_fh
*vfh
= file
->private_data
;
111 struct v4l2_subdev_fh
*subdev_fh
= to_v4l2_subdev_fh(vfh
);
113 if (sd
->internal_ops
&& sd
->internal_ops
->close
)
114 sd
->internal_ops
->close(sd
, subdev_fh
);
115 #if defined(CONFIG_MEDIA_CONTROLLER)
116 if (sd
->v4l2_dev
->mdev
)
117 media_entity_put(&sd
->entity
);
121 subdev_fh_free(subdev_fh
);
123 file
->private_data
= NULL
;
128 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
129 static int check_format(struct v4l2_subdev
*sd
,
130 struct v4l2_subdev_format
*format
)
132 if (format
->which
!= V4L2_SUBDEV_FORMAT_TRY
&&
133 format
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
136 if (format
->pad
>= sd
->entity
.num_pads
)
142 static int check_crop(struct v4l2_subdev
*sd
, struct v4l2_subdev_crop
*crop
)
144 if (crop
->which
!= V4L2_SUBDEV_FORMAT_TRY
&&
145 crop
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
148 if (crop
->pad
>= sd
->entity
.num_pads
)
154 static int check_selection(struct v4l2_subdev
*sd
,
155 struct v4l2_subdev_selection
*sel
)
157 if (sel
->which
!= V4L2_SUBDEV_FORMAT_TRY
&&
158 sel
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
161 if (sel
->pad
>= sd
->entity
.num_pads
)
167 static int check_edid(struct v4l2_subdev
*sd
, struct v4l2_subdev_edid
*edid
)
169 if (edid
->pad
>= sd
->entity
.num_pads
)
172 if (edid
->blocks
&& edid
->edid
== NULL
)
179 static long subdev_do_ioctl(struct file
*file
, unsigned int cmd
, void *arg
)
181 struct video_device
*vdev
= video_devdata(file
);
182 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
183 struct v4l2_fh
*vfh
= file
->private_data
;
184 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
185 struct v4l2_subdev_fh
*subdev_fh
= to_v4l2_subdev_fh(vfh
);
190 case VIDIOC_QUERYCTRL
:
191 return v4l2_queryctrl(vfh
->ctrl_handler
, arg
);
193 case VIDIOC_QUERY_EXT_CTRL
:
194 return v4l2_query_ext_ctrl(vfh
->ctrl_handler
, arg
);
196 case VIDIOC_QUERYMENU
:
197 return v4l2_querymenu(vfh
->ctrl_handler
, arg
);
200 return v4l2_g_ctrl(vfh
->ctrl_handler
, arg
);
203 return v4l2_s_ctrl(vfh
, vfh
->ctrl_handler
, arg
);
205 case VIDIOC_G_EXT_CTRLS
:
206 return v4l2_g_ext_ctrls(vfh
->ctrl_handler
, arg
);
208 case VIDIOC_S_EXT_CTRLS
:
209 return v4l2_s_ext_ctrls(vfh
, vfh
->ctrl_handler
, arg
);
211 case VIDIOC_TRY_EXT_CTRLS
:
212 return v4l2_try_ext_ctrls(vfh
->ctrl_handler
, arg
);
215 if (!(sd
->flags
& V4L2_SUBDEV_FL_HAS_EVENTS
))
218 return v4l2_event_dequeue(vfh
, arg
, file
->f_flags
& O_NONBLOCK
);
220 case VIDIOC_SUBSCRIBE_EVENT
:
221 return v4l2_subdev_call(sd
, core
, subscribe_event
, vfh
, arg
);
223 case VIDIOC_UNSUBSCRIBE_EVENT
:
224 return v4l2_subdev_call(sd
, core
, unsubscribe_event
, vfh
, arg
);
226 #ifdef CONFIG_VIDEO_ADV_DEBUG
227 case VIDIOC_DBG_G_REGISTER
:
229 struct v4l2_dbg_register
*p
= arg
;
231 if (!capable(CAP_SYS_ADMIN
))
233 return v4l2_subdev_call(sd
, core
, g_register
, p
);
235 case VIDIOC_DBG_S_REGISTER
:
237 struct v4l2_dbg_register
*p
= arg
;
239 if (!capable(CAP_SYS_ADMIN
))
241 return v4l2_subdev_call(sd
, core
, s_register
, p
);
245 case VIDIOC_LOG_STATUS
: {
248 pr_info("%s: ================= START STATUS =================\n",
250 ret
= v4l2_subdev_call(sd
, core
, log_status
);
251 pr_info("%s: ================== END STATUS ==================\n",
256 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
257 case VIDIOC_SUBDEV_G_FMT
: {
258 struct v4l2_subdev_format
*format
= arg
;
260 rval
= check_format(sd
, format
);
264 return v4l2_subdev_call(sd
, pad
, get_fmt
, subdev_fh
->pad
, format
);
267 case VIDIOC_SUBDEV_S_FMT
: {
268 struct v4l2_subdev_format
*format
= arg
;
270 rval
= check_format(sd
, format
);
274 return v4l2_subdev_call(sd
, pad
, set_fmt
, subdev_fh
->pad
, format
);
277 case VIDIOC_SUBDEV_G_CROP
: {
278 struct v4l2_subdev_crop
*crop
= arg
;
279 struct v4l2_subdev_selection sel
;
281 rval
= check_crop(sd
, crop
);
285 memset(&sel
, 0, sizeof(sel
));
286 sel
.which
= crop
->which
;
288 sel
.target
= V4L2_SEL_TGT_CROP
;
290 rval
= v4l2_subdev_call(
291 sd
, pad
, get_selection
, subdev_fh
->pad
, &sel
);
298 case VIDIOC_SUBDEV_S_CROP
: {
299 struct v4l2_subdev_crop
*crop
= arg
;
300 struct v4l2_subdev_selection sel
;
302 rval
= check_crop(sd
, crop
);
306 memset(&sel
, 0, sizeof(sel
));
307 sel
.which
= crop
->which
;
309 sel
.target
= V4L2_SEL_TGT_CROP
;
312 rval
= v4l2_subdev_call(
313 sd
, pad
, set_selection
, subdev_fh
->pad
, &sel
);
320 case VIDIOC_SUBDEV_ENUM_MBUS_CODE
: {
321 struct v4l2_subdev_mbus_code_enum
*code
= arg
;
323 if (code
->which
!= V4L2_SUBDEV_FORMAT_TRY
&&
324 code
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
327 if (code
->pad
>= sd
->entity
.num_pads
)
330 return v4l2_subdev_call(sd
, pad
, enum_mbus_code
, subdev_fh
->pad
,
334 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE
: {
335 struct v4l2_subdev_frame_size_enum
*fse
= arg
;
337 if (fse
->which
!= V4L2_SUBDEV_FORMAT_TRY
&&
338 fse
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
341 if (fse
->pad
>= sd
->entity
.num_pads
)
344 return v4l2_subdev_call(sd
, pad
, enum_frame_size
, subdev_fh
->pad
,
348 case VIDIOC_SUBDEV_G_FRAME_INTERVAL
: {
349 struct v4l2_subdev_frame_interval
*fi
= arg
;
351 if (fi
->pad
>= sd
->entity
.num_pads
)
354 return v4l2_subdev_call(sd
, video
, g_frame_interval
, arg
);
357 case VIDIOC_SUBDEV_S_FRAME_INTERVAL
: {
358 struct v4l2_subdev_frame_interval
*fi
= arg
;
360 if (fi
->pad
>= sd
->entity
.num_pads
)
363 return v4l2_subdev_call(sd
, video
, s_frame_interval
, arg
);
366 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL
: {
367 struct v4l2_subdev_frame_interval_enum
*fie
= arg
;
369 if (fie
->which
!= V4L2_SUBDEV_FORMAT_TRY
&&
370 fie
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
373 if (fie
->pad
>= sd
->entity
.num_pads
)
376 return v4l2_subdev_call(sd
, pad
, enum_frame_interval
, subdev_fh
->pad
,
380 case VIDIOC_SUBDEV_G_SELECTION
: {
381 struct v4l2_subdev_selection
*sel
= arg
;
383 rval
= check_selection(sd
, sel
);
387 return v4l2_subdev_call(
388 sd
, pad
, get_selection
, subdev_fh
->pad
, sel
);
391 case VIDIOC_SUBDEV_S_SELECTION
: {
392 struct v4l2_subdev_selection
*sel
= arg
;
394 rval
= check_selection(sd
, sel
);
398 return v4l2_subdev_call(
399 sd
, pad
, set_selection
, subdev_fh
->pad
, sel
);
402 case VIDIOC_G_EDID
: {
403 struct v4l2_subdev_edid
*edid
= arg
;
405 rval
= check_edid(sd
, edid
);
409 return v4l2_subdev_call(sd
, pad
, get_edid
, edid
);
412 case VIDIOC_S_EDID
: {
413 struct v4l2_subdev_edid
*edid
= arg
;
415 rval
= check_edid(sd
, edid
);
419 return v4l2_subdev_call(sd
, pad
, set_edid
, edid
);
422 case VIDIOC_SUBDEV_DV_TIMINGS_CAP
: {
423 struct v4l2_dv_timings_cap
*cap
= arg
;
425 if (cap
->pad
>= sd
->entity
.num_pads
)
428 return v4l2_subdev_call(sd
, pad
, dv_timings_cap
, cap
);
431 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS
: {
432 struct v4l2_enum_dv_timings
*dvt
= arg
;
434 if (dvt
->pad
>= sd
->entity
.num_pads
)
437 return v4l2_subdev_call(sd
, pad
, enum_dv_timings
, dvt
);
440 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS
:
441 return v4l2_subdev_call(sd
, video
, query_dv_timings
, arg
);
443 case VIDIOC_SUBDEV_G_DV_TIMINGS
:
444 return v4l2_subdev_call(sd
, video
, g_dv_timings
, arg
);
446 case VIDIOC_SUBDEV_S_DV_TIMINGS
:
447 return v4l2_subdev_call(sd
, video
, s_dv_timings
, arg
);
450 return v4l2_subdev_call(sd
, core
, ioctl
, cmd
, arg
);
456 static long subdev_ioctl(struct file
*file
, unsigned int cmd
,
459 return video_usercopy(file
, cmd
, arg
, subdev_do_ioctl
);
463 static long subdev_compat_ioctl32(struct file
*file
, unsigned int cmd
,
466 struct video_device
*vdev
= video_devdata(file
);
467 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
469 return v4l2_subdev_call(sd
, core
, compat_ioctl32
, cmd
, arg
);
473 static unsigned int subdev_poll(struct file
*file
, poll_table
*wait
)
475 struct video_device
*vdev
= video_devdata(file
);
476 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
477 struct v4l2_fh
*fh
= file
->private_data
;
479 if (!(sd
->flags
& V4L2_SUBDEV_FL_HAS_EVENTS
))
482 poll_wait(file
, &fh
->wait
, wait
);
484 if (v4l2_event_pending(fh
))
490 const struct v4l2_file_operations v4l2_subdev_fops
= {
491 .owner
= THIS_MODULE
,
493 .unlocked_ioctl
= subdev_ioctl
,
495 .compat_ioctl32
= subdev_compat_ioctl32
,
497 .release
= subdev_close
,
501 #ifdef CONFIG_MEDIA_CONTROLLER
502 int v4l2_subdev_link_validate_default(struct v4l2_subdev
*sd
,
503 struct media_link
*link
,
504 struct v4l2_subdev_format
*source_fmt
,
505 struct v4l2_subdev_format
*sink_fmt
)
507 /* The width, height and code must match. */
508 if (source_fmt
->format
.width
!= sink_fmt
->format
.width
509 || source_fmt
->format
.height
!= sink_fmt
->format
.height
510 || source_fmt
->format
.code
!= sink_fmt
->format
.code
)
513 /* The field order must match, or the sink field order must be NONE
514 * to support interlaced hardware connected to bridges that support
515 * progressive formats only.
517 if (source_fmt
->format
.field
!= sink_fmt
->format
.field
&&
518 sink_fmt
->format
.field
!= V4L2_FIELD_NONE
)
523 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default
);
526 v4l2_subdev_link_validate_get_format(struct media_pad
*pad
,
527 struct v4l2_subdev_format
*fmt
)
529 if (media_entity_type(pad
->entity
) == MEDIA_ENT_T_V4L2_SUBDEV
) {
530 struct v4l2_subdev
*sd
=
531 media_entity_to_v4l2_subdev(pad
->entity
);
533 fmt
->which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
534 fmt
->pad
= pad
->index
;
535 return v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, fmt
);
538 WARN(pad
->entity
->type
!= MEDIA_ENT_T_DEVNODE_V4L
,
539 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
540 pad
->entity
->type
, pad
->entity
->name
);
545 int v4l2_subdev_link_validate(struct media_link
*link
)
547 struct v4l2_subdev
*sink
;
548 struct v4l2_subdev_format sink_fmt
, source_fmt
;
551 rval
= v4l2_subdev_link_validate_get_format(
552 link
->source
, &source_fmt
);
556 rval
= v4l2_subdev_link_validate_get_format(
557 link
->sink
, &sink_fmt
);
561 sink
= media_entity_to_v4l2_subdev(link
->sink
->entity
);
563 rval
= v4l2_subdev_call(sink
, pad
, link_validate
, link
,
564 &source_fmt
, &sink_fmt
);
565 if (rval
!= -ENOIOCTLCMD
)
568 return v4l2_subdev_link_validate_default(
569 sink
, link
, &source_fmt
, &sink_fmt
);
571 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate
);
572 #endif /* CONFIG_MEDIA_CONTROLLER */
574 void v4l2_subdev_init(struct v4l2_subdev
*sd
, const struct v4l2_subdev_ops
*ops
)
576 INIT_LIST_HEAD(&sd
->list
);
584 sd
->host_priv
= NULL
;
585 #if defined(CONFIG_MEDIA_CONTROLLER)
586 sd
->entity
.name
= sd
->name
;
587 sd
->entity
.type
= MEDIA_ENT_T_V4L2_SUBDEV
;
590 EXPORT_SYMBOL(v4l2_subdev_init
);
593 * v4l2_subdev_notify_event() - Delivers event notification for subdevice
594 * @sd: The subdev for which to deliver the event
595 * @ev: The event to deliver
597 * Will deliver the specified event to all userspace event listeners which are
598 * subscribed to the v42l subdev event queue as well as to the bridge driver
599 * using the notify callback. The notification type for the notify callback
600 * will be V4L2_DEVICE_NOTIFY_EVENT.
602 void v4l2_subdev_notify_event(struct v4l2_subdev
*sd
,
603 const struct v4l2_event
*ev
)
605 v4l2_event_queue(sd
->devnode
, ev
);
606 v4l2_subdev_notify(sd
, V4L2_DEVICE_NOTIFY_EVENT
, (void *)ev
);
608 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event
);