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