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