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