blob: f331501b6750213db0e044edd7c161aeb655f99b [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
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
27#include <linux/slab.h>
Heikki Orsila9040b322007-04-27 12:31:18 -030028#include <linux/sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Hans Verkuila2004502013-12-14 08:28:28 -030030#include <media/v4l2-common.h>
31#include <media/v4l2-event.h>
32#include <media/saa6588.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "saa7134-reg.h"
35#include "saa7134.h"
36
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 Chehab8c854542007-12-07 17:34:48 -030055#define dprintk(fmt, arg...) if (video_debug&0x04) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
57
58/* ------------------------------------------------------------------ */
Robert W. Boone2f8d4f52005-11-08 21:37:10 -080059/* Defines for Video Output Port Register at address 0x191 */
60
61/* Bit 0: VIP code T bit polarity */
62
63#define VP_T_CODE_P_NON_INVERTED 0x00
64#define VP_T_CODE_P_INVERTED 0x01
65
66/* ------------------------------------------------------------------ */
67/* Defines for Video Output Port Register at address 0x195 */
68
69/* Bit 2: Video output clock delay control */
70
71#define VP_CLK_CTRL2_NOT_DELAYED 0x00
72#define VP_CLK_CTRL2_DELAYED 0x04
73
74/* Bit 1: Video output clock invert control */
75
76#define VP_CLK_CTRL1_NON_INVERTED 0x00
77#define VP_CLK_CTRL1_INVERTED 0x02
78
79/* ------------------------------------------------------------------ */
80/* Defines for Video Output Port Register at address 0x196 */
81
82/* Bits 2 to 0: VSYNC pin video vertical sync type */
83
84#define VP_VS_TYPE_MASK 0x07
85
86#define VP_VS_TYPE_OFF 0x00
87#define VP_VS_TYPE_V123 0x01
88#define VP_VS_TYPE_V_ITU 0x02
89#define VP_VS_TYPE_VGATE_L 0x03
90#define VP_VS_TYPE_RESERVED1 0x04
91#define VP_VS_TYPE_RESERVED2 0x05
92#define VP_VS_TYPE_F_ITU 0x06
93#define VP_VS_TYPE_SC_FID 0x07
94
95/* ------------------------------------------------------------------ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/* data structs for video */
97
98static int video_out[][9] = {
99 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
100};
101
102static struct saa7134_format formats[] = {
103 {
104 .name = "8 bpp gray",
105 .fourcc = V4L2_PIX_FMT_GREY,
106 .depth = 8,
107 .pm = 0x06,
108 },{
109 .name = "15 bpp RGB, le",
110 .fourcc = V4L2_PIX_FMT_RGB555,
111 .depth = 16,
112 .pm = 0x13 | 0x80,
113 },{
114 .name = "15 bpp RGB, be",
115 .fourcc = V4L2_PIX_FMT_RGB555X,
116 .depth = 16,
117 .pm = 0x13 | 0x80,
118 .bswap = 1,
119 },{
120 .name = "16 bpp RGB, le",
121 .fourcc = V4L2_PIX_FMT_RGB565,
122 .depth = 16,
123 .pm = 0x10 | 0x80,
124 },{
125 .name = "16 bpp RGB, be",
126 .fourcc = V4L2_PIX_FMT_RGB565X,
127 .depth = 16,
128 .pm = 0x10 | 0x80,
129 .bswap = 1,
130 },{
131 .name = "24 bpp RGB, le",
132 .fourcc = V4L2_PIX_FMT_BGR24,
133 .depth = 24,
134 .pm = 0x11,
135 },{
136 .name = "24 bpp RGB, be",
137 .fourcc = V4L2_PIX_FMT_RGB24,
138 .depth = 24,
139 .pm = 0x11,
140 .bswap = 1,
141 },{
142 .name = "32 bpp RGB, le",
143 .fourcc = V4L2_PIX_FMT_BGR32,
144 .depth = 32,
145 .pm = 0x12,
146 },{
147 .name = "32 bpp RGB, be",
148 .fourcc = V4L2_PIX_FMT_RGB32,
149 .depth = 32,
150 .pm = 0x12,
151 .bswap = 1,
152 .wswap = 1,
153 },{
154 .name = "4:2:2 packed, YUYV",
155 .fourcc = V4L2_PIX_FMT_YUYV,
156 .depth = 16,
157 .pm = 0x00,
158 .bswap = 1,
159 .yuv = 1,
160 },{
161 .name = "4:2:2 packed, UYVY",
162 .fourcc = V4L2_PIX_FMT_UYVY,
163 .depth = 16,
164 .pm = 0x00,
165 .yuv = 1,
166 },{
167 .name = "4:2:2 planar, Y-Cb-Cr",
168 .fourcc = V4L2_PIX_FMT_YUV422P,
169 .depth = 16,
170 .pm = 0x09,
171 .yuv = 1,
172 .planar = 1,
173 .hshift = 1,
174 .vshift = 0,
175 },{
176 .name = "4:2:0 planar, Y-Cb-Cr",
177 .fourcc = V4L2_PIX_FMT_YUV420,
178 .depth = 12,
179 .pm = 0x0a,
180 .yuv = 1,
181 .planar = 1,
182 .hshift = 1,
183 .vshift = 1,
184 },{
185 .name = "4:2:0 planar, Y-Cb-Cr",
186 .fourcc = V4L2_PIX_FMT_YVU420,
187 .depth = 12,
188 .pm = 0x0a,
189 .yuv = 1,
190 .planar = 1,
191 .uvswap = 1,
192 .hshift = 1,
193 .vshift = 1,
194 }
195};
196#define FORMATS ARRAY_SIZE(formats)
197
198#define NORM_625_50 \
199 .h_start = 0, \
200 .h_stop = 719, \
201 .video_v_start = 24, \
202 .video_v_stop = 311, \
Michael Schimekf246a812005-06-23 22:04:47 -0700203 .vbi_v_start_0 = 7, \
204 .vbi_v_stop_0 = 22, \
205 .vbi_v_start_1 = 319, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 .src_timing = 4
207
208#define NORM_525_60 \
209 .h_start = 0, \
Sean Youngdd6ed862010-02-06 10:58:41 -0300210 .h_stop = 719, \
Michael Schimekf246a812005-06-23 22:04:47 -0700211 .video_v_start = 23, \
212 .video_v_stop = 262, \
213 .vbi_v_start_0 = 10, \
214 .vbi_v_stop_0 = 21, \
215 .vbi_v_start_1 = 273, \
216 .src_timing = 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218static struct saa7134_tvnorm tvnorms[] = {
219 {
220 .name = "PAL", /* autodetect */
221 .id = V4L2_STD_PAL,
222 NORM_625_50,
223
224 .sync_control = 0x18,
225 .luma_control = 0x40,
226 .chroma_ctrl1 = 0x81,
227 .chroma_gain = 0x2a,
228 .chroma_ctrl2 = 0x06,
229 .vgate_misc = 0x1c,
230
231 },{
232 .name = "PAL-BG",
233 .id = V4L2_STD_PAL_BG,
234 NORM_625_50,
235
236 .sync_control = 0x18,
237 .luma_control = 0x40,
238 .chroma_ctrl1 = 0x81,
239 .chroma_gain = 0x2a,
240 .chroma_ctrl2 = 0x06,
241 .vgate_misc = 0x1c,
242
243 },{
244 .name = "PAL-I",
245 .id = V4L2_STD_PAL_I,
246 NORM_625_50,
247
248 .sync_control = 0x18,
249 .luma_control = 0x40,
250 .chroma_ctrl1 = 0x81,
251 .chroma_gain = 0x2a,
252 .chroma_ctrl2 = 0x06,
253 .vgate_misc = 0x1c,
254
255 },{
256 .name = "PAL-DK",
257 .id = V4L2_STD_PAL_DK,
258 NORM_625_50,
259
260 .sync_control = 0x18,
261 .luma_control = 0x40,
262 .chroma_ctrl1 = 0x81,
263 .chroma_gain = 0x2a,
264 .chroma_ctrl2 = 0x06,
265 .vgate_misc = 0x1c,
266
267 },{
268 .name = "NTSC",
269 .id = V4L2_STD_NTSC,
270 NORM_525_60,
271
272 .sync_control = 0x59,
273 .luma_control = 0x40,
274 .chroma_ctrl1 = 0x89,
275 .chroma_gain = 0x2a,
276 .chroma_ctrl2 = 0x0e,
277 .vgate_misc = 0x18,
278
279 },{
280 .name = "SECAM",
281 .id = V4L2_STD_SECAM,
282 NORM_625_50,
283
Hartmut Hackmann17b10a72006-10-02 19:55:07 -0300284 .sync_control = 0x18,
285 .luma_control = 0x1b,
286 .chroma_ctrl1 = 0xd1,
287 .chroma_gain = 0x80,
288 .chroma_ctrl2 = 0x00,
289 .vgate_misc = 0x1c,
290
291 },{
292 .name = "SECAM-DK",
293 .id = V4L2_STD_SECAM_DK,
294 NORM_625_50,
295
296 .sync_control = 0x18,
297 .luma_control = 0x1b,
298 .chroma_ctrl1 = 0xd1,
299 .chroma_gain = 0x80,
300 .chroma_ctrl2 = 0x00,
301 .vgate_misc = 0x1c,
302
303 },{
304 .name = "SECAM-L",
305 .id = V4L2_STD_SECAM_L,
306 NORM_625_50,
307
308 .sync_control = 0x18,
309 .luma_control = 0x1b,
310 .chroma_ctrl1 = 0xd1,
311 .chroma_gain = 0x80,
312 .chroma_ctrl2 = 0x00,
313 .vgate_misc = 0x1c,
314
315 },{
316 .name = "SECAM-Lc",
317 .id = V4L2_STD_SECAM_LC,
318 NORM_625_50,
319
320 .sync_control = 0x18,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 .luma_control = 0x1b,
322 .chroma_ctrl1 = 0xd1,
323 .chroma_gain = 0x80,
324 .chroma_ctrl2 = 0x00,
325 .vgate_misc = 0x1c,
326
327 },{
328 .name = "PAL-M",
329 .id = V4L2_STD_PAL_M,
330 NORM_525_60,
331
332 .sync_control = 0x59,
333 .luma_control = 0x40,
334 .chroma_ctrl1 = 0xb9,
335 .chroma_gain = 0x2a,
336 .chroma_ctrl2 = 0x0e,
337 .vgate_misc = 0x18,
338
339 },{
340 .name = "PAL-Nc",
341 .id = V4L2_STD_PAL_Nc,
342 NORM_625_50,
343
344 .sync_control = 0x18,
345 .luma_control = 0x40,
346 .chroma_ctrl1 = 0xa1,
347 .chroma_gain = 0x2a,
348 .chroma_ctrl2 = 0x06,
349 .vgate_misc = 0x1c,
350
351 },{
352 .name = "PAL-60",
353 .id = V4L2_STD_PAL_60,
354
355 .h_start = 0,
356 .h_stop = 719,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800357 .video_v_start = 23,
358 .video_v_stop = 262,
359 .vbi_v_start_0 = 10,
360 .vbi_v_stop_0 = 21,
361 .vbi_v_start_1 = 273,
362 .src_timing = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 .sync_control = 0x18,
365 .luma_control = 0x40,
366 .chroma_ctrl1 = 0x81,
367 .chroma_gain = 0x2a,
368 .chroma_ctrl2 = 0x06,
369 .vgate_misc = 0x1c,
370 }
371};
372#define TVNORMS ARRAY_SIZE(tvnorms)
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
375{
376 unsigned int i;
377
378 for (i = 0; i < FORMATS; i++)
379 if (formats[i].fourcc == fourcc)
380 return formats+i;
381 return NULL;
382}
383
384/* ----------------------------------------------------------------------- */
385/* resource management */
386
387static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
388{
389 if (fh->resources & bit)
390 /* have it already allocated */
391 return 1;
392
393 /* is it free? */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200394 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (dev->resources & bit) {
396 /* no, someone else uses it */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200397 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
399 }
400 /* it's free, grab it */
401 fh->resources |= bit;
402 dev->resources |= bit;
403 dprintk("res: get %d\n",bit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200404 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return 1;
406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static
409void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
410{
Eric Sesterhennae246012006-03-13 13:17:11 -0300411 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Ingo Molnar3593cab2006-02-07 06:49:14 -0200413 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 fh->resources &= ~bits;
415 dev->resources &= ~bits;
416 dprintk("res: put %d\n",bits);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200417 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420/* ------------------------------------------------------------------ */
421
Adrian Bunkb02044d2007-10-24 13:23:14 -0300422static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 dprintk("set tv norm = %s\n",norm->name);
425 dev->tvnorm = norm;
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* setup cropping */
428 dev->crop_bounds.left = norm->h_start;
429 dev->crop_defrect.left = norm->h_start;
430 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
431 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
432
Michael Schimekf246a812005-06-23 22:04:47 -0700433 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 dev->crop_defrect.top = norm->video_v_start*2;
435 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
436 - dev->crop_bounds.top;
437 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
438
439 dev->crop_current = dev->crop_defrect;
440
Maxim Levitskyc4584732007-10-12 00:57:15 -0300441 saa7134_set_tvnorm_hw(dev);
Maxim Levitskycb712012007-09-27 20:34:25 -0300442}
443
444static void video_mux(struct saa7134_dev *dev, int input)
445{
446 dprintk("video input = %d [%s]\n", input, card_in(dev, input).name);
447 dev->ctl_input = input;
448 set_tvnorm(dev, dev->tvnorm);
449 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
450}
451
Maxim Levitskyc4584732007-10-12 00:57:15 -0300452
453static void saa7134_set_decoder(struct saa7134_dev *dev)
Maxim Levitskycb712012007-09-27 20:34:25 -0300454{
455 int luma_control, sync_control, mux;
456
457 struct saa7134_tvnorm *norm = dev->tvnorm;
458 mux = card_in(dev, dev->ctl_input).vmux;
459
460 luma_control = norm->luma_control;
461 sync_control = norm->sync_control;
462
463 if (mux > 5)
464 luma_control |= 0x80; /* svideo */
465 if (noninterlaced || dev->nosignal)
466 sync_control |= 0x20;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* setup video decoder */
469 saa_writeb(SAA7134_INCR_DELAY, 0x08);
470 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
471 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
472
473 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
474 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
475 saa_writeb(SAA7134_HSYNC_START, 0xeb);
476 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
477 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
478
479 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
480 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
481 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Maxim Levitskyf5a1ac62007-09-27 20:34:20 -0300483 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
484 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
485
486 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
487 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
490 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
491 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
492
493 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
494 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
495
496 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
497 saa_writeb(SAA7134_VGATE_START, 0x11);
498 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
499 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
500 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
501 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Maxim Levitskyc4584732007-10-12 00:57:15 -0300504void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
505{
506 saa7134_set_decoder(dev);
507
Hartmut Hackmann7bff4b42008-04-22 14:46:08 -0300508 if (card_in(dev, dev->ctl_input).tv)
Hans Verkuilf41737e2009-04-01 03:52:39 -0300509 saa_call_all(dev, core, s_std, dev->tvnorm->id);
Hans Verkuil9afb7372008-09-06 06:34:44 -0300510 /* Set the correct norm for the saa6752hs. This function
511 does nothing if there is no saa6752hs. */
Hans Verkuilf41737e2009-04-01 03:52:39 -0300512 saa_call_empress(dev, core, s_std, dev->tvnorm->id);
Maxim Levitskyc4584732007-10-12 00:57:15 -0300513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
516{
517 static const struct {
518 int xpsc;
519 int xacl;
520 int xc2_1;
521 int xdcg;
522 int vpfy;
523 } vals[] = {
524 /* XPSC XACL XC2_1 XDCG VPFY */
525 { 1, 0, 0, 0, 0 },
526 { 2, 2, 1, 2, 2 },
527 { 3, 4, 1, 3, 2 },
528 { 4, 8, 1, 4, 2 },
529 { 5, 8, 1, 4, 2 },
530 { 6, 8, 1, 4, 3 },
531 { 7, 8, 1, 4, 3 },
532 { 8, 15, 0, 4, 3 },
533 { 9, 15, 0, 4, 3 },
534 { 10, 16, 1, 5, 3 },
535 };
536 static const int count = ARRAY_SIZE(vals);
537 int i;
538
539 for (i = 0; i < count; i++)
540 if (vals[i].xpsc == prescale)
541 break;
542 if (i == count)
543 return;
544
545 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
546 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
547 saa_writeb(SAA7134_LEVEL_CTRL(task),
548 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
549 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
550 (vals[i].vpfy << 2) | vals[i].vpfy);
551}
552
553static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
554{
555 int val,mirror;
556
557 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
558 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
559
560 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
561 if (yscale < 2048) {
562 /* LPI */
563 dprintk("yscale LPI yscale=%d\n",yscale);
564 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
565 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
566 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
567 } else {
568 /* ACM */
569 val = 0x40 * 1024 / yscale;
570 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
571 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
572 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
573 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
574 }
575 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
576}
577
578static void set_size(struct saa7134_dev *dev, int task,
579 int width, int height, int interlace)
580{
581 int prescale,xscale,yscale,y_even,y_odd;
582 int h_start, h_stop, v_start, v_stop;
583 int div = interlace ? 2 : 1;
584
585 /* setup video scaler */
586 h_start = dev->crop_current.left;
587 v_start = dev->crop_current.top/2;
588 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
589 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
590
591 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
592 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
593 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
594 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
595 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
596 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
597 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
598 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
599
600 prescale = dev->crop_current.width / width;
601 if (0 == prescale)
602 prescale = 1;
603 xscale = 1024 * dev->crop_current.width / prescale / width;
604 yscale = 512 * div * dev->crop_current.height / height;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800605 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 set_h_prescale(dev,task,prescale);
607 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
608 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
609 set_v_scale(dev,task,yscale);
610
611 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
612 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
613 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
614 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
615
616 /* deinterlace y offsets */
617 y_odd = dev->ctl_y_odd;
618 y_even = dev->ctl_y_even;
619 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
620 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
621 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
622 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
623}
624
625/* ------------------------------------------------------------------ */
626
627struct cliplist {
628 __u16 position;
629 __u8 enable;
630 __u8 disable;
631};
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633static void set_cliplist(struct saa7134_dev *dev, int reg,
634 struct cliplist *cl, int entries, char *name)
635{
636 __u8 winbits = 0;
637 int i;
638
639 for (i = 0; i < entries; i++) {
640 winbits |= cl[i].enable;
641 winbits &= ~cl[i].disable;
642 if (i < 15 && cl[i].position == cl[i+1].position)
643 continue;
644 saa_writeb(reg + 0, winbits);
645 saa_writeb(reg + 2, cl[i].position & 0xff);
646 saa_writeb(reg + 3, cl[i].position >> 8);
647 dprintk("clip: %s winbits=%02x pos=%d\n",
648 name,winbits,cl[i].position);
649 reg += 8;
650 }
651 for (; reg < 0x400; reg += 8) {
652 saa_writeb(reg+ 0, 0);
653 saa_writeb(reg + 1, 0);
654 saa_writeb(reg + 2, 0);
655 saa_writeb(reg + 3, 0);
656 }
657}
658
659static int clip_range(int val)
660{
661 if (val < 0)
662 val = 0;
663 return val;
664}
665
Heikki Orsila9040b322007-04-27 12:31:18 -0300666/* Sort into smallest position first order */
667static int cliplist_cmp(const void *a, const void *b)
668{
669 const struct cliplist *cla = a;
670 const struct cliplist *clb = b;
671 if (cla->position < clb->position)
672 return -1;
673 if (cla->position > clb->position)
674 return 1;
675 return 0;
676}
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
679 int nclips, int interlace)
680{
681 struct cliplist col[16], row[16];
Heikki Orsila9040b322007-04-27 12:31:18 -0300682 int cols = 0, rows = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 int div = interlace ? 2 : 1;
684
Heikki Orsila9040b322007-04-27 12:31:18 -0300685 memset(col, 0, sizeof(col));
686 memset(row, 0, sizeof(row));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 for (i = 0; i < nclips && i < 8; i++) {
688 col[cols].position = clip_range(clips[i].c.left);
689 col[cols].enable = (1 << i);
690 cols++;
691 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
692 col[cols].disable = (1 << i);
693 cols++;
694 row[rows].position = clip_range(clips[i].c.top / div);
695 row[rows].enable = (1 << i);
696 rows++;
697 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
698 / div);
699 row[rows].disable = (1 << i);
700 rows++;
701 }
Heikki Orsila9040b322007-04-27 12:31:18 -0300702 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
703 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 set_cliplist(dev,0x380,col,cols,"cols");
705 set_cliplist(dev,0x384,row,rows,"rows");
706 return 0;
707}
708
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300709static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 enum v4l2_field field;
712 int maxw, maxh;
713
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300714 if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -EINVAL;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300716 if (win->w.width < 48)
717 win->w.width = 48;
718 if (win->w.height < 32)
719 win->w.height = 32;
720 if (win->clipcount > 8)
721 win->clipcount = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300723 win->chromakey = 0;
724 win->global_alpha = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 field = win->field;
726 maxw = dev->crop_current.width;
727 maxh = dev->crop_current.height;
728
729 if (V4L2_FIELD_ANY == field) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800730 field = (win->w.height > maxh/2)
731 ? V4L2_FIELD_INTERLACED
732 : V4L2_FIELD_TOP;
733 }
734 switch (field) {
735 case V4L2_FIELD_TOP:
736 case V4L2_FIELD_BOTTOM:
737 maxh = maxh / 2;
738 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800739 default:
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300740 field = V4L2_FIELD_INTERLACED;
741 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 win->field = field;
745 if (win->w.width > maxw)
746 win->w.width = maxw;
747 if (win->w.height > maxh)
748 win->w.height = maxh;
749 return 0;
750}
751
Hans Verkuil718bde12013-12-14 08:28:24 -0300752static int start_preview(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 unsigned long base,control,bpl;
755 int err;
756
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300757 err = verify_preview(dev, &dev->win, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (0 != err)
759 return err;
760
Hans Verkuilb12262f2013-05-31 08:22:24 -0300761 dev->ovfield = dev->win.field;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
Hans Verkuilb12262f2013-05-31 08:22:24 -0300763 dev->win.w.width, dev->win.w.height,
764 dev->win.w.left, dev->win.w.top,
765 dev->ovfmt->name, v4l2_field_names[dev->ovfield]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 /* setup window + clipping */
Hans Verkuilb12262f2013-05-31 08:22:24 -0300768 set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 V4L2_FIELD_HAS_BOTH(dev->ovfield));
Hans Verkuilb12262f2013-05-31 08:22:24 -0300770 setup_clipping(dev, dev->clips, dev->nclips,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 V4L2_FIELD_HAS_BOTH(dev->ovfield));
772 if (dev->ovfmt->yuv)
773 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
774 else
775 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
776 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
777
778 /* dma: setup channel 1 (= Video Task B) */
779 base = (unsigned long)dev->ovbuf.base;
Hans Verkuilb12262f2013-05-31 08:22:24 -0300780 base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
781 base += dev->ovfmt->depth/8 * dev->win.w.left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 bpl = dev->ovbuf.fmt.bytesperline;
783 control = SAA7134_RS_CONTROL_BURST_16;
784 if (dev->ovfmt->bswap)
785 control |= SAA7134_RS_CONTROL_BSWAP;
786 if (dev->ovfmt->wswap)
787 control |= SAA7134_RS_CONTROL_WSWAP;
788 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
789 saa_writel(SAA7134_RS_BA1(1),base);
790 saa_writel(SAA7134_RS_BA2(1),base+bpl);
791 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
792 saa_writel(SAA7134_RS_CONTROL(1),control);
793 } else {
794 saa_writel(SAA7134_RS_BA1(1),base);
795 saa_writel(SAA7134_RS_BA2(1),base);
796 saa_writel(SAA7134_RS_PITCH(1),bpl);
797 saa_writel(SAA7134_RS_CONTROL(1),control);
798 }
799
800 /* start dma */
801 dev->ovenable = 1;
802 saa7134_set_dmabits(dev);
803
804 return 0;
805}
806
Hans Verkuil718bde12013-12-14 08:28:24 -0300807static int stop_preview(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 dev->ovenable = 0;
810 saa7134_set_dmabits(dev);
811 return 0;
812}
813
814/* ------------------------------------------------------------------ */
815
816static int buffer_activate(struct saa7134_dev *dev,
817 struct saa7134_buf *buf,
818 struct saa7134_buf *next)
819{
820 unsigned long base,control,bpl;
821 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
822
823 dprintk("buffer_activate buf=%p\n",buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300824 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 buf->top_seen = 0;
826
Hans Verkuile72936d2014-04-17 03:48:50 -0300827 set_size(dev, TASK_A, buf->vb.width, buf->vb.height,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 V4L2_FIELD_HAS_BOTH(buf->vb.field));
Hans Verkuil9e534f82014-04-17 05:00:39 -0300829 if (dev->fmt->yuv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
831 else
832 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
Hans Verkuil9e534f82014-04-17 05:00:39 -0300833 saa_writeb(SAA7134_OFMT_VIDEO_A, dev->fmt->pm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /* DMA: setup channel 0 (= Video Task A0) */
836 base = saa7134_buffer_base(buf);
Hans Verkuil9e534f82014-04-17 05:00:39 -0300837 if (dev->fmt->planar)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 bpl = buf->vb.width;
839 else
Hans Verkuil9e534f82014-04-17 05:00:39 -0300840 bpl = (buf->vb.width * dev->fmt->depth) / 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 control = SAA7134_RS_CONTROL_BURST_16 |
842 SAA7134_RS_CONTROL_ME |
843 (buf->pt->dma >> 12);
Hans Verkuil9e534f82014-04-17 05:00:39 -0300844 if (dev->fmt->bswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 control |= SAA7134_RS_CONTROL_BSWAP;
Hans Verkuil9e534f82014-04-17 05:00:39 -0300846 if (dev->fmt->wswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 control |= SAA7134_RS_CONTROL_WSWAP;
848 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
849 /* interlaced */
850 saa_writel(SAA7134_RS_BA1(0),base);
851 saa_writel(SAA7134_RS_BA2(0),base+bpl);
852 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
853 } else {
854 /* non-interlaced */
855 saa_writel(SAA7134_RS_BA1(0),base);
856 saa_writel(SAA7134_RS_BA2(0),base);
857 saa_writel(SAA7134_RS_PITCH(0),bpl);
858 }
859 saa_writel(SAA7134_RS_CONTROL(0),control);
860
Hans Verkuil9e534f82014-04-17 05:00:39 -0300861 if (dev->fmt->planar) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /* DMA: setup channel 4+5 (= planar task A) */
Hans Verkuil9e534f82014-04-17 05:00:39 -0300863 bpl_uv = bpl >> dev->fmt->hshift;
864 lines_uv = buf->vb.height >> dev->fmt->vshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 base2 = base + bpl * buf->vb.height;
866 base3 = base2 + bpl_uv * lines_uv;
Hans Verkuil9e534f82014-04-17 05:00:39 -0300867 if (dev->fmt->uvswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 tmp = base2, base2 = base3, base3 = tmp;
869 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
870 bpl_uv,lines_uv,base2,base3);
871 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
872 /* interlaced */
873 saa_writel(SAA7134_RS_BA1(4),base2);
874 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
875 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
876 saa_writel(SAA7134_RS_BA1(5),base3);
877 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
878 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
879 } else {
880 /* non-interlaced */
881 saa_writel(SAA7134_RS_BA1(4),base2);
882 saa_writel(SAA7134_RS_BA2(4),base2);
883 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
884 saa_writel(SAA7134_RS_BA1(5),base3);
885 saa_writel(SAA7134_RS_BA2(5),base3);
886 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
887 }
888 saa_writel(SAA7134_RS_CONTROL(4),control);
889 saa_writel(SAA7134_RS_CONTROL(5),control);
890 }
891
892 /* start DMA */
893 saa7134_set_dmabits(dev);
Hans Verkuile72936d2014-04-17 03:48:50 -0300894 mod_timer(&dev->video_q.timeout, jiffies + BUFFER_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return 0;
896}
897
898static int buffer_prepare(struct videobuf_queue *q,
899 struct videobuf_buffer *vb,
900 enum v4l2_field field)
901{
Hans Verkuil9db0fb12013-12-14 08:28:23 -0300902 struct saa7134_dev *dev = q->priv_data;
Hans Verkuile72936d2014-04-17 03:48:50 -0300903 struct saa7134_buf *buf = container_of(vb, struct saa7134_buf, vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 unsigned int size;
905 int err;
906
907 /* sanity checks */
Hans Verkuil813b9df2013-05-31 08:30:49 -0300908 if (NULL == dev->fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -EINVAL;
Hans Verkuil813b9df2013-05-31 08:30:49 -0300910 if (dev->width < 48 ||
911 dev->height < 32 ||
912 dev->width/4 > dev->crop_current.width ||
913 dev->height/4 > dev->crop_current.height ||
914 dev->width > dev->crop_bounds.width ||
915 dev->height > dev->crop_bounds.height)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return -EINVAL;
Hans Verkuil813b9df2013-05-31 08:30:49 -0300917 size = (dev->width * dev->height * dev->fmt->depth) >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (0 != buf->vb.baddr && buf->vb.bsize < size)
919 return -EINVAL;
920
921 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
Hans Verkuil813b9df2013-05-31 08:30:49 -0300922 vb->i, dev->width, dev->height, size, v4l2_field_names[field],
923 dev->fmt->name);
924 if (buf->vb.width != dev->width ||
925 buf->vb.height != dev->height ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 buf->vb.size != size ||
Hans Verkuil9e534f82014-04-17 05:00:39 -0300927 buf->vb.field != field) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300928 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930
Brandon Philips0fc06862007-11-06 20:02:36 -0300931 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300932 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
933
Hans Verkuil813b9df2013-05-31 08:30:49 -0300934 buf->vb.width = dev->width;
935 buf->vb.height = dev->height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 buf->vb.size = size;
937 buf->vb.field = field;
Hans Verkuil9db0fb12013-12-14 08:28:23 -0300938 buf->pt = &dev->pt_cap;
figo.zhangd5709a02009-05-07 23:31:02 -0300939 dev->video_q.curr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300941 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (err)
943 goto oops;
944 err = saa7134_pgtable_build(dev->pci,buf->pt,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300945 dma->sglist,
946 dma->sglen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 saa7134_buffer_startpage(buf));
948 if (err)
949 goto oops;
950 }
Brandon Philips0fc06862007-11-06 20:02:36 -0300951 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 buf->activate = buffer_activate;
953 return 0;
954
955 oops:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300956 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return err;
958}
959
960static int
961buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
962{
Hans Verkuil9db0fb12013-12-14 08:28:23 -0300963 struct saa7134_dev *dev = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Hans Verkuil813b9df2013-05-31 08:30:49 -0300965 *size = dev->fmt->depth * dev->width * dev->height >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (0 == *count)
967 *count = gbuffers;
968 *count = saa7134_buffer_count(*size,*count);
969 return 0;
970}
971
972static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
973{
Hans Verkuil9db0fb12013-12-14 08:28:23 -0300974 struct saa7134_dev *dev = q->priv_data;
Hans Verkuile72936d2014-04-17 03:48:50 -0300975 struct saa7134_buf *buf = container_of(vb, struct saa7134_buf, vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Hans Verkuil9db0fb12013-12-14 08:28:23 -0300977 saa7134_buffer_queue(dev, &dev->video_q, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
980static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
981{
Hans Verkuile72936d2014-04-17 03:48:50 -0300982 struct saa7134_buf *buf = container_of(vb, struct saa7134_buf, vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300984 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987static struct videobuf_queue_ops video_qops = {
988 .buf_setup = buffer_setup,
989 .buf_prepare = buffer_prepare,
990 .buf_queue = buffer_queue,
991 .buf_release = buffer_release,
992};
993
994/* ------------------------------------------------------------------ */
995
Hans Verkuil718bde12013-12-14 08:28:24 -0300996static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Hans Verkuil718bde12013-12-14 08:28:24 -0300998 struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 unsigned long flags;
1000 int restart_overlay = 0;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001001
Hans Verkuil718bde12013-12-14 08:28:24 -03001002 switch (ctrl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 case V4L2_CID_BRIGHTNESS:
Hans Verkuil718bde12013-12-14 08:28:24 -03001004 dev->ctl_bright = ctrl->val;
1005 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 break;
1007 case V4L2_CID_HUE:
Hans Verkuil718bde12013-12-14 08:28:24 -03001008 dev->ctl_hue = ctrl->val;
1009 saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 break;
1011 case V4L2_CID_CONTRAST:
Hans Verkuil718bde12013-12-14 08:28:24 -03001012 dev->ctl_contrast = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1014 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1015 break;
1016 case V4L2_CID_SATURATION:
Hans Verkuil718bde12013-12-14 08:28:24 -03001017 dev->ctl_saturation = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1019 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1020 break;
1021 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil718bde12013-12-14 08:28:24 -03001022 dev->ctl_mute = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 saa7134_tvaudio_setmute(dev);
1024 break;
1025 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuil718bde12013-12-14 08:28:24 -03001026 dev->ctl_volume = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1028 break;
1029 case V4L2_CID_PRIVATE_INVERT:
Hans Verkuil718bde12013-12-14 08:28:24 -03001030 dev->ctl_invert = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1032 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1033 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1034 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1035 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001036 case V4L2_CID_HFLIP:
Hans Verkuil718bde12013-12-14 08:28:24 -03001037 dev->ctl_mirror = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 restart_overlay = 1;
1039 break;
1040 case V4L2_CID_PRIVATE_Y_EVEN:
Hans Verkuil718bde12013-12-14 08:28:24 -03001041 dev->ctl_y_even = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 restart_overlay = 1;
1043 break;
1044 case V4L2_CID_PRIVATE_Y_ODD:
Hans Verkuil718bde12013-12-14 08:28:24 -03001045 dev->ctl_y_odd = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 restart_overlay = 1;
1047 break;
1048 case V4L2_CID_PRIVATE_AUTOMUTE:
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001049 {
1050 struct v4l2_priv_tun_config tda9887_cfg;
1051
1052 tda9887_cfg.tuner = TUNER_TDA9887;
1053 tda9887_cfg.priv = &dev->tda9887_conf;
1054
Hans Verkuil718bde12013-12-14 08:28:24 -03001055 dev->ctl_automute = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (dev->tda9887_conf) {
1057 if (dev->ctl_automute)
1058 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1059 else
1060 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001061
Hans Verkuilfac6986c2009-01-17 12:17:14 -03001062 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064 break;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 default:
Hans Verkuil718bde12013-12-14 08:28:24 -03001067 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
Hans Verkuil718bde12013-12-14 08:28:24 -03001069 if (restart_overlay && res_locked(dev, RESOURCE_OVERLAY)) {
1070 spin_lock_irqsave(&dev->slock, flags);
1071 stop_preview(dev);
1072 start_preview(dev);
1073 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
Hans Verkuil718bde12013-12-14 08:28:24 -03001075 return 0;
Hans Verkuil531d83a2008-07-26 09:04:06 -03001076}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078/* ------------------------------------------------------------------ */
1079
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001080static struct videobuf_queue *saa7134_queue(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001082 struct video_device *vdev = video_devdata(file);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001083 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuilce791132013-12-14 08:28:27 -03001084 struct saa7134_fh *fh = file->private_data;
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001085 struct videobuf_queue *q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001087 switch (vdev->vfl_type) {
1088 case VFL_TYPE_GRABBER:
Hans Verkuilce791132013-12-14 08:28:27 -03001089 q = fh->is_empress ? &dev->empress_tsq : &dev->cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 break;
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001091 case VFL_TYPE_VBI:
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001092 q = &dev->vbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 break;
1094 default:
1095 BUG();
1096 }
1097 return q;
1098}
1099
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001100static int saa7134_resource(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001102 struct video_device *vdev = video_devdata(file);
Hans Verkuilce791132013-12-14 08:28:27 -03001103 struct saa7134_fh *fh = file->private_data;
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001104
1105 if (vdev->vfl_type == VFL_TYPE_GRABBER)
Hans Verkuilce791132013-12-14 08:28:27 -03001106 return fh->is_empress ? RESOURCE_EMPRESS : RESOURCE_VIDEO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001108 if (vdev->vfl_type == VFL_TYPE_VBI)
Heikki Orsila9040b322007-04-27 12:31:18 -03001109 return RESOURCE_VBI;
1110
1111 BUG();
1112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Hans Verkuilbec43662008-12-30 06:58:20 -03001115static int video_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001117 struct video_device *vdev = video_devdata(file);
1118 struct saa7134_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 struct saa7134_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001122 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Mauro Carvalho Chehabbefd6e62009-02-09 12:27:03 -03001123 if (NULL == fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return -ENOMEM;
Mauro Carvalho Chehabbefd6e62009-02-09 12:27:03 -03001125
Ondrej Zary3bbaa3a2013-02-01 20:01:16 -03001126 v4l2_fh_init(&fh->fh, vdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 file->private_data = fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001129 if (vdev->vfl_type == VFL_TYPE_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /* switch to radio mode */
Hans Verkuile72936d2014-04-17 03:48:50 -03001131 saa7134_tvaudio_setinput(dev, &card(dev).radio);
Hans Verkuilfac6986c2009-01-17 12:17:14 -03001132 saa_call_all(dev, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 } else {
1134 /* switch to video/vbi mode */
Hans Verkuile72936d2014-04-17 03:48:50 -03001135 video_mux(dev, dev->ctl_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
Ondrej Zary3bbaa3a2013-02-01 20:01:16 -03001137 v4l2_fh_add(&fh->fh);
1138
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001139 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
1142static ssize_t
1143video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1144{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001145 struct video_device *vdev = video_devdata(file);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001146 struct saa7134_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 struct saa7134_fh *fh = file->private_data;
1148
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001149 switch (vdev->vfl_type) {
1150 case VFL_TYPE_GRABBER:
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001151 if (res_locked(dev, RESOURCE_VIDEO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return -EBUSY;
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001153 return videobuf_read_one(saa7134_queue(file),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 data, count, ppos,
1155 file->f_flags & O_NONBLOCK);
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001156 case VFL_TYPE_VBI:
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001157 if (!res_get(dev, fh, RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return -EBUSY;
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001159 return videobuf_read_stream(saa7134_queue(file),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 data, count, ppos, 1,
1161 file->f_flags & O_NONBLOCK);
1162 break;
1163 default:
1164 BUG();
1165 return 0;
1166 }
1167}
1168
1169static unsigned int
1170video_poll(struct file *file, struct poll_table_struct *wait)
1171{
Hans Verkuila2004502013-12-14 08:28:28 -03001172 unsigned long req_events = poll_requested_events(wait);
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001173 struct video_device *vdev = video_devdata(file);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001174 struct saa7134_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 struct saa7134_fh *fh = file->private_data;
1176 struct videobuf_buffer *buf = NULL;
figo.zhang4a567392009-05-17 23:13:13 -03001177 unsigned int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Hans Verkuila2004502013-12-14 08:28:28 -03001179 if (v4l2_event_pending(&fh->fh))
1180 rc = POLLPRI;
1181 else if (req_events & POLLPRI)
1182 poll_wait(file, &fh->fh.wait, wait);
1183
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001184 if (vdev->vfl_type == VFL_TYPE_VBI)
Hans Verkuila2004502013-12-14 08:28:28 -03001185 return rc | videobuf_poll_stream(file, &dev->vbi, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001187 if (res_check(fh, RESOURCE_VIDEO)) {
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001188 mutex_lock(&dev->cap.vb_lock);
1189 if (!list_empty(&dev->cap.stream))
1190 buf = list_entry(dev->cap.stream.next, struct videobuf_buffer, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 } else {
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001192 mutex_lock(&dev->cap.vb_lock);
1193 if (UNSET == dev->cap.read_off) {
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001194 /* need to capture a new frame */
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001195 if (res_locked(dev, RESOURCE_VIDEO))
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001196 goto err;
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001197 if (0 != dev->cap.ops->buf_prepare(&dev->cap,
1198 dev->cap.read_buf, dev->cap.field))
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001199 goto err;
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001200 dev->cap.ops->buf_queue(&dev->cap, dev->cap.read_buf);
1201 dev->cap.read_off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001203 buf = dev->cap.read_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205
1206 if (!buf)
figo.zhang4a567392009-05-17 23:13:13 -03001207 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 poll_wait(file, &buf->done, wait);
Hans Verkuila2004502013-12-14 08:28:28 -03001210 if (buf->state == VIDEOBUF_DONE || buf->state == VIDEOBUF_ERROR)
1211 rc |= POLLIN | POLLRDNORM;
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001212 mutex_unlock(&dev->cap.vb_lock);
figo.zhang4a567392009-05-17 23:13:13 -03001213 return rc;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001214
1215err:
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001216 mutex_unlock(&dev->cap.vb_lock);
Hans Verkuila2004502013-12-14 08:28:28 -03001217 return rc | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Hans Verkuilbec43662008-12-30 06:58:20 -03001220static int video_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001222 struct video_device *vdev = video_devdata(file);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001223 struct saa7134_dev *dev = video_drvdata(file);
1224 struct saa7134_fh *fh = file->private_data;
Hans Verkuilb9218f22010-12-27 12:22:46 -03001225 struct saa6588_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 unsigned long flags;
1227
Stas Sergeeva1dca1e2011-12-03 16:40:45 -03001228 saa7134_tvaudio_close(dev);
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /* turn off overlay */
1231 if (res_check(fh, RESOURCE_OVERLAY)) {
1232 spin_lock_irqsave(&dev->slock,flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001233 stop_preview(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 spin_unlock_irqrestore(&dev->slock,flags);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001235 res_free(dev, fh, RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237
1238 /* stop video capture */
1239 if (res_check(fh, RESOURCE_VIDEO)) {
Simon Farnsworth9c9cff52013-09-20 13:15:28 -03001240 pm_qos_remove_request(&dev->qos_request);
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001241 videobuf_streamoff(&dev->cap);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001242 res_free(dev, fh, RESOURCE_VIDEO);
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001243 videobuf_mmap_free(&dev->cap);
Hans Verkuil17e7f1b2014-04-17 07:24:31 -03001244 INIT_LIST_HEAD(&dev->cap.stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001246 if (dev->cap.read_buf) {
1247 buffer_release(&dev->cap, dev->cap.read_buf);
1248 kfree(dev->cap.read_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250
1251 /* stop vbi capture */
1252 if (res_check(fh, RESOURCE_VBI)) {
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001253 videobuf_stop(&dev->vbi);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001254 res_free(dev, fh, RESOURCE_VBI);
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001255 videobuf_mmap_free(&dev->vbi);
Hans Verkuil17e7f1b2014-04-17 07:24:31 -03001256 INIT_LIST_HEAD(&dev->vbi.stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001259 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1260 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1261 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1262 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1263 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1264
Laurent Pinchart622b8282009-10-05 10:48:17 -03001265 saa_call_all(dev, core, s_power, 0);
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001266 if (vdev->vfl_type == VFL_TYPE_RADIO)
Hans Verkuilb9218f22010-12-27 12:22:46 -03001267 saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001268
Ondrej Zary3bbaa3a2013-02-01 20:01:16 -03001269 v4l2_fh_del(&fh->fh);
1270 v4l2_fh_exit(&fh->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 file->private_data = NULL;
1272 kfree(fh);
1273 return 0;
1274}
1275
Heikki Orsila9040b322007-04-27 12:31:18 -03001276static int video_mmap(struct file *file, struct vm_area_struct * vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001278 return videobuf_mmap_mapper(saa7134_queue(file), vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
Hans Verkuil2983baf2009-03-29 06:26:27 -03001281static ssize_t radio_read(struct file *file, char __user *data,
1282 size_t count, loff_t *ppos)
1283{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001284 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuilb9218f22010-12-27 12:22:46 -03001285 struct saa6588_command cmd;
Hans Verkuil2983baf2009-03-29 06:26:27 -03001286
1287 cmd.block_count = count/3;
Hans Verkuil09092782013-12-14 08:28:36 -03001288 cmd.nonblocking = file->f_flags & O_NONBLOCK;
Hans Verkuil2983baf2009-03-29 06:26:27 -03001289 cmd.buffer = data;
1290 cmd.instance = file;
1291 cmd.result = -ENODEV;
1292
Hans Verkuilb9218f22010-12-27 12:22:46 -03001293 saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
Hans Verkuil2983baf2009-03-29 06:26:27 -03001294
1295 return cmd.result;
1296}
1297
1298static unsigned int radio_poll(struct file *file, poll_table *wait)
1299{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001300 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuilb9218f22010-12-27 12:22:46 -03001301 struct saa6588_command cmd;
Hans Verkuila2004502013-12-14 08:28:28 -03001302 unsigned int rc = v4l2_ctrl_poll(file, wait);
Hans Verkuil2983baf2009-03-29 06:26:27 -03001303
1304 cmd.instance = file;
1305 cmd.event_list = wait;
Hans Verkuila2004502013-12-14 08:28:28 -03001306 cmd.result = 0;
Hans Verkuilb9218f22010-12-27 12:22:46 -03001307 saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
Hans Verkuil2983baf2009-03-29 06:26:27 -03001308
Hans Verkuila2004502013-12-14 08:28:28 -03001309 return rc | cmd.result;
Hans Verkuil2983baf2009-03-29 06:26:27 -03001310}
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312/* ------------------------------------------------------------------ */
1313
Hans Verkuil78b526a2008-05-28 12:16:41 -03001314static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001315 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001317 struct saa7134_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 struct saa7134_tvnorm *norm = dev->tvnorm;
1319
Ondrej Zaryd0477952013-02-01 20:01:21 -03001320 memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 f->fmt.vbi.sampling_rate = 6750000 * 4;
1322 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1323 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1324 f->fmt.vbi.offset = 64 * 4;
Michael Schimekf246a812005-06-23 22:04:47 -07001325 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1326 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1327 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1329 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1330
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Hans Verkuil78b526a2008-05-28 12:16:41 -03001334static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001335 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001337 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001338
Hans Verkuil813b9df2013-05-31 08:30:49 -03001339 f->fmt.pix.width = dev->width;
1340 f->fmt.pix.height = dev->height;
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001341 f->fmt.pix.field = dev->cap.field;
Hans Verkuil813b9df2013-05-31 08:30:49 -03001342 f->fmt.pix.pixelformat = dev->fmt->fourcc;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001343 f->fmt.pix.bytesperline =
Hans Verkuil813b9df2013-05-31 08:30:49 -03001344 (f->fmt.pix.width * dev->fmt->depth) >> 3;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001345 f->fmt.pix.sizeimage =
1346 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001347 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1348 f->fmt.pix.priv = 0;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001349 return 0;
1350}
1351
Hans Verkuil78b526a2008-05-28 12:16:41 -03001352static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001353 struct v4l2_format *f)
1354{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001355 struct saa7134_dev *dev = video_drvdata(file);
Emil Goode12d866e2013-06-22 08:02:52 -03001356 struct v4l2_clip __user *clips = f->fmt.win.clips;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001357 u32 clipcount = f->fmt.win.clipcount;
1358 int err = 0;
1359 int i;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001360
1361 if (saa7134_no_overlay > 0) {
1362 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -EINVAL;
1364 }
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001365 mutex_lock(&dev->lock);
Hans Verkuilb12262f2013-05-31 08:22:24 -03001366 f->fmt.win = dev->win;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001367 f->fmt.win.clips = clips;
1368 if (clips == NULL)
1369 clipcount = 0;
1370 if (dev->nclips < clipcount)
1371 clipcount = dev->nclips;
1372 f->fmt.win.clipcount = clipcount;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001373
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001374 for (i = 0; !err && i < clipcount; i++) {
1375 if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c,
1376 sizeof(struct v4l2_rect)))
1377 err = -EFAULT;
1378 }
1379 mutex_unlock(&dev->lock);
1380
1381 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
Hans Verkuil78b526a2008-05-28 12:16:41 -03001384static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001385 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001387 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001388 struct saa7134_format *fmt;
1389 enum v4l2_field field;
1390 unsigned int maxw, maxh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001392 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1393 if (NULL == fmt)
1394 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001396 field = f->fmt.pix.field;
1397 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1398 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001400 if (V4L2_FIELD_ANY == field) {
1401 field = (f->fmt.pix.height > maxh/2)
1402 ? V4L2_FIELD_INTERLACED
1403 : V4L2_FIELD_BOTTOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001405 switch (field) {
1406 case V4L2_FIELD_TOP:
1407 case V4L2_FIELD_BOTTOM:
1408 maxh = maxh / 2;
1409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 default:
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001411 field = V4L2_FIELD_INTERLACED;
1412 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001414
1415 f->fmt.pix.field = field;
Herton Ronaldo Krzesinski0a062032010-03-19 14:58:23 -03001416 if (f->fmt.pix.width < 48)
1417 f->fmt.pix.width = 48;
1418 if (f->fmt.pix.height < 32)
1419 f->fmt.pix.height = 32;
1420 if (f->fmt.pix.width > maxw)
1421 f->fmt.pix.width = maxw;
1422 if (f->fmt.pix.height > maxh)
1423 f->fmt.pix.height = maxh;
1424 f->fmt.pix.width &= ~0x03;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001425 f->fmt.pix.bytesperline =
1426 (f->fmt.pix.width * fmt->depth) >> 3;
1427 f->fmt.pix.sizeimage =
1428 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001429 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1430 f->fmt.pix.priv = 0;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001431
1432 return 0;
1433}
1434
Hans Verkuil78b526a2008-05-28 12:16:41 -03001435static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001436 struct v4l2_format *f)
1437{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001438 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001439
1440 if (saa7134_no_overlay > 0) {
1441 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1442 return -EINVAL;
1443 }
1444
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001445 if (f->fmt.win.clips == NULL)
1446 f->fmt.win.clipcount = 0;
1447 return verify_preview(dev, &f->fmt.win, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
Hans Verkuil78b526a2008-05-28 12:16:41 -03001450static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001451 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001453 struct saa7134_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 int err;
1455
Hans Verkuil78b526a2008-05-28 12:16:41 -03001456 err = saa7134_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001457 if (0 != err)
1458 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Hans Verkuile72936d2014-04-17 03:48:50 -03001460 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1461 dev->width = f->fmt.pix.width;
1462 dev->height = f->fmt.pix.height;
Hans Verkuil9db0fb12013-12-14 08:28:23 -03001463 dev->cap.field = f->fmt.pix.field;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001464 return 0;
1465}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Hans Verkuil78b526a2008-05-28 12:16:41 -03001467static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001468 struct v4l2_format *f)
1469{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001470 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001471 int err;
Steven Rostedta8b1ecf2008-05-06 20:42:29 -07001472 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001474 if (saa7134_no_overlay > 0) {
1475 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return -EINVAL;
1477 }
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001478 if (f->fmt.win.clips == NULL)
1479 f->fmt.win.clipcount = 0;
1480 err = verify_preview(dev, &f->fmt.win, true);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001481 if (0 != err)
1482 return err;
1483
1484 mutex_lock(&dev->lock);
1485
Hans Verkuilb12262f2013-05-31 08:22:24 -03001486 dev->win = f->fmt.win;
1487 dev->nclips = f->fmt.win.clipcount;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001488
Hans Verkuilb12262f2013-05-31 08:22:24 -03001489 if (copy_from_user(dev->clips, f->fmt.win.clips,
1490 sizeof(struct v4l2_clip) * dev->nclips)) {
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001491 mutex_unlock(&dev->lock);
1492 return -EFAULT;
1493 }
1494
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001495 if (res_check(priv, RESOURCE_OVERLAY)) {
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001496 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001497 stop_preview(dev);
1498 start_preview(dev);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001499 spin_unlock_irqrestore(&dev->slock, flags);
1500 }
1501
1502 mutex_unlock(&dev->lock);
1503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001506int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001507{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001508 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001509 unsigned int n;
1510
1511 n = i->index;
1512 if (n >= SAA7134_INPUT_MAX)
1513 return -EINVAL;
1514 if (NULL == card_in(dev, i->index).name)
1515 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001516 i->index = n;
1517 i->type = V4L2_INPUT_TYPE_CAMERA;
1518 strcpy(i->name, card_in(dev, n).name);
1519 if (card_in(dev, n).tv)
1520 i->type = V4L2_INPUT_TYPE_TUNER;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001521 if (n == dev->ctl_input) {
1522 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1523 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1524
1525 if (0 != (v1 & 0x40))
1526 i->status |= V4L2_IN_ST_NO_H_LOCK;
1527 if (0 != (v2 & 0x40))
Hans Verkuil95075dd2013-12-14 08:28:29 -03001528 i->status |= V4L2_IN_ST_NO_SIGNAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001529 if (0 != (v2 & 0x0e))
1530 i->status |= V4L2_IN_ST_MACROVISION;
1531 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001532 i->std = SAA7134_NORMS;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001533 return 0;
1534}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001535EXPORT_SYMBOL_GPL(saa7134_enum_input);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001536
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001537int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001538{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001539 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001540
1541 *i = dev->ctl_input;
1542 return 0;
1543}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001544EXPORT_SYMBOL_GPL(saa7134_g_input);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001545
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001546int saa7134_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001547{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001548 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001549
Roel Kluinde81c3c2009-07-02 16:09:25 -03001550 if (i >= SAA7134_INPUT_MAX)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001551 return -EINVAL;
1552 if (NULL == card_in(dev, i).name)
1553 return -EINVAL;
1554 mutex_lock(&dev->lock);
1555 video_mux(dev, i);
1556 mutex_unlock(&dev->lock);
1557 return 0;
1558}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001559EXPORT_SYMBOL_GPL(saa7134_s_input);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001560
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001561int saa7134_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001562 struct v4l2_capability *cap)
1563{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001564 struct saa7134_dev *dev = video_drvdata(file);
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03001565 struct video_device *vdev = video_devdata(file);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001566 struct saa7134_fh *fh = priv;
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03001567 u32 radio_caps, video_caps, vbi_caps;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001568
1569 unsigned int tuner_type = dev->tuner_type;
1570
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001571 strcpy(cap->driver, "saa7134");
1572 strlcpy(cap->card, saa7134_boards[dev->board].name,
1573 sizeof(cap->card));
1574 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001575
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03001576 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1577 if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET))
1578 cap->device_caps |= V4L2_CAP_TUNER;
1579
1580 radio_caps = V4L2_CAP_RADIO;
1581 if (dev->has_rds)
1582 radio_caps |= V4L2_CAP_RDS_CAPTURE;
1583
1584 video_caps = V4L2_CAP_VIDEO_CAPTURE;
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001585 if (saa7134_no_overlay <= 0 && !fh->is_empress)
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03001586 video_caps |= V4L2_CAP_VIDEO_OVERLAY;
1587
1588 vbi_caps = V4L2_CAP_VBI_CAPTURE;
1589
1590 switch (vdev->vfl_type) {
1591 case VFL_TYPE_RADIO:
1592 cap->device_caps |= radio_caps;
1593 break;
1594 case VFL_TYPE_GRABBER:
1595 cap->device_caps |= video_caps;
1596 break;
1597 case VFL_TYPE_VBI:
1598 cap->device_caps |= vbi_caps;
1599 break;
1600 }
1601 cap->capabilities = radio_caps | video_caps | vbi_caps |
1602 cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1603 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1604 cap->device_caps &= ~V4L2_CAP_STREAMING;
1605 if (!dev->has_rds)
1606 cap->device_caps &= ~V4L2_CAP_READWRITE;
1607 }
1608
Julia Lawall473d8022010-08-05 17:22:44 -03001609 return 0;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001610}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001611EXPORT_SYMBOL_GPL(saa7134_querycap);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001612
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001613int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001614{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001615 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001616 struct saa7134_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 unsigned long flags;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001618 unsigned int i;
1619 v4l2_std_id fixup;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001620
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001621 if (fh->is_empress && res_locked(dev, RESOURCE_OVERLAY)) {
Hans Verkuil9afb7372008-09-06 06:34:44 -03001622 /* Don't change the std from the mpeg device
1623 if overlay is active. */
1624 return -EBUSY;
1625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001627 for (i = 0; i < TVNORMS; i++)
Hans Verkuil314527a2013-03-15 06:10:40 -03001628 if (id == tvnorms[i].id)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001629 break;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001630
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001631 if (i == TVNORMS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 for (i = 0; i < TVNORMS; i++)
Hans Verkuil314527a2013-03-15 06:10:40 -03001633 if (id & tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 break;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001635 if (i == TVNORMS)
1636 return -EINVAL;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001637
Hans Verkuil314527a2013-03-15 06:10:40 -03001638 if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001639 if (secam[0] == 'L' || secam[0] == 'l') {
1640 if (secam[1] == 'C' || secam[1] == 'c')
1641 fixup = V4L2_STD_SECAM_LC;
1642 else
1643 fixup = V4L2_STD_SECAM_L;
1644 } else {
1645 if (secam[0] == 'D' || secam[0] == 'd')
1646 fixup = V4L2_STD_SECAM_DK;
1647 else
1648 fixup = V4L2_STD_SECAM;
Hartmut Hackmann17b10a72006-10-02 19:55:07 -03001649 }
Dan Carpenter06e65882010-10-04 16:28:01 -03001650 for (i = 0; i < TVNORMS; i++) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001651 if (fixup == tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 break;
Dan Carpenter06e65882010-10-04 16:28:01 -03001653 }
1654 if (i == TVNORMS)
1655 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001657
Hans Verkuil314527a2013-03-15 06:10:40 -03001658 id = tvnorms[i].id;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001659
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001660 mutex_lock(&dev->lock);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001661 if (!fh->is_empress && res_check(fh, RESOURCE_OVERLAY)) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001662 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001663 stop_preview(dev);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001664 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001666 set_tvnorm(dev, &tvnorms[i]);
1667
1668 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001669 start_preview(dev);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001670 spin_unlock_irqrestore(&dev->slock, flags);
1671 } else
1672 set_tvnorm(dev, &tvnorms[i]);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001673
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001674 saa7134_tvaudio_do_scan(dev);
1675 mutex_unlock(&dev->lock);
1676 return 0;
1677}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001678EXPORT_SYMBOL_GPL(saa7134_s_std);
Hans Verkuil9afb7372008-09-06 06:34:44 -03001679
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001680int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
Hans Verkuil9afb7372008-09-06 06:34:44 -03001681{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001682 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuil9afb7372008-09-06 06:34:44 -03001683
1684 *id = dev->tvnorm->id;
1685 return 0;
1686}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001687EXPORT_SYMBOL_GPL(saa7134_g_std);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001688
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001689static int saa7134_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001690 struct v4l2_cropcap *cap)
1691{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001692 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001693
1694 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1695 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1696 return -EINVAL;
1697 cap->bounds = dev->crop_bounds;
1698 cap->defrect = dev->crop_defrect;
1699 cap->pixelaspect.numerator = 1;
1700 cap->pixelaspect.denominator = 1;
1701 if (dev->tvnorm->id & V4L2_STD_525_60) {
1702 cap->pixelaspect.numerator = 11;
1703 cap->pixelaspect.denominator = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001705 if (dev->tvnorm->id & V4L2_STD_625_50) {
1706 cap->pixelaspect.numerator = 54;
1707 cap->pixelaspect.denominator = 59;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709 return 0;
1710}
1711
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001712static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001713{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001714 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001715
1716 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1717 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1718 return -EINVAL;
1719 crop->c = dev->crop_current;
1720 return 0;
1721}
1722
Hans Verkuil4f996592012-09-05 05:10:48 -03001723static int saa7134_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001724{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001725 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001726 struct v4l2_rect *b = &dev->crop_bounds;
Hans Verkuil4f996592012-09-05 05:10:48 -03001727 struct v4l2_rect *c = &dev->crop_current;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001728
1729 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1730 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1731 return -EINVAL;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001732
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001733 if (res_locked(dev, RESOURCE_OVERLAY))
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001734 return -EBUSY;
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001735 if (res_locked(dev, RESOURCE_VIDEO))
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001736 return -EBUSY;
1737
Hans Verkuil4f996592012-09-05 05:10:48 -03001738 *c = crop->c;
1739 if (c->top < b->top)
1740 c->top = b->top;
1741 if (c->top > b->top + b->height)
1742 c->top = b->top + b->height;
1743 if (c->height > b->top - c->top + b->height)
1744 c->height = b->top - c->top + b->height;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001745
Hans Verkuil4f996592012-09-05 05:10:48 -03001746 if (c->left < b->left)
1747 c->left = b->left;
1748 if (c->left > b->left + b->width)
1749 c->left = b->left + b->width;
1750 if (c->width > b->left - c->left + b->width)
1751 c->width = b->left - c->left + b->width;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001752 return 0;
1753}
1754
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001755int saa7134_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001756 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001758 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001759 int n;
1760
1761 if (0 != t->index)
1762 return -EINVAL;
1763 memset(t, 0, sizeof(*t));
Dan Carpenter06e65882010-10-04 16:28:01 -03001764 for (n = 0; n < SAA7134_INPUT_MAX; n++) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001765 if (card_in(dev, n).tv)
1766 break;
Dan Carpenter06e65882010-10-04 16:28:01 -03001767 }
1768 if (n == SAA7134_INPUT_MAX)
1769 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001770 if (NULL != card_in(dev, n).name) {
1771 strcpy(t->name, "Television");
1772 t->type = V4L2_TUNER_ANALOG_TV;
Ondrej Zaryce5bdd52013-02-01 20:01:18 -03001773 saa_call_all(dev, tuner, g_tuner, t);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001774 t->capability = V4L2_TUNER_CAP_NORM |
1775 V4L2_TUNER_CAP_STEREO |
1776 V4L2_TUNER_CAP_LANG1 |
1777 V4L2_TUNER_CAP_LANG2;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001778 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1779 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1780 }
1781 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1782 t->signal = 0xffff;
1783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001785EXPORT_SYMBOL_GPL(saa7134_g_tuner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001787int saa7134_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001788 const struct v4l2_tuner *t)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001789{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001790 struct saa7134_dev *dev = video_drvdata(file);
Ondrej Zary3bbaa3a2013-02-01 20:01:16 -03001791 int rx, mode;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001792
Ondrej Zaryce5bdd52013-02-01 20:01:18 -03001793 if (0 != t->index)
1794 return -EINVAL;
1795
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001796 mode = dev->thread.mode;
1797 if (UNSET == mode) {
1798 rx = saa7134_tvaudio_getstereo(dev);
Hans Verkuile92ba282012-05-14 10:17:35 -03001799 mode = saa7134_tvaudio_rx2mode(rx);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001800 }
1801 if (mode != t->audmode)
1802 dev->thread.mode = t->audmode;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001803
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001804 return 0;
1805}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001806EXPORT_SYMBOL_GPL(saa7134_s_tuner);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001807
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001808int saa7134_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001809 struct v4l2_frequency *f)
1810{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001811 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001812
Ondrej Zary0a5ea882013-02-01 20:01:17 -03001813 if (0 != f->tuner)
1814 return -EINVAL;
1815
Ondrej Zary0a5ea882013-02-01 20:01:17 -03001816 saa_call_all(dev, tuner, g_frequency, f);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001817
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001818 return 0;
1819}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001820EXPORT_SYMBOL_GPL(saa7134_g_frequency);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001821
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001822int saa7134_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001823 const struct v4l2_frequency *f)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001824{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001825 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001826
1827 if (0 != f->tuner)
1828 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001829 mutex_lock(&dev->lock);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001830
Hans Verkuilfac6986c2009-01-17 12:17:14 -03001831 saa_call_all(dev, tuner, s_frequency, f);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001832
1833 saa7134_tvaudio_do_scan(dev);
1834 mutex_unlock(&dev->lock);
1835 return 0;
1836}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001837EXPORT_SYMBOL_GPL(saa7134_s_frequency);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001838
Hans Verkuil78b526a2008-05-28 12:16:41 -03001839static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001840 struct v4l2_fmtdesc *f)
1841{
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001842 if (f->index >= FORMATS)
1843 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001844
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001845 strlcpy(f->description, formats[f->index].name,
1846 sizeof(f->description));
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001847
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001848 f->pixelformat = formats[f->index].fourcc;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001849
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001850 return 0;
1851}
1852
Hans Verkuil78b526a2008-05-28 12:16:41 -03001853static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001854 struct v4l2_fmtdesc *f)
1855{
1856 if (saa7134_no_overlay > 0) {
1857 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001858 return -EINVAL;
1859 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001860
1861 if ((f->index >= FORMATS) || formats[f->index].planar)
1862 return -EINVAL;
1863
1864 strlcpy(f->description, formats[f->index].name,
1865 sizeof(f->description));
1866
1867 f->pixelformat = formats[f->index].fourcc;
1868
1869 return 0;
1870}
1871
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001872static int saa7134_g_fbuf(struct file *file, void *f,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001873 struct v4l2_framebuffer *fb)
1874{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001875 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001876
1877 *fb = dev->ovbuf;
1878 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1879
1880 return 0;
1881}
1882
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001883static int saa7134_s_fbuf(struct file *file, void *f,
Hans Verkuile6eb28c2012-09-04 10:26:45 -03001884 const struct v4l2_framebuffer *fb)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001885{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001886 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001887 struct saa7134_format *fmt;
1888
1889 if (!capable(CAP_SYS_ADMIN) &&
1890 !capable(CAP_SYS_RAWIO))
1891 return -EPERM;
1892
1893 /* check args */
1894 fmt = format_by_fourcc(fb->fmt.pixelformat);
1895 if (NULL == fmt)
1896 return -EINVAL;
1897
1898 /* ok, accept it */
1899 dev->ovbuf = *fb;
1900 dev->ovfmt = fmt;
1901 if (0 == dev->ovbuf.fmt.bytesperline)
1902 dev->ovbuf.fmt.bytesperline =
1903 dev->ovbuf.fmt.width*fmt->depth/8;
1904 return 0;
1905}
1906
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001907static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001908{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001909 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001910 unsigned long flags;
1911
1912 if (on) {
1913 if (saa7134_no_overlay > 0) {
1914 dprintk("no_overlay\n");
1915 return -EINVAL;
1916 }
1917
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001918 if (!res_get(dev, priv, RESOURCE_OVERLAY))
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001919 return -EBUSY;
1920 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001921 start_preview(dev);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001922 spin_unlock_irqrestore(&dev->slock, flags);
1923 }
1924 if (!on) {
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001925 if (!res_check(priv, RESOURCE_OVERLAY))
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001926 return -EINVAL;
1927 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001928 stop_preview(dev);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001929 spin_unlock_irqrestore(&dev->slock, flags);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001930 res_free(dev, priv, RESOURCE_OVERLAY);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001931 }
1932 return 0;
1933}
1934
Hans Verkuilce791132013-12-14 08:28:27 -03001935int saa7134_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001936 struct v4l2_requestbuffers *p)
1937{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001938 return videobuf_reqbufs(saa7134_queue(file), p);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001939}
Hans Verkuilce791132013-12-14 08:28:27 -03001940EXPORT_SYMBOL_GPL(saa7134_reqbufs);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001941
Hans Verkuilce791132013-12-14 08:28:27 -03001942int saa7134_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001943 struct v4l2_buffer *b)
1944{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001945 return videobuf_querybuf(saa7134_queue(file), b);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001946}
Hans Verkuilce791132013-12-14 08:28:27 -03001947EXPORT_SYMBOL_GPL(saa7134_querybuf);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001948
Hans Verkuilce791132013-12-14 08:28:27 -03001949int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001950{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001951 return videobuf_qbuf(saa7134_queue(file), b);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001952}
Hans Verkuilce791132013-12-14 08:28:27 -03001953EXPORT_SYMBOL_GPL(saa7134_qbuf);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001954
Hans Verkuilce791132013-12-14 08:28:27 -03001955int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001956{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001957 return videobuf_dqbuf(saa7134_queue(file), b,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001958 file->f_flags & O_NONBLOCK);
1959}
Hans Verkuilce791132013-12-14 08:28:27 -03001960EXPORT_SYMBOL_GPL(saa7134_dqbuf);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001961
Hans Verkuilce791132013-12-14 08:28:27 -03001962int saa7134_streamon(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001963 enum v4l2_buf_type type)
1964{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001965 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001966 int res = saa7134_resource(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001967
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001968 if (!res_get(dev, priv, res))
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001969 return -EBUSY;
1970
Simon Farnsworth87739862012-12-10 08:35:09 -03001971 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
1972 * configured conservatively, there's 22 usec of buffering for video.
1973 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
1974 * margin in case the FIFO is configured differently to the datasheet.
1975 * Unfortunately, I lack register-level documentation to check the
1976 * Linux FIFO setup and confirm the perfect value.
1977 */
Hans Verkuilce791132013-12-14 08:28:27 -03001978 if (res != RESOURCE_EMPRESS)
1979 pm_qos_add_request(&dev->qos_request,
1980 PM_QOS_CPU_DMA_LATENCY, 20);
Simon Farnsworth87739862012-12-10 08:35:09 -03001981
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001982 return videobuf_streamon(saa7134_queue(file));
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001983}
Hans Verkuilce791132013-12-14 08:28:27 -03001984EXPORT_SYMBOL_GPL(saa7134_streamon);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001985
Hans Verkuilce791132013-12-14 08:28:27 -03001986int saa7134_streamoff(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001987 enum v4l2_buf_type type)
1988{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001989 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001990 int res = saa7134_resource(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001991
Hans Verkuilce791132013-12-14 08:28:27 -03001992 if (res != RESOURCE_EMPRESS)
1993 pm_qos_remove_request(&dev->qos_request);
Simon Farnsworth87739862012-12-10 08:35:09 -03001994
Hans Verkuil17e7f1b2014-04-17 07:24:31 -03001995 return videobuf_streamoff(saa7134_queue(file));
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001996}
Hans Verkuilce791132013-12-14 08:28:27 -03001997EXPORT_SYMBOL_GPL(saa7134_streamoff);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001998
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03001999#ifdef CONFIG_VIDEO_ADV_DEBUG
2000static int vidioc_g_register (struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002001 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002002{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03002003 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002004
Hans Verkuilb8300702013-05-29 06:59:59 -03002005 reg->val = saa_readb(reg->reg & 0xffffff);
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002006 reg->size = 1;
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002007 return 0;
2008}
2009
2010static int vidioc_s_register (struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03002011 const struct v4l2_dbg_register *reg)
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002012{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03002013 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002014
Hans Verkuilb8300702013-05-29 06:59:59 -03002015 saa_writeb(reg->reg & 0xffffff, reg->val);
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002016 return 0;
2017}
2018#endif
2019
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002020static int radio_g_tuner(struct file *file, void *priv,
2021 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03002023 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002024
2025 if (0 != t->index)
2026 return -EINVAL;
2027
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002028 strcpy(t->name, "Radio");
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002029
Hans Verkuilfac6986c2009-01-17 12:17:14 -03002030 saa_call_all(dev, tuner, g_tuner, t);
Ondrej Zary82456702013-02-01 20:01:15 -03002031 t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002032 if (dev->input->amux == TV) {
2033 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2034 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2035 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2036 }
2037 return 0;
2038}
2039static int radio_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03002040 const struct v4l2_tuner *t)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002041{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03002042 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002043
2044 if (0 != t->index)
2045 return -EINVAL;
2046
Hans Verkuilfac6986c2009-01-17 12:17:14 -03002047 saa_call_all(dev, tuner, s_tuner, t);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002048 return 0;
2049}
2050
Hans Verkuilbec43662008-12-30 06:58:20 -03002051static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 .owner = THIS_MODULE,
2054 .open = video_open,
2055 .release = video_release,
2056 .read = video_read,
2057 .poll = video_poll,
2058 .mmap = video_mmap,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002059 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060};
2061
Hans Verkuila3998102008-07-21 02:57:38 -03002062static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002063 .vidioc_querycap = saa7134_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002064 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
2065 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
2066 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
2067 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
2068 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
2069 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
2070 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
2071 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002072 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2073 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2074 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002075 .vidioc_cropcap = saa7134_cropcap,
2076 .vidioc_reqbufs = saa7134_reqbufs,
2077 .vidioc_querybuf = saa7134_querybuf,
2078 .vidioc_qbuf = saa7134_qbuf,
2079 .vidioc_dqbuf = saa7134_dqbuf,
2080 .vidioc_s_std = saa7134_s_std,
Hans Verkuil9afb7372008-09-06 06:34:44 -03002081 .vidioc_g_std = saa7134_g_std,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002082 .vidioc_enum_input = saa7134_enum_input,
2083 .vidioc_g_input = saa7134_g_input,
2084 .vidioc_s_input = saa7134_s_input,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002085 .vidioc_streamon = saa7134_streamon,
2086 .vidioc_streamoff = saa7134_streamoff,
2087 .vidioc_g_tuner = saa7134_g_tuner,
2088 .vidioc_s_tuner = saa7134_s_tuner,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002089 .vidioc_g_crop = saa7134_g_crop,
2090 .vidioc_s_crop = saa7134_s_crop,
2091 .vidioc_g_fbuf = saa7134_g_fbuf,
2092 .vidioc_s_fbuf = saa7134_s_fbuf,
2093 .vidioc_overlay = saa7134_overlay,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002094 .vidioc_g_frequency = saa7134_g_frequency,
2095 .vidioc_s_frequency = saa7134_s_frequency,
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002096#ifdef CONFIG_VIDEO_ADV_DEBUG
2097 .vidioc_g_register = vidioc_g_register,
2098 .vidioc_s_register = vidioc_s_register,
2099#endif
Hans Verkuila2004502013-12-14 08:28:28 -03002100 .vidioc_log_status = v4l2_ctrl_log_status,
2101 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2102 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103};
2104
Hans Verkuilbec43662008-12-30 06:58:20 -03002105static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002106 .owner = THIS_MODULE,
2107 .open = video_open,
Hans Verkuil2983baf2009-03-29 06:26:27 -03002108 .read = radio_read,
Hans Verkuila3998102008-07-21 02:57:38 -03002109 .release = video_release,
2110 .ioctl = video_ioctl2,
Hans Verkuil2983baf2009-03-29 06:26:27 -03002111 .poll = radio_poll,
Hans Verkuila3998102008-07-21 02:57:38 -03002112};
2113
2114static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03002115 .vidioc_querycap = saa7134_querycap,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002116 .vidioc_g_tuner = radio_g_tuner,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002117 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002118 .vidioc_g_frequency = saa7134_g_frequency,
2119 .vidioc_s_frequency = saa7134_s_frequency,
Hans Verkuila2004502013-12-14 08:28:28 -03002120 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2121 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122};
2123
Hans Verkuila3998102008-07-21 02:57:38 -03002124/* ----------------------------------------------------------- */
2125/* exported stuff */
2126
2127struct video_device saa7134_video_template = {
2128 .name = "saa7134-video",
Hans Verkuila3998102008-07-21 02:57:38 -03002129 .fops = &video_fops,
2130 .ioctl_ops = &video_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002131 .tvnorms = SAA7134_NORMS,
Hans Verkuila3998102008-07-21 02:57:38 -03002132};
2133
2134struct video_device saa7134_radio_template = {
2135 .name = "saa7134-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002136 .fops = &radio_fops,
2137 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002138};
2139
Hans Verkuil718bde12013-12-14 08:28:24 -03002140static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
2141 .s_ctrl = saa7134_s_ctrl,
2142};
2143
2144static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
2145 .ops = &saa7134_ctrl_ops,
2146 .id = V4L2_CID_PRIVATE_INVERT,
2147 .name = "Invert",
2148 .type = V4L2_CTRL_TYPE_BOOLEAN,
2149 .min = 0,
2150 .max = 1,
2151 .step = 1,
2152};
2153
2154static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
2155 .ops = &saa7134_ctrl_ops,
2156 .id = V4L2_CID_PRIVATE_Y_ODD,
2157 .name = "Y Offset Odd Field",
2158 .type = V4L2_CTRL_TYPE_INTEGER,
2159 .min = 0,
2160 .max = 128,
2161 .step = 1,
2162};
2163
2164static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
2165 .ops = &saa7134_ctrl_ops,
2166 .id = V4L2_CID_PRIVATE_Y_EVEN,
2167 .name = "Y Offset Even Field",
2168 .type = V4L2_CTRL_TYPE_INTEGER,
2169 .min = 0,
2170 .max = 128,
2171 .step = 1,
2172};
2173
2174static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
2175 .ops = &saa7134_ctrl_ops,
2176 .id = V4L2_CID_PRIVATE_AUTOMUTE,
2177 .name = "Automute",
2178 .type = V4L2_CTRL_TYPE_BOOLEAN,
2179 .min = 0,
2180 .max = 1,
2181 .step = 1,
2182 .def = 1,
2183};
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185int saa7134_video_init1(struct saa7134_dev *dev)
2186{
Hans Verkuil718bde12013-12-14 08:28:24 -03002187 struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 /* sanitycheck insmod options */
2190 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2191 gbuffers = 2;
Mauro Carvalho Chehab3eeba4a2012-10-27 16:20:27 -03002192 if (gbufsize > gbufsize_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 gbufsize = gbufsize_max;
2194 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2195
Hans Verkuil718bde12013-12-14 08:28:24 -03002196 v4l2_ctrl_handler_init(hdl, 11);
2197 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2198 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
2199 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2200 V4L2_CID_CONTRAST, 0, 127, 1, 68);
2201 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2202 V4L2_CID_SATURATION, 0, 127, 1, 64);
2203 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2204 V4L2_CID_HUE, -128, 127, 1, 0);
2205 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2206 V4L2_CID_HFLIP, 0, 1, 1, 0);
2207 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2208 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
2209 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2210 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
2211 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
2212 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
2213 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
2214 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
2215 if (hdl->error)
2216 return hdl->error;
2217 if (card_has_radio(dev)) {
2218 hdl = &dev->radio_ctrl_handler;
2219 v4l2_ctrl_handler_init(hdl, 2);
2220 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
2221 v4l2_ctrl_radio_filter);
2222 if (hdl->error)
2223 return hdl->error;
2224 }
2225 dev->ctl_mute = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Hans Verkuil718bde12013-12-14 08:28:24 -03002227 if (dev->tda9887_conf && saa7134_ctrl_automute.def)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2229 dev->automute = 0;
2230
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002231 INIT_LIST_HEAD(&dev->video_q.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 init_timer(&dev->video_q.timeout);
2233 dev->video_q.timeout.function = saa7134_buffer_timeout;
2234 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2235 dev->video_q.dev = dev;
Hans Verkuil813b9df2013-05-31 08:30:49 -03002236 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
2237 dev->width = 720;
2238 dev->height = 576;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03002239 dev->win.w.width = dev->width;
2240 dev->win.w.height = dev->height;
2241 dev->win.field = V4L2_FIELD_INTERLACED;
2242 dev->ovbuf.fmt.width = dev->width;
2243 dev->ovbuf.fmt.height = dev->height;
2244 dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
2245 dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Maxim Levitskycb712012007-09-27 20:34:25 -03002247 if (saa7134_boards[dev->board].video_out)
2248 saa7134_videoport_init(dev);
2249
Hans Verkuil9db0fb12013-12-14 08:28:23 -03002250 videobuf_queue_sg_init(&dev->cap, &video_qops,
2251 &dev->pci->dev, &dev->slock,
2252 V4L2_BUF_TYPE_VIDEO_CAPTURE,
2253 V4L2_FIELD_INTERLACED,
2254 sizeof(struct saa7134_buf),
2255 dev, NULL);
2256 videobuf_queue_sg_init(&dev->vbi, &saa7134_vbi_qops,
2257 &dev->pci->dev, &dev->slock,
2258 V4L2_BUF_TYPE_VBI_CAPTURE,
2259 V4L2_FIELD_SEQ_TB,
2260 sizeof(struct saa7134_buf),
2261 dev, NULL);
2262 saa7134_pgtable_alloc(dev->pci, &dev->pt_cap);
2263 saa7134_pgtable_alloc(dev->pci, &dev->pt_vbi);
2264
Maxim Levitskycb712012007-09-27 20:34:25 -03002265 return 0;
2266}
2267
Hans Verkuil9db0fb12013-12-14 08:28:23 -03002268void saa7134_video_fini(struct saa7134_dev *dev)
2269{
2270 /* free stuff */
2271 saa7134_pgtable_free(dev->pci, &dev->pt_cap);
2272 saa7134_pgtable_free(dev->pci, &dev->pt_vbi);
Hans Verkuil718bde12013-12-14 08:28:24 -03002273 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2274 if (card_has_radio(dev))
2275 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
Hans Verkuil9db0fb12013-12-14 08:28:23 -03002276}
2277
Maxim Levitskycb712012007-09-27 20:34:25 -03002278int saa7134_videoport_init(struct saa7134_dev *dev)
2279{
2280 /* enable video output */
2281 int vo = saa7134_boards[dev->board].video_out;
2282 int video_reg;
2283 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
Dmitry Belimov37df96d2008-06-10 12:43:58 -03002284
2285 /* Configure videoport */
Maxim Levitskycb712012007-09-27 20:34:25 -03002286 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2287 video_reg = video_out[vo][1];
2288 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2289 video_reg &= ~VP_T_CODE_P_INVERTED;
2290 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2291 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
Maxim Levitskycb712012007-09-27 20:34:25 -03002292 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2293 video_reg = video_out[vo][5];
2294 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2295 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2296 if (vid_port_opts & SET_CLOCK_INVERTED)
2297 video_reg |= VP_CLK_CTRL1_INVERTED;
2298 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2299 video_reg = video_out[vo][6];
2300 if (vid_port_opts & SET_VSYNC_OFF) {
2301 video_reg &= ~VP_VS_TYPE_MASK;
2302 video_reg |= VP_VS_TYPE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 }
Maxim Levitskycb712012007-09-27 20:34:25 -03002304 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2305 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2306 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Dmitry Belimov37df96d2008-06-10 12:43:58 -03002308 /* Start videoport */
2309 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return 0;
2312}
2313
2314int saa7134_video_init2(struct saa7134_dev *dev)
2315{
2316 /* init video hw */
2317 set_tvnorm(dev,&tvnorms[0]);
2318 video_mux(dev,0);
Hans Verkuil718bde12013-12-14 08:28:24 -03002319 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 saa7134_tvaudio_setmute(dev);
2321 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2322 return 0;
2323}
2324
Maxim Levitsky11f70782007-09-27 20:44:39 -03002325void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
2327 static const char *st[] = {
2328 "(no signal)", "NTSC", "PAL", "SECAM" };
2329 u32 st1,st2;
2330
2331 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2332 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2333 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2334 (st1 & 0x40) ? "not locked" : "locked",
2335 (st2 & 0x40) ? "no" : "yes",
2336 st[st1 & 0x03]);
Maxim Levitsky11f70782007-09-27 20:44:39 -03002337 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 if (dev->nosignal) {
2340 /* no video signal -> mute audio */
2341 if (dev->ctl_automute)
2342 dev->automute = 1;
2343 saa7134_tvaudio_setmute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 } else {
2345 /* wake up tvaudio audio carrier scan thread */
2346 saa7134_tvaudio_do_scan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
Maxim Levitsky11f70782007-09-27 20:44:39 -03002348
2349 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2350 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2351 else
2352 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (dev->mops && dev->mops->signal_change)
2355 dev->mops->signal_change(dev);
2356}
2357
Maxim Levitsky11f70782007-09-27 20:44:39 -03002358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2360{
2361 enum v4l2_field field;
2362
2363 spin_lock(&dev->slock);
2364 if (dev->video_q.curr) {
2365 dev->video_fieldcount++;
2366 field = dev->video_q.curr->vb.field;
2367 if (V4L2_FIELD_HAS_BOTH(field)) {
2368 /* make sure we have seen both fields */
2369 if ((status & 0x10) == 0x00) {
2370 dev->video_q.curr->top_seen = 1;
2371 goto done;
2372 }
2373 if (!dev->video_q.curr->top_seen)
2374 goto done;
2375 } else if (field == V4L2_FIELD_TOP) {
2376 if ((status & 0x10) != 0x10)
2377 goto done;
2378 } else if (field == V4L2_FIELD_BOTTOM) {
2379 if ((status & 0x10) != 0x00)
2380 goto done;
2381 }
2382 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
Hans Verkuile72936d2014-04-17 03:48:50 -03002383 saa7134_buffer_finish(dev, &dev->video_q, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
Hans Verkuile72936d2014-04-17 03:48:50 -03002385 saa7134_buffer_next(dev, &dev->video_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 done:
2388 spin_unlock(&dev->slock);
2389}