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