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