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