Subversion Repositories shark

Rev

Rev 618 | Rev 623 | 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
 
614 giacomo 46
unsigned int bttv_debug = 0;
428 giacomo 47
unsigned int bttv_verbose = 1;
614 giacomo 48
unsigned int bttv_gpio = 0;
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
 
541 giacomo 61
static int video_nr = -1;
62
static int radio_nr = -1;
516 giacomo 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
        }
541 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
 
541 giacomo 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
 
607 giacomo 1267
        dprintk("switch_overlay: enter [new=%x]\n",(int)new);
428 giacomo 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) {
607 giacomo 1278
                dprintk("switch_overlay: old=%x state is %d\n",(int)old,old->vb.state);
608 giacomo 1279
                bttv_dma_free(btv, old);
428 giacomo 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;
480 giacomo 1498
                //down(&btv->lock);
428 giacomo 1499
                if (channel == btv->input &&
1500
                    v->norm == btv->tvnorm) {
1501
                        /* nothing to do */
480 giacomo 1502
                        //up(&btv->lock);
428 giacomo 1503
                        return 0;
1504
                }
170 giacomo 1505
 
428 giacomo 1506
                btv->tvnorm = v->norm;
1507
                set_input(btv,v->channel);
480 giacomo 1508
                //up(&btv->lock);
428 giacomo 1509
                return 0;
1510
        }
170 giacomo 1511
 
428 giacomo 1512
        case VIDIOCGAUDIO:
1513
        {
1514
                struct video_audio *v = arg;
170 giacomo 1515
 
428 giacomo 1516
                memset(v,0,sizeof(*v));
1517
                strcpy(v->name,"Television");
1518
                v->flags |= VIDEO_AUDIO_MUTABLE;
1519
                v->mode  = VIDEO_SOUND_MONO;
170 giacomo 1520
 
480 giacomo 1521
                //down(&btv->lock);
428 giacomo 1522
                bttv_call_i2c_clients(btv,cmd,v);
170 giacomo 1523
 
428 giacomo 1524
                /* card specific hooks */
1525
                if (btv->audio_hook)
1526
                        btv->audio_hook(btv,v,0);
170 giacomo 1527
 
480 giacomo 1528
                //up(&btv->lock);
428 giacomo 1529
                return 0;
1530
        }
1531
        case VIDIOCSAUDIO:
1532
        {
1533
                struct video_audio *v = arg;
1534
                unsigned int audio = v->audio;
170 giacomo 1535
 
428 giacomo 1536
                if (audio >= bttv_tvcards[btv->type].audio_inputs)
1537
                        return -EINVAL;
170 giacomo 1538
 
480 giacomo 1539
                //down(&btv->lock);
428 giacomo 1540
                audio_mux(btv, (v->flags&VIDEO_AUDIO_MUTE) ? AUDIO_MUTE : AUDIO_UNMUTE);
1541
                bttv_call_i2c_clients(btv,cmd,v);
170 giacomo 1542
 
428 giacomo 1543
                /* card specific hooks */
1544
                if (btv->audio_hook)
1545
                        btv->audio_hook(btv,v,1);
1546
 
480 giacomo 1547
                //up(&btv->lock);
428 giacomo 1548
                return 0;
1549
        }
170 giacomo 1550
 
428 giacomo 1551
        /* ***  v4l2  *** ************************************************ */
1552
        case VIDIOC_ENUMSTD:
1553
        {
1554
                struct v4l2_standard *e = arg;
1555
                unsigned int index = e->index;
1556
 
1557
                if (index >= BTTV_TVNORMS)
1558
                        return -EINVAL;
516 giacomo 1559
                //v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1560
                //                       bttv_tvnorms[e->index].name);
428 giacomo 1561
                e->index = index;
1562
                return 0;
1563
        }
1564
        case VIDIOC_G_STD:
1565
        {
1566
                v4l2_std_id *id = arg;
1567
                *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1568
                return 0;
1569
        }
1570
        case VIDIOC_S_STD:
1571
        {
1572
                v4l2_std_id *id = arg;
1573
                unsigned int i;
170 giacomo 1574
 
428 giacomo 1575
                for (i = 0; i < BTTV_TVNORMS; i++)
1576
                        if (*id & bttv_tvnorms[i].v4l2_id)
1577
                                break;
1578
                if (i == BTTV_TVNORMS)
1579
                        return -EINVAL;
170 giacomo 1580
 
480 giacomo 1581
                //down(&btv->lock);
428 giacomo 1582
                set_tvnorm(btv,i);
1583
                i2c_vidiocschan(btv);
480 giacomo 1584
                //up(&btv->lock);
428 giacomo 1585
                return 0;
1586
        }
1587
        case VIDIOC_QUERYSTD:
1588
        {
1589
                v4l2_std_id *id = arg;
1590
 
1591
                if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1592
                        *id = V4L2_STD_625_50;
1593
                else
1594
                        *id = V4L2_STD_525_60;
1595
                return 0;
1596
        }
170 giacomo 1597
 
428 giacomo 1598
        case VIDIOC_ENUMINPUT:
1599
        {
1600
                struct v4l2_input *i = arg;
1601
                unsigned int n;
1602
 
1603
                n = i->index;
1604
                if (n >= bttv_tvcards[btv->type].video_inputs)
1605
                        return -EINVAL;
1606
                memset(i,0,sizeof(*i));
1607
                i->index    = n;
1608
                i->type     = V4L2_INPUT_TYPE_CAMERA;
1609
                i->audioset = 1;
1610
                if (i->index == bttv_tvcards[btv->type].tuner) {
463 giacomo 1611
                        sprintf26(i->name, "Television");
428 giacomo 1612
                        i->type  = V4L2_INPUT_TYPE_TUNER;
1613
                        i->tuner = 0;
1614
                } else if (i->index == btv->svhs) {
463 giacomo 1615
                        sprintf26(i->name, "S-Video");
428 giacomo 1616
                } else {
463 giacomo 1617
                        sprintf26(i->name,"Composite%d",i->index);
428 giacomo 1618
                }
1619
                if (i->index == btv->input) {
1620
                        __u32 dstatus = btread(BT848_DSTATUS);
1621
                        if (0 == (dstatus & BT848_DSTATUS_PRES))
1622
                                i->status |= V4L2_IN_ST_NO_SIGNAL;
1623
                        if (0 == (dstatus & BT848_DSTATUS_HLOC))
1624
                                i->status |= V4L2_IN_ST_NO_H_LOCK;
1625
                }
1626
                for (n = 0; n < BTTV_TVNORMS; n++)
1627
                        i->std |= bttv_tvnorms[n].v4l2_id;
1628
                return 0;
1629
        }
1630
        case VIDIOC_G_INPUT:
1631
        {
1632
                int *i = arg;
1633
                *i = btv->input;
1634
                return 0;
1635
        }
1636
        case VIDIOC_S_INPUT:
1637
        {
1638
                unsigned int *i = arg;
1639
 
1640
                if (*i > bttv_tvcards[btv->type].video_inputs)
1641
                        return -EINVAL;
480 giacomo 1642
                //down(&btv->lock);
428 giacomo 1643
                set_input(btv,*i);
1644
                i2c_vidiocschan(btv);
480 giacomo 1645
                //up(&btv->lock);
428 giacomo 1646
                return 0;
1647
        }
1648
 
1649
        case VIDIOC_G_TUNER:
1650
        {
1651
                struct v4l2_tuner *t = arg;
170 giacomo 1652
 
428 giacomo 1653
                if (UNSET == bttv_tvcards[btv->type].tuner)
1654
                        return -EINVAL;
1655
                if (0 != t->index)
1656
                        return -EINVAL;
480 giacomo 1657
                //down(&btv->lock);
428 giacomo 1658
                memset(t,0,sizeof(*t));
1659
                strcpy(t->name, "Television");
1660
                t->type       = V4L2_TUNER_ANALOG_TV;
1661
                t->rangehigh  = 0xffffffffUL;
1662
                t->capability = V4L2_TUNER_CAP_NORM;
1663
                t->rxsubchans = V4L2_TUNER_SUB_MONO;
1664
                if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
1665
                        t->signal = 0xffff;
1666
                {
1667
                        /* Hmmm ... */
1668
                        struct video_audio va;
1669
                        memset(&va, 0, sizeof(struct video_audio));
1670
                        bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1671
                        if (btv->audio_hook)
1672
                                btv->audio_hook(btv,&va,0);
1673
                        if(va.mode & VIDEO_SOUND_STEREO) {
1674
                                t->audmode     = V4L2_TUNER_MODE_STEREO;
1675
                                t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1676
                        }
1677
                        if(va.mode & VIDEO_SOUND_LANG1) {
1678
                                t->audmode    = V4L2_TUNER_MODE_LANG1;
1679
                                t->rxsubchans = V4L2_TUNER_SUB_LANG1
1680
                                        | V4L2_TUNER_SUB_LANG2;
1681
                        }
1682
                }
1683
                /* FIXME: fill capability+audmode */
480 giacomo 1684
                //up(&btv->lock);
428 giacomo 1685
                return 0;
1686
        }
1687
        case VIDIOC_S_TUNER:
1688
        {
1689
                struct v4l2_tuner *t = arg;
170 giacomo 1690
 
428 giacomo 1691
                if (UNSET == bttv_tvcards[btv->type].tuner)
1692
                        return -EINVAL;
1693
                if (0 != t->index)
1694
                        return -EINVAL;
480 giacomo 1695
                //down(&btv->lock);
428 giacomo 1696
                {
1697
                        struct video_audio va;
1698
                        memset(&va, 0, sizeof(struct video_audio));
1699
                        bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1700
                        if (t->audmode == V4L2_TUNER_MODE_MONO)
1701
                                va.mode = VIDEO_SOUND_MONO;
1702
                        else if (t->audmode == V4L2_TUNER_MODE_STEREO)
1703
                                va.mode = VIDEO_SOUND_STEREO;
1704
                        else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1705
                                va.mode = VIDEO_SOUND_LANG1;
1706
                        else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1707
                                va.mode = VIDEO_SOUND_LANG2;
1708
                        bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1709
                        if (btv->audio_hook)
1710
                                btv->audio_hook(btv,&va,1);
1711
                }
480 giacomo 1712
                //up(&btv->lock);
428 giacomo 1713
                return 0;
1714
        }
1715
 
1716
        case VIDIOC_G_FREQUENCY:
1717
        {
1718
                struct v4l2_frequency *f = arg;
1719
 
1720
                memset(f,0,sizeof(*f));
1721
                f->type = V4L2_TUNER_ANALOG_TV;
1722
                f->frequency = btv->freq;
1723
                return 0;
1724
        }
1725
        case VIDIOC_S_FREQUENCY:
1726
        {
1727
                struct v4l2_frequency *f = arg;
1728
 
1729
                if (unlikely(f->tuner != 0))
1730
                        return -EINVAL;
1731
                if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1732
                        return -EINVAL;
480 giacomo 1733
                //down(&btv->lock);
428 giacomo 1734
                btv->freq = f->frequency;
1735
                bttv_call_i2c_clients(btv,VIDIOCSFREQ,&btv->freq);
1736
                if (btv->has_matchbox && btv->radio_user)
1737
                        tea5757_set_freq(btv,btv->freq);
480 giacomo 1738
                //up(&btv->lock);
428 giacomo 1739
                return 0;
1740
        }
1741
 
1742
        default:
1743
                return -ENOIOCTLCMD;
1744
 
1745
        }
1746
        return 0;
170 giacomo 1747
}
1748
 
428 giacomo 1749
static int verify_window(const struct bttv_tvnorm *tvn,
1750
                         struct v4l2_window *win, int fixup)
