aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/bttv-driver.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/media/video/bttv-driver.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/media/video/bttv-driver.c')
-rw-r--r--drivers/media/video/bttv-driver.c4116
1 files changed, 4116 insertions, 0 deletions
diff --git a/drivers/media/video/bttv-driver.c b/drivers/media/video/bttv-driver.c
new file mode 100644
index 000000000000..c13f222fe6bd
--- /dev/null
+++ b/drivers/media/video/bttv-driver.c
@@ -0,0 +1,4116 @@
1/*
2 $Id: bttv-driver.c,v 1.37 2005/02/21 13:57:59 kraxel Exp $
3
4 bttv - Bt848 frame grabber driver
5
6 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
7 & Marcus Metzler <mocm@thp.uni-koeln.de>
8 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
9
10 some v4l2 code lines are taken from Justin's bttv2 driver which is
11 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26*/
27
28#include <linux/init.h>
29#include <linux/module.h>
30#include <linux/moduleparam.h>
31#include <linux/delay.h>
32#include <linux/errno.h>
33#include <linux/fs.h>
34#include <linux/kernel.h>
35#include <linux/sched.h>
36#include <linux/interrupt.h>
37#include <linux/kdev_t.h>
38
39#include <asm/io.h>
40#include <asm/byteorder.h>
41
42#include "bttvp.h"
43
44unsigned int bttv_num; /* number of Bt848s in use */
45struct bttv bttvs[BTTV_MAX];
46
47unsigned int bttv_debug = 0;
48unsigned int bttv_verbose = 1;
49unsigned int bttv_gpio = 0;
50
51/* config variables */
52#ifdef __BIG_ENDIAN
53static unsigned int bigendian=1;
54#else
55static unsigned int bigendian=0;
56#endif
57static unsigned int radio[BTTV_MAX];
58static unsigned int irq_debug = 0;
59static unsigned int gbuffers = 8;
60static unsigned int gbufsize = 0x208000;
61
62static int video_nr = -1;
63static int radio_nr = -1;
64static int vbi_nr = -1;
65static int debug_latency = 0;
66
67static unsigned int fdsr = 0;
68
69/* options */
70static unsigned int combfilter = 0;
71static unsigned int lumafilter = 0;
72static unsigned int automute = 1;
73static unsigned int chroma_agc = 0;
74static unsigned int adc_crush = 1;
75static unsigned int whitecrush_upper = 0xCF;
76static unsigned int whitecrush_lower = 0x7F;
77static unsigned int vcr_hack = 0;
78static unsigned int irq_iswitch = 0;
79
80/* API features (turn on/off stuff for testing) */
81static unsigned int v4l2 = 1;
82
83
84/* insmod args */
85module_param(bttv_verbose, int, 0644);
86module_param(bttv_gpio, int, 0644);
87module_param(bttv_debug, int, 0644);
88module_param(irq_debug, int, 0644);
89module_param(debug_latency, int, 0644);
90
91module_param(fdsr, int, 0444);
92module_param(video_nr, int, 0444);
93module_param(radio_nr, int, 0444);
94module_param(vbi_nr, int, 0444);
95module_param(gbuffers, int, 0444);
96module_param(gbufsize, int, 0444);
97
98module_param(v4l2, int, 0644);
99module_param(bigendian, int, 0644);
100module_param(irq_iswitch, int, 0644);
101module_param(combfilter, int, 0444);
102module_param(lumafilter, int, 0444);
103module_param(automute, int, 0444);
104module_param(chroma_agc, int, 0444);
105module_param(adc_crush, int, 0444);
106module_param(whitecrush_upper, int, 0444);
107module_param(whitecrush_lower, int, 0444);
108module_param(vcr_hack, int, 0444);
109
110module_param_array(radio, int, NULL, 0444);
111
112MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
113MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
114MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
115MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
116MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
117MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
118MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
119MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
120MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
121MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
122MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
123MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
124MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
125MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
126MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
127
128MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
129MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
130MODULE_LICENSE("GPL");
131
132/* ----------------------------------------------------------------------- */
133/* sysfs */
134
135static ssize_t show_card(struct class_device *cd, char *buf)
136{
137 struct video_device *vfd = to_video_device(cd);
138 struct bttv *btv = dev_get_drvdata(vfd->dev);
139 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
140}
141static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
142
143/* ----------------------------------------------------------------------- */
144/* static data */
145
146/* special timing tables from conexant... */
147static u8 SRAM_Table[][60] =
148{
149 /* PAL digital input over GPIO[7:0] */
150 {
151 45, // 45 bytes following
152 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
153 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
154 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
155 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
156 0x37,0x00,0xAF,0x21,0x00
157 },
158 /* NTSC digital input over GPIO[7:0] */
159 {
160 51, // 51 bytes following
161 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
162 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
163 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
164 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
165 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
166 0x00,
167 },
168 // TGB_NTSC392 // quartzsight
169 // This table has been modified to be used for Fusion Rev D
170 {
171 0x2A, // size of table = 42
172 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
173 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
174 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
175 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
176 0x20, 0x00
177 }
178};
179
180const struct bttv_tvnorm bttv_tvnorms[] = {
181 /* PAL-BDGHI */
182 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
183 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
184 {
185 .v4l2_id = V4L2_STD_PAL,
186 .name = "PAL",
187 .Fsc = 35468950,
188 .swidth = 924,
189 .sheight = 576,
190 .totalwidth = 1135,
191 .adelay = 0x7f,
192 .bdelay = 0x72,
193 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
194 .scaledtwidth = 1135,
195 .hdelayx1 = 186,
196 .hactivex1 = 924,
197 .vdelay = 0x20,
198 .vbipack = 255,
199 .sram = 0,
200 },{
201 .v4l2_id = V4L2_STD_NTSC_M,
202 .name = "NTSC",
203 .Fsc = 28636363,
204 .swidth = 768,
205 .sheight = 480,
206 .totalwidth = 910,
207 .adelay = 0x68,
208 .bdelay = 0x5d,
209 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
210 .scaledtwidth = 910,
211 .hdelayx1 = 128,
212 .hactivex1 = 910,
213 .vdelay = 0x1a,
214 .vbipack = 144,
215 .sram = 1,
216 },{
217 .v4l2_id = V4L2_STD_SECAM,
218 .name = "SECAM",
219 .Fsc = 35468950,
220 .swidth = 924,
221 .sheight = 576,
222 .totalwidth = 1135,
223 .adelay = 0x7f,
224 .bdelay = 0xb0,
225 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
226 .scaledtwidth = 1135,
227 .hdelayx1 = 186,
228 .hactivex1 = 922,
229 .vdelay = 0x20,
230 .vbipack = 255,
231 .sram = 0, /* like PAL, correct? */
232 },{
233 .v4l2_id = V4L2_STD_PAL_Nc,
234 .name = "PAL-Nc",
235 .Fsc = 28636363,
236 .swidth = 640,
237 .sheight = 576,
238 .totalwidth = 910,
239 .adelay = 0x68,
240 .bdelay = 0x5d,
241 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
242 .scaledtwidth = 780,
243 .hdelayx1 = 130,
244 .hactivex1 = 734,
245 .vdelay = 0x1a,
246 .vbipack = 144,
247 .sram = -1,
248 },{
249 .v4l2_id = V4L2_STD_PAL_M,
250 .name = "PAL-M",
251 .Fsc = 28636363,
252 .swidth = 640,
253 .sheight = 480,
254 .totalwidth = 910,
255 .adelay = 0x68,
256 .bdelay = 0x5d,
257 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
258 .scaledtwidth = 780,
259 .hdelayx1 = 135,
260 .hactivex1 = 754,
261 .vdelay = 0x1a,
262 .vbipack = 144,
263 .sram = -1,
264 },{
265 .v4l2_id = V4L2_STD_PAL_N,
266 .name = "PAL-N",
267 .Fsc = 35468950,
268 .swidth = 768,
269 .sheight = 576,
270 .totalwidth = 1135,
271 .adelay = 0x7f,
272 .bdelay = 0x72,
273 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
274 .scaledtwidth = 944,
275 .hdelayx1 = 186,
276 .hactivex1 = 922,
277 .vdelay = 0x20,
278 .vbipack = 144,
279 .sram = -1,
280 },{
281 .v4l2_id = V4L2_STD_NTSC_M_JP,
282 .name = "NTSC-JP",
283 .Fsc = 28636363,
284 .swidth = 640,
285 .sheight = 480,
286 .totalwidth = 910,
287 .adelay = 0x68,
288 .bdelay = 0x5d,
289 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
290 .scaledtwidth = 780,
291 .hdelayx1 = 135,
292 .hactivex1 = 754,
293 .vdelay = 0x16,
294 .vbipack = 144,
295 .sram = -1,
296 },{
297 /* that one hopefully works with the strange timing
298 * which video recorders produce when playing a NTSC
299 * tape on a PAL TV ... */
300 .v4l2_id = V4L2_STD_PAL_60,
301 .name = "PAL-60",
302 .Fsc = 35468950,
303 .swidth = 924,
304 .sheight = 480,
305 .totalwidth = 1135,
306 .adelay = 0x7f,
307 .bdelay = 0x72,
308 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
309 .scaledtwidth = 1135,
310 .hdelayx1 = 186,
311 .hactivex1 = 924,
312 .vdelay = 0x1a,
313 .vbipack = 255,
314 .vtotal = 524,
315 .sram = -1,
316 }
317};
318static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
319
320/* ----------------------------------------------------------------------- */
321/* bttv format list
322 packed pixel formats must come first */
323static const struct bttv_format bttv_formats[] = {
324 {
325 .name = "8 bpp, gray",
326 .palette = VIDEO_PALETTE_GREY,
327 .fourcc = V4L2_PIX_FMT_GREY,
328 .btformat = BT848_COLOR_FMT_Y8,
329 .depth = 8,
330 .flags = FORMAT_FLAGS_PACKED,
331 },{
332 .name = "8 bpp, dithered color",
333 .palette = VIDEO_PALETTE_HI240,
334 .fourcc = V4L2_PIX_FMT_HI240,
335 .btformat = BT848_COLOR_FMT_RGB8,
336 .depth = 8,
337 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
338 },{
339 .name = "15 bpp RGB, le",
340 .palette = VIDEO_PALETTE_RGB555,
341 .fourcc = V4L2_PIX_FMT_RGB555,
342 .btformat = BT848_COLOR_FMT_RGB15,
343 .depth = 16,
344 .flags = FORMAT_FLAGS_PACKED,
345 },{
346 .name = "15 bpp RGB, be",
347 .palette = -1,
348 .fourcc = V4L2_PIX_FMT_RGB555X,
349 .btformat = BT848_COLOR_FMT_RGB15,
350 .btswap = 0x03, /* byteswap */
351 .depth = 16,
352 .flags = FORMAT_FLAGS_PACKED,
353 },{
354 .name = "16 bpp RGB, le",
355 .palette = VIDEO_PALETTE_RGB565,
356 .fourcc = V4L2_PIX_FMT_RGB565,
357 .btformat = BT848_COLOR_FMT_RGB16,
358 .depth = 16,
359 .flags = FORMAT_FLAGS_PACKED,
360 },{
361 .name = "16 bpp RGB, be",
362 .palette = -1,
363 .fourcc = V4L2_PIX_FMT_RGB565X,
364 .btformat = BT848_COLOR_FMT_RGB16,
365 .btswap = 0x03, /* byteswap */
366 .depth = 16,
367 .flags = FORMAT_FLAGS_PACKED,
368 },{
369 .name = "24 bpp RGB, le",
370 .palette = VIDEO_PALETTE_RGB24,
371 .fourcc = V4L2_PIX_FMT_BGR24,
372 .btformat = BT848_COLOR_FMT_RGB24,
373 .depth = 24,
374 .flags = FORMAT_FLAGS_PACKED,
375 },{
376 .name = "32 bpp RGB, le",
377 .palette = VIDEO_PALETTE_RGB32,
378 .fourcc = V4L2_PIX_FMT_BGR32,
379 .btformat = BT848_COLOR_FMT_RGB32,
380 .depth = 32,
381 .flags = FORMAT_FLAGS_PACKED,
382 },{
383 .name = "32 bpp RGB, be",
384 .palette = -1,
385 .fourcc = V4L2_PIX_FMT_RGB32,
386 .btformat = BT848_COLOR_FMT_RGB32,
387 .btswap = 0x0f, /* byte+word swap */
388 .depth = 32,
389 .flags = FORMAT_FLAGS_PACKED,
390 },{
391 .name = "4:2:2, packed, YUYV",
392 .palette = VIDEO_PALETTE_YUV422,
393 .fourcc = V4L2_PIX_FMT_YUYV,
394 .btformat = BT848_COLOR_FMT_YUY2,
395 .depth = 16,
396 .flags = FORMAT_FLAGS_PACKED,
397 },{
398 .name = "4:2:2, packed, YUYV",
399 .palette = VIDEO_PALETTE_YUYV,
400 .fourcc = V4L2_PIX_FMT_YUYV,
401 .btformat = BT848_COLOR_FMT_YUY2,
402 .depth = 16,
403 .flags = FORMAT_FLAGS_PACKED,
404 },{
405 .name = "4:2:2, packed, UYVY",
406 .palette = VIDEO_PALETTE_UYVY,
407 .fourcc = V4L2_PIX_FMT_UYVY,
408 .btformat = BT848_COLOR_FMT_YUY2,
409 .btswap = 0x03, /* byteswap */
410 .depth = 16,
411 .flags = FORMAT_FLAGS_PACKED,
412 },{
413 .name = "4:2:2, planar, Y-Cb-Cr",
414 .palette = VIDEO_PALETTE_YUV422P,
415 .fourcc = V4L2_PIX_FMT_YUV422P,
416 .btformat = BT848_COLOR_FMT_YCrCb422,
417 .depth = 16,
418 .flags = FORMAT_FLAGS_PLANAR,
419 .hshift = 1,
420 .vshift = 0,
421 },{
422 .name = "4:2:0, planar, Y-Cb-Cr",
423 .palette = VIDEO_PALETTE_YUV420P,
424 .fourcc = V4L2_PIX_FMT_YUV420,
425 .btformat = BT848_COLOR_FMT_YCrCb422,
426 .depth = 12,
427 .flags = FORMAT_FLAGS_PLANAR,
428 .hshift = 1,
429 .vshift = 1,
430 },{
431 .name = "4:2:0, planar, Y-Cr-Cb",
432 .palette = -1,
433 .fourcc = V4L2_PIX_FMT_YVU420,
434 .btformat = BT848_COLOR_FMT_YCrCb422,
435 .depth = 12,
436 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
437 .hshift = 1,
438 .vshift = 1,
439 },{
440 .name = "4:1:1, planar, Y-Cb-Cr",
441 .palette = VIDEO_PALETTE_YUV411P,
442 .fourcc = V4L2_PIX_FMT_YUV411P,
443 .btformat = BT848_COLOR_FMT_YCrCb411,
444 .depth = 12,
445 .flags = FORMAT_FLAGS_PLANAR,
446 .hshift = 2,
447 .vshift = 0,
448 },{
449 .name = "4:1:0, planar, Y-Cb-Cr",
450 .palette = VIDEO_PALETTE_YUV410P,
451 .fourcc = V4L2_PIX_FMT_YUV410,
452 .btformat = BT848_COLOR_FMT_YCrCb411,
453 .depth = 9,
454 .flags = FORMAT_FLAGS_PLANAR,
455 .hshift = 2,
456 .vshift = 2,
457 },{
458 .name = "4:1:0, planar, Y-Cr-Cb",
459 .palette = -1,
460 .fourcc = V4L2_PIX_FMT_YVU410,
461 .btformat = BT848_COLOR_FMT_YCrCb411,
462 .depth = 9,
463 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
464 .hshift = 2,
465 .vshift = 2,
466 },{
467 .name = "raw scanlines",
468 .palette = VIDEO_PALETTE_RAW,
469 .fourcc = -1,
470 .btformat = BT848_COLOR_FMT_RAW,
471 .depth = 8,
472 .flags = FORMAT_FLAGS_RAW,
473 }
474};
475static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
476
477/* ----------------------------------------------------------------------- */
478
479#define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
480#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
481#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
482#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
483#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
484#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
485#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
486#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
487#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 8)
488
489static const struct v4l2_queryctrl no_ctl = {
490 .name = "42",
491 .flags = V4L2_CTRL_FLAG_DISABLED,
492};
493static const struct v4l2_queryctrl bttv_ctls[] = {
494 /* --- video --- */
495 {
496 .id = V4L2_CID_BRIGHTNESS,
497 .name = "Brightness",
498 .minimum = 0,
499 .maximum = 65535,
500 .step = 256,
501 .default_value = 32768,
502 .type = V4L2_CTRL_TYPE_INTEGER,
503 },{
504 .id = V4L2_CID_CONTRAST,
505 .name = "Contrast",
506 .minimum = 0,
507 .maximum = 65535,
508 .step = 128,
509 .default_value = 32768,
510 .type = V4L2_CTRL_TYPE_INTEGER,
511 },{
512 .id = V4L2_CID_SATURATION,
513 .name = "Saturation",
514 .minimum = 0,
515 .maximum = 65535,
516 .step = 128,
517 .default_value = 32768,
518 .type = V4L2_CTRL_TYPE_INTEGER,
519 },{
520 .id = V4L2_CID_HUE,
521 .name = "Hue",
522 .minimum = 0,
523 .maximum = 65535,
524 .step = 256,
525 .default_value = 32768,
526 .type = V4L2_CTRL_TYPE_INTEGER,
527 },
528 /* --- audio --- */
529 {
530 .id = V4L2_CID_AUDIO_MUTE,
531 .name = "Mute",
532 .minimum = 0,
533 .maximum = 1,
534 .type = V4L2_CTRL_TYPE_BOOLEAN,
535 },{
536 .id = V4L2_CID_AUDIO_VOLUME,
537 .name = "Volume",
538 .minimum = 0,
539 .maximum = 65535,
540 .step = 65535/100,
541 .default_value = 65535,
542 .type = V4L2_CTRL_TYPE_INTEGER,
543 },{
544 .id = V4L2_CID_AUDIO_BALANCE,
545 .name = "Balance",
546 .minimum = 0,
547 .maximum = 65535,
548 .step = 65535/100,
549 .default_value = 32768,
550 .type = V4L2_CTRL_TYPE_INTEGER,
551 },{
552 .id = V4L2_CID_AUDIO_BASS,
553 .name = "Bass",
554 .minimum = 0,
555 .maximum = 65535,
556 .step = 65535/100,
557 .default_value = 32768,
558 .type = V4L2_CTRL_TYPE_INTEGER,
559 },{
560 .id = V4L2_CID_AUDIO_TREBLE,
561 .name = "Treble",
562 .minimum = 0,
563 .maximum = 65535,
564 .step = 65535/100,
565 .default_value = 32768,
566 .type = V4L2_CTRL_TYPE_INTEGER,
567 },
568 /* --- private --- */
569 {
570 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
571 .name = "chroma agc",
572 .minimum = 0,
573 .maximum = 1,
574 .type = V4L2_CTRL_TYPE_BOOLEAN,
575 },{
576 .id = V4L2_CID_PRIVATE_COMBFILTER,
577 .name = "combfilter",
578 .minimum = 0,
579 .maximum = 1,
580 .type = V4L2_CTRL_TYPE_BOOLEAN,
581 },{
582 .id = V4L2_CID_PRIVATE_AUTOMUTE,
583 .name = "automute",
584 .minimum = 0,
585 .maximum = 1,
586 .type = V4L2_CTRL_TYPE_BOOLEAN,
587 },{
588 .id = V4L2_CID_PRIVATE_LUMAFILTER,
589 .name = "luma decimation filter",
590 .minimum = 0,
591 .maximum = 1,
592 .type = V4L2_CTRL_TYPE_BOOLEAN,
593 },{
594 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
595 .name = "agc crush",
596 .minimum = 0,
597 .maximum = 1,
598 .type = V4L2_CTRL_TYPE_BOOLEAN,
599 },{
600 .id = V4L2_CID_PRIVATE_VCR_HACK,
601 .name = "vcr hack",
602 .minimum = 0,
603 .maximum = 1,
604 .type = V4L2_CTRL_TYPE_BOOLEAN,
605 },{
606 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
607 .name = "whitecrush upper",
608 .minimum = 0,
609 .maximum = 255,
610 .step = 1,
611 .default_value = 0xCF,
612 .type = V4L2_CTRL_TYPE_INTEGER,
613 },{
614 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
615 .name = "whitecrush lower",
616 .minimum = 0,
617 .maximum = 255,
618 .step = 1,
619 .default_value = 0x7F,
620 .type = V4L2_CTRL_TYPE_INTEGER,
621 }
622
623};
624static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
625
626/* ----------------------------------------------------------------------- */
627/* resource management */
628
629static
630int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
631{
632 if (fh->resources & bit)
633 /* have it already allocated */
634 return 1;
635
636 /* is it free? */
637 down(&btv->reslock);
638 if (btv->resources & bit) {
639 /* no, someone else uses it */
640 up(&btv->reslock);
641 return 0;
642 }
643 /* it's free, grab it */
644 fh->resources |= bit;
645 btv->resources |= bit;
646 up(&btv->reslock);
647 return 1;
648}
649
650static
651int check_btres(struct bttv_fh *fh, int bit)
652{
653 return (fh->resources & bit);
654}
655
656static
657int locked_btres(struct bttv *btv, int bit)
658{
659 return (btv->resources & bit);
660}
661
662static
663void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
664{
665#if 1 /* DEBUG */
666 if ((fh->resources & bits) != bits) {
667 /* trying to free ressources not allocated by us ... */
668 printk("bttv: BUG! (btres)\n");
669 }
670#endif
671 down(&btv->reslock);
672 fh->resources &= ~bits;
673 btv->resources &= ~bits;
674 up(&btv->reslock);
675}
676
677/* ----------------------------------------------------------------------- */
678/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
679
680/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
681 PLL_X = Reference pre-divider (0=1, 1=2)
682 PLL_C = Post divider (0=6, 1=4)
683 PLL_I = Integer input
684 PLL_F = Fractional input
685
686 F_input = 28.636363 MHz:
687 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
688*/
689
690static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
691{
692 unsigned char fl, fh, fi;
693
694 /* prevent overflows */
695 fin/=4;
696 fout/=4;
697
698 fout*=12;
699 fi=fout/fin;
700
701 fout=(fout%fin)*256;
702 fh=fout/fin;
703
704 fout=(fout%fin)*256;
705 fl=fout/fin;
706
707 btwrite(fl, BT848_PLL_F_LO);
708 btwrite(fh, BT848_PLL_F_HI);
709 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
710}
711
712static void set_pll(struct bttv *btv)
713{
714 int i;
715
716 if (!btv->pll.pll_crystal)
717 return;
718
719 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
720 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
721 return;
722 }
723
724 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
725 /* no PLL needed */
726 if (btv->pll.pll_current == 0)
727 return;
728 vprintk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
729 btv->c.nr,btv->pll.pll_ifreq);
730 btwrite(0x00,BT848_TGCTRL);
731 btwrite(0x00,BT848_PLL_XCI);
732 btv->pll.pll_current = 0;
733 return;
734 }
735
736 vprintk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
737 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
738 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
739
740 for (i=0; i<10; i++) {
741 /* Let other people run while the PLL stabilizes */
742 vprintk(".");
743 msleep(10);
744
745 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
746 btwrite(0,BT848_DSTATUS);
747 } else {
748 btwrite(0x08,BT848_TGCTRL);
749 btv->pll.pll_current = btv->pll.pll_ofreq;
750 vprintk(" ok\n");
751 return;
752 }
753 }
754 btv->pll.pll_current = -1;
755 vprintk("failed\n");
756 return;
757}
758
759/* used to switch between the bt848's analog/digital video capture modes */
760static void bt848A_set_timing(struct bttv *btv)
761{
762 int i, len;
763 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
764 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
765
766 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
767 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
768 btv->c.nr,table_idx);
769
770 /* timing change...reset timing generator address */
771 btwrite(0x00, BT848_TGCTRL);
772 btwrite(0x02, BT848_TGCTRL);
773 btwrite(0x00, BT848_TGCTRL);
774
775 len=SRAM_Table[table_idx][0];
776 for(i = 1; i <= len; i++)
777 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
778 btv->pll.pll_ofreq = 27000000;
779
780 set_pll(btv);
781 btwrite(0x11, BT848_TGCTRL);
782 btwrite(0x41, BT848_DVSIF);
783 } else {
784 btv->pll.pll_ofreq = fsc;
785 set_pll(btv);
786 btwrite(0x0, BT848_DVSIF);
787 }
788}
789
790/* ----------------------------------------------------------------------- */
791
792static void bt848_bright(struct bttv *btv, int bright)
793{
794 int value;
795
796 // printk("bttv: set bright: %d\n",bright); // DEBUG
797 btv->bright = bright;
798
799 /* We want -128 to 127 we get 0-65535 */
800 value = (bright >> 8) - 128;
801 btwrite(value & 0xff, BT848_BRIGHT);
802}
803
804static void bt848_hue(struct bttv *btv, int hue)
805{
806 int value;
807
808 btv->hue = hue;
809
810 /* -128 to 127 */
811 value = (hue >> 8) - 128;
812 btwrite(value & 0xff, BT848_HUE);
813}
814
815static void bt848_contrast(struct bttv *btv, int cont)
816{
817 int value,hibit;
818
819 btv->contrast = cont;
820
821 /* 0-511 */
822 value = (cont >> 7);
823 hibit = (value >> 6) & 4;
824 btwrite(value & 0xff, BT848_CONTRAST_LO);
825 btaor(hibit, ~4, BT848_E_CONTROL);
826 btaor(hibit, ~4, BT848_O_CONTROL);
827}
828
829static void bt848_sat(struct bttv *btv, int color)
830{
831 int val_u,val_v,hibits;
832
833 btv->saturation = color;
834
835 /* 0-511 for the color */
836 val_u = color >> 7;
837 val_v = ((color>>7)*180L)/254;
838 hibits = (val_u >> 7) & 2;
839 hibits |= (val_v >> 8) & 1;
840 btwrite(val_u & 0xff, BT848_SAT_U_LO);
841 btwrite(val_v & 0xff, BT848_SAT_V_LO);
842 btaor(hibits, ~3, BT848_E_CONTROL);
843 btaor(hibits, ~3, BT848_O_CONTROL);
844}
845
846/* ----------------------------------------------------------------------- */
847
848static int
849video_mux(struct bttv *btv, unsigned int input)
850{
851 int mux,mask2;
852
853 if (input >= bttv_tvcards[btv->c.type].video_inputs)
854 return -EINVAL;
855
856 /* needed by RemoteVideo MX */
857 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
858 if (mask2)
859 gpio_inout(mask2,mask2);
860
861 if (input == btv->svhs) {
862 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
863 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
864 } else {
865 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
866 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
867 }
868 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
869 btaor(mux<<5, ~(3<<5), BT848_IFORM);
870 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
871 btv->c.nr,input,mux);
872
873 /* card specific hook */
874 if(bttv_tvcards[btv->c.type].muxsel_hook)
875 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
876 return 0;
877}
878
879static char *audio_modes[] = {
880 "audio: tuner", "audio: radio", "audio: extern",
881 "audio: intern", "audio: off"
882};
883
884static int
885audio_mux(struct bttv *btv, int mode)
886{
887 int val,mux,i2c_mux,signal;
888
889 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
890 bttv_tvcards[btv->c.type].gpiomask);
891 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
892
893 switch (mode) {
894 case AUDIO_MUTE:
895 btv->audio |= AUDIO_MUTE;
896 break;
897 case AUDIO_UNMUTE:
898 btv->audio &= ~AUDIO_MUTE;
899 break;
900 case AUDIO_TUNER:
901 case AUDIO_RADIO:
902 case AUDIO_EXTERN:
903 case AUDIO_INTERN:
904 btv->audio &= AUDIO_MUTE;
905 btv->audio |= mode;
906 }
907 i2c_mux = mux = (btv->audio & AUDIO_MUTE) ? AUDIO_OFF : btv->audio;
908 if (btv->opt_automute && !signal && !btv->radio_user)
909 mux = AUDIO_OFF;
910#if 0
911 printk("bttv%d: amux: mode=%d audio=%d signal=%s mux=%d/%d irq=%s\n",
912 btv->c.nr, mode, btv->audio, signal ? "yes" : "no",
913 mux, i2c_mux, in_interrupt() ? "yes" : "no");
914#endif
915
916 val = bttv_tvcards[btv->c.type].audiomux[mux];
917 gpio_bits(bttv_tvcards[btv->c.type].gpiomask,val);
918 if (bttv_gpio)
919 bttv_gpio_tracking(btv,audio_modes[mux]);
920 if (!in_interrupt())
921 bttv_call_i2c_clients(btv,AUDC_SET_INPUT,&(i2c_mux));
922 return 0;
923}
924
925static void
926i2c_vidiocschan(struct bttv *btv)
927{
928 struct video_channel c;
929
930 memset(&c,0,sizeof(c));
931 c.norm = btv->tvnorm;
932 c.channel = btv->input;
933 bttv_call_i2c_clients(btv,VIDIOCSCHAN,&c);
934 if (btv->c.type == BTTV_VOODOOTV_FM)
935 bttv_tda9880_setnorm(btv,c.norm);
936}
937
938static int
939set_tvnorm(struct bttv *btv, unsigned int norm)
940{
941 const struct bttv_tvnorm *tvnorm;
942
943 if (norm < 0 || norm >= BTTV_TVNORMS)
944 return -EINVAL;
945
946 btv->tvnorm = norm;
947 tvnorm = &bttv_tvnorms[norm];
948
949 btwrite(tvnorm->adelay, BT848_ADELAY);
950 btwrite(tvnorm->bdelay, BT848_BDELAY);
951 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
952 BT848_IFORM);
953 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
954 btwrite(1, BT848_VBI_PACK_DEL);
955 bt848A_set_timing(btv);
956
957 switch (btv->c.type) {
958 case BTTV_VOODOOTV_FM:
959 bttv_tda9880_setnorm(btv,norm);
960 break;
961#if 0
962 case BTTV_OSPREY540:
963 osprey_540_set_norm(btv,norm);
964 break;
965#endif
966 }
967 return 0;
968}
969
970static void
971set_input(struct bttv *btv, unsigned int input)
972{
973 unsigned long flags;
974
975 btv->input = input;
976 if (irq_iswitch) {
977 spin_lock_irqsave(&btv->s_lock,flags);
978 if (btv->curr.frame_irq) {
979 /* active capture -> delayed input switch */
980 btv->new_input = input;
981 } else {
982 video_mux(btv,input);
983 }
984 spin_unlock_irqrestore(&btv->s_lock,flags);
985 } else {
986 video_mux(btv,input);
987 }
988 audio_mux(btv,(input == bttv_tvcards[btv->c.type].tuner ?
989 AUDIO_TUNER : AUDIO_EXTERN));
990 set_tvnorm(btv,btv->tvnorm);
991 i2c_vidiocschan(btv);
992}
993
994static void init_irqreg(struct bttv *btv)
995{
996 /* clear status */
997 btwrite(0xfffffUL, BT848_INT_STAT);
998
999 if (bttv_tvcards[btv->c.type].no_video) {
1000 /* i2c only */
1001 btwrite(BT848_INT_I2CDONE,
1002 BT848_INT_MASK);
1003 } else {
1004 /* full video */
1005 btwrite((btv->triton1) |
1006 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1007 BT848_INT_SCERR |
1008 (fdsr ? BT848_INT_FDSR : 0) |
1009 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1010 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1011 BT848_INT_I2CDONE,
1012 BT848_INT_MASK);
1013 }
1014}
1015
1016static void init_bt848(struct bttv *btv)
1017{
1018 int val;
1019
1020 if (bttv_tvcards[btv->c.type].no_video) {
1021 /* very basic init only */
1022 init_irqreg(btv);
1023 return;
1024 }
1025
1026 btwrite(0x00, BT848_CAP_CTL);
1027 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1028 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1029
1030 /* set planar and packed mode trigger points and */
1031 /* set rising edge of inverted GPINTR pin as irq trigger */
1032 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1033 BT848_GPIO_DMA_CTL_PLTP1_16|
1034 BT848_GPIO_DMA_CTL_PLTP23_16|
1035 BT848_GPIO_DMA_CTL_GPINTC|
1036 BT848_GPIO_DMA_CTL_GPINTI,
1037 BT848_GPIO_DMA_CTL);
1038
1039 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1040 btwrite(val, BT848_E_SCLOOP);
1041 btwrite(val, BT848_O_SCLOOP);
1042
1043 btwrite(0x20, BT848_E_VSCALE_HI);
1044 btwrite(0x20, BT848_O_VSCALE_HI);
1045 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1046 BT848_ADC);
1047
1048 btwrite(whitecrush_upper, BT848_WC_UP);
1049 btwrite(whitecrush_lower, BT848_WC_DOWN);
1050
1051 if (btv->opt_lumafilter) {
1052 btwrite(0, BT848_E_CONTROL);
1053 btwrite(0, BT848_O_CONTROL);
1054 } else {
1055 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1056 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1057 }
1058
1059 bt848_bright(btv, btv->bright);
1060 bt848_hue(btv, btv->hue);
1061 bt848_contrast(btv, btv->contrast);
1062 bt848_sat(btv, btv->saturation);
1063
1064 /* interrupt */
1065 init_irqreg(btv);
1066}
1067
1068static void bttv_reinit_bt848(struct bttv *btv)
1069{
1070 unsigned long flags;
1071
1072 if (bttv_verbose)
1073 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1074 spin_lock_irqsave(&btv->s_lock,flags);
1075 btv->errors=0;
1076 bttv_set_dma(btv,0);
1077 spin_unlock_irqrestore(&btv->s_lock,flags);
1078
1079 init_bt848(btv);
1080 btv->pll.pll_current = -1;
1081 set_input(btv,btv->input);
1082}
1083
1084static int get_control(struct bttv *btv, struct v4l2_control *c)
1085{
1086 struct video_audio va;
1087 int i;
1088
1089 for (i = 0; i < BTTV_CTLS; i++)
1090 if (bttv_ctls[i].id == c->id)
1091 break;
1092 if (i == BTTV_CTLS)
1093 return -EINVAL;
1094 if (i >= 4 && i <= 8) {
1095 memset(&va,0,sizeof(va));
1096 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1097 if (btv->audio_hook)
1098 btv->audio_hook(btv,&va,0);
1099 }
1100 switch (c->id) {
1101 case V4L2_CID_BRIGHTNESS:
1102 c->value = btv->bright;
1103 break;
1104 case V4L2_CID_HUE:
1105 c->value = btv->hue;
1106 break;
1107 case V4L2_CID_CONTRAST:
1108 c->value = btv->contrast;
1109 break;
1110 case V4L2_CID_SATURATION:
1111 c->value = btv->saturation;
1112 break;
1113
1114 case V4L2_CID_AUDIO_MUTE:
1115 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1116 break;
1117 case V4L2_CID_AUDIO_VOLUME:
1118 c->value = va.volume;
1119 break;
1120 case V4L2_CID_AUDIO_BALANCE:
1121 c->value = va.balance;
1122 break;
1123 case V4L2_CID_AUDIO_BASS:
1124 c->value = va.bass;
1125 break;
1126 case V4L2_CID_AUDIO_TREBLE:
1127 c->value = va.treble;
1128 break;
1129
1130 case V4L2_CID_PRIVATE_CHROMA_AGC:
1131 c->value = btv->opt_chroma_agc;
1132 break;
1133 case V4L2_CID_PRIVATE_COMBFILTER:
1134 c->value = btv->opt_combfilter;
1135 break;
1136 case V4L2_CID_PRIVATE_LUMAFILTER:
1137 c->value = btv->opt_lumafilter;
1138 break;
1139 case V4L2_CID_PRIVATE_AUTOMUTE:
1140 c->value = btv->opt_automute;
1141 break;
1142 case V4L2_CID_PRIVATE_AGC_CRUSH:
1143 c->value = btv->opt_adc_crush;
1144 break;
1145 case V4L2_CID_PRIVATE_VCR_HACK:
1146 c->value = btv->opt_vcr_hack;
1147 break;
1148 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1149 c->value = btv->opt_whitecrush_upper;
1150 break;
1151 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1152 c->value = btv->opt_whitecrush_lower;
1153 break;
1154 default:
1155 return -EINVAL;
1156 }
1157 return 0;
1158}
1159
1160static int set_control(struct bttv *btv, struct v4l2_control *c)
1161{
1162 struct video_audio va;
1163 int i,val;
1164
1165 for (i = 0; i < BTTV_CTLS; i++)
1166 if (bttv_ctls[i].id == c->id)
1167 break;
1168 if (i == BTTV_CTLS)
1169 return -EINVAL;
1170 if (i >= 4 && i <= 8) {
1171 memset(&va,0,sizeof(va));
1172 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1173 if (btv->audio_hook)
1174 btv->audio_hook(btv,&va,0);
1175 }
1176 switch (c->id) {
1177 case V4L2_CID_BRIGHTNESS:
1178 bt848_bright(btv,c->value);
1179 break;
1180 case V4L2_CID_HUE:
1181 bt848_hue(btv,c->value);
1182 break;
1183 case V4L2_CID_CONTRAST:
1184 bt848_contrast(btv,c->value);
1185 break;
1186 case V4L2_CID_SATURATION:
1187 bt848_sat(btv,c->value);
1188 break;
1189 case V4L2_CID_AUDIO_MUTE:
1190 if (c->value) {
1191 va.flags |= VIDEO_AUDIO_MUTE;
1192 audio_mux(btv, AUDIO_MUTE);
1193 } else {
1194 va.flags &= ~VIDEO_AUDIO_MUTE;
1195 audio_mux(btv, AUDIO_UNMUTE);
1196 }
1197 break;
1198
1199 case V4L2_CID_AUDIO_VOLUME:
1200 va.volume = c->value;
1201 break;
1202 case V4L2_CID_AUDIO_BALANCE:
1203 va.balance = c->value;
1204 break;
1205 case V4L2_CID_AUDIO_BASS:
1206 va.bass = c->value;
1207 break;
1208 case V4L2_CID_AUDIO_TREBLE:
1209 va.treble = c->value;
1210 break;
1211
1212 case V4L2_CID_PRIVATE_CHROMA_AGC:
1213 btv->opt_chroma_agc = c->value;
1214 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1215 btwrite(val, BT848_E_SCLOOP);
1216 btwrite(val, BT848_O_SCLOOP);
1217 break;
1218 case V4L2_CID_PRIVATE_COMBFILTER:
1219 btv->opt_combfilter = c->value;
1220 break;
1221 case V4L2_CID_PRIVATE_LUMAFILTER:
1222 btv->opt_lumafilter = c->value;
1223 if (btv->opt_lumafilter) {
1224 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1225 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1226 } else {
1227 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1228 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1229 }
1230 break;
1231 case V4L2_CID_PRIVATE_AUTOMUTE:
1232 btv->opt_automute = c->value;
1233 break;
1234 case V4L2_CID_PRIVATE_AGC_CRUSH:
1235 btv->opt_adc_crush = c->value;
1236 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1237 BT848_ADC);
1238 break;
1239 case V4L2_CID_PRIVATE_VCR_HACK:
1240 btv->opt_vcr_hack = c->value;
1241 break;
1242 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1243 btv->opt_whitecrush_upper = c->value;
1244 btwrite(c->value, BT848_WC_UP);
1245 break;
1246 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1247 btv->opt_whitecrush_lower = c->value;
1248 btwrite(c->value, BT848_WC_DOWN);
1249 break;
1250 default:
1251 return -EINVAL;
1252 }
1253 if (i >= 4 && i <= 8) {
1254 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1255 if (btv->audio_hook)
1256 btv->audio_hook(btv,&va,1);
1257 }
1258 return 0;
1259}
1260
1261/* ----------------------------------------------------------------------- */
1262
1263void bttv_gpio_tracking(struct bttv *btv, char *comment)
1264{
1265 unsigned int outbits, data;
1266 outbits = btread(BT848_GPIO_OUT_EN);
1267 data = btread(BT848_GPIO_DATA);
1268 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1269 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1270}
1271
1272static void bttv_field_count(struct bttv *btv)
1273{
1274 int need_count = 0;
1275
1276 if (btv->users)
1277 need_count++;
1278
1279 if (need_count) {
1280 /* start field counter */
1281 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1282 } else {
1283 /* stop field counter */
1284 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1285 btv->field_count = 0;
1286 }
1287}
1288
1289static const struct bttv_format*
1290format_by_palette(int palette)
1291{
1292 unsigned int i;
1293
1294 for (i = 0; i < BTTV_FORMATS; i++) {
1295 if (-1 == bttv_formats[i].palette)
1296 continue;
1297 if (bttv_formats[i].palette == palette)
1298 return bttv_formats+i;
1299 }
1300 return NULL;
1301}
1302
1303static const struct bttv_format*
1304format_by_fourcc(int fourcc)
1305{
1306 unsigned int i;
1307
1308 for (i = 0; i < BTTV_FORMATS; i++) {
1309 if (-1 == bttv_formats[i].fourcc)
1310 continue;
1311 if (bttv_formats[i].fourcc == fourcc)
1312 return bttv_formats+i;
1313 }
1314 return NULL;
1315}
1316
1317/* ----------------------------------------------------------------------- */
1318/* misc helpers */
1319
1320static int
1321bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1322 struct bttv_buffer *new)
1323{
1324 struct bttv_buffer *old;
1325 unsigned long flags;
1326 int retval = 0;
1327
1328 dprintk("switch_overlay: enter [new=%p]\n",new);
1329 if (new)
1330 new->vb.state = STATE_DONE;
1331 spin_lock_irqsave(&btv->s_lock,flags);
1332 old = btv->screen;
1333 btv->screen = new;
1334 btv->loop_irq |= 1;
1335 bttv_set_dma(btv, 0x03);
1336 spin_unlock_irqrestore(&btv->s_lock,flags);
1337 if (NULL == new)
1338 free_btres(btv,fh,RESOURCE_OVERLAY);
1339 if (NULL != old) {
1340 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1341 bttv_dma_free(btv, old);
1342 kfree(old);
1343 }
1344 dprintk("switch_overlay: done\n");
1345 return retval;
1346}
1347
1348/* ----------------------------------------------------------------------- */
1349/* video4linux (1) interface */
1350
1351static int bttv_prepare_buffer(struct bttv *btv, struct bttv_buffer *buf,
1352 const struct bttv_format *fmt,
1353 unsigned int width, unsigned int height,
1354 enum v4l2_field field)
1355{
1356 int redo_dma_risc = 0;
1357 int rc;
1358
1359 /* check settings */
1360 if (NULL == fmt)
1361 return -EINVAL;
1362 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1363 width = RAW_BPL;
1364 height = RAW_LINES*2;
1365 if (width*height > buf->vb.bsize)
1366 return -EINVAL;
1367 buf->vb.size = buf->vb.bsize;
1368 } else {
1369 if (width < 48 ||
1370 height < 32 ||
1371 width > bttv_tvnorms[btv->tvnorm].swidth ||
1372 height > bttv_tvnorms[btv->tvnorm].sheight)
1373 return -EINVAL;
1374 buf->vb.size = (width * height * fmt->depth) >> 3;
1375 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1376 return -EINVAL;
1377 }
1378
1379 /* alloc + fill struct bttv_buffer (if changed) */
1380 if (buf->vb.width != width || buf->vb.height != height ||
1381 buf->vb.field != field ||
1382 buf->tvnorm != btv->tvnorm || buf->fmt != fmt) {
1383 buf->vb.width = width;
1384 buf->vb.height = height;
1385 buf->vb.field = field;
1386 buf->tvnorm = btv->tvnorm;
1387 buf->fmt = fmt;
1388 redo_dma_risc = 1;
1389 }
1390
1391 /* alloc risc memory */
1392 if (STATE_NEEDS_INIT == buf->vb.state) {
1393 redo_dma_risc = 1;
1394 if (0 != (rc = videobuf_iolock(btv->c.pci,&buf->vb,&btv->fbuf)))
1395 goto fail;
1396 }
1397
1398 if (redo_dma_risc)
1399 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1400 goto fail;
1401
1402 buf->vb.state = STATE_PREPARED;
1403 return 0;
1404
1405 fail:
1406 bttv_dma_free(btv,buf);
1407 return rc;
1408}
1409
1410static int
1411buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1412{
1413 struct bttv_fh *fh = q->priv_data;
1414
1415 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1416 if (0 == *count)
1417 *count = gbuffers;
1418 while (*size * *count > gbuffers * gbufsize)
1419 (*count)--;
1420 return 0;
1421}
1422
1423static int
1424buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1425 enum v4l2_field field)
1426{
1427 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1428 struct bttv_fh *fh = q->priv_data;
1429
1430 return bttv_prepare_buffer(fh->btv, buf, fh->fmt,
1431 fh->width, fh->height, field);
1432}
1433
1434static void
1435buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1436{
1437 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1438 struct bttv_fh *fh = q->priv_data;
1439 struct bttv *btv = fh->btv;
1440
1441 buf->vb.state = STATE_QUEUED;
1442 list_add_tail(&buf->vb.queue,&btv->capture);
1443 if (!btv->curr.frame_irq) {
1444 btv->loop_irq |= 1;
1445 bttv_set_dma(btv, 0x03);
1446 }
1447}
1448
1449static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1450{
1451 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1452 struct bttv_fh *fh = q->priv_data;
1453
1454 bttv_dma_free(fh->btv,buf);
1455}
1456
1457static struct videobuf_queue_ops bttv_video_qops = {
1458 .buf_setup = buffer_setup,
1459 .buf_prepare = buffer_prepare,
1460 .buf_queue = buffer_queue,
1461 .buf_release = buffer_release,
1462};
1463
1464static const char *v4l1_ioctls[] = {
1465 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
1466 "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
1467 "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
1468 "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
1469 "SMICROCODE", "GVBIFMT", "SVBIFMT" };
1470#define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
1471
1472static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1473{
1474 switch (cmd) {
1475 case BTTV_VERSION:
1476 return BTTV_VERSION_CODE;
1477
1478 /* *** v4l1 *** ************************************************ */
1479 case VIDIOCGFREQ:
1480 {
1481 unsigned long *freq = arg;
1482 *freq = btv->freq;
1483 return 0;
1484 }
1485 case VIDIOCSFREQ:
1486 {
1487 unsigned long *freq = arg;
1488 down(&btv->lock);
1489 btv->freq=*freq;
1490 bttv_call_i2c_clients(btv,VIDIOCSFREQ,freq);
1491 if (btv->has_matchbox && btv->radio_user)
1492 tea5757_set_freq(btv,*freq);
1493 up(&btv->lock);
1494 return 0;
1495 }
1496
1497 case VIDIOCGTUNER:
1498 {
1499 struct video_tuner *v = arg;
1500
1501 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1502 return -EINVAL;
1503 if (v->tuner) /* Only tuner 0 */
1504 return -EINVAL;
1505 strcpy(v->name, "Television");
1506 v->rangelow = 0;
1507 v->rangehigh = 0x7FFFFFFF;
1508 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1509 v->mode = btv->tvnorm;
1510 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1511 bttv_call_i2c_clients(btv,cmd,v);
1512 return 0;
1513 }
1514 case VIDIOCSTUNER:
1515 {
1516 struct video_tuner *v = arg;
1517
1518 if (v->tuner) /* Only tuner 0 */
1519 return -EINVAL;
1520 if (v->mode >= BTTV_TVNORMS)
1521 return -EINVAL;
1522
1523 down(&btv->lock);
1524 set_tvnorm(btv,v->mode);
1525 bttv_call_i2c_clients(btv,cmd,v);
1526 up(&btv->lock);
1527 return 0;
1528 }
1529
1530 case VIDIOCGCHAN:
1531 {
1532 struct video_channel *v = arg;
1533 unsigned int channel = v->channel;
1534
1535 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1536 return -EINVAL;
1537 v->tuners=0;
1538 v->flags = VIDEO_VC_AUDIO;
1539 v->type = VIDEO_TYPE_CAMERA;
1540 v->norm = btv->tvnorm;
1541 if (channel == bttv_tvcards[btv->c.type].tuner) {
1542 strcpy(v->name,"Television");
1543 v->flags|=VIDEO_VC_TUNER;
1544 v->type=VIDEO_TYPE_TV;
1545 v->tuners=1;
1546 } else if (channel == btv->svhs) {
1547 strcpy(v->name,"S-Video");
1548 } else {
1549 sprintf(v->name,"Composite%d",channel);
1550 }
1551 return 0;
1552 }
1553 case VIDIOCSCHAN:
1554 {
1555 struct video_channel *v = arg;
1556 unsigned int channel = v->channel;
1557
1558 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1559 return -EINVAL;
1560 if (v->norm >= BTTV_TVNORMS)
1561 return -EINVAL;
1562
1563 down(&btv->lock);
1564 if (channel == btv->input &&
1565 v->norm == btv->tvnorm) {
1566 /* nothing to do */
1567 up(&btv->lock);
1568 return 0;
1569 }
1570
1571 btv->tvnorm = v->norm;
1572 set_input(btv,v->channel);
1573 up(&btv->lock);
1574 return 0;
1575 }
1576
1577 case VIDIOCGAUDIO:
1578 {
1579 struct video_audio *v = arg;
1580
1581 memset(v,0,sizeof(*v));
1582 strcpy(v->name,"Television");
1583 v->flags |= VIDEO_AUDIO_MUTABLE;
1584 v->mode = VIDEO_SOUND_MONO;
1585
1586 down(&btv->lock);
1587 bttv_call_i2c_clients(btv,cmd,v);
1588
1589 /* card specific hooks */
1590 if (btv->audio_hook)
1591 btv->audio_hook(btv,v,0);
1592
1593 up(&btv->lock);
1594 return 0;
1595 }
1596 case VIDIOCSAUDIO:
1597 {
1598 struct video_audio *v = arg;
1599 unsigned int audio = v->audio;
1600
1601 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
1602 return -EINVAL;
1603
1604 down(&btv->lock);
1605 audio_mux(btv, (v->flags&VIDEO_AUDIO_MUTE) ? AUDIO_MUTE : AUDIO_UNMUTE);
1606 bttv_call_i2c_clients(btv,cmd,v);
1607
1608 /* card specific hooks */
1609 if (btv->audio_hook)
1610 btv->audio_hook(btv,v,1);
1611
1612 up(&btv->lock);
1613 return 0;
1614 }
1615
1616 /* *** v4l2 *** ************************************************ */
1617 case VIDIOC_ENUMSTD:
1618 {
1619 struct v4l2_standard *e = arg;
1620 unsigned int index = e->index;
1621
1622 if (index >= BTTV_TVNORMS)
1623 return -EINVAL;
1624 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1625 bttv_tvnorms[e->index].name);
1626 e->index = index;
1627 return 0;
1628 }
1629 case VIDIOC_G_STD:
1630 {
1631 v4l2_std_id *id = arg;
1632 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1633 return 0;
1634 }
1635 case VIDIOC_S_STD:
1636 {
1637 v4l2_std_id *id = arg;
1638 unsigned int i;
1639
1640 for (i = 0; i < BTTV_TVNORMS; i++)
1641 if (*id & bttv_tvnorms[i].v4l2_id)
1642 break;
1643 if (i == BTTV_TVNORMS)
1644 return -EINVAL;
1645
1646 down(&btv->lock);
1647 set_tvnorm(btv,i);
1648 i2c_vidiocschan(btv);
1649 up(&btv->lock);
1650 return 0;
1651 }
1652 case VIDIOC_QUERYSTD:
1653 {
1654 v4l2_std_id *id = arg;
1655
1656 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1657 *id = V4L2_STD_625_50;
1658 else
1659 *id = V4L2_STD_525_60;
1660 return 0;
1661 }
1662
1663 case VIDIOC_ENUMINPUT:
1664 {
1665 struct v4l2_input *i = arg;
1666 unsigned int n;
1667
1668 n = i->index;
1669 if (n >= bttv_tvcards[btv->c.type].video_inputs)
1670 return -EINVAL;
1671 memset(i,0,sizeof(*i));
1672 i->index = n;
1673 i->type = V4L2_INPUT_TYPE_CAMERA;
1674 i->audioset = 1;
1675 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1676 sprintf(i->name, "Television");
1677 i->type = V4L2_INPUT_TYPE_TUNER;
1678 i->tuner = 0;
1679 } else if (i->index == btv->svhs) {
1680 sprintf(i->name, "S-Video");
1681 } else {
1682 sprintf(i->name,"Composite%d",i->index);
1683 }
1684 if (i->index == btv->input) {
1685 __u32 dstatus = btread(BT848_DSTATUS);
1686 if (0 == (dstatus & BT848_DSTATUS_PRES))
1687 i->status |= V4L2_IN_ST_NO_SIGNAL;
1688 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1689 i->status |= V4L2_IN_ST_NO_H_LOCK;
1690 }
1691 for (n = 0; n < BTTV_TVNORMS; n++)
1692 i->std |= bttv_tvnorms[n].v4l2_id;
1693 return 0;
1694 }
1695 case VIDIOC_G_INPUT:
1696 {
1697 int *i = arg;
1698 *i = btv->input;
1699 return 0;
1700 }
1701 case VIDIOC_S_INPUT:
1702 {
1703 unsigned int *i = arg;
1704
1705 if (*i > bttv_tvcards[btv->c.type].video_inputs)
1706 return -EINVAL;
1707 down(&btv->lock);
1708 set_input(btv,*i);
1709 up(&btv->lock);
1710 return 0;
1711 }
1712
1713 case VIDIOC_G_TUNER:
1714 {
1715 struct v4l2_tuner *t = arg;
1716
1717 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1718 return -EINVAL;
1719 if (0 != t->index)
1720 return -EINVAL;
1721 down(&btv->lock);
1722 memset(t,0,sizeof(*t));
1723 strcpy(t->name, "Television");
1724 t->type = V4L2_TUNER_ANALOG_TV;
1725 t->rangehigh = 0xffffffffUL;
1726 t->capability = V4L2_TUNER_CAP_NORM;
1727 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1728 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
1729 t->signal = 0xffff;
1730 {
1731 /* Hmmm ... */
1732 struct video_audio va;
1733 memset(&va, 0, sizeof(struct video_audio));
1734 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1735 if (btv->audio_hook)
1736 btv->audio_hook(btv,&va,0);
1737 if(va.mode & VIDEO_SOUND_STEREO) {
1738 t->audmode = V4L2_TUNER_MODE_STEREO;
1739 t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1740 }
1741 if(va.mode & VIDEO_SOUND_LANG1) {
1742 t->audmode = V4L2_TUNER_MODE_LANG1;
1743 t->rxsubchans = V4L2_TUNER_SUB_LANG1
1744 | V4L2_TUNER_SUB_LANG2;
1745 }
1746 }
1747 /* FIXME: fill capability+audmode */
1748 up(&btv->lock);
1749 return 0;
1750 }
1751 case VIDIOC_S_TUNER:
1752 {
1753 struct v4l2_tuner *t = arg;
1754
1755 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1756 return -EINVAL;
1757 if (0 != t->index)
1758 return -EINVAL;
1759 down(&btv->lock);
1760 {
1761 struct video_audio va;
1762 memset(&va, 0, sizeof(struct video_audio));
1763 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1764 if (t->audmode == V4L2_TUNER_MODE_MONO)
1765 va.mode = VIDEO_SOUND_MONO;
1766 else if (t->audmode == V4L2_TUNER_MODE_STEREO)
1767 va.mode = VIDEO_SOUND_STEREO;
1768 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1769 va.mode = VIDEO_SOUND_LANG1;
1770 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1771 va.mode = VIDEO_SOUND_LANG2;
1772 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1773 if (btv->audio_hook)
1774 btv->audio_hook(btv,&va,1);
1775 }
1776 up(&btv->lock);
1777 return 0;
1778 }
1779
1780 case VIDIOC_G_FREQUENCY:
1781 {
1782 struct v4l2_frequency *f = arg;
1783
1784 memset(f,0,sizeof(*f));
1785 f->type = V4L2_TUNER_ANALOG_TV;
1786 f->frequency = btv->freq;
1787 return 0;
1788 }
1789 case VIDIOC_S_FREQUENCY:
1790 {
1791 struct v4l2_frequency *f = arg;
1792
1793 if (unlikely(f->tuner != 0))
1794 return -EINVAL;
1795 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1796 return -EINVAL;
1797 down(&btv->lock);
1798 btv->freq = f->frequency;
1799 bttv_call_i2c_clients(btv,VIDIOCSFREQ,&btv->freq);
1800 if (btv->has_matchbox && btv->radio_user)
1801 tea5757_set_freq(btv,btv->freq);
1802 up(&btv->lock);
1803 return 0;
1804 }
1805
1806 default:
1807 return -ENOIOCTLCMD;
1808
1809 }
1810 return 0;
1811}
1812
1813static int verify_window(const struct bttv_tvnorm *tvn,
1814 struct v4l2_window *win, int fixup)
1815{
1816 enum v4l2_field field;
1817 int maxw, maxh;
1818
1819 if (win->w.width < 48 || win->w.height < 32)
1820 return -EINVAL;
1821 if (win->clipcount > 2048)
1822 return -EINVAL;
1823
1824 field = win->field;
1825 maxw = tvn->swidth;
1826 maxh = tvn->sheight;
1827
1828 if (V4L2_FIELD_ANY == field) {
1829 field = (win->w.height > maxh/2)
1830 ? V4L2_FIELD_INTERLACED
1831 : V4L2_FIELD_TOP;
1832 }
1833 switch (field) {
1834 case V4L2_FIELD_TOP:
1835 case V4L2_FIELD_BOTTOM:
1836 maxh = maxh / 2;
1837 break;
1838 case V4L2_FIELD_INTERLACED:
1839 break;
1840 default:
1841 return -EINVAL;
1842 }
1843
1844 if (!fixup && (win->w.width > maxw || win->w.height > maxh))
1845 return -EINVAL;
1846
1847 if (win->w.width > maxw)
1848 win->w.width = maxw;
1849 if (win->w.height > maxh)
1850 win->w.height = maxh;
1851 win->field = field;
1852 return 0;
1853}
1854
1855static int setup_window(struct bttv_fh *fh, struct bttv *btv,
1856 struct v4l2_window *win, int fixup)
1857{
1858 struct v4l2_clip *clips = NULL;
1859 int n,size,retval = 0;
1860
1861 if (NULL == fh->ovfmt)
1862 return -EINVAL;
1863 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
1864 return -EINVAL;
1865 retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
1866 if (0 != retval)
1867 return retval;
1868
1869 /* copy clips -- luckily v4l1 + v4l2 are binary
1870 compatible here ...*/
1871 n = win->clipcount;
1872 size = sizeof(*clips)*(n+4);
1873 clips = kmalloc(size,GFP_KERNEL);
1874 if (NULL == clips)
1875 return -ENOMEM;
1876 if (n > 0) {
1877 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
1878 kfree(clips);
1879 return -EFAULT;
1880 }
1881 }
1882 /* clip against screen */
1883 if (NULL != btv->fbuf.base)
1884 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
1885 &win->w, clips, n);
1886 btcx_sort_clips(clips,n);
1887
1888 /* 4-byte alignments */
1889 switch (fh->ovfmt->depth) {
1890 case 8:
1891 case 24:
1892 btcx_align(&win->w, clips, n, 3);
1893 break;
1894 case 16:
1895 btcx_align(&win->w, clips, n, 1);
1896 break;
1897 case 32:
1898 /* no alignment fixups needed */
1899 break;
1900 default:
1901 BUG();
1902 }
1903
1904 down(&fh->cap.lock);
1905 if (fh->ov.clips)
1906 kfree(fh->ov.clips);
1907 fh->ov.clips = clips;
1908 fh->ov.nclips = n;
1909
1910 fh->ov.w = win->w;
1911 fh->ov.field = win->field;
1912 fh->ov.setup_ok = 1;
1913 btv->init.ov.w.width = win->w.width;
1914 btv->init.ov.w.height = win->w.height;
1915 btv->init.ov.field = win->field;
1916
1917 /* update overlay if needed */
1918 retval = 0;
1919 if (check_btres(fh, RESOURCE_OVERLAY)) {
1920 struct bttv_buffer *new;
1921
1922 new = videobuf_alloc(sizeof(*new));
1923 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
1924 retval = bttv_switch_overlay(btv,fh,new);
1925 }
1926 up(&fh->cap.lock);
1927 return retval;
1928}
1929
1930/* ----------------------------------------------------------------------- */
1931
1932static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
1933{
1934 struct videobuf_queue* q = NULL;
1935
1936 switch (fh->type) {
1937 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1938 q = &fh->cap;
1939 break;
1940 case V4L2_BUF_TYPE_VBI_CAPTURE:
1941 q = &fh->vbi;
1942 break;
1943 default:
1944 BUG();
1945 }
1946 return q;
1947}
1948
1949static int bttv_resource(struct bttv_fh *fh)
1950{
1951 int res = 0;
1952
1953 switch (fh->type) {
1954 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1955 res = RESOURCE_VIDEO;
1956 break;
1957 case V4L2_BUF_TYPE_VBI_CAPTURE:
1958 res = RESOURCE_VBI;
1959 break;
1960 default:
1961 BUG();
1962 }
1963 return res;
1964}
1965
1966static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
1967{
1968 struct videobuf_queue *q = bttv_queue(fh);
1969 int res = bttv_resource(fh);
1970
1971 if (check_btres(fh,res))
1972 return -EBUSY;
1973 if (videobuf_queue_is_busy(q))
1974 return -EBUSY;
1975 fh->type = type;
1976 return 0;
1977}
1978
1979static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
1980{
1981 switch (f->type) {
1982 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1983 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
1984 f->fmt.pix.width = fh->width;
1985 f->fmt.pix.height = fh->height;
1986 f->fmt.pix.field = fh->cap.field;
1987 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1988 f->fmt.pix.bytesperline =
1989 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1990 f->fmt.pix.sizeimage =
1991 f->fmt.pix.height * f->fmt.pix.bytesperline;
1992 return 0;
1993 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1994 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
1995 f->fmt.win.w = fh->ov.w;
1996 f->fmt.win.field = fh->ov.field;
1997 return 0;
1998 case V4L2_BUF_TYPE_VBI_CAPTURE:
1999 bttv_vbi_get_fmt(fh,f);
2000 return 0;
2001 default:
2002 return -EINVAL;
2003 }
2004}
2005
2006static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2007 struct v4l2_format *f)
2008{
2009 switch (f->type) {
2010 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2011 {
2012 const struct bttv_format *fmt;
2013 enum v4l2_field field;
2014 unsigned int maxw,maxh;
2015
2016 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2017 if (NULL == fmt)
2018 return -EINVAL;
2019
2020 /* fixup format */
2021 maxw = bttv_tvnorms[btv->tvnorm].swidth;
2022 maxh = bttv_tvnorms[btv->tvnorm].sheight;
2023 field = f->fmt.pix.field;
2024 if (V4L2_FIELD_ANY == field)
2025 field = (f->fmt.pix.height > maxh/2)
2026 ? V4L2_FIELD_INTERLACED
2027 : V4L2_FIELD_BOTTOM;
2028 if (V4L2_FIELD_SEQ_BT == field)
2029 field = V4L2_FIELD_SEQ_TB;
2030 switch (field) {
2031 case V4L2_FIELD_TOP:
2032 case V4L2_FIELD_BOTTOM:
2033 case V4L2_FIELD_ALTERNATE:
2034 maxh = maxh/2;
2035 break;
2036 case V4L2_FIELD_INTERLACED:
2037 break;
2038 case V4L2_FIELD_SEQ_TB:
2039 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2040 return -EINVAL;
2041 break;
2042 default:
2043 return -EINVAL;
2044 }
2045
2046 /* update data for the application */
2047 f->fmt.pix.field = field;
2048 if (f->fmt.pix.width < 48)
2049 f->fmt.pix.width = 48;
2050 if (f->fmt.pix.height < 32)
2051 f->fmt.pix.height = 32;
2052 if (f->fmt.pix.width > maxw)
2053 f->fmt.pix.width = maxw;
2054 if (f->fmt.pix.height > maxh)
2055 f->fmt.pix.height = maxh;
2056 f->fmt.pix.width &= ~0x03;
2057 f->fmt.pix.bytesperline =
2058 (f->fmt.pix.width * fmt->depth) >> 3;
2059 f->fmt.pix.sizeimage =
2060 f->fmt.pix.height * f->fmt.pix.bytesperline;
2061
2062 return 0;
2063 }
2064 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2065 return verify_window(&bttv_tvnorms[btv->tvnorm],
2066 &f->fmt.win, 1);
2067 case V4L2_BUF_TYPE_VBI_CAPTURE:
2068 bttv_vbi_try_fmt(fh,f);
2069 return 0;
2070 default:
2071 return -EINVAL;
2072 }
2073}
2074
2075static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2076 struct v4l2_format *f)
2077{
2078 int retval;
2079
2080 switch (f->type) {
2081 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2082 {
2083 const struct bttv_format *fmt;
2084
2085 retval = bttv_switch_type(fh,f->type);
2086 if (0 != retval)
2087 return retval;
2088 retval = bttv_try_fmt(fh,btv,f);
2089 if (0 != retval)
2090 return retval;
2091 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2092
2093 /* update our state informations */
2094 down(&fh->cap.lock);
2095 fh->fmt = fmt;
2096 fh->cap.field = f->fmt.pix.field;
2097 fh->cap.last = V4L2_FIELD_NONE;
2098 fh->width = f->fmt.pix.width;
2099 fh->height = f->fmt.pix.height;
2100 btv->init.fmt = fmt;
2101 btv->init.width = f->fmt.pix.width;
2102 btv->init.height = f->fmt.pix.height;
2103 up(&fh->cap.lock);
2104
2105 return 0;
2106 }
2107 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2108 return setup_window(fh, btv, &f->fmt.win, 1);
2109 case V4L2_BUF_TYPE_VBI_CAPTURE:
2110 retval = bttv_switch_type(fh,f->type);
2111 if (0 != retval)
2112 return retval;
2113 if (locked_btres(fh->btv, RESOURCE_VBI))
2114 return -EBUSY;
2115 bttv_vbi_try_fmt(fh,f);
2116 bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
2117 bttv_vbi_get_fmt(fh,f);
2118 return 0;
2119 default:
2120 return -EINVAL;
2121 }
2122}
2123
2124static int bttv_do_ioctl(struct inode *inode, struct file *file,
2125 unsigned int cmd, void *arg)
2126{
2127 struct bttv_fh *fh = file->private_data;
2128 struct bttv *btv = fh->btv;
2129 unsigned long flags;
2130 int retval = 0;
2131
2132 if (bttv_debug > 1) {
2133 switch (_IOC_TYPE(cmd)) {
2134 case 'v':
2135 printk("bttv%d: ioctl 0x%x (v4l1, VIDIOC%s)\n",
2136 btv->c.nr, cmd, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
2137 v4l1_ioctls[_IOC_NR(cmd)] : "???");
2138 break;
2139 case 'V':
2140 printk("bttv%d: ioctl 0x%x (v4l2, %s)\n",
2141 btv->c.nr, cmd, v4l2_ioctl_names[_IOC_NR(cmd)]);
2142 break;
2143 default:
2144 printk("bttv%d: ioctl 0x%x (???)\n",
2145 btv->c.nr, cmd);
2146 }
2147 }
2148 if (btv->errors)
2149 bttv_reinit_bt848(btv);
2150
2151 switch (cmd) {
2152 case VIDIOCSFREQ:
2153 case VIDIOCSTUNER:
2154 case VIDIOCSCHAN:
2155 case VIDIOC_S_CTRL:
2156 case VIDIOC_S_STD:
2157 case VIDIOC_S_INPUT:
2158 case VIDIOC_S_TUNER:
2159 case VIDIOC_S_FREQUENCY:
2160 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2161 if (0 != retval)
2162 return retval;
2163 };
2164
2165 switch (cmd) {
2166
2167 /* *** v4l1 *** ************************************************ */
2168 case VIDIOCGCAP:
2169 {
2170 struct video_capability *cap = arg;
2171
2172 memset(cap,0,sizeof(*cap));
2173 strcpy(cap->name,btv->video_dev->name);
2174 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2175 /* vbi */
2176 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2177 } else {
2178 /* others */
2179 cap->type = VID_TYPE_CAPTURE|
2180 VID_TYPE_TUNER|
2181 VID_TYPE_OVERLAY|
2182 VID_TYPE_CLIPPING|
2183 VID_TYPE_SCALES;
2184 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2185 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2186 cap->minwidth = 48;
2187 cap->minheight = 32;
2188 }
2189 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2190 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2191 return 0;
2192 }
2193
2194 case VIDIOCGPICT:
2195 {
2196 struct video_picture *pic = arg;
2197
2198 memset(pic,0,sizeof(*pic));
2199 pic->brightness = btv->bright;
2200 pic->contrast = btv->contrast;
2201 pic->hue = btv->hue;
2202 pic->colour = btv->saturation;
2203 if (fh->fmt) {
2204 pic->depth = fh->fmt->depth;
2205 pic->palette = fh->fmt->palette;
2206 }
2207 return 0;
2208 }
2209 case VIDIOCSPICT:
2210 {
2211 struct video_picture *pic = arg;
2212 const struct bttv_format *fmt;
2213
2214 fmt = format_by_palette(pic->palette);
2215 if (NULL == fmt)
2216 return -EINVAL;
2217 down(&fh->cap.lock);
2218 if (fmt->depth != pic->depth) {
2219 retval = -EINVAL;
2220 goto fh_unlock_and_return;
2221 }
2222 fh->ovfmt = fmt;
2223 fh->fmt = fmt;
2224 btv->init.ovfmt = fmt;
2225 btv->init.fmt = fmt;
2226 if (bigendian) {
2227 /* dirty hack time: swap bytes for overlay if the
2228 display adaptor is big endian (insmod option) */
2229 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2230 fmt->palette == VIDEO_PALETTE_RGB565 ||
2231 fmt->palette == VIDEO_PALETTE_RGB32) {
2232 fh->ovfmt = fmt+1;
2233 }
2234 }
2235 bt848_bright(btv,pic->brightness);
2236 bt848_contrast(btv,pic->contrast);
2237 bt848_hue(btv,pic->hue);
2238 bt848_sat(btv,pic->colour);
2239 up(&fh->cap.lock);
2240 return 0;
2241 }
2242
2243 case VIDIOCGWIN:
2244 {
2245 struct video_window *win = arg;
2246
2247 memset(win,0,sizeof(*win));
2248 win->x = fh->ov.w.left;
2249 win->y = fh->ov.w.top;
2250 win->width = fh->ov.w.width;
2251 win->height = fh->ov.w.height;
2252 return 0;
2253 }
2254 case VIDIOCSWIN:
2255 {
2256 struct video_window *win = arg;
2257 struct v4l2_window w2;
2258
2259 w2.field = V4L2_FIELD_ANY;
2260 w2.w.left = win->x;
2261 w2.w.top = win->y;
2262 w2.w.width = win->width;
2263 w2.w.height = win->height;
2264 w2.clipcount = win->clipcount;
2265 w2.clips = (struct v4l2_clip __user *)win->clips;
2266 retval = setup_window(fh, btv, &w2, 0);
2267 if (0 == retval) {
2268 /* on v4l1 this ioctl affects the read() size too */
2269 fh->width = fh->ov.w.width;
2270 fh->height = fh->ov.w.height;
2271 btv->init.width = fh->ov.w.width;
2272 btv->init.height = fh->ov.w.height;
2273 }
2274 return retval;
2275 }
2276
2277 case VIDIOCGFBUF:
2278 {
2279 struct video_buffer *fbuf = arg;
2280
2281 fbuf->base = btv->fbuf.base;
2282 fbuf->width = btv->fbuf.fmt.width;
2283 fbuf->height = btv->fbuf.fmt.height;
2284 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2285 if (fh->ovfmt)
2286 fbuf->depth = fh->ovfmt->depth;
2287 return 0;
2288 }
2289 case VIDIOCSFBUF:
2290 {
2291 struct video_buffer *fbuf = arg;
2292 const struct bttv_format *fmt;
2293 unsigned long end;
2294
2295 if(!capable(CAP_SYS_ADMIN) &&
2296 !capable(CAP_SYS_RAWIO))
2297 return -EPERM;
2298 end = (unsigned long)fbuf->base +
2299 fbuf->height * fbuf->bytesperline;
2300 down(&fh->cap.lock);
2301 retval = -EINVAL;
2302
2303 switch (fbuf->depth) {
2304 case 8:
2305 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2306 break;
2307 case 16:
2308 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2309 break;
2310 case 24:
2311 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2312 break;
2313 case 32:
2314 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2315 break;
2316 case 15:
2317 fbuf->depth = 16;
2318 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2319 break;
2320 default:
2321 fmt = NULL;
2322 break;
2323 }
2324 if (NULL == fmt)
2325 goto fh_unlock_and_return;
2326
2327 fh->ovfmt = fmt;
2328 fh->fmt = fmt;
2329 btv->init.ovfmt = fmt;
2330 btv->init.fmt = fmt;
2331 btv->fbuf.base = fbuf->base;
2332 btv->fbuf.fmt.width = fbuf->width;
2333 btv->fbuf.fmt.height = fbuf->height;
2334 if (fbuf->bytesperline)
2335 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2336 else
2337 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
2338 up(&fh->cap.lock);
2339 return 0;
2340 }
2341
2342 case VIDIOCCAPTURE:
2343 case VIDIOC_OVERLAY:
2344 {
2345 struct bttv_buffer *new;
2346 int *on = arg;
2347
2348 if (*on) {
2349 /* verify args */
2350 if (NULL == btv->fbuf.base)
2351 return -EINVAL;
2352 if (!fh->ov.setup_ok) {
2353 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2354 return -EINVAL;
2355 }
2356 }
2357
2358 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2359 return -EBUSY;
2360
2361 down(&fh->cap.lock);
2362 if (*on) {
2363 fh->ov.tvnorm = btv->tvnorm;
2364 new = videobuf_alloc(sizeof(*new));
2365 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2366 } else {
2367 new = NULL;
2368 }
2369
2370 /* switch over */
2371 retval = bttv_switch_overlay(btv,fh,new);
2372 up(&fh->cap.lock);
2373 return retval;
2374 }
2375
2376 case VIDIOCGMBUF:
2377 {
2378 struct video_mbuf *mbuf = arg;
2379 unsigned int i;
2380
2381 down(&fh->cap.lock);
2382 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
2383 V4L2_MEMORY_MMAP);
2384 if (retval < 0)
2385 goto fh_unlock_and_return;
2386 memset(mbuf,0,sizeof(*mbuf));
2387 mbuf->frames = gbuffers;
2388 mbuf->size = gbuffers * gbufsize;
2389 for (i = 0; i < gbuffers; i++)
2390 mbuf->offsets[i] = i * gbufsize;
2391 up(&fh->cap.lock);
2392 return 0;
2393 }
2394 case VIDIOCMCAPTURE:
2395 {
2396 struct video_mmap *vm = arg;
2397 struct bttv_buffer *buf;
2398 enum v4l2_field field;
2399
2400 if (vm->frame >= VIDEO_MAX_FRAME)
2401 return -EINVAL;
2402
2403 down(&fh->cap.lock);
2404 retval = -EINVAL;
2405 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
2406 if (NULL == buf)
2407 goto fh_unlock_and_return;
2408 if (0 == buf->vb.baddr)
2409 goto fh_unlock_and_return;
2410 if (buf->vb.state == STATE_QUEUED ||
2411 buf->vb.state == STATE_ACTIVE)
2412 goto fh_unlock_and_return;
2413
2414 field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
2415 ? V4L2_FIELD_INTERLACED
2416 : V4L2_FIELD_BOTTOM;
2417 retval = bttv_prepare_buffer(btv,buf,
2418 format_by_palette(vm->format),
2419 vm->width,vm->height,field);
2420 if (0 != retval)
2421 goto fh_unlock_and_return;
2422 spin_lock_irqsave(&btv->s_lock,flags);
2423 buffer_queue(&fh->cap,&buf->vb);
2424 spin_unlock_irqrestore(&btv->s_lock,flags);
2425 up(&fh->cap.lock);
2426 return 0;
2427 }
2428 case VIDIOCSYNC:
2429 {
2430 int *frame = arg;
2431 struct bttv_buffer *buf;
2432
2433 if (*frame >= VIDEO_MAX_FRAME)
2434 return -EINVAL;
2435
2436 down(&fh->cap.lock);
2437 retval = -EINVAL;
2438 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
2439 if (NULL == buf)
2440 goto fh_unlock_and_return;
2441 retval = videobuf_waiton(&buf->vb,0,1);
2442 if (0 != retval)
2443 goto fh_unlock_and_return;
2444 switch (buf->vb.state) {
2445 case STATE_ERROR:
2446 retval = -EIO;
2447 /* fall through */
2448 case STATE_DONE:
2449 videobuf_dma_pci_sync(btv->c.pci,&buf->vb.dma);
2450 bttv_dma_free(btv,buf);
2451 break;
2452 default:
2453 retval = -EINVAL;
2454 break;
2455 }
2456 up(&fh->cap.lock);
2457 return retval;
2458 }
2459
2460 case VIDIOCGVBIFMT:
2461 {
2462 struct vbi_format *fmt = (void *) arg;
2463 struct v4l2_format fmt2;
2464
2465 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
2466 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2467 if (0 != retval)
2468 return retval;
2469 }
2470 bttv_vbi_get_fmt(fh, &fmt2);
2471
2472 memset(fmt,0,sizeof(*fmt));
2473 fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
2474 fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
2475 fmt->sample_format = VIDEO_PALETTE_RAW;
2476 fmt->start[0] = fmt2.fmt.vbi.start[0];
2477 fmt->count[0] = fmt2.fmt.vbi.count[0];
2478 fmt->start[1] = fmt2.fmt.vbi.start[1];
2479 fmt->count[1] = fmt2.fmt.vbi.count[1];
2480 if (fmt2.fmt.vbi.flags & VBI_UNSYNC)
2481 fmt->flags |= V4L2_VBI_UNSYNC;
2482 if (fmt2.fmt.vbi.flags & VBI_INTERLACED)
2483 fmt->flags |= V4L2_VBI_INTERLACED;
2484 return 0;
2485 }
2486 case VIDIOCSVBIFMT:
2487 {
2488 struct vbi_format *fmt = (void *) arg;
2489 struct v4l2_format fmt2;
2490
2491 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2492 if (0 != retval)
2493 return retval;
2494 bttv_vbi_get_fmt(fh, &fmt2);
2495
2496 if (fmt->sampling_rate != fmt2.fmt.vbi.sampling_rate ||
2497 fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line ||
2498 fmt->sample_format != VIDEO_PALETTE_RAW ||
2499 fmt->start[0] != fmt2.fmt.vbi.start[0] ||
2500 fmt->start[1] != fmt2.fmt.vbi.start[1] ||
2501 fmt->count[0] != fmt->count[1] ||
2502 fmt->count[0] < 1 ||
2503 fmt->count[0] > 32 /* VBI_MAXLINES */)
2504 return -EINVAL;
2505
2506 bttv_vbi_setlines(fh,btv,fmt->count[0]);
2507 return 0;
2508 }
2509
2510 case BTTV_VERSION:
2511 case VIDIOCGFREQ:
2512 case VIDIOCSFREQ:
2513 case VIDIOCGTUNER:
2514 case VIDIOCSTUNER:
2515 case VIDIOCGCHAN:
2516 case VIDIOCSCHAN:
2517 case VIDIOCGAUDIO:
2518 case VIDIOCSAUDIO:
2519 return bttv_common_ioctls(btv,cmd,arg);
2520
2521 /* *** v4l2 *** ************************************************ */
2522 case VIDIOC_QUERYCAP:
2523 {
2524 struct v4l2_capability *cap = arg;
2525
2526 if (0 == v4l2)
2527 return -EINVAL;
2528 strcpy(cap->driver,"bttv");
2529 strlcpy(cap->card,btv->video_dev->name,sizeof(cap->card));
2530 sprintf(cap->bus_info,"PCI:%s",pci_name(btv->c.pci));
2531 cap->version = BTTV_VERSION_CODE;
2532 cap->capabilities =
2533 V4L2_CAP_VIDEO_CAPTURE |
2534 V4L2_CAP_VIDEO_OVERLAY |
2535 V4L2_CAP_VBI_CAPTURE |
2536 V4L2_CAP_READWRITE |
2537 V4L2_CAP_STREAMING;
2538 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
2539 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
2540 cap->capabilities |= V4L2_CAP_TUNER;
2541 return 0;
2542 }
2543
2544 case VIDIOC_ENUM_FMT:
2545 {
2546 struct v4l2_fmtdesc *f = arg;
2547 enum v4l2_buf_type type;
2548 unsigned int i;
2549 int index;
2550
2551 type = f->type;
2552 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2553 /* vbi */
2554 index = f->index;
2555 if (0 != index)
2556 return -EINVAL;
2557 memset(f,0,sizeof(*f));
2558 f->index = index;
2559 f->type = type;
2560 f->pixelformat = V4L2_PIX_FMT_GREY;
2561 strcpy(f->description,"vbi data");
2562 return 0;
2563 }
2564
2565 /* video capture + overlay */
2566 index = -1;
2567 for (i = 0; i < BTTV_FORMATS; i++) {
2568 if (bttv_formats[i].fourcc != -1)
2569 index++;
2570 if ((unsigned int)index == f->index)
2571 break;
2572 }
2573 if (BTTV_FORMATS == i)
2574 return -EINVAL;
2575
2576 switch (f->type) {
2577 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2578 break;
2579 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2580 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2581 return -EINVAL;
2582 break;
2583 default:
2584 return -EINVAL;
2585 }
2586 memset(f,0,sizeof(*f));
2587 f->index = index;
2588 f->type = type;
2589 f->pixelformat = bttv_formats[i].fourcc;
2590 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2591 return 0;
2592 }
2593
2594 case VIDIOC_TRY_FMT:
2595 {
2596 struct v4l2_format *f = arg;
2597 return bttv_try_fmt(fh,btv,f);
2598 }
2599 case VIDIOC_G_FMT:
2600 {
2601 struct v4l2_format *f = arg;
2602 return bttv_g_fmt(fh,f);
2603 }
2604 case VIDIOC_S_FMT:
2605 {
2606 struct v4l2_format *f = arg;
2607 return bttv_s_fmt(fh,btv,f);
2608 }
2609
2610 case VIDIOC_G_FBUF:
2611 {
2612 struct v4l2_framebuffer *fb = arg;
2613
2614 *fb = btv->fbuf;
2615 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2616 if (fh->ovfmt)
2617 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2618 return 0;
2619 }
2620 case VIDIOC_S_FBUF:
2621 {
2622 struct v4l2_framebuffer *fb = arg;
2623 const struct bttv_format *fmt;
2624
2625 if(!capable(CAP_SYS_ADMIN) &&
2626 !capable(CAP_SYS_RAWIO))
2627 return -EPERM;
2628
2629 /* check args */
2630 fmt = format_by_fourcc(fb->fmt.pixelformat);
2631 if (NULL == fmt)
2632 return -EINVAL;
2633 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2634 return -EINVAL;
2635
2636 down(&fh->cap.lock);
2637 retval = -EINVAL;
2638 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2639 if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
2640 goto fh_unlock_and_return;
2641 if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
2642 goto fh_unlock_and_return;
2643 }
2644
2645 /* ok, accept it */
2646 btv->fbuf.base = fb->base;
2647 btv->fbuf.fmt.width = fb->fmt.width;
2648 btv->fbuf.fmt.height = fb->fmt.height;
2649 if (0 != fb->fmt.bytesperline)
2650 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2651 else
2652 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2653
2654 retval = 0;
2655 fh->ovfmt = fmt;
2656 btv->init.ovfmt = fmt;
2657 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2658 fh->ov.w.left = 0;
2659 fh->ov.w.top = 0;
2660 fh->ov.w.width = fb->fmt.width;
2661 fh->ov.w.height = fb->fmt.height;
2662 btv->init.ov.w.width = fb->fmt.width;
2663 btv->init.ov.w.height = fb->fmt.height;
2664 if (fh->ov.clips)
2665 kfree(fh->ov.clips);
2666 fh->ov.clips = NULL;
2667 fh->ov.nclips = 0;
2668
2669 if (check_btres(fh, RESOURCE_OVERLAY)) {
2670 struct bttv_buffer *new;
2671
2672 new = videobuf_alloc(sizeof(*new));
2673 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2674 retval = bttv_switch_overlay(btv,fh,new);
2675 }
2676 }
2677 up(&fh->cap.lock);
2678 return retval;
2679 }
2680
2681 case VIDIOC_REQBUFS:
2682 return videobuf_reqbufs(bttv_queue(fh),arg);
2683
2684 case VIDIOC_QUERYBUF:
2685 return videobuf_querybuf(bttv_queue(fh),arg);
2686
2687 case VIDIOC_QBUF:
2688 return videobuf_qbuf(bttv_queue(fh),arg);
2689
2690 case VIDIOC_DQBUF:
2691 return videobuf_dqbuf(bttv_queue(fh),arg,
2692 file->f_flags & O_NONBLOCK);
2693
2694 case VIDIOC_STREAMON:
2695 {
2696 int res = bttv_resource(fh);
2697
2698 if (!check_alloc_btres(btv,fh,res))
2699 return -EBUSY;
2700 return videobuf_streamon(bttv_queue(fh));
2701 }
2702 case VIDIOC_STREAMOFF:
2703 {
2704 int res = bttv_resource(fh);
2705
2706 retval = videobuf_streamoff(bttv_queue(fh));
2707 if (retval < 0)
2708 return retval;
2709 free_btres(btv,fh,res);
2710 return 0;
2711 }
2712
2713 case VIDIOC_QUERYCTRL:
2714 {
2715 struct v4l2_queryctrl *c = arg;
2716 int i;
2717
2718 if ((c->id < V4L2_CID_BASE ||
2719 c->id >= V4L2_CID_LASTP1) &&
2720 (c->id < V4L2_CID_PRIVATE_BASE ||
2721 c->id >= V4L2_CID_PRIVATE_LASTP1))
2722 return -EINVAL;
2723 for (i = 0; i < BTTV_CTLS; i++)
2724 if (bttv_ctls[i].id == c->id)
2725 break;
2726 if (i == BTTV_CTLS) {
2727 *c = no_ctl;
2728 return 0;
2729 }
2730 *c = bttv_ctls[i];
2731 if (i >= 4 && i <= 8) {
2732 struct video_audio va;
2733 memset(&va,0,sizeof(va));
2734 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
2735 if (btv->audio_hook)
2736 btv->audio_hook(btv,&va,0);
2737 switch (bttv_ctls[i].id) {
2738 case V4L2_CID_AUDIO_VOLUME:
2739 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2740 *c = no_ctl;
2741 break;
2742 case V4L2_CID_AUDIO_BALANCE:
2743 if (!(va.flags & VIDEO_AUDIO_BALANCE))
2744 *c = no_ctl;
2745 break;
2746 case V4L2_CID_AUDIO_BASS:
2747 if (!(va.flags & VIDEO_AUDIO_BASS))
2748 *c = no_ctl;
2749 break;
2750 case V4L2_CID_AUDIO_TREBLE:
2751 if (!(va.flags & VIDEO_AUDIO_TREBLE))
2752 *c = no_ctl;
2753 break;
2754 }
2755 }
2756 return 0;
2757 }
2758 case VIDIOC_G_CTRL:
2759 return get_control(btv,arg);
2760 case VIDIOC_S_CTRL:
2761 return set_control(btv,arg);
2762 case VIDIOC_G_PARM:
2763 {
2764 struct v4l2_streamparm *parm = arg;
2765 struct v4l2_standard s;
2766 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2767 return -EINVAL;
2768 memset(parm,0,sizeof(*parm));
2769 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2770 bttv_tvnorms[btv->tvnorm].name);
2771 parm->parm.capture.timeperframe = s.frameperiod;
2772 return 0;
2773 }
2774
2775 case VIDIOC_G_PRIORITY:
2776 {
2777 enum v4l2_priority *p = arg;
2778
2779 *p = v4l2_prio_max(&btv->prio);
2780 return 0;
2781 }
2782 case VIDIOC_S_PRIORITY:
2783 {
2784 enum v4l2_priority *prio = arg;
2785
2786 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
2787 }
2788
2789 case VIDIOC_ENUMSTD:
2790 case VIDIOC_G_STD:
2791 case VIDIOC_S_STD:
2792 case VIDIOC_ENUMINPUT:
2793 case VIDIOC_G_INPUT:
2794 case VIDIOC_S_INPUT:
2795 case VIDIOC_G_TUNER:
2796 case VIDIOC_S_TUNER:
2797 case VIDIOC_G_FREQUENCY:
2798 case VIDIOC_S_FREQUENCY:
2799 return bttv_common_ioctls(btv,cmd,arg);
2800
2801 default:
2802 return -ENOIOCTLCMD;
2803 }
2804 return 0;
2805
2806 fh_unlock_and_return:
2807 up(&fh->cap.lock);
2808 return retval;
2809}
2810
2811static int bttv_ioctl(struct inode *inode, struct file *file,
2812 unsigned int cmd, unsigned long arg)
2813{
2814 struct bttv_fh *fh = file->private_data;
2815
2816 switch (cmd) {
2817 case BTTV_VBISIZE:
2818 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2819 return fh->lines * 2 * 2048;
2820 default:
2821 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
2822 }
2823}
2824
2825static ssize_t bttv_read(struct file *file, char __user *data,
2826 size_t count, loff_t *ppos)
2827{
2828 struct bttv_fh *fh = file->private_data;
2829 int retval = 0;
2830
2831 if (fh->btv->errors)
2832 bttv_reinit_bt848(fh->btv);
2833 dprintk("bttv%d: read count=%d type=%s\n",
2834 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
2835
2836 switch (fh->type) {
2837 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2838 if (locked_btres(fh->btv,RESOURCE_VIDEO))
2839 return -EBUSY;
2840 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2841 file->f_flags & O_NONBLOCK);
2842 break;
2843 case V4L2_BUF_TYPE_VBI_CAPTURE:
2844 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2845 return -EBUSY;
2846 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2847 file->f_flags & O_NONBLOCK);
2848 break;
2849 default:
2850 BUG();
2851 }
2852 return retval;
2853}
2854
2855static unsigned int bttv_poll(struct file *file, poll_table *wait)
2856{
2857 struct bttv_fh *fh = file->private_data;
2858 struct bttv_buffer *buf;
2859 enum v4l2_field field;
2860
2861 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2862 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2863 return POLLERR;
2864 return videobuf_poll_stream(file, &fh->vbi, wait);
2865 }
2866
2867 if (check_btres(fh,RESOURCE_VIDEO)) {
2868 /* streaming capture */
2869 if (list_empty(&fh->cap.stream))
2870 return POLLERR;
2871 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2872 } else {
2873 /* read() capture */
2874 down(&fh->cap.lock);
2875 if (NULL == fh->cap.read_buf) {
2876 /* need to capture a new frame */
2877 if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
2878 up(&fh->cap.lock);
2879 return POLLERR;
2880 }
2881 fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
2882 if (NULL == fh->cap.read_buf) {
2883 up(&fh->cap.lock);
2884 return POLLERR;
2885 }
2886 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2887 field = videobuf_next_field(&fh->cap);
2888 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
2889 up(&fh->cap.lock);
2890 return POLLERR;
2891 }
2892 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
2893 fh->cap.read_off = 0;
2894 }
2895 up(&fh->cap.lock);
2896 buf = (struct bttv_buffer*)fh->cap.read_buf;
2897 }
2898
2899 poll_wait(file, &buf->vb.done, wait);
2900 if (buf->vb.state == STATE_DONE ||
2901 buf->vb.state == STATE_ERROR)
2902 return POLLIN|POLLRDNORM;
2903 return 0;
2904}
2905
2906static int bttv_open(struct inode *inode, struct file *file)
2907{
2908 int minor = iminor(inode);
2909 struct bttv *btv = NULL;
2910 struct bttv_fh *fh;
2911 enum v4l2_buf_type type = 0;
2912 unsigned int i;
2913
2914 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
2915
2916 for (i = 0; i < bttv_num; i++) {
2917 if (bttvs[i].video_dev &&
2918 bttvs[i].video_dev->minor == minor) {
2919 btv = &bttvs[i];
2920 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2921 break;
2922 }
2923 if (bttvs[i].vbi_dev &&
2924 bttvs[i].vbi_dev->minor == minor) {
2925 btv = &bttvs[i];
2926 type = V4L2_BUF_TYPE_VBI_CAPTURE;
2927 break;
2928 }
2929 }
2930 if (NULL == btv)
2931 return -ENODEV;
2932
2933 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
2934 btv->c.nr,v4l2_type_names[type]);
2935
2936 /* allocate per filehandle data */
2937 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
2938 if (NULL == fh)
2939 return -ENOMEM;
2940 file->private_data = fh;
2941 *fh = btv->init;
2942 fh->type = type;
2943 fh->ov.setup_ok = 0;
2944 v4l2_prio_open(&btv->prio,&fh->prio);
2945
2946 videobuf_queue_init(&fh->cap, &bttv_video_qops,
2947 btv->c.pci, &btv->s_lock,
2948 V4L2_BUF_TYPE_VIDEO_CAPTURE,
2949 V4L2_FIELD_INTERLACED,
2950 sizeof(struct bttv_buffer),
2951 fh);
2952 videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
2953 btv->c.pci, &btv->s_lock,
2954 V4L2_BUF_TYPE_VBI_CAPTURE,
2955 V4L2_FIELD_SEQ_TB,
2956 sizeof(struct bttv_buffer),
2957 fh);
2958 i2c_vidiocschan(btv);
2959
2960 btv->users++;
2961 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
2962 bttv_vbi_setlines(fh,btv,16);
2963 bttv_field_count(btv);
2964 return 0;
2965}
2966
2967static int bttv_release(struct inode *inode, struct file *file)
2968{
2969 struct bttv_fh *fh = file->private_data;
2970 struct bttv *btv = fh->btv;
2971
2972 /* turn off overlay */
2973 if (check_btres(fh, RESOURCE_OVERLAY))
2974 bttv_switch_overlay(btv,fh,NULL);
2975
2976 /* stop video capture */
2977 if (check_btres(fh, RESOURCE_VIDEO)) {
2978 videobuf_streamoff(&fh->cap);
2979 free_btres(btv,fh,RESOURCE_VIDEO);
2980 }
2981 if (fh->cap.read_buf) {
2982 buffer_release(&fh->cap,fh->cap.read_buf);
2983 kfree(fh->cap.read_buf);
2984 }
2985
2986 /* stop vbi capture */
2987 if (check_btres(fh, RESOURCE_VBI)) {
2988 if (fh->vbi.streaming)
2989 videobuf_streamoff(&fh->vbi);
2990 if (fh->vbi.reading)
2991 videobuf_read_stop(&fh->vbi);
2992 free_btres(btv,fh,RESOURCE_VBI);
2993 }
2994
2995 /* free stuff */
2996 videobuf_mmap_free(&fh->cap);
2997 videobuf_mmap_free(&fh->vbi);
2998 v4l2_prio_close(&btv->prio,&fh->prio);
2999 file->private_data = NULL;
3000 kfree(fh);
3001
3002 btv->users--;
3003 bttv_field_count(btv);
3004 return 0;
3005}
3006
3007static int
3008bttv_mmap(struct file *file, struct vm_area_struct *vma)
3009{
3010 struct bttv_fh *fh = file->private_data;
3011
3012 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3013 fh->btv->c.nr, v4l2_type_names[fh->type],
3014 vma->vm_start, vma->vm_end - vma->vm_start);
3015 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3016}
3017
3018static struct file_operations bttv_fops =
3019{
3020 .owner = THIS_MODULE,
3021 .open = bttv_open,
3022 .release = bttv_release,
3023 .ioctl = bttv_ioctl,
3024 .llseek = no_llseek,
3025 .read = bttv_read,
3026 .mmap = bttv_mmap,
3027 .poll = bttv_poll,
3028};
3029
3030static struct video_device bttv_video_template =
3031{
3032 .name = "UNSET",
3033 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_OVERLAY|
3034 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3035 .hardware = VID_HARDWARE_BT848,
3036 .fops = &bttv_fops,
3037 .minor = -1,
3038};
3039
3040static struct video_device bttv_vbi_template =
3041{
3042 .name = "bt848/878 vbi",
3043 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3044 .hardware = VID_HARDWARE_BT848,
3045 .fops = &bttv_fops,
3046 .minor = -1,
3047};
3048
3049/* ----------------------------------------------------------------------- */
3050/* radio interface */
3051
3052static int radio_open(struct inode *inode, struct file *file)
3053{
3054 int minor = iminor(inode);
3055 struct bttv *btv = NULL;
3056 unsigned int i;
3057
3058 dprintk("bttv: open minor=%d\n",minor);
3059
3060 for (i = 0; i < bttv_num; i++) {
3061 if (bttvs[i].radio_dev->minor == minor) {
3062 btv = &bttvs[i];
3063 break;
3064 }
3065 }
3066 if (NULL == btv)
3067 return -ENODEV;
3068
3069 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3070 down(&btv->lock);
3071 if (btv->radio_user) {
3072 up(&btv->lock);
3073 return -EBUSY;
3074 }
3075 btv->radio_user++;
3076 file->private_data = btv;
3077
3078 i2c_vidiocschan(btv);
3079 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
3080 audio_mux(btv,AUDIO_RADIO);
3081
3082 up(&btv->lock);
3083 return 0;
3084}
3085
3086static int radio_release(struct inode *inode, struct file *file)
3087{
3088 struct bttv *btv = file->private_data;
3089
3090 btv->radio_user--;
3091 return 0;
3092}
3093
3094static int radio_do_ioctl(struct inode *inode, struct file *file,
3095 unsigned int cmd, void *arg)
3096{
3097 struct bttv *btv = file->private_data;
3098
3099 switch (cmd) {
3100 case VIDIOCGCAP:
3101 {
3102 struct video_capability *cap = arg;
3103
3104 memset(cap,0,sizeof(*cap));
3105 strcpy(cap->name,btv->radio_dev->name);
3106 cap->type = VID_TYPE_TUNER;
3107 cap->channels = 1;
3108 cap->audios = 1;
3109 return 0;
3110 }
3111
3112 case VIDIOCGTUNER:
3113 {
3114 struct video_tuner *v = arg;
3115
3116 if(v->tuner)
3117 return -EINVAL;
3118 memset(v,0,sizeof(*v));
3119 strcpy(v->name, "Radio");
3120 /* japan: 76.0 MHz - 89.9 MHz
3121 western europe: 87.5 MHz - 108.0 MHz
3122 russia: 65.0 MHz - 108.0 MHz */
3123 v->rangelow=(int)(65*16);
3124 v->rangehigh=(int)(108*16);
3125 bttv_call_i2c_clients(btv,cmd,v);
3126 return 0;
3127 }
3128 case VIDIOCSTUNER:
3129 /* nothing to do */
3130 return 0;
3131
3132 case BTTV_VERSION:
3133 case VIDIOCGFREQ:
3134 case VIDIOCSFREQ:
3135 case VIDIOCGAUDIO:
3136 case VIDIOCSAUDIO:
3137 return bttv_common_ioctls(btv,cmd,arg);
3138
3139 default:
3140 return -ENOIOCTLCMD;
3141 }
3142 return 0;
3143}
3144
3145static int radio_ioctl(struct inode *inode, struct file *file,
3146 unsigned int cmd, unsigned long arg)
3147{
3148 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3149}
3150
3151static struct file_operations radio_fops =
3152{
3153 .owner = THIS_MODULE,
3154 .open = radio_open,
3155 .release = radio_release,
3156 .ioctl = radio_ioctl,
3157 .llseek = no_llseek,
3158};
3159
3160static struct video_device radio_template =
3161{
3162 .name = "bt848/878 radio",
3163 .type = VID_TYPE_TUNER,
3164 .hardware = VID_HARDWARE_BT848,
3165 .fops = &radio_fops,
3166 .minor = -1,
3167};
3168
3169/* ----------------------------------------------------------------------- */
3170/* some debug code */
3171
3172int bttv_risc_decode(u32 risc)
3173{
3174 static char *instr[16] = {
3175 [ BT848_RISC_WRITE >> 28 ] = "write",
3176 [ BT848_RISC_SKIP >> 28 ] = "skip",
3177 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3178 [ BT848_RISC_JUMP >> 28 ] = "jump",
3179 [ BT848_RISC_SYNC >> 28 ] = "sync",
3180 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3181 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3182 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3183 };
3184 static int incr[16] = {
3185 [ BT848_RISC_WRITE >> 28 ] = 2,
3186 [ BT848_RISC_JUMP >> 28 ] = 2,
3187 [ BT848_RISC_SYNC >> 28 ] = 2,
3188 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3189 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3190 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3191 };
3192 static char *bits[] = {
3193 "be0", "be1", "be2", "be3/resync",
3194 "set0", "set1", "set2", "set3",
3195 "clr0", "clr1", "clr2", "clr3",
3196 "irq", "res", "eol", "sol",
3197 };
3198 int i;
3199
3200 printk("0x%08x [ %s", risc,
3201 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3202 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3203 if (risc & (1 << (i + 12)))
3204 printk(" %s",bits[i]);
3205 printk(" count=%d ]\n", risc & 0xfff);
3206 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3207}
3208
3209void bttv_risc_disasm(struct bttv *btv,
3210 struct btcx_riscmem *risc)
3211{
3212 unsigned int i,j,n;
3213
3214 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3215 btv->c.name, risc->cpu, (unsigned long)risc->dma);
3216 for (i = 0; i < (risc->size >> 2); i += n) {
3217 printk("%s: 0x%lx: ", btv->c.name,
3218 (unsigned long)(risc->dma + (i<<2)));
3219 n = bttv_risc_decode(risc->cpu[i]);
3220 for (j = 1; j < n; j++)
3221 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3222 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3223 risc->cpu[i+j], j);
3224 if (0 == risc->cpu[i])
3225 break;
3226 }
3227}
3228
3229static void bttv_print_riscaddr(struct bttv *btv)
3230{
3231 printk(" main: %08Lx\n",
3232 (unsigned long long)btv->main.dma);
3233 printk(" vbi : o=%08Lx e=%08Lx\n",
3234 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3235 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3236 printk(" cap : o=%08Lx e=%08Lx\n",
3237 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3238 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3239 printk(" scr : o=%08Lx e=%08Lx\n",
3240 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3241 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3242 bttv_risc_disasm(btv, &btv->main);
3243}
3244
3245/* ----------------------------------------------------------------------- */
3246/* irq handler */
3247
3248static char *irq_name[] = {
3249 "FMTCHG", // format change detected (525 vs. 625)
3250 "VSYNC", // vertical sync (new field)
3251 "HSYNC", // horizontal sync
3252 "OFLOW", // chroma/luma AGC overflow
3253 "HLOCK", // horizontal lock changed
3254 "VPRES", // video presence changed
3255 "6", "7",
3256 "I2CDONE", // hw irc operation finished
3257 "GPINT", // gpio port triggered irq
3258 "10",
3259 "RISCI", // risc instruction triggered irq
3260 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3261 "FTRGT", // pixel data fifo overrun
3262 "FDSR", // fifo data stream resyncronisation
3263 "PPERR", // parity error (data transfer)
3264 "RIPERR", // parity error (read risc instructions)
3265 "PABORT", // pci abort
3266 "OCERR", // risc instruction error
3267 "SCERR", // syncronisation error
3268};
3269
3270static void bttv_print_irqbits(u32 print, u32 mark)
3271{
3272 unsigned int i;
3273
3274 printk("bits:");
3275 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3276 if (print & (1 << i))
3277 printk(" %s",irq_name[i]);
3278 if (mark & (1 << i))
3279 printk("*");
3280 }
3281}
3282
3283static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3284{
3285 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3286 btv->c.nr,
3287 (unsigned long)btv->main.dma,
3288 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
3289 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
3290 (unsigned long)rc);
3291
3292 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3293 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3294 "Ok, then this is harmless, don't worry ;)\n",
3295 btv->c.nr);
3296 return;
3297 }
3298 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3299 btv->c.nr);
3300 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3301 btv->c.nr);
3302 dump_stack();
3303}
3304
3305static int
3306bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3307{
3308 struct bttv_buffer *item;
3309
3310 memset(set,0,sizeof(*set));
3311
3312 /* capture request ? */
3313 if (!list_empty(&btv->capture)) {
3314 set->frame_irq = 1;
3315 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3316 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3317 set->top = item;
3318 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3319 set->bottom = item;
3320
3321 /* capture request for other field ? */
3322 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3323 (item->vb.queue.next != &btv->capture)) {
3324 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3325 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3326 if (NULL == set->top &&
3327 V4L2_FIELD_TOP == item->vb.field) {
3328 set->top = item;
3329 }
3330 if (NULL == set->bottom &&
3331 V4L2_FIELD_BOTTOM == item->vb.field) {
3332 set->bottom = item;
3333 }
3334 if (NULL != set->top && NULL != set->bottom)
3335 set->top_irq = 2;
3336 }
3337 }
3338 }
3339
3340 /* screen overlay ? */
3341 if (NULL != btv->screen) {
3342 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3343 if (NULL == set->top && NULL == set->bottom) {
3344 set->top = btv->screen;
3345 set->bottom = btv->screen;
3346 }
3347 } else {
3348 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3349 NULL == set->top) {
3350 set->top = btv->screen;
3351 }
3352 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3353 NULL == set->bottom) {
3354 set->bottom = btv->screen;
3355 }
3356 }
3357 }
3358
3359 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3360 btv->c.nr,set->top, set->bottom,
3361 btv->screen,set->frame_irq,set->top_irq);
3362 return 0;
3363}
3364
3365static void
3366bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3367 struct bttv_buffer_set *curr, unsigned int state)
3368{
3369 struct timeval ts;
3370
3371 do_gettimeofday(&ts);
3372
3373 if (wakeup->top == wakeup->bottom) {
3374 if (NULL != wakeup->top && curr->top != wakeup->top) {
3375 if (irq_debug > 1)
3376 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3377 wakeup->top->vb.ts = ts;
3378 wakeup->top->vb.field_count = btv->field_count;
3379 wakeup->top->vb.state = state;
3380 wake_up(&wakeup->top->vb.done);
3381 }
3382 } else {
3383 if (NULL != wakeup->top && curr->top != wakeup->top) {
3384 if (irq_debug > 1)
3385 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3386 wakeup->top->vb.ts = ts;
3387 wakeup->top->vb.field_count = btv->field_count;
3388 wakeup->top->vb.state = state;
3389 wake_up(&wakeup->top->vb.done);
3390 }
3391 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3392 if (irq_debug > 1)
3393 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3394 wakeup->bottom->vb.ts = ts;
3395 wakeup->bottom->vb.field_count = btv->field_count;
3396 wakeup->bottom->vb.state = state;
3397 wake_up(&wakeup->bottom->vb.done);
3398 }
3399 }
3400}
3401
3402static void
3403bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3404 unsigned int state)
3405{
3406 struct timeval ts;
3407
3408 if (NULL == wakeup)
3409 return;
3410
3411 do_gettimeofday(&ts);
3412 wakeup->vb.ts = ts;
3413 wakeup->vb.field_count = btv->field_count;
3414 wakeup->vb.state = state;
3415 wake_up(&wakeup->vb.done);
3416}
3417
3418static void bttv_irq_timeout(unsigned long data)
3419{
3420 struct bttv *btv = (struct bttv *)data;
3421 struct bttv_buffer_set old,new;
3422 struct bttv_buffer *ovbi;
3423 struct bttv_buffer *item;
3424 unsigned long flags;
3425
3426 if (bttv_verbose) {
3427 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3428 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3429 btread(BT848_RISC_COUNT));
3430 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3431 printk("\n");
3432 }
3433
3434 spin_lock_irqsave(&btv->s_lock,flags);
3435
3436 /* deactivate stuff */
3437 memset(&new,0,sizeof(new));
3438 old = btv->curr;
3439 ovbi = btv->cvbi;
3440 btv->curr = new;
3441 btv->cvbi = NULL;
3442 btv->loop_irq = 0;
3443 bttv_buffer_activate_video(btv, &new);
3444 bttv_buffer_activate_vbi(btv, NULL);
3445 bttv_set_dma(btv, 0);
3446
3447 /* wake up */
3448 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
3449 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
3450
3451 /* cancel all outstanding capture / vbi requests */
3452 while (!list_empty(&btv->capture)) {
3453 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3454 list_del(&item->vb.queue);
3455 item->vb.state = STATE_ERROR;
3456 wake_up(&item->vb.done);
3457 }
3458 while (!list_empty(&btv->vcapture)) {
3459 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3460 list_del(&item->vb.queue);
3461 item->vb.state = STATE_ERROR;
3462 wake_up(&item->vb.done);
3463 }
3464
3465 btv->errors++;
3466 spin_unlock_irqrestore(&btv->s_lock,flags);
3467}
3468
3469static void
3470bttv_irq_wakeup_top(struct bttv *btv)
3471{
3472 struct bttv_buffer *wakeup = btv->curr.top;
3473
3474 if (NULL == wakeup)
3475 return;
3476
3477 spin_lock(&btv->s_lock);
3478 btv->curr.top_irq = 0;
3479 btv->curr.top = NULL;
3480 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3481
3482 do_gettimeofday(&wakeup->vb.ts);
3483 wakeup->vb.field_count = btv->field_count;
3484 wakeup->vb.state = STATE_DONE;
3485 wake_up(&wakeup->vb.done);
3486 spin_unlock(&btv->s_lock);
3487}
3488
3489static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3490{
3491 if (rc < risc->dma)
3492 return 0;
3493 if (rc > risc->dma + risc->size)
3494 return 0;
3495 return 1;
3496}
3497
3498static void
3499bttv_irq_switch_video(struct bttv *btv)
3500{
3501 struct bttv_buffer_set new;
3502 struct bttv_buffer_set old;
3503 dma_addr_t rc;
3504
3505 spin_lock(&btv->s_lock);
3506
3507 /* new buffer set */
3508 bttv_irq_next_video(btv, &new);
3509 rc = btread(BT848_RISC_COUNT);
3510 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3511 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3512 btv->framedrop++;
3513 if (debug_latency)
3514 bttv_irq_debug_low_latency(btv, rc);
3515 spin_unlock(&btv->s_lock);
3516 return;
3517 }
3518
3519 /* switch over */
3520 old = btv->curr;
3521 btv->curr = new;
3522 btv->loop_irq &= ~1;
3523 bttv_buffer_activate_video(btv, &new);
3524 bttv_set_dma(btv, 0);
3525
3526 /* switch input */
3527 if (UNSET != btv->new_input) {
3528 video_mux(btv,btv->new_input);
3529 btv->new_input = UNSET;
3530 }
3531
3532 /* wake up finished buffers */
3533 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
3534 spin_unlock(&btv->s_lock);
3535}
3536
3537static void
3538bttv_irq_switch_vbi(struct bttv *btv)
3539{
3540 struct bttv_buffer *new = NULL;
3541 struct bttv_buffer *old;
3542 u32 rc;
3543
3544 spin_lock(&btv->s_lock);
3545
3546 if (!list_empty(&btv->vcapture))
3547 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3548 old = btv->cvbi;
3549
3550 rc = btread(BT848_RISC_COUNT);
3551 if (NULL != old && (is_active(&old->top, rc) ||
3552 is_active(&old->bottom, rc))) {
3553 btv->framedrop++;
3554 if (debug_latency)
3555 bttv_irq_debug_low_latency(btv, rc);
3556 spin_unlock(&btv->s_lock);
3557 return;
3558 }
3559
3560 /* switch */
3561 btv->cvbi = new;
3562 btv->loop_irq &= ~4;
3563 bttv_buffer_activate_vbi(btv, new);
3564 bttv_set_dma(btv, 0);
3565
3566 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
3567 spin_unlock(&btv->s_lock);
3568}
3569
3570static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3571{
3572 u32 stat,astat;
3573 u32 dstat;
3574 int count;
3575 struct bttv *btv;
3576 int handled = 0;
3577
3578 btv=(struct bttv *)dev_id;
3579 count=0;
3580 while (1) {
3581 /* get/clear interrupt status bits */
3582 stat=btread(BT848_INT_STAT);
3583 astat=stat&btread(BT848_INT_MASK);
3584 if (!astat)
3585 break;
3586 handled = 1;
3587 btwrite(stat,BT848_INT_STAT);
3588
3589 /* get device status bits */
3590 dstat=btread(BT848_DSTATUS);
3591
3592 if (irq_debug) {
3593 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
3594 "riscs=%x, riscc=%08x, ",
3595 btv->c.nr, count, btv->field_count,
3596 stat>>28, btread(BT848_RISC_COUNT));
3597 bttv_print_irqbits(stat,astat);
3598 if (stat & BT848_INT_HLOCK)
3599 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
3600 ? "yes" : "no");
3601 if (stat & BT848_INT_VPRES)
3602 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
3603 ? "yes" : "no");
3604 if (stat & BT848_INT_FMTCHG)
3605 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
3606 ? "625" : "525");
3607 printk("\n");
3608 }
3609
3610 if (astat&BT848_INT_VSYNC)
3611 btv->field_count++;
3612
3613 if (astat & BT848_INT_GPINT) {
3614 wake_up(&btv->gpioq);
3615 bttv_gpio_irq(&btv->c);
3616 }
3617
3618 if (astat & BT848_INT_I2CDONE) {
3619 btv->i2c_done = stat;
3620 wake_up(&btv->i2c_queue);
3621 }
3622
3623 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
3624 bttv_irq_switch_vbi(btv);
3625
3626 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
3627 bttv_irq_wakeup_top(btv);
3628
3629 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
3630 bttv_irq_switch_video(btv);
3631
3632 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3633 audio_mux(btv, -1);
3634
3635 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3636 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
3637 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3638 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3639 btread(BT848_RISC_COUNT));
3640 bttv_print_irqbits(stat,astat);
3641 printk("\n");
3642 if (bttv_debug)
3643 bttv_print_riscaddr(btv);
3644 }
3645 if (fdsr && astat & BT848_INT_FDSR) {
3646 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
3647 btv->c.nr,btread(BT848_RISC_COUNT));
3648 if (bttv_debug)
3649 bttv_print_riscaddr(btv);
3650 }
3651
3652 count++;
3653 if (count > 4) {
3654 btwrite(0, BT848_INT_MASK);
3655 printk(KERN_ERR
3656 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
3657 bttv_print_irqbits(stat,astat);
3658 printk("]\n");
3659 }
3660 }
3661 btv->irq_total++;
3662 if (handled)
3663 btv->irq_me++;
3664 return IRQ_RETVAL(handled);
3665}
3666
3667
3668/* ----------------------------------------------------------------------- */
3669/* initialitation */
3670
3671static struct video_device *vdev_init(struct bttv *btv,
3672 struct video_device *template,
3673 char *type)
3674{
3675 struct video_device *vfd;
3676
3677 vfd = video_device_alloc();
3678 if (NULL == vfd)
3679 return NULL;
3680 *vfd = *template;
3681 vfd->minor = -1;
3682 vfd->dev = &btv->c.pci->dev;
3683 vfd->release = video_device_release;
3684 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3685 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3686 type, bttv_tvcards[btv->c.type].name);
3687 return vfd;
3688}
3689
3690static void bttv_unregister_video(struct bttv *btv)
3691{
3692 if (btv->video_dev) {
3693 if (-1 != btv->video_dev->minor)
3694 video_unregister_device(btv->video_dev);
3695 else
3696 video_device_release(btv->video_dev);
3697 btv->video_dev = NULL;
3698 }
3699 if (btv->vbi_dev) {
3700 if (-1 != btv->vbi_dev->minor)
3701 video_unregister_device(btv->vbi_dev);
3702 else
3703 video_device_release(btv->vbi_dev);
3704 btv->vbi_dev = NULL;
3705 }
3706 if (btv->radio_dev) {
3707 if (-1 != btv->radio_dev->minor)
3708 video_unregister_device(btv->radio_dev);
3709 else
3710 video_device_release(btv->radio_dev);
3711 btv->radio_dev = NULL;
3712 }
3713}
3714
3715/* register video4linux devices */
3716static int __devinit bttv_register_video(struct bttv *btv)
3717{
3718 /* video */
3719 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
3720 if (NULL == btv->video_dev)
3721 goto err;
3722 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
3723 goto err;
3724 printk(KERN_INFO "bttv%d: registered device video%d\n",
3725 btv->c.nr,btv->video_dev->minor & 0x1f);
3726 video_device_create_file(btv->video_dev, &class_device_attr_card);
3727
3728 /* vbi */
3729 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
3730 if (NULL == btv->vbi_dev)
3731 goto err;
3732 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
3733 goto err;
3734 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
3735 btv->c.nr,btv->vbi_dev->minor & 0x1f);
3736
3737 if (!btv->has_radio)
3738 return 0;
3739 /* radio */
3740 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
3741 if (NULL == btv->radio_dev)
3742 goto err;
3743 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
3744 goto err;
3745 printk(KERN_INFO "bttv%d: registered device radio%d\n",
3746 btv->c.nr,btv->radio_dev->minor & 0x1f);
3747
3748 /* all done */
3749 return 0;
3750
3751 err:
3752 bttv_unregister_video(btv);
3753 return -1;
3754}
3755
3756
3757/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3758/* response on cards with no firmware is not enabled by OF */
3759static void pci_set_command(struct pci_dev *dev)
3760{
3761#if defined(__powerpc__)
3762 unsigned int cmd;
3763
3764 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3765 cmd = (cmd | PCI_COMMAND_MEMORY );
3766 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3767#endif
3768}
3769
3770static int __devinit bttv_probe(struct pci_dev *dev,
3771 const struct pci_device_id *pci_id)
3772{
3773 int result;
3774 unsigned char lat;
3775 struct bttv *btv;
3776
3777 if (bttv_num == BTTV_MAX)
3778 return -ENOMEM;
3779 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
3780 btv=&bttvs[bttv_num];
3781 memset(btv,0,sizeof(*btv));
3782 btv->c.nr = bttv_num;
3783 sprintf(btv->c.name,"bttv%d",btv->c.nr);
3784
3785 /* initialize structs / fill in defaults */
3786 init_MUTEX(&btv->lock);
3787 init_MUTEX(&btv->reslock);
3788 spin_lock_init(&btv->s_lock);
3789 spin_lock_init(&btv->gpio_lock);
3790 init_waitqueue_head(&btv->gpioq);
3791 init_waitqueue_head(&btv->i2c_queue);
3792 INIT_LIST_HEAD(&btv->c.subs);
3793 INIT_LIST_HEAD(&btv->capture);
3794 INIT_LIST_HEAD(&btv->vcapture);
3795 v4l2_prio_init(&btv->prio);
3796
3797 init_timer(&btv->timeout);
3798 btv->timeout.function = bttv_irq_timeout;
3799 btv->timeout.data = (unsigned long)btv;
3800
3801 btv->i2c_rc = -1;
3802 btv->tuner_type = UNSET;
3803 btv->pinnacle_id = UNSET;
3804 btv->new_input = UNSET;
3805 btv->gpioirq = 1;
3806 btv->has_radio=radio[btv->c.nr];
3807
3808 /* pci stuff (init, get irq/mmio, ... */
3809 btv->c.pci = dev;
3810 btv->id = dev->device;
3811 if (pci_enable_device(dev)) {
3812 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
3813 btv->c.nr);
3814 return -EIO;
3815 }
3816 if (pci_set_dma_mask(dev, 0xffffffff)) {
3817 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
3818 btv->c.nr);
3819 return -EIO;
3820 }
3821 if (!request_mem_region(pci_resource_start(dev,0),
3822 pci_resource_len(dev,0),
3823 btv->c.name)) {
3824 printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
3825 btv->c.nr, pci_resource_start(dev,0));
3826 return -EBUSY;
3827 }
3828 pci_set_master(dev);
3829 pci_set_command(dev);
3830 pci_set_drvdata(dev,btv);
3831 if (!pci_dma_supported(dev,0xffffffff)) {
3832 printk("bttv%d: Oops: no 32bit PCI DMA ???\n", btv->c.nr);
3833 result = -EIO;
3834 goto fail1;
3835 }
3836
3837 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3838 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3839 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
3840 bttv_num,btv->id, btv->revision, pci_name(dev));
3841 printk("irq: %d, latency: %d, mmio: 0x%lx\n",
3842 btv->c.pci->irq, lat, pci_resource_start(dev,0));
3843 schedule();
3844
3845 btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
3846 if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
3847 printk("bttv%d: ioremap() failed\n", btv->c.nr);
3848 result = -EIO;
3849 goto fail1;
3850 }
3851
3852 /* identify card */
3853 bttv_idcard(btv);
3854
3855 /* disable irqs, register irq handler */
3856 btwrite(0, BT848_INT_MASK);
3857 result = request_irq(btv->c.pci->irq, bttv_irq,
3858 SA_SHIRQ | SA_INTERRUPT,btv->c.name,(void *)btv);
3859 if (result < 0) {
3860 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
3861 bttv_num,btv->c.pci->irq);
3862 goto fail1;
3863 }
3864
3865 if (0 != bttv_handle_chipset(btv)) {
3866 result = -EIO;
3867 goto fail2;
3868 }
3869
3870 /* init options from insmod args */
3871 btv->opt_combfilter = combfilter;
3872 btv->opt_lumafilter = lumafilter;
3873 btv->opt_automute = automute;
3874 btv->opt_chroma_agc = chroma_agc;
3875 btv->opt_adc_crush = adc_crush;
3876 btv->opt_vcr_hack = vcr_hack;
3877 btv->opt_whitecrush_upper = whitecrush_upper;
3878 btv->opt_whitecrush_lower = whitecrush_lower;
3879
3880 /* fill struct bttv with some useful defaults */
3881 btv->init.btv = btv;
3882 btv->init.ov.w.width = 320;
3883 btv->init.ov.w.height = 240;
3884 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
3885 btv->init.width = 320;
3886 btv->init.height = 240;
3887 btv->init.lines = 16;
3888 btv->input = 0;
3889
3890 /* initialize hardware */
3891 if (bttv_gpio)
3892 bttv_gpio_tracking(btv,"pre-init");
3893
3894 bttv_risc_init_main(btv);
3895 init_bt848(btv);
3896
3897 /* gpio */
3898 btwrite(0x00, BT848_GPIO_REG_INP);
3899 btwrite(0x00, BT848_GPIO_OUT_EN);
3900 if (bttv_verbose)
3901 bttv_gpio_tracking(btv,"init");
3902
3903 /* needs to be done before i2c is registered */
3904 bttv_init_card1(btv);
3905
3906 /* register i2c + gpio */
3907 init_bttv_i2c(btv);
3908
3909 /* some card-specific stuff (needs working i2c) */
3910 bttv_init_card2(btv);
3911 init_irqreg(btv);
3912
3913 /* register video4linux + input */
3914 if (!bttv_tvcards[btv->c.type].no_video) {
3915 bttv_register_video(btv);
3916 bt848_bright(btv,32768);
3917 bt848_contrast(btv,32768);
3918 bt848_hue(btv,32768);
3919 bt848_sat(btv,32768);
3920 audio_mux(btv,AUDIO_MUTE);
3921 set_input(btv,0);
3922 }
3923
3924 /* add subdevices */
3925 if (btv->has_remote)
3926 bttv_sub_add_device(&btv->c, "remote");
3927 if (bttv_tvcards[btv->c.type].has_dvb)
3928 bttv_sub_add_device(&btv->c, "dvb");
3929
3930 /* everything is fine */
3931 bttv_num++;
3932 return 0;
3933
3934 fail2:
3935 free_irq(btv->c.pci->irq,btv);
3936
3937 fail1:
3938 if (btv->bt848_mmio)
3939 iounmap(btv->bt848_mmio);
3940 release_mem_region(pci_resource_start(btv->c.pci,0),
3941 pci_resource_len(btv->c.pci,0));
3942 pci_set_drvdata(dev,NULL);
3943 return result;
3944}
3945
3946static void __devexit bttv_remove(struct pci_dev *pci_dev)
3947{
3948 struct bttv *btv = pci_get_drvdata(pci_dev);
3949
3950 if (bttv_verbose)
3951 printk("bttv%d: unloading\n",btv->c.nr);
3952
3953 /* shutdown everything (DMA+IRQs) */
3954 btand(~15, BT848_GPIO_DMA_CTL);
3955 btwrite(0, BT848_INT_MASK);
3956 btwrite(~0x0, BT848_INT_STAT);
3957 btwrite(0x0, BT848_GPIO_OUT_EN);
3958 if (bttv_gpio)
3959 bttv_gpio_tracking(btv,"cleanup");
3960
3961 /* tell gpio modules we are leaving ... */
3962 btv->shutdown=1;
3963 wake_up(&btv->gpioq);
3964 bttv_sub_del_devices(&btv->c);
3965
3966 /* unregister i2c_bus + input */
3967 fini_bttv_i2c(btv);
3968
3969 /* unregister video4linux */
3970 bttv_unregister_video(btv);
3971
3972 /* free allocated memory */
3973 btcx_riscmem_free(btv->c.pci,&btv->main);
3974
3975 /* free ressources */
3976 free_irq(btv->c.pci->irq,btv);
3977 iounmap(btv->bt848_mmio);
3978 release_mem_region(pci_resource_start(btv->c.pci,0),
3979 pci_resource_len(btv->c.pci,0));
3980
3981 pci_set_drvdata(pci_dev, NULL);
3982 return;
3983}
3984
3985static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
3986{
3987 struct bttv *btv = pci_get_drvdata(pci_dev);
3988 struct bttv_buffer_set idle;
3989 unsigned long flags;
3990
3991 dprintk("bttv%d: suspend %d\n", btv->c.nr, state);
3992
3993 /* stop dma + irqs */
3994 spin_lock_irqsave(&btv->s_lock,flags);
3995 memset(&idle, 0, sizeof(idle));
3996 btv->state.video = btv->curr;
3997 btv->state.vbi = btv->cvbi;
3998 btv->state.loop_irq = btv->loop_irq;
3999 btv->curr = idle;
4000 btv->loop_irq = 0;
4001 bttv_buffer_activate_video(btv, &idle);
4002 bttv_buffer_activate_vbi(btv, NULL);
4003 bttv_set_dma(btv, 0);
4004 btwrite(0, BT848_INT_MASK);
4005 spin_unlock_irqrestore(&btv->s_lock,flags);
4006
4007 /* save bt878 state */
4008 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4009 btv->state.gpio_data = gpio_read();
4010
4011 /* save pci state */
4012 pci_save_state(pci_dev);
4013 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4014 pci_disable_device(pci_dev);
4015 btv->state.disabled = 1;
4016 }
4017 return 0;
4018}
4019
4020static int bttv_resume(struct pci_dev *pci_dev)
4021{
4022 struct bttv *btv = pci_get_drvdata(pci_dev);
4023 unsigned long flags;
4024
4025 dprintk("bttv%d: resume\n", btv->c.nr);
4026
4027 /* restore pci state */
4028 if (btv->state.disabled) {
4029 pci_enable_device(pci_dev);
4030 btv->state.disabled = 0;
4031 }
4032 pci_set_power_state(pci_dev, PCI_D0);
4033 pci_restore_state(pci_dev);
4034
4035 /* restore bt878 state */
4036 bttv_reinit_bt848(btv);
4037 gpio_inout(0xffffff, btv->state.gpio_enable);
4038 gpio_write(btv->state.gpio_data);
4039
4040 /* restart dma */
4041 spin_lock_irqsave(&btv->s_lock,flags);
4042 btv->curr = btv->state.video;
4043 btv->cvbi = btv->state.vbi;
4044 btv->loop_irq = btv->state.loop_irq;
4045 bttv_buffer_activate_video(btv, &btv->curr);
4046 bttv_buffer_activate_vbi(btv, btv->cvbi);
4047 bttv_set_dma(btv, 0);
4048 spin_unlock_irqrestore(&btv->s_lock,flags);
4049 return 0;
4050}
4051
4052static struct pci_device_id bttv_pci_tbl[] = {
4053 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4054 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4055 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
4056 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4057 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
4058 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4059 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
4060 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4061 {0,}
4062};
4063
4064MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4065
4066static struct pci_driver bttv_pci_driver = {
4067 .name = "bttv",
4068 .id_table = bttv_pci_tbl,
4069 .probe = bttv_probe,
4070 .remove = __devexit_p(bttv_remove),
4071 .suspend = bttv_suspend,
4072 .resume = bttv_resume,
4073};
4074
4075static int bttv_init_module(void)
4076{
4077 bttv_num = 0;
4078
4079 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4080 (BTTV_VERSION_CODE >> 16) & 0xff,
4081 (BTTV_VERSION_CODE >> 8) & 0xff,
4082 BTTV_VERSION_CODE & 0xff);
4083#ifdef SNAPSHOT
4084 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4085 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4086#endif
4087 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4088 gbuffers = 2;
4089 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4090 gbufsize = BTTV_MAX_FBUF;
4091 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4092 if (bttv_verbose)
4093 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4094 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4095
4096 bttv_check_chipset();
4097
4098 bus_register(&bttv_sub_bus_type);
4099 return pci_module_init(&bttv_pci_driver);
4100}
4101
4102static void bttv_cleanup_module(void)
4103{
4104 pci_unregister_driver(&bttv_pci_driver);
4105 bus_unregister(&bttv_sub_bus_type);
4106 return;
4107}
4108
4109module_init(bttv_init_module);
4110module_exit(bttv_cleanup_module);
4111
4112/*
4113 * Local variables:
4114 * c-basic-offset: 8
4115 * End:
4116 */