Merge ../torvalds-2.6/
[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 #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
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 = kmalloc(sizeof(*cap2),GFP_KERNEL);
309                 memset(cap, 0, sizeof(*cap));
310                 memset(cap2, 0, sizeof(*cap2));
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 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
426                 memset(win,0,sizeof(*win));
427                 memset(fmt2,0,sizeof(*fmt2));
428
429                 fmt2->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
430                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
431                 if (err < 0)
432                         dprintk("VIDIOCGWIN / VIDIOC_G_WIN: %d\n",err);
433                 if (err == 0) {
434                         win->x         = fmt2->fmt.win.w.left;
435                         win->y         = fmt2->fmt.win.w.top;
436                         win->width     = fmt2->fmt.win.w.width;
437                         win->height    = fmt2->fmt.win.w.height;
438                         win->chromakey = fmt2->fmt.win.chromakey;
439                         win->clips     = NULL;
440                         win->clipcount = 0;
441                         break;
442                 }
443
444                 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
445                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
446                 if (err < 0) {
447                         dprintk("VIDIOCGWIN / VIDIOC_G_FMT: %d\n",err);
448                         break;
449                 }
450                 win->x         = 0;
451                 win->y         = 0;
452                 win->width     = fmt2->fmt.pix.width;
453                 win->height    = fmt2->fmt.pix.height;
454                 win->chromakey = 0;
455                 win->clips     = NULL;
456                 win->clipcount = 0;
457                 break;
458         }
459         case VIDIOCSWIN: /*  set window and/or capture dimensions  */
460         {
461                 struct video_window     *win = arg;
462                 int err1,err2;
463
464                 fmt2 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
465                 memset(fmt2,0,sizeof(*fmt2));
466                 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
467                 drv(inode, file, VIDIOC_STREAMOFF, &fmt2->type);
468                 err1 = drv(inode, file, VIDIOC_G_FMT, fmt2);
469                 if (err1 < 0)
470                         dprintk("VIDIOCSWIN / VIDIOC_G_FMT: %d\n",err);
471                 if (err1 == 0) {
472                         fmt2->fmt.pix.width  = win->width;
473                         fmt2->fmt.pix.height = win->height;
474                         fmt2->fmt.pix.field  = V4L2_FIELD_ANY;
475                         fmt2->fmt.pix.bytesperline = 0;
476                         err = drv(inode, file, VIDIOC_S_FMT, fmt2);
477                         if (err < 0)
478                                 dprintk("VIDIOCSWIN / VIDIOC_S_FMT #1: %d\n",
479                                         err);
480                         win->width  = fmt2->fmt.pix.width;
481                         win->height = fmt2->fmt.pix.height;
482                 }
483
484                 memset(fmt2,0,sizeof(*fmt2));
485                 fmt2->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
486                 fmt2->fmt.win.w.left    = win->x;
487                 fmt2->fmt.win.w.top     = win->y;
488                 fmt2->fmt.win.w.width   = win->width;
489                 fmt2->fmt.win.w.height  = win->height;
490                 fmt2->fmt.win.chromakey = win->chromakey;
491                 fmt2->fmt.win.clips     = (void __user *)win->clips;
492                 fmt2->fmt.win.clipcount = win->clipcount;
493                 err2 = drv(inode, file, VIDIOC_S_FMT, fmt2);
494                 if (err2 < 0)
495                         dprintk("VIDIOCSWIN / VIDIOC_S_FMT #2: %d\n",err);
496
497                 if (err1 != 0 && err2 != 0)
498                         err = err1;
499                 break;
500         }
501         case VIDIOCCAPTURE: /*  turn on/off preview  */
502         {
503                 int *on = arg;
504
505                 if (0 == *on) {
506                         /* dirty hack time.  But v4l1 has no STREAMOFF
507                          * equivalent in the API, and this one at
508                          * least comes close ... */
509                         drv(inode, file, VIDIOC_STREAMOFF, &captype);
510                 }
511                 err = drv(inode, file, VIDIOC_OVERLAY, arg);
512                 if (err < 0)
513                         dprintk("VIDIOCCAPTURE / VIDIOC_PREVIEW: %d\n",err);
514                 break;
515         }
516         case VIDIOCGCHAN: /*  get input information  */
517         {
518                 struct video_channel    *chan = arg;
519
520                 memset(&input2,0,sizeof(input2));
521                 input2.index = chan->channel;
522                 err = drv(inode, file, VIDIOC_ENUMINPUT, &input2);
523                 if (err < 0) {
524                         dprintk("VIDIOCGCHAN / VIDIOC_ENUMINPUT: "
525                                 "channel=%d err=%d\n",chan->channel,err);
526                         break;
527                 }
528                 chan->channel = input2.index;
529                 memcpy(chan->name, input2.name,
530                        min(sizeof(chan->name), sizeof(input2.name)));
531                 chan->name[sizeof(chan->name) - 1] = 0;
532                 chan->tuners = (input2.type == V4L2_INPUT_TYPE_TUNER) ? 1 : 0;
533                 chan->flags = (chan->tuners) ? VIDEO_VC_TUNER : 0;
534                 switch (input2.type) {
535                 case V4L2_INPUT_TYPE_TUNER:
536                         chan->type = VIDEO_TYPE_TV;
537                         break;
538                 default:
539                 case V4L2_INPUT_TYPE_CAMERA:
540                         chan->type = VIDEO_TYPE_CAMERA;
541                         break;
542                 }
543                 chan->norm = 0;
544                 err = drv(inode, file, VIDIOC_G_STD, &sid);
545                 if (err < 0)
546                         dprintk("VIDIOCGCHAN / VIDIOC_G_STD: %d\n",err);
547                 if (err == 0) {
548                         if (sid & V4L2_STD_PAL)
549                                 chan->norm = VIDEO_MODE_PAL;
550                         if (sid & V4L2_STD_NTSC)
551                                 chan->norm = VIDEO_MODE_NTSC;
552                         if (sid & V4L2_STD_SECAM)
553                                 chan->norm = VIDEO_MODE_SECAM;
554                 }
555                 break;
556         }
557         case VIDIOCSCHAN: /*  set input  */
558         {
559                 struct video_channel *chan = arg;
560
561                 sid = 0;
562                 err = drv(inode, file, VIDIOC_S_INPUT, &chan->channel);
563                 if (err < 0)
564                         dprintk("VIDIOCSCHAN / VIDIOC_S_INPUT: %d\n",err);
565                 switch (chan->norm) {
566                 case VIDEO_MODE_PAL:
567                         sid = V4L2_STD_PAL;
568                         break;
569                 case VIDEO_MODE_NTSC:
570                         sid = V4L2_STD_NTSC;
571                         break;
572                 case VIDEO_MODE_SECAM:
573                         sid = V4L2_STD_SECAM;
574                         break;
575                 }
576                 if (0 != sid) {
577                         err = drv(inode, file, VIDIOC_S_STD, &sid);
578                         if (err < 0)
579                                 dprintk("VIDIOCSCHAN / VIDIOC_S_STD: %d\n",err);
580                 }
581                 break;
582         }
583         case VIDIOCGPICT: /*  get tone controls & partial capture format  */
584         {
585                 struct video_picture    *pict = arg;
586
587                 pict->brightness = get_v4l_control(inode, file,
588                                                    V4L2_CID_BRIGHTNESS,drv);
589                 pict->hue = get_v4l_control(inode, file,
590                                             V4L2_CID_HUE, drv);
591                 pict->contrast = get_v4l_control(inode, file,
592                                                  V4L2_CID_CONTRAST, drv);
593                 pict->colour = get_v4l_control(inode, file,
594                                                V4L2_CID_SATURATION, drv);
595                 pict->whiteness = get_v4l_control(inode, file,
596                                                   V4L2_CID_WHITENESS, drv);
597
598                 fmt2 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
599                 memset(fmt2,0,sizeof(*fmt2));
600                 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
601                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
602                 if (err < 0) {
603                         dprintk("VIDIOCGPICT / VIDIOC_G_FMT: %d\n",err);
604                         break;
605                 }
606                 pict->palette = pixelformat_to_palette(
607                         fmt2->fmt.pix.pixelformat);
608                 break;
609         }
610         case VIDIOCSPICT: /*  set tone controls & partial capture format  */
611         {
612                 struct video_picture    *pict = arg;
613
614                 set_v4l_control(inode, file,
615                                 V4L2_CID_BRIGHTNESS, pict->brightness, drv);
616                 set_v4l_control(inode, file,
617                                 V4L2_CID_HUE, pict->hue, drv);
618                 set_v4l_control(inode, file,
619                                 V4L2_CID_CONTRAST, pict->contrast, drv);
620                 set_v4l_control(inode, file,
621                                 V4L2_CID_SATURATION, pict->colour, drv);
622                 set_v4l_control(inode, file,
623                                 V4L2_CID_WHITENESS, pict->whiteness, drv);
624
625                 fmt2 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
626                 memset(fmt2,0,sizeof(*fmt2));
627                 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
628                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
629                 if (err < 0)
630                         dprintk("VIDIOCSPICT / VIDIOC_G_FMT: %d\n",err);
631                 if (fmt2->fmt.pix.pixelformat !=
632                     palette_to_pixelformat(pict->palette)) {
633                         fmt2->fmt.pix.pixelformat = palette_to_pixelformat(
634                                 pict->palette);
635                         err = drv(inode, file, VIDIOC_S_FMT, fmt2);
636                         if (err < 0)
637                                 dprintk("VIDIOCSPICT / VIDIOC_S_FMT: %d\n",err);
638                 }
639
640                 err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
641                 if (err < 0)
642                         dprintk("VIDIOCSPICT / VIDIOC_G_FBUF: %d\n",err);
643                 if (fbuf2.fmt.pixelformat !=
644                     palette_to_pixelformat(pict->palette)) {
645                         fbuf2.fmt.pixelformat = palette_to_pixelformat(
646                                 pict->palette);
647                         err = drv(inode, file, VIDIOC_S_FBUF, &fbuf2);
648                         if (err < 0)
649                                 dprintk("VIDIOCSPICT / VIDIOC_S_FBUF: %d\n",err);
650                         err = 0; /* likely fails for non-root */
651                 }
652                 break;
653         }
654         case VIDIOCGTUNER: /*  get tuner information  */
655         {
656                 struct video_tuner      *tun = arg;
657
658                 memset(&tun2,0,sizeof(tun2));
659                 err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
660                 if (err < 0) {
661                         dprintk("VIDIOCGTUNER / VIDIOC_G_TUNER: %d\n",err);
662                         break;
663                 }
664                 memcpy(tun->name, tun2.name,
665                        min(sizeof(tun->name), sizeof(tun2.name)));
666                 tun->name[sizeof(tun->name) - 1] = 0;
667                 tun->rangelow = tun2.rangelow;
668                 tun->rangehigh = tun2.rangehigh;
669                 tun->flags = 0;
670                 tun->mode = VIDEO_MODE_AUTO;
671
672                 for (i = 0; i < 64; i++) {
673                         memset(&std2,0,sizeof(std2));
674                         std2.index = i;
675                         if (0 != drv(inode, file, VIDIOC_ENUMSTD, &std2))
676                                 break;
677                         if (std2.id & V4L2_STD_PAL)
678                                 tun->flags |= VIDEO_TUNER_PAL;
679                         if (std2.id & V4L2_STD_NTSC)
680                                 tun->flags |= VIDEO_TUNER_NTSC;
681                         if (std2.id & V4L2_STD_SECAM)
682                                 tun->flags |= VIDEO_TUNER_SECAM;
683                 }
684
685                 err = drv(inode, file, VIDIOC_G_STD, &sid);
686                 if (err < 0)
687                         dprintk("VIDIOCGTUNER / VIDIOC_G_STD: %d\n",err);
688                 if (err == 0) {
689                         if (sid & V4L2_STD_PAL)
690                                 tun->mode = VIDEO_MODE_PAL;
691                         if (sid & V4L2_STD_NTSC)
692                                 tun->mode = VIDEO_MODE_NTSC;
693                         if (sid & V4L2_STD_SECAM)
694                                 tun->mode = VIDEO_MODE_SECAM;
695                 }
696
697                 if (tun2.capability & V4L2_TUNER_CAP_LOW)
698                         tun->flags |= VIDEO_TUNER_LOW;
699                 if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
700                         tun->flags |= VIDEO_TUNER_STEREO_ON;
701                 tun->signal = tun2.signal;
702                 break;
703         }
704         case VIDIOCSTUNER: /*  select a tuner input  */
705         {
706                 err = 0;
707                 break;
708         }
709         case VIDIOCGFREQ: /*  get frequency  */
710         {
711                 int *freq = arg;
712
713                 freq2.tuner = 0;
714                 err = drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
715                 if (err < 0)
716                         dprintk("VIDIOCGFREQ / VIDIOC_G_FREQUENCY: %d\n",err);
717                 if (0 == err)
718                         *freq = freq2.frequency;
719                 break;
720         }
721         case VIDIOCSFREQ: /*  set frequency  */
722         {
723                 int *freq = arg;
724
725                 freq2.tuner = 0;
726                 drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
727                 freq2.frequency = *freq;
728                 err = drv(inode, file, VIDIOC_S_FREQUENCY, &freq2);
729                 if (err < 0)
730                         dprintk("VIDIOCSFREQ / VIDIOC_S_FREQUENCY: %d\n",err);
731                 break;
732         }
733         case VIDIOCGAUDIO: /*  get audio properties/controls  */
734         {
735                 struct video_audio      *aud = arg;
736
737                 err = drv(inode, file, VIDIOC_G_AUDIO, &aud2);
738                 if (err < 0) {
739                         dprintk("VIDIOCGAUDIO / VIDIOC_G_AUDIO: %d\n",err);
740                         break;
741                 }
742                 memcpy(aud->name, aud2.name,
743                        min(sizeof(aud->name), sizeof(aud2.name)));
744                 aud->name[sizeof(aud->name) - 1] = 0;
745                 aud->audio = aud2.index;
746                 aud->flags = 0;
747                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME, drv);
748                 if (i >= 0) {
749                         aud->volume = i;
750                         aud->flags |= VIDEO_AUDIO_VOLUME;
751                 }
752                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BASS, drv);
753                 if (i >= 0) {
754                         aud->bass = i;
755                         aud->flags |= VIDEO_AUDIO_BASS;
756                 }
757                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE, drv);
758                 if (i >= 0) {
759                         aud->treble = i;
760                         aud->flags |= VIDEO_AUDIO_TREBLE;
761                 }
762                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE, drv);
763                 if (i >= 0) {
764                         aud->balance = i;
765                         aud->flags |= VIDEO_AUDIO_BALANCE;
766                 }
767                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE, drv);
768                 if (i >= 0) {
769                         if (i)
770                                 aud->flags |= VIDEO_AUDIO_MUTE;
771                         aud->flags |= VIDEO_AUDIO_MUTABLE;
772                 }
773                 aud->step = 1;
774                 qctrl2.id = V4L2_CID_AUDIO_VOLUME;
775                 if (drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2) == 0 &&
776                     !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
777                         aud->step = qctrl2.step;
778                 aud->mode = 0;
779
780                 memset(&tun2,0,sizeof(tun2));
781                 err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
782                 if (err < 0) {
783                         dprintk("VIDIOCGAUDIO / VIDIOC_G_TUNER: %d\n",err);
784                         err = 0;
785                         break;
786                 }
787
788                 if (tun2.rxsubchans & V4L2_TUNER_SUB_LANG2)
789                         aud->mode = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
790                 else if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
791                         aud->mode = VIDEO_SOUND_STEREO;
792                 else if (tun2.rxsubchans & V4L2_TUNER_SUB_MONO)
793                         aud->mode = VIDEO_SOUND_MONO;
794                 break;
795         }
796         case VIDIOCSAUDIO: /*  set audio controls  */
797         {
798                 struct video_audio      *aud = arg;
799
800                 memset(&aud2,0,sizeof(aud2));
801                 memset(&tun2,0,sizeof(tun2));
802
803                 aud2.index = aud->audio;
804                 err = drv(inode, file, VIDIOC_S_AUDIO, &aud2);
805                 if (err < 0) {
806                         dprintk("VIDIOCSAUDIO / VIDIOC_S_AUDIO: %d\n",err);
807                         break;
808                 }
809
810                 set_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME,
811                                 aud->volume, drv);
812                 set_v4l_control(inode, file, V4L2_CID_AUDIO_BASS,
813                                 aud->bass, drv);
814                 set_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE,
815                                 aud->treble, drv);
816                 set_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE,
817                                 aud->balance, drv);
818                 set_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE,
819                                 !!(aud->flags & VIDEO_AUDIO_MUTE), drv);
820
821                 err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
822                 if (err < 0)
823                         dprintk("VIDIOCSAUDIO / VIDIOC_G_TUNER: %d\n",err);
824                 if (err == 0) {
825                         switch (aud->mode) {
826                         default:
827                         case VIDEO_SOUND_MONO:
828                         case VIDEO_SOUND_LANG1:
829                                 tun2.audmode = V4L2_TUNER_MODE_MONO;
830                                 break;
831                         case VIDEO_SOUND_STEREO:
832                                 tun2.audmode = V4L2_TUNER_MODE_STEREO;
833                                 break;
834                         case VIDEO_SOUND_LANG2:
835                                 tun2.audmode = V4L2_TUNER_MODE_LANG2;
836                                 break;
837                         }
838                         err = drv(inode, file, VIDIOC_S_TUNER, &tun2);
839                         if (err < 0)
840                                 dprintk("VIDIOCSAUDIO / VIDIOC_S_TUNER: %d\n",err);
841                 }
842                 err = 0;
843                 break;
844         }
845         case VIDIOCMCAPTURE: /*  capture a frame  */
846         {
847                 struct video_mmap       *mm = arg;
848
849                 fmt2 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
850                 memset(&buf2,0,sizeof(buf2));
851                 memset(fmt2,0,sizeof(*fmt2));
852
853                 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
854                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
855                 if (err < 0) {
856                         dprintk("VIDIOCMCAPTURE / VIDIOC_G_FMT: %d\n",err);
857                         break;
858                 }
859                 if (mm->width   != fmt2->fmt.pix.width  ||
860                     mm->height  != fmt2->fmt.pix.height ||
861                     palette_to_pixelformat(mm->format) !=
862                     fmt2->fmt.pix.pixelformat)
863                 {/* New capture format...  */
864                         fmt2->fmt.pix.width = mm->width;
865                         fmt2->fmt.pix.height = mm->height;
866                         fmt2->fmt.pix.pixelformat =
867                                 palette_to_pixelformat(mm->format);
868                         fmt2->fmt.pix.field = V4L2_FIELD_ANY;
869                         fmt2->fmt.pix.bytesperline = 0;
870                         err = drv(inode, file, VIDIOC_S_FMT, fmt2);
871                         if (err < 0) {
872                                 dprintk("VIDIOCMCAPTURE / VIDIOC_S_FMT: %d\n",err);
873                                 break;
874                         }
875                 }
876                 buf2.index = mm->frame;
877                 buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
878                 err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
879                 if (err < 0) {
880                         dprintk("VIDIOCMCAPTURE / VIDIOC_QUERYBUF: %d\n",err);
881                         break;
882                 }
883                 err = drv(inode, file, VIDIOC_QBUF, &buf2);
884                 if (err < 0) {
885                         dprintk("VIDIOCMCAPTURE / VIDIOC_QBUF: %d\n",err);
886                         break;
887                 }
888                 err = drv(inode, file, VIDIOC_STREAMON, &captype);
889                 if (err < 0)
890                         dprintk("VIDIOCMCAPTURE / VIDIOC_STREAMON: %d\n",err);
891                 break;
892         }
893         case VIDIOCSYNC: /*  wait for a frame  */
894         {
895                 int                     *i = arg;
896
897                 buf2.index = *i;
898                 buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
899                 err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
900                 if (err < 0) {
901                         /*  No such buffer */
902                         dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
903                         break;
904                 }
905                 if (!(buf2.flags & V4L2_BUF_FLAG_MAPPED)) {
906                         /* Buffer is not mapped  */
907                         err = -EINVAL;
908                         break;
909                 }
910
911                 /* make sure capture actually runs so we don't block forever */
912                 err = drv(inode, file, VIDIOC_STREAMON, &captype);
913                 if (err < 0) {
914                         dprintk("VIDIOCSYNC / VIDIOC_STREAMON: %d\n",err);
915                         break;
916                 }
917
918                 /*  Loop as long as the buffer is queued, but not done  */
919                 while ((buf2.flags &
920                         (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
921                        == V4L2_BUF_FLAG_QUEUED)
922                 {
923                         err = poll_one(file);
924                         if (err < 0 ||  /* error or sleep was interrupted  */
925                             err == 0)   /* timeout? Shouldn't occur.  */
926                                 break;
927                         err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
928                         if (err < 0)
929                                 dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
930                 }
931                 if (!(buf2.flags & V4L2_BUF_FLAG_DONE)) /* not done */
932                         break;
933                 do {
934                         err = drv(inode, file, VIDIOC_DQBUF, &buf2);
935                         if (err < 0)
936                                 dprintk("VIDIOCSYNC / VIDIOC_DQBUF: %d\n",err);
937                 } while (err == 0 && buf2.index != *i);
938                 break;
939         }
940
941         case VIDIOCGVBIFMT: /* query VBI data capture format */
942         {
943                 struct vbi_format      *fmt = arg;
944
945                 fmt2 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
946                 memset(fmt2, 0, sizeof(*fmt2));
947                 fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
948
949                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
950                 if (err < 0) {
951                         dprintk("VIDIOCGVBIFMT / VIDIOC_G_FMT: %d\n", err);
952                         break;
953                 }
954                 memset(fmt, 0, sizeof(*fmt));
955                 fmt->samples_per_line = fmt2->fmt.vbi.samples_per_line;
956                 fmt->sampling_rate    = fmt2->fmt.vbi.sampling_rate;
957                 fmt->sample_format    = VIDEO_PALETTE_RAW;
958                 fmt->start[0]         = fmt2->fmt.vbi.start[0];
959                 fmt->count[0]         = fmt2->fmt.vbi.count[0];
960                 fmt->start[1]         = fmt2->fmt.vbi.start[1];
961                 fmt->count[1]         = fmt2->fmt.vbi.count[1];
962                 fmt->flags            = fmt2->fmt.vbi.flags & 0x03;
963                 break;
964         }
965         case VIDIOCSVBIFMT:
966         {
967                 struct vbi_format      *fmt = arg;
968
969                 fmt2 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
970                 memset(fmt2, 0, sizeof(*fmt2));
971
972                 fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
973                 fmt2->fmt.vbi.samples_per_line = fmt->samples_per_line;
974                 fmt2->fmt.vbi.sampling_rate    = fmt->sampling_rate;
975                 fmt2->fmt.vbi.sample_format    = V4L2_PIX_FMT_GREY;
976                 fmt2->fmt.vbi.start[0]         = fmt->start[0];
977                 fmt2->fmt.vbi.count[0]         = fmt->count[0];
978                 fmt2->fmt.vbi.start[1]         = fmt->start[1];
979                 fmt2->fmt.vbi.count[1]         = fmt->count[1];
980                 fmt2->fmt.vbi.flags            = fmt->flags;
981                 err = drv(inode, file, VIDIOC_TRY_FMT, fmt2);
982                 if (err < 0) {
983                         dprintk("VIDIOCSVBIFMT / VIDIOC_TRY_FMT: %d\n", err);
984                         break;
985                 }
986
987                 if (fmt2->fmt.vbi.samples_per_line != fmt->samples_per_line ||
988                     fmt2->fmt.vbi.sampling_rate    != fmt->sampling_rate    ||
989                     VIDEO_PALETTE_RAW              != fmt->sample_format    ||
990                     fmt2->fmt.vbi.start[0]         != fmt->start[0]         ||
991                     fmt2->fmt.vbi.count[0]         != fmt->count[0]         ||
992                     fmt2->fmt.vbi.start[1]         != fmt->start[1]         ||
993                     fmt2->fmt.vbi.count[1]         != fmt->count[1]         ||
994                     fmt2->fmt.vbi.flags            != fmt->flags) {
995                         err = -EINVAL;
996                         break;
997                 }
998                 err = drv(inode, file, VIDIOC_S_FMT, fmt2);
999                 if (err < 0)
1000                         dprintk("VIDIOCSVBIFMT / VIDIOC_S_FMT: %d\n", err);
1001                 break;
1002         }
1003
1004         default:
1005                 err = -ENOIOCTLCMD;
1006                 break;
1007         }
1008
1009         if (cap2)
1010                 kfree(cap2);
1011         if (fmt2)
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  */