170 giacomo 1751
{
428 giacomo 1752
        enum v4l2_field field;
1753
        int maxw, maxh;
170 giacomo 1754
 
428 giacomo 1755
        if (win->w.width  < 48 || win->w.height < 32)
1756
                return -EINVAL;
1757
        if (win->clipcount > 2048)
1758
                return -EINVAL;
170 giacomo 1759
 
428 giacomo 1760
        field = win->field;
1761
        maxw  = tvn->swidth;
1762
        maxh  = tvn->sheight;
170 giacomo 1763
 
428 giacomo 1764
        if (V4L2_FIELD_ANY == field) {
1765
                field = (win->w.height > maxh/2)
1766
                        ? V4L2_FIELD_INTERLACED
1767
                        : V4L2_FIELD_TOP;
1768
        }
1769
        switch (field) {
1770
        case V4L2_FIELD_TOP:
1771
        case V4L2_FIELD_BOTTOM:
1772
                maxh = maxh / 2;
1773
                break;
1774
        case V4L2_FIELD_INTERLACED:
1775
                break;
1776
        default:
1777
                return -EINVAL;
1778
        }
170 giacomo 1779
 
428 giacomo 1780
        if (!fixup && (win->w.width > maxw || win->w.height > maxh))
1781
                return -EINVAL;
170 giacomo 1782
 
428 giacomo 1783
        if (win->w.width > maxw)
1784
                win->w.width = maxw;
1785
        if (win->w.height > maxh)
1786
                win->w.height = maxh;
1787
        win->field = field;
1788
        return 0;
170 giacomo 1789
}
1790
 
428 giacomo 1791
static int setup_window(struct bttv_fh *fh, struct bttv *btv,
1792
                        struct v4l2_window *win, int fixup)
170 giacomo 1793
{
428 giacomo 1794
        struct v4l2_clip *clips = NULL;
1795
        int n,size,retval = 0;
170 giacomo 1796
 
428 giacomo 1797
        if (NULL == fh->ovfmt)
1798
                return -EINVAL;
1799
        retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
1800
        if (0 != retval)
1801
                return retval;
257 giacomo 1802
 
428 giacomo 1803
        /* copy clips  --  luckily v4l1 + v4l2 are binary
1804
           compatible here ...*/
1805
        n = win->clipcount;
1806
        size = sizeof(*clips)*(n+4);
1807
        clips = kmalloc(size,GFP_KERNEL);
1808
        if (NULL == clips)
1809
                return -ENOMEM;
1810
        if (n > 0) {
1811
                if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
1812
                        kfree(clips);
1813
                        return -EFAULT;
1814
                }
1815
        }
541 giacomo 1816
 
428 giacomo 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
        }
541 giacomo 1861
 
480 giacomo 1862
        //up(&fh->cap.lock);
428 giacomo 1863
        return retval;
170 giacomo 1864
}
1865
 
428 giacomo 1866
/* ----------------------------------------------------------------------- */
170 giacomo 1867
 
428 giacomo 1868
static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
170 giacomo 1869
{
428 giacomo 1870
        struct videobuf_queue* q = NULL;
1871
 
1872
        switch (fh->type) {
1873
        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1874
                q = &fh->cap;
1875
                break;
1876
        case V4L2_BUF_TYPE_VBI_CAPTURE:
1877
                q = &fh->vbi;
1878
                break;
1879
        default:
1880
                BUG();
1881
        }
1882
        return q;
170 giacomo 1883
}
1884
 
428 giacomo 1885
static int bttv_resource(struct bttv_fh *fh)
170 giacomo 1886
{
428 giacomo 1887
        int res = 0;
1888
 
1889
        switch (fh->type) {
1890
        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1891
                res = RESOURCE_VIDEO;
1892
                break;
1893
        case V4L2_BUF_TYPE_VBI_CAPTURE:
1894
                res = RESOURCE_VBI;
1895
                break;
1896
        default:
1897
                BUG();
1898
        }
1899
        return res;
170 giacomo 1900
}
1901
 
428 giacomo 1902
static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
170 giacomo 1903
{
428 giacomo 1904
        struct videobuf_queue *q = bttv_queue(fh);
1905
        int res = bttv_resource(fh);
170 giacomo 1906
 
428 giacomo 1907
        if (check_btres(fh,res))
1908
                return -EBUSY;
1909
        if (videobuf_queue_is_busy(q))
1910
                return -EBUSY;
1911
        fh->type = type;
1912
        return 0;
170 giacomo 1913
}
1914
 
428 giacomo 1915
static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
170 giacomo 1916
{
428 giacomo 1917
        switch (f->type) {
1918
        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1919
                memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
1920
                f->fmt.pix.width        = fh->width;
1921
                f->fmt.pix.height       = fh->height;
1922
                f->fmt.pix.field        = fh->cap.field;
1923
                f->fmt.pix.pixelformat  = fh->fmt->fourcc;
1924
                f->fmt.pix.bytesperline =
1925
                        (f->fmt.pix.width * fh->fmt->depth) >> 3;
1926
                f->fmt.pix.sizeimage =
1927
                        f->fmt.pix.height * f->fmt.pix.bytesperline;
1928
                return 0;
1929
        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1930
                memset(&f->fmt.win,0,sizeof(struct v4l2_window));
1931
                f->fmt.win.w     = fh->ov.w;
1932
                f->fmt.win.field = fh->ov.field;
1933
                return 0;
1934
        case V4L2_BUF_TYPE_VBI_CAPTURE:
516 giacomo 1935
                //bttv_vbi_get_fmt(fh,f);
428 giacomo 1936
                return 0;
1937
        default:
1938
                return -EINVAL;
1939
        }
1940
}
170 giacomo 1941
 
428 giacomo 1942
static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
1943
                        struct v4l2_format *f)
1944
{
1945
        switch (f->type) {
1946
        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1947
        {
1948
                const struct bttv_format *fmt;
1949
                enum v4l2_field field;
1950
                unsigned int maxw,maxh;
1951
 
1952
                fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1953
                if (NULL == fmt)
1954
                        return -EINVAL;
1955
 
1956
                /* fixup format */
1957
                maxw  = bttv_tvnorms[btv->tvnorm].swidth;
1958
                maxh  = bttv_tvnorms[btv->tvnorm].sheight;
1959
                field = f->fmt.pix.field;
1960
                if (V4L2_FIELD_ANY == field)
1961
                        field = (f->fmt.pix.height > maxh/2)
1962
                                ? V4L2_FIELD_INTERLACED
1963
                                : V4L2_FIELD_BOTTOM;
1964
                if (V4L2_FIELD_SEQ_BT == field)
1965
                        field = V4L2_FIELD_SEQ_TB;
1966
                switch (field) {
1967
                case V4L2_FIELD_TOP:
1968
                case V4L2_FIELD_BOTTOM:
1969
                case V4L2_FIELD_ALTERNATE:
1970
                        maxh = maxh/2;
1971
                        break;
1972
                case V4L2_FIELD_INTERLACED:
1973
                        break;
1974
                case V4L2_FIELD_SEQ_TB:
1975
                        if (fmt->flags & FORMAT_FLAGS_PLANAR)
1976
                                return -EINVAL;
1977
                        break;
1978
                default:
1979
                        return -EINVAL;
1980
                }
1981
 
1982
                /* update data for the application */
1983
                f->fmt.pix.field = field;
1984
                if (f->fmt.pix.width  < 48)
1985
                        f->fmt.pix.width  = 48;
1986
                if (f->fmt.pix.height < 32)
1987
                        f->fmt.pix.height = 32;
1988
                if (f->fmt.pix.width  > maxw)
1989
                        f->fmt.pix.width = maxw;
1990
                if (f->fmt.pix.height > maxh)
1991
                        f->fmt.pix.height = maxh;
1992
                f->fmt.pix.bytesperline =
1993
                        (f->fmt.pix.width * fmt->depth) >> 3;
1994
                f->fmt.pix.sizeimage =
1995
                        f->fmt.pix.height * f->fmt.pix.bytesperline;
1996
 
1997
                return 0;
1998
        }
1999
        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2000
                return verify_window(&bttv_tvnorms[btv->tvnorm],
2001
                                     &f->fmt.win, 1);
2002
        case V4L2_BUF_TYPE_VBI_CAPTURE:
516 giacomo 2003
                //bttv_vbi_try_fmt(fh,f);
428 giacomo 2004
                return 0;
2005
        default:
2006
                return -EINVAL;
2007
        }
170 giacomo 2008
}
2009
 
428 giacomo 2010
static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2011
                      struct v4l2_format *f)
170 giacomo 2012
{
428 giacomo 2013
        int retval;
2014
 
2015
        switch (f->type) {
2016
        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2017
        {
2018
                const struct bttv_format *fmt;
170 giacomo 2019
 
428 giacomo 2020
                retval = bttv_switch_type(fh,f->type);
2021
                if (0 != retval)
2022
                        return retval;
2023
                retval = bttv_try_fmt(fh,btv,f);
2024
                if (0 != retval)
2025
                        return retval;
2026
                fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2027
 
2028
                /* update our state informations */
480 giacomo 2029
                //down(&fh->cap.lock);
428 giacomo 2030
                fh->fmt              = fmt;
2031
                fh->cap.field        = f->fmt.pix.field;
2032
                fh->cap.last         = V4L2_FIELD_NONE;
2033
                fh->width            = f->fmt.pix.width;
2034
                fh->height           = f->fmt.pix.height;
2035
                btv->init.fmt        = fmt;
2036
                btv->init.width      = f->fmt.pix.width;
2037
                btv->init.height     = f->fmt.pix.height;
480 giacomo 2038
                //up(&fh->cap.lock);
428 giacomo 2039
 
2040
                return 0;
2041
        }
2042
        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2043
                return setup_window(fh, btv, &f->fmt.win, 1);
2044
        case V4L2_BUF_TYPE_VBI_CAPTURE:
2045
                retval = bttv_switch_type(fh,f->type);
2046
                if (0 != retval)
2047
                        return retval;
2048
                if (locked_btres(fh->btv, RESOURCE_VBI))
2049
                        return -EBUSY;
516 giacomo 2050
                //bttv_vbi_try_fmt(fh,f);
2051
                //bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
2052
                //bttv_vbi_get_fmt(fh,f);
428 giacomo 2053
                return 0;
2054
        default:
2055
                return -EINVAL;
2056
        }
170 giacomo 2057
}
2058
 
516 giacomo 2059
int bttv_do_ioctl(struct inode *inode, struct file *file,
428 giacomo 2060
                         unsigned int cmd, void *arg)
