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 	static struct lock_class_key key;
31 
32 	state = __v4l2_subdev_state_alloc(sd, "fh->state->lock", &key);
33 	if (IS_ERR(state))
34 		return PTR_ERR(state);
35 
36 	fh->state = state;
37 
38 	return 0;
39 }
40 
41 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
42 {
43 	__v4l2_subdev_state_free(fh->state);
44 	fh->state = NULL;
45 }
46 
47 static int subdev_open(struct file *file)
48 {
49 	struct video_device *vdev = video_devdata(file);
50 	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
51 	struct v4l2_subdev_fh *subdev_fh;
52 	int ret;
53 
54 	subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
55 	if (subdev_fh == NULL)
56 		return -ENOMEM;
57 
58 	ret = subdev_fh_init(subdev_fh, sd);
59 	if (ret) {
60 		kfree(subdev_fh);
61 		return ret;
62 	}
63 
64 	v4l2_fh_init(&subdev_fh->vfh, vdev);
65 	v4l2_fh_add(&subdev_fh->vfh);
66 	file->private_data = &subdev_fh->vfh;
67 
68 	if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
69 		struct module *owner;
70 
71 		owner = sd->entity.graph_obj.mdev->dev->driver->owner;
72 		if (!try_module_get(owner)) {
73 			ret = -EBUSY;
74 			goto err;
75 		}
76 		subdev_fh->owner = owner;
77 	}
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_s_stream(struct v4l2_subdev *sd, int enable)
322 {
323 	int ret;
324 
325 	ret = sd->ops->video->s_stream(sd, enable);
326 
327 	if (!enable && ret < 0) {
328 		dev_warn(sd->dev, "disabling streaming failed (%d)\n", ret);
329 		return 0;
330 	}
331 
332 	return ret;
333 }
334 
335 #ifdef CONFIG_MEDIA_CONTROLLER
336 /*
337  * Create state-management wrapper for pad ops dealing with subdev state. The
338  * wrapper handles the case where the caller does not provide the called
339  * subdev's state. This should be removed when all the callers are fixed.
340  */
341 #define DEFINE_STATE_WRAPPER(f, arg_type)                                  \
342 	static int call_##f##_state(struct v4l2_subdev *sd,                \
343 				    struct v4l2_subdev_state *_state,      \
344 				    arg_type *arg)                         \
345 	{                                                                  \
346 		struct v4l2_subdev_state *state = _state;                  \
347 		int ret;                                                   \
348 		if (!_state)                                               \
349 			state = v4l2_subdev_lock_and_get_active_state(sd); \
350 		ret = call_##f(sd, state, arg);                            \
351 		if (!_state && state)                                      \
352 			v4l2_subdev_unlock_state(state);                   \
353 		return ret;                                                \
354 	}
355 
356 #else /* CONFIG_MEDIA_CONTROLLER */
357 
358 #define DEFINE_STATE_WRAPPER(f, arg_type)                            \
359 	static int call_##f##_state(struct v4l2_subdev *sd,          \
360 				    struct v4l2_subdev_state *state, \
361 				    arg_type *arg)                   \
362 	{                                                            \
363 		return call_##f(sd, state, arg);                     \
364 	}
365 
366 #endif /* CONFIG_MEDIA_CONTROLLER */
367 
368 DEFINE_STATE_WRAPPER(get_fmt, struct v4l2_subdev_format);
369 DEFINE_STATE_WRAPPER(set_fmt, struct v4l2_subdev_format);
370 DEFINE_STATE_WRAPPER(enum_mbus_code, struct v4l2_subdev_mbus_code_enum);
371 DEFINE_STATE_WRAPPER(enum_frame_size, struct v4l2_subdev_frame_size_enum);
372 DEFINE_STATE_WRAPPER(enum_frame_interval, struct v4l2_subdev_frame_interval_enum);
373 DEFINE_STATE_WRAPPER(get_selection, struct v4l2_subdev_selection);
374 DEFINE_STATE_WRAPPER(set_selection, struct v4l2_subdev_selection);
375 
376 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = {
377 	.get_fmt		= call_get_fmt_state,
378 	.set_fmt		= call_set_fmt_state,
379 	.enum_mbus_code		= call_enum_mbus_code_state,
380 	.enum_frame_size	= call_enum_frame_size_state,
381 	.enum_frame_interval	= call_enum_frame_interval_state,
382 	.get_selection		= call_get_selection_state,
383 	.set_selection		= call_set_selection_state,
384 	.get_edid		= call_get_edid,
385 	.set_edid		= call_set_edid,
386 	.dv_timings_cap		= call_dv_timings_cap,
387 	.enum_dv_timings	= call_enum_dv_timings,
388 	.get_mbus_config	= call_get_mbus_config,
389 };
390 
391 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = {
392 	.g_frame_interval	= call_g_frame_interval,
393 	.s_frame_interval	= call_s_frame_interval,
394 	.s_stream		= call_s_stream,
395 };
396 
397 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = {
398 	.pad	= &v4l2_subdev_call_pad_wrappers,
399 	.video	= &v4l2_subdev_call_video_wrappers,
400 };
401 EXPORT_SYMBOL(v4l2_subdev_call_wrappers);
402 
403 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
404 
405 static struct v4l2_subdev_state *
406 subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh,
407 		       unsigned int cmd, void *arg)
408 {
409 	u32 which;
410 
411 	switch (cmd) {
412 	default:
413 		return NULL;
414 	case VIDIOC_SUBDEV_G_FMT:
415 	case VIDIOC_SUBDEV_S_FMT:
416 		which = ((struct v4l2_subdev_format *)arg)->which;
417 		break;
418 	case VIDIOC_SUBDEV_G_CROP:
419 	case VIDIOC_SUBDEV_S_CROP:
420 		which = ((struct v4l2_subdev_crop *)arg)->which;
421 		break;
422 	case VIDIOC_SUBDEV_ENUM_MBUS_CODE:
423 		which = ((struct v4l2_subdev_mbus_code_enum *)arg)->which;
424 		break;
425 	case VIDIOC_SUBDEV_ENUM_FRAME_SIZE:
426 		which = ((struct v4l2_subdev_frame_size_enum *)arg)->which;
427 		break;
428 	case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL:
429 		which = ((struct v4l2_subdev_frame_interval_enum *)arg)->which;
430 		break;
431 	case VIDIOC_SUBDEV_G_SELECTION:
432 	case VIDIOC_SUBDEV_S_SELECTION:
433 		which = ((struct v4l2_subdev_selection *)arg)->which;
434 		break;
435 	}
436 
437 	return which == V4L2_SUBDEV_FORMAT_TRY ?
438 			     subdev_fh->state :
439 			     v4l2_subdev_get_unlocked_active_state(sd);
440 }
441 
442 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
443 			    struct v4l2_subdev_state *state)
444 {
445 	struct video_device *vdev = video_devdata(file);
446 	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
447 	struct v4l2_fh *vfh = file->private_data;
448 	bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
449 	int rval;
450 
451 	switch (cmd) {
452 	case VIDIOC_SUBDEV_QUERYCAP: {
453 		struct v4l2_subdev_capability *cap = arg;
454 
455 		memset(cap->reserved, 0, sizeof(cap->reserved));
456 		cap->version = LINUX_VERSION_CODE;
457 		cap->capabilities = ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0;
458 
459 		return 0;
460 	}
461 
462 	case VIDIOC_QUERYCTRL:
463 		/*
464 		 * TODO: this really should be folded into v4l2_queryctrl (this
465 		 * currently returns -EINVAL for NULL control handlers).
466 		 * However, v4l2_queryctrl() is still called directly by
467 		 * drivers as well and until that has been addressed I believe
468 		 * it is safer to do the check here. The same is true for the
469 		 * other control ioctls below.
470 		 */
471 		if (!vfh->ctrl_handler)
472 			return -ENOTTY;
473 		return v4l2_queryctrl(vfh->ctrl_handler, arg);
474 
475 	case VIDIOC_QUERY_EXT_CTRL:
476 		if (!vfh->ctrl_handler)
477 			return -ENOTTY;
478 		return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
479 
480 	case VIDIOC_QUERYMENU:
481 		if (!vfh->ctrl_handler)
482 			return -ENOTTY;
483 		return v4l2_querymenu(vfh->ctrl_handler, arg);
484 
485 	case VIDIOC_G_CTRL:
486 		if (!vfh->ctrl_handler)
487 			return -ENOTTY;
488 		return v4l2_g_ctrl(vfh->ctrl_handler, arg);
489 
490 	case VIDIOC_S_CTRL:
491 		if (!vfh->ctrl_handler)
492 			return -ENOTTY;
493 		return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
494 
495 	case VIDIOC_G_EXT_CTRLS:
496 		if (!vfh->ctrl_handler)
497 			return -ENOTTY;
498 		return v4l2_g_ext_ctrls(vfh->ctrl_handler,
499 					vdev, sd->v4l2_dev->mdev, arg);
500 
501 	case VIDIOC_S_EXT_CTRLS:
502 		if (!vfh->ctrl_handler)
503 			return -ENOTTY;
504 		return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
505 					vdev, sd->v4l2_dev->mdev, arg);
506 
507 	case VIDIOC_TRY_EXT_CTRLS:
508 		if (!vfh->ctrl_handler)
509 			return -ENOTTY;
510 		return v4l2_try_ext_ctrls(vfh->ctrl_handler,
511 					  vdev, sd->v4l2_dev->mdev, arg);
512 
513 	case VIDIOC_DQEVENT:
514 		if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
515 			return -ENOIOCTLCMD;
516 
517 		return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
518 
519 	case VIDIOC_SUBSCRIBE_EVENT:
520 		return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
521 
522 	case VIDIOC_UNSUBSCRIBE_EVENT:
523 		return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
524 
525 #ifdef CONFIG_VIDEO_ADV_DEBUG
526 	case VIDIOC_DBG_G_REGISTER:
527 	{
528 		struct v4l2_dbg_register *p = arg;
529 
530 		if (!capable(CAP_SYS_ADMIN))
531 			return -EPERM;
532 		return v4l2_subdev_call(sd, core, g_register, p);
533 	}
534 	case VIDIOC_DBG_S_REGISTER:
535 	{
536 		struct v4l2_dbg_register *p = arg;
537 
538 		if (!capable(CAP_SYS_ADMIN))
539 			return -EPERM;
540 		return v4l2_subdev_call(sd, core, s_register, p);
541 	}
542 	case VIDIOC_DBG_G_CHIP_INFO:
543 	{
544 		struct v4l2_dbg_chip_info *p = arg;
545 
546 		if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
547 			return -EINVAL;
548 		if (sd->ops->core && sd->ops->core->s_register)
549 			p->flags |= V4L2_CHIP_FL_WRITABLE;
550 		if (sd->ops->core && sd->ops->core->g_register)
551 			p->flags |= V4L2_CHIP_FL_READABLE;
552 		strscpy(p->name, sd->name, sizeof(p->name));
553 		return 0;
554 	}
555 #endif
556 
557 	case VIDIOC_LOG_STATUS: {
558 		int ret;
559 
560 		pr_info("%s: =================  START STATUS  =================\n",
561 			sd->name);
562 		ret = v4l2_subdev_call(sd, core, log_status);
563 		pr_info("%s: ==================  END STATUS  ==================\n",
564 			sd->name);
565 		return ret;
566 	}
567 
568 	case VIDIOC_SUBDEV_G_FMT: {
569 		struct v4l2_subdev_format *format = arg;
570 
571 		memset(format->reserved, 0, sizeof(format->reserved));
572 		memset(format->format.reserved, 0, sizeof(format->format.reserved));
573 		return v4l2_subdev_call(sd, pad, get_fmt, state, format);
574 	}
575 
576 	case VIDIOC_SUBDEV_S_FMT: {
577 		struct v4l2_subdev_format *format = arg;
578 
579 		if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
580 			return -EPERM;
581 
582 		memset(format->reserved, 0, sizeof(format->reserved));
583 		memset(format->format.reserved, 0, sizeof(format->format.reserved));
584 		return v4l2_subdev_call(sd, pad, set_fmt, state, format);
585 	}
586 
587 	case VIDIOC_SUBDEV_G_CROP: {
588 		struct v4l2_subdev_crop *crop = arg;
589 		struct v4l2_subdev_selection sel;
590 
591 		memset(crop->reserved, 0, sizeof(crop->reserved));
592 		memset(&sel, 0, sizeof(sel));
593 		sel.which = crop->which;
594 		sel.pad = crop->pad;
595 		sel.target = V4L2_SEL_TGT_CROP;
596 
597 		rval = v4l2_subdev_call(
598 			sd, pad, get_selection, state, &sel);
599 
600 		crop->rect = sel.r;
601 
602 		return rval;
603 	}
604 
605 	case VIDIOC_SUBDEV_S_CROP: {
606 		struct v4l2_subdev_crop *crop = arg;
607 		struct v4l2_subdev_selection sel;
608 
609 		if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
610 			return -EPERM;
611 
612 		memset(crop->reserved, 0, sizeof(crop->reserved));
613 		memset(&sel, 0, sizeof(sel));
614 		sel.which = crop->which;
615 		sel.pad = crop->pad;
616 		sel.target = V4L2_SEL_TGT_CROP;
617 		sel.r = crop->rect;
618 
619 		rval = v4l2_subdev_call(
620 			sd, pad, set_selection, state, &sel);
621 
622 		crop->rect = sel.r;
623 
624 		return rval;
625 	}
626 
627 	case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
628 		struct v4l2_subdev_mbus_code_enum *code = arg;
629 
630 		memset(code->reserved, 0, sizeof(code->reserved));
631 		return v4l2_subdev_call(sd, pad, enum_mbus_code, state,
632 					code);
633 	}
634 
635 	case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
636 		struct v4l2_subdev_frame_size_enum *fse = arg;
637 
638 		memset(fse->reserved, 0, sizeof(fse->reserved));
639 		return v4l2_subdev_call(sd, pad, enum_frame_size, state,
640 					fse);
641 	}
642 
643 	case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
644 		struct v4l2_subdev_frame_interval *fi = arg;
645 
646 		memset(fi->reserved, 0, sizeof(fi->reserved));
647 		return v4l2_subdev_call(sd, video, g_frame_interval, arg);
648 	}
649 
650 	case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
651 		struct v4l2_subdev_frame_interval *fi = arg;
652 
653 		if (ro_subdev)
654 			return -EPERM;
655 
656 		memset(fi->reserved, 0, sizeof(fi->reserved));
657 		return v4l2_subdev_call(sd, video, s_frame_interval, arg);
658 	}
659 
660 	case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
661 		struct v4l2_subdev_frame_interval_enum *fie = arg;
662 
663 		memset(fie->reserved, 0, sizeof(fie->reserved));
664 		return v4l2_subdev_call(sd, pad, enum_frame_interval, state,
665 					fie);
666 	}
667 
668 	case VIDIOC_SUBDEV_G_SELECTION: {
669 		struct v4l2_subdev_selection *sel = arg;
670 
671 		memset(sel->reserved, 0, sizeof(sel->reserved));
672 		return v4l2_subdev_call(
673 			sd, pad, get_selection, state, sel);
674 	}
675 
676 	case VIDIOC_SUBDEV_S_SELECTION: {
677 		struct v4l2_subdev_selection *sel = arg;
678 
679 		if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
680 			return -EPERM;
681 
682 		memset(sel->reserved, 0, sizeof(sel->reserved));
683 		return v4l2_subdev_call(
684 			sd, pad, set_selection, state, sel);
685 	}
686 
687 	case VIDIOC_G_EDID: {
688 		struct v4l2_subdev_edid *edid = arg;
689 
690 		return v4l2_subdev_call(sd, pad, get_edid, edid);
691 	}
692 
693 	case VIDIOC_S_EDID: {
694 		struct v4l2_subdev_edid *edid = arg;
695 
696 		return v4l2_subdev_call(sd, pad, set_edid, edid);
697 	}
698 
699 	case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
700 		struct v4l2_dv_timings_cap *cap = arg;
701 
702 		return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
703 	}
704 
705 	case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
706 		struct v4l2_enum_dv_timings *dvt = arg;
707 
708 		return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
709 	}
710 
711 	case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
712 		return v4l2_subdev_call(sd, video, query_dv_timings, arg);
713 
714 	case VIDIOC_SUBDEV_G_DV_TIMINGS:
715 		return v4l2_subdev_call(sd, video, g_dv_timings, arg);
716 
717 	case VIDIOC_SUBDEV_S_DV_TIMINGS:
718 		if (ro_subdev)
719 			return -EPERM;
720 
721 		return v4l2_subdev_call(sd, video, s_dv_timings, arg);
722 
723 	case VIDIOC_SUBDEV_G_STD:
724 		return v4l2_subdev_call(sd, video, g_std, arg);
725 
726 	case VIDIOC_SUBDEV_S_STD: {
727 		v4l2_std_id *std = arg;
728 
729 		if (ro_subdev)
730 			return -EPERM;
731 
732 		return v4l2_subdev_call(sd, video, s_std, *std);
733 	}
734 
735 	case VIDIOC_SUBDEV_ENUMSTD: {
736 		struct v4l2_standard *p = arg;
737 		v4l2_std_id id;
738 
739 		if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
740 			return -EINVAL;
741 
742 		return v4l_video_std_enumstd(p, id);
743 	}
744 
745 	case VIDIOC_SUBDEV_QUERYSTD:
746 		return v4l2_subdev_call(sd, video, querystd, arg);
747 
748 	default:
749 		return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
750 	}
751 
752 	return 0;
753 }
754 
755 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
756 {
757 	struct video_device *vdev = video_devdata(file);
758 	struct mutex *lock = vdev->lock;
759 	long ret = -ENODEV;
760 
761 	if (lock && mutex_lock_interruptible(lock))
762 		return -ERESTARTSYS;
763 
764 	if (video_is_registered(vdev)) {
765 		struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
766 		struct v4l2_fh *vfh = file->private_data;
767 		struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
768 		struct v4l2_subdev_state *state;
769 
770 		state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg);
771 
772 		if (state)
773 			v4l2_subdev_lock_state(state);
774 
775 		ret = subdev_do_ioctl(file, cmd, arg, state);
776 
777 		if (state)
778 			v4l2_subdev_unlock_state(state);
779 	}
780 
781 	if (lock)
782 		mutex_unlock(lock);
783 	return ret;
784 }
785 
786 static long subdev_ioctl(struct file *file, unsigned int cmd,
787 	unsigned long arg)
788 {
789 	return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
790 }
791 
792 #ifdef CONFIG_COMPAT
793 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
794 	unsigned long arg)
795 {
796 	struct video_device *vdev = video_devdata(file);
797 	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
798 
799 	return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
800 }
801 #endif
802 
803 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
804 static long subdev_ioctl(struct file *file, unsigned int cmd,
805 			 unsigned long arg)
806 {
807 	return -ENODEV;
808 }
809 
810 #ifdef CONFIG_COMPAT
811 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
812 				  unsigned long arg)
813 {
814 	return -ENODEV;
815 }
816 #endif
817 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
818 
819 static __poll_t subdev_poll(struct file *file, poll_table *wait)
820 {
821 	struct video_device *vdev = video_devdata(file);
822 	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
823 	struct v4l2_fh *fh = file->private_data;
824 
825 	if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
826 		return EPOLLERR;
827 
828 	poll_wait(file, &fh->wait, wait);
829 
830 	if (v4l2_event_pending(fh))
831 		return EPOLLPRI;
832 
833 	return 0;
834 }
835 
836 const struct v4l2_file_operations v4l2_subdev_fops = {
837 	.owner = THIS_MODULE,
838 	.open = subdev_open,
839 	.unlocked_ioctl = subdev_ioctl,
840 #ifdef CONFIG_COMPAT
841 	.compat_ioctl32 = subdev_compat_ioctl32,
842 #endif
843 	.release = subdev_close,
844 	.poll = subdev_poll,
845 };
846 
847 #ifdef CONFIG_MEDIA_CONTROLLER
848 
849 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
850 				      struct fwnode_endpoint *endpoint)
851 {
852 	struct fwnode_handle *fwnode;
853 	struct v4l2_subdev *sd;
854 
855 	if (!is_media_entity_v4l2_subdev(entity))
856 		return -EINVAL;
857 
858 	sd = media_entity_to_v4l2_subdev(entity);
859 
860 	fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
861 	fwnode_handle_put(fwnode);
862 
863 	if (device_match_fwnode(sd->dev, fwnode))
864 		return endpoint->port;
865 
866 	return -ENXIO;
867 }
868 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
869 
870 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
871 				      struct media_link *link,
872 				      struct v4l2_subdev_format *source_fmt,
873 				      struct v4l2_subdev_format *sink_fmt)
874 {
875 	bool pass = true;
876 
877 	/* The width, height and code must match. */
878 	if (source_fmt->format.width != sink_fmt->format.width) {
879 		dev_dbg(sd->entity.graph_obj.mdev->dev,
880 			"%s: width does not match (source %u, sink %u)\n",
881 			__func__,
882 			source_fmt->format.width, sink_fmt->format.width);
883 		pass = false;
884 	}
885 
886 	if (source_fmt->format.height != sink_fmt->format.height) {
887 		dev_dbg(sd->entity.graph_obj.mdev->dev,
888 			"%s: height does not match (source %u, sink %u)\n",
889 			__func__,
890 			source_fmt->format.height, sink_fmt->format.height);
891 		pass = false;
892 	}
893 
894 	if (source_fmt->format.code != sink_fmt->format.code) {
895 		dev_dbg(sd->entity.graph_obj.mdev->dev,
896 			"%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
897 			__func__,
898 			source_fmt->format.code, sink_fmt->format.code);
899 		pass = false;
900 	}
901 
902 	/* The field order must match, or the sink field order must be NONE
903 	 * to support interlaced hardware connected to bridges that support
904 	 * progressive formats only.
905 	 */
906 	if (source_fmt->format.field != sink_fmt->format.field &&
907 	    sink_fmt->format.field != V4L2_FIELD_NONE) {
908 		dev_dbg(sd->entity.graph_obj.mdev->dev,
909 			"%s: field does not match (source %u, sink %u)\n",
910 			__func__,
911 			source_fmt->format.field, sink_fmt->format.field);
912 		pass = false;
913 	}
914 
915 	if (pass)
916 		return 0;
917 
918 	dev_dbg(sd->entity.graph_obj.mdev->dev,
919 		"%s: link was \"%s\":%u -> \"%s\":%u\n", __func__,
920 		link->source->entity->name, link->source->index,
921 		link->sink->entity->name, link->sink->index);
922 
923 	return -EPIPE;
924 }
925 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
926 
927 static int
928 v4l2_subdev_link_validate_get_format(struct media_pad *pad,
929 				     struct v4l2_subdev_format *fmt)
930 {
931 	if (is_media_entity_v4l2_subdev(pad->entity)) {
932 		struct v4l2_subdev *sd =
933 			media_entity_to_v4l2_subdev(pad->entity);
934 
935 		fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
936 		fmt->pad = pad->index;
937 		return v4l2_subdev_call_state_active(sd, pad, get_fmt, fmt);
938 	}
939 
940 	WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
941 	     "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
942 	     pad->entity->function, pad->entity->name);
943 
944 	return -EINVAL;
945 }
946 
947 int v4l2_subdev_link_validate(struct media_link *link)
948 {
949 	struct v4l2_subdev *sink;
950 	struct v4l2_subdev_format sink_fmt, source_fmt;
951 	int rval;
952 
953 	rval = v4l2_subdev_link_validate_get_format(
954 		link->source, &source_fmt);
955 	if (rval < 0)
956 		return 0;
957 
958 	rval = v4l2_subdev_link_validate_get_format(
959 		link->sink, &sink_fmt);
960 	if (rval < 0)
961 		return 0;
962 
963 	sink = media_entity_to_v4l2_subdev(link->sink->entity);
964 
965 	rval = v4l2_subdev_call(sink, pad, link_validate, link,
966 				&source_fmt, &sink_fmt);
967 	if (rval != -ENOIOCTLCMD)
968 		return rval;
969 
970 	return v4l2_subdev_link_validate_default(
971 		sink, link, &source_fmt, &sink_fmt);
972 }
973 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
974 
975 struct v4l2_subdev_state *
976 __v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name,
977 			  struct lock_class_key *lock_key)
978 {
979 	struct v4l2_subdev_state *state;
980 	int ret;
981 
982 	state = kzalloc(sizeof(*state), GFP_KERNEL);
983 	if (!state)
984 		return ERR_PTR(-ENOMEM);
985 
986 	__mutex_init(&state->_lock, lock_name, lock_key);
987 	if (sd->state_lock)
988 		state->lock = sd->state_lock;
989 	else
990 		state->lock = &state->_lock;
991 
992 	if (sd->entity.num_pads) {
993 		state->pads = kvcalloc(sd->entity.num_pads,
994 				       sizeof(*state->pads), GFP_KERNEL);
995 		if (!state->pads) {
996 			ret = -ENOMEM;
997 			goto err;
998 		}
999 	}
1000 
1001 	/*
1002 	 * There can be no race at this point, but we lock the state anyway to
1003 	 * satisfy lockdep checks.
1004 	 */
1005 	v4l2_subdev_lock_state(state);
1006 	ret = v4l2_subdev_call(sd, pad, init_cfg, state);
1007 	v4l2_subdev_unlock_state(state);
1008 
1009 	if (ret < 0 && ret != -ENOIOCTLCMD)
1010 		goto err;
1011 
1012 	return state;
1013 
1014 err:
1015 	if (state && state->pads)
1016 		kvfree(state->pads);
1017 
1018 	kfree(state);
1019 
1020 	return ERR_PTR(ret);
1021 }
1022 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc);
1023 
1024 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state)
1025 {
1026 	if (!state)
1027 		return;
1028 
1029 	mutex_destroy(&state->_lock);
1030 
1031 	kvfree(state->pads);
1032 	kfree(state);
1033 }
1034 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free);
1035 
1036 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
1037 				struct lock_class_key *key)
1038 {
1039 	struct v4l2_subdev_state *state;
1040 
1041 	state = __v4l2_subdev_state_alloc(sd, name, key);
1042 	if (IS_ERR(state))
1043 		return PTR_ERR(state);
1044 
1045 	sd->active_state = state;
1046 
1047 	return 0;
1048 }
1049 EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize);
1050 
1051 void v4l2_subdev_cleanup(struct v4l2_subdev *sd)
1052 {
1053 	__v4l2_subdev_state_free(sd->active_state);
1054 	sd->active_state = NULL;
1055 }
1056 EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup);
1057 
1058 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1059 
1060 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
1061 			struct v4l2_subdev_format *format)
1062 {
1063 	struct v4l2_mbus_framefmt *fmt;
1064 
1065 	if (format->pad >= sd->entity.num_pads)
1066 		return -EINVAL;
1067 
1068 	fmt = v4l2_subdev_get_pad_format(sd, state, format->pad);
1069 	if (!fmt)
1070 		return -EINVAL;
1071 
1072 	format->format = *fmt;
1073 
1074 	return 0;
1075 }
1076 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt);
1077 
1078 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1079 
1080 #endif /* CONFIG_MEDIA_CONTROLLER */
1081 
1082 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
1083 {
1084 	INIT_LIST_HEAD(&sd->list);
1085 	BUG_ON(!ops);
1086 	sd->ops = ops;
1087 	sd->v4l2_dev = NULL;
1088 	sd->flags = 0;
1089 	sd->name[0] = '\0';
1090 	sd->grp_id = 0;
1091 	sd->dev_priv = NULL;
1092 	sd->host_priv = NULL;
1093 #if defined(CONFIG_MEDIA_CONTROLLER)
1094 	sd->entity.name = sd->name;
1095 	sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
1096 	sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
1097 #endif
1098 }
1099 EXPORT_SYMBOL(v4l2_subdev_init);
1100 
1101 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
1102 			      const struct v4l2_event *ev)
1103 {
1104 	v4l2_event_queue(sd->devnode, ev);
1105 	v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
1106 }
1107 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);
1108