blob: f2ae8e6c7517b6eb89227e840aae0a3fed2bc698 [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{
Mikhail Domrachev707b7f82014-04-01 09:28:17 -0300455 int luma_control, sync_control, chroma_ctrl1, mux;
Maxim Levitskycb712012007-09-27 20:34:25 -0300456
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;
Mikhail Domrachev707b7f82014-04-01 09:28:17 -0300462 chroma_ctrl1 = norm->chroma_ctrl1;
Maxim Levitskycb712012007-09-27 20:34:25 -0300463
464 if (mux > 5)
465 luma_control |= 0x80; /* svideo */
466 if (noninterlaced || dev->nosignal)
467 sync_control |= 0x20;
468
Mikhail Domrachev707b7f82014-04-01 09:28:17 -0300469 /* switch on auto standard detection */
470 sync_control |= SAA7134_SYNC_CTRL_AUFD;
471 chroma_ctrl1 |= SAA7134_CHROMA_CTRL1_AUTO0;
472 chroma_ctrl1 &= ~SAA7134_CHROMA_CTRL1_FCTC;
473 luma_control &= ~SAA7134_LUMA_CTRL_LDEL;
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /* setup video decoder */
476 saa_writeb(SAA7134_INCR_DELAY, 0x08);
477 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
478 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
479
480 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
481 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
482 saa_writeb(SAA7134_HSYNC_START, 0xeb);
483 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
484 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
485
486 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
487 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
488 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Maxim Levitskyf5a1ac62007-09-27 20:34:20 -0300490 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
491 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
492
493 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
494 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
Mikhail Domrachev707b7f82014-04-01 09:28:17 -0300497 saa_writeb(SAA7134_CHROMA_CTRL1, chroma_ctrl1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
499
500 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
501 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
502
503 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
504 saa_writeb(SAA7134_VGATE_START, 0x11);
505 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
506 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
507 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
508 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Maxim Levitskyc4584732007-10-12 00:57:15 -0300511void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
512{
513 saa7134_set_decoder(dev);
514
Hartmut Hackmann7bff4b42008-04-22 14:46:08 -0300515 if (card_in(dev, dev->ctl_input).tv)
Hans Verkuilf41737e2009-04-01 03:52:39 -0300516 saa_call_all(dev, core, s_std, dev->tvnorm->id);
Hans Verkuil9afb7372008-09-06 06:34:44 -0300517 /* Set the correct norm for the saa6752hs. This function
518 does nothing if there is no saa6752hs. */
Hans Verkuilf41737e2009-04-01 03:52:39 -0300519 saa_call_empress(dev, core, s_std, dev->tvnorm->id);
Maxim Levitskyc4584732007-10-12 00:57:15 -0300520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
523{
524 static const struct {
525 int xpsc;
526 int xacl;
527 int xc2_1;
528 int xdcg;
529 int vpfy;
530 } vals[] = {
531 /* XPSC XACL XC2_1 XDCG VPFY */
532 { 1, 0, 0, 0, 0 },
533 { 2, 2, 1, 2, 2 },
534 { 3, 4, 1, 3, 2 },
535 { 4, 8, 1, 4, 2 },
536 { 5, 8, 1, 4, 2 },
537 { 6, 8, 1, 4, 3 },
538 { 7, 8, 1, 4, 3 },
539 { 8, 15, 0, 4, 3 },
540 { 9, 15, 0, 4, 3 },
541 { 10, 16, 1, 5, 3 },
542 };
543 static const int count = ARRAY_SIZE(vals);
544 int i;
545
546 for (i = 0; i < count; i++)
547 if (vals[i].xpsc == prescale)
548 break;
549 if (i == count)
550 return;
551
552 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
553 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
554 saa_writeb(SAA7134_LEVEL_CTRL(task),
555 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
556 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
557 (vals[i].vpfy << 2) | vals[i].vpfy);
558}
559
560static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
561{
562 int val,mirror;
563
564 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
565 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
566
567 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
568 if (yscale < 2048) {
569 /* LPI */
570 dprintk("yscale LPI yscale=%d\n",yscale);
571 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
572 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
573 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
574 } else {
575 /* ACM */
576 val = 0x40 * 1024 / yscale;
577 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
578 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
579 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
580 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
581 }
582 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
583}
584
585static void set_size(struct saa7134_dev *dev, int task,
586 int width, int height, int interlace)
587{
588 int prescale,xscale,yscale,y_even,y_odd;
589 int h_start, h_stop, v_start, v_stop;
590 int div = interlace ? 2 : 1;
591
592 /* setup video scaler */
593 h_start = dev->crop_current.left;
594 v_start = dev->crop_current.top/2;
595 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
596 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
597
598 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
599 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
600 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
601 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
602 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
603 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
604 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
605 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
606
607 prescale = dev->crop_current.width / width;
608 if (0 == prescale)
609 prescale = 1;
610 xscale = 1024 * dev->crop_current.width / prescale / width;
611 yscale = 512 * div * dev->crop_current.height / height;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800612 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 set_h_prescale(dev,task,prescale);
614 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
615 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
616 set_v_scale(dev,task,yscale);
617
618 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
619 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
620 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
621 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
622
623 /* deinterlace y offsets */
624 y_odd = dev->ctl_y_odd;
625 y_even = dev->ctl_y_even;
626 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
627 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
628 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
629 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
630}
631
632/* ------------------------------------------------------------------ */
633
634struct cliplist {
635 __u16 position;
636 __u8 enable;
637 __u8 disable;
638};
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640static void set_cliplist(struct saa7134_dev *dev, int reg,
641 struct cliplist *cl, int entries, char *name)
642{
643 __u8 winbits = 0;
644 int i;
645
646 for (i = 0; i < entries; i++) {
647 winbits |= cl[i].enable;
648 winbits &= ~cl[i].disable;
649 if (i < 15 && cl[i].position == cl[i+1].position)
650 continue;
651 saa_writeb(reg + 0, winbits);
652 saa_writeb(reg + 2, cl[i].position & 0xff);
653 saa_writeb(reg + 3, cl[i].position >> 8);
654 dprintk("clip: %s winbits=%02x pos=%d\n",
655 name,winbits,cl[i].position);
656 reg += 8;
657 }
658 for (; reg < 0x400; reg += 8) {
659 saa_writeb(reg+ 0, 0);
660 saa_writeb(reg + 1, 0);
661 saa_writeb(reg + 2, 0);
662 saa_writeb(reg + 3, 0);
663 }
664}
665
666static int clip_range(int val)
667{
668 if (val < 0)
669 val = 0;
670 return val;
671}
672
Heikki Orsila9040b322007-04-27 12:31:18 -0300673/* Sort into smallest position first order */
674static int cliplist_cmp(const void *a, const void *b)
675{
676 const struct cliplist *cla = a;
677 const struct cliplist *clb = b;
678 if (cla->position < clb->position)
679 return -1;
680 if (cla->position > clb->position)
681 return 1;
682 return 0;
683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
686 int nclips, int interlace)
687{
688 struct cliplist col[16], row[16];
Heikki Orsila9040b322007-04-27 12:31:18 -0300689 int cols = 0, rows = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int div = interlace ? 2 : 1;
691
Heikki Orsila9040b322007-04-27 12:31:18 -0300692 memset(col, 0, sizeof(col));
693 memset(row, 0, sizeof(row));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 for (i = 0; i < nclips && i < 8; i++) {
695 col[cols].position = clip_range(clips[i].c.left);
696 col[cols].enable = (1 << i);
697 cols++;
698 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
699 col[cols].disable = (1 << i);
700 cols++;
701 row[rows].position = clip_range(clips[i].c.top / div);
702 row[rows].enable = (1 << i);
703 rows++;
704 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
705 / div);
706 row[rows].disable = (1 << i);
707 rows++;
708 }
Heikki Orsila9040b322007-04-27 12:31:18 -0300709 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
710 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 set_cliplist(dev,0x380,col,cols,"cols");
712 set_cliplist(dev,0x384,row,rows,"rows");
713 return 0;
714}
715
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300716static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 enum v4l2_field field;
719 int maxw, maxh;
720
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300721 if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -EINVAL;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300723 if (win->w.width < 48)
724 win->w.width = 48;
725 if (win->w.height < 32)
726 win->w.height = 32;
727 if (win->clipcount > 8)
728 win->clipcount = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300730 win->chromakey = 0;
731 win->global_alpha = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 field = win->field;
733 maxw = dev->crop_current.width;
734 maxh = dev->crop_current.height;
735
736 if (V4L2_FIELD_ANY == field) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800737 field = (win->w.height > maxh/2)
738 ? V4L2_FIELD_INTERLACED
739 : V4L2_FIELD_TOP;
740 }
741 switch (field) {
742 case V4L2_FIELD_TOP:
743 case V4L2_FIELD_BOTTOM:
744 maxh = maxh / 2;
745 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800746 default:
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300747 field = V4L2_FIELD_INTERLACED;
748 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 win->field = field;
752 if (win->w.width > maxw)
753 win->w.width = maxw;
754 if (win->w.height > maxh)
755 win->w.height = maxh;
756 return 0;
757}
758
Hans Verkuil718bde12013-12-14 08:28:24 -0300759static int start_preview(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 unsigned long base,control,bpl;
762 int err;
763
Hans Verkuil3a0a5a72013-05-31 11:48:50 -0300764 err = verify_preview(dev, &dev->win, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (0 != err)
766 return err;
767
Hans Verkuilb12262f2013-05-31 08:22:24 -0300768 dev->ovfield = dev->win.field;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
Hans Verkuilb12262f2013-05-31 08:22:24 -0300770 dev->win.w.width, dev->win.w.height,
771 dev->win.w.left, dev->win.w.top,
772 dev->ovfmt->name, v4l2_field_names[dev->ovfield]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /* setup window + clipping */
Hans Verkuilb12262f2013-05-31 08:22:24 -0300775 set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 V4L2_FIELD_HAS_BOTH(dev->ovfield));
Hans Verkuilb12262f2013-05-31 08:22:24 -0300777 setup_clipping(dev, dev->clips, dev->nclips,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 V4L2_FIELD_HAS_BOTH(dev->ovfield));
779 if (dev->ovfmt->yuv)
780 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
781 else
782 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
783 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
784
785 /* dma: setup channel 1 (= Video Task B) */
786 base = (unsigned long)dev->ovbuf.base;
Hans Verkuilb12262f2013-05-31 08:22:24 -0300787 base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
788 base += dev->ovfmt->depth/8 * dev->win.w.left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 bpl = dev->ovbuf.fmt.bytesperline;
790 control = SAA7134_RS_CONTROL_BURST_16;
791 if (dev->ovfmt->bswap)
792 control |= SAA7134_RS_CONTROL_BSWAP;
793 if (dev->ovfmt->wswap)
794 control |= SAA7134_RS_CONTROL_WSWAP;
795 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
796 saa_writel(SAA7134_RS_BA1(1),base);
797 saa_writel(SAA7134_RS_BA2(1),base+bpl);
798 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
799 saa_writel(SAA7134_RS_CONTROL(1),control);
800 } else {
801 saa_writel(SAA7134_RS_BA1(1),base);
802 saa_writel(SAA7134_RS_BA2(1),base);
803 saa_writel(SAA7134_RS_PITCH(1),bpl);
804 saa_writel(SAA7134_RS_CONTROL(1),control);
805 }
806
807 /* start dma */
808 dev->ovenable = 1;
809 saa7134_set_dmabits(dev);
810
811 return 0;
812}
813
Hans Verkuil718bde12013-12-14 08:28:24 -0300814static int stop_preview(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 dev->ovenable = 0;
817 saa7134_set_dmabits(dev);
818 return 0;
819}
820
821/* ------------------------------------------------------------------ */
822
823static int buffer_activate(struct saa7134_dev *dev,
824 struct saa7134_buf *buf,
825 struct saa7134_buf *next)
826{
827 unsigned long base,control,bpl;
828 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
829
830 dprintk("buffer_activate buf=%p\n",buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300831 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 buf->top_seen = 0;
833
Hans Verkuile72936d2014-04-17 03:48:50 -0300834 set_size(dev, TASK_A, buf->vb.width, buf->vb.height,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 V4L2_FIELD_HAS_BOTH(buf->vb.field));
Hans Verkuil9e534f82014-04-17 05:00:39 -0300836 if (dev->fmt->yuv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
838 else
839 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
Hans Verkuil9e534f82014-04-17 05:00:39 -0300840 saa_writeb(SAA7134_OFMT_VIDEO_A, dev->fmt->pm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* DMA: setup channel 0 (= Video Task A0) */
843 base = saa7134_buffer_base(buf);
Hans Verkuil9e534f82014-04-17 05:00:39 -0300844 if (dev->fmt->planar)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 bpl = buf->vb.width;
846 else
Hans Verkuil9e534f82014-04-17 05:00:39 -0300847 bpl = (buf->vb.width * dev->fmt->depth) / 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 control = SAA7134_RS_CONTROL_BURST_16 |
849 SAA7134_RS_CONTROL_ME |
850 (buf->pt->dma >> 12);
Hans Verkuil9e534f82014-04-17 05:00:39 -0300851 if (dev->fmt->bswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 control |= SAA7134_RS_CONTROL_BSWAP;
Hans Verkuil9e534f82014-04-17 05:00:39 -0300853 if (dev->fmt->wswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 control |= SAA7134_RS_CONTROL_WSWAP;
855 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
856 /* interlaced */
857 saa_writel(SAA7134_RS_BA1(0),base);
858 saa_writel(SAA7134_RS_BA2(0),base+bpl);
859 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
860 } else {
861 /* non-interlaced */
862 saa_writel(SAA7134_RS_BA1(0),base);
863 saa_writel(SAA7134_RS_BA2(0),base);
864 saa_writel(SAA7134_RS_PITCH(0),bpl);
865 }
866 saa_writel(SAA7134_RS_CONTROL(0),control);
867
Hans Verkuil9e534f82014-04-17 05:00:39 -0300868 if (dev->fmt->planar) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 /* DMA: setup channel 4+5 (= planar task A) */
Hans Verkuil9e534f82014-04-17 05:00:39 -0300870 bpl_uv = bpl >> dev->fmt->hshift;
871 lines_uv = buf->vb.height >> dev->fmt->vshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 base2 = base + bpl * buf->vb.height;
873 base3 = base2 + bpl_uv * lines_uv;
Hans Verkuil9e534f82014-04-17 05:00:39 -0300874 if (dev->fmt->uvswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 tmp = base2, base2 = base3, base3 = tmp;
876 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
877 bpl_uv,lines_uv,base2,base3);
878 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
879 /* interlaced */
880 saa_writel(SAA7134_RS_BA1(4),base2);
881 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
882 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
883 saa_writel(SAA7134_RS_BA1(5),base3);
884 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
885 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
886 } else {
887 /* non-interlaced */
888 saa_writel(SAA7134_RS_BA1(4),base2);
889 saa_writel(SAA7134_RS_BA2(4),base2);
890 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
891 saa_writel(SAA7134_RS_BA1(5),base3);
892 saa_writel(SAA7134_RS_BA2(5),base3);
893 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
894 }
895 saa_writel(SAA7134_RS_CONTROL(4),control);
896 saa_writel(SAA7134_RS_CONTROL(5),control);
897 }
898
899 /* start DMA */
900 saa7134_set_dmabits(dev);
Hans Verkuile72936d2014-04-17 03:48:50 -0300901 mod_timer(&dev->video_q.timeout, jiffies + BUFFER_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return 0;
903}
904
905static int buffer_prepare(struct videobuf_queue *q,
906 struct videobuf_buffer *vb,
907 enum v4l2_field field)
908{
Hans Verkuil9db0fb12013-12-14 08:28:23 -0300909 struct saa7134_dev *dev = q->priv_data;
Hans Verkuile72936d2014-04-17 03:48:50 -0300910 struct saa7134_buf *buf = container_of(vb, struct saa7134_buf, vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 unsigned int size;
912 int err;
913
914 /* sanity checks */
Hans Verkuil813b9df2013-05-31 08:30:49 -0300915 if (NULL == dev->fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return -EINVAL;
Hans Verkuil813b9df2013-05-31 08:30:49 -0300917 if (dev->width < 48 ||
918 dev->height < 32 ||
919 dev->width/4 > dev->crop_current.width ||
920 dev->height/4 > dev->crop_current.height ||
921 dev->width > dev->crop_bounds.width ||
922 dev->height > dev->crop_bounds.height)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -EINVAL;
Hans Verkuil813b9df2013-05-31 08:30:49 -0300924 size = (dev->width * dev->height * dev->fmt->depth) >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (0 != buf->vb.baddr && buf->vb.bsize < size)
926 return -EINVAL;
927
928 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
Hans Verkuil813b9df2013-05-31 08:30:49 -0300929 vb->i, dev->width, dev->height, size, v4l2_field_names[field],
930 dev->fmt->name);
931 if (buf->vb.width != dev->width ||
932 buf->vb.height != dev->height ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 buf->vb.size != size ||
Hans Verkuil9e534f82014-04-17 05:00:39 -0300934 buf->vb.field != field) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300935 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
Brandon Philips0fc06862007-11-06 20:02:36 -0300938 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300939 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
940
Hans Verkuil813b9df2013-05-31 08:30:49 -0300941 buf->vb.width = dev->width;
942 buf->vb.height = dev->height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 buf->vb.size = size;
944 buf->vb.field = field;
Hans Verkuil9db0fb12013-12-14 08:28:23 -0300945 buf->pt = &dev->pt_cap;
figo.zhangd5709a02009-05-07 23:31:02 -0300946 dev->video_q.curr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300948 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (err)
950 goto oops;
951 err = saa7134_pgtable_build(dev->pci,buf->pt,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300952 dma->sglist,
953 dma->sglen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 saa7134_buffer_startpage(buf));
955 if (err)
956 goto oops;
957 }
Brandon Philips0fc06862007-11-06 20:02:36 -0300958 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 buf->activate = buffer_activate;
960 return 0;
961
962 oops:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300963 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return err;
965}
966
967static int
968buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
969{
Hans Verkuil9db0fb12013-12-14 08:28:23 -0300970 struct saa7134_dev *dev = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Hans Verkuil813b9df2013-05-31 08:30:49 -0300972 *size = dev->fmt->depth * dev->width * dev->height >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (0 == *count)
974 *count = gbuffers;
975 *count = saa7134_buffer_count(*size,*count);
976 return 0;
977}
978
979static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
980{
Hans Verkuil9db0fb12013-12-14 08:28:23 -0300981 struct saa7134_dev *dev = q->priv_data;
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
Hans Verkuil9db0fb12013-12-14 08:28:23 -0300984 saa7134_buffer_queue(dev, &dev->video_q, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
988{
Hans Verkuile72936d2014-04-17 03:48:50 -0300989 struct saa7134_buf *buf = container_of(vb, struct saa7134_buf, vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300991 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994static struct videobuf_queue_ops video_qops = {
995 .buf_setup = buffer_setup,
996 .buf_prepare = buffer_prepare,
997 .buf_queue = buffer_queue,
998 .buf_release = buffer_release,
999};
1000
1001/* ------------------------------------------------------------------ */
1002
Hans Verkuil718bde12013-12-14 08:28:24 -03001003static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Hans Verkuil718bde12013-12-14 08:28:24 -03001005 struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 unsigned long flags;
1007 int restart_overlay = 0;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001008
Hans Verkuil718bde12013-12-14 08:28:24 -03001009 switch (ctrl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 case V4L2_CID_BRIGHTNESS:
Hans Verkuil718bde12013-12-14 08:28:24 -03001011 dev->ctl_bright = ctrl->val;
1012 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 break;
1014 case V4L2_CID_HUE:
Hans Verkuil718bde12013-12-14 08:28:24 -03001015 dev->ctl_hue = ctrl->val;
1016 saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 break;
1018 case V4L2_CID_CONTRAST:
Hans Verkuil718bde12013-12-14 08:28:24 -03001019 dev->ctl_contrast = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1021 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1022 break;
1023 case V4L2_CID_SATURATION:
Hans Verkuil718bde12013-12-14 08:28:24 -03001024 dev->ctl_saturation = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1026 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1027 break;
1028 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil718bde12013-12-14 08:28:24 -03001029 dev->ctl_mute = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 saa7134_tvaudio_setmute(dev);
1031 break;
1032 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuil718bde12013-12-14 08:28:24 -03001033 dev->ctl_volume = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1035 break;
1036 case V4L2_CID_PRIVATE_INVERT:
Hans Verkuil718bde12013-12-14 08:28:24 -03001037 dev->ctl_invert = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1039 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1040 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1041 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1042 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001043 case V4L2_CID_HFLIP:
Hans Verkuil718bde12013-12-14 08:28:24 -03001044 dev->ctl_mirror = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 restart_overlay = 1;
1046 break;
1047 case V4L2_CID_PRIVATE_Y_EVEN:
Hans Verkuil718bde12013-12-14 08:28:24 -03001048 dev->ctl_y_even = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 restart_overlay = 1;
1050 break;
1051 case V4L2_CID_PRIVATE_Y_ODD:
Hans Verkuil718bde12013-12-14 08:28:24 -03001052 dev->ctl_y_odd = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 restart_overlay = 1;
1054 break;
1055 case V4L2_CID_PRIVATE_AUTOMUTE:
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001056 {
1057 struct v4l2_priv_tun_config tda9887_cfg;
1058
1059 tda9887_cfg.tuner = TUNER_TDA9887;
1060 tda9887_cfg.priv = &dev->tda9887_conf;
1061
Hans Verkuil718bde12013-12-14 08:28:24 -03001062 dev->ctl_automute = ctrl->val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (dev->tda9887_conf) {
1064 if (dev->ctl_automute)
1065 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1066 else
1067 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001068
Hans Verkuilfac69862009-01-17 12:17:14 -03001069 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071 break;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 default:
Hans Verkuil718bde12013-12-14 08:28:24 -03001074 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Hans Verkuil718bde12013-12-14 08:28:24 -03001076 if (restart_overlay && res_locked(dev, RESOURCE_OVERLAY)) {
1077 spin_lock_irqsave(&dev->slock, flags);
1078 stop_preview(dev);
1079 start_preview(dev);
1080 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
Hans Verkuil718bde12013-12-14 08:28:24 -03001082 return 0;
Hans Verkuil531d83a2008-07-26 09:04:06 -03001083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085/* ------------------------------------------------------------------ */
1086
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001087static struct videobuf_queue *saa7134_queue(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001089 struct video_device *vdev = video_devdata(file);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001090 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuilce791132013-12-14 08:28:27 -03001091 struct saa7134_fh *fh = file->private_data;
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001092 struct videobuf_queue *q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001094 switch (vdev->vfl_type) {
1095 case VFL_TYPE_GRABBER:
Hans Verkuil5c440462014-05-13 14:24:44 -03001096 q = fh->is_empress ? &dev->empress_vbq : &dev->video_vbq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 break;
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001098 case VFL_TYPE_VBI:
Hans Verkuil5c440462014-05-13 14:24:44 -03001099 q = &dev->vbi_vbq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 break;
1101 default:
1102 BUG();
1103 }
1104 return q;
1105}
1106
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001107static int saa7134_resource(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001109 struct video_device *vdev = video_devdata(file);
Hans Verkuilce791132013-12-14 08:28:27 -03001110 struct saa7134_fh *fh = file->private_data;
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001111
1112 if (vdev->vfl_type == VFL_TYPE_GRABBER)
Hans Verkuilce791132013-12-14 08:28:27 -03001113 return fh->is_empress ? RESOURCE_EMPRESS : RESOURCE_VIDEO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001115 if (vdev->vfl_type == VFL_TYPE_VBI)
Heikki Orsila9040b322007-04-27 12:31:18 -03001116 return RESOURCE_VBI;
1117
1118 BUG();
1119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Hans Verkuilbec43662008-12-30 06:58:20 -03001122static int video_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001124 struct video_device *vdev = video_devdata(file);
1125 struct saa7134_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct saa7134_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001129 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Mauro Carvalho Chehabbefd6e62009-02-09 12:27:03 -03001130 if (NULL == fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return -ENOMEM;
Mauro Carvalho Chehabbefd6e62009-02-09 12:27:03 -03001132
Ondrej Zary3bbaa3a2013-02-01 20:01:16 -03001133 v4l2_fh_init(&fh->fh, vdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 file->private_data = fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001136 if (vdev->vfl_type == VFL_TYPE_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /* switch to radio mode */
Hans Verkuile72936d2014-04-17 03:48:50 -03001138 saa7134_tvaudio_setinput(dev, &card(dev).radio);
Hans Verkuilfac69862009-01-17 12:17:14 -03001139 saa_call_all(dev, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 } else {
1141 /* switch to video/vbi mode */
Hans Verkuile72936d2014-04-17 03:48:50 -03001142 video_mux(dev, dev->ctl_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Ondrej Zary3bbaa3a2013-02-01 20:01:16 -03001144 v4l2_fh_add(&fh->fh);
1145
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
1149static ssize_t
1150video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1151{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001152 struct video_device *vdev = video_devdata(file);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001153 struct saa7134_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct saa7134_fh *fh = file->private_data;
1155
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001156 switch (vdev->vfl_type) {
1157 case VFL_TYPE_GRABBER:
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001158 if (res_locked(dev, RESOURCE_VIDEO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return -EBUSY;
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001160 return videobuf_read_one(saa7134_queue(file),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 data, count, ppos,
1162 file->f_flags & O_NONBLOCK);
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001163 case VFL_TYPE_VBI:
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001164 if (!res_get(dev, fh, RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return -EBUSY;
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001166 return videobuf_read_stream(saa7134_queue(file),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 data, count, ppos, 1,
1168 file->f_flags & O_NONBLOCK);
1169 break;
1170 default:
1171 BUG();
1172 return 0;
1173 }
1174}
1175
1176static unsigned int
1177video_poll(struct file *file, struct poll_table_struct *wait)
1178{
Hans Verkuila2004502013-12-14 08:28:28 -03001179 unsigned long req_events = poll_requested_events(wait);
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001180 struct video_device *vdev = video_devdata(file);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001181 struct saa7134_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 struct saa7134_fh *fh = file->private_data;
1183 struct videobuf_buffer *buf = NULL;
Hans Verkuil5c440462014-05-13 14:24:44 -03001184 struct videobuf_queue *q = &dev->video_vbq;
figo.zhang4a567392009-05-17 23:13:13 -03001185 unsigned int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Hans Verkuila2004502013-12-14 08:28:28 -03001187 if (v4l2_event_pending(&fh->fh))
1188 rc = POLLPRI;
1189 else if (req_events & POLLPRI)
1190 poll_wait(file, &fh->fh.wait, wait);
1191
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001192 if (vdev->vfl_type == VFL_TYPE_VBI)
Hans Verkuil5c440462014-05-13 14:24:44 -03001193 return rc | videobuf_poll_stream(file, &dev->vbi_vbq, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001195 if (res_check(fh, RESOURCE_VIDEO)) {
Hans Verkuil5c440462014-05-13 14:24:44 -03001196 mutex_lock(&q->vb_lock);
1197 if (!list_empty(&q->stream))
1198 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 } else {
Hans Verkuil5c440462014-05-13 14:24:44 -03001200 mutex_lock(&q->vb_lock);
1201 if (UNSET == q->read_off) {
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001202 /* need to capture a new frame */
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001203 if (res_locked(dev, RESOURCE_VIDEO))
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001204 goto err;
Hans Verkuil5c440462014-05-13 14:24:44 -03001205 if (0 != q->ops->buf_prepare(q, q->read_buf, q->field))
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001206 goto err;
Hans Verkuil5c440462014-05-13 14:24:44 -03001207 q->ops->buf_queue(q, q->read_buf);
1208 q->read_off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
Hans Verkuil5c440462014-05-13 14:24:44 -03001210 buf = q->read_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212
1213 if (!buf)
figo.zhang4a567392009-05-17 23:13:13 -03001214 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 poll_wait(file, &buf->done, wait);
Hans Verkuila2004502013-12-14 08:28:28 -03001217 if (buf->state == VIDEOBUF_DONE || buf->state == VIDEOBUF_ERROR)
1218 rc |= POLLIN | POLLRDNORM;
Hans Verkuil5c440462014-05-13 14:24:44 -03001219 mutex_unlock(&q->vb_lock);
figo.zhang4a567392009-05-17 23:13:13 -03001220 return rc;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001221
1222err:
Hans Verkuil5c440462014-05-13 14:24:44 -03001223 mutex_unlock(&q->vb_lock);
Hans Verkuila2004502013-12-14 08:28:28 -03001224 return rc | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Hans Verkuilbec43662008-12-30 06:58:20 -03001227static int video_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001229 struct video_device *vdev = video_devdata(file);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001230 struct saa7134_dev *dev = video_drvdata(file);
1231 struct saa7134_fh *fh = file->private_data;
Hans Verkuilb9218f22010-12-27 12:22:46 -03001232 struct saa6588_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 unsigned long flags;
1234
Stas Sergeeva1dca1e2011-12-03 16:40:45 -03001235 saa7134_tvaudio_close(dev);
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /* turn off overlay */
1238 if (res_check(fh, RESOURCE_OVERLAY)) {
1239 spin_lock_irqsave(&dev->slock,flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001240 stop_preview(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 spin_unlock_irqrestore(&dev->slock,flags);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001242 res_free(dev, fh, RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244
1245 /* stop video capture */
1246 if (res_check(fh, RESOURCE_VIDEO)) {
Simon Farnsworth9c9cff52013-09-20 13:15:28 -03001247 pm_qos_remove_request(&dev->qos_request);
Hans Verkuil5c440462014-05-13 14:24:44 -03001248 videobuf_streamoff(&dev->video_vbq);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001249 res_free(dev, fh, RESOURCE_VIDEO);
Hans Verkuil5c440462014-05-13 14:24:44 -03001250 videobuf_mmap_free(&dev->video_vbq);
1251 INIT_LIST_HEAD(&dev->video_vbq.stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
Hans Verkuil5c440462014-05-13 14:24:44 -03001253 if (dev->video_vbq.read_buf) {
1254 buffer_release(&dev->video_vbq, dev->video_vbq.read_buf);
1255 kfree(dev->video_vbq.read_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
1258 /* stop vbi capture */
1259 if (res_check(fh, RESOURCE_VBI)) {
Hans Verkuil5c440462014-05-13 14:24:44 -03001260 videobuf_stop(&dev->vbi_vbq);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001261 res_free(dev, fh, RESOURCE_VBI);
Hans Verkuil5c440462014-05-13 14:24:44 -03001262 videobuf_mmap_free(&dev->vbi_vbq);
1263 INIT_LIST_HEAD(&dev->vbi_vbq.stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001266 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1267 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1268 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1269 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1270 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1271
Laurent Pinchart622b8282009-10-05 10:48:17 -03001272 saa_call_all(dev, core, s_power, 0);
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001273 if (vdev->vfl_type == VFL_TYPE_RADIO)
Hans Verkuilb9218f22010-12-27 12:22:46 -03001274 saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001275
Ondrej Zary3bbaa3a2013-02-01 20:01:16 -03001276 v4l2_fh_del(&fh->fh);
1277 v4l2_fh_exit(&fh->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 file->private_data = NULL;
1279 kfree(fh);
1280 return 0;
1281}
1282
Heikki Orsila9040b322007-04-27 12:31:18 -03001283static int video_mmap(struct file *file, struct vm_area_struct * vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001285 return videobuf_mmap_mapper(saa7134_queue(file), vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Hans Verkuil2983baf2009-03-29 06:26:27 -03001288static ssize_t radio_read(struct file *file, char __user *data,
1289 size_t count, loff_t *ppos)
1290{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001291 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuilb9218f22010-12-27 12:22:46 -03001292 struct saa6588_command cmd;
Hans Verkuil2983baf2009-03-29 06:26:27 -03001293
1294 cmd.block_count = count/3;
Hans Verkuil09092782013-12-14 08:28:36 -03001295 cmd.nonblocking = file->f_flags & O_NONBLOCK;
Hans Verkuil2983baf2009-03-29 06:26:27 -03001296 cmd.buffer = data;
1297 cmd.instance = file;
1298 cmd.result = -ENODEV;
1299
Hans Verkuilb9218f22010-12-27 12:22:46 -03001300 saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
Hans Verkuil2983baf2009-03-29 06:26:27 -03001301
1302 return cmd.result;
1303}
1304
1305static unsigned int radio_poll(struct file *file, poll_table *wait)
1306{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001307 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuilb9218f22010-12-27 12:22:46 -03001308 struct saa6588_command cmd;
Hans Verkuila2004502013-12-14 08:28:28 -03001309 unsigned int rc = v4l2_ctrl_poll(file, wait);
Hans Verkuil2983baf2009-03-29 06:26:27 -03001310
1311 cmd.instance = file;
1312 cmd.event_list = wait;
Hans Verkuila2004502013-12-14 08:28:28 -03001313 cmd.result = 0;
Hans Verkuilb9218f22010-12-27 12:22:46 -03001314 saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
Hans Verkuil2983baf2009-03-29 06:26:27 -03001315
Hans Verkuila2004502013-12-14 08:28:28 -03001316 return rc | cmd.result;
Hans Verkuil2983baf2009-03-29 06:26:27 -03001317}
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319/* ------------------------------------------------------------------ */
1320
Hans Verkuil78b526a2008-05-28 12:16:41 -03001321static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001322 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001324 struct saa7134_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 struct saa7134_tvnorm *norm = dev->tvnorm;
1326
Ondrej Zaryd0477952013-02-01 20:01:21 -03001327 memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 f->fmt.vbi.sampling_rate = 6750000 * 4;
1329 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1330 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1331 f->fmt.vbi.offset = 64 * 4;
Michael Schimekf246a812005-06-23 22:04:47 -07001332 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1333 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1334 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1336 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1337
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Hans Verkuil78b526a2008-05-28 12:16:41 -03001341static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001342 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001344 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001345
Hans Verkuil813b9df2013-05-31 08:30:49 -03001346 f->fmt.pix.width = dev->width;
1347 f->fmt.pix.height = dev->height;
Hans Verkuil5c440462014-05-13 14:24:44 -03001348 f->fmt.pix.field = dev->video_vbq.field;
Hans Verkuil813b9df2013-05-31 08:30:49 -03001349 f->fmt.pix.pixelformat = dev->fmt->fourcc;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001350 f->fmt.pix.bytesperline =
Hans Verkuil813b9df2013-05-31 08:30:49 -03001351 (f->fmt.pix.width * dev->fmt->depth) >> 3;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001352 f->fmt.pix.sizeimage =
1353 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001354 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1355 f->fmt.pix.priv = 0;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001356 return 0;
1357}
1358
Hans Verkuil78b526a2008-05-28 12:16:41 -03001359static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001360 struct v4l2_format *f)
1361{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001362 struct saa7134_dev *dev = video_drvdata(file);
Emil Goode12d866e2013-06-22 08:02:52 -03001363 struct v4l2_clip __user *clips = f->fmt.win.clips;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001364 u32 clipcount = f->fmt.win.clipcount;
1365 int err = 0;
1366 int i;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001367
1368 if (saa7134_no_overlay > 0) {
1369 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return -EINVAL;
1371 }
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001372 mutex_lock(&dev->lock);
Hans Verkuilb12262f2013-05-31 08:22:24 -03001373 f->fmt.win = dev->win;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001374 f->fmt.win.clips = clips;
1375 if (clips == NULL)
1376 clipcount = 0;
1377 if (dev->nclips < clipcount)
1378 clipcount = dev->nclips;
1379 f->fmt.win.clipcount = clipcount;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001380
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001381 for (i = 0; !err && i < clipcount; i++) {
1382 if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c,
1383 sizeof(struct v4l2_rect)))
1384 err = -EFAULT;
1385 }
1386 mutex_unlock(&dev->lock);
1387
1388 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Hans Verkuil78b526a2008-05-28 12:16:41 -03001391static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001392 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001394 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001395 struct saa7134_format *fmt;
1396 enum v4l2_field field;
1397 unsigned int maxw, maxh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001399 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1400 if (NULL == fmt)
1401 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001403 field = f->fmt.pix.field;
1404 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1405 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001407 if (V4L2_FIELD_ANY == field) {
1408 field = (f->fmt.pix.height > maxh/2)
1409 ? V4L2_FIELD_INTERLACED
1410 : V4L2_FIELD_BOTTOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001412 switch (field) {
1413 case V4L2_FIELD_TOP:
1414 case V4L2_FIELD_BOTTOM:
1415 maxh = maxh / 2;
1416 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 default:
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001418 field = V4L2_FIELD_INTERLACED;
1419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001421
1422 f->fmt.pix.field = field;
Herton Ronaldo Krzesinski0a062032010-03-19 14:58:23 -03001423 if (f->fmt.pix.width < 48)
1424 f->fmt.pix.width = 48;
1425 if (f->fmt.pix.height < 32)
1426 f->fmt.pix.height = 32;
1427 if (f->fmt.pix.width > maxw)
1428 f->fmt.pix.width = maxw;
1429 if (f->fmt.pix.height > maxh)
1430 f->fmt.pix.height = maxh;
1431 f->fmt.pix.width &= ~0x03;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001432 f->fmt.pix.bytesperline =
1433 (f->fmt.pix.width * fmt->depth) >> 3;
1434 f->fmt.pix.sizeimage =
1435 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001436 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1437 f->fmt.pix.priv = 0;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001438
1439 return 0;
1440}
1441
Hans Verkuil78b526a2008-05-28 12:16:41 -03001442static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001443 struct v4l2_format *f)
1444{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001445 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001446
1447 if (saa7134_no_overlay > 0) {
1448 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1449 return -EINVAL;
1450 }
1451
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001452 if (f->fmt.win.clips == NULL)
1453 f->fmt.win.clipcount = 0;
1454 return verify_preview(dev, &f->fmt.win, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Hans Verkuil78b526a2008-05-28 12:16:41 -03001457static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001458 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001460 struct saa7134_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 int err;
1462
Hans Verkuil78b526a2008-05-28 12:16:41 -03001463 err = saa7134_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001464 if (0 != err)
1465 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Hans Verkuile72936d2014-04-17 03:48:50 -03001467 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1468 dev->width = f->fmt.pix.width;
1469 dev->height = f->fmt.pix.height;
Hans Verkuil5c440462014-05-13 14:24:44 -03001470 dev->video_vbq.field = f->fmt.pix.field;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001471 return 0;
1472}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Hans Verkuil78b526a2008-05-28 12:16:41 -03001474static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001475 struct v4l2_format *f)
1476{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001477 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001478 int err;
Steven Rostedta8b1ecf2008-05-06 20:42:29 -07001479 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001481 if (saa7134_no_overlay > 0) {
1482 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return -EINVAL;
1484 }
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03001485 if (f->fmt.win.clips == NULL)
1486 f->fmt.win.clipcount = 0;
1487 err = verify_preview(dev, &f->fmt.win, true);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001488 if (0 != err)
1489 return err;
1490
1491 mutex_lock(&dev->lock);
1492
Hans Verkuilb12262f2013-05-31 08:22:24 -03001493 dev->win = f->fmt.win;
1494 dev->nclips = f->fmt.win.clipcount;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001495
Hans Verkuilb12262f2013-05-31 08:22:24 -03001496 if (copy_from_user(dev->clips, f->fmt.win.clips,
1497 sizeof(struct v4l2_clip) * dev->nclips)) {
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001498 mutex_unlock(&dev->lock);
1499 return -EFAULT;
1500 }
1501
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001502 if (res_check(priv, RESOURCE_OVERLAY)) {
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001503 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001504 stop_preview(dev);
1505 start_preview(dev);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001506 spin_unlock_irqrestore(&dev->slock, flags);
1507 }
1508
1509 mutex_unlock(&dev->lock);
1510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001513int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001514{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001515 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001516 unsigned int n;
1517
1518 n = i->index;
1519 if (n >= SAA7134_INPUT_MAX)
1520 return -EINVAL;
1521 if (NULL == card_in(dev, i->index).name)
1522 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001523 i->index = n;
1524 i->type = V4L2_INPUT_TYPE_CAMERA;
1525 strcpy(i->name, card_in(dev, n).name);
1526 if (card_in(dev, n).tv)
1527 i->type = V4L2_INPUT_TYPE_TUNER;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001528 if (n == dev->ctl_input) {
1529 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1530 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1531
1532 if (0 != (v1 & 0x40))
1533 i->status |= V4L2_IN_ST_NO_H_LOCK;
1534 if (0 != (v2 & 0x40))
Hans Verkuil95075dd2013-12-14 08:28:29 -03001535 i->status |= V4L2_IN_ST_NO_SIGNAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001536 if (0 != (v2 & 0x0e))
1537 i->status |= V4L2_IN_ST_MACROVISION;
1538 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001539 i->std = SAA7134_NORMS;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001540 return 0;
1541}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001542EXPORT_SYMBOL_GPL(saa7134_enum_input);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001543
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001544int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001545{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001546 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001547
1548 *i = dev->ctl_input;
1549 return 0;
1550}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001551EXPORT_SYMBOL_GPL(saa7134_g_input);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001552
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001553int saa7134_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001554{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001555 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001556
Roel Kluinde81c3c2009-07-02 16:09:25 -03001557 if (i >= SAA7134_INPUT_MAX)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001558 return -EINVAL;
1559 if (NULL == card_in(dev, i).name)
1560 return -EINVAL;
1561 mutex_lock(&dev->lock);
1562 video_mux(dev, i);
1563 mutex_unlock(&dev->lock);
1564 return 0;
1565}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001566EXPORT_SYMBOL_GPL(saa7134_s_input);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001567
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001568int saa7134_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001569 struct v4l2_capability *cap)
1570{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001571 struct saa7134_dev *dev = video_drvdata(file);
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03001572 struct video_device *vdev = video_devdata(file);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001573 struct saa7134_fh *fh = priv;
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03001574 u32 radio_caps, video_caps, vbi_caps;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001575
1576 unsigned int tuner_type = dev->tuner_type;
1577
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001578 strcpy(cap->driver, "saa7134");
1579 strlcpy(cap->card, saa7134_boards[dev->board].name,
1580 sizeof(cap->card));
1581 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001582
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03001583 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1584 if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET))
1585 cap->device_caps |= V4L2_CAP_TUNER;
1586
1587 radio_caps = V4L2_CAP_RADIO;
1588 if (dev->has_rds)
1589 radio_caps |= V4L2_CAP_RDS_CAPTURE;
1590
1591 video_caps = V4L2_CAP_VIDEO_CAPTURE;
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001592 if (saa7134_no_overlay <= 0 && !fh->is_empress)
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03001593 video_caps |= V4L2_CAP_VIDEO_OVERLAY;
1594
1595 vbi_caps = V4L2_CAP_VBI_CAPTURE;
1596
1597 switch (vdev->vfl_type) {
1598 case VFL_TYPE_RADIO:
1599 cap->device_caps |= radio_caps;
1600 break;
1601 case VFL_TYPE_GRABBER:
1602 cap->device_caps |= video_caps;
1603 break;
1604 case VFL_TYPE_VBI:
1605 cap->device_caps |= vbi_caps;
1606 break;
1607 }
1608 cap->capabilities = radio_caps | video_caps | vbi_caps |
1609 cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1610 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1611 cap->device_caps &= ~V4L2_CAP_STREAMING;
1612 if (!dev->has_rds)
1613 cap->device_caps &= ~V4L2_CAP_READWRITE;
1614 }
1615
Julia Lawall473d8022010-08-05 17:22:44 -03001616 return 0;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001617}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001618EXPORT_SYMBOL_GPL(saa7134_querycap);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001619
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001620int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001621{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001622 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001623 struct saa7134_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 unsigned long flags;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001625 unsigned int i;
1626 v4l2_std_id fixup;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001627
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001628 if (fh->is_empress && res_locked(dev, RESOURCE_OVERLAY)) {
Hans Verkuil9afb7372008-09-06 06:34:44 -03001629 /* Don't change the std from the mpeg device
1630 if overlay is active. */
1631 return -EBUSY;
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001634 for (i = 0; i < TVNORMS; i++)
Hans Verkuil314527a2013-03-15 06:10:40 -03001635 if (id == tvnorms[i].id)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001636 break;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001637
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001638 if (i == TVNORMS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 for (i = 0; i < TVNORMS; i++)
Hans Verkuil314527a2013-03-15 06:10:40 -03001640 if (id & tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 break;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001642 if (i == TVNORMS)
1643 return -EINVAL;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001644
Hans Verkuil314527a2013-03-15 06:10:40 -03001645 if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001646 if (secam[0] == 'L' || secam[0] == 'l') {
1647 if (secam[1] == 'C' || secam[1] == 'c')
1648 fixup = V4L2_STD_SECAM_LC;
1649 else
1650 fixup = V4L2_STD_SECAM_L;
1651 } else {
1652 if (secam[0] == 'D' || secam[0] == 'd')
1653 fixup = V4L2_STD_SECAM_DK;
1654 else
1655 fixup = V4L2_STD_SECAM;
Hartmut Hackmann17b10a72006-10-02 19:55:07 -03001656 }
Dan Carpenter06e65882010-10-04 16:28:01 -03001657 for (i = 0; i < TVNORMS; i++) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001658 if (fixup == tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 break;
Dan Carpenter06e65882010-10-04 16:28:01 -03001660 }
1661 if (i == TVNORMS)
1662 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001664
Hans Verkuil314527a2013-03-15 06:10:40 -03001665 id = tvnorms[i].id;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001666
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001667 mutex_lock(&dev->lock);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001668 if (!fh->is_empress && res_check(fh, RESOURCE_OVERLAY)) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001669 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001670 stop_preview(dev);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001671 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001673 set_tvnorm(dev, &tvnorms[i]);
1674
1675 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001676 start_preview(dev);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001677 spin_unlock_irqrestore(&dev->slock, flags);
1678 } else
1679 set_tvnorm(dev, &tvnorms[i]);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001680
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001681 saa7134_tvaudio_do_scan(dev);
1682 mutex_unlock(&dev->lock);
1683 return 0;
1684}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001685EXPORT_SYMBOL_GPL(saa7134_s_std);
Hans Verkuil9afb7372008-09-06 06:34:44 -03001686
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001687int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
Hans Verkuil9afb7372008-09-06 06:34:44 -03001688{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001689 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuil9afb7372008-09-06 06:34:44 -03001690
1691 *id = dev->tvnorm->id;
1692 return 0;
1693}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001694EXPORT_SYMBOL_GPL(saa7134_g_std);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001695
Mikhail Domrachev707b7f82014-04-01 09:28:17 -03001696static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
1697{
1698 static v4l2_std_id stds[] = {
1699 V4L2_STD_UNKNOWN,
1700 V4L2_STD_NTSC,
1701 V4L2_STD_PAL,
1702 V4L2_STD_SECAM };
1703
1704 v4l2_std_id result = 0;
1705
1706 u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
1707 u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
1708
1709 if (!(st2 & 0x1)) /* RDCAP == 0 */
1710 result = V4L2_STD_UNKNOWN;
1711 else
1712 result = stds[st1 & 0x03];
1713
1714 return result;
1715}
1716
1717int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
1718{
1719 struct saa7134_dev *dev = video_drvdata(file);
1720 *std &= saa7134_read_std(dev);
1721 return 0;
1722}
1723EXPORT_SYMBOL_GPL(saa7134_querystd);
1724
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001725static int saa7134_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001726 struct v4l2_cropcap *cap)
1727{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001728 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001729
1730 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1731 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1732 return -EINVAL;
1733 cap->bounds = dev->crop_bounds;
1734 cap->defrect = dev->crop_defrect;
1735 cap->pixelaspect.numerator = 1;
1736 cap->pixelaspect.denominator = 1;
1737 if (dev->tvnorm->id & V4L2_STD_525_60) {
1738 cap->pixelaspect.numerator = 11;
1739 cap->pixelaspect.denominator = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001741 if (dev->tvnorm->id & V4L2_STD_625_50) {
1742 cap->pixelaspect.numerator = 54;
1743 cap->pixelaspect.denominator = 59;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745 return 0;
1746}
1747
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001748static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001749{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001750 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001751
1752 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1753 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1754 return -EINVAL;
1755 crop->c = dev->crop_current;
1756 return 0;
1757}
1758
Hans Verkuil4f996592012-09-05 05:10:48 -03001759static int saa7134_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001760{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001761 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001762 struct v4l2_rect *b = &dev->crop_bounds;
Hans Verkuil4f996592012-09-05 05:10:48 -03001763 struct v4l2_rect *c = &dev->crop_current;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001764
1765 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1766 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1767 return -EINVAL;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001768
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001769 if (res_locked(dev, RESOURCE_OVERLAY))
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001770 return -EBUSY;
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001771 if (res_locked(dev, RESOURCE_VIDEO))
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001772 return -EBUSY;
1773
Hans Verkuil4f996592012-09-05 05:10:48 -03001774 *c = crop->c;
1775 if (c->top < b->top)
1776 c->top = b->top;
1777 if (c->top > b->top + b->height)
1778 c->top = b->top + b->height;
1779 if (c->height > b->top - c->top + b->height)
1780 c->height = b->top - c->top + b->height;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001781
Hans Verkuil4f996592012-09-05 05:10:48 -03001782 if (c->left < b->left)
1783 c->left = b->left;
1784 if (c->left > b->left + b->width)
1785 c->left = b->left + b->width;
1786 if (c->width > b->left - c->left + b->width)
1787 c->width = b->left - c->left + b->width;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001788 return 0;
1789}
1790
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001791int saa7134_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001792 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001794 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001795 int n;
1796
1797 if (0 != t->index)
1798 return -EINVAL;
1799 memset(t, 0, sizeof(*t));
Dan Carpenter06e65882010-10-04 16:28:01 -03001800 for (n = 0; n < SAA7134_INPUT_MAX; n++) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001801 if (card_in(dev, n).tv)
1802 break;
Dan Carpenter06e65882010-10-04 16:28:01 -03001803 }
1804 if (n == SAA7134_INPUT_MAX)
1805 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001806 if (NULL != card_in(dev, n).name) {
1807 strcpy(t->name, "Television");
1808 t->type = V4L2_TUNER_ANALOG_TV;
Ondrej Zaryce5bdd52013-02-01 20:01:18 -03001809 saa_call_all(dev, tuner, g_tuner, t);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001810 t->capability = V4L2_TUNER_CAP_NORM |
1811 V4L2_TUNER_CAP_STEREO |
1812 V4L2_TUNER_CAP_LANG1 |
1813 V4L2_TUNER_CAP_LANG2;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001814 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1815 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1816 }
1817 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1818 t->signal = 0xffff;
1819 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001821EXPORT_SYMBOL_GPL(saa7134_g_tuner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001823int saa7134_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001824 const struct v4l2_tuner *t)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001825{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001826 struct saa7134_dev *dev = video_drvdata(file);
Ondrej Zary3bbaa3a2013-02-01 20:01:16 -03001827 int rx, mode;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001828
Ondrej Zaryce5bdd52013-02-01 20:01:18 -03001829 if (0 != t->index)
1830 return -EINVAL;
1831
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001832 mode = dev->thread.mode;
1833 if (UNSET == mode) {
1834 rx = saa7134_tvaudio_getstereo(dev);
Hans Verkuile92ba282012-05-14 10:17:35 -03001835 mode = saa7134_tvaudio_rx2mode(rx);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001836 }
1837 if (mode != t->audmode)
1838 dev->thread.mode = t->audmode;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001839
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001840 return 0;
1841}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001842EXPORT_SYMBOL_GPL(saa7134_s_tuner);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001843
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001844int saa7134_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001845 struct v4l2_frequency *f)
1846{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001847 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001848
Ondrej Zary0a5ea882013-02-01 20:01:17 -03001849 if (0 != f->tuner)
1850 return -EINVAL;
1851
Ondrej Zary0a5ea882013-02-01 20:01:17 -03001852 saa_call_all(dev, tuner, g_frequency, f);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001853
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001854 return 0;
1855}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001856EXPORT_SYMBOL_GPL(saa7134_g_frequency);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001857
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001858int saa7134_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001859 const struct v4l2_frequency *f)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001860{
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001861 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001862
1863 if (0 != f->tuner)
1864 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001865 mutex_lock(&dev->lock);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001866
Hans Verkuilfac69862009-01-17 12:17:14 -03001867 saa_call_all(dev, tuner, s_frequency, f);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001868
1869 saa7134_tvaudio_do_scan(dev);
1870 mutex_unlock(&dev->lock);
1871 return 0;
1872}
Hans Verkuilb93a18d2013-12-14 08:28:25 -03001873EXPORT_SYMBOL_GPL(saa7134_s_frequency);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001874
Hans Verkuil78b526a2008-05-28 12:16:41 -03001875static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001876 struct v4l2_fmtdesc *f)
1877{
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001878 if (f->index >= FORMATS)
1879 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001880
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001881 strlcpy(f->description, formats[f->index].name,
1882 sizeof(f->description));
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001883
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001884 f->pixelformat = formats[f->index].fourcc;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001885
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001886 return 0;
1887}
1888
Hans Verkuil78b526a2008-05-28 12:16:41 -03001889static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001890 struct v4l2_fmtdesc *f)
1891{
1892 if (saa7134_no_overlay > 0) {
1893 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001894 return -EINVAL;
1895 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001896
1897 if ((f->index >= FORMATS) || formats[f->index].planar)
1898 return -EINVAL;
1899
1900 strlcpy(f->description, formats[f->index].name,
1901 sizeof(f->description));
1902
1903 f->pixelformat = formats[f->index].fourcc;
1904
1905 return 0;
1906}
1907
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001908static int saa7134_g_fbuf(struct file *file, void *f,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001909 struct v4l2_framebuffer *fb)
1910{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001911 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001912
1913 *fb = dev->ovbuf;
1914 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1915
1916 return 0;
1917}
1918
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001919static int saa7134_s_fbuf(struct file *file, void *f,
Hans Verkuile6eb28c2012-09-04 10:26:45 -03001920 const struct v4l2_framebuffer *fb)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001921{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001922 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001923 struct saa7134_format *fmt;
1924
1925 if (!capable(CAP_SYS_ADMIN) &&
1926 !capable(CAP_SYS_RAWIO))
1927 return -EPERM;
1928
1929 /* check args */
1930 fmt = format_by_fourcc(fb->fmt.pixelformat);
1931 if (NULL == fmt)
1932 return -EINVAL;
1933
1934 /* ok, accept it */
1935 dev->ovbuf = *fb;
1936 dev->ovfmt = fmt;
1937 if (0 == dev->ovbuf.fmt.bytesperline)
1938 dev->ovbuf.fmt.bytesperline =
1939 dev->ovbuf.fmt.width*fmt->depth/8;
1940 return 0;
1941}
1942
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001943static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001944{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001945 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001946 unsigned long flags;
1947
1948 if (on) {
1949 if (saa7134_no_overlay > 0) {
1950 dprintk("no_overlay\n");
1951 return -EINVAL;
1952 }
1953
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001954 if (!res_get(dev, priv, RESOURCE_OVERLAY))
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001955 return -EBUSY;
1956 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001957 start_preview(dev);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001958 spin_unlock_irqrestore(&dev->slock, flags);
1959 }
1960 if (!on) {
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001961 if (!res_check(priv, RESOURCE_OVERLAY))
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001962 return -EINVAL;
1963 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuil718bde12013-12-14 08:28:24 -03001964 stop_preview(dev);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001965 spin_unlock_irqrestore(&dev->slock, flags);
Hans Verkuilb9f63b22013-12-14 08:28:26 -03001966 res_free(dev, priv, RESOURCE_OVERLAY);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001967 }
1968 return 0;
1969}
1970
Hans Verkuilce791132013-12-14 08:28:27 -03001971int saa7134_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001972 struct v4l2_requestbuffers *p)
1973{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001974 return videobuf_reqbufs(saa7134_queue(file), p);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001975}
Hans Verkuilce791132013-12-14 08:28:27 -03001976EXPORT_SYMBOL_GPL(saa7134_reqbufs);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001977
Hans Verkuilce791132013-12-14 08:28:27 -03001978int saa7134_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001979 struct v4l2_buffer *b)
1980{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001981 return videobuf_querybuf(saa7134_queue(file), b);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001982}
Hans Verkuilce791132013-12-14 08:28:27 -03001983EXPORT_SYMBOL_GPL(saa7134_querybuf);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001984
Hans Verkuilce791132013-12-14 08:28:27 -03001985int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001986{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001987 return videobuf_qbuf(saa7134_queue(file), b);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001988}
Hans Verkuilce791132013-12-14 08:28:27 -03001989EXPORT_SYMBOL_GPL(saa7134_qbuf);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001990
Hans Verkuilce791132013-12-14 08:28:27 -03001991int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001992{
Hans Verkuil8fcd4762013-05-31 07:57:10 -03001993 return videobuf_dqbuf(saa7134_queue(file), b,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001994 file->f_flags & O_NONBLOCK);
1995}
Hans Verkuilce791132013-12-14 08:28:27 -03001996EXPORT_SYMBOL_GPL(saa7134_dqbuf);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001997
Hans Verkuilce791132013-12-14 08:28:27 -03001998int saa7134_streamon(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001999 enum v4l2_buf_type type)
2000{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03002001 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuil8fcd4762013-05-31 07:57:10 -03002002 int res = saa7134_resource(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002003
Hans Verkuilb9f63b22013-12-14 08:28:26 -03002004 if (!res_get(dev, priv, res))
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002005 return -EBUSY;
2006
Simon Farnsworth87739862012-12-10 08:35:09 -03002007 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
2008 * configured conservatively, there's 22 usec of buffering for video.
2009 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
2010 * margin in case the FIFO is configured differently to the datasheet.
2011 * Unfortunately, I lack register-level documentation to check the
2012 * Linux FIFO setup and confirm the perfect value.
2013 */
Hans Verkuilce791132013-12-14 08:28:27 -03002014 if (res != RESOURCE_EMPRESS)
2015 pm_qos_add_request(&dev->qos_request,
2016 PM_QOS_CPU_DMA_LATENCY, 20);
Simon Farnsworth87739862012-12-10 08:35:09 -03002017
Hans Verkuil8fcd4762013-05-31 07:57:10 -03002018 return videobuf_streamon(saa7134_queue(file));
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002019}
Hans Verkuilce791132013-12-14 08:28:27 -03002020EXPORT_SYMBOL_GPL(saa7134_streamon);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002021
Hans Verkuilce791132013-12-14 08:28:27 -03002022int saa7134_streamoff(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002023 enum v4l2_buf_type type)
2024{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03002025 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuil8fcd4762013-05-31 07:57:10 -03002026 int res = saa7134_resource(file);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002027
Hans Verkuilce791132013-12-14 08:28:27 -03002028 if (res != RESOURCE_EMPRESS)
2029 pm_qos_remove_request(&dev->qos_request);
Simon Farnsworth87739862012-12-10 08:35:09 -03002030
Hans Verkuil17e7f1b2014-04-17 07:24:31 -03002031 return videobuf_streamoff(saa7134_queue(file));
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002032}
Hans Verkuilce791132013-12-14 08:28:27 -03002033EXPORT_SYMBOL_GPL(saa7134_streamoff);
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002034
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002035#ifdef CONFIG_VIDEO_ADV_DEBUG
2036static int vidioc_g_register (struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002037 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002038{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03002039 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002040
Hans Verkuilb8300702013-05-29 06:59:59 -03002041 reg->val = saa_readb(reg->reg & 0xffffff);
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002042 reg->size = 1;
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002043 return 0;
2044}
2045
2046static int vidioc_s_register (struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03002047 const struct v4l2_dbg_register *reg)
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002048{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03002049 struct saa7134_dev *dev = video_drvdata(file);
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002050
Hans Verkuilb8300702013-05-29 06:59:59 -03002051 saa_writeb(reg->reg & 0xffffff, reg->val);
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002052 return 0;
2053}
2054#endif
2055
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002056static int radio_g_tuner(struct file *file, void *priv,
2057 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03002059 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002060
2061 if (0 != t->index)
2062 return -EINVAL;
2063
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002064 strcpy(t->name, "Radio");
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002065
Hans Verkuilfac69862009-01-17 12:17:14 -03002066 saa_call_all(dev, tuner, g_tuner, t);
Ondrej Zary82456702013-02-01 20:01:15 -03002067 t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002068 if (dev->input->amux == TV) {
2069 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2070 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2071 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2072 }
2073 return 0;
2074}
2075static int radio_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03002076 const struct v4l2_tuner *t)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002077{
Hans Verkuilb9f63b22013-12-14 08:28:26 -03002078 struct saa7134_dev *dev = video_drvdata(file);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002079
2080 if (0 != t->index)
2081 return -EINVAL;
2082
Hans Verkuilfac69862009-01-17 12:17:14 -03002083 saa_call_all(dev, tuner, s_tuner, t);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002084 return 0;
2085}
2086
Hans Verkuilbec43662008-12-30 06:58:20 -03002087static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 .owner = THIS_MODULE,
2090 .open = video_open,
2091 .release = video_release,
2092 .read = video_read,
2093 .poll = video_poll,
2094 .mmap = video_mmap,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002095 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096};
2097
Hans Verkuila3998102008-07-21 02:57:38 -03002098static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002099 .vidioc_querycap = saa7134_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002100 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
2101 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
2102 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
2103 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
2104 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
2105 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
2106 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
2107 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002108 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2109 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2110 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002111 .vidioc_cropcap = saa7134_cropcap,
2112 .vidioc_reqbufs = saa7134_reqbufs,
2113 .vidioc_querybuf = saa7134_querybuf,
2114 .vidioc_qbuf = saa7134_qbuf,
2115 .vidioc_dqbuf = saa7134_dqbuf,
2116 .vidioc_s_std = saa7134_s_std,
Hans Verkuil9afb7372008-09-06 06:34:44 -03002117 .vidioc_g_std = saa7134_g_std,
Mikhail Domrachev707b7f82014-04-01 09:28:17 -03002118 .vidioc_querystd = saa7134_querystd,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002119 .vidioc_enum_input = saa7134_enum_input,
2120 .vidioc_g_input = saa7134_g_input,
2121 .vidioc_s_input = saa7134_s_input,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002122 .vidioc_streamon = saa7134_streamon,
2123 .vidioc_streamoff = saa7134_streamoff,
2124 .vidioc_g_tuner = saa7134_g_tuner,
2125 .vidioc_s_tuner = saa7134_s_tuner,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002126 .vidioc_g_crop = saa7134_g_crop,
2127 .vidioc_s_crop = saa7134_s_crop,
2128 .vidioc_g_fbuf = saa7134_g_fbuf,
2129 .vidioc_s_fbuf = saa7134_s_fbuf,
2130 .vidioc_overlay = saa7134_overlay,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002131 .vidioc_g_frequency = saa7134_g_frequency,
2132 .vidioc_s_frequency = saa7134_s_frequency,
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002133#ifdef CONFIG_VIDEO_ADV_DEBUG
2134 .vidioc_g_register = vidioc_g_register,
2135 .vidioc_s_register = vidioc_s_register,
2136#endif
Hans Verkuila2004502013-12-14 08:28:28 -03002137 .vidioc_log_status = v4l2_ctrl_log_status,
2138 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2139 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140};
2141
Hans Verkuilbec43662008-12-30 06:58:20 -03002142static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002143 .owner = THIS_MODULE,
2144 .open = video_open,
Hans Verkuil2983baf2009-03-29 06:26:27 -03002145 .read = radio_read,
Hans Verkuila3998102008-07-21 02:57:38 -03002146 .release = video_release,
2147 .ioctl = video_ioctl2,
Hans Verkuil2983baf2009-03-29 06:26:27 -03002148 .poll = radio_poll,
Hans Verkuila3998102008-07-21 02:57:38 -03002149};
2150
2151static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Ondrej Zaryc3b3e0c2013-02-01 20:01:14 -03002152 .vidioc_querycap = saa7134_querycap,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002153 .vidioc_g_tuner = radio_g_tuner,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002154 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002155 .vidioc_g_frequency = saa7134_g_frequency,
2156 .vidioc_s_frequency = saa7134_s_frequency,
Hans Verkuila2004502013-12-14 08:28:28 -03002157 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2158 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159};
2160
Hans Verkuila3998102008-07-21 02:57:38 -03002161/* ----------------------------------------------------------- */
2162/* exported stuff */
2163
2164struct video_device saa7134_video_template = {
2165 .name = "saa7134-video",
Hans Verkuila3998102008-07-21 02:57:38 -03002166 .fops = &video_fops,
2167 .ioctl_ops = &video_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002168 .tvnorms = SAA7134_NORMS,
Hans Verkuila3998102008-07-21 02:57:38 -03002169};
2170
2171struct video_device saa7134_radio_template = {
2172 .name = "saa7134-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002173 .fops = &radio_fops,
2174 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002175};
2176
Hans Verkuil718bde12013-12-14 08:28:24 -03002177static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
2178 .s_ctrl = saa7134_s_ctrl,
2179};
2180
2181static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
2182 .ops = &saa7134_ctrl_ops,
2183 .id = V4L2_CID_PRIVATE_INVERT,
2184 .name = "Invert",
2185 .type = V4L2_CTRL_TYPE_BOOLEAN,
2186 .min = 0,
2187 .max = 1,
2188 .step = 1,
2189};
2190
2191static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
2192 .ops = &saa7134_ctrl_ops,
2193 .id = V4L2_CID_PRIVATE_Y_ODD,
2194 .name = "Y Offset Odd Field",
2195 .type = V4L2_CTRL_TYPE_INTEGER,
2196 .min = 0,
2197 .max = 128,
2198 .step = 1,
2199};
2200
2201static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
2202 .ops = &saa7134_ctrl_ops,
2203 .id = V4L2_CID_PRIVATE_Y_EVEN,
2204 .name = "Y Offset Even Field",
2205 .type = V4L2_CTRL_TYPE_INTEGER,
2206 .min = 0,
2207 .max = 128,
2208 .step = 1,
2209};
2210
2211static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
2212 .ops = &saa7134_ctrl_ops,
2213 .id = V4L2_CID_PRIVATE_AUTOMUTE,
2214 .name = "Automute",
2215 .type = V4L2_CTRL_TYPE_BOOLEAN,
2216 .min = 0,
2217 .max = 1,
2218 .step = 1,
2219 .def = 1,
2220};
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222int saa7134_video_init1(struct saa7134_dev *dev)
2223{
Hans Verkuil718bde12013-12-14 08:28:24 -03002224 struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /* sanitycheck insmod options */
2227 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2228 gbuffers = 2;
Mauro Carvalho Chehab3eeba4a2012-10-27 16:20:27 -03002229 if (gbufsize > gbufsize_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 gbufsize = gbufsize_max;
2231 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2232
Hans Verkuil718bde12013-12-14 08:28:24 -03002233 v4l2_ctrl_handler_init(hdl, 11);
2234 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2235 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
2236 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2237 V4L2_CID_CONTRAST, 0, 127, 1, 68);
2238 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2239 V4L2_CID_SATURATION, 0, 127, 1, 64);
2240 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2241 V4L2_CID_HUE, -128, 127, 1, 0);
2242 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2243 V4L2_CID_HFLIP, 0, 1, 1, 0);
2244 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2245 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
2246 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2247 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
2248 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
2249 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
2250 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
2251 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
2252 if (hdl->error)
2253 return hdl->error;
2254 if (card_has_radio(dev)) {
2255 hdl = &dev->radio_ctrl_handler;
2256 v4l2_ctrl_handler_init(hdl, 2);
2257 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
2258 v4l2_ctrl_radio_filter);
2259 if (hdl->error)
2260 return hdl->error;
2261 }
2262 dev->ctl_mute = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Hans Verkuil718bde12013-12-14 08:28:24 -03002264 if (dev->tda9887_conf && saa7134_ctrl_automute.def)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2266 dev->automute = 0;
2267
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002268 INIT_LIST_HEAD(&dev->video_q.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 init_timer(&dev->video_q.timeout);
2270 dev->video_q.timeout.function = saa7134_buffer_timeout;
2271 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2272 dev->video_q.dev = dev;
Hans Verkuil813b9df2013-05-31 08:30:49 -03002273 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
2274 dev->width = 720;
2275 dev->height = 576;
Hans Verkuil3a0a5a72013-05-31 11:48:50 -03002276 dev->win.w.width = dev->width;
2277 dev->win.w.height = dev->height;
2278 dev->win.field = V4L2_FIELD_INTERLACED;
2279 dev->ovbuf.fmt.width = dev->width;
2280 dev->ovbuf.fmt.height = dev->height;
2281 dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
2282 dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Maxim Levitskycb712012007-09-27 20:34:25 -03002284 if (saa7134_boards[dev->board].video_out)
2285 saa7134_videoport_init(dev);
2286
Hans Verkuil5c440462014-05-13 14:24:44 -03002287 videobuf_queue_sg_init(&dev->video_vbq, &video_qops,
Hans Verkuil9db0fb12013-12-14 08:28:23 -03002288 &dev->pci->dev, &dev->slock,
2289 V4L2_BUF_TYPE_VIDEO_CAPTURE,
2290 V4L2_FIELD_INTERLACED,
2291 sizeof(struct saa7134_buf),
2292 dev, NULL);
Hans Verkuil5c440462014-05-13 14:24:44 -03002293 videobuf_queue_sg_init(&dev->vbi_vbq, &saa7134_vbi_qops,
Hans Verkuil9db0fb12013-12-14 08:28:23 -03002294 &dev->pci->dev, &dev->slock,
2295 V4L2_BUF_TYPE_VBI_CAPTURE,
2296 V4L2_FIELD_SEQ_TB,
2297 sizeof(struct saa7134_buf),
2298 dev, NULL);
2299 saa7134_pgtable_alloc(dev->pci, &dev->pt_cap);
2300 saa7134_pgtable_alloc(dev->pci, &dev->pt_vbi);
2301
Maxim Levitskycb712012007-09-27 20:34:25 -03002302 return 0;
2303}
2304
Hans Verkuil9db0fb12013-12-14 08:28:23 -03002305void saa7134_video_fini(struct saa7134_dev *dev)
2306{
2307 /* free stuff */
2308 saa7134_pgtable_free(dev->pci, &dev->pt_cap);
2309 saa7134_pgtable_free(dev->pci, &dev->pt_vbi);
Hans Verkuil718bde12013-12-14 08:28:24 -03002310 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2311 if (card_has_radio(dev))
2312 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
Hans Verkuil9db0fb12013-12-14 08:28:23 -03002313}
2314
Maxim Levitskycb712012007-09-27 20:34:25 -03002315int saa7134_videoport_init(struct saa7134_dev *dev)
2316{
2317 /* enable video output */
2318 int vo = saa7134_boards[dev->board].video_out;
2319 int video_reg;
2320 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
Dmitry Belimov37df96d2008-06-10 12:43:58 -03002321
2322 /* Configure videoport */
Maxim Levitskycb712012007-09-27 20:34:25 -03002323 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2324 video_reg = video_out[vo][1];
2325 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2326 video_reg &= ~VP_T_CODE_P_INVERTED;
2327 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2328 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
Maxim Levitskycb712012007-09-27 20:34:25 -03002329 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2330 video_reg = video_out[vo][5];
2331 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2332 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2333 if (vid_port_opts & SET_CLOCK_INVERTED)
2334 video_reg |= VP_CLK_CTRL1_INVERTED;
2335 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2336 video_reg = video_out[vo][6];
2337 if (vid_port_opts & SET_VSYNC_OFF) {
2338 video_reg &= ~VP_VS_TYPE_MASK;
2339 video_reg |= VP_VS_TYPE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
Maxim Levitskycb712012007-09-27 20:34:25 -03002341 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2342 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2343 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Dmitry Belimov37df96d2008-06-10 12:43:58 -03002345 /* Start videoport */
2346 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return 0;
2349}
2350
2351int saa7134_video_init2(struct saa7134_dev *dev)
2352{
2353 /* init video hw */
2354 set_tvnorm(dev,&tvnorms[0]);
2355 video_mux(dev,0);
Hans Verkuil718bde12013-12-14 08:28:24 -03002356 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 saa7134_tvaudio_setmute(dev);
2358 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2359 return 0;
2360}
2361
Maxim Levitsky11f70782007-09-27 20:44:39 -03002362void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
2364 static const char *st[] = {
2365 "(no signal)", "NTSC", "PAL", "SECAM" };
2366 u32 st1,st2;
2367
2368 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2369 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2370 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2371 (st1 & 0x40) ? "not locked" : "locked",
2372 (st2 & 0x40) ? "no" : "yes",
2373 st[st1 & 0x03]);
Maxim Levitsky11f70782007-09-27 20:44:39 -03002374 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 if (dev->nosignal) {
2377 /* no video signal -> mute audio */
2378 if (dev->ctl_automute)
2379 dev->automute = 1;
2380 saa7134_tvaudio_setmute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 } else {
2382 /* wake up tvaudio audio carrier scan thread */
2383 saa7134_tvaudio_do_scan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
Maxim Levitsky11f70782007-09-27 20:44:39 -03002385
2386 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2387 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2388 else
2389 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (dev->mops && dev->mops->signal_change)
2392 dev->mops->signal_change(dev);
2393}
2394
Maxim Levitsky11f70782007-09-27 20:44:39 -03002395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2397{
2398 enum v4l2_field field;
2399
2400 spin_lock(&dev->slock);
2401 if (dev->video_q.curr) {
2402 dev->video_fieldcount++;
2403 field = dev->video_q.curr->vb.field;
2404 if (V4L2_FIELD_HAS_BOTH(field)) {
2405 /* make sure we have seen both fields */
2406 if ((status & 0x10) == 0x00) {
2407 dev->video_q.curr->top_seen = 1;
2408 goto done;
2409 }
2410 if (!dev->video_q.curr->top_seen)
2411 goto done;
2412 } else if (field == V4L2_FIELD_TOP) {
2413 if ((status & 0x10) != 0x10)
2414 goto done;
2415 } else if (field == V4L2_FIELD_BOTTOM) {
2416 if ((status & 0x10) != 0x00)
2417 goto done;
2418 }
2419 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
Hans Verkuile72936d2014-04-17 03:48:50 -03002420 saa7134_buffer_finish(dev, &dev->video_q, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
Hans Verkuile72936d2014-04-17 03:48:50 -03002422 saa7134_buffer_next(dev, &dev->video_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424 done:
2425 spin_unlock(&dev->slock);
2426}