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