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>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-fh.h>
32 #include <media/v4l2-event.h>
34 static int subdev_fh_init(struct v4l2_subdev_fh
*fh
, struct v4l2_subdev
*sd
)
36 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
37 /* Allocate try format and crop in the same memory block */
38 fh
->try_fmt
= kzalloc((sizeof(*fh
->try_fmt
) + sizeof(*fh
->try_crop
))
39 * sd
->entity
.num_pads
, GFP_KERNEL
);
40 if (fh
->try_fmt
== NULL
)
43 fh
->try_crop
= (struct v4l2_rect
*)
44 (fh
->try_fmt
+ sd
->entity
.num_pads
);
49 static void subdev_fh_free(struct v4l2_subdev_fh
*fh
)
51 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
58 static int subdev_open(struct file
*file
)
60 struct video_device
*vdev
= video_devdata(file
);
61 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
62 struct v4l2_subdev_fh
*subdev_fh
;
63 #if defined(CONFIG_MEDIA_CONTROLLER)
64 struct media_entity
*entity
= NULL
;
68 subdev_fh
= kzalloc(sizeof(*subdev_fh
), GFP_KERNEL
);
69 if (subdev_fh
== NULL
)
72 ret
= subdev_fh_init(subdev_fh
, sd
);
78 v4l2_fh_init(&subdev_fh
->vfh
, vdev
);
79 v4l2_fh_add(&subdev_fh
->vfh
);
80 file
->private_data
= &subdev_fh
->vfh
;
81 #if defined(CONFIG_MEDIA_CONTROLLER)
82 if (sd
->v4l2_dev
->mdev
) {
83 entity
= media_entity_get(&sd
->entity
);
91 if (sd
->internal_ops
&& sd
->internal_ops
->open
) {
92 ret
= sd
->internal_ops
->open(sd
, subdev_fh
);
100 #if defined(CONFIG_MEDIA_CONTROLLER)
102 media_entity_put(entity
);
104 v4l2_fh_del(&subdev_fh
->vfh
);
105 v4l2_fh_exit(&subdev_fh
->vfh
);
106 subdev_fh_free(subdev_fh
);
112 static int subdev_close(struct file
*file
)
114 struct video_device
*vdev
= video_devdata(file
);
115 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
116 struct v4l2_fh
*vfh
= file
->private_data
;
117 struct v4l2_subdev_fh
*subdev_fh
= to_v4l2_subdev_fh(vfh
);
119 if (sd
->internal_ops
&& sd
->internal_ops
->close
)
120 sd
->internal_ops
->close(sd
, subdev_fh
);
121 #if defined(CONFIG_MEDIA_CONTROLLER)
122 if (sd
->v4l2_dev
->mdev
)
123 media_entity_put(&sd
->entity
);
127 subdev_fh_free(subdev_fh
);
129 file
->private_data
= NULL
;
134 static long subdev_do_ioctl(struct file
*file
, unsigned int cmd
, void *arg
)
136 struct video_device
*vdev
= video_devdata(file
);
137 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
138 struct v4l2_fh
*vfh
= file
->private_data
;
139 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
140 struct v4l2_subdev_fh
*subdev_fh
= to_v4l2_subdev_fh(vfh
);
144 case VIDIOC_QUERYCTRL
:
145 return v4l2_queryctrl(vfh
->ctrl_handler
, arg
);
147 case VIDIOC_QUERYMENU
:
148 return v4l2_querymenu(vfh
->ctrl_handler
, arg
);
151 return v4l2_g_ctrl(vfh
->ctrl_handler
, arg
);
154 return v4l2_s_ctrl(vfh
, vfh
->ctrl_handler
, arg
);
156 case VIDIOC_G_EXT_CTRLS
:
157 return v4l2_g_ext_ctrls(vfh
->ctrl_handler
, arg
);
159 case VIDIOC_S_EXT_CTRLS
:
160 return v4l2_s_ext_ctrls(vfh
, vfh
->ctrl_handler
, arg
);
162 case VIDIOC_TRY_EXT_CTRLS
:
163 return v4l2_try_ext_ctrls(vfh
->ctrl_handler
, arg
);
166 if (!(sd
->flags
& V4L2_SUBDEV_FL_HAS_EVENTS
))
169 return v4l2_event_dequeue(vfh
, arg
, file
->f_flags
& O_NONBLOCK
);
171 case VIDIOC_SUBSCRIBE_EVENT
:
172 return v4l2_subdev_call(sd
, core
, subscribe_event
, vfh
, arg
);
174 case VIDIOC_UNSUBSCRIBE_EVENT
:
175 return v4l2_subdev_call(sd
, core
, unsubscribe_event
, vfh
, arg
);
176 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
177 case VIDIOC_SUBDEV_G_FMT
: {
178 struct v4l2_subdev_format
*format
= arg
;
180 if (format
->which
!= V4L2_SUBDEV_FORMAT_TRY
&&
181 format
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
184 if (format
->pad
>= sd
->entity
.num_pads
)
187 return v4l2_subdev_call(sd
, pad
, get_fmt
, subdev_fh
, format
);
190 case VIDIOC_SUBDEV_S_FMT
: {
191 struct v4l2_subdev_format
*format
= arg
;
193 if (format
->which
!= V4L2_SUBDEV_FORMAT_TRY
&&
194 format
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
197 if (format
->pad
>= sd
->entity
.num_pads
)
200 return v4l2_subdev_call(sd
, pad
, set_fmt
, subdev_fh
, format
);
203 case VIDIOC_SUBDEV_G_CROP
: {
204 struct v4l2_subdev_crop
*crop
= arg
;
206 if (crop
->which
!= V4L2_SUBDEV_FORMAT_TRY
&&
207 crop
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
210 if (crop
->pad
>= sd
->entity
.num_pads
)
213 return v4l2_subdev_call(sd
, pad
, get_crop
, subdev_fh
, crop
);
216 case VIDIOC_SUBDEV_S_CROP
: {
217 struct v4l2_subdev_crop
*crop
= arg
;
219 if (crop
->which
!= V4L2_SUBDEV_FORMAT_TRY
&&
220 crop
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
223 if (crop
->pad
>= sd
->entity
.num_pads
)
226 return v4l2_subdev_call(sd
, pad
, set_crop
, subdev_fh
, crop
);
229 case VIDIOC_SUBDEV_ENUM_MBUS_CODE
: {
230 struct v4l2_subdev_mbus_code_enum
*code
= arg
;
232 if (code
->pad
>= sd
->entity
.num_pads
)
235 return v4l2_subdev_call(sd
, pad
, enum_mbus_code
, subdev_fh
,
239 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE
: {
240 struct v4l2_subdev_frame_size_enum
*fse
= arg
;
242 if (fse
->pad
>= sd
->entity
.num_pads
)
245 return v4l2_subdev_call(sd
, pad
, enum_frame_size
, subdev_fh
,
249 case VIDIOC_SUBDEV_G_FRAME_INTERVAL
:
250 return v4l2_subdev_call(sd
, video
, g_frame_interval
, arg
);
252 case VIDIOC_SUBDEV_S_FRAME_INTERVAL
:
253 return v4l2_subdev_call(sd
, video
, s_frame_interval
, arg
);
255 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL
: {
256 struct v4l2_subdev_frame_interval_enum
*fie
= arg
;
258 if (fie
->pad
>= sd
->entity
.num_pads
)
261 return v4l2_subdev_call(sd
, pad
, enum_frame_interval
, subdev_fh
,
266 return v4l2_subdev_call(sd
, core
, ioctl
, cmd
, arg
);
272 static long subdev_ioctl(struct file
*file
, unsigned int cmd
,
275 return video_usercopy(file
, cmd
, arg
, subdev_do_ioctl
);
278 static unsigned int subdev_poll(struct file
*file
, poll_table
*wait
)
280 struct video_device
*vdev
= video_devdata(file
);
281 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
282 struct v4l2_fh
*fh
= file
->private_data
;
284 if (!(sd
->flags
& V4L2_SUBDEV_FL_HAS_EVENTS
))
287 poll_wait(file
, &fh
->wait
, wait
);
289 if (v4l2_event_pending(fh
))
295 const struct v4l2_file_operations v4l2_subdev_fops
= {
296 .owner
= THIS_MODULE
,
298 .unlocked_ioctl
= subdev_ioctl
,
299 .release
= subdev_close
,
303 void v4l2_subdev_init(struct v4l2_subdev
*sd
, const struct v4l2_subdev_ops
*ops
)
305 INIT_LIST_HEAD(&sd
->list
);
313 sd
->host_priv
= NULL
;
314 #if defined(CONFIG_MEDIA_CONTROLLER)
315 sd
->entity
.name
= sd
->name
;
316 sd
->entity
.type
= MEDIA_ENT_T_V4L2_SUBDEV
;
319 EXPORT_SYMBOL(v4l2_subdev_init
);