blob: 48e1a01718ec37390f72fa8ed731bfaa0d16bc78 [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
30#include "saa7134-reg.h"
31#include "saa7134.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020032#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030034#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080035/* Include V4L1 specific functions. Should be removed soon */
36#include <linux/videodev.h>
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030037#endif
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* ------------------------------------------------------------------ */
40
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -030041unsigned int video_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static unsigned int gbuffers = 8;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030043static unsigned int noninterlaced; /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static unsigned int gbufsize = 720*576*4;
45static unsigned int gbufsize_max = 720*576*4;
Hartmut Hackmann17b10a72006-10-02 19:55:07 -030046static char secam[] = "--";
Linus Torvalds1da177e2005-04-16 15:20:36 -070047module_param(video_debug, int, 0644);
48MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
49module_param(gbuffers, int, 0444);
50MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
51module_param(noninterlaced, int, 0644);
Hartmut Hackmannd5fdd132006-07-15 09:45:34 -030052MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
Hartmut Hackmann17b10a72006-10-02 19:55:07 -030053module_param_string(secam, secam, sizeof(secam), 0644);
54MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -030057#define dprintk(fmt, arg...) if (video_debug&0x04) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
59
60/* ------------------------------------------------------------------ */
Robert W. Boone2f8d4f52005-11-08 21:37:10 -080061/* Defines for Video Output Port Register at address 0x191 */
62
63/* Bit 0: VIP code T bit polarity */
64
65#define VP_T_CODE_P_NON_INVERTED 0x00
66#define VP_T_CODE_P_INVERTED 0x01
67
68/* ------------------------------------------------------------------ */
69/* Defines for Video Output Port Register at address 0x195 */
70
71/* Bit 2: Video output clock delay control */
72
73#define VP_CLK_CTRL2_NOT_DELAYED 0x00
74#define VP_CLK_CTRL2_DELAYED 0x04
75
76/* Bit 1: Video output clock invert control */
77
78#define VP_CLK_CTRL1_NON_INVERTED 0x00
79#define VP_CLK_CTRL1_INVERTED 0x02
80
81/* ------------------------------------------------------------------ */
82/* Defines for Video Output Port Register at address 0x196 */
83
84/* Bits 2 to 0: VSYNC pin video vertical sync type */
85
86#define VP_VS_TYPE_MASK 0x07
87
88#define VP_VS_TYPE_OFF 0x00
89#define VP_VS_TYPE_V123 0x01
90#define VP_VS_TYPE_V_ITU 0x02
91#define VP_VS_TYPE_VGATE_L 0x03
92#define VP_VS_TYPE_RESERVED1 0x04
93#define VP_VS_TYPE_RESERVED2 0x05
94#define VP_VS_TYPE_F_ITU 0x06
95#define VP_VS_TYPE_SC_FID 0x07
96
97/* ------------------------------------------------------------------ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* data structs for video */
99
100static int video_out[][9] = {
101 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
102};
103
104static struct saa7134_format formats[] = {
105 {
106 .name = "8 bpp gray",
107 .fourcc = V4L2_PIX_FMT_GREY,
108 .depth = 8,
109 .pm = 0x06,
110 },{
111 .name = "15 bpp RGB, le",
112 .fourcc = V4L2_PIX_FMT_RGB555,
113 .depth = 16,
114 .pm = 0x13 | 0x80,
115 },{
116 .name = "15 bpp RGB, be",
117 .fourcc = V4L2_PIX_FMT_RGB555X,
118 .depth = 16,
119 .pm = 0x13 | 0x80,
120 .bswap = 1,
121 },{
122 .name = "16 bpp RGB, le",
123 .fourcc = V4L2_PIX_FMT_RGB565,
124 .depth = 16,
125 .pm = 0x10 | 0x80,
126 },{
127 .name = "16 bpp RGB, be",
128 .fourcc = V4L2_PIX_FMT_RGB565X,
129 .depth = 16,
130 .pm = 0x10 | 0x80,
131 .bswap = 1,
132 },{
133 .name = "24 bpp RGB, le",
134 .fourcc = V4L2_PIX_FMT_BGR24,
135 .depth = 24,
136 .pm = 0x11,
137 },{
138 .name = "24 bpp RGB, be",
139 .fourcc = V4L2_PIX_FMT_RGB24,
140 .depth = 24,
141 .pm = 0x11,
142 .bswap = 1,
143 },{
144 .name = "32 bpp RGB, le",
145 .fourcc = V4L2_PIX_FMT_BGR32,
146 .depth = 32,
147 .pm = 0x12,
148 },{
149 .name = "32 bpp RGB, be",
150 .fourcc = V4L2_PIX_FMT_RGB32,
151 .depth = 32,
152 .pm = 0x12,
153 .bswap = 1,
154 .wswap = 1,
155 },{
156 .name = "4:2:2 packed, YUYV",
157 .fourcc = V4L2_PIX_FMT_YUYV,
158 .depth = 16,
159 .pm = 0x00,
160 .bswap = 1,
161 .yuv = 1,
162 },{
163 .name = "4:2:2 packed, UYVY",
164 .fourcc = V4L2_PIX_FMT_UYVY,
165 .depth = 16,
166 .pm = 0x00,
167 .yuv = 1,
168 },{
169 .name = "4:2:2 planar, Y-Cb-Cr",
170 .fourcc = V4L2_PIX_FMT_YUV422P,
171 .depth = 16,
172 .pm = 0x09,
173 .yuv = 1,
174 .planar = 1,
175 .hshift = 1,
176 .vshift = 0,
177 },{
178 .name = "4:2:0 planar, Y-Cb-Cr",
179 .fourcc = V4L2_PIX_FMT_YUV420,
180 .depth = 12,
181 .pm = 0x0a,
182 .yuv = 1,
183 .planar = 1,
184 .hshift = 1,
185 .vshift = 1,
186 },{
187 .name = "4:2:0 planar, Y-Cb-Cr",
188 .fourcc = V4L2_PIX_FMT_YVU420,
189 .depth = 12,
190 .pm = 0x0a,
191 .yuv = 1,
192 .planar = 1,
193 .uvswap = 1,
194 .hshift = 1,
195 .vshift = 1,
196 }
197};
198#define FORMATS ARRAY_SIZE(formats)
199
200#define NORM_625_50 \
201 .h_start = 0, \
202 .h_stop = 719, \
203 .video_v_start = 24, \
204 .video_v_stop = 311, \
Michael Schimekf246a812005-06-23 22:04:47 -0700205 .vbi_v_start_0 = 7, \
206 .vbi_v_stop_0 = 22, \
207 .vbi_v_start_1 = 319, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .src_timing = 4
209
210#define NORM_525_60 \
211 .h_start = 0, \
212 .h_stop = 703, \
Michael Schimekf246a812005-06-23 22:04:47 -0700213 .video_v_start = 23, \
214 .video_v_stop = 262, \
215 .vbi_v_start_0 = 10, \
216 .vbi_v_stop_0 = 21, \
217 .vbi_v_start_1 = 273, \
218 .src_timing = 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220static struct saa7134_tvnorm tvnorms[] = {
221 {
222 .name = "PAL", /* autodetect */
223 .id = V4L2_STD_PAL,
224 NORM_625_50,
225
226 .sync_control = 0x18,
227 .luma_control = 0x40,
228 .chroma_ctrl1 = 0x81,
229 .chroma_gain = 0x2a,
230 .chroma_ctrl2 = 0x06,
231 .vgate_misc = 0x1c,
232
233 },{
234 .name = "PAL-BG",
235 .id = V4L2_STD_PAL_BG,
236 NORM_625_50,
237
238 .sync_control = 0x18,
239 .luma_control = 0x40,
240 .chroma_ctrl1 = 0x81,
241 .chroma_gain = 0x2a,
242 .chroma_ctrl2 = 0x06,
243 .vgate_misc = 0x1c,
244
245 },{
246 .name = "PAL-I",
247 .id = V4L2_STD_PAL_I,
248 NORM_625_50,
249
250 .sync_control = 0x18,
251 .luma_control = 0x40,
252 .chroma_ctrl1 = 0x81,
253 .chroma_gain = 0x2a,
254 .chroma_ctrl2 = 0x06,
255 .vgate_misc = 0x1c,
256
257 },{
258 .name = "PAL-DK",
259 .id = V4L2_STD_PAL_DK,
260 NORM_625_50,
261
262 .sync_control = 0x18,
263 .luma_control = 0x40,
264 .chroma_ctrl1 = 0x81,
265 .chroma_gain = 0x2a,
266 .chroma_ctrl2 = 0x06,
267 .vgate_misc = 0x1c,
268
269 },{
270 .name = "NTSC",
271 .id = V4L2_STD_NTSC,
272 NORM_525_60,
273
274 .sync_control = 0x59,
275 .luma_control = 0x40,
276 .chroma_ctrl1 = 0x89,
277 .chroma_gain = 0x2a,
278 .chroma_ctrl2 = 0x0e,
279 .vgate_misc = 0x18,
280
281 },{
282 .name = "SECAM",
283 .id = V4L2_STD_SECAM,
284 NORM_625_50,
285
Hartmut Hackmann17b10a72006-10-02 19:55:07 -0300286 .sync_control = 0x18,
287 .luma_control = 0x1b,
288 .chroma_ctrl1 = 0xd1,
289 .chroma_gain = 0x80,
290 .chroma_ctrl2 = 0x00,
291 .vgate_misc = 0x1c,
292
293 },{
294 .name = "SECAM-DK",
295 .id = V4L2_STD_SECAM_DK,
296 NORM_625_50,
297
298 .sync_control = 0x18,
299 .luma_control = 0x1b,
300 .chroma_ctrl1 = 0xd1,
301 .chroma_gain = 0x80,
302 .chroma_ctrl2 = 0x00,
303 .vgate_misc = 0x1c,
304
305 },{
306 .name = "SECAM-L",
307 .id = V4L2_STD_SECAM_L,
308 NORM_625_50,
309
310 .sync_control = 0x18,
311 .luma_control = 0x1b,
312 .chroma_ctrl1 = 0xd1,
313 .chroma_gain = 0x80,
314 .chroma_ctrl2 = 0x00,
315 .vgate_misc = 0x1c,
316
317 },{
318 .name = "SECAM-Lc",
319 .id = V4L2_STD_SECAM_LC,
320 NORM_625_50,
321
322 .sync_control = 0x18,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .luma_control = 0x1b,
324 .chroma_ctrl1 = 0xd1,
325 .chroma_gain = 0x80,
326 .chroma_ctrl2 = 0x00,
327 .vgate_misc = 0x1c,
328
329 },{
330 .name = "PAL-M",
331 .id = V4L2_STD_PAL_M,
332 NORM_525_60,
333
334 .sync_control = 0x59,
335 .luma_control = 0x40,
336 .chroma_ctrl1 = 0xb9,
337 .chroma_gain = 0x2a,
338 .chroma_ctrl2 = 0x0e,
339 .vgate_misc = 0x18,
340
341 },{
342 .name = "PAL-Nc",
343 .id = V4L2_STD_PAL_Nc,
344 NORM_625_50,
345
346 .sync_control = 0x18,
347 .luma_control = 0x40,
348 .chroma_ctrl1 = 0xa1,
349 .chroma_gain = 0x2a,
350 .chroma_ctrl2 = 0x06,
351 .vgate_misc = 0x1c,
352
353 },{
354 .name = "PAL-60",
355 .id = V4L2_STD_PAL_60,
356
357 .h_start = 0,
358 .h_stop = 719,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800359 .video_v_start = 23,
360 .video_v_stop = 262,
361 .vbi_v_start_0 = 10,
362 .vbi_v_stop_0 = 21,
363 .vbi_v_start_1 = 273,
364 .src_timing = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 .sync_control = 0x18,
367 .luma_control = 0x40,
368 .chroma_ctrl1 = 0x81,
369 .chroma_gain = 0x2a,
370 .chroma_ctrl2 = 0x06,
371 .vgate_misc = 0x1c,
372 }
373};
374#define TVNORMS ARRAY_SIZE(tvnorms)
375
376#define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
377#define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
378#define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
379#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
380#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
381
382static const struct v4l2_queryctrl no_ctrl = {
383 .name = "42",
384 .flags = V4L2_CTRL_FLAG_DISABLED,
385};
386static const struct v4l2_queryctrl video_ctrls[] = {
387 /* --- video --- */
388 {
389 .id = V4L2_CID_BRIGHTNESS,
390 .name = "Brightness",
391 .minimum = 0,
392 .maximum = 255,
393 .step = 1,
394 .default_value = 128,
395 .type = V4L2_CTRL_TYPE_INTEGER,
396 },{
397 .id = V4L2_CID_CONTRAST,
398 .name = "Contrast",
399 .minimum = 0,
400 .maximum = 127,
401 .step = 1,
402 .default_value = 68,
403 .type = V4L2_CTRL_TYPE_INTEGER,
404 },{
405 .id = V4L2_CID_SATURATION,
406 .name = "Saturation",
407 .minimum = 0,
408 .maximum = 127,
409 .step = 1,
410 .default_value = 64,
411 .type = V4L2_CTRL_TYPE_INTEGER,
412 },{
413 .id = V4L2_CID_HUE,
414 .name = "Hue",
415 .minimum = -128,
416 .maximum = 127,
417 .step = 1,
418 .default_value = 0,
419 .type = V4L2_CTRL_TYPE_INTEGER,
420 },{
Michael Schimekf246a812005-06-23 22:04:47 -0700421 .id = V4L2_CID_HFLIP,
422 .name = "Mirror",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 .minimum = 0,
424 .maximum = 1,
425 .type = V4L2_CTRL_TYPE_BOOLEAN,
426 },
427 /* --- audio --- */
428 {
429 .id = V4L2_CID_AUDIO_MUTE,
430 .name = "Mute",
431 .minimum = 0,
432 .maximum = 1,
433 .type = V4L2_CTRL_TYPE_BOOLEAN,
434 },{
435 .id = V4L2_CID_AUDIO_VOLUME,
436 .name = "Volume",
437 .minimum = -15,
438 .maximum = 15,
439 .step = 1,
440 .default_value = 0,
441 .type = V4L2_CTRL_TYPE_INTEGER,
442 },
443 /* --- private --- */
444 {
445 .id = V4L2_CID_PRIVATE_INVERT,
446 .name = "Invert",
447 .minimum = 0,
448 .maximum = 1,
449 .type = V4L2_CTRL_TYPE_BOOLEAN,
450 },{
451 .id = V4L2_CID_PRIVATE_Y_ODD,
452 .name = "y offset odd field",
453 .minimum = 0,
454 .maximum = 128,
455 .default_value = 0,
456 .type = V4L2_CTRL_TYPE_INTEGER,
457 },{
458 .id = V4L2_CID_PRIVATE_Y_EVEN,
459 .name = "y offset even field",
460 .minimum = 0,
461 .maximum = 128,
462 .default_value = 0,
463 .type = V4L2_CTRL_TYPE_INTEGER,
464 },{
465 .id = V4L2_CID_PRIVATE_AUTOMUTE,
466 .name = "automute",
467 .minimum = 0,
468 .maximum = 1,
469 .default_value = 1,
470 .type = V4L2_CTRL_TYPE_BOOLEAN,
471 }
472};
473static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
474
475static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
476{
477 unsigned int i;
478
479 for (i = 0; i < CTRLS; i++)
480 if (video_ctrls[i].id == id)
481 return video_ctrls+i;
482 return NULL;
483}
484
485static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
486{
487 unsigned int i;
488
489 for (i = 0; i < FORMATS; i++)
490 if (formats[i].fourcc == fourcc)
491 return formats+i;
492 return NULL;
493}
494
495/* ----------------------------------------------------------------------- */
496/* resource management */
497
498static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
499{
500 if (fh->resources & bit)
501 /* have it already allocated */
502 return 1;
503
504 /* is it free? */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200505 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (dev->resources & bit) {
507 /* no, someone else uses it */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200508 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return 0;
510 }
511 /* it's free, grab it */
512 fh->resources |= bit;
513 dev->resources |= bit;
514 dprintk("res: get %d\n",bit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200515 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return 1;
517}
518
Heikki Orsila9040b322007-04-27 12:31:18 -0300519static int res_check(struct saa7134_fh *fh, unsigned int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 return (fh->resources & bit);
522}
523
Heikki Orsila9040b322007-04-27 12:31:18 -0300524static int res_locked(struct saa7134_dev *dev, unsigned int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 return (dev->resources & bit);
527}
528
529static
530void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
531{
Eric Sesterhennae246012006-03-13 13:17:11 -0300532 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Ingo Molnar3593cab2006-02-07 06:49:14 -0200534 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 fh->resources &= ~bits;
536 dev->resources &= ~bits;
537 dprintk("res: put %d\n",bits);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200538 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541/* ------------------------------------------------------------------ */
542
Adrian Bunkb02044d2007-10-24 13:23:14 -0300543static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 dprintk("set tv norm = %s\n",norm->name);
546 dev->tvnorm = norm;
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* setup cropping */
549 dev->crop_bounds.left = norm->h_start;
550 dev->crop_defrect.left = norm->h_start;
551 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
552 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
553
Michael Schimekf246a812005-06-23 22:04:47 -0700554 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 dev->crop_defrect.top = norm->video_v_start*2;
556 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
557 - dev->crop_bounds.top;
558 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
559
560 dev->crop_current = dev->crop_defrect;
561
Maxim Levitskyc4584732007-10-12 00:57:15 -0300562 saa7134_set_tvnorm_hw(dev);
Maxim Levitskycb712012007-09-27 20:34:25 -0300563}
564
565static void video_mux(struct saa7134_dev *dev, int input)
566{
567 dprintk("video input = %d [%s]\n", input, card_in(dev, input).name);
568 dev->ctl_input = input;
569 set_tvnorm(dev, dev->tvnorm);
570 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
571}
572
Maxim Levitskyc4584732007-10-12 00:57:15 -0300573
574static void saa7134_set_decoder(struct saa7134_dev *dev)
Maxim Levitskycb712012007-09-27 20:34:25 -0300575{
576 int luma_control, sync_control, mux;
577
578 struct saa7134_tvnorm *norm = dev->tvnorm;
579 mux = card_in(dev, dev->ctl_input).vmux;
580
581 luma_control = norm->luma_control;
582 sync_control = norm->sync_control;
583
584 if (mux > 5)
585 luma_control |= 0x80; /* svideo */
586 if (noninterlaced || dev->nosignal)
587 sync_control |= 0x20;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* setup video decoder */
590 saa_writeb(SAA7134_INCR_DELAY, 0x08);
591 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
592 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
593
594 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
595 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
596 saa_writeb(SAA7134_HSYNC_START, 0xeb);
597 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
598 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
599
600 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
601 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
602 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Maxim Levitskyf5a1ac62007-09-27 20:34:20 -0300604 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
605 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
606
607 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
608 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
611 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
612 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
613
614 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
615 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
616
617 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
618 saa_writeb(SAA7134_VGATE_START, 0x11);
619 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
620 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
621 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
622 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Maxim Levitskyc4584732007-10-12 00:57:15 -0300625void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
626{
627 saa7134_set_decoder(dev);
628
Hartmut Hackmann7bff4b42008-04-22 14:46:08 -0300629 if (card_in(dev, dev->ctl_input).tv)
Maxim Levitskyc4584732007-10-12 00:57:15 -0300630 saa7134_i2c_call_clients(dev, VIDIOC_S_STD, &dev->tvnorm->id);
Maxim Levitskyc4584732007-10-12 00:57:15 -0300631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
634{
635 static const struct {
636 int xpsc;
637 int xacl;
638 int xc2_1;
639 int xdcg;
640 int vpfy;
641 } vals[] = {
642 /* XPSC XACL XC2_1 XDCG VPFY */
643 { 1, 0, 0, 0, 0 },
644 { 2, 2, 1, 2, 2 },
645 { 3, 4, 1, 3, 2 },
646 { 4, 8, 1, 4, 2 },
647 { 5, 8, 1, 4, 2 },
648 { 6, 8, 1, 4, 3 },
649 { 7, 8, 1, 4, 3 },
650 { 8, 15, 0, 4, 3 },
651 { 9, 15, 0, 4, 3 },
652 { 10, 16, 1, 5, 3 },
653 };
654 static const int count = ARRAY_SIZE(vals);
655 int i;
656
657 for (i = 0; i < count; i++)
658 if (vals[i].xpsc == prescale)
659 break;
660 if (i == count)
661 return;
662
663 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
664 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
665 saa_writeb(SAA7134_LEVEL_CTRL(task),
666 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
667 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
668 (vals[i].vpfy << 2) | vals[i].vpfy);
669}
670
671static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
672{
673 int val,mirror;
674
675 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
676 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
677
678 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
679 if (yscale < 2048) {
680 /* LPI */
681 dprintk("yscale LPI yscale=%d\n",yscale);
682 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
683 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
684 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
685 } else {
686 /* ACM */
687 val = 0x40 * 1024 / yscale;
688 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
689 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
690 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
691 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
692 }
693 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
694}
695
696static void set_size(struct saa7134_dev *dev, int task,
697 int width, int height, int interlace)
698{
699 int prescale,xscale,yscale,y_even,y_odd;
700 int h_start, h_stop, v_start, v_stop;
701 int div = interlace ? 2 : 1;
702
703 /* setup video scaler */
704 h_start = dev->crop_current.left;
705 v_start = dev->crop_current.top/2;
706 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
707 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
708
709 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
710 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
711 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
712 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
713 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
714 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
715 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
716 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
717
718 prescale = dev->crop_current.width / width;
719 if (0 == prescale)
720 prescale = 1;
721 xscale = 1024 * dev->crop_current.width / prescale / width;
722 yscale = 512 * div * dev->crop_current.height / height;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800723 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 set_h_prescale(dev,task,prescale);
725 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
726 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
727 set_v_scale(dev,task,yscale);
728
729 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
730 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
731 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
732 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
733
734 /* deinterlace y offsets */
735 y_odd = dev->ctl_y_odd;
736 y_even = dev->ctl_y_even;
737 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
738 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
739 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
740 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
741}
742
743/* ------------------------------------------------------------------ */
744
745struct cliplist {
746 __u16 position;
747 __u8 enable;
748 __u8 disable;
749};
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751static void set_cliplist(struct saa7134_dev *dev, int reg,
752 struct cliplist *cl, int entries, char *name)
753{
754 __u8 winbits = 0;
755 int i;
756
757 for (i = 0; i < entries; i++) {
758 winbits |= cl[i].enable;
759 winbits &= ~cl[i].disable;
760 if (i < 15 && cl[i].position == cl[i+1].position)
761 continue;
762 saa_writeb(reg + 0, winbits);
763 saa_writeb(reg + 2, cl[i].position & 0xff);
764 saa_writeb(reg + 3, cl[i].position >> 8);
765 dprintk("clip: %s winbits=%02x pos=%d\n",
766 name,winbits,cl[i].position);
767 reg += 8;
768 }
769 for (; reg < 0x400; reg += 8) {
770 saa_writeb(reg+ 0, 0);
771 saa_writeb(reg + 1, 0);
772 saa_writeb(reg + 2, 0);
773 saa_writeb(reg + 3, 0);
774 }
775}
776
777static int clip_range(int val)
778{
779 if (val < 0)
780 val = 0;
781 return val;
782}
783
Heikki Orsila9040b322007-04-27 12:31:18 -0300784/* Sort into smallest position first order */
785static int cliplist_cmp(const void *a, const void *b)
786{
787 const struct cliplist *cla = a;
788 const struct cliplist *clb = b;
789 if (cla->position < clb->position)
790 return -1;
791 if (cla->position > clb->position)
792 return 1;
793 return 0;
794}
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
797 int nclips, int interlace)
798{
799 struct cliplist col[16], row[16];
Heikki Orsila9040b322007-04-27 12:31:18 -0300800 int cols = 0, rows = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 int div = interlace ? 2 : 1;
802
Heikki Orsila9040b322007-04-27 12:31:18 -0300803 memset(col, 0, sizeof(col));
804 memset(row, 0, sizeof(row));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 for (i = 0; i < nclips && i < 8; i++) {
806 col[cols].position = clip_range(clips[i].c.left);
807 col[cols].enable = (1 << i);
808 cols++;
809 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
810 col[cols].disable = (1 << i);
811 cols++;
812 row[rows].position = clip_range(clips[i].c.top / div);
813 row[rows].enable = (1 << i);
814 rows++;
815 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
816 / div);
817 row[rows].disable = (1 << i);
818 rows++;
819 }
Heikki Orsila9040b322007-04-27 12:31:18 -0300820 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
821 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 set_cliplist(dev,0x380,col,cols,"cols");
823 set_cliplist(dev,0x384,row,rows,"rows");
824 return 0;
825}
826
827static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
828{
829 enum v4l2_field field;
830 int maxw, maxh;
831
832 if (NULL == dev->ovbuf.base)
833 return -EINVAL;
834 if (NULL == dev->ovfmt)
835 return -EINVAL;
836 if (win->w.width < 48 || win->w.height < 32)
837 return -EINVAL;
838 if (win->clipcount > 2048)
839 return -EINVAL;
840
841 field = win->field;
842 maxw = dev->crop_current.width;
843 maxh = dev->crop_current.height;
844
845 if (V4L2_FIELD_ANY == field) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800846 field = (win->w.height > maxh/2)
847 ? V4L2_FIELD_INTERLACED
848 : V4L2_FIELD_TOP;
849 }
850 switch (field) {
851 case V4L2_FIELD_TOP:
852 case V4L2_FIELD_BOTTOM:
853 maxh = maxh / 2;
854 break;
855 case V4L2_FIELD_INTERLACED:
856 break;
857 default:
858 return -EINVAL;
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 win->field = field;
862 if (win->w.width > maxw)
863 win->w.width = maxw;
864 if (win->w.height > maxh)
865 win->w.height = maxh;
866 return 0;
867}
868
869static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
870{
871 unsigned long base,control,bpl;
872 int err;
873
874 err = verify_preview(dev,&fh->win);
875 if (0 != err)
876 return err;
877
878 dev->ovfield = fh->win.field;
879 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
880 fh->win.w.width,fh->win.w.height,
881 fh->win.w.left,fh->win.w.top,
882 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
883
884 /* setup window + clipping */
885 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
886 V4L2_FIELD_HAS_BOTH(dev->ovfield));
887 setup_clipping(dev,fh->clips,fh->nclips,
888 V4L2_FIELD_HAS_BOTH(dev->ovfield));
889 if (dev->ovfmt->yuv)
890 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
891 else
892 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
893 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
894
895 /* dma: setup channel 1 (= Video Task B) */
896 base = (unsigned long)dev->ovbuf.base;
897 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
898 base += dev->ovfmt->depth/8 * fh->win.w.left;
899 bpl = dev->ovbuf.fmt.bytesperline;
900 control = SAA7134_RS_CONTROL_BURST_16;
901 if (dev->ovfmt->bswap)
902 control |= SAA7134_RS_CONTROL_BSWAP;
903 if (dev->ovfmt->wswap)
904 control |= SAA7134_RS_CONTROL_WSWAP;
905 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
906 saa_writel(SAA7134_RS_BA1(1),base);
907 saa_writel(SAA7134_RS_BA2(1),base+bpl);
908 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
909 saa_writel(SAA7134_RS_CONTROL(1),control);
910 } else {
911 saa_writel(SAA7134_RS_BA1(1),base);
912 saa_writel(SAA7134_RS_BA2(1),base);
913 saa_writel(SAA7134_RS_PITCH(1),bpl);
914 saa_writel(SAA7134_RS_CONTROL(1),control);
915 }
916
917 /* start dma */
918 dev->ovenable = 1;
919 saa7134_set_dmabits(dev);
920
921 return 0;
922}
923
924static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
925{
926 dev->ovenable = 0;
927 saa7134_set_dmabits(dev);
928 return 0;
929}
930
931/* ------------------------------------------------------------------ */
932
933static int buffer_activate(struct saa7134_dev *dev,
934 struct saa7134_buf *buf,
935 struct saa7134_buf *next)
936{
937 unsigned long base,control,bpl;
938 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
939
940 dprintk("buffer_activate buf=%p\n",buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300941 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 buf->top_seen = 0;
943
944 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
945 V4L2_FIELD_HAS_BOTH(buf->vb.field));
946 if (buf->fmt->yuv)
947 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
948 else
949 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
950 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
951
952 /* DMA: setup channel 0 (= Video Task A0) */
953 base = saa7134_buffer_base(buf);
954 if (buf->fmt->planar)
955 bpl = buf->vb.width;
956 else
957 bpl = (buf->vb.width * buf->fmt->depth) / 8;
958 control = SAA7134_RS_CONTROL_BURST_16 |
959 SAA7134_RS_CONTROL_ME |
960 (buf->pt->dma >> 12);
961 if (buf->fmt->bswap)
962 control |= SAA7134_RS_CONTROL_BSWAP;
963 if (buf->fmt->wswap)
964 control |= SAA7134_RS_CONTROL_WSWAP;
965 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
966 /* interlaced */
967 saa_writel(SAA7134_RS_BA1(0),base);
968 saa_writel(SAA7134_RS_BA2(0),base+bpl);
969 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
970 } else {
971 /* non-interlaced */
972 saa_writel(SAA7134_RS_BA1(0),base);
973 saa_writel(SAA7134_RS_BA2(0),base);
974 saa_writel(SAA7134_RS_PITCH(0),bpl);
975 }
976 saa_writel(SAA7134_RS_CONTROL(0),control);
977
978 if (buf->fmt->planar) {
979 /* DMA: setup channel 4+5 (= planar task A) */
980 bpl_uv = bpl >> buf->fmt->hshift;
981 lines_uv = buf->vb.height >> buf->fmt->vshift;
982 base2 = base + bpl * buf->vb.height;
983 base3 = base2 + bpl_uv * lines_uv;
984 if (buf->fmt->uvswap)
985 tmp = base2, base2 = base3, base3 = tmp;
986 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
987 bpl_uv,lines_uv,base2,base3);
988 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
989 /* interlaced */
990 saa_writel(SAA7134_RS_BA1(4),base2);
991 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
992 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
993 saa_writel(SAA7134_RS_BA1(5),base3);
994 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
995 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
996 } else {
997 /* non-interlaced */
998 saa_writel(SAA7134_RS_BA1(4),base2);
999 saa_writel(SAA7134_RS_BA2(4),base2);
1000 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
1001 saa_writel(SAA7134_RS_BA1(5),base3);
1002 saa_writel(SAA7134_RS_BA2(5),base3);
1003 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
1004 }
1005 saa_writel(SAA7134_RS_CONTROL(4),control);
1006 saa_writel(SAA7134_RS_CONTROL(5),control);
1007 }
1008
1009 /* start DMA */
1010 saa7134_set_dmabits(dev);
1011 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1012 return 0;
1013}
1014
1015static int buffer_prepare(struct videobuf_queue *q,
1016 struct videobuf_buffer *vb,
1017 enum v4l2_field field)
1018{
1019 struct saa7134_fh *fh = q->priv_data;
1020 struct saa7134_dev *dev = fh->dev;
1021 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1022 unsigned int size;
1023 int err;
1024
1025 /* sanity checks */
1026 if (NULL == fh->fmt)
1027 return -EINVAL;
1028 if (fh->width < 48 ||
1029 fh->height < 32 ||
1030 fh->width/4 > dev->crop_current.width ||
1031 fh->height/4 > dev->crop_current.height ||
1032 fh->width > dev->crop_bounds.width ||
1033 fh->height > dev->crop_bounds.height)
1034 return -EINVAL;
1035 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1036 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1037 return -EINVAL;
1038
1039 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1040 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1041 fh->fmt->name);
1042 if (buf->vb.width != fh->width ||
1043 buf->vb.height != fh->height ||
1044 buf->vb.size != size ||
1045 buf->vb.field != field ||
1046 buf->fmt != fh->fmt) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001047 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049
Brandon Philips0fc06862007-11-06 20:02:36 -03001050 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001051 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 buf->vb.width = fh->width;
1054 buf->vb.height = fh->height;
1055 buf->vb.size = size;
1056 buf->vb.field = field;
1057 buf->fmt = fh->fmt;
1058 buf->pt = &fh->pt_cap;
1059
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001060 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (err)
1062 goto oops;
1063 err = saa7134_pgtable_build(dev->pci,buf->pt,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001064 dma->sglist,
1065 dma->sglen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 saa7134_buffer_startpage(buf));
1067 if (err)
1068 goto oops;
1069 }
Brandon Philips0fc06862007-11-06 20:02:36 -03001070 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 buf->activate = buffer_activate;
1072 return 0;
1073
1074 oops:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001075 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return err;
1077}
1078
1079static int
1080buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1081{
1082 struct saa7134_fh *fh = q->priv_data;
1083
1084 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1085 if (0 == *count)
1086 *count = gbuffers;
1087 *count = saa7134_buffer_count(*size,*count);
1088 return 0;
1089}
1090
1091static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1092{
1093 struct saa7134_fh *fh = q->priv_data;
1094 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1095
1096 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1097}
1098
1099static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1102
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001103 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106static struct videobuf_queue_ops video_qops = {
1107 .buf_setup = buffer_setup,
1108 .buf_prepare = buffer_prepare,
1109 .buf_queue = buffer_queue,
1110 .buf_release = buffer_release,
1111};
1112
1113/* ------------------------------------------------------------------ */
1114
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -03001115int saa7134_g_ctrl(struct file *file, void *priv, struct v4l2_control *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001117 struct saa7134_fh *fh = priv;
1118 struct saa7134_dev *dev = fh->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 const struct v4l2_queryctrl* ctrl;
1120
1121 ctrl = ctrl_by_id(c->id);
1122 if (NULL == ctrl)
1123 return -EINVAL;
1124 switch (c->id) {
1125 case V4L2_CID_BRIGHTNESS:
1126 c->value = dev->ctl_bright;
1127 break;
1128 case V4L2_CID_HUE:
1129 c->value = dev->ctl_hue;
1130 break;
1131 case V4L2_CID_CONTRAST:
1132 c->value = dev->ctl_contrast;
1133 break;
1134 case V4L2_CID_SATURATION:
1135 c->value = dev->ctl_saturation;
1136 break;
1137 case V4L2_CID_AUDIO_MUTE:
1138 c->value = dev->ctl_mute;
1139 break;
1140 case V4L2_CID_AUDIO_VOLUME:
1141 c->value = dev->ctl_volume;
1142 break;
1143 case V4L2_CID_PRIVATE_INVERT:
1144 c->value = dev->ctl_invert;
1145 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001146 case V4L2_CID_HFLIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 c->value = dev->ctl_mirror;
1148 break;
1149 case V4L2_CID_PRIVATE_Y_EVEN:
1150 c->value = dev->ctl_y_even;
1151 break;
1152 case V4L2_CID_PRIVATE_Y_ODD:
1153 c->value = dev->ctl_y_odd;
1154 break;
1155 case V4L2_CID_PRIVATE_AUTOMUTE:
1156 c->value = dev->ctl_automute;
1157 break;
1158 default:
1159 return -EINVAL;
1160 }
1161 return 0;
1162}
Mauro Carvalho Chehabf6e90a62007-12-11 15:01:57 -03001163EXPORT_SYMBOL_GPL(saa7134_g_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -03001165int saa7134_s_ctrl(struct file *file, void *f, struct v4l2_control *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 const struct v4l2_queryctrl* ctrl;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001168 struct saa7134_fh *fh = f;
1169 struct saa7134_dev *dev = fh->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 unsigned long flags;
1171 int restart_overlay = 0;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001172 int err = -EINVAL;
1173
1174 err = v4l2_prio_check(&dev->prio, &fh->prio);
1175 if (0 != err)
1176 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001178 mutex_lock(&dev->lock);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 ctrl = ctrl_by_id(c->id);
1181 if (NULL == ctrl)
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001182 goto error;
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1185 switch (ctrl->type) {
1186 case V4L2_CTRL_TYPE_BOOLEAN:
1187 case V4L2_CTRL_TYPE_MENU:
1188 case V4L2_CTRL_TYPE_INTEGER:
1189 if (c->value < ctrl->minimum)
1190 c->value = ctrl->minimum;
1191 if (c->value > ctrl->maximum)
1192 c->value = ctrl->maximum;
1193 break;
1194 default:
1195 /* nothing */;
1196 };
1197 switch (c->id) {
1198 case V4L2_CID_BRIGHTNESS:
1199 dev->ctl_bright = c->value;
1200 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1201 break;
1202 case V4L2_CID_HUE:
1203 dev->ctl_hue = c->value;
1204 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1205 break;
1206 case V4L2_CID_CONTRAST:
1207 dev->ctl_contrast = c->value;
1208 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1209 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1210 break;
1211 case V4L2_CID_SATURATION:
1212 dev->ctl_saturation = c->value;
1213 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1214 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1215 break;
1216 case V4L2_CID_AUDIO_MUTE:
1217 dev->ctl_mute = c->value;
1218 saa7134_tvaudio_setmute(dev);
1219 break;
1220 case V4L2_CID_AUDIO_VOLUME:
1221 dev->ctl_volume = c->value;
1222 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1223 break;
1224 case V4L2_CID_PRIVATE_INVERT:
1225 dev->ctl_invert = c->value;
1226 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1227 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1228 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1229 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1230 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001231 case V4L2_CID_HFLIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 dev->ctl_mirror = c->value;
1233 restart_overlay = 1;
1234 break;
1235 case V4L2_CID_PRIVATE_Y_EVEN:
1236 dev->ctl_y_even = c->value;
1237 restart_overlay = 1;
1238 break;
1239 case V4L2_CID_PRIVATE_Y_ODD:
1240 dev->ctl_y_odd = c->value;
1241 restart_overlay = 1;
1242 break;
1243 case V4L2_CID_PRIVATE_AUTOMUTE:
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001244 {
1245 struct v4l2_priv_tun_config tda9887_cfg;
1246
1247 tda9887_cfg.tuner = TUNER_TDA9887;
1248 tda9887_cfg.priv = &dev->tda9887_conf;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 dev->ctl_automute = c->value;
1251 if (dev->tda9887_conf) {
1252 if (dev->ctl_automute)
1253 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1254 else
1255 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001256
1257 saa7134_i2c_call_clients(dev, TUNER_SET_CONFIG,
1258 &tda9887_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260 break;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 default:
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001263 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1266 spin_lock_irqsave(&dev->slock,flags);
1267 stop_preview(dev,fh);
1268 start_preview(dev,fh);
1269 spin_unlock_irqrestore(&dev->slock,flags);
1270 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001271 err = 0;
1272
1273error:
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001274 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001275 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
Mauro Carvalho Chehabf6e90a62007-12-11 15:01:57 -03001277EXPORT_SYMBOL_GPL(saa7134_s_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279/* ------------------------------------------------------------------ */
1280
1281static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1282{
1283 struct videobuf_queue* q = NULL;
1284
1285 switch (fh->type) {
1286 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1287 q = &fh->cap;
1288 break;
1289 case V4L2_BUF_TYPE_VBI_CAPTURE:
1290 q = &fh->vbi;
1291 break;
1292 default:
1293 BUG();
1294 }
1295 return q;
1296}
1297
1298static int saa7134_resource(struct saa7134_fh *fh)
1299{
Heikki Orsila9040b322007-04-27 12:31:18 -03001300 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1301 return RESOURCE_VIDEO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Heikki Orsila9040b322007-04-27 12:31:18 -03001303 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1304 return RESOURCE_VBI;
1305
1306 BUG();
1307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
1310static int video_open(struct inode *inode, struct file *file)
1311{
1312 int minor = iminor(inode);
Trent Piepho6d28e982007-10-10 05:37:42 -03001313 struct saa7134_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 struct saa7134_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1316 int radio = 0;
Trent Piepho6d28e982007-10-10 05:37:42 -03001317 list_for_each_entry(dev, &saa7134_devlist, devlist) {
1318 if (dev->video_dev && (dev->video_dev->minor == minor))
1319 goto found;
1320 if (dev->radio_dev && (dev->radio_dev->minor == minor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 radio = 1;
Trent Piepho6d28e982007-10-10 05:37:42 -03001322 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
Trent Piepho6d28e982007-10-10 05:37:42 -03001324 if (dev->vbi_dev && (dev->vbi_dev->minor == minor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 type = V4L2_BUF_TYPE_VBI_CAPTURE;
Trent Piepho6d28e982007-10-10 05:37:42 -03001326 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328 }
Trent Piepho6d28e982007-10-10 05:37:42 -03001329 return -ENODEV;
1330 found:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1333 v4l2_type_names[type]);
1334
1335 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001336 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (NULL == fh)
1338 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 file->private_data = fh;
1340 fh->dev = dev;
1341 fh->radio = radio;
1342 fh->type = type;
1343 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1344 fh->width = 720;
1345 fh->height = 576;
1346 v4l2_prio_open(&dev->prio,&fh->prio);
1347
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03001348 videobuf_queue_sg_init(&fh->cap, &video_qops,
1349 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1351 V4L2_FIELD_INTERLACED,
1352 sizeof(struct saa7134_buf),
1353 fh);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03001354 videobuf_queue_sg_init(&fh->vbi, &saa7134_vbi_qops,
1355 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 V4L2_BUF_TYPE_VBI_CAPTURE,
1357 V4L2_FIELD_SEQ_TB,
1358 sizeof(struct saa7134_buf),
1359 fh);
1360 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1361 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1362
1363 if (fh->radio) {
1364 /* switch to radio mode */
1365 saa7134_tvaudio_setinput(dev,&card(dev).radio);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001366 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 } else {
1368 /* switch to video/vbi mode */
1369 video_mux(dev,dev->ctl_input);
1370 }
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001371 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
1374static ssize_t
1375video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1376{
1377 struct saa7134_fh *fh = file->private_data;
1378
1379 switch (fh->type) {
1380 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1381 if (res_locked(fh->dev,RESOURCE_VIDEO))
1382 return -EBUSY;
1383 return videobuf_read_one(saa7134_queue(fh),
1384 data, count, ppos,
1385 file->f_flags & O_NONBLOCK);
1386 case V4L2_BUF_TYPE_VBI_CAPTURE:
1387 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1388 return -EBUSY;
1389 return videobuf_read_stream(saa7134_queue(fh),
1390 data, count, ppos, 1,
1391 file->f_flags & O_NONBLOCK);
1392 break;
1393 default:
1394 BUG();
1395 return 0;
1396 }
1397}
1398
1399static unsigned int
1400video_poll(struct file *file, struct poll_table_struct *wait)
1401{
1402 struct saa7134_fh *fh = file->private_data;
1403 struct videobuf_buffer *buf = NULL;
1404
1405 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1406 return videobuf_poll_stream(file, &fh->vbi, wait);
1407
1408 if (res_check(fh,RESOURCE_VIDEO)) {
1409 if (!list_empty(&fh->cap.stream))
1410 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1411 } else {
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001412 mutex_lock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (UNSET == fh->cap.read_off) {
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001414 /* need to capture a new frame */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001415 if (res_locked(fh->dev,RESOURCE_VIDEO))
1416 goto err;
1417 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field))
1418 goto err;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001419 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1420 fh->cap.read_off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001422 mutex_unlock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 buf = fh->cap.read_buf;
1424 }
1425
1426 if (!buf)
1427 return POLLERR;
1428
1429 poll_wait(file, &buf->done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03001430 if (buf->state == VIDEOBUF_DONE ||
1431 buf->state == VIDEOBUF_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return POLLIN|POLLRDNORM;
1433 return 0;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001434
1435err:
1436 mutex_unlock(&fh->cap.vb_lock);
1437 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
1440static int video_release(struct inode *inode, struct file *file)
1441{
1442 struct saa7134_fh *fh = file->private_data;
1443 struct saa7134_dev *dev = fh->dev;
1444 unsigned long flags;
1445
1446 /* turn off overlay */
1447 if (res_check(fh, RESOURCE_OVERLAY)) {
1448 spin_lock_irqsave(&dev->slock,flags);
1449 stop_preview(dev,fh);
1450 spin_unlock_irqrestore(&dev->slock,flags);
1451 res_free(dev,fh,RESOURCE_OVERLAY);
1452 }
1453
1454 /* stop video capture */
1455 if (res_check(fh, RESOURCE_VIDEO)) {
1456 videobuf_streamoff(&fh->cap);
1457 res_free(dev,fh,RESOURCE_VIDEO);
1458 }
1459 if (fh->cap.read_buf) {
1460 buffer_release(&fh->cap,fh->cap.read_buf);
1461 kfree(fh->cap.read_buf);
1462 }
1463
1464 /* stop vbi capture */
1465 if (res_check(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03001466 videobuf_stop(&fh->vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 res_free(dev,fh,RESOURCE_VBI);
1468 }
1469
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001470 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1471 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1472 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1473 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1474 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1475
Mauro Carvalho Chehab2f180712005-09-09 13:03:46 -07001476 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /* free stuff */
1479 videobuf_mmap_free(&fh->cap);
1480 videobuf_mmap_free(&fh->vbi);
1481 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1482 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1483
1484 v4l2_prio_close(&dev->prio,&fh->prio);
1485 file->private_data = NULL;
1486 kfree(fh);
1487 return 0;
1488}
1489
Heikki Orsila9040b322007-04-27 12:31:18 -03001490static int video_mmap(struct file *file, struct vm_area_struct * vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 struct saa7134_fh *fh = file->private_data;
1493
1494 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1495}
1496
1497/* ------------------------------------------------------------------ */
1498
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001499static int saa7134_try_get_set_fmt_vbi(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001500 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001502 struct saa7134_fh *fh = priv;
1503 struct saa7134_dev *dev = fh->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 struct saa7134_tvnorm *norm = dev->tvnorm;
1505
1506 f->fmt.vbi.sampling_rate = 6750000 * 4;
1507 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1508 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1509 f->fmt.vbi.offset = 64 * 4;
Michael Schimekf246a812005-06-23 22:04:47 -07001510 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1511 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1512 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1514 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1515
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001519static int saa7134_g_fmt_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001520 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001522 struct saa7134_fh *fh = priv;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001523
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001524 f->fmt.pix.width = fh->width;
1525 f->fmt.pix.height = fh->height;
1526 f->fmt.pix.field = fh->cap.field;
1527 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1528 f->fmt.pix.bytesperline =
1529 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1530 f->fmt.pix.sizeimage =
1531 f->fmt.pix.height * f->fmt.pix.bytesperline;
1532 return 0;
1533}
1534
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001535static int saa7134_g_fmt_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001536 struct v4l2_format *f)
1537{
1538 struct saa7134_fh *fh = priv;
1539
1540 if (saa7134_no_overlay > 0) {
1541 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return -EINVAL;
1543 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001544 f->fmt.win = fh->win;
1545
1546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001549static int saa7134_try_fmt_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001550 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001552 struct saa7134_fh *fh = priv;
1553 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001554 struct saa7134_format *fmt;
1555 enum v4l2_field field;
1556 unsigned int maxw, maxh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001558 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1559 if (NULL == fmt)
1560 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001562 field = f->fmt.pix.field;
1563 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1564 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001566 if (V4L2_FIELD_ANY == field) {
1567 field = (f->fmt.pix.height > maxh/2)
1568 ? V4L2_FIELD_INTERLACED
1569 : V4L2_FIELD_BOTTOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001571 switch (field) {
1572 case V4L2_FIELD_TOP:
1573 case V4L2_FIELD_BOTTOM:
1574 maxh = maxh / 2;
1575 break;
1576 case V4L2_FIELD_INTERLACED:
1577 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 default:
1579 return -EINVAL;
1580 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001581
1582 f->fmt.pix.field = field;
1583 if (f->fmt.pix.width < 48)
1584 f->fmt.pix.width = 48;
1585 if (f->fmt.pix.height < 32)
1586 f->fmt.pix.height = 32;
1587 if (f->fmt.pix.width > maxw)
1588 f->fmt.pix.width = maxw;
1589 if (f->fmt.pix.height > maxh)
1590 f->fmt.pix.height = maxh;
1591 f->fmt.pix.width &= ~0x03;
1592 f->fmt.pix.bytesperline =
1593 (f->fmt.pix.width * fmt->depth) >> 3;
1594 f->fmt.pix.sizeimage =
1595 f->fmt.pix.height * f->fmt.pix.bytesperline;
1596
1597 return 0;
1598}
1599
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001600static int saa7134_try_fmt_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001601 struct v4l2_format *f)
1602{
1603 struct saa7134_fh *fh = priv;
1604 struct saa7134_dev *dev = fh->dev;
1605
1606 if (saa7134_no_overlay > 0) {
1607 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1608 return -EINVAL;
1609 }
1610
1611 return verify_preview(dev, &f->fmt.win);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001614static int saa7134_s_fmt_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001615 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001617 struct saa7134_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 int err;
1619
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001620 err = saa7134_try_fmt_cap(file, priv, f);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001621 if (0 != err)
1622 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001624 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1625 fh->width = f->fmt.pix.width;
1626 fh->height = f->fmt.pix.height;
1627 fh->cap.field = f->fmt.pix.field;
1628 return 0;
1629}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001631static int saa7134_s_fmt_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001632 struct v4l2_format *f)
1633{
1634 struct saa7134_fh *fh = priv;
1635 struct saa7134_dev *dev = fh->dev;
1636 int err;
Steven Rostedta8b1ecf2008-05-06 20:42:29 -07001637 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001639 if (saa7134_no_overlay > 0) {
1640 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return -EINVAL;
1642 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001643 err = verify_preview(dev, &f->fmt.win);
1644 if (0 != err)
1645 return err;
1646
1647 mutex_lock(&dev->lock);
1648
1649 fh->win = f->fmt.win;
1650 fh->nclips = f->fmt.win.clipcount;
1651
1652 if (fh->nclips > 8)
1653 fh->nclips = 8;
1654
1655 if (copy_from_user(fh->clips, f->fmt.win.clips,
1656 sizeof(struct v4l2_clip)*fh->nclips)) {
1657 mutex_unlock(&dev->lock);
1658 return -EFAULT;
1659 }
1660
1661 if (res_check(fh, RESOURCE_OVERLAY)) {
1662 spin_lock_irqsave(&dev->slock, flags);
1663 stop_preview(dev, fh);
1664 start_preview(dev, fh);
1665 spin_unlock_irqrestore(&dev->slock, flags);
1666 }
1667
1668 mutex_unlock(&dev->lock);
1669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -03001672int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001673{
1674 const struct v4l2_queryctrl *ctrl;
1675
1676 if ((c->id < V4L2_CID_BASE ||
1677 c->id >= V4L2_CID_LASTP1) &&
1678 (c->id < V4L2_CID_PRIVATE_BASE ||
1679 c->id >= V4L2_CID_PRIVATE_LASTP1))
1680 return -EINVAL;
1681 ctrl = ctrl_by_id(c->id);
1682 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1683 return 0;
1684}
Mauro Carvalho Chehabf6e90a62007-12-11 15:01:57 -03001685EXPORT_SYMBOL_GPL(saa7134_queryctrl);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001686
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001687static int saa7134_enum_input(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001688 struct v4l2_input *i)
1689{
1690 struct saa7134_fh *fh = priv;
1691 struct saa7134_dev *dev = fh->dev;
1692 unsigned int n;
1693
1694 n = i->index;
1695 if (n >= SAA7134_INPUT_MAX)
1696 return -EINVAL;
1697 if (NULL == card_in(dev, i->index).name)
1698 return -EINVAL;
1699 memset(i, 0, sizeof(*i));
1700 i->index = n;
1701 i->type = V4L2_INPUT_TYPE_CAMERA;
1702 strcpy(i->name, card_in(dev, n).name);
1703 if (card_in(dev, n).tv)
1704 i->type = V4L2_INPUT_TYPE_TUNER;
1705 i->audioset = 1;
1706 if (n == dev->ctl_input) {
1707 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1708 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1709
1710 if (0 != (v1 & 0x40))
1711 i->status |= V4L2_IN_ST_NO_H_LOCK;
1712 if (0 != (v2 & 0x40))
1713 i->status |= V4L2_IN_ST_NO_SYNC;
1714 if (0 != (v2 & 0x0e))
1715 i->status |= V4L2_IN_ST_MACROVISION;
1716 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001717 i->std = SAA7134_NORMS;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001718 return 0;
1719}
1720
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001721static int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001722{
1723 struct saa7134_fh *fh = priv;
1724 struct saa7134_dev *dev = fh->dev;
1725
1726 *i = dev->ctl_input;
1727 return 0;
1728}
1729
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001730static int saa7134_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001731{
1732 struct saa7134_fh *fh = priv;
1733 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001734 int err;
1735
1736 err = v4l2_prio_check(&dev->prio, &fh->prio);
1737 if (0 != err)
1738 return err;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001739
1740 if (i < 0 || i >= SAA7134_INPUT_MAX)
1741 return -EINVAL;
1742 if (NULL == card_in(dev, i).name)
1743 return -EINVAL;
1744 mutex_lock(&dev->lock);
1745 video_mux(dev, i);
1746 mutex_unlock(&dev->lock);
1747 return 0;
1748}
1749
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001750static int saa7134_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001751 struct v4l2_capability *cap)
1752{
1753 struct saa7134_fh *fh = priv;
1754 struct saa7134_dev *dev = fh->dev;
1755
1756 unsigned int tuner_type = dev->tuner_type;
1757
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001758 strcpy(cap->driver, "saa7134");
1759 strlcpy(cap->card, saa7134_boards[dev->board].name,
1760 sizeof(cap->card));
1761 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1762 cap->version = SAA7134_VERSION_CODE;
1763 cap->capabilities =
1764 V4L2_CAP_VIDEO_CAPTURE |
1765 V4L2_CAP_VBI_CAPTURE |
1766 V4L2_CAP_READWRITE |
1767 V4L2_CAP_STREAMING |
1768 V4L2_CAP_TUNER;
1769 if (saa7134_no_overlay <= 0)
1770 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1771
1772 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1773 cap->capabilities &= ~V4L2_CAP_TUNER;
1774 return 0;
1775}
1776
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001777static int saa7134_s_std(struct file *file, void *priv, v4l2_std_id *id)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001778{
1779 struct saa7134_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 struct saa7134_dev *dev = fh->dev;
1781 unsigned long flags;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001782 unsigned int i;
1783 v4l2_std_id fixup;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001784 int err;
1785
1786 err = v4l2_prio_check(&dev->prio, &fh->prio);
1787 if (0 != err)
1788 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001790 for (i = 0; i < TVNORMS; i++)
1791 if (*id == tvnorms[i].id)
1792 break;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001793
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001794 if (i == TVNORMS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 for (i = 0; i < TVNORMS; i++)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001796 if (*id & tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 break;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001798 if (i == TVNORMS)
1799 return -EINVAL;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001800
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001801 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1802 if (secam[0] == 'L' || secam[0] == 'l') {
1803 if (secam[1] == 'C' || secam[1] == 'c')
1804 fixup = V4L2_STD_SECAM_LC;
1805 else
1806 fixup = V4L2_STD_SECAM_L;
1807 } else {
1808 if (secam[0] == 'D' || secam[0] == 'd')
1809 fixup = V4L2_STD_SECAM_DK;
1810 else
1811 fixup = V4L2_STD_SECAM;
Hartmut Hackmann17b10a72006-10-02 19:55:07 -03001812 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001813 for (i = 0; i < TVNORMS; i++)
1814 if (fixup == tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001817
1818 *id = tvnorms[i].id;
1819
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001820 mutex_lock(&dev->lock);
1821 if (res_check(fh, RESOURCE_OVERLAY)) {
1822 spin_lock_irqsave(&dev->slock, flags);
1823 stop_preview(dev, fh);
1824 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001826 set_tvnorm(dev, &tvnorms[i]);
1827
1828 spin_lock_irqsave(&dev->slock, flags);
1829 start_preview(dev, fh);
1830 spin_unlock_irqrestore(&dev->slock, flags);
1831 } else
1832 set_tvnorm(dev, &tvnorms[i]);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001833
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001834 saa7134_tvaudio_do_scan(dev);
1835 mutex_unlock(&dev->lock);
1836 return 0;
1837}
1838
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001839static int saa7134_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001840 struct v4l2_cropcap *cap)
1841{
1842 struct saa7134_fh *fh = priv;
1843 struct saa7134_dev *dev = fh->dev;
1844
1845 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1846 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1847 return -EINVAL;
1848 cap->bounds = dev->crop_bounds;
1849 cap->defrect = dev->crop_defrect;
1850 cap->pixelaspect.numerator = 1;
1851 cap->pixelaspect.denominator = 1;
1852 if (dev->tvnorm->id & V4L2_STD_525_60) {
1853 cap->pixelaspect.numerator = 11;
1854 cap->pixelaspect.denominator = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001856 if (dev->tvnorm->id & V4L2_STD_625_50) {
1857 cap->pixelaspect.numerator = 54;
1858 cap->pixelaspect.denominator = 59;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860 return 0;
1861}
1862
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001863static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001864{
1865 struct saa7134_fh *fh = f;
1866 struct saa7134_dev *dev = fh->dev;
1867
1868 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1869 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1870 return -EINVAL;
1871 crop->c = dev->crop_current;
1872 return 0;
1873}
1874
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001875static int saa7134_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001876{
1877 struct saa7134_fh *fh = f;
1878 struct saa7134_dev *dev = fh->dev;
1879 struct v4l2_rect *b = &dev->crop_bounds;
1880
1881 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1882 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1883 return -EINVAL;
1884 if (crop->c.height < 0)
1885 return -EINVAL;
1886 if (crop->c.width < 0)
1887 return -EINVAL;
1888
1889 if (res_locked(fh->dev, RESOURCE_OVERLAY))
1890 return -EBUSY;
1891 if (res_locked(fh->dev, RESOURCE_VIDEO))
1892 return -EBUSY;
1893
1894 if (crop->c.top < b->top)
1895 crop->c.top = b->top;
1896 if (crop->c.top > b->top + b->height)
1897 crop->c.top = b->top + b->height;
1898 if (crop->c.height > b->top - crop->c.top + b->height)
1899 crop->c.height = b->top - crop->c.top + b->height;
1900
1901 if (crop->c.left < b->left)
1902 crop->c.left = b->left;
1903 if (crop->c.left > b->left + b->width)
1904 crop->c.left = b->left + b->width;
1905 if (crop->c.width > b->left - crop->c.left + b->width)
1906 crop->c.width = b->left - crop->c.left + b->width;
1907
1908 dev->crop_current = crop->c;
1909 return 0;
1910}
1911
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001912static int saa7134_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001913 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001915 struct saa7134_fh *fh = priv;
1916 struct saa7134_dev *dev = fh->dev;
1917 int n;
1918
1919 if (0 != t->index)
1920 return -EINVAL;
1921 memset(t, 0, sizeof(*t));
1922 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1923 if (card_in(dev, n).tv)
1924 break;
1925 if (NULL != card_in(dev, n).name) {
1926 strcpy(t->name, "Television");
1927 t->type = V4L2_TUNER_ANALOG_TV;
1928 t->capability = V4L2_TUNER_CAP_NORM |
1929 V4L2_TUNER_CAP_STEREO |
1930 V4L2_TUNER_CAP_LANG1 |
1931 V4L2_TUNER_CAP_LANG2;
1932 t->rangehigh = 0xffffffffUL;
1933 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1934 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1935 }
1936 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1937 t->signal = 0xffff;
1938 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001941static int saa7134_s_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001942 struct v4l2_tuner *t)
1943{
1944 struct saa7134_fh *fh = priv;
1945 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001946 int rx, mode, err;
1947
1948 err = v4l2_prio_check(&dev->prio, &fh->prio);
1949 if (0 != err)
1950 return err;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001951
1952 mode = dev->thread.mode;
1953 if (UNSET == mode) {
1954 rx = saa7134_tvaudio_getstereo(dev);
1955 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1956 }
1957 if (mode != t->audmode)
1958 dev->thread.mode = t->audmode;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001959
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001960 return 0;
1961}
1962
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001963static int saa7134_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001964 struct v4l2_frequency *f)
1965{
1966 struct saa7134_fh *fh = priv;
1967 struct saa7134_dev *dev = fh->dev;
1968
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001969 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1970 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001971
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001972 return 0;
1973}
1974
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001975static int saa7134_s_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001976 struct v4l2_frequency *f)
1977{
1978 struct saa7134_fh *fh = priv;
1979 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001980 int err;
1981
1982 err = v4l2_prio_check(&dev->prio, &fh->prio);
1983 if (0 != err)
1984 return err;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001985
1986 if (0 != f->tuner)
1987 return -EINVAL;
1988 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1989 return -EINVAL;
1990 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1991 return -EINVAL;
1992 mutex_lock(&dev->lock);
1993 dev->ctl_freq = f->frequency;
1994
1995 saa7134_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1996
1997 saa7134_tvaudio_do_scan(dev);
1998 mutex_unlock(&dev->lock);
1999 return 0;
2000}
2001
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002002static int saa7134_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002003{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002004 strcpy(a->name, "audio");
2005 return 0;
2006}
2007
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002008static int saa7134_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002009{
2010 return 0;
2011}
2012
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002013static int saa7134_g_priority(struct file *file, void *f, enum v4l2_priority *p)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002014{
2015 struct saa7134_fh *fh = f;
2016 struct saa7134_dev *dev = fh->dev;
2017
2018 *p = v4l2_prio_max(&dev->prio);
2019 return 0;
2020}
2021
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002022static int saa7134_s_priority(struct file *file, void *f,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002023 enum v4l2_priority prio)
2024{
2025 struct saa7134_fh *fh = f;
2026 struct saa7134_dev *dev = fh->dev;
2027
2028 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
2029}
2030
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002031static int saa7134_enum_fmt_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002032 struct v4l2_fmtdesc *f)
2033{
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002034 if (f->index >= FORMATS)
2035 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002036
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002037 strlcpy(f->description, formats[f->index].name,
2038 sizeof(f->description));
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002039
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002040 f->pixelformat = formats[f->index].fourcc;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002041
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002042 return 0;
2043}
2044
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002045static int saa7134_enum_fmt_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002046 struct v4l2_fmtdesc *f)
2047{
2048 if (saa7134_no_overlay > 0) {
2049 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002050 return -EINVAL;
2051 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002052
2053 if ((f->index >= FORMATS) || formats[f->index].planar)
2054 return -EINVAL;
2055
2056 strlcpy(f->description, formats[f->index].name,
2057 sizeof(f->description));
2058
2059 f->pixelformat = formats[f->index].fourcc;
2060
2061 return 0;
2062}
2063
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002064static int saa7134_enum_fmt_vbi(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002065 struct v4l2_fmtdesc *f)
2066{
2067 if (0 != f->index)
2068 return -EINVAL;
2069
2070 f->pixelformat = V4L2_PIX_FMT_GREY;
2071 strcpy(f->description, "vbi data");
2072
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002073 return 0;
2074}
2075
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002076static int saa7134_g_fbuf(struct file *file, void *f,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002077 struct v4l2_framebuffer *fb)
2078{
2079 struct saa7134_fh *fh = f;
2080 struct saa7134_dev *dev = fh->dev;
2081
2082 *fb = dev->ovbuf;
2083 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2084
2085 return 0;
2086}
2087
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002088static int saa7134_s_fbuf(struct file *file, void *f,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002089 struct v4l2_framebuffer *fb)
2090{
2091 struct saa7134_fh *fh = f;
2092 struct saa7134_dev *dev = fh->dev;
2093 struct saa7134_format *fmt;
2094
2095 if (!capable(CAP_SYS_ADMIN) &&
2096 !capable(CAP_SYS_RAWIO))
2097 return -EPERM;
2098
2099 /* check args */
2100 fmt = format_by_fourcc(fb->fmt.pixelformat);
2101 if (NULL == fmt)
2102 return -EINVAL;
2103
2104 /* ok, accept it */
2105 dev->ovbuf = *fb;
2106 dev->ovfmt = fmt;
2107 if (0 == dev->ovbuf.fmt.bytesperline)
2108 dev->ovbuf.fmt.bytesperline =
2109 dev->ovbuf.fmt.width*fmt->depth/8;
2110 return 0;
2111}
2112
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002113static int saa7134_overlay(struct file *file, void *f, unsigned int on)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002114{
2115 struct saa7134_fh *fh = f;
2116 struct saa7134_dev *dev = fh->dev;
2117 unsigned long flags;
2118
2119 if (on) {
2120 if (saa7134_no_overlay > 0) {
2121 dprintk("no_overlay\n");
2122 return -EINVAL;
2123 }
2124
2125 if (!res_get(dev, fh, RESOURCE_OVERLAY))
2126 return -EBUSY;
2127 spin_lock_irqsave(&dev->slock, flags);
2128 start_preview(dev, fh);
2129 spin_unlock_irqrestore(&dev->slock, flags);
2130 }
2131 if (!on) {
2132 if (!res_check(fh, RESOURCE_OVERLAY))
2133 return -EINVAL;
2134 spin_lock_irqsave(&dev->slock, flags);
2135 stop_preview(dev, fh);
2136 spin_unlock_irqrestore(&dev->slock, flags);
2137 res_free(dev, fh, RESOURCE_OVERLAY);
2138 }
2139 return 0;
2140}
2141
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002142#ifdef CONFIG_VIDEO_V4L1_COMPAT
2143static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2144{
2145 struct saa7134_fh *fh = file->private_data;
2146 return videobuf_cgmbuf(saa7134_queue(fh), mbuf, 8);
2147}
2148#endif
2149
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002150static int saa7134_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002151 struct v4l2_requestbuffers *p)
2152{
2153 struct saa7134_fh *fh = priv;
2154 return videobuf_reqbufs(saa7134_queue(fh), p);
2155}
2156
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002157static int saa7134_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002158 struct v4l2_buffer *b)
2159{
2160 struct saa7134_fh *fh = priv;
2161 return videobuf_querybuf(saa7134_queue(fh), b);
2162}
2163
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002164static int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002165{
2166 struct saa7134_fh *fh = priv;
2167 return videobuf_qbuf(saa7134_queue(fh), b);
2168}
2169
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002170static int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002171{
2172 struct saa7134_fh *fh = priv;
2173 return videobuf_dqbuf(saa7134_queue(fh), b,
2174 file->f_flags & O_NONBLOCK);
2175}
2176
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002177static int saa7134_streamon(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002178 enum v4l2_buf_type type)
2179{
2180 struct saa7134_fh *fh = priv;
2181 struct saa7134_dev *dev = fh->dev;
2182 int res = saa7134_resource(fh);
2183
2184 if (!res_get(dev, fh, res))
2185 return -EBUSY;
2186
2187 return videobuf_streamon(saa7134_queue(fh));
2188}
2189
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002190static int saa7134_streamoff(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002191 enum v4l2_buf_type type)
2192{
2193 int err;
2194 struct saa7134_fh *fh = priv;
2195 struct saa7134_dev *dev = fh->dev;
2196 int res = saa7134_resource(fh);
2197
2198 err = videobuf_streamoff(saa7134_queue(fh));
2199 if (err < 0)
2200 return err;
2201 res_free(dev, fh, res);
2202 return 0;
2203}
2204
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002205static int saa7134_g_parm(struct file *file, void *fh,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002206 struct v4l2_streamparm *parm)
2207{
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002208 return 0;
2209}
2210
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002211static int radio_querycap(struct file *file, void *priv,
2212 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 struct saa7134_fh *fh = file->private_data;
2215 struct saa7134_dev *dev = fh->dev;
2216
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002217 strcpy(cap->driver, "saa7134");
2218 strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
2219 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
2220 cap->version = SAA7134_VERSION_CODE;
2221 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 return 0;
2223}
2224
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002225static int radio_g_tuner(struct file *file, void *priv,
2226 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002228 struct saa7134_fh *fh = file->private_data;
2229 struct saa7134_dev *dev = fh->dev;
2230
2231 if (0 != t->index)
2232 return -EINVAL;
2233
2234 memset(t, 0, sizeof(*t));
2235 strcpy(t->name, "Radio");
2236 t->type = V4L2_TUNER_RADIO;
2237
2238 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
2239 if (dev->input->amux == TV) {
2240 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2241 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2242 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2243 }
2244 return 0;
2245}
2246static int radio_s_tuner(struct file *file, void *priv,
2247 struct v4l2_tuner *t)
2248{
2249 struct saa7134_fh *fh = file->private_data;
2250 struct saa7134_dev *dev = fh->dev;
2251
2252 if (0 != t->index)
2253 return -EINVAL;
2254
2255 saa7134_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
2256 return 0;
2257}
2258
2259static int radio_enum_input(struct file *file, void *priv,
2260 struct v4l2_input *i)
2261{
2262 if (i->index != 0)
2263 return -EINVAL;
2264
2265 strcpy(i->name, "Radio");
2266 i->type = V4L2_INPUT_TYPE_TUNER;
2267
2268 return 0;
2269}
2270
2271static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2272{
2273 *i = 0;
2274 return 0;
2275}
2276
2277static int radio_g_audio(struct file *file, void *priv,
2278 struct v4l2_audio *a)
2279{
2280 memset(a, 0, sizeof(*a));
2281 strcpy(a->name, "Radio");
2282 return 0;
2283}
2284
2285static int radio_s_audio(struct file *file, void *priv,
2286 struct v4l2_audio *a)
2287{
2288 return 0;
2289}
2290
2291static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2292{
2293 return 0;
2294}
2295
2296static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
2297{
2298 return 0;
2299}
2300
2301static int radio_queryctrl(struct file *file, void *priv,
2302 struct v4l2_queryctrl *c)
2303{
2304 const struct v4l2_queryctrl *ctrl;
2305
2306 if (c->id < V4L2_CID_BASE ||
2307 c->id >= V4L2_CID_LASTP1)
2308 return -EINVAL;
2309 if (c->id == V4L2_CID_AUDIO_MUTE) {
2310 ctrl = ctrl_by_id(c->id);
2311 *c = *ctrl;
2312 } else
2313 *c = no_ctrl;
2314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Arjan van de Venfa027c22007-02-12 00:55:33 -08002317static const struct file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
2319 .owner = THIS_MODULE,
2320 .open = video_open,
2321 .release = video_release,
2322 .read = video_read,
2323 .poll = video_poll,
2324 .mmap = video_mmap,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002325 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02002326 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 .llseek = no_llseek,
2328};
2329
Arjan van de Venfa027c22007-02-12 00:55:33 -08002330static const struct file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
2332 .owner = THIS_MODULE,
2333 .open = video_open,
2334 .release = video_release,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002335 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02002336 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 .llseek = no_llseek,
2338};
2339
2340/* ----------------------------------------------------------- */
2341/* exported stuff */
2342
2343struct video_device saa7134_video_template =
2344{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002345 .name = "saa7134-video",
2346 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER |
2347 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2348 .fops = &video_fops,
2349 .minor = -1,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002350 .vidioc_querycap = saa7134_querycap,
2351 .vidioc_enum_fmt_cap = saa7134_enum_fmt_cap,
2352 .vidioc_g_fmt_cap = saa7134_g_fmt_cap,
2353 .vidioc_try_fmt_cap = saa7134_try_fmt_cap,
2354 .vidioc_s_fmt_cap = saa7134_s_fmt_cap,
2355 .vidioc_enum_fmt_overlay = saa7134_enum_fmt_overlay,
2356 .vidioc_g_fmt_overlay = saa7134_g_fmt_overlay,
2357 .vidioc_try_fmt_overlay = saa7134_try_fmt_overlay,
2358 .vidioc_s_fmt_overlay = saa7134_s_fmt_overlay,
2359 .vidioc_enum_fmt_vbi = saa7134_enum_fmt_vbi,
2360 .vidioc_g_fmt_vbi = saa7134_try_get_set_fmt_vbi,
2361 .vidioc_try_fmt_vbi = saa7134_try_get_set_fmt_vbi,
2362 .vidioc_s_fmt_vbi = saa7134_try_get_set_fmt_vbi,
2363 .vidioc_g_audio = saa7134_g_audio,
2364 .vidioc_s_audio = saa7134_s_audio,
2365 .vidioc_cropcap = saa7134_cropcap,
2366 .vidioc_reqbufs = saa7134_reqbufs,
2367 .vidioc_querybuf = saa7134_querybuf,
2368 .vidioc_qbuf = saa7134_qbuf,
2369 .vidioc_dqbuf = saa7134_dqbuf,
2370 .vidioc_s_std = saa7134_s_std,
2371 .vidioc_enum_input = saa7134_enum_input,
2372 .vidioc_g_input = saa7134_g_input,
2373 .vidioc_s_input = saa7134_s_input,
2374 .vidioc_queryctrl = saa7134_queryctrl,
2375 .vidioc_g_ctrl = saa7134_g_ctrl,
2376 .vidioc_s_ctrl = saa7134_s_ctrl,
2377 .vidioc_streamon = saa7134_streamon,
2378 .vidioc_streamoff = saa7134_streamoff,
2379 .vidioc_g_tuner = saa7134_g_tuner,
2380 .vidioc_s_tuner = saa7134_s_tuner,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002381#ifdef CONFIG_VIDEO_V4L1_COMPAT
2382 .vidiocgmbuf = vidiocgmbuf,
2383#endif
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002384 .vidioc_g_crop = saa7134_g_crop,
2385 .vidioc_s_crop = saa7134_s_crop,
2386 .vidioc_g_fbuf = saa7134_g_fbuf,
2387 .vidioc_s_fbuf = saa7134_s_fbuf,
2388 .vidioc_overlay = saa7134_overlay,
2389 .vidioc_g_priority = saa7134_g_priority,
2390 .vidioc_s_priority = saa7134_s_priority,
2391 .vidioc_g_parm = saa7134_g_parm,
2392 .vidioc_g_frequency = saa7134_g_frequency,
2393 .vidioc_s_frequency = saa7134_s_frequency,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002394 .tvnorms = SAA7134_NORMS,
2395 .current_norm = V4L2_STD_PAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396};
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398struct video_device saa7134_radio_template =
2399{
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002400 .name = "saa7134-radio",
2401 .type = VID_TYPE_TUNER,
2402 .fops = &radio_fops,
2403 .minor = -1,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002404 .vidioc_querycap = radio_querycap,
2405 .vidioc_g_tuner = radio_g_tuner,
2406 .vidioc_enum_input = radio_enum_input,
2407 .vidioc_g_audio = radio_g_audio,
2408 .vidioc_s_tuner = radio_s_tuner,
2409 .vidioc_s_audio = radio_s_audio,
2410 .vidioc_s_input = radio_s_input,
2411 .vidioc_s_std = radio_s_std,
2412 .vidioc_queryctrl = radio_queryctrl,
2413 .vidioc_g_input = radio_g_input,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002414 .vidioc_g_ctrl = saa7134_g_ctrl,
2415 .vidioc_s_ctrl = saa7134_s_ctrl,
2416 .vidioc_g_frequency = saa7134_g_frequency,
2417 .vidioc_s_frequency = saa7134_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418};
2419
2420int saa7134_video_init1(struct saa7134_dev *dev)
2421{
2422 /* sanitycheck insmod options */
2423 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2424 gbuffers = 2;
2425 if (gbufsize < 0 || gbufsize > gbufsize_max)
2426 gbufsize = gbufsize_max;
2427 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2428
2429 /* put some sensible defaults into the data structures ... */
2430 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2431 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2432 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2433 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2434 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2435 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2436 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2437 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2438
2439 if (dev->tda9887_conf && dev->ctl_automute)
2440 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2441 dev->automute = 0;
2442
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002443 INIT_LIST_HEAD(&dev->video_q.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 init_timer(&dev->video_q.timeout);
2445 dev->video_q.timeout.function = saa7134_buffer_timeout;
2446 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2447 dev->video_q.dev = dev;
2448
Maxim Levitskycb712012007-09-27 20:34:25 -03002449 if (saa7134_boards[dev->board].video_out)
2450 saa7134_videoport_init(dev);
2451
2452 return 0;
2453}
2454
2455int saa7134_videoport_init(struct saa7134_dev *dev)
2456{
2457 /* enable video output */
2458 int vo = saa7134_boards[dev->board].video_out;
2459 int video_reg;
2460 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2461 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2462 video_reg = video_out[vo][1];
2463 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2464 video_reg &= ~VP_T_CODE_P_INVERTED;
2465 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2466 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2467 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2468 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2469 video_reg = video_out[vo][5];
2470 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2471 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2472 if (vid_port_opts & SET_CLOCK_INVERTED)
2473 video_reg |= VP_CLK_CTRL1_INVERTED;
2474 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2475 video_reg = video_out[vo][6];
2476 if (vid_port_opts & SET_VSYNC_OFF) {
2477 video_reg &= ~VP_VS_TYPE_MASK;
2478 video_reg |= VP_VS_TYPE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
Maxim Levitskycb712012007-09-27 20:34:25 -03002480 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2481 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2482 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 return 0;
2485}
2486
2487int saa7134_video_init2(struct saa7134_dev *dev)
2488{
2489 /* init video hw */
2490 set_tvnorm(dev,&tvnorms[0]);
2491 video_mux(dev,0);
2492 saa7134_tvaudio_setmute(dev);
2493 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2494 return 0;
2495}
2496
Maxim Levitsky11f70782007-09-27 20:44:39 -03002497void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
2499 static const char *st[] = {
2500 "(no signal)", "NTSC", "PAL", "SECAM" };
2501 u32 st1,st2;
2502
2503 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2504 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2505 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2506 (st1 & 0x40) ? "not locked" : "locked",
2507 (st2 & 0x40) ? "no" : "yes",
2508 st[st1 & 0x03]);
Maxim Levitsky11f70782007-09-27 20:44:39 -03002509 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 if (dev->nosignal) {
2512 /* no video signal -> mute audio */
2513 if (dev->ctl_automute)
2514 dev->automute = 1;
2515 saa7134_tvaudio_setmute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 } else {
2517 /* wake up tvaudio audio carrier scan thread */
2518 saa7134_tvaudio_do_scan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
Maxim Levitsky11f70782007-09-27 20:44:39 -03002520
2521 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2522 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2523 else
2524 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (dev->mops && dev->mops->signal_change)
2527 dev->mops->signal_change(dev);
2528}
2529
Maxim Levitsky11f70782007-09-27 20:44:39 -03002530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2532{
2533 enum v4l2_field field;
2534
2535 spin_lock(&dev->slock);
2536 if (dev->video_q.curr) {
2537 dev->video_fieldcount++;
2538 field = dev->video_q.curr->vb.field;
2539 if (V4L2_FIELD_HAS_BOTH(field)) {
2540 /* make sure we have seen both fields */
2541 if ((status & 0x10) == 0x00) {
2542 dev->video_q.curr->top_seen = 1;
2543 goto done;
2544 }
2545 if (!dev->video_q.curr->top_seen)
2546 goto done;
2547 } else if (field == V4L2_FIELD_TOP) {
2548 if ((status & 0x10) != 0x10)
2549 goto done;
2550 } else if (field == V4L2_FIELD_BOTTOM) {
2551 if ((status & 0x10) != 0x00)
2552 goto done;
2553 }
2554 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
Brandon Philips0fc06862007-11-06 20:02:36 -03002555 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557 saa7134_buffer_next(dev,&dev->video_q);
2558
2559 done:
2560 spin_unlock(&dev->slock);
2561}
2562
2563/* ----------------------------------------------------------- */
2564/*
2565 * Local variables:
2566 * c-basic-offset: 8
2567 * End:
2568 */