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