Subversion Repositories shark

Rev

Rev 516 | Rev 541 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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