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