170 giacomo 2061
{
428 giacomo 2062
        struct bttv_fh *fh  = file->private_data;
2063
        struct bttv    *btv = fh->btv;
2064
        unsigned long flags;
2065
        int retval = 0;
170 giacomo 2066
 
428 giacomo 2067
        if (bttv_debug > 1) {
2068
                switch (_IOC_TYPE(cmd)) {
2069
                case 'v':
516 giacomo 2070
                        //printk("bttv%d: ioctl 0x%x (v4l1, VIDIOC%s)\n",
2071
                        //       btv->nr, cmd, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
2072
                        //       v4l1_ioctls[_IOC_NR(cmd)] : "???");
428 giacomo 2073
                        break;
2074
                case 'V':
516 giacomo 2075
                        //printk("bttv%d: ioctl 0x%x (v4l2, %s)\n",
2076
                        //       btv->nr, cmd,  v4l2_ioctl_names[_IOC_NR(cmd)]);
428 giacomo 2077
                        break;
2078
                default:
2079
                        printk("bttv%d: ioctl 0x%x ()\n",
2080
                               btv->nr, cmd);
2081
                }
2082
        }
2083
        if (btv->errors)
2084
                bttv_reinit_bt848(btv);
170 giacomo 2085
 
428 giacomo 2086
#ifdef VIDIOC_G_PRIORITY
170 giacomo 2087
        switch (cmd) {
428 giacomo 2088
        case VIDIOCSFREQ:
2089
        case VIDIOCSTUNER:
2090
        case VIDIOCSCHAN:
2091
        case VIDIOC_S_CTRL:
2092
        case VIDIOC_S_STD:
2093
        case VIDIOC_S_INPUT:
2094
        case VIDIOC_S_TUNER:
2095
        case VIDIOC_S_FREQUENCY:
2096
                retval = v4l2_prio_check(&btv->prio,&fh->prio);
2097
                if (0 != retval)
2098
                        return retval;
2099
        };
2100
#endif
605 giacomo 2101
 
428 giacomo 2102
        switch (cmd) {
2103
 
2104
        /* ***  v4l1  *** ************************************************ */
170 giacomo 2105
        case VIDIOCGCAP:
2106
        {
428 giacomo 2107
                struct video_capability *cap = arg;
2108
 
2109
                memset(cap,0,sizeof(*cap));
2110
                strcpy(cap->name,btv->video_dev->name);
2111
                if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2112
                        /* vbi */
2113
                        cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2114
                } else {
2115
                        /* others */
2116
                        cap->type = VID_TYPE_CAPTURE|
2117
                                VID_TYPE_TUNER|
2118
                                VID_TYPE_OVERLAY|
2119
                                VID_TYPE_CLIPPING|
2120
                                VID_TYPE_SCALES;
2121
                        cap->channels  = bttv_tvcards[btv->type].video_inputs;
2122
                        cap->audios    = bttv_tvcards[btv->type].audio_inputs;
2123
                        cap->maxwidth  = bttv_tvnorms[btv->tvnorm].swidth;
2124
                        cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2125
                        cap->minwidth  = 48;
2126
                        cap->minheight = 32;
2127
                }
2128
                return 0;
170 giacomo 2129
        }
428 giacomo 2130
 
2131
        case VIDIOCGPICT:
170 giacomo 2132
        {
428 giacomo 2133
                struct video_picture *pic = arg;
2134
 
2135
                memset(pic,0,sizeof(*pic));
2136
                pic->brightness = btv->bright;
2137
                pic->contrast   = btv->contrast;
2138
                pic->hue        = btv->hue;
2139
                pic->colour     = btv->saturation;
2140
                if (fh->fmt) {
2141
                        pic->depth   = fh->fmt->depth;
2142
                        pic->palette = fh->fmt->palette;
2143
                }
170 giacomo 2144
                return 0;
2145
        }
428 giacomo 2146
        case VIDIOCSPICT:
170 giacomo 2147
        {
428 giacomo 2148
                struct video_picture *pic = arg;
2149
                const struct bttv_format *fmt;
170 giacomo 2150
 
428 giacomo 2151
                fmt = format_by_palette(pic->palette);
2152
                if (NULL == fmt)
170 giacomo 2153
                        return -EINVAL;
480 giacomo 2154
                //down(&fh->cap.lock);
428 giacomo 2155
                if (fmt->depth != pic->depth && !sloppy) {
2156
                        retval = -EINVAL;
2157
                        goto fh_unlock_and_return;
2158
                }
2159
                fh->ovfmt   = fmt;
2160
                fh->fmt     = fmt;
2161
                btv->init.ovfmt   = fmt;
2162
                btv->init.fmt     = fmt;
2163
                if (bigendian) {
2164
                        /* dirty hack time:  swap bytes for overlay if the
2165
                           display adaptor is big endian (insmod option) */
2166
                        if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2167
                            fmt->palette == VIDEO_PALETTE_RGB565 ||
2168
                            fmt->palette == VIDEO_PALETTE_RGB32) {
2169
                                fh->ovfmt = fmt+1;
2170
                        }
2171
                }
2172
                bt848_bright(btv,pic->brightness);
2173
                bt848_contrast(btv,pic->contrast);
2174
                bt848_hue(btv,pic->hue);
2175
                bt848_sat(btv,pic->colour);
480 giacomo 2176
                //up(&fh->cap.lock);
428 giacomo 2177
                return 0;
2178
        }
170 giacomo 2179
 
428 giacomo 2180
        case VIDIOCGWIN:
2181
        {
2182
                struct video_window *win = arg;
2183
 
2184
                memset(win,0,sizeof(*win));
2185
                win->x      = fh->ov.w.left;
2186
                win->y      = fh->ov.w.top;
2187
                win->width  = fh->ov.w.width;
2188
                win->height = fh->ov.w.height;
170 giacomo 2189
                return 0;
2190
        }
428 giacomo 2191
        case VIDIOCSWIN:
170 giacomo 2192
        {
428 giacomo 2193
                struct video_window *win = arg;
2194
                struct v4l2_window w2;
2195
 
2196
                w2.field = V4L2_FIELD_ANY;
2197
                w2.w.left    = win->x;
2198
                w2.w.top     = win->y;
2199
                w2.w.width   = win->width;
2200
                w2.w.height  = win->height;
2201
                w2.clipcount = win->clipcount;
2202
                w2.clips     = (struct v4l2_clip*)win->clips;
2203
                retval = setup_window(fh, btv, &w2, 0);
2204
                if (0 == retval) {
2205
                        /* on v4l1 this ioctl affects the read() size too */
2206
                        fh->width  = fh->ov.w.width;
2207
                        fh->height = fh->ov.w.height;
2208
                        btv->init.width  = fh->ov.w.width;
2209
                        btv->init.height = fh->ov.w.height;
2210
                }
2211
                return retval;
2212
        }
2213
 
2214
        case VIDIOCGFBUF:
2215
        {
2216
                struct video_buffer *fbuf = arg;
2217
 
2218
                fbuf->base          = btv->fbuf.base;
2219
                fbuf->width         = btv->fbuf.fmt.width;
2220
                fbuf->height        = btv->fbuf.fmt.height;
2221
                fbuf->bytesperline  = btv->fbuf.fmt.bytesperline;
2222
                if (fh->ovfmt)
2223
                        fbuf->depth = fh->ovfmt->depth;
170 giacomo 2224
                return 0;
2225
        }
428 giacomo 2226
        case VIDIOCSFBUF:
170 giacomo 2227
        {
428 giacomo 2228
                struct video_buffer *fbuf = arg;
2229
                const struct bttv_format *fmt;
2230
                unsigned long end;
2231
 
2232
                end = (unsigned long)fbuf->base +
2233
                        fbuf->height * fbuf->bytesperline;
480 giacomo 2234
                //down(&fh->cap.lock);
428 giacomo 2235
                retval = -EINVAL;
2236
                if (sloppy) {
2237
                        /* also set the default palette -- for backward
2238
                           compatibility with older versions */
2239
                        switch (fbuf->depth) {
2240
                        case 8:
2241
                                fmt = format_by_palette(VIDEO_PALETTE_HI240);
2242
                                break;
2243
                        case 16:
2244
                                fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2245
                                break;
2246
                        case 24:
2247
                                fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2248
                                break;
2249
                        case 32:
2250
                                fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2251
                                break;
2252
                        case 15:
2253
                                fbuf->depth = 16;
2254
                                fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2255
                                break;
2256
                        default:
2257
                                fmt = NULL;
2258
                                break;
2259
                        }
2260
                        if (NULL == fmt)
2261
                                goto fh_unlock_and_return;
2262
                        fh->ovfmt = fmt;
2263
                        fh->fmt   = fmt;
2264
                        btv->init.ovfmt = fmt;
2265
                        btv->init.fmt   = fmt;
2266
                } else {
2267
                        if (15 == fbuf->depth)
2268
                                fbuf->depth = 16;
2269
                        if (fbuf->depth !=  8 && fbuf->depth != 16 &&
2270
                            fbuf->depth != 24 && fbuf->depth != 32)
2271
                                goto fh_unlock_and_return;
170 giacomo 2272
                }
428 giacomo 2273
                btv->fbuf.base             = fbuf->base;
2274
                btv->fbuf.fmt.width        = fbuf->width;
2275
                btv->fbuf.fmt.height       = fbuf->height;
2276
                if (fbuf->bytesperline)
2277
                        btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2278
                else
2279
                        btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
480 giacomo 2280
                //up(&fh->cap.lock);
170 giacomo 2281
                return 0;
2282
        }
428 giacomo 2283
 
2284
        case VIDIOCCAPTURE:
2285
        case VIDIOC_OVERLAY:
170 giacomo 2286
        {
428 giacomo 2287
                struct bttv_buffer *new;
2288
                int *on = arg;
2289
 
2290
                if (*on) {
2291
                        /* verify args */
2292
                        if (NULL == btv->fbuf.base)
2293
                                return -EINVAL;
2294
                        if (!fh->ov.setup_ok) {
2295
                                dprintk("bttv%d: overlay: !setup_ok\n",btv->nr);
2296
                                return -EINVAL;
2297
                        }
2298
                }
2299
 
2300
                if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2301
                        return -EBUSY;
2302
 
480 giacomo 2303
                //down(&fh->cap.lock);
428 giacomo 2304
                if (*on) {
2305
                        fh->ov.tvnorm = btv->tvnorm;
2306
                        new = videobuf_alloc(sizeof(*new));
2307
                        bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2308
                } else {
2309
                        new = NULL;
2310
                }
2311
 
2312
                /* switch over */
2313
                retval = bttv_switch_overlay(btv,fh,new);
480 giacomo 2314
                //up(&fh->cap.lock);
428 giacomo 2315
                return retval;
2316
        }
2317
 
2318
        case VIDIOCGMBUF:
2319
        {
2320
                struct video_mbuf *mbuf = arg;
2321
                unsigned int i;
2322
 
480 giacomo 2323
                //down(&fh->cap.lock);
428 giacomo 2324
                retval = videobuf_mmap_setup(file,&fh->cap,gbuffers,gbufsize,
2325
                                             V4L2_MEMORY_MMAP);
2326
                if (retval < 0)
2327
                        goto fh_unlock_and_return;
2328
                memset(mbuf,0,sizeof(*mbuf));
2329
                mbuf->frames = gbuffers;
2330
                mbuf->size   = gbuffers * gbufsize;
2331
                for (i = 0; i < gbuffers; i++)
2332
                        mbuf->offsets[i] = i * gbufsize;
480 giacomo 2333
                //up(&fh->cap.lock);
170 giacomo 2334
                return 0;
2335
        }
428 giacomo 2336
        case VIDIOCMCAPTURE:
170 giacomo 2337
        {
428 giacomo 2338
                struct video_mmap *vm = arg;
2339
                struct bttv_buffer *buf;
2340
                enum v4l2_field field;
2341
 
2342
                if (vm->frame >= VIDEO_MAX_FRAME)
170 giacomo 2343
                        return -EINVAL;
428 giacomo 2344
 
480 giacomo 2345
                //down(&fh->cap.lock);
428 giacomo 2346
                retval = -EINVAL;
2347
                buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
2348
                if (NULL == buf)
2349
                        goto fh_unlock_and_return;
606 giacomo 2350
 
428 giacomo 2351
                if (0 == buf->vb.baddr)
2352
                        goto fh_unlock_and_return;
541 giacomo 2353
 
428 giacomo 2354
                if (buf->vb.state == STATE_QUEUED ||
2355
                    buf->vb.state == STATE_ACTIVE)
2356
                        goto fh_unlock_and_return;
2357
 
2358
                field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
2359
                        ? V4L2_FIELD_INTERLACED
2360
                        : V4L2_FIELD_BOTTOM;
2361
                retval = bttv_prepare_buffer(btv,buf,
2362
                                             format_by_palette(vm->format),
2363
                                             vm->width,vm->height,field);
2364
                if (0 != retval)
2365
                        goto fh_unlock_and_return;
2366
                spin_lock_irqsave(&btv->s_lock,flags);
2367
                buffer_queue(file,&buf->vb);
2368
                spin_unlock_irqrestore(&btv->s_lock,flags);
480 giacomo 2369
                //up(&fh->cap.lock);
170 giacomo 2370
                return 0;
2371
        }
428 giacomo 2372
        case VIDIOCSYNC:
170 giacomo 2373
        {
428 giacomo 2374
                int *frame = arg;
2375
                struct bttv_buffer *buf;
170 giacomo 2376
 
428 giacomo 2377
                if (*frame >= VIDEO_MAX_FRAME)
170 giacomo 2378
                        return -EINVAL;
2379
 
480 giacomo 2380
                //down(&fh->cap.lock);
428 giacomo 2381
                retval = -EINVAL;
2382
                buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
2383
                if (NULL == buf)
2384
                        goto fh_unlock_and_return;
2385
                retval = videobuf_waiton(&buf->vb,0,1);
2386
                if (0 != retval)
2387
                        goto fh_unlock_and_return;
2388
                switch (buf->vb.state) {
2389
                case STATE_ERROR:
2390
                        retval = -EIO;
2391
                        /* fall through */
2392
                case STATE_DONE:
516 giacomo 2393
                        //videobuf_dma_pci_sync(btv->dev,&buf->vb.dma);
428 giacomo 2394
                        bttv_dma_free(btv,buf);
2395
                        break;
2396
                default:
2397
                        retval = -EINVAL;
2398
                        break;
170 giacomo 2399
                }
480 giacomo 2400
                //up(&fh->cap.lock);
428 giacomo 2401
                return retval;
170 giacomo 2402
        }
