ext3: Update MAINTAINERS for ext3 and JBD
[linux/fpc-iii.git] / drivers / media / video / v4l1-compat.c
blob761fbd64db5850adfeba11a8cc3b32ba0f7f75a4
1 /*
3 * Video for Linux Two
4 * Backward Compatibility Layer
6 * Support subroutines for providing V4L2 drivers with backward
7 * compatibility with applications using the old API.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 * Author: Bill Dirks <bill@thedirks.org>
15 * et al.
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/mm.h>
26 #include <linux/fs.h>
27 #include <linux/file.h>
28 #include <linux/string.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/videodev.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
35 #include <asm/uaccess.h>
36 #include <asm/system.h>
37 #include <asm/pgtable.h>
39 static unsigned int debug;
40 module_param(debug, int, 0644);
41 MODULE_PARM_DESC(debug, "enable debug messages");
42 MODULE_AUTHOR("Bill Dirks");
43 MODULE_DESCRIPTION("v4l(1) compatibility layer for v4l2 drivers.");
44 MODULE_LICENSE("GPL");
46 #define dprintk(fmt, arg...) \
47 do { \
48 if (debug) \
49 printk(KERN_DEBUG "v4l1-compat: " fmt , ## arg);\
50 } while (0)
53 * I O C T L T R A N S L A T I O N
55 * From here on down is the code for translating the numerous
56 * ioctl commands from the old API to the new API.
59 static int
60 get_v4l_control(struct file *file,
61 int cid,
62 v4l2_kioctl drv)
64 struct v4l2_queryctrl qctrl2;
65 struct v4l2_control ctrl2;
66 int err;
68 qctrl2.id = cid;
69 err = drv(file, VIDIOC_QUERYCTRL, &qctrl2);
70 if (err < 0)
71 dprintk("VIDIOC_QUERYCTRL: %d\n", err);
72 if (err == 0 && !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED)) {
73 ctrl2.id = qctrl2.id;
74 err = drv(file, VIDIOC_G_CTRL, &ctrl2);
75 if (err < 0) {
76 dprintk("VIDIOC_G_CTRL: %d\n", err);
77 return 0;
79 return DIV_ROUND_CLOSEST((ctrl2.value-qctrl2.minimum) * 65535,
80 qctrl2.maximum - qctrl2.minimum);
82 return 0;
85 static int
86 set_v4l_control(struct file *file,
87 int cid,
88 int value,
89 v4l2_kioctl drv)
91 struct v4l2_queryctrl qctrl2;
92 struct v4l2_control ctrl2;
93 int err;
95 qctrl2.id = cid;
96 err = drv(file, VIDIOC_QUERYCTRL, &qctrl2);
97 if (err < 0)
98 dprintk("VIDIOC_QUERYCTRL: %d\n", err);
99 if (err == 0 &&
100 !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED) &&
101 !(qctrl2.flags & V4L2_CTRL_FLAG_GRABBED)) {
102 if (value < 0)
103 value = 0;
104 if (value > 65535)
105 value = 65535;
106 if (value && qctrl2.type == V4L2_CTRL_TYPE_BOOLEAN)
107 value = 65535;
108 ctrl2.id = qctrl2.id;
109 ctrl2.value =
110 (value * (qctrl2.maximum - qctrl2.minimum)
111 + 32767)
112 / 65535;
113 ctrl2.value += qctrl2.minimum;
114 err = drv(file, VIDIOC_S_CTRL, &ctrl2);
115 if (err < 0)
116 dprintk("VIDIOC_S_CTRL: %d\n", err);
118 return 0;
121 /* ----------------------------------------------------------------- */
123 static const unsigned int palette2pixelformat[] = {
124 [VIDEO_PALETTE_GREY] = V4L2_PIX_FMT_GREY,
125 [VIDEO_PALETTE_RGB555] = V4L2_PIX_FMT_RGB555,
126 [VIDEO_PALETTE_RGB565] = V4L2_PIX_FMT_RGB565,
127 [VIDEO_PALETTE_RGB24] = V4L2_PIX_FMT_BGR24,
128 [VIDEO_PALETTE_RGB32] = V4L2_PIX_FMT_BGR32,
129 /* yuv packed pixel */
130 [VIDEO_PALETTE_YUYV] = V4L2_PIX_FMT_YUYV,
131 [VIDEO_PALETTE_YUV422] = V4L2_PIX_FMT_YUYV,
132 [VIDEO_PALETTE_UYVY] = V4L2_PIX_FMT_UYVY,
133 /* yuv planar */
134 [VIDEO_PALETTE_YUV410P] = V4L2_PIX_FMT_YUV410,
135 [VIDEO_PALETTE_YUV420] = V4L2_PIX_FMT_YUV420,
136 [VIDEO_PALETTE_YUV420P] = V4L2_PIX_FMT_YUV420,
137 [VIDEO_PALETTE_YUV411P] = V4L2_PIX_FMT_YUV411P,
138 [VIDEO_PALETTE_YUV422P] = V4L2_PIX_FMT_YUV422P,
141 static unsigned int __pure
142 palette_to_pixelformat(unsigned int palette)
144 if (palette < ARRAY_SIZE(palette2pixelformat))
145 return palette2pixelformat[palette];
146 else
147 return 0;
150 static unsigned int __attribute_const__
151 pixelformat_to_palette(unsigned int pixelformat)
153 int palette = 0;
154 switch (pixelformat) {
155 case V4L2_PIX_FMT_GREY:
156 palette = VIDEO_PALETTE_GREY;
157 break;
158 case V4L2_PIX_FMT_RGB555:
159 palette = VIDEO_PALETTE_RGB555;
160 break;
161 case V4L2_PIX_FMT_RGB565:
162 palette = VIDEO_PALETTE_RGB565;
163 break;
164 case V4L2_PIX_FMT_BGR24:
165 palette = VIDEO_PALETTE_RGB24;
166 break;
167 case V4L2_PIX_FMT_BGR32:
168 palette = VIDEO_PALETTE_RGB32;
169 break;
170 /* yuv packed pixel */
171 case V4L2_PIX_FMT_YUYV:
172 palette = VIDEO_PALETTE_YUYV;
173 break;
174 case V4L2_PIX_FMT_UYVY:
175 palette = VIDEO_PALETTE_UYVY;
176 break;
177 /* yuv planar */
178 case V4L2_PIX_FMT_YUV410:
179 palette = VIDEO_PALETTE_YUV420;
180 break;
181 case V4L2_PIX_FMT_YUV420:
182 palette = VIDEO_PALETTE_YUV420;
183 break;
184 case V4L2_PIX_FMT_YUV411P:
185 palette = VIDEO_PALETTE_YUV411P;
186 break;
187 case V4L2_PIX_FMT_YUV422P:
188 palette = VIDEO_PALETTE_YUV422P;
189 break;
191 return palette;
194 /* ----------------------------------------------------------------- */
196 static int poll_one(struct file *file, struct poll_wqueues *pwq)
198 int retval = 1;
199 poll_table *table;
201 poll_initwait(pwq);
202 table = &pwq->pt;
203 for (;;) {
204 int mask;
205 mask = file->f_op->poll(file, table);
206 if (mask & POLLIN)
207 break;
208 table = NULL;
209 if (signal_pending(current)) {
210 retval = -ERESTARTSYS;
211 break;
213 poll_schedule(pwq, TASK_INTERRUPTIBLE);
215 poll_freewait(pwq);
216 return retval;
219 static int count_inputs(
220 struct file *file,
221 v4l2_kioctl drv)
223 struct v4l2_input input2;
224 int i;
226 for (i = 0;; i++) {
227 memset(&input2, 0, sizeof(input2));
228 input2.index = i;
229 if (0 != drv(file, VIDIOC_ENUMINPUT, &input2))
230 break;
232 return i;
235 static int check_size(
236 struct file *file,
237 v4l2_kioctl drv,
238 int *maxw,
239 int *maxh)
241 struct v4l2_fmtdesc desc2;
242 struct v4l2_format fmt2;
244 memset(&desc2, 0, sizeof(desc2));
245 memset(&fmt2, 0, sizeof(fmt2));
247 desc2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
248 if (0 != drv(file, VIDIOC_ENUM_FMT, &desc2))
249 goto done;
251 fmt2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
252 fmt2.fmt.pix.width = 10000;
253 fmt2.fmt.pix.height = 10000;
254 fmt2.fmt.pix.pixelformat = desc2.pixelformat;
255 if (0 != drv(file, VIDIOC_TRY_FMT, &fmt2))
256 goto done;
258 *maxw = fmt2.fmt.pix.width;
259 *maxh = fmt2.fmt.pix.height;
261 done:
262 return 0;
265 /* ----------------------------------------------------------------- */
267 static noinline long v4l1_compat_get_capabilities(
268 struct video_capability *cap,
269 struct file *file,
270 v4l2_kioctl drv)
272 long err;
273 struct v4l2_framebuffer fbuf;
274 struct v4l2_capability *cap2;
276 cap2 = kzalloc(sizeof(*cap2), GFP_KERNEL);
277 if (!cap2) {
278 err = -ENOMEM;
279 return err;
281 memset(cap, 0, sizeof(*cap));
282 memset(&fbuf, 0, sizeof(fbuf));
284 err = drv(file, VIDIOC_QUERYCAP, cap2);
285 if (err < 0) {
286 dprintk("VIDIOCGCAP / VIDIOC_QUERYCAP: %ld\n", err);
287 goto done;
289 if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY) {
290 err = drv(file, VIDIOC_G_FBUF, &fbuf);
291 if (err < 0) {
292 dprintk("VIDIOCGCAP / VIDIOC_G_FBUF: %ld\n", err);
293 memset(&fbuf, 0, sizeof(fbuf));
295 err = 0;
298 memcpy(cap->name, cap2->card,
299 min(sizeof(cap->name), sizeof(cap2->card)));
300 cap->name[sizeof(cap->name) - 1] = 0;
301 if (cap2->capabilities & V4L2_CAP_VIDEO_CAPTURE)
302 cap->type |= VID_TYPE_CAPTURE;
303 if (cap2->capabilities & V4L2_CAP_TUNER)
304 cap->type |= VID_TYPE_TUNER;
305 if (cap2->capabilities & V4L2_CAP_VBI_CAPTURE)
306 cap->type |= VID_TYPE_TELETEXT;
307 if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY)
308 cap->type |= VID_TYPE_OVERLAY;
309 if (fbuf.capability & V4L2_FBUF_CAP_LIST_CLIPPING)
310 cap->type |= VID_TYPE_CLIPPING;
312 cap->channels = count_inputs(file, drv);
313 check_size(file, drv,
314 &cap->maxwidth, &cap->maxheight);
315 cap->audios = 0; /* FIXME */
316 cap->minwidth = 48; /* FIXME */
317 cap->minheight = 32; /* FIXME */
319 done:
320 kfree(cap2);
321 return err;
324 static noinline long v4l1_compat_get_frame_buffer(
325 struct video_buffer *buffer,
326 struct file *file,
327 v4l2_kioctl drv)
329 long err;
330 struct v4l2_framebuffer fbuf;
332 memset(buffer, 0, sizeof(*buffer));
333 memset(&fbuf, 0, sizeof(fbuf));
335 err = drv(file, VIDIOC_G_FBUF, &fbuf);
336 if (err < 0) {
337 dprintk("VIDIOCGFBUF / VIDIOC_G_FBUF: %ld\n", err);
338 goto done;
340 buffer->base = fbuf.base;
341 buffer->height = fbuf.fmt.height;
342 buffer->width = fbuf.fmt.width;
344 switch (fbuf.fmt.pixelformat) {
345 case V4L2_PIX_FMT_RGB332:
346 buffer->depth = 8;
347 break;
348 case V4L2_PIX_FMT_RGB555:
349 buffer->depth = 15;
350 break;
351 case V4L2_PIX_FMT_RGB565:
352 buffer->depth = 16;
353 break;
354 case V4L2_PIX_FMT_BGR24:
355 buffer->depth = 24;
356 break;
357 case V4L2_PIX_FMT_BGR32:
358 buffer->depth = 32;
359 break;
360 default:
361 buffer->depth = 0;
363 if (fbuf.fmt.bytesperline) {
364 buffer->bytesperline = fbuf.fmt.bytesperline;
365 if (!buffer->depth && buffer->width)
366 buffer->depth = ((fbuf.fmt.bytesperline<<3)
367 + (buffer->width-1))
368 / buffer->width;
369 } else {
370 buffer->bytesperline =
371 (buffer->width * buffer->depth + 7) & 7;
372 buffer->bytesperline >>= 3;
374 done:
375 return err;
378 static noinline long v4l1_compat_set_frame_buffer(
379 struct video_buffer *buffer,
380 struct file *file,
381 v4l2_kioctl drv)
383 long err;
384 struct v4l2_framebuffer fbuf;
386 memset(&fbuf, 0, sizeof(fbuf));
387 fbuf.base = buffer->base;
388 fbuf.fmt.height = buffer->height;
389 fbuf.fmt.width = buffer->width;
390 switch (buffer->depth) {
391 case 8:
392 fbuf.fmt.pixelformat = V4L2_PIX_FMT_RGB332;
393 break;
394 case 15:
395 fbuf.fmt.pixelformat = V4L2_PIX_FMT_RGB555;
396 break;
397 case 16:
398 fbuf.fmt.pixelformat = V4L2_PIX_FMT_RGB565;
399 break;
400 case 24:
401 fbuf.fmt.pixelformat = V4L2_PIX_FMT_BGR24;
402 break;
403 case 32:
404 fbuf.fmt.pixelformat = V4L2_PIX_FMT_BGR32;
405 break;
407 fbuf.fmt.bytesperline = buffer->bytesperline;
408 err = drv(file, VIDIOC_S_FBUF, &fbuf);
409 if (err < 0)
410 dprintk("VIDIOCSFBUF / VIDIOC_S_FBUF: %ld\n", err);
411 return err;
414 static noinline long v4l1_compat_get_win_cap_dimensions(
415 struct video_window *win,
416 struct file *file,
417 v4l2_kioctl drv)
419 long err;
420 struct v4l2_format *fmt;
422 fmt = kzalloc(sizeof(*fmt), GFP_KERNEL);
423 if (!fmt) {
424 err = -ENOMEM;
425 return err;
427 memset(win, 0, sizeof(*win));
429 fmt->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
430 err = drv(file, VIDIOC_G_FMT, fmt);
431 if (err < 0)
432 dprintk("VIDIOCGWIN / VIDIOC_G_WIN: %ld\n", err);
433 if (err == 0) {
434 win->x = fmt->fmt.win.w.left;
435 win->y = fmt->fmt.win.w.top;
436 win->width = fmt->fmt.win.w.width;
437 win->height = fmt->fmt.win.w.height;
438 win->chromakey = fmt->fmt.win.chromakey;
439 win->clips = NULL;
440 win->clipcount = 0;
441 goto done;
444 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
445 err = drv(file, VIDIOC_G_FMT, fmt);
446 if (err < 0) {
447 dprintk("VIDIOCGWIN / VIDIOC_G_FMT: %ld\n", err);
448 goto done;
450 win->x = 0;
451 win->y = 0;
452 win->width = fmt->fmt.pix.width;
453 win->height = fmt->fmt.pix.height;
454 win->chromakey = 0;
455 win->clips = NULL;
456 win->clipcount = 0;
457 done:
458 kfree(fmt);
459 return err;
462 static noinline long v4l1_compat_set_win_cap_dimensions(
463 struct video_window *win,
464 struct file *file,
465 v4l2_kioctl drv)
467 long err, err1, err2;
468 struct v4l2_format *fmt;
470 fmt = kzalloc(sizeof(*fmt), GFP_KERNEL);
471 if (!fmt) {
472 err = -ENOMEM;
473 return err;
475 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
476 drv(file, VIDIOC_STREAMOFF, &fmt->type);
477 err1 = drv(file, VIDIOC_G_FMT, fmt);
478 if (err1 < 0)
479 dprintk("VIDIOCSWIN / VIDIOC_G_FMT: %ld\n", err1);
480 if (err1 == 0) {
481 fmt->fmt.pix.width = win->width;
482 fmt->fmt.pix.height = win->height;
483 fmt->fmt.pix.field = V4L2_FIELD_ANY;
484 fmt->fmt.pix.bytesperline = 0;
485 err = drv(file, VIDIOC_S_FMT, fmt);
486 if (err < 0)
487 dprintk("VIDIOCSWIN / VIDIOC_S_FMT #1: %ld\n",
488 err);
489 win->width = fmt->fmt.pix.width;
490 win->height = fmt->fmt.pix.height;
493 memset(fmt, 0, sizeof(*fmt));
494 fmt->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
495 fmt->fmt.win.w.left = win->x;
496 fmt->fmt.win.w.top = win->y;
497 fmt->fmt.win.w.width = win->width;
498 fmt->fmt.win.w.height = win->height;
499 fmt->fmt.win.chromakey = win->chromakey;
500 fmt->fmt.win.clips = (void __user *)win->clips;
501 fmt->fmt.win.clipcount = win->clipcount;
502 err2 = drv(file, VIDIOC_S_FMT, fmt);
503 if (err2 < 0)
504 dprintk("VIDIOCSWIN / VIDIOC_S_FMT #2: %ld\n", err2);
506 if (err1 != 0 && err2 != 0)
507 err = err1;
508 else
509 err = 0;
510 kfree(fmt);
511 return err;
514 static noinline long v4l1_compat_turn_preview_on_off(
515 int *on,
516 struct file *file,
517 v4l2_kioctl drv)
519 long err;
520 enum v4l2_buf_type captype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
522 if (0 == *on) {
523 /* dirty hack time. But v4l1 has no STREAMOFF
524 * equivalent in the API, and this one at
525 * least comes close ... */
526 drv(file, VIDIOC_STREAMOFF, &captype);
528 err = drv(file, VIDIOC_OVERLAY, on);
529 if (err < 0)
530 dprintk("VIDIOCCAPTURE / VIDIOC_PREVIEW: %ld\n", err);
531 return err;
534 static noinline long v4l1_compat_get_input_info(
535 struct video_channel *chan,
536 struct file *file,
537 v4l2_kioctl drv)
539 long err;
540 struct v4l2_input input2;
541 v4l2_std_id sid;
543 memset(&input2, 0, sizeof(input2));
544 input2.index = chan->channel;
545 err = drv(file, VIDIOC_ENUMINPUT, &input2);
546 if (err < 0) {
547 dprintk("VIDIOCGCHAN / VIDIOC_ENUMINPUT: "
548 "channel=%d err=%ld\n", chan->channel, err);
549 goto done;
551 chan->channel = input2.index;
552 memcpy(chan->name, input2.name,
553 min(sizeof(chan->name), sizeof(input2.name)));
554 chan->name[sizeof(chan->name) - 1] = 0;
555 chan->tuners = (input2.type == V4L2_INPUT_TYPE_TUNER) ? 1 : 0;
556 chan->flags = (chan->tuners) ? VIDEO_VC_TUNER : 0;
557 switch (input2.type) {
558 case V4L2_INPUT_TYPE_TUNER:
559 chan->type = VIDEO_TYPE_TV;
560 break;
561 default:
562 case V4L2_INPUT_TYPE_CAMERA:
563 chan->type = VIDEO_TYPE_CAMERA;
564 break;
566 chan->norm = 0;
567 err = drv(file, VIDIOC_G_STD, &sid);
568 if (err < 0)
569 dprintk("VIDIOCGCHAN / VIDIOC_G_STD: %ld\n", err);
570 if (err == 0) {
571 if (sid & V4L2_STD_PAL)
572 chan->norm = VIDEO_MODE_PAL;
573 if (sid & V4L2_STD_NTSC)
574 chan->norm = VIDEO_MODE_NTSC;
575 if (sid & V4L2_STD_SECAM)
576 chan->norm = VIDEO_MODE_SECAM;
577 if (sid == V4L2_STD_ALL)
578 chan->norm = VIDEO_MODE_AUTO;
580 done:
581 return err;
584 static noinline long v4l1_compat_set_input(
585 struct video_channel *chan,
586 struct file *file,
587 v4l2_kioctl drv)
589 long err;
590 v4l2_std_id sid = 0;
592 err = drv(file, VIDIOC_S_INPUT, &chan->channel);
593 if (err < 0)
594 dprintk("VIDIOCSCHAN / VIDIOC_S_INPUT: %ld\n", err);
595 switch (chan->norm) {
596 case VIDEO_MODE_PAL:
597 sid = V4L2_STD_PAL;
598 break;
599 case VIDEO_MODE_NTSC:
600 sid = V4L2_STD_NTSC;
601 break;
602 case VIDEO_MODE_SECAM:
603 sid = V4L2_STD_SECAM;
604 break;
605 case VIDEO_MODE_AUTO:
606 sid = V4L2_STD_ALL;
607 break;
609 if (0 != sid) {
610 err = drv(file, VIDIOC_S_STD, &sid);
611 if (err < 0)
612 dprintk("VIDIOCSCHAN / VIDIOC_S_STD: %ld\n", err);
614 return err;
617 static noinline long v4l1_compat_get_picture(
618 struct video_picture *pict,
619 struct file *file,
620 v4l2_kioctl drv)
622 long err;
623 struct v4l2_format *fmt;
625 fmt = kzalloc(sizeof(*fmt), GFP_KERNEL);
626 if (!fmt) {
627 err = -ENOMEM;
628 return err;
631 pict->brightness = get_v4l_control(file,
632 V4L2_CID_BRIGHTNESS, drv);
633 pict->hue = get_v4l_control(file,
634 V4L2_CID_HUE, drv);
635 pict->contrast = get_v4l_control(file,
636 V4L2_CID_CONTRAST, drv);
637 pict->colour = get_v4l_control(file,
638 V4L2_CID_SATURATION, drv);
639 pict->whiteness = get_v4l_control(file,
640 V4L2_CID_WHITENESS, drv);
642 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
643 err = drv(file, VIDIOC_G_FMT, fmt);
644 if (err < 0) {
645 dprintk("VIDIOCGPICT / VIDIOC_G_FMT: %ld\n", err);
646 goto done;
649 pict->depth = ((fmt->fmt.pix.bytesperline << 3)
650 + (fmt->fmt.pix.width - 1))
651 / fmt->fmt.pix.width;
652 pict->palette = pixelformat_to_palette(
653 fmt->fmt.pix.pixelformat);
654 done:
655 kfree(fmt);
656 return err;
659 static noinline long v4l1_compat_set_picture(
660 struct video_picture *pict,
661 struct file *file,
662 v4l2_kioctl drv)
664 long err;
665 struct v4l2_framebuffer fbuf;
666 int mem_err = 0, ovl_err = 0;
667 struct v4l2_format *fmt;
669 fmt = kzalloc(sizeof(*fmt), GFP_KERNEL);
670 if (!fmt) {
671 err = -ENOMEM;
672 return err;
674 memset(&fbuf, 0, sizeof(fbuf));
676 set_v4l_control(file,
677 V4L2_CID_BRIGHTNESS, pict->brightness, drv);
678 set_v4l_control(file,
679 V4L2_CID_HUE, pict->hue, drv);
680 set_v4l_control(file,
681 V4L2_CID_CONTRAST, pict->contrast, drv);
682 set_v4l_control(file,
683 V4L2_CID_SATURATION, pict->colour, drv);
684 set_v4l_control(file,
685 V4L2_CID_WHITENESS, pict->whiteness, drv);
687 * V4L1 uses this ioctl to set both memory capture and overlay
688 * pixel format, while V4L2 has two different ioctls for this.
689 * Some cards may not support one or the other, and may support
690 * different pixel formats for memory vs overlay.
693 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
694 err = drv(file, VIDIOC_G_FMT, fmt);
695 /* If VIDIOC_G_FMT failed, then the driver likely doesn't
696 support memory capture. Trying to set the memory capture
697 parameters would be pointless. */
698 if (err < 0) {
699 dprintk("VIDIOCSPICT / VIDIOC_G_FMT: %ld\n", err);
700 mem_err = -1000; /* didn't even try */
701 } else if (fmt->fmt.pix.pixelformat !=
702 palette_to_pixelformat(pict->palette)) {
703 fmt->fmt.pix.pixelformat = palette_to_pixelformat(
704 pict->palette);
705 mem_err = drv(file, VIDIOC_S_FMT, fmt);
706 if (mem_err < 0)
707 dprintk("VIDIOCSPICT / VIDIOC_S_FMT: %d\n",
708 mem_err);
711 err = drv(file, VIDIOC_G_FBUF, &fbuf);
712 /* If VIDIOC_G_FBUF failed, then the driver likely doesn't
713 support overlay. Trying to set the overlay parameters
714 would be quite pointless. */
715 if (err < 0) {
716 dprintk("VIDIOCSPICT / VIDIOC_G_FBUF: %ld\n", err);
717 ovl_err = -1000; /* didn't even try */
718 } else if (fbuf.fmt.pixelformat !=
719 palette_to_pixelformat(pict->palette)) {
720 fbuf.fmt.pixelformat = palette_to_pixelformat(
721 pict->palette);
722 ovl_err = drv(file, VIDIOC_S_FBUF, &fbuf);
723 if (ovl_err < 0)
724 dprintk("VIDIOCSPICT / VIDIOC_S_FBUF: %d\n",
725 ovl_err);
727 if (ovl_err < 0 && mem_err < 0) {
728 /* ioctl failed, couldn't set either parameter */
729 if (mem_err != -1000)
730 err = mem_err;
731 else if (ovl_err == -EPERM)
732 err = 0;
733 else
734 err = ovl_err;
735 } else
736 err = 0;
737 kfree(fmt);
738 return err;
741 static noinline long v4l1_compat_get_tuner(
742 struct video_tuner *tun,
743 struct file *file,
744 v4l2_kioctl drv)
746 long err;
747 int i;
748 struct v4l2_tuner tun2;
749 struct v4l2_standard std2;
750 v4l2_std_id sid;
752 memset(&tun2, 0, sizeof(tun2));
753 err = drv(file, VIDIOC_G_TUNER, &tun2);
754 if (err < 0) {
755 dprintk("VIDIOCGTUNER / VIDIOC_G_TUNER: %ld\n", err);
756 goto done;
758 memcpy(tun->name, tun2.name,
759 min(sizeof(tun->name), sizeof(tun2.name)));
760 tun->name[sizeof(tun->name) - 1] = 0;
761 tun->rangelow = tun2.rangelow;
762 tun->rangehigh = tun2.rangehigh;
763 tun->flags = 0;
764 tun->mode = VIDEO_MODE_AUTO;
766 for (i = 0; i < 64; i++) {
767 memset(&std2, 0, sizeof(std2));
768 std2.index = i;
769 if (0 != drv(file, VIDIOC_ENUMSTD, &std2))
770 break;
771 if (std2.id & V4L2_STD_PAL)
772 tun->flags |= VIDEO_TUNER_PAL;
773 if (std2.id & V4L2_STD_NTSC)
774 tun->flags |= VIDEO_TUNER_NTSC;
775 if (std2.id & V4L2_STD_SECAM)
776 tun->flags |= VIDEO_TUNER_SECAM;
779 err = drv(file, VIDIOC_G_STD, &sid);
780 if (err < 0)
781 dprintk("VIDIOCGTUNER / VIDIOC_G_STD: %ld\n", err);
782 if (err == 0) {
783 if (sid & V4L2_STD_PAL)
784 tun->mode = VIDEO_MODE_PAL;
785 if (sid & V4L2_STD_NTSC)
786 tun->mode = VIDEO_MODE_NTSC;
787 if (sid & V4L2_STD_SECAM)
788 tun->mode = VIDEO_MODE_SECAM;
791 if (tun2.capability & V4L2_TUNER_CAP_LOW)
792 tun->flags |= VIDEO_TUNER_LOW;
793 if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
794 tun->flags |= VIDEO_TUNER_STEREO_ON;
795 tun->signal = tun2.signal;
796 done:
797 return err;
800 static noinline long v4l1_compat_select_tuner(
801 struct video_tuner *tun,
802 struct file *file,
803 v4l2_kioctl drv)
805 long err;
806 struct v4l2_tuner t;/*84 bytes on x86_64*/
807 memset(&t, 0, sizeof(t));
809 t.index = tun->tuner;
811 err = drv(file, VIDIOC_S_TUNER, &t);
812 if (err < 0)
813 dprintk("VIDIOCSTUNER / VIDIOC_S_TUNER: %ld\n", err);
814 return err;
817 static noinline long v4l1_compat_get_frequency(
818 unsigned long *freq,
819 struct file *file,
820 v4l2_kioctl drv)
822 long err;
823 struct v4l2_frequency freq2;
824 memset(&freq2, 0, sizeof(freq2));
826 freq2.tuner = 0;
827 err = drv(file, VIDIOC_G_FREQUENCY, &freq2);
828 if (err < 0)
829 dprintk("VIDIOCGFREQ / VIDIOC_G_FREQUENCY: %ld\n", err);
830 if (0 == err)
831 *freq = freq2.frequency;
832 return err;
835 static noinline long v4l1_compat_set_frequency(
836 unsigned long *freq,
837 struct file *file,
838 v4l2_kioctl drv)
840 long err;
841 struct v4l2_frequency freq2;
842 memset(&freq2, 0, sizeof(freq2));
844 drv(file, VIDIOC_G_FREQUENCY, &freq2);
845 freq2.frequency = *freq;
846 err = drv(file, VIDIOC_S_FREQUENCY, &freq2);
847 if (err < 0)
848 dprintk("VIDIOCSFREQ / VIDIOC_S_FREQUENCY: %ld\n", err);
849 return err;
852 static noinline long v4l1_compat_get_audio(
853 struct video_audio *aud,
854 struct file *file,
855 v4l2_kioctl drv)
857 long err;
858 int i;
859 struct v4l2_queryctrl qctrl2;
860 struct v4l2_audio aud2;
861 struct v4l2_tuner tun2;
862 memset(&aud2, 0, sizeof(aud2));
864 err = drv(file, VIDIOC_G_AUDIO, &aud2);
865 if (err < 0) {
866 dprintk("VIDIOCGAUDIO / VIDIOC_G_AUDIO: %ld\n", err);
867 goto done;
869 memcpy(aud->name, aud2.name,
870 min(sizeof(aud->name), sizeof(aud2.name)));
871 aud->name[sizeof(aud->name) - 1] = 0;
872 aud->audio = aud2.index;
873 aud->flags = 0;
874 i = get_v4l_control(file, V4L2_CID_AUDIO_VOLUME, drv);
875 if (i >= 0) {
876 aud->volume = i;
877 aud->flags |= VIDEO_AUDIO_VOLUME;
879 i = get_v4l_control(file, V4L2_CID_AUDIO_BASS, drv);
880 if (i >= 0) {
881 aud->bass = i;
882 aud->flags |= VIDEO_AUDIO_BASS;
884 i = get_v4l_control(file, V4L2_CID_AUDIO_TREBLE, drv);
885 if (i >= 0) {
886 aud->treble = i;
887 aud->flags |= VIDEO_AUDIO_TREBLE;
889 i = get_v4l_control(file, V4L2_CID_AUDIO_BALANCE, drv);
890 if (i >= 0) {
891 aud->balance = i;
892 aud->flags |= VIDEO_AUDIO_BALANCE;
894 i = get_v4l_control(file, V4L2_CID_AUDIO_MUTE, drv);
895 if (i >= 0) {
896 if (i)
897 aud->flags |= VIDEO_AUDIO_MUTE;
898 aud->flags |= VIDEO_AUDIO_MUTABLE;
900 aud->step = 1;
901 qctrl2.id = V4L2_CID_AUDIO_VOLUME;
902 if (drv(file, VIDIOC_QUERYCTRL, &qctrl2) == 0 &&
903 !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
904 aud->step = qctrl2.step;
905 aud->mode = 0;
907 memset(&tun2, 0, sizeof(tun2));
908 err = drv(file, VIDIOC_G_TUNER, &tun2);
909 if (err < 0) {
910 dprintk("VIDIOCGAUDIO / VIDIOC_G_TUNER: %ld\n", err);
911 err = 0;
912 goto done;
915 if (tun2.rxsubchans & V4L2_TUNER_SUB_LANG2)
916 aud->mode = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
917 else if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
918 aud->mode = VIDEO_SOUND_STEREO;
919 else if (tun2.rxsubchans & V4L2_TUNER_SUB_MONO)
920 aud->mode = VIDEO_SOUND_MONO;
921 done:
922 return err;
925 static noinline long v4l1_compat_set_audio(
926 struct video_audio *aud,
927 struct file *file,
928 v4l2_kioctl drv)
930 long err;
931 struct v4l2_audio aud2;
932 struct v4l2_tuner tun2;
934 memset(&aud2, 0, sizeof(aud2));
935 memset(&tun2, 0, sizeof(tun2));
937 aud2.index = aud->audio;
938 err = drv(file, VIDIOC_S_AUDIO, &aud2);
939 if (err < 0) {
940 dprintk("VIDIOCSAUDIO / VIDIOC_S_AUDIO: %ld\n", err);
941 goto done;
944 set_v4l_control(file, V4L2_CID_AUDIO_VOLUME,
945 aud->volume, drv);
946 set_v4l_control(file, V4L2_CID_AUDIO_BASS,
947 aud->bass, drv);
948 set_v4l_control(file, V4L2_CID_AUDIO_TREBLE,
949 aud->treble, drv);
950 set_v4l_control(file, V4L2_CID_AUDIO_BALANCE,
951 aud->balance, drv);
952 set_v4l_control(file, V4L2_CID_AUDIO_MUTE,
953 !!(aud->flags & VIDEO_AUDIO_MUTE), drv);
955 err = drv(file, VIDIOC_G_TUNER, &tun2);
956 if (err < 0)
957 dprintk("VIDIOCSAUDIO / VIDIOC_G_TUNER: %ld\n", err);
958 if (err == 0) {
959 switch (aud->mode) {
960 default:
961 case VIDEO_SOUND_MONO:
962 case VIDEO_SOUND_LANG1:
963 tun2.audmode = V4L2_TUNER_MODE_MONO;
964 break;
965 case VIDEO_SOUND_STEREO:
966 tun2.audmode = V4L2_TUNER_MODE_STEREO;
967 break;
968 case VIDEO_SOUND_LANG2:
969 tun2.audmode = V4L2_TUNER_MODE_LANG2;
970 break;
972 err = drv(file, VIDIOC_S_TUNER, &tun2);
973 if (err < 0)
974 dprintk("VIDIOCSAUDIO / VIDIOC_S_TUNER: %ld\n", err);
976 err = 0;
977 done:
978 return err;
981 static noinline long v4l1_compat_capture_frame(
982 struct video_mmap *mm,
983 struct file *file,
984 v4l2_kioctl drv)
986 long err;
987 enum v4l2_buf_type captype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
988 struct v4l2_buffer buf;
989 struct v4l2_format *fmt;
991 fmt = kzalloc(sizeof(*fmt), GFP_KERNEL);
992 if (!fmt) {
993 err = -ENOMEM;
994 return err;
996 memset(&buf, 0, sizeof(buf));
998 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
999 err = drv(file, VIDIOC_G_FMT, fmt);
1000 if (err < 0) {
1001 dprintk("VIDIOCMCAPTURE / VIDIOC_G_FMT: %ld\n", err);
1002 goto done;
1004 if (mm->width != fmt->fmt.pix.width ||
1005 mm->height != fmt->fmt.pix.height ||
1006 palette_to_pixelformat(mm->format) !=
1007 fmt->fmt.pix.pixelformat) {
1008 /* New capture format... */
1009 fmt->fmt.pix.width = mm->width;
1010 fmt->fmt.pix.height = mm->height;
1011 fmt->fmt.pix.pixelformat =
1012 palette_to_pixelformat(mm->format);
1013 fmt->fmt.pix.field = V4L2_FIELD_ANY;
1014 fmt->fmt.pix.bytesperline = 0;
1015 err = drv(file, VIDIOC_S_FMT, fmt);
1016 if (err < 0) {
1017 dprintk("VIDIOCMCAPTURE / VIDIOC_S_FMT: %ld\n", err);
1018 goto done;
1021 buf.index = mm->frame;
1022 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1023 err = drv(file, VIDIOC_QUERYBUF, &buf);
1024 if (err < 0) {
1025 dprintk("VIDIOCMCAPTURE / VIDIOC_QUERYBUF: %ld\n", err);
1026 goto done;
1028 err = drv(file, VIDIOC_QBUF, &buf);
1029 if (err < 0) {
1030 dprintk("VIDIOCMCAPTURE / VIDIOC_QBUF: %ld\n", err);
1031 goto done;
1033 err = drv(file, VIDIOC_STREAMON, &captype);
1034 if (err < 0)
1035 dprintk("VIDIOCMCAPTURE / VIDIOC_STREAMON: %ld\n", err);
1036 done:
1037 kfree(fmt);
1038 return err;
1041 static noinline long v4l1_compat_sync(
1042 int *i,
1043 struct file *file,
1044 v4l2_kioctl drv)
1046 long err;
1047 enum v4l2_buf_type captype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1048 struct v4l2_buffer buf;
1049 struct poll_wqueues *pwq;
1051 memset(&buf, 0, sizeof(buf));
1052 buf.index = *i;
1053 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1054 err = drv(file, VIDIOC_QUERYBUF, &buf);
1055 if (err < 0) {
1056 /* No such buffer */
1057 dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %ld\n", err);
1058 goto done;
1060 if (!(buf.flags & V4L2_BUF_FLAG_MAPPED)) {
1061 /* Buffer is not mapped */
1062 err = -EINVAL;
1063 goto done;
1066 /* make sure capture actually runs so we don't block forever */
1067 err = drv(file, VIDIOC_STREAMON, &captype);
1068 if (err < 0) {
1069 dprintk("VIDIOCSYNC / VIDIOC_STREAMON: %ld\n", err);
1070 goto done;
1073 pwq = kmalloc(sizeof(*pwq), GFP_KERNEL);
1074 /* Loop as long as the buffer is queued, but not done */
1075 while ((buf.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
1076 == V4L2_BUF_FLAG_QUEUED) {
1077 err = poll_one(file, pwq);
1078 if (err < 0 || /* error or sleep was interrupted */
1079 err == 0) /* timeout? Shouldn't occur. */
1080 break;
1081 err = drv(file, VIDIOC_QUERYBUF, &buf);
1082 if (err < 0)
1083 dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %ld\n", err);
1085 kfree(pwq);
1086 if (!(buf.flags & V4L2_BUF_FLAG_DONE)) /* not done */
1087 goto done;
1088 do {
1089 err = drv(file, VIDIOC_DQBUF, &buf);
1090 if (err < 0)
1091 dprintk("VIDIOCSYNC / VIDIOC_DQBUF: %ld\n", err);
1092 } while (err == 0 && buf.index != *i);
1093 done:
1094 return err;
1097 static noinline long v4l1_compat_get_vbi_format(
1098 struct vbi_format *fmt,
1099 struct file *file,
1100 v4l2_kioctl drv)
1102 long err;
1103 struct v4l2_format *fmt2;
1105 fmt2 = kzalloc(sizeof(*fmt2), GFP_KERNEL);
1106 if (!fmt2) {
1107 err = -ENOMEM;
1108 return err;
1110 fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1112 err = drv(file, VIDIOC_G_FMT, fmt2);
1113 if (err < 0) {
1114 dprintk("VIDIOCGVBIFMT / VIDIOC_G_FMT: %ld\n", err);
1115 goto done;
1117 if (fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY) {
1118 err = -EINVAL;
1119 goto done;
1121 memset(fmt, 0, sizeof(*fmt));
1122 fmt->samples_per_line = fmt2->fmt.vbi.samples_per_line;
1123 fmt->sampling_rate = fmt2->fmt.vbi.sampling_rate;
1124 fmt->sample_format = VIDEO_PALETTE_RAW;
1125 fmt->start[0] = fmt2->fmt.vbi.start[0];
1126 fmt->count[0] = fmt2->fmt.vbi.count[0];
1127 fmt->start[1] = fmt2->fmt.vbi.start[1];
1128 fmt->count[1] = fmt2->fmt.vbi.count[1];
1129 fmt->flags = fmt2->fmt.vbi.flags & 0x03;
1130 done:
1131 kfree(fmt2);
1132 return err;
1135 static noinline long v4l1_compat_set_vbi_format(
1136 struct vbi_format *fmt,
1137 struct file *file,
1138 v4l2_kioctl drv)
1140 long err;
1141 struct v4l2_format *fmt2 = NULL;
1143 if (VIDEO_PALETTE_RAW != fmt->sample_format) {
1144 err = -EINVAL;
1145 return err;
1148 fmt2 = kzalloc(sizeof(*fmt2), GFP_KERNEL);
1149 if (!fmt2) {
1150 err = -ENOMEM;
1151 return err;
1153 fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1154 fmt2->fmt.vbi.samples_per_line = fmt->samples_per_line;
1155 fmt2->fmt.vbi.sampling_rate = fmt->sampling_rate;
1156 fmt2->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1157 fmt2->fmt.vbi.start[0] = fmt->start[0];
1158 fmt2->fmt.vbi.count[0] = fmt->count[0];
1159 fmt2->fmt.vbi.start[1] = fmt->start[1];
1160 fmt2->fmt.vbi.count[1] = fmt->count[1];
1161 fmt2->fmt.vbi.flags = fmt->flags;
1162 err = drv(file, VIDIOC_TRY_FMT, fmt2);
1163 if (err < 0) {
1164 dprintk("VIDIOCSVBIFMT / VIDIOC_TRY_FMT: %ld\n", err);
1165 goto done;
1168 if (fmt2->fmt.vbi.samples_per_line != fmt->samples_per_line ||
1169 fmt2->fmt.vbi.sampling_rate != fmt->sampling_rate ||
1170 fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY ||
1171 fmt2->fmt.vbi.start[0] != fmt->start[0] ||
1172 fmt2->fmt.vbi.count[0] != fmt->count[0] ||
1173 fmt2->fmt.vbi.start[1] != fmt->start[1] ||
1174 fmt2->fmt.vbi.count[1] != fmt->count[1] ||
1175 fmt2->fmt.vbi.flags != fmt->flags) {
1176 err = -EINVAL;
1177 goto done;
1179 err = drv(file, VIDIOC_S_FMT, fmt2);
1180 if (err < 0)
1181 dprintk("VIDIOCSVBIFMT / VIDIOC_S_FMT: %ld\n", err);
1182 done:
1183 kfree(fmt2);
1184 return err;
1188 * This function is exported.
1190 long
1191 v4l_compat_translate_ioctl(struct file *file,
1192 int cmd,
1193 void *arg,
1194 v4l2_kioctl drv)
1196 long err;
1198 switch (cmd) {
1199 case VIDIOCGCAP: /* capability */
1200 err = v4l1_compat_get_capabilities(arg, file, drv);
1201 break;
1202 case VIDIOCGFBUF: /* get frame buffer */
1203 err = v4l1_compat_get_frame_buffer(arg, file, drv);
1204 break;
1205 case VIDIOCSFBUF: /* set frame buffer */
1206 err = v4l1_compat_set_frame_buffer(arg, file, drv);
1207 break;
1208 case VIDIOCGWIN: /* get window or capture dimensions */
1209 err = v4l1_compat_get_win_cap_dimensions(arg, file, drv);
1210 break;
1211 case VIDIOCSWIN: /* set window and/or capture dimensions */
1212 err = v4l1_compat_set_win_cap_dimensions(arg, file, drv);
1213 break;
1214 case VIDIOCCAPTURE: /* turn on/off preview */
1215 err = v4l1_compat_turn_preview_on_off(arg, file, drv);
1216 break;
1217 case VIDIOCGCHAN: /* get input information */
1218 err = v4l1_compat_get_input_info(arg, file, drv);
1219 break;
1220 case VIDIOCSCHAN: /* set input */
1221 err = v4l1_compat_set_input(arg, file, drv);
1222 break;
1223 case VIDIOCGPICT: /* get tone controls & partial capture format */
1224 err = v4l1_compat_get_picture(arg, file, drv);
1225 break;
1226 case VIDIOCSPICT: /* set tone controls & partial capture format */
1227 err = v4l1_compat_set_picture(arg, file, drv);
1228 break;
1229 case VIDIOCGTUNER: /* get tuner information */
1230 err = v4l1_compat_get_tuner(arg, file, drv);
1231 break;
1232 case VIDIOCSTUNER: /* select a tuner input */
1233 err = v4l1_compat_select_tuner(arg, file, drv);
1234 break;
1235 case VIDIOCGFREQ: /* get frequency */
1236 err = v4l1_compat_get_frequency(arg, file, drv);
1237 break;
1238 case VIDIOCSFREQ: /* set frequency */
1239 err = v4l1_compat_set_frequency(arg, file, drv);
1240 break;
1241 case VIDIOCGAUDIO: /* get audio properties/controls */
1242 err = v4l1_compat_get_audio(arg, file, drv);
1243 break;
1244 case VIDIOCSAUDIO: /* set audio controls */
1245 err = v4l1_compat_set_audio(arg, file, drv);
1246 break;
1247 case VIDIOCMCAPTURE: /* capture a frame */
1248 err = v4l1_compat_capture_frame(arg, file, drv);
1249 break;
1250 case VIDIOCSYNC: /* wait for a frame */
1251 err = v4l1_compat_sync(arg, file, drv);
1252 break;
1253 case VIDIOCGVBIFMT: /* query VBI data capture format */
1254 err = v4l1_compat_get_vbi_format(arg, file, drv);
1255 break;
1256 case VIDIOCSVBIFMT:
1257 err = v4l1_compat_set_vbi_format(arg, file, drv);
1258 break;
1259 default:
1260 err = -ENOIOCTLCMD;
1261 break;
1264 return err;
1266 EXPORT_SYMBOL(v4l_compat_translate_ioctl);
1269 * Local variables:
1270 * c-basic-offset: 8
1271 * End: