blob: 54579e4c740b4da7f8590ad345145553d63d8062 [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 Verkuil8bf2f8e2006-03-18 21:31:00 -030052#include <media/tvaudio.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030053#include <media/msp3400.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020054
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -070055#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#include <asm/io.h>
58#include <asm/byteorder.h>
59
Hans Verkuilb9218f22010-12-27 12:22:46 -030060#include <media/saa6588.h>
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070061
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030062#define BTTV_VERSION "0.9.19"
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064unsigned int bttv_num; /* number of Bt848s in use */
Trent Piepho4b10d3b2009-01-28 21:32:59 -030065struct bttv *bttvs[BTTV_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020067unsigned int bttv_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068unsigned int bttv_verbose = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020069unsigned int bttv_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/* config variables */
72#ifdef __BIG_ENDIAN
73static unsigned int bigendian=1;
74#else
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020075static unsigned int bigendian;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#endif
77static unsigned int radio[BTTV_MAX];
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020078static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static unsigned int gbuffers = 8;
80static unsigned int gbufsize = 0x208000;
Michael Schimeke5bd0262007-01-18 16:17:39 -030081static unsigned int reset_crop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Jean Delvare176c2f32008-09-07 12:49:59 -030083static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
84static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
85static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020086static int debug_latency;
Jean Delvared90a4ae2010-02-16 14:22:37 -030087static int disable_ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020089static unsigned int fdsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/* options */
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020092static unsigned int combfilter;
93static unsigned int lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static unsigned int automute = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020095static unsigned int chroma_agc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static unsigned int adc_crush = 1;
97static unsigned int whitecrush_upper = 0xCF;
98static unsigned int whitecrush_lower = 0x7F;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020099static unsigned int vcr_hack;
100static unsigned int irq_iswitch;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700101static unsigned int uv_ratio = 50;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200102static unsigned int full_luma_range;
103static unsigned int coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/* API features (turn on/off stuff for testing) */
106static unsigned int v4l2 = 1;
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* insmod args */
109module_param(bttv_verbose, int, 0644);
110module_param(bttv_gpio, int, 0644);
111module_param(bttv_debug, int, 0644);
112module_param(irq_debug, int, 0644);
113module_param(debug_latency, int, 0644);
Jean Delvared90a4ae2010-02-16 14:22:37 -0300114module_param(disable_ir, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116module_param(fdsr, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117module_param(gbuffers, int, 0444);
118module_param(gbufsize, int, 0444);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300119module_param(reset_crop, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121module_param(v4l2, int, 0644);
122module_param(bigendian, int, 0644);
123module_param(irq_iswitch, int, 0644);
124module_param(combfilter, int, 0444);
125module_param(lumafilter, int, 0444);
126module_param(automute, int, 0444);
127module_param(chroma_agc, int, 0444);
128module_param(adc_crush, int, 0444);
129module_param(whitecrush_upper, int, 0444);
130module_param(whitecrush_lower, int, 0444);
131module_param(vcr_hack, int, 0444);
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700132module_param(uv_ratio, int, 0444);
133module_param(full_luma_range, int, 0444);
134module_param(coring, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Jean Delvare176c2f32008-09-07 12:49:59 -0300136module_param_array(radio, int, NULL, 0444);
137module_param_array(video_nr, int, NULL, 0444);
138module_param_array(radio_nr, int, NULL, 0444);
139module_param_array(vbi_nr, int, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
142MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
143MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
144MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
145MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
146MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
Jean Delvared90a4ae2010-02-16 14:22:37 -0300147MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
149MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
Michael Schimeke5bd0262007-01-18 16:17:39 -0300150MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
151 "is 1 (yes) for compatibility with older applications");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
153MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
154MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
155MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
156MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
157MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
158MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700159MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
160MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
161MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
Jean Delvare176c2f32008-09-07 12:49:59 -0300162MODULE_PARM_DESC(video_nr, "video device numbers");
163MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
164MODULE_PARM_DESC(radio_nr, "radio device numbers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
167MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
168MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -0300169MODULE_VERSION(BTTV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/* ----------------------------------------------------------------------- */
172/* sysfs */
173
Kay Sievers54bd5b62007-10-08 16:26:13 -0300174static ssize_t show_card(struct device *cd,
175 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300177 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -0300178 struct bttv *btv = video_get_drvdata(vfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
180}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300181static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183/* ----------------------------------------------------------------------- */
Jarod Wilsonf992a492007-03-24 15:23:50 -0300184/* dvb auto-load setup */
185#if defined(CONFIG_MODULES) && defined(MODULE)
186static void request_module_async(struct work_struct *work)
187{
188 request_module("dvb-bt8xx");
189}
190
191static void request_modules(struct bttv *dev)
192{
193 INIT_WORK(&dev->request_module_wk, request_module_async);
194 schedule_work(&dev->request_module_wk);
195}
Tejun Heo707bcf32010-12-24 16:14:20 +0100196
197static void flush_request_modules(struct bttv *dev)
198{
Linus Torvalds0b8e74c2012-10-07 17:49:05 +0900199 flush_work(&dev->request_module_wk);
Tejun Heo707bcf32010-12-24 16:14:20 +0100200}
Jarod Wilsonf992a492007-03-24 15:23:50 -0300201#else
202#define request_modules(dev)
Mauro Carvalho Chehaba1fd2872012-10-27 16:33:41 -0300203#define flush_request_modules(dev) do {} while(0)
Jarod Wilsonf992a492007-03-24 15:23:50 -0300204#endif /* CONFIG_MODULES */
205
206
207/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/* static data */
209
210/* special timing tables from conexant... */
211static u8 SRAM_Table[][60] =
212{
213 /* PAL digital input over GPIO[7:0] */
214 {
215 45, // 45 bytes following
216 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
217 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
218 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
219 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
220 0x37,0x00,0xAF,0x21,0x00
221 },
222 /* NTSC digital input over GPIO[7:0] */
223 {
224 51, // 51 bytes following
225 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
226 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
227 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
228 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
229 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
230 0x00,
231 },
232 // TGB_NTSC392 // quartzsight
233 // This table has been modified to be used for Fusion Rev D
234 {
235 0x2A, // size of table = 42
236 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
237 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
238 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
239 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
240 0x20, 0x00
241 }
242};
243
Michael Schimeke5bd0262007-01-18 16:17:39 -0300244/* minhdelayx1 first video pixel we can capture on a line and
245 hdelayx1 start of active video, both relative to rising edge of
246 /HRESET pulse (0H) in 1 / fCLKx1.
247 swidth width of active video and
248 totalwidth total line width, both in 1 / fCLKx1.
249 sqwidth total line width in square pixels.
250 vdelay start of active video in 2 * field lines relative to
251 trailing edge of /VRESET pulse (VDELAY register).
252 sheight height of active video in 2 * field lines.
Hans de Goede35ccece2013-03-25 14:45:54 -0300253 extraheight Added to sheight for cropcap.bounds.height only
Michael Schimeke5bd0262007-01-18 16:17:39 -0300254 videostart0 ITU-R frame line number of the line corresponding
255 to vdelay in the first field. */
256#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
Hans de Goede35ccece2013-03-25 14:45:54 -0300257 vdelay, sheight, extraheight, videostart0) \
Michael Schimeke5bd0262007-01-18 16:17:39 -0300258 .cropcap.bounds.left = minhdelayx1, \
259 /* * 2 because vertically we count field lines times two, */ \
260 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
261 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
262 /* 4 is a safety margin at the end of the line. */ \
263 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
Hans de Goede35ccece2013-03-25 14:45:54 -0300264 .cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) - \
265 MIN_VDELAY, \
Michael Schimeke5bd0262007-01-18 16:17:39 -0300266 .cropcap.defrect.left = hdelayx1, \
267 .cropcap.defrect.top = (videostart0) * 2, \
268 .cropcap.defrect.width = swidth, \
269 .cropcap.defrect.height = sheight, \
270 .cropcap.pixelaspect.numerator = totalwidth, \
271 .cropcap.pixelaspect.denominator = sqwidth,
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273const struct bttv_tvnorm bttv_tvnorms[] = {
274 /* PAL-BDGHI */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800275 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
276 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 {
278 .v4l2_id = V4L2_STD_PAL,
279 .name = "PAL",
280 .Fsc = 35468950,
281 .swidth = 924,
282 .sheight = 576,
283 .totalwidth = 1135,
284 .adelay = 0x7f,
285 .bdelay = 0x72,
286 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
287 .scaledtwidth = 1135,
288 .hdelayx1 = 186,
289 .hactivex1 = 924,
290 .vdelay = 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300291 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 .sram = 0,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200293 /* ITU-R frame line number of the first VBI line
Michael Schimeke5bd0262007-01-18 16:17:39 -0300294 we can capture, of the first and second field.
295 The last line is determined by cropcap.bounds. */
296 .vbistart = { 7, 320 },
297 CROPCAP(/* minhdelayx1 */ 68,
298 /* hdelayx1 */ 186,
299 /* Should be (768 * 1135 + 944 / 2) / 944.
300 cropcap.defrect is used for image width
301 checks, so we keep the old value 924. */
302 /* swidth */ 924,
303 /* totalwidth */ 1135,
304 /* sqwidth */ 944,
305 /* vdelay */ 0x20,
Hans de Goede35ccece2013-03-25 14:45:54 -0300306 /* sheight */ 576,
307 /* bt878 (and bt848?) can capture another
308 line below active video. */
309 /* extraheight */ 2,
Mauro Carvalho Chehaba1fd2872012-10-27 16:33:41 -0300310 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 },{
Hans Verkuild97a11e2006-02-07 06:48:40 -0200312 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 .name = "NTSC",
314 .Fsc = 28636363,
315 .swidth = 768,
316 .sheight = 480,
317 .totalwidth = 910,
318 .adelay = 0x68,
319 .bdelay = 0x5d,
320 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
321 .scaledtwidth = 910,
322 .hdelayx1 = 128,
323 .hactivex1 = 910,
324 .vdelay = 0x1a,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300325 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 .sram = 1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200327 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300328 CROPCAP(/* minhdelayx1 */ 68,
329 /* hdelayx1 */ 128,
330 /* Should be (640 * 910 + 780 / 2) / 780? */
331 /* swidth */ 768,
332 /* totalwidth */ 910,
333 /* sqwidth */ 780,
334 /* vdelay */ 0x1a,
335 /* sheight */ 480,
Hans de Goede35ccece2013-03-25 14:45:54 -0300336 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300337 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 },{
339 .v4l2_id = V4L2_STD_SECAM,
340 .name = "SECAM",
341 .Fsc = 35468950,
342 .swidth = 924,
343 .sheight = 576,
344 .totalwidth = 1135,
345 .adelay = 0x7f,
346 .bdelay = 0xb0,
347 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
348 .scaledtwidth = 1135,
349 .hdelayx1 = 186,
350 .hactivex1 = 922,
351 .vdelay = 0x20,
352 .vbipack = 255,
353 .sram = 0, /* like PAL, correct? */
Michael H. Schimek67f15702006-01-09 15:25:27 -0200354 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300355 CROPCAP(/* minhdelayx1 */ 68,
356 /* hdelayx1 */ 186,
357 /* swidth */ 924,
358 /* totalwidth */ 1135,
359 /* sqwidth */ 944,
360 /* vdelay */ 0x20,
361 /* sheight */ 576,
Hans de Goede35ccece2013-03-25 14:45:54 -0300362 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300363 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 },{
365 .v4l2_id = V4L2_STD_PAL_Nc,
366 .name = "PAL-Nc",
367 .Fsc = 28636363,
368 .swidth = 640,
369 .sheight = 576,
370 .totalwidth = 910,
371 .adelay = 0x68,
372 .bdelay = 0x5d,
373 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
374 .scaledtwidth = 780,
375 .hdelayx1 = 130,
376 .hactivex1 = 734,
377 .vdelay = 0x1a,
378 .vbipack = 144,
379 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200380 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300381 CROPCAP(/* minhdelayx1 */ 68,
382 /* hdelayx1 */ 130,
383 /* swidth */ (640 * 910 + 780 / 2) / 780,
384 /* totalwidth */ 910,
385 /* sqwidth */ 780,
386 /* vdelay */ 0x1a,
387 /* sheight */ 576,
Hans de Goede35ccece2013-03-25 14:45:54 -0300388 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300389 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 },{
391 .v4l2_id = V4L2_STD_PAL_M,
392 .name = "PAL-M",
393 .Fsc = 28636363,
394 .swidth = 640,
395 .sheight = 480,
396 .totalwidth = 910,
397 .adelay = 0x68,
398 .bdelay = 0x5d,
399 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
400 .scaledtwidth = 780,
401 .hdelayx1 = 135,
402 .hactivex1 = 754,
403 .vdelay = 0x1a,
404 .vbipack = 144,
405 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200406 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300407 CROPCAP(/* minhdelayx1 */ 68,
408 /* hdelayx1 */ 135,
409 /* swidth */ (640 * 910 + 780 / 2) / 780,
410 /* totalwidth */ 910,
411 /* sqwidth */ 780,
412 /* vdelay */ 0x1a,
413 /* sheight */ 480,
Hans de Goede35ccece2013-03-25 14:45:54 -0300414 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300415 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 },{
417 .v4l2_id = V4L2_STD_PAL_N,
418 .name = "PAL-N",
419 .Fsc = 35468950,
420 .swidth = 768,
421 .sheight = 576,
422 .totalwidth = 1135,
423 .adelay = 0x7f,
424 .bdelay = 0x72,
425 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
426 .scaledtwidth = 944,
427 .hdelayx1 = 186,
428 .hactivex1 = 922,
429 .vdelay = 0x20,
430 .vbipack = 144,
431 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300432 .vbistart = { 7, 320 },
433 CROPCAP(/* minhdelayx1 */ 68,
434 /* hdelayx1 */ 186,
435 /* swidth */ (768 * 1135 + 944 / 2) / 944,
436 /* totalwidth */ 1135,
437 /* sqwidth */ 944,
438 /* vdelay */ 0x20,
439 /* sheight */ 576,
Hans de Goede35ccece2013-03-25 14:45:54 -0300440 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300441 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 },{
443 .v4l2_id = V4L2_STD_NTSC_M_JP,
444 .name = "NTSC-JP",
445 .Fsc = 28636363,
446 .swidth = 640,
447 .sheight = 480,
448 .totalwidth = 910,
449 .adelay = 0x68,
450 .bdelay = 0x5d,
451 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
452 .scaledtwidth = 780,
453 .hdelayx1 = 135,
454 .hactivex1 = 754,
455 .vdelay = 0x16,
456 .vbipack = 144,
457 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300458 .vbistart = { 10, 273 },
459 CROPCAP(/* minhdelayx1 */ 68,
460 /* hdelayx1 */ 135,
461 /* swidth */ (640 * 910 + 780 / 2) / 780,
462 /* totalwidth */ 910,
463 /* sqwidth */ 780,
464 /* vdelay */ 0x16,
465 /* sheight */ 480,
Hans de Goede35ccece2013-03-25 14:45:54 -0300466 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300467 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 },{
469 /* that one hopefully works with the strange timing
470 * which video recorders produce when playing a NTSC
471 * tape on a PAL TV ... */
472 .v4l2_id = V4L2_STD_PAL_60,
473 .name = "PAL-60",
474 .Fsc = 35468950,
475 .swidth = 924,
476 .sheight = 480,
477 .totalwidth = 1135,
478 .adelay = 0x7f,
479 .bdelay = 0x72,
480 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
481 .scaledtwidth = 1135,
482 .hdelayx1 = 186,
483 .hactivex1 = 924,
484 .vdelay = 0x1a,
485 .vbipack = 255,
486 .vtotal = 524,
487 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200488 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300489 CROPCAP(/* minhdelayx1 */ 68,
490 /* hdelayx1 */ 186,
491 /* swidth */ 924,
492 /* totalwidth */ 1135,
493 /* sqwidth */ 944,
494 /* vdelay */ 0x1a,
495 /* sheight */ 480,
Hans de Goede35ccece2013-03-25 14:45:54 -0300496 /* extraheight */ 0,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300497 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499};
500static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
501
502/* ----------------------------------------------------------------------- */
503/* bttv format list
504 packed pixel formats must come first */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300505static const struct bttv_format formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 {
507 .name = "8 bpp, gray",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 .fourcc = V4L2_PIX_FMT_GREY,
509 .btformat = BT848_COLOR_FMT_Y8,
510 .depth = 8,
511 .flags = FORMAT_FLAGS_PACKED,
512 },{
513 .name = "8 bpp, dithered color",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 .fourcc = V4L2_PIX_FMT_HI240,
515 .btformat = BT848_COLOR_FMT_RGB8,
516 .depth = 8,
517 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
518 },{
519 .name = "15 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 .fourcc = V4L2_PIX_FMT_RGB555,
521 .btformat = BT848_COLOR_FMT_RGB15,
522 .depth = 16,
523 .flags = FORMAT_FLAGS_PACKED,
524 },{
525 .name = "15 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 .fourcc = V4L2_PIX_FMT_RGB555X,
527 .btformat = BT848_COLOR_FMT_RGB15,
528 .btswap = 0x03, /* byteswap */
529 .depth = 16,
530 .flags = FORMAT_FLAGS_PACKED,
531 },{
532 .name = "16 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 .fourcc = V4L2_PIX_FMT_RGB565,
534 .btformat = BT848_COLOR_FMT_RGB16,
535 .depth = 16,
536 .flags = FORMAT_FLAGS_PACKED,
537 },{
538 .name = "16 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 .fourcc = V4L2_PIX_FMT_RGB565X,
540 .btformat = BT848_COLOR_FMT_RGB16,
541 .btswap = 0x03, /* byteswap */
542 .depth = 16,
543 .flags = FORMAT_FLAGS_PACKED,
544 },{
545 .name = "24 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 .fourcc = V4L2_PIX_FMT_BGR24,
547 .btformat = BT848_COLOR_FMT_RGB24,
548 .depth = 24,
549 .flags = FORMAT_FLAGS_PACKED,
550 },{
551 .name = "32 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 .fourcc = V4L2_PIX_FMT_BGR32,
553 .btformat = BT848_COLOR_FMT_RGB32,
554 .depth = 32,
555 .flags = FORMAT_FLAGS_PACKED,
556 },{
557 .name = "32 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 .fourcc = V4L2_PIX_FMT_RGB32,
559 .btformat = BT848_COLOR_FMT_RGB32,
560 .btswap = 0x0f, /* byte+word swap */
561 .depth = 32,
562 .flags = FORMAT_FLAGS_PACKED,
563 },{
564 .name = "4:2:2, packed, YUYV",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 .fourcc = V4L2_PIX_FMT_YUYV,
566 .btformat = BT848_COLOR_FMT_YUY2,
567 .depth = 16,
568 .flags = FORMAT_FLAGS_PACKED,
569 },{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 .name = "4:2:2, packed, UYVY",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 .fourcc = V4L2_PIX_FMT_UYVY,
572 .btformat = BT848_COLOR_FMT_YUY2,
573 .btswap = 0x03, /* byteswap */
574 .depth = 16,
575 .flags = FORMAT_FLAGS_PACKED,
576 },{
577 .name = "4:2:2, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 .fourcc = V4L2_PIX_FMT_YUV422P,
579 .btformat = BT848_COLOR_FMT_YCrCb422,
580 .depth = 16,
581 .flags = FORMAT_FLAGS_PLANAR,
582 .hshift = 1,
583 .vshift = 0,
584 },{
585 .name = "4:2:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 .fourcc = V4L2_PIX_FMT_YUV420,
587 .btformat = BT848_COLOR_FMT_YCrCb422,
588 .depth = 12,
589 .flags = FORMAT_FLAGS_PLANAR,
590 .hshift = 1,
591 .vshift = 1,
592 },{
593 .name = "4:2:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 .fourcc = V4L2_PIX_FMT_YVU420,
595 .btformat = BT848_COLOR_FMT_YCrCb422,
596 .depth = 12,
597 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
598 .hshift = 1,
599 .vshift = 1,
600 },{
601 .name = "4:1:1, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 .fourcc = V4L2_PIX_FMT_YUV411P,
603 .btformat = BT848_COLOR_FMT_YCrCb411,
604 .depth = 12,
605 .flags = FORMAT_FLAGS_PLANAR,
606 .hshift = 2,
607 .vshift = 0,
608 },{
609 .name = "4:1:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 .fourcc = V4L2_PIX_FMT_YUV410,
611 .btformat = BT848_COLOR_FMT_YCrCb411,
612 .depth = 9,
613 .flags = FORMAT_FLAGS_PLANAR,
614 .hshift = 2,
615 .vshift = 2,
616 },{
617 .name = "4:1:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 .fourcc = V4L2_PIX_FMT_YVU410,
619 .btformat = BT848_COLOR_FMT_YCrCb411,
620 .depth = 9,
621 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
622 .hshift = 2,
623 .vshift = 2,
624 },{
625 .name = "raw scanlines",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 .fourcc = -1,
627 .btformat = BT848_COLOR_FMT_RAW,
628 .depth = 8,
629 .flags = FORMAT_FLAGS_RAW,
630 }
631};
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300632static const unsigned int FORMATS = ARRAY_SIZE(formats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634/* ----------------------------------------------------------------------- */
635
636#define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
637#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
638#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
639#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
640#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
641#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
642#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
643#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700644#define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
645#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
646#define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
647#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649static const struct v4l2_queryctrl no_ctl = {
650 .name = "42",
651 .flags = V4L2_CTRL_FLAG_DISABLED,
652};
653static const struct v4l2_queryctrl bttv_ctls[] = {
654 /* --- video --- */
655 {
656 .id = V4L2_CID_BRIGHTNESS,
657 .name = "Brightness",
658 .minimum = 0,
659 .maximum = 65535,
660 .step = 256,
661 .default_value = 32768,
662 .type = V4L2_CTRL_TYPE_INTEGER,
663 },{
664 .id = V4L2_CID_CONTRAST,
665 .name = "Contrast",
666 .minimum = 0,
667 .maximum = 65535,
668 .step = 128,
Alan McIvor961e6682012-04-01 21:11:08 -0300669 .default_value = 27648,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 .type = V4L2_CTRL_TYPE_INTEGER,
671 },{
672 .id = V4L2_CID_SATURATION,
673 .name = "Saturation",
674 .minimum = 0,
675 .maximum = 65535,
676 .step = 128,
677 .default_value = 32768,
678 .type = V4L2_CTRL_TYPE_INTEGER,
679 },{
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -0300680 .id = V4L2_CID_COLOR_KILLER,
681 .name = "Color killer",
682 .minimum = 0,
683 .maximum = 1,
684 .type = V4L2_CTRL_TYPE_BOOLEAN,
685 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 .id = V4L2_CID_HUE,
687 .name = "Hue",
688 .minimum = 0,
689 .maximum = 65535,
690 .step = 256,
691 .default_value = 32768,
692 .type = V4L2_CTRL_TYPE_INTEGER,
693 },
694 /* --- audio --- */
695 {
696 .id = V4L2_CID_AUDIO_MUTE,
697 .name = "Mute",
698 .minimum = 0,
699 .maximum = 1,
700 .type = V4L2_CTRL_TYPE_BOOLEAN,
701 },{
702 .id = V4L2_CID_AUDIO_VOLUME,
703 .name = "Volume",
704 .minimum = 0,
705 .maximum = 65535,
706 .step = 65535/100,
707 .default_value = 65535,
708 .type = V4L2_CTRL_TYPE_INTEGER,
709 },{
710 .id = V4L2_CID_AUDIO_BALANCE,
711 .name = "Balance",
712 .minimum = 0,
713 .maximum = 65535,
714 .step = 65535/100,
715 .default_value = 32768,
716 .type = V4L2_CTRL_TYPE_INTEGER,
717 },{
718 .id = V4L2_CID_AUDIO_BASS,
719 .name = "Bass",
720 .minimum = 0,
721 .maximum = 65535,
722 .step = 65535/100,
723 .default_value = 32768,
724 .type = V4L2_CTRL_TYPE_INTEGER,
725 },{
726 .id = V4L2_CID_AUDIO_TREBLE,
727 .name = "Treble",
728 .minimum = 0,
729 .maximum = 65535,
730 .step = 65535/100,
731 .default_value = 32768,
732 .type = V4L2_CTRL_TYPE_INTEGER,
733 },
734 /* --- private --- */
735 {
736 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
737 .name = "chroma agc",
738 .minimum = 0,
739 .maximum = 1,
740 .type = V4L2_CTRL_TYPE_BOOLEAN,
741 },{
742 .id = V4L2_CID_PRIVATE_COMBFILTER,
743 .name = "combfilter",
744 .minimum = 0,
745 .maximum = 1,
746 .type = V4L2_CTRL_TYPE_BOOLEAN,
747 },{
748 .id = V4L2_CID_PRIVATE_AUTOMUTE,
749 .name = "automute",
750 .minimum = 0,
751 .maximum = 1,
752 .type = V4L2_CTRL_TYPE_BOOLEAN,
753 },{
754 .id = V4L2_CID_PRIVATE_LUMAFILTER,
755 .name = "luma decimation filter",
756 .minimum = 0,
757 .maximum = 1,
758 .type = V4L2_CTRL_TYPE_BOOLEAN,
759 },{
760 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
761 .name = "agc crush",
762 .minimum = 0,
763 .maximum = 1,
764 .type = V4L2_CTRL_TYPE_BOOLEAN,
765 },{
766 .id = V4L2_CID_PRIVATE_VCR_HACK,
767 .name = "vcr hack",
768 .minimum = 0,
769 .maximum = 1,
770 .type = V4L2_CTRL_TYPE_BOOLEAN,
771 },{
772 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
773 .name = "whitecrush upper",
774 .minimum = 0,
775 .maximum = 255,
776 .step = 1,
777 .default_value = 0xCF,
778 .type = V4L2_CTRL_TYPE_INTEGER,
779 },{
780 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
781 .name = "whitecrush lower",
782 .minimum = 0,
783 .maximum = 255,
784 .step = 1,
785 .default_value = 0x7F,
786 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700787 },{
788 .id = V4L2_CID_PRIVATE_UV_RATIO,
789 .name = "uv ratio",
790 .minimum = 0,
791 .maximum = 100,
792 .step = 1,
793 .default_value = 50,
794 .type = V4L2_CTRL_TYPE_INTEGER,
795 },{
796 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
797 .name = "full luma range",
798 .minimum = 0,
799 .maximum = 1,
800 .type = V4L2_CTRL_TYPE_BOOLEAN,
801 },{
802 .id = V4L2_CID_PRIVATE_CORING,
803 .name = "coring",
804 .minimum = 0,
805 .maximum = 3,
806 .step = 1,
807 .default_value = 0,
808 .type = V4L2_CTRL_TYPE_INTEGER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700811
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300815static const struct v4l2_queryctrl *ctrl_by_id(int id)
816{
817 int i;
818
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300819 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300820 if (bttv_ctls[i].id == id)
821 return bttv_ctls+i;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300822
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300823 return NULL;
824}
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826/* ----------------------------------------------------------------------- */
827/* resource management */
828
Michael Schimeke5bd0262007-01-18 16:17:39 -0300829/*
830 RESOURCE_ allocated by freed by
831
832 VIDEO_READ bttv_read 1) bttv_read 2)
833
834 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
835 VIDIOC_QBUF 1) bttv_release
836 VIDIOCMCAPTURE 1)
837
838 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
839 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
840 3) bttv_release
841
842 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
843 VIDIOC_QBUF 1) bttv_release
844 bttv_read, bttv_poll 1) 4)
845
846 1) The resource must be allocated when we enter buffer prepare functions
847 and remain allocated while buffers are in the DMA queue.
848 2) This is a single frame read.
849 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
850 RESOURCE_OVERLAY is allocated.
851 4) This is a continuous read, implies VIDIOC_STREAMON.
852
853 Note this driver permits video input and standard changes regardless if
854 resources are allocated.
855*/
856
857#define VBI_RESOURCES (RESOURCE_VBI)
858#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
859 RESOURCE_VIDEO_STREAM | \
860 RESOURCE_OVERLAY)
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300863int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Michael Schimeke5bd0262007-01-18 16:17:39 -0300865 int xbits; /* mutual exclusive resources */
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (fh->resources & bit)
868 /* have it already allocated */
869 return 1;
870
Michael Schimeke5bd0262007-01-18 16:17:39 -0300871 xbits = bit;
872 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
873 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* is it free? */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300876 if (btv->resources & xbits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /* no, someone else uses it */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300878 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300880
881 if ((bit & VIDEO_RESOURCES)
882 && 0 == (btv->resources & VIDEO_RESOURCES)) {
883 /* Do crop - use current, don't - use default parameters. */
884 __s32 top = btv->crop[!!fh->do_crop].rect.top;
885
886 if (btv->vbi_end > top)
887 goto fail;
888
889 /* We cannot capture the same line as video and VBI data.
890 Claim scan lines crop[].rect.top to bottom. */
891 btv->crop_start = top;
892 } else if (bit & VBI_RESOURCES) {
893 __s32 end = fh->vbi_fmt.end;
894
895 if (end > btv->crop_start)
896 goto fail;
897
898 /* Claim scan lines above fh->vbi_fmt.end. */
899 btv->vbi_end = end;
900 }
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* it's free, grab it */
903 fh->resources |= bit;
904 btv->resources |= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return 1;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300906
907 fail:
Michael Schimeke5bd0262007-01-18 16:17:39 -0300908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911static
912int check_btres(struct bttv_fh *fh, int bit)
913{
914 return (fh->resources & bit);
915}
916
917static
918int locked_btres(struct bttv *btv, int bit)
919{
920 return (btv->resources & bit);
921}
922
Michael Schimeke5bd0262007-01-18 16:17:39 -0300923/* Call with btv->lock down. */
924static void
925disclaim_vbi_lines(struct bttv *btv)
926{
927 btv->vbi_end = 0;
928}
929
930/* Call with btv->lock down. */
931static void
932disclaim_video_lines(struct bttv *btv)
933{
934 const struct bttv_tvnorm *tvnorm;
935 u8 crop;
936
937 tvnorm = &bttv_tvnorms[btv->tvnorm];
938 btv->crop_start = tvnorm->cropcap.bounds.top
939 + tvnorm->cropcap.bounds.height;
940
941 /* VBI capturing ends at VDELAY, start of video capturing, no
942 matter how many lines the VBI RISC program expects. When video
943 capturing is off, it shall no longer "preempt" VBI capturing,
944 so we set VDELAY to maximum. */
945 crop = btread(BT848_E_CROP) | 0xc0;
946 btwrite(crop, BT848_E_CROP);
947 btwrite(0xfe, BT848_E_VDELAY_LO);
948 btwrite(crop, BT848_O_CROP);
949 btwrite(0xfe, BT848_O_VDELAY_LO);
950}
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300953void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if ((fh->resources & bits) != bits) {
Joe Perches8af443e2011-08-21 19:56:48 -0300956 /* trying to free resources not allocated by us ... */
957 pr_err("BUG! (btres)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 fh->resources &= ~bits;
960 btv->resources &= ~bits;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300961
962 bits = btv->resources;
963
964 if (0 == (bits & VIDEO_RESOURCES))
965 disclaim_video_lines(btv);
966
967 if (0 == (bits & VBI_RESOURCES))
968 disclaim_vbi_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
971/* ----------------------------------------------------------------------- */
972/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
973
974/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
975 PLL_X = Reference pre-divider (0=1, 1=2)
976 PLL_C = Post divider (0=6, 1=4)
977 PLL_I = Integer input
978 PLL_F = Fractional input
979
980 F_input = 28.636363 MHz:
981 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
982*/
983
984static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
985{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800986 unsigned char fl, fh, fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800988 /* prevent overflows */
989 fin/=4;
990 fout/=4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800992 fout*=12;
993 fi=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800995 fout=(fout%fin)*256;
996 fh=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800998 fout=(fout%fin)*256;
999 fl=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001001 btwrite(fl, BT848_PLL_F_LO);
1002 btwrite(fh, BT848_PLL_F_HI);
1003 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006static void set_pll(struct bttv *btv)
1007{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001008 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001010 if (!btv->pll.pll_crystal)
1011 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
Joe Perches8af443e2011-08-21 19:56:48 -03001014 dprintk("%d: PLL: no change required\n", btv->c.nr);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001015 return;
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001018 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1019 /* no PLL needed */
1020 if (btv->pll.pll_current == 0)
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001021 return;
Joe Perches8af443e2011-08-21 19:56:48 -03001022 if (bttv_verbose)
1023 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
1024 btv->c.nr, btv->pll.pll_ifreq);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001025 btwrite(0x00,BT848_TGCTRL);
1026 btwrite(0x00,BT848_PLL_XCI);
1027 btv->pll.pll_current = 0;
1028 return;
1029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Joe Perches8af443e2011-08-21 19:56:48 -03001031 if (bttv_verbose)
1032 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
1033 btv->c.nr,
1034 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1036
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001037 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /* Let other people run while the PLL stabilizes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 msleep(10);
1040
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001041 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 btwrite(0,BT848_DSTATUS);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001043 } else {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001044 btwrite(0x08,BT848_TGCTRL);
1045 btv->pll.pll_current = btv->pll.pll_ofreq;
Joe Perches8af443e2011-08-21 19:56:48 -03001046 if (bttv_verbose)
1047 pr_info("PLL set ok\n");
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001048 return;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001049 }
1050 }
1051 btv->pll.pll_current = -1;
Joe Perches8af443e2011-08-21 19:56:48 -03001052 if (bttv_verbose)
1053 pr_info("Setting PLL failed\n");
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001054 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057/* used to switch between the bt848's analog/digital video capture modes */
1058static void bt848A_set_timing(struct bttv *btv)
1059{
1060 int i, len;
1061 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1062 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1063
Trent Piepho5221e212009-01-28 21:32:59 -03001064 if (btv->input == btv->dig) {
Joe Perches8af443e2011-08-21 19:56:48 -03001065 dprintk("%d: load digital timing table (table_idx=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 btv->c.nr,table_idx);
1067
1068 /* timing change...reset timing generator address */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001069 btwrite(0x00, BT848_TGCTRL);
1070 btwrite(0x02, BT848_TGCTRL);
1071 btwrite(0x00, BT848_TGCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 len=SRAM_Table[table_idx][0];
1074 for(i = 1; i <= len; i++)
1075 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1076 btv->pll.pll_ofreq = 27000000;
1077
1078 set_pll(btv);
1079 btwrite(0x11, BT848_TGCTRL);
1080 btwrite(0x41, BT848_DVSIF);
1081 } else {
1082 btv->pll.pll_ofreq = fsc;
1083 set_pll(btv);
1084 btwrite(0x0, BT848_DVSIF);
1085 }
1086}
1087
1088/* ----------------------------------------------------------------------- */
1089
1090static void bt848_bright(struct bttv *btv, int bright)
1091{
1092 int value;
1093
Joe Perches8af443e2011-08-21 19:56:48 -03001094 // printk("set bright: %d\n", bright); // DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 btv->bright = bright;
1096
1097 /* We want -128 to 127 we get 0-65535 */
1098 value = (bright >> 8) - 128;
1099 btwrite(value & 0xff, BT848_BRIGHT);
1100}
1101
1102static void bt848_hue(struct bttv *btv, int hue)
1103{
1104 int value;
1105
1106 btv->hue = hue;
1107
1108 /* -128 to 127 */
1109 value = (hue >> 8) - 128;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001110 btwrite(value & 0xff, BT848_HUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
1113static void bt848_contrast(struct bttv *btv, int cont)
1114{
1115 int value,hibit;
1116
1117 btv->contrast = cont;
1118
1119 /* 0-511 */
1120 value = (cont >> 7);
1121 hibit = (value >> 6) & 4;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001122 btwrite(value & 0xff, BT848_CONTRAST_LO);
1123 btaor(hibit, ~4, BT848_E_CONTROL);
1124 btaor(hibit, ~4, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
1127static void bt848_sat(struct bttv *btv, int color)
1128{
1129 int val_u,val_v,hibits;
1130
1131 btv->saturation = color;
1132
1133 /* 0-511 for the color */
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001134 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1135 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001136 hibits = (val_u >> 7) & 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 hibits |= (val_v >> 8) & 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001138 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1139 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1140 btaor(hibits, ~3, BT848_E_CONTROL);
1141 btaor(hibits, ~3, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144/* ----------------------------------------------------------------------- */
1145
1146static int
1147video_mux(struct bttv *btv, unsigned int input)
1148{
1149 int mux,mask2;
1150
1151 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1152 return -EINVAL;
1153
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001154 /* needed by RemoteVideo MX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1156 if (mask2)
1157 gpio_inout(mask2,mask2);
1158
1159 if (input == btv->svhs) {
1160 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1161 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1162 } else {
1163 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1164 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1165 }
Trent Piepho6f987002009-01-28 21:32:59 -03001166 mux = bttv_muxsel(btv, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 btaor(mux<<5, ~(3<<5), BT848_IFORM);
Joe Perches8af443e2011-08-21 19:56:48 -03001168 dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /* card specific hook */
1171 if(bttv_tvcards[btv->c.type].muxsel_hook)
1172 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1173 return 0;
1174}
1175
1176static char *audio_modes[] = {
1177 "audio: tuner", "audio: radio", "audio: extern",
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001178 "audio: intern", "audio: mute"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179};
1180
1181static int
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001182audio_mux(struct bttv *btv, int input, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001184 int gpio_val, signal;
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001185 struct v4l2_control ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1188 bttv_tvcards[btv->c.type].gpiomask);
1189 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1190
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001191 btv->mute = mute;
1192 btv->audio = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001194 /* automute */
1195 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1196
1197 if (mute)
1198 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1199 else
1200 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001201
Trent Piepho72134a62009-01-28 21:32:59 -03001202 switch (btv->c.type) {
1203 case BTTV_BOARD_VOODOOTV_FM:
1204 case BTTV_BOARD_VOODOOTV_200:
1205 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1206 break;
1207
1208 default:
1209 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1210 }
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (bttv_gpio)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001213 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1214 if (in_interrupt())
1215 return 0;
1216
1217 ctrl.id = V4L2_CID_AUDIO_MUTE;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001218 ctrl.value = btv->mute;
Hans Verkuil859f0272009-03-28 08:29:00 -03001219 bttv_call_all(btv, core, s_ctrl, &ctrl);
1220 if (btv->sd_msp34xx) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001221 u32 in;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001222
1223 /* Note: the inputs tuner/radio/extern/intern are translated
1224 to msp routings. This assumes common behavior for all msp3400
1225 based TV cards. When this assumption fails, then the
1226 specific MSP routing must be added to the card table.
1227 For now this is sufficient. */
1228 switch (input) {
1229 case TVAUDIO_INPUT_RADIO:
Hans de Goede7025e522012-05-21 07:46:22 -03001230 /* Some boards need the msp do to the radio demod */
1231 if (btv->radio_uses_msp_demodulator) {
1232 in = MSP_INPUT_DEFAULT;
1233 break;
1234 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001235 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001236 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001237 break;
1238 case TVAUDIO_INPUT_EXTERN:
Hans Verkuil5325b422009-04-02 11:26:22 -03001239 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001240 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001241 break;
1242 case TVAUDIO_INPUT_INTERN:
1243 /* Yes, this is the same input as for RADIO. I doubt
1244 if this is ever used. The only board with an INTERN
1245 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1246 that was tested. My guess is that the whole INTERN
1247 input does not work. */
Hans Verkuil5325b422009-04-02 11:26:22 -03001248 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001249 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001250 break;
1251 case TVAUDIO_INPUT_TUNER:
1252 default:
Wade Berrier434b2522007-06-25 13:02:16 -03001253 /* This is the only card that uses TUNER2, and afaik,
1254 is the only difference between the VOODOOTV_FM
1255 and VOODOOTV_200 */
1256 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
Hans Verkuil5325b422009-04-02 11:26:22 -03001257 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
Wade Berrier434b2522007-06-25 13:02:16 -03001258 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1259 else
Hans Verkuil5325b422009-04-02 11:26:22 -03001260 in = MSP_INPUT_DEFAULT;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001261 break;
1262 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001263 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1264 in, MSP_OUTPUT_DEFAULT, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001265 }
Hans Verkuil859f0272009-03-28 08:29:00 -03001266 if (btv->sd_tvaudio) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001267 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1268 input, 0, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return 0;
1271}
1272
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001273static inline int
1274audio_mute(struct bttv *btv, int mute)
1275{
1276 return audio_mux(btv, btv->audio, mute);
1277}
1278
1279static inline int
1280audio_input(struct bttv *btv, int input)
1281{
1282 return audio_mux(btv, input, btv->mute);
1283}
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285static void
Michael Schimeke5bd0262007-01-18 16:17:39 -03001286bttv_crop_calc_limits(struct bttv_crop *c)
1287{
1288 /* Scale factor min. 1:1, max. 16:1. Min. image size
1289 48 x 32. Scaled width must be a multiple of 4. */
1290
1291 if (1) {
1292 /* For bug compatibility with VIDIOCGCAP and image
1293 size checks in earlier driver versions. */
1294 c->min_scaled_width = 48;
1295 c->min_scaled_height = 32;
1296 } else {
1297 c->min_scaled_width =
1298 (max(48, c->rect.width >> 4) + 3) & ~3;
1299 c->min_scaled_height =
1300 max(32, c->rect.height >> 4);
1301 }
1302
1303 c->max_scaled_width = c->rect.width & ~3;
1304 c->max_scaled_height = c->rect.height;
1305}
1306
1307static void
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001308bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
Michael Schimeke5bd0262007-01-18 16:17:39 -03001309{
1310 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1311 bttv_crop_calc_limits(c);
1312}
1313
1314/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315static int
1316set_tvnorm(struct bttv *btv, unsigned int norm)
1317{
1318 const struct bttv_tvnorm *tvnorm;
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001319 v4l2_std_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001321 BUG_ON(norm >= BTTV_TVNORMS);
1322 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 tvnorm = &bttv_tvnorms[norm];
1325
Mike Isely2de26c02009-09-21 12:42:22 -03001326 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001327 sizeof (tvnorm->cropcap))) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001328 bttv_crop_reset(&btv->crop[0], norm);
1329 btv->crop[1] = btv->crop[0]; /* current = default */
1330
1331 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1332 btv->crop_start = tvnorm->cropcap.bounds.top
1333 + tvnorm->cropcap.bounds.height;
1334 }
1335 }
1336
1337 btv->tvnorm = norm;
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 btwrite(tvnorm->adelay, BT848_ADELAY);
1340 btwrite(tvnorm->bdelay, BT848_BDELAY);
1341 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1342 BT848_IFORM);
1343 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1344 btwrite(1, BT848_VBI_PACK_DEL);
1345 bt848A_set_timing(btv);
1346
1347 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -08001348 case BTTV_BOARD_VOODOOTV_FM:
Wade Berrier434b2522007-06-25 13:02:16 -03001349 case BTTV_BOARD_VOODOOTV_200:
Trent Piepho72134a62009-01-28 21:32:59 -03001350 bttv_tda9880_setnorm(btv, gpio_read());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001353 id = tvnorm->v4l2_id;
Hans Verkuilf41737e2009-04-01 03:52:39 -03001354 bttv_call_all(btv, core, s_std, id);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return 0;
1357}
1358
Michael Schimeke5bd0262007-01-18 16:17:39 -03001359/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360static void
Trent Piepho333408f2007-07-03 15:08:10 -03001361set_input(struct bttv *btv, unsigned int input, unsigned int norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 unsigned long flags;
1364
1365 btv->input = input;
1366 if (irq_iswitch) {
1367 spin_lock_irqsave(&btv->s_lock,flags);
1368 if (btv->curr.frame_irq) {
1369 /* active capture -> delayed input switch */
1370 btv->new_input = input;
1371 } else {
1372 video_mux(btv,input);
1373 }
1374 spin_unlock_irqrestore(&btv->s_lock,flags);
1375 } else {
1376 video_mux(btv,input);
1377 }
Trent Piephoabb03622009-01-28 21:32:59 -03001378 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1379 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
Trent Piepho333408f2007-07-03 15:08:10 -03001380 set_tvnorm(btv, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
1383static void init_irqreg(struct bttv *btv)
1384{
1385 /* clear status */
1386 btwrite(0xfffffUL, BT848_INT_STAT);
1387
1388 if (bttv_tvcards[btv->c.type].no_video) {
1389 /* i2c only */
1390 btwrite(BT848_INT_I2CDONE,
1391 BT848_INT_MASK);
1392 } else {
1393 /* full video */
1394 btwrite((btv->triton1) |
1395 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1396 BT848_INT_SCERR |
1397 (fdsr ? BT848_INT_FDSR : 0) |
Jean Delvareeb1b27b2008-08-30 10:18:21 -03001398 BT848_INT_RISCI | BT848_INT_OCERR |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1400 BT848_INT_I2CDONE,
1401 BT848_INT_MASK);
1402 }
1403}
1404
1405static void init_bt848(struct bttv *btv)
1406{
1407 int val;
1408
1409 if (bttv_tvcards[btv->c.type].no_video) {
1410 /* very basic init only */
1411 init_irqreg(btv);
1412 return;
1413 }
1414
1415 btwrite(0x00, BT848_CAP_CTL);
1416 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1417 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1418
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001419 /* set planar and packed mode trigger points and */
1420 /* set rising edge of inverted GPINTR pin as irq trigger */
1421 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1422 BT848_GPIO_DMA_CTL_PLTP1_16|
1423 BT848_GPIO_DMA_CTL_PLTP23_16|
1424 BT848_GPIO_DMA_CTL_GPINTC|
1425 BT848_GPIO_DMA_CTL_GPINTI,
1426 BT848_GPIO_DMA_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001429 btwrite(val, BT848_E_SCLOOP);
1430 btwrite(val, BT848_O_SCLOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001432 btwrite(0x20, BT848_E_VSCALE_HI);
1433 btwrite(0x20, BT848_O_VSCALE_HI);
1434 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 BT848_ADC);
1436
1437 btwrite(whitecrush_upper, BT848_WC_UP);
1438 btwrite(whitecrush_lower, BT848_WC_DOWN);
1439
1440 if (btv->opt_lumafilter) {
1441 btwrite(0, BT848_E_CONTROL);
1442 btwrite(0, BT848_O_CONTROL);
1443 } else {
1444 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1445 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1446 }
1447
1448 bt848_bright(btv, btv->bright);
1449 bt848_hue(btv, btv->hue);
1450 bt848_contrast(btv, btv->contrast);
1451 bt848_sat(btv, btv->saturation);
1452
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001453 /* interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 init_irqreg(btv);
1455}
1456
1457static void bttv_reinit_bt848(struct bttv *btv)
1458{
1459 unsigned long flags;
1460
1461 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03001462 pr_info("%d: reset, reinitialize\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 spin_lock_irqsave(&btv->s_lock,flags);
1464 btv->errors=0;
1465 bttv_set_dma(btv,0);
1466 spin_unlock_irqrestore(&btv->s_lock,flags);
1467
1468 init_bt848(btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001469 btv->pll.pll_current = -1;
Trent Piepho333408f2007-07-03 15:08:10 -03001470 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001473static int bttv_g_ctrl(struct file *file, void *priv,
1474 struct v4l2_control *c)
1475{
1476 struct bttv_fh *fh = priv;
1477 struct bttv *btv = fh->btv;
1478
1479 switch (c->id) {
1480 case V4L2_CID_BRIGHTNESS:
1481 c->value = btv->bright;
1482 break;
1483 case V4L2_CID_HUE:
1484 c->value = btv->hue;
1485 break;
1486 case V4L2_CID_CONTRAST:
1487 c->value = btv->contrast;
1488 break;
1489 case V4L2_CID_SATURATION:
1490 c->value = btv->saturation;
1491 break;
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -03001492 case V4L2_CID_COLOR_KILLER:
1493 c->value = btv->opt_color_killer;
1494 break;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001495
1496 case V4L2_CID_AUDIO_MUTE:
1497 case V4L2_CID_AUDIO_VOLUME:
1498 case V4L2_CID_AUDIO_BALANCE:
1499 case V4L2_CID_AUDIO_BASS:
1500 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil859f0272009-03-28 08:29:00 -03001501 bttv_call_all(btv, core, g_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001502 break;
1503
1504 case V4L2_CID_PRIVATE_CHROMA_AGC:
1505 c->value = btv->opt_chroma_agc;
1506 break;
1507 case V4L2_CID_PRIVATE_COMBFILTER:
1508 c->value = btv->opt_combfilter;
1509 break;
1510 case V4L2_CID_PRIVATE_LUMAFILTER:
1511 c->value = btv->opt_lumafilter;
1512 break;
1513 case V4L2_CID_PRIVATE_AUTOMUTE:
1514 c->value = btv->opt_automute;
1515 break;
1516 case V4L2_CID_PRIVATE_AGC_CRUSH:
1517 c->value = btv->opt_adc_crush;
1518 break;
1519 case V4L2_CID_PRIVATE_VCR_HACK:
1520 c->value = btv->opt_vcr_hack;
1521 break;
1522 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1523 c->value = btv->opt_whitecrush_upper;
1524 break;
1525 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1526 c->value = btv->opt_whitecrush_lower;
1527 break;
1528 case V4L2_CID_PRIVATE_UV_RATIO:
1529 c->value = btv->opt_uv_ratio;
1530 break;
1531 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1532 c->value = btv->opt_full_luma_range;
1533 break;
1534 case V4L2_CID_PRIVATE_CORING:
1535 c->value = btv->opt_coring;
1536 break;
1537 default:
1538 return -EINVAL;
1539 }
1540 return 0;
1541}
1542
1543static int bttv_s_ctrl(struct file *file, void *f,
1544 struct v4l2_control *c)
1545{
1546 int err;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001547 struct bttv_fh *fh = f;
1548 struct bttv *btv = fh->btv;
1549
Hans Verkuilffb48772010-05-01 08:03:24 -03001550 err = v4l2_prio_check(&btv->prio, fh->prio);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001551 if (0 != err)
1552 return err;
1553
1554 switch (c->id) {
1555 case V4L2_CID_BRIGHTNESS:
1556 bt848_bright(btv, c->value);
1557 break;
1558 case V4L2_CID_HUE:
1559 bt848_hue(btv, c->value);
1560 break;
1561 case V4L2_CID_CONTRAST:
1562 bt848_contrast(btv, c->value);
1563 break;
1564 case V4L2_CID_SATURATION:
1565 bt848_sat(btv, c->value);
1566 break;
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -03001567 case V4L2_CID_COLOR_KILLER:
1568 btv->opt_color_killer = c->value;
1569 if (btv->opt_color_killer) {
1570 btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1571 btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1572 } else {
1573 btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1574 btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1575 }
1576 break;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001577 case V4L2_CID_AUDIO_MUTE:
1578 audio_mute(btv, c->value);
1579 /* fall through */
1580 case V4L2_CID_AUDIO_VOLUME:
1581 if (btv->volume_gpio)
1582 btv->volume_gpio(btv, c->value);
1583
Hans Verkuil859f0272009-03-28 08:29:00 -03001584 bttv_call_all(btv, core, s_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001585 break;
1586 case V4L2_CID_AUDIO_BALANCE:
1587 case V4L2_CID_AUDIO_BASS:
1588 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil859f0272009-03-28 08:29:00 -03001589 bttv_call_all(btv, core, s_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001590 break;
1591
1592 case V4L2_CID_PRIVATE_CHROMA_AGC:
1593 btv->opt_chroma_agc = c->value;
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -03001594 if (btv->opt_chroma_agc) {
1595 btor(BT848_SCLOOP_CAGC, BT848_E_SCLOOP);
1596 btor(BT848_SCLOOP_CAGC, BT848_O_SCLOOP);
1597 } else {
1598 btand(~BT848_SCLOOP_CAGC, BT848_E_SCLOOP);
1599 btand(~BT848_SCLOOP_CAGC, BT848_O_SCLOOP);
1600 }
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001601 break;
1602 case V4L2_CID_PRIVATE_COMBFILTER:
1603 btv->opt_combfilter = c->value;
1604 break;
1605 case V4L2_CID_PRIVATE_LUMAFILTER:
1606 btv->opt_lumafilter = c->value;
1607 if (btv->opt_lumafilter) {
1608 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1609 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1610 } else {
1611 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1612 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1613 }
1614 break;
1615 case V4L2_CID_PRIVATE_AUTOMUTE:
1616 btv->opt_automute = c->value;
1617 break;
1618 case V4L2_CID_PRIVATE_AGC_CRUSH:
1619 btv->opt_adc_crush = c->value;
1620 btwrite(BT848_ADC_RESERVED |
1621 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1622 BT848_ADC);
1623 break;
1624 case V4L2_CID_PRIVATE_VCR_HACK:
1625 btv->opt_vcr_hack = c->value;
1626 break;
1627 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1628 btv->opt_whitecrush_upper = c->value;
1629 btwrite(c->value, BT848_WC_UP);
1630 break;
1631 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1632 btv->opt_whitecrush_lower = c->value;
1633 btwrite(c->value, BT848_WC_DOWN);
1634 break;
1635 case V4L2_CID_PRIVATE_UV_RATIO:
1636 btv->opt_uv_ratio = c->value;
1637 bt848_sat(btv, btv->saturation);
1638 break;
1639 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1640 btv->opt_full_luma_range = c->value;
1641 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1642 break;
1643 case V4L2_CID_PRIVATE_CORING:
1644 btv->opt_coring = c->value;
1645 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1646 break;
1647 default:
1648 return -EINVAL;
1649 }
1650 return 0;
1651}
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653/* ----------------------------------------------------------------------- */
1654
1655void bttv_gpio_tracking(struct bttv *btv, char *comment)
1656{
1657 unsigned int outbits, data;
1658 outbits = btread(BT848_GPIO_OUT_EN);
1659 data = btread(BT848_GPIO_DATA);
Joe Perches8af443e2011-08-21 19:56:48 -03001660 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1661 btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
1664static void bttv_field_count(struct bttv *btv)
1665{
1666 int need_count = 0;
1667
1668 if (btv->users)
1669 need_count++;
1670
1671 if (need_count) {
1672 /* start field counter */
1673 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1674 } else {
1675 /* stop field counter */
1676 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1677 btv->field_count = 0;
1678 }
1679}
1680
1681static const struct bttv_format*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682format_by_fourcc(int fourcc)
1683{
1684 unsigned int i;
1685
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001686 for (i = 0; i < FORMATS; i++) {
1687 if (-1 == formats[i].fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 continue;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001689 if (formats[i].fourcc == fourcc)
1690 return formats+i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692 return NULL;
1693}
1694
1695/* ----------------------------------------------------------------------- */
1696/* misc helpers */
1697
1698static int
1699bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1700 struct bttv_buffer *new)
1701{
1702 struct bttv_buffer *old;
1703 unsigned long flags;
1704 int retval = 0;
1705
Joe Perches8af443e2011-08-21 19:56:48 -03001706 dprintk("switch_overlay: enter [new=%p]\n", new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (new)
Brandon Philips0fc06862007-11-06 20:02:36 -03001708 new->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 spin_lock_irqsave(&btv->s_lock,flags);
1710 old = btv->screen;
1711 btv->screen = new;
1712 btv->loop_irq |= 1;
1713 bttv_set_dma(btv, 0x03);
1714 spin_unlock_irqrestore(&btv->s_lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (NULL != old) {
Joe Perches8af443e2011-08-21 19:56:48 -03001716 dprintk("switch_overlay: old=%p state is %d\n",
1717 old, old->vb.state);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001718 bttv_dma_free(&fh->cap,btv, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 kfree(old);
1720 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03001721 if (NULL == new)
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03001722 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 dprintk("switch_overlay: done\n");
1724 return retval;
1725}
1726
1727/* ----------------------------------------------------------------------- */
1728/* video4linux (1) interface */
1729
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001730static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1731 struct bttv_buffer *buf,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001732 const struct bttv_format *fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 unsigned int width, unsigned int height,
1734 enum v4l2_field field)
1735{
Michael Schimeke5bd0262007-01-18 16:17:39 -03001736 struct bttv_fh *fh = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 int redo_dma_risc = 0;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001738 struct bttv_crop c;
1739 int norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 int rc;
1741
1742 /* check settings */
1743 if (NULL == fmt)
1744 return -EINVAL;
1745 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1746 width = RAW_BPL;
1747 height = RAW_LINES*2;
1748 if (width*height > buf->vb.bsize)
1749 return -EINVAL;
1750 buf->vb.size = buf->vb.bsize;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001751
1752 /* Make sure tvnorm and vbi_end remain consistent
1753 until we're done. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03001754
1755 norm = btv->tvnorm;
1756
1757 /* In this mode capturing always starts at defrect.top
1758 (default VDELAY), ignoring cropping parameters. */
1759 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001761 }
1762
Michael Schimeke5bd0262007-01-18 16:17:39 -03001763 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1764 } else {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001765 norm = btv->tvnorm;
1766 c = btv->crop[!!fh->do_crop];
1767
Michael Schimeke5bd0262007-01-18 16:17:39 -03001768 if (width < c.min_scaled_width ||
1769 width > c.max_scaled_width ||
1770 height < c.min_scaled_height)
1771 return -EINVAL;
1772
1773 switch (field) {
1774 case V4L2_FIELD_TOP:
1775 case V4L2_FIELD_BOTTOM:
1776 case V4L2_FIELD_ALTERNATE:
1777 /* btv->crop counts frame lines. Max. scale
1778 factor is 16:1 for frames, 8:1 for fields. */
1779 if (height * 2 > c.max_scaled_height)
1780 return -EINVAL;
1781 break;
1782
1783 default:
1784 if (height > c.max_scaled_height)
1785 return -EINVAL;
1786 break;
1787 }
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 buf->vb.size = (width * height * fmt->depth) >> 3;
1790 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1791 return -EINVAL;
1792 }
1793
1794 /* alloc + fill struct bttv_buffer (if changed) */
1795 if (buf->vb.width != width || buf->vb.height != height ||
1796 buf->vb.field != field ||
Michael Schimeke5bd0262007-01-18 16:17:39 -03001797 buf->tvnorm != norm || buf->fmt != fmt ||
1798 buf->crop.top != c.rect.top ||
1799 buf->crop.left != c.rect.left ||
1800 buf->crop.width != c.rect.width ||
1801 buf->crop.height != c.rect.height) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 buf->vb.width = width;
1803 buf->vb.height = height;
1804 buf->vb.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001805 buf->tvnorm = norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 buf->fmt = fmt;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001807 buf->crop = c.rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 redo_dma_risc = 1;
1809 }
1810
1811 /* alloc risc memory */
Brandon Philips0fc06862007-11-06 20:02:36 -03001812 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 redo_dma_risc = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001814 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 goto fail;
1816 }
1817
1818 if (redo_dma_risc)
1819 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1820 goto fail;
1821
Brandon Philips0fc06862007-11-06 20:02:36 -03001822 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return 0;
1824
1825 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001826 bttv_dma_free(q,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return rc;
1828}
1829
1830static int
1831buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1832{
1833 struct bttv_fh *fh = q->priv_data;
1834
1835 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1836 if (0 == *count)
1837 *count = gbuffers;
Andreas Bombedab7e312010-03-21 16:02:45 -03001838 if (*size * *count > gbuffers * gbufsize)
1839 *count = (gbuffers * gbufsize) / *size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return 0;
1841}
1842
1843static int
1844buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1845 enum v4l2_field field)
1846{
1847 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1848 struct bttv_fh *fh = q->priv_data;
1849
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001850 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 fh->width, fh->height, field);
1852}
1853
1854static void
1855buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1856{
1857 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1858 struct bttv_fh *fh = q->priv_data;
1859 struct bttv *btv = fh->btv;
1860
Brandon Philips0fc06862007-11-06 20:02:36 -03001861 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 list_add_tail(&buf->vb.queue,&btv->capture);
1863 if (!btv->curr.frame_irq) {
1864 btv->loop_irq |= 1;
1865 bttv_set_dma(btv, 0x03);
1866 }
1867}
1868
1869static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1870{
1871 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1872 struct bttv_fh *fh = q->priv_data;
1873
Michael Schimekfeaba7a2007-01-26 08:30:05 -03001874 bttv_dma_free(q,fh->btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
1877static struct videobuf_queue_ops bttv_video_qops = {
1878 .buf_setup = buffer_setup,
1879 .buf_prepare = buffer_prepare,
1880 .buf_queue = buffer_queue,
1881 .buf_release = buffer_release,
1882};
1883
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001884static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001886 struct bttv_fh *fh = priv;
1887 struct bttv *btv = fh->btv;
1888 unsigned int i;
1889 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Hans Verkuilffb48772010-05-01 08:03:24 -03001891 err = v4l2_prio_check(&btv->prio, fh->prio);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001892 if (err)
1893 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001895 for (i = 0; i < BTTV_TVNORMS; i++)
1896 if (*id & bttv_tvnorms[i].v4l2_id)
1897 break;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001898 if (i == BTTV_TVNORMS) {
1899 err = -EINVAL;
1900 goto err;
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001903 set_tvnorm(btv, i);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001904
1905err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001907 return err;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001908}
1909
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001910static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001911{
1912 struct bttv_fh *fh = f;
1913 struct bttv *btv = fh->btv;
1914
1915 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1916 *id = V4L2_STD_625_50;
1917 else
1918 *id = V4L2_STD_525_60;
1919 return 0;
1920}
1921
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001922static int bttv_enum_input(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001923 struct v4l2_input *i)
1924{
1925 struct bttv_fh *fh = priv;
1926 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001927 int rc = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001928
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001929 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) {
1930 rc = -EINVAL;
1931 goto err;
1932 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001933
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001934 i->type = V4L2_INPUT_TYPE_CAMERA;
1935 i->audioset = 1;
1936
Trent Piephoabb03622009-01-28 21:32:59 -03001937 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001938 sprintf(i->name, "Television");
1939 i->type = V4L2_INPUT_TYPE_TUNER;
1940 i->tuner = 0;
1941 } else if (i->index == btv->svhs) {
1942 sprintf(i->name, "S-Video");
1943 } else {
1944 sprintf(i->name, "Composite%d", i->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001947 if (i->index == btv->input) {
1948 __u32 dstatus = btread(BT848_DSTATUS);
1949 if (0 == (dstatus & BT848_DSTATUS_PRES))
1950 i->status |= V4L2_IN_ST_NO_SIGNAL;
1951 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1952 i->status |= V4L2_IN_ST_NO_H_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001955 i->std = BTTV_NORMS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001957err:
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001958
1959 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001960}
Nickolay V. Shmyrev4b9b9362006-08-25 16:53:04 -03001961
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001962static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001963{
1964 struct bttv_fh *fh = priv;
1965 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001967 *i = btv->input;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001968
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001969 return 0;
1970}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001972static int bttv_s_input(struct file *file, void *priv, unsigned int i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001973{
1974 struct bttv_fh *fh = priv;
1975 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001977 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001979 err = v4l2_prio_check(&btv->prio, fh->prio);
1980 if (unlikely(err))
1981 goto err;
1982
1983 if (i > bttv_tvcards[btv->c.type].video_inputs) {
1984 err = -EINVAL;
1985 goto err;
1986 }
1987
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001988 set_input(btv, i, btv->tvnorm);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001989
1990err:
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001991 return 0;
1992}
1993
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001994static int bttv_s_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001995 struct v4l2_tuner *t)
1996{
1997 struct bttv_fh *fh = priv;
1998 struct bttv *btv = fh->btv;
1999 int err;
2000
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002001 if (unlikely(0 != t->index))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002002 return -EINVAL;
2003
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002004 if (unlikely(btv->tuner_type == TUNER_ABSENT)) {
2005 err = -EINVAL;
2006 goto err;
2007 }
2008
2009 err = v4l2_prio_check(&btv->prio, fh->prio);
2010 if (unlikely(err))
2011 goto err;
2012
Hans Verkuil859f0272009-03-28 08:29:00 -03002013 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002014
2015 if (btv->audio_mode_gpio)
2016 btv->audio_mode_gpio(btv, t, 1);
2017
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002018err:
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002019
2020 return 0;
2021}
2022
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002023static int bttv_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002024 struct v4l2_frequency *f)
2025{
2026 struct bttv_fh *fh = priv;
2027 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002028
Robert Fitzsimons1b069012008-04-01 11:41:54 -03002029 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002030 f->frequency = btv->freq;
2031
2032 return 0;
2033}
2034
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002035static int bttv_s_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002036 struct v4l2_frequency *f)
2037{
2038 struct bttv_fh *fh = priv;
2039 struct bttv *btv = fh->btv;
2040 int err;
2041
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002042 if (unlikely(f->tuner != 0))
2043 return -EINVAL;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002044
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002045 err = v4l2_prio_check(&btv->prio, fh->prio);
2046 if (unlikely(err))
2047 goto err;
2048
2049 if (unlikely(f->type != (btv->radio_user
2050 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV))) {
2051 err = -EINVAL;
2052 goto err;
2053 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002054 btv->freq = f->frequency;
Hans Verkuil859f0272009-03-28 08:29:00 -03002055 bttv_call_all(btv, tuner, s_frequency, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002056 if (btv->has_matchbox && btv->radio_user)
2057 tea5757_set_freq(btv, btv->freq);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002058err:
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002059
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002060 return 0;
2061}
2062
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002063static int bttv_log_status(struct file *file, void *f)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002064{
2065 struct bttv_fh *fh = f;
2066 struct bttv *btv = fh->btv;
2067
Hans Verkuil859f0272009-03-28 08:29:00 -03002068 bttv_call_all(btv, core, log_status);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002069 return 0;
2070}
2071
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002072#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002073static int bttv_g_register(struct file *file, void *f,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002074 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002075{
2076 struct bttv_fh *fh = f;
2077 struct bttv *btv = fh->btv;
2078
2079 if (!capable(CAP_SYS_ADMIN))
2080 return -EPERM;
2081
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002082 if (!v4l2_chip_match_host(&reg->match))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002083 return -EINVAL;
2084
2085 /* bt848 has a 12-bit register space */
2086 reg->reg &= 0xfff;
2087 reg->val = btread(reg->reg);
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002088 reg->size = 1;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002089
2090 return 0;
2091}
2092
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002093static int bttv_s_register(struct file *file, void *f,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002094 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002095{
2096 struct bttv_fh *fh = f;
2097 struct bttv *btv = fh->btv;
2098
2099 if (!capable(CAP_SYS_ADMIN))
2100 return -EPERM;
2101
Hans Verkuilaecde8b52008-12-30 07:14:19 -03002102 if (!v4l2_chip_match_host(&reg->match))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002103 return -EINVAL;
2104
2105 /* bt848 has a 12-bit register space */
2106 reg->reg &= 0xfff;
2107 btwrite(reg->val, reg->reg);
2108
2109 return 0;
2110}
2111#endif
2112
Michael Schimeke5bd0262007-01-18 16:17:39 -03002113/* Given cropping boundaries b and the scaled width and height of a
2114 single field or frame, which must not exceed hardware limits, this
2115 function adjusts the cropping parameters c. */
2116static void
2117bttv_crop_adjust (struct bttv_crop * c,
2118 const struct v4l2_rect * b,
2119 __s32 width,
2120 __s32 height,
2121 enum v4l2_field field)
2122{
2123 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2124 __s32 max_left;
2125 __s32 max_top;
2126
2127 if (width < c->min_scaled_width) {
2128 /* Max. hor. scale factor 16:1. */
2129 c->rect.width = width * 16;
2130 } else if (width > c->max_scaled_width) {
2131 /* Min. hor. scale factor 1:1. */
2132 c->rect.width = width;
2133
2134 max_left = b->left + b->width - width;
2135 max_left = min(max_left, (__s32) MAX_HDELAY);
2136 if (c->rect.left > max_left)
2137 c->rect.left = max_left;
2138 }
2139
2140 if (height < c->min_scaled_height) {
2141 /* Max. vert. scale factor 16:1, single fields 8:1. */
2142 c->rect.height = height * 16;
2143 } else if (frame_height > c->max_scaled_height) {
2144 /* Min. vert. scale factor 1:1.
2145 Top and height count field lines times two. */
2146 c->rect.height = (frame_height + 1) & ~1;
2147
2148 max_top = b->top + b->height - c->rect.height;
2149 if (c->rect.top > max_top)
2150 c->rect.top = max_top;
2151 }
2152
2153 bttv_crop_calc_limits(c);
2154}
2155
2156/* Returns an error if scaling to a frame or single field with the given
2157 width and height is not possible with the current cropping parameters
2158 and width aligned according to width_mask. If adjust_size is TRUE the
2159 function may adjust the width and/or height instead, rounding width
2160 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2161 also adjust the current cropping parameters to get closer to the
2162 desired image size. */
2163static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002164limit_scaled_size_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002165 __s32 * width,
2166 __s32 * height,
2167 enum v4l2_field field,
2168 unsigned int width_mask,
2169 unsigned int width_bias,
2170 int adjust_size,
2171 int adjust_crop)
2172{
2173 struct bttv *btv = fh->btv;
2174 const struct v4l2_rect *b;
2175 struct bttv_crop *c;
2176 __s32 min_width;
2177 __s32 min_height;
2178 __s32 max_width;
2179 __s32 max_height;
2180 int rc;
2181
2182 BUG_ON((int) width_mask >= 0 ||
2183 width_bias >= (unsigned int) -width_mask);
2184
2185 /* Make sure tvnorm, vbi_end and the current cropping parameters
2186 remain consistent until we're done. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03002187
2188 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2189
2190 /* Do crop - use current, don't - use default parameters. */
2191 c = &btv->crop[!!fh->do_crop];
2192
2193 if (fh->do_crop
2194 && adjust_size
2195 && adjust_crop
2196 && !locked_btres(btv, VIDEO_RESOURCES)) {
2197 min_width = 48;
2198 min_height = 32;
2199
2200 /* We cannot scale up. When the scaled image is larger
2201 than crop.rect we adjust the crop.rect as required
2202 by the V4L2 spec, hence cropcap.bounds are our limit. */
2203 max_width = min(b->width, (__s32) MAX_HACTIVE);
2204 max_height = b->height;
2205
2206 /* We cannot capture the same line as video and VBI data.
2207 Note btv->vbi_end is really a minimum, see
2208 bttv_vbi_try_fmt(). */
2209 if (btv->vbi_end > b->top) {
2210 max_height -= btv->vbi_end - b->top;
2211 rc = -EBUSY;
2212 if (min_height > max_height)
2213 goto fail;
2214 }
2215 } else {
2216 rc = -EBUSY;
2217 if (btv->vbi_end > c->rect.top)
2218 goto fail;
2219
2220 min_width = c->min_scaled_width;
2221 min_height = c->min_scaled_height;
2222 max_width = c->max_scaled_width;
2223 max_height = c->max_scaled_height;
2224
2225 adjust_crop = 0;
2226 }
2227
2228 min_width = (min_width - width_mask - 1) & width_mask;
2229 max_width = max_width & width_mask;
2230
2231 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2232 min_height = min_height;
2233 /* Min. scale factor is 1:1. */
2234 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2235
2236 if (adjust_size) {
2237 *width = clamp(*width, min_width, max_width);
2238 *height = clamp(*height, min_height, max_height);
2239
2240 /* Round after clamping to avoid overflow. */
2241 *width = (*width + width_bias) & width_mask;
2242
2243 if (adjust_crop) {
2244 bttv_crop_adjust(c, b, *width, *height, field);
2245
2246 if (btv->vbi_end > c->rect.top) {
2247 /* Move the crop window out of the way. */
2248 c->rect.top = btv->vbi_end;
2249 }
2250 }
2251 } else {
2252 rc = -EINVAL;
2253 if (*width < min_width ||
2254 *height < min_height ||
2255 *width > max_width ||
2256 *height > max_height ||
2257 0 != (*width & ~width_mask))
2258 goto fail;
2259 }
2260
2261 rc = 0; /* success */
2262
2263 fail:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002264
2265 return rc;
2266}
2267
2268/* Returns an error if the given overlay window dimensions are not
2269 possible with the current cropping parameters. If adjust_size is
2270 TRUE the function may adjust the window width and/or height
2271 instead, however it always rounds the horizontal position and
2272 width as btcx_align() does. If adjust_crop is TRUE the function
2273 may also adjust the current cropping parameters to get closer
2274 to the desired window size. */
2275static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002276verify_window_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002277 struct v4l2_window * win,
2278 int adjust_size,
2279 int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
2281 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002282 unsigned int width_mask;
2283 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 if (win->w.width < 48 || win->w.height < 32)
2286 return -EINVAL;
2287 if (win->clipcount > 2048)
2288 return -EINVAL;
2289
2290 field = win->field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 if (V4L2_FIELD_ANY == field) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002293 __s32 height2;
2294
2295 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2296 field = (win->w.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 ? V4L2_FIELD_INTERLACED
2298 : V4L2_FIELD_TOP;
2299 }
2300 switch (field) {
2301 case V4L2_FIELD_TOP:
2302 case V4L2_FIELD_BOTTOM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 case V4L2_FIELD_INTERLACED:
2304 break;
2305 default:
2306 return -EINVAL;
2307 }
2308
Michael Schimeke5bd0262007-01-18 16:17:39 -03002309 /* 4-byte alignment. */
2310 if (NULL == fh->ovfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002312 width_mask = ~0;
2313 switch (fh->ovfmt->depth) {
2314 case 8:
2315 case 24:
2316 width_mask = ~3;
2317 break;
2318 case 16:
2319 width_mask = ~1;
2320 break;
2321 case 32:
2322 break;
2323 default:
2324 BUG();
2325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Michael Schimeke5bd0262007-01-18 16:17:39 -03002327 win->w.width -= win->w.left & ~width_mask;
2328 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2329
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002330 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002331 field, width_mask,
2332 /* width_bias: round down */ 0,
2333 adjust_size, adjust_crop);
2334 if (0 != rc)
2335 return rc;
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 win->field = field;
2338 return 0;
2339}
2340
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002341static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 struct v4l2_window *win, int fixup)
2343{
2344 struct v4l2_clip *clips = NULL;
2345 int n,size,retval = 0;
2346
2347 if (NULL == fh->ovfmt)
2348 return -EINVAL;
2349 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2350 return -EINVAL;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002351 retval = verify_window_lock(fh, win,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002352 /* adjust_size */ fixup,
2353 /* adjust_crop */ fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (0 != retval)
2355 return retval;
2356
2357 /* copy clips -- luckily v4l1 + v4l2 are binary
2358 compatible here ...*/
2359 n = win->clipcount;
2360 size = sizeof(*clips)*(n+4);
2361 clips = kmalloc(size,GFP_KERNEL);
2362 if (NULL == clips)
2363 return -ENOMEM;
2364 if (n > 0) {
2365 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2366 kfree(clips);
2367 return -EFAULT;
2368 }
2369 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 /* clip against screen */
2372 if (NULL != btv->fbuf.base)
2373 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2374 &win->w, clips, n);
2375 btcx_sort_clips(clips,n);
2376
2377 /* 4-byte alignments */
2378 switch (fh->ovfmt->depth) {
2379 case 8:
2380 case 24:
2381 btcx_align(&win->w, clips, n, 3);
2382 break;
2383 case 16:
2384 btcx_align(&win->w, clips, n, 1);
2385 break;
2386 case 32:
2387 /* no alignment fixups needed */
2388 break;
2389 default:
2390 BUG();
2391 }
2392
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002393 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 fh->ov.clips = clips;
2395 fh->ov.nclips = n;
2396
2397 fh->ov.w = win->w;
2398 fh->ov.field = win->field;
2399 fh->ov.setup_ok = 1;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 btv->init.ov.w.width = win->w.width;
2402 btv->init.ov.w.height = win->w.height;
2403 btv->init.ov.field = win->field;
2404
2405 /* update overlay if needed */
2406 retval = 0;
2407 if (check_btres(fh, RESOURCE_OVERLAY)) {
2408 struct bttv_buffer *new;
2409
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002410 new = videobuf_sg_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03002411 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2413 retval = bttv_switch_overlay(btv,fh,new);
2414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return retval;
2416}
2417
2418/* ----------------------------------------------------------------------- */
2419
2420static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2421{
2422 struct videobuf_queue* q = NULL;
2423
2424 switch (fh->type) {
2425 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2426 q = &fh->cap;
2427 break;
2428 case V4L2_BUF_TYPE_VBI_CAPTURE:
2429 q = &fh->vbi;
2430 break;
2431 default:
2432 BUG();
2433 }
2434 return q;
2435}
2436
2437static int bttv_resource(struct bttv_fh *fh)
2438{
2439 int res = 0;
2440
2441 switch (fh->type) {
2442 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002443 res = RESOURCE_VIDEO_STREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 break;
2445 case V4L2_BUF_TYPE_VBI_CAPTURE:
2446 res = RESOURCE_VBI;
2447 break;
2448 default:
2449 BUG();
2450 }
2451 return res;
2452}
2453
2454static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2455{
2456 struct videobuf_queue *q = bttv_queue(fh);
2457 int res = bttv_resource(fh);
2458
2459 if (check_btres(fh,res))
2460 return -EBUSY;
2461 if (videobuf_queue_is_busy(q))
2462 return -EBUSY;
2463 fh->type = type;
2464 return 0;
2465}
2466
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002467static void
2468pix_format_set_size (struct v4l2_pix_format * f,
2469 const struct bttv_format * fmt,
2470 unsigned int width,
2471 unsigned int height)
2472{
2473 f->width = width;
2474 f->height = height;
2475
2476 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2477 f->bytesperline = width; /* Y plane */
2478 f->sizeimage = (width * height * fmt->depth) >> 3;
2479 } else {
2480 f->bytesperline = (width * fmt->depth) >> 3;
2481 f->sizeimage = height * f->bytesperline;
2482 }
2483}
2484
Hans Verkuil78b526a2008-05-28 12:16:41 -03002485static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002486 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002488 struct bttv_fh *fh = priv;
2489
2490 pix_format_set_size(&f->fmt.pix, fh->fmt,
2491 fh->width, fh->height);
2492 f->fmt.pix.field = fh->cap.field;
2493 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2494
2495 return 0;
2496}
2497
Hans Verkuil78b526a2008-05-28 12:16:41 -03002498static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002499 struct v4l2_format *f)
2500{
2501 struct bttv_fh *fh = priv;
2502
2503 f->fmt.win.w = fh->ov.w;
2504 f->fmt.win.field = fh->ov.field;
2505
2506 return 0;
2507}
2508
Hans Verkuil78b526a2008-05-28 12:16:41 -03002509static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002510 struct v4l2_format *f)
2511{
2512 const struct bttv_format *fmt;
2513 struct bttv_fh *fh = priv;
2514 struct bttv *btv = fh->btv;
2515 enum v4l2_field field;
2516 __s32 width, height;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002517 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002518
2519 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2520 if (NULL == fmt)
2521 return -EINVAL;
2522
2523 field = f->fmt.pix.field;
2524
2525 if (V4L2_FIELD_ANY == field) {
2526 __s32 height2;
2527
2528 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2529 field = (f->fmt.pix.height > height2)
2530 ? V4L2_FIELD_INTERLACED
2531 : V4L2_FIELD_BOTTOM;
2532 }
2533
2534 if (V4L2_FIELD_SEQ_BT == field)
2535 field = V4L2_FIELD_SEQ_TB;
2536
2537 switch (field) {
2538 case V4L2_FIELD_TOP:
2539 case V4L2_FIELD_BOTTOM:
2540 case V4L2_FIELD_ALTERNATE:
2541 case V4L2_FIELD_INTERLACED:
2542 break;
2543 case V4L2_FIELD_SEQ_TB:
2544 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2545 return -EINVAL;
2546 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 default:
2548 return -EINVAL;
2549 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002550
2551 width = f->fmt.pix.width;
2552 height = f->fmt.pix.height;
2553
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002554 rc = limit_scaled_size_lock(fh, &width, &height, field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002555 /* width_mask: 4 pixels */ ~3,
2556 /* width_bias: nearest */ 2,
2557 /* adjust_size */ 1,
2558 /* adjust_crop */ 0);
2559 if (0 != rc)
2560 return rc;
2561
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002562 /* update data for the application */
2563 f->fmt.pix.field = field;
2564 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2565
2566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
Hans Verkuil78b526a2008-05-28 12:16:41 -03002569static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002570 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002572 struct bttv_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002574 return verify_window_lock(fh, &f->fmt.win,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002575 /* adjust_size */ 1,
2576 /* adjust_crop */ 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
Hans Verkuil78b526a2008-05-28 12:16:41 -03002579static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002580 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
2582 int retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002583 const struct bttv_format *fmt;
2584 struct bttv_fh *fh = priv;
2585 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002586 __s32 width, height;
2587 enum v4l2_field field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002589 retval = bttv_switch_type(fh, f->type);
2590 if (0 != retval)
2591 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Hans Verkuil78b526a2008-05-28 12:16:41 -03002593 retval = bttv_try_fmt_vid_cap(file, priv, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002594 if (0 != retval)
2595 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002597 width = f->fmt.pix.width;
2598 height = f->fmt.pix.height;
2599 field = f->fmt.pix.field;
2600
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002601 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002602 /* width_mask: 4 pixels */ ~3,
2603 /* width_bias: nearest */ 2,
2604 /* adjust_size */ 1,
2605 /* adjust_crop */ 1);
2606 if (0 != retval)
2607 return retval;
2608
2609 f->fmt.pix.field = field;
2610
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002611 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002613 /* update our state informations */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002614 fh->fmt = fmt;
2615 fh->cap.field = f->fmt.pix.field;
2616 fh->cap.last = V4L2_FIELD_NONE;
2617 fh->width = f->fmt.pix.width;
2618 fh->height = f->fmt.pix.height;
2619 btv->init.fmt = fmt;
2620 btv->init.width = f->fmt.pix.width;
2621 btv->init.height = f->fmt.pix.height;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002622
2623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624}
2625
Hans Verkuil78b526a2008-05-28 12:16:41 -03002626static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002627 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002629 struct bttv_fh *fh = priv;
2630 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002632 if (no_overlay > 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03002633 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002634 return -EINVAL;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002635 }
Michael Krufky5e453dc2006-01-09 15:32:31 -02002636
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002637 return setup_window_lock(fh, btv, &f->fmt.win, 1);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002638}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002640static int bttv_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002641 struct v4l2_capability *cap)
2642{
2643 struct bttv_fh *fh = priv;
2644 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002646 if (0 == v4l2)
2647 return -EINVAL;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002648
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002649 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2650 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2651 snprintf(cap->bus_info, sizeof(cap->bus_info),
2652 "PCI:%s", pci_name(btv->c.pci));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002653 cap->capabilities =
2654 V4L2_CAP_VIDEO_CAPTURE |
2655 V4L2_CAP_VBI_CAPTURE |
2656 V4L2_CAP_READWRITE |
2657 V4L2_CAP_STREAMING;
2658 if (no_overlay <= 0)
2659 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002661 /*
2662 * No need to lock here: those vars are initialized during board
2663 * probe and remains untouched during the rest of the driver lifecycle
2664 */
2665 if (btv->has_saa6588)
2666 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
Trent Piephoabb03622009-01-28 21:32:59 -03002667 if (btv->tuner_type != TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002668 cap->capabilities |= V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 return 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002670}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002672static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2673{
2674 int index = -1, i;
2675
2676 for (i = 0; i < FORMATS; i++) {
2677 if (formats[i].fourcc != -1)
2678 index++;
2679 if ((unsigned int)index == f->index)
2680 break;
2681 }
2682 if (FORMATS == i)
2683 return -EINVAL;
2684
2685 f->pixelformat = formats[i].fourcc;
2686 strlcpy(f->description, formats[i].name, sizeof(f->description));
2687
2688 return i;
2689}
2690
Hans Verkuil78b526a2008-05-28 12:16:41 -03002691static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002692 struct v4l2_fmtdesc *f)
2693{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002694 int rc = bttv_enum_fmt_cap_ovr(f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002695
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002696 if (rc < 0)
2697 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002698
2699 return 0;
2700}
2701
Hans Verkuil78b526a2008-05-28 12:16:41 -03002702static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002703 struct v4l2_fmtdesc *f)
2704{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002705 int rc;
2706
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002707 if (no_overlay > 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03002708 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002709 return -EINVAL;
2710 }
2711
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002712 rc = bttv_enum_fmt_cap_ovr(f);
2713
2714 if (rc < 0)
2715 return rc;
2716
2717 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002718 return -EINVAL;
2719
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002720 return 0;
2721}
2722
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002723static int bttv_g_fbuf(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002724 struct v4l2_framebuffer *fb)
2725{
2726 struct bttv_fh *fh = f;
2727 struct bttv *btv = fh->btv;
2728
2729 *fb = btv->fbuf;
2730 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2731 if (fh->ovfmt)
2732 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2733 return 0;
2734}
2735
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002736static int bttv_overlay(struct file *file, void *f, unsigned int on)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002737{
2738 struct bttv_fh *fh = f;
2739 struct bttv *btv = fh->btv;
2740 struct bttv_buffer *new;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002741 int retval = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002742
2743 if (on) {
2744 /* verify args */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002745 if (unlikely(!btv->fbuf.base)) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002746 return -EINVAL;
2747 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002748 if (unlikely(!fh->ov.setup_ok)) {
Joe Perches8af443e2011-08-21 19:56:48 -03002749 dprintk("%d: overlay: !setup_ok\n", btv->c.nr);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002750 retval = -EINVAL;
2751 }
2752 if (retval)
2753 return retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002754 }
2755
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002756 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002757 return -EBUSY;
2758
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002759 if (on) {
2760 fh->ov.tvnorm = btv->tvnorm;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002761 new = videobuf_sg_alloc(sizeof(*new));
Robert Fitzsimons7c018802008-02-13 16:38:11 -03002762 new->crop = btv->crop[!!fh->do_crop].rect;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002763 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2764 } else {
2765 new = NULL;
2766 }
2767
2768 /* switch over */
2769 retval = bttv_switch_overlay(btv, fh, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return retval;
2771}
2772
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002773static int bttv_s_fbuf(struct file *file, void *f,
Hans Verkuile6eb28c2012-09-04 10:26:45 -03002774 const struct v4l2_framebuffer *fb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002776 struct bttv_fh *fh = f;
2777 struct bttv *btv = fh->btv;
2778 const struct bttv_format *fmt;
2779 int retval;
2780
2781 if (!capable(CAP_SYS_ADMIN) &&
2782 !capable(CAP_SYS_RAWIO))
2783 return -EPERM;
2784
2785 /* check args */
2786 fmt = format_by_fourcc(fb->fmt.pixelformat);
2787 if (NULL == fmt)
2788 return -EINVAL;
2789 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2790 return -EINVAL;
2791
2792 retval = -EINVAL;
2793 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2794 __s32 width = fb->fmt.width;
2795 __s32 height = fb->fmt.height;
2796
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002797 retval = limit_scaled_size_lock(fh, &width, &height,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002798 V4L2_FIELD_INTERLACED,
2799 /* width_mask */ ~3,
2800 /* width_bias */ 2,
2801 /* adjust_size */ 0,
2802 /* adjust_crop */ 0);
2803 if (0 != retval)
2804 return retval;
2805 }
2806
2807 /* ok, accept it */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002808 btv->fbuf.base = fb->base;
2809 btv->fbuf.fmt.width = fb->fmt.width;
2810 btv->fbuf.fmt.height = fb->fmt.height;
2811 if (0 != fb->fmt.bytesperline)
2812 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2813 else
2814 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2815
2816 retval = 0;
2817 fh->ovfmt = fmt;
2818 btv->init.ovfmt = fmt;
2819 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2820 fh->ov.w.left = 0;
2821 fh->ov.w.top = 0;
2822 fh->ov.w.width = fb->fmt.width;
2823 fh->ov.w.height = fb->fmt.height;
2824 btv->init.ov.w.width = fb->fmt.width;
2825 btv->init.ov.w.height = fb->fmt.height;
2826 kfree(fh->ov.clips);
2827 fh->ov.clips = NULL;
2828 fh->ov.nclips = 0;
2829
2830 if (check_btres(fh, RESOURCE_OVERLAY)) {
2831 struct bttv_buffer *new;
2832
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002833 new = videobuf_sg_alloc(sizeof(*new));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002834 new->crop = btv->crop[!!fh->do_crop].rect;
2835 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2836 retval = bttv_switch_overlay(btv, fh, new);
2837 }
2838 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002839 return retval;
2840}
2841
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002842static int bttv_reqbufs(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002843 struct v4l2_requestbuffers *p)
2844{
2845 struct bttv_fh *fh = priv;
2846 return videobuf_reqbufs(bttv_queue(fh), p);
2847}
2848
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002849static int bttv_querybuf(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002850 struct v4l2_buffer *b)
2851{
2852 struct bttv_fh *fh = priv;
2853 return videobuf_querybuf(bttv_queue(fh), b);
2854}
2855
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002856static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002857{
2858 struct bttv_fh *fh = priv;
2859 struct bttv *btv = fh->btv;
2860 int res = bttv_resource(fh);
2861
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002862 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002863 return -EBUSY;
2864
2865 return videobuf_qbuf(bttv_queue(fh), b);
2866}
2867
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002868static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002869{
2870 struct bttv_fh *fh = priv;
2871 return videobuf_dqbuf(bttv_queue(fh), b,
2872 file->f_flags & O_NONBLOCK);
2873}
2874
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002875static int bttv_streamon(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002876 enum v4l2_buf_type type)
2877{
2878 struct bttv_fh *fh = priv;
2879 struct bttv *btv = fh->btv;
2880 int res = bttv_resource(fh);
2881
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002882 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002883 return -EBUSY;
2884 return videobuf_streamon(bttv_queue(fh));
2885}
2886
2887
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002888static int bttv_streamoff(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002889 enum v4l2_buf_type type)
2890{
2891 struct bttv_fh *fh = priv;
2892 struct bttv *btv = fh->btv;
2893 int retval;
2894 int res = bttv_resource(fh);
2895
2896
2897 retval = videobuf_streamoff(bttv_queue(fh));
2898 if (retval < 0)
2899 return retval;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002900 free_btres_lock(btv, fh, res);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002901 return 0;
2902}
2903
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002904static int bttv_queryctrl(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002905 struct v4l2_queryctrl *c)
2906{
2907 struct bttv_fh *fh = priv;
2908 struct bttv *btv = fh->btv;
2909 const struct v4l2_queryctrl *ctrl;
2910
2911 if ((c->id < V4L2_CID_BASE ||
2912 c->id >= V4L2_CID_LASTP1) &&
2913 (c->id < V4L2_CID_PRIVATE_BASE ||
2914 c->id >= V4L2_CID_PRIVATE_LASTP1))
2915 return -EINVAL;
2916
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002917 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2918 *c = no_ctl;
2919 else {
2920 ctrl = ctrl_by_id(c->id);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002921
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002922 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2923 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002924
2925 return 0;
2926}
2927
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002928static int bttv_g_parm(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002929 struct v4l2_streamparm *parm)
2930{
2931 struct bttv_fh *fh = f;
2932 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002933
Trent Piepho51f0b8d52009-03-04 01:21:02 -03002934 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2935 &parm->parm.capture.timeperframe);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002936
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002937 return 0;
2938}
2939
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002940static int bttv_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002941 struct v4l2_tuner *t)
2942{
2943 struct bttv_fh *fh = priv;
2944 struct bttv *btv = fh->btv;
2945
Trent Piephoabb03622009-01-28 21:32:59 -03002946 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002947 return -EINVAL;
2948 if (0 != t->index)
2949 return -EINVAL;
2950
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002951 t->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil859f0272009-03-28 08:29:00 -03002952 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002953 strcpy(t->name, "Television");
2954 t->capability = V4L2_TUNER_CAP_NORM;
2955 t->type = V4L2_TUNER_ANALOG_TV;
2956 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2957 t->signal = 0xffff;
2958
2959 if (btv->audio_mode_gpio)
2960 btv->audio_mode_gpio(btv, t, 0);
2961
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002962 return 0;
2963}
2964
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002965static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002966{
2967 struct bttv_fh *fh = f;
2968 struct bttv *btv = fh->btv;
2969
2970 *p = v4l2_prio_max(&btv->prio);
2971
2972 return 0;
2973}
2974
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002975static int bttv_s_priority(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002976 enum v4l2_priority prio)
2977{
2978 struct bttv_fh *fh = f;
2979 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002980 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002981
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002982 rc = v4l2_prio_change(&btv->prio, &fh->prio, prio);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002983
2984 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002985}
2986
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002987static int bttv_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002988 struct v4l2_cropcap *cap)
2989{
2990 struct bttv_fh *fh = priv;
2991 struct bttv *btv = fh->btv;
2992
2993 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2994 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2995 return -EINVAL;
2996
2997 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
2998
2999 return 0;
3000}
3001
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003002static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003003{
3004 struct bttv_fh *fh = f;
3005 struct bttv *btv = fh->btv;
3006
3007 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3008 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3009 return -EINVAL;
3010
3011 /* No fh->do_crop = 1; because btv->crop[1] may be
3012 inconsistent with fh->width or fh->height and apps
3013 do not expect a change here. */
3014
3015 crop->c = btv->crop[!!fh->do_crop].rect;
3016
3017 return 0;
3018}
3019
Hans Verkuil4f996592012-09-05 05:10:48 -03003020static int bttv_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003021{
3022 struct bttv_fh *fh = f;
3023 struct bttv *btv = fh->btv;
3024 const struct v4l2_rect *b;
3025 int retval;
3026 struct bttv_crop c;
3027 __s32 b_left;
3028 __s32 b_top;
3029 __s32 b_right;
3030 __s32 b_bottom;
3031
3032 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3033 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3034 return -EINVAL;
3035
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003036 /* Make sure tvnorm, vbi_end and the current cropping
3037 parameters remain consistent until we're done. Note
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003038 read() may change vbi_end in check_alloc_btres_lock(). */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003039 retval = v4l2_prio_check(&btv->prio, fh->prio);
3040 if (0 != retval) {
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003041 return retval;
3042 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003043
3044 retval = -EBUSY;
3045
3046 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003047 return retval;
3048 }
3049
3050 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3051
3052 b_left = b->left;
3053 b_right = b_left + b->width;
3054 b_bottom = b->top + b->height;
3055
3056 b_top = max(b->top, btv->vbi_end);
3057 if (b_top + 32 >= b_bottom) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003058 return retval;
3059 }
3060
3061 /* Min. scaled size 48 x 32. */
Hans Verkuil4f996592012-09-05 05:10:48 -03003062 c.rect.left = clamp_t(s32, crop->c.left, b_left, b_right - 48);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003063 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3064
Hans Verkuil4f996592012-09-05 05:10:48 -03003065 c.rect.width = clamp_t(s32, crop->c.width,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003066 48, b_right - c.rect.left);
3067
Hans Verkuil4f996592012-09-05 05:10:48 -03003068 c.rect.top = clamp_t(s32, crop->c.top, b_top, b_bottom - 32);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003069 /* Top and height must be a multiple of two. */
3070 c.rect.top = (c.rect.top + 1) & ~1;
3071
Hans Verkuil4f996592012-09-05 05:10:48 -03003072 c.rect.height = clamp_t(s32, crop->c.height,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003073 32, b_bottom - c.rect.top);
3074 c.rect.height = (c.rect.height + 1) & ~1;
3075
3076 bttv_crop_calc_limits(&c);
3077
3078 btv->crop[1] = c;
3079
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003080 fh->do_crop = 1;
3081
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003082 if (fh->width < c.min_scaled_width) {
3083 fh->width = c.min_scaled_width;
3084 btv->init.width = c.min_scaled_width;
3085 } else if (fh->width > c.max_scaled_width) {
3086 fh->width = c.max_scaled_width;
3087 btv->init.width = c.max_scaled_width;
3088 }
3089
3090 if (fh->height < c.min_scaled_height) {
3091 fh->height = c.min_scaled_height;
3092 btv->init.height = c.min_scaled_height;
3093 } else if (fh->height > c.max_scaled_height) {
3094 fh->height = c.max_scaled_height;
3095 btv->init.height = c.max_scaled_height;
3096 }
3097
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003098 return 0;
3099}
3100
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003101static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003102{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003103 if (unlikely(a->index))
3104 return -EINVAL;
3105
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003106 strcpy(a->name, "audio");
3107 return 0;
3108}
3109
Hans Verkuil0e8025b92012-09-04 11:59:31 -03003110static int bttv_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003111{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003112 if (unlikely(a->index))
3113 return -EINVAL;
3114
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116}
3117
3118static ssize_t bttv_read(struct file *file, char __user *data,
3119 size_t count, loff_t *ppos)
3120{
3121 struct bttv_fh *fh = file->private_data;
3122 int retval = 0;
3123
3124 if (fh->btv->errors)
3125 bttv_reinit_bt848(fh->btv);
Joe Perches8af443e2011-08-21 19:56:48 -03003126 dprintk("%d: read count=%d type=%s\n",
3127 fh->btv->c.nr, (int)count, v4l2_type_names[fh->type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 switch (fh->type) {
3130 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003131 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03003132 /* VIDEO_READ in use by another fh,
3133 or VIDEO_STREAM by any fh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return -EBUSY;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3137 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003138 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 break;
3140 case V4L2_BUF_TYPE_VBI_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003141 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return -EBUSY;
3143 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3144 file->f_flags & O_NONBLOCK);
3145 break;
3146 default:
3147 BUG();
3148 }
3149 return retval;
3150}
3151
3152static unsigned int bttv_poll(struct file *file, poll_table *wait)
3153{
3154 struct bttv_fh *fh = file->private_data;
3155 struct bttv_buffer *buf;
3156 enum v4l2_field field;
Figo.zhang9fd64182009-06-16 13:31:29 -03003157 unsigned int rc = POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
3159 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003160 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 return POLLERR;
3162 return videobuf_poll_stream(file, &fh->vbi, wait);
3163 }
3164
Michael Schimeke5bd0262007-01-18 16:17:39 -03003165 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 /* streaming capture */
3167 if (list_empty(&fh->cap.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -03003168 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3170 } else {
3171 /* read() capture */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 if (NULL == fh->cap.read_buf) {
3173 /* need to capture a new frame */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003174 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3175 goto err;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003176 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003177 if (NULL == fh->cap.read_buf)
3178 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3180 field = videobuf_next_field(&fh->cap);
3181 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -08003182 kfree (fh->cap.read_buf);
3183 fh->cap.read_buf = NULL;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003184 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 }
3186 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3187 fh->cap.read_off = 0;
3188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 buf = (struct bttv_buffer*)fh->cap.read_buf;
3190 }
3191
3192 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03003193 if (buf->vb.state == VIDEOBUF_DONE ||
3194 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -03003195 rc = POLLIN|POLLRDNORM;
3196 else
3197 rc = 0;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003198err:
Figo.zhang9fd64182009-06-16 13:31:29 -03003199 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200}
3201
Hans Verkuilbec43662008-12-30 06:58:20 -03003202static int bttv_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003204 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003205 struct bttv *btv = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 struct bttv_fh *fh;
3207 enum v4l2_buf_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Joe Perches8af443e2011-08-21 19:56:48 -03003209 dprintk("open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Laurent Pinchart327ae592009-11-27 13:57:55 -03003211 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003212 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Laurent Pinchart327ae592009-11-27 13:57:55 -03003213 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003214 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3215 } else {
3216 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -03003218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Joe Perches8af443e2011-08-21 19:56:48 -03003220 dprintk("%d: open called (type=%s)\n",
3221 btv->c.nr, v4l2_type_names[type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
3223 /* allocate per filehandle data */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003224 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3225 if (unlikely(!fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return -ENOMEM;
3227 file->private_data = fh;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 *fh = btv->init;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 fh->type = type;
3232 fh->ov.setup_ok = 0;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003233
Hans Verkuilffb48772010-05-01 08:03:24 -03003234 v4l2_prio_open(&btv->prio, &fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003236 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3237 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3239 V4L2_FIELD_INTERLACED,
3240 sizeof(struct bttv_buffer),
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003241 fh, &btv->lock);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003242 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3243 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 V4L2_BUF_TYPE_VBI_CAPTURE,
3245 V4L2_FIELD_SEQ_TB,
3246 sizeof(struct bttv_buffer),
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003247 fh, &btv->lock);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03003248 set_tvnorm(btv,btv->tvnorm);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003249 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
3251 btv->users++;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003252
3253 /* The V4L2 spec requires one global set of cropping parameters
3254 which only change on request. These are stored in btv->crop[1].
3255 However for compatibility with V4L apps and cropping unaware
3256 V4L2 apps we now reset the cropping parameters as seen through
3257 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3258 will use btv->crop[0], the default cropping parameters for the
3259 current video standard, and VIDIOC_S_FMT will not implicitely
3260 change the cropping parameters until VIDIOC_S_CROP has been
3261 called. */
3262 fh->do_crop = !reset_crop; /* module parameter */
3263
3264 /* Likewise there should be one global set of VBI capture
3265 parameters, but for compatibility with V4L apps and earlier
3266 driver versions each fh has its own parameters. */
3267 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3268
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 bttv_field_count(btv);
3270 return 0;
3271}
3272
Hans Verkuilbec43662008-12-30 06:58:20 -03003273static int bttv_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
3275 struct bttv_fh *fh = file->private_data;
3276 struct bttv *btv = fh->btv;
3277
3278 /* turn off overlay */
3279 if (check_btres(fh, RESOURCE_OVERLAY))
3280 bttv_switch_overlay(btv,fh,NULL);
3281
3282 /* stop video capture */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003283 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 videobuf_streamoff(&fh->cap);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003285 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 }
3287 if (fh->cap.read_buf) {
3288 buffer_release(&fh->cap,fh->cap.read_buf);
3289 kfree(fh->cap.read_buf);
3290 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03003291 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003292 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
3295 /* stop vbi capture */
3296 if (check_btres(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03003297 videobuf_stop(&fh->vbi);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003298 free_btres_lock(btv,fh,RESOURCE_VBI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 }
3300
3301 /* free stuff */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 videobuf_mmap_free(&fh->cap);
3304 videobuf_mmap_free(&fh->vbi);
Hans Verkuilffb48772010-05-01 08:03:24 -03003305 v4l2_prio_close(&btv->prio, fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 file->private_data = NULL;
3307 kfree(fh);
3308
3309 btv->users--;
3310 bttv_field_count(btv);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003311
3312 if (!btv->users)
3313 audio_mute(btv, 1);
3314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 return 0;
3316}
3317
3318static int
3319bttv_mmap(struct file *file, struct vm_area_struct *vma)
3320{
3321 struct bttv_fh *fh = file->private_data;
3322
Joe Perches8af443e2011-08-21 19:56:48 -03003323 dprintk("%d: mmap type=%s 0x%lx+%ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 fh->btv->c.nr, v4l2_type_names[fh->type],
3325 vma->vm_start, vma->vm_end - vma->vm_start);
3326 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3327}
3328
Hans Verkuilbec43662008-12-30 06:58:20 -03003329static const struct v4l2_file_operations bttv_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
Mauro Carvalho Chehab8979e9d2010-09-15 08:22:09 -03003331 .owner = THIS_MODULE,
3332 .open = bttv_open,
3333 .release = bttv_release,
3334 .unlocked_ioctl = video_ioctl2,
3335 .read = bttv_read,
3336 .mmap = bttv_mmap,
3337 .poll = bttv_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338};
3339
Hans Verkuila3998102008-07-21 02:57:38 -03003340static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003341 .vidioc_querycap = bttv_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003342 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3343 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3344 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3345 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3346 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3347 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3348 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3349 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003350 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3351 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3352 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003353 .vidioc_g_audio = bttv_g_audio,
3354 .vidioc_s_audio = bttv_s_audio,
3355 .vidioc_cropcap = bttv_cropcap,
3356 .vidioc_reqbufs = bttv_reqbufs,
3357 .vidioc_querybuf = bttv_querybuf,
3358 .vidioc_qbuf = bttv_qbuf,
3359 .vidioc_dqbuf = bttv_dqbuf,
3360 .vidioc_s_std = bttv_s_std,
3361 .vidioc_enum_input = bttv_enum_input,
3362 .vidioc_g_input = bttv_g_input,
3363 .vidioc_s_input = bttv_s_input,
3364 .vidioc_queryctrl = bttv_queryctrl,
3365 .vidioc_g_ctrl = bttv_g_ctrl,
3366 .vidioc_s_ctrl = bttv_s_ctrl,
3367 .vidioc_streamon = bttv_streamon,
3368 .vidioc_streamoff = bttv_streamoff,
3369 .vidioc_g_tuner = bttv_g_tuner,
3370 .vidioc_s_tuner = bttv_s_tuner,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003371 .vidioc_g_crop = bttv_g_crop,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003372 .vidioc_s_crop = bttv_s_crop,
3373 .vidioc_g_fbuf = bttv_g_fbuf,
3374 .vidioc_s_fbuf = bttv_s_fbuf,
3375 .vidioc_overlay = bttv_overlay,
3376 .vidioc_g_priority = bttv_g_priority,
3377 .vidioc_s_priority = bttv_s_priority,
3378 .vidioc_g_parm = bttv_g_parm,
3379 .vidioc_g_frequency = bttv_g_frequency,
3380 .vidioc_s_frequency = bttv_s_frequency,
3381 .vidioc_log_status = bttv_log_status,
3382 .vidioc_querystd = bttv_querystd,
Zoltan Devai43846832008-01-14 13:24:38 -03003383#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003384 .vidioc_g_register = bttv_g_register,
3385 .vidioc_s_register = bttv_s_register,
Zoltan Devai43846832008-01-14 13:24:38 -03003386#endif
Hans Verkuila3998102008-07-21 02:57:38 -03003387};
3388
3389static struct video_device bttv_video_template = {
3390 .fops = &bttv_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003391 .ioctl_ops = &bttv_ioctl_ops,
3392 .tvnorms = BTTV_NORMS,
3393 .current_norm = V4L2_STD_PAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394};
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396/* ----------------------------------------------------------------------- */
3397/* radio interface */
3398
Hans Verkuilbec43662008-12-30 06:58:20 -03003399static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003401 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003402 struct bttv *btv = video_drvdata(file);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003403 struct bttv_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Joe Perches8af443e2011-08-21 19:56:48 -03003405 dprintk("open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Joe Perches8af443e2011-08-21 19:56:48 -03003407 dprintk("%d: open called (radio)\n", btv->c.nr);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003408
3409 /* allocate per filehandle data */
3410 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003411 if (unlikely(!fh))
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003412 return -ENOMEM;
3413 file->private_data = fh;
3414 *fh = btv->init;
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003415
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003416 v4l2_prio_open(&btv->prio, &fh->prio);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 btv->radio_user++;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003419
Hans Verkuil859f0272009-03-28 08:29:00 -03003420 bttv_call_all(btv, tuner, s_radio);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03003421 audio_input(btv,TVAUDIO_INPUT_RADIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424}
3425
Hans Verkuilbec43662008-12-30 06:58:20 -03003426static int radio_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003428 struct bttv_fh *fh = file->private_data;
3429 struct bttv *btv = fh->btv;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003430 struct saa6588_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
Hans Verkuilffb48772010-05-01 08:03:24 -03003432 v4l2_prio_close(&btv->prio, fh->prio);
Robert Fitzsimonsb9bc07a2008-04-10 09:40:31 -03003433 file->private_data = NULL;
3434 kfree(fh);
3435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 btv->radio_user--;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003437
Hans Verkuilb9218f22010-12-27 12:22:46 -03003438 bttv_call_all(btv, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003439
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 return 0;
3441}
3442
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003443static int radio_querycap(struct file *file, void *priv,
3444 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003446 struct bttv_fh *fh = priv;
3447 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003449 strcpy(cap->driver, "bttv");
3450 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
3451 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003452 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 return 0;
3455}
3456
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003457static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003459 struct bttv_fh *fh = priv;
3460 struct bttv *btv = fh->btv;
3461
Trent Piephoabb03622009-01-28 21:32:59 -03003462 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003463 return -EINVAL;
3464 if (0 != t->index)
3465 return -EINVAL;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003466 strcpy(t->name, "Radio");
3467 t->type = V4L2_TUNER_RADIO;
3468
Hans Verkuil859f0272009-03-28 08:29:00 -03003469 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003470
3471 if (btv->audio_mode_gpio)
3472 btv->audio_mode_gpio(btv, t, 0);
3473
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003474 return 0;
3475}
3476
3477static int radio_enum_input(struct file *file, void *priv,
3478 struct v4l2_input *i)
3479{
3480 if (i->index != 0)
3481 return -EINVAL;
3482
3483 strcpy(i->name, "Radio");
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003484 i->type = V4L2_INPUT_TYPE_TUNER;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003485
3486 return 0;
3487}
3488
3489static int radio_g_audio(struct file *file, void *priv,
3490 struct v4l2_audio *a)
3491{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003492 if (unlikely(a->index))
Cyrill Gorcunov1a002eb2008-04-01 17:49:13 -03003493 return -EINVAL;
3494
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003495 strcpy(a->name, "Radio");
Cyrill Gorcunov1a002eb2008-04-01 17:49:13 -03003496
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003497 return 0;
3498}
3499
3500static int radio_s_tuner(struct file *file, void *priv,
3501 struct v4l2_tuner *t)
3502{
3503 struct bttv_fh *fh = priv;
3504 struct bttv *btv = fh->btv;
3505
3506 if (0 != t->index)
3507 return -EINVAL;
3508
Hans Verkuila024c1a2011-06-12 07:02:43 -03003509 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003510 return 0;
3511}
3512
3513static int radio_s_audio(struct file *file, void *priv,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03003514 const struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003515{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003516 if (unlikely(a->index))
3517 return -EINVAL;
3518
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003519 return 0;
3520}
3521
3522static int radio_s_input(struct file *filp, void *priv, unsigned int i)
3523{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003524 if (unlikely(i))
3525 return -EINVAL;
3526
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003527 return 0;
3528}
3529
3530static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
3531{
3532 return 0;
3533}
3534
3535static int radio_queryctrl(struct file *file, void *priv,
3536 struct v4l2_queryctrl *c)
3537{
3538 const struct v4l2_queryctrl *ctrl;
3539
3540 if (c->id < V4L2_CID_BASE ||
3541 c->id >= V4L2_CID_LASTP1)
3542 return -EINVAL;
3543
3544 if (c->id == V4L2_CID_AUDIO_MUTE) {
3545 ctrl = ctrl_by_id(c->id);
3546 *c = *ctrl;
3547 } else
3548 *c = no_ctl;
3549
3550 return 0;
3551}
3552
3553static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
3554{
3555 *i = 0;
3556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557}
3558
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003559static ssize_t radio_read(struct file *file, char __user *data,
3560 size_t count, loff_t *ppos)
3561{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003562 struct bttv_fh *fh = file->private_data;
3563 struct bttv *btv = fh->btv;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003564 struct saa6588_command cmd;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003565 cmd.block_count = count/3;
3566 cmd.buffer = data;
3567 cmd.instance = file;
3568 cmd.result = -ENODEV;
3569
Hans Verkuilb9218f22010-12-27 12:22:46 -03003570 bttv_call_all(btv, core, ioctl, SAA6588_CMD_READ, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003571
3572 return cmd.result;
3573}
3574
3575static unsigned int radio_poll(struct file *file, poll_table *wait)
3576{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003577 struct bttv_fh *fh = file->private_data;
3578 struct bttv *btv = fh->btv;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003579 struct saa6588_command cmd;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003580 cmd.instance = file;
3581 cmd.event_list = wait;
3582 cmd.result = -ENODEV;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003583 bttv_call_all(btv, core, ioctl, SAA6588_CMD_POLL, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003584
3585 return cmd.result;
3586}
3587
Hans Verkuilbec43662008-12-30 06:58:20 -03003588static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589{
3590 .owner = THIS_MODULE,
3591 .open = radio_open,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003592 .read = radio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 .release = radio_release,
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003594 .unlocked_ioctl = video_ioctl2,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003595 .poll = radio_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596};
3597
Hans Verkuila3998102008-07-21 02:57:38 -03003598static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003599 .vidioc_querycap = radio_querycap,
3600 .vidioc_g_tuner = radio_g_tuner,
3601 .vidioc_enum_input = radio_enum_input,
3602 .vidioc_g_audio = radio_g_audio,
3603 .vidioc_s_tuner = radio_s_tuner,
3604 .vidioc_s_audio = radio_s_audio,
3605 .vidioc_s_input = radio_s_input,
3606 .vidioc_s_std = radio_s_std,
3607 .vidioc_queryctrl = radio_queryctrl,
3608 .vidioc_g_input = radio_g_input,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003609 .vidioc_g_ctrl = bttv_g_ctrl,
3610 .vidioc_s_ctrl = bttv_s_ctrl,
3611 .vidioc_g_frequency = bttv_g_frequency,
3612 .vidioc_s_frequency = bttv_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613};
3614
Hans Verkuila3998102008-07-21 02:57:38 -03003615static struct video_device radio_template = {
3616 .fops = &radio_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003617 .ioctl_ops = &radio_ioctl_ops,
3618};
3619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620/* ----------------------------------------------------------------------- */
3621/* some debug code */
3622
Adrian Bunk408b6642005-05-01 08:59:29 -07003623static int bttv_risc_decode(u32 risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
3625 static char *instr[16] = {
3626 [ BT848_RISC_WRITE >> 28 ] = "write",
3627 [ BT848_RISC_SKIP >> 28 ] = "skip",
3628 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3629 [ BT848_RISC_JUMP >> 28 ] = "jump",
3630 [ BT848_RISC_SYNC >> 28 ] = "sync",
3631 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3632 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3633 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3634 };
3635 static int incr[16] = {
3636 [ BT848_RISC_WRITE >> 28 ] = 2,
3637 [ BT848_RISC_JUMP >> 28 ] = 2,
3638 [ BT848_RISC_SYNC >> 28 ] = 2,
3639 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3640 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3641 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3642 };
3643 static char *bits[] = {
3644 "be0", "be1", "be2", "be3/resync",
3645 "set0", "set1", "set2", "set3",
3646 "clr0", "clr1", "clr2", "clr3",
3647 "irq", "res", "eol", "sol",
3648 };
3649 int i;
3650
Joe Perches8af443e2011-08-21 19:56:48 -03003651 pr_cont("0x%08x [ %s", risc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3653 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3654 if (risc & (1 << (i + 12)))
Joe Perches8af443e2011-08-21 19:56:48 -03003655 pr_cont(" %s", bits[i]);
3656 pr_cont(" count=%d ]\n", risc & 0xfff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3658}
3659
Adrian Bunk408b6642005-05-01 08:59:29 -07003660static void bttv_risc_disasm(struct bttv *btv,
3661 struct btcx_riscmem *risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
3663 unsigned int i,j,n;
3664
Joe Perches8af443e2011-08-21 19:56:48 -03003665 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
3666 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 for (i = 0; i < (risc->size >> 2); i += n) {
Joe Perches8af443e2011-08-21 19:56:48 -03003668 pr_info("%s: 0x%lx: ",
3669 btv->c.v4l2_dev.name,
3670 (unsigned long)(risc->dma + (i<<2)));
Al Viro3aa71102008-06-22 14:20:09 -03003671 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 for (j = 1; j < n; j++)
Joe Perches8af443e2011-08-21 19:56:48 -03003673 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3674 btv->c.v4l2_dev.name,
3675 (unsigned long)(risc->dma + ((i+j)<<2)),
3676 risc->cpu[i+j], j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 if (0 == risc->cpu[i])
3678 break;
3679 }
3680}
3681
3682static void bttv_print_riscaddr(struct bttv *btv)
3683{
Joe Perches8af443e2011-08-21 19:56:48 -03003684 pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
3685 pr_info(" vbi : o=%08llx e=%08llx\n",
3686 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3687 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3688 pr_info(" cap : o=%08llx e=%08llx\n",
3689 btv->curr.top
3690 ? (unsigned long long)btv->curr.top->top.dma : 0,
3691 btv->curr.bottom
3692 ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3693 pr_info(" scr : o=%08llx e=%08llx\n",
3694 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3695 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 bttv_risc_disasm(btv, &btv->main);
3697}
3698
3699/* ----------------------------------------------------------------------- */
3700/* irq handler */
3701
3702static char *irq_name[] = {
3703 "FMTCHG", // format change detected (525 vs. 625)
3704 "VSYNC", // vertical sync (new field)
3705 "HSYNC", // horizontal sync
3706 "OFLOW", // chroma/luma AGC overflow
3707 "HLOCK", // horizontal lock changed
3708 "VPRES", // video presence changed
3709 "6", "7",
3710 "I2CDONE", // hw irc operation finished
3711 "GPINT", // gpio port triggered irq
3712 "10",
3713 "RISCI", // risc instruction triggered irq
3714 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3715 "FTRGT", // pixel data fifo overrun
3716 "FDSR", // fifo data stream resyncronisation
3717 "PPERR", // parity error (data transfer)
3718 "RIPERR", // parity error (read risc instructions)
3719 "PABORT", // pci abort
3720 "OCERR", // risc instruction error
3721 "SCERR", // syncronisation error
3722};
3723
3724static void bttv_print_irqbits(u32 print, u32 mark)
3725{
3726 unsigned int i;
3727
Joe Perches8af443e2011-08-21 19:56:48 -03003728 pr_cont("bits:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3730 if (print & (1 << i))
Joe Perches8af443e2011-08-21 19:56:48 -03003731 pr_cont(" %s", irq_name[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 if (mark & (1 << i))
Joe Perches8af443e2011-08-21 19:56:48 -03003733 pr_cont("*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 }
3735}
3736
3737static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3738{
Joe Perches8af443e2011-08-21 19:56:48 -03003739 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3740 btv->c.nr,
3741 (unsigned long)btv->main.dma,
3742 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3743 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3744 (unsigned long)rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
3746 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
Joe Perches8af443e2011-08-21 19:56:48 -03003747 pr_notice("%d: Oh, there (temporarily?) is no input signal. "
3748 "Ok, then this is harmless, don't worry ;)\n",
3749 btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 return;
3751 }
Joe Perches8af443e2011-08-21 19:56:48 -03003752 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
3753 btv->c.nr);
3754 pr_notice("%d: Lets try to catch the culpit red-handed ...\n",
3755 btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 dump_stack();
3757}
3758
3759static int
3760bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3761{
3762 struct bttv_buffer *item;
3763
3764 memset(set,0,sizeof(*set));
3765
3766 /* capture request ? */
3767 if (!list_empty(&btv->capture)) {
3768 set->frame_irq = 1;
3769 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3770 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3771 set->top = item;
3772 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3773 set->bottom = item;
3774
3775 /* capture request for other field ? */
3776 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3777 (item->vb.queue.next != &btv->capture)) {
3778 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
Mike Isely66349b42009-09-21 12:09:08 -03003779 /* Mike Isely <isely@pobox.com> - Only check
3780 * and set up the bottom field in the logic
3781 * below. Don't ever do the top field. This
3782 * of course means that if we set up the
3783 * bottom field in the above code that we'll
3784 * actually skip a field. But that's OK.
3785 * Having processed only a single buffer this
3786 * time, then the next time around the first
3787 * available buffer should be for a top field.
3788 * That will then cause us here to set up a
3789 * top then a bottom field in the normal way.
3790 * The alternative to this understanding is
3791 * that we set up the second available buffer
3792 * as a top field, but that's out of order
3793 * since this driver always processes the top
3794 * field first - the effect will be the two
3795 * buffers being returned in the wrong order,
3796 * with the second buffer also being delayed
3797 * by one field time (owing to the fifo nature
3798 * of videobuf). Worse still, we'll be stuck
3799 * doing fields out of order now every time
3800 * until something else causes a field to be
3801 * dropped. By effectively forcing a field to
3802 * drop this way then we always get back into
3803 * sync within a single frame time. (Out of
3804 * order fields can screw up deinterlacing
3805 * algorithms.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 if (NULL == set->bottom &&
3808 V4L2_FIELD_BOTTOM == item->vb.field) {
3809 set->bottom = item;
3810 }
3811 if (NULL != set->top && NULL != set->bottom)
3812 set->top_irq = 2;
3813 }
3814 }
3815 }
3816
3817 /* screen overlay ? */
3818 if (NULL != btv->screen) {
3819 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3820 if (NULL == set->top && NULL == set->bottom) {
3821 set->top = btv->screen;
3822 set->bottom = btv->screen;
3823 }
3824 } else {
3825 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3826 NULL == set->top) {
3827 set->top = btv->screen;
3828 }
3829 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3830 NULL == set->bottom) {
3831 set->bottom = btv->screen;
3832 }
3833 }
3834 }
3835
Joe Perches8af443e2011-08-21 19:56:48 -03003836 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3837 btv->c.nr, set->top, set->bottom,
3838 btv->screen, set->frame_irq, set->top_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 return 0;
3840}
3841
3842static void
3843bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3844 struct bttv_buffer_set *curr, unsigned int state)
3845{
3846 struct timeval ts;
3847
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003848 v4l2_get_timestamp(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
3850 if (wakeup->top == wakeup->bottom) {
3851 if (NULL != wakeup->top && curr->top != wakeup->top) {
3852 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003853 pr_debug("%d: wakeup: both=%p\n",
3854 btv->c.nr, wakeup->top);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 wakeup->top->vb.ts = ts;
3856 wakeup->top->vb.field_count = btv->field_count;
3857 wakeup->top->vb.state = state;
3858 wake_up(&wakeup->top->vb.done);
3859 }
3860 } else {
3861 if (NULL != wakeup->top && curr->top != wakeup->top) {
3862 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003863 pr_debug("%d: wakeup: top=%p\n",
3864 btv->c.nr, wakeup->top);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 wakeup->top->vb.ts = ts;
3866 wakeup->top->vb.field_count = btv->field_count;
3867 wakeup->top->vb.state = state;
3868 wake_up(&wakeup->top->vb.done);
3869 }
3870 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3871 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003872 pr_debug("%d: wakeup: bottom=%p\n",
3873 btv->c.nr, wakeup->bottom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 wakeup->bottom->vb.ts = ts;
3875 wakeup->bottom->vb.field_count = btv->field_count;
3876 wakeup->bottom->vb.state = state;
3877 wake_up(&wakeup->bottom->vb.done);
3878 }
3879 }
3880}
3881
3882static void
3883bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3884 unsigned int state)
3885{
3886 struct timeval ts;
3887
3888 if (NULL == wakeup)
3889 return;
3890
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003891 v4l2_get_timestamp(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 wakeup->vb.ts = ts;
3893 wakeup->vb.field_count = btv->field_count;
3894 wakeup->vb.state = state;
3895 wake_up(&wakeup->vb.done);
3896}
3897
3898static void bttv_irq_timeout(unsigned long data)
3899{
3900 struct bttv *btv = (struct bttv *)data;
3901 struct bttv_buffer_set old,new;
3902 struct bttv_buffer *ovbi;
3903 struct bttv_buffer *item;
3904 unsigned long flags;
3905
3906 if (bttv_verbose) {
Joe Perches8af443e2011-08-21 19:56:48 -03003907 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3908 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3909 btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 bttv_print_irqbits(btread(BT848_INT_STAT),0);
Joe Perches8af443e2011-08-21 19:56:48 -03003911 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 }
3913
3914 spin_lock_irqsave(&btv->s_lock,flags);
3915
3916 /* deactivate stuff */
3917 memset(&new,0,sizeof(new));
3918 old = btv->curr;
3919 ovbi = btv->cvbi;
3920 btv->curr = new;
3921 btv->cvbi = NULL;
3922 btv->loop_irq = 0;
3923 bttv_buffer_activate_video(btv, &new);
3924 bttv_buffer_activate_vbi(btv, NULL);
3925 bttv_set_dma(btv, 0);
3926
3927 /* wake up */
Brandon Philips0fc06862007-11-06 20:02:36 -03003928 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3929 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 /* cancel all outstanding capture / vbi requests */
3932 while (!list_empty(&btv->capture)) {
3933 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3934 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003935 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 wake_up(&item->vb.done);
3937 }
3938 while (!list_empty(&btv->vcapture)) {
3939 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3940 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003941 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 wake_up(&item->vb.done);
3943 }
3944
3945 btv->errors++;
3946 spin_unlock_irqrestore(&btv->s_lock,flags);
3947}
3948
3949static void
3950bttv_irq_wakeup_top(struct bttv *btv)
3951{
3952 struct bttv_buffer *wakeup = btv->curr.top;
3953
3954 if (NULL == wakeup)
3955 return;
3956
3957 spin_lock(&btv->s_lock);
3958 btv->curr.top_irq = 0;
3959 btv->curr.top = NULL;
3960 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3961
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003962 v4l2_get_timestamp(&wakeup->vb.ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 wakeup->vb.field_count = btv->field_count;
Brandon Philips0fc06862007-11-06 20:02:36 -03003964 wakeup->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 wake_up(&wakeup->vb.done);
3966 spin_unlock(&btv->s_lock);
3967}
3968
3969static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3970{
3971 if (rc < risc->dma)
3972 return 0;
3973 if (rc > risc->dma + risc->size)
3974 return 0;
3975 return 1;
3976}
3977
3978static void
3979bttv_irq_switch_video(struct bttv *btv)
3980{
3981 struct bttv_buffer_set new;
3982 struct bttv_buffer_set old;
3983 dma_addr_t rc;
3984
3985 spin_lock(&btv->s_lock);
3986
3987 /* new buffer set */
3988 bttv_irq_next_video(btv, &new);
3989 rc = btread(BT848_RISC_COUNT);
3990 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3991 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3992 btv->framedrop++;
3993 if (debug_latency)
3994 bttv_irq_debug_low_latency(btv, rc);
3995 spin_unlock(&btv->s_lock);
3996 return;
3997 }
3998
3999 /* switch over */
4000 old = btv->curr;
4001 btv->curr = new;
4002 btv->loop_irq &= ~1;
4003 bttv_buffer_activate_video(btv, &new);
4004 bttv_set_dma(btv, 0);
4005
4006 /* switch input */
4007 if (UNSET != btv->new_input) {
4008 video_mux(btv,btv->new_input);
4009 btv->new_input = UNSET;
4010 }
4011
4012 /* wake up finished buffers */
Brandon Philips0fc06862007-11-06 20:02:36 -03004013 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 spin_unlock(&btv->s_lock);
4015}
4016
4017static void
4018bttv_irq_switch_vbi(struct bttv *btv)
4019{
4020 struct bttv_buffer *new = NULL;
4021 struct bttv_buffer *old;
4022 u32 rc;
4023
4024 spin_lock(&btv->s_lock);
4025
4026 if (!list_empty(&btv->vcapture))
4027 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4028 old = btv->cvbi;
4029
4030 rc = btread(BT848_RISC_COUNT);
4031 if (NULL != old && (is_active(&old->top, rc) ||
4032 is_active(&old->bottom, rc))) {
4033 btv->framedrop++;
4034 if (debug_latency)
4035 bttv_irq_debug_low_latency(btv, rc);
4036 spin_unlock(&btv->s_lock);
4037 return;
4038 }
4039
4040 /* switch */
4041 btv->cvbi = new;
4042 btv->loop_irq &= ~4;
4043 bttv_buffer_activate_vbi(btv, new);
4044 bttv_set_dma(btv, 0);
4045
Brandon Philips0fc06862007-11-06 20:02:36 -03004046 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 spin_unlock(&btv->s_lock);
4048}
4049
David Howells7d12e782006-10-05 14:55:46 +01004050static irqreturn_t bttv_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051{
4052 u32 stat,astat;
4053 u32 dstat;
4054 int count;
4055 struct bttv *btv;
4056 int handled = 0;
4057
4058 btv=(struct bttv *)dev_id;
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004059
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 count=0;
4061 while (1) {
4062 /* get/clear interrupt status bits */
4063 stat=btread(BT848_INT_STAT);
4064 astat=stat&btread(BT848_INT_MASK);
4065 if (!astat)
4066 break;
4067 handled = 1;
4068 btwrite(stat,BT848_INT_STAT);
4069
4070 /* get device status bits */
4071 dstat=btread(BT848_DSTATUS);
4072
4073 if (irq_debug) {
Joe Perches8af443e2011-08-21 19:56:48 -03004074 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
4075 btv->c.nr, count, btv->field_count,
4076 stat>>28, btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 bttv_print_irqbits(stat,astat);
4078 if (stat & BT848_INT_HLOCK)
Joe Perches8af443e2011-08-21 19:56:48 -03004079 pr_cont(" HLOC => %s",
4080 dstat & BT848_DSTATUS_HLOC
4081 ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 if (stat & BT848_INT_VPRES)
Joe Perches8af443e2011-08-21 19:56:48 -03004083 pr_cont(" PRES => %s",
4084 dstat & BT848_DSTATUS_PRES
4085 ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 if (stat & BT848_INT_FMTCHG)
Joe Perches8af443e2011-08-21 19:56:48 -03004087 pr_cont(" NUML => %s",
4088 dstat & BT848_DSTATUS_NUML
4089 ? "625" : "525");
4090 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 }
4092
4093 if (astat&BT848_INT_VSYNC)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004094 btv->field_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004096 if ((astat & BT848_INT_GPINT) && btv->remote) {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004097 bttv_input_irq(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 }
4099
4100 if (astat & BT848_INT_I2CDONE) {
4101 btv->i2c_done = stat;
4102 wake_up(&btv->i2c_queue);
4103 }
4104
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004105 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 bttv_irq_switch_vbi(btv);
4107
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004108 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 bttv_irq_wakeup_top(btv);
4110
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004111 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 bttv_irq_switch_video(btv);
4113
4114 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004115 audio_mute(btv, btv->mute); /* trigger automute */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
4117 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
Joe Perches8af443e2011-08-21 19:56:48 -03004118 pr_info("%d: %s%s @ %08x,",
4119 btv->c.nr,
4120 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4121 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4122 btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 bttv_print_irqbits(stat,astat);
Joe Perches8af443e2011-08-21 19:56:48 -03004124 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 if (bttv_debug)
4126 bttv_print_riscaddr(btv);
4127 }
4128 if (fdsr && astat & BT848_INT_FDSR) {
Joe Perches8af443e2011-08-21 19:56:48 -03004129 pr_info("%d: FDSR @ %08x\n",
4130 btv->c.nr, btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 if (bttv_debug)
4132 bttv_print_riscaddr(btv);
4133 }
4134
4135 count++;
4136 if (count > 4) {
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004137
4138 if (count > 8 || !(astat & BT848_INT_GPINT)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004139 btwrite(0, BT848_INT_MASK);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004140
Joe Perches8af443e2011-08-21 19:56:48 -03004141 pr_err("%d: IRQ lockup, cleared int mask [",
4142 btv->c.nr);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004143 } else {
Joe Perches8af443e2011-08-21 19:56:48 -03004144 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
4145 btv->c.nr);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004146
4147 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4148 BT848_INT_MASK);
4149 };
4150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 bttv_print_irqbits(stat,astat);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004152
Joe Perches8af443e2011-08-21 19:56:48 -03004153 pr_cont("]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 }
4155 }
4156 btv->irq_total++;
4157 if (handled)
4158 btv->irq_me++;
4159 return IRQ_RETVAL(handled);
4160}
4161
4162
4163/* ----------------------------------------------------------------------- */
4164/* initialitation */
4165
4166static struct video_device *vdev_init(struct bttv *btv,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004167 const struct video_device *template,
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004168 const char *type_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169{
4170 struct video_device *vfd;
4171
4172 vfd = video_device_alloc();
4173 if (NULL == vfd)
4174 return NULL;
4175 *vfd = *template;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004176 vfd->v4l2_dev = &btv->c.v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 vfd->release = video_device_release;
Mauro Carvalho Chehab1d0a4362008-06-23 12:31:29 -03004178 vfd->debug = bttv_debug;
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004179 video_set_drvdata(vfd, btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4181 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004182 type_name, bttv_tvcards[btv->c.type].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 return vfd;
4184}
4185
4186static void bttv_unregister_video(struct bttv *btv)
4187{
4188 if (btv->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004189 if (video_is_registered(btv->video_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 video_unregister_device(btv->video_dev);
4191 else
4192 video_device_release(btv->video_dev);
4193 btv->video_dev = NULL;
4194 }
4195 if (btv->vbi_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004196 if (video_is_registered(btv->vbi_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 video_unregister_device(btv->vbi_dev);
4198 else
4199 video_device_release(btv->vbi_dev);
4200 btv->vbi_dev = NULL;
4201 }
4202 if (btv->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004203 if (video_is_registered(btv->radio_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 video_unregister_device(btv->radio_dev);
4205 else
4206 video_device_release(btv->radio_dev);
4207 btv->radio_dev = NULL;
4208 }
4209}
4210
4211/* register video4linux devices */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004212static int bttv_register_video(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213{
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004214 if (no_overlay > 0)
Joe Perches8af443e2011-08-21 19:56:48 -03004215 pr_notice("Overlay support disabled\n");
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004216
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 /* video */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004218 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004219
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004220 if (NULL == btv->video_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004222 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
4223 video_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03004225 pr_info("%d: registered device %s\n",
4226 btv->c.nr, video_device_node_name(btv->video_dev));
Hans Verkuil22a04f12008-07-20 06:35:02 -03004227 if (device_create_file(&btv->video_dev->dev,
Kay Sievers54bd5b62007-10-08 16:26:13 -03004228 &dev_attr_card)<0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004229 pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
Trent Piephod94fc9a2006-07-29 17:18:06 -03004230 goto err;
4231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
4233 /* vbi */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004234 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004235
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004236 if (NULL == btv->vbi_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004238 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
4239 vbi_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03004241 pr_info("%d: registered device %s\n",
4242 btv->c.nr, video_device_node_name(btv->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004244 if (!btv->has_radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 return 0;
4246 /* radio */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004247 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004248 if (NULL == btv->radio_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004250 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4251 radio_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03004253 pr_info("%d: registered device %s\n",
4254 btv->c.nr, video_device_node_name(btv->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
4256 /* all done */
4257 return 0;
4258
4259 err:
4260 bttv_unregister_video(btv);
4261 return -1;
4262}
4263
4264
4265/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4266/* response on cards with no firmware is not enabled by OF */
4267static void pci_set_command(struct pci_dev *dev)
4268{
4269#if defined(__powerpc__)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004270 unsigned int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004272 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4273 cmd = (cmd | PCI_COMMAND_MEMORY );
4274 pci_write_config_dword(dev, PCI_COMMAND, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275#endif
4276}
4277
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004278static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
4280 int result;
4281 unsigned char lat;
4282 struct bttv *btv;
4283
4284 if (bttv_num == BTTV_MAX)
4285 return -ENOMEM;
Joe Perches8af443e2011-08-21 19:56:48 -03004286 pr_info("Bt8xx card found (%d)\n", bttv_num);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004287 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004288 if (btv == NULL) {
Joe Perches8af443e2011-08-21 19:56:48 -03004289 pr_err("out of memory\n");
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004290 return -ENOMEM;
4291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 btv->c.nr = bttv_num;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004293 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4294 "bttv%d", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
4296 /* initialize structs / fill in defaults */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02004297 mutex_init(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004298 spin_lock_init(&btv->s_lock);
4299 spin_lock_init(&btv->gpio_lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004300 init_waitqueue_head(&btv->i2c_queue);
4301 INIT_LIST_HEAD(&btv->c.subs);
4302 INIT_LIST_HEAD(&btv->capture);
4303 INIT_LIST_HEAD(&btv->vcapture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 v4l2_prio_init(&btv->prio);
4305
4306 init_timer(&btv->timeout);
4307 btv->timeout.function = bttv_irq_timeout;
4308 btv->timeout.data = (unsigned long)btv;
4309
Michael Krufky7c08fb02005-11-08 21:36:21 -08004310 btv->i2c_rc = -1;
4311 btv->tuner_type = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 btv->new_input = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 btv->has_radio=radio[btv->c.nr];
4314
4315 /* pci stuff (init, get irq/mmio, ... */
4316 btv->c.pci = dev;
Michael Krufky7c08fb02005-11-08 21:36:21 -08004317 btv->id = dev->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 if (pci_enable_device(dev)) {
Joe Perches8af443e2011-08-21 19:56:48 -03004319 pr_warn("%d: Can't enable device\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 return -EIO;
4321 }
Yang Hongyang284901a2009-04-06 19:01:15 -07004322 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Joe Perches8af443e2011-08-21 19:56:48 -03004323 pr_warn("%d: No suitable DMA available\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 return -EIO;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 if (!request_mem_region(pci_resource_start(dev,0),
4327 pci_resource_len(dev,0),
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004328 btv->c.v4l2_dev.name)) {
Joe Perches8af443e2011-08-21 19:56:48 -03004329 pr_warn("%d: can't request iomem (0x%llx)\n",
4330 btv->c.nr,
4331 (unsigned long long)pci_resource_start(dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 return -EBUSY;
4333 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004334 pci_set_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 pci_set_command(dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004336
4337 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4338 if (result < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004339 pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004340 goto fail0;
4341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Bjørn Morkabd34d82011-03-21 11:35:56 -03004343 btv->revision = dev->revision;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004344 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
Joe Perches8af443e2011-08-21 19:56:48 -03004345 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
4346 bttv_num, btv->id, btv->revision, pci_name(dev),
4347 btv->c.pci->irq, lat,
4348 (unsigned long long)pci_resource_start(dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 schedule();
4350
Akinobu Mita5f1693f2006-12-20 10:08:56 -03004351 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4352 if (NULL == btv->bt848_mmio) {
Joe Perches8af443e2011-08-21 19:56:48 -03004353 pr_err("%d: ioremap() failed\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 result = -EIO;
4355 goto fail1;
4356 }
4357
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004358 /* identify card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 bttv_idcard(btv);
4360
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004361 /* disable irqs, register irq handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 btwrite(0, BT848_INT_MASK);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004363 result = request_irq(btv->c.pci->irq, bttv_irq,
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004364 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004365 if (result < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004366 pr_err("%d: can't get IRQ %d\n",
4367 bttv_num, btv->c.pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 goto fail1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
4371 if (0 != bttv_handle_chipset(btv)) {
4372 result = -EIO;
4373 goto fail2;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
4376 /* init options from insmod args */
4377 btv->opt_combfilter = combfilter;
4378 btv->opt_lumafilter = lumafilter;
4379 btv->opt_automute = automute;
4380 btv->opt_chroma_agc = chroma_agc;
4381 btv->opt_adc_crush = adc_crush;
4382 btv->opt_vcr_hack = vcr_hack;
4383 btv->opt_whitecrush_upper = whitecrush_upper;
4384 btv->opt_whitecrush_lower = whitecrush_lower;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07004385 btv->opt_uv_ratio = uv_ratio;
4386 btv->opt_full_luma_range = full_luma_range;
4387 btv->opt_coring = coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
4389 /* fill struct bttv with some useful defaults */
4390 btv->init.btv = btv;
4391 btv->init.ov.w.width = 320;
4392 btv->init.ov.w.height = 240;
Mauro Carvalho Chehabc96dd072007-10-26 16:51:47 -03004393 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 btv->init.width = 320;
4395 btv->init.height = 240;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 btv->input = 0;
4397
4398 /* initialize hardware */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004399 if (bttv_gpio)
4400 bttv_gpio_tracking(btv,"pre-init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
4402 bttv_risc_init_main(btv);
4403 init_bt848(btv);
4404
4405 /* gpio */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004406 btwrite(0x00, BT848_GPIO_REG_INP);
4407 btwrite(0x00, BT848_GPIO_OUT_EN);
4408 if (bttv_verbose)
4409 bttv_gpio_tracking(btv,"init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004411 /* needs to be done before i2c is registered */
4412 bttv_init_card1(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004414 /* register i2c + gpio */
4415 init_bttv_i2c(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004417 /* some card-specific stuff (needs working i2c) */
4418 bttv_init_card2(btv);
Hans Verkuil2c905772009-07-20 08:14:17 -03004419 bttv_init_tuner(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 init_irqreg(btv);
4421
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004422 /* register video4linux + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 if (!bttv_tvcards[btv->c.type].no_video) {
4424 bttv_register_video(btv);
4425 bt848_bright(btv,32768);
Alan McIvor961e6682012-04-01 21:11:08 -03004426 bt848_contrast(btv, 27648);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 bt848_hue(btv,32768);
4428 bt848_sat(btv,32768);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004429 audio_mute(btv, 1);
Trent Piepho333408f2007-07-03 15:08:10 -03004430 set_input(btv, 0, btv->tvnorm);
Michael Schimeke5bd0262007-01-18 16:17:39 -03004431 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4432 btv->crop[1] = btv->crop[0]; /* current = default */
4433 disclaim_vbi_lines(btv);
4434 disclaim_video_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 }
4436
Jarod Wilsonf992a492007-03-24 15:23:50 -03004437 /* add subdevices and autoload dvb-bt8xx if needed */
4438 if (bttv_tvcards[btv->c.type].has_dvb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 bttv_sub_add_device(&btv->c, "dvb");
Jarod Wilsonf992a492007-03-24 15:23:50 -03004440 request_modules(btv);
4441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Jean Delvared90a4ae2010-02-16 14:22:37 -03004443 if (!disable_ir) {
4444 init_bttv_i2c_ir(btv);
4445 bttv_input_init(btv);
4446 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 /* everything is fine */
4449 bttv_num++;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004452fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004453 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004455fail1:
4456 v4l2_device_unregister(&btv->c.v4l2_dev);
4457
4458fail0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 if (btv->bt848_mmio)
4460 iounmap(btv->bt848_mmio);
4461 release_mem_region(pci_resource_start(btv->c.pci,0),
4462 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 return result;
4464}
4465
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004466static void bttv_remove(struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004468 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4469 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
4471 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03004472 pr_info("%d: unloading\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
Tejun Heo707bcf32010-12-24 16:14:20 +01004474 if (bttv_tvcards[btv->c.type].has_dvb)
4475 flush_request_modules(btv);
4476
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004477 /* shutdown everything (DMA+IRQs) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 btand(~15, BT848_GPIO_DMA_CTL);
4479 btwrite(0, BT848_INT_MASK);
4480 btwrite(~0x0, BT848_INT_STAT);
4481 btwrite(0x0, BT848_GPIO_OUT_EN);
4482 if (bttv_gpio)
4483 bttv_gpio_tracking(btv,"cleanup");
4484
4485 /* tell gpio modules we are leaving ... */
4486 btv->shutdown=1;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004487 bttv_input_fini(btv);
Christopher Pascoe889aee82006-01-09 15:25:28 -02004488 bttv_sub_del_devices(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004490 /* unregister i2c_bus + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 fini_bttv_i2c(btv);
4492
4493 /* unregister video4linux */
4494 bttv_unregister_video(btv);
4495
4496 /* free allocated memory */
4497 btcx_riscmem_free(btv->c.pci,&btv->main);
4498
4499 /* free ressources */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004500 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 iounmap(btv->bt848_mmio);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004502 release_mem_region(pci_resource_start(btv->c.pci,0),
4503 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004505 v4l2_device_unregister(&btv->c.v4l2_dev);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004506 bttvs[btv->c.nr] = NULL;
4507 kfree(btv);
4508
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004509 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510}
4511
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004512#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4514{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004515 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4516 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 struct bttv_buffer_set idle;
4518 unsigned long flags;
4519
Joe Perches8af443e2011-08-21 19:56:48 -03004520 dprintk("%d: suspend %d\n", btv->c.nr, state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
4522 /* stop dma + irqs */
4523 spin_lock_irqsave(&btv->s_lock,flags);
4524 memset(&idle, 0, sizeof(idle));
4525 btv->state.video = btv->curr;
4526 btv->state.vbi = btv->cvbi;
4527 btv->state.loop_irq = btv->loop_irq;
4528 btv->curr = idle;
4529 btv->loop_irq = 0;
4530 bttv_buffer_activate_video(btv, &idle);
4531 bttv_buffer_activate_vbi(btv, NULL);
4532 bttv_set_dma(btv, 0);
4533 btwrite(0, BT848_INT_MASK);
4534 spin_unlock_irqrestore(&btv->s_lock,flags);
4535
4536 /* save bt878 state */
4537 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4538 btv->state.gpio_data = gpio_read();
4539
4540 /* save pci state */
4541 pci_save_state(pci_dev);
4542 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4543 pci_disable_device(pci_dev);
4544 btv->state.disabled = 1;
4545 }
4546 return 0;
4547}
4548
4549static int bttv_resume(struct pci_dev *pci_dev)
4550{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004551 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4552 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004554 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Joe Perches8af443e2011-08-21 19:56:48 -03004556 dprintk("%d: resume\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
4558 /* restore pci state */
4559 if (btv->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004560 err=pci_enable_device(pci_dev);
4561 if (err) {
Joe Perches8af443e2011-08-21 19:56:48 -03004562 pr_warn("%d: Can't enable device\n", btv->c.nr);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004563 return err;
4564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 btv->state.disabled = 0;
4566 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004567 err=pci_set_power_state(pci_dev, PCI_D0);
4568 if (err) {
4569 pci_disable_device(pci_dev);
Joe Perches8af443e2011-08-21 19:56:48 -03004570 pr_warn("%d: Can't enable device\n", btv->c.nr);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004571 btv->state.disabled = 1;
4572 return err;
4573 }
4574
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 pci_restore_state(pci_dev);
4576
4577 /* restore bt878 state */
4578 bttv_reinit_bt848(btv);
4579 gpio_inout(0xffffff, btv->state.gpio_enable);
4580 gpio_write(btv->state.gpio_data);
4581
4582 /* restart dma */
4583 spin_lock_irqsave(&btv->s_lock,flags);
4584 btv->curr = btv->state.video;
4585 btv->cvbi = btv->state.vbi;
4586 btv->loop_irq = btv->state.loop_irq;
4587 bttv_buffer_activate_video(btv, &btv->curr);
4588 bttv_buffer_activate_vbi(btv, btv->cvbi);
4589 bttv_set_dma(btv, 0);
4590 spin_unlock_irqrestore(&btv->s_lock,flags);
4591 return 0;
4592}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004593#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
4595static struct pci_device_id bttv_pci_tbl[] = {
Joe Perches76e97412009-07-05 15:59:21 -03004596 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4597 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4598 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4599 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
Peter De Schrijverc540d9f2011-11-06 10:47:58 -03004600 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004601 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602};
4603
4604MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4605
4606static struct pci_driver bttv_pci_driver = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004607 .name = "bttv",
4608 .id_table = bttv_pci_tbl,
4609 .probe = bttv_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004610 .remove = bttv_remove,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004611#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 .suspend = bttv_suspend,
4613 .resume = bttv_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004614#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615};
4616
Adrian Bunk7d44e892007-12-11 19:23:43 -03004617static int __init bttv_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618{
Randy Dunlapc526e222006-07-15 09:08:26 -03004619 int ret;
4620
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 bttv_num = 0;
4622
Joe Perches8af443e2011-08-21 19:56:48 -03004623 pr_info("driver version %s loaded\n", BTTV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4625 gbuffers = 2;
Filipe Rossetf41b6962009-05-28 11:11:53 -03004626 if (gbufsize > BTTV_MAX_FBUF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 gbufsize = BTTV_MAX_FBUF;
4628 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4629 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03004630 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
4631 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
4633 bttv_check_chipset();
4634
Randy Dunlapc526e222006-07-15 09:08:26 -03004635 ret = bus_register(&bttv_sub_bus_type);
4636 if (ret < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004637 pr_warn("bus_register error: %d\n", ret);
Randy Dunlapc526e222006-07-15 09:08:26 -03004638 return ret;
4639 }
Akinobu Mita9e7e85e2007-12-17 14:26:29 -03004640 ret = pci_register_driver(&bttv_pci_driver);
4641 if (ret < 0)
4642 bus_unregister(&bttv_sub_bus_type);
4643
4644 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645}
4646
Adrian Bunk7d44e892007-12-11 19:23:43 -03004647static void __exit bttv_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
4649 pci_unregister_driver(&bttv_pci_driver);
4650 bus_unregister(&bttv_sub_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651}
4652
4653module_init(bttv_init_module);
4654module_exit(bttv_cleanup_module);
4655
4656/*
4657 * Local variables:
4658 * c-basic-offset: 8
4659 * End:
4660 */