428 giacomo 2403
 
2404
        case VIDIOCGVBIFMT:
170 giacomo 2405
        {
428 giacomo 2406
                struct vbi_format *fmt = (void *) arg;
2407
                struct v4l2_format fmt2;
2408
 
2409
                if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
2410
                        retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2411
                        if (0 != retval)
2412
                                return retval;
2413
                }
516 giacomo 2414
                //bttv_vbi_get_fmt(fh, &fmt2);
428 giacomo 2415
 
2416
                memset(fmt,0,sizeof(*fmt));
2417
                fmt->sampling_rate    = fmt2.fmt.vbi.sampling_rate;
2418
                fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
2419
                fmt->sample_format    = VIDEO_PALETTE_RAW;
2420
                fmt->start[0]         = fmt2.fmt.vbi.start[0];
2421
                fmt->count[0]         = fmt2.fmt.vbi.count[0];
2422
                fmt->start[1]         = fmt2.fmt.vbi.start[1];
2423
                fmt->count[1]         = fmt2.fmt.vbi.count[1];
2424
                if (fmt2.fmt.vbi.flags & VBI_UNSYNC)
2425
                        fmt->flags   |= V4L2_VBI_UNSYNC;
2426
                if (fmt2.fmt.vbi.flags & VBI_INTERLACED)
2427
                        fmt->flags   |= V4L2_VBI_INTERLACED;
170 giacomo 2428
                return 0;
2429
        }
428 giacomo 2430
        case VIDIOCSVBIFMT:
170 giacomo 2431
        {
428 giacomo 2432
                struct vbi_format *fmt = (void *) arg;
2433
                struct v4l2_format fmt2;
2434
 
2435
                retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2436
                if (0 != retval)
2437
                        return retval;
516 giacomo 2438
                //bttv_vbi_get_fmt(fh, &fmt2);
428 giacomo 2439
 
2440
                if (fmt->sampling_rate    != fmt2.fmt.vbi.sampling_rate     ||
2441
                    fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line  ||
2442
                    fmt->sample_format    != VIDEO_PALETTE_RAW              ||
2443
                    fmt->start[0]         != fmt2.fmt.vbi.start[0]          ||
2444
                    fmt->start[1]         != fmt2.fmt.vbi.start[1]          ||
2445
                    fmt->count[0]         != fmt->count[1]                  ||
2446
                    fmt->count[0]         <  1                              ||
2447
                    fmt->count[0]         >  32 /* VBI_MAXLINES */)
170 giacomo 2448
                        return -EINVAL;
428 giacomo 2449
 
516 giacomo 2450
                //bttv_vbi_setlines(fh,btv,fmt->count[0]);
170 giacomo 2451
                return 0;
2452
        }
428 giacomo 2453
 
2454
        case BTTV_VERSION:
2455
        case VIDIOCGFREQ:
2456
        case VIDIOCSFREQ:
2457
        case VIDIOCGTUNER:
2458
        case VIDIOCSTUNER:
2459
        case VIDIOCGCHAN:
2460
        case VIDIOCSCHAN:
2461
        case VIDIOCGAUDIO:
2462
        case VIDIOCSAUDIO:
2463
                return bttv_common_ioctls(btv,cmd,arg);
2464
 
2465
        /* ***  v4l2  *** ************************************************ */
2466
        case VIDIOC_QUERYCAP:
170 giacomo 2467
        {
428 giacomo 2468
                struct v4l2_capability *cap = arg;
2469
 
2470
                if (0 == v4l2)
2471
                        return -EINVAL;
2472
                strcpy(cap->driver,"bttv");
516 giacomo 2473
                strncpy(cap->card,btv->video_dev->name,sizeof(cap->card));
463 giacomo 2474
                sprintf26(cap->bus_info,"PCI:%s",pci_name(btv->dev));
428 giacomo 2475
                cap->version = BTTV_VERSION_CODE;
2476
                cap->capabilities =
2477
                        V4L2_CAP_VIDEO_CAPTURE |
2478
                        V4L2_CAP_VIDEO_OVERLAY |
2479
                        V4L2_CAP_VBI_CAPTURE |
2480
                        V4L2_CAP_TUNER |
2481
                        V4L2_CAP_READWRITE |
2482
                        V4L2_CAP_STREAMING;
170 giacomo 2483
                return 0;
2484
        }
428 giacomo 2485
 
2486
        case VIDIOC_ENUM_FMT:
170 giacomo 2487
        {
428 giacomo 2488
                struct v4l2_fmtdesc *f = arg;
2489
                enum v4l2_buf_type type;
2490
                unsigned int i;
2491
                int index;
2492
 
2493
                type  = f->type;
2494
                if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2495
                        /* vbi */
2496
                        index = f->index;
2497
                        if (0 != index)
2498
                                return -EINVAL;
2499
                        memset(f,0,sizeof(*f));
2500
                        f->index       = index;
2501
                        f->type        = type;
2502
                        f->pixelformat = V4L2_PIX_FMT_GREY;
2503
                        strcpy(f->description,"vbi data");
2504
                        return 0;
2505
                }
2506
 
2507
                /* video capture + overlay */
2508
                index = -1;
2509
                for (i = 0; i < BTTV_FORMATS; i++) {
2510
                        if (bttv_formats[i].fourcc != -1)
2511
                                index++;
2512
                        if ((unsigned int)index == f->index)
2513
                                break;
2514
                }
2515
                if (BTTV_FORMATS == i)
170 giacomo 2516
                        return -EINVAL;
2517
 
428 giacomo 2518
                switch (f->type) {
2519
                case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2520
                        break;
2521
                case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2522
                        if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2523
                                return -EINVAL;
2524
                        break;
2525
                default:
2526
                        return -EINVAL;
170 giacomo 2527
                }
428 giacomo 2528
                memset(f,0,sizeof(*f));
2529
                f->index       = index;
2530
                f->type        = type;
2531
                f->pixelformat = bttv_formats[i].fourcc;
516 giacomo 2532
                strncpy(f->description,bttv_formats[i].name,sizeof(f->description));
428 giacomo 2533
                return 0;
170 giacomo 2534
        }
428 giacomo 2535
 
2536
        case VIDIOC_TRY_FMT:
170 giacomo 2537
        {
428 giacomo 2538
                struct v4l2_format *f = arg;
2539
                return bttv_try_fmt(fh,btv,f);
170 giacomo 2540
        }
428 giacomo 2541
        case VIDIOC_G_FMT:
170 giacomo 2542
        {
428 giacomo 2543
                struct v4l2_format *f = arg;
2544
                return bttv_g_fmt(fh,f);
170 giacomo 2545
        }
428 giacomo 2546
        case VIDIOC_S_FMT:
170 giacomo 2547
        {
428 giacomo 2548
                struct v4l2_format *f = arg;
2549
                return bttv_s_fmt(fh,btv,f);
2550
        }
2551
 
2552
        case VIDIOC_G_FBUF:
2553
        {
2554
                struct v4l2_framebuffer *fb = arg;
2555
 
2556
                *fb = btv->fbuf;
2557
                fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2558
                if (fh->ovfmt)
2559
                        fb->fmt.pixelformat  = fh->ovfmt->fourcc;
170 giacomo 2560
                return 0;
2561
        }
428 giacomo 2562
        case VIDIOC_S_FBUF:
170 giacomo 2563
        {
428 giacomo 2564
                struct v4l2_framebuffer *fb = arg;
2565
                const struct bttv_format *fmt;
170 giacomo 2566
 
428 giacomo 2567
                /* check args */
2568
                fmt = format_by_fourcc(fb->fmt.pixelformat);
2569
                if (NULL == fmt)
2570
                        return -EINVAL;
2571
                if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2572
                        return -EINVAL;
2573
 
480 giacomo 2574
                //down(&fh->cap.lock);
428 giacomo 2575
                retval = -EINVAL;
2576
                if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2577
                        if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
2578
                                goto fh_unlock_and_return;
2579
                        if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
2580
                                goto fh_unlock_and_return;
2581
                }
2582
 
2583
                /* ok, accept it */
2584
                btv->fbuf.base       = fb->base;
2585
                btv->fbuf.fmt.width  = fb->fmt.width;
2586
                btv->fbuf.fmt.height = fb->fmt.height;
2587
                if (0 != fb->fmt.bytesperline)
2588
                        btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2589
                else
2590
                        btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2591
 
2592
                retval = 0;
2593
                fh->ovfmt = fmt;
2594
                btv->init.ovfmt = fmt;
2595
                if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2596
                        fh->ov.w.left   = 0;
2597
                        fh->ov.w.top    = 0;
2598
                        fh->ov.w.width  = fb->fmt.width;
2599
                        fh->ov.w.height = fb->fmt.height;
2600
                        btv->init.ov.w.width  = fb->fmt.width;
2601
                        btv->init.ov.w.height = fb->fmt.height;
2602
                        if (fh->ov.clips)
2603
                                kfree(fh->ov.clips);
2604
                        fh->ov.clips = NULL;
2605
                        fh->ov.nclips = 0;
2606
 
2607
                        if (check_btres(fh, RESOURCE_OVERLAY)) {
2608
                                struct bttv_buffer *new;
2609
 
2610
                                new = videobuf_alloc(sizeof(*new));
2611
                                bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2612
                                retval = bttv_switch_overlay(btv,fh,new);
2613
                        }
2614
                }
480 giacomo 2615
                //up(&fh->cap.lock);
428 giacomo 2616
                return retval;
170 giacomo 2617
        }
2618
 
428 giacomo 2619
        case VIDIOC_REQBUFS:
2620
                return videobuf_reqbufs(file,bttv_queue(fh),arg);
2621
 
2622
        case VIDIOC_QUERYBUF:
2623
                return videobuf_querybuf(bttv_queue(fh),arg);
2624
 
2625
        case VIDIOC_QBUF:
2626
                return videobuf_qbuf(file,bttv_queue(fh),arg);
2627
 
2628
        case VIDIOC_DQBUF:
2629
                return videobuf_dqbuf(file,bttv_queue(fh),arg);
2630
 
2631
        case VIDIOC_STREAMON:
170 giacomo 2632
        {
428 giacomo 2633
                int res = bttv_resource(fh);
2634
 
2635
                if (!check_alloc_btres(btv,fh,res))
2636
                        return -EBUSY;
2637
                return videobuf_streamon(file,bttv_queue(fh));
170 giacomo 2638
        }
428 giacomo 2639
        case VIDIOC_STREAMOFF:
170 giacomo 2640
        {
428 giacomo 2641
                int res = bttv_resource(fh);
2642
 
2643
                retval = videobuf_streamoff(file,bttv_queue(fh));
2644
                if (retval < 0)
2645
                        return retval;
2646
                free_btres(btv,fh,res);
170 giacomo 2647
                return 0;
2648
        }
428 giacomo 2649
 
2650
        case VIDIOC_QUERYCTRL:
