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
format_by_fourcc(unsigned int fourcc)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
set_tvnorm(struct saa7134_dev * dev,struct saa7134_tvnorm * norm)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
video_mux(struct saa7134_dev * dev,int input)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
saa7134_set_decoder(struct saa7134_dev * dev)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
saa7134_set_tvnorm_hw(struct saa7134_dev * dev)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
set_h_prescale(struct saa7134_dev * dev,int task,int prescale)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
set_v_scale(struct saa7134_dev * dev,int task,int yscale)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
set_size(struct saa7134_dev * dev,int task,int width,int height,int interlace)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",
579 prescale, xscale, yscale);
580 set_h_prescale(dev,task,prescale);
581 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
582 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
583 set_v_scale(dev,task,yscale);
584
585 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
586 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
587 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
588 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
589
590 /* deinterlace y offsets */
591 y_odd = dev->ctl_y_odd;
592 y_even = dev->ctl_y_even;
593 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
594 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
595 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
596 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
597 }
598
599 /* ------------------------------------------------------------------ */
600
601 struct cliplist {
602 __u16 position;
603 __u8 enable;
604 __u8 disable;
605 };
606
set_cliplist(struct saa7134_dev * dev,int reg,struct cliplist * cl,int entries,char * name)607 static void set_cliplist(struct saa7134_dev *dev, int reg,
608 struct cliplist *cl, int entries, char *name)
609 {
610 __u8 winbits = 0;
611 int i;
612
613 for (i = 0; i < entries; i++) {
614 winbits |= cl[i].enable;
615 winbits &= ~cl[i].disable;
616 if (i < 15 && cl[i].position == cl[i+1].position)
617 continue;
618 saa_writeb(reg + 0, winbits);
619 saa_writeb(reg + 2, cl[i].position & 0xff);
620 saa_writeb(reg + 3, cl[i].position >> 8);
621 video_dbg("clip: %s winbits=%02x pos=%d\n",
622 name,winbits,cl[i].position);
623 reg += 8;
624 }
625 for (; reg < 0x400; reg += 8) {
626 saa_writeb(reg+ 0, 0);
627 saa_writeb(reg + 1, 0);
628 saa_writeb(reg + 2, 0);
629 saa_writeb(reg + 3, 0);
630 }
631 }
632
clip_range(int val)633 static int clip_range(int val)
634 {
635 if (val < 0)
636 val = 0;
637 return val;
638 }
639
640 /* Sort into smallest position first order */
cliplist_cmp(const void * a,const void * b)641 static int cliplist_cmp(const void *a, const void *b)
642 {
643 const struct cliplist *cla = a;
644 const struct cliplist *clb = b;
645 if (cla->position < clb->position)
646 return -1;
647 if (cla->position > clb->position)
648 return 1;
649 return 0;
650 }
651
setup_clipping(struct saa7134_dev * dev,struct v4l2_clip * clips,int nclips,int interlace)652 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
653 int nclips, int interlace)
654 {
655 struct cliplist col[16], row[16];
656 int cols = 0, rows = 0, i;
657 int div = interlace ? 2 : 1;
658
659 memset(col, 0, sizeof(col));
660 memset(row, 0, sizeof(row));
661 for (i = 0; i < nclips && i < 8; i++) {
662 col[cols].position = clip_range(clips[i].c.left);
663 col[cols].enable = (1 << i);
664 cols++;
665 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
666 col[cols].disable = (1 << i);
667 cols++;
668 row[rows].position = clip_range(clips[i].c.top / div);
669 row[rows].enable = (1 << i);
670 rows++;
671 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
672 / div);
673 row[rows].disable = (1 << i);
674 rows++;
675 }
676 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
677 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
678 set_cliplist(dev,0x380,col,cols,"cols");
679 set_cliplist(dev,0x384,row,rows,"rows");
680 return 0;
681 }
682
verify_preview(struct saa7134_dev * dev,struct v4l2_window * win,bool try)683 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
684 {
685 enum v4l2_field field;
686 int maxw, maxh;
687
688 if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
689 return -EINVAL;
690 if (win->w.width < 48)
691 win->w.width = 48;
692 if (win->w.height < 32)
693 win->w.height = 32;
694 if (win->clipcount > 8)
695 win->clipcount = 8;
696
697 win->chromakey = 0;
698 win->global_alpha = 0;
699 field = win->field;
700 maxw = dev->crop_current.width;
701 maxh = dev->crop_current.height;
702
703 if (V4L2_FIELD_ANY == field) {
704 field = (win->w.height > maxh/2)
705 ? V4L2_FIELD_INTERLACED
706 : V4L2_FIELD_TOP;
707 }
708 switch (field) {
709 case V4L2_FIELD_TOP:
710 case V4L2_FIELD_BOTTOM:
711 maxh = maxh / 2;
712 break;
713 default:
714 field = V4L2_FIELD_INTERLACED;
715 break;
716 }
717
718 win->field = field;
719 if (win->w.width > maxw)
720 win->w.width = maxw;
721 if (win->w.height > maxh)
722 win->w.height = maxh;
723 return 0;
724 }
725
start_preview(struct saa7134_dev * dev)726 static int start_preview(struct saa7134_dev *dev)
727 {
728 unsigned long base,control,bpl;
729 int err;
730
731 err = verify_preview(dev, &dev->win, false);
732 if (0 != err)
733 return err;
734
735 dev->ovfield = dev->win.field;
736 video_dbg("start_preview %dx%d+%d+%d %s field=%s\n",
737 dev->win.w.width, dev->win.w.height,
738 dev->win.w.left, dev->win.w.top,
739 dev->ovfmt->name, v4l2_field_names[dev->ovfield]);
740
741 /* setup window + clipping */
742 set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
743 V4L2_FIELD_HAS_BOTH(dev->ovfield));
744 setup_clipping(dev, dev->clips, dev->nclips,
745 V4L2_FIELD_HAS_BOTH(dev->ovfield));
746 if (dev->ovfmt->yuv)
747 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
748 else
749 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
750 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
751
752 /* dma: setup channel 1 (= Video Task B) */
753 base = (unsigned long)dev->ovbuf.base;
754 base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
755 base += dev->ovfmt->depth/8 * dev->win.w.left;
756 bpl = dev->ovbuf.fmt.bytesperline;
757 control = SAA7134_RS_CONTROL_BURST_16;
758 if (dev->ovfmt->bswap)
759 control |= SAA7134_RS_CONTROL_BSWAP;
760 if (dev->ovfmt->wswap)
761 control |= SAA7134_RS_CONTROL_WSWAP;
762 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
763 saa_writel(SAA7134_RS_BA1(1),base);
764 saa_writel(SAA7134_RS_BA2(1),base+bpl);
765 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
766 saa_writel(SAA7134_RS_CONTROL(1),control);
767 } else {
768 saa_writel(SAA7134_RS_BA1(1),base);
769 saa_writel(SAA7134_RS_BA2(1),base);
770 saa_writel(SAA7134_RS_PITCH(1),bpl);
771 saa_writel(SAA7134_RS_CONTROL(1),control);
772 }
773
774 /* start dma */
775 dev->ovenable = 1;
776 saa7134_set_dmabits(dev);
777
778 return 0;
779 }
780
stop_preview(struct saa7134_dev * dev)781 static int stop_preview(struct saa7134_dev *dev)
782 {
783 dev->ovenable = 0;
784 saa7134_set_dmabits(dev);
785 return 0;
786 }
787
788 /* ------------------------------------------------------------------ */
789
buffer_activate(struct saa7134_dev * dev,struct saa7134_buf * buf,struct saa7134_buf * next)790 static int buffer_activate(struct saa7134_dev *dev,
791 struct saa7134_buf *buf,
792 struct saa7134_buf *next)
793 {
794 struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_buf.vb2_queue->drv_priv;
795 unsigned long base,control,bpl;
796 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
797
798 video_dbg("buffer_activate buf=%p\n", buf);
799 buf->top_seen = 0;
800
801 set_size(dev, TASK_A, dev->width, dev->height,
802 V4L2_FIELD_HAS_BOTH(dev->field));
803 if (dev->fmt->yuv)
804 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
805 else
806 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
807 saa_writeb(SAA7134_OFMT_VIDEO_A, dev->fmt->pm);
808
809 /* DMA: setup channel 0 (= Video Task A0) */
810 base = saa7134_buffer_base(buf);
811 if (dev->fmt->planar)
812 bpl = dev->width;
813 else
814 bpl = (dev->width * dev->fmt->depth) / 8;
815 control = SAA7134_RS_CONTROL_BURST_16 |
816 SAA7134_RS_CONTROL_ME |
817 (dmaq->pt.dma >> 12);
818 if (dev->fmt->bswap)
819 control |= SAA7134_RS_CONTROL_BSWAP;
820 if (dev->fmt->wswap)
821 control |= SAA7134_RS_CONTROL_WSWAP;
822 if (V4L2_FIELD_HAS_BOTH(dev->field)) {
823 /* interlaced */
824 saa_writel(SAA7134_RS_BA1(0),base);
825 saa_writel(SAA7134_RS_BA2(0),base+bpl);
826 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
827 } else {
828 /* non-interlaced */
829 saa_writel(SAA7134_RS_BA1(0),base);
830 saa_writel(SAA7134_RS_BA2(0),base);
831 saa_writel(SAA7134_RS_PITCH(0),bpl);
832 }
833 saa_writel(SAA7134_RS_CONTROL(0),control);
834
835 if (dev->fmt->planar) {
836 /* DMA: setup channel 4+5 (= planar task A) */
837 bpl_uv = bpl >> dev->fmt->hshift;
838 lines_uv = dev->height >> dev->fmt->vshift;
839 base2 = base + bpl * dev->height;
840 base3 = base2 + bpl_uv * lines_uv;
841 if (dev->fmt->uvswap)
842 tmp = base2, base2 = base3, base3 = tmp;
843 video_dbg("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
844 bpl_uv,lines_uv,base2,base3);
845 if (V4L2_FIELD_HAS_BOTH(dev->field)) {
846 /* interlaced */
847 saa_writel(SAA7134_RS_BA1(4),base2);
848 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
849 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
850 saa_writel(SAA7134_RS_BA1(5),base3);
851 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
852 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
853 } else {
854 /* non-interlaced */
855 saa_writel(SAA7134_RS_BA1(4),base2);
856 saa_writel(SAA7134_RS_BA2(4),base2);
857 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
858 saa_writel(SAA7134_RS_BA1(5),base3);
859 saa_writel(SAA7134_RS_BA2(5),base3);
860 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
861 }
862 saa_writel(SAA7134_RS_CONTROL(4),control);
863 saa_writel(SAA7134_RS_CONTROL(5),control);
864 }
865
866 /* start DMA */
867 saa7134_set_dmabits(dev);
868 mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
869 return 0;
870 }
871
buffer_init(struct vb2_buffer * vb2)872 static int buffer_init(struct vb2_buffer *vb2)
873 {
874 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
875 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
876 struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
877
878 dmaq->curr = NULL;
879 buf->activate = buffer_activate;
880 return 0;
881 }
882
buffer_prepare(struct vb2_buffer * vb2)883 static int buffer_prepare(struct vb2_buffer *vb2)
884 {
885 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
886 struct saa7134_dev *dev = dmaq->dev;
887 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
888 struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
889 struct sg_table *dma = vb2_dma_sg_plane_desc(vb2, 0);
890 unsigned int size;
891
892 if (dma->sgl->offset) {
893 pr_err("The buffer is not page-aligned\n");
894 return -EINVAL;
895 }
896 size = (dev->width * dev->height * dev->fmt->depth) >> 3;
897 if (vb2_plane_size(vb2, 0) < size)
898 return -EINVAL;
899
900 vb2_set_plane_payload(vb2, 0, size);
901 vbuf->field = dev->field;
902
903 return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
904 saa7134_buffer_startpage(buf));
905 }
906
queue_setup(struct vb2_queue * q,const void * parg,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],void * alloc_ctxs[])907 static int queue_setup(struct vb2_queue *q, const void *parg,
908 unsigned int *nbuffers, unsigned int *nplanes,
909 unsigned int sizes[], void *alloc_ctxs[])
910 {
911 struct saa7134_dmaqueue *dmaq = q->drv_priv;
912 struct saa7134_dev *dev = dmaq->dev;
913 int size = dev->fmt->depth * dev->width * dev->height >> 3;
914
915 if (dev->width < 48 ||
916 dev->height < 32 ||
917 dev->width/4 > dev->crop_current.width ||
918 dev->height/4 > dev->crop_current.height ||
919 dev->width > dev->crop_bounds.width ||
920 dev->height > dev->crop_bounds.height)
921 return -EINVAL;
922
923 *nbuffers = saa7134_buffer_count(size, *nbuffers);
924 *nplanes = 1;
925 sizes[0] = size;
926 alloc_ctxs[0] = dev->alloc_ctx;
927 return 0;
928 }
929
930 /*
931 * move buffer to hardware queue
932 */
saa7134_vb2_buffer_queue(struct vb2_buffer * vb)933 void saa7134_vb2_buffer_queue(struct vb2_buffer *vb)
934 {
935 struct saa7134_dmaqueue *dmaq = vb->vb2_queue->drv_priv;
936 struct saa7134_dev *dev = dmaq->dev;
937 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
938 struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
939
940 saa7134_buffer_queue(dev, dmaq, buf);
941 }
942 EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue);
943
saa7134_vb2_start_streaming(struct vb2_queue * vq,unsigned int count)944 int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
945 {
946 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
947 struct saa7134_dev *dev = dmaq->dev;
948
949 /*
950 * Planar video capture and TS share the same DMA channel,
951 * so only one can be active at a time.
952 */
953 if (card_is_empress(dev) && vb2_is_busy(&dev->empress_vbq) &&
954 dmaq == &dev->video_q && dev->fmt->planar) {
955 struct saa7134_buf *buf, *tmp;
956
957 list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
958 list_del(&buf->entry);
959 vb2_buffer_done(&buf->vb2.vb2_buf,
960 VB2_BUF_STATE_QUEUED);
961 }
962 if (dmaq->curr) {
963 vb2_buffer_done(&dmaq->curr->vb2.vb2_buf,
964 VB2_BUF_STATE_QUEUED);
965 dmaq->curr = NULL;
966 }
967 return -EBUSY;
968 }
969
970 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
971 * configured conservatively, there's 22 usec of buffering for video.
972 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
973 * margin in case the FIFO is configured differently to the datasheet.
974 * Unfortunately, I lack register-level documentation to check the
975 * Linux FIFO setup and confirm the perfect value.
976 */
977 if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
978 (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
979 pm_qos_add_request(&dev->qos_request,
980 PM_QOS_CPU_DMA_LATENCY, 20);
981 dmaq->seq_nr = 0;
982
983 return 0;
984 }
985
saa7134_vb2_stop_streaming(struct vb2_queue * vq)986 void saa7134_vb2_stop_streaming(struct vb2_queue *vq)
987 {
988 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
989 struct saa7134_dev *dev = dmaq->dev;
990
991 saa7134_stop_streaming(dev, dmaq);
992
993 if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
994 (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
995 pm_qos_remove_request(&dev->qos_request);
996 }
997
998 static struct vb2_ops vb2_qops = {
999 .queue_setup = queue_setup,
1000 .buf_init = buffer_init,
1001 .buf_prepare = buffer_prepare,
1002 .buf_queue = saa7134_vb2_buffer_queue,
1003 .wait_prepare = vb2_ops_wait_prepare,
1004 .wait_finish = vb2_ops_wait_finish,
1005 .start_streaming = saa7134_vb2_start_streaming,
1006 .stop_streaming = saa7134_vb2_stop_streaming,
1007 };
1008
1009 /* ------------------------------------------------------------------ */
1010
saa7134_s_ctrl(struct v4l2_ctrl * ctrl)1011 static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
1012 {
1013 struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
1014 unsigned long flags;
1015 int restart_overlay = 0;
1016
1017 switch (ctrl->id) {
1018 case V4L2_CID_BRIGHTNESS:
1019 dev->ctl_bright = ctrl->val;
1020 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
1021 break;
1022 case V4L2_CID_HUE:
1023 dev->ctl_hue = ctrl->val;
1024 saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
1025 break;
1026 case V4L2_CID_CONTRAST:
1027 dev->ctl_contrast = ctrl->val;
1028 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1029 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1030 break;
1031 case V4L2_CID_SATURATION:
1032 dev->ctl_saturation = ctrl->val;
1033 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1034 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1035 break;
1036 case V4L2_CID_AUDIO_MUTE:
1037 dev->ctl_mute = ctrl->val;
1038 saa7134_tvaudio_setmute(dev);
1039 break;
1040 case V4L2_CID_AUDIO_VOLUME:
1041 dev->ctl_volume = ctrl->val;
1042 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1043 break;
1044 case V4L2_CID_PRIVATE_INVERT:
1045 dev->ctl_invert = ctrl->val;
1046 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1047 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1048 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1049 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1050 break;
1051 case V4L2_CID_HFLIP:
1052 dev->ctl_mirror = ctrl->val;
1053 restart_overlay = 1;
1054 break;
1055 case V4L2_CID_PRIVATE_Y_EVEN:
1056 dev->ctl_y_even = ctrl->val;
1057 restart_overlay = 1;
1058 break;
1059 case V4L2_CID_PRIVATE_Y_ODD:
1060 dev->ctl_y_odd = ctrl->val;
1061 restart_overlay = 1;
1062 break;
1063 case V4L2_CID_PRIVATE_AUTOMUTE:
1064 {
1065 struct v4l2_priv_tun_config tda9887_cfg;
1066
1067 tda9887_cfg.tuner = TUNER_TDA9887;
1068 tda9887_cfg.priv = &dev->tda9887_conf;
1069
1070 dev->ctl_automute = ctrl->val;
1071 if (dev->tda9887_conf) {
1072 if (dev->ctl_automute)
1073 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1074 else
1075 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1076
1077 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1078 }
1079 break;
1080 }
1081 default:
1082 return -EINVAL;
1083 }
1084 if (restart_overlay && dev->overlay_owner) {
1085 spin_lock_irqsave(&dev->slock, flags);
1086 stop_preview(dev);
1087 start_preview(dev);
1088 spin_unlock_irqrestore(&dev->slock, flags);
1089 }
1090 return 0;
1091 }
1092
1093 /* ------------------------------------------------------------------ */
1094
video_open(struct file * file)1095 static int video_open(struct file *file)
1096 {
1097 struct video_device *vdev = video_devdata(file);
1098 struct saa7134_dev *dev = video_drvdata(file);
1099 int ret = v4l2_fh_open(file);
1100
1101 if (ret < 0)
1102 return ret;
1103
1104 mutex_lock(&dev->lock);
1105 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1106 /* switch to radio mode */
1107 saa7134_tvaudio_setinput(dev, &card(dev).radio);
1108 saa_call_all(dev, tuner, s_radio);
1109 } else {
1110 /* switch to video/vbi mode */
1111 video_mux(dev, dev->ctl_input);
1112 }
1113 mutex_unlock(&dev->lock);
1114
1115 return 0;
1116 }
1117
video_release(struct file * file)1118 static int video_release(struct file *file)
1119 {
1120 struct video_device *vdev = video_devdata(file);
1121 struct saa7134_dev *dev = video_drvdata(file);
1122 struct v4l2_fh *fh = file->private_data;
1123 struct saa6588_command cmd;
1124 unsigned long flags;
1125
1126 mutex_lock(&dev->lock);
1127 saa7134_tvaudio_close(dev);
1128
1129 /* turn off overlay */
1130 if (fh == dev->overlay_owner) {
1131 spin_lock_irqsave(&dev->slock,flags);
1132 stop_preview(dev);
1133 spin_unlock_irqrestore(&dev->slock,flags);
1134 dev->overlay_owner = NULL;
1135 }
1136
1137 if (vdev->vfl_type == VFL_TYPE_RADIO)
1138 v4l2_fh_release(file);
1139 else
1140 _vb2_fop_release(file, NULL);
1141
1142 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1143 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1144 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1145 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1146 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1147
1148 saa_call_all(dev, core, s_power, 0);
1149 if (vdev->vfl_type == VFL_TYPE_RADIO)
1150 saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
1151 mutex_unlock(&dev->lock);
1152
1153 return 0;
1154 }
1155
radio_read(struct file * file,char __user * data,size_t count,loff_t * ppos)1156 static ssize_t radio_read(struct file *file, char __user *data,
1157 size_t count, loff_t *ppos)
1158 {
1159 struct saa7134_dev *dev = video_drvdata(file);
1160 struct saa6588_command cmd;
1161
1162 cmd.block_count = count/3;
1163 cmd.nonblocking = file->f_flags & O_NONBLOCK;
1164 cmd.buffer = data;
1165 cmd.instance = file;
1166 cmd.result = -ENODEV;
1167
1168 mutex_lock(&dev->lock);
1169 saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
1170 mutex_unlock(&dev->lock);
1171
1172 return cmd.result;
1173 }
1174
radio_poll(struct file * file,poll_table * wait)1175 static unsigned int radio_poll(struct file *file, poll_table *wait)
1176 {
1177 struct saa7134_dev *dev = video_drvdata(file);
1178 struct saa6588_command cmd;
1179 unsigned int rc = v4l2_ctrl_poll(file, wait);
1180
1181 cmd.instance = file;
1182 cmd.event_list = wait;
1183 cmd.result = 0;
1184 mutex_lock(&dev->lock);
1185 saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
1186 mutex_unlock(&dev->lock);
1187
1188 return rc | cmd.result;
1189 }
1190
1191 /* ------------------------------------------------------------------ */
1192
saa7134_try_get_set_fmt_vbi_cap(struct file * file,void * priv,struct v4l2_format * f)1193 static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1194 struct v4l2_format *f)
1195 {
1196 struct saa7134_dev *dev = video_drvdata(file);
1197 struct saa7134_tvnorm *norm = dev->tvnorm;
1198
1199 memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1200 f->fmt.vbi.sampling_rate = 6750000 * 4;
1201 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1202 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1203 f->fmt.vbi.offset = 64 * 4;
1204 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1205 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1206 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1207 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1208 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1209
1210 return 0;
1211 }
1212
saa7134_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1213 static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1214 struct v4l2_format *f)
1215 {
1216 struct saa7134_dev *dev = video_drvdata(file);
1217
1218 f->fmt.pix.width = dev->width;
1219 f->fmt.pix.height = dev->height;
1220 f->fmt.pix.field = dev->field;
1221 f->fmt.pix.pixelformat = dev->fmt->fourcc;
1222 if (dev->fmt->planar)
1223 f->fmt.pix.bytesperline = f->fmt.pix.width;
1224 else
1225 f->fmt.pix.bytesperline =
1226 (f->fmt.pix.width * dev->fmt->depth) / 8;
1227 f->fmt.pix.sizeimage =
1228 (f->fmt.pix.height * f->fmt.pix.width * dev->fmt->depth) / 8;
1229 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1230 return 0;
1231 }
1232
saa7134_g_fmt_vid_overlay(struct file * file,void * priv,struct v4l2_format * f)1233 static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1234 struct v4l2_format *f)
1235 {
1236 struct saa7134_dev *dev = video_drvdata(file);
1237 struct v4l2_clip __user *clips = f->fmt.win.clips;
1238 u32 clipcount = f->fmt.win.clipcount;
1239 int err = 0;
1240 int i;
1241
1242 if (saa7134_no_overlay > 0) {
1243 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1244 return -EINVAL;
1245 }
1246 f->fmt.win = dev->win;
1247 f->fmt.win.clips = clips;
1248 if (clips == NULL)
1249 clipcount = 0;
1250 if (dev->nclips < clipcount)
1251 clipcount = dev->nclips;
1252 f->fmt.win.clipcount = clipcount;
1253
1254 for (i = 0; !err && i < clipcount; i++) {
1255 if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c,
1256 sizeof(struct v4l2_rect)))
1257 err = -EFAULT;
1258 }
1259
1260 return err;
1261 }
1262
saa7134_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1263 static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1264 struct v4l2_format *f)
1265 {
1266 struct saa7134_dev *dev = video_drvdata(file);
1267 struct saa7134_format *fmt;
1268 enum v4l2_field field;
1269 unsigned int maxw, maxh;
1270
1271 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1272 if (NULL == fmt)
1273 return -EINVAL;
1274
1275 field = f->fmt.pix.field;
1276 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1277 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1278
1279 if (V4L2_FIELD_ANY == field) {
1280 field = (f->fmt.pix.height > maxh/2)
1281 ? V4L2_FIELD_INTERLACED
1282 : V4L2_FIELD_BOTTOM;
1283 }
1284 switch (field) {
1285 case V4L2_FIELD_TOP:
1286 case V4L2_FIELD_BOTTOM:
1287 maxh = maxh / 2;
1288 break;
1289 default:
1290 field = V4L2_FIELD_INTERLACED;
1291 break;
1292 }
1293
1294 f->fmt.pix.field = field;
1295 if (f->fmt.pix.width < 48)
1296 f->fmt.pix.width = 48;
1297 if (f->fmt.pix.height < 32)
1298 f->fmt.pix.height = 32;
1299 if (f->fmt.pix.width > maxw)
1300 f->fmt.pix.width = maxw;
1301 if (f->fmt.pix.height > maxh)
1302 f->fmt.pix.height = maxh;
1303 f->fmt.pix.width &= ~0x03;
1304 if (fmt->planar)
1305 f->fmt.pix.bytesperline = f->fmt.pix.width;
1306 else
1307 f->fmt.pix.bytesperline =
1308 (f->fmt.pix.width * fmt->depth) / 8;
1309 f->fmt.pix.sizeimage =
1310 (f->fmt.pix.height * f->fmt.pix.width * fmt->depth) / 8;
1311 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1312
1313 return 0;
1314 }
1315
saa7134_try_fmt_vid_overlay(struct file * file,void * priv,struct v4l2_format * f)1316 static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1317 struct v4l2_format *f)
1318 {
1319 struct saa7134_dev *dev = video_drvdata(file);
1320
1321 if (saa7134_no_overlay > 0) {
1322 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1323 return -EINVAL;
1324 }
1325
1326 if (f->fmt.win.clips == NULL)
1327 f->fmt.win.clipcount = 0;
1328 return verify_preview(dev, &f->fmt.win, true);
1329 }
1330
saa7134_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1331 static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1332 struct v4l2_format *f)
1333 {
1334 struct saa7134_dev *dev = video_drvdata(file);
1335 int err;
1336
1337 err = saa7134_try_fmt_vid_cap(file, priv, f);
1338 if (0 != err)
1339 return err;
1340
1341 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1342 dev->width = f->fmt.pix.width;
1343 dev->height = f->fmt.pix.height;
1344 dev->field = f->fmt.pix.field;
1345 return 0;
1346 }
1347
saa7134_s_fmt_vid_overlay(struct file * file,void * priv,struct v4l2_format * f)1348 static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1349 struct v4l2_format *f)
1350 {
1351 struct saa7134_dev *dev = video_drvdata(file);
1352 int err;
1353 unsigned long flags;
1354
1355 if (saa7134_no_overlay > 0) {
1356 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1357 return -EINVAL;
1358 }
1359 if (f->fmt.win.clips == NULL)
1360 f->fmt.win.clipcount = 0;
1361 err = verify_preview(dev, &f->fmt.win, true);
1362 if (0 != err)
1363 return err;
1364
1365 dev->win = f->fmt.win;
1366 dev->nclips = f->fmt.win.clipcount;
1367
1368 if (copy_from_user(dev->clips, f->fmt.win.clips,
1369 sizeof(struct v4l2_clip) * dev->nclips))
1370 return -EFAULT;
1371
1372 if (priv == dev->overlay_owner) {
1373 spin_lock_irqsave(&dev->slock, flags);
1374 stop_preview(dev);
1375 start_preview(dev);
1376 spin_unlock_irqrestore(&dev->slock, flags);
1377 }
1378
1379 return 0;
1380 }
1381
saa7134_enum_input(struct file * file,void * priv,struct v4l2_input * i)1382 int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
1383 {
1384 struct saa7134_dev *dev = video_drvdata(file);
1385 unsigned int n;
1386
1387 n = i->index;
1388 if (n >= SAA7134_INPUT_MAX)
1389 return -EINVAL;
1390 if (NULL == card_in(dev, i->index).name)
1391 return -EINVAL;
1392 i->index = n;
1393 i->type = V4L2_INPUT_TYPE_CAMERA;
1394 strcpy(i->name, card_in(dev, n).name);
1395 if (card_in(dev, n).tv)
1396 i->type = V4L2_INPUT_TYPE_TUNER;
1397 if (n == dev->ctl_input) {
1398 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1399 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1400
1401 if (0 != (v1 & 0x40))
1402 i->status |= V4L2_IN_ST_NO_H_LOCK;
1403 if (0 != (v2 & 0x40))
1404 i->status |= V4L2_IN_ST_NO_SIGNAL;
1405 if (0 != (v2 & 0x0e))
1406 i->status |= V4L2_IN_ST_MACROVISION;
1407 }
1408 i->std = SAA7134_NORMS;
1409 return 0;
1410 }
1411 EXPORT_SYMBOL_GPL(saa7134_enum_input);
1412
saa7134_g_input(struct file * file,void * priv,unsigned int * i)1413 int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1414 {
1415 struct saa7134_dev *dev = video_drvdata(file);
1416
1417 *i = dev->ctl_input;
1418 return 0;
1419 }
1420 EXPORT_SYMBOL_GPL(saa7134_g_input);
1421
saa7134_s_input(struct file * file,void * priv,unsigned int i)1422 int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1423 {
1424 struct saa7134_dev *dev = video_drvdata(file);
1425
1426 if (i >= SAA7134_INPUT_MAX)
1427 return -EINVAL;
1428 if (NULL == card_in(dev, i).name)
1429 return -EINVAL;
1430 video_mux(dev, i);
1431 return 0;
1432 }
1433 EXPORT_SYMBOL_GPL(saa7134_s_input);
1434
saa7134_querycap(struct file * file,void * priv,struct v4l2_capability * cap)1435 int saa7134_querycap(struct file *file, void *priv,
1436 struct v4l2_capability *cap)
1437 {
1438 struct saa7134_dev *dev = video_drvdata(file);
1439 struct video_device *vdev = video_devdata(file);
1440 u32 radio_caps, video_caps, vbi_caps;
1441
1442 unsigned int tuner_type = dev->tuner_type;
1443
1444 strcpy(cap->driver, "saa7134");
1445 strlcpy(cap->card, saa7134_boards[dev->board].name,
1446 sizeof(cap->card));
1447 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1448
1449 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1450 if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET))
1451 cap->device_caps |= V4L2_CAP_TUNER;
1452
1453 radio_caps = V4L2_CAP_RADIO;
1454 if (dev->has_rds)
1455 radio_caps |= V4L2_CAP_RDS_CAPTURE;
1456
1457 video_caps = V4L2_CAP_VIDEO_CAPTURE;
1458 if (saa7134_no_overlay <= 0 && !is_empress(file))
1459 video_caps |= V4L2_CAP_VIDEO_OVERLAY;
1460
1461 vbi_caps = V4L2_CAP_VBI_CAPTURE;
1462
1463 switch (vdev->vfl_type) {
1464 case VFL_TYPE_RADIO:
1465 cap->device_caps |= radio_caps;
1466 break;
1467 case VFL_TYPE_GRABBER:
1468 cap->device_caps |= video_caps;
1469 break;
1470 case VFL_TYPE_VBI:
1471 cap->device_caps |= vbi_caps;
1472 break;
1473 }
1474 cap->capabilities = radio_caps | video_caps | vbi_caps |
1475 cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1476 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1477 cap->device_caps &= ~V4L2_CAP_STREAMING;
1478 if (!dev->has_rds)
1479 cap->device_caps &= ~V4L2_CAP_READWRITE;
1480 }
1481
1482 return 0;
1483 }
1484 EXPORT_SYMBOL_GPL(saa7134_querycap);
1485
saa7134_s_std(struct file * file,void * priv,v4l2_std_id id)1486 int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
1487 {
1488 struct saa7134_dev *dev = video_drvdata(file);
1489 struct v4l2_fh *fh = priv;
1490 unsigned long flags;
1491 unsigned int i;
1492 v4l2_std_id fixup;
1493
1494 if (is_empress(file) && dev->overlay_owner) {
1495 /* Don't change the std from the mpeg device
1496 if overlay is active. */
1497 return -EBUSY;
1498 }
1499
1500 for (i = 0; i < TVNORMS; i++)
1501 if (id == tvnorms[i].id)
1502 break;
1503
1504 if (i == TVNORMS)
1505 for (i = 0; i < TVNORMS; i++)
1506 if (id & tvnorms[i].id)
1507 break;
1508 if (i == TVNORMS)
1509 return -EINVAL;
1510
1511 if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1512 if (secam[0] == 'L' || secam[0] == 'l') {
1513 if (secam[1] == 'C' || secam[1] == 'c')
1514 fixup = V4L2_STD_SECAM_LC;
1515 else
1516 fixup = V4L2_STD_SECAM_L;
1517 } else {
1518 if (secam[0] == 'D' || secam[0] == 'd')
1519 fixup = V4L2_STD_SECAM_DK;
1520 else
1521 fixup = V4L2_STD_SECAM;
1522 }
1523 for (i = 0; i < TVNORMS; i++) {
1524 if (fixup == tvnorms[i].id)
1525 break;
1526 }
1527 if (i == TVNORMS)
1528 return -EINVAL;
1529 }
1530
1531 id = tvnorms[i].id;
1532
1533 if (!is_empress(file) && fh == dev->overlay_owner) {
1534 spin_lock_irqsave(&dev->slock, flags);
1535 stop_preview(dev);
1536 spin_unlock_irqrestore(&dev->slock, flags);
1537
1538 set_tvnorm(dev, &tvnorms[i]);
1539
1540 spin_lock_irqsave(&dev->slock, flags);
1541 start_preview(dev);
1542 spin_unlock_irqrestore(&dev->slock, flags);
1543 } else
1544 set_tvnorm(dev, &tvnorms[i]);
1545
1546 saa7134_tvaudio_do_scan(dev);
1547 return 0;
1548 }
1549 EXPORT_SYMBOL_GPL(saa7134_s_std);
1550
saa7134_g_std(struct file * file,void * priv,v4l2_std_id * id)1551 int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1552 {
1553 struct saa7134_dev *dev = video_drvdata(file);
1554
1555 *id = dev->tvnorm->id;
1556 return 0;
1557 }
1558 EXPORT_SYMBOL_GPL(saa7134_g_std);
1559
saa7134_read_std(struct saa7134_dev * dev)1560 static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
1561 {
1562 static v4l2_std_id stds[] = {
1563 V4L2_STD_UNKNOWN,
1564 V4L2_STD_NTSC,
1565 V4L2_STD_PAL,
1566 V4L2_STD_SECAM };
1567
1568 v4l2_std_id result = 0;
1569
1570 u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
1571 u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
1572
1573 if (!(st2 & 0x1)) /* RDCAP == 0 */
1574 result = V4L2_STD_UNKNOWN;
1575 else
1576 result = stds[st1 & 0x03];
1577
1578 return result;
1579 }
1580
saa7134_querystd(struct file * file,void * priv,v4l2_std_id * std)1581 int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
1582 {
1583 struct saa7134_dev *dev = video_drvdata(file);
1584 *std &= saa7134_read_std(dev);
1585 return 0;
1586 }
1587 EXPORT_SYMBOL_GPL(saa7134_querystd);
1588
saa7134_cropcap(struct file * file,void * priv,struct v4l2_cropcap * cap)1589 static int saa7134_cropcap(struct file *file, void *priv,
1590 struct v4l2_cropcap *cap)
1591 {
1592 struct saa7134_dev *dev = video_drvdata(file);
1593
1594 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1595 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1596 return -EINVAL;
1597 cap->bounds = dev->crop_bounds;
1598 cap->defrect = dev->crop_defrect;
1599 cap->pixelaspect.numerator = 1;
1600 cap->pixelaspect.denominator = 1;
1601 if (dev->tvnorm->id & V4L2_STD_525_60) {
1602 cap->pixelaspect.numerator = 11;
1603 cap->pixelaspect.denominator = 10;
1604 }
1605 if (dev->tvnorm->id & V4L2_STD_625_50) {
1606 cap->pixelaspect.numerator = 54;
1607 cap->pixelaspect.denominator = 59;
1608 }
1609 return 0;
1610 }
1611
saa7134_g_crop(struct file * file,void * f,struct v4l2_crop * crop)1612 static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1613 {
1614 struct saa7134_dev *dev = video_drvdata(file);
1615
1616 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1617 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1618 return -EINVAL;
1619 crop->c = dev->crop_current;
1620 return 0;
1621 }
1622
saa7134_s_crop(struct file * file,void * f,const struct v4l2_crop * crop)1623 static int saa7134_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
1624 {
1625 struct saa7134_dev *dev = video_drvdata(file);
1626 struct v4l2_rect *b = &dev->crop_bounds;
1627 struct v4l2_rect *c = &dev->crop_current;
1628
1629 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1630 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1631 return -EINVAL;
1632
1633 if (dev->overlay_owner)
1634 return -EBUSY;
1635 if (vb2_is_streaming(&dev->video_vbq))
1636 return -EBUSY;
1637
1638 *c = crop->c;
1639 if (c->top < b->top)
1640 c->top = b->top;
1641 if (c->top > b->top + b->height)
1642 c->top = b->top + b->height;
1643 if (c->height > b->top - c->top + b->height)
1644 c->height = b->top - c->top + b->height;
1645
1646 if (c->left < b->left)
1647 c->left = b->left;
1648 if (c->left > b->left + b->width)
1649 c->left = b->left + b->width;
1650 if (c->width > b->left - c->left + b->width)
1651 c->width = b->left - c->left + b->width;
1652 return 0;
1653 }
1654
saa7134_g_tuner(struct file * file,void * priv,struct v4l2_tuner * t)1655 int saa7134_g_tuner(struct file *file, void *priv,
1656 struct v4l2_tuner *t)
1657 {
1658 struct saa7134_dev *dev = video_drvdata(file);
1659 int n;
1660
1661 if (0 != t->index)
1662 return -EINVAL;
1663 memset(t, 0, sizeof(*t));
1664 for (n = 0; n < SAA7134_INPUT_MAX; n++) {
1665 if (card_in(dev, n).tv)
1666 break;
1667 }
1668 if (n == SAA7134_INPUT_MAX)
1669 return -EINVAL;
1670 if (NULL != card_in(dev, n).name) {
1671 strcpy(t->name, "Television");
1672 t->type = V4L2_TUNER_ANALOG_TV;
1673 saa_call_all(dev, tuner, g_tuner, t);
1674 t->capability = V4L2_TUNER_CAP_NORM |
1675 V4L2_TUNER_CAP_STEREO |
1676 V4L2_TUNER_CAP_LANG1 |
1677 V4L2_TUNER_CAP_LANG2;
1678 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1679 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1680 }
1681 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1682 t->signal = 0xffff;
1683 return 0;
1684 }
1685 EXPORT_SYMBOL_GPL(saa7134_g_tuner);
1686
saa7134_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * t)1687 int saa7134_s_tuner(struct file *file, void *priv,
1688 const struct v4l2_tuner *t)
1689 {
1690 struct saa7134_dev *dev = video_drvdata(file);
1691 int rx, mode;
1692
1693 if (0 != t->index)
1694 return -EINVAL;
1695
1696 mode = dev->thread.mode;
1697 if (UNSET == mode) {
1698 rx = saa7134_tvaudio_getstereo(dev);
1699 mode = saa7134_tvaudio_rx2mode(rx);
1700 }
1701 if (mode != t->audmode)
1702 dev->thread.mode = t->audmode;
1703
1704 return 0;
1705 }
1706 EXPORT_SYMBOL_GPL(saa7134_s_tuner);
1707
saa7134_g_frequency(struct file * file,void * priv,struct v4l2_frequency * f)1708 int saa7134_g_frequency(struct file *file, void *priv,
1709 struct v4l2_frequency *f)
1710 {
1711 struct saa7134_dev *dev = video_drvdata(file);
1712
1713 if (0 != f->tuner)
1714 return -EINVAL;
1715
1716 saa_call_all(dev, tuner, g_frequency, f);
1717
1718 return 0;
1719 }
1720 EXPORT_SYMBOL_GPL(saa7134_g_frequency);
1721
saa7134_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * f)1722 int saa7134_s_frequency(struct file *file, void *priv,
1723 const struct v4l2_frequency *f)
1724 {
1725 struct saa7134_dev *dev = video_drvdata(file);
1726
1727 if (0 != f->tuner)
1728 return -EINVAL;
1729
1730 saa_call_all(dev, tuner, s_frequency, f);
1731
1732 saa7134_tvaudio_do_scan(dev);
1733 return 0;
1734 }
1735 EXPORT_SYMBOL_GPL(saa7134_s_frequency);
1736
saa7134_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)1737 static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
1738 struct v4l2_fmtdesc *f)
1739 {
1740 if (f->index >= FORMATS)
1741 return -EINVAL;
1742
1743 strlcpy(f->description, formats[f->index].name,
1744 sizeof(f->description));
1745
1746 f->pixelformat = formats[f->index].fourcc;
1747
1748 return 0;
1749 }
1750
saa7134_enum_fmt_vid_overlay(struct file * file,void * priv,struct v4l2_fmtdesc * f)1751 static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
1752 struct v4l2_fmtdesc *f)
1753 {
1754 if (saa7134_no_overlay > 0) {
1755 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1756 return -EINVAL;
1757 }
1758
1759 if ((f->index >= FORMATS) || formats[f->index].planar)
1760 return -EINVAL;
1761
1762 strlcpy(f->description, formats[f->index].name,
1763 sizeof(f->description));
1764
1765 f->pixelformat = formats[f->index].fourcc;
1766
1767 return 0;
1768 }
1769
saa7134_g_fbuf(struct file * file,void * f,struct v4l2_framebuffer * fb)1770 static int saa7134_g_fbuf(struct file *file, void *f,
1771 struct v4l2_framebuffer *fb)
1772 {
1773 struct saa7134_dev *dev = video_drvdata(file);
1774
1775 *fb = dev->ovbuf;
1776 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1777
1778 return 0;
1779 }
1780
saa7134_s_fbuf(struct file * file,void * f,const struct v4l2_framebuffer * fb)1781 static int saa7134_s_fbuf(struct file *file, void *f,
1782 const struct v4l2_framebuffer *fb)
1783 {
1784 struct saa7134_dev *dev = video_drvdata(file);
1785 struct saa7134_format *fmt;
1786
1787 if (!capable(CAP_SYS_ADMIN) &&
1788 !capable(CAP_SYS_RAWIO))
1789 return -EPERM;
1790
1791 /* check args */
1792 fmt = format_by_fourcc(fb->fmt.pixelformat);
1793 if (NULL == fmt)
1794 return -EINVAL;
1795
1796 /* ok, accept it */
1797 dev->ovbuf = *fb;
1798 dev->ovfmt = fmt;
1799 if (0 == dev->ovbuf.fmt.bytesperline)
1800 dev->ovbuf.fmt.bytesperline =
1801 dev->ovbuf.fmt.width*fmt->depth/8;
1802 return 0;
1803 }
1804
saa7134_overlay(struct file * file,void * priv,unsigned int on)1805 static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
1806 {
1807 struct saa7134_dev *dev = video_drvdata(file);
1808 unsigned long flags;
1809
1810 if (on) {
1811 if (saa7134_no_overlay > 0) {
1812 video_dbg("no_overlay\n");
1813 return -EINVAL;
1814 }
1815
1816 if (dev->overlay_owner && priv != dev->overlay_owner)
1817 return -EBUSY;
1818 dev->overlay_owner = priv;
1819 spin_lock_irqsave(&dev->slock, flags);
1820 start_preview(dev);
1821 spin_unlock_irqrestore(&dev->slock, flags);
1822 }
1823 if (!on) {
1824 if (priv != dev->overlay_owner)
1825 return -EINVAL;
1826 spin_lock_irqsave(&dev->slock, flags);
1827 stop_preview(dev);
1828 spin_unlock_irqrestore(&dev->slock, flags);
1829 dev->overlay_owner = NULL;
1830 }
1831 return 0;
1832 }
1833
1834 #ifdef CONFIG_VIDEO_ADV_DEBUG
vidioc_g_register(struct file * file,void * priv,struct v4l2_dbg_register * reg)1835 static int vidioc_g_register (struct file *file, void *priv,
1836 struct v4l2_dbg_register *reg)
1837 {
1838 struct saa7134_dev *dev = video_drvdata(file);
1839
1840 reg->val = saa_readb(reg->reg & 0xffffff);
1841 reg->size = 1;
1842 return 0;
1843 }
1844
vidioc_s_register(struct file * file,void * priv,const struct v4l2_dbg_register * reg)1845 static int vidioc_s_register (struct file *file, void *priv,
1846 const struct v4l2_dbg_register *reg)
1847 {
1848 struct saa7134_dev *dev = video_drvdata(file);
1849
1850 saa_writeb(reg->reg & 0xffffff, reg->val);
1851 return 0;
1852 }
1853 #endif
1854
radio_g_tuner(struct file * file,void * priv,struct v4l2_tuner * t)1855 static int radio_g_tuner(struct file *file, void *priv,
1856 struct v4l2_tuner *t)
1857 {
1858 struct saa7134_dev *dev = video_drvdata(file);
1859
1860 if (0 != t->index)
1861 return -EINVAL;
1862
1863 strcpy(t->name, "Radio");
1864
1865 saa_call_all(dev, tuner, g_tuner, t);
1866 t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
1867 if (dev->input->amux == TV) {
1868 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
1869 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
1870 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
1871 }
1872 return 0;
1873 }
radio_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * t)1874 static int radio_s_tuner(struct file *file, void *priv,
1875 const struct v4l2_tuner *t)
1876 {
1877 struct saa7134_dev *dev = video_drvdata(file);
1878
1879 if (0 != t->index)
1880 return -EINVAL;
1881
1882 saa_call_all(dev, tuner, s_tuner, t);
1883 return 0;
1884 }
1885
1886 static const struct v4l2_file_operations video_fops =
1887 {
1888 .owner = THIS_MODULE,
1889 .open = video_open,
1890 .release = video_release,
1891 .read = vb2_fop_read,
1892 .poll = vb2_fop_poll,
1893 .mmap = vb2_fop_mmap,
1894 .unlocked_ioctl = video_ioctl2,
1895 };
1896
1897 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1898 .vidioc_querycap = saa7134_querycap,
1899 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
1900 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
1901 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
1902 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
1903 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
1904 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
1905 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
1906 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
1907 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1908 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1909 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1910 .vidioc_cropcap = saa7134_cropcap,
1911 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1912 .vidioc_querybuf = vb2_ioctl_querybuf,
1913 .vidioc_qbuf = vb2_ioctl_qbuf,
1914 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1915 .vidioc_s_std = saa7134_s_std,
1916 .vidioc_g_std = saa7134_g_std,
1917 .vidioc_querystd = saa7134_querystd,
1918 .vidioc_enum_input = saa7134_enum_input,
1919 .vidioc_g_input = saa7134_g_input,
1920 .vidioc_s_input = saa7134_s_input,
1921 .vidioc_streamon = vb2_ioctl_streamon,
1922 .vidioc_streamoff = vb2_ioctl_streamoff,
1923 .vidioc_g_tuner = saa7134_g_tuner,
1924 .vidioc_s_tuner = saa7134_s_tuner,
1925 .vidioc_g_crop = saa7134_g_crop,
1926 .vidioc_s_crop = saa7134_s_crop,
1927 .vidioc_g_fbuf = saa7134_g_fbuf,
1928 .vidioc_s_fbuf = saa7134_s_fbuf,
1929 .vidioc_overlay = saa7134_overlay,
1930 .vidioc_g_frequency = saa7134_g_frequency,
1931 .vidioc_s_frequency = saa7134_s_frequency,
1932 #ifdef CONFIG_VIDEO_ADV_DEBUG
1933 .vidioc_g_register = vidioc_g_register,
1934 .vidioc_s_register = vidioc_s_register,
1935 #endif
1936 .vidioc_log_status = v4l2_ctrl_log_status,
1937 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1938 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1939 };
1940
1941 static const struct v4l2_file_operations radio_fops = {
1942 .owner = THIS_MODULE,
1943 .open = video_open,
1944 .read = radio_read,
1945 .release = video_release,
1946 .unlocked_ioctl = video_ioctl2,
1947 .poll = radio_poll,
1948 };
1949
1950 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1951 .vidioc_querycap = saa7134_querycap,
1952 .vidioc_g_tuner = radio_g_tuner,
1953 .vidioc_s_tuner = radio_s_tuner,
1954 .vidioc_g_frequency = saa7134_g_frequency,
1955 .vidioc_s_frequency = saa7134_s_frequency,
1956 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1957 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1958 };
1959
1960 /* ----------------------------------------------------------- */
1961 /* exported stuff */
1962
1963 struct video_device saa7134_video_template = {
1964 .name = "saa7134-video",
1965 .fops = &video_fops,
1966 .ioctl_ops = &video_ioctl_ops,
1967 .tvnorms = SAA7134_NORMS,
1968 };
1969
1970 struct video_device saa7134_radio_template = {
1971 .name = "saa7134-radio",
1972 .fops = &radio_fops,
1973 .ioctl_ops = &radio_ioctl_ops,
1974 };
1975
1976 static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
1977 .s_ctrl = saa7134_s_ctrl,
1978 };
1979
1980 static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
1981 .ops = &saa7134_ctrl_ops,
1982 .id = V4L2_CID_PRIVATE_INVERT,
1983 .name = "Invert",
1984 .type = V4L2_CTRL_TYPE_BOOLEAN,
1985 .min = 0,
1986 .max = 1,
1987 .step = 1,
1988 };
1989
1990 static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
1991 .ops = &saa7134_ctrl_ops,
1992 .id = V4L2_CID_PRIVATE_Y_ODD,
1993 .name = "Y Offset Odd Field",
1994 .type = V4L2_CTRL_TYPE_INTEGER,
1995 .min = 0,
1996 .max = 128,
1997 .step = 1,
1998 };
1999
2000 static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
2001 .ops = &saa7134_ctrl_ops,
2002 .id = V4L2_CID_PRIVATE_Y_EVEN,
2003 .name = "Y Offset Even Field",
2004 .type = V4L2_CTRL_TYPE_INTEGER,
2005 .min = 0,
2006 .max = 128,
2007 .step = 1,
2008 };
2009
2010 static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
2011 .ops = &saa7134_ctrl_ops,
2012 .id = V4L2_CID_PRIVATE_AUTOMUTE,
2013 .name = "Automute",
2014 .type = V4L2_CTRL_TYPE_BOOLEAN,
2015 .min = 0,
2016 .max = 1,
2017 .step = 1,
2018 .def = 1,
2019 };
2020
saa7134_video_init1(struct saa7134_dev * dev)2021 int saa7134_video_init1(struct saa7134_dev *dev)
2022 {
2023 struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
2024 struct vb2_queue *q;
2025 int ret;
2026
2027 /* sanitycheck insmod options */
2028 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2029 gbuffers = 2;
2030 if (gbufsize > gbufsize_max)
2031 gbufsize = gbufsize_max;
2032 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2033
2034 v4l2_ctrl_handler_init(hdl, 11);
2035 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2036 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
2037 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2038 V4L2_CID_CONTRAST, 0, 127, 1, 68);
2039 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2040 V4L2_CID_SATURATION, 0, 127, 1, 64);
2041 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2042 V4L2_CID_HUE, -128, 127, 1, 0);
2043 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2044 V4L2_CID_HFLIP, 0, 1, 1, 0);
2045 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2046 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
2047 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2048 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
2049 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
2050 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
2051 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
2052 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
2053 if (hdl->error)
2054 return hdl->error;
2055 if (card_has_radio(dev)) {
2056 hdl = &dev->radio_ctrl_handler;
2057 v4l2_ctrl_handler_init(hdl, 2);
2058 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
2059 v4l2_ctrl_radio_filter);
2060 if (hdl->error)
2061 return hdl->error;
2062 }
2063 dev->ctl_mute = 1;
2064
2065 if (dev->tda9887_conf && saa7134_ctrl_automute.def)
2066 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2067 dev->automute = 0;
2068
2069 INIT_LIST_HEAD(&dev->video_q.queue);
2070 init_timer(&dev->video_q.timeout);
2071 dev->video_q.timeout.function = saa7134_buffer_timeout;
2072 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2073 dev->video_q.dev = dev;
2074 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
2075 dev->width = 720;
2076 dev->height = 576;
2077 dev->field = V4L2_FIELD_INTERLACED;
2078 dev->win.w.width = dev->width;
2079 dev->win.w.height = dev->height;
2080 dev->win.field = V4L2_FIELD_INTERLACED;
2081 dev->ovbuf.fmt.width = dev->width;
2082 dev->ovbuf.fmt.height = dev->height;
2083 dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
2084 dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
2085
2086 if (saa7134_boards[dev->board].video_out)
2087 saa7134_videoport_init(dev);
2088
2089 q = &dev->video_vbq;
2090 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2091 /*
2092 * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
2093 * engine cannot handle transfers that do not start at the beginning
2094 * of a page. A user-provided pointer can start anywhere in a page, so
2095 * USERPTR support is a no-go unless the application knows about these
2096 * limitations and has special support for this.
2097 */
2098 q->io_modes = VB2_MMAP | VB2_READ;
2099 if (saa7134_userptr)
2100 q->io_modes |= VB2_USERPTR;
2101 q->drv_priv = &dev->video_q;
2102 q->ops = &vb2_qops;
2103 q->gfp_flags = GFP_DMA32;
2104 q->mem_ops = &vb2_dma_sg_memops;
2105 q->buf_struct_size = sizeof(struct saa7134_buf);
2106 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2107 q->lock = &dev->lock;
2108 ret = vb2_queue_init(q);
2109 if (ret)
2110 return ret;
2111 saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
2112
2113 q = &dev->vbi_vbq;
2114 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
2115 /* Don't add VB2_USERPTR, see comment above */
2116 q->io_modes = VB2_MMAP | VB2_READ;
2117 if (saa7134_userptr)
2118 q->io_modes |= VB2_USERPTR;
2119 q->drv_priv = &dev->vbi_q;
2120 q->ops = &saa7134_vbi_qops;
2121 q->gfp_flags = GFP_DMA32;
2122 q->mem_ops = &vb2_dma_sg_memops;
2123 q->buf_struct_size = sizeof(struct saa7134_buf);
2124 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2125 q->lock = &dev->lock;
2126 ret = vb2_queue_init(q);
2127 if (ret)
2128 return ret;
2129 saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
2130
2131 return 0;
2132 }
2133
saa7134_video_fini(struct saa7134_dev * dev)2134 void saa7134_video_fini(struct saa7134_dev *dev)
2135 {
2136 /* free stuff */
2137 vb2_queue_release(&dev->video_vbq);
2138 saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
2139 vb2_queue_release(&dev->vbi_vbq);
2140 saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
2141 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2142 if (card_has_radio(dev))
2143 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
2144 }
2145
saa7134_videoport_init(struct saa7134_dev * dev)2146 int saa7134_videoport_init(struct saa7134_dev *dev)
2147 {
2148 /* enable video output */
2149 int vo = saa7134_boards[dev->board].video_out;
2150 int video_reg;
2151 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2152
2153 /* Configure videoport */
2154 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2155 video_reg = video_out[vo][1];
2156 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2157 video_reg &= ~VP_T_CODE_P_INVERTED;
2158 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2159 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2160 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2161 video_reg = video_out[vo][5];
2162 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2163 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2164 if (vid_port_opts & SET_CLOCK_INVERTED)
2165 video_reg |= VP_CLK_CTRL1_INVERTED;
2166 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2167 video_reg = video_out[vo][6];
2168 if (vid_port_opts & SET_VSYNC_OFF) {
2169 video_reg &= ~VP_VS_TYPE_MASK;
2170 video_reg |= VP_VS_TYPE_OFF;
2171 }
2172 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2173 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2174 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2175
2176 /* Start videoport */
2177 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2178
2179 return 0;
2180 }
2181
saa7134_video_init2(struct saa7134_dev * dev)2182 int saa7134_video_init2(struct saa7134_dev *dev)
2183 {
2184 /* init video hw */
2185 set_tvnorm(dev,&tvnorms[0]);
2186 video_mux(dev,0);
2187 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
2188 saa7134_tvaudio_setmute(dev);
2189 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2190 return 0;
2191 }
2192
saa7134_irq_video_signalchange(struct saa7134_dev * dev)2193 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2194 {
2195 static const char *st[] = {
2196 "(no signal)", "NTSC", "PAL", "SECAM" };
2197 u32 st1,st2;
2198
2199 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2200 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2201 video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
2202 (st1 & 0x40) ? "not locked" : "locked",
2203 (st2 & 0x40) ? "no" : "yes",
2204 st[st1 & 0x03]);
2205 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
2206
2207 if (dev->nosignal) {
2208 /* no video signal -> mute audio */
2209 if (dev->ctl_automute)
2210 dev->automute = 1;
2211 saa7134_tvaudio_setmute(dev);
2212 } else {
2213 /* wake up tvaudio audio carrier scan thread */
2214 saa7134_tvaudio_do_scan(dev);
2215 }
2216
2217 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2218 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2219 else
2220 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2221
2222 if (dev->mops && dev->mops->signal_change)
2223 dev->mops->signal_change(dev);
2224 }
2225
2226
saa7134_irq_video_done(struct saa7134_dev * dev,unsigned long status)2227 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2228 {
2229 enum v4l2_field field;
2230
2231 spin_lock(&dev->slock);
2232 if (dev->video_q.curr) {
2233 field = dev->field;
2234 if (V4L2_FIELD_HAS_BOTH(field)) {
2235 /* make sure we have seen both fields */
2236 if ((status & 0x10) == 0x00) {
2237 dev->video_q.curr->top_seen = 1;
2238 goto done;
2239 }
2240 if (!dev->video_q.curr->top_seen)
2241 goto done;
2242 } else if (field == V4L2_FIELD_TOP) {
2243 if ((status & 0x10) != 0x10)
2244 goto done;
2245 } else if (field == V4L2_FIELD_BOTTOM) {
2246 if ((status & 0x10) != 0x00)
2247 goto done;
2248 }
2249 saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
2250 }
2251 saa7134_buffer_next(dev, &dev->video_q);
2252
2253 done:
2254 spin_unlock(&dev->slock);
2255 }
2256