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