blob: fae5e97a3eb286c37623cb8ac75b2a9b28929806 [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>
Hans Verkuil2983baf2009-03-29 06:26:27 -030033#include <media/rds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/* ------------------------------------------------------------------ */
36
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -030037unsigned int video_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static unsigned int gbuffers = 8;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030039static unsigned int noninterlaced; /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static unsigned int gbufsize = 720*576*4;
41static unsigned int gbufsize_max = 720*576*4;
Hartmut Hackmann17b10a72006-10-02 19:55:07 -030042static char secam[] = "--";
Linus Torvalds1da177e2005-04-16 15:20:36 -070043module_param(video_debug, int, 0644);
44MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
45module_param(gbuffers, int, 0444);
46MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
47module_param(noninterlaced, int, 0644);
Hartmut Hackmannd5fdd132006-07-15 09:45:34 -030048MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
Hartmut Hackmann17b10a72006-10-02 19:55:07 -030049module_param_string(secam, secam, sizeof(secam), 0644);
50MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -030053#define dprintk(fmt, arg...) if (video_debug&0x04) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
55
56/* ------------------------------------------------------------------ */
Robert W. Boone2f8d4f52005-11-08 21:37:10 -080057/* Defines for Video Output Port Register at address 0x191 */
58
59/* Bit 0: VIP code T bit polarity */
60
61#define VP_T_CODE_P_NON_INVERTED 0x00
62#define VP_T_CODE_P_INVERTED 0x01
63
64/* ------------------------------------------------------------------ */
65/* Defines for Video Output Port Register at address 0x195 */
66
67/* Bit 2: Video output clock delay control */
68
69#define VP_CLK_CTRL2_NOT_DELAYED 0x00
70#define VP_CLK_CTRL2_DELAYED 0x04
71
72/* Bit 1: Video output clock invert control */
73
74#define VP_CLK_CTRL1_NON_INVERTED 0x00
75#define VP_CLK_CTRL1_INVERTED 0x02
76
77/* ------------------------------------------------------------------ */
78/* Defines for Video Output Port Register at address 0x196 */
79
80/* Bits 2 to 0: VSYNC pin video vertical sync type */
81
82#define VP_VS_TYPE_MASK 0x07
83
84#define VP_VS_TYPE_OFF 0x00
85#define VP_VS_TYPE_V123 0x01
86#define VP_VS_TYPE_V_ITU 0x02
87#define VP_VS_TYPE_VGATE_L 0x03
88#define VP_VS_TYPE_RESERVED1 0x04
89#define VP_VS_TYPE_RESERVED2 0x05
90#define VP_VS_TYPE_F_ITU 0x06
91#define VP_VS_TYPE_SC_FID 0x07
92
93/* ------------------------------------------------------------------ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* data structs for video */
95
96static int video_out[][9] = {
97 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
98};
99
100static struct saa7134_format formats[] = {
101 {
102 .name = "8 bpp gray",
103 .fourcc = V4L2_PIX_FMT_GREY,
104 .depth = 8,
105 .pm = 0x06,
106 },{
107 .name = "15 bpp RGB, le",
108 .fourcc = V4L2_PIX_FMT_RGB555,
109 .depth = 16,
110 .pm = 0x13 | 0x80,
111 },{
112 .name = "15 bpp RGB, be",
113 .fourcc = V4L2_PIX_FMT_RGB555X,
114 .depth = 16,
115 .pm = 0x13 | 0x80,
116 .bswap = 1,
117 },{
118 .name = "16 bpp RGB, le",
119 .fourcc = V4L2_PIX_FMT_RGB565,
120 .depth = 16,
121 .pm = 0x10 | 0x80,
122 },{
123 .name = "16 bpp RGB, be",
124 .fourcc = V4L2_PIX_FMT_RGB565X,
125 .depth = 16,
126 .pm = 0x10 | 0x80,
127 .bswap = 1,
128 },{
129 .name = "24 bpp RGB, le",
130 .fourcc = V4L2_PIX_FMT_BGR24,
131 .depth = 24,
132 .pm = 0x11,
133 },{
134 .name = "24 bpp RGB, be",
135 .fourcc = V4L2_PIX_FMT_RGB24,
136 .depth = 24,
137 .pm = 0x11,
138 .bswap = 1,
139 },{
140 .name = "32 bpp RGB, le",
141 .fourcc = V4L2_PIX_FMT_BGR32,
142 .depth = 32,
143 .pm = 0x12,
144 },{
145 .name = "32 bpp RGB, be",
146 .fourcc = V4L2_PIX_FMT_RGB32,
147 .depth = 32,
148 .pm = 0x12,
149 .bswap = 1,
150 .wswap = 1,
151 },{
152 .name = "4:2:2 packed, YUYV",
153 .fourcc = V4L2_PIX_FMT_YUYV,
154 .depth = 16,
155 .pm = 0x00,
156 .bswap = 1,
157 .yuv = 1,
158 },{
159 .name = "4:2:2 packed, UYVY",
160 .fourcc = V4L2_PIX_FMT_UYVY,
161 .depth = 16,
162 .pm = 0x00,
163 .yuv = 1,
164 },{
165 .name = "4:2:2 planar, Y-Cb-Cr",
166 .fourcc = V4L2_PIX_FMT_YUV422P,
167 .depth = 16,
168 .pm = 0x09,
169 .yuv = 1,
170 .planar = 1,
171 .hshift = 1,
172 .vshift = 0,
173 },{
174 .name = "4:2:0 planar, Y-Cb-Cr",
175 .fourcc = V4L2_PIX_FMT_YUV420,
176 .depth = 12,
177 .pm = 0x0a,
178 .yuv = 1,
179 .planar = 1,
180 .hshift = 1,
181 .vshift = 1,
182 },{
183 .name = "4:2:0 planar, Y-Cb-Cr",
184 .fourcc = V4L2_PIX_FMT_YVU420,
185 .depth = 12,
186 .pm = 0x0a,
187 .yuv = 1,
188 .planar = 1,
189 .uvswap = 1,
190 .hshift = 1,
191 .vshift = 1,
192 }
193};
194#define FORMATS ARRAY_SIZE(formats)
195
196#define NORM_625_50 \
197 .h_start = 0, \
198 .h_stop = 719, \
199 .video_v_start = 24, \
200 .video_v_stop = 311, \
Michael Schimekf246a812005-06-23 22:04:47 -0700201 .vbi_v_start_0 = 7, \
202 .vbi_v_stop_0 = 22, \
203 .vbi_v_start_1 = 319, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 .src_timing = 4
205
206#define NORM_525_60 \
207 .h_start = 0, \
Sean Youngdd6ed862010-02-06 10:58:41 -0300208 .h_stop = 719, \
Michael Schimekf246a812005-06-23 22:04:47 -0700209 .video_v_start = 23, \
210 .video_v_stop = 262, \
211 .vbi_v_start_0 = 10, \
212 .vbi_v_stop_0 = 21, \
213 .vbi_v_start_1 = 273, \
214 .src_timing = 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216static struct saa7134_tvnorm tvnorms[] = {
217 {
218 .name = "PAL", /* autodetect */
219 .id = V4L2_STD_PAL,
220 NORM_625_50,
221
222 .sync_control = 0x18,
223 .luma_control = 0x40,
224 .chroma_ctrl1 = 0x81,
225 .chroma_gain = 0x2a,
226 .chroma_ctrl2 = 0x06,
227 .vgate_misc = 0x1c,
228
229 },{
230 .name = "PAL-BG",
231 .id = V4L2_STD_PAL_BG,
232 NORM_625_50,
233
234 .sync_control = 0x18,
235 .luma_control = 0x40,
236 .chroma_ctrl1 = 0x81,
237 .chroma_gain = 0x2a,
238 .chroma_ctrl2 = 0x06,
239 .vgate_misc = 0x1c,
240
241 },{
242 .name = "PAL-I",
243 .id = V4L2_STD_PAL_I,
244 NORM_625_50,
245
246 .sync_control = 0x18,
247 .luma_control = 0x40,
248 .chroma_ctrl1 = 0x81,
249 .chroma_gain = 0x2a,
250 .chroma_ctrl2 = 0x06,
251 .vgate_misc = 0x1c,
252
253 },{
254 .name = "PAL-DK",
255 .id = V4L2_STD_PAL_DK,
256 NORM_625_50,
257
258 .sync_control = 0x18,
259 .luma_control = 0x40,
260 .chroma_ctrl1 = 0x81,
261 .chroma_gain = 0x2a,
262 .chroma_ctrl2 = 0x06,
263 .vgate_misc = 0x1c,
264
265 },{
266 .name = "NTSC",
267 .id = V4L2_STD_NTSC,
268 NORM_525_60,
269
270 .sync_control = 0x59,
271 .luma_control = 0x40,
272 .chroma_ctrl1 = 0x89,
273 .chroma_gain = 0x2a,
274 .chroma_ctrl2 = 0x0e,
275 .vgate_misc = 0x18,
276
277 },{
278 .name = "SECAM",
279 .id = V4L2_STD_SECAM,
280 NORM_625_50,
281
Hartmut Hackmann17b10a72006-10-02 19:55:07 -0300282 .sync_control = 0x18,
283 .luma_control = 0x1b,
284 .chroma_ctrl1 = 0xd1,
285 .chroma_gain = 0x80,
286 .chroma_ctrl2 = 0x00,
287 .vgate_misc = 0x1c,
288
289 },{
290 .name = "SECAM-DK",
291 .id = V4L2_STD_SECAM_DK,
292 NORM_625_50,
293
294 .sync_control = 0x18,
295 .luma_control = 0x1b,
296 .chroma_ctrl1 = 0xd1,
297 .chroma_gain = 0x80,
298 .chroma_ctrl2 = 0x00,
299 .vgate_misc = 0x1c,
300
301 },{
302 .name = "SECAM-L",
303 .id = V4L2_STD_SECAM_L,
304 NORM_625_50,
305
306 .sync_control = 0x18,
307 .luma_control = 0x1b,
308 .chroma_ctrl1 = 0xd1,
309 .chroma_gain = 0x80,
310 .chroma_ctrl2 = 0x00,
311 .vgate_misc = 0x1c,
312
313 },{
314 .name = "SECAM-Lc",
315 .id = V4L2_STD_SECAM_LC,
316 NORM_625_50,
317
318 .sync_control = 0x18,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 .luma_control = 0x1b,
320 .chroma_ctrl1 = 0xd1,
321 .chroma_gain = 0x80,
322 .chroma_ctrl2 = 0x00,
323 .vgate_misc = 0x1c,
324
325 },{
326 .name = "PAL-M",
327 .id = V4L2_STD_PAL_M,
328 NORM_525_60,
329
330 .sync_control = 0x59,
331 .luma_control = 0x40,
332 .chroma_ctrl1 = 0xb9,
333 .chroma_gain = 0x2a,
334 .chroma_ctrl2 = 0x0e,
335 .vgate_misc = 0x18,
336
337 },{
338 .name = "PAL-Nc",
339 .id = V4L2_STD_PAL_Nc,
340 NORM_625_50,
341
342 .sync_control = 0x18,
343 .luma_control = 0x40,
344 .chroma_ctrl1 = 0xa1,
345 .chroma_gain = 0x2a,
346 .chroma_ctrl2 = 0x06,
347 .vgate_misc = 0x1c,
348
349 },{
350 .name = "PAL-60",
351 .id = V4L2_STD_PAL_60,
352
353 .h_start = 0,
354 .h_stop = 719,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800355 .video_v_start = 23,
356 .video_v_stop = 262,
357 .vbi_v_start_0 = 10,
358 .vbi_v_stop_0 = 21,
359 .vbi_v_start_1 = 273,
360 .src_timing = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 .sync_control = 0x18,
363 .luma_control = 0x40,
364 .chroma_ctrl1 = 0x81,
365 .chroma_gain = 0x2a,
366 .chroma_ctrl2 = 0x06,
367 .vgate_misc = 0x1c,
368 }
369};
370#define TVNORMS ARRAY_SIZE(tvnorms)
371
372#define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
373#define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
374#define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
375#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
376#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
377
378static const struct v4l2_queryctrl no_ctrl = {
379 .name = "42",
380 .flags = V4L2_CTRL_FLAG_DISABLED,
381};
382static const struct v4l2_queryctrl video_ctrls[] = {
383 /* --- video --- */
384 {
385 .id = V4L2_CID_BRIGHTNESS,
386 .name = "Brightness",
387 .minimum = 0,
388 .maximum = 255,
389 .step = 1,
390 .default_value = 128,
391 .type = V4L2_CTRL_TYPE_INTEGER,
392 },{
393 .id = V4L2_CID_CONTRAST,
394 .name = "Contrast",
395 .minimum = 0,
396 .maximum = 127,
397 .step = 1,
398 .default_value = 68,
399 .type = V4L2_CTRL_TYPE_INTEGER,
400 },{
401 .id = V4L2_CID_SATURATION,
402 .name = "Saturation",
403 .minimum = 0,
404 .maximum = 127,
405 .step = 1,
406 .default_value = 64,
407 .type = V4L2_CTRL_TYPE_INTEGER,
408 },{
409 .id = V4L2_CID_HUE,
410 .name = "Hue",
411 .minimum = -128,
412 .maximum = 127,
413 .step = 1,
414 .default_value = 0,
415 .type = V4L2_CTRL_TYPE_INTEGER,
416 },{
Michael Schimekf246a812005-06-23 22:04:47 -0700417 .id = V4L2_CID_HFLIP,
418 .name = "Mirror",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 .minimum = 0,
420 .maximum = 1,
421 .type = V4L2_CTRL_TYPE_BOOLEAN,
422 },
423 /* --- audio --- */
424 {
425 .id = V4L2_CID_AUDIO_MUTE,
426 .name = "Mute",
427 .minimum = 0,
428 .maximum = 1,
429 .type = V4L2_CTRL_TYPE_BOOLEAN,
430 },{
431 .id = V4L2_CID_AUDIO_VOLUME,
432 .name = "Volume",
433 .minimum = -15,
434 .maximum = 15,
435 .step = 1,
436 .default_value = 0,
437 .type = V4L2_CTRL_TYPE_INTEGER,
438 },
439 /* --- private --- */
440 {
441 .id = V4L2_CID_PRIVATE_INVERT,
442 .name = "Invert",
443 .minimum = 0,
444 .maximum = 1,
445 .type = V4L2_CTRL_TYPE_BOOLEAN,
446 },{
447 .id = V4L2_CID_PRIVATE_Y_ODD,
448 .name = "y offset odd field",
449 .minimum = 0,
450 .maximum = 128,
Mauro Carvalho Chehab553d3c52009-02-09 05:33:54 -0300451 .step = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 .default_value = 0,
453 .type = V4L2_CTRL_TYPE_INTEGER,
454 },{
455 .id = V4L2_CID_PRIVATE_Y_EVEN,
456 .name = "y offset even field",
457 .minimum = 0,
458 .maximum = 128,
Mauro Carvalho Chehab553d3c52009-02-09 05:33:54 -0300459 .step = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 .default_value = 0,
461 .type = V4L2_CTRL_TYPE_INTEGER,
462 },{
463 .id = V4L2_CID_PRIVATE_AUTOMUTE,
464 .name = "automute",
465 .minimum = 0,
466 .maximum = 1,
467 .default_value = 1,
468 .type = V4L2_CTRL_TYPE_BOOLEAN,
469 }
470};
471static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
472
473static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
474{
475 unsigned int i;
476
477 for (i = 0; i < CTRLS; i++)
478 if (video_ctrls[i].id == id)
479 return video_ctrls+i;
480 return NULL;
481}
482
483static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
484{
485 unsigned int i;
486
487 for (i = 0; i < FORMATS; i++)
488 if (formats[i].fourcc == fourcc)
489 return formats+i;
490 return NULL;
491}
492
493/* ----------------------------------------------------------------------- */
494/* resource management */
495
496static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
497{
498 if (fh->resources & bit)
499 /* have it already allocated */
500 return 1;
501
502 /* is it free? */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200503 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (dev->resources & bit) {
505 /* no, someone else uses it */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200506 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 0;
508 }
509 /* it's free, grab it */
510 fh->resources |= bit;
511 dev->resources |= bit;
512 dprintk("res: get %d\n",bit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200513 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 1;
515}
516
Heikki Orsila9040b322007-04-27 12:31:18 -0300517static int res_check(struct saa7134_fh *fh, unsigned int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 return (fh->resources & bit);
520}
521
Heikki Orsila9040b322007-04-27 12:31:18 -0300522static int res_locked(struct saa7134_dev *dev, unsigned int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 return (dev->resources & bit);
525}
526
527static
528void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
529{
Eric Sesterhennae246012006-03-13 13:17:11 -0300530 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Ingo Molnar3593cab2006-02-07 06:49:14 -0200532 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 fh->resources &= ~bits;
534 dev->resources &= ~bits;
535 dprintk("res: put %d\n",bits);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200536 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539/* ------------------------------------------------------------------ */
540
Adrian Bunkb02044d2007-10-24 13:23:14 -0300541static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 dprintk("set tv norm = %s\n",norm->name);
544 dev->tvnorm = norm;
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* setup cropping */
547 dev->crop_bounds.left = norm->h_start;
548 dev->crop_defrect.left = norm->h_start;
549 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
550 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
551
Michael Schimekf246a812005-06-23 22:04:47 -0700552 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 dev->crop_defrect.top = norm->video_v_start*2;
554 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
555 - dev->crop_bounds.top;
556 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
557
558 dev->crop_current = dev->crop_defrect;
559
Maxim Levitskyc4584732007-10-12 00:57:15 -0300560 saa7134_set_tvnorm_hw(dev);
Maxim Levitskycb712012007-09-27 20:34:25 -0300561}
562
563static void video_mux(struct saa7134_dev *dev, int input)
564{
565 dprintk("video input = %d [%s]\n", input, card_in(dev, input).name);
566 dev->ctl_input = input;
567 set_tvnorm(dev, dev->tvnorm);
568 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
569}
570
Maxim Levitskyc4584732007-10-12 00:57:15 -0300571
572static void saa7134_set_decoder(struct saa7134_dev *dev)
Maxim Levitskycb712012007-09-27 20:34:25 -0300573{
574 int luma_control, sync_control, mux;
575
576 struct saa7134_tvnorm *norm = dev->tvnorm;
577 mux = card_in(dev, dev->ctl_input).vmux;
578
579 luma_control = norm->luma_control;
580 sync_control = norm->sync_control;
581
582 if (mux > 5)
583 luma_control |= 0x80; /* svideo */
584 if (noninterlaced || dev->nosignal)
585 sync_control |= 0x20;
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* setup video decoder */
588 saa_writeb(SAA7134_INCR_DELAY, 0x08);
589 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
590 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
591
592 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
593 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
594 saa_writeb(SAA7134_HSYNC_START, 0xeb);
595 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
596 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
597
598 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
599 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
600 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Maxim Levitskyf5a1ac62007-09-27 20:34:20 -0300602 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
603 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
604
605 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
606 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
609 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
610 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
611
612 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
613 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
614
615 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
616 saa_writeb(SAA7134_VGATE_START, 0x11);
617 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
618 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
619 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
620 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Maxim Levitskyc4584732007-10-12 00:57:15 -0300623void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
624{
625 saa7134_set_decoder(dev);
626
Hartmut Hackmann7bff4b42008-04-22 14:46:08 -0300627 if (card_in(dev, dev->ctl_input).tv)
Hans Verkuilf41737e2009-04-01 03:52:39 -0300628 saa_call_all(dev, core, s_std, dev->tvnorm->id);
Hans Verkuil9afb7372008-09-06 06:34:44 -0300629 /* Set the correct norm for the saa6752hs. This function
630 does nothing if there is no saa6752hs. */
Hans Verkuilf41737e2009-04-01 03:52:39 -0300631 saa_call_empress(dev, core, s_std, dev->tvnorm->id);
Maxim Levitskyc4584732007-10-12 00:57:15 -0300632}
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
635{
636 static const struct {
637 int xpsc;
638 int xacl;
639 int xc2_1;
640 int xdcg;
641 int vpfy;
642 } vals[] = {
643 /* XPSC XACL XC2_1 XDCG VPFY */
644 { 1, 0, 0, 0, 0 },
645 { 2, 2, 1, 2, 2 },
646 { 3, 4, 1, 3, 2 },
647 { 4, 8, 1, 4, 2 },
648 { 5, 8, 1, 4, 2 },
649 { 6, 8, 1, 4, 3 },
650 { 7, 8, 1, 4, 3 },
651 { 8, 15, 0, 4, 3 },
652 { 9, 15, 0, 4, 3 },
653 { 10, 16, 1, 5, 3 },
654 };
655 static const int count = ARRAY_SIZE(vals);
656 int i;
657
658 for (i = 0; i < count; i++)
659 if (vals[i].xpsc == prescale)
660 break;
661 if (i == count)
662 return;
663
664 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
665 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
666 saa_writeb(SAA7134_LEVEL_CTRL(task),
667 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
668 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
669 (vals[i].vpfy << 2) | vals[i].vpfy);
670}
671
672static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
673{
674 int val,mirror;
675
676 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
677 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
678
679 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
680 if (yscale < 2048) {
681 /* LPI */
682 dprintk("yscale LPI yscale=%d\n",yscale);
683 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
684 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
685 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
686 } else {
687 /* ACM */
688 val = 0x40 * 1024 / yscale;
689 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
690 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
691 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
692 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
693 }
694 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
695}
696
697static void set_size(struct saa7134_dev *dev, int task,
698 int width, int height, int interlace)
699{
700 int prescale,xscale,yscale,y_even,y_odd;
701 int h_start, h_stop, v_start, v_stop;
702 int div = interlace ? 2 : 1;
703
704 /* setup video scaler */
705 h_start = dev->crop_current.left;
706 v_start = dev->crop_current.top/2;
707 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
708 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
709
710 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
711 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
712 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
713 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
714 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
715 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
716 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
717 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
718
719 prescale = dev->crop_current.width / width;
720 if (0 == prescale)
721 prescale = 1;
722 xscale = 1024 * dev->crop_current.width / prescale / width;
723 yscale = 512 * div * dev->crop_current.height / height;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800724 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 set_h_prescale(dev,task,prescale);
726 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
727 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
728 set_v_scale(dev,task,yscale);
729
730 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
731 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
732 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
733 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
734
735 /* deinterlace y offsets */
736 y_odd = dev->ctl_y_odd;
737 y_even = dev->ctl_y_even;
738 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
739 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
740 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
741 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
742}
743
744/* ------------------------------------------------------------------ */
745
746struct cliplist {
747 __u16 position;
748 __u8 enable;
749 __u8 disable;
750};
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752static void set_cliplist(struct saa7134_dev *dev, int reg,
753 struct cliplist *cl, int entries, char *name)
754{
755 __u8 winbits = 0;
756 int i;
757
758 for (i = 0; i < entries; i++) {
759 winbits |= cl[i].enable;
760 winbits &= ~cl[i].disable;
761 if (i < 15 && cl[i].position == cl[i+1].position)
762 continue;
763 saa_writeb(reg + 0, winbits);
764 saa_writeb(reg + 2, cl[i].position & 0xff);
765 saa_writeb(reg + 3, cl[i].position >> 8);
766 dprintk("clip: %s winbits=%02x pos=%d\n",
767 name,winbits,cl[i].position);
768 reg += 8;
769 }
770 for (; reg < 0x400; reg += 8) {
771 saa_writeb(reg+ 0, 0);
772 saa_writeb(reg + 1, 0);
773 saa_writeb(reg + 2, 0);
774 saa_writeb(reg + 3, 0);
775 }
776}
777
778static int clip_range(int val)
779{
780 if (val < 0)
781 val = 0;
782 return val;
783}
784
Heikki Orsila9040b322007-04-27 12:31:18 -0300785/* Sort into smallest position first order */
786static int cliplist_cmp(const void *a, const void *b)
787{
788 const struct cliplist *cla = a;
789 const struct cliplist *clb = b;
790 if (cla->position < clb->position)
791 return -1;
792 if (cla->position > clb->position)
793 return 1;
794 return 0;
795}
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
798 int nclips, int interlace)
799{
800 struct cliplist col[16], row[16];
Heikki Orsila9040b322007-04-27 12:31:18 -0300801 int cols = 0, rows = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 int div = interlace ? 2 : 1;
803
Heikki Orsila9040b322007-04-27 12:31:18 -0300804 memset(col, 0, sizeof(col));
805 memset(row, 0, sizeof(row));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 for (i = 0; i < nclips && i < 8; i++) {
807 col[cols].position = clip_range(clips[i].c.left);
808 col[cols].enable = (1 << i);
809 cols++;
810 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
811 col[cols].disable = (1 << i);
812 cols++;
813 row[rows].position = clip_range(clips[i].c.top / div);
814 row[rows].enable = (1 << i);
815 rows++;
816 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
817 / div);
818 row[rows].disable = (1 << i);
819 rows++;
820 }
Heikki Orsila9040b322007-04-27 12:31:18 -0300821 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
822 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 set_cliplist(dev,0x380,col,cols,"cols");
824 set_cliplist(dev,0x384,row,rows,"rows");
825 return 0;
826}
827
828static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
829{
830 enum v4l2_field field;
831 int maxw, maxh;
832
833 if (NULL == dev->ovbuf.base)
834 return -EINVAL;
835 if (NULL == dev->ovfmt)
836 return -EINVAL;
837 if (win->w.width < 48 || win->w.height < 32)
838 return -EINVAL;
839 if (win->clipcount > 2048)
840 return -EINVAL;
841
842 field = win->field;
843 maxw = dev->crop_current.width;
844 maxh = dev->crop_current.height;
845
846 if (V4L2_FIELD_ANY == field) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800847 field = (win->w.height > maxh/2)
848 ? V4L2_FIELD_INTERLACED
849 : V4L2_FIELD_TOP;
850 }
851 switch (field) {
852 case V4L2_FIELD_TOP:
853 case V4L2_FIELD_BOTTOM:
854 maxh = maxh / 2;
855 break;
856 case V4L2_FIELD_INTERLACED:
857 break;
858 default:
859 return -EINVAL;
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 win->field = field;
863 if (win->w.width > maxw)
864 win->w.width = maxw;
865 if (win->w.height > maxh)
866 win->w.height = maxh;
867 return 0;
868}
869
870static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
871{
872 unsigned long base,control,bpl;
873 int err;
874
875 err = verify_preview(dev,&fh->win);
876 if (0 != err)
877 return err;
878
879 dev->ovfield = fh->win.field;
880 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
881 fh->win.w.width,fh->win.w.height,
882 fh->win.w.left,fh->win.w.top,
883 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
884
885 /* setup window + clipping */
886 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
887 V4L2_FIELD_HAS_BOTH(dev->ovfield));
888 setup_clipping(dev,fh->clips,fh->nclips,
889 V4L2_FIELD_HAS_BOTH(dev->ovfield));
890 if (dev->ovfmt->yuv)
891 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
892 else
893 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
894 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
895
896 /* dma: setup channel 1 (= Video Task B) */
897 base = (unsigned long)dev->ovbuf.base;
898 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
899 base += dev->ovfmt->depth/8 * fh->win.w.left;
900 bpl = dev->ovbuf.fmt.bytesperline;
901 control = SAA7134_RS_CONTROL_BURST_16;
902 if (dev->ovfmt->bswap)
903 control |= SAA7134_RS_CONTROL_BSWAP;
904 if (dev->ovfmt->wswap)
905 control |= SAA7134_RS_CONTROL_WSWAP;
906 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
907 saa_writel(SAA7134_RS_BA1(1),base);
908 saa_writel(SAA7134_RS_BA2(1),base+bpl);
909 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
910 saa_writel(SAA7134_RS_CONTROL(1),control);
911 } else {
912 saa_writel(SAA7134_RS_BA1(1),base);
913 saa_writel(SAA7134_RS_BA2(1),base);
914 saa_writel(SAA7134_RS_PITCH(1),bpl);
915 saa_writel(SAA7134_RS_CONTROL(1),control);
916 }
917
918 /* start dma */
919 dev->ovenable = 1;
920 saa7134_set_dmabits(dev);
921
922 return 0;
923}
924
925static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
926{
927 dev->ovenable = 0;
928 saa7134_set_dmabits(dev);
929 return 0;
930}
931
932/* ------------------------------------------------------------------ */
933
934static int buffer_activate(struct saa7134_dev *dev,
935 struct saa7134_buf *buf,
936 struct saa7134_buf *next)
937{
938 unsigned long base,control,bpl;
939 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
940
941 dprintk("buffer_activate buf=%p\n",buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300942 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 buf->top_seen = 0;
944
945 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
946 V4L2_FIELD_HAS_BOTH(buf->vb.field));
947 if (buf->fmt->yuv)
948 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
949 else
950 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
951 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
952
953 /* DMA: setup channel 0 (= Video Task A0) */
954 base = saa7134_buffer_base(buf);
955 if (buf->fmt->planar)
956 bpl = buf->vb.width;
957 else
958 bpl = (buf->vb.width * buf->fmt->depth) / 8;
959 control = SAA7134_RS_CONTROL_BURST_16 |
960 SAA7134_RS_CONTROL_ME |
961 (buf->pt->dma >> 12);
962 if (buf->fmt->bswap)
963 control |= SAA7134_RS_CONTROL_BSWAP;
964 if (buf->fmt->wswap)
965 control |= SAA7134_RS_CONTROL_WSWAP;
966 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
967 /* interlaced */
968 saa_writel(SAA7134_RS_BA1(0),base);
969 saa_writel(SAA7134_RS_BA2(0),base+bpl);
970 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
971 } else {
972 /* non-interlaced */
973 saa_writel(SAA7134_RS_BA1(0),base);
974 saa_writel(SAA7134_RS_BA2(0),base);
975 saa_writel(SAA7134_RS_PITCH(0),bpl);
976 }
977 saa_writel(SAA7134_RS_CONTROL(0),control);
978
979 if (buf->fmt->planar) {
980 /* DMA: setup channel 4+5 (= planar task A) */
981 bpl_uv = bpl >> buf->fmt->hshift;
982 lines_uv = buf->vb.height >> buf->fmt->vshift;
983 base2 = base + bpl * buf->vb.height;
984 base3 = base2 + bpl_uv * lines_uv;
985 if (buf->fmt->uvswap)
986 tmp = base2, base2 = base3, base3 = tmp;
987 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
988 bpl_uv,lines_uv,base2,base3);
989 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
990 /* interlaced */
991 saa_writel(SAA7134_RS_BA1(4),base2);
992 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
993 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
994 saa_writel(SAA7134_RS_BA1(5),base3);
995 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
996 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
997 } else {
998 /* non-interlaced */
999 saa_writel(SAA7134_RS_BA1(4),base2);
1000 saa_writel(SAA7134_RS_BA2(4),base2);
1001 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
1002 saa_writel(SAA7134_RS_BA1(5),base3);
1003 saa_writel(SAA7134_RS_BA2(5),base3);
1004 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
1005 }
1006 saa_writel(SAA7134_RS_CONTROL(4),control);
1007 saa_writel(SAA7134_RS_CONTROL(5),control);
1008 }
1009
1010 /* start DMA */
1011 saa7134_set_dmabits(dev);
1012 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1013 return 0;
1014}
1015
1016static int buffer_prepare(struct videobuf_queue *q,
1017 struct videobuf_buffer *vb,
1018 enum v4l2_field field)
1019{
1020 struct saa7134_fh *fh = q->priv_data;
1021 struct saa7134_dev *dev = fh->dev;
1022 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1023 unsigned int size;
1024 int err;
1025
1026 /* sanity checks */
1027 if (NULL == fh->fmt)
1028 return -EINVAL;
1029 if (fh->width < 48 ||
1030 fh->height < 32 ||
1031 fh->width/4 > dev->crop_current.width ||
1032 fh->height/4 > dev->crop_current.height ||
1033 fh->width > dev->crop_bounds.width ||
1034 fh->height > dev->crop_bounds.height)
1035 return -EINVAL;
1036 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1037 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1038 return -EINVAL;
1039
1040 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1041 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1042 fh->fmt->name);
1043 if (buf->vb.width != fh->width ||
1044 buf->vb.height != fh->height ||
1045 buf->vb.size != size ||
1046 buf->vb.field != field ||
1047 buf->fmt != fh->fmt) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001048 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050
Brandon Philips0fc06862007-11-06 20:02:36 -03001051 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001052 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 buf->vb.width = fh->width;
1055 buf->vb.height = fh->height;
1056 buf->vb.size = size;
1057 buf->vb.field = field;
1058 buf->fmt = fh->fmt;
1059 buf->pt = &fh->pt_cap;
figo.zhangd5709a02009-05-07 23:31:02 -03001060 dev->video_q.curr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001062 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (err)
1064 goto oops;
1065 err = saa7134_pgtable_build(dev->pci,buf->pt,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001066 dma->sglist,
1067 dma->sglen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 saa7134_buffer_startpage(buf));
1069 if (err)
1070 goto oops;
1071 }
Brandon Philips0fc06862007-11-06 20:02:36 -03001072 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 buf->activate = buffer_activate;
1074 return 0;
1075
1076 oops:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001077 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return err;
1079}
1080
1081static int
1082buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1083{
1084 struct saa7134_fh *fh = q->priv_data;
1085
1086 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1087 if (0 == *count)
1088 *count = gbuffers;
1089 *count = saa7134_buffer_count(*size,*count);
1090 return 0;
1091}
1092
1093static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1094{
1095 struct saa7134_fh *fh = q->priv_data;
1096 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1097
1098 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1099}
1100
1101static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1102{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1104
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001105 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108static struct videobuf_queue_ops video_qops = {
1109 .buf_setup = buffer_setup,
1110 .buf_prepare = buffer_prepare,
1111 .buf_queue = buffer_queue,
1112 .buf_release = buffer_release,
1113};
1114
1115/* ------------------------------------------------------------------ */
1116
Hans Verkuil531d83a2008-07-26 09:04:06 -03001117int saa7134_g_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 const struct v4l2_queryctrl* ctrl;
1120
1121 ctrl = ctrl_by_id(c->id);
1122 if (NULL == ctrl)
1123 return -EINVAL;
1124 switch (c->id) {
1125 case V4L2_CID_BRIGHTNESS:
1126 c->value = dev->ctl_bright;
1127 break;
1128 case V4L2_CID_HUE:
1129 c->value = dev->ctl_hue;
1130 break;
1131 case V4L2_CID_CONTRAST:
1132 c->value = dev->ctl_contrast;
1133 break;
1134 case V4L2_CID_SATURATION:
1135 c->value = dev->ctl_saturation;
1136 break;
1137 case V4L2_CID_AUDIO_MUTE:
1138 c->value = dev->ctl_mute;
1139 break;
1140 case V4L2_CID_AUDIO_VOLUME:
1141 c->value = dev->ctl_volume;
1142 break;
1143 case V4L2_CID_PRIVATE_INVERT:
1144 c->value = dev->ctl_invert;
1145 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001146 case V4L2_CID_HFLIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 c->value = dev->ctl_mirror;
1148 break;
1149 case V4L2_CID_PRIVATE_Y_EVEN:
1150 c->value = dev->ctl_y_even;
1151 break;
1152 case V4L2_CID_PRIVATE_Y_ODD:
1153 c->value = dev->ctl_y_odd;
1154 break;
1155 case V4L2_CID_PRIVATE_AUTOMUTE:
1156 c->value = dev->ctl_automute;
1157 break;
1158 default:
1159 return -EINVAL;
1160 }
1161 return 0;
1162}
Hans Verkuil531d83a2008-07-26 09:04:06 -03001163EXPORT_SYMBOL_GPL(saa7134_g_ctrl_internal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Hans Verkuil531d83a2008-07-26 09:04:06 -03001165static int saa7134_g_ctrl(struct file *file, void *priv, struct v4l2_control *c)
1166{
1167 struct saa7134_fh *fh = priv;
1168
1169 return saa7134_g_ctrl_internal(fh->dev, fh, c);
1170}
1171
1172int saa7134_s_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 const struct v4l2_queryctrl* ctrl;
1175 unsigned long flags;
1176 int restart_overlay = 0;
Hans Verkuil531d83a2008-07-26 09:04:06 -03001177 int err;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001178
Hans Verkuil531d83a2008-07-26 09:04:06 -03001179 /* When called from the empress code fh == NULL.
1180 That needs to be fixed somehow, but for now this is
1181 good enough. */
1182 if (fh) {
Hans Verkuilffb48772010-05-01 08:03:24 -03001183 err = v4l2_prio_check(&dev->prio, fh->prio);
Hans Verkuil531d83a2008-07-26 09:04:06 -03001184 if (0 != err)
1185 return err;
1186 }
1187 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001189 mutex_lock(&dev->lock);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 ctrl = ctrl_by_id(c->id);
1192 if (NULL == ctrl)
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001193 goto error;
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1196 switch (ctrl->type) {
1197 case V4L2_CTRL_TYPE_BOOLEAN:
1198 case V4L2_CTRL_TYPE_MENU:
1199 case V4L2_CTRL_TYPE_INTEGER:
1200 if (c->value < ctrl->minimum)
1201 c->value = ctrl->minimum;
1202 if (c->value > ctrl->maximum)
1203 c->value = ctrl->maximum;
1204 break;
1205 default:
1206 /* nothing */;
1207 };
1208 switch (c->id) {
1209 case V4L2_CID_BRIGHTNESS:
1210 dev->ctl_bright = c->value;
1211 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1212 break;
1213 case V4L2_CID_HUE:
1214 dev->ctl_hue = c->value;
1215 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1216 break;
1217 case V4L2_CID_CONTRAST:
1218 dev->ctl_contrast = c->value;
1219 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1220 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1221 break;
1222 case V4L2_CID_SATURATION:
1223 dev->ctl_saturation = c->value;
1224 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1225 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1226 break;
1227 case V4L2_CID_AUDIO_MUTE:
1228 dev->ctl_mute = c->value;
1229 saa7134_tvaudio_setmute(dev);
1230 break;
1231 case V4L2_CID_AUDIO_VOLUME:
1232 dev->ctl_volume = c->value;
1233 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1234 break;
1235 case V4L2_CID_PRIVATE_INVERT:
1236 dev->ctl_invert = c->value;
1237 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1238 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1239 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1240 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1241 break;
Michael Schimekf246a812005-06-23 22:04:47 -07001242 case V4L2_CID_HFLIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 dev->ctl_mirror = c->value;
1244 restart_overlay = 1;
1245 break;
1246 case V4L2_CID_PRIVATE_Y_EVEN:
1247 dev->ctl_y_even = c->value;
1248 restart_overlay = 1;
1249 break;
1250 case V4L2_CID_PRIVATE_Y_ODD:
1251 dev->ctl_y_odd = c->value;
1252 restart_overlay = 1;
1253 break;
1254 case V4L2_CID_PRIVATE_AUTOMUTE:
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001255 {
1256 struct v4l2_priv_tun_config tda9887_cfg;
1257
1258 tda9887_cfg.tuner = TUNER_TDA9887;
1259 tda9887_cfg.priv = &dev->tda9887_conf;
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 dev->ctl_automute = c->value;
1262 if (dev->tda9887_conf) {
1263 if (dev->ctl_automute)
1264 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1265 else
1266 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001267
Hans Verkuilfac69862009-01-17 12:17:14 -03001268 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270 break;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 default:
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001273 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1276 spin_lock_irqsave(&dev->slock,flags);
1277 stop_preview(dev,fh);
1278 start_preview(dev,fh);
1279 spin_unlock_irqrestore(&dev->slock,flags);
1280 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001281 err = 0;
1282
1283error:
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001284 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001285 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
Hans Verkuil531d83a2008-07-26 09:04:06 -03001287EXPORT_SYMBOL_GPL(saa7134_s_ctrl_internal);
1288
1289static int saa7134_s_ctrl(struct file *file, void *f, struct v4l2_control *c)
1290{
1291 struct saa7134_fh *fh = f;
1292
1293 return saa7134_s_ctrl_internal(fh->dev, fh, c);
1294}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296/* ------------------------------------------------------------------ */
1297
1298static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1299{
1300 struct videobuf_queue* q = NULL;
1301
1302 switch (fh->type) {
1303 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1304 q = &fh->cap;
1305 break;
1306 case V4L2_BUF_TYPE_VBI_CAPTURE:
1307 q = &fh->vbi;
1308 break;
1309 default:
1310 BUG();
1311 }
1312 return q;
1313}
1314
1315static int saa7134_resource(struct saa7134_fh *fh)
1316{
Heikki Orsila9040b322007-04-27 12:31:18 -03001317 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1318 return RESOURCE_VIDEO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Heikki Orsila9040b322007-04-27 12:31:18 -03001320 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1321 return RESOURCE_VBI;
1322
1323 BUG();
1324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Hans Verkuilbec43662008-12-30 06:58:20 -03001327static int video_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001329 struct video_device *vdev = video_devdata(file);
1330 struct saa7134_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 struct saa7134_fh *fh;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001332 enum v4l2_buf_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 int radio = 0;
Hans Verkuild56dc612008-07-30 08:43:36 -03001334
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001335 switch (vdev->vfl_type) {
1336 case VFL_TYPE_GRABBER:
1337 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1338 break;
1339 case VFL_TYPE_VBI:
1340 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1341 break;
1342 case VFL_TYPE_RADIO:
1343 radio = 1;
1344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001347 dprintk("open dev=%s radio=%d type=%s\n", video_device_node_name(vdev),
1348 radio, v4l2_type_names[type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001351 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Mauro Carvalho Chehabbefd6e62009-02-09 12:27:03 -03001352 if (NULL == fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -ENOMEM;
Mauro Carvalho Chehabbefd6e62009-02-09 12:27:03 -03001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 file->private_data = fh;
1356 fh->dev = dev;
1357 fh->radio = radio;
1358 fh->type = type;
1359 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1360 fh->width = 720;
1361 fh->height = 576;
Hans Verkuilffb48772010-05-01 08:03:24 -03001362 v4l2_prio_open(&dev->prio, &fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03001364 videobuf_queue_sg_init(&fh->cap, &video_qops,
1365 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1367 V4L2_FIELD_INTERLACED,
1368 sizeof(struct saa7134_buf),
Hans Verkuil08bff032010-09-20 17:39:46 -03001369 fh, NULL);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03001370 videobuf_queue_sg_init(&fh->vbi, &saa7134_vbi_qops,
1371 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 V4L2_BUF_TYPE_VBI_CAPTURE,
1373 V4L2_FIELD_SEQ_TB,
1374 sizeof(struct saa7134_buf),
Hans Verkuil08bff032010-09-20 17:39:46 -03001375 fh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1377 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1378
1379 if (fh->radio) {
1380 /* switch to radio mode */
1381 saa7134_tvaudio_setinput(dev,&card(dev).radio);
Hans Verkuilfac69862009-01-17 12:17:14 -03001382 saa_call_all(dev, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 } else {
1384 /* switch to video/vbi mode */
1385 video_mux(dev,dev->ctl_input);
1386 }
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
1390static ssize_t
1391video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1392{
1393 struct saa7134_fh *fh = file->private_data;
1394
1395 switch (fh->type) {
1396 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1397 if (res_locked(fh->dev,RESOURCE_VIDEO))
1398 return -EBUSY;
1399 return videobuf_read_one(saa7134_queue(fh),
1400 data, count, ppos,
1401 file->f_flags & O_NONBLOCK);
1402 case V4L2_BUF_TYPE_VBI_CAPTURE:
1403 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1404 return -EBUSY;
1405 return videobuf_read_stream(saa7134_queue(fh),
1406 data, count, ppos, 1,
1407 file->f_flags & O_NONBLOCK);
1408 break;
1409 default:
1410 BUG();
1411 return 0;
1412 }
1413}
1414
1415static unsigned int
1416video_poll(struct file *file, struct poll_table_struct *wait)
1417{
1418 struct saa7134_fh *fh = file->private_data;
1419 struct videobuf_buffer *buf = NULL;
figo.zhang4a567392009-05-17 23:13:13 -03001420 unsigned int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1423 return videobuf_poll_stream(file, &fh->vbi, wait);
1424
1425 if (res_check(fh,RESOURCE_VIDEO)) {
figo.zhang4a567392009-05-17 23:13:13 -03001426 mutex_lock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (!list_empty(&fh->cap.stream))
1428 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1429 } else {
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001430 mutex_lock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (UNSET == fh->cap.read_off) {
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001432 /* need to capture a new frame */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001433 if (res_locked(fh->dev,RESOURCE_VIDEO))
1434 goto err;
1435 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field))
1436 goto err;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001437 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1438 fh->cap.read_off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 buf = fh->cap.read_buf;
1441 }
1442
1443 if (!buf)
figo.zhang4a567392009-05-17 23:13:13 -03001444 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 poll_wait(file, &buf->done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03001447 if (buf->state == VIDEOBUF_DONE ||
1448 buf->state == VIDEOBUF_ERROR)
figo.zhang4a567392009-05-17 23:13:13 -03001449 rc = POLLIN|POLLRDNORM;
1450 mutex_unlock(&fh->cap.vb_lock);
1451 return rc;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001452
1453err:
1454 mutex_unlock(&fh->cap.vb_lock);
1455 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Hans Verkuilbec43662008-12-30 06:58:20 -03001458static int video_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 struct saa7134_fh *fh = file->private_data;
1461 struct saa7134_dev *dev = fh->dev;
Hans Verkuil2983baf2009-03-29 06:26:27 -03001462 struct rds_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 unsigned long flags;
1464
1465 /* turn off overlay */
1466 if (res_check(fh, RESOURCE_OVERLAY)) {
1467 spin_lock_irqsave(&dev->slock,flags);
1468 stop_preview(dev,fh);
1469 spin_unlock_irqrestore(&dev->slock,flags);
1470 res_free(dev,fh,RESOURCE_OVERLAY);
1471 }
1472
1473 /* stop video capture */
1474 if (res_check(fh, RESOURCE_VIDEO)) {
1475 videobuf_streamoff(&fh->cap);
1476 res_free(dev,fh,RESOURCE_VIDEO);
1477 }
1478 if (fh->cap.read_buf) {
1479 buffer_release(&fh->cap,fh->cap.read_buf);
1480 kfree(fh->cap.read_buf);
1481 }
1482
1483 /* stop vbi capture */
1484 if (res_check(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03001485 videobuf_stop(&fh->vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 res_free(dev,fh,RESOURCE_VBI);
1487 }
1488
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001489 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1490 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1491 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1492 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1493 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1494
Laurent Pinchart622b8282009-10-05 10:48:17 -03001495 saa_call_all(dev, core, s_power, 0);
Hans Verkuil2983baf2009-03-29 06:26:27 -03001496 if (fh->radio)
1497 saa_call_all(dev, core, ioctl, RDS_CMD_CLOSE, &cmd);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -07001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /* free stuff */
1500 videobuf_mmap_free(&fh->cap);
1501 videobuf_mmap_free(&fh->vbi);
1502 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1503 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1504
Hans Verkuilffb48772010-05-01 08:03:24 -03001505 v4l2_prio_close(&dev->prio, fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 file->private_data = NULL;
1507 kfree(fh);
1508 return 0;
1509}
1510
Heikki Orsila9040b322007-04-27 12:31:18 -03001511static int video_mmap(struct file *file, struct vm_area_struct * vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 struct saa7134_fh *fh = file->private_data;
1514
1515 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1516}
1517
Hans Verkuil2983baf2009-03-29 06:26:27 -03001518static ssize_t radio_read(struct file *file, char __user *data,
1519 size_t count, loff_t *ppos)
1520{
1521 struct saa7134_fh *fh = file->private_data;
1522 struct saa7134_dev *dev = fh->dev;
1523 struct rds_command cmd;
1524
1525 cmd.block_count = count/3;
1526 cmd.buffer = data;
1527 cmd.instance = file;
1528 cmd.result = -ENODEV;
1529
1530 saa_call_all(dev, core, ioctl, RDS_CMD_READ, &cmd);
1531
1532 return cmd.result;
1533}
1534
1535static unsigned int radio_poll(struct file *file, poll_table *wait)
1536{
1537 struct saa7134_fh *fh = file->private_data;
1538 struct saa7134_dev *dev = fh->dev;
1539 struct rds_command cmd;
1540
1541 cmd.instance = file;
1542 cmd.event_list = wait;
1543 cmd.result = -ENODEV;
1544 saa_call_all(dev, core, ioctl, RDS_CMD_POLL, &cmd);
1545
1546 return cmd.result;
1547}
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549/* ------------------------------------------------------------------ */
1550
Hans Verkuil78b526a2008-05-28 12:16:41 -03001551static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001552 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001554 struct saa7134_fh *fh = priv;
1555 struct saa7134_dev *dev = fh->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct saa7134_tvnorm *norm = dev->tvnorm;
1557
1558 f->fmt.vbi.sampling_rate = 6750000 * 4;
1559 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1560 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1561 f->fmt.vbi.offset = 64 * 4;
Michael Schimekf246a812005-06-23 22:04:47 -07001562 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1563 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1564 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1566 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1567
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Hans Verkuil78b526a2008-05-28 12:16:41 -03001571static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001572 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001574 struct saa7134_fh *fh = priv;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001575
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001576 f->fmt.pix.width = fh->width;
1577 f->fmt.pix.height = fh->height;
1578 f->fmt.pix.field = fh->cap.field;
1579 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1580 f->fmt.pix.bytesperline =
1581 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1582 f->fmt.pix.sizeimage =
1583 f->fmt.pix.height * f->fmt.pix.bytesperline;
1584 return 0;
1585}
1586
Hans Verkuil78b526a2008-05-28 12:16:41 -03001587static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001588 struct v4l2_format *f)
1589{
1590 struct saa7134_fh *fh = priv;
1591
1592 if (saa7134_no_overlay > 0) {
1593 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return -EINVAL;
1595 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001596 f->fmt.win = fh->win;
1597
1598 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
Hans Verkuil78b526a2008-05-28 12:16:41 -03001601static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001602 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001604 struct saa7134_fh *fh = priv;
1605 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001606 struct saa7134_format *fmt;
1607 enum v4l2_field field;
1608 unsigned int maxw, maxh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001610 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1611 if (NULL == fmt)
1612 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001614 field = f->fmt.pix.field;
1615 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1616 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001618 if (V4L2_FIELD_ANY == field) {
1619 field = (f->fmt.pix.height > maxh/2)
1620 ? V4L2_FIELD_INTERLACED
1621 : V4L2_FIELD_BOTTOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001623 switch (field) {
1624 case V4L2_FIELD_TOP:
1625 case V4L2_FIELD_BOTTOM:
1626 maxh = maxh / 2;
1627 break;
1628 case V4L2_FIELD_INTERLACED:
1629 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 default:
1631 return -EINVAL;
1632 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001633
1634 f->fmt.pix.field = field;
Herton Ronaldo Krzesinski0a062032010-03-19 14:58:23 -03001635 if (f->fmt.pix.width < 48)
1636 f->fmt.pix.width = 48;
1637 if (f->fmt.pix.height < 32)
1638 f->fmt.pix.height = 32;
1639 if (f->fmt.pix.width > maxw)
1640 f->fmt.pix.width = maxw;
1641 if (f->fmt.pix.height > maxh)
1642 f->fmt.pix.height = maxh;
1643 f->fmt.pix.width &= ~0x03;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001644 f->fmt.pix.bytesperline =
1645 (f->fmt.pix.width * fmt->depth) >> 3;
1646 f->fmt.pix.sizeimage =
1647 f->fmt.pix.height * f->fmt.pix.bytesperline;
1648
1649 return 0;
1650}
1651
Hans Verkuil78b526a2008-05-28 12:16:41 -03001652static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001653 struct v4l2_format *f)
1654{
1655 struct saa7134_fh *fh = priv;
1656 struct saa7134_dev *dev = fh->dev;
1657
1658 if (saa7134_no_overlay > 0) {
1659 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1660 return -EINVAL;
1661 }
1662
1663 return verify_preview(dev, &f->fmt.win);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
Hans Verkuil78b526a2008-05-28 12:16:41 -03001666static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001667 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001669 struct saa7134_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 int err;
1671
Hans Verkuil78b526a2008-05-28 12:16:41 -03001672 err = saa7134_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001673 if (0 != err)
1674 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001676 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1677 fh->width = f->fmt.pix.width;
1678 fh->height = f->fmt.pix.height;
1679 fh->cap.field = f->fmt.pix.field;
1680 return 0;
1681}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Hans Verkuil78b526a2008-05-28 12:16:41 -03001683static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001684 struct v4l2_format *f)
1685{
1686 struct saa7134_fh *fh = priv;
1687 struct saa7134_dev *dev = fh->dev;
1688 int err;
Steven Rostedta8b1ecf2008-05-06 20:42:29 -07001689 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001691 if (saa7134_no_overlay > 0) {
1692 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return -EINVAL;
1694 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001695 err = verify_preview(dev, &f->fmt.win);
1696 if (0 != err)
1697 return err;
1698
1699 mutex_lock(&dev->lock);
1700
1701 fh->win = f->fmt.win;
1702 fh->nclips = f->fmt.win.clipcount;
1703
1704 if (fh->nclips > 8)
1705 fh->nclips = 8;
1706
1707 if (copy_from_user(fh->clips, f->fmt.win.clips,
1708 sizeof(struct v4l2_clip)*fh->nclips)) {
1709 mutex_unlock(&dev->lock);
1710 return -EFAULT;
1711 }
1712
1713 if (res_check(fh, RESOURCE_OVERLAY)) {
1714 spin_lock_irqsave(&dev->slock, flags);
1715 stop_preview(dev, fh);
1716 start_preview(dev, fh);
1717 spin_unlock_irqrestore(&dev->slock, flags);
1718 }
1719
1720 mutex_unlock(&dev->lock);
1721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -03001724int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001725{
1726 const struct v4l2_queryctrl *ctrl;
1727
1728 if ((c->id < V4L2_CID_BASE ||
1729 c->id >= V4L2_CID_LASTP1) &&
1730 (c->id < V4L2_CID_PRIVATE_BASE ||
1731 c->id >= V4L2_CID_PRIVATE_LASTP1))
1732 return -EINVAL;
1733 ctrl = ctrl_by_id(c->id);
1734 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1735 return 0;
1736}
Mauro Carvalho Chehabf6e90a62007-12-11 15:01:57 -03001737EXPORT_SYMBOL_GPL(saa7134_queryctrl);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001738
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001739static int saa7134_enum_input(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001740 struct v4l2_input *i)
1741{
1742 struct saa7134_fh *fh = priv;
1743 struct saa7134_dev *dev = fh->dev;
1744 unsigned int n;
1745
1746 n = i->index;
1747 if (n >= SAA7134_INPUT_MAX)
1748 return -EINVAL;
1749 if (NULL == card_in(dev, i->index).name)
1750 return -EINVAL;
1751 memset(i, 0, sizeof(*i));
1752 i->index = n;
1753 i->type = V4L2_INPUT_TYPE_CAMERA;
1754 strcpy(i->name, card_in(dev, n).name);
1755 if (card_in(dev, n).tv)
1756 i->type = V4L2_INPUT_TYPE_TUNER;
1757 i->audioset = 1;
1758 if (n == dev->ctl_input) {
1759 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1760 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1761
1762 if (0 != (v1 & 0x40))
1763 i->status |= V4L2_IN_ST_NO_H_LOCK;
1764 if (0 != (v2 & 0x40))
1765 i->status |= V4L2_IN_ST_NO_SYNC;
1766 if (0 != (v2 & 0x0e))
1767 i->status |= V4L2_IN_ST_MACROVISION;
1768 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001769 i->std = SAA7134_NORMS;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001770 return 0;
1771}
1772
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001773static int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001774{
1775 struct saa7134_fh *fh = priv;
1776 struct saa7134_dev *dev = fh->dev;
1777
1778 *i = dev->ctl_input;
1779 return 0;
1780}
1781
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001782static int saa7134_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001783{
1784 struct saa7134_fh *fh = priv;
1785 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001786 int err;
1787
Hans Verkuilffb48772010-05-01 08:03:24 -03001788 err = v4l2_prio_check(&dev->prio, fh->prio);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001789 if (0 != err)
1790 return err;
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001791
Roel Kluinde81c3c2009-07-02 16:09:25 -03001792 if (i >= SAA7134_INPUT_MAX)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001793 return -EINVAL;
1794 if (NULL == card_in(dev, i).name)
1795 return -EINVAL;
1796 mutex_lock(&dev->lock);
1797 video_mux(dev, i);
1798 mutex_unlock(&dev->lock);
1799 return 0;
1800}
1801
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001802static int saa7134_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001803 struct v4l2_capability *cap)
1804{
1805 struct saa7134_fh *fh = priv;
1806 struct saa7134_dev *dev = fh->dev;
1807
1808 unsigned int tuner_type = dev->tuner_type;
1809
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001810 strcpy(cap->driver, "saa7134");
1811 strlcpy(cap->card, saa7134_boards[dev->board].name,
1812 sizeof(cap->card));
1813 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1814 cap->version = SAA7134_VERSION_CODE;
1815 cap->capabilities =
1816 V4L2_CAP_VIDEO_CAPTURE |
1817 V4L2_CAP_VBI_CAPTURE |
1818 V4L2_CAP_READWRITE |
1819 V4L2_CAP_STREAMING |
1820 V4L2_CAP_TUNER;
Hans Verkuilf101a2a2009-06-20 06:09:52 -03001821 if (dev->has_rds)
1822 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001823 if (saa7134_no_overlay <= 0)
1824 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1825
1826 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1827 cap->capabilities &= ~V4L2_CAP_TUNER;
Julia Lawall473d8022010-08-05 17:22:44 -03001828 return 0;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001829}
1830
Hans Verkuil9afb7372008-09-06 06:34:44 -03001831int saa7134_s_std_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, v4l2_std_id *id)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001832{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 unsigned long flags;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001834 unsigned int i;
1835 v4l2_std_id fixup;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001836 int err;
1837
Hans Verkuil9afb7372008-09-06 06:34:44 -03001838 /* When called from the empress code fh == NULL.
1839 That needs to be fixed somehow, but for now this is
1840 good enough. */
1841 if (fh) {
Hans Verkuilffb48772010-05-01 08:03:24 -03001842 err = v4l2_prio_check(&dev->prio, fh->prio);
Hans Verkuil9afb7372008-09-06 06:34:44 -03001843 if (0 != err)
1844 return err;
1845 } else if (res_locked(dev, RESOURCE_OVERLAY)) {
1846 /* Don't change the std from the mpeg device
1847 if overlay is active. */
1848 return -EBUSY;
1849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001851 for (i = 0; i < TVNORMS; i++)
1852 if (*id == tvnorms[i].id)
1853 break;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001854
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001855 if (i == TVNORMS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 for (i = 0; i < TVNORMS; i++)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001857 if (*id & tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 break;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001859 if (i == TVNORMS)
1860 return -EINVAL;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001861
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001862 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1863 if (secam[0] == 'L' || secam[0] == 'l') {
1864 if (secam[1] == 'C' || secam[1] == 'c')
1865 fixup = V4L2_STD_SECAM_LC;
1866 else
1867 fixup = V4L2_STD_SECAM_L;
1868 } else {
1869 if (secam[0] == 'D' || secam[0] == 'd')
1870 fixup = V4L2_STD_SECAM_DK;
1871 else
1872 fixup = V4L2_STD_SECAM;
Hartmut Hackmann17b10a72006-10-02 19:55:07 -03001873 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001874 for (i = 0; i < TVNORMS; i++)
1875 if (fixup == tvnorms[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001878
1879 *id = tvnorms[i].id;
1880
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001881 mutex_lock(&dev->lock);
Hans Verkuil9afb7372008-09-06 06:34:44 -03001882 if (fh && res_check(fh, RESOURCE_OVERLAY)) {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001883 spin_lock_irqsave(&dev->slock, flags);
1884 stop_preview(dev, fh);
1885 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001887 set_tvnorm(dev, &tvnorms[i]);
1888
1889 spin_lock_irqsave(&dev->slock, flags);
1890 start_preview(dev, fh);
1891 spin_unlock_irqrestore(&dev->slock, flags);
1892 } else
1893 set_tvnorm(dev, &tvnorms[i]);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03001894
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001895 saa7134_tvaudio_do_scan(dev);
1896 mutex_unlock(&dev->lock);
1897 return 0;
1898}
Hans Verkuil9afb7372008-09-06 06:34:44 -03001899EXPORT_SYMBOL_GPL(saa7134_s_std_internal);
1900
1901static int saa7134_s_std(struct file *file, void *priv, v4l2_std_id *id)
1902{
1903 struct saa7134_fh *fh = priv;
1904
1905 return saa7134_s_std_internal(fh->dev, fh, id);
1906}
1907
1908static int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1909{
1910 struct saa7134_fh *fh = priv;
1911 struct saa7134_dev *dev = fh->dev;
1912
1913 *id = dev->tvnorm->id;
1914 return 0;
1915}
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001916
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001917static int saa7134_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001918 struct v4l2_cropcap *cap)
1919{
1920 struct saa7134_fh *fh = priv;
1921 struct saa7134_dev *dev = fh->dev;
1922
1923 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1924 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1925 return -EINVAL;
1926 cap->bounds = dev->crop_bounds;
1927 cap->defrect = dev->crop_defrect;
1928 cap->pixelaspect.numerator = 1;
1929 cap->pixelaspect.denominator = 1;
1930 if (dev->tvnorm->id & V4L2_STD_525_60) {
1931 cap->pixelaspect.numerator = 11;
1932 cap->pixelaspect.denominator = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001934 if (dev->tvnorm->id & V4L2_STD_625_50) {
1935 cap->pixelaspect.numerator = 54;
1936 cap->pixelaspect.denominator = 59;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938 return 0;
1939}
1940
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001941static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001942{
1943 struct saa7134_fh *fh = f;
1944 struct saa7134_dev *dev = fh->dev;
1945
1946 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1947 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1948 return -EINVAL;
1949 crop->c = dev->crop_current;
1950 return 0;
1951}
1952
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001953static int saa7134_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03001954{
1955 struct saa7134_fh *fh = f;
1956 struct saa7134_dev *dev = fh->dev;
1957 struct v4l2_rect *b = &dev->crop_bounds;
1958
1959 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1960 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1961 return -EINVAL;
1962 if (crop->c.height < 0)
1963 return -EINVAL;
1964 if (crop->c.width < 0)
1965 return -EINVAL;
1966
1967 if (res_locked(fh->dev, RESOURCE_OVERLAY))
1968 return -EBUSY;
1969 if (res_locked(fh->dev, RESOURCE_VIDEO))
1970 return -EBUSY;
1971
1972 if (crop->c.top < b->top)
1973 crop->c.top = b->top;
1974 if (crop->c.top > b->top + b->height)
1975 crop->c.top = b->top + b->height;
1976 if (crop->c.height > b->top - crop->c.top + b->height)
1977 crop->c.height = b->top - crop->c.top + b->height;
1978
1979 if (crop->c.left < b->left)
1980 crop->c.left = b->left;
1981 if (crop->c.left > b->left + b->width)
1982 crop->c.left = b->left + b->width;
1983 if (crop->c.width > b->left - crop->c.left + b->width)
1984 crop->c.width = b->left - crop->c.left + b->width;
1985
1986 dev->crop_current = crop->c;
1987 return 0;
1988}
1989
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03001990static int saa7134_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001991 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03001993 struct saa7134_fh *fh = priv;
1994 struct saa7134_dev *dev = fh->dev;
1995 int n;
1996
1997 if (0 != t->index)
1998 return -EINVAL;
1999 memset(t, 0, sizeof(*t));
2000 for (n = 0; n < SAA7134_INPUT_MAX; n++)
2001 if (card_in(dev, n).tv)
2002 break;
2003 if (NULL != card_in(dev, n).name) {
2004 strcpy(t->name, "Television");
2005 t->type = V4L2_TUNER_ANALOG_TV;
2006 t->capability = V4L2_TUNER_CAP_NORM |
2007 V4L2_TUNER_CAP_STEREO |
2008 V4L2_TUNER_CAP_LANG1 |
2009 V4L2_TUNER_CAP_LANG2;
2010 t->rangehigh = 0xffffffffUL;
2011 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
2012 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
2013 }
2014 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
2015 t->signal = 0xffff;
2016 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002019static int saa7134_s_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002020 struct v4l2_tuner *t)
2021{
2022 struct saa7134_fh *fh = priv;
2023 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002024 int rx, mode, err;
2025
Hans Verkuilffb48772010-05-01 08:03:24 -03002026 err = v4l2_prio_check(&dev->prio, fh->prio);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002027 if (0 != err)
2028 return err;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002029
2030 mode = dev->thread.mode;
2031 if (UNSET == mode) {
2032 rx = saa7134_tvaudio_getstereo(dev);
2033 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
2034 }
2035 if (mode != t->audmode)
2036 dev->thread.mode = t->audmode;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002037
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002038 return 0;
2039}
2040
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002041static int saa7134_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002042 struct v4l2_frequency *f)
2043{
2044 struct saa7134_fh *fh = priv;
2045 struct saa7134_dev *dev = fh->dev;
2046
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002047 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2048 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002049
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002050 return 0;
2051}
2052
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002053static int saa7134_s_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002054 struct v4l2_frequency *f)
2055{
2056 struct saa7134_fh *fh = priv;
2057 struct saa7134_dev *dev = fh->dev;
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002058 int err;
2059
Hans Verkuilffb48772010-05-01 08:03:24 -03002060 err = v4l2_prio_check(&dev->prio, fh->prio);
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002061 if (0 != err)
2062 return err;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002063
2064 if (0 != f->tuner)
2065 return -EINVAL;
2066 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
2067 return -EINVAL;
2068 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
2069 return -EINVAL;
2070 mutex_lock(&dev->lock);
2071 dev->ctl_freq = f->frequency;
2072
Hans Verkuilfac69862009-01-17 12:17:14 -03002073 saa_call_all(dev, tuner, s_frequency, f);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002074
2075 saa7134_tvaudio_do_scan(dev);
2076 mutex_unlock(&dev->lock);
2077 return 0;
2078}
2079
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002080static int saa7134_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002081{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002082 strcpy(a->name, "audio");
2083 return 0;
2084}
2085
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002086static int saa7134_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002087{
2088 return 0;
2089}
2090
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002091static int saa7134_g_priority(struct file *file, void *f, enum v4l2_priority *p)
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002092{
2093 struct saa7134_fh *fh = f;
2094 struct saa7134_dev *dev = fh->dev;
2095
2096 *p = v4l2_prio_max(&dev->prio);
2097 return 0;
2098}
2099
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002100static int saa7134_s_priority(struct file *file, void *f,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002101 enum v4l2_priority prio)
2102{
2103 struct saa7134_fh *fh = f;
2104 struct saa7134_dev *dev = fh->dev;
2105
2106 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
2107}
2108
Hans Verkuil78b526a2008-05-28 12:16:41 -03002109static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002110 struct v4l2_fmtdesc *f)
2111{
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002112 if (f->index >= FORMATS)
2113 return -EINVAL;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002114
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002115 strlcpy(f->description, formats[f->index].name,
2116 sizeof(f->description));
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002117
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002118 f->pixelformat = formats[f->index].fourcc;
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002119
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002120 return 0;
2121}
2122
Hans Verkuil78b526a2008-05-28 12:16:41 -03002123static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002124 struct v4l2_fmtdesc *f)
2125{
2126 if (saa7134_no_overlay > 0) {
2127 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002128 return -EINVAL;
2129 }
Mauro Carvalho Chehab8c854542007-12-07 17:34:48 -03002130
2131 if ((f->index >= FORMATS) || formats[f->index].planar)
2132 return -EINVAL;
2133
2134 strlcpy(f->description, formats[f->index].name,
2135 sizeof(f->description));
2136
2137 f->pixelformat = formats[f->index].fourcc;
2138
2139 return 0;
2140}
2141
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002142static int saa7134_g_fbuf(struct file *file, void *f,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002143 struct v4l2_framebuffer *fb)
2144{
2145 struct saa7134_fh *fh = f;
2146 struct saa7134_dev *dev = fh->dev;
2147
2148 *fb = dev->ovbuf;
2149 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2150
2151 return 0;
2152}
2153
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002154static int saa7134_s_fbuf(struct file *file, void *f,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002155 struct v4l2_framebuffer *fb)
2156{
2157 struct saa7134_fh *fh = f;
2158 struct saa7134_dev *dev = fh->dev;
2159 struct saa7134_format *fmt;
2160
2161 if (!capable(CAP_SYS_ADMIN) &&
2162 !capable(CAP_SYS_RAWIO))
2163 return -EPERM;
2164
2165 /* check args */
2166 fmt = format_by_fourcc(fb->fmt.pixelformat);
2167 if (NULL == fmt)
2168 return -EINVAL;
2169
2170 /* ok, accept it */
2171 dev->ovbuf = *fb;
2172 dev->ovfmt = fmt;
2173 if (0 == dev->ovbuf.fmt.bytesperline)
2174 dev->ovbuf.fmt.bytesperline =
2175 dev->ovbuf.fmt.width*fmt->depth/8;
2176 return 0;
2177}
2178
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002179static int saa7134_overlay(struct file *file, void *f, unsigned int on)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002180{
2181 struct saa7134_fh *fh = f;
2182 struct saa7134_dev *dev = fh->dev;
2183 unsigned long flags;
2184
2185 if (on) {
2186 if (saa7134_no_overlay > 0) {
2187 dprintk("no_overlay\n");
2188 return -EINVAL;
2189 }
2190
2191 if (!res_get(dev, fh, RESOURCE_OVERLAY))
2192 return -EBUSY;
2193 spin_lock_irqsave(&dev->slock, flags);
2194 start_preview(dev, fh);
2195 spin_unlock_irqrestore(&dev->slock, flags);
2196 }
2197 if (!on) {
2198 if (!res_check(fh, RESOURCE_OVERLAY))
2199 return -EINVAL;
2200 spin_lock_irqsave(&dev->slock, flags);
2201 stop_preview(dev, fh);
2202 spin_unlock_irqrestore(&dev->slock, flags);
2203 res_free(dev, fh, RESOURCE_OVERLAY);
2204 }
2205 return 0;
2206}
2207
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002208#ifdef CONFIG_VIDEO_V4L1_COMPAT
2209static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2210{
2211 struct saa7134_fh *fh = file->private_data;
2212 return videobuf_cgmbuf(saa7134_queue(fh), mbuf, 8);
2213}
2214#endif
2215
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002216static int saa7134_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002217 struct v4l2_requestbuffers *p)
2218{
2219 struct saa7134_fh *fh = priv;
2220 return videobuf_reqbufs(saa7134_queue(fh), p);
2221}
2222
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002223static int saa7134_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002224 struct v4l2_buffer *b)
2225{
2226 struct saa7134_fh *fh = priv;
2227 return videobuf_querybuf(saa7134_queue(fh), b);
2228}
2229
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002230static int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002231{
2232 struct saa7134_fh *fh = priv;
2233 return videobuf_qbuf(saa7134_queue(fh), b);
2234}
2235
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002236static int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002237{
2238 struct saa7134_fh *fh = priv;
2239 return videobuf_dqbuf(saa7134_queue(fh), b,
2240 file->f_flags & O_NONBLOCK);
2241}
2242
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002243static int saa7134_streamon(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002244 enum v4l2_buf_type type)
2245{
2246 struct saa7134_fh *fh = priv;
2247 struct saa7134_dev *dev = fh->dev;
2248 int res = saa7134_resource(fh);
2249
2250 if (!res_get(dev, fh, res))
2251 return -EBUSY;
2252
2253 return videobuf_streamon(saa7134_queue(fh));
2254}
2255
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002256static int saa7134_streamoff(struct file *file, void *priv,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002257 enum v4l2_buf_type type)
2258{
2259 int err;
2260 struct saa7134_fh *fh = priv;
2261 struct saa7134_dev *dev = fh->dev;
2262 int res = saa7134_resource(fh);
2263
2264 err = videobuf_streamoff(saa7134_queue(fh));
2265 if (err < 0)
2266 return err;
2267 res_free(dev, fh, res);
2268 return 0;
2269}
2270
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002271static int saa7134_g_parm(struct file *file, void *fh,
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002272 struct v4l2_streamparm *parm)
2273{
Mauro Carvalho Chehab90bdc142007-12-07 17:23:38 -03002274 return 0;
2275}
2276
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03002277#ifdef CONFIG_VIDEO_ADV_DEBUG
2278static int vidioc_g_register (struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002279 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03002280{
2281 struct saa7134_fh *fh = priv;
2282 struct saa7134_dev *dev = fh->dev;
2283
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002284 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03002285 return -EINVAL;
2286 reg->val = saa_readb(reg->reg);
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002287 reg->size = 1;
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03002288 return 0;
2289}
2290
2291static int vidioc_s_register (struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002292 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03002293{
2294 struct saa7134_fh *fh = priv;
2295 struct saa7134_dev *dev = fh->dev;
2296
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002297 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03002298 return -EINVAL;
2299 saa_writeb(reg->reg&0xffffff, reg->val);
2300 return 0;
2301}
2302#endif
2303
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002304static int radio_querycap(struct file *file, void *priv,
2305 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
2307 struct saa7134_fh *fh = file->private_data;
2308 struct saa7134_dev *dev = fh->dev;
2309
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002310 strcpy(cap->driver, "saa7134");
2311 strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
2312 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
2313 cap->version = SAA7134_VERSION_CODE;
2314 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return 0;
2316}
2317
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002318static int radio_g_tuner(struct file *file, void *priv,
2319 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002321 struct saa7134_fh *fh = file->private_data;
2322 struct saa7134_dev *dev = fh->dev;
2323
2324 if (0 != t->index)
2325 return -EINVAL;
2326
2327 memset(t, 0, sizeof(*t));
2328 strcpy(t->name, "Radio");
2329 t->type = V4L2_TUNER_RADIO;
2330
Hans Verkuilfac69862009-01-17 12:17:14 -03002331 saa_call_all(dev, tuner, g_tuner, t);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002332 if (dev->input->amux == TV) {
2333 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2334 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2335 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2336 }
2337 return 0;
2338}
2339static int radio_s_tuner(struct file *file, void *priv,
2340 struct v4l2_tuner *t)
2341{
2342 struct saa7134_fh *fh = file->private_data;
2343 struct saa7134_dev *dev = fh->dev;
2344
2345 if (0 != t->index)
2346 return -EINVAL;
2347
Hans Verkuilfac69862009-01-17 12:17:14 -03002348 saa_call_all(dev, tuner, s_tuner, t);
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002349 return 0;
2350}
2351
2352static int radio_enum_input(struct file *file, void *priv,
2353 struct v4l2_input *i)
2354{
2355 if (i->index != 0)
2356 return -EINVAL;
2357
2358 strcpy(i->name, "Radio");
2359 i->type = V4L2_INPUT_TYPE_TUNER;
2360
2361 return 0;
2362}
2363
2364static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2365{
2366 *i = 0;
2367 return 0;
2368}
2369
2370static int radio_g_audio(struct file *file, void *priv,
2371 struct v4l2_audio *a)
2372{
2373 memset(a, 0, sizeof(*a));
2374 strcpy(a->name, "Radio");
2375 return 0;
2376}
2377
2378static int radio_s_audio(struct file *file, void *priv,
2379 struct v4l2_audio *a)
2380{
2381 return 0;
2382}
2383
2384static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2385{
2386 return 0;
2387}
2388
2389static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
2390{
2391 return 0;
2392}
2393
2394static int radio_queryctrl(struct file *file, void *priv,
2395 struct v4l2_queryctrl *c)
2396{
2397 const struct v4l2_queryctrl *ctrl;
2398
2399 if (c->id < V4L2_CID_BASE ||
2400 c->id >= V4L2_CID_LASTP1)
2401 return -EINVAL;
2402 if (c->id == V4L2_CID_AUDIO_MUTE) {
2403 ctrl = ctrl_by_id(c->id);
2404 *c = *ctrl;
2405 } else
2406 *c = no_ctrl;
2407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
Hans Verkuilbec43662008-12-30 06:58:20 -03002410static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
2412 .owner = THIS_MODULE,
2413 .open = video_open,
2414 .release = video_release,
2415 .read = video_read,
2416 .poll = video_poll,
2417 .mmap = video_mmap,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002418 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419};
2420
Hans Verkuila3998102008-07-21 02:57:38 -03002421static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002422 .vidioc_querycap = saa7134_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002423 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
2424 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
2425 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
2426 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
2427 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
2428 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
2429 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
2430 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002431 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2432 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2433 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002434 .vidioc_g_audio = saa7134_g_audio,
2435 .vidioc_s_audio = saa7134_s_audio,
2436 .vidioc_cropcap = saa7134_cropcap,
2437 .vidioc_reqbufs = saa7134_reqbufs,
2438 .vidioc_querybuf = saa7134_querybuf,
2439 .vidioc_qbuf = saa7134_qbuf,
2440 .vidioc_dqbuf = saa7134_dqbuf,
2441 .vidioc_s_std = saa7134_s_std,
Hans Verkuil9afb7372008-09-06 06:34:44 -03002442 .vidioc_g_std = saa7134_g_std,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002443 .vidioc_enum_input = saa7134_enum_input,
2444 .vidioc_g_input = saa7134_g_input,
2445 .vidioc_s_input = saa7134_s_input,
2446 .vidioc_queryctrl = saa7134_queryctrl,
2447 .vidioc_g_ctrl = saa7134_g_ctrl,
2448 .vidioc_s_ctrl = saa7134_s_ctrl,
2449 .vidioc_streamon = saa7134_streamon,
2450 .vidioc_streamoff = saa7134_streamoff,
2451 .vidioc_g_tuner = saa7134_g_tuner,
2452 .vidioc_s_tuner = saa7134_s_tuner,
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002453#ifdef CONFIG_VIDEO_V4L1_COMPAT
2454 .vidiocgmbuf = vidiocgmbuf,
2455#endif
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002456 .vidioc_g_crop = saa7134_g_crop,
2457 .vidioc_s_crop = saa7134_s_crop,
2458 .vidioc_g_fbuf = saa7134_g_fbuf,
2459 .vidioc_s_fbuf = saa7134_s_fbuf,
2460 .vidioc_overlay = saa7134_overlay,
2461 .vidioc_g_priority = saa7134_g_priority,
2462 .vidioc_s_priority = saa7134_s_priority,
2463 .vidioc_g_parm = saa7134_g_parm,
2464 .vidioc_g_frequency = saa7134_g_frequency,
2465 .vidioc_s_frequency = saa7134_s_frequency,
Mauro Carvalho Chehabc121ba1f2008-06-16 12:31:57 -03002466#ifdef CONFIG_VIDEO_ADV_DEBUG
2467 .vidioc_g_register = vidioc_g_register,
2468 .vidioc_s_register = vidioc_s_register,
2469#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470};
2471
Hans Verkuilbec43662008-12-30 06:58:20 -03002472static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002473 .owner = THIS_MODULE,
2474 .open = video_open,
Hans Verkuil2983baf2009-03-29 06:26:27 -03002475 .read = radio_read,
Hans Verkuila3998102008-07-21 02:57:38 -03002476 .release = video_release,
2477 .ioctl = video_ioctl2,
Hans Verkuil2983baf2009-03-29 06:26:27 -03002478 .poll = radio_poll,
Hans Verkuila3998102008-07-21 02:57:38 -03002479};
2480
2481static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Douglas Schilling Landgraf8f61ae22007-12-07 17:09:53 -03002482 .vidioc_querycap = radio_querycap,
2483 .vidioc_g_tuner = radio_g_tuner,
2484 .vidioc_enum_input = radio_enum_input,
2485 .vidioc_g_audio = radio_g_audio,
2486 .vidioc_s_tuner = radio_s_tuner,
2487 .vidioc_s_audio = radio_s_audio,
2488 .vidioc_s_input = radio_s_input,
2489 .vidioc_s_std = radio_s_std,
2490 .vidioc_queryctrl = radio_queryctrl,
2491 .vidioc_g_input = radio_g_input,
Mauro Carvalho Chehabbfb12e32007-12-11 11:51:53 -03002492 .vidioc_g_ctrl = saa7134_g_ctrl,
2493 .vidioc_s_ctrl = saa7134_s_ctrl,
2494 .vidioc_g_frequency = saa7134_g_frequency,
2495 .vidioc_s_frequency = saa7134_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496};
2497
Hans Verkuila3998102008-07-21 02:57:38 -03002498/* ----------------------------------------------------------- */
2499/* exported stuff */
2500
2501struct video_device saa7134_video_template = {
2502 .name = "saa7134-video",
Hans Verkuila3998102008-07-21 02:57:38 -03002503 .fops = &video_fops,
2504 .ioctl_ops = &video_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002505 .tvnorms = SAA7134_NORMS,
2506 .current_norm = V4L2_STD_PAL,
2507};
2508
2509struct video_device saa7134_radio_template = {
2510 .name = "saa7134-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002511 .fops = &radio_fops,
2512 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002513};
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515int saa7134_video_init1(struct saa7134_dev *dev)
2516{
2517 /* sanitycheck insmod options */
2518 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2519 gbuffers = 2;
2520 if (gbufsize < 0 || gbufsize > gbufsize_max)
2521 gbufsize = gbufsize_max;
2522 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2523
2524 /* put some sensible defaults into the data structures ... */
2525 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2526 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2527 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2528 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2529 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2530 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2531 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2532 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2533
2534 if (dev->tda9887_conf && dev->ctl_automute)
2535 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2536 dev->automute = 0;
2537
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002538 INIT_LIST_HEAD(&dev->video_q.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 init_timer(&dev->video_q.timeout);
2540 dev->video_q.timeout.function = saa7134_buffer_timeout;
2541 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2542 dev->video_q.dev = dev;
2543
Maxim Levitskycb712012007-09-27 20:34:25 -03002544 if (saa7134_boards[dev->board].video_out)
2545 saa7134_videoport_init(dev);
2546
2547 return 0;
2548}
2549
2550int saa7134_videoport_init(struct saa7134_dev *dev)
2551{
2552 /* enable video output */
2553 int vo = saa7134_boards[dev->board].video_out;
2554 int video_reg;
2555 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
Dmitry Belimov37df96d2008-06-10 12:43:58 -03002556
2557 /* Configure videoport */
Maxim Levitskycb712012007-09-27 20:34:25 -03002558 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2559 video_reg = video_out[vo][1];
2560 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2561 video_reg &= ~VP_T_CODE_P_INVERTED;
2562 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2563 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
Maxim Levitskycb712012007-09-27 20:34:25 -03002564 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2565 video_reg = video_out[vo][5];
2566 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2567 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2568 if (vid_port_opts & SET_CLOCK_INVERTED)
2569 video_reg |= VP_CLK_CTRL1_INVERTED;
2570 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2571 video_reg = video_out[vo][6];
2572 if (vid_port_opts & SET_VSYNC_OFF) {
2573 video_reg &= ~VP_VS_TYPE_MASK;
2574 video_reg |= VP_VS_TYPE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
Maxim Levitskycb712012007-09-27 20:34:25 -03002576 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2577 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2578 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Dmitry Belimov37df96d2008-06-10 12:43:58 -03002580 /* Start videoport */
2581 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return 0;
2584}
2585
2586int saa7134_video_init2(struct saa7134_dev *dev)
2587{
2588 /* init video hw */
2589 set_tvnorm(dev,&tvnorms[0]);
2590 video_mux(dev,0);
2591 saa7134_tvaudio_setmute(dev);
2592 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2593 return 0;
2594}
2595
Maxim Levitsky11f70782007-09-27 20:44:39 -03002596void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
2598 static const char *st[] = {
2599 "(no signal)", "NTSC", "PAL", "SECAM" };
2600 u32 st1,st2;
2601
2602 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2603 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2604 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2605 (st1 & 0x40) ? "not locked" : "locked",
2606 (st2 & 0x40) ? "no" : "yes",
2607 st[st1 & 0x03]);
Maxim Levitsky11f70782007-09-27 20:44:39 -03002608 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 if (dev->nosignal) {
2611 /* no video signal -> mute audio */
2612 if (dev->ctl_automute)
2613 dev->automute = 1;
2614 saa7134_tvaudio_setmute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 } else {
2616 /* wake up tvaudio audio carrier scan thread */
2617 saa7134_tvaudio_do_scan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 }
Maxim Levitsky11f70782007-09-27 20:44:39 -03002619
2620 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2621 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2622 else
2623 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (dev->mops && dev->mops->signal_change)
2626 dev->mops->signal_change(dev);
2627}
2628
Maxim Levitsky11f70782007-09-27 20:44:39 -03002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2631{
2632 enum v4l2_field field;
2633
2634 spin_lock(&dev->slock);
2635 if (dev->video_q.curr) {
2636 dev->video_fieldcount++;
2637 field = dev->video_q.curr->vb.field;
2638 if (V4L2_FIELD_HAS_BOTH(field)) {
2639 /* make sure we have seen both fields */
2640 if ((status & 0x10) == 0x00) {
2641 dev->video_q.curr->top_seen = 1;
2642 goto done;
2643 }
2644 if (!dev->video_q.curr->top_seen)
2645 goto done;
2646 } else if (field == V4L2_FIELD_TOP) {
2647 if ((status & 0x10) != 0x10)
2648 goto done;
2649 } else if (field == V4L2_FIELD_BOTTOM) {
2650 if ((status & 0x10) != 0x00)
2651 goto done;
2652 }
2653 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
Brandon Philips0fc06862007-11-06 20:02:36 -03002654 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
2656 saa7134_buffer_next(dev,&dev->video_q);
2657
2658 done:
2659 spin_unlock(&dev->slock);
2660}
2661
2662/* ----------------------------------------------------------- */
2663/*
2664 * Local variables:
2665 * c-basic-offset: 8
2666 * End:
2667 */