blob: cffa8b6683ea6633bcb004464a1d0a2b19e8c2ac [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 Verkuil1b9e94d2012-09-09 09:23:31 -030052#include <media/v4l2-chip-ident.h>
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -030053#include <media/tvaudio.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030054#include <media/msp3400.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020055
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -070056#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#include <asm/io.h>
59#include <asm/byteorder.h>
60
Hans Verkuilb9218f22010-12-27 12:22:46 -030061#include <media/saa6588.h>
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070062
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030063#define BTTV_VERSION "0.9.19"
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065unsigned int bttv_num; /* number of Bt848s in use */
Trent Piepho4b10d3b2009-01-28 21:32:59 -030066struct bttv *bttvs[BTTV_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020068unsigned int bttv_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069unsigned int bttv_verbose = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020070unsigned int bttv_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/* config variables */
73#ifdef __BIG_ENDIAN
74static unsigned int bigendian=1;
75#else
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020076static unsigned int bigendian;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
78static unsigned int radio[BTTV_MAX];
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020079static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static unsigned int gbuffers = 8;
81static unsigned int gbufsize = 0x208000;
Michael Schimeke5bd0262007-01-18 16:17:39 -030082static unsigned int reset_crop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Jean Delvare176c2f32008-09-07 12:49:59 -030084static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
85static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
86static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020087static int debug_latency;
Jean Delvared90a4ae2010-02-16 14:22:37 -030088static int disable_ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020090static unsigned int fdsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* options */
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020093static unsigned int combfilter;
94static unsigned int lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static unsigned int automute = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020096static unsigned int chroma_agc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static unsigned int adc_crush = 1;
98static unsigned int whitecrush_upper = 0xCF;
99static unsigned int whitecrush_lower = 0x7F;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200100static unsigned int vcr_hack;
101static unsigned int irq_iswitch;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700102static unsigned int uv_ratio = 50;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200103static unsigned int full_luma_range;
104static unsigned int coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/* API features (turn on/off stuff for testing) */
107static unsigned int v4l2 = 1;
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* insmod args */
110module_param(bttv_verbose, int, 0644);
111module_param(bttv_gpio, int, 0644);
112module_param(bttv_debug, int, 0644);
113module_param(irq_debug, int, 0644);
114module_param(debug_latency, int, 0644);
Jean Delvared90a4ae2010-02-16 14:22:37 -0300115module_param(disable_ir, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117module_param(fdsr, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118module_param(gbuffers, int, 0444);
119module_param(gbufsize, int, 0444);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300120module_param(reset_crop, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122module_param(v4l2, int, 0644);
123module_param(bigendian, int, 0644);
124module_param(irq_iswitch, int, 0644);
125module_param(combfilter, int, 0444);
126module_param(lumafilter, int, 0444);
127module_param(automute, int, 0444);
128module_param(chroma_agc, int, 0444);
129module_param(adc_crush, int, 0444);
130module_param(whitecrush_upper, int, 0444);
131module_param(whitecrush_lower, int, 0444);
132module_param(vcr_hack, int, 0444);
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700133module_param(uv_ratio, int, 0444);
134module_param(full_luma_range, int, 0444);
135module_param(coring, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Jean Delvare176c2f32008-09-07 12:49:59 -0300137module_param_array(radio, int, NULL, 0444);
138module_param_array(video_nr, int, NULL, 0444);
139module_param_array(radio_nr, int, NULL, 0444);
140module_param_array(vbi_nr, int, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
143MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
144MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
145MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
146MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
147MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
Jean Delvared90a4ae2010-02-16 14:22:37 -0300148MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
150MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
Michael Schimeke5bd0262007-01-18 16:17:39 -0300151MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
152 "is 1 (yes) for compatibility with older applications");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
154MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
155MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
156MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
157MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
158MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
159MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700160MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
161MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
162MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
Jean Delvare176c2f32008-09-07 12:49:59 -0300163MODULE_PARM_DESC(video_nr, "video device numbers");
164MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
165MODULE_PARM_DESC(radio_nr, "radio device numbers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
168MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
169MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -0300170MODULE_VERSION(BTTV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/* ----------------------------------------------------------------------- */
173/* sysfs */
174
Kay Sievers54bd5b62007-10-08 16:26:13 -0300175static ssize_t show_card(struct device *cd,
176 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300178 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -0300179 struct bttv *btv = video_get_drvdata(vfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
181}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300182static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184/* ----------------------------------------------------------------------- */
Jarod Wilsonf992a492007-03-24 15:23:50 -0300185/* dvb auto-load setup */
186#if defined(CONFIG_MODULES) && defined(MODULE)
187static void request_module_async(struct work_struct *work)
188{
189 request_module("dvb-bt8xx");
190}
191
192static void request_modules(struct bttv *dev)
193{
194 INIT_WORK(&dev->request_module_wk, request_module_async);
195 schedule_work(&dev->request_module_wk);
196}
Tejun Heo707bcf32010-12-24 16:14:20 +0100197
198static void flush_request_modules(struct bttv *dev)
199{
Linus Torvalds0b8e74c2012-10-07 17:49:05 +0900200 flush_work(&dev->request_module_wk);
Tejun Heo707bcf32010-12-24 16:14:20 +0100201}
Jarod Wilsonf992a492007-03-24 15:23:50 -0300202#else
203#define request_modules(dev)
Mauro Carvalho Chehaba1fd2872012-10-27 16:33:41 -0300204#define flush_request_modules(dev) do {} while(0)
Jarod Wilsonf992a492007-03-24 15:23:50 -0300205#endif /* CONFIG_MODULES */
206
207
208/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/* static data */
210
211/* special timing tables from conexant... */
212static u8 SRAM_Table[][60] =
213{
214 /* PAL digital input over GPIO[7:0] */
215 {
216 45, // 45 bytes following
217 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
218 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
219 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
220 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
221 0x37,0x00,0xAF,0x21,0x00
222 },
223 /* NTSC digital input over GPIO[7:0] */
224 {
225 51, // 51 bytes following
226 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
227 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
228 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
229 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
230 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
231 0x00,
232 },
233 // TGB_NTSC392 // quartzsight
234 // This table has been modified to be used for Fusion Rev D
235 {
236 0x2A, // size of table = 42
237 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
238 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
239 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
240 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
241 0x20, 0x00
242 }
243};
244
Michael Schimeke5bd0262007-01-18 16:17:39 -0300245/* minhdelayx1 first video pixel we can capture on a line and
246 hdelayx1 start of active video, both relative to rising edge of
247 /HRESET pulse (0H) in 1 / fCLKx1.
248 swidth width of active video and
249 totalwidth total line width, both in 1 / fCLKx1.
250 sqwidth total line width in square pixels.
251 vdelay start of active video in 2 * field lines relative to
252 trailing edge of /VRESET pulse (VDELAY register).
253 sheight height of active video in 2 * field lines.
254 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, \
257 vdelay, sheight, videostart0) \
258 .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, \
264 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
265 .cropcap.defrect.left = hdelayx1, \
266 .cropcap.defrect.top = (videostart0) * 2, \
267 .cropcap.defrect.width = swidth, \
268 .cropcap.defrect.height = sheight, \
269 .cropcap.pixelaspect.numerator = totalwidth, \
270 .cropcap.pixelaspect.denominator = sqwidth,
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272const struct bttv_tvnorm bttv_tvnorms[] = {
273 /* PAL-BDGHI */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800274 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
275 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 {
277 .v4l2_id = V4L2_STD_PAL,
278 .name = "PAL",
279 .Fsc = 35468950,
280 .swidth = 924,
281 .sheight = 576,
282 .totalwidth = 1135,
283 .adelay = 0x7f,
284 .bdelay = 0x72,
285 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
286 .scaledtwidth = 1135,
287 .hdelayx1 = 186,
288 .hactivex1 = 924,
289 .vdelay = 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300290 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 .sram = 0,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200292 /* ITU-R frame line number of the first VBI line
Michael Schimeke5bd0262007-01-18 16:17:39 -0300293 we can capture, of the first and second field.
294 The last line is determined by cropcap.bounds. */
295 .vbistart = { 7, 320 },
296 CROPCAP(/* minhdelayx1 */ 68,
297 /* hdelayx1 */ 186,
298 /* Should be (768 * 1135 + 944 / 2) / 944.
299 cropcap.defrect is used for image width
300 checks, so we keep the old value 924. */
301 /* swidth */ 924,
302 /* totalwidth */ 1135,
303 /* sqwidth */ 944,
304 /* vdelay */ 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300305 /* bt878 (and bt848?) can capture another
306 line below active video. */
Mauro Carvalho Chehaba1fd2872012-10-27 16:33:41 -0300307 /* sheight */ (576 + 2) + 0x20 - 2,
308 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 },{
Hans Verkuild97a11e2006-02-07 06:48:40 -0200310 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 .name = "NTSC",
312 .Fsc = 28636363,
313 .swidth = 768,
314 .sheight = 480,
315 .totalwidth = 910,
316 .adelay = 0x68,
317 .bdelay = 0x5d,
318 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
319 .scaledtwidth = 910,
320 .hdelayx1 = 128,
321 .hactivex1 = 910,
322 .vdelay = 0x1a,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300323 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 .sram = 1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200325 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300326 CROPCAP(/* minhdelayx1 */ 68,
327 /* hdelayx1 */ 128,
328 /* Should be (640 * 910 + 780 / 2) / 780? */
329 /* swidth */ 768,
330 /* totalwidth */ 910,
331 /* sqwidth */ 780,
332 /* vdelay */ 0x1a,
333 /* sheight */ 480,
334 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 },{
336 .v4l2_id = V4L2_STD_SECAM,
337 .name = "SECAM",
338 .Fsc = 35468950,
339 .swidth = 924,
340 .sheight = 576,
341 .totalwidth = 1135,
342 .adelay = 0x7f,
343 .bdelay = 0xb0,
344 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
345 .scaledtwidth = 1135,
346 .hdelayx1 = 186,
347 .hactivex1 = 922,
348 .vdelay = 0x20,
349 .vbipack = 255,
350 .sram = 0, /* like PAL, correct? */
Michael H. Schimek67f15702006-01-09 15:25:27 -0200351 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300352 CROPCAP(/* minhdelayx1 */ 68,
353 /* hdelayx1 */ 186,
354 /* swidth */ 924,
355 /* totalwidth */ 1135,
356 /* sqwidth */ 944,
357 /* vdelay */ 0x20,
358 /* sheight */ 576,
359 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 },{
361 .v4l2_id = V4L2_STD_PAL_Nc,
362 .name = "PAL-Nc",
363 .Fsc = 28636363,
364 .swidth = 640,
365 .sheight = 576,
366 .totalwidth = 910,
367 .adelay = 0x68,
368 .bdelay = 0x5d,
369 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
370 .scaledtwidth = 780,
371 .hdelayx1 = 130,
372 .hactivex1 = 734,
373 .vdelay = 0x1a,
374 .vbipack = 144,
375 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200376 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300377 CROPCAP(/* minhdelayx1 */ 68,
378 /* hdelayx1 */ 130,
379 /* swidth */ (640 * 910 + 780 / 2) / 780,
380 /* totalwidth */ 910,
381 /* sqwidth */ 780,
382 /* vdelay */ 0x1a,
383 /* sheight */ 576,
384 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 },{
386 .v4l2_id = V4L2_STD_PAL_M,
387 .name = "PAL-M",
388 .Fsc = 28636363,
389 .swidth = 640,
390 .sheight = 480,
391 .totalwidth = 910,
392 .adelay = 0x68,
393 .bdelay = 0x5d,
394 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
395 .scaledtwidth = 780,
396 .hdelayx1 = 135,
397 .hactivex1 = 754,
398 .vdelay = 0x1a,
399 .vbipack = 144,
400 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200401 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300402 CROPCAP(/* minhdelayx1 */ 68,
403 /* hdelayx1 */ 135,
404 /* swidth */ (640 * 910 + 780 / 2) / 780,
405 /* totalwidth */ 910,
406 /* sqwidth */ 780,
407 /* vdelay */ 0x1a,
408 /* sheight */ 480,
409 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 },{
411 .v4l2_id = V4L2_STD_PAL_N,
412 .name = "PAL-N",
413 .Fsc = 35468950,
414 .swidth = 768,
415 .sheight = 576,
416 .totalwidth = 1135,
417 .adelay = 0x7f,
418 .bdelay = 0x72,
419 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
420 .scaledtwidth = 944,
421 .hdelayx1 = 186,
422 .hactivex1 = 922,
423 .vdelay = 0x20,
424 .vbipack = 144,
425 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300426 .vbistart = { 7, 320 },
427 CROPCAP(/* minhdelayx1 */ 68,
428 /* hdelayx1 */ 186,
429 /* swidth */ (768 * 1135 + 944 / 2) / 944,
430 /* totalwidth */ 1135,
431 /* sqwidth */ 944,
432 /* vdelay */ 0x20,
433 /* sheight */ 576,
434 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 },{
436 .v4l2_id = V4L2_STD_NTSC_M_JP,
437 .name = "NTSC-JP",
438 .Fsc = 28636363,
439 .swidth = 640,
440 .sheight = 480,
441 .totalwidth = 910,
442 .adelay = 0x68,
443 .bdelay = 0x5d,
444 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
445 .scaledtwidth = 780,
446 .hdelayx1 = 135,
447 .hactivex1 = 754,
448 .vdelay = 0x16,
449 .vbipack = 144,
450 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300451 .vbistart = { 10, 273 },
452 CROPCAP(/* minhdelayx1 */ 68,
453 /* hdelayx1 */ 135,
454 /* swidth */ (640 * 910 + 780 / 2) / 780,
455 /* totalwidth */ 910,
456 /* sqwidth */ 780,
457 /* vdelay */ 0x16,
458 /* sheight */ 480,
459 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 },{
461 /* that one hopefully works with the strange timing
462 * which video recorders produce when playing a NTSC
463 * tape on a PAL TV ... */
464 .v4l2_id = V4L2_STD_PAL_60,
465 .name = "PAL-60",
466 .Fsc = 35468950,
467 .swidth = 924,
468 .sheight = 480,
469 .totalwidth = 1135,
470 .adelay = 0x7f,
471 .bdelay = 0x72,
472 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
473 .scaledtwidth = 1135,
474 .hdelayx1 = 186,
475 .hactivex1 = 924,
476 .vdelay = 0x1a,
477 .vbipack = 255,
478 .vtotal = 524,
479 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200480 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300481 CROPCAP(/* minhdelayx1 */ 68,
482 /* hdelayx1 */ 186,
483 /* swidth */ 924,
484 /* totalwidth */ 1135,
485 /* sqwidth */ 944,
486 /* vdelay */ 0x1a,
487 /* sheight */ 480,
488 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490};
491static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
492
493/* ----------------------------------------------------------------------- */
494/* bttv format list
495 packed pixel formats must come first */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300496static const struct bttv_format formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 {
498 .name = "8 bpp, gray",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 .fourcc = V4L2_PIX_FMT_GREY,
500 .btformat = BT848_COLOR_FMT_Y8,
501 .depth = 8,
502 .flags = FORMAT_FLAGS_PACKED,
503 },{
504 .name = "8 bpp, dithered color",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .fourcc = V4L2_PIX_FMT_HI240,
506 .btformat = BT848_COLOR_FMT_RGB8,
507 .depth = 8,
508 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
509 },{
510 .name = "15 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 .fourcc = V4L2_PIX_FMT_RGB555,
512 .btformat = BT848_COLOR_FMT_RGB15,
513 .depth = 16,
514 .flags = FORMAT_FLAGS_PACKED,
515 },{
516 .name = "15 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 .fourcc = V4L2_PIX_FMT_RGB555X,
518 .btformat = BT848_COLOR_FMT_RGB15,
519 .btswap = 0x03, /* byteswap */
520 .depth = 16,
521 .flags = FORMAT_FLAGS_PACKED,
522 },{
523 .name = "16 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 .fourcc = V4L2_PIX_FMT_RGB565,
525 .btformat = BT848_COLOR_FMT_RGB16,
526 .depth = 16,
527 .flags = FORMAT_FLAGS_PACKED,
528 },{
529 .name = "16 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 .fourcc = V4L2_PIX_FMT_RGB565X,
531 .btformat = BT848_COLOR_FMT_RGB16,
532 .btswap = 0x03, /* byteswap */
533 .depth = 16,
534 .flags = FORMAT_FLAGS_PACKED,
535 },{
536 .name = "24 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 .fourcc = V4L2_PIX_FMT_BGR24,
538 .btformat = BT848_COLOR_FMT_RGB24,
539 .depth = 24,
540 .flags = FORMAT_FLAGS_PACKED,
541 },{
542 .name = "32 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 .fourcc = V4L2_PIX_FMT_BGR32,
544 .btformat = BT848_COLOR_FMT_RGB32,
545 .depth = 32,
546 .flags = FORMAT_FLAGS_PACKED,
547 },{
548 .name = "32 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 .fourcc = V4L2_PIX_FMT_RGB32,
550 .btformat = BT848_COLOR_FMT_RGB32,
551 .btswap = 0x0f, /* byte+word swap */
552 .depth = 32,
553 .flags = FORMAT_FLAGS_PACKED,
554 },{
555 .name = "4:2:2, packed, YUYV",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 .fourcc = V4L2_PIX_FMT_YUYV,
557 .btformat = BT848_COLOR_FMT_YUY2,
558 .depth = 16,
559 .flags = FORMAT_FLAGS_PACKED,
560 },{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 .name = "4:2:2, packed, UYVY",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 .fourcc = V4L2_PIX_FMT_UYVY,
563 .btformat = BT848_COLOR_FMT_YUY2,
564 .btswap = 0x03, /* byteswap */
565 .depth = 16,
566 .flags = FORMAT_FLAGS_PACKED,
567 },{
568 .name = "4:2:2, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 .fourcc = V4L2_PIX_FMT_YUV422P,
570 .btformat = BT848_COLOR_FMT_YCrCb422,
571 .depth = 16,
572 .flags = FORMAT_FLAGS_PLANAR,
573 .hshift = 1,
574 .vshift = 0,
575 },{
576 .name = "4:2:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 .fourcc = V4L2_PIX_FMT_YUV420,
578 .btformat = BT848_COLOR_FMT_YCrCb422,
579 .depth = 12,
580 .flags = FORMAT_FLAGS_PLANAR,
581 .hshift = 1,
582 .vshift = 1,
583 },{
584 .name = "4:2:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 .fourcc = V4L2_PIX_FMT_YVU420,
586 .btformat = BT848_COLOR_FMT_YCrCb422,
587 .depth = 12,
588 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
589 .hshift = 1,
590 .vshift = 1,
591 },{
592 .name = "4:1:1, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 .fourcc = V4L2_PIX_FMT_YUV411P,
594 .btformat = BT848_COLOR_FMT_YCrCb411,
595 .depth = 12,
596 .flags = FORMAT_FLAGS_PLANAR,
597 .hshift = 2,
598 .vshift = 0,
599 },{
600 .name = "4:1:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 .fourcc = V4L2_PIX_FMT_YUV410,
602 .btformat = BT848_COLOR_FMT_YCrCb411,
603 .depth = 9,
604 .flags = FORMAT_FLAGS_PLANAR,
605 .hshift = 2,
606 .vshift = 2,
607 },{
608 .name = "4:1:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 .fourcc = V4L2_PIX_FMT_YVU410,
610 .btformat = BT848_COLOR_FMT_YCrCb411,
611 .depth = 9,
612 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
613 .hshift = 2,
614 .vshift = 2,
615 },{
616 .name = "raw scanlines",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 .fourcc = -1,
618 .btformat = BT848_COLOR_FMT_RAW,
619 .depth = 8,
620 .flags = FORMAT_FLAGS_RAW,
621 }
622};
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300623static const unsigned int FORMATS = ARRAY_SIZE(formats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625/* ----------------------------------------------------------------------- */
626
627#define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
628#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
629#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
630#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
631#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
632#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
633#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
634#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700635#define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
636#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
637#define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
638#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640static const struct v4l2_queryctrl no_ctl = {
641 .name = "42",
642 .flags = V4L2_CTRL_FLAG_DISABLED,
643};
644static const struct v4l2_queryctrl bttv_ctls[] = {
645 /* --- video --- */
646 {
647 .id = V4L2_CID_BRIGHTNESS,
648 .name = "Brightness",
649 .minimum = 0,
650 .maximum = 65535,
651 .step = 256,
652 .default_value = 32768,
653 .type = V4L2_CTRL_TYPE_INTEGER,
654 },{
655 .id = V4L2_CID_CONTRAST,
656 .name = "Contrast",
657 .minimum = 0,
658 .maximum = 65535,
659 .step = 128,
Alan McIvor961e6682012-04-01 21:11:08 -0300660 .default_value = 27648,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 .type = V4L2_CTRL_TYPE_INTEGER,
662 },{
663 .id = V4L2_CID_SATURATION,
664 .name = "Saturation",
665 .minimum = 0,
666 .maximum = 65535,
667 .step = 128,
668 .default_value = 32768,
669 .type = V4L2_CTRL_TYPE_INTEGER,
670 },{
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -0300671 .id = V4L2_CID_COLOR_KILLER,
672 .name = "Color killer",
673 .minimum = 0,
674 .maximum = 1,
675 .type = V4L2_CTRL_TYPE_BOOLEAN,
676 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 .id = V4L2_CID_HUE,
678 .name = "Hue",
679 .minimum = 0,
680 .maximum = 65535,
681 .step = 256,
682 .default_value = 32768,
683 .type = V4L2_CTRL_TYPE_INTEGER,
684 },
685 /* --- audio --- */
686 {
687 .id = V4L2_CID_AUDIO_MUTE,
688 .name = "Mute",
689 .minimum = 0,
690 .maximum = 1,
691 .type = V4L2_CTRL_TYPE_BOOLEAN,
692 },{
693 .id = V4L2_CID_AUDIO_VOLUME,
694 .name = "Volume",
695 .minimum = 0,
696 .maximum = 65535,
697 .step = 65535/100,
698 .default_value = 65535,
699 .type = V4L2_CTRL_TYPE_INTEGER,
700 },{
701 .id = V4L2_CID_AUDIO_BALANCE,
702 .name = "Balance",
703 .minimum = 0,
704 .maximum = 65535,
705 .step = 65535/100,
706 .default_value = 32768,
707 .type = V4L2_CTRL_TYPE_INTEGER,
708 },{
709 .id = V4L2_CID_AUDIO_BASS,
710 .name = "Bass",
711 .minimum = 0,
712 .maximum = 65535,
713 .step = 65535/100,
714 .default_value = 32768,
715 .type = V4L2_CTRL_TYPE_INTEGER,
716 },{
717 .id = V4L2_CID_AUDIO_TREBLE,
718 .name = "Treble",
719 .minimum = 0,
720 .maximum = 65535,
721 .step = 65535/100,
722 .default_value = 32768,
723 .type = V4L2_CTRL_TYPE_INTEGER,
724 },
725 /* --- private --- */
726 {
727 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
728 .name = "chroma agc",
729 .minimum = 0,
730 .maximum = 1,
731 .type = V4L2_CTRL_TYPE_BOOLEAN,
732 },{
733 .id = V4L2_CID_PRIVATE_COMBFILTER,
734 .name = "combfilter",
735 .minimum = 0,
736 .maximum = 1,
737 .type = V4L2_CTRL_TYPE_BOOLEAN,
738 },{
739 .id = V4L2_CID_PRIVATE_AUTOMUTE,
740 .name = "automute",
741 .minimum = 0,
742 .maximum = 1,
743 .type = V4L2_CTRL_TYPE_BOOLEAN,
744 },{
745 .id = V4L2_CID_PRIVATE_LUMAFILTER,
746 .name = "luma decimation filter",
747 .minimum = 0,
748 .maximum = 1,
749 .type = V4L2_CTRL_TYPE_BOOLEAN,
750 },{
751 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
752 .name = "agc crush",
753 .minimum = 0,
754 .maximum = 1,
755 .type = V4L2_CTRL_TYPE_BOOLEAN,
756 },{
757 .id = V4L2_CID_PRIVATE_VCR_HACK,
758 .name = "vcr hack",
759 .minimum = 0,
760 .maximum = 1,
761 .type = V4L2_CTRL_TYPE_BOOLEAN,
762 },{
763 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
764 .name = "whitecrush upper",
765 .minimum = 0,
766 .maximum = 255,
767 .step = 1,
768 .default_value = 0xCF,
769 .type = V4L2_CTRL_TYPE_INTEGER,
770 },{
771 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
772 .name = "whitecrush lower",
773 .minimum = 0,
774 .maximum = 255,
775 .step = 1,
776 .default_value = 0x7F,
777 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700778 },{
779 .id = V4L2_CID_PRIVATE_UV_RATIO,
780 .name = "uv ratio",
781 .minimum = 0,
782 .maximum = 100,
783 .step = 1,
784 .default_value = 50,
785 .type = V4L2_CTRL_TYPE_INTEGER,
786 },{
787 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
788 .name = "full luma range",
789 .minimum = 0,
790 .maximum = 1,
791 .type = V4L2_CTRL_TYPE_BOOLEAN,
792 },{
793 .id = V4L2_CID_PRIVATE_CORING,
794 .name = "coring",
795 .minimum = 0,
796 .maximum = 3,
797 .step = 1,
798 .default_value = 0,
799 .type = V4L2_CTRL_TYPE_INTEGER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700802
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300806static const struct v4l2_queryctrl *ctrl_by_id(int id)
807{
808 int i;
809
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300810 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300811 if (bttv_ctls[i].id == id)
812 return bttv_ctls+i;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300813
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300814 return NULL;
815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817/* ----------------------------------------------------------------------- */
818/* resource management */
819
Michael Schimeke5bd0262007-01-18 16:17:39 -0300820/*
821 RESOURCE_ allocated by freed by
822
823 VIDEO_READ bttv_read 1) bttv_read 2)
824
825 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
826 VIDIOC_QBUF 1) bttv_release
827 VIDIOCMCAPTURE 1)
828
829 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
830 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
831 3) bttv_release
832
833 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
834 VIDIOC_QBUF 1) bttv_release
835 bttv_read, bttv_poll 1) 4)
836
837 1) The resource must be allocated when we enter buffer prepare functions
838 and remain allocated while buffers are in the DMA queue.
839 2) This is a single frame read.
840 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
841 RESOURCE_OVERLAY is allocated.
842 4) This is a continuous read, implies VIDIOC_STREAMON.
843
844 Note this driver permits video input and standard changes regardless if
845 resources are allocated.
846*/
847
848#define VBI_RESOURCES (RESOURCE_VBI)
849#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
850 RESOURCE_VIDEO_STREAM | \
851 RESOURCE_OVERLAY)
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300854int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Michael Schimeke5bd0262007-01-18 16:17:39 -0300856 int xbits; /* mutual exclusive resources */
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (fh->resources & bit)
859 /* have it already allocated */
860 return 1;
861
Michael Schimeke5bd0262007-01-18 16:17:39 -0300862 xbits = bit;
863 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
864 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 /* is it free? */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300867 if (btv->resources & xbits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* no, someone else uses it */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300869 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300871
872 if ((bit & VIDEO_RESOURCES)
873 && 0 == (btv->resources & VIDEO_RESOURCES)) {
874 /* Do crop - use current, don't - use default parameters. */
875 __s32 top = btv->crop[!!fh->do_crop].rect.top;
876
877 if (btv->vbi_end > top)
878 goto fail;
879
880 /* We cannot capture the same line as video and VBI data.
881 Claim scan lines crop[].rect.top to bottom. */
882 btv->crop_start = top;
883 } else if (bit & VBI_RESOURCES) {
884 __s32 end = fh->vbi_fmt.end;
885
886 if (end > btv->crop_start)
887 goto fail;
888
889 /* Claim scan lines above fh->vbi_fmt.end. */
890 btv->vbi_end = end;
891 }
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* it's free, grab it */
894 fh->resources |= bit;
895 btv->resources |= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return 1;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300897
898 fail:
Michael Schimeke5bd0262007-01-18 16:17:39 -0300899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902static
903int check_btres(struct bttv_fh *fh, int bit)
904{
905 return (fh->resources & bit);
906}
907
908static
909int locked_btres(struct bttv *btv, int bit)
910{
911 return (btv->resources & bit);
912}
913
Michael Schimeke5bd0262007-01-18 16:17:39 -0300914/* Call with btv->lock down. */
915static void
916disclaim_vbi_lines(struct bttv *btv)
917{
918 btv->vbi_end = 0;
919}
920
921/* Call with btv->lock down. */
922static void
923disclaim_video_lines(struct bttv *btv)
924{
925 const struct bttv_tvnorm *tvnorm;
926 u8 crop;
927
928 tvnorm = &bttv_tvnorms[btv->tvnorm];
929 btv->crop_start = tvnorm->cropcap.bounds.top
930 + tvnorm->cropcap.bounds.height;
931
932 /* VBI capturing ends at VDELAY, start of video capturing, no
933 matter how many lines the VBI RISC program expects. When video
934 capturing is off, it shall no longer "preempt" VBI capturing,
935 so we set VDELAY to maximum. */
936 crop = btread(BT848_E_CROP) | 0xc0;
937 btwrite(crop, BT848_E_CROP);
938 btwrite(0xfe, BT848_E_VDELAY_LO);
939 btwrite(crop, BT848_O_CROP);
940 btwrite(0xfe, BT848_O_VDELAY_LO);
941}
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300944void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if ((fh->resources & bits) != bits) {
Joe Perches8af443e2011-08-21 19:56:48 -0300947 /* trying to free resources not allocated by us ... */
948 pr_err("BUG! (btres)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 fh->resources &= ~bits;
951 btv->resources &= ~bits;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300952
953 bits = btv->resources;
954
955 if (0 == (bits & VIDEO_RESOURCES))
956 disclaim_video_lines(btv);
957
958 if (0 == (bits & VBI_RESOURCES))
959 disclaim_vbi_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
962/* ----------------------------------------------------------------------- */
963/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
964
965/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
966 PLL_X = Reference pre-divider (0=1, 1=2)
967 PLL_C = Post divider (0=6, 1=4)
968 PLL_I = Integer input
969 PLL_F = Fractional input
970
971 F_input = 28.636363 MHz:
972 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
973*/
974
975static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
976{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800977 unsigned char fl, fh, fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800979 /* prevent overflows */
980 fin/=4;
981 fout/=4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800983 fout*=12;
984 fi=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800986 fout=(fout%fin)*256;
987 fh=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800989 fout=(fout%fin)*256;
990 fl=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800992 btwrite(fl, BT848_PLL_F_LO);
993 btwrite(fh, BT848_PLL_F_HI);
994 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997static void set_pll(struct bttv *btv)
998{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800999 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001001 if (!btv->pll.pll_crystal)
1002 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
Joe Perches8af443e2011-08-21 19:56:48 -03001005 dprintk("%d: PLL: no change required\n", btv->c.nr);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001006 return;
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001009 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1010 /* no PLL needed */
1011 if (btv->pll.pll_current == 0)
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001012 return;
Joe Perches8af443e2011-08-21 19:56:48 -03001013 if (bttv_verbose)
1014 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
1015 btv->c.nr, btv->pll.pll_ifreq);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001016 btwrite(0x00,BT848_TGCTRL);
1017 btwrite(0x00,BT848_PLL_XCI);
1018 btv->pll.pll_current = 0;
1019 return;
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Joe Perches8af443e2011-08-21 19:56:48 -03001022 if (bttv_verbose)
1023 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
1024 btv->c.nr,
1025 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1027
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001028 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 /* Let other people run while the PLL stabilizes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 msleep(10);
1031
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001032 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 btwrite(0,BT848_DSTATUS);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001034 } else {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001035 btwrite(0x08,BT848_TGCTRL);
1036 btv->pll.pll_current = btv->pll.pll_ofreq;
Joe Perches8af443e2011-08-21 19:56:48 -03001037 if (bttv_verbose)
1038 pr_info("PLL set ok\n");
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001039 return;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001040 }
1041 }
1042 btv->pll.pll_current = -1;
Joe Perches8af443e2011-08-21 19:56:48 -03001043 if (bttv_verbose)
1044 pr_info("Setting PLL failed\n");
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001045 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
1048/* used to switch between the bt848's analog/digital video capture modes */
1049static void bt848A_set_timing(struct bttv *btv)
1050{
1051 int i, len;
1052 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1053 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1054
Trent Piepho5221e212009-01-28 21:32:59 -03001055 if (btv->input == btv->dig) {
Joe Perches8af443e2011-08-21 19:56:48 -03001056 dprintk("%d: load digital timing table (table_idx=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 btv->c.nr,table_idx);
1058
1059 /* timing change...reset timing generator address */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001060 btwrite(0x00, BT848_TGCTRL);
1061 btwrite(0x02, BT848_TGCTRL);
1062 btwrite(0x00, BT848_TGCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 len=SRAM_Table[table_idx][0];
1065 for(i = 1; i <= len; i++)
1066 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1067 btv->pll.pll_ofreq = 27000000;
1068
1069 set_pll(btv);
1070 btwrite(0x11, BT848_TGCTRL);
1071 btwrite(0x41, BT848_DVSIF);
1072 } else {
1073 btv->pll.pll_ofreq = fsc;
1074 set_pll(btv);
1075 btwrite(0x0, BT848_DVSIF);
1076 }
1077}
1078
1079/* ----------------------------------------------------------------------- */
1080
1081static void bt848_bright(struct bttv *btv, int bright)
1082{
1083 int value;
1084
Joe Perches8af443e2011-08-21 19:56:48 -03001085 // printk("set bright: %d\n", bright); // DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 btv->bright = bright;
1087
1088 /* We want -128 to 127 we get 0-65535 */
1089 value = (bright >> 8) - 128;
1090 btwrite(value & 0xff, BT848_BRIGHT);
1091}
1092
1093static void bt848_hue(struct bttv *btv, int hue)
1094{
1095 int value;
1096
1097 btv->hue = hue;
1098
1099 /* -128 to 127 */
1100 value = (hue >> 8) - 128;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001101 btwrite(value & 0xff, BT848_HUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104static void bt848_contrast(struct bttv *btv, int cont)
1105{
1106 int value,hibit;
1107
1108 btv->contrast = cont;
1109
1110 /* 0-511 */
1111 value = (cont >> 7);
1112 hibit = (value >> 6) & 4;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001113 btwrite(value & 0xff, BT848_CONTRAST_LO);
1114 btaor(hibit, ~4, BT848_E_CONTROL);
1115 btaor(hibit, ~4, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118static void bt848_sat(struct bttv *btv, int color)
1119{
1120 int val_u,val_v,hibits;
1121
1122 btv->saturation = color;
1123
1124 /* 0-511 for the color */
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001125 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1126 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001127 hibits = (val_u >> 7) & 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 hibits |= (val_v >> 8) & 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001129 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1130 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1131 btaor(hibits, ~3, BT848_E_CONTROL);
1132 btaor(hibits, ~3, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
1135/* ----------------------------------------------------------------------- */
1136
1137static int
1138video_mux(struct bttv *btv, unsigned int input)
1139{
1140 int mux,mask2;
1141
1142 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1143 return -EINVAL;
1144
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001145 /* needed by RemoteVideo MX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1147 if (mask2)
1148 gpio_inout(mask2,mask2);
1149
1150 if (input == btv->svhs) {
1151 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1152 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1153 } else {
1154 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1155 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1156 }
Trent Piepho6f987002009-01-28 21:32:59 -03001157 mux = bttv_muxsel(btv, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 btaor(mux<<5, ~(3<<5), BT848_IFORM);
Joe Perches8af443e2011-08-21 19:56:48 -03001159 dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 /* card specific hook */
1162 if(bttv_tvcards[btv->c.type].muxsel_hook)
1163 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1164 return 0;
1165}
1166
1167static char *audio_modes[] = {
1168 "audio: tuner", "audio: radio", "audio: extern",
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001169 "audio: intern", "audio: mute"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170};
1171
1172static int
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001173audio_mux(struct bttv *btv, int input, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001175 int gpio_val, signal;
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001176 struct v4l2_control ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1179 bttv_tvcards[btv->c.type].gpiomask);
1180 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1181
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001182 btv->mute = mute;
1183 btv->audio = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001185 /* automute */
1186 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1187
1188 if (mute)
1189 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1190 else
1191 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001192
Trent Piepho72134a62009-01-28 21:32:59 -03001193 switch (btv->c.type) {
1194 case BTTV_BOARD_VOODOOTV_FM:
1195 case BTTV_BOARD_VOODOOTV_200:
1196 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1197 break;
1198
1199 default:
1200 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1201 }
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (bttv_gpio)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001204 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1205 if (in_interrupt())
1206 return 0;
1207
1208 ctrl.id = V4L2_CID_AUDIO_MUTE;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001209 ctrl.value = btv->mute;
Hans Verkuil859f0272009-03-28 08:29:00 -03001210 bttv_call_all(btv, core, s_ctrl, &ctrl);
1211 if (btv->sd_msp34xx) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001212 u32 in;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001213
1214 /* Note: the inputs tuner/radio/extern/intern are translated
1215 to msp routings. This assumes common behavior for all msp3400
1216 based TV cards. When this assumption fails, then the
1217 specific MSP routing must be added to the card table.
1218 For now this is sufficient. */
1219 switch (input) {
1220 case TVAUDIO_INPUT_RADIO:
Hans de Goede7025e522012-05-21 07:46:22 -03001221 /* Some boards need the msp do to the radio demod */
1222 if (btv->radio_uses_msp_demodulator) {
1223 in = MSP_INPUT_DEFAULT;
1224 break;
1225 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001226 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001227 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001228 break;
1229 case TVAUDIO_INPUT_EXTERN:
Hans Verkuil5325b422009-04-02 11:26:22 -03001230 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001231 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001232 break;
1233 case TVAUDIO_INPUT_INTERN:
1234 /* Yes, this is the same input as for RADIO. I doubt
1235 if this is ever used. The only board with an INTERN
1236 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1237 that was tested. My guess is that the whole INTERN
1238 input does not work. */
Hans Verkuil5325b422009-04-02 11:26:22 -03001239 in = MSP_INPUT(MSP_IN_SCART2, 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_TUNER:
1243 default:
Wade Berrier434b2522007-06-25 13:02:16 -03001244 /* This is the only card that uses TUNER2, and afaik,
1245 is the only difference between the VOODOOTV_FM
1246 and VOODOOTV_200 */
1247 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
Hans Verkuil5325b422009-04-02 11:26:22 -03001248 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
Wade Berrier434b2522007-06-25 13:02:16 -03001249 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1250 else
Hans Verkuil5325b422009-04-02 11:26:22 -03001251 in = MSP_INPUT_DEFAULT;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001252 break;
1253 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001254 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1255 in, MSP_OUTPUT_DEFAULT, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001256 }
Hans Verkuil859f0272009-03-28 08:29:00 -03001257 if (btv->sd_tvaudio) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001258 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1259 input, 0, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return 0;
1262}
1263
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001264static inline int
1265audio_mute(struct bttv *btv, int mute)
1266{
1267 return audio_mux(btv, btv->audio, mute);
1268}
1269
1270static inline int
1271audio_input(struct bttv *btv, int input)
1272{
1273 return audio_mux(btv, input, btv->mute);
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276static void
Michael Schimeke5bd0262007-01-18 16:17:39 -03001277bttv_crop_calc_limits(struct bttv_crop *c)
1278{
1279 /* Scale factor min. 1:1, max. 16:1. Min. image size
1280 48 x 32. Scaled width must be a multiple of 4. */
1281
1282 if (1) {
1283 /* For bug compatibility with VIDIOCGCAP and image
1284 size checks in earlier driver versions. */
1285 c->min_scaled_width = 48;
1286 c->min_scaled_height = 32;
1287 } else {
1288 c->min_scaled_width =
1289 (max(48, c->rect.width >> 4) + 3) & ~3;
1290 c->min_scaled_height =
1291 max(32, c->rect.height >> 4);
1292 }
1293
1294 c->max_scaled_width = c->rect.width & ~3;
1295 c->max_scaled_height = c->rect.height;
1296}
1297
1298static void
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001299bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
Michael Schimeke5bd0262007-01-18 16:17:39 -03001300{
1301 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1302 bttv_crop_calc_limits(c);
1303}
1304
1305/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306static int
1307set_tvnorm(struct bttv *btv, unsigned int norm)
1308{
1309 const struct bttv_tvnorm *tvnorm;
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001310 v4l2_std_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001312 BUG_ON(norm >= BTTV_TVNORMS);
1313 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 tvnorm = &bttv_tvnorms[norm];
1316
Mike Isely2de26c02009-09-21 12:42:22 -03001317 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001318 sizeof (tvnorm->cropcap))) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001319 bttv_crop_reset(&btv->crop[0], norm);
1320 btv->crop[1] = btv->crop[0]; /* current = default */
1321
1322 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1323 btv->crop_start = tvnorm->cropcap.bounds.top
1324 + tvnorm->cropcap.bounds.height;
1325 }
1326 }
1327
1328 btv->tvnorm = norm;
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 btwrite(tvnorm->adelay, BT848_ADELAY);
1331 btwrite(tvnorm->bdelay, BT848_BDELAY);
1332 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1333 BT848_IFORM);
1334 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1335 btwrite(1, BT848_VBI_PACK_DEL);
1336 bt848A_set_timing(btv);
1337
1338 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -08001339 case BTTV_BOARD_VOODOOTV_FM:
Wade Berrier434b2522007-06-25 13:02:16 -03001340 case BTTV_BOARD_VOODOOTV_200:
Trent Piepho72134a62009-01-28 21:32:59 -03001341 bttv_tda9880_setnorm(btv, gpio_read());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001344 id = tvnorm->v4l2_id;
Hans Verkuilf41737e2009-04-01 03:52:39 -03001345 bttv_call_all(btv, core, s_std, id);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return 0;
1348}
1349
Michael Schimeke5bd0262007-01-18 16:17:39 -03001350/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351static void
Trent Piepho333408f2007-07-03 15:08:10 -03001352set_input(struct bttv *btv, unsigned int input, unsigned int norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 unsigned long flags;
1355
1356 btv->input = input;
1357 if (irq_iswitch) {
1358 spin_lock_irqsave(&btv->s_lock,flags);
1359 if (btv->curr.frame_irq) {
1360 /* active capture -> delayed input switch */
1361 btv->new_input = input;
1362 } else {
1363 video_mux(btv,input);
1364 }
1365 spin_unlock_irqrestore(&btv->s_lock,flags);
1366 } else {
1367 video_mux(btv,input);
1368 }
Trent Piephoabb03622009-01-28 21:32:59 -03001369 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1370 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
Trent Piepho333408f2007-07-03 15:08:10 -03001371 set_tvnorm(btv, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
1374static void init_irqreg(struct bttv *btv)
1375{
1376 /* clear status */
1377 btwrite(0xfffffUL, BT848_INT_STAT);
1378
1379 if (bttv_tvcards[btv->c.type].no_video) {
1380 /* i2c only */
1381 btwrite(BT848_INT_I2CDONE,
1382 BT848_INT_MASK);
1383 } else {
1384 /* full video */
1385 btwrite((btv->triton1) |
1386 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1387 BT848_INT_SCERR |
1388 (fdsr ? BT848_INT_FDSR : 0) |
Jean Delvareeb1b27b2008-08-30 10:18:21 -03001389 BT848_INT_RISCI | BT848_INT_OCERR |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1391 BT848_INT_I2CDONE,
1392 BT848_INT_MASK);
1393 }
1394}
1395
1396static void init_bt848(struct bttv *btv)
1397{
1398 int val;
1399
1400 if (bttv_tvcards[btv->c.type].no_video) {
1401 /* very basic init only */
1402 init_irqreg(btv);
1403 return;
1404 }
1405
1406 btwrite(0x00, BT848_CAP_CTL);
1407 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1408 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1409
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001410 /* set planar and packed mode trigger points and */
1411 /* set rising edge of inverted GPINTR pin as irq trigger */
1412 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1413 BT848_GPIO_DMA_CTL_PLTP1_16|
1414 BT848_GPIO_DMA_CTL_PLTP23_16|
1415 BT848_GPIO_DMA_CTL_GPINTC|
1416 BT848_GPIO_DMA_CTL_GPINTI,
1417 BT848_GPIO_DMA_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001420 btwrite(val, BT848_E_SCLOOP);
1421 btwrite(val, BT848_O_SCLOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001423 btwrite(0x20, BT848_E_VSCALE_HI);
1424 btwrite(0x20, BT848_O_VSCALE_HI);
1425 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 BT848_ADC);
1427
1428 btwrite(whitecrush_upper, BT848_WC_UP);
1429 btwrite(whitecrush_lower, BT848_WC_DOWN);
1430
1431 if (btv->opt_lumafilter) {
1432 btwrite(0, BT848_E_CONTROL);
1433 btwrite(0, BT848_O_CONTROL);
1434 } else {
1435 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1436 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1437 }
1438
1439 bt848_bright(btv, btv->bright);
1440 bt848_hue(btv, btv->hue);
1441 bt848_contrast(btv, btv->contrast);
1442 bt848_sat(btv, btv->saturation);
1443
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001444 /* interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 init_irqreg(btv);
1446}
1447
1448static void bttv_reinit_bt848(struct bttv *btv)
1449{
1450 unsigned long flags;
1451
1452 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03001453 pr_info("%d: reset, reinitialize\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 spin_lock_irqsave(&btv->s_lock,flags);
1455 btv->errors=0;
1456 bttv_set_dma(btv,0);
1457 spin_unlock_irqrestore(&btv->s_lock,flags);
1458
1459 init_bt848(btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001460 btv->pll.pll_current = -1;
Trent Piepho333408f2007-07-03 15:08:10 -03001461 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001464static int bttv_g_ctrl(struct file *file, void *priv,
1465 struct v4l2_control *c)
1466{
1467 struct bttv_fh *fh = priv;
1468 struct bttv *btv = fh->btv;
1469
1470 switch (c->id) {
1471 case V4L2_CID_BRIGHTNESS:
1472 c->value = btv->bright;
1473 break;
1474 case V4L2_CID_HUE:
1475 c->value = btv->hue;
1476 break;
1477 case V4L2_CID_CONTRAST:
1478 c->value = btv->contrast;
1479 break;
1480 case V4L2_CID_SATURATION:
1481 c->value = btv->saturation;
1482 break;
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -03001483 case V4L2_CID_COLOR_KILLER:
1484 c->value = btv->opt_color_killer;
1485 break;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001486
1487 case V4L2_CID_AUDIO_MUTE:
1488 case V4L2_CID_AUDIO_VOLUME:
1489 case V4L2_CID_AUDIO_BALANCE:
1490 case V4L2_CID_AUDIO_BASS:
1491 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil859f0272009-03-28 08:29:00 -03001492 bttv_call_all(btv, core, g_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001493 break;
1494
1495 case V4L2_CID_PRIVATE_CHROMA_AGC:
1496 c->value = btv->opt_chroma_agc;
1497 break;
1498 case V4L2_CID_PRIVATE_COMBFILTER:
1499 c->value = btv->opt_combfilter;
1500 break;
1501 case V4L2_CID_PRIVATE_LUMAFILTER:
1502 c->value = btv->opt_lumafilter;
1503 break;
1504 case V4L2_CID_PRIVATE_AUTOMUTE:
1505 c->value = btv->opt_automute;
1506 break;
1507 case V4L2_CID_PRIVATE_AGC_CRUSH:
1508 c->value = btv->opt_adc_crush;
1509 break;
1510 case V4L2_CID_PRIVATE_VCR_HACK:
1511 c->value = btv->opt_vcr_hack;
1512 break;
1513 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1514 c->value = btv->opt_whitecrush_upper;
1515 break;
1516 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1517 c->value = btv->opt_whitecrush_lower;
1518 break;
1519 case V4L2_CID_PRIVATE_UV_RATIO:
1520 c->value = btv->opt_uv_ratio;
1521 break;
1522 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1523 c->value = btv->opt_full_luma_range;
1524 break;
1525 case V4L2_CID_PRIVATE_CORING:
1526 c->value = btv->opt_coring;
1527 break;
1528 default:
1529 return -EINVAL;
1530 }
1531 return 0;
1532}
1533
1534static int bttv_s_ctrl(struct file *file, void *f,
1535 struct v4l2_control *c)
1536{
1537 int err;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001538 struct bttv_fh *fh = f;
1539 struct bttv *btv = fh->btv;
1540
Hans Verkuilffb48772010-05-01 08:03:24 -03001541 err = v4l2_prio_check(&btv->prio, fh->prio);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001542 if (0 != err)
1543 return err;
1544
1545 switch (c->id) {
1546 case V4L2_CID_BRIGHTNESS:
1547 bt848_bright(btv, c->value);
1548 break;
1549 case V4L2_CID_HUE:
1550 bt848_hue(btv, c->value);
1551 break;
1552 case V4L2_CID_CONTRAST:
1553 bt848_contrast(btv, c->value);
1554 break;
1555 case V4L2_CID_SATURATION:
1556 bt848_sat(btv, c->value);
1557 break;
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -03001558 case V4L2_CID_COLOR_KILLER:
1559 btv->opt_color_killer = c->value;
1560 if (btv->opt_color_killer) {
1561 btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1562 btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1563 } else {
1564 btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1565 btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1566 }
1567 break;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001568 case V4L2_CID_AUDIO_MUTE:
1569 audio_mute(btv, c->value);
1570 /* fall through */
1571 case V4L2_CID_AUDIO_VOLUME:
1572 if (btv->volume_gpio)
1573 btv->volume_gpio(btv, c->value);
1574
Hans Verkuil859f0272009-03-28 08:29:00 -03001575 bttv_call_all(btv, core, s_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001576 break;
1577 case V4L2_CID_AUDIO_BALANCE:
1578 case V4L2_CID_AUDIO_BASS:
1579 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil859f0272009-03-28 08:29:00 -03001580 bttv_call_all(btv, core, s_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001581 break;
1582
1583 case V4L2_CID_PRIVATE_CHROMA_AGC:
1584 btv->opt_chroma_agc = c->value;
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -03001585 if (btv->opt_chroma_agc) {
1586 btor(BT848_SCLOOP_CAGC, BT848_E_SCLOOP);
1587 btor(BT848_SCLOOP_CAGC, BT848_O_SCLOOP);
1588 } else {
1589 btand(~BT848_SCLOOP_CAGC, BT848_E_SCLOOP);
1590 btand(~BT848_SCLOOP_CAGC, BT848_O_SCLOOP);
1591 }
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001592 break;
1593 case V4L2_CID_PRIVATE_COMBFILTER:
1594 btv->opt_combfilter = c->value;
1595 break;
1596 case V4L2_CID_PRIVATE_LUMAFILTER:
1597 btv->opt_lumafilter = c->value;
1598 if (btv->opt_lumafilter) {
1599 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1600 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1601 } else {
1602 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1603 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1604 }
1605 break;
1606 case V4L2_CID_PRIVATE_AUTOMUTE:
1607 btv->opt_automute = c->value;
1608 break;
1609 case V4L2_CID_PRIVATE_AGC_CRUSH:
1610 btv->opt_adc_crush = c->value;
1611 btwrite(BT848_ADC_RESERVED |
1612 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1613 BT848_ADC);
1614 break;
1615 case V4L2_CID_PRIVATE_VCR_HACK:
1616 btv->opt_vcr_hack = c->value;
1617 break;
1618 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1619 btv->opt_whitecrush_upper = c->value;
1620 btwrite(c->value, BT848_WC_UP);
1621 break;
1622 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1623 btv->opt_whitecrush_lower = c->value;
1624 btwrite(c->value, BT848_WC_DOWN);
1625 break;
1626 case V4L2_CID_PRIVATE_UV_RATIO:
1627 btv->opt_uv_ratio = c->value;
1628 bt848_sat(btv, btv->saturation);
1629 break;
1630 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1631 btv->opt_full_luma_range = c->value;
1632 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1633 break;
1634 case V4L2_CID_PRIVATE_CORING:
1635 btv->opt_coring = c->value;
1636 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1637 break;
1638 default:
1639 return -EINVAL;
1640 }
1641 return 0;
1642}
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644/* ----------------------------------------------------------------------- */
1645
1646void bttv_gpio_tracking(struct bttv *btv, char *comment)
1647{
1648 unsigned int outbits, data;
1649 outbits = btread(BT848_GPIO_OUT_EN);
1650 data = btread(BT848_GPIO_DATA);
Joe Perches8af443e2011-08-21 19:56:48 -03001651 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1652 btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
1655static void bttv_field_count(struct bttv *btv)
1656{
1657 int need_count = 0;
1658
1659 if (btv->users)
1660 need_count++;
1661
1662 if (need_count) {
1663 /* start field counter */
1664 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1665 } else {
1666 /* stop field counter */
1667 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1668 btv->field_count = 0;
1669 }
1670}
1671
1672static const struct bttv_format*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673format_by_fourcc(int fourcc)
1674{
1675 unsigned int i;
1676
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001677 for (i = 0; i < FORMATS; i++) {
1678 if (-1 == formats[i].fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 continue;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001680 if (formats[i].fourcc == fourcc)
1681 return formats+i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683 return NULL;
1684}
1685
1686/* ----------------------------------------------------------------------- */
1687/* misc helpers */
1688
1689static int
1690bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1691 struct bttv_buffer *new)
1692{
1693 struct bttv_buffer *old;
1694 unsigned long flags;
1695 int retval = 0;
1696
Joe Perches8af443e2011-08-21 19:56:48 -03001697 dprintk("switch_overlay: enter [new=%p]\n", new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (new)
Brandon Philips0fc06862007-11-06 20:02:36 -03001699 new->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 spin_lock_irqsave(&btv->s_lock,flags);
1701 old = btv->screen;
1702 btv->screen = new;
1703 btv->loop_irq |= 1;
1704 bttv_set_dma(btv, 0x03);
1705 spin_unlock_irqrestore(&btv->s_lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (NULL != old) {
Joe Perches8af443e2011-08-21 19:56:48 -03001707 dprintk("switch_overlay: old=%p state is %d\n",
1708 old, old->vb.state);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001709 bttv_dma_free(&fh->cap,btv, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 kfree(old);
1711 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03001712 if (NULL == new)
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03001713 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 dprintk("switch_overlay: done\n");
1715 return retval;
1716}
1717
1718/* ----------------------------------------------------------------------- */
1719/* video4linux (1) interface */
1720
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001721static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1722 struct bttv_buffer *buf,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001723 const struct bttv_format *fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 unsigned int width, unsigned int height,
1725 enum v4l2_field field)
1726{
Michael Schimeke5bd0262007-01-18 16:17:39 -03001727 struct bttv_fh *fh = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 int redo_dma_risc = 0;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001729 struct bttv_crop c;
1730 int norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 int rc;
1732
1733 /* check settings */
1734 if (NULL == fmt)
1735 return -EINVAL;
1736 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1737 width = RAW_BPL;
1738 height = RAW_LINES*2;
1739 if (width*height > buf->vb.bsize)
1740 return -EINVAL;
1741 buf->vb.size = buf->vb.bsize;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001742
1743 /* Make sure tvnorm and vbi_end remain consistent
1744 until we're done. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03001745
1746 norm = btv->tvnorm;
1747
1748 /* In this mode capturing always starts at defrect.top
1749 (default VDELAY), ignoring cropping parameters. */
1750 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001752 }
1753
Michael Schimeke5bd0262007-01-18 16:17:39 -03001754 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1755 } else {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001756 norm = btv->tvnorm;
1757 c = btv->crop[!!fh->do_crop];
1758
Michael Schimeke5bd0262007-01-18 16:17:39 -03001759 if (width < c.min_scaled_width ||
1760 width > c.max_scaled_width ||
1761 height < c.min_scaled_height)
1762 return -EINVAL;
1763
1764 switch (field) {
1765 case V4L2_FIELD_TOP:
1766 case V4L2_FIELD_BOTTOM:
1767 case V4L2_FIELD_ALTERNATE:
1768 /* btv->crop counts frame lines. Max. scale
1769 factor is 16:1 for frames, 8:1 for fields. */
1770 if (height * 2 > c.max_scaled_height)
1771 return -EINVAL;
1772 break;
1773
1774 default:
1775 if (height > c.max_scaled_height)
1776 return -EINVAL;
1777 break;
1778 }
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 buf->vb.size = (width * height * fmt->depth) >> 3;
1781 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1782 return -EINVAL;
1783 }
1784
1785 /* alloc + fill struct bttv_buffer (if changed) */
1786 if (buf->vb.width != width || buf->vb.height != height ||
1787 buf->vb.field != field ||
Michael Schimeke5bd0262007-01-18 16:17:39 -03001788 buf->tvnorm != norm || buf->fmt != fmt ||
1789 buf->crop.top != c.rect.top ||
1790 buf->crop.left != c.rect.left ||
1791 buf->crop.width != c.rect.width ||
1792 buf->crop.height != c.rect.height) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 buf->vb.width = width;
1794 buf->vb.height = height;
1795 buf->vb.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001796 buf->tvnorm = norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 buf->fmt = fmt;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001798 buf->crop = c.rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 redo_dma_risc = 1;
1800 }
1801
1802 /* alloc risc memory */
Brandon Philips0fc06862007-11-06 20:02:36 -03001803 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 redo_dma_risc = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001805 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto fail;
1807 }
1808
1809 if (redo_dma_risc)
1810 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1811 goto fail;
1812
Brandon Philips0fc06862007-11-06 20:02:36 -03001813 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return 0;
1815
1816 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001817 bttv_dma_free(q,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return rc;
1819}
1820
1821static int
1822buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1823{
1824 struct bttv_fh *fh = q->priv_data;
1825
1826 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1827 if (0 == *count)
1828 *count = gbuffers;
Andreas Bombedab7e312010-03-21 16:02:45 -03001829 if (*size * *count > gbuffers * gbufsize)
1830 *count = (gbuffers * gbufsize) / *size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return 0;
1832}
1833
1834static int
1835buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1836 enum v4l2_field field)
1837{
1838 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1839 struct bttv_fh *fh = q->priv_data;
1840
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001841 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 fh->width, fh->height, field);
1843}
1844
1845static void
1846buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1847{
1848 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1849 struct bttv_fh *fh = q->priv_data;
1850 struct bttv *btv = fh->btv;
1851
Brandon Philips0fc06862007-11-06 20:02:36 -03001852 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 list_add_tail(&buf->vb.queue,&btv->capture);
1854 if (!btv->curr.frame_irq) {
1855 btv->loop_irq |= 1;
1856 bttv_set_dma(btv, 0x03);
1857 }
1858}
1859
1860static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1861{
1862 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1863 struct bttv_fh *fh = q->priv_data;
1864
Michael Schimekfeaba7a2007-01-26 08:30:05 -03001865 bttv_dma_free(q,fh->btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
1868static struct videobuf_queue_ops bttv_video_qops = {
1869 .buf_setup = buffer_setup,
1870 .buf_prepare = buffer_prepare,
1871 .buf_queue = buffer_queue,
1872 .buf_release = buffer_release,
1873};
1874
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001875static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001877 struct bttv_fh *fh = priv;
1878 struct bttv *btv = fh->btv;
1879 unsigned int i;
1880 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Hans Verkuilffb48772010-05-01 08:03:24 -03001882 err = v4l2_prio_check(&btv->prio, fh->prio);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001883 if (err)
1884 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001886 for (i = 0; i < BTTV_TVNORMS; i++)
1887 if (*id & bttv_tvnorms[i].v4l2_id)
1888 break;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001889 if (i == BTTV_TVNORMS) {
1890 err = -EINVAL;
1891 goto err;
1892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001894 set_tvnorm(btv, i);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001895
1896err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001898 return err;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001899}
1900
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001901static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001902{
1903 struct bttv_fh *fh = f;
1904 struct bttv *btv = fh->btv;
1905
1906 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1907 *id = V4L2_STD_625_50;
1908 else
1909 *id = V4L2_STD_525_60;
1910 return 0;
1911}
1912
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001913static int bttv_enum_input(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001914 struct v4l2_input *i)
1915{
1916 struct bttv_fh *fh = priv;
1917 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001918 int rc = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001919
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001920 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) {
1921 rc = -EINVAL;
1922 goto err;
1923 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001924
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001925 i->type = V4L2_INPUT_TYPE_CAMERA;
Hans Verkuilf74f89cb2013-01-31 08:45:13 -03001926 i->audioset = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001927
Trent Piephoabb03622009-01-28 21:32:59 -03001928 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001929 sprintf(i->name, "Television");
1930 i->type = V4L2_INPUT_TYPE_TUNER;
1931 i->tuner = 0;
1932 } else if (i->index == btv->svhs) {
1933 sprintf(i->name, "S-Video");
1934 } else {
1935 sprintf(i->name, "Composite%d", i->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001938 if (i->index == btv->input) {
1939 __u32 dstatus = btread(BT848_DSTATUS);
1940 if (0 == (dstatus & BT848_DSTATUS_PRES))
1941 i->status |= V4L2_IN_ST_NO_SIGNAL;
1942 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1943 i->status |= V4L2_IN_ST_NO_H_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001946 i->std = BTTV_NORMS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001948err:
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001949
1950 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001951}
Nickolay V. Shmyrev4b9b9362006-08-25 16:53:04 -03001952
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001953static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001954{
1955 struct bttv_fh *fh = priv;
1956 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001958 *i = btv->input;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001959
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001960 return 0;
1961}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001963static int bttv_s_input(struct file *file, void *priv, unsigned int i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001964{
1965 struct bttv_fh *fh = priv;
1966 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001967 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001969 err = v4l2_prio_check(&btv->prio, fh->prio);
Hans Verkuilf74f89cb2013-01-31 08:45:13 -03001970 if (err)
1971 return err;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001972
Hans Verkuilf74f89cb2013-01-31 08:45:13 -03001973 if (i >= bttv_tvcards[btv->c.type].video_inputs)
1974 return -EINVAL;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001975
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001976 set_input(btv, i, btv->tvnorm);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001977 return 0;
1978}
1979
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001980static int bttv_s_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001981 struct v4l2_tuner *t)
1982{
1983 struct bttv_fh *fh = priv;
1984 struct bttv *btv = fh->btv;
1985 int err;
1986
Hans Verkuild9b67072013-02-06 11:43:07 -03001987 if (t->index)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001988 return -EINVAL;
1989
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001990 err = v4l2_prio_check(&btv->prio, fh->prio);
Hans Verkuild9b67072013-02-06 11:43:07 -03001991 if (err)
1992 return err;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001993
Hans Verkuil859f0272009-03-28 08:29:00 -03001994 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001995
1996 if (btv->audio_mode_gpio)
1997 btv->audio_mode_gpio(btv, t, 1);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001998 return 0;
1999}
2000
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002001static int bttv_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002002 struct v4l2_frequency *f)
2003{
2004 struct bttv_fh *fh = priv;
2005 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002006
Hans Verkuild9b67072013-02-06 11:43:07 -03002007 if (f->tuner)
2008 return -EINVAL;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002009
Hans Verkuil76ea9922013-02-06 11:49:14 -03002010 f->frequency = f->type == V4L2_TUNER_RADIO ?
2011 btv->radio_freq : btv->tv_freq;
2012
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002013 return 0;
2014}
2015
Hans Verkuil76ea9922013-02-06 11:49:14 -03002016static void bttv_set_frequency(struct bttv *btv, struct v4l2_frequency *f)
2017{
2018 bttv_call_all(btv, tuner, s_frequency, f);
2019 /* s_frequency may clamp the frequency, so get the actual
2020 frequency before assigning radio/tv_freq. */
2021 bttv_call_all(btv, tuner, g_frequency, f);
2022 if (f->type == V4L2_TUNER_RADIO) {
2023 btv->radio_freq = f->frequency;
2024 if (btv->has_matchbox)
2025 tea5757_set_freq(btv, btv->radio_freq);
2026 } else {
2027 btv->tv_freq = f->frequency;
2028 }
2029}
2030
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002031static int bttv_s_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002032 struct v4l2_frequency *f)
2033{
2034 struct bttv_fh *fh = priv;
2035 struct bttv *btv = fh->btv;
2036 int err;
2037
Hans Verkuild9b67072013-02-06 11:43:07 -03002038 if (f->tuner)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002039 return -EINVAL;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002040
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002041 err = v4l2_prio_check(&btv->prio, fh->prio);
Hans Verkuild9b67072013-02-06 11:43:07 -03002042 if (err)
2043 return err;
Hans Verkuil76ea9922013-02-06 11:49:14 -03002044 bttv_set_frequency(btv, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002045 return 0;
2046}
2047
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002048static int bttv_log_status(struct file *file, void *f)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002049{
2050 struct bttv_fh *fh = f;
2051 struct bttv *btv = fh->btv;
2052
Hans Verkuil859f0272009-03-28 08:29:00 -03002053 bttv_call_all(btv, core, log_status);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002054 return 0;
2055}
2056
Hans Verkuil1b9e94d2012-09-09 09:23:31 -03002057static int bttv_g_chip_ident(struct file *file, void *f, struct v4l2_dbg_chip_ident *chip)
2058{
2059 struct bttv_fh *fh = f;
2060 struct bttv *btv = fh->btv;
2061
2062 chip->ident = V4L2_IDENT_NONE;
2063 chip->revision = 0;
2064 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
2065 if (v4l2_chip_match_host(&chip->match)) {
2066 chip->ident = btv->id;
2067 if (chip->ident == PCI_DEVICE_ID_FUSION879)
2068 chip->ident = V4L2_IDENT_BT879;
2069 }
2070 return 0;
2071 }
2072 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
2073 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2074 return -EINVAL;
2075 /* TODO: is this correct? */
2076 return bttv_call_all_err(btv, core, g_chip_ident, chip);
2077}
2078
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002079#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002080static int bttv_g_register(struct file *file, void *f,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002081 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002082{
2083 struct bttv_fh *fh = f;
2084 struct bttv *btv = fh->btv;
2085
2086 if (!capable(CAP_SYS_ADMIN))
2087 return -EPERM;
2088
Hans Verkuil1b9e94d2012-09-09 09:23:31 -03002089 if (!v4l2_chip_match_host(&reg->match)) {
2090 /* TODO: subdev errors should not be ignored, this should become a
2091 subdev helper function. */
2092 bttv_call_all(btv, core, g_register, reg);
2093 return 0;
2094 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002095
2096 /* bt848 has a 12-bit register space */
2097 reg->reg &= 0xfff;
2098 reg->val = btread(reg->reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002099 reg->size = 1;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002100
2101 return 0;
2102}
2103
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002104static int bttv_s_register(struct file *file, void *f,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002105 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002106{
2107 struct bttv_fh *fh = f;
2108 struct bttv *btv = fh->btv;
2109
2110 if (!capable(CAP_SYS_ADMIN))
2111 return -EPERM;
2112
Hans Verkuil1b9e94d2012-09-09 09:23:31 -03002113 if (!v4l2_chip_match_host(&reg->match)) {
2114 /* TODO: subdev errors should not be ignored, this should become a
2115 subdev helper function. */
2116 bttv_call_all(btv, core, s_register, reg);
2117 return 0;
2118 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002119
2120 /* bt848 has a 12-bit register space */
2121 reg->reg &= 0xfff;
2122 btwrite(reg->val, reg->reg);
2123
2124 return 0;
2125}
2126#endif
2127
Michael Schimeke5bd0262007-01-18 16:17:39 -03002128/* Given cropping boundaries b and the scaled width and height of a
2129 single field or frame, which must not exceed hardware limits, this
2130 function adjusts the cropping parameters c. */
2131static void
2132bttv_crop_adjust (struct bttv_crop * c,
2133 const struct v4l2_rect * b,
2134 __s32 width,
2135 __s32 height,
2136 enum v4l2_field field)
2137{
2138 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2139 __s32 max_left;
2140 __s32 max_top;
2141
2142 if (width < c->min_scaled_width) {
2143 /* Max. hor. scale factor 16:1. */
2144 c->rect.width = width * 16;
2145 } else if (width > c->max_scaled_width) {
2146 /* Min. hor. scale factor 1:1. */
2147 c->rect.width = width;
2148
2149 max_left = b->left + b->width - width;
2150 max_left = min(max_left, (__s32) MAX_HDELAY);
2151 if (c->rect.left > max_left)
2152 c->rect.left = max_left;
2153 }
2154
2155 if (height < c->min_scaled_height) {
2156 /* Max. vert. scale factor 16:1, single fields 8:1. */
2157 c->rect.height = height * 16;
2158 } else if (frame_height > c->max_scaled_height) {
2159 /* Min. vert. scale factor 1:1.
2160 Top and height count field lines times two. */
2161 c->rect.height = (frame_height + 1) & ~1;
2162
2163 max_top = b->top + b->height - c->rect.height;
2164 if (c->rect.top > max_top)
2165 c->rect.top = max_top;
2166 }
2167
2168 bttv_crop_calc_limits(c);
2169}
2170
2171/* Returns an error if scaling to a frame or single field with the given
2172 width and height is not possible with the current cropping parameters
2173 and width aligned according to width_mask. If adjust_size is TRUE the
2174 function may adjust the width and/or height instead, rounding width
2175 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2176 also adjust the current cropping parameters to get closer to the
2177 desired image size. */
2178static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002179limit_scaled_size_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002180 __s32 * width,
2181 __s32 * height,
2182 enum v4l2_field field,
2183 unsigned int width_mask,
2184 unsigned int width_bias,
2185 int adjust_size,
2186 int adjust_crop)
2187{
2188 struct bttv *btv = fh->btv;
2189 const struct v4l2_rect *b;
2190 struct bttv_crop *c;
2191 __s32 min_width;
2192 __s32 min_height;
2193 __s32 max_width;
2194 __s32 max_height;
2195 int rc;
2196
2197 BUG_ON((int) width_mask >= 0 ||
2198 width_bias >= (unsigned int) -width_mask);
2199
2200 /* Make sure tvnorm, vbi_end and the current cropping parameters
2201 remain consistent until we're done. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03002202
2203 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2204
2205 /* Do crop - use current, don't - use default parameters. */
2206 c = &btv->crop[!!fh->do_crop];
2207
2208 if (fh->do_crop
2209 && adjust_size
2210 && adjust_crop
2211 && !locked_btres(btv, VIDEO_RESOURCES)) {
2212 min_width = 48;
2213 min_height = 32;
2214
2215 /* We cannot scale up. When the scaled image is larger
2216 than crop.rect we adjust the crop.rect as required
2217 by the V4L2 spec, hence cropcap.bounds are our limit. */
2218 max_width = min(b->width, (__s32) MAX_HACTIVE);
2219 max_height = b->height;
2220
2221 /* We cannot capture the same line as video and VBI data.
2222 Note btv->vbi_end is really a minimum, see
2223 bttv_vbi_try_fmt(). */
2224 if (btv->vbi_end > b->top) {
2225 max_height -= btv->vbi_end - b->top;
2226 rc = -EBUSY;
2227 if (min_height > max_height)
2228 goto fail;
2229 }
2230 } else {
2231 rc = -EBUSY;
2232 if (btv->vbi_end > c->rect.top)
2233 goto fail;
2234
2235 min_width = c->min_scaled_width;
2236 min_height = c->min_scaled_height;
2237 max_width = c->max_scaled_width;
2238 max_height = c->max_scaled_height;
2239
2240 adjust_crop = 0;
2241 }
2242
2243 min_width = (min_width - width_mask - 1) & width_mask;
2244 max_width = max_width & width_mask;
2245
2246 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2247 min_height = min_height;
2248 /* Min. scale factor is 1:1. */
2249 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2250
2251 if (adjust_size) {
2252 *width = clamp(*width, min_width, max_width);
2253 *height = clamp(*height, min_height, max_height);
2254
2255 /* Round after clamping to avoid overflow. */
2256 *width = (*width + width_bias) & width_mask;
2257
2258 if (adjust_crop) {
2259 bttv_crop_adjust(c, b, *width, *height, field);
2260
2261 if (btv->vbi_end > c->rect.top) {
2262 /* Move the crop window out of the way. */
2263 c->rect.top = btv->vbi_end;
2264 }
2265 }
2266 } else {
2267 rc = -EINVAL;
2268 if (*width < min_width ||
2269 *height < min_height ||
2270 *width > max_width ||
2271 *height > max_height ||
2272 0 != (*width & ~width_mask))
2273 goto fail;
2274 }
2275
2276 rc = 0; /* success */
2277
2278 fail:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002279
2280 return rc;
2281}
2282
2283/* Returns an error if the given overlay window dimensions are not
2284 possible with the current cropping parameters. If adjust_size is
2285 TRUE the function may adjust the window width and/or height
2286 instead, however it always rounds the horizontal position and
2287 width as btcx_align() does. If adjust_crop is TRUE the function
2288 may also adjust the current cropping parameters to get closer
2289 to the desired window size. */
2290static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002291verify_window_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002292 struct v4l2_window * win,
2293 int adjust_size,
2294 int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002297 unsigned int width_mask;
2298 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 if (win->w.width < 48 || win->w.height < 32)
2301 return -EINVAL;
2302 if (win->clipcount > 2048)
2303 return -EINVAL;
2304
2305 field = win->field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 if (V4L2_FIELD_ANY == field) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002308 __s32 height2;
2309
2310 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2311 field = (win->w.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 ? V4L2_FIELD_INTERLACED
2313 : V4L2_FIELD_TOP;
2314 }
2315 switch (field) {
2316 case V4L2_FIELD_TOP:
2317 case V4L2_FIELD_BOTTOM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 case V4L2_FIELD_INTERLACED:
2319 break;
2320 default:
2321 return -EINVAL;
2322 }
2323
Michael Schimeke5bd0262007-01-18 16:17:39 -03002324 /* 4-byte alignment. */
2325 if (NULL == fh->ovfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002327 width_mask = ~0;
2328 switch (fh->ovfmt->depth) {
2329 case 8:
2330 case 24:
2331 width_mask = ~3;
2332 break;
2333 case 16:
2334 width_mask = ~1;
2335 break;
2336 case 32:
2337 break;
2338 default:
2339 BUG();
2340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Michael Schimeke5bd0262007-01-18 16:17:39 -03002342 win->w.width -= win->w.left & ~width_mask;
2343 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2344
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002345 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002346 field, width_mask,
2347 /* width_bias: round down */ 0,
2348 adjust_size, adjust_crop);
2349 if (0 != rc)
2350 return rc;
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 win->field = field;
2353 return 0;
2354}
2355
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002356static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 struct v4l2_window *win, int fixup)
2358{
2359 struct v4l2_clip *clips = NULL;
2360 int n,size,retval = 0;
2361
2362 if (NULL == fh->ovfmt)
2363 return -EINVAL;
2364 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2365 return -EINVAL;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002366 retval = verify_window_lock(fh, win,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002367 /* adjust_size */ fixup,
2368 /* adjust_crop */ fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (0 != retval)
2370 return retval;
2371
2372 /* copy clips -- luckily v4l1 + v4l2 are binary
2373 compatible here ...*/
2374 n = win->clipcount;
2375 size = sizeof(*clips)*(n+4);
2376 clips = kmalloc(size,GFP_KERNEL);
2377 if (NULL == clips)
2378 return -ENOMEM;
2379 if (n > 0) {
2380 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2381 kfree(clips);
2382 return -EFAULT;
2383 }
2384 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 /* clip against screen */
2387 if (NULL != btv->fbuf.base)
2388 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2389 &win->w, clips, n);
2390 btcx_sort_clips(clips,n);
2391
2392 /* 4-byte alignments */
2393 switch (fh->ovfmt->depth) {
2394 case 8:
2395 case 24:
2396 btcx_align(&win->w, clips, n, 3);
2397 break;
2398 case 16:
2399 btcx_align(&win->w, clips, n, 1);
2400 break;
2401 case 32:
2402 /* no alignment fixups needed */
2403 break;
2404 default:
2405 BUG();
2406 }
2407
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002408 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 fh->ov.clips = clips;
2410 fh->ov.nclips = n;
2411
2412 fh->ov.w = win->w;
2413 fh->ov.field = win->field;
2414 fh->ov.setup_ok = 1;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 btv->init.ov.w.width = win->w.width;
2417 btv->init.ov.w.height = win->w.height;
2418 btv->init.ov.field = win->field;
2419
2420 /* update overlay if needed */
2421 retval = 0;
2422 if (check_btres(fh, RESOURCE_OVERLAY)) {
2423 struct bttv_buffer *new;
2424
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002425 new = videobuf_sg_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03002426 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2428 retval = bttv_switch_overlay(btv,fh,new);
2429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 return retval;
2431}
2432
2433/* ----------------------------------------------------------------------- */
2434
2435static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2436{
2437 struct videobuf_queue* q = NULL;
2438
2439 switch (fh->type) {
2440 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2441 q = &fh->cap;
2442 break;
2443 case V4L2_BUF_TYPE_VBI_CAPTURE:
2444 q = &fh->vbi;
2445 break;
2446 default:
2447 BUG();
2448 }
2449 return q;
2450}
2451
2452static int bttv_resource(struct bttv_fh *fh)
2453{
2454 int res = 0;
2455
2456 switch (fh->type) {
2457 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002458 res = RESOURCE_VIDEO_STREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 break;
2460 case V4L2_BUF_TYPE_VBI_CAPTURE:
2461 res = RESOURCE_VBI;
2462 break;
2463 default:
2464 BUG();
2465 }
2466 return res;
2467}
2468
2469static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2470{
2471 struct videobuf_queue *q = bttv_queue(fh);
2472 int res = bttv_resource(fh);
2473
2474 if (check_btres(fh,res))
2475 return -EBUSY;
2476 if (videobuf_queue_is_busy(q))
2477 return -EBUSY;
2478 fh->type = type;
2479 return 0;
2480}
2481
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002482static void
2483pix_format_set_size (struct v4l2_pix_format * f,
2484 const struct bttv_format * fmt,
2485 unsigned int width,
2486 unsigned int height)
2487{
2488 f->width = width;
2489 f->height = height;
2490
2491 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2492 f->bytesperline = width; /* Y plane */
2493 f->sizeimage = (width * height * fmt->depth) >> 3;
2494 } else {
2495 f->bytesperline = (width * fmt->depth) >> 3;
2496 f->sizeimage = height * f->bytesperline;
2497 }
2498}
2499
Hans Verkuil78b526a2008-05-28 12:16:41 -03002500static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002501 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002503 struct bttv_fh *fh = priv;
2504
2505 pix_format_set_size(&f->fmt.pix, fh->fmt,
2506 fh->width, fh->height);
2507 f->fmt.pix.field = fh->cap.field;
2508 f->fmt.pix.pixelformat = fh->fmt->fourcc;
Hans Verkuilf5864892013-02-06 11:58:59 -03002509 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002510
2511 return 0;
2512}
2513
Hans Verkuil78b526a2008-05-28 12:16:41 -03002514static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002515 struct v4l2_format *f)
2516{
2517 struct bttv_fh *fh = priv;
2518
2519 f->fmt.win.w = fh->ov.w;
2520 f->fmt.win.field = fh->ov.field;
2521
2522 return 0;
2523}
2524
Hans Verkuil78b526a2008-05-28 12:16:41 -03002525static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002526 struct v4l2_format *f)
2527{
2528 const struct bttv_format *fmt;
2529 struct bttv_fh *fh = priv;
2530 struct bttv *btv = fh->btv;
2531 enum v4l2_field field;
2532 __s32 width, height;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002533 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002534
2535 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2536 if (NULL == fmt)
2537 return -EINVAL;
2538
2539 field = f->fmt.pix.field;
2540
2541 if (V4L2_FIELD_ANY == field) {
2542 __s32 height2;
2543
2544 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2545 field = (f->fmt.pix.height > height2)
2546 ? V4L2_FIELD_INTERLACED
2547 : V4L2_FIELD_BOTTOM;
2548 }
2549
2550 if (V4L2_FIELD_SEQ_BT == field)
2551 field = V4L2_FIELD_SEQ_TB;
2552
2553 switch (field) {
2554 case V4L2_FIELD_TOP:
2555 case V4L2_FIELD_BOTTOM:
2556 case V4L2_FIELD_ALTERNATE:
2557 case V4L2_FIELD_INTERLACED:
2558 break;
2559 case V4L2_FIELD_SEQ_TB:
2560 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2561 return -EINVAL;
2562 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 default:
2564 return -EINVAL;
2565 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002566
2567 width = f->fmt.pix.width;
2568 height = f->fmt.pix.height;
2569
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002570 rc = limit_scaled_size_lock(fh, &width, &height, field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002571 /* width_mask: 4 pixels */ ~3,
2572 /* width_bias: nearest */ 2,
2573 /* adjust_size */ 1,
2574 /* adjust_crop */ 0);
2575 if (0 != rc)
2576 return rc;
2577
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002578 /* update data for the application */
2579 f->fmt.pix.field = field;
2580 pix_format_set_size(&f->fmt.pix, fmt, width, height);
Hans Verkuilf5864892013-02-06 11:58:59 -03002581 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002582
2583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Hans Verkuil78b526a2008-05-28 12:16:41 -03002586static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002587 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002589 struct bttv_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002591 return verify_window_lock(fh, &f->fmt.win,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002592 /* adjust_size */ 1,
2593 /* adjust_crop */ 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
Hans Verkuil78b526a2008-05-28 12:16:41 -03002596static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002597 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
2599 int retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002600 const struct bttv_format *fmt;
2601 struct bttv_fh *fh = priv;
2602 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002603 __s32 width, height;
2604 enum v4l2_field field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002606 retval = bttv_switch_type(fh, f->type);
2607 if (0 != retval)
2608 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Hans Verkuil78b526a2008-05-28 12:16:41 -03002610 retval = bttv_try_fmt_vid_cap(file, priv, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002611 if (0 != retval)
2612 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002614 width = f->fmt.pix.width;
2615 height = f->fmt.pix.height;
2616 field = f->fmt.pix.field;
2617
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002618 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002619 /* width_mask: 4 pixels */ ~3,
2620 /* width_bias: nearest */ 2,
2621 /* adjust_size */ 1,
2622 /* adjust_crop */ 1);
2623 if (0 != retval)
2624 return retval;
2625
2626 f->fmt.pix.field = field;
2627
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002628 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002630 /* update our state informations */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002631 fh->fmt = fmt;
2632 fh->cap.field = f->fmt.pix.field;
2633 fh->cap.last = V4L2_FIELD_NONE;
2634 fh->width = f->fmt.pix.width;
2635 fh->height = f->fmt.pix.height;
2636 btv->init.fmt = fmt;
2637 btv->init.width = f->fmt.pix.width;
2638 btv->init.height = f->fmt.pix.height;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002639
2640 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
Hans Verkuil78b526a2008-05-28 12:16:41 -03002643static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002644 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002646 struct bttv_fh *fh = priv;
2647 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002649 if (no_overlay > 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03002650 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002651 return -EINVAL;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002652 }
Michael Krufky5e453dc2006-01-09 15:32:31 -02002653
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002654 return setup_window_lock(fh, btv, &f->fmt.win, 1);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002655}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002657static int bttv_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002658 struct v4l2_capability *cap)
2659{
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002660 struct video_device *vdev = video_devdata(file);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002661 struct bttv_fh *fh = priv;
2662 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002664 if (0 == v4l2)
2665 return -EINVAL;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002666
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002667 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2668 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2669 snprintf(cap->bus_info, sizeof(cap->bus_info),
2670 "PCI:%s", pci_name(btv->c.pci));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002671 cap->capabilities =
2672 V4L2_CAP_VIDEO_CAPTURE |
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002673 V4L2_CAP_READWRITE |
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002674 V4L2_CAP_STREAMING |
2675 V4L2_CAP_DEVICE_CAPS;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002676 if (no_overlay <= 0)
2677 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002678 if (btv->vbi_dev)
2679 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
2680 if (btv->radio_dev)
2681 cap->capabilities |= V4L2_CAP_RADIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002683 /*
2684 * No need to lock here: those vars are initialized during board
2685 * probe and remains untouched during the rest of the driver lifecycle
2686 */
2687 if (btv->has_saa6588)
2688 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
Trent Piephoabb03622009-01-28 21:32:59 -03002689 if (btv->tuner_type != TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002690 cap->capabilities |= V4L2_CAP_TUNER;
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002691 if (vdev->vfl_type == VFL_TYPE_GRABBER)
2692 cap->device_caps = cap->capabilities &
2693 (V4L2_CAP_VIDEO_CAPTURE |
2694 V4L2_CAP_READWRITE |
2695 V4L2_CAP_STREAMING |
2696 V4L2_CAP_VIDEO_OVERLAY |
2697 V4L2_CAP_TUNER);
2698 else if (vdev->vfl_type == VFL_TYPE_VBI)
2699 cap->device_caps = cap->capabilities &
2700 (V4L2_CAP_VBI_CAPTURE |
2701 V4L2_CAP_READWRITE |
2702 V4L2_CAP_STREAMING |
2703 V4L2_CAP_TUNER);
2704 else {
2705 cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
2706 if (btv->has_saa6588)
2707 cap->device_caps |= V4L2_CAP_READWRITE |
2708 V4L2_CAP_RDS_CAPTURE;
2709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002711}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002713static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2714{
2715 int index = -1, i;
2716
2717 for (i = 0; i < FORMATS; i++) {
2718 if (formats[i].fourcc != -1)
2719 index++;
2720 if ((unsigned int)index == f->index)
2721 break;
2722 }
2723 if (FORMATS == i)
2724 return -EINVAL;
2725
2726 f->pixelformat = formats[i].fourcc;
2727 strlcpy(f->description, formats[i].name, sizeof(f->description));
2728
2729 return i;
2730}
2731
Hans Verkuil78b526a2008-05-28 12:16:41 -03002732static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002733 struct v4l2_fmtdesc *f)
2734{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002735 int rc = bttv_enum_fmt_cap_ovr(f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002736
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002737 if (rc < 0)
2738 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002739
2740 return 0;
2741}
2742
Hans Verkuil78b526a2008-05-28 12:16:41 -03002743static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002744 struct v4l2_fmtdesc *f)
2745{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002746 int rc;
2747
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002748 if (no_overlay > 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03002749 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002750 return -EINVAL;
2751 }
2752
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002753 rc = bttv_enum_fmt_cap_ovr(f);
2754
2755 if (rc < 0)
2756 return rc;
2757
2758 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002759 return -EINVAL;
2760
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002761 return 0;
2762}
2763
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002764static int bttv_g_fbuf(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002765 struct v4l2_framebuffer *fb)
2766{
2767 struct bttv_fh *fh = f;
2768 struct bttv *btv = fh->btv;
2769
2770 *fb = btv->fbuf;
2771 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
Hans Verkuila12fd702013-02-06 12:00:03 -03002772 fb->flags = V4L2_FBUF_FLAG_PRIMARY;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002773 if (fh->ovfmt)
2774 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2775 return 0;
2776}
2777
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002778static int bttv_overlay(struct file *file, void *f, unsigned int on)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002779{
2780 struct bttv_fh *fh = f;
2781 struct bttv *btv = fh->btv;
2782 struct bttv_buffer *new;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002783 int retval = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002784
2785 if (on) {
2786 /* verify args */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002787 if (unlikely(!btv->fbuf.base)) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002788 return -EINVAL;
2789 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002790 if (unlikely(!fh->ov.setup_ok)) {
Joe Perches8af443e2011-08-21 19:56:48 -03002791 dprintk("%d: overlay: !setup_ok\n", btv->c.nr);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002792 retval = -EINVAL;
2793 }
2794 if (retval)
2795 return retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002796 }
2797
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002798 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002799 return -EBUSY;
2800
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002801 if (on) {
2802 fh->ov.tvnorm = btv->tvnorm;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002803 new = videobuf_sg_alloc(sizeof(*new));
Robert Fitzsimons7c018802008-02-13 16:38:11 -03002804 new->crop = btv->crop[!!fh->do_crop].rect;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002805 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2806 } else {
2807 new = NULL;
2808 }
2809
2810 /* switch over */
2811 retval = bttv_switch_overlay(btv, fh, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 return retval;
2813}
2814
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002815static int bttv_s_fbuf(struct file *file, void *f,
Hans Verkuile6eb28c2012-09-04 10:26:45 -03002816 const struct v4l2_framebuffer *fb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002818 struct bttv_fh *fh = f;
2819 struct bttv *btv = fh->btv;
2820 const struct bttv_format *fmt;
2821 int retval;
2822
2823 if (!capable(CAP_SYS_ADMIN) &&
2824 !capable(CAP_SYS_RAWIO))
2825 return -EPERM;
2826
2827 /* check args */
2828 fmt = format_by_fourcc(fb->fmt.pixelformat);
2829 if (NULL == fmt)
2830 return -EINVAL;
2831 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2832 return -EINVAL;
2833
2834 retval = -EINVAL;
2835 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2836 __s32 width = fb->fmt.width;
2837 __s32 height = fb->fmt.height;
2838
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002839 retval = limit_scaled_size_lock(fh, &width, &height,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002840 V4L2_FIELD_INTERLACED,
2841 /* width_mask */ ~3,
2842 /* width_bias */ 2,
2843 /* adjust_size */ 0,
2844 /* adjust_crop */ 0);
2845 if (0 != retval)
2846 return retval;
2847 }
2848
2849 /* ok, accept it */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002850 btv->fbuf.base = fb->base;
2851 btv->fbuf.fmt.width = fb->fmt.width;
2852 btv->fbuf.fmt.height = fb->fmt.height;
2853 if (0 != fb->fmt.bytesperline)
2854 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2855 else
2856 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2857
2858 retval = 0;
2859 fh->ovfmt = fmt;
2860 btv->init.ovfmt = fmt;
2861 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2862 fh->ov.w.left = 0;
2863 fh->ov.w.top = 0;
2864 fh->ov.w.width = fb->fmt.width;
2865 fh->ov.w.height = fb->fmt.height;
2866 btv->init.ov.w.width = fb->fmt.width;
2867 btv->init.ov.w.height = fb->fmt.height;
2868 kfree(fh->ov.clips);
2869 fh->ov.clips = NULL;
2870 fh->ov.nclips = 0;
2871
2872 if (check_btres(fh, RESOURCE_OVERLAY)) {
2873 struct bttv_buffer *new;
2874
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002875 new = videobuf_sg_alloc(sizeof(*new));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002876 new->crop = btv->crop[!!fh->do_crop].rect;
2877 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2878 retval = bttv_switch_overlay(btv, fh, new);
2879 }
2880 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002881 return retval;
2882}
2883
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002884static int bttv_reqbufs(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002885 struct v4l2_requestbuffers *p)
2886{
2887 struct bttv_fh *fh = priv;
2888 return videobuf_reqbufs(bttv_queue(fh), p);
2889}
2890
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002891static int bttv_querybuf(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002892 struct v4l2_buffer *b)
2893{
2894 struct bttv_fh *fh = priv;
2895 return videobuf_querybuf(bttv_queue(fh), b);
2896}
2897
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002898static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002899{
2900 struct bttv_fh *fh = priv;
2901 struct bttv *btv = fh->btv;
2902 int res = bttv_resource(fh);
2903
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002904 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002905 return -EBUSY;
2906
2907 return videobuf_qbuf(bttv_queue(fh), b);
2908}
2909
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002910static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002911{
2912 struct bttv_fh *fh = priv;
2913 return videobuf_dqbuf(bttv_queue(fh), b,
2914 file->f_flags & O_NONBLOCK);
2915}
2916
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002917static int bttv_streamon(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002918 enum v4l2_buf_type type)
2919{
2920 struct bttv_fh *fh = priv;
2921 struct bttv *btv = fh->btv;
2922 int res = bttv_resource(fh);
2923
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002924 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002925 return -EBUSY;
2926 return videobuf_streamon(bttv_queue(fh));
2927}
2928
2929
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002930static int bttv_streamoff(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002931 enum v4l2_buf_type type)
2932{
2933 struct bttv_fh *fh = priv;
2934 struct bttv *btv = fh->btv;
2935 int retval;
2936 int res = bttv_resource(fh);
2937
2938
2939 retval = videobuf_streamoff(bttv_queue(fh));
2940 if (retval < 0)
2941 return retval;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002942 free_btres_lock(btv, fh, res);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002943 return 0;
2944}
2945
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002946static int bttv_queryctrl(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002947 struct v4l2_queryctrl *c)
2948{
2949 struct bttv_fh *fh = priv;
2950 struct bttv *btv = fh->btv;
2951 const struct v4l2_queryctrl *ctrl;
2952
2953 if ((c->id < V4L2_CID_BASE ||
2954 c->id >= V4L2_CID_LASTP1) &&
2955 (c->id < V4L2_CID_PRIVATE_BASE ||
2956 c->id >= V4L2_CID_PRIVATE_LASTP1))
2957 return -EINVAL;
2958
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002959 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2960 *c = no_ctl;
2961 else {
2962 ctrl = ctrl_by_id(c->id);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002963
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002964 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2965 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002966
2967 return 0;
2968}
2969
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002970static int bttv_g_parm(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002971 struct v4l2_streamparm *parm)
2972{
2973 struct bttv_fh *fh = f;
2974 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002975
Hans Verkuila652ef62012-09-09 10:27:57 -03002976 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2977 return -EINVAL;
2978 parm->parm.capture.readbuffers = gbuffers;
Trent Piepho51f0b8d2009-03-04 01:21:02 -03002979 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2980 &parm->parm.capture.timeperframe);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002981
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002982 return 0;
2983}
2984
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002985static int bttv_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002986 struct v4l2_tuner *t)
2987{
2988 struct bttv_fh *fh = priv;
2989 struct bttv *btv = fh->btv;
2990
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002991 if (0 != t->index)
2992 return -EINVAL;
2993
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002994 t->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil859f0272009-03-28 08:29:00 -03002995 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002996 strcpy(t->name, "Television");
2997 t->capability = V4L2_TUNER_CAP_NORM;
2998 t->type = V4L2_TUNER_ANALOG_TV;
2999 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
3000 t->signal = 0xffff;
3001
3002 if (btv->audio_mode_gpio)
3003 btv->audio_mode_gpio(btv, t, 0);
3004
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003005 return 0;
3006}
3007
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003008static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003009{
3010 struct bttv_fh *fh = f;
3011 struct bttv *btv = fh->btv;
3012
3013 *p = v4l2_prio_max(&btv->prio);
3014
3015 return 0;
3016}
3017
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003018static int bttv_s_priority(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003019 enum v4l2_priority prio)
3020{
3021 struct bttv_fh *fh = f;
3022 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003023 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003024
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003025 rc = v4l2_prio_change(&btv->prio, &fh->prio, prio);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003026
3027 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003028}
3029
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003030static int bttv_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003031 struct v4l2_cropcap *cap)
3032{
3033 struct bttv_fh *fh = priv;
3034 struct bttv *btv = fh->btv;
3035
3036 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3037 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3038 return -EINVAL;
3039
3040 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3041
3042 return 0;
3043}
3044
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003045static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003046{
3047 struct bttv_fh *fh = f;
3048 struct bttv *btv = fh->btv;
3049
3050 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3051 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3052 return -EINVAL;
3053
3054 /* No fh->do_crop = 1; because btv->crop[1] may be
3055 inconsistent with fh->width or fh->height and apps
3056 do not expect a change here. */
3057
3058 crop->c = btv->crop[!!fh->do_crop].rect;
3059
3060 return 0;
3061}
3062
Hans Verkuil4f9965942012-09-05 05:10:48 -03003063static int bttv_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003064{
3065 struct bttv_fh *fh = f;
3066 struct bttv *btv = fh->btv;
3067 const struct v4l2_rect *b;
3068 int retval;
3069 struct bttv_crop c;
3070 __s32 b_left;
3071 __s32 b_top;
3072 __s32 b_right;
3073 __s32 b_bottom;
3074
3075 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3076 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3077 return -EINVAL;
3078
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003079 /* Make sure tvnorm, vbi_end and the current cropping
3080 parameters remain consistent until we're done. Note
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003081 read() may change vbi_end in check_alloc_btres_lock(). */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003082 retval = v4l2_prio_check(&btv->prio, fh->prio);
3083 if (0 != retval) {
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003084 return retval;
3085 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003086
3087 retval = -EBUSY;
3088
3089 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003090 return retval;
3091 }
3092
3093 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3094
3095 b_left = b->left;
3096 b_right = b_left + b->width;
3097 b_bottom = b->top + b->height;
3098
3099 b_top = max(b->top, btv->vbi_end);
3100 if (b_top + 32 >= b_bottom) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003101 return retval;
3102 }
3103
3104 /* Min. scaled size 48 x 32. */
Hans Verkuil4f9965942012-09-05 05:10:48 -03003105 c.rect.left = clamp_t(s32, crop->c.left, b_left, b_right - 48);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003106 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3107
Hans Verkuil4f9965942012-09-05 05:10:48 -03003108 c.rect.width = clamp_t(s32, crop->c.width,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003109 48, b_right - c.rect.left);
3110
Hans Verkuil4f9965942012-09-05 05:10:48 -03003111 c.rect.top = clamp_t(s32, crop->c.top, b_top, b_bottom - 32);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003112 /* Top and height must be a multiple of two. */
3113 c.rect.top = (c.rect.top + 1) & ~1;
3114
Hans Verkuil4f9965942012-09-05 05:10:48 -03003115 c.rect.height = clamp_t(s32, crop->c.height,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003116 32, b_bottom - c.rect.top);
3117 c.rect.height = (c.rect.height + 1) & ~1;
3118
3119 bttv_crop_calc_limits(&c);
3120
3121 btv->crop[1] = c;
3122
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003123 fh->do_crop = 1;
3124
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003125 if (fh->width < c.min_scaled_width) {
3126 fh->width = c.min_scaled_width;
3127 btv->init.width = c.min_scaled_width;
3128 } else if (fh->width > c.max_scaled_width) {
3129 fh->width = c.max_scaled_width;
3130 btv->init.width = c.max_scaled_width;
3131 }
3132
3133 if (fh->height < c.min_scaled_height) {
3134 fh->height = c.min_scaled_height;
3135 btv->init.height = c.min_scaled_height;
3136 } else if (fh->height > c.max_scaled_height) {
3137 fh->height = c.max_scaled_height;
3138 btv->init.height = c.max_scaled_height;
3139 }
3140
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003141 return 0;
3142}
3143
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003144static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003145{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003146 if (unlikely(a->index))
3147 return -EINVAL;
3148
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003149 strcpy(a->name, "audio");
3150 return 0;
3151}
3152
Hans Verkuil0e8025b2012-09-04 11:59:31 -03003153static int bttv_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003154{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003155 if (unlikely(a->index))
3156 return -EINVAL;
3157
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
3161static ssize_t bttv_read(struct file *file, char __user *data,
3162 size_t count, loff_t *ppos)
3163{
3164 struct bttv_fh *fh = file->private_data;
3165 int retval = 0;
3166
3167 if (fh->btv->errors)
3168 bttv_reinit_bt848(fh->btv);
Joe Perches8af443e2011-08-21 19:56:48 -03003169 dprintk("%d: read count=%d type=%s\n",
3170 fh->btv->c.nr, (int)count, v4l2_type_names[fh->type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
3172 switch (fh->type) {
3173 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003174 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03003175 /* VIDEO_READ in use by another fh,
3176 or VIDEO_STREAM by any fh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return -EBUSY;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3180 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003181 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 break;
3183 case V4L2_BUF_TYPE_VBI_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003184 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return -EBUSY;
3186 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3187 file->f_flags & O_NONBLOCK);
3188 break;
3189 default:
3190 BUG();
3191 }
3192 return retval;
3193}
3194
3195static unsigned int bttv_poll(struct file *file, poll_table *wait)
3196{
3197 struct bttv_fh *fh = file->private_data;
3198 struct bttv_buffer *buf;
3199 enum v4l2_field field;
Figo.zhang9fd64182009-06-16 13:31:29 -03003200 unsigned int rc = POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
3202 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003203 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 return POLLERR;
3205 return videobuf_poll_stream(file, &fh->vbi, wait);
3206 }
3207
Michael Schimeke5bd0262007-01-18 16:17:39 -03003208 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 /* streaming capture */
3210 if (list_empty(&fh->cap.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -03003211 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3213 } else {
3214 /* read() capture */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 if (NULL == fh->cap.read_buf) {
3216 /* need to capture a new frame */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003217 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3218 goto err;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003219 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003220 if (NULL == fh->cap.read_buf)
3221 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3223 field = videobuf_next_field(&fh->cap);
3224 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -08003225 kfree (fh->cap.read_buf);
3226 fh->cap.read_buf = NULL;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003227 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 }
3229 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3230 fh->cap.read_off = 0;
3231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 buf = (struct bttv_buffer*)fh->cap.read_buf;
3233 }
3234
3235 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03003236 if (buf->vb.state == VIDEOBUF_DONE ||
3237 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -03003238 rc = POLLIN|POLLRDNORM;
3239 else
3240 rc = 0;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003241err:
Figo.zhang9fd64182009-06-16 13:31:29 -03003242 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243}
3244
Hans Verkuilbec43662008-12-30 06:58:20 -03003245static int bttv_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003247 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003248 struct bttv *btv = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 struct bttv_fh *fh;
3250 enum v4l2_buf_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Joe Perches8af443e2011-08-21 19:56:48 -03003252 dprintk("open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Laurent Pinchart327ae592009-11-27 13:57:55 -03003254 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003255 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Laurent Pinchart327ae592009-11-27 13:57:55 -03003256 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003257 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3258 } else {
3259 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -03003261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Joe Perches8af443e2011-08-21 19:56:48 -03003263 dprintk("%d: open called (type=%s)\n",
3264 btv->c.nr, v4l2_type_names[type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 /* allocate per filehandle data */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003267 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3268 if (unlikely(!fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 return -ENOMEM;
3270 file->private_data = fh;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 *fh = btv->init;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 fh->type = type;
3275 fh->ov.setup_ok = 0;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003276
Hans Verkuilffb48772010-05-01 08:03:24 -03003277 v4l2_prio_open(&btv->prio, &fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003279 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3280 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3282 V4L2_FIELD_INTERLACED,
3283 sizeof(struct bttv_buffer),
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003284 fh, &btv->lock);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003285 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3286 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 V4L2_BUF_TYPE_VBI_CAPTURE,
3288 V4L2_FIELD_SEQ_TB,
3289 sizeof(struct bttv_buffer),
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003290 fh, &btv->lock);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03003291 set_tvnorm(btv,btv->tvnorm);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003292 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294 btv->users++;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003295
3296 /* The V4L2 spec requires one global set of cropping parameters
3297 which only change on request. These are stored in btv->crop[1].
3298 However for compatibility with V4L apps and cropping unaware
3299 V4L2 apps we now reset the cropping parameters as seen through
3300 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3301 will use btv->crop[0], the default cropping parameters for the
3302 current video standard, and VIDIOC_S_FMT will not implicitely
3303 change the cropping parameters until VIDIOC_S_CROP has been
3304 called. */
3305 fh->do_crop = !reset_crop; /* module parameter */
3306
3307 /* Likewise there should be one global set of VBI capture
3308 parameters, but for compatibility with V4L apps and earlier
3309 driver versions each fh has its own parameters. */
3310 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3311
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 bttv_field_count(btv);
3313 return 0;
3314}
3315
Hans Verkuilbec43662008-12-30 06:58:20 -03003316static int bttv_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
3318 struct bttv_fh *fh = file->private_data;
3319 struct bttv *btv = fh->btv;
3320
3321 /* turn off overlay */
3322 if (check_btres(fh, RESOURCE_OVERLAY))
3323 bttv_switch_overlay(btv,fh,NULL);
3324
3325 /* stop video capture */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003326 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 videobuf_streamoff(&fh->cap);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003328 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 }
3330 if (fh->cap.read_buf) {
3331 buffer_release(&fh->cap,fh->cap.read_buf);
3332 kfree(fh->cap.read_buf);
3333 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03003334 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003335 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
3338 /* stop vbi capture */
3339 if (check_btres(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03003340 videobuf_stop(&fh->vbi);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003341 free_btres_lock(btv,fh,RESOURCE_VBI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343
3344 /* free stuff */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 videobuf_mmap_free(&fh->cap);
3347 videobuf_mmap_free(&fh->vbi);
Hans Verkuilffb48772010-05-01 08:03:24 -03003348 v4l2_prio_close(&btv->prio, fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 file->private_data = NULL;
3350 kfree(fh);
3351
3352 btv->users--;
3353 bttv_field_count(btv);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003354
3355 if (!btv->users)
3356 audio_mute(btv, 1);
3357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 return 0;
3359}
3360
3361static int
3362bttv_mmap(struct file *file, struct vm_area_struct *vma)
3363{
3364 struct bttv_fh *fh = file->private_data;
3365
Joe Perches8af443e2011-08-21 19:56:48 -03003366 dprintk("%d: mmap type=%s 0x%lx+%ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 fh->btv->c.nr, v4l2_type_names[fh->type],
3368 vma->vm_start, vma->vm_end - vma->vm_start);
3369 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3370}
3371
Hans Verkuilbec43662008-12-30 06:58:20 -03003372static const struct v4l2_file_operations bttv_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
Mauro Carvalho Chehab8979e9d2010-09-15 08:22:09 -03003374 .owner = THIS_MODULE,
3375 .open = bttv_open,
3376 .release = bttv_release,
3377 .unlocked_ioctl = video_ioctl2,
3378 .read = bttv_read,
3379 .mmap = bttv_mmap,
3380 .poll = bttv_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381};
3382
Hans Verkuila3998102008-07-21 02:57:38 -03003383static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003384 .vidioc_querycap = bttv_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003385 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3386 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3387 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3388 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3389 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3390 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3391 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3392 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003393 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3394 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3395 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003396 .vidioc_g_audio = bttv_g_audio,
3397 .vidioc_s_audio = bttv_s_audio,
3398 .vidioc_cropcap = bttv_cropcap,
3399 .vidioc_reqbufs = bttv_reqbufs,
3400 .vidioc_querybuf = bttv_querybuf,
3401 .vidioc_qbuf = bttv_qbuf,
3402 .vidioc_dqbuf = bttv_dqbuf,
3403 .vidioc_s_std = bttv_s_std,
3404 .vidioc_enum_input = bttv_enum_input,
3405 .vidioc_g_input = bttv_g_input,
3406 .vidioc_s_input = bttv_s_input,
3407 .vidioc_queryctrl = bttv_queryctrl,
3408 .vidioc_g_ctrl = bttv_g_ctrl,
3409 .vidioc_s_ctrl = bttv_s_ctrl,
3410 .vidioc_streamon = bttv_streamon,
3411 .vidioc_streamoff = bttv_streamoff,
3412 .vidioc_g_tuner = bttv_g_tuner,
3413 .vidioc_s_tuner = bttv_s_tuner,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003414 .vidioc_g_crop = bttv_g_crop,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003415 .vidioc_s_crop = bttv_s_crop,
3416 .vidioc_g_fbuf = bttv_g_fbuf,
3417 .vidioc_s_fbuf = bttv_s_fbuf,
3418 .vidioc_overlay = bttv_overlay,
3419 .vidioc_g_priority = bttv_g_priority,
3420 .vidioc_s_priority = bttv_s_priority,
3421 .vidioc_g_parm = bttv_g_parm,
3422 .vidioc_g_frequency = bttv_g_frequency,
3423 .vidioc_s_frequency = bttv_s_frequency,
3424 .vidioc_log_status = bttv_log_status,
3425 .vidioc_querystd = bttv_querystd,
Hans Verkuil1b9e94d2012-09-09 09:23:31 -03003426 .vidioc_g_chip_ident = bttv_g_chip_ident,
Zoltan Devai43846832008-01-14 13:24:38 -03003427#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003428 .vidioc_g_register = bttv_g_register,
3429 .vidioc_s_register = bttv_s_register,
Zoltan Devai43846832008-01-14 13:24:38 -03003430#endif
Hans Verkuila3998102008-07-21 02:57:38 -03003431};
3432
3433static struct video_device bttv_video_template = {
3434 .fops = &bttv_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003435 .ioctl_ops = &bttv_ioctl_ops,
3436 .tvnorms = BTTV_NORMS,
3437 .current_norm = V4L2_STD_PAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438};
3439
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440/* ----------------------------------------------------------------------- */
3441/* radio interface */
3442
Hans Verkuilbec43662008-12-30 06:58:20 -03003443static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003445 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003446 struct bttv *btv = video_drvdata(file);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003447 struct bttv_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Joe Perches8af443e2011-08-21 19:56:48 -03003449 dprintk("open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Joe Perches8af443e2011-08-21 19:56:48 -03003451 dprintk("%d: open called (radio)\n", btv->c.nr);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003452
3453 /* allocate per filehandle data */
3454 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003455 if (unlikely(!fh))
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003456 return -ENOMEM;
3457 file->private_data = fh;
3458 *fh = btv->init;
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003459
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003460 v4l2_prio_open(&btv->prio, &fh->prio);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003461
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 btv->radio_user++;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003463
Hans Verkuil859f0272009-03-28 08:29:00 -03003464 bttv_call_all(btv, tuner, s_radio);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03003465 audio_input(btv,TVAUDIO_INPUT_RADIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468}
3469
Hans Verkuilbec43662008-12-30 06:58:20 -03003470static int radio_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003472 struct bttv_fh *fh = file->private_data;
3473 struct bttv *btv = fh->btv;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003474 struct saa6588_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Hans Verkuilffb48772010-05-01 08:03:24 -03003476 v4l2_prio_close(&btv->prio, fh->prio);
Robert Fitzsimonsb9bc07a2008-04-10 09:40:31 -03003477 file->private_data = NULL;
3478 kfree(fh);
3479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 btv->radio_user--;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003481
Hans Verkuilb9218f22010-12-27 12:22:46 -03003482 bttv_call_all(btv, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 return 0;
3485}
3486
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003487static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003489 struct bttv_fh *fh = priv;
3490 struct bttv *btv = fh->btv;
3491
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003492 if (0 != t->index)
3493 return -EINVAL;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003494 strcpy(t->name, "Radio");
3495 t->type = V4L2_TUNER_RADIO;
3496
Hans Verkuil859f0272009-03-28 08:29:00 -03003497 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003498
3499 if (btv->audio_mode_gpio)
3500 btv->audio_mode_gpio(btv, t, 0);
3501
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003502 return 0;
3503}
3504
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003505static int radio_s_tuner(struct file *file, void *priv,
3506 struct v4l2_tuner *t)
3507{
3508 struct bttv_fh *fh = priv;
3509 struct bttv *btv = fh->btv;
3510
3511 if (0 != t->index)
3512 return -EINVAL;
3513
Hans Verkuila024c1a2011-06-12 07:02:43 -03003514 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003515 return 0;
3516}
3517
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003518static int radio_queryctrl(struct file *file, void *priv,
3519 struct v4l2_queryctrl *c)
3520{
3521 const struct v4l2_queryctrl *ctrl;
3522
3523 if (c->id < V4L2_CID_BASE ||
3524 c->id >= V4L2_CID_LASTP1)
3525 return -EINVAL;
3526
3527 if (c->id == V4L2_CID_AUDIO_MUTE) {
3528 ctrl = ctrl_by_id(c->id);
3529 *c = *ctrl;
3530 } else
3531 *c = no_ctl;
3532
3533 return 0;
3534}
3535
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003536static ssize_t radio_read(struct file *file, char __user *data,
3537 size_t count, loff_t *ppos)
3538{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003539 struct bttv_fh *fh = file->private_data;
3540 struct bttv *btv = fh->btv;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003541 struct saa6588_command cmd;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003542 cmd.block_count = count/3;
3543 cmd.buffer = data;
3544 cmd.instance = file;
3545 cmd.result = -ENODEV;
3546
Hans Verkuilb9218f22010-12-27 12:22:46 -03003547 bttv_call_all(btv, core, ioctl, SAA6588_CMD_READ, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003548
3549 return cmd.result;
3550}
3551
3552static unsigned int radio_poll(struct file *file, poll_table *wait)
3553{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003554 struct bttv_fh *fh = file->private_data;
3555 struct bttv *btv = fh->btv;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003556 struct saa6588_command cmd;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003557 cmd.instance = file;
3558 cmd.event_list = wait;
3559 cmd.result = -ENODEV;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003560 bttv_call_all(btv, core, ioctl, SAA6588_CMD_POLL, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003561
3562 return cmd.result;
3563}
3564
Hans Verkuilbec43662008-12-30 06:58:20 -03003565static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566{
3567 .owner = THIS_MODULE,
3568 .open = radio_open,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003569 .read = radio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 .release = radio_release,
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003571 .unlocked_ioctl = video_ioctl2,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003572 .poll = radio_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573};
3574
Hans Verkuila3998102008-07-21 02:57:38 -03003575static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil78dea1a2012-09-09 09:03:29 -03003576 .vidioc_querycap = bttv_querycap,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003577 .vidioc_g_tuner = radio_g_tuner,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003578 .vidioc_s_tuner = radio_s_tuner,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003579 .vidioc_queryctrl = radio_queryctrl,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003580 .vidioc_g_ctrl = bttv_g_ctrl,
3581 .vidioc_s_ctrl = bttv_s_ctrl,
3582 .vidioc_g_frequency = bttv_g_frequency,
3583 .vidioc_s_frequency = bttv_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584};
3585
Hans Verkuila3998102008-07-21 02:57:38 -03003586static struct video_device radio_template = {
3587 .fops = &radio_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003588 .ioctl_ops = &radio_ioctl_ops,
3589};
3590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591/* ----------------------------------------------------------------------- */
3592/* some debug code */
3593
Adrian Bunk408b6642005-05-01 08:59:29 -07003594static int bttv_risc_decode(u32 risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595{
3596 static char *instr[16] = {
3597 [ BT848_RISC_WRITE >> 28 ] = "write",
3598 [ BT848_RISC_SKIP >> 28 ] = "skip",
3599 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3600 [ BT848_RISC_JUMP >> 28 ] = "jump",
3601 [ BT848_RISC_SYNC >> 28 ] = "sync",
3602 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3603 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3604 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3605 };
3606 static int incr[16] = {
3607 [ BT848_RISC_WRITE >> 28 ] = 2,
3608 [ BT848_RISC_JUMP >> 28 ] = 2,
3609 [ BT848_RISC_SYNC >> 28 ] = 2,
3610 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3611 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3612 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3613 };
3614 static char *bits[] = {
3615 "be0", "be1", "be2", "be3/resync",
3616 "set0", "set1", "set2", "set3",
3617 "clr0", "clr1", "clr2", "clr3",
3618 "irq", "res", "eol", "sol",
3619 };
3620 int i;
3621
Joe Perches8af443e2011-08-21 19:56:48 -03003622 pr_cont("0x%08x [ %s", risc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3624 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3625 if (risc & (1 << (i + 12)))
Joe Perches8af443e2011-08-21 19:56:48 -03003626 pr_cont(" %s", bits[i]);
3627 pr_cont(" count=%d ]\n", risc & 0xfff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3629}
3630
Adrian Bunk408b6642005-05-01 08:59:29 -07003631static void bttv_risc_disasm(struct bttv *btv,
3632 struct btcx_riscmem *risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
3634 unsigned int i,j,n;
3635
Joe Perches8af443e2011-08-21 19:56:48 -03003636 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
3637 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 for (i = 0; i < (risc->size >> 2); i += n) {
Joe Perches8af443e2011-08-21 19:56:48 -03003639 pr_info("%s: 0x%lx: ",
3640 btv->c.v4l2_dev.name,
3641 (unsigned long)(risc->dma + (i<<2)));
Al Viro3aa71102008-06-22 14:20:09 -03003642 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 for (j = 1; j < n; j++)
Joe Perches8af443e2011-08-21 19:56:48 -03003644 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3645 btv->c.v4l2_dev.name,
3646 (unsigned long)(risc->dma + ((i+j)<<2)),
3647 risc->cpu[i+j], j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 if (0 == risc->cpu[i])
3649 break;
3650 }
3651}
3652
3653static void bttv_print_riscaddr(struct bttv *btv)
3654{
Joe Perches8af443e2011-08-21 19:56:48 -03003655 pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
3656 pr_info(" vbi : o=%08llx e=%08llx\n",
3657 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3658 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3659 pr_info(" cap : o=%08llx e=%08llx\n",
3660 btv->curr.top
3661 ? (unsigned long long)btv->curr.top->top.dma : 0,
3662 btv->curr.bottom
3663 ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3664 pr_info(" scr : o=%08llx e=%08llx\n",
3665 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3666 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 bttv_risc_disasm(btv, &btv->main);
3668}
3669
3670/* ----------------------------------------------------------------------- */
3671/* irq handler */
3672
3673static char *irq_name[] = {
3674 "FMTCHG", // format change detected (525 vs. 625)
3675 "VSYNC", // vertical sync (new field)
3676 "HSYNC", // horizontal sync
3677 "OFLOW", // chroma/luma AGC overflow
3678 "HLOCK", // horizontal lock changed
3679 "VPRES", // video presence changed
3680 "6", "7",
3681 "I2CDONE", // hw irc operation finished
3682 "GPINT", // gpio port triggered irq
3683 "10",
3684 "RISCI", // risc instruction triggered irq
3685 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3686 "FTRGT", // pixel data fifo overrun
3687 "FDSR", // fifo data stream resyncronisation
3688 "PPERR", // parity error (data transfer)
3689 "RIPERR", // parity error (read risc instructions)
3690 "PABORT", // pci abort
3691 "OCERR", // risc instruction error
3692 "SCERR", // syncronisation error
3693};
3694
3695static void bttv_print_irqbits(u32 print, u32 mark)
3696{
3697 unsigned int i;
3698
Joe Perches8af443e2011-08-21 19:56:48 -03003699 pr_cont("bits:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3701 if (print & (1 << i))
Joe Perches8af443e2011-08-21 19:56:48 -03003702 pr_cont(" %s", irq_name[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 if (mark & (1 << i))
Joe Perches8af443e2011-08-21 19:56:48 -03003704 pr_cont("*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 }
3706}
3707
3708static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3709{
Joe Perches8af443e2011-08-21 19:56:48 -03003710 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3711 btv->c.nr,
3712 (unsigned long)btv->main.dma,
3713 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3714 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3715 (unsigned long)rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
Joe Perches8af443e2011-08-21 19:56:48 -03003718 pr_notice("%d: Oh, there (temporarily?) is no input signal. "
3719 "Ok, then this is harmless, don't worry ;)\n",
3720 btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 return;
3722 }
Joe Perches8af443e2011-08-21 19:56:48 -03003723 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
3724 btv->c.nr);
3725 pr_notice("%d: Lets try to catch the culpit red-handed ...\n",
3726 btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 dump_stack();
3728}
3729
3730static int
3731bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3732{
3733 struct bttv_buffer *item;
3734
3735 memset(set,0,sizeof(*set));
3736
3737 /* capture request ? */
3738 if (!list_empty(&btv->capture)) {
3739 set->frame_irq = 1;
3740 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3741 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3742 set->top = item;
3743 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3744 set->bottom = item;
3745
3746 /* capture request for other field ? */
3747 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3748 (item->vb.queue.next != &btv->capture)) {
3749 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
Mike Isely66349b42009-09-21 12:09:08 -03003750 /* Mike Isely <isely@pobox.com> - Only check
3751 * and set up the bottom field in the logic
3752 * below. Don't ever do the top field. This
3753 * of course means that if we set up the
3754 * bottom field in the above code that we'll
3755 * actually skip a field. But that's OK.
3756 * Having processed only a single buffer this
3757 * time, then the next time around the first
3758 * available buffer should be for a top field.
3759 * That will then cause us here to set up a
3760 * top then a bottom field in the normal way.
3761 * The alternative to this understanding is
3762 * that we set up the second available buffer
3763 * as a top field, but that's out of order
3764 * since this driver always processes the top
3765 * field first - the effect will be the two
3766 * buffers being returned in the wrong order,
3767 * with the second buffer also being delayed
3768 * by one field time (owing to the fifo nature
3769 * of videobuf). Worse still, we'll be stuck
3770 * doing fields out of order now every time
3771 * until something else causes a field to be
3772 * dropped. By effectively forcing a field to
3773 * drop this way then we always get back into
3774 * sync within a single frame time. (Out of
3775 * order fields can screw up deinterlacing
3776 * algorithms.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 if (NULL == set->bottom &&
3779 V4L2_FIELD_BOTTOM == item->vb.field) {
3780 set->bottom = item;
3781 }
3782 if (NULL != set->top && NULL != set->bottom)
3783 set->top_irq = 2;
3784 }
3785 }
3786 }
3787
3788 /* screen overlay ? */
3789 if (NULL != btv->screen) {
3790 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3791 if (NULL == set->top && NULL == set->bottom) {
3792 set->top = btv->screen;
3793 set->bottom = btv->screen;
3794 }
3795 } else {
3796 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3797 NULL == set->top) {
3798 set->top = btv->screen;
3799 }
3800 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3801 NULL == set->bottom) {
3802 set->bottom = btv->screen;
3803 }
3804 }
3805 }
3806
Joe Perches8af443e2011-08-21 19:56:48 -03003807 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3808 btv->c.nr, set->top, set->bottom,
3809 btv->screen, set->frame_irq, set->top_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 return 0;
3811}
3812
3813static void
3814bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3815 struct bttv_buffer_set *curr, unsigned int state)
3816{
3817 struct timeval ts;
3818
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003819 v4l2_get_timestamp(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
3821 if (wakeup->top == wakeup->bottom) {
3822 if (NULL != wakeup->top && curr->top != wakeup->top) {
3823 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003824 pr_debug("%d: wakeup: both=%p\n",
3825 btv->c.nr, wakeup->top);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 wakeup->top->vb.ts = ts;
3827 wakeup->top->vb.field_count = btv->field_count;
3828 wakeup->top->vb.state = state;
3829 wake_up(&wakeup->top->vb.done);
3830 }
3831 } else {
3832 if (NULL != wakeup->top && curr->top != wakeup->top) {
3833 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003834 pr_debug("%d: wakeup: top=%p\n",
3835 btv->c.nr, wakeup->top);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 wakeup->top->vb.ts = ts;
3837 wakeup->top->vb.field_count = btv->field_count;
3838 wakeup->top->vb.state = state;
3839 wake_up(&wakeup->top->vb.done);
3840 }
3841 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3842 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003843 pr_debug("%d: wakeup: bottom=%p\n",
3844 btv->c.nr, wakeup->bottom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 wakeup->bottom->vb.ts = ts;
3846 wakeup->bottom->vb.field_count = btv->field_count;
3847 wakeup->bottom->vb.state = state;
3848 wake_up(&wakeup->bottom->vb.done);
3849 }
3850 }
3851}
3852
3853static void
3854bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3855 unsigned int state)
3856{
3857 struct timeval ts;
3858
3859 if (NULL == wakeup)
3860 return;
3861
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003862 v4l2_get_timestamp(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 wakeup->vb.ts = ts;
3864 wakeup->vb.field_count = btv->field_count;
3865 wakeup->vb.state = state;
3866 wake_up(&wakeup->vb.done);
3867}
3868
3869static void bttv_irq_timeout(unsigned long data)
3870{
3871 struct bttv *btv = (struct bttv *)data;
3872 struct bttv_buffer_set old,new;
3873 struct bttv_buffer *ovbi;
3874 struct bttv_buffer *item;
3875 unsigned long flags;
3876
3877 if (bttv_verbose) {
Joe Perches8af443e2011-08-21 19:56:48 -03003878 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3879 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3880 btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 bttv_print_irqbits(btread(BT848_INT_STAT),0);
Joe Perches8af443e2011-08-21 19:56:48 -03003882 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 }
3884
3885 spin_lock_irqsave(&btv->s_lock,flags);
3886
3887 /* deactivate stuff */
3888 memset(&new,0,sizeof(new));
3889 old = btv->curr;
3890 ovbi = btv->cvbi;
3891 btv->curr = new;
3892 btv->cvbi = NULL;
3893 btv->loop_irq = 0;
3894 bttv_buffer_activate_video(btv, &new);
3895 bttv_buffer_activate_vbi(btv, NULL);
3896 bttv_set_dma(btv, 0);
3897
3898 /* wake up */
Brandon Philips0fc06862007-11-06 20:02:36 -03003899 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3900 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
3902 /* cancel all outstanding capture / vbi requests */
3903 while (!list_empty(&btv->capture)) {
3904 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3905 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003906 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 wake_up(&item->vb.done);
3908 }
3909 while (!list_empty(&btv->vcapture)) {
3910 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3911 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003912 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 wake_up(&item->vb.done);
3914 }
3915
3916 btv->errors++;
3917 spin_unlock_irqrestore(&btv->s_lock,flags);
3918}
3919
3920static void
3921bttv_irq_wakeup_top(struct bttv *btv)
3922{
3923 struct bttv_buffer *wakeup = btv->curr.top;
3924
3925 if (NULL == wakeup)
3926 return;
3927
3928 spin_lock(&btv->s_lock);
3929 btv->curr.top_irq = 0;
3930 btv->curr.top = NULL;
3931 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3932
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003933 v4l2_get_timestamp(&wakeup->vb.ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 wakeup->vb.field_count = btv->field_count;
Brandon Philips0fc06862007-11-06 20:02:36 -03003935 wakeup->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 wake_up(&wakeup->vb.done);
3937 spin_unlock(&btv->s_lock);
3938}
3939
3940static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3941{
3942 if (rc < risc->dma)
3943 return 0;
3944 if (rc > risc->dma + risc->size)
3945 return 0;
3946 return 1;
3947}
3948
3949static void
3950bttv_irq_switch_video(struct bttv *btv)
3951{
3952 struct bttv_buffer_set new;
3953 struct bttv_buffer_set old;
3954 dma_addr_t rc;
3955
3956 spin_lock(&btv->s_lock);
3957
3958 /* new buffer set */
3959 bttv_irq_next_video(btv, &new);
3960 rc = btread(BT848_RISC_COUNT);
3961 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3962 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3963 btv->framedrop++;
3964 if (debug_latency)
3965 bttv_irq_debug_low_latency(btv, rc);
3966 spin_unlock(&btv->s_lock);
3967 return;
3968 }
3969
3970 /* switch over */
3971 old = btv->curr;
3972 btv->curr = new;
3973 btv->loop_irq &= ~1;
3974 bttv_buffer_activate_video(btv, &new);
3975 bttv_set_dma(btv, 0);
3976
3977 /* switch input */
3978 if (UNSET != btv->new_input) {
3979 video_mux(btv,btv->new_input);
3980 btv->new_input = UNSET;
3981 }
3982
3983 /* wake up finished buffers */
Brandon Philips0fc06862007-11-06 20:02:36 -03003984 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 spin_unlock(&btv->s_lock);
3986}
3987
3988static void
3989bttv_irq_switch_vbi(struct bttv *btv)
3990{
3991 struct bttv_buffer *new = NULL;
3992 struct bttv_buffer *old;
3993 u32 rc;
3994
3995 spin_lock(&btv->s_lock);
3996
3997 if (!list_empty(&btv->vcapture))
3998 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3999 old = btv->cvbi;
4000
4001 rc = btread(BT848_RISC_COUNT);
4002 if (NULL != old && (is_active(&old->top, rc) ||
4003 is_active(&old->bottom, rc))) {
4004 btv->framedrop++;
4005 if (debug_latency)
4006 bttv_irq_debug_low_latency(btv, rc);
4007 spin_unlock(&btv->s_lock);
4008 return;
4009 }
4010
4011 /* switch */
4012 btv->cvbi = new;
4013 btv->loop_irq &= ~4;
4014 bttv_buffer_activate_vbi(btv, new);
4015 bttv_set_dma(btv, 0);
4016
Brandon Philips0fc06862007-11-06 20:02:36 -03004017 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 spin_unlock(&btv->s_lock);
4019}
4020
David Howells7d12e782006-10-05 14:55:46 +01004021static irqreturn_t bttv_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022{
4023 u32 stat,astat;
4024 u32 dstat;
4025 int count;
4026 struct bttv *btv;
4027 int handled = 0;
4028
4029 btv=(struct bttv *)dev_id;
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004030
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 count=0;
4032 while (1) {
4033 /* get/clear interrupt status bits */
4034 stat=btread(BT848_INT_STAT);
4035 astat=stat&btread(BT848_INT_MASK);
4036 if (!astat)
4037 break;
4038 handled = 1;
4039 btwrite(stat,BT848_INT_STAT);
4040
4041 /* get device status bits */
4042 dstat=btread(BT848_DSTATUS);
4043
4044 if (irq_debug) {
Joe Perches8af443e2011-08-21 19:56:48 -03004045 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
4046 btv->c.nr, count, btv->field_count,
4047 stat>>28, btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 bttv_print_irqbits(stat,astat);
4049 if (stat & BT848_INT_HLOCK)
Joe Perches8af443e2011-08-21 19:56:48 -03004050 pr_cont(" HLOC => %s",
4051 dstat & BT848_DSTATUS_HLOC
4052 ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 if (stat & BT848_INT_VPRES)
Joe Perches8af443e2011-08-21 19:56:48 -03004054 pr_cont(" PRES => %s",
4055 dstat & BT848_DSTATUS_PRES
4056 ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 if (stat & BT848_INT_FMTCHG)
Joe Perches8af443e2011-08-21 19:56:48 -03004058 pr_cont(" NUML => %s",
4059 dstat & BT848_DSTATUS_NUML
4060 ? "625" : "525");
4061 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 }
4063
4064 if (astat&BT848_INT_VSYNC)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004065 btv->field_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004067 if ((astat & BT848_INT_GPINT) && btv->remote) {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004068 bttv_input_irq(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 }
4070
4071 if (astat & BT848_INT_I2CDONE) {
4072 btv->i2c_done = stat;
4073 wake_up(&btv->i2c_queue);
4074 }
4075
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004076 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 bttv_irq_switch_vbi(btv);
4078
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004079 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 bttv_irq_wakeup_top(btv);
4081
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004082 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 bttv_irq_switch_video(btv);
4084
4085 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004086 audio_mute(btv, btv->mute); /* trigger automute */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
4088 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
Joe Perches8af443e2011-08-21 19:56:48 -03004089 pr_info("%d: %s%s @ %08x,",
4090 btv->c.nr,
4091 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4092 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4093 btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 bttv_print_irqbits(stat,astat);
Joe Perches8af443e2011-08-21 19:56:48 -03004095 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 if (bttv_debug)
4097 bttv_print_riscaddr(btv);
4098 }
4099 if (fdsr && astat & BT848_INT_FDSR) {
Joe Perches8af443e2011-08-21 19:56:48 -03004100 pr_info("%d: FDSR @ %08x\n",
4101 btv->c.nr, btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 if (bttv_debug)
4103 bttv_print_riscaddr(btv);
4104 }
4105
4106 count++;
4107 if (count > 4) {
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004108
4109 if (count > 8 || !(astat & BT848_INT_GPINT)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004110 btwrite(0, BT848_INT_MASK);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004111
Joe Perches8af443e2011-08-21 19:56:48 -03004112 pr_err("%d: IRQ lockup, cleared int mask [",
4113 btv->c.nr);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004114 } else {
Joe Perches8af443e2011-08-21 19:56:48 -03004115 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
4116 btv->c.nr);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004117
4118 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4119 BT848_INT_MASK);
4120 };
4121
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 bttv_print_irqbits(stat,astat);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004123
Joe Perches8af443e2011-08-21 19:56:48 -03004124 pr_cont("]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 }
4126 }
4127 btv->irq_total++;
4128 if (handled)
4129 btv->irq_me++;
4130 return IRQ_RETVAL(handled);
4131}
4132
4133
4134/* ----------------------------------------------------------------------- */
Hans Verkuild9b67072013-02-06 11:43:07 -03004135/* initialization */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
4137static struct video_device *vdev_init(struct bttv *btv,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004138 const struct video_device *template,
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004139 const char *type_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140{
4141 struct video_device *vfd;
4142
4143 vfd = video_device_alloc();
4144 if (NULL == vfd)
4145 return NULL;
4146 *vfd = *template;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004147 vfd->v4l2_dev = &btv->c.v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 vfd->release = video_device_release;
Mauro Carvalho Chehab1d0a4362008-06-23 12:31:29 -03004149 vfd->debug = bttv_debug;
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004150 video_set_drvdata(vfd, btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4152 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004153 type_name, bttv_tvcards[btv->c.type].name);
Hans Verkuild9b67072013-02-06 11:43:07 -03004154 if (btv->tuner_type == TUNER_ABSENT) {
4155 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
4156 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
4157 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
4158 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
4159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 return vfd;
4161}
4162
4163static void bttv_unregister_video(struct bttv *btv)
4164{
4165 if (btv->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004166 if (video_is_registered(btv->video_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 video_unregister_device(btv->video_dev);
4168 else
4169 video_device_release(btv->video_dev);
4170 btv->video_dev = NULL;
4171 }
4172 if (btv->vbi_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004173 if (video_is_registered(btv->vbi_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 video_unregister_device(btv->vbi_dev);
4175 else
4176 video_device_release(btv->vbi_dev);
4177 btv->vbi_dev = NULL;
4178 }
4179 if (btv->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004180 if (video_is_registered(btv->radio_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 video_unregister_device(btv->radio_dev);
4182 else
4183 video_device_release(btv->radio_dev);
4184 btv->radio_dev = NULL;
4185 }
4186}
4187
4188/* register video4linux devices */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004189static int bttv_register_video(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190{
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004191 if (no_overlay > 0)
Joe Perches8af443e2011-08-21 19:56:48 -03004192 pr_notice("Overlay support disabled\n");
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004193
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 /* video */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004195 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004196
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004197 if (NULL == btv->video_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004199 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
4200 video_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03004202 pr_info("%d: registered device %s\n",
4203 btv->c.nr, video_device_node_name(btv->video_dev));
Hans Verkuil22a04f12008-07-20 06:35:02 -03004204 if (device_create_file(&btv->video_dev->dev,
Kay Sievers54bd5b62007-10-08 16:26:13 -03004205 &dev_attr_card)<0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004206 pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
Trent Piephod94fc9a2006-07-29 17:18:06 -03004207 goto err;
4208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
4210 /* vbi */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004211 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004212
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004213 if (NULL == btv->vbi_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004215 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
4216 vbi_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03004218 pr_info("%d: registered device %s\n",
4219 btv->c.nr, video_device_node_name(btv->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004221 if (!btv->has_radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 return 0;
4223 /* radio */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004224 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004225 if (NULL == btv->radio_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004227 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4228 radio_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03004230 pr_info("%d: registered device %s\n",
4231 btv->c.nr, video_device_node_name(btv->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
4233 /* all done */
4234 return 0;
4235
4236 err:
4237 bttv_unregister_video(btv);
4238 return -1;
4239}
4240
4241
4242/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4243/* response on cards with no firmware is not enabled by OF */
4244static void pci_set_command(struct pci_dev *dev)
4245{
4246#if defined(__powerpc__)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004247 unsigned int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004249 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4250 cmd = (cmd | PCI_COMMAND_MEMORY );
4251 pci_write_config_dword(dev, PCI_COMMAND, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252#endif
4253}
4254
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004255static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256{
Hans Verkuil76ea9922013-02-06 11:49:14 -03004257 struct v4l2_frequency init_freq = {
4258 .tuner = 0,
4259 .type = V4L2_TUNER_ANALOG_TV,
4260 .frequency = 980,
4261 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 int result;
4263 unsigned char lat;
4264 struct bttv *btv;
4265
4266 if (bttv_num == BTTV_MAX)
4267 return -ENOMEM;
Joe Perches8af443e2011-08-21 19:56:48 -03004268 pr_info("Bt8xx card found (%d)\n", bttv_num);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004269 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004270 if (btv == NULL) {
Joe Perches8af443e2011-08-21 19:56:48 -03004271 pr_err("out of memory\n");
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004272 return -ENOMEM;
4273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 btv->c.nr = bttv_num;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004275 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4276 "bttv%d", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
4278 /* initialize structs / fill in defaults */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02004279 mutex_init(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004280 spin_lock_init(&btv->s_lock);
4281 spin_lock_init(&btv->gpio_lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004282 init_waitqueue_head(&btv->i2c_queue);
4283 INIT_LIST_HEAD(&btv->c.subs);
4284 INIT_LIST_HEAD(&btv->capture);
4285 INIT_LIST_HEAD(&btv->vcapture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 v4l2_prio_init(&btv->prio);
4287
4288 init_timer(&btv->timeout);
4289 btv->timeout.function = bttv_irq_timeout;
4290 btv->timeout.data = (unsigned long)btv;
4291
Michael Krufky7c08fb02005-11-08 21:36:21 -08004292 btv->i2c_rc = -1;
4293 btv->tuner_type = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 btv->new_input = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 btv->has_radio=radio[btv->c.nr];
4296
4297 /* pci stuff (init, get irq/mmio, ... */
4298 btv->c.pci = dev;
Michael Krufky7c08fb02005-11-08 21:36:21 -08004299 btv->id = dev->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 if (pci_enable_device(dev)) {
Joe Perches8af443e2011-08-21 19:56:48 -03004301 pr_warn("%d: Can't enable device\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 return -EIO;
4303 }
Yang Hongyang284901a2009-04-06 19:01:15 -07004304 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Joe Perches8af443e2011-08-21 19:56:48 -03004305 pr_warn("%d: No suitable DMA available\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 return -EIO;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 if (!request_mem_region(pci_resource_start(dev,0),
4309 pci_resource_len(dev,0),
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004310 btv->c.v4l2_dev.name)) {
Joe Perches8af443e2011-08-21 19:56:48 -03004311 pr_warn("%d: can't request iomem (0x%llx)\n",
4312 btv->c.nr,
4313 (unsigned long long)pci_resource_start(dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 return -EBUSY;
4315 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004316 pci_set_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 pci_set_command(dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004318
4319 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4320 if (result < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004321 pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004322 goto fail0;
4323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Bjørn Morkabd34d82011-03-21 11:35:56 -03004325 btv->revision = dev->revision;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004326 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
Joe Perches8af443e2011-08-21 19:56:48 -03004327 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
4328 bttv_num, btv->id, btv->revision, pci_name(dev),
4329 btv->c.pci->irq, lat,
4330 (unsigned long long)pci_resource_start(dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 schedule();
4332
Akinobu Mita5f1693f2006-12-20 10:08:56 -03004333 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4334 if (NULL == btv->bt848_mmio) {
Joe Perches8af443e2011-08-21 19:56:48 -03004335 pr_err("%d: ioremap() failed\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 result = -EIO;
4337 goto fail1;
4338 }
4339
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004340 /* identify card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 bttv_idcard(btv);
4342
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004343 /* disable irqs, register irq handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 btwrite(0, BT848_INT_MASK);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004345 result = request_irq(btv->c.pci->irq, bttv_irq,
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004346 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004347 if (result < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004348 pr_err("%d: can't get IRQ %d\n",
4349 bttv_num, btv->c.pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 goto fail1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
4353 if (0 != bttv_handle_chipset(btv)) {
4354 result = -EIO;
4355 goto fail2;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
4358 /* init options from insmod args */
4359 btv->opt_combfilter = combfilter;
4360 btv->opt_lumafilter = lumafilter;
4361 btv->opt_automute = automute;
4362 btv->opt_chroma_agc = chroma_agc;
4363 btv->opt_adc_crush = adc_crush;
4364 btv->opt_vcr_hack = vcr_hack;
4365 btv->opt_whitecrush_upper = whitecrush_upper;
4366 btv->opt_whitecrush_lower = whitecrush_lower;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07004367 btv->opt_uv_ratio = uv_ratio;
4368 btv->opt_full_luma_range = full_luma_range;
4369 btv->opt_coring = coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
4371 /* fill struct bttv with some useful defaults */
4372 btv->init.btv = btv;
4373 btv->init.ov.w.width = 320;
4374 btv->init.ov.w.height = 240;
Mauro Carvalho Chehabc96dd072007-10-26 16:51:47 -03004375 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 btv->init.width = 320;
4377 btv->init.height = 240;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 btv->input = 0;
4379
4380 /* initialize hardware */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004381 if (bttv_gpio)
4382 bttv_gpio_tracking(btv,"pre-init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
4384 bttv_risc_init_main(btv);
4385 init_bt848(btv);
4386
4387 /* gpio */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004388 btwrite(0x00, BT848_GPIO_REG_INP);
4389 btwrite(0x00, BT848_GPIO_OUT_EN);
4390 if (bttv_verbose)
4391 bttv_gpio_tracking(btv,"init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004393 /* needs to be done before i2c is registered */
4394 bttv_init_card1(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004396 /* register i2c + gpio */
4397 init_bttv_i2c(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004399 /* some card-specific stuff (needs working i2c) */
4400 bttv_init_card2(btv);
Hans Verkuil2c905772009-07-20 08:14:17 -03004401 bttv_init_tuner(btv);
Hans Verkuil76ea9922013-02-06 11:49:14 -03004402 if (btv->tuner_type != TUNER_ABSENT) {
4403 bttv_set_frequency(btv, &init_freq);
4404 btv->radio_freq = 90500 * 16; /* 90.5Mhz default */
4405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 init_irqreg(btv);
4407
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004408 /* register video4linux + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 if (!bttv_tvcards[btv->c.type].no_video) {
4410 bttv_register_video(btv);
4411 bt848_bright(btv,32768);
Alan McIvor961e6682012-04-01 21:11:08 -03004412 bt848_contrast(btv, 27648);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 bt848_hue(btv,32768);
4414 bt848_sat(btv,32768);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004415 audio_mute(btv, 1);
Trent Piepho333408f2007-07-03 15:08:10 -03004416 set_input(btv, 0, btv->tvnorm);
Michael Schimeke5bd0262007-01-18 16:17:39 -03004417 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4418 btv->crop[1] = btv->crop[0]; /* current = default */
4419 disclaim_vbi_lines(btv);
4420 disclaim_video_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 }
4422
Jarod Wilsonf992a492007-03-24 15:23:50 -03004423 /* add subdevices and autoload dvb-bt8xx if needed */
4424 if (bttv_tvcards[btv->c.type].has_dvb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 bttv_sub_add_device(&btv->c, "dvb");
Jarod Wilsonf992a492007-03-24 15:23:50 -03004426 request_modules(btv);
4427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
Jean Delvared90a4ae2010-02-16 14:22:37 -03004429 if (!disable_ir) {
4430 init_bttv_i2c_ir(btv);
4431 bttv_input_init(btv);
4432 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 /* everything is fine */
4435 bttv_num++;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004438fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004439 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004441fail1:
4442 v4l2_device_unregister(&btv->c.v4l2_dev);
4443
4444fail0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 if (btv->bt848_mmio)
4446 iounmap(btv->bt848_mmio);
4447 release_mem_region(pci_resource_start(btv->c.pci,0),
4448 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 return result;
4450}
4451
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004452static void bttv_remove(struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004454 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4455 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
4457 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03004458 pr_info("%d: unloading\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Tejun Heo707bcf32010-12-24 16:14:20 +01004460 if (bttv_tvcards[btv->c.type].has_dvb)
4461 flush_request_modules(btv);
4462
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004463 /* shutdown everything (DMA+IRQs) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 btand(~15, BT848_GPIO_DMA_CTL);
4465 btwrite(0, BT848_INT_MASK);
4466 btwrite(~0x0, BT848_INT_STAT);
4467 btwrite(0x0, BT848_GPIO_OUT_EN);
4468 if (bttv_gpio)
4469 bttv_gpio_tracking(btv,"cleanup");
4470
4471 /* tell gpio modules we are leaving ... */
4472 btv->shutdown=1;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004473 bttv_input_fini(btv);
Christopher Pascoe889aee82006-01-09 15:25:28 -02004474 bttv_sub_del_devices(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004476 /* unregister i2c_bus + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 fini_bttv_i2c(btv);
4478
4479 /* unregister video4linux */
4480 bttv_unregister_video(btv);
4481
4482 /* free allocated memory */
4483 btcx_riscmem_free(btv->c.pci,&btv->main);
4484
4485 /* free ressources */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004486 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 iounmap(btv->bt848_mmio);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004488 release_mem_region(pci_resource_start(btv->c.pci,0),
4489 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004491 v4l2_device_unregister(&btv->c.v4l2_dev);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004492 bttvs[btv->c.nr] = NULL;
4493 kfree(btv);
4494
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004495 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496}
4497
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004498#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4500{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004501 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4502 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 struct bttv_buffer_set idle;
4504 unsigned long flags;
4505
Joe Perches8af443e2011-08-21 19:56:48 -03004506 dprintk("%d: suspend %d\n", btv->c.nr, state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
4508 /* stop dma + irqs */
4509 spin_lock_irqsave(&btv->s_lock,flags);
4510 memset(&idle, 0, sizeof(idle));
4511 btv->state.video = btv->curr;
4512 btv->state.vbi = btv->cvbi;
4513 btv->state.loop_irq = btv->loop_irq;
4514 btv->curr = idle;
4515 btv->loop_irq = 0;
4516 bttv_buffer_activate_video(btv, &idle);
4517 bttv_buffer_activate_vbi(btv, NULL);
4518 bttv_set_dma(btv, 0);
4519 btwrite(0, BT848_INT_MASK);
4520 spin_unlock_irqrestore(&btv->s_lock,flags);
4521
4522 /* save bt878 state */
4523 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4524 btv->state.gpio_data = gpio_read();
4525
4526 /* save pci state */
4527 pci_save_state(pci_dev);
4528 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4529 pci_disable_device(pci_dev);
4530 btv->state.disabled = 1;
4531 }
4532 return 0;
4533}
4534
4535static int bttv_resume(struct pci_dev *pci_dev)
4536{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004537 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4538 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004540 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
Joe Perches8af443e2011-08-21 19:56:48 -03004542 dprintk("%d: resume\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
4544 /* restore pci state */
4545 if (btv->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004546 err=pci_enable_device(pci_dev);
4547 if (err) {
Joe Perches8af443e2011-08-21 19:56:48 -03004548 pr_warn("%d: Can't enable device\n", btv->c.nr);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004549 return err;
4550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 btv->state.disabled = 0;
4552 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004553 err=pci_set_power_state(pci_dev, PCI_D0);
4554 if (err) {
4555 pci_disable_device(pci_dev);
Joe Perches8af443e2011-08-21 19:56:48 -03004556 pr_warn("%d: Can't enable device\n", btv->c.nr);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004557 btv->state.disabled = 1;
4558 return err;
4559 }
4560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 pci_restore_state(pci_dev);
4562
4563 /* restore bt878 state */
4564 bttv_reinit_bt848(btv);
4565 gpio_inout(0xffffff, btv->state.gpio_enable);
4566 gpio_write(btv->state.gpio_data);
4567
4568 /* restart dma */
4569 spin_lock_irqsave(&btv->s_lock,flags);
4570 btv->curr = btv->state.video;
4571 btv->cvbi = btv->state.vbi;
4572 btv->loop_irq = btv->state.loop_irq;
4573 bttv_buffer_activate_video(btv, &btv->curr);
4574 bttv_buffer_activate_vbi(btv, btv->cvbi);
4575 bttv_set_dma(btv, 0);
4576 spin_unlock_irqrestore(&btv->s_lock,flags);
4577 return 0;
4578}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004579#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
4581static struct pci_device_id bttv_pci_tbl[] = {
Joe Perches76e97412009-07-05 15:59:21 -03004582 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4583 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4584 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4585 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
Peter De Schrijverc540d9f2011-11-06 10:47:58 -03004586 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004587 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588};
4589
4590MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4591
4592static struct pci_driver bttv_pci_driver = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004593 .name = "bttv",
4594 .id_table = bttv_pci_tbl,
4595 .probe = bttv_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004596 .remove = bttv_remove,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004597#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 .suspend = bttv_suspend,
4599 .resume = bttv_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004600#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601};
4602
Adrian Bunk7d44e892007-12-11 19:23:43 -03004603static int __init bttv_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604{
Randy Dunlapc526e222006-07-15 09:08:26 -03004605 int ret;
4606
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 bttv_num = 0;
4608
Joe Perches8af443e2011-08-21 19:56:48 -03004609 pr_info("driver version %s loaded\n", BTTV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4611 gbuffers = 2;
Filipe Rossetf41b6962009-05-28 11:11:53 -03004612 if (gbufsize > BTTV_MAX_FBUF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 gbufsize = BTTV_MAX_FBUF;
4614 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4615 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03004616 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
4617 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
4619 bttv_check_chipset();
4620
Randy Dunlapc526e222006-07-15 09:08:26 -03004621 ret = bus_register(&bttv_sub_bus_type);
4622 if (ret < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004623 pr_warn("bus_register error: %d\n", ret);
Randy Dunlapc526e222006-07-15 09:08:26 -03004624 return ret;
4625 }
Akinobu Mita9e7e85e2007-12-17 14:26:29 -03004626 ret = pci_register_driver(&bttv_pci_driver);
4627 if (ret < 0)
4628 bus_unregister(&bttv_sub_bus_type);
4629
4630 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631}
4632
Adrian Bunk7d44e892007-12-11 19:23:43 -03004633static void __exit bttv_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634{
4635 pci_unregister_driver(&bttv_pci_driver);
4636 bus_unregister(&bttv_sub_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637}
4638
4639module_init(bttv_init_module);
4640module_exit(bttv_cleanup_module);
4641
4642/*
4643 * Local variables:
4644 * c-basic-offset: 8
4645 * End:
4646 */