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