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