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