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