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