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 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
116 static int check_format(struct v4l2_subdev *sd,
117 			struct v4l2_subdev_format *format)
118 {
119 	if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
120 	    format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
121 		return -EINVAL;
122 
123 	if (format->pad >= sd->entity.num_pads)
124 		return -EINVAL;
125 
126 	return 0;
127 }
128 
129 static int check_crop(struct v4l2_subdev *sd, struct v4l2_subdev_crop *crop)
130 {
131 	if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
132 	    crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
133 		return -EINVAL;
134 
135 	if (crop->pad >= sd->entity.num_pads)
136 		return -EINVAL;
137 
138 	return 0;
139 }
140 
141 static int check_selection(struct v4l2_subdev *sd,
142 			   struct v4l2_subdev_selection *sel)
143 {
144 	if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
145 	    sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
146 		return -EINVAL;
147 
148 	if (sel->pad >= sd->entity.num_pads)
149 		return -EINVAL;
150 
151 	return 0;
152 }
153 
154 static int check_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
155 {
156 	if (edid->pad >= sd->entity.num_pads)
157 		return -EINVAL;
158 
159 	if (edid->blocks && edid->edid == NULL)
160 		return -EINVAL;
161 
162 	return 0;
163 }
164 #endif
165 
166 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
167 {
168 	struct video_device *vdev = video_devdata(file);
169 	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
170 	struct v4l2_fh *vfh = file->private_data;
171 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
172 	struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
173 	int rval;
174 #endif
175 
176 	switch (cmd) {
177 	case VIDIOC_QUERYCTRL:
178 		/*
179 		 * TODO: this really should be folded into v4l2_queryctrl (this
180 		 * currently returns -EINVAL for NULL control handlers).
181 		 * However, v4l2_queryctrl() is still called directly by
182 		 * drivers as well and until that has been addressed I believe
183 		 * it is safer to do the check here. The same is true for the
184 		 * other control ioctls below.
185 		 */
186 		if (!vfh->ctrl_handler)
187 			return -ENOTTY;
188 		return v4l2_queryctrl(vfh->ctrl_handler, arg);
189 
190 	case VIDIOC_QUERY_EXT_CTRL:
191 		if (!vfh->ctrl_handler)
192 			return -ENOTTY;
193 		return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
194 
195 	case VIDIOC_QUERYMENU:
196 		if (!vfh->ctrl_handler)
197 			return -ENOTTY;
198 		return v4l2_querymenu(vfh->ctrl_handler, arg);
199 
200 	case VIDIOC_G_CTRL:
201 		if (!vfh->ctrl_handler)
202 			return -ENOTTY;
203 		return v4l2_g_ctrl(vfh->ctrl_handler, arg);
204 
205 	case VIDIOC_S_CTRL:
206 		if (!vfh->ctrl_handler)
207 			return -ENOTTY;
208 		return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
209 
210 	case VIDIOC_G_EXT_CTRLS:
211 		if (!vfh->ctrl_handler)
212 			return -ENOTTY;
213 		return v4l2_g_ext_ctrls(vfh->ctrl_handler,
214 					sd->v4l2_dev->mdev, arg);
215 
216 	case VIDIOC_S_EXT_CTRLS:
217 		if (!vfh->ctrl_handler)
218 			return -ENOTTY;
219 		return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
220 					sd->v4l2_dev->mdev, arg);
221 
222 	case VIDIOC_TRY_EXT_CTRLS:
223 		if (!vfh->ctrl_handler)
224 			return -ENOTTY;
225 		return v4l2_try_ext_ctrls(vfh->ctrl_handler,
226 					  sd->v4l2_dev->mdev, arg);
227 
228 	case VIDIOC_DQEVENT:
229 		if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
230 			return -ENOIOCTLCMD;
231 
232 		return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
233 
234 	case VIDIOC_SUBSCRIBE_EVENT:
235 		return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
236 
237 	case VIDIOC_UNSUBSCRIBE_EVENT:
238 		return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
239 
240 #ifdef CONFIG_VIDEO_ADV_DEBUG
241 	case VIDIOC_DBG_G_REGISTER:
242 	{
243 		struct v4l2_dbg_register *p = arg;
244 
245 		if (!capable(CAP_SYS_ADMIN))
246 			return -EPERM;
247 		return v4l2_subdev_call(sd, core, g_register, p);
248 	}
249 	case VIDIOC_DBG_S_REGISTER:
250 	{
251 		struct v4l2_dbg_register *p = arg;
252 
253 		if (!capable(CAP_SYS_ADMIN))
254 			return -EPERM;
255 		return v4l2_subdev_call(sd, core, s_register, p);
256 	}
257 	case VIDIOC_DBG_G_CHIP_INFO:
258 	{
259 		struct v4l2_dbg_chip_info *p = arg;
260 
261 		if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
262 			return -EINVAL;
263 		if (sd->ops->core && sd->ops->core->s_register)
264 			p->flags |= V4L2_CHIP_FL_WRITABLE;
265 		if (sd->ops->core && sd->ops->core->g_register)
266 			p->flags |= V4L2_CHIP_FL_READABLE;
267 		strscpy(p->name, sd->name, sizeof(p->name));
268 		return 0;
269 	}
270 #endif
271 
272 	case VIDIOC_LOG_STATUS: {
273 		int ret;
274 
275 		pr_info("%s: =================  START STATUS  =================\n",
276 			sd->name);
277 		ret = v4l2_subdev_call(sd, core, log_status);
278 		pr_info("%s: ==================  END STATUS  ==================\n",
279 			sd->name);
280 		return ret;
281 	}
282 
283 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
284 	case VIDIOC_SUBDEV_G_FMT: {
285 		struct v4l2_subdev_format *format = arg;
286 
287 		rval = check_format(sd, format);
288 		if (rval)
289 			return rval;
290 
291 		memset(format->reserved, 0, sizeof(format->reserved));
292 		memset(format->format.reserved, 0, sizeof(format->format.reserved));
293 		return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->pad, format);
294 	}
295 
296 	case VIDIOC_SUBDEV_S_FMT: {
297 		struct v4l2_subdev_format *format = arg;
298 
299 		rval = check_format(sd, format);
300 		if (rval)
301 			return rval;
302 
303 		memset(format->reserved, 0, sizeof(format->reserved));
304 		memset(format->format.reserved, 0, sizeof(format->format.reserved));
305 		return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->pad, format);
306 	}
307 
308 	case VIDIOC_SUBDEV_G_CROP: {
309 		struct v4l2_subdev_crop *crop = arg;
310 		struct v4l2_subdev_selection sel;
311 
312 		rval = check_crop(sd, crop);
313 		if (rval)
314 			return rval;
315 
316 		memset(crop->reserved, 0, sizeof(crop->reserved));
317 		memset(&sel, 0, sizeof(sel));
318 		sel.which = crop->which;
319 		sel.pad = crop->pad;
320 		sel.target = V4L2_SEL_TGT_CROP;
321 
322 		rval = v4l2_subdev_call(
323 			sd, pad, get_selection, subdev_fh->pad, &sel);
324 
325 		crop->rect = sel.r;
326 
327 		return rval;
328 	}
329 
330 	case VIDIOC_SUBDEV_S_CROP: {
331 		struct v4l2_subdev_crop *crop = arg;
332 		struct v4l2_subdev_selection sel;
333 
334 		memset(crop->reserved, 0, sizeof(crop->reserved));
335 		rval = check_crop(sd, crop);
336 		if (rval)
337 			return rval;
338 
339 		memset(&sel, 0, sizeof(sel));
340 		sel.which = crop->which;
341 		sel.pad = crop->pad;
342 		sel.target = V4L2_SEL_TGT_CROP;
343 		sel.r = crop->rect;
344 
345 		rval = v4l2_subdev_call(
346 			sd, pad, set_selection, subdev_fh->pad, &sel);
347 
348 		crop->rect = sel.r;
349 
350 		return rval;
351 	}
352 
353 	case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
354 		struct v4l2_subdev_mbus_code_enum *code = arg;
355 
356 		if (code->which != V4L2_SUBDEV_FORMAT_TRY &&
357 		    code->which != V4L2_SUBDEV_FORMAT_ACTIVE)
358 			return -EINVAL;
359 
360 		if (code->pad >= sd->entity.num_pads)
361 			return -EINVAL;
362 
363 		memset(code->reserved, 0, sizeof(code->reserved));
364 		return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->pad,
365 					code);
366 	}
367 
368 	case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
369 		struct v4l2_subdev_frame_size_enum *fse = arg;
370 
371 		if (fse->which != V4L2_SUBDEV_FORMAT_TRY &&
372 		    fse->which != V4L2_SUBDEV_FORMAT_ACTIVE)
373 			return -EINVAL;
374 
375 		if (fse->pad >= sd->entity.num_pads)
376 			return -EINVAL;
377 
378 		memset(fse->reserved, 0, sizeof(fse->reserved));
379 		return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->pad,
380 					fse);
381 	}
382 
383 	case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
384 		struct v4l2_subdev_frame_interval *fi = arg;
385 
386 		if (fi->pad >= sd->entity.num_pads)
387 			return -EINVAL;
388 
389 		memset(fi->reserved, 0, sizeof(fi->reserved));
390 		return v4l2_subdev_call(sd, video, g_frame_interval, arg);
391 	}
392 
393 	case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
394 		struct v4l2_subdev_frame_interval *fi = arg;
395 
396 		if (fi->pad >= sd->entity.num_pads)
397 			return -EINVAL;
398 
399 		memset(fi->reserved, 0, sizeof(fi->reserved));
400 		return v4l2_subdev_call(sd, video, s_frame_interval, arg);
401 	}
402 
403 	case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
404 		struct v4l2_subdev_frame_interval_enum *fie = arg;
405 
406 		if (fie->which != V4L2_SUBDEV_FORMAT_TRY &&
407 		    fie->which != V4L2_SUBDEV_FORMAT_ACTIVE)
408 			return -EINVAL;
409 
410 		if (fie->pad >= sd->entity.num_pads)
411 			return -EINVAL;
412 
413 		memset(fie->reserved, 0, sizeof(fie->reserved));
414 		return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->pad,
415 					fie);
416 	}
417 
418 	case VIDIOC_SUBDEV_G_SELECTION: {
419 		struct v4l2_subdev_selection *sel = arg;
420 
421 		rval = check_selection(sd, sel);
422 		if (rval)
423 			return rval;
424 
425 		memset(sel->reserved, 0, sizeof(sel->reserved));
426 		return v4l2_subdev_call(
427 			sd, pad, get_selection, subdev_fh->pad, sel);
428 	}
429 
430 	case VIDIOC_SUBDEV_S_SELECTION: {
431 		struct v4l2_subdev_selection *sel = arg;
432 
433 		rval = check_selection(sd, sel);
434 		if (rval)
435 			return rval;
436 
437 		memset(sel->reserved, 0, sizeof(sel->reserved));
438 		return v4l2_subdev_call(
439 			sd, pad, set_selection, subdev_fh->pad, sel);
440 	}
441 
442 	case VIDIOC_G_EDID: {
443 		struct v4l2_subdev_edid *edid = arg;
444 
445 		rval = check_edid(sd, edid);
446 		if (rval)
447 			return rval;
448 
449 		return v4l2_subdev_call(sd, pad, get_edid, edid);
450 	}
451 
452 	case VIDIOC_S_EDID: {
453 		struct v4l2_subdev_edid *edid = arg;
454 
455 		rval = check_edid(sd, edid);
456 		if (rval)
457 			return rval;
458 
459 		return v4l2_subdev_call(sd, pad, set_edid, edid);
460 	}
461 
462 	case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
463 		struct v4l2_dv_timings_cap *cap = arg;
464 
465 		if (cap->pad >= sd->entity.num_pads)
466 			return -EINVAL;
467 
468 		return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
469 	}
470 
471 	case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
472 		struct v4l2_enum_dv_timings *dvt = arg;
473 
474 		if (dvt->pad >= sd->entity.num_pads)
475 			return -EINVAL;
476 
477 		return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
478 	}
479 
480 	case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
481 		return v4l2_subdev_call(sd, video, query_dv_timings, arg);
482 
483 	case VIDIOC_SUBDEV_G_DV_TIMINGS:
484 		return v4l2_subdev_call(sd, video, g_dv_timings, arg);
485 
486 	case VIDIOC_SUBDEV_S_DV_TIMINGS:
487 		return v4l2_subdev_call(sd, video, s_dv_timings, arg);
488 
489 	case VIDIOC_SUBDEV_G_STD:
490 		return v4l2_subdev_call(sd, video, g_std, arg);
491 
492 	case VIDIOC_SUBDEV_S_STD: {
493 		v4l2_std_id *std = arg;
494 
495 		return v4l2_subdev_call(sd, video, s_std, *std);
496 	}
497 
498 	case VIDIOC_SUBDEV_ENUMSTD: {
499 		struct v4l2_standard *p = arg;
500 		v4l2_std_id id;
501 
502 		if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
503 			return -EINVAL;
504 
505 		return v4l_video_std_enumstd(p, id);
506 	}
507 
508 	case VIDIOC_SUBDEV_QUERYSTD:
509 		return v4l2_subdev_call(sd, video, querystd, arg);
510 #endif
511 	default:
512 		return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
513 	}
514 
515 	return 0;
516 }
517 
518 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
519 {
520 	struct video_device *vdev = video_devdata(file);
521 	struct mutex *lock = vdev->lock;
522 	long ret = -ENODEV;
523 
524 	if (lock && mutex_lock_interruptible(lock))
525 		return -ERESTARTSYS;
526 	if (video_is_registered(vdev))
527 		ret = subdev_do_ioctl(file, cmd, arg);
528 	if (lock)
529 		mutex_unlock(lock);
530 	return ret;
531 }
532 
533 static long subdev_ioctl(struct file *file, unsigned int cmd,
534 	unsigned long arg)
535 {
536 	return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
537 }
538 
539 #ifdef CONFIG_COMPAT
540 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
541 	unsigned long arg)
542 {
543 	struct video_device *vdev = video_devdata(file);
544 	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
545 
546 	return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
547 }
548 #endif
549 
550 static __poll_t subdev_poll(struct file *file, poll_table *wait)
551 {
552 	struct video_device *vdev = video_devdata(file);
553 	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
554 	struct v4l2_fh *fh = file->private_data;
555 
556 	if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
557 		return EPOLLERR;
558 
559 	poll_wait(file, &fh->wait, wait);
560 
561 	if (v4l2_event_pending(fh))
562 		return EPOLLPRI;
563 
564 	return 0;
565 }
566 
567 const struct v4l2_file_operations v4l2_subdev_fops = {
568 	.owner = THIS_MODULE,
569 	.open = subdev_open,
570 	.unlocked_ioctl = subdev_ioctl,
571 #ifdef CONFIG_COMPAT
572 	.compat_ioctl32 = subdev_compat_ioctl32,
573 #endif
574 	.release = subdev_close,
575 	.poll = subdev_poll,
576 };
577 
578 #ifdef CONFIG_MEDIA_CONTROLLER
579 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
580 				      struct media_link *link,
581 				      struct v4l2_subdev_format *source_fmt,
582 				      struct v4l2_subdev_format *sink_fmt)
583 {
584 	/* The width, height and code must match. */
585 	if (source_fmt->format.width != sink_fmt->format.width
586 	    || source_fmt->format.height != sink_fmt->format.height
587 	    || source_fmt->format.code != sink_fmt->format.code)
588 		return -EPIPE;
589 
590 	/* The field order must match, or the sink field order must be NONE
591 	 * to support interlaced hardware connected to bridges that support
592 	 * progressive formats only.
593 	 */
594 	if (source_fmt->format.field != sink_fmt->format.field &&
595 	    sink_fmt->format.field != V4L2_FIELD_NONE)
596 		return -EPIPE;
597 
598 	return 0;
599 }
600 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
601 
602 static int
603 v4l2_subdev_link_validate_get_format(struct media_pad *pad,
604 				     struct v4l2_subdev_format *fmt)
605 {
606 	if (is_media_entity_v4l2_subdev(pad->entity)) {
607 		struct v4l2_subdev *sd =
608 			media_entity_to_v4l2_subdev(pad->entity);
609 
610 		fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
611 		fmt->pad = pad->index;
612 		return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
613 	}
614 
615 	WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
616 	     "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
617 	     pad->entity->function, pad->entity->name);
618 
619 	return -EINVAL;
620 }
621 
622 int v4l2_subdev_link_validate(struct media_link *link)
623 {
624 	struct v4l2_subdev *sink;
625 	struct v4l2_subdev_format sink_fmt, source_fmt;
626 	int rval;
627 
628 	rval = v4l2_subdev_link_validate_get_format(
629 		link->source, &source_fmt);
630 	if (rval < 0)
631 		return 0;
632 
633 	rval = v4l2_subdev_link_validate_get_format(
634 		link->sink, &sink_fmt);
635 	if (rval < 0)
636 		return 0;
637 
638 	sink = media_entity_to_v4l2_subdev(link->sink->entity);
639 
640 	rval = v4l2_subdev_call(sink, pad, link_validate, link,
641 				&source_fmt, &sink_fmt);
642 	if (rval != -ENOIOCTLCMD)
643 		return rval;
644 
645 	return v4l2_subdev_link_validate_default(
646 		sink, link, &source_fmt, &sink_fmt);
647 }
648 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
649 
650 struct v4l2_subdev_pad_config *
651 v4l2_subdev_alloc_pad_config(struct v4l2_subdev *sd)
652 {
653 	struct v4l2_subdev_pad_config *cfg;
654 	int ret;
655 
656 	if (!sd->entity.num_pads)
657 		return NULL;
658 
659 	cfg = kvmalloc_array(sd->entity.num_pads, sizeof(*cfg),
660 			     GFP_KERNEL | __GFP_ZERO);
661 	if (!cfg)
662 		return NULL;
663 
664 	ret = v4l2_subdev_call(sd, pad, init_cfg, cfg);
665 	if (ret < 0 && ret != -ENOIOCTLCMD) {
666 		kvfree(cfg);
667 		return NULL;
668 	}
669 
670 	return cfg;
671 }
672 EXPORT_SYMBOL_GPL(v4l2_subdev_alloc_pad_config);
673 
674 void v4l2_subdev_free_pad_config(struct v4l2_subdev_pad_config *cfg)
675 {
676 	kvfree(cfg);
677 }
678 EXPORT_SYMBOL_GPL(v4l2_subdev_free_pad_config);
679 #endif /* CONFIG_MEDIA_CONTROLLER */
680 
681 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
682 {
683 	INIT_LIST_HEAD(&sd->list);
684 	BUG_ON(!ops);
685 	sd->ops = ops;
686 	sd->v4l2_dev = NULL;
687 	sd->flags = 0;
688 	sd->name[0] = '\0';
689 	sd->grp_id = 0;
690 	sd->dev_priv = NULL;
691 	sd->host_priv = NULL;
692 #if defined(CONFIG_MEDIA_CONTROLLER)
693 	sd->entity.name = sd->name;
694 	sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
695 	sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
696 #endif
697 }
698 EXPORT_SYMBOL(v4l2_subdev_init);
699 
700 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
701 			      const struct v4l2_event *ev)
702 {
703 	v4l2_event_queue(sd->devnode, ev);
704 	v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
705 }
706 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);
707