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