blob: aa7fa1f73a5694a4207d158e6577a38970c01e3c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
5 *
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
27#include <linux/slab.h>
Heikki Orsila9040b322007-04-27 12:31:18 -030028#include <linux/sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include "saa7134-reg.h"
31#include "saa7134.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020032#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030034#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080035/* Include V4L1 specific functions. Should be removed soon */
36#include <linux/videodev.h>
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030037#endif
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* ------------------------------------------------------------------ */
40
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -030041unsigned int video_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static unsigned int gbuffers = 8;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030043static unsigned int noninterlaced; /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static unsigned int gbufsize = 720*576*4;
45static unsigned int gbufsize_max = 720*576*4;
Hartmut Hackmann17b10a72006-10-02 19:55:07 -030046static char secam[] = "--";
Linus Torvalds1da177e2005-04-16 15:20:36 -070047module_param(video_debug, int, 0644);
48MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
49module_param(gbuffers, int, 0444);
50MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
51module_param(noninterlaced, int, 0644);
Hartmut Hackmannd5fdd132006-07-15 09:45:34 -030052MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
Hartmut Hackmann17b10a72006-10-02 19:55:07 -030053module_param_string(secam, secam, sizeof(secam), 0644);
54MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -030057#define dprintk(fmt, arg...) if (video_debug&0x04) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
59
60/* ------------------------------------------------------------------ */
Robert W. Boone2f8d4f52005-11-08 21:37:10 -080061/* Defines for Video Output Port Register at address 0x191 */
62
63/* Bit 0: VIP code T bit polarity */
64
65#define VP_T_CODE_P_NON_INVERTED 0x00
66#define VP_T_CODE_P_INVERTED 0x01
67
68/* ------------------------------------------------------------------ */
69/* Defines for Video Output Port Register at address 0x195 */
70
71/* Bit 2: Video output clock delay control */
72
73#define VP_CLK_CTRL2_NOT_DELAYED 0x00
74#define VP_CLK_CTRL2_DELAYED 0x04
75
76/* Bit 1: Video output clock invert control */
77
78#define VP_CLK_CTRL1_NON_INVERTED 0x00
79#define VP_CLK_CTRL1_INVERTED 0x02
80
81/* ------------------------------------------------------------------ */
82/* Defines for Video Output Port Register at address 0x196 */
83
84/* Bits 2 to 0: VSYNC pin video vertical sync type */
85
86#define VP_VS_TYPE_MASK 0x07
87
88#define VP_VS_TYPE_OFF 0x00
89#define VP_VS_TYPE_V123 0x01
90#define VP_VS_TYPE_V_ITU 0x02
91#define VP_VS_TYPE_VGATE_L 0x03
92#define VP_VS_TYPE_RESERVED1 0x04
93#define VP_VS_TYPE_RESERVED2 0x05
94#define VP_VS_TYPE_F_ITU 0x06
95#define VP_VS_TYPE_SC_FID 0x07
96
97/* ------------------------------------------------------------------ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* data structs for video */
99
100static int video_out[][9] = {
101 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
102};
103
104static struct saa7134_format formats[] = {
105 {
106 .name = "8 bpp gray",
107 .fourcc = V4L2_PIX_FMT_GREY,
108 .depth = 8,
109 .pm = 0x06,
110 },{
111 .name = "15 bpp RGB, le",
112 .fourcc = V4L2_PIX_FMT_RGB555,
113 .depth = 16,
114 .pm = 0x13 | 0x80,
115 },{
116 .name = "15 bpp RGB, be",
117 .fourcc = V4L2_PIX_FMT_RGB555X,
118 .depth = 16,
119 .pm = 0x13 | 0x80,
120 .bswap = 1,
121 },{
122 .name = "16 bpp RGB, le",
123 .fourcc = V4L2_PIX_FMT_RGB565,
124 .depth = 16,
125 .pm = 0x10 | 0x80,
126 },{
127 .name = "16 bpp RGB, be",
128 .fourcc = V4L2_PIX_FMT_RGB565X,
129 .depth = 16,
130 .pm = 0x10 | 0x80,
131 .bswap = 1,
132 },{
133 .name = "24 bpp RGB, le",
134 .fourcc = V4L2_PIX_FMT_BGR24,
135 .depth = 24,
136 .pm = 0x11,
137 },{
138 .name = "24 bpp RGB, be",
139 .fourcc = V4L2_PIX_FMT_RGB24,
140 .depth = 24,
141 .pm = 0x11,
142 .bswap = 1,
143 },{
144 .name = "32 bpp RGB, le",
145 .fourcc = V4L2_PIX_FMT_BGR32,
146 .depth = 32,
147 .pm = 0x12,
148 },{
149 .name = "32 bpp RGB, be",
150 .fourcc = V4L2_PIX_FMT_RGB32,
151 .depth = 32,
152 .pm = 0x12,
153 .bswap = 1,
154 .wswap = 1,
155 },{
156 .name = "4:2:2 packed, YUYV",
157 .fourcc = V4L2_PIX_FMT_YUYV,
158 .depth = 16,
159 .pm = 0x00,
160 .bswap = 1,
161 .yuv = 1,
162 },{
163 .name = "4:2:2 packed, UYVY",
164 .fourcc = V4L2_PIX_FMT_UYVY,
165 .depth = 16,
166 .pm = 0x00,
167 .yuv = 1,
168 },{
169 .name = "4:2:2 planar, Y-Cb-Cr",
170 .fourcc = V4L2_PIX_FMT_YUV422P,
171 .depth = 16,
172 .pm = 0x09,
173 .yuv = 1,
174 .planar = 1,
175 .hshift = 1,
176 .vshift = 0,
177 },{
178 .name = "4:2:0 planar, Y-Cb-Cr",
179 .fourcc = V4L2_PIX_FMT_YUV420,
180 .depth = 12,
181 .pm = 0x0a,
182 .yuv = 1,
183 .planar = 1,
184 .hshift = 1,
185 .vshift = 1,
186 },{
187 .name = "4:2:0 planar, Y-Cb-Cr",
188 .fourcc = V4L2_PIX_FMT_YVU420,
189 .depth = 12,
190 .pm = 0x0a,
191 .yuv = 1,
192 .planar = 1,
193 .uvswap = 1,
194 .hshift = 1,
195 .vshift = 1,
196 }
197};
198#define FORMATS ARRAY_SIZE(formats)
199
200#define NORM_625_50 \
201 .h_start = 0, \
202 .h_stop = 719, \
203 .video_v_start = 24, \
204 .video_v_stop = 311, \
Michael Schimekf246a812005-06-23 22:04:47 -0700205 .vbi_v_start_0 = 7, \
206 .vbi_v_stop_0 = 22, \
207 .vbi_v_start_1 = 319, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .src_timing = 4
209
210#define NORM_525_60 \
211 .h_start = 0, \
212 .h_stop = 703, \
Michael Schimekf246a812005-06-23 22:04:47 -0700213 .video_v_start = 23, \
214 .video_v_stop = 262, \
215 .vbi_v_start_0 = 10, \
216 .vbi_v_stop_0 = 21, \
217 .vbi_v_start_1 = 273, \
218 .src_timing = 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220static struct saa7134_tvnorm tvnorms[] = {
221 {
222 .name = "PAL", /* autodetect */
223 .id = V4L2_STD_PAL,
224 NORM_625_50,
225
226 .sync_control = 0x18,
227 .luma_control = 0x40,
228 .chroma_ctrl1 = 0x81,
229 .chroma_gain = 0x2a,
230 .chroma_ctrl2 = 0x06,
231 .vgate_misc = 0x1c,
232
233 },{
234 .name = "PAL-BG",
235 .id = V4L2_STD_PAL_BG,
236 NORM_625_50,
237
238 .sync_control = 0x18,
239 .luma_control = 0x40,
240 .chroma_ctrl1 = 0x81,
241 .chroma_gain = 0x2a,
242 .chroma_ctrl2 = 0x06,
243 .vgate_misc = 0x1c,
244
245 },{
246 .name = "PAL-I",
247 .id = V4L2_STD_PAL_I,
248 NORM_625_50,
249
250 .sync_control = 0x18,
251 .luma_control = 0x40,
252 .chroma_ctrl1 = 0x81,
253 .chroma_gain = 0x2a,
254 .chroma_ctrl2 = 0x06,
255 .vgate_misc = 0x1c,
256
257 },{
258 .name = "PAL-DK",
259 .id = V4L2_STD_PAL_DK,
260 NORM_625_50,
261
262 .sync_control = 0x18,
263 .luma_control = 0x40,
264 .chroma_ctrl1 = 0x81,
265 .chroma_gain = 0x2a,
266 .chroma_ctrl2 = 0x06,
267 .vgate_misc = 0x1c,
268
269 },{
270 .name = "NTSC",
271 .id = V4L2_STD_NTSC,
272 NORM_525_60,
273
274 .sync_control = 0x59,
275 .luma_control = 0x40,
276 .chroma_ctrl1 = 0x89,
277 .chroma_gain = 0x2a,
278 .chroma_ctrl2 = 0x0e,
279 .vgate_misc = 0x18,
280
281 },{
282 .name = "SECAM",
283 .id = V4L2_STD_SECAM,
284 NORM_625_50,
285
Hartmut Hackmann17b10a72006-10-02 19:55:07 -0300286 .sync_control = 0x18,
287 .luma_control = 0x1b,
288 .chroma_ctrl1 = 0xd1,
289 .chroma_gain = 0x80,
290 .chroma_ctrl2 = 0x00,
291 .vgate_misc = 0x1c,
292
293 },{
294 .name = "SECAM-DK",
295 .id = V4L2_STD_SECAM_DK,
296 NORM_625_50,
297
298 .sync_control = 0x18,
299 .luma_control = 0x1b,
300 .chroma_ctrl1 = 0xd1,
301 .chroma_gain = 0x80,
302 .chroma_ctrl2 = 0x00,
303 .vgate_misc = 0x1c,
304
305 },{
306 .name = "SECAM-L",
307 .id = V4L2_STD_SECAM_L,
308 NORM_625_50,
309
310 .sync_control = 0x18,
311 .luma_control = 0x1b,
312 .chroma_ctrl1 = 0xd1,
313 .chroma_gain = 0x80,
314 .chroma_ctrl2 = 0x00,
315 .vgate_misc = 0x1c,
316
317 },{
318 .name = "SECAM-Lc",
319 .id = V4L2_STD_SECAM_LC,
320 NORM_625_50,
321
322 .sync_control = 0x18,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .luma_control = 0x1b,
324 .chroma_ctrl1 = 0xd1,
325 .chroma_gain = 0x80,
326 .chroma_ctrl2 = 0x00,
327 .vgate_misc = 0x1c,
328
329 },{
330 .name = "PAL-M",
331 .id = V4L2_STD_PAL_M,
332 NORM_525_60,
333
334 .sync_control = 0x59,
335 .luma_control = 0x40,
336 .chroma_ctrl1 = 0xb9,
337 .chroma_gain = 0x2a,
338 .chroma_ctrl2 = 0x0e,
339 .vgate_misc = 0x18,
340
341 },{
342 .name = "PAL-Nc",
343 .id = V4L2_STD_PAL_Nc,
344 NORM_625_50,
345
346 .sync_control = 0x18,
347 .luma_control = 0x40,
348 .chroma_ctrl1 = 0xa1,
349 .chroma_gain = 0x2a,
350 .chroma_ctrl2 = 0x06,
351 .vgate_misc = 0x1c,
352
353 },{
354 .name = "PAL-60",
355 .id = V4L2_STD_PAL_60,
356
357 .h_start = 0,
358 .h_stop = 719,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800359 .video_v_start = 23,
360 .video_v_stop = 262,
361 .vbi_v_start_0 = 10,
362 .vbi_v_stop_0 = 21,
363 .vbi_v_start_1 = 273,
364 .src_timing = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 .sync_control = 0x18,
367 .luma_control = 0x40,
368 .chroma_ctrl1 = 0x81,
369 .chroma_gain = 0x2a,
370 .chroma_ctrl2 = 0x06,
371 .vgate_misc = 0x1c,
372 }
373};
374#define TVNORMS ARRAY_SIZE(tvnorms)
375
376#define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
377#define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
378#define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
379#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
380#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
381
382static const struct v4l2_queryctrl no_ctrl = {
383 .name = "42",
384 .flags = V4L2_CTRL_FLAG_DISABLED,
385};
386static const struct v4l2_queryctrl video_ctrls[] = {
387 /* --- video --- */
388 {
389 .id = V4L2_CID_BRIGHTNESS,
390 .name = "Brightness",
391 .minimum = 0,
392 .maximum = 255,
393 .step = 1,
394 .default_value = 128,
395 .type = V4L2_CTRL_TYPE_INTEGER,
396 },{
397 .id = V4L2_CID_CONTRAST,
398 .name = "Contrast",
399 .minimum = 0,
400 .maximum = 127,
401 .step = 1,
402 .default_value = 68,
403 .type = V4L2_CTRL_TYPE_INTEGER,
404 },{
405 .id = V4L2_CID_SATURATION,
406 .name = "Saturation",
407 .minimum = 0,
408 .maximum = 127,
409 .step = 1,
410 .default_value = 64,
411 .type = V4L2_CTRL_TYPE_INTEGER,
412 },{
413 .id = V4L2_CID_HUE,
414 .name = "Hue",
415 .minimum = -128,
416 .maximum = 127,
417 .step = 1,
418 .default_value = 0,
419 .type = V4L2_CTRL_TYPE_INTEGER,
420 },{
Michael Schimekf246a812005-06-23 22:04:47 -0700421 .id = V4L2_CID_HFLIP,
422 .name = "Mirror",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 .minimum = 0,
424 .maximum = 1,
425 .type = V4L2_CTRL_TYPE_BOOLEAN,
426 },
427 /* --- audio --- */
428 {
429 .id = V4L2_CID_AUDIO_MUTE,
430 .name = "Mute",
431 .minimum = 0,
432 .maximum = 1,
433 .type = V4L2_CTRL_TYPE_BOOLEAN,
434 },{
435 .id = V4L2_CID_AUDIO_VOLUME,
436 .name = "Volume",
437 .minimum = -15,
438 .maximum = 15,
439 .step = 1,
440 .default_value = 0,
441 .type = V4L2_CTRL_TYPE_INTEGER,
442 },
443 /* --- private --- */
444 {
445 .id = V4L2_CID_PRIVATE_INVERT,
446 .name = "Invert",
447 .minimum = 0,
448 .maximum = 1,
449 .type = V4L2_CTRL_TYPE_BOOLEAN,
450 },{
451 .id = V4L2_CID_PRIVATE_Y_ODD,
452 .name = "y offset odd field",
453 .minimum = 0,
454 .maximum = 128,
Mauro Carvalho Chehab553d3c52009-02-09 05:33:54 -0300455 .step = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 .default_value = 0,
457 .type = V4L2_CTRL_TYPE_INTEGER,
458 },{
459 .id = V4L2_CID_PRIVATE_Y_EVEN,
460 .name = "y offset even field",
461 .minimum = 0,
462 .maximum = 128,
Mauro Carvalho Chehab553d3c52009-02-09 05:33:54 -0300463 .step = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 .default_value = 0,
465 .type = V4L2_CTRL_TYPE_INTEGER,
466 },{
467 .id = V4L2_CID_PRIVATE_AUTOMUTE,
468 .name = "automute",
469 .minimum = 0,
470 .maximum = 1,
471 .default_value = 1,
472 .type = V4L2_CTRL_TYPE_BOOLEAN,
473 }
474};
475static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
476
477static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
478{
479 unsigned int i;
480
481 for (i = 0; i < CTRLS; i++)
482 if (video_ctrls[i].id == id)
483 return video_ctrls+i;
484 return NULL;
485}
486
487static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
488{
489 unsigned int i;
490
491 for (i = 0; i < FORMATS; i++)
492 if (formats[i].fourcc == fourcc)
493 return formats+i;
494 return NULL;
495}
496
497/* ----------------------------------------------------------------------- */
498/* resource management */
499
500static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
501{
502 if (fh->resources & bit)
503 /* have it already allocated */
504 return 1;
505
506 /* is it free? */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200507 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (dev->resources & bit) {
509 /* no, someone else uses it */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200510 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return 0;
512 }
513 /* it's free, grab it */
514 fh->resources |= bit;
515 dev->resources |= bit;
516 dprintk("res: get %d\n",bit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200517 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return 1;
519}
520
Heikki Orsila9040b322007-04-27 12:31:18 -0300521static int res_check(struct saa7134_fh *fh, unsigned int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 return (fh->resources & bit);
524}
525
Heikki Orsila9040b322007-04-27 12:31:18 -0300526static int res_locked(struct saa7134_dev *dev, unsigned int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 return (dev->resources & bit);
529}
530
531static
532void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
533{
Eric Sesterhennae246012006-03-13 13:17:11 -0300534 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Ingo Molnar3593cab2006-02-07 06:49:14 -0200536 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 fh->resources &= ~bits;
538 dev->resources &= ~bits;
539 dprintk("res: put %d\n",bits);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200540 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543/* ------------------------------------------------------------------ */
544
Adrian Bunkb02044d2007-10-24 13:23:14 -0300545static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 dprintk("set tv norm = %s\n",norm->name);
548 dev->tvnorm = norm;
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* setup cropping */
551 dev->crop_bounds.left = norm->h_start;
552 dev->crop_defrect.left = norm->h_start;
553 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
554 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
555
Michael Schimekf246a812005-06-23 22:04:47 -0700556 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 dev->crop_defrect.top = norm->video_v_start*2;
558 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
559 - dev->crop_bounds.top;
560 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
561
562 dev->crop_current = dev->crop_defrect;
563
Maxim Levitskyc4584732007-10-12 00:57:15 -0300564 saa7134_set_tvnorm_hw(dev);
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
Hartmut Hackmann7bff4b42008-04-22 14:46:08 -0300631 if (card_in(dev, dev->ctl_input).tv)
Hans Verkuilfac69862009-01-17 12:17:14 -0300632 saa_call_all(dev, tuner, s_std, dev->tvnorm->id);
Hans Verkuil9afb7372008-09-06 06:34:44 -0300633 /* Set the correct norm for the saa6752hs. This function
634 does nothing if there is no saa6752hs. */
Hans Verkuilfac69862009-01-17 12:17:14 -0300635 saa_call_empress(dev, tuner, s_std, dev->tvnorm->id);
Maxim Levitskyc4584732007-10-12 00:57:15 -0300636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
639{
640 static const struct {
641 int xpsc;
642 int xacl;
643 int xc2_1;
644 int xdcg;
645 int vpfy;
646 } vals[] = {
647 /* XPSC XACL XC2_1 XDCG VPFY */
648 { 1, 0, 0, 0, 0 },
649 { 2, 2, 1, 2, 2 },
650 { 3, 4, 1, 3, 2 },
651 { 4, 8, 1, 4, 2 },
652 { 5, 8, 1, 4, 2 },
653 { 6, 8, 1, 4, 3 },
654 { 7, 8, 1, 4, 3 },
655 { 8, 15, 0, 4, 3 },
656 { 9, 15, 0, 4, 3 },
657 { 10, 16, 1, 5, 3 },
658 };
659 static const int count = ARRAY_SIZE(vals);
660 int i;
661
662 for (i = 0; i < count; i++)
663 if (vals[i].xpsc == prescale)
664 break;
665 if (i == count)
666 return;
667
668 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
669 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
670 saa_writeb(SAA7134_LEVEL_CTRL(task),
671 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
672 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
673 (vals[i].vpfy << 2) | vals[i].vpfy);
674}
675
676static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
677{
678 int val,mirror;
679
680 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
681 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
682
683 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
684 if (yscale < 2048) {
685 /* LPI */
686 dprintk("yscale LPI yscale=%d\n",yscale);
687 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
688 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
689 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
690 } else {
691 /* ACM */
692 val = 0x40 * 1024 / yscale;
693 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
694 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
695 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
696 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
697 }
698 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
699}
700
701static void set_size(struct saa7134_dev *dev, int task,
702 int width, int height, int interlace)
703{
704 int prescale,xscale,yscale,y_even,y_odd;
705 int h_start, h_stop, v_start, v_stop;
706 int div = interlace ? 2 : 1;
707
708 /* setup video scaler */
709 h_start = dev->crop_current.left;
710 v_start = dev->crop_current.top/2;
711 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
712 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
713
714 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
715 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
716 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
717 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
718 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
719 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
720 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
721 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
722
723 prescale = dev->crop_current.width / width;
724 if (0 == prescale)
725 prescale = 1;
726 xscale = 1024 * dev->crop_current.width / prescale / width;
727 yscale = 512 * div * dev->crop_current.height / height;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800728 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 set_h_prescale(dev,task,prescale);
730 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
731 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
732 set_v_scale(dev,task,yscale);
733
734 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
735 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
736 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
737 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
738
739 /* deinterlace y offsets */
740 y_odd = dev->ctl_y_odd;
741 y_even = dev->ctl_y_even;
742 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
743 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
744 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
745 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
746}
747
748/* ------------------------------------------------------------------ */
749
750struct cliplist {
751 __u16 position;
752 __u8 enable;
753 __u8 disable;
754};
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756static void set_cliplist(struct saa7134_dev *dev, int reg,
757 struct cliplist *cl, int entries, char *name)
758{
759 __u8 winbits = 0;
760 int i;
761
762 for (i = 0; i < entries; i++) {
763 winbits |= cl[i].enable;
764 winbits &= ~cl[i].disable;
765 if (i < 15 && cl[i].position == cl[i+1].position)
766 continue;
767 saa_writeb(reg + 0, winbits);
768 saa_writeb(reg + 2, cl[i].position & 0xff);
769 saa_writeb(reg + 3, cl[i].position >> 8);
770 dprintk("clip: %s winbits=%02x pos=%d\n",
771 name,winbits,cl[i].position);
772 reg += 8;
773 }
774 for (; reg < 0x400; reg += 8) {
775 saa_writeb(reg+ 0, 0);
776 saa_writeb(reg + 1, 0);
777 saa_writeb(reg + 2, 0);
778 saa_writeb(reg + 3, 0);
779 }
780}
781
782static int clip_range(int val)
783{
784 if (val < 0)
785 val = 0;
786 return val;
787}
788
Heikki Orsila9040b322007-04-27 12:31:18 -0300789/* Sort into smallest position first order */
790static int cliplist_cmp(const void *a, const void *b)
791{
792 const struct cliplist *cla = a;
793 const struct cliplist *clb = b;
794 if (cla->position < clb->position)
795 return -1;
796 if (cla->position > clb->position)
797 return 1;
798 return 0;
799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
802 int nclips, int interlace)
803{
804 struct cliplist col[16], row[16];
Heikki Orsila9040b322007-04-27 12:31:18 -0300805 int cols = 0, rows = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 int div = interlace ? 2 : 1;
807
Heikki Orsila9040b322007-04-27 12:31:18 -0300808 memset(col, 0, sizeof(col));
809 memset(row, 0, sizeof(row));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 for (i = 0; i < nclips && i < 8; i++) {
811 col[cols].position = clip_range(clips[i].c.left);
812 col[cols].enable = (1 << i);
813 cols++;
814 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
815 col[cols].disable = (1 << i);
816 cols++;
817 row[rows].position = clip_range(clips[i].c.top / div);
818 row[rows].enable = (1 << i);
819 rows++;
820 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
821 / div);
822 row[rows].disable = (1 << i);
823 rows++;
824 }
Heikki Orsila9040b322007-04-27 12:31:18 -0300825 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
826 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 set_cliplist(dev,0x380,col,cols,"cols");
828 set_cliplist(dev,0x384,row,rows,"rows");
829 return 0;
830}
831
832static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
833{
834 enum v4l2_field field;
835 int maxw, maxh;
836
837 if (NULL == dev->ovbuf.base)
838 return -EINVAL;
839 if (NULL == dev->ovfmt)
840 return -EINVAL;
841 if (win->w.width < 48 || win->w.height < 32)
842 return -EINVAL;
843 if (win->clipcount > 2048)
844 return -EINVAL;
845
846 field = win->field;
847 maxw = dev->crop_current.width;
848 maxh = dev->crop_current.height;
849
850 if (V4L2_FIELD_ANY == field) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800851 field = (win->w.height > maxh/2)
852 ? V4L2_FIELD_INTERLACED
853 : V4L2_FIELD_TOP;
854 }
855 switch (field) {
856 case V4L2_FIELD_TOP:
857 case V4L2_FIELD_BOTTOM:
858 maxh = maxh / 2;
859 break;
860 case V4L2_FIELD_INTERLACED:
861 break;
862 default:
863 return -EINVAL;
864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 win->field = field;
867 if (win->w.width > maxw)
868 win->w.width = maxw;
869 if (win->w.height > maxh)
870 win->w.height = maxh;
871 return 0;
872}
873
874static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
875{
876 unsigned long base,control,bpl;
877 int err;
878
879 err = verify_preview(dev,&fh->win);
880 if (0 != err)
881 return err;
882
883 dev->ovfield = fh->win.field;
884 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
885 fh->win.w.width,fh->win.w.height,
886 fh->win.w.left,fh->win.w.top,
887 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
888
889 /* setup window + clipping */
890 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
891 V4L2_FIELD_HAS_BOTH(dev->ovfield));
892 setup_clipping(dev,fh->clips,fh->nclips,
893 V4L2_FIELD_HAS_BOTH(dev->ovfield));
894 if (dev->ovfmt->yuv)
895 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
896 else
897 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
898 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
899
900 /* dma: setup channel 1 (= Video Task B) */
901 base = (unsigned long)dev->ovbuf.base;
902 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
903 base += dev->ovfmt->depth/8 * fh->win.w.left;
904 bpl = dev->ovbuf.fmt.bytesperline;
905 control = SAA7134_RS_CONTROL_BURST_16;
906 if (dev->ovfmt->bswap)
907 control |= SAA7134_RS_CONTROL_BSWAP;
908 if (dev->ovfmt->wswap)
909 control |= SAA7134_RS_CONTROL_WSWAP;
910 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
911 saa_writel(SAA7134_RS_BA1(1),base);
912 saa_writel(SAA7134_RS_BA2(1),base+bpl);
913 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
914 saa_writel(SAA7134_RS_CONTROL(1),control);
915 } else {
916 saa_writel(SAA7134_RS_BA1(1),base);
917 saa_writel(SAA7134_RS_BA2(1),base);
918 saa_writel(SAA7134_RS_PITCH(1),bpl);
919 saa_writel(SAA7134_RS_CONTROL(1),control);
920 }
921
922 /* start dma */
923 dev->ovenable = 1;
924 saa7134_set_dmabits(dev);
925
926 return 0;
927}
928
929static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
930{
931 dev->ovenable = 0;
932 saa7134_set_dmabits(dev);
933 return 0;
934}
935
936/* ------------------------------------------------------------------ */
937
938static int buffer_activate(struct saa7134_dev *dev,
939 struct saa7134_buf *buf,
940 struct saa7134_buf *next)
941{
942 unsigned long base,control,bpl;
943 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
944
945 dprintk("buffer_activate buf=%p\n",buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300946 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 buf->top_seen = 0;
948
949 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
950 V4L2_FIELD_HAS_BOTH(buf->vb.field));
951 if (buf->fmt->yuv)
952 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
953 else
954 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
955 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
956
957 /* DMA: setup channel 0 (= Video Task A0) */
958 base = saa7134_buffer_base(buf);
959 if (buf->fmt->planar)
960 bpl = buf->vb.width;
961 else
962 bpl = (buf->vb.width * buf->fmt->depth) / 8;
963 control = SAA7134_RS_CONTROL_BURST_16 |
964 SAA7134_RS_CONTROL_ME |
965 (buf->pt->dma >> 12);
966 if (buf->fmt->bswap)
967 control |= SAA7134_RS_CONTROL_BSWAP;
968 if (buf->fmt->wswap)
969 control |= SAA7134_RS_CONTROL_WSWAP;
970 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
971 /* interlaced */
972 saa_writel(SAA7134_RS_BA1(0),base);
973 saa_writel(SAA7134_RS_BA2(0),base+bpl);
974 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
975 } else {
976 /* non-interlaced */
977 saa_writel(SAA7134_RS_BA1(0),base);
978 saa_writel(SAA7134_RS_BA2(0),base);
979 saa_writel(SAA7134_RS_PITCH(0),bpl);
980 }
981 saa_writel(SAA7134_RS_CONTROL(0),control);
982
983 if (buf->fmt->planar) {
984 /* DMA: setup channel 4+5 (= planar task A) */
985 bpl_uv = bpl >> buf->fmt->hshift;
986 lines_uv = buf->vb.height >> buf->fmt->vshift;
987 base2 = base + bpl * buf->vb.height;
988 base3 = base2 + bpl_uv * lines_uv;
989 if (buf->fmt->uvswap)
990 tmp = base2, base2 = base3, base3 = tmp;
991 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
992 bpl_uv,lines_uv,base2,base3);
993 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
994 /* interlaced */
995 saa_writel(SAA7134_RS_BA1(4),base2);
996 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
997 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
998 saa_writel(SAA7134_RS_BA1(5),base3);
999 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
1000 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
1001 } else {
1002 /* non-interlaced */
1003 saa_writel(SAA7134_RS_BA1(4),base2);
1004 saa_writel(SAA7134_RS_BA2(4),base2);
1005 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
1006 saa_writel(SAA7134_RS_BA1(5),base3);
1007 saa_writel(SAA7134_RS_BA2(5),base3);
1008 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
1009 }
1010 saa_writel(SAA7134_RS_CONTROL(4),control);
1011 saa_writel(SAA7134_RS_CONTROL(5),control);
1012 }
1013
1014 /* start DMA */
1015 saa7134_set_dmabits(dev);
1016 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1017 return 0;
1018}
1019
1020static int buffer_prepare(struct videobuf_queue *q,
1021 struct videobuf_buffer *vb,
1022 enum v4l2_field field)
1023{
1024 struct saa7134_fh *fh = q->priv_data;
1025 struct saa7134_dev *dev = fh->dev;
1026 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1027 unsigned int size;
1028 int err;
1029
1030 /* sanity checks */
1031 if (NULL == fh->fmt)
1032 return -EINVAL;
1033 if (fh->width < 48 ||
1034 fh->height < 32 ||
1035 fh->width/4 > dev->crop_current.width ||
1036 fh->height/4 > dev->crop_current.height ||
1037 fh->width > dev->crop_bounds.width ||
1038 fh->height > dev->crop_bounds.height)
1039 return -EINVAL;
1040 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1041 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1042 return -EINVAL;
1043
1044 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1045 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1046 fh->fmt->name);
1047 if (buf->vb.width != fh->width ||
1048 buf->vb.height != fh->height ||
1049 buf->vb.size != size ||
1050 buf->vb.field != field ||
1051 buf->fmt != fh->fmt) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001052 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
Brandon Philips0fc06862007-11-06 20:02:36 -03001055 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001056 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 buf->vb.width = fh->width;
1059 buf->vb.height = fh->height;
1060 buf->vb.size = size;
1061 buf->vb.field = field;
1062 buf->fmt = fh->fmt;
1063 buf->pt = &fh->pt_cap;
1064
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001065 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (err)
1067 goto oops;
1068 err = saa7134_pgtable_build(dev->pci,buf->pt,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001069 dma->sglist,
1070 dma->sglen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 saa7134_buffer_startpage(buf));
1072 if (err)
1073 goto oops;
1074 }
Brandon Philips0fc06862007-11-06 20:02:36 -03001075 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 buf->activate = buffer_activate;
1077 return 0;
1078
1079 oops:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001080 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return err;
1082}
1083
1084static int
1085buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1086{
1087 struct saa7134_fh *fh = q->priv_data;
1088
1089 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1090 if (0 == *count)
1091 *count = gbuffers;
1092 *count = saa7134_buffer_count(*size,*count);
1093 return 0;
1094}
1095
1096static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1097{
1098 struct saa7134_fh *fh = q->priv_data;
1099 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1100
1101 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1102}
1103
1104static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1105{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1107
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001108 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
1111static struct videobuf_queue_ops video_qops = {
1112 .buf_setup = buffer_setup,
1113 .buf_prepare = buffer_prepare,
1114 .buf_queue = buffer_queue,
1115 .buf_release = buffer_release,
1116};
1117
1118/* ------------------------------------------------------------------ */
1119
Hans Verkuil531d83a2008-07-26 09:04:06 -03001120int saa7134_g_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 const struct v4l2_queryctrl* ctrl;
1123
1124 ctrl = ctrl_by_id(c->id);
1125 if (NULL == ctrl)
1126 return -EINVAL;
1127 switch (c->id) {
1128 case V4L2_CID_BRIGHTNESS:
1129 c->value = dev->ctl_bright;
1130 break;
1131 case V4L2_CID_HUE:
1132 c->value = dev->ctl_hue;
1133 break;
1134 case V4L2_CID_CONTRAST:
1135 c->value = dev->ctl_contrast;
1136 break;
1137 case V4L2_CID_SATURATION:
1138 c->value = dev->ctl_saturation;
1139 break;
1140 case V4L2_CID_AUDIO_MUTE:
1141 c->value = dev->ctl_mute;
1142 break;
1143 case V4L2_CID_AUDIO_VOLUME:
1144 c->value = dev->ctl_volume;
1145 break;
1146 case V4L2_CID_PRIVATE_INVERT:
1147 c->value = dev->ctl_invert;
1148 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001149 case V4L2_CID_HFLIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 c->value = dev->ctl_mirror;
1151 break;
1152 case V4L2_CID_PRIVATE_Y_EVEN:
1153 c->value = dev->ctl_y_even;
1154 break;
1155 case V4L2_CID_PRIVATE_Y_ODD:
1156 c->value = dev->ctl_y_odd;
1157 break;
1158 case V4L2_CID_PRIVATE_AUTOMUTE:
1159 c->value = dev->ctl_automute;
1160 break;
1161 default:
1162 return -EINVAL;
1163 }
1164 return 0;
1165}
Hans Verkuil531d83a2008-07-26 09:04:06 -03001166EXPORT_SYMBOL_GPL(saa7134_g_ctrl_internal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Hans Verkuil531d83a2008-07-26 09:04:06 -03001168static int saa7134_g_ctrl(struct file *file, void *priv, struct v4l2_control *c)
1169{
1170 struct saa7134_fh *fh = priv;
1171
1172 return saa7134_g_ctrl_internal(fh->dev, fh, c);
1173}
1174
1175int saa7134_s_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 const struct v4l2_queryctrl* ctrl;
1178 unsigned long flags;
1179 int restart_overlay = 0;
Hans Verkuil531d83a2008-07-26 09:04:06 -03001180 int err;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001181
Hans Verkuil531d83a2008-07-26 09:04:06 -03001182 /* When called from the empress code fh == NULL.
1183 That needs to be fixed somehow, but for now this is
1184 good enough. */
1185 if (fh) {
1186 err = v4l2_prio_check(&dev->prio, &fh->prio);
1187 if (0 != err)
1188 return err;
1189 }
1190 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001192 mutex_lock(&dev->lock);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 ctrl = ctrl_by_id(c->id);
1195 if (NULL == ctrl)
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001196 goto error;
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1199 switch (ctrl->type) {
1200 case V4L2_CTRL_TYPE_BOOLEAN:
1201 case V4L2_CTRL_TYPE_MENU:
1202 case V4L2_CTRL_TYPE_INTEGER:
1203 if (c->value < ctrl->minimum)
1204 c->value = ctrl->minimum;
1205 if (c->value > ctrl->maximum)
1206 c->value = ctrl->maximum;
1207 break;
1208 default:
1209 /* nothing */;
1210 };
1211 switch (c->id) {
1212 case V4L2_CID_BRIGHTNESS:
1213 dev->ctl_bright = c->value;
1214 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1215 break;
1216 case V4L2_CID_HUE:
1217 dev->ctl_hue = c->value;
1218 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1219 break;
1220 case V4L2_CID_CONTRAST:
1221 dev->ctl_contrast = c->value;
1222 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1223 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1224 break;
1225 case V4L2_CID_SATURATION:
1226 dev->ctl_saturation = c->value;
1227 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1228 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1229 break;
1230 case V4L2_CID_AUDIO_MUTE:
1231 dev->ctl_mute = c->value;
1232 saa7134_tvaudio_setmute(dev);
1233 break;
1234 case V4L2_CID_AUDIO_VOLUME:
1235 dev->ctl_volume = c->value;
1236 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1237 break;
1238 case V4L2_CID_PRIVATE_INVERT:
1239 dev->ctl_invert = c->value;
1240 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1241 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1242 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1243 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1244 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001245 case V4L2_CID_HFLIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 dev->ctl_mirror = c->value;
1247 restart_overlay = 1;
1248 break;
1249 case V4L2_CID_PRIVATE_Y_EVEN:
1250 dev->ctl_y_even = c->value;
1251 restart_overlay = 1;
1252 break;
1253 case V4L2_CID_PRIVATE_Y_ODD:
1254 dev->ctl_y_odd = c->value;
1255 restart_overlay = 1;
1256 break;
1257 case V4L2_CID_PRIVATE_AUTOMUTE:
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001258 {
1259 struct v4l2_priv_tun_config tda9887_cfg;
1260
1261 tda9887_cfg.tuner = TUNER_TDA9887;
1262 tda9887_cfg.priv = &dev->tda9887_conf;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 dev->ctl_automute = c->value;
1265 if (dev->tda9887_conf) {
1266 if (dev->ctl_automute)
1267 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1268 else
1269 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001270
Hans Verkuilfac69862009-01-17 12:17:14 -03001271 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273 break;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 default:
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001276 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1279 spin_lock_irqsave(&dev->slock,flags);
1280 stop_preview(dev,fh);
1281 start_preview(dev,fh);
1282 spin_unlock_irqrestore(&dev->slock,flags);
1283 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001284 err = 0;
1285
1286error:
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001287 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001288 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
Hans Verkuil531d83a2008-07-26 09:04:06 -03001290EXPORT_SYMBOL_GPL(saa7134_s_ctrl_internal);
1291
1292static int saa7134_s_ctrl(struct file *file, void *f, struct v4l2_control *c)
1293{
1294 struct saa7134_fh *fh = f;
1295
1296 return saa7134_s_ctrl_internal(fh->dev, fh, c);
1297}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299/* ------------------------------------------------------------------ */
1300
1301static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1302{
1303 struct videobuf_queue* q = NULL;
1304
1305 switch (fh->type) {
1306 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1307 q = &fh->cap;
1308 break;
1309 case V4L2_BUF_TYPE_VBI_CAPTURE:
1310 q = &fh->vbi;
1311 break;
1312 default:
1313 BUG();
1314 }
1315 return q;
1316}
1317
1318static int saa7134_resource(struct saa7134_fh *fh)
1319{
Heikki Orsila9040b322007-04-27 12:31:18 -03001320 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1321 return RESOURCE_VIDEO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Heikki Orsila9040b322007-04-27 12:31:18 -03001323 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1324 return RESOURCE_VBI;
1325
1326 BUG();
1327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
Hans Verkuilbec43662008-12-30 06:58:20 -03001330static int video_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Hans Verkuilbec43662008-12-30 06:58:20 -03001332 int minor = video_devdata(file)->minor;
Trent Piepho6d28e982007-10-10 05:37:42 -03001333 struct saa7134_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 struct saa7134_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1336 int radio = 0;
Hans Verkuild56dc612008-07-30 08:43:36 -03001337
Mauro Carvalho Chehabbefd6e62009-02-09 12:27:03 -03001338 mutex_lock(&saa7134_devlist_lock);
Trent Piepho6d28e982007-10-10 05:37:42 -03001339 list_for_each_entry(dev, &saa7134_devlist, devlist) {
1340 if (dev->video_dev && (dev->video_dev->minor == minor))
1341 goto found;
1342 if (dev->radio_dev && (dev->radio_dev->minor == minor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 radio = 1;
Trent Piepho6d28e982007-10-10 05:37:42 -03001344 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Trent Piepho6d28e982007-10-10 05:37:42 -03001346 if (dev->vbi_dev && (dev->vbi_dev->minor == minor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 type = V4L2_BUF_TYPE_VBI_CAPTURE;
Trent Piepho6d28e982007-10-10 05:37:42 -03001348 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 }
Mauro Carvalho Chehabbefd6e62009-02-09 12:27:03 -03001351 mutex_unlock(&saa7134_devlist_lock);
Trent Piepho6d28e982007-10-10 05:37:42 -03001352 return -ENODEV;
Mauro Carvalho Chehabbefd6e62009-02-09 12:27:03 -03001353
1354found:
1355 mutex_unlock(&saa7134_devlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1358 v4l2_type_names[type]);
1359
1360 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001361 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Mauro Carvalho Chehabbefd6e62009-02-09 12:27:03 -03001362 if (NULL == fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -ENOMEM;
Mauro Carvalho Chehabbefd6e62009-02-09 12:27:03 -03001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 file->private_data = fh;
1366 fh->dev = dev;
1367 fh->radio = radio;
1368 fh->type = type;
1369 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1370 fh->width = 720;
1371 fh->height = 576;
1372 v4l2_prio_open(&dev->prio,&fh->prio);
1373
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03001374 videobuf_queue_sg_init(&fh->cap, &video_qops,
1375 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1377 V4L2_FIELD_INTERLACED,
1378 sizeof(struct saa7134_buf),
1379 fh);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03001380 videobuf_queue_sg_init(&fh->vbi, &saa7134_vbi_qops,
1381 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 V4L2_BUF_TYPE_VBI_CAPTURE,
1383 V4L2_FIELD_SEQ_TB,
1384 sizeof(struct saa7134_buf),
1385 fh);
1386 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1387 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1388
1389 if (fh->radio) {
1390 /* switch to radio mode */
1391 saa7134_tvaudio_setinput(dev,&card(dev).radio);
Hans Verkuilfac69862009-01-17 12:17:14 -03001392 saa_call_all(dev, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 } else {
1394 /* switch to video/vbi mode */
1395 video_mux(dev,dev->ctl_input);
1396 }
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
1400static ssize_t
1401video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1402{
1403 struct saa7134_fh *fh = file->private_data;
1404
1405 switch (fh->type) {
1406 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1407 if (res_locked(fh->dev,RESOURCE_VIDEO))
1408 return -EBUSY;
1409 return videobuf_read_one(saa7134_queue(fh),
1410 data, count, ppos,
1411 file->f_flags & O_NONBLOCK);
1412 case V4L2_BUF_TYPE_VBI_CAPTURE:
1413 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1414 return -EBUSY;
1415 return videobuf_read_stream(saa7134_queue(fh),
1416 data, count, ppos, 1,
1417 file->f_flags & O_NONBLOCK);
1418 break;
1419 default:
1420 BUG();
1421 return 0;
1422 }
1423}
1424
1425static unsigned int
1426video_poll(struct file *file, struct poll_table_struct *wait)
1427{
1428 struct saa7134_fh *fh = file->private_data;
1429 struct videobuf_buffer *buf = NULL;
1430
1431 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1432 return videobuf_poll_stream(file, &fh->vbi, wait);
1433
1434 if (res_check(fh,RESOURCE_VIDEO)) {
1435 if (!list_empty(&fh->cap.stream))
1436 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1437 } else {
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001438 mutex_lock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (UNSET == fh->cap.read_off) {
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001440 /* need to capture a new frame */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001441 if (res_locked(fh->dev,RESOURCE_VIDEO))
1442 goto err;
1443 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field))
1444 goto err;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001445 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1446 fh->cap.read_off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001448 mutex_unlock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 buf = fh->cap.read_buf;
1450 }
1451
1452 if (!buf)
1453 return POLLERR;
1454
1455 poll_wait(file, &buf->done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03001456 if (buf->state == VIDEOBUF_DONE ||
1457 buf->state == VIDEOBUF_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return POLLIN|POLLRDNORM;
1459 return 0;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001460
1461err:
1462 mutex_unlock(&fh->cap.vb_lock);
1463 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Hans Verkuilbec43662008-12-30 06:58:20 -03001466static int video_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 struct saa7134_fh *fh = file->private_data;
1469 struct saa7134_dev *dev = fh->dev;
1470 unsigned long flags;
1471
1472 /* turn off overlay */
1473 if (res_check(fh, RESOURCE_OVERLAY)) {
1474 spin_lock_irqsave(&dev->slock,flags);
1475 stop_preview(dev,fh);
1476 spin_unlock_irqrestore(&dev->slock,flags);
1477 res_free(dev,fh,RESOURCE_OVERLAY);
1478 }
1479
1480 /* stop video capture */
1481 if (res_check(fh, RESOURCE_VIDEO)) {
1482 videobuf_streamoff(&fh->cap);
1483 res_free(dev,fh,RESOURCE_VIDEO);
1484 }
1485 if (fh->cap.read_buf) {
1486 buffer_release(&fh->cap,fh->cap.read_buf);
1487 kfree(fh->cap.read_buf);
1488 }
1489
1490 /* stop vbi capture */
1491 if (res_check(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03001492 videobuf_stop(&fh->vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 res_free(dev,fh,RESOURCE_VBI);
1494 }
1495
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001496 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1497 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1498 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1499 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1500 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1501
Hans Verkuilfac69862009-01-17 12:17:14 -03001502 saa_call_all(dev, core, s_standby, 0);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 /* free stuff */
1505 videobuf_mmap_free(&fh->cap);
1506 videobuf_mmap_free(&fh->vbi);
1507 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1508 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1509
1510 v4l2_prio_close(&dev->prio,&fh->prio);
1511 file->private_data = NULL;
1512 kfree(fh);
1513 return 0;
1514}
1515
Heikki Orsila9040b322007-04-27 12:31:18 -03001516static int video_mmap(struct file *file, struct vm_area_struct * vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 struct saa7134_fh *fh = file->private_data;
1519
1520 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1521}
1522
1523/* ------------------------------------------------------------------ */
1524
Hans Verkuil78b526a2008-05-28 12:16:41 -03001525static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001526 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001528 struct saa7134_fh *fh = priv;
1529 struct saa7134_dev *dev = fh->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 struct saa7134_tvnorm *norm = dev->tvnorm;
1531
1532 f->fmt.vbi.sampling_rate = 6750000 * 4;
1533 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1534 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1535 f->fmt.vbi.offset = 64 * 4;
Michael Schimekf246a812005-06-23 22:04:47 -07001536 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1537 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1538 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1540 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1541
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
Hans Verkuil78b526a2008-05-28 12:16:41 -03001545static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001546 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001548 struct saa7134_fh *fh = priv;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001549
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001550 f->fmt.pix.width = fh->width;
1551 f->fmt.pix.height = fh->height;
1552 f->fmt.pix.field = fh->cap.field;
1553 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1554 f->fmt.pix.bytesperline =
1555 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1556 f->fmt.pix.sizeimage =
1557 f->fmt.pix.height * f->fmt.pix.bytesperline;
1558 return 0;
1559}
1560
Hans Verkuil78b526a2008-05-28 12:16:41 -03001561static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001562 struct v4l2_format *f)
1563{
1564 struct saa7134_fh *fh = priv;
1565
1566 if (saa7134_no_overlay > 0) {
1567 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return -EINVAL;
1569 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001570 f->fmt.win = fh->win;
1571
1572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Hans Verkuil78b526a2008-05-28 12:16:41 -03001575static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001576 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001578 struct saa7134_fh *fh = priv;
1579 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001580 struct saa7134_format *fmt;
1581 enum v4l2_field field;
1582 unsigned int maxw, maxh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001584 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1585 if (NULL == fmt)
1586 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001588 field = f->fmt.pix.field;
1589 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1590 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001592 if (V4L2_FIELD_ANY == field) {
1593 field = (f->fmt.pix.height > maxh/2)
1594 ? V4L2_FIELD_INTERLACED
1595 : V4L2_FIELD_BOTTOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001597 switch (field) {
1598 case V4L2_FIELD_TOP:
1599 case V4L2_FIELD_BOTTOM:
1600 maxh = maxh / 2;
1601 break;
1602 case V4L2_FIELD_INTERLACED:
1603 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 default:
1605 return -EINVAL;
1606 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001607
1608 f->fmt.pix.field = field;
1609 if (f->fmt.pix.width < 48)
1610 f->fmt.pix.width = 48;
1611 if (f->fmt.pix.height < 32)
1612 f->fmt.pix.height = 32;
1613 if (f->fmt.pix.width > maxw)
1614 f->fmt.pix.width = maxw;
1615 if (f->fmt.pix.height > maxh)
1616 f->fmt.pix.height = maxh;
1617 f->fmt.pix.width &= ~0x03;
1618 f->fmt.pix.bytesperline =
1619 (f->fmt.pix.width * fmt->depth) >> 3;
1620 f->fmt.pix.sizeimage =
1621 f->fmt.pix.height * f->fmt.pix.bytesperline;
1622
1623 return 0;
1624}
1625
Hans Verkuil78b526a2008-05-28 12:16:41 -03001626static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001627 struct v4l2_format *f)
1628{
1629 struct saa7134_fh *fh = priv;
1630 struct saa7134_dev *dev = fh->dev;
1631
1632 if (saa7134_no_overlay > 0) {
1633 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1634 return -EINVAL;
1635 }
1636
1637 return verify_preview(dev, &f->fmt.win);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Hans Verkuil78b526a2008-05-28 12:16:41 -03001640static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001641 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001643 struct saa7134_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 int err;
1645
Hans Verkuil78b526a2008-05-28 12:16:41 -03001646 err = saa7134_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001647 if (0 != err)
1648 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001650 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1651 fh->width = f->fmt.pix.width;
1652 fh->height = f->fmt.pix.height;
1653 fh->cap.field = f->fmt.pix.field;
1654 return 0;
1655}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Hans Verkuil78b526a2008-05-28 12:16:41 -03001657static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001658 struct v4l2_format *f)
1659{
1660 struct saa7134_fh *fh = priv;
1661 struct saa7134_dev *dev = fh->dev;
1662 int err;
Steven Rostedta8b1ecf2008-05-06 20:42:29 -07001663 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001665 if (saa7134_no_overlay > 0) {
1666 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return -EINVAL;
1668 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001669 err = verify_preview(dev, &f->fmt.win);
1670 if (0 != err)
1671 return err;
1672
1673 mutex_lock(&dev->lock);
1674
1675 fh->win = f->fmt.win;
1676 fh->nclips = f->fmt.win.clipcount;
1677
1678 if (fh->nclips > 8)
1679 fh->nclips = 8;
1680
1681 if (copy_from_user(fh->clips, f->fmt.win.clips,
1682 sizeof(struct v4l2_clip)*fh->nclips)) {
1683 mutex_unlock(&dev->lock);
1684 return -EFAULT;
1685 }
1686
1687 if (res_check(fh, RESOURCE_OVERLAY)) {
1688 spin_lock_irqsave(&dev->slock, flags);
1689 stop_preview(dev, fh);
1690 start_preview(dev, fh);
1691 spin_unlock_irqrestore(&dev->slock, flags);
1692 }
1693
1694 mutex_unlock(&dev->lock);
1695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -03001698int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001699{
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}
Mauro Carvalho Chehabf6e90a62007-12-11 15:01:57 -03001711EXPORT_SYMBOL_GPL(saa7134_queryctrl);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001712
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001713static int saa7134_enum_input(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001714 struct v4l2_input *i)
1715{
1716 struct saa7134_fh *fh = priv;
1717 struct saa7134_dev *dev = fh->dev;
1718 unsigned int n;
1719
1720 n = i->index;
1721 if (n >= SAA7134_INPUT_MAX)
1722 return -EINVAL;
1723 if (NULL == card_in(dev, i->index).name)
1724 return -EINVAL;
1725 memset(i, 0, sizeof(*i));
1726 i->index = n;
1727 i->type = V4L2_INPUT_TYPE_CAMERA;
1728 strcpy(i->name, card_in(dev, n).name);
1729 if (card_in(dev, n).tv)
1730 i->type = V4L2_INPUT_TYPE_TUNER;
1731 i->audioset = 1;
1732 if (n == dev->ctl_input) {
1733 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1734 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1735
1736 if (0 != (v1 & 0x40))
1737 i->status |= V4L2_IN_ST_NO_H_LOCK;
1738 if (0 != (v2 & 0x40))
1739 i->status |= V4L2_IN_ST_NO_SYNC;
1740 if (0 != (v2 & 0x0e))
1741 i->status |= V4L2_IN_ST_MACROVISION;
1742 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001743 i->std = SAA7134_NORMS;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001744 return 0;
1745}
1746
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001747static int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001748{
1749 struct saa7134_fh *fh = priv;
1750 struct saa7134_dev *dev = fh->dev;
1751
1752 *i = dev->ctl_input;
1753 return 0;
1754}
1755
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001756static int saa7134_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001757{
1758 struct saa7134_fh *fh = priv;
1759 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001760 int err;
1761
1762 err = v4l2_prio_check(&dev->prio, &fh->prio);
1763 if (0 != err)
1764 return err;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001765
1766 if (i < 0 || i >= SAA7134_INPUT_MAX)
1767 return -EINVAL;
1768 if (NULL == card_in(dev, i).name)
1769 return -EINVAL;
1770 mutex_lock(&dev->lock);
1771 video_mux(dev, i);
1772 mutex_unlock(&dev->lock);
1773 return 0;
1774}
1775
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001776static int saa7134_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001777 struct v4l2_capability *cap)
1778{
1779 struct saa7134_fh *fh = priv;
1780 struct saa7134_dev *dev = fh->dev;
1781
1782 unsigned int tuner_type = dev->tuner_type;
1783
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001784 strcpy(cap->driver, "saa7134");
1785 strlcpy(cap->card, saa7134_boards[dev->board].name,
1786 sizeof(cap->card));
1787 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1788 cap->version = SAA7134_VERSION_CODE;
1789 cap->capabilities =
1790 V4L2_CAP_VIDEO_CAPTURE |
1791 V4L2_CAP_VBI_CAPTURE |
1792 V4L2_CAP_READWRITE |
1793 V4L2_CAP_STREAMING |
1794 V4L2_CAP_TUNER;
1795 if (saa7134_no_overlay <= 0)
1796 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1797
1798 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1799 cap->capabilities &= ~V4L2_CAP_TUNER;
1800 return 0;
1801}
1802
Hans Verkuil9afb7372008-09-06 06:34:44 -03001803int saa7134_s_std_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, v4l2_std_id *id)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001804{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 unsigned long flags;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001806 unsigned int i;
1807 v4l2_std_id fixup;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001808 int err;
1809
Hans Verkuil9afb7372008-09-06 06:34:44 -03001810 /* When called from the empress code fh == NULL.
1811 That needs to be fixed somehow, but for now this is
1812 good enough. */
1813 if (fh) {
1814 err = v4l2_prio_check(&dev->prio, &fh->prio);
1815 if (0 != err)
1816 return err;
1817 } else if (res_locked(dev, RESOURCE_OVERLAY)) {
1818 /* Don't change the std from the mpeg device
1819 if overlay is active. */
1820 return -EBUSY;
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001823 for (i = 0; i < TVNORMS; i++)
1824 if (*id == tvnorms[i].id)
1825 break;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001826
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001827 if (i == TVNORMS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 for (i = 0; i < TVNORMS; i++)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001829 if (*id & tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 break;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001831 if (i == TVNORMS)
1832 return -EINVAL;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001833
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001834 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1835 if (secam[0] == 'L' || secam[0] == 'l') {
1836 if (secam[1] == 'C' || secam[1] == 'c')
1837 fixup = V4L2_STD_SECAM_LC;
1838 else
1839 fixup = V4L2_STD_SECAM_L;
1840 } else {
1841 if (secam[0] == 'D' || secam[0] == 'd')
1842 fixup = V4L2_STD_SECAM_DK;
1843 else
1844 fixup = V4L2_STD_SECAM;
Hartmut Hackmann17b10a72006-10-02 19:55:07 -03001845 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001846 for (i = 0; i < TVNORMS; i++)
1847 if (fixup == tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001850
1851 *id = tvnorms[i].id;
1852
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001853 mutex_lock(&dev->lock);
Hans Verkuil9afb7372008-09-06 06:34:44 -03001854 if (fh && res_check(fh, RESOURCE_OVERLAY)) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001855 spin_lock_irqsave(&dev->slock, flags);
1856 stop_preview(dev, fh);
1857 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001859 set_tvnorm(dev, &tvnorms[i]);
1860
1861 spin_lock_irqsave(&dev->slock, flags);
1862 start_preview(dev, fh);
1863 spin_unlock_irqrestore(&dev->slock, flags);
1864 } else
1865 set_tvnorm(dev, &tvnorms[i]);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001866
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001867 saa7134_tvaudio_do_scan(dev);
1868 mutex_unlock(&dev->lock);
1869 return 0;
1870}
Hans Verkuil9afb7372008-09-06 06:34:44 -03001871EXPORT_SYMBOL_GPL(saa7134_s_std_internal);
1872
1873static int saa7134_s_std(struct file *file, void *priv, v4l2_std_id *id)
1874{
1875 struct saa7134_fh *fh = priv;
1876
1877 return saa7134_s_std_internal(fh->dev, fh, id);
1878}
1879
1880static int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1881{
1882 struct saa7134_fh *fh = priv;
1883 struct saa7134_dev *dev = fh->dev;
1884
1885 *id = dev->tvnorm->id;
1886 return 0;
1887}
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001888
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001889static int saa7134_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001890 struct v4l2_cropcap *cap)
1891{
1892 struct saa7134_fh *fh = priv;
1893 struct saa7134_dev *dev = fh->dev;
1894
1895 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1896 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1897 return -EINVAL;
1898 cap->bounds = dev->crop_bounds;
1899 cap->defrect = dev->crop_defrect;
1900 cap->pixelaspect.numerator = 1;
1901 cap->pixelaspect.denominator = 1;
1902 if (dev->tvnorm->id & V4L2_STD_525_60) {
1903 cap->pixelaspect.numerator = 11;
1904 cap->pixelaspect.denominator = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001906 if (dev->tvnorm->id & V4L2_STD_625_50) {
1907 cap->pixelaspect.numerator = 54;
1908 cap->pixelaspect.denominator = 59;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910 return 0;
1911}
1912
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001913static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001914{
1915 struct saa7134_fh *fh = f;
1916 struct saa7134_dev *dev = fh->dev;
1917
1918 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1919 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1920 return -EINVAL;
1921 crop->c = dev->crop_current;
1922 return 0;
1923}
1924
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001925static int saa7134_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001926{
1927 struct saa7134_fh *fh = f;
1928 struct saa7134_dev *dev = fh->dev;
1929 struct v4l2_rect *b = &dev->crop_bounds;
1930
1931 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1932 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1933 return -EINVAL;
1934 if (crop->c.height < 0)
1935 return -EINVAL;
1936 if (crop->c.width < 0)
1937 return -EINVAL;
1938
1939 if (res_locked(fh->dev, RESOURCE_OVERLAY))
1940 return -EBUSY;
1941 if (res_locked(fh->dev, RESOURCE_VIDEO))
1942 return -EBUSY;
1943
1944 if (crop->c.top < b->top)
1945 crop->c.top = b->top;
1946 if (crop->c.top > b->top + b->height)
1947 crop->c.top = b->top + b->height;
1948 if (crop->c.height > b->top - crop->c.top + b->height)
1949 crop->c.height = b->top - crop->c.top + b->height;
1950
1951 if (crop->c.left < b->left)
1952 crop->c.left = b->left;
1953 if (crop->c.left > b->left + b->width)
1954 crop->c.left = b->left + b->width;
1955 if (crop->c.width > b->left - crop->c.left + b->width)
1956 crop->c.width = b->left - crop->c.left + b->width;
1957
1958 dev->crop_current = crop->c;
1959 return 0;
1960}
1961
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001962static int saa7134_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001963 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001965 struct saa7134_fh *fh = priv;
1966 struct saa7134_dev *dev = fh->dev;
1967 int n;
1968
1969 if (0 != t->index)
1970 return -EINVAL;
1971 memset(t, 0, sizeof(*t));
1972 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1973 if (card_in(dev, n).tv)
1974 break;
1975 if (NULL != card_in(dev, n).name) {
1976 strcpy(t->name, "Television");
1977 t->type = V4L2_TUNER_ANALOG_TV;
1978 t->capability = V4L2_TUNER_CAP_NORM |
1979 V4L2_TUNER_CAP_STEREO |
1980 V4L2_TUNER_CAP_LANG1 |
1981 V4L2_TUNER_CAP_LANG2;
1982 t->rangehigh = 0xffffffffUL;
1983 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1984 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1985 }
1986 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1987 t->signal = 0xffff;
1988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001991static int saa7134_s_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001992 struct v4l2_tuner *t)
1993{
1994 struct saa7134_fh *fh = priv;
1995 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001996 int rx, mode, err;
1997
1998 err = v4l2_prio_check(&dev->prio, &fh->prio);
1999 if (0 != err)
2000 return err;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002001
2002 mode = dev->thread.mode;
2003 if (UNSET == mode) {
2004 rx = saa7134_tvaudio_getstereo(dev);
2005 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
2006 }
2007 if (mode != t->audmode)
2008 dev->thread.mode = t->audmode;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002009
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002010 return 0;
2011}
2012
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002013static int saa7134_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002014 struct v4l2_frequency *f)
2015{
2016 struct saa7134_fh *fh = priv;
2017 struct saa7134_dev *dev = fh->dev;
2018
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002019 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2020 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002021
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002022 return 0;
2023}
2024
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002025static int saa7134_s_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002026 struct v4l2_frequency *f)
2027{
2028 struct saa7134_fh *fh = priv;
2029 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002030 int err;
2031
2032 err = v4l2_prio_check(&dev->prio, &fh->prio);
2033 if (0 != err)
2034 return err;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002035
2036 if (0 != f->tuner)
2037 return -EINVAL;
2038 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
2039 return -EINVAL;
2040 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
2041 return -EINVAL;
2042 mutex_lock(&dev->lock);
2043 dev->ctl_freq = f->frequency;
2044
Hans Verkuilfac69862009-01-17 12:17:14 -03002045 saa_call_all(dev, tuner, s_frequency, f);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002046
2047 saa7134_tvaudio_do_scan(dev);
2048 mutex_unlock(&dev->lock);
2049 return 0;
2050}
2051
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002052static int saa7134_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002053{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002054 strcpy(a->name, "audio");
2055 return 0;
2056}
2057
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002058static int saa7134_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002059{
2060 return 0;
2061}
2062
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002063static int saa7134_g_priority(struct file *file, void *f, enum v4l2_priority *p)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002064{
2065 struct saa7134_fh *fh = f;
2066 struct saa7134_dev *dev = fh->dev;
2067
2068 *p = v4l2_prio_max(&dev->prio);
2069 return 0;
2070}
2071
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002072static int saa7134_s_priority(struct file *file, void *f,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002073 enum v4l2_priority prio)
2074{
2075 struct saa7134_fh *fh = f;
2076 struct saa7134_dev *dev = fh->dev;
2077
2078 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
2079}
2080
Hans Verkuil78b526a2008-05-28 12:16:41 -03002081static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002082 struct v4l2_fmtdesc *f)
2083{
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002084 if (f->index >= FORMATS)
2085 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002086
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002087 strlcpy(f->description, formats[f->index].name,
2088 sizeof(f->description));
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002089
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002090 f->pixelformat = formats[f->index].fourcc;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002091
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002092 return 0;
2093}
2094
Hans Verkuil78b526a2008-05-28 12:16:41 -03002095static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002096 struct v4l2_fmtdesc *f)
2097{
2098 if (saa7134_no_overlay > 0) {
2099 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002100 return -EINVAL;
2101 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002102
2103 if ((f->index >= FORMATS) || formats[f->index].planar)
2104 return -EINVAL;
2105
2106 strlcpy(f->description, formats[f->index].name,
2107 sizeof(f->description));
2108
2109 f->pixelformat = formats[f->index].fourcc;
2110
2111 return 0;
2112}
2113
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002114static int saa7134_g_fbuf(struct file *file, void *f,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002115 struct v4l2_framebuffer *fb)
2116{
2117 struct saa7134_fh *fh = f;
2118 struct saa7134_dev *dev = fh->dev;
2119
2120 *fb = dev->ovbuf;
2121 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2122
2123 return 0;
2124}
2125
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002126static int saa7134_s_fbuf(struct file *file, void *f,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002127 struct v4l2_framebuffer *fb)
2128{
2129 struct saa7134_fh *fh = f;
2130 struct saa7134_dev *dev = fh->dev;
2131 struct saa7134_format *fmt;
2132
2133 if (!capable(CAP_SYS_ADMIN) &&
2134 !capable(CAP_SYS_RAWIO))
2135 return -EPERM;
2136
2137 /* check args */
2138 fmt = format_by_fourcc(fb->fmt.pixelformat);
2139 if (NULL == fmt)
2140 return -EINVAL;
2141
2142 /* ok, accept it */
2143 dev->ovbuf = *fb;
2144 dev->ovfmt = fmt;
2145 if (0 == dev->ovbuf.fmt.bytesperline)
2146 dev->ovbuf.fmt.bytesperline =
2147 dev->ovbuf.fmt.width*fmt->depth/8;
2148 return 0;
2149}
2150
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002151static int saa7134_overlay(struct file *file, void *f, unsigned int on)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002152{
2153 struct saa7134_fh *fh = f;
2154 struct saa7134_dev *dev = fh->dev;
2155 unsigned long flags;
2156
2157 if (on) {
2158 if (saa7134_no_overlay > 0) {
2159 dprintk("no_overlay\n");
2160 return -EINVAL;
2161 }
2162
2163 if (!res_get(dev, fh, RESOURCE_OVERLAY))
2164 return -EBUSY;
2165 spin_lock_irqsave(&dev->slock, flags);
2166 start_preview(dev, fh);
2167 spin_unlock_irqrestore(&dev->slock, flags);
2168 }
2169 if (!on) {
2170 if (!res_check(fh, RESOURCE_OVERLAY))
2171 return -EINVAL;
2172 spin_lock_irqsave(&dev->slock, flags);
2173 stop_preview(dev, fh);
2174 spin_unlock_irqrestore(&dev->slock, flags);
2175 res_free(dev, fh, RESOURCE_OVERLAY);
2176 }
2177 return 0;
2178}
2179
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002180#ifdef CONFIG_VIDEO_V4L1_COMPAT
2181static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2182{
2183 struct saa7134_fh *fh = file->private_data;
2184 return videobuf_cgmbuf(saa7134_queue(fh), mbuf, 8);
2185}
2186#endif
2187
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002188static int saa7134_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002189 struct v4l2_requestbuffers *p)
2190{
2191 struct saa7134_fh *fh = priv;
2192 return videobuf_reqbufs(saa7134_queue(fh), p);
2193}
2194
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002195static int saa7134_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002196 struct v4l2_buffer *b)
2197{
2198 struct saa7134_fh *fh = priv;
2199 return videobuf_querybuf(saa7134_queue(fh), b);
2200}
2201
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002202static int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002203{
2204 struct saa7134_fh *fh = priv;
2205 return videobuf_qbuf(saa7134_queue(fh), b);
2206}
2207
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002208static int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002209{
2210 struct saa7134_fh *fh = priv;
2211 return videobuf_dqbuf(saa7134_queue(fh), b,
2212 file->f_flags & O_NONBLOCK);
2213}
2214
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002215static int saa7134_streamon(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002216 enum v4l2_buf_type type)
2217{
2218 struct saa7134_fh *fh = priv;
2219 struct saa7134_dev *dev = fh->dev;
2220 int res = saa7134_resource(fh);
2221
2222 if (!res_get(dev, fh, res))
2223 return -EBUSY;
2224
2225 return videobuf_streamon(saa7134_queue(fh));
2226}
2227
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002228static int saa7134_streamoff(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002229 enum v4l2_buf_type type)
2230{
2231 int err;
2232 struct saa7134_fh *fh = priv;
2233 struct saa7134_dev *dev = fh->dev;
2234 int res = saa7134_resource(fh);
2235
2236 err = videobuf_streamoff(saa7134_queue(fh));
2237 if (err < 0)
2238 return err;
2239 res_free(dev, fh, res);
2240 return 0;
2241}
2242
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002243static int saa7134_g_parm(struct file *file, void *fh,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002244 struct v4l2_streamparm *parm)
2245{
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002246 return 0;
2247}
2248
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002249#ifdef CONFIG_VIDEO_ADV_DEBUG
2250static int vidioc_g_register (struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002251 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002252{
2253 struct saa7134_fh *fh = priv;
2254 struct saa7134_dev *dev = fh->dev;
2255
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002256 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002257 return -EINVAL;
2258 reg->val = saa_readb(reg->reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002259 reg->size = 1;
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002260 return 0;
2261}
2262
2263static int vidioc_s_register (struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002264 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002265{
2266 struct saa7134_fh *fh = priv;
2267 struct saa7134_dev *dev = fh->dev;
2268
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002269 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002270 return -EINVAL;
2271 saa_writeb(reg->reg&0xffffff, reg->val);
2272 return 0;
2273}
2274#endif
2275
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002276static int radio_querycap(struct file *file, void *priv,
2277 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 struct saa7134_fh *fh = file->private_data;
2280 struct saa7134_dev *dev = fh->dev;
2281
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002282 strcpy(cap->driver, "saa7134");
2283 strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
2284 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
2285 cap->version = SAA7134_VERSION_CODE;
2286 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return 0;
2288}
2289
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002290static int radio_g_tuner(struct file *file, void *priv,
2291 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002293 struct saa7134_fh *fh = file->private_data;
2294 struct saa7134_dev *dev = fh->dev;
2295
2296 if (0 != t->index)
2297 return -EINVAL;
2298
2299 memset(t, 0, sizeof(*t));
2300 strcpy(t->name, "Radio");
2301 t->type = V4L2_TUNER_RADIO;
2302
Hans Verkuilfac69862009-01-17 12:17:14 -03002303 saa_call_all(dev, tuner, g_tuner, t);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002304 if (dev->input->amux == TV) {
2305 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2306 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2307 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2308 }
2309 return 0;
2310}
2311static int radio_s_tuner(struct file *file, void *priv,
2312 struct v4l2_tuner *t)
2313{
2314 struct saa7134_fh *fh = file->private_data;
2315 struct saa7134_dev *dev = fh->dev;
2316
2317 if (0 != t->index)
2318 return -EINVAL;
2319
Hans Verkuilfac69862009-01-17 12:17:14 -03002320 saa_call_all(dev, tuner, s_tuner, t);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002321 return 0;
2322}
2323
2324static int radio_enum_input(struct file *file, void *priv,
2325 struct v4l2_input *i)
2326{
2327 if (i->index != 0)
2328 return -EINVAL;
2329
2330 strcpy(i->name, "Radio");
2331 i->type = V4L2_INPUT_TYPE_TUNER;
2332
2333 return 0;
2334}
2335
2336static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2337{
2338 *i = 0;
2339 return 0;
2340}
2341
2342static int radio_g_audio(struct file *file, void *priv,
2343 struct v4l2_audio *a)
2344{
2345 memset(a, 0, sizeof(*a));
2346 strcpy(a->name, "Radio");
2347 return 0;
2348}
2349
2350static int radio_s_audio(struct file *file, void *priv,
2351 struct v4l2_audio *a)
2352{
2353 return 0;
2354}
2355
2356static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2357{
2358 return 0;
2359}
2360
2361static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
2362{
2363 return 0;
2364}
2365
2366static int radio_queryctrl(struct file *file, void *priv,
2367 struct v4l2_queryctrl *c)
2368{
2369 const struct v4l2_queryctrl *ctrl;
2370
2371 if (c->id < V4L2_CID_BASE ||
2372 c->id >= V4L2_CID_LASTP1)
2373 return -EINVAL;
2374 if (c->id == V4L2_CID_AUDIO_MUTE) {
2375 ctrl = ctrl_by_id(c->id);
2376 *c = *ctrl;
2377 } else
2378 *c = no_ctrl;
2379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381
Hans Verkuilbec43662008-12-30 06:58:20 -03002382static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
2384 .owner = THIS_MODULE,
2385 .open = video_open,
2386 .release = video_release,
2387 .read = video_read,
2388 .poll = video_poll,
2389 .mmap = video_mmap,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002390 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391};
2392
Hans Verkuila3998102008-07-21 02:57:38 -03002393static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002394 .vidioc_querycap = saa7134_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002395 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
2396 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
2397 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
2398 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
2399 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
2400 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
2401 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
2402 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002403 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2404 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2405 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002406 .vidioc_g_audio = saa7134_g_audio,
2407 .vidioc_s_audio = saa7134_s_audio,
2408 .vidioc_cropcap = saa7134_cropcap,
2409 .vidioc_reqbufs = saa7134_reqbufs,
2410 .vidioc_querybuf = saa7134_querybuf,
2411 .vidioc_qbuf = saa7134_qbuf,
2412 .vidioc_dqbuf = saa7134_dqbuf,
2413 .vidioc_s_std = saa7134_s_std,
Hans Verkuil9afb7372008-09-06 06:34:44 -03002414 .vidioc_g_std = saa7134_g_std,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002415 .vidioc_enum_input = saa7134_enum_input,
2416 .vidioc_g_input = saa7134_g_input,
2417 .vidioc_s_input = saa7134_s_input,
2418 .vidioc_queryctrl = saa7134_queryctrl,
2419 .vidioc_g_ctrl = saa7134_g_ctrl,
2420 .vidioc_s_ctrl = saa7134_s_ctrl,
2421 .vidioc_streamon = saa7134_streamon,
2422 .vidioc_streamoff = saa7134_streamoff,
2423 .vidioc_g_tuner = saa7134_g_tuner,
2424 .vidioc_s_tuner = saa7134_s_tuner,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002425#ifdef CONFIG_VIDEO_V4L1_COMPAT
2426 .vidiocgmbuf = vidiocgmbuf,
2427#endif
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002428 .vidioc_g_crop = saa7134_g_crop,
2429 .vidioc_s_crop = saa7134_s_crop,
2430 .vidioc_g_fbuf = saa7134_g_fbuf,
2431 .vidioc_s_fbuf = saa7134_s_fbuf,
2432 .vidioc_overlay = saa7134_overlay,
2433 .vidioc_g_priority = saa7134_g_priority,
2434 .vidioc_s_priority = saa7134_s_priority,
2435 .vidioc_g_parm = saa7134_g_parm,
2436 .vidioc_g_frequency = saa7134_g_frequency,
2437 .vidioc_s_frequency = saa7134_s_frequency,
Mauro Carvalho Chehabc121ba12008-06-16 12:31:57 -03002438#ifdef CONFIG_VIDEO_ADV_DEBUG
2439 .vidioc_g_register = vidioc_g_register,
2440 .vidioc_s_register = vidioc_s_register,
2441#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442};
2443
Hans Verkuilbec43662008-12-30 06:58:20 -03002444static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002445 .owner = THIS_MODULE,
2446 .open = video_open,
2447 .release = video_release,
2448 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002449};
2450
2451static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002452 .vidioc_querycap = radio_querycap,
2453 .vidioc_g_tuner = radio_g_tuner,
2454 .vidioc_enum_input = radio_enum_input,
2455 .vidioc_g_audio = radio_g_audio,
2456 .vidioc_s_tuner = radio_s_tuner,
2457 .vidioc_s_audio = radio_s_audio,
2458 .vidioc_s_input = radio_s_input,
2459 .vidioc_s_std = radio_s_std,
2460 .vidioc_queryctrl = radio_queryctrl,
2461 .vidioc_g_input = radio_g_input,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002462 .vidioc_g_ctrl = saa7134_g_ctrl,
2463 .vidioc_s_ctrl = saa7134_s_ctrl,
2464 .vidioc_g_frequency = saa7134_g_frequency,
2465 .vidioc_s_frequency = saa7134_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466};
2467
Hans Verkuila3998102008-07-21 02:57:38 -03002468/* ----------------------------------------------------------- */
2469/* exported stuff */
2470
2471struct video_device saa7134_video_template = {
2472 .name = "saa7134-video",
Hans Verkuila3998102008-07-21 02:57:38 -03002473 .fops = &video_fops,
2474 .ioctl_ops = &video_ioctl_ops,
2475 .minor = -1,
2476 .tvnorms = SAA7134_NORMS,
2477 .current_norm = V4L2_STD_PAL,
2478};
2479
2480struct video_device saa7134_radio_template = {
2481 .name = "saa7134-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002482 .fops = &radio_fops,
2483 .ioctl_ops = &radio_ioctl_ops,
2484 .minor = -1,
2485};
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487int saa7134_video_init1(struct saa7134_dev *dev)
2488{
2489 /* sanitycheck insmod options */
2490 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2491 gbuffers = 2;
2492 if (gbufsize < 0 || gbufsize > gbufsize_max)
2493 gbufsize = gbufsize_max;
2494 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2495
2496 /* put some sensible defaults into the data structures ... */
2497 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2498 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2499 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2500 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2501 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2502 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2503 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2504 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2505
2506 if (dev->tda9887_conf && dev->ctl_automute)
2507 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2508 dev->automute = 0;
2509
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002510 INIT_LIST_HEAD(&dev->video_q.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 init_timer(&dev->video_q.timeout);
2512 dev->video_q.timeout.function = saa7134_buffer_timeout;
2513 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2514 dev->video_q.dev = dev;
2515
Maxim Levitskycb712012007-09-27 20:34:25 -03002516 if (saa7134_boards[dev->board].video_out)
2517 saa7134_videoport_init(dev);
2518
2519 return 0;
2520}
2521
2522int saa7134_videoport_init(struct saa7134_dev *dev)
2523{
2524 /* enable video output */
2525 int vo = saa7134_boards[dev->board].video_out;
2526 int video_reg;
2527 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
Dmitry Belimov37df96d2008-06-10 12:43:58 -03002528
2529 /* Configure videoport */
Maxim Levitskycb712012007-09-27 20:34:25 -03002530 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2531 video_reg = video_out[vo][1];
2532 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2533 video_reg &= ~VP_T_CODE_P_INVERTED;
2534 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2535 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
Maxim Levitskycb712012007-09-27 20:34:25 -03002536 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2537 video_reg = video_out[vo][5];
2538 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2539 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2540 if (vid_port_opts & SET_CLOCK_INVERTED)
2541 video_reg |= VP_CLK_CTRL1_INVERTED;
2542 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2543 video_reg = video_out[vo][6];
2544 if (vid_port_opts & SET_VSYNC_OFF) {
2545 video_reg &= ~VP_VS_TYPE_MASK;
2546 video_reg |= VP_VS_TYPE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
Maxim Levitskycb712012007-09-27 20:34:25 -03002548 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2549 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2550 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Dmitry Belimov37df96d2008-06-10 12:43:58 -03002552 /* Start videoport */
2553 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 return 0;
2556}
2557
2558int saa7134_video_init2(struct saa7134_dev *dev)
2559{
2560 /* init video hw */
2561 set_tvnorm(dev,&tvnorms[0]);
2562 video_mux(dev,0);
2563 saa7134_tvaudio_setmute(dev);
2564 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2565 return 0;
2566}
2567
Maxim Levitsky11f70782007-09-27 20:44:39 -03002568void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
2570 static const char *st[] = {
2571 "(no signal)", "NTSC", "PAL", "SECAM" };
2572 u32 st1,st2;
2573
2574 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2575 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2576 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2577 (st1 & 0x40) ? "not locked" : "locked",
2578 (st2 & 0x40) ? "no" : "yes",
2579 st[st1 & 0x03]);
Maxim Levitsky11f70782007-09-27 20:44:39 -03002580 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 if (dev->nosignal) {
2583 /* no video signal -> mute audio */
2584 if (dev->ctl_automute)
2585 dev->automute = 1;
2586 saa7134_tvaudio_setmute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 } else {
2588 /* wake up tvaudio audio carrier scan thread */
2589 saa7134_tvaudio_do_scan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
Maxim Levitsky11f70782007-09-27 20:44:39 -03002591
2592 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2593 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2594 else
2595 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 if (dev->mops && dev->mops->signal_change)
2598 dev->mops->signal_change(dev);
2599}
2600
Maxim Levitsky11f70782007-09-27 20:44:39 -03002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2603{
2604 enum v4l2_field field;
2605
2606 spin_lock(&dev->slock);
2607 if (dev->video_q.curr) {
2608 dev->video_fieldcount++;
2609 field = dev->video_q.curr->vb.field;
2610 if (V4L2_FIELD_HAS_BOTH(field)) {
2611 /* make sure we have seen both fields */
2612 if ((status & 0x10) == 0x00) {
2613 dev->video_q.curr->top_seen = 1;
2614 goto done;
2615 }
2616 if (!dev->video_q.curr->top_seen)
2617 goto done;
2618 } else if (field == V4L2_FIELD_TOP) {
2619 if ((status & 0x10) != 0x10)
2620 goto done;
2621 } else if (field == V4L2_FIELD_BOTTOM) {
2622 if ((status & 0x10) != 0x00)
2623 goto done;
2624 }
2625 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
Brandon Philips0fc06862007-11-06 20:02:36 -03002626 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
2628 saa7134_buffer_next(dev,&dev->video_q);
2629
2630 done:
2631 spin_unlock(&dev->slock);
2632}
2633
2634/* ----------------------------------------------------------- */
2635/*
2636 * Local variables:
2637 * c-basic-offset: 8
2638 * End:
2639 */