f874b0c9fe4aebc4f2253dc1cb9656c40c32b797
[deliverable/linux.git] / drivers / media / pci / saa7134 / saa7134-video.c
1 /*
2 *
3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
5 *
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "saa7134.h"
24 #include "saa7134-reg.h"
25
26 #include <linux/init.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/sort.h>
32
33 #include <media/v4l2-common.h>
34 #include <media/v4l2-event.h>
35 #include <media/saa6588.h>
36
37 /* ------------------------------------------------------------------ */
38
39 unsigned int video_debug;
40 static unsigned int gbuffers = 8;
41 static unsigned int noninterlaced; /* 0 */
42 static unsigned int gbufsize = 720*576*4;
43 static unsigned int gbufsize_max = 720*576*4;
44 static char secam[] = "--";
45 module_param(video_debug, int, 0644);
46 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
47 module_param(gbuffers, int, 0444);
48 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
49 module_param(noninterlaced, int, 0644);
50 MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
51 module_param_string(secam, secam, sizeof(secam), 0644);
52 MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
53
54
55 #define video_dbg(fmt, arg...) do { \
56 if (video_debug & 0x04) \
57 printk(KERN_DEBUG pr_fmt("video: " fmt), ## arg); \
58 } while (0)
59
60 /* ------------------------------------------------------------------ */
61 /* Defines for Video Output Port Register at address 0x191 */
62
63 /* Bit 0: VIP code T bit polarity */
64
65 #define VP_T_CODE_P_NON_INVERTED 0x00
66 #define VP_T_CODE_P_INVERTED 0x01
67
68 /* ------------------------------------------------------------------ */
69 /* Defines for Video Output Port Register at address 0x195 */
70
71 /* Bit 2: Video output clock delay control */
72
73 #define VP_CLK_CTRL2_NOT_DELAYED 0x00
74 #define VP_CLK_CTRL2_DELAYED 0x04
75
76 /* Bit 1: Video output clock invert control */
77
78 #define VP_CLK_CTRL1_NON_INVERTED 0x00
79 #define VP_CLK_CTRL1_INVERTED 0x02
80
81 /* ------------------------------------------------------------------ */
82 /* Defines for Video Output Port Register at address 0x196 */
83
84 /* Bits 2 to 0: VSYNC pin video vertical sync type */
85
86 #define VP_VS_TYPE_MASK 0x07
87
88 #define VP_VS_TYPE_OFF 0x00
89 #define VP_VS_TYPE_V123 0x01
90 #define VP_VS_TYPE_V_ITU 0x02
91 #define VP_VS_TYPE_VGATE_L 0x03
92 #define VP_VS_TYPE_RESERVED1 0x04
93 #define VP_VS_TYPE_RESERVED2 0x05
94 #define VP_VS_TYPE_F_ITU 0x06
95 #define VP_VS_TYPE_SC_FID 0x07
96
97 /* ------------------------------------------------------------------ */
98 /* data structs for video */
99
100 static int video_out[][9] = {
101 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
102 };
103
104 static struct saa7134_format formats[] = {
105 {
106 .name = "8 bpp gray",
107 .fourcc = V4L2_PIX_FMT_GREY,
108 .depth = 8,
109 .pm = 0x06,
110 },{
111 .name = "15 bpp RGB, le",
112 .fourcc = V4L2_PIX_FMT_RGB555,
113 .depth = 16,
114 .pm = 0x13 | 0x80,
115 },{
116 .name = "15 bpp RGB, be",
117 .fourcc = V4L2_PIX_FMT_RGB555X,
118 .depth = 16,
119 .pm = 0x13 | 0x80,
120 .bswap = 1,
121 },{
122 .name = "16 bpp RGB, le",
123 .fourcc = V4L2_PIX_FMT_RGB565,
124 .depth = 16,
125 .pm = 0x10 | 0x80,
126 },{
127 .name = "16 bpp RGB, be",
128 .fourcc = V4L2_PIX_FMT_RGB565X,
129 .depth = 16,
130 .pm = 0x10 | 0x80,
131 .bswap = 1,
132 },{
133 .name = "24 bpp RGB, le",
134 .fourcc = V4L2_PIX_FMT_BGR24,
135 .depth = 24,
136 .pm = 0x11,
137 },{
138 .name = "24 bpp RGB, be",
139 .fourcc = V4L2_PIX_FMT_RGB24,
140 .depth = 24,
141 .pm = 0x11,
142 .bswap = 1,
143 },{
144 .name = "32 bpp RGB, le",
145 .fourcc = V4L2_PIX_FMT_BGR32,
146 .depth = 32,
147 .pm = 0x12,
148 },{
149 .name = "32 bpp RGB, be",
150 .fourcc = V4L2_PIX_FMT_RGB32,
151 .depth = 32,
152 .pm = 0x12,
153 .bswap = 1,
154 .wswap = 1,
155 },{
156 .name = "4:2:2 packed, YUYV",
157 .fourcc = V4L2_PIX_FMT_YUYV,
158 .depth = 16,
159 .pm = 0x00,
160 .bswap = 1,
161 .yuv = 1,
162 },{
163 .name = "4:2:2 packed, UYVY",
164 .fourcc = V4L2_PIX_FMT_UYVY,
165 .depth = 16,
166 .pm = 0x00,
167 .yuv = 1,
168 },{
169 .name = "4:2:2 planar, Y-Cb-Cr",
170 .fourcc = V4L2_PIX_FMT_YUV422P,
171 .depth = 16,
172 .pm = 0x09,
173 .yuv = 1,
174 .planar = 1,
175 .hshift = 1,
176 .vshift = 0,
177 },{
178 .name = "4:2:0 planar, Y-Cb-Cr",
179 .fourcc = V4L2_PIX_FMT_YUV420,
180 .depth = 12,
181 .pm = 0x0a,
182 .yuv = 1,
183 .planar = 1,
184 .hshift = 1,
185 .vshift = 1,
186 },{
187 .name = "4:2:0 planar, Y-Cb-Cr",
188 .fourcc = V4L2_PIX_FMT_YVU420,
189 .depth = 12,
190 .pm = 0x0a,
191 .yuv = 1,
192 .planar = 1,
193 .uvswap = 1,
194 .hshift = 1,
195 .vshift = 1,
196 }
197 };
198 #define FORMATS ARRAY_SIZE(formats)
199
200 #define NORM_625_50 \
201 .h_start = 0, \
202 .h_stop = 719, \
203 .video_v_start = 24, \
204 .video_v_stop = 311, \
205 .vbi_v_start_0 = 7, \
206 .vbi_v_stop_0 = 23, \
207 .vbi_v_start_1 = 319, \
208 .src_timing = 4
209
210 #define NORM_525_60 \
211 .h_start = 0, \
212 .h_stop = 719, \
213 .video_v_start = 23, \
214 .video_v_stop = 262, \
215 .vbi_v_start_0 = 10, \
216 .vbi_v_stop_0 = 21, \
217 .vbi_v_start_1 = 273, \
218 .src_timing = 7
219
220 static struct saa7134_tvnorm tvnorms[] = {
221 {
222 .name = "PAL", /* autodetect */
223 .id = V4L2_STD_PAL,
224 NORM_625_50,
225
226 .sync_control = 0x18,
227 .luma_control = 0x40,
228 .chroma_ctrl1 = 0x81,
229 .chroma_gain = 0x2a,
230 .chroma_ctrl2 = 0x06,
231 .vgate_misc = 0x1c,
232
233 },{
234 .name = "PAL-BG",
235 .id = V4L2_STD_PAL_BG,
236 NORM_625_50,
237
238 .sync_control = 0x18,
239 .luma_control = 0x40,
240 .chroma_ctrl1 = 0x81,
241 .chroma_gain = 0x2a,
242 .chroma_ctrl2 = 0x06,
243 .vgate_misc = 0x1c,
244
245 },{
246 .name = "PAL-I",
247 .id = V4L2_STD_PAL_I,
248 NORM_625_50,
249
250 .sync_control = 0x18,
251 .luma_control = 0x40,
252 .chroma_ctrl1 = 0x81,
253 .chroma_gain = 0x2a,
254 .chroma_ctrl2 = 0x06,
255 .vgate_misc = 0x1c,
256
257 },{
258 .name = "PAL-DK",
259 .id = V4L2_STD_PAL_DK,
260 NORM_625_50,
261
262 .sync_control = 0x18,
263 .luma_control = 0x40,
264 .chroma_ctrl1 = 0x81,
265 .chroma_gain = 0x2a,
266 .chroma_ctrl2 = 0x06,
267 .vgate_misc = 0x1c,
268
269 },{
270 .name = "NTSC",
271 .id = V4L2_STD_NTSC,
272 NORM_525_60,
273
274 .sync_control = 0x59,
275 .luma_control = 0x40,
276 .chroma_ctrl1 = 0x89,
277 .chroma_gain = 0x2a,
278 .chroma_ctrl2 = 0x0e,
279 .vgate_misc = 0x18,
280
281 },{
282 .name = "SECAM",
283 .id = V4L2_STD_SECAM,
284 NORM_625_50,
285
286 .sync_control = 0x18,
287 .luma_control = 0x1b,
288 .chroma_ctrl1 = 0xd1,
289 .chroma_gain = 0x80,
290 .chroma_ctrl2 = 0x00,
291 .vgate_misc = 0x1c,
292
293 },{
294 .name = "SECAM-DK",
295 .id = V4L2_STD_SECAM_DK,
296 NORM_625_50,
297
298 .sync_control = 0x18,
299 .luma_control = 0x1b,
300 .chroma_ctrl1 = 0xd1,
301 .chroma_gain = 0x80,
302 .chroma_ctrl2 = 0x00,
303 .vgate_misc = 0x1c,
304
305 },{
306 .name = "SECAM-L",
307 .id = V4L2_STD_SECAM_L,
308 NORM_625_50,
309
310 .sync_control = 0x18,
311 .luma_control = 0x1b,
312 .chroma_ctrl1 = 0xd1,
313 .chroma_gain = 0x80,
314 .chroma_ctrl2 = 0x00,
315 .vgate_misc = 0x1c,
316
317 },{
318 .name = "SECAM-Lc",
319 .id = V4L2_STD_SECAM_LC,
320 NORM_625_50,
321
322 .sync_control = 0x18,
323 .luma_control = 0x1b,
324 .chroma_ctrl1 = 0xd1,
325 .chroma_gain = 0x80,
326 .chroma_ctrl2 = 0x00,
327 .vgate_misc = 0x1c,
328
329 },{
330 .name = "PAL-M",
331 .id = V4L2_STD_PAL_M,
332 NORM_525_60,
333
334 .sync_control = 0x59,
335 .luma_control = 0x40,
336 .chroma_ctrl1 = 0xb9,
337 .chroma_gain = 0x2a,
338 .chroma_ctrl2 = 0x0e,
339 .vgate_misc = 0x18,
340
341 },{
342 .name = "PAL-Nc",
343 .id = V4L2_STD_PAL_Nc,
344 NORM_625_50,
345
346 .sync_control = 0x18,
347 .luma_control = 0x40,
348 .chroma_ctrl1 = 0xa1,
349 .chroma_gain = 0x2a,
350 .chroma_ctrl2 = 0x06,
351 .vgate_misc = 0x1c,
352
353 },{
354 .name = "PAL-60",
355 .id = V4L2_STD_PAL_60,
356
357 .h_start = 0,
358 .h_stop = 719,
359 .video_v_start = 23,
360 .video_v_stop = 262,
361 .vbi_v_start_0 = 10,
362 .vbi_v_stop_0 = 21,
363 .vbi_v_start_1 = 273,
364 .src_timing = 7,
365
366 .sync_control = 0x18,
367 .luma_control = 0x40,
368 .chroma_ctrl1 = 0x81,
369 .chroma_gain = 0x2a,
370 .chroma_ctrl2 = 0x06,
371 .vgate_misc = 0x1c,
372 }
373 };
374 #define TVNORMS ARRAY_SIZE(tvnorms)
375
376 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
377 {
378 unsigned int i;
379
380 for (i = 0; i < FORMATS; i++)
381 if (formats[i].fourcc == fourcc)
382 return formats+i;
383 return NULL;
384 }
385
386 /* ------------------------------------------------------------------ */
387
388 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
389 {
390 video_dbg("set tv norm = %s\n",norm->name);
391 dev->tvnorm = norm;
392
393 /* setup cropping */
394 dev->crop_bounds.left = norm->h_start;
395 dev->crop_defrect.left = norm->h_start;
396 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
397 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
398
399 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
400 dev->crop_defrect.top = norm->video_v_start*2;
401 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
402 - dev->crop_bounds.top;
403 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
404
405 dev->crop_current = dev->crop_defrect;
406
407 saa7134_set_tvnorm_hw(dev);
408 }
409
410 static void video_mux(struct saa7134_dev *dev, int input)
411 {
412 video_dbg("video input = %d [%s]\n", input, card_in(dev, input).name);
413 dev->ctl_input = input;
414 set_tvnorm(dev, dev->tvnorm);
415 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
416 }
417
418
419 static void saa7134_set_decoder(struct saa7134_dev *dev)
420 {
421 int luma_control, sync_control, chroma_ctrl1, mux;
422
423 struct saa7134_tvnorm *norm = dev->tvnorm;
424 mux = card_in(dev, dev->ctl_input).vmux;
425
426 luma_control = norm->luma_control;
427 sync_control = norm->sync_control;
428 chroma_ctrl1 = norm->chroma_ctrl1;
429
430 if (mux > 5)
431 luma_control |= 0x80; /* svideo */
432 if (noninterlaced || dev->nosignal)
433 sync_control |= 0x20;
434
435 /* switch on auto standard detection */
436 sync_control |= SAA7134_SYNC_CTRL_AUFD;
437 chroma_ctrl1 |= SAA7134_CHROMA_CTRL1_AUTO0;
438 chroma_ctrl1 &= ~SAA7134_CHROMA_CTRL1_FCTC;
439 luma_control &= ~SAA7134_LUMA_CTRL_LDEL;
440
441 /* setup video decoder */
442 saa_writeb(SAA7134_INCR_DELAY, 0x08);
443 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
444 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
445
446 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
447 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
448 saa_writeb(SAA7134_HSYNC_START, 0xeb);
449 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
450 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
451
452 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
453 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
454 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
455
456 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
457 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
458
459 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
460 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
461
462 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
463 saa_writeb(SAA7134_CHROMA_CTRL1, chroma_ctrl1);
464 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
465
466 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
467 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
468
469 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
470 saa_writeb(SAA7134_VGATE_START, 0x11);
471 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
472 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
473 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
474 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
475 }
476
477 void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
478 {
479 saa7134_set_decoder(dev);
480
481 if (card_in(dev, dev->ctl_input).tv)
482 saa_call_all(dev, video, s_std, dev->tvnorm->id);
483 /* Set the correct norm for the saa6752hs. This function
484 does nothing if there is no saa6752hs. */
485 saa_call_empress(dev, video, s_std, dev->tvnorm->id);
486 }
487
488 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
489 {
490 static const struct {
491 int xpsc;
492 int xacl;
493 int xc2_1;
494 int xdcg;
495 int vpfy;
496 } vals[] = {
497 /* XPSC XACL XC2_1 XDCG VPFY */
498 { 1, 0, 0, 0, 0 },
499 { 2, 2, 1, 2, 2 },
500 { 3, 4, 1, 3, 2 },
501 { 4, 8, 1, 4, 2 },
502 { 5, 8, 1, 4, 2 },
503 { 6, 8, 1, 4, 3 },
504 { 7, 8, 1, 4, 3 },
505 { 8, 15, 0, 4, 3 },
506 { 9, 15, 0, 4, 3 },
507 { 10, 16, 1, 5, 3 },
508 };
509 static const int count = ARRAY_SIZE(vals);
510 int i;
511
512 for (i = 0; i < count; i++)
513 if (vals[i].xpsc == prescale)
514 break;
515 if (i == count)
516 return;
517
518 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
519 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
520 saa_writeb(SAA7134_LEVEL_CTRL(task),
521 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
522 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
523 (vals[i].vpfy << 2) | vals[i].vpfy);
524 }
525
526 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
527 {
528 int val,mirror;
529
530 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
531 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
532
533 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
534 if (yscale < 2048) {
535 /* LPI */
536 video_dbg("yscale LPI yscale=%d\n",yscale);
537 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
538 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
539 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
540 } else {
541 /* ACM */
542 val = 0x40 * 1024 / yscale;
543 video_dbg("yscale ACM yscale=%d val=0x%x\n",yscale,val);
544 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
545 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
546 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
547 }
548 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
549 }
550
551 static void set_size(struct saa7134_dev *dev, int task,
552 int width, int height, int interlace)
553 {
554 int prescale,xscale,yscale,y_even,y_odd;
555 int h_start, h_stop, v_start, v_stop;
556 int div = interlace ? 2 : 1;
557
558 /* setup video scaler */
559 h_start = dev->crop_current.left;
560 v_start = dev->crop_current.top/2;
561 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
562 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
563
564 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
565 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
566 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
567 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
568 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
569 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
570 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
571 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
572
573 prescale = dev->crop_current.width / width;
574 if (0 == prescale)
575 prescale = 1;
576 xscale = 1024 * dev->crop_current.width / prescale / width;
577 yscale = 512 * div * dev->crop_current.height / height;
578 video_dbg("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
579 set_h_prescale(dev,task,prescale);
580 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
581 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
582 set_v_scale(dev,task,yscale);
583
584 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
585 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
586 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
587 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
588
589 /* deinterlace y offsets */
590 y_odd = dev->ctl_y_odd;
591 y_even = dev->ctl_y_even;
592 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
593 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
594 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
595 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
596 }
597
598 /* ------------------------------------------------------------------ */
599
600 struct cliplist {
601 __u16 position;
602 __u8 enable;
603 __u8 disable;
604 };
605
606 static void set_cliplist(struct saa7134_dev *dev, int reg,
607 struct cliplist *cl, int entries, char *name)
608 {
609 __u8 winbits = 0;
610 int i;
611
612 for (i = 0; i < entries; i++) {
613 winbits |= cl[i].enable;
614 winbits &= ~cl[i].disable;
615 if (i < 15 && cl[i].position == cl[i+1].position)
616 continue;
617 saa_writeb(reg + 0, winbits);
618 saa_writeb(reg + 2, cl[i].position & 0xff);
619 saa_writeb(reg + 3, cl[i].position >> 8);
620 video_dbg("clip: %s winbits=%02x pos=%d\n",
621 name,winbits,cl[i].position);
622 reg += 8;
623 }
624 for (; reg < 0x400; reg += 8) {
625 saa_writeb(reg+ 0, 0);
626 saa_writeb(reg + 1, 0);
627 saa_writeb(reg + 2, 0);
628 saa_writeb(reg + 3, 0);
629 }
630 }
631
632 static int clip_range(int val)
633 {
634 if (val < 0)
635 val = 0;
636 return val;
637 }
638
639 /* Sort into smallest position first order */
640 static int cliplist_cmp(const void *a, const void *b)
641 {
642 const struct cliplist *cla = a;
643 const struct cliplist *clb = b;
644 if (cla->position < clb->position)
645 return -1;
646 if (cla->position > clb->position)
647 return 1;
648 return 0;
649 }
650
651 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
652 int nclips, int interlace)
653 {
654 struct cliplist col[16], row[16];
655 int cols = 0, rows = 0, i;
656 int div = interlace ? 2 : 1;
657
658 memset(col, 0, sizeof(col));
659 memset(row, 0, sizeof(row));
660 for (i = 0; i < nclips && i < 8; i++) {
661 col[cols].position = clip_range(clips[i].c.left);
662 col[cols].enable = (1 << i);
663 cols++;
664 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
665 col[cols].disable = (1 << i);
666 cols++;
667 row[rows].position = clip_range(clips[i].c.top / div);
668 row[rows].enable = (1 << i);
669 rows++;
670 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
671 / div);
672 row[rows].disable = (1 << i);
673 rows++;
674 }
675 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
676 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
677 set_cliplist(dev,0x380,col,cols,"cols");
678 set_cliplist(dev,0x384,row,rows,"rows");
679 return 0;
680 }
681
682 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
683 {
684 enum v4l2_field field;
685 int maxw, maxh;
686
687 if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
688 return -EINVAL;
689 if (win->w.width < 48)
690 win->w.width = 48;
691 if (win->w.height < 32)
692 win->w.height = 32;
693 if (win->clipcount > 8)
694 win->clipcount = 8;
695
696 win->chromakey = 0;
697 win->global_alpha = 0;
698 field = win->field;
699 maxw = dev->crop_current.width;
700 maxh = dev->crop_current.height;
701
702 if (V4L2_FIELD_ANY == field) {
703 field = (win->w.height > maxh/2)
704 ? V4L2_FIELD_INTERLACED
705 : V4L2_FIELD_TOP;
706 }
707 switch (field) {
708 case V4L2_FIELD_TOP:
709 case V4L2_FIELD_BOTTOM:
710 maxh = maxh / 2;
711 break;
712 default:
713 field = V4L2_FIELD_INTERLACED;
714 break;
715 }
716
717 win->field = field;
718 if (win->w.width > maxw)
719 win->w.width = maxw;
720 if (win->w.height > maxh)
721 win->w.height = maxh;
722 return 0;
723 }
724
725 static int start_preview(struct saa7134_dev *dev)
726 {
727 unsigned long base,control,bpl;
728 int err;
729
730 err = verify_preview(dev, &dev->win, false);
731 if (0 != err)
732 return err;
733
734 dev->ovfield = dev->win.field;
735 video_dbg("start_preview %dx%d+%d+%d %s field=%s\n",
736 dev->win.w.width, dev->win.w.height,
737 dev->win.w.left, dev->win.w.top,
738 dev->ovfmt->name, v4l2_field_names[dev->ovfield]);
739
740 /* setup window + clipping */
741 set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
742 V4L2_FIELD_HAS_BOTH(dev->ovfield));
743 setup_clipping(dev, dev->clips, dev->nclips,
744 V4L2_FIELD_HAS_BOTH(dev->ovfield));
745 if (dev->ovfmt->yuv)
746 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
747 else
748 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
749 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
750
751 /* dma: setup channel 1 (= Video Task B) */
752 base = (unsigned long)dev->ovbuf.base;
753 base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
754 base += dev->ovfmt->depth/8 * dev->win.w.left;
755 bpl = dev->ovbuf.fmt.bytesperline;
756 control = SAA7134_RS_CONTROL_BURST_16;
757 if (dev->ovfmt->bswap)
758 control |= SAA7134_RS_CONTROL_BSWAP;
759 if (dev->ovfmt->wswap)
760 control |= SAA7134_RS_CONTROL_WSWAP;
761 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
762 saa_writel(SAA7134_RS_BA1(1),base);
763 saa_writel(SAA7134_RS_BA2(1),base+bpl);
764 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
765 saa_writel(SAA7134_RS_CONTROL(1),control);
766 } else {
767 saa_writel(SAA7134_RS_BA1(1),base);
768 saa_writel(SAA7134_RS_BA2(1),base);
769 saa_writel(SAA7134_RS_PITCH(1),bpl);
770 saa_writel(SAA7134_RS_CONTROL(1),control);
771 }
772
773 /* start dma */
774 dev->ovenable = 1;
775 saa7134_set_dmabits(dev);
776
777 return 0;
778 }
779
780 static int stop_preview(struct saa7134_dev *dev)
781 {
782 dev->ovenable = 0;
783 saa7134_set_dmabits(dev);
784 return 0;
785 }
786
787 /* ------------------------------------------------------------------ */
788
789 static int buffer_activate(struct saa7134_dev *dev,
790 struct saa7134_buf *buf,
791 struct saa7134_buf *next)
792 {
793 struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_queue->drv_priv;
794 unsigned long base,control,bpl;
795 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
796
797 video_dbg("buffer_activate buf=%p\n",buf);
798 buf->top_seen = 0;
799
800 set_size(dev, TASK_A, dev->width, dev->height,
801 V4L2_FIELD_HAS_BOTH(dev->field));
802 if (dev->fmt->yuv)
803 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
804 else
805 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
806 saa_writeb(SAA7134_OFMT_VIDEO_A, dev->fmt->pm);
807
808 /* DMA: setup channel 0 (= Video Task A0) */
809 base = saa7134_buffer_base(buf);
810 if (dev->fmt->planar)
811 bpl = dev->width;
812 else
813 bpl = (dev->width * dev->fmt->depth) / 8;
814 control = SAA7134_RS_CONTROL_BURST_16 |
815 SAA7134_RS_CONTROL_ME |
816 (dmaq->pt.dma >> 12);
817 if (dev->fmt->bswap)
818 control |= SAA7134_RS_CONTROL_BSWAP;
819 if (dev->fmt->wswap)
820 control |= SAA7134_RS_CONTROL_WSWAP;
821 if (V4L2_FIELD_HAS_BOTH(dev->field)) {
822 /* interlaced */
823 saa_writel(SAA7134_RS_BA1(0),base);
824 saa_writel(SAA7134_RS_BA2(0),base+bpl);
825 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
826 } else {
827 /* non-interlaced */
828 saa_writel(SAA7134_RS_BA1(0),base);
829 saa_writel(SAA7134_RS_BA2(0),base);
830 saa_writel(SAA7134_RS_PITCH(0),bpl);
831 }
832 saa_writel(SAA7134_RS_CONTROL(0),control);
833
834 if (dev->fmt->planar) {
835 /* DMA: setup channel 4+5 (= planar task A) */
836 bpl_uv = bpl >> dev->fmt->hshift;
837 lines_uv = dev->height >> dev->fmt->vshift;
838 base2 = base + bpl * dev->height;
839 base3 = base2 + bpl_uv * lines_uv;
840 if (dev->fmt->uvswap)
841 tmp = base2, base2 = base3, base3 = tmp;
842 video_dbg("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
843 bpl_uv,lines_uv,base2,base3);
844 if (V4L2_FIELD_HAS_BOTH(dev->field)) {
845 /* interlaced */
846 saa_writel(SAA7134_RS_BA1(4),base2);
847 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
848 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
849 saa_writel(SAA7134_RS_BA1(5),base3);
850 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
851 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
852 } else {
853 /* non-interlaced */
854 saa_writel(SAA7134_RS_BA1(4),base2);
855 saa_writel(SAA7134_RS_BA2(4),base2);
856 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
857 saa_writel(SAA7134_RS_BA1(5),base3);
858 saa_writel(SAA7134_RS_BA2(5),base3);
859 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
860 }
861 saa_writel(SAA7134_RS_CONTROL(4),control);
862 saa_writel(SAA7134_RS_CONTROL(5),control);
863 }
864
865 /* start DMA */
866 saa7134_set_dmabits(dev);
867 mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
868 return 0;
869 }
870
871 static int buffer_init(struct vb2_buffer *vb2)
872 {
873 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
874 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
875
876 dmaq->curr = NULL;
877 buf->activate = buffer_activate;
878 return 0;
879 }
880
881 static int buffer_prepare(struct vb2_buffer *vb2)
882 {
883 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
884 struct saa7134_dev *dev = dmaq->dev;
885 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
886 struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2, 0);
887 unsigned int size;
888
889 if (dma->sgl->offset) {
890 pr_err("The buffer is not page-aligned\n");
891 return -EINVAL;
892 }
893 size = (dev->width * dev->height * dev->fmt->depth) >> 3;
894 if (vb2_plane_size(vb2, 0) < size)
895 return -EINVAL;
896
897 vb2_set_plane_payload(vb2, 0, size);
898 vb2->v4l2_buf.field = dev->field;
899
900 return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
901 saa7134_buffer_startpage(buf));
902 }
903
904 static int queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
905 unsigned int *nbuffers, unsigned int *nplanes,
906 unsigned int sizes[], void *alloc_ctxs[])
907 {
908 struct saa7134_dmaqueue *dmaq = q->drv_priv;
909 struct saa7134_dev *dev = dmaq->dev;
910 int size = dev->fmt->depth * dev->width * dev->height >> 3;
911
912 if (dev->width < 48 ||
913 dev->height < 32 ||
914 dev->width/4 > dev->crop_current.width ||
915 dev->height/4 > dev->crop_current.height ||
916 dev->width > dev->crop_bounds.width ||
917 dev->height > dev->crop_bounds.height)
918 return -EINVAL;
919
920 *nbuffers = saa7134_buffer_count(size, *nbuffers);
921 *nplanes = 1;
922 sizes[0] = size;
923 alloc_ctxs[0] = dev->alloc_ctx;
924 return 0;
925 }
926
927 /*
928 * move buffer to hardware queue
929 */
930 void saa7134_vb2_buffer_queue(struct vb2_buffer *vb)
931 {
932 struct saa7134_dmaqueue *dmaq = vb->vb2_queue->drv_priv;
933 struct saa7134_dev *dev = dmaq->dev;
934 struct saa7134_buf *buf = container_of(vb, struct saa7134_buf, vb2);
935
936 saa7134_buffer_queue(dev, dmaq, buf);
937 }
938 EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue);
939
940 int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
941 {
942 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
943 struct saa7134_dev *dev = dmaq->dev;
944
945 /*
946 * Planar video capture and TS share the same DMA channel,
947 * so only one can be active at a time.
948 */
949 if (card_is_empress(dev) && vb2_is_busy(&dev->empress_vbq) &&
950 dmaq == &dev->video_q && dev->fmt->planar) {
951 struct saa7134_buf *buf, *tmp;
952
953 list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
954 list_del(&buf->entry);
955 vb2_buffer_done(&buf->vb2, VB2_BUF_STATE_QUEUED);
956 }
957 if (dmaq->curr) {
958 vb2_buffer_done(&dmaq->curr->vb2, VB2_BUF_STATE_QUEUED);
959 dmaq->curr = NULL;
960 }
961 return -EBUSY;
962 }
963
964 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
965 * configured conservatively, there's 22 usec of buffering for video.
966 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
967 * margin in case the FIFO is configured differently to the datasheet.
968 * Unfortunately, I lack register-level documentation to check the
969 * Linux FIFO setup and confirm the perfect value.
970 */
971 if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
972 (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
973 pm_qos_add_request(&dev->qos_request,
974 PM_QOS_CPU_DMA_LATENCY, 20);
975 dmaq->seq_nr = 0;
976
977 return 0;
978 }
979
980 void saa7134_vb2_stop_streaming(struct vb2_queue *vq)
981 {
982 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
983 struct saa7134_dev *dev = dmaq->dev;
984
985 saa7134_stop_streaming(dev, dmaq);
986
987 if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
988 (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
989 pm_qos_remove_request(&dev->qos_request);
990 }
991
992 static struct vb2_ops vb2_qops = {
993 .queue_setup = queue_setup,
994 .buf_init = buffer_init,
995 .buf_prepare = buffer_prepare,
996 .buf_queue = saa7134_vb2_buffer_queue,
997 .wait_prepare = vb2_ops_wait_prepare,
998 .wait_finish = vb2_ops_wait_finish,
999 .start_streaming = saa7134_vb2_start_streaming,
1000 .stop_streaming = saa7134_vb2_stop_streaming,
1001 };
1002
1003 /* ------------------------------------------------------------------ */
1004
1005 static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
1006 {
1007 struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
1008 unsigned long flags;
1009 int restart_overlay = 0;
1010
1011 switch (ctrl->id) {
1012 case V4L2_CID_BRIGHTNESS:
1013 dev->ctl_bright = ctrl->val;
1014 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
1015 break;
1016 case V4L2_CID_HUE:
1017 dev->ctl_hue = ctrl->val;
1018 saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
1019 break;
1020 case V4L2_CID_CONTRAST:
1021 dev->ctl_contrast = ctrl->val;
1022 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1023 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1024 break;
1025 case V4L2_CID_SATURATION:
1026 dev->ctl_saturation = ctrl->val;
1027 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1028 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1029 break;
1030 case V4L2_CID_AUDIO_MUTE:
1031 dev->ctl_mute = ctrl->val;
1032 saa7134_tvaudio_setmute(dev);
1033 break;
1034 case V4L2_CID_AUDIO_VOLUME:
1035 dev->ctl_volume = ctrl->val;
1036 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1037 break;
1038 case V4L2_CID_PRIVATE_INVERT:
1039 dev->ctl_invert = ctrl->val;
1040 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1041 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1042 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1043 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1044 break;
1045 case V4L2_CID_HFLIP:
1046 dev->ctl_mirror = ctrl->val;
1047 restart_overlay = 1;
1048 break;
1049 case V4L2_CID_PRIVATE_Y_EVEN:
1050 dev->ctl_y_even = ctrl->val;
1051 restart_overlay = 1;
1052 break;
1053 case V4L2_CID_PRIVATE_Y_ODD:
1054 dev->ctl_y_odd = ctrl->val;
1055 restart_overlay = 1;
1056 break;
1057 case V4L2_CID_PRIVATE_AUTOMUTE:
1058 {
1059 struct v4l2_priv_tun_config tda9887_cfg;
1060
1061 tda9887_cfg.tuner = TUNER_TDA9887;
1062 tda9887_cfg.priv = &dev->tda9887_conf;
1063
1064 dev->ctl_automute = ctrl->val;
1065 if (dev->tda9887_conf) {
1066 if (dev->ctl_automute)
1067 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1068 else
1069 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1070
1071 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1072 }
1073 break;
1074 }
1075 default:
1076 return -EINVAL;
1077 }
1078 if (restart_overlay && dev->overlay_owner) {
1079 spin_lock_irqsave(&dev->slock, flags);
1080 stop_preview(dev);
1081 start_preview(dev);
1082 spin_unlock_irqrestore(&dev->slock, flags);
1083 }
1084 return 0;
1085 }
1086
1087 /* ------------------------------------------------------------------ */
1088
1089 static int video_open(struct file *file)
1090 {
1091 struct video_device *vdev = video_devdata(file);
1092 struct saa7134_dev *dev = video_drvdata(file);
1093 int ret = v4l2_fh_open(file);
1094
1095 if (ret < 0)
1096 return ret;
1097
1098 mutex_lock(&dev->lock);
1099 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1100 /* switch to radio mode */
1101 saa7134_tvaudio_setinput(dev, &card(dev).radio);
1102 saa_call_all(dev, tuner, s_radio);
1103 } else {
1104 /* switch to video/vbi mode */
1105 video_mux(dev, dev->ctl_input);
1106 }
1107 mutex_unlock(&dev->lock);
1108
1109 return 0;
1110 }
1111
1112 static int video_release(struct file *file)
1113 {
1114 struct video_device *vdev = video_devdata(file);
1115 struct saa7134_dev *dev = video_drvdata(file);
1116 struct v4l2_fh *fh = file->private_data;
1117 struct saa6588_command cmd;
1118 unsigned long flags;
1119
1120 mutex_lock(&dev->lock);
1121 saa7134_tvaudio_close(dev);
1122
1123 /* turn off overlay */
1124 if (fh == dev->overlay_owner) {
1125 spin_lock_irqsave(&dev->slock,flags);
1126 stop_preview(dev);
1127 spin_unlock_irqrestore(&dev->slock,flags);
1128 dev->overlay_owner = NULL;
1129 }
1130
1131 if (vdev->vfl_type == VFL_TYPE_RADIO)
1132 v4l2_fh_release(file);
1133 else
1134 _vb2_fop_release(file, NULL);
1135
1136 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1137 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1138 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1139 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1140 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1141
1142 saa_call_all(dev, core, s_power, 0);
1143 if (vdev->vfl_type == VFL_TYPE_RADIO)
1144 saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
1145 mutex_unlock(&dev->lock);
1146
1147 return 0;
1148 }
1149
1150 static ssize_t radio_read(struct file *file, char __user *data,
1151 size_t count, loff_t *ppos)
1152 {
1153 struct saa7134_dev *dev = video_drvdata(file);
1154 struct saa6588_command cmd;
1155
1156 cmd.block_count = count/3;
1157 cmd.nonblocking = file->f_flags & O_NONBLOCK;
1158 cmd.buffer = data;
1159 cmd.instance = file;
1160 cmd.result = -ENODEV;
1161
1162 mutex_lock(&dev->lock);
1163 saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
1164 mutex_unlock(&dev->lock);
1165
1166 return cmd.result;
1167 }
1168
1169 static unsigned int radio_poll(struct file *file, poll_table *wait)
1170 {
1171 struct saa7134_dev *dev = video_drvdata(file);
1172 struct saa6588_command cmd;
1173 unsigned int rc = v4l2_ctrl_poll(file, wait);
1174
1175 cmd.instance = file;
1176 cmd.event_list = wait;
1177 cmd.result = 0;
1178 mutex_lock(&dev->lock);
1179 saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
1180 mutex_unlock(&dev->lock);
1181
1182 return rc | cmd.result;
1183 }
1184
1185 /* ------------------------------------------------------------------ */
1186
1187 static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1188 struct v4l2_format *f)
1189 {
1190 struct saa7134_dev *dev = video_drvdata(file);
1191 struct saa7134_tvnorm *norm = dev->tvnorm;
1192
1193 memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1194 f->fmt.vbi.sampling_rate = 6750000 * 4;
1195 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1196 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1197 f->fmt.vbi.offset = 64 * 4;
1198 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1199 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1200 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1201 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1202 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1203
1204 return 0;
1205 }
1206
1207 static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1208 struct v4l2_format *f)
1209 {
1210 struct saa7134_dev *dev = video_drvdata(file);
1211
1212 f->fmt.pix.width = dev->width;
1213 f->fmt.pix.height = dev->height;
1214 f->fmt.pix.field = dev->field;
1215 f->fmt.pix.pixelformat = dev->fmt->fourcc;
1216 f->fmt.pix.bytesperline =
1217 (f->fmt.pix.width * dev->fmt->depth) >> 3;
1218 f->fmt.pix.sizeimage =
1219 f->fmt.pix.height * f->fmt.pix.bytesperline;
1220 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1221 return 0;
1222 }
1223
1224 static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1225 struct v4l2_format *f)
1226 {
1227 struct saa7134_dev *dev = video_drvdata(file);
1228 struct v4l2_clip __user *clips = f->fmt.win.clips;
1229 u32 clipcount = f->fmt.win.clipcount;
1230 int err = 0;
1231 int i;
1232
1233 if (saa7134_no_overlay > 0) {
1234 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1235 return -EINVAL;
1236 }
1237 f->fmt.win = dev->win;
1238 f->fmt.win.clips = clips;
1239 if (clips == NULL)
1240 clipcount = 0;
1241 if (dev->nclips < clipcount)
1242 clipcount = dev->nclips;
1243 f->fmt.win.clipcount = clipcount;
1244
1245 for (i = 0; !err && i < clipcount; i++) {
1246 if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c,
1247 sizeof(struct v4l2_rect)))
1248 err = -EFAULT;
1249 }
1250
1251 return err;
1252 }
1253
1254 static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1255 struct v4l2_format *f)
1256 {
1257 struct saa7134_dev *dev = video_drvdata(file);
1258 struct saa7134_format *fmt;
1259 enum v4l2_field field;
1260 unsigned int maxw, maxh;
1261
1262 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1263 if (NULL == fmt)
1264 return -EINVAL;
1265
1266 field = f->fmt.pix.field;
1267 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1268 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1269
1270 if (V4L2_FIELD_ANY == field) {
1271 field = (f->fmt.pix.height > maxh/2)
1272 ? V4L2_FIELD_INTERLACED
1273 : V4L2_FIELD_BOTTOM;
1274 }
1275 switch (field) {
1276 case V4L2_FIELD_TOP:
1277 case V4L2_FIELD_BOTTOM:
1278 maxh = maxh / 2;
1279 break;
1280 default:
1281 field = V4L2_FIELD_INTERLACED;
1282 break;
1283 }
1284
1285 f->fmt.pix.field = field;
1286 if (f->fmt.pix.width < 48)
1287 f->fmt.pix.width = 48;
1288 if (f->fmt.pix.height < 32)
1289 f->fmt.pix.height = 32;
1290 if (f->fmt.pix.width > maxw)
1291 f->fmt.pix.width = maxw;
1292 if (f->fmt.pix.height > maxh)
1293 f->fmt.pix.height = maxh;
1294 f->fmt.pix.width &= ~0x03;
1295 f->fmt.pix.bytesperline =
1296 (f->fmt.pix.width * fmt->depth) >> 3;
1297 f->fmt.pix.sizeimage =
1298 f->fmt.pix.height * f->fmt.pix.bytesperline;
1299 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1300
1301 return 0;
1302 }
1303
1304 static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1305 struct v4l2_format *f)
1306 {
1307 struct saa7134_dev *dev = video_drvdata(file);
1308
1309 if (saa7134_no_overlay > 0) {
1310 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1311 return -EINVAL;
1312 }
1313
1314 if (f->fmt.win.clips == NULL)
1315 f->fmt.win.clipcount = 0;
1316 return verify_preview(dev, &f->fmt.win, true);
1317 }
1318
1319 static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1320 struct v4l2_format *f)
1321 {
1322 struct saa7134_dev *dev = video_drvdata(file);
1323 int err;
1324
1325 err = saa7134_try_fmt_vid_cap(file, priv, f);
1326 if (0 != err)
1327 return err;
1328
1329 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1330 dev->width = f->fmt.pix.width;
1331 dev->height = f->fmt.pix.height;
1332 dev->field = f->fmt.pix.field;
1333 return 0;
1334 }
1335
1336 static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1337 struct v4l2_format *f)
1338 {
1339 struct saa7134_dev *dev = video_drvdata(file);
1340 int err;
1341 unsigned long flags;
1342
1343 if (saa7134_no_overlay > 0) {
1344 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1345 return -EINVAL;
1346 }
1347 if (f->fmt.win.clips == NULL)
1348 f->fmt.win.clipcount = 0;
1349 err = verify_preview(dev, &f->fmt.win, true);
1350 if (0 != err)
1351 return err;
1352
1353 dev->win = f->fmt.win;
1354 dev->nclips = f->fmt.win.clipcount;
1355
1356 if (copy_from_user(dev->clips, f->fmt.win.clips,
1357 sizeof(struct v4l2_clip) * dev->nclips))
1358 return -EFAULT;
1359
1360 if (priv == dev->overlay_owner) {
1361 spin_lock_irqsave(&dev->slock, flags);
1362 stop_preview(dev);
1363 start_preview(dev);
1364 spin_unlock_irqrestore(&dev->slock, flags);
1365 }
1366
1367 return 0;
1368 }
1369
1370 int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
1371 {
1372 struct saa7134_dev *dev = video_drvdata(file);
1373 unsigned int n;
1374
1375 n = i->index;
1376 if (n >= SAA7134_INPUT_MAX)
1377 return -EINVAL;
1378 if (NULL == card_in(dev, i->index).name)
1379 return -EINVAL;
1380 i->index = n;
1381 i->type = V4L2_INPUT_TYPE_CAMERA;
1382 strcpy(i->name, card_in(dev, n).name);
1383 if (card_in(dev, n).tv)
1384 i->type = V4L2_INPUT_TYPE_TUNER;
1385 if (n == dev->ctl_input) {
1386 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1387 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1388
1389 if (0 != (v1 & 0x40))
1390 i->status |= V4L2_IN_ST_NO_H_LOCK;
1391 if (0 != (v2 & 0x40))
1392 i->status |= V4L2_IN_ST_NO_SIGNAL;
1393 if (0 != (v2 & 0x0e))
1394 i->status |= V4L2_IN_ST_MACROVISION;
1395 }
1396 i->std = SAA7134_NORMS;
1397 return 0;
1398 }
1399 EXPORT_SYMBOL_GPL(saa7134_enum_input);
1400
1401 int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1402 {
1403 struct saa7134_dev *dev = video_drvdata(file);
1404
1405 *i = dev->ctl_input;
1406 return 0;
1407 }
1408 EXPORT_SYMBOL_GPL(saa7134_g_input);
1409
1410 int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1411 {
1412 struct saa7134_dev *dev = video_drvdata(file);
1413
1414 if (i >= SAA7134_INPUT_MAX)
1415 return -EINVAL;
1416 if (NULL == card_in(dev, i).name)
1417 return -EINVAL;
1418 video_mux(dev, i);
1419 return 0;
1420 }
1421 EXPORT_SYMBOL_GPL(saa7134_s_input);
1422
1423 int saa7134_querycap(struct file *file, void *priv,
1424 struct v4l2_capability *cap)
1425 {
1426 struct saa7134_dev *dev = video_drvdata(file);
1427 struct video_device *vdev = video_devdata(file);
1428 u32 radio_caps, video_caps, vbi_caps;
1429
1430 unsigned int tuner_type = dev->tuner_type;
1431
1432 strcpy(cap->driver, "saa7134");
1433 strlcpy(cap->card, saa7134_boards[dev->board].name,
1434 sizeof(cap->card));
1435 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1436
1437 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1438 if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET))
1439 cap->device_caps |= V4L2_CAP_TUNER;
1440
1441 radio_caps = V4L2_CAP_RADIO;
1442 if (dev->has_rds)
1443 radio_caps |= V4L2_CAP_RDS_CAPTURE;
1444
1445 video_caps = V4L2_CAP_VIDEO_CAPTURE;
1446 if (saa7134_no_overlay <= 0 && !is_empress(file))
1447 video_caps |= V4L2_CAP_VIDEO_OVERLAY;
1448
1449 vbi_caps = V4L2_CAP_VBI_CAPTURE;
1450
1451 switch (vdev->vfl_type) {
1452 case VFL_TYPE_RADIO:
1453 cap->device_caps |= radio_caps;
1454 break;
1455 case VFL_TYPE_GRABBER:
1456 cap->device_caps |= video_caps;
1457 break;
1458 case VFL_TYPE_VBI:
1459 cap->device_caps |= vbi_caps;
1460 break;
1461 }
1462 cap->capabilities = radio_caps | video_caps | vbi_caps |
1463 cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1464 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1465 cap->device_caps &= ~V4L2_CAP_STREAMING;
1466 if (!dev->has_rds)
1467 cap->device_caps &= ~V4L2_CAP_READWRITE;
1468 }
1469
1470 return 0;
1471 }
1472 EXPORT_SYMBOL_GPL(saa7134_querycap);
1473
1474 int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
1475 {
1476 struct saa7134_dev *dev = video_drvdata(file);
1477 struct v4l2_fh *fh = priv;
1478 unsigned long flags;
1479 unsigned int i;
1480 v4l2_std_id fixup;
1481
1482 if (is_empress(file) && dev->overlay_owner) {
1483 /* Don't change the std from the mpeg device
1484 if overlay is active. */
1485 return -EBUSY;
1486 }
1487
1488 for (i = 0; i < TVNORMS; i++)
1489 if (id == tvnorms[i].id)
1490 break;
1491
1492 if (i == TVNORMS)
1493 for (i = 0; i < TVNORMS; i++)
1494 if (id & tvnorms[i].id)
1495 break;
1496 if (i == TVNORMS)
1497 return -EINVAL;
1498
1499 if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1500 if (secam[0] == 'L' || secam[0] == 'l') {
1501 if (secam[1] == 'C' || secam[1] == 'c')
1502 fixup = V4L2_STD_SECAM_LC;
1503 else
1504 fixup = V4L2_STD_SECAM_L;
1505 } else {
1506 if (secam[0] == 'D' || secam[0] == 'd')
1507 fixup = V4L2_STD_SECAM_DK;
1508 else
1509 fixup = V4L2_STD_SECAM;
1510 }
1511 for (i = 0; i < TVNORMS; i++) {
1512 if (fixup == tvnorms[i].id)
1513 break;
1514 }
1515 if (i == TVNORMS)
1516 return -EINVAL;
1517 }
1518
1519 id = tvnorms[i].id;
1520
1521 if (!is_empress(file) && fh == dev->overlay_owner) {
1522 spin_lock_irqsave(&dev->slock, flags);
1523 stop_preview(dev);
1524 spin_unlock_irqrestore(&dev->slock, flags);
1525
1526 set_tvnorm(dev, &tvnorms[i]);
1527
1528 spin_lock_irqsave(&dev->slock, flags);
1529 start_preview(dev);
1530 spin_unlock_irqrestore(&dev->slock, flags);
1531 } else
1532 set_tvnorm(dev, &tvnorms[i]);
1533
1534 saa7134_tvaudio_do_scan(dev);
1535 return 0;
1536 }
1537 EXPORT_SYMBOL_GPL(saa7134_s_std);
1538
1539 int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1540 {
1541 struct saa7134_dev *dev = video_drvdata(file);
1542
1543 *id = dev->tvnorm->id;
1544 return 0;
1545 }
1546 EXPORT_SYMBOL_GPL(saa7134_g_std);
1547
1548 static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
1549 {
1550 static v4l2_std_id stds[] = {
1551 V4L2_STD_UNKNOWN,
1552 V4L2_STD_NTSC,
1553 V4L2_STD_PAL,
1554 V4L2_STD_SECAM };
1555
1556 v4l2_std_id result = 0;
1557
1558 u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
1559 u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
1560
1561 if (!(st2 & 0x1)) /* RDCAP == 0 */
1562 result = V4L2_STD_UNKNOWN;
1563 else
1564 result = stds[st1 & 0x03];
1565
1566 return result;
1567 }
1568
1569 int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
1570 {
1571 struct saa7134_dev *dev = video_drvdata(file);
1572 *std &= saa7134_read_std(dev);
1573 return 0;
1574 }
1575 EXPORT_SYMBOL_GPL(saa7134_querystd);
1576
1577 static int saa7134_cropcap(struct file *file, void *priv,
1578 struct v4l2_cropcap *cap)
1579 {
1580 struct saa7134_dev *dev = video_drvdata(file);
1581
1582 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1583 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1584 return -EINVAL;
1585 cap->bounds = dev->crop_bounds;
1586 cap->defrect = dev->crop_defrect;
1587 cap->pixelaspect.numerator = 1;
1588 cap->pixelaspect.denominator = 1;
1589 if (dev->tvnorm->id & V4L2_STD_525_60) {
1590 cap->pixelaspect.numerator = 11;
1591 cap->pixelaspect.denominator = 10;
1592 }
1593 if (dev->tvnorm->id & V4L2_STD_625_50) {
1594 cap->pixelaspect.numerator = 54;
1595 cap->pixelaspect.denominator = 59;
1596 }
1597 return 0;
1598 }
1599
1600 static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1601 {
1602 struct saa7134_dev *dev = video_drvdata(file);
1603
1604 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1605 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1606 return -EINVAL;
1607 crop->c = dev->crop_current;
1608 return 0;
1609 }
1610
1611 static int saa7134_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
1612 {
1613 struct saa7134_dev *dev = video_drvdata(file);
1614 struct v4l2_rect *b = &dev->crop_bounds;
1615 struct v4l2_rect *c = &dev->crop_current;
1616
1617 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1618 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1619 return -EINVAL;
1620
1621 if (dev->overlay_owner)
1622 return -EBUSY;
1623 if (vb2_is_streaming(&dev->video_vbq))
1624 return -EBUSY;
1625
1626 *c = crop->c;
1627 if (c->top < b->top)
1628 c->top = b->top;
1629 if (c->top > b->top + b->height)
1630 c->top = b->top + b->height;
1631 if (c->height > b->top - c->top + b->height)
1632 c->height = b->top - c->top + b->height;
1633
1634 if (c->left < b->left)
1635 c->left = b->left;
1636 if (c->left > b->left + b->width)
1637 c->left = b->left + b->width;
1638 if (c->width > b->left - c->left + b->width)
1639 c->width = b->left - c->left + b->width;
1640 return 0;
1641 }
1642
1643 int saa7134_g_tuner(struct file *file, void *priv,
1644 struct v4l2_tuner *t)
1645 {
1646 struct saa7134_dev *dev = video_drvdata(file);
1647 int n;
1648
1649 if (0 != t->index)
1650 return -EINVAL;
1651 memset(t, 0, sizeof(*t));
1652 for (n = 0; n < SAA7134_INPUT_MAX; n++) {
1653 if (card_in(dev, n).tv)
1654 break;
1655 }
1656 if (n == SAA7134_INPUT_MAX)
1657 return -EINVAL;
1658 if (NULL != card_in(dev, n).name) {
1659 strcpy(t->name, "Television");
1660 t->type = V4L2_TUNER_ANALOG_TV;
1661 saa_call_all(dev, tuner, g_tuner, t);
1662 t->capability = V4L2_TUNER_CAP_NORM |
1663 V4L2_TUNER_CAP_STEREO |
1664 V4L2_TUNER_CAP_LANG1 |
1665 V4L2_TUNER_CAP_LANG2;
1666 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1667 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1668 }
1669 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1670 t->signal = 0xffff;
1671 return 0;
1672 }
1673 EXPORT_SYMBOL_GPL(saa7134_g_tuner);
1674
1675 int saa7134_s_tuner(struct file *file, void *priv,
1676 const struct v4l2_tuner *t)
1677 {
1678 struct saa7134_dev *dev = video_drvdata(file);
1679 int rx, mode;
1680
1681 if (0 != t->index)
1682 return -EINVAL;
1683
1684 mode = dev->thread.mode;
1685 if (UNSET == mode) {
1686 rx = saa7134_tvaudio_getstereo(dev);
1687 mode = saa7134_tvaudio_rx2mode(rx);
1688 }
1689 if (mode != t->audmode)
1690 dev->thread.mode = t->audmode;
1691
1692 return 0;
1693 }
1694 EXPORT_SYMBOL_GPL(saa7134_s_tuner);
1695
1696 int saa7134_g_frequency(struct file *file, void *priv,
1697 struct v4l2_frequency *f)
1698 {
1699 struct saa7134_dev *dev = video_drvdata(file);
1700
1701 if (0 != f->tuner)
1702 return -EINVAL;
1703
1704 saa_call_all(dev, tuner, g_frequency, f);
1705
1706 return 0;
1707 }
1708 EXPORT_SYMBOL_GPL(saa7134_g_frequency);
1709
1710 int saa7134_s_frequency(struct file *file, void *priv,
1711 const struct v4l2_frequency *f)
1712 {
1713 struct saa7134_dev *dev = video_drvdata(file);
1714
1715 if (0 != f->tuner)
1716 return -EINVAL;
1717
1718 saa_call_all(dev, tuner, s_frequency, f);
1719
1720 saa7134_tvaudio_do_scan(dev);
1721 return 0;
1722 }
1723 EXPORT_SYMBOL_GPL(saa7134_s_frequency);
1724
1725 static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
1726 struct v4l2_fmtdesc *f)
1727 {
1728 if (f->index >= FORMATS)
1729 return -EINVAL;
1730
1731 strlcpy(f->description, formats[f->index].name,
1732 sizeof(f->description));
1733
1734 f->pixelformat = formats[f->index].fourcc;
1735
1736 return 0;
1737 }
1738
1739 static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
1740 struct v4l2_fmtdesc *f)
1741 {
1742 if (saa7134_no_overlay > 0) {
1743 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1744 return -EINVAL;
1745 }
1746
1747 if ((f->index >= FORMATS) || formats[f->index].planar)
1748 return -EINVAL;
1749
1750 strlcpy(f->description, formats[f->index].name,
1751 sizeof(f->description));
1752
1753 f->pixelformat = formats[f->index].fourcc;
1754
1755 return 0;
1756 }
1757
1758 static int saa7134_g_fbuf(struct file *file, void *f,
1759 struct v4l2_framebuffer *fb)
1760 {
1761 struct saa7134_dev *dev = video_drvdata(file);
1762
1763 *fb = dev->ovbuf;
1764 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1765
1766 return 0;
1767 }
1768
1769 static int saa7134_s_fbuf(struct file *file, void *f,
1770 const struct v4l2_framebuffer *fb)
1771 {
1772 struct saa7134_dev *dev = video_drvdata(file);
1773 struct saa7134_format *fmt;
1774
1775 if (!capable(CAP_SYS_ADMIN) &&
1776 !capable(CAP_SYS_RAWIO))
1777 return -EPERM;
1778
1779 /* check args */
1780 fmt = format_by_fourcc(fb->fmt.pixelformat);
1781 if (NULL == fmt)
1782 return -EINVAL;
1783
1784 /* ok, accept it */
1785 dev->ovbuf = *fb;
1786 dev->ovfmt = fmt;
1787 if (0 == dev->ovbuf.fmt.bytesperline)
1788 dev->ovbuf.fmt.bytesperline =
1789 dev->ovbuf.fmt.width*fmt->depth/8;
1790 return 0;
1791 }
1792
1793 static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
1794 {
1795 struct saa7134_dev *dev = video_drvdata(file);
1796 unsigned long flags;
1797
1798 if (on) {
1799 if (saa7134_no_overlay > 0) {
1800 video_dbg("no_overlay\n");
1801 return -EINVAL;
1802 }
1803
1804 if (dev->overlay_owner && priv != dev->overlay_owner)
1805 return -EBUSY;
1806 dev->overlay_owner = priv;
1807 spin_lock_irqsave(&dev->slock, flags);
1808 start_preview(dev);
1809 spin_unlock_irqrestore(&dev->slock, flags);
1810 }
1811 if (!on) {
1812 if (priv != dev->overlay_owner)
1813 return -EINVAL;
1814 spin_lock_irqsave(&dev->slock, flags);
1815 stop_preview(dev);
1816 spin_unlock_irqrestore(&dev->slock, flags);
1817 dev->overlay_owner = NULL;
1818 }
1819 return 0;
1820 }
1821
1822 #ifdef CONFIG_VIDEO_ADV_DEBUG
1823 static int vidioc_g_register (struct file *file, void *priv,
1824 struct v4l2_dbg_register *reg)
1825 {
1826 struct saa7134_dev *dev = video_drvdata(file);
1827
1828 reg->val = saa_readb(reg->reg & 0xffffff);
1829 reg->size = 1;
1830 return 0;
1831 }
1832
1833 static int vidioc_s_register (struct file *file, void *priv,
1834 const struct v4l2_dbg_register *reg)
1835 {
1836 struct saa7134_dev *dev = video_drvdata(file);
1837
1838 saa_writeb(reg->reg & 0xffffff, reg->val);
1839 return 0;
1840 }
1841 #endif
1842
1843 static int radio_g_tuner(struct file *file, void *priv,
1844 struct v4l2_tuner *t)
1845 {
1846 struct saa7134_dev *dev = video_drvdata(file);
1847
1848 if (0 != t->index)
1849 return -EINVAL;
1850
1851 strcpy(t->name, "Radio");
1852
1853 saa_call_all(dev, tuner, g_tuner, t);
1854 t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
1855 if (dev->input->amux == TV) {
1856 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
1857 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
1858 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
1859 }
1860 return 0;
1861 }
1862 static int radio_s_tuner(struct file *file, void *priv,
1863 const struct v4l2_tuner *t)
1864 {
1865 struct saa7134_dev *dev = video_drvdata(file);
1866
1867 if (0 != t->index)
1868 return -EINVAL;
1869
1870 saa_call_all(dev, tuner, s_tuner, t);
1871 return 0;
1872 }
1873
1874 static const struct v4l2_file_operations video_fops =
1875 {
1876 .owner = THIS_MODULE,
1877 .open = video_open,
1878 .release = video_release,
1879 .read = vb2_fop_read,
1880 .poll = vb2_fop_poll,
1881 .mmap = vb2_fop_mmap,
1882 .unlocked_ioctl = video_ioctl2,
1883 };
1884
1885 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1886 .vidioc_querycap = saa7134_querycap,
1887 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
1888 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
1889 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
1890 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
1891 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
1892 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
1893 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
1894 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
1895 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1896 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1897 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1898 .vidioc_cropcap = saa7134_cropcap,
1899 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1900 .vidioc_querybuf = vb2_ioctl_querybuf,
1901 .vidioc_qbuf = vb2_ioctl_qbuf,
1902 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1903 .vidioc_s_std = saa7134_s_std,
1904 .vidioc_g_std = saa7134_g_std,
1905 .vidioc_querystd = saa7134_querystd,
1906 .vidioc_enum_input = saa7134_enum_input,
1907 .vidioc_g_input = saa7134_g_input,
1908 .vidioc_s_input = saa7134_s_input,
1909 .vidioc_streamon = vb2_ioctl_streamon,
1910 .vidioc_streamoff = vb2_ioctl_streamoff,
1911 .vidioc_g_tuner = saa7134_g_tuner,
1912 .vidioc_s_tuner = saa7134_s_tuner,
1913 .vidioc_g_crop = saa7134_g_crop,
1914 .vidioc_s_crop = saa7134_s_crop,
1915 .vidioc_g_fbuf = saa7134_g_fbuf,
1916 .vidioc_s_fbuf = saa7134_s_fbuf,
1917 .vidioc_overlay = saa7134_overlay,
1918 .vidioc_g_frequency = saa7134_g_frequency,
1919 .vidioc_s_frequency = saa7134_s_frequency,
1920 #ifdef CONFIG_VIDEO_ADV_DEBUG
1921 .vidioc_g_register = vidioc_g_register,
1922 .vidioc_s_register = vidioc_s_register,
1923 #endif
1924 .vidioc_log_status = v4l2_ctrl_log_status,
1925 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1926 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1927 };
1928
1929 static const struct v4l2_file_operations radio_fops = {
1930 .owner = THIS_MODULE,
1931 .open = video_open,
1932 .read = radio_read,
1933 .release = video_release,
1934 .unlocked_ioctl = video_ioctl2,
1935 .poll = radio_poll,
1936 };
1937
1938 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1939 .vidioc_querycap = saa7134_querycap,
1940 .vidioc_g_tuner = radio_g_tuner,
1941 .vidioc_s_tuner = radio_s_tuner,
1942 .vidioc_g_frequency = saa7134_g_frequency,
1943 .vidioc_s_frequency = saa7134_s_frequency,
1944 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1945 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1946 };
1947
1948 /* ----------------------------------------------------------- */
1949 /* exported stuff */
1950
1951 struct video_device saa7134_video_template = {
1952 .name = "saa7134-video",
1953 .fops = &video_fops,
1954 .ioctl_ops = &video_ioctl_ops,
1955 .tvnorms = SAA7134_NORMS,
1956 };
1957
1958 struct video_device saa7134_radio_template = {
1959 .name = "saa7134-radio",
1960 .fops = &radio_fops,
1961 .ioctl_ops = &radio_ioctl_ops,
1962 };
1963
1964 static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
1965 .s_ctrl = saa7134_s_ctrl,
1966 };
1967
1968 static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
1969 .ops = &saa7134_ctrl_ops,
1970 .id = V4L2_CID_PRIVATE_INVERT,
1971 .name = "Invert",
1972 .type = V4L2_CTRL_TYPE_BOOLEAN,
1973 .min = 0,
1974 .max = 1,
1975 .step = 1,
1976 };
1977
1978 static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
1979 .ops = &saa7134_ctrl_ops,
1980 .id = V4L2_CID_PRIVATE_Y_ODD,
1981 .name = "Y Offset Odd Field",
1982 .type = V4L2_CTRL_TYPE_INTEGER,
1983 .min = 0,
1984 .max = 128,
1985 .step = 1,
1986 };
1987
1988 static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
1989 .ops = &saa7134_ctrl_ops,
1990 .id = V4L2_CID_PRIVATE_Y_EVEN,
1991 .name = "Y Offset Even Field",
1992 .type = V4L2_CTRL_TYPE_INTEGER,
1993 .min = 0,
1994 .max = 128,
1995 .step = 1,
1996 };
1997
1998 static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
1999 .ops = &saa7134_ctrl_ops,
2000 .id = V4L2_CID_PRIVATE_AUTOMUTE,
2001 .name = "Automute",
2002 .type = V4L2_CTRL_TYPE_BOOLEAN,
2003 .min = 0,
2004 .max = 1,
2005 .step = 1,
2006 .def = 1,
2007 };
2008
2009 int saa7134_video_init1(struct saa7134_dev *dev)
2010 {
2011 struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
2012 struct vb2_queue *q;
2013 int ret;
2014
2015 /* sanitycheck insmod options */
2016 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2017 gbuffers = 2;
2018 if (gbufsize > gbufsize_max)
2019 gbufsize = gbufsize_max;
2020 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2021
2022 v4l2_ctrl_handler_init(hdl, 11);
2023 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2024 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
2025 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2026 V4L2_CID_CONTRAST, 0, 127, 1, 68);
2027 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2028 V4L2_CID_SATURATION, 0, 127, 1, 64);
2029 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2030 V4L2_CID_HUE, -128, 127, 1, 0);
2031 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2032 V4L2_CID_HFLIP, 0, 1, 1, 0);
2033 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2034 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
2035 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2036 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
2037 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
2038 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
2039 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
2040 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
2041 if (hdl->error)
2042 return hdl->error;
2043 if (card_has_radio(dev)) {
2044 hdl = &dev->radio_ctrl_handler;
2045 v4l2_ctrl_handler_init(hdl, 2);
2046 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
2047 v4l2_ctrl_radio_filter);
2048 if (hdl->error)
2049 return hdl->error;
2050 }
2051 dev->ctl_mute = 1;
2052
2053 if (dev->tda9887_conf && saa7134_ctrl_automute.def)
2054 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2055 dev->automute = 0;
2056
2057 INIT_LIST_HEAD(&dev->video_q.queue);
2058 init_timer(&dev->video_q.timeout);
2059 dev->video_q.timeout.function = saa7134_buffer_timeout;
2060 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2061 dev->video_q.dev = dev;
2062 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
2063 dev->width = 720;
2064 dev->height = 576;
2065 dev->field = V4L2_FIELD_INTERLACED;
2066 dev->win.w.width = dev->width;
2067 dev->win.w.height = dev->height;
2068 dev->win.field = V4L2_FIELD_INTERLACED;
2069 dev->ovbuf.fmt.width = dev->width;
2070 dev->ovbuf.fmt.height = dev->height;
2071 dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
2072 dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
2073
2074 if (saa7134_boards[dev->board].video_out)
2075 saa7134_videoport_init(dev);
2076
2077 q = &dev->video_vbq;
2078 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2079 /*
2080 * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
2081 * engine cannot handle transfers that do not start at the beginning
2082 * of a page. A user-provided pointer can start anywhere in a page, so
2083 * USERPTR support is a no-go unless the application knows about these
2084 * limitations and has special support for this.
2085 */
2086 q->io_modes = VB2_MMAP | VB2_READ;
2087 if (saa7134_userptr)
2088 q->io_modes |= VB2_USERPTR;
2089 q->drv_priv = &dev->video_q;
2090 q->ops = &vb2_qops;
2091 q->gfp_flags = GFP_DMA32;
2092 q->mem_ops = &vb2_dma_sg_memops;
2093 q->buf_struct_size = sizeof(struct saa7134_buf);
2094 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2095 q->lock = &dev->lock;
2096 ret = vb2_queue_init(q);
2097 if (ret)
2098 return ret;
2099 saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
2100
2101 q = &dev->vbi_vbq;
2102 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
2103 /* Don't add VB2_USERPTR, see comment above */
2104 q->io_modes = VB2_MMAP | VB2_READ;
2105 if (saa7134_userptr)
2106 q->io_modes |= VB2_USERPTR;
2107 q->drv_priv = &dev->vbi_q;
2108 q->ops = &saa7134_vbi_qops;
2109 q->gfp_flags = GFP_DMA32;
2110 q->mem_ops = &vb2_dma_sg_memops;
2111 q->buf_struct_size = sizeof(struct saa7134_buf);
2112 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2113 q->lock = &dev->lock;
2114 ret = vb2_queue_init(q);
2115 if (ret)
2116 return ret;
2117 saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
2118
2119 return 0;
2120 }
2121
2122 void saa7134_video_fini(struct saa7134_dev *dev)
2123 {
2124 /* free stuff */
2125 vb2_queue_release(&dev->video_vbq);
2126 saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
2127 vb2_queue_release(&dev->vbi_vbq);
2128 saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
2129 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2130 if (card_has_radio(dev))
2131 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
2132 }
2133
2134 int saa7134_videoport_init(struct saa7134_dev *dev)
2135 {
2136 /* enable video output */
2137 int vo = saa7134_boards[dev->board].video_out;
2138 int video_reg;
2139 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2140
2141 /* Configure videoport */
2142 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2143 video_reg = video_out[vo][1];
2144 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2145 video_reg &= ~VP_T_CODE_P_INVERTED;
2146 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2147 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2148 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2149 video_reg = video_out[vo][5];
2150 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2151 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2152 if (vid_port_opts & SET_CLOCK_INVERTED)
2153 video_reg |= VP_CLK_CTRL1_INVERTED;
2154 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2155 video_reg = video_out[vo][6];
2156 if (vid_port_opts & SET_VSYNC_OFF) {
2157 video_reg &= ~VP_VS_TYPE_MASK;
2158 video_reg |= VP_VS_TYPE_OFF;
2159 }
2160 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2161 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2162 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2163
2164 /* Start videoport */
2165 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2166
2167 return 0;
2168 }
2169
2170 int saa7134_video_init2(struct saa7134_dev *dev)
2171 {
2172 /* init video hw */
2173 set_tvnorm(dev,&tvnorms[0]);
2174 video_mux(dev,0);
2175 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
2176 saa7134_tvaudio_setmute(dev);
2177 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2178 return 0;
2179 }
2180
2181 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2182 {
2183 static const char *st[] = {
2184 "(no signal)", "NTSC", "PAL", "SECAM" };
2185 u32 st1,st2;
2186
2187 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2188 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2189 video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
2190 (st1 & 0x40) ? "not locked" : "locked",
2191 (st2 & 0x40) ? "no" : "yes",
2192 st[st1 & 0x03]);
2193 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
2194
2195 if (dev->nosignal) {
2196 /* no video signal -> mute audio */
2197 if (dev->ctl_automute)
2198 dev->automute = 1;
2199 saa7134_tvaudio_setmute(dev);
2200 } else {
2201 /* wake up tvaudio audio carrier scan thread */
2202 saa7134_tvaudio_do_scan(dev);
2203 }
2204
2205 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2206 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2207 else
2208 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2209
2210 if (dev->mops && dev->mops->signal_change)
2211 dev->mops->signal_change(dev);
2212 }
2213
2214
2215 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2216 {
2217 enum v4l2_field field;
2218
2219 spin_lock(&dev->slock);
2220 if (dev->video_q.curr) {
2221 field = dev->field;
2222 if (V4L2_FIELD_HAS_BOTH(field)) {
2223 /* make sure we have seen both fields */
2224 if ((status & 0x10) == 0x00) {
2225 dev->video_q.curr->top_seen = 1;
2226 goto done;
2227 }
2228 if (!dev->video_q.curr->top_seen)
2229 goto done;
2230 } else if (field == V4L2_FIELD_TOP) {
2231 if ((status & 0x10) != 0x10)
2232 goto done;
2233 } else if (field == V4L2_FIELD_BOTTOM) {
2234 if ((status & 0x10) != 0x00)
2235 goto done;
2236 }
2237 saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
2238 }
2239 saa7134_buffer_next(dev, &dev->video_q);
2240
2241 done:
2242 spin_unlock(&dev->slock);
2243 }
This page took 0.133753 seconds and 4 git commands to generate.