blob: afcd53bfcf8ec88264c97fd80c75b373e2512810 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
3 bttv - Bt848 frame grabber driver
4
5 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08006 & Marcus Metzler <mocm@thp.uni-koeln.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
8
9 some v4l2 code lines are taken from Justin's bttv2 driver which is
10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
11
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -030012 V4L1 removal from:
13 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
14
Mauro Carvalho Chehabc96dd072007-10-26 16:51:47 -030015 Fixes to be fully V4L2 compliant by
16 (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
17
Michael Schimeke5bd0262007-01-18 16:17:39 -030018 Cropping and overscan support
19 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
20 Sponsored by OPQ Systems AB
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35*/
36
Joe Perches8af443e2011-08-21 19:56:48 -030037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
40#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/fs.h>
45#include <linux/kernel.h>
46#include <linux/sched.h>
47#include <linux/interrupt.h>
48#include <linux/kdev_t.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020049#include "bttvp.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020050#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030051#include <media/v4l2-ioctl.h>
Hans Verkuilae50f0f2013-02-06 12:42:13 -030052#include <media/v4l2-event.h>
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -030053#include <media/tvaudio.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030054#include <media/msp3400.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020055
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -070056#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#include <asm/io.h>
59#include <asm/byteorder.h>
60
Hans Verkuilb9218f22010-12-27 12:22:46 -030061#include <media/saa6588.h>
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070062
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030063#define BTTV_VERSION "0.9.19"
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065unsigned int bttv_num; /* number of Bt848s in use */
Trent Piepho4b10d3b2009-01-28 21:32:59 -030066struct bttv *bttvs[BTTV_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020068unsigned int bttv_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069unsigned int bttv_verbose = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020070unsigned int bttv_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/* config variables */
73#ifdef __BIG_ENDIAN
74static unsigned int bigendian=1;
75#else
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020076static unsigned int bigendian;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
78static unsigned int radio[BTTV_MAX];
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020079static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static unsigned int gbuffers = 8;
81static unsigned int gbufsize = 0x208000;
Michael Schimeke5bd0262007-01-18 16:17:39 -030082static unsigned int reset_crop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Jean Delvare176c2f32008-09-07 12:49:59 -030084static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
85static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
86static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020087static int debug_latency;
Jean Delvared90a4ae2010-02-16 14:22:37 -030088static int disable_ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020090static unsigned int fdsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* options */
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020093static unsigned int combfilter;
94static unsigned int lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static unsigned int automute = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020096static unsigned int chroma_agc;
Hans Verkuil01df5302013-02-06 12:40:28 -030097static unsigned int agc_crush = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static unsigned int whitecrush_upper = 0xCF;
99static unsigned int whitecrush_lower = 0x7F;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200100static unsigned int vcr_hack;
101static unsigned int irq_iswitch;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700102static unsigned int uv_ratio = 50;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200103static unsigned int full_luma_range;
104static unsigned int coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/* API features (turn on/off stuff for testing) */
107static unsigned int v4l2 = 1;
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* insmod args */
110module_param(bttv_verbose, int, 0644);
111module_param(bttv_gpio, int, 0644);
112module_param(bttv_debug, int, 0644);
113module_param(irq_debug, int, 0644);
114module_param(debug_latency, int, 0644);
Jean Delvared90a4ae2010-02-16 14:22:37 -0300115module_param(disable_ir, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117module_param(fdsr, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118module_param(gbuffers, int, 0444);
119module_param(gbufsize, int, 0444);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300120module_param(reset_crop, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122module_param(v4l2, int, 0644);
123module_param(bigendian, int, 0644);
124module_param(irq_iswitch, int, 0644);
125module_param(combfilter, int, 0444);
126module_param(lumafilter, int, 0444);
127module_param(automute, int, 0444);
128module_param(chroma_agc, int, 0444);
Hans Verkuil01df5302013-02-06 12:40:28 -0300129module_param(agc_crush, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130module_param(whitecrush_upper, int, 0444);
131module_param(whitecrush_lower, int, 0444);
132module_param(vcr_hack, int, 0444);
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700133module_param(uv_ratio, int, 0444);
134module_param(full_luma_range, int, 0444);
135module_param(coring, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Jean Delvare176c2f32008-09-07 12:49:59 -0300137module_param_array(radio, int, NULL, 0444);
138module_param_array(video_nr, int, NULL, 0444);
139module_param_array(radio_nr, int, NULL, 0444);
140module_param_array(vbi_nr, int, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Hans Verkuil01df5302013-02-06 12:40:28 -0300142MODULE_PARM_DESC(radio, "The TV card supports radio, default is 0 (no)");
143MODULE_PARM_DESC(bigendian, "byte order of the framebuffer, default is native endian");
144MODULE_PARM_DESC(bttv_verbose, "verbose startup messages, default is 1 (yes)");
145MODULE_PARM_DESC(bttv_gpio, "log gpio changes, default is 0 (no)");
146MODULE_PARM_DESC(bttv_debug, "debug messages, default is 0 (no)");
147MODULE_PARM_DESC(irq_debug, "irq handler debug messages, default is 0 (no)");
Jean Delvared90a4ae2010-02-16 14:22:37 -0300148MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
Hans Verkuil01df5302013-02-06 12:40:28 -0300149MODULE_PARM_DESC(gbuffers, "number of capture buffers. range 2-32, default 8");
150MODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 0x208000");
151MODULE_PARM_DESC(reset_crop, "reset cropping parameters at open(), default "
Michael Schimeke5bd0262007-01-18 16:17:39 -0300152 "is 1 (yes) for compatibility with older applications");
Hans Verkuil01df5302013-02-06 12:40:28 -0300153MODULE_PARM_DESC(automute, "mute audio on bad/missing video signal, default is 1 (yes)");
154MODULE_PARM_DESC(chroma_agc, "enables the AGC of chroma signal, default is 0 (no)");
155MODULE_PARM_DESC(agc_crush, "enables the luminance AGC crush, default is 1 (yes)");
156MODULE_PARM_DESC(whitecrush_upper, "sets the white crush upper value, default is 207");
157MODULE_PARM_DESC(whitecrush_lower, "sets the white crush lower value, default is 127");
158MODULE_PARM_DESC(vcr_hack, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
159MODULE_PARM_DESC(irq_iswitch, "switch inputs in irq handler");
160MODULE_PARM_DESC(uv_ratio, "ratio between u and v gains, default is 50");
161MODULE_PARM_DESC(full_luma_range, "use the full luma range, default is 0 (no)");
162MODULE_PARM_DESC(coring, "set the luma coring level, default is 0 (no)");
Jean Delvare176c2f32008-09-07 12:49:59 -0300163MODULE_PARM_DESC(video_nr, "video device numbers");
164MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
165MODULE_PARM_DESC(radio_nr, "radio device numbers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
168MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
169MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -0300170MODULE_VERSION(BTTV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Hans Verkuil01df5302013-02-06 12:40:28 -0300172#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_USER_BTTV_BASE + 0)
173#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_USER_BTTV_BASE + 1)
174#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_USER_BTTV_BASE + 2)
175#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_USER_BTTV_BASE + 3)
176#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_USER_BTTV_BASE + 4)
177#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_USER_BTTV_BASE + 5)
178#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_USER_BTTV_BASE + 6)
179#define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_USER_BTTV_BASE + 7)
180#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_USER_BTTV_BASE + 8)
181#define V4L2_CID_PRIVATE_CORING (V4L2_CID_USER_BTTV_BASE + 9)
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/* ----------------------------------------------------------------------- */
184/* sysfs */
185
Kay Sievers54bd5b62007-10-08 16:26:13 -0300186static ssize_t show_card(struct device *cd,
187 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300189 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -0300190 struct bttv *btv = video_get_drvdata(vfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
192}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300193static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/* ----------------------------------------------------------------------- */
Jarod Wilsonf992a492007-03-24 15:23:50 -0300196/* dvb auto-load setup */
197#if defined(CONFIG_MODULES) && defined(MODULE)
198static void request_module_async(struct work_struct *work)
199{
200 request_module("dvb-bt8xx");
201}
202
203static void request_modules(struct bttv *dev)
204{
205 INIT_WORK(&dev->request_module_wk, request_module_async);
206 schedule_work(&dev->request_module_wk);
207}
Tejun Heo707bcf32010-12-24 16:14:20 +0100208
209static void flush_request_modules(struct bttv *dev)
210{
Linus Torvalds0b8e74c2012-10-07 17:49:05 +0900211 flush_work(&dev->request_module_wk);
Tejun Heo707bcf32010-12-24 16:14:20 +0100212}
Jarod Wilsonf992a492007-03-24 15:23:50 -0300213#else
214#define request_modules(dev)
Mauro Carvalho Chehaba1fd2872012-10-27 16:33:41 -0300215#define flush_request_modules(dev) do {} while(0)
Jarod Wilsonf992a492007-03-24 15:23:50 -0300216#endif /* CONFIG_MODULES */
217
218
219/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/* static data */
221
222/* special timing tables from conexant... */
223static u8 SRAM_Table[][60] =
224{
225 /* PAL digital input over GPIO[7:0] */
226 {
227 45, // 45 bytes following
228 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
229 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
230 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
231 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
232 0x37,0x00,0xAF,0x21,0x00
233 },
234 /* NTSC digital input over GPIO[7:0] */
235 {
236 51, // 51 bytes following
237 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
238 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
239 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
240 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
241 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
242 0x00,
243 },
244 // TGB_NTSC392 // quartzsight
245 // This table has been modified to be used for Fusion Rev D
246 {
247 0x2A, // size of table = 42
248 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
249 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
250 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
251 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
252 0x20, 0x00
253 }
254};
255
Michael Schimeke5bd0262007-01-18 16:17:39 -0300256/* minhdelayx1 first video pixel we can capture on a line and
257 hdelayx1 start of active video, both relative to rising edge of
258 /HRESET pulse (0H) in 1 / fCLKx1.
259 swidth width of active video and
260 totalwidth total line width, both in 1 / fCLKx1.
261 sqwidth total line width in square pixels.
262 vdelay start of active video in 2 * field lines relative to
263 trailing edge of /VRESET pulse (VDELAY register).
264 sheight height of active video in 2 * field lines.
Hans de Goede35ccece2013-03-25 14:45:54 -0300265 extraheight Added to sheight for cropcap.bounds.height only
Michael Schimeke5bd0262007-01-18 16:17:39 -0300266 videostart0 ITU-R frame line number of the line corresponding
267 to vdelay in the first field. */
268#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
Hans de Goede35ccece2013-03-25 14:45:54 -0300269 vdelay, sheight, extraheight, videostart0) \
Michael Schimeke5bd0262007-01-18 16:17:39 -0300270 .cropcap.bounds.left = minhdelayx1, \
271 /* * 2 because vertically we count field lines times two, */ \
272 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
273 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
274 /* 4 is a safety margin at the end of the line. */ \
275 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
Hans de Goede35ccece2013-03-25 14:45:54 -0300276 .cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) - \
277 MIN_VDELAY, \
Michael Schimeke5bd0262007-01-18 16:17:39 -0300278 .cropcap.defrect.left = hdelayx1, \
279 .cropcap.defrect.top = (videostart0) * 2, \
280 .cropcap.defrect.width = swidth, \
281 .cropcap.defrect.height = sheight, \
282 .cropcap.pixelaspect.numerator = totalwidth, \
283 .cropcap.pixelaspect.denominator = sqwidth,
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285const struct bttv_tvnorm bttv_tvnorms[] = {
286 /* PAL-BDGHI */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800287 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
288 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 {
290 .v4l2_id = V4L2_STD_PAL,
291 .name = "PAL",
292 .Fsc = 35468950,
293 .swidth = 924,
294 .sheight = 576,
295 .totalwidth = 1135,
296 .adelay = 0x7f,
297 .bdelay = 0x72,
298 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
299 .scaledtwidth = 1135,
300 .hdelayx1 = 186,
301 .hactivex1 = 924,
302 .vdelay = 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300303 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 .sram = 0,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200305 /* ITU-R frame line number of the first VBI line
Michael Schimeke5bd0262007-01-18 16:17:39 -0300306 we can capture, of the first and second field.
307 The last line is determined by cropcap.bounds. */
308 .vbistart = { 7, 320 },
309 CROPCAP(/* minhdelayx1 */ 68,
310 /* hdelayx1 */ 186,
311 /* Should be (768 * 1135 + 944 / 2) / 944.
312 cropcap.defrect is used for image width
313 checks, so we keep the old value 924. */
314 /* swidth */ 924,
315 /* totalwidth */ 1135,
316 /* sqwidth */ 944,
317 /* vdelay */ 0x20,
Hans de Goede35ccece2013-03-25 14:45:54 -0300318 /* sheight */ 576,
319 /* bt878 (and bt848?) can capture another
320 line below active video. */
321 /* extraheight */ 2,
Mauro Carvalho Chehaba1fd2872012-10-27 16:33:41 -0300322 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 },{
Hans Verkuild97a11e2006-02-07 06:48:40 -0200324 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 .name = "NTSC",
326 .Fsc = 28636363,
327 .swidth = 768,
328 .sheight = 480,
329 .totalwidth = 910,
330 .adelay = 0x68,
331 .bdelay = 0x5d,
332 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
333 .scaledtwidth = 910,
334 .hdelayx1 = 128,
335 .hactivex1 = 910,
336 .vdelay = 0x1a,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300337 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 .sram = 1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200339 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300340 CROPCAP(/* minhdelayx1 */ 68,
341 /* hdelayx1 */ 128,
342 /* Should be (640 * 910 + 780 / 2) / 780? */
343 /* swidth */ 768,
344 /* totalwidth */ 910,
345 /* sqwidth */ 780,
346 /* vdelay */ 0x1a,
347 /* sheight */ 480,
Hans de Goede35ccece2013-03-25 14:45:54 -0300348 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300349 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 },{
351 .v4l2_id = V4L2_STD_SECAM,
352 .name = "SECAM",
353 .Fsc = 35468950,
354 .swidth = 924,
355 .sheight = 576,
356 .totalwidth = 1135,
357 .adelay = 0x7f,
358 .bdelay = 0xb0,
359 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
360 .scaledtwidth = 1135,
361 .hdelayx1 = 186,
362 .hactivex1 = 922,
363 .vdelay = 0x20,
364 .vbipack = 255,
365 .sram = 0, /* like PAL, correct? */
Michael H. Schimek67f15702006-01-09 15:25:27 -0200366 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300367 CROPCAP(/* minhdelayx1 */ 68,
368 /* hdelayx1 */ 186,
369 /* swidth */ 924,
370 /* totalwidth */ 1135,
371 /* sqwidth */ 944,
372 /* vdelay */ 0x20,
373 /* sheight */ 576,
Hans de Goede35ccece2013-03-25 14:45:54 -0300374 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300375 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 },{
377 .v4l2_id = V4L2_STD_PAL_Nc,
378 .name = "PAL-Nc",
379 .Fsc = 28636363,
380 .swidth = 640,
381 .sheight = 576,
382 .totalwidth = 910,
383 .adelay = 0x68,
384 .bdelay = 0x5d,
385 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
386 .scaledtwidth = 780,
387 .hdelayx1 = 130,
388 .hactivex1 = 734,
389 .vdelay = 0x1a,
390 .vbipack = 144,
391 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200392 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300393 CROPCAP(/* minhdelayx1 */ 68,
394 /* hdelayx1 */ 130,
395 /* swidth */ (640 * 910 + 780 / 2) / 780,
396 /* totalwidth */ 910,
397 /* sqwidth */ 780,
398 /* vdelay */ 0x1a,
399 /* sheight */ 576,
Hans de Goede35ccece2013-03-25 14:45:54 -0300400 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300401 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 },{
403 .v4l2_id = V4L2_STD_PAL_M,
404 .name = "PAL-M",
405 .Fsc = 28636363,
406 .swidth = 640,
407 .sheight = 480,
408 .totalwidth = 910,
409 .adelay = 0x68,
410 .bdelay = 0x5d,
411 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
412 .scaledtwidth = 780,
413 .hdelayx1 = 135,
414 .hactivex1 = 754,
415 .vdelay = 0x1a,
416 .vbipack = 144,
417 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200418 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300419 CROPCAP(/* minhdelayx1 */ 68,
420 /* hdelayx1 */ 135,
421 /* swidth */ (640 * 910 + 780 / 2) / 780,
422 /* totalwidth */ 910,
423 /* sqwidth */ 780,
424 /* vdelay */ 0x1a,
425 /* sheight */ 480,
Hans de Goede35ccece2013-03-25 14:45:54 -0300426 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300427 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 },{
429 .v4l2_id = V4L2_STD_PAL_N,
430 .name = "PAL-N",
431 .Fsc = 35468950,
432 .swidth = 768,
433 .sheight = 576,
434 .totalwidth = 1135,
435 .adelay = 0x7f,
436 .bdelay = 0x72,
437 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
438 .scaledtwidth = 944,
439 .hdelayx1 = 186,
440 .hactivex1 = 922,
441 .vdelay = 0x20,
442 .vbipack = 144,
443 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300444 .vbistart = { 7, 320 },
445 CROPCAP(/* minhdelayx1 */ 68,
446 /* hdelayx1 */ 186,
447 /* swidth */ (768 * 1135 + 944 / 2) / 944,
448 /* totalwidth */ 1135,
449 /* sqwidth */ 944,
450 /* vdelay */ 0x20,
451 /* sheight */ 576,
Hans de Goede35ccece2013-03-25 14:45:54 -0300452 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300453 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 },{
455 .v4l2_id = V4L2_STD_NTSC_M_JP,
456 .name = "NTSC-JP",
457 .Fsc = 28636363,
458 .swidth = 640,
459 .sheight = 480,
460 .totalwidth = 910,
461 .adelay = 0x68,
462 .bdelay = 0x5d,
463 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
464 .scaledtwidth = 780,
465 .hdelayx1 = 135,
466 .hactivex1 = 754,
467 .vdelay = 0x16,
468 .vbipack = 144,
469 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300470 .vbistart = { 10, 273 },
471 CROPCAP(/* minhdelayx1 */ 68,
472 /* hdelayx1 */ 135,
473 /* swidth */ (640 * 910 + 780 / 2) / 780,
474 /* totalwidth */ 910,
475 /* sqwidth */ 780,
476 /* vdelay */ 0x16,
477 /* sheight */ 480,
Hans de Goede35ccece2013-03-25 14:45:54 -0300478 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300479 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 },{
481 /* that one hopefully works with the strange timing
482 * which video recorders produce when playing a NTSC
483 * tape on a PAL TV ... */
484 .v4l2_id = V4L2_STD_PAL_60,
485 .name = "PAL-60",
486 .Fsc = 35468950,
487 .swidth = 924,
488 .sheight = 480,
489 .totalwidth = 1135,
490 .adelay = 0x7f,
491 .bdelay = 0x72,
492 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
493 .scaledtwidth = 1135,
494 .hdelayx1 = 186,
495 .hactivex1 = 924,
496 .vdelay = 0x1a,
497 .vbipack = 255,
498 .vtotal = 524,
499 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200500 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300501 CROPCAP(/* minhdelayx1 */ 68,
502 /* hdelayx1 */ 186,
503 /* swidth */ 924,
504 /* totalwidth */ 1135,
505 /* sqwidth */ 944,
506 /* vdelay */ 0x1a,
507 /* sheight */ 480,
Hans de Goede35ccece2013-03-25 14:45:54 -0300508 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300509 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511};
512static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
513
514/* ----------------------------------------------------------------------- */
515/* bttv format list
516 packed pixel formats must come first */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300517static const struct bttv_format formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 {
519 .name = "8 bpp, gray",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 .fourcc = V4L2_PIX_FMT_GREY,
521 .btformat = BT848_COLOR_FMT_Y8,
522 .depth = 8,
523 .flags = FORMAT_FLAGS_PACKED,
524 },{
525 .name = "8 bpp, dithered color",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 .fourcc = V4L2_PIX_FMT_HI240,
527 .btformat = BT848_COLOR_FMT_RGB8,
528 .depth = 8,
529 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
530 },{
531 .name = "15 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 .fourcc = V4L2_PIX_FMT_RGB555,
533 .btformat = BT848_COLOR_FMT_RGB15,
534 .depth = 16,
535 .flags = FORMAT_FLAGS_PACKED,
536 },{
537 .name = "15 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 .fourcc = V4L2_PIX_FMT_RGB555X,
539 .btformat = BT848_COLOR_FMT_RGB15,
540 .btswap = 0x03, /* byteswap */
541 .depth = 16,
542 .flags = FORMAT_FLAGS_PACKED,
543 },{
544 .name = "16 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 .fourcc = V4L2_PIX_FMT_RGB565,
546 .btformat = BT848_COLOR_FMT_RGB16,
547 .depth = 16,
548 .flags = FORMAT_FLAGS_PACKED,
549 },{
550 .name = "16 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 .fourcc = V4L2_PIX_FMT_RGB565X,
552 .btformat = BT848_COLOR_FMT_RGB16,
553 .btswap = 0x03, /* byteswap */
554 .depth = 16,
555 .flags = FORMAT_FLAGS_PACKED,
556 },{
557 .name = "24 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 .fourcc = V4L2_PIX_FMT_BGR24,
559 .btformat = BT848_COLOR_FMT_RGB24,
560 .depth = 24,
561 .flags = FORMAT_FLAGS_PACKED,
562 },{
563 .name = "32 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 .fourcc = V4L2_PIX_FMT_BGR32,
565 .btformat = BT848_COLOR_FMT_RGB32,
566 .depth = 32,
567 .flags = FORMAT_FLAGS_PACKED,
568 },{
569 .name = "32 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 .fourcc = V4L2_PIX_FMT_RGB32,
571 .btformat = BT848_COLOR_FMT_RGB32,
572 .btswap = 0x0f, /* byte+word swap */
573 .depth = 32,
574 .flags = FORMAT_FLAGS_PACKED,
575 },{
576 .name = "4:2:2, packed, YUYV",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 .fourcc = V4L2_PIX_FMT_YUYV,
578 .btformat = BT848_COLOR_FMT_YUY2,
579 .depth = 16,
580 .flags = FORMAT_FLAGS_PACKED,
581 },{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 .name = "4:2:2, packed, UYVY",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 .fourcc = V4L2_PIX_FMT_UYVY,
584 .btformat = BT848_COLOR_FMT_YUY2,
585 .btswap = 0x03, /* byteswap */
586 .depth = 16,
587 .flags = FORMAT_FLAGS_PACKED,
588 },{
589 .name = "4:2:2, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 .fourcc = V4L2_PIX_FMT_YUV422P,
591 .btformat = BT848_COLOR_FMT_YCrCb422,
592 .depth = 16,
593 .flags = FORMAT_FLAGS_PLANAR,
594 .hshift = 1,
595 .vshift = 0,
596 },{
597 .name = "4:2:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 .fourcc = V4L2_PIX_FMT_YUV420,
599 .btformat = BT848_COLOR_FMT_YCrCb422,
600 .depth = 12,
601 .flags = FORMAT_FLAGS_PLANAR,
602 .hshift = 1,
603 .vshift = 1,
604 },{
605 .name = "4:2:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 .fourcc = V4L2_PIX_FMT_YVU420,
607 .btformat = BT848_COLOR_FMT_YCrCb422,
608 .depth = 12,
609 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
610 .hshift = 1,
611 .vshift = 1,
612 },{
613 .name = "4:1:1, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 .fourcc = V4L2_PIX_FMT_YUV411P,
615 .btformat = BT848_COLOR_FMT_YCrCb411,
616 .depth = 12,
617 .flags = FORMAT_FLAGS_PLANAR,
618 .hshift = 2,
619 .vshift = 0,
620 },{
621 .name = "4:1:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 .fourcc = V4L2_PIX_FMT_YUV410,
623 .btformat = BT848_COLOR_FMT_YCrCb411,
624 .depth = 9,
625 .flags = FORMAT_FLAGS_PLANAR,
626 .hshift = 2,
627 .vshift = 2,
628 },{
629 .name = "4:1:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 .fourcc = V4L2_PIX_FMT_YVU410,
631 .btformat = BT848_COLOR_FMT_YCrCb411,
632 .depth = 9,
633 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
634 .hshift = 2,
635 .vshift = 2,
636 },{
637 .name = "raw scanlines",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 .fourcc = -1,
639 .btformat = BT848_COLOR_FMT_RAW,
640 .depth = 8,
641 .flags = FORMAT_FLAGS_RAW,
642 }
643};
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300644static const unsigned int FORMATS = ARRAY_SIZE(formats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/* resource management */
648
Michael Schimeke5bd0262007-01-18 16:17:39 -0300649/*
650 RESOURCE_ allocated by freed by
651
652 VIDEO_READ bttv_read 1) bttv_read 2)
653
654 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
655 VIDIOC_QBUF 1) bttv_release
656 VIDIOCMCAPTURE 1)
657
658 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
659 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
660 3) bttv_release
661
662 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
663 VIDIOC_QBUF 1) bttv_release
664 bttv_read, bttv_poll 1) 4)
665
666 1) The resource must be allocated when we enter buffer prepare functions
667 and remain allocated while buffers are in the DMA queue.
668 2) This is a single frame read.
669 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
670 RESOURCE_OVERLAY is allocated.
671 4) This is a continuous read, implies VIDIOC_STREAMON.
672
673 Note this driver permits video input and standard changes regardless if
674 resources are allocated.
675*/
676
677#define VBI_RESOURCES (RESOURCE_VBI)
678#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
679 RESOURCE_VIDEO_STREAM | \
680 RESOURCE_OVERLAY)
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300683int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Michael Schimeke5bd0262007-01-18 16:17:39 -0300685 int xbits; /* mutual exclusive resources */
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (fh->resources & bit)
688 /* have it already allocated */
689 return 1;
690
Michael Schimeke5bd0262007-01-18 16:17:39 -0300691 xbits = bit;
692 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
693 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* is it free? */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300696 if (btv->resources & xbits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /* no, someone else uses it */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300698 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300700
701 if ((bit & VIDEO_RESOURCES)
702 && 0 == (btv->resources & VIDEO_RESOURCES)) {
703 /* Do crop - use current, don't - use default parameters. */
704 __s32 top = btv->crop[!!fh->do_crop].rect.top;
705
706 if (btv->vbi_end > top)
707 goto fail;
708
709 /* We cannot capture the same line as video and VBI data.
710 Claim scan lines crop[].rect.top to bottom. */
711 btv->crop_start = top;
712 } else if (bit & VBI_RESOURCES) {
713 __s32 end = fh->vbi_fmt.end;
714
715 if (end > btv->crop_start)
716 goto fail;
717
718 /* Claim scan lines above fh->vbi_fmt.end. */
719 btv->vbi_end = end;
720 }
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* it's free, grab it */
723 fh->resources |= bit;
724 btv->resources |= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 1;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300726
727 fail:
Michael Schimeke5bd0262007-01-18 16:17:39 -0300728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731static
732int check_btres(struct bttv_fh *fh, int bit)
733{
734 return (fh->resources & bit);
735}
736
737static
738int locked_btres(struct bttv *btv, int bit)
739{
740 return (btv->resources & bit);
741}
742
Michael Schimeke5bd0262007-01-18 16:17:39 -0300743/* Call with btv->lock down. */
744static void
745disclaim_vbi_lines(struct bttv *btv)
746{
747 btv->vbi_end = 0;
748}
749
750/* Call with btv->lock down. */
751static void
752disclaim_video_lines(struct bttv *btv)
753{
754 const struct bttv_tvnorm *tvnorm;
755 u8 crop;
756
757 tvnorm = &bttv_tvnorms[btv->tvnorm];
758 btv->crop_start = tvnorm->cropcap.bounds.top
759 + tvnorm->cropcap.bounds.height;
760
761 /* VBI capturing ends at VDELAY, start of video capturing, no
762 matter how many lines the VBI RISC program expects. When video
763 capturing is off, it shall no longer "preempt" VBI capturing,
764 so we set VDELAY to maximum. */
765 crop = btread(BT848_E_CROP) | 0xc0;
766 btwrite(crop, BT848_E_CROP);
767 btwrite(0xfe, BT848_E_VDELAY_LO);
768 btwrite(crop, BT848_O_CROP);
769 btwrite(0xfe, BT848_O_VDELAY_LO);
770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300773void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if ((fh->resources & bits) != bits) {
Joe Perches8af443e2011-08-21 19:56:48 -0300776 /* trying to free resources not allocated by us ... */
777 pr_err("BUG! (btres)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 fh->resources &= ~bits;
780 btv->resources &= ~bits;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300781
782 bits = btv->resources;
783
784 if (0 == (bits & VIDEO_RESOURCES))
785 disclaim_video_lines(btv);
786
787 if (0 == (bits & VBI_RESOURCES))
788 disclaim_vbi_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791/* ----------------------------------------------------------------------- */
792/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
793
794/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
795 PLL_X = Reference pre-divider (0=1, 1=2)
796 PLL_C = Post divider (0=6, 1=4)
797 PLL_I = Integer input
798 PLL_F = Fractional input
799
800 F_input = 28.636363 MHz:
801 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
802*/
803
804static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
805{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800806 unsigned char fl, fh, fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800808 /* prevent overflows */
809 fin/=4;
810 fout/=4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800812 fout*=12;
813 fi=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800815 fout=(fout%fin)*256;
816 fh=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800818 fout=(fout%fin)*256;
819 fl=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800821 btwrite(fl, BT848_PLL_F_LO);
822 btwrite(fh, BT848_PLL_F_HI);
823 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826static void set_pll(struct bttv *btv)
827{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800828 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800830 if (!btv->pll.pll_crystal)
831 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
Joe Perches8af443e2011-08-21 19:56:48 -0300834 dprintk("%d: PLL: no change required\n", btv->c.nr);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800835 return;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800838 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
839 /* no PLL needed */
840 if (btv->pll.pll_current == 0)
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800841 return;
Joe Perches8af443e2011-08-21 19:56:48 -0300842 if (bttv_verbose)
843 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
844 btv->c.nr, btv->pll.pll_ifreq);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800845 btwrite(0x00,BT848_TGCTRL);
846 btwrite(0x00,BT848_PLL_XCI);
847 btv->pll.pll_current = 0;
848 return;
849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Joe Perches8af443e2011-08-21 19:56:48 -0300851 if (bttv_verbose)
852 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
853 btv->c.nr,
854 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
856
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800857 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* Let other people run while the PLL stabilizes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 msleep(10);
860
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800861 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 btwrite(0,BT848_DSTATUS);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800863 } else {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800864 btwrite(0x08,BT848_TGCTRL);
865 btv->pll.pll_current = btv->pll.pll_ofreq;
Joe Perches8af443e2011-08-21 19:56:48 -0300866 if (bttv_verbose)
867 pr_info("PLL set ok\n");
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800868 return;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800869 }
870 }
871 btv->pll.pll_current = -1;
Joe Perches8af443e2011-08-21 19:56:48 -0300872 if (bttv_verbose)
873 pr_info("Setting PLL failed\n");
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800874 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
877/* used to switch between the bt848's analog/digital video capture modes */
878static void bt848A_set_timing(struct bttv *btv)
879{
880 int i, len;
881 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
882 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
883
Trent Piepho5221e212009-01-28 21:32:59 -0300884 if (btv->input == btv->dig) {
Joe Perches8af443e2011-08-21 19:56:48 -0300885 dprintk("%d: load digital timing table (table_idx=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 btv->c.nr,table_idx);
887
888 /* timing change...reset timing generator address */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800889 btwrite(0x00, BT848_TGCTRL);
890 btwrite(0x02, BT848_TGCTRL);
891 btwrite(0x00, BT848_TGCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 len=SRAM_Table[table_idx][0];
894 for(i = 1; i <= len; i++)
895 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
896 btv->pll.pll_ofreq = 27000000;
897
898 set_pll(btv);
899 btwrite(0x11, BT848_TGCTRL);
900 btwrite(0x41, BT848_DVSIF);
901 } else {
902 btv->pll.pll_ofreq = fsc;
903 set_pll(btv);
904 btwrite(0x0, BT848_DVSIF);
905 }
906}
907
908/* ----------------------------------------------------------------------- */
909
910static void bt848_bright(struct bttv *btv, int bright)
911{
912 int value;
913
Joe Perches8af443e2011-08-21 19:56:48 -0300914 // printk("set bright: %d\n", bright); // DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 btv->bright = bright;
916
917 /* We want -128 to 127 we get 0-65535 */
918 value = (bright >> 8) - 128;
919 btwrite(value & 0xff, BT848_BRIGHT);
920}
921
922static void bt848_hue(struct bttv *btv, int hue)
923{
924 int value;
925
926 btv->hue = hue;
927
928 /* -128 to 127 */
929 value = (hue >> 8) - 128;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800930 btwrite(value & 0xff, BT848_HUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933static void bt848_contrast(struct bttv *btv, int cont)
934{
935 int value,hibit;
936
937 btv->contrast = cont;
938
939 /* 0-511 */
940 value = (cont >> 7);
941 hibit = (value >> 6) & 4;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800942 btwrite(value & 0xff, BT848_CONTRAST_LO);
943 btaor(hibit, ~4, BT848_E_CONTROL);
944 btaor(hibit, ~4, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947static void bt848_sat(struct bttv *btv, int color)
948{
949 int val_u,val_v,hibits;
950
951 btv->saturation = color;
952
953 /* 0-511 for the color */
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700954 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
955 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800956 hibits = (val_u >> 7) & 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 hibits |= (val_v >> 8) & 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800958 btwrite(val_u & 0xff, BT848_SAT_U_LO);
959 btwrite(val_v & 0xff, BT848_SAT_V_LO);
960 btaor(hibits, ~3, BT848_E_CONTROL);
961 btaor(hibits, ~3, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964/* ----------------------------------------------------------------------- */
965
966static int
967video_mux(struct bttv *btv, unsigned int input)
968{
969 int mux,mask2;
970
971 if (input >= bttv_tvcards[btv->c.type].video_inputs)
972 return -EINVAL;
973
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800974 /* needed by RemoteVideo MX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
976 if (mask2)
977 gpio_inout(mask2,mask2);
978
979 if (input == btv->svhs) {
980 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
981 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
982 } else {
983 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
984 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
985 }
Trent Piepho6f987002009-01-28 21:32:59 -0300986 mux = bttv_muxsel(btv, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 btaor(mux<<5, ~(3<<5), BT848_IFORM);
Joe Perches8af443e2011-08-21 19:56:48 -0300988 dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /* card specific hook */
991 if(bttv_tvcards[btv->c.type].muxsel_hook)
992 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
993 return 0;
994}
995
996static char *audio_modes[] = {
997 "audio: tuner", "audio: radio", "audio: extern",
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -0300998 "audio: intern", "audio: mute"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999};
1000
Frank Schaefer5f4567812013-03-21 13:51:17 -03001001static void
1002audio_mux_gpio(struct bttv *btv, int input, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Frank Schaefer28cafc32013-03-21 13:51:13 -03001004 int gpio_val, signal, mute_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1007 bttv_tvcards[btv->c.type].gpiomask);
1008 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1009
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001010 /* automute */
Frank Schaefer28cafc32013-03-21 13:51:13 -03001011 mute_gpio = mute || (btv->opt_automute && (!signal || !btv->users)
Hans Verkuilb8e2a362013-02-10 09:24:14 -03001012 && !btv->has_radio_tuner);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001013
Frank Schaefer28cafc32013-03-21 13:51:13 -03001014 if (mute_gpio)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001015 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1016 else
1017 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001018
Trent Piepho72134a62009-01-28 21:32:59 -03001019 switch (btv->c.type) {
1020 case BTTV_BOARD_VOODOOTV_FM:
1021 case BTTV_BOARD_VOODOOTV_200:
1022 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1023 break;
1024
1025 default:
1026 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1027 }
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (bttv_gpio)
Frank Schaefer28cafc32013-03-21 13:51:13 -03001030 bttv_gpio_tracking(btv, audio_modes[mute_gpio ? 4 : input]);
Frank Schaefer5f4567812013-03-21 13:51:17 -03001031}
1032
1033static int
Frank Schaeferb1a30152013-03-21 13:51:18 -03001034audio_mute(struct bttv *btv, int mute)
Frank Schaefer5f4567812013-03-21 13:51:17 -03001035{
1036 struct v4l2_ctrl *ctrl;
1037
Frank Schaeferb1a30152013-03-21 13:51:18 -03001038 audio_mux_gpio(btv, btv->audio_input, mute);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001039
Hans Verkuil859f0272009-03-28 08:29:00 -03001040 if (btv->sd_msp34xx) {
Hans Verkuil01df5302013-02-06 12:40:28 -03001041 ctrl = v4l2_ctrl_find(btv->sd_msp34xx->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1042 if (ctrl)
Frank Schaefer598728e2013-03-21 13:51:14 -03001043 v4l2_ctrl_s_ctrl(ctrl, mute);
Frank Schaeferb1a30152013-03-21 13:51:18 -03001044 }
1045 if (btv->sd_tvaudio) {
1046 ctrl = v4l2_ctrl_find(btv->sd_tvaudio->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1047 if (ctrl)
1048 v4l2_ctrl_s_ctrl(ctrl, mute);
1049 }
1050 if (btv->sd_tda7432) {
1051 ctrl = v4l2_ctrl_find(btv->sd_tda7432->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1052 if (ctrl)
1053 v4l2_ctrl_s_ctrl(ctrl, mute);
1054 }
1055 return 0;
1056}
1057
1058static int
1059audio_input(struct bttv *btv, int input)
1060{
1061 audio_mux_gpio(btv, input, btv->mute);
1062
1063 if (btv->sd_msp34xx) {
1064 u32 in;
Hans Verkuil01df5302013-02-06 12:40:28 -03001065
Hans Verkuil2474ed42006-03-19 12:35:57 -03001066 /* Note: the inputs tuner/radio/extern/intern are translated
1067 to msp routings. This assumes common behavior for all msp3400
1068 based TV cards. When this assumption fails, then the
1069 specific MSP routing must be added to the card table.
1070 For now this is sufficient. */
1071 switch (input) {
1072 case TVAUDIO_INPUT_RADIO:
Hans de Goede7025e522012-05-21 07:46:22 -03001073 /* Some boards need the msp do to the radio demod */
1074 if (btv->radio_uses_msp_demodulator) {
1075 in = MSP_INPUT_DEFAULT;
1076 break;
1077 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001078 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001079 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001080 break;
1081 case TVAUDIO_INPUT_EXTERN:
Hans Verkuil5325b422009-04-02 11:26:22 -03001082 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001083 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001084 break;
1085 case TVAUDIO_INPUT_INTERN:
1086 /* Yes, this is the same input as for RADIO. I doubt
1087 if this is ever used. The only board with an INTERN
1088 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1089 that was tested. My guess is that the whole INTERN
1090 input does not work. */
Hans Verkuil5325b422009-04-02 11:26:22 -03001091 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001092 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001093 break;
1094 case TVAUDIO_INPUT_TUNER:
1095 default:
Wade Berrier434b2522007-06-25 13:02:16 -03001096 /* This is the only card that uses TUNER2, and afaik,
1097 is the only difference between the VOODOOTV_FM
1098 and VOODOOTV_200 */
1099 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
Hans Verkuil5325b422009-04-02 11:26:22 -03001100 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
Wade Berrier434b2522007-06-25 13:02:16 -03001101 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1102 else
Hans Verkuil5325b422009-04-02 11:26:22 -03001103 in = MSP_INPUT_DEFAULT;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001104 break;
1105 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001106 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1107 in, MSP_OUTPUT_DEFAULT, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001108 }
Hans Verkuil859f0272009-03-28 08:29:00 -03001109 if (btv->sd_tvaudio) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001110 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
Frank Schaeferb1a30152013-03-21 13:51:18 -03001111 input, 0, 0);
Hans Verkuil01df5302013-02-06 12:40:28 -03001112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return 0;
1114}
1115
1116static void
Michael Schimeke5bd0262007-01-18 16:17:39 -03001117bttv_crop_calc_limits(struct bttv_crop *c)
1118{
1119 /* Scale factor min. 1:1, max. 16:1. Min. image size
1120 48 x 32. Scaled width must be a multiple of 4. */
1121
1122 if (1) {
1123 /* For bug compatibility with VIDIOCGCAP and image
1124 size checks in earlier driver versions. */
1125 c->min_scaled_width = 48;
1126 c->min_scaled_height = 32;
1127 } else {
1128 c->min_scaled_width =
Ricardo Ribaldaf90580c2013-11-26 05:31:42 -03001129 (max_t(unsigned int, 48, c->rect.width >> 4) + 3) & ~3;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001130 c->min_scaled_height =
Ricardo Ribaldaf90580c2013-11-26 05:31:42 -03001131 max_t(unsigned int, 32, c->rect.height >> 4);
Michael Schimeke5bd0262007-01-18 16:17:39 -03001132 }
1133
1134 c->max_scaled_width = c->rect.width & ~3;
1135 c->max_scaled_height = c->rect.height;
1136}
1137
1138static void
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001139bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
Michael Schimeke5bd0262007-01-18 16:17:39 -03001140{
1141 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1142 bttv_crop_calc_limits(c);
1143}
1144
1145/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146static int
1147set_tvnorm(struct bttv *btv, unsigned int norm)
1148{
1149 const struct bttv_tvnorm *tvnorm;
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001150 v4l2_std_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001152 BUG_ON(norm >= BTTV_TVNORMS);
1153 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 tvnorm = &bttv_tvnorms[norm];
1156
Mike Isely2de26c02009-09-21 12:42:22 -03001157 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001158 sizeof (tvnorm->cropcap))) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001159 bttv_crop_reset(&btv->crop[0], norm);
1160 btv->crop[1] = btv->crop[0]; /* current = default */
1161
1162 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1163 btv->crop_start = tvnorm->cropcap.bounds.top
1164 + tvnorm->cropcap.bounds.height;
1165 }
1166 }
1167
1168 btv->tvnorm = norm;
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 btwrite(tvnorm->adelay, BT848_ADELAY);
1171 btwrite(tvnorm->bdelay, BT848_BDELAY);
1172 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1173 BT848_IFORM);
1174 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1175 btwrite(1, BT848_VBI_PACK_DEL);
1176 bt848A_set_timing(btv);
1177
1178 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -08001179 case BTTV_BOARD_VOODOOTV_FM:
Wade Berrier434b2522007-06-25 13:02:16 -03001180 case BTTV_BOARD_VOODOOTV_200:
Trent Piepho72134a62009-01-28 21:32:59 -03001181 bttv_tda9880_setnorm(btv, gpio_read());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001184 id = tvnorm->v4l2_id;
Hans Verkuilf41737e2009-04-01 03:52:39 -03001185 bttv_call_all(btv, core, s_std, id);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return 0;
1188}
1189
Michael Schimeke5bd0262007-01-18 16:17:39 -03001190/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191static void
Trent Piepho333408f2007-07-03 15:08:10 -03001192set_input(struct bttv *btv, unsigned int input, unsigned int norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 unsigned long flags;
1195
1196 btv->input = input;
1197 if (irq_iswitch) {
1198 spin_lock_irqsave(&btv->s_lock,flags);
1199 if (btv->curr.frame_irq) {
1200 /* active capture -> delayed input switch */
1201 btv->new_input = input;
1202 } else {
1203 video_mux(btv,input);
1204 }
1205 spin_unlock_irqrestore(&btv->s_lock,flags);
1206 } else {
1207 video_mux(btv,input);
1208 }
Frank Schaefer2166f0a2013-03-21 13:51:16 -03001209 btv->audio_input = (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1210 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN;
1211 audio_input(btv, btv->audio_input);
Trent Piepho333408f2007-07-03 15:08:10 -03001212 set_tvnorm(btv, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215static void init_irqreg(struct bttv *btv)
1216{
1217 /* clear status */
1218 btwrite(0xfffffUL, BT848_INT_STAT);
1219
1220 if (bttv_tvcards[btv->c.type].no_video) {
1221 /* i2c only */
1222 btwrite(BT848_INT_I2CDONE,
1223 BT848_INT_MASK);
1224 } else {
1225 /* full video */
1226 btwrite((btv->triton1) |
1227 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1228 BT848_INT_SCERR |
1229 (fdsr ? BT848_INT_FDSR : 0) |
Jean Delvareeb1b27b2008-08-30 10:18:21 -03001230 BT848_INT_RISCI | BT848_INT_OCERR |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1232 BT848_INT_I2CDONE,
1233 BT848_INT_MASK);
1234 }
1235}
1236
1237static void init_bt848(struct bttv *btv)
1238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (bttv_tvcards[btv->c.type].no_video) {
1240 /* very basic init only */
1241 init_irqreg(btv);
1242 return;
1243 }
1244
1245 btwrite(0x00, BT848_CAP_CTL);
1246 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1247 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1248
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001249 /* set planar and packed mode trigger points and */
1250 /* set rising edge of inverted GPINTR pin as irq trigger */
1251 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1252 BT848_GPIO_DMA_CTL_PLTP1_16|
1253 BT848_GPIO_DMA_CTL_PLTP23_16|
1254 BT848_GPIO_DMA_CTL_GPINTC|
1255 BT848_GPIO_DMA_CTL_GPINTI,
1256 BT848_GPIO_DMA_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001258 btwrite(0x20, BT848_E_VSCALE_HI);
1259 btwrite(0x20, BT848_O_VSCALE_HI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Hans Verkuil01df5302013-02-06 12:40:28 -03001261 v4l2_ctrl_handler_setup(&btv->ctrl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001263 /* interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 init_irqreg(btv);
1265}
1266
1267static void bttv_reinit_bt848(struct bttv *btv)
1268{
1269 unsigned long flags;
1270
1271 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03001272 pr_info("%d: reset, reinitialize\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 spin_lock_irqsave(&btv->s_lock,flags);
1274 btv->errors=0;
1275 bttv_set_dma(btv,0);
1276 spin_unlock_irqrestore(&btv->s_lock,flags);
1277
1278 init_bt848(btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001279 btv->pll.pll_current = -1;
Trent Piepho333408f2007-07-03 15:08:10 -03001280 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Hans Verkuil01df5302013-02-06 12:40:28 -03001283static int bttv_s_ctrl(struct v4l2_ctrl *c)
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001284{
Hans Verkuil01df5302013-02-06 12:40:28 -03001285 struct bttv *btv = container_of(c->handler, struct bttv, ctrl_handler);
1286 int val;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001287
1288 switch (c->id) {
1289 case V4L2_CID_BRIGHTNESS:
Hans Verkuil01df5302013-02-06 12:40:28 -03001290 bt848_bright(btv, c->val);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001291 break;
1292 case V4L2_CID_HUE:
Hans Verkuil01df5302013-02-06 12:40:28 -03001293 bt848_hue(btv, c->val);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001294 break;
1295 case V4L2_CID_CONTRAST:
Hans Verkuil01df5302013-02-06 12:40:28 -03001296 bt848_contrast(btv, c->val);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001297 break;
1298 case V4L2_CID_SATURATION:
Hans Verkuil01df5302013-02-06 12:40:28 -03001299 bt848_sat(btv, c->val);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001300 break;
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -03001301 case V4L2_CID_COLOR_KILLER:
Hans Verkuil01df5302013-02-06 12:40:28 -03001302 if (c->val) {
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -03001303 btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1304 btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1305 } else {
1306 btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1307 btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1308 }
1309 break;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001310 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil01df5302013-02-06 12:40:28 -03001311 audio_mute(btv, c->val);
Frank Schaefer598728e2013-03-21 13:51:14 -03001312 btv->mute = c->val;
Hans Verkuil01df5302013-02-06 12:40:28 -03001313 break;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001314 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuil01df5302013-02-06 12:40:28 -03001315 btv->volume_gpio(btv, c->val);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001316 break;
1317
Hans Verkuil01df5302013-02-06 12:40:28 -03001318 case V4L2_CID_CHROMA_AGC:
1319 val = c->val ? BT848_SCLOOP_CAGC : 0;
1320 btwrite(val, BT848_E_SCLOOP);
1321 btwrite(val, BT848_O_SCLOOP);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001322 break;
1323 case V4L2_CID_PRIVATE_COMBFILTER:
Hans Verkuil01df5302013-02-06 12:40:28 -03001324 btv->opt_combfilter = c->val;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001325 break;
1326 case V4L2_CID_PRIVATE_LUMAFILTER:
Hans Verkuil01df5302013-02-06 12:40:28 -03001327 if (c->val) {
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001328 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1329 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1330 } else {
1331 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1332 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1333 }
1334 break;
1335 case V4L2_CID_PRIVATE_AUTOMUTE:
Hans Verkuil01df5302013-02-06 12:40:28 -03001336 btv->opt_automute = c->val;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001337 break;
1338 case V4L2_CID_PRIVATE_AGC_CRUSH:
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001339 btwrite(BT848_ADC_RESERVED |
Hans Verkuil01df5302013-02-06 12:40:28 -03001340 (c->val ? BT848_ADC_CRUSH : 0),
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001341 BT848_ADC);
1342 break;
1343 case V4L2_CID_PRIVATE_VCR_HACK:
Hans Verkuil01df5302013-02-06 12:40:28 -03001344 btv->opt_vcr_hack = c->val;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001345 break;
1346 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
Hans Verkuil01df5302013-02-06 12:40:28 -03001347 btwrite(c->val, BT848_WC_UP);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001348 break;
1349 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
Hans Verkuil01df5302013-02-06 12:40:28 -03001350 btwrite(c->val, BT848_WC_DOWN);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001351 break;
1352 case V4L2_CID_PRIVATE_UV_RATIO:
Hans Verkuil01df5302013-02-06 12:40:28 -03001353 btv->opt_uv_ratio = c->val;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001354 bt848_sat(btv, btv->saturation);
1355 break;
1356 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
Hans Verkuil01df5302013-02-06 12:40:28 -03001357 btaor((c->val << 7), ~BT848_OFORM_RANGE, BT848_OFORM);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001358 break;
1359 case V4L2_CID_PRIVATE_CORING:
Hans Verkuil01df5302013-02-06 12:40:28 -03001360 btaor((c->val << 5), ~BT848_OFORM_CORE32, BT848_OFORM);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001361 break;
1362 default:
1363 return -EINVAL;
1364 }
1365 return 0;
1366}
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368/* ----------------------------------------------------------------------- */
1369
Hans Verkuil01df5302013-02-06 12:40:28 -03001370static const struct v4l2_ctrl_ops bttv_ctrl_ops = {
1371 .s_ctrl = bttv_s_ctrl,
1372};
1373
1374static struct v4l2_ctrl_config bttv_ctrl_combfilter = {
1375 .ops = &bttv_ctrl_ops,
1376 .id = V4L2_CID_PRIVATE_COMBFILTER,
1377 .name = "Comb Filter",
1378 .type = V4L2_CTRL_TYPE_BOOLEAN,
1379 .min = 0,
1380 .max = 1,
1381 .step = 1,
1382 .def = 1,
1383};
1384
1385static struct v4l2_ctrl_config bttv_ctrl_automute = {
1386 .ops = &bttv_ctrl_ops,
1387 .id = V4L2_CID_PRIVATE_AUTOMUTE,
1388 .name = "Auto Mute",
1389 .type = V4L2_CTRL_TYPE_BOOLEAN,
1390 .min = 0,
1391 .max = 1,
1392 .step = 1,
1393 .def = 1,
1394};
1395
1396static struct v4l2_ctrl_config bttv_ctrl_lumafilter = {
1397 .ops = &bttv_ctrl_ops,
1398 .id = V4L2_CID_PRIVATE_LUMAFILTER,
1399 .name = "Luma Decimation Filter",
1400 .type = V4L2_CTRL_TYPE_BOOLEAN,
1401 .min = 0,
1402 .max = 1,
1403 .step = 1,
1404 .def = 1,
1405};
1406
1407static struct v4l2_ctrl_config bttv_ctrl_agc_crush = {
1408 .ops = &bttv_ctrl_ops,
1409 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
1410 .name = "AGC Crush",
1411 .type = V4L2_CTRL_TYPE_BOOLEAN,
1412 .min = 0,
1413 .max = 1,
1414 .step = 1,
1415 .def = 1,
1416};
1417
1418static struct v4l2_ctrl_config bttv_ctrl_vcr_hack = {
1419 .ops = &bttv_ctrl_ops,
1420 .id = V4L2_CID_PRIVATE_VCR_HACK,
1421 .name = "VCR Hack",
1422 .type = V4L2_CTRL_TYPE_BOOLEAN,
1423 .min = 0,
1424 .max = 1,
1425 .step = 1,
1426 .def = 1,
1427};
1428
1429static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower = {
1430 .ops = &bttv_ctrl_ops,
1431 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
1432 .name = "Whitecrush Lower",
1433 .type = V4L2_CTRL_TYPE_INTEGER,
1434 .min = 0,
1435 .max = 255,
1436 .step = 1,
1437 .def = 0x7f,
1438};
1439
1440static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper = {
1441 .ops = &bttv_ctrl_ops,
1442 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
1443 .name = "Whitecrush Upper",
1444 .type = V4L2_CTRL_TYPE_INTEGER,
1445 .min = 0,
1446 .max = 255,
1447 .step = 1,
1448 .def = 0xcf,
1449};
1450
1451static struct v4l2_ctrl_config bttv_ctrl_uv_ratio = {
1452 .ops = &bttv_ctrl_ops,
1453 .id = V4L2_CID_PRIVATE_UV_RATIO,
1454 .name = "UV Ratio",
1455 .type = V4L2_CTRL_TYPE_INTEGER,
1456 .min = 0,
1457 .max = 100,
1458 .step = 1,
1459 .def = 50,
1460};
1461
1462static struct v4l2_ctrl_config bttv_ctrl_full_luma = {
1463 .ops = &bttv_ctrl_ops,
1464 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
1465 .name = "Full Luma Range",
1466 .type = V4L2_CTRL_TYPE_BOOLEAN,
1467 .min = 0,
1468 .max = 1,
1469 .step = 1,
1470};
1471
1472static struct v4l2_ctrl_config bttv_ctrl_coring = {
1473 .ops = &bttv_ctrl_ops,
1474 .id = V4L2_CID_PRIVATE_CORING,
1475 .name = "Coring",
1476 .type = V4L2_CTRL_TYPE_INTEGER,
1477 .min = 0,
1478 .max = 3,
1479 .step = 1,
1480};
1481
1482
1483/* ----------------------------------------------------------------------- */
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485void bttv_gpio_tracking(struct bttv *btv, char *comment)
1486{
1487 unsigned int outbits, data;
1488 outbits = btread(BT848_GPIO_OUT_EN);
1489 data = btread(BT848_GPIO_DATA);
Joe Perches8af443e2011-08-21 19:56:48 -03001490 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1491 btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
1494static void bttv_field_count(struct bttv *btv)
1495{
1496 int need_count = 0;
1497
1498 if (btv->users)
1499 need_count++;
1500
1501 if (need_count) {
1502 /* start field counter */
1503 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1504 } else {
1505 /* stop field counter */
1506 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1507 btv->field_count = 0;
1508 }
1509}
1510
1511static const struct bttv_format*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512format_by_fourcc(int fourcc)
1513{
1514 unsigned int i;
1515
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001516 for (i = 0; i < FORMATS; i++) {
1517 if (-1 == formats[i].fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 continue;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001519 if (formats[i].fourcc == fourcc)
1520 return formats+i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522 return NULL;
1523}
1524
1525/* ----------------------------------------------------------------------- */
1526/* misc helpers */
1527
1528static int
1529bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1530 struct bttv_buffer *new)
1531{
1532 struct bttv_buffer *old;
1533 unsigned long flags;
1534 int retval = 0;
1535
Joe Perches8af443e2011-08-21 19:56:48 -03001536 dprintk("switch_overlay: enter [new=%p]\n", new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (new)
Brandon Philips0fc06862007-11-06 20:02:36 -03001538 new->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 spin_lock_irqsave(&btv->s_lock,flags);
1540 old = btv->screen;
1541 btv->screen = new;
1542 btv->loop_irq |= 1;
1543 bttv_set_dma(btv, 0x03);
1544 spin_unlock_irqrestore(&btv->s_lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (NULL != old) {
Joe Perches8af443e2011-08-21 19:56:48 -03001546 dprintk("switch_overlay: old=%p state is %d\n",
1547 old, old->vb.state);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001548 bttv_dma_free(&fh->cap,btv, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 kfree(old);
1550 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03001551 if (NULL == new)
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03001552 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 dprintk("switch_overlay: done\n");
1554 return retval;
1555}
1556
1557/* ----------------------------------------------------------------------- */
1558/* video4linux (1) interface */
1559
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001560static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1561 struct bttv_buffer *buf,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001562 const struct bttv_format *fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 unsigned int width, unsigned int height,
1564 enum v4l2_field field)
1565{
Michael Schimeke5bd0262007-01-18 16:17:39 -03001566 struct bttv_fh *fh = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 int redo_dma_risc = 0;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001568 struct bttv_crop c;
1569 int norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 int rc;
1571
1572 /* check settings */
1573 if (NULL == fmt)
1574 return -EINVAL;
1575 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1576 width = RAW_BPL;
1577 height = RAW_LINES*2;
1578 if (width*height > buf->vb.bsize)
1579 return -EINVAL;
1580 buf->vb.size = buf->vb.bsize;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001581
1582 /* Make sure tvnorm and vbi_end remain consistent
1583 until we're done. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03001584
1585 norm = btv->tvnorm;
1586
1587 /* In this mode capturing always starts at defrect.top
1588 (default VDELAY), ignoring cropping parameters. */
1589 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001591 }
1592
Michael Schimeke5bd0262007-01-18 16:17:39 -03001593 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1594 } else {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001595 norm = btv->tvnorm;
1596 c = btv->crop[!!fh->do_crop];
1597
Michael Schimeke5bd0262007-01-18 16:17:39 -03001598 if (width < c.min_scaled_width ||
1599 width > c.max_scaled_width ||
1600 height < c.min_scaled_height)
1601 return -EINVAL;
1602
1603 switch (field) {
1604 case V4L2_FIELD_TOP:
1605 case V4L2_FIELD_BOTTOM:
1606 case V4L2_FIELD_ALTERNATE:
1607 /* btv->crop counts frame lines. Max. scale
1608 factor is 16:1 for frames, 8:1 for fields. */
1609 if (height * 2 > c.max_scaled_height)
1610 return -EINVAL;
1611 break;
1612
1613 default:
1614 if (height > c.max_scaled_height)
1615 return -EINVAL;
1616 break;
1617 }
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 buf->vb.size = (width * height * fmt->depth) >> 3;
1620 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1621 return -EINVAL;
1622 }
1623
1624 /* alloc + fill struct bttv_buffer (if changed) */
1625 if (buf->vb.width != width || buf->vb.height != height ||
1626 buf->vb.field != field ||
Michael Schimeke5bd0262007-01-18 16:17:39 -03001627 buf->tvnorm != norm || buf->fmt != fmt ||
1628 buf->crop.top != c.rect.top ||
1629 buf->crop.left != c.rect.left ||
1630 buf->crop.width != c.rect.width ||
1631 buf->crop.height != c.rect.height) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 buf->vb.width = width;
1633 buf->vb.height = height;
1634 buf->vb.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001635 buf->tvnorm = norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 buf->fmt = fmt;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001637 buf->crop = c.rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 redo_dma_risc = 1;
1639 }
1640
1641 /* alloc risc memory */
Brandon Philips0fc06862007-11-06 20:02:36 -03001642 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 redo_dma_risc = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001644 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 goto fail;
1646 }
1647
1648 if (redo_dma_risc)
1649 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1650 goto fail;
1651
Brandon Philips0fc06862007-11-06 20:02:36 -03001652 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return 0;
1654
1655 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001656 bttv_dma_free(q,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return rc;
1658}
1659
1660static int
1661buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1662{
1663 struct bttv_fh *fh = q->priv_data;
1664
1665 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1666 if (0 == *count)
1667 *count = gbuffers;
Andreas Bombedab7e312010-03-21 16:02:45 -03001668 if (*size * *count > gbuffers * gbufsize)
1669 *count = (gbuffers * gbufsize) / *size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return 0;
1671}
1672
1673static int
1674buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1675 enum v4l2_field field)
1676{
1677 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1678 struct bttv_fh *fh = q->priv_data;
1679
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001680 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 fh->width, fh->height, field);
1682}
1683
1684static void
1685buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1686{
1687 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1688 struct bttv_fh *fh = q->priv_data;
1689 struct bttv *btv = fh->btv;
1690
Brandon Philips0fc06862007-11-06 20:02:36 -03001691 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 list_add_tail(&buf->vb.queue,&btv->capture);
1693 if (!btv->curr.frame_irq) {
1694 btv->loop_irq |= 1;
1695 bttv_set_dma(btv, 0x03);
1696 }
1697}
1698
1699static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1700{
1701 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1702 struct bttv_fh *fh = q->priv_data;
1703
Michael Schimekfeaba7a2007-01-26 08:30:05 -03001704 bttv_dma_free(q,fh->btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
1707static struct videobuf_queue_ops bttv_video_qops = {
1708 .buf_setup = buffer_setup,
1709 .buf_prepare = buffer_prepare,
1710 .buf_queue = buffer_queue,
1711 .buf_release = buffer_release,
1712};
1713
Hans Verkuilb8e2a362013-02-10 09:24:14 -03001714static void radio_enable(struct bttv *btv)
1715{
1716 /* Switch to the radio tuner */
1717 if (!btv->has_radio_tuner) {
1718 btv->has_radio_tuner = 1;
1719 bttv_call_all(btv, tuner, s_radio);
Frank Schaefer2166f0a2013-03-21 13:51:16 -03001720 btv->audio_input = TVAUDIO_INPUT_RADIO;
1721 audio_input(btv, btv->audio_input);
Hans Verkuilb8e2a362013-02-10 09:24:14 -03001722 }
1723}
1724
Hans Verkuil314527a2013-03-15 06:10:40 -03001725static int bttv_s_std(struct file *file, void *priv, v4l2_std_id id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001727 struct bttv_fh *fh = priv;
1728 struct bttv *btv = fh->btv;
1729 unsigned int i;
Hans Verkuil8c14cc12013-02-06 12:42:40 -03001730 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001732 for (i = 0; i < BTTV_TVNORMS; i++)
Hans Verkuil314527a2013-03-15 06:10:40 -03001733 if (id & bttv_tvnorms[i].v4l2_id)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001734 break;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001735 if (i == BTTV_TVNORMS) {
1736 err = -EINVAL;
1737 goto err;
1738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Hans Verkuil314527a2013-03-15 06:10:40 -03001740 btv->std = id;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001741 set_tvnorm(btv, i);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001742
1743err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001745 return err;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001746}
1747
Hans Verkuil6795cc52013-02-06 12:43:07 -03001748static int bttv_g_std(struct file *file, void *priv, v4l2_std_id *id)
1749{
1750 struct bttv_fh *fh = priv;
1751 struct bttv *btv = fh->btv;
1752
1753 *id = btv->std;
1754 return 0;
1755}
1756
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001757static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001758{
1759 struct bttv_fh *fh = f;
1760 struct bttv *btv = fh->btv;
1761
1762 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
Hans Verkuil0bde6c32013-05-29 10:19:02 -03001763 *id &= V4L2_STD_625_50;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001764 else
Hans Verkuil0bde6c32013-05-29 10:19:02 -03001765 *id &= V4L2_STD_525_60;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001766 return 0;
1767}
1768
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001769static int bttv_enum_input(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001770 struct v4l2_input *i)
1771{
1772 struct bttv_fh *fh = priv;
1773 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001774 int rc = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001775
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001776 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) {
1777 rc = -EINVAL;
1778 goto err;
1779 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001780
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001781 i->type = V4L2_INPUT_TYPE_CAMERA;
Hans Verkuilf74f89cb2013-01-31 08:45:13 -03001782 i->audioset = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001783
Trent Piephoabb03622009-01-28 21:32:59 -03001784 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001785 sprintf(i->name, "Television");
1786 i->type = V4L2_INPUT_TYPE_TUNER;
1787 i->tuner = 0;
1788 } else if (i->index == btv->svhs) {
1789 sprintf(i->name, "S-Video");
1790 } else {
1791 sprintf(i->name, "Composite%d", i->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001794 if (i->index == btv->input) {
1795 __u32 dstatus = btread(BT848_DSTATUS);
1796 if (0 == (dstatus & BT848_DSTATUS_PRES))
1797 i->status |= V4L2_IN_ST_NO_SIGNAL;
1798 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1799 i->status |= V4L2_IN_ST_NO_H_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001802 i->std = BTTV_NORMS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001804err:
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001805
1806 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001807}
Nickolay V. Shmyrev4b9b9362006-08-25 16:53:04 -03001808
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001809static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001810{
1811 struct bttv_fh *fh = priv;
1812 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001814 *i = btv->input;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001815
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001816 return 0;
1817}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001819static int bttv_s_input(struct file *file, void *priv, unsigned int i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001820{
1821 struct bttv_fh *fh = priv;
1822 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001823
Hans Verkuilf74f89cb2013-01-31 08:45:13 -03001824 if (i >= bttv_tvcards[btv->c.type].video_inputs)
1825 return -EINVAL;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001826
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001827 set_input(btv, i, btv->tvnorm);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001828 return 0;
1829}
1830
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001831static int bttv_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001832 const struct v4l2_tuner *t)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001833{
1834 struct bttv_fh *fh = priv;
1835 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001836
Hans Verkuild9b67072013-02-06 11:43:07 -03001837 if (t->index)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001838 return -EINVAL;
1839
Hans Verkuil859f0272009-03-28 08:29:00 -03001840 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001841
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001842 if (btv->audio_mode_gpio) {
1843 struct v4l2_tuner copy = *t;
1844
1845 btv->audio_mode_gpio(btv, &copy, 1);
1846 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001847 return 0;
1848}
1849
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001850static int bttv_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001851 struct v4l2_frequency *f)
1852{
1853 struct bttv_fh *fh = priv;
1854 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001855
Hans Verkuild9b67072013-02-06 11:43:07 -03001856 if (f->tuner)
1857 return -EINVAL;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001858
Hans Verkuilb8e2a362013-02-10 09:24:14 -03001859 if (f->type == V4L2_TUNER_RADIO)
1860 radio_enable(btv);
Hans Verkuil76ea9922013-02-06 11:49:14 -03001861 f->frequency = f->type == V4L2_TUNER_RADIO ?
1862 btv->radio_freq : btv->tv_freq;
1863
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001864 return 0;
1865}
1866
Hans Verkuilb530a442013-03-19 04:09:26 -03001867static void bttv_set_frequency(struct bttv *btv, const struct v4l2_frequency *f)
Hans Verkuil76ea9922013-02-06 11:49:14 -03001868{
Hans Verkuilb530a442013-03-19 04:09:26 -03001869 struct v4l2_frequency new_freq = *f;
1870
Hans Verkuil76ea9922013-02-06 11:49:14 -03001871 bttv_call_all(btv, tuner, s_frequency, f);
1872 /* s_frequency may clamp the frequency, so get the actual
1873 frequency before assigning radio/tv_freq. */
Hans Verkuilb530a442013-03-19 04:09:26 -03001874 bttv_call_all(btv, tuner, g_frequency, &new_freq);
1875 if (new_freq.type == V4L2_TUNER_RADIO) {
Hans Verkuilb8e2a362013-02-10 09:24:14 -03001876 radio_enable(btv);
Hans Verkuilb530a442013-03-19 04:09:26 -03001877 btv->radio_freq = new_freq.frequency;
Hans Verkuil76ea9922013-02-06 11:49:14 -03001878 if (btv->has_matchbox)
1879 tea5757_set_freq(btv, btv->radio_freq);
1880 } else {
Hans Verkuilb530a442013-03-19 04:09:26 -03001881 btv->tv_freq = new_freq.frequency;
Hans Verkuil76ea9922013-02-06 11:49:14 -03001882 }
1883}
1884
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001885static int bttv_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001886 const struct v4l2_frequency *f)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001887{
1888 struct bttv_fh *fh = priv;
1889 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001890
Hans Verkuild9b67072013-02-06 11:43:07 -03001891 if (f->tuner)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001892 return -EINVAL;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001893
Hans Verkuil76ea9922013-02-06 11:49:14 -03001894 bttv_set_frequency(btv, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001895 return 0;
1896}
1897
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001898static int bttv_log_status(struct file *file, void *f)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001899{
Hans Verkuil01df5302013-02-06 12:40:28 -03001900 struct video_device *vdev = video_devdata(file);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001901 struct bttv_fh *fh = f;
1902 struct bttv *btv = fh->btv;
1903
Hans Verkuil01df5302013-02-06 12:40:28 -03001904 v4l2_ctrl_handler_log_status(vdev->ctrl_handler, btv->c.v4l2_dev.name);
Hans Verkuil859f0272009-03-28 08:29:00 -03001905 bttv_call_all(btv, core, log_status);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001906 return 0;
1907}
1908
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001909#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001910static int bttv_g_register(struct file *file, void *f,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001911 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001912{
1913 struct bttv_fh *fh = f;
1914 struct bttv *btv = fh->btv;
1915
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001916 /* bt848 has a 12-bit register space */
1917 reg->reg &= 0xfff;
1918 reg->val = btread(reg->reg);
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001919 reg->size = 1;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001920
1921 return 0;
1922}
1923
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001924static int bttv_s_register(struct file *file, void *f,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001925 const struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001926{
1927 struct bttv_fh *fh = f;
1928 struct bttv *btv = fh->btv;
1929
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001930 /* bt848 has a 12-bit register space */
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001931 btwrite(reg->val, reg->reg & 0xfff);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001932
1933 return 0;
1934}
1935#endif
1936
Michael Schimeke5bd0262007-01-18 16:17:39 -03001937/* Given cropping boundaries b and the scaled width and height of a
1938 single field or frame, which must not exceed hardware limits, this
1939 function adjusts the cropping parameters c. */
1940static void
1941bttv_crop_adjust (struct bttv_crop * c,
1942 const struct v4l2_rect * b,
1943 __s32 width,
1944 __s32 height,
1945 enum v4l2_field field)
1946{
1947 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
1948 __s32 max_left;
1949 __s32 max_top;
1950
1951 if (width < c->min_scaled_width) {
1952 /* Max. hor. scale factor 16:1. */
1953 c->rect.width = width * 16;
1954 } else if (width > c->max_scaled_width) {
1955 /* Min. hor. scale factor 1:1. */
1956 c->rect.width = width;
1957
1958 max_left = b->left + b->width - width;
1959 max_left = min(max_left, (__s32) MAX_HDELAY);
1960 if (c->rect.left > max_left)
1961 c->rect.left = max_left;
1962 }
1963
1964 if (height < c->min_scaled_height) {
1965 /* Max. vert. scale factor 16:1, single fields 8:1. */
1966 c->rect.height = height * 16;
1967 } else if (frame_height > c->max_scaled_height) {
1968 /* Min. vert. scale factor 1:1.
1969 Top and height count field lines times two. */
1970 c->rect.height = (frame_height + 1) & ~1;
1971
1972 max_top = b->top + b->height - c->rect.height;
1973 if (c->rect.top > max_top)
1974 c->rect.top = max_top;
1975 }
1976
1977 bttv_crop_calc_limits(c);
1978}
1979
1980/* Returns an error if scaling to a frame or single field with the given
1981 width and height is not possible with the current cropping parameters
1982 and width aligned according to width_mask. If adjust_size is TRUE the
1983 function may adjust the width and/or height instead, rounding width
1984 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
1985 also adjust the current cropping parameters to get closer to the
1986 desired image size. */
1987static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03001988limit_scaled_size_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03001989 __s32 * width,
1990 __s32 * height,
1991 enum v4l2_field field,
1992 unsigned int width_mask,
1993 unsigned int width_bias,
1994 int adjust_size,
1995 int adjust_crop)
1996{
1997 struct bttv *btv = fh->btv;
1998 const struct v4l2_rect *b;
1999 struct bttv_crop *c;
2000 __s32 min_width;
2001 __s32 min_height;
2002 __s32 max_width;
2003 __s32 max_height;
2004 int rc;
2005
2006 BUG_ON((int) width_mask >= 0 ||
2007 width_bias >= (unsigned int) -width_mask);
2008
2009 /* Make sure tvnorm, vbi_end and the current cropping parameters
2010 remain consistent until we're done. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03002011
2012 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2013
2014 /* Do crop - use current, don't - use default parameters. */
2015 c = &btv->crop[!!fh->do_crop];
2016
2017 if (fh->do_crop
2018 && adjust_size
2019 && adjust_crop
2020 && !locked_btres(btv, VIDEO_RESOURCES)) {
2021 min_width = 48;
2022 min_height = 32;
2023
2024 /* We cannot scale up. When the scaled image is larger
2025 than crop.rect we adjust the crop.rect as required
2026 by the V4L2 spec, hence cropcap.bounds are our limit. */
Ricardo Ribaldaf90580c2013-11-26 05:31:42 -03002027 max_width = min_t(unsigned int, b->width, MAX_HACTIVE);
Michael Schimeke5bd0262007-01-18 16:17:39 -03002028 max_height = b->height;
2029
2030 /* We cannot capture the same line as video and VBI data.
2031 Note btv->vbi_end is really a minimum, see
2032 bttv_vbi_try_fmt(). */
2033 if (btv->vbi_end > b->top) {
2034 max_height -= btv->vbi_end - b->top;
2035 rc = -EBUSY;
2036 if (min_height > max_height)
2037 goto fail;
2038 }
2039 } else {
2040 rc = -EBUSY;
2041 if (btv->vbi_end > c->rect.top)
2042 goto fail;
2043
2044 min_width = c->min_scaled_width;
2045 min_height = c->min_scaled_height;
2046 max_width = c->max_scaled_width;
2047 max_height = c->max_scaled_height;
2048
2049 adjust_crop = 0;
2050 }
2051
2052 min_width = (min_width - width_mask - 1) & width_mask;
2053 max_width = max_width & width_mask;
2054
2055 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2056 min_height = min_height;
2057 /* Min. scale factor is 1:1. */
2058 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2059
2060 if (adjust_size) {
2061 *width = clamp(*width, min_width, max_width);
2062 *height = clamp(*height, min_height, max_height);
2063
2064 /* Round after clamping to avoid overflow. */
2065 *width = (*width + width_bias) & width_mask;
2066
2067 if (adjust_crop) {
2068 bttv_crop_adjust(c, b, *width, *height, field);
2069
2070 if (btv->vbi_end > c->rect.top) {
2071 /* Move the crop window out of the way. */
2072 c->rect.top = btv->vbi_end;
2073 }
2074 }
2075 } else {
2076 rc = -EINVAL;
2077 if (*width < min_width ||
2078 *height < min_height ||
2079 *width > max_width ||
2080 *height > max_height ||
2081 0 != (*width & ~width_mask))
2082 goto fail;
2083 }
2084
2085 rc = 0; /* success */
2086
2087 fail:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002088
2089 return rc;
2090}
2091
2092/* Returns an error if the given overlay window dimensions are not
2093 possible with the current cropping parameters. If adjust_size is
2094 TRUE the function may adjust the window width and/or height
2095 instead, however it always rounds the horizontal position and
2096 width as btcx_align() does. If adjust_crop is TRUE the function
2097 may also adjust the current cropping parameters to get closer
2098 to the desired window size. */
2099static int
Hans Verkuilc13eb702013-02-07 07:56:11 -03002100verify_window_lock(struct bttv_fh *fh, struct v4l2_window *win,
2101 int adjust_size, int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
2103 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002104 unsigned int width_mask;
2105 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Hans Verkuilc13eb702013-02-07 07:56:11 -03002107 if (win->w.width < 48)
2108 win->w.width = 48;
2109 if (win->w.height < 32)
2110 win->w.height = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (win->clipcount > 2048)
Hans Verkuilc13eb702013-02-07 07:56:11 -03002112 win->clipcount = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Hans Verkuilc13eb702013-02-07 07:56:11 -03002114 win->chromakey = 0;
2115 win->global_alpha = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 field = win->field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Hans Verkuilc13eb702013-02-07 07:56:11 -03002118 switch (field) {
2119 case V4L2_FIELD_TOP:
2120 case V4L2_FIELD_BOTTOM:
2121 case V4L2_FIELD_INTERLACED:
2122 break;
2123 default:
2124 field = V4L2_FIELD_ANY;
2125 break;
2126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (V4L2_FIELD_ANY == field) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002128 __s32 height2;
2129
2130 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2131 field = (win->w.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 ? V4L2_FIELD_INTERLACED
2133 : V4L2_FIELD_TOP;
2134 }
Hans Verkuilc13eb702013-02-07 07:56:11 -03002135 win->field = field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Michael Schimeke5bd0262007-01-18 16:17:39 -03002137 if (NULL == fh->ovfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return -EINVAL;
Hans Verkuilc13eb702013-02-07 07:56:11 -03002139 /* 4-byte alignment. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03002140 width_mask = ~0;
2141 switch (fh->ovfmt->depth) {
2142 case 8:
2143 case 24:
2144 width_mask = ~3;
2145 break;
2146 case 16:
2147 width_mask = ~1;
2148 break;
2149 case 32:
2150 break;
2151 default:
2152 BUG();
2153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Michael Schimeke5bd0262007-01-18 16:17:39 -03002155 win->w.width -= win->w.left & ~width_mask;
2156 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2157
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002158 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002159 field, width_mask,
2160 /* width_bias: round down */ 0,
2161 adjust_size, adjust_crop);
2162 if (0 != rc)
2163 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return 0;
2165}
2166
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002167static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 struct v4l2_window *win, int fixup)
2169{
2170 struct v4l2_clip *clips = NULL;
2171 int n,size,retval = 0;
2172
2173 if (NULL == fh->ovfmt)
2174 return -EINVAL;
2175 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2176 return -EINVAL;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002177 retval = verify_window_lock(fh, win,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002178 /* adjust_size */ fixup,
2179 /* adjust_crop */ fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (0 != retval)
2181 return retval;
2182
2183 /* copy clips -- luckily v4l1 + v4l2 are binary
2184 compatible here ...*/
2185 n = win->clipcount;
2186 size = sizeof(*clips)*(n+4);
2187 clips = kmalloc(size,GFP_KERNEL);
2188 if (NULL == clips)
2189 return -ENOMEM;
2190 if (n > 0) {
2191 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2192 kfree(clips);
2193 return -EFAULT;
2194 }
2195 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 /* clip against screen */
2198 if (NULL != btv->fbuf.base)
2199 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2200 &win->w, clips, n);
2201 btcx_sort_clips(clips,n);
2202
2203 /* 4-byte alignments */
2204 switch (fh->ovfmt->depth) {
2205 case 8:
2206 case 24:
2207 btcx_align(&win->w, clips, n, 3);
2208 break;
2209 case 16:
2210 btcx_align(&win->w, clips, n, 1);
2211 break;
2212 case 32:
2213 /* no alignment fixups needed */
2214 break;
2215 default:
2216 BUG();
2217 }
2218
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002219 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 fh->ov.clips = clips;
2221 fh->ov.nclips = n;
2222
2223 fh->ov.w = win->w;
2224 fh->ov.field = win->field;
2225 fh->ov.setup_ok = 1;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 btv->init.ov.w.width = win->w.width;
2228 btv->init.ov.w.height = win->w.height;
2229 btv->init.ov.field = win->field;
2230
2231 /* update overlay if needed */
2232 retval = 0;
2233 if (check_btres(fh, RESOURCE_OVERLAY)) {
2234 struct bttv_buffer *new;
2235
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002236 new = videobuf_sg_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03002237 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2239 retval = bttv_switch_overlay(btv,fh,new);
2240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return retval;
2242}
2243
2244/* ----------------------------------------------------------------------- */
2245
2246static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2247{
2248 struct videobuf_queue* q = NULL;
2249
2250 switch (fh->type) {
2251 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2252 q = &fh->cap;
2253 break;
2254 case V4L2_BUF_TYPE_VBI_CAPTURE:
2255 q = &fh->vbi;
2256 break;
2257 default:
2258 BUG();
2259 }
2260 return q;
2261}
2262
2263static int bttv_resource(struct bttv_fh *fh)
2264{
2265 int res = 0;
2266
2267 switch (fh->type) {
2268 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002269 res = RESOURCE_VIDEO_STREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 break;
2271 case V4L2_BUF_TYPE_VBI_CAPTURE:
2272 res = RESOURCE_VBI;
2273 break;
2274 default:
2275 BUG();
2276 }
2277 return res;
2278}
2279
2280static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2281{
2282 struct videobuf_queue *q = bttv_queue(fh);
2283 int res = bttv_resource(fh);
2284
2285 if (check_btres(fh,res))
2286 return -EBUSY;
2287 if (videobuf_queue_is_busy(q))
2288 return -EBUSY;
2289 fh->type = type;
2290 return 0;
2291}
2292
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002293static void
2294pix_format_set_size (struct v4l2_pix_format * f,
2295 const struct bttv_format * fmt,
2296 unsigned int width,
2297 unsigned int height)
2298{
2299 f->width = width;
2300 f->height = height;
2301
2302 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2303 f->bytesperline = width; /* Y plane */
2304 f->sizeimage = (width * height * fmt->depth) >> 3;
2305 } else {
2306 f->bytesperline = (width * fmt->depth) >> 3;
2307 f->sizeimage = height * f->bytesperline;
2308 }
2309}
2310
Hans Verkuil78b526a2008-05-28 12:16:41 -03002311static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002312 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002314 struct bttv_fh *fh = priv;
2315
2316 pix_format_set_size(&f->fmt.pix, fh->fmt,
2317 fh->width, fh->height);
2318 f->fmt.pix.field = fh->cap.field;
2319 f->fmt.pix.pixelformat = fh->fmt->fourcc;
Hans Verkuilf5864892013-02-06 11:58:59 -03002320 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002321
2322 return 0;
2323}
2324
Hans Verkuil78b526a2008-05-28 12:16:41 -03002325static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002326 struct v4l2_format *f)
2327{
2328 struct bttv_fh *fh = priv;
2329
2330 f->fmt.win.w = fh->ov.w;
2331 f->fmt.win.field = fh->ov.field;
2332
2333 return 0;
2334}
2335
Hans Verkuil78b526a2008-05-28 12:16:41 -03002336static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002337 struct v4l2_format *f)
2338{
2339 const struct bttv_format *fmt;
2340 struct bttv_fh *fh = priv;
2341 struct bttv *btv = fh->btv;
2342 enum v4l2_field field;
2343 __s32 width, height;
Hans Verkuilee70e3d2013-02-06 12:03:30 -03002344 __s32 height2;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002345 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002346
2347 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2348 if (NULL == fmt)
2349 return -EINVAL;
2350
2351 field = f->fmt.pix.field;
2352
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002353 switch (field) {
2354 case V4L2_FIELD_TOP:
2355 case V4L2_FIELD_BOTTOM:
2356 case V4L2_FIELD_ALTERNATE:
2357 case V4L2_FIELD_INTERLACED:
2358 break;
Hans Verkuilee70e3d2013-02-06 12:03:30 -03002359 case V4L2_FIELD_SEQ_BT:
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002360 case V4L2_FIELD_SEQ_TB:
Hans Verkuilee70e3d2013-02-06 12:03:30 -03002361 if (!(fmt->flags & FORMAT_FLAGS_PLANAR)) {
2362 field = V4L2_FIELD_SEQ_TB;
2363 break;
2364 }
2365 /* fall through */
2366 default: /* FIELD_ANY case */
2367 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2368 field = (f->fmt.pix.height > height2)
2369 ? V4L2_FIELD_INTERLACED
2370 : V4L2_FIELD_BOTTOM;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002371 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002373
2374 width = f->fmt.pix.width;
2375 height = f->fmt.pix.height;
2376
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002377 rc = limit_scaled_size_lock(fh, &width, &height, field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002378 /* width_mask: 4 pixels */ ~3,
2379 /* width_bias: nearest */ 2,
2380 /* adjust_size */ 1,
2381 /* adjust_crop */ 0);
2382 if (0 != rc)
2383 return rc;
2384
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002385 /* update data for the application */
2386 f->fmt.pix.field = field;
2387 pix_format_set_size(&f->fmt.pix, fmt, width, height);
Hans Verkuilf5864892013-02-06 11:58:59 -03002388 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002389
2390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
Hans Verkuil78b526a2008-05-28 12:16:41 -03002393static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002394 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002396 struct bttv_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Hans Verkuilc13eb702013-02-07 07:56:11 -03002398 verify_window_lock(fh, &f->fmt.win,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002399 /* adjust_size */ 1,
2400 /* adjust_crop */ 0);
Hans Verkuilc13eb702013-02-07 07:56:11 -03002401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
2403
Hans Verkuil78b526a2008-05-28 12:16:41 -03002404static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002405 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
2407 int retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002408 const struct bttv_format *fmt;
2409 struct bttv_fh *fh = priv;
2410 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002411 __s32 width, height;
2412 enum v4l2_field field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002414 retval = bttv_switch_type(fh, f->type);
2415 if (0 != retval)
2416 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Hans Verkuil78b526a2008-05-28 12:16:41 -03002418 retval = bttv_try_fmt_vid_cap(file, priv, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002419 if (0 != retval)
2420 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002422 width = f->fmt.pix.width;
2423 height = f->fmt.pix.height;
2424 field = f->fmt.pix.field;
2425
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002426 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002427 /* width_mask: 4 pixels */ ~3,
2428 /* width_bias: nearest */ 2,
2429 /* adjust_size */ 1,
2430 /* adjust_crop */ 1);
2431 if (0 != retval)
2432 return retval;
2433
2434 f->fmt.pix.field = field;
2435
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002436 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002438 /* update our state informations */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002439 fh->fmt = fmt;
2440 fh->cap.field = f->fmt.pix.field;
2441 fh->cap.last = V4L2_FIELD_NONE;
2442 fh->width = f->fmt.pix.width;
2443 fh->height = f->fmt.pix.height;
2444 btv->init.fmt = fmt;
2445 btv->init.width = f->fmt.pix.width;
2446 btv->init.height = f->fmt.pix.height;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002447
2448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
Hans Verkuil78b526a2008-05-28 12:16:41 -03002451static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002452 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002454 struct bttv_fh *fh = priv;
2455 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002457 if (no_overlay > 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03002458 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002459 return -EINVAL;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002460 }
Michael Krufky5e453dc2006-01-09 15:32:31 -02002461
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002462 return setup_window_lock(fh, btv, &f->fmt.win, 1);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002463}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002465static int bttv_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002466 struct v4l2_capability *cap)
2467{
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002468 struct video_device *vdev = video_devdata(file);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002469 struct bttv_fh *fh = priv;
2470 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002472 if (0 == v4l2)
2473 return -EINVAL;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002474
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002475 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2476 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2477 snprintf(cap->bus_info, sizeof(cap->bus_info),
2478 "PCI:%s", pci_name(btv->c.pci));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002479 cap->capabilities =
2480 V4L2_CAP_VIDEO_CAPTURE |
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002481 V4L2_CAP_READWRITE |
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002482 V4L2_CAP_STREAMING |
2483 V4L2_CAP_DEVICE_CAPS;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002484 if (no_overlay <= 0)
2485 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002486 if (btv->vbi_dev)
2487 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
2488 if (btv->radio_dev)
2489 cap->capabilities |= V4L2_CAP_RADIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002491 /*
2492 * No need to lock here: those vars are initialized during board
2493 * probe and remains untouched during the rest of the driver lifecycle
2494 */
2495 if (btv->has_saa6588)
2496 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
Trent Piephoabb03622009-01-28 21:32:59 -03002497 if (btv->tuner_type != TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002498 cap->capabilities |= V4L2_CAP_TUNER;
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002499 if (vdev->vfl_type == VFL_TYPE_GRABBER)
2500 cap->device_caps = cap->capabilities &
2501 (V4L2_CAP_VIDEO_CAPTURE |
2502 V4L2_CAP_READWRITE |
2503 V4L2_CAP_STREAMING |
2504 V4L2_CAP_VIDEO_OVERLAY |
2505 V4L2_CAP_TUNER);
2506 else if (vdev->vfl_type == VFL_TYPE_VBI)
2507 cap->device_caps = cap->capabilities &
2508 (V4L2_CAP_VBI_CAPTURE |
2509 V4L2_CAP_READWRITE |
2510 V4L2_CAP_STREAMING |
2511 V4L2_CAP_TUNER);
2512 else {
2513 cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
2514 if (btv->has_saa6588)
2515 cap->device_caps |= V4L2_CAP_READWRITE |
2516 V4L2_CAP_RDS_CAPTURE;
2517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002521static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2522{
2523 int index = -1, i;
2524
2525 for (i = 0; i < FORMATS; i++) {
2526 if (formats[i].fourcc != -1)
2527 index++;
2528 if ((unsigned int)index == f->index)
2529 break;
2530 }
2531 if (FORMATS == i)
2532 return -EINVAL;
2533
2534 f->pixelformat = formats[i].fourcc;
2535 strlcpy(f->description, formats[i].name, sizeof(f->description));
2536
2537 return i;
2538}
2539
Hans Verkuil78b526a2008-05-28 12:16:41 -03002540static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002541 struct v4l2_fmtdesc *f)
2542{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002543 int rc = bttv_enum_fmt_cap_ovr(f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002544
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002545 if (rc < 0)
2546 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002547
2548 return 0;
2549}
2550
Hans Verkuil78b526a2008-05-28 12:16:41 -03002551static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002552 struct v4l2_fmtdesc *f)
2553{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002554 int rc;
2555
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002556 if (no_overlay > 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03002557 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002558 return -EINVAL;
2559 }
2560
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002561 rc = bttv_enum_fmt_cap_ovr(f);
2562
2563 if (rc < 0)
2564 return rc;
2565
2566 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002567 return -EINVAL;
2568
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002569 return 0;
2570}
2571
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002572static int bttv_g_fbuf(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002573 struct v4l2_framebuffer *fb)
2574{
2575 struct bttv_fh *fh = f;
2576 struct bttv *btv = fh->btv;
2577
2578 *fb = btv->fbuf;
2579 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
Hans Verkuila12fd702013-02-06 12:00:03 -03002580 fb->flags = V4L2_FBUF_FLAG_PRIMARY;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002581 if (fh->ovfmt)
2582 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2583 return 0;
2584}
2585
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002586static int bttv_overlay(struct file *file, void *f, unsigned int on)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002587{
2588 struct bttv_fh *fh = f;
2589 struct bttv *btv = fh->btv;
2590 struct bttv_buffer *new;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002591 int retval = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002592
2593 if (on) {
2594 /* verify args */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002595 if (unlikely(!btv->fbuf.base)) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002596 return -EINVAL;
2597 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002598 if (unlikely(!fh->ov.setup_ok)) {
Joe Perches8af443e2011-08-21 19:56:48 -03002599 dprintk("%d: overlay: !setup_ok\n", btv->c.nr);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002600 retval = -EINVAL;
2601 }
2602 if (retval)
2603 return retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002604 }
2605
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002606 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002607 return -EBUSY;
2608
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002609 if (on) {
2610 fh->ov.tvnorm = btv->tvnorm;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002611 new = videobuf_sg_alloc(sizeof(*new));
Robert Fitzsimons7c018802008-02-13 16:38:11 -03002612 new->crop = btv->crop[!!fh->do_crop].rect;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002613 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2614 } else {
2615 new = NULL;
2616 }
2617
2618 /* switch over */
2619 retval = bttv_switch_overlay(btv, fh, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 return retval;
2621}
2622
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002623static int bttv_s_fbuf(struct file *file, void *f,
Hans Verkuile6eb28c2012-09-04 10:26:45 -03002624 const struct v4l2_framebuffer *fb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002626 struct bttv_fh *fh = f;
2627 struct bttv *btv = fh->btv;
2628 const struct bttv_format *fmt;
2629 int retval;
2630
2631 if (!capable(CAP_SYS_ADMIN) &&
2632 !capable(CAP_SYS_RAWIO))
2633 return -EPERM;
2634
2635 /* check args */
2636 fmt = format_by_fourcc(fb->fmt.pixelformat);
2637 if (NULL == fmt)
2638 return -EINVAL;
2639 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2640 return -EINVAL;
2641
2642 retval = -EINVAL;
2643 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2644 __s32 width = fb->fmt.width;
2645 __s32 height = fb->fmt.height;
2646
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002647 retval = limit_scaled_size_lock(fh, &width, &height,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002648 V4L2_FIELD_INTERLACED,
2649 /* width_mask */ ~3,
2650 /* width_bias */ 2,
2651 /* adjust_size */ 0,
2652 /* adjust_crop */ 0);
2653 if (0 != retval)
2654 return retval;
2655 }
2656
2657 /* ok, accept it */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002658 btv->fbuf.base = fb->base;
2659 btv->fbuf.fmt.width = fb->fmt.width;
2660 btv->fbuf.fmt.height = fb->fmt.height;
2661 if (0 != fb->fmt.bytesperline)
2662 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2663 else
2664 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2665
2666 retval = 0;
2667 fh->ovfmt = fmt;
2668 btv->init.ovfmt = fmt;
2669 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2670 fh->ov.w.left = 0;
2671 fh->ov.w.top = 0;
2672 fh->ov.w.width = fb->fmt.width;
2673 fh->ov.w.height = fb->fmt.height;
2674 btv->init.ov.w.width = fb->fmt.width;
2675 btv->init.ov.w.height = fb->fmt.height;
2676 kfree(fh->ov.clips);
2677 fh->ov.clips = NULL;
2678 fh->ov.nclips = 0;
2679
2680 if (check_btres(fh, RESOURCE_OVERLAY)) {
2681 struct bttv_buffer *new;
2682
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002683 new = videobuf_sg_alloc(sizeof(*new));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002684 new->crop = btv->crop[!!fh->do_crop].rect;
2685 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2686 retval = bttv_switch_overlay(btv, fh, new);
2687 }
2688 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002689 return retval;
2690}
2691
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002692static int bttv_reqbufs(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002693 struct v4l2_requestbuffers *p)
2694{
2695 struct bttv_fh *fh = priv;
2696 return videobuf_reqbufs(bttv_queue(fh), p);
2697}
2698
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002699static int bttv_querybuf(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002700 struct v4l2_buffer *b)
2701{
2702 struct bttv_fh *fh = priv;
2703 return videobuf_querybuf(bttv_queue(fh), b);
2704}
2705
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002706static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002707{
2708 struct bttv_fh *fh = priv;
2709 struct bttv *btv = fh->btv;
2710 int res = bttv_resource(fh);
2711
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002712 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002713 return -EBUSY;
2714
2715 return videobuf_qbuf(bttv_queue(fh), b);
2716}
2717
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002718static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002719{
2720 struct bttv_fh *fh = priv;
2721 return videobuf_dqbuf(bttv_queue(fh), b,
2722 file->f_flags & O_NONBLOCK);
2723}
2724
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002725static int bttv_streamon(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002726 enum v4l2_buf_type type)
2727{
2728 struct bttv_fh *fh = priv;
2729 struct bttv *btv = fh->btv;
2730 int res = bttv_resource(fh);
2731
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002732 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002733 return -EBUSY;
2734 return videobuf_streamon(bttv_queue(fh));
2735}
2736
2737
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002738static int bttv_streamoff(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002739 enum v4l2_buf_type type)
2740{
2741 struct bttv_fh *fh = priv;
2742 struct bttv *btv = fh->btv;
2743 int retval;
2744 int res = bttv_resource(fh);
2745
2746
2747 retval = videobuf_streamoff(bttv_queue(fh));
2748 if (retval < 0)
2749 return retval;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002750 free_btres_lock(btv, fh, res);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002751 return 0;
2752}
2753
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002754static int bttv_g_parm(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002755 struct v4l2_streamparm *parm)
2756{
2757 struct bttv_fh *fh = f;
2758 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002759
Hans Verkuila652ef62012-09-09 10:27:57 -03002760 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2761 return -EINVAL;
2762 parm->parm.capture.readbuffers = gbuffers;
Trent Piepho51f0b8d52009-03-04 01:21:02 -03002763 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2764 &parm->parm.capture.timeperframe);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002765
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002766 return 0;
2767}
2768
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002769static int bttv_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002770 struct v4l2_tuner *t)
2771{
2772 struct bttv_fh *fh = priv;
2773 struct bttv *btv = fh->btv;
2774
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002775 if (0 != t->index)
2776 return -EINVAL;
2777
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002778 t->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil3d4b8032013-02-06 12:44:07 -03002779 t->capability = V4L2_TUNER_CAP_NORM;
Hans Verkuil859f0272009-03-28 08:29:00 -03002780 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002781 strcpy(t->name, "Television");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002782 t->type = V4L2_TUNER_ANALOG_TV;
2783 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2784 t->signal = 0xffff;
2785
2786 if (btv->audio_mode_gpio)
2787 btv->audio_mode_gpio(btv, t, 0);
2788
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002789 return 0;
2790}
2791
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002792static int bttv_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002793 struct v4l2_cropcap *cap)
2794{
2795 struct bttv_fh *fh = priv;
2796 struct bttv *btv = fh->btv;
2797
2798 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2799 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2800 return -EINVAL;
2801
2802 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
2803
2804 return 0;
2805}
2806
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002807static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002808{
2809 struct bttv_fh *fh = f;
2810 struct bttv *btv = fh->btv;
2811
2812 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2813 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2814 return -EINVAL;
2815
2816 /* No fh->do_crop = 1; because btv->crop[1] may be
2817 inconsistent with fh->width or fh->height and apps
2818 do not expect a change here. */
2819
2820 crop->c = btv->crop[!!fh->do_crop].rect;
2821
2822 return 0;
2823}
2824
Hans Verkuil4f996592012-09-05 05:10:48 -03002825static int bttv_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002826{
2827 struct bttv_fh *fh = f;
2828 struct bttv *btv = fh->btv;
2829 const struct v4l2_rect *b;
2830 int retval;
2831 struct bttv_crop c;
2832 __s32 b_left;
2833 __s32 b_top;
2834 __s32 b_right;
2835 __s32 b_bottom;
2836
2837 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2838 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2839 return -EINVAL;
2840
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002841 /* Make sure tvnorm, vbi_end and the current cropping
2842 parameters remain consistent until we're done. Note
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002843 read() may change vbi_end in check_alloc_btres_lock(). */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002844 retval = -EBUSY;
2845
2846 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002847 return retval;
2848 }
2849
2850 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2851
2852 b_left = b->left;
2853 b_right = b_left + b->width;
2854 b_bottom = b->top + b->height;
2855
2856 b_top = max(b->top, btv->vbi_end);
2857 if (b_top + 32 >= b_bottom) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002858 return retval;
2859 }
2860
2861 /* Min. scaled size 48 x 32. */
Hans Verkuil4f996592012-09-05 05:10:48 -03002862 c.rect.left = clamp_t(s32, crop->c.left, b_left, b_right - 48);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002863 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
2864
Hans Verkuil4f996592012-09-05 05:10:48 -03002865 c.rect.width = clamp_t(s32, crop->c.width,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002866 48, b_right - c.rect.left);
2867
Hans Verkuil4f996592012-09-05 05:10:48 -03002868 c.rect.top = clamp_t(s32, crop->c.top, b_top, b_bottom - 32);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002869 /* Top and height must be a multiple of two. */
2870 c.rect.top = (c.rect.top + 1) & ~1;
2871
Hans Verkuil4f996592012-09-05 05:10:48 -03002872 c.rect.height = clamp_t(s32, crop->c.height,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002873 32, b_bottom - c.rect.top);
2874 c.rect.height = (c.rect.height + 1) & ~1;
2875
2876 bttv_crop_calc_limits(&c);
2877
2878 btv->crop[1] = c;
2879
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002880 fh->do_crop = 1;
2881
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002882 if (fh->width < c.min_scaled_width) {
2883 fh->width = c.min_scaled_width;
2884 btv->init.width = c.min_scaled_width;
2885 } else if (fh->width > c.max_scaled_width) {
2886 fh->width = c.max_scaled_width;
2887 btv->init.width = c.max_scaled_width;
2888 }
2889
2890 if (fh->height < c.min_scaled_height) {
2891 fh->height = c.min_scaled_height;
2892 btv->init.height = c.min_scaled_height;
2893 } else if (fh->height > c.max_scaled_height) {
2894 fh->height = c.max_scaled_height;
2895 btv->init.height = c.max_scaled_height;
2896 }
2897
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002898 return 0;
2899}
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901static ssize_t bttv_read(struct file *file, char __user *data,
2902 size_t count, loff_t *ppos)
2903{
2904 struct bttv_fh *fh = file->private_data;
2905 int retval = 0;
2906
2907 if (fh->btv->errors)
2908 bttv_reinit_bt848(fh->btv);
Joe Perches8af443e2011-08-21 19:56:48 -03002909 dprintk("%d: read count=%d type=%s\n",
2910 fh->btv->c.nr, (int)count, v4l2_type_names[fh->type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912 switch (fh->type) {
2913 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002914 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002915 /* VIDEO_READ in use by another fh,
2916 or VIDEO_STREAM by any fh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 return -EBUSY;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2920 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002921 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 break;
2923 case V4L2_BUF_TYPE_VBI_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002924 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return -EBUSY;
2926 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2927 file->f_flags & O_NONBLOCK);
2928 break;
2929 default:
2930 BUG();
2931 }
2932 return retval;
2933}
2934
2935static unsigned int bttv_poll(struct file *file, poll_table *wait)
2936{
2937 struct bttv_fh *fh = file->private_data;
2938 struct bttv_buffer *buf;
2939 enum v4l2_field field;
Hans Verkuilae50f0f2013-02-06 12:42:13 -03002940 unsigned int rc = 0;
2941 unsigned long req_events = poll_requested_events(wait);
2942
2943 if (v4l2_event_pending(&fh->fh))
2944 rc = POLLPRI;
2945 else if (req_events & POLLPRI)
2946 poll_wait(file, &fh->fh.wait, wait);
2947
2948 if (!(req_events & (POLLIN | POLLRDNORM)))
2949 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002952 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Hans Verkuilae50f0f2013-02-06 12:42:13 -03002953 return rc | POLLERR;
2954 return rc | videobuf_poll_stream(file, &fh->vbi, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956
Michael Schimeke5bd0262007-01-18 16:17:39 -03002957 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 /* streaming capture */
2959 if (list_empty(&fh->cap.stream))
Hans Verkuilae50f0f2013-02-06 12:42:13 -03002960 return rc | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2962 } else {
2963 /* read() capture */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (NULL == fh->cap.read_buf) {
2965 /* need to capture a new frame */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002966 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
Hans Verkuilae50f0f2013-02-06 12:42:13 -03002967 return rc | POLLERR;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002968 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002969 if (NULL == fh->cap.read_buf)
Hans Verkuilae50f0f2013-02-06 12:42:13 -03002970 return rc | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2972 field = videobuf_next_field(&fh->cap);
2973 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -08002974 kfree (fh->cap.read_buf);
2975 fh->cap.read_buf = NULL;
Hans Verkuilae50f0f2013-02-06 12:42:13 -03002976 return rc | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
2978 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
2979 fh->cap.read_off = 0;
2980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 buf = (struct bttv_buffer*)fh->cap.read_buf;
2982 }
2983
2984 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03002985 if (buf->vb.state == VIDEOBUF_DONE ||
2986 buf->vb.state == VIDEOBUF_ERROR)
Hans Verkuilae50f0f2013-02-06 12:42:13 -03002987 rc = rc | POLLIN|POLLRDNORM;
Figo.zhang9fd64182009-06-16 13:31:29 -03002988 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
Hans Verkuilbec43662008-12-30 06:58:20 -03002991static int bttv_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002993 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03002994 struct bttv *btv = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 struct bttv_fh *fh;
2996 enum v4l2_buf_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Joe Perches8af443e2011-08-21 19:56:48 -03002998 dprintk("open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Laurent Pinchart327ae592009-11-27 13:57:55 -03003000 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003001 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Laurent Pinchart327ae592009-11-27 13:57:55 -03003002 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003003 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3004 } else {
3005 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -03003007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Joe Perches8af443e2011-08-21 19:56:48 -03003009 dprintk("%d: open called (type=%s)\n",
3010 btv->c.nr, v4l2_type_names[type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
3012 /* allocate per filehandle data */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003013 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3014 if (unlikely(!fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return -ENOMEM;
Hans Verkuil01df5302013-02-06 12:40:28 -03003016 btv->users++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 file->private_data = fh;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 *fh = btv->init;
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003020 v4l2_fh_init(&fh->fh, vdev);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 fh->type = type;
3023 fh->ov.setup_ok = 0;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003024
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003025 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3026 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3028 V4L2_FIELD_INTERLACED,
3029 sizeof(struct bttv_buffer),
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003030 fh, &btv->lock);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003031 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3032 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 V4L2_BUF_TYPE_VBI_CAPTURE,
3034 V4L2_FIELD_SEQ_TB,
3035 sizeof(struct bttv_buffer),
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003036 fh, &btv->lock);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03003037 set_tvnorm(btv,btv->tvnorm);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003038 set_input(btv, btv->input, btv->tvnorm);
Frank Schaefer0757f5c2013-03-21 13:51:20 -03003039 audio_mute(btv, btv->mute);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003040
3041 /* The V4L2 spec requires one global set of cropping parameters
3042 which only change on request. These are stored in btv->crop[1].
3043 However for compatibility with V4L apps and cropping unaware
3044 V4L2 apps we now reset the cropping parameters as seen through
3045 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3046 will use btv->crop[0], the default cropping parameters for the
3047 current video standard, and VIDIOC_S_FMT will not implicitely
3048 change the cropping parameters until VIDIOC_S_CROP has been
3049 called. */
3050 fh->do_crop = !reset_crop; /* module parameter */
3051
3052 /* Likewise there should be one global set of VBI capture
3053 parameters, but for compatibility with V4L apps and earlier
3054 driver versions each fh has its own parameters. */
3055 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3056
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 bttv_field_count(btv);
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003058 v4l2_fh_add(&fh->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return 0;
3060}
3061
Hans Verkuilbec43662008-12-30 06:58:20 -03003062static int bttv_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
3064 struct bttv_fh *fh = file->private_data;
3065 struct bttv *btv = fh->btv;
3066
3067 /* turn off overlay */
3068 if (check_btres(fh, RESOURCE_OVERLAY))
3069 bttv_switch_overlay(btv,fh,NULL);
3070
3071 /* stop video capture */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003072 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 videobuf_streamoff(&fh->cap);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003074 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 }
3076 if (fh->cap.read_buf) {
3077 buffer_release(&fh->cap,fh->cap.read_buf);
3078 kfree(fh->cap.read_buf);
3079 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03003080 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003081 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
3084 /* stop vbi capture */
3085 if (check_btres(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03003086 videobuf_stop(&fh->vbi);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003087 free_btres_lock(btv,fh,RESOURCE_VBI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
3089
3090 /* free stuff */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 videobuf_mmap_free(&fh->cap);
3093 videobuf_mmap_free(&fh->vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
3096 btv->users--;
3097 bttv_field_count(btv);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003098
3099 if (!btv->users)
Hans Verkuil01df5302013-02-06 12:40:28 -03003100 audio_mute(btv, btv->mute);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003101
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003102 v4l2_fh_del(&fh->fh);
3103 v4l2_fh_exit(&fh->fh);
3104 kfree(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 return 0;
3106}
3107
3108static int
3109bttv_mmap(struct file *file, struct vm_area_struct *vma)
3110{
3111 struct bttv_fh *fh = file->private_data;
3112
Joe Perches8af443e2011-08-21 19:56:48 -03003113 dprintk("%d: mmap type=%s 0x%lx+%ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 fh->btv->c.nr, v4l2_type_names[fh->type],
3115 vma->vm_start, vma->vm_end - vma->vm_start);
3116 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3117}
3118
Hans Verkuilbec43662008-12-30 06:58:20 -03003119static const struct v4l2_file_operations bttv_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
Mauro Carvalho Chehab8979e9d2010-09-15 08:22:09 -03003121 .owner = THIS_MODULE,
3122 .open = bttv_open,
3123 .release = bttv_release,
3124 .unlocked_ioctl = video_ioctl2,
3125 .read = bttv_read,
3126 .mmap = bttv_mmap,
3127 .poll = bttv_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128};
3129
Hans Verkuila3998102008-07-21 02:57:38 -03003130static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003131 .vidioc_querycap = bttv_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003132 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3133 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3134 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3135 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3136 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3137 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3138 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3139 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003140 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3141 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3142 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003143 .vidioc_cropcap = bttv_cropcap,
3144 .vidioc_reqbufs = bttv_reqbufs,
3145 .vidioc_querybuf = bttv_querybuf,
3146 .vidioc_qbuf = bttv_qbuf,
3147 .vidioc_dqbuf = bttv_dqbuf,
3148 .vidioc_s_std = bttv_s_std,
Hans Verkuil6795cc52013-02-06 12:43:07 -03003149 .vidioc_g_std = bttv_g_std,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003150 .vidioc_enum_input = bttv_enum_input,
3151 .vidioc_g_input = bttv_g_input,
3152 .vidioc_s_input = bttv_s_input,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003153 .vidioc_streamon = bttv_streamon,
3154 .vidioc_streamoff = bttv_streamoff,
3155 .vidioc_g_tuner = bttv_g_tuner,
3156 .vidioc_s_tuner = bttv_s_tuner,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003157 .vidioc_g_crop = bttv_g_crop,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003158 .vidioc_s_crop = bttv_s_crop,
3159 .vidioc_g_fbuf = bttv_g_fbuf,
3160 .vidioc_s_fbuf = bttv_s_fbuf,
3161 .vidioc_overlay = bttv_overlay,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003162 .vidioc_g_parm = bttv_g_parm,
3163 .vidioc_g_frequency = bttv_g_frequency,
3164 .vidioc_s_frequency = bttv_s_frequency,
3165 .vidioc_log_status = bttv_log_status,
3166 .vidioc_querystd = bttv_querystd,
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003167 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
3168 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Zoltan Devai43846832008-01-14 13:24:38 -03003169#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003170 .vidioc_g_register = bttv_g_register,
3171 .vidioc_s_register = bttv_s_register,
Zoltan Devai43846832008-01-14 13:24:38 -03003172#endif
Hans Verkuila3998102008-07-21 02:57:38 -03003173};
3174
3175static struct video_device bttv_video_template = {
3176 .fops = &bttv_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003177 .ioctl_ops = &bttv_ioctl_ops,
3178 .tvnorms = BTTV_NORMS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179};
3180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181/* ----------------------------------------------------------------------- */
3182/* radio interface */
3183
Hans Verkuilbec43662008-12-30 06:58:20 -03003184static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003186 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003187 struct bttv *btv = video_drvdata(file);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003188 struct bttv_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Joe Perches8af443e2011-08-21 19:56:48 -03003190 dprintk("open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Joe Perches8af443e2011-08-21 19:56:48 -03003192 dprintk("%d: open called (radio)\n", btv->c.nr);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003193
3194 /* allocate per filehandle data */
3195 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003196 if (unlikely(!fh))
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003197 return -ENOMEM;
3198 file->private_data = fh;
3199 *fh = btv->init;
Hans Verkuil8c14cc12013-02-06 12:42:40 -03003200 v4l2_fh_init(&fh->fh, vdev);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 btv->radio_user++;
Frank Schaefer0757f5c2013-03-21 13:51:20 -03003203 audio_mute(btv, btv->mute);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003204
Hans Verkuil8c14cc12013-02-06 12:42:40 -03003205 v4l2_fh_add(&fh->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208}
3209
Hans Verkuilbec43662008-12-30 06:58:20 -03003210static int radio_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003212 struct bttv_fh *fh = file->private_data;
3213 struct bttv *btv = fh->btv;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003214 struct saa6588_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Robert Fitzsimonsb9bc07a2008-04-10 09:40:31 -03003216 file->private_data = NULL;
Hans Verkuil8c14cc12013-02-06 12:42:40 -03003217 v4l2_fh_del(&fh->fh);
3218 v4l2_fh_exit(&fh->fh);
Robert Fitzsimonsb9bc07a2008-04-10 09:40:31 -03003219 kfree(fh);
3220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 btv->radio_user--;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003222
Hans Verkuilb9218f22010-12-27 12:22:46 -03003223 bttv_call_all(btv, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003224
Hans Verkuilb8e2a362013-02-10 09:24:14 -03003225 if (btv->radio_user == 0)
3226 btv->has_radio_tuner = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return 0;
3228}
3229
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003230static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003232 struct bttv_fh *fh = priv;
3233 struct bttv *btv = fh->btv;
3234
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003235 if (0 != t->index)
3236 return -EINVAL;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003237 strcpy(t->name, "Radio");
3238 t->type = V4L2_TUNER_RADIO;
Hans Verkuilb8e2a362013-02-10 09:24:14 -03003239 radio_enable(btv);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003240
Hans Verkuil859f0272009-03-28 08:29:00 -03003241 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003242
3243 if (btv->audio_mode_gpio)
3244 btv->audio_mode_gpio(btv, t, 0);
3245
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003246 return 0;
3247}
3248
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003249static int radio_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03003250 const struct v4l2_tuner *t)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003251{
3252 struct bttv_fh *fh = priv;
3253 struct bttv *btv = fh->btv;
3254
3255 if (0 != t->index)
3256 return -EINVAL;
3257
Hans Verkuilb8e2a362013-02-10 09:24:14 -03003258 radio_enable(btv);
Hans Verkuila024c1a2011-06-12 07:02:43 -03003259 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003260 return 0;
3261}
3262
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003263static ssize_t radio_read(struct file *file, char __user *data,
3264 size_t count, loff_t *ppos)
3265{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003266 struct bttv_fh *fh = file->private_data;
3267 struct bttv *btv = fh->btv;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003268 struct saa6588_command cmd;
Hans Verkuil09092782013-12-14 08:28:36 -03003269
3270 cmd.block_count = count / 3;
3271 cmd.nonblocking = file->f_flags & O_NONBLOCK;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003272 cmd.buffer = data;
3273 cmd.instance = file;
3274 cmd.result = -ENODEV;
Hans Verkuilb8e2a362013-02-10 09:24:14 -03003275 radio_enable(btv);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003276
Hans Verkuilb9218f22010-12-27 12:22:46 -03003277 bttv_call_all(btv, core, ioctl, SAA6588_CMD_READ, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003278
3279 return cmd.result;
3280}
3281
3282static unsigned int radio_poll(struct file *file, poll_table *wait)
3283{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003284 struct bttv_fh *fh = file->private_data;
3285 struct bttv *btv = fh->btv;
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003286 unsigned long req_events = poll_requested_events(wait);
Hans Verkuilb9218f22010-12-27 12:22:46 -03003287 struct saa6588_command cmd;
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003288 unsigned int res = 0;
3289
3290 if (v4l2_event_pending(&fh->fh))
3291 res = POLLPRI;
3292 else if (req_events & POLLPRI)
3293 poll_wait(file, &fh->fh.wait, wait);
Hans Verkuilb8e2a362013-02-10 09:24:14 -03003294 radio_enable(btv);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003295 cmd.instance = file;
3296 cmd.event_list = wait;
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003297 cmd.result = res;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003298 bttv_call_all(btv, core, ioctl, SAA6588_CMD_POLL, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003299
3300 return cmd.result;
3301}
3302
Hans Verkuilbec43662008-12-30 06:58:20 -03003303static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
3305 .owner = THIS_MODULE,
3306 .open = radio_open,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003307 .read = radio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 .release = radio_release,
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003309 .unlocked_ioctl = video_ioctl2,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003310 .poll = radio_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311};
3312
Hans Verkuila3998102008-07-21 02:57:38 -03003313static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil78dea1a2012-09-09 09:03:29 -03003314 .vidioc_querycap = bttv_querycap,
Hans Verkuil01df5302013-02-06 12:40:28 -03003315 .vidioc_log_status = bttv_log_status,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003316 .vidioc_g_tuner = radio_g_tuner,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003317 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003318 .vidioc_g_frequency = bttv_g_frequency,
3319 .vidioc_s_frequency = bttv_s_frequency,
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003320 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
3321 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322};
3323
Hans Verkuila3998102008-07-21 02:57:38 -03003324static struct video_device radio_template = {
3325 .fops = &radio_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003326 .ioctl_ops = &radio_ioctl_ops,
3327};
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329/* ----------------------------------------------------------------------- */
3330/* some debug code */
3331
Adrian Bunk408b6642005-05-01 08:59:29 -07003332static int bttv_risc_decode(u32 risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
3334 static char *instr[16] = {
3335 [ BT848_RISC_WRITE >> 28 ] = "write",
3336 [ BT848_RISC_SKIP >> 28 ] = "skip",
3337 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3338 [ BT848_RISC_JUMP >> 28 ] = "jump",
3339 [ BT848_RISC_SYNC >> 28 ] = "sync",
3340 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3341 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3342 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3343 };
3344 static int incr[16] = {
3345 [ BT848_RISC_WRITE >> 28 ] = 2,
3346 [ BT848_RISC_JUMP >> 28 ] = 2,
3347 [ BT848_RISC_SYNC >> 28 ] = 2,
3348 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3349 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3350 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3351 };
3352 static char *bits[] = {
3353 "be0", "be1", "be2", "be3/resync",
3354 "set0", "set1", "set2", "set3",
3355 "clr0", "clr1", "clr2", "clr3",
3356 "irq", "res", "eol", "sol",
3357 };
3358 int i;
3359
Joe Perches8af443e2011-08-21 19:56:48 -03003360 pr_cont("0x%08x [ %s", risc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3362 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3363 if (risc & (1 << (i + 12)))
Joe Perches8af443e2011-08-21 19:56:48 -03003364 pr_cont(" %s", bits[i]);
3365 pr_cont(" count=%d ]\n", risc & 0xfff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3367}
3368
Adrian Bunk408b6642005-05-01 08:59:29 -07003369static void bttv_risc_disasm(struct bttv *btv,
3370 struct btcx_riscmem *risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
3372 unsigned int i,j,n;
3373
Joe Perches8af443e2011-08-21 19:56:48 -03003374 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
3375 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 for (i = 0; i < (risc->size >> 2); i += n) {
Joe Perches8af443e2011-08-21 19:56:48 -03003377 pr_info("%s: 0x%lx: ",
3378 btv->c.v4l2_dev.name,
3379 (unsigned long)(risc->dma + (i<<2)));
Al Viro3aa71102008-06-22 14:20:09 -03003380 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 for (j = 1; j < n; j++)
Joe Perches8af443e2011-08-21 19:56:48 -03003382 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3383 btv->c.v4l2_dev.name,
3384 (unsigned long)(risc->dma + ((i+j)<<2)),
3385 risc->cpu[i+j], j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 if (0 == risc->cpu[i])
3387 break;
3388 }
3389}
3390
3391static void bttv_print_riscaddr(struct bttv *btv)
3392{
Joe Perches8af443e2011-08-21 19:56:48 -03003393 pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
3394 pr_info(" vbi : o=%08llx e=%08llx\n",
3395 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3396 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3397 pr_info(" cap : o=%08llx e=%08llx\n",
3398 btv->curr.top
3399 ? (unsigned long long)btv->curr.top->top.dma : 0,
3400 btv->curr.bottom
3401 ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3402 pr_info(" scr : o=%08llx e=%08llx\n",
3403 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3404 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 bttv_risc_disasm(btv, &btv->main);
3406}
3407
3408/* ----------------------------------------------------------------------- */
3409/* irq handler */
3410
3411static char *irq_name[] = {
3412 "FMTCHG", // format change detected (525 vs. 625)
3413 "VSYNC", // vertical sync (new field)
3414 "HSYNC", // horizontal sync
3415 "OFLOW", // chroma/luma AGC overflow
3416 "HLOCK", // horizontal lock changed
3417 "VPRES", // video presence changed
3418 "6", "7",
3419 "I2CDONE", // hw irc operation finished
3420 "GPINT", // gpio port triggered irq
3421 "10",
3422 "RISCI", // risc instruction triggered irq
3423 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3424 "FTRGT", // pixel data fifo overrun
3425 "FDSR", // fifo data stream resyncronisation
3426 "PPERR", // parity error (data transfer)
3427 "RIPERR", // parity error (read risc instructions)
3428 "PABORT", // pci abort
3429 "OCERR", // risc instruction error
3430 "SCERR", // syncronisation error
3431};
3432
3433static void bttv_print_irqbits(u32 print, u32 mark)
3434{
3435 unsigned int i;
3436
Joe Perches8af443e2011-08-21 19:56:48 -03003437 pr_cont("bits:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3439 if (print & (1 << i))
Joe Perches8af443e2011-08-21 19:56:48 -03003440 pr_cont(" %s", irq_name[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 if (mark & (1 << i))
Joe Perches8af443e2011-08-21 19:56:48 -03003442 pr_cont("*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 }
3444}
3445
3446static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3447{
Joe Perches8af443e2011-08-21 19:56:48 -03003448 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3449 btv->c.nr,
3450 (unsigned long)btv->main.dma,
3451 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3452 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3453 (unsigned long)rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
3455 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
Joe Perches8af443e2011-08-21 19:56:48 -03003456 pr_notice("%d: Oh, there (temporarily?) is no input signal. "
3457 "Ok, then this is harmless, don't worry ;)\n",
3458 btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 return;
3460 }
Joe Perches8af443e2011-08-21 19:56:48 -03003461 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
3462 btv->c.nr);
3463 pr_notice("%d: Lets try to catch the culpit red-handed ...\n",
3464 btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 dump_stack();
3466}
3467
3468static int
3469bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3470{
3471 struct bttv_buffer *item;
3472
3473 memset(set,0,sizeof(*set));
3474
3475 /* capture request ? */
3476 if (!list_empty(&btv->capture)) {
3477 set->frame_irq = 1;
3478 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3479 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3480 set->top = item;
3481 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3482 set->bottom = item;
3483
3484 /* capture request for other field ? */
3485 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3486 (item->vb.queue.next != &btv->capture)) {
3487 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
Mike Isely66349b42009-09-21 12:09:08 -03003488 /* Mike Isely <isely@pobox.com> - Only check
3489 * and set up the bottom field in the logic
3490 * below. Don't ever do the top field. This
3491 * of course means that if we set up the
3492 * bottom field in the above code that we'll
3493 * actually skip a field. But that's OK.
3494 * Having processed only a single buffer this
3495 * time, then the next time around the first
3496 * available buffer should be for a top field.
3497 * That will then cause us here to set up a
3498 * top then a bottom field in the normal way.
3499 * The alternative to this understanding is
3500 * that we set up the second available buffer
3501 * as a top field, but that's out of order
3502 * since this driver always processes the top
3503 * field first - the effect will be the two
3504 * buffers being returned in the wrong order,
3505 * with the second buffer also being delayed
3506 * by one field time (owing to the fifo nature
3507 * of videobuf). Worse still, we'll be stuck
3508 * doing fields out of order now every time
3509 * until something else causes a field to be
3510 * dropped. By effectively forcing a field to
3511 * drop this way then we always get back into
3512 * sync within a single frame time. (Out of
3513 * order fields can screw up deinterlacing
3514 * algorithms.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 if (NULL == set->bottom &&
3517 V4L2_FIELD_BOTTOM == item->vb.field) {
3518 set->bottom = item;
3519 }
3520 if (NULL != set->top && NULL != set->bottom)
3521 set->top_irq = 2;
3522 }
3523 }
3524 }
3525
3526 /* screen overlay ? */
3527 if (NULL != btv->screen) {
3528 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3529 if (NULL == set->top && NULL == set->bottom) {
3530 set->top = btv->screen;
3531 set->bottom = btv->screen;
3532 }
3533 } else {
3534 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3535 NULL == set->top) {
3536 set->top = btv->screen;
3537 }
3538 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3539 NULL == set->bottom) {
3540 set->bottom = btv->screen;
3541 }
3542 }
3543 }
3544
Joe Perches8af443e2011-08-21 19:56:48 -03003545 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3546 btv->c.nr, set->top, set->bottom,
3547 btv->screen, set->frame_irq, set->top_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 return 0;
3549}
3550
3551static void
3552bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3553 struct bttv_buffer_set *curr, unsigned int state)
3554{
3555 struct timeval ts;
3556
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003557 v4l2_get_timestamp(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
3559 if (wakeup->top == wakeup->bottom) {
3560 if (NULL != wakeup->top && curr->top != wakeup->top) {
3561 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003562 pr_debug("%d: wakeup: both=%p\n",
3563 btv->c.nr, wakeup->top);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 wakeup->top->vb.ts = ts;
3565 wakeup->top->vb.field_count = btv->field_count;
3566 wakeup->top->vb.state = state;
3567 wake_up(&wakeup->top->vb.done);
3568 }
3569 } else {
3570 if (NULL != wakeup->top && curr->top != wakeup->top) {
3571 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003572 pr_debug("%d: wakeup: top=%p\n",
3573 btv->c.nr, wakeup->top);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 wakeup->top->vb.ts = ts;
3575 wakeup->top->vb.field_count = btv->field_count;
3576 wakeup->top->vb.state = state;
3577 wake_up(&wakeup->top->vb.done);
3578 }
3579 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3580 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003581 pr_debug("%d: wakeup: bottom=%p\n",
3582 btv->c.nr, wakeup->bottom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 wakeup->bottom->vb.ts = ts;
3584 wakeup->bottom->vb.field_count = btv->field_count;
3585 wakeup->bottom->vb.state = state;
3586 wake_up(&wakeup->bottom->vb.done);
3587 }
3588 }
3589}
3590
3591static void
3592bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3593 unsigned int state)
3594{
3595 struct timeval ts;
3596
3597 if (NULL == wakeup)
3598 return;
3599
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003600 v4l2_get_timestamp(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 wakeup->vb.ts = ts;
3602 wakeup->vb.field_count = btv->field_count;
3603 wakeup->vb.state = state;
3604 wake_up(&wakeup->vb.done);
3605}
3606
3607static void bttv_irq_timeout(unsigned long data)
3608{
3609 struct bttv *btv = (struct bttv *)data;
3610 struct bttv_buffer_set old,new;
3611 struct bttv_buffer *ovbi;
3612 struct bttv_buffer *item;
3613 unsigned long flags;
3614
3615 if (bttv_verbose) {
Joe Perches8af443e2011-08-21 19:56:48 -03003616 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3617 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3618 btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 bttv_print_irqbits(btread(BT848_INT_STAT),0);
Joe Perches8af443e2011-08-21 19:56:48 -03003620 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 }
3622
3623 spin_lock_irqsave(&btv->s_lock,flags);
3624
3625 /* deactivate stuff */
3626 memset(&new,0,sizeof(new));
3627 old = btv->curr;
3628 ovbi = btv->cvbi;
3629 btv->curr = new;
3630 btv->cvbi = NULL;
3631 btv->loop_irq = 0;
3632 bttv_buffer_activate_video(btv, &new);
3633 bttv_buffer_activate_vbi(btv, NULL);
3634 bttv_set_dma(btv, 0);
3635
3636 /* wake up */
Brandon Philips0fc06862007-11-06 20:02:36 -03003637 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3638 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
3640 /* cancel all outstanding capture / vbi requests */
3641 while (!list_empty(&btv->capture)) {
3642 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3643 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003644 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 wake_up(&item->vb.done);
3646 }
3647 while (!list_empty(&btv->vcapture)) {
3648 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3649 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003650 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 wake_up(&item->vb.done);
3652 }
3653
3654 btv->errors++;
3655 spin_unlock_irqrestore(&btv->s_lock,flags);
3656}
3657
3658static void
3659bttv_irq_wakeup_top(struct bttv *btv)
3660{
3661 struct bttv_buffer *wakeup = btv->curr.top;
3662
3663 if (NULL == wakeup)
3664 return;
3665
3666 spin_lock(&btv->s_lock);
3667 btv->curr.top_irq = 0;
3668 btv->curr.top = NULL;
3669 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3670
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003671 v4l2_get_timestamp(&wakeup->vb.ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 wakeup->vb.field_count = btv->field_count;
Brandon Philips0fc06862007-11-06 20:02:36 -03003673 wakeup->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 wake_up(&wakeup->vb.done);
3675 spin_unlock(&btv->s_lock);
3676}
3677
3678static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3679{
3680 if (rc < risc->dma)
3681 return 0;
3682 if (rc > risc->dma + risc->size)
3683 return 0;
3684 return 1;
3685}
3686
3687static void
3688bttv_irq_switch_video(struct bttv *btv)
3689{
3690 struct bttv_buffer_set new;
3691 struct bttv_buffer_set old;
3692 dma_addr_t rc;
3693
3694 spin_lock(&btv->s_lock);
3695
3696 /* new buffer set */
3697 bttv_irq_next_video(btv, &new);
3698 rc = btread(BT848_RISC_COUNT);
3699 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3700 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3701 btv->framedrop++;
3702 if (debug_latency)
3703 bttv_irq_debug_low_latency(btv, rc);
3704 spin_unlock(&btv->s_lock);
3705 return;
3706 }
3707
3708 /* switch over */
3709 old = btv->curr;
3710 btv->curr = new;
3711 btv->loop_irq &= ~1;
3712 bttv_buffer_activate_video(btv, &new);
3713 bttv_set_dma(btv, 0);
3714
3715 /* switch input */
3716 if (UNSET != btv->new_input) {
3717 video_mux(btv,btv->new_input);
3718 btv->new_input = UNSET;
3719 }
3720
3721 /* wake up finished buffers */
Brandon Philips0fc06862007-11-06 20:02:36 -03003722 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 spin_unlock(&btv->s_lock);
3724}
3725
3726static void
3727bttv_irq_switch_vbi(struct bttv *btv)
3728{
3729 struct bttv_buffer *new = NULL;
3730 struct bttv_buffer *old;
3731 u32 rc;
3732
3733 spin_lock(&btv->s_lock);
3734
3735 if (!list_empty(&btv->vcapture))
3736 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3737 old = btv->cvbi;
3738
3739 rc = btread(BT848_RISC_COUNT);
3740 if (NULL != old && (is_active(&old->top, rc) ||
3741 is_active(&old->bottom, rc))) {
3742 btv->framedrop++;
3743 if (debug_latency)
3744 bttv_irq_debug_low_latency(btv, rc);
3745 spin_unlock(&btv->s_lock);
3746 return;
3747 }
3748
3749 /* switch */
3750 btv->cvbi = new;
3751 btv->loop_irq &= ~4;
3752 bttv_buffer_activate_vbi(btv, new);
3753 bttv_set_dma(btv, 0);
3754
Brandon Philips0fc06862007-11-06 20:02:36 -03003755 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 spin_unlock(&btv->s_lock);
3757}
3758
David Howells7d12e782006-10-05 14:55:46 +01003759static irqreturn_t bttv_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760{
3761 u32 stat,astat;
3762 u32 dstat;
3763 int count;
3764 struct bttv *btv;
3765 int handled = 0;
3766
3767 btv=(struct bttv *)dev_id;
Mark Weaver6c6c0b22005-11-13 16:07:52 -08003768
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 count=0;
3770 while (1) {
3771 /* get/clear interrupt status bits */
3772 stat=btread(BT848_INT_STAT);
3773 astat=stat&btread(BT848_INT_MASK);
3774 if (!astat)
3775 break;
3776 handled = 1;
3777 btwrite(stat,BT848_INT_STAT);
3778
3779 /* get device status bits */
3780 dstat=btread(BT848_DSTATUS);
3781
3782 if (irq_debug) {
Joe Perches8af443e2011-08-21 19:56:48 -03003783 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
3784 btv->c.nr, count, btv->field_count,
3785 stat>>28, btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 bttv_print_irqbits(stat,astat);
3787 if (stat & BT848_INT_HLOCK)
Joe Perches8af443e2011-08-21 19:56:48 -03003788 pr_cont(" HLOC => %s",
3789 dstat & BT848_DSTATUS_HLOC
3790 ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 if (stat & BT848_INT_VPRES)
Joe Perches8af443e2011-08-21 19:56:48 -03003792 pr_cont(" PRES => %s",
3793 dstat & BT848_DSTATUS_PRES
3794 ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 if (stat & BT848_INT_FMTCHG)
Joe Perches8af443e2011-08-21 19:56:48 -03003796 pr_cont(" NUML => %s",
3797 dstat & BT848_DSTATUS_NUML
3798 ? "625" : "525");
3799 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 }
3801
3802 if (astat&BT848_INT_VSYNC)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003803 btv->field_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02003805 if ((astat & BT848_INT_GPINT) && btv->remote) {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02003806 bttv_input_irq(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
3808
3809 if (astat & BT848_INT_I2CDONE) {
3810 btv->i2c_done = stat;
3811 wake_up(&btv->i2c_queue);
3812 }
3813
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003814 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 bttv_irq_switch_vbi(btv);
3816
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003817 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 bttv_irq_wakeup_top(btv);
3819
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003820 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 bttv_irq_switch_video(btv);
3822
3823 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
Frank Schaefer5f4567812013-03-21 13:51:17 -03003824 /* trigger automute */
3825 audio_mux_gpio(btv, btv->audio_input, btv->mute);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
3827 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
Joe Perches8af443e2011-08-21 19:56:48 -03003828 pr_info("%d: %s%s @ %08x,",
3829 btv->c.nr,
3830 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3831 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3832 btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 bttv_print_irqbits(stat,astat);
Joe Perches8af443e2011-08-21 19:56:48 -03003834 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 if (bttv_debug)
3836 bttv_print_riscaddr(btv);
3837 }
3838 if (fdsr && astat & BT848_INT_FDSR) {
Joe Perches8af443e2011-08-21 19:56:48 -03003839 pr_info("%d: FDSR @ %08x\n",
3840 btv->c.nr, btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 if (bttv_debug)
3842 bttv_print_riscaddr(btv);
3843 }
3844
3845 count++;
3846 if (count > 4) {
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08003847
3848 if (count > 8 || !(astat & BT848_INT_GPINT)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003849 btwrite(0, BT848_INT_MASK);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08003850
Joe Perches8af443e2011-08-21 19:56:48 -03003851 pr_err("%d: IRQ lockup, cleared int mask [",
3852 btv->c.nr);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08003853 } else {
Joe Perches8af443e2011-08-21 19:56:48 -03003854 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
3855 btv->c.nr);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08003856
3857 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3858 BT848_INT_MASK);
3859 };
3860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 bttv_print_irqbits(stat,astat);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08003862
Joe Perches8af443e2011-08-21 19:56:48 -03003863 pr_cont("]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 }
3865 }
3866 btv->irq_total++;
3867 if (handled)
3868 btv->irq_me++;
3869 return IRQ_RETVAL(handled);
3870}
3871
3872
3873/* ----------------------------------------------------------------------- */
Hans Verkuild9b67072013-02-06 11:43:07 -03003874/* initialization */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
3876static struct video_device *vdev_init(struct bttv *btv,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03003877 const struct video_device *template,
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03003878 const char *type_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879{
3880 struct video_device *vfd;
3881
3882 vfd = video_device_alloc();
3883 if (NULL == vfd)
3884 return NULL;
3885 *vfd = *template;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003886 vfd->v4l2_dev = &btv->c.v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 vfd->release = video_device_release;
Mauro Carvalho Chehab1d0a4362008-06-23 12:31:29 -03003888 vfd->debug = bttv_debug;
Hans Verkuil8c14cc12013-02-06 12:42:40 -03003889 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003890 video_set_drvdata(vfd, btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3892 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03003893 type_name, bttv_tvcards[btv->c.type].name);
Hans Verkuild9b67072013-02-06 11:43:07 -03003894 if (btv->tuner_type == TUNER_ABSENT) {
3895 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
3896 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
3897 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
3898 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
3899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 return vfd;
3901}
3902
3903static void bttv_unregister_video(struct bttv *btv)
3904{
3905 if (btv->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03003906 if (video_is_registered(btv->video_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 video_unregister_device(btv->video_dev);
3908 else
3909 video_device_release(btv->video_dev);
3910 btv->video_dev = NULL;
3911 }
3912 if (btv->vbi_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03003913 if (video_is_registered(btv->vbi_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 video_unregister_device(btv->vbi_dev);
3915 else
3916 video_device_release(btv->vbi_dev);
3917 btv->vbi_dev = NULL;
3918 }
3919 if (btv->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03003920 if (video_is_registered(btv->radio_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 video_unregister_device(btv->radio_dev);
3922 else
3923 video_device_release(btv->radio_dev);
3924 btv->radio_dev = NULL;
3925 }
3926}
3927
3928/* register video4linux devices */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003929static int bttv_register_video(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930{
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03003931 if (no_overlay > 0)
Joe Perches8af443e2011-08-21 19:56:48 -03003932 pr_notice("Overlay support disabled\n");
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07003933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 /* video */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03003935 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03003936
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003937 if (NULL == btv->video_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03003939 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
3940 video_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03003942 pr_info("%d: registered device %s\n",
3943 btv->c.nr, video_device_node_name(btv->video_dev));
Hans Verkuil22a04f12008-07-20 06:35:02 -03003944 if (device_create_file(&btv->video_dev->dev,
Kay Sievers54bd5b62007-10-08 16:26:13 -03003945 &dev_attr_card)<0) {
Joe Perches8af443e2011-08-21 19:56:48 -03003946 pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
Trent Piephod94fc9a2006-07-29 17:18:06 -03003947 goto err;
3948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
3950 /* vbi */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03003951 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03003952
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003953 if (NULL == btv->vbi_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03003955 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
3956 vbi_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03003958 pr_info("%d: registered device %s\n",
3959 btv->c.nr, video_device_node_name(btv->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003961 if (!btv->has_radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 return 0;
3963 /* radio */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03003964 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003965 if (NULL == btv->radio_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 goto err;
Hans Verkuil01df5302013-02-06 12:40:28 -03003967 btv->radio_dev->ctrl_handler = &btv->radio_ctrl_handler;
Jean Delvare176c2f32008-09-07 12:49:59 -03003968 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
3969 radio_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03003971 pr_info("%d: registered device %s\n",
3972 btv->c.nr, video_device_node_name(btv->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
3974 /* all done */
3975 return 0;
3976
3977 err:
3978 bttv_unregister_video(btv);
3979 return -1;
3980}
3981
3982
3983/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3984/* response on cards with no firmware is not enabled by OF */
3985static void pci_set_command(struct pci_dev *dev)
3986{
3987#if defined(__powerpc__)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003988 unsigned int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003990 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3991 cmd = (cmd | PCI_COMMAND_MEMORY );
3992 pci_write_config_dword(dev, PCI_COMMAND, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993#endif
3994}
3995
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003996static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
Hans Verkuil76ea9922013-02-06 11:49:14 -03003998 struct v4l2_frequency init_freq = {
3999 .tuner = 0,
4000 .type = V4L2_TUNER_ANALOG_TV,
4001 .frequency = 980,
4002 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 int result;
4004 unsigned char lat;
4005 struct bttv *btv;
Hans Verkuil01df5302013-02-06 12:40:28 -03004006 struct v4l2_ctrl_handler *hdl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
4008 if (bttv_num == BTTV_MAX)
4009 return -ENOMEM;
Joe Perches8af443e2011-08-21 19:56:48 -03004010 pr_info("Bt8xx card found (%d)\n", bttv_num);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004011 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004012 if (btv == NULL) {
Joe Perches8af443e2011-08-21 19:56:48 -03004013 pr_err("out of memory\n");
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004014 return -ENOMEM;
4015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 btv->c.nr = bttv_num;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004017 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4018 "bttv%d", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
4020 /* initialize structs / fill in defaults */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02004021 mutex_init(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004022 spin_lock_init(&btv->s_lock);
4023 spin_lock_init(&btv->gpio_lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004024 init_waitqueue_head(&btv->i2c_queue);
4025 INIT_LIST_HEAD(&btv->c.subs);
4026 INIT_LIST_HEAD(&btv->capture);
4027 INIT_LIST_HEAD(&btv->vcapture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
4029 init_timer(&btv->timeout);
4030 btv->timeout.function = bttv_irq_timeout;
4031 btv->timeout.data = (unsigned long)btv;
4032
Michael Krufky7c08fb02005-11-08 21:36:21 -08004033 btv->i2c_rc = -1;
4034 btv->tuner_type = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 btv->new_input = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 btv->has_radio=radio[btv->c.nr];
4037
4038 /* pci stuff (init, get irq/mmio, ... */
4039 btv->c.pci = dev;
Michael Krufky7c08fb02005-11-08 21:36:21 -08004040 btv->id = dev->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 if (pci_enable_device(dev)) {
Joe Perches8af443e2011-08-21 19:56:48 -03004042 pr_warn("%d: Can't enable device\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 return -EIO;
4044 }
Yang Hongyang284901a2009-04-06 19:01:15 -07004045 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Joe Perches8af443e2011-08-21 19:56:48 -03004046 pr_warn("%d: No suitable DMA available\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 return -EIO;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 if (!request_mem_region(pci_resource_start(dev,0),
4050 pci_resource_len(dev,0),
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004051 btv->c.v4l2_dev.name)) {
Joe Perches8af443e2011-08-21 19:56:48 -03004052 pr_warn("%d: can't request iomem (0x%llx)\n",
4053 btv->c.nr,
4054 (unsigned long long)pci_resource_start(dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 return -EBUSY;
4056 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004057 pci_set_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 pci_set_command(dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004059
4060 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4061 if (result < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004062 pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004063 goto fail0;
4064 }
Hans Verkuil01df5302013-02-06 12:40:28 -03004065 hdl = &btv->ctrl_handler;
4066 v4l2_ctrl_handler_init(hdl, 20);
4067 btv->c.v4l2_dev.ctrl_handler = hdl;
4068 v4l2_ctrl_handler_init(&btv->radio_ctrl_handler, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
Bjørn Morkabd34d82011-03-21 11:35:56 -03004070 btv->revision = dev->revision;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004071 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
Joe Perches8af443e2011-08-21 19:56:48 -03004072 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
4073 bttv_num, btv->id, btv->revision, pci_name(dev),
4074 btv->c.pci->irq, lat,
4075 (unsigned long long)pci_resource_start(dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 schedule();
4077
Akinobu Mita5f1693f2006-12-20 10:08:56 -03004078 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4079 if (NULL == btv->bt848_mmio) {
Joe Perches8af443e2011-08-21 19:56:48 -03004080 pr_err("%d: ioremap() failed\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 result = -EIO;
4082 goto fail1;
4083 }
4084
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004085 /* identify card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 bttv_idcard(btv);
4087
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004088 /* disable irqs, register irq handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 btwrite(0, BT848_INT_MASK);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004090 result = request_irq(btv->c.pci->irq, bttv_irq,
Michael Opdenacker3e018fe2013-10-13 02:49:29 -03004091 IRQF_SHARED, btv->c.v4l2_dev.name, (void *)btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004092 if (result < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004093 pr_err("%d: can't get IRQ %d\n",
4094 bttv_num, btv->c.pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 goto fail1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
4098 if (0 != bttv_handle_chipset(btv)) {
4099 result = -EIO;
4100 goto fail2;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
4103 /* init options from insmod args */
4104 btv->opt_combfilter = combfilter;
Hans Verkuil01df5302013-02-06 12:40:28 -03004105 bttv_ctrl_combfilter.def = combfilter;
4106 bttv_ctrl_lumafilter.def = lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 btv->opt_automute = automute;
Hans Verkuil01df5302013-02-06 12:40:28 -03004108 bttv_ctrl_automute.def = automute;
4109 bttv_ctrl_agc_crush.def = agc_crush;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 btv->opt_vcr_hack = vcr_hack;
Hans Verkuil01df5302013-02-06 12:40:28 -03004111 bttv_ctrl_vcr_hack.def = vcr_hack;
4112 bttv_ctrl_whitecrush_upper.def = whitecrush_upper;
4113 bttv_ctrl_whitecrush_lower.def = whitecrush_lower;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07004114 btv->opt_uv_ratio = uv_ratio;
Hans Verkuil01df5302013-02-06 12:40:28 -03004115 bttv_ctrl_uv_ratio.def = uv_ratio;
4116 bttv_ctrl_full_luma.def = full_luma_range;
4117 bttv_ctrl_coring.def = coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
4119 /* fill struct bttv with some useful defaults */
4120 btv->init.btv = btv;
4121 btv->init.ov.w.width = 320;
4122 btv->init.ov.w.height = 240;
Mauro Carvalho Chehabc96dd072007-10-26 16:51:47 -03004123 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 btv->init.width = 320;
4125 btv->init.height = 240;
Hans Verkuilc13eb702013-02-07 07:56:11 -03004126 btv->init.ov.w.width = 320;
4127 btv->init.ov.w.height = 240;
4128 btv->init.ov.field = V4L2_FIELD_INTERLACED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 btv->input = 0;
4130
Hans Verkuil01df5302013-02-06 12:40:28 -03004131 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4132 V4L2_CID_BRIGHTNESS, 0, 0xff00, 0x100, 32768);
4133 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4134 V4L2_CID_CONTRAST, 0, 0xff80, 0x80, 0x6c00);
4135 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4136 V4L2_CID_SATURATION, 0, 0xff80, 0x80, 32768);
4137 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4138 V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
4139 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4140 V4L2_CID_HUE, 0, 0xff00, 0x100, 32768);
4141 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4142 V4L2_CID_CHROMA_AGC, 0, 1, 1, !!chroma_agc);
4143 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4144 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
4145 if (btv->volume_gpio)
4146 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4147 V4L2_CID_AUDIO_VOLUME, 0, 0xff00, 0x100, 0xff00);
4148 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_combfilter, NULL);
4149 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_automute, NULL);
4150 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_lumafilter, NULL);
4151 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_agc_crush, NULL);
4152 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_vcr_hack, NULL);
4153 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_lower, NULL);
4154 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_upper, NULL);
4155 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_uv_ratio, NULL);
4156 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_full_luma, NULL);
4157 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_coring, NULL);
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 /* initialize hardware */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004160 if (bttv_gpio)
4161 bttv_gpio_tracking(btv,"pre-init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
4163 bttv_risc_init_main(btv);
4164 init_bt848(btv);
4165
4166 /* gpio */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004167 btwrite(0x00, BT848_GPIO_REG_INP);
4168 btwrite(0x00, BT848_GPIO_OUT_EN);
4169 if (bttv_verbose)
4170 bttv_gpio_tracking(btv,"init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004172 /* needs to be done before i2c is registered */
4173 bttv_init_card1(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004175 /* register i2c + gpio */
4176 init_bttv_i2c(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004178 /* some card-specific stuff (needs working i2c) */
4179 bttv_init_card2(btv);
Hans Verkuil2c905772009-07-20 08:14:17 -03004180 bttv_init_tuner(btv);
Hans Verkuil76ea9922013-02-06 11:49:14 -03004181 if (btv->tuner_type != TUNER_ABSENT) {
4182 bttv_set_frequency(btv, &init_freq);
4183 btv->radio_freq = 90500 * 16; /* 90.5Mhz default */
4184 }
Hans Verkuil6795cc52013-02-06 12:43:07 -03004185 btv->std = V4L2_STD_PAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 init_irqreg(btv);
Hans Verkuilf8e1b692013-11-11 08:16:03 -03004187 if (!bttv_tvcards[btv->c.type].no_video)
4188 v4l2_ctrl_handler_setup(hdl);
Hans Verkuil01df5302013-02-06 12:40:28 -03004189 if (hdl->error) {
4190 result = hdl->error;
4191 goto fail2;
4192 }
Frank Schaeferda9f07f2013-03-21 13:51:19 -03004193 /* mute device */
4194 audio_mute(btv, 1);
4195
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004196 /* register video4linux + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 if (!bttv_tvcards[btv->c.type].no_video) {
Hans Verkuil01df5302013-02-06 12:40:28 -03004198 v4l2_ctrl_add_handler(&btv->radio_ctrl_handler, hdl,
4199 v4l2_ctrl_radio_filter);
4200 if (btv->radio_ctrl_handler.error) {
4201 result = btv->radio_ctrl_handler.error;
4202 goto fail2;
4203 }
Trent Piepho333408f2007-07-03 15:08:10 -03004204 set_input(btv, 0, btv->tvnorm);
Michael Schimeke5bd0262007-01-18 16:17:39 -03004205 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4206 btv->crop[1] = btv->crop[0]; /* current = default */
4207 disclaim_vbi_lines(btv);
4208 disclaim_video_lines(btv);
Hans Verkuil01df5302013-02-06 12:40:28 -03004209 bttv_register_video(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 }
4211
Jarod Wilsonf992a492007-03-24 15:23:50 -03004212 /* add subdevices and autoload dvb-bt8xx if needed */
4213 if (bttv_tvcards[btv->c.type].has_dvb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 bttv_sub_add_device(&btv->c, "dvb");
Jarod Wilsonf992a492007-03-24 15:23:50 -03004215 request_modules(btv);
4216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
Jean Delvared90a4ae2010-02-16 14:22:37 -03004218 if (!disable_ir) {
4219 init_bttv_i2c_ir(btv);
4220 bttv_input_init(btv);
4221 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 /* everything is fine */
4224 bttv_num++;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004227fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004228 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004230fail1:
Hans Verkuil01df5302013-02-06 12:40:28 -03004231 v4l2_ctrl_handler_free(&btv->ctrl_handler);
4232 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004233 v4l2_device_unregister(&btv->c.v4l2_dev);
4234
4235fail0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 if (btv->bt848_mmio)
4237 iounmap(btv->bt848_mmio);
4238 release_mem_region(pci_resource_start(btv->c.pci,0),
4239 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 return result;
4241}
4242
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004243static void bttv_remove(struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004245 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4246 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
4248 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03004249 pr_info("%d: unloading\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Tejun Heo707bcf32010-12-24 16:14:20 +01004251 if (bttv_tvcards[btv->c.type].has_dvb)
4252 flush_request_modules(btv);
4253
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004254 /* shutdown everything (DMA+IRQs) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 btand(~15, BT848_GPIO_DMA_CTL);
4256 btwrite(0, BT848_INT_MASK);
4257 btwrite(~0x0, BT848_INT_STAT);
4258 btwrite(0x0, BT848_GPIO_OUT_EN);
4259 if (bttv_gpio)
4260 bttv_gpio_tracking(btv,"cleanup");
4261
4262 /* tell gpio modules we are leaving ... */
4263 btv->shutdown=1;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004264 bttv_input_fini(btv);
Christopher Pascoe889aee82006-01-09 15:25:28 -02004265 bttv_sub_del_devices(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004267 /* unregister i2c_bus + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 fini_bttv_i2c(btv);
4269
4270 /* unregister video4linux */
4271 bttv_unregister_video(btv);
4272
4273 /* free allocated memory */
Hans Verkuil01df5302013-02-06 12:40:28 -03004274 v4l2_ctrl_handler_free(&btv->ctrl_handler);
4275 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 btcx_riscmem_free(btv->c.pci,&btv->main);
4277
Hans Verkuil01df5302013-02-06 12:40:28 -03004278 /* free resources */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004279 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 iounmap(btv->bt848_mmio);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004281 release_mem_region(pci_resource_start(btv->c.pci,0),
4282 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004284 v4l2_device_unregister(&btv->c.v4l2_dev);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004285 bttvs[btv->c.nr] = NULL;
4286 kfree(btv);
4287
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004288 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289}
4290
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004291#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4293{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004294 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4295 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 struct bttv_buffer_set idle;
4297 unsigned long flags;
4298
Joe Perches8af443e2011-08-21 19:56:48 -03004299 dprintk("%d: suspend %d\n", btv->c.nr, state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
4301 /* stop dma + irqs */
4302 spin_lock_irqsave(&btv->s_lock,flags);
4303 memset(&idle, 0, sizeof(idle));
4304 btv->state.video = btv->curr;
4305 btv->state.vbi = btv->cvbi;
4306 btv->state.loop_irq = btv->loop_irq;
4307 btv->curr = idle;
4308 btv->loop_irq = 0;
4309 bttv_buffer_activate_video(btv, &idle);
4310 bttv_buffer_activate_vbi(btv, NULL);
4311 bttv_set_dma(btv, 0);
4312 btwrite(0, BT848_INT_MASK);
4313 spin_unlock_irqrestore(&btv->s_lock,flags);
4314
4315 /* save bt878 state */
4316 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4317 btv->state.gpio_data = gpio_read();
4318
4319 /* save pci state */
4320 pci_save_state(pci_dev);
4321 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4322 pci_disable_device(pci_dev);
4323 btv->state.disabled = 1;
4324 }
4325 return 0;
4326}
4327
4328static int bttv_resume(struct pci_dev *pci_dev)
4329{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004330 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4331 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004333 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
Joe Perches8af443e2011-08-21 19:56:48 -03004335 dprintk("%d: resume\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
4337 /* restore pci state */
4338 if (btv->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004339 err=pci_enable_device(pci_dev);
4340 if (err) {
Joe Perches8af443e2011-08-21 19:56:48 -03004341 pr_warn("%d: Can't enable device\n", btv->c.nr);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004342 return err;
4343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 btv->state.disabled = 0;
4345 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004346 err=pci_set_power_state(pci_dev, PCI_D0);
4347 if (err) {
4348 pci_disable_device(pci_dev);
Joe Perches8af443e2011-08-21 19:56:48 -03004349 pr_warn("%d: Can't enable device\n", btv->c.nr);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004350 btv->state.disabled = 1;
4351 return err;
4352 }
4353
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 pci_restore_state(pci_dev);
4355
4356 /* restore bt878 state */
4357 bttv_reinit_bt848(btv);
4358 gpio_inout(0xffffff, btv->state.gpio_enable);
4359 gpio_write(btv->state.gpio_data);
4360
4361 /* restart dma */
4362 spin_lock_irqsave(&btv->s_lock,flags);
4363 btv->curr = btv->state.video;
4364 btv->cvbi = btv->state.vbi;
4365 btv->loop_irq = btv->state.loop_irq;
4366 bttv_buffer_activate_video(btv, &btv->curr);
4367 bttv_buffer_activate_vbi(btv, btv->cvbi);
4368 bttv_set_dma(btv, 0);
4369 spin_unlock_irqrestore(&btv->s_lock,flags);
4370 return 0;
4371}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004372#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
4374static struct pci_device_id bttv_pci_tbl[] = {
Joe Perches76e97412009-07-05 15:59:21 -03004375 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4376 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4377 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4378 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
Peter De Schrijverc540d9f2011-11-06 10:47:58 -03004379 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004380 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381};
4382
4383MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4384
4385static struct pci_driver bttv_pci_driver = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004386 .name = "bttv",
4387 .id_table = bttv_pci_tbl,
4388 .probe = bttv_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004389 .remove = bttv_remove,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004390#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 .suspend = bttv_suspend,
4392 .resume = bttv_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004393#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394};
4395
Adrian Bunk7d44e892007-12-11 19:23:43 -03004396static int __init bttv_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397{
Randy Dunlapc526e222006-07-15 09:08:26 -03004398 int ret;
4399
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 bttv_num = 0;
4401
Joe Perches8af443e2011-08-21 19:56:48 -03004402 pr_info("driver version %s loaded\n", BTTV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4404 gbuffers = 2;
Filipe Rossetf41b6962009-05-28 11:11:53 -03004405 if (gbufsize > BTTV_MAX_FBUF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 gbufsize = BTTV_MAX_FBUF;
4407 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4408 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03004409 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
4410 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
4412 bttv_check_chipset();
4413
Randy Dunlapc526e222006-07-15 09:08:26 -03004414 ret = bus_register(&bttv_sub_bus_type);
4415 if (ret < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004416 pr_warn("bus_register error: %d\n", ret);
Randy Dunlapc526e222006-07-15 09:08:26 -03004417 return ret;
4418 }
Akinobu Mita9e7e85e2007-12-17 14:26:29 -03004419 ret = pci_register_driver(&bttv_pci_driver);
4420 if (ret < 0)
4421 bus_unregister(&bttv_sub_bus_type);
4422
4423 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424}
4425
Adrian Bunk7d44e892007-12-11 19:23:43 -03004426static void __exit bttv_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
4428 pci_unregister_driver(&bttv_pci_driver);
4429 bus_unregister(&bttv_sub_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430}
4431
4432module_init(bttv_init_module);
4433module_exit(bttv_cleanup_module);
4434
4435/*
4436 * Local variables:
4437 * c-basic-offset: 8
4438 * End:
4439 */