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