Pull context-bitmap into release branch
[linux-2.6] / drivers / media / video / saa7134 / saa7134-video.c
1 /*
2  *
3  * device driver for philips saa7134 based TV cards
4  * video4linux video interface
5  *
6  * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29
30 #include "saa7134-reg.h"
31 #include "saa7134.h"
32
33 /* Include V4L1 specific functions. Should be removed soon */
34 #include <linux/videodev.h>
35
36 /* ------------------------------------------------------------------ */
37
38 static unsigned int video_debug   = 0;
39 static unsigned int gbuffers      = 8;
40 static unsigned int noninterlaced = 0;
41 static unsigned int gbufsize      = 720*576*4;
42 static unsigned int gbufsize_max  = 720*576*4;
43 module_param(video_debug, int, 0644);
44 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
45 module_param(gbuffers, int, 0444);
46 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
47 module_param(noninterlaced, int, 0644);
48 MODULE_PARM_DESC(noninterlaced,"video input is noninterlaced");
49
50 #define dprintk(fmt, arg...)    if (video_debug) \
51         printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
52
53 /* ------------------------------------------------------------------ */
54 /* Defines for Video Output Port Register at address 0x191            */
55
56 /* Bit 0: VIP code T bit polarity */
57
58 #define VP_T_CODE_P_NON_INVERTED        0x00
59 #define VP_T_CODE_P_INVERTED            0x01
60
61 /* ------------------------------------------------------------------ */
62 /* Defines for Video Output Port Register at address 0x195            */
63
64 /* Bit 2: Video output clock delay control */
65
66 #define VP_CLK_CTRL2_NOT_DELAYED        0x00
67 #define VP_CLK_CTRL2_DELAYED            0x04
68
69 /* Bit 1: Video output clock invert control */
70
71 #define VP_CLK_CTRL1_NON_INVERTED       0x00
72 #define VP_CLK_CTRL1_INVERTED           0x02
73
74 /* ------------------------------------------------------------------ */
75 /* Defines for Video Output Port Register at address 0x196            */
76
77 /* Bits 2 to 0: VSYNC pin video vertical sync type */
78
79 #define VP_VS_TYPE_MASK                 0x07
80
81 #define VP_VS_TYPE_OFF                  0x00
82 #define VP_VS_TYPE_V123                 0x01
83 #define VP_VS_TYPE_V_ITU                0x02
84 #define VP_VS_TYPE_VGATE_L              0x03
85 #define VP_VS_TYPE_RESERVED1            0x04
86 #define VP_VS_TYPE_RESERVED2            0x05
87 #define VP_VS_TYPE_F_ITU                0x06
88 #define VP_VS_TYPE_SC_FID               0x07
89
90 /* ------------------------------------------------------------------ */
91 /* data structs for video                                             */
92
93 static int video_out[][9] = {
94         [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
95 };
96
97 static struct saa7134_format formats[] = {
98         {
99                 .name     = "8 bpp gray",
100                 .fourcc   = V4L2_PIX_FMT_GREY,
101                 .depth    = 8,
102                 .pm       = 0x06,
103         },{
104                 .name     = "15 bpp RGB, le",
105                 .fourcc   = V4L2_PIX_FMT_RGB555,
106                 .depth    = 16,
107                 .pm       = 0x13 | 0x80,
108         },{
109                 .name     = "15 bpp RGB, be",
110                 .fourcc   = V4L2_PIX_FMT_RGB555X,
111                 .depth    = 16,
112                 .pm       = 0x13 | 0x80,
113                 .bswap    = 1,
114         },{
115                 .name     = "16 bpp RGB, le",
116                 .fourcc   = V4L2_PIX_FMT_RGB565,
117                 .depth    = 16,
118                 .pm       = 0x10 | 0x80,
119         },{
120                 .name     = "16 bpp RGB, be",
121                 .fourcc   = V4L2_PIX_FMT_RGB565X,
122                 .depth    = 16,
123                 .pm       = 0x10 | 0x80,
124                 .bswap    = 1,
125         },{
126                 .name     = "24 bpp RGB, le",
127                 .fourcc   = V4L2_PIX_FMT_BGR24,
128                 .depth    = 24,
129                 .pm       = 0x11,
130         },{
131                 .name     = "24 bpp RGB, be",
132                 .fourcc   = V4L2_PIX_FMT_RGB24,
133                 .depth    = 24,
134                 .pm       = 0x11,
135                 .bswap    = 1,
136         },{
137                 .name     = "32 bpp RGB, le",
138                 .fourcc   = V4L2_PIX_FMT_BGR32,
139                 .depth    = 32,
140                 .pm       = 0x12,
141         },{
142                 .name     = "32 bpp RGB, be",
143                 .fourcc   = V4L2_PIX_FMT_RGB32,
144                 .depth    = 32,
145                 .pm       = 0x12,
146                 .bswap    = 1,
147                 .wswap    = 1,
148         },{
149                 .name     = "4:2:2 packed, YUYV",
150                 .fourcc   = V4L2_PIX_FMT_YUYV,
151                 .depth    = 16,
152                 .pm       = 0x00,
153                 .bswap    = 1,
154                 .yuv      = 1,
155         },{
156                 .name     = "4:2:2 packed, UYVY",
157                 .fourcc   = V4L2_PIX_FMT_UYVY,
158                 .depth    = 16,
159                 .pm       = 0x00,
160                 .yuv      = 1,
161         },{
162                 .name     = "4:2:2 planar, Y-Cb-Cr",
163                 .fourcc   = V4L2_PIX_FMT_YUV422P,
164                 .depth    = 16,
165                 .pm       = 0x09,
166                 .yuv      = 1,
167                 .planar   = 1,
168                 .hshift   = 1,
169                 .vshift   = 0,
170         },{
171                 .name     = "4:2:0 planar, Y-Cb-Cr",
172                 .fourcc   = V4L2_PIX_FMT_YUV420,
173                 .depth    = 12,
174                 .pm       = 0x0a,
175                 .yuv      = 1,
176                 .planar   = 1,
177                 .hshift   = 1,
178                 .vshift   = 1,
179         },{
180                 .name     = "4:2:0 planar, Y-Cb-Cr",
181                 .fourcc   = V4L2_PIX_FMT_YVU420,
182                 .depth    = 12,
183                 .pm       = 0x0a,
184                 .yuv      = 1,
185                 .planar   = 1,
186                 .uvswap   = 1,
187                 .hshift   = 1,
188                 .vshift   = 1,
189         }
190 };
191 #define FORMATS ARRAY_SIZE(formats)
192
193 #define NORM_625_50                     \
194                 .h_start       = 0,     \
195                 .h_stop        = 719,   \
196                 .video_v_start = 24,    \
197                 .video_v_stop  = 311,   \
198                 .vbi_v_start_0 = 7,     \
199                 .vbi_v_stop_0  = 22,    \
200                 .vbi_v_start_1 = 319,   \
201                 .src_timing    = 4
202
203 #define NORM_525_60                     \
204                 .h_start       = 0,     \
205                 .h_stop        = 703,   \
206                 .video_v_start = 23,    \
207                 .video_v_stop  = 262,   \
208                 .vbi_v_start_0 = 10,    \
209                 .vbi_v_stop_0  = 21,    \
210                 .vbi_v_start_1 = 273,   \
211                 .src_timing    = 7
212
213 static struct saa7134_tvnorm tvnorms[] = {
214         {
215                 .name          = "PAL", /* autodetect */
216                 .id            = V4L2_STD_PAL,
217                 NORM_625_50,
218
219                 .sync_control  = 0x18,
220                 .luma_control  = 0x40,
221                 .chroma_ctrl1  = 0x81,
222                 .chroma_gain   = 0x2a,
223                 .chroma_ctrl2  = 0x06,
224                 .vgate_misc    = 0x1c,
225
226         },{
227                 .name          = "PAL-BG",
228                 .id            = V4L2_STD_PAL_BG,
229                 NORM_625_50,
230
231                 .sync_control  = 0x18,
232                 .luma_control  = 0x40,
233                 .chroma_ctrl1  = 0x81,
234                 .chroma_gain   = 0x2a,
235                 .chroma_ctrl2  = 0x06,
236                 .vgate_misc    = 0x1c,
237
238         },{
239                 .name          = "PAL-I",
240                 .id            = V4L2_STD_PAL_I,
241                 NORM_625_50,
242
243                 .sync_control  = 0x18,
244                 .luma_control  = 0x40,
245                 .chroma_ctrl1  = 0x81,
246                 .chroma_gain   = 0x2a,
247                 .chroma_ctrl2  = 0x06,
248                 .vgate_misc    = 0x1c,
249
250         },{
251                 .name          = "PAL-DK",
252                 .id            = V4L2_STD_PAL_DK,
253                 NORM_625_50,
254
255                 .sync_control  = 0x18,
256                 .luma_control  = 0x40,
257                 .chroma_ctrl1  = 0x81,
258                 .chroma_gain   = 0x2a,
259                 .chroma_ctrl2  = 0x06,
260                 .vgate_misc    = 0x1c,
261
262         },{
263                 .name          = "NTSC",
264                 .id            = V4L2_STD_NTSC,
265                 NORM_525_60,
266
267                 .sync_control  = 0x59,
268                 .luma_control  = 0x40,
269                 .chroma_ctrl1  = 0x89,
270                 .chroma_gain   = 0x2a,
271                 .chroma_ctrl2  = 0x0e,
272                 .vgate_misc    = 0x18,
273
274         },{
275                 .name          = "SECAM",
276                 .id            = V4L2_STD_SECAM,
277                 NORM_625_50,
278
279                 .sync_control  = 0x18, /* old: 0x58, */
280                 .luma_control  = 0x1b,
281                 .chroma_ctrl1  = 0xd1,
282                 .chroma_gain   = 0x80,
283                 .chroma_ctrl2  = 0x00,
284                 .vgate_misc    = 0x1c,
285
286         },{
287                 .name          = "PAL-M",
288                 .id            = V4L2_STD_PAL_M,
289                 NORM_525_60,
290
291                 .sync_control  = 0x59,
292                 .luma_control  = 0x40,
293                 .chroma_ctrl1  = 0xb9,
294                 .chroma_gain   = 0x2a,
295                 .chroma_ctrl2  = 0x0e,
296                 .vgate_misc    = 0x18,
297
298         },{
299                 .name          = "PAL-Nc",
300                 .id            = V4L2_STD_PAL_Nc,
301                 NORM_625_50,
302
303                 .sync_control  = 0x18,
304                 .luma_control  = 0x40,
305                 .chroma_ctrl1  = 0xa1,
306                 .chroma_gain   = 0x2a,
307                 .chroma_ctrl2  = 0x06,
308                 .vgate_misc    = 0x1c,
309
310         },{
311                 .name          = "PAL-60",
312                 .id            = V4L2_STD_PAL_60,
313
314                 .h_start       = 0,
315                 .h_stop        = 719,
316                 .video_v_start = 23,
317                 .video_v_stop  = 262,
318                 .vbi_v_start_0 = 10,
319                 .vbi_v_stop_0  = 21,
320                 .vbi_v_start_1 = 273,
321                 .src_timing    = 7,
322
323                 .sync_control  = 0x18,
324                 .luma_control  = 0x40,
325                 .chroma_ctrl1  = 0x81,
326                 .chroma_gain   = 0x2a,
327                 .chroma_ctrl2  = 0x06,
328                 .vgate_misc    = 0x1c,
329         }
330 };
331 #define TVNORMS ARRAY_SIZE(tvnorms)
332
333 #define V4L2_CID_PRIVATE_INVERT      (V4L2_CID_PRIVATE_BASE + 0)
334 #define V4L2_CID_PRIVATE_Y_ODD       (V4L2_CID_PRIVATE_BASE + 1)
335 #define V4L2_CID_PRIVATE_Y_EVEN      (V4L2_CID_PRIVATE_BASE + 2)
336 #define V4L2_CID_PRIVATE_AUTOMUTE    (V4L2_CID_PRIVATE_BASE + 3)
337 #define V4L2_CID_PRIVATE_LASTP1      (V4L2_CID_PRIVATE_BASE + 4)
338
339 static const struct v4l2_queryctrl no_ctrl = {
340         .name  = "42",
341         .flags = V4L2_CTRL_FLAG_DISABLED,
342 };
343 static const struct v4l2_queryctrl video_ctrls[] = {
344         /* --- video --- */
345         {
346                 .id            = V4L2_CID_BRIGHTNESS,
347                 .name          = "Brightness",
348                 .minimum       = 0,
349                 .maximum       = 255,
350                 .step          = 1,
351                 .default_value = 128,
352                 .type          = V4L2_CTRL_TYPE_INTEGER,
353         },{
354                 .id            = V4L2_CID_CONTRAST,
355                 .name          = "Contrast",
356                 .minimum       = 0,
357                 .maximum       = 127,
358                 .step          = 1,
359                 .default_value = 68,
360                 .type          = V4L2_CTRL_TYPE_INTEGER,
361         },{
362                 .id            = V4L2_CID_SATURATION,
363                 .name          = "Saturation",
364                 .minimum       = 0,
365                 .maximum       = 127,
366                 .step          = 1,
367                 .default_value = 64,
368                 .type          = V4L2_CTRL_TYPE_INTEGER,
369         },{
370                 .id            = V4L2_CID_HUE,
371                 .name          = "Hue",
372                 .minimum       = -128,
373                 .maximum       = 127,
374                 .step          = 1,
375                 .default_value = 0,
376                 .type          = V4L2_CTRL_TYPE_INTEGER,
377         },{
378                 .id            = V4L2_CID_HFLIP,
379                 .name          = "Mirror",
380                 .minimum       = 0,
381                 .maximum       = 1,
382                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
383         },
384         /* --- audio --- */
385         {
386                 .id            = V4L2_CID_AUDIO_MUTE,
387                 .name          = "Mute",
388                 .minimum       = 0,
389                 .maximum       = 1,
390                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
391         },{
392                 .id            = V4L2_CID_AUDIO_VOLUME,
393                 .name          = "Volume",
394                 .minimum       = -15,
395                 .maximum       = 15,
396                 .step          = 1,
397                 .default_value = 0,
398                 .type          = V4L2_CTRL_TYPE_INTEGER,
399         },
400         /* --- private --- */
401         {
402                 .id            = V4L2_CID_PRIVATE_INVERT,
403                 .name          = "Invert",
404                 .minimum       = 0,
405                 .maximum       = 1,
406                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
407         },{
408                 .id            = V4L2_CID_PRIVATE_Y_ODD,
409                 .name          = "y offset odd field",
410                 .minimum       = 0,
411                 .maximum       = 128,
412                 .default_value = 0,
413                 .type          = V4L2_CTRL_TYPE_INTEGER,
414         },{
415                 .id            = V4L2_CID_PRIVATE_Y_EVEN,
416                 .name          = "y offset even field",
417                 .minimum       = 0,
418                 .maximum       = 128,
419                 .default_value = 0,
420                 .type          = V4L2_CTRL_TYPE_INTEGER,
421         },{
422                 .id            = V4L2_CID_PRIVATE_AUTOMUTE,
423                 .name          = "automute",
424                 .minimum       = 0,
425                 .maximum       = 1,
426                 .default_value = 1,
427                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
428         }
429 };
430 static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
431
432 static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
433 {
434         unsigned int i;
435
436         for (i = 0; i < CTRLS; i++)
437                 if (video_ctrls[i].id == id)
438                         return video_ctrls+i;
439         return NULL;
440 }
441
442 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
443 {
444         unsigned int i;
445
446         for (i = 0; i < FORMATS; i++)
447                 if (formats[i].fourcc == fourcc)
448                         return formats+i;
449         return NULL;
450 }
451
452 /* ----------------------------------------------------------------------- */
453 /* resource management                                                     */
454
455 static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
456 {
457         if (fh->resources & bit)
458                 /* have it already allocated */
459                 return 1;
460
461         /* is it free? */
462         down(&dev->lock);
463         if (dev->resources & bit) {
464                 /* no, someone else uses it */
465                 up(&dev->lock);
466                 return 0;
467         }
468         /* it's free, grab it */
469         fh->resources  |= bit;
470         dev->resources |= bit;
471         dprintk("res: get %d\n",bit);
472         up(&dev->lock);
473         return 1;
474 }
475
476 static
477 int res_check(struct saa7134_fh *fh, unsigned int bit)
478 {
479         return (fh->resources & bit);
480 }
481
482 static
483 int res_locked(struct saa7134_dev *dev, unsigned int bit)
484 {
485         return (dev->resources & bit);
486 }
487
488 static
489 void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
490 {
491         if ((fh->resources & bits) != bits)
492                 BUG();
493
494         down(&dev->lock);
495         fh->resources  &= ~bits;
496         dev->resources &= ~bits;
497         dprintk("res: put %d\n",bits);
498         up(&dev->lock);
499 }
500
501 /* ------------------------------------------------------------------ */
502
503 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
504 {
505         int luma_control,sync_control,mux;
506
507         dprintk("set tv norm = %s\n",norm->name);
508         dev->tvnorm = norm;
509
510         mux = card_in(dev,dev->ctl_input).vmux;
511         luma_control = norm->luma_control;
512         sync_control = norm->sync_control;
513
514         if (mux > 5)
515                 luma_control |= 0x80; /* svideo */
516         if (noninterlaced || dev->nosignal)
517                 sync_control |= 0x20;
518
519         /* setup cropping */
520         dev->crop_bounds.left    = norm->h_start;
521         dev->crop_defrect.left   = norm->h_start;
522         dev->crop_bounds.width   = norm->h_stop - norm->h_start +1;
523         dev->crop_defrect.width  = norm->h_stop - norm->h_start +1;
524
525         dev->crop_bounds.top     = (norm->vbi_v_stop_0+1)*2;
526         dev->crop_defrect.top    = norm->video_v_start*2;
527         dev->crop_bounds.height  = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
528                 - dev->crop_bounds.top;
529         dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
530
531         dev->crop_current = dev->crop_defrect;
532
533         /* setup video decoder */
534         saa_writeb(SAA7134_INCR_DELAY,            0x08);
535         saa_writeb(SAA7134_ANALOG_IN_CTRL1,       0xc0 | mux);
536         saa_writeb(SAA7134_ANALOG_IN_CTRL2,       0x00);
537
538         saa_writeb(SAA7134_ANALOG_IN_CTRL3,       0x90);
539         saa_writeb(SAA7134_ANALOG_IN_CTRL4,       0x90);
540         saa_writeb(SAA7134_HSYNC_START,           0xeb);
541         saa_writeb(SAA7134_HSYNC_STOP,            0xe0);
542         saa_writeb(SAA7134_SOURCE_TIMING1,        norm->src_timing);
543
544         saa_writeb(SAA7134_SYNC_CTRL,             sync_control);
545         saa_writeb(SAA7134_LUMA_CTRL,             luma_control);
546         saa_writeb(SAA7134_DEC_LUMA_BRIGHT,       dev->ctl_bright);
547         saa_writeb(SAA7134_DEC_LUMA_CONTRAST,     dev->ctl_contrast);
548
549         saa_writeb(SAA7134_DEC_CHROMA_SATURATION, dev->ctl_saturation);
550         saa_writeb(SAA7134_DEC_CHROMA_HUE,        dev->ctl_hue);
551         saa_writeb(SAA7134_CHROMA_CTRL1,          norm->chroma_ctrl1);
552         saa_writeb(SAA7134_CHROMA_GAIN,           norm->chroma_gain);
553
554         saa_writeb(SAA7134_CHROMA_CTRL2,          norm->chroma_ctrl2);
555         saa_writeb(SAA7134_MODE_DELAY_CTRL,       0x00);
556
557         saa_writeb(SAA7134_ANALOG_ADC,            0x01);
558         saa_writeb(SAA7134_VGATE_START,           0x11);
559         saa_writeb(SAA7134_VGATE_STOP,            0xfe);
560         saa_writeb(SAA7134_MISC_VGATE_MSB,        norm->vgate_misc);
561         saa_writeb(SAA7134_RAW_DATA_GAIN,         0x40);
562         saa_writeb(SAA7134_RAW_DATA_OFFSET,       0x80);
563
564         saa7134_i2c_call_clients(dev,VIDIOC_S_STD,&norm->id);
565 }
566
567 static void video_mux(struct saa7134_dev *dev, int input)
568 {
569         dprintk("video input = %d [%s]\n",input,card_in(dev,input).name);
570         dev->ctl_input = input;
571         set_tvnorm(dev,dev->tvnorm);
572         saa7134_tvaudio_setinput(dev,&card_in(dev,input));
573 }
574
575 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
576 {
577         static const struct {
578                 int xpsc;
579                 int xacl;
580                 int xc2_1;
581                 int xdcg;
582                 int vpfy;
583         } vals[] = {
584                 /* XPSC XACL XC2_1 XDCG VPFY */
585                 {    1,   0,    0,    0,   0 },
586                 {    2,   2,    1,    2,   2 },
587                 {    3,   4,    1,    3,   2 },
588                 {    4,   8,    1,    4,   2 },
589                 {    5,   8,    1,    4,   2 },
590                 {    6,   8,    1,    4,   3 },
591                 {    7,   8,    1,    4,   3 },
592                 {    8,  15,    0,    4,   3 },
593                 {    9,  15,    0,    4,   3 },
594                 {   10,  16,    1,    5,   3 },
595         };
596         static const int count = ARRAY_SIZE(vals);
597         int i;
598
599         for (i = 0; i < count; i++)
600                 if (vals[i].xpsc == prescale)
601                         break;
602         if (i == count)
603                 return;
604
605         saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
606         saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
607         saa_writeb(SAA7134_LEVEL_CTRL(task),
608                    (vals[i].xc2_1 << 3) | (vals[i].xdcg));
609         saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
610                    (vals[i].vpfy << 2) | vals[i].vpfy);
611 }
612
613 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
614 {
615         int val,mirror;
616
617         saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale &  0xff);
618         saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
619
620         mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
621         if (yscale < 2048) {
622                 /* LPI */
623                 dprintk("yscale LPI yscale=%d\n",yscale);
624                 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
625                 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
626                 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
627         } else {
628                 /* ACM */
629                 val = 0x40 * 1024 / yscale;
630                 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
631                 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
632                 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
633                 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
634         }
635         saa_writeb(SAA7134_LUMA_BRIGHT(task),       0x80);
636 }
637
638 static void set_size(struct saa7134_dev *dev, int task,
639                      int width, int height, int interlace)
640 {
641         int prescale,xscale,yscale,y_even,y_odd;
642         int h_start, h_stop, v_start, v_stop;
643         int div = interlace ? 2 : 1;
644
645         /* setup video scaler */
646         h_start = dev->crop_current.left;
647         v_start = dev->crop_current.top/2;
648         h_stop  = (dev->crop_current.left + dev->crop_current.width -1);
649         v_stop  = (dev->crop_current.top + dev->crop_current.height -1)/2;
650
651         saa_writeb(SAA7134_VIDEO_H_START1(task), h_start &  0xff);
652         saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
653         saa_writeb(SAA7134_VIDEO_H_STOP1(task),  h_stop  &  0xff);
654         saa_writeb(SAA7134_VIDEO_H_STOP2(task),  h_stop  >> 8);
655         saa_writeb(SAA7134_VIDEO_V_START1(task), v_start &  0xff);
656         saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
657         saa_writeb(SAA7134_VIDEO_V_STOP1(task),  v_stop  &  0xff);
658         saa_writeb(SAA7134_VIDEO_V_STOP2(task),  v_stop  >> 8);
659
660         prescale = dev->crop_current.width / width;
661         if (0 == prescale)
662                 prescale = 1;
663         xscale = 1024 * dev->crop_current.width / prescale / width;
664         yscale = 512 * div * dev->crop_current.height / height;
665         dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
666         set_h_prescale(dev,task,prescale);
667         saa_writeb(SAA7134_H_SCALE_INC1(task),      xscale &  0xff);
668         saa_writeb(SAA7134_H_SCALE_INC2(task),      xscale >> 8);
669         set_v_scale(dev,task,yscale);
670
671         saa_writeb(SAA7134_VIDEO_PIXELS1(task),     width  & 0xff);
672         saa_writeb(SAA7134_VIDEO_PIXELS2(task),     width  >> 8);
673         saa_writeb(SAA7134_VIDEO_LINES1(task),      height/div & 0xff);
674         saa_writeb(SAA7134_VIDEO_LINES2(task),      height/div >> 8);
675
676         /* deinterlace y offsets */
677         y_odd  = dev->ctl_y_odd;
678         y_even = dev->ctl_y_even;
679         saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
680         saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
681         saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
682         saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
683 }
684
685 /* ------------------------------------------------------------------ */
686
687 struct cliplist {
688         __u16 position;
689         __u8  enable;
690         __u8  disable;
691 };
692
693 static void sort_cliplist(struct cliplist *cl, int entries)
694 {
695         struct cliplist swap;
696         int i,j,n;
697
698         for (i = entries-2; i >= 0; i--) {
699                 for (n = 0, j = 0; j <= i; j++) {
700                         if (cl[j].position > cl[j+1].position) {
701                                 swap = cl[j];
702                                 cl[j] = cl[j+1];
703                                 cl[j+1] = swap;
704                                 n++;
705                         }
706                 }
707                 if (0 == n)
708                         break;
709         }
710 }
711
712 static void set_cliplist(struct saa7134_dev *dev, int reg,
713                         struct cliplist *cl, int entries, char *name)
714 {
715         __u8 winbits = 0;
716         int i;
717
718         for (i = 0; i < entries; i++) {
719                 winbits |= cl[i].enable;
720                 winbits &= ~cl[i].disable;
721                 if (i < 15 && cl[i].position == cl[i+1].position)
722                         continue;
723                 saa_writeb(reg + 0, winbits);
724                 saa_writeb(reg + 2, cl[i].position & 0xff);
725                 saa_writeb(reg + 3, cl[i].position >> 8);
726                 dprintk("clip: %s winbits=%02x pos=%d\n",
727                         name,winbits,cl[i].position);
728                 reg += 8;
729         }
730         for (; reg < 0x400; reg += 8) {
731                 saa_writeb(reg+ 0, 0);
732                 saa_writeb(reg + 1, 0);
733                 saa_writeb(reg + 2, 0);
734                 saa_writeb(reg + 3, 0);
735         }
736 }
737
738 static int clip_range(int val)
739 {
740         if (val < 0)
741                 val = 0;
742         return val;
743 }
744
745 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
746                           int nclips, int interlace)
747 {
748         struct cliplist col[16], row[16];
749         int cols, rows, i;
750         int div = interlace ? 2 : 1;
751
752         memset(col,0,sizeof(col)); cols = 0;
753         memset(row,0,sizeof(row)); rows = 0;
754         for (i = 0; i < nclips && i < 8; i++) {
755                 col[cols].position = clip_range(clips[i].c.left);
756                 col[cols].enable   = (1 << i);
757                 cols++;
758                 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
759                 col[cols].disable  = (1 << i);
760                 cols++;
761                 row[rows].position = clip_range(clips[i].c.top / div);
762                 row[rows].enable   = (1 << i);
763                 rows++;
764                 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
765                                                 / div);
766                 row[rows].disable  = (1 << i);
767                 rows++;
768         }
769         sort_cliplist(col,cols);
770         sort_cliplist(row,rows);
771         set_cliplist(dev,0x380,col,cols,"cols");
772         set_cliplist(dev,0x384,row,rows,"rows");
773         return 0;
774 }
775
776 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
777 {
778         enum v4l2_field field;
779         int maxw, maxh;
780
781         if (NULL == dev->ovbuf.base)
782                 return -EINVAL;
783         if (NULL == dev->ovfmt)
784                 return -EINVAL;
785         if (win->w.width < 48 || win->w.height <  32)
786                 return -EINVAL;
787         if (win->clipcount > 2048)
788                 return -EINVAL;
789
790         field = win->field;
791         maxw  = dev->crop_current.width;
792         maxh  = dev->crop_current.height;
793
794         if (V4L2_FIELD_ANY == field) {
795                 field = (win->w.height > maxh/2)
796                         ? V4L2_FIELD_INTERLACED
797                         : V4L2_FIELD_TOP;
798         }
799         switch (field) {
800         case V4L2_FIELD_TOP:
801         case V4L2_FIELD_BOTTOM:
802                 maxh = maxh / 2;
803                 break;
804         case V4L2_FIELD_INTERLACED:
805                 break;
806         default:
807                 return -EINVAL;
808         }
809
810         win->field = field;
811         if (win->w.width > maxw)
812                 win->w.width = maxw;
813         if (win->w.height > maxh)
814                 win->w.height = maxh;
815         return 0;
816 }
817
818 static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
819 {
820         unsigned long base,control,bpl;
821         int err;
822
823         err = verify_preview(dev,&fh->win);
824         if (0 != err)
825                 return err;
826
827         dev->ovfield = fh->win.field;
828         dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
829                 fh->win.w.width,fh->win.w.height,
830                 fh->win.w.left,fh->win.w.top,
831                 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
832
833         /* setup window + clipping */
834         set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
835                  V4L2_FIELD_HAS_BOTH(dev->ovfield));
836         setup_clipping(dev,fh->clips,fh->nclips,
837                        V4L2_FIELD_HAS_BOTH(dev->ovfield));
838         if (dev->ovfmt->yuv)
839                 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
840         else
841                 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
842         saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
843
844         /* dma: setup channel 1 (= Video Task B) */
845         base  = (unsigned long)dev->ovbuf.base;
846         base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
847         base += dev->ovfmt->depth/8         * fh->win.w.left;
848         bpl   = dev->ovbuf.fmt.bytesperline;
849         control = SAA7134_RS_CONTROL_BURST_16;
850         if (dev->ovfmt->bswap)
851                 control |= SAA7134_RS_CONTROL_BSWAP;
852         if (dev->ovfmt->wswap)
853                 control |= SAA7134_RS_CONTROL_WSWAP;
854         if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
855                 saa_writel(SAA7134_RS_BA1(1),base);
856                 saa_writel(SAA7134_RS_BA2(1),base+bpl);
857                 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
858                 saa_writel(SAA7134_RS_CONTROL(1),control);
859         } else {
860                 saa_writel(SAA7134_RS_BA1(1),base);
861                 saa_writel(SAA7134_RS_BA2(1),base);
862                 saa_writel(SAA7134_RS_PITCH(1),bpl);
863                 saa_writel(SAA7134_RS_CONTROL(1),control);
864         }
865
866         /* start dma */
867         dev->ovenable = 1;
868         saa7134_set_dmabits(dev);
869
870         return 0;
871 }
872
873 static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
874 {
875         dev->ovenable = 0;
876         saa7134_set_dmabits(dev);
877         return 0;
878 }
879
880 /* ------------------------------------------------------------------ */
881
882 static int buffer_activate(struct saa7134_dev *dev,
883                            struct saa7134_buf *buf,
884                            struct saa7134_buf *next)
885 {
886         unsigned long base,control,bpl;
887         unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
888
889         dprintk("buffer_activate buf=%p\n",buf);
890         buf->vb.state = STATE_ACTIVE;
891         buf->top_seen = 0;
892
893         set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
894                  V4L2_FIELD_HAS_BOTH(buf->vb.field));
895         if (buf->fmt->yuv)
896                 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
897         else
898                 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
899         saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
900
901         /* DMA: setup channel 0 (= Video Task A0) */
902         base  = saa7134_buffer_base(buf);
903         if (buf->fmt->planar)
904                 bpl = buf->vb.width;
905         else
906                 bpl = (buf->vb.width * buf->fmt->depth) / 8;
907         control = SAA7134_RS_CONTROL_BURST_16 |
908                 SAA7134_RS_CONTROL_ME |
909                 (buf->pt->dma >> 12);
910         if (buf->fmt->bswap)
911                 control |= SAA7134_RS_CONTROL_BSWAP;
912         if (buf->fmt->wswap)
913                 control |= SAA7134_RS_CONTROL_WSWAP;
914         if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
915                 /* interlaced */
916                 saa_writel(SAA7134_RS_BA1(0),base);
917                 saa_writel(SAA7134_RS_BA2(0),base+bpl);
918                 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
919         } else {
920                 /* non-interlaced */
921                 saa_writel(SAA7134_RS_BA1(0),base);
922                 saa_writel(SAA7134_RS_BA2(0),base);
923                 saa_writel(SAA7134_RS_PITCH(0),bpl);
924         }
925         saa_writel(SAA7134_RS_CONTROL(0),control);
926
927         if (buf->fmt->planar) {
928                 /* DMA: setup channel 4+5 (= planar task A) */
929                 bpl_uv   = bpl >> buf->fmt->hshift;
930                 lines_uv = buf->vb.height >> buf->fmt->vshift;
931                 base2    = base + bpl * buf->vb.height;
932                 base3    = base2 + bpl_uv * lines_uv;
933                 if (buf->fmt->uvswap)
934                         tmp = base2, base2 = base3, base3 = tmp;
935                 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
936                         bpl_uv,lines_uv,base2,base3);
937                 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
938                         /* interlaced */
939                         saa_writel(SAA7134_RS_BA1(4),base2);
940                         saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
941                         saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
942                         saa_writel(SAA7134_RS_BA1(5),base3);
943                         saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
944                         saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
945                 } else {
946                         /* non-interlaced */
947                         saa_writel(SAA7134_RS_BA1(4),base2);
948                         saa_writel(SAA7134_RS_BA2(4),base2);
949                         saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
950                         saa_writel(SAA7134_RS_BA1(5),base3);
951                         saa_writel(SAA7134_RS_BA2(5),base3);
952                         saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
953                 }
954                 saa_writel(SAA7134_RS_CONTROL(4),control);
955                 saa_writel(SAA7134_RS_CONTROL(5),control);
956         }
957
958         /* start DMA */
959         saa7134_set_dmabits(dev);
960         mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
961         return 0;
962 }
963
964 static int buffer_prepare(struct videobuf_queue *q,
965                           struct videobuf_buffer *vb,
966                           enum v4l2_field field)
967 {
968         struct saa7134_fh *fh = q->priv_data;
969         struct saa7134_dev *dev = fh->dev;
970         struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
971         unsigned int size;
972         int err;
973
974         /* sanity checks */
975         if (NULL == fh->fmt)
976                 return -EINVAL;
977         if (fh->width    < 48 ||
978             fh->height   < 32 ||
979             fh->width/4  > dev->crop_current.width  ||
980             fh->height/4 > dev->crop_current.height ||
981             fh->width    > dev->crop_bounds.width  ||
982             fh->height   > dev->crop_bounds.height)
983                 return -EINVAL;
984         size = (fh->width * fh->height * fh->fmt->depth) >> 3;
985         if (0 != buf->vb.baddr  &&  buf->vb.bsize < size)
986                 return -EINVAL;
987
988         dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
989                 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
990                 fh->fmt->name);
991         if (buf->vb.width  != fh->width  ||
992             buf->vb.height != fh->height ||
993             buf->vb.size   != size       ||
994             buf->vb.field  != field      ||
995             buf->fmt       != fh->fmt) {
996                 saa7134_dma_free(dev,buf);
997         }
998
999         if (STATE_NEEDS_INIT == buf->vb.state) {
1000                 buf->vb.width  = fh->width;
1001                 buf->vb.height = fh->height;
1002                 buf->vb.size   = size;
1003                 buf->vb.field  = field;
1004                 buf->fmt       = fh->fmt;
1005                 buf->pt        = &fh->pt_cap;
1006
1007                 err = videobuf_iolock(dev->pci,&buf->vb,&dev->ovbuf);
1008                 if (err)
1009                         goto oops;
1010                 err = saa7134_pgtable_build(dev->pci,buf->pt,
1011                                             buf->vb.dma.sglist,
1012                                             buf->vb.dma.sglen,
1013                                             saa7134_buffer_startpage(buf));
1014                 if (err)
1015                         goto oops;
1016         }
1017         buf->vb.state = STATE_PREPARED;
1018         buf->activate = buffer_activate;
1019         return 0;
1020
1021  oops:
1022         saa7134_dma_free(dev,buf);
1023         return err;
1024 }
1025
1026 static int
1027 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1028 {
1029         struct saa7134_fh *fh = q->priv_data;
1030
1031         *size = fh->fmt->depth * fh->width * fh->height >> 3;
1032         if (0 == *count)
1033                 *count = gbuffers;
1034         *count = saa7134_buffer_count(*size,*count);
1035         return 0;
1036 }
1037
1038 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1039 {
1040         struct saa7134_fh *fh = q->priv_data;
1041         struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1042
1043         saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1044 }
1045
1046 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1047 {
1048         struct saa7134_fh *fh = q->priv_data;
1049         struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1050
1051         saa7134_dma_free(fh->dev,buf);
1052 }
1053
1054 static struct videobuf_queue_ops video_qops = {
1055         .buf_setup    = buffer_setup,
1056         .buf_prepare  = buffer_prepare,
1057         .buf_queue    = buffer_queue,
1058         .buf_release  = buffer_release,
1059 };
1060
1061 /* ------------------------------------------------------------------ */
1062
1063 static int get_control(struct saa7134_dev *dev, struct v4l2_control *c)
1064 {
1065         const struct v4l2_queryctrl* ctrl;
1066
1067         ctrl = ctrl_by_id(c->id);
1068         if (NULL == ctrl)
1069                 return -EINVAL;
1070         switch (c->id) {
1071         case V4L2_CID_BRIGHTNESS:
1072                 c->value = dev->ctl_bright;
1073                 break;
1074         case V4L2_CID_HUE:
1075                 c->value = dev->ctl_hue;
1076                 break;
1077         case V4L2_CID_CONTRAST:
1078                 c->value = dev->ctl_contrast;
1079                 break;
1080         case V4L2_CID_SATURATION:
1081                 c->value = dev->ctl_saturation;
1082                 break;
1083         case V4L2_CID_AUDIO_MUTE:
1084                 c->value = dev->ctl_mute;
1085                 break;
1086         case V4L2_CID_AUDIO_VOLUME:
1087                 c->value = dev->ctl_volume;
1088                 break;
1089         case V4L2_CID_PRIVATE_INVERT:
1090                 c->value = dev->ctl_invert;
1091                 break;
1092         case V4L2_CID_HFLIP:
1093                 c->value = dev->ctl_mirror;
1094                 break;
1095         case V4L2_CID_PRIVATE_Y_EVEN:
1096                 c->value = dev->ctl_y_even;
1097                 break;
1098         case V4L2_CID_PRIVATE_Y_ODD:
1099                 c->value = dev->ctl_y_odd;
1100                 break;
1101         case V4L2_CID_PRIVATE_AUTOMUTE:
1102                 c->value = dev->ctl_automute;
1103                 break;
1104         default:
1105                 return -EINVAL;
1106         }
1107         return 0;
1108 }
1109
1110 static int set_control(struct saa7134_dev *dev, struct saa7134_fh *fh,
1111                        struct v4l2_control *c)
1112 {
1113         const struct v4l2_queryctrl* ctrl;
1114         unsigned long flags;
1115         int restart_overlay = 0;
1116
1117         ctrl = ctrl_by_id(c->id);
1118         if (NULL == ctrl)
1119                 return -EINVAL;
1120         dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1121         switch (ctrl->type) {
1122         case V4L2_CTRL_TYPE_BOOLEAN:
1123         case V4L2_CTRL_TYPE_MENU:
1124         case V4L2_CTRL_TYPE_INTEGER:
1125                 if (c->value < ctrl->minimum)
1126                         c->value = ctrl->minimum;
1127                 if (c->value > ctrl->maximum)
1128                         c->value = ctrl->maximum;
1129                 break;
1130         default:
1131                 /* nothing */;
1132         };
1133         switch (c->id) {
1134         case V4L2_CID_BRIGHTNESS:
1135                 dev->ctl_bright = c->value;
1136                 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1137                 break;
1138         case V4L2_CID_HUE:
1139                 dev->ctl_hue = c->value;
1140                 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1141                 break;
1142         case V4L2_CID_CONTRAST:
1143                 dev->ctl_contrast = c->value;
1144                 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1145                            dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1146                 break;
1147         case V4L2_CID_SATURATION:
1148                 dev->ctl_saturation = c->value;
1149                 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1150                            dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1151                 break;
1152         case V4L2_CID_AUDIO_MUTE:
1153                 dev->ctl_mute = c->value;
1154                 saa7134_tvaudio_setmute(dev);
1155                 break;
1156         case V4L2_CID_AUDIO_VOLUME:
1157                 dev->ctl_volume = c->value;
1158                 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1159                 break;
1160         case V4L2_CID_PRIVATE_INVERT:
1161                 dev->ctl_invert = c->value;
1162                 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1163                            dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1164                 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1165                            dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1166                 break;
1167         case V4L2_CID_HFLIP:
1168                 dev->ctl_mirror = c->value;
1169                 restart_overlay = 1;
1170                 break;
1171         case V4L2_CID_PRIVATE_Y_EVEN:
1172                 dev->ctl_y_even = c->value;
1173                 restart_overlay = 1;
1174                 break;
1175         case V4L2_CID_PRIVATE_Y_ODD:
1176                 dev->ctl_y_odd = c->value;
1177                 restart_overlay = 1;
1178                 break;
1179         case V4L2_CID_PRIVATE_AUTOMUTE:
1180                 dev->ctl_automute = c->value;
1181                 if (dev->tda9887_conf) {
1182                         if (dev->ctl_automute)
1183                                 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1184                         else
1185                                 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1186                         saa7134_i2c_call_clients(dev, TDA9887_SET_CONFIG,
1187                                                  &dev->tda9887_conf);
1188                 }
1189                 break;
1190         default:
1191                 return -EINVAL;
1192         }
1193         if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1194                 spin_lock_irqsave(&dev->slock,flags);
1195                 stop_preview(dev,fh);
1196                 start_preview(dev,fh);
1197                 spin_unlock_irqrestore(&dev->slock,flags);
1198         }
1199         return 0;
1200 }
1201
1202 /* ------------------------------------------------------------------ */
1203
1204 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1205 {
1206         struct videobuf_queue* q = NULL;
1207
1208         switch (fh->type) {
1209         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1210                 q = &fh->cap;
1211                 break;
1212         case V4L2_BUF_TYPE_VBI_CAPTURE:
1213                 q = &fh->vbi;
1214                 break;
1215         default:
1216                 BUG();
1217         }
1218         return q;
1219 }
1220
1221 static int saa7134_resource(struct saa7134_fh *fh)
1222 {
1223         int res = 0;
1224
1225         switch (fh->type) {
1226         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1227                 res = RESOURCE_VIDEO;
1228                 break;
1229         case V4L2_BUF_TYPE_VBI_CAPTURE:
1230                 res = RESOURCE_VBI;
1231                 break;
1232         default:
1233                 BUG();
1234         }
1235         return res;
1236 }
1237
1238 static int video_open(struct inode *inode, struct file *file)
1239 {
1240         int minor = iminor(inode);
1241         struct saa7134_dev *h,*dev = NULL;
1242         struct saa7134_fh *fh;
1243         struct list_head *list;
1244         enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1245         int radio = 0;
1246         list_for_each(list,&saa7134_devlist) {
1247                 h = list_entry(list, struct saa7134_dev, devlist);
1248                 if (h->video_dev && (h->video_dev->minor == minor))
1249                         dev = h;
1250                 if (h->radio_dev && (h->radio_dev->minor == minor)) {
1251                         radio = 1;
1252                         dev = h;
1253                 }
1254                 if (h->vbi_dev && (h->vbi_dev->minor == minor)) {
1255                         type = V4L2_BUF_TYPE_VBI_CAPTURE;
1256                         dev = h;
1257                 }
1258         }
1259         if (NULL == dev)
1260                 return -ENODEV;
1261
1262         dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1263                 v4l2_type_names[type]);
1264
1265         /* allocate + initialize per filehandle data */
1266         fh = kmalloc(sizeof(*fh),GFP_KERNEL);
1267         if (NULL == fh)
1268                 return -ENOMEM;
1269         memset(fh,0,sizeof(*fh));
1270         file->private_data = fh;
1271         fh->dev      = dev;
1272         fh->radio    = radio;
1273         fh->type     = type;
1274         fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1275         fh->width    = 720;
1276         fh->height   = 576;
1277         v4l2_prio_open(&dev->prio,&fh->prio);
1278
1279         videobuf_queue_init(&fh->cap, &video_qops,
1280                             dev->pci, &dev->slock,
1281                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
1282                             V4L2_FIELD_INTERLACED,
1283                             sizeof(struct saa7134_buf),
1284                             fh);
1285         videobuf_queue_init(&fh->vbi, &saa7134_vbi_qops,
1286                             dev->pci, &dev->slock,
1287                             V4L2_BUF_TYPE_VBI_CAPTURE,
1288                             V4L2_FIELD_SEQ_TB,
1289                             sizeof(struct saa7134_buf),
1290                             fh);
1291         saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1292         saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1293
1294         if (fh->radio) {
1295                 /* switch to radio mode */
1296                 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1297                 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO, NULL);
1298         } else {
1299                 /* switch to video/vbi mode */
1300                 video_mux(dev,dev->ctl_input);
1301         }
1302         return 0;
1303 }
1304
1305 static ssize_t
1306 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1307 {
1308         struct saa7134_fh *fh = file->private_data;
1309
1310         switch (fh->type) {
1311         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1312                 if (res_locked(fh->dev,RESOURCE_VIDEO))
1313                         return -EBUSY;
1314                 return videobuf_read_one(saa7134_queue(fh),
1315                                          data, count, ppos,
1316                                          file->f_flags & O_NONBLOCK);
1317         case V4L2_BUF_TYPE_VBI_CAPTURE:
1318                 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1319                         return -EBUSY;
1320                 return videobuf_read_stream(saa7134_queue(fh),
1321                                             data, count, ppos, 1,
1322                                             file->f_flags & O_NONBLOCK);
1323                 break;
1324         default:
1325                 BUG();
1326                 return 0;
1327         }
1328 }
1329
1330 static unsigned int
1331 video_poll(struct file *file, struct poll_table_struct *wait)
1332 {
1333         struct saa7134_fh *fh = file->private_data;
1334         struct videobuf_buffer *buf = NULL;
1335
1336         if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1337                 return videobuf_poll_stream(file, &fh->vbi, wait);
1338
1339         if (res_check(fh,RESOURCE_VIDEO)) {
1340                 if (!list_empty(&fh->cap.stream))
1341                         buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1342         } else {
1343                 down(&fh->cap.lock);
1344                 if (UNSET == fh->cap.read_off) {
1345                         /* need to capture a new frame */
1346                         if (res_locked(fh->dev,RESOURCE_VIDEO)) {
1347                                 up(&fh->cap.lock);
1348                                 return POLLERR;
1349                         }
1350                         if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field)) {
1351                                 up(&fh->cap.lock);
1352                                 return POLLERR;
1353                         }
1354                         fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1355                         fh->cap.read_off = 0;
1356                 }
1357                 up(&fh->cap.lock);
1358                 buf = fh->cap.read_buf;
1359         }
1360
1361         if (!buf)
1362                 return POLLERR;
1363
1364         poll_wait(file, &buf->done, wait);
1365         if (buf->state == STATE_DONE ||
1366             buf->state == STATE_ERROR)
1367                 return POLLIN|POLLRDNORM;
1368         return 0;
1369 }
1370
1371 static int video_release(struct inode *inode, struct file *file)
1372 {
1373         struct saa7134_fh  *fh  = file->private_data;
1374         struct saa7134_dev *dev = fh->dev;
1375         unsigned long flags;
1376
1377         /* turn off overlay */
1378         if (res_check(fh, RESOURCE_OVERLAY)) {
1379                 spin_lock_irqsave(&dev->slock,flags);
1380                 stop_preview(dev,fh);
1381                 spin_unlock_irqrestore(&dev->slock,flags);
1382                 res_free(dev,fh,RESOURCE_OVERLAY);
1383         }
1384
1385         /* stop video capture */
1386         if (res_check(fh, RESOURCE_VIDEO)) {
1387                 videobuf_streamoff(&fh->cap);
1388                 res_free(dev,fh,RESOURCE_VIDEO);
1389         }
1390         if (fh->cap.read_buf) {
1391                 buffer_release(&fh->cap,fh->cap.read_buf);
1392                 kfree(fh->cap.read_buf);
1393         }
1394
1395         /* stop vbi capture */
1396         if (res_check(fh, RESOURCE_VBI)) {
1397                 if (fh->vbi.streaming)
1398                         videobuf_streamoff(&fh->vbi);
1399                 if (fh->vbi.reading)
1400                         videobuf_read_stop(&fh->vbi);
1401                 res_free(dev,fh,RESOURCE_VBI);
1402         }
1403
1404         /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1405         saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1406         saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1407         saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1408         saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1409
1410         saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1411
1412         /* free stuff */
1413         videobuf_mmap_free(&fh->cap);
1414         videobuf_mmap_free(&fh->vbi);
1415         saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1416         saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1417
1418         v4l2_prio_close(&dev->prio,&fh->prio);
1419         file->private_data = NULL;
1420         kfree(fh);
1421         return 0;
1422 }
1423
1424 static int
1425 video_mmap(struct file *file, struct vm_area_struct * vma)
1426 {
1427         struct saa7134_fh *fh = file->private_data;
1428
1429         return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1430 }
1431
1432 /* ------------------------------------------------------------------ */
1433
1434 static void saa7134_vbi_fmt(struct saa7134_dev *dev, struct v4l2_format *f)
1435 {
1436         struct saa7134_tvnorm *norm = dev->tvnorm;
1437
1438         f->fmt.vbi.sampling_rate = 6750000 * 4;
1439         f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1440         f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1441         f->fmt.vbi.offset = 64 * 4;
1442         f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1443         f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1444         f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1445         f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1446         f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1447
1448 }
1449
1450 static int saa7134_g_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1451                          struct v4l2_format *f)
1452 {
1453         switch (f->type) {
1454         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1455                 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1456                 f->fmt.pix.width        = fh->width;
1457                 f->fmt.pix.height       = fh->height;
1458                 f->fmt.pix.field        = fh->cap.field;
1459                 f->fmt.pix.pixelformat  = fh->fmt->fourcc;
1460                 f->fmt.pix.bytesperline =
1461                         (f->fmt.pix.width * fh->fmt->depth) >> 3;
1462                 f->fmt.pix.sizeimage =
1463                         f->fmt.pix.height * f->fmt.pix.bytesperline;
1464                 return 0;
1465         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1466                 f->fmt.win = fh->win;
1467                 return 0;
1468         case V4L2_BUF_TYPE_VBI_CAPTURE:
1469                 saa7134_vbi_fmt(dev,f);
1470                 return 0;
1471         default:
1472                 return -EINVAL;
1473         }
1474 }
1475
1476 static int saa7134_try_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1477                            struct v4l2_format *f)
1478 {
1479         int err;
1480
1481         switch (f->type) {
1482         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1483         {
1484                 struct saa7134_format *fmt;
1485                 enum v4l2_field field;
1486                 unsigned int maxw, maxh;
1487
1488                 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1489                 if (NULL == fmt)
1490                         return -EINVAL;
1491
1492                 field = f->fmt.pix.field;
1493                 maxw  = min(dev->crop_current.width*4,  dev->crop_bounds.width);
1494                 maxh  = min(dev->crop_current.height*4, dev->crop_bounds.height);
1495
1496                 if (V4L2_FIELD_ANY == field) {
1497                         field = (f->fmt.pix.height > maxh/2)
1498                                 ? V4L2_FIELD_INTERLACED
1499                                 : V4L2_FIELD_BOTTOM;
1500                 }
1501                 switch (field) {
1502                 case V4L2_FIELD_TOP:
1503                 case V4L2_FIELD_BOTTOM:
1504                         maxh = maxh / 2;
1505                         break;
1506                 case V4L2_FIELD_INTERLACED:
1507                         break;
1508                 default:
1509                         return -EINVAL;
1510                 }
1511
1512                 f->fmt.pix.field = field;
1513                 if (f->fmt.pix.width  < 48)
1514                         f->fmt.pix.width  = 48;
1515                 if (f->fmt.pix.height < 32)
1516                         f->fmt.pix.height = 32;
1517                 if (f->fmt.pix.width > maxw)
1518                         f->fmt.pix.width = maxw;
1519                 if (f->fmt.pix.height > maxh)
1520                         f->fmt.pix.height = maxh;
1521                 f->fmt.pix.width &= ~0x03;
1522                 f->fmt.pix.bytesperline =
1523                         (f->fmt.pix.width * fmt->depth) >> 3;
1524                 f->fmt.pix.sizeimage =
1525                         f->fmt.pix.height * f->fmt.pix.bytesperline;
1526
1527                 return 0;
1528         }
1529         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1530                 err = verify_preview(dev,&f->fmt.win);
1531                 if (0 != err)
1532                         return err;
1533                 return 0;
1534         case V4L2_BUF_TYPE_VBI_CAPTURE:
1535                 saa7134_vbi_fmt(dev,f);
1536                 return 0;
1537         default:
1538                 return -EINVAL;
1539         }
1540 }
1541
1542 static int saa7134_s_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1543                          struct v4l2_format *f)
1544 {
1545         unsigned long flags;
1546         int err;
1547
1548         switch (f->type) {
1549         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1550                 err = saa7134_try_fmt(dev,fh,f);
1551                 if (0 != err)
1552                         return err;
1553
1554                 fh->fmt       = format_by_fourcc(f->fmt.pix.pixelformat);
1555                 fh->width     = f->fmt.pix.width;
1556                 fh->height    = f->fmt.pix.height;
1557                 fh->cap.field = f->fmt.pix.field;
1558                 return 0;
1559         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1560                 err = verify_preview(dev,&f->fmt.win);
1561                 if (0 != err)
1562                         return err;
1563
1564                 down(&dev->lock);
1565                 fh->win    = f->fmt.win;
1566                 fh->nclips = f->fmt.win.clipcount;
1567                 if (fh->nclips > 8)
1568                         fh->nclips = 8;
1569                 if (copy_from_user(fh->clips,f->fmt.win.clips,
1570                                    sizeof(struct v4l2_clip)*fh->nclips)) {
1571                         up(&dev->lock);
1572                         return -EFAULT;
1573                 }
1574
1575                 if (res_check(fh, RESOURCE_OVERLAY)) {
1576                         spin_lock_irqsave(&dev->slock,flags);
1577                         stop_preview(dev,fh);
1578                         start_preview(dev,fh);
1579                         spin_unlock_irqrestore(&dev->slock,flags);
1580                 }
1581                 up(&dev->lock);
1582                 return 0;
1583         case V4L2_BUF_TYPE_VBI_CAPTURE:
1584                 saa7134_vbi_fmt(dev,f);
1585                 return 0;
1586         default:
1587                 return -EINVAL;
1588         }
1589 }
1590
1591 int saa7134_common_ioctl(struct saa7134_dev *dev,
1592                          unsigned int cmd, void *arg)
1593 {
1594         int err;
1595
1596         switch (cmd) {
1597         case VIDIOC_QUERYCTRL:
1598         {
1599                 const struct v4l2_queryctrl *ctrl;
1600                 struct v4l2_queryctrl *c = arg;
1601
1602                 if ((c->id <  V4L2_CID_BASE ||
1603                      c->id >= V4L2_CID_LASTP1) &&
1604                     (c->id <  V4L2_CID_PRIVATE_BASE ||
1605                      c->id >= V4L2_CID_PRIVATE_LASTP1))
1606                         return -EINVAL;
1607                 ctrl = ctrl_by_id(c->id);
1608                 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1609                 return 0;
1610         }
1611         case VIDIOC_G_CTRL:
1612                 return get_control(dev,arg);
1613         case VIDIOC_S_CTRL:
1614         {
1615                 down(&dev->lock);
1616                 err = set_control(dev,NULL,arg);
1617                 up(&dev->lock);
1618                 return err;
1619         }
1620         /* --- input switching --------------------------------------- */
1621         case VIDIOC_ENUMINPUT:
1622         {
1623                 struct v4l2_input *i = arg;
1624                 unsigned int n;
1625
1626                 n = i->index;
1627                 if (n >= SAA7134_INPUT_MAX)
1628                         return -EINVAL;
1629                 if (NULL == card_in(dev,i->index).name)
1630                         return -EINVAL;
1631                 memset(i,0,sizeof(*i));
1632                 i->index = n;
1633                 i->type  = V4L2_INPUT_TYPE_CAMERA;
1634                 strcpy(i->name,card_in(dev,n).name);
1635                 if (card_in(dev,n).tv)
1636                         i->type = V4L2_INPUT_TYPE_TUNER;
1637                 i->audioset = 1;
1638                 if (n == dev->ctl_input) {
1639                         int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1640                         int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1641
1642                         if (0 != (v1 & 0x40))
1643                                 i->status |= V4L2_IN_ST_NO_H_LOCK;
1644                         if (0 != (v2 & 0x40))
1645                                 i->status |= V4L2_IN_ST_NO_SYNC;
1646                         if (0 != (v2 & 0x0e))
1647                                 i->status |= V4L2_IN_ST_MACROVISION;
1648                 }
1649                 for (n = 0; n < TVNORMS; n++)
1650                         i->std |= tvnorms[n].id;
1651                 return 0;
1652         }
1653         case VIDIOC_G_INPUT:
1654         {
1655                 int *i = arg;
1656                 *i = dev->ctl_input;
1657                 return 0;
1658         }
1659         case VIDIOC_S_INPUT:
1660         {
1661                 int *i = arg;
1662
1663                 if (*i < 0  ||  *i >= SAA7134_INPUT_MAX)
1664                         return -EINVAL;
1665                 if (NULL == card_in(dev,*i).name)
1666                         return -EINVAL;
1667                 down(&dev->lock);
1668                 video_mux(dev,*i);
1669                 up(&dev->lock);
1670                 return 0;
1671         }
1672
1673         }
1674         return 0;
1675 }
1676 EXPORT_SYMBOL(saa7134_common_ioctl);
1677
1678 /*
1679  * This function is _not_ called directly, but from
1680  * video_generic_ioctl (and maybe others).  userspace
1681  * copying is done already, arg is a kernel pointer.
1682  */
1683 static int video_do_ioctl(struct inode *inode, struct file *file,
1684                           unsigned int cmd, void *arg)
1685 {
1686         struct saa7134_fh *fh = file->private_data;
1687         struct saa7134_dev *dev = fh->dev;
1688         unsigned long flags;
1689         int err;
1690
1691         if (video_debug > 1)
1692                 saa7134_print_ioctl(dev->name,cmd);
1693
1694         switch (cmd) {
1695         case VIDIOC_S_CTRL:
1696         case VIDIOC_S_STD:
1697         case VIDIOC_S_INPUT:
1698         case VIDIOC_S_TUNER:
1699         case VIDIOC_S_FREQUENCY:
1700                 err = v4l2_prio_check(&dev->prio,&fh->prio);
1701                 if (0 != err)
1702                         return err;
1703         }
1704
1705         switch (cmd) {
1706         case VIDIOC_QUERYCAP:
1707         {
1708                 struct v4l2_capability *cap = arg;
1709                 unsigned int tuner_type = dev->tuner_type;
1710
1711                 memset(cap,0,sizeof(*cap));
1712                 strcpy(cap->driver, "saa7134");
1713                 strlcpy(cap->card, saa7134_boards[dev->board].name,
1714                         sizeof(cap->card));
1715                 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1716                 cap->version = SAA7134_VERSION_CODE;
1717                 cap->capabilities =
1718                         V4L2_CAP_VIDEO_CAPTURE |
1719                         V4L2_CAP_VIDEO_OVERLAY |
1720                         V4L2_CAP_VBI_CAPTURE |
1721                         V4L2_CAP_READWRITE |
1722                         V4L2_CAP_STREAMING |
1723                         V4L2_CAP_TUNER;
1724
1725                 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1726                         cap->capabilities &= ~V4L2_CAP_TUNER;
1727
1728                 return 0;
1729         }
1730
1731         /* --- tv standards ------------------------------------------ */
1732         case VIDIOC_ENUMSTD:
1733         {
1734                 struct v4l2_standard *e = arg;
1735                 unsigned int i;
1736
1737                 i = e->index;
1738                 if (i >= TVNORMS)
1739                         return -EINVAL;
1740                 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1741                                                tvnorms[e->index].name);
1742                 e->index = i;
1743                 if (err < 0)
1744                         return err;
1745                 return 0;
1746         }
1747         case VIDIOC_G_STD:
1748         {
1749                 v4l2_std_id *id = arg;
1750
1751                 *id = dev->tvnorm->id;
1752                 return 0;
1753         }
1754         case VIDIOC_S_STD:
1755         {
1756                 v4l2_std_id *id = arg;
1757                 unsigned int i;
1758
1759                 for (i = 0; i < TVNORMS; i++)
1760                         if (*id == tvnorms[i].id)
1761                                 break;
1762                 if (i == TVNORMS)
1763                         for (i = 0; i < TVNORMS; i++)
1764                                 if (*id & tvnorms[i].id)
1765                                         break;
1766                 if (i == TVNORMS)
1767                         return -EINVAL;
1768
1769                 down(&dev->lock);
1770                 if (res_check(fh, RESOURCE_OVERLAY)) {
1771                         spin_lock_irqsave(&dev->slock,flags);
1772                         stop_preview(dev,fh);
1773                         set_tvnorm(dev,&tvnorms[i]);
1774                         start_preview(dev,fh);
1775                         spin_unlock_irqrestore(&dev->slock,flags);
1776                 } else
1777                         set_tvnorm(dev,&tvnorms[i]);
1778                 saa7134_tvaudio_do_scan(dev);
1779                 up(&dev->lock);
1780                 return 0;
1781         }
1782
1783         case VIDIOC_CROPCAP:
1784         {
1785                 struct v4l2_cropcap *cap = arg;
1786
1787                 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1788                     cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1789                         return -EINVAL;
1790                 cap->bounds  = dev->crop_bounds;
1791                 cap->defrect = dev->crop_defrect;
1792                 cap->pixelaspect.numerator   = 1;
1793                 cap->pixelaspect.denominator = 1;
1794                 if (dev->tvnorm->id & V4L2_STD_525_60) {
1795                         cap->pixelaspect.numerator   = 11;
1796                         cap->pixelaspect.denominator = 10;
1797                 }
1798                 if (dev->tvnorm->id & V4L2_STD_625_50) {
1799                         cap->pixelaspect.numerator   = 54;
1800                         cap->pixelaspect.denominator = 59;
1801                 }
1802                 return 0;
1803         }
1804
1805         case VIDIOC_G_CROP:
1806         {
1807                 struct v4l2_crop * crop = arg;
1808
1809                 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1810                     crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1811                         return -EINVAL;
1812                 crop->c = dev->crop_current;
1813                 return 0;
1814         }
1815         case VIDIOC_S_CROP:
1816         {
1817                 struct v4l2_crop *crop = arg;
1818                 struct v4l2_rect *b = &dev->crop_bounds;
1819
1820                 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1821                     crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1822                         return -EINVAL;
1823                 if (crop->c.height < 0)
1824                         return -EINVAL;
1825                 if (crop->c.width < 0)
1826                         return -EINVAL;
1827
1828                 if (res_locked(fh->dev,RESOURCE_OVERLAY))
1829                         return -EBUSY;
1830                 if (res_locked(fh->dev,RESOURCE_VIDEO))
1831                         return -EBUSY;
1832
1833                 if (crop->c.top < b->top)
1834                         crop->c.top = b->top;
1835                 if (crop->c.top > b->top + b->height)
1836                         crop->c.top = b->top + b->height;
1837                 if (crop->c.height > b->top - crop->c.top + b->height)
1838                         crop->c.height = b->top - crop->c.top + b->height;
1839
1840                 if (crop->c.left < b->left)
1841                         crop->c.left = b->left;
1842                 if (crop->c.left > b->left + b->width)
1843                         crop->c.left = b->left + b->width;
1844                 if (crop->c.width > b->left - crop->c.left + b->width)
1845                         crop->c.width = b->left - crop->c.left + b->width;
1846
1847                 dev->crop_current = crop->c;
1848                 return 0;
1849         }
1850
1851         /* --- tuner ioctls ------------------------------------------ */
1852         case VIDIOC_G_TUNER:
1853         {
1854                 struct v4l2_tuner *t = arg;
1855                 int n;
1856
1857                 if (0 != t->index)
1858                         return -EINVAL;
1859                 memset(t,0,sizeof(*t));
1860                 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1861                         if (card_in(dev,n).tv)
1862                                 break;
1863                 if (NULL != card_in(dev,n).name) {
1864                         strcpy(t->name, "Television");
1865                         t->type = V4L2_TUNER_ANALOG_TV;
1866                         t->capability = V4L2_TUNER_CAP_NORM |
1867                                 V4L2_TUNER_CAP_STEREO |
1868                                 V4L2_TUNER_CAP_LANG1 |
1869                                 V4L2_TUNER_CAP_LANG2;
1870                         t->rangehigh = 0xffffffffUL;
1871                         t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1872                         t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1873                 }
1874                 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1875                         t->signal = 0xffff;
1876                 return 0;
1877         }
1878         case VIDIOC_S_TUNER:
1879         {
1880                 struct v4l2_tuner *t = arg;
1881                 int rx,mode;
1882
1883                 mode = dev->thread.mode;
1884                 if (UNSET == mode) {
1885                         rx   = saa7134_tvaudio_getstereo(dev);
1886                         mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1887                 }
1888                 if (mode != t->audmode) {
1889                         dev->thread.mode = t->audmode;
1890                 }
1891                 return 0;
1892         }
1893         case VIDIOC_G_FREQUENCY:
1894         {
1895                 struct v4l2_frequency *f = arg;
1896
1897                 memset(f,0,sizeof(*f));
1898                 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1899                 f->frequency = dev->ctl_freq;
1900                 return 0;
1901         }
1902         case VIDIOC_S_FREQUENCY:
1903         {
1904                 struct v4l2_frequency *f = arg;
1905
1906                 if (0 != f->tuner)
1907                         return -EINVAL;
1908                 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1909                         return -EINVAL;
1910                 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1911                         return -EINVAL;
1912                 down(&dev->lock);
1913                 dev->ctl_freq = f->frequency;
1914
1915                 saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f);
1916
1917                 saa7134_tvaudio_do_scan(dev);
1918                 up(&dev->lock);
1919                 return 0;
1920         }
1921
1922         /* --- control ioctls ---------------------------------------- */
1923         case VIDIOC_ENUMINPUT:
1924         case VIDIOC_G_INPUT:
1925         case VIDIOC_S_INPUT:
1926         case VIDIOC_QUERYCTRL:
1927         case VIDIOC_G_CTRL:
1928         case VIDIOC_S_CTRL:
1929                 return saa7134_common_ioctl(dev, cmd, arg);
1930
1931         case VIDIOC_G_AUDIO:
1932         {
1933                 struct v4l2_audio *a = arg;
1934
1935                 memset(a,0,sizeof(*a));
1936                 strcpy(a->name,"audio");
1937                 return 0;
1938         }
1939         case VIDIOC_S_AUDIO:
1940                 return 0;
1941         case VIDIOC_G_PARM:
1942         {
1943                 struct v4l2_captureparm *parm = arg;
1944                 memset(parm,0,sizeof(*parm));
1945                 return 0;
1946         }
1947
1948         case VIDIOC_G_PRIORITY:
1949         {
1950                 enum v4l2_priority *p = arg;
1951
1952                 *p = v4l2_prio_max(&dev->prio);
1953                 return 0;
1954         }
1955         case VIDIOC_S_PRIORITY:
1956         {
1957                 enum v4l2_priority *prio = arg;
1958
1959                 return v4l2_prio_change(&dev->prio, &fh->prio, *prio);
1960         }
1961
1962         /* --- preview ioctls ---------------------------------------- */
1963         case VIDIOC_ENUM_FMT:
1964         {
1965                 struct v4l2_fmtdesc *f = arg;
1966                 enum v4l2_buf_type type;
1967                 unsigned int index;
1968
1969                 index = f->index;
1970                 type  = f->type;
1971                 switch (type) {
1972                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1973                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1974                         if (index >= FORMATS)
1975                                 return -EINVAL;
1976                         if (f->type == V4L2_BUF_TYPE_VIDEO_OVERLAY &&
1977                             formats[index].planar)
1978                                 return -EINVAL;
1979                         memset(f,0,sizeof(*f));
1980                         f->index = index;
1981                         f->type  = type;
1982                         strlcpy(f->description,formats[index].name,sizeof(f->description));
1983                         f->pixelformat = formats[index].fourcc;
1984                         break;
1985                 case V4L2_BUF_TYPE_VBI_CAPTURE:
1986                         if (0 != index)
1987                                 return -EINVAL;
1988                         memset(f,0,sizeof(*f));
1989                         f->index = index;
1990                         f->type  = type;
1991                         f->pixelformat = V4L2_PIX_FMT_GREY;
1992                         strcpy(f->description,"vbi data");
1993                         break;
1994                 default:
1995                         return -EINVAL;
1996                 }
1997                 return 0;
1998         }
1999         case VIDIOC_G_FBUF:
2000         {
2001                 struct v4l2_framebuffer *fb = arg;
2002
2003                 *fb = dev->ovbuf;
2004                 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2005                 return 0;
2006         }
2007         case VIDIOC_S_FBUF:
2008         {
2009                 struct v4l2_framebuffer *fb = arg;
2010                 struct saa7134_format *fmt;
2011
2012                 if(!capable(CAP_SYS_ADMIN) &&
2013                    !capable(CAP_SYS_RAWIO))
2014                         return -EPERM;
2015
2016                 /* check args */
2017                 fmt = format_by_fourcc(fb->fmt.pixelformat);
2018                 if (NULL == fmt)
2019                         return -EINVAL;
2020
2021                 /* ok, accept it */
2022                 dev->ovbuf = *fb;
2023                 dev->ovfmt = fmt;
2024                 if (0 == dev->ovbuf.fmt.bytesperline)
2025                         dev->ovbuf.fmt.bytesperline =
2026                                 dev->ovbuf.fmt.width*fmt->depth/8;
2027                 return 0;
2028         }
2029         case VIDIOC_OVERLAY:
2030         {
2031                 int *on = arg;
2032
2033                 if (*on) {
2034                         if (!res_get(dev,fh,RESOURCE_OVERLAY))
2035                                 return -EBUSY;
2036                         spin_lock_irqsave(&dev->slock,flags);
2037                         start_preview(dev,fh);
2038                         spin_unlock_irqrestore(&dev->slock,flags);
2039                 }
2040                 if (!*on) {
2041                         if (!res_check(fh, RESOURCE_OVERLAY))
2042                                 return -EINVAL;
2043                         spin_lock_irqsave(&dev->slock,flags);
2044                         stop_preview(dev,fh);
2045                         spin_unlock_irqrestore(&dev->slock,flags);
2046                         res_free(dev,fh,RESOURCE_OVERLAY);
2047                 }
2048                 return 0;
2049         }
2050
2051         /* --- capture ioctls ---------------------------------------- */
2052         case VIDIOC_G_FMT:
2053         {
2054                 struct v4l2_format *f = arg;
2055                 return saa7134_g_fmt(dev,fh,f);
2056         }
2057         case VIDIOC_S_FMT:
2058         {
2059                 struct v4l2_format *f = arg;
2060                 return saa7134_s_fmt(dev,fh,f);
2061         }
2062         case VIDIOC_TRY_FMT:
2063         {
2064                 struct v4l2_format *f = arg;
2065                 return saa7134_try_fmt(dev,fh,f);
2066         }
2067 #ifdef HAVE_V4L1
2068         case VIDIOCGMBUF:
2069         {
2070                 struct video_mbuf *mbuf = arg;
2071                 struct videobuf_queue *q;
2072                 struct v4l2_requestbuffers req;
2073                 unsigned int i;
2074
2075                 q = saa7134_queue(fh);
2076                 memset(&req,0,sizeof(req));
2077                 req.type   = q->type;
2078                 req.count  = gbuffers;
2079                 req.memory = V4L2_MEMORY_MMAP;
2080                 err = videobuf_reqbufs(q,&req);
2081                 if (err < 0)
2082                         return err;
2083                 memset(mbuf,0,sizeof(*mbuf));
2084                 mbuf->frames = req.count;
2085                 mbuf->size   = 0;
2086                 for (i = 0; i < mbuf->frames; i++) {
2087                         mbuf->offsets[i]  = q->bufs[i]->boff;
2088                         mbuf->size       += q->bufs[i]->bsize;
2089                 }
2090                 return 0;
2091         }
2092 #endif
2093         case VIDIOC_REQBUFS:
2094                 return videobuf_reqbufs(saa7134_queue(fh),arg);
2095
2096         case VIDIOC_QUERYBUF:
2097                 return videobuf_querybuf(saa7134_queue(fh),arg);
2098
2099         case VIDIOC_QBUF:
2100                 return videobuf_qbuf(saa7134_queue(fh),arg);
2101
2102         case VIDIOC_DQBUF:
2103                 return videobuf_dqbuf(saa7134_queue(fh),arg,
2104                                       file->f_flags & O_NONBLOCK);
2105
2106         case VIDIOC_STREAMON:
2107         {
2108                 int res = saa7134_resource(fh);
2109
2110                 if (!res_get(dev,fh,res))
2111                         return -EBUSY;
2112                 return videobuf_streamon(saa7134_queue(fh));
2113         }
2114         case VIDIOC_STREAMOFF:
2115         {
2116                 int res = saa7134_resource(fh);
2117
2118                 err = videobuf_streamoff(saa7134_queue(fh));
2119                 if (err < 0)
2120                         return err;
2121                 res_free(dev,fh,res);
2122                 return 0;
2123         }
2124
2125         default:
2126                 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2127                                                   video_do_ioctl);
2128         }
2129         return 0;
2130 }
2131
2132 static int video_ioctl(struct inode *inode, struct file *file,
2133                        unsigned int cmd, unsigned long arg)
2134 {
2135         return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
2136 }
2137
2138 static int radio_do_ioctl(struct inode *inode, struct file *file,
2139                           unsigned int cmd, void *arg)
2140 {
2141         struct saa7134_fh *fh = file->private_data;
2142         struct saa7134_dev *dev = fh->dev;
2143
2144         if (video_debug > 1)
2145                 saa7134_print_ioctl(dev->name,cmd);
2146         switch (cmd) {
2147         case VIDIOC_QUERYCAP:
2148         {
2149                 struct v4l2_capability *cap = arg;
2150
2151                 memset(cap,0,sizeof(*cap));
2152                 strcpy(cap->driver, "saa7134");
2153                 strlcpy(cap->card, saa7134_boards[dev->board].name,
2154                         sizeof(cap->card));
2155                 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
2156                 cap->version = SAA7134_VERSION_CODE;
2157                 cap->capabilities = V4L2_CAP_TUNER;
2158                 return 0;
2159         }
2160         case VIDIOC_G_TUNER:
2161         {
2162                 struct v4l2_tuner *t = arg;
2163
2164                 if (0 != t->index)
2165                         return -EINVAL;
2166
2167                 memset(t,0,sizeof(*t));
2168                 strcpy(t->name, "Radio");
2169                 t->type = V4L2_TUNER_RADIO;
2170
2171                 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
2172
2173                 return 0;
2174         }
2175         case VIDIOC_S_TUNER:
2176         {
2177                 struct v4l2_tuner *t = arg;
2178
2179                 if (0 != t->index)
2180                         return -EINVAL;
2181
2182                 saa7134_i2c_call_clients(dev,VIDIOC_S_TUNER,t);
2183
2184                 return 0;
2185         }
2186         case VIDIOC_ENUMINPUT:
2187         {
2188                 struct v4l2_input *i = arg;
2189
2190                 if (i->index != 0)
2191                         return -EINVAL;
2192                 strcpy(i->name,"Radio");
2193                 i->type = V4L2_INPUT_TYPE_TUNER;
2194                 return 0;
2195         }
2196         case VIDIOC_G_INPUT:
2197         {
2198                 int *i = arg;
2199                 *i = 0;
2200                 return 0;
2201         }
2202         case VIDIOC_G_AUDIO:
2203         {
2204                 struct v4l2_audio *a = arg;
2205
2206                 memset(a,0,sizeof(*a));
2207                 strcpy(a->name,"Radio");
2208                 return 0;
2209         }
2210         case VIDIOC_G_STD:
2211         {
2212                 v4l2_std_id *id = arg;
2213                 *id = 0;
2214                 return 0;
2215         }
2216         case VIDIOC_S_AUDIO:
2217         case VIDIOC_S_INPUT:
2218         case VIDIOC_S_STD:
2219                 return 0;
2220
2221         case VIDIOC_QUERYCTRL:
2222         {
2223                 const struct v4l2_queryctrl *ctrl;
2224                 struct v4l2_queryctrl *c = arg;
2225
2226                 if (c->id <  V4L2_CID_BASE ||
2227                     c->id >= V4L2_CID_LASTP1)
2228                         return -EINVAL;
2229                 if (c->id == V4L2_CID_AUDIO_MUTE) {
2230                         ctrl = ctrl_by_id(c->id);
2231                         *c = *ctrl;
2232                 } else
2233                         *c = no_ctrl;
2234                 return 0;
2235         }
2236
2237         case VIDIOC_G_CTRL:
2238         case VIDIOC_S_CTRL:
2239         case VIDIOC_G_FREQUENCY:
2240         case VIDIOC_S_FREQUENCY:
2241                 return video_do_ioctl(inode,file,cmd,arg);
2242
2243         default:
2244                 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2245                                                   radio_do_ioctl);
2246         }
2247         return 0;
2248 }
2249
2250 static int radio_ioctl(struct inode *inode, struct file *file,
2251                        unsigned int cmd, unsigned long arg)
2252 {
2253         return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
2254 }
2255
2256 static struct file_operations video_fops =
2257 {
2258         .owner    = THIS_MODULE,
2259         .open     = video_open,
2260         .release  = video_release,
2261         .read     = video_read,
2262         .poll     = video_poll,
2263         .mmap     = video_mmap,
2264         .ioctl    = video_ioctl,
2265         .llseek   = no_llseek,
2266 };
2267
2268 static struct file_operations radio_fops =
2269 {
2270         .owner    = THIS_MODULE,
2271         .open     = video_open,
2272         .release  = video_release,
2273         .ioctl    = radio_ioctl,
2274         .llseek   = no_llseek,
2275 };
2276
2277 /* ----------------------------------------------------------- */
2278 /* exported stuff                                              */
2279
2280 struct video_device saa7134_video_template =
2281 {
2282         .name          = "saa7134-video",
2283         .type          = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_OVERLAY|
2284                          VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2285         .hardware      = 0,
2286         .fops          = &video_fops,
2287         .minor         = -1,
2288 };
2289
2290 struct video_device saa7134_vbi_template =
2291 {
2292         .name          = "saa7134-vbi",
2293         .type          = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
2294         .hardware      = 0,
2295         .fops          = &video_fops,
2296         .minor         = -1,
2297 };
2298
2299 struct video_device saa7134_radio_template =
2300 {
2301         .name          = "saa7134-radio",
2302         .type          = VID_TYPE_TUNER,
2303         .hardware      = 0,
2304         .fops          = &radio_fops,
2305         .minor         = -1,
2306 };
2307
2308 int saa7134_video_init1(struct saa7134_dev *dev)
2309 {
2310         /* sanitycheck insmod options */
2311         if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2312                 gbuffers = 2;
2313         if (gbufsize < 0 || gbufsize > gbufsize_max)
2314                 gbufsize = gbufsize_max;
2315         gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2316
2317         /* put some sensible defaults into the data structures ... */
2318         dev->ctl_bright     = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2319         dev->ctl_contrast   = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2320         dev->ctl_hue        = ctrl_by_id(V4L2_CID_HUE)->default_value;
2321         dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2322         dev->ctl_volume     = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2323         dev->ctl_mute       = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2324         dev->ctl_invert     = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2325         dev->ctl_automute   = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2326
2327         if (dev->tda9887_conf && dev->ctl_automute)
2328                 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2329         dev->automute       = 0;
2330
2331         INIT_LIST_HEAD(&dev->video_q.queue);
2332         init_timer(&dev->video_q.timeout);
2333         dev->video_q.timeout.function = saa7134_buffer_timeout;
2334         dev->video_q.timeout.data     = (unsigned long)(&dev->video_q);
2335         dev->video_q.dev              = dev;
2336
2337         if (saa7134_boards[dev->board].video_out) {
2338                 /* enable video output */
2339                 int vo = saa7134_boards[dev->board].video_out;
2340                 int video_reg;
2341                 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2342                 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2343                 video_reg = video_out[vo][1];
2344                 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2345                         video_reg &= ~VP_T_CODE_P_INVERTED;
2346                 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2347                 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2348                 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2349                 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2350                 video_reg = video_out[vo][5];
2351                 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2352                         video_reg &= ~VP_CLK_CTRL2_DELAYED;
2353                 if (vid_port_opts & SET_CLOCK_INVERTED)
2354                         video_reg |= VP_CLK_CTRL1_INVERTED;
2355                 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2356                 video_reg = video_out[vo][6];
2357                 if (vid_port_opts & SET_VSYNC_OFF) {
2358                         video_reg &= ~VP_VS_TYPE_MASK;
2359                         video_reg |= VP_VS_TYPE_OFF;
2360                 }
2361                 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2362                 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2363                 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2364         }
2365
2366         return 0;
2367 }
2368
2369 int saa7134_video_init2(struct saa7134_dev *dev)
2370 {
2371         /* init video hw */
2372         set_tvnorm(dev,&tvnorms[0]);
2373         video_mux(dev,0);
2374         saa7134_tvaudio_setmute(dev);
2375         saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2376         return 0;
2377 }
2378
2379 int saa7134_video_fini(struct saa7134_dev *dev)
2380 {
2381         /* nothing */
2382         return 0;
2383 }
2384
2385 void saa7134_irq_video_intl(struct saa7134_dev *dev)
2386 {
2387         static const char *st[] = {
2388                 "(no signal)", "NTSC", "PAL", "SECAM" };
2389         u32 st1,st2;
2390
2391         st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2392         st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2393         dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2394                 (st1 & 0x40) ? "not locked" : "locked",
2395                 (st2 & 0x40) ? "no"         : "yes",
2396                 st[st1 & 0x03]);
2397         dev->nosignal = (st1 & 0x40) || (st2 & 0x40);
2398
2399         if (dev->nosignal) {
2400                 /* no video signal -> mute audio */
2401                 if (dev->ctl_automute)
2402                         dev->automute = 1;
2403                 saa7134_tvaudio_setmute(dev);
2404                 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2405         } else {
2406                 /* wake up tvaudio audio carrier scan thread */
2407                 saa7134_tvaudio_do_scan(dev);
2408                 if (!noninterlaced)
2409                         saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2410         }
2411         if (dev->mops && dev->mops->signal_change)
2412                 dev->mops->signal_change(dev);
2413 }
2414
2415 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2416 {
2417         enum v4l2_field field;
2418
2419         spin_lock(&dev->slock);
2420         if (dev->video_q.curr) {
2421                 dev->video_fieldcount++;
2422                 field = dev->video_q.curr->vb.field;
2423                 if (V4L2_FIELD_HAS_BOTH(field)) {
2424                         /* make sure we have seen both fields */
2425                         if ((status & 0x10) == 0x00) {
2426                                 dev->video_q.curr->top_seen = 1;
2427                                 goto done;
2428                         }
2429                         if (!dev->video_q.curr->top_seen)
2430                                 goto done;
2431                 } else if (field == V4L2_FIELD_TOP) {
2432                         if ((status & 0x10) != 0x10)
2433                                 goto done;
2434                 } else if (field == V4L2_FIELD_BOTTOM) {
2435                         if ((status & 0x10) != 0x00)
2436                                 goto done;
2437                 }
2438                 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2439                 saa7134_buffer_finish(dev,&dev->video_q,STATE_DONE);
2440         }
2441         saa7134_buffer_next(dev,&dev->video_q);
2442
2443  done:
2444         spin_unlock(&dev->slock);
2445 }
2446
2447 /* ----------------------------------------------------------- */
2448 /*
2449  * Local variables:
2450  * c-basic-offset: 8
2451  * End:
2452  */