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