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