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