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