]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/v4l2-core/v4l2-subdev.c
Merge branch 'clockevents/3.19' of http://git.linaro.org/people/daniel.lezcano/linux...
[karo-tx-linux.git] / drivers / media / v4l2-core / v4l2-subdev.c
1 /*
2  * V4L2 sub-device
3  *
4  * Copyright (C) 2010 Nokia Corporation
5  *
6  * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7  *          Sakari Ailus <sakari.ailus@iki.fi>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <linux/ioctl.h>
24 #include <linux/slab.h>
25 #include <linux/types.h>
26 #include <linux/videodev2.h>
27 #include <linux/export.h>
28
29 #include <media/v4l2-ctrls.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-fh.h>
33 #include <media/v4l2-event.h>
34
35 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
36 {
37 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
38         fh->pad = kzalloc(sizeof(*fh->pad) * sd->entity.num_pads, GFP_KERNEL);
39         if (fh->pad == NULL)
40                 return -ENOMEM;
41 #endif
42         return 0;
43 }
44
45 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
46 {
47 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
48         kfree(fh->pad);
49         fh->pad = NULL;
50 #endif
51 }
52
53 static int subdev_open(struct file *file)
54 {
55         struct video_device *vdev = video_devdata(file);
56         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
57         struct v4l2_subdev_fh *subdev_fh;
58 #if defined(CONFIG_MEDIA_CONTROLLER)
59         struct media_entity *entity = NULL;
60 #endif
61         int ret;
62
63         subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
64         if (subdev_fh == NULL)
65                 return -ENOMEM;
66
67         ret = subdev_fh_init(subdev_fh, sd);
68         if (ret) {
69                 kfree(subdev_fh);
70                 return ret;
71         }
72
73         v4l2_fh_init(&subdev_fh->vfh, vdev);
74         v4l2_fh_add(&subdev_fh->vfh);
75         file->private_data = &subdev_fh->vfh;
76 #if defined(CONFIG_MEDIA_CONTROLLER)
77         if (sd->v4l2_dev->mdev) {
78                 entity = media_entity_get(&sd->entity);
79                 if (!entity) {
80                         ret = -EBUSY;
81                         goto err;
82                 }
83         }
84 #endif
85
86         if (sd->internal_ops && sd->internal_ops->open) {
87                 ret = sd->internal_ops->open(sd, subdev_fh);
88                 if (ret < 0)
89                         goto err;
90         }
91
92         return 0;
93
94 err:
95 #if defined(CONFIG_MEDIA_CONTROLLER)
96         if (entity)
97                 media_entity_put(entity);
98 #endif
99         v4l2_fh_del(&subdev_fh->vfh);
100         v4l2_fh_exit(&subdev_fh->vfh);
101         subdev_fh_free(subdev_fh);
102         kfree(subdev_fh);
103
104         return ret;
105 }
106
107 static int subdev_close(struct file *file)
108 {
109         struct video_device *vdev = video_devdata(file);
110         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
111         struct v4l2_fh *vfh = file->private_data;
112         struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
113
114         if (sd->internal_ops && sd->internal_ops->close)
115                 sd->internal_ops->close(sd, subdev_fh);
116 #if defined(CONFIG_MEDIA_CONTROLLER)
117         if (sd->v4l2_dev->mdev)
118                 media_entity_put(&sd->entity);
119 #endif
120         v4l2_fh_del(vfh);
121         v4l2_fh_exit(vfh);
122         subdev_fh_free(subdev_fh);
123         kfree(subdev_fh);
124         file->private_data = NULL;
125
126         return 0;
127 }
128
129 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
130 static int check_format(struct v4l2_subdev *sd,
131                         struct v4l2_subdev_format *format)
132 {
133         if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
134             format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
135                 return -EINVAL;
136
137         if (format->pad >= sd->entity.num_pads)
138                 return -EINVAL;
139
140         return 0;
141 }
142
143 static int check_crop(struct v4l2_subdev *sd, struct v4l2_subdev_crop *crop)
144 {
145         if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
146             crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
147                 return -EINVAL;
148
149         if (crop->pad >= sd->entity.num_pads)
150                 return -EINVAL;
151
152         return 0;
153 }
154
155 static int check_selection(struct v4l2_subdev *sd,
156                            struct v4l2_subdev_selection *sel)
157 {
158         if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
159             sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
160                 return -EINVAL;
161
162         if (sel->pad >= sd->entity.num_pads)
163                 return -EINVAL;
164
165         return 0;
166 }
167
168 static int check_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
169 {
170         if (edid->pad >= sd->entity.num_pads)
171                 return -EINVAL;
172
173         if (edid->blocks && edid->edid == NULL)
174                 return -EINVAL;
175
176         return 0;
177 }
178 #endif
179
180 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
181 {
182         struct video_device *vdev = video_devdata(file);
183         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
184         struct v4l2_fh *vfh = file->private_data;
185 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
186         struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
187         int rval;
188 #endif
189
190         switch (cmd) {
191         case VIDIOC_QUERYCTRL:
192                 return v4l2_queryctrl(vfh->ctrl_handler, arg);
193
194         case VIDIOC_QUERY_EXT_CTRL:
195                 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
196
197         case VIDIOC_QUERYMENU:
198                 return v4l2_querymenu(vfh->ctrl_handler, arg);
199
200         case VIDIOC_G_CTRL:
201                 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
202
203         case VIDIOC_S_CTRL:
204                 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
205
206         case VIDIOC_G_EXT_CTRLS:
207                 return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg);
208
209         case VIDIOC_S_EXT_CTRLS:
210                 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg);
211
212         case VIDIOC_TRY_EXT_CTRLS:
213                 return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg);
214
215         case VIDIOC_DQEVENT:
216                 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
217                         return -ENOIOCTLCMD;
218
219                 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
220
221         case VIDIOC_SUBSCRIBE_EVENT:
222                 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
223
224         case VIDIOC_UNSUBSCRIBE_EVENT:
225                 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
226
227 #ifdef CONFIG_VIDEO_ADV_DEBUG
228         case VIDIOC_DBG_G_REGISTER:
229         {
230                 struct v4l2_dbg_register *p = arg;
231
232                 if (!capable(CAP_SYS_ADMIN))
233                         return -EPERM;
234                 return v4l2_subdev_call(sd, core, g_register, p);
235         }
236         case VIDIOC_DBG_S_REGISTER:
237         {
238                 struct v4l2_dbg_register *p = arg;
239
240                 if (!capable(CAP_SYS_ADMIN))
241                         return -EPERM;
242                 return v4l2_subdev_call(sd, core, s_register, p);
243         }
244 #endif
245
246         case VIDIOC_LOG_STATUS: {
247                 int ret;
248
249                 pr_info("%s: =================  START STATUS  =================\n",
250                         sd->name);
251                 ret = v4l2_subdev_call(sd, core, log_status);
252                 pr_info("%s: ==================  END STATUS  ==================\n",
253                         sd->name);
254                 return ret;
255         }
256
257 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
258         case VIDIOC_SUBDEV_G_FMT: {
259                 struct v4l2_subdev_format *format = arg;
260
261                 rval = check_format(sd, format);
262                 if (rval)
263                         return rval;
264
265                 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh, format);
266         }
267
268         case VIDIOC_SUBDEV_S_FMT: {
269                 struct v4l2_subdev_format *format = arg;
270
271                 rval = check_format(sd, format);
272                 if (rval)
273                         return rval;
274
275                 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format);
276         }
277
278         case VIDIOC_SUBDEV_G_CROP: {
279                 struct v4l2_subdev_crop *crop = arg;
280                 struct v4l2_subdev_selection sel;
281
282                 rval = check_crop(sd, crop);
283                 if (rval)
284                         return rval;
285
286                 rval = v4l2_subdev_call(sd, pad, get_crop, subdev_fh, crop);
287                 if (rval != -ENOIOCTLCMD)
288                         return rval;
289
290                 memset(&sel, 0, sizeof(sel));
291                 sel.which = crop->which;
292                 sel.pad = crop->pad;
293                 sel.target = V4L2_SEL_TGT_CROP;
294
295                 rval = v4l2_subdev_call(
296                         sd, pad, get_selection, subdev_fh, &sel);
297
298                 crop->rect = sel.r;
299
300                 return rval;
301         }
302
303         case VIDIOC_SUBDEV_S_CROP: {
304                 struct v4l2_subdev_crop *crop = arg;
305                 struct v4l2_subdev_selection sel;
306
307                 rval = check_crop(sd, crop);
308                 if (rval)
309                         return rval;
310
311                 rval = v4l2_subdev_call(sd, pad, set_crop, subdev_fh, crop);
312                 if (rval != -ENOIOCTLCMD)
313                         return rval;
314
315                 memset(&sel, 0, sizeof(sel));
316                 sel.which = crop->which;
317                 sel.pad = crop->pad;
318                 sel.target = V4L2_SEL_TGT_CROP;
319                 sel.r = crop->rect;
320
321                 rval = v4l2_subdev_call(
322                         sd, pad, set_selection, subdev_fh, &sel);
323
324                 crop->rect = sel.r;
325
326                 return rval;
327         }
328
329         case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
330                 struct v4l2_subdev_mbus_code_enum *code = arg;
331
332                 if (code->pad >= sd->entity.num_pads)
333                         return -EINVAL;
334
335                 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh,
336                                         code);
337         }
338
339         case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
340                 struct v4l2_subdev_frame_size_enum *fse = arg;
341
342                 if (fse->pad >= sd->entity.num_pads)
343                         return -EINVAL;
344
345                 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh,
346                                         fse);
347         }
348
349         case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
350                 struct v4l2_subdev_frame_interval *fi = arg;
351
352                 if (fi->pad >= sd->entity.num_pads)
353                         return -EINVAL;
354
355                 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
356         }
357
358         case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
359                 struct v4l2_subdev_frame_interval *fi = arg;
360
361                 if (fi->pad >= sd->entity.num_pads)
362                         return -EINVAL;
363
364                 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
365         }
366
367         case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
368                 struct v4l2_subdev_frame_interval_enum *fie = arg;
369
370                 if (fie->pad >= sd->entity.num_pads)
371                         return -EINVAL;
372
373                 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh,
374                                         fie);
375         }
376
377         case VIDIOC_SUBDEV_G_SELECTION: {
378                 struct v4l2_subdev_selection *sel = arg;
379
380                 rval = check_selection(sd, sel);
381                 if (rval)
382                         return rval;
383
384                 return v4l2_subdev_call(
385                         sd, pad, get_selection, subdev_fh, sel);
386         }
387
388         case VIDIOC_SUBDEV_S_SELECTION: {
389                 struct v4l2_subdev_selection *sel = arg;
390
391                 rval = check_selection(sd, sel);
392                 if (rval)
393                         return rval;
394
395                 return v4l2_subdev_call(
396                         sd, pad, set_selection, subdev_fh, sel);
397         }
398
399         case VIDIOC_G_EDID: {
400                 struct v4l2_subdev_edid *edid = arg;
401
402                 rval = check_edid(sd, edid);
403                 if (rval)
404                         return rval;
405
406                 return v4l2_subdev_call(sd, pad, get_edid, edid);
407         }
408
409         case VIDIOC_S_EDID: {
410                 struct v4l2_subdev_edid *edid = arg;
411
412                 rval = check_edid(sd, edid);
413                 if (rval)
414                         return rval;
415
416                 return v4l2_subdev_call(sd, pad, set_edid, edid);
417         }
418
419         case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
420                 struct v4l2_dv_timings_cap *cap = arg;
421
422                 if (cap->pad >= sd->entity.num_pads)
423                         return -EINVAL;
424
425                 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
426         }
427
428         case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
429                 struct v4l2_enum_dv_timings *dvt = arg;
430
431                 if (dvt->pad >= sd->entity.num_pads)
432                         return -EINVAL;
433
434                 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
435         }
436
437         case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
438                 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
439
440         case VIDIOC_SUBDEV_G_DV_TIMINGS:
441                 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
442
443         case VIDIOC_SUBDEV_S_DV_TIMINGS:
444                 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
445 #endif
446         default:
447                 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
448         }
449
450         return 0;
451 }
452
453 static long subdev_ioctl(struct file *file, unsigned int cmd,
454         unsigned long arg)
455 {
456         return video_usercopy(file, cmd, arg, subdev_do_ioctl);
457 }
458
459 #ifdef CONFIG_COMPAT
460 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
461         unsigned long arg)
462 {
463         struct video_device *vdev = video_devdata(file);
464         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
465
466         return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
467 }
468 #endif
469
470 static unsigned int subdev_poll(struct file *file, poll_table *wait)
471 {
472         struct video_device *vdev = video_devdata(file);
473         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
474         struct v4l2_fh *fh = file->private_data;
475
476         if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
477                 return POLLERR;
478
479         poll_wait(file, &fh->wait, wait);
480
481         if (v4l2_event_pending(fh))
482                 return POLLPRI;
483
484         return 0;
485 }
486
487 const struct v4l2_file_operations v4l2_subdev_fops = {
488         .owner = THIS_MODULE,
489         .open = subdev_open,
490         .unlocked_ioctl = subdev_ioctl,
491 #ifdef CONFIG_COMPAT
492         .compat_ioctl32 = subdev_compat_ioctl32,
493 #endif
494         .release = subdev_close,
495         .poll = subdev_poll,
496 };
497
498 #ifdef CONFIG_MEDIA_CONTROLLER
499 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
500                                       struct media_link *link,
501                                       struct v4l2_subdev_format *source_fmt,
502                                       struct v4l2_subdev_format *sink_fmt)
503 {
504         /* The width, height and code must match. */
505         if (source_fmt->format.width != sink_fmt->format.width
506             || source_fmt->format.height != sink_fmt->format.height
507             || source_fmt->format.code != sink_fmt->format.code)
508                 return -EINVAL;
509
510         /* The field order must match, or the sink field order must be NONE
511          * to support interlaced hardware connected to bridges that support
512          * progressive formats only.
513          */
514         if (source_fmt->format.field != sink_fmt->format.field &&
515             sink_fmt->format.field != V4L2_FIELD_NONE)
516                 return -EINVAL;
517
518         return 0;
519 }
520 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
521
522 static int
523 v4l2_subdev_link_validate_get_format(struct media_pad *pad,
524                                      struct v4l2_subdev_format *fmt)
525 {
526         if (media_entity_type(pad->entity) == MEDIA_ENT_T_V4L2_SUBDEV) {
527                 struct v4l2_subdev *sd =
528                         media_entity_to_v4l2_subdev(pad->entity);
529
530                 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
531                 fmt->pad = pad->index;
532                 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
533         }
534
535         WARN(pad->entity->type != MEDIA_ENT_T_DEVNODE_V4L,
536              "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
537              pad->entity->type, pad->entity->name);
538
539         return -EINVAL;
540 }
541
542 int v4l2_subdev_link_validate(struct media_link *link)
543 {
544         struct v4l2_subdev *sink;
545         struct v4l2_subdev_format sink_fmt, source_fmt;
546         int rval;
547
548         rval = v4l2_subdev_link_validate_get_format(
549                 link->source, &source_fmt);
550         if (rval < 0)
551                 return 0;
552
553         rval = v4l2_subdev_link_validate_get_format(
554                 link->sink, &sink_fmt);
555         if (rval < 0)
556                 return 0;
557
558         sink = media_entity_to_v4l2_subdev(link->sink->entity);
559
560         rval = v4l2_subdev_call(sink, pad, link_validate, link,
561                                 &source_fmt, &sink_fmt);
562         if (rval != -ENOIOCTLCMD)
563                 return rval;
564
565         return v4l2_subdev_link_validate_default(
566                 sink, link, &source_fmt, &sink_fmt);
567 }
568 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
569 #endif /* CONFIG_MEDIA_CONTROLLER */
570
571 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
572 {
573         INIT_LIST_HEAD(&sd->list);
574         BUG_ON(!ops);
575         sd->ops = ops;
576         sd->v4l2_dev = NULL;
577         sd->flags = 0;
578         sd->name[0] = '\0';
579         sd->grp_id = 0;
580         sd->dev_priv = NULL;
581         sd->host_priv = NULL;
582 #if defined(CONFIG_MEDIA_CONTROLLER)
583         sd->entity.name = sd->name;
584         sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
585 #endif
586 }
587 EXPORT_SYMBOL(v4l2_subdev_init);