]> Git Repo - linux.git/blob - drivers/media/v4l2-core/v4l2-subdev.c
platform/x86: amd-pmc: Move to later in the suspend process
[linux.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 <[email protected]>
8  *          Sakari Ailus <[email protected]>
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
31         state = v4l2_subdev_alloc_state(sd);
32         if (IS_ERR(state))
33                 return PTR_ERR(state);
34
35         fh->state = state;
36
37         return 0;
38 }
39
40 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
41 {
42         v4l2_subdev_free_state(fh->state);
43         fh->state = NULL;
44 }
45
46 static int subdev_open(struct file *file)
47 {
48         struct video_device *vdev = video_devdata(file);
49         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
50         struct v4l2_subdev_fh *subdev_fh;
51         int ret;
52
53         subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
54         if (subdev_fh == NULL)
55                 return -ENOMEM;
56
57         ret = subdev_fh_init(subdev_fh, sd);
58         if (ret) {
59                 kfree(subdev_fh);
60                 return ret;
61         }
62
63         v4l2_fh_init(&subdev_fh->vfh, vdev);
64         v4l2_fh_add(&subdev_fh->vfh);
65         file->private_data = &subdev_fh->vfh;
66 #if defined(CONFIG_MEDIA_CONTROLLER)
67         if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
68                 struct module *owner;
69
70                 owner = sd->entity.graph_obj.mdev->dev->driver->owner;
71                 if (!try_module_get(owner)) {
72                         ret = -EBUSY;
73                         goto err;
74                 }
75                 subdev_fh->owner = owner;
76         }
77 #endif
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_set_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
322                                 struct v4l2_mbus_config *config)
323 {
324         return check_pad(sd, pad) ? :
325                sd->ops->pad->get_mbus_config(sd, pad, config);
326 }
327
328 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = {
329         .get_fmt                = call_get_fmt,
330         .set_fmt                = call_set_fmt,
331         .enum_mbus_code         = call_enum_mbus_code,
332         .enum_frame_size        = call_enum_frame_size,
333         .enum_frame_interval    = call_enum_frame_interval,
334         .get_selection          = call_get_selection,
335         .set_selection          = call_set_selection,
336         .get_edid               = call_get_edid,
337         .set_edid               = call_set_edid,
338         .dv_timings_cap         = call_dv_timings_cap,
339         .enum_dv_timings        = call_enum_dv_timings,
340         .get_mbus_config        = call_get_mbus_config,
341         .set_mbus_config        = call_set_mbus_config,
342 };
343
344 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = {
345         .g_frame_interval       = call_g_frame_interval,
346         .s_frame_interval       = call_s_frame_interval,
347 };
348
349 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = {
350         .pad    = &v4l2_subdev_call_pad_wrappers,
351         .video  = &v4l2_subdev_call_video_wrappers,
352 };
353 EXPORT_SYMBOL(v4l2_subdev_call_wrappers);
354
355 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
356 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
357 {
358         struct video_device *vdev = video_devdata(file);
359         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
360         struct v4l2_fh *vfh = file->private_data;
361         struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
362         bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
363         int rval;
364
365         switch (cmd) {
366         case VIDIOC_SUBDEV_QUERYCAP: {
367                 struct v4l2_subdev_capability *cap = arg;
368
369                 memset(cap->reserved, 0, sizeof(cap->reserved));
370                 cap->version = LINUX_VERSION_CODE;
371                 cap->capabilities = ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0;
372
373                 return 0;
374         }
375
376         case VIDIOC_QUERYCTRL:
377                 /*
378                  * TODO: this really should be folded into v4l2_queryctrl (this
379                  * currently returns -EINVAL for NULL control handlers).
380                  * However, v4l2_queryctrl() is still called directly by
381                  * drivers as well and until that has been addressed I believe
382                  * it is safer to do the check here. The same is true for the
383                  * other control ioctls below.
384                  */
385                 if (!vfh->ctrl_handler)
386                         return -ENOTTY;
387                 return v4l2_queryctrl(vfh->ctrl_handler, arg);
388
389         case VIDIOC_QUERY_EXT_CTRL:
390                 if (!vfh->ctrl_handler)
391                         return -ENOTTY;
392                 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
393
394         case VIDIOC_QUERYMENU:
395                 if (!vfh->ctrl_handler)
396                         return -ENOTTY;
397                 return v4l2_querymenu(vfh->ctrl_handler, arg);
398
399         case VIDIOC_G_CTRL:
400                 if (!vfh->ctrl_handler)
401                         return -ENOTTY;
402                 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
403
404         case VIDIOC_S_CTRL:
405                 if (!vfh->ctrl_handler)
406                         return -ENOTTY;
407                 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
408
409         case VIDIOC_G_EXT_CTRLS:
410                 if (!vfh->ctrl_handler)
411                         return -ENOTTY;
412                 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
413                                         vdev, sd->v4l2_dev->mdev, arg);
414
415         case VIDIOC_S_EXT_CTRLS:
416                 if (!vfh->ctrl_handler)
417                         return -ENOTTY;
418                 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
419                                         vdev, sd->v4l2_dev->mdev, arg);
420
421         case VIDIOC_TRY_EXT_CTRLS:
422                 if (!vfh->ctrl_handler)
423                         return -ENOTTY;
424                 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
425                                           vdev, sd->v4l2_dev->mdev, arg);
426
427         case VIDIOC_DQEVENT:
428                 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
429                         return -ENOIOCTLCMD;
430
431                 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
432
433         case VIDIOC_SUBSCRIBE_EVENT:
434                 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
435
436         case VIDIOC_UNSUBSCRIBE_EVENT:
437                 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
438
439 #ifdef CONFIG_VIDEO_ADV_DEBUG
440         case VIDIOC_DBG_G_REGISTER:
441         {
442                 struct v4l2_dbg_register *p = arg;
443
444                 if (!capable(CAP_SYS_ADMIN))
445                         return -EPERM;
446                 return v4l2_subdev_call(sd, core, g_register, p);
447         }
448         case VIDIOC_DBG_S_REGISTER:
449         {
450                 struct v4l2_dbg_register *p = arg;
451
452                 if (!capable(CAP_SYS_ADMIN))
453                         return -EPERM;
454                 return v4l2_subdev_call(sd, core, s_register, p);
455         }
456         case VIDIOC_DBG_G_CHIP_INFO:
457         {
458                 struct v4l2_dbg_chip_info *p = arg;
459
460                 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
461                         return -EINVAL;
462                 if (sd->ops->core && sd->ops->core->s_register)
463                         p->flags |= V4L2_CHIP_FL_WRITABLE;
464                 if (sd->ops->core && sd->ops->core->g_register)
465                         p->flags |= V4L2_CHIP_FL_READABLE;
466                 strscpy(p->name, sd->name, sizeof(p->name));
467                 return 0;
468         }
469 #endif
470
471         case VIDIOC_LOG_STATUS: {
472                 int ret;
473
474                 pr_info("%s: =================  START STATUS  =================\n",
475                         sd->name);
476                 ret = v4l2_subdev_call(sd, core, log_status);
477                 pr_info("%s: ==================  END STATUS  ==================\n",
478                         sd->name);
479                 return ret;
480         }
481
482         case VIDIOC_SUBDEV_G_FMT: {
483                 struct v4l2_subdev_format *format = arg;
484
485                 memset(format->reserved, 0, sizeof(format->reserved));
486                 memset(format->format.reserved, 0, sizeof(format->format.reserved));
487                 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->state, format);
488         }
489
490         case VIDIOC_SUBDEV_S_FMT: {
491                 struct v4l2_subdev_format *format = arg;
492
493                 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
494                         return -EPERM;
495
496                 memset(format->reserved, 0, sizeof(format->reserved));
497                 memset(format->format.reserved, 0, sizeof(format->format.reserved));
498                 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->state, format);
499         }
500
501         case VIDIOC_SUBDEV_G_CROP: {
502                 struct v4l2_subdev_crop *crop = arg;
503                 struct v4l2_subdev_selection sel;
504
505                 memset(crop->reserved, 0, sizeof(crop->reserved));
506                 memset(&sel, 0, sizeof(sel));
507                 sel.which = crop->which;
508                 sel.pad = crop->pad;
509                 sel.target = V4L2_SEL_TGT_CROP;
510
511                 rval = v4l2_subdev_call(
512                         sd, pad, get_selection, subdev_fh->state, &sel);
513
514                 crop->rect = sel.r;
515
516                 return rval;
517         }
518
519         case VIDIOC_SUBDEV_S_CROP: {
520                 struct v4l2_subdev_crop *crop = arg;
521                 struct v4l2_subdev_selection sel;
522
523                 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
524                         return -EPERM;
525
526                 memset(crop->reserved, 0, sizeof(crop->reserved));
527                 memset(&sel, 0, sizeof(sel));
528                 sel.which = crop->which;
529                 sel.pad = crop->pad;
530                 sel.target = V4L2_SEL_TGT_CROP;
531                 sel.r = crop->rect;
532
533                 rval = v4l2_subdev_call(
534                         sd, pad, set_selection, subdev_fh->state, &sel);
535
536                 crop->rect = sel.r;
537
538                 return rval;
539         }
540
541         case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
542                 struct v4l2_subdev_mbus_code_enum *code = arg;
543
544                 memset(code->reserved, 0, sizeof(code->reserved));
545                 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->state,
546                                         code);
547         }
548
549         case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
550                 struct v4l2_subdev_frame_size_enum *fse = arg;
551
552                 memset(fse->reserved, 0, sizeof(fse->reserved));
553                 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->state,
554                                         fse);
555         }
556
557         case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
558                 struct v4l2_subdev_frame_interval *fi = arg;
559
560                 memset(fi->reserved, 0, sizeof(fi->reserved));
561                 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
562         }
563
564         case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
565                 struct v4l2_subdev_frame_interval *fi = arg;
566
567                 if (ro_subdev)
568                         return -EPERM;
569
570                 memset(fi->reserved, 0, sizeof(fi->reserved));
571                 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
572         }
573
574         case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
575                 struct v4l2_subdev_frame_interval_enum *fie = arg;
576
577                 memset(fie->reserved, 0, sizeof(fie->reserved));
578                 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->state,
579                                         fie);
580         }
581
582         case VIDIOC_SUBDEV_G_SELECTION: {
583                 struct v4l2_subdev_selection *sel = arg;
584
585                 memset(sel->reserved, 0, sizeof(sel->reserved));
586                 return v4l2_subdev_call(
587                         sd, pad, get_selection, subdev_fh->state, sel);
588         }
589
590         case VIDIOC_SUBDEV_S_SELECTION: {
591                 struct v4l2_subdev_selection *sel = arg;
592
593                 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
594                         return -EPERM;
595
596                 memset(sel->reserved, 0, sizeof(sel->reserved));
597                 return v4l2_subdev_call(
598                         sd, pad, set_selection, subdev_fh->state, sel);
599         }
600
601         case VIDIOC_G_EDID: {
602                 struct v4l2_subdev_edid *edid = arg;
603
604                 return v4l2_subdev_call(sd, pad, get_edid, edid);
605         }
606
607         case VIDIOC_S_EDID: {
608                 struct v4l2_subdev_edid *edid = arg;
609
610                 return v4l2_subdev_call(sd, pad, set_edid, edid);
611         }
612
613         case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
614                 struct v4l2_dv_timings_cap *cap = arg;
615
616                 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
617         }
618
619         case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
620                 struct v4l2_enum_dv_timings *dvt = arg;
621
622                 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
623         }
624
625         case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
626                 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
627
628         case VIDIOC_SUBDEV_G_DV_TIMINGS:
629                 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
630
631         case VIDIOC_SUBDEV_S_DV_TIMINGS:
632                 if (ro_subdev)
633                         return -EPERM;
634
635                 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
636
637         case VIDIOC_SUBDEV_G_STD:
638                 return v4l2_subdev_call(sd, video, g_std, arg);
639
640         case VIDIOC_SUBDEV_S_STD: {
641                 v4l2_std_id *std = arg;
642
643                 if (ro_subdev)
644                         return -EPERM;
645
646                 return v4l2_subdev_call(sd, video, s_std, *std);
647         }
648
649         case VIDIOC_SUBDEV_ENUMSTD: {
650                 struct v4l2_standard *p = arg;
651                 v4l2_std_id id;
652
653                 if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
654                         return -EINVAL;
655
656                 return v4l_video_std_enumstd(p, id);
657         }
658
659         case VIDIOC_SUBDEV_QUERYSTD:
660                 return v4l2_subdev_call(sd, video, querystd, arg);
661
662         default:
663                 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
664         }
665
666         return 0;
667 }
668
669 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
670 {
671         struct video_device *vdev = video_devdata(file);
672         struct mutex *lock = vdev->lock;
673         long ret = -ENODEV;
674
675         if (lock && mutex_lock_interruptible(lock))
676                 return -ERESTARTSYS;
677         if (video_is_registered(vdev))
678                 ret = subdev_do_ioctl(file, cmd, arg);
679         if (lock)
680                 mutex_unlock(lock);
681         return ret;
682 }
683
684 static long subdev_ioctl(struct file *file, unsigned int cmd,
685         unsigned long arg)
686 {
687         return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
688 }
689
690 #ifdef CONFIG_COMPAT
691 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
692         unsigned long arg)
693 {
694         struct video_device *vdev = video_devdata(file);
695         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
696
697         return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
698 }
699 #endif
700
701 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
702 static long subdev_ioctl(struct file *file, unsigned int cmd,
703                          unsigned long arg)
704 {
705         return -ENODEV;
706 }
707
708 #ifdef CONFIG_COMPAT
709 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
710                                   unsigned long arg)
711 {
712         return -ENODEV;
713 }
714 #endif
715 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
716
717 static __poll_t subdev_poll(struct file *file, poll_table *wait)
718 {
719         struct video_device *vdev = video_devdata(file);
720         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
721         struct v4l2_fh *fh = file->private_data;
722
723         if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
724                 return EPOLLERR;
725
726         poll_wait(file, &fh->wait, wait);
727
728         if (v4l2_event_pending(fh))
729                 return EPOLLPRI;
730
731         return 0;
732 }
733
734 const struct v4l2_file_operations v4l2_subdev_fops = {
735         .owner = THIS_MODULE,
736         .open = subdev_open,
737         .unlocked_ioctl = subdev_ioctl,
738 #ifdef CONFIG_COMPAT
739         .compat_ioctl32 = subdev_compat_ioctl32,
740 #endif
741         .release = subdev_close,
742         .poll = subdev_poll,
743 };
744
745 #ifdef CONFIG_MEDIA_CONTROLLER
746
747 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
748                                       struct fwnode_endpoint *endpoint)
749 {
750         struct fwnode_handle *fwnode;
751         struct v4l2_subdev *sd;
752
753         if (!is_media_entity_v4l2_subdev(entity))
754                 return -EINVAL;
755
756         sd = media_entity_to_v4l2_subdev(entity);
757
758         fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
759         fwnode_handle_put(fwnode);
760
761         if (dev_fwnode(sd->dev) == fwnode)
762                 return endpoint->port;
763
764         return -ENXIO;
765 }
766 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
767
768 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
769                                       struct media_link *link,
770                                       struct v4l2_subdev_format *source_fmt,
771                                       struct v4l2_subdev_format *sink_fmt)
772 {
773         bool pass = true;
774
775         /* The width, height and code must match. */
776         if (source_fmt->format.width != sink_fmt->format.width) {
777                 dev_dbg(sd->entity.graph_obj.mdev->dev,
778                         "%s: width does not match (source %u, sink %u)\n",
779                         __func__,
780                         source_fmt->format.width, sink_fmt->format.width);
781                 pass = false;
782         }
783
784         if (source_fmt->format.height != sink_fmt->format.height) {
785                 dev_dbg(sd->entity.graph_obj.mdev->dev,
786                         "%s: height does not match (source %u, sink %u)\n",
787                         __func__,
788                         source_fmt->format.height, sink_fmt->format.height);
789                 pass = false;
790         }
791
792         if (source_fmt->format.code != sink_fmt->format.code) {
793                 dev_dbg(sd->entity.graph_obj.mdev->dev,
794                         "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
795                         __func__,
796                         source_fmt->format.code, sink_fmt->format.code);
797                 pass = false;
798         }
799
800         /* The field order must match, or the sink field order must be NONE
801          * to support interlaced hardware connected to bridges that support
802          * progressive formats only.
803          */
804         if (source_fmt->format.field != sink_fmt->format.field &&
805             sink_fmt->format.field != V4L2_FIELD_NONE) {
806                 dev_dbg(sd->entity.graph_obj.mdev->dev,
807                         "%s: field does not match (source %u, sink %u)\n",
808                         __func__,
809                         source_fmt->format.field, sink_fmt->format.field);
810                 pass = false;
811         }
812
813         if (pass)
814                 return 0;
815
816         dev_dbg(sd->entity.graph_obj.mdev->dev,
817                 "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__,
818                 link->source->entity->name, link->source->index,
819                 link->sink->entity->name, link->sink->index);
820
821         return -EPIPE;
822 }
823 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
824
825 static int
826 v4l2_subdev_link_validate_get_format(struct media_pad *pad,
827                                      struct v4l2_subdev_format *fmt)
828 {
829         if (is_media_entity_v4l2_subdev(pad->entity)) {
830                 struct v4l2_subdev *sd =
831                         media_entity_to_v4l2_subdev(pad->entity);
832
833                 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
834                 fmt->pad = pad->index;
835                 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
836         }
837
838         WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
839              "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
840              pad->entity->function, pad->entity->name);
841
842         return -EINVAL;
843 }
844
845 int v4l2_subdev_link_validate(struct media_link *link)
846 {
847         struct v4l2_subdev *sink;
848         struct v4l2_subdev_format sink_fmt, source_fmt;
849         int rval;
850
851         rval = v4l2_subdev_link_validate_get_format(
852                 link->source, &source_fmt);
853         if (rval < 0)
854                 return 0;
855
856         rval = v4l2_subdev_link_validate_get_format(
857                 link->sink, &sink_fmt);
858         if (rval < 0)
859                 return 0;
860
861         sink = media_entity_to_v4l2_subdev(link->sink->entity);
862
863         rval = v4l2_subdev_call(sink, pad, link_validate, link,
864                                 &source_fmt, &sink_fmt);
865         if (rval != -ENOIOCTLCMD)
866                 return rval;
867
868         return v4l2_subdev_link_validate_default(
869                 sink, link, &source_fmt, &sink_fmt);
870 }
871 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
872
873 struct v4l2_subdev_state *v4l2_subdev_alloc_state(struct v4l2_subdev *sd)
874 {
875         struct v4l2_subdev_state *state;
876         int ret;
877
878         state = kzalloc(sizeof(*state), GFP_KERNEL);
879         if (!state)
880                 return ERR_PTR(-ENOMEM);
881
882         if (sd->entity.num_pads) {
883                 state->pads = kvmalloc_array(sd->entity.num_pads,
884                                              sizeof(*state->pads),
885                                              GFP_KERNEL | __GFP_ZERO);
886                 if (!state->pads) {
887                         ret = -ENOMEM;
888                         goto err;
889                 }
890         }
891
892         ret = v4l2_subdev_call(sd, pad, init_cfg, state);
893         if (ret < 0 && ret != -ENOIOCTLCMD)
894                 goto err;
895
896         return state;
897
898 err:
899         if (state && state->pads)
900                 kvfree(state->pads);
901
902         kfree(state);
903
904         return ERR_PTR(ret);
905 }
906 EXPORT_SYMBOL_GPL(v4l2_subdev_alloc_state);
907
908 void v4l2_subdev_free_state(struct v4l2_subdev_state *state)
909 {
910         if (!state)
911                 return;
912
913         kvfree(state->pads);
914         kfree(state);
915 }
916 EXPORT_SYMBOL_GPL(v4l2_subdev_free_state);
917
918 #endif /* CONFIG_MEDIA_CONTROLLER */
919
920 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
921 {
922         INIT_LIST_HEAD(&sd->list);
923         BUG_ON(!ops);
924         sd->ops = ops;
925         sd->v4l2_dev = NULL;
926         sd->flags = 0;
927         sd->name[0] = '\0';
928         sd->grp_id = 0;
929         sd->dev_priv = NULL;
930         sd->host_priv = NULL;
931 #if defined(CONFIG_MEDIA_CONTROLLER)
932         sd->entity.name = sd->name;
933         sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
934         sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
935 #endif
936 }
937 EXPORT_SYMBOL(v4l2_subdev_init);
938
939 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
940                               const struct v4l2_event *ev)
941 {
942         v4l2_event_queue(sd->devnode, ev);
943         v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
944 }
945 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);
This page took 0.082475 seconds and 4 git commands to generate.