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