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