170 giacomo 2651
        {
428 giacomo 2652
                struct v4l2_queryctrl *c = arg;
2653
                int i;
2654
 
2655
                if ((c->id <  V4L2_CID_BASE ||
2656
                     c->id >= V4L2_CID_LASTP1) &&
2657
                    (c->id <  V4L2_CID_PRIVATE_BASE ||
2658
                     c->id >= V4L2_CID_PRIVATE_LASTP1))
2659
                        return -EINVAL;
2660
                for (i = 0; i < BTTV_CTLS; i++)
2661
                        if (bttv_ctls[i].id == c->id)
2662
                                break;
2663
                if (i == BTTV_CTLS) {
2664
                        *c = no_ctl;
2665
                        return 0;
2666
                }
2667
                *c = bttv_ctls[i];
2668
                if (i >= 4 && i <= 8) {
2669
                        struct video_audio va;
2670
                        memset(&va,0,sizeof(va));
2671
                        bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
2672
                        if (btv->audio_hook)
2673
                                btv->audio_hook(btv,&va,0);
2674
                        switch (bttv_ctls[i].id) {
2675
                        case V4L2_CID_AUDIO_VOLUME:
2676
                                if (!(va.flags & VIDEO_AUDIO_VOLUME))
2677
                                        *c = no_ctl;
2678
                                break;
2679
                        case V4L2_CID_AUDIO_BALANCE:
2680
                                if (!(va.flags & VIDEO_AUDIO_BALANCE))
2681
                                        *c = no_ctl;
2682
                                break;
2683
                        case V4L2_CID_AUDIO_BASS:
2684
                                if (!(va.flags & VIDEO_AUDIO_BASS))
2685
                                        *c = no_ctl;
2686
                                break;
2687
                        case V4L2_CID_AUDIO_TREBLE:
2688
                                if (!(va.flags & VIDEO_AUDIO_TREBLE))
2689
                                        *c = no_ctl;
2690
                                break;
2691
                        }
2692
                }
170 giacomo 2693
                return 0;
2694
        }
428 giacomo 2695
        case VIDIOC_G_CTRL:
2696
                return get_control(btv,arg);
2697
        case VIDIOC_S_CTRL:
2698
                return set_control(btv,arg);
2699
        case VIDIOC_G_PARM:
170 giacomo 2700
        {
428 giacomo 2701
                struct v4l2_streamparm *parm = arg;
2702
                struct v4l2_standard s;
2703
                if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2704
                        return -EINVAL;
2705
                memset(parm,0,sizeof(*parm));
516 giacomo 2706
                //v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2707
                //                       bttv_tvnorms[btv->tvnorm].name);
428 giacomo 2708
                parm->parm.capture.timeperframe = s.frameperiod;
170 giacomo 2709
                return 0;
2710
        }
2711
 
428 giacomo 2712
#ifdef VIDIOC_G_PRIORITY
2713
        case VIDIOC_G_PRIORITY:
170 giacomo 2714
        {
428 giacomo 2715
                enum v4l2_priority *p = arg;
170 giacomo 2716
 
428 giacomo 2717
                *p = v4l2_prio_max(&btv->prio);
170 giacomo 2718
                return 0;
2719
        }
428 giacomo 2720
        case VIDIOC_S_PRIORITY:
170 giacomo 2721
        {
428 giacomo 2722
                enum v4l2_priority *prio = arg;
2723
 
2724
                return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
170 giacomo 2725
        }
428 giacomo 2726
#endif
170 giacomo 2727
 
428 giacomo 2728
 
2729
        case VIDIOC_ENUMSTD:
2730
        case VIDIOC_G_STD:
2731
        case VIDIOC_S_STD:
2732
        case VIDIOC_ENUMINPUT:
2733
        case VIDIOC_G_INPUT:
2734
        case VIDIOC_S_INPUT:
2735
        case VIDIOC_G_TUNER:
2736
        case VIDIOC_S_TUNER:
2737
        case VIDIOC_G_FREQUENCY:
2738
        case VIDIOC_S_FREQUENCY:
2739
                return bttv_common_ioctls(btv,cmd,arg);
2740
 
170 giacomo 2741
        default:
428 giacomo 2742
                return -ENOIOCTLCMD;
170 giacomo 2743
        }
2744
        return 0;
428 giacomo 2745
 
2746
 fh_unlock_and_return:
480 giacomo 2747
        //up(&fh->cap.lock);
428 giacomo 2748
        return retval;
170 giacomo 2749
}
2750
 
516 giacomo 2751
int bttv_ioctl(struct inode *inode, struct file *file,
428 giacomo 2752
                      unsigned int cmd, unsigned long arg)
170 giacomo 2753
{
428 giacomo 2754
        struct bttv_fh *fh  = file->private_data;
2755
 
2756
        switch (cmd) {
2757
        case BTTV_VBISIZE:
2758
                bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2759
                return fh->lines * 2 * 2048;
2760
        default:
541 giacomo 2761
                return bttv_do_ioctl(inode, file, cmd, (void *)arg);
2762
                        //video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
170 giacomo 2763
        }
428 giacomo 2764
}
170 giacomo 2765
 
516 giacomo 2766
ssize_t bttv_read(struct file *file, char *data,
428 giacomo 2767
                         size_t count, loff_t *ppos)
2768
{
2769
        struct bttv_fh *fh = file->private_data;
2770
        int retval = 0;
170 giacomo 2771
 
428 giacomo 2772
        if (fh->btv->errors)
2773
                bttv_reinit_bt848(fh->btv);
516 giacomo 2774
        //dprintk("bttv%d: read count=%d type=%s\n",
2775
        //      fh->btv->nr,(int)count,v4l2_type_names[fh->type]);
170 giacomo 2776
 
428 giacomo 2777
        switch (fh->type) {
2778
        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2779
                if (locked_btres(fh->btv,RESOURCE_VIDEO))
2780
                        return -EBUSY;
2781
                retval = videobuf_read_one(file, &fh->cap, data, count, ppos);
2782
                break;
2783
        case V4L2_BUF_TYPE_VBI_CAPTURE:
2784
                if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2785
                        return -EBUSY;
2786
                retval = videobuf_read_stream(file, &fh->vbi, data, count, ppos, 1);
2787
                break;
2788
        default:
2789
                BUG();
170 giacomo 2790
        }
428 giacomo 2791
        return retval;
2792
}
170 giacomo 2793
 
491 giacomo 2794
/*
428 giacomo 2795
static unsigned int bttv_poll(struct file *file, poll_table *wait)
2796
{
2797
        struct bttv_fh *fh = file->private_data;
2798
        struct bttv_buffer *buf;
2799
        enum v4l2_field field;
2800
 
2801
        if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2802
                if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2803
                        return -EBUSY;
2804
                return videobuf_poll_stream(file, &fh->vbi, wait);
2805
        }
2806
 
2807
        if (check_btres(fh,RESOURCE_VIDEO)) {
2808
                if (list_empty(&fh->cap.stream))
2809
                        return POLLERR;
2810
                buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
170 giacomo 2811
        } else {
480 giacomo 2812
                //down(&fh->cap.lock);
428 giacomo 2813
                if (NULL == fh->cap.read_buf) {
2814
                        if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
480 giacomo 2815
                                //up(&fh->cap.lock);
428 giacomo 2816
                                return POLLERR;
2817
                        }
2818
                        fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
2819
                        if (NULL == fh->cap.read_buf) {
480 giacomo 2820
                                //up(&fh->cap.lock);
428 giacomo 2821
                                return POLLERR;
2822
                        }
2823
                        fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2824
                        field = videobuf_next_field(&fh->cap);
2825
                        if (0 != fh->cap.ops->buf_prepare(file,fh->cap.read_buf,field)) {
480 giacomo 2826
                                //up(&fh->cap.lock);
428 giacomo 2827
                                return POLLERR;
2828
                        }
2829
                        fh->cap.ops->buf_queue(file,fh->cap.read_buf);
2830
                        fh->cap.read_off = 0;
2831
                }
480 giacomo 2832
                //up(&fh->cap.lock);
428 giacomo 2833
                buf = (struct bttv_buffer*)fh->cap.read_buf;
170 giacomo 2834
        }
428 giacomo 2835
 
491 giacomo 2836
        //poll_wait(file, &buf->vb.done, wait);
428 giacomo 2837
        if (buf->vb.state == STATE_DONE ||
2838
            buf->vb.state == STATE_ERROR)
2839
                return POLLIN|POLLRDNORM;
2840
        return 0;
2841
}
491 giacomo 2842
*/
170 giacomo 2843
 
516 giacomo 2844
int bttv_open(struct inode *inode, struct file *file)
428 giacomo 2845
{
2846
        int minor = iminor(inode);
2847
        struct bttv *btv = NULL;
2848
        struct bttv_fh *fh;
2849
        enum v4l2_buf_type type = 0;
2850
        unsigned int i;
170 giacomo 2851
 
428 giacomo 2852
        dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
170 giacomo 2853
 
428 giacomo 2854
        for (i = 0; i < bttv_num; i++) {
2855
                if (bttvs[i].video_dev->minor == minor) {
2856
                        btv = &bttvs[i];
2857
                        type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2858
                        break;
2859
                }
2860
                if (bttvs[i].vbi_dev->minor == minor) {
2861
                        btv = &bttvs[i];
2862
                        type = V4L2_BUF_TYPE_VBI_CAPTURE;
2863
                        break;
2864
                }
170 giacomo 2865
        }
428 giacomo 2866
        if (NULL == btv)
2867
                return -ENODEV;
170 giacomo 2868
 
516 giacomo 2869
        //dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
2870
        //      btv->nr,v4l2_type_names[type]);
428 giacomo 2871
 
2872
        /* allocate per filehandle data */
2873
        fh = kmalloc(sizeof(*fh),GFP_KERNEL);
2874
        if (NULL == fh)
2875
                return -ENOMEM;
2876
        file->private_data = fh;
2877
        *fh = btv->init;
2878
        fh->type = type;
2879
        fh->ov.setup_ok = 0;
2880
#ifdef VIDIOC_G_PRIORITY
2881
        v4l2_prio_open(&btv->prio,&fh->prio);
2882
#endif
516 giacomo 2883
 
428 giacomo 2884
        videobuf_queue_init(&fh->cap, &bttv_video_qops,
2885
                            btv->dev, &btv->s_lock,
2886
                            V4L2_BUF_TYPE_VIDEO_CAPTURE,
2887
                            V4L2_FIELD_INTERLACED,
2888
                            sizeof(struct bttv_buffer));
516 giacomo 2889
        /*
428 giacomo 2890
        videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
2891
                            btv->dev, &btv->s_lock,
2892
                            V4L2_BUF_TYPE_VBI_CAPTURE,
2893
                            V4L2_FIELD_SEQ_TB,
2894
                            sizeof(struct bttv_buffer));
516 giacomo 2895
        */
428 giacomo 2896
        i2c_vidiocschan(btv);
2897
 
2898
        btv->users++;
516 giacomo 2899
        //if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
2900
                //bttv_vbi_setlines(fh,btv,16);
621 giacomo 2901
        //bttv_field_count(btv);
428 giacomo 2902
        return 0;
2903
}
2904
 
516 giacomo 2905
int bttv_release(struct inode *inode, struct file *file)
428 giacomo 2906
{
2907
        struct bttv_fh *fh = file->private_data;
2908
        struct bttv *btv = fh->btv;
2909
 
2910
        /* turn off overlay */
2911
        if (check_btres(fh, RESOURCE_OVERLAY))
2912
                bttv_switch_overlay(btv,fh,NULL);
2913
 
2914
        /* stop video capture */
2915
        if (check_btres(fh, RESOURCE_VIDEO)) {
2916
                videobuf_streamoff(file,&fh->cap);
2917
                free_btres(btv,fh,RESOURCE_VIDEO);
170 giacomo 2918
        }
428 giacomo 2919
        if (fh->cap.read_buf) {
2920
                buffer_release(file,fh->cap.read_buf);
2921
                kfree(fh->cap.read_buf);
2922
        }
170 giacomo 2923
 
428 giacomo 2924
        /* stop vbi capture */
2925
        if (check_btres(fh, RESOURCE_VBI)) {
2926
                if (fh->vbi.streaming)
2927
                        videobuf_streamoff(file,&fh->vbi);
2928
                if (fh->vbi.reading)
2929
                        videobuf_read_stop(file,&fh->vbi);
2930
                free_btres(btv,fh,RESOURCE_VBI);
170 giacomo 2931
        }
2932
 
428 giacomo 2933
#ifdef VIDIOC_G_PRIORITY
2934
        v4l2_prio_close(&btv->prio,&fh->prio);
2935
#endif
2936
        file->private_data = NULL;
2937
        kfree(fh);
2938
 
2939
        btv->users--;
621 giacomo 2940
        //bttv_field_count(btv);
428 giacomo 2941
        return 0;
170 giacomo 2942
}
2943
 
