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