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