516 giacomo 2944
int
428 giacomo 2945
bttv_mmap(struct file *file, struct vm_area_struct *vma)
170 giacomo 2946
{
428 giacomo 2947
        struct bttv_fh *fh = file->private_data;
170 giacomo 2948
 
516 giacomo 2949
        //dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
2950
        //      fh->btv->nr, v4l2_type_names[fh->type],
2951
        //      vma->vm_start, vma->vm_end - vma->vm_start);
428 giacomo 2952
        return videobuf_mmap_mapper(vma,bttv_queue(fh));
2953
}
541 giacomo 2954
 
428 giacomo 2955
static struct file_operations bttv_fops =
2956
{
2957
        .owner    = THIS_MODULE,
2958
        .open     = bttv_open,
2959
        .release  = bttv_release,
2960
        .ioctl    = bttv_ioctl,
2961
        .llseek   = no_llseek,
2962
        .read     = bttv_read,
2963
        .mmap     = bttv_mmap,
2964
};
541 giacomo 2965
 
2966
 
428 giacomo 2967
static struct video_device bttv_video_template =
2968
{
2969
        .name     = "UNSET",
2970
        .type     = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_OVERLAY|
2971
                    VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2972
        .hardware = VID_HARDWARE_BT848,
2973
        .fops     = &bttv_fops,
2974
        .minor    = -1,
2975
};
541 giacomo 2976
/*
428 giacomo 2977
struct video_device bttv_vbi_template =
2978
{
2979
        .name     = "bt848/878 vbi",
2980
        .type     = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
2981
        .hardware = VID_HARDWARE_BT848,
2982
        .fops     = &bttv_fops,
2983
        .minor    = -1,
2984
};
516 giacomo 2985
*/
428 giacomo 2986
/* ----------------------------------------------------------------------- */
2987
/* radio interface                                                         */
170 giacomo 2988
 
516 giacomo 2989
int radio_open(struct inode *inode, struct file *file)
428 giacomo 2990
{
2991
        int minor = iminor(inode);
2992
        struct bttv *btv = NULL;
2993
        unsigned int i;
170 giacomo 2994
 
428 giacomo 2995
        dprintk("bttv: open minor=%d\n",minor);
170 giacomo 2996
 
428 giacomo 2997
        for (i = 0; i < bttv_num; i++) {
2998
                if (bttvs[i].radio_dev->minor == minor) {
2999
                        btv = &bttvs[i];
3000
                        break;
3001
                }
3002
        }
3003
        if (NULL == btv)
3004
                return -ENODEV;
170 giacomo 3005
 
428 giacomo 3006
        dprintk("bttv%d: open called (radio)\n",btv->nr);
480 giacomo 3007
        //down(&btv->lock);
428 giacomo 3008
        if (btv->radio_user) {
480 giacomo 3009
                //up(&btv->lock);
428 giacomo 3010
                return -EBUSY;
3011
        }
3012
        btv->radio_user++;
3013
        file->private_data = btv;
170 giacomo 3014
 
428 giacomo 3015
        i2c_vidiocschan(btv);
3016
        bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
3017
        audio_mux(btv,AUDIO_RADIO);
3018
 
480 giacomo 3019
        //up(&btv->lock);
428 giacomo 3020
        return 0;
3021
}
3022
 
516 giacomo 3023
int radio_release(struct inode *inode, struct file *file)
428 giacomo 3024
{
3025
        struct bttv    *btv = file->private_data;
3026
 
3027
        btv->radio_user--;
3028
        return 0;
3029
}
3030
 
516 giacomo 3031
int radio_do_ioctl(struct inode *inode, struct file *file,
428 giacomo 3032
                          unsigned int cmd, void *arg)
3033
{
3034
        struct bttv    *btv = file->private_data;
3035
 
3036
        switch (cmd) {
3037
        case VIDIOCGCAP:
3038
        {
3039
                struct video_capability *cap = arg;
3040
 
3041
                memset(cap,0,sizeof(*cap));
3042
                strcpy(cap->name,btv->radio_dev->name);
3043
                cap->type = VID_TYPE_TUNER;
3044
                cap->channels = 1;
3045
                cap->audios = 1;
3046
                return 0;
170 giacomo 3047
        }
428 giacomo 3048
 
3049
        case VIDIOCGTUNER:
3050
        {
3051
                struct video_tuner *v = arg;
3052
 
3053
                if(v->tuner)
3054
                        return -EINVAL;
3055
                memset(v,0,sizeof(*v));
3056
                strcpy(v->name, "Radio");
3057
                /* japan:          76.0 MHz -  89.9 MHz
3058
                   western europe: 87.5 MHz - 108.0 MHz
3059
                   russia:         65.0 MHz - 108.0 MHz */
3060
                v->rangelow=(int)(65*16);
3061
                v->rangehigh=(int)(108*16);
3062
                bttv_call_i2c_clients(btv,cmd,v);
3063
                return 0;
3064
        }
3065
        case VIDIOCSTUNER:
3066
                /* nothing to do */
3067
                return 0;
170 giacomo 3068
 
428 giacomo 3069
        case BTTV_VERSION:
3070
        case VIDIOCGFREQ:
3071
        case VIDIOCSFREQ:
3072
        case VIDIOCGAUDIO:
3073
        case VIDIOCSAUDIO:
3074
                return bttv_common_ioctls(btv,cmd,arg);
170 giacomo 3075
 
428 giacomo 3076
        default:
3077
                return -ENOIOCTLCMD;
3078
        }
3079
        return 0;
3080
}
170 giacomo 3081
 
516 giacomo 3082
int radio_ioctl(struct inode *inode, struct file *file,
428 giacomo 3083
                       unsigned int cmd, unsigned long arg)
3084
{
516 giacomo 3085
        return 0;//video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
428 giacomo 3086
}
541 giacomo 3087
 
428 giacomo 3088
static struct file_operations radio_fops =
3089
{
3090
        .owner    = THIS_MODULE,
3091
        .open     = radio_open,
3092
        .release  = radio_release,
3093
        .ioctl    = radio_ioctl,
3094
        .llseek   = no_llseek,
3095
};
541 giacomo 3096
 
3097
 
428 giacomo 3098
static struct video_device radio_template =
3099
{
3100
        .name     = "bt848/878 radio",
3101
        .type     = VID_TYPE_TUNER,
3102
        .hardware = VID_HARDWARE_BT848,
3103
        .fops     = &radio_fops,
3104
        .minor    = -1,
3105
};
541 giacomo 3106
 
428 giacomo 3107
/* ----------------------------------------------------------------------- */
3108
/* irq handler                                                             */
170 giacomo 3109
 
428 giacomo 3110
static char *irq_name[] = { "FMTCHG", "VSYNC", "HSYNC", "OFLOW", "HLOCK",
3111
                            "VPRES", "6", "7", "I2CDONE", "GPINT", "10",
3112
                            "RISCI", "FBUS", "FTRGT", "FDSR", "PPERR",
3113
                            "RIPERR", "PABORT", "OCERR", "SCERR" };
170 giacomo 3114
 
428 giacomo 3115
static void bttv_print_irqbits(u32 print, u32 mark)
3116
{
3117
        unsigned int i;
3118
 
3119
        printk("bits:");
3120
        for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3121
                if (print & (1 << i))
3122
                        printk(" %s",irq_name[i]);
3123
                if (mark & (1 << i))
3124
                        printk("*");
170 giacomo 3125
        }
428 giacomo 3126
}
170 giacomo 3127
 
428 giacomo 3128
static void bttv_print_riscaddr(struct bttv *btv)
3129
{
3130
        printk("  main: %08Lx\n",
3131
               (unsigned long long)btv->main.dma);
3132
        printk("  vbi : o=%08Lx e=%08Lx\n",
3133
               btv->curr.vbi ? (unsigned long long)btv->curr.vbi->top.dma : 0,
3134
               btv->curr.vbi ? (unsigned long long)btv->curr.vbi->bottom.dma : 0);
3135
        printk("  cap : o=%08Lx e=%08Lx\n",
3136
               btv->curr.top    ? (unsigned long long)btv->curr.top->top.dma : 0,
3137
               btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3138
        printk("  scr : o=%08Lx e=%08Lx\n",
3139
               btv->screen ? (unsigned long long)btv->screen->top.dma  : 0,
3140
               btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3141
}
170 giacomo 3142
 
428 giacomo 3143
static int
3144
bttv_irq_next_set(struct bttv *btv, struct bttv_buffer_set *set)
3145
{
3146
        struct bttv_buffer *item;
170 giacomo 3147
 
428 giacomo 3148
        memset(set,0,sizeof(*set));
170 giacomo 3149
 
428 giacomo 3150
        if (!list_empty(&btv->vcapture)) {
3151
                set->irqflags = 1;
3152
                set->vbi = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3153
        }
170 giacomo 3154
 
428 giacomo 3155
        if (!list_empty(&btv->capture)) {
3156
                set->irqflags = 1;
3157
                item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3158
                if (V4L2_FIELD_HAS_TOP(item->vb.field))
3159
                        set->top    = item;
3160
                if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3161
                        set->bottom = item;
170 giacomo 3162
 
428 giacomo 3163
                if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3164
                    (item->vb.queue.next != &btv->capture)) {
3165
                        item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3166
                        if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3167
                                if (NULL == set->top &&
3168
                                    V4L2_FIELD_TOP == item->vb.field) {
3169
                                        set->top = item;
3170
                                }
3171
                                if (NULL == set->bottom &&
3172
                                    V4L2_FIELD_BOTTOM == item->vb.field) {
3173
                                        set->bottom = item;
3174
                                }
3175
                                if (NULL != set->top  &&  NULL != set->bottom)
3176
                                        set->topirq = 2;
3177
                        }
3178
                }
3179
        }
170 giacomo 3180
 
428 giacomo 3181
        if (NULL != btv->screen) {
3182
                if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3183
                        if (NULL == set->top && NULL == set->bottom) {
3184
                                set->top    = btv->screen;
3185
                                set->bottom = btv->screen;
3186
                        }
3187
                } else {
3188
                        if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3189
                            NULL == set->top) {
3190
                                set->top = btv->screen;
3191
                        }
3192
                        if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3193
                            NULL == set->bottom) {
3194
                                set->bottom = btv->screen;
3195
                        }
3196
                }
3197
        }
607 giacomo 3198
 
3199
        dprintk("bttv%d: next set: top=%x bottom=%x vbi=%x "
3200
                "[screen=%x,irq=%d,%d]\n",
3201
                btv->nr,(int)set->top, (int)set->bottom, (int)set->vbi,
3202
                (int)btv->screen,set->irqflags,set->topirq);
170 giacomo 3203
        return 0;
3204
}
3205
 
428 giacomo 3206
static void
3207
bttv_irq_wakeup_set(struct bttv *btv, struct bttv_buffer_set *wakeup,
3208
                    struct bttv_buffer_set *curr, unsigned int state)
3209
{
3210
        struct timeval ts;
170 giacomo 3211
 
516 giacomo 3212
        //do_gettimeofday(&ts);
170 giacomo 3213
 
428 giacomo 3214
        if (NULL != wakeup->vbi) {
3215
                wakeup->vbi->vb.ts = ts;
3216
                wakeup->vbi->vb.field_count = btv->field_count;
3217
                wakeup->vbi->vb.state = state;
491 giacomo 3218
                //wake_up(&wakeup->vbi->vb.done);
428 giacomo 3219
        }
3220
        if (wakeup->top == wakeup->bottom) {
3221
                if (NULL != wakeup->top && curr->top != wakeup->top) {
3222
                        if (irq_debug > 1)
607 giacomo 3223
                                printk("bttv%d: wakeup: both=%x\n",btv->nr,(int)wakeup->top);
428 giacomo 3224
                        wakeup->top->vb.ts = ts;
3225
                        wakeup->top->vb.field_count = btv->field_count;
3226
                        wakeup->top->vb.state = state;
491 giacomo 3227
                        //wake_up(&wakeup->top->vb.done);
428 giacomo 3228
                }
3229
        } else {
3230
                if (NULL != wakeup->top && curr->top != wakeup->top) {
3231
                        if (irq_debug > 1)
607 giacomo 3232
                                printk("bttv%d: wakeup: top=%x\n",btv->nr,(int)wakeup->top);
428 giacomo 3233
                        wakeup->top->vb.ts = ts;
3234
                        wakeup->top->vb.field_count = btv->field_count;
3235
                        wakeup->top->vb.state = state;
491 giacomo 3236
                        //wake_up(&wakeup->top->vb.done);
428 giacomo 3237
                }
3238
                if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3239
                        if (irq_debug > 1)
607 giacomo 3240
                                printk("bttv%d: wakeup: bottom=%x\n",btv->nr,(int)wakeup->bottom);
428 giacomo 3241
                        wakeup->bottom->vb.ts = ts;
3242
                        wakeup->bottom->vb.field_count = btv->field_count;
3243
                        wakeup->bottom->vb.state = state;
491 giacomo 3244
                        //wake_up(&wakeup->bottom->vb.done);
428 giacomo 3245
                }
3246
        }
