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