V4L/DVB (4068): Removed all references to kernel stuff from videodev.h and videodev2.h
[linux-2.6.git] / drivers / media / video / v4l1-compat.c
1 /*
2  *
3  *      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  *
14  * Author:      Bill Dirks <bdirks@pacbell.net>
15  *              et al.
16  *
17  */
18
19 #include <linux/config.h>
20
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/smp_lock.h>
28 #include <linux/mm.h>
29 #include <linux/fs.h>
30 #include <linux/file.h>
31 #include <linux/string.h>
32 #include <linux/errno.h>
33 #include <linux/slab.h>
34 #include <linux/videodev.h>
35 #include <media/v4l2-common.h>
36
37 #include <asm/uaccess.h>
38 #include <asm/system.h>
39 #include <asm/pgtable.h>
40
41 #ifdef CONFIG_KMOD
42 #include <linux/kmod.h>
43 #endif
44
45 static unsigned int debug  = 0;
46 module_param(debug, int, 0644);
47 MODULE_PARM_DESC(debug,"enable debug messages");
48 MODULE_AUTHOR("Bill Dirks");
49 MODULE_DESCRIPTION("v4l(1) compatibility layer for v4l2 drivers.");
50 MODULE_LICENSE("GPL");
51
52 #define dprintk(fmt, arg...)    if (debug) \
53         printk(KERN_DEBUG "v4l1-compat: " fmt , ## arg)
54
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
62 static int
63 get_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)
75                 dprintk("VIDIOC_QUERYCTRL: %d\n",err);
76         if (err == 0 &&
77             !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
78         {
79                 ctrl2.id = qctrl2.id;
80                 err = drv(inode, file, VIDIOC_G_CTRL, &ctrl2);
81                 if (err < 0) {
82                         dprintk("VIDIOC_G_CTRL: %d\n",err);
83                         return 0;
84                 }
85                 return ((ctrl2.value - qctrl2.minimum) * 65535
86                          + (qctrl2.maximum - qctrl2.minimum) / 2)
87                         / (qctrl2.maximum - qctrl2.minimum);
88         }
89         return 0;
90 }
91
92 static int
93 set_v4l_control(struct inode            *inode,
94                 struct file             *file,
95                 int                     cid,
96                 int                     value,
97                 v4l2_kioctl             drv)
98 {
99         struct v4l2_queryctrl   qctrl2;
100         struct v4l2_control     ctrl2;
101         int                     err;
102
103         qctrl2.id = cid;
104         err = drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2);
105         if (err < 0)
106                 dprintk("VIDIOC_QUERYCTRL: %d\n",err);
107         if (err == 0 &&
108             !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED) &&
109             !(qctrl2.flags & V4L2_CTRL_FLAG_GRABBED))
110         {
111                 if (value < 0)
112                         value = 0;
113                 if (value > 65535)
114                         value = 65535;
115                 if (value && qctrl2.type == V4L2_CTRL_TYPE_BOOLEAN)
116                         value = 65535;
117                 ctrl2.id = qctrl2.id;
118                 ctrl2.value =
119                         (value * (qctrl2.maximum - qctrl2.minimum)
120                          + 32767)
121                         / 65535;
122                 ctrl2.value += qctrl2.minimum;
123                 err = drv(inode, file, VIDIOC_S_CTRL, &ctrl2);
124                 if (err < 0)
125                         dprintk("VIDIOC_S_CTRL: %d\n",err);
126         }
127         return 0;
128 }
129
130 /* ----------------------------------------------------------------- */
131
132 static int palette2pixelformat[] = {
133         [VIDEO_PALETTE_GREY]    = V4L2_PIX_FMT_GREY,
134         [VIDEO_PALETTE_RGB555]  = V4L2_PIX_FMT_RGB555,
135         [VIDEO_PALETTE_RGB565]  = V4L2_PIX_FMT_RGB565,
136         [VIDEO_PALETTE_RGB24]   = V4L2_PIX_FMT_BGR24,
137         [VIDEO_PALETTE_RGB32]   = V4L2_PIX_FMT_BGR32,
138         /* yuv packed pixel */
139         [VIDEO_PALETTE_YUYV]    = V4L2_PIX_FMT_YUYV,
140         [VIDEO_PALETTE_YUV422]  = V4L2_PIX_FMT_YUYV,
141         [VIDEO_PALETTE_UYVY]    = V4L2_PIX_FMT_UYVY,
142         /* yuv planar */
143         [VIDEO_PALETTE_YUV410P] = V4L2_PIX_FMT_YUV410,
144         [VIDEO_PALETTE_YUV420]  = V4L2_PIX_FMT_YUV420,
145         [VIDEO_PALETTE_YUV420P] = V4L2_PIX_FMT_YUV420,
146         [VIDEO_PALETTE_YUV411P] = V4L2_PIX_FMT_YUV411P,
147         [VIDEO_PALETTE_YUV422P] = V4L2_PIX_FMT_YUV422P,
148 };
149
150 static unsigned int
151 palette_to_pixelformat(unsigned int palette)
152 {
153         if (palette < ARRAY_SIZE(palette2pixelformat))
154                 return palette2pixelformat[palette];
155         else
156                 return 0;
157 }
158
159 static unsigned int
160 pixelformat_to_palette(int pixelformat)
161 {
162         int     palette = 0;
163         switch (pixelformat)
164         {
165         case V4L2_PIX_FMT_GREY:
166                 palette = VIDEO_PALETTE_GREY;
167                 break;
168         case V4L2_PIX_FMT_RGB555:
169                 palette = VIDEO_PALETTE_RGB555;
170                 break;
171         case V4L2_PIX_FMT_RGB565:
172                 palette = VIDEO_PALETTE_RGB565;
173                 break;
174         case V4L2_PIX_FMT_BGR24:
175                 palette = VIDEO_PALETTE_RGB24;
176                 break;
177         case V4L2_PIX_FMT_BGR32:
178                 palette = VIDEO_PALETTE_RGB32;
179                 break;
180         /* yuv packed pixel */
181         case V4L2_PIX_FMT_YUYV:
182                 palette = VIDEO_PALETTE_YUYV;
183                 break;
184         case V4L2_PIX_FMT_UYVY:
185                 palette = VIDEO_PALETTE_UYVY;
186                 break;
187         /* yuv planar */
188         case V4L2_PIX_FMT_YUV410:
189                 palette = VIDEO_PALETTE_YUV420;
190                 break;
191         case V4L2_PIX_FMT_YUV420:
192                 palette = VIDEO_PALETTE_YUV420;
193                 break;
194         case V4L2_PIX_FMT_YUV411P:
195                 palette = VIDEO_PALETTE_YUV411P;
196                 break;
197         case V4L2_PIX_FMT_YUV422P:
198                 palette = VIDEO_PALETTE_YUV422P;
199                 break;
200         }
201         return palette;
202 }
203
204 /* ----------------------------------------------------------------- */
205
206 static int poll_one(struct file *file)
207 {
208         int retval = 1;
209         poll_table *table;
210         struct poll_wqueues pwq;
211
212         poll_initwait(&pwq);
213         table = &pwq.pt;
214         for (;;) {
215                 int mask;
216                 set_current_state(TASK_INTERRUPTIBLE);
217                 mask = file->f_op->poll(file, table);
218                 if (mask & POLLIN)
219                         break;
220                 table = NULL;
221                 if (signal_pending(current)) {
222                         retval = -ERESTARTSYS;
223                         break;
224                 }
225                 schedule();
226         }
227         set_current_state(TASK_RUNNING);
228         poll_freewait(&pwq);
229         return retval;
230 }
231
232 static int count_inputs(struct inode         *inode,
233                         struct file          *file,
234                         v4l2_kioctl          drv)
235 {
236         struct v4l2_input input2;
237         int i;
238
239         for (i = 0;; i++) {
240                 memset(&input2,0,sizeof(input2));
241                 input2.index = i;
242                 if (0 != drv(inode,file,VIDIOC_ENUMINPUT, &input2))
243                         break;
244         }
245         return i;
246 }
247
248 static int check_size(struct inode         *inode,
249                       struct file          *file,
250                       v4l2_kioctl          drv,
251                       int *maxw, int *maxh)
252 {
253         struct v4l2_fmtdesc desc2;
254         struct v4l2_format  fmt2;
255
256         memset(&desc2,0,sizeof(desc2));
257         memset(&fmt2,0,sizeof(fmt2));
258
259         desc2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
260         if (0 != drv(inode,file,VIDIOC_ENUM_FMT, &desc2))
261                 goto done;
262
263         fmt2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
264         fmt2.fmt.pix.width       = 10000;
265         fmt2.fmt.pix.height      = 10000;
266         fmt2.fmt.pix.pixelformat = desc2.pixelformat;
267         if (0 != drv(inode,file,VIDIOC_TRY_FMT, &fmt2))
268                 goto done;
269
270         *maxw = fmt2.fmt.pix.width;
271         *maxh = fmt2.fmt.pix.height;
272
273  done:
274         return 0;
275 }
276
277 /* ----------------------------------------------------------------- */
278
279 /*
280  *      This function is exported.
281  */
282 int
283 v4l_compat_translate_ioctl(struct inode         *inode,
284                            struct file          *file,
285                            int                  cmd,
286                            void                 *arg,
287                            v4l2_kioctl          drv)
288 {
289         struct v4l2_capability  *cap2 = NULL;
290         struct v4l2_format      *fmt2 = NULL;
291         enum v4l2_buf_type      captype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
292
293         struct v4l2_framebuffer fbuf2;
294         struct v4l2_input       input2;
295         struct v4l2_tuner       tun2;
296         struct v4l2_standard    std2;
297         struct v4l2_frequency   freq2;
298         struct v4l2_audio       aud2;
299         struct v4l2_queryctrl   qctrl2;
300         struct v4l2_buffer      buf2;
301         v4l2_std_id             sid;
302         int i, err = 0;
303
304         switch (cmd) {
305         case VIDIOCGCAP:        /* capability */
306         {
307                 struct video_capability *cap = arg;
308
309                 cap2 = kzalloc(sizeof(*cap2),GFP_KERNEL);
310                 memset(cap, 0, sizeof(*cap));
311                 memset(&fbuf2, 0, sizeof(fbuf2));
312
313                 err = drv(inode, file, VIDIOC_QUERYCAP, cap2);
314                 if (err < 0) {
315                         dprintk("VIDIOCGCAP / VIDIOC_QUERYCAP: %d\n",err);
316                         break;
317                 }
318                 if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY) {
319                         err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
320                         if (err < 0) {
321                                 dprintk("VIDIOCGCAP / VIDIOC_G_FBUF: %d\n",err);
322                                 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
341                 cap->channels  = count_inputs(inode,file,drv);
342                 check_size(inode,file,drv,
343                            &cap->maxwidth,&cap->maxheight);
344                 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
353                 err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
354                 if (err < 0) {
355                         dprintk("VIDIOCGFBUF / VIDIOC_G_FBUF: %d\n",err);
356                         break;
357                 }
358                 buffer->base   = fbuf2.base;
359                 buffer->height = fbuf2.fmt.height;
360                 buffer->width  = fbuf2.fmt.width;
361
362                 switch (fbuf2.fmt.pixelformat) {
363                 case V4L2_PIX_FMT_RGB332:
364                         buffer->depth = 8;
365                                 break;
366                 case V4L2_PIX_FMT_RGB555:
367                         buffer->depth = 15;
368                         break;
369                 case V4L2_PIX_FMT_RGB565:
370                         buffer->depth = 16;
371                         break;
372                 case V4L2_PIX_FMT_BGR24:
373                         buffer->depth = 24;
374                         break;
375                 case V4L2_PIX_FMT_BGR32:
376                         buffer->depth = 32;
377                         break;
378                 default:
379                         buffer->depth = 0;
380                 }
381                 if (0 != fbuf2.fmt.bytesperline)
382                         buffer->bytesperline = fbuf2.fmt.bytesperline;
383                 else {
384                         buffer->bytesperline =
385                                 (buffer->width * buffer->depth + 7) & 7;
386                         buffer->bytesperline >>= 3;
387                 }
388                 break;
389         }
390         case VIDIOCSFBUF: /*  set frame buffer  */
391         {
392                 struct video_buffer     *buffer = arg;
393
394                 memset(&fbuf2, 0, sizeof(fbuf2));
395                 fbuf2.base       = buffer->base;
396                 fbuf2.fmt.height = buffer->height;
397                 fbuf2.fmt.width  = buffer->width;
398                 switch (buffer->depth) {
399                 case 8:
400                         fbuf2.fmt.pixelformat = V4L2_PIX_FMT_RGB332;
401                         break;
402                 case 15:
403                         fbuf2.fmt.pixelformat = V4L2_PIX_FMT_RGB555;
404                         break;
405                 case 16:
406                         fbuf2.fmt.pixelformat = V4L2_PIX_FMT_RGB565;
407                         break;
408                 case 24:
409                         fbuf2.fmt.pixelformat = V4L2_PIX_FMT_BGR24;
410                         break;
411                 case 32:
412                         fbuf2.fmt.pixelformat = V4L2_PIX_FMT_BGR32;
413                         break;
414                 }
415                 fbuf2.fmt.bytesperline = buffer->bytesperline;
416                 err = drv(inode, file, VIDIOC_S_FBUF, &fbuf2);
417                 if (err < 0)
418                         dprintk("VIDIOCSFBUF / VIDIOC_S_FBUF: %d\n",err);
419                 break;
420         }
421         case VIDIOCGWIN: /*  get window or capture dimensions  */
422         {
423                 struct video_window     *win = arg;
424
425                 fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
426                 memset(win,0,sizeof(*win));
427
428                 fmt2->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
429                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
430                 if (err < 0)
431                         dprintk("VIDIOCGWIN / VIDIOC_G_WIN: %d\n",err);
432                 if (err == 0) {
433                         win->x         = fmt2->fmt.win.w.left;
434                         win->y         = fmt2->fmt.win.w.top;
435                         win->width     = fmt2->fmt.win.w.width;
436                         win->height    = fmt2->fmt.win.w.height;
437                         win->chromakey = fmt2->fmt.win.chromakey;
438                         win->clips     = NULL;
439                         win->clipcount = 0;
440                         break;
441                 }
442
443                 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
444                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
445                 if (err < 0) {
446                         dprintk("VIDIOCGWIN / VIDIOC_G_FMT: %d\n",err);
447                         break;
448                 }
449                 win->x         = 0;
450                 win->y         = 0;
451                 win->width     = fmt2->fmt.pix.width;
452                 win->height    = fmt2->fmt.pix.height;
453                 win->chromakey = 0;
454                 win->clips     = NULL;
455                 win->clipcount = 0;
456                 break;
457         }
458         case VIDIOCSWIN: /*  set window and/or capture dimensions  */
459         {
460                 struct video_window     *win = arg;
461                 int err1,err2;
462
463                 fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
464                 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
465                 drv(inode, file, VIDIOC_STREAMOFF, &fmt2->type);
466                 err1 = drv(inode, file, VIDIOC_G_FMT, fmt2);
467                 if (err1 < 0)
468                         dprintk("VIDIOCSWIN / VIDIOC_G_FMT: %d\n",err);
469                 if (err1 == 0) {
470                         fmt2->fmt.pix.width  = win->width;
471                         fmt2->fmt.pix.height = win->height;
472                         fmt2->fmt.pix.field  = V4L2_FIELD_ANY;
473                         fmt2->fmt.pix.bytesperline = 0;
474                         err = drv(inode, file, VIDIOC_S_FMT, fmt2);
475                         if (err < 0)
476                                 dprintk("VIDIOCSWIN / VIDIOC_S_FMT #1: %d\n",
477                                         err);
478                         win->width  = fmt2->fmt.pix.width;
479                         win->height = fmt2->fmt.pix.height;
480                 }
481
482                 memset(fmt2,0,sizeof(*fmt2));
483                 fmt2->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
484                 fmt2->fmt.win.w.left    = win->x;
485                 fmt2->fmt.win.w.top     = win->y;
486                 fmt2->fmt.win.w.width   = win->width;
487                 fmt2->fmt.win.w.height  = win->height;
488                 fmt2->fmt.win.chromakey = win->chromakey;
489                 fmt2->fmt.win.clips     = (void __user *)win->clips;
490                 fmt2->fmt.win.clipcount = win->clipcount;
491                 err2 = drv(inode, file, VIDIOC_S_FMT, fmt2);
492                 if (err2 < 0)
493                         dprintk("VIDIOCSWIN / VIDIOC_S_FMT #2: %d\n",err);
494
495                 if (err1 != 0 && err2 != 0)
496                         err = err1;
497                 break;
498         }
499         case VIDIOCCAPTURE: /*  turn on/off preview  */
500         {
501                 int *on = arg;
502
503                 if (0 == *on) {
504                         /* dirty hack time.  But v4l1 has no STREAMOFF
505                          * equivalent in the API, and this one at
506                          * least comes close ... */
507                         drv(inode, file, VIDIOC_STREAMOFF, &captype);
508                 }
509                 err = drv(inode, file, VIDIOC_OVERLAY, arg);
510                 if (err < 0)
511                         dprintk("VIDIOCCAPTURE / VIDIOC_PREVIEW: %d\n",err);
512                 break;
513         }
514         case VIDIOCGCHAN: /*  get input information  */
515         {
516                 struct video_channel    *chan = arg;
517
518                 memset(&input2,0,sizeof(input2));
519                 input2.index = chan->channel;
520                 err = drv(inode, file, VIDIOC_ENUMINPUT, &input2);
521                 if (err < 0) {
522                         dprintk("VIDIOCGCHAN / VIDIOC_ENUMINPUT: "
523                                 "channel=%d err=%d\n",chan->channel,err);
524                         break;
525                 }
526                 chan->channel = input2.index;
527                 memcpy(chan->name, input2.name,
528                        min(sizeof(chan->name), sizeof(input2.name)));
529                 chan->name[sizeof(chan->name) - 1] = 0;
530                 chan->tuners = (input2.type == V4L2_INPUT_TYPE_TUNER) ? 1 : 0;
531                 chan->flags = (chan->tuners) ? VIDEO_VC_TUNER : 0;
532                 switch (input2.type) {
533                 case V4L2_INPUT_TYPE_TUNER:
534                         chan->type = VIDEO_TYPE_TV;
535                         break;
536                 default:
537                 case V4L2_INPUT_TYPE_CAMERA:
538                         chan->type = VIDEO_TYPE_CAMERA;
539                         break;
540                 }
541                 chan->norm = 0;
542                 err = drv(inode, file, VIDIOC_G_STD, &sid);
543                 if (err < 0)
544                         dprintk("VIDIOCGCHAN / VIDIOC_G_STD: %d\n",err);
545                 if (err == 0) {
546                         if (sid & V4L2_STD_PAL)
547                                 chan->norm = VIDEO_MODE_PAL;
548                         if (sid & V4L2_STD_NTSC)
549                                 chan->norm = VIDEO_MODE_NTSC;
550                         if (sid & V4L2_STD_SECAM)
551                                 chan->norm = VIDEO_MODE_SECAM;
552                 }
553                 break;
554         }
555         case VIDIOCSCHAN: /*  set input  */
556         {
557                 struct video_channel *chan = arg;
558
559                 sid = 0;
560                 err = drv(inode, file, VIDIOC_S_INPUT, &chan->channel);
561                 if (err < 0)
562                         dprintk("VIDIOCSCHAN / VIDIOC_S_INPUT: %d\n",err);
563                 switch (chan->norm) {
564                 case VIDEO_MODE_PAL:
565                         sid = V4L2_STD_PAL;
566                         break;
567                 case VIDEO_MODE_NTSC:
568                         sid = V4L2_STD_NTSC;
569                         break;
570                 case VIDEO_MODE_SECAM:
571                         sid = V4L2_STD_SECAM;
572                         break;
573                 }
574                 if (0 != sid) {
575                         err = drv(inode, file, VIDIOC_S_STD, &sid);
576                         if (err < 0)
577                                 dprintk("VIDIOCSCHAN / VIDIOC_S_STD: %d\n",err);
578                 }
579                 break;
580         }
581         case VIDIOCGPICT: /*  get tone controls & partial capture format  */
582         {
583                 struct video_picture    *pict = arg;
584
585                 pict->brightness = get_v4l_control(inode, file,
586                                                    V4L2_CID_BRIGHTNESS,drv);
587                 pict->hue = get_v4l_control(inode, file,
588                                             V4L2_CID_HUE, drv);
589                 pict->contrast = get_v4l_control(inode, file,
590                                                  V4L2_CID_CONTRAST, drv);
591                 pict->colour = get_v4l_control(inode, file,
592                                                V4L2_CID_SATURATION, drv);
593                 pict->whiteness = get_v4l_control(inode, file,
594                                                   V4L2_CID_WHITENESS, drv);
595
596                 fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
597                 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
598                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
599                 if (err < 0) {
600                         dprintk("VIDIOCGPICT / VIDIOC_G_FMT: %d\n",err);
601                         break;
602                 }
603                 pict->palette = pixelformat_to_palette(
604                         fmt2->fmt.pix.pixelformat);
605                 break;
606         }
607         case VIDIOCSPICT: /*  set tone controls & partial capture format  */
608         {
609                 struct video_picture    *pict = arg;
610
611                 set_v4l_control(inode, file,
612                                 V4L2_CID_BRIGHTNESS, pict->brightness, drv);
613                 set_v4l_control(inode, file,
614                                 V4L2_CID_HUE, pict->hue, drv);
615                 set_v4l_control(inode, file,
616                                 V4L2_CID_CONTRAST, pict->contrast, drv);
617                 set_v4l_control(inode, file,
618                                 V4L2_CID_SATURATION, pict->colour, drv);
619                 set_v4l_control(inode, file,
620                                 V4L2_CID_WHITENESS, pict->whiteness, drv);
621
622                 fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
623                 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
624                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
625                 if (err < 0)
626                         dprintk("VIDIOCSPICT / VIDIOC_G_FMT: %d\n",err);
627                 if (fmt2->fmt.pix.pixelformat !=
628                     palette_to_pixelformat(pict->palette)) {
629                         fmt2->fmt.pix.pixelformat = palette_to_pixelformat(
630                                 pict->palette);
631                         err = drv(inode, file, VIDIOC_S_FMT, fmt2);
632                         if (err < 0)
633                                 dprintk("VIDIOCSPICT / VIDIOC_S_FMT: %d\n",err);
634                 }
635
636                 err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
637                 if (err < 0)
638                         dprintk("VIDIOCSPICT / VIDIOC_G_FBUF: %d\n",err);
639                 if (fbuf2.fmt.pixelformat !=
640                     palette_to_pixelformat(pict->palette)) {
641                         fbuf2.fmt.pixelformat = palette_to_pixelformat(
642                                 pict->palette);
643                         err = drv(inode, file, VIDIOC_S_FBUF, &fbuf2);
644                         if (err < 0)
645                                 dprintk("VIDIOCSPICT / VIDIOC_S_FBUF: %d\n",err);
646                         err = 0; /* likely fails for non-root */
647                 }
648                 break;
649         }
650         case VIDIOCGTUNER: /*  get tuner information  */
651         {
652                 struct video_tuner      *tun = arg;
653
654                 memset(&tun2,0,sizeof(tun2));
655                 err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
656                 if (err < 0) {
657                         dprintk("VIDIOCGTUNER / VIDIOC_G_TUNER: %d\n",err);
658                         break;
659                 }
660                 memcpy(tun->name, tun2.name,
661                        min(sizeof(tun->name), sizeof(tun2.name)));
662                 tun->name[sizeof(tun->name) - 1] = 0;
663                 tun->rangelow = tun2.rangelow;
664                 tun->rangehigh = tun2.rangehigh;
665                 tun->flags = 0;
666                 tun->mode = VIDEO_MODE_AUTO;
667
668                 for (i = 0; i < 64; i++) {
669                         memset(&std2,0,sizeof(std2));
670                         std2.index = i;
671                         if (0 != drv(inode, file, VIDIOC_ENUMSTD, &std2))
672                                 break;
673                         if (std2.id & V4L2_STD_PAL)
674                                 tun->flags |= VIDEO_TUNER_PAL;
675                         if (std2.id & V4L2_STD_NTSC)
676                                 tun->flags |= VIDEO_TUNER_NTSC;
677                         if (std2.id & V4L2_STD_SECAM)
678                                 tun->flags |= VIDEO_TUNER_SECAM;
679                 }
680
681                 err = drv(inode, file, VIDIOC_G_STD, &sid);
682                 if (err < 0)
683                         dprintk("VIDIOCGTUNER / VIDIOC_G_STD: %d\n",err);
684                 if (err == 0) {
685                         if (sid & V4L2_STD_PAL)
686                                 tun->mode = VIDEO_MODE_PAL;
687                         if (sid & V4L2_STD_NTSC)
688                                 tun->mode = VIDEO_MODE_NTSC;
689                         if (sid & V4L2_STD_SECAM)
690                                 tun->mode = VIDEO_MODE_SECAM;
691                 }
692
693                 if (tun2.capability & V4L2_TUNER_CAP_LOW)
694                         tun->flags |= VIDEO_TUNER_LOW;
695                 if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
696                         tun->flags |= VIDEO_TUNER_STEREO_ON;
697                 tun->signal = tun2.signal;
698                 break;
699         }
700         case VIDIOCSTUNER: /*  select a tuner input  */
701         {
702                 err = 0;
703                 break;
704         }
705         case VIDIOCGFREQ: /*  get frequency  */
706         {
707                 unsigned long *freq = arg;
708
709                 freq2.tuner = 0;
710                 err = drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
711                 if (err < 0)
712                         dprintk("VIDIOCGFREQ / VIDIOC_G_FREQUENCY: %d\n",err);
713                 if (0 == err)
714                         *freq = freq2.frequency;
715                 break;
716         }
717         case VIDIOCSFREQ: /*  set frequency  */
718         {
719                 unsigned long *freq = arg;
720
721                 freq2.tuner = 0;
722                 drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
723                 freq2.frequency = *freq;
724                 err = drv(inode, file, VIDIOC_S_FREQUENCY, &freq2);
725                 if (err < 0)
726                         dprintk("VIDIOCSFREQ / VIDIOC_S_FREQUENCY: %d\n",err);
727                 break;
728         }
729         case VIDIOCGAUDIO: /*  get audio properties/controls  */
730         {
731                 struct video_audio      *aud = arg;
732
733                 err = drv(inode, file, VIDIOC_G_AUDIO, &aud2);
734                 if (err < 0) {
735                         dprintk("VIDIOCGAUDIO / VIDIOC_G_AUDIO: %d\n",err);
736                         break;
737                 }
738                 memcpy(aud->name, aud2.name,
739                        min(sizeof(aud->name), sizeof(aud2.name)));
740                 aud->name[sizeof(aud->name) - 1] = 0;
741                 aud->audio = aud2.index;
742                 aud->flags = 0;
743                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME, drv);
744                 if (i >= 0) {
745                         aud->volume = i;
746                         aud->flags |= VIDEO_AUDIO_VOLUME;
747                 }
748                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BASS, drv);
749                 if (i >= 0) {
750                         aud->bass = i;
751                         aud->flags |= VIDEO_AUDIO_BASS;
752                 }
753                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE, drv);
754                 if (i >= 0) {
755                         aud->treble = i;
756                         aud->flags |= VIDEO_AUDIO_TREBLE;
757                 }
758                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE, drv);
759                 if (i >= 0) {
760                         aud->balance = i;
761                         aud->flags |= VIDEO_AUDIO_BALANCE;
762                 }
763                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE, drv);
764                 if (i >= 0) {
765                         if (i)
766                                 aud->flags |= VIDEO_AUDIO_MUTE;
767                         aud->flags |= VIDEO_AUDIO_MUTABLE;
768                 }
769                 aud->step = 1;
770                 qctrl2.id = V4L2_CID_AUDIO_VOLUME;
771                 if (drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2) == 0 &&
772                     !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
773                         aud->step = qctrl2.step;
774                 aud->mode = 0;
775
776                 memset(&tun2,0,sizeof(tun2));
777                 err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
778                 if (err < 0) {
779                         dprintk("VIDIOCGAUDIO / VIDIOC_G_TUNER: %d\n",err);
780                         err = 0;
781                         break;
782                 }
783
784                 if (tun2.rxsubchans & V4L2_TUNER_SUB_LANG2)
785                         aud->mode = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
786                 else if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
787                         aud->mode = VIDEO_SOUND_STEREO;
788                 else if (tun2.rxsubchans & V4L2_TUNER_SUB_MONO)
789                         aud->mode = VIDEO_SOUND_MONO;
790                 break;
791         }
792         case VIDIOCSAUDIO: /*  set audio controls  */
793         {
794                 struct video_audio      *aud = arg;
795
796                 memset(&aud2,0,sizeof(aud2));
797                 memset(&tun2,0,sizeof(tun2));
798
799                 aud2.index = aud->audio;
800                 err = drv(inode, file, VIDIOC_S_AUDIO, &aud2);
801                 if (err < 0) {
802                         dprintk("VIDIOCSAUDIO / VIDIOC_S_AUDIO: %d\n",err);
803                         break;
804                 }
805
806                 set_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME,
807                                 aud->volume, drv);
808                 set_v4l_control(inode, file, V4L2_CID_AUDIO_BASS,
809                                 aud->bass, drv);
810                 set_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE,
811                                 aud->treble, drv);
812                 set_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE,
813                                 aud->balance, drv);
814                 set_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE,
815                                 !!(aud->flags & VIDEO_AUDIO_MUTE), drv);
816
817                 err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
818                 if (err < 0)
819                         dprintk("VIDIOCSAUDIO / VIDIOC_G_TUNER: %d\n",err);
820                 if (err == 0) {
821                         switch (aud->mode) {
822                         default:
823                         case VIDEO_SOUND_MONO:
824                         case VIDEO_SOUND_LANG1:
825                                 tun2.audmode = V4L2_TUNER_MODE_MONO;
826                                 break;
827                         case VIDEO_SOUND_STEREO:
828                                 tun2.audmode = V4L2_TUNER_MODE_STEREO;
829                                 break;
830                         case VIDEO_SOUND_LANG2:
831                                 tun2.audmode = V4L2_TUNER_MODE_LANG2;
832                                 break;
833                         }
834                         err = drv(inode, file, VIDIOC_S_TUNER, &tun2);
835                         if (err < 0)
836                                 dprintk("VIDIOCSAUDIO / VIDIOC_S_TUNER: %d\n",err);
837                 }
838                 err = 0;
839                 break;
840         }
841         case VIDIOCMCAPTURE: /*  capture a frame  */
842         {
843                 struct video_mmap       *mm = arg;
844
845                 fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
846                 memset(&buf2,0,sizeof(buf2));
847
848                 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
849                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
850                 if (err < 0) {
851                         dprintk("VIDIOCMCAPTURE / VIDIOC_G_FMT: %d\n",err);
852                         break;
853                 }
854                 if (mm->width   != fmt2->fmt.pix.width  ||
855                     mm->height  != fmt2->fmt.pix.height ||
856                     palette_to_pixelformat(mm->format) !=
857                     fmt2->fmt.pix.pixelformat)
858                 {/* New capture format...  */
859                         fmt2->fmt.pix.width = mm->width;
860                         fmt2->fmt.pix.height = mm->height;
861                         fmt2->fmt.pix.pixelformat =
862                                 palette_to_pixelformat(mm->format);
863                         fmt2->fmt.pix.field = V4L2_FIELD_ANY;
864                         fmt2->fmt.pix.bytesperline = 0;
865                         err = drv(inode, file, VIDIOC_S_FMT, fmt2);
866                         if (err < 0) {
867                                 dprintk("VIDIOCMCAPTURE / VIDIOC_S_FMT: %d\n",err);
868                                 break;
869                         }
870                 }
871                 buf2.index = mm->frame;
872                 buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
873                 err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
874                 if (err < 0) {
875                         dprintk("VIDIOCMCAPTURE / VIDIOC_QUERYBUF: %d\n",err);
876                         break;
877                 }
878                 err = drv(inode, file, VIDIOC_QBUF, &buf2);
879                 if (err < 0) {
880                         dprintk("VIDIOCMCAPTURE / VIDIOC_QBUF: %d\n",err);
881                         break;
882                 }
883                 err = drv(inode, file, VIDIOC_STREAMON, &captype);
884                 if (err < 0)
885                         dprintk("VIDIOCMCAPTURE / VIDIOC_STREAMON: %d\n",err);
886                 break;
887         }
888         case VIDIOCSYNC: /*  wait for a frame  */
889         {
890                 int                     *i = arg;
891
892                 buf2.index = *i;
893                 buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
894                 err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
895                 if (err < 0) {
896                         /*  No such buffer */
897                         dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
898                         break;
899                 }
900                 if (!(buf2.flags & V4L2_BUF_FLAG_MAPPED)) {
901                         /* Buffer is not mapped  */
902                         err = -EINVAL;
903                         break;
904                 }
905
906                 /* make sure capture actually runs so we don't block forever */
907                 err = drv(inode, file, VIDIOC_STREAMON, &captype);
908                 if (err < 0) {
909                         dprintk("VIDIOCSYNC / VIDIOC_STREAMON: %d\n",err);
910                         break;
911                 }
912
913                 /*  Loop as long as the buffer is queued, but not done  */
914                 while ((buf2.flags &
915                         (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
916                        == V4L2_BUF_FLAG_QUEUED)
917                 {
918                         err = poll_one(file);
919                         if (err < 0 ||  /* error or sleep was interrupted  */
920                             err == 0)   /* timeout? Shouldn't occur.  */
921                                 break;
922                         err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
923                         if (err < 0)
924                                 dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
925                 }
926                 if (!(buf2.flags & V4L2_BUF_FLAG_DONE)) /* not done */
927                         break;
928                 do {
929                         err = drv(inode, file, VIDIOC_DQBUF, &buf2);
930                         if (err < 0)
931                                 dprintk("VIDIOCSYNC / VIDIOC_DQBUF: %d\n",err);
932                 } while (err == 0 && buf2.index != *i);
933                 break;
934         }
935
936         case VIDIOCGVBIFMT: /* query VBI data capture format */
937         {
938                 struct vbi_format      *fmt = arg;
939
940                 fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
941                 fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
942
943                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
944                 if (err < 0) {
945                         dprintk("VIDIOCGVBIFMT / VIDIOC_G_FMT: %d\n", err);
946                         break;
947                 }
948                 if (fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY) {
949                         err = -EINVAL;
950                         break;
951                 }
952                 memset(fmt, 0, sizeof(*fmt));
953                 fmt->samples_per_line = fmt2->fmt.vbi.samples_per_line;
954                 fmt->sampling_rate    = fmt2->fmt.vbi.sampling_rate;
955                 fmt->sample_format    = VIDEO_PALETTE_RAW;
956                 fmt->start[0]         = fmt2->fmt.vbi.start[0];
957                 fmt->count[0]         = fmt2->fmt.vbi.count[0];
958                 fmt->start[1]         = fmt2->fmt.vbi.start[1];
959                 fmt->count[1]         = fmt2->fmt.vbi.count[1];
960                 fmt->flags            = fmt2->fmt.vbi.flags & 0x03;
961                 break;
962         }
963         case VIDIOCSVBIFMT:
964         {
965                 struct vbi_format      *fmt = arg;
966
967                 if (VIDEO_PALETTE_RAW != fmt->sample_format) {
968                         err = -EINVAL;
969                         break;
970                 }
971
972                 fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
973
974                 fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
975                 fmt2->fmt.vbi.samples_per_line = fmt->samples_per_line;
976                 fmt2->fmt.vbi.sampling_rate    = fmt->sampling_rate;
977                 fmt2->fmt.vbi.sample_format    = V4L2_PIX_FMT_GREY;
978                 fmt2->fmt.vbi.start[0]         = fmt->start[0];
979                 fmt2->fmt.vbi.count[0]         = fmt->count[0];
980                 fmt2->fmt.vbi.start[1]         = fmt->start[1];
981                 fmt2->fmt.vbi.count[1]         = fmt->count[1];
982                 fmt2->fmt.vbi.flags            = fmt->flags;
983                 err = drv(inode, file, VIDIOC_TRY_FMT, fmt2);
984                 if (err < 0) {
985                         dprintk("VIDIOCSVBIFMT / VIDIOC_TRY_FMT: %d\n", err);
986                         break;
987                 }
988
989                 if (fmt2->fmt.vbi.samples_per_line != fmt->samples_per_line ||
990                     fmt2->fmt.vbi.sampling_rate    != fmt->sampling_rate    ||
991                     fmt2->fmt.vbi.sample_format    != V4L2_PIX_FMT_GREY     ||
992                     fmt2->fmt.vbi.start[0]         != fmt->start[0]         ||
993                     fmt2->fmt.vbi.count[0]         != fmt->count[0]         ||
994                     fmt2->fmt.vbi.start[1]         != fmt->start[1]         ||
995                     fmt2->fmt.vbi.count[1]         != fmt->count[1]         ||
996                     fmt2->fmt.vbi.flags            != fmt->flags) {
997                         err = -EINVAL;
998                         break;
999                 }
1000                 err = drv(inode, file, VIDIOC_S_FMT, fmt2);
1001                 if (err < 0)
1002                         dprintk("VIDIOCSVBIFMT / VIDIOC_S_FMT: %d\n", err);
1003                 break;
1004         }
1005
1006         default:
1007                 err = -ENOIOCTLCMD;
1008                 break;
1009         }
1010
1011         kfree(cap2);
1012         kfree(fmt2);
1013         return err;
1014 }
1015
1016 EXPORT_SYMBOL(v4l_compat_translate_ioctl);
1017
1018 /*
1019  * Local variables:
1020  * c-basic-offset: 8
1021  * End:
1022  */