blob: cc7f58f94cde3659f30b50b846bc7412309b3e3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
3 bttv - Bt848 frame grabber driver
4
5 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08006 & Marcus Metzler <mocm@thp.uni-koeln.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
8
9 some v4l2 code lines are taken from Justin's bttv2 driver which is
10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
11
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -030012 V4L1 removal from:
13 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
14
Mauro Carvalho Chehabc96dd072007-10-26 16:51:47 -030015 Fixes to be fully V4L2 compliant by
16 (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
17
Michael Schimeke5bd0262007-01-18 16:17:39 -030018 Cropping and overscan support
19 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
20 Sponsored by OPQ Systems AB
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35*/
36
Joe Perches8af443e2011-08-21 19:56:48 -030037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
40#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/fs.h>
45#include <linux/kernel.h>
46#include <linux/sched.h>
47#include <linux/interrupt.h>
48#include <linux/kdev_t.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020049#include "bttvp.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020050#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030051#include <media/v4l2-ioctl.h>
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -030052#include <media/tvaudio.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030053#include <media/msp3400.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020054
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -070055#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#include <asm/io.h>
58#include <asm/byteorder.h>
59
Hans Verkuilb9218f22010-12-27 12:22:46 -030060#include <media/saa6588.h>
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070061
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030062#define BTTV_VERSION "0.9.19"
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064unsigned int bttv_num; /* number of Bt848s in use */
Trent Piepho4b10d3b2009-01-28 21:32:59 -030065struct bttv *bttvs[BTTV_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020067unsigned int bttv_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068unsigned int bttv_verbose = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020069unsigned int bttv_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/* config variables */
72#ifdef __BIG_ENDIAN
73static unsigned int bigendian=1;
74#else
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020075static unsigned int bigendian;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#endif
77static unsigned int radio[BTTV_MAX];
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020078static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static unsigned int gbuffers = 8;
80static unsigned int gbufsize = 0x208000;
Michael Schimeke5bd0262007-01-18 16:17:39 -030081static unsigned int reset_crop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Jean Delvare176c2f32008-09-07 12:49:59 -030083static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
84static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
85static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020086static int debug_latency;
Jean Delvared90a4ae2010-02-16 14:22:37 -030087static int disable_ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020089static unsigned int fdsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/* options */
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020092static unsigned int combfilter;
93static unsigned int lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static unsigned int automute = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020095static unsigned int chroma_agc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static unsigned int adc_crush = 1;
97static unsigned int whitecrush_upper = 0xCF;
98static unsigned int whitecrush_lower = 0x7F;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020099static unsigned int vcr_hack;
100static unsigned int irq_iswitch;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700101static unsigned int uv_ratio = 50;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200102static unsigned int full_luma_range;
103static unsigned int coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/* API features (turn on/off stuff for testing) */
106static unsigned int v4l2 = 1;
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* insmod args */
109module_param(bttv_verbose, int, 0644);
110module_param(bttv_gpio, int, 0644);
111module_param(bttv_debug, int, 0644);
112module_param(irq_debug, int, 0644);
113module_param(debug_latency, int, 0644);
Jean Delvared90a4ae2010-02-16 14:22:37 -0300114module_param(disable_ir, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116module_param(fdsr, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117module_param(gbuffers, int, 0444);
118module_param(gbufsize, int, 0444);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300119module_param(reset_crop, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121module_param(v4l2, int, 0644);
122module_param(bigendian, int, 0644);
123module_param(irq_iswitch, int, 0644);
124module_param(combfilter, int, 0444);
125module_param(lumafilter, int, 0444);
126module_param(automute, int, 0444);
127module_param(chroma_agc, int, 0444);
128module_param(adc_crush, int, 0444);
129module_param(whitecrush_upper, int, 0444);
130module_param(whitecrush_lower, int, 0444);
131module_param(vcr_hack, int, 0444);
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700132module_param(uv_ratio, int, 0444);
133module_param(full_luma_range, int, 0444);
134module_param(coring, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Jean Delvare176c2f32008-09-07 12:49:59 -0300136module_param_array(radio, int, NULL, 0444);
137module_param_array(video_nr, int, NULL, 0444);
138module_param_array(radio_nr, int, NULL, 0444);
139module_param_array(vbi_nr, int, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
142MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
143MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
144MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
145MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
146MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
Jean Delvared90a4ae2010-02-16 14:22:37 -0300147MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
149MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
Michael Schimeke5bd0262007-01-18 16:17:39 -0300150MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
151 "is 1 (yes) for compatibility with older applications");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
153MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
154MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
155MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
156MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
157MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
158MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700159MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
160MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
161MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
Jean Delvare176c2f32008-09-07 12:49:59 -0300162MODULE_PARM_DESC(video_nr, "video device numbers");
163MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
164MODULE_PARM_DESC(radio_nr, "radio device numbers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
167MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
168MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -0300169MODULE_VERSION(BTTV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/* ----------------------------------------------------------------------- */
172/* sysfs */
173
Kay Sievers54bd5b62007-10-08 16:26:13 -0300174static ssize_t show_card(struct device *cd,
175 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300177 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -0300178 struct bttv *btv = video_get_drvdata(vfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
180}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300181static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183/* ----------------------------------------------------------------------- */
Jarod Wilsonf992a492007-03-24 15:23:50 -0300184/* dvb auto-load setup */
185#if defined(CONFIG_MODULES) && defined(MODULE)
186static void request_module_async(struct work_struct *work)
187{
188 request_module("dvb-bt8xx");
189}
190
191static void request_modules(struct bttv *dev)
192{
193 INIT_WORK(&dev->request_module_wk, request_module_async);
194 schedule_work(&dev->request_module_wk);
195}
Tejun Heo707bcf32010-12-24 16:14:20 +0100196
197static void flush_request_modules(struct bttv *dev)
198{
Linus Torvalds0b8e74c2012-10-07 17:49:05 +0900199 flush_work(&dev->request_module_wk);
Tejun Heo707bcf32010-12-24 16:14:20 +0100200}
Jarod Wilsonf992a492007-03-24 15:23:50 -0300201#else
202#define request_modules(dev)
Mauro Carvalho Chehaba1fd2872012-10-27 16:33:41 -0300203#define flush_request_modules(dev) do {} while(0)
Jarod Wilsonf992a492007-03-24 15:23:50 -0300204#endif /* CONFIG_MODULES */
205
206
207/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/* static data */
209
210/* special timing tables from conexant... */
211static u8 SRAM_Table[][60] =
212{
213 /* PAL digital input over GPIO[7:0] */
214 {
215 45, // 45 bytes following
216 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
217 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
218 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
219 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
220 0x37,0x00,0xAF,0x21,0x00
221 },
222 /* NTSC digital input over GPIO[7:0] */
223 {
224 51, // 51 bytes following
225 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
226 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
227 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
228 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
229 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
230 0x00,
231 },
232 // TGB_NTSC392 // quartzsight
233 // This table has been modified to be used for Fusion Rev D
234 {
235 0x2A, // size of table = 42
236 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
237 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
238 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
239 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
240 0x20, 0x00
241 }
242};
243
Michael Schimeke5bd0262007-01-18 16:17:39 -0300244/* minhdelayx1 first video pixel we can capture on a line and
245 hdelayx1 start of active video, both relative to rising edge of
246 /HRESET pulse (0H) in 1 / fCLKx1.
247 swidth width of active video and
248 totalwidth total line width, both in 1 / fCLKx1.
249 sqwidth total line width in square pixels.
250 vdelay start of active video in 2 * field lines relative to
251 trailing edge of /VRESET pulse (VDELAY register).
252 sheight height of active video in 2 * field lines.
253 videostart0 ITU-R frame line number of the line corresponding
254 to vdelay in the first field. */
255#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
256 vdelay, sheight, videostart0) \
257 .cropcap.bounds.left = minhdelayx1, \
258 /* * 2 because vertically we count field lines times two, */ \
259 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
260 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
261 /* 4 is a safety margin at the end of the line. */ \
262 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
263 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
264 .cropcap.defrect.left = hdelayx1, \
265 .cropcap.defrect.top = (videostart0) * 2, \
266 .cropcap.defrect.width = swidth, \
267 .cropcap.defrect.height = sheight, \
268 .cropcap.pixelaspect.numerator = totalwidth, \
269 .cropcap.pixelaspect.denominator = sqwidth,
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271const struct bttv_tvnorm bttv_tvnorms[] = {
272 /* PAL-BDGHI */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800273 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
274 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 {
276 .v4l2_id = V4L2_STD_PAL,
277 .name = "PAL",
278 .Fsc = 35468950,
279 .swidth = 924,
280 .sheight = 576,
281 .totalwidth = 1135,
282 .adelay = 0x7f,
283 .bdelay = 0x72,
284 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
285 .scaledtwidth = 1135,
286 .hdelayx1 = 186,
287 .hactivex1 = 924,
288 .vdelay = 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300289 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 .sram = 0,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200291 /* ITU-R frame line number of the first VBI line
Michael Schimeke5bd0262007-01-18 16:17:39 -0300292 we can capture, of the first and second field.
293 The last line is determined by cropcap.bounds. */
294 .vbistart = { 7, 320 },
295 CROPCAP(/* minhdelayx1 */ 68,
296 /* hdelayx1 */ 186,
297 /* Should be (768 * 1135 + 944 / 2) / 944.
298 cropcap.defrect is used for image width
299 checks, so we keep the old value 924. */
300 /* swidth */ 924,
301 /* totalwidth */ 1135,
302 /* sqwidth */ 944,
303 /* vdelay */ 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300304 /* bt878 (and bt848?) can capture another
305 line below active video. */
Mauro Carvalho Chehaba1fd2872012-10-27 16:33:41 -0300306 /* sheight */ (576 + 2) + 0x20 - 2,
307 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 },{
Hans Verkuild97a11e2006-02-07 06:48:40 -0200309 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 .name = "NTSC",
311 .Fsc = 28636363,
312 .swidth = 768,
313 .sheight = 480,
314 .totalwidth = 910,
315 .adelay = 0x68,
316 .bdelay = 0x5d,
317 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
318 .scaledtwidth = 910,
319 .hdelayx1 = 128,
320 .hactivex1 = 910,
321 .vdelay = 0x1a,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300322 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .sram = 1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200324 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300325 CROPCAP(/* minhdelayx1 */ 68,
326 /* hdelayx1 */ 128,
327 /* Should be (640 * 910 + 780 / 2) / 780? */
328 /* swidth */ 768,
329 /* totalwidth */ 910,
330 /* sqwidth */ 780,
331 /* vdelay */ 0x1a,
332 /* sheight */ 480,
333 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 },{
335 .v4l2_id = V4L2_STD_SECAM,
336 .name = "SECAM",
337 .Fsc = 35468950,
338 .swidth = 924,
339 .sheight = 576,
340 .totalwidth = 1135,
341 .adelay = 0x7f,
342 .bdelay = 0xb0,
343 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
344 .scaledtwidth = 1135,
345 .hdelayx1 = 186,
346 .hactivex1 = 922,
347 .vdelay = 0x20,
348 .vbipack = 255,
349 .sram = 0, /* like PAL, correct? */
Michael H. Schimek67f15702006-01-09 15:25:27 -0200350 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300351 CROPCAP(/* minhdelayx1 */ 68,
352 /* hdelayx1 */ 186,
353 /* swidth */ 924,
354 /* totalwidth */ 1135,
355 /* sqwidth */ 944,
356 /* vdelay */ 0x20,
357 /* sheight */ 576,
358 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 },{
360 .v4l2_id = V4L2_STD_PAL_Nc,
361 .name = "PAL-Nc",
362 .Fsc = 28636363,
363 .swidth = 640,
364 .sheight = 576,
365 .totalwidth = 910,
366 .adelay = 0x68,
367 .bdelay = 0x5d,
368 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
369 .scaledtwidth = 780,
370 .hdelayx1 = 130,
371 .hactivex1 = 734,
372 .vdelay = 0x1a,
373 .vbipack = 144,
374 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200375 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300376 CROPCAP(/* minhdelayx1 */ 68,
377 /* hdelayx1 */ 130,
378 /* swidth */ (640 * 910 + 780 / 2) / 780,
379 /* totalwidth */ 910,
380 /* sqwidth */ 780,
381 /* vdelay */ 0x1a,
382 /* sheight */ 576,
383 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 },{
385 .v4l2_id = V4L2_STD_PAL_M,
386 .name = "PAL-M",
387 .Fsc = 28636363,
388 .swidth = 640,
389 .sheight = 480,
390 .totalwidth = 910,
391 .adelay = 0x68,
392 .bdelay = 0x5d,
393 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
394 .scaledtwidth = 780,
395 .hdelayx1 = 135,
396 .hactivex1 = 754,
397 .vdelay = 0x1a,
398 .vbipack = 144,
399 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200400 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300401 CROPCAP(/* minhdelayx1 */ 68,
402 /* hdelayx1 */ 135,
403 /* swidth */ (640 * 910 + 780 / 2) / 780,
404 /* totalwidth */ 910,
405 /* sqwidth */ 780,
406 /* vdelay */ 0x1a,
407 /* sheight */ 480,
408 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 },{
410 .v4l2_id = V4L2_STD_PAL_N,
411 .name = "PAL-N",
412 .Fsc = 35468950,
413 .swidth = 768,
414 .sheight = 576,
415 .totalwidth = 1135,
416 .adelay = 0x7f,
417 .bdelay = 0x72,
418 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
419 .scaledtwidth = 944,
420 .hdelayx1 = 186,
421 .hactivex1 = 922,
422 .vdelay = 0x20,
423 .vbipack = 144,
424 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300425 .vbistart = { 7, 320 },
426 CROPCAP(/* minhdelayx1 */ 68,
427 /* hdelayx1 */ 186,
428 /* swidth */ (768 * 1135 + 944 / 2) / 944,
429 /* totalwidth */ 1135,
430 /* sqwidth */ 944,
431 /* vdelay */ 0x20,
432 /* sheight */ 576,
433 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 },{
435 .v4l2_id = V4L2_STD_NTSC_M_JP,
436 .name = "NTSC-JP",
437 .Fsc = 28636363,
438 .swidth = 640,
439 .sheight = 480,
440 .totalwidth = 910,
441 .adelay = 0x68,
442 .bdelay = 0x5d,
443 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
444 .scaledtwidth = 780,
445 .hdelayx1 = 135,
446 .hactivex1 = 754,
447 .vdelay = 0x16,
448 .vbipack = 144,
449 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300450 .vbistart = { 10, 273 },
451 CROPCAP(/* minhdelayx1 */ 68,
452 /* hdelayx1 */ 135,
453 /* swidth */ (640 * 910 + 780 / 2) / 780,
454 /* totalwidth */ 910,
455 /* sqwidth */ 780,
456 /* vdelay */ 0x16,
457 /* sheight */ 480,
458 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 },{
460 /* that one hopefully works with the strange timing
461 * which video recorders produce when playing a NTSC
462 * tape on a PAL TV ... */
463 .v4l2_id = V4L2_STD_PAL_60,
464 .name = "PAL-60",
465 .Fsc = 35468950,
466 .swidth = 924,
467 .sheight = 480,
468 .totalwidth = 1135,
469 .adelay = 0x7f,
470 .bdelay = 0x72,
471 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
472 .scaledtwidth = 1135,
473 .hdelayx1 = 186,
474 .hactivex1 = 924,
475 .vdelay = 0x1a,
476 .vbipack = 255,
477 .vtotal = 524,
478 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200479 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300480 CROPCAP(/* minhdelayx1 */ 68,
481 /* hdelayx1 */ 186,
482 /* swidth */ 924,
483 /* totalwidth */ 1135,
484 /* sqwidth */ 944,
485 /* vdelay */ 0x1a,
486 /* sheight */ 480,
487 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489};
490static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
491
492/* ----------------------------------------------------------------------- */
493/* bttv format list
494 packed pixel formats must come first */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300495static const struct bttv_format formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 {
497 .name = "8 bpp, gray",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 .fourcc = V4L2_PIX_FMT_GREY,
499 .btformat = BT848_COLOR_FMT_Y8,
500 .depth = 8,
501 .flags = FORMAT_FLAGS_PACKED,
502 },{
503 .name = "8 bpp, dithered color",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 .fourcc = V4L2_PIX_FMT_HI240,
505 .btformat = BT848_COLOR_FMT_RGB8,
506 .depth = 8,
507 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
508 },{
509 .name = "15 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .fourcc = V4L2_PIX_FMT_RGB555,
511 .btformat = BT848_COLOR_FMT_RGB15,
512 .depth = 16,
513 .flags = FORMAT_FLAGS_PACKED,
514 },{
515 .name = "15 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 .fourcc = V4L2_PIX_FMT_RGB555X,
517 .btformat = BT848_COLOR_FMT_RGB15,
518 .btswap = 0x03, /* byteswap */
519 .depth = 16,
520 .flags = FORMAT_FLAGS_PACKED,
521 },{
522 .name = "16 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 .fourcc = V4L2_PIX_FMT_RGB565,
524 .btformat = BT848_COLOR_FMT_RGB16,
525 .depth = 16,
526 .flags = FORMAT_FLAGS_PACKED,
527 },{
528 .name = "16 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 .fourcc = V4L2_PIX_FMT_RGB565X,
530 .btformat = BT848_COLOR_FMT_RGB16,
531 .btswap = 0x03, /* byteswap */
532 .depth = 16,
533 .flags = FORMAT_FLAGS_PACKED,
534 },{
535 .name = "24 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 .fourcc = V4L2_PIX_FMT_BGR24,
537 .btformat = BT848_COLOR_FMT_RGB24,
538 .depth = 24,
539 .flags = FORMAT_FLAGS_PACKED,
540 },{
541 .name = "32 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 .fourcc = V4L2_PIX_FMT_BGR32,
543 .btformat = BT848_COLOR_FMT_RGB32,
544 .depth = 32,
545 .flags = FORMAT_FLAGS_PACKED,
546 },{
547 .name = "32 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 .fourcc = V4L2_PIX_FMT_RGB32,
549 .btformat = BT848_COLOR_FMT_RGB32,
550 .btswap = 0x0f, /* byte+word swap */
551 .depth = 32,
552 .flags = FORMAT_FLAGS_PACKED,
553 },{
554 .name = "4:2:2, packed, YUYV",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 .fourcc = V4L2_PIX_FMT_YUYV,
556 .btformat = BT848_COLOR_FMT_YUY2,
557 .depth = 16,
558 .flags = FORMAT_FLAGS_PACKED,
559 },{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 .name = "4:2:2, packed, UYVY",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 .fourcc = V4L2_PIX_FMT_UYVY,
562 .btformat = BT848_COLOR_FMT_YUY2,
563 .btswap = 0x03, /* byteswap */
564 .depth = 16,
565 .flags = FORMAT_FLAGS_PACKED,
566 },{
567 .name = "4:2:2, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 .fourcc = V4L2_PIX_FMT_YUV422P,
569 .btformat = BT848_COLOR_FMT_YCrCb422,
570 .depth = 16,
571 .flags = FORMAT_FLAGS_PLANAR,
572 .hshift = 1,
573 .vshift = 0,
574 },{
575 .name = "4:2:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 .fourcc = V4L2_PIX_FMT_YUV420,
577 .btformat = BT848_COLOR_FMT_YCrCb422,
578 .depth = 12,
579 .flags = FORMAT_FLAGS_PLANAR,
580 .hshift = 1,
581 .vshift = 1,
582 },{
583 .name = "4:2:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 .fourcc = V4L2_PIX_FMT_YVU420,
585 .btformat = BT848_COLOR_FMT_YCrCb422,
586 .depth = 12,
587 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
588 .hshift = 1,
589 .vshift = 1,
590 },{
591 .name = "4:1:1, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 .fourcc = V4L2_PIX_FMT_YUV411P,
593 .btformat = BT848_COLOR_FMT_YCrCb411,
594 .depth = 12,
595 .flags = FORMAT_FLAGS_PLANAR,
596 .hshift = 2,
597 .vshift = 0,
598 },{
599 .name = "4:1:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 .fourcc = V4L2_PIX_FMT_YUV410,
601 .btformat = BT848_COLOR_FMT_YCrCb411,
602 .depth = 9,
603 .flags = FORMAT_FLAGS_PLANAR,
604 .hshift = 2,
605 .vshift = 2,
606 },{
607 .name = "4:1:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 .fourcc = V4L2_PIX_FMT_YVU410,
609 .btformat = BT848_COLOR_FMT_YCrCb411,
610 .depth = 9,
611 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
612 .hshift = 2,
613 .vshift = 2,
614 },{
615 .name = "raw scanlines",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 .fourcc = -1,
617 .btformat = BT848_COLOR_FMT_RAW,
618 .depth = 8,
619 .flags = FORMAT_FLAGS_RAW,
620 }
621};
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300622static const unsigned int FORMATS = ARRAY_SIZE(formats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624/* ----------------------------------------------------------------------- */
625
626#define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
627#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
628#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
629#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
630#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
631#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
632#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
633#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700634#define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
635#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
636#define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
637#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639static const struct v4l2_queryctrl no_ctl = {
640 .name = "42",
641 .flags = V4L2_CTRL_FLAG_DISABLED,
642};
643static const struct v4l2_queryctrl bttv_ctls[] = {
644 /* --- video --- */
645 {
646 .id = V4L2_CID_BRIGHTNESS,
647 .name = "Brightness",
648 .minimum = 0,
649 .maximum = 65535,
650 .step = 256,
651 .default_value = 32768,
652 .type = V4L2_CTRL_TYPE_INTEGER,
653 },{
654 .id = V4L2_CID_CONTRAST,
655 .name = "Contrast",
656 .minimum = 0,
657 .maximum = 65535,
658 .step = 128,
Alan McIvor961e6682012-04-01 21:11:08 -0300659 .default_value = 27648,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 .type = V4L2_CTRL_TYPE_INTEGER,
661 },{
662 .id = V4L2_CID_SATURATION,
663 .name = "Saturation",
664 .minimum = 0,
665 .maximum = 65535,
666 .step = 128,
667 .default_value = 32768,
668 .type = V4L2_CTRL_TYPE_INTEGER,
669 },{
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -0300670 .id = V4L2_CID_COLOR_KILLER,
671 .name = "Color killer",
672 .minimum = 0,
673 .maximum = 1,
674 .type = V4L2_CTRL_TYPE_BOOLEAN,
675 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 .id = V4L2_CID_HUE,
677 .name = "Hue",
678 .minimum = 0,
679 .maximum = 65535,
680 .step = 256,
681 .default_value = 32768,
682 .type = V4L2_CTRL_TYPE_INTEGER,
683 },
684 /* --- audio --- */
685 {
686 .id = V4L2_CID_AUDIO_MUTE,
687 .name = "Mute",
688 .minimum = 0,
689 .maximum = 1,
690 .type = V4L2_CTRL_TYPE_BOOLEAN,
691 },{
692 .id = V4L2_CID_AUDIO_VOLUME,
693 .name = "Volume",
694 .minimum = 0,
695 .maximum = 65535,
696 .step = 65535/100,
697 .default_value = 65535,
698 .type = V4L2_CTRL_TYPE_INTEGER,
699 },{
700 .id = V4L2_CID_AUDIO_BALANCE,
701 .name = "Balance",
702 .minimum = 0,
703 .maximum = 65535,
704 .step = 65535/100,
705 .default_value = 32768,
706 .type = V4L2_CTRL_TYPE_INTEGER,
707 },{
708 .id = V4L2_CID_AUDIO_BASS,
709 .name = "Bass",
710 .minimum = 0,
711 .maximum = 65535,
712 .step = 65535/100,
713 .default_value = 32768,
714 .type = V4L2_CTRL_TYPE_INTEGER,
715 },{
716 .id = V4L2_CID_AUDIO_TREBLE,
717 .name = "Treble",
718 .minimum = 0,
719 .maximum = 65535,
720 .step = 65535/100,
721 .default_value = 32768,
722 .type = V4L2_CTRL_TYPE_INTEGER,
723 },
724 /* --- private --- */
725 {
726 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
727 .name = "chroma agc",
728 .minimum = 0,
729 .maximum = 1,
730 .type = V4L2_CTRL_TYPE_BOOLEAN,
731 },{
732 .id = V4L2_CID_PRIVATE_COMBFILTER,
733 .name = "combfilter",
734 .minimum = 0,
735 .maximum = 1,
736 .type = V4L2_CTRL_TYPE_BOOLEAN,
737 },{
738 .id = V4L2_CID_PRIVATE_AUTOMUTE,
739 .name = "automute",
740 .minimum = 0,
741 .maximum = 1,
742 .type = V4L2_CTRL_TYPE_BOOLEAN,
743 },{
744 .id = V4L2_CID_PRIVATE_LUMAFILTER,
745 .name = "luma decimation filter",
746 .minimum = 0,
747 .maximum = 1,
748 .type = V4L2_CTRL_TYPE_BOOLEAN,
749 },{
750 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
751 .name = "agc crush",
752 .minimum = 0,
753 .maximum = 1,
754 .type = V4L2_CTRL_TYPE_BOOLEAN,
755 },{
756 .id = V4L2_CID_PRIVATE_VCR_HACK,
757 .name = "vcr hack",
758 .minimum = 0,
759 .maximum = 1,
760 .type = V4L2_CTRL_TYPE_BOOLEAN,
761 },{
762 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
763 .name = "whitecrush upper",
764 .minimum = 0,
765 .maximum = 255,
766 .step = 1,
767 .default_value = 0xCF,
768 .type = V4L2_CTRL_TYPE_INTEGER,
769 },{
770 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
771 .name = "whitecrush lower",
772 .minimum = 0,
773 .maximum = 255,
774 .step = 1,
775 .default_value = 0x7F,
776 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700777 },{
778 .id = V4L2_CID_PRIVATE_UV_RATIO,
779 .name = "uv ratio",
780 .minimum = 0,
781 .maximum = 100,
782 .step = 1,
783 .default_value = 50,
784 .type = V4L2_CTRL_TYPE_INTEGER,
785 },{
786 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
787 .name = "full luma range",
788 .minimum = 0,
789 .maximum = 1,
790 .type = V4L2_CTRL_TYPE_BOOLEAN,
791 },{
792 .id = V4L2_CID_PRIVATE_CORING,
793 .name = "coring",
794 .minimum = 0,
795 .maximum = 3,
796 .step = 1,
797 .default_value = 0,
798 .type = V4L2_CTRL_TYPE_INTEGER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700801
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300805static const struct v4l2_queryctrl *ctrl_by_id(int id)
806{
807 int i;
808
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300809 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300810 if (bttv_ctls[i].id == id)
811 return bttv_ctls+i;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300812
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300813 return NULL;
814}
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816/* ----------------------------------------------------------------------- */
817/* resource management */
818
Michael Schimeke5bd0262007-01-18 16:17:39 -0300819/*
820 RESOURCE_ allocated by freed by
821
822 VIDEO_READ bttv_read 1) bttv_read 2)
823
824 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
825 VIDIOC_QBUF 1) bttv_release
826 VIDIOCMCAPTURE 1)
827
828 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
829 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
830 3) bttv_release
831
832 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
833 VIDIOC_QBUF 1) bttv_release
834 bttv_read, bttv_poll 1) 4)
835
836 1) The resource must be allocated when we enter buffer prepare functions
837 and remain allocated while buffers are in the DMA queue.
838 2) This is a single frame read.
839 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
840 RESOURCE_OVERLAY is allocated.
841 4) This is a continuous read, implies VIDIOC_STREAMON.
842
843 Note this driver permits video input and standard changes regardless if
844 resources are allocated.
845*/
846
847#define VBI_RESOURCES (RESOURCE_VBI)
848#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
849 RESOURCE_VIDEO_STREAM | \
850 RESOURCE_OVERLAY)
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300853int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Michael Schimeke5bd0262007-01-18 16:17:39 -0300855 int xbits; /* mutual exclusive resources */
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (fh->resources & bit)
858 /* have it already allocated */
859 return 1;
860
Michael Schimeke5bd0262007-01-18 16:17:39 -0300861 xbits = bit;
862 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
863 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 /* is it free? */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300866 if (btv->resources & xbits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* no, someone else uses it */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300868 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300870
871 if ((bit & VIDEO_RESOURCES)
872 && 0 == (btv->resources & VIDEO_RESOURCES)) {
873 /* Do crop - use current, don't - use default parameters. */
874 __s32 top = btv->crop[!!fh->do_crop].rect.top;
875
876 if (btv->vbi_end > top)
877 goto fail;
878
879 /* We cannot capture the same line as video and VBI data.
880 Claim scan lines crop[].rect.top to bottom. */
881 btv->crop_start = top;
882 } else if (bit & VBI_RESOURCES) {
883 __s32 end = fh->vbi_fmt.end;
884
885 if (end > btv->crop_start)
886 goto fail;
887
888 /* Claim scan lines above fh->vbi_fmt.end. */
889 btv->vbi_end = end;
890 }
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /* it's free, grab it */
893 fh->resources |= bit;
894 btv->resources |= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return 1;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300896
897 fail:
Michael Schimeke5bd0262007-01-18 16:17:39 -0300898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901static
902int check_btres(struct bttv_fh *fh, int bit)
903{
904 return (fh->resources & bit);
905}
906
907static
908int locked_btres(struct bttv *btv, int bit)
909{
910 return (btv->resources & bit);
911}
912
Michael Schimeke5bd0262007-01-18 16:17:39 -0300913/* Call with btv->lock down. */
914static void
915disclaim_vbi_lines(struct bttv *btv)
916{
917 btv->vbi_end = 0;
918}
919
920/* Call with btv->lock down. */
921static void
922disclaim_video_lines(struct bttv *btv)
923{
924 const struct bttv_tvnorm *tvnorm;
925 u8 crop;
926
927 tvnorm = &bttv_tvnorms[btv->tvnorm];
928 btv->crop_start = tvnorm->cropcap.bounds.top
929 + tvnorm->cropcap.bounds.height;
930
931 /* VBI capturing ends at VDELAY, start of video capturing, no
932 matter how many lines the VBI RISC program expects. When video
933 capturing is off, it shall no longer "preempt" VBI capturing,
934 so we set VDELAY to maximum. */
935 crop = btread(BT848_E_CROP) | 0xc0;
936 btwrite(crop, BT848_E_CROP);
937 btwrite(0xfe, BT848_E_VDELAY_LO);
938 btwrite(crop, BT848_O_CROP);
939 btwrite(0xfe, BT848_O_VDELAY_LO);
940}
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300943void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if ((fh->resources & bits) != bits) {
Joe Perches8af443e2011-08-21 19:56:48 -0300946 /* trying to free resources not allocated by us ... */
947 pr_err("BUG! (btres)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 fh->resources &= ~bits;
950 btv->resources &= ~bits;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300951
952 bits = btv->resources;
953
954 if (0 == (bits & VIDEO_RESOURCES))
955 disclaim_video_lines(btv);
956
957 if (0 == (bits & VBI_RESOURCES))
958 disclaim_vbi_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961/* ----------------------------------------------------------------------- */
962/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
963
964/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
965 PLL_X = Reference pre-divider (0=1, 1=2)
966 PLL_C = Post divider (0=6, 1=4)
967 PLL_I = Integer input
968 PLL_F = Fractional input
969
970 F_input = 28.636363 MHz:
971 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
972*/
973
974static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
975{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800976 unsigned char fl, fh, fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800978 /* prevent overflows */
979 fin/=4;
980 fout/=4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800982 fout*=12;
983 fi=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800985 fout=(fout%fin)*256;
986 fh=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800988 fout=(fout%fin)*256;
989 fl=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800991 btwrite(fl, BT848_PLL_F_LO);
992 btwrite(fh, BT848_PLL_F_HI);
993 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
996static void set_pll(struct bttv *btv)
997{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800998 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001000 if (!btv->pll.pll_crystal)
1001 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
Joe Perches8af443e2011-08-21 19:56:48 -03001004 dprintk("%d: PLL: no change required\n", btv->c.nr);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001005 return;
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001008 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1009 /* no PLL needed */
1010 if (btv->pll.pll_current == 0)
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001011 return;
Joe Perches8af443e2011-08-21 19:56:48 -03001012 if (bttv_verbose)
1013 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
1014 btv->c.nr, btv->pll.pll_ifreq);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001015 btwrite(0x00,BT848_TGCTRL);
1016 btwrite(0x00,BT848_PLL_XCI);
1017 btv->pll.pll_current = 0;
1018 return;
1019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Joe Perches8af443e2011-08-21 19:56:48 -03001021 if (bttv_verbose)
1022 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
1023 btv->c.nr,
1024 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1026
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001027 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /* Let other people run while the PLL stabilizes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 msleep(10);
1030
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001031 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 btwrite(0,BT848_DSTATUS);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001033 } else {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001034 btwrite(0x08,BT848_TGCTRL);
1035 btv->pll.pll_current = btv->pll.pll_ofreq;
Joe Perches8af443e2011-08-21 19:56:48 -03001036 if (bttv_verbose)
1037 pr_info("PLL set ok\n");
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001038 return;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001039 }
1040 }
1041 btv->pll.pll_current = -1;
Joe Perches8af443e2011-08-21 19:56:48 -03001042 if (bttv_verbose)
1043 pr_info("Setting PLL failed\n");
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001044 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047/* used to switch between the bt848's analog/digital video capture modes */
1048static void bt848A_set_timing(struct bttv *btv)
1049{
1050 int i, len;
1051 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1052 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1053
Trent Piepho5221e212009-01-28 21:32:59 -03001054 if (btv->input == btv->dig) {
Joe Perches8af443e2011-08-21 19:56:48 -03001055 dprintk("%d: load digital timing table (table_idx=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 btv->c.nr,table_idx);
1057
1058 /* timing change...reset timing generator address */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001059 btwrite(0x00, BT848_TGCTRL);
1060 btwrite(0x02, BT848_TGCTRL);
1061 btwrite(0x00, BT848_TGCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 len=SRAM_Table[table_idx][0];
1064 for(i = 1; i <= len; i++)
1065 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1066 btv->pll.pll_ofreq = 27000000;
1067
1068 set_pll(btv);
1069 btwrite(0x11, BT848_TGCTRL);
1070 btwrite(0x41, BT848_DVSIF);
1071 } else {
1072 btv->pll.pll_ofreq = fsc;
1073 set_pll(btv);
1074 btwrite(0x0, BT848_DVSIF);
1075 }
1076}
1077
1078/* ----------------------------------------------------------------------- */
1079
1080static void bt848_bright(struct bttv *btv, int bright)
1081{
1082 int value;
1083
Joe Perches8af443e2011-08-21 19:56:48 -03001084 // printk("set bright: %d\n", bright); // DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 btv->bright = bright;
1086
1087 /* We want -128 to 127 we get 0-65535 */
1088 value = (bright >> 8) - 128;
1089 btwrite(value & 0xff, BT848_BRIGHT);
1090}
1091
1092static void bt848_hue(struct bttv *btv, int hue)
1093{
1094 int value;
1095
1096 btv->hue = hue;
1097
1098 /* -128 to 127 */
1099 value = (hue >> 8) - 128;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001100 btwrite(value & 0xff, BT848_HUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
1103static void bt848_contrast(struct bttv *btv, int cont)
1104{
1105 int value,hibit;
1106
1107 btv->contrast = cont;
1108
1109 /* 0-511 */
1110 value = (cont >> 7);
1111 hibit = (value >> 6) & 4;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001112 btwrite(value & 0xff, BT848_CONTRAST_LO);
1113 btaor(hibit, ~4, BT848_E_CONTROL);
1114 btaor(hibit, ~4, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
1117static void bt848_sat(struct bttv *btv, int color)
1118{
1119 int val_u,val_v,hibits;
1120
1121 btv->saturation = color;
1122
1123 /* 0-511 for the color */
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001124 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1125 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001126 hibits = (val_u >> 7) & 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 hibits |= (val_v >> 8) & 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001128 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1129 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1130 btaor(hibits, ~3, BT848_E_CONTROL);
1131 btaor(hibits, ~3, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
1134/* ----------------------------------------------------------------------- */
1135
1136static int
1137video_mux(struct bttv *btv, unsigned int input)
1138{
1139 int mux,mask2;
1140
1141 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1142 return -EINVAL;
1143
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001144 /* needed by RemoteVideo MX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1146 if (mask2)
1147 gpio_inout(mask2,mask2);
1148
1149 if (input == btv->svhs) {
1150 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1151 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1152 } else {
1153 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1154 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1155 }
Trent Piepho6f987002009-01-28 21:32:59 -03001156 mux = bttv_muxsel(btv, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 btaor(mux<<5, ~(3<<5), BT848_IFORM);
Joe Perches8af443e2011-08-21 19:56:48 -03001158 dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 /* card specific hook */
1161 if(bttv_tvcards[btv->c.type].muxsel_hook)
1162 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1163 return 0;
1164}
1165
1166static char *audio_modes[] = {
1167 "audio: tuner", "audio: radio", "audio: extern",
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001168 "audio: intern", "audio: mute"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169};
1170
1171static int
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001172audio_mux(struct bttv *btv, int input, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001174 int gpio_val, signal;
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001175 struct v4l2_control ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1178 bttv_tvcards[btv->c.type].gpiomask);
1179 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1180
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001181 btv->mute = mute;
1182 btv->audio = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001184 /* automute */
1185 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1186
1187 if (mute)
1188 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1189 else
1190 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001191
Trent Piepho72134a62009-01-28 21:32:59 -03001192 switch (btv->c.type) {
1193 case BTTV_BOARD_VOODOOTV_FM:
1194 case BTTV_BOARD_VOODOOTV_200:
1195 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1196 break;
1197
1198 default:
1199 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1200 }
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (bttv_gpio)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001203 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1204 if (in_interrupt())
1205 return 0;
1206
1207 ctrl.id = V4L2_CID_AUDIO_MUTE;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001208 ctrl.value = btv->mute;
Hans Verkuil859f0272009-03-28 08:29:00 -03001209 bttv_call_all(btv, core, s_ctrl, &ctrl);
1210 if (btv->sd_msp34xx) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001211 u32 in;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001212
1213 /* Note: the inputs tuner/radio/extern/intern are translated
1214 to msp routings. This assumes common behavior for all msp3400
1215 based TV cards. When this assumption fails, then the
1216 specific MSP routing must be added to the card table.
1217 For now this is sufficient. */
1218 switch (input) {
1219 case TVAUDIO_INPUT_RADIO:
Hans de Goede7025e522012-05-21 07:46:22 -03001220 /* Some boards need the msp do to the radio demod */
1221 if (btv->radio_uses_msp_demodulator) {
1222 in = MSP_INPUT_DEFAULT;
1223 break;
1224 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001225 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001226 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001227 break;
1228 case TVAUDIO_INPUT_EXTERN:
Hans Verkuil5325b422009-04-02 11:26:22 -03001229 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001230 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001231 break;
1232 case TVAUDIO_INPUT_INTERN:
1233 /* Yes, this is the same input as for RADIO. I doubt
1234 if this is ever used. The only board with an INTERN
1235 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1236 that was tested. My guess is that the whole INTERN
1237 input does not work. */
Hans Verkuil5325b422009-04-02 11:26:22 -03001238 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001239 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001240 break;
1241 case TVAUDIO_INPUT_TUNER:
1242 default:
Wade Berrier434b2522007-06-25 13:02:16 -03001243 /* This is the only card that uses TUNER2, and afaik,
1244 is the only difference between the VOODOOTV_FM
1245 and VOODOOTV_200 */
1246 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
Hans Verkuil5325b422009-04-02 11:26:22 -03001247 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
Wade Berrier434b2522007-06-25 13:02:16 -03001248 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1249 else
Hans Verkuil5325b422009-04-02 11:26:22 -03001250 in = MSP_INPUT_DEFAULT;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001251 break;
1252 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001253 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1254 in, MSP_OUTPUT_DEFAULT, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001255 }
Hans Verkuil859f0272009-03-28 08:29:00 -03001256 if (btv->sd_tvaudio) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001257 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1258 input, 0, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return 0;
1261}
1262
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001263static inline int
1264audio_mute(struct bttv *btv, int mute)
1265{
1266 return audio_mux(btv, btv->audio, mute);
1267}
1268
1269static inline int
1270audio_input(struct bttv *btv, int input)
1271{
1272 return audio_mux(btv, input, btv->mute);
1273}
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275static void
Michael Schimeke5bd0262007-01-18 16:17:39 -03001276bttv_crop_calc_limits(struct bttv_crop *c)
1277{
1278 /* Scale factor min. 1:1, max. 16:1. Min. image size
1279 48 x 32. Scaled width must be a multiple of 4. */
1280
1281 if (1) {
1282 /* For bug compatibility with VIDIOCGCAP and image
1283 size checks in earlier driver versions. */
1284 c->min_scaled_width = 48;
1285 c->min_scaled_height = 32;
1286 } else {
1287 c->min_scaled_width =
1288 (max(48, c->rect.width >> 4) + 3) & ~3;
1289 c->min_scaled_height =
1290 max(32, c->rect.height >> 4);
1291 }
1292
1293 c->max_scaled_width = c->rect.width & ~3;
1294 c->max_scaled_height = c->rect.height;
1295}
1296
1297static void
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001298bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
Michael Schimeke5bd0262007-01-18 16:17:39 -03001299{
1300 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1301 bttv_crop_calc_limits(c);
1302}
1303
1304/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305static int
1306set_tvnorm(struct bttv *btv, unsigned int norm)
1307{
1308 const struct bttv_tvnorm *tvnorm;
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001309 v4l2_std_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001311 BUG_ON(norm >= BTTV_TVNORMS);
1312 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 tvnorm = &bttv_tvnorms[norm];
1315
Mike Isely2de26c02009-09-21 12:42:22 -03001316 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001317 sizeof (tvnorm->cropcap))) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001318 bttv_crop_reset(&btv->crop[0], norm);
1319 btv->crop[1] = btv->crop[0]; /* current = default */
1320
1321 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1322 btv->crop_start = tvnorm->cropcap.bounds.top
1323 + tvnorm->cropcap.bounds.height;
1324 }
1325 }
1326
1327 btv->tvnorm = norm;
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 btwrite(tvnorm->adelay, BT848_ADELAY);
1330 btwrite(tvnorm->bdelay, BT848_BDELAY);
1331 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1332 BT848_IFORM);
1333 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1334 btwrite(1, BT848_VBI_PACK_DEL);
1335 bt848A_set_timing(btv);
1336
1337 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -08001338 case BTTV_BOARD_VOODOOTV_FM:
Wade Berrier434b2522007-06-25 13:02:16 -03001339 case BTTV_BOARD_VOODOOTV_200:
Trent Piepho72134a62009-01-28 21:32:59 -03001340 bttv_tda9880_setnorm(btv, gpio_read());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001343 id = tvnorm->v4l2_id;
Hans Verkuilf41737e2009-04-01 03:52:39 -03001344 bttv_call_all(btv, core, s_std, id);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return 0;
1347}
1348
Michael Schimeke5bd0262007-01-18 16:17:39 -03001349/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350static void
Trent Piepho333408f2007-07-03 15:08:10 -03001351set_input(struct bttv *btv, unsigned int input, unsigned int norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 unsigned long flags;
1354
1355 btv->input = input;
1356 if (irq_iswitch) {
1357 spin_lock_irqsave(&btv->s_lock,flags);
1358 if (btv->curr.frame_irq) {
1359 /* active capture -> delayed input switch */
1360 btv->new_input = input;
1361 } else {
1362 video_mux(btv,input);
1363 }
1364 spin_unlock_irqrestore(&btv->s_lock,flags);
1365 } else {
1366 video_mux(btv,input);
1367 }
Trent Piephoabb03622009-01-28 21:32:59 -03001368 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1369 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
Trent Piepho333408f2007-07-03 15:08:10 -03001370 set_tvnorm(btv, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
1373static void init_irqreg(struct bttv *btv)
1374{
1375 /* clear status */
1376 btwrite(0xfffffUL, BT848_INT_STAT);
1377
1378 if (bttv_tvcards[btv->c.type].no_video) {
1379 /* i2c only */
1380 btwrite(BT848_INT_I2CDONE,
1381 BT848_INT_MASK);
1382 } else {
1383 /* full video */
1384 btwrite((btv->triton1) |
1385 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1386 BT848_INT_SCERR |
1387 (fdsr ? BT848_INT_FDSR : 0) |
Jean Delvareeb1b27b2008-08-30 10:18:21 -03001388 BT848_INT_RISCI | BT848_INT_OCERR |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1390 BT848_INT_I2CDONE,
1391 BT848_INT_MASK);
1392 }
1393}
1394
1395static void init_bt848(struct bttv *btv)
1396{
1397 int val;
1398
1399 if (bttv_tvcards[btv->c.type].no_video) {
1400 /* very basic init only */
1401 init_irqreg(btv);
1402 return;
1403 }
1404
1405 btwrite(0x00, BT848_CAP_CTL);
1406 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1407 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1408
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001409 /* set planar and packed mode trigger points and */
1410 /* set rising edge of inverted GPINTR pin as irq trigger */
1411 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1412 BT848_GPIO_DMA_CTL_PLTP1_16|
1413 BT848_GPIO_DMA_CTL_PLTP23_16|
1414 BT848_GPIO_DMA_CTL_GPINTC|
1415 BT848_GPIO_DMA_CTL_GPINTI,
1416 BT848_GPIO_DMA_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001419 btwrite(val, BT848_E_SCLOOP);
1420 btwrite(val, BT848_O_SCLOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001422 btwrite(0x20, BT848_E_VSCALE_HI);
1423 btwrite(0x20, BT848_O_VSCALE_HI);
1424 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 BT848_ADC);
1426
1427 btwrite(whitecrush_upper, BT848_WC_UP);
1428 btwrite(whitecrush_lower, BT848_WC_DOWN);
1429
1430 if (btv->opt_lumafilter) {
1431 btwrite(0, BT848_E_CONTROL);
1432 btwrite(0, BT848_O_CONTROL);
1433 } else {
1434 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1435 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1436 }
1437
1438 bt848_bright(btv, btv->bright);
1439 bt848_hue(btv, btv->hue);
1440 bt848_contrast(btv, btv->contrast);
1441 bt848_sat(btv, btv->saturation);
1442
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001443 /* interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 init_irqreg(btv);
1445}
1446
1447static void bttv_reinit_bt848(struct bttv *btv)
1448{
1449 unsigned long flags;
1450
1451 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03001452 pr_info("%d: reset, reinitialize\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 spin_lock_irqsave(&btv->s_lock,flags);
1454 btv->errors=0;
1455 bttv_set_dma(btv,0);
1456 spin_unlock_irqrestore(&btv->s_lock,flags);
1457
1458 init_bt848(btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001459 btv->pll.pll_current = -1;
Trent Piepho333408f2007-07-03 15:08:10 -03001460 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001463static int bttv_g_ctrl(struct file *file, void *priv,
1464 struct v4l2_control *c)
1465{
1466 struct bttv_fh *fh = priv;
1467 struct bttv *btv = fh->btv;
1468
1469 switch (c->id) {
1470 case V4L2_CID_BRIGHTNESS:
1471 c->value = btv->bright;
1472 break;
1473 case V4L2_CID_HUE:
1474 c->value = btv->hue;
1475 break;
1476 case V4L2_CID_CONTRAST:
1477 c->value = btv->contrast;
1478 break;
1479 case V4L2_CID_SATURATION:
1480 c->value = btv->saturation;
1481 break;
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -03001482 case V4L2_CID_COLOR_KILLER:
1483 c->value = btv->opt_color_killer;
1484 break;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001485
1486 case V4L2_CID_AUDIO_MUTE:
1487 case V4L2_CID_AUDIO_VOLUME:
1488 case V4L2_CID_AUDIO_BALANCE:
1489 case V4L2_CID_AUDIO_BASS:
1490 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil859f0272009-03-28 08:29:00 -03001491 bttv_call_all(btv, core, g_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001492 break;
1493
1494 case V4L2_CID_PRIVATE_CHROMA_AGC:
1495 c->value = btv->opt_chroma_agc;
1496 break;
1497 case V4L2_CID_PRIVATE_COMBFILTER:
1498 c->value = btv->opt_combfilter;
1499 break;
1500 case V4L2_CID_PRIVATE_LUMAFILTER:
1501 c->value = btv->opt_lumafilter;
1502 break;
1503 case V4L2_CID_PRIVATE_AUTOMUTE:
1504 c->value = btv->opt_automute;
1505 break;
1506 case V4L2_CID_PRIVATE_AGC_CRUSH:
1507 c->value = btv->opt_adc_crush;
1508 break;
1509 case V4L2_CID_PRIVATE_VCR_HACK:
1510 c->value = btv->opt_vcr_hack;
1511 break;
1512 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1513 c->value = btv->opt_whitecrush_upper;
1514 break;
1515 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1516 c->value = btv->opt_whitecrush_lower;
1517 break;
1518 case V4L2_CID_PRIVATE_UV_RATIO:
1519 c->value = btv->opt_uv_ratio;
1520 break;
1521 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1522 c->value = btv->opt_full_luma_range;
1523 break;
1524 case V4L2_CID_PRIVATE_CORING:
1525 c->value = btv->opt_coring;
1526 break;
1527 default:
1528 return -EINVAL;
1529 }
1530 return 0;
1531}
1532
1533static int bttv_s_ctrl(struct file *file, void *f,
1534 struct v4l2_control *c)
1535{
1536 int err;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001537 struct bttv_fh *fh = f;
1538 struct bttv *btv = fh->btv;
1539
Hans Verkuilffb48772010-05-01 08:03:24 -03001540 err = v4l2_prio_check(&btv->prio, fh->prio);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001541 if (0 != err)
1542 return err;
1543
1544 switch (c->id) {
1545 case V4L2_CID_BRIGHTNESS:
1546 bt848_bright(btv, c->value);
1547 break;
1548 case V4L2_CID_HUE:
1549 bt848_hue(btv, c->value);
1550 break;
1551 case V4L2_CID_CONTRAST:
1552 bt848_contrast(btv, c->value);
1553 break;
1554 case V4L2_CID_SATURATION:
1555 bt848_sat(btv, c->value);
1556 break;
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -03001557 case V4L2_CID_COLOR_KILLER:
1558 btv->opt_color_killer = c->value;
1559 if (btv->opt_color_killer) {
1560 btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1561 btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1562 } else {
1563 btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1564 btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1565 }
1566 break;
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001567 case V4L2_CID_AUDIO_MUTE:
1568 audio_mute(btv, c->value);
1569 /* fall through */
1570 case V4L2_CID_AUDIO_VOLUME:
1571 if (btv->volume_gpio)
1572 btv->volume_gpio(btv, c->value);
1573
Hans Verkuil859f0272009-03-28 08:29:00 -03001574 bttv_call_all(btv, core, s_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001575 break;
1576 case V4L2_CID_AUDIO_BALANCE:
1577 case V4L2_CID_AUDIO_BASS:
1578 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil859f0272009-03-28 08:29:00 -03001579 bttv_call_all(btv, core, s_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001580 break;
1581
1582 case V4L2_CID_PRIVATE_CHROMA_AGC:
1583 btv->opt_chroma_agc = c->value;
Guilherme Herrmann Destefani34e59a72012-09-25 18:10:52 -03001584 if (btv->opt_chroma_agc) {
1585 btor(BT848_SCLOOP_CAGC, BT848_E_SCLOOP);
1586 btor(BT848_SCLOOP_CAGC, BT848_O_SCLOOP);
1587 } else {
1588 btand(~BT848_SCLOOP_CAGC, BT848_E_SCLOOP);
1589 btand(~BT848_SCLOOP_CAGC, BT848_O_SCLOOP);
1590 }
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001591 break;
1592 case V4L2_CID_PRIVATE_COMBFILTER:
1593 btv->opt_combfilter = c->value;
1594 break;
1595 case V4L2_CID_PRIVATE_LUMAFILTER:
1596 btv->opt_lumafilter = c->value;
1597 if (btv->opt_lumafilter) {
1598 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1599 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1600 } else {
1601 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1602 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1603 }
1604 break;
1605 case V4L2_CID_PRIVATE_AUTOMUTE:
1606 btv->opt_automute = c->value;
1607 break;
1608 case V4L2_CID_PRIVATE_AGC_CRUSH:
1609 btv->opt_adc_crush = c->value;
1610 btwrite(BT848_ADC_RESERVED |
1611 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1612 BT848_ADC);
1613 break;
1614 case V4L2_CID_PRIVATE_VCR_HACK:
1615 btv->opt_vcr_hack = c->value;
1616 break;
1617 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1618 btv->opt_whitecrush_upper = c->value;
1619 btwrite(c->value, BT848_WC_UP);
1620 break;
1621 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1622 btv->opt_whitecrush_lower = c->value;
1623 btwrite(c->value, BT848_WC_DOWN);
1624 break;
1625 case V4L2_CID_PRIVATE_UV_RATIO:
1626 btv->opt_uv_ratio = c->value;
1627 bt848_sat(btv, btv->saturation);
1628 break;
1629 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1630 btv->opt_full_luma_range = c->value;
1631 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1632 break;
1633 case V4L2_CID_PRIVATE_CORING:
1634 btv->opt_coring = c->value;
1635 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1636 break;
1637 default:
1638 return -EINVAL;
1639 }
1640 return 0;
1641}
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643/* ----------------------------------------------------------------------- */
1644
1645void bttv_gpio_tracking(struct bttv *btv, char *comment)
1646{
1647 unsigned int outbits, data;
1648 outbits = btread(BT848_GPIO_OUT_EN);
1649 data = btread(BT848_GPIO_DATA);
Joe Perches8af443e2011-08-21 19:56:48 -03001650 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1651 btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654static void bttv_field_count(struct bttv *btv)
1655{
1656 int need_count = 0;
1657
1658 if (btv->users)
1659 need_count++;
1660
1661 if (need_count) {
1662 /* start field counter */
1663 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1664 } else {
1665 /* stop field counter */
1666 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1667 btv->field_count = 0;
1668 }
1669}
1670
1671static const struct bttv_format*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672format_by_fourcc(int fourcc)
1673{
1674 unsigned int i;
1675
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001676 for (i = 0; i < FORMATS; i++) {
1677 if (-1 == formats[i].fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 continue;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001679 if (formats[i].fourcc == fourcc)
1680 return formats+i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682 return NULL;
1683}
1684
1685/* ----------------------------------------------------------------------- */
1686/* misc helpers */
1687
1688static int
1689bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1690 struct bttv_buffer *new)
1691{
1692 struct bttv_buffer *old;
1693 unsigned long flags;
1694 int retval = 0;
1695
Joe Perches8af443e2011-08-21 19:56:48 -03001696 dprintk("switch_overlay: enter [new=%p]\n", new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (new)
Brandon Philips0fc06862007-11-06 20:02:36 -03001698 new->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 spin_lock_irqsave(&btv->s_lock,flags);
1700 old = btv->screen;
1701 btv->screen = new;
1702 btv->loop_irq |= 1;
1703 bttv_set_dma(btv, 0x03);
1704 spin_unlock_irqrestore(&btv->s_lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (NULL != old) {
Joe Perches8af443e2011-08-21 19:56:48 -03001706 dprintk("switch_overlay: old=%p state is %d\n",
1707 old, old->vb.state);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001708 bttv_dma_free(&fh->cap,btv, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 kfree(old);
1710 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03001711 if (NULL == new)
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03001712 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 dprintk("switch_overlay: done\n");
1714 return retval;
1715}
1716
1717/* ----------------------------------------------------------------------- */
1718/* video4linux (1) interface */
1719
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001720static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1721 struct bttv_buffer *buf,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001722 const struct bttv_format *fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 unsigned int width, unsigned int height,
1724 enum v4l2_field field)
1725{
Michael Schimeke5bd0262007-01-18 16:17:39 -03001726 struct bttv_fh *fh = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int redo_dma_risc = 0;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001728 struct bttv_crop c;
1729 int norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 int rc;
1731
1732 /* check settings */
1733 if (NULL == fmt)
1734 return -EINVAL;
1735 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1736 width = RAW_BPL;
1737 height = RAW_LINES*2;
1738 if (width*height > buf->vb.bsize)
1739 return -EINVAL;
1740 buf->vb.size = buf->vb.bsize;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001741
1742 /* Make sure tvnorm and vbi_end remain consistent
1743 until we're done. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03001744
1745 norm = btv->tvnorm;
1746
1747 /* In this mode capturing always starts at defrect.top
1748 (default VDELAY), ignoring cropping parameters. */
1749 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001751 }
1752
Michael Schimeke5bd0262007-01-18 16:17:39 -03001753 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1754 } else {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001755 norm = btv->tvnorm;
1756 c = btv->crop[!!fh->do_crop];
1757
Michael Schimeke5bd0262007-01-18 16:17:39 -03001758 if (width < c.min_scaled_width ||
1759 width > c.max_scaled_width ||
1760 height < c.min_scaled_height)
1761 return -EINVAL;
1762
1763 switch (field) {
1764 case V4L2_FIELD_TOP:
1765 case V4L2_FIELD_BOTTOM:
1766 case V4L2_FIELD_ALTERNATE:
1767 /* btv->crop counts frame lines. Max. scale
1768 factor is 16:1 for frames, 8:1 for fields. */
1769 if (height * 2 > c.max_scaled_height)
1770 return -EINVAL;
1771 break;
1772
1773 default:
1774 if (height > c.max_scaled_height)
1775 return -EINVAL;
1776 break;
1777 }
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 buf->vb.size = (width * height * fmt->depth) >> 3;
1780 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1781 return -EINVAL;
1782 }
1783
1784 /* alloc + fill struct bttv_buffer (if changed) */
1785 if (buf->vb.width != width || buf->vb.height != height ||
1786 buf->vb.field != field ||
Michael Schimeke5bd0262007-01-18 16:17:39 -03001787 buf->tvnorm != norm || buf->fmt != fmt ||
1788 buf->crop.top != c.rect.top ||
1789 buf->crop.left != c.rect.left ||
1790 buf->crop.width != c.rect.width ||
1791 buf->crop.height != c.rect.height) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 buf->vb.width = width;
1793 buf->vb.height = height;
1794 buf->vb.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001795 buf->tvnorm = norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 buf->fmt = fmt;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001797 buf->crop = c.rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 redo_dma_risc = 1;
1799 }
1800
1801 /* alloc risc memory */
Brandon Philips0fc06862007-11-06 20:02:36 -03001802 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 redo_dma_risc = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001804 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 goto fail;
1806 }
1807
1808 if (redo_dma_risc)
1809 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1810 goto fail;
1811
Brandon Philips0fc06862007-11-06 20:02:36 -03001812 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return 0;
1814
1815 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001816 bttv_dma_free(q,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return rc;
1818}
1819
1820static int
1821buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1822{
1823 struct bttv_fh *fh = q->priv_data;
1824
1825 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1826 if (0 == *count)
1827 *count = gbuffers;
Andreas Bombedab7e312010-03-21 16:02:45 -03001828 if (*size * *count > gbuffers * gbufsize)
1829 *count = (gbuffers * gbufsize) / *size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return 0;
1831}
1832
1833static int
1834buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1835 enum v4l2_field field)
1836{
1837 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1838 struct bttv_fh *fh = q->priv_data;
1839
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001840 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 fh->width, fh->height, field);
1842}
1843
1844static void
1845buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1846{
1847 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1848 struct bttv_fh *fh = q->priv_data;
1849 struct bttv *btv = fh->btv;
1850
Brandon Philips0fc06862007-11-06 20:02:36 -03001851 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 list_add_tail(&buf->vb.queue,&btv->capture);
1853 if (!btv->curr.frame_irq) {
1854 btv->loop_irq |= 1;
1855 bttv_set_dma(btv, 0x03);
1856 }
1857}
1858
1859static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1860{
1861 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1862 struct bttv_fh *fh = q->priv_data;
1863
Michael Schimekfeaba7a2007-01-26 08:30:05 -03001864 bttv_dma_free(q,fh->btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
1867static struct videobuf_queue_ops bttv_video_qops = {
1868 .buf_setup = buffer_setup,
1869 .buf_prepare = buffer_prepare,
1870 .buf_queue = buffer_queue,
1871 .buf_release = buffer_release,
1872};
1873
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001874static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001876 struct bttv_fh *fh = priv;
1877 struct bttv *btv = fh->btv;
1878 unsigned int i;
1879 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Hans Verkuilffb48772010-05-01 08:03:24 -03001881 err = v4l2_prio_check(&btv->prio, fh->prio);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001882 if (err)
1883 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001885 for (i = 0; i < BTTV_TVNORMS; i++)
1886 if (*id & bttv_tvnorms[i].v4l2_id)
1887 break;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001888 if (i == BTTV_TVNORMS) {
1889 err = -EINVAL;
1890 goto err;
1891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001893 set_tvnorm(btv, i);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001894
1895err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001897 return err;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001898}
1899
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001900static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001901{
1902 struct bttv_fh *fh = f;
1903 struct bttv *btv = fh->btv;
1904
1905 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1906 *id = V4L2_STD_625_50;
1907 else
1908 *id = V4L2_STD_525_60;
1909 return 0;
1910}
1911
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001912static int bttv_enum_input(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001913 struct v4l2_input *i)
1914{
1915 struct bttv_fh *fh = priv;
1916 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001917 int rc = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001918
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001919 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) {
1920 rc = -EINVAL;
1921 goto err;
1922 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001923
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001924 i->type = V4L2_INPUT_TYPE_CAMERA;
1925 i->audioset = 1;
1926
Trent Piephoabb03622009-01-28 21:32:59 -03001927 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001928 sprintf(i->name, "Television");
1929 i->type = V4L2_INPUT_TYPE_TUNER;
1930 i->tuner = 0;
1931 } else if (i->index == btv->svhs) {
1932 sprintf(i->name, "S-Video");
1933 } else {
1934 sprintf(i->name, "Composite%d", i->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 }
1936
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001937 if (i->index == btv->input) {
1938 __u32 dstatus = btread(BT848_DSTATUS);
1939 if (0 == (dstatus & BT848_DSTATUS_PRES))
1940 i->status |= V4L2_IN_ST_NO_SIGNAL;
1941 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1942 i->status |= V4L2_IN_ST_NO_H_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001945 i->std = BTTV_NORMS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001947err:
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001948
1949 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001950}
Nickolay V. Shmyrev4b9b9362006-08-25 16:53:04 -03001951
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001952static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001953{
1954 struct bttv_fh *fh = priv;
1955 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001957 *i = btv->input;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001958
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001959 return 0;
1960}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001962static int bttv_s_input(struct file *file, void *priv, unsigned int i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001963{
1964 struct bttv_fh *fh = priv;
1965 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001967 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);
1970 if (unlikely(err))
1971 goto err;
1972
1973 if (i > bttv_tvcards[btv->c.type].video_inputs) {
1974 err = -EINVAL;
1975 goto err;
1976 }
1977
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001978 set_input(btv, i, btv->tvnorm);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001979
1980err:
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001981 return 0;
1982}
1983
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001984static int bttv_s_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001985 struct v4l2_tuner *t)
1986{
1987 struct bttv_fh *fh = priv;
1988 struct bttv *btv = fh->btv;
1989 int err;
1990
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001991 if (unlikely(0 != t->index))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001992 return -EINVAL;
1993
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001994 if (unlikely(btv->tuner_type == TUNER_ABSENT)) {
1995 err = -EINVAL;
1996 goto err;
1997 }
1998
1999 err = v4l2_prio_check(&btv->prio, fh->prio);
2000 if (unlikely(err))
2001 goto err;
2002
Hans Verkuil859f0272009-03-28 08:29:00 -03002003 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002004
2005 if (btv->audio_mode_gpio)
2006 btv->audio_mode_gpio(btv, t, 1);
2007
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002008err:
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002009
2010 return 0;
2011}
2012
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002013static int bttv_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002014 struct v4l2_frequency *f)
2015{
2016 struct bttv_fh *fh = priv;
2017 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002018
Robert Fitzsimons1b069012008-04-01 11:41:54 -03002019 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002020 f->frequency = btv->freq;
2021
2022 return 0;
2023}
2024
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002025static int bttv_s_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002026 struct v4l2_frequency *f)
2027{
2028 struct bttv_fh *fh = priv;
2029 struct bttv *btv = fh->btv;
2030 int err;
2031
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002032 if (unlikely(f->tuner != 0))
2033 return -EINVAL;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002034
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002035 err = v4l2_prio_check(&btv->prio, fh->prio);
2036 if (unlikely(err))
2037 goto err;
2038
2039 if (unlikely(f->type != (btv->radio_user
2040 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV))) {
2041 err = -EINVAL;
2042 goto err;
2043 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002044 btv->freq = f->frequency;
Hans Verkuil859f0272009-03-28 08:29:00 -03002045 bttv_call_all(btv, tuner, s_frequency, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002046 if (btv->has_matchbox && btv->radio_user)
2047 tea5757_set_freq(btv, btv->freq);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002048err:
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002049
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002050 return 0;
2051}
2052
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002053static int bttv_log_status(struct file *file, void *f)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002054{
2055 struct bttv_fh *fh = f;
2056 struct bttv *btv = fh->btv;
2057
Hans Verkuil859f0272009-03-28 08:29:00 -03002058 bttv_call_all(btv, core, log_status);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002059 return 0;
2060}
2061
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002062#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002063static int bttv_g_register(struct file *file, void *f,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002064 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002065{
2066 struct bttv_fh *fh = f;
2067 struct bttv *btv = fh->btv;
2068
2069 if (!capable(CAP_SYS_ADMIN))
2070 return -EPERM;
2071
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002072 if (!v4l2_chip_match_host(&reg->match))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002073 return -EINVAL;
2074
2075 /* bt848 has a 12-bit register space */
2076 reg->reg &= 0xfff;
2077 reg->val = btread(reg->reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002078 reg->size = 1;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002079
2080 return 0;
2081}
2082
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002083static int bttv_s_register(struct file *file, void *f,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002084 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002085{
2086 struct bttv_fh *fh = f;
2087 struct bttv *btv = fh->btv;
2088
2089 if (!capable(CAP_SYS_ADMIN))
2090 return -EPERM;
2091
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002092 if (!v4l2_chip_match_host(&reg->match))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002093 return -EINVAL;
2094
2095 /* bt848 has a 12-bit register space */
2096 reg->reg &= 0xfff;
2097 btwrite(reg->val, reg->reg);
2098
2099 return 0;
2100}
2101#endif
2102
Michael Schimeke5bd0262007-01-18 16:17:39 -03002103/* Given cropping boundaries b and the scaled width and height of a
2104 single field or frame, which must not exceed hardware limits, this
2105 function adjusts the cropping parameters c. */
2106static void
2107bttv_crop_adjust (struct bttv_crop * c,
2108 const struct v4l2_rect * b,
2109 __s32 width,
2110 __s32 height,
2111 enum v4l2_field field)
2112{
2113 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2114 __s32 max_left;
2115 __s32 max_top;
2116
2117 if (width < c->min_scaled_width) {
2118 /* Max. hor. scale factor 16:1. */
2119 c->rect.width = width * 16;
2120 } else if (width > c->max_scaled_width) {
2121 /* Min. hor. scale factor 1:1. */
2122 c->rect.width = width;
2123
2124 max_left = b->left + b->width - width;
2125 max_left = min(max_left, (__s32) MAX_HDELAY);
2126 if (c->rect.left > max_left)
2127 c->rect.left = max_left;
2128 }
2129
2130 if (height < c->min_scaled_height) {
2131 /* Max. vert. scale factor 16:1, single fields 8:1. */
2132 c->rect.height = height * 16;
2133 } else if (frame_height > c->max_scaled_height) {
2134 /* Min. vert. scale factor 1:1.
2135 Top and height count field lines times two. */
2136 c->rect.height = (frame_height + 1) & ~1;
2137
2138 max_top = b->top + b->height - c->rect.height;
2139 if (c->rect.top > max_top)
2140 c->rect.top = max_top;
2141 }
2142
2143 bttv_crop_calc_limits(c);
2144}
2145
2146/* Returns an error if scaling to a frame or single field with the given
2147 width and height is not possible with the current cropping parameters
2148 and width aligned according to width_mask. If adjust_size is TRUE the
2149 function may adjust the width and/or height instead, rounding width
2150 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2151 also adjust the current cropping parameters to get closer to the
2152 desired image size. */
2153static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002154limit_scaled_size_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002155 __s32 * width,
2156 __s32 * height,
2157 enum v4l2_field field,
2158 unsigned int width_mask,
2159 unsigned int width_bias,
2160 int adjust_size,
2161 int adjust_crop)
2162{
2163 struct bttv *btv = fh->btv;
2164 const struct v4l2_rect *b;
2165 struct bttv_crop *c;
2166 __s32 min_width;
2167 __s32 min_height;
2168 __s32 max_width;
2169 __s32 max_height;
2170 int rc;
2171
2172 BUG_ON((int) width_mask >= 0 ||
2173 width_bias >= (unsigned int) -width_mask);
2174
2175 /* Make sure tvnorm, vbi_end and the current cropping parameters
2176 remain consistent until we're done. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03002177
2178 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2179
2180 /* Do crop - use current, don't - use default parameters. */
2181 c = &btv->crop[!!fh->do_crop];
2182
2183 if (fh->do_crop
2184 && adjust_size
2185 && adjust_crop
2186 && !locked_btres(btv, VIDEO_RESOURCES)) {
2187 min_width = 48;
2188 min_height = 32;
2189
2190 /* We cannot scale up. When the scaled image is larger
2191 than crop.rect we adjust the crop.rect as required
2192 by the V4L2 spec, hence cropcap.bounds are our limit. */
2193 max_width = min(b->width, (__s32) MAX_HACTIVE);
2194 max_height = b->height;
2195
2196 /* We cannot capture the same line as video and VBI data.
2197 Note btv->vbi_end is really a minimum, see
2198 bttv_vbi_try_fmt(). */
2199 if (btv->vbi_end > b->top) {
2200 max_height -= btv->vbi_end - b->top;
2201 rc = -EBUSY;
2202 if (min_height > max_height)
2203 goto fail;
2204 }
2205 } else {
2206 rc = -EBUSY;
2207 if (btv->vbi_end > c->rect.top)
2208 goto fail;
2209
2210 min_width = c->min_scaled_width;
2211 min_height = c->min_scaled_height;
2212 max_width = c->max_scaled_width;
2213 max_height = c->max_scaled_height;
2214
2215 adjust_crop = 0;
2216 }
2217
2218 min_width = (min_width - width_mask - 1) & width_mask;
2219 max_width = max_width & width_mask;
2220
2221 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2222 min_height = min_height;
2223 /* Min. scale factor is 1:1. */
2224 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2225
2226 if (adjust_size) {
2227 *width = clamp(*width, min_width, max_width);
2228 *height = clamp(*height, min_height, max_height);
2229
2230 /* Round after clamping to avoid overflow. */
2231 *width = (*width + width_bias) & width_mask;
2232
2233 if (adjust_crop) {
2234 bttv_crop_adjust(c, b, *width, *height, field);
2235
2236 if (btv->vbi_end > c->rect.top) {
2237 /* Move the crop window out of the way. */
2238 c->rect.top = btv->vbi_end;
2239 }
2240 }
2241 } else {
2242 rc = -EINVAL;
2243 if (*width < min_width ||
2244 *height < min_height ||
2245 *width > max_width ||
2246 *height > max_height ||
2247 0 != (*width & ~width_mask))
2248 goto fail;
2249 }
2250
2251 rc = 0; /* success */
2252
2253 fail:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002254
2255 return rc;
2256}
2257
2258/* Returns an error if the given overlay window dimensions are not
2259 possible with the current cropping parameters. If adjust_size is
2260 TRUE the function may adjust the window width and/or height
2261 instead, however it always rounds the horizontal position and
2262 width as btcx_align() does. If adjust_crop is TRUE the function
2263 may also adjust the current cropping parameters to get closer
2264 to the desired window size. */
2265static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002266verify_window_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002267 struct v4l2_window * win,
2268 int adjust_size,
2269 int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002272 unsigned int width_mask;
2273 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 if (win->w.width < 48 || win->w.height < 32)
2276 return -EINVAL;
2277 if (win->clipcount > 2048)
2278 return -EINVAL;
2279
2280 field = win->field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 if (V4L2_FIELD_ANY == field) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002283 __s32 height2;
2284
2285 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2286 field = (win->w.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 ? V4L2_FIELD_INTERLACED
2288 : V4L2_FIELD_TOP;
2289 }
2290 switch (field) {
2291 case V4L2_FIELD_TOP:
2292 case V4L2_FIELD_BOTTOM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 case V4L2_FIELD_INTERLACED:
2294 break;
2295 default:
2296 return -EINVAL;
2297 }
2298
Michael Schimeke5bd0262007-01-18 16:17:39 -03002299 /* 4-byte alignment. */
2300 if (NULL == fh->ovfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002302 width_mask = ~0;
2303 switch (fh->ovfmt->depth) {
2304 case 8:
2305 case 24:
2306 width_mask = ~3;
2307 break;
2308 case 16:
2309 width_mask = ~1;
2310 break;
2311 case 32:
2312 break;
2313 default:
2314 BUG();
2315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Michael Schimeke5bd0262007-01-18 16:17:39 -03002317 win->w.width -= win->w.left & ~width_mask;
2318 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2319
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002320 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002321 field, width_mask,
2322 /* width_bias: round down */ 0,
2323 adjust_size, adjust_crop);
2324 if (0 != rc)
2325 return rc;
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 win->field = field;
2328 return 0;
2329}
2330
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002331static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 struct v4l2_window *win, int fixup)
2333{
2334 struct v4l2_clip *clips = NULL;
2335 int n,size,retval = 0;
2336
2337 if (NULL == fh->ovfmt)
2338 return -EINVAL;
2339 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2340 return -EINVAL;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002341 retval = verify_window_lock(fh, win,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002342 /* adjust_size */ fixup,
2343 /* adjust_crop */ fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (0 != retval)
2345 return retval;
2346
2347 /* copy clips -- luckily v4l1 + v4l2 are binary
2348 compatible here ...*/
2349 n = win->clipcount;
2350 size = sizeof(*clips)*(n+4);
2351 clips = kmalloc(size,GFP_KERNEL);
2352 if (NULL == clips)
2353 return -ENOMEM;
2354 if (n > 0) {
2355 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2356 kfree(clips);
2357 return -EFAULT;
2358 }
2359 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 /* clip against screen */
2362 if (NULL != btv->fbuf.base)
2363 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2364 &win->w, clips, n);
2365 btcx_sort_clips(clips,n);
2366
2367 /* 4-byte alignments */
2368 switch (fh->ovfmt->depth) {
2369 case 8:
2370 case 24:
2371 btcx_align(&win->w, clips, n, 3);
2372 break;
2373 case 16:
2374 btcx_align(&win->w, clips, n, 1);
2375 break;
2376 case 32:
2377 /* no alignment fixups needed */
2378 break;
2379 default:
2380 BUG();
2381 }
2382
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002383 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 fh->ov.clips = clips;
2385 fh->ov.nclips = n;
2386
2387 fh->ov.w = win->w;
2388 fh->ov.field = win->field;
2389 fh->ov.setup_ok = 1;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 btv->init.ov.w.width = win->w.width;
2392 btv->init.ov.w.height = win->w.height;
2393 btv->init.ov.field = win->field;
2394
2395 /* update overlay if needed */
2396 retval = 0;
2397 if (check_btres(fh, RESOURCE_OVERLAY)) {
2398 struct bttv_buffer *new;
2399
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002400 new = videobuf_sg_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03002401 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2403 retval = bttv_switch_overlay(btv,fh,new);
2404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 return retval;
2406}
2407
2408/* ----------------------------------------------------------------------- */
2409
2410static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2411{
2412 struct videobuf_queue* q = NULL;
2413
2414 switch (fh->type) {
2415 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2416 q = &fh->cap;
2417 break;
2418 case V4L2_BUF_TYPE_VBI_CAPTURE:
2419 q = &fh->vbi;
2420 break;
2421 default:
2422 BUG();
2423 }
2424 return q;
2425}
2426
2427static int bttv_resource(struct bttv_fh *fh)
2428{
2429 int res = 0;
2430
2431 switch (fh->type) {
2432 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002433 res = RESOURCE_VIDEO_STREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 break;
2435 case V4L2_BUF_TYPE_VBI_CAPTURE:
2436 res = RESOURCE_VBI;
2437 break;
2438 default:
2439 BUG();
2440 }
2441 return res;
2442}
2443
2444static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2445{
2446 struct videobuf_queue *q = bttv_queue(fh);
2447 int res = bttv_resource(fh);
2448
2449 if (check_btres(fh,res))
2450 return -EBUSY;
2451 if (videobuf_queue_is_busy(q))
2452 return -EBUSY;
2453 fh->type = type;
2454 return 0;
2455}
2456
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002457static void
2458pix_format_set_size (struct v4l2_pix_format * f,
2459 const struct bttv_format * fmt,
2460 unsigned int width,
2461 unsigned int height)
2462{
2463 f->width = width;
2464 f->height = height;
2465
2466 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2467 f->bytesperline = width; /* Y plane */
2468 f->sizeimage = (width * height * fmt->depth) >> 3;
2469 } else {
2470 f->bytesperline = (width * fmt->depth) >> 3;
2471 f->sizeimage = height * f->bytesperline;
2472 }
2473}
2474
Hans Verkuil78b526a2008-05-28 12:16:41 -03002475static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002476 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002478 struct bttv_fh *fh = priv;
2479
2480 pix_format_set_size(&f->fmt.pix, fh->fmt,
2481 fh->width, fh->height);
2482 f->fmt.pix.field = fh->cap.field;
2483 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2484
2485 return 0;
2486}
2487
Hans Verkuil78b526a2008-05-28 12:16:41 -03002488static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002489 struct v4l2_format *f)
2490{
2491 struct bttv_fh *fh = priv;
2492
2493 f->fmt.win.w = fh->ov.w;
2494 f->fmt.win.field = fh->ov.field;
2495
2496 return 0;
2497}
2498
Hans Verkuil78b526a2008-05-28 12:16:41 -03002499static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002500 struct v4l2_format *f)
2501{
2502 const struct bttv_format *fmt;
2503 struct bttv_fh *fh = priv;
2504 struct bttv *btv = fh->btv;
2505 enum v4l2_field field;
2506 __s32 width, height;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002507 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002508
2509 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2510 if (NULL == fmt)
2511 return -EINVAL;
2512
2513 field = f->fmt.pix.field;
2514
2515 if (V4L2_FIELD_ANY == field) {
2516 __s32 height2;
2517
2518 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2519 field = (f->fmt.pix.height > height2)
2520 ? V4L2_FIELD_INTERLACED
2521 : V4L2_FIELD_BOTTOM;
2522 }
2523
2524 if (V4L2_FIELD_SEQ_BT == field)
2525 field = V4L2_FIELD_SEQ_TB;
2526
2527 switch (field) {
2528 case V4L2_FIELD_TOP:
2529 case V4L2_FIELD_BOTTOM:
2530 case V4L2_FIELD_ALTERNATE:
2531 case V4L2_FIELD_INTERLACED:
2532 break;
2533 case V4L2_FIELD_SEQ_TB:
2534 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2535 return -EINVAL;
2536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 default:
2538 return -EINVAL;
2539 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002540
2541 width = f->fmt.pix.width;
2542 height = f->fmt.pix.height;
2543
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002544 rc = limit_scaled_size_lock(fh, &width, &height, field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002545 /* width_mask: 4 pixels */ ~3,
2546 /* width_bias: nearest */ 2,
2547 /* adjust_size */ 1,
2548 /* adjust_crop */ 0);
2549 if (0 != rc)
2550 return rc;
2551
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002552 /* update data for the application */
2553 f->fmt.pix.field = field;
2554 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2555
2556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
Hans Verkuil78b526a2008-05-28 12:16:41 -03002559static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002560 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002562 struct bttv_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002564 return verify_window_lock(fh, &f->fmt.win,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002565 /* adjust_size */ 1,
2566 /* adjust_crop */ 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
Hans Verkuil78b526a2008-05-28 12:16:41 -03002569static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002570 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
2572 int retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002573 const struct bttv_format *fmt;
2574 struct bttv_fh *fh = priv;
2575 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002576 __s32 width, height;
2577 enum v4l2_field field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002579 retval = bttv_switch_type(fh, f->type);
2580 if (0 != retval)
2581 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Hans Verkuil78b526a2008-05-28 12:16:41 -03002583 retval = bttv_try_fmt_vid_cap(file, priv, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002584 if (0 != retval)
2585 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002587 width = f->fmt.pix.width;
2588 height = f->fmt.pix.height;
2589 field = f->fmt.pix.field;
2590
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002591 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002592 /* width_mask: 4 pixels */ ~3,
2593 /* width_bias: nearest */ 2,
2594 /* adjust_size */ 1,
2595 /* adjust_crop */ 1);
2596 if (0 != retval)
2597 return retval;
2598
2599 f->fmt.pix.field = field;
2600
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002601 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002603 /* update our state informations */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002604 fh->fmt = fmt;
2605 fh->cap.field = f->fmt.pix.field;
2606 fh->cap.last = V4L2_FIELD_NONE;
2607 fh->width = f->fmt.pix.width;
2608 fh->height = f->fmt.pix.height;
2609 btv->init.fmt = fmt;
2610 btv->init.width = f->fmt.pix.width;
2611 btv->init.height = f->fmt.pix.height;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002612
2613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
Hans Verkuil78b526a2008-05-28 12:16:41 -03002616static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002617 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002619 struct bttv_fh *fh = priv;
2620 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002622 if (no_overlay > 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03002623 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002624 return -EINVAL;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002625 }
Michael Krufky5e453dc2006-01-09 15:32:31 -02002626
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002627 return setup_window_lock(fh, btv, &f->fmt.win, 1);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002628}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002630static int bttv_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002631 struct v4l2_capability *cap)
2632{
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002633 struct video_device *vdev = video_devdata(file);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002634 struct bttv_fh *fh = priv;
2635 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002637 if (0 == v4l2)
2638 return -EINVAL;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002639
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002640 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2641 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2642 snprintf(cap->bus_info, sizeof(cap->bus_info),
2643 "PCI:%s", pci_name(btv->c.pci));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002644 cap->capabilities =
2645 V4L2_CAP_VIDEO_CAPTURE |
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002646 V4L2_CAP_READWRITE |
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002647 V4L2_CAP_STREAMING |
2648 V4L2_CAP_DEVICE_CAPS;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002649 if (no_overlay <= 0)
2650 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002651 if (btv->vbi_dev)
2652 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
2653 if (btv->radio_dev)
2654 cap->capabilities |= V4L2_CAP_RADIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002656 /*
2657 * No need to lock here: those vars are initialized during board
2658 * probe and remains untouched during the rest of the driver lifecycle
2659 */
2660 if (btv->has_saa6588)
2661 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
Trent Piephoabb03622009-01-28 21:32:59 -03002662 if (btv->tuner_type != TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002663 cap->capabilities |= V4L2_CAP_TUNER;
Hans Verkuil78dea1a2012-09-09 09:03:29 -03002664 if (vdev->vfl_type == VFL_TYPE_GRABBER)
2665 cap->device_caps = cap->capabilities &
2666 (V4L2_CAP_VIDEO_CAPTURE |
2667 V4L2_CAP_READWRITE |
2668 V4L2_CAP_STREAMING |
2669 V4L2_CAP_VIDEO_OVERLAY |
2670 V4L2_CAP_TUNER);
2671 else if (vdev->vfl_type == VFL_TYPE_VBI)
2672 cap->device_caps = cap->capabilities &
2673 (V4L2_CAP_VBI_CAPTURE |
2674 V4L2_CAP_READWRITE |
2675 V4L2_CAP_STREAMING |
2676 V4L2_CAP_TUNER);
2677 else {
2678 cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
2679 if (btv->has_saa6588)
2680 cap->device_caps |= V4L2_CAP_READWRITE |
2681 V4L2_CAP_RDS_CAPTURE;
2682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002684}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002686static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2687{
2688 int index = -1, i;
2689
2690 for (i = 0; i < FORMATS; i++) {
2691 if (formats[i].fourcc != -1)
2692 index++;
2693 if ((unsigned int)index == f->index)
2694 break;
2695 }
2696 if (FORMATS == i)
2697 return -EINVAL;
2698
2699 f->pixelformat = formats[i].fourcc;
2700 strlcpy(f->description, formats[i].name, sizeof(f->description));
2701
2702 return i;
2703}
2704
Hans Verkuil78b526a2008-05-28 12:16:41 -03002705static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002706 struct v4l2_fmtdesc *f)
2707{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002708 int rc = bttv_enum_fmt_cap_ovr(f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002709
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002710 if (rc < 0)
2711 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002712
2713 return 0;
2714}
2715
Hans Verkuil78b526a2008-05-28 12:16:41 -03002716static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002717 struct v4l2_fmtdesc *f)
2718{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002719 int rc;
2720
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002721 if (no_overlay > 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03002722 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002723 return -EINVAL;
2724 }
2725
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002726 rc = bttv_enum_fmt_cap_ovr(f);
2727
2728 if (rc < 0)
2729 return rc;
2730
2731 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002732 return -EINVAL;
2733
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002734 return 0;
2735}
2736
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002737static int bttv_g_fbuf(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002738 struct v4l2_framebuffer *fb)
2739{
2740 struct bttv_fh *fh = f;
2741 struct bttv *btv = fh->btv;
2742
2743 *fb = btv->fbuf;
2744 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2745 if (fh->ovfmt)
2746 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2747 return 0;
2748}
2749
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002750static int bttv_overlay(struct file *file, void *f, unsigned int on)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002751{
2752 struct bttv_fh *fh = f;
2753 struct bttv *btv = fh->btv;
2754 struct bttv_buffer *new;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002755 int retval = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002756
2757 if (on) {
2758 /* verify args */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002759 if (unlikely(!btv->fbuf.base)) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002760 return -EINVAL;
2761 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002762 if (unlikely(!fh->ov.setup_ok)) {
Joe Perches8af443e2011-08-21 19:56:48 -03002763 dprintk("%d: overlay: !setup_ok\n", btv->c.nr);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002764 retval = -EINVAL;
2765 }
2766 if (retval)
2767 return retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002768 }
2769
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002770 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002771 return -EBUSY;
2772
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002773 if (on) {
2774 fh->ov.tvnorm = btv->tvnorm;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002775 new = videobuf_sg_alloc(sizeof(*new));
Robert Fitzsimons7c018802008-02-13 16:38:11 -03002776 new->crop = btv->crop[!!fh->do_crop].rect;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002777 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2778 } else {
2779 new = NULL;
2780 }
2781
2782 /* switch over */
2783 retval = bttv_switch_overlay(btv, fh, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return retval;
2785}
2786
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002787static int bttv_s_fbuf(struct file *file, void *f,
Hans Verkuile6eb28c2012-09-04 10:26:45 -03002788 const struct v4l2_framebuffer *fb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002790 struct bttv_fh *fh = f;
2791 struct bttv *btv = fh->btv;
2792 const struct bttv_format *fmt;
2793 int retval;
2794
2795 if (!capable(CAP_SYS_ADMIN) &&
2796 !capable(CAP_SYS_RAWIO))
2797 return -EPERM;
2798
2799 /* check args */
2800 fmt = format_by_fourcc(fb->fmt.pixelformat);
2801 if (NULL == fmt)
2802 return -EINVAL;
2803 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2804 return -EINVAL;
2805
2806 retval = -EINVAL;
2807 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2808 __s32 width = fb->fmt.width;
2809 __s32 height = fb->fmt.height;
2810
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002811 retval = limit_scaled_size_lock(fh, &width, &height,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002812 V4L2_FIELD_INTERLACED,
2813 /* width_mask */ ~3,
2814 /* width_bias */ 2,
2815 /* adjust_size */ 0,
2816 /* adjust_crop */ 0);
2817 if (0 != retval)
2818 return retval;
2819 }
2820
2821 /* ok, accept it */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002822 btv->fbuf.base = fb->base;
2823 btv->fbuf.fmt.width = fb->fmt.width;
2824 btv->fbuf.fmt.height = fb->fmt.height;
2825 if (0 != fb->fmt.bytesperline)
2826 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2827 else
2828 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2829
2830 retval = 0;
2831 fh->ovfmt = fmt;
2832 btv->init.ovfmt = fmt;
2833 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2834 fh->ov.w.left = 0;
2835 fh->ov.w.top = 0;
2836 fh->ov.w.width = fb->fmt.width;
2837 fh->ov.w.height = fb->fmt.height;
2838 btv->init.ov.w.width = fb->fmt.width;
2839 btv->init.ov.w.height = fb->fmt.height;
2840 kfree(fh->ov.clips);
2841 fh->ov.clips = NULL;
2842 fh->ov.nclips = 0;
2843
2844 if (check_btres(fh, RESOURCE_OVERLAY)) {
2845 struct bttv_buffer *new;
2846
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002847 new = videobuf_sg_alloc(sizeof(*new));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002848 new->crop = btv->crop[!!fh->do_crop].rect;
2849 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2850 retval = bttv_switch_overlay(btv, fh, new);
2851 }
2852 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002853 return retval;
2854}
2855
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002856static int bttv_reqbufs(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002857 struct v4l2_requestbuffers *p)
2858{
2859 struct bttv_fh *fh = priv;
2860 return videobuf_reqbufs(bttv_queue(fh), p);
2861}
2862
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002863static int bttv_querybuf(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002864 struct v4l2_buffer *b)
2865{
2866 struct bttv_fh *fh = priv;
2867 return videobuf_querybuf(bttv_queue(fh), b);
2868}
2869
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002870static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002871{
2872 struct bttv_fh *fh = priv;
2873 struct bttv *btv = fh->btv;
2874 int res = bttv_resource(fh);
2875
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002876 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002877 return -EBUSY;
2878
2879 return videobuf_qbuf(bttv_queue(fh), b);
2880}
2881
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002882static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002883{
2884 struct bttv_fh *fh = priv;
2885 return videobuf_dqbuf(bttv_queue(fh), b,
2886 file->f_flags & O_NONBLOCK);
2887}
2888
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002889static int bttv_streamon(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002890 enum v4l2_buf_type type)
2891{
2892 struct bttv_fh *fh = priv;
2893 struct bttv *btv = fh->btv;
2894 int res = bttv_resource(fh);
2895
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002896 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002897 return -EBUSY;
2898 return videobuf_streamon(bttv_queue(fh));
2899}
2900
2901
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002902static int bttv_streamoff(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002903 enum v4l2_buf_type type)
2904{
2905 struct bttv_fh *fh = priv;
2906 struct bttv *btv = fh->btv;
2907 int retval;
2908 int res = bttv_resource(fh);
2909
2910
2911 retval = videobuf_streamoff(bttv_queue(fh));
2912 if (retval < 0)
2913 return retval;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002914 free_btres_lock(btv, fh, res);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002915 return 0;
2916}
2917
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002918static int bttv_queryctrl(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002919 struct v4l2_queryctrl *c)
2920{
2921 struct bttv_fh *fh = priv;
2922 struct bttv *btv = fh->btv;
2923 const struct v4l2_queryctrl *ctrl;
2924
2925 if ((c->id < V4L2_CID_BASE ||
2926 c->id >= V4L2_CID_LASTP1) &&
2927 (c->id < V4L2_CID_PRIVATE_BASE ||
2928 c->id >= V4L2_CID_PRIVATE_LASTP1))
2929 return -EINVAL;
2930
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002931 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2932 *c = no_ctl;
2933 else {
2934 ctrl = ctrl_by_id(c->id);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002935
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002936 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2937 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002938
2939 return 0;
2940}
2941
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002942static int bttv_g_parm(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002943 struct v4l2_streamparm *parm)
2944{
2945 struct bttv_fh *fh = f;
2946 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002947
Trent Piepho51f0b8d2009-03-04 01:21:02 -03002948 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2949 &parm->parm.capture.timeperframe);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002950
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002951 return 0;
2952}
2953
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002954static int bttv_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002955 struct v4l2_tuner *t)
2956{
2957 struct bttv_fh *fh = priv;
2958 struct bttv *btv = fh->btv;
2959
Trent Piephoabb03622009-01-28 21:32:59 -03002960 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002961 return -EINVAL;
2962 if (0 != t->index)
2963 return -EINVAL;
2964
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002965 t->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil859f0272009-03-28 08:29:00 -03002966 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002967 strcpy(t->name, "Television");
2968 t->capability = V4L2_TUNER_CAP_NORM;
2969 t->type = V4L2_TUNER_ANALOG_TV;
2970 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2971 t->signal = 0xffff;
2972
2973 if (btv->audio_mode_gpio)
2974 btv->audio_mode_gpio(btv, t, 0);
2975
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002976 return 0;
2977}
2978
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002979static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002980{
2981 struct bttv_fh *fh = f;
2982 struct bttv *btv = fh->btv;
2983
2984 *p = v4l2_prio_max(&btv->prio);
2985
2986 return 0;
2987}
2988
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002989static int bttv_s_priority(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002990 enum v4l2_priority prio)
2991{
2992 struct bttv_fh *fh = f;
2993 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002994 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002995
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002996 rc = v4l2_prio_change(&btv->prio, &fh->prio, prio);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002997
2998 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002999}
3000
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003001static int bttv_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003002 struct v4l2_cropcap *cap)
3003{
3004 struct bttv_fh *fh = priv;
3005 struct bttv *btv = fh->btv;
3006
3007 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3008 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3009 return -EINVAL;
3010
3011 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3012
3013 return 0;
3014}
3015
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003016static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003017{
3018 struct bttv_fh *fh = f;
3019 struct bttv *btv = fh->btv;
3020
3021 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3022 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3023 return -EINVAL;
3024
3025 /* No fh->do_crop = 1; because btv->crop[1] may be
3026 inconsistent with fh->width or fh->height and apps
3027 do not expect a change here. */
3028
3029 crop->c = btv->crop[!!fh->do_crop].rect;
3030
3031 return 0;
3032}
3033
Hans Verkuil4f9965942012-09-05 05:10:48 -03003034static int bttv_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003035{
3036 struct bttv_fh *fh = f;
3037 struct bttv *btv = fh->btv;
3038 const struct v4l2_rect *b;
3039 int retval;
3040 struct bttv_crop c;
3041 __s32 b_left;
3042 __s32 b_top;
3043 __s32 b_right;
3044 __s32 b_bottom;
3045
3046 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3047 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3048 return -EINVAL;
3049
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003050 /* Make sure tvnorm, vbi_end and the current cropping
3051 parameters remain consistent until we're done. Note
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003052 read() may change vbi_end in check_alloc_btres_lock(). */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003053 retval = v4l2_prio_check(&btv->prio, fh->prio);
3054 if (0 != retval) {
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003055 return retval;
3056 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003057
3058 retval = -EBUSY;
3059
3060 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003061 return retval;
3062 }
3063
3064 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3065
3066 b_left = b->left;
3067 b_right = b_left + b->width;
3068 b_bottom = b->top + b->height;
3069
3070 b_top = max(b->top, btv->vbi_end);
3071 if (b_top + 32 >= b_bottom) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003072 return retval;
3073 }
3074
3075 /* Min. scaled size 48 x 32. */
Hans Verkuil4f9965942012-09-05 05:10:48 -03003076 c.rect.left = clamp_t(s32, crop->c.left, b_left, b_right - 48);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003077 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3078
Hans Verkuil4f9965942012-09-05 05:10:48 -03003079 c.rect.width = clamp_t(s32, crop->c.width,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003080 48, b_right - c.rect.left);
3081
Hans Verkuil4f9965942012-09-05 05:10:48 -03003082 c.rect.top = clamp_t(s32, crop->c.top, b_top, b_bottom - 32);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003083 /* Top and height must be a multiple of two. */
3084 c.rect.top = (c.rect.top + 1) & ~1;
3085
Hans Verkuil4f9965942012-09-05 05:10:48 -03003086 c.rect.height = clamp_t(s32, crop->c.height,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003087 32, b_bottom - c.rect.top);
3088 c.rect.height = (c.rect.height + 1) & ~1;
3089
3090 bttv_crop_calc_limits(&c);
3091
3092 btv->crop[1] = c;
3093
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003094 fh->do_crop = 1;
3095
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003096 if (fh->width < c.min_scaled_width) {
3097 fh->width = c.min_scaled_width;
3098 btv->init.width = c.min_scaled_width;
3099 } else if (fh->width > c.max_scaled_width) {
3100 fh->width = c.max_scaled_width;
3101 btv->init.width = c.max_scaled_width;
3102 }
3103
3104 if (fh->height < c.min_scaled_height) {
3105 fh->height = c.min_scaled_height;
3106 btv->init.height = c.min_scaled_height;
3107 } else if (fh->height > c.max_scaled_height) {
3108 fh->height = c.max_scaled_height;
3109 btv->init.height = c.max_scaled_height;
3110 }
3111
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003112 return 0;
3113}
3114
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003115static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003116{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003117 if (unlikely(a->index))
3118 return -EINVAL;
3119
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003120 strcpy(a->name, "audio");
3121 return 0;
3122}
3123
Hans Verkuil0e8025b2012-09-04 11:59:31 -03003124static int bttv_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003125{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003126 if (unlikely(a->index))
3127 return -EINVAL;
3128
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
3132static ssize_t bttv_read(struct file *file, char __user *data,
3133 size_t count, loff_t *ppos)
3134{
3135 struct bttv_fh *fh = file->private_data;
3136 int retval = 0;
3137
3138 if (fh->btv->errors)
3139 bttv_reinit_bt848(fh->btv);
Joe Perches8af443e2011-08-21 19:56:48 -03003140 dprintk("%d: read count=%d type=%s\n",
3141 fh->btv->c.nr, (int)count, v4l2_type_names[fh->type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
3143 switch (fh->type) {
3144 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003145 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03003146 /* VIDEO_READ in use by another fh,
3147 or VIDEO_STREAM by any fh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 return -EBUSY;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3151 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003152 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 break;
3154 case V4L2_BUF_TYPE_VBI_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003155 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 return -EBUSY;
3157 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3158 file->f_flags & O_NONBLOCK);
3159 break;
3160 default:
3161 BUG();
3162 }
3163 return retval;
3164}
3165
3166static unsigned int bttv_poll(struct file *file, poll_table *wait)
3167{
3168 struct bttv_fh *fh = file->private_data;
3169 struct bttv_buffer *buf;
3170 enum v4l2_field field;
Figo.zhang9fd64182009-06-16 13:31:29 -03003171 unsigned int rc = POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003174 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return POLLERR;
3176 return videobuf_poll_stream(file, &fh->vbi, wait);
3177 }
3178
Michael Schimeke5bd0262007-01-18 16:17:39 -03003179 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 /* streaming capture */
3181 if (list_empty(&fh->cap.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -03003182 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3184 } else {
3185 /* read() capture */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 if (NULL == fh->cap.read_buf) {
3187 /* need to capture a new frame */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003188 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3189 goto err;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003190 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003191 if (NULL == fh->cap.read_buf)
3192 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3194 field = videobuf_next_field(&fh->cap);
3195 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -08003196 kfree (fh->cap.read_buf);
3197 fh->cap.read_buf = NULL;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003198 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
3200 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3201 fh->cap.read_off = 0;
3202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 buf = (struct bttv_buffer*)fh->cap.read_buf;
3204 }
3205
3206 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03003207 if (buf->vb.state == VIDEOBUF_DONE ||
3208 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -03003209 rc = POLLIN|POLLRDNORM;
3210 else
3211 rc = 0;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003212err:
Figo.zhang9fd64182009-06-16 13:31:29 -03003213 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214}
3215
Hans Verkuilbec43662008-12-30 06:58:20 -03003216static int bttv_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003218 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003219 struct bttv *btv = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 struct bttv_fh *fh;
3221 enum v4l2_buf_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Joe Perches8af443e2011-08-21 19:56:48 -03003223 dprintk("open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
Laurent Pinchart327ae592009-11-27 13:57:55 -03003225 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003226 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Laurent Pinchart327ae592009-11-27 13:57:55 -03003227 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003228 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3229 } else {
3230 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -03003232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Joe Perches8af443e2011-08-21 19:56:48 -03003234 dprintk("%d: open called (type=%s)\n",
3235 btv->c.nr, v4l2_type_names[type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
3237 /* allocate per filehandle data */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003238 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3239 if (unlikely(!fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return -ENOMEM;
3241 file->private_data = fh;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003242
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 *fh = btv->init;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 fh->type = type;
3246 fh->ov.setup_ok = 0;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003247
Hans Verkuilffb48772010-05-01 08:03:24 -03003248 v4l2_prio_open(&btv->prio, &fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003250 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3251 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3253 V4L2_FIELD_INTERLACED,
3254 sizeof(struct bttv_buffer),
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003255 fh, &btv->lock);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003256 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3257 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 V4L2_BUF_TYPE_VBI_CAPTURE,
3259 V4L2_FIELD_SEQ_TB,
3260 sizeof(struct bttv_buffer),
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003261 fh, &btv->lock);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03003262 set_tvnorm(btv,btv->tvnorm);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003263 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
3265 btv->users++;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003266
3267 /* The V4L2 spec requires one global set of cropping parameters
3268 which only change on request. These are stored in btv->crop[1].
3269 However for compatibility with V4L apps and cropping unaware
3270 V4L2 apps we now reset the cropping parameters as seen through
3271 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3272 will use btv->crop[0], the default cropping parameters for the
3273 current video standard, and VIDIOC_S_FMT will not implicitely
3274 change the cropping parameters until VIDIOC_S_CROP has been
3275 called. */
3276 fh->do_crop = !reset_crop; /* module parameter */
3277
3278 /* Likewise there should be one global set of VBI capture
3279 parameters, but for compatibility with V4L apps and earlier
3280 driver versions each fh has its own parameters. */
3281 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 bttv_field_count(btv);
3284 return 0;
3285}
3286
Hans Verkuilbec43662008-12-30 06:58:20 -03003287static int bttv_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
3289 struct bttv_fh *fh = file->private_data;
3290 struct bttv *btv = fh->btv;
3291
3292 /* turn off overlay */
3293 if (check_btres(fh, RESOURCE_OVERLAY))
3294 bttv_switch_overlay(btv,fh,NULL);
3295
3296 /* stop video capture */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003297 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 videobuf_streamoff(&fh->cap);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003299 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 }
3301 if (fh->cap.read_buf) {
3302 buffer_release(&fh->cap,fh->cap.read_buf);
3303 kfree(fh->cap.read_buf);
3304 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03003305 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003306 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
3309 /* stop vbi capture */
3310 if (check_btres(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03003311 videobuf_stop(&fh->vbi);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003312 free_btres_lock(btv,fh,RESOURCE_VBI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 }
3314
3315 /* free stuff */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 videobuf_mmap_free(&fh->cap);
3318 videobuf_mmap_free(&fh->vbi);
Hans Verkuilffb48772010-05-01 08:03:24 -03003319 v4l2_prio_close(&btv->prio, fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 file->private_data = NULL;
3321 kfree(fh);
3322
3323 btv->users--;
3324 bttv_field_count(btv);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003325
3326 if (!btv->users)
3327 audio_mute(btv, 1);
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return 0;
3330}
3331
3332static int
3333bttv_mmap(struct file *file, struct vm_area_struct *vma)
3334{
3335 struct bttv_fh *fh = file->private_data;
3336
Joe Perches8af443e2011-08-21 19:56:48 -03003337 dprintk("%d: mmap type=%s 0x%lx+%ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 fh->btv->c.nr, v4l2_type_names[fh->type],
3339 vma->vm_start, vma->vm_end - vma->vm_start);
3340 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3341}
3342
Hans Verkuilbec43662008-12-30 06:58:20 -03003343static const struct v4l2_file_operations bttv_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
Mauro Carvalho Chehab8979e9d2010-09-15 08:22:09 -03003345 .owner = THIS_MODULE,
3346 .open = bttv_open,
3347 .release = bttv_release,
3348 .unlocked_ioctl = video_ioctl2,
3349 .read = bttv_read,
3350 .mmap = bttv_mmap,
3351 .poll = bttv_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352};
3353
Hans Verkuila3998102008-07-21 02:57:38 -03003354static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003355 .vidioc_querycap = bttv_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003356 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3357 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3358 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3359 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3360 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3361 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3362 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3363 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003364 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3365 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3366 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003367 .vidioc_g_audio = bttv_g_audio,
3368 .vidioc_s_audio = bttv_s_audio,
3369 .vidioc_cropcap = bttv_cropcap,
3370 .vidioc_reqbufs = bttv_reqbufs,
3371 .vidioc_querybuf = bttv_querybuf,
3372 .vidioc_qbuf = bttv_qbuf,
3373 .vidioc_dqbuf = bttv_dqbuf,
3374 .vidioc_s_std = bttv_s_std,
3375 .vidioc_enum_input = bttv_enum_input,
3376 .vidioc_g_input = bttv_g_input,
3377 .vidioc_s_input = bttv_s_input,
3378 .vidioc_queryctrl = bttv_queryctrl,
3379 .vidioc_g_ctrl = bttv_g_ctrl,
3380 .vidioc_s_ctrl = bttv_s_ctrl,
3381 .vidioc_streamon = bttv_streamon,
3382 .vidioc_streamoff = bttv_streamoff,
3383 .vidioc_g_tuner = bttv_g_tuner,
3384 .vidioc_s_tuner = bttv_s_tuner,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003385 .vidioc_g_crop = bttv_g_crop,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003386 .vidioc_s_crop = bttv_s_crop,
3387 .vidioc_g_fbuf = bttv_g_fbuf,
3388 .vidioc_s_fbuf = bttv_s_fbuf,
3389 .vidioc_overlay = bttv_overlay,
3390 .vidioc_g_priority = bttv_g_priority,
3391 .vidioc_s_priority = bttv_s_priority,
3392 .vidioc_g_parm = bttv_g_parm,
3393 .vidioc_g_frequency = bttv_g_frequency,
3394 .vidioc_s_frequency = bttv_s_frequency,
3395 .vidioc_log_status = bttv_log_status,
3396 .vidioc_querystd = bttv_querystd,
Zoltan Devai43846832008-01-14 13:24:38 -03003397#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003398 .vidioc_g_register = bttv_g_register,
3399 .vidioc_s_register = bttv_s_register,
Zoltan Devai43846832008-01-14 13:24:38 -03003400#endif
Hans Verkuila3998102008-07-21 02:57:38 -03003401};
3402
3403static struct video_device bttv_video_template = {
3404 .fops = &bttv_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003405 .ioctl_ops = &bttv_ioctl_ops,
3406 .tvnorms = BTTV_NORMS,
3407 .current_norm = V4L2_STD_PAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408};
3409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410/* ----------------------------------------------------------------------- */
3411/* radio interface */
3412
Hans Verkuilbec43662008-12-30 06:58:20 -03003413static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003415 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003416 struct bttv *btv = video_drvdata(file);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003417 struct bttv_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Joe Perches8af443e2011-08-21 19:56:48 -03003419 dprintk("open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Joe Perches8af443e2011-08-21 19:56:48 -03003421 dprintk("%d: open called (radio)\n", btv->c.nr);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003422
3423 /* allocate per filehandle data */
3424 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003425 if (unlikely(!fh))
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003426 return -ENOMEM;
3427 file->private_data = fh;
3428 *fh = btv->init;
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003429
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003430 v4l2_prio_open(&btv->prio, &fh->prio);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003431
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 btv->radio_user++;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003433
Hans Verkuil859f0272009-03-28 08:29:00 -03003434 bttv_call_all(btv, tuner, s_radio);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03003435 audio_input(btv,TVAUDIO_INPUT_RADIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438}
3439
Hans Verkuilbec43662008-12-30 06:58:20 -03003440static int radio_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003442 struct bttv_fh *fh = file->private_data;
3443 struct bttv *btv = fh->btv;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003444 struct saa6588_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Hans Verkuilffb48772010-05-01 08:03:24 -03003446 v4l2_prio_close(&btv->prio, fh->prio);
Robert Fitzsimonsb9bc07a2008-04-10 09:40:31 -03003447 file->private_data = NULL;
3448 kfree(fh);
3449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 btv->radio_user--;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003451
Hans Verkuilb9218f22010-12-27 12:22:46 -03003452 bttv_call_all(btv, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 return 0;
3455}
3456
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003457static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003459 struct bttv_fh *fh = priv;
3460 struct bttv *btv = fh->btv;
3461
Trent Piephoabb03622009-01-28 21:32:59 -03003462 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003463 return -EINVAL;
3464 if (0 != t->index)
3465 return -EINVAL;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003466 strcpy(t->name, "Radio");
3467 t->type = V4L2_TUNER_RADIO;
3468
Hans Verkuil859f0272009-03-28 08:29:00 -03003469 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003470
3471 if (btv->audio_mode_gpio)
3472 btv->audio_mode_gpio(btv, t, 0);
3473
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003474 return 0;
3475}
3476
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003477static int radio_s_tuner(struct file *file, void *priv,
3478 struct v4l2_tuner *t)
3479{
3480 struct bttv_fh *fh = priv;
3481 struct bttv *btv = fh->btv;
3482
3483 if (0 != t->index)
3484 return -EINVAL;
3485
Hans Verkuila024c1a2011-06-12 07:02:43 -03003486 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003487 return 0;
3488}
3489
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003490static int radio_queryctrl(struct file *file, void *priv,
3491 struct v4l2_queryctrl *c)
3492{
3493 const struct v4l2_queryctrl *ctrl;
3494
3495 if (c->id < V4L2_CID_BASE ||
3496 c->id >= V4L2_CID_LASTP1)
3497 return -EINVAL;
3498
3499 if (c->id == V4L2_CID_AUDIO_MUTE) {
3500 ctrl = ctrl_by_id(c->id);
3501 *c = *ctrl;
3502 } else
3503 *c = no_ctl;
3504
3505 return 0;
3506}
3507
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003508static ssize_t radio_read(struct file *file, char __user *data,
3509 size_t count, loff_t *ppos)
3510{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003511 struct bttv_fh *fh = file->private_data;
3512 struct bttv *btv = fh->btv;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003513 struct saa6588_command cmd;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003514 cmd.block_count = count/3;
3515 cmd.buffer = data;
3516 cmd.instance = file;
3517 cmd.result = -ENODEV;
3518
Hans Verkuilb9218f22010-12-27 12:22:46 -03003519 bttv_call_all(btv, core, ioctl, SAA6588_CMD_READ, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003520
3521 return cmd.result;
3522}
3523
3524static unsigned int radio_poll(struct file *file, poll_table *wait)
3525{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003526 struct bttv_fh *fh = file->private_data;
3527 struct bttv *btv = fh->btv;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003528 struct saa6588_command cmd;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003529 cmd.instance = file;
3530 cmd.event_list = wait;
3531 cmd.result = -ENODEV;
Hans Verkuilb9218f22010-12-27 12:22:46 -03003532 bttv_call_all(btv, core, ioctl, SAA6588_CMD_POLL, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003533
3534 return cmd.result;
3535}
3536
Hans Verkuilbec43662008-12-30 06:58:20 -03003537static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
3539 .owner = THIS_MODULE,
3540 .open = radio_open,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003541 .read = radio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 .release = radio_release,
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003543 .unlocked_ioctl = video_ioctl2,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003544 .poll = radio_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545};
3546
Hans Verkuila3998102008-07-21 02:57:38 -03003547static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil78dea1a2012-09-09 09:03:29 -03003548 .vidioc_querycap = bttv_querycap,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003549 .vidioc_g_tuner = radio_g_tuner,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003550 .vidioc_s_tuner = radio_s_tuner,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003551 .vidioc_queryctrl = radio_queryctrl,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003552 .vidioc_g_ctrl = bttv_g_ctrl,
3553 .vidioc_s_ctrl = bttv_s_ctrl,
3554 .vidioc_g_frequency = bttv_g_frequency,
3555 .vidioc_s_frequency = bttv_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556};
3557
Hans Verkuila3998102008-07-21 02:57:38 -03003558static struct video_device radio_template = {
3559 .fops = &radio_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003560 .ioctl_ops = &radio_ioctl_ops,
3561};
3562
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563/* ----------------------------------------------------------------------- */
3564/* some debug code */
3565
Adrian Bunk408b6642005-05-01 08:59:29 -07003566static int bttv_risc_decode(u32 risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
3568 static char *instr[16] = {
3569 [ BT848_RISC_WRITE >> 28 ] = "write",
3570 [ BT848_RISC_SKIP >> 28 ] = "skip",
3571 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3572 [ BT848_RISC_JUMP >> 28 ] = "jump",
3573 [ BT848_RISC_SYNC >> 28 ] = "sync",
3574 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3575 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3576 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3577 };
3578 static int incr[16] = {
3579 [ BT848_RISC_WRITE >> 28 ] = 2,
3580 [ BT848_RISC_JUMP >> 28 ] = 2,
3581 [ BT848_RISC_SYNC >> 28 ] = 2,
3582 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3583 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3584 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3585 };
3586 static char *bits[] = {
3587 "be0", "be1", "be2", "be3/resync",
3588 "set0", "set1", "set2", "set3",
3589 "clr0", "clr1", "clr2", "clr3",
3590 "irq", "res", "eol", "sol",
3591 };
3592 int i;
3593
Joe Perches8af443e2011-08-21 19:56:48 -03003594 pr_cont("0x%08x [ %s", risc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3596 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3597 if (risc & (1 << (i + 12)))
Joe Perches8af443e2011-08-21 19:56:48 -03003598 pr_cont(" %s", bits[i]);
3599 pr_cont(" count=%d ]\n", risc & 0xfff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3601}
3602
Adrian Bunk408b6642005-05-01 08:59:29 -07003603static void bttv_risc_disasm(struct bttv *btv,
3604 struct btcx_riscmem *risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
3606 unsigned int i,j,n;
3607
Joe Perches8af443e2011-08-21 19:56:48 -03003608 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
3609 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 for (i = 0; i < (risc->size >> 2); i += n) {
Joe Perches8af443e2011-08-21 19:56:48 -03003611 pr_info("%s: 0x%lx: ",
3612 btv->c.v4l2_dev.name,
3613 (unsigned long)(risc->dma + (i<<2)));
Al Viro3aa71102008-06-22 14:20:09 -03003614 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 for (j = 1; j < n; j++)
Joe Perches8af443e2011-08-21 19:56:48 -03003616 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3617 btv->c.v4l2_dev.name,
3618 (unsigned long)(risc->dma + ((i+j)<<2)),
3619 risc->cpu[i+j], j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 if (0 == risc->cpu[i])
3621 break;
3622 }
3623}
3624
3625static void bttv_print_riscaddr(struct bttv *btv)
3626{
Joe Perches8af443e2011-08-21 19:56:48 -03003627 pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
3628 pr_info(" vbi : o=%08llx e=%08llx\n",
3629 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3630 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3631 pr_info(" cap : o=%08llx e=%08llx\n",
3632 btv->curr.top
3633 ? (unsigned long long)btv->curr.top->top.dma : 0,
3634 btv->curr.bottom
3635 ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3636 pr_info(" scr : o=%08llx e=%08llx\n",
3637 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3638 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 bttv_risc_disasm(btv, &btv->main);
3640}
3641
3642/* ----------------------------------------------------------------------- */
3643/* irq handler */
3644
3645static char *irq_name[] = {
3646 "FMTCHG", // format change detected (525 vs. 625)
3647 "VSYNC", // vertical sync (new field)
3648 "HSYNC", // horizontal sync
3649 "OFLOW", // chroma/luma AGC overflow
3650 "HLOCK", // horizontal lock changed
3651 "VPRES", // video presence changed
3652 "6", "7",
3653 "I2CDONE", // hw irc operation finished
3654 "GPINT", // gpio port triggered irq
3655 "10",
3656 "RISCI", // risc instruction triggered irq
3657 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3658 "FTRGT", // pixel data fifo overrun
3659 "FDSR", // fifo data stream resyncronisation
3660 "PPERR", // parity error (data transfer)
3661 "RIPERR", // parity error (read risc instructions)
3662 "PABORT", // pci abort
3663 "OCERR", // risc instruction error
3664 "SCERR", // syncronisation error
3665};
3666
3667static void bttv_print_irqbits(u32 print, u32 mark)
3668{
3669 unsigned int i;
3670
Joe Perches8af443e2011-08-21 19:56:48 -03003671 pr_cont("bits:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3673 if (print & (1 << i))
Joe Perches8af443e2011-08-21 19:56:48 -03003674 pr_cont(" %s", irq_name[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 if (mark & (1 << i))
Joe Perches8af443e2011-08-21 19:56:48 -03003676 pr_cont("*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 }
3678}
3679
3680static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3681{
Joe Perches8af443e2011-08-21 19:56:48 -03003682 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3683 btv->c.nr,
3684 (unsigned long)btv->main.dma,
3685 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3686 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3687 (unsigned long)rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
3689 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
Joe Perches8af443e2011-08-21 19:56:48 -03003690 pr_notice("%d: Oh, there (temporarily?) is no input signal. "
3691 "Ok, then this is harmless, don't worry ;)\n",
3692 btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 return;
3694 }
Joe Perches8af443e2011-08-21 19:56:48 -03003695 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
3696 btv->c.nr);
3697 pr_notice("%d: Lets try to catch the culpit red-handed ...\n",
3698 btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 dump_stack();
3700}
3701
3702static int
3703bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3704{
3705 struct bttv_buffer *item;
3706
3707 memset(set,0,sizeof(*set));
3708
3709 /* capture request ? */
3710 if (!list_empty(&btv->capture)) {
3711 set->frame_irq = 1;
3712 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3713 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3714 set->top = item;
3715 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3716 set->bottom = item;
3717
3718 /* capture request for other field ? */
3719 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3720 (item->vb.queue.next != &btv->capture)) {
3721 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
Mike Isely66349b42009-09-21 12:09:08 -03003722 /* Mike Isely <isely@pobox.com> - Only check
3723 * and set up the bottom field in the logic
3724 * below. Don't ever do the top field. This
3725 * of course means that if we set up the
3726 * bottom field in the above code that we'll
3727 * actually skip a field. But that's OK.
3728 * Having processed only a single buffer this
3729 * time, then the next time around the first
3730 * available buffer should be for a top field.
3731 * That will then cause us here to set up a
3732 * top then a bottom field in the normal way.
3733 * The alternative to this understanding is
3734 * that we set up the second available buffer
3735 * as a top field, but that's out of order
3736 * since this driver always processes the top
3737 * field first - the effect will be the two
3738 * buffers being returned in the wrong order,
3739 * with the second buffer also being delayed
3740 * by one field time (owing to the fifo nature
3741 * of videobuf). Worse still, we'll be stuck
3742 * doing fields out of order now every time
3743 * until something else causes a field to be
3744 * dropped. By effectively forcing a field to
3745 * drop this way then we always get back into
3746 * sync within a single frame time. (Out of
3747 * order fields can screw up deinterlacing
3748 * algorithms.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 if (NULL == set->bottom &&
3751 V4L2_FIELD_BOTTOM == item->vb.field) {
3752 set->bottom = item;
3753 }
3754 if (NULL != set->top && NULL != set->bottom)
3755 set->top_irq = 2;
3756 }
3757 }
3758 }
3759
3760 /* screen overlay ? */
3761 if (NULL != btv->screen) {
3762 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3763 if (NULL == set->top && NULL == set->bottom) {
3764 set->top = btv->screen;
3765 set->bottom = btv->screen;
3766 }
3767 } else {
3768 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3769 NULL == set->top) {
3770 set->top = btv->screen;
3771 }
3772 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3773 NULL == set->bottom) {
3774 set->bottom = btv->screen;
3775 }
3776 }
3777 }
3778
Joe Perches8af443e2011-08-21 19:56:48 -03003779 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3780 btv->c.nr, set->top, set->bottom,
3781 btv->screen, set->frame_irq, set->top_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 return 0;
3783}
3784
3785static void
3786bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3787 struct bttv_buffer_set *curr, unsigned int state)
3788{
3789 struct timeval ts;
3790
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003791 v4l2_get_timestamp(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
3793 if (wakeup->top == wakeup->bottom) {
3794 if (NULL != wakeup->top && curr->top != wakeup->top) {
3795 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003796 pr_debug("%d: wakeup: both=%p\n",
3797 btv->c.nr, wakeup->top);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 wakeup->top->vb.ts = ts;
3799 wakeup->top->vb.field_count = btv->field_count;
3800 wakeup->top->vb.state = state;
3801 wake_up(&wakeup->top->vb.done);
3802 }
3803 } else {
3804 if (NULL != wakeup->top && curr->top != wakeup->top) {
3805 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003806 pr_debug("%d: wakeup: top=%p\n",
3807 btv->c.nr, wakeup->top);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 wakeup->top->vb.ts = ts;
3809 wakeup->top->vb.field_count = btv->field_count;
3810 wakeup->top->vb.state = state;
3811 wake_up(&wakeup->top->vb.done);
3812 }
3813 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3814 if (irq_debug > 1)
Joe Perches8af443e2011-08-21 19:56:48 -03003815 pr_debug("%d: wakeup: bottom=%p\n",
3816 btv->c.nr, wakeup->bottom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 wakeup->bottom->vb.ts = ts;
3818 wakeup->bottom->vb.field_count = btv->field_count;
3819 wakeup->bottom->vb.state = state;
3820 wake_up(&wakeup->bottom->vb.done);
3821 }
3822 }
3823}
3824
3825static void
3826bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3827 unsigned int state)
3828{
3829 struct timeval ts;
3830
3831 if (NULL == wakeup)
3832 return;
3833
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003834 v4l2_get_timestamp(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 wakeup->vb.ts = ts;
3836 wakeup->vb.field_count = btv->field_count;
3837 wakeup->vb.state = state;
3838 wake_up(&wakeup->vb.done);
3839}
3840
3841static void bttv_irq_timeout(unsigned long data)
3842{
3843 struct bttv *btv = (struct bttv *)data;
3844 struct bttv_buffer_set old,new;
3845 struct bttv_buffer *ovbi;
3846 struct bttv_buffer *item;
3847 unsigned long flags;
3848
3849 if (bttv_verbose) {
Joe Perches8af443e2011-08-21 19:56:48 -03003850 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3851 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3852 btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 bttv_print_irqbits(btread(BT848_INT_STAT),0);
Joe Perches8af443e2011-08-21 19:56:48 -03003854 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 }
3856
3857 spin_lock_irqsave(&btv->s_lock,flags);
3858
3859 /* deactivate stuff */
3860 memset(&new,0,sizeof(new));
3861 old = btv->curr;
3862 ovbi = btv->cvbi;
3863 btv->curr = new;
3864 btv->cvbi = NULL;
3865 btv->loop_irq = 0;
3866 bttv_buffer_activate_video(btv, &new);
3867 bttv_buffer_activate_vbi(btv, NULL);
3868 bttv_set_dma(btv, 0);
3869
3870 /* wake up */
Brandon Philips0fc06862007-11-06 20:02:36 -03003871 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3872 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
3874 /* cancel all outstanding capture / vbi requests */
3875 while (!list_empty(&btv->capture)) {
3876 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3877 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003878 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 wake_up(&item->vb.done);
3880 }
3881 while (!list_empty(&btv->vcapture)) {
3882 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3883 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003884 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 wake_up(&item->vb.done);
3886 }
3887
3888 btv->errors++;
3889 spin_unlock_irqrestore(&btv->s_lock,flags);
3890}
3891
3892static void
3893bttv_irq_wakeup_top(struct bttv *btv)
3894{
3895 struct bttv_buffer *wakeup = btv->curr.top;
3896
3897 if (NULL == wakeup)
3898 return;
3899
3900 spin_lock(&btv->s_lock);
3901 btv->curr.top_irq = 0;
3902 btv->curr.top = NULL;
3903 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3904
Sakari Ailus8e6057b2012-09-15 15:14:42 -03003905 v4l2_get_timestamp(&wakeup->vb.ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 wakeup->vb.field_count = btv->field_count;
Brandon Philips0fc06862007-11-06 20:02:36 -03003907 wakeup->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 wake_up(&wakeup->vb.done);
3909 spin_unlock(&btv->s_lock);
3910}
3911
3912static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3913{
3914 if (rc < risc->dma)
3915 return 0;
3916 if (rc > risc->dma + risc->size)
3917 return 0;
3918 return 1;
3919}
3920
3921static void
3922bttv_irq_switch_video(struct bttv *btv)
3923{
3924 struct bttv_buffer_set new;
3925 struct bttv_buffer_set old;
3926 dma_addr_t rc;
3927
3928 spin_lock(&btv->s_lock);
3929
3930 /* new buffer set */
3931 bttv_irq_next_video(btv, &new);
3932 rc = btread(BT848_RISC_COUNT);
3933 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3934 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3935 btv->framedrop++;
3936 if (debug_latency)
3937 bttv_irq_debug_low_latency(btv, rc);
3938 spin_unlock(&btv->s_lock);
3939 return;
3940 }
3941
3942 /* switch over */
3943 old = btv->curr;
3944 btv->curr = new;
3945 btv->loop_irq &= ~1;
3946 bttv_buffer_activate_video(btv, &new);
3947 bttv_set_dma(btv, 0);
3948
3949 /* switch input */
3950 if (UNSET != btv->new_input) {
3951 video_mux(btv,btv->new_input);
3952 btv->new_input = UNSET;
3953 }
3954
3955 /* wake up finished buffers */
Brandon Philips0fc06862007-11-06 20:02:36 -03003956 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 spin_unlock(&btv->s_lock);
3958}
3959
3960static void
3961bttv_irq_switch_vbi(struct bttv *btv)
3962{
3963 struct bttv_buffer *new = NULL;
3964 struct bttv_buffer *old;
3965 u32 rc;
3966
3967 spin_lock(&btv->s_lock);
3968
3969 if (!list_empty(&btv->vcapture))
3970 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3971 old = btv->cvbi;
3972
3973 rc = btread(BT848_RISC_COUNT);
3974 if (NULL != old && (is_active(&old->top, rc) ||
3975 is_active(&old->bottom, rc))) {
3976 btv->framedrop++;
3977 if (debug_latency)
3978 bttv_irq_debug_low_latency(btv, rc);
3979 spin_unlock(&btv->s_lock);
3980 return;
3981 }
3982
3983 /* switch */
3984 btv->cvbi = new;
3985 btv->loop_irq &= ~4;
3986 bttv_buffer_activate_vbi(btv, new);
3987 bttv_set_dma(btv, 0);
3988
Brandon Philips0fc06862007-11-06 20:02:36 -03003989 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 spin_unlock(&btv->s_lock);
3991}
3992
David Howells7d12e782006-10-05 14:55:46 +01003993static irqreturn_t bttv_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994{
3995 u32 stat,astat;
3996 u32 dstat;
3997 int count;
3998 struct bttv *btv;
3999 int handled = 0;
4000
4001 btv=(struct bttv *)dev_id;
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 count=0;
4004 while (1) {
4005 /* get/clear interrupt status bits */
4006 stat=btread(BT848_INT_STAT);
4007 astat=stat&btread(BT848_INT_MASK);
4008 if (!astat)
4009 break;
4010 handled = 1;
4011 btwrite(stat,BT848_INT_STAT);
4012
4013 /* get device status bits */
4014 dstat=btread(BT848_DSTATUS);
4015
4016 if (irq_debug) {
Joe Perches8af443e2011-08-21 19:56:48 -03004017 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
4018 btv->c.nr, count, btv->field_count,
4019 stat>>28, btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 bttv_print_irqbits(stat,astat);
4021 if (stat & BT848_INT_HLOCK)
Joe Perches8af443e2011-08-21 19:56:48 -03004022 pr_cont(" HLOC => %s",
4023 dstat & BT848_DSTATUS_HLOC
4024 ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 if (stat & BT848_INT_VPRES)
Joe Perches8af443e2011-08-21 19:56:48 -03004026 pr_cont(" PRES => %s",
4027 dstat & BT848_DSTATUS_PRES
4028 ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 if (stat & BT848_INT_FMTCHG)
Joe Perches8af443e2011-08-21 19:56:48 -03004030 pr_cont(" NUML => %s",
4031 dstat & BT848_DSTATUS_NUML
4032 ? "625" : "525");
4033 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 }
4035
4036 if (astat&BT848_INT_VSYNC)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004037 btv->field_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004039 if ((astat & BT848_INT_GPINT) && btv->remote) {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004040 bttv_input_irq(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 }
4042
4043 if (astat & BT848_INT_I2CDONE) {
4044 btv->i2c_done = stat;
4045 wake_up(&btv->i2c_queue);
4046 }
4047
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004048 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 bttv_irq_switch_vbi(btv);
4050
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004051 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 bttv_irq_wakeup_top(btv);
4053
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004054 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 bttv_irq_switch_video(btv);
4056
4057 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004058 audio_mute(btv, btv->mute); /* trigger automute */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
4060 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
Joe Perches8af443e2011-08-21 19:56:48 -03004061 pr_info("%d: %s%s @ %08x,",
4062 btv->c.nr,
4063 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4064 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4065 btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 bttv_print_irqbits(stat,astat);
Joe Perches8af443e2011-08-21 19:56:48 -03004067 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 if (bttv_debug)
4069 bttv_print_riscaddr(btv);
4070 }
4071 if (fdsr && astat & BT848_INT_FDSR) {
Joe Perches8af443e2011-08-21 19:56:48 -03004072 pr_info("%d: FDSR @ %08x\n",
4073 btv->c.nr, btread(BT848_RISC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 if (bttv_debug)
4075 bttv_print_riscaddr(btv);
4076 }
4077
4078 count++;
4079 if (count > 4) {
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004080
4081 if (count > 8 || !(astat & BT848_INT_GPINT)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004082 btwrite(0, BT848_INT_MASK);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004083
Joe Perches8af443e2011-08-21 19:56:48 -03004084 pr_err("%d: IRQ lockup, cleared int mask [",
4085 btv->c.nr);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004086 } else {
Joe Perches8af443e2011-08-21 19:56:48 -03004087 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
4088 btv->c.nr);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004089
4090 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4091 BT848_INT_MASK);
4092 };
4093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 bttv_print_irqbits(stat,astat);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004095
Joe Perches8af443e2011-08-21 19:56:48 -03004096 pr_cont("]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 }
4098 }
4099 btv->irq_total++;
4100 if (handled)
4101 btv->irq_me++;
4102 return IRQ_RETVAL(handled);
4103}
4104
4105
4106/* ----------------------------------------------------------------------- */
4107/* initialitation */
4108
4109static struct video_device *vdev_init(struct bttv *btv,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004110 const struct video_device *template,
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004111 const char *type_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
4113 struct video_device *vfd;
4114
4115 vfd = video_device_alloc();
4116 if (NULL == vfd)
4117 return NULL;
4118 *vfd = *template;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004119 vfd->v4l2_dev = &btv->c.v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 vfd->release = video_device_release;
Mauro Carvalho Chehab1d0a4362008-06-23 12:31:29 -03004121 vfd->debug = bttv_debug;
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004122 video_set_drvdata(vfd, btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4124 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004125 type_name, bttv_tvcards[btv->c.type].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 return vfd;
4127}
4128
4129static void bttv_unregister_video(struct bttv *btv)
4130{
4131 if (btv->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004132 if (video_is_registered(btv->video_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 video_unregister_device(btv->video_dev);
4134 else
4135 video_device_release(btv->video_dev);
4136 btv->video_dev = NULL;
4137 }
4138 if (btv->vbi_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004139 if (video_is_registered(btv->vbi_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 video_unregister_device(btv->vbi_dev);
4141 else
4142 video_device_release(btv->vbi_dev);
4143 btv->vbi_dev = NULL;
4144 }
4145 if (btv->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004146 if (video_is_registered(btv->radio_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 video_unregister_device(btv->radio_dev);
4148 else
4149 video_device_release(btv->radio_dev);
4150 btv->radio_dev = NULL;
4151 }
4152}
4153
4154/* register video4linux devices */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004155static int bttv_register_video(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156{
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004157 if (no_overlay > 0)
Joe Perches8af443e2011-08-21 19:56:48 -03004158 pr_notice("Overlay support disabled\n");
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004159
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 /* video */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004161 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004162
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004163 if (NULL == btv->video_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004165 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
4166 video_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03004168 pr_info("%d: registered device %s\n",
4169 btv->c.nr, video_device_node_name(btv->video_dev));
Hans Verkuil22a04f12008-07-20 06:35:02 -03004170 if (device_create_file(&btv->video_dev->dev,
Kay Sievers54bd5b62007-10-08 16:26:13 -03004171 &dev_attr_card)<0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004172 pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
Trent Piephod94fc9a2006-07-29 17:18:06 -03004173 goto err;
4174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
4176 /* vbi */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004177 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004178
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004179 if (NULL == btv->vbi_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004181 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
4182 vbi_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03004184 pr_info("%d: registered device %s\n",
4185 btv->c.nr, video_device_node_name(btv->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004187 if (!btv->has_radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 return 0;
4189 /* radio */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004190 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004191 if (NULL == btv->radio_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004193 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4194 radio_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 goto err;
Joe Perches8af443e2011-08-21 19:56:48 -03004196 pr_info("%d: registered device %s\n",
4197 btv->c.nr, video_device_node_name(btv->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
4199 /* all done */
4200 return 0;
4201
4202 err:
4203 bttv_unregister_video(btv);
4204 return -1;
4205}
4206
4207
4208/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4209/* response on cards with no firmware is not enabled by OF */
4210static void pci_set_command(struct pci_dev *dev)
4211{
4212#if defined(__powerpc__)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004213 unsigned int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004215 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4216 cmd = (cmd | PCI_COMMAND_MEMORY );
4217 pci_write_config_dword(dev, PCI_COMMAND, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218#endif
4219}
4220
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004221static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222{
4223 int result;
4224 unsigned char lat;
4225 struct bttv *btv;
4226
4227 if (bttv_num == BTTV_MAX)
4228 return -ENOMEM;
Joe Perches8af443e2011-08-21 19:56:48 -03004229 pr_info("Bt8xx card found (%d)\n", bttv_num);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004230 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004231 if (btv == NULL) {
Joe Perches8af443e2011-08-21 19:56:48 -03004232 pr_err("out of memory\n");
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004233 return -ENOMEM;
4234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 btv->c.nr = bttv_num;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004236 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4237 "bttv%d", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
4239 /* initialize structs / fill in defaults */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02004240 mutex_init(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004241 spin_lock_init(&btv->s_lock);
4242 spin_lock_init(&btv->gpio_lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004243 init_waitqueue_head(&btv->i2c_queue);
4244 INIT_LIST_HEAD(&btv->c.subs);
4245 INIT_LIST_HEAD(&btv->capture);
4246 INIT_LIST_HEAD(&btv->vcapture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 v4l2_prio_init(&btv->prio);
4248
4249 init_timer(&btv->timeout);
4250 btv->timeout.function = bttv_irq_timeout;
4251 btv->timeout.data = (unsigned long)btv;
4252
Michael Krufky7c08fb02005-11-08 21:36:21 -08004253 btv->i2c_rc = -1;
4254 btv->tuner_type = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 btv->new_input = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 btv->has_radio=radio[btv->c.nr];
4257
4258 /* pci stuff (init, get irq/mmio, ... */
4259 btv->c.pci = dev;
Michael Krufky7c08fb02005-11-08 21:36:21 -08004260 btv->id = dev->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 if (pci_enable_device(dev)) {
Joe Perches8af443e2011-08-21 19:56:48 -03004262 pr_warn("%d: Can't enable device\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 return -EIO;
4264 }
Yang Hongyang284901a2009-04-06 19:01:15 -07004265 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Joe Perches8af443e2011-08-21 19:56:48 -03004266 pr_warn("%d: No suitable DMA available\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 return -EIO;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 if (!request_mem_region(pci_resource_start(dev,0),
4270 pci_resource_len(dev,0),
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004271 btv->c.v4l2_dev.name)) {
Joe Perches8af443e2011-08-21 19:56:48 -03004272 pr_warn("%d: can't request iomem (0x%llx)\n",
4273 btv->c.nr,
4274 (unsigned long long)pci_resource_start(dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 return -EBUSY;
4276 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004277 pci_set_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 pci_set_command(dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004279
4280 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4281 if (result < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004282 pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004283 goto fail0;
4284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Bjørn Morkabd34d82011-03-21 11:35:56 -03004286 btv->revision = dev->revision;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004287 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
Joe Perches8af443e2011-08-21 19:56:48 -03004288 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
4289 bttv_num, btv->id, btv->revision, pci_name(dev),
4290 btv->c.pci->irq, lat,
4291 (unsigned long long)pci_resource_start(dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 schedule();
4293
Akinobu Mita5f1693f2006-12-20 10:08:56 -03004294 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4295 if (NULL == btv->bt848_mmio) {
Joe Perches8af443e2011-08-21 19:56:48 -03004296 pr_err("%d: ioremap() failed\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 result = -EIO;
4298 goto fail1;
4299 }
4300
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004301 /* identify card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 bttv_idcard(btv);
4303
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004304 /* disable irqs, register irq handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 btwrite(0, BT848_INT_MASK);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004306 result = request_irq(btv->c.pci->irq, bttv_irq,
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004307 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004308 if (result < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004309 pr_err("%d: can't get IRQ %d\n",
4310 bttv_num, btv->c.pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 goto fail1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
4314 if (0 != bttv_handle_chipset(btv)) {
4315 result = -EIO;
4316 goto fail2;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
4319 /* init options from insmod args */
4320 btv->opt_combfilter = combfilter;
4321 btv->opt_lumafilter = lumafilter;
4322 btv->opt_automute = automute;
4323 btv->opt_chroma_agc = chroma_agc;
4324 btv->opt_adc_crush = adc_crush;
4325 btv->opt_vcr_hack = vcr_hack;
4326 btv->opt_whitecrush_upper = whitecrush_upper;
4327 btv->opt_whitecrush_lower = whitecrush_lower;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07004328 btv->opt_uv_ratio = uv_ratio;
4329 btv->opt_full_luma_range = full_luma_range;
4330 btv->opt_coring = coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
4332 /* fill struct bttv with some useful defaults */
4333 btv->init.btv = btv;
4334 btv->init.ov.w.width = 320;
4335 btv->init.ov.w.height = 240;
Mauro Carvalho Chehabc96dd072007-10-26 16:51:47 -03004336 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 btv->init.width = 320;
4338 btv->init.height = 240;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 btv->input = 0;
4340
4341 /* initialize hardware */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004342 if (bttv_gpio)
4343 bttv_gpio_tracking(btv,"pre-init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
4345 bttv_risc_init_main(btv);
4346 init_bt848(btv);
4347
4348 /* gpio */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004349 btwrite(0x00, BT848_GPIO_REG_INP);
4350 btwrite(0x00, BT848_GPIO_OUT_EN);
4351 if (bttv_verbose)
4352 bttv_gpio_tracking(btv,"init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004354 /* needs to be done before i2c is registered */
4355 bttv_init_card1(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004357 /* register i2c + gpio */
4358 init_bttv_i2c(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004360 /* some card-specific stuff (needs working i2c) */
4361 bttv_init_card2(btv);
Hans Verkuil2c905772009-07-20 08:14:17 -03004362 bttv_init_tuner(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 init_irqreg(btv);
4364
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004365 /* register video4linux + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 if (!bttv_tvcards[btv->c.type].no_video) {
4367 bttv_register_video(btv);
4368 bt848_bright(btv,32768);
Alan McIvor961e6682012-04-01 21:11:08 -03004369 bt848_contrast(btv, 27648);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 bt848_hue(btv,32768);
4371 bt848_sat(btv,32768);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004372 audio_mute(btv, 1);
Trent Piepho333408f2007-07-03 15:08:10 -03004373 set_input(btv, 0, btv->tvnorm);
Michael Schimeke5bd0262007-01-18 16:17:39 -03004374 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4375 btv->crop[1] = btv->crop[0]; /* current = default */
4376 disclaim_vbi_lines(btv);
4377 disclaim_video_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 }
4379
Jarod Wilsonf992a492007-03-24 15:23:50 -03004380 /* add subdevices and autoload dvb-bt8xx if needed */
4381 if (bttv_tvcards[btv->c.type].has_dvb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 bttv_sub_add_device(&btv->c, "dvb");
Jarod Wilsonf992a492007-03-24 15:23:50 -03004383 request_modules(btv);
4384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
Jean Delvared90a4ae2010-02-16 14:22:37 -03004386 if (!disable_ir) {
4387 init_bttv_i2c_ir(btv);
4388 bttv_input_init(btv);
4389 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004390
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 /* everything is fine */
4392 bttv_num++;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004395fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004396 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004398fail1:
4399 v4l2_device_unregister(&btv->c.v4l2_dev);
4400
4401fail0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 if (btv->bt848_mmio)
4403 iounmap(btv->bt848_mmio);
4404 release_mem_region(pci_resource_start(btv->c.pci,0),
4405 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 return result;
4407}
4408
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004409static void bttv_remove(struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004411 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4412 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
4414 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03004415 pr_info("%d: unloading\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Tejun Heo707bcf32010-12-24 16:14:20 +01004417 if (bttv_tvcards[btv->c.type].has_dvb)
4418 flush_request_modules(btv);
4419
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004420 /* shutdown everything (DMA+IRQs) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 btand(~15, BT848_GPIO_DMA_CTL);
4422 btwrite(0, BT848_INT_MASK);
4423 btwrite(~0x0, BT848_INT_STAT);
4424 btwrite(0x0, BT848_GPIO_OUT_EN);
4425 if (bttv_gpio)
4426 bttv_gpio_tracking(btv,"cleanup");
4427
4428 /* tell gpio modules we are leaving ... */
4429 btv->shutdown=1;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004430 bttv_input_fini(btv);
Christopher Pascoe889aee82006-01-09 15:25:28 -02004431 bttv_sub_del_devices(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004433 /* unregister i2c_bus + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 fini_bttv_i2c(btv);
4435
4436 /* unregister video4linux */
4437 bttv_unregister_video(btv);
4438
4439 /* free allocated memory */
4440 btcx_riscmem_free(btv->c.pci,&btv->main);
4441
4442 /* free ressources */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004443 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 iounmap(btv->bt848_mmio);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004445 release_mem_region(pci_resource_start(btv->c.pci,0),
4446 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004448 v4l2_device_unregister(&btv->c.v4l2_dev);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004449 bttvs[btv->c.nr] = NULL;
4450 kfree(btv);
4451
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004452 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453}
4454
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004455#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4457{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004458 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4459 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 struct bttv_buffer_set idle;
4461 unsigned long flags;
4462
Joe Perches8af443e2011-08-21 19:56:48 -03004463 dprintk("%d: suspend %d\n", btv->c.nr, state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
4465 /* stop dma + irqs */
4466 spin_lock_irqsave(&btv->s_lock,flags);
4467 memset(&idle, 0, sizeof(idle));
4468 btv->state.video = btv->curr;
4469 btv->state.vbi = btv->cvbi;
4470 btv->state.loop_irq = btv->loop_irq;
4471 btv->curr = idle;
4472 btv->loop_irq = 0;
4473 bttv_buffer_activate_video(btv, &idle);
4474 bttv_buffer_activate_vbi(btv, NULL);
4475 bttv_set_dma(btv, 0);
4476 btwrite(0, BT848_INT_MASK);
4477 spin_unlock_irqrestore(&btv->s_lock,flags);
4478
4479 /* save bt878 state */
4480 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4481 btv->state.gpio_data = gpio_read();
4482
4483 /* save pci state */
4484 pci_save_state(pci_dev);
4485 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4486 pci_disable_device(pci_dev);
4487 btv->state.disabled = 1;
4488 }
4489 return 0;
4490}
4491
4492static int bttv_resume(struct pci_dev *pci_dev)
4493{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004494 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4495 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004497 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
Joe Perches8af443e2011-08-21 19:56:48 -03004499 dprintk("%d: resume\n", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
4501 /* restore pci state */
4502 if (btv->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004503 err=pci_enable_device(pci_dev);
4504 if (err) {
Joe Perches8af443e2011-08-21 19:56:48 -03004505 pr_warn("%d: Can't enable device\n", btv->c.nr);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004506 return err;
4507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 btv->state.disabled = 0;
4509 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004510 err=pci_set_power_state(pci_dev, PCI_D0);
4511 if (err) {
4512 pci_disable_device(pci_dev);
Joe Perches8af443e2011-08-21 19:56:48 -03004513 pr_warn("%d: Can't enable device\n", btv->c.nr);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004514 btv->state.disabled = 1;
4515 return err;
4516 }
4517
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 pci_restore_state(pci_dev);
4519
4520 /* restore bt878 state */
4521 bttv_reinit_bt848(btv);
4522 gpio_inout(0xffffff, btv->state.gpio_enable);
4523 gpio_write(btv->state.gpio_data);
4524
4525 /* restart dma */
4526 spin_lock_irqsave(&btv->s_lock,flags);
4527 btv->curr = btv->state.video;
4528 btv->cvbi = btv->state.vbi;
4529 btv->loop_irq = btv->state.loop_irq;
4530 bttv_buffer_activate_video(btv, &btv->curr);
4531 bttv_buffer_activate_vbi(btv, btv->cvbi);
4532 bttv_set_dma(btv, 0);
4533 spin_unlock_irqrestore(&btv->s_lock,flags);
4534 return 0;
4535}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004536#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
4538static struct pci_device_id bttv_pci_tbl[] = {
Joe Perches76e97412009-07-05 15:59:21 -03004539 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4540 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4541 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4542 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
Peter De Schrijverc540d9f2011-11-06 10:47:58 -03004543 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004544 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545};
4546
4547MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4548
4549static struct pci_driver bttv_pci_driver = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004550 .name = "bttv",
4551 .id_table = bttv_pci_tbl,
4552 .probe = bttv_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08004553 .remove = bttv_remove,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004554#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 .suspend = bttv_suspend,
4556 .resume = bttv_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004557#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558};
4559
Adrian Bunk7d44e892007-12-11 19:23:43 -03004560static int __init bttv_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561{
Randy Dunlapc526e222006-07-15 09:08:26 -03004562 int ret;
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 bttv_num = 0;
4565
Joe Perches8af443e2011-08-21 19:56:48 -03004566 pr_info("driver version %s loaded\n", BTTV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4568 gbuffers = 2;
Filipe Rossetf41b6962009-05-28 11:11:53 -03004569 if (gbufsize > BTTV_MAX_FBUF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 gbufsize = BTTV_MAX_FBUF;
4571 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4572 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -03004573 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
4574 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575
4576 bttv_check_chipset();
4577
Randy Dunlapc526e222006-07-15 09:08:26 -03004578 ret = bus_register(&bttv_sub_bus_type);
4579 if (ret < 0) {
Joe Perches8af443e2011-08-21 19:56:48 -03004580 pr_warn("bus_register error: %d\n", ret);
Randy Dunlapc526e222006-07-15 09:08:26 -03004581 return ret;
4582 }
Akinobu Mita9e7e85e2007-12-17 14:26:29 -03004583 ret = pci_register_driver(&bttv_pci_driver);
4584 if (ret < 0)
4585 bus_unregister(&bttv_sub_bus_type);
4586
4587 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588}
4589
Adrian Bunk7d44e892007-12-11 19:23:43 -03004590static void __exit bttv_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591{
4592 pci_unregister_driver(&bttv_pci_driver);
4593 bus_unregister(&bttv_sub_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594}
4595
4596module_init(bttv_init_module);
4597module_exit(bttv_cleanup_module);
4598
4599/*
4600 * Local variables:
4601 * c-basic-offset: 8
4602 * End:
4603 */