V4L/DVB (6736): Fix some errors at the video_ioctl2 conversion
[linux-2.6/cjktty.git] / drivers / media / video / saa7134 / saa7134-video.c
blob043e1396857a72df39f8d59c90a5c0be270f286d
1 /*
3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/sort.h>
30 #include "saa7134-reg.h"
31 #include "saa7134.h"
32 #include <media/v4l2-common.h>
34 #ifdef CONFIG_VIDEO_V4L1_COMPAT
35 /* Include V4L1 specific functions. Should be removed soon */
36 #include <linux/videodev.h>
37 #endif
39 /* ------------------------------------------------------------------ */
41 unsigned int video_debug;
42 static unsigned int gbuffers = 8;
43 static unsigned int noninterlaced = 0;
44 static unsigned int gbufsize = 720*576*4;
45 static unsigned int gbufsize_max = 720*576*4;
46 static char secam[] = "--";
47 module_param(video_debug, int, 0644);
48 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
49 module_param(gbuffers, int, 0444);
50 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
51 module_param(noninterlaced, int, 0644);
52 MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
53 module_param_string(secam, secam, sizeof(secam), 0644);
54 MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
57 #define dprintk(fmt, arg...) if (video_debug&0x04) \
58 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
60 /* ------------------------------------------------------------------ */
61 /* Defines for Video Output Port Register at address 0x191 */
63 /* Bit 0: VIP code T bit polarity */
65 #define VP_T_CODE_P_NON_INVERTED 0x00
66 #define VP_T_CODE_P_INVERTED 0x01
68 /* ------------------------------------------------------------------ */
69 /* Defines for Video Output Port Register at address 0x195 */
71 /* Bit 2: Video output clock delay control */
73 #define VP_CLK_CTRL2_NOT_DELAYED 0x00
74 #define VP_CLK_CTRL2_DELAYED 0x04
76 /* Bit 1: Video output clock invert control */
78 #define VP_CLK_CTRL1_NON_INVERTED 0x00
79 #define VP_CLK_CTRL1_INVERTED 0x02
81 /* ------------------------------------------------------------------ */
82 /* Defines for Video Output Port Register at address 0x196 */
84 /* Bits 2 to 0: VSYNC pin video vertical sync type */
86 #define VP_VS_TYPE_MASK 0x07
88 #define VP_VS_TYPE_OFF 0x00
89 #define VP_VS_TYPE_V123 0x01
90 #define VP_VS_TYPE_V_ITU 0x02
91 #define VP_VS_TYPE_VGATE_L 0x03
92 #define VP_VS_TYPE_RESERVED1 0x04
93 #define VP_VS_TYPE_RESERVED2 0x05
94 #define VP_VS_TYPE_F_ITU 0x06
95 #define VP_VS_TYPE_SC_FID 0x07
97 /* ------------------------------------------------------------------ */
98 /* data structs for video */
100 static int video_out[][9] = {
101 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
104 static struct saa7134_format formats[] = {
106 .name = "8 bpp gray",
107 .fourcc = V4L2_PIX_FMT_GREY,
108 .depth = 8,
109 .pm = 0x06,
111 .name = "15 bpp RGB, le",
112 .fourcc = V4L2_PIX_FMT_RGB555,
113 .depth = 16,
114 .pm = 0x13 | 0x80,
116 .name = "15 bpp RGB, be",
117 .fourcc = V4L2_PIX_FMT_RGB555X,
118 .depth = 16,
119 .pm = 0x13 | 0x80,
120 .bswap = 1,
122 .name = "16 bpp RGB, le",
123 .fourcc = V4L2_PIX_FMT_RGB565,
124 .depth = 16,
125 .pm = 0x10 | 0x80,
127 .name = "16 bpp RGB, be",
128 .fourcc = V4L2_PIX_FMT_RGB565X,
129 .depth = 16,
130 .pm = 0x10 | 0x80,
131 .bswap = 1,
133 .name = "24 bpp RGB, le",
134 .fourcc = V4L2_PIX_FMT_BGR24,
135 .depth = 24,
136 .pm = 0x11,
138 .name = "24 bpp RGB, be",
139 .fourcc = V4L2_PIX_FMT_RGB24,
140 .depth = 24,
141 .pm = 0x11,
142 .bswap = 1,
144 .name = "32 bpp RGB, le",
145 .fourcc = V4L2_PIX_FMT_BGR32,
146 .depth = 32,
147 .pm = 0x12,
149 .name = "32 bpp RGB, be",
150 .fourcc = V4L2_PIX_FMT_RGB32,
151 .depth = 32,
152 .pm = 0x12,
153 .bswap = 1,
154 .wswap = 1,
156 .name = "4:2:2 packed, YUYV",
157 .fourcc = V4L2_PIX_FMT_YUYV,
158 .depth = 16,
159 .pm = 0x00,
160 .bswap = 1,
161 .yuv = 1,
163 .name = "4:2:2 packed, UYVY",
164 .fourcc = V4L2_PIX_FMT_UYVY,
165 .depth = 16,
166 .pm = 0x00,
167 .yuv = 1,
169 .name = "4:2:2 planar, Y-Cb-Cr",
170 .fourcc = V4L2_PIX_FMT_YUV422P,
171 .depth = 16,
172 .pm = 0x09,
173 .yuv = 1,
174 .planar = 1,
175 .hshift = 1,
176 .vshift = 0,
178 .name = "4:2:0 planar, Y-Cb-Cr",
179 .fourcc = V4L2_PIX_FMT_YUV420,
180 .depth = 12,
181 .pm = 0x0a,
182 .yuv = 1,
183 .planar = 1,
184 .hshift = 1,
185 .vshift = 1,
187 .name = "4:2:0 planar, Y-Cb-Cr",
188 .fourcc = V4L2_PIX_FMT_YVU420,
189 .depth = 12,
190 .pm = 0x0a,
191 .yuv = 1,
192 .planar = 1,
193 .uvswap = 1,
194 .hshift = 1,
195 .vshift = 1,
198 #define FORMATS ARRAY_SIZE(formats)
200 #define NORM_625_50 \
201 .h_start = 0, \
202 .h_stop = 719, \
203 .video_v_start = 24, \
204 .video_v_stop = 311, \
205 .vbi_v_start_0 = 7, \
206 .vbi_v_stop_0 = 22, \
207 .vbi_v_start_1 = 319, \
208 .src_timing = 4
210 #define NORM_525_60 \
211 .h_start = 0, \
212 .h_stop = 703, \
213 .video_v_start = 23, \
214 .video_v_stop = 262, \
215 .vbi_v_start_0 = 10, \
216 .vbi_v_stop_0 = 21, \
217 .vbi_v_start_1 = 273, \
218 .src_timing = 7
220 #define SAA7134_NORMS \
221 V4L2_STD_PAL | V4L2_STD_PAL_N | \
222 V4L2_STD_PAL_Nc | V4L2_STD_SECAM | \
223 V4L2_STD_NTSC | V4L2_STD_PAL_M | \
224 V4L2_STD_PAL_60
226 static struct saa7134_tvnorm tvnorms[] = {
228 .name = "PAL", /* autodetect */
229 .id = V4L2_STD_PAL,
230 NORM_625_50,
232 .sync_control = 0x18,
233 .luma_control = 0x40,
234 .chroma_ctrl1 = 0x81,
235 .chroma_gain = 0x2a,
236 .chroma_ctrl2 = 0x06,
237 .vgate_misc = 0x1c,
240 .name = "PAL-BG",
241 .id = V4L2_STD_PAL_BG,
242 NORM_625_50,
244 .sync_control = 0x18,
245 .luma_control = 0x40,
246 .chroma_ctrl1 = 0x81,
247 .chroma_gain = 0x2a,
248 .chroma_ctrl2 = 0x06,
249 .vgate_misc = 0x1c,
252 .name = "PAL-I",
253 .id = V4L2_STD_PAL_I,
254 NORM_625_50,
256 .sync_control = 0x18,
257 .luma_control = 0x40,
258 .chroma_ctrl1 = 0x81,
259 .chroma_gain = 0x2a,
260 .chroma_ctrl2 = 0x06,
261 .vgate_misc = 0x1c,
264 .name = "PAL-DK",
265 .id = V4L2_STD_PAL_DK,
266 NORM_625_50,
268 .sync_control = 0x18,
269 .luma_control = 0x40,
270 .chroma_ctrl1 = 0x81,
271 .chroma_gain = 0x2a,
272 .chroma_ctrl2 = 0x06,
273 .vgate_misc = 0x1c,
276 .name = "NTSC",
277 .id = V4L2_STD_NTSC,
278 NORM_525_60,
280 .sync_control = 0x59,
281 .luma_control = 0x40,
282 .chroma_ctrl1 = 0x89,
283 .chroma_gain = 0x2a,
284 .chroma_ctrl2 = 0x0e,
285 .vgate_misc = 0x18,
288 .name = "SECAM",
289 .id = V4L2_STD_SECAM,
290 NORM_625_50,
292 .sync_control = 0x18,
293 .luma_control = 0x1b,
294 .chroma_ctrl1 = 0xd1,
295 .chroma_gain = 0x80,
296 .chroma_ctrl2 = 0x00,
297 .vgate_misc = 0x1c,
300 .name = "SECAM-DK",
301 .id = V4L2_STD_SECAM_DK,
302 NORM_625_50,
304 .sync_control = 0x18,
305 .luma_control = 0x1b,
306 .chroma_ctrl1 = 0xd1,
307 .chroma_gain = 0x80,
308 .chroma_ctrl2 = 0x00,
309 .vgate_misc = 0x1c,
312 .name = "SECAM-L",
313 .id = V4L2_STD_SECAM_L,
314 NORM_625_50,
316 .sync_control = 0x18,
317 .luma_control = 0x1b,
318 .chroma_ctrl1 = 0xd1,
319 .chroma_gain = 0x80,
320 .chroma_ctrl2 = 0x00,
321 .vgate_misc = 0x1c,
324 .name = "SECAM-Lc",
325 .id = V4L2_STD_SECAM_LC,
326 NORM_625_50,
328 .sync_control = 0x18,
329 .luma_control = 0x1b,
330 .chroma_ctrl1 = 0xd1,
331 .chroma_gain = 0x80,
332 .chroma_ctrl2 = 0x00,
333 .vgate_misc = 0x1c,
336 .name = "PAL-M",
337 .id = V4L2_STD_PAL_M,
338 NORM_525_60,
340 .sync_control = 0x59,
341 .luma_control = 0x40,
342 .chroma_ctrl1 = 0xb9,
343 .chroma_gain = 0x2a,
344 .chroma_ctrl2 = 0x0e,
345 .vgate_misc = 0x18,
348 .name = "PAL-Nc",
349 .id = V4L2_STD_PAL_Nc,
350 NORM_625_50,
352 .sync_control = 0x18,
353 .luma_control = 0x40,
354 .chroma_ctrl1 = 0xa1,
355 .chroma_gain = 0x2a,
356 .chroma_ctrl2 = 0x06,
357 .vgate_misc = 0x1c,
360 .name = "PAL-60",
361 .id = V4L2_STD_PAL_60,
363 .h_start = 0,
364 .h_stop = 719,
365 .video_v_start = 23,
366 .video_v_stop = 262,
367 .vbi_v_start_0 = 10,
368 .vbi_v_stop_0 = 21,
369 .vbi_v_start_1 = 273,
370 .src_timing = 7,
372 .sync_control = 0x18,
373 .luma_control = 0x40,
374 .chroma_ctrl1 = 0x81,
375 .chroma_gain = 0x2a,
376 .chroma_ctrl2 = 0x06,
377 .vgate_misc = 0x1c,
380 #define TVNORMS ARRAY_SIZE(tvnorms)
382 #define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
383 #define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
384 #define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
385 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
386 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
388 static const struct v4l2_queryctrl no_ctrl = {
389 .name = "42",
390 .flags = V4L2_CTRL_FLAG_DISABLED,
392 static const struct v4l2_queryctrl video_ctrls[] = {
393 /* --- video --- */
395 .id = V4L2_CID_BRIGHTNESS,
396 .name = "Brightness",
397 .minimum = 0,
398 .maximum = 255,
399 .step = 1,
400 .default_value = 128,
401 .type = V4L2_CTRL_TYPE_INTEGER,
403 .id = V4L2_CID_CONTRAST,
404 .name = "Contrast",
405 .minimum = 0,
406 .maximum = 127,
407 .step = 1,
408 .default_value = 68,
409 .type = V4L2_CTRL_TYPE_INTEGER,
411 .id = V4L2_CID_SATURATION,
412 .name = "Saturation",
413 .minimum = 0,
414 .maximum = 127,
415 .step = 1,
416 .default_value = 64,
417 .type = V4L2_CTRL_TYPE_INTEGER,
419 .id = V4L2_CID_HUE,
420 .name = "Hue",
421 .minimum = -128,
422 .maximum = 127,
423 .step = 1,
424 .default_value = 0,
425 .type = V4L2_CTRL_TYPE_INTEGER,
427 .id = V4L2_CID_HFLIP,
428 .name = "Mirror",
429 .minimum = 0,
430 .maximum = 1,
431 .type = V4L2_CTRL_TYPE_BOOLEAN,
433 /* --- audio --- */
435 .id = V4L2_CID_AUDIO_MUTE,
436 .name = "Mute",
437 .minimum = 0,
438 .maximum = 1,
439 .type = V4L2_CTRL_TYPE_BOOLEAN,
441 .id = V4L2_CID_AUDIO_VOLUME,
442 .name = "Volume",
443 .minimum = -15,
444 .maximum = 15,
445 .step = 1,
446 .default_value = 0,
447 .type = V4L2_CTRL_TYPE_INTEGER,
449 /* --- private --- */
451 .id = V4L2_CID_PRIVATE_INVERT,
452 .name = "Invert",
453 .minimum = 0,
454 .maximum = 1,
455 .type = V4L2_CTRL_TYPE_BOOLEAN,
457 .id = V4L2_CID_PRIVATE_Y_ODD,
458 .name = "y offset odd field",
459 .minimum = 0,
460 .maximum = 128,
461 .default_value = 0,
462 .type = V4L2_CTRL_TYPE_INTEGER,
464 .id = V4L2_CID_PRIVATE_Y_EVEN,
465 .name = "y offset even field",
466 .minimum = 0,
467 .maximum = 128,
468 .default_value = 0,
469 .type = V4L2_CTRL_TYPE_INTEGER,
471 .id = V4L2_CID_PRIVATE_AUTOMUTE,
472 .name = "automute",
473 .minimum = 0,
474 .maximum = 1,
475 .default_value = 1,
476 .type = V4L2_CTRL_TYPE_BOOLEAN,
479 static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
481 static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
483 unsigned int i;
485 for (i = 0; i < CTRLS; i++)
486 if (video_ctrls[i].id == id)
487 return video_ctrls+i;
488 return NULL;
491 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
493 unsigned int i;
495 for (i = 0; i < FORMATS; i++)
496 if (formats[i].fourcc == fourcc)
497 return formats+i;
498 return NULL;
501 /* ----------------------------------------------------------------------- */
502 /* resource management */
504 static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
506 if (fh->resources & bit)
507 /* have it already allocated */
508 return 1;
510 /* is it free? */
511 mutex_lock(&dev->lock);
512 if (dev->resources & bit) {
513 /* no, someone else uses it */
514 mutex_unlock(&dev->lock);
515 return 0;
517 /* it's free, grab it */
518 fh->resources |= bit;
519 dev->resources |= bit;
520 dprintk("res: get %d\n",bit);
521 mutex_unlock(&dev->lock);
522 return 1;
525 static int res_check(struct saa7134_fh *fh, unsigned int bit)
527 return (fh->resources & bit);
530 static int res_locked(struct saa7134_dev *dev, unsigned int bit)
532 return (dev->resources & bit);
535 static
536 void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
538 BUG_ON((fh->resources & bits) != bits);
540 mutex_lock(&dev->lock);
541 fh->resources &= ~bits;
542 dev->resources &= ~bits;
543 dprintk("res: put %d\n",bits);
544 mutex_unlock(&dev->lock);
547 /* ------------------------------------------------------------------ */
549 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
551 dprintk("set tv norm = %s\n",norm->name);
552 dev->tvnorm = norm;
554 /* setup cropping */
555 dev->crop_bounds.left = norm->h_start;
556 dev->crop_defrect.left = norm->h_start;
557 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
558 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
560 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
561 dev->crop_defrect.top = norm->video_v_start*2;
562 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
563 - dev->crop_bounds.top;
564 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
566 dev->crop_current = dev->crop_defrect;
568 saa7134_set_tvnorm_hw(dev);
571 static void video_mux(struct saa7134_dev *dev, int input)
573 dprintk("video input = %d [%s]\n", input, card_in(dev, input).name);
574 dev->ctl_input = input;
575 set_tvnorm(dev, dev->tvnorm);
576 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
580 static void saa7134_set_decoder(struct saa7134_dev *dev)
582 int luma_control, sync_control, mux;
584 struct saa7134_tvnorm *norm = dev->tvnorm;
585 mux = card_in(dev, dev->ctl_input).vmux;
587 luma_control = norm->luma_control;
588 sync_control = norm->sync_control;
590 if (mux > 5)
591 luma_control |= 0x80; /* svideo */
592 if (noninterlaced || dev->nosignal)
593 sync_control |= 0x20;
595 /* setup video decoder */
596 saa_writeb(SAA7134_INCR_DELAY, 0x08);
597 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
598 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
600 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
601 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
602 saa_writeb(SAA7134_HSYNC_START, 0xeb);
603 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
604 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
606 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
607 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
608 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
610 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
611 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
613 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
614 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
616 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
617 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
618 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
620 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
621 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
623 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
624 saa_writeb(SAA7134_VGATE_START, 0x11);
625 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
626 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
627 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
628 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
631 void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
633 saa7134_set_decoder(dev);
635 if (card_in(dev, dev->ctl_input).tv) {
636 if ((card(dev).tuner_type == TUNER_PHILIPS_TDA8290)
637 && ((card(dev).tuner_config == 1)
638 || (card(dev).tuner_config == 2)))
639 saa7134_set_gpio(dev, 22, 5);
640 saa7134_i2c_call_clients(dev, VIDIOC_S_STD, &dev->tvnorm->id);
644 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
646 static const struct {
647 int xpsc;
648 int xacl;
649 int xc2_1;
650 int xdcg;
651 int vpfy;
652 } vals[] = {
653 /* XPSC XACL XC2_1 XDCG VPFY */
654 { 1, 0, 0, 0, 0 },
655 { 2, 2, 1, 2, 2 },
656 { 3, 4, 1, 3, 2 },
657 { 4, 8, 1, 4, 2 },
658 { 5, 8, 1, 4, 2 },
659 { 6, 8, 1, 4, 3 },
660 { 7, 8, 1, 4, 3 },
661 { 8, 15, 0, 4, 3 },
662 { 9, 15, 0, 4, 3 },
663 { 10, 16, 1, 5, 3 },
665 static const int count = ARRAY_SIZE(vals);
666 int i;
668 for (i = 0; i < count; i++)
669 if (vals[i].xpsc == prescale)
670 break;
671 if (i == count)
672 return;
674 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
675 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
676 saa_writeb(SAA7134_LEVEL_CTRL(task),
677 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
678 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
679 (vals[i].vpfy << 2) | vals[i].vpfy);
682 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
684 int val,mirror;
686 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
687 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
689 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
690 if (yscale < 2048) {
691 /* LPI */
692 dprintk("yscale LPI yscale=%d\n",yscale);
693 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
694 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
695 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
696 } else {
697 /* ACM */
698 val = 0x40 * 1024 / yscale;
699 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
700 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
701 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
702 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
704 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
707 static void set_size(struct saa7134_dev *dev, int task,
708 int width, int height, int interlace)
710 int prescale,xscale,yscale,y_even,y_odd;
711 int h_start, h_stop, v_start, v_stop;
712 int div = interlace ? 2 : 1;
714 /* setup video scaler */
715 h_start = dev->crop_current.left;
716 v_start = dev->crop_current.top/2;
717 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
718 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
720 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
721 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
722 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
723 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
724 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
725 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
726 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
727 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
729 prescale = dev->crop_current.width / width;
730 if (0 == prescale)
731 prescale = 1;
732 xscale = 1024 * dev->crop_current.width / prescale / width;
733 yscale = 512 * div * dev->crop_current.height / height;
734 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
735 set_h_prescale(dev,task,prescale);
736 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
737 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
738 set_v_scale(dev,task,yscale);
740 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
741 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
742 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
743 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
745 /* deinterlace y offsets */
746 y_odd = dev->ctl_y_odd;
747 y_even = dev->ctl_y_even;
748 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
749 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
750 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
751 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
754 /* ------------------------------------------------------------------ */
756 struct cliplist {
757 __u16 position;
758 __u8 enable;
759 __u8 disable;
762 static void set_cliplist(struct saa7134_dev *dev, int reg,
763 struct cliplist *cl, int entries, char *name)
765 __u8 winbits = 0;
766 int i;
768 for (i = 0; i < entries; i++) {
769 winbits |= cl[i].enable;
770 winbits &= ~cl[i].disable;
771 if (i < 15 && cl[i].position == cl[i+1].position)
772 continue;
773 saa_writeb(reg + 0, winbits);
774 saa_writeb(reg + 2, cl[i].position & 0xff);
775 saa_writeb(reg + 3, cl[i].position >> 8);
776 dprintk("clip: %s winbits=%02x pos=%d\n",
777 name,winbits,cl[i].position);
778 reg += 8;
780 for (; reg < 0x400; reg += 8) {
781 saa_writeb(reg+ 0, 0);
782 saa_writeb(reg + 1, 0);
783 saa_writeb(reg + 2, 0);
784 saa_writeb(reg + 3, 0);
788 static int clip_range(int val)
790 if (val < 0)
791 val = 0;
792 return val;
795 /* Sort into smallest position first order */
796 static int cliplist_cmp(const void *a, const void *b)
798 const struct cliplist *cla = a;
799 const struct cliplist *clb = b;
800 if (cla->position < clb->position)
801 return -1;
802 if (cla->position > clb->position)
803 return 1;
804 return 0;
807 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
808 int nclips, int interlace)
810 struct cliplist col[16], row[16];
811 int cols = 0, rows = 0, i;
812 int div = interlace ? 2 : 1;
814 memset(col, 0, sizeof(col));
815 memset(row, 0, sizeof(row));
816 for (i = 0; i < nclips && i < 8; i++) {
817 col[cols].position = clip_range(clips[i].c.left);
818 col[cols].enable = (1 << i);
819 cols++;
820 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
821 col[cols].disable = (1 << i);
822 cols++;
823 row[rows].position = clip_range(clips[i].c.top / div);
824 row[rows].enable = (1 << i);
825 rows++;
826 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
827 / div);
828 row[rows].disable = (1 << i);
829 rows++;
831 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
832 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
833 set_cliplist(dev,0x380,col,cols,"cols");
834 set_cliplist(dev,0x384,row,rows,"rows");
835 return 0;
838 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
840 enum v4l2_field field;
841 int maxw, maxh;
843 if (NULL == dev->ovbuf.base)
844 return -EINVAL;
845 if (NULL == dev->ovfmt)
846 return -EINVAL;
847 if (win->w.width < 48 || win->w.height < 32)
848 return -EINVAL;
849 if (win->clipcount > 2048)
850 return -EINVAL;
852 field = win->field;
853 maxw = dev->crop_current.width;
854 maxh = dev->crop_current.height;
856 if (V4L2_FIELD_ANY == field) {
857 field = (win->w.height > maxh/2)
858 ? V4L2_FIELD_INTERLACED
859 : V4L2_FIELD_TOP;
861 switch (field) {
862 case V4L2_FIELD_TOP:
863 case V4L2_FIELD_BOTTOM:
864 maxh = maxh / 2;
865 break;
866 case V4L2_FIELD_INTERLACED:
867 break;
868 default:
869 return -EINVAL;
872 win->field = field;
873 if (win->w.width > maxw)
874 win->w.width = maxw;
875 if (win->w.height > maxh)
876 win->w.height = maxh;
877 return 0;
880 static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
882 unsigned long base,control,bpl;
883 int err;
885 err = verify_preview(dev,&fh->win);
886 if (0 != err)
887 return err;
889 dev->ovfield = fh->win.field;
890 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
891 fh->win.w.width,fh->win.w.height,
892 fh->win.w.left,fh->win.w.top,
893 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
895 /* setup window + clipping */
896 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
897 V4L2_FIELD_HAS_BOTH(dev->ovfield));
898 setup_clipping(dev,fh->clips,fh->nclips,
899 V4L2_FIELD_HAS_BOTH(dev->ovfield));
900 if (dev->ovfmt->yuv)
901 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
902 else
903 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
904 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
906 /* dma: setup channel 1 (= Video Task B) */
907 base = (unsigned long)dev->ovbuf.base;
908 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
909 base += dev->ovfmt->depth/8 * fh->win.w.left;
910 bpl = dev->ovbuf.fmt.bytesperline;
911 control = SAA7134_RS_CONTROL_BURST_16;
912 if (dev->ovfmt->bswap)
913 control |= SAA7134_RS_CONTROL_BSWAP;
914 if (dev->ovfmt->wswap)
915 control |= SAA7134_RS_CONTROL_WSWAP;
916 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
917 saa_writel(SAA7134_RS_BA1(1),base);
918 saa_writel(SAA7134_RS_BA2(1),base+bpl);
919 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
920 saa_writel(SAA7134_RS_CONTROL(1),control);
921 } else {
922 saa_writel(SAA7134_RS_BA1(1),base);
923 saa_writel(SAA7134_RS_BA2(1),base);
924 saa_writel(SAA7134_RS_PITCH(1),bpl);
925 saa_writel(SAA7134_RS_CONTROL(1),control);
928 /* start dma */
929 dev->ovenable = 1;
930 saa7134_set_dmabits(dev);
932 return 0;
935 static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
937 dev->ovenable = 0;
938 saa7134_set_dmabits(dev);
939 return 0;
942 /* ------------------------------------------------------------------ */
944 static int buffer_activate(struct saa7134_dev *dev,
945 struct saa7134_buf *buf,
946 struct saa7134_buf *next)
948 unsigned long base,control,bpl;
949 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
951 dprintk("buffer_activate buf=%p\n",buf);
952 buf->vb.state = VIDEOBUF_ACTIVE;
953 buf->top_seen = 0;
955 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
956 V4L2_FIELD_HAS_BOTH(buf->vb.field));
957 if (buf->fmt->yuv)
958 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
959 else
960 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
961 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
963 /* DMA: setup channel 0 (= Video Task A0) */
964 base = saa7134_buffer_base(buf);
965 if (buf->fmt->planar)
966 bpl = buf->vb.width;
967 else
968 bpl = (buf->vb.width * buf->fmt->depth) / 8;
969 control = SAA7134_RS_CONTROL_BURST_16 |
970 SAA7134_RS_CONTROL_ME |
971 (buf->pt->dma >> 12);
972 if (buf->fmt->bswap)
973 control |= SAA7134_RS_CONTROL_BSWAP;
974 if (buf->fmt->wswap)
975 control |= SAA7134_RS_CONTROL_WSWAP;
976 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
977 /* interlaced */
978 saa_writel(SAA7134_RS_BA1(0),base);
979 saa_writel(SAA7134_RS_BA2(0),base+bpl);
980 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
981 } else {
982 /* non-interlaced */
983 saa_writel(SAA7134_RS_BA1(0),base);
984 saa_writel(SAA7134_RS_BA2(0),base);
985 saa_writel(SAA7134_RS_PITCH(0),bpl);
987 saa_writel(SAA7134_RS_CONTROL(0),control);
989 if (buf->fmt->planar) {
990 /* DMA: setup channel 4+5 (= planar task A) */
991 bpl_uv = bpl >> buf->fmt->hshift;
992 lines_uv = buf->vb.height >> buf->fmt->vshift;
993 base2 = base + bpl * buf->vb.height;
994 base3 = base2 + bpl_uv * lines_uv;
995 if (buf->fmt->uvswap)
996 tmp = base2, base2 = base3, base3 = tmp;
997 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
998 bpl_uv,lines_uv,base2,base3);
999 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
1000 /* interlaced */
1001 saa_writel(SAA7134_RS_BA1(4),base2);
1002 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
1003 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
1004 saa_writel(SAA7134_RS_BA1(5),base3);
1005 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
1006 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
1007 } else {
1008 /* non-interlaced */
1009 saa_writel(SAA7134_RS_BA1(4),base2);
1010 saa_writel(SAA7134_RS_BA2(4),base2);
1011 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
1012 saa_writel(SAA7134_RS_BA1(5),base3);
1013 saa_writel(SAA7134_RS_BA2(5),base3);
1014 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
1016 saa_writel(SAA7134_RS_CONTROL(4),control);
1017 saa_writel(SAA7134_RS_CONTROL(5),control);
1020 /* start DMA */
1021 saa7134_set_dmabits(dev);
1022 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1023 return 0;
1026 static int buffer_prepare(struct videobuf_queue *q,
1027 struct videobuf_buffer *vb,
1028 enum v4l2_field field)
1030 struct saa7134_fh *fh = q->priv_data;
1031 struct saa7134_dev *dev = fh->dev;
1032 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1033 unsigned int size;
1034 int err;
1036 /* sanity checks */
1037 if (NULL == fh->fmt)
1038 return -EINVAL;
1039 if (fh->width < 48 ||
1040 fh->height < 32 ||
1041 fh->width/4 > dev->crop_current.width ||
1042 fh->height/4 > dev->crop_current.height ||
1043 fh->width > dev->crop_bounds.width ||
1044 fh->height > dev->crop_bounds.height)
1045 return -EINVAL;
1046 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1047 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1048 return -EINVAL;
1050 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1051 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1052 fh->fmt->name);
1053 if (buf->vb.width != fh->width ||
1054 buf->vb.height != fh->height ||
1055 buf->vb.size != size ||
1056 buf->vb.field != field ||
1057 buf->fmt != fh->fmt) {
1058 saa7134_dma_free(q,buf);
1061 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1062 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1064 buf->vb.width = fh->width;
1065 buf->vb.height = fh->height;
1066 buf->vb.size = size;
1067 buf->vb.field = field;
1068 buf->fmt = fh->fmt;
1069 buf->pt = &fh->pt_cap;
1071 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
1072 if (err)
1073 goto oops;
1074 err = saa7134_pgtable_build(dev->pci,buf->pt,
1075 dma->sglist,
1076 dma->sglen,
1077 saa7134_buffer_startpage(buf));
1078 if (err)
1079 goto oops;
1081 buf->vb.state = VIDEOBUF_PREPARED;
1082 buf->activate = buffer_activate;
1083 return 0;
1085 oops:
1086 saa7134_dma_free(q,buf);
1087 return err;
1090 static int
1091 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1093 struct saa7134_fh *fh = q->priv_data;
1095 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1096 if (0 == *count)
1097 *count = gbuffers;
1098 *count = saa7134_buffer_count(*size,*count);
1099 return 0;
1102 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1104 struct saa7134_fh *fh = q->priv_data;
1105 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1107 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1110 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1112 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1114 saa7134_dma_free(q,buf);
1117 static struct videobuf_queue_ops video_qops = {
1118 .buf_setup = buffer_setup,
1119 .buf_prepare = buffer_prepare,
1120 .buf_queue = buffer_queue,
1121 .buf_release = buffer_release,
1124 /* ------------------------------------------------------------------ */
1126 static int vidioc_g_ctrl(struct file *file, void *priv,
1127 struct v4l2_control *c)
1129 struct saa7134_fh *fh = priv;
1130 struct saa7134_dev *dev = fh->dev;
1131 const struct v4l2_queryctrl* ctrl;
1133 ctrl = ctrl_by_id(c->id);
1134 if (NULL == ctrl)
1135 return -EINVAL;
1136 switch (c->id) {
1137 case V4L2_CID_BRIGHTNESS:
1138 c->value = dev->ctl_bright;
1139 break;
1140 case V4L2_CID_HUE:
1141 c->value = dev->ctl_hue;
1142 break;
1143 case V4L2_CID_CONTRAST:
1144 c->value = dev->ctl_contrast;
1145 break;
1146 case V4L2_CID_SATURATION:
1147 c->value = dev->ctl_saturation;
1148 break;
1149 case V4L2_CID_AUDIO_MUTE:
1150 c->value = dev->ctl_mute;
1151 break;
1152 case V4L2_CID_AUDIO_VOLUME:
1153 c->value = dev->ctl_volume;
1154 break;
1155 case V4L2_CID_PRIVATE_INVERT:
1156 c->value = dev->ctl_invert;
1157 break;
1158 case V4L2_CID_HFLIP:
1159 c->value = dev->ctl_mirror;
1160 break;
1161 case V4L2_CID_PRIVATE_Y_EVEN:
1162 c->value = dev->ctl_y_even;
1163 break;
1164 case V4L2_CID_PRIVATE_Y_ODD:
1165 c->value = dev->ctl_y_odd;
1166 break;
1167 case V4L2_CID_PRIVATE_AUTOMUTE:
1168 c->value = dev->ctl_automute;
1169 break;
1170 default:
1171 return -EINVAL;
1173 return 0;
1176 static int vidioc_s_ctrl(struct file *file, void *f,
1177 struct v4l2_control *c)
1179 const struct v4l2_queryctrl* ctrl;
1180 struct saa7134_fh *fh = f;
1181 struct saa7134_dev *dev = fh->dev;
1182 unsigned long flags;
1183 int restart_overlay = 0;
1184 int err = -EINVAL;
1186 err = v4l2_prio_check(&dev->prio, &fh->prio);
1187 if (0 != err)
1188 return err;
1190 mutex_lock(&dev->lock);
1192 ctrl = ctrl_by_id(c->id);
1193 if (NULL == ctrl)
1194 goto error;
1196 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1197 switch (ctrl->type) {
1198 case V4L2_CTRL_TYPE_BOOLEAN:
1199 case V4L2_CTRL_TYPE_MENU:
1200 case V4L2_CTRL_TYPE_INTEGER:
1201 if (c->value < ctrl->minimum)
1202 c->value = ctrl->minimum;
1203 if (c->value > ctrl->maximum)
1204 c->value = ctrl->maximum;
1205 break;
1206 default:
1207 /* nothing */;
1209 switch (c->id) {
1210 case V4L2_CID_BRIGHTNESS:
1211 dev->ctl_bright = c->value;
1212 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1213 break;
1214 case V4L2_CID_HUE:
1215 dev->ctl_hue = c->value;
1216 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1217 break;
1218 case V4L2_CID_CONTRAST:
1219 dev->ctl_contrast = c->value;
1220 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1221 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1222 break;
1223 case V4L2_CID_SATURATION:
1224 dev->ctl_saturation = c->value;
1225 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1226 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1227 break;
1228 case V4L2_CID_AUDIO_MUTE:
1229 dev->ctl_mute = c->value;
1230 saa7134_tvaudio_setmute(dev);
1231 break;
1232 case V4L2_CID_AUDIO_VOLUME:
1233 dev->ctl_volume = c->value;
1234 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1235 break;
1236 case V4L2_CID_PRIVATE_INVERT:
1237 dev->ctl_invert = c->value;
1238 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1239 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1240 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1241 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1242 break;
1243 case V4L2_CID_HFLIP:
1244 dev->ctl_mirror = c->value;
1245 restart_overlay = 1;
1246 break;
1247 case V4L2_CID_PRIVATE_Y_EVEN:
1248 dev->ctl_y_even = c->value;
1249 restart_overlay = 1;
1250 break;
1251 case V4L2_CID_PRIVATE_Y_ODD:
1252 dev->ctl_y_odd = c->value;
1253 restart_overlay = 1;
1254 break;
1255 case V4L2_CID_PRIVATE_AUTOMUTE:
1257 struct v4l2_priv_tun_config tda9887_cfg;
1259 tda9887_cfg.tuner = TUNER_TDA9887;
1260 tda9887_cfg.priv = &dev->tda9887_conf;
1262 dev->ctl_automute = c->value;
1263 if (dev->tda9887_conf) {
1264 if (dev->ctl_automute)
1265 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1266 else
1267 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1269 saa7134_i2c_call_clients(dev, TUNER_SET_CONFIG,
1270 &tda9887_cfg);
1272 break;
1274 default:
1275 goto error;
1277 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1278 spin_lock_irqsave(&dev->slock,flags);
1279 stop_preview(dev,fh);
1280 start_preview(dev,fh);
1281 spin_unlock_irqrestore(&dev->slock,flags);
1283 err = 0;
1285 error:
1286 mutex_unlock(&dev->lock);
1287 return err;
1290 /* ------------------------------------------------------------------ */
1292 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1294 struct videobuf_queue* q = NULL;
1296 switch (fh->type) {
1297 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1298 q = &fh->cap;
1299 break;
1300 case V4L2_BUF_TYPE_VBI_CAPTURE:
1301 q = &fh->vbi;
1302 break;
1303 default:
1304 BUG();
1306 return q;
1309 static int saa7134_resource(struct saa7134_fh *fh)
1311 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1312 return RESOURCE_VIDEO;
1314 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1315 return RESOURCE_VBI;
1317 BUG();
1318 return 0;
1321 static int video_open(struct inode *inode, struct file *file)
1323 int minor = iminor(inode);
1324 struct saa7134_dev *dev;
1325 struct saa7134_fh *fh;
1326 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1327 int radio = 0;
1328 list_for_each_entry(dev, &saa7134_devlist, devlist) {
1329 if (dev->video_dev && (dev->video_dev->minor == minor))
1330 goto found;
1331 if (dev->radio_dev && (dev->radio_dev->minor == minor)) {
1332 radio = 1;
1333 goto found;
1335 if (dev->vbi_dev && (dev->vbi_dev->minor == minor)) {
1336 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1337 goto found;
1340 return -ENODEV;
1341 found:
1343 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1344 v4l2_type_names[type]);
1346 /* allocate + initialize per filehandle data */
1347 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1348 if (NULL == fh)
1349 return -ENOMEM;
1350 file->private_data = fh;
1351 fh->dev = dev;
1352 fh->radio = radio;
1353 fh->type = type;
1354 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1355 fh->width = 720;
1356 fh->height = 576;
1357 v4l2_prio_open(&dev->prio,&fh->prio);
1359 videobuf_queue_pci_init(&fh->cap, &video_qops,
1360 dev->pci, &dev->slock,
1361 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1362 V4L2_FIELD_INTERLACED,
1363 sizeof(struct saa7134_buf),
1364 fh);
1365 videobuf_queue_pci_init(&fh->vbi, &saa7134_vbi_qops,
1366 dev->pci, &dev->slock,
1367 V4L2_BUF_TYPE_VBI_CAPTURE,
1368 V4L2_FIELD_SEQ_TB,
1369 sizeof(struct saa7134_buf),
1370 fh);
1371 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1372 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1374 if (fh->radio) {
1375 /* switch to radio mode */
1376 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1377 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO, NULL);
1378 } else {
1379 /* switch to video/vbi mode */
1380 video_mux(dev,dev->ctl_input);
1382 return 0;
1385 static ssize_t
1386 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1388 struct saa7134_fh *fh = file->private_data;
1390 switch (fh->type) {
1391 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1392 if (res_locked(fh->dev,RESOURCE_VIDEO))
1393 return -EBUSY;
1394 return videobuf_read_one(saa7134_queue(fh),
1395 data, count, ppos,
1396 file->f_flags & O_NONBLOCK);
1397 case V4L2_BUF_TYPE_VBI_CAPTURE:
1398 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1399 return -EBUSY;
1400 return videobuf_read_stream(saa7134_queue(fh),
1401 data, count, ppos, 1,
1402 file->f_flags & O_NONBLOCK);
1403 break;
1404 default:
1405 BUG();
1406 return 0;
1410 static unsigned int
1411 video_poll(struct file *file, struct poll_table_struct *wait)
1413 struct saa7134_fh *fh = file->private_data;
1414 struct videobuf_buffer *buf = NULL;
1416 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1417 return videobuf_poll_stream(file, &fh->vbi, wait);
1419 if (res_check(fh,RESOURCE_VIDEO)) {
1420 if (!list_empty(&fh->cap.stream))
1421 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1422 } else {
1423 mutex_lock(&fh->cap.lock);
1424 if (UNSET == fh->cap.read_off) {
1425 /* need to capture a new frame */
1426 if (res_locked(fh->dev,RESOURCE_VIDEO)) {
1427 mutex_unlock(&fh->cap.lock);
1428 return POLLERR;
1430 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field)) {
1431 mutex_unlock(&fh->cap.lock);
1432 return POLLERR;
1434 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1435 fh->cap.read_off = 0;
1437 mutex_unlock(&fh->cap.lock);
1438 buf = fh->cap.read_buf;
1441 if (!buf)
1442 return POLLERR;
1444 poll_wait(file, &buf->done, wait);
1445 if (buf->state == VIDEOBUF_DONE ||
1446 buf->state == VIDEOBUF_ERROR)
1447 return POLLIN|POLLRDNORM;
1448 return 0;
1451 static int video_release(struct inode *inode, struct file *file)
1453 struct saa7134_fh *fh = file->private_data;
1454 struct saa7134_dev *dev = fh->dev;
1455 unsigned long flags;
1457 /* turn off overlay */
1458 if (res_check(fh, RESOURCE_OVERLAY)) {
1459 spin_lock_irqsave(&dev->slock,flags);
1460 stop_preview(dev,fh);
1461 spin_unlock_irqrestore(&dev->slock,flags);
1462 res_free(dev,fh,RESOURCE_OVERLAY);
1465 /* stop video capture */
1466 if (res_check(fh, RESOURCE_VIDEO)) {
1467 videobuf_streamoff(&fh->cap);
1468 res_free(dev,fh,RESOURCE_VIDEO);
1470 if (fh->cap.read_buf) {
1471 buffer_release(&fh->cap,fh->cap.read_buf);
1472 kfree(fh->cap.read_buf);
1475 /* stop vbi capture */
1476 if (res_check(fh, RESOURCE_VBI)) {
1477 videobuf_stop(&fh->vbi);
1478 res_free(dev,fh,RESOURCE_VBI);
1481 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1482 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1483 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1484 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1485 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1487 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1489 /* free stuff */
1490 videobuf_mmap_free(&fh->cap);
1491 videobuf_mmap_free(&fh->vbi);
1492 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1493 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1495 v4l2_prio_close(&dev->prio,&fh->prio);
1496 file->private_data = NULL;
1497 kfree(fh);
1498 return 0;
1501 static int video_mmap(struct file *file, struct vm_area_struct * vma)
1503 struct saa7134_fh *fh = file->private_data;
1505 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1508 /* ------------------------------------------------------------------ */
1510 static int vidioc_try_get_set_fmt_vbi(struct file *file, void *priv,
1511 struct v4l2_format *f)
1513 struct saa7134_fh *fh = priv;
1514 struct saa7134_dev *dev = fh->dev;
1515 struct saa7134_tvnorm *norm = dev->tvnorm;
1517 f->fmt.vbi.sampling_rate = 6750000 * 4;
1518 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1519 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1520 f->fmt.vbi.offset = 64 * 4;
1521 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1522 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1523 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1524 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1525 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1527 return 0;
1530 static int vidioc_g_fmt_cap(struct file *file, void *priv,
1531 struct v4l2_format *f)
1533 struct saa7134_fh *fh = priv;
1535 f->fmt.pix.width = fh->width;
1536 f->fmt.pix.height = fh->height;
1537 f->fmt.pix.field = fh->cap.field;
1538 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1539 f->fmt.pix.bytesperline =
1540 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1541 f->fmt.pix.sizeimage =
1542 f->fmt.pix.height * f->fmt.pix.bytesperline;
1543 return 0;
1546 static int vidioc_g_fmt_overlay(struct file *file, void *priv,
1547 struct v4l2_format *f)
1549 struct saa7134_fh *fh = priv;
1551 if (saa7134_no_overlay > 0) {
1552 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1553 return -EINVAL;
1555 f->fmt.win = fh->win;
1557 return 0;
1560 static int vidioc_try_fmt_cap(struct file *file, void *priv,
1561 struct v4l2_format *f)
1563 struct saa7134_fh *fh = priv;
1564 struct saa7134_dev *dev = fh->dev;
1565 struct saa7134_format *fmt;
1566 enum v4l2_field field;
1567 unsigned int maxw, maxh;
1569 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1570 if (NULL == fmt)
1571 return -EINVAL;
1573 field = f->fmt.pix.field;
1574 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1575 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1577 if (V4L2_FIELD_ANY == field) {
1578 field = (f->fmt.pix.height > maxh/2)
1579 ? V4L2_FIELD_INTERLACED
1580 : V4L2_FIELD_BOTTOM;
1582 switch (field) {
1583 case V4L2_FIELD_TOP:
1584 case V4L2_FIELD_BOTTOM:
1585 maxh = maxh / 2;
1586 break;
1587 case V4L2_FIELD_INTERLACED:
1588 break;
1589 default:
1590 return -EINVAL;
1593 f->fmt.pix.field = field;
1594 if (f->fmt.pix.width < 48)
1595 f->fmt.pix.width = 48;
1596 if (f->fmt.pix.height < 32)
1597 f->fmt.pix.height = 32;
1598 if (f->fmt.pix.width > maxw)
1599 f->fmt.pix.width = maxw;
1600 if (f->fmt.pix.height > maxh)
1601 f->fmt.pix.height = maxh;
1602 f->fmt.pix.width &= ~0x03;
1603 f->fmt.pix.bytesperline =
1604 (f->fmt.pix.width * fmt->depth) >> 3;
1605 f->fmt.pix.sizeimage =
1606 f->fmt.pix.height * f->fmt.pix.bytesperline;
1608 return 0;
1611 static int vidioc_try_fmt_overlay(struct file *file, void *priv,
1612 struct v4l2_format *f)
1614 struct saa7134_fh *fh = priv;
1615 struct saa7134_dev *dev = fh->dev;
1617 if (saa7134_no_overlay > 0) {
1618 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1619 return -EINVAL;
1622 return verify_preview(dev, &f->fmt.win);
1625 static int vidioc_s_fmt_cap(struct file *file, void *priv,
1626 struct v4l2_format *f)
1628 struct saa7134_fh *fh = priv;
1629 int err;
1631 err = vidioc_try_fmt_cap(file, priv, f);
1632 if (0 != err)
1633 return err;
1635 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1636 fh->width = f->fmt.pix.width;
1637 fh->height = f->fmt.pix.height;
1638 fh->cap.field = f->fmt.pix.field;
1639 return 0;
1642 static int vidioc_s_fmt_overlay(struct file *file, void *priv,
1643 struct v4l2_format *f)
1645 struct saa7134_fh *fh = priv;
1646 struct saa7134_dev *dev = fh->dev;
1647 int err;
1648 unsigned int flags;
1650 if (saa7134_no_overlay > 0) {
1651 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1652 return -EINVAL;
1654 err = verify_preview(dev, &f->fmt.win);
1655 if (0 != err)
1656 return err;
1658 mutex_lock(&dev->lock);
1660 fh->win = f->fmt.win;
1661 fh->nclips = f->fmt.win.clipcount;
1663 if (fh->nclips > 8)
1664 fh->nclips = 8;
1666 if (copy_from_user(fh->clips, f->fmt.win.clips,
1667 sizeof(struct v4l2_clip)*fh->nclips)) {
1668 mutex_unlock(&dev->lock);
1669 return -EFAULT;
1672 if (res_check(fh, RESOURCE_OVERLAY)) {
1673 spin_lock_irqsave(&dev->slock, flags);
1674 stop_preview(dev, fh);
1675 start_preview(dev, fh);
1676 spin_unlock_irqrestore(&dev->slock, flags);
1679 mutex_unlock(&dev->lock);
1680 return 0;
1683 static int vidioc_queryctrl(struct file *file, void *priv,
1684 struct v4l2_queryctrl *c)
1686 const struct v4l2_queryctrl *ctrl;
1688 if ((c->id < V4L2_CID_BASE ||
1689 c->id >= V4L2_CID_LASTP1) &&
1690 (c->id < V4L2_CID_PRIVATE_BASE ||
1691 c->id >= V4L2_CID_PRIVATE_LASTP1))
1692 return -EINVAL;
1693 ctrl = ctrl_by_id(c->id);
1694 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1695 return 0;
1698 static int vidioc_enum_input(struct file *file, void *priv,
1699 struct v4l2_input *i)
1701 struct saa7134_fh *fh = priv;
1702 struct saa7134_dev *dev = fh->dev;
1703 unsigned int n;
1705 n = i->index;
1706 if (n >= SAA7134_INPUT_MAX)
1707 return -EINVAL;
1708 if (NULL == card_in(dev, i->index).name)
1709 return -EINVAL;
1710 memset(i, 0, sizeof(*i));
1711 i->index = n;
1712 i->type = V4L2_INPUT_TYPE_CAMERA;
1713 strcpy(i->name, card_in(dev, n).name);
1714 if (card_in(dev, n).tv)
1715 i->type = V4L2_INPUT_TYPE_TUNER;
1716 i->audioset = 1;
1717 if (n == dev->ctl_input) {
1718 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1719 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1721 if (0 != (v1 & 0x40))
1722 i->status |= V4L2_IN_ST_NO_H_LOCK;
1723 if (0 != (v2 & 0x40))
1724 i->status |= V4L2_IN_ST_NO_SYNC;
1725 if (0 != (v2 & 0x0e))
1726 i->status |= V4L2_IN_ST_MACROVISION;
1728 i->std = SAA7134_NORMS;
1729 return 0;
1732 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1734 struct saa7134_fh *fh = priv;
1735 struct saa7134_dev *dev = fh->dev;
1737 *i = dev->ctl_input;
1738 return 0;
1741 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1743 struct saa7134_fh *fh = priv;
1744 struct saa7134_dev *dev = fh->dev;
1745 int err;
1747 err = v4l2_prio_check(&dev->prio, &fh->prio);
1748 if (0 != err)
1749 return err;
1751 if (i < 0 || i >= SAA7134_INPUT_MAX)
1752 return -EINVAL;
1753 if (NULL == card_in(dev, i).name)
1754 return -EINVAL;
1755 mutex_lock(&dev->lock);
1756 video_mux(dev, i);
1757 mutex_unlock(&dev->lock);
1758 return 0;
1761 static int vidioc_querycap(struct file *file, void *priv,
1762 struct v4l2_capability *cap)
1764 struct saa7134_fh *fh = priv;
1765 struct saa7134_dev *dev = fh->dev;
1767 unsigned int tuner_type = dev->tuner_type;
1769 strcpy(cap->driver, "saa7134");
1770 strlcpy(cap->card, saa7134_boards[dev->board].name,
1771 sizeof(cap->card));
1772 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1773 cap->version = SAA7134_VERSION_CODE;
1774 cap->capabilities =
1775 V4L2_CAP_VIDEO_CAPTURE |
1776 V4L2_CAP_VBI_CAPTURE |
1777 V4L2_CAP_READWRITE |
1778 V4L2_CAP_STREAMING |
1779 V4L2_CAP_TUNER;
1780 if (saa7134_no_overlay <= 0)
1781 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1783 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1784 cap->capabilities &= ~V4L2_CAP_TUNER;
1785 return 0;
1788 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * id)
1790 struct saa7134_fh *fh = priv;
1791 struct saa7134_dev *dev = fh->dev;
1792 unsigned long flags;
1793 unsigned int i;
1794 v4l2_std_id fixup;
1795 int err;
1797 err = v4l2_prio_check(&dev->prio, &fh->prio);
1798 if (0 != err)
1799 return err;
1801 for (i = 0; i < TVNORMS; i++)
1802 if (*id == tvnorms[i].id)
1803 break;
1805 if (i == TVNORMS)
1806 for (i = 0; i < TVNORMS; i++)
1807 if (*id & tvnorms[i].id)
1808 break;
1809 if (i == TVNORMS)
1810 return -EINVAL;
1812 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1813 if (secam[0] == 'L' || secam[0] == 'l') {
1814 if (secam[1] == 'C' || secam[1] == 'c')
1815 fixup = V4L2_STD_SECAM_LC;
1816 else
1817 fixup = V4L2_STD_SECAM_L;
1818 } else {
1819 if (secam[0] == 'D' || secam[0] == 'd')
1820 fixup = V4L2_STD_SECAM_DK;
1821 else
1822 fixup = V4L2_STD_SECAM;
1824 for (i = 0; i < TVNORMS; i++)
1825 if (fixup == tvnorms[i].id)
1826 break;
1829 *id = tvnorms[i].id;
1831 mutex_lock(&dev->lock);
1832 if (res_check(fh, RESOURCE_OVERLAY)) {
1833 spin_lock_irqsave(&dev->slock, flags);
1834 stop_preview(dev, fh);
1835 spin_unlock_irqrestore(&dev->slock, flags);
1837 set_tvnorm(dev, &tvnorms[i]);
1839 spin_lock_irqsave(&dev->slock, flags);
1840 start_preview(dev, fh);
1841 spin_unlock_irqrestore(&dev->slock, flags);
1842 } else
1843 set_tvnorm(dev, &tvnorms[i]);
1845 saa7134_tvaudio_do_scan(dev);
1846 mutex_unlock(&dev->lock);
1847 return 0;
1850 static int vidioc_cropcap(struct file *file, void *priv,
1851 struct v4l2_cropcap *cap)
1853 struct saa7134_fh *fh = priv;
1854 struct saa7134_dev *dev = fh->dev;
1856 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1857 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1858 return -EINVAL;
1859 cap->bounds = dev->crop_bounds;
1860 cap->defrect = dev->crop_defrect;
1861 cap->pixelaspect.numerator = 1;
1862 cap->pixelaspect.denominator = 1;
1863 if (dev->tvnorm->id & V4L2_STD_525_60) {
1864 cap->pixelaspect.numerator = 11;
1865 cap->pixelaspect.denominator = 10;
1867 if (dev->tvnorm->id & V4L2_STD_625_50) {
1868 cap->pixelaspect.numerator = 54;
1869 cap->pixelaspect.denominator = 59;
1871 return 0;
1874 static int vidioc_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1876 struct saa7134_fh *fh = f;
1877 struct saa7134_dev *dev = fh->dev;
1879 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1880 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1881 return -EINVAL;
1882 crop->c = dev->crop_current;
1883 return 0;
1886 static int vidioc_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
1888 struct saa7134_fh *fh = f;
1889 struct saa7134_dev *dev = fh->dev;
1890 struct v4l2_rect *b = &dev->crop_bounds;
1892 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1893 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1894 return -EINVAL;
1895 if (crop->c.height < 0)
1896 return -EINVAL;
1897 if (crop->c.width < 0)
1898 return -EINVAL;
1900 if (res_locked(fh->dev, RESOURCE_OVERLAY))
1901 return -EBUSY;
1902 if (res_locked(fh->dev, RESOURCE_VIDEO))
1903 return -EBUSY;
1905 if (crop->c.top < b->top)
1906 crop->c.top = b->top;
1907 if (crop->c.top > b->top + b->height)
1908 crop->c.top = b->top + b->height;
1909 if (crop->c.height > b->top - crop->c.top + b->height)
1910 crop->c.height = b->top - crop->c.top + b->height;
1912 if (crop->c.left < b->left)
1913 crop->c.left = b->left;
1914 if (crop->c.left > b->left + b->width)
1915 crop->c.left = b->left + b->width;
1916 if (crop->c.width > b->left - crop->c.left + b->width)
1917 crop->c.width = b->left - crop->c.left + b->width;
1919 dev->crop_current = crop->c;
1920 return 0;
1923 static int vidioc_g_tuner(struct file *file, void *priv,
1924 struct v4l2_tuner *t)
1926 struct saa7134_fh *fh = priv;
1927 struct saa7134_dev *dev = fh->dev;
1928 int n;
1930 if (0 != t->index)
1931 return -EINVAL;
1932 memset(t, 0, sizeof(*t));
1933 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1934 if (card_in(dev, n).tv)
1935 break;
1936 if (NULL != card_in(dev, n).name) {
1937 strcpy(t->name, "Television");
1938 t->type = V4L2_TUNER_ANALOG_TV;
1939 t->capability = V4L2_TUNER_CAP_NORM |
1940 V4L2_TUNER_CAP_STEREO |
1941 V4L2_TUNER_CAP_LANG1 |
1942 V4L2_TUNER_CAP_LANG2;
1943 t->rangehigh = 0xffffffffUL;
1944 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1945 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1947 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1948 t->signal = 0xffff;
1949 return 0;
1952 static int vidioc_s_tuner(struct file *file, void *priv,
1953 struct v4l2_tuner *t)
1955 struct saa7134_fh *fh = priv;
1956 struct saa7134_dev *dev = fh->dev;
1957 int rx, mode, err;
1959 err = v4l2_prio_check(&dev->prio, &fh->prio);
1960 if (0 != err)
1961 return err;
1963 mode = dev->thread.mode;
1964 if (UNSET == mode) {
1965 rx = saa7134_tvaudio_getstereo(dev);
1966 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1968 if (mode != t->audmode)
1969 dev->thread.mode = t->audmode;
1971 return 0;
1974 static int vidioc_g_frequency(struct file *file, void *priv,
1975 struct v4l2_frequency *f)
1977 struct saa7134_fh *fh = priv;
1978 struct saa7134_dev *dev = fh->dev;
1980 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1981 f->frequency = dev->ctl_freq;
1983 return 0;
1986 static int vidioc_s_frequency(struct file *file, void *priv,
1987 struct v4l2_frequency *f)
1989 struct saa7134_fh *fh = priv;
1990 struct saa7134_dev *dev = fh->dev;
1991 int err;
1993 err = v4l2_prio_check(&dev->prio, &fh->prio);
1994 if (0 != err)
1995 return err;
1997 if (0 != f->tuner)
1998 return -EINVAL;
1999 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
2000 return -EINVAL;
2001 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
2002 return -EINVAL;
2003 mutex_lock(&dev->lock);
2004 dev->ctl_freq = f->frequency;
2006 saa7134_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
2008 saa7134_tvaudio_do_scan(dev);
2009 mutex_unlock(&dev->lock);
2010 return 0;
2013 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2015 strcpy(a->name, "audio");
2016 return 0;
2019 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
2021 return 0;
2024 static int vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2026 struct saa7134_fh *fh = f;
2027 struct saa7134_dev *dev = fh->dev;
2029 *p = v4l2_prio_max(&dev->prio);
2030 return 0;
2033 static int vidioc_s_priority(struct file *file, void *f,
2034 enum v4l2_priority prio)
2036 struct saa7134_fh *fh = f;
2037 struct saa7134_dev *dev = fh->dev;
2039 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
2042 static int vidioc_enum_fmt_cap(struct file *file, void *priv,
2043 struct v4l2_fmtdesc *f)
2045 if (f->index >= FORMATS)
2046 return -EINVAL;
2048 strlcpy(f->description, formats[f->index].name,
2049 sizeof(f->description));
2051 f->pixelformat = formats[f->index].fourcc;
2053 return 0;
2056 static int vidioc_enum_fmt_overlay(struct file *file, void *priv,
2057 struct v4l2_fmtdesc *f)
2059 if (saa7134_no_overlay > 0) {
2060 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2061 return -EINVAL;
2064 if ((f->index >= FORMATS) || formats[f->index].planar)
2065 return -EINVAL;
2067 strlcpy(f->description, formats[f->index].name,
2068 sizeof(f->description));
2070 f->pixelformat = formats[f->index].fourcc;
2072 return 0;
2075 static int vidioc_enum_fmt_vbi(struct file *file, void *priv,
2076 struct v4l2_fmtdesc *f)
2078 if (0 != f->index)
2079 return -EINVAL;
2081 f->pixelformat = V4L2_PIX_FMT_GREY;
2082 strcpy(f->description, "vbi data");
2084 return 0;
2087 static int vidioc_g_fbuf(struct file *file, void *f,
2088 struct v4l2_framebuffer *fb)
2090 struct saa7134_fh *fh = f;
2091 struct saa7134_dev *dev = fh->dev;
2093 *fb = dev->ovbuf;
2094 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2096 return 0;
2099 static int vidioc_s_fbuf(struct file *file, void *f,
2100 struct v4l2_framebuffer *fb)
2102 struct saa7134_fh *fh = f;
2103 struct saa7134_dev *dev = fh->dev;
2104 struct saa7134_format *fmt;
2106 if (!capable(CAP_SYS_ADMIN) &&
2107 !capable(CAP_SYS_RAWIO))
2108 return -EPERM;
2110 /* check args */
2111 fmt = format_by_fourcc(fb->fmt.pixelformat);
2112 if (NULL == fmt)
2113 return -EINVAL;
2115 /* ok, accept it */
2116 dev->ovbuf = *fb;
2117 dev->ovfmt = fmt;
2118 if (0 == dev->ovbuf.fmt.bytesperline)
2119 dev->ovbuf.fmt.bytesperline =
2120 dev->ovbuf.fmt.width*fmt->depth/8;
2121 return 0;
2124 static int vidioc_overlay(struct file *file, void *f, unsigned int on)
2126 struct saa7134_fh *fh = f;
2127 struct saa7134_dev *dev = fh->dev;
2128 unsigned long flags;
2130 if (on) {
2131 if (saa7134_no_overlay > 0) {
2132 dprintk("no_overlay\n");
2133 return -EINVAL;
2136 if (!res_get(dev, fh, RESOURCE_OVERLAY))
2137 return -EBUSY;
2138 spin_lock_irqsave(&dev->slock, flags);
2139 start_preview(dev, fh);
2140 spin_unlock_irqrestore(&dev->slock, flags);
2142 if (!on) {
2143 if (!res_check(fh, RESOURCE_OVERLAY))
2144 return -EINVAL;
2145 spin_lock_irqsave(&dev->slock, flags);
2146 stop_preview(dev, fh);
2147 spin_unlock_irqrestore(&dev->slock, flags);
2148 res_free(dev, fh, RESOURCE_OVERLAY);
2150 return 0;
2153 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2154 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2156 struct saa7134_fh *fh = file->private_data;
2157 return videobuf_cgmbuf(saa7134_queue(fh), mbuf, 8);
2159 #endif
2161 static int vidioc_reqbufs(struct file *file, void *priv,
2162 struct v4l2_requestbuffers *p)
2164 struct saa7134_fh *fh = priv;
2165 return videobuf_reqbufs(saa7134_queue(fh), p);
2168 static int vidioc_querybuf(struct file *file, void *priv,
2169 struct v4l2_buffer *b)
2171 struct saa7134_fh *fh = priv;
2172 return videobuf_querybuf(saa7134_queue(fh), b);
2175 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2177 struct saa7134_fh *fh = priv;
2178 return videobuf_qbuf(saa7134_queue(fh), b);
2181 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2183 struct saa7134_fh *fh = priv;
2184 return videobuf_dqbuf(saa7134_queue(fh), b,
2185 file->f_flags & O_NONBLOCK);
2188 static int vidioc_streamon(struct file *file, void *priv,
2189 enum v4l2_buf_type type)
2191 struct saa7134_fh *fh = priv;
2192 struct saa7134_dev *dev = fh->dev;
2193 int res = saa7134_resource(fh);
2195 if (!res_get(dev, fh, res))
2196 return -EBUSY;
2198 return videobuf_streamon(saa7134_queue(fh));
2201 static int vidioc_streamoff(struct file *file, void *priv,
2202 enum v4l2_buf_type type)
2204 int err;
2205 struct saa7134_fh *fh = priv;
2206 struct saa7134_dev *dev = fh->dev;
2207 int res = saa7134_resource(fh);
2209 err = videobuf_streamoff(saa7134_queue(fh));
2210 if (err < 0)
2211 return err;
2212 res_free(dev, fh, res);
2213 return 0;
2216 static int vidioc_g_parm(struct file *file, void *fh,
2217 struct v4l2_streamparm *parm)
2219 return 0;
2222 static int radio_querycap(struct file *file, void *priv,
2223 struct v4l2_capability *cap)
2225 struct saa7134_fh *fh = file->private_data;
2226 struct saa7134_dev *dev = fh->dev;
2228 strcpy(cap->driver, "saa7134");
2229 strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
2230 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
2231 cap->version = SAA7134_VERSION_CODE;
2232 cap->capabilities = V4L2_CAP_TUNER;
2233 return 0;
2236 static int radio_g_tuner(struct file *file, void *priv,
2237 struct v4l2_tuner *t)
2239 struct saa7134_fh *fh = file->private_data;
2240 struct saa7134_dev *dev = fh->dev;
2242 if (0 != t->index)
2243 return -EINVAL;
2245 memset(t, 0, sizeof(*t));
2246 strcpy(t->name, "Radio");
2247 t->type = V4L2_TUNER_RADIO;
2249 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
2250 if (dev->input->amux == TV) {
2251 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2252 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2253 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2255 return 0;
2257 static int radio_s_tuner(struct file *file, void *priv,
2258 struct v4l2_tuner *t)
2260 struct saa7134_fh *fh = file->private_data;
2261 struct saa7134_dev *dev = fh->dev;
2263 if (0 != t->index)
2264 return -EINVAL;
2266 saa7134_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
2267 return 0;
2270 static int radio_enum_input(struct file *file, void *priv,
2271 struct v4l2_input *i)
2273 if (i->index != 0)
2274 return -EINVAL;
2276 strcpy(i->name, "Radio");
2277 i->type = V4L2_INPUT_TYPE_TUNER;
2279 return 0;
2282 static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2284 *i = 0;
2285 return 0;
2288 static int radio_g_audio(struct file *file, void *priv,
2289 struct v4l2_audio *a)
2291 memset(a, 0, sizeof(*a));
2292 strcpy(a->name, "Radio");
2293 return 0;
2296 static int radio_s_audio(struct file *file, void *priv,
2297 struct v4l2_audio *a)
2299 return 0;
2302 static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2304 return 0;
2307 static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
2309 return 0;
2312 static int radio_queryctrl(struct file *file, void *priv,
2313 struct v4l2_queryctrl *c)
2315 const struct v4l2_queryctrl *ctrl;
2317 if (c->id < V4L2_CID_BASE ||
2318 c->id >= V4L2_CID_LASTP1)
2319 return -EINVAL;
2320 if (c->id == V4L2_CID_AUDIO_MUTE) {
2321 ctrl = ctrl_by_id(c->id);
2322 *c = *ctrl;
2323 } else
2324 *c = no_ctrl;
2325 return 0;
2328 static const struct file_operations video_fops =
2330 .owner = THIS_MODULE,
2331 .open = video_open,
2332 .release = video_release,
2333 .read = video_read,
2334 .poll = video_poll,
2335 .mmap = video_mmap,
2336 .ioctl = video_ioctl2,
2337 .compat_ioctl = v4l_compat_ioctl32,
2338 .llseek = no_llseek,
2341 static const struct file_operations radio_fops =
2343 .owner = THIS_MODULE,
2344 .open = video_open,
2345 .release = video_release,
2346 .ioctl = video_ioctl2,
2347 .compat_ioctl = v4l_compat_ioctl32,
2348 .llseek = no_llseek,
2351 /* ----------------------------------------------------------- */
2352 /* exported stuff */
2354 struct video_device saa7134_video_template =
2356 .name = "saa7134-video",
2357 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER |
2358 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2359 .fops = &video_fops,
2360 .minor = -1,
2361 .vidioc_querycap = vidioc_querycap,
2362 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
2363 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
2364 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
2365 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
2366 .vidioc_enum_fmt_overlay = vidioc_enum_fmt_overlay,
2367 .vidioc_g_fmt_overlay = vidioc_g_fmt_overlay,
2368 .vidioc_try_fmt_overlay = vidioc_try_fmt_overlay,
2369 .vidioc_s_fmt_overlay = vidioc_s_fmt_overlay,
2370 .vidioc_enum_fmt_vbi = vidioc_enum_fmt_vbi,
2371 .vidioc_g_fmt_vbi = vidioc_try_get_set_fmt_vbi,
2372 .vidioc_try_fmt_vbi = vidioc_try_get_set_fmt_vbi,
2373 .vidioc_s_fmt_vbi = vidioc_try_get_set_fmt_vbi,
2374 .vidioc_g_audio = vidioc_g_audio,
2375 .vidioc_s_audio = vidioc_s_audio,
2376 .vidioc_cropcap = vidioc_cropcap,
2377 .vidioc_reqbufs = vidioc_reqbufs,
2378 .vidioc_querybuf = vidioc_querybuf,
2379 .vidioc_qbuf = vidioc_qbuf,
2380 .vidioc_dqbuf = vidioc_dqbuf,
2381 .vidioc_s_std = vidioc_s_std,
2382 .vidioc_enum_input = vidioc_enum_input,
2383 .vidioc_g_input = vidioc_g_input,
2384 .vidioc_s_input = vidioc_s_input,
2385 .vidioc_queryctrl = vidioc_queryctrl,
2386 .vidioc_g_ctrl = vidioc_g_ctrl,
2387 .vidioc_s_ctrl = vidioc_s_ctrl,
2388 .vidioc_streamon = vidioc_streamon,
2389 .vidioc_streamoff = vidioc_streamoff,
2390 .vidioc_g_tuner = vidioc_g_tuner,
2391 .vidioc_s_tuner = vidioc_s_tuner,
2392 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2393 .vidiocgmbuf = vidiocgmbuf,
2394 #endif
2395 .vidioc_g_crop = vidioc_g_crop,
2396 .vidioc_s_crop = vidioc_s_crop,
2397 .vidioc_g_fbuf = vidioc_g_fbuf,
2398 .vidioc_s_fbuf = vidioc_s_fbuf,
2399 .vidioc_overlay = vidioc_overlay,
2400 .vidioc_g_priority = vidioc_g_priority,
2401 .vidioc_s_priority = vidioc_s_priority,
2402 .vidioc_g_parm = vidioc_g_parm,
2403 .vidioc_g_frequency = vidioc_g_frequency,
2404 .vidioc_s_frequency = vidioc_s_frequency,
2405 .tvnorms = SAA7134_NORMS,
2406 .current_norm = V4L2_STD_PAL,
2409 struct video_device saa7134_vbi_template =
2411 .name = "saa7134-vbi",
2412 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
2413 .fops = &video_fops,
2414 .minor = -1,
2417 struct video_device saa7134_radio_template =
2419 .name = "saa7134-radio",
2420 .type = VID_TYPE_TUNER,
2421 .fops = &radio_fops,
2422 .minor = -1,
2423 .vidioc_querycap = radio_querycap,
2424 .vidioc_g_tuner = radio_g_tuner,
2425 .vidioc_enum_input = radio_enum_input,
2426 .vidioc_g_audio = radio_g_audio,
2427 .vidioc_s_tuner = radio_s_tuner,
2428 .vidioc_s_audio = radio_s_audio,
2429 .vidioc_s_input = radio_s_input,
2430 .vidioc_s_std = radio_s_std,
2431 .vidioc_queryctrl = radio_queryctrl,
2432 .vidioc_g_input = radio_g_input,
2433 .vidioc_g_ctrl = vidioc_g_ctrl,
2434 .vidioc_s_ctrl = vidioc_s_ctrl,
2435 .vidioc_g_frequency = vidioc_g_frequency,
2436 .vidioc_s_frequency = vidioc_s_frequency,
2439 int saa7134_video_init1(struct saa7134_dev *dev)
2441 /* sanitycheck insmod options */
2442 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2443 gbuffers = 2;
2444 if (gbufsize < 0 || gbufsize > gbufsize_max)
2445 gbufsize = gbufsize_max;
2446 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2448 /* put some sensible defaults into the data structures ... */
2449 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2450 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2451 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2452 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2453 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2454 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2455 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2456 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2458 if (dev->tda9887_conf && dev->ctl_automute)
2459 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2460 dev->automute = 0;
2462 INIT_LIST_HEAD(&dev->video_q.queue);
2463 init_timer(&dev->video_q.timeout);
2464 dev->video_q.timeout.function = saa7134_buffer_timeout;
2465 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2466 dev->video_q.dev = dev;
2468 if (saa7134_boards[dev->board].video_out)
2469 saa7134_videoport_init(dev);
2471 return 0;
2474 int saa7134_videoport_init(struct saa7134_dev *dev)
2476 /* enable video output */
2477 int vo = saa7134_boards[dev->board].video_out;
2478 int video_reg;
2479 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2480 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2481 video_reg = video_out[vo][1];
2482 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2483 video_reg &= ~VP_T_CODE_P_INVERTED;
2484 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2485 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2486 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2487 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2488 video_reg = video_out[vo][5];
2489 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2490 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2491 if (vid_port_opts & SET_CLOCK_INVERTED)
2492 video_reg |= VP_CLK_CTRL1_INVERTED;
2493 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2494 video_reg = video_out[vo][6];
2495 if (vid_port_opts & SET_VSYNC_OFF) {
2496 video_reg &= ~VP_VS_TYPE_MASK;
2497 video_reg |= VP_VS_TYPE_OFF;
2499 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2500 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2501 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2503 return 0;
2506 int saa7134_video_init2(struct saa7134_dev *dev)
2508 /* init video hw */
2509 set_tvnorm(dev,&tvnorms[0]);
2510 video_mux(dev,0);
2511 saa7134_tvaudio_setmute(dev);
2512 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2513 return 0;
2516 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2518 static const char *st[] = {
2519 "(no signal)", "NTSC", "PAL", "SECAM" };
2520 u32 st1,st2;
2522 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2523 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2524 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2525 (st1 & 0x40) ? "not locked" : "locked",
2526 (st2 & 0x40) ? "no" : "yes",
2527 st[st1 & 0x03]);
2528 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
2530 if (dev->nosignal) {
2531 /* no video signal -> mute audio */
2532 if (dev->ctl_automute)
2533 dev->automute = 1;
2534 saa7134_tvaudio_setmute(dev);
2535 } else {
2536 /* wake up tvaudio audio carrier scan thread */
2537 saa7134_tvaudio_do_scan(dev);
2540 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2541 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2542 else
2543 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2545 if (dev->mops && dev->mops->signal_change)
2546 dev->mops->signal_change(dev);
2550 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2552 enum v4l2_field field;
2554 spin_lock(&dev->slock);
2555 if (dev->video_q.curr) {
2556 dev->video_fieldcount++;
2557 field = dev->video_q.curr->vb.field;
2558 if (V4L2_FIELD_HAS_BOTH(field)) {
2559 /* make sure we have seen both fields */
2560 if ((status & 0x10) == 0x00) {
2561 dev->video_q.curr->top_seen = 1;
2562 goto done;
2564 if (!dev->video_q.curr->top_seen)
2565 goto done;
2566 } else if (field == V4L2_FIELD_TOP) {
2567 if ((status & 0x10) != 0x10)
2568 goto done;
2569 } else if (field == V4L2_FIELD_BOTTOM) {
2570 if ((status & 0x10) != 0x00)
2571 goto done;
2573 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2574 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
2576 saa7134_buffer_next(dev,&dev->video_q);
2578 done:
2579 spin_unlock(&dev->slock);
2582 /* ----------------------------------------------------------- */
2584 * Local variables:
2585 * c-basic-offset: 8
2586 * End: