[media] mn88473: simplify bandwidth registers setting code
[deliverable/linux.git] / drivers / media / v4l2-core / v4l2-subdev.c
CommitLineData
2096a5dc 1/*
3dd5ee08 2 * V4L2 sub-device
2096a5dc 3 *
3dd5ee08 4 * Copyright (C) 2010 Nokia Corporation
2096a5dc 5 *
3dd5ee08
LP
6 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
2096a5dc 8 *
3dd5ee08
LP
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.
2096a5dc 12 *
3dd5ee08
LP
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.
2096a5dc 17 *
3dd5ee08
LP
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
2096a5dc
LP
21 */
22
2096a5dc 23#include <linux/ioctl.h>
02adb1cc
SA
24#include <linux/slab.h>
25#include <linux/types.h>
2096a5dc 26#include <linux/videodev2.h>
35a24636 27#include <linux/export.h>
2096a5dc 28
ea8aa434 29#include <media/v4l2-ctrls.h>
2096a5dc
LP
30#include <media/v4l2-device.h>
31#include <media/v4l2-ioctl.h>
02adb1cc
SA
32#include <media/v4l2-fh.h>
33#include <media/v4l2-event.h>
2096a5dc 34
7cd5a16b
SV
35static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
36{
37#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
ae184cda
SA
38 fh->pad = kzalloc(sizeof(*fh->pad) * sd->entity.num_pads, GFP_KERNEL);
39 if (fh->pad == NULL)
7cd5a16b 40 return -ENOMEM;
7cd5a16b
SV
41#endif
42 return 0;
43}
44
45static void subdev_fh_free(struct v4l2_subdev_fh *fh)
46{
47#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
ae184cda
SA
48 kfree(fh->pad);
49 fh->pad = NULL;
7cd5a16b
SV
50#endif
51}
52
2096a5dc
LP
53static int subdev_open(struct file *file)
54{
02adb1cc
SA
55 struct video_device *vdev = video_devdata(file);
56 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
7cd5a16b 57 struct v4l2_subdev_fh *subdev_fh;
61f5db54 58#if defined(CONFIG_MEDIA_CONTROLLER)
f0beea8f 59 struct media_entity *entity = NULL;
61f5db54 60#endif
02adb1cc
SA
61 int ret;
62
7cd5a16b
SV
63 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
64 if (subdev_fh == NULL)
65 return -ENOMEM;
02adb1cc 66
7cd5a16b
SV
67 ret = subdev_fh_init(subdev_fh, sd);
68 if (ret) {
69 kfree(subdev_fh);
70 return ret;
71 }
72
523f46d6 73 v4l2_fh_init(&subdev_fh->vfh, vdev);
7cd5a16b
SV
74 v4l2_fh_add(&subdev_fh->vfh);
75 file->private_data = &subdev_fh->vfh;
61f5db54
LP
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
7cd5a16b 85
f0beea8f
LP
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
2096a5dc 92 return 0;
02adb1cc
SA
93
94err:
f0beea8f
LP
95#if defined(CONFIG_MEDIA_CONTROLLER)
96 if (entity)
97 media_entity_put(entity);
98#endif
7cd5a16b
SV
99 v4l2_fh_del(&subdev_fh->vfh);
100 v4l2_fh_exit(&subdev_fh->vfh);
101 subdev_fh_free(subdev_fh);
102 kfree(subdev_fh);
02adb1cc
SA
103
104 return ret;
2096a5dc
LP
105}
106
107static int subdev_close(struct file *file)
108{
61f5db54
LP
109 struct video_device *vdev = video_devdata(file);
110 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
02adb1cc 111 struct v4l2_fh *vfh = file->private_data;
7cd5a16b 112 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
02adb1cc 113
f0beea8f
LP
114 if (sd->internal_ops && sd->internal_ops->close)
115 sd->internal_ops->close(sd, subdev_fh);
61f5db54
LP
116#if defined(CONFIG_MEDIA_CONTROLLER)
117 if (sd->v4l2_dev->mdev)
118 media_entity_put(&sd->entity);
119#endif
7cd5a16b
SV
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;
02adb1cc 125
2096a5dc
LP
126 return 0;
127}
128
d352bcc9 129#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
b225e398
SA
130static 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
143static 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
155static 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
168static 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}
d352bcc9 178#endif
b225e398 179
2096a5dc
LP
180static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
181{
ea8aa434
LP
182 struct video_device *vdev = video_devdata(file);
183 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
7cd5a16b 184 struct v4l2_fh *vfh = file->private_data;
333c8b97
LP
185#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
186 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
b225e398 187 int rval;
d352bcc9 188#endif
ea8aa434 189
2096a5dc 190 switch (cmd) {
ea8aa434 191 case VIDIOC_QUERYCTRL:
18d171ba 192 return v4l2_queryctrl(vfh->ctrl_handler, arg);
ea8aa434 193
e6bee368
HV
194 case VIDIOC_QUERY_EXT_CTRL:
195 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
196
ea8aa434 197 case VIDIOC_QUERYMENU:
18d171ba 198 return v4l2_querymenu(vfh->ctrl_handler, arg);
ea8aa434
LP
199
200 case VIDIOC_G_CTRL:
18d171ba 201 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
ea8aa434
LP
202
203 case VIDIOC_S_CTRL:
ab892bac 204 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
ea8aa434
LP
205
206 case VIDIOC_G_EXT_CTRLS:
18d171ba 207 return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg);
ea8aa434
LP
208
209 case VIDIOC_S_EXT_CTRLS:
ab892bac 210 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg);
ea8aa434
LP
211
212 case VIDIOC_TRY_EXT_CTRLS:
18d171ba 213 return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg);
ea8aa434 214
02adb1cc
SA
215 case VIDIOC_DQEVENT:
216 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
217 return -ENOIOCTLCMD;
218
7cd5a16b 219 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
02adb1cc
SA
220
221 case VIDIOC_SUBSCRIBE_EVENT:
7cd5a16b 222 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
02adb1cc
SA
223
224 case VIDIOC_UNSUBSCRIBE_EVENT:
7cd5a16b 225 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
69967a71
MH
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
bcd158de 245
42194e72
HV
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 }
bcd158de 256
333c8b97
LP
257#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
258 case VIDIOC_SUBDEV_G_FMT: {
259 struct v4l2_subdev_format *format = arg;
260
b225e398
SA
261 rval = check_format(sd, format);
262 if (rval)
263 return rval;
333c8b97
LP
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
b225e398
SA
271 rval = check_format(sd, format);
272 if (rval)
273 return rval;
02adb1cc 274
333c8b97
LP
275 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format);
276 }
277
f6a5cb1b
AK
278 case VIDIOC_SUBDEV_G_CROP: {
279 struct v4l2_subdev_crop *crop = arg;
5b9d770f 280 struct v4l2_subdev_selection sel;
f6a5cb1b 281
b225e398
SA
282 rval = check_crop(sd, crop);
283 if (rval)
284 return rval;
f6a5cb1b 285
5b9d770f
SA
286 memset(&sel, 0, sizeof(sel));
287 sel.which = crop->which;
288 sel.pad = crop->pad;
5689b288 289 sel.target = V4L2_SEL_TGT_CROP;
5b9d770f
SA
290
291 rval = v4l2_subdev_call(
292 sd, pad, get_selection, subdev_fh, &sel);
293
294 crop->rect = sel.r;
295
296 return rval;
f6a5cb1b
AK
297 }
298
299 case VIDIOC_SUBDEV_S_CROP: {
300 struct v4l2_subdev_crop *crop = arg;
5b9d770f 301 struct v4l2_subdev_selection sel;
f6a5cb1b 302
b225e398
SA
303 rval = check_crop(sd, crop);
304 if (rval)
305 return rval;
f6a5cb1b 306
5b9d770f
SA
307 memset(&sel, 0, sizeof(sel));
308 sel.which = crop->which;
309 sel.pad = crop->pad;
5689b288 310 sel.target = V4L2_SEL_TGT_CROP;
5b9d770f
SA
311 sel.r = crop->rect;
312
313 rval = v4l2_subdev_call(
314 sd, pad, set_selection, subdev_fh, &sel);
315
316 crop->rect = sel.r;
317
318 return rval;
f6a5cb1b
AK
319 }
320
333c8b97
LP
321 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
322 struct v4l2_subdev_mbus_code_enum *code = arg;
323
324 if (code->pad >= sd->entity.num_pads)
325 return -EINVAL;
326
327 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh,
328 code);
329 }
330
331 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
332 struct v4l2_subdev_frame_size_enum *fse = arg;
333
334 if (fse->pad >= sd->entity.num_pads)
335 return -EINVAL;
336
337 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh,
338 fse);
339 }
35c3017a 340
743e1837
SA
341 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
342 struct v4l2_subdev_frame_interval *fi = arg;
343
344 if (fi->pad >= sd->entity.num_pads)
345 return -EINVAL;
346
35c3017a 347 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
743e1837
SA
348 }
349
350 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
351 struct v4l2_subdev_frame_interval *fi = arg;
352
353 if (fi->pad >= sd->entity.num_pads)
354 return -EINVAL;
35c3017a 355
35c3017a 356 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
743e1837 357 }
35c3017a
LP
358
359 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
360 struct v4l2_subdev_frame_interval_enum *fie = arg;
361
362 if (fie->pad >= sd->entity.num_pads)
363 return -EINVAL;
364
365 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh,
366 fie);
367 }
ae184cda
SA
368
369 case VIDIOC_SUBDEV_G_SELECTION: {
370 struct v4l2_subdev_selection *sel = arg;
371
b225e398
SA
372 rval = check_selection(sd, sel);
373 if (rval)
374 return rval;
ae184cda
SA
375
376 return v4l2_subdev_call(
377 sd, pad, get_selection, subdev_fh, sel);
378 }
379
380 case VIDIOC_SUBDEV_S_SELECTION: {
381 struct v4l2_subdev_selection *sel = arg;
382
b225e398
SA
383 rval = check_selection(sd, sel);
384 if (rval)
385 return rval;
ae184cda
SA
386
387 return v4l2_subdev_call(
388 sd, pad, set_selection, subdev_fh, sel);
389 }
ed45ce2c 390
f2e90847
LP
391 case VIDIOC_G_EDID: {
392 struct v4l2_subdev_edid *edid = arg;
ed45ce2c 393
b225e398
SA
394 rval = check_edid(sd, edid);
395 if (rval)
396 return rval;
f2e90847
LP
397
398 return v4l2_subdev_call(sd, pad, get_edid, edid);
399 }
400
401 case VIDIOC_S_EDID: {
402 struct v4l2_subdev_edid *edid = arg;
403
b225e398
SA
404 rval = check_edid(sd, edid);
405 if (rval)
406 return rval;
f2e90847
LP
407
408 return v4l2_subdev_call(sd, pad, set_edid, edid);
409 }
9cfd65e8
LP
410
411 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
412 struct v4l2_dv_timings_cap *cap = arg;
413
414 if (cap->pad >= sd->entity.num_pads)
415 return -EINVAL;
416
417 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
418 }
419
420 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
421 struct v4l2_enum_dv_timings *dvt = arg;
422
423 if (dvt->pad >= sd->entity.num_pads)
424 return -EINVAL;
425
426 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
427 }
428
429 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
430 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
431
432 case VIDIOC_SUBDEV_G_DV_TIMINGS:
433 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
434
435 case VIDIOC_SUBDEV_S_DV_TIMINGS:
436 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
333c8b97 437#endif
2096a5dc 438 default:
c30b46e5 439 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
2096a5dc
LP
440 }
441
442 return 0;
443}
444
445static long subdev_ioctl(struct file *file, unsigned int cmd,
446 unsigned long arg)
447{
448 return video_usercopy(file, cmd, arg, subdev_do_ioctl);
449}
450
ab58a301
HV
451#ifdef CONFIG_COMPAT
452static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
453 unsigned long arg)
454{
455 struct video_device *vdev = video_devdata(file);
456 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
457
458 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
459}
460#endif
461
02adb1cc
SA
462static unsigned int subdev_poll(struct file *file, poll_table *wait)
463{
464 struct video_device *vdev = video_devdata(file);
465 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
466 struct v4l2_fh *fh = file->private_data;
467
468 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
469 return POLLERR;
470
523f46d6 471 poll_wait(file, &fh->wait, wait);
02adb1cc
SA
472
473 if (v4l2_event_pending(fh))
474 return POLLPRI;
475
476 return 0;
477}
478
2096a5dc
LP
479const struct v4l2_file_operations v4l2_subdev_fops = {
480 .owner = THIS_MODULE,
481 .open = subdev_open,
482 .unlocked_ioctl = subdev_ioctl,
ab58a301
HV
483#ifdef CONFIG_COMPAT
484 .compat_ioctl32 = subdev_compat_ioctl32,
485#endif
2096a5dc 486 .release = subdev_close,
02adb1cc 487 .poll = subdev_poll,
2096a5dc 488};
3dd5ee08 489
8227c92b
SA
490#ifdef CONFIG_MEDIA_CONTROLLER
491int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
492 struct media_link *link,
493 struct v4l2_subdev_format *source_fmt,
494 struct v4l2_subdev_format *sink_fmt)
495{
24acf8b2 496 /* The width, height and code must match. */
8227c92b
SA
497 if (source_fmt->format.width != sink_fmt->format.width
498 || source_fmt->format.height != sink_fmt->format.height
499 || source_fmt->format.code != sink_fmt->format.code)
500 return -EINVAL;
501
24acf8b2
LP
502 /* The field order must match, or the sink field order must be NONE
503 * to support interlaced hardware connected to bridges that support
504 * progressive formats only.
505 */
506 if (source_fmt->format.field != sink_fmt->format.field &&
507 sink_fmt->format.field != V4L2_FIELD_NONE)
508 return -EINVAL;
509
8227c92b
SA
510 return 0;
511}
512EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
513
514static int
515v4l2_subdev_link_validate_get_format(struct media_pad *pad,
516 struct v4l2_subdev_format *fmt)
517{
864a1212
LP
518 if (media_entity_type(pad->entity) == MEDIA_ENT_T_V4L2_SUBDEV) {
519 struct v4l2_subdev *sd =
520 media_entity_to_v4l2_subdev(pad->entity);
521
8227c92b
SA
522 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
523 fmt->pad = pad->index;
864a1212 524 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
8227c92b 525 }
864a1212
LP
526
527 WARN(pad->entity->type != MEDIA_ENT_T_DEVNODE_V4L,
528 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
529 pad->entity->type, pad->entity->name);
530
531 return -EINVAL;
8227c92b
SA
532}
533
534int v4l2_subdev_link_validate(struct media_link *link)
535{
536 struct v4l2_subdev *sink;
537 struct v4l2_subdev_format sink_fmt, source_fmt;
538 int rval;
539
540 rval = v4l2_subdev_link_validate_get_format(
541 link->source, &source_fmt);
542 if (rval < 0)
543 return 0;
544
545 rval = v4l2_subdev_link_validate_get_format(
546 link->sink, &sink_fmt);
547 if (rval < 0)
548 return 0;
549
550 sink = media_entity_to_v4l2_subdev(link->sink->entity);
551
552 rval = v4l2_subdev_call(sink, pad, link_validate, link,
553 &source_fmt, &sink_fmt);
554 if (rval != -ENOIOCTLCMD)
555 return rval;
556
557 return v4l2_subdev_link_validate_default(
558 sink, link, &source_fmt, &sink_fmt);
559}
560EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
561#endif /* CONFIG_MEDIA_CONTROLLER */
562
3dd5ee08
LP
563void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
564{
565 INIT_LIST_HEAD(&sd->list);
566 BUG_ON(!ops);
567 sd->ops = ops;
568 sd->v4l2_dev = NULL;
569 sd->flags = 0;
570 sd->name[0] = '\0';
571 sd->grp_id = 0;
572 sd->dev_priv = NULL;
573 sd->host_priv = NULL;
61f5db54
LP
574#if defined(CONFIG_MEDIA_CONTROLLER)
575 sd->entity.name = sd->name;
576 sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
577#endif
3dd5ee08
LP
578}
579EXPORT_SYMBOL(v4l2_subdev_init);
This page took 0.255899 seconds and 5 git commands to generate.