blob: 954542e5c99f3b60d3c8ed2ff5d1e8d03dd5565e [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
41static unsigned int video_debug = 0;
42static unsigned int gbuffers = 8;
Maxim Levitsky11f70782007-09-27 20:44:39 -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
57#define dprintk(fmt, arg...) if (video_debug) \
58 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
546 dprintk("set tv norm = %s\n",norm->name);
547 dev->tvnorm = norm;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* setup cropping */
550 dev->crop_bounds.left = norm->h_start;
551 dev->crop_defrect.left = norm->h_start;
552 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
553 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
554
Michael Schimekf246a812005-06-23 22:04:47 -0700555 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 dev->crop_defrect.top = norm->video_v_start*2;
557 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
558 - dev->crop_bounds.top;
559 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
560
561 dev->crop_current = dev->crop_defrect;
562
Maxim Levitskyc4584732007-10-12 00:57:15 -0300563 saa7134_set_tvnorm_hw(dev);
Maxim Levitskycb712012007-09-27 20:34:25 -0300564
Maxim Levitskycb712012007-09-27 20:34:25 -0300565}
566
567static void video_mux(struct saa7134_dev *dev, int input)
568{
569 dprintk("video input = %d [%s]\n", input, card_in(dev, input).name);
570 dev->ctl_input = input;
571 set_tvnorm(dev, dev->tvnorm);
572 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
573}
574
Maxim Levitskyc4584732007-10-12 00:57:15 -0300575
576static void saa7134_set_decoder(struct saa7134_dev *dev)
Maxim Levitskycb712012007-09-27 20:34:25 -0300577{
578 int luma_control, sync_control, mux;
579
580 struct saa7134_tvnorm *norm = dev->tvnorm;
581 mux = card_in(dev, dev->ctl_input).vmux;
582
583 luma_control = norm->luma_control;
584 sync_control = norm->sync_control;
585
586 if (mux > 5)
587 luma_control |= 0x80; /* svideo */
588 if (noninterlaced || dev->nosignal)
589 sync_control |= 0x20;
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /* setup video decoder */
592 saa_writeb(SAA7134_INCR_DELAY, 0x08);
593 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
594 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
595
596 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
597 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
598 saa_writeb(SAA7134_HSYNC_START, 0xeb);
599 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
600 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
601
602 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
603 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
604 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Maxim Levitskyf5a1ac62007-09-27 20:34:20 -0300606 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
607 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
608
609 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
610 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
613 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
614 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
615
616 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
617 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
618
619 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
620 saa_writeb(SAA7134_VGATE_START, 0x11);
621 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
622 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
623 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
624 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Maxim Levitskyc4584732007-10-12 00:57:15 -0300627void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
628{
629 saa7134_set_decoder(dev);
630
631 if (card_in(dev, dev->ctl_input).tv) {
632 if ((card(dev).tuner_type == TUNER_PHILIPS_TDA8290)
633 && ((card(dev).tuner_config == 1)
634 || (card(dev).tuner_config == 2)))
635 saa7134_set_gpio(dev, 22, 5);
636 saa7134_i2c_call_clients(dev, VIDIOC_S_STD, &dev->tvnorm->id);
637 }
638}
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
641{
642 static const struct {
643 int xpsc;
644 int xacl;
645 int xc2_1;
646 int xdcg;
647 int vpfy;
648 } vals[] = {
649 /* XPSC XACL XC2_1 XDCG VPFY */
650 { 1, 0, 0, 0, 0 },
651 { 2, 2, 1, 2, 2 },
652 { 3, 4, 1, 3, 2 },
653 { 4, 8, 1, 4, 2 },
654 { 5, 8, 1, 4, 2 },
655 { 6, 8, 1, 4, 3 },
656 { 7, 8, 1, 4, 3 },
657 { 8, 15, 0, 4, 3 },
658 { 9, 15, 0, 4, 3 },
659 { 10, 16, 1, 5, 3 },
660 };
661 static const int count = ARRAY_SIZE(vals);
662 int i;
663
664 for (i = 0; i < count; i++)
665 if (vals[i].xpsc == prescale)
666 break;
667 if (i == count)
668 return;
669
670 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
671 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
672 saa_writeb(SAA7134_LEVEL_CTRL(task),
673 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
674 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
675 (vals[i].vpfy << 2) | vals[i].vpfy);
676}
677
678static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
679{
680 int val,mirror;
681
682 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
683 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
684
685 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
686 if (yscale < 2048) {
687 /* LPI */
688 dprintk("yscale LPI yscale=%d\n",yscale);
689 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
690 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
691 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
692 } else {
693 /* ACM */
694 val = 0x40 * 1024 / yscale;
695 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
696 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
697 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
698 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
699 }
700 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
701}
702
703static void set_size(struct saa7134_dev *dev, int task,
704 int width, int height, int interlace)
705{
706 int prescale,xscale,yscale,y_even,y_odd;
707 int h_start, h_stop, v_start, v_stop;
708 int div = interlace ? 2 : 1;
709
710 /* setup video scaler */
711 h_start = dev->crop_current.left;
712 v_start = dev->crop_current.top/2;
713 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
714 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
715
716 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
717 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
718 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
719 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
720 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
721 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
722 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
723 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
724
725 prescale = dev->crop_current.width / width;
726 if (0 == prescale)
727 prescale = 1;
728 xscale = 1024 * dev->crop_current.width / prescale / width;
729 yscale = 512 * div * dev->crop_current.height / height;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800730 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 set_h_prescale(dev,task,prescale);
732 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
733 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
734 set_v_scale(dev,task,yscale);
735
736 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
737 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
738 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
739 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
740
741 /* deinterlace y offsets */
742 y_odd = dev->ctl_y_odd;
743 y_even = dev->ctl_y_even;
744 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
745 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
746 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
747 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
748}
749
750/* ------------------------------------------------------------------ */
751
752struct cliplist {
753 __u16 position;
754 __u8 enable;
755 __u8 disable;
756};
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758static void set_cliplist(struct saa7134_dev *dev, int reg,
759 struct cliplist *cl, int entries, char *name)
760{
761 __u8 winbits = 0;
762 int i;
763
764 for (i = 0; i < entries; i++) {
765 winbits |= cl[i].enable;
766 winbits &= ~cl[i].disable;
767 if (i < 15 && cl[i].position == cl[i+1].position)
768 continue;
769 saa_writeb(reg + 0, winbits);
770 saa_writeb(reg + 2, cl[i].position & 0xff);
771 saa_writeb(reg + 3, cl[i].position >> 8);
772 dprintk("clip: %s winbits=%02x pos=%d\n",
773 name,winbits,cl[i].position);
774 reg += 8;
775 }
776 for (; reg < 0x400; reg += 8) {
777 saa_writeb(reg+ 0, 0);
778 saa_writeb(reg + 1, 0);
779 saa_writeb(reg + 2, 0);
780 saa_writeb(reg + 3, 0);
781 }
782}
783
784static int clip_range(int val)
785{
786 if (val < 0)
787 val = 0;
788 return val;
789}
790
Heikki Orsila9040b322007-04-27 12:31:18 -0300791/* Sort into smallest position first order */
792static int cliplist_cmp(const void *a, const void *b)
793{
794 const struct cliplist *cla = a;
795 const struct cliplist *clb = b;
796 if (cla->position < clb->position)
797 return -1;
798 if (cla->position > clb->position)
799 return 1;
800 return 0;
801}
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
804 int nclips, int interlace)
805{
806 struct cliplist col[16], row[16];
Heikki Orsila9040b322007-04-27 12:31:18 -0300807 int cols = 0, rows = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int div = interlace ? 2 : 1;
809
Heikki Orsila9040b322007-04-27 12:31:18 -0300810 memset(col, 0, sizeof(col));
811 memset(row, 0, sizeof(row));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 for (i = 0; i < nclips && i < 8; i++) {
813 col[cols].position = clip_range(clips[i].c.left);
814 col[cols].enable = (1 << i);
815 cols++;
816 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
817 col[cols].disable = (1 << i);
818 cols++;
819 row[rows].position = clip_range(clips[i].c.top / div);
820 row[rows].enable = (1 << i);
821 rows++;
822 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
823 / div);
824 row[rows].disable = (1 << i);
825 rows++;
826 }
Heikki Orsila9040b322007-04-27 12:31:18 -0300827 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
828 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 set_cliplist(dev,0x380,col,cols,"cols");
830 set_cliplist(dev,0x384,row,rows,"rows");
831 return 0;
832}
833
834static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
835{
836 enum v4l2_field field;
837 int maxw, maxh;
838
839 if (NULL == dev->ovbuf.base)
840 return -EINVAL;
841 if (NULL == dev->ovfmt)
842 return -EINVAL;
843 if (win->w.width < 48 || win->w.height < 32)
844 return -EINVAL;
845 if (win->clipcount > 2048)
846 return -EINVAL;
847
848 field = win->field;
849 maxw = dev->crop_current.width;
850 maxh = dev->crop_current.height;
851
852 if (V4L2_FIELD_ANY == field) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800853 field = (win->w.height > maxh/2)
854 ? V4L2_FIELD_INTERLACED
855 : V4L2_FIELD_TOP;
856 }
857 switch (field) {
858 case V4L2_FIELD_TOP:
859 case V4L2_FIELD_BOTTOM:
860 maxh = maxh / 2;
861 break;
862 case V4L2_FIELD_INTERLACED:
863 break;
864 default:
865 return -EINVAL;
866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 win->field = field;
869 if (win->w.width > maxw)
870 win->w.width = maxw;
871 if (win->w.height > maxh)
872 win->w.height = maxh;
873 return 0;
874}
875
876static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
877{
878 unsigned long base,control,bpl;
879 int err;
880
881 err = verify_preview(dev,&fh->win);
882 if (0 != err)
883 return err;
884
885 dev->ovfield = fh->win.field;
886 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
887 fh->win.w.width,fh->win.w.height,
888 fh->win.w.left,fh->win.w.top,
889 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
890
891 /* setup window + clipping */
892 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
893 V4L2_FIELD_HAS_BOTH(dev->ovfield));
894 setup_clipping(dev,fh->clips,fh->nclips,
895 V4L2_FIELD_HAS_BOTH(dev->ovfield));
896 if (dev->ovfmt->yuv)
897 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
898 else
899 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
900 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
901
902 /* dma: setup channel 1 (= Video Task B) */
903 base = (unsigned long)dev->ovbuf.base;
904 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
905 base += dev->ovfmt->depth/8 * fh->win.w.left;
906 bpl = dev->ovbuf.fmt.bytesperline;
907 control = SAA7134_RS_CONTROL_BURST_16;
908 if (dev->ovfmt->bswap)
909 control |= SAA7134_RS_CONTROL_BSWAP;
910 if (dev->ovfmt->wswap)
911 control |= SAA7134_RS_CONTROL_WSWAP;
912 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
913 saa_writel(SAA7134_RS_BA1(1),base);
914 saa_writel(SAA7134_RS_BA2(1),base+bpl);
915 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
916 saa_writel(SAA7134_RS_CONTROL(1),control);
917 } else {
918 saa_writel(SAA7134_RS_BA1(1),base);
919 saa_writel(SAA7134_RS_BA2(1),base);
920 saa_writel(SAA7134_RS_PITCH(1),bpl);
921 saa_writel(SAA7134_RS_CONTROL(1),control);
922 }
923
924 /* start dma */
925 dev->ovenable = 1;
926 saa7134_set_dmabits(dev);
927
928 return 0;
929}
930
931static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
932{
933 dev->ovenable = 0;
934 saa7134_set_dmabits(dev);
935 return 0;
936}
937
938/* ------------------------------------------------------------------ */
939
940static int buffer_activate(struct saa7134_dev *dev,
941 struct saa7134_buf *buf,
942 struct saa7134_buf *next)
943{
944 unsigned long base,control,bpl;
945 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
946
947 dprintk("buffer_activate buf=%p\n",buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300948 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 buf->top_seen = 0;
950
951 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
952 V4L2_FIELD_HAS_BOTH(buf->vb.field));
953 if (buf->fmt->yuv)
954 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
955 else
956 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
957 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
958
959 /* DMA: setup channel 0 (= Video Task A0) */
960 base = saa7134_buffer_base(buf);
961 if (buf->fmt->planar)
962 bpl = buf->vb.width;
963 else
964 bpl = (buf->vb.width * buf->fmt->depth) / 8;
965 control = SAA7134_RS_CONTROL_BURST_16 |
966 SAA7134_RS_CONTROL_ME |
967 (buf->pt->dma >> 12);
968 if (buf->fmt->bswap)
969 control |= SAA7134_RS_CONTROL_BSWAP;
970 if (buf->fmt->wswap)
971 control |= SAA7134_RS_CONTROL_WSWAP;
972 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
973 /* interlaced */
974 saa_writel(SAA7134_RS_BA1(0),base);
975 saa_writel(SAA7134_RS_BA2(0),base+bpl);
976 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
977 } else {
978 /* non-interlaced */
979 saa_writel(SAA7134_RS_BA1(0),base);
980 saa_writel(SAA7134_RS_BA2(0),base);
981 saa_writel(SAA7134_RS_PITCH(0),bpl);
982 }
983 saa_writel(SAA7134_RS_CONTROL(0),control);
984
985 if (buf->fmt->planar) {
986 /* DMA: setup channel 4+5 (= planar task A) */
987 bpl_uv = bpl >> buf->fmt->hshift;
988 lines_uv = buf->vb.height >> buf->fmt->vshift;
989 base2 = base + bpl * buf->vb.height;
990 base3 = base2 + bpl_uv * lines_uv;
991 if (buf->fmt->uvswap)
992 tmp = base2, base2 = base3, base3 = tmp;
993 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
994 bpl_uv,lines_uv,base2,base3);
995 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
996 /* interlaced */
997 saa_writel(SAA7134_RS_BA1(4),base2);
998 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
999 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
1000 saa_writel(SAA7134_RS_BA1(5),base3);
1001 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
1002 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
1003 } else {
1004 /* non-interlaced */
1005 saa_writel(SAA7134_RS_BA1(4),base2);
1006 saa_writel(SAA7134_RS_BA2(4),base2);
1007 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
1008 saa_writel(SAA7134_RS_BA1(5),base3);
1009 saa_writel(SAA7134_RS_BA2(5),base3);
1010 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
1011 }
1012 saa_writel(SAA7134_RS_CONTROL(4),control);
1013 saa_writel(SAA7134_RS_CONTROL(5),control);
1014 }
1015
1016 /* start DMA */
1017 saa7134_set_dmabits(dev);
1018 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1019 return 0;
1020}
1021
1022static int buffer_prepare(struct videobuf_queue *q,
1023 struct videobuf_buffer *vb,
1024 enum v4l2_field field)
1025{
1026 struct saa7134_fh *fh = q->priv_data;
1027 struct saa7134_dev *dev = fh->dev;
1028 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1029 unsigned int size;
1030 int err;
1031
1032 /* sanity checks */
1033 if (NULL == fh->fmt)
1034 return -EINVAL;
1035 if (fh->width < 48 ||
1036 fh->height < 32 ||
1037 fh->width/4 > dev->crop_current.width ||
1038 fh->height/4 > dev->crop_current.height ||
1039 fh->width > dev->crop_bounds.width ||
1040 fh->height > dev->crop_bounds.height)
1041 return -EINVAL;
1042 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1043 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1044 return -EINVAL;
1045
1046 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1047 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1048 fh->fmt->name);
1049 if (buf->vb.width != fh->width ||
1050 buf->vb.height != fh->height ||
1051 buf->vb.size != size ||
1052 buf->vb.field != field ||
1053 buf->fmt != fh->fmt) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001054 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056
Brandon Philips0fc06862007-11-06 20:02:36 -03001057 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001058 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 buf->vb.width = fh->width;
1061 buf->vb.height = fh->height;
1062 buf->vb.size = size;
1063 buf->vb.field = field;
1064 buf->fmt = fh->fmt;
1065 buf->pt = &fh->pt_cap;
1066
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001067 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (err)
1069 goto oops;
1070 err = saa7134_pgtable_build(dev->pci,buf->pt,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001071 dma->sglist,
1072 dma->sglen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 saa7134_buffer_startpage(buf));
1074 if (err)
1075 goto oops;
1076 }
Brandon Philips0fc06862007-11-06 20:02:36 -03001077 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 buf->activate = buffer_activate;
1079 return 0;
1080
1081 oops:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001082 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return err;
1084}
1085
1086static int
1087buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1088{
1089 struct saa7134_fh *fh = q->priv_data;
1090
1091 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1092 if (0 == *count)
1093 *count = gbuffers;
1094 *count = saa7134_buffer_count(*size,*count);
1095 return 0;
1096}
1097
1098static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1099{
1100 struct saa7134_fh *fh = q->priv_data;
1101 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1102
1103 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1104}
1105
1106static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1107{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1109
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001110 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
1113static struct videobuf_queue_ops video_qops = {
1114 .buf_setup = buffer_setup,
1115 .buf_prepare = buffer_prepare,
1116 .buf_queue = buffer_queue,
1117 .buf_release = buffer_release,
1118};
1119
1120/* ------------------------------------------------------------------ */
1121
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001122static int vidioc_g_ctrl(struct file *file, void *priv,
1123 struct v4l2_control *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001125 struct saa7134_fh *fh = priv;
1126 struct saa7134_dev *dev = fh->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 const struct v4l2_queryctrl* ctrl;
1128
1129 ctrl = ctrl_by_id(c->id);
1130 if (NULL == ctrl)
1131 return -EINVAL;
1132 switch (c->id) {
1133 case V4L2_CID_BRIGHTNESS:
1134 c->value = dev->ctl_bright;
1135 break;
1136 case V4L2_CID_HUE:
1137 c->value = dev->ctl_hue;
1138 break;
1139 case V4L2_CID_CONTRAST:
1140 c->value = dev->ctl_contrast;
1141 break;
1142 case V4L2_CID_SATURATION:
1143 c->value = dev->ctl_saturation;
1144 break;
1145 case V4L2_CID_AUDIO_MUTE:
1146 c->value = dev->ctl_mute;
1147 break;
1148 case V4L2_CID_AUDIO_VOLUME:
1149 c->value = dev->ctl_volume;
1150 break;
1151 case V4L2_CID_PRIVATE_INVERT:
1152 c->value = dev->ctl_invert;
1153 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001154 case V4L2_CID_HFLIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 c->value = dev->ctl_mirror;
1156 break;
1157 case V4L2_CID_PRIVATE_Y_EVEN:
1158 c->value = dev->ctl_y_even;
1159 break;
1160 case V4L2_CID_PRIVATE_Y_ODD:
1161 c->value = dev->ctl_y_odd;
1162 break;
1163 case V4L2_CID_PRIVATE_AUTOMUTE:
1164 c->value = dev->ctl_automute;
1165 break;
1166 default:
1167 return -EINVAL;
1168 }
1169 return 0;
1170}
1171
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001172static int vidioc_s_ctrl(struct file *file, void *f,
1173 struct v4l2_control *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 const struct v4l2_queryctrl* ctrl;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001176 struct saa7134_fh *fh = f;
1177 struct saa7134_dev *dev = fh->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 unsigned long flags;
1179 int restart_overlay = 0;
1180
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001181 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 ctrl = ctrl_by_id(c->id);
1183 if (NULL == ctrl)
1184 return -EINVAL;
1185 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1186 switch (ctrl->type) {
1187 case V4L2_CTRL_TYPE_BOOLEAN:
1188 case V4L2_CTRL_TYPE_MENU:
1189 case V4L2_CTRL_TYPE_INTEGER:
1190 if (c->value < ctrl->minimum)
1191 c->value = ctrl->minimum;
1192 if (c->value > ctrl->maximum)
1193 c->value = ctrl->maximum;
1194 break;
1195 default:
1196 /* nothing */;
1197 };
1198 switch (c->id) {
1199 case V4L2_CID_BRIGHTNESS:
1200 dev->ctl_bright = c->value;
1201 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1202 break;
1203 case V4L2_CID_HUE:
1204 dev->ctl_hue = c->value;
1205 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1206 break;
1207 case V4L2_CID_CONTRAST:
1208 dev->ctl_contrast = c->value;
1209 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1210 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1211 break;
1212 case V4L2_CID_SATURATION:
1213 dev->ctl_saturation = c->value;
1214 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1215 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1216 break;
1217 case V4L2_CID_AUDIO_MUTE:
1218 dev->ctl_mute = c->value;
1219 saa7134_tvaudio_setmute(dev);
1220 break;
1221 case V4L2_CID_AUDIO_VOLUME:
1222 dev->ctl_volume = c->value;
1223 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1224 break;
1225 case V4L2_CID_PRIVATE_INVERT:
1226 dev->ctl_invert = c->value;
1227 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1228 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1229 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1230 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1231 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001232 case V4L2_CID_HFLIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 dev->ctl_mirror = c->value;
1234 restart_overlay = 1;
1235 break;
1236 case V4L2_CID_PRIVATE_Y_EVEN:
1237 dev->ctl_y_even = c->value;
1238 restart_overlay = 1;
1239 break;
1240 case V4L2_CID_PRIVATE_Y_ODD:
1241 dev->ctl_y_odd = c->value;
1242 restart_overlay = 1;
1243 break;
1244 case V4L2_CID_PRIVATE_AUTOMUTE:
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001245 {
1246 struct v4l2_priv_tun_config tda9887_cfg;
1247
1248 tda9887_cfg.tuner = TUNER_TDA9887;
1249 tda9887_cfg.priv = &dev->tda9887_conf;
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 dev->ctl_automute = c->value;
1252 if (dev->tda9887_conf) {
1253 if (dev->ctl_automute)
1254 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1255 else
1256 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001257
1258 saa7134_i2c_call_clients(dev, TUNER_SET_CONFIG,
1259 &tda9887_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261 break;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 default:
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001264 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return -EINVAL;
1266 }
1267 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1268 spin_lock_irqsave(&dev->slock,flags);
1269 stop_preview(dev,fh);
1270 start_preview(dev,fh);
1271 spin_unlock_irqrestore(&dev->slock,flags);
1272 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001273 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return 0;
1275}
1276
1277/* ------------------------------------------------------------------ */
1278
1279static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1280{
1281 struct videobuf_queue* q = NULL;
1282
1283 switch (fh->type) {
1284 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1285 q = &fh->cap;
1286 break;
1287 case V4L2_BUF_TYPE_VBI_CAPTURE:
1288 q = &fh->vbi;
1289 break;
1290 default:
1291 BUG();
1292 }
1293 return q;
1294}
1295
1296static int saa7134_resource(struct saa7134_fh *fh)
1297{
Heikki Orsila9040b322007-04-27 12:31:18 -03001298 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1299 return RESOURCE_VIDEO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Heikki Orsila9040b322007-04-27 12:31:18 -03001301 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1302 return RESOURCE_VBI;
1303
1304 BUG();
1305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308static int video_open(struct inode *inode, struct file *file)
1309{
1310 int minor = iminor(inode);
Trent Piepho6d28e982007-10-10 05:37:42 -03001311 struct saa7134_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 struct saa7134_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1314 int radio = 0;
Trent Piepho6d28e982007-10-10 05:37:42 -03001315 list_for_each_entry(dev, &saa7134_devlist, devlist) {
1316 if (dev->video_dev && (dev->video_dev->minor == minor))
1317 goto found;
1318 if (dev->radio_dev && (dev->radio_dev->minor == minor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 radio = 1;
Trent Piepho6d28e982007-10-10 05:37:42 -03001320 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Trent Piepho6d28e982007-10-10 05:37:42 -03001322 if (dev->vbi_dev && (dev->vbi_dev->minor == minor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 type = V4L2_BUF_TYPE_VBI_CAPTURE;
Trent Piepho6d28e982007-10-10 05:37:42 -03001324 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326 }
Trent Piepho6d28e982007-10-10 05:37:42 -03001327 return -ENODEV;
1328 found:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1331 v4l2_type_names[type]);
1332
1333 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001334 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (NULL == fh)
1336 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 file->private_data = fh;
1338 fh->dev = dev;
1339 fh->radio = radio;
1340 fh->type = type;
1341 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1342 fh->width = 720;
1343 fh->height = 576;
1344 v4l2_prio_open(&dev->prio,&fh->prio);
1345
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001346 videobuf_queue_pci_init(&fh->cap, &video_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 dev->pci, &dev->slock,
1348 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1349 V4L2_FIELD_INTERLACED,
1350 sizeof(struct saa7134_buf),
1351 fh);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001352 videobuf_queue_pci_init(&fh->vbi, &saa7134_vbi_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 dev->pci, &dev->slock,
1354 V4L2_BUF_TYPE_VBI_CAPTURE,
1355 V4L2_FIELD_SEQ_TB,
1356 sizeof(struct saa7134_buf),
1357 fh);
1358 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1359 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1360
1361 if (fh->radio) {
1362 /* switch to radio mode */
1363 saa7134_tvaudio_setinput(dev,&card(dev).radio);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001364 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 } else {
1366 /* switch to video/vbi mode */
1367 video_mux(dev,dev->ctl_input);
1368 }
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
1372static ssize_t
1373video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1374{
1375 struct saa7134_fh *fh = file->private_data;
1376
1377 switch (fh->type) {
1378 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1379 if (res_locked(fh->dev,RESOURCE_VIDEO))
1380 return -EBUSY;
1381 return videobuf_read_one(saa7134_queue(fh),
1382 data, count, ppos,
1383 file->f_flags & O_NONBLOCK);
1384 case V4L2_BUF_TYPE_VBI_CAPTURE:
1385 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1386 return -EBUSY;
1387 return videobuf_read_stream(saa7134_queue(fh),
1388 data, count, ppos, 1,
1389 file->f_flags & O_NONBLOCK);
1390 break;
1391 default:
1392 BUG();
1393 return 0;
1394 }
1395}
1396
1397static unsigned int
1398video_poll(struct file *file, struct poll_table_struct *wait)
1399{
1400 struct saa7134_fh *fh = file->private_data;
1401 struct videobuf_buffer *buf = NULL;
1402
1403 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1404 return videobuf_poll_stream(file, &fh->vbi, wait);
1405
1406 if (res_check(fh,RESOURCE_VIDEO)) {
1407 if (!list_empty(&fh->cap.stream))
1408 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1409 } else {
Ingo Molnar3593cab2006-02-07 06:49:14 -02001410 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (UNSET == fh->cap.read_off) {
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001412 /* need to capture a new frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (res_locked(fh->dev,RESOURCE_VIDEO)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02001414 mutex_unlock(&fh->cap.lock);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001415 return POLLERR;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001416 }
1417 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02001418 mutex_unlock(&fh->cap.lock);
Mauro Carvalho Chehabf2421ca2005-11-08 21:37:45 -08001419 return POLLERR;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001420 }
1421 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1422 fh->cap.read_off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02001424 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 buf = fh->cap.read_buf;
1426 }
1427
1428 if (!buf)
1429 return POLLERR;
1430
1431 poll_wait(file, &buf->done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03001432 if (buf->state == VIDEOBUF_DONE ||
1433 buf->state == VIDEOBUF_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return POLLIN|POLLRDNORM;
1435 return 0;
1436}
1437
1438static int video_release(struct inode *inode, struct file *file)
1439{
1440 struct saa7134_fh *fh = file->private_data;
1441 struct saa7134_dev *dev = fh->dev;
1442 unsigned long flags;
1443
1444 /* turn off overlay */
1445 if (res_check(fh, RESOURCE_OVERLAY)) {
1446 spin_lock_irqsave(&dev->slock,flags);
1447 stop_preview(dev,fh);
1448 spin_unlock_irqrestore(&dev->slock,flags);
1449 res_free(dev,fh,RESOURCE_OVERLAY);
1450 }
1451
1452 /* stop video capture */
1453 if (res_check(fh, RESOURCE_VIDEO)) {
1454 videobuf_streamoff(&fh->cap);
1455 res_free(dev,fh,RESOURCE_VIDEO);
1456 }
1457 if (fh->cap.read_buf) {
1458 buffer_release(&fh->cap,fh->cap.read_buf);
1459 kfree(fh->cap.read_buf);
1460 }
1461
1462 /* stop vbi capture */
1463 if (res_check(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03001464 videobuf_stop(&fh->vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 res_free(dev,fh,RESOURCE_VBI);
1466 }
1467
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001468 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1469 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1470 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1471 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1472 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1473
Mauro Carvalho Chehab2f180712005-09-09 13:03:46 -07001474 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /* free stuff */
1477 videobuf_mmap_free(&fh->cap);
1478 videobuf_mmap_free(&fh->vbi);
1479 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1480 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1481
1482 v4l2_prio_close(&dev->prio,&fh->prio);
1483 file->private_data = NULL;
1484 kfree(fh);
1485 return 0;
1486}
1487
Heikki Orsila9040b322007-04-27 12:31:18 -03001488static int video_mmap(struct file *file, struct vm_area_struct * vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 struct saa7134_fh *fh = file->private_data;
1491
1492 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1493}
1494
1495/* ------------------------------------------------------------------ */
1496
1497static void saa7134_vbi_fmt(struct saa7134_dev *dev, struct v4l2_format *f)
1498{
1499 struct saa7134_tvnorm *norm = dev->tvnorm;
1500
1501 f->fmt.vbi.sampling_rate = 6750000 * 4;
1502 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1503 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1504 f->fmt.vbi.offset = 64 * 4;
Michael Schimekf246a812005-06-23 22:04:47 -07001505 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1506 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1507 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1509 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001513static int vidioc_g_fmt_cap(struct file *file, void *priv,
1514 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001516 struct saa7134_fh *fh = priv;
1517 struct saa7134_dev *dev = fh->dev;
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 switch (f->type) {
1520 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1521 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1522 f->fmt.pix.width = fh->width;
1523 f->fmt.pix.height = fh->height;
1524 f->fmt.pix.field = fh->cap.field;
1525 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1526 f->fmt.pix.bytesperline =
1527 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1528 f->fmt.pix.sizeimage =
1529 f->fmt.pix.height * f->fmt.pix.bytesperline;
1530 return 0;
1531 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehabecb73772006-03-17 20:54:32 -03001532 if (saa7134_no_overlay > 0) {
1533 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1534 return -EINVAL;
1535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 f->fmt.win = fh->win;
1537 return 0;
1538 case V4L2_BUF_TYPE_VBI_CAPTURE:
1539 saa7134_vbi_fmt(dev,f);
1540 return 0;
1541 default:
1542 return -EINVAL;
1543 }
1544}
1545
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001546static int vidioc_try_fmt_cap(struct file *file, void *priv,
1547 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001549 struct saa7134_fh *fh = priv;
1550 struct saa7134_dev *dev = fh->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 int err;
1552
1553 switch (f->type) {
1554 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1555 {
1556 struct saa7134_format *fmt;
1557 enum v4l2_field field;
1558 unsigned int maxw, maxh;
1559
1560 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1561 if (NULL == fmt)
1562 return -EINVAL;
1563
1564 field = f->fmt.pix.field;
1565 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1566 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1567
1568 if (V4L2_FIELD_ANY == field) {
1569 field = (f->fmt.pix.height > maxh/2)
1570 ? V4L2_FIELD_INTERLACED
1571 : V4L2_FIELD_BOTTOM;
1572 }
1573 switch (field) {
1574 case V4L2_FIELD_TOP:
1575 case V4L2_FIELD_BOTTOM:
1576 maxh = maxh / 2;
1577 break;
1578 case V4L2_FIELD_INTERLACED:
1579 break;
1580 default:
1581 return -EINVAL;
1582 }
1583
1584 f->fmt.pix.field = field;
1585 if (f->fmt.pix.width < 48)
1586 f->fmt.pix.width = 48;
1587 if (f->fmt.pix.height < 32)
1588 f->fmt.pix.height = 32;
1589 if (f->fmt.pix.width > maxw)
1590 f->fmt.pix.width = maxw;
1591 if (f->fmt.pix.height > maxh)
1592 f->fmt.pix.height = maxh;
1593 f->fmt.pix.width &= ~0x03;
1594 f->fmt.pix.bytesperline =
1595 (f->fmt.pix.width * fmt->depth) >> 3;
1596 f->fmt.pix.sizeimage =
1597 f->fmt.pix.height * f->fmt.pix.bytesperline;
1598
1599 return 0;
1600 }
1601 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehabecb73772006-03-17 20:54:32 -03001602 if (saa7134_no_overlay > 0) {
1603 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1604 return -EINVAL;
1605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 err = verify_preview(dev,&f->fmt.win);
1607 if (0 != err)
1608 return err;
1609 return 0;
1610 case V4L2_BUF_TYPE_VBI_CAPTURE:
1611 saa7134_vbi_fmt(dev,f);
1612 return 0;
1613 default:
1614 return -EINVAL;
1615 }
1616}
1617
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001618static int vidioc_s_fmt_cap(struct file *file, void *priv,
1619 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001621 struct saa7134_fh *fh = priv;
1622 struct saa7134_dev *dev = fh->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 unsigned long flags;
1624 int err;
1625
1626 switch (f->type) {
1627 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001628 err = vidioc_try_fmt_cap(file, priv, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (0 != err)
1630 return err;
1631
1632 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1633 fh->width = f->fmt.pix.width;
1634 fh->height = f->fmt.pix.height;
1635 fh->cap.field = f->fmt.pix.field;
1636 return 0;
1637 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehabecb73772006-03-17 20:54:32 -03001638 if (saa7134_no_overlay > 0) {
1639 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1640 return -EINVAL;
1641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 err = verify_preview(dev,&f->fmt.win);
1643 if (0 != err)
1644 return err;
1645
Ingo Molnar3593cab2006-02-07 06:49:14 -02001646 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 fh->win = f->fmt.win;
1648 fh->nclips = f->fmt.win.clipcount;
1649 if (fh->nclips > 8)
1650 fh->nclips = 8;
1651 if (copy_from_user(fh->clips,f->fmt.win.clips,
1652 sizeof(struct v4l2_clip)*fh->nclips)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02001653 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return -EFAULT;
1655 }
1656
1657 if (res_check(fh, RESOURCE_OVERLAY)) {
1658 spin_lock_irqsave(&dev->slock,flags);
1659 stop_preview(dev,fh);
1660 start_preview(dev,fh);
1661 spin_unlock_irqrestore(&dev->slock,flags);
1662 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02001663 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return 0;
1665 case V4L2_BUF_TYPE_VBI_CAPTURE:
1666 saa7134_vbi_fmt(dev,f);
1667 return 0;
1668 default:
1669 return -EINVAL;
1670 }
1671}
1672
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001673static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001675 struct saa7134_fh *fh = priv;
1676 struct saa7134_dev *dev = fh->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001678 *i = dev->ctl_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return 0;
1680}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001682static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001684 struct saa7134_fh *fh = priv;
1685 struct saa7134_dev *dev = fh->dev;
1686
1687 if (i < 0 || i >= SAA7134_INPUT_MAX)
1688 return -EINVAL;
1689 if (NULL == card_in(dev, i).name)
1690 return -EINVAL;
1691 mutex_lock(&dev->lock);
1692 video_mux(dev, i);
1693 mutex_unlock(&dev->lock);
1694 return 0;
1695}
1696
1697static int vidioc_queryctrl(struct file *file, void *priv,
1698 struct v4l2_queryctrl *c)
1699{
1700 const struct v4l2_queryctrl *ctrl;
1701
1702 if ((c->id < V4L2_CID_BASE ||
1703 c->id >= V4L2_CID_LASTP1) &&
1704 (c->id < V4L2_CID_PRIVATE_BASE ||
1705 c->id >= V4L2_CID_PRIVATE_LASTP1))
1706 return -EINVAL;
1707 ctrl = ctrl_by_id(c->id);
1708 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1709 return 0;
1710}
1711
1712static int vidioc_enum_input(struct file *file, void *priv,
1713 struct v4l2_input *i)
1714{
1715 struct saa7134_fh *fh = priv;
1716 struct saa7134_dev *dev = fh->dev;
1717 unsigned int n;
1718
1719 n = i->index;
1720 if (n >= SAA7134_INPUT_MAX)
1721 return -EINVAL;
1722 if (NULL == card_in(dev, i->index).name)
1723 return -EINVAL;
1724 memset(i, 0, sizeof(*i));
1725 i->index = n;
1726 i->type = V4L2_INPUT_TYPE_CAMERA;
1727 strcpy(i->name, card_in(dev, n).name);
1728 if (card_in(dev, n).tv)
1729 i->type = V4L2_INPUT_TYPE_TUNER;
1730 i->audioset = 1;
1731 if (n == dev->ctl_input) {
1732 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1733 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1734
1735 if (0 != (v1 & 0x40))
1736 i->status |= V4L2_IN_ST_NO_H_LOCK;
1737 if (0 != (v2 & 0x40))
1738 i->status |= V4L2_IN_ST_NO_SYNC;
1739 if (0 != (v2 & 0x0e))
1740 i->status |= V4L2_IN_ST_MACROVISION;
1741 }
1742 for (n = 0; n < TVNORMS; n++)
1743 i->std |= tvnorms[n].id;
1744 return 0;
1745}
1746
1747static int vidioc_querycap(struct file *file, void *priv,
1748 struct v4l2_capability *cap)
1749{
1750 struct saa7134_fh *fh = priv;
1751 struct saa7134_dev *dev = fh->dev;
1752
1753 unsigned int tuner_type = dev->tuner_type;
1754
1755 memset(cap, 0, sizeof(*cap));
1756 strcpy(cap->driver, "saa7134");
1757 strlcpy(cap->card, saa7134_boards[dev->board].name,
1758 sizeof(cap->card));
1759 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1760 cap->version = SAA7134_VERSION_CODE;
1761 cap->capabilities =
1762 V4L2_CAP_VIDEO_CAPTURE |
1763 V4L2_CAP_VBI_CAPTURE |
1764 V4L2_CAP_READWRITE |
1765 V4L2_CAP_STREAMING |
1766 V4L2_CAP_TUNER;
1767 if (saa7134_no_overlay <= 0)
1768 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1769
1770 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1771 cap->capabilities &= ~V4L2_CAP_TUNER;
1772 return 0;
1773}
1774
1775static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * id)
1776{
1777 struct saa7134_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 struct saa7134_dev *dev = fh->dev;
1779 unsigned long flags;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001780 unsigned int i;
1781 v4l2_std_id fixup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001783 for (i = 0; i < TVNORMS; i++)
1784 if (*id == tvnorms[i].id)
1785 break;
1786 if (i == TVNORMS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 for (i = 0; i < TVNORMS; i++)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001788 if (*id & tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 break;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001790 if (i == TVNORMS)
1791 return -EINVAL;
1792 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1793 if (secam[0] == 'L' || secam[0] == 'l') {
1794 if (secam[1] == 'C' || secam[1] == 'c')
1795 fixup = V4L2_STD_SECAM_LC;
1796 else
1797 fixup = V4L2_STD_SECAM_L;
1798 } else {
1799 if (secam[0] == 'D' || secam[0] == 'd')
1800 fixup = V4L2_STD_SECAM_DK;
1801 else
1802 fixup = V4L2_STD_SECAM;
Hartmut Hackmann17b10a72006-10-02 19:55:07 -03001803 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001804 for (i = 0; i < TVNORMS; i++)
1805 if (fixup == tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001808 mutex_lock(&dev->lock);
1809 if (res_check(fh, RESOURCE_OVERLAY)) {
1810 spin_lock_irqsave(&dev->slock, flags);
1811 stop_preview(dev, fh);
1812 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001814 set_tvnorm(dev, &tvnorms[i]);
1815
1816 spin_lock_irqsave(&dev->slock, flags);
1817 start_preview(dev, fh);
1818 spin_unlock_irqrestore(&dev->slock, flags);
1819 } else
1820 set_tvnorm(dev, &tvnorms[i]);
1821 saa7134_tvaudio_do_scan(dev);
1822 mutex_unlock(&dev->lock);
1823 return 0;
1824}
1825
1826static int vidioc_cropcap(struct file *file, void *priv,
1827 struct v4l2_cropcap *cap)
1828{
1829 struct saa7134_fh *fh = priv;
1830 struct saa7134_dev *dev = fh->dev;
1831
1832 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1833 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1834 return -EINVAL;
1835 cap->bounds = dev->crop_bounds;
1836 cap->defrect = dev->crop_defrect;
1837 cap->pixelaspect.numerator = 1;
1838 cap->pixelaspect.denominator = 1;
1839 if (dev->tvnorm->id & V4L2_STD_525_60) {
1840 cap->pixelaspect.numerator = 11;
1841 cap->pixelaspect.denominator = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001843 if (dev->tvnorm->id & V4L2_STD_625_50) {
1844 cap->pixelaspect.numerator = 54;
1845 cap->pixelaspect.denominator = 59;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847 return 0;
1848}
1849
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001850static int vidioc_g_tuner(struct file *file, void *priv,
1851 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001853 struct saa7134_fh *fh = priv;
1854 struct saa7134_dev *dev = fh->dev;
1855 int n;
1856
1857 if (0 != t->index)
1858 return -EINVAL;
1859 memset(t, 0, sizeof(*t));
1860 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1861 if (card_in(dev, n).tv)
1862 break;
1863 if (NULL != card_in(dev, n).name) {
1864 strcpy(t->name, "Television");
1865 t->type = V4L2_TUNER_ANALOG_TV;
1866 t->capability = V4L2_TUNER_CAP_NORM |
1867 V4L2_TUNER_CAP_STEREO |
1868 V4L2_TUNER_CAP_LANG1 |
1869 V4L2_TUNER_CAP_LANG2;
1870 t->rangehigh = 0xffffffffUL;
1871 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1872 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1873 }
1874 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1875 t->signal = 0xffff;
1876 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001879static int vidioc_s_tuner(struct file *file, void *priv,
1880 struct v4l2_tuner *t)
1881{
1882 struct saa7134_fh *fh = priv;
1883 struct saa7134_dev *dev = fh->dev;
1884 int rx, mode;
1885
1886 mode = dev->thread.mode;
1887 if (UNSET == mode) {
1888 rx = saa7134_tvaudio_getstereo(dev);
1889 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1890 }
1891 if (mode != t->audmode)
1892 dev->thread.mode = t->audmode;
1893 return 0;
1894}
1895
1896static int vidioc_g_frequency(struct file *file, void *priv,
1897 struct v4l2_frequency *f)
1898{
1899 struct saa7134_fh *fh = priv;
1900 struct saa7134_dev *dev = fh->dev;
1901
1902 memset(f, 0, sizeof(*f));
1903 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1904 f->frequency = dev->ctl_freq;
1905 return 0;
1906}
1907
1908static int vidioc_s_frequency(struct file *file, void *priv,
1909 struct v4l2_frequency *f)
1910{
1911 struct saa7134_fh *fh = priv;
1912 struct saa7134_dev *dev = fh->dev;
1913
1914 if (0 != f->tuner)
1915 return -EINVAL;
1916 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1917 return -EINVAL;
1918 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1919 return -EINVAL;
1920 mutex_lock(&dev->lock);
1921 dev->ctl_freq = f->frequency;
1922
1923 saa7134_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1924
1925 saa7134_tvaudio_do_scan(dev);
1926 mutex_unlock(&dev->lock);
1927 return 0;
1928}
1929
1930static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1931{
1932 memset(a, 0, sizeof(*a));
1933 strcpy(a->name, "audio");
1934 return 0;
1935}
1936
1937static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1938{
1939 return 0;
1940}
1941
1942static int vidioc_streamon(struct file *file, void *priv,
1943 enum v4l2_buf_type type)
1944{
1945 struct saa7134_fh *fh = priv;
1946 struct saa7134_dev *dev = fh->dev;
1947 int res = saa7134_resource(fh);
1948
1949 if (!res_get(dev, fh, res))
1950 return -EBUSY;
1951
1952 return videobuf_streamon(saa7134_queue(fh));
1953}
1954
1955static int vidioc_streamoff(struct file *file, void *priv,
1956 enum v4l2_buf_type type)
1957{
1958 int err;
1959 struct saa7134_fh *fh = priv;
1960 struct saa7134_dev *dev = fh->dev;
1961 int res = saa7134_resource(fh);
1962
1963 err = videobuf_streamoff(saa7134_queue(fh));
1964 if (err < 0)
1965 return err;
1966 res_free(dev, fh, res);
1967 return 0;
1968}
1969
1970static int vidioc_reqbufs(struct file *file, void *priv,
1971 struct v4l2_requestbuffers *p)
1972{
1973 struct saa7134_fh *fh = priv;
1974 return videobuf_reqbufs(saa7134_queue(fh), p);
1975}
1976
1977static int vidioc_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
1978{
1979 struct saa7134_fh *fh = f;
1980 struct saa7134_dev *dev = fh->dev;
1981 struct v4l2_rect *b = &dev->crop_bounds;
1982
1983 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1984 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1985 return -EINVAL;
1986 if (crop->c.height < 0)
1987 return -EINVAL;
1988 if (crop->c.width < 0)
1989 return -EINVAL;
1990
1991 if (res_locked(fh->dev, RESOURCE_OVERLAY))
1992 return -EBUSY;
1993 if (res_locked(fh->dev, RESOURCE_VIDEO))
1994 return -EBUSY;
1995
1996 if (crop->c.top < b->top)
1997 crop->c.top = b->top;
1998 if (crop->c.top > b->top + b->height)
1999 crop->c.top = b->top + b->height;
2000 if (crop->c.height > b->top - crop->c.top + b->height)
2001 crop->c.height = b->top - crop->c.top + b->height;
2002
2003 if (crop->c.left < b->left)
2004 crop->c.left = b->left;
2005 if (crop->c.left > b->left + b->width)
2006 crop->c.left = b->left + b->width;
2007 if (crop->c.width > b->left - crop->c.left + b->width)
2008 crop->c.width = b->left - crop->c.left + b->width;
2009
2010 dev->crop_current = crop->c;
2011 return 0;
2012}
2013
2014static int vidioc_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
2015{
2016 struct saa7134_fh *fh = f;
2017 struct saa7134_dev *dev = fh->dev;
2018
2019 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2020 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2021 return -EINVAL;
2022 crop->c = dev->crop_current;
2023 return 0;
2024}
2025
2026static int vidioc_g_parm(struct file *file, void *fh,
2027 struct v4l2_streamparm *parm)
2028{
2029 memset(parm, 0, sizeof(*parm));
2030 return 0;
2031}
2032
2033static int vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2034{
2035 struct saa7134_fh *fh = f;
2036 struct saa7134_dev *dev = fh->dev;
2037
2038 *p = v4l2_prio_max(&dev->prio);
2039 return 0;
2040}
2041
2042static int vidioc_s_priority(struct file *file, void *f,
2043 enum v4l2_priority prio)
2044{
2045 struct saa7134_fh *fh = f;
2046 struct saa7134_dev *dev = fh->dev;
2047
2048 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
2049}
2050
2051static int vidioc_g_fbuf(struct file *file, void *f,
2052 struct v4l2_framebuffer *fb)
2053{
2054 struct saa7134_fh *fh = f;
2055 struct saa7134_dev *dev = fh->dev;
2056
2057 *fb = dev->ovbuf;
2058 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2059
2060 return 0;
2061}
2062
2063static int vidioc_s_fbuf(struct file *file, void *f,
2064 struct v4l2_framebuffer *fb)
2065{
2066 struct saa7134_fh *fh = f;
2067 struct saa7134_dev *dev = fh->dev;
2068 struct saa7134_format *fmt;
2069
2070 if (!capable(CAP_SYS_ADMIN) &&
2071 !capable(CAP_SYS_RAWIO))
2072 return -EPERM;
2073
2074 /* check args */
2075 fmt = format_by_fourcc(fb->fmt.pixelformat);
2076 if (NULL == fmt)
2077 return -EINVAL;
2078
2079 /* ok, accept it */
2080 dev->ovbuf = *fb;
2081 dev->ovfmt = fmt;
2082 if (0 == dev->ovbuf.fmt.bytesperline)
2083 dev->ovbuf.fmt.bytesperline =
2084 dev->ovbuf.fmt.width*fmt->depth/8;
2085 return 0;
2086}
2087
2088static int vidioc_querybuf(struct file *file, void *priv,
2089 struct v4l2_buffer *b)
2090{
2091 struct saa7134_fh *fh = priv;
2092 return videobuf_querybuf(saa7134_queue(fh), b);
2093}
2094
2095static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2096{
2097 struct saa7134_fh *fh = priv;
2098 return videobuf_qbuf(saa7134_queue(fh), b);
2099}
2100
2101static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2102{
2103 struct saa7134_fh *fh = priv;
2104 return videobuf_dqbuf(saa7134_queue(fh), b,
2105 file->f_flags & O_NONBLOCK);
2106}
2107
2108static int vidioc_overlay(struct file *file, void *f, unsigned int on)
2109{
2110 struct saa7134_fh *fh = f;
2111 struct saa7134_dev *dev = fh->dev;
2112 unsigned long flags;
2113
2114 if (on) {
2115 if (saa7134_no_overlay > 0) {
2116 dprintk("no_overlay\n");
2117 return -EINVAL;
2118 }
2119
2120 if (!res_get(dev, fh, RESOURCE_OVERLAY))
2121 return -EBUSY;
2122 spin_lock_irqsave(&dev->slock, flags);
2123 start_preview(dev, fh);
2124 spin_unlock_irqrestore(&dev->slock, flags);
2125 }
2126 if (!on) {
2127 if (!res_check(fh, RESOURCE_OVERLAY))
2128 return -EINVAL;
2129 spin_lock_irqsave(&dev->slock, flags);
2130 stop_preview(dev, fh);
2131 spin_unlock_irqrestore(&dev->slock, flags);
2132 res_free(dev, fh, RESOURCE_OVERLAY);
2133 }
2134 return 0;
2135}
2136
2137static int vidioc_enum_fmt_cap(struct file *file, void *priv,
2138 struct v4l2_fmtdesc *f)
2139{
2140 enum v4l2_buf_type type;
2141 unsigned int index;
2142
2143 index = f->index;
2144 type = f->type;
2145 switch (type) {
2146 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2147 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2148 if (saa7134_no_overlay > 0)
2149 return -EINVAL;
2150
2151 if (index >= FORMATS)
2152 return -EINVAL;
2153
2154 if (f->type == V4L2_BUF_TYPE_VIDEO_OVERLAY &&
2155 formats[index].planar)
2156 return -EINVAL;
2157 memset(f, 0, sizeof(*f));
2158 f->index = index;
2159 f->type = type;
2160 strlcpy(f->description, formats[index].name,
2161 sizeof(f->description));
2162 f->pixelformat = formats[index].fourcc;
2163 break;
2164 case V4L2_BUF_TYPE_VBI_CAPTURE:
2165 if (0 != index)
2166 return -EINVAL;
2167 memset(f, 0, sizeof(*f));
2168 f->index = index;
2169 f->type = type;
2170 f->pixelformat = V4L2_PIX_FMT_GREY;
2171 strcpy(f->description, "vbi data");
2172 break;
2173 default:
2174 return -EINVAL;
2175 }
2176 return 0;
2177}
2178
2179#ifdef CONFIG_VIDEO_V4L1_COMPAT
2180static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2181{
2182 struct saa7134_fh *fh = file->private_data;
2183 return videobuf_cgmbuf(saa7134_queue(fh), mbuf, 8);
2184}
2185#endif
2186
2187static int radio_querycap(struct file *file, void *priv,
2188 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
2190 struct saa7134_fh *fh = file->private_data;
2191 struct saa7134_dev *dev = fh->dev;
2192
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002193 memset(cap, 0, sizeof(*cap));
2194 strcpy(cap->driver, "saa7134");
2195 strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
2196 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
2197 cap->version = SAA7134_VERSION_CODE;
2198 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return 0;
2200}
2201
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002202static int radio_g_tuner(struct file *file, void *priv,
2203 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002205 struct saa7134_fh *fh = file->private_data;
2206 struct saa7134_dev *dev = fh->dev;
2207
2208 if (0 != t->index)
2209 return -EINVAL;
2210
2211 memset(t, 0, sizeof(*t));
2212 strcpy(t->name, "Radio");
2213 t->type = V4L2_TUNER_RADIO;
2214
2215 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
2216 if (dev->input->amux == TV) {
2217 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2218 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2219 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2220 }
2221 return 0;
2222}
2223static int radio_s_tuner(struct file *file, void *priv,
2224 struct v4l2_tuner *t)
2225{
2226 struct saa7134_fh *fh = file->private_data;
2227 struct saa7134_dev *dev = fh->dev;
2228
2229 if (0 != t->index)
2230 return -EINVAL;
2231
2232 saa7134_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
2233 return 0;
2234}
2235
2236static int radio_enum_input(struct file *file, void *priv,
2237 struct v4l2_input *i)
2238{
2239 if (i->index != 0)
2240 return -EINVAL;
2241
2242 strcpy(i->name, "Radio");
2243 i->type = V4L2_INPUT_TYPE_TUNER;
2244
2245 return 0;
2246}
2247
2248static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2249{
2250 *i = 0;
2251 return 0;
2252}
2253
2254static int radio_g_audio(struct file *file, void *priv,
2255 struct v4l2_audio *a)
2256{
2257 memset(a, 0, sizeof(*a));
2258 strcpy(a->name, "Radio");
2259 return 0;
2260}
2261
2262static int radio_s_audio(struct file *file, void *priv,
2263 struct v4l2_audio *a)
2264{
2265 return 0;
2266}
2267
2268static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2269{
2270 return 0;
2271}
2272
2273static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
2274{
2275 return 0;
2276}
2277
2278static int radio_queryctrl(struct file *file, void *priv,
2279 struct v4l2_queryctrl *c)
2280{
2281 const struct v4l2_queryctrl *ctrl;
2282
2283 if (c->id < V4L2_CID_BASE ||
2284 c->id >= V4L2_CID_LASTP1)
2285 return -EINVAL;
2286 if (c->id == V4L2_CID_AUDIO_MUTE) {
2287 ctrl = ctrl_by_id(c->id);
2288 *c = *ctrl;
2289 } else
2290 *c = no_ctrl;
2291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
Arjan van de Venfa027c22007-02-12 00:55:33 -08002294static const struct file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 .owner = THIS_MODULE,
2297 .open = video_open,
2298 .release = video_release,
2299 .read = video_read,
2300 .poll = video_poll,
2301 .mmap = video_mmap,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002302 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02002303 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 .llseek = no_llseek,
2305};
2306
Arjan van de Venfa027c22007-02-12 00:55:33 -08002307static const struct file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 .owner = THIS_MODULE,
2310 .open = video_open,
2311 .release = video_release,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002312 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02002313 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 .llseek = no_llseek,
2315};
2316
2317/* ----------------------------------------------------------- */
2318/* exported stuff */
2319
2320struct video_device saa7134_video_template =
2321{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002322 .name = "saa7134-video",
2323 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER |
2324 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2325 .fops = &video_fops,
2326 .minor = -1,
2327 .vidioc_querycap = vidioc_querycap,
2328 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
2329 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
2330 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
2331 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
2332 .vidioc_g_audio = vidioc_g_audio,
2333 .vidioc_s_audio = vidioc_s_audio,
2334 .vidioc_cropcap = vidioc_cropcap,
2335 .vidioc_reqbufs = vidioc_reqbufs,
2336 .vidioc_querybuf = vidioc_querybuf,
2337 .vidioc_qbuf = vidioc_qbuf,
2338 .vidioc_dqbuf = vidioc_dqbuf,
2339 .vidioc_s_std = vidioc_s_std,
2340 .vidioc_enum_input = vidioc_enum_input,
2341 .vidioc_g_input = vidioc_g_input,
2342 .vidioc_s_input = vidioc_s_input,
2343 .vidioc_queryctrl = vidioc_queryctrl,
2344 .vidioc_g_ctrl = vidioc_g_ctrl,
2345 .vidioc_s_ctrl = vidioc_s_ctrl,
2346 .vidioc_streamon = vidioc_streamon,
2347 .vidioc_streamoff = vidioc_streamoff,
2348 .vidioc_g_tuner = vidioc_g_tuner,
2349 .vidioc_s_tuner = vidioc_s_tuner,
2350#ifdef CONFIG_VIDEO_V4L1_COMPAT
2351 .vidiocgmbuf = vidiocgmbuf,
2352#endif
2353 .vidioc_g_crop = vidioc_g_crop,
2354 .vidioc_s_crop = vidioc_s_crop,
2355 .vidioc_g_fbuf = vidioc_g_fbuf,
2356 .vidioc_s_fbuf = vidioc_s_fbuf,
2357 .vidioc_overlay = vidioc_overlay,
2358 .vidioc_g_priority = vidioc_g_priority,
2359 .vidioc_s_priority = vidioc_s_priority,
2360 .vidioc_g_parm = vidioc_g_parm,
2361 .vidioc_g_frequency = vidioc_g_frequency,
2362 .vidioc_s_frequency = vidioc_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363};
2364
2365struct video_device saa7134_vbi_template =
2366{
2367 .name = "saa7134-vbi",
2368 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 .fops = &video_fops,
2370 .minor = -1,
2371};
2372
2373struct video_device saa7134_radio_template =
2374{
2375 .name = "saa7134-radio",
2376 .type = VID_TYPE_TUNER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 .fops = &radio_fops,
2378 .minor = -1,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002379 .vidioc_querycap = radio_querycap,
2380 .vidioc_g_tuner = radio_g_tuner,
2381 .vidioc_enum_input = radio_enum_input,
2382 .vidioc_g_audio = radio_g_audio,
2383 .vidioc_s_tuner = radio_s_tuner,
2384 .vidioc_s_audio = radio_s_audio,
2385 .vidioc_s_input = radio_s_input,
2386 .vidioc_s_std = radio_s_std,
2387 .vidioc_queryctrl = radio_queryctrl,
2388 .vidioc_g_input = radio_g_input,
2389 .vidioc_g_ctrl = vidioc_g_ctrl,
2390 .vidioc_s_ctrl = vidioc_s_ctrl,
2391 .vidioc_g_frequency = vidioc_g_frequency,
2392 .vidioc_s_frequency = vidioc_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393};
2394
2395int saa7134_video_init1(struct saa7134_dev *dev)
2396{
2397 /* sanitycheck insmod options */
2398 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2399 gbuffers = 2;
2400 if (gbufsize < 0 || gbufsize > gbufsize_max)
2401 gbufsize = gbufsize_max;
2402 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2403
2404 /* put some sensible defaults into the data structures ... */
2405 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2406 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2407 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2408 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2409 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2410 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2411 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2412 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2413
2414 if (dev->tda9887_conf && dev->ctl_automute)
2415 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2416 dev->automute = 0;
2417
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002418 INIT_LIST_HEAD(&dev->video_q.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 init_timer(&dev->video_q.timeout);
2420 dev->video_q.timeout.function = saa7134_buffer_timeout;
2421 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2422 dev->video_q.dev = dev;
2423
Maxim Levitskycb712012007-09-27 20:34:25 -03002424 if (saa7134_boards[dev->board].video_out)
2425 saa7134_videoport_init(dev);
2426
2427 return 0;
2428}
2429
2430int saa7134_videoport_init(struct saa7134_dev *dev)
2431{
2432 /* enable video output */
2433 int vo = saa7134_boards[dev->board].video_out;
2434 int video_reg;
2435 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2436 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2437 video_reg = video_out[vo][1];
2438 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2439 video_reg &= ~VP_T_CODE_P_INVERTED;
2440 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2441 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2442 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2443 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2444 video_reg = video_out[vo][5];
2445 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2446 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2447 if (vid_port_opts & SET_CLOCK_INVERTED)
2448 video_reg |= VP_CLK_CTRL1_INVERTED;
2449 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2450 video_reg = video_out[vo][6];
2451 if (vid_port_opts & SET_VSYNC_OFF) {
2452 video_reg &= ~VP_VS_TYPE_MASK;
2453 video_reg |= VP_VS_TYPE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
Maxim Levitskycb712012007-09-27 20:34:25 -03002455 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2456 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2457 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459 return 0;
2460}
2461
2462int saa7134_video_init2(struct saa7134_dev *dev)
2463{
2464 /* init video hw */
2465 set_tvnorm(dev,&tvnorms[0]);
2466 video_mux(dev,0);
2467 saa7134_tvaudio_setmute(dev);
2468 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2469 return 0;
2470}
2471
Maxim Levitsky11f70782007-09-27 20:44:39 -03002472void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
2474 static const char *st[] = {
2475 "(no signal)", "NTSC", "PAL", "SECAM" };
2476 u32 st1,st2;
2477
2478 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2479 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2480 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2481 (st1 & 0x40) ? "not locked" : "locked",
2482 (st2 & 0x40) ? "no" : "yes",
2483 st[st1 & 0x03]);
Maxim Levitsky11f70782007-09-27 20:44:39 -03002484 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 if (dev->nosignal) {
2487 /* no video signal -> mute audio */
2488 if (dev->ctl_automute)
2489 dev->automute = 1;
2490 saa7134_tvaudio_setmute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 } else {
2492 /* wake up tvaudio audio carrier scan thread */
2493 saa7134_tvaudio_do_scan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
Maxim Levitsky11f70782007-09-27 20:44:39 -03002495
2496 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2497 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2498 else
2499 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 if (dev->mops && dev->mops->signal_change)
2502 dev->mops->signal_change(dev);
2503}
2504
Maxim Levitsky11f70782007-09-27 20:44:39 -03002505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2507{
2508 enum v4l2_field field;
2509
2510 spin_lock(&dev->slock);
2511 if (dev->video_q.curr) {
2512 dev->video_fieldcount++;
2513 field = dev->video_q.curr->vb.field;
2514 if (V4L2_FIELD_HAS_BOTH(field)) {
2515 /* make sure we have seen both fields */
2516 if ((status & 0x10) == 0x00) {
2517 dev->video_q.curr->top_seen = 1;
2518 goto done;
2519 }
2520 if (!dev->video_q.curr->top_seen)
2521 goto done;
2522 } else if (field == V4L2_FIELD_TOP) {
2523 if ((status & 0x10) != 0x10)
2524 goto done;
2525 } else if (field == V4L2_FIELD_BOTTOM) {
2526 if ((status & 0x10) != 0x00)
2527 goto done;
2528 }
2529 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
Brandon Philips0fc06862007-11-06 20:02:36 -03002530 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
2532 saa7134_buffer_next(dev,&dev->video_q);
2533
2534 done:
2535 spin_unlock(&dev->slock);
2536}
2537
2538/* ----------------------------------------------------------- */
2539/*
2540 * Local variables:
2541 * c-basic-offset: 8
2542 * End:
2543 */