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