blob: 965ade72f5c5d2f7f17e6d8e416d1a7d594a6278 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
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
Mauro Carvalho Chehab9a12ccf2015-04-30 06:44:56 -030023#include "saa7134.h"
24#include "saa7134-reg.h"
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
27#include <linux/list.h>
28#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
30#include <linux/slab.h>
Heikki Orsila9040b322007-04-27 12:31:18 -030031#include <linux/sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Hans Verkuila2004502013-12-14 08:28:28 -030033#include <media/v4l2-common.h>
34#include <media/v4l2-event.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020035#include <media/i2c/saa6588.h>
Hans Verkuila2004502013-12-14 08:28:28 -030036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/* ------------------------------------------------------------------ */
38
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -030039unsigned int video_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static unsigned int gbuffers = 8;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030041static unsigned int noninterlaced; /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static unsigned int gbufsize = 720*576*4;
43static unsigned int gbufsize_max = 720*576*4;
Hartmut Hackmann17b10a72006-10-02 19:55:07 -030044static char secam[] = "--";
Linus Torvalds1da177e2005-04-16 15:20:36 -070045module_param(video_debug, int, 0644);
46MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
47module_param(gbuffers, int, 0444);
48MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
49module_param(noninterlaced, int, 0644);
Hartmut Hackmannd5fdd132006-07-15 09:45:34 -030050MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
Hartmut Hackmann17b10a72006-10-02 19:55:07 -030051module_param_string(secam, secam, sizeof(secam), 0644);
52MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Mauro Carvalho Chehab45f38cb2015-05-13 14:09:25 -030055#define video_dbg(fmt, arg...) do { \
56 if (video_debug & 0x04) \
57 printk(KERN_DEBUG pr_fmt("video: " fmt), ## arg); \
58 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/* ------------------------------------------------------------------ */
Robert W. Boone2f8d4f52005-11-08 21:37:10 -080061/* 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/* ------------------------------------------------------------------ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* data structs for video */
99
100static int video_out[][9] = {
101 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
102};
103
104static 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, \
Michael Schimekf246a812005-06-23 22:04:47 -0700205 .vbi_v_start_0 = 7, \
Hans Verkuil033d0082014-09-21 06:38:55 -0300206 .vbi_v_stop_0 = 23, \
Michael Schimekf246a812005-06-23 22:04:47 -0700207 .vbi_v_start_1 = 319, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .src_timing = 4
209
210#define NORM_525_60 \
211 .h_start = 0, \
Sean Youngdd6ed862010-02-06 10:58:41 -0300212 .h_stop = 719, \
Michael Schimekf246a812005-06-23 22:04:47 -0700213 .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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220static 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
Hartmut Hackmann17b10a72006-10-02 19:55:07 -0300286 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .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,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800359 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/* ------------------------------------------------------------------ */
387
Adrian Bunkb02044d2007-10-24 13:23:14 -0300388static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Mauro Carvalho Chehab6139ebc2015-05-13 14:09:42 -0300390 video_dbg("set tv norm = %s\n", norm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 dev->tvnorm = norm;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* 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
Michael Schimekf246a812005-06-23 22:04:47 -0700399 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 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
Maxim Levitskyc4584732007-10-12 00:57:15 -0300407 saa7134_set_tvnorm_hw(dev);
Maxim Levitskycb712012007-09-27 20:34:25 -0300408}
409
410static void video_mux(struct saa7134_dev *dev, int input)
411{
Mauro Carvalho Chehab8fc34862016-02-05 10:24:37 -0200412 video_dbg("video input = %d [%s]\n",
413 input, saa7134_input_name[card_in(dev, input).type]);
Maxim Levitskycb712012007-09-27 20:34:25 -0300414 dev->ctl_input = input;
415 set_tvnorm(dev, dev->tvnorm);
416 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
417}
418
Maxim Levitskyc4584732007-10-12 00:57:15 -0300419
420static void saa7134_set_decoder(struct saa7134_dev *dev)
Maxim Levitskycb712012007-09-27 20:34:25 -0300421{
Mikhail Domrachev707b7f82014-04-01 09:28:17 -0300422 int luma_control, sync_control, chroma_ctrl1, mux;
Maxim Levitskycb712012007-09-27 20:34:25 -0300423
424 struct saa7134_tvnorm *norm = dev->tvnorm;
425 mux = card_in(dev, dev->ctl_input).vmux;
426
427 luma_control = norm->luma_control;
428 sync_control = norm->sync_control;
Mikhail Domrachev707b7f82014-04-01 09:28:17 -0300429 chroma_ctrl1 = norm->chroma_ctrl1;
Maxim Levitskycb712012007-09-27 20:34:25 -0300430
431 if (mux > 5)
432 luma_control |= 0x80; /* svideo */
433 if (noninterlaced || dev->nosignal)
434 sync_control |= 0x20;
435
Mikhail Domrachev707b7f82014-04-01 09:28:17 -0300436 /* switch on auto standard detection */
437 sync_control |= SAA7134_SYNC_CTRL_AUFD;
438 chroma_ctrl1 |= SAA7134_CHROMA_CTRL1_AUTO0;
439 chroma_ctrl1 &= ~SAA7134_CHROMA_CTRL1_FCTC;
440 luma_control &= ~SAA7134_LUMA_CTRL_LDEL;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* setup video decoder */
443 saa_writeb(SAA7134_INCR_DELAY, 0x08);
444 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
445 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
446
447 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
448 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
449 saa_writeb(SAA7134_HSYNC_START, 0xeb);
450 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
451 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
452
453 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
454 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
455 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Maxim Levitskyf5a1ac62007-09-27 20:34:20 -0300457 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
458 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
459
460 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
461 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
Mikhail Domrachev707b7f82014-04-01 09:28:17 -0300464 saa_writeb(SAA7134_CHROMA_CTRL1, chroma_ctrl1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
466
467 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
468 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
469
470 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
471 saa_writeb(SAA7134_VGATE_START, 0x11);
472 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
473 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
474 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
475 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Maxim Levitskyc4584732007-10-12 00:57:15 -0300478void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
479{
480 saa7134_set_decoder(dev);
481
Mauro Carvalho Chehab568b2fe2016-02-05 11:14:05 -0200482 saa_call_all(dev, video, s_std, dev->tvnorm->id);
Hans Verkuil9afb7372008-09-06 06:34:44 -0300483 /* Set the correct norm for the saa6752hs. This function
484 does nothing if there is no saa6752hs. */
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300485 saa_call_empress(dev, video, s_std, dev->tvnorm->id);
Maxim Levitskyc4584732007-10-12 00:57:15 -0300486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static 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
526static 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 */
Mauro Carvalho Chehab6139ebc2015-05-13 14:09:42 -0300536 video_dbg("yscale LPI yscale=%d\n", yscale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 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;
Mauro Carvalho Chehab6139ebc2015-05-13 14:09:42 -0300543 video_dbg("yscale ACM yscale=%d val=0x%x\n", yscale, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 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
551static 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;
Mauro Carvalho Chehab6139ebc2015-05-13 14:09:42 -0300578 video_dbg("prescale=%d xscale=%d yscale=%d\n",
579 prescale, xscale, yscale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 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
601struct cliplist {
602 __u16 position;
603 __u8 enable;
604 __u8 disable;
605};
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607static 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);
Mauro Carvalho Chehab630983b2015-04-30 08:52:35 -0300621 video_dbg("clip: %s winbits=%02x pos=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 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
633static int clip_range(int val)
634{
635 if (val < 0)
636 val = 0;
637 return val;
638}
639
Heikki Orsila9040b322007-04-27 12:31:18 -0300640/* Sort into smallest position first order */
641static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
653 int nclips, int interlace)
654{
655 struct cliplist col[16], row[16];
Heikki Orsila9040b322007-04-27 12:31:18 -0300656 int cols = 0, rows = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int div = interlace ? 2 : 1;
658
Heikki Orsila9040b322007-04-27 12:31:18 -0300659 memset(col, 0, sizeof(col));
660 memset(row, 0, sizeof(row));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 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 }
Heikki Orsila9040b322007-04-27 12:31:18 -0300676 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
677 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 set_cliplist(dev,0x380,col,cols,"cols");
679 set_cliplist(dev,0x384,row,rows,"rows");
680 return 0;
681}
682
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300683static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 enum v4l2_field field;
686 int maxw, maxh;
687
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300688 if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return -EINVAL;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300690 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300697 win->chromakey = 0;
698 win->global_alpha = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 field = win->field;
700 maxw = dev->crop_current.width;
701 maxh = dev->crop_current.height;
702
703 if (V4L2_FIELD_ANY == field) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800704 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;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800713 default:
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300714 field = V4L2_FIELD_INTERLACED;
715 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
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
Hans Verkuil718bde12013-12-14 08:28:24 -0300726static int start_preview(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 unsigned long base,control,bpl;
729 int err;
730
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300731 err = verify_preview(dev, &dev->win, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (0 != err)
733 return err;
734
Hans Verkuilb12262f2013-05-31 08:22:24 -0300735 dev->ovfield = dev->win.field;
Mauro Carvalho Chehab630983b2015-04-30 08:52:35 -0300736 video_dbg("start_preview %dx%d+%d+%d %s field=%s\n",
Hans Verkuilb12262f2013-05-31 08:22:24 -0300737 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /* setup window + clipping */
Hans Verkuilb12262f2013-05-31 08:22:24 -0300742 set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 V4L2_FIELD_HAS_BOTH(dev->ovfield));
Hans Verkuilb12262f2013-05-31 08:22:24 -0300744 setup_clipping(dev, dev->clips, dev->nclips,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 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;
Hans Verkuilb12262f2013-05-31 08:22:24 -0300754 base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
755 base += dev->ovfmt->depth/8 * dev->win.w.left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 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
Hans Verkuil718bde12013-12-14 08:28:24 -0300781static int stop_preview(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 dev->ovenable = 0;
784 saa7134_set_dmabits(dev);
785 return 0;
786}
787
Mauro Carvalho Chehabac90aa02016-02-05 08:33:04 -0200788/*
789 * Media Controller helper functions
790 */
791
792static int saa7134_enable_analog_tuner(struct saa7134_dev *dev)
793{
794#ifdef CONFIG_MEDIA_CONTROLLER
795 struct media_device *mdev = dev->media_dev;
796 struct media_entity *source;
797 struct media_link *link, *found_link = NULL;
798 int ret, active_links = 0;
799
800 if (!mdev || !dev->decoder)
801 return 0;
802
803 /*
804 * This will find the tuner that is connected into the decoder.
805 * Technically, this is not 100% correct, as the device may be
806 * using an analog input instead of the tuner. However, as we can't
807 * do DVB streaming while the DMA engine is being used for V4L2,
808 * this should be enough for the actual needs.
809 */
810 list_for_each_entry(link, &dev->decoder->links, list) {
811 if (link->sink->entity == dev->decoder) {
812 found_link = link;
813 if (link->flags & MEDIA_LNK_FL_ENABLED)
814 active_links++;
815 break;
816 }
817 }
818
819 if (active_links == 1 || !found_link)
820 return 0;
821
822 source = found_link->source->entity;
823 list_for_each_entry(link, &source->links, list) {
824 struct media_entity *sink;
825 int flags = 0;
826
827 sink = link->sink->entity;
828
829 if (sink == dev->decoder)
830 flags = MEDIA_LNK_FL_ENABLED;
831
832 ret = media_entity_setup_link(link, flags);
833 if (ret) {
834 pr_err("Couldn't change link %s->%s to %s. Error %d\n",
835 source->name, sink->name,
836 flags ? "enabled" : "disabled",
837 ret);
838 return ret;
839 }
840 }
841#endif
842 return 0;
843}
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845/* ------------------------------------------------------------------ */
846
847static int buffer_activate(struct saa7134_dev *dev,
848 struct saa7134_buf *buf,
849 struct saa7134_buf *next)
850{
Junghak Sung2d700712015-09-22 10:30:30 -0300851 struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_buf.vb2_queue->drv_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 unsigned long base,control,bpl;
853 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
854
Mauro Carvalho Chehab6139ebc2015-05-13 14:09:42 -0300855 video_dbg("buffer_activate buf=%p\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 buf->top_seen = 0;
857
Hans Verkuil2ada8152014-04-17 07:30:53 -0300858 set_size(dev, TASK_A, dev->width, dev->height,
859 V4L2_FIELD_HAS_BOTH(dev->field));
Hans Verkuil9e534f82014-04-17 05:00:39 -0300860 if (dev->fmt->yuv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
862 else
863 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
Hans Verkuil9e534f82014-04-17 05:00:39 -0300864 saa_writeb(SAA7134_OFMT_VIDEO_A, dev->fmt->pm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 /* DMA: setup channel 0 (= Video Task A0) */
867 base = saa7134_buffer_base(buf);
Hans Verkuil9e534f82014-04-17 05:00:39 -0300868 if (dev->fmt->planar)
Hans Verkuil2ada8152014-04-17 07:30:53 -0300869 bpl = dev->width;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 else
Hans Verkuil2ada8152014-04-17 07:30:53 -0300871 bpl = (dev->width * dev->fmt->depth) / 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 control = SAA7134_RS_CONTROL_BURST_16 |
873 SAA7134_RS_CONTROL_ME |
Hans Verkuil2ada8152014-04-17 07:30:53 -0300874 (dmaq->pt.dma >> 12);
Hans Verkuil9e534f82014-04-17 05:00:39 -0300875 if (dev->fmt->bswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 control |= SAA7134_RS_CONTROL_BSWAP;
Hans Verkuil9e534f82014-04-17 05:00:39 -0300877 if (dev->fmt->wswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 control |= SAA7134_RS_CONTROL_WSWAP;
Hans Verkuil2ada8152014-04-17 07:30:53 -0300879 if (V4L2_FIELD_HAS_BOTH(dev->field)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* interlaced */
881 saa_writel(SAA7134_RS_BA1(0),base);
882 saa_writel(SAA7134_RS_BA2(0),base+bpl);
883 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
884 } else {
885 /* non-interlaced */
886 saa_writel(SAA7134_RS_BA1(0),base);
887 saa_writel(SAA7134_RS_BA2(0),base);
888 saa_writel(SAA7134_RS_PITCH(0),bpl);
889 }
890 saa_writel(SAA7134_RS_CONTROL(0),control);
891
Hans Verkuil9e534f82014-04-17 05:00:39 -0300892 if (dev->fmt->planar) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* DMA: setup channel 4+5 (= planar task A) */
Hans Verkuil9e534f82014-04-17 05:00:39 -0300894 bpl_uv = bpl >> dev->fmt->hshift;
Hans Verkuil2ada8152014-04-17 07:30:53 -0300895 lines_uv = dev->height >> dev->fmt->vshift;
896 base2 = base + bpl * dev->height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 base3 = base2 + bpl_uv * lines_uv;
Hans Verkuil9e534f82014-04-17 05:00:39 -0300898 if (dev->fmt->uvswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 tmp = base2, base2 = base3, base3 = tmp;
Mauro Carvalho Chehab630983b2015-04-30 08:52:35 -0300900 video_dbg("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 bpl_uv,lines_uv,base2,base3);
Hans Verkuil2ada8152014-04-17 07:30:53 -0300902 if (V4L2_FIELD_HAS_BOTH(dev->field)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /* interlaced */
904 saa_writel(SAA7134_RS_BA1(4),base2);
905 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
906 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
907 saa_writel(SAA7134_RS_BA1(5),base3);
908 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
909 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
910 } else {
911 /* non-interlaced */
912 saa_writel(SAA7134_RS_BA1(4),base2);
913 saa_writel(SAA7134_RS_BA2(4),base2);
914 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
915 saa_writel(SAA7134_RS_BA1(5),base3);
916 saa_writel(SAA7134_RS_BA2(5),base3);
917 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
918 }
919 saa_writel(SAA7134_RS_CONTROL(4),control);
920 saa_writel(SAA7134_RS_CONTROL(5),control);
921 }
922
923 /* start DMA */
924 saa7134_set_dmabits(dev);
Hans Verkuil2ada8152014-04-17 07:30:53 -0300925 mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return 0;
927}
928
Hans Verkuil2ada8152014-04-17 07:30:53 -0300929static int buffer_init(struct vb2_buffer *vb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Hans Verkuil2ada8152014-04-17 07:30:53 -0300931 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
Junghak Sung2d700712015-09-22 10:30:30 -0300932 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
933 struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Hans Verkuil2ada8152014-04-17 07:30:53 -0300935 dmaq->curr = NULL;
936 buf->activate = buffer_activate;
937 return 0;
938}
939
940static int buffer_prepare(struct vb2_buffer *vb2)
941{
942 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
943 struct saa7134_dev *dev = dmaq->dev;
Junghak Sung2d700712015-09-22 10:30:30 -0300944 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
945 struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
946 struct sg_table *dma = vb2_dma_sg_plane_desc(vb2, 0);
Hans Verkuil2ada8152014-04-17 07:30:53 -0300947 unsigned int size;
Hans Verkuil2ada8152014-04-17 07:30:53 -0300948
Hans Verkuila3f415a2014-04-17 06:44:31 -0300949 if (dma->sgl->offset) {
950 pr_err("The buffer is not page-aligned\n");
951 return -EINVAL;
952 }
Hans Verkuil2ada8152014-04-17 07:30:53 -0300953 size = (dev->width * dev->height * dev->fmt->depth) >> 3;
954 if (vb2_plane_size(vb2, 0) < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -EINVAL;
Hans Verkuil2ada8152014-04-17 07:30:53 -0300956
957 vb2_set_plane_payload(vb2, 0, size);
Junghak Sung2d700712015-09-22 10:30:30 -0300958 vbuf->field = dev->field;
Hans Verkuil2ada8152014-04-17 07:30:53 -0300959
Hans Verkuil2ada8152014-04-17 07:30:53 -0300960 return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
961 saa7134_buffer_startpage(buf));
962}
963
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200964static int queue_setup(struct vb2_queue *q,
Hans Verkuil2ada8152014-04-17 07:30:53 -0300965 unsigned int *nbuffers, unsigned int *nplanes,
966 unsigned int sizes[], void *alloc_ctxs[])
967{
968 struct saa7134_dmaqueue *dmaq = q->drv_priv;
969 struct saa7134_dev *dev = dmaq->dev;
970 int size = dev->fmt->depth * dev->width * dev->height >> 3;
971
Hans Verkuil813b9df2013-05-31 08:30:49 -0300972 if (dev->width < 48 ||
973 dev->height < 32 ||
974 dev->width/4 > dev->crop_current.width ||
975 dev->height/4 > dev->crop_current.height ||
976 dev->width > dev->crop_bounds.width ||
977 dev->height > dev->crop_bounds.height)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Hans Verkuil2ada8152014-04-17 07:30:53 -0300980 *nbuffers = saa7134_buffer_count(size, *nbuffers);
981 *nplanes = 1;
982 sizes[0] = size;
Mauro Carvalho Chehabac90aa02016-02-05 08:33:04 -0200983
984 saa7134_enable_analog_tuner(dev);
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return 0;
987}
988
Hans Verkuil2ada8152014-04-17 07:30:53 -0300989/*
990 * move buffer to hardware queue
991 */
992void saa7134_vb2_buffer_queue(struct vb2_buffer *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Hans Verkuil2ada8152014-04-17 07:30:53 -0300994 struct saa7134_dmaqueue *dmaq = vb->vb2_queue->drv_priv;
Hans Verkuila00e6882014-04-17 06:06:06 -0300995 struct saa7134_dev *dev = dmaq->dev;
Junghak Sung2d700712015-09-22 10:30:30 -0300996 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
997 struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Hans Verkuil2ada8152014-04-17 07:30:53 -0300999 saa7134_buffer_queue(dev, dmaq, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
Hans Verkuil2ada8152014-04-17 07:30:53 -03001001EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Hans Verkuil2ada8152014-04-17 07:30:53 -03001003int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Hans Verkuil2ada8152014-04-17 07:30:53 -03001005 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
1006 struct saa7134_dev *dev = dmaq->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Hans Verkuil2ada8152014-04-17 07:30:53 -03001008 /*
1009 * Planar video capture and TS share the same DMA channel,
1010 * so only one can be active at a time.
1011 */
1012 if (card_is_empress(dev) && vb2_is_busy(&dev->empress_vbq) &&
1013 dmaq == &dev->video_q && dev->fmt->planar) {
1014 struct saa7134_buf *buf, *tmp;
1015
1016 list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
1017 list_del(&buf->entry);
Junghak Sung2d700712015-09-22 10:30:30 -03001018 vb2_buffer_done(&buf->vb2.vb2_buf,
1019 VB2_BUF_STATE_QUEUED);
Hans Verkuil2ada8152014-04-17 07:30:53 -03001020 }
1021 if (dmaq->curr) {
Junghak Sung2d700712015-09-22 10:30:30 -03001022 vb2_buffer_done(&dmaq->curr->vb2.vb2_buf,
1023 VB2_BUF_STATE_QUEUED);
Hans Verkuil2ada8152014-04-17 07:30:53 -03001024 dmaq->curr = NULL;
1025 }
1026 return -EBUSY;
1027 }
1028
1029 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
1030 * configured conservatively, there's 22 usec of buffering for video.
1031 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
1032 * margin in case the FIFO is configured differently to the datasheet.
1033 * Unfortunately, I lack register-level documentation to check the
1034 * Linux FIFO setup and confirm the perfect value.
1035 */
1036 if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
1037 (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
1038 pm_qos_add_request(&dev->qos_request,
1039 PM_QOS_CPU_DMA_LATENCY, 20);
1040 dmaq->seq_nr = 0;
1041
1042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
Hans Verkuil2ada8152014-04-17 07:30:53 -03001045void saa7134_vb2_stop_streaming(struct vb2_queue *vq)
1046{
1047 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
1048 struct saa7134_dev *dev = dmaq->dev;
1049
1050 saa7134_stop_streaming(dev, dmaq);
1051
1052 if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
1053 (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
1054 pm_qos_remove_request(&dev->qos_request);
1055}
1056
1057static struct vb2_ops vb2_qops = {
1058 .queue_setup = queue_setup,
1059 .buf_init = buffer_init,
1060 .buf_prepare = buffer_prepare,
Hans Verkuil2ada8152014-04-17 07:30:53 -03001061 .buf_queue = saa7134_vb2_buffer_queue,
1062 .wait_prepare = vb2_ops_wait_prepare,
1063 .wait_finish = vb2_ops_wait_finish,
1064 .start_streaming = saa7134_vb2_start_streaming,
1065 .stop_streaming = saa7134_vb2_stop_streaming,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066};
1067
1068/* ------------------------------------------------------------------ */
1069
Hans Verkuil718bde12013-12-14 08:28:24 -03001070static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Hans Verkuil718bde12013-12-14 08:28:24 -03001072 struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 unsigned long flags;
1074 int restart_overlay = 0;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001075
Hans Verkuil718bde12013-12-14 08:28:24 -03001076 switch (ctrl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 case V4L2_CID_BRIGHTNESS:
Hans Verkuil718bde12013-12-14 08:28:24 -03001078 dev->ctl_bright = ctrl->val;
1079 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 break;
1081 case V4L2_CID_HUE:
Hans Verkuil718bde12013-12-14 08:28:24 -03001082 dev->ctl_hue = ctrl->val;
1083 saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 break;
1085 case V4L2_CID_CONTRAST:
Hans Verkuil718bde12013-12-14 08:28:24 -03001086 dev->ctl_contrast = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1088 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1089 break;
1090 case V4L2_CID_SATURATION:
Hans Verkuil718bde12013-12-14 08:28:24 -03001091 dev->ctl_saturation = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1093 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1094 break;
1095 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil718bde12013-12-14 08:28:24 -03001096 dev->ctl_mute = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 saa7134_tvaudio_setmute(dev);
1098 break;
1099 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuil718bde12013-12-14 08:28:24 -03001100 dev->ctl_volume = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1102 break;
1103 case V4L2_CID_PRIVATE_INVERT:
Hans Verkuil718bde12013-12-14 08:28:24 -03001104 dev->ctl_invert = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1106 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1107 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1108 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1109 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001110 case V4L2_CID_HFLIP:
Hans Verkuil718bde12013-12-14 08:28:24 -03001111 dev->ctl_mirror = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 restart_overlay = 1;
1113 break;
1114 case V4L2_CID_PRIVATE_Y_EVEN:
Hans Verkuil718bde12013-12-14 08:28:24 -03001115 dev->ctl_y_even = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 restart_overlay = 1;
1117 break;
1118 case V4L2_CID_PRIVATE_Y_ODD:
Hans Verkuil718bde12013-12-14 08:28:24 -03001119 dev->ctl_y_odd = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 restart_overlay = 1;
1121 break;
1122 case V4L2_CID_PRIVATE_AUTOMUTE:
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001123 {
1124 struct v4l2_priv_tun_config tda9887_cfg;
1125
1126 tda9887_cfg.tuner = TUNER_TDA9887;
1127 tda9887_cfg.priv = &dev->tda9887_conf;
1128
Hans Verkuil718bde12013-12-14 08:28:24 -03001129 dev->ctl_automute = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (dev->tda9887_conf) {
1131 if (dev->ctl_automute)
1132 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1133 else
1134 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001135
Hans Verkuilfac69862009-01-17 12:17:14 -03001136 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 break;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 default:
Hans Verkuil718bde12013-12-14 08:28:24 -03001141 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
Hans Verkuil2ada8152014-04-17 07:30:53 -03001143 if (restart_overlay && dev->overlay_owner) {
Hans Verkuil718bde12013-12-14 08:28:24 -03001144 spin_lock_irqsave(&dev->slock, flags);
1145 stop_preview(dev);
1146 start_preview(dev);
1147 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
Hans Verkuil718bde12013-12-14 08:28:24 -03001149 return 0;
Hans Verkuil531d83a2008-07-26 09:04:06 -03001150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152/* ------------------------------------------------------------------ */
1153
Hans Verkuilbec43662008-12-30 06:58:20 -03001154static int video_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001156 struct video_device *vdev = video_devdata(file);
1157 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuil2ada8152014-04-17 07:30:53 -03001158 int ret = v4l2_fh_open(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Hans Verkuil2ada8152014-04-17 07:30:53 -03001160 if (ret < 0)
1161 return ret;
Mauro Carvalho Chehabbefd6e62009-02-09 12:27:03 -03001162
Hans Verkuil2ada8152014-04-17 07:30:53 -03001163 mutex_lock(&dev->lock);
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001164 if (vdev->vfl_type == VFL_TYPE_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* switch to radio mode */
Hans Verkuile72936d2014-04-17 03:48:50 -03001166 saa7134_tvaudio_setinput(dev, &card(dev).radio);
Hans Verkuilfac69862009-01-17 12:17:14 -03001167 saa_call_all(dev, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 } else {
1169 /* switch to video/vbi mode */
Hans Verkuile72936d2014-04-17 03:48:50 -03001170 video_mux(dev, dev->ctl_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Hans Verkuil2ada8152014-04-17 07:30:53 -03001172 mutex_unlock(&dev->lock);
Ondrej Zary3bbaa3a2013-02-01 20:01:16 -03001173
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Hans Verkuilbec43662008-12-30 06:58:20 -03001177static int video_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001179 struct video_device *vdev = video_devdata(file);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001180 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuil2ada8152014-04-17 07:30:53 -03001181 struct v4l2_fh *fh = file->private_data;
Hans Verkuilb9218f22010-12-27 12:22:46 -03001182 struct saa6588_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 unsigned long flags;
1184
Hans Verkuil2ada8152014-04-17 07:30:53 -03001185 mutex_lock(&dev->lock);
Stas Sergeeva1dca1e2011-12-03 16:40:45 -03001186 saa7134_tvaudio_close(dev);
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 /* turn off overlay */
Hans Verkuil2ada8152014-04-17 07:30:53 -03001189 if (fh == dev->overlay_owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 spin_lock_irqsave(&dev->slock,flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001191 stop_preview(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 spin_unlock_irqrestore(&dev->slock,flags);
Hans Verkuil2ada8152014-04-17 07:30:53 -03001193 dev->overlay_owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195
Hans Verkuil2ada8152014-04-17 07:30:53 -03001196 if (vdev->vfl_type == VFL_TYPE_RADIO)
1197 v4l2_fh_release(file);
1198 else
1199 _vb2_fop_release(file, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001201 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1202 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1203 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1204 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1205 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1206
Laurent Pinchart622b8282009-10-05 10:48:17 -03001207 saa_call_all(dev, core, s_power, 0);
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001208 if (vdev->vfl_type == VFL_TYPE_RADIO)
Hans Verkuilb9218f22010-12-27 12:22:46 -03001209 saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
Hans Verkuil2ada8152014-04-17 07:30:53 -03001210 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return 0;
1213}
1214
Hans Verkuil2983baf2009-03-29 06:26:27 -03001215static ssize_t radio_read(struct file *file, char __user *data,
1216 size_t count, loff_t *ppos)
1217{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001218 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuilb9218f22010-12-27 12:22:46 -03001219 struct saa6588_command cmd;
Hans Verkuil2983baf2009-03-29 06:26:27 -03001220
1221 cmd.block_count = count/3;
Hans Verkuil09092782013-12-14 08:28:36 -03001222 cmd.nonblocking = file->f_flags & O_NONBLOCK;
Hans Verkuil2983baf2009-03-29 06:26:27 -03001223 cmd.buffer = data;
1224 cmd.instance = file;
1225 cmd.result = -ENODEV;
1226
Hans Verkuil2ada8152014-04-17 07:30:53 -03001227 mutex_lock(&dev->lock);
Hans Verkuilb9218f22010-12-27 12:22:46 -03001228 saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
Hans Verkuil2ada8152014-04-17 07:30:53 -03001229 mutex_unlock(&dev->lock);
Hans Verkuil2983baf2009-03-29 06:26:27 -03001230
1231 return cmd.result;
1232}
1233
1234static unsigned int radio_poll(struct file *file, poll_table *wait)
1235{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001236 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuilb9218f22010-12-27 12:22:46 -03001237 struct saa6588_command cmd;
Hans Verkuila2004502013-12-14 08:28:28 -03001238 unsigned int rc = v4l2_ctrl_poll(file, wait);
Hans Verkuil2983baf2009-03-29 06:26:27 -03001239
1240 cmd.instance = file;
1241 cmd.event_list = wait;
Hans Verkuila2004502013-12-14 08:28:28 -03001242 cmd.result = 0;
Hans Verkuil2ada8152014-04-17 07:30:53 -03001243 mutex_lock(&dev->lock);
Hans Verkuilb9218f22010-12-27 12:22:46 -03001244 saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
Hans Verkuil2ada8152014-04-17 07:30:53 -03001245 mutex_unlock(&dev->lock);
Hans Verkuil2983baf2009-03-29 06:26:27 -03001246
Hans Verkuila2004502013-12-14 08:28:28 -03001247 return rc | cmd.result;
Hans Verkuil2983baf2009-03-29 06:26:27 -03001248}
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250/* ------------------------------------------------------------------ */
1251
Hans Verkuil78b526a2008-05-28 12:16:41 -03001252static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001253 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001255 struct saa7134_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 struct saa7134_tvnorm *norm = dev->tvnorm;
1257
Ondrej Zaryd0477952013-02-01 20:01:21 -03001258 memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 f->fmt.vbi.sampling_rate = 6750000 * 4;
1260 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1261 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1262 f->fmt.vbi.offset = 64 * 4;
Michael Schimekf246a812005-06-23 22:04:47 -07001263 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1264 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1265 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1267 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1268
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Hans Verkuil78b526a2008-05-28 12:16:41 -03001272static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001273 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001275 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001276
Hans Verkuil813b9df2013-05-31 08:30:49 -03001277 f->fmt.pix.width = dev->width;
1278 f->fmt.pix.height = dev->height;
Hans Verkuil2ada8152014-04-17 07:30:53 -03001279 f->fmt.pix.field = dev->field;
Hans Verkuil813b9df2013-05-31 08:30:49 -03001280 f->fmt.pix.pixelformat = dev->fmt->fourcc;
Hans de Goede3e71da12016-02-14 17:51:37 -02001281 if (dev->fmt->planar)
1282 f->fmt.pix.bytesperline = f->fmt.pix.width;
1283 else
1284 f->fmt.pix.bytesperline =
1285 (f->fmt.pix.width * dev->fmt->depth) / 8;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001286 f->fmt.pix.sizeimage =
Hans de Goede3e71da12016-02-14 17:51:37 -02001287 (f->fmt.pix.height * f->fmt.pix.width * dev->fmt->depth) / 8;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001288 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001289 return 0;
1290}
1291
Hans Verkuil78b526a2008-05-28 12:16:41 -03001292static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001293 struct v4l2_format *f)
1294{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001295 struct saa7134_dev *dev = video_drvdata(file);
Emil Goode12d866e2013-06-22 08:02:52 -03001296 struct v4l2_clip __user *clips = f->fmt.win.clips;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001297 u32 clipcount = f->fmt.win.clipcount;
1298 int err = 0;
1299 int i;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001300
1301 if (saa7134_no_overlay > 0) {
Mauro Carvalho Chehab83582002015-04-30 06:46:34 -03001302 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return -EINVAL;
1304 }
Hans Verkuilb12262f2013-05-31 08:22:24 -03001305 f->fmt.win = dev->win;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001306 f->fmt.win.clips = clips;
1307 if (clips == NULL)
1308 clipcount = 0;
1309 if (dev->nclips < clipcount)
1310 clipcount = dev->nclips;
1311 f->fmt.win.clipcount = clipcount;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001312
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001313 for (i = 0; !err && i < clipcount; i++) {
1314 if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c,
1315 sizeof(struct v4l2_rect)))
1316 err = -EFAULT;
1317 }
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001318
1319 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Hans Verkuil78b526a2008-05-28 12:16:41 -03001322static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001323 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001325 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001326 struct saa7134_format *fmt;
1327 enum v4l2_field field;
1328 unsigned int maxw, maxh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001330 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1331 if (NULL == fmt)
1332 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001334 field = f->fmt.pix.field;
1335 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1336 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001338 if (V4L2_FIELD_ANY == field) {
1339 field = (f->fmt.pix.height > maxh/2)
1340 ? V4L2_FIELD_INTERLACED
1341 : V4L2_FIELD_BOTTOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001343 switch (field) {
1344 case V4L2_FIELD_TOP:
1345 case V4L2_FIELD_BOTTOM:
1346 maxh = maxh / 2;
1347 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 default:
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001349 field = V4L2_FIELD_INTERLACED;
1350 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001352
1353 f->fmt.pix.field = field;
Herton Ronaldo Krzesinski0a062032010-03-19 14:58:23 -03001354 if (f->fmt.pix.width < 48)
1355 f->fmt.pix.width = 48;
1356 if (f->fmt.pix.height < 32)
1357 f->fmt.pix.height = 32;
1358 if (f->fmt.pix.width > maxw)
1359 f->fmt.pix.width = maxw;
1360 if (f->fmt.pix.height > maxh)
1361 f->fmt.pix.height = maxh;
1362 f->fmt.pix.width &= ~0x03;
Hans de Goede3e71da12016-02-14 17:51:37 -02001363 if (fmt->planar)
1364 f->fmt.pix.bytesperline = f->fmt.pix.width;
1365 else
1366 f->fmt.pix.bytesperline =
1367 (f->fmt.pix.width * fmt->depth) / 8;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001368 f->fmt.pix.sizeimage =
Hans de Goede3e71da12016-02-14 17:51:37 -02001369 (f->fmt.pix.height * f->fmt.pix.width * fmt->depth) / 8;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001370 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001371
1372 return 0;
1373}
1374
Hans Verkuil78b526a2008-05-28 12:16:41 -03001375static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001376 struct v4l2_format *f)
1377{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001378 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001379
1380 if (saa7134_no_overlay > 0) {
Mauro Carvalho Chehab83582002015-04-30 06:46:34 -03001381 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001382 return -EINVAL;
1383 }
1384
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001385 if (f->fmt.win.clips == NULL)
1386 f->fmt.win.clipcount = 0;
1387 return verify_preview(dev, &f->fmt.win, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
Hans Verkuil78b526a2008-05-28 12:16:41 -03001390static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001391 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001393 struct saa7134_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 int err;
1395
Hans Verkuil78b526a2008-05-28 12:16:41 -03001396 err = saa7134_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001397 if (0 != err)
1398 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Hans Verkuile72936d2014-04-17 03:48:50 -03001400 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1401 dev->width = f->fmt.pix.width;
1402 dev->height = f->fmt.pix.height;
Hans Verkuil2ada8152014-04-17 07:30:53 -03001403 dev->field = f->fmt.pix.field;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001404 return 0;
1405}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Hans Verkuil78b526a2008-05-28 12:16:41 -03001407static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001408 struct v4l2_format *f)
1409{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001410 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001411 int err;
Steven Rostedta8b1ecf2008-05-06 20:42:29 -07001412 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001414 if (saa7134_no_overlay > 0) {
Mauro Carvalho Chehab83582002015-04-30 06:46:34 -03001415 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return -EINVAL;
1417 }
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001418 if (f->fmt.win.clips == NULL)
1419 f->fmt.win.clipcount = 0;
1420 err = verify_preview(dev, &f->fmt.win, true);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001421 if (0 != err)
1422 return err;
1423
Hans Verkuilb12262f2013-05-31 08:22:24 -03001424 dev->win = f->fmt.win;
1425 dev->nclips = f->fmt.win.clipcount;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001426
Hans Verkuilb12262f2013-05-31 08:22:24 -03001427 if (copy_from_user(dev->clips, f->fmt.win.clips,
Hans Verkuil2ada8152014-04-17 07:30:53 -03001428 sizeof(struct v4l2_clip) * dev->nclips))
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001429 return -EFAULT;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001430
Hans Verkuil2ada8152014-04-17 07:30:53 -03001431 if (priv == dev->overlay_owner) {
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001432 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001433 stop_preview(dev);
1434 start_preview(dev);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001435 spin_unlock_irqrestore(&dev->slock, flags);
1436 }
1437
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001441int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001442{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001443 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001444 unsigned int n;
1445
1446 n = i->index;
1447 if (n >= SAA7134_INPUT_MAX)
1448 return -EINVAL;
Mauro Carvalho Chehab8fc34862016-02-05 10:24:37 -02001449 if (card_in(dev, i->index).type == SAA7134_NO_INPUT)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001450 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001451 i->index = n;
Mauro Carvalho Chehab8fc34862016-02-05 10:24:37 -02001452 strcpy(i->name, saa7134_input_name[card_in(dev, n).type]);
Mauro Carvalho Chehab8bf77f92016-02-05 10:37:18 -02001453 switch (card_in(dev, n).type) {
1454 case SAA7134_INPUT_TV:
1455 case SAA7134_INPUT_TV_MONO:
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001456 i->type = V4L2_INPUT_TYPE_TUNER;
Mauro Carvalho Chehab8bf77f92016-02-05 10:37:18 -02001457 break;
1458 default:
1459 i->type = V4L2_INPUT_TYPE_CAMERA;
1460 break;
1461 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001462 if (n == dev->ctl_input) {
1463 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1464 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1465
1466 if (0 != (v1 & 0x40))
1467 i->status |= V4L2_IN_ST_NO_H_LOCK;
1468 if (0 != (v2 & 0x40))
Hans Verkuil95075dd2013-12-14 08:28:29 -03001469 i->status |= V4L2_IN_ST_NO_SIGNAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001470 if (0 != (v2 & 0x0e))
1471 i->status |= V4L2_IN_ST_MACROVISION;
1472 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001473 i->std = SAA7134_NORMS;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001474 return 0;
1475}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001476EXPORT_SYMBOL_GPL(saa7134_enum_input);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001477
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001478int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001479{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001480 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001481
1482 *i = dev->ctl_input;
1483 return 0;
1484}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001485EXPORT_SYMBOL_GPL(saa7134_g_input);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001486
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001487int saa7134_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001488{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001489 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001490
Roel Kluinde81c3c2009-07-02 16:09:25 -03001491 if (i >= SAA7134_INPUT_MAX)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001492 return -EINVAL;
Mauro Carvalho Chehab8fc34862016-02-05 10:24:37 -02001493 if (card_in(dev, i).type == SAA7134_NO_INPUT)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001494 return -EINVAL;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001495 video_mux(dev, i);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001496 return 0;
1497}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001498EXPORT_SYMBOL_GPL(saa7134_s_input);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001499
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001500int saa7134_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001501 struct v4l2_capability *cap)
1502{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001503 struct saa7134_dev *dev = video_drvdata(file);
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03001504 struct video_device *vdev = video_devdata(file);
1505 u32 radio_caps, video_caps, vbi_caps;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001506
1507 unsigned int tuner_type = dev->tuner_type;
1508
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001509 strcpy(cap->driver, "saa7134");
1510 strlcpy(cap->card, saa7134_boards[dev->board].name,
1511 sizeof(cap->card));
1512 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001513
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03001514 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1515 if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET))
1516 cap->device_caps |= V4L2_CAP_TUNER;
1517
1518 radio_caps = V4L2_CAP_RADIO;
1519 if (dev->has_rds)
1520 radio_caps |= V4L2_CAP_RDS_CAPTURE;
1521
1522 video_caps = V4L2_CAP_VIDEO_CAPTURE;
Hans Verkuil2ada8152014-04-17 07:30:53 -03001523 if (saa7134_no_overlay <= 0 && !is_empress(file))
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03001524 video_caps |= V4L2_CAP_VIDEO_OVERLAY;
1525
1526 vbi_caps = V4L2_CAP_VBI_CAPTURE;
1527
1528 switch (vdev->vfl_type) {
1529 case VFL_TYPE_RADIO:
1530 cap->device_caps |= radio_caps;
1531 break;
1532 case VFL_TYPE_GRABBER:
1533 cap->device_caps |= video_caps;
1534 break;
1535 case VFL_TYPE_VBI:
1536 cap->device_caps |= vbi_caps;
1537 break;
1538 }
1539 cap->capabilities = radio_caps | video_caps | vbi_caps |
1540 cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1541 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1542 cap->device_caps &= ~V4L2_CAP_STREAMING;
1543 if (!dev->has_rds)
1544 cap->device_caps &= ~V4L2_CAP_READWRITE;
1545 }
1546
Julia Lawall473d8022010-08-05 17:22:44 -03001547 return 0;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001548}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001549EXPORT_SYMBOL_GPL(saa7134_querycap);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001550
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001551int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001552{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001553 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuil2ada8152014-04-17 07:30:53 -03001554 struct v4l2_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 unsigned long flags;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001556 unsigned int i;
1557 v4l2_std_id fixup;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001558
Hans Verkuil2ada8152014-04-17 07:30:53 -03001559 if (is_empress(file) && dev->overlay_owner) {
Hans Verkuil9afb7372008-09-06 06:34:44 -03001560 /* Don't change the std from the mpeg device
1561 if overlay is active. */
1562 return -EBUSY;
1563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001565 for (i = 0; i < TVNORMS; i++)
Hans Verkuil314527a2013-03-15 06:10:40 -03001566 if (id == tvnorms[i].id)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001567 break;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001568
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001569 if (i == TVNORMS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 for (i = 0; i < TVNORMS; i++)
Hans Verkuil314527a2013-03-15 06:10:40 -03001571 if (id & tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 break;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001573 if (i == TVNORMS)
1574 return -EINVAL;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001575
Hans Verkuil314527a2013-03-15 06:10:40 -03001576 if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001577 if (secam[0] == 'L' || secam[0] == 'l') {
1578 if (secam[1] == 'C' || secam[1] == 'c')
1579 fixup = V4L2_STD_SECAM_LC;
1580 else
1581 fixup = V4L2_STD_SECAM_L;
1582 } else {
1583 if (secam[0] == 'D' || secam[0] == 'd')
1584 fixup = V4L2_STD_SECAM_DK;
1585 else
1586 fixup = V4L2_STD_SECAM;
Hartmut Hackmann17b10a72006-10-02 19:55:07 -03001587 }
Dan Carpenter06e65882010-10-04 16:28:01 -03001588 for (i = 0; i < TVNORMS; i++) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001589 if (fixup == tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 break;
Dan Carpenter06e65882010-10-04 16:28:01 -03001591 }
1592 if (i == TVNORMS)
1593 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001595
Hans Verkuil314527a2013-03-15 06:10:40 -03001596 id = tvnorms[i].id;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001597
Hans Verkuil2ada8152014-04-17 07:30:53 -03001598 if (!is_empress(file) && fh == dev->overlay_owner) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001599 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001600 stop_preview(dev);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001601 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001603 set_tvnorm(dev, &tvnorms[i]);
1604
1605 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001606 start_preview(dev);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001607 spin_unlock_irqrestore(&dev->slock, flags);
1608 } else
1609 set_tvnorm(dev, &tvnorms[i]);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001610
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001611 saa7134_tvaudio_do_scan(dev);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001612 return 0;
1613}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001614EXPORT_SYMBOL_GPL(saa7134_s_std);
Hans Verkuil9afb7372008-09-06 06:34:44 -03001615
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001616int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
Hans Verkuil9afb7372008-09-06 06:34:44 -03001617{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001618 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuil9afb7372008-09-06 06:34:44 -03001619
1620 *id = dev->tvnorm->id;
1621 return 0;
1622}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001623EXPORT_SYMBOL_GPL(saa7134_g_std);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001624
Mikhail Domrachev707b7f82014-04-01 09:28:17 -03001625static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
1626{
1627 static v4l2_std_id stds[] = {
1628 V4L2_STD_UNKNOWN,
1629 V4L2_STD_NTSC,
1630 V4L2_STD_PAL,
1631 V4L2_STD_SECAM };
1632
1633 v4l2_std_id result = 0;
1634
1635 u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
1636 u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
1637
1638 if (!(st2 & 0x1)) /* RDCAP == 0 */
1639 result = V4L2_STD_UNKNOWN;
1640 else
1641 result = stds[st1 & 0x03];
1642
1643 return result;
1644}
1645
1646int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
1647{
1648 struct saa7134_dev *dev = video_drvdata(file);
1649 *std &= saa7134_read_std(dev);
1650 return 0;
1651}
1652EXPORT_SYMBOL_GPL(saa7134_querystd);
1653
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001654static int saa7134_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001655 struct v4l2_cropcap *cap)
1656{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001657 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001658
1659 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1660 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1661 return -EINVAL;
1662 cap->bounds = dev->crop_bounds;
1663 cap->defrect = dev->crop_defrect;
1664 cap->pixelaspect.numerator = 1;
1665 cap->pixelaspect.denominator = 1;
1666 if (dev->tvnorm->id & V4L2_STD_525_60) {
1667 cap->pixelaspect.numerator = 11;
1668 cap->pixelaspect.denominator = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001670 if (dev->tvnorm->id & V4L2_STD_625_50) {
1671 cap->pixelaspect.numerator = 54;
1672 cap->pixelaspect.denominator = 59;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674 return 0;
1675}
1676
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001677static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001678{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001679 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001680
1681 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1682 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1683 return -EINVAL;
1684 crop->c = dev->crop_current;
1685 return 0;
1686}
1687
Hans Verkuil4f996592012-09-05 05:10:48 -03001688static int saa7134_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001689{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001690 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001691 struct v4l2_rect *b = &dev->crop_bounds;
Hans Verkuil4f996592012-09-05 05:10:48 -03001692 struct v4l2_rect *c = &dev->crop_current;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001693
1694 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1695 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1696 return -EINVAL;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001697
Hans Verkuil2ada8152014-04-17 07:30:53 -03001698 if (dev->overlay_owner)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001699 return -EBUSY;
Hans Verkuil2ada8152014-04-17 07:30:53 -03001700 if (vb2_is_streaming(&dev->video_vbq))
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001701 return -EBUSY;
1702
Hans Verkuil4f996592012-09-05 05:10:48 -03001703 *c = crop->c;
1704 if (c->top < b->top)
1705 c->top = b->top;
1706 if (c->top > b->top + b->height)
1707 c->top = b->top + b->height;
1708 if (c->height > b->top - c->top + b->height)
1709 c->height = b->top - c->top + b->height;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001710
Hans Verkuil4f996592012-09-05 05:10:48 -03001711 if (c->left < b->left)
1712 c->left = b->left;
1713 if (c->left > b->left + b->width)
1714 c->left = b->left + b->width;
1715 if (c->width > b->left - c->left + b->width)
1716 c->width = b->left - c->left + b->width;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001717 return 0;
1718}
1719
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001720int saa7134_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001721 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001723 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001724 int n;
1725
1726 if (0 != t->index)
1727 return -EINVAL;
1728 memset(t, 0, sizeof(*t));
Dan Carpenter06e65882010-10-04 16:28:01 -03001729 for (n = 0; n < SAA7134_INPUT_MAX; n++) {
Mauro Carvalho Chehab8bf77f92016-02-05 10:37:18 -02001730 if (card_in(dev, n).type == SAA7134_INPUT_TV ||
1731 card_in(dev, n).type == SAA7134_INPUT_TV_MONO)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001732 break;
Dan Carpenter06e65882010-10-04 16:28:01 -03001733 }
1734 if (n == SAA7134_INPUT_MAX)
1735 return -EINVAL;
Mauro Carvalho Chehab8fc34862016-02-05 10:24:37 -02001736 if (card_in(dev, n).type != SAA7134_NO_INPUT) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001737 strcpy(t->name, "Television");
1738 t->type = V4L2_TUNER_ANALOG_TV;
Ondrej Zaryce5bdd52013-02-01 20:01:18 -03001739 saa_call_all(dev, tuner, g_tuner, t);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001740 t->capability = V4L2_TUNER_CAP_NORM |
1741 V4L2_TUNER_CAP_STEREO |
1742 V4L2_TUNER_CAP_LANG1 |
1743 V4L2_TUNER_CAP_LANG2;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001744 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1745 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1746 }
1747 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1748 t->signal = 0xffff;
1749 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001751EXPORT_SYMBOL_GPL(saa7134_g_tuner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001753int saa7134_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001754 const struct v4l2_tuner *t)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001755{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001756 struct saa7134_dev *dev = video_drvdata(file);
Ondrej Zary3bbaa3a2013-02-01 20:01:16 -03001757 int rx, mode;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001758
Ondrej Zaryce5bdd52013-02-01 20:01:18 -03001759 if (0 != t->index)
1760 return -EINVAL;
1761
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001762 mode = dev->thread.mode;
1763 if (UNSET == mode) {
1764 rx = saa7134_tvaudio_getstereo(dev);
Hans Verkuile92ba282012-05-14 10:17:35 -03001765 mode = saa7134_tvaudio_rx2mode(rx);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001766 }
1767 if (mode != t->audmode)
1768 dev->thread.mode = t->audmode;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001769
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001770 return 0;
1771}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001772EXPORT_SYMBOL_GPL(saa7134_s_tuner);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001773
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001774int saa7134_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001775 struct v4l2_frequency *f)
1776{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001777 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001778
Ondrej Zary0a5ea882013-02-01 20:01:17 -03001779 if (0 != f->tuner)
1780 return -EINVAL;
1781
Ondrej Zary0a5ea882013-02-01 20:01:17 -03001782 saa_call_all(dev, tuner, g_frequency, f);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001783
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001784 return 0;
1785}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001786EXPORT_SYMBOL_GPL(saa7134_g_frequency);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001787
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001788int saa7134_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001789 const struct v4l2_frequency *f)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001790{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001791 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001792
1793 if (0 != f->tuner)
1794 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001795
Hans Verkuilfac69862009-01-17 12:17:14 -03001796 saa_call_all(dev, tuner, s_frequency, f);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001797
1798 saa7134_tvaudio_do_scan(dev);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001799 return 0;
1800}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001801EXPORT_SYMBOL_GPL(saa7134_s_frequency);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001802
Hans Verkuil78b526a2008-05-28 12:16:41 -03001803static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001804 struct v4l2_fmtdesc *f)
1805{
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001806 if (f->index >= FORMATS)
1807 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001808
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001809 strlcpy(f->description, formats[f->index].name,
1810 sizeof(f->description));
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001811
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001812 f->pixelformat = formats[f->index].fourcc;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001813
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001814 return 0;
1815}
1816
Hans Verkuil78b526a2008-05-28 12:16:41 -03001817static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001818 struct v4l2_fmtdesc *f)
1819{
1820 if (saa7134_no_overlay > 0) {
Mauro Carvalho Chehab83582002015-04-30 06:46:34 -03001821 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001822 return -EINVAL;
1823 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001824
1825 if ((f->index >= FORMATS) || formats[f->index].planar)
1826 return -EINVAL;
1827
1828 strlcpy(f->description, formats[f->index].name,
1829 sizeof(f->description));
1830
1831 f->pixelformat = formats[f->index].fourcc;
1832
1833 return 0;
1834}
1835
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001836static int saa7134_g_fbuf(struct file *file, void *f,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001837 struct v4l2_framebuffer *fb)
1838{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001839 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001840
1841 *fb = dev->ovbuf;
1842 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1843
1844 return 0;
1845}
1846
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001847static int saa7134_s_fbuf(struct file *file, void *f,
Hans Verkuile6eb28c2012-09-04 10:26:45 -03001848 const struct v4l2_framebuffer *fb)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001849{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001850 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001851 struct saa7134_format *fmt;
1852
1853 if (!capable(CAP_SYS_ADMIN) &&
1854 !capable(CAP_SYS_RAWIO))
1855 return -EPERM;
1856
1857 /* check args */
1858 fmt = format_by_fourcc(fb->fmt.pixelformat);
1859 if (NULL == fmt)
1860 return -EINVAL;
1861
1862 /* ok, accept it */
1863 dev->ovbuf = *fb;
1864 dev->ovfmt = fmt;
1865 if (0 == dev->ovbuf.fmt.bytesperline)
1866 dev->ovbuf.fmt.bytesperline =
1867 dev->ovbuf.fmt.width*fmt->depth/8;
1868 return 0;
1869}
1870
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001871static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001872{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001873 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001874 unsigned long flags;
1875
1876 if (on) {
1877 if (saa7134_no_overlay > 0) {
Mauro Carvalho Chehab630983b2015-04-30 08:52:35 -03001878 video_dbg("no_overlay\n");
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001879 return -EINVAL;
1880 }
1881
Hans Verkuil2ada8152014-04-17 07:30:53 -03001882 if (dev->overlay_owner && priv != dev->overlay_owner)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001883 return -EBUSY;
Hans Verkuil2ada8152014-04-17 07:30:53 -03001884 dev->overlay_owner = priv;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001885 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001886 start_preview(dev);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001887 spin_unlock_irqrestore(&dev->slock, flags);
1888 }
1889 if (!on) {
Hans Verkuil2ada8152014-04-17 07:30:53 -03001890 if (priv != dev->overlay_owner)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001891 return -EINVAL;
1892 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001893 stop_preview(dev);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001894 spin_unlock_irqrestore(&dev->slock, flags);
Hans Verkuil2ada8152014-04-17 07:30:53 -03001895 dev->overlay_owner = NULL;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001896 }
1897 return 0;
1898}
1899
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03001900#ifdef CONFIG_VIDEO_ADV_DEBUG
1901static int vidioc_g_register (struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001902 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03001903{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001904 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03001905
Hans Verkuilb8300702013-05-29 06:59:59 -03001906 reg->val = saa_readb(reg->reg & 0xffffff);
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001907 reg->size = 1;
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03001908 return 0;
1909}
1910
1911static int vidioc_s_register (struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001912 const struct v4l2_dbg_register *reg)
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03001913{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001914 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03001915
Hans Verkuilb8300702013-05-29 06:59:59 -03001916 saa_writeb(reg->reg & 0xffffff, reg->val);
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03001917 return 0;
1918}
1919#endif
1920
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001921static int radio_g_tuner(struct file *file, void *priv,
1922 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001924 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001925
1926 if (0 != t->index)
1927 return -EINVAL;
1928
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001929 strcpy(t->name, "Radio");
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001930
Hans Verkuilfac69862009-01-17 12:17:14 -03001931 saa_call_all(dev, tuner, g_tuner, t);
Ondrej Zary82456702013-02-01 20:01:15 -03001932 t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001933 if (dev->input->amux == TV) {
1934 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
1935 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
1936 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
1937 }
1938 return 0;
1939}
1940static int radio_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001941 const struct v4l2_tuner *t)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001942{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001943 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001944
1945 if (0 != t->index)
1946 return -EINVAL;
1947
Hans Verkuilfac69862009-01-17 12:17:14 -03001948 saa_call_all(dev, tuner, s_tuner, t);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001949 return 0;
1950}
1951
Hans Verkuilbec43662008-12-30 06:58:20 -03001952static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
1954 .owner = THIS_MODULE,
1955 .open = video_open,
1956 .release = video_release,
Hans Verkuil2ada8152014-04-17 07:30:53 -03001957 .read = vb2_fop_read,
1958 .poll = vb2_fop_poll,
1959 .mmap = vb2_fop_mmap,
1960 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961};
1962
Hans Verkuila3998102008-07-21 02:57:38 -03001963static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001964 .vidioc_querycap = saa7134_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001965 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
1966 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
1967 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
1968 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
1969 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
1970 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
1971 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
1972 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001973 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1974 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1975 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001976 .vidioc_cropcap = saa7134_cropcap,
Hans Verkuil2ada8152014-04-17 07:30:53 -03001977 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1978 .vidioc_querybuf = vb2_ioctl_querybuf,
1979 .vidioc_qbuf = vb2_ioctl_qbuf,
1980 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuil9f183022015-12-14 08:17:00 -02001981 .vidioc_expbuf = vb2_ioctl_expbuf,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001982 .vidioc_s_std = saa7134_s_std,
Hans Verkuil9afb7372008-09-06 06:34:44 -03001983 .vidioc_g_std = saa7134_g_std,
Mikhail Domrachev707b7f82014-04-01 09:28:17 -03001984 .vidioc_querystd = saa7134_querystd,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001985 .vidioc_enum_input = saa7134_enum_input,
1986 .vidioc_g_input = saa7134_g_input,
1987 .vidioc_s_input = saa7134_s_input,
Hans Verkuil2ada8152014-04-17 07:30:53 -03001988 .vidioc_streamon = vb2_ioctl_streamon,
1989 .vidioc_streamoff = vb2_ioctl_streamoff,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001990 .vidioc_g_tuner = saa7134_g_tuner,
1991 .vidioc_s_tuner = saa7134_s_tuner,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001992 .vidioc_g_crop = saa7134_g_crop,
1993 .vidioc_s_crop = saa7134_s_crop,
1994 .vidioc_g_fbuf = saa7134_g_fbuf,
1995 .vidioc_s_fbuf = saa7134_s_fbuf,
1996 .vidioc_overlay = saa7134_overlay,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001997 .vidioc_g_frequency = saa7134_g_frequency,
1998 .vidioc_s_frequency = saa7134_s_frequency,
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03001999#ifdef CONFIG_VIDEO_ADV_DEBUG
2000 .vidioc_g_register = vidioc_g_register,
2001 .vidioc_s_register = vidioc_s_register,
2002#endif
Hans Verkuila2004502013-12-14 08:28:28 -03002003 .vidioc_log_status = v4l2_ctrl_log_status,
2004 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2005 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006};
2007
Hans Verkuilbec43662008-12-30 06:58:20 -03002008static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002009 .owner = THIS_MODULE,
2010 .open = video_open,
Hans Verkuil2983baf2009-03-29 06:26:27 -03002011 .read = radio_read,
Hans Verkuila3998102008-07-21 02:57:38 -03002012 .release = video_release,
Hans Verkuil2ada8152014-04-17 07:30:53 -03002013 .unlocked_ioctl = video_ioctl2,
Hans Verkuil2983baf2009-03-29 06:26:27 -03002014 .poll = radio_poll,
Hans Verkuila3998102008-07-21 02:57:38 -03002015};
2016
2017static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03002018 .vidioc_querycap = saa7134_querycap,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002019 .vidioc_g_tuner = radio_g_tuner,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002020 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002021 .vidioc_g_frequency = saa7134_g_frequency,
2022 .vidioc_s_frequency = saa7134_s_frequency,
Hans Verkuila2004502013-12-14 08:28:28 -03002023 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2024 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025};
2026
Hans Verkuila3998102008-07-21 02:57:38 -03002027/* ----------------------------------------------------------- */
2028/* exported stuff */
2029
2030struct video_device saa7134_video_template = {
2031 .name = "saa7134-video",
Hans Verkuila3998102008-07-21 02:57:38 -03002032 .fops = &video_fops,
2033 .ioctl_ops = &video_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002034 .tvnorms = SAA7134_NORMS,
Hans Verkuila3998102008-07-21 02:57:38 -03002035};
2036
2037struct video_device saa7134_radio_template = {
2038 .name = "saa7134-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002039 .fops = &radio_fops,
2040 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002041};
2042
Hans Verkuil718bde12013-12-14 08:28:24 -03002043static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
2044 .s_ctrl = saa7134_s_ctrl,
2045};
2046
2047static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
2048 .ops = &saa7134_ctrl_ops,
2049 .id = V4L2_CID_PRIVATE_INVERT,
2050 .name = "Invert",
2051 .type = V4L2_CTRL_TYPE_BOOLEAN,
2052 .min = 0,
2053 .max = 1,
2054 .step = 1,
2055};
2056
2057static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
2058 .ops = &saa7134_ctrl_ops,
2059 .id = V4L2_CID_PRIVATE_Y_ODD,
2060 .name = "Y Offset Odd Field",
2061 .type = V4L2_CTRL_TYPE_INTEGER,
2062 .min = 0,
2063 .max = 128,
2064 .step = 1,
2065};
2066
2067static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
2068 .ops = &saa7134_ctrl_ops,
2069 .id = V4L2_CID_PRIVATE_Y_EVEN,
2070 .name = "Y Offset Even Field",
2071 .type = V4L2_CTRL_TYPE_INTEGER,
2072 .min = 0,
2073 .max = 128,
2074 .step = 1,
2075};
2076
2077static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
2078 .ops = &saa7134_ctrl_ops,
2079 .id = V4L2_CID_PRIVATE_AUTOMUTE,
2080 .name = "Automute",
2081 .type = V4L2_CTRL_TYPE_BOOLEAN,
2082 .min = 0,
2083 .max = 1,
2084 .step = 1,
2085 .def = 1,
2086};
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088int saa7134_video_init1(struct saa7134_dev *dev)
2089{
Hans Verkuil718bde12013-12-14 08:28:24 -03002090 struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
Hans Verkuil2ada8152014-04-17 07:30:53 -03002091 struct vb2_queue *q;
2092 int ret;
Hans Verkuil718bde12013-12-14 08:28:24 -03002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /* sanitycheck insmod options */
2095 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2096 gbuffers = 2;
Mauro Carvalho Chehab3eeba4a2012-10-27 16:20:27 -03002097 if (gbufsize > gbufsize_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 gbufsize = gbufsize_max;
2099 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2100
Hans Verkuil718bde12013-12-14 08:28:24 -03002101 v4l2_ctrl_handler_init(hdl, 11);
2102 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2103 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
2104 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2105 V4L2_CID_CONTRAST, 0, 127, 1, 68);
2106 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2107 V4L2_CID_SATURATION, 0, 127, 1, 64);
2108 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2109 V4L2_CID_HUE, -128, 127, 1, 0);
2110 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2111 V4L2_CID_HFLIP, 0, 1, 1, 0);
2112 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2113 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
2114 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2115 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
2116 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
2117 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
2118 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
2119 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
2120 if (hdl->error)
2121 return hdl->error;
2122 if (card_has_radio(dev)) {
2123 hdl = &dev->radio_ctrl_handler;
2124 v4l2_ctrl_handler_init(hdl, 2);
2125 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
2126 v4l2_ctrl_radio_filter);
2127 if (hdl->error)
2128 return hdl->error;
2129 }
2130 dev->ctl_mute = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Hans Verkuil718bde12013-12-14 08:28:24 -03002132 if (dev->tda9887_conf && saa7134_ctrl_automute.def)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2134 dev->automute = 0;
2135
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002136 INIT_LIST_HEAD(&dev->video_q.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 init_timer(&dev->video_q.timeout);
2138 dev->video_q.timeout.function = saa7134_buffer_timeout;
2139 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2140 dev->video_q.dev = dev;
Hans Verkuil813b9df2013-05-31 08:30:49 -03002141 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
2142 dev->width = 720;
2143 dev->height = 576;
Hans Verkuil2ada8152014-04-17 07:30:53 -03002144 dev->field = V4L2_FIELD_INTERLACED;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03002145 dev->win.w.width = dev->width;
2146 dev->win.w.height = dev->height;
2147 dev->win.field = V4L2_FIELD_INTERLACED;
2148 dev->ovbuf.fmt.width = dev->width;
2149 dev->ovbuf.fmt.height = dev->height;
2150 dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
2151 dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Maxim Levitskycb712012007-09-27 20:34:25 -03002153 if (saa7134_boards[dev->board].video_out)
2154 saa7134_videoport_init(dev);
2155
Hans Verkuil2ada8152014-04-17 07:30:53 -03002156 q = &dev->video_vbq;
2157 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2158 /*
Hans Verkuila3f415a2014-04-17 06:44:31 -03002159 * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
2160 * engine cannot handle transfers that do not start at the beginning
2161 * of a page. A user-provided pointer can start anywhere in a page, so
2162 * USERPTR support is a no-go unless the application knows about these
2163 * limitations and has special support for this.
Hans Verkuil2ada8152014-04-17 07:30:53 -03002164 */
Hans Verkuil9f183022015-12-14 08:17:00 -02002165 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
Hans Verkuila3f415a2014-04-17 06:44:31 -03002166 if (saa7134_userptr)
2167 q->io_modes |= VB2_USERPTR;
Hans Verkuil2ada8152014-04-17 07:30:53 -03002168 q->drv_priv = &dev->video_q;
2169 q->ops = &vb2_qops;
2170 q->gfp_flags = GFP_DMA32;
2171 q->mem_ops = &vb2_dma_sg_memops;
2172 q->buf_struct_size = sizeof(struct saa7134_buf);
2173 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2174 q->lock = &dev->lock;
Hans Verkuil2bc46b32016-02-15 12:37:15 -02002175 q->dev = &dev->pci->dev;
Hans Verkuil2ada8152014-04-17 07:30:53 -03002176 ret = vb2_queue_init(q);
2177 if (ret)
2178 return ret;
Hans Verkuila00e6882014-04-17 06:06:06 -03002179 saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
Hans Verkuil2ada8152014-04-17 07:30:53 -03002180
2181 q = &dev->vbi_vbq;
2182 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
2183 /* Don't add VB2_USERPTR, see comment above */
2184 q->io_modes = VB2_MMAP | VB2_READ;
Hans Verkuila3f415a2014-04-17 06:44:31 -03002185 if (saa7134_userptr)
2186 q->io_modes |= VB2_USERPTR;
Hans Verkuil2ada8152014-04-17 07:30:53 -03002187 q->drv_priv = &dev->vbi_q;
2188 q->ops = &saa7134_vbi_qops;
2189 q->gfp_flags = GFP_DMA32;
2190 q->mem_ops = &vb2_dma_sg_memops;
2191 q->buf_struct_size = sizeof(struct saa7134_buf);
2192 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2193 q->lock = &dev->lock;
Hans Verkuil2bc46b32016-02-15 12:37:15 -02002194 q->dev = &dev->pci->dev;
Hans Verkuil2ada8152014-04-17 07:30:53 -03002195 ret = vb2_queue_init(q);
2196 if (ret)
2197 return ret;
Hans Verkuila00e6882014-04-17 06:06:06 -03002198 saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
Hans Verkuil9db0fb12013-12-14 08:28:23 -03002199
Maxim Levitskycb712012007-09-27 20:34:25 -03002200 return 0;
2201}
2202
Hans Verkuil9db0fb12013-12-14 08:28:23 -03002203void saa7134_video_fini(struct saa7134_dev *dev)
2204{
2205 /* free stuff */
Hans Verkuil2ada8152014-04-17 07:30:53 -03002206 vb2_queue_release(&dev->video_vbq);
Hans Verkuila00e6882014-04-17 06:06:06 -03002207 saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
Hans Verkuil2ada8152014-04-17 07:30:53 -03002208 vb2_queue_release(&dev->vbi_vbq);
Hans Verkuila00e6882014-04-17 06:06:06 -03002209 saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
Hans Verkuil718bde12013-12-14 08:28:24 -03002210 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2211 if (card_has_radio(dev))
2212 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
Hans Verkuil9db0fb12013-12-14 08:28:23 -03002213}
2214
Maxim Levitskycb712012007-09-27 20:34:25 -03002215int saa7134_videoport_init(struct saa7134_dev *dev)
2216{
2217 /* enable video output */
2218 int vo = saa7134_boards[dev->board].video_out;
2219 int video_reg;
2220 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
Dmitry Belimov37df96d2008-06-10 12:43:58 -03002221
2222 /* Configure videoport */
Maxim Levitskycb712012007-09-27 20:34:25 -03002223 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2224 video_reg = video_out[vo][1];
2225 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2226 video_reg &= ~VP_T_CODE_P_INVERTED;
2227 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2228 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
Maxim Levitskycb712012007-09-27 20:34:25 -03002229 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2230 video_reg = video_out[vo][5];
2231 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2232 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2233 if (vid_port_opts & SET_CLOCK_INVERTED)
2234 video_reg |= VP_CLK_CTRL1_INVERTED;
2235 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2236 video_reg = video_out[vo][6];
2237 if (vid_port_opts & SET_VSYNC_OFF) {
2238 video_reg &= ~VP_VS_TYPE_MASK;
2239 video_reg |= VP_VS_TYPE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
Maxim Levitskycb712012007-09-27 20:34:25 -03002241 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2242 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2243 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Dmitry Belimov37df96d2008-06-10 12:43:58 -03002245 /* Start videoport */
2246 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return 0;
2249}
2250
2251int saa7134_video_init2(struct saa7134_dev *dev)
2252{
2253 /* init video hw */
2254 set_tvnorm(dev,&tvnorms[0]);
2255 video_mux(dev,0);
Hans Verkuil718bde12013-12-14 08:28:24 -03002256 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 saa7134_tvaudio_setmute(dev);
2258 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2259 return 0;
2260}
2261
Maxim Levitsky11f70782007-09-27 20:44:39 -03002262void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
2264 static const char *st[] = {
2265 "(no signal)", "NTSC", "PAL", "SECAM" };
2266 u32 st1,st2;
2267
2268 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2269 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
Mauro Carvalho Chehab630983b2015-04-30 08:52:35 -03002270 video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 (st1 & 0x40) ? "not locked" : "locked",
2272 (st2 & 0x40) ? "no" : "yes",
2273 st[st1 & 0x03]);
Maxim Levitsky11f70782007-09-27 20:44:39 -03002274 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
2276 if (dev->nosignal) {
2277 /* no video signal -> mute audio */
2278 if (dev->ctl_automute)
2279 dev->automute = 1;
2280 saa7134_tvaudio_setmute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 } else {
2282 /* wake up tvaudio audio carrier scan thread */
2283 saa7134_tvaudio_do_scan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
Maxim Levitsky11f70782007-09-27 20:44:39 -03002285
2286 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2287 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2288 else
2289 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (dev->mops && dev->mops->signal_change)
2292 dev->mops->signal_change(dev);
2293}
2294
Maxim Levitsky11f70782007-09-27 20:44:39 -03002295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2297{
2298 enum v4l2_field field;
2299
2300 spin_lock(&dev->slock);
2301 if (dev->video_q.curr) {
Hans Verkuil2ada8152014-04-17 07:30:53 -03002302 field = dev->field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if (V4L2_FIELD_HAS_BOTH(field)) {
2304 /* make sure we have seen both fields */
2305 if ((status & 0x10) == 0x00) {
2306 dev->video_q.curr->top_seen = 1;
2307 goto done;
2308 }
2309 if (!dev->video_q.curr->top_seen)
2310 goto done;
2311 } else if (field == V4L2_FIELD_TOP) {
2312 if ((status & 0x10) != 0x10)
2313 goto done;
2314 } else if (field == V4L2_FIELD_BOTTOM) {
2315 if ((status & 0x10) != 0x00)
2316 goto done;
2317 }
Hans Verkuil2ada8152014-04-17 07:30:53 -03002318 saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
Hans Verkuile72936d2014-04-17 03:48:50 -03002320 saa7134_buffer_next(dev, &dev->video_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 done:
2323 spin_unlock(&dev->slock);
2324}