Merge branch 'x86/urgent' into x86/pat
[deliverable/linux.git] / drivers / media / video / bt8xx / bttv-driver.c
1 /*
2
3 bttv - Bt848 frame grabber driver
4
5 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
6 & Marcus Metzler <mocm@thp.uni-koeln.de>
7 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
8
9 some v4l2 code lines are taken from Justin's bttv2 driver which is
10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
11
12 V4L1 removal from:
13 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
14
15 Fixes to be fully V4L2 compliant by
16 (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
17
18 Cropping and overscan support
19 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
20 Sponsored by OPQ Systems AB
21
22 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 */
36
37 #include <linux/init.h>
38 #include <linux/module.h>
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/fs.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/smp_lock.h>
45 #include <linux/interrupt.h>
46 #include <linux/kdev_t.h>
47 #include "bttvp.h"
48 #include <media/v4l2-common.h>
49 #include <media/v4l2-ioctl.h>
50 #include <media/tvaudio.h>
51 #include <media/msp3400.h>
52
53 #include <linux/dma-mapping.h>
54
55 #include <asm/io.h>
56 #include <asm/byteorder.h>
57
58 #include <media/rds.h>
59
60
61 unsigned int bttv_num; /* number of Bt848s in use */
62 struct bttv *bttvs[BTTV_MAX];
63
64 unsigned int bttv_debug;
65 unsigned int bttv_verbose = 1;
66 unsigned int bttv_gpio;
67
68 /* config variables */
69 #ifdef __BIG_ENDIAN
70 static unsigned int bigendian=1;
71 #else
72 static unsigned int bigendian;
73 #endif
74 static unsigned int radio[BTTV_MAX];
75 static unsigned int irq_debug;
76 static unsigned int gbuffers = 8;
77 static unsigned int gbufsize = 0x208000;
78 static unsigned int reset_crop = 1;
79
80 static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
81 static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
82 static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
83 static int debug_latency;
84
85 static unsigned int fdsr;
86
87 /* options */
88 static unsigned int combfilter;
89 static unsigned int lumafilter;
90 static unsigned int automute = 1;
91 static unsigned int chroma_agc;
92 static unsigned int adc_crush = 1;
93 static unsigned int whitecrush_upper = 0xCF;
94 static unsigned int whitecrush_lower = 0x7F;
95 static unsigned int vcr_hack;
96 static unsigned int irq_iswitch;
97 static unsigned int uv_ratio = 50;
98 static unsigned int full_luma_range;
99 static unsigned int coring;
100
101 /* API features (turn on/off stuff for testing) */
102 static unsigned int v4l2 = 1;
103
104 /* insmod args */
105 module_param(bttv_verbose, int, 0644);
106 module_param(bttv_gpio, int, 0644);
107 module_param(bttv_debug, int, 0644);
108 module_param(irq_debug, int, 0644);
109 module_param(debug_latency, int, 0644);
110
111 module_param(fdsr, int, 0444);
112 module_param(gbuffers, int, 0444);
113 module_param(gbufsize, int, 0444);
114 module_param(reset_crop, int, 0444);
115
116 module_param(v4l2, int, 0644);
117 module_param(bigendian, int, 0644);
118 module_param(irq_iswitch, int, 0644);
119 module_param(combfilter, int, 0444);
120 module_param(lumafilter, int, 0444);
121 module_param(automute, int, 0444);
122 module_param(chroma_agc, int, 0444);
123 module_param(adc_crush, int, 0444);
124 module_param(whitecrush_upper, int, 0444);
125 module_param(whitecrush_lower, int, 0444);
126 module_param(vcr_hack, int, 0444);
127 module_param(uv_ratio, int, 0444);
128 module_param(full_luma_range, int, 0444);
129 module_param(coring, int, 0444);
130
131 module_param_array(radio, int, NULL, 0444);
132 module_param_array(video_nr, int, NULL, 0444);
133 module_param_array(radio_nr, int, NULL, 0444);
134 module_param_array(vbi_nr, int, NULL, 0444);
135
136 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
137 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
138 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
139 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
140 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
141 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
142 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
143 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
144 MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
145 "is 1 (yes) for compatibility with older applications");
146 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
147 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
148 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
149 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
150 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
151 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
152 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
153 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
154 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
155 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
156 MODULE_PARM_DESC(video_nr, "video device numbers");
157 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
158 MODULE_PARM_DESC(radio_nr, "radio device numbers");
159
160 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
161 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
162 MODULE_LICENSE("GPL");
163
164 /* ----------------------------------------------------------------------- */
165 /* sysfs */
166
167 static ssize_t show_card(struct device *cd,
168 struct device_attribute *attr, char *buf)
169 {
170 struct video_device *vfd = container_of(cd, struct video_device, dev);
171 struct bttv *btv = video_get_drvdata(vfd);
172 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
173 }
174 static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
175
176 /* ----------------------------------------------------------------------- */
177 /* dvb auto-load setup */
178 #if defined(CONFIG_MODULES) && defined(MODULE)
179 static void request_module_async(struct work_struct *work)
180 {
181 request_module("dvb-bt8xx");
182 }
183
184 static void request_modules(struct bttv *dev)
185 {
186 INIT_WORK(&dev->request_module_wk, request_module_async);
187 schedule_work(&dev->request_module_wk);
188 }
189 #else
190 #define request_modules(dev)
191 #endif /* CONFIG_MODULES */
192
193
194 /* ----------------------------------------------------------------------- */
195 /* static data */
196
197 /* special timing tables from conexant... */
198 static u8 SRAM_Table[][60] =
199 {
200 /* PAL digital input over GPIO[7:0] */
201 {
202 45, // 45 bytes following
203 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
204 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
205 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
206 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
207 0x37,0x00,0xAF,0x21,0x00
208 },
209 /* NTSC digital input over GPIO[7:0] */
210 {
211 51, // 51 bytes following
212 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
213 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
214 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
215 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
216 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
217 0x00,
218 },
219 // TGB_NTSC392 // quartzsight
220 // This table has been modified to be used for Fusion Rev D
221 {
222 0x2A, // size of table = 42
223 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
224 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
225 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
226 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
227 0x20, 0x00
228 }
229 };
230
231 /* minhdelayx1 first video pixel we can capture on a line and
232 hdelayx1 start of active video, both relative to rising edge of
233 /HRESET pulse (0H) in 1 / fCLKx1.
234 swidth width of active video and
235 totalwidth total line width, both in 1 / fCLKx1.
236 sqwidth total line width in square pixels.
237 vdelay start of active video in 2 * field lines relative to
238 trailing edge of /VRESET pulse (VDELAY register).
239 sheight height of active video in 2 * field lines.
240 videostart0 ITU-R frame line number of the line corresponding
241 to vdelay in the first field. */
242 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
243 vdelay, sheight, videostart0) \
244 .cropcap.bounds.left = minhdelayx1, \
245 /* * 2 because vertically we count field lines times two, */ \
246 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
247 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
248 /* 4 is a safety margin at the end of the line. */ \
249 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
250 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
251 .cropcap.defrect.left = hdelayx1, \
252 .cropcap.defrect.top = (videostart0) * 2, \
253 .cropcap.defrect.width = swidth, \
254 .cropcap.defrect.height = sheight, \
255 .cropcap.pixelaspect.numerator = totalwidth, \
256 .cropcap.pixelaspect.denominator = sqwidth,
257
258 const struct bttv_tvnorm bttv_tvnorms[] = {
259 /* PAL-BDGHI */
260 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
261 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
262 {
263 .v4l2_id = V4L2_STD_PAL,
264 .name = "PAL",
265 .Fsc = 35468950,
266 .swidth = 924,
267 .sheight = 576,
268 .totalwidth = 1135,
269 .adelay = 0x7f,
270 .bdelay = 0x72,
271 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
272 .scaledtwidth = 1135,
273 .hdelayx1 = 186,
274 .hactivex1 = 924,
275 .vdelay = 0x20,
276 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
277 .sram = 0,
278 /* ITU-R frame line number of the first VBI line
279 we can capture, of the first and second field.
280 The last line is determined by cropcap.bounds. */
281 .vbistart = { 7, 320 },
282 CROPCAP(/* minhdelayx1 */ 68,
283 /* hdelayx1 */ 186,
284 /* Should be (768 * 1135 + 944 / 2) / 944.
285 cropcap.defrect is used for image width
286 checks, so we keep the old value 924. */
287 /* swidth */ 924,
288 /* totalwidth */ 1135,
289 /* sqwidth */ 944,
290 /* vdelay */ 0x20,
291 /* sheight */ 576,
292 /* videostart0 */ 23)
293 /* bt878 (and bt848?) can capture another
294 line below active video. */
295 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
296 },{
297 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
298 .name = "NTSC",
299 .Fsc = 28636363,
300 .swidth = 768,
301 .sheight = 480,
302 .totalwidth = 910,
303 .adelay = 0x68,
304 .bdelay = 0x5d,
305 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
306 .scaledtwidth = 910,
307 .hdelayx1 = 128,
308 .hactivex1 = 910,
309 .vdelay = 0x1a,
310 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
311 .sram = 1,
312 .vbistart = { 10, 273 },
313 CROPCAP(/* minhdelayx1 */ 68,
314 /* hdelayx1 */ 128,
315 /* Should be (640 * 910 + 780 / 2) / 780? */
316 /* swidth */ 768,
317 /* totalwidth */ 910,
318 /* sqwidth */ 780,
319 /* vdelay */ 0x1a,
320 /* sheight */ 480,
321 /* videostart0 */ 23)
322 },{
323 .v4l2_id = V4L2_STD_SECAM,
324 .name = "SECAM",
325 .Fsc = 35468950,
326 .swidth = 924,
327 .sheight = 576,
328 .totalwidth = 1135,
329 .adelay = 0x7f,
330 .bdelay = 0xb0,
331 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
332 .scaledtwidth = 1135,
333 .hdelayx1 = 186,
334 .hactivex1 = 922,
335 .vdelay = 0x20,
336 .vbipack = 255,
337 .sram = 0, /* like PAL, correct? */
338 .vbistart = { 7, 320 },
339 CROPCAP(/* minhdelayx1 */ 68,
340 /* hdelayx1 */ 186,
341 /* swidth */ 924,
342 /* totalwidth */ 1135,
343 /* sqwidth */ 944,
344 /* vdelay */ 0x20,
345 /* sheight */ 576,
346 /* videostart0 */ 23)
347 },{
348 .v4l2_id = V4L2_STD_PAL_Nc,
349 .name = "PAL-Nc",
350 .Fsc = 28636363,
351 .swidth = 640,
352 .sheight = 576,
353 .totalwidth = 910,
354 .adelay = 0x68,
355 .bdelay = 0x5d,
356 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
357 .scaledtwidth = 780,
358 .hdelayx1 = 130,
359 .hactivex1 = 734,
360 .vdelay = 0x1a,
361 .vbipack = 144,
362 .sram = -1,
363 .vbistart = { 7, 320 },
364 CROPCAP(/* minhdelayx1 */ 68,
365 /* hdelayx1 */ 130,
366 /* swidth */ (640 * 910 + 780 / 2) / 780,
367 /* totalwidth */ 910,
368 /* sqwidth */ 780,
369 /* vdelay */ 0x1a,
370 /* sheight */ 576,
371 /* videostart0 */ 23)
372 },{
373 .v4l2_id = V4L2_STD_PAL_M,
374 .name = "PAL-M",
375 .Fsc = 28636363,
376 .swidth = 640,
377 .sheight = 480,
378 .totalwidth = 910,
379 .adelay = 0x68,
380 .bdelay = 0x5d,
381 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
382 .scaledtwidth = 780,
383 .hdelayx1 = 135,
384 .hactivex1 = 754,
385 .vdelay = 0x1a,
386 .vbipack = 144,
387 .sram = -1,
388 .vbistart = { 10, 273 },
389 CROPCAP(/* minhdelayx1 */ 68,
390 /* hdelayx1 */ 135,
391 /* swidth */ (640 * 910 + 780 / 2) / 780,
392 /* totalwidth */ 910,
393 /* sqwidth */ 780,
394 /* vdelay */ 0x1a,
395 /* sheight */ 480,
396 /* videostart0 */ 23)
397 },{
398 .v4l2_id = V4L2_STD_PAL_N,
399 .name = "PAL-N",
400 .Fsc = 35468950,
401 .swidth = 768,
402 .sheight = 576,
403 .totalwidth = 1135,
404 .adelay = 0x7f,
405 .bdelay = 0x72,
406 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
407 .scaledtwidth = 944,
408 .hdelayx1 = 186,
409 .hactivex1 = 922,
410 .vdelay = 0x20,
411 .vbipack = 144,
412 .sram = -1,
413 .vbistart = { 7, 320 },
414 CROPCAP(/* minhdelayx1 */ 68,
415 /* hdelayx1 */ 186,
416 /* swidth */ (768 * 1135 + 944 / 2) / 944,
417 /* totalwidth */ 1135,
418 /* sqwidth */ 944,
419 /* vdelay */ 0x20,
420 /* sheight */ 576,
421 /* videostart0 */ 23)
422 },{
423 .v4l2_id = V4L2_STD_NTSC_M_JP,
424 .name = "NTSC-JP",
425 .Fsc = 28636363,
426 .swidth = 640,
427 .sheight = 480,
428 .totalwidth = 910,
429 .adelay = 0x68,
430 .bdelay = 0x5d,
431 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
432 .scaledtwidth = 780,
433 .hdelayx1 = 135,
434 .hactivex1 = 754,
435 .vdelay = 0x16,
436 .vbipack = 144,
437 .sram = -1,
438 .vbistart = { 10, 273 },
439 CROPCAP(/* minhdelayx1 */ 68,
440 /* hdelayx1 */ 135,
441 /* swidth */ (640 * 910 + 780 / 2) / 780,
442 /* totalwidth */ 910,
443 /* sqwidth */ 780,
444 /* vdelay */ 0x16,
445 /* sheight */ 480,
446 /* videostart0 */ 23)
447 },{
448 /* that one hopefully works with the strange timing
449 * which video recorders produce when playing a NTSC
450 * tape on a PAL TV ... */
451 .v4l2_id = V4L2_STD_PAL_60,
452 .name = "PAL-60",
453 .Fsc = 35468950,
454 .swidth = 924,
455 .sheight = 480,
456 .totalwidth = 1135,
457 .adelay = 0x7f,
458 .bdelay = 0x72,
459 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
460 .scaledtwidth = 1135,
461 .hdelayx1 = 186,
462 .hactivex1 = 924,
463 .vdelay = 0x1a,
464 .vbipack = 255,
465 .vtotal = 524,
466 .sram = -1,
467 .vbistart = { 10, 273 },
468 CROPCAP(/* minhdelayx1 */ 68,
469 /* hdelayx1 */ 186,
470 /* swidth */ 924,
471 /* totalwidth */ 1135,
472 /* sqwidth */ 944,
473 /* vdelay */ 0x1a,
474 /* sheight */ 480,
475 /* videostart0 */ 23)
476 }
477 };
478 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
479
480 /* ----------------------------------------------------------------------- */
481 /* bttv format list
482 packed pixel formats must come first */
483 static const struct bttv_format formats[] = {
484 {
485 .name = "8 bpp, gray",
486 .fourcc = V4L2_PIX_FMT_GREY,
487 .btformat = BT848_COLOR_FMT_Y8,
488 .depth = 8,
489 .flags = FORMAT_FLAGS_PACKED,
490 },{
491 .name = "8 bpp, dithered color",
492 .fourcc = V4L2_PIX_FMT_HI240,
493 .btformat = BT848_COLOR_FMT_RGB8,
494 .depth = 8,
495 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
496 },{
497 .name = "15 bpp RGB, le",
498 .fourcc = V4L2_PIX_FMT_RGB555,
499 .btformat = BT848_COLOR_FMT_RGB15,
500 .depth = 16,
501 .flags = FORMAT_FLAGS_PACKED,
502 },{
503 .name = "15 bpp RGB, be",
504 .fourcc = V4L2_PIX_FMT_RGB555X,
505 .btformat = BT848_COLOR_FMT_RGB15,
506 .btswap = 0x03, /* byteswap */
507 .depth = 16,
508 .flags = FORMAT_FLAGS_PACKED,
509 },{
510 .name = "16 bpp RGB, le",
511 .fourcc = V4L2_PIX_FMT_RGB565,
512 .btformat = BT848_COLOR_FMT_RGB16,
513 .depth = 16,
514 .flags = FORMAT_FLAGS_PACKED,
515 },{
516 .name = "16 bpp RGB, be",
517 .fourcc = V4L2_PIX_FMT_RGB565X,
518 .btformat = BT848_COLOR_FMT_RGB16,
519 .btswap = 0x03, /* byteswap */
520 .depth = 16,
521 .flags = FORMAT_FLAGS_PACKED,
522 },{
523 .name = "24 bpp RGB, le",
524 .fourcc = V4L2_PIX_FMT_BGR24,
525 .btformat = BT848_COLOR_FMT_RGB24,
526 .depth = 24,
527 .flags = FORMAT_FLAGS_PACKED,
528 },{
529 .name = "32 bpp RGB, le",
530 .fourcc = V4L2_PIX_FMT_BGR32,
531 .btformat = BT848_COLOR_FMT_RGB32,
532 .depth = 32,
533 .flags = FORMAT_FLAGS_PACKED,
534 },{
535 .name = "32 bpp RGB, be",
536 .fourcc = V4L2_PIX_FMT_RGB32,
537 .btformat = BT848_COLOR_FMT_RGB32,
538 .btswap = 0x0f, /* byte+word swap */
539 .depth = 32,
540 .flags = FORMAT_FLAGS_PACKED,
541 },{
542 .name = "4:2:2, packed, YUYV",
543 .fourcc = V4L2_PIX_FMT_YUYV,
544 .btformat = BT848_COLOR_FMT_YUY2,
545 .depth = 16,
546 .flags = FORMAT_FLAGS_PACKED,
547 },{
548 .name = "4:2:2, packed, YUYV",
549 .fourcc = V4L2_PIX_FMT_YUYV,
550 .btformat = BT848_COLOR_FMT_YUY2,
551 .depth = 16,
552 .flags = FORMAT_FLAGS_PACKED,
553 },{
554 .name = "4:2:2, packed, UYVY",
555 .fourcc = V4L2_PIX_FMT_UYVY,
556 .btformat = BT848_COLOR_FMT_YUY2,
557 .btswap = 0x03, /* byteswap */
558 .depth = 16,
559 .flags = FORMAT_FLAGS_PACKED,
560 },{
561 .name = "4:2:2, planar, Y-Cb-Cr",
562 .fourcc = V4L2_PIX_FMT_YUV422P,
563 .btformat = BT848_COLOR_FMT_YCrCb422,
564 .depth = 16,
565 .flags = FORMAT_FLAGS_PLANAR,
566 .hshift = 1,
567 .vshift = 0,
568 },{
569 .name = "4:2:0, planar, Y-Cb-Cr",
570 .fourcc = V4L2_PIX_FMT_YUV420,
571 .btformat = BT848_COLOR_FMT_YCrCb422,
572 .depth = 12,
573 .flags = FORMAT_FLAGS_PLANAR,
574 .hshift = 1,
575 .vshift = 1,
576 },{
577 .name = "4:2:0, planar, Y-Cr-Cb",
578 .fourcc = V4L2_PIX_FMT_YVU420,
579 .btformat = BT848_COLOR_FMT_YCrCb422,
580 .depth = 12,
581 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
582 .hshift = 1,
583 .vshift = 1,
584 },{
585 .name = "4:1:1, planar, Y-Cb-Cr",
586 .fourcc = V4L2_PIX_FMT_YUV411P,
587 .btformat = BT848_COLOR_FMT_YCrCb411,
588 .depth = 12,
589 .flags = FORMAT_FLAGS_PLANAR,
590 .hshift = 2,
591 .vshift = 0,
592 },{
593 .name = "4:1:0, planar, Y-Cb-Cr",
594 .fourcc = V4L2_PIX_FMT_YUV410,
595 .btformat = BT848_COLOR_FMT_YCrCb411,
596 .depth = 9,
597 .flags = FORMAT_FLAGS_PLANAR,
598 .hshift = 2,
599 .vshift = 2,
600 },{
601 .name = "4:1:0, planar, Y-Cr-Cb",
602 .fourcc = V4L2_PIX_FMT_YVU410,
603 .btformat = BT848_COLOR_FMT_YCrCb411,
604 .depth = 9,
605 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
606 .hshift = 2,
607 .vshift = 2,
608 },{
609 .name = "raw scanlines",
610 .fourcc = -1,
611 .btformat = BT848_COLOR_FMT_RAW,
612 .depth = 8,
613 .flags = FORMAT_FLAGS_RAW,
614 }
615 };
616 static const unsigned int FORMATS = ARRAY_SIZE(formats);
617
618 /* ----------------------------------------------------------------------- */
619
620 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
621 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
622 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
623 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
624 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
625 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
626 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
627 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
628 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
629 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
630 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
631 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
632
633 static const struct v4l2_queryctrl no_ctl = {
634 .name = "42",
635 .flags = V4L2_CTRL_FLAG_DISABLED,
636 };
637 static const struct v4l2_queryctrl bttv_ctls[] = {
638 /* --- video --- */
639 {
640 .id = V4L2_CID_BRIGHTNESS,
641 .name = "Brightness",
642 .minimum = 0,
643 .maximum = 65535,
644 .step = 256,
645 .default_value = 32768,
646 .type = V4L2_CTRL_TYPE_INTEGER,
647 },{
648 .id = V4L2_CID_CONTRAST,
649 .name = "Contrast",
650 .minimum = 0,
651 .maximum = 65535,
652 .step = 128,
653 .default_value = 32768,
654 .type = V4L2_CTRL_TYPE_INTEGER,
655 },{
656 .id = V4L2_CID_SATURATION,
657 .name = "Saturation",
658 .minimum = 0,
659 .maximum = 65535,
660 .step = 128,
661 .default_value = 32768,
662 .type = V4L2_CTRL_TYPE_INTEGER,
663 },{
664 .id = V4L2_CID_HUE,
665 .name = "Hue",
666 .minimum = 0,
667 .maximum = 65535,
668 .step = 256,
669 .default_value = 32768,
670 .type = V4L2_CTRL_TYPE_INTEGER,
671 },
672 /* --- audio --- */
673 {
674 .id = V4L2_CID_AUDIO_MUTE,
675 .name = "Mute",
676 .minimum = 0,
677 .maximum = 1,
678 .type = V4L2_CTRL_TYPE_BOOLEAN,
679 },{
680 .id = V4L2_CID_AUDIO_VOLUME,
681 .name = "Volume",
682 .minimum = 0,
683 .maximum = 65535,
684 .step = 65535/100,
685 .default_value = 65535,
686 .type = V4L2_CTRL_TYPE_INTEGER,
687 },{
688 .id = V4L2_CID_AUDIO_BALANCE,
689 .name = "Balance",
690 .minimum = 0,
691 .maximum = 65535,
692 .step = 65535/100,
693 .default_value = 32768,
694 .type = V4L2_CTRL_TYPE_INTEGER,
695 },{
696 .id = V4L2_CID_AUDIO_BASS,
697 .name = "Bass",
698 .minimum = 0,
699 .maximum = 65535,
700 .step = 65535/100,
701 .default_value = 32768,
702 .type = V4L2_CTRL_TYPE_INTEGER,
703 },{
704 .id = V4L2_CID_AUDIO_TREBLE,
705 .name = "Treble",
706 .minimum = 0,
707 .maximum = 65535,
708 .step = 65535/100,
709 .default_value = 32768,
710 .type = V4L2_CTRL_TYPE_INTEGER,
711 },
712 /* --- private --- */
713 {
714 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
715 .name = "chroma agc",
716 .minimum = 0,
717 .maximum = 1,
718 .type = V4L2_CTRL_TYPE_BOOLEAN,
719 },{
720 .id = V4L2_CID_PRIVATE_COMBFILTER,
721 .name = "combfilter",
722 .minimum = 0,
723 .maximum = 1,
724 .type = V4L2_CTRL_TYPE_BOOLEAN,
725 },{
726 .id = V4L2_CID_PRIVATE_AUTOMUTE,
727 .name = "automute",
728 .minimum = 0,
729 .maximum = 1,
730 .type = V4L2_CTRL_TYPE_BOOLEAN,
731 },{
732 .id = V4L2_CID_PRIVATE_LUMAFILTER,
733 .name = "luma decimation filter",
734 .minimum = 0,
735 .maximum = 1,
736 .type = V4L2_CTRL_TYPE_BOOLEAN,
737 },{
738 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
739 .name = "agc crush",
740 .minimum = 0,
741 .maximum = 1,
742 .type = V4L2_CTRL_TYPE_BOOLEAN,
743 },{
744 .id = V4L2_CID_PRIVATE_VCR_HACK,
745 .name = "vcr hack",
746 .minimum = 0,
747 .maximum = 1,
748 .type = V4L2_CTRL_TYPE_BOOLEAN,
749 },{
750 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
751 .name = "whitecrush upper",
752 .minimum = 0,
753 .maximum = 255,
754 .step = 1,
755 .default_value = 0xCF,
756 .type = V4L2_CTRL_TYPE_INTEGER,
757 },{
758 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
759 .name = "whitecrush lower",
760 .minimum = 0,
761 .maximum = 255,
762 .step = 1,
763 .default_value = 0x7F,
764 .type = V4L2_CTRL_TYPE_INTEGER,
765 },{
766 .id = V4L2_CID_PRIVATE_UV_RATIO,
767 .name = "uv ratio",
768 .minimum = 0,
769 .maximum = 100,
770 .step = 1,
771 .default_value = 50,
772 .type = V4L2_CTRL_TYPE_INTEGER,
773 },{
774 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
775 .name = "full luma range",
776 .minimum = 0,
777 .maximum = 1,
778 .type = V4L2_CTRL_TYPE_BOOLEAN,
779 },{
780 .id = V4L2_CID_PRIVATE_CORING,
781 .name = "coring",
782 .minimum = 0,
783 .maximum = 3,
784 .step = 1,
785 .default_value = 0,
786 .type = V4L2_CTRL_TYPE_INTEGER,
787 }
788
789
790
791 };
792
793 static const struct v4l2_queryctrl *ctrl_by_id(int id)
794 {
795 int i;
796
797 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
798 if (bttv_ctls[i].id == id)
799 return bttv_ctls+i;
800
801 return NULL;
802 }
803
804 /* ----------------------------------------------------------------------- */
805 /* resource management */
806
807 /*
808 RESOURCE_ allocated by freed by
809
810 VIDEO_READ bttv_read 1) bttv_read 2)
811
812 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
813 VIDIOC_QBUF 1) bttv_release
814 VIDIOCMCAPTURE 1)
815
816 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
817 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
818 3) bttv_release
819
820 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
821 VIDIOC_QBUF 1) bttv_release
822 bttv_read, bttv_poll 1) 4)
823
824 1) The resource must be allocated when we enter buffer prepare functions
825 and remain allocated while buffers are in the DMA queue.
826 2) This is a single frame read.
827 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
828 RESOURCE_OVERLAY is allocated.
829 4) This is a continuous read, implies VIDIOC_STREAMON.
830
831 Note this driver permits video input and standard changes regardless if
832 resources are allocated.
833 */
834
835 #define VBI_RESOURCES (RESOURCE_VBI)
836 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
837 RESOURCE_VIDEO_STREAM | \
838 RESOURCE_OVERLAY)
839
840 static
841 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
842 {
843 int xbits; /* mutual exclusive resources */
844
845 if (fh->resources & bit)
846 /* have it already allocated */
847 return 1;
848
849 xbits = bit;
850 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
851 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
852
853 /* is it free? */
854 mutex_lock(&btv->lock);
855 if (btv->resources & xbits) {
856 /* no, someone else uses it */
857 goto fail;
858 }
859
860 if ((bit & VIDEO_RESOURCES)
861 && 0 == (btv->resources & VIDEO_RESOURCES)) {
862 /* Do crop - use current, don't - use default parameters. */
863 __s32 top = btv->crop[!!fh->do_crop].rect.top;
864
865 if (btv->vbi_end > top)
866 goto fail;
867
868 /* We cannot capture the same line as video and VBI data.
869 Claim scan lines crop[].rect.top to bottom. */
870 btv->crop_start = top;
871 } else if (bit & VBI_RESOURCES) {
872 __s32 end = fh->vbi_fmt.end;
873
874 if (end > btv->crop_start)
875 goto fail;
876
877 /* Claim scan lines above fh->vbi_fmt.end. */
878 btv->vbi_end = end;
879 }
880
881 /* it's free, grab it */
882 fh->resources |= bit;
883 btv->resources |= bit;
884 mutex_unlock(&btv->lock);
885 return 1;
886
887 fail:
888 mutex_unlock(&btv->lock);
889 return 0;
890 }
891
892 static
893 int check_btres(struct bttv_fh *fh, int bit)
894 {
895 return (fh->resources & bit);
896 }
897
898 static
899 int locked_btres(struct bttv *btv, int bit)
900 {
901 return (btv->resources & bit);
902 }
903
904 /* Call with btv->lock down. */
905 static void
906 disclaim_vbi_lines(struct bttv *btv)
907 {
908 btv->vbi_end = 0;
909 }
910
911 /* Call with btv->lock down. */
912 static void
913 disclaim_video_lines(struct bttv *btv)
914 {
915 const struct bttv_tvnorm *tvnorm;
916 u8 crop;
917
918 tvnorm = &bttv_tvnorms[btv->tvnorm];
919 btv->crop_start = tvnorm->cropcap.bounds.top
920 + tvnorm->cropcap.bounds.height;
921
922 /* VBI capturing ends at VDELAY, start of video capturing, no
923 matter how many lines the VBI RISC program expects. When video
924 capturing is off, it shall no longer "preempt" VBI capturing,
925 so we set VDELAY to maximum. */
926 crop = btread(BT848_E_CROP) | 0xc0;
927 btwrite(crop, BT848_E_CROP);
928 btwrite(0xfe, BT848_E_VDELAY_LO);
929 btwrite(crop, BT848_O_CROP);
930 btwrite(0xfe, BT848_O_VDELAY_LO);
931 }
932
933 static
934 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
935 {
936 if ((fh->resources & bits) != bits) {
937 /* trying to free ressources not allocated by us ... */
938 printk("bttv: BUG! (btres)\n");
939 }
940 mutex_lock(&btv->lock);
941 fh->resources &= ~bits;
942 btv->resources &= ~bits;
943
944 bits = btv->resources;
945
946 if (0 == (bits & VIDEO_RESOURCES))
947 disclaim_video_lines(btv);
948
949 if (0 == (bits & VBI_RESOURCES))
950 disclaim_vbi_lines(btv);
951
952 mutex_unlock(&btv->lock);
953 }
954
955 /* ----------------------------------------------------------------------- */
956 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
957
958 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
959 PLL_X = Reference pre-divider (0=1, 1=2)
960 PLL_C = Post divider (0=6, 1=4)
961 PLL_I = Integer input
962 PLL_F = Fractional input
963
964 F_input = 28.636363 MHz:
965 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
966 */
967
968 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
969 {
970 unsigned char fl, fh, fi;
971
972 /* prevent overflows */
973 fin/=4;
974 fout/=4;
975
976 fout*=12;
977 fi=fout/fin;
978
979 fout=(fout%fin)*256;
980 fh=fout/fin;
981
982 fout=(fout%fin)*256;
983 fl=fout/fin;
984
985 btwrite(fl, BT848_PLL_F_LO);
986 btwrite(fh, BT848_PLL_F_HI);
987 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
988 }
989
990 static void set_pll(struct bttv *btv)
991 {
992 int i;
993
994 if (!btv->pll.pll_crystal)
995 return;
996
997 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
998 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
999 return;
1000 }
1001
1002 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1003 /* no PLL needed */
1004 if (btv->pll.pll_current == 0)
1005 return;
1006 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
1007 btv->c.nr,btv->pll.pll_ifreq);
1008 btwrite(0x00,BT848_TGCTRL);
1009 btwrite(0x00,BT848_PLL_XCI);
1010 btv->pll.pll_current = 0;
1011 return;
1012 }
1013
1014 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
1015 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1016 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1017
1018 for (i=0; i<10; i++) {
1019 /* Let other people run while the PLL stabilizes */
1020 bttv_printk(".");
1021 msleep(10);
1022
1023 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
1024 btwrite(0,BT848_DSTATUS);
1025 } else {
1026 btwrite(0x08,BT848_TGCTRL);
1027 btv->pll.pll_current = btv->pll.pll_ofreq;
1028 bttv_printk(" ok\n");
1029 return;
1030 }
1031 }
1032 btv->pll.pll_current = -1;
1033 bttv_printk("failed\n");
1034 return;
1035 }
1036
1037 /* used to switch between the bt848's analog/digital video capture modes */
1038 static void bt848A_set_timing(struct bttv *btv)
1039 {
1040 int i, len;
1041 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1042 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1043
1044 if (btv->input == btv->dig) {
1045 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1046 btv->c.nr,table_idx);
1047
1048 /* timing change...reset timing generator address */
1049 btwrite(0x00, BT848_TGCTRL);
1050 btwrite(0x02, BT848_TGCTRL);
1051 btwrite(0x00, BT848_TGCTRL);
1052
1053 len=SRAM_Table[table_idx][0];
1054 for(i = 1; i <= len; i++)
1055 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1056 btv->pll.pll_ofreq = 27000000;
1057
1058 set_pll(btv);
1059 btwrite(0x11, BT848_TGCTRL);
1060 btwrite(0x41, BT848_DVSIF);
1061 } else {
1062 btv->pll.pll_ofreq = fsc;
1063 set_pll(btv);
1064 btwrite(0x0, BT848_DVSIF);
1065 }
1066 }
1067
1068 /* ----------------------------------------------------------------------- */
1069
1070 static void bt848_bright(struct bttv *btv, int bright)
1071 {
1072 int value;
1073
1074 // printk("bttv: set bright: %d\n",bright); // DEBUG
1075 btv->bright = bright;
1076
1077 /* We want -128 to 127 we get 0-65535 */
1078 value = (bright >> 8) - 128;
1079 btwrite(value & 0xff, BT848_BRIGHT);
1080 }
1081
1082 static void bt848_hue(struct bttv *btv, int hue)
1083 {
1084 int value;
1085
1086 btv->hue = hue;
1087
1088 /* -128 to 127 */
1089 value = (hue >> 8) - 128;
1090 btwrite(value & 0xff, BT848_HUE);
1091 }
1092
1093 static void bt848_contrast(struct bttv *btv, int cont)
1094 {
1095 int value,hibit;
1096
1097 btv->contrast = cont;
1098
1099 /* 0-511 */
1100 value = (cont >> 7);
1101 hibit = (value >> 6) & 4;
1102 btwrite(value & 0xff, BT848_CONTRAST_LO);
1103 btaor(hibit, ~4, BT848_E_CONTROL);
1104 btaor(hibit, ~4, BT848_O_CONTROL);
1105 }
1106
1107 static void bt848_sat(struct bttv *btv, int color)
1108 {
1109 int val_u,val_v,hibits;
1110
1111 btv->saturation = color;
1112
1113 /* 0-511 for the color */
1114 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1115 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
1116 hibits = (val_u >> 7) & 2;
1117 hibits |= (val_v >> 8) & 1;
1118 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1119 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1120 btaor(hibits, ~3, BT848_E_CONTROL);
1121 btaor(hibits, ~3, BT848_O_CONTROL);
1122 }
1123
1124 /* ----------------------------------------------------------------------- */
1125
1126 static int
1127 video_mux(struct bttv *btv, unsigned int input)
1128 {
1129 int mux,mask2;
1130
1131 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1132 return -EINVAL;
1133
1134 /* needed by RemoteVideo MX */
1135 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1136 if (mask2)
1137 gpio_inout(mask2,mask2);
1138
1139 if (input == btv->svhs) {
1140 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1141 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1142 } else {
1143 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1144 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1145 }
1146 mux = bttv_muxsel(btv, input);
1147 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1148 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1149 btv->c.nr,input,mux);
1150
1151 /* card specific hook */
1152 if(bttv_tvcards[btv->c.type].muxsel_hook)
1153 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1154 return 0;
1155 }
1156
1157 static char *audio_modes[] = {
1158 "audio: tuner", "audio: radio", "audio: extern",
1159 "audio: intern", "audio: mute"
1160 };
1161
1162 static int
1163 audio_mux(struct bttv *btv, int input, int mute)
1164 {
1165 int gpio_val, signal;
1166 struct v4l2_control ctrl;
1167
1168 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1169 bttv_tvcards[btv->c.type].gpiomask);
1170 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1171
1172 btv->mute = mute;
1173 btv->audio = input;
1174
1175 /* automute */
1176 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1177
1178 if (mute)
1179 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1180 else
1181 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
1182
1183 switch (btv->c.type) {
1184 case BTTV_BOARD_VOODOOTV_FM:
1185 case BTTV_BOARD_VOODOOTV_200:
1186 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1187 break;
1188
1189 default:
1190 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1191 }
1192
1193 if (bttv_gpio)
1194 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1195 if (in_interrupt())
1196 return 0;
1197
1198 ctrl.id = V4L2_CID_AUDIO_MUTE;
1199 ctrl.value = btv->mute;
1200 bttv_call_all(btv, core, s_ctrl, &ctrl);
1201 if (btv->sd_msp34xx) {
1202 u32 in;
1203
1204 /* Note: the inputs tuner/radio/extern/intern are translated
1205 to msp routings. This assumes common behavior for all msp3400
1206 based TV cards. When this assumption fails, then the
1207 specific MSP routing must be added to the card table.
1208 For now this is sufficient. */
1209 switch (input) {
1210 case TVAUDIO_INPUT_RADIO:
1211 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1212 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1213 break;
1214 case TVAUDIO_INPUT_EXTERN:
1215 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1216 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1217 break;
1218 case TVAUDIO_INPUT_INTERN:
1219 /* Yes, this is the same input as for RADIO. I doubt
1220 if this is ever used. The only board with an INTERN
1221 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1222 that was tested. My guess is that the whole INTERN
1223 input does not work. */
1224 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1225 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1226 break;
1227 case TVAUDIO_INPUT_TUNER:
1228 default:
1229 /* This is the only card that uses TUNER2, and afaik,
1230 is the only difference between the VOODOOTV_FM
1231 and VOODOOTV_200 */
1232 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1233 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1234 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1235 else
1236 in = MSP_INPUT_DEFAULT;
1237 break;
1238 }
1239 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1240 in, MSP_OUTPUT_DEFAULT, 0);
1241 }
1242 if (btv->sd_tvaudio) {
1243 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1244 input, 0, 0);
1245 }
1246 return 0;
1247 }
1248
1249 static inline int
1250 audio_mute(struct bttv *btv, int mute)
1251 {
1252 return audio_mux(btv, btv->audio, mute);
1253 }
1254
1255 static inline int
1256 audio_input(struct bttv *btv, int input)
1257 {
1258 return audio_mux(btv, input, btv->mute);
1259 }
1260
1261 static void
1262 bttv_crop_calc_limits(struct bttv_crop *c)
1263 {
1264 /* Scale factor min. 1:1, max. 16:1. Min. image size
1265 48 x 32. Scaled width must be a multiple of 4. */
1266
1267 if (1) {
1268 /* For bug compatibility with VIDIOCGCAP and image
1269 size checks in earlier driver versions. */
1270 c->min_scaled_width = 48;
1271 c->min_scaled_height = 32;
1272 } else {
1273 c->min_scaled_width =
1274 (max(48, c->rect.width >> 4) + 3) & ~3;
1275 c->min_scaled_height =
1276 max(32, c->rect.height >> 4);
1277 }
1278
1279 c->max_scaled_width = c->rect.width & ~3;
1280 c->max_scaled_height = c->rect.height;
1281 }
1282
1283 static void
1284 bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
1285 {
1286 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1287 bttv_crop_calc_limits(c);
1288 }
1289
1290 /* Call with btv->lock down. */
1291 static int
1292 set_tvnorm(struct bttv *btv, unsigned int norm)
1293 {
1294 const struct bttv_tvnorm *tvnorm;
1295 v4l2_std_id id;
1296
1297 BUG_ON(norm >= BTTV_TVNORMS);
1298 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
1299
1300 tvnorm = &bttv_tvnorms[norm];
1301
1302 if (!memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
1303 sizeof (tvnorm->cropcap))) {
1304 bttv_crop_reset(&btv->crop[0], norm);
1305 btv->crop[1] = btv->crop[0]; /* current = default */
1306
1307 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1308 btv->crop_start = tvnorm->cropcap.bounds.top
1309 + tvnorm->cropcap.bounds.height;
1310 }
1311 }
1312
1313 btv->tvnorm = norm;
1314
1315 btwrite(tvnorm->adelay, BT848_ADELAY);
1316 btwrite(tvnorm->bdelay, BT848_BDELAY);
1317 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1318 BT848_IFORM);
1319 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1320 btwrite(1, BT848_VBI_PACK_DEL);
1321 bt848A_set_timing(btv);
1322
1323 switch (btv->c.type) {
1324 case BTTV_BOARD_VOODOOTV_FM:
1325 case BTTV_BOARD_VOODOOTV_200:
1326 bttv_tda9880_setnorm(btv, gpio_read());
1327 break;
1328 }
1329 id = tvnorm->v4l2_id;
1330 bttv_call_all(btv, core, s_std, id);
1331
1332 return 0;
1333 }
1334
1335 /* Call with btv->lock down. */
1336 static void
1337 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1338 {
1339 unsigned long flags;
1340
1341 btv->input = input;
1342 if (irq_iswitch) {
1343 spin_lock_irqsave(&btv->s_lock,flags);
1344 if (btv->curr.frame_irq) {
1345 /* active capture -> delayed input switch */
1346 btv->new_input = input;
1347 } else {
1348 video_mux(btv,input);
1349 }
1350 spin_unlock_irqrestore(&btv->s_lock,flags);
1351 } else {
1352 video_mux(btv,input);
1353 }
1354 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1355 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
1356 set_tvnorm(btv, norm);
1357 }
1358
1359 static void init_irqreg(struct bttv *btv)
1360 {
1361 /* clear status */
1362 btwrite(0xfffffUL, BT848_INT_STAT);
1363
1364 if (bttv_tvcards[btv->c.type].no_video) {
1365 /* i2c only */
1366 btwrite(BT848_INT_I2CDONE,
1367 BT848_INT_MASK);
1368 } else {
1369 /* full video */
1370 btwrite((btv->triton1) |
1371 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1372 BT848_INT_SCERR |
1373 (fdsr ? BT848_INT_FDSR : 0) |
1374 BT848_INT_RISCI | BT848_INT_OCERR |
1375 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1376 BT848_INT_I2CDONE,
1377 BT848_INT_MASK);
1378 }
1379 }
1380
1381 static void init_bt848(struct bttv *btv)
1382 {
1383 int val;
1384
1385 if (bttv_tvcards[btv->c.type].no_video) {
1386 /* very basic init only */
1387 init_irqreg(btv);
1388 return;
1389 }
1390
1391 btwrite(0x00, BT848_CAP_CTL);
1392 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1393 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1394
1395 /* set planar and packed mode trigger points and */
1396 /* set rising edge of inverted GPINTR pin as irq trigger */
1397 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1398 BT848_GPIO_DMA_CTL_PLTP1_16|
1399 BT848_GPIO_DMA_CTL_PLTP23_16|
1400 BT848_GPIO_DMA_CTL_GPINTC|
1401 BT848_GPIO_DMA_CTL_GPINTI,
1402 BT848_GPIO_DMA_CTL);
1403
1404 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1405 btwrite(val, BT848_E_SCLOOP);
1406 btwrite(val, BT848_O_SCLOOP);
1407
1408 btwrite(0x20, BT848_E_VSCALE_HI);
1409 btwrite(0x20, BT848_O_VSCALE_HI);
1410 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1411 BT848_ADC);
1412
1413 btwrite(whitecrush_upper, BT848_WC_UP);
1414 btwrite(whitecrush_lower, BT848_WC_DOWN);
1415
1416 if (btv->opt_lumafilter) {
1417 btwrite(0, BT848_E_CONTROL);
1418 btwrite(0, BT848_O_CONTROL);
1419 } else {
1420 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1421 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1422 }
1423
1424 bt848_bright(btv, btv->bright);
1425 bt848_hue(btv, btv->hue);
1426 bt848_contrast(btv, btv->contrast);
1427 bt848_sat(btv, btv->saturation);
1428
1429 /* interrupt */
1430 init_irqreg(btv);
1431 }
1432
1433 static void bttv_reinit_bt848(struct bttv *btv)
1434 {
1435 unsigned long flags;
1436
1437 if (bttv_verbose)
1438 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1439 spin_lock_irqsave(&btv->s_lock,flags);
1440 btv->errors=0;
1441 bttv_set_dma(btv,0);
1442 spin_unlock_irqrestore(&btv->s_lock,flags);
1443
1444 init_bt848(btv);
1445 btv->pll.pll_current = -1;
1446 set_input(btv, btv->input, btv->tvnorm);
1447 }
1448
1449 static int bttv_g_ctrl(struct file *file, void *priv,
1450 struct v4l2_control *c)
1451 {
1452 struct bttv_fh *fh = priv;
1453 struct bttv *btv = fh->btv;
1454
1455 switch (c->id) {
1456 case V4L2_CID_BRIGHTNESS:
1457 c->value = btv->bright;
1458 break;
1459 case V4L2_CID_HUE:
1460 c->value = btv->hue;
1461 break;
1462 case V4L2_CID_CONTRAST:
1463 c->value = btv->contrast;
1464 break;
1465 case V4L2_CID_SATURATION:
1466 c->value = btv->saturation;
1467 break;
1468
1469 case V4L2_CID_AUDIO_MUTE:
1470 case V4L2_CID_AUDIO_VOLUME:
1471 case V4L2_CID_AUDIO_BALANCE:
1472 case V4L2_CID_AUDIO_BASS:
1473 case V4L2_CID_AUDIO_TREBLE:
1474 bttv_call_all(btv, core, g_ctrl, c);
1475 break;
1476
1477 case V4L2_CID_PRIVATE_CHROMA_AGC:
1478 c->value = btv->opt_chroma_agc;
1479 break;
1480 case V4L2_CID_PRIVATE_COMBFILTER:
1481 c->value = btv->opt_combfilter;
1482 break;
1483 case V4L2_CID_PRIVATE_LUMAFILTER:
1484 c->value = btv->opt_lumafilter;
1485 break;
1486 case V4L2_CID_PRIVATE_AUTOMUTE:
1487 c->value = btv->opt_automute;
1488 break;
1489 case V4L2_CID_PRIVATE_AGC_CRUSH:
1490 c->value = btv->opt_adc_crush;
1491 break;
1492 case V4L2_CID_PRIVATE_VCR_HACK:
1493 c->value = btv->opt_vcr_hack;
1494 break;
1495 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1496 c->value = btv->opt_whitecrush_upper;
1497 break;
1498 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1499 c->value = btv->opt_whitecrush_lower;
1500 break;
1501 case V4L2_CID_PRIVATE_UV_RATIO:
1502 c->value = btv->opt_uv_ratio;
1503 break;
1504 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1505 c->value = btv->opt_full_luma_range;
1506 break;
1507 case V4L2_CID_PRIVATE_CORING:
1508 c->value = btv->opt_coring;
1509 break;
1510 default:
1511 return -EINVAL;
1512 }
1513 return 0;
1514 }
1515
1516 static int bttv_s_ctrl(struct file *file, void *f,
1517 struct v4l2_control *c)
1518 {
1519 int err;
1520 int val;
1521 struct bttv_fh *fh = f;
1522 struct bttv *btv = fh->btv;
1523
1524 err = v4l2_prio_check(&btv->prio, &fh->prio);
1525 if (0 != err)
1526 return err;
1527
1528 switch (c->id) {
1529 case V4L2_CID_BRIGHTNESS:
1530 bt848_bright(btv, c->value);
1531 break;
1532 case V4L2_CID_HUE:
1533 bt848_hue(btv, c->value);
1534 break;
1535 case V4L2_CID_CONTRAST:
1536 bt848_contrast(btv, c->value);
1537 break;
1538 case V4L2_CID_SATURATION:
1539 bt848_sat(btv, c->value);
1540 break;
1541 case V4L2_CID_AUDIO_MUTE:
1542 audio_mute(btv, c->value);
1543 /* fall through */
1544 case V4L2_CID_AUDIO_VOLUME:
1545 if (btv->volume_gpio)
1546 btv->volume_gpio(btv, c->value);
1547
1548 bttv_call_all(btv, core, s_ctrl, c);
1549 break;
1550 case V4L2_CID_AUDIO_BALANCE:
1551 case V4L2_CID_AUDIO_BASS:
1552 case V4L2_CID_AUDIO_TREBLE:
1553 bttv_call_all(btv, core, s_ctrl, c);
1554 break;
1555
1556 case V4L2_CID_PRIVATE_CHROMA_AGC:
1557 btv->opt_chroma_agc = c->value;
1558 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1559 btwrite(val, BT848_E_SCLOOP);
1560 btwrite(val, BT848_O_SCLOOP);
1561 break;
1562 case V4L2_CID_PRIVATE_COMBFILTER:
1563 btv->opt_combfilter = c->value;
1564 break;
1565 case V4L2_CID_PRIVATE_LUMAFILTER:
1566 btv->opt_lumafilter = c->value;
1567 if (btv->opt_lumafilter) {
1568 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1569 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1570 } else {
1571 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1572 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1573 }
1574 break;
1575 case V4L2_CID_PRIVATE_AUTOMUTE:
1576 btv->opt_automute = c->value;
1577 break;
1578 case V4L2_CID_PRIVATE_AGC_CRUSH:
1579 btv->opt_adc_crush = c->value;
1580 btwrite(BT848_ADC_RESERVED |
1581 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1582 BT848_ADC);
1583 break;
1584 case V4L2_CID_PRIVATE_VCR_HACK:
1585 btv->opt_vcr_hack = c->value;
1586 break;
1587 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1588 btv->opt_whitecrush_upper = c->value;
1589 btwrite(c->value, BT848_WC_UP);
1590 break;
1591 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1592 btv->opt_whitecrush_lower = c->value;
1593 btwrite(c->value, BT848_WC_DOWN);
1594 break;
1595 case V4L2_CID_PRIVATE_UV_RATIO:
1596 btv->opt_uv_ratio = c->value;
1597 bt848_sat(btv, btv->saturation);
1598 break;
1599 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1600 btv->opt_full_luma_range = c->value;
1601 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1602 break;
1603 case V4L2_CID_PRIVATE_CORING:
1604 btv->opt_coring = c->value;
1605 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1606 break;
1607 default:
1608 return -EINVAL;
1609 }
1610 return 0;
1611 }
1612
1613 /* ----------------------------------------------------------------------- */
1614
1615 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1616 {
1617 unsigned int outbits, data;
1618 outbits = btread(BT848_GPIO_OUT_EN);
1619 data = btread(BT848_GPIO_DATA);
1620 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1621 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1622 }
1623
1624 static void bttv_field_count(struct bttv *btv)
1625 {
1626 int need_count = 0;
1627
1628 if (btv->users)
1629 need_count++;
1630
1631 if (need_count) {
1632 /* start field counter */
1633 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1634 } else {
1635 /* stop field counter */
1636 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1637 btv->field_count = 0;
1638 }
1639 }
1640
1641 static const struct bttv_format*
1642 format_by_fourcc(int fourcc)
1643 {
1644 unsigned int i;
1645
1646 for (i = 0; i < FORMATS; i++) {
1647 if (-1 == formats[i].fourcc)
1648 continue;
1649 if (formats[i].fourcc == fourcc)
1650 return formats+i;
1651 }
1652 return NULL;
1653 }
1654
1655 /* ----------------------------------------------------------------------- */
1656 /* misc helpers */
1657
1658 static int
1659 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1660 struct bttv_buffer *new)
1661 {
1662 struct bttv_buffer *old;
1663 unsigned long flags;
1664 int retval = 0;
1665
1666 dprintk("switch_overlay: enter [new=%p]\n",new);
1667 if (new)
1668 new->vb.state = VIDEOBUF_DONE;
1669 spin_lock_irqsave(&btv->s_lock,flags);
1670 old = btv->screen;
1671 btv->screen = new;
1672 btv->loop_irq |= 1;
1673 bttv_set_dma(btv, 0x03);
1674 spin_unlock_irqrestore(&btv->s_lock,flags);
1675 if (NULL != old) {
1676 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1677 bttv_dma_free(&fh->cap,btv, old);
1678 kfree(old);
1679 }
1680 if (NULL == new)
1681 free_btres(btv,fh,RESOURCE_OVERLAY);
1682 dprintk("switch_overlay: done\n");
1683 return retval;
1684 }
1685
1686 /* ----------------------------------------------------------------------- */
1687 /* video4linux (1) interface */
1688
1689 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1690 struct bttv_buffer *buf,
1691 const struct bttv_format *fmt,
1692 unsigned int width, unsigned int height,
1693 enum v4l2_field field)
1694 {
1695 struct bttv_fh *fh = q->priv_data;
1696 int redo_dma_risc = 0;
1697 struct bttv_crop c;
1698 int norm;
1699 int rc;
1700
1701 /* check settings */
1702 if (NULL == fmt)
1703 return -EINVAL;
1704 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1705 width = RAW_BPL;
1706 height = RAW_LINES*2;
1707 if (width*height > buf->vb.bsize)
1708 return -EINVAL;
1709 buf->vb.size = buf->vb.bsize;
1710
1711 /* Make sure tvnorm and vbi_end remain consistent
1712 until we're done. */
1713 mutex_lock(&btv->lock);
1714
1715 norm = btv->tvnorm;
1716
1717 /* In this mode capturing always starts at defrect.top
1718 (default VDELAY), ignoring cropping parameters. */
1719 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1720 mutex_unlock(&btv->lock);
1721 return -EINVAL;
1722 }
1723
1724 mutex_unlock(&btv->lock);
1725
1726 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1727 } else {
1728 mutex_lock(&btv->lock);
1729
1730 norm = btv->tvnorm;
1731 c = btv->crop[!!fh->do_crop];
1732
1733 mutex_unlock(&btv->lock);
1734
1735 if (width < c.min_scaled_width ||
1736 width > c.max_scaled_width ||
1737 height < c.min_scaled_height)
1738 return -EINVAL;
1739
1740 switch (field) {
1741 case V4L2_FIELD_TOP:
1742 case V4L2_FIELD_BOTTOM:
1743 case V4L2_FIELD_ALTERNATE:
1744 /* btv->crop counts frame lines. Max. scale
1745 factor is 16:1 for frames, 8:1 for fields. */
1746 if (height * 2 > c.max_scaled_height)
1747 return -EINVAL;
1748 break;
1749
1750 default:
1751 if (height > c.max_scaled_height)
1752 return -EINVAL;
1753 break;
1754 }
1755
1756 buf->vb.size = (width * height * fmt->depth) >> 3;
1757 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1758 return -EINVAL;
1759 }
1760
1761 /* alloc + fill struct bttv_buffer (if changed) */
1762 if (buf->vb.width != width || buf->vb.height != height ||
1763 buf->vb.field != field ||
1764 buf->tvnorm != norm || buf->fmt != fmt ||
1765 buf->crop.top != c.rect.top ||
1766 buf->crop.left != c.rect.left ||
1767 buf->crop.width != c.rect.width ||
1768 buf->crop.height != c.rect.height) {
1769 buf->vb.width = width;
1770 buf->vb.height = height;
1771 buf->vb.field = field;
1772 buf->tvnorm = norm;
1773 buf->fmt = fmt;
1774 buf->crop = c.rect;
1775 redo_dma_risc = 1;
1776 }
1777
1778 /* alloc risc memory */
1779 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1780 redo_dma_risc = 1;
1781 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1782 goto fail;
1783 }
1784
1785 if (redo_dma_risc)
1786 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1787 goto fail;
1788
1789 buf->vb.state = VIDEOBUF_PREPARED;
1790 return 0;
1791
1792 fail:
1793 bttv_dma_free(q,btv,buf);
1794 return rc;
1795 }
1796
1797 static int
1798 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1799 {
1800 struct bttv_fh *fh = q->priv_data;
1801
1802 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1803 if (0 == *count)
1804 *count = gbuffers;
1805 while (*size * *count > gbuffers * gbufsize)
1806 (*count)--;
1807 return 0;
1808 }
1809
1810 static int
1811 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1812 enum v4l2_field field)
1813 {
1814 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1815 struct bttv_fh *fh = q->priv_data;
1816
1817 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1818 fh->width, fh->height, field);
1819 }
1820
1821 static void
1822 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1823 {
1824 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1825 struct bttv_fh *fh = q->priv_data;
1826 struct bttv *btv = fh->btv;
1827
1828 buf->vb.state = VIDEOBUF_QUEUED;
1829 list_add_tail(&buf->vb.queue,&btv->capture);
1830 if (!btv->curr.frame_irq) {
1831 btv->loop_irq |= 1;
1832 bttv_set_dma(btv, 0x03);
1833 }
1834 }
1835
1836 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1837 {
1838 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1839 struct bttv_fh *fh = q->priv_data;
1840
1841 bttv_dma_free(q,fh->btv,buf);
1842 }
1843
1844 static struct videobuf_queue_ops bttv_video_qops = {
1845 .buf_setup = buffer_setup,
1846 .buf_prepare = buffer_prepare,
1847 .buf_queue = buffer_queue,
1848 .buf_release = buffer_release,
1849 };
1850
1851 static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
1852 {
1853 struct bttv_fh *fh = priv;
1854 struct bttv *btv = fh->btv;
1855 unsigned int i;
1856 int err;
1857
1858 err = v4l2_prio_check(&btv->prio, &fh->prio);
1859 if (0 != err)
1860 return err;
1861
1862 for (i = 0; i < BTTV_TVNORMS; i++)
1863 if (*id & bttv_tvnorms[i].v4l2_id)
1864 break;
1865 if (i == BTTV_TVNORMS)
1866 return -EINVAL;
1867
1868 mutex_lock(&btv->lock);
1869 set_tvnorm(btv, i);
1870 mutex_unlock(&btv->lock);
1871
1872 return 0;
1873 }
1874
1875 static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1876 {
1877 struct bttv_fh *fh = f;
1878 struct bttv *btv = fh->btv;
1879
1880 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1881 *id = V4L2_STD_625_50;
1882 else
1883 *id = V4L2_STD_525_60;
1884 return 0;
1885 }
1886
1887 static int bttv_enum_input(struct file *file, void *priv,
1888 struct v4l2_input *i)
1889 {
1890 struct bttv_fh *fh = priv;
1891 struct bttv *btv = fh->btv;
1892 int n;
1893
1894 if (i->index >= bttv_tvcards[btv->c.type].video_inputs)
1895 return -EINVAL;
1896
1897 i->type = V4L2_INPUT_TYPE_CAMERA;
1898 i->audioset = 1;
1899
1900 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
1901 sprintf(i->name, "Television");
1902 i->type = V4L2_INPUT_TYPE_TUNER;
1903 i->tuner = 0;
1904 } else if (i->index == btv->svhs) {
1905 sprintf(i->name, "S-Video");
1906 } else {
1907 sprintf(i->name, "Composite%d", i->index);
1908 }
1909
1910 if (i->index == btv->input) {
1911 __u32 dstatus = btread(BT848_DSTATUS);
1912 if (0 == (dstatus & BT848_DSTATUS_PRES))
1913 i->status |= V4L2_IN_ST_NO_SIGNAL;
1914 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1915 i->status |= V4L2_IN_ST_NO_H_LOCK;
1916 }
1917
1918 for (n = 0; n < BTTV_TVNORMS; n++)
1919 i->std |= bttv_tvnorms[n].v4l2_id;
1920
1921 return 0;
1922 }
1923
1924 static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1925 {
1926 struct bttv_fh *fh = priv;
1927 struct bttv *btv = fh->btv;
1928
1929 *i = btv->input;
1930 return 0;
1931 }
1932
1933 static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1934 {
1935 struct bttv_fh *fh = priv;
1936 struct bttv *btv = fh->btv;
1937
1938 int err;
1939
1940 err = v4l2_prio_check(&btv->prio, &fh->prio);
1941 if (0 != err)
1942 return err;
1943
1944 if (i > bttv_tvcards[btv->c.type].video_inputs)
1945 return -EINVAL;
1946
1947 mutex_lock(&btv->lock);
1948 set_input(btv, i, btv->tvnorm);
1949 mutex_unlock(&btv->lock);
1950 return 0;
1951 }
1952
1953 static int bttv_s_tuner(struct file *file, void *priv,
1954 struct v4l2_tuner *t)
1955 {
1956 struct bttv_fh *fh = priv;
1957 struct bttv *btv = fh->btv;
1958 int err;
1959
1960 err = v4l2_prio_check(&btv->prio, &fh->prio);
1961 if (0 != err)
1962 return err;
1963
1964 if (btv->tuner_type == TUNER_ABSENT)
1965 return -EINVAL;
1966
1967 if (0 != t->index)
1968 return -EINVAL;
1969
1970 mutex_lock(&btv->lock);
1971 bttv_call_all(btv, tuner, s_tuner, t);
1972
1973 if (btv->audio_mode_gpio)
1974 btv->audio_mode_gpio(btv, t, 1);
1975
1976 mutex_unlock(&btv->lock);
1977
1978 return 0;
1979 }
1980
1981 static int bttv_g_frequency(struct file *file, void *priv,
1982 struct v4l2_frequency *f)
1983 {
1984 struct bttv_fh *fh = priv;
1985 struct bttv *btv = fh->btv;
1986 int err;
1987
1988 err = v4l2_prio_check(&btv->prio, &fh->prio);
1989 if (0 != err)
1990 return err;
1991
1992 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1993 f->frequency = btv->freq;
1994
1995 return 0;
1996 }
1997
1998 static int bttv_s_frequency(struct file *file, void *priv,
1999 struct v4l2_frequency *f)
2000 {
2001 struct bttv_fh *fh = priv;
2002 struct bttv *btv = fh->btv;
2003 int err;
2004
2005 err = v4l2_prio_check(&btv->prio, &fh->prio);
2006 if (0 != err)
2007 return err;
2008
2009 if (unlikely(f->tuner != 0))
2010 return -EINVAL;
2011 if (unlikely(f->type != (btv->radio_user
2012 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV)))
2013 return -EINVAL;
2014 mutex_lock(&btv->lock);
2015 btv->freq = f->frequency;
2016 bttv_call_all(btv, tuner, s_frequency, f);
2017 if (btv->has_matchbox && btv->radio_user)
2018 tea5757_set_freq(btv, btv->freq);
2019 mutex_unlock(&btv->lock);
2020 return 0;
2021 }
2022
2023 static int bttv_log_status(struct file *file, void *f)
2024 {
2025 struct bttv_fh *fh = f;
2026 struct bttv *btv = fh->btv;
2027
2028 printk(KERN_INFO "bttv%d: ======== START STATUS CARD #%d ========\n",
2029 btv->c.nr, btv->c.nr);
2030 bttv_call_all(btv, core, log_status);
2031 printk(KERN_INFO "bttv%d: ======== END STATUS CARD #%d ========\n",
2032 btv->c.nr, btv->c.nr);
2033 return 0;
2034 }
2035
2036 #ifdef CONFIG_VIDEO_ADV_DEBUG
2037 static int bttv_g_register(struct file *file, void *f,
2038 struct v4l2_dbg_register *reg)
2039 {
2040 struct bttv_fh *fh = f;
2041 struct bttv *btv = fh->btv;
2042
2043 if (!capable(CAP_SYS_ADMIN))
2044 return -EPERM;
2045
2046 if (!v4l2_chip_match_host(&reg->match))
2047 return -EINVAL;
2048
2049 /* bt848 has a 12-bit register space */
2050 reg->reg &= 0xfff;
2051 reg->val = btread(reg->reg);
2052 reg->size = 1;
2053
2054 return 0;
2055 }
2056
2057 static int bttv_s_register(struct file *file, void *f,
2058 struct v4l2_dbg_register *reg)
2059 {
2060 struct bttv_fh *fh = f;
2061 struct bttv *btv = fh->btv;
2062
2063 if (!capable(CAP_SYS_ADMIN))
2064 return -EPERM;
2065
2066 if (!v4l2_chip_match_host(&reg->match))
2067 return -EINVAL;
2068
2069 /* bt848 has a 12-bit register space */
2070 reg->reg &= 0xfff;
2071 btwrite(reg->val, reg->reg);
2072
2073 return 0;
2074 }
2075 #endif
2076
2077 /* Given cropping boundaries b and the scaled width and height of a
2078 single field or frame, which must not exceed hardware limits, this
2079 function adjusts the cropping parameters c. */
2080 static void
2081 bttv_crop_adjust (struct bttv_crop * c,
2082 const struct v4l2_rect * b,
2083 __s32 width,
2084 __s32 height,
2085 enum v4l2_field field)
2086 {
2087 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2088 __s32 max_left;
2089 __s32 max_top;
2090
2091 if (width < c->min_scaled_width) {
2092 /* Max. hor. scale factor 16:1. */
2093 c->rect.width = width * 16;
2094 } else if (width > c->max_scaled_width) {
2095 /* Min. hor. scale factor 1:1. */
2096 c->rect.width = width;
2097
2098 max_left = b->left + b->width - width;
2099 max_left = min(max_left, (__s32) MAX_HDELAY);
2100 if (c->rect.left > max_left)
2101 c->rect.left = max_left;
2102 }
2103
2104 if (height < c->min_scaled_height) {
2105 /* Max. vert. scale factor 16:1, single fields 8:1. */
2106 c->rect.height = height * 16;
2107 } else if (frame_height > c->max_scaled_height) {
2108 /* Min. vert. scale factor 1:1.
2109 Top and height count field lines times two. */
2110 c->rect.height = (frame_height + 1) & ~1;
2111
2112 max_top = b->top + b->height - c->rect.height;
2113 if (c->rect.top > max_top)
2114 c->rect.top = max_top;
2115 }
2116
2117 bttv_crop_calc_limits(c);
2118 }
2119
2120 /* Returns an error if scaling to a frame or single field with the given
2121 width and height is not possible with the current cropping parameters
2122 and width aligned according to width_mask. If adjust_size is TRUE the
2123 function may adjust the width and/or height instead, rounding width
2124 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2125 also adjust the current cropping parameters to get closer to the
2126 desired image size. */
2127 static int
2128 limit_scaled_size (struct bttv_fh * fh,
2129 __s32 * width,
2130 __s32 * height,
2131 enum v4l2_field field,
2132 unsigned int width_mask,
2133 unsigned int width_bias,
2134 int adjust_size,
2135 int adjust_crop)
2136 {
2137 struct bttv *btv = fh->btv;
2138 const struct v4l2_rect *b;
2139 struct bttv_crop *c;
2140 __s32 min_width;
2141 __s32 min_height;
2142 __s32 max_width;
2143 __s32 max_height;
2144 int rc;
2145
2146 BUG_ON((int) width_mask >= 0 ||
2147 width_bias >= (unsigned int) -width_mask);
2148
2149 /* Make sure tvnorm, vbi_end and the current cropping parameters
2150 remain consistent until we're done. */
2151 mutex_lock(&btv->lock);
2152
2153 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2154
2155 /* Do crop - use current, don't - use default parameters. */
2156 c = &btv->crop[!!fh->do_crop];
2157
2158 if (fh->do_crop
2159 && adjust_size
2160 && adjust_crop
2161 && !locked_btres(btv, VIDEO_RESOURCES)) {
2162 min_width = 48;
2163 min_height = 32;
2164
2165 /* We cannot scale up. When the scaled image is larger
2166 than crop.rect we adjust the crop.rect as required
2167 by the V4L2 spec, hence cropcap.bounds are our limit. */
2168 max_width = min(b->width, (__s32) MAX_HACTIVE);
2169 max_height = b->height;
2170
2171 /* We cannot capture the same line as video and VBI data.
2172 Note btv->vbi_end is really a minimum, see
2173 bttv_vbi_try_fmt(). */
2174 if (btv->vbi_end > b->top) {
2175 max_height -= btv->vbi_end - b->top;
2176 rc = -EBUSY;
2177 if (min_height > max_height)
2178 goto fail;
2179 }
2180 } else {
2181 rc = -EBUSY;
2182 if (btv->vbi_end > c->rect.top)
2183 goto fail;
2184
2185 min_width = c->min_scaled_width;
2186 min_height = c->min_scaled_height;
2187 max_width = c->max_scaled_width;
2188 max_height = c->max_scaled_height;
2189
2190 adjust_crop = 0;
2191 }
2192
2193 min_width = (min_width - width_mask - 1) & width_mask;
2194 max_width = max_width & width_mask;
2195
2196 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2197 min_height = min_height;
2198 /* Min. scale factor is 1:1. */
2199 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2200
2201 if (adjust_size) {
2202 *width = clamp(*width, min_width, max_width);
2203 *height = clamp(*height, min_height, max_height);
2204
2205 /* Round after clamping to avoid overflow. */
2206 *width = (*width + width_bias) & width_mask;
2207
2208 if (adjust_crop) {
2209 bttv_crop_adjust(c, b, *width, *height, field);
2210
2211 if (btv->vbi_end > c->rect.top) {
2212 /* Move the crop window out of the way. */
2213 c->rect.top = btv->vbi_end;
2214 }
2215 }
2216 } else {
2217 rc = -EINVAL;
2218 if (*width < min_width ||
2219 *height < min_height ||
2220 *width > max_width ||
2221 *height > max_height ||
2222 0 != (*width & ~width_mask))
2223 goto fail;
2224 }
2225
2226 rc = 0; /* success */
2227
2228 fail:
2229 mutex_unlock(&btv->lock);
2230
2231 return rc;
2232 }
2233
2234 /* Returns an error if the given overlay window dimensions are not
2235 possible with the current cropping parameters. If adjust_size is
2236 TRUE the function may adjust the window width and/or height
2237 instead, however it always rounds the horizontal position and
2238 width as btcx_align() does. If adjust_crop is TRUE the function
2239 may also adjust the current cropping parameters to get closer
2240 to the desired window size. */
2241 static int
2242 verify_window (struct bttv_fh * fh,
2243 struct v4l2_window * win,
2244 int adjust_size,
2245 int adjust_crop)
2246 {
2247 enum v4l2_field field;
2248 unsigned int width_mask;
2249 int rc;
2250
2251 if (win->w.width < 48 || win->w.height < 32)
2252 return -EINVAL;
2253 if (win->clipcount > 2048)
2254 return -EINVAL;
2255
2256 field = win->field;
2257
2258 if (V4L2_FIELD_ANY == field) {
2259 __s32 height2;
2260
2261 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2262 field = (win->w.height > height2)
2263 ? V4L2_FIELD_INTERLACED
2264 : V4L2_FIELD_TOP;
2265 }
2266 switch (field) {
2267 case V4L2_FIELD_TOP:
2268 case V4L2_FIELD_BOTTOM:
2269 case V4L2_FIELD_INTERLACED:
2270 break;
2271 default:
2272 return -EINVAL;
2273 }
2274
2275 /* 4-byte alignment. */
2276 if (NULL == fh->ovfmt)
2277 return -EINVAL;
2278 width_mask = ~0;
2279 switch (fh->ovfmt->depth) {
2280 case 8:
2281 case 24:
2282 width_mask = ~3;
2283 break;
2284 case 16:
2285 width_mask = ~1;
2286 break;
2287 case 32:
2288 break;
2289 default:
2290 BUG();
2291 }
2292
2293 win->w.width -= win->w.left & ~width_mask;
2294 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2295
2296 rc = limit_scaled_size(fh, &win->w.width, &win->w.height,
2297 field, width_mask,
2298 /* width_bias: round down */ 0,
2299 adjust_size, adjust_crop);
2300 if (0 != rc)
2301 return rc;
2302
2303 win->field = field;
2304 return 0;
2305 }
2306
2307 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2308 struct v4l2_window *win, int fixup)
2309 {
2310 struct v4l2_clip *clips = NULL;
2311 int n,size,retval = 0;
2312
2313 if (NULL == fh->ovfmt)
2314 return -EINVAL;
2315 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2316 return -EINVAL;
2317 retval = verify_window(fh, win,
2318 /* adjust_size */ fixup,
2319 /* adjust_crop */ fixup);
2320 if (0 != retval)
2321 return retval;
2322
2323 /* copy clips -- luckily v4l1 + v4l2 are binary
2324 compatible here ...*/
2325 n = win->clipcount;
2326 size = sizeof(*clips)*(n+4);
2327 clips = kmalloc(size,GFP_KERNEL);
2328 if (NULL == clips)
2329 return -ENOMEM;
2330 if (n > 0) {
2331 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2332 kfree(clips);
2333 return -EFAULT;
2334 }
2335 }
2336 /* clip against screen */
2337 if (NULL != btv->fbuf.base)
2338 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2339 &win->w, clips, n);
2340 btcx_sort_clips(clips,n);
2341
2342 /* 4-byte alignments */
2343 switch (fh->ovfmt->depth) {
2344 case 8:
2345 case 24:
2346 btcx_align(&win->w, clips, n, 3);
2347 break;
2348 case 16:
2349 btcx_align(&win->w, clips, n, 1);
2350 break;
2351 case 32:
2352 /* no alignment fixups needed */
2353 break;
2354 default:
2355 BUG();
2356 }
2357
2358 mutex_lock(&fh->cap.vb_lock);
2359 kfree(fh->ov.clips);
2360 fh->ov.clips = clips;
2361 fh->ov.nclips = n;
2362
2363 fh->ov.w = win->w;
2364 fh->ov.field = win->field;
2365 fh->ov.setup_ok = 1;
2366 btv->init.ov.w.width = win->w.width;
2367 btv->init.ov.w.height = win->w.height;
2368 btv->init.ov.field = win->field;
2369
2370 /* update overlay if needed */
2371 retval = 0;
2372 if (check_btres(fh, RESOURCE_OVERLAY)) {
2373 struct bttv_buffer *new;
2374
2375 new = videobuf_sg_alloc(sizeof(*new));
2376 new->crop = btv->crop[!!fh->do_crop].rect;
2377 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2378 retval = bttv_switch_overlay(btv,fh,new);
2379 }
2380 mutex_unlock(&fh->cap.vb_lock);
2381 return retval;
2382 }
2383
2384 /* ----------------------------------------------------------------------- */
2385
2386 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2387 {
2388 struct videobuf_queue* q = NULL;
2389
2390 switch (fh->type) {
2391 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2392 q = &fh->cap;
2393 break;
2394 case V4L2_BUF_TYPE_VBI_CAPTURE:
2395 q = &fh->vbi;
2396 break;
2397 default:
2398 BUG();
2399 }
2400 return q;
2401 }
2402
2403 static int bttv_resource(struct bttv_fh *fh)
2404 {
2405 int res = 0;
2406
2407 switch (fh->type) {
2408 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2409 res = RESOURCE_VIDEO_STREAM;
2410 break;
2411 case V4L2_BUF_TYPE_VBI_CAPTURE:
2412 res = RESOURCE_VBI;
2413 break;
2414 default:
2415 BUG();
2416 }
2417 return res;
2418 }
2419
2420 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2421 {
2422 struct videobuf_queue *q = bttv_queue(fh);
2423 int res = bttv_resource(fh);
2424
2425 if (check_btres(fh,res))
2426 return -EBUSY;
2427 if (videobuf_queue_is_busy(q))
2428 return -EBUSY;
2429 fh->type = type;
2430 return 0;
2431 }
2432
2433 static void
2434 pix_format_set_size (struct v4l2_pix_format * f,
2435 const struct bttv_format * fmt,
2436 unsigned int width,
2437 unsigned int height)
2438 {
2439 f->width = width;
2440 f->height = height;
2441
2442 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2443 f->bytesperline = width; /* Y plane */
2444 f->sizeimage = (width * height * fmt->depth) >> 3;
2445 } else {
2446 f->bytesperline = (width * fmt->depth) >> 3;
2447 f->sizeimage = height * f->bytesperline;
2448 }
2449 }
2450
2451 static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
2452 struct v4l2_format *f)
2453 {
2454 struct bttv_fh *fh = priv;
2455
2456 pix_format_set_size(&f->fmt.pix, fh->fmt,
2457 fh->width, fh->height);
2458 f->fmt.pix.field = fh->cap.field;
2459 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2460
2461 return 0;
2462 }
2463
2464 static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
2465 struct v4l2_format *f)
2466 {
2467 struct bttv_fh *fh = priv;
2468
2469 f->fmt.win.w = fh->ov.w;
2470 f->fmt.win.field = fh->ov.field;
2471
2472 return 0;
2473 }
2474
2475 static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2476 struct v4l2_format *f)
2477 {
2478 const struct bttv_format *fmt;
2479 struct bttv_fh *fh = priv;
2480 struct bttv *btv = fh->btv;
2481 enum v4l2_field field;
2482 __s32 width, height;
2483 int rc;
2484
2485 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2486 if (NULL == fmt)
2487 return -EINVAL;
2488
2489 field = f->fmt.pix.field;
2490
2491 if (V4L2_FIELD_ANY == field) {
2492 __s32 height2;
2493
2494 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2495 field = (f->fmt.pix.height > height2)
2496 ? V4L2_FIELD_INTERLACED
2497 : V4L2_FIELD_BOTTOM;
2498 }
2499
2500 if (V4L2_FIELD_SEQ_BT == field)
2501 field = V4L2_FIELD_SEQ_TB;
2502
2503 switch (field) {
2504 case V4L2_FIELD_TOP:
2505 case V4L2_FIELD_BOTTOM:
2506 case V4L2_FIELD_ALTERNATE:
2507 case V4L2_FIELD_INTERLACED:
2508 break;
2509 case V4L2_FIELD_SEQ_TB:
2510 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2511 return -EINVAL;
2512 break;
2513 default:
2514 return -EINVAL;
2515 }
2516
2517 width = f->fmt.pix.width;
2518 height = f->fmt.pix.height;
2519
2520 rc = limit_scaled_size(fh, &width, &height, field,
2521 /* width_mask: 4 pixels */ ~3,
2522 /* width_bias: nearest */ 2,
2523 /* adjust_size */ 1,
2524 /* adjust_crop */ 0);
2525 if (0 != rc)
2526 return rc;
2527
2528 /* update data for the application */
2529 f->fmt.pix.field = field;
2530 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2531
2532 return 0;
2533 }
2534
2535 static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
2536 struct v4l2_format *f)
2537 {
2538 struct bttv_fh *fh = priv;
2539
2540 return verify_window(fh, &f->fmt.win,
2541 /* adjust_size */ 1,
2542 /* adjust_crop */ 0);
2543 }
2544
2545 static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2546 struct v4l2_format *f)
2547 {
2548 int retval;
2549 const struct bttv_format *fmt;
2550 struct bttv_fh *fh = priv;
2551 struct bttv *btv = fh->btv;
2552 __s32 width, height;
2553 enum v4l2_field field;
2554
2555 retval = bttv_switch_type(fh, f->type);
2556 if (0 != retval)
2557 return retval;
2558
2559 retval = bttv_try_fmt_vid_cap(file, priv, f);
2560 if (0 != retval)
2561 return retval;
2562
2563 width = f->fmt.pix.width;
2564 height = f->fmt.pix.height;
2565 field = f->fmt.pix.field;
2566
2567 retval = limit_scaled_size(fh, &width, &height, f->fmt.pix.field,
2568 /* width_mask: 4 pixels */ ~3,
2569 /* width_bias: nearest */ 2,
2570 /* adjust_size */ 1,
2571 /* adjust_crop */ 1);
2572 if (0 != retval)
2573 return retval;
2574
2575 f->fmt.pix.field = field;
2576
2577 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2578
2579 /* update our state informations */
2580 mutex_lock(&fh->cap.vb_lock);
2581 fh->fmt = fmt;
2582 fh->cap.field = f->fmt.pix.field;
2583 fh->cap.last = V4L2_FIELD_NONE;
2584 fh->width = f->fmt.pix.width;
2585 fh->height = f->fmt.pix.height;
2586 btv->init.fmt = fmt;
2587 btv->init.width = f->fmt.pix.width;
2588 btv->init.height = f->fmt.pix.height;
2589 mutex_unlock(&fh->cap.vb_lock);
2590
2591 return 0;
2592 }
2593
2594 static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
2595 struct v4l2_format *f)
2596 {
2597 struct bttv_fh *fh = priv;
2598 struct bttv *btv = fh->btv;
2599
2600 if (no_overlay > 0) {
2601 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2602 return -EINVAL;
2603 }
2604
2605 return setup_window(fh, btv, &f->fmt.win, 1);
2606 }
2607
2608 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2609 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2610 {
2611 int retval;
2612 unsigned int i;
2613 struct bttv_fh *fh = priv;
2614
2615 mutex_lock(&fh->cap.vb_lock);
2616 retval = __videobuf_mmap_setup(&fh->cap, gbuffers, gbufsize,
2617 V4L2_MEMORY_MMAP);
2618 if (retval < 0) {
2619 mutex_unlock(&fh->cap.vb_lock);
2620 return retval;
2621 }
2622
2623 gbuffers = retval;
2624 memset(mbuf, 0, sizeof(*mbuf));
2625 mbuf->frames = gbuffers;
2626 mbuf->size = gbuffers * gbufsize;
2627
2628 for (i = 0; i < gbuffers; i++)
2629 mbuf->offsets[i] = i * gbufsize;
2630
2631 mutex_unlock(&fh->cap.vb_lock);
2632 return 0;
2633 }
2634 #endif
2635
2636 static int bttv_querycap(struct file *file, void *priv,
2637 struct v4l2_capability *cap)
2638 {
2639 struct bttv_fh *fh = priv;
2640 struct bttv *btv = fh->btv;
2641
2642 if (0 == v4l2)
2643 return -EINVAL;
2644
2645 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2646 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2647 snprintf(cap->bus_info, sizeof(cap->bus_info),
2648 "PCI:%s", pci_name(btv->c.pci));
2649 cap->version = BTTV_VERSION_CODE;
2650 cap->capabilities =
2651 V4L2_CAP_VIDEO_CAPTURE |
2652 V4L2_CAP_VBI_CAPTURE |
2653 V4L2_CAP_READWRITE |
2654 V4L2_CAP_STREAMING;
2655 if (no_overlay <= 0)
2656 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2657
2658 if (btv->tuner_type != TUNER_ABSENT)
2659 cap->capabilities |= V4L2_CAP_TUNER;
2660 return 0;
2661 }
2662
2663 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2664 {
2665 int index = -1, i;
2666
2667 for (i = 0; i < FORMATS; i++) {
2668 if (formats[i].fourcc != -1)
2669 index++;
2670 if ((unsigned int)index == f->index)
2671 break;
2672 }
2673 if (FORMATS == i)
2674 return -EINVAL;
2675
2676 f->pixelformat = formats[i].fourcc;
2677 strlcpy(f->description, formats[i].name, sizeof(f->description));
2678
2679 return i;
2680 }
2681
2682 static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
2683 struct v4l2_fmtdesc *f)
2684 {
2685 int rc = bttv_enum_fmt_cap_ovr(f);
2686
2687 if (rc < 0)
2688 return rc;
2689
2690 return 0;
2691 }
2692
2693 static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
2694 struct v4l2_fmtdesc *f)
2695 {
2696 int rc;
2697
2698 if (no_overlay > 0) {
2699 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2700 return -EINVAL;
2701 }
2702
2703 rc = bttv_enum_fmt_cap_ovr(f);
2704
2705 if (rc < 0)
2706 return rc;
2707
2708 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
2709 return -EINVAL;
2710
2711 return 0;
2712 }
2713
2714 static int bttv_g_fbuf(struct file *file, void *f,
2715 struct v4l2_framebuffer *fb)
2716 {
2717 struct bttv_fh *fh = f;
2718 struct bttv *btv = fh->btv;
2719
2720 *fb = btv->fbuf;
2721 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2722 if (fh->ovfmt)
2723 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2724 return 0;
2725 }
2726
2727 static int bttv_overlay(struct file *file, void *f, unsigned int on)
2728 {
2729 struct bttv_fh *fh = f;
2730 struct bttv *btv = fh->btv;
2731 struct bttv_buffer *new;
2732 int retval;
2733
2734 if (on) {
2735 /* verify args */
2736 if (NULL == btv->fbuf.base)
2737 return -EINVAL;
2738 if (!fh->ov.setup_ok) {
2739 dprintk("bttv%d: overlay: !setup_ok\n", btv->c.nr);
2740 return -EINVAL;
2741 }
2742 }
2743
2744 if (!check_alloc_btres(btv, fh, RESOURCE_OVERLAY))
2745 return -EBUSY;
2746
2747 mutex_lock(&fh->cap.vb_lock);
2748 if (on) {
2749 fh->ov.tvnorm = btv->tvnorm;
2750 new = videobuf_sg_alloc(sizeof(*new));
2751 new->crop = btv->crop[!!fh->do_crop].rect;
2752 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2753 } else {
2754 new = NULL;
2755 }
2756
2757 /* switch over */
2758 retval = bttv_switch_overlay(btv, fh, new);
2759 mutex_unlock(&fh->cap.vb_lock);
2760 return retval;
2761 }
2762
2763 static int bttv_s_fbuf(struct file *file, void *f,
2764 struct v4l2_framebuffer *fb)
2765 {
2766 struct bttv_fh *fh = f;
2767 struct bttv *btv = fh->btv;
2768 const struct bttv_format *fmt;
2769 int retval;
2770
2771 if (!capable(CAP_SYS_ADMIN) &&
2772 !capable(CAP_SYS_RAWIO))
2773 return -EPERM;
2774
2775 /* check args */
2776 fmt = format_by_fourcc(fb->fmt.pixelformat);
2777 if (NULL == fmt)
2778 return -EINVAL;
2779 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2780 return -EINVAL;
2781
2782 retval = -EINVAL;
2783 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2784 __s32 width = fb->fmt.width;
2785 __s32 height = fb->fmt.height;
2786
2787 retval = limit_scaled_size(fh, &width, &height,
2788 V4L2_FIELD_INTERLACED,
2789 /* width_mask */ ~3,
2790 /* width_bias */ 2,
2791 /* adjust_size */ 0,
2792 /* adjust_crop */ 0);
2793 if (0 != retval)
2794 return retval;
2795 }
2796
2797 /* ok, accept it */
2798 mutex_lock(&fh->cap.vb_lock);
2799 btv->fbuf.base = fb->base;
2800 btv->fbuf.fmt.width = fb->fmt.width;
2801 btv->fbuf.fmt.height = fb->fmt.height;
2802 if (0 != fb->fmt.bytesperline)
2803 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2804 else
2805 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2806
2807 retval = 0;
2808 fh->ovfmt = fmt;
2809 btv->init.ovfmt = fmt;
2810 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2811 fh->ov.w.left = 0;
2812 fh->ov.w.top = 0;
2813 fh->ov.w.width = fb->fmt.width;
2814 fh->ov.w.height = fb->fmt.height;
2815 btv->init.ov.w.width = fb->fmt.width;
2816 btv->init.ov.w.height = fb->fmt.height;
2817 kfree(fh->ov.clips);
2818 fh->ov.clips = NULL;
2819 fh->ov.nclips = 0;
2820
2821 if (check_btres(fh, RESOURCE_OVERLAY)) {
2822 struct bttv_buffer *new;
2823
2824 new = videobuf_sg_alloc(sizeof(*new));
2825 new->crop = btv->crop[!!fh->do_crop].rect;
2826 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2827 retval = bttv_switch_overlay(btv, fh, new);
2828 }
2829 }
2830 mutex_unlock(&fh->cap.vb_lock);
2831 return retval;
2832 }
2833
2834 static int bttv_reqbufs(struct file *file, void *priv,
2835 struct v4l2_requestbuffers *p)
2836 {
2837 struct bttv_fh *fh = priv;
2838 return videobuf_reqbufs(bttv_queue(fh), p);
2839 }
2840
2841 static int bttv_querybuf(struct file *file, void *priv,
2842 struct v4l2_buffer *b)
2843 {
2844 struct bttv_fh *fh = priv;
2845 return videobuf_querybuf(bttv_queue(fh), b);
2846 }
2847
2848 static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2849 {
2850 struct bttv_fh *fh = priv;
2851 struct bttv *btv = fh->btv;
2852 int res = bttv_resource(fh);
2853
2854 if (!check_alloc_btres(btv, fh, res))
2855 return -EBUSY;
2856
2857 return videobuf_qbuf(bttv_queue(fh), b);
2858 }
2859
2860 static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2861 {
2862 struct bttv_fh *fh = priv;
2863 return videobuf_dqbuf(bttv_queue(fh), b,
2864 file->f_flags & O_NONBLOCK);
2865 }
2866
2867 static int bttv_streamon(struct file *file, void *priv,
2868 enum v4l2_buf_type type)
2869 {
2870 struct bttv_fh *fh = priv;
2871 struct bttv *btv = fh->btv;
2872 int res = bttv_resource(fh);
2873
2874 if (!check_alloc_btres(btv, fh, res))
2875 return -EBUSY;
2876 return videobuf_streamon(bttv_queue(fh));
2877 }
2878
2879
2880 static int bttv_streamoff(struct file *file, void *priv,
2881 enum v4l2_buf_type type)
2882 {
2883 struct bttv_fh *fh = priv;
2884 struct bttv *btv = fh->btv;
2885 int retval;
2886 int res = bttv_resource(fh);
2887
2888
2889 retval = videobuf_streamoff(bttv_queue(fh));
2890 if (retval < 0)
2891 return retval;
2892 free_btres(btv, fh, res);
2893 return 0;
2894 }
2895
2896 static int bttv_queryctrl(struct file *file, void *priv,
2897 struct v4l2_queryctrl *c)
2898 {
2899 struct bttv_fh *fh = priv;
2900 struct bttv *btv = fh->btv;
2901 const struct v4l2_queryctrl *ctrl;
2902
2903 if ((c->id < V4L2_CID_BASE ||
2904 c->id >= V4L2_CID_LASTP1) &&
2905 (c->id < V4L2_CID_PRIVATE_BASE ||
2906 c->id >= V4L2_CID_PRIVATE_LASTP1))
2907 return -EINVAL;
2908
2909 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2910 *c = no_ctl;
2911 else {
2912 ctrl = ctrl_by_id(c->id);
2913
2914 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2915 }
2916
2917 return 0;
2918 }
2919
2920 static int bttv_g_parm(struct file *file, void *f,
2921 struct v4l2_streamparm *parm)
2922 {
2923 struct bttv_fh *fh = f;
2924 struct bttv *btv = fh->btv;
2925
2926 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2927 &parm->parm.capture.timeperframe);
2928 return 0;
2929 }
2930
2931 static int bttv_g_tuner(struct file *file, void *priv,
2932 struct v4l2_tuner *t)
2933 {
2934 struct bttv_fh *fh = priv;
2935 struct bttv *btv = fh->btv;
2936
2937 if (btv->tuner_type == TUNER_ABSENT)
2938 return -EINVAL;
2939 if (0 != t->index)
2940 return -EINVAL;
2941
2942 mutex_lock(&btv->lock);
2943 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2944 bttv_call_all(btv, tuner, g_tuner, t);
2945 strcpy(t->name, "Television");
2946 t->capability = V4L2_TUNER_CAP_NORM;
2947 t->type = V4L2_TUNER_ANALOG_TV;
2948 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2949 t->signal = 0xffff;
2950
2951 if (btv->audio_mode_gpio)
2952 btv->audio_mode_gpio(btv, t, 0);
2953
2954 mutex_unlock(&btv->lock);
2955 return 0;
2956 }
2957
2958 static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2959 {
2960 struct bttv_fh *fh = f;
2961 struct bttv *btv = fh->btv;
2962
2963 *p = v4l2_prio_max(&btv->prio);
2964
2965 return 0;
2966 }
2967
2968 static int bttv_s_priority(struct file *file, void *f,
2969 enum v4l2_priority prio)
2970 {
2971 struct bttv_fh *fh = f;
2972 struct bttv *btv = fh->btv;
2973
2974 return v4l2_prio_change(&btv->prio, &fh->prio, prio);
2975 }
2976
2977 static int bttv_cropcap(struct file *file, void *priv,
2978 struct v4l2_cropcap *cap)
2979 {
2980 struct bttv_fh *fh = priv;
2981 struct bttv *btv = fh->btv;
2982
2983 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2984 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2985 return -EINVAL;
2986
2987 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
2988
2989 return 0;
2990 }
2991
2992 static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
2993 {
2994 struct bttv_fh *fh = f;
2995 struct bttv *btv = fh->btv;
2996
2997 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2998 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2999 return -EINVAL;
3000
3001 /* No fh->do_crop = 1; because btv->crop[1] may be
3002 inconsistent with fh->width or fh->height and apps
3003 do not expect a change here. */
3004
3005 crop->c = btv->crop[!!fh->do_crop].rect;
3006
3007 return 0;
3008 }
3009
3010 static int bttv_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
3011 {
3012 struct bttv_fh *fh = f;
3013 struct bttv *btv = fh->btv;
3014 const struct v4l2_rect *b;
3015 int retval;
3016 struct bttv_crop c;
3017 __s32 b_left;
3018 __s32 b_top;
3019 __s32 b_right;
3020 __s32 b_bottom;
3021
3022 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3023 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3024 return -EINVAL;
3025
3026 retval = v4l2_prio_check(&btv->prio, &fh->prio);
3027 if (0 != retval)
3028 return retval;
3029
3030 /* Make sure tvnorm, vbi_end and the current cropping
3031 parameters remain consistent until we're done. Note
3032 read() may change vbi_end in check_alloc_btres(). */
3033 mutex_lock(&btv->lock);
3034
3035 retval = -EBUSY;
3036
3037 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
3038 mutex_unlock(&btv->lock);
3039 return retval;
3040 }
3041
3042 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3043
3044 b_left = b->left;
3045 b_right = b_left + b->width;
3046 b_bottom = b->top + b->height;
3047
3048 b_top = max(b->top, btv->vbi_end);
3049 if (b_top + 32 >= b_bottom) {
3050 mutex_unlock(&btv->lock);
3051 return retval;
3052 }
3053
3054 /* Min. scaled size 48 x 32. */
3055 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3056 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3057
3058 c.rect.width = clamp(crop->c.width,
3059 48, b_right - c.rect.left);
3060
3061 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3062 /* Top and height must be a multiple of two. */
3063 c.rect.top = (c.rect.top + 1) & ~1;
3064
3065 c.rect.height = clamp(crop->c.height,
3066 32, b_bottom - c.rect.top);
3067 c.rect.height = (c.rect.height + 1) & ~1;
3068
3069 bttv_crop_calc_limits(&c);
3070
3071 btv->crop[1] = c;
3072
3073 mutex_unlock(&btv->lock);
3074
3075 fh->do_crop = 1;
3076
3077 mutex_lock(&fh->cap.vb_lock);
3078
3079 if (fh->width < c.min_scaled_width) {
3080 fh->width = c.min_scaled_width;
3081 btv->init.width = c.min_scaled_width;
3082 } else if (fh->width > c.max_scaled_width) {
3083 fh->width = c.max_scaled_width;
3084 btv->init.width = c.max_scaled_width;
3085 }
3086
3087 if (fh->height < c.min_scaled_height) {
3088 fh->height = c.min_scaled_height;
3089 btv->init.height = c.min_scaled_height;
3090 } else if (fh->height > c.max_scaled_height) {
3091 fh->height = c.max_scaled_height;
3092 btv->init.height = c.max_scaled_height;
3093 }
3094
3095 mutex_unlock(&fh->cap.vb_lock);
3096
3097 return 0;
3098 }
3099
3100 static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
3101 {
3102 if (unlikely(a->index))
3103 return -EINVAL;
3104
3105 strcpy(a->name, "audio");
3106 return 0;
3107 }
3108
3109 static int bttv_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
3110 {
3111 if (unlikely(a->index))
3112 return -EINVAL;
3113
3114 return 0;
3115 }
3116
3117 static ssize_t bttv_read(struct file *file, char __user *data,
3118 size_t count, loff_t *ppos)
3119 {
3120 struct bttv_fh *fh = file->private_data;
3121 int retval = 0;
3122
3123 if (fh->btv->errors)
3124 bttv_reinit_bt848(fh->btv);
3125 dprintk("bttv%d: read count=%d type=%s\n",
3126 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3127
3128 switch (fh->type) {
3129 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3130 if (!check_alloc_btres(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3131 /* VIDEO_READ in use by another fh,
3132 or VIDEO_STREAM by any fh. */
3133 return -EBUSY;
3134 }
3135 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3136 file->f_flags & O_NONBLOCK);
3137 free_btres(fh->btv, fh, RESOURCE_VIDEO_READ);
3138 break;
3139 case V4L2_BUF_TYPE_VBI_CAPTURE:
3140 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3141 return -EBUSY;
3142 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3143 file->f_flags & O_NONBLOCK);
3144 break;
3145 default:
3146 BUG();
3147 }
3148 return retval;
3149 }
3150
3151 static unsigned int bttv_poll(struct file *file, poll_table *wait)
3152 {
3153 struct bttv_fh *fh = file->private_data;
3154 struct bttv_buffer *buf;
3155 enum v4l2_field field;
3156 unsigned int rc = POLLERR;
3157
3158 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3159 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3160 return POLLERR;
3161 return videobuf_poll_stream(file, &fh->vbi, wait);
3162 }
3163
3164 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
3165 mutex_lock(&fh->cap.vb_lock);
3166 /* streaming capture */
3167 if (list_empty(&fh->cap.stream))
3168 goto err;
3169 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3170 } else {
3171 /* read() capture */
3172 mutex_lock(&fh->cap.vb_lock);
3173 if (NULL == fh->cap.read_buf) {
3174 /* need to capture a new frame */
3175 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3176 goto err;
3177 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
3178 if (NULL == fh->cap.read_buf)
3179 goto err;
3180 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3181 field = videobuf_next_field(&fh->cap);
3182 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
3183 kfree (fh->cap.read_buf);
3184 fh->cap.read_buf = NULL;
3185 goto err;
3186 }
3187 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3188 fh->cap.read_off = 0;
3189 }
3190 mutex_unlock(&fh->cap.vb_lock);
3191 buf = (struct bttv_buffer*)fh->cap.read_buf;
3192 }
3193
3194 poll_wait(file, &buf->vb.done, wait);
3195 if (buf->vb.state == VIDEOBUF_DONE ||
3196 buf->vb.state == VIDEOBUF_ERROR)
3197 rc = POLLIN|POLLRDNORM;
3198 else
3199 rc = 0;
3200 err:
3201 mutex_unlock(&fh->cap.vb_lock);
3202 return rc;
3203 }
3204
3205 static int bttv_open(struct file *file)
3206 {
3207 int minor = video_devdata(file)->minor;
3208 struct bttv *btv = video_drvdata(file);
3209 struct bttv_fh *fh;
3210 enum v4l2_buf_type type = 0;
3211
3212 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3213
3214 lock_kernel();
3215 if (btv->video_dev->minor == minor) {
3216 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3217 } else if (btv->vbi_dev->minor == minor) {
3218 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3219 } else {
3220 WARN_ON(1);
3221 unlock_kernel();
3222 return -ENODEV;
3223 }
3224
3225 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3226 btv->c.nr,v4l2_type_names[type]);
3227
3228 /* allocate per filehandle data */
3229 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3230 if (NULL == fh) {
3231 unlock_kernel();
3232 return -ENOMEM;
3233 }
3234 file->private_data = fh;
3235 *fh = btv->init;
3236 fh->type = type;
3237 fh->ov.setup_ok = 0;
3238 v4l2_prio_open(&btv->prio,&fh->prio);
3239
3240 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3241 &btv->c.pci->dev, &btv->s_lock,
3242 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3243 V4L2_FIELD_INTERLACED,
3244 sizeof(struct bttv_buffer),
3245 fh);
3246 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3247 &btv->c.pci->dev, &btv->s_lock,
3248 V4L2_BUF_TYPE_VBI_CAPTURE,
3249 V4L2_FIELD_SEQ_TB,
3250 sizeof(struct bttv_buffer),
3251 fh);
3252 set_tvnorm(btv,btv->tvnorm);
3253 set_input(btv, btv->input, btv->tvnorm);
3254
3255 btv->users++;
3256
3257 /* The V4L2 spec requires one global set of cropping parameters
3258 which only change on request. These are stored in btv->crop[1].
3259 However for compatibility with V4L apps and cropping unaware
3260 V4L2 apps we now reset the cropping parameters as seen through
3261 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3262 will use btv->crop[0], the default cropping parameters for the
3263 current video standard, and VIDIOC_S_FMT will not implicitely
3264 change the cropping parameters until VIDIOC_S_CROP has been
3265 called. */
3266 fh->do_crop = !reset_crop; /* module parameter */
3267
3268 /* Likewise there should be one global set of VBI capture
3269 parameters, but for compatibility with V4L apps and earlier
3270 driver versions each fh has its own parameters. */
3271 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3272
3273 bttv_field_count(btv);
3274 unlock_kernel();
3275 return 0;
3276 }
3277
3278 static int bttv_release(struct file *file)
3279 {
3280 struct bttv_fh *fh = file->private_data;
3281 struct bttv *btv = fh->btv;
3282
3283 /* turn off overlay */
3284 if (check_btres(fh, RESOURCE_OVERLAY))
3285 bttv_switch_overlay(btv,fh,NULL);
3286
3287 /* stop video capture */
3288 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
3289 videobuf_streamoff(&fh->cap);
3290 free_btres(btv,fh,RESOURCE_VIDEO_STREAM);
3291 }
3292 if (fh->cap.read_buf) {
3293 buffer_release(&fh->cap,fh->cap.read_buf);
3294 kfree(fh->cap.read_buf);
3295 }
3296 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3297 free_btres(btv, fh, RESOURCE_VIDEO_READ);
3298 }
3299
3300 /* stop vbi capture */
3301 if (check_btres(fh, RESOURCE_VBI)) {
3302 videobuf_stop(&fh->vbi);
3303 free_btres(btv,fh,RESOURCE_VBI);
3304 }
3305
3306 /* free stuff */
3307 videobuf_mmap_free(&fh->cap);
3308 videobuf_mmap_free(&fh->vbi);
3309 v4l2_prio_close(&btv->prio,&fh->prio);
3310 file->private_data = NULL;
3311 kfree(fh);
3312
3313 btv->users--;
3314 bttv_field_count(btv);
3315
3316 if (!btv->users)
3317 audio_mute(btv, 1);
3318
3319 return 0;
3320 }
3321
3322 static int
3323 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3324 {
3325 struct bttv_fh *fh = file->private_data;
3326
3327 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3328 fh->btv->c.nr, v4l2_type_names[fh->type],
3329 vma->vm_start, vma->vm_end - vma->vm_start);
3330 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3331 }
3332
3333 static const struct v4l2_file_operations bttv_fops =
3334 {
3335 .owner = THIS_MODULE,
3336 .open = bttv_open,
3337 .release = bttv_release,
3338 .ioctl = video_ioctl2,
3339 .read = bttv_read,
3340 .mmap = bttv_mmap,
3341 .poll = bttv_poll,
3342 };
3343
3344 static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
3345 .vidioc_querycap = bttv_querycap,
3346 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3347 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3348 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3349 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3350 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3351 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3352 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3353 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
3354 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3355 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3356 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
3357 .vidioc_g_audio = bttv_g_audio,
3358 .vidioc_s_audio = bttv_s_audio,
3359 .vidioc_cropcap = bttv_cropcap,
3360 .vidioc_reqbufs = bttv_reqbufs,
3361 .vidioc_querybuf = bttv_querybuf,
3362 .vidioc_qbuf = bttv_qbuf,
3363 .vidioc_dqbuf = bttv_dqbuf,
3364 .vidioc_s_std = bttv_s_std,
3365 .vidioc_enum_input = bttv_enum_input,
3366 .vidioc_g_input = bttv_g_input,
3367 .vidioc_s_input = bttv_s_input,
3368 .vidioc_queryctrl = bttv_queryctrl,
3369 .vidioc_g_ctrl = bttv_g_ctrl,
3370 .vidioc_s_ctrl = bttv_s_ctrl,
3371 .vidioc_streamon = bttv_streamon,
3372 .vidioc_streamoff = bttv_streamoff,
3373 .vidioc_g_tuner = bttv_g_tuner,
3374 .vidioc_s_tuner = bttv_s_tuner,
3375 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3376 .vidiocgmbuf = vidiocgmbuf,
3377 #endif
3378 .vidioc_g_crop = bttv_g_crop,
3379 .vidioc_s_crop = bttv_s_crop,
3380 .vidioc_g_fbuf = bttv_g_fbuf,
3381 .vidioc_s_fbuf = bttv_s_fbuf,
3382 .vidioc_overlay = bttv_overlay,
3383 .vidioc_g_priority = bttv_g_priority,
3384 .vidioc_s_priority = bttv_s_priority,
3385 .vidioc_g_parm = bttv_g_parm,
3386 .vidioc_g_frequency = bttv_g_frequency,
3387 .vidioc_s_frequency = bttv_s_frequency,
3388 .vidioc_log_status = bttv_log_status,
3389 .vidioc_querystd = bttv_querystd,
3390 #ifdef CONFIG_VIDEO_ADV_DEBUG
3391 .vidioc_g_register = bttv_g_register,
3392 .vidioc_s_register = bttv_s_register,
3393 #endif
3394 };
3395
3396 static struct video_device bttv_video_template = {
3397 .fops = &bttv_fops,
3398 .minor = -1,
3399 .ioctl_ops = &bttv_ioctl_ops,
3400 .tvnorms = BTTV_NORMS,
3401 .current_norm = V4L2_STD_PAL,
3402 };
3403
3404 /* ----------------------------------------------------------------------- */
3405 /* radio interface */
3406
3407 static int radio_open(struct file *file)
3408 {
3409 int minor = video_devdata(file)->minor;
3410 struct bttv *btv = video_drvdata(file);
3411 struct bttv_fh *fh;
3412
3413 dprintk("bttv: open minor=%d\n",minor);
3414
3415 lock_kernel();
3416 WARN_ON(btv->radio_dev && btv->radio_dev->minor != minor);
3417 if (!btv->radio_dev || btv->radio_dev->minor != minor) {
3418 unlock_kernel();
3419 return -ENODEV;
3420 }
3421
3422 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3423
3424 /* allocate per filehandle data */
3425 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3426 if (NULL == fh) {
3427 unlock_kernel();
3428 return -ENOMEM;
3429 }
3430 file->private_data = fh;
3431 *fh = btv->init;
3432 v4l2_prio_open(&btv->prio, &fh->prio);
3433
3434 mutex_lock(&btv->lock);
3435
3436 btv->radio_user++;
3437
3438 bttv_call_all(btv, tuner, s_radio);
3439 audio_input(btv,TVAUDIO_INPUT_RADIO);
3440
3441 mutex_unlock(&btv->lock);
3442 unlock_kernel();
3443 return 0;
3444 }
3445
3446 static int radio_release(struct file *file)
3447 {
3448 struct bttv_fh *fh = file->private_data;
3449 struct bttv *btv = fh->btv;
3450 struct rds_command cmd;
3451
3452 v4l2_prio_close(&btv->prio,&fh->prio);
3453 file->private_data = NULL;
3454 kfree(fh);
3455
3456 btv->radio_user--;
3457
3458 bttv_call_all(btv, core, ioctl, RDS_CMD_CLOSE, &cmd);
3459
3460 return 0;
3461 }
3462
3463 static int radio_querycap(struct file *file, void *priv,
3464 struct v4l2_capability *cap)
3465 {
3466 struct bttv_fh *fh = priv;
3467 struct bttv *btv = fh->btv;
3468
3469 strcpy(cap->driver, "bttv");
3470 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
3471 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
3472 cap->version = BTTV_VERSION_CODE;
3473 cap->capabilities = V4L2_CAP_TUNER;
3474
3475 return 0;
3476 }
3477
3478 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
3479 {
3480 struct bttv_fh *fh = priv;
3481 struct bttv *btv = fh->btv;
3482
3483 if (btv->tuner_type == TUNER_ABSENT)
3484 return -EINVAL;
3485 if (0 != t->index)
3486 return -EINVAL;
3487 mutex_lock(&btv->lock);
3488 strcpy(t->name, "Radio");
3489 t->type = V4L2_TUNER_RADIO;
3490
3491 bttv_call_all(btv, tuner, g_tuner, t);
3492
3493 if (btv->audio_mode_gpio)
3494 btv->audio_mode_gpio(btv, t, 0);
3495
3496 mutex_unlock(&btv->lock);
3497
3498 return 0;
3499 }
3500
3501 static int radio_enum_input(struct file *file, void *priv,
3502 struct v4l2_input *i)
3503 {
3504 if (i->index != 0)
3505 return -EINVAL;
3506
3507 strcpy(i->name, "Radio");
3508 i->type = V4L2_INPUT_TYPE_TUNER;
3509
3510 return 0;
3511 }
3512
3513 static int radio_g_audio(struct file *file, void *priv,
3514 struct v4l2_audio *a)
3515 {
3516 if (unlikely(a->index))
3517 return -EINVAL;
3518
3519 strcpy(a->name, "Radio");
3520
3521 return 0;
3522 }
3523
3524 static int radio_s_tuner(struct file *file, void *priv,
3525 struct v4l2_tuner *t)
3526 {
3527 struct bttv_fh *fh = priv;
3528 struct bttv *btv = fh->btv;
3529
3530 if (0 != t->index)
3531 return -EINVAL;
3532
3533 bttv_call_all(btv, tuner, g_tuner, t);
3534 return 0;
3535 }
3536
3537 static int radio_s_audio(struct file *file, void *priv,
3538 struct v4l2_audio *a)
3539 {
3540 if (unlikely(a->index))
3541 return -EINVAL;
3542
3543 return 0;
3544 }
3545
3546 static int radio_s_input(struct file *filp, void *priv, unsigned int i)
3547 {
3548 if (unlikely(i))
3549 return -EINVAL;
3550
3551 return 0;
3552 }
3553
3554 static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
3555 {
3556 return 0;
3557 }
3558
3559 static int radio_queryctrl(struct file *file, void *priv,
3560 struct v4l2_queryctrl *c)
3561 {
3562 const struct v4l2_queryctrl *ctrl;
3563
3564 if (c->id < V4L2_CID_BASE ||
3565 c->id >= V4L2_CID_LASTP1)
3566 return -EINVAL;
3567
3568 if (c->id == V4L2_CID_AUDIO_MUTE) {
3569 ctrl = ctrl_by_id(c->id);
3570 *c = *ctrl;
3571 } else
3572 *c = no_ctl;
3573
3574 return 0;
3575 }
3576
3577 static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
3578 {
3579 *i = 0;
3580 return 0;
3581 }
3582
3583 static ssize_t radio_read(struct file *file, char __user *data,
3584 size_t count, loff_t *ppos)
3585 {
3586 struct bttv_fh *fh = file->private_data;
3587 struct bttv *btv = fh->btv;
3588 struct rds_command cmd;
3589 cmd.block_count = count/3;
3590 cmd.buffer = data;
3591 cmd.instance = file;
3592 cmd.result = -ENODEV;
3593
3594 bttv_call_all(btv, core, ioctl, RDS_CMD_READ, &cmd);
3595
3596 return cmd.result;
3597 }
3598
3599 static unsigned int radio_poll(struct file *file, poll_table *wait)
3600 {
3601 struct bttv_fh *fh = file->private_data;
3602 struct bttv *btv = fh->btv;
3603 struct rds_command cmd;
3604 cmd.instance = file;
3605 cmd.event_list = wait;
3606 cmd.result = -ENODEV;
3607 bttv_call_all(btv, core, ioctl, RDS_CMD_POLL, &cmd);
3608
3609 return cmd.result;
3610 }
3611
3612 static const struct v4l2_file_operations radio_fops =
3613 {
3614 .owner = THIS_MODULE,
3615 .open = radio_open,
3616 .read = radio_read,
3617 .release = radio_release,
3618 .ioctl = video_ioctl2,
3619 .poll = radio_poll,
3620 };
3621
3622 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
3623 .vidioc_querycap = radio_querycap,
3624 .vidioc_g_tuner = radio_g_tuner,
3625 .vidioc_enum_input = radio_enum_input,
3626 .vidioc_g_audio = radio_g_audio,
3627 .vidioc_s_tuner = radio_s_tuner,
3628 .vidioc_s_audio = radio_s_audio,
3629 .vidioc_s_input = radio_s_input,
3630 .vidioc_s_std = radio_s_std,
3631 .vidioc_queryctrl = radio_queryctrl,
3632 .vidioc_g_input = radio_g_input,
3633 .vidioc_g_ctrl = bttv_g_ctrl,
3634 .vidioc_s_ctrl = bttv_s_ctrl,
3635 .vidioc_g_frequency = bttv_g_frequency,
3636 .vidioc_s_frequency = bttv_s_frequency,
3637 };
3638
3639 static struct video_device radio_template = {
3640 .fops = &radio_fops,
3641 .minor = -1,
3642 .ioctl_ops = &radio_ioctl_ops,
3643 };
3644
3645 /* ----------------------------------------------------------------------- */
3646 /* some debug code */
3647
3648 static int bttv_risc_decode(u32 risc)
3649 {
3650 static char *instr[16] = {
3651 [ BT848_RISC_WRITE >> 28 ] = "write",
3652 [ BT848_RISC_SKIP >> 28 ] = "skip",
3653 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3654 [ BT848_RISC_JUMP >> 28 ] = "jump",
3655 [ BT848_RISC_SYNC >> 28 ] = "sync",
3656 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3657 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3658 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3659 };
3660 static int incr[16] = {
3661 [ BT848_RISC_WRITE >> 28 ] = 2,
3662 [ BT848_RISC_JUMP >> 28 ] = 2,
3663 [ BT848_RISC_SYNC >> 28 ] = 2,
3664 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3665 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3666 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3667 };
3668 static char *bits[] = {
3669 "be0", "be1", "be2", "be3/resync",
3670 "set0", "set1", "set2", "set3",
3671 "clr0", "clr1", "clr2", "clr3",
3672 "irq", "res", "eol", "sol",
3673 };
3674 int i;
3675
3676 printk("0x%08x [ %s", risc,
3677 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3678 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3679 if (risc & (1 << (i + 12)))
3680 printk(" %s",bits[i]);
3681 printk(" count=%d ]\n", risc & 0xfff);
3682 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3683 }
3684
3685 static void bttv_risc_disasm(struct bttv *btv,
3686 struct btcx_riscmem *risc)
3687 {
3688 unsigned int i,j,n;
3689
3690 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3691 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
3692 for (i = 0; i < (risc->size >> 2); i += n) {
3693 printk("%s: 0x%lx: ", btv->c.v4l2_dev.name,
3694 (unsigned long)(risc->dma + (i<<2)));
3695 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
3696 for (j = 1; j < n; j++)
3697 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3698 btv->c.v4l2_dev.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3699 risc->cpu[i+j], j);
3700 if (0 == risc->cpu[i])
3701 break;
3702 }
3703 }
3704
3705 static void bttv_print_riscaddr(struct bttv *btv)
3706 {
3707 printk(" main: %08Lx\n",
3708 (unsigned long long)btv->main.dma);
3709 printk(" vbi : o=%08Lx e=%08Lx\n",
3710 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3711 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3712 printk(" cap : o=%08Lx e=%08Lx\n",
3713 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3714 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3715 printk(" scr : o=%08Lx e=%08Lx\n",
3716 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3717 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3718 bttv_risc_disasm(btv, &btv->main);
3719 }
3720
3721 /* ----------------------------------------------------------------------- */
3722 /* irq handler */
3723
3724 static char *irq_name[] = {
3725 "FMTCHG", // format change detected (525 vs. 625)
3726 "VSYNC", // vertical sync (new field)
3727 "HSYNC", // horizontal sync
3728 "OFLOW", // chroma/luma AGC overflow
3729 "HLOCK", // horizontal lock changed
3730 "VPRES", // video presence changed
3731 "6", "7",
3732 "I2CDONE", // hw irc operation finished
3733 "GPINT", // gpio port triggered irq
3734 "10",
3735 "RISCI", // risc instruction triggered irq
3736 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3737 "FTRGT", // pixel data fifo overrun
3738 "FDSR", // fifo data stream resyncronisation
3739 "PPERR", // parity error (data transfer)
3740 "RIPERR", // parity error (read risc instructions)
3741 "PABORT", // pci abort
3742 "OCERR", // risc instruction error
3743 "SCERR", // syncronisation error
3744 };
3745
3746 static void bttv_print_irqbits(u32 print, u32 mark)
3747 {
3748 unsigned int i;
3749
3750 printk("bits:");
3751 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3752 if (print & (1 << i))
3753 printk(" %s",irq_name[i]);
3754 if (mark & (1 << i))
3755 printk("*");
3756 }
3757 }
3758
3759 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3760 {
3761 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3762 btv->c.nr,
3763 (unsigned long)btv->main.dma,
3764 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3765 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3766 (unsigned long)rc);
3767
3768 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3769 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3770 "Ok, then this is harmless, don't worry ;)\n",
3771 btv->c.nr);
3772 return;
3773 }
3774 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3775 btv->c.nr);
3776 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3777 btv->c.nr);
3778 dump_stack();
3779 }
3780
3781 static int
3782 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3783 {
3784 struct bttv_buffer *item;
3785
3786 memset(set,0,sizeof(*set));
3787
3788 /* capture request ? */
3789 if (!list_empty(&btv->capture)) {
3790 set->frame_irq = 1;
3791 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3792 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3793 set->top = item;
3794 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3795 set->bottom = item;
3796
3797 /* capture request for other field ? */
3798 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3799 (item->vb.queue.next != &btv->capture)) {
3800 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3801 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3802 if (NULL == set->top &&
3803 V4L2_FIELD_TOP == item->vb.field) {
3804 set->top = item;
3805 }
3806 if (NULL == set->bottom &&
3807 V4L2_FIELD_BOTTOM == item->vb.field) {
3808 set->bottom = item;
3809 }
3810 if (NULL != set->top && NULL != set->bottom)
3811 set->top_irq = 2;
3812 }
3813 }
3814 }
3815
3816 /* screen overlay ? */
3817 if (NULL != btv->screen) {
3818 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3819 if (NULL == set->top && NULL == set->bottom) {
3820 set->top = btv->screen;
3821 set->bottom = btv->screen;
3822 }
3823 } else {
3824 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3825 NULL == set->top) {
3826 set->top = btv->screen;
3827 }
3828 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3829 NULL == set->bottom) {
3830 set->bottom = btv->screen;
3831 }
3832 }
3833 }
3834
3835 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3836 btv->c.nr,set->top, set->bottom,
3837 btv->screen,set->frame_irq,set->top_irq);
3838 return 0;
3839 }
3840
3841 static void
3842 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3843 struct bttv_buffer_set *curr, unsigned int state)
3844 {
3845 struct timeval ts;
3846
3847 do_gettimeofday(&ts);
3848
3849 if (wakeup->top == wakeup->bottom) {
3850 if (NULL != wakeup->top && curr->top != wakeup->top) {
3851 if (irq_debug > 1)
3852 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3853 wakeup->top->vb.ts = ts;
3854 wakeup->top->vb.field_count = btv->field_count;
3855 wakeup->top->vb.state = state;
3856 wake_up(&wakeup->top->vb.done);
3857 }
3858 } else {
3859 if (NULL != wakeup->top && curr->top != wakeup->top) {
3860 if (irq_debug > 1)
3861 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3862 wakeup->top->vb.ts = ts;
3863 wakeup->top->vb.field_count = btv->field_count;
3864 wakeup->top->vb.state = state;
3865 wake_up(&wakeup->top->vb.done);
3866 }
3867 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3868 if (irq_debug > 1)
3869 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3870 wakeup->bottom->vb.ts = ts;
3871 wakeup->bottom->vb.field_count = btv->field_count;
3872 wakeup->bottom->vb.state = state;
3873 wake_up(&wakeup->bottom->vb.done);
3874 }
3875 }
3876 }
3877
3878 static void
3879 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3880 unsigned int state)
3881 {
3882 struct timeval ts;
3883
3884 if (NULL == wakeup)
3885 return;
3886
3887 do_gettimeofday(&ts);
3888 wakeup->vb.ts = ts;
3889 wakeup->vb.field_count = btv->field_count;
3890 wakeup->vb.state = state;
3891 wake_up(&wakeup->vb.done);
3892 }
3893
3894 static void bttv_irq_timeout(unsigned long data)
3895 {
3896 struct bttv *btv = (struct bttv *)data;
3897 struct bttv_buffer_set old,new;
3898 struct bttv_buffer *ovbi;
3899 struct bttv_buffer *item;
3900 unsigned long flags;
3901
3902 if (bttv_verbose) {
3903 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3904 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3905 btread(BT848_RISC_COUNT));
3906 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3907 printk("\n");
3908 }
3909
3910 spin_lock_irqsave(&btv->s_lock,flags);
3911
3912 /* deactivate stuff */
3913 memset(&new,0,sizeof(new));
3914 old = btv->curr;
3915 ovbi = btv->cvbi;
3916 btv->curr = new;
3917 btv->cvbi = NULL;
3918 btv->loop_irq = 0;
3919 bttv_buffer_activate_video(btv, &new);
3920 bttv_buffer_activate_vbi(btv, NULL);
3921 bttv_set_dma(btv, 0);
3922
3923 /* wake up */
3924 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3925 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
3926
3927 /* cancel all outstanding capture / vbi requests */
3928 while (!list_empty(&btv->capture)) {
3929 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3930 list_del(&item->vb.queue);
3931 item->vb.state = VIDEOBUF_ERROR;
3932 wake_up(&item->vb.done);
3933 }
3934 while (!list_empty(&btv->vcapture)) {
3935 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3936 list_del(&item->vb.queue);
3937 item->vb.state = VIDEOBUF_ERROR;
3938 wake_up(&item->vb.done);
3939 }
3940
3941 btv->errors++;
3942 spin_unlock_irqrestore(&btv->s_lock,flags);
3943 }
3944
3945 static void
3946 bttv_irq_wakeup_top(struct bttv *btv)
3947 {
3948 struct bttv_buffer *wakeup = btv->curr.top;
3949
3950 if (NULL == wakeup)
3951 return;
3952
3953 spin_lock(&btv->s_lock);
3954 btv->curr.top_irq = 0;
3955 btv->curr.top = NULL;
3956 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3957
3958 do_gettimeofday(&wakeup->vb.ts);
3959 wakeup->vb.field_count = btv->field_count;
3960 wakeup->vb.state = VIDEOBUF_DONE;
3961 wake_up(&wakeup->vb.done);
3962 spin_unlock(&btv->s_lock);
3963 }
3964
3965 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3966 {
3967 if (rc < risc->dma)
3968 return 0;
3969 if (rc > risc->dma + risc->size)
3970 return 0;
3971 return 1;
3972 }
3973
3974 static void
3975 bttv_irq_switch_video(struct bttv *btv)
3976 {
3977 struct bttv_buffer_set new;
3978 struct bttv_buffer_set old;
3979 dma_addr_t rc;
3980
3981 spin_lock(&btv->s_lock);
3982
3983 /* new buffer set */
3984 bttv_irq_next_video(btv, &new);
3985 rc = btread(BT848_RISC_COUNT);
3986 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3987 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3988 btv->framedrop++;
3989 if (debug_latency)
3990 bttv_irq_debug_low_latency(btv, rc);
3991 spin_unlock(&btv->s_lock);
3992 return;
3993 }
3994
3995 /* switch over */
3996 old = btv->curr;
3997 btv->curr = new;
3998 btv->loop_irq &= ~1;
3999 bttv_buffer_activate_video(btv, &new);
4000 bttv_set_dma(btv, 0);
4001
4002 /* switch input */
4003 if (UNSET != btv->new_input) {
4004 video_mux(btv,btv->new_input);
4005 btv->new_input = UNSET;
4006 }
4007
4008 /* wake up finished buffers */
4009 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
4010 spin_unlock(&btv->s_lock);
4011 }
4012
4013 static void
4014 bttv_irq_switch_vbi(struct bttv *btv)
4015 {
4016 struct bttv_buffer *new = NULL;
4017 struct bttv_buffer *old;
4018 u32 rc;
4019
4020 spin_lock(&btv->s_lock);
4021
4022 if (!list_empty(&btv->vcapture))
4023 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4024 old = btv->cvbi;
4025
4026 rc = btread(BT848_RISC_COUNT);
4027 if (NULL != old && (is_active(&old->top, rc) ||
4028 is_active(&old->bottom, rc))) {
4029 btv->framedrop++;
4030 if (debug_latency)
4031 bttv_irq_debug_low_latency(btv, rc);
4032 spin_unlock(&btv->s_lock);
4033 return;
4034 }
4035
4036 /* switch */
4037 btv->cvbi = new;
4038 btv->loop_irq &= ~4;
4039 bttv_buffer_activate_vbi(btv, new);
4040 bttv_set_dma(btv, 0);
4041
4042 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
4043 spin_unlock(&btv->s_lock);
4044 }
4045
4046 static irqreturn_t bttv_irq(int irq, void *dev_id)
4047 {
4048 u32 stat,astat;
4049 u32 dstat;
4050 int count;
4051 struct bttv *btv;
4052 int handled = 0;
4053
4054 btv=(struct bttv *)dev_id;
4055
4056 if (btv->custom_irq)
4057 handled = btv->custom_irq(btv);
4058
4059 count=0;
4060 while (1) {
4061 /* get/clear interrupt status bits */
4062 stat=btread(BT848_INT_STAT);
4063 astat=stat&btread(BT848_INT_MASK);
4064 if (!astat)
4065 break;
4066 handled = 1;
4067 btwrite(stat,BT848_INT_STAT);
4068
4069 /* get device status bits */
4070 dstat=btread(BT848_DSTATUS);
4071
4072 if (irq_debug) {
4073 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4074 "riscs=%x, riscc=%08x, ",
4075 btv->c.nr, count, btv->field_count,
4076 stat>>28, btread(BT848_RISC_COUNT));
4077 bttv_print_irqbits(stat,astat);
4078 if (stat & BT848_INT_HLOCK)
4079 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4080 ? "yes" : "no");
4081 if (stat & BT848_INT_VPRES)
4082 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4083 ? "yes" : "no");
4084 if (stat & BT848_INT_FMTCHG)
4085 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4086 ? "625" : "525");
4087 printk("\n");
4088 }
4089
4090 if (astat&BT848_INT_VSYNC)
4091 btv->field_count++;
4092
4093 if ((astat & BT848_INT_GPINT) && btv->remote) {
4094 wake_up(&btv->gpioq);
4095 bttv_input_irq(btv);
4096 }
4097
4098 if (astat & BT848_INT_I2CDONE) {
4099 btv->i2c_done = stat;
4100 wake_up(&btv->i2c_queue);
4101 }
4102
4103 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
4104 bttv_irq_switch_vbi(btv);
4105
4106 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
4107 bttv_irq_wakeup_top(btv);
4108
4109 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
4110 bttv_irq_switch_video(btv);
4111
4112 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
4113 audio_mute(btv, btv->mute); /* trigger automute */
4114
4115 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4116 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4117 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4118 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4119 btread(BT848_RISC_COUNT));
4120 bttv_print_irqbits(stat,astat);
4121 printk("\n");
4122 if (bttv_debug)
4123 bttv_print_riscaddr(btv);
4124 }
4125 if (fdsr && astat & BT848_INT_FDSR) {
4126 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4127 btv->c.nr,btread(BT848_RISC_COUNT));
4128 if (bttv_debug)
4129 bttv_print_riscaddr(btv);
4130 }
4131
4132 count++;
4133 if (count > 4) {
4134
4135 if (count > 8 || !(astat & BT848_INT_GPINT)) {
4136 btwrite(0, BT848_INT_MASK);
4137
4138 printk(KERN_ERR
4139 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4140 } else {
4141 printk(KERN_ERR
4142 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4143
4144 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4145 BT848_INT_MASK);
4146 };
4147
4148 bttv_print_irqbits(stat,astat);
4149
4150 printk("]\n");
4151 }
4152 }
4153 btv->irq_total++;
4154 if (handled)
4155 btv->irq_me++;
4156 return IRQ_RETVAL(handled);
4157 }
4158
4159
4160 /* ----------------------------------------------------------------------- */
4161 /* initialitation */
4162
4163 static struct video_device *vdev_init(struct bttv *btv,
4164 const struct video_device *template,
4165 const char *type_name)
4166 {
4167 struct video_device *vfd;
4168
4169 vfd = video_device_alloc();
4170 if (NULL == vfd)
4171 return NULL;
4172 *vfd = *template;
4173 vfd->v4l2_dev = &btv->c.v4l2_dev;
4174 vfd->release = video_device_release;
4175 vfd->debug = bttv_debug;
4176 video_set_drvdata(vfd, btv);
4177 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4178 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4179 type_name, bttv_tvcards[btv->c.type].name);
4180 return vfd;
4181 }
4182
4183 static void bttv_unregister_video(struct bttv *btv)
4184 {
4185 if (btv->video_dev) {
4186 if (-1 != btv->video_dev->minor)
4187 video_unregister_device(btv->video_dev);
4188 else
4189 video_device_release(btv->video_dev);
4190 btv->video_dev = NULL;
4191 }
4192 if (btv->vbi_dev) {
4193 if (-1 != btv->vbi_dev->minor)
4194 video_unregister_device(btv->vbi_dev);
4195 else
4196 video_device_release(btv->vbi_dev);
4197 btv->vbi_dev = NULL;
4198 }
4199 if (btv->radio_dev) {
4200 if (-1 != btv->radio_dev->minor)
4201 video_unregister_device(btv->radio_dev);
4202 else
4203 video_device_release(btv->radio_dev);
4204 btv->radio_dev = NULL;
4205 }
4206 }
4207
4208 /* register video4linux devices */
4209 static int __devinit bttv_register_video(struct bttv *btv)
4210 {
4211 if (no_overlay > 0)
4212 printk("bttv: Overlay support disabled.\n");
4213
4214 /* video */
4215 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
4216
4217 if (NULL == btv->video_dev)
4218 goto err;
4219 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
4220 video_nr[btv->c.nr]) < 0)
4221 goto err;
4222 printk(KERN_INFO "bttv%d: registered device video%d\n",
4223 btv->c.nr, btv->video_dev->num);
4224 if (device_create_file(&btv->video_dev->dev,
4225 &dev_attr_card)<0) {
4226 printk(KERN_ERR "bttv%d: device_create_file 'card' "
4227 "failed\n", btv->c.nr);
4228 goto err;
4229 }
4230
4231 /* vbi */
4232 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
4233
4234 if (NULL == btv->vbi_dev)
4235 goto err;
4236 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
4237 vbi_nr[btv->c.nr]) < 0)
4238 goto err;
4239 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
4240 btv->c.nr, btv->vbi_dev->num);
4241
4242 if (!btv->has_radio)
4243 return 0;
4244 /* radio */
4245 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
4246 if (NULL == btv->radio_dev)
4247 goto err;
4248 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4249 radio_nr[btv->c.nr]) < 0)
4250 goto err;
4251 printk(KERN_INFO "bttv%d: registered device radio%d\n",
4252 btv->c.nr, btv->radio_dev->num);
4253
4254 /* all done */
4255 return 0;
4256
4257 err:
4258 bttv_unregister_video(btv);
4259 return -1;
4260 }
4261
4262
4263 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4264 /* response on cards with no firmware is not enabled by OF */
4265 static void pci_set_command(struct pci_dev *dev)
4266 {
4267 #if defined(__powerpc__)
4268 unsigned int cmd;
4269
4270 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4271 cmd = (cmd | PCI_COMMAND_MEMORY );
4272 pci_write_config_dword(dev, PCI_COMMAND, cmd);
4273 #endif
4274 }
4275
4276 static int __devinit bttv_probe(struct pci_dev *dev,
4277 const struct pci_device_id *pci_id)
4278 {
4279 int result;
4280 unsigned char lat;
4281 struct bttv *btv;
4282
4283 if (bttv_num == BTTV_MAX)
4284 return -ENOMEM;
4285 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
4286 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
4287 if (btv == NULL) {
4288 printk(KERN_ERR "bttv: out of memory.\n");
4289 return -ENOMEM;
4290 }
4291 btv->c.nr = bttv_num;
4292 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4293 "bttv%d", btv->c.nr);
4294
4295 /* initialize structs / fill in defaults */
4296 mutex_init(&btv->lock);
4297 spin_lock_init(&btv->s_lock);
4298 spin_lock_init(&btv->gpio_lock);
4299 init_waitqueue_head(&btv->gpioq);
4300 init_waitqueue_head(&btv->i2c_queue);
4301 INIT_LIST_HEAD(&btv->c.subs);
4302 INIT_LIST_HEAD(&btv->capture);
4303 INIT_LIST_HEAD(&btv->vcapture);
4304 v4l2_prio_init(&btv->prio);
4305
4306 init_timer(&btv->timeout);
4307 btv->timeout.function = bttv_irq_timeout;
4308 btv->timeout.data = (unsigned long)btv;
4309
4310 btv->i2c_rc = -1;
4311 btv->tuner_type = UNSET;
4312 btv->new_input = UNSET;
4313 btv->has_radio=radio[btv->c.nr];
4314
4315 /* pci stuff (init, get irq/mmio, ... */
4316 btv->c.pci = dev;
4317 btv->id = dev->device;
4318 if (pci_enable_device(dev)) {
4319 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4320 btv->c.nr);
4321 return -EIO;
4322 }
4323 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
4324 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
4325 btv->c.nr);
4326 return -EIO;
4327 }
4328 if (!request_mem_region(pci_resource_start(dev,0),
4329 pci_resource_len(dev,0),
4330 btv->c.v4l2_dev.name)) {
4331 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4332 btv->c.nr,
4333 (unsigned long long)pci_resource_start(dev,0));
4334 return -EBUSY;
4335 }
4336 pci_set_master(dev);
4337 pci_set_command(dev);
4338
4339 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4340 if (result < 0) {
4341 printk(KERN_WARNING "bttv%d: v4l2_device_register() failed\n", btv->c.nr);
4342 goto fail0;
4343 }
4344
4345 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4346 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4347 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4348 bttv_num,btv->id, btv->revision, pci_name(dev));
4349 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4350 btv->c.pci->irq, lat,
4351 (unsigned long long)pci_resource_start(dev,0));
4352 schedule();
4353
4354 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4355 if (NULL == btv->bt848_mmio) {
4356 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4357 result = -EIO;
4358 goto fail1;
4359 }
4360
4361 /* identify card */
4362 bttv_idcard(btv);
4363
4364 /* disable irqs, register irq handler */
4365 btwrite(0, BT848_INT_MASK);
4366 result = request_irq(btv->c.pci->irq, bttv_irq,
4367 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
4368 if (result < 0) {
4369 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
4370 bttv_num,btv->c.pci->irq);
4371 goto fail1;
4372 }
4373
4374 if (0 != bttv_handle_chipset(btv)) {
4375 result = -EIO;
4376 goto fail2;
4377 }
4378
4379 /* init options from insmod args */
4380 btv->opt_combfilter = combfilter;
4381 btv->opt_lumafilter = lumafilter;
4382 btv->opt_automute = automute;
4383 btv->opt_chroma_agc = chroma_agc;
4384 btv->opt_adc_crush = adc_crush;
4385 btv->opt_vcr_hack = vcr_hack;
4386 btv->opt_whitecrush_upper = whitecrush_upper;
4387 btv->opt_whitecrush_lower = whitecrush_lower;
4388 btv->opt_uv_ratio = uv_ratio;
4389 btv->opt_full_luma_range = full_luma_range;
4390 btv->opt_coring = coring;
4391
4392 /* fill struct bttv with some useful defaults */
4393 btv->init.btv = btv;
4394 btv->init.ov.w.width = 320;
4395 btv->init.ov.w.height = 240;
4396 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
4397 btv->init.width = 320;
4398 btv->init.height = 240;
4399 btv->input = 0;
4400
4401 /* initialize hardware */
4402 if (bttv_gpio)
4403 bttv_gpio_tracking(btv,"pre-init");
4404
4405 bttv_risc_init_main(btv);
4406 init_bt848(btv);
4407
4408 /* gpio */
4409 btwrite(0x00, BT848_GPIO_REG_INP);
4410 btwrite(0x00, BT848_GPIO_OUT_EN);
4411 if (bttv_verbose)
4412 bttv_gpio_tracking(btv,"init");
4413
4414 /* needs to be done before i2c is registered */
4415 bttv_init_card1(btv);
4416
4417 /* register i2c + gpio */
4418 init_bttv_i2c(btv);
4419
4420 /* some card-specific stuff (needs working i2c) */
4421 bttv_init_card2(btv);
4422 bttv_init_tuner(btv);
4423 init_irqreg(btv);
4424
4425 /* register video4linux + input */
4426 if (!bttv_tvcards[btv->c.type].no_video) {
4427 bttv_register_video(btv);
4428 bt848_bright(btv,32768);
4429 bt848_contrast(btv,32768);
4430 bt848_hue(btv,32768);
4431 bt848_sat(btv,32768);
4432 audio_mute(btv, 1);
4433 set_input(btv, 0, btv->tvnorm);
4434 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4435 btv->crop[1] = btv->crop[0]; /* current = default */
4436 disclaim_vbi_lines(btv);
4437 disclaim_video_lines(btv);
4438 }
4439
4440 /* add subdevices and autoload dvb-bt8xx if needed */
4441 if (bttv_tvcards[btv->c.type].has_dvb) {
4442 bttv_sub_add_device(&btv->c, "dvb");
4443 request_modules(btv);
4444 }
4445
4446 bttv_input_init(btv);
4447
4448 /* everything is fine */
4449 bttv_num++;
4450 return 0;
4451
4452 fail2:
4453 free_irq(btv->c.pci->irq,btv);
4454
4455 fail1:
4456 v4l2_device_unregister(&btv->c.v4l2_dev);
4457
4458 fail0:
4459 if (btv->bt848_mmio)
4460 iounmap(btv->bt848_mmio);
4461 release_mem_region(pci_resource_start(btv->c.pci,0),
4462 pci_resource_len(btv->c.pci,0));
4463 return result;
4464 }
4465
4466 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4467 {
4468 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4469 struct bttv *btv = to_bttv(v4l2_dev);
4470
4471 if (bttv_verbose)
4472 printk("bttv%d: unloading\n",btv->c.nr);
4473
4474 /* shutdown everything (DMA+IRQs) */
4475 btand(~15, BT848_GPIO_DMA_CTL);
4476 btwrite(0, BT848_INT_MASK);
4477 btwrite(~0x0, BT848_INT_STAT);
4478 btwrite(0x0, BT848_GPIO_OUT_EN);
4479 if (bttv_gpio)
4480 bttv_gpio_tracking(btv,"cleanup");
4481
4482 /* tell gpio modules we are leaving ... */
4483 btv->shutdown=1;
4484 wake_up(&btv->gpioq);
4485 bttv_input_fini(btv);
4486 bttv_sub_del_devices(&btv->c);
4487
4488 /* unregister i2c_bus + input */
4489 fini_bttv_i2c(btv);
4490
4491 /* unregister video4linux */
4492 bttv_unregister_video(btv);
4493
4494 /* free allocated memory */
4495 btcx_riscmem_free(btv->c.pci,&btv->main);
4496
4497 /* free ressources */
4498 free_irq(btv->c.pci->irq,btv);
4499 iounmap(btv->bt848_mmio);
4500 release_mem_region(pci_resource_start(btv->c.pci,0),
4501 pci_resource_len(btv->c.pci,0));
4502
4503 v4l2_device_unregister(&btv->c.v4l2_dev);
4504 bttvs[btv->c.nr] = NULL;
4505 kfree(btv);
4506
4507 return;
4508 }
4509
4510 #ifdef CONFIG_PM
4511 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4512 {
4513 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4514 struct bttv *btv = to_bttv(v4l2_dev);
4515 struct bttv_buffer_set idle;
4516 unsigned long flags;
4517
4518 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4519
4520 /* stop dma + irqs */
4521 spin_lock_irqsave(&btv->s_lock,flags);
4522 memset(&idle, 0, sizeof(idle));
4523 btv->state.video = btv->curr;
4524 btv->state.vbi = btv->cvbi;
4525 btv->state.loop_irq = btv->loop_irq;
4526 btv->curr = idle;
4527 btv->loop_irq = 0;
4528 bttv_buffer_activate_video(btv, &idle);
4529 bttv_buffer_activate_vbi(btv, NULL);
4530 bttv_set_dma(btv, 0);
4531 btwrite(0, BT848_INT_MASK);
4532 spin_unlock_irqrestore(&btv->s_lock,flags);
4533
4534 /* save bt878 state */
4535 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4536 btv->state.gpio_data = gpio_read();
4537
4538 /* save pci state */
4539 pci_save_state(pci_dev);
4540 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4541 pci_disable_device(pci_dev);
4542 btv->state.disabled = 1;
4543 }
4544 return 0;
4545 }
4546
4547 static int bttv_resume(struct pci_dev *pci_dev)
4548 {
4549 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4550 struct bttv *btv = to_bttv(v4l2_dev);
4551 unsigned long flags;
4552 int err;
4553
4554 dprintk("bttv%d: resume\n", btv->c.nr);
4555
4556 /* restore pci state */
4557 if (btv->state.disabled) {
4558 err=pci_enable_device(pci_dev);
4559 if (err) {
4560 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4561 btv->c.nr);
4562 return err;
4563 }
4564 btv->state.disabled = 0;
4565 }
4566 err=pci_set_power_state(pci_dev, PCI_D0);
4567 if (err) {
4568 pci_disable_device(pci_dev);
4569 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4570 btv->c.nr);
4571 btv->state.disabled = 1;
4572 return err;
4573 }
4574
4575 pci_restore_state(pci_dev);
4576
4577 /* restore bt878 state */
4578 bttv_reinit_bt848(btv);
4579 gpio_inout(0xffffff, btv->state.gpio_enable);
4580 gpio_write(btv->state.gpio_data);
4581
4582 /* restart dma */
4583 spin_lock_irqsave(&btv->s_lock,flags);
4584 btv->curr = btv->state.video;
4585 btv->cvbi = btv->state.vbi;
4586 btv->loop_irq = btv->state.loop_irq;
4587 bttv_buffer_activate_video(btv, &btv->curr);
4588 bttv_buffer_activate_vbi(btv, btv->cvbi);
4589 bttv_set_dma(btv, 0);
4590 spin_unlock_irqrestore(&btv->s_lock,flags);
4591 return 0;
4592 }
4593 #endif
4594
4595 static struct pci_device_id bttv_pci_tbl[] = {
4596 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4597 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4598 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
4599 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4600 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
4601 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4602 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
4603 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4604 {0,}
4605 };
4606
4607 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4608
4609 static struct pci_driver bttv_pci_driver = {
4610 .name = "bttv",
4611 .id_table = bttv_pci_tbl,
4612 .probe = bttv_probe,
4613 .remove = __devexit_p(bttv_remove),
4614 #ifdef CONFIG_PM
4615 .suspend = bttv_suspend,
4616 .resume = bttv_resume,
4617 #endif
4618 };
4619
4620 static int __init bttv_init_module(void)
4621 {
4622 int ret;
4623
4624 bttv_num = 0;
4625
4626 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4627 (BTTV_VERSION_CODE >> 16) & 0xff,
4628 (BTTV_VERSION_CODE >> 8) & 0xff,
4629 BTTV_VERSION_CODE & 0xff);
4630 #ifdef SNAPSHOT
4631 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4632 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4633 #endif
4634 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4635 gbuffers = 2;
4636 if (gbufsize > BTTV_MAX_FBUF)
4637 gbufsize = BTTV_MAX_FBUF;
4638 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4639 if (bttv_verbose)
4640 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4641 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4642
4643 bttv_check_chipset();
4644
4645 ret = bus_register(&bttv_sub_bus_type);
4646 if (ret < 0) {
4647 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4648 return ret;
4649 }
4650 ret = pci_register_driver(&bttv_pci_driver);
4651 if (ret < 0)
4652 bus_unregister(&bttv_sub_bus_type);
4653
4654 return ret;
4655 }
4656
4657 static void __exit bttv_cleanup_module(void)
4658 {
4659 pci_unregister_driver(&bttv_pci_driver);
4660 bus_unregister(&bttv_sub_bus_type);
4661 }
4662
4663 module_init(bttv_init_module);
4664 module_exit(bttv_cleanup_module);
4665
4666 /*
4667 * Local variables:
4668 * c-basic-offset: 8
4669 * End:
4670 */
This page took 0.379018 seconds and 5 git commands to generate.