blob: 83615b8fb46add402cf5bee98bc4936c6e9cc368 [file] [log] [blame]
Laurent Pinchart2096a5d2009-12-09 08:38:49 -03001/*
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -03002 * V4L2 sub-device
Laurent Pinchart2096a5d2009-12-09 08:38:49 -03003 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -03004 * Copyright (C) 2010 Nokia Corporation
Laurent Pinchart2096a5d2009-12-09 08:38:49 -03005 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -03006 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -03008 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -03009 * 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.
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030012 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -030013 * 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.
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030017 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -030018 * 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
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030021 */
22
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030023#include <linux/ioctl.h>
Sakari Ailus02adb1c2010-03-03 12:49:38 -030024#include <linux/slab.h>
25#include <linux/types.h>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030026#include <linux/videodev2.h>
Paul Gortmaker35a24632011-08-01 15:26:38 -040027#include <linux/export.h>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030028
Laurent Pinchartea8aa432009-12-09 08:39:54 -030029#include <media/v4l2-ctrls.h>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030030#include <media/v4l2-device.h>
31#include <media/v4l2-ioctl.h>
Sakari Ailus02adb1c2010-03-03 12:49:38 -030032#include <media/v4l2-fh.h>
33#include <media/v4l2-event.h>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030034
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030035static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
36{
37#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
Sakari Ailusae184cd2011-10-14 14:14:26 -030038 fh->pad = kzalloc(sizeof(*fh->pad) * sd->entity.num_pads, GFP_KERNEL);
39 if (fh->pad == NULL)
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030040 return -ENOMEM;
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030041#endif
42 return 0;
43}
44
45static void subdev_fh_free(struct v4l2_subdev_fh *fh)
46{
47#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
Sakari Ailusae184cd2011-10-14 14:14:26 -030048 kfree(fh->pad);
49 fh->pad = NULL;
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030050#endif
51}
52
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030053static int subdev_open(struct file *file)
54{
Sakari Ailus02adb1c2010-03-03 12:49:38 -030055 struct video_device *vdev = video_devdata(file);
56 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030057 struct v4l2_subdev_fh *subdev_fh;
Laurent Pinchart61f5db52009-12-09 08:40:08 -030058#if defined(CONFIG_MEDIA_CONTROLLER)
Laurent Pinchartf0beea82010-08-01 19:05:09 -030059 struct media_entity *entity = NULL;
Laurent Pinchart61f5db52009-12-09 08:40:08 -030060#endif
Sakari Ailus02adb1c2010-03-03 12:49:38 -030061 int ret;
62
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030063 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
64 if (subdev_fh == NULL)
65 return -ENOMEM;
Sakari Ailus02adb1c2010-03-03 12:49:38 -030066
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030067 ret = subdev_fh_init(subdev_fh, sd);
68 if (ret) {
69 kfree(subdev_fh);
70 return ret;
Sakari Ailus02adb1c2010-03-03 12:49:38 -030071 }
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030072
Hans Verkuil523f46d2011-06-13 17:44:42 -030073 v4l2_fh_init(&subdev_fh->vfh, vdev);
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030074 v4l2_fh_add(&subdev_fh->vfh);
75 file->private_data = &subdev_fh->vfh;
Laurent Pinchart61f5db52009-12-09 08:40:08 -030076#if defined(CONFIG_MEDIA_CONTROLLER)
77 if (sd->v4l2_dev->mdev) {
78 entity = media_entity_get(&sd->entity);
79 if (!entity) {
80 ret = -EBUSY;
81 goto err;
82 }
83 }
84#endif
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030085
Laurent Pinchartf0beea82010-08-01 19:05:09 -030086 if (sd->internal_ops && sd->internal_ops->open) {
87 ret = sd->internal_ops->open(sd, subdev_fh);
88 if (ret < 0)
89 goto err;
90 }
91
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030092 return 0;
Sakari Ailus02adb1c2010-03-03 12:49:38 -030093
94err:
Laurent Pinchartf0beea82010-08-01 19:05:09 -030095#if defined(CONFIG_MEDIA_CONTROLLER)
Markus Elfring5164d6a2015-02-03 11:27:38 -030096 media_entity_put(entity);
Laurent Pinchartf0beea82010-08-01 19:05:09 -030097#endif
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -030098 v4l2_fh_del(&subdev_fh->vfh);
99 v4l2_fh_exit(&subdev_fh->vfh);
100 subdev_fh_free(subdev_fh);
101 kfree(subdev_fh);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300102
103 return ret;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300104}
105
106static int subdev_close(struct file *file)
107{
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300108 struct video_device *vdev = video_devdata(file);
109 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300110 struct v4l2_fh *vfh = file->private_data;
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300111 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300112
Laurent Pinchartf0beea82010-08-01 19:05:09 -0300113 if (sd->internal_ops && sd->internal_ops->close)
114 sd->internal_ops->close(sd, subdev_fh);
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300115#if defined(CONFIG_MEDIA_CONTROLLER)
116 if (sd->v4l2_dev->mdev)
117 media_entity_put(&sd->entity);
118#endif
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300119 v4l2_fh_del(vfh);
120 v4l2_fh_exit(vfh);
121 subdev_fh_free(subdev_fh);
122 kfree(subdev_fh);
123 file->private_data = NULL;
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300124
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300125 return 0;
126}
127
Mauro Carvalho Chehabd352bcc2014-07-22 01:40:00 -0300128#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
Sakari Ailusb225e392014-06-04 08:22:03 -0300129static int check_format(struct v4l2_subdev *sd,
130 struct v4l2_subdev_format *format)
131{
132 if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
133 format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
134 return -EINVAL;
135
136 if (format->pad >= sd->entity.num_pads)
137 return -EINVAL;
138
139 return 0;
140}
141
142static int check_crop(struct v4l2_subdev *sd, struct v4l2_subdev_crop *crop)
143{
144 if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
145 crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
146 return -EINVAL;
147
148 if (crop->pad >= sd->entity.num_pads)
149 return -EINVAL;
150
151 return 0;
152}
153
154static int check_selection(struct v4l2_subdev *sd,
155 struct v4l2_subdev_selection *sel)
156{
157 if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
158 sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
159 return -EINVAL;
160
161 if (sel->pad >= sd->entity.num_pads)
162 return -EINVAL;
163
164 return 0;
165}
166
167static int check_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
168{
169 if (edid->pad >= sd->entity.num_pads)
170 return -EINVAL;
171
172 if (edid->blocks && edid->edid == NULL)
173 return -EINVAL;
174
175 return 0;
176}
Mauro Carvalho Chehabd352bcc2014-07-22 01:40:00 -0300177#endif
Sakari Ailusb225e392014-06-04 08:22:03 -0300178
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300179static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
180{
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300181 struct video_device *vdev = video_devdata(file);
182 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300183 struct v4l2_fh *vfh = file->private_data;
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300184#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
185 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
Sakari Ailusb225e392014-06-04 08:22:03 -0300186 int rval;
Mauro Carvalho Chehabd352bcc2014-07-22 01:40:00 -0300187#endif
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300188
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300189 switch (cmd) {
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300190 case VIDIOC_QUERYCTRL:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300191 return v4l2_queryctrl(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300192
Hans Verkuile6bee362014-06-10 04:22:06 -0300193 case VIDIOC_QUERY_EXT_CTRL:
194 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
195
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300196 case VIDIOC_QUERYMENU:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300197 return v4l2_querymenu(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300198
199 case VIDIOC_G_CTRL:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300200 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300201
202 case VIDIOC_S_CTRL:
Hans Verkuilab892ba2011-06-07 06:47:18 -0300203 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300204
205 case VIDIOC_G_EXT_CTRLS:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300206 return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300207
208 case VIDIOC_S_EXT_CTRLS:
Hans Verkuilab892ba2011-06-07 06:47:18 -0300209 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300210
211 case VIDIOC_TRY_EXT_CTRLS:
Hans Verkuil18d171b2011-05-25 08:52:13 -0300212 return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg);
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300213
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300214 case VIDIOC_DQEVENT:
215 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
216 return -ENOIOCTLCMD;
217
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300218 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300219
220 case VIDIOC_SUBSCRIBE_EVENT:
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300221 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300222
223 case VIDIOC_UNSUBSCRIBE_EVENT:
Stanimir Varbanov7cd5a162010-05-21 06:04:24 -0300224 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
Martin Hostettler69967a72011-09-19 02:04:56 -0300225
226#ifdef CONFIG_VIDEO_ADV_DEBUG
227 case VIDIOC_DBG_G_REGISTER:
228 {
229 struct v4l2_dbg_register *p = arg;
230
231 if (!capable(CAP_SYS_ADMIN))
232 return -EPERM;
233 return v4l2_subdev_call(sd, core, g_register, p);
234 }
235 case VIDIOC_DBG_S_REGISTER:
236 {
237 struct v4l2_dbg_register *p = arg;
238
239 if (!capable(CAP_SYS_ADMIN))
240 return -EPERM;
241 return v4l2_subdev_call(sd, core, s_register, p);
242 }
243#endif
Sylwester Nawrockibcd158d2011-10-01 11:13:59 -0300244
Hans Verkuil42194e72012-02-02 08:26:20 -0300245 case VIDIOC_LOG_STATUS: {
246 int ret;
247
248 pr_info("%s: ================= START STATUS =================\n",
249 sd->name);
250 ret = v4l2_subdev_call(sd, core, log_status);
251 pr_info("%s: ================== END STATUS ==================\n",
252 sd->name);
253 return ret;
254 }
Sylwester Nawrockibcd158d2011-10-01 11:13:59 -0300255
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300256#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
257 case VIDIOC_SUBDEV_G_FMT: {
258 struct v4l2_subdev_format *format = arg;
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300259
Sakari Ailusb225e392014-06-04 08:22:03 -0300260 rval = check_format(sd, format);
261 if (rval)
262 return rval;
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300263
Hans Verkuilf7234132015-03-04 01:47:54 -0800264 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->pad, format);
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300265 }
266
267 case VIDIOC_SUBDEV_S_FMT: {
268 struct v4l2_subdev_format *format = arg;
269
Sakari Ailusb225e392014-06-04 08:22:03 -0300270 rval = check_format(sd, format);
271 if (rval)
272 return rval;
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300273
Hans Verkuilf7234132015-03-04 01:47:54 -0800274 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->pad, format);
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300275 }
276
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300277 case VIDIOC_SUBDEV_G_CROP: {
278 struct v4l2_subdev_crop *crop = arg;
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300279 struct v4l2_subdev_selection sel;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300280
Sakari Ailusb225e392014-06-04 08:22:03 -0300281 rval = check_crop(sd, crop);
282 if (rval)
283 return rval;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300284
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300285 memset(&sel, 0, sizeof(sel));
286 sel.which = crop->which;
287 sel.pad = crop->pad;
Sakari Ailus5689b282012-05-18 09:31:18 -0300288 sel.target = V4L2_SEL_TGT_CROP;
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300289
290 rval = v4l2_subdev_call(
Hans Verkuilf7234132015-03-04 01:47:54 -0800291 sd, pad, get_selection, subdev_fh->pad, &sel);
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300292
293 crop->rect = sel.r;
294
295 return rval;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300296 }
297
298 case VIDIOC_SUBDEV_S_CROP: {
299 struct v4l2_subdev_crop *crop = arg;
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300300 struct v4l2_subdev_selection sel;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300301
Sakari Ailusb225e392014-06-04 08:22:03 -0300302 rval = check_crop(sd, crop);
303 if (rval)
304 return rval;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300305
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300306 memset(&sel, 0, sizeof(sel));
307 sel.which = crop->which;
308 sel.pad = crop->pad;
Sakari Ailus5689b282012-05-18 09:31:18 -0300309 sel.target = V4L2_SEL_TGT_CROP;
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300310 sel.r = crop->rect;
311
312 rval = v4l2_subdev_call(
Hans Verkuilf7234132015-03-04 01:47:54 -0800313 sd, pad, set_selection, subdev_fh->pad, &sel);
Sakari Ailus5b9d7702011-11-14 15:30:24 -0300314
315 crop->rect = sel.r;
316
317 return rval;
Antti Koskipaaf6a5cb12010-06-23 05:03:42 -0300318 }
319
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300320 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
321 struct v4l2_subdev_mbus_code_enum *code = arg;
322
Hans Verkuil20058f92015-03-04 01:47:56 -0800323 if (code->which != V4L2_SUBDEV_FORMAT_TRY &&
324 code->which != V4L2_SUBDEV_FORMAT_ACTIVE)
325 return -EINVAL;
326
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300327 if (code->pad >= sd->entity.num_pads)
328 return -EINVAL;
329
Hans Verkuilf7234132015-03-04 01:47:54 -0800330 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->pad,
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300331 code);
332 }
333
334 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
335 struct v4l2_subdev_frame_size_enum *fse = arg;
336
Hans Verkuil20058f92015-03-04 01:47:56 -0800337 if (fse->which != V4L2_SUBDEV_FORMAT_TRY &&
338 fse->which != V4L2_SUBDEV_FORMAT_ACTIVE)
339 return -EINVAL;
340
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300341 if (fse->pad >= sd->entity.num_pads)
342 return -EINVAL;
343
Hans Verkuilf7234132015-03-04 01:47:54 -0800344 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->pad,
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300345 fse);
346 }
Laurent Pinchart35c30172010-05-05 11:38:35 -0300347
Sakari Ailus743e1832013-04-22 10:24:51 -0300348 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
349 struct v4l2_subdev_frame_interval *fi = arg;
Laurent Pinchart35c30172010-05-05 11:38:35 -0300350
Sakari Ailus743e1832013-04-22 10:24:51 -0300351 if (fi->pad >= sd->entity.num_pads)
352 return -EINVAL;
353
354 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
355 }
356
357 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
358 struct v4l2_subdev_frame_interval *fi = arg;
359
360 if (fi->pad >= sd->entity.num_pads)
361 return -EINVAL;
362
Laurent Pinchart35c30172010-05-05 11:38:35 -0300363 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
Sakari Ailus743e1832013-04-22 10:24:51 -0300364 }
Laurent Pinchart35c30172010-05-05 11:38:35 -0300365
366 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
367 struct v4l2_subdev_frame_interval_enum *fie = arg;
368
Hans Verkuil20058f92015-03-04 01:47:56 -0800369 if (fie->which != V4L2_SUBDEV_FORMAT_TRY &&
370 fie->which != V4L2_SUBDEV_FORMAT_ACTIVE)
371 return -EINVAL;
372
Laurent Pinchart35c30172010-05-05 11:38:35 -0300373 if (fie->pad >= sd->entity.num_pads)
374 return -EINVAL;
375
Hans Verkuilf7234132015-03-04 01:47:54 -0800376 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->pad,
Laurent Pinchart35c30172010-05-05 11:38:35 -0300377 fie);
378 }
Sakari Ailusae184cd2011-10-14 14:14:26 -0300379
380 case VIDIOC_SUBDEV_G_SELECTION: {
381 struct v4l2_subdev_selection *sel = arg;
382
Sakari Ailusb225e392014-06-04 08:22:03 -0300383 rval = check_selection(sd, sel);
384 if (rval)
385 return rval;
Sakari Ailusae184cd2011-10-14 14:14:26 -0300386
387 return v4l2_subdev_call(
Hans Verkuilf7234132015-03-04 01:47:54 -0800388 sd, pad, get_selection, subdev_fh->pad, sel);
Sakari Ailusae184cd2011-10-14 14:14:26 -0300389 }
390
391 case VIDIOC_SUBDEV_S_SELECTION: {
392 struct v4l2_subdev_selection *sel = arg;
393
Sakari Ailusb225e392014-06-04 08:22:03 -0300394 rval = check_selection(sd, sel);
395 if (rval)
396 return rval;
Sakari Ailusae184cd2011-10-14 14:14:26 -0300397
398 return v4l2_subdev_call(
Hans Verkuilf7234132015-03-04 01:47:54 -0800399 sd, pad, set_selection, subdev_fh->pad, sel);
Sakari Ailusae184cd2011-10-14 14:14:26 -0300400 }
Hans Verkuiled45ce22012-08-10 06:07:12 -0300401
Laurent Pinchartf2e90842014-01-29 10:07:13 -0300402 case VIDIOC_G_EDID: {
403 struct v4l2_subdev_edid *edid = arg;
Hans Verkuiled45ce22012-08-10 06:07:12 -0300404
Sakari Ailusb225e392014-06-04 08:22:03 -0300405 rval = check_edid(sd, edid);
406 if (rval)
407 return rval;
Laurent Pinchartf2e90842014-01-29 10:07:13 -0300408
409 return v4l2_subdev_call(sd, pad, get_edid, edid);
410 }
411
412 case VIDIOC_S_EDID: {
413 struct v4l2_subdev_edid *edid = arg;
414
Sakari Ailusb225e392014-06-04 08:22:03 -0300415 rval = check_edid(sd, edid);
416 if (rval)
417 return rval;
Laurent Pinchartf2e90842014-01-29 10:07:13 -0300418
419 return v4l2_subdev_call(sd, pad, set_edid, edid);
420 }
Laurent Pinchart9cfd65e2014-01-29 10:07:13 -0300421
422 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
423 struct v4l2_dv_timings_cap *cap = arg;
424
425 if (cap->pad >= sd->entity.num_pads)
426 return -EINVAL;
427
428 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
429 }
430
431 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
432 struct v4l2_enum_dv_timings *dvt = arg;
433
434 if (dvt->pad >= sd->entity.num_pads)
435 return -EINVAL;
436
437 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
438 }
439
440 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
441 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
442
443 case VIDIOC_SUBDEV_G_DV_TIMINGS:
444 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
445
446 case VIDIOC_SUBDEV_S_DV_TIMINGS:
447 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
Laurent Pinchart333c8b92010-03-15 20:26:04 -0300448#endif
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300449 default:
Laurent Pinchartc30b46e2010-02-26 12:23:10 -0300450 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300451 }
452
453 return 0;
454}
455
456static long subdev_ioctl(struct file *file, unsigned int cmd,
457 unsigned long arg)
458{
459 return video_usercopy(file, cmd, arg, subdev_do_ioctl);
460}
461
Hans Verkuilab58a302014-02-10 08:08:44 -0300462#ifdef CONFIG_COMPAT
463static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
464 unsigned long arg)
465{
466 struct video_device *vdev = video_devdata(file);
467 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
468
469 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
470}
471#endif
472
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300473static unsigned int subdev_poll(struct file *file, poll_table *wait)
474{
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;
478
479 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
480 return POLLERR;
481
Hans Verkuil523f46d2011-06-13 17:44:42 -0300482 poll_wait(file, &fh->wait, wait);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300483
484 if (v4l2_event_pending(fh))
485 return POLLPRI;
486
487 return 0;
488}
489
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300490const struct v4l2_file_operations v4l2_subdev_fops = {
491 .owner = THIS_MODULE,
492 .open = subdev_open,
493 .unlocked_ioctl = subdev_ioctl,
Hans Verkuilab58a302014-02-10 08:08:44 -0300494#ifdef CONFIG_COMPAT
495 .compat_ioctl32 = subdev_compat_ioctl32,
496#endif
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300497 .release = subdev_close,
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300498 .poll = subdev_poll,
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300499};
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300500
Sakari Ailus8227c922011-10-10 17:01:25 -0300501#ifdef CONFIG_MEDIA_CONTROLLER
502int 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)
506{
Laurent Pinchart24acf8b2014-05-19 11:36:23 -0300507 /* The width, height and code must match. */
Sakari Ailus8227c922011-10-10 17:01:25 -0300508 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)
511 return -EINVAL;
512
Laurent Pinchart24acf8b2014-05-19 11:36:23 -0300513 /* 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.
516 */
517 if (source_fmt->format.field != sink_fmt->format.field &&
518 sink_fmt->format.field != V4L2_FIELD_NONE)
519 return -EINVAL;
520
Sakari Ailus8227c922011-10-10 17:01:25 -0300521 return 0;
522}
523EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
524
525static int
526v4l2_subdev_link_validate_get_format(struct media_pad *pad,
527 struct v4l2_subdev_format *fmt)
528{
Laurent Pinchart864a1212012-10-20 17:47:22 -0300529 if (media_entity_type(pad->entity) == MEDIA_ENT_T_V4L2_SUBDEV) {
530 struct v4l2_subdev *sd =
531 media_entity_to_v4l2_subdev(pad->entity);
532
Sakari Ailus8227c922011-10-10 17:01:25 -0300533 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
534 fmt->pad = pad->index;
Laurent Pinchart864a1212012-10-20 17:47:22 -0300535 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
Sakari Ailus8227c922011-10-10 17:01:25 -0300536 }
Laurent Pinchart864a1212012-10-20 17:47:22 -0300537
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);
541
542 return -EINVAL;
Sakari Ailus8227c922011-10-10 17:01:25 -0300543}
544
545int v4l2_subdev_link_validate(struct media_link *link)
546{
547 struct v4l2_subdev *sink;
548 struct v4l2_subdev_format sink_fmt, source_fmt;
549 int rval;
550
551 rval = v4l2_subdev_link_validate_get_format(
552 link->source, &source_fmt);
553 if (rval < 0)
554 return 0;
555
556 rval = v4l2_subdev_link_validate_get_format(
557 link->sink, &sink_fmt);
558 if (rval < 0)
559 return 0;
560
561 sink = media_entity_to_v4l2_subdev(link->sink->entity);
562
563 rval = v4l2_subdev_call(sink, pad, link_validate, link,
564 &source_fmt, &sink_fmt);
565 if (rval != -ENOIOCTLCMD)
566 return rval;
567
568 return v4l2_subdev_link_validate_default(
569 sink, link, &source_fmt, &sink_fmt);
570}
571EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
572#endif /* CONFIG_MEDIA_CONTROLLER */
573
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300574void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
575{
576 INIT_LIST_HEAD(&sd->list);
577 BUG_ON(!ops);
578 sd->ops = ops;
579 sd->v4l2_dev = NULL;
580 sd->flags = 0;
581 sd->name[0] = '\0';
582 sd->grp_id = 0;
583 sd->dev_priv = NULL;
584 sd->host_priv = NULL;
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300585#if defined(CONFIG_MEDIA_CONTROLLER)
586 sd->entity.name = sd->name;
587 sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
588#endif
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300589}
590EXPORT_SYMBOL(v4l2_subdev_init);
Lars-Peter Clausen8ae56402015-06-24 13:50:29 -0300591
592/**
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
596 *
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.
601 */
602void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
603 const struct v4l2_event *ev)
604{
605 v4l2_event_queue(sd->devnode, ev);
606 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
607}
608EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);