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