blob: 525b5b77c13a8fed3e9b1c4d739a99905ec01f7e [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;
Hartmut Hackmannd5fdd132006-07-15 09:45:34 -030043static unsigned int noninterlaced = 1;
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
543static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
544{
545 int luma_control,sync_control,mux;
546
547 dprintk("set tv norm = %s\n",norm->name);
548 dev->tvnorm = norm;
549
550 mux = card_in(dev,dev->ctl_input).vmux;
551 luma_control = norm->luma_control;
552 sync_control = norm->sync_control;
553
554 if (mux > 5)
555 luma_control |= 0x80; /* svideo */
556 if (noninterlaced || dev->nosignal)
557 sync_control |= 0x20;
558
559 /* setup cropping */
560 dev->crop_bounds.left = norm->h_start;
561 dev->crop_defrect.left = norm->h_start;
562 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
563 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
564
Michael Schimekf246a812005-06-23 22:04:47 -0700565 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 dev->crop_defrect.top = norm->video_v_start*2;
567 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
568 - dev->crop_bounds.top;
569 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
570
571 dev->crop_current = dev->crop_defrect;
572
573 /* setup video decoder */
574 saa_writeb(SAA7134_INCR_DELAY, 0x08);
575 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
576 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
577
578 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
579 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
580 saa_writeb(SAA7134_HSYNC_START, 0xeb);
581 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
582 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
583
584 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
585 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
586 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Maxim Levitskyf5a1ac62007-09-27 20:34:20 -0300588 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
589 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
590
591 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
592 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
595 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
596 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
597
598 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
599 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
600
601 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
602 saa_writeb(SAA7134_VGATE_START, 0x11);
603 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
604 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
605 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
606 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
607
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300608 /* only tell the tuner if this is a tv input */
609 if (card_in(dev,dev->ctl_input).tv) {
610 if ((card(dev).tuner_type == TUNER_PHILIPS_TDA8290)
611 && ((card(dev).tuner_config == 1)
612 || (card(dev).tuner_config == 2)))
613 saa7134_set_gpio(dev, 22, 5);
614 saa7134_i2c_call_clients(dev,VIDIOC_S_STD,&norm->id);
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618static void video_mux(struct saa7134_dev *dev, int input)
619{
620 dprintk("video input = %d [%s]\n",input,card_in(dev,input).name);
621 dev->ctl_input = input;
622 set_tvnorm(dev,dev->tvnorm);
623 saa7134_tvaudio_setinput(dev,&card_in(dev,input));
624}
625
626static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
627{
628 static const struct {
629 int xpsc;
630 int xacl;
631 int xc2_1;
632 int xdcg;
633 int vpfy;
634 } vals[] = {
635 /* XPSC XACL XC2_1 XDCG VPFY */
636 { 1, 0, 0, 0, 0 },
637 { 2, 2, 1, 2, 2 },
638 { 3, 4, 1, 3, 2 },
639 { 4, 8, 1, 4, 2 },
640 { 5, 8, 1, 4, 2 },
641 { 6, 8, 1, 4, 3 },
642 { 7, 8, 1, 4, 3 },
643 { 8, 15, 0, 4, 3 },
644 { 9, 15, 0, 4, 3 },
645 { 10, 16, 1, 5, 3 },
646 };
647 static const int count = ARRAY_SIZE(vals);
648 int i;
649
650 for (i = 0; i < count; i++)
651 if (vals[i].xpsc == prescale)
652 break;
653 if (i == count)
654 return;
655
656 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
657 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
658 saa_writeb(SAA7134_LEVEL_CTRL(task),
659 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
660 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
661 (vals[i].vpfy << 2) | vals[i].vpfy);
662}
663
664static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
665{
666 int val,mirror;
667
668 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
669 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
670
671 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
672 if (yscale < 2048) {
673 /* LPI */
674 dprintk("yscale LPI yscale=%d\n",yscale);
675 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
676 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
677 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
678 } else {
679 /* ACM */
680 val = 0x40 * 1024 / yscale;
681 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
682 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
683 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
684 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
685 }
686 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
687}
688
689static void set_size(struct saa7134_dev *dev, int task,
690 int width, int height, int interlace)
691{
692 int prescale,xscale,yscale,y_even,y_odd;
693 int h_start, h_stop, v_start, v_stop;
694 int div = interlace ? 2 : 1;
695
696 /* setup video scaler */
697 h_start = dev->crop_current.left;
698 v_start = dev->crop_current.top/2;
699 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
700 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
701
702 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
703 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
704 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
705 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
706 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
707 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
708 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
709 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
710
711 prescale = dev->crop_current.width / width;
712 if (0 == prescale)
713 prescale = 1;
714 xscale = 1024 * dev->crop_current.width / prescale / width;
715 yscale = 512 * div * dev->crop_current.height / height;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800716 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 set_h_prescale(dev,task,prescale);
718 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
719 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
720 set_v_scale(dev,task,yscale);
721
722 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
723 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
724 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
725 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
726
727 /* deinterlace y offsets */
728 y_odd = dev->ctl_y_odd;
729 y_even = dev->ctl_y_even;
730 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
731 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
732 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
733 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
734}
735
736/* ------------------------------------------------------------------ */
737
738struct cliplist {
739 __u16 position;
740 __u8 enable;
741 __u8 disable;
742};
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744static void set_cliplist(struct saa7134_dev *dev, int reg,
745 struct cliplist *cl, int entries, char *name)
746{
747 __u8 winbits = 0;
748 int i;
749
750 for (i = 0; i < entries; i++) {
751 winbits |= cl[i].enable;
752 winbits &= ~cl[i].disable;
753 if (i < 15 && cl[i].position == cl[i+1].position)
754 continue;
755 saa_writeb(reg + 0, winbits);
756 saa_writeb(reg + 2, cl[i].position & 0xff);
757 saa_writeb(reg + 3, cl[i].position >> 8);
758 dprintk("clip: %s winbits=%02x pos=%d\n",
759 name,winbits,cl[i].position);
760 reg += 8;
761 }
762 for (; reg < 0x400; reg += 8) {
763 saa_writeb(reg+ 0, 0);
764 saa_writeb(reg + 1, 0);
765 saa_writeb(reg + 2, 0);
766 saa_writeb(reg + 3, 0);
767 }
768}
769
770static int clip_range(int val)
771{
772 if (val < 0)
773 val = 0;
774 return val;
775}
776
Heikki Orsila9040b322007-04-27 12:31:18 -0300777/* Sort into smallest position first order */
778static int cliplist_cmp(const void *a, const void *b)
779{
780 const struct cliplist *cla = a;
781 const struct cliplist *clb = b;
782 if (cla->position < clb->position)
783 return -1;
784 if (cla->position > clb->position)
785 return 1;
786 return 0;
787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
790 int nclips, int interlace)
791{
792 struct cliplist col[16], row[16];
Heikki Orsila9040b322007-04-27 12:31:18 -0300793 int cols = 0, rows = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int div = interlace ? 2 : 1;
795
Heikki Orsila9040b322007-04-27 12:31:18 -0300796 memset(col, 0, sizeof(col));
797 memset(row, 0, sizeof(row));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 for (i = 0; i < nclips && i < 8; i++) {
799 col[cols].position = clip_range(clips[i].c.left);
800 col[cols].enable = (1 << i);
801 cols++;
802 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
803 col[cols].disable = (1 << i);
804 cols++;
805 row[rows].position = clip_range(clips[i].c.top / div);
806 row[rows].enable = (1 << i);
807 rows++;
808 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
809 / div);
810 row[rows].disable = (1 << i);
811 rows++;
812 }
Heikki Orsila9040b322007-04-27 12:31:18 -0300813 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
814 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 set_cliplist(dev,0x380,col,cols,"cols");
816 set_cliplist(dev,0x384,row,rows,"rows");
817 return 0;
818}
819
820static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
821{
822 enum v4l2_field field;
823 int maxw, maxh;
824
825 if (NULL == dev->ovbuf.base)
826 return -EINVAL;
827 if (NULL == dev->ovfmt)
828 return -EINVAL;
829 if (win->w.width < 48 || win->w.height < 32)
830 return -EINVAL;
831 if (win->clipcount > 2048)
832 return -EINVAL;
833
834 field = win->field;
835 maxw = dev->crop_current.width;
836 maxh = dev->crop_current.height;
837
838 if (V4L2_FIELD_ANY == field) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800839 field = (win->w.height > maxh/2)
840 ? V4L2_FIELD_INTERLACED
841 : V4L2_FIELD_TOP;
842 }
843 switch (field) {
844 case V4L2_FIELD_TOP:
845 case V4L2_FIELD_BOTTOM:
846 maxh = maxh / 2;
847 break;
848 case V4L2_FIELD_INTERLACED:
849 break;
850 default:
851 return -EINVAL;
852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 win->field = field;
855 if (win->w.width > maxw)
856 win->w.width = maxw;
857 if (win->w.height > maxh)
858 win->w.height = maxh;
859 return 0;
860}
861
862static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
863{
864 unsigned long base,control,bpl;
865 int err;
866
867 err = verify_preview(dev,&fh->win);
868 if (0 != err)
869 return err;
870
871 dev->ovfield = fh->win.field;
872 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
873 fh->win.w.width,fh->win.w.height,
874 fh->win.w.left,fh->win.w.top,
875 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
876
877 /* setup window + clipping */
878 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
879 V4L2_FIELD_HAS_BOTH(dev->ovfield));
880 setup_clipping(dev,fh->clips,fh->nclips,
881 V4L2_FIELD_HAS_BOTH(dev->ovfield));
882 if (dev->ovfmt->yuv)
883 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
884 else
885 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
886 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
887
888 /* dma: setup channel 1 (= Video Task B) */
889 base = (unsigned long)dev->ovbuf.base;
890 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
891 base += dev->ovfmt->depth/8 * fh->win.w.left;
892 bpl = dev->ovbuf.fmt.bytesperline;
893 control = SAA7134_RS_CONTROL_BURST_16;
894 if (dev->ovfmt->bswap)
895 control |= SAA7134_RS_CONTROL_BSWAP;
896 if (dev->ovfmt->wswap)
897 control |= SAA7134_RS_CONTROL_WSWAP;
898 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
899 saa_writel(SAA7134_RS_BA1(1),base);
900 saa_writel(SAA7134_RS_BA2(1),base+bpl);
901 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
902 saa_writel(SAA7134_RS_CONTROL(1),control);
903 } else {
904 saa_writel(SAA7134_RS_BA1(1),base);
905 saa_writel(SAA7134_RS_BA2(1),base);
906 saa_writel(SAA7134_RS_PITCH(1),bpl);
907 saa_writel(SAA7134_RS_CONTROL(1),control);
908 }
909
910 /* start dma */
911 dev->ovenable = 1;
912 saa7134_set_dmabits(dev);
913
914 return 0;
915}
916
917static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
918{
919 dev->ovenable = 0;
920 saa7134_set_dmabits(dev);
921 return 0;
922}
923
924/* ------------------------------------------------------------------ */
925
926static int buffer_activate(struct saa7134_dev *dev,
927 struct saa7134_buf *buf,
928 struct saa7134_buf *next)
929{
930 unsigned long base,control,bpl;
931 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
932
933 dprintk("buffer_activate buf=%p\n",buf);
934 buf->vb.state = STATE_ACTIVE;
935 buf->top_seen = 0;
936
937 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
938 V4L2_FIELD_HAS_BOTH(buf->vb.field));
939 if (buf->fmt->yuv)
940 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
941 else
942 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
943 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
944
945 /* DMA: setup channel 0 (= Video Task A0) */
946 base = saa7134_buffer_base(buf);
947 if (buf->fmt->planar)
948 bpl = buf->vb.width;
949 else
950 bpl = (buf->vb.width * buf->fmt->depth) / 8;
951 control = SAA7134_RS_CONTROL_BURST_16 |
952 SAA7134_RS_CONTROL_ME |
953 (buf->pt->dma >> 12);
954 if (buf->fmt->bswap)
955 control |= SAA7134_RS_CONTROL_BSWAP;
956 if (buf->fmt->wswap)
957 control |= SAA7134_RS_CONTROL_WSWAP;
958 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
959 /* interlaced */
960 saa_writel(SAA7134_RS_BA1(0),base);
961 saa_writel(SAA7134_RS_BA2(0),base+bpl);
962 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
963 } else {
964 /* non-interlaced */
965 saa_writel(SAA7134_RS_BA1(0),base);
966 saa_writel(SAA7134_RS_BA2(0),base);
967 saa_writel(SAA7134_RS_PITCH(0),bpl);
968 }
969 saa_writel(SAA7134_RS_CONTROL(0),control);
970
971 if (buf->fmt->planar) {
972 /* DMA: setup channel 4+5 (= planar task A) */
973 bpl_uv = bpl >> buf->fmt->hshift;
974 lines_uv = buf->vb.height >> buf->fmt->vshift;
975 base2 = base + bpl * buf->vb.height;
976 base3 = base2 + bpl_uv * lines_uv;
977 if (buf->fmt->uvswap)
978 tmp = base2, base2 = base3, base3 = tmp;
979 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
980 bpl_uv,lines_uv,base2,base3);
981 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
982 /* interlaced */
983 saa_writel(SAA7134_RS_BA1(4),base2);
984 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
985 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
986 saa_writel(SAA7134_RS_BA1(5),base3);
987 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
988 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
989 } else {
990 /* non-interlaced */
991 saa_writel(SAA7134_RS_BA1(4),base2);
992 saa_writel(SAA7134_RS_BA2(4),base2);
993 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
994 saa_writel(SAA7134_RS_BA1(5),base3);
995 saa_writel(SAA7134_RS_BA2(5),base3);
996 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
997 }
998 saa_writel(SAA7134_RS_CONTROL(4),control);
999 saa_writel(SAA7134_RS_CONTROL(5),control);
1000 }
1001
1002 /* start DMA */
1003 saa7134_set_dmabits(dev);
1004 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1005 return 0;
1006}
1007
1008static int buffer_prepare(struct videobuf_queue *q,
1009 struct videobuf_buffer *vb,
1010 enum v4l2_field field)
1011{
1012 struct saa7134_fh *fh = q->priv_data;
1013 struct saa7134_dev *dev = fh->dev;
1014 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1015 unsigned int size;
1016 int err;
1017
1018 /* sanity checks */
1019 if (NULL == fh->fmt)
1020 return -EINVAL;
1021 if (fh->width < 48 ||
1022 fh->height < 32 ||
1023 fh->width/4 > dev->crop_current.width ||
1024 fh->height/4 > dev->crop_current.height ||
1025 fh->width > dev->crop_bounds.width ||
1026 fh->height > dev->crop_bounds.height)
1027 return -EINVAL;
1028 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1029 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1030 return -EINVAL;
1031
1032 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1033 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1034 fh->fmt->name);
1035 if (buf->vb.width != fh->width ||
1036 buf->vb.height != fh->height ||
1037 buf->vb.size != size ||
1038 buf->vb.field != field ||
1039 buf->fmt != fh->fmt) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001040 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
1043 if (STATE_NEEDS_INIT == buf->vb.state) {
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001044 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 buf->vb.width = fh->width;
1047 buf->vb.height = fh->height;
1048 buf->vb.size = size;
1049 buf->vb.field = field;
1050 buf->fmt = fh->fmt;
1051 buf->pt = &fh->pt_cap;
1052
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001053 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (err)
1055 goto oops;
1056 err = saa7134_pgtable_build(dev->pci,buf->pt,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001057 dma->sglist,
1058 dma->sglen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 saa7134_buffer_startpage(buf));
1060 if (err)
1061 goto oops;
1062 }
1063 buf->vb.state = STATE_PREPARED;
1064 buf->activate = buffer_activate;
1065 return 0;
1066
1067 oops:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001068 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return err;
1070}
1071
1072static int
1073buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1074{
1075 struct saa7134_fh *fh = q->priv_data;
1076
1077 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1078 if (0 == *count)
1079 *count = gbuffers;
1080 *count = saa7134_buffer_count(*size,*count);
1081 return 0;
1082}
1083
1084static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1085{
1086 struct saa7134_fh *fh = q->priv_data;
1087 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1088
1089 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1090}
1091
1092static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1093{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1095
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001096 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
1099static struct videobuf_queue_ops video_qops = {
1100 .buf_setup = buffer_setup,
1101 .buf_prepare = buffer_prepare,
1102 .buf_queue = buffer_queue,
1103 .buf_release = buffer_release,
1104};
1105
1106/* ------------------------------------------------------------------ */
1107
1108static int get_control(struct saa7134_dev *dev, struct v4l2_control *c)
1109{
1110 const struct v4l2_queryctrl* ctrl;
1111
1112 ctrl = ctrl_by_id(c->id);
1113 if (NULL == ctrl)
1114 return -EINVAL;
1115 switch (c->id) {
1116 case V4L2_CID_BRIGHTNESS:
1117 c->value = dev->ctl_bright;
1118 break;
1119 case V4L2_CID_HUE:
1120 c->value = dev->ctl_hue;
1121 break;
1122 case V4L2_CID_CONTRAST:
1123 c->value = dev->ctl_contrast;
1124 break;
1125 case V4L2_CID_SATURATION:
1126 c->value = dev->ctl_saturation;
1127 break;
1128 case V4L2_CID_AUDIO_MUTE:
1129 c->value = dev->ctl_mute;
1130 break;
1131 case V4L2_CID_AUDIO_VOLUME:
1132 c->value = dev->ctl_volume;
1133 break;
1134 case V4L2_CID_PRIVATE_INVERT:
1135 c->value = dev->ctl_invert;
1136 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001137 case V4L2_CID_HFLIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 c->value = dev->ctl_mirror;
1139 break;
1140 case V4L2_CID_PRIVATE_Y_EVEN:
1141 c->value = dev->ctl_y_even;
1142 break;
1143 case V4L2_CID_PRIVATE_Y_ODD:
1144 c->value = dev->ctl_y_odd;
1145 break;
1146 case V4L2_CID_PRIVATE_AUTOMUTE:
1147 c->value = dev->ctl_automute;
1148 break;
1149 default:
1150 return -EINVAL;
1151 }
1152 return 0;
1153}
1154
1155static int set_control(struct saa7134_dev *dev, struct saa7134_fh *fh,
1156 struct v4l2_control *c)
1157{
1158 const struct v4l2_queryctrl* ctrl;
1159 unsigned long flags;
1160 int restart_overlay = 0;
1161
1162 ctrl = ctrl_by_id(c->id);
1163 if (NULL == ctrl)
1164 return -EINVAL;
1165 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1166 switch (ctrl->type) {
1167 case V4L2_CTRL_TYPE_BOOLEAN:
1168 case V4L2_CTRL_TYPE_MENU:
1169 case V4L2_CTRL_TYPE_INTEGER:
1170 if (c->value < ctrl->minimum)
1171 c->value = ctrl->minimum;
1172 if (c->value > ctrl->maximum)
1173 c->value = ctrl->maximum;
1174 break;
1175 default:
1176 /* nothing */;
1177 };
1178 switch (c->id) {
1179 case V4L2_CID_BRIGHTNESS:
1180 dev->ctl_bright = c->value;
1181 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1182 break;
1183 case V4L2_CID_HUE:
1184 dev->ctl_hue = c->value;
1185 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1186 break;
1187 case V4L2_CID_CONTRAST:
1188 dev->ctl_contrast = c->value;
1189 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1190 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1191 break;
1192 case V4L2_CID_SATURATION:
1193 dev->ctl_saturation = c->value;
1194 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1195 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1196 break;
1197 case V4L2_CID_AUDIO_MUTE:
1198 dev->ctl_mute = c->value;
1199 saa7134_tvaudio_setmute(dev);
1200 break;
1201 case V4L2_CID_AUDIO_VOLUME:
1202 dev->ctl_volume = c->value;
1203 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1204 break;
1205 case V4L2_CID_PRIVATE_INVERT:
1206 dev->ctl_invert = c->value;
1207 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1208 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1209 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1210 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1211 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001212 case V4L2_CID_HFLIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 dev->ctl_mirror = c->value;
1214 restart_overlay = 1;
1215 break;
1216 case V4L2_CID_PRIVATE_Y_EVEN:
1217 dev->ctl_y_even = c->value;
1218 restart_overlay = 1;
1219 break;
1220 case V4L2_CID_PRIVATE_Y_ODD:
1221 dev->ctl_y_odd = c->value;
1222 restart_overlay = 1;
1223 break;
1224 case V4L2_CID_PRIVATE_AUTOMUTE:
1225 dev->ctl_automute = c->value;
1226 if (dev->tda9887_conf) {
1227 if (dev->ctl_automute)
1228 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1229 else
1230 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1231 saa7134_i2c_call_clients(dev, TDA9887_SET_CONFIG,
1232 &dev->tda9887_conf);
1233 }
1234 break;
1235 default:
1236 return -EINVAL;
1237 }
1238 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1239 spin_lock_irqsave(&dev->slock,flags);
1240 stop_preview(dev,fh);
1241 start_preview(dev,fh);
1242 spin_unlock_irqrestore(&dev->slock,flags);
1243 }
1244 return 0;
1245}
1246
1247/* ------------------------------------------------------------------ */
1248
1249static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1250{
1251 struct videobuf_queue* q = NULL;
1252
1253 switch (fh->type) {
1254 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1255 q = &fh->cap;
1256 break;
1257 case V4L2_BUF_TYPE_VBI_CAPTURE:
1258 q = &fh->vbi;
1259 break;
1260 default:
1261 BUG();
1262 }
1263 return q;
1264}
1265
1266static int saa7134_resource(struct saa7134_fh *fh)
1267{
Heikki Orsila9040b322007-04-27 12:31:18 -03001268 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1269 return RESOURCE_VIDEO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Heikki Orsila9040b322007-04-27 12:31:18 -03001271 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1272 return RESOURCE_VBI;
1273
1274 BUG();
1275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
1278static int video_open(struct inode *inode, struct file *file)
1279{
1280 int minor = iminor(inode);
1281 struct saa7134_dev *h,*dev = NULL;
1282 struct saa7134_fh *fh;
1283 struct list_head *list;
1284 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1285 int radio = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 list_for_each(list,&saa7134_devlist) {
1287 h = list_entry(list, struct saa7134_dev, devlist);
1288 if (h->video_dev && (h->video_dev->minor == minor))
1289 dev = h;
1290 if (h->radio_dev && (h->radio_dev->minor == minor)) {
1291 radio = 1;
1292 dev = h;
1293 }
1294 if (h->vbi_dev && (h->vbi_dev->minor == minor)) {
1295 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1296 dev = h;
1297 }
1298 }
1299 if (NULL == dev)
1300 return -ENODEV;
1301
1302 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1303 v4l2_type_names[type]);
1304
1305 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001306 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (NULL == fh)
1308 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 file->private_data = fh;
1310 fh->dev = dev;
1311 fh->radio = radio;
1312 fh->type = type;
1313 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1314 fh->width = 720;
1315 fh->height = 576;
1316 v4l2_prio_open(&dev->prio,&fh->prio);
1317
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001318 videobuf_queue_pci_init(&fh->cap, &video_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 dev->pci, &dev->slock,
1320 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1321 V4L2_FIELD_INTERLACED,
1322 sizeof(struct saa7134_buf),
1323 fh);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001324 videobuf_queue_pci_init(&fh->vbi, &saa7134_vbi_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 dev->pci, &dev->slock,
1326 V4L2_BUF_TYPE_VBI_CAPTURE,
1327 V4L2_FIELD_SEQ_TB,
1328 sizeof(struct saa7134_buf),
1329 fh);
1330 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1331 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1332
1333 if (fh->radio) {
1334 /* switch to radio mode */
1335 saa7134_tvaudio_setinput(dev,&card(dev).radio);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001336 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 } else {
1338 /* switch to video/vbi mode */
1339 video_mux(dev,dev->ctl_input);
1340 }
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
1344static ssize_t
1345video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1346{
1347 struct saa7134_fh *fh = file->private_data;
1348
1349 switch (fh->type) {
1350 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1351 if (res_locked(fh->dev,RESOURCE_VIDEO))
1352 return -EBUSY;
1353 return videobuf_read_one(saa7134_queue(fh),
1354 data, count, ppos,
1355 file->f_flags & O_NONBLOCK);
1356 case V4L2_BUF_TYPE_VBI_CAPTURE:
1357 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1358 return -EBUSY;
1359 return videobuf_read_stream(saa7134_queue(fh),
1360 data, count, ppos, 1,
1361 file->f_flags & O_NONBLOCK);
1362 break;
1363 default:
1364 BUG();
1365 return 0;
1366 }
1367}
1368
1369static unsigned int
1370video_poll(struct file *file, struct poll_table_struct *wait)
1371{
1372 struct saa7134_fh *fh = file->private_data;
1373 struct videobuf_buffer *buf = NULL;
1374
1375 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1376 return videobuf_poll_stream(file, &fh->vbi, wait);
1377
1378 if (res_check(fh,RESOURCE_VIDEO)) {
1379 if (!list_empty(&fh->cap.stream))
1380 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1381 } else {
Ingo Molnar3593cab2006-02-07 06:49:14 -02001382 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (UNSET == fh->cap.read_off) {
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001384 /* need to capture a new frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (res_locked(fh->dev,RESOURCE_VIDEO)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02001386 mutex_unlock(&fh->cap.lock);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001387 return POLLERR;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001388 }
1389 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02001390 mutex_unlock(&fh->cap.lock);
Mauro Carvalho Chehabf2421ca2005-11-08 21:37:45 -08001391 return POLLERR;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001392 }
1393 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1394 fh->cap.read_off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02001396 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 buf = fh->cap.read_buf;
1398 }
1399
1400 if (!buf)
1401 return POLLERR;
1402
1403 poll_wait(file, &buf->done, wait);
1404 if (buf->state == STATE_DONE ||
1405 buf->state == STATE_ERROR)
1406 return POLLIN|POLLRDNORM;
1407 return 0;
1408}
1409
1410static int video_release(struct inode *inode, struct file *file)
1411{
1412 struct saa7134_fh *fh = file->private_data;
1413 struct saa7134_dev *dev = fh->dev;
1414 unsigned long flags;
1415
1416 /* turn off overlay */
1417 if (res_check(fh, RESOURCE_OVERLAY)) {
1418 spin_lock_irqsave(&dev->slock,flags);
1419 stop_preview(dev,fh);
1420 spin_unlock_irqrestore(&dev->slock,flags);
1421 res_free(dev,fh,RESOURCE_OVERLAY);
1422 }
1423
1424 /* stop video capture */
1425 if (res_check(fh, RESOURCE_VIDEO)) {
1426 videobuf_streamoff(&fh->cap);
1427 res_free(dev,fh,RESOURCE_VIDEO);
1428 }
1429 if (fh->cap.read_buf) {
1430 buffer_release(&fh->cap,fh->cap.read_buf);
1431 kfree(fh->cap.read_buf);
1432 }
1433
1434 /* stop vbi capture */
1435 if (res_check(fh, RESOURCE_VBI)) {
1436 if (fh->vbi.streaming)
1437 videobuf_streamoff(&fh->vbi);
1438 if (fh->vbi.reading)
1439 videobuf_read_stop(&fh->vbi);
1440 res_free(dev,fh,RESOURCE_VBI);
1441 }
1442
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001443 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1444 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1445 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1446 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1447 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1448
Mauro Carvalho Chehab2f180712005-09-09 13:03:46 -07001449 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /* free stuff */
1452 videobuf_mmap_free(&fh->cap);
1453 videobuf_mmap_free(&fh->vbi);
1454 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1455 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1456
1457 v4l2_prio_close(&dev->prio,&fh->prio);
1458 file->private_data = NULL;
1459 kfree(fh);
1460 return 0;
1461}
1462
Heikki Orsila9040b322007-04-27 12:31:18 -03001463static int video_mmap(struct file *file, struct vm_area_struct * vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 struct saa7134_fh *fh = file->private_data;
1466
1467 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1468}
1469
1470/* ------------------------------------------------------------------ */
1471
1472static void saa7134_vbi_fmt(struct saa7134_dev *dev, struct v4l2_format *f)
1473{
1474 struct saa7134_tvnorm *norm = dev->tvnorm;
1475
1476 f->fmt.vbi.sampling_rate = 6750000 * 4;
1477 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1478 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1479 f->fmt.vbi.offset = 64 * 4;
Michael Schimekf246a812005-06-23 22:04:47 -07001480 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1481 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1482 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1484 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488static int saa7134_g_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1489 struct v4l2_format *f)
1490{
1491 switch (f->type) {
1492 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1493 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1494 f->fmt.pix.width = fh->width;
1495 f->fmt.pix.height = fh->height;
1496 f->fmt.pix.field = fh->cap.field;
1497 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1498 f->fmt.pix.bytesperline =
1499 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1500 f->fmt.pix.sizeimage =
1501 f->fmt.pix.height * f->fmt.pix.bytesperline;
1502 return 0;
1503 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehabecb73772006-03-17 20:54:32 -03001504 if (saa7134_no_overlay > 0) {
1505 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1506 return -EINVAL;
1507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 f->fmt.win = fh->win;
1509 return 0;
1510 case V4L2_BUF_TYPE_VBI_CAPTURE:
1511 saa7134_vbi_fmt(dev,f);
1512 return 0;
1513 default:
1514 return -EINVAL;
1515 }
1516}
1517
1518static int saa7134_try_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1519 struct v4l2_format *f)
1520{
1521 int err;
1522
1523 switch (f->type) {
1524 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1525 {
1526 struct saa7134_format *fmt;
1527 enum v4l2_field field;
1528 unsigned int maxw, maxh;
1529
1530 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1531 if (NULL == fmt)
1532 return -EINVAL;
1533
1534 field = f->fmt.pix.field;
1535 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1536 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1537
1538 if (V4L2_FIELD_ANY == field) {
1539 field = (f->fmt.pix.height > maxh/2)
1540 ? V4L2_FIELD_INTERLACED
1541 : V4L2_FIELD_BOTTOM;
1542 }
1543 switch (field) {
1544 case V4L2_FIELD_TOP:
1545 case V4L2_FIELD_BOTTOM:
1546 maxh = maxh / 2;
1547 break;
1548 case V4L2_FIELD_INTERLACED:
1549 break;
1550 default:
1551 return -EINVAL;
1552 }
1553
1554 f->fmt.pix.field = field;
1555 if (f->fmt.pix.width < 48)
1556 f->fmt.pix.width = 48;
1557 if (f->fmt.pix.height < 32)
1558 f->fmt.pix.height = 32;
1559 if (f->fmt.pix.width > maxw)
1560 f->fmt.pix.width = maxw;
1561 if (f->fmt.pix.height > maxh)
1562 f->fmt.pix.height = maxh;
1563 f->fmt.pix.width &= ~0x03;
1564 f->fmt.pix.bytesperline =
1565 (f->fmt.pix.width * fmt->depth) >> 3;
1566 f->fmt.pix.sizeimage =
1567 f->fmt.pix.height * f->fmt.pix.bytesperline;
1568
1569 return 0;
1570 }
1571 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehabecb73772006-03-17 20:54:32 -03001572 if (saa7134_no_overlay > 0) {
1573 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1574 return -EINVAL;
1575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 err = verify_preview(dev,&f->fmt.win);
1577 if (0 != err)
1578 return err;
1579 return 0;
1580 case V4L2_BUF_TYPE_VBI_CAPTURE:
1581 saa7134_vbi_fmt(dev,f);
1582 return 0;
1583 default:
1584 return -EINVAL;
1585 }
1586}
1587
1588static int saa7134_s_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1589 struct v4l2_format *f)
1590{
1591 unsigned long flags;
1592 int err;
1593
1594 switch (f->type) {
1595 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1596 err = saa7134_try_fmt(dev,fh,f);
1597 if (0 != err)
1598 return err;
1599
1600 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1601 fh->width = f->fmt.pix.width;
1602 fh->height = f->fmt.pix.height;
1603 fh->cap.field = f->fmt.pix.field;
1604 return 0;
1605 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehabecb73772006-03-17 20:54:32 -03001606 if (saa7134_no_overlay > 0) {
1607 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1608 return -EINVAL;
1609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 err = verify_preview(dev,&f->fmt.win);
1611 if (0 != err)
1612 return err;
1613
Ingo Molnar3593cab2006-02-07 06:49:14 -02001614 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 fh->win = f->fmt.win;
1616 fh->nclips = f->fmt.win.clipcount;
1617 if (fh->nclips > 8)
1618 fh->nclips = 8;
1619 if (copy_from_user(fh->clips,f->fmt.win.clips,
1620 sizeof(struct v4l2_clip)*fh->nclips)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02001621 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return -EFAULT;
1623 }
1624
1625 if (res_check(fh, RESOURCE_OVERLAY)) {
1626 spin_lock_irqsave(&dev->slock,flags);
1627 stop_preview(dev,fh);
1628 start_preview(dev,fh);
1629 spin_unlock_irqrestore(&dev->slock,flags);
1630 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02001631 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return 0;
1633 case V4L2_BUF_TYPE_VBI_CAPTURE:
1634 saa7134_vbi_fmt(dev,f);
1635 return 0;
1636 default:
1637 return -EINVAL;
1638 }
1639}
1640
1641int saa7134_common_ioctl(struct saa7134_dev *dev,
1642 unsigned int cmd, void *arg)
1643{
1644 int err;
1645
1646 switch (cmd) {
1647 case VIDIOC_QUERYCTRL:
1648 {
1649 const struct v4l2_queryctrl *ctrl;
1650 struct v4l2_queryctrl *c = arg;
1651
1652 if ((c->id < V4L2_CID_BASE ||
1653 c->id >= V4L2_CID_LASTP1) &&
1654 (c->id < V4L2_CID_PRIVATE_BASE ||
1655 c->id >= V4L2_CID_PRIVATE_LASTP1))
1656 return -EINVAL;
1657 ctrl = ctrl_by_id(c->id);
1658 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1659 return 0;
1660 }
1661 case VIDIOC_G_CTRL:
1662 return get_control(dev,arg);
1663 case VIDIOC_S_CTRL:
1664 {
Ingo Molnar3593cab2006-02-07 06:49:14 -02001665 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 err = set_control(dev,NULL,arg);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001667 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return err;
1669 }
1670 /* --- input switching --------------------------------------- */
1671 case VIDIOC_ENUMINPUT:
1672 {
1673 struct v4l2_input *i = arg;
1674 unsigned int n;
1675
1676 n = i->index;
1677 if (n >= SAA7134_INPUT_MAX)
1678 return -EINVAL;
1679 if (NULL == card_in(dev,i->index).name)
1680 return -EINVAL;
1681 memset(i,0,sizeof(*i));
1682 i->index = n;
1683 i->type = V4L2_INPUT_TYPE_CAMERA;
1684 strcpy(i->name,card_in(dev,n).name);
1685 if (card_in(dev,n).tv)
1686 i->type = V4L2_INPUT_TYPE_TUNER;
1687 i->audioset = 1;
1688 if (n == dev->ctl_input) {
1689 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1690 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1691
1692 if (0 != (v1 & 0x40))
1693 i->status |= V4L2_IN_ST_NO_H_LOCK;
1694 if (0 != (v2 & 0x40))
1695 i->status |= V4L2_IN_ST_NO_SYNC;
1696 if (0 != (v2 & 0x0e))
1697 i->status |= V4L2_IN_ST_MACROVISION;
1698 }
1699 for (n = 0; n < TVNORMS; n++)
1700 i->std |= tvnorms[n].id;
1701 return 0;
1702 }
1703 case VIDIOC_G_INPUT:
1704 {
1705 int *i = arg;
1706 *i = dev->ctl_input;
1707 return 0;
1708 }
1709 case VIDIOC_S_INPUT:
1710 {
1711 int *i = arg;
1712
1713 if (*i < 0 || *i >= SAA7134_INPUT_MAX)
1714 return -EINVAL;
1715 if (NULL == card_in(dev,*i).name)
1716 return -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001717 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 video_mux(dev,*i);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001719 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return 0;
1721 }
1722
1723 }
1724 return 0;
1725}
1726EXPORT_SYMBOL(saa7134_common_ioctl);
1727
1728/*
1729 * This function is _not_ called directly, but from
1730 * video_generic_ioctl (and maybe others). userspace
1731 * copying is done already, arg is a kernel pointer.
1732 */
1733static int video_do_ioctl(struct inode *inode, struct file *file,
1734 unsigned int cmd, void *arg)
1735{
1736 struct saa7134_fh *fh = file->private_data;
1737 struct saa7134_dev *dev = fh->dev;
1738 unsigned long flags;
1739 int err;
1740
1741 if (video_debug > 1)
Michael Krufky5e453dc2006-01-09 15:32:31 -02001742 v4l_print_ioctl(dev->name,cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 switch (cmd) {
1745 case VIDIOC_S_CTRL:
1746 case VIDIOC_S_STD:
1747 case VIDIOC_S_INPUT:
1748 case VIDIOC_S_TUNER:
1749 case VIDIOC_S_FREQUENCY:
1750 err = v4l2_prio_check(&dev->prio,&fh->prio);
1751 if (0 != err)
1752 return err;
1753 }
1754
1755 switch (cmd) {
1756 case VIDIOC_QUERYCAP:
1757 {
1758 struct v4l2_capability *cap = arg;
Nickolay V. Shmyrevbb881f12005-11-08 21:36:35 -08001759 unsigned int tuner_type = dev->tuner_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 memset(cap,0,sizeof(*cap));
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001762 strcpy(cap->driver, "saa7134");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 strlcpy(cap->card, saa7134_boards[dev->board].name,
1764 sizeof(cap->card));
1765 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1766 cap->version = SAA7134_VERSION_CODE;
1767 cap->capabilities =
1768 V4L2_CAP_VIDEO_CAPTURE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 V4L2_CAP_VBI_CAPTURE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 V4L2_CAP_READWRITE |
Nickolay V. Shmyrevbb881f12005-11-08 21:36:35 -08001771 V4L2_CAP_STREAMING |
1772 V4L2_CAP_TUNER;
Mauro Carvalho Chehabecb73772006-03-17 20:54:32 -03001773 if (saa7134_no_overlay <= 0) {
1774 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1775 }
Nickolay V. Shmyrevbb881f12005-11-08 21:36:35 -08001776
1777 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1778 cap->capabilities &= ~V4L2_CAP_TUNER;
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return 0;
1781 }
1782
1783 /* --- tv standards ------------------------------------------ */
1784 case VIDIOC_ENUMSTD:
1785 {
1786 struct v4l2_standard *e = arg;
1787 unsigned int i;
1788
1789 i = e->index;
1790 if (i >= TVNORMS)
1791 return -EINVAL;
1792 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1793 tvnorms[e->index].name);
1794 e->index = i;
1795 if (err < 0)
1796 return err;
1797 return 0;
1798 }
1799 case VIDIOC_G_STD:
1800 {
1801 v4l2_std_id *id = arg;
1802
1803 *id = dev->tvnorm->id;
1804 return 0;
1805 }
1806 case VIDIOC_S_STD:
1807 {
1808 v4l2_std_id *id = arg;
1809 unsigned int i;
Hartmut Hackmann17b10a72006-10-02 19:55:07 -03001810 v4l2_std_id fixup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 for (i = 0; i < TVNORMS; i++)
1813 if (*id == tvnorms[i].id)
1814 break;
1815 if (i == TVNORMS)
1816 for (i = 0; i < TVNORMS; i++)
1817 if (*id & tvnorms[i].id)
1818 break;
1819 if (i == TVNORMS)
1820 return -EINVAL;
Hartmut Hackmann94480252006-10-02 21:00:32 -03001821 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1822 if (secam[0] == 'L' || secam[0] == 'l') {
Hartmut Hackmann17b10a72006-10-02 19:55:07 -03001823 if (secam[1] == 'C' || secam[1] == 'c')
1824 fixup = V4L2_STD_SECAM_LC;
1825 else
1826 fixup = V4L2_STD_SECAM_L;
Hartmut Hackmann94480252006-10-02 21:00:32 -03001827 } else {
Hartmut Hackmann17b10a72006-10-02 19:55:07 -03001828 if (secam[0] == 'D' || secam[0] == 'd')
1829 fixup = V4L2_STD_SECAM_DK;
Hartmut Hackmann94480252006-10-02 21:00:32 -03001830 else
1831 fixup = V4L2_STD_SECAM;
1832 }
Hartmut Hackmann17b10a72006-10-02 19:55:07 -03001833 for (i = 0; i < TVNORMS; i++)
1834 if (fixup == tvnorms[i].id)
1835 break;
1836 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02001837 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (res_check(fh, RESOURCE_OVERLAY)) {
1839 spin_lock_irqsave(&dev->slock,flags);
1840 stop_preview(dev,fh);
Maxim Levitskyb4aeb8b2007-09-27 20:34:15 -03001841 spin_unlock_irqrestore(&dev->slock, flags);
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 set_tvnorm(dev,&tvnorms[i]);
Maxim Levitskyb4aeb8b2007-09-27 20:34:15 -03001844
1845 spin_lock_irqsave(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 start_preview(dev,fh);
1847 spin_unlock_irqrestore(&dev->slock,flags);
1848 } else
1849 set_tvnorm(dev,&tvnorms[i]);
1850 saa7134_tvaudio_do_scan(dev);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001851 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return 0;
1853 }
1854
1855 case VIDIOC_CROPCAP:
1856 {
1857 struct v4l2_cropcap *cap = arg;
1858
1859 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1860 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1861 return -EINVAL;
1862 cap->bounds = dev->crop_bounds;
1863 cap->defrect = dev->crop_defrect;
1864 cap->pixelaspect.numerator = 1;
1865 cap->pixelaspect.denominator = 1;
1866 if (dev->tvnorm->id & V4L2_STD_525_60) {
1867 cap->pixelaspect.numerator = 11;
1868 cap->pixelaspect.denominator = 10;
1869 }
1870 if (dev->tvnorm->id & V4L2_STD_625_50) {
1871 cap->pixelaspect.numerator = 54;
1872 cap->pixelaspect.denominator = 59;
1873 }
1874 return 0;
1875 }
1876
1877 case VIDIOC_G_CROP:
1878 {
1879 struct v4l2_crop * crop = arg;
1880
1881 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1882 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1883 return -EINVAL;
1884 crop->c = dev->crop_current;
1885 return 0;
1886 }
1887 case VIDIOC_S_CROP:
1888 {
1889 struct v4l2_crop *crop = arg;
1890 struct v4l2_rect *b = &dev->crop_bounds;
1891
1892 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1893 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1894 return -EINVAL;
1895 if (crop->c.height < 0)
1896 return -EINVAL;
1897 if (crop->c.width < 0)
1898 return -EINVAL;
1899
1900 if (res_locked(fh->dev,RESOURCE_OVERLAY))
1901 return -EBUSY;
1902 if (res_locked(fh->dev,RESOURCE_VIDEO))
1903 return -EBUSY;
1904
1905 if (crop->c.top < b->top)
1906 crop->c.top = b->top;
1907 if (crop->c.top > b->top + b->height)
1908 crop->c.top = b->top + b->height;
1909 if (crop->c.height > b->top - crop->c.top + b->height)
1910 crop->c.height = b->top - crop->c.top + b->height;
1911
1912 if (crop->c.left < b->left)
Nickolay V. Shmyrev9f95a0b2005-11-08 21:36:37 -08001913 crop->c.left = b->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (crop->c.left > b->left + b->width)
Nickolay V. Shmyrev9f95a0b2005-11-08 21:36:37 -08001915 crop->c.left = b->left + b->width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (crop->c.width > b->left - crop->c.left + b->width)
1917 crop->c.width = b->left - crop->c.left + b->width;
1918
1919 dev->crop_current = crop->c;
1920 return 0;
1921 }
1922
1923 /* --- tuner ioctls ------------------------------------------ */
1924 case VIDIOC_G_TUNER:
1925 {
1926 struct v4l2_tuner *t = arg;
1927 int n;
1928
1929 if (0 != t->index)
1930 return -EINVAL;
1931 memset(t,0,sizeof(*t));
1932 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1933 if (card_in(dev,n).tv)
1934 break;
1935 if (NULL != card_in(dev,n).name) {
1936 strcpy(t->name, "Television");
Elad Lahav48c42592005-11-08 21:37:59 -08001937 t->type = V4L2_TUNER_ANALOG_TV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 t->capability = V4L2_TUNER_CAP_NORM |
1939 V4L2_TUNER_CAP_STEREO |
1940 V4L2_TUNER_CAP_LANG1 |
1941 V4L2_TUNER_CAP_LANG2;
1942 t->rangehigh = 0xffffffffUL;
1943 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1944 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1945 }
1946 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1947 t->signal = 0xffff;
1948 return 0;
1949 }
1950 case VIDIOC_S_TUNER:
1951 {
1952 struct v4l2_tuner *t = arg;
1953 int rx,mode;
1954
1955 mode = dev->thread.mode;
1956 if (UNSET == mode) {
1957 rx = saa7134_tvaudio_getstereo(dev);
1958 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1959 }
1960 if (mode != t->audmode) {
1961 dev->thread.mode = t->audmode;
1962 }
1963 return 0;
1964 }
1965 case VIDIOC_G_FREQUENCY:
1966 {
1967 struct v4l2_frequency *f = arg;
1968
1969 memset(f,0,sizeof(*f));
1970 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1971 f->frequency = dev->ctl_freq;
1972 return 0;
1973 }
1974 case VIDIOC_S_FREQUENCY:
1975 {
1976 struct v4l2_frequency *f = arg;
1977
1978 if (0 != f->tuner)
1979 return -EINVAL;
1980 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1981 return -EINVAL;
1982 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1983 return -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001984 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 dev->ctl_freq = f->frequency;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -07001986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f);
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -07001988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 saa7134_tvaudio_do_scan(dev);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001990 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return 0;
1992 }
1993
1994 /* --- control ioctls ---------------------------------------- */
1995 case VIDIOC_ENUMINPUT:
1996 case VIDIOC_G_INPUT:
1997 case VIDIOC_S_INPUT:
1998 case VIDIOC_QUERYCTRL:
1999 case VIDIOC_G_CTRL:
2000 case VIDIOC_S_CTRL:
2001 return saa7134_common_ioctl(dev, cmd, arg);
2002
2003 case VIDIOC_G_AUDIO:
2004 {
2005 struct v4l2_audio *a = arg;
2006
2007 memset(a,0,sizeof(*a));
2008 strcpy(a->name,"audio");
2009 return 0;
2010 }
2011 case VIDIOC_S_AUDIO:
2012 return 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002013 case VIDIOC_G_PARM:
2014 {
2015 struct v4l2_captureparm *parm = arg;
2016 memset(parm,0,sizeof(*parm));
2017 return 0;
2018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002020 case VIDIOC_G_PRIORITY:
2021 {
2022 enum v4l2_priority *p = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002024 *p = v4l2_prio_max(&dev->prio);
2025 return 0;
2026 }
2027 case VIDIOC_S_PRIORITY:
2028 {
2029 enum v4l2_priority *prio = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002031 return v4l2_prio_change(&dev->prio, &fh->prio, *prio);
2032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 /* --- preview ioctls ---------------------------------------- */
2035 case VIDIOC_ENUM_FMT:
2036 {
2037 struct v4l2_fmtdesc *f = arg;
2038 enum v4l2_buf_type type;
2039 unsigned int index;
2040
2041 index = f->index;
2042 type = f->type;
2043 switch (type) {
2044 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2045 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehabecb73772006-03-17 20:54:32 -03002046 if (saa7134_no_overlay > 0) {
2047 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2048 return -EINVAL;
2049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (index >= FORMATS)
2051 return -EINVAL;
2052 if (f->type == V4L2_BUF_TYPE_VIDEO_OVERLAY &&
2053 formats[index].planar)
2054 return -EINVAL;
2055 memset(f,0,sizeof(*f));
2056 f->index = index;
2057 f->type = type;
2058 strlcpy(f->description,formats[index].name,sizeof(f->description));
2059 f->pixelformat = formats[index].fourcc;
2060 break;
2061 case V4L2_BUF_TYPE_VBI_CAPTURE:
2062 if (0 != index)
2063 return -EINVAL;
2064 memset(f,0,sizeof(*f));
2065 f->index = index;
2066 f->type = type;
2067 f->pixelformat = V4L2_PIX_FMT_GREY;
2068 strcpy(f->description,"vbi data");
2069 break;
2070 default:
2071 return -EINVAL;
2072 }
2073 return 0;
2074 }
2075 case VIDIOC_G_FBUF:
2076 {
2077 struct v4l2_framebuffer *fb = arg;
2078
2079 *fb = dev->ovbuf;
2080 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2081 return 0;
2082 }
2083 case VIDIOC_S_FBUF:
2084 {
2085 struct v4l2_framebuffer *fb = arg;
2086 struct saa7134_format *fmt;
2087
2088 if(!capable(CAP_SYS_ADMIN) &&
2089 !capable(CAP_SYS_RAWIO))
2090 return -EPERM;
2091
2092 /* check args */
2093 fmt = format_by_fourcc(fb->fmt.pixelformat);
2094 if (NULL == fmt)
2095 return -EINVAL;
2096
2097 /* ok, accept it */
2098 dev->ovbuf = *fb;
2099 dev->ovfmt = fmt;
2100 if (0 == dev->ovbuf.fmt.bytesperline)
2101 dev->ovbuf.fmt.bytesperline =
2102 dev->ovbuf.fmt.width*fmt->depth/8;
2103 return 0;
2104 }
2105 case VIDIOC_OVERLAY:
2106 {
2107 int *on = arg;
2108
2109 if (*on) {
Mauro Carvalho Chehabecb73772006-03-17 20:54:32 -03002110 if (saa7134_no_overlay > 0) {
2111 printk ("no_overlay\n");
2112 return -EINVAL;
2113 }
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (!res_get(dev,fh,RESOURCE_OVERLAY))
2116 return -EBUSY;
2117 spin_lock_irqsave(&dev->slock,flags);
2118 start_preview(dev,fh);
2119 spin_unlock_irqrestore(&dev->slock,flags);
2120 }
2121 if (!*on) {
2122 if (!res_check(fh, RESOURCE_OVERLAY))
2123 return -EINVAL;
2124 spin_lock_irqsave(&dev->slock,flags);
2125 stop_preview(dev,fh);
2126 spin_unlock_irqrestore(&dev->slock,flags);
2127 res_free(dev,fh,RESOURCE_OVERLAY);
2128 }
2129 return 0;
2130 }
2131
2132 /* --- capture ioctls ---------------------------------------- */
2133 case VIDIOC_G_FMT:
2134 {
2135 struct v4l2_format *f = arg;
2136 return saa7134_g_fmt(dev,fh,f);
2137 }
2138 case VIDIOC_S_FMT:
2139 {
2140 struct v4l2_format *f = arg;
2141 return saa7134_s_fmt(dev,fh,f);
2142 }
2143 case VIDIOC_TRY_FMT:
2144 {
2145 struct v4l2_format *f = arg;
2146 return saa7134_try_fmt(dev,fh,f);
2147 }
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -03002148#ifdef CONFIG_VIDEO_V4L1_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 case VIDIOCGMBUF:
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03002150 return videobuf_cgmbuf(saa7134_queue(fh), arg, gbuffers);
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -08002151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 case VIDIOC_REQBUFS:
2153 return videobuf_reqbufs(saa7134_queue(fh),arg);
2154
2155 case VIDIOC_QUERYBUF:
2156 return videobuf_querybuf(saa7134_queue(fh),arg);
2157
2158 case VIDIOC_QBUF:
2159 return videobuf_qbuf(saa7134_queue(fh),arg);
2160
2161 case VIDIOC_DQBUF:
2162 return videobuf_dqbuf(saa7134_queue(fh),arg,
2163 file->f_flags & O_NONBLOCK);
2164
2165 case VIDIOC_STREAMON:
2166 {
2167 int res = saa7134_resource(fh);
2168
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002169 if (!res_get(dev,fh,res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return -EBUSY;
2171 return videobuf_streamon(saa7134_queue(fh));
2172 }
2173 case VIDIOC_STREAMOFF:
2174 {
2175 int res = saa7134_resource(fh);
2176
2177 err = videobuf_streamoff(saa7134_queue(fh));
2178 if (err < 0)
2179 return err;
2180 res_free(dev,fh,res);
2181 return 0;
2182 }
2183
2184 default:
2185 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2186 video_do_ioctl);
2187 }
2188 return 0;
2189}
2190
2191static int video_ioctl(struct inode *inode, struct file *file,
2192 unsigned int cmd, unsigned long arg)
2193{
2194 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
2195}
2196
2197static int radio_do_ioctl(struct inode *inode, struct file *file,
2198 unsigned int cmd, void *arg)
2199{
2200 struct saa7134_fh *fh = file->private_data;
2201 struct saa7134_dev *dev = fh->dev;
2202
2203 if (video_debug > 1)
Michael Krufky5e453dc2006-01-09 15:32:31 -02002204 v4l_print_ioctl(dev->name,cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 switch (cmd) {
2206 case VIDIOC_QUERYCAP:
2207 {
2208 struct v4l2_capability *cap = arg;
2209
2210 memset(cap,0,sizeof(*cap));
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002211 strcpy(cap->driver, "saa7134");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 strlcpy(cap->card, saa7134_boards[dev->board].name,
2213 sizeof(cap->card));
2214 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
2215 cap->version = SAA7134_VERSION_CODE;
2216 cap->capabilities = V4L2_CAP_TUNER;
2217 return 0;
2218 }
2219 case VIDIOC_G_TUNER:
2220 {
2221 struct v4l2_tuner *t = arg;
2222
2223 if (0 != t->index)
2224 return -EINVAL;
2225
2226 memset(t,0,sizeof(*t));
2227 strcpy(t->name, "Radio");
Elad Lahav48c42592005-11-08 21:37:59 -08002228 t->type = V4L2_TUNER_RADIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -07002230 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
Hartmut Hackmann2e7cf3e2006-10-06 19:45:23 -03002231 if (dev->input->amux == TV) {
2232 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2233 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2234 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2235 }
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -07002236 return 0;
2237 }
2238 case VIDIOC_S_TUNER:
2239 {
2240 struct v4l2_tuner *t = arg;
2241
2242 if (0 != t->index)
2243 return -EINVAL;
2244
2245 saa7134_i2c_call_clients(dev,VIDIOC_S_TUNER,t);
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return 0;
2248 }
2249 case VIDIOC_ENUMINPUT:
2250 {
2251 struct v4l2_input *i = arg;
2252
2253 if (i->index != 0)
2254 return -EINVAL;
2255 strcpy(i->name,"Radio");
2256 i->type = V4L2_INPUT_TYPE_TUNER;
2257 return 0;
2258 }
2259 case VIDIOC_G_INPUT:
2260 {
2261 int *i = arg;
2262 *i = 0;
2263 return 0;
2264 }
2265 case VIDIOC_G_AUDIO:
2266 {
2267 struct v4l2_audio *a = arg;
2268
2269 memset(a,0,sizeof(*a));
2270 strcpy(a->name,"Radio");
2271 return 0;
2272 }
2273 case VIDIOC_G_STD:
2274 {
2275 v4l2_std_id *id = arg;
2276 *id = 0;
2277 return 0;
2278 }
2279 case VIDIOC_S_AUDIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 case VIDIOC_S_INPUT:
2281 case VIDIOC_S_STD:
2282 return 0;
2283
2284 case VIDIOC_QUERYCTRL:
2285 {
2286 const struct v4l2_queryctrl *ctrl;
2287 struct v4l2_queryctrl *c = arg;
2288
2289 if (c->id < V4L2_CID_BASE ||
2290 c->id >= V4L2_CID_LASTP1)
2291 return -EINVAL;
2292 if (c->id == V4L2_CID_AUDIO_MUTE) {
2293 ctrl = ctrl_by_id(c->id);
2294 *c = *ctrl;
2295 } else
2296 *c = no_ctrl;
2297 return 0;
2298 }
2299
2300 case VIDIOC_G_CTRL:
2301 case VIDIOC_S_CTRL:
2302 case VIDIOC_G_FREQUENCY:
2303 case VIDIOC_S_FREQUENCY:
2304 return video_do_ioctl(inode,file,cmd,arg);
2305
2306 default:
2307 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2308 radio_do_ioctl);
2309 }
2310 return 0;
2311}
2312
2313static int radio_ioctl(struct inode *inode, struct file *file,
2314 unsigned int cmd, unsigned long arg)
2315{
2316 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
2317}
2318
Arjan van de Venfa027c22007-02-12 00:55:33 -08002319static const struct file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
2321 .owner = THIS_MODULE,
2322 .open = video_open,
2323 .release = video_release,
2324 .read = video_read,
2325 .poll = video_poll,
2326 .mmap = video_mmap,
2327 .ioctl = video_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02002328 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 .llseek = no_llseek,
2330};
2331
Arjan van de Venfa027c22007-02-12 00:55:33 -08002332static const struct file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 .owner = THIS_MODULE,
2335 .open = video_open,
2336 .release = video_release,
2337 .ioctl = radio_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02002338 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 .llseek = no_llseek,
2340};
2341
2342/* ----------------------------------------------------------- */
2343/* exported stuff */
2344
2345struct video_device saa7134_video_template =
2346{
2347 .name = "saa7134-video",
Mauro Carvalho Chehabecb73772006-03-17 20:54:32 -03002348 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002349 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 .hardware = 0,
2351 .fops = &video_fops,
2352 .minor = -1,
2353};
2354
2355struct video_device saa7134_vbi_template =
2356{
2357 .name = "saa7134-vbi",
2358 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
2359 .hardware = 0,
2360 .fops = &video_fops,
2361 .minor = -1,
2362};
2363
2364struct video_device saa7134_radio_template =
2365{
2366 .name = "saa7134-radio",
2367 .type = VID_TYPE_TUNER,
2368 .hardware = 0,
2369 .fops = &radio_fops,
2370 .minor = -1,
2371};
2372
2373int saa7134_video_init1(struct saa7134_dev *dev)
2374{
2375 /* sanitycheck insmod options */
2376 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2377 gbuffers = 2;
2378 if (gbufsize < 0 || gbufsize > gbufsize_max)
2379 gbufsize = gbufsize_max;
2380 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2381
2382 /* put some sensible defaults into the data structures ... */
2383 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2384 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2385 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2386 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2387 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2388 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2389 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2390 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2391
2392 if (dev->tda9887_conf && dev->ctl_automute)
2393 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2394 dev->automute = 0;
2395
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002396 INIT_LIST_HEAD(&dev->video_q.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 init_timer(&dev->video_q.timeout);
2398 dev->video_q.timeout.function = saa7134_buffer_timeout;
2399 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2400 dev->video_q.dev = dev;
2401
2402 if (saa7134_boards[dev->board].video_out) {
2403 /* enable video output */
2404 int vo = saa7134_boards[dev->board].video_out;
Robert W. Boone2f8d4f52005-11-08 21:37:10 -08002405 int video_reg;
2406 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
Robert W. Boone2f8d4f52005-11-08 21:37:10 -08002408 video_reg = video_out[vo][1];
2409 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2410 video_reg &= ~VP_T_CODE_P_INVERTED;
2411 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2413 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2414 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
Robert W. Boone2f8d4f52005-11-08 21:37:10 -08002415 video_reg = video_out[vo][5];
2416 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2417 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2418 if (vid_port_opts & SET_CLOCK_INVERTED)
2419 video_reg |= VP_CLK_CTRL1_INVERTED;
2420 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2421 video_reg = video_out[vo][6];
2422 if (vid_port_opts & SET_VSYNC_OFF) {
2423 video_reg &= ~VP_VS_TYPE_MASK;
2424 video_reg |= VP_VS_TYPE_OFF;
2425 }
2426 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2428 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2429 }
2430
2431 return 0;
2432}
2433
2434int saa7134_video_init2(struct saa7134_dev *dev)
2435{
2436 /* init video hw */
2437 set_tvnorm(dev,&tvnorms[0]);
2438 video_mux(dev,0);
2439 saa7134_tvaudio_setmute(dev);
2440 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2441 return 0;
2442}
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444void saa7134_irq_video_intl(struct saa7134_dev *dev)
2445{
2446 static const char *st[] = {
2447 "(no signal)", "NTSC", "PAL", "SECAM" };
2448 u32 st1,st2;
2449
2450 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2451 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2452 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2453 (st1 & 0x40) ? "not locked" : "locked",
2454 (st2 & 0x40) ? "no" : "yes",
2455 st[st1 & 0x03]);
2456 dev->nosignal = (st1 & 0x40) || (st2 & 0x40);
2457
2458 if (dev->nosignal) {
2459 /* no video signal -> mute audio */
2460 if (dev->ctl_automute)
2461 dev->automute = 1;
2462 saa7134_tvaudio_setmute(dev);
2463 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2464 } else {
2465 /* wake up tvaudio audio carrier scan thread */
2466 saa7134_tvaudio_do_scan(dev);
2467 if (!noninterlaced)
2468 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2469 }
2470 if (dev->mops && dev->mops->signal_change)
2471 dev->mops->signal_change(dev);
2472}
2473
2474void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2475{
2476 enum v4l2_field field;
2477
2478 spin_lock(&dev->slock);
2479 if (dev->video_q.curr) {
2480 dev->video_fieldcount++;
2481 field = dev->video_q.curr->vb.field;
2482 if (V4L2_FIELD_HAS_BOTH(field)) {
2483 /* make sure we have seen both fields */
2484 if ((status & 0x10) == 0x00) {
2485 dev->video_q.curr->top_seen = 1;
2486 goto done;
2487 }
2488 if (!dev->video_q.curr->top_seen)
2489 goto done;
2490 } else if (field == V4L2_FIELD_TOP) {
2491 if ((status & 0x10) != 0x10)
2492 goto done;
2493 } else if (field == V4L2_FIELD_BOTTOM) {
2494 if ((status & 0x10) != 0x00)
2495 goto done;
2496 }
2497 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2498 saa7134_buffer_finish(dev,&dev->video_q,STATE_DONE);
2499 }
2500 saa7134_buffer_next(dev,&dev->video_q);
2501
2502 done:
2503 spin_unlock(&dev->slock);
2504}
2505
2506/* ----------------------------------------------------------- */
2507/*
2508 * Local variables:
2509 * c-basic-offset: 8
2510 * End:
2511 */