blob: f13c0a9d684fe997dcbe0ab593f04d1c49557c21 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -07002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Video for Linux Two
4 * Backward Compatibility Layer
5 *
6 * Support subroutines for providing V4L2 drivers with backward
7 * compatibility with applications using the old API.
8 *
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.
13 *
Mauro Carvalho Chehab43db48d2007-01-09 11:20:59 -030014 * Author: Bill Dirks <bill@thedirks.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * et al.
16 *
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030032#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030033#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <asm/uaccess.h>
36#include <asm/system.h>
37#include <asm/pgtable.h>
38
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030039static unsigned int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040module_param(debug, int, 0644);
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -030041MODULE_PARM_DESC(debug, "enable debug messages");
Linus Torvalds1da177e2005-04-16 15:20:36 -070042MODULE_AUTHOR("Bill Dirks");
43MODULE_DESCRIPTION("v4l(1) compatibility layer for v4l2 drivers.");
44MODULE_LICENSE("GPL");
45
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -030046#define dprintk(fmt, arg...) \
47 do { \
48 if (debug) \
49 printk(KERN_DEBUG "v4l1-compat: " fmt , ## arg);\
50 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
53 * I O C T L T R A N S L A T I O N
54 *
55 * From here on down is the code for translating the numerous
56 * ioctl commands from the old API to the new API.
57 */
58
59static int
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -030060get_v4l_control(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int cid,
62 v4l2_kioctl drv)
63{
64 struct v4l2_queryctrl qctrl2;
65 struct v4l2_control ctrl2;
66 int err;
67
68 qctrl2.id = cid;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -030069 err = drv(file, VIDIOC_QUERYCTRL, &qctrl2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -030071 dprintk("VIDIOC_QUERYCTRL: %d\n", err);
72 if (err == 0 && !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 ctrl2.id = qctrl2.id;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -030074 err = drv(file, VIDIOC_G_CTRL, &ctrl2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -030076 dprintk("VIDIOC_G_CTRL: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return 0;
78 }
79 return ((ctrl2.value - qctrl2.minimum) * 65535
80 + (qctrl2.maximum - qctrl2.minimum) / 2)
81 / (qctrl2.maximum - qctrl2.minimum);
82 }
83 return 0;
84}
85
86static int
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -030087set_v4l_control(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 int cid,
89 int value,
90 v4l2_kioctl drv)
91{
92 struct v4l2_queryctrl qctrl2;
93 struct v4l2_control ctrl2;
94 int err;
95
96 qctrl2.id = cid;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -030097 err = drv(file, VIDIOC_QUERYCTRL, &qctrl2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -030099 dprintk("VIDIOC_QUERYCTRL: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (err == 0 &&
101 !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED) &&
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300102 !(qctrl2.flags & V4L2_CTRL_FLAG_GRABBED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (value < 0)
104 value = 0;
105 if (value > 65535)
106 value = 65535;
107 if (value && qctrl2.type == V4L2_CTRL_TYPE_BOOLEAN)
108 value = 65535;
109 ctrl2.id = qctrl2.id;
110 ctrl2.value =
111 (value * (qctrl2.maximum - qctrl2.minimum)
112 + 32767)
113 / 65535;
114 ctrl2.value += qctrl2.minimum;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300115 err = drv(file, VIDIOC_S_CTRL, &ctrl2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300117 dprintk("VIDIOC_S_CTRL: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 return 0;
120}
121
122/* ----------------------------------------------------------------- */
123
Tobias Klauserc81010b2008-04-21 22:30:21 +0000124static const unsigned int palette2pixelformat[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 [VIDEO_PALETTE_GREY] = V4L2_PIX_FMT_GREY,
126 [VIDEO_PALETTE_RGB555] = V4L2_PIX_FMT_RGB555,
127 [VIDEO_PALETTE_RGB565] = V4L2_PIX_FMT_RGB565,
128 [VIDEO_PALETTE_RGB24] = V4L2_PIX_FMT_BGR24,
129 [VIDEO_PALETTE_RGB32] = V4L2_PIX_FMT_BGR32,
130 /* yuv packed pixel */
131 [VIDEO_PALETTE_YUYV] = V4L2_PIX_FMT_YUYV,
132 [VIDEO_PALETTE_YUV422] = V4L2_PIX_FMT_YUYV,
133 [VIDEO_PALETTE_UYVY] = V4L2_PIX_FMT_UYVY,
134 /* yuv planar */
135 [VIDEO_PALETTE_YUV410P] = V4L2_PIX_FMT_YUV410,
136 [VIDEO_PALETTE_YUV420] = V4L2_PIX_FMT_YUV420,
137 [VIDEO_PALETTE_YUV420P] = V4L2_PIX_FMT_YUV420,
138 [VIDEO_PALETTE_YUV411P] = V4L2_PIX_FMT_YUV411P,
139 [VIDEO_PALETTE_YUV422P] = V4L2_PIX_FMT_YUV422P,
140};
141
Ralf Baechlee8c44312007-10-18 03:07:07 -0700142static unsigned int __pure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143palette_to_pixelformat(unsigned int palette)
144{
145 if (palette < ARRAY_SIZE(palette2pixelformat))
146 return palette2pixelformat[palette];
147 else
148 return 0;
149}
150
Trent Piepho5f124912007-04-27 22:56:28 -0300151static unsigned int __attribute_const__
152pixelformat_to_palette(unsigned int pixelformat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 int palette = 0;
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300155 switch (pixelformat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 case V4L2_PIX_FMT_GREY:
157 palette = VIDEO_PALETTE_GREY;
158 break;
159 case V4L2_PIX_FMT_RGB555:
160 palette = VIDEO_PALETTE_RGB555;
161 break;
162 case V4L2_PIX_FMT_RGB565:
163 palette = VIDEO_PALETTE_RGB565;
164 break;
165 case V4L2_PIX_FMT_BGR24:
166 palette = VIDEO_PALETTE_RGB24;
167 break;
168 case V4L2_PIX_FMT_BGR32:
169 palette = VIDEO_PALETTE_RGB32;
170 break;
171 /* yuv packed pixel */
172 case V4L2_PIX_FMT_YUYV:
173 palette = VIDEO_PALETTE_YUYV;
174 break;
175 case V4L2_PIX_FMT_UYVY:
176 palette = VIDEO_PALETTE_UYVY;
177 break;
178 /* yuv planar */
179 case V4L2_PIX_FMT_YUV410:
180 palette = VIDEO_PALETTE_YUV420;
181 break;
182 case V4L2_PIX_FMT_YUV420:
183 palette = VIDEO_PALETTE_YUV420;
184 break;
185 case V4L2_PIX_FMT_YUV411P:
186 palette = VIDEO_PALETTE_YUV411P;
187 break;
188 case V4L2_PIX_FMT_YUV422P:
189 palette = VIDEO_PALETTE_YUV422P;
190 break;
191 }
192 return palette;
193}
194
195/* ----------------------------------------------------------------- */
196
Marcin Slusarz76e41e42008-04-22 14:45:57 -0300197static int poll_one(struct file *file, struct poll_wqueues *pwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 int retval = 1;
200 poll_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Marcin Slusarz76e41e42008-04-22 14:45:57 -0300202 poll_initwait(pwq);
203 table = &pwq->pt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 for (;;) {
205 int mask;
206 set_current_state(TASK_INTERRUPTIBLE);
207 mask = file->f_op->poll(file, table);
208 if (mask & POLLIN)
209 break;
210 table = NULL;
211 if (signal_pending(current)) {
212 retval = -ERESTARTSYS;
213 break;
214 }
215 schedule();
216 }
217 set_current_state(TASK_RUNNING);
Marcin Slusarz76e41e42008-04-22 14:45:57 -0300218 poll_freewait(pwq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return retval;
220}
221
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300222static int count_inputs(
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300223 struct file *file,
224 v4l2_kioctl drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct v4l2_input input2;
227 int i;
228
229 for (i = 0;; i++) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300230 memset(&input2, 0, sizeof(input2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 input2.index = i;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300232 if (0 != drv(file, VIDIOC_ENUMINPUT, &input2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 break;
234 }
235 return i;
236}
237
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300238static int check_size(
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300239 struct file *file,
240 v4l2_kioctl drv,
241 int *maxw,
242 int *maxh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct v4l2_fmtdesc desc2;
245 struct v4l2_format fmt2;
246
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300247 memset(&desc2, 0, sizeof(desc2));
248 memset(&fmt2, 0, sizeof(fmt2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 desc2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300251 if (0 != drv(file, VIDIOC_ENUM_FMT, &desc2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 goto done;
253
254 fmt2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
255 fmt2.fmt.pix.width = 10000;
256 fmt2.fmt.pix.height = 10000;
257 fmt2.fmt.pix.pixelformat = desc2.pixelformat;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300258 if (0 != drv(file, VIDIOC_TRY_FMT, &fmt2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto done;
260
261 *maxw = fmt2.fmt.pix.width;
262 *maxh = fmt2.fmt.pix.height;
263
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300264done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return 0;
266}
267
268/* ----------------------------------------------------------------- */
269
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300270static noinline int v4l1_compat_get_capabilities(
271 struct video_capability *cap,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300272 struct file *file,
273 v4l2_kioctl drv)
274{
275 int err;
276 struct v4l2_framebuffer fbuf;
277 struct v4l2_capability *cap2;
278
279 cap2 = kzalloc(sizeof(*cap2), GFP_KERNEL);
280 if (!cap2) {
281 err = -ENOMEM;
282 return err;
283 }
284 memset(cap, 0, sizeof(*cap));
285 memset(&fbuf, 0, sizeof(fbuf));
286
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300287 err = drv(file, VIDIOC_QUERYCAP, cap2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300288 if (err < 0) {
289 dprintk("VIDIOCGCAP / VIDIOC_QUERYCAP: %d\n", err);
290 goto done;
291 }
292 if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY) {
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300293 err = drv(file, VIDIOC_G_FBUF, &fbuf);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300294 if (err < 0) {
295 dprintk("VIDIOCGCAP / VIDIOC_G_FBUF: %d\n", err);
296 memset(&fbuf, 0, sizeof(fbuf));
297 }
298 err = 0;
299 }
300
301 memcpy(cap->name, cap2->card,
302 min(sizeof(cap->name), sizeof(cap2->card)));
303 cap->name[sizeof(cap->name) - 1] = 0;
304 if (cap2->capabilities & V4L2_CAP_VIDEO_CAPTURE)
305 cap->type |= VID_TYPE_CAPTURE;
306 if (cap2->capabilities & V4L2_CAP_TUNER)
307 cap->type |= VID_TYPE_TUNER;
308 if (cap2->capabilities & V4L2_CAP_VBI_CAPTURE)
309 cap->type |= VID_TYPE_TELETEXT;
310 if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY)
311 cap->type |= VID_TYPE_OVERLAY;
312 if (fbuf.capability & V4L2_FBUF_CAP_LIST_CLIPPING)
313 cap->type |= VID_TYPE_CLIPPING;
314
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300315 cap->channels = count_inputs(file, drv);
316 check_size(file, drv,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300317 &cap->maxwidth, &cap->maxheight);
318 cap->audios = 0; /* FIXME */
319 cap->minwidth = 48; /* FIXME */
320 cap->minheight = 32; /* FIXME */
321
322done:
323 kfree(cap2);
324 return err;
325}
326
327static noinline int v4l1_compat_get_frame_buffer(
328 struct video_buffer *buffer,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300329 struct file *file,
330 v4l2_kioctl drv)
331{
332 int err;
333 struct v4l2_framebuffer fbuf;
334
335 memset(buffer, 0, sizeof(*buffer));
336 memset(&fbuf, 0, sizeof(fbuf));
337
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300338 err = drv(file, VIDIOC_G_FBUF, &fbuf);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300339 if (err < 0) {
340 dprintk("VIDIOCGFBUF / VIDIOC_G_FBUF: %d\n", err);
341 goto done;
342 }
343 buffer->base = fbuf.base;
344 buffer->height = fbuf.fmt.height;
345 buffer->width = fbuf.fmt.width;
346
347 switch (fbuf.fmt.pixelformat) {
348 case V4L2_PIX_FMT_RGB332:
349 buffer->depth = 8;
350 break;
351 case V4L2_PIX_FMT_RGB555:
352 buffer->depth = 15;
353 break;
354 case V4L2_PIX_FMT_RGB565:
355 buffer->depth = 16;
356 break;
357 case V4L2_PIX_FMT_BGR24:
358 buffer->depth = 24;
359 break;
360 case V4L2_PIX_FMT_BGR32:
361 buffer->depth = 32;
362 break;
363 default:
364 buffer->depth = 0;
365 }
366 if (fbuf.fmt.bytesperline) {
367 buffer->bytesperline = fbuf.fmt.bytesperline;
368 if (!buffer->depth && buffer->width)
369 buffer->depth = ((fbuf.fmt.bytesperline<<3)
370 + (buffer->width-1))
371 / buffer->width;
372 } else {
373 buffer->bytesperline =
374 (buffer->width * buffer->depth + 7) & 7;
375 buffer->bytesperline >>= 3;
376 }
377done:
378 return err;
379}
380
381static noinline int v4l1_compat_set_frame_buffer(
382 struct video_buffer *buffer,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300383 struct file *file,
384 v4l2_kioctl drv)
385{
386 int err;
387 struct v4l2_framebuffer fbuf;
388
389 memset(&fbuf, 0, sizeof(fbuf));
390 fbuf.base = buffer->base;
391 fbuf.fmt.height = buffer->height;
392 fbuf.fmt.width = buffer->width;
393 switch (buffer->depth) {
394 case 8:
395 fbuf.fmt.pixelformat = V4L2_PIX_FMT_RGB332;
396 break;
397 case 15:
398 fbuf.fmt.pixelformat = V4L2_PIX_FMT_RGB555;
399 break;
400 case 16:
401 fbuf.fmt.pixelformat = V4L2_PIX_FMT_RGB565;
402 break;
403 case 24:
404 fbuf.fmt.pixelformat = V4L2_PIX_FMT_BGR24;
405 break;
406 case 32:
407 fbuf.fmt.pixelformat = V4L2_PIX_FMT_BGR32;
408 break;
409 }
410 fbuf.fmt.bytesperline = buffer->bytesperline;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300411 err = drv(file, VIDIOC_S_FBUF, &fbuf);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300412 if (err < 0)
413 dprintk("VIDIOCSFBUF / VIDIOC_S_FBUF: %d\n", err);
414 return err;
415}
416
417static noinline int v4l1_compat_get_win_cap_dimensions(
418 struct video_window *win,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300419 struct file *file,
420 v4l2_kioctl drv)
421{
422 int err;
423 struct v4l2_format *fmt;
424
425 fmt = kzalloc(sizeof(*fmt), GFP_KERNEL);
426 if (!fmt) {
427 err = -ENOMEM;
428 return err;
429 }
430 memset(win, 0, sizeof(*win));
431
432 fmt->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300433 err = drv(file, VIDIOC_G_FMT, fmt);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300434 if (err < 0)
435 dprintk("VIDIOCGWIN / VIDIOC_G_WIN: %d\n", err);
436 if (err == 0) {
437 win->x = fmt->fmt.win.w.left;
438 win->y = fmt->fmt.win.w.top;
439 win->width = fmt->fmt.win.w.width;
440 win->height = fmt->fmt.win.w.height;
441 win->chromakey = fmt->fmt.win.chromakey;
442 win->clips = NULL;
443 win->clipcount = 0;
444 goto done;
445 }
446
447 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300448 err = drv(file, VIDIOC_G_FMT, fmt);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300449 if (err < 0) {
450 dprintk("VIDIOCGWIN / VIDIOC_G_FMT: %d\n", err);
451 goto done;
452 }
453 win->x = 0;
454 win->y = 0;
455 win->width = fmt->fmt.pix.width;
456 win->height = fmt->fmt.pix.height;
457 win->chromakey = 0;
458 win->clips = NULL;
459 win->clipcount = 0;
460done:
461 kfree(fmt);
462 return err;
463}
464
465static noinline int v4l1_compat_set_win_cap_dimensions(
466 struct video_window *win,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300467 struct file *file,
468 v4l2_kioctl drv)
469{
470 int err, err1, err2;
471 struct v4l2_format *fmt;
472
473 fmt = kzalloc(sizeof(*fmt), GFP_KERNEL);
474 if (!fmt) {
475 err = -ENOMEM;
476 return err;
477 }
478 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300479 drv(file, VIDIOC_STREAMOFF, &fmt->type);
480 err1 = drv(file, VIDIOC_G_FMT, fmt);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300481 if (err1 < 0)
482 dprintk("VIDIOCSWIN / VIDIOC_G_FMT: %d\n", err1);
483 if (err1 == 0) {
484 fmt->fmt.pix.width = win->width;
485 fmt->fmt.pix.height = win->height;
486 fmt->fmt.pix.field = V4L2_FIELD_ANY;
487 fmt->fmt.pix.bytesperline = 0;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300488 err = drv(file, VIDIOC_S_FMT, fmt);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300489 if (err < 0)
490 dprintk("VIDIOCSWIN / VIDIOC_S_FMT #1: %d\n",
491 err);
492 win->width = fmt->fmt.pix.width;
493 win->height = fmt->fmt.pix.height;
494 }
495
496 memset(fmt, 0, sizeof(*fmt));
497 fmt->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
498 fmt->fmt.win.w.left = win->x;
499 fmt->fmt.win.w.top = win->y;
500 fmt->fmt.win.w.width = win->width;
501 fmt->fmt.win.w.height = win->height;
502 fmt->fmt.win.chromakey = win->chromakey;
503 fmt->fmt.win.clips = (void __user *)win->clips;
504 fmt->fmt.win.clipcount = win->clipcount;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300505 err2 = drv(file, VIDIOC_S_FMT, fmt);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300506 if (err2 < 0)
507 dprintk("VIDIOCSWIN / VIDIOC_S_FMT #2: %d\n", err2);
508
509 if (err1 != 0 && err2 != 0)
510 err = err1;
511 else
512 err = 0;
513 kfree(fmt);
514 return err;
515}
516
517static noinline int v4l1_compat_turn_preview_on_off(
518 int *on,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300519 struct file *file,
520 v4l2_kioctl drv)
521{
522 int err;
523 enum v4l2_buf_type captype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
524
525 if (0 == *on) {
526 /* dirty hack time. But v4l1 has no STREAMOFF
527 * equivalent in the API, and this one at
528 * least comes close ... */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300529 drv(file, VIDIOC_STREAMOFF, &captype);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300530 }
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300531 err = drv(file, VIDIOC_OVERLAY, on);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300532 if (err < 0)
533 dprintk("VIDIOCCAPTURE / VIDIOC_PREVIEW: %d\n", err);
534 return err;
535}
536
537static noinline int v4l1_compat_get_input_info(
538 struct video_channel *chan,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300539 struct file *file,
540 v4l2_kioctl drv)
541{
542 int err;
543 struct v4l2_input input2;
544 v4l2_std_id sid;
545
546 memset(&input2, 0, sizeof(input2));
547 input2.index = chan->channel;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300548 err = drv(file, VIDIOC_ENUMINPUT, &input2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300549 if (err < 0) {
550 dprintk("VIDIOCGCHAN / VIDIOC_ENUMINPUT: "
551 "channel=%d err=%d\n", chan->channel, err);
552 goto done;
553 }
554 chan->channel = input2.index;
555 memcpy(chan->name, input2.name,
556 min(sizeof(chan->name), sizeof(input2.name)));
557 chan->name[sizeof(chan->name) - 1] = 0;
558 chan->tuners = (input2.type == V4L2_INPUT_TYPE_TUNER) ? 1 : 0;
559 chan->flags = (chan->tuners) ? VIDEO_VC_TUNER : 0;
560 switch (input2.type) {
561 case V4L2_INPUT_TYPE_TUNER:
562 chan->type = VIDEO_TYPE_TV;
563 break;
564 default:
565 case V4L2_INPUT_TYPE_CAMERA:
566 chan->type = VIDEO_TYPE_CAMERA;
567 break;
568 }
569 chan->norm = 0;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300570 err = drv(file, VIDIOC_G_STD, &sid);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300571 if (err < 0)
572 dprintk("VIDIOCGCHAN / VIDIOC_G_STD: %d\n", err);
573 if (err == 0) {
574 if (sid & V4L2_STD_PAL)
575 chan->norm = VIDEO_MODE_PAL;
576 if (sid & V4L2_STD_NTSC)
577 chan->norm = VIDEO_MODE_NTSC;
578 if (sid & V4L2_STD_SECAM)
579 chan->norm = VIDEO_MODE_SECAM;
580 }
581done:
582 return err;
583}
584
585static noinline int v4l1_compat_set_input(
586 struct video_channel *chan,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300587 struct file *file,
588 v4l2_kioctl drv)
589{
590 int err;
591 v4l2_std_id sid = 0;
592
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300593 err = drv(file, VIDIOC_S_INPUT, &chan->channel);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300594 if (err < 0)
595 dprintk("VIDIOCSCHAN / VIDIOC_S_INPUT: %d\n", err);
596 switch (chan->norm) {
597 case VIDEO_MODE_PAL:
598 sid = V4L2_STD_PAL;
599 break;
600 case VIDEO_MODE_NTSC:
601 sid = V4L2_STD_NTSC;
602 break;
603 case VIDEO_MODE_SECAM:
604 sid = V4L2_STD_SECAM;
605 break;
606 }
607 if (0 != sid) {
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300608 err = drv(file, VIDIOC_S_STD, &sid);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300609 if (err < 0)
610 dprintk("VIDIOCSCHAN / VIDIOC_S_STD: %d\n", err);
611 }
612 return err;
613}
614
615static noinline int v4l1_compat_get_picture(
616 struct video_picture *pict,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300617 struct file *file,
618 v4l2_kioctl drv)
619{
620 int err;
621 struct v4l2_format *fmt;
622
623 fmt = kzalloc(sizeof(*fmt), GFP_KERNEL);
624 if (!fmt) {
625 err = -ENOMEM;
626 return err;
627 }
628
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300629 pict->brightness = get_v4l_control(file,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300630 V4L2_CID_BRIGHTNESS, drv);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300631 pict->hue = get_v4l_control(file,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300632 V4L2_CID_HUE, drv);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300633 pict->contrast = get_v4l_control(file,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300634 V4L2_CID_CONTRAST, drv);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300635 pict->colour = get_v4l_control(file,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300636 V4L2_CID_SATURATION, drv);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300637 pict->whiteness = get_v4l_control(file,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300638 V4L2_CID_WHITENESS, drv);
639
640 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300641 err = drv(file, VIDIOC_G_FMT, fmt);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300642 if (err < 0) {
643 dprintk("VIDIOCGPICT / VIDIOC_G_FMT: %d\n", err);
644 goto done;
645 }
646
647 pict->depth = ((fmt->fmt.pix.bytesperline << 3)
648 + (fmt->fmt.pix.width - 1))
649 / fmt->fmt.pix.width;
650 pict->palette = pixelformat_to_palette(
651 fmt->fmt.pix.pixelformat);
652done:
653 kfree(fmt);
654 return err;
655}
656
657static noinline int v4l1_compat_set_picture(
658 struct video_picture *pict,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300659 struct file *file,
660 v4l2_kioctl drv)
661{
662 int err;
663 struct v4l2_framebuffer fbuf;
664 int mem_err = 0, ovl_err = 0;
665 struct v4l2_format *fmt;
666
667 fmt = kzalloc(sizeof(*fmt), GFP_KERNEL);
668 if (!fmt) {
669 err = -ENOMEM;
670 return err;
671 }
672 memset(&fbuf, 0, sizeof(fbuf));
673
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300674 set_v4l_control(file,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300675 V4L2_CID_BRIGHTNESS, pict->brightness, drv);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300676 set_v4l_control(file,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300677 V4L2_CID_HUE, pict->hue, drv);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300678 set_v4l_control(file,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300679 V4L2_CID_CONTRAST, pict->contrast, drv);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300680 set_v4l_control(file,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300681 V4L2_CID_SATURATION, pict->colour, drv);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300682 set_v4l_control(file,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300683 V4L2_CID_WHITENESS, pict->whiteness, drv);
684 /*
685 * V4L1 uses this ioctl to set both memory capture and overlay
686 * pixel format, while V4L2 has two different ioctls for this.
687 * Some cards may not support one or the other, and may support
688 * different pixel formats for memory vs overlay.
689 */
690
691 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300692 err = drv(file, VIDIOC_G_FMT, fmt);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300693 /* If VIDIOC_G_FMT failed, then the driver likely doesn't
694 support memory capture. Trying to set the memory capture
695 parameters would be pointless. */
696 if (err < 0) {
697 dprintk("VIDIOCSPICT / VIDIOC_G_FMT: %d\n", err);
698 mem_err = -1000; /* didn't even try */
699 } else if (fmt->fmt.pix.pixelformat !=
700 palette_to_pixelformat(pict->palette)) {
701 fmt->fmt.pix.pixelformat = palette_to_pixelformat(
702 pict->palette);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300703 mem_err = drv(file, VIDIOC_S_FMT, fmt);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300704 if (mem_err < 0)
705 dprintk("VIDIOCSPICT / VIDIOC_S_FMT: %d\n",
706 mem_err);
707 }
708
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300709 err = drv(file, VIDIOC_G_FBUF, &fbuf);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300710 /* If VIDIOC_G_FBUF failed, then the driver likely doesn't
711 support overlay. Trying to set the overlay parameters
712 would be quite pointless. */
713 if (err < 0) {
714 dprintk("VIDIOCSPICT / VIDIOC_G_FBUF: %d\n", err);
715 ovl_err = -1000; /* didn't even try */
716 } else if (fbuf.fmt.pixelformat !=
717 palette_to_pixelformat(pict->palette)) {
718 fbuf.fmt.pixelformat = palette_to_pixelformat(
719 pict->palette);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300720 ovl_err = drv(file, VIDIOC_S_FBUF, &fbuf);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300721 if (ovl_err < 0)
722 dprintk("VIDIOCSPICT / VIDIOC_S_FBUF: %d\n",
723 ovl_err);
724 }
725 if (ovl_err < 0 && mem_err < 0) {
726 /* ioctl failed, couldn't set either parameter */
727 if (mem_err != -1000)
728 err = mem_err;
729 else if (ovl_err == -EPERM)
730 err = 0;
731 else
732 err = ovl_err;
733 } else
734 err = 0;
735 kfree(fmt);
736 return err;
737}
738
739static noinline int v4l1_compat_get_tuner(
740 struct video_tuner *tun,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300741 struct file *file,
742 v4l2_kioctl drv)
743{
744 int err, i;
745 struct v4l2_tuner tun2;
746 struct v4l2_standard std2;
747 v4l2_std_id sid;
748
749 memset(&tun2, 0, sizeof(tun2));
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300750 err = drv(file, VIDIOC_G_TUNER, &tun2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300751 if (err < 0) {
752 dprintk("VIDIOCGTUNER / VIDIOC_G_TUNER: %d\n", err);
753 goto done;
754 }
755 memcpy(tun->name, tun2.name,
756 min(sizeof(tun->name), sizeof(tun2.name)));
757 tun->name[sizeof(tun->name) - 1] = 0;
758 tun->rangelow = tun2.rangelow;
759 tun->rangehigh = tun2.rangehigh;
760 tun->flags = 0;
761 tun->mode = VIDEO_MODE_AUTO;
762
763 for (i = 0; i < 64; i++) {
764 memset(&std2, 0, sizeof(std2));
765 std2.index = i;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300766 if (0 != drv(file, VIDIOC_ENUMSTD, &std2))
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300767 break;
768 if (std2.id & V4L2_STD_PAL)
769 tun->flags |= VIDEO_TUNER_PAL;
770 if (std2.id & V4L2_STD_NTSC)
771 tun->flags |= VIDEO_TUNER_NTSC;
772 if (std2.id & V4L2_STD_SECAM)
773 tun->flags |= VIDEO_TUNER_SECAM;
774 }
775
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300776 err = drv(file, VIDIOC_G_STD, &sid);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300777 if (err < 0)
778 dprintk("VIDIOCGTUNER / VIDIOC_G_STD: %d\n", err);
779 if (err == 0) {
780 if (sid & V4L2_STD_PAL)
781 tun->mode = VIDEO_MODE_PAL;
782 if (sid & V4L2_STD_NTSC)
783 tun->mode = VIDEO_MODE_NTSC;
784 if (sid & V4L2_STD_SECAM)
785 tun->mode = VIDEO_MODE_SECAM;
786 }
787
788 if (tun2.capability & V4L2_TUNER_CAP_LOW)
789 tun->flags |= VIDEO_TUNER_LOW;
790 if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
791 tun->flags |= VIDEO_TUNER_STEREO_ON;
792 tun->signal = tun2.signal;
793done:
794 return err;
795}
796
797static noinline int v4l1_compat_select_tuner(
798 struct video_tuner *tun,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300799 struct file *file,
800 v4l2_kioctl drv)
801{
802 int err;
803 struct v4l2_tuner t;/*84 bytes on x86_64*/
804 memset(&t, 0, sizeof(t));
805
806 t.index = tun->tuner;
807
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300808 err = drv(file, VIDIOC_S_INPUT, &t);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300809 if (err < 0)
810 dprintk("VIDIOCSTUNER / VIDIOC_S_INPUT: %d\n", err);
811 return err;
812}
813
814static noinline int v4l1_compat_get_frequency(
815 unsigned long *freq,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300816 struct file *file,
817 v4l2_kioctl drv)
818{
819 int err;
820 struct v4l2_frequency freq2;
821 memset(&freq2, 0, sizeof(freq2));
822
823 freq2.tuner = 0;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300824 err = drv(file, VIDIOC_G_FREQUENCY, &freq2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300825 if (err < 0)
826 dprintk("VIDIOCGFREQ / VIDIOC_G_FREQUENCY: %d\n", err);
827 if (0 == err)
828 *freq = freq2.frequency;
829 return err;
830}
831
832static noinline int v4l1_compat_set_frequency(
833 unsigned long *freq,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300834 struct file *file,
835 v4l2_kioctl drv)
836{
837 int err;
838 struct v4l2_frequency freq2;
839 memset(&freq2, 0, sizeof(freq2));
840
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300841 drv(file, VIDIOC_G_FREQUENCY, &freq2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300842 freq2.frequency = *freq;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300843 err = drv(file, VIDIOC_S_FREQUENCY, &freq2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300844 if (err < 0)
845 dprintk("VIDIOCSFREQ / VIDIOC_S_FREQUENCY: %d\n", err);
846 return err;
847}
848
849static noinline int v4l1_compat_get_audio(
850 struct video_audio *aud,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300851 struct file *file,
852 v4l2_kioctl drv)
853{
854 int err, i;
855 struct v4l2_queryctrl qctrl2;
856 struct v4l2_audio aud2;
857 struct v4l2_tuner tun2;
858 memset(&aud2, 0, sizeof(aud2));
859
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300860 err = drv(file, VIDIOC_G_AUDIO, &aud2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300861 if (err < 0) {
862 dprintk("VIDIOCGAUDIO / VIDIOC_G_AUDIO: %d\n", err);
863 goto done;
864 }
865 memcpy(aud->name, aud2.name,
866 min(sizeof(aud->name), sizeof(aud2.name)));
867 aud->name[sizeof(aud->name) - 1] = 0;
868 aud->audio = aud2.index;
869 aud->flags = 0;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300870 i = get_v4l_control(file, V4L2_CID_AUDIO_VOLUME, drv);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300871 if (i >= 0) {
872 aud->volume = i;
873 aud->flags |= VIDEO_AUDIO_VOLUME;
874 }
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300875 i = get_v4l_control(file, V4L2_CID_AUDIO_BASS, drv);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300876 if (i >= 0) {
877 aud->bass = i;
878 aud->flags |= VIDEO_AUDIO_BASS;
879 }
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300880 i = get_v4l_control(file, V4L2_CID_AUDIO_TREBLE, drv);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300881 if (i >= 0) {
882 aud->treble = i;
883 aud->flags |= VIDEO_AUDIO_TREBLE;
884 }
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300885 i = get_v4l_control(file, V4L2_CID_AUDIO_BALANCE, drv);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300886 if (i >= 0) {
887 aud->balance = i;
888 aud->flags |= VIDEO_AUDIO_BALANCE;
889 }
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300890 i = get_v4l_control(file, V4L2_CID_AUDIO_MUTE, drv);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300891 if (i >= 0) {
892 if (i)
893 aud->flags |= VIDEO_AUDIO_MUTE;
894 aud->flags |= VIDEO_AUDIO_MUTABLE;
895 }
896 aud->step = 1;
897 qctrl2.id = V4L2_CID_AUDIO_VOLUME;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300898 if (drv(file, VIDIOC_QUERYCTRL, &qctrl2) == 0 &&
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300899 !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
900 aud->step = qctrl2.step;
901 aud->mode = 0;
902
903 memset(&tun2, 0, sizeof(tun2));
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300904 err = drv(file, VIDIOC_G_TUNER, &tun2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300905 if (err < 0) {
906 dprintk("VIDIOCGAUDIO / VIDIOC_G_TUNER: %d\n", err);
907 err = 0;
908 goto done;
909 }
910
911 if (tun2.rxsubchans & V4L2_TUNER_SUB_LANG2)
912 aud->mode = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
913 else if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
914 aud->mode = VIDEO_SOUND_STEREO;
915 else if (tun2.rxsubchans & V4L2_TUNER_SUB_MONO)
916 aud->mode = VIDEO_SOUND_MONO;
917done:
918 return err;
919}
920
921static noinline int v4l1_compat_set_audio(
922 struct video_audio *aud,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300923 struct file *file,
924 v4l2_kioctl drv)
925{
926 int err;
927 struct v4l2_audio aud2;
928 struct v4l2_tuner tun2;
929
930 memset(&aud2, 0, sizeof(aud2));
931 memset(&tun2, 0, sizeof(tun2));
932
933 aud2.index = aud->audio;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300934 err = drv(file, VIDIOC_S_AUDIO, &aud2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300935 if (err < 0) {
936 dprintk("VIDIOCSAUDIO / VIDIOC_S_AUDIO: %d\n", err);
937 goto done;
938 }
939
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300940 set_v4l_control(file, V4L2_CID_AUDIO_VOLUME,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300941 aud->volume, drv);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300942 set_v4l_control(file, V4L2_CID_AUDIO_BASS,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300943 aud->bass, drv);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300944 set_v4l_control(file, V4L2_CID_AUDIO_TREBLE,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300945 aud->treble, drv);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300946 set_v4l_control(file, V4L2_CID_AUDIO_BALANCE,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300947 aud->balance, drv);
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300948 set_v4l_control(file, V4L2_CID_AUDIO_MUTE,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300949 !!(aud->flags & VIDEO_AUDIO_MUTE), drv);
950
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300951 err = drv(file, VIDIOC_G_TUNER, &tun2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300952 if (err < 0)
953 dprintk("VIDIOCSAUDIO / VIDIOC_G_TUNER: %d\n", err);
954 if (err == 0) {
955 switch (aud->mode) {
956 default:
957 case VIDEO_SOUND_MONO:
958 case VIDEO_SOUND_LANG1:
959 tun2.audmode = V4L2_TUNER_MODE_MONO;
960 break;
961 case VIDEO_SOUND_STEREO:
962 tun2.audmode = V4L2_TUNER_MODE_STEREO;
963 break;
964 case VIDEO_SOUND_LANG2:
965 tun2.audmode = V4L2_TUNER_MODE_LANG2;
966 break;
967 }
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300968 err = drv(file, VIDIOC_S_TUNER, &tun2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300969 if (err < 0)
970 dprintk("VIDIOCSAUDIO / VIDIOC_S_TUNER: %d\n", err);
971 }
972 err = 0;
973done:
974 return err;
975}
976
977static noinline int v4l1_compat_capture_frame(
978 struct video_mmap *mm,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300979 struct file *file,
980 v4l2_kioctl drv)
981{
982 int err;
983 enum v4l2_buf_type captype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
984 struct v4l2_buffer buf;
985 struct v4l2_format *fmt;
986
987 fmt = kzalloc(sizeof(*fmt), GFP_KERNEL);
988 if (!fmt) {
989 err = -ENOMEM;
990 return err;
991 }
992 memset(&buf, 0, sizeof(buf));
993
994 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -0300995 err = drv(file, VIDIOC_G_FMT, fmt);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -0300996 if (err < 0) {
997 dprintk("VIDIOCMCAPTURE / VIDIOC_G_FMT: %d\n", err);
998 goto done;
999 }
1000 if (mm->width != fmt->fmt.pix.width ||
1001 mm->height != fmt->fmt.pix.height ||
1002 palette_to_pixelformat(mm->format) !=
1003 fmt->fmt.pix.pixelformat) {
1004 /* New capture format... */
1005 fmt->fmt.pix.width = mm->width;
1006 fmt->fmt.pix.height = mm->height;
1007 fmt->fmt.pix.pixelformat =
1008 palette_to_pixelformat(mm->format);
1009 fmt->fmt.pix.field = V4L2_FIELD_ANY;
1010 fmt->fmt.pix.bytesperline = 0;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001011 err = drv(file, VIDIOC_S_FMT, fmt);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001012 if (err < 0) {
1013 dprintk("VIDIOCMCAPTURE / VIDIOC_S_FMT: %d\n", err);
1014 goto done;
1015 }
1016 }
1017 buf.index = mm->frame;
1018 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001019 err = drv(file, VIDIOC_QUERYBUF, &buf);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001020 if (err < 0) {
1021 dprintk("VIDIOCMCAPTURE / VIDIOC_QUERYBUF: %d\n", err);
1022 goto done;
1023 }
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001024 err = drv(file, VIDIOC_QBUF, &buf);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001025 if (err < 0) {
1026 dprintk("VIDIOCMCAPTURE / VIDIOC_QBUF: %d\n", err);
1027 goto done;
1028 }
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001029 err = drv(file, VIDIOC_STREAMON, &captype);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001030 if (err < 0)
1031 dprintk("VIDIOCMCAPTURE / VIDIOC_STREAMON: %d\n", err);
1032done:
1033 kfree(fmt);
1034 return err;
1035}
1036
1037static noinline int v4l1_compat_sync(
1038 int *i,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001039 struct file *file,
1040 v4l2_kioctl drv)
1041{
1042 int err;
1043 enum v4l2_buf_type captype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1044 struct v4l2_buffer buf;
Marcin Slusarz76e41e42008-04-22 14:45:57 -03001045 struct poll_wqueues *pwq;
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001046
1047 memset(&buf, 0, sizeof(buf));
1048 buf.index = *i;
1049 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001050 err = drv(file, VIDIOC_QUERYBUF, &buf);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001051 if (err < 0) {
1052 /* No such buffer */
1053 dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n", err);
1054 goto done;
1055 }
1056 if (!(buf.flags & V4L2_BUF_FLAG_MAPPED)) {
1057 /* Buffer is not mapped */
1058 err = -EINVAL;
1059 goto done;
1060 }
1061
1062 /* make sure capture actually runs so we don't block forever */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001063 err = drv(file, VIDIOC_STREAMON, &captype);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001064 if (err < 0) {
1065 dprintk("VIDIOCSYNC / VIDIOC_STREAMON: %d\n", err);
1066 goto done;
1067 }
1068
Marcin Slusarz76e41e42008-04-22 14:45:57 -03001069 pwq = kmalloc(sizeof(*pwq), GFP_KERNEL);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001070 /* Loop as long as the buffer is queued, but not done */
1071 while ((buf.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
1072 == V4L2_BUF_FLAG_QUEUED) {
Marcin Slusarz76e41e42008-04-22 14:45:57 -03001073 err = poll_one(file, pwq);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001074 if (err < 0 || /* error or sleep was interrupted */
1075 err == 0) /* timeout? Shouldn't occur. */
1076 break;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001077 err = drv(file, VIDIOC_QUERYBUF, &buf);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001078 if (err < 0)
1079 dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n", err);
1080 }
Marcin Slusarz76e41e42008-04-22 14:45:57 -03001081 kfree(pwq);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001082 if (!(buf.flags & V4L2_BUF_FLAG_DONE)) /* not done */
1083 goto done;
1084 do {
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001085 err = drv(file, VIDIOC_DQBUF, &buf);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001086 if (err < 0)
1087 dprintk("VIDIOCSYNC / VIDIOC_DQBUF: %d\n", err);
1088 } while (err == 0 && buf.index != *i);
1089done:
1090 return err;
1091}
1092
1093static noinline int v4l1_compat_get_vbi_format(
1094 struct vbi_format *fmt,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001095 struct file *file,
1096 v4l2_kioctl drv)
1097{
1098 int err;
1099 struct v4l2_format *fmt2;
1100
1101 fmt2 = kzalloc(sizeof(*fmt2), GFP_KERNEL);
1102 if (!fmt2) {
1103 err = -ENOMEM;
1104 return err;
1105 }
1106 fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1107
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001108 err = drv(file, VIDIOC_G_FMT, fmt2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001109 if (err < 0) {
1110 dprintk("VIDIOCGVBIFMT / VIDIOC_G_FMT: %d\n", err);
1111 goto done;
1112 }
1113 if (fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY) {
1114 err = -EINVAL;
1115 goto done;
1116 }
1117 memset(fmt, 0, sizeof(*fmt));
1118 fmt->samples_per_line = fmt2->fmt.vbi.samples_per_line;
1119 fmt->sampling_rate = fmt2->fmt.vbi.sampling_rate;
1120 fmt->sample_format = VIDEO_PALETTE_RAW;
1121 fmt->start[0] = fmt2->fmt.vbi.start[0];
1122 fmt->count[0] = fmt2->fmt.vbi.count[0];
1123 fmt->start[1] = fmt2->fmt.vbi.start[1];
1124 fmt->count[1] = fmt2->fmt.vbi.count[1];
1125 fmt->flags = fmt2->fmt.vbi.flags & 0x03;
1126done:
1127 kfree(fmt2);
1128 return err;
1129}
1130
1131static noinline int v4l1_compat_set_vbi_format(
1132 struct vbi_format *fmt,
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001133 struct file *file,
1134 v4l2_kioctl drv)
1135{
1136 int err;
1137 struct v4l2_format *fmt2 = NULL;
1138
1139 if (VIDEO_PALETTE_RAW != fmt->sample_format) {
1140 err = -EINVAL;
1141 return err;
1142 }
1143
1144 fmt2 = kzalloc(sizeof(*fmt2), GFP_KERNEL);
1145 if (!fmt2) {
1146 err = -ENOMEM;
1147 return err;
1148 }
1149 fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1150 fmt2->fmt.vbi.samples_per_line = fmt->samples_per_line;
1151 fmt2->fmt.vbi.sampling_rate = fmt->sampling_rate;
1152 fmt2->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1153 fmt2->fmt.vbi.start[0] = fmt->start[0];
1154 fmt2->fmt.vbi.count[0] = fmt->count[0];
1155 fmt2->fmt.vbi.start[1] = fmt->start[1];
1156 fmt2->fmt.vbi.count[1] = fmt->count[1];
1157 fmt2->fmt.vbi.flags = fmt->flags;
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001158 err = drv(file, VIDIOC_TRY_FMT, fmt2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001159 if (err < 0) {
1160 dprintk("VIDIOCSVBIFMT / VIDIOC_TRY_FMT: %d\n", err);
1161 goto done;
1162 }
1163
1164 if (fmt2->fmt.vbi.samples_per_line != fmt->samples_per_line ||
1165 fmt2->fmt.vbi.sampling_rate != fmt->sampling_rate ||
1166 fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY ||
1167 fmt2->fmt.vbi.start[0] != fmt->start[0] ||
1168 fmt2->fmt.vbi.count[0] != fmt->count[0] ||
1169 fmt2->fmt.vbi.start[1] != fmt->start[1] ||
1170 fmt2->fmt.vbi.count[1] != fmt->count[1] ||
1171 fmt2->fmt.vbi.flags != fmt->flags) {
1172 err = -EINVAL;
1173 goto done;
1174 }
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001175 err = drv(file, VIDIOC_S_FMT, fmt2);
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001176 if (err < 0)
1177 dprintk("VIDIOCSVBIFMT / VIDIOC_S_FMT: %d\n", err);
1178done:
1179 kfree(fmt2);
1180 return err;
1181}
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183/*
1184 * This function is exported.
1185 */
1186int
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001187v4l_compat_translate_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 int cmd,
1189 void *arg,
1190 v4l2_kioctl drv)
1191{
Marcin Slusarzb524f7b2008-04-22 14:45:57 -03001192 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 switch (cmd) {
1195 case VIDIOCGCAP: /* capability */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001196 err = v4l1_compat_get_capabilities(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 case VIDIOCGFBUF: /* get frame buffer */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001199 err = v4l1_compat_get_frame_buffer(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 case VIDIOCSFBUF: /* set frame buffer */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001202 err = v4l1_compat_set_frame_buffer(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 case VIDIOCGWIN: /* get window or capture dimensions */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001205 err = v4l1_compat_get_win_cap_dimensions(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 case VIDIOCSWIN: /* set window and/or capture dimensions */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001208 err = v4l1_compat_set_win_cap_dimensions(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 case VIDIOCCAPTURE: /* turn on/off preview */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001211 err = v4l1_compat_turn_preview_on_off(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 case VIDIOCGCHAN: /* get input information */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001214 err = v4l1_compat_get_input_info(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 case VIDIOCSCHAN: /* set input */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001217 err = v4l1_compat_set_input(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 case VIDIOCGPICT: /* get tone controls & partial capture format */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001220 err = v4l1_compat_get_picture(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 case VIDIOCSPICT: /* set tone controls & partial capture format */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001223 err = v4l1_compat_set_picture(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 case VIDIOCGTUNER: /* get tuner information */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001226 err = v4l1_compat_get_tuner(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 case VIDIOCSTUNER: /* select a tuner input */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001229 err = v4l1_compat_select_tuner(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 case VIDIOCGFREQ: /* get frequency */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001232 err = v4l1_compat_get_frequency(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 case VIDIOCSFREQ: /* set frequency */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001235 err = v4l1_compat_set_frequency(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 case VIDIOCGAUDIO: /* get audio properties/controls */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001238 err = v4l1_compat_get_audio(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 case VIDIOCSAUDIO: /* set audio controls */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001241 err = v4l1_compat_set_audio(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 case VIDIOCMCAPTURE: /* capture a frame */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001244 err = v4l1_compat_capture_frame(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 case VIDIOCSYNC: /* wait for a frame */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001247 err = v4l1_compat_sync(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 case VIDIOCGVBIFMT: /* query VBI data capture format */
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001250 err = v4l1_compat_get_vbi_format(arg, file, drv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001251 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 case VIDIOCSVBIFMT:
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001253 err = v4l1_compat_set_vbi_format(arg, file, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 default:
1256 err = -ENOIOCTLCMD;
1257 break;
1258 }
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return err;
1261}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262EXPORT_SYMBOL(v4l_compat_translate_ioctl);
1263
1264/*
1265 * Local variables:
1266 * c-basic-offset: 8
1267 * End:
1268 */