blob: 193b07d83d1eaa4c96b481ab537cf0c5f0b56062 [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)
Hans Verkuilfac69862009-01-17 12:17:14 -0300630 saa_call_all(dev, tuner, s_std, dev->tvnorm->id);
Hans Verkuil9afb7372008-09-06 06:34:44 -0300631 /* Set the correct norm for the saa6752hs. This function
632 does nothing if there is no saa6752hs. */
Hans Verkuilfac69862009-01-17 12:17:14 -0300633 saa_call_empress(dev, tuner, s_std, dev->tvnorm->id);
Maxim Levitskyc4584732007-10-12 00:57:15 -0300634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
637{
638 static const struct {
639 int xpsc;
640 int xacl;
641 int xc2_1;
642 int xdcg;
643 int vpfy;
644 } vals[] = {
645 /* XPSC XACL XC2_1 XDCG VPFY */
646 { 1, 0, 0, 0, 0 },
647 { 2, 2, 1, 2, 2 },
648 { 3, 4, 1, 3, 2 },
649 { 4, 8, 1, 4, 2 },
650 { 5, 8, 1, 4, 2 },
651 { 6, 8, 1, 4, 3 },
652 { 7, 8, 1, 4, 3 },
653 { 8, 15, 0, 4, 3 },
654 { 9, 15, 0, 4, 3 },
655 { 10, 16, 1, 5, 3 },
656 };
657 static const int count = ARRAY_SIZE(vals);
658 int i;
659
660 for (i = 0; i < count; i++)
661 if (vals[i].xpsc == prescale)
662 break;
663 if (i == count)
664 return;
665
666 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
667 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
668 saa_writeb(SAA7134_LEVEL_CTRL(task),
669 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
670 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
671 (vals[i].vpfy << 2) | vals[i].vpfy);
672}
673
674static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
675{
676 int val,mirror;
677
678 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
679 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
680
681 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
682 if (yscale < 2048) {
683 /* LPI */
684 dprintk("yscale LPI yscale=%d\n",yscale);
685 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
686 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
687 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
688 } else {
689 /* ACM */
690 val = 0x40 * 1024 / yscale;
691 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
692 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
693 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
694 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
695 }
696 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
697}
698
699static void set_size(struct saa7134_dev *dev, int task,
700 int width, int height, int interlace)
701{
702 int prescale,xscale,yscale,y_even,y_odd;
703 int h_start, h_stop, v_start, v_stop;
704 int div = interlace ? 2 : 1;
705
706 /* setup video scaler */
707 h_start = dev->crop_current.left;
708 v_start = dev->crop_current.top/2;
709 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
710 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
711
712 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
713 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
714 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
715 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
716 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
717 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
718 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
719 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
720
721 prescale = dev->crop_current.width / width;
722 if (0 == prescale)
723 prescale = 1;
724 xscale = 1024 * dev->crop_current.width / prescale / width;
725 yscale = 512 * div * dev->crop_current.height / height;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800726 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 set_h_prescale(dev,task,prescale);
728 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
729 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
730 set_v_scale(dev,task,yscale);
731
732 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
733 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
734 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
735 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
736
737 /* deinterlace y offsets */
738 y_odd = dev->ctl_y_odd;
739 y_even = dev->ctl_y_even;
740 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
741 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
742 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
743 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
744}
745
746/* ------------------------------------------------------------------ */
747
748struct cliplist {
749 __u16 position;
750 __u8 enable;
751 __u8 disable;
752};
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754static void set_cliplist(struct saa7134_dev *dev, int reg,
755 struct cliplist *cl, int entries, char *name)
756{
757 __u8 winbits = 0;
758 int i;
759
760 for (i = 0; i < entries; i++) {
761 winbits |= cl[i].enable;
762 winbits &= ~cl[i].disable;
763 if (i < 15 && cl[i].position == cl[i+1].position)
764 continue;
765 saa_writeb(reg + 0, winbits);
766 saa_writeb(reg + 2, cl[i].position & 0xff);
767 saa_writeb(reg + 3, cl[i].position >> 8);
768 dprintk("clip: %s winbits=%02x pos=%d\n",
769 name,winbits,cl[i].position);
770 reg += 8;
771 }
772 for (; reg < 0x400; reg += 8) {
773 saa_writeb(reg+ 0, 0);
774 saa_writeb(reg + 1, 0);
775 saa_writeb(reg + 2, 0);
776 saa_writeb(reg + 3, 0);
777 }
778}
779
780static int clip_range(int val)
781{
782 if (val < 0)
783 val = 0;
784 return val;
785}
786
Heikki Orsila9040b322007-04-27 12:31:18 -0300787/* Sort into smallest position first order */
788static int cliplist_cmp(const void *a, const void *b)
789{
790 const struct cliplist *cla = a;
791 const struct cliplist *clb = b;
792 if (cla->position < clb->position)
793 return -1;
794 if (cla->position > clb->position)
795 return 1;
796 return 0;
797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
800 int nclips, int interlace)
801{
802 struct cliplist col[16], row[16];
Heikki Orsila9040b322007-04-27 12:31:18 -0300803 int cols = 0, rows = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 int div = interlace ? 2 : 1;
805
Heikki Orsila9040b322007-04-27 12:31:18 -0300806 memset(col, 0, sizeof(col));
807 memset(row, 0, sizeof(row));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 for (i = 0; i < nclips && i < 8; i++) {
809 col[cols].position = clip_range(clips[i].c.left);
810 col[cols].enable = (1 << i);
811 cols++;
812 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
813 col[cols].disable = (1 << i);
814 cols++;
815 row[rows].position = clip_range(clips[i].c.top / div);
816 row[rows].enable = (1 << i);
817 rows++;
818 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
819 / div);
820 row[rows].disable = (1 << i);
821 rows++;
822 }
Heikki Orsila9040b322007-04-27 12:31:18 -0300823 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
824 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 set_cliplist(dev,0x380,col,cols,"cols");
826 set_cliplist(dev,0x384,row,rows,"rows");
827 return 0;
828}
829
830static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
831{
832 enum v4l2_field field;
833 int maxw, maxh;
834
835 if (NULL == dev->ovbuf.base)
836 return -EINVAL;
837 if (NULL == dev->ovfmt)
838 return -EINVAL;
839 if (win->w.width < 48 || win->w.height < 32)
840 return -EINVAL;
841 if (win->clipcount > 2048)
842 return -EINVAL;
843
844 field = win->field;
845 maxw = dev->crop_current.width;
846 maxh = dev->crop_current.height;
847
848 if (V4L2_FIELD_ANY == field) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800849 field = (win->w.height > maxh/2)
850 ? V4L2_FIELD_INTERLACED
851 : V4L2_FIELD_TOP;
852 }
853 switch (field) {
854 case V4L2_FIELD_TOP:
855 case V4L2_FIELD_BOTTOM:
856 maxh = maxh / 2;
857 break;
858 case V4L2_FIELD_INTERLACED:
859 break;
860 default:
861 return -EINVAL;
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 win->field = field;
865 if (win->w.width > maxw)
866 win->w.width = maxw;
867 if (win->w.height > maxh)
868 win->w.height = maxh;
869 return 0;
870}
871
872static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
873{
874 unsigned long base,control,bpl;
875 int err;
876
877 err = verify_preview(dev,&fh->win);
878 if (0 != err)
879 return err;
880
881 dev->ovfield = fh->win.field;
882 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
883 fh->win.w.width,fh->win.w.height,
884 fh->win.w.left,fh->win.w.top,
885 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
886
887 /* setup window + clipping */
888 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
889 V4L2_FIELD_HAS_BOTH(dev->ovfield));
890 setup_clipping(dev,fh->clips,fh->nclips,
891 V4L2_FIELD_HAS_BOTH(dev->ovfield));
892 if (dev->ovfmt->yuv)
893 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
894 else
895 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
896 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
897
898 /* dma: setup channel 1 (= Video Task B) */
899 base = (unsigned long)dev->ovbuf.base;
900 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
901 base += dev->ovfmt->depth/8 * fh->win.w.left;
902 bpl = dev->ovbuf.fmt.bytesperline;
903 control = SAA7134_RS_CONTROL_BURST_16;
904 if (dev->ovfmt->bswap)
905 control |= SAA7134_RS_CONTROL_BSWAP;
906 if (dev->ovfmt->wswap)
907 control |= SAA7134_RS_CONTROL_WSWAP;
908 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
909 saa_writel(SAA7134_RS_BA1(1),base);
910 saa_writel(SAA7134_RS_BA2(1),base+bpl);
911 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
912 saa_writel(SAA7134_RS_CONTROL(1),control);
913 } else {
914 saa_writel(SAA7134_RS_BA1(1),base);
915 saa_writel(SAA7134_RS_BA2(1),base);
916 saa_writel(SAA7134_RS_PITCH(1),bpl);
917 saa_writel(SAA7134_RS_CONTROL(1),control);
918 }
919
920 /* start dma */
921 dev->ovenable = 1;
922 saa7134_set_dmabits(dev);
923
924 return 0;
925}
926
927static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
928{
929 dev->ovenable = 0;
930 saa7134_set_dmabits(dev);
931 return 0;
932}
933
934/* ------------------------------------------------------------------ */
935
936static int buffer_activate(struct saa7134_dev *dev,
937 struct saa7134_buf *buf,
938 struct saa7134_buf *next)
939{
940 unsigned long base,control,bpl;
941 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
942
943 dprintk("buffer_activate buf=%p\n",buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300944 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 buf->top_seen = 0;
946
947 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
948 V4L2_FIELD_HAS_BOTH(buf->vb.field));
949 if (buf->fmt->yuv)
950 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
951 else
952 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
953 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
954
955 /* DMA: setup channel 0 (= Video Task A0) */
956 base = saa7134_buffer_base(buf);
957 if (buf->fmt->planar)
958 bpl = buf->vb.width;
959 else
960 bpl = (buf->vb.width * buf->fmt->depth) / 8;
961 control = SAA7134_RS_CONTROL_BURST_16 |
962 SAA7134_RS_CONTROL_ME |
963 (buf->pt->dma >> 12);
964 if (buf->fmt->bswap)
965 control |= SAA7134_RS_CONTROL_BSWAP;
966 if (buf->fmt->wswap)
967 control |= SAA7134_RS_CONTROL_WSWAP;
968 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
969 /* interlaced */
970 saa_writel(SAA7134_RS_BA1(0),base);
971 saa_writel(SAA7134_RS_BA2(0),base+bpl);
972 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
973 } else {
974 /* non-interlaced */
975 saa_writel(SAA7134_RS_BA1(0),base);
976 saa_writel(SAA7134_RS_BA2(0),base);
977 saa_writel(SAA7134_RS_PITCH(0),bpl);
978 }
979 saa_writel(SAA7134_RS_CONTROL(0),control);
980
981 if (buf->fmt->planar) {
982 /* DMA: setup channel 4+5 (= planar task A) */
983 bpl_uv = bpl >> buf->fmt->hshift;
984 lines_uv = buf->vb.height >> buf->fmt->vshift;
985 base2 = base + bpl * buf->vb.height;
986 base3 = base2 + bpl_uv * lines_uv;
987 if (buf->fmt->uvswap)
988 tmp = base2, base2 = base3, base3 = tmp;
989 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
990 bpl_uv,lines_uv,base2,base3);
991 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
992 /* interlaced */
993 saa_writel(SAA7134_RS_BA1(4),base2);
994 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
995 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
996 saa_writel(SAA7134_RS_BA1(5),base3);
997 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
998 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
999 } else {
1000 /* non-interlaced */
1001 saa_writel(SAA7134_RS_BA1(4),base2);
1002 saa_writel(SAA7134_RS_BA2(4),base2);
1003 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
1004 saa_writel(SAA7134_RS_BA1(5),base3);
1005 saa_writel(SAA7134_RS_BA2(5),base3);
1006 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
1007 }
1008 saa_writel(SAA7134_RS_CONTROL(4),control);
1009 saa_writel(SAA7134_RS_CONTROL(5),control);
1010 }
1011
1012 /* start DMA */
1013 saa7134_set_dmabits(dev);
1014 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1015 return 0;
1016}
1017
1018static int buffer_prepare(struct videobuf_queue *q,
1019 struct videobuf_buffer *vb,
1020 enum v4l2_field field)
1021{
1022 struct saa7134_fh *fh = q->priv_data;
1023 struct saa7134_dev *dev = fh->dev;
1024 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1025 unsigned int size;
1026 int err;
1027
1028 /* sanity checks */
1029 if (NULL == fh->fmt)
1030 return -EINVAL;
1031 if (fh->width < 48 ||
1032 fh->height < 32 ||
1033 fh->width/4 > dev->crop_current.width ||
1034 fh->height/4 > dev->crop_current.height ||
1035 fh->width > dev->crop_bounds.width ||
1036 fh->height > dev->crop_bounds.height)
1037 return -EINVAL;
1038 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1039 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1040 return -EINVAL;
1041
1042 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1043 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1044 fh->fmt->name);
1045 if (buf->vb.width != fh->width ||
1046 buf->vb.height != fh->height ||
1047 buf->vb.size != size ||
1048 buf->vb.field != field ||
1049 buf->fmt != fh->fmt) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001050 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
Brandon Philips0fc06862007-11-06 20:02:36 -03001053 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001054 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 buf->vb.width = fh->width;
1057 buf->vb.height = fh->height;
1058 buf->vb.size = size;
1059 buf->vb.field = field;
1060 buf->fmt = fh->fmt;
1061 buf->pt = &fh->pt_cap;
1062
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001063 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (err)
1065 goto oops;
1066 err = saa7134_pgtable_build(dev->pci,buf->pt,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001067 dma->sglist,
1068 dma->sglen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 saa7134_buffer_startpage(buf));
1070 if (err)
1071 goto oops;
1072 }
Brandon Philips0fc06862007-11-06 20:02:36 -03001073 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 buf->activate = buffer_activate;
1075 return 0;
1076
1077 oops:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001078 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return err;
1080}
1081
1082static int
1083buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1084{
1085 struct saa7134_fh *fh = q->priv_data;
1086
1087 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1088 if (0 == *count)
1089 *count = gbuffers;
1090 *count = saa7134_buffer_count(*size,*count);
1091 return 0;
1092}
1093
1094static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1095{
1096 struct saa7134_fh *fh = q->priv_data;
1097 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1098
1099 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1100}
1101
1102static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1105
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001106 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
1109static struct videobuf_queue_ops video_qops = {
1110 .buf_setup = buffer_setup,
1111 .buf_prepare = buffer_prepare,
1112 .buf_queue = buffer_queue,
1113 .buf_release = buffer_release,
1114};
1115
1116/* ------------------------------------------------------------------ */
1117
Hans Verkuil531d83a2008-07-26 09:04:06 -03001118int saa7134_g_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 const struct v4l2_queryctrl* ctrl;
1121
1122 ctrl = ctrl_by_id(c->id);
1123 if (NULL == ctrl)
1124 return -EINVAL;
1125 switch (c->id) {
1126 case V4L2_CID_BRIGHTNESS:
1127 c->value = dev->ctl_bright;
1128 break;
1129 case V4L2_CID_HUE:
1130 c->value = dev->ctl_hue;
1131 break;
1132 case V4L2_CID_CONTRAST:
1133 c->value = dev->ctl_contrast;
1134 break;
1135 case V4L2_CID_SATURATION:
1136 c->value = dev->ctl_saturation;
1137 break;
1138 case V4L2_CID_AUDIO_MUTE:
1139 c->value = dev->ctl_mute;
1140 break;
1141 case V4L2_CID_AUDIO_VOLUME:
1142 c->value = dev->ctl_volume;
1143 break;
1144 case V4L2_CID_PRIVATE_INVERT:
1145 c->value = dev->ctl_invert;
1146 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001147 case V4L2_CID_HFLIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 c->value = dev->ctl_mirror;
1149 break;
1150 case V4L2_CID_PRIVATE_Y_EVEN:
1151 c->value = dev->ctl_y_even;
1152 break;
1153 case V4L2_CID_PRIVATE_Y_ODD:
1154 c->value = dev->ctl_y_odd;
1155 break;
1156 case V4L2_CID_PRIVATE_AUTOMUTE:
1157 c->value = dev->ctl_automute;
1158 break;
1159 default:
1160 return -EINVAL;
1161 }
1162 return 0;
1163}
Hans Verkuil531d83a2008-07-26 09:04:06 -03001164EXPORT_SYMBOL_GPL(saa7134_g_ctrl_internal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Hans Verkuil531d83a2008-07-26 09:04:06 -03001166static int saa7134_g_ctrl(struct file *file, void *priv, struct v4l2_control *c)
1167{
1168 struct saa7134_fh *fh = priv;
1169
1170 return saa7134_g_ctrl_internal(fh->dev, fh, c);
1171}
1172
1173int saa7134_s_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 const struct v4l2_queryctrl* ctrl;
1176 unsigned long flags;
1177 int restart_overlay = 0;
Hans Verkuil531d83a2008-07-26 09:04:06 -03001178 int err;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001179
Hans Verkuil531d83a2008-07-26 09:04:06 -03001180 /* When called from the empress code fh == NULL.
1181 That needs to be fixed somehow, but for now this is
1182 good enough. */
1183 if (fh) {
1184 err = v4l2_prio_check(&dev->prio, &fh->prio);
1185 if (0 != err)
1186 return err;
1187 }
1188 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001190 mutex_lock(&dev->lock);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 ctrl = ctrl_by_id(c->id);
1193 if (NULL == ctrl)
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001194 goto error;
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1197 switch (ctrl->type) {
1198 case V4L2_CTRL_TYPE_BOOLEAN:
1199 case V4L2_CTRL_TYPE_MENU:
1200 case V4L2_CTRL_TYPE_INTEGER:
1201 if (c->value < ctrl->minimum)
1202 c->value = ctrl->minimum;
1203 if (c->value > ctrl->maximum)
1204 c->value = ctrl->maximum;
1205 break;
1206 default:
1207 /* nothing */;
1208 };
1209 switch (c->id) {
1210 case V4L2_CID_BRIGHTNESS:
1211 dev->ctl_bright = c->value;
1212 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1213 break;
1214 case V4L2_CID_HUE:
1215 dev->ctl_hue = c->value;
1216 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1217 break;
1218 case V4L2_CID_CONTRAST:
1219 dev->ctl_contrast = c->value;
1220 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1221 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1222 break;
1223 case V4L2_CID_SATURATION:
1224 dev->ctl_saturation = c->value;
1225 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1226 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1227 break;
1228 case V4L2_CID_AUDIO_MUTE:
1229 dev->ctl_mute = c->value;
1230 saa7134_tvaudio_setmute(dev);
1231 break;
1232 case V4L2_CID_AUDIO_VOLUME:
1233 dev->ctl_volume = c->value;
1234 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1235 break;
1236 case V4L2_CID_PRIVATE_INVERT:
1237 dev->ctl_invert = c->value;
1238 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1239 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1240 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1241 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1242 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001243 case V4L2_CID_HFLIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 dev->ctl_mirror = c->value;
1245 restart_overlay = 1;
1246 break;
1247 case V4L2_CID_PRIVATE_Y_EVEN:
1248 dev->ctl_y_even = c->value;
1249 restart_overlay = 1;
1250 break;
1251 case V4L2_CID_PRIVATE_Y_ODD:
1252 dev->ctl_y_odd = c->value;
1253 restart_overlay = 1;
1254 break;
1255 case V4L2_CID_PRIVATE_AUTOMUTE:
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001256 {
1257 struct v4l2_priv_tun_config tda9887_cfg;
1258
1259 tda9887_cfg.tuner = TUNER_TDA9887;
1260 tda9887_cfg.priv = &dev->tda9887_conf;
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 dev->ctl_automute = c->value;
1263 if (dev->tda9887_conf) {
1264 if (dev->ctl_automute)
1265 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1266 else
1267 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001268
Hans Verkuilfac69862009-01-17 12:17:14 -03001269 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271 break;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 default:
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001274 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1277 spin_lock_irqsave(&dev->slock,flags);
1278 stop_preview(dev,fh);
1279 start_preview(dev,fh);
1280 spin_unlock_irqrestore(&dev->slock,flags);
1281 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001282 err = 0;
1283
1284error:
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001285 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001286 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
Hans Verkuil531d83a2008-07-26 09:04:06 -03001288EXPORT_SYMBOL_GPL(saa7134_s_ctrl_internal);
1289
1290static int saa7134_s_ctrl(struct file *file, void *f, struct v4l2_control *c)
1291{
1292 struct saa7134_fh *fh = f;
1293
1294 return saa7134_s_ctrl_internal(fh->dev, fh, c);
1295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297/* ------------------------------------------------------------------ */
1298
1299static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1300{
1301 struct videobuf_queue* q = NULL;
1302
1303 switch (fh->type) {
1304 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1305 q = &fh->cap;
1306 break;
1307 case V4L2_BUF_TYPE_VBI_CAPTURE:
1308 q = &fh->vbi;
1309 break;
1310 default:
1311 BUG();
1312 }
1313 return q;
1314}
1315
1316static int saa7134_resource(struct saa7134_fh *fh)
1317{
Heikki Orsila9040b322007-04-27 12:31:18 -03001318 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1319 return RESOURCE_VIDEO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Heikki Orsila9040b322007-04-27 12:31:18 -03001321 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1322 return RESOURCE_VBI;
1323
1324 BUG();
1325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
Hans Verkuilbec43662008-12-30 06:58:20 -03001328static int video_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Hans Verkuilbec43662008-12-30 06:58:20 -03001330 int minor = video_devdata(file)->minor;
Trent Piepho6d28e982007-10-10 05:37:42 -03001331 struct saa7134_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 struct saa7134_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1334 int radio = 0;
Hans Verkuild56dc612008-07-30 08:43:36 -03001335
1336 lock_kernel();
Trent Piepho6d28e982007-10-10 05:37:42 -03001337 list_for_each_entry(dev, &saa7134_devlist, devlist) {
1338 if (dev->video_dev && (dev->video_dev->minor == minor))
1339 goto found;
1340 if (dev->radio_dev && (dev->radio_dev->minor == minor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 radio = 1;
Trent Piepho6d28e982007-10-10 05:37:42 -03001342 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
Trent Piepho6d28e982007-10-10 05:37:42 -03001344 if (dev->vbi_dev && (dev->vbi_dev->minor == minor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 type = V4L2_BUF_TYPE_VBI_CAPTURE;
Trent Piepho6d28e982007-10-10 05:37:42 -03001346 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348 }
Hans Verkuild56dc612008-07-30 08:43:36 -03001349 unlock_kernel();
Trent Piepho6d28e982007-10-10 05:37:42 -03001350 return -ENODEV;
1351 found:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1354 v4l2_type_names[type]);
1355
1356 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001357 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Hans Verkuild56dc612008-07-30 08:43:36 -03001358 if (NULL == fh) {
1359 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return -ENOMEM;
Hans Verkuild56dc612008-07-30 08:43:36 -03001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 file->private_data = fh;
1363 fh->dev = dev;
1364 fh->radio = radio;
1365 fh->type = type;
1366 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1367 fh->width = 720;
1368 fh->height = 576;
1369 v4l2_prio_open(&dev->prio,&fh->prio);
1370
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03001371 videobuf_queue_sg_init(&fh->cap, &video_qops,
1372 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1374 V4L2_FIELD_INTERLACED,
1375 sizeof(struct saa7134_buf),
1376 fh);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03001377 videobuf_queue_sg_init(&fh->vbi, &saa7134_vbi_qops,
1378 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 V4L2_BUF_TYPE_VBI_CAPTURE,
1380 V4L2_FIELD_SEQ_TB,
1381 sizeof(struct saa7134_buf),
1382 fh);
1383 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1384 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1385
1386 if (fh->radio) {
1387 /* switch to radio mode */
1388 saa7134_tvaudio_setinput(dev,&card(dev).radio);
Hans Verkuilfac69862009-01-17 12:17:14 -03001389 saa_call_all(dev, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 } else {
1391 /* switch to video/vbi mode */
1392 video_mux(dev,dev->ctl_input);
1393 }
Hans Verkuild56dc612008-07-30 08:43:36 -03001394 unlock_kernel();
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
1398static ssize_t
1399video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1400{
1401 struct saa7134_fh *fh = file->private_data;
1402
1403 switch (fh->type) {
1404 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1405 if (res_locked(fh->dev,RESOURCE_VIDEO))
1406 return -EBUSY;
1407 return videobuf_read_one(saa7134_queue(fh),
1408 data, count, ppos,
1409 file->f_flags & O_NONBLOCK);
1410 case V4L2_BUF_TYPE_VBI_CAPTURE:
1411 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1412 return -EBUSY;
1413 return videobuf_read_stream(saa7134_queue(fh),
1414 data, count, ppos, 1,
1415 file->f_flags & O_NONBLOCK);
1416 break;
1417 default:
1418 BUG();
1419 return 0;
1420 }
1421}
1422
1423static unsigned int
1424video_poll(struct file *file, struct poll_table_struct *wait)
1425{
1426 struct saa7134_fh *fh = file->private_data;
1427 struct videobuf_buffer *buf = NULL;
1428
1429 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1430 return videobuf_poll_stream(file, &fh->vbi, wait);
1431
1432 if (res_check(fh,RESOURCE_VIDEO)) {
1433 if (!list_empty(&fh->cap.stream))
1434 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1435 } else {
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001436 mutex_lock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (UNSET == fh->cap.read_off) {
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001438 /* need to capture a new frame */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001439 if (res_locked(fh->dev,RESOURCE_VIDEO))
1440 goto err;
1441 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field))
1442 goto err;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001443 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1444 fh->cap.read_off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001446 mutex_unlock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 buf = fh->cap.read_buf;
1448 }
1449
1450 if (!buf)
1451 return POLLERR;
1452
1453 poll_wait(file, &buf->done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03001454 if (buf->state == VIDEOBUF_DONE ||
1455 buf->state == VIDEOBUF_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return POLLIN|POLLRDNORM;
1457 return 0;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001458
1459err:
1460 mutex_unlock(&fh->cap.vb_lock);
1461 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Hans Verkuilbec43662008-12-30 06:58:20 -03001464static int video_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 struct saa7134_fh *fh = file->private_data;
1467 struct saa7134_dev *dev = fh->dev;
1468 unsigned long flags;
1469
1470 /* turn off overlay */
1471 if (res_check(fh, RESOURCE_OVERLAY)) {
1472 spin_lock_irqsave(&dev->slock,flags);
1473 stop_preview(dev,fh);
1474 spin_unlock_irqrestore(&dev->slock,flags);
1475 res_free(dev,fh,RESOURCE_OVERLAY);
1476 }
1477
1478 /* stop video capture */
1479 if (res_check(fh, RESOURCE_VIDEO)) {
1480 videobuf_streamoff(&fh->cap);
1481 res_free(dev,fh,RESOURCE_VIDEO);
1482 }
1483 if (fh->cap.read_buf) {
1484 buffer_release(&fh->cap,fh->cap.read_buf);
1485 kfree(fh->cap.read_buf);
1486 }
1487
1488 /* stop vbi capture */
1489 if (res_check(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03001490 videobuf_stop(&fh->vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 res_free(dev,fh,RESOURCE_VBI);
1492 }
1493
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001494 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1495 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1496 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1497 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1498 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1499
Hans Verkuilfac69862009-01-17 12:17:14 -03001500 saa_call_all(dev, core, s_standby, 0);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /* free stuff */
1503 videobuf_mmap_free(&fh->cap);
1504 videobuf_mmap_free(&fh->vbi);
1505 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1506 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1507
1508 v4l2_prio_close(&dev->prio,&fh->prio);
1509 file->private_data = NULL;
1510 kfree(fh);
1511 return 0;
1512}
1513
Heikki Orsila9040b322007-04-27 12:31:18 -03001514static int video_mmap(struct file *file, struct vm_area_struct * vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 struct saa7134_fh *fh = file->private_data;
1517
1518 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1519}
1520
1521/* ------------------------------------------------------------------ */
1522
Hans Verkuil78b526a2008-05-28 12:16:41 -03001523static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001524 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001526 struct saa7134_fh *fh = priv;
1527 struct saa7134_dev *dev = fh->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 struct saa7134_tvnorm *norm = dev->tvnorm;
1529
1530 f->fmt.vbi.sampling_rate = 6750000 * 4;
1531 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1532 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1533 f->fmt.vbi.offset = 64 * 4;
Michael Schimekf246a812005-06-23 22:04:47 -07001534 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1535 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1536 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1538 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1539
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Hans Verkuil78b526a2008-05-28 12:16:41 -03001543static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001544 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001546 struct saa7134_fh *fh = priv;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001547
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001548 f->fmt.pix.width = fh->width;
1549 f->fmt.pix.height = fh->height;
1550 f->fmt.pix.field = fh->cap.field;
1551 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1552 f->fmt.pix.bytesperline =
1553 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1554 f->fmt.pix.sizeimage =
1555 f->fmt.pix.height * f->fmt.pix.bytesperline;
1556 return 0;
1557}
1558
Hans Verkuil78b526a2008-05-28 12:16:41 -03001559static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001560 struct v4l2_format *f)
1561{
1562 struct saa7134_fh *fh = priv;
1563
1564 if (saa7134_no_overlay > 0) {
1565 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return -EINVAL;
1567 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001568 f->fmt.win = fh->win;
1569
1570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
Hans Verkuil78b526a2008-05-28 12:16:41 -03001573static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001574 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001576 struct saa7134_fh *fh = priv;
1577 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001578 struct saa7134_format *fmt;
1579 enum v4l2_field field;
1580 unsigned int maxw, maxh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001582 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1583 if (NULL == fmt)
1584 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001586 field = f->fmt.pix.field;
1587 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1588 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001590 if (V4L2_FIELD_ANY == field) {
1591 field = (f->fmt.pix.height > maxh/2)
1592 ? V4L2_FIELD_INTERLACED
1593 : V4L2_FIELD_BOTTOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001595 switch (field) {
1596 case V4L2_FIELD_TOP:
1597 case V4L2_FIELD_BOTTOM:
1598 maxh = maxh / 2;
1599 break;
1600 case V4L2_FIELD_INTERLACED:
1601 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 default:
1603 return -EINVAL;
1604 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001605
1606 f->fmt.pix.field = field;
1607 if (f->fmt.pix.width < 48)
1608 f->fmt.pix.width = 48;
1609 if (f->fmt.pix.height < 32)
1610 f->fmt.pix.height = 32;
1611 if (f->fmt.pix.width > maxw)
1612 f->fmt.pix.width = maxw;
1613 if (f->fmt.pix.height > maxh)
1614 f->fmt.pix.height = maxh;
1615 f->fmt.pix.width &= ~0x03;
1616 f->fmt.pix.bytesperline =
1617 (f->fmt.pix.width * fmt->depth) >> 3;
1618 f->fmt.pix.sizeimage =
1619 f->fmt.pix.height * f->fmt.pix.bytesperline;
1620
1621 return 0;
1622}
1623
Hans Verkuil78b526a2008-05-28 12:16:41 -03001624static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001625 struct v4l2_format *f)
1626{
1627 struct saa7134_fh *fh = priv;
1628 struct saa7134_dev *dev = fh->dev;
1629
1630 if (saa7134_no_overlay > 0) {
1631 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1632 return -EINVAL;
1633 }
1634
1635 return verify_preview(dev, &f->fmt.win);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
Hans Verkuil78b526a2008-05-28 12:16:41 -03001638static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001639 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001641 struct saa7134_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 int err;
1643
Hans Verkuil78b526a2008-05-28 12:16:41 -03001644 err = saa7134_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001645 if (0 != err)
1646 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001648 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1649 fh->width = f->fmt.pix.width;
1650 fh->height = f->fmt.pix.height;
1651 fh->cap.field = f->fmt.pix.field;
1652 return 0;
1653}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Hans Verkuil78b526a2008-05-28 12:16:41 -03001655static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001656 struct v4l2_format *f)
1657{
1658 struct saa7134_fh *fh = priv;
1659 struct saa7134_dev *dev = fh->dev;
1660 int err;
Steven Rostedta8b1ecf2008-05-06 20:42:29 -07001661 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001663 if (saa7134_no_overlay > 0) {
1664 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return -EINVAL;
1666 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001667 err = verify_preview(dev, &f->fmt.win);
1668 if (0 != err)
1669 return err;
1670
1671 mutex_lock(&dev->lock);
1672
1673 fh->win = f->fmt.win;
1674 fh->nclips = f->fmt.win.clipcount;
1675
1676 if (fh->nclips > 8)
1677 fh->nclips = 8;
1678
1679 if (copy_from_user(fh->clips, f->fmt.win.clips,
1680 sizeof(struct v4l2_clip)*fh->nclips)) {
1681 mutex_unlock(&dev->lock);
1682 return -EFAULT;
1683 }
1684
1685 if (res_check(fh, RESOURCE_OVERLAY)) {
1686 spin_lock_irqsave(&dev->slock, flags);
1687 stop_preview(dev, fh);
1688 start_preview(dev, fh);
1689 spin_unlock_irqrestore(&dev->slock, flags);
1690 }
1691
1692 mutex_unlock(&dev->lock);
1693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -03001696int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001697{
1698 const struct v4l2_queryctrl *ctrl;
1699
1700 if ((c->id < V4L2_CID_BASE ||
1701 c->id >= V4L2_CID_LASTP1) &&
1702 (c->id < V4L2_CID_PRIVATE_BASE ||
1703 c->id >= V4L2_CID_PRIVATE_LASTP1))
1704 return -EINVAL;
1705 ctrl = ctrl_by_id(c->id);
1706 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1707 return 0;
1708}
Mauro Carvalho Chehabf6e90a62007-12-11 15:01:57 -03001709EXPORT_SYMBOL_GPL(saa7134_queryctrl);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001710
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001711static int saa7134_enum_input(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001712 struct v4l2_input *i)
1713{
1714 struct saa7134_fh *fh = priv;
1715 struct saa7134_dev *dev = fh->dev;
1716 unsigned int n;
1717
1718 n = i->index;
1719 if (n >= SAA7134_INPUT_MAX)
1720 return -EINVAL;
1721 if (NULL == card_in(dev, i->index).name)
1722 return -EINVAL;
1723 memset(i, 0, sizeof(*i));
1724 i->index = n;
1725 i->type = V4L2_INPUT_TYPE_CAMERA;
1726 strcpy(i->name, card_in(dev, n).name);
1727 if (card_in(dev, n).tv)
1728 i->type = V4L2_INPUT_TYPE_TUNER;
1729 i->audioset = 1;
1730 if (n == dev->ctl_input) {
1731 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1732 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1733
1734 if (0 != (v1 & 0x40))
1735 i->status |= V4L2_IN_ST_NO_H_LOCK;
1736 if (0 != (v2 & 0x40))
1737 i->status |= V4L2_IN_ST_NO_SYNC;
1738 if (0 != (v2 & 0x0e))
1739 i->status |= V4L2_IN_ST_MACROVISION;
1740 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001741 i->std = SAA7134_NORMS;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001742 return 0;
1743}
1744
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001745static int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001746{
1747 struct saa7134_fh *fh = priv;
1748 struct saa7134_dev *dev = fh->dev;
1749
1750 *i = dev->ctl_input;
1751 return 0;
1752}
1753
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001754static int saa7134_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001755{
1756 struct saa7134_fh *fh = priv;
1757 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001758 int err;
1759
1760 err = v4l2_prio_check(&dev->prio, &fh->prio);
1761 if (0 != err)
1762 return err;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001763
1764 if (i < 0 || i >= SAA7134_INPUT_MAX)
1765 return -EINVAL;
1766 if (NULL == card_in(dev, i).name)
1767 return -EINVAL;
1768 mutex_lock(&dev->lock);
1769 video_mux(dev, i);
1770 mutex_unlock(&dev->lock);
1771 return 0;
1772}
1773
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001774static int saa7134_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001775 struct v4l2_capability *cap)
1776{
1777 struct saa7134_fh *fh = priv;
1778 struct saa7134_dev *dev = fh->dev;
1779
1780 unsigned int tuner_type = dev->tuner_type;
1781
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001782 strcpy(cap->driver, "saa7134");
1783 strlcpy(cap->card, saa7134_boards[dev->board].name,
1784 sizeof(cap->card));
1785 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1786 cap->version = SAA7134_VERSION_CODE;
1787 cap->capabilities =
1788 V4L2_CAP_VIDEO_CAPTURE |
1789 V4L2_CAP_VBI_CAPTURE |
1790 V4L2_CAP_READWRITE |
1791 V4L2_CAP_STREAMING |
1792 V4L2_CAP_TUNER;
1793 if (saa7134_no_overlay <= 0)
1794 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1795
1796 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1797 cap->capabilities &= ~V4L2_CAP_TUNER;
1798 return 0;
1799}
1800
Hans Verkuil9afb7372008-09-06 06:34:44 -03001801int saa7134_s_std_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, v4l2_std_id *id)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001802{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 unsigned long flags;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001804 unsigned int i;
1805 v4l2_std_id fixup;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001806 int err;
1807
Hans Verkuil9afb7372008-09-06 06:34:44 -03001808 /* When called from the empress code fh == NULL.
1809 That needs to be fixed somehow, but for now this is
1810 good enough. */
1811 if (fh) {
1812 err = v4l2_prio_check(&dev->prio, &fh->prio);
1813 if (0 != err)
1814 return err;
1815 } else if (res_locked(dev, RESOURCE_OVERLAY)) {
1816 /* Don't change the std from the mpeg device
1817 if overlay is active. */
1818 return -EBUSY;
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001821 for (i = 0; i < TVNORMS; i++)
1822 if (*id == tvnorms[i].id)
1823 break;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001824
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001825 if (i == TVNORMS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 for (i = 0; i < TVNORMS; i++)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001827 if (*id & tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 break;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001829 if (i == TVNORMS)
1830 return -EINVAL;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001831
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001832 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1833 if (secam[0] == 'L' || secam[0] == 'l') {
1834 if (secam[1] == 'C' || secam[1] == 'c')
1835 fixup = V4L2_STD_SECAM_LC;
1836 else
1837 fixup = V4L2_STD_SECAM_L;
1838 } else {
1839 if (secam[0] == 'D' || secam[0] == 'd')
1840 fixup = V4L2_STD_SECAM_DK;
1841 else
1842 fixup = V4L2_STD_SECAM;
Hartmut Hackmann17b10a72006-10-02 19:55:07 -03001843 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001844 for (i = 0; i < TVNORMS; i++)
1845 if (fixup == tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001848
1849 *id = tvnorms[i].id;
1850
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001851 mutex_lock(&dev->lock);
Hans Verkuil9afb7372008-09-06 06:34:44 -03001852 if (fh && res_check(fh, RESOURCE_OVERLAY)) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001853 spin_lock_irqsave(&dev->slock, flags);
1854 stop_preview(dev, fh);
1855 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001857 set_tvnorm(dev, &tvnorms[i]);
1858
1859 spin_lock_irqsave(&dev->slock, flags);
1860 start_preview(dev, fh);
1861 spin_unlock_irqrestore(&dev->slock, flags);
1862 } else
1863 set_tvnorm(dev, &tvnorms[i]);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001864
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001865 saa7134_tvaudio_do_scan(dev);
1866 mutex_unlock(&dev->lock);
1867 return 0;
1868}
Hans Verkuil9afb7372008-09-06 06:34:44 -03001869EXPORT_SYMBOL_GPL(saa7134_s_std_internal);
1870
1871static int saa7134_s_std(struct file *file, void *priv, v4l2_std_id *id)
1872{
1873 struct saa7134_fh *fh = priv;
1874
1875 return saa7134_s_std_internal(fh->dev, fh, id);
1876}
1877
1878static int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1879{
1880 struct saa7134_fh *fh = priv;
1881 struct saa7134_dev *dev = fh->dev;
1882
1883 *id = dev->tvnorm->id;
1884 return 0;
1885}
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001886
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001887static int saa7134_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001888 struct v4l2_cropcap *cap)
1889{
1890 struct saa7134_fh *fh = priv;
1891 struct saa7134_dev *dev = fh->dev;
1892
1893 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1894 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1895 return -EINVAL;
1896 cap->bounds = dev->crop_bounds;
1897 cap->defrect = dev->crop_defrect;
1898 cap->pixelaspect.numerator = 1;
1899 cap->pixelaspect.denominator = 1;
1900 if (dev->tvnorm->id & V4L2_STD_525_60) {
1901 cap->pixelaspect.numerator = 11;
1902 cap->pixelaspect.denominator = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001904 if (dev->tvnorm->id & V4L2_STD_625_50) {
1905 cap->pixelaspect.numerator = 54;
1906 cap->pixelaspect.denominator = 59;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908 return 0;
1909}
1910
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001911static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001912{
1913 struct saa7134_fh *fh = f;
1914 struct saa7134_dev *dev = fh->dev;
1915
1916 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1917 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1918 return -EINVAL;
1919 crop->c = dev->crop_current;
1920 return 0;
1921}
1922
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001923static int saa7134_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001924{
1925 struct saa7134_fh *fh = f;
1926 struct saa7134_dev *dev = fh->dev;
1927 struct v4l2_rect *b = &dev->crop_bounds;
1928
1929 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1930 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1931 return -EINVAL;
1932 if (crop->c.height < 0)
1933 return -EINVAL;
1934 if (crop->c.width < 0)
1935 return -EINVAL;
1936
1937 if (res_locked(fh->dev, RESOURCE_OVERLAY))
1938 return -EBUSY;
1939 if (res_locked(fh->dev, RESOURCE_VIDEO))
1940 return -EBUSY;
1941
1942 if (crop->c.top < b->top)
1943 crop->c.top = b->top;
1944 if (crop->c.top > b->top + b->height)
1945 crop->c.top = b->top + b->height;
1946 if (crop->c.height > b->top - crop->c.top + b->height)
1947 crop->c.height = b->top - crop->c.top + b->height;
1948
1949 if (crop->c.left < b->left)
1950 crop->c.left = b->left;
1951 if (crop->c.left > b->left + b->width)
1952 crop->c.left = b->left + b->width;
1953 if (crop->c.width > b->left - crop->c.left + b->width)
1954 crop->c.width = b->left - crop->c.left + b->width;
1955
1956 dev->crop_current = crop->c;
1957 return 0;
1958}
1959
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001960static int saa7134_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001961 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001963 struct saa7134_fh *fh = priv;
1964 struct saa7134_dev *dev = fh->dev;
1965 int n;
1966
1967 if (0 != t->index)
1968 return -EINVAL;
1969 memset(t, 0, sizeof(*t));
1970 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1971 if (card_in(dev, n).tv)
1972 break;
1973 if (NULL != card_in(dev, n).name) {
1974 strcpy(t->name, "Television");
1975 t->type = V4L2_TUNER_ANALOG_TV;
1976 t->capability = V4L2_TUNER_CAP_NORM |
1977 V4L2_TUNER_CAP_STEREO |
1978 V4L2_TUNER_CAP_LANG1 |
1979 V4L2_TUNER_CAP_LANG2;
1980 t->rangehigh = 0xffffffffUL;
1981 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1982 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1983 }
1984 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1985 t->signal = 0xffff;
1986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001989static int saa7134_s_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001990 struct v4l2_tuner *t)
1991{
1992 struct saa7134_fh *fh = priv;
1993 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001994 int rx, mode, err;
1995
1996 err = v4l2_prio_check(&dev->prio, &fh->prio);
1997 if (0 != err)
1998 return err;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001999
2000 mode = dev->thread.mode;
2001 if (UNSET == mode) {
2002 rx = saa7134_tvaudio_getstereo(dev);
2003 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
2004 }
2005 if (mode != t->audmode)
2006 dev->thread.mode = t->audmode;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002007
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002008 return 0;
2009}
2010
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002011static int saa7134_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002012 struct v4l2_frequency *f)
2013{
2014 struct saa7134_fh *fh = priv;
2015 struct saa7134_dev *dev = fh->dev;
2016
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002017 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2018 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002019
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002020 return 0;
2021}
2022
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002023static int saa7134_s_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002024 struct v4l2_frequency *f)
2025{
2026 struct saa7134_fh *fh = priv;
2027 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002028 int err;
2029
2030 err = v4l2_prio_check(&dev->prio, &fh->prio);
2031 if (0 != err)
2032 return err;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002033
2034 if (0 != f->tuner)
2035 return -EINVAL;
2036 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
2037 return -EINVAL;
2038 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
2039 return -EINVAL;
2040 mutex_lock(&dev->lock);
2041 dev->ctl_freq = f->frequency;
2042
Hans Verkuilfac69862009-01-17 12:17:14 -03002043 saa_call_all(dev, tuner, s_frequency, f);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002044
2045 saa7134_tvaudio_do_scan(dev);
2046 mutex_unlock(&dev->lock);
2047 return 0;
2048}
2049
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002050static int saa7134_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002051{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002052 strcpy(a->name, "audio");
2053 return 0;
2054}
2055
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002056static int saa7134_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002057{
2058 return 0;
2059}
2060
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002061static int saa7134_g_priority(struct file *file, void *f, enum v4l2_priority *p)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002062{
2063 struct saa7134_fh *fh = f;
2064 struct saa7134_dev *dev = fh->dev;
2065
2066 *p = v4l2_prio_max(&dev->prio);
2067 return 0;
2068}
2069
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002070static int saa7134_s_priority(struct file *file, void *f,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002071 enum v4l2_priority prio)
2072{
2073 struct saa7134_fh *fh = f;
2074 struct saa7134_dev *dev = fh->dev;
2075
2076 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
2077}
2078
Hans Verkuil78b526a2008-05-28 12:16:41 -03002079static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002080 struct v4l2_fmtdesc *f)
2081{
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002082 if (f->index >= FORMATS)
2083 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002084
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002085 strlcpy(f->description, formats[f->index].name,
2086 sizeof(f->description));
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002087
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002088 f->pixelformat = formats[f->index].fourcc;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002089
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002090 return 0;
2091}
2092
Hans Verkuil78b526a2008-05-28 12:16:41 -03002093static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002094 struct v4l2_fmtdesc *f)
2095{
2096 if (saa7134_no_overlay > 0) {
2097 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002098 return -EINVAL;
2099 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002100
2101 if ((f->index >= FORMATS) || formats[f->index].planar)
2102 return -EINVAL;
2103
2104 strlcpy(f->description, formats[f->index].name,
2105 sizeof(f->description));
2106
2107 f->pixelformat = formats[f->index].fourcc;
2108
2109 return 0;
2110}
2111
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002112static int saa7134_g_fbuf(struct file *file, void *f,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002113 struct v4l2_framebuffer *fb)
2114{
2115 struct saa7134_fh *fh = f;
2116 struct saa7134_dev *dev = fh->dev;
2117
2118 *fb = dev->ovbuf;
2119 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2120
2121 return 0;
2122}
2123
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002124static int saa7134_s_fbuf(struct file *file, void *f,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002125 struct v4l2_framebuffer *fb)
2126{
2127 struct saa7134_fh *fh = f;
2128 struct saa7134_dev *dev = fh->dev;
2129 struct saa7134_format *fmt;
2130
2131 if (!capable(CAP_SYS_ADMIN) &&
2132 !capable(CAP_SYS_RAWIO))
2133 return -EPERM;
2134
2135 /* check args */
2136 fmt = format_by_fourcc(fb->fmt.pixelformat);
2137 if (NULL == fmt)
2138 return -EINVAL;
2139
2140 /* ok, accept it */
2141 dev->ovbuf = *fb;
2142 dev->ovfmt = fmt;
2143 if (0 == dev->ovbuf.fmt.bytesperline)
2144 dev->ovbuf.fmt.bytesperline =
2145 dev->ovbuf.fmt.width*fmt->depth/8;
2146 return 0;
2147}
2148
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002149static int saa7134_overlay(struct file *file, void *f, unsigned int on)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002150{
2151 struct saa7134_fh *fh = f;
2152 struct saa7134_dev *dev = fh->dev;
2153 unsigned long flags;
2154
2155 if (on) {
2156 if (saa7134_no_overlay > 0) {
2157 dprintk("no_overlay\n");
2158 return -EINVAL;
2159 }
2160
2161 if (!res_get(dev, fh, RESOURCE_OVERLAY))
2162 return -EBUSY;
2163 spin_lock_irqsave(&dev->slock, flags);
2164 start_preview(dev, fh);
2165 spin_unlock_irqrestore(&dev->slock, flags);
2166 }
2167 if (!on) {
2168 if (!res_check(fh, RESOURCE_OVERLAY))
2169 return -EINVAL;
2170 spin_lock_irqsave(&dev->slock, flags);
2171 stop_preview(dev, fh);
2172 spin_unlock_irqrestore(&dev->slock, flags);
2173 res_free(dev, fh, RESOURCE_OVERLAY);
2174 }
2175 return 0;
2176}
2177
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002178#ifdef CONFIG_VIDEO_V4L1_COMPAT
2179static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2180{
2181 struct saa7134_fh *fh = file->private_data;
2182 return videobuf_cgmbuf(saa7134_queue(fh), mbuf, 8);
2183}
2184#endif
2185
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002186static int saa7134_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002187 struct v4l2_requestbuffers *p)
2188{
2189 struct saa7134_fh *fh = priv;
2190 return videobuf_reqbufs(saa7134_queue(fh), p);
2191}
2192
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002193static int saa7134_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002194 struct v4l2_buffer *b)
2195{
2196 struct saa7134_fh *fh = priv;
2197 return videobuf_querybuf(saa7134_queue(fh), b);
2198}
2199
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002200static int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002201{
2202 struct saa7134_fh *fh = priv;
2203 return videobuf_qbuf(saa7134_queue(fh), b);
2204}
2205
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002206static int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002207{
2208 struct saa7134_fh *fh = priv;
2209 return videobuf_dqbuf(saa7134_queue(fh), b,
2210 file->f_flags & O_NONBLOCK);
2211}
2212
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002213static int saa7134_streamon(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002214 enum v4l2_buf_type type)
2215{
2216 struct saa7134_fh *fh = priv;
2217 struct saa7134_dev *dev = fh->dev;
2218 int res = saa7134_resource(fh);
2219
2220 if (!res_get(dev, fh, res))
2221 return -EBUSY;
2222
2223 return videobuf_streamon(saa7134_queue(fh));
2224}
2225
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002226static int saa7134_streamoff(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002227 enum v4l2_buf_type type)
2228{
2229 int err;
2230 struct saa7134_fh *fh = priv;
2231 struct saa7134_dev *dev = fh->dev;
2232 int res = saa7134_resource(fh);
2233
2234 err = videobuf_streamoff(saa7134_queue(fh));
2235 if (err < 0)
2236 return err;
2237 res_free(dev, fh, res);
2238 return 0;
2239}
2240
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002241static int saa7134_g_parm(struct file *file, void *fh,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002242 struct v4l2_streamparm *parm)
2243{
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002244 return 0;
2245}
2246
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002247#ifdef CONFIG_VIDEO_ADV_DEBUG
2248static int vidioc_g_register (struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002249 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002250{
2251 struct saa7134_fh *fh = priv;
2252 struct saa7134_dev *dev = fh->dev;
2253
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002254 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002255 return -EINVAL;
2256 reg->val = saa_readb(reg->reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002257 reg->size = 1;
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002258 return 0;
2259}
2260
2261static int vidioc_s_register (struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002262 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002263{
2264 struct saa7134_fh *fh = priv;
2265 struct saa7134_dev *dev = fh->dev;
2266
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002267 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002268 return -EINVAL;
2269 saa_writeb(reg->reg&0xffffff, reg->val);
2270 return 0;
2271}
2272#endif
2273
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002274static int radio_querycap(struct file *file, void *priv,
2275 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 struct saa7134_fh *fh = file->private_data;
2278 struct saa7134_dev *dev = fh->dev;
2279
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002280 strcpy(cap->driver, "saa7134");
2281 strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
2282 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
2283 cap->version = SAA7134_VERSION_CODE;
2284 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return 0;
2286}
2287
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002288static int radio_g_tuner(struct file *file, void *priv,
2289 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002291 struct saa7134_fh *fh = file->private_data;
2292 struct saa7134_dev *dev = fh->dev;
2293
2294 if (0 != t->index)
2295 return -EINVAL;
2296
2297 memset(t, 0, sizeof(*t));
2298 strcpy(t->name, "Radio");
2299 t->type = V4L2_TUNER_RADIO;
2300
Hans Verkuilfac69862009-01-17 12:17:14 -03002301 saa_call_all(dev, tuner, g_tuner, t);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002302 if (dev->input->amux == TV) {
2303 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2304 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2305 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2306 }
2307 return 0;
2308}
2309static int radio_s_tuner(struct file *file, void *priv,
2310 struct v4l2_tuner *t)
2311{
2312 struct saa7134_fh *fh = file->private_data;
2313 struct saa7134_dev *dev = fh->dev;
2314
2315 if (0 != t->index)
2316 return -EINVAL;
2317
Hans Verkuilfac69862009-01-17 12:17:14 -03002318 saa_call_all(dev, tuner, s_tuner, t);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002319 return 0;
2320}
2321
2322static int radio_enum_input(struct file *file, void *priv,
2323 struct v4l2_input *i)
2324{
2325 if (i->index != 0)
2326 return -EINVAL;
2327
2328 strcpy(i->name, "Radio");
2329 i->type = V4L2_INPUT_TYPE_TUNER;
2330
2331 return 0;
2332}
2333
2334static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2335{
2336 *i = 0;
2337 return 0;
2338}
2339
2340static int radio_g_audio(struct file *file, void *priv,
2341 struct v4l2_audio *a)
2342{
2343 memset(a, 0, sizeof(*a));
2344 strcpy(a->name, "Radio");
2345 return 0;
2346}
2347
2348static int radio_s_audio(struct file *file, void *priv,
2349 struct v4l2_audio *a)
2350{
2351 return 0;
2352}
2353
2354static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2355{
2356 return 0;
2357}
2358
2359static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
2360{
2361 return 0;
2362}
2363
2364static int radio_queryctrl(struct file *file, void *priv,
2365 struct v4l2_queryctrl *c)
2366{
2367 const struct v4l2_queryctrl *ctrl;
2368
2369 if (c->id < V4L2_CID_BASE ||
2370 c->id >= V4L2_CID_LASTP1)
2371 return -EINVAL;
2372 if (c->id == V4L2_CID_AUDIO_MUTE) {
2373 ctrl = ctrl_by_id(c->id);
2374 *c = *ctrl;
2375 } else
2376 *c = no_ctrl;
2377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
Hans Verkuilbec43662008-12-30 06:58:20 -03002380static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 .owner = THIS_MODULE,
2383 .open = video_open,
2384 .release = video_release,
2385 .read = video_read,
2386 .poll = video_poll,
2387 .mmap = video_mmap,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002388 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389};
2390
Hans Verkuila3998102008-07-21 02:57:38 -03002391static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002392 .vidioc_querycap = saa7134_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002393 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
2394 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
2395 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
2396 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
2397 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
2398 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
2399 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
2400 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002401 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2402 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2403 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002404 .vidioc_g_audio = saa7134_g_audio,
2405 .vidioc_s_audio = saa7134_s_audio,
2406 .vidioc_cropcap = saa7134_cropcap,
2407 .vidioc_reqbufs = saa7134_reqbufs,
2408 .vidioc_querybuf = saa7134_querybuf,
2409 .vidioc_qbuf = saa7134_qbuf,
2410 .vidioc_dqbuf = saa7134_dqbuf,
2411 .vidioc_s_std = saa7134_s_std,
Hans Verkuil9afb7372008-09-06 06:34:44 -03002412 .vidioc_g_std = saa7134_g_std,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002413 .vidioc_enum_input = saa7134_enum_input,
2414 .vidioc_g_input = saa7134_g_input,
2415 .vidioc_s_input = saa7134_s_input,
2416 .vidioc_queryctrl = saa7134_queryctrl,
2417 .vidioc_g_ctrl = saa7134_g_ctrl,
2418 .vidioc_s_ctrl = saa7134_s_ctrl,
2419 .vidioc_streamon = saa7134_streamon,
2420 .vidioc_streamoff = saa7134_streamoff,
2421 .vidioc_g_tuner = saa7134_g_tuner,
2422 .vidioc_s_tuner = saa7134_s_tuner,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002423#ifdef CONFIG_VIDEO_V4L1_COMPAT
2424 .vidiocgmbuf = vidiocgmbuf,
2425#endif
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002426 .vidioc_g_crop = saa7134_g_crop,
2427 .vidioc_s_crop = saa7134_s_crop,
2428 .vidioc_g_fbuf = saa7134_g_fbuf,
2429 .vidioc_s_fbuf = saa7134_s_fbuf,
2430 .vidioc_overlay = saa7134_overlay,
2431 .vidioc_g_priority = saa7134_g_priority,
2432 .vidioc_s_priority = saa7134_s_priority,
2433 .vidioc_g_parm = saa7134_g_parm,
2434 .vidioc_g_frequency = saa7134_g_frequency,
2435 .vidioc_s_frequency = saa7134_s_frequency,
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002436#ifdef CONFIG_VIDEO_ADV_DEBUG
2437 .vidioc_g_register = vidioc_g_register,
2438 .vidioc_s_register = vidioc_s_register,
2439#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440};
2441
Hans Verkuilbec43662008-12-30 06:58:20 -03002442static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002443 .owner = THIS_MODULE,
2444 .open = video_open,
2445 .release = video_release,
2446 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002447};
2448
2449static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002450 .vidioc_querycap = radio_querycap,
2451 .vidioc_g_tuner = radio_g_tuner,
2452 .vidioc_enum_input = radio_enum_input,
2453 .vidioc_g_audio = radio_g_audio,
2454 .vidioc_s_tuner = radio_s_tuner,
2455 .vidioc_s_audio = radio_s_audio,
2456 .vidioc_s_input = radio_s_input,
2457 .vidioc_s_std = radio_s_std,
2458 .vidioc_queryctrl = radio_queryctrl,
2459 .vidioc_g_input = radio_g_input,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002460 .vidioc_g_ctrl = saa7134_g_ctrl,
2461 .vidioc_s_ctrl = saa7134_s_ctrl,
2462 .vidioc_g_frequency = saa7134_g_frequency,
2463 .vidioc_s_frequency = saa7134_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464};
2465
Hans Verkuila3998102008-07-21 02:57:38 -03002466/* ----------------------------------------------------------- */
2467/* exported stuff */
2468
2469struct video_device saa7134_video_template = {
2470 .name = "saa7134-video",
Hans Verkuila3998102008-07-21 02:57:38 -03002471 .fops = &video_fops,
2472 .ioctl_ops = &video_ioctl_ops,
2473 .minor = -1,
2474 .tvnorms = SAA7134_NORMS,
2475 .current_norm = V4L2_STD_PAL,
2476};
2477
2478struct video_device saa7134_radio_template = {
2479 .name = "saa7134-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002480 .fops = &radio_fops,
2481 .ioctl_ops = &radio_ioctl_ops,
2482 .minor = -1,
2483};
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485int saa7134_video_init1(struct saa7134_dev *dev)
2486{
2487 /* sanitycheck insmod options */
2488 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2489 gbuffers = 2;
2490 if (gbufsize < 0 || gbufsize > gbufsize_max)
2491 gbufsize = gbufsize_max;
2492 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2493
2494 /* put some sensible defaults into the data structures ... */
2495 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2496 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2497 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2498 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2499 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2500 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2501 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2502 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2503
2504 if (dev->tda9887_conf && dev->ctl_automute)
2505 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2506 dev->automute = 0;
2507
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002508 INIT_LIST_HEAD(&dev->video_q.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 init_timer(&dev->video_q.timeout);
2510 dev->video_q.timeout.function = saa7134_buffer_timeout;
2511 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2512 dev->video_q.dev = dev;
2513
Maxim Levitskycb712012007-09-27 20:34:25 -03002514 if (saa7134_boards[dev->board].video_out)
2515 saa7134_videoport_init(dev);
2516
2517 return 0;
2518}
2519
2520int saa7134_videoport_init(struct saa7134_dev *dev)
2521{
2522 /* enable video output */
2523 int vo = saa7134_boards[dev->board].video_out;
2524 int video_reg;
2525 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
Dmitry Belimov37df96d2008-06-10 12:43:58 -03002526
2527 /* Configure videoport */
Maxim Levitskycb712012007-09-27 20:34:25 -03002528 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2529 video_reg = video_out[vo][1];
2530 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2531 video_reg &= ~VP_T_CODE_P_INVERTED;
2532 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2533 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
Maxim Levitskycb712012007-09-27 20:34:25 -03002534 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2535 video_reg = video_out[vo][5];
2536 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2537 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2538 if (vid_port_opts & SET_CLOCK_INVERTED)
2539 video_reg |= VP_CLK_CTRL1_INVERTED;
2540 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2541 video_reg = video_out[vo][6];
2542 if (vid_port_opts & SET_VSYNC_OFF) {
2543 video_reg &= ~VP_VS_TYPE_MASK;
2544 video_reg |= VP_VS_TYPE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
Maxim Levitskycb712012007-09-27 20:34:25 -03002546 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2547 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2548 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Dmitry Belimov37df96d2008-06-10 12:43:58 -03002550 /* Start videoport */
2551 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return 0;
2554}
2555
2556int saa7134_video_init2(struct saa7134_dev *dev)
2557{
2558 /* init video hw */
2559 set_tvnorm(dev,&tvnorms[0]);
2560 video_mux(dev,0);
2561 saa7134_tvaudio_setmute(dev);
2562 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2563 return 0;
2564}
2565
Maxim Levitsky11f70782007-09-27 20:44:39 -03002566void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
2568 static const char *st[] = {
2569 "(no signal)", "NTSC", "PAL", "SECAM" };
2570 u32 st1,st2;
2571
2572 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2573 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2574 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2575 (st1 & 0x40) ? "not locked" : "locked",
2576 (st2 & 0x40) ? "no" : "yes",
2577 st[st1 & 0x03]);
Maxim Levitsky11f70782007-09-27 20:44:39 -03002578 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 if (dev->nosignal) {
2581 /* no video signal -> mute audio */
2582 if (dev->ctl_automute)
2583 dev->automute = 1;
2584 saa7134_tvaudio_setmute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 } else {
2586 /* wake up tvaudio audio carrier scan thread */
2587 saa7134_tvaudio_do_scan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 }
Maxim Levitsky11f70782007-09-27 20:44:39 -03002589
2590 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2591 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2592 else
2593 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2594
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 if (dev->mops && dev->mops->signal_change)
2596 dev->mops->signal_change(dev);
2597}
2598
Maxim Levitsky11f70782007-09-27 20:44:39 -03002599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2601{
2602 enum v4l2_field field;
2603
2604 spin_lock(&dev->slock);
2605 if (dev->video_q.curr) {
2606 dev->video_fieldcount++;
2607 field = dev->video_q.curr->vb.field;
2608 if (V4L2_FIELD_HAS_BOTH(field)) {
2609 /* make sure we have seen both fields */
2610 if ((status & 0x10) == 0x00) {
2611 dev->video_q.curr->top_seen = 1;
2612 goto done;
2613 }
2614 if (!dev->video_q.curr->top_seen)
2615 goto done;
2616 } else if (field == V4L2_FIELD_TOP) {
2617 if ((status & 0x10) != 0x10)
2618 goto done;
2619 } else if (field == V4L2_FIELD_BOTTOM) {
2620 if ((status & 0x10) != 0x00)
2621 goto done;
2622 }
2623 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
Brandon Philips0fc06862007-11-06 20:02:36 -03002624 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 }
2626 saa7134_buffer_next(dev,&dev->video_q);
2627
2628 done:
2629 spin_unlock(&dev->slock);
2630}
2631
2632/* ----------------------------------------------------------- */
2633/*
2634 * Local variables:
2635 * c-basic-offset: 8
2636 * End:
2637 */