header cleaning: don't include smp_lock.h when not used
[linux-2.6] / 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 <bill@thedirks.org>
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/mm.h>
27 #include <linux/fs.h>
28 #include <linux/file.h>
29 #include <linux/string.h>
30 #include <linux/errno.h>
31 #include <linux/slab.h>
32 #include <linux/videodev.h>
33 #include <media/v4l2-common.h>
34
35 #include <asm/uaccess.h>
36 #include <asm/system.h>
37 #include <asm/pgtable.h>
38
39 #ifdef CONFIG_KMOD
40 #include <linux/kmod.h>
41 #endif
42
43 static unsigned int debug  = 0;
44 module_param(debug, int, 0644);
45 MODULE_PARM_DESC(debug,"enable debug messages");
46 MODULE_AUTHOR("Bill Dirks");
47 MODULE_DESCRIPTION("v4l(1) compatibility layer for v4l2 drivers.");
48 MODULE_LICENSE("GPL");
49
50 #define dprintk(fmt, arg...)    if (debug) \
51         printk(KERN_DEBUG "v4l1-compat: " fmt , ## arg)
52
53 /*
54  *      I O C T L   T R A N S L A T I O N
55  *
56  *      From here on down is the code for translating the numerous
57  *      ioctl commands from the old API to the new API.
58  */
59
60 static int
61 get_v4l_control(struct inode            *inode,
62                 struct file             *file,
63                 int                     cid,
64                 v4l2_kioctl             drv)
65 {
66         struct v4l2_queryctrl   qctrl2;
67         struct v4l2_control     ctrl2;
68         int                     err;
69
70         qctrl2.id = cid;
71         err = drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2);
72         if (err < 0)
73                 dprintk("VIDIOC_QUERYCTRL: %d\n",err);
74         if (err == 0 &&
75             !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
76         {
77                 ctrl2.id = qctrl2.id;
78                 err = drv(inode, file, VIDIOC_G_CTRL, &ctrl2);
79                 if (err < 0) {
80                         dprintk("VIDIOC_G_CTRL: %d\n",err);
81                         return 0;
82                 }
83                 return ((ctrl2.value - qctrl2.minimum) * 65535
84                          + (qctrl2.maximum - qctrl2.minimum) / 2)
85                         / (qctrl2.maximum - qctrl2.minimum);
86         }
87         return 0;
88 }
89
90 static int
91 set_v4l_control(struct inode            *inode,
92                 struct file             *file,
93                 int                     cid,
94                 int                     value,
95                 v4l2_kioctl             drv)
96 {
97         struct v4l2_queryctrl   qctrl2;
98         struct v4l2_control     ctrl2;
99         int                     err;
100
101         qctrl2.id = cid;
102         err = drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2);
103         if (err < 0)
104                 dprintk("VIDIOC_QUERYCTRL: %d\n",err);
105         if (err == 0 &&
106             !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED) &&
107             !(qctrl2.flags & V4L2_CTRL_FLAG_GRABBED))
108         {
109                 if (value < 0)
110                         value = 0;
111                 if (value > 65535)
112                         value = 65535;
113                 if (value && qctrl2.type == V4L2_CTRL_TYPE_BOOLEAN)
114                         value = 65535;
115                 ctrl2.id = qctrl2.id;
116                 ctrl2.value =
117                         (value * (qctrl2.maximum - qctrl2.minimum)
118                          + 32767)
119                         / 65535;
120                 ctrl2.value += qctrl2.minimum;
121                 err = drv(inode, file, VIDIOC_S_CTRL, &ctrl2);
122                 if (err < 0)
123                         dprintk("VIDIOC_S_CTRL: %d\n",err);
124         }
125         return 0;
126 }
127
128 /* ----------------------------------------------------------------- */
129
130 static int palette2pixelformat[] = {
131         [VIDEO_PALETTE_GREY]    = V4L2_PIX_FMT_GREY,
132         [VIDEO_PALETTE_RGB555]  = V4L2_PIX_FMT_RGB555,
133         [VIDEO_PALETTE_RGB565]  = V4L2_PIX_FMT_RGB565,
134         [VIDEO_PALETTE_RGB24]   = V4L2_PIX_FMT_BGR24,
135         [VIDEO_PALETTE_RGB32]   = V4L2_PIX_FMT_BGR32,
136         /* yuv packed pixel */
137         [VIDEO_PALETTE_YUYV]    = V4L2_PIX_FMT_YUYV,
138         [VIDEO_PALETTE_YUV422]  = V4L2_PIX_FMT_YUYV,
139         [VIDEO_PALETTE_UYVY]    = V4L2_PIX_FMT_UYVY,
140         /* yuv planar */
141         [VIDEO_PALETTE_YUV410P] = V4L2_PIX_FMT_YUV410,
142         [VIDEO_PALETTE_YUV420]  = V4L2_PIX_FMT_YUV420,
143         [VIDEO_PALETTE_YUV420P] = V4L2_PIX_FMT_YUV420,
144         [VIDEO_PALETTE_YUV411P] = V4L2_PIX_FMT_YUV411P,
145         [VIDEO_PALETTE_YUV422P] = V4L2_PIX_FMT_YUV422P,
146 };
147
148 static unsigned int
149 palette_to_pixelformat(unsigned int palette)
150 {
151         if (palette < ARRAY_SIZE(palette2pixelformat))
152                 return palette2pixelformat[palette];
153         else
154                 return 0;
155 }
156
157 static unsigned int
158 pixelformat_to_palette(int pixelformat)
159 {
160         int     palette = 0;
161         switch (pixelformat)
162         {
163         case V4L2_PIX_FMT_GREY:
164                 palette = VIDEO_PALETTE_GREY;
165                 break;
166         case V4L2_PIX_FMT_RGB555:
167                 palette = VIDEO_PALETTE_RGB555;
168                 break;
169         case V4L2_PIX_FMT_RGB565:
170                 palette = VIDEO_PALETTE_RGB565;
171                 break;
172         case V4L2_PIX_FMT_BGR24:
173                 palette = VIDEO_PALETTE_RGB24;
174                 break;
175         case V4L2_PIX_FMT_BGR32:
176                 palette = VIDEO_PALETTE_RGB32;
177                 break;
178         /* yuv packed pixel */
179         case V4L2_PIX_FMT_YUYV:
180                 palette = VIDEO_PALETTE_YUYV;
181                 break;
182         case V4L2_PIX_FMT_UYVY:
183                 palette = VIDEO_PALETTE_UYVY;
184                 break;
185         /* yuv planar */
186         case V4L2_PIX_FMT_YUV410:
187                 palette = VIDEO_PALETTE_YUV420;
188                 break;
189         case V4L2_PIX_FMT_YUV420:
190                 palette = VIDEO_PALETTE_YUV420;
191                 break;
192         case V4L2_PIX_FMT_YUV411P:
193                 palette = VIDEO_PALETTE_YUV411P;
194                 break;
195         case V4L2_PIX_FMT_YUV422P:
196                 palette = VIDEO_PALETTE_YUV422P;
197                 break;
198         }
199         return palette;
200 }
201
202 /* ----------------------------------------------------------------- */
203
204 static int poll_one(struct file *file)
205 {
206         int retval = 1;
207         poll_table *table;
208         struct poll_wqueues pwq;
209
210         poll_initwait(&pwq);
211         table = &pwq.pt;
212         for (;;) {
213                 int mask;
214                 set_current_state(TASK_INTERRUPTIBLE);
215                 mask = file->f_op->poll(file, table);
216                 if (mask & POLLIN)
217                         break;
218                 table = NULL;
219                 if (signal_pending(current)) {
220                         retval = -ERESTARTSYS;
221                         break;
222                 }
223                 schedule();
224         }
225         set_current_state(TASK_RUNNING);
226         poll_freewait(&pwq);
227         return retval;
228 }
229
230 static int count_inputs(struct inode         *inode,
231                         struct file          *file,
232                         v4l2_kioctl          drv)
233 {
234         struct v4l2_input input2;
235         int i;
236
237         for (i = 0;; i++) {
238                 memset(&input2,0,sizeof(input2));
239                 input2.index = i;
240                 if (0 != drv(inode,file,VIDIOC_ENUMINPUT, &input2))
241                         break;
242         }
243         return i;
244 }
245
246 static int check_size(struct inode         *inode,
247                       struct file          *file,
248                       v4l2_kioctl          drv,
249                       int *maxw, int *maxh)
250 {
251         struct v4l2_fmtdesc desc2;
252         struct v4l2_format  fmt2;
253
254         memset(&desc2,0,sizeof(desc2));
255         memset(&fmt2,0,sizeof(fmt2));
256
257         desc2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
258         if (0 != drv(inode,file,VIDIOC_ENUM_FMT, &desc2))
259                 goto done;
260
261         fmt2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
262         fmt2.fmt.pix.width       = 10000;
263         fmt2.fmt.pix.height      = 10000;
264         fmt2.fmt.pix.pixelformat = desc2.pixelformat;
265         if (0 != drv(inode,file,VIDIOC_TRY_FMT, &fmt2))
266                 goto done;
267
268         *maxw = fmt2.fmt.pix.width;
269         *maxh = fmt2.fmt.pix.height;
270
271  done:
272         return 0;
273 }
274
275 /* ----------------------------------------------------------------- */
276
277 /*
278  *      This function is exported.
279  */
280 int
281 v4l_compat_translate_ioctl(struct inode         *inode,
282                            struct file          *file,
283                            int                  cmd,
284                            void                 *arg,
285                            v4l2_kioctl          drv)
286 {
287         struct v4l2_capability  *cap2 = NULL;
288         struct v4l2_format      *fmt2 = NULL;
289         enum v4l2_buf_type      captype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
290
291         struct v4l2_framebuffer fbuf2;
292         struct v4l2_input       input2;
293         struct v4l2_tuner       tun2;
294         struct v4l2_standard    std2;
295         struct v4l2_frequency   freq2;
296         struct v4l2_audio       aud2;
297         struct v4l2_queryctrl   qctrl2;
298         struct v4l2_buffer      buf2;
299         v4l2_std_id             sid;
300         int i, err = 0;
301
302         switch (cmd) {
303         case VIDIOCGCAP:        /* capability */
304         {
305                 struct video_capability *cap = arg;
306
307                 cap2 = kzalloc(sizeof(*cap2),GFP_KERNEL);
308                 memset(cap, 0, sizeof(*cap));
309                 memset(&fbuf2, 0, sizeof(fbuf2));
310
311                 err = drv(inode, file, VIDIOC_QUERYCAP, cap2);
312                 if (err < 0) {
313                         dprintk("VIDIOCGCAP / VIDIOC_QUERYCAP: %d\n",err);
314                         break;
315                 }
316                 if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY) {
317                         err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
318                         if (err < 0) {
319                                 dprintk("VIDIOCGCAP / VIDIOC_G_FBUF: %d\n",err);
320                                 memset(&fbuf2, 0, sizeof(fbuf2));
321                         }
322                         err = 0;
323                 }
324
325                 memcpy(cap->name, cap2->card,
326                        min(sizeof(cap->name), sizeof(cap2->card)));
327                 cap->name[sizeof(cap->name) - 1] = 0;
328                 if (cap2->capabilities & V4L2_CAP_VIDEO_CAPTURE)
329                         cap->type |= VID_TYPE_CAPTURE;
330                 if (cap2->capabilities & V4L2_CAP_TUNER)
331                         cap->type |= VID_TYPE_TUNER;
332                 if (cap2->capabilities & V4L2_CAP_VBI_CAPTURE)
333                         cap->type |= VID_TYPE_TELETEXT;
334                 if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY)
335                         cap->type |= VID_TYPE_OVERLAY;
336                 if (fbuf2.capability & V4L2_FBUF_CAP_LIST_CLIPPING)
337                         cap->type |= VID_TYPE_CLIPPING;
338
339                 cap->channels  = count_inputs(inode,file,drv);
340                 check_size(inode,file,drv,
341                            &cap->maxwidth,&cap->maxheight);
342                 cap->audios    =  0; /* FIXME */
343                 cap->minwidth  = 48; /* FIXME */
344                 cap->minheight = 32; /* FIXME */
345                 break;
346         }
347         case VIDIOCGFBUF: /*  get frame buffer  */
348         {
349                 struct video_buffer     *buffer = arg;
350
351                 memset(buffer, 0, sizeof(*buffer));
352                 memset(&fbuf2, 0, sizeof(fbuf2));
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                 memset(&fbuf2, 0, sizeof(fbuf2));
620
621                 set_v4l_control(inode, file,
622                                 V4L2_CID_BRIGHTNESS, pict->brightness, drv);
623                 set_v4l_control(inode, file,
624                                 V4L2_CID_HUE, pict->hue, drv);
625                 set_v4l_control(inode, file,
626                                 V4L2_CID_CONTRAST, pict->contrast, drv);
627                 set_v4l_control(inode, file,
628                                 V4L2_CID_SATURATION, pict->colour, drv);
629                 set_v4l_control(inode, file,
630                                 V4L2_CID_WHITENESS, pict->whiteness, drv);
631
632                 fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
633                 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
634                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
635                 if (err < 0)
636                         dprintk("VIDIOCSPICT / VIDIOC_G_FMT: %d\n",err);
637                 if (fmt2->fmt.pix.pixelformat !=
638                     palette_to_pixelformat(pict->palette)) {
639                         fmt2->fmt.pix.pixelformat = palette_to_pixelformat(
640                                 pict->palette);
641                         err = drv(inode, file, VIDIOC_S_FMT, fmt2);
642                         if (err < 0)
643                                 dprintk("VIDIOCSPICT / VIDIOC_S_FMT: %d\n",err);
644                 }
645
646                 err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
647                 if (err < 0)
648                         dprintk("VIDIOCSPICT / VIDIOC_G_FBUF: %d\n",err);
649                 if (fbuf2.fmt.pixelformat !=
650                     palette_to_pixelformat(pict->palette)) {
651                         fbuf2.fmt.pixelformat = palette_to_pixelformat(
652                                 pict->palette);
653                         err = drv(inode, file, VIDIOC_S_FBUF, &fbuf2);
654                         if (err < 0)
655                                 dprintk("VIDIOCSPICT / VIDIOC_S_FBUF: %d\n",err);
656                         err = 0; /* likely fails for non-root */
657                 }
658                 break;
659         }
660         case VIDIOCGTUNER: /*  get tuner information  */
661         {
662                 struct video_tuner      *tun = arg;
663
664                 memset(&tun2,0,sizeof(tun2));
665                 err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
666                 if (err < 0) {
667                         dprintk("VIDIOCGTUNER / VIDIOC_G_TUNER: %d\n",err);
668                         break;
669                 }
670                 memcpy(tun->name, tun2.name,
671                        min(sizeof(tun->name), sizeof(tun2.name)));
672                 tun->name[sizeof(tun->name) - 1] = 0;
673                 tun->rangelow = tun2.rangelow;
674                 tun->rangehigh = tun2.rangehigh;
675                 tun->flags = 0;
676                 tun->mode = VIDEO_MODE_AUTO;
677
678                 for (i = 0; i < 64; i++) {
679                         memset(&std2,0,sizeof(std2));
680                         std2.index = i;
681                         if (0 != drv(inode, file, VIDIOC_ENUMSTD, &std2))
682                                 break;
683                         if (std2.id & V4L2_STD_PAL)
684                                 tun->flags |= VIDEO_TUNER_PAL;
685                         if (std2.id & V4L2_STD_NTSC)
686                                 tun->flags |= VIDEO_TUNER_NTSC;
687                         if (std2.id & V4L2_STD_SECAM)
688                                 tun->flags |= VIDEO_TUNER_SECAM;
689                 }
690
691                 err = drv(inode, file, VIDIOC_G_STD, &sid);
692                 if (err < 0)
693                         dprintk("VIDIOCGTUNER / VIDIOC_G_STD: %d\n",err);
694                 if (err == 0) {
695                         if (sid & V4L2_STD_PAL)
696                                 tun->mode = VIDEO_MODE_PAL;
697                         if (sid & V4L2_STD_NTSC)
698                                 tun->mode = VIDEO_MODE_NTSC;
699                         if (sid & V4L2_STD_SECAM)
700                                 tun->mode = VIDEO_MODE_SECAM;
701                 }
702
703                 if (tun2.capability & V4L2_TUNER_CAP_LOW)
704                         tun->flags |= VIDEO_TUNER_LOW;
705                 if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
706                         tun->flags |= VIDEO_TUNER_STEREO_ON;
707                 tun->signal = tun2.signal;
708                 break;
709         }
710         case VIDIOCSTUNER: /*  select a tuner input  */
711         {
712                 struct video_tuner      *tun = arg;
713                 struct v4l2_tuner       t;
714                 memset(&t,0,sizeof(t));
715
716                 t.index=tun->tuner;
717
718                 err = drv(inode, file, VIDIOC_S_INPUT, &t);
719                 if (err < 0)
720                         dprintk("VIDIOCSTUNER / VIDIOC_S_INPUT: %d\n",err);
721
722                 break;
723         }
724         case VIDIOCGFREQ: /*  get frequency  */
725         {
726                 unsigned long *freq = arg;
727                 memset(&freq2,0,sizeof(freq2));
728
729                 freq2.tuner = 0;
730                 err = drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
731                 if (err < 0)
732                         dprintk("VIDIOCGFREQ / VIDIOC_G_FREQUENCY: %d\n",err);
733                 if (0 == err)
734                         *freq = freq2.frequency;
735                 break;
736         }
737         case VIDIOCSFREQ: /*  set frequency  */
738         {
739                 unsigned long *freq = arg;
740                 memset(&freq2,0,sizeof(freq2));
741
742                 drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
743                 freq2.frequency = *freq;
744                 err = drv(inode, file, VIDIOC_S_FREQUENCY, &freq2);
745                 if (err < 0)
746                         dprintk("VIDIOCSFREQ / VIDIOC_S_FREQUENCY: %d\n",err);
747                 break;
748         }
749         case VIDIOCGAUDIO: /*  get audio properties/controls  */
750         {
751                 struct video_audio      *aud = arg;
752                 memset(&aud2,0,sizeof(aud2));
753
754                 err = drv(inode, file, VIDIOC_G_AUDIO, &aud2);
755                 if (err < 0) {
756                         dprintk("VIDIOCGAUDIO / VIDIOC_G_AUDIO: %d\n",err);
757                         break;
758                 }
759                 memcpy(aud->name, aud2.name,
760                        min(sizeof(aud->name), sizeof(aud2.name)));
761                 aud->name[sizeof(aud->name) - 1] = 0;
762                 aud->audio = aud2.index;
763                 aud->flags = 0;
764                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME, drv);
765                 if (i >= 0) {
766                         aud->volume = i;
767                         aud->flags |= VIDEO_AUDIO_VOLUME;
768                 }
769                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BASS, drv);
770                 if (i >= 0) {
771                         aud->bass = i;
772                         aud->flags |= VIDEO_AUDIO_BASS;
773                 }
774                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE, drv);
775                 if (i >= 0) {
776                         aud->treble = i;
777                         aud->flags |= VIDEO_AUDIO_TREBLE;
778                 }
779                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE, drv);
780                 if (i >= 0) {
781                         aud->balance = i;
782                         aud->flags |= VIDEO_AUDIO_BALANCE;
783                 }
784                 i = get_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE, drv);
785                 if (i >= 0) {
786                         if (i)
787                                 aud->flags |= VIDEO_AUDIO_MUTE;
788                         aud->flags |= VIDEO_AUDIO_MUTABLE;
789                 }
790                 aud->step = 1;
791                 qctrl2.id = V4L2_CID_AUDIO_VOLUME;
792                 if (drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2) == 0 &&
793                     !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
794                         aud->step = qctrl2.step;
795                 aud->mode = 0;
796
797                 memset(&tun2,0,sizeof(tun2));
798                 err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
799                 if (err < 0) {
800                         dprintk("VIDIOCGAUDIO / VIDIOC_G_TUNER: %d\n",err);
801                         err = 0;
802                         break;
803                 }
804
805                 if (tun2.rxsubchans & V4L2_TUNER_SUB_LANG2)
806                         aud->mode = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
807                 else if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
808                         aud->mode = VIDEO_SOUND_STEREO;
809                 else if (tun2.rxsubchans & V4L2_TUNER_SUB_MONO)
810                         aud->mode = VIDEO_SOUND_MONO;
811                 break;
812         }
813         case VIDIOCSAUDIO: /*  set audio controls  */
814         {
815                 struct video_audio      *aud = arg;
816
817                 memset(&aud2,0,sizeof(aud2));
818                 memset(&tun2,0,sizeof(tun2));
819
820                 aud2.index = aud->audio;
821                 err = drv(inode, file, VIDIOC_S_AUDIO, &aud2);
822                 if (err < 0) {
823                         dprintk("VIDIOCSAUDIO / VIDIOC_S_AUDIO: %d\n",err);
824                         break;
825                 }
826
827                 set_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME,
828                                 aud->volume, drv);
829                 set_v4l_control(inode, file, V4L2_CID_AUDIO_BASS,
830                                 aud->bass, drv);
831                 set_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE,
832                                 aud->treble, drv);
833                 set_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE,
834                                 aud->balance, drv);
835                 set_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE,
836                                 !!(aud->flags & VIDEO_AUDIO_MUTE), drv);
837
838                 err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
839                 if (err < 0)
840                         dprintk("VIDIOCSAUDIO / VIDIOC_G_TUNER: %d\n",err);
841                 if (err == 0) {
842                         switch (aud->mode) {
843                         default:
844                         case VIDEO_SOUND_MONO:
845                         case VIDEO_SOUND_LANG1:
846                                 tun2.audmode = V4L2_TUNER_MODE_MONO;
847                                 break;
848                         case VIDEO_SOUND_STEREO:
849                                 tun2.audmode = V4L2_TUNER_MODE_STEREO;
850                                 break;
851                         case VIDEO_SOUND_LANG2:
852                                 tun2.audmode = V4L2_TUNER_MODE_LANG2;
853                                 break;
854                         }
855                         err = drv(inode, file, VIDIOC_S_TUNER, &tun2);
856                         if (err < 0)
857                                 dprintk("VIDIOCSAUDIO / VIDIOC_S_TUNER: %d\n",err);
858                 }
859                 err = 0;
860                 break;
861         }
862         case VIDIOCMCAPTURE: /*  capture a frame  */
863         {
864                 struct video_mmap       *mm = arg;
865
866                 fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
867                 memset(&buf2,0,sizeof(buf2));
868
869                 fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
870                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
871                 if (err < 0) {
872                         dprintk("VIDIOCMCAPTURE / VIDIOC_G_FMT: %d\n",err);
873                         break;
874                 }
875                 if (mm->width   != fmt2->fmt.pix.width  ||
876                     mm->height  != fmt2->fmt.pix.height ||
877                     palette_to_pixelformat(mm->format) !=
878                     fmt2->fmt.pix.pixelformat)
879                 {/* New capture format...  */
880                         fmt2->fmt.pix.width = mm->width;
881                         fmt2->fmt.pix.height = mm->height;
882                         fmt2->fmt.pix.pixelformat =
883                                 palette_to_pixelformat(mm->format);
884                         fmt2->fmt.pix.field = V4L2_FIELD_ANY;
885                         fmt2->fmt.pix.bytesperline = 0;
886                         err = drv(inode, file, VIDIOC_S_FMT, fmt2);
887                         if (err < 0) {
888                                 dprintk("VIDIOCMCAPTURE / VIDIOC_S_FMT: %d\n",err);
889                                 break;
890                         }
891                 }
892                 buf2.index = mm->frame;
893                 buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
894                 err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
895                 if (err < 0) {
896                         dprintk("VIDIOCMCAPTURE / VIDIOC_QUERYBUF: %d\n",err);
897                         break;
898                 }
899                 err = drv(inode, file, VIDIOC_QBUF, &buf2);
900                 if (err < 0) {
901                         dprintk("VIDIOCMCAPTURE / VIDIOC_QBUF: %d\n",err);
902                         break;
903                 }
904                 err = drv(inode, file, VIDIOC_STREAMON, &captype);
905                 if (err < 0)
906                         dprintk("VIDIOCMCAPTURE / VIDIOC_STREAMON: %d\n",err);
907                 break;
908         }
909         case VIDIOCSYNC: /*  wait for a frame  */
910         {
911                 int                     *i = arg;
912
913                 memset(&buf2,0,sizeof(buf2));
914                 buf2.index = *i;
915                 buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
916                 err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
917                 if (err < 0) {
918                         /*  No such buffer */
919                         dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
920                         break;
921                 }
922                 if (!(buf2.flags & V4L2_BUF_FLAG_MAPPED)) {
923                         /* Buffer is not mapped  */
924                         err = -EINVAL;
925                         break;
926                 }
927
928                 /* make sure capture actually runs so we don't block forever */
929                 err = drv(inode, file, VIDIOC_STREAMON, &captype);
930                 if (err < 0) {
931                         dprintk("VIDIOCSYNC / VIDIOC_STREAMON: %d\n",err);
932                         break;
933                 }
934
935                 /*  Loop as long as the buffer is queued, but not done  */
936                 while ((buf2.flags &
937                         (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
938                        == V4L2_BUF_FLAG_QUEUED)
939                 {
940                         err = poll_one(file);
941                         if (err < 0 ||  /* error or sleep was interrupted  */
942                             err == 0)   /* timeout? Shouldn't occur.  */
943                                 break;
944                         err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
945                         if (err < 0)
946                                 dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
947                 }
948                 if (!(buf2.flags & V4L2_BUF_FLAG_DONE)) /* not done */
949                         break;
950                 do {
951                         err = drv(inode, file, VIDIOC_DQBUF, &buf2);
952                         if (err < 0)
953                                 dprintk("VIDIOCSYNC / VIDIOC_DQBUF: %d\n",err);
954                 } while (err == 0 && buf2.index != *i);
955                 break;
956         }
957
958         case VIDIOCGVBIFMT: /* query VBI data capture format */
959         {
960                 struct vbi_format      *fmt = arg;
961
962                 fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
963                 fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
964
965                 err = drv(inode, file, VIDIOC_G_FMT, fmt2);
966                 if (err < 0) {
967                         dprintk("VIDIOCGVBIFMT / VIDIOC_G_FMT: %d\n", err);
968                         break;
969                 }
970                 if (fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY) {
971                         err = -EINVAL;
972                         break;
973                 }
974                 memset(fmt, 0, sizeof(*fmt));
975                 fmt->samples_per_line = fmt2->fmt.vbi.samples_per_line;
976                 fmt->sampling_rate    = fmt2->fmt.vbi.sampling_rate;
977                 fmt->sample_format    = VIDEO_PALETTE_RAW;
978                 fmt->start[0]         = fmt2->fmt.vbi.start[0];
979                 fmt->count[0]         = fmt2->fmt.vbi.count[0];
980                 fmt->start[1]         = fmt2->fmt.vbi.start[1];
981                 fmt->count[1]         = fmt2->fmt.vbi.count[1];
982                 fmt->flags            = fmt2->fmt.vbi.flags & 0x03;
983                 break;
984         }
985         case VIDIOCSVBIFMT:
986         {
987                 struct vbi_format      *fmt = arg;
988
989                 if (VIDEO_PALETTE_RAW != fmt->sample_format) {
990                         err = -EINVAL;
991                         break;
992                 }
993
994                 fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
995
996                 fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
997                 fmt2->fmt.vbi.samples_per_line = fmt->samples_per_line;
998                 fmt2->fmt.vbi.sampling_rate    = fmt->sampling_rate;
999                 fmt2->fmt.vbi.sample_format    = V4L2_PIX_FMT_GREY;
1000                 fmt2->fmt.vbi.start[0]         = fmt->start[0];
1001                 fmt2->fmt.vbi.count[0]         = fmt->count[0];
1002                 fmt2->fmt.vbi.start[1]         = fmt->start[1];
1003                 fmt2->fmt.vbi.count[1]         = fmt->count[1];
1004                 fmt2->fmt.vbi.flags            = fmt->flags;
1005                 err = drv(inode, file, VIDIOC_TRY_FMT, fmt2);
1006                 if (err < 0) {
1007                         dprintk("VIDIOCSVBIFMT / VIDIOC_TRY_FMT: %d\n", err);
1008                         break;
1009                 }
1010
1011                 if (fmt2->fmt.vbi.samples_per_line != fmt->samples_per_line ||
1012                     fmt2->fmt.vbi.sampling_rate    != fmt->sampling_rate    ||
1013                     fmt2->fmt.vbi.sample_format    != V4L2_PIX_FMT_GREY     ||
1014                     fmt2->fmt.vbi.start[0]         != fmt->start[0]         ||
1015                     fmt2->fmt.vbi.count[0]         != fmt->count[0]         ||
1016                     fmt2->fmt.vbi.start[1]         != fmt->start[1]         ||
1017                     fmt2->fmt.vbi.count[1]         != fmt->count[1]         ||
1018                     fmt2->fmt.vbi.flags            != fmt->flags) {
1019                         err = -EINVAL;
1020                         break;
1021                 }
1022                 err = drv(inode, file, VIDIOC_S_FMT, fmt2);
1023                 if (err < 0)
1024                         dprintk("VIDIOCSVBIFMT / VIDIOC_S_FMT: %d\n", err);
1025                 break;
1026         }
1027
1028         default:
1029                 err = -ENOIOCTLCMD;
1030                 break;
1031         }
1032
1033         kfree(cap2);
1034         kfree(fmt2);
1035         return err;
1036 }
1037
1038 EXPORT_SYMBOL(v4l_compat_translate_ioctl);
1039
1040 /*
1041  * Local variables:
1042  * c-basic-offset: 8
1043  * End:
1044  */