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