3247
}
3248
 
3249
static void bttv_irq_timeout(unsigned long data)
170 giacomo 3250
{
428 giacomo 3251
        struct bttv *btv = (struct bttv *)data;
3252
        struct bttv_buffer_set old,new;
3253
        struct bttv_buffer *item;
3254
 
3255
        if (bttv_verbose) {
3256
                printk(KERN_INFO "bttv%d: timeout: risc=%08x, ",
3257
                       btv->nr,btread(BT848_RISC_COUNT));
3258
                bttv_print_irqbits(btread(BT848_INT_STAT),0);
3259
                printk("\n");
3260
        }
3261
 
3262
        spin_lock(&btv->s_lock);
3263
 
3264
        /* deactivate stuff */
3265
        memset(&new,0,sizeof(new));
3266
        old = btv->curr;
3267
        btv->curr = new;
3268
        bttv_buffer_set_activate(btv, &new);
3269
        bttv_set_dma(btv, 0, 0);
3270
 
3271
        /* wake up */
3272
        bttv_irq_wakeup_set(btv, &old, &new, STATE_ERROR);
3273
 
3274
        /* cancel all outstanding capture / vbi requests */
3275
        while (!list_empty(&btv->capture)) {
3276
                item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3277
                list_del(&item->vb.queue);
3278
                item->vb.state = STATE_ERROR;
491 giacomo 3279
                //wake_up(&item->vb.done);
428 giacomo 3280
        }
3281
        while (!list_empty(&btv->vcapture)) {
3282
                item = list_entry(btv->vcapture.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
 
3288
        btv->errors++;
3289
        spin_unlock(&btv->s_lock);     
3290
}
3291
 
3292
static void
3293
bttv_irq_wakeup_top(struct bttv *btv)
3294
{
3295
        struct bttv_buffer *wakeup = btv->curr.top;
3296
 
3297
        if (NULL == wakeup)
3298
                return;
3299
 
3300
        spin_lock(&btv->s_lock);
3301
        btv->curr.topirq = 0;
3302
        btv->curr.top = NULL;
3303
        bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3304
 
516 giacomo 3305
        //do_gettimeofday(&wakeup->vb.ts);
428 giacomo 3306
        wakeup->vb.field_count = btv->field_count;
3307
        wakeup->vb.state = STATE_DONE;
491 giacomo 3308
        //wake_up(&wakeup->vb.done);
428 giacomo 3309
        spin_unlock(&btv->s_lock);
3310
}
3311
 
3312
static void
3313
bttv_irq_switch_fields(struct bttv *btv)
3314
{
3315
        struct bttv_buffer_set new;
3316
        struct bttv_buffer_set old;
3317
        dma_addr_t rc;
3318
 
3319
        spin_lock(&btv->s_lock);
3320
 
3321
        /* new buffer set */
3322
        bttv_irq_next_set(btv, &new);
3323
        rc = btread(BT848_RISC_COUNT);
3324
        if (rc < btv->main.dma || rc > btv->main.dma + 0x100) {
3325
                if (1 /* irq_debug */)
3326
                        printk("bttv%d: skipped frame. no signal? high irq latency?\n",
3327
                               btv->nr);
3328
                spin_unlock(&btv->s_lock);
3329
                return;
3330
        }
3331
 
3332
        /* switch over */
3333
        old = btv->curr;
3334
        btv->curr = new;
3335
        bttv_buffer_set_activate(btv, &new);
3336
        bttv_set_dma(btv, 0, new.irqflags);
3337
 
3338
        /* switch input */
3339
        if (UNSET != btv->new_input) {
516 giacomo 3340
                //video_mux(btv,btv->new_input);
428 giacomo 3341
                btv->new_input = UNSET;
3342
        }
3343
 
3344
        /* wake up finished buffers */
3345
        bttv_irq_wakeup_set(btv, &old, &new, STATE_DONE);
3346
        spin_unlock(&btv->s_lock);
3347
}
3348
 
3349
static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3350
{
170 giacomo 3351
        u32 stat,astat;
3352
        u32 dstat;
3353
        int count;
428 giacomo 3354
        struct bttv *btv;
3355
        int handled = 0;
170 giacomo 3356
 
428 giacomo 3357
        btv=(struct bttv *)dev_id;
170 giacomo 3358
        count=0;
428 giacomo 3359
        while (1) {
170 giacomo 3360
                /* get/clear interrupt status bits */
3361
                stat=btread(BT848_INT_STAT);
3362
                astat=stat&btread(BT848_INT_MASK);
428 giacomo 3363
                if (!astat)
3364
                        break;
3365
                handled = 1;
170 giacomo 3366
                btwrite(stat,BT848_INT_STAT);
3367
 
3368
                /* get device status bits */
3369
                dstat=btread(BT848_DSTATUS);
3370
 
3371
                if (irq_debug) {
428 giacomo 3372
                        printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
3373
                               "riscs=%x, riscc=%08x, ",
3374
                               btv->nr, count, btv->field_count,
3375
                               stat>>28, btread(BT848_RISC_COUNT));
3376
                        bttv_print_irqbits(stat,astat);
170 giacomo 3377
                        if (stat & BT848_INT_HLOCK)
428 giacomo 3378
                                printk("   HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
170 giacomo 3379
                                       ? "yes" : "no");
3380
                        if (stat & BT848_INT_VPRES)
428 giacomo 3381
                                printk("   PRES => %s", (dstat & BT848_DSTATUS_PRES)
170 giacomo 3382
                                       ? "yes" : "no");
3383
                        if (stat & BT848_INT_FMTCHG)
428 giacomo 3384
                                printk("   NUML => %s", (dstat & BT848_DSTATUS_NUML)
170 giacomo 3385
                                       ? "625" : "525");
428 giacomo 3386
                        printk("\n");
170 giacomo 3387
                }
3388
 
3389
                if (astat&BT848_INT_VSYNC)
428 giacomo 3390
                        btv->field_count++;
170 giacomo 3391
 
428 giacomo 3392
                if (astat & BT848_INT_GPINT) {
3393
#ifdef CONFIG_VIDEO_IR
3394
                        if (btv->remote)
3395
                                bttv_input_irq(btv);
3396
#endif
491 giacomo 3397
                        //wake_up(&btv->gpioq);
170 giacomo 3398
                }
3399
 
428 giacomo 3400
                if ((astat & BT848_INT_RISCI)  &&  (stat & (2<<28)))
3401
                        bttv_irq_wakeup_top(btv);
170 giacomo 3402
 
428 giacomo 3403
                if ((astat & BT848_INT_RISCI)  &&  (stat & (1<<28)))
3404
                        bttv_irq_switch_fields(btv);
3405
 
3406
                if ((astat & BT848_INT_HLOCK)  &&  btv->opt_automute)
3407
                        audio_mux(btv, -1);
3408
 
3409
                if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3410
                        printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->nr,
3411
                               (astat & BT848_INT_SCERR) ? "SCERR" : "",
3412
                               (astat & BT848_INT_OCERR) ? "OCERR" : "",
3413
                               btread(BT848_RISC_COUNT));
3414
                        bttv_print_irqbits(stat,astat);
3415
                        printk("\n");
3416
                        if (bttv_debug)
3417
                                bttv_print_riscaddr(btv);
170 giacomo 3418
                }
428 giacomo 3419
                if (fdsr && astat & BT848_INT_FDSR) {
3420
                        printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
3421
                               btv->nr,btread(BT848_RISC_COUNT));
3422
                        if (bttv_debug)
3423
                                bttv_print_riscaddr(btv);
3424
                }
170 giacomo 3425
 
3426
                count++;
428 giacomo 3427
                if (count > 4) {
170 giacomo 3428
                        btwrite(0, BT848_INT_MASK);
428 giacomo 3429
                        printk(KERN_ERR
3430
                               "bttv%d: IRQ lockup, cleared int mask [", btv->nr);
3431
                        bttv_print_irqbits(stat,astat);
3432
                        printk("]\n");
170 giacomo 3433
                }
3434
        }
428 giacomo 3435
        return IRQ_RETVAL(handled);
3436
}
170 giacomo 3437
 
257 giacomo 3438
 
428 giacomo 3439
/* ----------------------------------------------------------------------- */
3440
/* initialitation                                                          */
541 giacomo 3441
 
428 giacomo 3442
static struct video_device *vdev_init(struct bttv *btv,
3443
                                      struct video_device *template,
3444
                                      char *type)
3445
{
3446
        struct video_device *vfd;
3447
 
3448
        vfd = video_device_alloc();
3449
        if (NULL == vfd)
3450
                return NULL;
3451
        *vfd = *template;
3452
        vfd->minor   = -1;
3453
        vfd->dev     = &btv->dev->dev;
3454
        vfd->release = video_device_release;
463 giacomo 3455
        snprintf26(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
428 giacomo 3456
                 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3457
                 type, bttv_tvcards[btv->type].name);
3458
        return vfd;
3459
}
3460
 
541 giacomo 3461
void bttv_unregister_video(struct bttv *btv)
428 giacomo 3462
{
3463
        if (btv->video_dev) {
3464
                if (-1 != btv->video_dev->minor)
3465
                        video_unregister_device(btv->video_dev);
3466
                else
3467
                        video_device_release(btv->video_dev);
3468
                btv->video_dev = NULL;
3469
        }
541 giacomo 3470
        /*
428 giacomo 3471
        if (btv->vbi_dev) {
3472
                if (-1 != btv->vbi_dev->minor)
3473
                        video_unregister_device(btv->vbi_dev);
3474
                else
3475
                        video_device_release(btv->vbi_dev);
3476
                btv->vbi_dev = NULL;
3477
        }
541 giacomo 3478
        */
428 giacomo 3479
        if (btv->radio_dev) {
3480
                if (-1 != btv->radio_dev->minor)
3481
                        video_unregister_device(btv->radio_dev);
3482
                else
3483
                        video_device_release(btv->radio_dev);
3484
                btv->radio_dev = NULL;
3485
        }
3486
}
541 giacomo 3487
 
428 giacomo 3488
/* register video4linux devices */
541 giacomo 3489
 
3490
int __devinit bttv_register_video(struct bttv *btv)
428 giacomo 3491
{
3492
        btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
3493
        if (NULL == btv->video_dev)
3494
                goto err;
3495
        if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
3496
                goto err;
3497
        printk(KERN_INFO "bttv%d: registered device video%d\n",
3498
               btv->nr,btv->video_dev->minor & 0x1f);
541 giacomo 3499
        /*
428 giacomo 3500
        video_device_create_file(btv->video_dev, &class_device_attr_card);
3501
        btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
3502
        if (NULL == btv->vbi_dev)
3503
                goto err;
3504
        if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
3505
                goto err;
3506
        printk(KERN_INFO "bttv%d: registered device vbi%d\n",
3507
               btv->nr,btv->vbi_dev->minor & 0x1f);
541 giacomo 3508
        */
428 giacomo 3509
        if (!btv->has_radio)
3510
                return 0;
3511
        btv->radio_dev = vdev_init(btv, &radio_template, "radio");
3512
        if (NULL == btv->radio_dev)
3513
                goto err;
3514
        if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
3515
                goto err;
3516
        printk(KERN_INFO "bttv%d: registered device radio%d\n",
3517
               btv->nr,btv->radio_dev->minor & 0x1f);
3518
 
3519
        return 0;
3520
 
3521
 err:
3522
        bttv_unregister_video(btv);
3523
        return -1;
3524
}
3525
 
