Subversion Repositories shark

Rev

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

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