V4L/DVB (8915): cx18: Increment u8 pointers not void pointers.
[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
8c85454a 41unsigned int video_debug;
1da177e4 42static unsigned int gbuffers = 8;
ff699e6b 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 56
8c85454a 57#define dprintk(fmt, arg...) if (video_debug&0x04) \
1da177e4
LT
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 dprintk("set tv norm = %s\n",norm->name);
546 dev->tvnorm = norm;
547
1da177e4
LT
548 /* setup cropping */
549 dev->crop_bounds.left = norm->h_start;
550 dev->crop_defrect.left = norm->h_start;
551 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
552 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
553
f246a817 554 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
1da177e4
LT
555 dev->crop_defrect.top = norm->video_v_start*2;
556 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
557 - dev->crop_bounds.top;
558 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
559
560 dev->crop_current = dev->crop_defrect;
561
c458473e 562 saa7134_set_tvnorm_hw(dev);
cb71201f
ML
563}
564
565static void video_mux(struct saa7134_dev *dev, int input)
566{
567 dprintk("video input = %d [%s]\n", input, card_in(dev, input).name);
568 dev->ctl_input = input;
569 set_tvnorm(dev, dev->tvnorm);
570 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
571}
572
c458473e
ML
573
574static void saa7134_set_decoder(struct saa7134_dev *dev)
cb71201f
ML
575{
576 int luma_control, sync_control, mux;
577
578 struct saa7134_tvnorm *norm = dev->tvnorm;
579 mux = card_in(dev, dev->ctl_input).vmux;
580
581 luma_control = norm->luma_control;
582 sync_control = norm->sync_control;
583
584 if (mux > 5)
585 luma_control |= 0x80; /* svideo */
586 if (noninterlaced || dev->nosignal)
587 sync_control |= 0x20;
588
1da177e4
LT
589 /* setup video decoder */
590 saa_writeb(SAA7134_INCR_DELAY, 0x08);
591 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
592 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
593
594 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
595 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
596 saa_writeb(SAA7134_HSYNC_START, 0xeb);
597 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
598 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
599
600 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
601 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
602 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1da177e4 603
f5a1ac64
ML
604 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
605 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
606
607 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
608 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
609
1da177e4
LT
610 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
611 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
612 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
613
614 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
615 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
616
617 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
618 saa_writeb(SAA7134_VGATE_START, 0x11);
619 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
620 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
621 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
622 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
1da177e4
LT
623}
624
c458473e
ML
625void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
626{
627 saa7134_set_decoder(dev);
628
7bff4b4d 629 if (card_in(dev, dev->ctl_input).tv)
c458473e 630 saa7134_i2c_call_clients(dev, VIDIOC_S_STD, &dev->tvnorm->id);
c458473e
ML
631}
632
1da177e4
LT
633static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
634{
635 static const struct {
636 int xpsc;
637 int xacl;
638 int xc2_1;
639 int xdcg;
640 int vpfy;
641 } vals[] = {
642 /* XPSC XACL XC2_1 XDCG VPFY */
643 { 1, 0, 0, 0, 0 },
644 { 2, 2, 1, 2, 2 },
645 { 3, 4, 1, 3, 2 },
646 { 4, 8, 1, 4, 2 },
647 { 5, 8, 1, 4, 2 },
648 { 6, 8, 1, 4, 3 },
649 { 7, 8, 1, 4, 3 },
650 { 8, 15, 0, 4, 3 },
651 { 9, 15, 0, 4, 3 },
652 { 10, 16, 1, 5, 3 },
653 };
654 static const int count = ARRAY_SIZE(vals);
655 int i;
656
657 for (i = 0; i < count; i++)
658 if (vals[i].xpsc == prescale)
659 break;
660 if (i == count)
661 return;
662
663 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
664 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
665 saa_writeb(SAA7134_LEVEL_CTRL(task),
666 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
667 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
668 (vals[i].vpfy << 2) | vals[i].vpfy);
669}
670
671static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
672{
673 int val,mirror;
674
675 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
676 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
677
678 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
679 if (yscale < 2048) {
680 /* LPI */
681 dprintk("yscale LPI yscale=%d\n",yscale);
682 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
683 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
684 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
685 } else {
686 /* ACM */
687 val = 0x40 * 1024 / yscale;
688 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
689 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
690 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
691 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
692 }
693 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
694}
695
696static void set_size(struct saa7134_dev *dev, int task,
697 int width, int height, int interlace)
698{
699 int prescale,xscale,yscale,y_even,y_odd;
700 int h_start, h_stop, v_start, v_stop;
701 int div = interlace ? 2 : 1;
702
703 /* setup video scaler */
704 h_start = dev->crop_current.left;
705 v_start = dev->crop_current.top/2;
706 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
707 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
708
709 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
710 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
711 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
712 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
713 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
714 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
715 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
716 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
717
718 prescale = dev->crop_current.width / width;
719 if (0 == prescale)
720 prescale = 1;
721 xscale = 1024 * dev->crop_current.width / prescale / width;
722 yscale = 512 * div * dev->crop_current.height / height;
4ac97914 723 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
1da177e4
LT
724 set_h_prescale(dev,task,prescale);
725 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
726 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
727 set_v_scale(dev,task,yscale);
728
729 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
730 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
731 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
732 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
733
734 /* deinterlace y offsets */
735 y_odd = dev->ctl_y_odd;
736 y_even = dev->ctl_y_even;
737 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
738 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
739 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
740 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
741}
742
743/* ------------------------------------------------------------------ */
744
745struct cliplist {
746 __u16 position;
747 __u8 enable;
748 __u8 disable;
749};
750
1da177e4
LT
751static void set_cliplist(struct saa7134_dev *dev, int reg,
752 struct cliplist *cl, int entries, char *name)
753{
754 __u8 winbits = 0;
755 int i;
756
757 for (i = 0; i < entries; i++) {
758 winbits |= cl[i].enable;
759 winbits &= ~cl[i].disable;
760 if (i < 15 && cl[i].position == cl[i+1].position)
761 continue;
762 saa_writeb(reg + 0, winbits);
763 saa_writeb(reg + 2, cl[i].position & 0xff);
764 saa_writeb(reg + 3, cl[i].position >> 8);
765 dprintk("clip: %s winbits=%02x pos=%d\n",
766 name,winbits,cl[i].position);
767 reg += 8;
768 }
769 for (; reg < 0x400; reg += 8) {
770 saa_writeb(reg+ 0, 0);
771 saa_writeb(reg + 1, 0);
772 saa_writeb(reg + 2, 0);
773 saa_writeb(reg + 3, 0);
774 }
775}
776
777static int clip_range(int val)
778{
779 if (val < 0)
780 val = 0;
781 return val;
782}
783
9040b32e
HO
784/* Sort into smallest position first order */
785static int cliplist_cmp(const void *a, const void *b)
786{
787 const struct cliplist *cla = a;
788 const struct cliplist *clb = b;
789 if (cla->position < clb->position)
790 return -1;
791 if (cla->position > clb->position)
792 return 1;
793 return 0;
794}
795
1da177e4
LT
796static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
797 int nclips, int interlace)
798{
799 struct cliplist col[16], row[16];
9040b32e 800 int cols = 0, rows = 0, i;
1da177e4
LT
801 int div = interlace ? 2 : 1;
802
9040b32e
HO
803 memset(col, 0, sizeof(col));
804 memset(row, 0, sizeof(row));
1da177e4
LT
805 for (i = 0; i < nclips && i < 8; i++) {
806 col[cols].position = clip_range(clips[i].c.left);
807 col[cols].enable = (1 << i);
808 cols++;
809 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
810 col[cols].disable = (1 << i);
811 cols++;
812 row[rows].position = clip_range(clips[i].c.top / div);
813 row[rows].enable = (1 << i);
814 rows++;
815 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
816 / div);
817 row[rows].disable = (1 << i);
818 rows++;
819 }
9040b32e
HO
820 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
821 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
1da177e4
LT
822 set_cliplist(dev,0x380,col,cols,"cols");
823 set_cliplist(dev,0x384,row,rows,"rows");
824 return 0;
825}
826
827static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
828{
829 enum v4l2_field field;
830 int maxw, maxh;
831
832 if (NULL == dev->ovbuf.base)
833 return -EINVAL;
834 if (NULL == dev->ovfmt)
835 return -EINVAL;
836 if (win->w.width < 48 || win->w.height < 32)
837 return -EINVAL;
838 if (win->clipcount > 2048)
839 return -EINVAL;
840
841 field = win->field;
842 maxw = dev->crop_current.width;
843 maxh = dev->crop_current.height;
844
845 if (V4L2_FIELD_ANY == field) {
4ac97914
MCC
846 field = (win->w.height > maxh/2)
847 ? V4L2_FIELD_INTERLACED
848 : V4L2_FIELD_TOP;
849 }
850 switch (field) {
851 case V4L2_FIELD_TOP:
852 case V4L2_FIELD_BOTTOM:
853 maxh = maxh / 2;
854 break;
855 case V4L2_FIELD_INTERLACED:
856 break;
857 default:
858 return -EINVAL;
859 }
1da177e4
LT
860
861 win->field = field;
862 if (win->w.width > maxw)
863 win->w.width = maxw;
864 if (win->w.height > maxh)
865 win->w.height = maxh;
866 return 0;
867}
868
869static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
870{
871 unsigned long base,control,bpl;
872 int err;
873
874 err = verify_preview(dev,&fh->win);
875 if (0 != err)
876 return err;
877
878 dev->ovfield = fh->win.field;
879 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
880 fh->win.w.width,fh->win.w.height,
881 fh->win.w.left,fh->win.w.top,
882 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
883
884 /* setup window + clipping */
885 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
886 V4L2_FIELD_HAS_BOTH(dev->ovfield));
887 setup_clipping(dev,fh->clips,fh->nclips,
888 V4L2_FIELD_HAS_BOTH(dev->ovfield));
889 if (dev->ovfmt->yuv)
890 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
891 else
892 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
893 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
894
895 /* dma: setup channel 1 (= Video Task B) */
896 base = (unsigned long)dev->ovbuf.base;
897 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
898 base += dev->ovfmt->depth/8 * fh->win.w.left;
899 bpl = dev->ovbuf.fmt.bytesperline;
900 control = SAA7134_RS_CONTROL_BURST_16;
901 if (dev->ovfmt->bswap)
902 control |= SAA7134_RS_CONTROL_BSWAP;
903 if (dev->ovfmt->wswap)
904 control |= SAA7134_RS_CONTROL_WSWAP;
905 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
906 saa_writel(SAA7134_RS_BA1(1),base);
907 saa_writel(SAA7134_RS_BA2(1),base+bpl);
908 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
909 saa_writel(SAA7134_RS_CONTROL(1),control);
910 } else {
911 saa_writel(SAA7134_RS_BA1(1),base);
912 saa_writel(SAA7134_RS_BA2(1),base);
913 saa_writel(SAA7134_RS_PITCH(1),bpl);
914 saa_writel(SAA7134_RS_CONTROL(1),control);
915 }
916
917 /* start dma */
918 dev->ovenable = 1;
919 saa7134_set_dmabits(dev);
920
921 return 0;
922}
923
924static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
925{
926 dev->ovenable = 0;
927 saa7134_set_dmabits(dev);
928 return 0;
929}
930
931/* ------------------------------------------------------------------ */
932
933static int buffer_activate(struct saa7134_dev *dev,
934 struct saa7134_buf *buf,
935 struct saa7134_buf *next)
936{
937 unsigned long base,control,bpl;
938 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
939
940 dprintk("buffer_activate buf=%p\n",buf);
0fc0686e 941 buf->vb.state = VIDEOBUF_ACTIVE;
1da177e4
LT
942 buf->top_seen = 0;
943
944 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
945 V4L2_FIELD_HAS_BOTH(buf->vb.field));
946 if (buf->fmt->yuv)
947 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
948 else
949 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
950 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
951
952 /* DMA: setup channel 0 (= Video Task A0) */
953 base = saa7134_buffer_base(buf);
954 if (buf->fmt->planar)
955 bpl = buf->vb.width;
956 else
957 bpl = (buf->vb.width * buf->fmt->depth) / 8;
958 control = SAA7134_RS_CONTROL_BURST_16 |
959 SAA7134_RS_CONTROL_ME |
960 (buf->pt->dma >> 12);
961 if (buf->fmt->bswap)
962 control |= SAA7134_RS_CONTROL_BSWAP;
963 if (buf->fmt->wswap)
964 control |= SAA7134_RS_CONTROL_WSWAP;
965 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
966 /* interlaced */
967 saa_writel(SAA7134_RS_BA1(0),base);
968 saa_writel(SAA7134_RS_BA2(0),base+bpl);
969 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
970 } else {
971 /* non-interlaced */
972 saa_writel(SAA7134_RS_BA1(0),base);
973 saa_writel(SAA7134_RS_BA2(0),base);
974 saa_writel(SAA7134_RS_PITCH(0),bpl);
975 }
976 saa_writel(SAA7134_RS_CONTROL(0),control);
977
978 if (buf->fmt->planar) {
979 /* DMA: setup channel 4+5 (= planar task A) */
980 bpl_uv = bpl >> buf->fmt->hshift;
981 lines_uv = buf->vb.height >> buf->fmt->vshift;
982 base2 = base + bpl * buf->vb.height;
983 base3 = base2 + bpl_uv * lines_uv;
984 if (buf->fmt->uvswap)
985 tmp = base2, base2 = base3, base3 = tmp;
986 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
987 bpl_uv,lines_uv,base2,base3);
988 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
989 /* interlaced */
990 saa_writel(SAA7134_RS_BA1(4),base2);
991 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
992 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
993 saa_writel(SAA7134_RS_BA1(5),base3);
994 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
995 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
996 } else {
997 /* non-interlaced */
998 saa_writel(SAA7134_RS_BA1(4),base2);
999 saa_writel(SAA7134_RS_BA2(4),base2);
1000 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
1001 saa_writel(SAA7134_RS_BA1(5),base3);
1002 saa_writel(SAA7134_RS_BA2(5),base3);
1003 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
1004 }
1005 saa_writel(SAA7134_RS_CONTROL(4),control);
1006 saa_writel(SAA7134_RS_CONTROL(5),control);
1007 }
1008
1009 /* start DMA */
1010 saa7134_set_dmabits(dev);
1011 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1012 return 0;
1013}
1014
1015static int buffer_prepare(struct videobuf_queue *q,
1016 struct videobuf_buffer *vb,
1017 enum v4l2_field field)
1018{
1019 struct saa7134_fh *fh = q->priv_data;
1020 struct saa7134_dev *dev = fh->dev;
1021 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1022 unsigned int size;
1023 int err;
1024
1025 /* sanity checks */
1026 if (NULL == fh->fmt)
1027 return -EINVAL;
1028 if (fh->width < 48 ||
1029 fh->height < 32 ||
1030 fh->width/4 > dev->crop_current.width ||
1031 fh->height/4 > dev->crop_current.height ||
1032 fh->width > dev->crop_bounds.width ||
1033 fh->height > dev->crop_bounds.height)
1034 return -EINVAL;
1035 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1036 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1037 return -EINVAL;
1038
1039 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1040 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1041 fh->fmt->name);
1042 if (buf->vb.width != fh->width ||
1043 buf->vb.height != fh->height ||
1044 buf->vb.size != size ||
1045 buf->vb.field != field ||
1046 buf->fmt != fh->fmt) {
c7b0ac05 1047 saa7134_dma_free(q,buf);
1da177e4
LT
1048 }
1049
0fc0686e 1050 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
c1accaa2
MCC
1051 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1052
1da177e4
LT
1053 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 buf->pt = &fh->pt_cap;
1059
c7b0ac05 1060 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
1da177e4
LT
1061 if (err)
1062 goto oops;
1063 err = saa7134_pgtable_build(dev->pci,buf->pt,
c1accaa2
MCC
1064 dma->sglist,
1065 dma->sglen,
1da177e4
LT
1066 saa7134_buffer_startpage(buf));
1067 if (err)
1068 goto oops;
1069 }
0fc0686e 1070 buf->vb.state = VIDEOBUF_PREPARED;
1da177e4
LT
1071 buf->activate = buffer_activate;
1072 return 0;
1073
1074 oops:
c7b0ac05 1075 saa7134_dma_free(q,buf);
1da177e4
LT
1076 return err;
1077}
1078
1079static int
1080buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1081{
1082 struct saa7134_fh *fh = q->priv_data;
1083
1084 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1085 if (0 == *count)
1086 *count = gbuffers;
1087 *count = saa7134_buffer_count(*size,*count);
1088 return 0;
1089}
1090
1091static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1092{
1093 struct saa7134_fh *fh = q->priv_data;
1094 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1095
1096 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1097}
1098
1099static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1100{
1da177e4
LT
1101 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1102
c7b0ac05 1103 saa7134_dma_free(q,buf);
1da177e4
LT
1104}
1105
1106static struct videobuf_queue_ops video_qops = {
1107 .buf_setup = buffer_setup,
1108 .buf_prepare = buffer_prepare,
1109 .buf_queue = buffer_queue,
1110 .buf_release = buffer_release,
1111};
1112
1113/* ------------------------------------------------------------------ */
1114
531d83a3 1115int saa7134_g_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
1da177e4
LT
1116{
1117 const struct v4l2_queryctrl* ctrl;
1118
1119 ctrl = ctrl_by_id(c->id);
1120 if (NULL == ctrl)
1121 return -EINVAL;
1122 switch (c->id) {
1123 case V4L2_CID_BRIGHTNESS:
1124 c->value = dev->ctl_bright;
1125 break;
1126 case V4L2_CID_HUE:
1127 c->value = dev->ctl_hue;
1128 break;
1129 case V4L2_CID_CONTRAST:
1130 c->value = dev->ctl_contrast;
1131 break;
1132 case V4L2_CID_SATURATION:
1133 c->value = dev->ctl_saturation;
1134 break;
1135 case V4L2_CID_AUDIO_MUTE:
1136 c->value = dev->ctl_mute;
1137 break;
1138 case V4L2_CID_AUDIO_VOLUME:
1139 c->value = dev->ctl_volume;
1140 break;
1141 case V4L2_CID_PRIVATE_INVERT:
1142 c->value = dev->ctl_invert;
1143 break;
f246a817 1144 case V4L2_CID_HFLIP:
1da177e4
LT
1145 c->value = dev->ctl_mirror;
1146 break;
1147 case V4L2_CID_PRIVATE_Y_EVEN:
1148 c->value = dev->ctl_y_even;
1149 break;
1150 case V4L2_CID_PRIVATE_Y_ODD:
1151 c->value = dev->ctl_y_odd;
1152 break;
1153 case V4L2_CID_PRIVATE_AUTOMUTE:
1154 c->value = dev->ctl_automute;
1155 break;
1156 default:
1157 return -EINVAL;
1158 }
1159 return 0;
1160}
531d83a3 1161EXPORT_SYMBOL_GPL(saa7134_g_ctrl_internal);
1da177e4 1162
531d83a3
HV
1163static int saa7134_g_ctrl(struct file *file, void *priv, struct v4l2_control *c)
1164{
1165 struct saa7134_fh *fh = priv;
1166
1167 return saa7134_g_ctrl_internal(fh->dev, fh, c);
1168}
1169
1170int saa7134_s_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
1da177e4
LT
1171{
1172 const struct v4l2_queryctrl* ctrl;
1173 unsigned long flags;
1174 int restart_overlay = 0;
531d83a3 1175 int err;
8c85454a 1176
531d83a3
HV
1177 /* When called from the empress code fh == NULL.
1178 That needs to be fixed somehow, but for now this is
1179 good enough. */
1180 if (fh) {
1181 err = v4l2_prio_check(&dev->prio, &fh->prio);
1182 if (0 != err)
1183 return err;
1184 }
1185 err = -EINVAL;
1da177e4 1186
8f61ae2f 1187 mutex_lock(&dev->lock);
8c85454a 1188
1da177e4
LT
1189 ctrl = ctrl_by_id(c->id);
1190 if (NULL == ctrl)
8c85454a
MCC
1191 goto error;
1192
1da177e4
LT
1193 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1194 switch (ctrl->type) {
1195 case V4L2_CTRL_TYPE_BOOLEAN:
1196 case V4L2_CTRL_TYPE_MENU:
1197 case V4L2_CTRL_TYPE_INTEGER:
1198 if (c->value < ctrl->minimum)
1199 c->value = ctrl->minimum;
1200 if (c->value > ctrl->maximum)
1201 c->value = ctrl->maximum;
1202 break;
1203 default:
1204 /* nothing */;
1205 };
1206 switch (c->id) {
1207 case V4L2_CID_BRIGHTNESS:
1208 dev->ctl_bright = c->value;
1209 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1210 break;
1211 case V4L2_CID_HUE:
1212 dev->ctl_hue = c->value;
1213 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1214 break;
1215 case V4L2_CID_CONTRAST:
1216 dev->ctl_contrast = c->value;
1217 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1218 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1219 break;
1220 case V4L2_CID_SATURATION:
1221 dev->ctl_saturation = c->value;
1222 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1223 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1224 break;
1225 case V4L2_CID_AUDIO_MUTE:
1226 dev->ctl_mute = c->value;
1227 saa7134_tvaudio_setmute(dev);
1228 break;
1229 case V4L2_CID_AUDIO_VOLUME:
1230 dev->ctl_volume = c->value;
1231 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1232 break;
1233 case V4L2_CID_PRIVATE_INVERT:
1234 dev->ctl_invert = c->value;
1235 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1236 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1237 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1238 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1239 break;
f246a817 1240 case V4L2_CID_HFLIP:
1da177e4
LT
1241 dev->ctl_mirror = c->value;
1242 restart_overlay = 1;
1243 break;
1244 case V4L2_CID_PRIVATE_Y_EVEN:
1245 dev->ctl_y_even = c->value;
1246 restart_overlay = 1;
1247 break;
1248 case V4L2_CID_PRIVATE_Y_ODD:
1249 dev->ctl_y_odd = c->value;
1250 restart_overlay = 1;
1251 break;
1252 case V4L2_CID_PRIVATE_AUTOMUTE:
7f171123
MCC
1253 {
1254 struct v4l2_priv_tun_config tda9887_cfg;
1255
1256 tda9887_cfg.tuner = TUNER_TDA9887;
1257 tda9887_cfg.priv = &dev->tda9887_conf;
1258
1da177e4
LT
1259 dev->ctl_automute = c->value;
1260 if (dev->tda9887_conf) {
1261 if (dev->ctl_automute)
1262 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1263 else
1264 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
7f171123
MCC
1265
1266 saa7134_i2c_call_clients(dev, TUNER_SET_CONFIG,
1267 &tda9887_cfg);
1da177e4
LT
1268 }
1269 break;
7f171123 1270 }
1da177e4 1271 default:
8c85454a 1272 goto error;
1da177e4
LT
1273 }
1274 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1275 spin_lock_irqsave(&dev->slock,flags);
1276 stop_preview(dev,fh);
1277 start_preview(dev,fh);
1278 spin_unlock_irqrestore(&dev->slock,flags);
1279 }
8c85454a
MCC
1280 err = 0;
1281
1282error:
8f61ae2f 1283 mutex_unlock(&dev->lock);
8c85454a 1284 return err;
1da177e4 1285}
531d83a3
HV
1286EXPORT_SYMBOL_GPL(saa7134_s_ctrl_internal);
1287
1288static int saa7134_s_ctrl(struct file *file, void *f, struct v4l2_control *c)
1289{
1290 struct saa7134_fh *fh = f;
1291
1292 return saa7134_s_ctrl_internal(fh->dev, fh, c);
1293}
1da177e4
LT
1294
1295/* ------------------------------------------------------------------ */
1296
1297static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1298{
1299 struct videobuf_queue* q = NULL;
1300
1301 switch (fh->type) {
1302 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1303 q = &fh->cap;
1304 break;
1305 case V4L2_BUF_TYPE_VBI_CAPTURE:
1306 q = &fh->vbi;
1307 break;
1308 default:
1309 BUG();
1310 }
1311 return q;
1312}
1313
1314static int saa7134_resource(struct saa7134_fh *fh)
1315{
9040b32e
HO
1316 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1317 return RESOURCE_VIDEO;
1da177e4 1318
9040b32e
HO
1319 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1320 return RESOURCE_VBI;
1321
1322 BUG();
1323 return 0;
1da177e4
LT
1324}
1325
1326static int video_open(struct inode *inode, struct file *file)
1327{
1328 int minor = iminor(inode);
6d28e989 1329 struct saa7134_dev *dev;
1da177e4 1330 struct saa7134_fh *fh;
1da177e4
LT
1331 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1332 int radio = 0;
d56dc612
HV
1333
1334 lock_kernel();
6d28e989
TP
1335 list_for_each_entry(dev, &saa7134_devlist, devlist) {
1336 if (dev->video_dev && (dev->video_dev->minor == minor))
1337 goto found;
1338 if (dev->radio_dev && (dev->radio_dev->minor == minor)) {
1da177e4 1339 radio = 1;
6d28e989 1340 goto found;
1da177e4 1341 }
6d28e989 1342 if (dev->vbi_dev && (dev->vbi_dev->minor == minor)) {
1da177e4 1343 type = V4L2_BUF_TYPE_VBI_CAPTURE;
6d28e989 1344 goto found;
1da177e4
LT
1345 }
1346 }
d56dc612 1347 unlock_kernel();
6d28e989
TP
1348 return -ENODEV;
1349 found:
1da177e4
LT
1350
1351 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1352 v4l2_type_names[type]);
1353
1354 /* allocate + initialize per filehandle data */
7408187d 1355 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
d56dc612
HV
1356 if (NULL == fh) {
1357 unlock_kernel();
1da177e4 1358 return -ENOMEM;
d56dc612 1359 }
1da177e4
LT
1360 file->private_data = fh;
1361 fh->dev = dev;
1362 fh->radio = radio;
1363 fh->type = type;
1364 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1365 fh->width = 720;
1366 fh->height = 576;
1367 v4l2_prio_open(&dev->prio,&fh->prio);
1368
0705135e
GL
1369 videobuf_queue_sg_init(&fh->cap, &video_qops,
1370 &dev->pci->dev, &dev->slock,
1da177e4
LT
1371 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1372 V4L2_FIELD_INTERLACED,
1373 sizeof(struct saa7134_buf),
1374 fh);
0705135e
GL
1375 videobuf_queue_sg_init(&fh->vbi, &saa7134_vbi_qops,
1376 &dev->pci->dev, &dev->slock,
1da177e4
LT
1377 V4L2_BUF_TYPE_VBI_CAPTURE,
1378 V4L2_FIELD_SEQ_TB,
1379 sizeof(struct saa7134_buf),
1380 fh);
1381 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1382 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1383
1384 if (fh->radio) {
1385 /* switch to radio mode */
1386 saa7134_tvaudio_setinput(dev,&card(dev).radio);
330a115a 1387 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO, NULL);
1da177e4
LT
1388 } else {
1389 /* switch to video/vbi mode */
1390 video_mux(dev,dev->ctl_input);
1391 }
d56dc612 1392 unlock_kernel();
330a115a 1393 return 0;
1da177e4
LT
1394}
1395
1396static ssize_t
1397video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1398{
1399 struct saa7134_fh *fh = file->private_data;
1400
1401 switch (fh->type) {
1402 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1403 if (res_locked(fh->dev,RESOURCE_VIDEO))
1404 return -EBUSY;
1405 return videobuf_read_one(saa7134_queue(fh),
1406 data, count, ppos,
1407 file->f_flags & O_NONBLOCK);
1408 case V4L2_BUF_TYPE_VBI_CAPTURE:
1409 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1410 return -EBUSY;
1411 return videobuf_read_stream(saa7134_queue(fh),
1412 data, count, ppos, 1,
1413 file->f_flags & O_NONBLOCK);
1414 break;
1415 default:
1416 BUG();
1417 return 0;
1418 }
1419}
1420
1421static unsigned int
1422video_poll(struct file *file, struct poll_table_struct *wait)
1423{
1424 struct saa7134_fh *fh = file->private_data;
1425 struct videobuf_buffer *buf = NULL;
1426
1427 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1428 return videobuf_poll_stream(file, &fh->vbi, wait);
1429
1430 if (res_check(fh,RESOURCE_VIDEO)) {
1431 if (!list_empty(&fh->cap.stream))
1432 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1433 } else {
64f9477f 1434 mutex_lock(&fh->cap.vb_lock);
1da177e4 1435 if (UNSET == fh->cap.read_off) {
330a115a 1436 /* need to capture a new frame */
64f9477f
MCC
1437 if (res_locked(fh->dev,RESOURCE_VIDEO))
1438 goto err;
1439 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field))
1440 goto err;
4ac97914
MCC
1441 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1442 fh->cap.read_off = 0;
1da177e4 1443 }
64f9477f 1444 mutex_unlock(&fh->cap.vb_lock);
1da177e4
LT
1445 buf = fh->cap.read_buf;
1446 }
1447
1448 if (!buf)
1449 return POLLERR;
1450
1451 poll_wait(file, &buf->done, wait);
0fc0686e
BP
1452 if (buf->state == VIDEOBUF_DONE ||
1453 buf->state == VIDEOBUF_ERROR)
1da177e4
LT
1454 return POLLIN|POLLRDNORM;
1455 return 0;
64f9477f
MCC
1456
1457err:
1458 mutex_unlock(&fh->cap.vb_lock);
1459 return POLLERR;
1da177e4
LT
1460}
1461
1462static int video_release(struct inode *inode, struct file *file)
1463{
1464 struct saa7134_fh *fh = file->private_data;
1465 struct saa7134_dev *dev = fh->dev;
1466 unsigned long flags;
1467
1468 /* turn off overlay */
1469 if (res_check(fh, RESOURCE_OVERLAY)) {
1470 spin_lock_irqsave(&dev->slock,flags);
1471 stop_preview(dev,fh);
1472 spin_unlock_irqrestore(&dev->slock,flags);
1473 res_free(dev,fh,RESOURCE_OVERLAY);
1474 }
1475
1476 /* stop video capture */
1477 if (res_check(fh, RESOURCE_VIDEO)) {
1478 videobuf_streamoff(&fh->cap);
1479 res_free(dev,fh,RESOURCE_VIDEO);
1480 }
1481 if (fh->cap.read_buf) {
1482 buffer_release(&fh->cap,fh->cap.read_buf);
1483 kfree(fh->cap.read_buf);
1484 }
1485
1486 /* stop vbi capture */
1487 if (res_check(fh, RESOURCE_VBI)) {
053fcb60 1488 videobuf_stop(&fh->vbi);
1da177e4
LT
1489 res_free(dev,fh,RESOURCE_VBI);
1490 }
1491
330a115a
MCC
1492 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1493 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1494 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1495 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1496 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1497
2f180710 1498 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
330a115a 1499
1da177e4
LT
1500 /* free stuff */
1501 videobuf_mmap_free(&fh->cap);
1502 videobuf_mmap_free(&fh->vbi);
1503 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1504 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1505
1506 v4l2_prio_close(&dev->prio,&fh->prio);
1507 file->private_data = NULL;
1508 kfree(fh);
1509 return 0;
1510}
1511
9040b32e 1512static int video_mmap(struct file *file, struct vm_area_struct * vma)
1da177e4
LT
1513{
1514 struct saa7134_fh *fh = file->private_data;
1515
1516 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1517}
1518
1519/* ------------------------------------------------------------------ */
1520
78b526a4 1521static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
8c85454a 1522 struct v4l2_format *f)
1da177e4 1523{
8c85454a
MCC
1524 struct saa7134_fh *fh = priv;
1525 struct saa7134_dev *dev = fh->dev;
1da177e4
LT
1526 struct saa7134_tvnorm *norm = dev->tvnorm;
1527
1528 f->fmt.vbi.sampling_rate = 6750000 * 4;
1529 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1530 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1531 f->fmt.vbi.offset = 64 * 4;
f246a817
MS
1532 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1533 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1534 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1da177e4
LT
1535 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1536 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1537
8c85454a 1538 return 0;
1da177e4
LT
1539}
1540
78b526a4 1541static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
8f61ae2f 1542 struct v4l2_format *f)
1da177e4 1543{
8f61ae2f 1544 struct saa7134_fh *fh = priv;
8f61ae2f 1545
8c85454a
MCC
1546 f->fmt.pix.width = fh->width;
1547 f->fmt.pix.height = fh->height;
1548 f->fmt.pix.field = fh->cap.field;
1549 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1550 f->fmt.pix.bytesperline =
1551 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1552 f->fmt.pix.sizeimage =
1553 f->fmt.pix.height * f->fmt.pix.bytesperline;
1554 return 0;
1555}
1556
78b526a4 1557static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
8c85454a
MCC
1558 struct v4l2_format *f)
1559{
1560 struct saa7134_fh *fh = priv;
1561
1562 if (saa7134_no_overlay > 0) {
1563 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1da177e4
LT
1564 return -EINVAL;
1565 }
8c85454a
MCC
1566 f->fmt.win = fh->win;
1567
1568 return 0;
1da177e4
LT
1569}
1570
78b526a4 1571static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
8f61ae2f 1572 struct v4l2_format *f)
1da177e4 1573{
8f61ae2f
DSL
1574 struct saa7134_fh *fh = priv;
1575 struct saa7134_dev *dev = fh->dev;
8c85454a
MCC
1576 struct saa7134_format *fmt;
1577 enum v4l2_field field;
1578 unsigned int maxw, maxh;
1da177e4 1579
8c85454a
MCC
1580 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1581 if (NULL == fmt)
1582 return -EINVAL;
1da177e4 1583
8c85454a
MCC
1584 field = f->fmt.pix.field;
1585 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1586 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1da177e4 1587
8c85454a
MCC
1588 if (V4L2_FIELD_ANY == field) {
1589 field = (f->fmt.pix.height > maxh/2)
1590 ? V4L2_FIELD_INTERLACED
1591 : V4L2_FIELD_BOTTOM;
1592 }
1593 switch (field) {
1594 case V4L2_FIELD_TOP:
1595 case V4L2_FIELD_BOTTOM:
1596 maxh = maxh / 2;
1597 break;
1598 case V4L2_FIELD_INTERLACED:
1599 break;
1600 default:
1601 return -EINVAL;
1602 }
1da177e4 1603
8c85454a
MCC
1604 f->fmt.pix.field = field;
1605 if (f->fmt.pix.width < 48)
1606 f->fmt.pix.width = 48;
1607 if (f->fmt.pix.height < 32)
1608 f->fmt.pix.height = 32;
1609 if (f->fmt.pix.width > maxw)
1610 f->fmt.pix.width = maxw;
1611 if (f->fmt.pix.height > maxh)
1612 f->fmt.pix.height = maxh;
1613 f->fmt.pix.width &= ~0x03;
1614 f->fmt.pix.bytesperline =
1615 (f->fmt.pix.width * fmt->depth) >> 3;
1616 f->fmt.pix.sizeimage =
1617 f->fmt.pix.height * f->fmt.pix.bytesperline;
1da177e4 1618
8c85454a
MCC
1619 return 0;
1620}
1da177e4 1621
78b526a4 1622static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
8c85454a
MCC
1623 struct v4l2_format *f)
1624{
1625 struct saa7134_fh *fh = priv;
1626 struct saa7134_dev *dev = fh->dev;
1627
1628 if (saa7134_no_overlay > 0) {
1629 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1da177e4
LT
1630 return -EINVAL;
1631 }
8c85454a
MCC
1632
1633 return verify_preview(dev, &f->fmt.win);
1da177e4
LT
1634}
1635
78b526a4 1636static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
8f61ae2f 1637 struct v4l2_format *f)
8c85454a
MCC
1638{
1639 struct saa7134_fh *fh = priv;
1640 int err;
1641
78b526a4 1642 err = saa7134_try_fmt_vid_cap(file, priv, f);
8c85454a
MCC
1643 if (0 != err)
1644 return err;
1645
1646 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1647 fh->width = f->fmt.pix.width;
1648 fh->height = f->fmt.pix.height;
1649 fh->cap.field = f->fmt.pix.field;
1650 return 0;
1651}
1652
78b526a4 1653static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
8c85454a 1654 struct v4l2_format *f)
1da177e4 1655{
8f61ae2f
DSL
1656 struct saa7134_fh *fh = priv;
1657 struct saa7134_dev *dev = fh->dev;
1da177e4 1658 int err;
a8b1ecf3 1659 unsigned long flags;
1da177e4 1660
8c85454a
MCC
1661 if (saa7134_no_overlay > 0) {
1662 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1663 return -EINVAL;
1664 }
1665 err = verify_preview(dev, &f->fmt.win);
1666 if (0 != err)
1667 return err;
1da177e4 1668
8c85454a
MCC
1669 mutex_lock(&dev->lock);
1670
1671 fh->win = f->fmt.win;
1672 fh->nclips = f->fmt.win.clipcount;
1673
1674 if (fh->nclips > 8)
1675 fh->nclips = 8;
1676
1677 if (copy_from_user(fh->clips, f->fmt.win.clips,
1678 sizeof(struct v4l2_clip)*fh->nclips)) {
3593cab5 1679 mutex_unlock(&dev->lock);
8c85454a 1680 return -EFAULT;
1da177e4 1681 }
8c85454a
MCC
1682
1683 if (res_check(fh, RESOURCE_OVERLAY)) {
1684 spin_lock_irqsave(&dev->slock, flags);
1685 stop_preview(dev, fh);
1686 start_preview(dev, fh);
1687 spin_unlock_irqrestore(&dev->slock, flags);
1688 }
1689
1690 mutex_unlock(&dev->lock);
1691 return 0;
1da177e4
LT
1692}
1693
2c10e8a8 1694int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c)
8f61ae2f
DSL
1695{
1696 const struct v4l2_queryctrl *ctrl;
1da177e4 1697
8f61ae2f
DSL
1698 if ((c->id < V4L2_CID_BASE ||
1699 c->id >= V4L2_CID_LASTP1) &&
1700 (c->id < V4L2_CID_PRIVATE_BASE ||
1701 c->id >= V4L2_CID_PRIVATE_LASTP1))
1702 return -EINVAL;
1703 ctrl = ctrl_by_id(c->id);
1704 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1da177e4
LT
1705 return 0;
1706}
f6e90a66 1707EXPORT_SYMBOL_GPL(saa7134_queryctrl);
1da177e4 1708
bfb12e38 1709static int saa7134_enum_input(struct file *file, void *priv,
8f61ae2f 1710 struct v4l2_input *i)
1da177e4 1711{
8f61ae2f 1712 struct saa7134_fh *fh = priv;
1da177e4 1713 struct saa7134_dev *dev = fh->dev;
8f61ae2f 1714 unsigned int n;
1da177e4 1715
8f61ae2f
DSL
1716 n = i->index;
1717 if (n >= SAA7134_INPUT_MAX)
1718 return -EINVAL;
1719 if (NULL == card_in(dev, i->index).name)
1720 return -EINVAL;
1721 memset(i, 0, sizeof(*i));
1722 i->index = n;
1723 i->type = V4L2_INPUT_TYPE_CAMERA;
1724 strcpy(i->name, card_in(dev, n).name);
1725 if (card_in(dev, n).tv)
1726 i->type = V4L2_INPUT_TYPE_TUNER;
1727 i->audioset = 1;
1728 if (n == dev->ctl_input) {
1729 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1730 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1731
1732 if (0 != (v1 & 0x40))
1733 i->status |= V4L2_IN_ST_NO_H_LOCK;
1734 if (0 != (v2 & 0x40))
1735 i->status |= V4L2_IN_ST_NO_SYNC;
1736 if (0 != (v2 & 0x0e))
1737 i->status |= V4L2_IN_ST_MACROVISION;
1738 }
8c85454a 1739 i->std = SAA7134_NORMS;
8f61ae2f
DSL
1740 return 0;
1741}
1da177e4 1742
bfb12e38 1743static int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
90bdc145
MCC
1744{
1745 struct saa7134_fh *fh = priv;
1746 struct saa7134_dev *dev = fh->dev;
1747
1748 *i = dev->ctl_input;
1749 return 0;
1750}
1751
bfb12e38 1752static int saa7134_s_input(struct file *file, void *priv, unsigned int i)
90bdc145
MCC
1753{
1754 struct saa7134_fh *fh = priv;
1755 struct saa7134_dev *dev = fh->dev;
8c85454a
MCC
1756 int err;
1757
1758 err = v4l2_prio_check(&dev->prio, &fh->prio);
1759 if (0 != err)
1760 return err;
90bdc145
MCC
1761
1762 if (i < 0 || i >= SAA7134_INPUT_MAX)
1763 return -EINVAL;
1764 if (NULL == card_in(dev, i).name)
1765 return -EINVAL;
1766 mutex_lock(&dev->lock);
1767 video_mux(dev, i);
1768 mutex_unlock(&dev->lock);
1769 return 0;
1770}
1771
bfb12e38 1772static int saa7134_querycap(struct file *file, void *priv,
8f61ae2f
DSL
1773 struct v4l2_capability *cap)
1774{
1775 struct saa7134_fh *fh = priv;
1776 struct saa7134_dev *dev = fh->dev;
1da177e4 1777
8f61ae2f
DSL
1778 unsigned int tuner_type = dev->tuner_type;
1779
8f61ae2f
DSL
1780 strcpy(cap->driver, "saa7134");
1781 strlcpy(cap->card, saa7134_boards[dev->board].name,
1782 sizeof(cap->card));
1783 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1784 cap->version = SAA7134_VERSION_CODE;
1785 cap->capabilities =
1786 V4L2_CAP_VIDEO_CAPTURE |
1787 V4L2_CAP_VBI_CAPTURE |
1788 V4L2_CAP_READWRITE |
1789 V4L2_CAP_STREAMING |
1790 V4L2_CAP_TUNER;
1791 if (saa7134_no_overlay <= 0)
1792 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1793
1794 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1795 cap->capabilities &= ~V4L2_CAP_TUNER;
1796 return 0;
1797}
bb881f14 1798
bfb12e38 1799static int saa7134_s_std(struct file *file, void *priv, v4l2_std_id *id)
8f61ae2f
DSL
1800{
1801 struct saa7134_fh *fh = priv;
1802 struct saa7134_dev *dev = fh->dev;
1803 unsigned long flags;
1804 unsigned int i;
1805 v4l2_std_id fixup;
8c85454a
MCC
1806 int err;
1807
1808 err = v4l2_prio_check(&dev->prio, &fh->prio);
1809 if (0 != err)
1810 return err;
bb881f14 1811
8f61ae2f
DSL
1812 for (i = 0; i < TVNORMS; i++)
1813 if (*id == tvnorms[i].id)
1814 break;
8c85454a 1815
8f61ae2f
DSL
1816 if (i == TVNORMS)
1817 for (i = 0; i < TVNORMS; i++)
1818 if (*id & tvnorms[i].id)
1819 break;
1820 if (i == TVNORMS)
1821 return -EINVAL;
8c85454a 1822
8f61ae2f
DSL
1823 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1824 if (secam[0] == 'L' || secam[0] == 'l') {
1825 if (secam[1] == 'C' || secam[1] == 'c')
1826 fixup = V4L2_STD_SECAM_LC;
1827 else
1828 fixup = V4L2_STD_SECAM_L;
1829 } else {
1830 if (secam[0] == 'D' || secam[0] == 'd')
1831 fixup = V4L2_STD_SECAM_DK;
1832 else
1833 fixup = V4L2_STD_SECAM;
1834 }
1835 for (i = 0; i < TVNORMS; i++)
1836 if (fixup == tvnorms[i].id)
1837 break;
1da177e4 1838 }
8c85454a
MCC
1839
1840 *id = tvnorms[i].id;
1841
8f61ae2f
DSL
1842 mutex_lock(&dev->lock);
1843 if (res_check(fh, RESOURCE_OVERLAY)) {
1844 spin_lock_irqsave(&dev->slock, flags);
1845 stop_preview(dev, fh);
1846 spin_unlock_irqrestore(&dev->slock, flags);
1847
1848 set_tvnorm(dev, &tvnorms[i]);
1849
1850 spin_lock_irqsave(&dev->slock, flags);
1851 start_preview(dev, fh);
1852 spin_unlock_irqrestore(&dev->slock, flags);
1853 } else
1854 set_tvnorm(dev, &tvnorms[i]);
8c85454a 1855
8f61ae2f
DSL
1856 saa7134_tvaudio_do_scan(dev);
1857 mutex_unlock(&dev->lock);
1858 return 0;
1859}
1da177e4 1860
bfb12e38 1861static int saa7134_cropcap(struct file *file, void *priv,
8f61ae2f
DSL
1862 struct v4l2_cropcap *cap)
1863{
1864 struct saa7134_fh *fh = priv;
1865 struct saa7134_dev *dev = fh->dev;
1da177e4 1866
8f61ae2f
DSL
1867 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1868 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1869 return -EINVAL;
1870 cap->bounds = dev->crop_bounds;
1871 cap->defrect = dev->crop_defrect;
1872 cap->pixelaspect.numerator = 1;
1873 cap->pixelaspect.denominator = 1;
1874 if (dev->tvnorm->id & V4L2_STD_525_60) {
1875 cap->pixelaspect.numerator = 11;
1876 cap->pixelaspect.denominator = 10;
1877 }
1878 if (dev->tvnorm->id & V4L2_STD_625_50) {
1879 cap->pixelaspect.numerator = 54;
1880 cap->pixelaspect.denominator = 59;
1da177e4 1881 }
8f61ae2f
DSL
1882 return 0;
1883}
1da177e4 1884
bfb12e38 1885static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
90bdc145
MCC
1886{
1887 struct saa7134_fh *fh = f;
1888 struct saa7134_dev *dev = fh->dev;
1889
1890 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1891 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1892 return -EINVAL;
1893 crop->c = dev->crop_current;
1894 return 0;
1895}
1896
bfb12e38 1897static int saa7134_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
90bdc145
MCC
1898{
1899 struct saa7134_fh *fh = f;
1900 struct saa7134_dev *dev = fh->dev;
1901 struct v4l2_rect *b = &dev->crop_bounds;
1902
1903 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1904 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1905 return -EINVAL;
1906 if (crop->c.height < 0)
1907 return -EINVAL;
1908 if (crop->c.width < 0)
1909 return -EINVAL;
1910
1911 if (res_locked(fh->dev, RESOURCE_OVERLAY))
1912 return -EBUSY;
1913 if (res_locked(fh->dev, RESOURCE_VIDEO))
1914 return -EBUSY;
1915
1916 if (crop->c.top < b->top)
1917 crop->c.top = b->top;
1918 if (crop->c.top > b->top + b->height)
1919 crop->c.top = b->top + b->height;
1920 if (crop->c.height > b->top - crop->c.top + b->height)
1921 crop->c.height = b->top - crop->c.top + b->height;
1922
1923 if (crop->c.left < b->left)
1924 crop->c.left = b->left;
1925 if (crop->c.left > b->left + b->width)
1926 crop->c.left = b->left + b->width;
1927 if (crop->c.width > b->left - crop->c.left + b->width)
1928 crop->c.width = b->left - crop->c.left + b->width;
1929
1930 dev->crop_current = crop->c;
1931 return 0;
1932}
1933
bfb12e38 1934static int saa7134_g_tuner(struct file *file, void *priv,
8f61ae2f
DSL
1935 struct v4l2_tuner *t)
1936{
1937 struct saa7134_fh *fh = priv;
1938 struct saa7134_dev *dev = fh->dev;
1939 int n;
1da177e4 1940
8f61ae2f
DSL
1941 if (0 != t->index)
1942 return -EINVAL;
1943 memset(t, 0, sizeof(*t));
1944 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1945 if (card_in(dev, n).tv)
1946 break;
1947 if (NULL != card_in(dev, n).name) {
1948 strcpy(t->name, "Television");
1949 t->type = V4L2_TUNER_ANALOG_TV;
1950 t->capability = V4L2_TUNER_CAP_NORM |
1951 V4L2_TUNER_CAP_STEREO |
1952 V4L2_TUNER_CAP_LANG1 |
1953 V4L2_TUNER_CAP_LANG2;
1954 t->rangehigh = 0xffffffffUL;
1955 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1956 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1957 }
1958 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1959 t->signal = 0xffff;
1960 return 0;
1961}
b4aeb8b8 1962
bfb12e38 1963static int saa7134_s_tuner(struct file *file, void *priv,
8f61ae2f
DSL
1964 struct v4l2_tuner *t)
1965{
1966 struct saa7134_fh *fh = priv;
1967 struct saa7134_dev *dev = fh->dev;
8c85454a
MCC
1968 int rx, mode, err;
1969
1970 err = v4l2_prio_check(&dev->prio, &fh->prio);
1971 if (0 != err)
1972 return err;
b4aeb8b8 1973
8f61ae2f
DSL
1974 mode = dev->thread.mode;
1975 if (UNSET == mode) {
1976 rx = saa7134_tvaudio_getstereo(dev);
1977 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1da177e4 1978 }
8f61ae2f
DSL
1979 if (mode != t->audmode)
1980 dev->thread.mode = t->audmode;
8c85454a 1981
8f61ae2f
DSL
1982 return 0;
1983}
1da177e4 1984
bfb12e38 1985static int saa7134_g_frequency(struct file *file, void *priv,
8f61ae2f
DSL
1986 struct v4l2_frequency *f)
1987{
1988 struct saa7134_fh *fh = priv;
1989 struct saa7134_dev *dev = fh->dev;
1da177e4 1990
8f61ae2f
DSL
1991 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1992 f->frequency = dev->ctl_freq;
8c85454a 1993
8f61ae2f
DSL
1994 return 0;
1995}
1da177e4 1996
bfb12e38 1997static int saa7134_s_frequency(struct file *file, void *priv,
8f61ae2f
DSL
1998 struct v4l2_frequency *f)
1999{
2000 struct saa7134_fh *fh = priv;
2001 struct saa7134_dev *dev = fh->dev;
8c85454a
MCC
2002 int err;
2003
2004 err = v4l2_prio_check(&dev->prio, &fh->prio);
2005 if (0 != err)
2006 return err;
1da177e4 2007
8f61ae2f
DSL
2008 if (0 != f->tuner)
2009 return -EINVAL;
2010 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
2011 return -EINVAL;
2012 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
2013 return -EINVAL;
2014 mutex_lock(&dev->lock);
2015 dev->ctl_freq = f->frequency;
1da177e4 2016
8f61ae2f 2017 saa7134_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1da177e4 2018
8f61ae2f
DSL
2019 saa7134_tvaudio_do_scan(dev);
2020 mutex_unlock(&dev->lock);
2021 return 0;
2022}
1da177e4 2023
bfb12e38 2024static int saa7134_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
8f61ae2f 2025{
8f61ae2f
DSL
2026 strcpy(a->name, "audio");
2027 return 0;
2028}
1da177e4 2029
bfb12e38 2030static int saa7134_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
8f61ae2f
DSL
2031{
2032 return 0;
2033}
1da177e4 2034
bfb12e38 2035static int saa7134_g_priority(struct file *file, void *f, enum v4l2_priority *p)
8f61ae2f
DSL
2036{
2037 struct saa7134_fh *fh = f;
2038 struct saa7134_dev *dev = fh->dev;
8f61ae2f 2039
90bdc145 2040 *p = v4l2_prio_max(&dev->prio);
8f61ae2f
DSL
2041 return 0;
2042}
4ac97914 2043
bfb12e38 2044static int saa7134_s_priority(struct file *file, void *f,
90bdc145 2045 enum v4l2_priority prio)
8f61ae2f
DSL
2046{
2047 struct saa7134_fh *fh = f;
2048 struct saa7134_dev *dev = fh->dev;
4ac97914 2049
90bdc145 2050 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
8f61ae2f 2051}
1da177e4 2052
78b526a4 2053static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
90bdc145 2054 struct v4l2_fmtdesc *f)
8f61ae2f 2055{
8c85454a
MCC
2056 if (f->index >= FORMATS)
2057 return -EINVAL;
1da177e4 2058
8c85454a
MCC
2059 strlcpy(f->description, formats[f->index].name,
2060 sizeof(f->description));
1da177e4 2061
8c85454a 2062 f->pixelformat = formats[f->index].fourcc;
1da177e4 2063
8c85454a
MCC
2064 return 0;
2065}
2066
78b526a4 2067static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
8c85454a
MCC
2068 struct v4l2_fmtdesc *f)
2069{
2070 if (saa7134_no_overlay > 0) {
2071 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
90bdc145
MCC
2072 return -EINVAL;
2073 }
8c85454a
MCC
2074
2075 if ((f->index >= FORMATS) || formats[f->index].planar)
2076 return -EINVAL;
2077
2078 strlcpy(f->description, formats[f->index].name,
2079 sizeof(f->description));
2080
2081 f->pixelformat = formats[f->index].fourcc;
2082
2083 return 0;
2084}
2085
78b526a4 2086static int saa7134_enum_fmt_vbi_cap(struct file *file, void *priv,
8c85454a
MCC
2087 struct v4l2_fmtdesc *f)
2088{
2089 if (0 != f->index)
2090 return -EINVAL;
2091
2092 f->pixelformat = V4L2_PIX_FMT_GREY;
2093 strcpy(f->description, "vbi data");
2094
90bdc145 2095 return 0;
8f61ae2f 2096}
1da177e4 2097
bfb12e38 2098static int saa7134_g_fbuf(struct file *file, void *f,
8f61ae2f
DSL
2099 struct v4l2_framebuffer *fb)
2100{
2101 struct saa7134_fh *fh = f;
2102 struct saa7134_dev *dev = fh->dev;
1da177e4 2103
8f61ae2f
DSL
2104 *fb = dev->ovbuf;
2105 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
ecb73774 2106
8f61ae2f
DSL
2107 return 0;
2108}
1da177e4 2109
bfb12e38 2110static int saa7134_s_fbuf(struct file *file, void *f,
8f61ae2f
DSL
2111 struct v4l2_framebuffer *fb)
2112{
2113 struct saa7134_fh *fh = f;
2114 struct saa7134_dev *dev = fh->dev;
2115 struct saa7134_format *fmt;
2116
2117 if (!capable(CAP_SYS_ADMIN) &&
2118 !capable(CAP_SYS_RAWIO))
2119 return -EPERM;
1da177e4 2120
8f61ae2f
DSL
2121 /* check args */
2122 fmt = format_by_fourcc(fb->fmt.pixelformat);
2123 if (NULL == fmt)
2124 return -EINVAL;
1da177e4 2125
8f61ae2f
DSL
2126 /* ok, accept it */
2127 dev->ovbuf = *fb;
2128 dev->ovfmt = fmt;
2129 if (0 == dev->ovbuf.fmt.bytesperline)
2130 dev->ovbuf.fmt.bytesperline =
2131 dev->ovbuf.fmt.width*fmt->depth/8;
2132 return 0;
2133}
1da177e4 2134
bfb12e38 2135static int saa7134_overlay(struct file *file, void *f, unsigned int on)
8f61ae2f
DSL
2136{
2137 struct saa7134_fh *fh = f;
2138 struct saa7134_dev *dev = fh->dev;
2139 unsigned long flags;
2140
2141 if (on) {
2142 if (saa7134_no_overlay > 0) {
2143 dprintk("no_overlay\n");
2144 return -EINVAL;
2145 }
1da177e4 2146
8f61ae2f 2147 if (!res_get(dev, fh, RESOURCE_OVERLAY))
1da177e4 2148 return -EBUSY;
8f61ae2f
DSL
2149 spin_lock_irqsave(&dev->slock, flags);
2150 start_preview(dev, fh);
2151 spin_unlock_irqrestore(&dev->slock, flags);
1da177e4 2152 }
8f61ae2f
DSL
2153 if (!on) {
2154 if (!res_check(fh, RESOURCE_OVERLAY))
2155 return -EINVAL;
2156 spin_lock_irqsave(&dev->slock, flags);
2157 stop_preview(dev, fh);
2158 spin_unlock_irqrestore(&dev->slock, flags);
2159 res_free(dev, fh, RESOURCE_OVERLAY);
1da177e4 2160 }
8f61ae2f
DSL
2161 return 0;
2162}
2163
90bdc145
MCC
2164#ifdef CONFIG_VIDEO_V4L1_COMPAT
2165static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
8f61ae2f 2166{
90bdc145
MCC
2167 struct saa7134_fh *fh = file->private_data;
2168 return videobuf_cgmbuf(saa7134_queue(fh), mbuf, 8);
2169}
2170#endif
1da177e4 2171
bfb12e38 2172static int saa7134_reqbufs(struct file *file, void *priv,
90bdc145
MCC
2173 struct v4l2_requestbuffers *p)
2174{
2175 struct saa7134_fh *fh = priv;
2176 return videobuf_reqbufs(saa7134_queue(fh), p);
2177}
8f61ae2f 2178
bfb12e38 2179static int saa7134_querybuf(struct file *file, void *priv,
90bdc145
MCC
2180 struct v4l2_buffer *b)
2181{
2182 struct saa7134_fh *fh = priv;
2183 return videobuf_querybuf(saa7134_queue(fh), b);
2184}
8f61ae2f 2185
bfb12e38 2186static int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
90bdc145
MCC
2187{
2188 struct saa7134_fh *fh = priv;
2189 return videobuf_qbuf(saa7134_queue(fh), b);
2190}
2191
bfb12e38 2192static int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
90bdc145
MCC
2193{
2194 struct saa7134_fh *fh = priv;
2195 return videobuf_dqbuf(saa7134_queue(fh), b,
2196 file->f_flags & O_NONBLOCK);
2197}
2198
bfb12e38 2199static int saa7134_streamon(struct file *file, void *priv,
90bdc145
MCC
2200 enum v4l2_buf_type type)
2201{
2202 struct saa7134_fh *fh = priv;
2203 struct saa7134_dev *dev = fh->dev;
2204 int res = saa7134_resource(fh);
2205
2206 if (!res_get(dev, fh, res))
2207 return -EBUSY;
2208
2209 return videobuf_streamon(saa7134_queue(fh));
2210}
2211
bfb12e38 2212static int saa7134_streamoff(struct file *file, void *priv,
90bdc145
MCC
2213 enum v4l2_buf_type type)
2214{
2215 int err;
2216 struct saa7134_fh *fh = priv;
2217 struct saa7134_dev *dev = fh->dev;
2218 int res = saa7134_resource(fh);
2219
2220 err = videobuf_streamoff(saa7134_queue(fh));
2221 if (err < 0)
2222 return err;
2223 res_free(dev, fh, res);
1da177e4
LT
2224 return 0;
2225}
2226
bfb12e38 2227static int saa7134_g_parm(struct file *file, void *fh,
90bdc145 2228 struct v4l2_streamparm *parm)
1da177e4 2229{
90bdc145 2230 return 0;
1da177e4
LT
2231}
2232
c121ba1f
MCC
2233#ifdef CONFIG_VIDEO_ADV_DEBUG
2234static int vidioc_g_register (struct file *file, void *priv,
2235 struct v4l2_register *reg)
2236{
2237 struct saa7134_fh *fh = priv;
2238 struct saa7134_dev *dev = fh->dev;
2239
2240 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
2241 return -EINVAL;
2242 reg->val = saa_readb(reg->reg);
2243 return 0;
2244}
2245
2246static int vidioc_s_register (struct file *file, void *priv,
2247 struct v4l2_register *reg)
2248{
2249 struct saa7134_fh *fh = priv;
2250 struct saa7134_dev *dev = fh->dev;
2251
2252 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
2253 return -EINVAL;
2254 saa_writeb(reg->reg&0xffffff, reg->val);
2255 return 0;
2256}
2257#endif
2258
8f61ae2f
DSL
2259static int radio_querycap(struct file *file, void *priv,
2260 struct v4l2_capability *cap)
1da177e4
LT
2261{
2262 struct saa7134_fh *fh = file->private_data;
2263 struct saa7134_dev *dev = fh->dev;
2264
8f61ae2f
DSL
2265 strcpy(cap->driver, "saa7134");
2266 strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
2267 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
2268 cap->version = SAA7134_VERSION_CODE;
2269 cap->capabilities = V4L2_CAP_TUNER;
2270 return 0;
2271}
1da177e4 2272
8f61ae2f
DSL
2273static int radio_g_tuner(struct file *file, void *priv,
2274 struct v4l2_tuner *t)
2275{
2276 struct saa7134_fh *fh = file->private_data;
2277 struct saa7134_dev *dev = fh->dev;
1da177e4 2278
8f61ae2f
DSL
2279 if (0 != t->index)
2280 return -EINVAL;
1da177e4 2281
8f61ae2f
DSL
2282 memset(t, 0, sizeof(*t));
2283 strcpy(t->name, "Radio");
2284 t->type = V4L2_TUNER_RADIO;
2285
2286 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
2287 if (dev->input->amux == TV) {
2288 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2289 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2290 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
ac19ecc6 2291 }
8f61ae2f
DSL
2292 return 0;
2293}
2294static int radio_s_tuner(struct file *file, void *priv,
2295 struct v4l2_tuner *t)
2296{
2297 struct saa7134_fh *fh = file->private_data;
2298 struct saa7134_dev *dev = fh->dev;
ac19ecc6 2299
8f61ae2f
DSL
2300 if (0 != t->index)
2301 return -EINVAL;
2302
2303 saa7134_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
2304 return 0;
2305}
ac19ecc6 2306
8f61ae2f
DSL
2307static int radio_enum_input(struct file *file, void *priv,
2308 struct v4l2_input *i)
2309{
2310 if (i->index != 0)
2311 return -EINVAL;
ac19ecc6 2312
8f61ae2f
DSL
2313 strcpy(i->name, "Radio");
2314 i->type = V4L2_INPUT_TYPE_TUNER;
1da177e4 2315
8f61ae2f
DSL
2316 return 0;
2317}
1da177e4 2318
8f61ae2f
DSL
2319static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2320{
2321 *i = 0;
2322 return 0;
2323}
1da177e4 2324
8f61ae2f
DSL
2325static int radio_g_audio(struct file *file, void *priv,
2326 struct v4l2_audio *a)
2327{
2328 memset(a, 0, sizeof(*a));
2329 strcpy(a->name, "Radio");
2330 return 0;
2331}
1da177e4 2332
8f61ae2f
DSL
2333static int radio_s_audio(struct file *file, void *priv,
2334 struct v4l2_audio *a)
2335{
2336 return 0;
2337}
1da177e4 2338
8f61ae2f
DSL
2339static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2340{
2341 return 0;
2342}
1da177e4 2343
8f61ae2f
DSL
2344static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
2345{
1da177e4
LT
2346 return 0;
2347}
2348
8f61ae2f
DSL
2349static int radio_queryctrl(struct file *file, void *priv,
2350 struct v4l2_queryctrl *c)
1da177e4 2351{
8f61ae2f
DSL
2352 const struct v4l2_queryctrl *ctrl;
2353
2354 if (c->id < V4L2_CID_BASE ||
2355 c->id >= V4L2_CID_LASTP1)
2356 return -EINVAL;
2357 if (c->id == V4L2_CID_AUDIO_MUTE) {
2358 ctrl = ctrl_by_id(c->id);
2359 *c = *ctrl;
2360 } else
2361 *c = no_ctrl;
2362 return 0;
1da177e4
LT
2363}
2364
fa027c2a 2365static const struct file_operations video_fops =
1da177e4
LT
2366{
2367 .owner = THIS_MODULE,
2368 .open = video_open,
2369 .release = video_release,
2370 .read = video_read,
2371 .poll = video_poll,
2372 .mmap = video_mmap,
8f61ae2f 2373 .ioctl = video_ioctl2,
0d0fbf81 2374 .compat_ioctl = v4l_compat_ioctl32,
1da177e4
LT
2375 .llseek = no_llseek,
2376};
2377
a399810c 2378static const struct v4l2_ioctl_ops video_ioctl_ops = {
bfb12e38 2379 .vidioc_querycap = saa7134_querycap,
78b526a4
HV
2380 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
2381 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
2382 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
2383 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
2384 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
2385 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
2386 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
2387 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
2388 .vidioc_enum_fmt_vbi_cap = saa7134_enum_fmt_vbi_cap,
2389 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2390 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2391 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
bfb12e38
MCC
2392 .vidioc_g_audio = saa7134_g_audio,
2393 .vidioc_s_audio = saa7134_s_audio,
2394 .vidioc_cropcap = saa7134_cropcap,
2395 .vidioc_reqbufs = saa7134_reqbufs,
2396 .vidioc_querybuf = saa7134_querybuf,
2397 .vidioc_qbuf = saa7134_qbuf,
2398 .vidioc_dqbuf = saa7134_dqbuf,
2399 .vidioc_s_std = saa7134_s_std,
2400 .vidioc_enum_input = saa7134_enum_input,
2401 .vidioc_g_input = saa7134_g_input,
2402 .vidioc_s_input = saa7134_s_input,
2403 .vidioc_queryctrl = saa7134_queryctrl,
2404 .vidioc_g_ctrl = saa7134_g_ctrl,
2405 .vidioc_s_ctrl = saa7134_s_ctrl,
2406 .vidioc_streamon = saa7134_streamon,
2407 .vidioc_streamoff = saa7134_streamoff,
2408 .vidioc_g_tuner = saa7134_g_tuner,
2409 .vidioc_s_tuner = saa7134_s_tuner,
8f61ae2f
DSL
2410#ifdef CONFIG_VIDEO_V4L1_COMPAT
2411 .vidiocgmbuf = vidiocgmbuf,
2412#endif
bfb12e38
MCC
2413 .vidioc_g_crop = saa7134_g_crop,
2414 .vidioc_s_crop = saa7134_s_crop,
2415 .vidioc_g_fbuf = saa7134_g_fbuf,
2416 .vidioc_s_fbuf = saa7134_s_fbuf,
2417 .vidioc_overlay = saa7134_overlay,
2418 .vidioc_g_priority = saa7134_g_priority,
2419 .vidioc_s_priority = saa7134_s_priority,
2420 .vidioc_g_parm = saa7134_g_parm,
2421 .vidioc_g_frequency = saa7134_g_frequency,
2422 .vidioc_s_frequency = saa7134_s_frequency,
c121ba1f
MCC
2423#ifdef CONFIG_VIDEO_ADV_DEBUG
2424 .vidioc_g_register = vidioc_g_register,
2425 .vidioc_s_register = vidioc_s_register,
2426#endif
1da177e4
LT
2427};
2428
a399810c
HV
2429static const struct file_operations radio_fops = {
2430 .owner = THIS_MODULE,
2431 .open = video_open,
2432 .release = video_release,
2433 .ioctl = video_ioctl2,
2434 .compat_ioctl = v4l_compat_ioctl32,
2435 .llseek = no_llseek,
2436};
2437
2438static const struct v4l2_ioctl_ops radio_ioctl_ops = {
8f61ae2f
DSL
2439 .vidioc_querycap = radio_querycap,
2440 .vidioc_g_tuner = radio_g_tuner,
2441 .vidioc_enum_input = radio_enum_input,
2442 .vidioc_g_audio = radio_g_audio,
2443 .vidioc_s_tuner = radio_s_tuner,
2444 .vidioc_s_audio = radio_s_audio,
2445 .vidioc_s_input = radio_s_input,
2446 .vidioc_s_std = radio_s_std,
2447 .vidioc_queryctrl = radio_queryctrl,
2448 .vidioc_g_input = radio_g_input,
bfb12e38
MCC
2449 .vidioc_g_ctrl = saa7134_g_ctrl,
2450 .vidioc_s_ctrl = saa7134_s_ctrl,
2451 .vidioc_g_frequency = saa7134_g_frequency,
2452 .vidioc_s_frequency = saa7134_s_frequency,
1da177e4
LT
2453};
2454
a399810c
HV
2455/* ----------------------------------------------------------- */
2456/* exported stuff */
2457
2458struct video_device saa7134_video_template = {
2459 .name = "saa7134-video",
a399810c
HV
2460 .fops = &video_fops,
2461 .ioctl_ops = &video_ioctl_ops,
2462 .minor = -1,
2463 .tvnorms = SAA7134_NORMS,
2464 .current_norm = V4L2_STD_PAL,
2465};
2466
2467struct video_device saa7134_radio_template = {
2468 .name = "saa7134-radio",
a399810c
HV
2469 .fops = &radio_fops,
2470 .ioctl_ops = &radio_ioctl_ops,
2471 .minor = -1,
2472};
2473
1da177e4
LT
2474int saa7134_video_init1(struct saa7134_dev *dev)
2475{
2476 /* sanitycheck insmod options */
2477 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2478 gbuffers = 2;
2479 if (gbufsize < 0 || gbufsize > gbufsize_max)
2480 gbufsize = gbufsize_max;
2481 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2482
2483 /* put some sensible defaults into the data structures ... */
2484 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2485 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2486 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2487 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2488 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2489 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2490 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2491 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2492
2493 if (dev->tda9887_conf && dev->ctl_automute)
2494 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2495 dev->automute = 0;
2496
4ac97914 2497 INIT_LIST_HEAD(&dev->video_q.queue);
1da177e4
LT
2498 init_timer(&dev->video_q.timeout);
2499 dev->video_q.timeout.function = saa7134_buffer_timeout;
2500 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2501 dev->video_q.dev = dev;
2502
cb71201f
ML
2503 if (saa7134_boards[dev->board].video_out)
2504 saa7134_videoport_init(dev);
2505
2506 return 0;
2507}
2508
2509int saa7134_videoport_init(struct saa7134_dev *dev)
2510{
2511 /* enable video output */
2512 int vo = saa7134_boards[dev->board].video_out;
2513 int video_reg;
2514 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
37df96d3
DB
2515
2516 /* Configure videoport */
cb71201f
ML
2517 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2518 video_reg = video_out[vo][1];
2519 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2520 video_reg &= ~VP_T_CODE_P_INVERTED;
2521 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2522 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
cb71201f
ML
2523 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2524 video_reg = video_out[vo][5];
2525 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2526 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2527 if (vid_port_opts & SET_CLOCK_INVERTED)
2528 video_reg |= VP_CLK_CTRL1_INVERTED;
2529 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2530 video_reg = video_out[vo][6];
2531 if (vid_port_opts & SET_VSYNC_OFF) {
2532 video_reg &= ~VP_VS_TYPE_MASK;
2533 video_reg |= VP_VS_TYPE_OFF;
2534 }
2535 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2536 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2537 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
1da177e4 2538
37df96d3
DB
2539 /* Start videoport */
2540 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2541
1da177e4
LT
2542 return 0;
2543}
2544
2545int saa7134_video_init2(struct saa7134_dev *dev)
2546{
2547 /* init video hw */
2548 set_tvnorm(dev,&tvnorms[0]);
2549 video_mux(dev,0);
2550 saa7134_tvaudio_setmute(dev);
2551 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2552 return 0;
2553}
2554
11f7078c 2555void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
1da177e4
LT
2556{
2557 static const char *st[] = {
2558 "(no signal)", "NTSC", "PAL", "SECAM" };
2559 u32 st1,st2;
2560
2561 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2562 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2563 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2564 (st1 & 0x40) ? "not locked" : "locked",
2565 (st2 & 0x40) ? "no" : "yes",
2566 st[st1 & 0x03]);
11f7078c 2567 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
1da177e4
LT
2568
2569 if (dev->nosignal) {
2570 /* no video signal -> mute audio */
2571 if (dev->ctl_automute)
2572 dev->automute = 1;
2573 saa7134_tvaudio_setmute(dev);
1da177e4
LT
2574 } else {
2575 /* wake up tvaudio audio carrier scan thread */
2576 saa7134_tvaudio_do_scan(dev);
1da177e4 2577 }
11f7078c
ML
2578
2579 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2580 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2581 else
2582 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2583
1da177e4
LT
2584 if (dev->mops && dev->mops->signal_change)
2585 dev->mops->signal_change(dev);
2586}
2587
11f7078c 2588
1da177e4
LT
2589void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2590{
2591 enum v4l2_field field;
2592
2593 spin_lock(&dev->slock);
2594 if (dev->video_q.curr) {
2595 dev->video_fieldcount++;
2596 field = dev->video_q.curr->vb.field;
2597 if (V4L2_FIELD_HAS_BOTH(field)) {
2598 /* make sure we have seen both fields */
2599 if ((status & 0x10) == 0x00) {
2600 dev->video_q.curr->top_seen = 1;
2601 goto done;
2602 }
2603 if (!dev->video_q.curr->top_seen)
2604 goto done;
2605 } else if (field == V4L2_FIELD_TOP) {
2606 if ((status & 0x10) != 0x10)
2607 goto done;
2608 } else if (field == V4L2_FIELD_BOTTOM) {
2609 if ((status & 0x10) != 0x00)
2610 goto done;
2611 }
2612 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
0fc0686e 2613 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
1da177e4
LT
2614 }
2615 saa7134_buffer_next(dev,&dev->video_q);
2616
2617 done:
2618 spin_unlock(&dev->slock);
2619}
2620
2621/* ----------------------------------------------------------- */
2622/*
2623 * Local variables:
2624 * c-basic-offset: 8
2625 * End:
2626 */