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 if (ret)
501 return ret;
502
503 memset(format->reserved, 0, sizeof(format->reserved));
504 memset(format->format.reserved, 0, sizeof(format->format.reserved));
505 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->pad, format);
506 }
507
508 case VIDIOC_SUBDEV_G_CROP: {
509 struct v4l2_subdev_crop *crop = arg;
510 struct v4l2_subdev_selection sel;
511
512 memset(crop->reserved, 0, sizeof(crop->reserved));
513 memset(&sel, 0, sizeof(sel));
514 sel.which = crop->which;
515 sel.pad = crop->pad;
516 sel.target = V4L2_SEL_TGT_CROP;
517
518 rval = v4l2_subdev_call(
519 sd, pad, get_selection, subdev_fh->pad, &sel);
520
521 crop->rect = sel.r;
522
523 return rval;
524 }
525
526 case VIDIOC_SUBDEV_S_CROP: {
527 struct v4l2_subdev_crop *crop = arg;
528 struct v4l2_subdev_selection sel;
529
530 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
531 return -EPERM;
532
533 memset(crop->reserved, 0, sizeof(crop->reserved));
534 memset(&sel, 0, sizeof(sel));
535 sel.which = crop->which;
536 sel.pad = crop->pad;
537 sel.target = V4L2_SEL_TGT_CROP;
538 sel.r = crop->rect;
539
540 rval = v4l2_subdev_call(
541 sd, pad, set_selection, subdev_fh->pad, &sel);
542
543 crop->rect = sel.r;
544
545 return rval;
546 }
547
548 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
549 struct v4l2_subdev_mbus_code_enum *code = arg;
550
551 memset(code->reserved, 0, sizeof(code->reserved));
552 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->pad,
553 code);
554 }
555
556 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
557 struct v4l2_subdev_frame_size_enum *fse = arg;
558
559 memset(fse->reserved, 0, sizeof(fse->reserved));
560 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->pad,
561 fse);
562 }
563
564 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
565 struct v4l2_subdev_frame_interval *fi = arg;
566
567 memset(fi->reserved, 0, sizeof(fi->reserved));
568 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
569 }
570
571 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
572 struct v4l2_subdev_frame_interval *fi = arg;
573 int ret = 0;
574
575 if (ro_subdev)
576 return -EPERM;
577
578 trace_android_vh_v4l2subdev_set_frame_interval(sd, fi, &ret);
579 if (ret)
580 return ret;
581
582 memset(fi->reserved, 0, sizeof(fi->reserved));
583 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
584 }
585
586 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
587 struct v4l2_subdev_frame_interval_enum *fie = arg;
588
589 memset(fie->reserved, 0, sizeof(fie->reserved));
590 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->pad,
591 fie);
592 }
593
594 case VIDIOC_SUBDEV_G_SELECTION: {
595 struct v4l2_subdev_selection *sel = arg;
596
597 memset(sel->reserved, 0, sizeof(sel->reserved));
598 return v4l2_subdev_call(
599 sd, pad, get_selection, subdev_fh->pad, sel);
600 }
601
602 case VIDIOC_SUBDEV_S_SELECTION: {
603 struct v4l2_subdev_selection *sel = arg;
604 int ret = 0;
605
606 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
607 return -EPERM;
608
609 trace_android_vh_v4l2subdev_set_selection(sd, subdev_fh->pad,
610 sel, &ret);
611 if (ret)
612 return ret;
613
614 memset(sel->reserved, 0, sizeof(sel->reserved));
615 return v4l2_subdev_call(
616 sd, pad, set_selection, subdev_fh->pad, sel);
617 }
618
619 case VIDIOC_G_EDID: {
620 struct v4l2_subdev_edid *edid = arg;
621
622 return v4l2_subdev_call(sd, pad, get_edid, edid);
623 }
624
625 case VIDIOC_S_EDID: {
626 struct v4l2_subdev_edid *edid = arg;
627
628 return v4l2_subdev_call(sd, pad, set_edid, edid);
629 }
630
631 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
632 struct v4l2_dv_timings_cap *cap = arg;
633
634 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
635 }
636
637 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
638 struct v4l2_enum_dv_timings *dvt = arg;
639
640 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
641 }
642
643 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
644 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
645
646 case VIDIOC_SUBDEV_G_DV_TIMINGS:
647 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
648
649 case VIDIOC_SUBDEV_S_DV_TIMINGS:
650 if (ro_subdev)
651 return -EPERM;
652
653 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
654
655 case VIDIOC_SUBDEV_G_STD:
656 return v4l2_subdev_call(sd, video, g_std, arg);
657
658 case VIDIOC_SUBDEV_S_STD: {
659 v4l2_std_id *std = arg;
660
661 if (ro_subdev)
662 return -EPERM;
663
664 return v4l2_subdev_call(sd, video, s_std, *std);
665 }
666
667 case VIDIOC_SUBDEV_ENUMSTD: {
668 struct v4l2_standard *p = arg;
669 v4l2_std_id id;
670
671 if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
672 return -EINVAL;
673
674 return v4l_video_std_enumstd(p, id);
675 }
676
677 case VIDIOC_SUBDEV_QUERYSTD:
678 return v4l2_subdev_call(sd, video, querystd, arg);
679
680 default:
681 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
682 }
683
684 return 0;
685 }
686
subdev_do_ioctl_lock(struct file * file,unsigned int cmd,void * arg)687 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
688 {
689 struct video_device *vdev = video_devdata(file);
690 struct mutex *lock = vdev->lock;
691 long ret = -ENODEV;
692
693 if (lock && mutex_lock_interruptible(lock))
694 return -ERESTARTSYS;
695 if (video_is_registered(vdev))
696 ret = subdev_do_ioctl(file, cmd, arg);
697 if (lock)
698 mutex_unlock(lock);
699 return ret;
700 }
701
subdev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)702 static long subdev_ioctl(struct file *file, unsigned int cmd,
703 unsigned long arg)
704 {
705 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
706 }
707
708 #ifdef CONFIG_COMPAT
subdev_compat_ioctl32(struct file * file,unsigned int cmd,unsigned long arg)709 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
710 unsigned long arg)
711 {
712 struct video_device *vdev = video_devdata(file);
713 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
714
715 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
716 }
717 #endif
718
719 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
subdev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)720 static long subdev_ioctl(struct file *file, unsigned int cmd,
721 unsigned long arg)
722 {
723 return -ENODEV;
724 }
725
726 #ifdef CONFIG_COMPAT
subdev_compat_ioctl32(struct file * file,unsigned int cmd,unsigned long arg)727 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
728 unsigned long arg)
729 {
730 return -ENODEV;
731 }
732 #endif
733 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
734
subdev_poll(struct file * file,poll_table * wait)735 static __poll_t subdev_poll(struct file *file, poll_table *wait)
736 {
737 struct video_device *vdev = video_devdata(file);
738 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
739 struct v4l2_fh *fh = file->private_data;
740
741 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
742 return EPOLLERR;
743
744 poll_wait(file, &fh->wait, wait);
745
746 if (v4l2_event_pending(fh))
747 return EPOLLPRI;
748
749 return 0;
750 }
751
752 const struct v4l2_file_operations v4l2_subdev_fops = {
753 .owner = THIS_MODULE,
754 .open = subdev_open,
755 .unlocked_ioctl = subdev_ioctl,
756 #ifdef CONFIG_COMPAT
757 .compat_ioctl32 = subdev_compat_ioctl32,
758 #endif
759 .release = subdev_close,
760 .poll = subdev_poll,
761 };
762
763 #ifdef CONFIG_MEDIA_CONTROLLER
764
v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity * entity,struct fwnode_endpoint * endpoint)765 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
766 struct fwnode_endpoint *endpoint)
767 {
768 struct fwnode_handle *fwnode;
769 struct v4l2_subdev *sd;
770
771 if (!is_media_entity_v4l2_subdev(entity))
772 return -EINVAL;
773
774 sd = media_entity_to_v4l2_subdev(entity);
775
776 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
777 fwnode_handle_put(fwnode);
778
779 if (dev_fwnode(sd->dev) == fwnode)
780 return endpoint->port;
781
782 return -ENXIO;
783 }
784 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
785
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)786 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
787 struct media_link *link,
788 struct v4l2_subdev_format *source_fmt,
789 struct v4l2_subdev_format *sink_fmt)
790 {
791 /* The width, height and code must match. */
792 if (source_fmt->format.width != sink_fmt->format.width
793 || source_fmt->format.height != sink_fmt->format.height
794 || source_fmt->format.code != sink_fmt->format.code)
795 return -EPIPE;
796
797 /* The field order must match, or the sink field order must be NONE
798 * to support interlaced hardware connected to bridges that support
799 * progressive formats only.
800 */
801 if (source_fmt->format.field != sink_fmt->format.field &&
802 sink_fmt->format.field != V4L2_FIELD_NONE)
803 return -EPIPE;
804
805 return 0;
806 }
807 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
808
809 static int
v4l2_subdev_link_validate_get_format(struct media_pad * pad,struct v4l2_subdev_format * fmt)810 v4l2_subdev_link_validate_get_format(struct media_pad *pad,
811 struct v4l2_subdev_format *fmt)
812 {
813 if (is_media_entity_v4l2_subdev(pad->entity)) {
814 struct v4l2_subdev *sd =
815 media_entity_to_v4l2_subdev(pad->entity);
816
817 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
818 fmt->pad = pad->index;
819 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
820 }
821
822 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
823 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
824 pad->entity->function, pad->entity->name);
825
826 return -EINVAL;
827 }
828
v4l2_subdev_link_validate(struct media_link * link)829 int v4l2_subdev_link_validate(struct media_link *link)
830 {
831 struct v4l2_subdev *sink;
832 struct v4l2_subdev_format sink_fmt, source_fmt;
833 int rval;
834
835 rval = v4l2_subdev_link_validate_get_format(
836 link->source, &source_fmt);
837 if (rval < 0)
838 return 0;
839
840 rval = v4l2_subdev_link_validate_get_format(
841 link->sink, &sink_fmt);
842 if (rval < 0)
843 return 0;
844
845 sink = media_entity_to_v4l2_subdev(link->sink->entity);
846
847 rval = v4l2_subdev_call(sink, pad, link_validate, link,
848 &source_fmt, &sink_fmt);
849 if (rval != -ENOIOCTLCMD)
850 return rval;
851
852 return v4l2_subdev_link_validate_default(
853 sink, link, &source_fmt, &sink_fmt);
854 }
855 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
856
857 struct v4l2_subdev_pad_config *
v4l2_subdev_alloc_pad_config(struct v4l2_subdev * sd)858 v4l2_subdev_alloc_pad_config(struct v4l2_subdev *sd)
859 {
860 struct v4l2_subdev_pad_config *cfg;
861 int ret;
862
863 if (!sd->entity.num_pads)
864 return NULL;
865
866 cfg = kvmalloc_array(sd->entity.num_pads, sizeof(*cfg),
867 GFP_KERNEL | __GFP_ZERO);
868 if (!cfg)
869 return NULL;
870
871 ret = v4l2_subdev_call(sd, pad, init_cfg, cfg);
872 if (ret < 0 && ret != -ENOIOCTLCMD) {
873 kvfree(cfg);
874 return NULL;
875 }
876
877 return cfg;
878 }
879 EXPORT_SYMBOL_GPL(v4l2_subdev_alloc_pad_config);
880
v4l2_subdev_free_pad_config(struct v4l2_subdev_pad_config * cfg)881 void v4l2_subdev_free_pad_config(struct v4l2_subdev_pad_config *cfg)
882 {
883 kvfree(cfg);
884 }
885 EXPORT_SYMBOL_GPL(v4l2_subdev_free_pad_config);
886 #endif /* CONFIG_MEDIA_CONTROLLER */
887
v4l2_subdev_init(struct v4l2_subdev * sd,const struct v4l2_subdev_ops * ops)888 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
889 {
890 INIT_LIST_HEAD(&sd->list);
891 BUG_ON(!ops);
892 sd->ops = ops;
893 sd->v4l2_dev = NULL;
894 sd->flags = 0;
895 sd->name[0] = '\0';
896 sd->grp_id = 0;
897 sd->dev_priv = NULL;
898 sd->host_priv = NULL;
899 #if defined(CONFIG_MEDIA_CONTROLLER)
900 sd->entity.name = sd->name;
901 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
902 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
903 #endif
904 }
905 EXPORT_SYMBOL(v4l2_subdev_init);
906
v4l2_subdev_notify_event(struct v4l2_subdev * sd,const struct v4l2_event * ev)907 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
908 const struct v4l2_event *ev)
909 {
910 v4l2_event_queue(sd->devnode, ev);
911 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
912 }
913 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);
914