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