blob: 6455b2f43bed6f30792cedf696fa9853df1a1bf6 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <asm/uaccess.h>
35#include <asm/system.h>
36#include <asm/pgtable.h>
37
38#ifdef CONFIG_KMOD
39#include <linux/kmod.h>
40#endif
41
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030042static unsigned int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043module_param(debug, int, 0644);
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -030044MODULE_PARM_DESC(debug, "enable debug messages");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045MODULE_AUTHOR("Bill Dirks");
46MODULE_DESCRIPTION("v4l(1) compatibility layer for v4l2 drivers.");
47MODULE_LICENSE("GPL");
48
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -030049#define dprintk(fmt, arg...) \
50 do { \
51 if (debug) \
52 printk(KERN_DEBUG "v4l1-compat: " fmt , ## arg);\
53 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * I O C T L T R A N S L A T I O N
57 *
58 * From here on down is the code for translating the numerous
59 * ioctl commands from the old API to the new API.
60 */
61
62static int
63get_v4l_control(struct inode *inode,
64 struct file *file,
65 int cid,
66 v4l2_kioctl drv)
67{
68 struct v4l2_queryctrl qctrl2;
69 struct v4l2_control ctrl2;
70 int err;
71
72 qctrl2.id = cid;
73 err = drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2);
74 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -030075 dprintk("VIDIOC_QUERYCTRL: %d\n", err);
76 if (err == 0 && !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 ctrl2.id = qctrl2.id;
78 err = drv(inode, file, VIDIOC_G_CTRL, &ctrl2);
79 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -030080 dprintk("VIDIOC_G_CTRL: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return 0;
82 }
83 return ((ctrl2.value - qctrl2.minimum) * 65535
84 + (qctrl2.maximum - qctrl2.minimum) / 2)
85 / (qctrl2.maximum - qctrl2.minimum);
86 }
87 return 0;
88}
89
90static int
91set_v4l_control(struct inode *inode,
92 struct file *file,
93 int cid,
94 int value,
95 v4l2_kioctl drv)
96{
97 struct v4l2_queryctrl qctrl2;
98 struct v4l2_control ctrl2;
99 int err;
100
101 qctrl2.id = cid;
102 err = drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2);
103 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300104 dprintk("VIDIOC_QUERYCTRL: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (err == 0 &&
106 !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED) &&
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300107 !(qctrl2.flags & V4L2_CTRL_FLAG_GRABBED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (value < 0)
109 value = 0;
110 if (value > 65535)
111 value = 65535;
112 if (value && qctrl2.type == V4L2_CTRL_TYPE_BOOLEAN)
113 value = 65535;
114 ctrl2.id = qctrl2.id;
115 ctrl2.value =
116 (value * (qctrl2.maximum - qctrl2.minimum)
117 + 32767)
118 / 65535;
119 ctrl2.value += qctrl2.minimum;
120 err = drv(inode, file, VIDIOC_S_CTRL, &ctrl2);
121 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300122 dprintk("VIDIOC_S_CTRL: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124 return 0;
125}
126
127/* ----------------------------------------------------------------- */
128
Tobias Klauserc81010b2008-04-21 22:30:21 +0000129static const unsigned int palette2pixelformat[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 [VIDEO_PALETTE_GREY] = V4L2_PIX_FMT_GREY,
131 [VIDEO_PALETTE_RGB555] = V4L2_PIX_FMT_RGB555,
132 [VIDEO_PALETTE_RGB565] = V4L2_PIX_FMT_RGB565,
133 [VIDEO_PALETTE_RGB24] = V4L2_PIX_FMT_BGR24,
134 [VIDEO_PALETTE_RGB32] = V4L2_PIX_FMT_BGR32,
135 /* yuv packed pixel */
136 [VIDEO_PALETTE_YUYV] = V4L2_PIX_FMT_YUYV,
137 [VIDEO_PALETTE_YUV422] = V4L2_PIX_FMT_YUYV,
138 [VIDEO_PALETTE_UYVY] = V4L2_PIX_FMT_UYVY,
139 /* yuv planar */
140 [VIDEO_PALETTE_YUV410P] = V4L2_PIX_FMT_YUV410,
141 [VIDEO_PALETTE_YUV420] = V4L2_PIX_FMT_YUV420,
142 [VIDEO_PALETTE_YUV420P] = V4L2_PIX_FMT_YUV420,
143 [VIDEO_PALETTE_YUV411P] = V4L2_PIX_FMT_YUV411P,
144 [VIDEO_PALETTE_YUV422P] = V4L2_PIX_FMT_YUV422P,
145};
146
Ralf Baechlee8c44312007-10-18 03:07:07 -0700147static unsigned int __pure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148palette_to_pixelformat(unsigned int palette)
149{
150 if (palette < ARRAY_SIZE(palette2pixelformat))
151 return palette2pixelformat[palette];
152 else
153 return 0;
154}
155
Trent Piepho5f124912007-04-27 22:56:28 -0300156static unsigned int __attribute_const__
157pixelformat_to_palette(unsigned int pixelformat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 int palette = 0;
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300160 switch (pixelformat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 case V4L2_PIX_FMT_GREY:
162 palette = VIDEO_PALETTE_GREY;
163 break;
164 case V4L2_PIX_FMT_RGB555:
165 palette = VIDEO_PALETTE_RGB555;
166 break;
167 case V4L2_PIX_FMT_RGB565:
168 palette = VIDEO_PALETTE_RGB565;
169 break;
170 case V4L2_PIX_FMT_BGR24:
171 palette = VIDEO_PALETTE_RGB24;
172 break;
173 case V4L2_PIX_FMT_BGR32:
174 palette = VIDEO_PALETTE_RGB32;
175 break;
176 /* yuv packed pixel */
177 case V4L2_PIX_FMT_YUYV:
178 palette = VIDEO_PALETTE_YUYV;
179 break;
180 case V4L2_PIX_FMT_UYVY:
181 palette = VIDEO_PALETTE_UYVY;
182 break;
183 /* yuv planar */
184 case V4L2_PIX_FMT_YUV410:
185 palette = VIDEO_PALETTE_YUV420;
186 break;
187 case V4L2_PIX_FMT_YUV420:
188 palette = VIDEO_PALETTE_YUV420;
189 break;
190 case V4L2_PIX_FMT_YUV411P:
191 palette = VIDEO_PALETTE_YUV411P;
192 break;
193 case V4L2_PIX_FMT_YUV422P:
194 palette = VIDEO_PALETTE_YUV422P;
195 break;
196 }
197 return palette;
198}
199
200/* ----------------------------------------------------------------- */
201
202static int poll_one(struct file *file)
203{
204 int retval = 1;
205 poll_table *table;
206 struct poll_wqueues pwq;
207
208 poll_initwait(&pwq);
209 table = &pwq.pt;
210 for (;;) {
211 int mask;
212 set_current_state(TASK_INTERRUPTIBLE);
213 mask = file->f_op->poll(file, table);
214 if (mask & POLLIN)
215 break;
216 table = NULL;
217 if (signal_pending(current)) {
218 retval = -ERESTARTSYS;
219 break;
220 }
221 schedule();
222 }
223 set_current_state(TASK_RUNNING);
224 poll_freewait(&pwq);
225 return retval;
226}
227
228static int count_inputs(struct inode *inode,
229 struct file *file,
230 v4l2_kioctl drv)
231{
232 struct v4l2_input input2;
233 int i;
234
235 for (i = 0;; i++) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300236 memset(&input2, 0, sizeof(input2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 input2.index = i;
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300238 if (0 != drv(inode, file, VIDIOC_ENUMINPUT, &input2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 break;
240 }
241 return i;
242}
243
244static int check_size(struct inode *inode,
245 struct file *file,
246 v4l2_kioctl drv,
247 int *maxw, int *maxh)
248{
249 struct v4l2_fmtdesc desc2;
250 struct v4l2_format fmt2;
251
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300252 memset(&desc2, 0, sizeof(desc2));
253 memset(&fmt2, 0, sizeof(fmt2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 desc2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300256 if (0 != drv(inode, file, VIDIOC_ENUM_FMT, &desc2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 goto done;
258
259 fmt2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
260 fmt2.fmt.pix.width = 10000;
261 fmt2.fmt.pix.height = 10000;
262 fmt2.fmt.pix.pixelformat = desc2.pixelformat;
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300263 if (0 != drv(inode, file, VIDIOC_TRY_FMT, &fmt2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto done;
265
266 *maxw = fmt2.fmt.pix.width;
267 *maxh = fmt2.fmt.pix.height;
268
269 done:
270 return 0;
271}
272
273/* ----------------------------------------------------------------- */
274
275/*
276 * This function is exported.
277 */
278int
279v4l_compat_translate_ioctl(struct inode *inode,
280 struct file *file,
281 int cmd,
282 void *arg,
283 v4l2_kioctl drv)
284{
285 struct v4l2_capability *cap2 = NULL;
286 struct v4l2_format *fmt2 = NULL;
287 enum v4l2_buf_type captype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
288
289 struct v4l2_framebuffer fbuf2;
290 struct v4l2_input input2;
291 struct v4l2_tuner tun2;
292 struct v4l2_standard std2;
293 struct v4l2_frequency freq2;
294 struct v4l2_audio aud2;
295 struct v4l2_queryctrl qctrl2;
296 struct v4l2_buffer buf2;
297 v4l2_std_id sid;
298 int i, err = 0;
299
300 switch (cmd) {
301 case VIDIOCGCAP: /* capability */
302 {
303 struct video_capability *cap = arg;
304
Cyrill Gorcunovc77990e2008-03-05 20:24:43 -0300305 cap2 = kzalloc(sizeof(*cap2), GFP_KERNEL);
306 if (!cap2) {
307 err = -ENOMEM;
308 break;
309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 memset(cap, 0, sizeof(*cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 memset(&fbuf2, 0, sizeof(fbuf2));
312
313 err = drv(inode, file, VIDIOC_QUERYCAP, cap2);
314 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300315 dprintk("VIDIOCGCAP / VIDIOC_QUERYCAP: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 break;
317 }
318 if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY) {
319 err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
320 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300321 dprintk("VIDIOCGCAP / VIDIOC_G_FBUF: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 memset(&fbuf2, 0, sizeof(fbuf2));
323 }
324 err = 0;
325 }
326
327 memcpy(cap->name, cap2->card,
328 min(sizeof(cap->name), sizeof(cap2->card)));
329 cap->name[sizeof(cap->name) - 1] = 0;
330 if (cap2->capabilities & V4L2_CAP_VIDEO_CAPTURE)
331 cap->type |= VID_TYPE_CAPTURE;
332 if (cap2->capabilities & V4L2_CAP_TUNER)
333 cap->type |= VID_TYPE_TUNER;
334 if (cap2->capabilities & V4L2_CAP_VBI_CAPTURE)
335 cap->type |= VID_TYPE_TELETEXT;
336 if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY)
337 cap->type |= VID_TYPE_OVERLAY;
338 if (fbuf2.capability & V4L2_FBUF_CAP_LIST_CLIPPING)
339 cap->type |= VID_TYPE_CLIPPING;
340
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300341 cap->channels = count_inputs(inode, file, drv);
342 check_size(inode, file, drv,
343 &cap->maxwidth, &cap->maxheight);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 cap->audios = 0; /* FIXME */
345 cap->minwidth = 48; /* FIXME */
346 cap->minheight = 32; /* FIXME */
347 break;
348 }
349 case VIDIOCGFBUF: /* get frame buffer */
350 {
351 struct video_buffer *buffer = arg;
352
Mauro Carvalho Chehab37026272006-08-06 09:10:06 -0300353 memset(buffer, 0, sizeof(*buffer));
audetto@tiscali.it499c1862006-11-20 18:27:44 -0300354 memset(&fbuf2, 0, sizeof(fbuf2));
Mauro Carvalho Chehab37026272006-08-06 09:10:06 -0300355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
357 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300358 dprintk("VIDIOCGFBUF / VIDIOC_G_FBUF: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 break;
360 }
361 buffer->base = fbuf2.base;
362 buffer->height = fbuf2.fmt.height;
363 buffer->width = fbuf2.fmt.width;
364
365 switch (fbuf2.fmt.pixelformat) {
366 case V4L2_PIX_FMT_RGB332:
367 buffer->depth = 8;
Mauro Carvalho Chehab37026272006-08-06 09:10:06 -0300368 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 case V4L2_PIX_FMT_RGB555:
370 buffer->depth = 15;
371 break;
372 case V4L2_PIX_FMT_RGB565:
373 buffer->depth = 16;
374 break;
375 case V4L2_PIX_FMT_BGR24:
376 buffer->depth = 24;
377 break;
378 case V4L2_PIX_FMT_BGR32:
379 buffer->depth = 32;
380 break;
381 default:
382 buffer->depth = 0;
383 }
Mauro Carvalho Chehab37026272006-08-06 09:10:06 -0300384 if (fbuf2.fmt.bytesperline) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 buffer->bytesperline = fbuf2.fmt.bytesperline;
Mauro Carvalho Chehab37026272006-08-06 09:10:06 -0300386 if (!buffer->depth && buffer->width)
387 buffer->depth = ((fbuf2.fmt.bytesperline<<3)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300388 + (buffer->width-1))
389 / buffer->width;
Mauro Carvalho Chehab37026272006-08-06 09:10:06 -0300390 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 buffer->bytesperline =
392 (buffer->width * buffer->depth + 7) & 7;
393 buffer->bytesperline >>= 3;
394 }
395 break;
396 }
397 case VIDIOCSFBUF: /* set frame buffer */
398 {
399 struct video_buffer *buffer = arg;
400
401 memset(&fbuf2, 0, sizeof(fbuf2));
402 fbuf2.base = buffer->base;
403 fbuf2.fmt.height = buffer->height;
404 fbuf2.fmt.width = buffer->width;
405 switch (buffer->depth) {
406 case 8:
407 fbuf2.fmt.pixelformat = V4L2_PIX_FMT_RGB332;
408 break;
409 case 15:
410 fbuf2.fmt.pixelformat = V4L2_PIX_FMT_RGB555;
411 break;
412 case 16:
413 fbuf2.fmt.pixelformat = V4L2_PIX_FMT_RGB565;
414 break;
415 case 24:
416 fbuf2.fmt.pixelformat = V4L2_PIX_FMT_BGR24;
417 break;
418 case 32:
419 fbuf2.fmt.pixelformat = V4L2_PIX_FMT_BGR32;
420 break;
421 }
422 fbuf2.fmt.bytesperline = buffer->bytesperline;
423 err = drv(inode, file, VIDIOC_S_FBUF, &fbuf2);
424 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300425 dprintk("VIDIOCSFBUF / VIDIOC_S_FBUF: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 break;
427 }
428 case VIDIOCGWIN: /* get window or capture dimensions */
429 {
430 struct video_window *win = arg;
431
Cyrill Gorcunovc77990e2008-03-05 20:24:43 -0300432 fmt2 = kzalloc(sizeof(*fmt2), GFP_KERNEL);
433 if (!fmt2) {
434 err = -ENOMEM;
435 break;
436 }
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300437 memset(win, 0, sizeof(*win));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 fmt2->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
440 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
441 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300442 dprintk("VIDIOCGWIN / VIDIOC_G_WIN: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (err == 0) {
444 win->x = fmt2->fmt.win.w.left;
445 win->y = fmt2->fmt.win.w.top;
446 win->width = fmt2->fmt.win.w.width;
447 win->height = fmt2->fmt.win.w.height;
448 win->chromakey = fmt2->fmt.win.chromakey;
449 win->clips = NULL;
450 win->clipcount = 0;
451 break;
452 }
453
454 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
455 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
456 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300457 dprintk("VIDIOCGWIN / VIDIOC_G_FMT: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 break;
459 }
460 win->x = 0;
461 win->y = 0;
462 win->width = fmt2->fmt.pix.width;
463 win->height = fmt2->fmt.pix.height;
464 win->chromakey = 0;
465 win->clips = NULL;
466 win->clipcount = 0;
467 break;
468 }
469 case VIDIOCSWIN: /* set window and/or capture dimensions */
470 {
471 struct video_window *win = arg;
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300472 int err1, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Cyrill Gorcunovc77990e2008-03-05 20:24:43 -0300474 fmt2 = kzalloc(sizeof(*fmt2), GFP_KERNEL);
475 if (!fmt2) {
476 err = -ENOMEM;
477 break;
478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
480 drv(inode, file, VIDIOC_STREAMOFF, &fmt2->type);
481 err1 = drv(inode, file, VIDIOC_G_FMT, fmt2);
482 if (err1 < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300483 dprintk("VIDIOCSWIN / VIDIOC_G_FMT: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (err1 == 0) {
485 fmt2->fmt.pix.width = win->width;
486 fmt2->fmt.pix.height = win->height;
487 fmt2->fmt.pix.field = V4L2_FIELD_ANY;
488 fmt2->fmt.pix.bytesperline = 0;
489 err = drv(inode, file, VIDIOC_S_FMT, fmt2);
490 if (err < 0)
491 dprintk("VIDIOCSWIN / VIDIOC_S_FMT #1: %d\n",
492 err);
493 win->width = fmt2->fmt.pix.width;
494 win->height = fmt2->fmt.pix.height;
495 }
496
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300497 memset(fmt2, 0, sizeof(*fmt2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 fmt2->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
499 fmt2->fmt.win.w.left = win->x;
500 fmt2->fmt.win.w.top = win->y;
501 fmt2->fmt.win.w.width = win->width;
502 fmt2->fmt.win.w.height = win->height;
503 fmt2->fmt.win.chromakey = win->chromakey;
504 fmt2->fmt.win.clips = (void __user *)win->clips;
505 fmt2->fmt.win.clipcount = win->clipcount;
506 err2 = drv(inode, file, VIDIOC_S_FMT, fmt2);
507 if (err2 < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300508 dprintk("VIDIOCSWIN / VIDIOC_S_FMT #2: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (err1 != 0 && err2 != 0)
511 err = err1;
512 break;
513 }
514 case VIDIOCCAPTURE: /* turn on/off preview */
515 {
516 int *on = arg;
517
518 if (0 == *on) {
519 /* dirty hack time. But v4l1 has no STREAMOFF
520 * equivalent in the API, and this one at
521 * least comes close ... */
522 drv(inode, file, VIDIOC_STREAMOFF, &captype);
523 }
524 err = drv(inode, file, VIDIOC_OVERLAY, arg);
525 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300526 dprintk("VIDIOCCAPTURE / VIDIOC_PREVIEW: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 break;
528 }
529 case VIDIOCGCHAN: /* get input information */
530 {
531 struct video_channel *chan = arg;
532
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300533 memset(&input2, 0, sizeof(input2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 input2.index = chan->channel;
535 err = drv(inode, file, VIDIOC_ENUMINPUT, &input2);
536 if (err < 0) {
537 dprintk("VIDIOCGCHAN / VIDIOC_ENUMINPUT: "
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300538 "channel=%d err=%d\n", chan->channel, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 break;
540 }
541 chan->channel = input2.index;
542 memcpy(chan->name, input2.name,
543 min(sizeof(chan->name), sizeof(input2.name)));
544 chan->name[sizeof(chan->name) - 1] = 0;
545 chan->tuners = (input2.type == V4L2_INPUT_TYPE_TUNER) ? 1 : 0;
546 chan->flags = (chan->tuners) ? VIDEO_VC_TUNER : 0;
547 switch (input2.type) {
548 case V4L2_INPUT_TYPE_TUNER:
549 chan->type = VIDEO_TYPE_TV;
550 break;
551 default:
552 case V4L2_INPUT_TYPE_CAMERA:
553 chan->type = VIDEO_TYPE_CAMERA;
554 break;
555 }
556 chan->norm = 0;
557 err = drv(inode, file, VIDIOC_G_STD, &sid);
558 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300559 dprintk("VIDIOCGCHAN / VIDIOC_G_STD: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (err == 0) {
561 if (sid & V4L2_STD_PAL)
562 chan->norm = VIDEO_MODE_PAL;
563 if (sid & V4L2_STD_NTSC)
564 chan->norm = VIDEO_MODE_NTSC;
565 if (sid & V4L2_STD_SECAM)
566 chan->norm = VIDEO_MODE_SECAM;
567 }
568 break;
569 }
570 case VIDIOCSCHAN: /* set input */
571 {
572 struct video_channel *chan = arg;
573
574 sid = 0;
575 err = drv(inode, file, VIDIOC_S_INPUT, &chan->channel);
576 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300577 dprintk("VIDIOCSCHAN / VIDIOC_S_INPUT: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 switch (chan->norm) {
579 case VIDEO_MODE_PAL:
580 sid = V4L2_STD_PAL;
581 break;
582 case VIDEO_MODE_NTSC:
583 sid = V4L2_STD_NTSC;
584 break;
585 case VIDEO_MODE_SECAM:
586 sid = V4L2_STD_SECAM;
587 break;
588 }
589 if (0 != sid) {
590 err = drv(inode, file, VIDIOC_S_STD, &sid);
591 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300592 dprintk("VIDIOCSCHAN / VIDIOC_S_STD: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 break;
595 }
596 case VIDIOCGPICT: /* get tone controls & partial capture format */
597 {
598 struct video_picture *pict = arg;
599
Cyrill Gorcunovc77990e2008-03-05 20:24:43 -0300600 fmt2 = kzalloc(sizeof(*fmt2), GFP_KERNEL);
601 if (!fmt2) {
602 err = -ENOMEM;
603 break;
604 }
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 pict->brightness = get_v4l_control(inode, file,
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300607 V4L2_CID_BRIGHTNESS, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 pict->hue = get_v4l_control(inode, file,
609 V4L2_CID_HUE, drv);
610 pict->contrast = get_v4l_control(inode, file,
611 V4L2_CID_CONTRAST, drv);
612 pict->colour = get_v4l_control(inode, file,
613 V4L2_CID_SATURATION, drv);
614 pict->whiteness = get_v4l_control(inode, file,
615 V4L2_CID_WHITENESS, drv);
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
618 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
619 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300620 dprintk("VIDIOCGPICT / VIDIOC_G_FMT: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 break;
622 }
Mauro Carvalho Chehab8a016dc2006-08-08 09:10:16 -0300623
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300624 pict->depth = ((fmt2->fmt.pix.bytesperline << 3)
625 + (fmt2->fmt.pix.width - 1))
626 / fmt2->fmt.pix.width;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 pict->palette = pixelformat_to_palette(
628 fmt2->fmt.pix.pixelformat);
629 break;
630 }
631 case VIDIOCSPICT: /* set tone controls & partial capture format */
632 {
633 struct video_picture *pict = arg;
Trent Piephobbe24862007-04-27 22:56:29 -0300634 int mem_err = 0, ovl_err = 0;
635
Cyrill Gorcunovc77990e2008-03-05 20:24:43 -0300636 fmt2 = kzalloc(sizeof(*fmt2), GFP_KERNEL);
637 if (!fmt2) {
638 err = -ENOMEM;
639 break;
640 }
audetto@tiscali.it499c1862006-11-20 18:27:44 -0300641 memset(&fbuf2, 0, sizeof(fbuf2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 set_v4l_control(inode, file,
644 V4L2_CID_BRIGHTNESS, pict->brightness, drv);
645 set_v4l_control(inode, file,
646 V4L2_CID_HUE, pict->hue, drv);
647 set_v4l_control(inode, file,
648 V4L2_CID_CONTRAST, pict->contrast, drv);
649 set_v4l_control(inode, file,
650 V4L2_CID_SATURATION, pict->colour, drv);
651 set_v4l_control(inode, file,
652 V4L2_CID_WHITENESS, pict->whiteness, drv);
Trent Piephobbe24862007-04-27 22:56:29 -0300653 /*
654 * V4L1 uses this ioctl to set both memory capture and overlay
655 * pixel format, while V4L2 has two different ioctls for this.
656 * Some cards may not support one or the other, and may support
657 * different pixel formats for memory vs overlay.
658 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
661 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
Trent Piephobbe24862007-04-27 22:56:29 -0300662 /* If VIDIOC_G_FMT failed, then the driver likely doesn't
663 support memory capture. Trying to set the memory capture
664 parameters would be pointless. */
665 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300666 dprintk("VIDIOCSPICT / VIDIOC_G_FMT: %d\n", err);
Trent Piephobbe24862007-04-27 22:56:29 -0300667 mem_err = -1000; /* didn't even try */
668 } else if (fmt2->fmt.pix.pixelformat !=
669 palette_to_pixelformat(pict->palette)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 fmt2->fmt.pix.pixelformat = palette_to_pixelformat(
671 pict->palette);
Trent Piephobbe24862007-04-27 22:56:29 -0300672 mem_err = drv(inode, file, VIDIOC_S_FMT, fmt2);
673 if (mem_err < 0)
674 dprintk("VIDIOCSPICT / VIDIOC_S_FMT: %d\n",
675 mem_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
678 err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
Trent Piephobbe24862007-04-27 22:56:29 -0300679 /* If VIDIOC_G_FBUF failed, then the driver likely doesn't
680 support overlay. Trying to set the overlay parameters
681 would be quite pointless. */
682 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300683 dprintk("VIDIOCSPICT / VIDIOC_G_FBUF: %d\n", err);
Trent Piephobbe24862007-04-27 22:56:29 -0300684 ovl_err = -1000; /* didn't even try */
685 } else if (fbuf2.fmt.pixelformat !=
686 palette_to_pixelformat(pict->palette)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 fbuf2.fmt.pixelformat = palette_to_pixelformat(
688 pict->palette);
Trent Piephobbe24862007-04-27 22:56:29 -0300689 ovl_err = drv(inode, file, VIDIOC_S_FBUF, &fbuf2);
690 if (ovl_err < 0)
691 dprintk("VIDIOCSPICT / VIDIOC_S_FBUF: %d\n",
692 ovl_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300694 if (ovl_err < 0 && mem_err < 0) {
Trent Piephobbe24862007-04-27 22:56:29 -0300695 /* ioctl failed, couldn't set either parameter */
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300696 if (mem_err != -1000)
697 err = mem_err;
698 else if (ovl_err == -EPERM)
699 err = 0;
700 else
701 err = ovl_err;
702 } else
Trent Piephobbe24862007-04-27 22:56:29 -0300703 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
705 }
706 case VIDIOCGTUNER: /* get tuner information */
707 {
708 struct video_tuner *tun = arg;
709
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300710 memset(&tun2, 0, sizeof(tun2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
712 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300713 dprintk("VIDIOCGTUNER / VIDIOC_G_TUNER: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 break;
715 }
716 memcpy(tun->name, tun2.name,
717 min(sizeof(tun->name), sizeof(tun2.name)));
718 tun->name[sizeof(tun->name) - 1] = 0;
719 tun->rangelow = tun2.rangelow;
720 tun->rangehigh = tun2.rangehigh;
721 tun->flags = 0;
722 tun->mode = VIDEO_MODE_AUTO;
723
724 for (i = 0; i < 64; i++) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300725 memset(&std2, 0, sizeof(std2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 std2.index = i;
727 if (0 != drv(inode, file, VIDIOC_ENUMSTD, &std2))
728 break;
729 if (std2.id & V4L2_STD_PAL)
730 tun->flags |= VIDEO_TUNER_PAL;
731 if (std2.id & V4L2_STD_NTSC)
732 tun->flags |= VIDEO_TUNER_NTSC;
733 if (std2.id & V4L2_STD_SECAM)
734 tun->flags |= VIDEO_TUNER_SECAM;
735 }
736
737 err = drv(inode, file, VIDIOC_G_STD, &sid);
738 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300739 dprintk("VIDIOCGTUNER / VIDIOC_G_STD: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (err == 0) {
741 if (sid & V4L2_STD_PAL)
742 tun->mode = VIDEO_MODE_PAL;
743 if (sid & V4L2_STD_NTSC)
744 tun->mode = VIDEO_MODE_NTSC;
745 if (sid & V4L2_STD_SECAM)
746 tun->mode = VIDEO_MODE_SECAM;
747 }
748
749 if (tun2.capability & V4L2_TUNER_CAP_LOW)
750 tun->flags |= VIDEO_TUNER_LOW;
751 if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
752 tun->flags |= VIDEO_TUNER_STEREO_ON;
753 tun->signal = tun2.signal;
754 break;
755 }
756 case VIDIOCSTUNER: /* select a tuner input */
757 {
Mauro Carvalho Chehab2aa92ff2006-11-20 12:10:04 -0300758 struct video_tuner *tun = arg;
759 struct v4l2_tuner t;
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300760 memset(&t, 0, sizeof(t));
Mauro Carvalho Chehab2aa92ff2006-11-20 12:10:04 -0300761
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300762 t.index = tun->tuner;
Mauro Carvalho Chehab2aa92ff2006-11-20 12:10:04 -0300763
764 err = drv(inode, file, VIDIOC_S_INPUT, &t);
765 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300766 dprintk("VIDIOCSTUNER / VIDIOC_S_INPUT: %d\n", err);
Mauro Carvalho Chehab2aa92ff2006-11-20 12:10:04 -0300767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 break;
769 }
770 case VIDIOCGFREQ: /* get frequency */
771 {
Hans Verkuil376f2692005-11-08 21:37:15 -0800772 unsigned long *freq = arg;
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300773 memset(&freq2, 0, sizeof(freq2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 freq2.tuner = 0;
776 err = drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
777 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300778 dprintk("VIDIOCGFREQ / VIDIOC_G_FREQUENCY: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (0 == err)
780 *freq = freq2.frequency;
781 break;
782 }
783 case VIDIOCSFREQ: /* set frequency */
784 {
Hans Verkuil376f2692005-11-08 21:37:15 -0800785 unsigned long *freq = arg;
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300786 memset(&freq2, 0, sizeof(freq2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
789 freq2.frequency = *freq;
790 err = drv(inode, file, VIDIOC_S_FREQUENCY, &freq2);
791 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300792 dprintk("VIDIOCSFREQ / VIDIOC_S_FREQUENCY: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 break;
794 }
795 case VIDIOCGAUDIO: /* get audio properties/controls */
796 {
797 struct video_audio *aud = arg;
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300798 memset(&aud2, 0, sizeof(aud2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 err = drv(inode, file, VIDIOC_G_AUDIO, &aud2);
801 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300802 dprintk("VIDIOCGAUDIO / VIDIOC_G_AUDIO: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 break;
804 }
805 memcpy(aud->name, aud2.name,
806 min(sizeof(aud->name), sizeof(aud2.name)));
807 aud->name[sizeof(aud->name) - 1] = 0;
808 aud->audio = aud2.index;
809 aud->flags = 0;
810 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME, drv);
811 if (i >= 0) {
812 aud->volume = i;
813 aud->flags |= VIDEO_AUDIO_VOLUME;
814 }
815 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BASS, drv);
816 if (i >= 0) {
817 aud->bass = i;
818 aud->flags |= VIDEO_AUDIO_BASS;
819 }
820 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE, drv);
821 if (i >= 0) {
822 aud->treble = i;
823 aud->flags |= VIDEO_AUDIO_TREBLE;
824 }
825 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE, drv);
826 if (i >= 0) {
827 aud->balance = i;
828 aud->flags |= VIDEO_AUDIO_BALANCE;
829 }
830 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE, drv);
831 if (i >= 0) {
832 if (i)
833 aud->flags |= VIDEO_AUDIO_MUTE;
834 aud->flags |= VIDEO_AUDIO_MUTABLE;
835 }
836 aud->step = 1;
837 qctrl2.id = V4L2_CID_AUDIO_VOLUME;
838 if (drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2) == 0 &&
839 !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
840 aud->step = qctrl2.step;
841 aud->mode = 0;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700842
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300843 memset(&tun2, 0, sizeof(tun2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
845 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300846 dprintk("VIDIOCGAUDIO / VIDIOC_G_TUNER: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 err = 0;
848 break;
849 }
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (tun2.rxsubchans & V4L2_TUNER_SUB_LANG2)
852 aud->mode = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
853 else if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
854 aud->mode = VIDEO_SOUND_STEREO;
855 else if (tun2.rxsubchans & V4L2_TUNER_SUB_MONO)
856 aud->mode = VIDEO_SOUND_MONO;
857 break;
858 }
859 case VIDIOCSAUDIO: /* set audio controls */
860 {
861 struct video_audio *aud = arg;
862
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300863 memset(&aud2, 0, sizeof(aud2));
864 memset(&tun2, 0, sizeof(tun2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 aud2.index = aud->audio;
867 err = drv(inode, file, VIDIOC_S_AUDIO, &aud2);
868 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300869 dprintk("VIDIOCSAUDIO / VIDIOC_S_AUDIO: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 break;
871 }
872
873 set_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME,
874 aud->volume, drv);
875 set_v4l_control(inode, file, V4L2_CID_AUDIO_BASS,
876 aud->bass, drv);
877 set_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE,
878 aud->treble, drv);
879 set_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE,
880 aud->balance, drv);
881 set_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE,
882 !!(aud->flags & VIDEO_AUDIO_MUTE), drv);
883
884 err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
885 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300886 dprintk("VIDIOCSAUDIO / VIDIOC_G_TUNER: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (err == 0) {
888 switch (aud->mode) {
889 default:
890 case VIDEO_SOUND_MONO:
891 case VIDEO_SOUND_LANG1:
892 tun2.audmode = V4L2_TUNER_MODE_MONO;
893 break;
894 case VIDEO_SOUND_STEREO:
895 tun2.audmode = V4L2_TUNER_MODE_STEREO;
896 break;
897 case VIDEO_SOUND_LANG2:
898 tun2.audmode = V4L2_TUNER_MODE_LANG2;
899 break;
900 }
901 err = drv(inode, file, VIDIOC_S_TUNER, &tun2);
902 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300903 dprintk("VIDIOCSAUDIO / VIDIOC_S_TUNER: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905 err = 0;
906 break;
907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 case VIDIOCMCAPTURE: /* capture a frame */
909 {
910 struct video_mmap *mm = arg;
911
Cyrill Gorcunovc77990e2008-03-05 20:24:43 -0300912 fmt2 = kzalloc(sizeof(*fmt2), GFP_KERNEL);
913 if (!fmt2) {
914 err = -ENOMEM;
915 break;
916 }
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300917 memset(&buf2, 0, sizeof(buf2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
920 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
921 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300922 dprintk("VIDIOCMCAPTURE / VIDIOC_G_FMT: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 break;
924 }
925 if (mm->width != fmt2->fmt.pix.width ||
926 mm->height != fmt2->fmt.pix.height ||
927 palette_to_pixelformat(mm->format) !=
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300928 fmt2->fmt.pix.pixelformat) {
929 /* New capture format... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 fmt2->fmt.pix.width = mm->width;
931 fmt2->fmt.pix.height = mm->height;
932 fmt2->fmt.pix.pixelformat =
933 palette_to_pixelformat(mm->format);
934 fmt2->fmt.pix.field = V4L2_FIELD_ANY;
935 fmt2->fmt.pix.bytesperline = 0;
936 err = drv(inode, file, VIDIOC_S_FMT, fmt2);
937 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300938 dprintk("VIDIOCMCAPTURE / VIDIOC_S_FMT: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 break;
940 }
941 }
942 buf2.index = mm->frame;
943 buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
944 err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
945 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300946 dprintk("VIDIOCMCAPTURE / VIDIOC_QUERYBUF: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 break;
948 }
949 err = drv(inode, file, VIDIOC_QBUF, &buf2);
950 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300951 dprintk("VIDIOCMCAPTURE / VIDIOC_QBUF: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 break;
953 }
954 err = drv(inode, file, VIDIOC_STREAMON, &captype);
955 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300956 dprintk("VIDIOCMCAPTURE / VIDIOC_STREAMON: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 break;
958 }
959 case VIDIOCSYNC: /* wait for a frame */
960 {
961 int *i = arg;
962
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300963 memset(&buf2, 0, sizeof(buf2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 buf2.index = *i;
965 buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
966 err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
967 if (err < 0) {
968 /* No such buffer */
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300969 dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 break;
971 }
972 if (!(buf2.flags & V4L2_BUF_FLAG_MAPPED)) {
973 /* Buffer is not mapped */
974 err = -EINVAL;
975 break;
976 }
977
978 /* make sure capture actually runs so we don't block forever */
979 err = drv(inode, file, VIDIOC_STREAMON, &captype);
980 if (err < 0) {
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300981 dprintk("VIDIOCSYNC / VIDIOC_STREAMON: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 break;
983 }
984
985 /* Loop as long as the buffer is queued, but not done */
986 while ((buf2.flags &
987 (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300988 == V4L2_BUF_FLAG_QUEUED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 err = poll_one(file);
990 if (err < 0 || /* error or sleep was interrupted */
991 err == 0) /* timeout? Shouldn't occur. */
992 break;
993 err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
994 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -0300995 dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 if (!(buf2.flags & V4L2_BUF_FLAG_DONE)) /* not done */
998 break;
999 do {
1000 err = drv(inode, file, VIDIOC_DQBUF, &buf2);
1001 if (err < 0)
Marcin Slusarz8b3b90a2008-04-22 14:45:57 -03001002 dprintk("VIDIOCSYNC / VIDIOC_DQBUF: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 } while (err == 0 && buf2.index != *i);
1004 break;
1005 }
1006
1007 case VIDIOCGVBIFMT: /* query VBI data capture format */
1008 {
1009 struct vbi_format *fmt = arg;
1010
Cyrill Gorcunovc77990e2008-03-05 20:24:43 -03001011 fmt2 = kzalloc(sizeof(*fmt2), GFP_KERNEL);
1012 if (!fmt2) {
1013 err = -ENOMEM;
1014 break;
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1017
1018 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
1019 if (err < 0) {
1020 dprintk("VIDIOCGVBIFMT / VIDIOC_G_FMT: %d\n", err);
1021 break;
1022 }
Michael H. Schimek67f15702006-01-09 15:25:27 -02001023 if (fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY) {
1024 err = -EINVAL;
1025 break;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 memset(fmt, 0, sizeof(*fmt));
1028 fmt->samples_per_line = fmt2->fmt.vbi.samples_per_line;
1029 fmt->sampling_rate = fmt2->fmt.vbi.sampling_rate;
1030 fmt->sample_format = VIDEO_PALETTE_RAW;
1031 fmt->start[0] = fmt2->fmt.vbi.start[0];
1032 fmt->count[0] = fmt2->fmt.vbi.count[0];
1033 fmt->start[1] = fmt2->fmt.vbi.start[1];
1034 fmt->count[1] = fmt2->fmt.vbi.count[1];
1035 fmt->flags = fmt2->fmt.vbi.flags & 0x03;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038 case VIDIOCSVBIFMT:
1039 {
1040 struct vbi_format *fmt = arg;
1041
Michael H. Schimek67f15702006-01-09 15:25:27 -02001042 if (VIDEO_PALETTE_RAW != fmt->sample_format) {
1043 err = -EINVAL;
1044 break;
1045 }
1046
Cyrill Gorcunovc77990e2008-03-05 20:24:43 -03001047 fmt2 = kzalloc(sizeof(*fmt2), GFP_KERNEL);
1048 if (!fmt2) {
1049 err = -ENOMEM;
1050 break;
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1053 fmt2->fmt.vbi.samples_per_line = fmt->samples_per_line;
1054 fmt2->fmt.vbi.sampling_rate = fmt->sampling_rate;
1055 fmt2->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1056 fmt2->fmt.vbi.start[0] = fmt->start[0];
1057 fmt2->fmt.vbi.count[0] = fmt->count[0];
1058 fmt2->fmt.vbi.start[1] = fmt->start[1];
1059 fmt2->fmt.vbi.count[1] = fmt->count[1];
1060 fmt2->fmt.vbi.flags = fmt->flags;
1061 err = drv(inode, file, VIDIOC_TRY_FMT, fmt2);
1062 if (err < 0) {
1063 dprintk("VIDIOCSVBIFMT / VIDIOC_TRY_FMT: %d\n", err);
1064 break;
1065 }
1066
1067 if (fmt2->fmt.vbi.samples_per_line != fmt->samples_per_line ||
1068 fmt2->fmt.vbi.sampling_rate != fmt->sampling_rate ||
Michael H. Schimek67f15702006-01-09 15:25:27 -02001069 fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 fmt2->fmt.vbi.start[0] != fmt->start[0] ||
1071 fmt2->fmt.vbi.count[0] != fmt->count[0] ||
1072 fmt2->fmt.vbi.start[1] != fmt->start[1] ||
1073 fmt2->fmt.vbi.count[1] != fmt->count[1] ||
1074 fmt2->fmt.vbi.flags != fmt->flags) {
1075 err = -EINVAL;
1076 break;
1077 }
1078 err = drv(inode, file, VIDIOC_S_FMT, fmt2);
1079 if (err < 0)
1080 dprintk("VIDIOCSVBIFMT / VIDIOC_S_FMT: %d\n", err);
1081 break;
1082 }
1083
1084 default:
1085 err = -ENOIOCTLCMD;
1086 break;
1087 }
1088
Jesper Juhl2ea75332005-11-07 01:01:31 -08001089 kfree(cap2);
1090 kfree(fmt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return err;
1092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093EXPORT_SYMBOL(v4l_compat_translate_ioctl);
1094
1095/*
1096 * Local variables:
1097 * c-basic-offset: 8
1098 * End:
1099 */