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