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