3526
/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3527
/* response on cards with no firmware is not enabled by OF */
3528
static void pci_set_command(struct pci_dev *dev)
3529
{
3530
#if defined(__powerpc__)
3531
        unsigned int cmd;
170 giacomo 3532
 
428 giacomo 3533
        pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3534
        cmd = (cmd | PCI_COMMAND_MEMORY );
3535
        pci_write_config_dword(dev, PCI_COMMAND, cmd);
3536
#endif
170 giacomo 3537
}
3538
 
428 giacomo 3539
static int __devinit bttv_probe(struct pci_dev *dev,
3540
                                const struct pci_device_id *pci_id)
170 giacomo 3541
{
428 giacomo 3542
        int result;
170 giacomo 3543
        unsigned char lat;
428 giacomo 3544
        struct bttv *btv;
3545
 
170 giacomo 3546
        if (bttv_num == BTTV_MAX)
3547
                return -ENOMEM;
3548
        printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
428 giacomo 3549
        btv=&bttvs[bttv_num];
3550
        memset(btv,0,sizeof(*btv));
3551
        btv->nr  = bttv_num;
463 giacomo 3552
        sprintf26(btv->name,"bttv%d",btv->nr);
170 giacomo 3553
 
428 giacomo 3554
        /* initialize structs / fill in defaults */
516 giacomo 3555
        //init_MUTEX(&btv->lock);
3556
        //init_MUTEX(&btv->reslock);
428 giacomo 3557
        btv->s_lock = SPIN_LOCK_UNLOCKED;
491 giacomo 3558
        //init_waitqueue_head(&btv->gpioq);
428 giacomo 3559
        INIT_LIST_HEAD(&btv->capture);
3560
        INIT_LIST_HEAD(&btv->vcapture);
3561
#ifdef VIDIOC_G_PRIORITY
3562
        v4l2_prio_init(&btv->prio);
3563
#endif
274 giacomo 3564
 
428 giacomo 3565
        init_timer(&btv->timeout);
3566
        btv->timeout.function = bttv_irq_timeout;
3567
        btv->timeout.data     = (unsigned long)btv;
3568
 
3569
        btv->i2c_rc = -1;
3570
        btv->tuner_type  = UNSET;
3571
        btv->pinnacle_id = UNSET;
3572
        btv->new_input   = UNSET;
3573
        btv->has_radio=radio[btv->nr];
3574
 
3575
        /* pci stuff (init, get irq/mmio, ... */
3576
        btv->dev = dev;
3577
        btv->id  = dev->device;
537 giacomo 3578
 
428 giacomo 3579
        if (pci_enable_device(dev)) {
3580
                printk(KERN_WARNING "bttv%d: Can't enable device.\n",
3581
                       btv->nr);
3582
                return -EIO;
3583
        }
537 giacomo 3584
 
428 giacomo 3585
        if (pci_set_dma_mask(dev, 0xffffffff)) {
3586
                printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
3587
                       btv->nr);
3588
                return -EIO;
3589
        }
3590
        if (!request_mem_region(pci_resource_start(dev,0),
3591
                                pci_resource_len(dev,0),
3592
                                btv->name)) {
3593
                printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
3594
                       btv->nr, pci_resource_start(dev,0));
3595
                return -EBUSY;
3596
        }
537 giacomo 3597
 
428 giacomo 3598
        pci_set_master(dev);
3599
        pci_set_command(dev);
3600
        pci_set_drvdata(dev,btv);
3601
        if (!pci_dma_supported(dev,0xffffffff)) {
3602
                printk("bttv%d: Oops: no 32bit PCI DMA ???\n", btv->nr);
3603
                result = -EIO;
3604
                goto fail1;
3605
        }
170 giacomo 3606
 
428 giacomo 3607
        pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3608
        pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3609
        printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
3610
               bttv_num,btv->id, btv->revision, pci_name(dev));
3611
        printk("irq: %d, latency: %d, mmio: 0x%lx\n",
3612
               btv->dev->irq, lat, pci_resource_start(dev,0));
537 giacomo 3613
 
3614
        udelay(1000);
3615
 
428 giacomo 3616
        btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
3617
        if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
3618
                printk("bttv%d: ioremap() failed\n", btv->nr);
3619
                result = -EIO;
3620
                goto fail1;
170 giacomo 3621
        }
3622
 
428 giacomo 3623
        /* identify card */
170 giacomo 3624
        bttv_idcard(btv);
3625
 
428 giacomo 3626
        /* disable irqs, register irq handler */
170 giacomo 3627
        btwrite(0, BT848_INT_MASK);
428 giacomo 3628
        result = request_irq(btv->dev->irq, bttv_irq,
3629
                             SA_SHIRQ | SA_INTERRUPT,btv->name,(void *)btv);
3630
        if (result < 0) {
3631
                printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
3632
                       bttv_num,btv->dev->irq);
3633
                goto fail1;
3634
        }
170 giacomo 3635
 
428 giacomo 3636
        if (0 != bttv_handle_chipset(btv)) {
3637
                result = -EIO;
3638
                goto fail2;
3639
        }
3640
 
3641
        /* init options from insmod args */
3642
        btv->opt_combfilter = combfilter;
3643
        btv->opt_lumafilter = lumafilter;
3644
        btv->opt_automute   = automute;
3645
        btv->opt_chroma_agc = chroma_agc;
3646
        btv->opt_adc_crush  = adc_crush;
3647
        btv->opt_vcr_hack   = vcr_hack;
170 giacomo 3648
 
428 giacomo 3649
        /* fill struct bttv with some useful defaults */
3650
        btv->init.btv         = btv;
3651
        btv->init.ov.w.width  = 320;
3652
        btv->init.ov.w.height = 240;
3653
        btv->init.fmt         = format_by_palette(VIDEO_PALETTE_RGB24);
3654
        btv->init.width       = 320;
3655
        btv->init.height      = 240;
3656
        btv->init.lines       = 16;
3657
        btv->input = 0;
3658
 
3659
        /* initialize hardware */
3660
        if (bttv_gpio)
3661
                bttv_gpio_tracking(btv,"pre-init");
3662
 
3663
        bttv_risc_init_main(btv);
3664
        if (!bttv_tvcards[btv->type].no_video)
3665
                init_bt848(btv);
3666
 
3667
        /* gpio */
3668
        btwrite(0x00, BT848_GPIO_REG_INP);
3669
        btwrite(0x00, BT848_GPIO_OUT_EN);
3670
        if (bttv_gpio)
3671
                bttv_gpio_tracking(btv,"init");
3672
 
3673
        /* needs to be done before i2c is registered */
3674
        bttv_init_card1(btv);
3675
 
3676
        /* register i2c */
3677
        init_bttv_i2c(btv);
3678
 
3679
        /* some card-specific stuff (needs working i2c) */
3680
        bttv_init_card2(btv);
3681
 
3682
        /* register video4linux + input */
3683
        if (!bttv_tvcards[btv->type].no_video) {
541 giacomo 3684
                bttv_register_video(btv);
428 giacomo 3685
#ifdef CONFIG_VIDEO_IR
3686
                bttv_input_init(btv);
3687
#endif
3688
 
3689
                bt848_bright(btv,32768);
3690
                bt848_contrast(btv,32768);
3691
                bt848_hue(btv,32768);
3692
                bt848_sat(btv,32768);
3693
                audio_mux(btv,AUDIO_MUTE);
3694
                set_input(btv,0);
3695
        }
3696
 
3697
        /* everything is fine */
170 giacomo 3698
        bttv_num++;
3699
        return 0;
3700
 
428 giacomo 3701
 fail2:
3702
        free_irq(btv->dev->irq,btv);
3703
 
3704
 fail1:
3705
        if (btv->bt848_mmio)
3706
                iounmap(btv->bt848_mmio);
3707
        release_mem_region(pci_resource_start(btv->dev,0),
3708
                           pci_resource_len(btv->dev,0));
3709
        pci_set_drvdata(dev,NULL);
3710
        return result;
170 giacomo 3711
}
3712
 
428 giacomo 3713
static void __devexit bttv_remove(struct pci_dev *pci_dev)
170 giacomo 3714
{
428 giacomo 3715
        struct bttv *btv = pci_get_drvdata(pci_dev);
170 giacomo 3716
 
428 giacomo 3717
        if (bttv_verbose)
3718
                printk("bttv%d: unloading\n",btv->nr);
170 giacomo 3719
 
428 giacomo 3720
        /* shutdown everything (DMA+IRQs) */
3721
        btand(~15, BT848_GPIO_DMA_CTL);
3722
        btwrite(0, BT848_INT_MASK);
3723
        btwrite(~0x0, BT848_INT_STAT);
3724
        btwrite(0x0, BT848_GPIO_OUT_EN);
3725
        if (bttv_gpio)
3726
                bttv_gpio_tracking(btv,"cleanup");
170 giacomo 3727
 
428 giacomo 3728
        /* tell gpio modules we are leaving ... */
3729
        btv->shutdown=1;
491 giacomo 3730
        //wake_up(&btv->gpioq);
428 giacomo 3731
 
3732
        /* unregister i2c_bus + input */
3733
        fini_bttv_i2c(btv);
3734
#ifdef CONFIG_VIDEO_IR
3735
        bttv_input_fini(btv);
3736
#endif
3737
 
3738
        /* unregister video4linux */
516 giacomo 3739
        //bttv_unregister_video(btv);
428 giacomo 3740
 
3741
        /* free allocated memory */
3742
        btcx_riscmem_free(btv->dev,&btv->main);
3743
 
3744
        /* free ressources */
3745
        free_irq(btv->dev->irq,btv);
3746
        iounmap(btv->bt848_mmio);
3747
        release_mem_region(pci_resource_start(btv->dev,0),
3748
                           pci_resource_len(btv->dev,0));
3749
 
3750
        pci_set_drvdata(pci_dev, NULL);
3751
        return;
3752
}
3753
 
3754
static struct pci_device_id bttv_pci_tbl[] = {
3755
        {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
3756
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3757
        {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
3758
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3759
        {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
3760
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3761
        {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
3762
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3763
        {0,}
3764
};
3765
 
3766
MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
3767
 
3768
static struct pci_driver bttv_pci_driver = {
3769
        .name     = "bttv",
3770
        .id_table = bttv_pci_tbl,
3771
        .probe    = bttv_probe,
3772
        .remove   = __devexit_p(bttv_remove),
3773
};
3774
 
463 giacomo 3775
int bttv_init_module(void)
428 giacomo 3776
{
3777
        int rc;
170 giacomo 3778
        bttv_num = 0;
3779
 
428 giacomo 3780
        printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
3781
               (BTTV_VERSION_CODE >> 16) & 0xff,
3782
               (BTTV_VERSION_CODE >> 8) & 0xff,
3783
               BTTV_VERSION_CODE & 0xff);
3784
        if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
170 giacomo 3785
                gbuffers = 2;
3786
        if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
3787
                gbufsize = BTTV_MAX_FBUF;
428 giacomo 3788
        gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
170 giacomo 3789
        if (bttv_verbose)
428 giacomo 3790
                printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
3791
                       gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
170 giacomo 3792
 
428 giacomo 3793
        bttv_check_chipset();
170 giacomo 3794
 
428 giacomo 3795
        rc = pci_module_init(&bttv_pci_driver);
3796
        if (-ENODEV == rc) {
3797
                /* plenty of people trying to use bttv for the cx2388x ... */
3798
                if (NULL != pci_find_device(0x14f1, 0x8800, NULL))
3799
                        printk("bttv doesn't support your Conexant 2388x card.\n");
3800
        }
3801
        return rc;
3802
}
170 giacomo 3803
 
618 giacomo 3804
void bttv_cleanup_module(void)
428 giacomo 3805
{
3806
        pci_unregister_driver(&bttv_pci_driver);
3807
        return;
3808
}
195 giacomo 3809
 
428 giacomo 3810
module_init(bttv_init_module);
3811
module_exit(bttv_cleanup_module);
195 giacomo 3812
 
428 giacomo 3813
/*
3814
 * Local variables:
3815
 * c-basic-offset: 8
3816
 * End:
3817
 */