blob: e33a2d2f81a877244806fab9d45b6da886c20241 [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 Verkuil1b9e94d2012-09-09 09:23:31 -030053#include <media/v4l2-chip-ident.h>
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -030054#include <media/tvaudio.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030055#include <media/msp3400.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020056
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -070057#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#include <asm/io.h>
60#include <asm/byteorder.h>
61
Hans Verkuilb9218f22010-12-27 12:22:46 -030062#include <media/saa6588.h>
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070063
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030064#define BTTV_VERSION "0.9.19"
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066unsigned int bttv_num; /* number of Bt848s in use */
Trent Piepho4b10d3b2009-01-28 21:32:59 -030067struct bttv *bttvs[BTTV_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020069unsigned int bttv_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070unsigned int bttv_verbose = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020071unsigned int bttv_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/* config variables */
74#ifdef __BIG_ENDIAN
75static unsigned int bigendian=1;
76#else
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020077static unsigned int bigendian;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#endif
79static unsigned int radio[BTTV_MAX];
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020080static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static unsigned int gbuffers = 8;
82static unsigned int gbufsize = 0x208000;
Michael Schimeke5bd0262007-01-18 16:17:39 -030083static unsigned int reset_crop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Jean Delvare176c2f32008-09-07 12:49:59 -030085static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
86static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
87static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020088static int debug_latency;
Jean Delvared90a4ae2010-02-16 14:22:37 -030089static int disable_ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020091static unsigned int fdsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/* options */
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020094static unsigned int combfilter;
95static unsigned int lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static unsigned int automute = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020097static unsigned int chroma_agc;
Hans Verkuil01df5302013-02-06 12:40:28 -030098static unsigned int agc_crush = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static unsigned int whitecrush_upper = 0xCF;
100static unsigned int whitecrush_lower = 0x7F;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200101static unsigned int vcr_hack;
102static unsigned int irq_iswitch;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700103static unsigned int uv_ratio = 50;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200104static unsigned int full_luma_range;
105static unsigned int coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* API features (turn on/off stuff for testing) */
108static unsigned int v4l2 = 1;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/* insmod args */
111module_param(bttv_verbose, int, 0644);
112module_param(bttv_gpio, int, 0644);
113module_param(bttv_debug, int, 0644);
114module_param(irq_debug, int, 0644);
115module_param(debug_latency, int, 0644);
Jean Delvared90a4ae2010-02-16 14:22:37 -0300116module_param(disable_ir, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118module_param(fdsr, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119module_param(gbuffers, int, 0444);
120module_param(gbufsize, int, 0444);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300121module_param(reset_crop, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123module_param(v4l2, int, 0644);
124module_param(bigendian, int, 0644);
125module_param(irq_iswitch, int, 0644);
126module_param(combfilter, int, 0444);
127module_param(lumafilter, int, 0444);
128module_param(automute, int, 0444);
129module_param(chroma_agc, int, 0444);
Hans Verkuil01df5302013-02-06 12:40:28 -0300130module_param(agc_crush, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131module_param(whitecrush_upper, int, 0444);
132module_param(whitecrush_lower, int, 0444);
133module_param(vcr_hack, int, 0444);
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700134module_param(uv_ratio, int, 0444);
135module_param(full_luma_range, int, 0444);
136module_param(coring, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jean Delvare176c2f32008-09-07 12:49:59 -0300138module_param_array(radio, int, NULL, 0444);
139module_param_array(video_nr, int, NULL, 0444);
140module_param_array(radio_nr, int, NULL, 0444);
141module_param_array(vbi_nr, int, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Hans Verkuil01df5302013-02-06 12:40:28 -0300143MODULE_PARM_DESC(radio, "The TV card supports radio, default is 0 (no)");
144MODULE_PARM_DESC(bigendian, "byte order of the framebuffer, default is native endian");
145MODULE_PARM_DESC(bttv_verbose, "verbose startup messages, default is 1 (yes)");
146MODULE_PARM_DESC(bttv_gpio, "log gpio changes, default is 0 (no)");
147MODULE_PARM_DESC(bttv_debug, "debug messages, default is 0 (no)");
148MODULE_PARM_DESC(irq_debug, "irq handler debug messages, default is 0 (no)");
Jean Delvared90a4ae2010-02-16 14:22:37 -0300149MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
Hans Verkuil01df5302013-02-06 12:40:28 -0300150MODULE_PARM_DESC(gbuffers, "number of capture buffers. range 2-32, default 8");
151MODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 0x208000");
152MODULE_PARM_DESC(reset_crop, "reset cropping parameters at open(), default "
Michael Schimeke5bd0262007-01-18 16:17:39 -0300153 "is 1 (yes) for compatibility with older applications");
Hans Verkuil01df5302013-02-06 12:40:28 -0300154MODULE_PARM_DESC(automute, "mute audio on bad/missing video signal, default is 1 (yes)");
155MODULE_PARM_DESC(chroma_agc, "enables the AGC of chroma signal, default is 0 (no)");
156MODULE_PARM_DESC(agc_crush, "enables the luminance AGC crush, default is 1 (yes)");
157MODULE_PARM_DESC(whitecrush_upper, "sets the white crush upper value, default is 207");
158MODULE_PARM_DESC(whitecrush_lower, "sets the white crush lower value, default is 127");
159MODULE_PARM_DESC(vcr_hack, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
160MODULE_PARM_DESC(irq_iswitch, "switch inputs in irq handler");
161MODULE_PARM_DESC(uv_ratio, "ratio between u and v gains, default is 50");
162MODULE_PARM_DESC(full_luma_range, "use the full luma range, default is 0 (no)");
163MODULE_PARM_DESC(coring, "set the luma coring level, default is 0 (no)");
Jean Delvare176c2f32008-09-07 12:49:59 -0300164MODULE_PARM_DESC(video_nr, "video device numbers");
165MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
166MODULE_PARM_DESC(radio_nr, "radio device numbers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
169MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
170MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -0300171MODULE_VERSION(BTTV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Hans Verkuil01df5302013-02-06 12:40:28 -0300173#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_USER_BTTV_BASE + 0)
174#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_USER_BTTV_BASE + 1)
175#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_USER_BTTV_BASE + 2)
176#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_USER_BTTV_BASE + 3)
177#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_USER_BTTV_BASE + 4)
178#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_USER_BTTV_BASE + 5)
179#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_USER_BTTV_BASE + 6)
180#define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_USER_BTTV_BASE + 7)
181#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_USER_BTTV_BASE + 8)
182#define V4L2_CID_PRIVATE_CORING (V4L2_CID_USER_BTTV_BASE + 9)
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/* ----------------------------------------------------------------------- */
185/* sysfs */
186
Kay Sievers54bd5b62007-10-08 16:26:13 -0300187static ssize_t show_card(struct device *cd,
188 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300190 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -0300191 struct bttv *btv = video_get_drvdata(vfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
193}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300194static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196/* ----------------------------------------------------------------------- */
Jarod Wilsonf992a492007-03-24 15:23:50 -0300197/* dvb auto-load setup */
198#if defined(CONFIG_MODULES) && defined(MODULE)
199static void request_module_async(struct work_struct *work)
200{
201 request_module("dvb-bt8xx");
202}
203
204static void request_modules(struct bttv *dev)
205{
206 INIT_WORK(&dev->request_module_wk, request_module_async);
207 schedule_work(&dev->request_module_wk);
208}
Tejun Heo707bcf32010-12-24 16:14:20 +0100209
210static void flush_request_modules(struct bttv *dev)
211{
Linus Torvalds0b8e74c2012-10-07 17:49:05 +0900212 flush_work(&dev->request_module_wk);
Tejun Heo707bcf32010-12-24 16:14:20 +0100213}
Jarod Wilsonf992a492007-03-24 15:23:50 -0300214#else
215#define request_modules(dev)
Mauro Carvalho Chehaba1fd2872012-10-27 16:33:41 -0300216#define flush_request_modules(dev) do {} while(0)
Jarod Wilsonf992a492007-03-24 15:23:50 -0300217#endif /* CONFIG_MODULES */
218
219
220/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/* static data */
222
223/* special timing tables from conexant... */
224static u8 SRAM_Table[][60] =
225{
226 /* PAL digital input over GPIO[7:0] */
227 {
228 45, // 45 bytes following
229 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
230 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
231 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
232 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
233 0x37,0x00,0xAF,0x21,0x00
234 },
235 /* NTSC digital input over GPIO[7:0] */
236 {
237 51, // 51 bytes following
238 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
239 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
240 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
241 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
242 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
243 0x00,
244 },
245 // TGB_NTSC392 // quartzsight
246 // This table has been modified to be used for Fusion Rev D
247 {
248 0x2A, // size of table = 42
249 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
250 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
251 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
252 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
253 0x20, 0x00
254 }
255};
256
Michael Schimeke5bd0262007-01-18 16:17:39 -0300257/* minhdelayx1 first video pixel we can capture on a line and
258 hdelayx1 start of active video, both relative to rising edge of
259 /HRESET pulse (0H) in 1 / fCLKx1.
260 swidth width of active video and
261 totalwidth total line width, both in 1 / fCLKx1.
262 sqwidth total line width in square pixels.
263 vdelay start of active video in 2 * field lines relative to
264 trailing edge of /VRESET pulse (VDELAY register).
265 sheight height of active video in 2 * field lines.
266 videostart0 ITU-R frame line number of the line corresponding
267 to vdelay in the first field. */
268#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
269 vdelay, sheight, videostart0) \
270 .cropcap.bounds.left = minhdelayx1, \
271 /* * 2 because vertically we count field lines times two, */ \
272 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
273 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
274 /* 4 is a safety margin at the end of the line. */ \
275 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
276 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
277 .cropcap.defrect.left = hdelayx1, \
278 .cropcap.defrect.top = (videostart0) * 2, \
279 .cropcap.defrect.width = swidth, \
280 .cropcap.defrect.height = sheight, \
281 .cropcap.pixelaspect.numerator = totalwidth, \
282 .cropcap.pixelaspect.denominator = sqwidth,
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284const struct bttv_tvnorm bttv_tvnorms[] = {
285 /* PAL-BDGHI */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800286 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
287 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 {
289 .v4l2_id = V4L2_STD_PAL,
290 .name = "PAL",
291 .Fsc = 35468950,
292 .swidth = 924,
293 .sheight = 576,
294 .totalwidth = 1135,
295 .adelay = 0x7f,
296 .bdelay = 0x72,
297 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
298 .scaledtwidth = 1135,
299 .hdelayx1 = 186,
300 .hactivex1 = 924,
301 .vdelay = 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300302 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 .sram = 0,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200304 /* ITU-R frame line number of the first VBI line
Michael Schimeke5bd0262007-01-18 16:17:39 -0300305 we can capture, of the first and second field.
306 The last line is determined by cropcap.bounds. */
307 .vbistart = { 7, 320 },
308 CROPCAP(/* minhdelayx1 */ 68,
309 /* hdelayx1 */ 186,
310 /* Should be (768 * 1135 + 944 / 2) / 944.
311 cropcap.defrect is used for image width
312 checks, so we keep the old value 924. */
313 /* swidth */ 924,
314 /* totalwidth */ 1135,
315 /* sqwidth */ 944,
316 /* vdelay */ 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300317 /* bt878 (and bt848?) can capture another
318 line below active video. */
Mauro Carvalho Chehaba1fd2872012-10-27 16:33:41 -0300319 /* sheight */ (576 + 2) + 0x20 - 2,
320 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 },{
Hans Verkuild97a11e2006-02-07 06:48:40 -0200322 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .name = "NTSC",
324 .Fsc = 28636363,
325 .swidth = 768,
326 .sheight = 480,
327 .totalwidth = 910,
328 .adelay = 0x68,
329 .bdelay = 0x5d,
330 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
331 .scaledtwidth = 910,
332 .hdelayx1 = 128,
333 .hactivex1 = 910,
334 .vdelay = 0x1a,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300335 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 .sram = 1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200337 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300338 CROPCAP(/* minhdelayx1 */ 68,
339 /* hdelayx1 */ 128,
340 /* Should be (640 * 910 + 780 / 2) / 780? */
341 /* swidth */ 768,
342 /* totalwidth */ 910,
343 /* sqwidth */ 780,
344 /* vdelay */ 0x1a,
345 /* sheight */ 480,
346 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 },{
348 .v4l2_id = V4L2_STD_SECAM,
349 .name = "SECAM",
350 .Fsc = 35468950,
351 .swidth = 924,
352 .sheight = 576,
353 .totalwidth = 1135,
354 .adelay = 0x7f,
355 .bdelay = 0xb0,
356 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
357 .scaledtwidth = 1135,
358 .hdelayx1 = 186,
359 .hactivex1 = 922,
360 .vdelay = 0x20,
361 .vbipack = 255,
362 .sram = 0, /* like PAL, correct? */
Michael H. Schimek67f15702006-01-09 15:25:27 -0200363 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300364 CROPCAP(/* minhdelayx1 */ 68,
365 /* hdelayx1 */ 186,
366 /* swidth */ 924,
367 /* totalwidth */ 1135,
368 /* sqwidth */ 944,
369 /* vdelay */ 0x20,
370 /* sheight */ 576,
371 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 },{
373 .v4l2_id = V4L2_STD_PAL_Nc,
374 .name = "PAL-Nc",
375 .Fsc = 28636363,
376 .swidth = 640,
377 .sheight = 576,
378 .totalwidth = 910,
379 .adelay = 0x68,
380 .bdelay = 0x5d,
381 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
382 .scaledtwidth = 780,
383 .hdelayx1 = 130,
384 .hactivex1 = 734,
385 .vdelay = 0x1a,
386 .vbipack = 144,
387 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200388 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300389 CROPCAP(/* minhdelayx1 */ 68,
390 /* hdelayx1 */ 130,
391 /* swidth */ (640 * 910 + 780 / 2) / 780,
392 /* totalwidth */ 910,
393 /* sqwidth */ 780,
394 /* vdelay */ 0x1a,
395 /* sheight */ 576,
396 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 },{
398 .v4l2_id = V4L2_STD_PAL_M,
399 .name = "PAL-M",
400 .Fsc = 28636363,
401 .swidth = 640,
402 .sheight = 480,
403 .totalwidth = 910,
404 .adelay = 0x68,
405 .bdelay = 0x5d,
406 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
407 .scaledtwidth = 780,
408 .hdelayx1 = 135,
409 .hactivex1 = 754,
410 .vdelay = 0x1a,
411 .vbipack = 144,
412 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200413 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300414 CROPCAP(/* minhdelayx1 */ 68,
415 /* hdelayx1 */ 135,
416 /* swidth */ (640 * 910 + 780 / 2) / 780,
417 /* totalwidth */ 910,
418 /* sqwidth */ 780,
419 /* vdelay */ 0x1a,
420 /* sheight */ 480,
421 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 },{
423 .v4l2_id = V4L2_STD_PAL_N,
424 .name = "PAL-N",
425 .Fsc = 35468950,
426 .swidth = 768,
427 .sheight = 576,
428 .totalwidth = 1135,
429 .adelay = 0x7f,
430 .bdelay = 0x72,
431 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
432 .scaledtwidth = 944,
433 .hdelayx1 = 186,
434 .hactivex1 = 922,
435 .vdelay = 0x20,
436 .vbipack = 144,
437 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300438 .vbistart = { 7, 320 },
439 CROPCAP(/* minhdelayx1 */ 68,
440 /* hdelayx1 */ 186,
441 /* swidth */ (768 * 1135 + 944 / 2) / 944,
442 /* totalwidth */ 1135,
443 /* sqwidth */ 944,
444 /* vdelay */ 0x20,
445 /* sheight */ 576,
446 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 },{
448 .v4l2_id = V4L2_STD_NTSC_M_JP,
449 .name = "NTSC-JP",
450 .Fsc = 28636363,
451 .swidth = 640,
452 .sheight = 480,
453 .totalwidth = 910,
454 .adelay = 0x68,
455 .bdelay = 0x5d,
456 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
457 .scaledtwidth = 780,
458 .hdelayx1 = 135,
459 .hactivex1 = 754,
460 .vdelay = 0x16,
461 .vbipack = 144,
462 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300463 .vbistart = { 10, 273 },
464 CROPCAP(/* minhdelayx1 */ 68,
465 /* hdelayx1 */ 135,
466 /* swidth */ (640 * 910 + 780 / 2) / 780,
467 /* totalwidth */ 910,
468 /* sqwidth */ 780,
469 /* vdelay */ 0x16,
470 /* sheight */ 480,
471 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 },{
473 /* that one hopefully works with the strange timing
474 * which video recorders produce when playing a NTSC
475 * tape on a PAL TV ... */
476 .v4l2_id = V4L2_STD_PAL_60,
477 .name = "PAL-60",
478 .Fsc = 35468950,
479 .swidth = 924,
480 .sheight = 480,
481 .totalwidth = 1135,
482 .adelay = 0x7f,
483 .bdelay = 0x72,
484 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
485 .scaledtwidth = 1135,
486 .hdelayx1 = 186,
487 .hactivex1 = 924,
488 .vdelay = 0x1a,
489 .vbipack = 255,
490 .vtotal = 524,
491 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200492 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300493 CROPCAP(/* minhdelayx1 */ 68,
494 /* hdelayx1 */ 186,
495 /* swidth */ 924,
496 /* totalwidth */ 1135,
497 /* sqwidth */ 944,
498 /* vdelay */ 0x1a,
499 /* sheight */ 480,
500 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502};
503static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
504
505/* ----------------------------------------------------------------------- */
506/* bttv format list
507 packed pixel formats must come first */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300508static const struct bttv_format formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 {
510 .name = "8 bpp, gray",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 .fourcc = V4L2_PIX_FMT_GREY,
512 .btformat = BT848_COLOR_FMT_Y8,
513 .depth = 8,
514 .flags = FORMAT_FLAGS_PACKED,
515 },{
516 .name = "8 bpp, dithered color",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 .fourcc = V4L2_PIX_FMT_HI240,
518 .btformat = BT848_COLOR_FMT_RGB8,
519 .depth = 8,
520 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
521 },{
522 .name = "15 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 .fourcc = V4L2_PIX_FMT_RGB555,
524 .btformat = BT848_COLOR_FMT_RGB15,
525 .depth = 16,
526 .flags = FORMAT_FLAGS_PACKED,
527 },{
528 .name = "15 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 .fourcc = V4L2_PIX_FMT_RGB555X,
530 .btformat = BT848_COLOR_FMT_RGB15,
531 .btswap = 0x03, /* byteswap */
532 .depth = 16,
533 .flags = FORMAT_FLAGS_PACKED,
534 },{
535 .name = "16 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 .fourcc = V4L2_PIX_FMT_RGB565,
537 .btformat = BT848_COLOR_FMT_RGB16,
538 .depth = 16,
539 .flags = FORMAT_FLAGS_PACKED,
540 },{
541 .name = "16 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 .fourcc = V4L2_PIX_FMT_RGB565X,
543 .btformat = BT848_COLOR_FMT_RGB16,
544 .btswap = 0x03, /* byteswap */
545 .depth = 16,
546 .flags = FORMAT_FLAGS_PACKED,
547 },{
548 .name = "24 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 .fourcc = V4L2_PIX_FMT_BGR24,
550 .btformat = BT848_COLOR_FMT_RGB24,
551 .depth = 24,
552 .flags = FORMAT_FLAGS_PACKED,
553 },{
554 .name = "32 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 .fourcc = V4L2_PIX_FMT_BGR32,
556 .btformat = BT848_COLOR_FMT_RGB32,
557 .depth = 32,
558 .flags = FORMAT_FLAGS_PACKED,
559 },{
560 .name = "32 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 .fourcc = V4L2_PIX_FMT_RGB32,
562 .btformat = BT848_COLOR_FMT_RGB32,
563 .btswap = 0x0f, /* byte+word swap */
564 .depth = 32,
565 .flags = FORMAT_FLAGS_PACKED,
566 },{
567 .name = "4:2:2, packed, YUYV",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 .fourcc = V4L2_PIX_FMT_YUYV,
569 .btformat = BT848_COLOR_FMT_YUY2,
570 .depth = 16,
571 .flags = FORMAT_FLAGS_PACKED,
572 },{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 .name = "4:2:2, packed, UYVY",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 .fourcc = V4L2_PIX_FMT_UYVY,
575 .btformat = BT848_COLOR_FMT_YUY2,
576 .btswap = 0x03, /* byteswap */
577 .depth = 16,
578 .flags = FORMAT_FLAGS_PACKED,
579 },{
580 .name = "4:2:2, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 .fourcc = V4L2_PIX_FMT_YUV422P,
582 .btformat = BT848_COLOR_FMT_YCrCb422,
583 .depth = 16,
584 .flags = FORMAT_FLAGS_PLANAR,
585 .hshift = 1,
586 .vshift = 0,
587 },{
588 .name = "4:2:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 .fourcc = V4L2_PIX_FMT_YUV420,
590 .btformat = BT848_COLOR_FMT_YCrCb422,
591 .depth = 12,
592 .flags = FORMAT_FLAGS_PLANAR,
593 .hshift = 1,
594 .vshift = 1,
595 },{
596 .name = "4:2:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 .fourcc = V4L2_PIX_FMT_YVU420,
598 .btformat = BT848_COLOR_FMT_YCrCb422,
599 .depth = 12,
600 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
601 .hshift = 1,
602 .vshift = 1,
603 },{
604 .name = "4:1:1, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 .fourcc = V4L2_PIX_FMT_YUV411P,
606 .btformat = BT848_COLOR_FMT_YCrCb411,
607 .depth = 12,
608 .flags = FORMAT_FLAGS_PLANAR,
609 .hshift = 2,
610 .vshift = 0,
611 },{
612 .name = "4:1:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 .fourcc = V4L2_PIX_FMT_YUV410,
614 .btformat = BT848_COLOR_FMT_YCrCb411,
615 .depth = 9,
616 .flags = FORMAT_FLAGS_PLANAR,
617 .hshift = 2,
618 .vshift = 2,
619 },{
620 .name = "4:1:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 .fourcc = V4L2_PIX_FMT_YVU410,
622 .btformat = BT848_COLOR_FMT_YCrCb411,
623 .depth = 9,
624 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
625 .hshift = 2,
626 .vshift = 2,
627 },{
628 .name = "raw scanlines",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 .fourcc = -1,
630 .btformat = BT848_COLOR_FMT_RAW,
631 .depth = 8,
632 .flags = FORMAT_FLAGS_RAW,
633 }
634};
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300635static const unsigned int FORMATS = ARRAY_SIZE(formats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/* resource management */
639
Michael Schimeke5bd0262007-01-18 16:17:39 -0300640/*
641 RESOURCE_ allocated by freed by
642
643 VIDEO_READ bttv_read 1) bttv_read 2)
644
645 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
646 VIDIOC_QBUF 1) bttv_release
647 VIDIOCMCAPTURE 1)
648
649 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
650 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
651 3) bttv_release
652
653 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
654 VIDIOC_QBUF 1) bttv_release
655 bttv_read, bttv_poll 1) 4)
656
657 1) The resource must be allocated when we enter buffer prepare functions
658 and remain allocated while buffers are in the DMA queue.
659 2) This is a single frame read.
660 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
661 RESOURCE_OVERLAY is allocated.
662 4) This is a continuous read, implies VIDIOC_STREAMON.
663
664 Note this driver permits video input and standard changes regardless if
665 resources are allocated.
666*/
667
668#define VBI_RESOURCES (RESOURCE_VBI)
669#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
670 RESOURCE_VIDEO_STREAM | \
671 RESOURCE_OVERLAY)
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300674int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Michael Schimeke5bd0262007-01-18 16:17:39 -0300676 int xbits; /* mutual exclusive resources */
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (fh->resources & bit)
679 /* have it already allocated */
680 return 1;
681
Michael Schimeke5bd0262007-01-18 16:17:39 -0300682 xbits = bit;
683 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
684 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /* is it free? */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300687 if (btv->resources & xbits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* no, someone else uses it */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300689 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300691
692 if ((bit & VIDEO_RESOURCES)
693 && 0 == (btv->resources & VIDEO_RESOURCES)) {
694 /* Do crop - use current, don't - use default parameters. */
695 __s32 top = btv->crop[!!fh->do_crop].rect.top;
696
697 if (btv->vbi_end > top)
698 goto fail;
699
700 /* We cannot capture the same line as video and VBI data.
701 Claim scan lines crop[].rect.top to bottom. */
702 btv->crop_start = top;
703 } else if (bit & VBI_RESOURCES) {
704 __s32 end = fh->vbi_fmt.end;
705
706 if (end > btv->crop_start)
707 goto fail;
708
709 /* Claim scan lines above fh->vbi_fmt.end. */
710 btv->vbi_end = end;
711 }
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* it's free, grab it */
714 fh->resources |= bit;
715 btv->resources |= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 1;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300717
718 fail:
Michael Schimeke5bd0262007-01-18 16:17:39 -0300719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722static
723int check_btres(struct bttv_fh *fh, int bit)
724{
725 return (fh->resources & bit);
726}
727
728static
729int locked_btres(struct bttv *btv, int bit)
730{
731 return (btv->resources & bit);
732}
733
Michael Schimeke5bd0262007-01-18 16:17:39 -0300734/* Call with btv->lock down. */
735static void
736disclaim_vbi_lines(struct bttv *btv)
737{
738 btv->vbi_end = 0;
739}
740
741/* Call with btv->lock down. */
742static void
743disclaim_video_lines(struct bttv *btv)
744{
745 const struct bttv_tvnorm *tvnorm;
746 u8 crop;
747
748 tvnorm = &bttv_tvnorms[btv->tvnorm];
749 btv->crop_start = tvnorm->cropcap.bounds.top
750 + tvnorm->cropcap.bounds.height;
751
752 /* VBI capturing ends at VDELAY, start of video capturing, no
753 matter how many lines the VBI RISC program expects. When video
754 capturing is off, it shall no longer "preempt" VBI capturing,
755 so we set VDELAY to maximum. */
756 crop = btread(BT848_E_CROP) | 0xc0;
757 btwrite(crop, BT848_E_CROP);
758 btwrite(0xfe, BT848_E_VDELAY_LO);
759 btwrite(crop, BT848_O_CROP);
760 btwrite(0xfe, BT848_O_VDELAY_LO);
761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300764void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if ((fh->resources & bits) != bits) {
Joe Perches8af443e2011-08-21 19:56:48 -0300767 /* trying to free resources not allocated by us ... */
768 pr_err("BUG! (btres)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 fh->resources &= ~bits;
771 btv->resources &= ~bits;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300772
773 bits = btv->resources;
774
775 if (0 == (bits & VIDEO_RESOURCES))
776 disclaim_video_lines(btv);
777
778 if (0 == (bits & VBI_RESOURCES))
779 disclaim_vbi_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782/* ----------------------------------------------------------------------- */
783/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
784
785/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
786 PLL_X = Reference pre-divider (0=1, 1=2)
787 PLL_C = Post divider (0=6, 1=4)
788 PLL_I = Integer input
789 PLL_F = Fractional input
790
791 F_input = 28.636363 MHz:
792 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
793*/
794
795static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
796{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800797 unsigned char fl, fh, fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800799 /* prevent overflows */
800 fin/=4;
801 fout/=4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800803 fout*=12;
804 fi=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800806 fout=(fout%fin)*256;
807 fh=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800809 fout=(fout%fin)*256;
810 fl=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800812 btwrite(fl, BT848_PLL_F_LO);
813 btwrite(fh, BT848_PLL_F_HI);
814 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
817static void set_pll(struct bttv *btv)
818{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800819 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800821 if (!btv->pll.pll_crystal)
822 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
Joe Perches8af443e2011-08-21 19:56:48 -0300825 dprintk("%d: PLL: no change required\n", btv->c.nr);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800826 return;
827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800829 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
830 /* no PLL needed */
831 if (btv->pll.pll_current == 0)
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800832 return;
Joe Perches8af443e2011-08-21 19:56:48 -0300833 if (bttv_verbose)
834 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
835 btv->c.nr, btv->pll.pll_ifreq);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800836 btwrite(0x00,BT848_TGCTRL);
837 btwrite(0x00,BT848_PLL_XCI);
838 btv->pll.pll_current = 0;
839 return;
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Joe Perches8af443e2011-08-21 19:56:48 -0300842 if (bttv_verbose)
843 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
844 btv->c.nr,
845 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
847
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800848 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* Let other people run while the PLL stabilizes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 msleep(10);
851
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800852 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 btwrite(0,BT848_DSTATUS);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800854 } else {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800855 btwrite(0x08,BT848_TGCTRL);
856 btv->pll.pll_current = btv->pll.pll_ofreq;
Joe Perches8af443e2011-08-21 19:56:48 -0300857 if (bttv_verbose)
858 pr_info("PLL set ok\n");
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800859 return;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800860 }
861 }
862 btv->pll.pll_current = -1;
Joe Perches8af443e2011-08-21 19:56:48 -0300863 if (bttv_verbose)
864 pr_info("Setting PLL failed\n");
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800865 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868/* used to switch between the bt848's analog/digital video capture modes */
869static void bt848A_set_timing(struct bttv *btv)
870{
871 int i, len;
872 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
873 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
874
Trent Piepho5221e212009-01-28 21:32:59 -0300875 if (btv->input == btv->dig) {
Joe Perches8af443e2011-08-21 19:56:48 -0300876 dprintk("%d: load digital timing table (table_idx=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 btv->c.nr,table_idx);
878
879 /* timing change...reset timing generator address */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800880 btwrite(0x00, BT848_TGCTRL);
881 btwrite(0x02, BT848_TGCTRL);
882 btwrite(0x00, BT848_TGCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 len=SRAM_Table[table_idx][0];
885 for(i = 1; i <= len; i++)
886 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
887 btv->pll.pll_ofreq = 27000000;
888
889 set_pll(btv);
890 btwrite(0x11, BT848_TGCTRL);
891 btwrite(0x41, BT848_DVSIF);
892 } else {
893 btv->pll.pll_ofreq = fsc;
894 set_pll(btv);
895 btwrite(0x0, BT848_DVSIF);
896 }
897}
898
899/* ----------------------------------------------------------------------- */
900
901static void bt848_bright(struct bttv *btv, int bright)
902{
903 int value;
904
Joe Perches8af443e2011-08-21 19:56:48 -0300905 // printk("set bright: %d\n", bright); // DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 btv->bright = bright;
907
908 /* We want -128 to 127 we get 0-65535 */
909 value = (bright >> 8) - 128;
910 btwrite(value & 0xff, BT848_BRIGHT);
911}
912
913static void bt848_hue(struct bttv *btv, int hue)
914{
915 int value;
916
917 btv->hue = hue;
918
919 /* -128 to 127 */
920 value = (hue >> 8) - 128;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800921 btwrite(value & 0xff, BT848_HUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924static void bt848_contrast(struct bttv *btv, int cont)
925{
926 int value,hibit;
927
928 btv->contrast = cont;
929
930 /* 0-511 */
931 value = (cont >> 7);
932 hibit = (value >> 6) & 4;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800933 btwrite(value & 0xff, BT848_CONTRAST_LO);
934 btaor(hibit, ~4, BT848_E_CONTROL);
935 btaor(hibit, ~4, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938static void bt848_sat(struct bttv *btv, int color)
939{
940 int val_u,val_v,hibits;
941
942 btv->saturation = color;
943
944 /* 0-511 for the color */
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700945 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
946 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800947 hibits = (val_u >> 7) & 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 hibits |= (val_v >> 8) & 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800949 btwrite(val_u & 0xff, BT848_SAT_U_LO);
950 btwrite(val_v & 0xff, BT848_SAT_V_LO);
951 btaor(hibits, ~3, BT848_E_CONTROL);
952 btaor(hibits, ~3, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955/* ----------------------------------------------------------------------- */
956
957static int
958video_mux(struct bttv *btv, unsigned int input)
959{
960 int mux,mask2;
961
962 if (input >= bttv_tvcards[btv->c.type].video_inputs)
963 return -EINVAL;
964
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800965 /* needed by RemoteVideo MX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
967 if (mask2)
968 gpio_inout(mask2,mask2);
969
970 if (input == btv->svhs) {
971 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
972 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
973 } else {
974 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
975 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
976 }
Trent Piepho6f987002009-01-28 21:32:59 -0300977 mux = bttv_muxsel(btv, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 btaor(mux<<5, ~(3<<5), BT848_IFORM);
Joe Perches8af443e2011-08-21 19:56:48 -0300979 dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 /* card specific hook */
982 if(bttv_tvcards[btv->c.type].muxsel_hook)
983 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
984 return 0;
985}
986
987static char *audio_modes[] = {
988 "audio: tuner", "audio: radio", "audio: extern",
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -0300989 "audio: intern", "audio: mute"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990};
991
Frank Schaefer5f4567812013-03-21 13:51:17 -0300992static void
993audio_mux_gpio(struct bttv *btv, int input, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Frank Schaefer28cafc32013-03-21 13:51:13 -0300995 int gpio_val, signal, mute_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
998 bttv_tvcards[btv->c.type].gpiomask);
999 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1000
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001001 /* automute */
Frank Schaefer28cafc32013-03-21 13:51:13 -03001002 mute_gpio = mute || (btv->opt_automute && (!signal || !btv->users)
Hans Verkuilb8e2a362013-02-10 09:24:14 -03001003 && !btv->has_radio_tuner);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001004
Frank Schaefer28cafc32013-03-21 13:51:13 -03001005 if (mute_gpio)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001006 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1007 else
1008 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001009
Trent Piepho72134a62009-01-28 21:32:59 -03001010 switch (btv->c.type) {
1011 case BTTV_BOARD_VOODOOTV_FM:
1012 case BTTV_BOARD_VOODOOTV_200:
1013 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1014 break;
1015
1016 default:
1017 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1018 }
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (bttv_gpio)
Frank Schaefer28cafc32013-03-21 13:51:13 -03001021 bttv_gpio_tracking(btv, audio_modes[mute_gpio ? 4 : input]);
Frank Schaefer5f4567812013-03-21 13:51:17 -03001022}
1023
1024static int
Frank Schaeferb1a30152013-03-21 13:51:18 -03001025audio_mute(struct bttv *btv, int mute)
Frank Schaefer5f4567812013-03-21 13:51:17 -03001026{
1027 struct v4l2_ctrl *ctrl;
1028
Frank Schaeferb1a30152013-03-21 13:51:18 -03001029 audio_mux_gpio(btv, btv->audio_input, mute);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001030
Hans Verkuil859f0272009-03-28 08:29:00 -03001031 if (btv->sd_msp34xx) {
Hans Verkuil01df5302013-02-06 12:40:28 -03001032 ctrl = v4l2_ctrl_find(btv->sd_msp34xx->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1033 if (ctrl)
Frank Schaefer598728e2013-03-21 13:51:14 -03001034 v4l2_ctrl_s_ctrl(ctrl, mute);
Frank Schaeferb1a30152013-03-21 13:51:18 -03001035 }
1036 if (btv->sd_tvaudio) {
1037 ctrl = v4l2_ctrl_find(btv->sd_tvaudio->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1038 if (ctrl)
1039 v4l2_ctrl_s_ctrl(ctrl, mute);
1040 }
1041 if (btv->sd_tda7432) {
1042 ctrl = v4l2_ctrl_find(btv->sd_tda7432->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1043 if (ctrl)
1044 v4l2_ctrl_s_ctrl(ctrl, mute);
1045 }
1046 return 0;
1047}
1048
1049static int
1050audio_input(struct bttv *btv, int input)
1051{
1052 audio_mux_gpio(btv, input, btv->mute);
1053
1054 if (btv->sd_msp34xx) {
1055 u32 in;
Hans Verkuil01df5302013-02-06 12:40:28 -03001056
Hans Verkuil2474ed42006-03-19 12:35:57 -03001057 /* Note: the inputs tuner/radio/extern/intern are translated
1058 to msp routings. This assumes common behavior for all msp3400
1059 based TV cards. When this assumption fails, then the
1060 specific MSP routing must be added to the card table.
1061 For now this is sufficient. */
1062 switch (input) {
1063 case TVAUDIO_INPUT_RADIO:
Hans de Goede7025e522012-05-21 07:46:22 -03001064 /* Some boards need the msp do to the radio demod */
1065 if (btv->radio_uses_msp_demodulator) {
1066 in = MSP_INPUT_DEFAULT;
1067 break;
1068 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001069 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001070 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001071 break;
1072 case TVAUDIO_INPUT_EXTERN:
Hans Verkuil5325b422009-04-02 11:26:22 -03001073 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001074 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001075 break;
1076 case TVAUDIO_INPUT_INTERN:
1077 /* Yes, this is the same input as for RADIO. I doubt
1078 if this is ever used. The only board with an INTERN
1079 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1080 that was tested. My guess is that the whole INTERN
1081 input does not work. */
Hans Verkuil5325b422009-04-02 11:26:22 -03001082 in = MSP_INPUT(MSP_IN_SCART2, 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_TUNER:
1086 default:
Wade Berrier434b2522007-06-25 13:02:16 -03001087 /* This is the only card that uses TUNER2, and afaik,
1088 is the only difference between the VOODOOTV_FM
1089 and VOODOOTV_200 */
1090 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
Hans Verkuil5325b422009-04-02 11:26:22 -03001091 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
Wade Berrier434b2522007-06-25 13:02:16 -03001092 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1093 else
Hans Verkuil5325b422009-04-02 11:26:22 -03001094 in = MSP_INPUT_DEFAULT;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001095 break;
1096 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001097 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1098 in, MSP_OUTPUT_DEFAULT, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001099 }
Hans Verkuil859f0272009-03-28 08:29:00 -03001100 if (btv->sd_tvaudio) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001101 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
Frank Schaeferb1a30152013-03-21 13:51:18 -03001102 input, 0, 0);
Hans Verkuil01df5302013-02-06 12:40:28 -03001103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return 0;
1105}
1106
1107static void
Michael Schimeke5bd0262007-01-18 16:17:39 -03001108bttv_crop_calc_limits(struct bttv_crop *c)
1109{
1110 /* Scale factor min. 1:1, max. 16:1. Min. image size
1111 48 x 32. Scaled width must be a multiple of 4. */
1112
1113 if (1) {
1114 /* For bug compatibility with VIDIOCGCAP and image
1115 size checks in earlier driver versions. */
1116 c->min_scaled_width = 48;
1117 c->min_scaled_height = 32;
1118 } else {
1119 c->min_scaled_width =
1120 (max(48, c->rect.width >> 4) + 3) & ~3;
1121 c->min_scaled_height =
1122 max(32, c->rect.height >> 4);
1123 }
1124
1125 c->max_scaled_width = c->rect.width & ~3;
1126 c->max_scaled_height = c->rect.height;
1127}
1128
1129static void
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001130bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
Michael Schimeke5bd0262007-01-18 16:17:39 -03001131{
1132 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1133 bttv_crop_calc_limits(c);
1134}
1135
1136/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137static int
1138set_tvnorm(struct bttv *btv, unsigned int norm)
1139{
1140 const struct bttv_tvnorm *tvnorm;
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001141 v4l2_std_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001143 BUG_ON(norm >= BTTV_TVNORMS);
1144 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 tvnorm = &bttv_tvnorms[norm];
1147
Mike Isely2de26c02009-09-21 12:42:22 -03001148 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001149 sizeof (tvnorm->cropcap))) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001150 bttv_crop_reset(&btv->crop[0], norm);
1151 btv->crop[1] = btv->crop[0]; /* current = default */
1152
1153 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1154 btv->crop_start = tvnorm->cropcap.bounds.top
1155 + tvnorm->cropcap.bounds.height;
1156 }
1157 }
1158
1159 btv->tvnorm = norm;
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 btwrite(tvnorm->adelay, BT848_ADELAY);
1162 btwrite(tvnorm->bdelay, BT848_BDELAY);
1163 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1164 BT848_IFORM);
1165 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1166 btwrite(1, BT848_VBI_PACK_DEL);
1167 bt848A_set_timing(btv);
1168
1169 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -08001170 case BTTV_BOARD_VOODOOTV_FM:
Wade Berrier434b2522007-06-25 13:02:16 -03001171 case BTTV_BOARD_VOODOOTV_200:
Trent Piepho72134a62009-01-28 21:32:59 -03001172 bttv_tda9880_setnorm(btv, gpio_read());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001175 id = tvnorm->v4l2_id;
Hans Verkuilf41737e2009-04-01 03:52:39 -03001176 bttv_call_all(btv, core, s_std, id);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return 0;
1179}
1180
Michael Schimeke5bd0262007-01-18 16:17:39 -03001181/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182static void
Trent Piepho333408f2007-07-03 15:08:10 -03001183set_input(struct bttv *btv, unsigned int input, unsigned int norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 unsigned long flags;
1186
1187 btv->input = input;
1188 if (irq_iswitch) {
1189 spin_lock_irqsave(&btv->s_lock,flags);
1190 if (btv->curr.frame_irq) {
1191 /* active capture -> delayed input switch */
1192 btv->new_input = input;
1193 } else {
1194 video_mux(btv,input);
1195 }
1196 spin_unlock_irqrestore(&btv->s_lock,flags);
1197 } else {
1198 video_mux(btv,input);
1199 }
Frank Schaefer2166f0a2013-03-21 13:51:16 -03001200 btv->audio_input = (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1201 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN;
1202 audio_input(btv, btv->audio_input);
Trent Piepho333408f2007-07-03 15:08:10 -03001203 set_tvnorm(btv, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
1206static void init_irqreg(struct bttv *btv)
1207{
1208 /* clear status */
1209 btwrite(0xfffffUL, BT848_INT_STAT);
1210
1211 if (bttv_tvcards[btv->c.type].no_video) {
1212 /* i2c only */
1213 btwrite(BT848_INT_I2CDONE,
1214 BT848_INT_MASK);
1215 } else {
1216 /* full video */
1217 btwrite((btv->triton1) |
1218 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1219 BT848_INT_SCERR |
1220 (fdsr ? BT848_INT_FDSR : 0) |
Jean Delvareeb1b27b2008-08-30 10:18:21 -03001221 BT848_INT_RISCI | BT848_INT_OCERR |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1223 BT848_INT_I2CDONE,
1224 BT848_INT_MASK);
1225 }
1226}
1227
1228static void init_bt848(struct bttv *btv)
1229{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (bttv_tvcards[btv->c.type].no_video) {
1231 /* very basic init only */
1232 init_irqreg(btv);
1233 return;
1234 }
1235
1236 btwrite(0x00, BT848_CAP_CTL);
1237 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1238 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1239
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001240 /* set planar and packed mode trigger points and */
1241 /* set rising edge of inverted GPINTR pin as irq trigger */
1242 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1243 BT848_GPIO_DMA_CTL_PLTP1_16|
1244 BT848_GPIO_DMA_CTL_PLTP23_16|
1245 BT848_GPIO_DMA_CTL_GPINTC|
1246 BT848_GPIO_DMA_CTL_GPINTI,
1247 BT848_GPIO_DMA_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001249 btwrite(0x20, BT848_E_VSCALE_HI);
1250 btwrite(0x20, BT848_O_VSCALE_HI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Hans Verkuil01df5302013-02-06 12:40:28 -03001252 v4l2_ctrl_handler_setup(&btv->ctrl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001254 /* interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 init_irqreg(btv);
1256}
1257
1258static void bttv_reinit_bt848(struct bttv *btv)
1259{
1260 unsigned long flags;
1261
1262 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03001263 pr_info("%d: reset, reinitialize\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 spin_lock_irqsave(&btv->s_lock,flags);
1265 btv->errors=0;
1266 bttv_set_dma(btv,0);
1267 spin_unlock_irqrestore(&btv->s_lock,flags);
1268
1269 init_bt848(btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001270 btv->pll.pll_current = -1;
Trent Piepho333408f2007-07-03 15:08:10 -03001271 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Hans Verkuil01df5302013-02-06 12:40:28 -03001274static int bttv_s_ctrl(struct v4l2_ctrl *c)
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001275{
Hans Verkuil01df5302013-02-06 12:40:28 -03001276 struct bttv *btv = container_of(c->handler, struct bttv, ctrl_handler);
1277 int val;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001278
1279 switch (c->id) {
1280 case V4L2_CID_BRIGHTNESS:
Hans Verkuil01df5302013-02-06 12:40:28 -03001281 bt848_bright(btv, c->val);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001282 break;
1283 case V4L2_CID_HUE:
Hans Verkuil01df5302013-02-06 12:40:28 -03001284 bt848_hue(btv, c->val);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001285 break;
1286 case V4L2_CID_CONTRAST:
Hans Verkuil01df5302013-02-06 12:40:28 -03001287 bt848_contrast(btv, c->val);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001288 break;
1289 case V4L2_CID_SATURATION:
Hans Verkuil01df5302013-02-06 12:40:28 -03001290 bt848_sat(btv, c->val);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001291 break;
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -03001292 case V4L2_CID_COLOR_KILLER:
Hans Verkuil01df5302013-02-06 12:40:28 -03001293 if (c->val) {
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -03001294 btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1295 btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1296 } else {
1297 btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1298 btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1299 }
1300 break;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001301 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil01df5302013-02-06 12:40:28 -03001302 audio_mute(btv, c->val);
Frank Schaefer598728e2013-03-21 13:51:14 -03001303 btv->mute = c->val;
Hans Verkuil01df5302013-02-06 12:40:28 -03001304 break;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001305 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuil01df5302013-02-06 12:40:28 -03001306 btv->volume_gpio(btv, c->val);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001307 break;
1308
Hans Verkuil01df5302013-02-06 12:40:28 -03001309 case V4L2_CID_CHROMA_AGC:
1310 val = c->val ? BT848_SCLOOP_CAGC : 0;
1311 btwrite(val, BT848_E_SCLOOP);
1312 btwrite(val, BT848_O_SCLOOP);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001313 break;
1314 case V4L2_CID_PRIVATE_COMBFILTER:
Hans Verkuil01df5302013-02-06 12:40:28 -03001315 btv->opt_combfilter = c->val;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001316 break;
1317 case V4L2_CID_PRIVATE_LUMAFILTER:
Hans Verkuil01df5302013-02-06 12:40:28 -03001318 if (c->val) {
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001319 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1320 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1321 } else {
1322 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1323 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1324 }
1325 break;
1326 case V4L2_CID_PRIVATE_AUTOMUTE:
Hans Verkuil01df5302013-02-06 12:40:28 -03001327 btv->opt_automute = c->val;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001328 break;
1329 case V4L2_CID_PRIVATE_AGC_CRUSH:
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001330 btwrite(BT848_ADC_RESERVED |
Hans Verkuil01df5302013-02-06 12:40:28 -03001331 (c->val ? BT848_ADC_CRUSH : 0),
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001332 BT848_ADC);
1333 break;
1334 case V4L2_CID_PRIVATE_VCR_HACK:
Hans Verkuil01df5302013-02-06 12:40:28 -03001335 btv->opt_vcr_hack = c->val;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001336 break;
1337 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
Hans Verkuil01df5302013-02-06 12:40:28 -03001338 btwrite(c->val, BT848_WC_UP);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001339 break;
1340 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
Hans Verkuil01df5302013-02-06 12:40:28 -03001341 btwrite(c->val, BT848_WC_DOWN);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001342 break;
1343 case V4L2_CID_PRIVATE_UV_RATIO:
Hans Verkuil01df5302013-02-06 12:40:28 -03001344 btv->opt_uv_ratio = c->val;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001345 bt848_sat(btv, btv->saturation);
1346 break;
1347 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
Hans Verkuil01df5302013-02-06 12:40:28 -03001348 btaor((c->val << 7), ~BT848_OFORM_RANGE, BT848_OFORM);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001349 break;
1350 case V4L2_CID_PRIVATE_CORING:
Hans Verkuil01df5302013-02-06 12:40:28 -03001351 btaor((c->val << 5), ~BT848_OFORM_CORE32, BT848_OFORM);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001352 break;
1353 default:
1354 return -EINVAL;
1355 }
1356 return 0;
1357}
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359/* ----------------------------------------------------------------------- */
1360
Hans Verkuil01df5302013-02-06 12:40:28 -03001361static const struct v4l2_ctrl_ops bttv_ctrl_ops = {
1362 .s_ctrl = bttv_s_ctrl,
1363};
1364
1365static struct v4l2_ctrl_config bttv_ctrl_combfilter = {
1366 .ops = &bttv_ctrl_ops,
1367 .id = V4L2_CID_PRIVATE_COMBFILTER,
1368 .name = "Comb Filter",
1369 .type = V4L2_CTRL_TYPE_BOOLEAN,
1370 .min = 0,
1371 .max = 1,
1372 .step = 1,
1373 .def = 1,
1374};
1375
1376static struct v4l2_ctrl_config bttv_ctrl_automute = {
1377 .ops = &bttv_ctrl_ops,
1378 .id = V4L2_CID_PRIVATE_AUTOMUTE,
1379 .name = "Auto Mute",
1380 .type = V4L2_CTRL_TYPE_BOOLEAN,
1381 .min = 0,
1382 .max = 1,
1383 .step = 1,
1384 .def = 1,
1385};
1386
1387static struct v4l2_ctrl_config bttv_ctrl_lumafilter = {
1388 .ops = &bttv_ctrl_ops,
1389 .id = V4L2_CID_PRIVATE_LUMAFILTER,
1390 .name = "Luma Decimation Filter",
1391 .type = V4L2_CTRL_TYPE_BOOLEAN,
1392 .min = 0,
1393 .max = 1,
1394 .step = 1,
1395 .def = 1,
1396};
1397
1398static struct v4l2_ctrl_config bttv_ctrl_agc_crush = {
1399 .ops = &bttv_ctrl_ops,
1400 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
1401 .name = "AGC Crush",
1402 .type = V4L2_CTRL_TYPE_BOOLEAN,
1403 .min = 0,
1404 .max = 1,
1405 .step = 1,
1406 .def = 1,
1407};
1408
1409static struct v4l2_ctrl_config bttv_ctrl_vcr_hack = {
1410 .ops = &bttv_ctrl_ops,
1411 .id = V4L2_CID_PRIVATE_VCR_HACK,
1412 .name = "VCR Hack",
1413 .type = V4L2_CTRL_TYPE_BOOLEAN,
1414 .min = 0,
1415 .max = 1,
1416 .step = 1,
1417 .def = 1,
1418};
1419
1420static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower = {
1421 .ops = &bttv_ctrl_ops,
1422 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
1423 .name = "Whitecrush Lower",
1424 .type = V4L2_CTRL_TYPE_INTEGER,
1425 .min = 0,
1426 .max = 255,
1427 .step = 1,
1428 .def = 0x7f,
1429};
1430
1431static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper = {
1432 .ops = &bttv_ctrl_ops,
1433 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
1434 .name = "Whitecrush Upper",
1435 .type = V4L2_CTRL_TYPE_INTEGER,
1436 .min = 0,
1437 .max = 255,
1438 .step = 1,
1439 .def = 0xcf,
1440};
1441
1442static struct v4l2_ctrl_config bttv_ctrl_uv_ratio = {
1443 .ops = &bttv_ctrl_ops,
1444 .id = V4L2_CID_PRIVATE_UV_RATIO,
1445 .name = "UV Ratio",
1446 .type = V4L2_CTRL_TYPE_INTEGER,
1447 .min = 0,
1448 .max = 100,
1449 .step = 1,
1450 .def = 50,
1451};
1452
1453static struct v4l2_ctrl_config bttv_ctrl_full_luma = {
1454 .ops = &bttv_ctrl_ops,
1455 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
1456 .name = "Full Luma Range",
1457 .type = V4L2_CTRL_TYPE_BOOLEAN,
1458 .min = 0,
1459 .max = 1,
1460 .step = 1,
1461};
1462
1463static struct v4l2_ctrl_config bttv_ctrl_coring = {
1464 .ops = &bttv_ctrl_ops,
1465 .id = V4L2_CID_PRIVATE_CORING,
1466 .name = "Coring",
1467 .type = V4L2_CTRL_TYPE_INTEGER,
1468 .min = 0,
1469 .max = 3,
1470 .step = 1,
1471};
1472
1473
1474/* ----------------------------------------------------------------------- */
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476void bttv_gpio_tracking(struct bttv *btv, char *comment)
1477{
1478 unsigned int outbits, data;
1479 outbits = btread(BT848_GPIO_OUT_EN);
1480 data = btread(BT848_GPIO_DATA);
Joe Perches8af443e2011-08-21 19:56:48 -03001481 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1482 btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
1485static void bttv_field_count(struct bttv *btv)
1486{
1487 int need_count = 0;
1488
1489 if (btv->users)
1490 need_count++;
1491
1492 if (need_count) {
1493 /* start field counter */
1494 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1495 } else {
1496 /* stop field counter */
1497 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1498 btv->field_count = 0;
1499 }
1500}
1501
1502static const struct bttv_format*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503format_by_fourcc(int fourcc)
1504{
1505 unsigned int i;
1506
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001507 for (i = 0; i < FORMATS; i++) {
1508 if (-1 == formats[i].fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 continue;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001510 if (formats[i].fourcc == fourcc)
1511 return formats+i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513 return NULL;
1514}
1515
1516/* ----------------------------------------------------------------------- */
1517/* misc helpers */
1518
1519static int
1520bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1521 struct bttv_buffer *new)
1522{
1523 struct bttv_buffer *old;
1524 unsigned long flags;
1525 int retval = 0;
1526
Joe Perches8af443e2011-08-21 19:56:48 -03001527 dprintk("switch_overlay: enter [new=%p]\n", new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if (new)
Brandon Philips0fc06862007-11-06 20:02:36 -03001529 new->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 spin_lock_irqsave(&btv->s_lock,flags);
1531 old = btv->screen;
1532 btv->screen = new;
1533 btv->loop_irq |= 1;
1534 bttv_set_dma(btv, 0x03);
1535 spin_unlock_irqrestore(&btv->s_lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (NULL != old) {
Joe Perches8af443e2011-08-21 19:56:48 -03001537 dprintk("switch_overlay: old=%p state is %d\n",
1538 old, old->vb.state);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001539 bttv_dma_free(&fh->cap,btv, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 kfree(old);
1541 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03001542 if (NULL == new)
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03001543 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 dprintk("switch_overlay: done\n");
1545 return retval;
1546}
1547
1548/* ----------------------------------------------------------------------- */
1549/* video4linux (1) interface */
1550
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001551static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1552 struct bttv_buffer *buf,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001553 const struct bttv_format *fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 unsigned int width, unsigned int height,
1555 enum v4l2_field field)
1556{
Michael Schimeke5bd0262007-01-18 16:17:39 -03001557 struct bttv_fh *fh = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int redo_dma_risc = 0;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001559 struct bttv_crop c;
1560 int norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int rc;
1562
1563 /* check settings */
1564 if (NULL == fmt)
1565 return -EINVAL;
1566 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1567 width = RAW_BPL;
1568 height = RAW_LINES*2;
1569 if (width*height > buf->vb.bsize)
1570 return -EINVAL;
1571 buf->vb.size = buf->vb.bsize;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001572
1573 /* Make sure tvnorm and vbi_end remain consistent
1574 until we're done. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03001575
1576 norm = btv->tvnorm;
1577
1578 /* In this mode capturing always starts at defrect.top
1579 (default VDELAY), ignoring cropping parameters. */
1580 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001582 }
1583
Michael Schimeke5bd0262007-01-18 16:17:39 -03001584 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1585 } else {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001586 norm = btv->tvnorm;
1587 c = btv->crop[!!fh->do_crop];
1588
Michael Schimeke5bd0262007-01-18 16:17:39 -03001589 if (width < c.min_scaled_width ||
1590 width > c.max_scaled_width ||
1591 height < c.min_scaled_height)
1592 return -EINVAL;
1593
1594 switch (field) {
1595 case V4L2_FIELD_TOP:
1596 case V4L2_FIELD_BOTTOM:
1597 case V4L2_FIELD_ALTERNATE:
1598 /* btv->crop counts frame lines. Max. scale
1599 factor is 16:1 for frames, 8:1 for fields. */
1600 if (height * 2 > c.max_scaled_height)
1601 return -EINVAL;
1602 break;
1603
1604 default:
1605 if (height > c.max_scaled_height)
1606 return -EINVAL;
1607 break;
1608 }
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 buf->vb.size = (width * height * fmt->depth) >> 3;
1611 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1612 return -EINVAL;
1613 }
1614
1615 /* alloc + fill struct bttv_buffer (if changed) */
1616 if (buf->vb.width != width || buf->vb.height != height ||
1617 buf->vb.field != field ||
Michael Schimeke5bd0262007-01-18 16:17:39 -03001618 buf->tvnorm != norm || buf->fmt != fmt ||
1619 buf->crop.top != c.rect.top ||
1620 buf->crop.left != c.rect.left ||
1621 buf->crop.width != c.rect.width ||
1622 buf->crop.height != c.rect.height) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 buf->vb.width = width;
1624 buf->vb.height = height;
1625 buf->vb.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001626 buf->tvnorm = norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 buf->fmt = fmt;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001628 buf->crop = c.rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 redo_dma_risc = 1;
1630 }
1631
1632 /* alloc risc memory */
Brandon Philips0fc06862007-11-06 20:02:36 -03001633 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 redo_dma_risc = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001635 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 goto fail;
1637 }
1638
1639 if (redo_dma_risc)
1640 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1641 goto fail;
1642
Brandon Philips0fc06862007-11-06 20:02:36 -03001643 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return 0;
1645
1646 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001647 bttv_dma_free(q,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return rc;
1649}
1650
1651static int
1652buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1653{
1654 struct bttv_fh *fh = q->priv_data;
1655
1656 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1657 if (0 == *count)
1658 *count = gbuffers;
Andreas Bombedab7e312010-03-21 16:02:45 -03001659 if (*size * *count > gbuffers * gbufsize)
1660 *count = (gbuffers * gbufsize) / *size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return 0;
1662}
1663
1664static int
1665buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1666 enum v4l2_field field)
1667{
1668 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1669 struct bttv_fh *fh = q->priv_data;
1670
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001671 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 fh->width, fh->height, field);
1673}
1674
1675static void
1676buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1677{
1678 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1679 struct bttv_fh *fh = q->priv_data;
1680 struct bttv *btv = fh->btv;
1681
Brandon Philips0fc06862007-11-06 20:02:36 -03001682 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 list_add_tail(&buf->vb.queue,&btv->capture);
1684 if (!btv->curr.frame_irq) {
1685 btv->loop_irq |= 1;
1686 bttv_set_dma(btv, 0x03);
1687 }
1688}
1689
1690static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1691{
1692 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1693 struct bttv_fh *fh = q->priv_data;
1694
Michael Schimekfeaba7a2007-01-26 08:30:05 -03001695 bttv_dma_free(q,fh->btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
1698static struct videobuf_queue_ops bttv_video_qops = {
1699 .buf_setup = buffer_setup,
1700 .buf_prepare = buffer_prepare,
1701 .buf_queue = buffer_queue,
1702 .buf_release = buffer_release,
1703};
1704
Hans Verkuilb8e2a362013-02-10 09:24:14 -03001705static void radio_enable(struct bttv *btv)
1706{
1707 /* Switch to the radio tuner */
1708 if (!btv->has_radio_tuner) {
1709 btv->has_radio_tuner = 1;
1710 bttv_call_all(btv, tuner, s_radio);
Frank Schaefer2166f0a2013-03-21 13:51:16 -03001711 btv->audio_input = TVAUDIO_INPUT_RADIO;
1712 audio_input(btv, btv->audio_input);
Hans Verkuilb8e2a362013-02-10 09:24:14 -03001713 }
1714}
1715
Hans Verkuil314527a2013-03-15 06:10:40 -03001716static int bttv_s_std(struct file *file, void *priv, v4l2_std_id id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001718 struct bttv_fh *fh = priv;
1719 struct bttv *btv = fh->btv;
1720 unsigned int i;
Hans Verkuil8c14cc12013-02-06 12:42:40 -03001721 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001723 for (i = 0; i < BTTV_TVNORMS; i++)
Hans Verkuil314527a2013-03-15 06:10:40 -03001724 if (id & bttv_tvnorms[i].v4l2_id)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001725 break;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001726 if (i == BTTV_TVNORMS) {
1727 err = -EINVAL;
1728 goto err;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Hans Verkuil314527a2013-03-15 06:10:40 -03001731 btv->std = id;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001732 set_tvnorm(btv, i);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001733
1734err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001736 return err;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001737}
1738
Hans Verkuil6795cc52013-02-06 12:43:07 -03001739static int bttv_g_std(struct file *file, void *priv, v4l2_std_id *id)
1740{
1741 struct bttv_fh *fh = priv;
1742 struct bttv *btv = fh->btv;
1743
1744 *id = btv->std;
1745 return 0;
1746}
1747
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001748static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001749{
1750 struct bttv_fh *fh = f;
1751 struct bttv *btv = fh->btv;
1752
1753 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1754 *id = V4L2_STD_625_50;
1755 else
1756 *id = V4L2_STD_525_60;
1757 return 0;
1758}
1759
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001760static int bttv_enum_input(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001761 struct v4l2_input *i)
1762{
1763 struct bttv_fh *fh = priv;
1764 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001765 int rc = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001766
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001767 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) {
1768 rc = -EINVAL;
1769 goto err;
1770 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001771
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001772 i->type = V4L2_INPUT_TYPE_CAMERA;
Hans Verkuilf74f89cb2013-01-31 08:45:13 -03001773 i->audioset = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001774
Trent Piephoabb03622009-01-28 21:32:59 -03001775 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001776 sprintf(i->name, "Television");
1777 i->type = V4L2_INPUT_TYPE_TUNER;
1778 i->tuner = 0;
1779 } else if (i->index == btv->svhs) {
1780 sprintf(i->name, "S-Video");
1781 } else {
1782 sprintf(i->name, "Composite%d", i->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001785 if (i->index == btv->input) {
1786 __u32 dstatus = btread(BT848_DSTATUS);
1787 if (0 == (dstatus & BT848_DSTATUS_PRES))
1788 i->status |= V4L2_IN_ST_NO_SIGNAL;
1789 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1790 i->status |= V4L2_IN_ST_NO_H_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001793 i->std = BTTV_NORMS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001795err:
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001796
1797 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001798}
Nickolay V. Shmyrev4b9b9362006-08-25 16:53:04 -03001799
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001800static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001801{
1802 struct bttv_fh *fh = priv;
1803 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001805 *i = btv->input;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001806
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001807 return 0;
1808}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001810static int bttv_s_input(struct file *file, void *priv, unsigned int i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001811{
1812 struct bttv_fh *fh = priv;
1813 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001814
Hans Verkuilf74f89cb2013-01-31 08:45:13 -03001815 if (i >= bttv_tvcards[btv->c.type].video_inputs)
1816 return -EINVAL;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001817
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001818 set_input(btv, i, btv->tvnorm);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001819 return 0;
1820}
1821
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001822static int bttv_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001823 const struct v4l2_tuner *t)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001824{
1825 struct bttv_fh *fh = priv;
1826 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001827
Hans Verkuild9b67072013-02-06 11:43:07 -03001828 if (t->index)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001829 return -EINVAL;
1830
Hans Verkuil859f0272009-03-28 08:29:00 -03001831 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001832
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001833 if (btv->audio_mode_gpio) {
1834 struct v4l2_tuner copy = *t;
1835
1836 btv->audio_mode_gpio(btv, &copy, 1);
1837 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001838 return 0;
1839}
1840
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001841static int bttv_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001842 struct v4l2_frequency *f)
1843{
1844 struct bttv_fh *fh = priv;
1845 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001846
Hans Verkuild9b67072013-02-06 11:43:07 -03001847 if (f->tuner)
1848 return -EINVAL;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001849
Hans Verkuilb8e2a362013-02-10 09:24:14 -03001850 if (f->type == V4L2_TUNER_RADIO)
1851 radio_enable(btv);
Hans Verkuil76ea9922013-02-06 11:49:14 -03001852 f->frequency = f->type == V4L2_TUNER_RADIO ?
1853 btv->radio_freq : btv->tv_freq;
1854
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001855 return 0;
1856}
1857
Hans Verkuilb530a442013-03-19 04:09:26 -03001858static void bttv_set_frequency(struct bttv *btv, const struct v4l2_frequency *f)
Hans Verkuil76ea9922013-02-06 11:49:14 -03001859{
Hans Verkuilb530a442013-03-19 04:09:26 -03001860 struct v4l2_frequency new_freq = *f;
1861
Hans Verkuil76ea9922013-02-06 11:49:14 -03001862 bttv_call_all(btv, tuner, s_frequency, f);
1863 /* s_frequency may clamp the frequency, so get the actual
1864 frequency before assigning radio/tv_freq. */
Hans Verkuilb530a442013-03-19 04:09:26 -03001865 bttv_call_all(btv, tuner, g_frequency, &new_freq);
1866 if (new_freq.type == V4L2_TUNER_RADIO) {
Hans Verkuilb8e2a362013-02-10 09:24:14 -03001867 radio_enable(btv);
Hans Verkuilb530a442013-03-19 04:09:26 -03001868 btv->radio_freq = new_freq.frequency;
Hans Verkuil76ea9922013-02-06 11:49:14 -03001869 if (btv->has_matchbox)
1870 tea5757_set_freq(btv, btv->radio_freq);
1871 } else {
Hans Verkuilb530a442013-03-19 04:09:26 -03001872 btv->tv_freq = new_freq.frequency;
Hans Verkuil76ea9922013-02-06 11:49:14 -03001873 }
1874}
1875
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001876static int bttv_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001877 const struct v4l2_frequency *f)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001878{
1879 struct bttv_fh *fh = priv;
1880 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001881
Hans Verkuild9b67072013-02-06 11:43:07 -03001882 if (f->tuner)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001883 return -EINVAL;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001884
Hans Verkuil76ea9922013-02-06 11:49:14 -03001885 bttv_set_frequency(btv, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001886 return 0;
1887}
1888
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001889static int bttv_log_status(struct file *file, void *f)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001890{
Hans Verkuil01df5302013-02-06 12:40:28 -03001891 struct video_device *vdev = video_devdata(file);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001892 struct bttv_fh *fh = f;
1893 struct bttv *btv = fh->btv;
1894
Hans Verkuil01df5302013-02-06 12:40:28 -03001895 v4l2_ctrl_handler_log_status(vdev->ctrl_handler, btv->c.v4l2_dev.name);
Hans Verkuil859f0272009-03-28 08:29:00 -03001896 bttv_call_all(btv, core, log_status);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001897 return 0;
1898}
1899
Hans Verkuil1b9e94d2012-09-09 09:23:31 -03001900static int bttv_g_chip_ident(struct file *file, void *f, struct v4l2_dbg_chip_ident *chip)
1901{
1902 struct bttv_fh *fh = f;
1903 struct bttv *btv = fh->btv;
1904
1905 chip->ident = V4L2_IDENT_NONE;
1906 chip->revision = 0;
1907 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
1908 if (v4l2_chip_match_host(&chip->match)) {
1909 chip->ident = btv->id;
1910 if (chip->ident == PCI_DEVICE_ID_FUSION879)
1911 chip->ident = V4L2_IDENT_BT879;
1912 }
1913 return 0;
1914 }
1915 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1916 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1917 return -EINVAL;
1918 /* TODO: is this correct? */
1919 return bttv_call_all_err(btv, core, g_chip_ident, chip);
1920}
1921
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001922#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001923static int bttv_g_register(struct file *file, void *f,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001924 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001925{
1926 struct bttv_fh *fh = f;
1927 struct bttv *btv = fh->btv;
1928
1929 if (!capable(CAP_SYS_ADMIN))
1930 return -EPERM;
1931
Hans Verkuil1b9e94d2012-09-09 09:23:31 -03001932 if (!v4l2_chip_match_host(&reg->match)) {
1933 /* TODO: subdev errors should not be ignored, this should become a
1934 subdev helper function. */
1935 bttv_call_all(btv, core, g_register, reg);
1936 return 0;
1937 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001938
1939 /* bt848 has a 12-bit register space */
1940 reg->reg &= 0xfff;
1941 reg->val = btread(reg->reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001942 reg->size = 1;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001943
1944 return 0;
1945}
1946
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001947static int bttv_s_register(struct file *file, void *f,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001948 const struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001949{
1950 struct bttv_fh *fh = f;
1951 struct bttv *btv = fh->btv;
1952
1953 if (!capable(CAP_SYS_ADMIN))
1954 return -EPERM;
1955
Hans Verkuil1b9e94d2012-09-09 09:23:31 -03001956 if (!v4l2_chip_match_host(&reg->match)) {
1957 /* TODO: subdev errors should not be ignored, this should become a
1958 subdev helper function. */
1959 bttv_call_all(btv, core, s_register, reg);
1960 return 0;
1961 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001962
1963 /* bt848 has a 12-bit register space */
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001964 btwrite(reg->val, reg->reg & 0xfff);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001965
1966 return 0;
1967}
1968#endif
1969
Michael Schimeke5bd0262007-01-18 16:17:39 -03001970/* Given cropping boundaries b and the scaled width and height of a
1971 single field or frame, which must not exceed hardware limits, this
1972 function adjusts the cropping parameters c. */
1973static void
1974bttv_crop_adjust (struct bttv_crop * c,
1975 const struct v4l2_rect * b,
1976 __s32 width,
1977 __s32 height,
1978 enum v4l2_field field)
1979{
1980 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
1981 __s32 max_left;
1982 __s32 max_top;
1983
1984 if (width < c->min_scaled_width) {
1985 /* Max. hor. scale factor 16:1. */
1986 c->rect.width = width * 16;
1987 } else if (width > c->max_scaled_width) {
1988 /* Min. hor. scale factor 1:1. */
1989 c->rect.width = width;
1990
1991 max_left = b->left + b->width - width;
1992 max_left = min(max_left, (__s32) MAX_HDELAY);
1993 if (c->rect.left > max_left)
1994 c->rect.left = max_left;
1995 }
1996
1997 if (height < c->min_scaled_height) {
1998 /* Max. vert. scale factor 16:1, single fields 8:1. */
1999 c->rect.height = height * 16;
2000 } else if (frame_height > c->max_scaled_height) {
2001 /* Min. vert. scale factor 1:1.
2002 Top and height count field lines times two. */
2003 c->rect.height = (frame_height + 1) & ~1;
2004
2005 max_top = b->top + b->height - c->rect.height;
2006 if (c->rect.top > max_top)
2007 c->rect.top = max_top;
2008 }
2009
2010 bttv_crop_calc_limits(c);
2011}
2012
2013/* Returns an error if scaling to a frame or single field with the given
2014 width and height is not possible with the current cropping parameters
2015 and width aligned according to width_mask. If adjust_size is TRUE the
2016 function may adjust the width and/or height instead, rounding width
2017 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2018 also adjust the current cropping parameters to get closer to the
2019 desired image size. */
2020static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002021limit_scaled_size_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002022 __s32 * width,
2023 __s32 * height,
2024 enum v4l2_field field,
2025 unsigned int width_mask,
2026 unsigned int width_bias,
2027 int adjust_size,
2028 int adjust_crop)
2029{
2030 struct bttv *btv = fh->btv;
2031 const struct v4l2_rect *b;
2032 struct bttv_crop *c;
2033 __s32 min_width;
2034 __s32 min_height;
2035 __s32 max_width;
2036 __s32 max_height;
2037 int rc;
2038
2039 BUG_ON((int) width_mask >= 0 ||
2040 width_bias >= (unsigned int) -width_mask);
2041
2042 /* Make sure tvnorm, vbi_end and the current cropping parameters
2043 remain consistent until we're done. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03002044
2045 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2046
2047 /* Do crop - use current, don't - use default parameters. */
2048 c = &btv->crop[!!fh->do_crop];
2049
2050 if (fh->do_crop
2051 && adjust_size
2052 && adjust_crop
2053 && !locked_btres(btv, VIDEO_RESOURCES)) {
2054 min_width = 48;
2055 min_height = 32;
2056
2057 /* We cannot scale up. When the scaled image is larger
2058 than crop.rect we adjust the crop.rect as required
2059 by the V4L2 spec, hence cropcap.bounds are our limit. */
2060 max_width = min(b->width, (__s32) MAX_HACTIVE);
2061 max_height = b->height;
2062
2063 /* We cannot capture the same line as video and VBI data.
2064 Note btv->vbi_end is really a minimum, see
2065 bttv_vbi_try_fmt(). */
2066 if (btv->vbi_end > b->top) {
2067 max_height -= btv->vbi_end - b->top;
2068 rc = -EBUSY;
2069 if (min_height > max_height)
2070 goto fail;
2071 }
2072 } else {
2073 rc = -EBUSY;
2074 if (btv->vbi_end > c->rect.top)
2075 goto fail;
2076
2077 min_width = c->min_scaled_width;
2078 min_height = c->min_scaled_height;
2079 max_width = c->max_scaled_width;
2080 max_height = c->max_scaled_height;
2081
2082 adjust_crop = 0;
2083 }
2084
2085 min_width = (min_width - width_mask - 1) & width_mask;
2086 max_width = max_width & width_mask;
2087
2088 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2089 min_height = min_height;
2090 /* Min. scale factor is 1:1. */
2091 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2092
2093 if (adjust_size) {
2094 *width = clamp(*width, min_width, max_width);
2095 *height = clamp(*height, min_height, max_height);
2096
2097 /* Round after clamping to avoid overflow. */
2098 *width = (*width + width_bias) & width_mask;
2099
2100 if (adjust_crop) {
2101 bttv_crop_adjust(c, b, *width, *height, field);
2102
2103 if (btv->vbi_end > c->rect.top) {
2104 /* Move the crop window out of the way. */
2105 c->rect.top = btv->vbi_end;
2106 }
2107 }
2108 } else {
2109 rc = -EINVAL;
2110 if (*width < min_width ||
2111 *height < min_height ||
2112 *width > max_width ||
2113 *height > max_height ||
2114 0 != (*width & ~width_mask))
2115 goto fail;
2116 }
2117
2118 rc = 0; /* success */
2119
2120 fail:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002121
2122 return rc;
2123}
2124
2125/* Returns an error if the given overlay window dimensions are not
2126 possible with the current cropping parameters. If adjust_size is
2127 TRUE the function may adjust the window width and/or height
2128 instead, however it always rounds the horizontal position and
2129 width as btcx_align() does. If adjust_crop is TRUE the function
2130 may also adjust the current cropping parameters to get closer
2131 to the desired window size. */
2132static int
Hans Verkuilc13eb7032013-02-07 07:56:11 -03002133verify_window_lock(struct bttv_fh *fh, struct v4l2_window *win,
2134 int adjust_size, int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002137 unsigned int width_mask;
2138 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Hans Verkuilc13eb7032013-02-07 07:56:11 -03002140 if (win->w.width < 48)
2141 win->w.width = 48;
2142 if (win->w.height < 32)
2143 win->w.height = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (win->clipcount > 2048)
Hans Verkuilc13eb7032013-02-07 07:56:11 -03002145 win->clipcount = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Hans Verkuilc13eb7032013-02-07 07:56:11 -03002147 win->chromakey = 0;
2148 win->global_alpha = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 field = win->field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Hans Verkuilc13eb7032013-02-07 07:56:11 -03002151 switch (field) {
2152 case V4L2_FIELD_TOP:
2153 case V4L2_FIELD_BOTTOM:
2154 case V4L2_FIELD_INTERLACED:
2155 break;
2156 default:
2157 field = V4L2_FIELD_ANY;
2158 break;
2159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if (V4L2_FIELD_ANY == field) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002161 __s32 height2;
2162
2163 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2164 field = (win->w.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 ? V4L2_FIELD_INTERLACED
2166 : V4L2_FIELD_TOP;
2167 }
Hans Verkuilc13eb7032013-02-07 07:56:11 -03002168 win->field = field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Michael Schimeke5bd0262007-01-18 16:17:39 -03002170 if (NULL == fh->ovfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return -EINVAL;
Hans Verkuilc13eb7032013-02-07 07:56:11 -03002172 /* 4-byte alignment. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03002173 width_mask = ~0;
2174 switch (fh->ovfmt->depth) {
2175 case 8:
2176 case 24:
2177 width_mask = ~3;
2178 break;
2179 case 16:
2180 width_mask = ~1;
2181 break;
2182 case 32:
2183 break;
2184 default:
2185 BUG();
2186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Michael Schimeke5bd0262007-01-18 16:17:39 -03002188 win->w.width -= win->w.left & ~width_mask;
2189 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2190
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002191 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002192 field, width_mask,
2193 /* width_bias: round down */ 0,
2194 adjust_size, adjust_crop);
2195 if (0 != rc)
2196 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return 0;
2198}
2199
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002200static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 struct v4l2_window *win, int fixup)
2202{
2203 struct v4l2_clip *clips = NULL;
2204 int n,size,retval = 0;
2205
2206 if (NULL == fh->ovfmt)
2207 return -EINVAL;
2208 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2209 return -EINVAL;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002210 retval = verify_window_lock(fh, win,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002211 /* adjust_size */ fixup,
2212 /* adjust_crop */ fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (0 != retval)
2214 return retval;
2215
2216 /* copy clips -- luckily v4l1 + v4l2 are binary
2217 compatible here ...*/
2218 n = win->clipcount;
2219 size = sizeof(*clips)*(n+4);
2220 clips = kmalloc(size,GFP_KERNEL);
2221 if (NULL == clips)
2222 return -ENOMEM;
2223 if (n > 0) {
2224 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2225 kfree(clips);
2226 return -EFAULT;
2227 }
2228 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 /* clip against screen */
2231 if (NULL != btv->fbuf.base)
2232 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2233 &win->w, clips, n);
2234 btcx_sort_clips(clips,n);
2235
2236 /* 4-byte alignments */
2237 switch (fh->ovfmt->depth) {
2238 case 8:
2239 case 24:
2240 btcx_align(&win->w, clips, n, 3);
2241 break;
2242 case 16:
2243 btcx_align(&win->w, clips, n, 1);
2244 break;
2245 case 32:
2246 /* no alignment fixups needed */
2247 break;
2248 default:
2249 BUG();
2250 }
2251
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002252 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 fh->ov.clips = clips;
2254 fh->ov.nclips = n;
2255
2256 fh->ov.w = win->w;
2257 fh->ov.field = win->field;
2258 fh->ov.setup_ok = 1;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 btv->init.ov.w.width = win->w.width;
2261 btv->init.ov.w.height = win->w.height;
2262 btv->init.ov.field = win->field;
2263
2264 /* update overlay if needed */
2265 retval = 0;
2266 if (check_btres(fh, RESOURCE_OVERLAY)) {
2267 struct bttv_buffer *new;
2268
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002269 new = videobuf_sg_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03002270 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2272 retval = bttv_switch_overlay(btv,fh,new);
2273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return retval;
2275}
2276
2277/* ----------------------------------------------------------------------- */
2278
2279static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2280{
2281 struct videobuf_queue* q = NULL;
2282
2283 switch (fh->type) {
2284 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2285 q = &fh->cap;
2286 break;
2287 case V4L2_BUF_TYPE_VBI_CAPTURE:
2288 q = &fh->vbi;
2289 break;
2290 default:
2291 BUG();
2292 }
2293 return q;
2294}
2295
2296static int bttv_resource(struct bttv_fh *fh)
2297{
2298 int res = 0;
2299
2300 switch (fh->type) {
2301 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002302 res = RESOURCE_VIDEO_STREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 break;
2304 case V4L2_BUF_TYPE_VBI_CAPTURE:
2305 res = RESOURCE_VBI;
2306 break;
2307 default:
2308 BUG();
2309 }
2310 return res;
2311}
2312
2313static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2314{
2315 struct videobuf_queue *q = bttv_queue(fh);
2316 int res = bttv_resource(fh);
2317
2318 if (check_btres(fh,res))
2319 return -EBUSY;
2320 if (videobuf_queue_is_busy(q))
2321 return -EBUSY;
2322 fh->type = type;
2323 return 0;
2324}
2325
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002326static void
2327pix_format_set_size (struct v4l2_pix_format * f,
2328 const struct bttv_format * fmt,
2329 unsigned int width,
2330 unsigned int height)
2331{
2332 f->width = width;
2333 f->height = height;
2334
2335 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2336 f->bytesperline = width; /* Y plane */
2337 f->sizeimage = (width * height * fmt->depth) >> 3;
2338 } else {
2339 f->bytesperline = (width * fmt->depth) >> 3;
2340 f->sizeimage = height * f->bytesperline;
2341 }
2342}
2343
Hans Verkuil78b526a2008-05-28 12:16:41 -03002344static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002345 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002347 struct bttv_fh *fh = priv;
2348
2349 pix_format_set_size(&f->fmt.pix, fh->fmt,
2350 fh->width, fh->height);
2351 f->fmt.pix.field = fh->cap.field;
2352 f->fmt.pix.pixelformat = fh->fmt->fourcc;
Hans Verkuilf5864892013-02-06 11:58:59 -03002353 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002354
2355 return 0;
2356}
2357
Hans Verkuil78b526a2008-05-28 12:16:41 -03002358static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002359 struct v4l2_format *f)
2360{
2361 struct bttv_fh *fh = priv;
2362
2363 f->fmt.win.w = fh->ov.w;
2364 f->fmt.win.field = fh->ov.field;
2365
2366 return 0;
2367}
2368
Hans Verkuil78b526a2008-05-28 12:16:41 -03002369static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002370 struct v4l2_format *f)
2371{
2372 const struct bttv_format *fmt;
2373 struct bttv_fh *fh = priv;
2374 struct bttv *btv = fh->btv;
2375 enum v4l2_field field;
2376 __s32 width, height;
Hans Verkuilee70e3d2013-02-06 12:03:30 -03002377 __s32 height2;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002378 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002379
2380 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2381 if (NULL == fmt)
2382 return -EINVAL;
2383
2384 field = f->fmt.pix.field;
2385
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002386 switch (field) {
2387 case V4L2_FIELD_TOP:
2388 case V4L2_FIELD_BOTTOM:
2389 case V4L2_FIELD_ALTERNATE:
2390 case V4L2_FIELD_INTERLACED:
2391 break;
Hans Verkuilee70e3d2013-02-06 12:03:30 -03002392 case V4L2_FIELD_SEQ_BT:
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002393 case V4L2_FIELD_SEQ_TB:
Hans Verkuilee70e3d2013-02-06 12:03:30 -03002394 if (!(fmt->flags & FORMAT_FLAGS_PLANAR)) {
2395 field = V4L2_FIELD_SEQ_TB;
2396 break;
2397 }
2398 /* fall through */
2399 default: /* FIELD_ANY case */
2400 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2401 field = (f->fmt.pix.height > height2)
2402 ? V4L2_FIELD_INTERLACED
2403 : V4L2_FIELD_BOTTOM;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002404 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002406
2407 width = f->fmt.pix.width;
2408 height = f->fmt.pix.height;
2409
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002410 rc = limit_scaled_size_lock(fh, &width, &height, field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002411 /* width_mask: 4 pixels */ ~3,
2412 /* width_bias: nearest */ 2,
2413 /* adjust_size */ 1,
2414 /* adjust_crop */ 0);
2415 if (0 != rc)
2416 return rc;
2417
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002418 /* update data for the application */
2419 f->fmt.pix.field = field;
2420 pix_format_set_size(&f->fmt.pix, fmt, width, height);
Hans Verkuilf5864892013-02-06 11:58:59 -03002421 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002422
2423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
Hans Verkuil78b526a2008-05-28 12:16:41 -03002426static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002427 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002429 struct bttv_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Hans Verkuilc13eb7032013-02-07 07:56:11 -03002431 verify_window_lock(fh, &f->fmt.win,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002432 /* adjust_size */ 1,
2433 /* adjust_crop */ 0);
Hans Verkuilc13eb7032013-02-07 07:56:11 -03002434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
Hans Verkuil78b526a2008-05-28 12:16:41 -03002437static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002438 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
2440 int retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002441 const struct bttv_format *fmt;
2442 struct bttv_fh *fh = priv;
2443 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002444 __s32 width, height;
2445 enum v4l2_field field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002447 retval = bttv_switch_type(fh, f->type);
2448 if (0 != retval)
2449 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Hans Verkuil78b526a2008-05-28 12:16:41 -03002451 retval = bttv_try_fmt_vid_cap(file, priv, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002452 if (0 != retval)
2453 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002455 width = f->fmt.pix.width;
2456 height = f->fmt.pix.height;
2457 field = f->fmt.pix.field;
2458
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002459 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002460 /* width_mask: 4 pixels */ ~3,
2461 /* width_bias: nearest */ 2,
2462 /* adjust_size */ 1,
2463 /* adjust_crop */ 1);
2464 if (0 != retval)
2465 return retval;
2466
2467 f->fmt.pix.field = field;
2468
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002469 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002471 /* update our state informations */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002472 fh->fmt = fmt;
2473 fh->cap.field = f->fmt.pix.field;
2474 fh->cap.last = V4L2_FIELD_NONE;
2475 fh->width = f->fmt.pix.width;
2476 fh->height = f->fmt.pix.height;
2477 btv->init.fmt = fmt;
2478 btv->init.width = f->fmt.pix.width;
2479 btv->init.height = f->fmt.pix.height;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002480
2481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Hans Verkuil78b526a2008-05-28 12:16:41 -03002484static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002485 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002487 struct bttv_fh *fh = priv;
2488 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002490 if (no_overlay > 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03002491 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002492 return -EINVAL;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002493 }
Michael Krufky5e453dc2006-01-09 15:32:31 -02002494
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002495 return setup_window_lock(fh, btv, &f->fmt.win, 1);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002496}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002498static int bttv_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002499 struct v4l2_capability *cap)
2500{
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002501 struct video_device *vdev = video_devdata(file);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002502 struct bttv_fh *fh = priv;
2503 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002505 if (0 == v4l2)
2506 return -EINVAL;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002507
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002508 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2509 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2510 snprintf(cap->bus_info, sizeof(cap->bus_info),
2511 "PCI:%s", pci_name(btv->c.pci));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002512 cap->capabilities =
2513 V4L2_CAP_VIDEO_CAPTURE |
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002514 V4L2_CAP_READWRITE |
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002515 V4L2_CAP_STREAMING |
2516 V4L2_CAP_DEVICE_CAPS;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002517 if (no_overlay <= 0)
2518 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002519 if (btv->vbi_dev)
2520 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
2521 if (btv->radio_dev)
2522 cap->capabilities |= V4L2_CAP_RADIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002524 /*
2525 * No need to lock here: those vars are initialized during board
2526 * probe and remains untouched during the rest of the driver lifecycle
2527 */
2528 if (btv->has_saa6588)
2529 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
Trent Piephoabb03622009-01-28 21:32:59 -03002530 if (btv->tuner_type != TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002531 cap->capabilities |= V4L2_CAP_TUNER;
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002532 if (vdev->vfl_type == VFL_TYPE_GRABBER)
2533 cap->device_caps = cap->capabilities &
2534 (V4L2_CAP_VIDEO_CAPTURE |
2535 V4L2_CAP_READWRITE |
2536 V4L2_CAP_STREAMING |
2537 V4L2_CAP_VIDEO_OVERLAY |
2538 V4L2_CAP_TUNER);
2539 else if (vdev->vfl_type == VFL_TYPE_VBI)
2540 cap->device_caps = cap->capabilities &
2541 (V4L2_CAP_VBI_CAPTURE |
2542 V4L2_CAP_READWRITE |
2543 V4L2_CAP_STREAMING |
2544 V4L2_CAP_TUNER);
2545 else {
2546 cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
2547 if (btv->has_saa6588)
2548 cap->device_caps |= V4L2_CAP_READWRITE |
2549 V4L2_CAP_RDS_CAPTURE;
2550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 return 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002552}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002554static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2555{
2556 int index = -1, i;
2557
2558 for (i = 0; i < FORMATS; i++) {
2559 if (formats[i].fourcc != -1)
2560 index++;
2561 if ((unsigned int)index == f->index)
2562 break;
2563 }
2564 if (FORMATS == i)
2565 return -EINVAL;
2566
2567 f->pixelformat = formats[i].fourcc;
2568 strlcpy(f->description, formats[i].name, sizeof(f->description));
2569
2570 return i;
2571}
2572
Hans Verkuil78b526a2008-05-28 12:16:41 -03002573static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002574 struct v4l2_fmtdesc *f)
2575{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002576 int rc = bttv_enum_fmt_cap_ovr(f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002577
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002578 if (rc < 0)
2579 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002580
2581 return 0;
2582}
2583
Hans Verkuil78b526a2008-05-28 12:16:41 -03002584static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002585 struct v4l2_fmtdesc *f)
2586{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002587 int rc;
2588
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002589 if (no_overlay > 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03002590 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002591 return -EINVAL;
2592 }
2593
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002594 rc = bttv_enum_fmt_cap_ovr(f);
2595
2596 if (rc < 0)
2597 return rc;
2598
2599 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002600 return -EINVAL;
2601
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002602 return 0;
2603}
2604
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002605static int bttv_g_fbuf(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002606 struct v4l2_framebuffer *fb)
2607{
2608 struct bttv_fh *fh = f;
2609 struct bttv *btv = fh->btv;
2610
2611 *fb = btv->fbuf;
2612 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
Hans Verkuila12fd702013-02-06 12:00:03 -03002613 fb->flags = V4L2_FBUF_FLAG_PRIMARY;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002614 if (fh->ovfmt)
2615 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2616 return 0;
2617}
2618
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002619static int bttv_overlay(struct file *file, void *f, unsigned int on)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002620{
2621 struct bttv_fh *fh = f;
2622 struct bttv *btv = fh->btv;
2623 struct bttv_buffer *new;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002624 int retval = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002625
2626 if (on) {
2627 /* verify args */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002628 if (unlikely(!btv->fbuf.base)) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002629 return -EINVAL;
2630 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002631 if (unlikely(!fh->ov.setup_ok)) {
Joe Perches8af443e2011-08-21 19:56:48 -03002632 dprintk("%d: overlay: !setup_ok\n", btv->c.nr);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002633 retval = -EINVAL;
2634 }
2635 if (retval)
2636 return retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002637 }
2638
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002639 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002640 return -EBUSY;
2641
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002642 if (on) {
2643 fh->ov.tvnorm = btv->tvnorm;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002644 new = videobuf_sg_alloc(sizeof(*new));
Robert Fitzsimons7c018802008-02-13 16:38:11 -03002645 new->crop = btv->crop[!!fh->do_crop].rect;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002646 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2647 } else {
2648 new = NULL;
2649 }
2650
2651 /* switch over */
2652 retval = bttv_switch_overlay(btv, fh, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return retval;
2654}
2655
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002656static int bttv_s_fbuf(struct file *file, void *f,
Hans Verkuile6eb28c2012-09-04 10:26:45 -03002657 const struct v4l2_framebuffer *fb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002659 struct bttv_fh *fh = f;
2660 struct bttv *btv = fh->btv;
2661 const struct bttv_format *fmt;
2662 int retval;
2663
2664 if (!capable(CAP_SYS_ADMIN) &&
2665 !capable(CAP_SYS_RAWIO))
2666 return -EPERM;
2667
2668 /* check args */
2669 fmt = format_by_fourcc(fb->fmt.pixelformat);
2670 if (NULL == fmt)
2671 return -EINVAL;
2672 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2673 return -EINVAL;
2674
2675 retval = -EINVAL;
2676 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2677 __s32 width = fb->fmt.width;
2678 __s32 height = fb->fmt.height;
2679
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002680 retval = limit_scaled_size_lock(fh, &width, &height,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002681 V4L2_FIELD_INTERLACED,
2682 /* width_mask */ ~3,
2683 /* width_bias */ 2,
2684 /* adjust_size */ 0,
2685 /* adjust_crop */ 0);
2686 if (0 != retval)
2687 return retval;
2688 }
2689
2690 /* ok, accept it */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002691 btv->fbuf.base = fb->base;
2692 btv->fbuf.fmt.width = fb->fmt.width;
2693 btv->fbuf.fmt.height = fb->fmt.height;
2694 if (0 != fb->fmt.bytesperline)
2695 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2696 else
2697 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2698
2699 retval = 0;
2700 fh->ovfmt = fmt;
2701 btv->init.ovfmt = fmt;
2702 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2703 fh->ov.w.left = 0;
2704 fh->ov.w.top = 0;
2705 fh->ov.w.width = fb->fmt.width;
2706 fh->ov.w.height = fb->fmt.height;
2707 btv->init.ov.w.width = fb->fmt.width;
2708 btv->init.ov.w.height = fb->fmt.height;
2709 kfree(fh->ov.clips);
2710 fh->ov.clips = NULL;
2711 fh->ov.nclips = 0;
2712
2713 if (check_btres(fh, RESOURCE_OVERLAY)) {
2714 struct bttv_buffer *new;
2715
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002716 new = videobuf_sg_alloc(sizeof(*new));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002717 new->crop = btv->crop[!!fh->do_crop].rect;
2718 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2719 retval = bttv_switch_overlay(btv, fh, new);
2720 }
2721 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002722 return retval;
2723}
2724
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002725static int bttv_reqbufs(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002726 struct v4l2_requestbuffers *p)
2727{
2728 struct bttv_fh *fh = priv;
2729 return videobuf_reqbufs(bttv_queue(fh), p);
2730}
2731
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002732static int bttv_querybuf(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002733 struct v4l2_buffer *b)
2734{
2735 struct bttv_fh *fh = priv;
2736 return videobuf_querybuf(bttv_queue(fh), b);
2737}
2738
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002739static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002740{
2741 struct bttv_fh *fh = priv;
2742 struct bttv *btv = fh->btv;
2743 int res = bttv_resource(fh);
2744
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002745 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002746 return -EBUSY;
2747
2748 return videobuf_qbuf(bttv_queue(fh), b);
2749}
2750
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002751static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002752{
2753 struct bttv_fh *fh = priv;
2754 return videobuf_dqbuf(bttv_queue(fh), b,
2755 file->f_flags & O_NONBLOCK);
2756}
2757
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002758static int bttv_streamon(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002759 enum v4l2_buf_type type)
2760{
2761 struct bttv_fh *fh = priv;
2762 struct bttv *btv = fh->btv;
2763 int res = bttv_resource(fh);
2764
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002765 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002766 return -EBUSY;
2767 return videobuf_streamon(bttv_queue(fh));
2768}
2769
2770
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002771static int bttv_streamoff(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002772 enum v4l2_buf_type type)
2773{
2774 struct bttv_fh *fh = priv;
2775 struct bttv *btv = fh->btv;
2776 int retval;
2777 int res = bttv_resource(fh);
2778
2779
2780 retval = videobuf_streamoff(bttv_queue(fh));
2781 if (retval < 0)
2782 return retval;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002783 free_btres_lock(btv, fh, res);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002784 return 0;
2785}
2786
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002787static int bttv_g_parm(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002788 struct v4l2_streamparm *parm)
2789{
2790 struct bttv_fh *fh = f;
2791 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002792
Hans Verkuila652ef62012-09-09 10:27:57 -03002793 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2794 return -EINVAL;
2795 parm->parm.capture.readbuffers = gbuffers;
Trent Piepho51f0b8d2009-03-04 01:21:02 -03002796 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2797 &parm->parm.capture.timeperframe);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002798
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002799 return 0;
2800}
2801
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002802static int bttv_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002803 struct v4l2_tuner *t)
2804{
2805 struct bttv_fh *fh = priv;
2806 struct bttv *btv = fh->btv;
2807
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002808 if (0 != t->index)
2809 return -EINVAL;
2810
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002811 t->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil3d4b8032013-02-06 12:44:07 -03002812 t->capability = V4L2_TUNER_CAP_NORM;
Hans Verkuil859f0272009-03-28 08:29:00 -03002813 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002814 strcpy(t->name, "Television");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002815 t->type = V4L2_TUNER_ANALOG_TV;
2816 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2817 t->signal = 0xffff;
2818
2819 if (btv->audio_mode_gpio)
2820 btv->audio_mode_gpio(btv, t, 0);
2821
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002822 return 0;
2823}
2824
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002825static int bttv_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002826 struct v4l2_cropcap *cap)
2827{
2828 struct bttv_fh *fh = priv;
2829 struct bttv *btv = fh->btv;
2830
2831 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2832 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2833 return -EINVAL;
2834
2835 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
2836
2837 return 0;
2838}
2839
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002840static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002841{
2842 struct bttv_fh *fh = f;
2843 struct bttv *btv = fh->btv;
2844
2845 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2846 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2847 return -EINVAL;
2848
2849 /* No fh->do_crop = 1; because btv->crop[1] may be
2850 inconsistent with fh->width or fh->height and apps
2851 do not expect a change here. */
2852
2853 crop->c = btv->crop[!!fh->do_crop].rect;
2854
2855 return 0;
2856}
2857
Hans Verkuil4f9965942012-09-05 05:10:48 -03002858static int bttv_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002859{
2860 struct bttv_fh *fh = f;
2861 struct bttv *btv = fh->btv;
2862 const struct v4l2_rect *b;
2863 int retval;
2864 struct bttv_crop c;
2865 __s32 b_left;
2866 __s32 b_top;
2867 __s32 b_right;
2868 __s32 b_bottom;
2869
2870 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2871 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2872 return -EINVAL;
2873
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002874 /* Make sure tvnorm, vbi_end and the current cropping
2875 parameters remain consistent until we're done. Note
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002876 read() may change vbi_end in check_alloc_btres_lock(). */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002877 retval = -EBUSY;
2878
2879 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002880 return retval;
2881 }
2882
2883 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2884
2885 b_left = b->left;
2886 b_right = b_left + b->width;
2887 b_bottom = b->top + b->height;
2888
2889 b_top = max(b->top, btv->vbi_end);
2890 if (b_top + 32 >= b_bottom) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002891 return retval;
2892 }
2893
2894 /* Min. scaled size 48 x 32. */
Hans Verkuil4f9965942012-09-05 05:10:48 -03002895 c.rect.left = clamp_t(s32, crop->c.left, b_left, b_right - 48);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002896 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
2897
Hans Verkuil4f9965942012-09-05 05:10:48 -03002898 c.rect.width = clamp_t(s32, crop->c.width,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002899 48, b_right - c.rect.left);
2900
Hans Verkuil4f9965942012-09-05 05:10:48 -03002901 c.rect.top = clamp_t(s32, crop->c.top, b_top, b_bottom - 32);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002902 /* Top and height must be a multiple of two. */
2903 c.rect.top = (c.rect.top + 1) & ~1;
2904
Hans Verkuil4f9965942012-09-05 05:10:48 -03002905 c.rect.height = clamp_t(s32, crop->c.height,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002906 32, b_bottom - c.rect.top);
2907 c.rect.height = (c.rect.height + 1) & ~1;
2908
2909 bttv_crop_calc_limits(&c);
2910
2911 btv->crop[1] = c;
2912
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002913 fh->do_crop = 1;
2914
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002915 if (fh->width < c.min_scaled_width) {
2916 fh->width = c.min_scaled_width;
2917 btv->init.width = c.min_scaled_width;
2918 } else if (fh->width > c.max_scaled_width) {
2919 fh->width = c.max_scaled_width;
2920 btv->init.width = c.max_scaled_width;
2921 }
2922
2923 if (fh->height < c.min_scaled_height) {
2924 fh->height = c.min_scaled_height;
2925 btv->init.height = c.min_scaled_height;
2926 } else if (fh->height > c.max_scaled_height) {
2927 fh->height = c.max_scaled_height;
2928 btv->init.height = c.max_scaled_height;
2929 }
2930
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002931 return 0;
2932}
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934static ssize_t bttv_read(struct file *file, char __user *data,
2935 size_t count, loff_t *ppos)
2936{
2937 struct bttv_fh *fh = file->private_data;
2938 int retval = 0;
2939
2940 if (fh->btv->errors)
2941 bttv_reinit_bt848(fh->btv);
Joe Perches8af443e2011-08-21 19:56:48 -03002942 dprintk("%d: read count=%d type=%s\n",
2943 fh->btv->c.nr, (int)count, v4l2_type_names[fh->type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 switch (fh->type) {
2946 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002947 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002948 /* VIDEO_READ in use by another fh,
2949 or VIDEO_STREAM by any fh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 return -EBUSY;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2953 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002954 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 break;
2956 case V4L2_BUF_TYPE_VBI_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002957 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 return -EBUSY;
2959 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2960 file->f_flags & O_NONBLOCK);
2961 break;
2962 default:
2963 BUG();
2964 }
2965 return retval;
2966}
2967
2968static unsigned int bttv_poll(struct file *file, poll_table *wait)
2969{
2970 struct bttv_fh *fh = file->private_data;
2971 struct bttv_buffer *buf;
2972 enum v4l2_field field;
Hans Verkuilae50f0f2013-02-06 12:42:13 -03002973 unsigned int rc = 0;
2974 unsigned long req_events = poll_requested_events(wait);
2975
2976 if (v4l2_event_pending(&fh->fh))
2977 rc = POLLPRI;
2978 else if (req_events & POLLPRI)
2979 poll_wait(file, &fh->fh.wait, wait);
2980
2981 if (!(req_events & (POLLIN | POLLRDNORM)))
2982 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002985 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Hans Verkuilae50f0f2013-02-06 12:42:13 -03002986 return rc | POLLERR;
2987 return rc | videobuf_poll_stream(file, &fh->vbi, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
2989
Michael Schimeke5bd0262007-01-18 16:17:39 -03002990 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 /* streaming capture */
2992 if (list_empty(&fh->cap.stream))
Hans Verkuilae50f0f2013-02-06 12:42:13 -03002993 return rc | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2995 } else {
2996 /* read() capture */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 if (NULL == fh->cap.read_buf) {
2998 /* need to capture a new frame */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002999 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003000 return rc | POLLERR;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003001 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003002 if (NULL == fh->cap.read_buf)
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003003 return rc | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3005 field = videobuf_next_field(&fh->cap);
3006 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -08003007 kfree (fh->cap.read_buf);
3008 fh->cap.read_buf = NULL;
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003009 return rc | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
3011 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3012 fh->cap.read_off = 0;
3013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 buf = (struct bttv_buffer*)fh->cap.read_buf;
3015 }
3016
3017 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03003018 if (buf->vb.state == VIDEOBUF_DONE ||
3019 buf->vb.state == VIDEOBUF_ERROR)
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003020 rc = rc | POLLIN|POLLRDNORM;
Figo.zhang9fd64182009-06-16 13:31:29 -03003021 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
Hans Verkuilbec43662008-12-30 06:58:20 -03003024static int bttv_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003026 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003027 struct bttv *btv = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 struct bttv_fh *fh;
3029 enum v4l2_buf_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Joe Perches8af443e2011-08-21 19:56:48 -03003031 dprintk("open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Laurent Pinchart327ae592009-11-27 13:57:55 -03003033 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003034 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Laurent Pinchart327ae592009-11-27 13:57:55 -03003035 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003036 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3037 } else {
3038 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -03003040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Joe Perches8af443e2011-08-21 19:56:48 -03003042 dprintk("%d: open called (type=%s)\n",
3043 btv->c.nr, v4l2_type_names[type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045 /* allocate per filehandle data */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003046 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3047 if (unlikely(!fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 return -ENOMEM;
Hans Verkuil01df5302013-02-06 12:40:28 -03003049 btv->users++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 file->private_data = fh;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003051
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 *fh = btv->init;
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003053 v4l2_fh_init(&fh->fh, vdev);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 fh->type = type;
3056 fh->ov.setup_ok = 0;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003057
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003058 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3059 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3061 V4L2_FIELD_INTERLACED,
3062 sizeof(struct bttv_buffer),
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003063 fh, &btv->lock);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003064 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3065 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 V4L2_BUF_TYPE_VBI_CAPTURE,
3067 V4L2_FIELD_SEQ_TB,
3068 sizeof(struct bttv_buffer),
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003069 fh, &btv->lock);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03003070 set_tvnorm(btv,btv->tvnorm);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003071 set_input(btv, btv->input, btv->tvnorm);
Frank Schaefer0757f5c2013-03-21 13:51:20 -03003072 audio_mute(btv, btv->mute);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003073
3074 /* The V4L2 spec requires one global set of cropping parameters
3075 which only change on request. These are stored in btv->crop[1].
3076 However for compatibility with V4L apps and cropping unaware
3077 V4L2 apps we now reset the cropping parameters as seen through
3078 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3079 will use btv->crop[0], the default cropping parameters for the
3080 current video standard, and VIDIOC_S_FMT will not implicitely
3081 change the cropping parameters until VIDIOC_S_CROP has been
3082 called. */
3083 fh->do_crop = !reset_crop; /* module parameter */
3084
3085 /* Likewise there should be one global set of VBI capture
3086 parameters, but for compatibility with V4L apps and earlier
3087 driver versions each fh has its own parameters. */
3088 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 bttv_field_count(btv);
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003091 v4l2_fh_add(&fh->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return 0;
3093}
3094
Hans Verkuilbec43662008-12-30 06:58:20 -03003095static int bttv_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
3097 struct bttv_fh *fh = file->private_data;
3098 struct bttv *btv = fh->btv;
3099
3100 /* turn off overlay */
3101 if (check_btres(fh, RESOURCE_OVERLAY))
3102 bttv_switch_overlay(btv,fh,NULL);
3103
3104 /* stop video capture */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003105 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 videobuf_streamoff(&fh->cap);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003107 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
3109 if (fh->cap.read_buf) {
3110 buffer_release(&fh->cap,fh->cap.read_buf);
3111 kfree(fh->cap.read_buf);
3112 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03003113 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003114 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 /* stop vbi capture */
3118 if (check_btres(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03003119 videobuf_stop(&fh->vbi);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003120 free_btres_lock(btv,fh,RESOURCE_VBI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 }
3122
3123 /* free stuff */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 videobuf_mmap_free(&fh->cap);
3126 videobuf_mmap_free(&fh->vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 btv->users--;
3130 bttv_field_count(btv);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003131
3132 if (!btv->users)
Hans Verkuil01df5302013-02-06 12:40:28 -03003133 audio_mute(btv, btv->mute);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003134
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003135 v4l2_fh_del(&fh->fh);
3136 v4l2_fh_exit(&fh->fh);
3137 kfree(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return 0;
3139}
3140
3141static int
3142bttv_mmap(struct file *file, struct vm_area_struct *vma)
3143{
3144 struct bttv_fh *fh = file->private_data;
3145
Joe Perches8af443e2011-08-21 19:56:48 -03003146 dprintk("%d: mmap type=%s 0x%lx+%ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 fh->btv->c.nr, v4l2_type_names[fh->type],
3148 vma->vm_start, vma->vm_end - vma->vm_start);
3149 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3150}
3151
Hans Verkuilbec43662008-12-30 06:58:20 -03003152static const struct v4l2_file_operations bttv_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153{
Mauro Carvalho Chehab8979e9d2010-09-15 08:22:09 -03003154 .owner = THIS_MODULE,
3155 .open = bttv_open,
3156 .release = bttv_release,
3157 .unlocked_ioctl = video_ioctl2,
3158 .read = bttv_read,
3159 .mmap = bttv_mmap,
3160 .poll = bttv_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161};
3162
Hans Verkuila3998102008-07-21 02:57:38 -03003163static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003164 .vidioc_querycap = bttv_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003165 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3166 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3167 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3168 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3169 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3170 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3171 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3172 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003173 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3174 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3175 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003176 .vidioc_cropcap = bttv_cropcap,
3177 .vidioc_reqbufs = bttv_reqbufs,
3178 .vidioc_querybuf = bttv_querybuf,
3179 .vidioc_qbuf = bttv_qbuf,
3180 .vidioc_dqbuf = bttv_dqbuf,
3181 .vidioc_s_std = bttv_s_std,
Hans Verkuil6795cc52013-02-06 12:43:07 -03003182 .vidioc_g_std = bttv_g_std,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003183 .vidioc_enum_input = bttv_enum_input,
3184 .vidioc_g_input = bttv_g_input,
3185 .vidioc_s_input = bttv_s_input,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003186 .vidioc_streamon = bttv_streamon,
3187 .vidioc_streamoff = bttv_streamoff,
3188 .vidioc_g_tuner = bttv_g_tuner,
3189 .vidioc_s_tuner = bttv_s_tuner,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003190 .vidioc_g_crop = bttv_g_crop,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003191 .vidioc_s_crop = bttv_s_crop,
3192 .vidioc_g_fbuf = bttv_g_fbuf,
3193 .vidioc_s_fbuf = bttv_s_fbuf,
3194 .vidioc_overlay = bttv_overlay,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003195 .vidioc_g_parm = bttv_g_parm,
3196 .vidioc_g_frequency = bttv_g_frequency,
3197 .vidioc_s_frequency = bttv_s_frequency,
3198 .vidioc_log_status = bttv_log_status,
3199 .vidioc_querystd = bttv_querystd,
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003200 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
3201 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Hans Verkuil1b9e94d2012-09-09 09:23:31 -03003202 .vidioc_g_chip_ident = bttv_g_chip_ident,
Zoltan Devai43846832008-01-14 13:24:38 -03003203#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003204 .vidioc_g_register = bttv_g_register,
3205 .vidioc_s_register = bttv_s_register,
Zoltan Devai43846832008-01-14 13:24:38 -03003206#endif
Hans Verkuila3998102008-07-21 02:57:38 -03003207};
3208
3209static struct video_device bttv_video_template = {
3210 .fops = &bttv_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003211 .ioctl_ops = &bttv_ioctl_ops,
3212 .tvnorms = BTTV_NORMS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213};
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215/* ----------------------------------------------------------------------- */
3216/* radio interface */
3217
Hans Verkuilbec43662008-12-30 06:58:20 -03003218static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003220 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003221 struct bttv *btv = video_drvdata(file);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003222 struct bttv_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Joe Perches8af443e2011-08-21 19:56:48 -03003224 dprintk("open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Joe Perches8af443e2011-08-21 19:56:48 -03003226 dprintk("%d: open called (radio)\n", btv->c.nr);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003227
3228 /* allocate per filehandle data */
3229 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003230 if (unlikely(!fh))
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003231 return -ENOMEM;
3232 file->private_data = fh;
3233 *fh = btv->init;
Hans Verkuil8c14cc12013-02-06 12:42:40 -03003234 v4l2_fh_init(&fh->fh, vdev);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 btv->radio_user++;
Frank Schaefer0757f5c2013-03-21 13:51:20 -03003237 audio_mute(btv, btv->mute);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003238
Hans Verkuil8c14cc12013-02-06 12:42:40 -03003239 v4l2_fh_add(&fh->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
Hans Verkuilbec43662008-12-30 06:58:20 -03003244static int radio_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003246 struct bttv_fh *fh = file->private_data;
3247 struct bttv *btv = fh->btv;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003248 struct saa6588_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Robert Fitzsimonsb9bc07a2008-04-10 09:40:31 -03003250 file->private_data = NULL;
Hans Verkuil8c14cc12013-02-06 12:42:40 -03003251 v4l2_fh_del(&fh->fh);
3252 v4l2_fh_exit(&fh->fh);
Robert Fitzsimonsb9bc07a2008-04-10 09:40:31 -03003253 kfree(fh);
3254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 btv->radio_user--;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003256
Hans Verkuilb9218f22010-12-27 12:22:46 -03003257 bttv_call_all(btv, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003258
Hans Verkuilb8e2a362013-02-10 09:24:14 -03003259 if (btv->radio_user == 0)
3260 btv->has_radio_tuner = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 return 0;
3262}
3263
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003264static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003266 struct bttv_fh *fh = priv;
3267 struct bttv *btv = fh->btv;
3268
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003269 if (0 != t->index)
3270 return -EINVAL;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003271 strcpy(t->name, "Radio");
3272 t->type = V4L2_TUNER_RADIO;
Hans Verkuilb8e2a362013-02-10 09:24:14 -03003273 radio_enable(btv);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003274
Hans Verkuil859f0272009-03-28 08:29:00 -03003275 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003276
3277 if (btv->audio_mode_gpio)
3278 btv->audio_mode_gpio(btv, t, 0);
3279
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003280 return 0;
3281}
3282
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003283static int radio_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03003284 const struct v4l2_tuner *t)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003285{
3286 struct bttv_fh *fh = priv;
3287 struct bttv *btv = fh->btv;
3288
3289 if (0 != t->index)
3290 return -EINVAL;
3291
Hans Verkuilb8e2a362013-02-10 09:24:14 -03003292 radio_enable(btv);
Hans Verkuila024c1a2011-06-12 07:02:43 -03003293 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003294 return 0;
3295}
3296
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003297static ssize_t radio_read(struct file *file, char __user *data,
3298 size_t count, loff_t *ppos)
3299{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003300 struct bttv_fh *fh = file->private_data;
3301 struct bttv *btv = fh->btv;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003302 struct saa6588_command cmd;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003303 cmd.block_count = count/3;
3304 cmd.buffer = data;
3305 cmd.instance = file;
3306 cmd.result = -ENODEV;
Hans Verkuilb8e2a362013-02-10 09:24:14 -03003307 radio_enable(btv);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003308
Hans Verkuilb9218f22010-12-27 12:22:46 -03003309 bttv_call_all(btv, core, ioctl, SAA6588_CMD_READ, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003310
3311 return cmd.result;
3312}
3313
3314static unsigned int radio_poll(struct file *file, poll_table *wait)
3315{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003316 struct bttv_fh *fh = file->private_data;
3317 struct bttv *btv = fh->btv;
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003318 unsigned long req_events = poll_requested_events(wait);
Hans Verkuilb9218f22010-12-27 12:22:46 -03003319 struct saa6588_command cmd;
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003320 unsigned int res = 0;
3321
3322 if (v4l2_event_pending(&fh->fh))
3323 res = POLLPRI;
3324 else if (req_events & POLLPRI)
3325 poll_wait(file, &fh->fh.wait, wait);
Hans Verkuilb8e2a362013-02-10 09:24:14 -03003326 radio_enable(btv);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003327 cmd.instance = file;
3328 cmd.event_list = wait;
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003329 cmd.result = res;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003330 bttv_call_all(btv, core, ioctl, SAA6588_CMD_POLL, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003331
3332 return cmd.result;
3333}
3334
Hans Verkuilbec43662008-12-30 06:58:20 -03003335static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
3337 .owner = THIS_MODULE,
3338 .open = radio_open,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003339 .read = radio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 .release = radio_release,
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003341 .unlocked_ioctl = video_ioctl2,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003342 .poll = radio_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343};
3344
Hans Verkuila3998102008-07-21 02:57:38 -03003345static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil78dea1a2012-09-09 09:03:29 -03003346 .vidioc_querycap = bttv_querycap,
Hans Verkuil01df5302013-02-06 12:40:28 -03003347 .vidioc_log_status = bttv_log_status,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003348 .vidioc_g_tuner = radio_g_tuner,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003349 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003350 .vidioc_g_frequency = bttv_g_frequency,
3351 .vidioc_s_frequency = bttv_s_frequency,
Hans Verkuilae50f0f2013-02-06 12:42:13 -03003352 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
3353 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354};
3355
Hans Verkuila3998102008-07-21 02:57:38 -03003356static struct video_device radio_template = {
3357 .fops = &radio_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003358 .ioctl_ops = &radio_ioctl_ops,
3359};
3360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361/* ----------------------------------------------------------------------- */
3362/* some debug code */
3363
Adrian Bunk408b6642005-05-01 08:59:29 -07003364static int bttv_risc_decode(u32 risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 static char *instr[16] = {
3367 [ BT848_RISC_WRITE >> 28 ] = "write",
3368 [ BT848_RISC_SKIP >> 28 ] = "skip",
3369 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3370 [ BT848_RISC_JUMP >> 28 ] = "jump",
3371 [ BT848_RISC_SYNC >> 28 ] = "sync",
3372 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3373 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3374 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3375 };
3376 static int incr[16] = {
3377 [ BT848_RISC_WRITE >> 28 ] = 2,
3378 [ BT848_RISC_JUMP >> 28 ] = 2,
3379 [ BT848_RISC_SYNC >> 28 ] = 2,
3380 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3381 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3382 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3383 };
3384 static char *bits[] = {
3385 "be0", "be1", "be2", "be3/resync",
3386 "set0", "set1", "set2", "set3",
3387 "clr0", "clr1", "clr2", "clr3",
3388 "irq", "res", "eol", "sol",
3389 };
3390 int i;
3391
Joe Perches8af443e2011-08-21 19:56:48 -03003392 pr_cont("0x%08x [ %s", risc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3394 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3395 if (risc & (1 << (i + 12)))
Joe Perches8af443e2011-08-21 19:56:48 -03003396 pr_cont(" %s", bits[i]);
3397 pr_cont(" count=%d ]\n", risc & 0xfff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3399}
3400
Adrian Bunk408b6642005-05-01 08:59:29 -07003401static void bttv_risc_disasm(struct bttv *btv,
3402 struct btcx_riscmem *risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
3404 unsigned int i,j,n;
3405
Joe Perches8af443e2011-08-21 19:56:48 -03003406 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
3407 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 for (i = 0; i < (risc->size >> 2); i += n) {
Joe Perches8af443e2011-08-21 19:56:48 -03003409 pr_info("%s: 0x%lx: ",
3410 btv->c.v4l2_dev.name,
3411 (unsigned long)(risc->dma + (i<<2)));
Al Viro3aa71102008-06-22 14:20:09 -03003412 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 for (j = 1; j < n; j++)
Joe Perches8af443e2011-08-21 19:56:48 -03003414 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3415 btv->c.v4l2_dev.name,
3416 (unsigned long)(risc->dma + ((i+j)<<2)),
3417 risc->cpu[i+j], j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 if (0 == risc->cpu[i])
3419 break;
3420 }
3421}
3422
3423static void bttv_print_riscaddr(struct bttv *btv)
3424{
Joe Perches8af443e2011-08-21 19:56:48 -03003425 pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
3426 pr_info(" vbi : o=%08llx e=%08llx\n",
3427 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3428 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3429 pr_info(" cap : o=%08llx e=%08llx\n",
3430 btv->curr.top
3431 ? (unsigned long long)btv->curr.top->top.dma : 0,
3432 btv->curr.bottom
3433 ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3434 pr_info(" scr : o=%08llx e=%08llx\n",
3435 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3436 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 bttv_risc_disasm(btv, &btv->main);
3438}
3439
3440/* ----------------------------------------------------------------------- */
3441/* irq handler */
3442
3443static char *irq_name[] = {
3444 "FMTCHG", // format change detected (525 vs. 625)
3445 "VSYNC", // vertical sync (new field)
3446 "HSYNC", // horizontal sync
3447 "OFLOW", // chroma/luma AGC overflow
3448 "HLOCK", // horizontal lock changed
3449 "VPRES", // video presence changed
3450 "6", "7",
3451 "I2CDONE", // hw irc operation finished
3452 "GPINT", // gpio port triggered irq
3453 "10",
3454 "RISCI", // risc instruction triggered irq
3455 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3456 "FTRGT", // pixel data fifo overrun
3457 "FDSR", // fifo data stream resyncronisation
3458 "PPERR", // parity error (data transfer)
3459 "RIPERR", // parity error (read risc instructions)
3460 "PABORT", // pci abort
3461 "OCERR", // risc instruction error
3462 "SCERR", // syncronisation error
3463};
3464
3465static void bttv_print_irqbits(u32 print, u32 mark)
3466{
3467 unsigned int i;
3468
Joe Perches8af443e2011-08-21 19:56:48 -03003469 pr_cont("bits:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3471 if (print & (1 << i))
Joe Perches8af443e2011-08-21 19:56:48 -03003472 pr_cont(" %s", irq_name[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 if (mark & (1 << i))
Joe Perches8af443e2011-08-21 19:56:48 -03003474 pr_cont("*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 }
3476}
3477
3478static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3479{
Joe Perches8af443e2011-08-21 19:56:48 -03003480 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3481 btv->c.nr,
3482 (unsigned long)btv->main.dma,
3483 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3484 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3485 (unsigned long)rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
3487 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
Joe Perches8af443e2011-08-21 19:56:48 -03003488 pr_notice("%d: Oh, there (temporarily?) is no input signal. "
3489 "Ok, then this is harmless, don't worry ;)\n",
3490 btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 return;
3492 }
Joe Perches8af443e2011-08-21 19:56:48 -03003493 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
3494 btv->c.nr);
3495 pr_notice("%d: Lets try to catch the culpit red-handed ...\n",
3496 btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 dump_stack();
3498}
3499
3500static int
3501bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3502{
3503 struct bttv_buffer *item;
3504
3505 memset(set,0,sizeof(*set));
3506
3507 /* capture request ? */
3508 if (!list_empty(&btv->capture)) {
3509 set->frame_irq = 1;
3510 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3511 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3512 set->top = item;
3513 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3514 set->bottom = item;
3515
3516 /* capture request for other field ? */
3517 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3518 (item->vb.queue.next != &btv->capture)) {
3519 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
Mike Isely66349b42009-09-21 12:09:08 -03003520 /* Mike Isely <isely@pobox.com> - Only check
3521 * and set up the bottom field in the logic
3522 * below. Don't ever do the top field. This
3523 * of course means that if we set up the
3524 * bottom field in the above code that we'll
3525 * actually skip a field. But that's OK.
3526 * Having processed only a single buffer this
3527 * time, then the next time around the first
3528 * available buffer should be for a top field.
3529 * That will then cause us here to set up a
3530 * top then a bottom field in the normal way.
3531 * The alternative to this understanding is
3532 * that we set up the second available buffer
3533 * as a top field, but that's out of order
3534 * since this driver always processes the top
3535 * field first - the effect will be the two
3536 * buffers being returned in the wrong order,
3537 * with the second buffer also being delayed
3538 * by one field time (owing to the fifo nature
3539 * of videobuf). Worse still, we'll be stuck
3540 * doing fields out of order now every time
3541 * until something else causes a field to be
3542 * dropped. By effectively forcing a field to
3543 * drop this way then we always get back into
3544 * sync within a single frame time. (Out of
3545 * order fields can screw up deinterlacing
3546 * algorithms.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 if (NULL == set->bottom &&
3549 V4L2_FIELD_BOTTOM == item->vb.field) {
3550 set->bottom = item;
3551 }
3552 if (NULL != set->top && NULL != set->bottom)
3553 set->top_irq = 2;
3554 }
3555 }
3556 }
3557
3558 /* screen overlay ? */
3559 if (NULL != btv->screen) {
3560 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3561 if (NULL == set->top && NULL == set->bottom) {
3562 set->top = btv->screen;
3563 set->bottom = btv->screen;
3564 }
3565 } else {
3566 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3567 NULL == set->top) {
3568 set->top = btv->screen;
3569 }
3570 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3571 NULL == set->bottom) {
3572 set->bottom = btv->screen;
3573 }
3574 }
3575 }
3576
Joe Perches8af443e2011-08-21 19:56:48 -03003577 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3578 btv->c.nr, set->top, set->bottom,
3579 btv->screen, set->frame_irq, set->top_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 return 0;
3581}
3582
3583static void
3584bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3585 struct bttv_buffer_set *curr, unsigned int state)
3586{
3587 struct timeval ts;
3588
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003589 v4l2_get_timestamp(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
3591 if (wakeup->top == wakeup->bottom) {
3592 if (NULL != wakeup->top && curr->top != wakeup->top) {
3593 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003594 pr_debug("%d: wakeup: both=%p\n",
3595 btv->c.nr, wakeup->top);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 wakeup->top->vb.ts = ts;
3597 wakeup->top->vb.field_count = btv->field_count;
3598 wakeup->top->vb.state = state;
3599 wake_up(&wakeup->top->vb.done);
3600 }
3601 } else {
3602 if (NULL != wakeup->top && curr->top != wakeup->top) {
3603 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003604 pr_debug("%d: wakeup: top=%p\n",
3605 btv->c.nr, wakeup->top);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 wakeup->top->vb.ts = ts;
3607 wakeup->top->vb.field_count = btv->field_count;
3608 wakeup->top->vb.state = state;
3609 wake_up(&wakeup->top->vb.done);
3610 }
3611 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3612 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003613 pr_debug("%d: wakeup: bottom=%p\n",
3614 btv->c.nr, wakeup->bottom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 wakeup->bottom->vb.ts = ts;
3616 wakeup->bottom->vb.field_count = btv->field_count;
3617 wakeup->bottom->vb.state = state;
3618 wake_up(&wakeup->bottom->vb.done);
3619 }
3620 }
3621}
3622
3623static void
3624bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3625 unsigned int state)
3626{
3627 struct timeval ts;
3628
3629 if (NULL == wakeup)
3630 return;
3631
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003632 v4l2_get_timestamp(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 wakeup->vb.ts = ts;
3634 wakeup->vb.field_count = btv->field_count;
3635 wakeup->vb.state = state;
3636 wake_up(&wakeup->vb.done);
3637}
3638
3639static void bttv_irq_timeout(unsigned long data)
3640{
3641 struct bttv *btv = (struct bttv *)data;
3642 struct bttv_buffer_set old,new;
3643 struct bttv_buffer *ovbi;
3644 struct bttv_buffer *item;
3645 unsigned long flags;
3646
3647 if (bttv_verbose) {
Joe Perches8af443e2011-08-21 19:56:48 -03003648 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3649 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3650 btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 bttv_print_irqbits(btread(BT848_INT_STAT),0);
Joe Perches8af443e2011-08-21 19:56:48 -03003652 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 }
3654
3655 spin_lock_irqsave(&btv->s_lock,flags);
3656
3657 /* deactivate stuff */
3658 memset(&new,0,sizeof(new));
3659 old = btv->curr;
3660 ovbi = btv->cvbi;
3661 btv->curr = new;
3662 btv->cvbi = NULL;
3663 btv->loop_irq = 0;
3664 bttv_buffer_activate_video(btv, &new);
3665 bttv_buffer_activate_vbi(btv, NULL);
3666 bttv_set_dma(btv, 0);
3667
3668 /* wake up */
Brandon Philips0fc06862007-11-06 20:02:36 -03003669 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3670 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
3672 /* cancel all outstanding capture / vbi requests */
3673 while (!list_empty(&btv->capture)) {
3674 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3675 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003676 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 wake_up(&item->vb.done);
3678 }
3679 while (!list_empty(&btv->vcapture)) {
3680 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3681 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003682 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 wake_up(&item->vb.done);
3684 }
3685
3686 btv->errors++;
3687 spin_unlock_irqrestore(&btv->s_lock,flags);
3688}
3689
3690static void
3691bttv_irq_wakeup_top(struct bttv *btv)
3692{
3693 struct bttv_buffer *wakeup = btv->curr.top;
3694
3695 if (NULL == wakeup)
3696 return;
3697
3698 spin_lock(&btv->s_lock);
3699 btv->curr.top_irq = 0;
3700 btv->curr.top = NULL;
3701 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3702
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003703 v4l2_get_timestamp(&wakeup->vb.ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 wakeup->vb.field_count = btv->field_count;
Brandon Philips0fc06862007-11-06 20:02:36 -03003705 wakeup->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 wake_up(&wakeup->vb.done);
3707 spin_unlock(&btv->s_lock);
3708}
3709
3710static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3711{
3712 if (rc < risc->dma)
3713 return 0;
3714 if (rc > risc->dma + risc->size)
3715 return 0;
3716 return 1;
3717}
3718
3719static void
3720bttv_irq_switch_video(struct bttv *btv)
3721{
3722 struct bttv_buffer_set new;
3723 struct bttv_buffer_set old;
3724 dma_addr_t rc;
3725
3726 spin_lock(&btv->s_lock);
3727
3728 /* new buffer set */
3729 bttv_irq_next_video(btv, &new);
3730 rc = btread(BT848_RISC_COUNT);
3731 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3732 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3733 btv->framedrop++;
3734 if (debug_latency)
3735 bttv_irq_debug_low_latency(btv, rc);
3736 spin_unlock(&btv->s_lock);
3737 return;
3738 }
3739
3740 /* switch over */
3741 old = btv->curr;
3742 btv->curr = new;
3743 btv->loop_irq &= ~1;
3744 bttv_buffer_activate_video(btv, &new);
3745 bttv_set_dma(btv, 0);
3746
3747 /* switch input */
3748 if (UNSET != btv->new_input) {
3749 video_mux(btv,btv->new_input);
3750 btv->new_input = UNSET;
3751 }
3752
3753 /* wake up finished buffers */
Brandon Philips0fc06862007-11-06 20:02:36 -03003754 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 spin_unlock(&btv->s_lock);
3756}
3757
3758static void
3759bttv_irq_switch_vbi(struct bttv *btv)
3760{
3761 struct bttv_buffer *new = NULL;
3762 struct bttv_buffer *old;
3763 u32 rc;
3764
3765 spin_lock(&btv->s_lock);
3766
3767 if (!list_empty(&btv->vcapture))
3768 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3769 old = btv->cvbi;
3770
3771 rc = btread(BT848_RISC_COUNT);
3772 if (NULL != old && (is_active(&old->top, rc) ||
3773 is_active(&old->bottom, rc))) {
3774 btv->framedrop++;
3775 if (debug_latency)
3776 bttv_irq_debug_low_latency(btv, rc);
3777 spin_unlock(&btv->s_lock);
3778 return;
3779 }
3780
3781 /* switch */
3782 btv->cvbi = new;
3783 btv->loop_irq &= ~4;
3784 bttv_buffer_activate_vbi(btv, new);
3785 bttv_set_dma(btv, 0);
3786
Brandon Philips0fc06862007-11-06 20:02:36 -03003787 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 spin_unlock(&btv->s_lock);
3789}
3790
David Howells7d12e782006-10-05 14:55:46 +01003791static irqreturn_t bttv_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792{
3793 u32 stat,astat;
3794 u32 dstat;
3795 int count;
3796 struct bttv *btv;
3797 int handled = 0;
3798
3799 btv=(struct bttv *)dev_id;
Mark Weaver6c6c0b22005-11-13 16:07:52 -08003800
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 count=0;
3802 while (1) {
3803 /* get/clear interrupt status bits */
3804 stat=btread(BT848_INT_STAT);
3805 astat=stat&btread(BT848_INT_MASK);
3806 if (!astat)
3807 break;
3808 handled = 1;
3809 btwrite(stat,BT848_INT_STAT);
3810
3811 /* get device status bits */
3812 dstat=btread(BT848_DSTATUS);
3813
3814 if (irq_debug) {
Joe Perches8af443e2011-08-21 19:56:48 -03003815 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
3816 btv->c.nr, count, btv->field_count,
3817 stat>>28, btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 bttv_print_irqbits(stat,astat);
3819 if (stat & BT848_INT_HLOCK)
Joe Perches8af443e2011-08-21 19:56:48 -03003820 pr_cont(" HLOC => %s",
3821 dstat & BT848_DSTATUS_HLOC
3822 ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 if (stat & BT848_INT_VPRES)
Joe Perches8af443e2011-08-21 19:56:48 -03003824 pr_cont(" PRES => %s",
3825 dstat & BT848_DSTATUS_PRES
3826 ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 if (stat & BT848_INT_FMTCHG)
Joe Perches8af443e2011-08-21 19:56:48 -03003828 pr_cont(" NUML => %s",
3829 dstat & BT848_DSTATUS_NUML
3830 ? "625" : "525");
3831 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 }
3833
3834 if (astat&BT848_INT_VSYNC)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003835 btv->field_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02003837 if ((astat & BT848_INT_GPINT) && btv->remote) {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02003838 bttv_input_irq(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 }
3840
3841 if (astat & BT848_INT_I2CDONE) {
3842 btv->i2c_done = stat;
3843 wake_up(&btv->i2c_queue);
3844 }
3845
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003846 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 bttv_irq_switch_vbi(btv);
3848
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003849 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 bttv_irq_wakeup_top(btv);
3851
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003852 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 bttv_irq_switch_video(btv);
3854
3855 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
Frank Schaefer5f4567812013-03-21 13:51:17 -03003856 /* trigger automute */
3857 audio_mux_gpio(btv, btv->audio_input, btv->mute);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
3859 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
Joe Perches8af443e2011-08-21 19:56:48 -03003860 pr_info("%d: %s%s @ %08x,",
3861 btv->c.nr,
3862 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3863 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3864 btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 bttv_print_irqbits(stat,astat);
Joe Perches8af443e2011-08-21 19:56:48 -03003866 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 if (bttv_debug)
3868 bttv_print_riscaddr(btv);
3869 }
3870 if (fdsr && astat & BT848_INT_FDSR) {
Joe Perches8af443e2011-08-21 19:56:48 -03003871 pr_info("%d: FDSR @ %08x\n",
3872 btv->c.nr, btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 if (bttv_debug)
3874 bttv_print_riscaddr(btv);
3875 }
3876
3877 count++;
3878 if (count > 4) {
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08003879
3880 if (count > 8 || !(astat & BT848_INT_GPINT)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003881 btwrite(0, BT848_INT_MASK);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08003882
Joe Perches8af443e2011-08-21 19:56:48 -03003883 pr_err("%d: IRQ lockup, cleared int mask [",
3884 btv->c.nr);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08003885 } else {
Joe Perches8af443e2011-08-21 19:56:48 -03003886 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
3887 btv->c.nr);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08003888
3889 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3890 BT848_INT_MASK);
3891 };
3892
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 bttv_print_irqbits(stat,astat);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08003894
Joe Perches8af443e2011-08-21 19:56:48 -03003895 pr_cont("]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 }
3897 }
3898 btv->irq_total++;
3899 if (handled)
3900 btv->irq_me++;
3901 return IRQ_RETVAL(handled);
3902}
3903
3904
3905/* ----------------------------------------------------------------------- */
Hans Verkuild9b67072013-02-06 11:43:07 -03003906/* initialization */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
3908static struct video_device *vdev_init(struct bttv *btv,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03003909 const struct video_device *template,
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03003910 const char *type_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911{
3912 struct video_device *vfd;
3913
3914 vfd = video_device_alloc();
3915 if (NULL == vfd)
3916 return NULL;
3917 *vfd = *template;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003918 vfd->v4l2_dev = &btv->c.v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 vfd->release = video_device_release;
Mauro Carvalho Chehab1d0a4362008-06-23 12:31:29 -03003920 vfd->debug = bttv_debug;
Hans Verkuil8c14cc12013-02-06 12:42:40 -03003921 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003922 video_set_drvdata(vfd, btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3924 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03003925 type_name, bttv_tvcards[btv->c.type].name);
Hans Verkuild9b67072013-02-06 11:43:07 -03003926 if (btv->tuner_type == TUNER_ABSENT) {
3927 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
3928 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
3929 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
3930 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
3931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 return vfd;
3933}
3934
3935static void bttv_unregister_video(struct bttv *btv)
3936{
3937 if (btv->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03003938 if (video_is_registered(btv->video_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 video_unregister_device(btv->video_dev);
3940 else
3941 video_device_release(btv->video_dev);
3942 btv->video_dev = NULL;
3943 }
3944 if (btv->vbi_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03003945 if (video_is_registered(btv->vbi_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 video_unregister_device(btv->vbi_dev);
3947 else
3948 video_device_release(btv->vbi_dev);
3949 btv->vbi_dev = NULL;
3950 }
3951 if (btv->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03003952 if (video_is_registered(btv->radio_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 video_unregister_device(btv->radio_dev);
3954 else
3955 video_device_release(btv->radio_dev);
3956 btv->radio_dev = NULL;
3957 }
3958}
3959
3960/* register video4linux devices */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003961static int bttv_register_video(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962{
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03003963 if (no_overlay > 0)
Joe Perches8af443e2011-08-21 19:56:48 -03003964 pr_notice("Overlay support disabled\n");
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07003965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 /* video */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03003967 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03003968
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003969 if (NULL == btv->video_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03003971 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
3972 video_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03003974 pr_info("%d: registered device %s\n",
3975 btv->c.nr, video_device_node_name(btv->video_dev));
Hans Verkuil22a04f12008-07-20 06:35:02 -03003976 if (device_create_file(&btv->video_dev->dev,
Kay Sievers54bd5b62007-10-08 16:26:13 -03003977 &dev_attr_card)<0) {
Joe Perches8af443e2011-08-21 19:56:48 -03003978 pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
Trent Piephod94fc9a2006-07-29 17:18:06 -03003979 goto err;
3980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
3982 /* vbi */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03003983 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03003984
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003985 if (NULL == btv->vbi_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03003987 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
3988 vbi_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03003990 pr_info("%d: registered device %s\n",
3991 btv->c.nr, video_device_node_name(btv->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003993 if (!btv->has_radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 return 0;
3995 /* radio */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03003996 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003997 if (NULL == btv->radio_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 goto err;
Hans Verkuil01df5302013-02-06 12:40:28 -03003999 btv->radio_dev->ctrl_handler = &btv->radio_ctrl_handler;
Jean Delvare176c2f32008-09-07 12:49:59 -03004000 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4001 radio_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03004003 pr_info("%d: registered device %s\n",
4004 btv->c.nr, video_device_node_name(btv->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
4006 /* all done */
4007 return 0;
4008
4009 err:
4010 bttv_unregister_video(btv);
4011 return -1;
4012}
4013
4014
4015/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4016/* response on cards with no firmware is not enabled by OF */
4017static void pci_set_command(struct pci_dev *dev)
4018{
4019#if defined(__powerpc__)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004020 unsigned int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004022 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4023 cmd = (cmd | PCI_COMMAND_MEMORY );
4024 pci_write_config_dword(dev, PCI_COMMAND, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025#endif
4026}
4027
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004028static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029{
Hans Verkuil76ea9922013-02-06 11:49:14 -03004030 struct v4l2_frequency init_freq = {
4031 .tuner = 0,
4032 .type = V4L2_TUNER_ANALOG_TV,
4033 .frequency = 980,
4034 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 int result;
4036 unsigned char lat;
4037 struct bttv *btv;
Hans Verkuil01df5302013-02-06 12:40:28 -03004038 struct v4l2_ctrl_handler *hdl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
4040 if (bttv_num == BTTV_MAX)
4041 return -ENOMEM;
Joe Perches8af443e2011-08-21 19:56:48 -03004042 pr_info("Bt8xx card found (%d)\n", bttv_num);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004043 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004044 if (btv == NULL) {
Joe Perches8af443e2011-08-21 19:56:48 -03004045 pr_err("out of memory\n");
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004046 return -ENOMEM;
4047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 btv->c.nr = bttv_num;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004049 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4050 "bttv%d", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
4052 /* initialize structs / fill in defaults */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02004053 mutex_init(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004054 spin_lock_init(&btv->s_lock);
4055 spin_lock_init(&btv->gpio_lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004056 init_waitqueue_head(&btv->i2c_queue);
4057 INIT_LIST_HEAD(&btv->c.subs);
4058 INIT_LIST_HEAD(&btv->capture);
4059 INIT_LIST_HEAD(&btv->vcapture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
4061 init_timer(&btv->timeout);
4062 btv->timeout.function = bttv_irq_timeout;
4063 btv->timeout.data = (unsigned long)btv;
4064
Michael Krufky7c08fb02005-11-08 21:36:21 -08004065 btv->i2c_rc = -1;
4066 btv->tuner_type = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 btv->new_input = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 btv->has_radio=radio[btv->c.nr];
4069
4070 /* pci stuff (init, get irq/mmio, ... */
4071 btv->c.pci = dev;
Michael Krufky7c08fb02005-11-08 21:36:21 -08004072 btv->id = dev->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 if (pci_enable_device(dev)) {
Joe Perches8af443e2011-08-21 19:56:48 -03004074 pr_warn("%d: Can't enable device\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 return -EIO;
4076 }
Yang Hongyang284901a2009-04-06 19:01:15 -07004077 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Joe Perches8af443e2011-08-21 19:56:48 -03004078 pr_warn("%d: No suitable DMA available\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 return -EIO;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 if (!request_mem_region(pci_resource_start(dev,0),
4082 pci_resource_len(dev,0),
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004083 btv->c.v4l2_dev.name)) {
Joe Perches8af443e2011-08-21 19:56:48 -03004084 pr_warn("%d: can't request iomem (0x%llx)\n",
4085 btv->c.nr,
4086 (unsigned long long)pci_resource_start(dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 return -EBUSY;
4088 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004089 pci_set_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 pci_set_command(dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004091
4092 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4093 if (result < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004094 pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004095 goto fail0;
4096 }
Hans Verkuil01df5302013-02-06 12:40:28 -03004097 hdl = &btv->ctrl_handler;
4098 v4l2_ctrl_handler_init(hdl, 20);
4099 btv->c.v4l2_dev.ctrl_handler = hdl;
4100 v4l2_ctrl_handler_init(&btv->radio_ctrl_handler, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
Bjørn Morkabd34d82011-03-21 11:35:56 -03004102 btv->revision = dev->revision;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004103 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
Joe Perches8af443e2011-08-21 19:56:48 -03004104 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
4105 bttv_num, btv->id, btv->revision, pci_name(dev),
4106 btv->c.pci->irq, lat,
4107 (unsigned long long)pci_resource_start(dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 schedule();
4109
Akinobu Mita5f1693f2006-12-20 10:08:56 -03004110 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4111 if (NULL == btv->bt848_mmio) {
Joe Perches8af443e2011-08-21 19:56:48 -03004112 pr_err("%d: ioremap() failed\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 result = -EIO;
4114 goto fail1;
4115 }
4116
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004117 /* identify card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 bttv_idcard(btv);
4119
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004120 /* disable irqs, register irq handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 btwrite(0, BT848_INT_MASK);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004122 result = request_irq(btv->c.pci->irq, bttv_irq,
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004123 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004124 if (result < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004125 pr_err("%d: can't get IRQ %d\n",
4126 bttv_num, btv->c.pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 goto fail1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
4130 if (0 != bttv_handle_chipset(btv)) {
4131 result = -EIO;
4132 goto fail2;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
4135 /* init options from insmod args */
4136 btv->opt_combfilter = combfilter;
Hans Verkuil01df5302013-02-06 12:40:28 -03004137 bttv_ctrl_combfilter.def = combfilter;
4138 bttv_ctrl_lumafilter.def = lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 btv->opt_automute = automute;
Hans Verkuil01df5302013-02-06 12:40:28 -03004140 bttv_ctrl_automute.def = automute;
4141 bttv_ctrl_agc_crush.def = agc_crush;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 btv->opt_vcr_hack = vcr_hack;
Hans Verkuil01df5302013-02-06 12:40:28 -03004143 bttv_ctrl_vcr_hack.def = vcr_hack;
4144 bttv_ctrl_whitecrush_upper.def = whitecrush_upper;
4145 bttv_ctrl_whitecrush_lower.def = whitecrush_lower;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07004146 btv->opt_uv_ratio = uv_ratio;
Hans Verkuil01df5302013-02-06 12:40:28 -03004147 bttv_ctrl_uv_ratio.def = uv_ratio;
4148 bttv_ctrl_full_luma.def = full_luma_range;
4149 bttv_ctrl_coring.def = coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
4151 /* fill struct bttv with some useful defaults */
4152 btv->init.btv = btv;
4153 btv->init.ov.w.width = 320;
4154 btv->init.ov.w.height = 240;
Mauro Carvalho Chehabc96dd072007-10-26 16:51:47 -03004155 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 btv->init.width = 320;
4157 btv->init.height = 240;
Hans Verkuilc13eb7032013-02-07 07:56:11 -03004158 btv->init.ov.w.width = 320;
4159 btv->init.ov.w.height = 240;
4160 btv->init.ov.field = V4L2_FIELD_INTERLACED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 btv->input = 0;
4162
Hans Verkuil01df5302013-02-06 12:40:28 -03004163 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4164 V4L2_CID_BRIGHTNESS, 0, 0xff00, 0x100, 32768);
4165 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4166 V4L2_CID_CONTRAST, 0, 0xff80, 0x80, 0x6c00);
4167 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4168 V4L2_CID_SATURATION, 0, 0xff80, 0x80, 32768);
4169 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4170 V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
4171 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4172 V4L2_CID_HUE, 0, 0xff00, 0x100, 32768);
4173 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4174 V4L2_CID_CHROMA_AGC, 0, 1, 1, !!chroma_agc);
4175 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4176 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
4177 if (btv->volume_gpio)
4178 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4179 V4L2_CID_AUDIO_VOLUME, 0, 0xff00, 0x100, 0xff00);
4180 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_combfilter, NULL);
4181 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_automute, NULL);
4182 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_lumafilter, NULL);
4183 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_agc_crush, NULL);
4184 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_vcr_hack, NULL);
4185 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_lower, NULL);
4186 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_upper, NULL);
4187 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_uv_ratio, NULL);
4188 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_full_luma, NULL);
4189 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_coring, NULL);
4190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 /* initialize hardware */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004192 if (bttv_gpio)
4193 bttv_gpio_tracking(btv,"pre-init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
4195 bttv_risc_init_main(btv);
4196 init_bt848(btv);
4197
4198 /* gpio */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004199 btwrite(0x00, BT848_GPIO_REG_INP);
4200 btwrite(0x00, BT848_GPIO_OUT_EN);
4201 if (bttv_verbose)
4202 bttv_gpio_tracking(btv,"init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004204 /* needs to be done before i2c is registered */
4205 bttv_init_card1(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004207 /* register i2c + gpio */
4208 init_bttv_i2c(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004210 /* some card-specific stuff (needs working i2c) */
4211 bttv_init_card2(btv);
Hans Verkuil2c905772009-07-20 08:14:17 -03004212 bttv_init_tuner(btv);
Hans Verkuil76ea9922013-02-06 11:49:14 -03004213 if (btv->tuner_type != TUNER_ABSENT) {
4214 bttv_set_frequency(btv, &init_freq);
4215 btv->radio_freq = 90500 * 16; /* 90.5Mhz default */
4216 }
Hans Verkuil6795cc52013-02-06 12:43:07 -03004217 btv->std = V4L2_STD_PAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 init_irqreg(btv);
Hans Verkuil01df5302013-02-06 12:40:28 -03004219 v4l2_ctrl_handler_setup(hdl);
Hans Verkuil01df5302013-02-06 12:40:28 -03004220 if (hdl->error) {
4221 result = hdl->error;
4222 goto fail2;
4223 }
Frank Schaeferda9f07f2013-03-21 13:51:19 -03004224 /* mute device */
4225 audio_mute(btv, 1);
4226
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004227 /* register video4linux + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 if (!bttv_tvcards[btv->c.type].no_video) {
Hans Verkuil01df5302013-02-06 12:40:28 -03004229 v4l2_ctrl_add_handler(&btv->radio_ctrl_handler, hdl,
4230 v4l2_ctrl_radio_filter);
4231 if (btv->radio_ctrl_handler.error) {
4232 result = btv->radio_ctrl_handler.error;
4233 goto fail2;
4234 }
Trent Piepho333408f2007-07-03 15:08:10 -03004235 set_input(btv, 0, btv->tvnorm);
Michael Schimeke5bd0262007-01-18 16:17:39 -03004236 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4237 btv->crop[1] = btv->crop[0]; /* current = default */
4238 disclaim_vbi_lines(btv);
4239 disclaim_video_lines(btv);
Hans Verkuil01df5302013-02-06 12:40:28 -03004240 bttv_register_video(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 }
4242
Jarod Wilsonf992a492007-03-24 15:23:50 -03004243 /* add subdevices and autoload dvb-bt8xx if needed */
4244 if (bttv_tvcards[btv->c.type].has_dvb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 bttv_sub_add_device(&btv->c, "dvb");
Jarod Wilsonf992a492007-03-24 15:23:50 -03004246 request_modules(btv);
4247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Jean Delvared90a4ae2010-02-16 14:22:37 -03004249 if (!disable_ir) {
4250 init_bttv_i2c_ir(btv);
4251 bttv_input_init(btv);
4252 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004253
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 /* everything is fine */
4255 bttv_num++;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004256 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004258fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004259 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004261fail1:
Hans Verkuil01df5302013-02-06 12:40:28 -03004262 v4l2_ctrl_handler_free(&btv->ctrl_handler);
4263 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004264 v4l2_device_unregister(&btv->c.v4l2_dev);
4265
4266fail0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 if (btv->bt848_mmio)
4268 iounmap(btv->bt848_mmio);
4269 release_mem_region(pci_resource_start(btv->c.pci,0),
4270 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 return result;
4272}
4273
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004274static void bttv_remove(struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004276 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4277 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
4279 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03004280 pr_info("%d: unloading\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
Tejun Heo707bcf32010-12-24 16:14:20 +01004282 if (bttv_tvcards[btv->c.type].has_dvb)
4283 flush_request_modules(btv);
4284
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004285 /* shutdown everything (DMA+IRQs) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 btand(~15, BT848_GPIO_DMA_CTL);
4287 btwrite(0, BT848_INT_MASK);
4288 btwrite(~0x0, BT848_INT_STAT);
4289 btwrite(0x0, BT848_GPIO_OUT_EN);
4290 if (bttv_gpio)
4291 bttv_gpio_tracking(btv,"cleanup");
4292
4293 /* tell gpio modules we are leaving ... */
4294 btv->shutdown=1;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004295 bttv_input_fini(btv);
Christopher Pascoe889aee82006-01-09 15:25:28 -02004296 bttv_sub_del_devices(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004298 /* unregister i2c_bus + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 fini_bttv_i2c(btv);
4300
4301 /* unregister video4linux */
4302 bttv_unregister_video(btv);
4303
4304 /* free allocated memory */
Hans Verkuil01df5302013-02-06 12:40:28 -03004305 v4l2_ctrl_handler_free(&btv->ctrl_handler);
4306 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 btcx_riscmem_free(btv->c.pci,&btv->main);
4308
Hans Verkuil01df5302013-02-06 12:40:28 -03004309 /* free resources */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004310 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 iounmap(btv->bt848_mmio);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004312 release_mem_region(pci_resource_start(btv->c.pci,0),
4313 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004315 v4l2_device_unregister(&btv->c.v4l2_dev);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004316 bttvs[btv->c.nr] = NULL;
4317 kfree(btv);
4318
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004319 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320}
4321
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004322#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4324{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004325 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4326 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 struct bttv_buffer_set idle;
4328 unsigned long flags;
4329
Joe Perches8af443e2011-08-21 19:56:48 -03004330 dprintk("%d: suspend %d\n", btv->c.nr, state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
4332 /* stop dma + irqs */
4333 spin_lock_irqsave(&btv->s_lock,flags);
4334 memset(&idle, 0, sizeof(idle));
4335 btv->state.video = btv->curr;
4336 btv->state.vbi = btv->cvbi;
4337 btv->state.loop_irq = btv->loop_irq;
4338 btv->curr = idle;
4339 btv->loop_irq = 0;
4340 bttv_buffer_activate_video(btv, &idle);
4341 bttv_buffer_activate_vbi(btv, NULL);
4342 bttv_set_dma(btv, 0);
4343 btwrite(0, BT848_INT_MASK);
4344 spin_unlock_irqrestore(&btv->s_lock,flags);
4345
4346 /* save bt878 state */
4347 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4348 btv->state.gpio_data = gpio_read();
4349
4350 /* save pci state */
4351 pci_save_state(pci_dev);
4352 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4353 pci_disable_device(pci_dev);
4354 btv->state.disabled = 1;
4355 }
4356 return 0;
4357}
4358
4359static int bttv_resume(struct pci_dev *pci_dev)
4360{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004361 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4362 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004364 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
Joe Perches8af443e2011-08-21 19:56:48 -03004366 dprintk("%d: resume\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
4368 /* restore pci state */
4369 if (btv->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004370 err=pci_enable_device(pci_dev);
4371 if (err) {
Joe Perches8af443e2011-08-21 19:56:48 -03004372 pr_warn("%d: Can't enable device\n", btv->c.nr);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004373 return err;
4374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 btv->state.disabled = 0;
4376 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004377 err=pci_set_power_state(pci_dev, PCI_D0);
4378 if (err) {
4379 pci_disable_device(pci_dev);
Joe Perches8af443e2011-08-21 19:56:48 -03004380 pr_warn("%d: Can't enable device\n", btv->c.nr);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004381 btv->state.disabled = 1;
4382 return err;
4383 }
4384
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 pci_restore_state(pci_dev);
4386
4387 /* restore bt878 state */
4388 bttv_reinit_bt848(btv);
4389 gpio_inout(0xffffff, btv->state.gpio_enable);
4390 gpio_write(btv->state.gpio_data);
4391
4392 /* restart dma */
4393 spin_lock_irqsave(&btv->s_lock,flags);
4394 btv->curr = btv->state.video;
4395 btv->cvbi = btv->state.vbi;
4396 btv->loop_irq = btv->state.loop_irq;
4397 bttv_buffer_activate_video(btv, &btv->curr);
4398 bttv_buffer_activate_vbi(btv, btv->cvbi);
4399 bttv_set_dma(btv, 0);
4400 spin_unlock_irqrestore(&btv->s_lock,flags);
4401 return 0;
4402}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004403#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
4405static struct pci_device_id bttv_pci_tbl[] = {
Joe Perches76e97412009-07-05 15:59:21 -03004406 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4407 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4408 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4409 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
Peter De Schrijverc540d9f2011-11-06 10:47:58 -03004410 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004411 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412};
4413
4414MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4415
4416static struct pci_driver bttv_pci_driver = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004417 .name = "bttv",
4418 .id_table = bttv_pci_tbl,
4419 .probe = bttv_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004420 .remove = bttv_remove,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004421#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 .suspend = bttv_suspend,
4423 .resume = bttv_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004424#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425};
4426
Adrian Bunk7d44e892007-12-11 19:23:43 -03004427static int __init bttv_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
Randy Dunlapc526e222006-07-15 09:08:26 -03004429 int ret;
4430
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 bttv_num = 0;
4432
Joe Perches8af443e2011-08-21 19:56:48 -03004433 pr_info("driver version %s loaded\n", BTTV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4435 gbuffers = 2;
Filipe Rossetf41b6962009-05-28 11:11:53 -03004436 if (gbufsize > BTTV_MAX_FBUF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 gbufsize = BTTV_MAX_FBUF;
4438 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4439 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03004440 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
4441 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
4443 bttv_check_chipset();
4444
Randy Dunlapc526e222006-07-15 09:08:26 -03004445 ret = bus_register(&bttv_sub_bus_type);
4446 if (ret < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004447 pr_warn("bus_register error: %d\n", ret);
Randy Dunlapc526e222006-07-15 09:08:26 -03004448 return ret;
4449 }
Akinobu Mita9e7e85e2007-12-17 14:26:29 -03004450 ret = pci_register_driver(&bttv_pci_driver);
4451 if (ret < 0)
4452 bus_unregister(&bttv_sub_bus_type);
4453
4454 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455}
4456
Adrian Bunk7d44e892007-12-11 19:23:43 -03004457static void __exit bttv_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458{
4459 pci_unregister_driver(&bttv_pci_driver);
4460 bus_unregister(&bttv_sub_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461}
4462
4463module_init(bttv_init_module);
4464module_exit(bttv_cleanup_module);
4465
4466/*
4467 * Local variables:
4468 * c-basic-offset: 8
4469 * End:
4470 */