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