1 // SPDX-License-Identifier: GPL-2.0-or-later
4 bttv - Bt848 frame grabber driver
6 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
7 & Marcus Metzler <mocm@thp.uni-koeln.de>
8 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
10 some v4l2 code lines are taken from Justin's bttv2 driver which is
11 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
14 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
16 Fixes to be fully V4L2 compliant by
17 (c) 2006 Mauro Carvalho Chehab <mchehab@kernel.org>
19 Cropping and overscan support
20 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
21 Sponsored by OPQ Systems AB
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/interrupt.h>
36 #include <linux/kdev_t.h>
38 #include <media/v4l2-common.h>
39 #include <media/v4l2-ioctl.h>
40 #include <media/v4l2-event.h>
41 #include <media/i2c/tvaudio.h>
42 #include <media/drv-intf/msp3400.h>
44 #include <linux/dma-mapping.h>
47 #include <asm/byteorder.h>
49 #include <media/i2c/saa6588.h>
51 #define BTTV_VERSION "0.9.19"
53 unsigned int bttv_num
; /* number of Bt848s in use */
54 struct bttv
*bttvs
[BTTV_MAX
];
56 unsigned int bttv_debug
;
57 unsigned int bttv_verbose
= 1;
58 unsigned int bttv_gpio
;
60 /* config variables */
62 static unsigned int bigendian
=1;
64 static unsigned int bigendian
;
66 static unsigned int radio
[BTTV_MAX
];
67 static unsigned int irq_debug
;
68 static unsigned int gbuffers
= 8;
69 static unsigned int gbufsize
= 0x208000;
70 static unsigned int reset_crop
= 1;
72 static int video_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
73 static int radio_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
74 static int vbi_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
75 static int debug_latency
;
76 static int disable_ir
;
78 static unsigned int fdsr
;
81 static unsigned int combfilter
;
82 static unsigned int lumafilter
;
83 static unsigned int automute
= 1;
84 static unsigned int chroma_agc
;
85 static unsigned int agc_crush
= 1;
86 static unsigned int whitecrush_upper
= 0xCF;
87 static unsigned int whitecrush_lower
= 0x7F;
88 static unsigned int vcr_hack
;
89 static unsigned int irq_iswitch
;
90 static unsigned int uv_ratio
= 50;
91 static unsigned int full_luma_range
;
92 static unsigned int coring
;
94 /* API features (turn on/off stuff for testing) */
95 static unsigned int v4l2
= 1;
98 module_param(bttv_verbose
, int, 0644);
99 module_param(bttv_gpio
, int, 0644);
100 module_param(bttv_debug
, int, 0644);
101 module_param(irq_debug
, int, 0644);
102 module_param(debug_latency
, int, 0644);
103 module_param(disable_ir
, int, 0444);
105 module_param(fdsr
, int, 0444);
106 module_param(gbuffers
, int, 0444);
107 module_param(gbufsize
, int, 0444);
108 module_param(reset_crop
, int, 0444);
110 module_param(v4l2
, int, 0644);
111 module_param(bigendian
, int, 0644);
112 module_param(irq_iswitch
, int, 0644);
113 module_param(combfilter
, int, 0444);
114 module_param(lumafilter
, int, 0444);
115 module_param(automute
, int, 0444);
116 module_param(chroma_agc
, int, 0444);
117 module_param(agc_crush
, int, 0444);
118 module_param(whitecrush_upper
, int, 0444);
119 module_param(whitecrush_lower
, int, 0444);
120 module_param(vcr_hack
, int, 0444);
121 module_param(uv_ratio
, int, 0444);
122 module_param(full_luma_range
, int, 0444);
123 module_param(coring
, int, 0444);
125 module_param_array(radio
, int, NULL
, 0444);
126 module_param_array(video_nr
, int, NULL
, 0444);
127 module_param_array(radio_nr
, int, NULL
, 0444);
128 module_param_array(vbi_nr
, int, NULL
, 0444);
130 MODULE_PARM_DESC(radio
, "The TV card supports radio, default is 0 (no)");
131 MODULE_PARM_DESC(bigendian
, "byte order of the framebuffer, default is native endian");
132 MODULE_PARM_DESC(bttv_verbose
, "verbose startup messages, default is 1 (yes)");
133 MODULE_PARM_DESC(bttv_gpio
, "log gpio changes, default is 0 (no)");
134 MODULE_PARM_DESC(bttv_debug
, "debug messages, default is 0 (no)");
135 MODULE_PARM_DESC(irq_debug
, "irq handler debug messages, default is 0 (no)");
136 MODULE_PARM_DESC(disable_ir
, "disable infrared remote support");
137 MODULE_PARM_DESC(gbuffers
, "number of capture buffers. range 2-32, default 8");
138 MODULE_PARM_DESC(gbufsize
, "size of the capture buffers, default is 0x208000");
139 MODULE_PARM_DESC(reset_crop
, "reset cropping parameters at open(), default is 1 (yes) for compatibility with older applications");
140 MODULE_PARM_DESC(automute
, "mute audio on bad/missing video signal, default is 1 (yes)");
141 MODULE_PARM_DESC(chroma_agc
, "enables the AGC of chroma signal, default is 0 (no)");
142 MODULE_PARM_DESC(agc_crush
, "enables the luminance AGC crush, default is 1 (yes)");
143 MODULE_PARM_DESC(whitecrush_upper
, "sets the white crush upper value, default is 207");
144 MODULE_PARM_DESC(whitecrush_lower
, "sets the white crush lower value, default is 127");
145 MODULE_PARM_DESC(vcr_hack
, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
146 MODULE_PARM_DESC(irq_iswitch
, "switch inputs in irq handler");
147 MODULE_PARM_DESC(uv_ratio
, "ratio between u and v gains, default is 50");
148 MODULE_PARM_DESC(full_luma_range
, "use the full luma range, default is 0 (no)");
149 MODULE_PARM_DESC(coring
, "set the luma coring level, default is 0 (no)");
150 MODULE_PARM_DESC(video_nr
, "video device numbers");
151 MODULE_PARM_DESC(vbi_nr
, "vbi device numbers");
152 MODULE_PARM_DESC(radio_nr
, "radio device numbers");
154 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
155 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
156 MODULE_LICENSE("GPL");
157 MODULE_VERSION(BTTV_VERSION
);
159 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_USER_BTTV_BASE + 0)
160 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_USER_BTTV_BASE + 1)
161 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_USER_BTTV_BASE + 2)
162 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_USER_BTTV_BASE + 3)
163 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_USER_BTTV_BASE + 4)
164 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_USER_BTTV_BASE + 5)
165 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_USER_BTTV_BASE + 6)
166 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_USER_BTTV_BASE + 7)
167 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_USER_BTTV_BASE + 8)
168 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_USER_BTTV_BASE + 9)
170 /* ----------------------------------------------------------------------- */
173 static ssize_t
show_card(struct device
*cd
,
174 struct device_attribute
*attr
, char *buf
)
176 struct video_device
*vfd
= to_video_device(cd
);
177 struct bttv
*btv
= video_get_drvdata(vfd
);
178 return sprintf(buf
, "%d\n", btv
? btv
->c
.type
: UNSET
);
180 static DEVICE_ATTR(card
, S_IRUGO
, show_card
, NULL
);
182 /* ----------------------------------------------------------------------- */
183 /* dvb auto-load setup */
184 #if defined(CONFIG_MODULES) && defined(MODULE)
185 static void request_module_async(struct work_struct
*work
)
187 request_module("dvb-bt8xx");
190 static void request_modules(struct bttv
*dev
)
192 INIT_WORK(&dev
->request_module_wk
, request_module_async
);
193 schedule_work(&dev
->request_module_wk
);
196 static void flush_request_modules(struct bttv
*dev
)
198 flush_work(&dev
->request_module_wk
);
201 #define request_modules(dev)
202 #define flush_request_modules(dev) do {} while(0)
203 #endif /* CONFIG_MODULES */
206 /* ----------------------------------------------------------------------- */
209 /* special timing tables from conexant... */
210 static u8 SRAM_Table
[][60] =
212 /* PAL digital input over GPIO[7:0] */
214 45, // 45 bytes following
215 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
216 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
217 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
218 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
219 0x37,0x00,0xAF,0x21,0x00
221 /* NTSC digital input over GPIO[7:0] */
223 51, // 51 bytes following
224 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
225 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
226 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
227 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
228 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
231 // TGB_NTSC392 // quartzsight
232 // This table has been modified to be used for Fusion Rev D
234 0x2A, // size of table = 42
235 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
236 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
237 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
238 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
243 /* minhdelayx1 first video pixel we can capture on a line and
244 hdelayx1 start of active video, both relative to rising edge of
245 /HRESET pulse (0H) in 1 / fCLKx1.
246 swidth width of active video and
247 totalwidth total line width, both in 1 / fCLKx1.
248 sqwidth total line width in square pixels.
249 vdelay start of active video in 2 * field lines relative to
250 trailing edge of /VRESET pulse (VDELAY register).
251 sheight height of active video in 2 * field lines.
252 extraheight Added to sheight for cropcap.bounds.height only
253 videostart0 ITU-R frame line number of the line corresponding
254 to vdelay in the first field. */
255 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
256 vdelay, sheight, extraheight, videostart0) \
257 .cropcap.bounds.left = minhdelayx1, \
258 /* * 2 because vertically we count field lines times two, */ \
259 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
260 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
261 /* 4 is a safety margin at the end of the line. */ \
262 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
263 .cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) - \
265 .cropcap.defrect.left = hdelayx1, \
266 .cropcap.defrect.top = (videostart0) * 2, \
267 .cropcap.defrect.width = swidth, \
268 .cropcap.defrect.height = sheight, \
269 .cropcap.pixelaspect.numerator = totalwidth, \
270 .cropcap.pixelaspect.denominator = sqwidth,
272 const struct bttv_tvnorm bttv_tvnorms
[] = {
274 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
275 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
277 .v4l2_id
= V4L2_STD_PAL
,
285 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
286 .scaledtwidth
= 1135,
290 .vbipack
= 255, /* min (2048 / 4, 0x1ff) & 0xff */
292 /* ITU-R frame line number of the first VBI line
293 we can capture, of the first and second field.
294 The last line is determined by cropcap.bounds. */
295 .vbistart
= { 7, 320 },
296 CROPCAP(/* minhdelayx1 */ 68,
298 /* Should be (768 * 1135 + 944 / 2) / 944.
299 cropcap.defrect is used for image width
300 checks, so we keep the old value 924. */
302 /* totalwidth */ 1135,
306 /* bt878 (and bt848?) can capture another
307 line below active video. */
309 /* videostart0 */ 23)
311 .v4l2_id
= V4L2_STD_NTSC_M
| V4L2_STD_NTSC_M_KR
,
319 .iform
= (BT848_IFORM_NTSC
|BT848_IFORM_XT0
),
324 .vbipack
= 144, /* min (1600 / 4, 0x1ff) & 0xff */
326 .vbistart
= { 10, 273 },
327 CROPCAP(/* minhdelayx1 */ 68,
329 /* Should be (640 * 910 + 780 / 2) / 780? */
331 /* totalwidth */ 910,
336 /* videostart0 */ 23)
338 .v4l2_id
= V4L2_STD_SECAM
,
346 .iform
= (BT848_IFORM_SECAM
|BT848_IFORM_XT1
),
347 .scaledtwidth
= 1135,
352 .sram
= 0, /* like PAL, correct? */
353 .vbistart
= { 7, 320 },
354 CROPCAP(/* minhdelayx1 */ 68,
357 /* totalwidth */ 1135,
362 /* videostart0 */ 23)
364 .v4l2_id
= V4L2_STD_PAL_Nc
,
372 .iform
= (BT848_IFORM_PAL_NC
|BT848_IFORM_XT0
),
379 .vbistart
= { 7, 320 },
380 CROPCAP(/* minhdelayx1 */ 68,
382 /* swidth */ (640 * 910 + 780 / 2) / 780,
383 /* totalwidth */ 910,
388 /* videostart0 */ 23)
390 .v4l2_id
= V4L2_STD_PAL_M
,
398 .iform
= (BT848_IFORM_PAL_M
|BT848_IFORM_XT0
),
405 .vbistart
= { 10, 273 },
406 CROPCAP(/* minhdelayx1 */ 68,
408 /* swidth */ (640 * 910 + 780 / 2) / 780,
409 /* totalwidth */ 910,
414 /* videostart0 */ 23)
416 .v4l2_id
= V4L2_STD_PAL_N
,
424 .iform
= (BT848_IFORM_PAL_N
|BT848_IFORM_XT1
),
431 .vbistart
= { 7, 320 },
432 CROPCAP(/* minhdelayx1 */ 68,
434 /* swidth */ (768 * 1135 + 944 / 2) / 944,
435 /* totalwidth */ 1135,
440 /* videostart0 */ 23)
442 .v4l2_id
= V4L2_STD_NTSC_M_JP
,
450 .iform
= (BT848_IFORM_NTSC_J
|BT848_IFORM_XT0
),
457 .vbistart
= { 10, 273 },
458 CROPCAP(/* minhdelayx1 */ 68,
460 /* swidth */ (640 * 910 + 780 / 2) / 780,
461 /* totalwidth */ 910,
466 /* videostart0 */ 23)
468 /* that one hopefully works with the strange timing
469 * which video recorders produce when playing a NTSC
470 * tape on a PAL TV ... */
471 .v4l2_id
= V4L2_STD_PAL_60
,
479 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
480 .scaledtwidth
= 1135,
487 .vbistart
= { 10, 273 },
488 CROPCAP(/* minhdelayx1 */ 68,
491 /* totalwidth */ 1135,
496 /* videostart0 */ 23)
499 static const unsigned int BTTV_TVNORMS
= ARRAY_SIZE(bttv_tvnorms
);
501 /* ----------------------------------------------------------------------- */
503 packed pixel formats must come first */
504 static const struct bttv_format formats
[] = {
506 .fourcc
= V4L2_PIX_FMT_GREY
,
507 .btformat
= BT848_COLOR_FMT_Y8
,
509 .flags
= FORMAT_FLAGS_PACKED
,
511 .fourcc
= V4L2_PIX_FMT_HI240
,
512 .btformat
= BT848_COLOR_FMT_RGB8
,
514 .flags
= FORMAT_FLAGS_PACKED
| FORMAT_FLAGS_DITHER
,
516 .fourcc
= V4L2_PIX_FMT_RGB555
,
517 .btformat
= BT848_COLOR_FMT_RGB15
,
519 .flags
= FORMAT_FLAGS_PACKED
,
521 .fourcc
= V4L2_PIX_FMT_RGB555X
,
522 .btformat
= BT848_COLOR_FMT_RGB15
,
523 .btswap
= 0x03, /* byteswap */
525 .flags
= FORMAT_FLAGS_PACKED
,
527 .fourcc
= V4L2_PIX_FMT_RGB565
,
528 .btformat
= BT848_COLOR_FMT_RGB16
,
530 .flags
= FORMAT_FLAGS_PACKED
,
532 .fourcc
= V4L2_PIX_FMT_RGB565X
,
533 .btformat
= BT848_COLOR_FMT_RGB16
,
534 .btswap
= 0x03, /* byteswap */
536 .flags
= FORMAT_FLAGS_PACKED
,
538 .fourcc
= V4L2_PIX_FMT_BGR24
,
539 .btformat
= BT848_COLOR_FMT_RGB24
,
541 .flags
= FORMAT_FLAGS_PACKED
,
543 .fourcc
= V4L2_PIX_FMT_BGR32
,
544 .btformat
= BT848_COLOR_FMT_RGB32
,
546 .flags
= FORMAT_FLAGS_PACKED
,
548 .fourcc
= V4L2_PIX_FMT_RGB32
,
549 .btformat
= BT848_COLOR_FMT_RGB32
,
550 .btswap
= 0x0f, /* byte+word swap */
552 .flags
= FORMAT_FLAGS_PACKED
,
554 .fourcc
= V4L2_PIX_FMT_YUYV
,
555 .btformat
= BT848_COLOR_FMT_YUY2
,
557 .flags
= FORMAT_FLAGS_PACKED
,
559 .fourcc
= V4L2_PIX_FMT_UYVY
,
560 .btformat
= BT848_COLOR_FMT_YUY2
,
561 .btswap
= 0x03, /* byteswap */
563 .flags
= FORMAT_FLAGS_PACKED
,
565 .fourcc
= V4L2_PIX_FMT_YUV422P
,
566 .btformat
= BT848_COLOR_FMT_YCrCb422
,
568 .flags
= FORMAT_FLAGS_PLANAR
,
572 .fourcc
= V4L2_PIX_FMT_YUV420
,
573 .btformat
= BT848_COLOR_FMT_YCrCb422
,
575 .flags
= FORMAT_FLAGS_PLANAR
,
579 .fourcc
= V4L2_PIX_FMT_YVU420
,
580 .btformat
= BT848_COLOR_FMT_YCrCb422
,
582 .flags
= FORMAT_FLAGS_PLANAR
| FORMAT_FLAGS_CrCb
,
586 .fourcc
= V4L2_PIX_FMT_YUV411P
,
587 .btformat
= BT848_COLOR_FMT_YCrCb411
,
589 .flags
= FORMAT_FLAGS_PLANAR
,
593 .fourcc
= V4L2_PIX_FMT_YUV410
,
594 .btformat
= BT848_COLOR_FMT_YCrCb411
,
596 .flags
= FORMAT_FLAGS_PLANAR
,
600 .fourcc
= V4L2_PIX_FMT_YVU410
,
601 .btformat
= BT848_COLOR_FMT_YCrCb411
,
603 .flags
= FORMAT_FLAGS_PLANAR
| FORMAT_FLAGS_CrCb
,
608 .btformat
= BT848_COLOR_FMT_RAW
,
610 .flags
= FORMAT_FLAGS_RAW
,
613 static const unsigned int FORMATS
= ARRAY_SIZE(formats
);
615 /* ----------------------------------------------------------------------- */
616 /* resource management */
619 RESOURCE_ allocated by freed by
621 VIDEO_READ bttv_read 1) bttv_read 2)
623 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
624 VIDIOC_QBUF 1) bttv_release
627 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
628 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
631 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
632 VIDIOC_QBUF 1) bttv_release
633 bttv_read, bttv_poll 1) 4)
635 1) The resource must be allocated when we enter buffer prepare functions
636 and remain allocated while buffers are in the DMA queue.
637 2) This is a single frame read.
638 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
639 RESOURCE_OVERLAY is allocated.
640 4) This is a continuous read, implies VIDIOC_STREAMON.
642 Note this driver permits video input and standard changes regardless if
643 resources are allocated.
646 #define VBI_RESOURCES (RESOURCE_VBI)
647 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
648 RESOURCE_VIDEO_STREAM | \
652 int check_alloc_btres_lock(struct bttv
*btv
, struct bttv_fh
*fh
, int bit
)
654 int xbits
; /* mutual exclusive resources */
656 if (fh
->resources
& bit
)
657 /* have it already allocated */
661 if (bit
& (RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
))
662 xbits
|= RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
;
665 if (btv
->resources
& xbits
) {
666 /* no, someone else uses it */
670 if ((bit
& VIDEO_RESOURCES
)
671 && 0 == (btv
->resources
& VIDEO_RESOURCES
)) {
672 /* Do crop - use current, don't - use default parameters. */
673 __s32 top
= btv
->crop
[!!fh
->do_crop
].rect
.top
;
675 if (btv
->vbi_end
> top
)
678 /* We cannot capture the same line as video and VBI data.
679 Claim scan lines crop[].rect.top to bottom. */
680 btv
->crop_start
= top
;
681 } else if (bit
& VBI_RESOURCES
) {
682 __s32 end
= fh
->vbi_fmt
.end
;
684 if (end
> btv
->crop_start
)
687 /* Claim scan lines above fh->vbi_fmt.end. */
691 /* it's free, grab it */
692 fh
->resources
|= bit
;
693 btv
->resources
|= bit
;
701 int check_btres(struct bttv_fh
*fh
, int bit
)
703 return (fh
->resources
& bit
);
707 int locked_btres(struct bttv
*btv
, int bit
)
709 return (btv
->resources
& bit
);
712 /* Call with btv->lock down. */
714 disclaim_vbi_lines(struct bttv
*btv
)
719 /* Call with btv->lock down. */
721 disclaim_video_lines(struct bttv
*btv
)
723 const struct bttv_tvnorm
*tvnorm
;
726 tvnorm
= &bttv_tvnorms
[btv
->tvnorm
];
727 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
728 + tvnorm
->cropcap
.bounds
.height
;
730 /* VBI capturing ends at VDELAY, start of video capturing, no
731 matter how many lines the VBI RISC program expects. When video
732 capturing is off, it shall no longer "preempt" VBI capturing,
733 so we set VDELAY to maximum. */
734 crop
= btread(BT848_E_CROP
) | 0xc0;
735 btwrite(crop
, BT848_E_CROP
);
736 btwrite(0xfe, BT848_E_VDELAY_LO
);
737 btwrite(crop
, BT848_O_CROP
);
738 btwrite(0xfe, BT848_O_VDELAY_LO
);
742 void free_btres_lock(struct bttv
*btv
, struct bttv_fh
*fh
, int bits
)
744 if ((fh
->resources
& bits
) != bits
) {
745 /* trying to free resources not allocated by us ... */
746 pr_err("BUG! (btres)\n");
748 fh
->resources
&= ~bits
;
749 btv
->resources
&= ~bits
;
751 bits
= btv
->resources
;
753 if (0 == (bits
& VIDEO_RESOURCES
))
754 disclaim_video_lines(btv
);
756 if (0 == (bits
& VBI_RESOURCES
))
757 disclaim_vbi_lines(btv
);
760 /* ----------------------------------------------------------------------- */
761 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
763 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
764 PLL_X = Reference pre-divider (0=1, 1=2)
765 PLL_C = Post divider (0=6, 1=4)
766 PLL_I = Integer input
767 PLL_F = Fractional input
769 F_input = 28.636363 MHz:
770 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
773 static void set_pll_freq(struct bttv
*btv
, unsigned int fin
, unsigned int fout
)
775 unsigned char fl
, fh
, fi
;
777 /* prevent overflows */
790 btwrite(fl
, BT848_PLL_F_LO
);
791 btwrite(fh
, BT848_PLL_F_HI
);
792 btwrite(fi
|BT848_PLL_X
, BT848_PLL_XCI
);
795 static void set_pll(struct bttv
*btv
)
799 if (!btv
->pll
.pll_crystal
)
802 if (btv
->pll
.pll_ofreq
== btv
->pll
.pll_current
) {
803 dprintk("%d: PLL: no change required\n", btv
->c
.nr
);
807 if (btv
->pll
.pll_ifreq
== btv
->pll
.pll_ofreq
) {
809 if (btv
->pll
.pll_current
== 0)
812 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
813 btv
->c
.nr
, btv
->pll
.pll_ifreq
);
814 btwrite(0x00,BT848_TGCTRL
);
815 btwrite(0x00,BT848_PLL_XCI
);
816 btv
->pll
.pll_current
= 0;
821 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
823 btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
824 set_pll_freq(btv
, btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
826 for (i
=0; i
<10; i
++) {
827 /* Let other people run while the PLL stabilizes */
830 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_PLOCK
) {
831 btwrite(0,BT848_DSTATUS
);
833 btwrite(0x08,BT848_TGCTRL
);
834 btv
->pll
.pll_current
= btv
->pll
.pll_ofreq
;
836 pr_info("PLL set ok\n");
840 btv
->pll
.pll_current
= -1;
842 pr_info("Setting PLL failed\n");
846 /* used to switch between the bt848's analog/digital video capture modes */
847 static void bt848A_set_timing(struct bttv
*btv
)
850 int table_idx
= bttv_tvnorms
[btv
->tvnorm
].sram
;
851 int fsc
= bttv_tvnorms
[btv
->tvnorm
].Fsc
;
853 if (btv
->input
== btv
->dig
) {
854 dprintk("%d: load digital timing table (table_idx=%d)\n",
855 btv
->c
.nr
,table_idx
);
857 /* timing change...reset timing generator address */
858 btwrite(0x00, BT848_TGCTRL
);
859 btwrite(0x02, BT848_TGCTRL
);
860 btwrite(0x00, BT848_TGCTRL
);
862 len
=SRAM_Table
[table_idx
][0];
863 for(i
= 1; i
<= len
; i
++)
864 btwrite(SRAM_Table
[table_idx
][i
],BT848_TGLB
);
865 btv
->pll
.pll_ofreq
= 27000000;
868 btwrite(0x11, BT848_TGCTRL
);
869 btwrite(0x41, BT848_DVSIF
);
871 btv
->pll
.pll_ofreq
= fsc
;
873 btwrite(0x0, BT848_DVSIF
);
877 /* ----------------------------------------------------------------------- */
879 static void bt848_bright(struct bttv
*btv
, int bright
)
883 // printk("set bright: %d\n", bright); // DEBUG
884 btv
->bright
= bright
;
886 /* We want -128 to 127 we get 0-65535 */
887 value
= (bright
>> 8) - 128;
888 btwrite(value
& 0xff, BT848_BRIGHT
);
891 static void bt848_hue(struct bttv
*btv
, int hue
)
898 value
= (hue
>> 8) - 128;
899 btwrite(value
& 0xff, BT848_HUE
);
902 static void bt848_contrast(struct bttv
*btv
, int cont
)
906 btv
->contrast
= cont
;
910 hibit
= (value
>> 6) & 4;
911 btwrite(value
& 0xff, BT848_CONTRAST_LO
);
912 btaor(hibit
, ~4, BT848_E_CONTROL
);
913 btaor(hibit
, ~4, BT848_O_CONTROL
);
916 static void bt848_sat(struct bttv
*btv
, int color
)
918 int val_u
,val_v
,hibits
;
920 btv
->saturation
= color
;
922 /* 0-511 for the color */
923 val_u
= ((color
* btv
->opt_uv_ratio
) / 50) >> 7;
924 val_v
= (((color
* (100 - btv
->opt_uv_ratio
) / 50) >>7)*180L)/254;
925 hibits
= (val_u
>> 7) & 2;
926 hibits
|= (val_v
>> 8) & 1;
927 btwrite(val_u
& 0xff, BT848_SAT_U_LO
);
928 btwrite(val_v
& 0xff, BT848_SAT_V_LO
);
929 btaor(hibits
, ~3, BT848_E_CONTROL
);
930 btaor(hibits
, ~3, BT848_O_CONTROL
);
933 /* ----------------------------------------------------------------------- */
936 video_mux(struct bttv
*btv
, unsigned int input
)
940 if (input
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
943 /* needed by RemoteVideo MX */
944 mask2
= bttv_tvcards
[btv
->c
.type
].gpiomask2
;
946 gpio_inout(mask2
,mask2
);
948 if (input
== btv
->svhs
) {
949 btor(BT848_CONTROL_COMP
, BT848_E_CONTROL
);
950 btor(BT848_CONTROL_COMP
, BT848_O_CONTROL
);
952 btand(~BT848_CONTROL_COMP
, BT848_E_CONTROL
);
953 btand(~BT848_CONTROL_COMP
, BT848_O_CONTROL
);
955 mux
= bttv_muxsel(btv
, input
);
956 btaor(mux
<<5, ~(3<<5), BT848_IFORM
);
957 dprintk("%d: video mux: input=%d mux=%d\n", btv
->c
.nr
, input
, mux
);
959 /* card specific hook */
960 if(bttv_tvcards
[btv
->c
.type
].muxsel_hook
)
961 bttv_tvcards
[btv
->c
.type
].muxsel_hook (btv
, input
);
965 static char *audio_modes
[] = {
966 "audio: tuner", "audio: radio", "audio: extern",
967 "audio: intern", "audio: mute"
971 audio_mux_gpio(struct bttv
*btv
, int input
, int mute
)
973 int gpio_val
, signal
, mute_gpio
;
975 gpio_inout(bttv_tvcards
[btv
->c
.type
].gpiomask
,
976 bttv_tvcards
[btv
->c
.type
].gpiomask
);
977 signal
= btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
;
980 mute_gpio
= mute
|| (btv
->opt_automute
&& (!signal
|| !btv
->users
)
981 && !btv
->has_radio_tuner
);
984 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomute
;
986 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomux
[input
];
988 switch (btv
->c
.type
) {
989 case BTTV_BOARD_VOODOOTV_FM
:
990 case BTTV_BOARD_VOODOOTV_200
:
991 gpio_val
= bttv_tda9880_setnorm(btv
, gpio_val
);
995 gpio_bits(bttv_tvcards
[btv
->c
.type
].gpiomask
, gpio_val
);
999 bttv_gpio_tracking(btv
, audio_modes
[mute_gpio
? 4 : input
]);
1003 audio_mute(struct bttv
*btv
, int mute
)
1005 struct v4l2_ctrl
*ctrl
;
1007 audio_mux_gpio(btv
, btv
->audio_input
, mute
);
1009 if (btv
->sd_msp34xx
) {
1010 ctrl
= v4l2_ctrl_find(btv
->sd_msp34xx
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
1012 v4l2_ctrl_s_ctrl(ctrl
, mute
);
1014 if (btv
->sd_tvaudio
) {
1015 ctrl
= v4l2_ctrl_find(btv
->sd_tvaudio
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
1017 v4l2_ctrl_s_ctrl(ctrl
, mute
);
1019 if (btv
->sd_tda7432
) {
1020 ctrl
= v4l2_ctrl_find(btv
->sd_tda7432
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
1022 v4l2_ctrl_s_ctrl(ctrl
, mute
);
1028 audio_input(struct bttv
*btv
, int input
)
1030 audio_mux_gpio(btv
, input
, btv
->mute
);
1032 if (btv
->sd_msp34xx
) {
1035 /* Note: the inputs tuner/radio/extern/intern are translated
1036 to msp routings. This assumes common behavior for all msp3400
1037 based TV cards. When this assumption fails, then the
1038 specific MSP routing must be added to the card table.
1039 For now this is sufficient. */
1041 case TVAUDIO_INPUT_RADIO
:
1042 /* Some boards need the msp do to the radio demod */
1043 if (btv
->radio_uses_msp_demodulator
) {
1044 in
= MSP_INPUT_DEFAULT
;
1047 in
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1048 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1050 case TVAUDIO_INPUT_EXTERN
:
1051 in
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER1
,
1052 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1054 case TVAUDIO_INPUT_INTERN
:
1055 /* Yes, this is the same input as for RADIO. I doubt
1056 if this is ever used. The only board with an INTERN
1057 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1058 that was tested. My guess is that the whole INTERN
1059 input does not work. */
1060 in
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1061 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1063 case TVAUDIO_INPUT_TUNER
:
1065 /* This is the only card that uses TUNER2, and afaik,
1066 is the only difference between the VOODOOTV_FM
1068 if (btv
->c
.type
== BTTV_BOARD_VOODOOTV_200
)
1069 in
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER2
, \
1070 MSP_DSP_IN_TUNER
, MSP_DSP_IN_TUNER
);
1072 in
= MSP_INPUT_DEFAULT
;
1075 v4l2_subdev_call(btv
->sd_msp34xx
, audio
, s_routing
,
1076 in
, MSP_OUTPUT_DEFAULT
, 0);
1078 if (btv
->sd_tvaudio
) {
1079 v4l2_subdev_call(btv
->sd_tvaudio
, audio
, s_routing
,
1086 bttv_crop_calc_limits(struct bttv_crop
*c
)
1088 /* Scale factor min. 1:1, max. 16:1. Min. image size
1089 48 x 32. Scaled width must be a multiple of 4. */
1092 /* For bug compatibility with VIDIOCGCAP and image
1093 size checks in earlier driver versions. */
1094 c
->min_scaled_width
= 48;
1095 c
->min_scaled_height
= 32;
1097 c
->min_scaled_width
=
1098 (max_t(unsigned int, 48, c
->rect
.width
>> 4) + 3) & ~3;
1099 c
->min_scaled_height
=
1100 max_t(unsigned int, 32, c
->rect
.height
>> 4);
1103 c
->max_scaled_width
= c
->rect
.width
& ~3;
1104 c
->max_scaled_height
= c
->rect
.height
;
1108 bttv_crop_reset(struct bttv_crop
*c
, unsigned int norm
)
1110 c
->rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1111 bttv_crop_calc_limits(c
);
1114 /* Call with btv->lock down. */
1116 set_tvnorm(struct bttv
*btv
, unsigned int norm
)
1118 const struct bttv_tvnorm
*tvnorm
;
1121 BUG_ON(norm
>= BTTV_TVNORMS
);
1122 BUG_ON(btv
->tvnorm
>= BTTV_TVNORMS
);
1124 tvnorm
= &bttv_tvnorms
[norm
];
1126 if (memcmp(&bttv_tvnorms
[btv
->tvnorm
].cropcap
, &tvnorm
->cropcap
,
1127 sizeof (tvnorm
->cropcap
))) {
1128 bttv_crop_reset(&btv
->crop
[0], norm
);
1129 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
1131 if (0 == (btv
->resources
& VIDEO_RESOURCES
)) {
1132 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
1133 + tvnorm
->cropcap
.bounds
.height
;
1139 btwrite(tvnorm
->adelay
, BT848_ADELAY
);
1140 btwrite(tvnorm
->bdelay
, BT848_BDELAY
);
1141 btaor(tvnorm
->iform
,~(BT848_IFORM_NORM
|BT848_IFORM_XTBOTH
),
1143 btwrite(tvnorm
->vbipack
, BT848_VBI_PACK_SIZE
);
1144 btwrite(1, BT848_VBI_PACK_DEL
);
1145 bt848A_set_timing(btv
);
1147 switch (btv
->c
.type
) {
1148 case BTTV_BOARD_VOODOOTV_FM
:
1149 case BTTV_BOARD_VOODOOTV_200
:
1150 bttv_tda9880_setnorm(btv
, gpio_read());
1153 id
= tvnorm
->v4l2_id
;
1154 bttv_call_all(btv
, video
, s_std
, id
);
1159 /* Call with btv->lock down. */
1161 set_input(struct bttv
*btv
, unsigned int input
, unsigned int norm
)
1163 unsigned long flags
;
1167 spin_lock_irqsave(&btv
->s_lock
,flags
);
1168 if (btv
->curr
.frame_irq
) {
1169 /* active capture -> delayed input switch */
1170 btv
->new_input
= input
;
1172 video_mux(btv
,input
);
1174 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1176 video_mux(btv
,input
);
1178 btv
->audio_input
= (btv
->tuner_type
!= TUNER_ABSENT
&& input
== 0) ?
1179 TVAUDIO_INPUT_TUNER
: TVAUDIO_INPUT_EXTERN
;
1180 audio_input(btv
, btv
->audio_input
);
1181 set_tvnorm(btv
, norm
);
1184 static void init_irqreg(struct bttv
*btv
)
1187 btwrite(0xfffffUL
, BT848_INT_STAT
);
1189 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1191 btwrite(BT848_INT_I2CDONE
,
1195 btwrite((btv
->triton1
) |
1196 (btv
->gpioirq
? BT848_INT_GPINT
: 0) |
1198 (fdsr
? BT848_INT_FDSR
: 0) |
1199 BT848_INT_RISCI
| BT848_INT_OCERR
|
1200 BT848_INT_FMTCHG
|BT848_INT_HLOCK
|
1206 static void init_bt848(struct bttv
*btv
)
1208 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1209 /* very basic init only */
1214 btwrite(0x00, BT848_CAP_CTL
);
1215 btwrite(BT848_COLOR_CTL_GAMMA
, BT848_COLOR_CTL
);
1216 btwrite(BT848_IFORM_XTAUTO
| BT848_IFORM_AUTO
, BT848_IFORM
);
1218 /* set planar and packed mode trigger points and */
1219 /* set rising edge of inverted GPINTR pin as irq trigger */
1220 btwrite(BT848_GPIO_DMA_CTL_PKTP_32
|
1221 BT848_GPIO_DMA_CTL_PLTP1_16
|
1222 BT848_GPIO_DMA_CTL_PLTP23_16
|
1223 BT848_GPIO_DMA_CTL_GPINTC
|
1224 BT848_GPIO_DMA_CTL_GPINTI
,
1225 BT848_GPIO_DMA_CTL
);
1227 btwrite(0x20, BT848_E_VSCALE_HI
);
1228 btwrite(0x20, BT848_O_VSCALE_HI
);
1230 v4l2_ctrl_handler_setup(&btv
->ctrl_handler
);
1236 static void bttv_reinit_bt848(struct bttv
*btv
)
1238 unsigned long flags
;
1241 pr_info("%d: reset, reinitialize\n", btv
->c
.nr
);
1242 spin_lock_irqsave(&btv
->s_lock
,flags
);
1244 bttv_set_dma(btv
,0);
1245 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1248 btv
->pll
.pll_current
= -1;
1249 set_input(btv
, btv
->input
, btv
->tvnorm
);
1252 static int bttv_s_ctrl(struct v4l2_ctrl
*c
)
1254 struct bttv
*btv
= container_of(c
->handler
, struct bttv
, ctrl_handler
);
1258 case V4L2_CID_BRIGHTNESS
:
1259 bt848_bright(btv
, c
->val
);
1262 bt848_hue(btv
, c
->val
);
1264 case V4L2_CID_CONTRAST
:
1265 bt848_contrast(btv
, c
->val
);
1267 case V4L2_CID_SATURATION
:
1268 bt848_sat(btv
, c
->val
);
1270 case V4L2_CID_COLOR_KILLER
:
1272 btor(BT848_SCLOOP_CKILL
, BT848_E_SCLOOP
);
1273 btor(BT848_SCLOOP_CKILL
, BT848_O_SCLOOP
);
1275 btand(~BT848_SCLOOP_CKILL
, BT848_E_SCLOOP
);
1276 btand(~BT848_SCLOOP_CKILL
, BT848_O_SCLOOP
);
1279 case V4L2_CID_AUDIO_MUTE
:
1280 audio_mute(btv
, c
->val
);
1283 case V4L2_CID_AUDIO_VOLUME
:
1284 btv
->volume_gpio(btv
, c
->val
);
1287 case V4L2_CID_CHROMA_AGC
:
1288 val
= c
->val
? BT848_SCLOOP_CAGC
: 0;
1289 btwrite(val
, BT848_E_SCLOOP
);
1290 btwrite(val
, BT848_O_SCLOOP
);
1292 case V4L2_CID_PRIVATE_COMBFILTER
:
1293 btv
->opt_combfilter
= c
->val
;
1295 case V4L2_CID_PRIVATE_LUMAFILTER
:
1297 btand(~BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1298 btand(~BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1300 btor(BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1301 btor(BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1304 case V4L2_CID_PRIVATE_AUTOMUTE
:
1305 btv
->opt_automute
= c
->val
;
1307 case V4L2_CID_PRIVATE_AGC_CRUSH
:
1308 btwrite(BT848_ADC_RESERVED
|
1309 (c
->val
? BT848_ADC_CRUSH
: 0),
1312 case V4L2_CID_PRIVATE_VCR_HACK
:
1313 btv
->opt_vcr_hack
= c
->val
;
1315 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER
:
1316 btwrite(c
->val
, BT848_WC_UP
);
1318 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER
:
1319 btwrite(c
->val
, BT848_WC_DOWN
);
1321 case V4L2_CID_PRIVATE_UV_RATIO
:
1322 btv
->opt_uv_ratio
= c
->val
;
1323 bt848_sat(btv
, btv
->saturation
);
1325 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE
:
1326 btaor((c
->val
<< 7), ~BT848_OFORM_RANGE
, BT848_OFORM
);
1328 case V4L2_CID_PRIVATE_CORING
:
1329 btaor((c
->val
<< 5), ~BT848_OFORM_CORE32
, BT848_OFORM
);
1337 /* ----------------------------------------------------------------------- */
1339 static const struct v4l2_ctrl_ops bttv_ctrl_ops
= {
1340 .s_ctrl
= bttv_s_ctrl
,
1343 static struct v4l2_ctrl_config bttv_ctrl_combfilter
= {
1344 .ops
= &bttv_ctrl_ops
,
1345 .id
= V4L2_CID_PRIVATE_COMBFILTER
,
1346 .name
= "Comb Filter",
1347 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1354 static struct v4l2_ctrl_config bttv_ctrl_automute
= {
1355 .ops
= &bttv_ctrl_ops
,
1356 .id
= V4L2_CID_PRIVATE_AUTOMUTE
,
1357 .name
= "Auto Mute",
1358 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1365 static struct v4l2_ctrl_config bttv_ctrl_lumafilter
= {
1366 .ops
= &bttv_ctrl_ops
,
1367 .id
= V4L2_CID_PRIVATE_LUMAFILTER
,
1368 .name
= "Luma Decimation Filter",
1369 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1376 static struct v4l2_ctrl_config bttv_ctrl_agc_crush
= {
1377 .ops
= &bttv_ctrl_ops
,
1378 .id
= V4L2_CID_PRIVATE_AGC_CRUSH
,
1379 .name
= "AGC Crush",
1380 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1387 static struct v4l2_ctrl_config bttv_ctrl_vcr_hack
= {
1388 .ops
= &bttv_ctrl_ops
,
1389 .id
= V4L2_CID_PRIVATE_VCR_HACK
,
1391 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1398 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower
= {
1399 .ops
= &bttv_ctrl_ops
,
1400 .id
= V4L2_CID_PRIVATE_WHITECRUSH_LOWER
,
1401 .name
= "Whitecrush Lower",
1402 .type
= V4L2_CTRL_TYPE_INTEGER
,
1409 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper
= {
1410 .ops
= &bttv_ctrl_ops
,
1411 .id
= V4L2_CID_PRIVATE_WHITECRUSH_UPPER
,
1412 .name
= "Whitecrush Upper",
1413 .type
= V4L2_CTRL_TYPE_INTEGER
,
1420 static struct v4l2_ctrl_config bttv_ctrl_uv_ratio
= {
1421 .ops
= &bttv_ctrl_ops
,
1422 .id
= V4L2_CID_PRIVATE_UV_RATIO
,
1424 .type
= V4L2_CTRL_TYPE_INTEGER
,
1431 static struct v4l2_ctrl_config bttv_ctrl_full_luma
= {
1432 .ops
= &bttv_ctrl_ops
,
1433 .id
= V4L2_CID_PRIVATE_FULL_LUMA_RANGE
,
1434 .name
= "Full Luma Range",
1435 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1441 static struct v4l2_ctrl_config bttv_ctrl_coring
= {
1442 .ops
= &bttv_ctrl_ops
,
1443 .id
= V4L2_CID_PRIVATE_CORING
,
1445 .type
= V4L2_CTRL_TYPE_INTEGER
,
1452 /* ----------------------------------------------------------------------- */
1454 void bttv_gpio_tracking(struct bttv
*btv
, char *comment
)
1456 unsigned int outbits
, data
;
1457 outbits
= btread(BT848_GPIO_OUT_EN
);
1458 data
= btread(BT848_GPIO_DATA
);
1459 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1460 btv
->c
.nr
, outbits
, data
& outbits
, data
& ~outbits
, comment
);
1463 static void bttv_field_count(struct bttv
*btv
)
1471 /* start field counter */
1472 btor(BT848_INT_VSYNC
,BT848_INT_MASK
);
1474 /* stop field counter */
1475 btand(~BT848_INT_VSYNC
,BT848_INT_MASK
);
1476 btv
->field_count
= 0;
1480 static const struct bttv_format
*
1481 format_by_fourcc(int fourcc
)
1485 for (i
= 0; i
< FORMATS
; i
++) {
1486 if (-1 == formats
[i
].fourcc
)
1488 if (formats
[i
].fourcc
== fourcc
)
1494 /* ----------------------------------------------------------------------- */
1498 bttv_switch_overlay(struct bttv
*btv
, struct bttv_fh
*fh
,
1499 struct bttv_buffer
*new)
1501 struct bttv_buffer
*old
;
1502 unsigned long flags
;
1504 dprintk("switch_overlay: enter [new=%p]\n", new);
1506 new->vb
.state
= VIDEOBUF_DONE
;
1507 spin_lock_irqsave(&btv
->s_lock
,flags
);
1511 bttv_set_dma(btv
, 0x03);
1512 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1514 dprintk("switch_overlay: old=%p state is %d\n",
1515 old
, old
->vb
.state
);
1516 bttv_dma_free(&fh
->cap
,btv
, old
);
1520 free_btres_lock(btv
,fh
,RESOURCE_OVERLAY
);
1521 dprintk("switch_overlay: done\n");
1525 /* ----------------------------------------------------------------------- */
1526 /* video4linux (1) interface */
1528 static int bttv_prepare_buffer(struct videobuf_queue
*q
,struct bttv
*btv
,
1529 struct bttv_buffer
*buf
,
1530 const struct bttv_format
*fmt
,
1531 unsigned int width
, unsigned int height
,
1532 enum v4l2_field field
)
1534 struct bttv_fh
*fh
= q
->priv_data
;
1535 int redo_dma_risc
= 0;
1540 /* check settings */
1543 if (fmt
->btformat
== BT848_COLOR_FMT_RAW
) {
1545 height
= RAW_LINES
*2;
1546 if (width
*height
> buf
->vb
.bsize
)
1548 buf
->vb
.size
= buf
->vb
.bsize
;
1550 /* Make sure tvnorm and vbi_end remain consistent
1551 until we're done. */
1555 /* In this mode capturing always starts at defrect.top
1556 (default VDELAY), ignoring cropping parameters. */
1557 if (btv
->vbi_end
> bttv_tvnorms
[norm
].cropcap
.defrect
.top
) {
1561 c
.rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1564 c
= btv
->crop
[!!fh
->do_crop
];
1566 if (width
< c
.min_scaled_width
||
1567 width
> c
.max_scaled_width
||
1568 height
< c
.min_scaled_height
)
1572 case V4L2_FIELD_TOP
:
1573 case V4L2_FIELD_BOTTOM
:
1574 case V4L2_FIELD_ALTERNATE
:
1575 /* btv->crop counts frame lines. Max. scale
1576 factor is 16:1 for frames, 8:1 for fields. */
1577 if (height
* 2 > c
.max_scaled_height
)
1582 if (height
> c
.max_scaled_height
)
1587 buf
->vb
.size
= (width
* height
* fmt
->depth
) >> 3;
1588 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
1592 /* alloc + fill struct bttv_buffer (if changed) */
1593 if (buf
->vb
.width
!= width
|| buf
->vb
.height
!= height
||
1594 buf
->vb
.field
!= field
||
1595 buf
->tvnorm
!= norm
|| buf
->fmt
!= fmt
||
1596 buf
->crop
.top
!= c
.rect
.top
||
1597 buf
->crop
.left
!= c
.rect
.left
||
1598 buf
->crop
.width
!= c
.rect
.width
||
1599 buf
->crop
.height
!= c
.rect
.height
) {
1600 buf
->vb
.width
= width
;
1601 buf
->vb
.height
= height
;
1602 buf
->vb
.field
= field
;
1609 /* alloc risc memory */
1610 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
1612 if (0 != (rc
= videobuf_iolock(q
,&buf
->vb
,&btv
->fbuf
)))
1617 if (0 != (rc
= bttv_buffer_risc(btv
,buf
)))
1620 buf
->vb
.state
= VIDEOBUF_PREPARED
;
1624 bttv_dma_free(q
,btv
,buf
);
1629 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1631 struct bttv_fh
*fh
= q
->priv_data
;
1633 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
1636 if (*size
* *count
> gbuffers
* gbufsize
)
1637 *count
= (gbuffers
* gbufsize
) / *size
;
1642 buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
1643 enum v4l2_field field
)
1645 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1646 struct bttv_fh
*fh
= q
->priv_data
;
1648 return bttv_prepare_buffer(q
,fh
->btv
, buf
, fh
->fmt
,
1649 fh
->width
, fh
->height
, field
);
1653 buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1655 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1656 struct bttv_fh
*fh
= q
->priv_data
;
1657 struct bttv
*btv
= fh
->btv
;
1659 buf
->vb
.state
= VIDEOBUF_QUEUED
;
1660 list_add_tail(&buf
->vb
.queue
,&btv
->capture
);
1661 if (!btv
->curr
.frame_irq
) {
1663 bttv_set_dma(btv
, 0x03);
1667 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1669 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1670 struct bttv_fh
*fh
= q
->priv_data
;
1672 bttv_dma_free(q
,fh
->btv
,buf
);
1675 static const struct videobuf_queue_ops bttv_video_qops
= {
1676 .buf_setup
= buffer_setup
,
1677 .buf_prepare
= buffer_prepare
,
1678 .buf_queue
= buffer_queue
,
1679 .buf_release
= buffer_release
,
1682 static void radio_enable(struct bttv
*btv
)
1684 /* Switch to the radio tuner */
1685 if (!btv
->has_radio_tuner
) {
1686 btv
->has_radio_tuner
= 1;
1687 bttv_call_all(btv
, tuner
, s_radio
);
1688 btv
->audio_input
= TVAUDIO_INPUT_RADIO
;
1689 audio_input(btv
, btv
->audio_input
);
1693 static int bttv_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
1695 struct bttv_fh
*fh
= priv
;
1696 struct bttv
*btv
= fh
->btv
;
1699 for (i
= 0; i
< BTTV_TVNORMS
; i
++)
1700 if (id
& bttv_tvnorms
[i
].v4l2_id
)
1702 if (i
== BTTV_TVNORMS
)
1709 static int bttv_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1711 struct bttv_fh
*fh
= priv
;
1712 struct bttv
*btv
= fh
->btv
;
1718 static int bttv_querystd(struct file
*file
, void *f
, v4l2_std_id
*id
)
1720 struct bttv_fh
*fh
= f
;
1721 struct bttv
*btv
= fh
->btv
;
1723 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_NUML
)
1724 *id
&= V4L2_STD_625_50
;
1726 *id
&= V4L2_STD_525_60
;
1730 static int bttv_enum_input(struct file
*file
, void *priv
,
1731 struct v4l2_input
*i
)
1733 struct bttv_fh
*fh
= priv
;
1734 struct bttv
*btv
= fh
->btv
;
1736 if (i
->index
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
1739 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1742 if (btv
->tuner_type
!= TUNER_ABSENT
&& i
->index
== 0) {
1743 sprintf(i
->name
, "Television");
1744 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1746 } else if (i
->index
== btv
->svhs
) {
1747 sprintf(i
->name
, "S-Video");
1749 sprintf(i
->name
, "Composite%d", i
->index
);
1752 if (i
->index
== btv
->input
) {
1753 __u32 dstatus
= btread(BT848_DSTATUS
);
1754 if (0 == (dstatus
& BT848_DSTATUS_PRES
))
1755 i
->status
|= V4L2_IN_ST_NO_SIGNAL
;
1756 if (0 == (dstatus
& BT848_DSTATUS_HLOC
))
1757 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1760 i
->std
= BTTV_NORMS
;
1764 static int bttv_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1766 struct bttv_fh
*fh
= priv
;
1767 struct bttv
*btv
= fh
->btv
;
1774 static int bttv_s_input(struct file
*file
, void *priv
, unsigned int i
)
1776 struct bttv_fh
*fh
= priv
;
1777 struct bttv
*btv
= fh
->btv
;
1779 if (i
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
1782 set_input(btv
, i
, btv
->tvnorm
);
1786 static int bttv_s_tuner(struct file
*file
, void *priv
,
1787 const struct v4l2_tuner
*t
)
1789 struct bttv_fh
*fh
= priv
;
1790 struct bttv
*btv
= fh
->btv
;
1795 bttv_call_all(btv
, tuner
, s_tuner
, t
);
1797 if (btv
->audio_mode_gpio
) {
1798 struct v4l2_tuner copy
= *t
;
1800 btv
->audio_mode_gpio(btv
, ©
, 1);
1805 static int bttv_g_frequency(struct file
*file
, void *priv
,
1806 struct v4l2_frequency
*f
)
1808 struct bttv_fh
*fh
= priv
;
1809 struct bttv
*btv
= fh
->btv
;
1814 if (f
->type
== V4L2_TUNER_RADIO
)
1816 f
->frequency
= f
->type
== V4L2_TUNER_RADIO
?
1817 btv
->radio_freq
: btv
->tv_freq
;
1822 static void bttv_set_frequency(struct bttv
*btv
, const struct v4l2_frequency
*f
)
1824 struct v4l2_frequency new_freq
= *f
;
1826 bttv_call_all(btv
, tuner
, s_frequency
, f
);
1827 /* s_frequency may clamp the frequency, so get the actual
1828 frequency before assigning radio/tv_freq. */
1829 bttv_call_all(btv
, tuner
, g_frequency
, &new_freq
);
1830 if (new_freq
.type
== V4L2_TUNER_RADIO
) {
1832 btv
->radio_freq
= new_freq
.frequency
;
1833 if (btv
->has_tea575x
) {
1834 btv
->tea
.freq
= btv
->radio_freq
;
1835 snd_tea575x_set_freq(&btv
->tea
);
1838 btv
->tv_freq
= new_freq
.frequency
;
1842 static int bttv_s_frequency(struct file
*file
, void *priv
,
1843 const struct v4l2_frequency
*f
)
1845 struct bttv_fh
*fh
= priv
;
1846 struct bttv
*btv
= fh
->btv
;
1851 bttv_set_frequency(btv
, f
);
1855 static int bttv_log_status(struct file
*file
, void *f
)
1857 struct video_device
*vdev
= video_devdata(file
);
1858 struct bttv_fh
*fh
= f
;
1859 struct bttv
*btv
= fh
->btv
;
1861 v4l2_ctrl_handler_log_status(vdev
->ctrl_handler
, btv
->c
.v4l2_dev
.name
);
1862 bttv_call_all(btv
, core
, log_status
);
1866 #ifdef CONFIG_VIDEO_ADV_DEBUG
1867 static int bttv_g_register(struct file
*file
, void *f
,
1868 struct v4l2_dbg_register
*reg
)
1870 struct bttv_fh
*fh
= f
;
1871 struct bttv
*btv
= fh
->btv
;
1873 /* bt848 has a 12-bit register space */
1875 reg
->val
= btread(reg
->reg
);
1881 static int bttv_s_register(struct file
*file
, void *f
,
1882 const struct v4l2_dbg_register
*reg
)
1884 struct bttv_fh
*fh
= f
;
1885 struct bttv
*btv
= fh
->btv
;
1887 /* bt848 has a 12-bit register space */
1888 btwrite(reg
->val
, reg
->reg
& 0xfff);
1894 /* Given cropping boundaries b and the scaled width and height of a
1895 single field or frame, which must not exceed hardware limits, this
1896 function adjusts the cropping parameters c. */
1898 bttv_crop_adjust (struct bttv_crop
* c
,
1899 const struct v4l2_rect
* b
,
1902 enum v4l2_field field
)
1904 __s32 frame_height
= height
<< !V4L2_FIELD_HAS_BOTH(field
);
1908 if (width
< c
->min_scaled_width
) {
1909 /* Max. hor. scale factor 16:1. */
1910 c
->rect
.width
= width
* 16;
1911 } else if (width
> c
->max_scaled_width
) {
1912 /* Min. hor. scale factor 1:1. */
1913 c
->rect
.width
= width
;
1915 max_left
= b
->left
+ b
->width
- width
;
1916 max_left
= min(max_left
, (__s32
) MAX_HDELAY
);
1917 if (c
->rect
.left
> max_left
)
1918 c
->rect
.left
= max_left
;
1921 if (height
< c
->min_scaled_height
) {
1922 /* Max. vert. scale factor 16:1, single fields 8:1. */
1923 c
->rect
.height
= height
* 16;
1924 } else if (frame_height
> c
->max_scaled_height
) {
1925 /* Min. vert. scale factor 1:1.
1926 Top and height count field lines times two. */
1927 c
->rect
.height
= (frame_height
+ 1) & ~1;
1929 max_top
= b
->top
+ b
->height
- c
->rect
.height
;
1930 if (c
->rect
.top
> max_top
)
1931 c
->rect
.top
= max_top
;
1934 bttv_crop_calc_limits(c
);
1937 /* Returns an error if scaling to a frame or single field with the given
1938 width and height is not possible with the current cropping parameters
1939 and width aligned according to width_mask. If adjust_size is TRUE the
1940 function may adjust the width and/or height instead, rounding width
1941 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
1942 also adjust the current cropping parameters to get closer to the
1943 desired image size. */
1945 limit_scaled_size_lock (struct bttv_fh
* fh
,
1948 enum v4l2_field field
,
1949 unsigned int width_mask
,
1950 unsigned int width_bias
,
1954 struct bttv
*btv
= fh
->btv
;
1955 const struct v4l2_rect
*b
;
1956 struct bttv_crop
*c
;
1963 BUG_ON((int) width_mask
>= 0 ||
1964 width_bias
>= (unsigned int) -width_mask
);
1966 /* Make sure tvnorm, vbi_end and the current cropping parameters
1967 remain consistent until we're done. */
1969 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
1971 /* Do crop - use current, don't - use default parameters. */
1972 c
= &btv
->crop
[!!fh
->do_crop
];
1977 && !locked_btres(btv
, VIDEO_RESOURCES
)) {
1981 /* We cannot scale up. When the scaled image is larger
1982 than crop.rect we adjust the crop.rect as required
1983 by the V4L2 spec, hence cropcap.bounds are our limit. */
1984 max_width
= min_t(unsigned int, b
->width
, MAX_HACTIVE
);
1985 max_height
= b
->height
;
1987 /* We cannot capture the same line as video and VBI data.
1988 Note btv->vbi_end is really a minimum, see
1989 bttv_vbi_try_fmt(). */
1990 if (btv
->vbi_end
> b
->top
) {
1991 max_height
-= btv
->vbi_end
- b
->top
;
1993 if (min_height
> max_height
)
1998 if (btv
->vbi_end
> c
->rect
.top
)
2001 min_width
= c
->min_scaled_width
;
2002 min_height
= c
->min_scaled_height
;
2003 max_width
= c
->max_scaled_width
;
2004 max_height
= c
->max_scaled_height
;
2009 min_width
= (min_width
- width_mask
- 1) & width_mask
;
2010 max_width
= max_width
& width_mask
;
2012 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2013 /* Min. scale factor is 1:1. */
2014 max_height
>>= !V4L2_FIELD_HAS_BOTH(field
);
2017 *width
= clamp(*width
, min_width
, max_width
);
2018 *height
= clamp(*height
, min_height
, max_height
);
2020 /* Round after clamping to avoid overflow. */
2021 *width
= (*width
+ width_bias
) & width_mask
;
2024 bttv_crop_adjust(c
, b
, *width
, *height
, field
);
2026 if (btv
->vbi_end
> c
->rect
.top
) {
2027 /* Move the crop window out of the way. */
2028 c
->rect
.top
= btv
->vbi_end
;
2033 if (*width
< min_width
||
2034 *height
< min_height
||
2035 *width
> max_width
||
2036 *height
> max_height
||
2037 0 != (*width
& ~width_mask
))
2041 rc
= 0; /* success */
2048 /* Returns an error if the given overlay window dimensions are not
2049 possible with the current cropping parameters. If adjust_size is
2050 TRUE the function may adjust the window width and/or height
2051 instead, however it always rounds the horizontal position and
2052 width as btcx_align() does. If adjust_crop is TRUE the function
2053 may also adjust the current cropping parameters to get closer
2054 to the desired window size. */
2056 verify_window_lock(struct bttv_fh
*fh
, struct v4l2_window
*win
,
2057 int adjust_size
, int adjust_crop
)
2059 enum v4l2_field field
;
2060 unsigned int width_mask
;
2062 if (win
->w
.width
< 48)
2064 if (win
->w
.height
< 32)
2066 if (win
->clipcount
> 2048)
2067 win
->clipcount
= 2048;
2070 win
->global_alpha
= 0;
2074 case V4L2_FIELD_TOP
:
2075 case V4L2_FIELD_BOTTOM
:
2076 case V4L2_FIELD_INTERLACED
:
2079 field
= V4L2_FIELD_ANY
;
2082 if (V4L2_FIELD_ANY
== field
) {
2085 height2
= fh
->btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2086 field
= (win
->w
.height
> height2
)
2087 ? V4L2_FIELD_INTERLACED
2092 if (NULL
== fh
->ovfmt
)
2094 /* 4-byte alignment. */
2096 switch (fh
->ovfmt
->depth
) {
2110 win
->w
.width
-= win
->w
.left
& ~width_mask
;
2111 win
->w
.left
= (win
->w
.left
- width_mask
- 1) & width_mask
;
2113 return limit_scaled_size_lock(fh
, &win
->w
.width
, &win
->w
.height
,
2115 /* width_bias: round down */ 0,
2116 adjust_size
, adjust_crop
);
2119 static int setup_window_lock(struct bttv_fh
*fh
, struct bttv
*btv
,
2120 struct v4l2_window
*win
, int fixup
)
2122 struct v4l2_clip
*clips
= NULL
;
2123 int n
,size
,retval
= 0;
2125 if (NULL
== fh
->ovfmt
)
2127 if (!(fh
->ovfmt
->flags
& FORMAT_FLAGS_PACKED
))
2129 retval
= verify_window_lock(fh
, win
,
2130 /* adjust_size */ fixup
,
2131 /* adjust_crop */ fixup
);
2135 /* copy clips -- luckily v4l1 + v4l2 are binary
2136 compatible here ...*/
2138 size
= sizeof(*clips
)*(n
+4);
2139 clips
= kmalloc(size
,GFP_KERNEL
);
2143 memcpy(clips
, win
->clips
, sizeof(struct v4l2_clip
) * n
);
2145 /* clip against screen */
2146 if (NULL
!= btv
->fbuf
.base
)
2147 n
= btcx_screen_clips(btv
->fbuf
.fmt
.width
, btv
->fbuf
.fmt
.height
,
2149 btcx_sort_clips(clips
,n
);
2151 /* 4-byte alignments */
2152 switch (fh
->ovfmt
->depth
) {
2155 btcx_align(&win
->w
, clips
, n
, 3);
2158 btcx_align(&win
->w
, clips
, n
, 1);
2161 /* no alignment fixups needed */
2167 kfree(fh
->ov
.clips
);
2168 fh
->ov
.clips
= clips
;
2172 fh
->ov
.field
= win
->field
;
2173 fh
->ov
.setup_ok
= 1;
2175 btv
->init
.ov
.w
.width
= win
->w
.width
;
2176 btv
->init
.ov
.w
.height
= win
->w
.height
;
2177 btv
->init
.ov
.field
= win
->field
;
2179 /* update overlay if needed */
2181 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
2182 struct bttv_buffer
*new;
2184 new = videobuf_sg_alloc(sizeof(*new));
2185 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2186 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2187 retval
= bttv_switch_overlay(btv
,fh
,new);
2192 /* ----------------------------------------------------------------------- */
2194 static struct videobuf_queue
* bttv_queue(struct bttv_fh
*fh
)
2196 struct videobuf_queue
* q
= NULL
;
2199 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2202 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2211 static int bttv_resource(struct bttv_fh
*fh
)
2216 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2217 res
= RESOURCE_VIDEO_STREAM
;
2219 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2228 static int bttv_switch_type(struct bttv_fh
*fh
, enum v4l2_buf_type type
)
2230 struct videobuf_queue
*q
= bttv_queue(fh
);
2231 int res
= bttv_resource(fh
);
2233 if (check_btres(fh
,res
))
2235 if (videobuf_queue_is_busy(q
))
2242 pix_format_set_size (struct v4l2_pix_format
* f
,
2243 const struct bttv_format
* fmt
,
2245 unsigned int height
)
2250 if (fmt
->flags
& FORMAT_FLAGS_PLANAR
) {
2251 f
->bytesperline
= width
; /* Y plane */
2252 f
->sizeimage
= (width
* height
* fmt
->depth
) >> 3;
2254 f
->bytesperline
= (width
* fmt
->depth
) >> 3;
2255 f
->sizeimage
= height
* f
->bytesperline
;
2259 static int bttv_g_fmt_vid_cap(struct file
*file
, void *priv
,
2260 struct v4l2_format
*f
)
2262 struct bttv_fh
*fh
= priv
;
2264 pix_format_set_size(&f
->fmt
.pix
, fh
->fmt
,
2265 fh
->width
, fh
->height
);
2266 f
->fmt
.pix
.field
= fh
->cap
.field
;
2267 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
2268 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
2273 static int bttv_g_fmt_vid_overlay(struct file
*file
, void *priv
,
2274 struct v4l2_format
*f
)
2276 struct bttv_fh
*fh
= priv
;
2278 f
->fmt
.win
.w
= fh
->ov
.w
;
2279 f
->fmt
.win
.field
= fh
->ov
.field
;
2284 static void bttv_get_width_mask_vid_cap(const struct bttv_format
*fmt
,
2285 unsigned int *width_mask
,
2286 unsigned int *width_bias
)
2288 if (fmt
->flags
& FORMAT_FLAGS_PLANAR
) {
2289 *width_mask
= ~15; /* width must be a multiple of 16 pixels */
2290 *width_bias
= 8; /* nearest */
2292 *width_mask
= ~3; /* width must be a multiple of 4 pixels */
2293 *width_bias
= 2; /* nearest */
2297 static int bttv_try_fmt_vid_cap(struct file
*file
, void *priv
,
2298 struct v4l2_format
*f
)
2300 const struct bttv_format
*fmt
;
2301 struct bttv_fh
*fh
= priv
;
2302 struct bttv
*btv
= fh
->btv
;
2303 enum v4l2_field field
;
2304 __s32 width
, height
;
2306 unsigned int width_mask
, width_bias
;
2309 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2313 field
= f
->fmt
.pix
.field
;
2316 case V4L2_FIELD_TOP
:
2317 case V4L2_FIELD_BOTTOM
:
2318 case V4L2_FIELD_ALTERNATE
:
2319 case V4L2_FIELD_INTERLACED
:
2321 case V4L2_FIELD_SEQ_BT
:
2322 case V4L2_FIELD_SEQ_TB
:
2323 if (!(fmt
->flags
& FORMAT_FLAGS_PLANAR
)) {
2324 field
= V4L2_FIELD_SEQ_TB
;
2328 default: /* FIELD_ANY case */
2329 height2
= btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2330 field
= (f
->fmt
.pix
.height
> height2
)
2331 ? V4L2_FIELD_INTERLACED
2332 : V4L2_FIELD_BOTTOM
;
2336 width
= f
->fmt
.pix
.width
;
2337 height
= f
->fmt
.pix
.height
;
2339 bttv_get_width_mask_vid_cap(fmt
, &width_mask
, &width_bias
);
2340 rc
= limit_scaled_size_lock(fh
, &width
, &height
, field
,
2341 width_mask
, width_bias
,
2342 /* adjust_size */ 1,
2343 /* adjust_crop */ 0);
2347 /* update data for the application */
2348 f
->fmt
.pix
.field
= field
;
2349 pix_format_set_size(&f
->fmt
.pix
, fmt
, width
, height
);
2350 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
2355 static int bttv_try_fmt_vid_overlay(struct file
*file
, void *priv
,
2356 struct v4l2_format
*f
)
2358 struct bttv_fh
*fh
= priv
;
2360 verify_window_lock(fh
, &f
->fmt
.win
,
2361 /* adjust_size */ 1,
2362 /* adjust_crop */ 0);
2366 static int bttv_s_fmt_vid_cap(struct file
*file
, void *priv
,
2367 struct v4l2_format
*f
)
2370 const struct bttv_format
*fmt
;
2371 struct bttv_fh
*fh
= priv
;
2372 struct bttv
*btv
= fh
->btv
;
2373 __s32 width
, height
;
2374 unsigned int width_mask
, width_bias
;
2375 enum v4l2_field field
;
2377 retval
= bttv_switch_type(fh
, f
->type
);
2381 retval
= bttv_try_fmt_vid_cap(file
, priv
, f
);
2385 width
= f
->fmt
.pix
.width
;
2386 height
= f
->fmt
.pix
.height
;
2387 field
= f
->fmt
.pix
.field
;
2389 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2390 bttv_get_width_mask_vid_cap(fmt
, &width_mask
, &width_bias
);
2391 retval
= limit_scaled_size_lock(fh
, &width
, &height
, f
->fmt
.pix
.field
,
2392 width_mask
, width_bias
,
2393 /* adjust_size */ 1,
2394 /* adjust_crop */ 1);
2398 f
->fmt
.pix
.field
= field
;
2400 /* update our state information */
2402 fh
->cap
.field
= f
->fmt
.pix
.field
;
2403 fh
->cap
.last
= V4L2_FIELD_NONE
;
2404 fh
->width
= f
->fmt
.pix
.width
;
2405 fh
->height
= f
->fmt
.pix
.height
;
2406 btv
->init
.fmt
= fmt
;
2407 btv
->init
.width
= f
->fmt
.pix
.width
;
2408 btv
->init
.height
= f
->fmt
.pix
.height
;
2413 static int bttv_s_fmt_vid_overlay(struct file
*file
, void *priv
,
2414 struct v4l2_format
*f
)
2416 struct bttv_fh
*fh
= priv
;
2417 struct bttv
*btv
= fh
->btv
;
2419 if (no_overlay
> 0) {
2420 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2424 return setup_window_lock(fh
, btv
, &f
->fmt
.win
, 1);
2427 static int bttv_querycap(struct file
*file
, void *priv
,
2428 struct v4l2_capability
*cap
)
2430 struct bttv_fh
*fh
= priv
;
2431 struct bttv
*btv
= fh
->btv
;
2436 strscpy(cap
->driver
, "bttv", sizeof(cap
->driver
));
2437 strscpy(cap
->card
, btv
->video_dev
.name
, sizeof(cap
->card
));
2438 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
2439 "PCI:%s", pci_name(btv
->c
.pci
));
2440 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
|
2441 V4L2_CAP_STREAMING
| V4L2_CAP_DEVICE_CAPS
;
2442 if (no_overlay
<= 0)
2443 cap
->capabilities
|= V4L2_CAP_VIDEO_OVERLAY
;
2444 if (video_is_registered(&btv
->vbi_dev
))
2445 cap
->capabilities
|= V4L2_CAP_VBI_CAPTURE
;
2446 if (video_is_registered(&btv
->radio_dev
)) {
2447 cap
->capabilities
|= V4L2_CAP_RADIO
;
2448 if (btv
->has_tea575x
)
2449 cap
->capabilities
|= V4L2_CAP_HW_FREQ_SEEK
;
2453 * No need to lock here: those vars are initialized during board
2454 * probe and remains untouched during the rest of the driver lifecycle
2456 if (btv
->has_saa6588
)
2457 cap
->capabilities
|= V4L2_CAP_RDS_CAPTURE
;
2458 if (btv
->tuner_type
!= TUNER_ABSENT
)
2459 cap
->capabilities
|= V4L2_CAP_TUNER
;
2463 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc
*f
)
2467 for (i
= 0; i
< FORMATS
; i
++) {
2468 if (formats
[i
].fourcc
!= -1)
2470 if ((unsigned int)index
== f
->index
)
2476 f
->pixelformat
= formats
[i
].fourcc
;
2481 static int bttv_enum_fmt_vid_cap(struct file
*file
, void *priv
,
2482 struct v4l2_fmtdesc
*f
)
2484 int rc
= bttv_enum_fmt_cap_ovr(f
);
2492 static int bttv_enum_fmt_vid_overlay(struct file
*file
, void *priv
,
2493 struct v4l2_fmtdesc
*f
)
2497 if (no_overlay
> 0) {
2498 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2502 rc
= bttv_enum_fmt_cap_ovr(f
);
2507 if (!(formats
[rc
].flags
& FORMAT_FLAGS_PACKED
))
2513 static int bttv_g_fbuf(struct file
*file
, void *f
,
2514 struct v4l2_framebuffer
*fb
)
2516 struct bttv_fh
*fh
= f
;
2517 struct bttv
*btv
= fh
->btv
;
2520 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
2521 fb
->flags
= V4L2_FBUF_FLAG_PRIMARY
;
2523 fb
->fmt
.pixelformat
= fh
->ovfmt
->fourcc
;
2527 static int bttv_overlay(struct file
*file
, void *f
, unsigned int on
)
2529 struct bttv_fh
*fh
= f
;
2530 struct bttv
*btv
= fh
->btv
;
2531 struct bttv_buffer
*new;
2536 if (unlikely(!btv
->fbuf
.base
)) {
2539 if (unlikely(!fh
->ov
.setup_ok
)) {
2540 dprintk("%d: overlay: !setup_ok\n", btv
->c
.nr
);
2547 if (!check_alloc_btres_lock(btv
, fh
, RESOURCE_OVERLAY
))
2551 fh
->ov
.tvnorm
= btv
->tvnorm
;
2552 new = videobuf_sg_alloc(sizeof(*new));
2553 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2554 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2560 retval
= bttv_switch_overlay(btv
, fh
, new);
2564 static int bttv_s_fbuf(struct file
*file
, void *f
,
2565 const struct v4l2_framebuffer
*fb
)
2567 struct bttv_fh
*fh
= f
;
2568 struct bttv
*btv
= fh
->btv
;
2569 const struct bttv_format
*fmt
;
2572 if (!capable(CAP_SYS_ADMIN
) &&
2573 !capable(CAP_SYS_RAWIO
))
2577 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
2580 if (0 == (fmt
->flags
& FORMAT_FLAGS_PACKED
))
2584 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
2585 __s32 width
= fb
->fmt
.width
;
2586 __s32 height
= fb
->fmt
.height
;
2588 retval
= limit_scaled_size_lock(fh
, &width
, &height
,
2589 V4L2_FIELD_INTERLACED
,
2590 /* width_mask */ ~3,
2592 /* adjust_size */ 0,
2593 /* adjust_crop */ 0);
2599 btv
->fbuf
.base
= fb
->base
;
2600 btv
->fbuf
.fmt
.width
= fb
->fmt
.width
;
2601 btv
->fbuf
.fmt
.height
= fb
->fmt
.height
;
2602 if (0 != fb
->fmt
.bytesperline
)
2603 btv
->fbuf
.fmt
.bytesperline
= fb
->fmt
.bytesperline
;
2605 btv
->fbuf
.fmt
.bytesperline
= btv
->fbuf
.fmt
.width
*fmt
->depth
/8;
2609 btv
->init
.ovfmt
= fmt
;
2610 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
2613 fh
->ov
.w
.width
= fb
->fmt
.width
;
2614 fh
->ov
.w
.height
= fb
->fmt
.height
;
2615 btv
->init
.ov
.w
.width
= fb
->fmt
.width
;
2616 btv
->init
.ov
.w
.height
= fb
->fmt
.height
;
2618 kfree(fh
->ov
.clips
);
2619 fh
->ov
.clips
= NULL
;
2622 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
2623 struct bttv_buffer
*new;
2625 new = videobuf_sg_alloc(sizeof(*new));
2626 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2627 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2628 retval
= bttv_switch_overlay(btv
, fh
, new);
2634 static int bttv_reqbufs(struct file
*file
, void *priv
,
2635 struct v4l2_requestbuffers
*p
)
2637 struct bttv_fh
*fh
= priv
;
2638 return videobuf_reqbufs(bttv_queue(fh
), p
);
2641 static int bttv_querybuf(struct file
*file
, void *priv
,
2642 struct v4l2_buffer
*b
)
2644 struct bttv_fh
*fh
= priv
;
2645 return videobuf_querybuf(bttv_queue(fh
), b
);
2648 static int bttv_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2650 struct bttv_fh
*fh
= priv
;
2651 struct bttv
*btv
= fh
->btv
;
2652 int res
= bttv_resource(fh
);
2654 if (!check_alloc_btres_lock(btv
, fh
, res
))
2657 return videobuf_qbuf(bttv_queue(fh
), b
);
2660 static int bttv_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2662 struct bttv_fh
*fh
= priv
;
2663 return videobuf_dqbuf(bttv_queue(fh
), b
,
2664 file
->f_flags
& O_NONBLOCK
);
2667 static int bttv_streamon(struct file
*file
, void *priv
,
2668 enum v4l2_buf_type type
)
2670 struct bttv_fh
*fh
= priv
;
2671 struct bttv
*btv
= fh
->btv
;
2672 int res
= bttv_resource(fh
);
2674 if (!check_alloc_btres_lock(btv
, fh
, res
))
2676 return videobuf_streamon(bttv_queue(fh
));
2680 static int bttv_streamoff(struct file
*file
, void *priv
,
2681 enum v4l2_buf_type type
)
2683 struct bttv_fh
*fh
= priv
;
2684 struct bttv
*btv
= fh
->btv
;
2686 int res
= bttv_resource(fh
);
2689 retval
= videobuf_streamoff(bttv_queue(fh
));
2692 free_btres_lock(btv
, fh
, res
);
2696 static int bttv_g_parm(struct file
*file
, void *f
,
2697 struct v4l2_streamparm
*parm
)
2699 struct bttv_fh
*fh
= f
;
2700 struct bttv
*btv
= fh
->btv
;
2702 if (parm
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2704 parm
->parm
.capture
.readbuffers
= gbuffers
;
2705 v4l2_video_std_frame_period(bttv_tvnorms
[btv
->tvnorm
].v4l2_id
,
2706 &parm
->parm
.capture
.timeperframe
);
2711 static int bttv_g_tuner(struct file
*file
, void *priv
,
2712 struct v4l2_tuner
*t
)
2714 struct bttv_fh
*fh
= priv
;
2715 struct bttv
*btv
= fh
->btv
;
2720 t
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
2721 t
->capability
= V4L2_TUNER_CAP_NORM
;
2722 bttv_call_all(btv
, tuner
, g_tuner
, t
);
2723 strscpy(t
->name
, "Television", sizeof(t
->name
));
2724 t
->type
= V4L2_TUNER_ANALOG_TV
;
2725 if (btread(BT848_DSTATUS
)&BT848_DSTATUS_HLOC
)
2728 if (btv
->audio_mode_gpio
)
2729 btv
->audio_mode_gpio(btv
, t
, 0);
2734 static int bttv_g_pixelaspect(struct file
*file
, void *priv
,
2735 int type
, struct v4l2_fract
*f
)
2737 struct bttv_fh
*fh
= priv
;
2738 struct bttv
*btv
= fh
->btv
;
2740 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2743 /* defrect and bounds are set via g_selection */
2744 *f
= bttv_tvnorms
[btv
->tvnorm
].cropcap
.pixelaspect
;
2748 static int bttv_g_selection(struct file
*file
, void *f
, struct v4l2_selection
*sel
)
2750 struct bttv_fh
*fh
= f
;
2751 struct bttv
*btv
= fh
->btv
;
2753 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2754 sel
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2757 switch (sel
->target
) {
2758 case V4L2_SEL_TGT_CROP
:
2760 * No fh->do_crop = 1; because btv->crop[1] may be
2761 * inconsistent with fh->width or fh->height and apps
2762 * do not expect a change here.
2764 sel
->r
= btv
->crop
[!!fh
->do_crop
].rect
;
2766 case V4L2_SEL_TGT_CROP_DEFAULT
:
2767 sel
->r
= bttv_tvnorms
[btv
->tvnorm
].cropcap
.defrect
;
2769 case V4L2_SEL_TGT_CROP_BOUNDS
:
2770 sel
->r
= bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
2779 static int bttv_s_selection(struct file
*file
, void *f
, struct v4l2_selection
*sel
)
2781 struct bttv_fh
*fh
= f
;
2782 struct bttv
*btv
= fh
->btv
;
2783 const struct v4l2_rect
*b
;
2791 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2792 sel
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2795 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
2798 /* Make sure tvnorm, vbi_end and the current cropping
2799 parameters remain consistent until we're done. Note
2800 read() may change vbi_end in check_alloc_btres_lock(). */
2803 if (locked_btres(fh
->btv
, VIDEO_RESOURCES
)) {
2807 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
2810 b_right
= b_left
+ b
->width
;
2811 b_bottom
= b
->top
+ b
->height
;
2813 b_top
= max(b
->top
, btv
->vbi_end
);
2814 if (b_top
+ 32 >= b_bottom
) {
2818 /* Min. scaled size 48 x 32. */
2819 c
.rect
.left
= clamp_t(s32
, sel
->r
.left
, b_left
, b_right
- 48);
2820 c
.rect
.left
= min(c
.rect
.left
, (__s32
) MAX_HDELAY
);
2822 c
.rect
.width
= clamp_t(s32
, sel
->r
.width
,
2823 48, b_right
- c
.rect
.left
);
2825 c
.rect
.top
= clamp_t(s32
, sel
->r
.top
, b_top
, b_bottom
- 32);
2826 /* Top and height must be a multiple of two. */
2827 c
.rect
.top
= (c
.rect
.top
+ 1) & ~1;
2829 c
.rect
.height
= clamp_t(s32
, sel
->r
.height
,
2830 32, b_bottom
- c
.rect
.top
);
2831 c
.rect
.height
= (c
.rect
.height
+ 1) & ~1;
2833 bttv_crop_calc_limits(&c
);
2841 if (fh
->width
< c
.min_scaled_width
) {
2842 fh
->width
= c
.min_scaled_width
;
2843 btv
->init
.width
= c
.min_scaled_width
;
2844 } else if (fh
->width
> c
.max_scaled_width
) {
2845 fh
->width
= c
.max_scaled_width
;
2846 btv
->init
.width
= c
.max_scaled_width
;
2849 if (fh
->height
< c
.min_scaled_height
) {
2850 fh
->height
= c
.min_scaled_height
;
2851 btv
->init
.height
= c
.min_scaled_height
;
2852 } else if (fh
->height
> c
.max_scaled_height
) {
2853 fh
->height
= c
.max_scaled_height
;
2854 btv
->init
.height
= c
.max_scaled_height
;
2860 static ssize_t
bttv_read(struct file
*file
, char __user
*data
,
2861 size_t count
, loff_t
*ppos
)
2863 struct bttv_fh
*fh
= file
->private_data
;
2866 if (fh
->btv
->errors
)
2867 bttv_reinit_bt848(fh
->btv
);
2868 dprintk("%d: read count=%d type=%s\n",
2869 fh
->btv
->c
.nr
, (int)count
, v4l2_type_names
[fh
->type
]);
2872 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2873 if (!check_alloc_btres_lock(fh
->btv
, fh
, RESOURCE_VIDEO_READ
)) {
2874 /* VIDEO_READ in use by another fh,
2875 or VIDEO_STREAM by any fh. */
2878 retval
= videobuf_read_one(&fh
->cap
, data
, count
, ppos
,
2879 file
->f_flags
& O_NONBLOCK
);
2880 free_btres_lock(fh
->btv
, fh
, RESOURCE_VIDEO_READ
);
2882 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2883 if (!check_alloc_btres_lock(fh
->btv
,fh
,RESOURCE_VBI
))
2885 retval
= videobuf_read_stream(&fh
->vbi
, data
, count
, ppos
, 1,
2886 file
->f_flags
& O_NONBLOCK
);
2894 static __poll_t
bttv_poll(struct file
*file
, poll_table
*wait
)
2896 struct bttv_fh
*fh
= file
->private_data
;
2897 struct bttv_buffer
*buf
;
2898 enum v4l2_field field
;
2900 __poll_t req_events
= poll_requested_events(wait
);
2902 if (v4l2_event_pending(&fh
->fh
))
2904 else if (req_events
& EPOLLPRI
)
2905 poll_wait(file
, &fh
->fh
.wait
, wait
);
2907 if (!(req_events
& (EPOLLIN
| EPOLLRDNORM
)))
2910 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
2911 if (!check_alloc_btres_lock(fh
->btv
,fh
,RESOURCE_VBI
))
2912 return rc
| EPOLLERR
;
2913 return rc
| videobuf_poll_stream(file
, &fh
->vbi
, wait
);
2916 if (check_btres(fh
,RESOURCE_VIDEO_STREAM
)) {
2917 /* streaming capture */
2918 if (list_empty(&fh
->cap
.stream
))
2919 return rc
| EPOLLERR
;
2920 buf
= list_entry(fh
->cap
.stream
.next
,struct bttv_buffer
,vb
.stream
);
2922 /* read() capture */
2923 if (NULL
== fh
->cap
.read_buf
) {
2924 /* need to capture a new frame */
2925 if (locked_btres(fh
->btv
,RESOURCE_VIDEO_STREAM
))
2926 return rc
| EPOLLERR
;
2927 fh
->cap
.read_buf
= videobuf_sg_alloc(fh
->cap
.msize
);
2928 if (NULL
== fh
->cap
.read_buf
)
2929 return rc
| EPOLLERR
;
2930 fh
->cap
.read_buf
->memory
= V4L2_MEMORY_USERPTR
;
2931 field
= videobuf_next_field(&fh
->cap
);
2932 if (0 != fh
->cap
.ops
->buf_prepare(&fh
->cap
,fh
->cap
.read_buf
,field
)) {
2933 kfree (fh
->cap
.read_buf
);
2934 fh
->cap
.read_buf
= NULL
;
2935 return rc
| EPOLLERR
;
2937 fh
->cap
.ops
->buf_queue(&fh
->cap
,fh
->cap
.read_buf
);
2938 fh
->cap
.read_off
= 0;
2940 buf
= (struct bttv_buffer
*)fh
->cap
.read_buf
;
2943 poll_wait(file
, &buf
->vb
.done
, wait
);
2944 if (buf
->vb
.state
== VIDEOBUF_DONE
||
2945 buf
->vb
.state
== VIDEOBUF_ERROR
)
2946 rc
= rc
| EPOLLIN
|EPOLLRDNORM
;
2950 static int bttv_open(struct file
*file
)
2952 struct video_device
*vdev
= video_devdata(file
);
2953 struct bttv
*btv
= video_drvdata(file
);
2955 enum v4l2_buf_type type
= 0;
2957 dprintk("open dev=%s\n", video_device_node_name(vdev
));
2959 if (vdev
->vfl_type
== VFL_TYPE_VIDEO
) {
2960 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2961 } else if (vdev
->vfl_type
== VFL_TYPE_VBI
) {
2962 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
2968 dprintk("%d: open called (type=%s)\n",
2969 btv
->c
.nr
, v4l2_type_names
[type
]);
2971 /* allocate per filehandle data */
2972 fh
= kmalloc(sizeof(*fh
), GFP_KERNEL
);
2976 file
->private_data
= fh
;
2979 v4l2_fh_init(&fh
->fh
, vdev
);
2982 fh
->ov
.setup_ok
= 0;
2984 videobuf_queue_sg_init(&fh
->cap
, &bttv_video_qops
,
2985 &btv
->c
.pci
->dev
, &btv
->s_lock
,
2986 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
2987 V4L2_FIELD_INTERLACED
,
2988 sizeof(struct bttv_buffer
),
2990 videobuf_queue_sg_init(&fh
->vbi
, &bttv_vbi_qops
,
2991 &btv
->c
.pci
->dev
, &btv
->s_lock
,
2992 V4L2_BUF_TYPE_VBI_CAPTURE
,
2994 sizeof(struct bttv_buffer
),
2996 set_tvnorm(btv
,btv
->tvnorm
);
2997 set_input(btv
, btv
->input
, btv
->tvnorm
);
2998 audio_mute(btv
, btv
->mute
);
3000 /* The V4L2 spec requires one global set of cropping parameters
3001 which only change on request. These are stored in btv->crop[1].
3002 However for compatibility with V4L apps and cropping unaware
3003 V4L2 apps we now reset the cropping parameters as seen through
3004 this fh, which is to say VIDIOC_G_SELECTION and scaling limit checks
3005 will use btv->crop[0], the default cropping parameters for the
3006 current video standard, and VIDIOC_S_FMT will not implicitly
3007 change the cropping parameters until VIDIOC_S_SELECTION has been
3009 fh
->do_crop
= !reset_crop
; /* module parameter */
3011 /* Likewise there should be one global set of VBI capture
3012 parameters, but for compatibility with V4L apps and earlier
3013 driver versions each fh has its own parameters. */
3014 bttv_vbi_fmt_reset(&fh
->vbi_fmt
, btv
->tvnorm
);
3016 bttv_field_count(btv
);
3017 v4l2_fh_add(&fh
->fh
);
3021 static int bttv_release(struct file
*file
)
3023 struct bttv_fh
*fh
= file
->private_data
;
3024 struct bttv
*btv
= fh
->btv
;
3026 /* turn off overlay */
3027 if (check_btres(fh
, RESOURCE_OVERLAY
))
3028 bttv_switch_overlay(btv
,fh
,NULL
);
3030 /* stop video capture */
3031 if (check_btres(fh
, RESOURCE_VIDEO_STREAM
)) {
3032 videobuf_streamoff(&fh
->cap
);
3033 free_btres_lock(btv
,fh
,RESOURCE_VIDEO_STREAM
);
3035 if (fh
->cap
.read_buf
) {
3036 buffer_release(&fh
->cap
,fh
->cap
.read_buf
);
3037 kfree(fh
->cap
.read_buf
);
3039 if (check_btres(fh
, RESOURCE_VIDEO_READ
)) {
3040 free_btres_lock(btv
, fh
, RESOURCE_VIDEO_READ
);
3043 /* stop vbi capture */
3044 if (check_btres(fh
, RESOURCE_VBI
)) {
3045 videobuf_stop(&fh
->vbi
);
3046 free_btres_lock(btv
,fh
,RESOURCE_VBI
);
3051 videobuf_mmap_free(&fh
->cap
);
3052 videobuf_mmap_free(&fh
->vbi
);
3053 file
->private_data
= NULL
;
3056 bttv_field_count(btv
);
3059 audio_mute(btv
, btv
->mute
);
3061 v4l2_fh_del(&fh
->fh
);
3062 v4l2_fh_exit(&fh
->fh
);
3068 bttv_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3070 struct bttv_fh
*fh
= file
->private_data
;
3072 dprintk("%d: mmap type=%s 0x%lx+%ld\n",
3073 fh
->btv
->c
.nr
, v4l2_type_names
[fh
->type
],
3074 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
3075 return videobuf_mmap_mapper(bttv_queue(fh
),vma
);
3078 static const struct v4l2_file_operations bttv_fops
=
3080 .owner
= THIS_MODULE
,
3082 .release
= bttv_release
,
3083 .unlocked_ioctl
= video_ioctl2
,
3089 static const struct v4l2_ioctl_ops bttv_ioctl_ops
= {
3090 .vidioc_querycap
= bttv_querycap
,
3091 .vidioc_enum_fmt_vid_cap
= bttv_enum_fmt_vid_cap
,
3092 .vidioc_g_fmt_vid_cap
= bttv_g_fmt_vid_cap
,
3093 .vidioc_try_fmt_vid_cap
= bttv_try_fmt_vid_cap
,
3094 .vidioc_s_fmt_vid_cap
= bttv_s_fmt_vid_cap
,
3095 .vidioc_enum_fmt_vid_overlay
= bttv_enum_fmt_vid_overlay
,
3096 .vidioc_g_fmt_vid_overlay
= bttv_g_fmt_vid_overlay
,
3097 .vidioc_try_fmt_vid_overlay
= bttv_try_fmt_vid_overlay
,
3098 .vidioc_s_fmt_vid_overlay
= bttv_s_fmt_vid_overlay
,
3099 .vidioc_g_fmt_vbi_cap
= bttv_g_fmt_vbi_cap
,
3100 .vidioc_try_fmt_vbi_cap
= bttv_try_fmt_vbi_cap
,
3101 .vidioc_s_fmt_vbi_cap
= bttv_s_fmt_vbi_cap
,
3102 .vidioc_g_pixelaspect
= bttv_g_pixelaspect
,
3103 .vidioc_reqbufs
= bttv_reqbufs
,
3104 .vidioc_querybuf
= bttv_querybuf
,
3105 .vidioc_qbuf
= bttv_qbuf
,
3106 .vidioc_dqbuf
= bttv_dqbuf
,
3107 .vidioc_s_std
= bttv_s_std
,
3108 .vidioc_g_std
= bttv_g_std
,
3109 .vidioc_enum_input
= bttv_enum_input
,
3110 .vidioc_g_input
= bttv_g_input
,
3111 .vidioc_s_input
= bttv_s_input
,
3112 .vidioc_streamon
= bttv_streamon
,
3113 .vidioc_streamoff
= bttv_streamoff
,
3114 .vidioc_g_tuner
= bttv_g_tuner
,
3115 .vidioc_s_tuner
= bttv_s_tuner
,
3116 .vidioc_g_selection
= bttv_g_selection
,
3117 .vidioc_s_selection
= bttv_s_selection
,
3118 .vidioc_g_fbuf
= bttv_g_fbuf
,
3119 .vidioc_s_fbuf
= bttv_s_fbuf
,
3120 .vidioc_overlay
= bttv_overlay
,
3121 .vidioc_g_parm
= bttv_g_parm
,
3122 .vidioc_g_frequency
= bttv_g_frequency
,
3123 .vidioc_s_frequency
= bttv_s_frequency
,
3124 .vidioc_log_status
= bttv_log_status
,
3125 .vidioc_querystd
= bttv_querystd
,
3126 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
3127 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
3128 #ifdef CONFIG_VIDEO_ADV_DEBUG
3129 .vidioc_g_register
= bttv_g_register
,
3130 .vidioc_s_register
= bttv_s_register
,
3134 static struct video_device bttv_video_template
= {
3136 .ioctl_ops
= &bttv_ioctl_ops
,
3137 .tvnorms
= BTTV_NORMS
,
3140 /* ----------------------------------------------------------------------- */
3141 /* radio interface */
3143 static int radio_open(struct file
*file
)
3145 struct video_device
*vdev
= video_devdata(file
);
3146 struct bttv
*btv
= video_drvdata(file
);
3149 dprintk("open dev=%s\n", video_device_node_name(vdev
));
3151 dprintk("%d: open called (radio)\n", btv
->c
.nr
);
3153 /* allocate per filehandle data */
3154 fh
= kmalloc(sizeof(*fh
), GFP_KERNEL
);
3157 file
->private_data
= fh
;
3159 v4l2_fh_init(&fh
->fh
, vdev
);
3162 audio_mute(btv
, btv
->mute
);
3164 v4l2_fh_add(&fh
->fh
);
3169 static int radio_release(struct file
*file
)
3171 struct bttv_fh
*fh
= file
->private_data
;
3172 struct bttv
*btv
= fh
->btv
;
3173 struct saa6588_command cmd
;
3175 file
->private_data
= NULL
;
3176 v4l2_fh_del(&fh
->fh
);
3177 v4l2_fh_exit(&fh
->fh
);
3182 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_CLOSE
, &cmd
);
3184 if (btv
->radio_user
== 0)
3185 btv
->has_radio_tuner
= 0;
3189 static int radio_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
3191 struct bttv_fh
*fh
= priv
;
3192 struct bttv
*btv
= fh
->btv
;
3196 strscpy(t
->name
, "Radio", sizeof(t
->name
));
3197 t
->type
= V4L2_TUNER_RADIO
;
3200 bttv_call_all(btv
, tuner
, g_tuner
, t
);
3202 if (btv
->audio_mode_gpio
)
3203 btv
->audio_mode_gpio(btv
, t
, 0);
3205 if (btv
->has_tea575x
)
3206 return snd_tea575x_g_tuner(&btv
->tea
, t
);
3211 static int radio_s_tuner(struct file
*file
, void *priv
,
3212 const struct v4l2_tuner
*t
)
3214 struct bttv_fh
*fh
= priv
;
3215 struct bttv
*btv
= fh
->btv
;
3221 bttv_call_all(btv
, tuner
, s_tuner
, t
);
3225 static int radio_s_hw_freq_seek(struct file
*file
, void *priv
,
3226 const struct v4l2_hw_freq_seek
*a
)
3228 struct bttv_fh
*fh
= priv
;
3229 struct bttv
*btv
= fh
->btv
;
3231 if (btv
->has_tea575x
)
3232 return snd_tea575x_s_hw_freq_seek(file
, &btv
->tea
, a
);
3237 static int radio_enum_freq_bands(struct file
*file
, void *priv
,
3238 struct v4l2_frequency_band
*band
)
3240 struct bttv_fh
*fh
= priv
;
3241 struct bttv
*btv
= fh
->btv
;
3243 if (btv
->has_tea575x
)
3244 return snd_tea575x_enum_freq_bands(&btv
->tea
, band
);
3249 static ssize_t
radio_read(struct file
*file
, char __user
*data
,
3250 size_t count
, loff_t
*ppos
)
3252 struct bttv_fh
*fh
= file
->private_data
;
3253 struct bttv
*btv
= fh
->btv
;
3254 struct saa6588_command cmd
;
3256 cmd
.block_count
= count
/ 3;
3257 cmd
.nonblocking
= file
->f_flags
& O_NONBLOCK
;
3259 cmd
.instance
= file
;
3260 cmd
.result
= -ENODEV
;
3263 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_READ
, &cmd
);
3268 static __poll_t
radio_poll(struct file
*file
, poll_table
*wait
)
3270 struct bttv_fh
*fh
= file
->private_data
;
3271 struct bttv
*btv
= fh
->btv
;
3272 __poll_t req_events
= poll_requested_events(wait
);
3273 struct saa6588_command cmd
;
3276 if (v4l2_event_pending(&fh
->fh
))
3278 else if (req_events
& EPOLLPRI
)
3279 poll_wait(file
, &fh
->fh
.wait
, wait
);
3281 cmd
.instance
= file
;
3282 cmd
.event_list
= wait
;
3283 cmd
.poll_mask
= res
;
3284 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_POLL
, &cmd
);
3286 return cmd
.poll_mask
;
3289 static const struct v4l2_file_operations radio_fops
=
3291 .owner
= THIS_MODULE
,
3294 .release
= radio_release
,
3295 .unlocked_ioctl
= video_ioctl2
,
3299 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
3300 .vidioc_querycap
= bttv_querycap
,
3301 .vidioc_log_status
= bttv_log_status
,
3302 .vidioc_g_tuner
= radio_g_tuner
,
3303 .vidioc_s_tuner
= radio_s_tuner
,
3304 .vidioc_g_frequency
= bttv_g_frequency
,
3305 .vidioc_s_frequency
= bttv_s_frequency
,
3306 .vidioc_s_hw_freq_seek
= radio_s_hw_freq_seek
,
3307 .vidioc_enum_freq_bands
= radio_enum_freq_bands
,
3308 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
3309 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
3312 static struct video_device radio_template
= {
3313 .fops
= &radio_fops
,
3314 .ioctl_ops
= &radio_ioctl_ops
,
3317 /* ----------------------------------------------------------------------- */
3318 /* some debug code */
3320 static int bttv_risc_decode(u32 risc
)
3322 static char *instr
[16] = {
3323 [ BT848_RISC_WRITE
>> 28 ] = "write",
3324 [ BT848_RISC_SKIP
>> 28 ] = "skip",
3325 [ BT848_RISC_WRITEC
>> 28 ] = "writec",
3326 [ BT848_RISC_JUMP
>> 28 ] = "jump",
3327 [ BT848_RISC_SYNC
>> 28 ] = "sync",
3328 [ BT848_RISC_WRITE123
>> 28 ] = "write123",
3329 [ BT848_RISC_SKIP123
>> 28 ] = "skip123",
3330 [ BT848_RISC_WRITE1S23
>> 28 ] = "write1s23",
3332 static int incr
[16] = {
3333 [ BT848_RISC_WRITE
>> 28 ] = 2,
3334 [ BT848_RISC_JUMP
>> 28 ] = 2,
3335 [ BT848_RISC_SYNC
>> 28 ] = 2,
3336 [ BT848_RISC_WRITE123
>> 28 ] = 5,
3337 [ BT848_RISC_SKIP123
>> 28 ] = 2,
3338 [ BT848_RISC_WRITE1S23
>> 28 ] = 3,
3340 static char *bits
[] = {
3341 "be0", "be1", "be2", "be3/resync",
3342 "set0", "set1", "set2", "set3",
3343 "clr0", "clr1", "clr2", "clr3",
3344 "irq", "res", "eol", "sol",
3348 pr_cont("0x%08x [ %s", risc
,
3349 instr
[risc
>> 28] ? instr
[risc
>> 28] : "INVALID");
3350 for (i
= ARRAY_SIZE(bits
)-1; i
>= 0; i
--)
3351 if (risc
& (1 << (i
+ 12)))
3352 pr_cont(" %s", bits
[i
]);
3353 pr_cont(" count=%d ]\n", risc
& 0xfff);
3354 return incr
[risc
>> 28] ? incr
[risc
>> 28] : 1;
3357 static void bttv_risc_disasm(struct bttv
*btv
,
3358 struct btcx_riscmem
*risc
)
3362 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
3363 btv
->c
.v4l2_dev
.name
, risc
->cpu
, (unsigned long)risc
->dma
);
3364 for (i
= 0; i
< (risc
->size
>> 2); i
+= n
) {
3365 pr_info("%s: 0x%lx: ",
3366 btv
->c
.v4l2_dev
.name
,
3367 (unsigned long)(risc
->dma
+ (i
<<2)));
3368 n
= bttv_risc_decode(le32_to_cpu(risc
->cpu
[i
]));
3369 for (j
= 1; j
< n
; j
++)
3370 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3371 btv
->c
.v4l2_dev
.name
,
3372 (unsigned long)(risc
->dma
+ ((i
+j
)<<2)),
3374 if (0 == risc
->cpu
[i
])
3379 static void bttv_print_riscaddr(struct bttv
*btv
)
3381 pr_info(" main: %08llx\n", (unsigned long long)btv
->main
.dma
);
3382 pr_info(" vbi : o=%08llx e=%08llx\n",
3383 btv
->cvbi
? (unsigned long long)btv
->cvbi
->top
.dma
: 0,
3384 btv
->cvbi
? (unsigned long long)btv
->cvbi
->bottom
.dma
: 0);
3385 pr_info(" cap : o=%08llx e=%08llx\n",
3387 ? (unsigned long long)btv
->curr
.top
->top
.dma
: 0,
3389 ? (unsigned long long)btv
->curr
.bottom
->bottom
.dma
: 0);
3390 pr_info(" scr : o=%08llx e=%08llx\n",
3391 btv
->screen
? (unsigned long long)btv
->screen
->top
.dma
: 0,
3392 btv
->screen
? (unsigned long long)btv
->screen
->bottom
.dma
: 0);
3393 bttv_risc_disasm(btv
, &btv
->main
);
3396 /* ----------------------------------------------------------------------- */
3399 static char *irq_name
[] = {
3400 "FMTCHG", // format change detected (525 vs. 625)
3401 "VSYNC", // vertical sync (new field)
3402 "HSYNC", // horizontal sync
3403 "OFLOW", // chroma/luma AGC overflow
3404 "HLOCK", // horizontal lock changed
3405 "VPRES", // video presence changed
3407 "I2CDONE", // hw irc operation finished
3408 "GPINT", // gpio port triggered irq
3410 "RISCI", // risc instruction triggered irq
3411 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3412 "FTRGT", // pixel data fifo overrun
3413 "FDSR", // fifo data stream resyncronisation
3414 "PPERR", // parity error (data transfer)
3415 "RIPERR", // parity error (read risc instructions)
3416 "PABORT", // pci abort
3417 "OCERR", // risc instruction error
3418 "SCERR", // syncronisation error
3421 static void bttv_print_irqbits(u32 print
, u32 mark
)
3426 for (i
= 0; i
< ARRAY_SIZE(irq_name
); i
++) {
3427 if (print
& (1 << i
))
3428 pr_cont(" %s", irq_name
[i
]);
3429 if (mark
& (1 << i
))
3434 static void bttv_irq_debug_low_latency(struct bttv
*btv
, u32 rc
)
3436 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3438 (unsigned long)btv
->main
.dma
,
3439 (unsigned long)le32_to_cpu(btv
->main
.cpu
[RISC_SLOT_O_VBI
+1]),
3440 (unsigned long)le32_to_cpu(btv
->main
.cpu
[RISC_SLOT_O_FIELD
+1]),
3443 if (0 == (btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
)) {
3444 pr_notice("%d: Oh, there (temporarily?) is no input signal. Ok, then this is harmless, don't worry ;)\n",
3448 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
3450 pr_notice("%d: Lets try to catch the culprit red-handed ...\n",
3456 bttv_irq_next_video(struct bttv
*btv
, struct bttv_buffer_set
*set
)
3458 struct bttv_buffer
*item
;
3460 memset(set
,0,sizeof(*set
));
3462 /* capture request ? */
3463 if (!list_empty(&btv
->capture
)) {
3465 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
3466 if (V4L2_FIELD_HAS_TOP(item
->vb
.field
))
3468 if (V4L2_FIELD_HAS_BOTTOM(item
->vb
.field
))
3471 /* capture request for other field ? */
3472 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
) &&
3473 (item
->vb
.queue
.next
!= &btv
->capture
)) {
3474 item
= list_entry(item
->vb
.queue
.next
, struct bttv_buffer
, vb
.queue
);
3475 /* Mike Isely <isely@pobox.com> - Only check
3476 * and set up the bottom field in the logic
3477 * below. Don't ever do the top field. This
3478 * of course means that if we set up the
3479 * bottom field in the above code that we'll
3480 * actually skip a field. But that's OK.
3481 * Having processed only a single buffer this
3482 * time, then the next time around the first
3483 * available buffer should be for a top field.
3484 * That will then cause us here to set up a
3485 * top then a bottom field in the normal way.
3486 * The alternative to this understanding is
3487 * that we set up the second available buffer
3488 * as a top field, but that's out of order
3489 * since this driver always processes the top
3490 * field first - the effect will be the two
3491 * buffers being returned in the wrong order,
3492 * with the second buffer also being delayed
3493 * by one field time (owing to the fifo nature
3494 * of videobuf). Worse still, we'll be stuck
3495 * doing fields out of order now every time
3496 * until something else causes a field to be
3497 * dropped. By effectively forcing a field to
3498 * drop this way then we always get back into
3499 * sync within a single frame time. (Out of
3500 * order fields can screw up deinterlacing
3502 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
)) {
3503 if (NULL
== set
->bottom
&&
3504 V4L2_FIELD_BOTTOM
== item
->vb
.field
) {
3507 if (NULL
!= set
->top
&& NULL
!= set
->bottom
)
3513 /* screen overlay ? */
3514 if (NULL
!= btv
->screen
) {
3515 if (V4L2_FIELD_HAS_BOTH(btv
->screen
->vb
.field
)) {
3516 if (NULL
== set
->top
&& NULL
== set
->bottom
) {
3517 set
->top
= btv
->screen
;
3518 set
->bottom
= btv
->screen
;
3521 if (V4L2_FIELD_TOP
== btv
->screen
->vb
.field
&&
3523 set
->top
= btv
->screen
;
3525 if (V4L2_FIELD_BOTTOM
== btv
->screen
->vb
.field
&&
3526 NULL
== set
->bottom
) {
3527 set
->bottom
= btv
->screen
;
3532 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3533 btv
->c
.nr
, set
->top
, set
->bottom
,
3534 btv
->screen
, set
->frame_irq
, set
->top_irq
);
3539 bttv_irq_wakeup_video(struct bttv
*btv
, struct bttv_buffer_set
*wakeup
,
3540 struct bttv_buffer_set
*curr
, unsigned int state
)
3542 u64 ts
= ktime_get_ns();
3544 if (wakeup
->top
== wakeup
->bottom
) {
3545 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
3547 pr_debug("%d: wakeup: both=%p\n",
3548 btv
->c
.nr
, wakeup
->top
);
3549 wakeup
->top
->vb
.ts
= ts
;
3550 wakeup
->top
->vb
.field_count
= btv
->field_count
;
3551 wakeup
->top
->vb
.state
= state
;
3552 wake_up(&wakeup
->top
->vb
.done
);
3555 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
3557 pr_debug("%d: wakeup: top=%p\n",
3558 btv
->c
.nr
, wakeup
->top
);
3559 wakeup
->top
->vb
.ts
= ts
;
3560 wakeup
->top
->vb
.field_count
= btv
->field_count
;
3561 wakeup
->top
->vb
.state
= state
;
3562 wake_up(&wakeup
->top
->vb
.done
);
3564 if (NULL
!= wakeup
->bottom
&& curr
->bottom
!= wakeup
->bottom
) {
3566 pr_debug("%d: wakeup: bottom=%p\n",
3567 btv
->c
.nr
, wakeup
->bottom
);
3568 wakeup
->bottom
->vb
.ts
= ts
;
3569 wakeup
->bottom
->vb
.field_count
= btv
->field_count
;
3570 wakeup
->bottom
->vb
.state
= state
;
3571 wake_up(&wakeup
->bottom
->vb
.done
);
3577 bttv_irq_wakeup_vbi(struct bttv
*btv
, struct bttv_buffer
*wakeup
,
3583 wakeup
->vb
.ts
= ktime_get_ns();
3584 wakeup
->vb
.field_count
= btv
->field_count
;
3585 wakeup
->vb
.state
= state
;
3586 wake_up(&wakeup
->vb
.done
);
3589 static void bttv_irq_timeout(struct timer_list
*t
)
3591 struct bttv
*btv
= from_timer(btv
, t
, timeout
);
3592 struct bttv_buffer_set old
,new;
3593 struct bttv_buffer
*ovbi
;
3594 struct bttv_buffer
*item
;
3595 unsigned long flags
;
3598 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3599 btv
->c
.nr
, btv
->framedrop
, btv
->irq_me
, btv
->irq_total
,
3600 btread(BT848_RISC_COUNT
));
3601 bttv_print_irqbits(btread(BT848_INT_STAT
),0);
3605 spin_lock_irqsave(&btv
->s_lock
,flags
);
3607 /* deactivate stuff */
3608 memset(&new,0,sizeof(new));
3614 bttv_buffer_activate_video(btv
, &new);
3615 bttv_buffer_activate_vbi(btv
, NULL
);
3616 bttv_set_dma(btv
, 0);
3619 bttv_irq_wakeup_video(btv
, &old
, &new, VIDEOBUF_ERROR
);
3620 bttv_irq_wakeup_vbi(btv
, ovbi
, VIDEOBUF_ERROR
);
3622 /* cancel all outstanding capture / vbi requests */
3623 while (!list_empty(&btv
->capture
)) {
3624 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
3625 list_del(&item
->vb
.queue
);
3626 item
->vb
.state
= VIDEOBUF_ERROR
;
3627 wake_up(&item
->vb
.done
);
3629 while (!list_empty(&btv
->vcapture
)) {
3630 item
= list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
3631 list_del(&item
->vb
.queue
);
3632 item
->vb
.state
= VIDEOBUF_ERROR
;
3633 wake_up(&item
->vb
.done
);
3637 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
3641 bttv_irq_wakeup_top(struct bttv
*btv
)
3643 struct bttv_buffer
*wakeup
= btv
->curr
.top
;
3648 spin_lock(&btv
->s_lock
);
3649 btv
->curr
.top_irq
= 0;
3650 btv
->curr
.top
= NULL
;
3651 bttv_risc_hook(btv
, RISC_SLOT_O_FIELD
, NULL
, 0);
3653 wakeup
->vb
.ts
= ktime_get_ns();
3654 wakeup
->vb
.field_count
= btv
->field_count
;
3655 wakeup
->vb
.state
= VIDEOBUF_DONE
;
3656 wake_up(&wakeup
->vb
.done
);
3657 spin_unlock(&btv
->s_lock
);
3660 static inline int is_active(struct btcx_riscmem
*risc
, u32 rc
)
3664 if (rc
> risc
->dma
+ risc
->size
)
3670 bttv_irq_switch_video(struct bttv
*btv
)
3672 struct bttv_buffer_set
new;
3673 struct bttv_buffer_set old
;
3676 spin_lock(&btv
->s_lock
);
3678 /* new buffer set */
3679 bttv_irq_next_video(btv
, &new);
3680 rc
= btread(BT848_RISC_COUNT
);
3681 if ((btv
->curr
.top
&& is_active(&btv
->curr
.top
->top
, rc
)) ||
3682 (btv
->curr
.bottom
&& is_active(&btv
->curr
.bottom
->bottom
, rc
))) {
3685 bttv_irq_debug_low_latency(btv
, rc
);
3686 spin_unlock(&btv
->s_lock
);
3693 btv
->loop_irq
&= ~1;
3694 bttv_buffer_activate_video(btv
, &new);
3695 bttv_set_dma(btv
, 0);
3698 if (UNSET
!= btv
->new_input
) {
3699 video_mux(btv
,btv
->new_input
);
3700 btv
->new_input
= UNSET
;
3703 /* wake up finished buffers */
3704 bttv_irq_wakeup_video(btv
, &old
, &new, VIDEOBUF_DONE
);
3705 spin_unlock(&btv
->s_lock
);
3709 bttv_irq_switch_vbi(struct bttv
*btv
)
3711 struct bttv_buffer
*new = NULL
;
3712 struct bttv_buffer
*old
;
3715 spin_lock(&btv
->s_lock
);
3717 if (!list_empty(&btv
->vcapture
))
3718 new = list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
3721 rc
= btread(BT848_RISC_COUNT
);
3722 if (NULL
!= old
&& (is_active(&old
->top
, rc
) ||
3723 is_active(&old
->bottom
, rc
))) {
3726 bttv_irq_debug_low_latency(btv
, rc
);
3727 spin_unlock(&btv
->s_lock
);
3733 btv
->loop_irq
&= ~4;
3734 bttv_buffer_activate_vbi(btv
, new);
3735 bttv_set_dma(btv
, 0);
3737 bttv_irq_wakeup_vbi(btv
, old
, VIDEOBUF_DONE
);
3738 spin_unlock(&btv
->s_lock
);
3741 static irqreturn_t
bttv_irq(int irq
, void *dev_id
)
3749 btv
=(struct bttv
*)dev_id
;
3753 /* get/clear interrupt status bits */
3754 stat
=btread(BT848_INT_STAT
);
3755 astat
=stat
&btread(BT848_INT_MASK
);
3759 btwrite(stat
,BT848_INT_STAT
);
3761 /* get device status bits */
3762 dstat
=btread(BT848_DSTATUS
);
3765 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
3766 btv
->c
.nr
, count
, btv
->field_count
,
3767 stat
>>28, btread(BT848_RISC_COUNT
));
3768 bttv_print_irqbits(stat
,astat
);
3769 if (stat
& BT848_INT_HLOCK
)
3770 pr_cont(" HLOC => %s",
3771 dstat
& BT848_DSTATUS_HLOC
3773 if (stat
& BT848_INT_VPRES
)
3774 pr_cont(" PRES => %s",
3775 dstat
& BT848_DSTATUS_PRES
3777 if (stat
& BT848_INT_FMTCHG
)
3778 pr_cont(" NUML => %s",
3779 dstat
& BT848_DSTATUS_NUML
3784 if (astat
&BT848_INT_VSYNC
)
3787 if ((astat
& BT848_INT_GPINT
) && btv
->remote
) {
3788 bttv_input_irq(btv
);
3791 if (astat
& BT848_INT_I2CDONE
) {
3792 btv
->i2c_done
= stat
;
3793 wake_up(&btv
->i2c_queue
);
3796 if ((astat
& BT848_INT_RISCI
) && (stat
& (4<<28)))
3797 bttv_irq_switch_vbi(btv
);
3799 if ((astat
& BT848_INT_RISCI
) && (stat
& (2<<28)))
3800 bttv_irq_wakeup_top(btv
);
3802 if ((astat
& BT848_INT_RISCI
) && (stat
& (1<<28)))
3803 bttv_irq_switch_video(btv
);
3805 if ((astat
& BT848_INT_HLOCK
) && btv
->opt_automute
)
3806 /* trigger automute */
3807 audio_mux_gpio(btv
, btv
->audio_input
, btv
->mute
);
3809 if (astat
& (BT848_INT_SCERR
|BT848_INT_OCERR
)) {
3810 pr_info("%d: %s%s @ %08x,",
3812 (astat
& BT848_INT_SCERR
) ? "SCERR" : "",
3813 (astat
& BT848_INT_OCERR
) ? "OCERR" : "",
3814 btread(BT848_RISC_COUNT
));
3815 bttv_print_irqbits(stat
,astat
);
3818 bttv_print_riscaddr(btv
);
3820 if (fdsr
&& astat
& BT848_INT_FDSR
) {
3821 pr_info("%d: FDSR @ %08x\n",
3822 btv
->c
.nr
, btread(BT848_RISC_COUNT
));
3824 bttv_print_riscaddr(btv
);
3830 if (count
> 8 || !(astat
& BT848_INT_GPINT
)) {
3831 btwrite(0, BT848_INT_MASK
);
3833 pr_err("%d: IRQ lockup, cleared int mask [",
3836 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
3839 btwrite(btread(BT848_INT_MASK
) & (-1 ^ BT848_INT_GPINT
),
3843 bttv_print_irqbits(stat
,astat
);
3851 return IRQ_RETVAL(handled
);
3855 /* ----------------------------------------------------------------------- */
3856 /* initialization */
3858 static void vdev_init(struct bttv
*btv
,
3859 struct video_device
*vfd
,
3860 const struct video_device
*template,
3861 const char *type_name
)
3864 vfd
->v4l2_dev
= &btv
->c
.v4l2_dev
;
3865 vfd
->release
= video_device_release_empty
;
3866 video_set_drvdata(vfd
, btv
);
3867 snprintf(vfd
->name
, sizeof(vfd
->name
), "BT%d%s %s (%s)",
3868 btv
->id
, (btv
->id
==848 && btv
->revision
==0x12) ? "A" : "",
3869 type_name
, bttv_tvcards
[btv
->c
.type
].name
);
3870 if (btv
->tuner_type
== TUNER_ABSENT
) {
3871 v4l2_disable_ioctl(vfd
, VIDIOC_G_FREQUENCY
);
3872 v4l2_disable_ioctl(vfd
, VIDIOC_S_FREQUENCY
);
3873 v4l2_disable_ioctl(vfd
, VIDIOC_G_TUNER
);
3874 v4l2_disable_ioctl(vfd
, VIDIOC_S_TUNER
);
3878 static void bttv_unregister_video(struct bttv
*btv
)
3880 video_unregister_device(&btv
->video_dev
);
3881 video_unregister_device(&btv
->vbi_dev
);
3882 video_unregister_device(&btv
->radio_dev
);
3885 /* register video4linux devices */
3886 static int bttv_register_video(struct bttv
*btv
)
3889 pr_notice("Overlay support disabled\n");
3892 vdev_init(btv
, &btv
->video_dev
, &bttv_video_template
, "video");
3893 btv
->video_dev
.device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_TUNER
|
3894 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
3895 if (btv
->tuner_type
!= TUNER_ABSENT
)
3896 btv
->video_dev
.device_caps
|= V4L2_CAP_TUNER
;
3897 if (no_overlay
<= 0)
3898 btv
->video_dev
.device_caps
|= V4L2_CAP_VIDEO_OVERLAY
;
3900 if (video_register_device(&btv
->video_dev
, VFL_TYPE_VIDEO
,
3901 video_nr
[btv
->c
.nr
]) < 0)
3903 pr_info("%d: registered device %s\n",
3904 btv
->c
.nr
, video_device_node_name(&btv
->video_dev
));
3905 if (device_create_file(&btv
->video_dev
.dev
,
3906 &dev_attr_card
)<0) {
3907 pr_err("%d: device_create_file 'card' failed\n", btv
->c
.nr
);
3912 vdev_init(btv
, &btv
->vbi_dev
, &bttv_video_template
, "vbi");
3913 btv
->vbi_dev
.device_caps
= V4L2_CAP_VBI_CAPTURE
| V4L2_CAP_READWRITE
|
3914 V4L2_CAP_STREAMING
| V4L2_CAP_TUNER
;
3915 if (btv
->tuner_type
!= TUNER_ABSENT
)
3916 btv
->vbi_dev
.device_caps
|= V4L2_CAP_TUNER
;
3918 if (video_register_device(&btv
->vbi_dev
, VFL_TYPE_VBI
,
3919 vbi_nr
[btv
->c
.nr
]) < 0)
3921 pr_info("%d: registered device %s\n",
3922 btv
->c
.nr
, video_device_node_name(&btv
->vbi_dev
));
3924 if (!btv
->has_radio
)
3927 vdev_init(btv
, &btv
->radio_dev
, &radio_template
, "radio");
3928 btv
->radio_dev
.device_caps
= V4L2_CAP_RADIO
| V4L2_CAP_TUNER
;
3929 if (btv
->has_saa6588
)
3930 btv
->radio_dev
.device_caps
|= V4L2_CAP_READWRITE
|
3931 V4L2_CAP_RDS_CAPTURE
;
3932 if (btv
->has_tea575x
)
3933 btv
->radio_dev
.device_caps
|= V4L2_CAP_HW_FREQ_SEEK
;
3934 btv
->radio_dev
.ctrl_handler
= &btv
->radio_ctrl_handler
;
3935 if (video_register_device(&btv
->radio_dev
, VFL_TYPE_RADIO
,
3936 radio_nr
[btv
->c
.nr
]) < 0)
3938 pr_info("%d: registered device %s\n",
3939 btv
->c
.nr
, video_device_node_name(&btv
->radio_dev
));
3945 bttv_unregister_video(btv
);
3950 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3951 /* response on cards with no firmware is not enabled by OF */
3952 static void pci_set_command(struct pci_dev
*dev
)
3954 #if defined(__powerpc__)
3957 pci_read_config_dword(dev
, PCI_COMMAND
, &cmd
);
3958 cmd
= (cmd
| PCI_COMMAND_MEMORY
);
3959 pci_write_config_dword(dev
, PCI_COMMAND
, cmd
);
3963 static int bttv_probe(struct pci_dev
*dev
, const struct pci_device_id
*pci_id
)
3965 struct v4l2_frequency init_freq
= {
3967 .type
= V4L2_TUNER_ANALOG_TV
,
3973 struct v4l2_ctrl_handler
*hdl
;
3975 if (bttv_num
== BTTV_MAX
)
3977 pr_info("Bt8xx card found (%d)\n", bttv_num
);
3978 bttvs
[bttv_num
] = btv
= kzalloc(sizeof(*btv
), GFP_KERNEL
);
3980 pr_err("out of memory\n");
3983 btv
->c
.nr
= bttv_num
;
3984 snprintf(btv
->c
.v4l2_dev
.name
, sizeof(btv
->c
.v4l2_dev
.name
),
3985 "bttv%d", btv
->c
.nr
);
3987 /* initialize structs / fill in defaults */
3988 mutex_init(&btv
->lock
);
3989 spin_lock_init(&btv
->s_lock
);
3990 spin_lock_init(&btv
->gpio_lock
);
3991 init_waitqueue_head(&btv
->i2c_queue
);
3992 INIT_LIST_HEAD(&btv
->c
.subs
);
3993 INIT_LIST_HEAD(&btv
->capture
);
3994 INIT_LIST_HEAD(&btv
->vcapture
);
3996 timer_setup(&btv
->timeout
, bttv_irq_timeout
, 0);
3999 btv
->tuner_type
= UNSET
;
4000 btv
->new_input
= UNSET
;
4001 btv
->has_radio
=radio
[btv
->c
.nr
];
4003 /* pci stuff (init, get irq/mmio, ... */
4005 btv
->id
= dev
->device
;
4006 if (pci_enable_device(dev
)) {
4007 pr_warn("%d: Can't enable device\n", btv
->c
.nr
);
4011 if (dma_set_mask(&dev
->dev
, DMA_BIT_MASK(32))) {
4012 pr_warn("%d: No suitable DMA available\n", btv
->c
.nr
);
4016 if (!request_mem_region(pci_resource_start(dev
,0),
4017 pci_resource_len(dev
,0),
4018 btv
->c
.v4l2_dev
.name
)) {
4019 pr_warn("%d: can't request iomem (0x%llx)\n",
4021 (unsigned long long)pci_resource_start(dev
, 0));
4025 pci_set_master(dev
);
4026 pci_set_command(dev
);
4028 result
= v4l2_device_register(&dev
->dev
, &btv
->c
.v4l2_dev
);
4030 pr_warn("%d: v4l2_device_register() failed\n", btv
->c
.nr
);
4033 hdl
= &btv
->ctrl_handler
;
4034 v4l2_ctrl_handler_init(hdl
, 20);
4035 btv
->c
.v4l2_dev
.ctrl_handler
= hdl
;
4036 v4l2_ctrl_handler_init(&btv
->radio_ctrl_handler
, 6);
4038 btv
->revision
= dev
->revision
;
4039 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &lat
);
4040 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
4041 bttv_num
, btv
->id
, btv
->revision
, pci_name(dev
),
4042 btv
->c
.pci
->irq
, lat
,
4043 (unsigned long long)pci_resource_start(dev
, 0));
4046 btv
->bt848_mmio
= ioremap(pci_resource_start(dev
, 0), 0x1000);
4047 if (NULL
== btv
->bt848_mmio
) {
4048 pr_err("%d: ioremap() failed\n", btv
->c
.nr
);
4056 /* disable irqs, register irq handler */
4057 btwrite(0, BT848_INT_MASK
);
4058 result
= request_irq(btv
->c
.pci
->irq
, bttv_irq
,
4059 IRQF_SHARED
, btv
->c
.v4l2_dev
.name
, (void *)btv
);
4061 pr_err("%d: can't get IRQ %d\n",
4062 bttv_num
, btv
->c
.pci
->irq
);
4066 if (0 != bttv_handle_chipset(btv
)) {
4071 /* init options from insmod args */
4072 btv
->opt_combfilter
= combfilter
;
4073 bttv_ctrl_combfilter
.def
= combfilter
;
4074 bttv_ctrl_lumafilter
.def
= lumafilter
;
4075 btv
->opt_automute
= automute
;
4076 bttv_ctrl_automute
.def
= automute
;
4077 bttv_ctrl_agc_crush
.def
= agc_crush
;
4078 btv
->opt_vcr_hack
= vcr_hack
;
4079 bttv_ctrl_vcr_hack
.def
= vcr_hack
;
4080 bttv_ctrl_whitecrush_upper
.def
= whitecrush_upper
;
4081 bttv_ctrl_whitecrush_lower
.def
= whitecrush_lower
;
4082 btv
->opt_uv_ratio
= uv_ratio
;
4083 bttv_ctrl_uv_ratio
.def
= uv_ratio
;
4084 bttv_ctrl_full_luma
.def
= full_luma_range
;
4085 bttv_ctrl_coring
.def
= coring
;
4087 /* fill struct bttv with some useful defaults */
4088 btv
->init
.btv
= btv
;
4089 btv
->init
.ov
.w
.width
= 320;
4090 btv
->init
.ov
.w
.height
= 240;
4091 btv
->init
.fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
4092 btv
->init
.width
= 320;
4093 btv
->init
.height
= 240;
4094 btv
->init
.ov
.w
.width
= 320;
4095 btv
->init
.ov
.w
.height
= 240;
4096 btv
->init
.ov
.field
= V4L2_FIELD_INTERLACED
;
4099 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4100 V4L2_CID_BRIGHTNESS
, 0, 0xff00, 0x100, 32768);
4101 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4102 V4L2_CID_CONTRAST
, 0, 0xff80, 0x80, 0x6c00);
4103 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4104 V4L2_CID_SATURATION
, 0, 0xff80, 0x80, 32768);
4105 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4106 V4L2_CID_COLOR_KILLER
, 0, 1, 1, 0);
4107 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4108 V4L2_CID_HUE
, 0, 0xff00, 0x100, 32768);
4109 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4110 V4L2_CID_CHROMA_AGC
, 0, 1, 1, !!chroma_agc
);
4111 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4112 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 0);
4113 if (btv
->volume_gpio
)
4114 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4115 V4L2_CID_AUDIO_VOLUME
, 0, 0xff00, 0x100, 0xff00);
4116 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_combfilter
, NULL
);
4117 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_automute
, NULL
);
4118 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_lumafilter
, NULL
);
4119 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_agc_crush
, NULL
);
4120 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_vcr_hack
, NULL
);
4121 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_whitecrush_lower
, NULL
);
4122 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_whitecrush_upper
, NULL
);
4123 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_uv_ratio
, NULL
);
4124 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_full_luma
, NULL
);
4125 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_coring
, NULL
);
4127 /* initialize hardware */
4129 bttv_gpio_tracking(btv
,"pre-init");
4131 bttv_risc_init_main(btv
);
4135 btwrite(0x00, BT848_GPIO_REG_INP
);
4136 btwrite(0x00, BT848_GPIO_OUT_EN
);
4138 bttv_gpio_tracking(btv
,"init");
4140 /* needs to be done before i2c is registered */
4141 bttv_init_card1(btv
);
4143 /* register i2c + gpio */
4146 /* some card-specific stuff (needs working i2c) */
4147 bttv_init_card2(btv
);
4148 bttv_init_tuner(btv
);
4149 if (btv
->tuner_type
!= TUNER_ABSENT
) {
4150 bttv_set_frequency(btv
, &init_freq
);
4151 btv
->radio_freq
= 90500 * 16; /* 90.5Mhz default */
4153 btv
->std
= V4L2_STD_PAL
;
4155 if (!bttv_tvcards
[btv
->c
.type
].no_video
)
4156 v4l2_ctrl_handler_setup(hdl
);
4158 result
= hdl
->error
;
4164 /* register video4linux + input */
4165 if (!bttv_tvcards
[btv
->c
.type
].no_video
) {
4166 v4l2_ctrl_add_handler(&btv
->radio_ctrl_handler
, hdl
,
4167 v4l2_ctrl_radio_filter
, false);
4168 if (btv
->radio_ctrl_handler
.error
) {
4169 result
= btv
->radio_ctrl_handler
.error
;
4172 set_input(btv
, 0, btv
->tvnorm
);
4173 bttv_crop_reset(&btv
->crop
[0], btv
->tvnorm
);
4174 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
4175 disclaim_vbi_lines(btv
);
4176 disclaim_video_lines(btv
);
4177 bttv_register_video(btv
);
4180 /* add subdevices and autoload dvb-bt8xx if needed */
4181 if (bttv_tvcards
[btv
->c
.type
].has_dvb
) {
4182 bttv_sub_add_device(&btv
->c
, "dvb");
4183 request_modules(btv
);
4187 init_bttv_i2c_ir(btv
);
4188 bttv_input_init(btv
);
4191 /* everything is fine */
4196 free_irq(btv
->c
.pci
->irq
,btv
);
4199 v4l2_ctrl_handler_free(&btv
->ctrl_handler
);
4200 v4l2_ctrl_handler_free(&btv
->radio_ctrl_handler
);
4201 v4l2_device_unregister(&btv
->c
.v4l2_dev
);
4204 if (btv
->bt848_mmio
)
4205 iounmap(btv
->bt848_mmio
);
4206 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4207 pci_resource_len(btv
->c
.pci
,0));
4208 pci_disable_device(btv
->c
.pci
);
4211 bttvs
[btv
->c
.nr
] = NULL
;
4216 static void bttv_remove(struct pci_dev
*pci_dev
)
4218 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4219 struct bttv
*btv
= to_bttv(v4l2_dev
);
4222 pr_info("%d: unloading\n", btv
->c
.nr
);
4224 if (bttv_tvcards
[btv
->c
.type
].has_dvb
)
4225 flush_request_modules(btv
);
4227 /* shutdown everything (DMA+IRQs) */
4228 btand(~15, BT848_GPIO_DMA_CTL
);
4229 btwrite(0, BT848_INT_MASK
);
4230 btwrite(~0x0, BT848_INT_STAT
);
4231 btwrite(0x0, BT848_GPIO_OUT_EN
);
4233 bttv_gpio_tracking(btv
,"cleanup");
4235 /* tell gpio modules we are leaving ... */
4237 bttv_input_fini(btv
);
4238 bttv_sub_del_devices(&btv
->c
);
4240 /* unregister i2c_bus + input */
4243 /* unregister video4linux */
4244 bttv_unregister_video(btv
);
4246 /* free allocated memory */
4247 v4l2_ctrl_handler_free(&btv
->ctrl_handler
);
4248 v4l2_ctrl_handler_free(&btv
->radio_ctrl_handler
);
4249 btcx_riscmem_free(btv
->c
.pci
,&btv
->main
);
4251 /* free resources */
4252 free_irq(btv
->c
.pci
->irq
,btv
);
4253 iounmap(btv
->bt848_mmio
);
4254 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4255 pci_resource_len(btv
->c
.pci
,0));
4256 pci_disable_device(btv
->c
.pci
);
4258 v4l2_device_unregister(&btv
->c
.v4l2_dev
);
4259 bttvs
[btv
->c
.nr
] = NULL
;
4265 static int __maybe_unused
bttv_suspend(struct device
*dev
)
4267 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(dev
);
4268 struct bttv
*btv
= to_bttv(v4l2_dev
);
4269 struct bttv_buffer_set idle
;
4270 unsigned long flags
;
4272 dprintk("%d: suspend\n", btv
->c
.nr
);
4274 /* stop dma + irqs */
4275 spin_lock_irqsave(&btv
->s_lock
,flags
);
4276 memset(&idle
, 0, sizeof(idle
));
4277 btv
->state
.video
= btv
->curr
;
4278 btv
->state
.vbi
= btv
->cvbi
;
4279 btv
->state
.loop_irq
= btv
->loop_irq
;
4282 bttv_buffer_activate_video(btv
, &idle
);
4283 bttv_buffer_activate_vbi(btv
, NULL
);
4284 bttv_set_dma(btv
, 0);
4285 btwrite(0, BT848_INT_MASK
);
4286 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4288 /* save bt878 state */
4289 btv
->state
.gpio_enable
= btread(BT848_GPIO_OUT_EN
);
4290 btv
->state
.gpio_data
= gpio_read();
4292 btv
->state
.disabled
= 1;
4296 static int __maybe_unused
bttv_resume(struct device
*dev
)
4298 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(dev
);
4299 struct bttv
*btv
= to_bttv(v4l2_dev
);
4300 unsigned long flags
;
4302 dprintk("%d: resume\n", btv
->c
.nr
);
4304 btv
->state
.disabled
= 0;
4306 /* restore bt878 state */
4307 bttv_reinit_bt848(btv
);
4308 gpio_inout(0xffffff, btv
->state
.gpio_enable
);
4309 gpio_write(btv
->state
.gpio_data
);
4312 spin_lock_irqsave(&btv
->s_lock
,flags
);
4313 btv
->curr
= btv
->state
.video
;
4314 btv
->cvbi
= btv
->state
.vbi
;
4315 btv
->loop_irq
= btv
->state
.loop_irq
;
4316 bttv_buffer_activate_video(btv
, &btv
->curr
);
4317 bttv_buffer_activate_vbi(btv
, btv
->cvbi
);
4318 bttv_set_dma(btv
, 0);
4319 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4323 static const struct pci_device_id bttv_pci_tbl
[] = {
4324 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT848
), 0},
4325 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT849
), 0},
4326 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT878
), 0},
4327 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT879
), 0},
4328 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_FUSION879
), 0},
4332 MODULE_DEVICE_TABLE(pci
, bttv_pci_tbl
);
4334 static SIMPLE_DEV_PM_OPS(bttv_pm_ops
,
4338 static struct pci_driver bttv_pci_driver
= {
4340 .id_table
= bttv_pci_tbl
,
4341 .probe
= bttv_probe
,
4342 .remove
= bttv_remove
,
4343 .driver
.pm
= &bttv_pm_ops
,
4346 static int __init
bttv_init_module(void)
4352 pr_info("driver version %s loaded\n", BTTV_VERSION
);
4353 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
4355 if (gbufsize
> BTTV_MAX_FBUF
)
4356 gbufsize
= BTTV_MAX_FBUF
;
4357 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
4359 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
4360 gbuffers
, gbufsize
>> 10, gbufsize
>> PAGE_SHIFT
);
4362 bttv_check_chipset();
4364 ret
= bus_register(&bttv_sub_bus_type
);
4366 pr_warn("bus_register error: %d\n", ret
);
4369 ret
= pci_register_driver(&bttv_pci_driver
);
4371 bus_unregister(&bttv_sub_bus_type
);
4376 static void __exit
bttv_cleanup_module(void)
4378 pci_unregister_driver(&bttv_pci_driver
);
4379 bus_unregister(&bttv_sub_bus_type
);
4382 module_init(bttv_init_module
);
4383 module_exit(bttv_cleanup_module
);