1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * V4L2 sub-device
4 *
5 * Copyright (C) 2010 Nokia Corporation
6 *
7 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 * Sakari Ailus <sakari.ailus@iki.fi>
9 */
10
11 #include <linux/ioctl.h>
12 #include <linux/mm.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>
19
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>
25 #ifndef __GENKSYMS__
26 #include <trace/hooks/v4l2core.h>
27 #endif
28
29 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
subdev_fh_init(struct v4l2_subdev_fh * fh,struct v4l2_subdev * sd)30 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
31 {
32 if (sd->entity.num_pads) {
33 fh->pad = v4l2_subdev_alloc_pad_config(sd);
34 if (fh->pad == NULL)
35 return -ENOMEM;
36 }
37
38 return 0;
39 }
40
subdev_fh_free(struct v4l2_subdev_fh * fh)41 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
42 {
43 v4l2_subdev_free_pad_config(fh->pad);
44 fh->pad = NULL;
45 }
46
subdev_open(struct file * file)47 static int subdev_open(struct file *file)
48 {
49 struct video_device *vdev = video_devdata(file);
50 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
51 struct v4l2_subdev_fh *subdev_fh;
52 int ret;
53
54 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
55 if (subdev_fh == NULL)
56 return -ENOMEM;
57
58 ret = subdev_fh_init(subdev_fh, sd);
59 if (ret) {
60 kfree(subdev_fh);
61 return ret;
62 }
63
64 v4l2_fh_init(&subdev_fh->vfh, vdev);
65 v4l2_fh_add(&subdev_fh->vfh);
66 file->private_data = &subdev_fh->vfh;
67 #if defined(CONFIG_MEDIA_CONTROLLER)
68 if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
69 struct module *owner;
70
71 owner = sd->entity.graph_obj.mdev->dev->driver->owner;
72 if (!try_module_get(owner)) {
73 ret = -EBUSY;
74 goto err;
75 }
76 subdev_fh->owner = owner;
77 }
78 #endif
79
80 if (sd->internal_ops && sd->internal_ops->open) {
81 ret = sd->internal_ops->open(sd, subdev_fh);
82 if (ret < 0)
83 goto err;
84 }
85
86 return 0;
87
88 err:
89 module_put(subdev_fh->owner);
90 v4l2_fh_del(&subdev_fh->vfh);
91 v4l2_fh_exit(&subdev_fh->vfh);
92 subdev_fh_free(subdev_fh);
93 kfree(subdev_fh);
94
95 return ret;
96 }
97
subdev_close(struct file * file)98 static int subdev_close(struct file *file)
99 {
100 struct video_device *vdev = video_devdata(file);
101 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
102 struct v4l2_fh *vfh = file->private_data;
103 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
104
105 if (sd->internal_ops && sd->internal_ops->close)
106 sd->internal_ops->close(sd, subdev_fh);
107 module_put(subdev_fh->owner);
108 v4l2_fh_del(vfh);
109 v4l2_fh_exit(vfh);
110 subdev_fh_free(subdev_fh);
111 kfree(subdev_fh);
112 file->private_data = NULL;
113
114 return 0;
115 }
116 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
subdev_open(struct file * file)117 static int subdev_open(struct file *file)
118 {
119 return -ENODEV;
120 }
121
subdev_close(struct file * file)122 static int subdev_close(struct file *file)
123 {
124 return -ENODEV;
125 }
126 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
127
check_which(u32 which)128 static inline int check_which(u32 which)
129 {
130 if (which != V4L2_SUBDEV_FORMAT_TRY &&
131 which != V4L2_SUBDEV_FORMAT_ACTIVE)
132 return -EINVAL;
133
134 return 0;
135 }
136
check_pad(struct v4l2_subdev * sd,u32 pad)137 static inline int check_pad(struct v4l2_subdev *sd, u32 pad)
138 {
139 #if defined(CONFIG_MEDIA_CONTROLLER)
140 if (sd->entity.num_pads) {
141 if (pad >= sd->entity.num_pads)
142 return -EINVAL;
143 return 0;
144 }
145 #endif
146 /* allow pad 0 on subdevices not registered as media entities */
147 if (pad > 0)
148 return -EINVAL;
149 return 0;
150 }
151
check_cfg(u32 which,struct v4l2_subdev_pad_config * cfg)152 static int check_cfg(u32 which, struct v4l2_subdev_pad_config *cfg)
153 {
154 if (which == V4L2_SUBDEV_FORMAT_TRY && !cfg)
155 return -EINVAL;
156
157 return 0;
158 }
159
check_format(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)160 static inline int check_format(struct v4l2_subdev *sd,
161 struct v4l2_subdev_pad_config *cfg,
162 struct v4l2_subdev_format *format)
163 {
164 if (!format)
165 return -EINVAL;
166
167 return check_which(format->which) ? : check_pad(sd, format->pad) ? :
168 check_cfg(format->which, cfg);
169 }
170
call_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)171 static int call_get_fmt(struct v4l2_subdev *sd,
172 struct v4l2_subdev_pad_config *cfg,
173 struct v4l2_subdev_format *format)
174 {
175 return check_format(sd, cfg, format) ? :
176 sd->ops->pad->get_fmt(sd, cfg, format);
177 }
178
call_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)179 static int call_set_fmt(struct v4l2_subdev *sd,
180 struct v4l2_subdev_pad_config *cfg,
181 struct v4l2_subdev_format *format)
182 {
183 return check_format(sd, cfg, format) ? :
184 sd->ops->pad->set_fmt(sd, cfg, format);
185 }
186
call_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)187 static int call_enum_mbus_code(struct v4l2_subdev *sd,
188 struct v4l2_subdev_pad_config *cfg,
189 struct v4l2_subdev_mbus_code_enum *code)
190 {
191 if (!code)
192 return -EINVAL;
193
194 return check_which(code->which) ? : check_pad(sd, code->pad) ? :
195 check_cfg(code->which, cfg) ? :
196 sd->ops->pad->enum_mbus_code(sd, cfg, code);
197 }
198
call_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)199 static int call_enum_frame_size(struct v4l2_subdev *sd,
200 struct v4l2_subdev_pad_config *cfg,
201 struct v4l2_subdev_frame_size_enum *fse)
202 {
203 if (!fse)
204 return -EINVAL;
205
206 return check_which(fse->which) ? : check_pad(sd, fse->pad) ? :
207 check_cfg(fse->which, cfg) ? :
208 sd->ops->pad->enum_frame_size(sd, cfg, fse);
209 }
210
check_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)211 static inline int check_frame_interval(struct v4l2_subdev *sd,
212 struct v4l2_subdev_frame_interval *fi)
213 {
214 if (!fi)
215 return -EINVAL;
216
217 return check_pad(sd, fi->pad);
218 }
219
call_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)220 static int call_g_frame_interval(struct v4l2_subdev *sd,
221 struct v4l2_subdev_frame_interval *fi)
222 {
223 return check_frame_interval(sd, fi) ? :
224 sd->ops->video->g_frame_interval(sd, fi);
225 }
226
call_s_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)227 static int call_s_frame_interval(struct v4l2_subdev *sd,
228 struct v4l2_subdev_frame_interval *fi)
229 {
230 return check_frame_interval(sd, fi) ? :
231 sd->ops->video->s_frame_interval(sd, fi);
232 }
233
call_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)234 static int call_enum_frame_interval(struct v4l2_subdev *sd,
235 struct v4l2_subdev_pad_config *cfg,
236 struct v4l2_subdev_frame_interval_enum *fie)
237 {
238 if (!fie)
239 return -EINVAL;
240
241 return check_which(fie->which) ? : check_pad(sd, fie->pad) ? :
242 check_cfg(fie->which, cfg) ? :
243 sd->ops->pad->enum_frame_interval(sd, cfg, fie);
244 }
245
check_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)246 static inline int check_selection(struct v4l2_subdev *sd,
247 struct v4l2_subdev_pad_config *cfg,
248 struct v4l2_subdev_selection *sel)
249 {
250 if (!sel)
251 return -EINVAL;
252
253 return check_which(sel->which) ? : check_pad(sd, sel->pad) ? :
254 check_cfg(sel->which, cfg);
255 }
256
call_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)257 static int call_get_selection(struct v4l2_subdev *sd,
258 struct v4l2_subdev_pad_config *cfg,
259 struct v4l2_subdev_selection *sel)
260 {
261 return check_selection(sd, cfg, sel) ? :
262 sd->ops->pad->get_selection(sd, cfg, sel);
263 }
264
call_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)265 static int call_set_selection(struct v4l2_subdev *sd,
266 struct v4l2_subdev_pad_config *cfg,
267 struct v4l2_subdev_selection *sel)
268 {
269 return check_selection(sd, cfg, sel) ? :
270 sd->ops->pad->set_selection(sd, cfg, sel);
271 }
272
check_edid(struct v4l2_subdev * sd,struct v4l2_subdev_edid * edid)273 static inline int check_edid(struct v4l2_subdev *sd,
274 struct v4l2_subdev_edid *edid)
275 {
276 if (!edid)
277 return -EINVAL;
278
279 if (edid->blocks && edid->edid == NULL)
280 return -EINVAL;
281
282 return check_pad(sd, edid->pad);
283 }
284
call_get_edid(struct v4l2_subdev * sd,struct v4l2_subdev_edid * edid)285 static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
286 {
287 return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid);
288 }
289
call_set_edid(struct v4l2_subdev * sd,struct v4l2_subdev_edid * edid)290 static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
291 {
292 return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid);
293 }
294
call_dv_timings_cap(struct v4l2_subdev * sd,struct v4l2_dv_timings_cap * cap)295 static int call_dv_timings_cap(struct v4l2_subdev *sd,
296 struct v4l2_dv_timings_cap *cap)
297 {
298 if (!cap)
299 return -EINVAL;
300
301 return check_pad(sd, cap->pad) ? :
302 sd->ops->pad->dv_timings_cap(sd, cap);
303 }
304
call_enum_dv_timings(struct v4l2_subdev * sd,struct v4l2_enum_dv_timings * dvt)305 static int call_enum_dv_timings(struct v4l2_subdev *sd,
306 struct v4l2_enum_dv_timings *dvt)
307 {
308 if (!dvt)
309 return -EINVAL;
310
311 return check_pad(sd, dvt->pad) ? :
312 sd->ops->pad->enum_dv_timings(sd, dvt);
313 }
314
call_get_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * config)315 static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
316 struct v4l2_mbus_config *config)
317 {
318 return check_pad(sd, pad) ? :
319 sd->ops->pad->get_mbus_config(sd, pad, config);
320 }
321
call_set_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * config)322 static int call_set_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
323 struct v4l2_mbus_config *config)
324 {
325 return check_pad(sd, pad) ? :
326 sd->ops->pad->get_mbus_config(sd, pad, config);
327 }
328
329 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = {
330 .get_fmt = call_get_fmt,
331 .set_fmt = call_set_fmt,
332 .enum_mbus_code = call_enum_mbus_code,
333 .enum_frame_size = call_enum_frame_size,
334 .enum_frame_interval = call_enum_frame_interval,
335 .get_selection = call_get_selection,
336 .set_selection = call_set_selection,
337 .get_edid = call_get_edid,
338 .set_edid = call_set_edid,
339 .dv_timings_cap = call_dv_timings_cap,
340 .enum_dv_timings = call_enum_dv_timings,
341 .get_mbus_config = call_get_mbus_config,
342 .set_mbus_config = call_set_mbus_config,
343 };
344
345 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = {
346 .g_frame_interval = call_g_frame_interval,
347 .s_frame_interval = call_s_frame_interval,
348 };
349
350 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = {
351 .pad = &v4l2_subdev_call_pad_wrappers,
352 .video = &v4l2_subdev_call_video_wrappers,
353 };
354 EXPORT_SYMBOL(v4l2_subdev_call_wrappers);
355
356 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
subdev_do_ioctl(struct file * file,unsigned int cmd,void * arg)357 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
358 {
359 struct video_device *vdev = video_devdata(file);
360 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
361 struct v4l2_fh *vfh = file->private_data;
362 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
363 bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
364 int rval;
365
366 switch (cmd) {
367 case VIDIOC_SUBDEV_QUERYCAP: {
368 struct v4l2_subdev_capability *cap = arg;
369
370 memset(cap->reserved, 0, sizeof(cap->reserved));
371 cap->version = LINUX_VERSION_CODE;
372 cap->capabilities = ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0;
373
374 return 0;
375 }
376
377 case VIDIOC_QUERYCTRL:
378 /*
379 * TODO: this really should be folded into v4l2_queryctrl (this
380 * currently returns -EINVAL for NULL control handlers).
381 * However, v4l2_queryctrl() is still called directly by
382 * drivers as well and until that has been addressed I believe
383 * it is safer to do the check here. The same is true for the
384 * other control ioctls below.
385 */
386 if (!vfh->ctrl_handler)
387 return -ENOTTY;
388 return v4l2_queryctrl(vfh->ctrl_handler, arg);
389
390 case VIDIOC_QUERY_EXT_CTRL:
391 if (!vfh->ctrl_handler)
392 return -ENOTTY;
393 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
394
395 case VIDIOC_QUERYMENU:
396 if (!vfh->ctrl_handler)
397 return -ENOTTY;
398 return v4l2_querymenu(vfh->ctrl_handler, arg);
399
400 case VIDIOC_G_CTRL:
401 if (!vfh->ctrl_handler)
402 return -ENOTTY;
403 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
404
405 case VIDIOC_S_CTRL:
406 if (!vfh->ctrl_handler)
407 return -ENOTTY;
408 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
409
410 case VIDIOC_G_EXT_CTRLS:
411 if (!vfh->ctrl_handler)
412 return -ENOTTY;
413 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
414 vdev, sd->v4l2_dev->mdev, arg);
415
416 case VIDIOC_S_EXT_CTRLS:
417 if (!vfh->ctrl_handler)
418 return -ENOTTY;
419 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
420 vdev, sd->v4l2_dev->mdev, arg);
421
422 case VIDIOC_TRY_EXT_CTRLS:
423 if (!vfh->ctrl_handler)
424 return -ENOTTY;
425 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
426 vdev, sd->v4l2_dev->mdev, arg);
427
428 case VIDIOC_DQEVENT:
429 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
430 return -ENOIOCTLCMD;
431
432 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
433
434 case VIDIOC_SUBSCRIBE_EVENT:
435 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
436
437 case VIDIOC_UNSUBSCRIBE_EVENT:
438 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
439
440 #ifdef CONFIG_VIDEO_ADV_DEBUG
441 case VIDIOC_DBG_G_REGISTER:
442 {
443 struct v4l2_dbg_register *p = arg;
444
445 if (!capable(CAP_SYS_ADMIN))
446 return -EPERM;
447 return v4l2_subdev_call(sd, core, g_register, p);
448 }
449 case VIDIOC_DBG_S_REGISTER:
450 {
451 struct v4l2_dbg_register *p = arg;
452
453 if (!capable(CAP_SYS_ADMIN))
454 return -EPERM;
455 return v4l2_subdev_call(sd, core, s_register, p);
456 }
457 case VIDIOC_DBG_G_CHIP_INFO:
458 {
459 struct v4l2_dbg_chip_info *p = arg;
460
461 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
462 return -EINVAL;
463 if (sd->ops->core && sd->ops->core->s_register)
464 p->flags |= V4L2_CHIP_FL_WRITABLE;
465 if (sd->ops->core && sd->ops->core->g_register)
466 p->flags |= V4L2_CHIP_FL_READABLE;
467 strscpy(p->name, sd->name, sizeof(p->name));
468 return 0;
469 }
470 #endif
471
472 case VIDIOC_LOG_STATUS: {
473 int ret;
474
475 pr_info("%s: ================= START STATUS =================\n",
476 sd->name);
477 ret = v4l2_subdev_call(sd, core, log_status);
478 pr_info("%s: ================== END STATUS ==================\n",
479 sd->name);
480 return ret;
481 }
482
483 case VIDIOC_SUBDEV_G_FMT: {
484 struct v4l2_subdev_format *format = arg;
485
486 memset(format->reserved, 0, sizeof(format->reserved));
487 memset(format->format.reserved, 0, sizeof(format->format.reserved));
488 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->pad, format);
489 }
490
491 case VIDIOC_SUBDEV_S_FMT: {
492 struct v4l2_subdev_format *format = arg;
493 int ret = 0;
494
495 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
496 return -EPERM;
497
498 trace_android_vh_v4l2subdev_set_fmt(sd, subdev_fh->pad,
499 format, &ret);
500 trace_android_rvh_v4l2subdev_set_fmt(sd, subdev_fh->pad,
501 format, &ret);
502 if (ret)
503 return ret;
504
505 memset(format->reserved, 0, sizeof(format->reserved));
506 memset(format->format.reserved, 0, sizeof(format->format.reserved));
507 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->pad, format);
508 }
509
510 case VIDIOC_SUBDEV_G_CROP: {
511 struct v4l2_subdev_crop *crop = arg;
512 struct v4l2_subdev_selection sel;
513
514 memset(crop->reserved, 0, sizeof(crop->reserved));
515 memset(&sel, 0, sizeof(sel));
516 sel.which = crop->which;
517 sel.pad = crop->pad;
518 sel.target = V4L2_SEL_TGT_CROP;
519
520 rval = v4l2_subdev_call(
521 sd, pad, get_selection, subdev_fh->pad, &sel);
522
523 crop->rect = sel.r;
524
525 return rval;
526 }
527
528 case VIDIOC_SUBDEV_S_CROP: {
529 struct v4l2_subdev_crop *crop = arg;
530 struct v4l2_subdev_selection sel;
531
532 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
533 return -EPERM;
534
535 memset(crop->reserved, 0, sizeof(crop->reserved));
536 memset(&sel, 0, sizeof(sel));
537 sel.which = crop->which;
538 sel.pad = crop->pad;
539 sel.target = V4L2_SEL_TGT_CROP;
540 sel.r = crop->rect;
541
542 rval = v4l2_subdev_call(
543 sd, pad, set_selection, subdev_fh->pad, &sel);
544
545 crop->rect = sel.r;
546
547 return rval;
548 }
549
550 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
551 struct v4l2_subdev_mbus_code_enum *code = arg;
552
553 memset(code->reserved, 0, sizeof(code->reserved));
554 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->pad,
555 code);
556 }
557
558 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
559 struct v4l2_subdev_frame_size_enum *fse = arg;
560
561 memset(fse->reserved, 0, sizeof(fse->reserved));
562 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->pad,
563 fse);
564 }
565
566 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
567 struct v4l2_subdev_frame_interval *fi = arg;
568
569 memset(fi->reserved, 0, sizeof(fi->reserved));
570 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
571 }
572
573 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
574 struct v4l2_subdev_frame_interval *fi = arg;
575 int ret = 0;
576
577 if (ro_subdev)
578 return -EPERM;
579
580 trace_android_vh_v4l2subdev_set_frame_interval(sd, fi, &ret);
581 trace_android_rvh_v4l2subdev_set_frame_interval(sd, fi, &ret);
582 if (ret)
583 return ret;
584
585 memset(fi->reserved, 0, sizeof(fi->reserved));
586 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
587 }
588
589 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
590 struct v4l2_subdev_frame_interval_enum *fie = arg;
591
592 memset(fie->reserved, 0, sizeof(fie->reserved));
593 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->pad,
594 fie);
595 }
596
597 case VIDIOC_SUBDEV_G_SELECTION: {
598 struct v4l2_subdev_selection *sel = arg;
599
600 memset(sel->reserved, 0, sizeof(sel->reserved));
601 return v4l2_subdev_call(
602 sd, pad, get_selection, subdev_fh->pad, sel);
603 }
604
605 case VIDIOC_SUBDEV_S_SELECTION: {
606 struct v4l2_subdev_selection *sel = arg;
607 int ret = 0;
608
609 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
610 return -EPERM;
611
612 trace_android_vh_v4l2subdev_set_selection(sd, subdev_fh->pad,
613 sel, &ret);
614 trace_android_rvh_v4l2subdev_set_selection(sd, subdev_fh->pad,
615 sel, &ret);
616 if (ret)
617 return ret;
618
619 memset(sel->reserved, 0, sizeof(sel->reserved));
620 return v4l2_subdev_call(
621 sd, pad, set_selection, subdev_fh->pad, sel);
622 }
623
624 case VIDIOC_G_EDID: {
625 struct v4l2_subdev_edid *edid = arg;
626
627 return v4l2_subdev_call(sd, pad, get_edid, edid);
628 }
629
630 case VIDIOC_S_EDID: {
631 struct v4l2_subdev_edid *edid = arg;
632
633 return v4l2_subdev_call(sd, pad, set_edid, edid);
634 }
635
636 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
637 struct v4l2_dv_timings_cap *cap = arg;
638
639 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
640 }
641
642 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
643 struct v4l2_enum_dv_timings *dvt = arg;
644
645 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
646 }
647
648 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
649 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
650
651 case VIDIOC_SUBDEV_G_DV_TIMINGS:
652 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
653
654 case VIDIOC_SUBDEV_S_DV_TIMINGS:
655 if (ro_subdev)
656 return -EPERM;
657
658 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
659
660 case VIDIOC_SUBDEV_G_STD:
661 return v4l2_subdev_call(sd, video, g_std, arg);
662
663 case VIDIOC_SUBDEV_S_STD: {
664 v4l2_std_id *std = arg;
665
666 if (ro_subdev)
667 return -EPERM;
668
669 return v4l2_subdev_call(sd, video, s_std, *std);
670 }
671
672 case VIDIOC_SUBDEV_ENUMSTD: {
673 struct v4l2_standard *p = arg;
674 v4l2_std_id id;
675
676 if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
677 return -EINVAL;
678
679 return v4l_video_std_enumstd(p, id);
680 }
681
682 case VIDIOC_SUBDEV_QUERYSTD:
683 return v4l2_subdev_call(sd, video, querystd, arg);
684
685 default:
686 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
687 }
688
689 return 0;
690 }
691
subdev_do_ioctl_lock(struct file * file,unsigned int cmd,void * arg)692 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
693 {
694 struct video_device *vdev = video_devdata(file);
695 struct mutex *lock = vdev->lock;
696 long ret = -ENODEV;
697
698 if (lock && mutex_lock_interruptible(lock))
699 return -ERESTARTSYS;
700 if (video_is_registered(vdev))
701 ret = subdev_do_ioctl(file, cmd, arg);
702 if (lock)
703 mutex_unlock(lock);
704 return ret;
705 }
706
subdev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)707 static long subdev_ioctl(struct file *file, unsigned int cmd,
708 unsigned long arg)
709 {
710 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
711 }
712
713 #ifdef CONFIG_COMPAT
subdev_compat_ioctl32(struct file * file,unsigned int cmd,unsigned long arg)714 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
715 unsigned long arg)
716 {
717 struct video_device *vdev = video_devdata(file);
718 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
719
720 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
721 }
722 #endif
723
724 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
subdev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)725 static long subdev_ioctl(struct file *file, unsigned int cmd,
726 unsigned long arg)
727 {
728 return -ENODEV;
729 }
730
731 #ifdef CONFIG_COMPAT
subdev_compat_ioctl32(struct file * file,unsigned int cmd,unsigned long arg)732 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
733 unsigned long arg)
734 {
735 return -ENODEV;
736 }
737 #endif
738 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
739
subdev_poll(struct file * file,poll_table * wait)740 static __poll_t subdev_poll(struct file *file, poll_table *wait)
741 {
742 struct video_device *vdev = video_devdata(file);
743 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
744 struct v4l2_fh *fh = file->private_data;
745
746 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
747 return EPOLLERR;
748
749 poll_wait(file, &fh->wait, wait);
750
751 if (v4l2_event_pending(fh))
752 return EPOLLPRI;
753
754 return 0;
755 }
756
757 const struct v4l2_file_operations v4l2_subdev_fops = {
758 .owner = THIS_MODULE,
759 .open = subdev_open,
760 .unlocked_ioctl = subdev_ioctl,
761 #ifdef CONFIG_COMPAT
762 .compat_ioctl32 = subdev_compat_ioctl32,
763 #endif
764 .release = subdev_close,
765 .poll = subdev_poll,
766 };
767
768 #ifdef CONFIG_MEDIA_CONTROLLER
769
v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity * entity,struct fwnode_endpoint * endpoint)770 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
771 struct fwnode_endpoint *endpoint)
772 {
773 struct fwnode_handle *fwnode;
774 struct v4l2_subdev *sd;
775
776 if (!is_media_entity_v4l2_subdev(entity))
777 return -EINVAL;
778
779 sd = media_entity_to_v4l2_subdev(entity);
780
781 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
782 fwnode_handle_put(fwnode);
783
784 if (dev_fwnode(sd->dev) == fwnode)
785 return endpoint->port;
786
787 return -ENXIO;
788 }
789 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
790
v4l2_subdev_link_validate_default(struct v4l2_subdev * sd,struct media_link * link,struct v4l2_subdev_format * source_fmt,struct v4l2_subdev_format * sink_fmt)791 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
792 struct media_link *link,
793 struct v4l2_subdev_format *source_fmt,
794 struct v4l2_subdev_format *sink_fmt)
795 {
796 /* The width, height and code must match. */
797 if (source_fmt->format.width != sink_fmt->format.width
798 || source_fmt->format.height != sink_fmt->format.height
799 || source_fmt->format.code != sink_fmt->format.code)
800 return -EPIPE;
801
802 /* The field order must match, or the sink field order must be NONE
803 * to support interlaced hardware connected to bridges that support
804 * progressive formats only.
805 */
806 if (source_fmt->format.field != sink_fmt->format.field &&
807 sink_fmt->format.field != V4L2_FIELD_NONE)
808 return -EPIPE;
809
810 return 0;
811 }
812 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
813
814 static int
v4l2_subdev_link_validate_get_format(struct media_pad * pad,struct v4l2_subdev_format * fmt)815 v4l2_subdev_link_validate_get_format(struct media_pad *pad,
816 struct v4l2_subdev_format *fmt)
817 {
818 if (is_media_entity_v4l2_subdev(pad->entity)) {
819 struct v4l2_subdev *sd =
820 media_entity_to_v4l2_subdev(pad->entity);
821
822 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
823 fmt->pad = pad->index;
824 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
825 }
826
827 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
828 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
829 pad->entity->function, pad->entity->name);
830
831 return -EINVAL;
832 }
833
v4l2_subdev_link_validate(struct media_link * link)834 int v4l2_subdev_link_validate(struct media_link *link)
835 {
836 struct v4l2_subdev *sink;
837 struct v4l2_subdev_format sink_fmt, source_fmt;
838 int rval;
839
840 rval = v4l2_subdev_link_validate_get_format(
841 link->source, &source_fmt);
842 if (rval < 0)
843 return 0;
844
845 rval = v4l2_subdev_link_validate_get_format(
846 link->sink, &sink_fmt);
847 if (rval < 0)
848 return 0;
849
850 sink = media_entity_to_v4l2_subdev(link->sink->entity);
851
852 rval = v4l2_subdev_call(sink, pad, link_validate, link,
853 &source_fmt, &sink_fmt);
854 if (rval != -ENOIOCTLCMD)
855 return rval;
856
857 return v4l2_subdev_link_validate_default(
858 sink, link, &source_fmt, &sink_fmt);
859 }
860 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
861
862 struct v4l2_subdev_pad_config *
v4l2_subdev_alloc_pad_config(struct v4l2_subdev * sd)863 v4l2_subdev_alloc_pad_config(struct v4l2_subdev *sd)
864 {
865 struct v4l2_subdev_pad_config *cfg;
866 int ret;
867
868 if (!sd->entity.num_pads)
869 return NULL;
870
871 cfg = kvmalloc_array(sd->entity.num_pads, sizeof(*cfg),
872 GFP_KERNEL | __GFP_ZERO);
873 if (!cfg)
874 return NULL;
875
876 ret = v4l2_subdev_call(sd, pad, init_cfg, cfg);
877 if (ret < 0 && ret != -ENOIOCTLCMD) {
878 kvfree(cfg);
879 return NULL;
880 }
881
882 return cfg;
883 }
884 EXPORT_SYMBOL_GPL(v4l2_subdev_alloc_pad_config);
885
v4l2_subdev_free_pad_config(struct v4l2_subdev_pad_config * cfg)886 void v4l2_subdev_free_pad_config(struct v4l2_subdev_pad_config *cfg)
887 {
888 kvfree(cfg);
889 }
890 EXPORT_SYMBOL_GPL(v4l2_subdev_free_pad_config);
891 #endif /* CONFIG_MEDIA_CONTROLLER */
892
v4l2_subdev_init(struct v4l2_subdev * sd,const struct v4l2_subdev_ops * ops)893 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
894 {
895 INIT_LIST_HEAD(&sd->list);
896 BUG_ON(!ops);
897 sd->ops = ops;
898 sd->v4l2_dev = NULL;
899 sd->flags = 0;
900 sd->name[0] = '\0';
901 sd->grp_id = 0;
902 sd->dev_priv = NULL;
903 sd->host_priv = NULL;
904 #if defined(CONFIG_MEDIA_CONTROLLER)
905 sd->entity.name = sd->name;
906 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
907 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
908 #endif
909 }
910 EXPORT_SYMBOL(v4l2_subdev_init);
911
v4l2_subdev_notify_event(struct v4l2_subdev * sd,const struct v4l2_event * ev)912 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
913 const struct v4l2_event *ev)
914 {
915 v4l2_event_queue(sd->devnode, ev);
916 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
917 }
918 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);
919