blob: c14b819d6b785b908734c4638a22ef8546996ff1 [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
37#include <linux/init.h>
38#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/errno.h>
42#include <linux/fs.h>
43#include <linux/kernel.h>
44#include <linux/sched.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040045#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/interrupt.h>
47#include <linux/kdev_t.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020048#include "bttvp.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020049#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030050#include <media/v4l2-ioctl.h>
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -030051#include <media/tvaudio.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030052#include <media/msp3400.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020053
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -070054#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <asm/io.h>
57#include <asm/byteorder.h>
58
Mauro Carvalho Chehabfa3fcce2006-03-23 21:45:24 -030059#include <media/rds.h>
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070060
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062unsigned int bttv_num; /* number of Bt848s in use */
Trent Piepho4b10d3b2009-01-28 21:32:59 -030063struct bttv *bttvs[BTTV_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020065unsigned int bttv_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066unsigned int bttv_verbose = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020067unsigned int bttv_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/* config variables */
70#ifdef __BIG_ENDIAN
71static unsigned int bigendian=1;
72#else
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020073static unsigned int bigendian;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif
75static unsigned int radio[BTTV_MAX];
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020076static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static unsigned int gbuffers = 8;
78static unsigned int gbufsize = 0x208000;
Michael Schimeke5bd0262007-01-18 16:17:39 -030079static unsigned int reset_crop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Jean Delvare176c2f32008-09-07 12:49:59 -030081static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
82static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
83static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020084static int debug_latency;
Jean Delvared90a4ae2010-02-16 14:22:37 -030085static int disable_ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020087static unsigned int fdsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/* options */
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020090static unsigned int combfilter;
91static unsigned int lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static unsigned int automute = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020093static unsigned int chroma_agc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static unsigned int adc_crush = 1;
95static unsigned int whitecrush_upper = 0xCF;
96static unsigned int whitecrush_lower = 0x7F;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020097static unsigned int vcr_hack;
98static unsigned int irq_iswitch;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -070099static unsigned int uv_ratio = 50;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200100static unsigned int full_luma_range;
101static unsigned int coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/* API features (turn on/off stuff for testing) */
104static unsigned int v4l2 = 1;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* insmod args */
107module_param(bttv_verbose, int, 0644);
108module_param(bttv_gpio, int, 0644);
109module_param(bttv_debug, int, 0644);
110module_param(irq_debug, int, 0644);
111module_param(debug_latency, int, 0644);
Jean Delvared90a4ae2010-02-16 14:22:37 -0300112module_param(disable_ir, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114module_param(fdsr, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115module_param(gbuffers, int, 0444);
116module_param(gbufsize, int, 0444);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300117module_param(reset_crop, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119module_param(v4l2, int, 0644);
120module_param(bigendian, int, 0644);
121module_param(irq_iswitch, int, 0644);
122module_param(combfilter, int, 0444);
123module_param(lumafilter, int, 0444);
124module_param(automute, int, 0444);
125module_param(chroma_agc, int, 0444);
126module_param(adc_crush, int, 0444);
127module_param(whitecrush_upper, int, 0444);
128module_param(whitecrush_lower, int, 0444);
129module_param(vcr_hack, int, 0444);
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700130module_param(uv_ratio, int, 0444);
131module_param(full_luma_range, int, 0444);
132module_param(coring, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Jean Delvare176c2f32008-09-07 12:49:59 -0300134module_param_array(radio, int, NULL, 0444);
135module_param_array(video_nr, int, NULL, 0444);
136module_param_array(radio_nr, int, NULL, 0444);
137module_param_array(vbi_nr, int, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
140MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
141MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
142MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
143MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
144MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
Jean Delvared90a4ae2010-02-16 14:22:37 -0300145MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
147MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
Michael Schimeke5bd0262007-01-18 16:17:39 -0300148MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
149 "is 1 (yes) for compatibility with older applications");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
151MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
152MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
153MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
154MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
155MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
156MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700157MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
158MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
159MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
Jean Delvare176c2f32008-09-07 12:49:59 -0300160MODULE_PARM_DESC(video_nr, "video device numbers");
161MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
162MODULE_PARM_DESC(radio_nr, "radio device numbers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
165MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
166MODULE_LICENSE("GPL");
167
168/* ----------------------------------------------------------------------- */
169/* sysfs */
170
Kay Sievers54bd5b62007-10-08 16:26:13 -0300171static ssize_t show_card(struct device *cd,
172 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300174 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -0300175 struct bttv *btv = video_get_drvdata(vfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
177}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300178static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180/* ----------------------------------------------------------------------- */
Jarod Wilsonf992a492007-03-24 15:23:50 -0300181/* dvb auto-load setup */
182#if defined(CONFIG_MODULES) && defined(MODULE)
183static void request_module_async(struct work_struct *work)
184{
185 request_module("dvb-bt8xx");
186}
187
188static void request_modules(struct bttv *dev)
189{
190 INIT_WORK(&dev->request_module_wk, request_module_async);
191 schedule_work(&dev->request_module_wk);
192}
193#else
194#define request_modules(dev)
195#endif /* CONFIG_MODULES */
196
197
198/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* static data */
200
201/* special timing tables from conexant... */
202static u8 SRAM_Table[][60] =
203{
204 /* PAL digital input over GPIO[7:0] */
205 {
206 45, // 45 bytes following
207 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
208 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
209 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
210 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
211 0x37,0x00,0xAF,0x21,0x00
212 },
213 /* NTSC digital input over GPIO[7:0] */
214 {
215 51, // 51 bytes following
216 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
217 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
218 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
219 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
220 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
221 0x00,
222 },
223 // TGB_NTSC392 // quartzsight
224 // This table has been modified to be used for Fusion Rev D
225 {
226 0x2A, // size of table = 42
227 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
228 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
229 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
230 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
231 0x20, 0x00
232 }
233};
234
Michael Schimeke5bd0262007-01-18 16:17:39 -0300235/* minhdelayx1 first video pixel we can capture on a line and
236 hdelayx1 start of active video, both relative to rising edge of
237 /HRESET pulse (0H) in 1 / fCLKx1.
238 swidth width of active video and
239 totalwidth total line width, both in 1 / fCLKx1.
240 sqwidth total line width in square pixels.
241 vdelay start of active video in 2 * field lines relative to
242 trailing edge of /VRESET pulse (VDELAY register).
243 sheight height of active video in 2 * field lines.
244 videostart0 ITU-R frame line number of the line corresponding
245 to vdelay in the first field. */
246#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
247 vdelay, sheight, videostart0) \
248 .cropcap.bounds.left = minhdelayx1, \
249 /* * 2 because vertically we count field lines times two, */ \
250 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
251 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
252 /* 4 is a safety margin at the end of the line. */ \
253 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
254 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
255 .cropcap.defrect.left = hdelayx1, \
256 .cropcap.defrect.top = (videostart0) * 2, \
257 .cropcap.defrect.width = swidth, \
258 .cropcap.defrect.height = sheight, \
259 .cropcap.pixelaspect.numerator = totalwidth, \
260 .cropcap.pixelaspect.denominator = sqwidth,
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262const struct bttv_tvnorm bttv_tvnorms[] = {
263 /* PAL-BDGHI */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800264 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
265 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 {
267 .v4l2_id = V4L2_STD_PAL,
268 .name = "PAL",
269 .Fsc = 35468950,
270 .swidth = 924,
271 .sheight = 576,
272 .totalwidth = 1135,
273 .adelay = 0x7f,
274 .bdelay = 0x72,
275 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
276 .scaledtwidth = 1135,
277 .hdelayx1 = 186,
278 .hactivex1 = 924,
279 .vdelay = 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300280 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 .sram = 0,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200282 /* ITU-R frame line number of the first VBI line
Michael Schimeke5bd0262007-01-18 16:17:39 -0300283 we can capture, of the first and second field.
284 The last line is determined by cropcap.bounds. */
285 .vbistart = { 7, 320 },
286 CROPCAP(/* minhdelayx1 */ 68,
287 /* hdelayx1 */ 186,
288 /* Should be (768 * 1135 + 944 / 2) / 944.
289 cropcap.defrect is used for image width
290 checks, so we keep the old value 924. */
291 /* swidth */ 924,
292 /* totalwidth */ 1135,
293 /* sqwidth */ 944,
294 /* vdelay */ 0x20,
295 /* sheight */ 576,
296 /* videostart0 */ 23)
297 /* bt878 (and bt848?) can capture another
298 line below active video. */
299 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 },{
Hans Verkuild97a11e2006-02-07 06:48:40 -0200301 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 .name = "NTSC",
303 .Fsc = 28636363,
304 .swidth = 768,
305 .sheight = 480,
306 .totalwidth = 910,
307 .adelay = 0x68,
308 .bdelay = 0x5d,
309 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
310 .scaledtwidth = 910,
311 .hdelayx1 = 128,
312 .hactivex1 = 910,
313 .vdelay = 0x1a,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300314 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 .sram = 1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200316 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300317 CROPCAP(/* minhdelayx1 */ 68,
318 /* hdelayx1 */ 128,
319 /* Should be (640 * 910 + 780 / 2) / 780? */
320 /* swidth */ 768,
321 /* totalwidth */ 910,
322 /* sqwidth */ 780,
323 /* vdelay */ 0x1a,
324 /* sheight */ 480,
325 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 },{
327 .v4l2_id = V4L2_STD_SECAM,
328 .name = "SECAM",
329 .Fsc = 35468950,
330 .swidth = 924,
331 .sheight = 576,
332 .totalwidth = 1135,
333 .adelay = 0x7f,
334 .bdelay = 0xb0,
335 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
336 .scaledtwidth = 1135,
337 .hdelayx1 = 186,
338 .hactivex1 = 922,
339 .vdelay = 0x20,
340 .vbipack = 255,
341 .sram = 0, /* like PAL, correct? */
Michael H. Schimek67f15702006-01-09 15:25:27 -0200342 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300343 CROPCAP(/* minhdelayx1 */ 68,
344 /* hdelayx1 */ 186,
345 /* swidth */ 924,
346 /* totalwidth */ 1135,
347 /* sqwidth */ 944,
348 /* vdelay */ 0x20,
349 /* sheight */ 576,
350 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 },{
352 .v4l2_id = V4L2_STD_PAL_Nc,
353 .name = "PAL-Nc",
354 .Fsc = 28636363,
355 .swidth = 640,
356 .sheight = 576,
357 .totalwidth = 910,
358 .adelay = 0x68,
359 .bdelay = 0x5d,
360 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
361 .scaledtwidth = 780,
362 .hdelayx1 = 130,
363 .hactivex1 = 734,
364 .vdelay = 0x1a,
365 .vbipack = 144,
366 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200367 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300368 CROPCAP(/* minhdelayx1 */ 68,
369 /* hdelayx1 */ 130,
370 /* swidth */ (640 * 910 + 780 / 2) / 780,
371 /* totalwidth */ 910,
372 /* sqwidth */ 780,
373 /* vdelay */ 0x1a,
374 /* sheight */ 576,
375 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 },{
377 .v4l2_id = V4L2_STD_PAL_M,
378 .name = "PAL-M",
379 .Fsc = 28636363,
380 .swidth = 640,
381 .sheight = 480,
382 .totalwidth = 910,
383 .adelay = 0x68,
384 .bdelay = 0x5d,
385 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
386 .scaledtwidth = 780,
387 .hdelayx1 = 135,
388 .hactivex1 = 754,
389 .vdelay = 0x1a,
390 .vbipack = 144,
391 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200392 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300393 CROPCAP(/* minhdelayx1 */ 68,
394 /* hdelayx1 */ 135,
395 /* swidth */ (640 * 910 + 780 / 2) / 780,
396 /* totalwidth */ 910,
397 /* sqwidth */ 780,
398 /* vdelay */ 0x1a,
399 /* sheight */ 480,
400 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 },{
402 .v4l2_id = V4L2_STD_PAL_N,
403 .name = "PAL-N",
404 .Fsc = 35468950,
405 .swidth = 768,
406 .sheight = 576,
407 .totalwidth = 1135,
408 .adelay = 0x7f,
409 .bdelay = 0x72,
410 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
411 .scaledtwidth = 944,
412 .hdelayx1 = 186,
413 .hactivex1 = 922,
414 .vdelay = 0x20,
415 .vbipack = 144,
416 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300417 .vbistart = { 7, 320 },
418 CROPCAP(/* minhdelayx1 */ 68,
419 /* hdelayx1 */ 186,
420 /* swidth */ (768 * 1135 + 944 / 2) / 944,
421 /* totalwidth */ 1135,
422 /* sqwidth */ 944,
423 /* vdelay */ 0x20,
424 /* sheight */ 576,
425 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 },{
427 .v4l2_id = V4L2_STD_NTSC_M_JP,
428 .name = "NTSC-JP",
429 .Fsc = 28636363,
430 .swidth = 640,
431 .sheight = 480,
432 .totalwidth = 910,
433 .adelay = 0x68,
434 .bdelay = 0x5d,
435 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
436 .scaledtwidth = 780,
437 .hdelayx1 = 135,
438 .hactivex1 = 754,
439 .vdelay = 0x16,
440 .vbipack = 144,
441 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300442 .vbistart = { 10, 273 },
443 CROPCAP(/* minhdelayx1 */ 68,
444 /* hdelayx1 */ 135,
445 /* swidth */ (640 * 910 + 780 / 2) / 780,
446 /* totalwidth */ 910,
447 /* sqwidth */ 780,
448 /* vdelay */ 0x16,
449 /* sheight */ 480,
450 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 },{
452 /* that one hopefully works with the strange timing
453 * which video recorders produce when playing a NTSC
454 * tape on a PAL TV ... */
455 .v4l2_id = V4L2_STD_PAL_60,
456 .name = "PAL-60",
457 .Fsc = 35468950,
458 .swidth = 924,
459 .sheight = 480,
460 .totalwidth = 1135,
461 .adelay = 0x7f,
462 .bdelay = 0x72,
463 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
464 .scaledtwidth = 1135,
465 .hdelayx1 = 186,
466 .hactivex1 = 924,
467 .vdelay = 0x1a,
468 .vbipack = 255,
469 .vtotal = 524,
470 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200471 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300472 CROPCAP(/* minhdelayx1 */ 68,
473 /* hdelayx1 */ 186,
474 /* swidth */ 924,
475 /* totalwidth */ 1135,
476 /* sqwidth */ 944,
477 /* vdelay */ 0x1a,
478 /* sheight */ 480,
479 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481};
482static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
483
484/* ----------------------------------------------------------------------- */
485/* bttv format list
486 packed pixel formats must come first */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300487static const struct bttv_format formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 {
489 .name = "8 bpp, gray",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 .fourcc = V4L2_PIX_FMT_GREY,
491 .btformat = BT848_COLOR_FMT_Y8,
492 .depth = 8,
493 .flags = FORMAT_FLAGS_PACKED,
494 },{
495 .name = "8 bpp, dithered color",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 .fourcc = V4L2_PIX_FMT_HI240,
497 .btformat = BT848_COLOR_FMT_RGB8,
498 .depth = 8,
499 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
500 },{
501 .name = "15 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 .fourcc = V4L2_PIX_FMT_RGB555,
503 .btformat = BT848_COLOR_FMT_RGB15,
504 .depth = 16,
505 .flags = FORMAT_FLAGS_PACKED,
506 },{
507 .name = "15 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 .fourcc = V4L2_PIX_FMT_RGB555X,
509 .btformat = BT848_COLOR_FMT_RGB15,
510 .btswap = 0x03, /* byteswap */
511 .depth = 16,
512 .flags = FORMAT_FLAGS_PACKED,
513 },{
514 .name = "16 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 .fourcc = V4L2_PIX_FMT_RGB565,
516 .btformat = BT848_COLOR_FMT_RGB16,
517 .depth = 16,
518 .flags = FORMAT_FLAGS_PACKED,
519 },{
520 .name = "16 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 .fourcc = V4L2_PIX_FMT_RGB565X,
522 .btformat = BT848_COLOR_FMT_RGB16,
523 .btswap = 0x03, /* byteswap */
524 .depth = 16,
525 .flags = FORMAT_FLAGS_PACKED,
526 },{
527 .name = "24 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 .fourcc = V4L2_PIX_FMT_BGR24,
529 .btformat = BT848_COLOR_FMT_RGB24,
530 .depth = 24,
531 .flags = FORMAT_FLAGS_PACKED,
532 },{
533 .name = "32 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 .fourcc = V4L2_PIX_FMT_BGR32,
535 .btformat = BT848_COLOR_FMT_RGB32,
536 .depth = 32,
537 .flags = FORMAT_FLAGS_PACKED,
538 },{
539 .name = "32 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 .fourcc = V4L2_PIX_FMT_RGB32,
541 .btformat = BT848_COLOR_FMT_RGB32,
542 .btswap = 0x0f, /* byte+word swap */
543 .depth = 32,
544 .flags = FORMAT_FLAGS_PACKED,
545 },{
546 .name = "4:2:2, packed, YUYV",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 .fourcc = V4L2_PIX_FMT_YUYV,
548 .btformat = BT848_COLOR_FMT_YUY2,
549 .depth = 16,
550 .flags = FORMAT_FLAGS_PACKED,
551 },{
552 .name = "4:2:2, packed, YUYV",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 .fourcc = V4L2_PIX_FMT_YUYV,
554 .btformat = BT848_COLOR_FMT_YUY2,
555 .depth = 16,
556 .flags = FORMAT_FLAGS_PACKED,
557 },{
558 .name = "4:2:2, packed, UYVY",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 .fourcc = V4L2_PIX_FMT_UYVY,
560 .btformat = BT848_COLOR_FMT_YUY2,
561 .btswap = 0x03, /* byteswap */
562 .depth = 16,
563 .flags = FORMAT_FLAGS_PACKED,
564 },{
565 .name = "4:2:2, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 .fourcc = V4L2_PIX_FMT_YUV422P,
567 .btformat = BT848_COLOR_FMT_YCrCb422,
568 .depth = 16,
569 .flags = FORMAT_FLAGS_PLANAR,
570 .hshift = 1,
571 .vshift = 0,
572 },{
573 .name = "4:2:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 .fourcc = V4L2_PIX_FMT_YUV420,
575 .btformat = BT848_COLOR_FMT_YCrCb422,
576 .depth = 12,
577 .flags = FORMAT_FLAGS_PLANAR,
578 .hshift = 1,
579 .vshift = 1,
580 },{
581 .name = "4:2:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 .fourcc = V4L2_PIX_FMT_YVU420,
583 .btformat = BT848_COLOR_FMT_YCrCb422,
584 .depth = 12,
585 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
586 .hshift = 1,
587 .vshift = 1,
588 },{
589 .name = "4:1:1, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 .fourcc = V4L2_PIX_FMT_YUV411P,
591 .btformat = BT848_COLOR_FMT_YCrCb411,
592 .depth = 12,
593 .flags = FORMAT_FLAGS_PLANAR,
594 .hshift = 2,
595 .vshift = 0,
596 },{
597 .name = "4:1:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 .fourcc = V4L2_PIX_FMT_YUV410,
599 .btformat = BT848_COLOR_FMT_YCrCb411,
600 .depth = 9,
601 .flags = FORMAT_FLAGS_PLANAR,
602 .hshift = 2,
603 .vshift = 2,
604 },{
605 .name = "4:1:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 .fourcc = V4L2_PIX_FMT_YVU410,
607 .btformat = BT848_COLOR_FMT_YCrCb411,
608 .depth = 9,
609 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
610 .hshift = 2,
611 .vshift = 2,
612 },{
613 .name = "raw scanlines",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 .fourcc = -1,
615 .btformat = BT848_COLOR_FMT_RAW,
616 .depth = 8,
617 .flags = FORMAT_FLAGS_RAW,
618 }
619};
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300620static const unsigned int FORMATS = ARRAY_SIZE(formats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622/* ----------------------------------------------------------------------- */
623
624#define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
625#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
626#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
627#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
628#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
629#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
630#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
631#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700632#define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
633#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
634#define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
635#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637static const struct v4l2_queryctrl no_ctl = {
638 .name = "42",
639 .flags = V4L2_CTRL_FLAG_DISABLED,
640};
641static const struct v4l2_queryctrl bttv_ctls[] = {
642 /* --- video --- */
643 {
644 .id = V4L2_CID_BRIGHTNESS,
645 .name = "Brightness",
646 .minimum = 0,
647 .maximum = 65535,
648 .step = 256,
649 .default_value = 32768,
650 .type = V4L2_CTRL_TYPE_INTEGER,
651 },{
652 .id = V4L2_CID_CONTRAST,
653 .name = "Contrast",
654 .minimum = 0,
655 .maximum = 65535,
656 .step = 128,
657 .default_value = 32768,
658 .type = V4L2_CTRL_TYPE_INTEGER,
659 },{
660 .id = V4L2_CID_SATURATION,
661 .name = "Saturation",
662 .minimum = 0,
663 .maximum = 65535,
664 .step = 128,
665 .default_value = 32768,
666 .type = V4L2_CTRL_TYPE_INTEGER,
667 },{
668 .id = V4L2_CID_HUE,
669 .name = "Hue",
670 .minimum = 0,
671 .maximum = 65535,
672 .step = 256,
673 .default_value = 32768,
674 .type = V4L2_CTRL_TYPE_INTEGER,
675 },
676 /* --- audio --- */
677 {
678 .id = V4L2_CID_AUDIO_MUTE,
679 .name = "Mute",
680 .minimum = 0,
681 .maximum = 1,
682 .type = V4L2_CTRL_TYPE_BOOLEAN,
683 },{
684 .id = V4L2_CID_AUDIO_VOLUME,
685 .name = "Volume",
686 .minimum = 0,
687 .maximum = 65535,
688 .step = 65535/100,
689 .default_value = 65535,
690 .type = V4L2_CTRL_TYPE_INTEGER,
691 },{
692 .id = V4L2_CID_AUDIO_BALANCE,
693 .name = "Balance",
694 .minimum = 0,
695 .maximum = 65535,
696 .step = 65535/100,
697 .default_value = 32768,
698 .type = V4L2_CTRL_TYPE_INTEGER,
699 },{
700 .id = V4L2_CID_AUDIO_BASS,
701 .name = "Bass",
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_TREBLE,
709 .name = "Treble",
710 .minimum = 0,
711 .maximum = 65535,
712 .step = 65535/100,
713 .default_value = 32768,
714 .type = V4L2_CTRL_TYPE_INTEGER,
715 },
716 /* --- private --- */
717 {
718 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
719 .name = "chroma agc",
720 .minimum = 0,
721 .maximum = 1,
722 .type = V4L2_CTRL_TYPE_BOOLEAN,
723 },{
724 .id = V4L2_CID_PRIVATE_COMBFILTER,
725 .name = "combfilter",
726 .minimum = 0,
727 .maximum = 1,
728 .type = V4L2_CTRL_TYPE_BOOLEAN,
729 },{
730 .id = V4L2_CID_PRIVATE_AUTOMUTE,
731 .name = "automute",
732 .minimum = 0,
733 .maximum = 1,
734 .type = V4L2_CTRL_TYPE_BOOLEAN,
735 },{
736 .id = V4L2_CID_PRIVATE_LUMAFILTER,
737 .name = "luma decimation filter",
738 .minimum = 0,
739 .maximum = 1,
740 .type = V4L2_CTRL_TYPE_BOOLEAN,
741 },{
742 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
743 .name = "agc crush",
744 .minimum = 0,
745 .maximum = 1,
746 .type = V4L2_CTRL_TYPE_BOOLEAN,
747 },{
748 .id = V4L2_CID_PRIVATE_VCR_HACK,
749 .name = "vcr hack",
750 .minimum = 0,
751 .maximum = 1,
752 .type = V4L2_CTRL_TYPE_BOOLEAN,
753 },{
754 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
755 .name = "whitecrush upper",
756 .minimum = 0,
757 .maximum = 255,
758 .step = 1,
759 .default_value = 0xCF,
760 .type = V4L2_CTRL_TYPE_INTEGER,
761 },{
762 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
763 .name = "whitecrush lower",
764 .minimum = 0,
765 .maximum = 255,
766 .step = 1,
767 .default_value = 0x7F,
768 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700769 },{
770 .id = V4L2_CID_PRIVATE_UV_RATIO,
771 .name = "uv ratio",
772 .minimum = 0,
773 .maximum = 100,
774 .step = 1,
775 .default_value = 50,
776 .type = V4L2_CTRL_TYPE_INTEGER,
777 },{
778 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
779 .name = "full luma range",
780 .minimum = 0,
781 .maximum = 1,
782 .type = V4L2_CTRL_TYPE_BOOLEAN,
783 },{
784 .id = V4L2_CID_PRIVATE_CORING,
785 .name = "coring",
786 .minimum = 0,
787 .maximum = 3,
788 .step = 1,
789 .default_value = 0,
790 .type = V4L2_CTRL_TYPE_INTEGER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700793
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300797static const struct v4l2_queryctrl *ctrl_by_id(int id)
798{
799 int i;
800
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300801 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300802 if (bttv_ctls[i].id == id)
803 return bttv_ctls+i;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300804
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300805 return NULL;
806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/* ----------------------------------------------------------------------- */
809/* resource management */
810
Michael Schimeke5bd0262007-01-18 16:17:39 -0300811/*
812 RESOURCE_ allocated by freed by
813
814 VIDEO_READ bttv_read 1) bttv_read 2)
815
816 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
817 VIDIOC_QBUF 1) bttv_release
818 VIDIOCMCAPTURE 1)
819
820 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
821 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
822 3) bttv_release
823
824 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
825 VIDIOC_QBUF 1) bttv_release
826 bttv_read, bttv_poll 1) 4)
827
828 1) The resource must be allocated when we enter buffer prepare functions
829 and remain allocated while buffers are in the DMA queue.
830 2) This is a single frame read.
831 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
832 RESOURCE_OVERLAY is allocated.
833 4) This is a continuous read, implies VIDIOC_STREAMON.
834
835 Note this driver permits video input and standard changes regardless if
836 resources are allocated.
837*/
838
839#define VBI_RESOURCES (RESOURCE_VBI)
840#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
841 RESOURCE_VIDEO_STREAM | \
842 RESOURCE_OVERLAY)
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300845int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Michael Schimeke5bd0262007-01-18 16:17:39 -0300847 int xbits; /* mutual exclusive resources */
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (fh->resources & bit)
850 /* have it already allocated */
851 return 1;
852
Michael Schimeke5bd0262007-01-18 16:17:39 -0300853 xbits = bit;
854 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
855 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /* is it free? */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300858 if (btv->resources & xbits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 /* no, someone else uses it */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300860 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300862
863 if ((bit & VIDEO_RESOURCES)
864 && 0 == (btv->resources & VIDEO_RESOURCES)) {
865 /* Do crop - use current, don't - use default parameters. */
866 __s32 top = btv->crop[!!fh->do_crop].rect.top;
867
868 if (btv->vbi_end > top)
869 goto fail;
870
871 /* We cannot capture the same line as video and VBI data.
872 Claim scan lines crop[].rect.top to bottom. */
873 btv->crop_start = top;
874 } else if (bit & VBI_RESOURCES) {
875 __s32 end = fh->vbi_fmt.end;
876
877 if (end > btv->crop_start)
878 goto fail;
879
880 /* Claim scan lines above fh->vbi_fmt.end. */
881 btv->vbi_end = end;
882 }
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* it's free, grab it */
885 fh->resources |= bit;
886 btv->resources |= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return 1;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300888
889 fail:
Michael Schimeke5bd0262007-01-18 16:17:39 -0300890 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893static
894int check_btres(struct bttv_fh *fh, int bit)
895{
896 return (fh->resources & bit);
897}
898
899static
900int locked_btres(struct bttv *btv, int bit)
901{
902 return (btv->resources & bit);
903}
904
Michael Schimeke5bd0262007-01-18 16:17:39 -0300905/* Call with btv->lock down. */
906static void
907disclaim_vbi_lines(struct bttv *btv)
908{
909 btv->vbi_end = 0;
910}
911
912/* Call with btv->lock down. */
913static void
914disclaim_video_lines(struct bttv *btv)
915{
916 const struct bttv_tvnorm *tvnorm;
917 u8 crop;
918
919 tvnorm = &bttv_tvnorms[btv->tvnorm];
920 btv->crop_start = tvnorm->cropcap.bounds.top
921 + tvnorm->cropcap.bounds.height;
922
923 /* VBI capturing ends at VDELAY, start of video capturing, no
924 matter how many lines the VBI RISC program expects. When video
925 capturing is off, it shall no longer "preempt" VBI capturing,
926 so we set VDELAY to maximum. */
927 crop = btread(BT848_E_CROP) | 0xc0;
928 btwrite(crop, BT848_E_CROP);
929 btwrite(0xfe, BT848_E_VDELAY_LO);
930 btwrite(crop, BT848_O_CROP);
931 btwrite(0xfe, BT848_O_VDELAY_LO);
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300935void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if ((fh->resources & bits) != bits) {
938 /* trying to free ressources not allocated by us ... */
939 printk("bttv: BUG! (btres)\n");
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 fh->resources &= ~bits;
942 btv->resources &= ~bits;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300943
944 bits = btv->resources;
945
946 if (0 == (bits & VIDEO_RESOURCES))
947 disclaim_video_lines(btv);
948
949 if (0 == (bits & VBI_RESOURCES))
950 disclaim_vbi_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953/* ----------------------------------------------------------------------- */
954/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
955
956/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
957 PLL_X = Reference pre-divider (0=1, 1=2)
958 PLL_C = Post divider (0=6, 1=4)
959 PLL_I = Integer input
960 PLL_F = Fractional input
961
962 F_input = 28.636363 MHz:
963 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
964*/
965
966static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
967{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800968 unsigned char fl, fh, fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800970 /* prevent overflows */
971 fin/=4;
972 fout/=4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800974 fout*=12;
975 fi=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800977 fout=(fout%fin)*256;
978 fh=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800980 fout=(fout%fin)*256;
981 fl=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800983 btwrite(fl, BT848_PLL_F_LO);
984 btwrite(fh, BT848_PLL_F_HI);
985 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
988static void set_pll(struct bttv *btv)
989{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800990 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800992 if (!btv->pll.pll_crystal)
993 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
996 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800997 return;
998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001000 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1001 /* no PLL needed */
1002 if (btv->pll.pll_current == 0)
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001003 return;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001004 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001005 btv->c.nr,btv->pll.pll_ifreq);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001006 btwrite(0x00,BT848_TGCTRL);
1007 btwrite(0x00,BT848_PLL_XCI);
1008 btv->pll.pll_current = 0;
1009 return;
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001012 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001013 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1015
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001016 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 /* Let other people run while the PLL stabilizes */
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001018 bttv_printk(".");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 msleep(10);
1020
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001021 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 btwrite(0,BT848_DSTATUS);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001023 } else {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001024 btwrite(0x08,BT848_TGCTRL);
1025 btv->pll.pll_current = btv->pll.pll_ofreq;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001026 bttv_printk(" ok\n");
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001027 return;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001028 }
1029 }
1030 btv->pll.pll_current = -1;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001031 bttv_printk("failed\n");
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001032 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035/* used to switch between the bt848's analog/digital video capture modes */
1036static void bt848A_set_timing(struct bttv *btv)
1037{
1038 int i, len;
1039 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1040 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1041
Trent Piepho5221e212009-01-28 21:32:59 -03001042 if (btv->input == btv->dig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1044 btv->c.nr,table_idx);
1045
1046 /* timing change...reset timing generator address */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001047 btwrite(0x00, BT848_TGCTRL);
1048 btwrite(0x02, BT848_TGCTRL);
1049 btwrite(0x00, BT848_TGCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 len=SRAM_Table[table_idx][0];
1052 for(i = 1; i <= len; i++)
1053 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1054 btv->pll.pll_ofreq = 27000000;
1055
1056 set_pll(btv);
1057 btwrite(0x11, BT848_TGCTRL);
1058 btwrite(0x41, BT848_DVSIF);
1059 } else {
1060 btv->pll.pll_ofreq = fsc;
1061 set_pll(btv);
1062 btwrite(0x0, BT848_DVSIF);
1063 }
1064}
1065
1066/* ----------------------------------------------------------------------- */
1067
1068static void bt848_bright(struct bttv *btv, int bright)
1069{
1070 int value;
1071
1072 // printk("bttv: set bright: %d\n",bright); // DEBUG
1073 btv->bright = bright;
1074
1075 /* We want -128 to 127 we get 0-65535 */
1076 value = (bright >> 8) - 128;
1077 btwrite(value & 0xff, BT848_BRIGHT);
1078}
1079
1080static void bt848_hue(struct bttv *btv, int hue)
1081{
1082 int value;
1083
1084 btv->hue = hue;
1085
1086 /* -128 to 127 */
1087 value = (hue >> 8) - 128;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001088 btwrite(value & 0xff, BT848_HUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091static void bt848_contrast(struct bttv *btv, int cont)
1092{
1093 int value,hibit;
1094
1095 btv->contrast = cont;
1096
1097 /* 0-511 */
1098 value = (cont >> 7);
1099 hibit = (value >> 6) & 4;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001100 btwrite(value & 0xff, BT848_CONTRAST_LO);
1101 btaor(hibit, ~4, BT848_E_CONTROL);
1102 btaor(hibit, ~4, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
1105static void bt848_sat(struct bttv *btv, int color)
1106{
1107 int val_u,val_v,hibits;
1108
1109 btv->saturation = color;
1110
1111 /* 0-511 for the color */
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001112 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1113 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001114 hibits = (val_u >> 7) & 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 hibits |= (val_v >> 8) & 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001116 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1117 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1118 btaor(hibits, ~3, BT848_E_CONTROL);
1119 btaor(hibits, ~3, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122/* ----------------------------------------------------------------------- */
1123
1124static int
1125video_mux(struct bttv *btv, unsigned int input)
1126{
1127 int mux,mask2;
1128
1129 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1130 return -EINVAL;
1131
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001132 /* needed by RemoteVideo MX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1134 if (mask2)
1135 gpio_inout(mask2,mask2);
1136
1137 if (input == btv->svhs) {
1138 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1139 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1140 } else {
1141 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1142 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1143 }
Trent Piepho6f987002009-01-28 21:32:59 -03001144 mux = bttv_muxsel(btv, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1146 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1147 btv->c.nr,input,mux);
1148
1149 /* card specific hook */
1150 if(bttv_tvcards[btv->c.type].muxsel_hook)
1151 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1152 return 0;
1153}
1154
1155static char *audio_modes[] = {
1156 "audio: tuner", "audio: radio", "audio: extern",
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001157 "audio: intern", "audio: mute"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158};
1159
1160static int
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001161audio_mux(struct bttv *btv, int input, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001163 int gpio_val, signal;
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001164 struct v4l2_control ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1167 bttv_tvcards[btv->c.type].gpiomask);
1168 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1169
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001170 btv->mute = mute;
1171 btv->audio = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001173 /* automute */
1174 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1175
1176 if (mute)
1177 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1178 else
1179 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001180
Trent Piepho72134a62009-01-28 21:32:59 -03001181 switch (btv->c.type) {
1182 case BTTV_BOARD_VOODOOTV_FM:
1183 case BTTV_BOARD_VOODOOTV_200:
1184 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1185 break;
1186
1187 default:
1188 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1189 }
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (bttv_gpio)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001192 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1193 if (in_interrupt())
1194 return 0;
1195
1196 ctrl.id = V4L2_CID_AUDIO_MUTE;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001197 ctrl.value = btv->mute;
Hans Verkuil859f0272009-03-28 08:29:00 -03001198 bttv_call_all(btv, core, s_ctrl, &ctrl);
1199 if (btv->sd_msp34xx) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001200 u32 in;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001201
1202 /* Note: the inputs tuner/radio/extern/intern are translated
1203 to msp routings. This assumes common behavior for all msp3400
1204 based TV cards. When this assumption fails, then the
1205 specific MSP routing must be added to the card table.
1206 For now this is sufficient. */
1207 switch (input) {
1208 case TVAUDIO_INPUT_RADIO:
Hans Verkuil5325b422009-04-02 11:26:22 -03001209 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001210 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001211 break;
1212 case TVAUDIO_INPUT_EXTERN:
Hans Verkuil5325b422009-04-02 11:26:22 -03001213 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001214 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001215 break;
1216 case TVAUDIO_INPUT_INTERN:
1217 /* Yes, this is the same input as for RADIO. I doubt
1218 if this is ever used. The only board with an INTERN
1219 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1220 that was tested. My guess is that the whole INTERN
1221 input does not work. */
Hans Verkuil5325b422009-04-02 11:26:22 -03001222 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001223 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001224 break;
1225 case TVAUDIO_INPUT_TUNER:
1226 default:
Wade Berrier434b2522007-06-25 13:02:16 -03001227 /* This is the only card that uses TUNER2, and afaik,
1228 is the only difference between the VOODOOTV_FM
1229 and VOODOOTV_200 */
1230 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
Hans Verkuil5325b422009-04-02 11:26:22 -03001231 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
Wade Berrier434b2522007-06-25 13:02:16 -03001232 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1233 else
Hans Verkuil5325b422009-04-02 11:26:22 -03001234 in = MSP_INPUT_DEFAULT;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001235 break;
1236 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001237 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1238 in, MSP_OUTPUT_DEFAULT, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001239 }
Hans Verkuil859f0272009-03-28 08:29:00 -03001240 if (btv->sd_tvaudio) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001241 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1242 input, 0, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return 0;
1245}
1246
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001247static inline int
1248audio_mute(struct bttv *btv, int mute)
1249{
1250 return audio_mux(btv, btv->audio, mute);
1251}
1252
1253static inline int
1254audio_input(struct bttv *btv, int input)
1255{
1256 return audio_mux(btv, input, btv->mute);
1257}
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259static void
Michael Schimeke5bd0262007-01-18 16:17:39 -03001260bttv_crop_calc_limits(struct bttv_crop *c)
1261{
1262 /* Scale factor min. 1:1, max. 16:1. Min. image size
1263 48 x 32. Scaled width must be a multiple of 4. */
1264
1265 if (1) {
1266 /* For bug compatibility with VIDIOCGCAP and image
1267 size checks in earlier driver versions. */
1268 c->min_scaled_width = 48;
1269 c->min_scaled_height = 32;
1270 } else {
1271 c->min_scaled_width =
1272 (max(48, c->rect.width >> 4) + 3) & ~3;
1273 c->min_scaled_height =
1274 max(32, c->rect.height >> 4);
1275 }
1276
1277 c->max_scaled_width = c->rect.width & ~3;
1278 c->max_scaled_height = c->rect.height;
1279}
1280
1281static void
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001282bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
Michael Schimeke5bd0262007-01-18 16:17:39 -03001283{
1284 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1285 bttv_crop_calc_limits(c);
1286}
1287
1288/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289static int
1290set_tvnorm(struct bttv *btv, unsigned int norm)
1291{
1292 const struct bttv_tvnorm *tvnorm;
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001293 v4l2_std_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001295 BUG_ON(norm >= BTTV_TVNORMS);
1296 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 tvnorm = &bttv_tvnorms[norm];
1299
Mike Isely2de26c02009-09-21 12:42:22 -03001300 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001301 sizeof (tvnorm->cropcap))) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001302 bttv_crop_reset(&btv->crop[0], norm);
1303 btv->crop[1] = btv->crop[0]; /* current = default */
1304
1305 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1306 btv->crop_start = tvnorm->cropcap.bounds.top
1307 + tvnorm->cropcap.bounds.height;
1308 }
1309 }
1310
1311 btv->tvnorm = norm;
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 btwrite(tvnorm->adelay, BT848_ADELAY);
1314 btwrite(tvnorm->bdelay, BT848_BDELAY);
1315 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1316 BT848_IFORM);
1317 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1318 btwrite(1, BT848_VBI_PACK_DEL);
1319 bt848A_set_timing(btv);
1320
1321 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -08001322 case BTTV_BOARD_VOODOOTV_FM:
Wade Berrier434b2522007-06-25 13:02:16 -03001323 case BTTV_BOARD_VOODOOTV_200:
Trent Piepho72134a62009-01-28 21:32:59 -03001324 bttv_tda9880_setnorm(btv, gpio_read());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001327 id = tvnorm->v4l2_id;
Hans Verkuilf41737e2009-04-01 03:52:39 -03001328 bttv_call_all(btv, core, s_std, id);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return 0;
1331}
1332
Michael Schimeke5bd0262007-01-18 16:17:39 -03001333/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334static void
Trent Piepho333408f2007-07-03 15:08:10 -03001335set_input(struct bttv *btv, unsigned int input, unsigned int norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 unsigned long flags;
1338
1339 btv->input = input;
1340 if (irq_iswitch) {
1341 spin_lock_irqsave(&btv->s_lock,flags);
1342 if (btv->curr.frame_irq) {
1343 /* active capture -> delayed input switch */
1344 btv->new_input = input;
1345 } else {
1346 video_mux(btv,input);
1347 }
1348 spin_unlock_irqrestore(&btv->s_lock,flags);
1349 } else {
1350 video_mux(btv,input);
1351 }
Trent Piephoabb03622009-01-28 21:32:59 -03001352 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1353 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
Trent Piepho333408f2007-07-03 15:08:10 -03001354 set_tvnorm(btv, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
1357static void init_irqreg(struct bttv *btv)
1358{
1359 /* clear status */
1360 btwrite(0xfffffUL, BT848_INT_STAT);
1361
1362 if (bttv_tvcards[btv->c.type].no_video) {
1363 /* i2c only */
1364 btwrite(BT848_INT_I2CDONE,
1365 BT848_INT_MASK);
1366 } else {
1367 /* full video */
1368 btwrite((btv->triton1) |
1369 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1370 BT848_INT_SCERR |
1371 (fdsr ? BT848_INT_FDSR : 0) |
Jean Delvareeb1b27b2008-08-30 10:18:21 -03001372 BT848_INT_RISCI | BT848_INT_OCERR |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1374 BT848_INT_I2CDONE,
1375 BT848_INT_MASK);
1376 }
1377}
1378
1379static void init_bt848(struct bttv *btv)
1380{
1381 int val;
1382
1383 if (bttv_tvcards[btv->c.type].no_video) {
1384 /* very basic init only */
1385 init_irqreg(btv);
1386 return;
1387 }
1388
1389 btwrite(0x00, BT848_CAP_CTL);
1390 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1391 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1392
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001393 /* set planar and packed mode trigger points and */
1394 /* set rising edge of inverted GPINTR pin as irq trigger */
1395 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1396 BT848_GPIO_DMA_CTL_PLTP1_16|
1397 BT848_GPIO_DMA_CTL_PLTP23_16|
1398 BT848_GPIO_DMA_CTL_GPINTC|
1399 BT848_GPIO_DMA_CTL_GPINTI,
1400 BT848_GPIO_DMA_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001403 btwrite(val, BT848_E_SCLOOP);
1404 btwrite(val, BT848_O_SCLOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001406 btwrite(0x20, BT848_E_VSCALE_HI);
1407 btwrite(0x20, BT848_O_VSCALE_HI);
1408 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 BT848_ADC);
1410
1411 btwrite(whitecrush_upper, BT848_WC_UP);
1412 btwrite(whitecrush_lower, BT848_WC_DOWN);
1413
1414 if (btv->opt_lumafilter) {
1415 btwrite(0, BT848_E_CONTROL);
1416 btwrite(0, BT848_O_CONTROL);
1417 } else {
1418 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1419 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1420 }
1421
1422 bt848_bright(btv, btv->bright);
1423 bt848_hue(btv, btv->hue);
1424 bt848_contrast(btv, btv->contrast);
1425 bt848_sat(btv, btv->saturation);
1426
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001427 /* interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 init_irqreg(btv);
1429}
1430
1431static void bttv_reinit_bt848(struct bttv *btv)
1432{
1433 unsigned long flags;
1434
1435 if (bttv_verbose)
1436 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1437 spin_lock_irqsave(&btv->s_lock,flags);
1438 btv->errors=0;
1439 bttv_set_dma(btv,0);
1440 spin_unlock_irqrestore(&btv->s_lock,flags);
1441
1442 init_bt848(btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001443 btv->pll.pll_current = -1;
Trent Piepho333408f2007-07-03 15:08:10 -03001444 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001447static int bttv_g_ctrl(struct file *file, void *priv,
1448 struct v4l2_control *c)
1449{
1450 struct bttv_fh *fh = priv;
1451 struct bttv *btv = fh->btv;
1452
1453 switch (c->id) {
1454 case V4L2_CID_BRIGHTNESS:
1455 c->value = btv->bright;
1456 break;
1457 case V4L2_CID_HUE:
1458 c->value = btv->hue;
1459 break;
1460 case V4L2_CID_CONTRAST:
1461 c->value = btv->contrast;
1462 break;
1463 case V4L2_CID_SATURATION:
1464 c->value = btv->saturation;
1465 break;
1466
1467 case V4L2_CID_AUDIO_MUTE:
1468 case V4L2_CID_AUDIO_VOLUME:
1469 case V4L2_CID_AUDIO_BALANCE:
1470 case V4L2_CID_AUDIO_BASS:
1471 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil859f0272009-03-28 08:29:00 -03001472 bttv_call_all(btv, core, g_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001473 break;
1474
1475 case V4L2_CID_PRIVATE_CHROMA_AGC:
1476 c->value = btv->opt_chroma_agc;
1477 break;
1478 case V4L2_CID_PRIVATE_COMBFILTER:
1479 c->value = btv->opt_combfilter;
1480 break;
1481 case V4L2_CID_PRIVATE_LUMAFILTER:
1482 c->value = btv->opt_lumafilter;
1483 break;
1484 case V4L2_CID_PRIVATE_AUTOMUTE:
1485 c->value = btv->opt_automute;
1486 break;
1487 case V4L2_CID_PRIVATE_AGC_CRUSH:
1488 c->value = btv->opt_adc_crush;
1489 break;
1490 case V4L2_CID_PRIVATE_VCR_HACK:
1491 c->value = btv->opt_vcr_hack;
1492 break;
1493 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1494 c->value = btv->opt_whitecrush_upper;
1495 break;
1496 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1497 c->value = btv->opt_whitecrush_lower;
1498 break;
1499 case V4L2_CID_PRIVATE_UV_RATIO:
1500 c->value = btv->opt_uv_ratio;
1501 break;
1502 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1503 c->value = btv->opt_full_luma_range;
1504 break;
1505 case V4L2_CID_PRIVATE_CORING:
1506 c->value = btv->opt_coring;
1507 break;
1508 default:
1509 return -EINVAL;
1510 }
1511 return 0;
1512}
1513
1514static int bttv_s_ctrl(struct file *file, void *f,
1515 struct v4l2_control *c)
1516{
1517 int err;
1518 int val;
1519 struct bttv_fh *fh = f;
1520 struct bttv *btv = fh->btv;
1521
Hans Verkuilffb48772010-05-01 08:03:24 -03001522 err = v4l2_prio_check(&btv->prio, fh->prio);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001523 if (0 != err)
1524 return err;
1525
1526 switch (c->id) {
1527 case V4L2_CID_BRIGHTNESS:
1528 bt848_bright(btv, c->value);
1529 break;
1530 case V4L2_CID_HUE:
1531 bt848_hue(btv, c->value);
1532 break;
1533 case V4L2_CID_CONTRAST:
1534 bt848_contrast(btv, c->value);
1535 break;
1536 case V4L2_CID_SATURATION:
1537 bt848_sat(btv, c->value);
1538 break;
1539 case V4L2_CID_AUDIO_MUTE:
1540 audio_mute(btv, c->value);
1541 /* fall through */
1542 case V4L2_CID_AUDIO_VOLUME:
1543 if (btv->volume_gpio)
1544 btv->volume_gpio(btv, c->value);
1545
Hans Verkuil859f0272009-03-28 08:29:00 -03001546 bttv_call_all(btv, core, s_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001547 break;
1548 case V4L2_CID_AUDIO_BALANCE:
1549 case V4L2_CID_AUDIO_BASS:
1550 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil859f0272009-03-28 08:29:00 -03001551 bttv_call_all(btv, core, s_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001552 break;
1553
1554 case V4L2_CID_PRIVATE_CHROMA_AGC:
1555 btv->opt_chroma_agc = c->value;
1556 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1557 btwrite(val, BT848_E_SCLOOP);
1558 btwrite(val, BT848_O_SCLOOP);
1559 break;
1560 case V4L2_CID_PRIVATE_COMBFILTER:
1561 btv->opt_combfilter = c->value;
1562 break;
1563 case V4L2_CID_PRIVATE_LUMAFILTER:
1564 btv->opt_lumafilter = c->value;
1565 if (btv->opt_lumafilter) {
1566 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1567 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1568 } else {
1569 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1570 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1571 }
1572 break;
1573 case V4L2_CID_PRIVATE_AUTOMUTE:
1574 btv->opt_automute = c->value;
1575 break;
1576 case V4L2_CID_PRIVATE_AGC_CRUSH:
1577 btv->opt_adc_crush = c->value;
1578 btwrite(BT848_ADC_RESERVED |
1579 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1580 BT848_ADC);
1581 break;
1582 case V4L2_CID_PRIVATE_VCR_HACK:
1583 btv->opt_vcr_hack = c->value;
1584 break;
1585 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1586 btv->opt_whitecrush_upper = c->value;
1587 btwrite(c->value, BT848_WC_UP);
1588 break;
1589 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1590 btv->opt_whitecrush_lower = c->value;
1591 btwrite(c->value, BT848_WC_DOWN);
1592 break;
1593 case V4L2_CID_PRIVATE_UV_RATIO:
1594 btv->opt_uv_ratio = c->value;
1595 bt848_sat(btv, btv->saturation);
1596 break;
1597 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1598 btv->opt_full_luma_range = c->value;
1599 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1600 break;
1601 case V4L2_CID_PRIVATE_CORING:
1602 btv->opt_coring = c->value;
1603 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1604 break;
1605 default:
1606 return -EINVAL;
1607 }
1608 return 0;
1609}
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611/* ----------------------------------------------------------------------- */
1612
1613void bttv_gpio_tracking(struct bttv *btv, char *comment)
1614{
1615 unsigned int outbits, data;
1616 outbits = btread(BT848_GPIO_OUT_EN);
1617 data = btread(BT848_GPIO_DATA);
1618 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1619 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1620}
1621
1622static void bttv_field_count(struct bttv *btv)
1623{
1624 int need_count = 0;
1625
1626 if (btv->users)
1627 need_count++;
1628
1629 if (need_count) {
1630 /* start field counter */
1631 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1632 } else {
1633 /* stop field counter */
1634 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1635 btv->field_count = 0;
1636 }
1637}
1638
1639static const struct bttv_format*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640format_by_fourcc(int fourcc)
1641{
1642 unsigned int i;
1643
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001644 for (i = 0; i < FORMATS; i++) {
1645 if (-1 == formats[i].fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 continue;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001647 if (formats[i].fourcc == fourcc)
1648 return formats+i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650 return NULL;
1651}
1652
1653/* ----------------------------------------------------------------------- */
1654/* misc helpers */
1655
1656static int
1657bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1658 struct bttv_buffer *new)
1659{
1660 struct bttv_buffer *old;
1661 unsigned long flags;
1662 int retval = 0;
1663
1664 dprintk("switch_overlay: enter [new=%p]\n",new);
1665 if (new)
Brandon Philips0fc06862007-11-06 20:02:36 -03001666 new->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 spin_lock_irqsave(&btv->s_lock,flags);
1668 old = btv->screen;
1669 btv->screen = new;
1670 btv->loop_irq |= 1;
1671 bttv_set_dma(btv, 0x03);
1672 spin_unlock_irqrestore(&btv->s_lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (NULL != old) {
1674 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001675 bttv_dma_free(&fh->cap,btv, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 kfree(old);
1677 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03001678 if (NULL == new)
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03001679 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 dprintk("switch_overlay: done\n");
1681 return retval;
1682}
1683
1684/* ----------------------------------------------------------------------- */
1685/* video4linux (1) interface */
1686
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001687static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1688 struct bttv_buffer *buf,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001689 const struct bttv_format *fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 unsigned int width, unsigned int height,
1691 enum v4l2_field field)
1692{
Michael Schimeke5bd0262007-01-18 16:17:39 -03001693 struct bttv_fh *fh = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 int redo_dma_risc = 0;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001695 struct bttv_crop c;
1696 int norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 int rc;
1698
1699 /* check settings */
1700 if (NULL == fmt)
1701 return -EINVAL;
1702 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1703 width = RAW_BPL;
1704 height = RAW_LINES*2;
1705 if (width*height > buf->vb.bsize)
1706 return -EINVAL;
1707 buf->vb.size = buf->vb.bsize;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001708
1709 /* Make sure tvnorm and vbi_end remain consistent
1710 until we're done. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03001711
1712 norm = btv->tvnorm;
1713
1714 /* In this mode capturing always starts at defrect.top
1715 (default VDELAY), ignoring cropping parameters. */
1716 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001718 }
1719
Michael Schimeke5bd0262007-01-18 16:17:39 -03001720 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1721 } else {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001722 norm = btv->tvnorm;
1723 c = btv->crop[!!fh->do_crop];
1724
Michael Schimeke5bd0262007-01-18 16:17:39 -03001725 if (width < c.min_scaled_width ||
1726 width > c.max_scaled_width ||
1727 height < c.min_scaled_height)
1728 return -EINVAL;
1729
1730 switch (field) {
1731 case V4L2_FIELD_TOP:
1732 case V4L2_FIELD_BOTTOM:
1733 case V4L2_FIELD_ALTERNATE:
1734 /* btv->crop counts frame lines. Max. scale
1735 factor is 16:1 for frames, 8:1 for fields. */
1736 if (height * 2 > c.max_scaled_height)
1737 return -EINVAL;
1738 break;
1739
1740 default:
1741 if (height > c.max_scaled_height)
1742 return -EINVAL;
1743 break;
1744 }
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 buf->vb.size = (width * height * fmt->depth) >> 3;
1747 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1748 return -EINVAL;
1749 }
1750
1751 /* alloc + fill struct bttv_buffer (if changed) */
1752 if (buf->vb.width != width || buf->vb.height != height ||
1753 buf->vb.field != field ||
Michael Schimeke5bd0262007-01-18 16:17:39 -03001754 buf->tvnorm != norm || buf->fmt != fmt ||
1755 buf->crop.top != c.rect.top ||
1756 buf->crop.left != c.rect.left ||
1757 buf->crop.width != c.rect.width ||
1758 buf->crop.height != c.rect.height) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 buf->vb.width = width;
1760 buf->vb.height = height;
1761 buf->vb.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001762 buf->tvnorm = norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 buf->fmt = fmt;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001764 buf->crop = c.rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 redo_dma_risc = 1;
1766 }
1767
1768 /* alloc risc memory */
Brandon Philips0fc06862007-11-06 20:02:36 -03001769 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 redo_dma_risc = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001771 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 goto fail;
1773 }
1774
1775 if (redo_dma_risc)
1776 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1777 goto fail;
1778
Brandon Philips0fc06862007-11-06 20:02:36 -03001779 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return 0;
1781
1782 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001783 bttv_dma_free(q,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return rc;
1785}
1786
1787static int
1788buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1789{
1790 struct bttv_fh *fh = q->priv_data;
1791
1792 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1793 if (0 == *count)
1794 *count = gbuffers;
Andreas Bombedab7e312010-03-21 16:02:45 -03001795 if (*size * *count > gbuffers * gbufsize)
1796 *count = (gbuffers * gbufsize) / *size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return 0;
1798}
1799
1800static int
1801buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1802 enum v4l2_field field)
1803{
1804 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1805 struct bttv_fh *fh = q->priv_data;
1806
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001807 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 fh->width, fh->height, field);
1809}
1810
1811static void
1812buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1813{
1814 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1815 struct bttv_fh *fh = q->priv_data;
1816 struct bttv *btv = fh->btv;
1817
Brandon Philips0fc06862007-11-06 20:02:36 -03001818 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 list_add_tail(&buf->vb.queue,&btv->capture);
1820 if (!btv->curr.frame_irq) {
1821 btv->loop_irq |= 1;
1822 bttv_set_dma(btv, 0x03);
1823 }
1824}
1825
1826static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1827{
1828 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1829 struct bttv_fh *fh = q->priv_data;
1830
Michael Schimekfeaba7a2007-01-26 08:30:05 -03001831 bttv_dma_free(q,fh->btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
1834static struct videobuf_queue_ops bttv_video_qops = {
1835 .buf_setup = buffer_setup,
1836 .buf_prepare = buffer_prepare,
1837 .buf_queue = buffer_queue,
1838 .buf_release = buffer_release,
1839};
1840
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001841static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001843 struct bttv_fh *fh = priv;
1844 struct bttv *btv = fh->btv;
1845 unsigned int i;
1846 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Hans Verkuilffb48772010-05-01 08:03:24 -03001848 err = v4l2_prio_check(&btv->prio, fh->prio);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001849 if (err)
1850 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001852 for (i = 0; i < BTTV_TVNORMS; i++)
1853 if (*id & bttv_tvnorms[i].v4l2_id)
1854 break;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001855 if (i == BTTV_TVNORMS) {
1856 err = -EINVAL;
1857 goto err;
1858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001860 set_tvnorm(btv, i);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001861
1862err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001864 return err;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001865}
1866
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001867static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001868{
1869 struct bttv_fh *fh = f;
1870 struct bttv *btv = fh->btv;
1871
1872 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1873 *id = V4L2_STD_625_50;
1874 else
1875 *id = V4L2_STD_525_60;
1876 return 0;
1877}
1878
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001879static int bttv_enum_input(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001880 struct v4l2_input *i)
1881{
1882 struct bttv_fh *fh = priv;
1883 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001884 int rc = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001885
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001886 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) {
1887 rc = -EINVAL;
1888 goto err;
1889 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001890
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001891 i->type = V4L2_INPUT_TYPE_CAMERA;
1892 i->audioset = 1;
1893
Trent Piephoabb03622009-01-28 21:32:59 -03001894 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001895 sprintf(i->name, "Television");
1896 i->type = V4L2_INPUT_TYPE_TUNER;
1897 i->tuner = 0;
1898 } else if (i->index == btv->svhs) {
1899 sprintf(i->name, "S-Video");
1900 } else {
1901 sprintf(i->name, "Composite%d", i->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001904 if (i->index == btv->input) {
1905 __u32 dstatus = btread(BT848_DSTATUS);
1906 if (0 == (dstatus & BT848_DSTATUS_PRES))
1907 i->status |= V4L2_IN_ST_NO_SIGNAL;
1908 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1909 i->status |= V4L2_IN_ST_NO_H_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001912 i->std = BTTV_NORMS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001914err:
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001915
1916 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001917}
Nickolay V. Shmyrev4b9b9362006-08-25 16:53:04 -03001918
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001919static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001920{
1921 struct bttv_fh *fh = priv;
1922 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001924 *i = btv->input;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001925
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001926 return 0;
1927}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001929static int bttv_s_input(struct file *file, void *priv, unsigned int i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001930{
1931 struct bttv_fh *fh = priv;
1932 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001934 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001936 err = v4l2_prio_check(&btv->prio, fh->prio);
1937 if (unlikely(err))
1938 goto err;
1939
1940 if (i > bttv_tvcards[btv->c.type].video_inputs) {
1941 err = -EINVAL;
1942 goto err;
1943 }
1944
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001945 set_input(btv, i, btv->tvnorm);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001946
1947err:
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001948 return 0;
1949}
1950
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001951static int bttv_s_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001952 struct v4l2_tuner *t)
1953{
1954 struct bttv_fh *fh = priv;
1955 struct bttv *btv = fh->btv;
1956 int err;
1957
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001958 if (unlikely(0 != t->index))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001959 return -EINVAL;
1960
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001961 if (unlikely(btv->tuner_type == TUNER_ABSENT)) {
1962 err = -EINVAL;
1963 goto err;
1964 }
1965
1966 err = v4l2_prio_check(&btv->prio, fh->prio);
1967 if (unlikely(err))
1968 goto err;
1969
Hans Verkuil859f0272009-03-28 08:29:00 -03001970 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001971
1972 if (btv->audio_mode_gpio)
1973 btv->audio_mode_gpio(btv, t, 1);
1974
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001975err:
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001976
1977 return 0;
1978}
1979
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001980static int bttv_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001981 struct v4l2_frequency *f)
1982{
1983 struct bttv_fh *fh = priv;
1984 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001985
Robert Fitzsimons1b069012008-04-01 11:41:54 -03001986 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001987 f->frequency = btv->freq;
1988
1989 return 0;
1990}
1991
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001992static int bttv_s_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001993 struct v4l2_frequency *f)
1994{
1995 struct bttv_fh *fh = priv;
1996 struct bttv *btv = fh->btv;
1997 int err;
1998
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001999 if (unlikely(f->tuner != 0))
2000 return -EINVAL;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002001
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002002 err = v4l2_prio_check(&btv->prio, fh->prio);
2003 if (unlikely(err))
2004 goto err;
2005
2006 if (unlikely(f->type != (btv->radio_user
2007 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV))) {
2008 err = -EINVAL;
2009 goto err;
2010 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002011 btv->freq = f->frequency;
Hans Verkuil859f0272009-03-28 08:29:00 -03002012 bttv_call_all(btv, tuner, s_frequency, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002013 if (btv->has_matchbox && btv->radio_user)
2014 tea5757_set_freq(btv, btv->freq);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002015err:
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002016
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002017 return 0;
2018}
2019
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002020static int bttv_log_status(struct file *file, void *f)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002021{
2022 struct bttv_fh *fh = f;
2023 struct bttv *btv = fh->btv;
2024
2025 printk(KERN_INFO "bttv%d: ======== START STATUS CARD #%d ========\n",
2026 btv->c.nr, btv->c.nr);
Hans Verkuil859f0272009-03-28 08:29:00 -03002027 bttv_call_all(btv, core, log_status);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002028 printk(KERN_INFO "bttv%d: ======== END STATUS CARD #%d ========\n",
2029 btv->c.nr, btv->c.nr);
2030 return 0;
2031}
2032
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002033#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002034static int bttv_g_register(struct file *file, void *f,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002035 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002036{
2037 struct bttv_fh *fh = f;
2038 struct bttv *btv = fh->btv;
2039
2040 if (!capable(CAP_SYS_ADMIN))
2041 return -EPERM;
2042
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002043 if (!v4l2_chip_match_host(&reg->match))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002044 return -EINVAL;
2045
2046 /* bt848 has a 12-bit register space */
2047 reg->reg &= 0xfff;
2048 reg->val = btread(reg->reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002049 reg->size = 1;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002050
2051 return 0;
2052}
2053
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002054static int bttv_s_register(struct file *file, void *f,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002055 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002056{
2057 struct bttv_fh *fh = f;
2058 struct bttv *btv = fh->btv;
2059
2060 if (!capable(CAP_SYS_ADMIN))
2061 return -EPERM;
2062
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002063 if (!v4l2_chip_match_host(&reg->match))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002064 return -EINVAL;
2065
2066 /* bt848 has a 12-bit register space */
2067 reg->reg &= 0xfff;
2068 btwrite(reg->val, reg->reg);
2069
2070 return 0;
2071}
2072#endif
2073
Michael Schimeke5bd0262007-01-18 16:17:39 -03002074/* Given cropping boundaries b and the scaled width and height of a
2075 single field or frame, which must not exceed hardware limits, this
2076 function adjusts the cropping parameters c. */
2077static void
2078bttv_crop_adjust (struct bttv_crop * c,
2079 const struct v4l2_rect * b,
2080 __s32 width,
2081 __s32 height,
2082 enum v4l2_field field)
2083{
2084 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2085 __s32 max_left;
2086 __s32 max_top;
2087
2088 if (width < c->min_scaled_width) {
2089 /* Max. hor. scale factor 16:1. */
2090 c->rect.width = width * 16;
2091 } else if (width > c->max_scaled_width) {
2092 /* Min. hor. scale factor 1:1. */
2093 c->rect.width = width;
2094
2095 max_left = b->left + b->width - width;
2096 max_left = min(max_left, (__s32) MAX_HDELAY);
2097 if (c->rect.left > max_left)
2098 c->rect.left = max_left;
2099 }
2100
2101 if (height < c->min_scaled_height) {
2102 /* Max. vert. scale factor 16:1, single fields 8:1. */
2103 c->rect.height = height * 16;
2104 } else if (frame_height > c->max_scaled_height) {
2105 /* Min. vert. scale factor 1:1.
2106 Top and height count field lines times two. */
2107 c->rect.height = (frame_height + 1) & ~1;
2108
2109 max_top = b->top + b->height - c->rect.height;
2110 if (c->rect.top > max_top)
2111 c->rect.top = max_top;
2112 }
2113
2114 bttv_crop_calc_limits(c);
2115}
2116
2117/* Returns an error if scaling to a frame or single field with the given
2118 width and height is not possible with the current cropping parameters
2119 and width aligned according to width_mask. If adjust_size is TRUE the
2120 function may adjust the width and/or height instead, rounding width
2121 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2122 also adjust the current cropping parameters to get closer to the
2123 desired image size. */
2124static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002125limit_scaled_size_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002126 __s32 * width,
2127 __s32 * height,
2128 enum v4l2_field field,
2129 unsigned int width_mask,
2130 unsigned int width_bias,
2131 int adjust_size,
2132 int adjust_crop)
2133{
2134 struct bttv *btv = fh->btv;
2135 const struct v4l2_rect *b;
2136 struct bttv_crop *c;
2137 __s32 min_width;
2138 __s32 min_height;
2139 __s32 max_width;
2140 __s32 max_height;
2141 int rc;
2142
2143 BUG_ON((int) width_mask >= 0 ||
2144 width_bias >= (unsigned int) -width_mask);
2145
2146 /* Make sure tvnorm, vbi_end and the current cropping parameters
2147 remain consistent until we're done. */
Michael Schimeke5bd0262007-01-18 16:17:39 -03002148
2149 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2150
2151 /* Do crop - use current, don't - use default parameters. */
2152 c = &btv->crop[!!fh->do_crop];
2153
2154 if (fh->do_crop
2155 && adjust_size
2156 && adjust_crop
2157 && !locked_btres(btv, VIDEO_RESOURCES)) {
2158 min_width = 48;
2159 min_height = 32;
2160
2161 /* We cannot scale up. When the scaled image is larger
2162 than crop.rect we adjust the crop.rect as required
2163 by the V4L2 spec, hence cropcap.bounds are our limit. */
2164 max_width = min(b->width, (__s32) MAX_HACTIVE);
2165 max_height = b->height;
2166
2167 /* We cannot capture the same line as video and VBI data.
2168 Note btv->vbi_end is really a minimum, see
2169 bttv_vbi_try_fmt(). */
2170 if (btv->vbi_end > b->top) {
2171 max_height -= btv->vbi_end - b->top;
2172 rc = -EBUSY;
2173 if (min_height > max_height)
2174 goto fail;
2175 }
2176 } else {
2177 rc = -EBUSY;
2178 if (btv->vbi_end > c->rect.top)
2179 goto fail;
2180
2181 min_width = c->min_scaled_width;
2182 min_height = c->min_scaled_height;
2183 max_width = c->max_scaled_width;
2184 max_height = c->max_scaled_height;
2185
2186 adjust_crop = 0;
2187 }
2188
2189 min_width = (min_width - width_mask - 1) & width_mask;
2190 max_width = max_width & width_mask;
2191
2192 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2193 min_height = min_height;
2194 /* Min. scale factor is 1:1. */
2195 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2196
2197 if (adjust_size) {
2198 *width = clamp(*width, min_width, max_width);
2199 *height = clamp(*height, min_height, max_height);
2200
2201 /* Round after clamping to avoid overflow. */
2202 *width = (*width + width_bias) & width_mask;
2203
2204 if (adjust_crop) {
2205 bttv_crop_adjust(c, b, *width, *height, field);
2206
2207 if (btv->vbi_end > c->rect.top) {
2208 /* Move the crop window out of the way. */
2209 c->rect.top = btv->vbi_end;
2210 }
2211 }
2212 } else {
2213 rc = -EINVAL;
2214 if (*width < min_width ||
2215 *height < min_height ||
2216 *width > max_width ||
2217 *height > max_height ||
2218 0 != (*width & ~width_mask))
2219 goto fail;
2220 }
2221
2222 rc = 0; /* success */
2223
2224 fail:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002225
2226 return rc;
2227}
2228
2229/* Returns an error if the given overlay window dimensions are not
2230 possible with the current cropping parameters. If adjust_size is
2231 TRUE the function may adjust the window width and/or height
2232 instead, however it always rounds the horizontal position and
2233 width as btcx_align() does. If adjust_crop is TRUE the function
2234 may also adjust the current cropping parameters to get closer
2235 to the desired window size. */
2236static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002237verify_window_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002238 struct v4l2_window * win,
2239 int adjust_size,
2240 int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
2242 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002243 unsigned int width_mask;
2244 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 if (win->w.width < 48 || win->w.height < 32)
2247 return -EINVAL;
2248 if (win->clipcount > 2048)
2249 return -EINVAL;
2250
2251 field = win->field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 if (V4L2_FIELD_ANY == field) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002254 __s32 height2;
2255
2256 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2257 field = (win->w.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 ? V4L2_FIELD_INTERLACED
2259 : V4L2_FIELD_TOP;
2260 }
2261 switch (field) {
2262 case V4L2_FIELD_TOP:
2263 case V4L2_FIELD_BOTTOM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 case V4L2_FIELD_INTERLACED:
2265 break;
2266 default:
2267 return -EINVAL;
2268 }
2269
Michael Schimeke5bd0262007-01-18 16:17:39 -03002270 /* 4-byte alignment. */
2271 if (NULL == fh->ovfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002273 width_mask = ~0;
2274 switch (fh->ovfmt->depth) {
2275 case 8:
2276 case 24:
2277 width_mask = ~3;
2278 break;
2279 case 16:
2280 width_mask = ~1;
2281 break;
2282 case 32:
2283 break;
2284 default:
2285 BUG();
2286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Michael Schimeke5bd0262007-01-18 16:17:39 -03002288 win->w.width -= win->w.left & ~width_mask;
2289 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2290
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002291 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002292 field, width_mask,
2293 /* width_bias: round down */ 0,
2294 adjust_size, adjust_crop);
2295 if (0 != rc)
2296 return rc;
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 win->field = field;
2299 return 0;
2300}
2301
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002302static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 struct v4l2_window *win, int fixup)
2304{
2305 struct v4l2_clip *clips = NULL;
2306 int n,size,retval = 0;
2307
2308 if (NULL == fh->ovfmt)
2309 return -EINVAL;
2310 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2311 return -EINVAL;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002312 retval = verify_window_lock(fh, win,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002313 /* adjust_size */ fixup,
2314 /* adjust_crop */ fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (0 != retval)
2316 return retval;
2317
2318 /* copy clips -- luckily v4l1 + v4l2 are binary
2319 compatible here ...*/
2320 n = win->clipcount;
2321 size = sizeof(*clips)*(n+4);
2322 clips = kmalloc(size,GFP_KERNEL);
2323 if (NULL == clips)
2324 return -ENOMEM;
2325 if (n > 0) {
2326 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2327 kfree(clips);
2328 return -EFAULT;
2329 }
2330 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 /* clip against screen */
2333 if (NULL != btv->fbuf.base)
2334 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2335 &win->w, clips, n);
2336 btcx_sort_clips(clips,n);
2337
2338 /* 4-byte alignments */
2339 switch (fh->ovfmt->depth) {
2340 case 8:
2341 case 24:
2342 btcx_align(&win->w, clips, n, 3);
2343 break;
2344 case 16:
2345 btcx_align(&win->w, clips, n, 1);
2346 break;
2347 case 32:
2348 /* no alignment fixups needed */
2349 break;
2350 default:
2351 BUG();
2352 }
2353
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002354 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 fh->ov.clips = clips;
2356 fh->ov.nclips = n;
2357
2358 fh->ov.w = win->w;
2359 fh->ov.field = win->field;
2360 fh->ov.setup_ok = 1;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002361
2362 /*
2363 * FIXME: btv is protected by btv->lock mutex, while btv->init
2364 * is protected by fh->cap.vb_lock. This seems to open the
2365 * possibility for some race situations. Maybe the better would
2366 * be to unify those locks or to use another way to store the
2367 * init values that will be consumed by videobuf callbacks
2368 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 btv->init.ov.w.width = win->w.width;
2370 btv->init.ov.w.height = win->w.height;
2371 btv->init.ov.field = win->field;
2372
2373 /* update overlay if needed */
2374 retval = 0;
2375 if (check_btres(fh, RESOURCE_OVERLAY)) {
2376 struct bttv_buffer *new;
2377
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002378 new = videobuf_sg_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03002379 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2381 retval = bttv_switch_overlay(btv,fh,new);
2382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return retval;
2384}
2385
2386/* ----------------------------------------------------------------------- */
2387
2388static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2389{
2390 struct videobuf_queue* q = NULL;
2391
2392 switch (fh->type) {
2393 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2394 q = &fh->cap;
2395 break;
2396 case V4L2_BUF_TYPE_VBI_CAPTURE:
2397 q = &fh->vbi;
2398 break;
2399 default:
2400 BUG();
2401 }
2402 return q;
2403}
2404
2405static int bttv_resource(struct bttv_fh *fh)
2406{
2407 int res = 0;
2408
2409 switch (fh->type) {
2410 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002411 res = RESOURCE_VIDEO_STREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 break;
2413 case V4L2_BUF_TYPE_VBI_CAPTURE:
2414 res = RESOURCE_VBI;
2415 break;
2416 default:
2417 BUG();
2418 }
2419 return res;
2420}
2421
2422static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2423{
2424 struct videobuf_queue *q = bttv_queue(fh);
2425 int res = bttv_resource(fh);
2426
2427 if (check_btres(fh,res))
2428 return -EBUSY;
2429 if (videobuf_queue_is_busy(q))
2430 return -EBUSY;
2431 fh->type = type;
2432 return 0;
2433}
2434
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002435static void
2436pix_format_set_size (struct v4l2_pix_format * f,
2437 const struct bttv_format * fmt,
2438 unsigned int width,
2439 unsigned int height)
2440{
2441 f->width = width;
2442 f->height = height;
2443
2444 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2445 f->bytesperline = width; /* Y plane */
2446 f->sizeimage = (width * height * fmt->depth) >> 3;
2447 } else {
2448 f->bytesperline = (width * fmt->depth) >> 3;
2449 f->sizeimage = height * f->bytesperline;
2450 }
2451}
2452
Hans Verkuil78b526a2008-05-28 12:16:41 -03002453static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002454 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002456 struct bttv_fh *fh = priv;
2457
2458 pix_format_set_size(&f->fmt.pix, fh->fmt,
2459 fh->width, fh->height);
2460 f->fmt.pix.field = fh->cap.field;
2461 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2462
2463 return 0;
2464}
2465
Hans Verkuil78b526a2008-05-28 12:16:41 -03002466static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002467 struct v4l2_format *f)
2468{
2469 struct bttv_fh *fh = priv;
2470
2471 f->fmt.win.w = fh->ov.w;
2472 f->fmt.win.field = fh->ov.field;
2473
2474 return 0;
2475}
2476
Hans Verkuil78b526a2008-05-28 12:16:41 -03002477static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002478 struct v4l2_format *f)
2479{
2480 const struct bttv_format *fmt;
2481 struct bttv_fh *fh = priv;
2482 struct bttv *btv = fh->btv;
2483 enum v4l2_field field;
2484 __s32 width, height;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002485 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002486
2487 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2488 if (NULL == fmt)
2489 return -EINVAL;
2490
2491 field = f->fmt.pix.field;
2492
2493 if (V4L2_FIELD_ANY == field) {
2494 __s32 height2;
2495
2496 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2497 field = (f->fmt.pix.height > height2)
2498 ? V4L2_FIELD_INTERLACED
2499 : V4L2_FIELD_BOTTOM;
2500 }
2501
2502 if (V4L2_FIELD_SEQ_BT == field)
2503 field = V4L2_FIELD_SEQ_TB;
2504
2505 switch (field) {
2506 case V4L2_FIELD_TOP:
2507 case V4L2_FIELD_BOTTOM:
2508 case V4L2_FIELD_ALTERNATE:
2509 case V4L2_FIELD_INTERLACED:
2510 break;
2511 case V4L2_FIELD_SEQ_TB:
2512 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2513 return -EINVAL;
2514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 default:
2516 return -EINVAL;
2517 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002518
2519 width = f->fmt.pix.width;
2520 height = f->fmt.pix.height;
2521
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002522 rc = limit_scaled_size_lock(fh, &width, &height, field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002523 /* width_mask: 4 pixels */ ~3,
2524 /* width_bias: nearest */ 2,
2525 /* adjust_size */ 1,
2526 /* adjust_crop */ 0);
2527 if (0 != rc)
2528 return rc;
2529
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002530 /* update data for the application */
2531 f->fmt.pix.field = field;
2532 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2533
2534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
Hans Verkuil78b526a2008-05-28 12:16:41 -03002537static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002538 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002540 struct bttv_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002542 return verify_window_lock(fh, &f->fmt.win,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002543 /* adjust_size */ 1,
2544 /* adjust_crop */ 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Hans Verkuil78b526a2008-05-28 12:16:41 -03002547static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002548 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
2550 int retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002551 const struct bttv_format *fmt;
2552 struct bttv_fh *fh = priv;
2553 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002554 __s32 width, height;
2555 enum v4l2_field field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002557 retval = bttv_switch_type(fh, f->type);
2558 if (0 != retval)
2559 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Hans Verkuil78b526a2008-05-28 12:16:41 -03002561 retval = bttv_try_fmt_vid_cap(file, priv, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002562 if (0 != retval)
2563 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002565 width = f->fmt.pix.width;
2566 height = f->fmt.pix.height;
2567 field = f->fmt.pix.field;
2568
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002569 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002570 /* width_mask: 4 pixels */ ~3,
2571 /* width_bias: nearest */ 2,
2572 /* adjust_size */ 1,
2573 /* adjust_crop */ 1);
2574 if (0 != retval)
2575 return retval;
2576
2577 f->fmt.pix.field = field;
2578
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002579 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002581 /* update our state informations */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002582 fh->fmt = fmt;
2583 fh->cap.field = f->fmt.pix.field;
2584 fh->cap.last = V4L2_FIELD_NONE;
2585 fh->width = f->fmt.pix.width;
2586 fh->height = f->fmt.pix.height;
2587 btv->init.fmt = fmt;
2588 btv->init.width = f->fmt.pix.width;
2589 btv->init.height = f->fmt.pix.height;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002590
2591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
Hans Verkuil78b526a2008-05-28 12:16:41 -03002594static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002595 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002597 struct bttv_fh *fh = priv;
2598 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002600 if (no_overlay > 0) {
2601 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002602 return -EINVAL;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002603 }
Michael Krufky5e453dc2006-01-09 15:32:31 -02002604
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002605 return setup_window_lock(fh, btv, &f->fmt.win, 1);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002606}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002608#ifdef CONFIG_VIDEO_V4L1_COMPAT
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002609static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2610{
2611 int retval;
2612 unsigned int i;
2613 struct bttv_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -07002615 retval = __videobuf_mmap_setup(&fh->cap, gbuffers, gbufsize,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002616 V4L2_MEMORY_MMAP);
2617 if (retval < 0) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002618 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002620
2621 gbuffers = retval;
2622 memset(mbuf, 0, sizeof(*mbuf));
2623 mbuf->frames = gbuffers;
2624 mbuf->size = gbuffers * gbufsize;
2625
2626 for (i = 0; i < gbuffers; i++)
2627 mbuf->offsets[i] = i * gbufsize;
2628
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002629 return 0;
2630}
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002631#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002633static int bttv_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002634 struct v4l2_capability *cap)
2635{
2636 struct bttv_fh *fh = priv;
2637 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002639 if (0 == v4l2)
2640 return -EINVAL;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002641
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002642 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2643 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2644 snprintf(cap->bus_info, sizeof(cap->bus_info),
2645 "PCI:%s", pci_name(btv->c.pci));
2646 cap->version = BTTV_VERSION_CODE;
2647 cap->capabilities =
2648 V4L2_CAP_VIDEO_CAPTURE |
2649 V4L2_CAP_VBI_CAPTURE |
2650 V4L2_CAP_READWRITE |
2651 V4L2_CAP_STREAMING;
2652 if (no_overlay <= 0)
2653 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002655 /*
2656 * No need to lock here: those vars are initialized during board
2657 * probe and remains untouched during the rest of the driver lifecycle
2658 */
2659 if (btv->has_saa6588)
2660 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
Trent Piephoabb03622009-01-28 21:32:59 -03002661 if (btv->tuner_type != TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002662 cap->capabilities |= V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002664}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002666static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2667{
2668 int index = -1, i;
2669
2670 for (i = 0; i < FORMATS; i++) {
2671 if (formats[i].fourcc != -1)
2672 index++;
2673 if ((unsigned int)index == f->index)
2674 break;
2675 }
2676 if (FORMATS == i)
2677 return -EINVAL;
2678
2679 f->pixelformat = formats[i].fourcc;
2680 strlcpy(f->description, formats[i].name, sizeof(f->description));
2681
2682 return i;
2683}
2684
Hans Verkuil78b526a2008-05-28 12:16:41 -03002685static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002686 struct v4l2_fmtdesc *f)
2687{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002688 int rc = bttv_enum_fmt_cap_ovr(f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002689
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002690 if (rc < 0)
2691 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002692
2693 return 0;
2694}
2695
Hans Verkuil78b526a2008-05-28 12:16:41 -03002696static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002697 struct v4l2_fmtdesc *f)
2698{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002699 int rc;
2700
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002701 if (no_overlay > 0) {
2702 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2703 return -EINVAL;
2704 }
2705
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002706 rc = bttv_enum_fmt_cap_ovr(f);
2707
2708 if (rc < 0)
2709 return rc;
2710
2711 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002712 return -EINVAL;
2713
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002714 return 0;
2715}
2716
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002717static int bttv_g_fbuf(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002718 struct v4l2_framebuffer *fb)
2719{
2720 struct bttv_fh *fh = f;
2721 struct bttv *btv = fh->btv;
2722
2723 *fb = btv->fbuf;
2724 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2725 if (fh->ovfmt)
2726 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2727 return 0;
2728}
2729
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002730static int bttv_overlay(struct file *file, void *f, unsigned int on)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002731{
2732 struct bttv_fh *fh = f;
2733 struct bttv *btv = fh->btv;
2734 struct bttv_buffer *new;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002735 int retval = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002736
2737 if (on) {
2738 /* verify args */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002739 if (unlikely(!btv->fbuf.base)) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002740 return -EINVAL;
2741 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002742 if (unlikely(!fh->ov.setup_ok)) {
2743 dprintk("bttv%d: overlay: !setup_ok\n", btv->c.nr);
2744 retval = -EINVAL;
2745 }
2746 if (retval)
2747 return retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002748 }
2749
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002750 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002751 return -EBUSY;
2752
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002753 if (on) {
2754 fh->ov.tvnorm = btv->tvnorm;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002755 new = videobuf_sg_alloc(sizeof(*new));
Robert Fitzsimons7c018802008-02-13 16:38:11 -03002756 new->crop = btv->crop[!!fh->do_crop].rect;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002757 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2758 } else {
2759 new = NULL;
2760 }
2761
2762 /* switch over */
2763 retval = bttv_switch_overlay(btv, fh, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return retval;
2765}
2766
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002767static int bttv_s_fbuf(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002768 struct v4l2_framebuffer *fb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002770 struct bttv_fh *fh = f;
2771 struct bttv *btv = fh->btv;
2772 const struct bttv_format *fmt;
2773 int retval;
2774
2775 if (!capable(CAP_SYS_ADMIN) &&
2776 !capable(CAP_SYS_RAWIO))
2777 return -EPERM;
2778
2779 /* check args */
2780 fmt = format_by_fourcc(fb->fmt.pixelformat);
2781 if (NULL == fmt)
2782 return -EINVAL;
2783 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2784 return -EINVAL;
2785
2786 retval = -EINVAL;
2787 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2788 __s32 width = fb->fmt.width;
2789 __s32 height = fb->fmt.height;
2790
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002791 retval = limit_scaled_size_lock(fh, &width, &height,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002792 V4L2_FIELD_INTERLACED,
2793 /* width_mask */ ~3,
2794 /* width_bias */ 2,
2795 /* adjust_size */ 0,
2796 /* adjust_crop */ 0);
2797 if (0 != retval)
2798 return retval;
2799 }
2800
2801 /* ok, accept it */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002802 btv->fbuf.base = fb->base;
2803 btv->fbuf.fmt.width = fb->fmt.width;
2804 btv->fbuf.fmt.height = fb->fmt.height;
2805 if (0 != fb->fmt.bytesperline)
2806 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2807 else
2808 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2809
2810 retval = 0;
2811 fh->ovfmt = fmt;
2812 btv->init.ovfmt = fmt;
2813 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2814 fh->ov.w.left = 0;
2815 fh->ov.w.top = 0;
2816 fh->ov.w.width = fb->fmt.width;
2817 fh->ov.w.height = fb->fmt.height;
2818 btv->init.ov.w.width = fb->fmt.width;
2819 btv->init.ov.w.height = fb->fmt.height;
2820 kfree(fh->ov.clips);
2821 fh->ov.clips = NULL;
2822 fh->ov.nclips = 0;
2823
2824 if (check_btres(fh, RESOURCE_OVERLAY)) {
2825 struct bttv_buffer *new;
2826
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002827 new = videobuf_sg_alloc(sizeof(*new));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002828 new->crop = btv->crop[!!fh->do_crop].rect;
2829 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2830 retval = bttv_switch_overlay(btv, fh, new);
2831 }
2832 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002833 return retval;
2834}
2835
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002836static int bttv_reqbufs(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002837 struct v4l2_requestbuffers *p)
2838{
2839 struct bttv_fh *fh = priv;
2840 return videobuf_reqbufs(bttv_queue(fh), p);
2841}
2842
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002843static int bttv_querybuf(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002844 struct v4l2_buffer *b)
2845{
2846 struct bttv_fh *fh = priv;
2847 return videobuf_querybuf(bttv_queue(fh), b);
2848}
2849
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002850static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002851{
2852 struct bttv_fh *fh = priv;
2853 struct bttv *btv = fh->btv;
2854 int res = bttv_resource(fh);
2855
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002856 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002857 return -EBUSY;
2858
2859 return videobuf_qbuf(bttv_queue(fh), b);
2860}
2861
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002862static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002863{
2864 struct bttv_fh *fh = priv;
2865 return videobuf_dqbuf(bttv_queue(fh), b,
2866 file->f_flags & O_NONBLOCK);
2867}
2868
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002869static int bttv_streamon(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002870 enum v4l2_buf_type type)
2871{
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 return videobuf_streamon(bttv_queue(fh));
2879}
2880
2881
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002882static int bttv_streamoff(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002883 enum v4l2_buf_type type)
2884{
2885 struct bttv_fh *fh = priv;
2886 struct bttv *btv = fh->btv;
2887 int retval;
2888 int res = bttv_resource(fh);
2889
2890
2891 retval = videobuf_streamoff(bttv_queue(fh));
2892 if (retval < 0)
2893 return retval;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002894 free_btres_lock(btv, fh, res);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002895 return 0;
2896}
2897
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002898static int bttv_queryctrl(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002899 struct v4l2_queryctrl *c)
2900{
2901 struct bttv_fh *fh = priv;
2902 struct bttv *btv = fh->btv;
2903 const struct v4l2_queryctrl *ctrl;
2904
2905 if ((c->id < V4L2_CID_BASE ||
2906 c->id >= V4L2_CID_LASTP1) &&
2907 (c->id < V4L2_CID_PRIVATE_BASE ||
2908 c->id >= V4L2_CID_PRIVATE_LASTP1))
2909 return -EINVAL;
2910
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002911 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2912 *c = no_ctl;
2913 else {
2914 ctrl = ctrl_by_id(c->id);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002915
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002916 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2917 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002918
2919 return 0;
2920}
2921
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002922static int bttv_g_parm(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002923 struct v4l2_streamparm *parm)
2924{
2925 struct bttv_fh *fh = f;
2926 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002927
Trent Piepho51f0b8d2009-03-04 01:21:02 -03002928 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2929 &parm->parm.capture.timeperframe);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002930
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002931 return 0;
2932}
2933
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002934static int bttv_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002935 struct v4l2_tuner *t)
2936{
2937 struct bttv_fh *fh = priv;
2938 struct bttv *btv = fh->btv;
2939
Trent Piephoabb03622009-01-28 21:32:59 -03002940 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002941 return -EINVAL;
2942 if (0 != t->index)
2943 return -EINVAL;
2944
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002945 t->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil859f0272009-03-28 08:29:00 -03002946 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002947 strcpy(t->name, "Television");
2948 t->capability = V4L2_TUNER_CAP_NORM;
2949 t->type = V4L2_TUNER_ANALOG_TV;
2950 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2951 t->signal = 0xffff;
2952
2953 if (btv->audio_mode_gpio)
2954 btv->audio_mode_gpio(btv, t, 0);
2955
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002956 return 0;
2957}
2958
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002959static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002960{
2961 struct bttv_fh *fh = f;
2962 struct bttv *btv = fh->btv;
2963
2964 *p = v4l2_prio_max(&btv->prio);
2965
2966 return 0;
2967}
2968
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002969static int bttv_s_priority(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002970 enum v4l2_priority prio)
2971{
2972 struct bttv_fh *fh = f;
2973 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002974 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002975
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002976 rc = v4l2_prio_change(&btv->prio, &fh->prio, prio);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002977
2978 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002979}
2980
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002981static int bttv_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002982 struct v4l2_cropcap *cap)
2983{
2984 struct bttv_fh *fh = priv;
2985 struct bttv *btv = fh->btv;
2986
2987 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2988 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2989 return -EINVAL;
2990
2991 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
2992
2993 return 0;
2994}
2995
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002996static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002997{
2998 struct bttv_fh *fh = f;
2999 struct bttv *btv = fh->btv;
3000
3001 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3002 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3003 return -EINVAL;
3004
3005 /* No fh->do_crop = 1; because btv->crop[1] may be
3006 inconsistent with fh->width or fh->height and apps
3007 do not expect a change here. */
3008
3009 crop->c = btv->crop[!!fh->do_crop].rect;
3010
3011 return 0;
3012}
3013
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003014static int bttv_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003015{
3016 struct bttv_fh *fh = f;
3017 struct bttv *btv = fh->btv;
3018 const struct v4l2_rect *b;
3019 int retval;
3020 struct bttv_crop c;
3021 __s32 b_left;
3022 __s32 b_top;
3023 __s32 b_right;
3024 __s32 b_bottom;
3025
3026 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3027 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3028 return -EINVAL;
3029
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003030 /* Make sure tvnorm, vbi_end and the current cropping
3031 parameters remain consistent until we're done. Note
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003032 read() may change vbi_end in check_alloc_btres_lock(). */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003033 retval = v4l2_prio_check(&btv->prio, fh->prio);
3034 if (0 != retval) {
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003035 return retval;
3036 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003037
3038 retval = -EBUSY;
3039
3040 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003041 return retval;
3042 }
3043
3044 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3045
3046 b_left = b->left;
3047 b_right = b_left + b->width;
3048 b_bottom = b->top + b->height;
3049
3050 b_top = max(b->top, btv->vbi_end);
3051 if (b_top + 32 >= b_bottom) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003052 return retval;
3053 }
3054
3055 /* Min. scaled size 48 x 32. */
3056 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3057 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3058
3059 c.rect.width = clamp(crop->c.width,
3060 48, b_right - c.rect.left);
3061
3062 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3063 /* Top and height must be a multiple of two. */
3064 c.rect.top = (c.rect.top + 1) & ~1;
3065
3066 c.rect.height = clamp(crop->c.height,
3067 32, b_bottom - c.rect.top);
3068 c.rect.height = (c.rect.height + 1) & ~1;
3069
3070 bttv_crop_calc_limits(&c);
3071
3072 btv->crop[1] = c;
3073
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003074 fh->do_crop = 1;
3075
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003076 if (fh->width < c.min_scaled_width) {
3077 fh->width = c.min_scaled_width;
3078 btv->init.width = c.min_scaled_width;
3079 } else if (fh->width > c.max_scaled_width) {
3080 fh->width = c.max_scaled_width;
3081 btv->init.width = c.max_scaled_width;
3082 }
3083
3084 if (fh->height < c.min_scaled_height) {
3085 fh->height = c.min_scaled_height;
3086 btv->init.height = c.min_scaled_height;
3087 } else if (fh->height > c.max_scaled_height) {
3088 fh->height = c.max_scaled_height;
3089 btv->init.height = c.max_scaled_height;
3090 }
3091
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003092 return 0;
3093}
3094
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003095static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003096{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003097 if (unlikely(a->index))
3098 return -EINVAL;
3099
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003100 strcpy(a->name, "audio");
3101 return 0;
3102}
3103
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003104static int bttv_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003105{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003106 if (unlikely(a->index))
3107 return -EINVAL;
3108
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110}
3111
3112static ssize_t bttv_read(struct file *file, char __user *data,
3113 size_t count, loff_t *ppos)
3114{
3115 struct bttv_fh *fh = file->private_data;
3116 int retval = 0;
3117
3118 if (fh->btv->errors)
3119 bttv_reinit_bt848(fh->btv);
3120 dprintk("bttv%d: read count=%d type=%s\n",
3121 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3122
3123 switch (fh->type) {
3124 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003125 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03003126 /* VIDEO_READ in use by another fh,
3127 or VIDEO_STREAM by any fh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 return -EBUSY;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3131 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003132 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 break;
3134 case V4L2_BUF_TYPE_VBI_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003135 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 return -EBUSY;
3137 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3138 file->f_flags & O_NONBLOCK);
3139 break;
3140 default:
3141 BUG();
3142 }
3143 return retval;
3144}
3145
3146static unsigned int bttv_poll(struct file *file, poll_table *wait)
3147{
3148 struct bttv_fh *fh = file->private_data;
3149 struct bttv_buffer *buf;
3150 enum v4l2_field field;
Figo.zhang9fd64182009-06-16 13:31:29 -03003151 unsigned int rc = POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003154 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return POLLERR;
3156 return videobuf_poll_stream(file, &fh->vbi, wait);
3157 }
3158
Michael Schimeke5bd0262007-01-18 16:17:39 -03003159 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 /* streaming capture */
3161 if (list_empty(&fh->cap.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -03003162 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3164 } else {
3165 /* read() capture */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 if (NULL == fh->cap.read_buf) {
3167 /* need to capture a new frame */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003168 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3169 goto err;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003170 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003171 if (NULL == fh->cap.read_buf)
3172 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3174 field = videobuf_next_field(&fh->cap);
3175 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -08003176 kfree (fh->cap.read_buf);
3177 fh->cap.read_buf = NULL;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003178 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 }
3180 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3181 fh->cap.read_off = 0;
3182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 buf = (struct bttv_buffer*)fh->cap.read_buf;
3184 }
3185
3186 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03003187 if (buf->vb.state == VIDEOBUF_DONE ||
3188 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -03003189 rc = POLLIN|POLLRDNORM;
3190 else
3191 rc = 0;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003192err:
Figo.zhang9fd64182009-06-16 13:31:29 -03003193 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194}
3195
Hans Verkuilbec43662008-12-30 06:58:20 -03003196static int bttv_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003198 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003199 struct bttv *btv = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 struct bttv_fh *fh;
3201 enum v4l2_buf_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003203 dprintk(KERN_DEBUG "bttv: open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Laurent Pinchart327ae592009-11-27 13:57:55 -03003205 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003206 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Laurent Pinchart327ae592009-11-27 13:57:55 -03003207 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003208 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3209 } else {
3210 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -03003212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
3214 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3215 btv->c.nr,v4l2_type_names[type]);
3216
3217 /* allocate per filehandle data */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003218 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3219 if (unlikely(!fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return -ENOMEM;
3221 file->private_data = fh;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003222
3223 /*
3224 * btv is protected by btv->lock mutex, while btv->init and other
3225 * streaming vars are protected by fh->cap.vb_lock. We need to take
3226 * care of both locks to avoid troubles. However, vb_lock is used also
3227 * inside videobuf, without calling buf->lock. So, it is a very bad
3228 * idea to hold both locks at the same time.
3229 * Let's first copy btv->init at fh, holding cap.vb_lock, and then work
3230 * with the rest of init, holding btv->lock.
3231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 *fh = btv->init;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 fh->type = type;
3235 fh->ov.setup_ok = 0;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003236
Hans Verkuilffb48772010-05-01 08:03:24 -03003237 v4l2_prio_open(&btv->prio, &fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003239 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3240 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3242 V4L2_FIELD_INTERLACED,
3243 sizeof(struct bttv_buffer),
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003244 fh, &btv->lock);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003245 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3246 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 V4L2_BUF_TYPE_VBI_CAPTURE,
3248 V4L2_FIELD_SEQ_TB,
3249 sizeof(struct bttv_buffer),
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003250 fh, &btv->lock);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03003251 set_tvnorm(btv,btv->tvnorm);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003252 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
3254 btv->users++;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003255
3256 /* The V4L2 spec requires one global set of cropping parameters
3257 which only change on request. These are stored in btv->crop[1].
3258 However for compatibility with V4L apps and cropping unaware
3259 V4L2 apps we now reset the cropping parameters as seen through
3260 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3261 will use btv->crop[0], the default cropping parameters for the
3262 current video standard, and VIDIOC_S_FMT will not implicitely
3263 change the cropping parameters until VIDIOC_S_CROP has been
3264 called. */
3265 fh->do_crop = !reset_crop; /* module parameter */
3266
3267 /* Likewise there should be one global set of VBI capture
3268 parameters, but for compatibility with V4L apps and earlier
3269 driver versions each fh has its own parameters. */
3270 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 bttv_field_count(btv);
3273 return 0;
3274}
3275
Hans Verkuilbec43662008-12-30 06:58:20 -03003276static int bttv_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
3278 struct bttv_fh *fh = file->private_data;
3279 struct bttv *btv = fh->btv;
3280
3281 /* turn off overlay */
3282 if (check_btres(fh, RESOURCE_OVERLAY))
3283 bttv_switch_overlay(btv,fh,NULL);
3284
3285 /* stop video capture */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003286 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 videobuf_streamoff(&fh->cap);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003288 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 }
3290 if (fh->cap.read_buf) {
3291 buffer_release(&fh->cap,fh->cap.read_buf);
3292 kfree(fh->cap.read_buf);
3293 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03003294 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003295 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
3298 /* stop vbi capture */
3299 if (check_btres(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03003300 videobuf_stop(&fh->vbi);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003301 free_btres_lock(btv,fh,RESOURCE_VBI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
3303
3304 /* free stuff */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003305
3306 /*
3307 * videobuf uses cap.vb_lock - we should avoid holding btv->lock,
3308 * otherwise we may have dead lock conditions
3309 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 videobuf_mmap_free(&fh->cap);
3311 videobuf_mmap_free(&fh->vbi);
Hans Verkuilffb48772010-05-01 08:03:24 -03003312 v4l2_prio_close(&btv->prio, fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 file->private_data = NULL;
3314 kfree(fh);
3315
3316 btv->users--;
3317 bttv_field_count(btv);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003318
3319 if (!btv->users)
3320 audio_mute(btv, 1);
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return 0;
3323}
3324
3325static int
3326bttv_mmap(struct file *file, struct vm_area_struct *vma)
3327{
3328 struct bttv_fh *fh = file->private_data;
3329
3330 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3331 fh->btv->c.nr, v4l2_type_names[fh->type],
3332 vma->vm_start, vma->vm_end - vma->vm_start);
3333 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3334}
3335
Hans Verkuilbec43662008-12-30 06:58:20 -03003336static const struct v4l2_file_operations bttv_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
Mauro Carvalho Chehab8979e9d2010-09-15 08:22:09 -03003338 .owner = THIS_MODULE,
3339 .open = bttv_open,
3340 .release = bttv_release,
3341 .unlocked_ioctl = video_ioctl2,
3342 .read = bttv_read,
3343 .mmap = bttv_mmap,
3344 .poll = bttv_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345};
3346
Hans Verkuila3998102008-07-21 02:57:38 -03003347static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003348 .vidioc_querycap = bttv_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003349 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3350 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3351 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3352 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3353 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3354 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3355 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3356 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003357 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3358 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3359 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003360 .vidioc_g_audio = bttv_g_audio,
3361 .vidioc_s_audio = bttv_s_audio,
3362 .vidioc_cropcap = bttv_cropcap,
3363 .vidioc_reqbufs = bttv_reqbufs,
3364 .vidioc_querybuf = bttv_querybuf,
3365 .vidioc_qbuf = bttv_qbuf,
3366 .vidioc_dqbuf = bttv_dqbuf,
3367 .vidioc_s_std = bttv_s_std,
3368 .vidioc_enum_input = bttv_enum_input,
3369 .vidioc_g_input = bttv_g_input,
3370 .vidioc_s_input = bttv_s_input,
3371 .vidioc_queryctrl = bttv_queryctrl,
3372 .vidioc_g_ctrl = bttv_g_ctrl,
3373 .vidioc_s_ctrl = bttv_s_ctrl,
3374 .vidioc_streamon = bttv_streamon,
3375 .vidioc_streamoff = bttv_streamoff,
3376 .vidioc_g_tuner = bttv_g_tuner,
3377 .vidioc_s_tuner = bttv_s_tuner,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003378#ifdef CONFIG_VIDEO_V4L1_COMPAT
3379 .vidiocgmbuf = vidiocgmbuf,
3380#endif
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003381 .vidioc_g_crop = bttv_g_crop,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003382 .vidioc_s_crop = bttv_s_crop,
3383 .vidioc_g_fbuf = bttv_g_fbuf,
3384 .vidioc_s_fbuf = bttv_s_fbuf,
3385 .vidioc_overlay = bttv_overlay,
3386 .vidioc_g_priority = bttv_g_priority,
3387 .vidioc_s_priority = bttv_s_priority,
3388 .vidioc_g_parm = bttv_g_parm,
3389 .vidioc_g_frequency = bttv_g_frequency,
3390 .vidioc_s_frequency = bttv_s_frequency,
3391 .vidioc_log_status = bttv_log_status,
3392 .vidioc_querystd = bttv_querystd,
Zoltan Devai43846832008-01-14 13:24:38 -03003393#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003394 .vidioc_g_register = bttv_g_register,
3395 .vidioc_s_register = bttv_s_register,
Zoltan Devai43846832008-01-14 13:24:38 -03003396#endif
Hans Verkuila3998102008-07-21 02:57:38 -03003397};
3398
3399static struct video_device bttv_video_template = {
3400 .fops = &bttv_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003401 .ioctl_ops = &bttv_ioctl_ops,
3402 .tvnorms = BTTV_NORMS,
3403 .current_norm = V4L2_STD_PAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404};
3405
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406/* ----------------------------------------------------------------------- */
3407/* radio interface */
3408
Hans Verkuilbec43662008-12-30 06:58:20 -03003409static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003411 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003412 struct bttv *btv = video_drvdata(file);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003413 struct bttv_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003415 dprintk("bttv: open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003418
3419 /* allocate per filehandle data */
3420 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003421 if (unlikely(!fh))
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003422 return -ENOMEM;
3423 file->private_data = fh;
3424 *fh = btv->init;
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003425
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003426 v4l2_prio_open(&btv->prio, &fh->prio);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 btv->radio_user++;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003429
Hans Verkuil859f0272009-03-28 08:29:00 -03003430 bttv_call_all(btv, tuner, s_radio);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03003431 audio_input(btv,TVAUDIO_INPUT_RADIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434}
3435
Hans Verkuilbec43662008-12-30 06:58:20 -03003436static int radio_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003438 struct bttv_fh *fh = file->private_data;
3439 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003440 struct rds_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Hans Verkuilffb48772010-05-01 08:03:24 -03003442 v4l2_prio_close(&btv->prio, fh->prio);
Robert Fitzsimonsb9bc07a2008-04-10 09:40:31 -03003443 file->private_data = NULL;
3444 kfree(fh);
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 btv->radio_user--;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003447
Hans Verkuil859f0272009-03-28 08:29:00 -03003448 bttv_call_all(btv, core, ioctl, RDS_CMD_CLOSE, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 return 0;
3451}
3452
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003453static int radio_querycap(struct file *file, void *priv,
3454 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003456 struct bttv_fh *fh = priv;
3457 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003459 strcpy(cap->driver, "bttv");
3460 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
3461 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
3462 cap->version = BTTV_VERSION_CODE;
3463 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return 0;
3466}
3467
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003468static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003470 struct bttv_fh *fh = priv;
3471 struct bttv *btv = fh->btv;
3472
Trent Piephoabb03622009-01-28 21:32:59 -03003473 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003474 return -EINVAL;
3475 if (0 != t->index)
3476 return -EINVAL;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003477 strcpy(t->name, "Radio");
3478 t->type = V4L2_TUNER_RADIO;
3479
Hans Verkuil859f0272009-03-28 08:29:00 -03003480 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003481
3482 if (btv->audio_mode_gpio)
3483 btv->audio_mode_gpio(btv, t, 0);
3484
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003485 return 0;
3486}
3487
3488static int radio_enum_input(struct file *file, void *priv,
3489 struct v4l2_input *i)
3490{
3491 if (i->index != 0)
3492 return -EINVAL;
3493
3494 strcpy(i->name, "Radio");
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003495 i->type = V4L2_INPUT_TYPE_TUNER;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003496
3497 return 0;
3498}
3499
3500static int radio_g_audio(struct file *file, void *priv,
3501 struct v4l2_audio *a)
3502{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003503 if (unlikely(a->index))
Cyrill Gorcunov1a002eb2008-04-01 17:49:13 -03003504 return -EINVAL;
3505
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003506 strcpy(a->name, "Radio");
Cyrill Gorcunov1a002eb2008-04-01 17:49:13 -03003507
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003508 return 0;
3509}
3510
3511static int radio_s_tuner(struct file *file, void *priv,
3512 struct v4l2_tuner *t)
3513{
3514 struct bttv_fh *fh = priv;
3515 struct bttv *btv = fh->btv;
3516
3517 if (0 != t->index)
3518 return -EINVAL;
3519
Hans Verkuil859f0272009-03-28 08:29:00 -03003520 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003521 return 0;
3522}
3523
3524static int radio_s_audio(struct file *file, void *priv,
3525 struct v4l2_audio *a)
3526{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003527 if (unlikely(a->index))
3528 return -EINVAL;
3529
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003530 return 0;
3531}
3532
3533static int radio_s_input(struct file *filp, void *priv, unsigned int i)
3534{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003535 if (unlikely(i))
3536 return -EINVAL;
3537
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003538 return 0;
3539}
3540
3541static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
3542{
3543 return 0;
3544}
3545
3546static int radio_queryctrl(struct file *file, void *priv,
3547 struct v4l2_queryctrl *c)
3548{
3549 const struct v4l2_queryctrl *ctrl;
3550
3551 if (c->id < V4L2_CID_BASE ||
3552 c->id >= V4L2_CID_LASTP1)
3553 return -EINVAL;
3554
3555 if (c->id == V4L2_CID_AUDIO_MUTE) {
3556 ctrl = ctrl_by_id(c->id);
3557 *c = *ctrl;
3558 } else
3559 *c = no_ctl;
3560
3561 return 0;
3562}
3563
3564static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
3565{
3566 *i = 0;
3567 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003570static ssize_t radio_read(struct file *file, char __user *data,
3571 size_t count, loff_t *ppos)
3572{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003573 struct bttv_fh *fh = file->private_data;
3574 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003575 struct rds_command cmd;
3576 cmd.block_count = count/3;
3577 cmd.buffer = data;
3578 cmd.instance = file;
3579 cmd.result = -ENODEV;
3580
Hans Verkuil859f0272009-03-28 08:29:00 -03003581 bttv_call_all(btv, core, ioctl, RDS_CMD_READ, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003582
3583 return cmd.result;
3584}
3585
3586static unsigned int radio_poll(struct file *file, poll_table *wait)
3587{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003588 struct bttv_fh *fh = file->private_data;
3589 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003590 struct rds_command cmd;
3591 cmd.instance = file;
3592 cmd.event_list = wait;
3593 cmd.result = -ENODEV;
Hans Verkuil859f0272009-03-28 08:29:00 -03003594 bttv_call_all(btv, core, ioctl, RDS_CMD_POLL, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003595
3596 return cmd.result;
3597}
3598
Hans Verkuilbec43662008-12-30 06:58:20 -03003599static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
3601 .owner = THIS_MODULE,
3602 .open = radio_open,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003603 .read = radio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 .release = radio_release,
Mauro Carvalho Chehab587f0d52010-12-15 18:45:42 -03003605 .unlocked_ioctl = video_ioctl2,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003606 .poll = radio_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607};
3608
Hans Verkuila3998102008-07-21 02:57:38 -03003609static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003610 .vidioc_querycap = radio_querycap,
3611 .vidioc_g_tuner = radio_g_tuner,
3612 .vidioc_enum_input = radio_enum_input,
3613 .vidioc_g_audio = radio_g_audio,
3614 .vidioc_s_tuner = radio_s_tuner,
3615 .vidioc_s_audio = radio_s_audio,
3616 .vidioc_s_input = radio_s_input,
3617 .vidioc_s_std = radio_s_std,
3618 .vidioc_queryctrl = radio_queryctrl,
3619 .vidioc_g_input = radio_g_input,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003620 .vidioc_g_ctrl = bttv_g_ctrl,
3621 .vidioc_s_ctrl = bttv_s_ctrl,
3622 .vidioc_g_frequency = bttv_g_frequency,
3623 .vidioc_s_frequency = bttv_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624};
3625
Hans Verkuila3998102008-07-21 02:57:38 -03003626static struct video_device radio_template = {
3627 .fops = &radio_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003628 .ioctl_ops = &radio_ioctl_ops,
3629};
3630
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631/* ----------------------------------------------------------------------- */
3632/* some debug code */
3633
Adrian Bunk408b6642005-05-01 08:59:29 -07003634static int bttv_risc_decode(u32 risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
3636 static char *instr[16] = {
3637 [ BT848_RISC_WRITE >> 28 ] = "write",
3638 [ BT848_RISC_SKIP >> 28 ] = "skip",
3639 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3640 [ BT848_RISC_JUMP >> 28 ] = "jump",
3641 [ BT848_RISC_SYNC >> 28 ] = "sync",
3642 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3643 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3644 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3645 };
3646 static int incr[16] = {
3647 [ BT848_RISC_WRITE >> 28 ] = 2,
3648 [ BT848_RISC_JUMP >> 28 ] = 2,
3649 [ BT848_RISC_SYNC >> 28 ] = 2,
3650 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3651 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3652 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3653 };
3654 static char *bits[] = {
3655 "be0", "be1", "be2", "be3/resync",
3656 "set0", "set1", "set2", "set3",
3657 "clr0", "clr1", "clr2", "clr3",
3658 "irq", "res", "eol", "sol",
3659 };
3660 int i;
3661
3662 printk("0x%08x [ %s", risc,
3663 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3664 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3665 if (risc & (1 << (i + 12)))
3666 printk(" %s",bits[i]);
3667 printk(" count=%d ]\n", risc & 0xfff);
3668 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3669}
3670
Adrian Bunk408b6642005-05-01 08:59:29 -07003671static void bttv_risc_disasm(struct bttv *btv,
3672 struct btcx_riscmem *risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
3674 unsigned int i,j,n;
3675
3676 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003677 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 for (i = 0; i < (risc->size >> 2); i += n) {
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003679 printk("%s: 0x%lx: ", btv->c.v4l2_dev.name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 (unsigned long)(risc->dma + (i<<2)));
Al Viro3aa71102008-06-22 14:20:09 -03003681 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 for (j = 1; j < n; j++)
3683 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003684 btv->c.v4l2_dev.name, (unsigned long)(risc->dma + ((i+j)<<2)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 risc->cpu[i+j], j);
3686 if (0 == risc->cpu[i])
3687 break;
3688 }
3689}
3690
3691static void bttv_print_riscaddr(struct bttv *btv)
3692{
3693 printk(" main: %08Lx\n",
3694 (unsigned long long)btv->main.dma);
3695 printk(" vbi : o=%08Lx e=%08Lx\n",
3696 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3697 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3698 printk(" cap : o=%08Lx e=%08Lx\n",
3699 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3700 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3701 printk(" scr : o=%08Lx e=%08Lx\n",
3702 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3703 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3704 bttv_risc_disasm(btv, &btv->main);
3705}
3706
3707/* ----------------------------------------------------------------------- */
3708/* irq handler */
3709
3710static char *irq_name[] = {
3711 "FMTCHG", // format change detected (525 vs. 625)
3712 "VSYNC", // vertical sync (new field)
3713 "HSYNC", // horizontal sync
3714 "OFLOW", // chroma/luma AGC overflow
3715 "HLOCK", // horizontal lock changed
3716 "VPRES", // video presence changed
3717 "6", "7",
3718 "I2CDONE", // hw irc operation finished
3719 "GPINT", // gpio port triggered irq
3720 "10",
3721 "RISCI", // risc instruction triggered irq
3722 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3723 "FTRGT", // pixel data fifo overrun
3724 "FDSR", // fifo data stream resyncronisation
3725 "PPERR", // parity error (data transfer)
3726 "RIPERR", // parity error (read risc instructions)
3727 "PABORT", // pci abort
3728 "OCERR", // risc instruction error
3729 "SCERR", // syncronisation error
3730};
3731
3732static void bttv_print_irqbits(u32 print, u32 mark)
3733{
3734 unsigned int i;
3735
3736 printk("bits:");
3737 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3738 if (print & (1 << i))
3739 printk(" %s",irq_name[i]);
3740 if (mark & (1 << i))
3741 printk("*");
3742 }
3743}
3744
3745static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3746{
3747 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3748 btv->c.nr,
3749 (unsigned long)btv->main.dma,
Al Viro3aa71102008-06-22 14:20:09 -03003750 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3751 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 (unsigned long)rc);
3753
3754 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3755 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3756 "Ok, then this is harmless, don't worry ;)\n",
3757 btv->c.nr);
3758 return;
3759 }
3760 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3761 btv->c.nr);
3762 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3763 btv->c.nr);
3764 dump_stack();
3765}
3766
3767static int
3768bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3769{
3770 struct bttv_buffer *item;
3771
3772 memset(set,0,sizeof(*set));
3773
3774 /* capture request ? */
3775 if (!list_empty(&btv->capture)) {
3776 set->frame_irq = 1;
3777 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3778 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3779 set->top = item;
3780 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3781 set->bottom = item;
3782
3783 /* capture request for other field ? */
3784 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3785 (item->vb.queue.next != &btv->capture)) {
3786 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
Mike Isely66349b42009-09-21 12:09:08 -03003787 /* Mike Isely <isely@pobox.com> - Only check
3788 * and set up the bottom field in the logic
3789 * below. Don't ever do the top field. This
3790 * of course means that if we set up the
3791 * bottom field in the above code that we'll
3792 * actually skip a field. But that's OK.
3793 * Having processed only a single buffer this
3794 * time, then the next time around the first
3795 * available buffer should be for a top field.
3796 * That will then cause us here to set up a
3797 * top then a bottom field in the normal way.
3798 * The alternative to this understanding is
3799 * that we set up the second available buffer
3800 * as a top field, but that's out of order
3801 * since this driver always processes the top
3802 * field first - the effect will be the two
3803 * buffers being returned in the wrong order,
3804 * with the second buffer also being delayed
3805 * by one field time (owing to the fifo nature
3806 * of videobuf). Worse still, we'll be stuck
3807 * doing fields out of order now every time
3808 * until something else causes a field to be
3809 * dropped. By effectively forcing a field to
3810 * drop this way then we always get back into
3811 * sync within a single frame time. (Out of
3812 * order fields can screw up deinterlacing
3813 * algorithms.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 if (NULL == set->bottom &&
3816 V4L2_FIELD_BOTTOM == item->vb.field) {
3817 set->bottom = item;
3818 }
3819 if (NULL != set->top && NULL != set->bottom)
3820 set->top_irq = 2;
3821 }
3822 }
3823 }
3824
3825 /* screen overlay ? */
3826 if (NULL != btv->screen) {
3827 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3828 if (NULL == set->top && NULL == set->bottom) {
3829 set->top = btv->screen;
3830 set->bottom = btv->screen;
3831 }
3832 } else {
3833 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3834 NULL == set->top) {
3835 set->top = btv->screen;
3836 }
3837 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3838 NULL == set->bottom) {
3839 set->bottom = btv->screen;
3840 }
3841 }
3842 }
3843
3844 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3845 btv->c.nr,set->top, set->bottom,
3846 btv->screen,set->frame_irq,set->top_irq);
3847 return 0;
3848}
3849
3850static void
3851bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3852 struct bttv_buffer_set *curr, unsigned int state)
3853{
3854 struct timeval ts;
3855
3856 do_gettimeofday(&ts);
3857
3858 if (wakeup->top == wakeup->bottom) {
3859 if (NULL != wakeup->top && curr->top != wakeup->top) {
3860 if (irq_debug > 1)
3861 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3862 wakeup->top->vb.ts = ts;
3863 wakeup->top->vb.field_count = btv->field_count;
3864 wakeup->top->vb.state = state;
3865 wake_up(&wakeup->top->vb.done);
3866 }
3867 } else {
3868 if (NULL != wakeup->top && curr->top != wakeup->top) {
3869 if (irq_debug > 1)
3870 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3871 wakeup->top->vb.ts = ts;
3872 wakeup->top->vb.field_count = btv->field_count;
3873 wakeup->top->vb.state = state;
3874 wake_up(&wakeup->top->vb.done);
3875 }
3876 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3877 if (irq_debug > 1)
3878 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3879 wakeup->bottom->vb.ts = ts;
3880 wakeup->bottom->vb.field_count = btv->field_count;
3881 wakeup->bottom->vb.state = state;
3882 wake_up(&wakeup->bottom->vb.done);
3883 }
3884 }
3885}
3886
3887static void
3888bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3889 unsigned int state)
3890{
3891 struct timeval ts;
3892
3893 if (NULL == wakeup)
3894 return;
3895
3896 do_gettimeofday(&ts);
3897 wakeup->vb.ts = ts;
3898 wakeup->vb.field_count = btv->field_count;
3899 wakeup->vb.state = state;
3900 wake_up(&wakeup->vb.done);
3901}
3902
3903static void bttv_irq_timeout(unsigned long data)
3904{
3905 struct bttv *btv = (struct bttv *)data;
3906 struct bttv_buffer_set old,new;
3907 struct bttv_buffer *ovbi;
3908 struct bttv_buffer *item;
3909 unsigned long flags;
3910
3911 if (bttv_verbose) {
3912 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3913 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3914 btread(BT848_RISC_COUNT));
3915 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3916 printk("\n");
3917 }
3918
3919 spin_lock_irqsave(&btv->s_lock,flags);
3920
3921 /* deactivate stuff */
3922 memset(&new,0,sizeof(new));
3923 old = btv->curr;
3924 ovbi = btv->cvbi;
3925 btv->curr = new;
3926 btv->cvbi = NULL;
3927 btv->loop_irq = 0;
3928 bttv_buffer_activate_video(btv, &new);
3929 bttv_buffer_activate_vbi(btv, NULL);
3930 bttv_set_dma(btv, 0);
3931
3932 /* wake up */
Brandon Philips0fc06862007-11-06 20:02:36 -03003933 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3934 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
3936 /* cancel all outstanding capture / vbi requests */
3937 while (!list_empty(&btv->capture)) {
3938 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3939 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003940 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 wake_up(&item->vb.done);
3942 }
3943 while (!list_empty(&btv->vcapture)) {
3944 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3945 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003946 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 wake_up(&item->vb.done);
3948 }
3949
3950 btv->errors++;
3951 spin_unlock_irqrestore(&btv->s_lock,flags);
3952}
3953
3954static void
3955bttv_irq_wakeup_top(struct bttv *btv)
3956{
3957 struct bttv_buffer *wakeup = btv->curr.top;
3958
3959 if (NULL == wakeup)
3960 return;
3961
3962 spin_lock(&btv->s_lock);
3963 btv->curr.top_irq = 0;
3964 btv->curr.top = NULL;
3965 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3966
3967 do_gettimeofday(&wakeup->vb.ts);
3968 wakeup->vb.field_count = btv->field_count;
Brandon Philips0fc06862007-11-06 20:02:36 -03003969 wakeup->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 wake_up(&wakeup->vb.done);
3971 spin_unlock(&btv->s_lock);
3972}
3973
3974static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3975{
3976 if (rc < risc->dma)
3977 return 0;
3978 if (rc > risc->dma + risc->size)
3979 return 0;
3980 return 1;
3981}
3982
3983static void
3984bttv_irq_switch_video(struct bttv *btv)
3985{
3986 struct bttv_buffer_set new;
3987 struct bttv_buffer_set old;
3988 dma_addr_t rc;
3989
3990 spin_lock(&btv->s_lock);
3991
3992 /* new buffer set */
3993 bttv_irq_next_video(btv, &new);
3994 rc = btread(BT848_RISC_COUNT);
3995 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3996 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3997 btv->framedrop++;
3998 if (debug_latency)
3999 bttv_irq_debug_low_latency(btv, rc);
4000 spin_unlock(&btv->s_lock);
4001 return;
4002 }
4003
4004 /* switch over */
4005 old = btv->curr;
4006 btv->curr = new;
4007 btv->loop_irq &= ~1;
4008 bttv_buffer_activate_video(btv, &new);
4009 bttv_set_dma(btv, 0);
4010
4011 /* switch input */
4012 if (UNSET != btv->new_input) {
4013 video_mux(btv,btv->new_input);
4014 btv->new_input = UNSET;
4015 }
4016
4017 /* wake up finished buffers */
Brandon Philips0fc06862007-11-06 20:02:36 -03004018 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 spin_unlock(&btv->s_lock);
4020}
4021
4022static void
4023bttv_irq_switch_vbi(struct bttv *btv)
4024{
4025 struct bttv_buffer *new = NULL;
4026 struct bttv_buffer *old;
4027 u32 rc;
4028
4029 spin_lock(&btv->s_lock);
4030
4031 if (!list_empty(&btv->vcapture))
4032 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4033 old = btv->cvbi;
4034
4035 rc = btread(BT848_RISC_COUNT);
4036 if (NULL != old && (is_active(&old->top, rc) ||
4037 is_active(&old->bottom, rc))) {
4038 btv->framedrop++;
4039 if (debug_latency)
4040 bttv_irq_debug_low_latency(btv, rc);
4041 spin_unlock(&btv->s_lock);
4042 return;
4043 }
4044
4045 /* switch */
4046 btv->cvbi = new;
4047 btv->loop_irq &= ~4;
4048 bttv_buffer_activate_vbi(btv, new);
4049 bttv_set_dma(btv, 0);
4050
Brandon Philips0fc06862007-11-06 20:02:36 -03004051 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 spin_unlock(&btv->s_lock);
4053}
4054
David Howells7d12e782006-10-05 14:55:46 +01004055static irqreturn_t bttv_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
4057 u32 stat,astat;
4058 u32 dstat;
4059 int count;
4060 struct bttv *btv;
4061 int handled = 0;
4062
4063 btv=(struct bttv *)dev_id;
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004064
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004065 if (btv->custom_irq)
4066 handled = btv->custom_irq(btv);
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004067
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 count=0;
4069 while (1) {
4070 /* get/clear interrupt status bits */
4071 stat=btread(BT848_INT_STAT);
4072 astat=stat&btread(BT848_INT_MASK);
4073 if (!astat)
4074 break;
4075 handled = 1;
4076 btwrite(stat,BT848_INT_STAT);
4077
4078 /* get device status bits */
4079 dstat=btread(BT848_DSTATUS);
4080
4081 if (irq_debug) {
4082 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4083 "riscs=%x, riscc=%08x, ",
4084 btv->c.nr, count, btv->field_count,
4085 stat>>28, btread(BT848_RISC_COUNT));
4086 bttv_print_irqbits(stat,astat);
4087 if (stat & BT848_INT_HLOCK)
4088 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4089 ? "yes" : "no");
4090 if (stat & BT848_INT_VPRES)
4091 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4092 ? "yes" : "no");
4093 if (stat & BT848_INT_FMTCHG)
4094 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4095 ? "625" : "525");
4096 printk("\n");
4097 }
4098
4099 if (astat&BT848_INT_VSYNC)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004100 btv->field_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004102 if ((astat & BT848_INT_GPINT) && btv->remote) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004104 bttv_input_irq(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 }
4106
4107 if (astat & BT848_INT_I2CDONE) {
4108 btv->i2c_done = stat;
4109 wake_up(&btv->i2c_queue);
4110 }
4111
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004112 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 bttv_irq_switch_vbi(btv);
4114
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004115 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 bttv_irq_wakeup_top(btv);
4117
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004118 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 bttv_irq_switch_video(btv);
4120
4121 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004122 audio_mute(btv, btv->mute); /* trigger automute */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
4124 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4125 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4126 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4127 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4128 btread(BT848_RISC_COUNT));
4129 bttv_print_irqbits(stat,astat);
4130 printk("\n");
4131 if (bttv_debug)
4132 bttv_print_riscaddr(btv);
4133 }
4134 if (fdsr && astat & BT848_INT_FDSR) {
4135 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4136 btv->c.nr,btread(BT848_RISC_COUNT));
4137 if (bttv_debug)
4138 bttv_print_riscaddr(btv);
4139 }
4140
4141 count++;
4142 if (count > 4) {
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004143
4144 if (count > 8 || !(astat & BT848_INT_GPINT)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004145 btwrite(0, BT848_INT_MASK);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004146
4147 printk(KERN_ERR
4148 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4149 } else {
4150 printk(KERN_ERR
4151 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4152
4153 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4154 BT848_INT_MASK);
4155 };
4156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 bttv_print_irqbits(stat,astat);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 printk("]\n");
4160 }
4161 }
4162 btv->irq_total++;
4163 if (handled)
4164 btv->irq_me++;
4165 return IRQ_RETVAL(handled);
4166}
4167
4168
4169/* ----------------------------------------------------------------------- */
4170/* initialitation */
4171
4172static struct video_device *vdev_init(struct bttv *btv,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004173 const struct video_device *template,
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004174 const char *type_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175{
4176 struct video_device *vfd;
4177
4178 vfd = video_device_alloc();
4179 if (NULL == vfd)
4180 return NULL;
4181 *vfd = *template;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004182 vfd->v4l2_dev = &btv->c.v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 vfd->release = video_device_release;
Mauro Carvalho Chehab1d0a4362008-06-23 12:31:29 -03004184 vfd->debug = bttv_debug;
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004185 video_set_drvdata(vfd, btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4187 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004188 type_name, bttv_tvcards[btv->c.type].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return vfd;
4190}
4191
4192static void bttv_unregister_video(struct bttv *btv)
4193{
4194 if (btv->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004195 if (video_is_registered(btv->video_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 video_unregister_device(btv->video_dev);
4197 else
4198 video_device_release(btv->video_dev);
4199 btv->video_dev = NULL;
4200 }
4201 if (btv->vbi_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004202 if (video_is_registered(btv->vbi_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 video_unregister_device(btv->vbi_dev);
4204 else
4205 video_device_release(btv->vbi_dev);
4206 btv->vbi_dev = NULL;
4207 }
4208 if (btv->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004209 if (video_is_registered(btv->radio_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 video_unregister_device(btv->radio_dev);
4211 else
4212 video_device_release(btv->radio_dev);
4213 btv->radio_dev = NULL;
4214 }
4215}
4216
4217/* register video4linux devices */
4218static int __devinit bttv_register_video(struct bttv *btv)
4219{
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004220 if (no_overlay > 0)
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004221 printk("bttv: Overlay support disabled.\n");
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 /* video */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004224 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004225
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004226 if (NULL == btv->video_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004228 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
4229 video_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 goto err;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03004231 printk(KERN_INFO "bttv%d: registered device %s\n",
4232 btv->c.nr, video_device_node_name(btv->video_dev));
Hans Verkuil22a04f12008-07-20 06:35:02 -03004233 if (device_create_file(&btv->video_dev->dev,
Kay Sievers54bd5b62007-10-08 16:26:13 -03004234 &dev_attr_card)<0) {
4235 printk(KERN_ERR "bttv%d: device_create_file 'card' "
Trent Piephod94fc9a2006-07-29 17:18:06 -03004236 "failed\n", btv->c.nr);
4237 goto err;
4238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
4240 /* vbi */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004241 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004242
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004243 if (NULL == btv->vbi_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004245 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
4246 vbi_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 goto err;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03004248 printk(KERN_INFO "bttv%d: registered device %s\n",
4249 btv->c.nr, video_device_node_name(btv->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004251 if (!btv->has_radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 return 0;
4253 /* radio */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004254 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004255 if (NULL == btv->radio_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004257 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4258 radio_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 goto err;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03004260 printk(KERN_INFO "bttv%d: registered device %s\n",
4261 btv->c.nr, video_device_node_name(btv->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
4263 /* all done */
4264 return 0;
4265
4266 err:
4267 bttv_unregister_video(btv);
4268 return -1;
4269}
4270
4271
4272/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4273/* response on cards with no firmware is not enabled by OF */
4274static void pci_set_command(struct pci_dev *dev)
4275{
4276#if defined(__powerpc__)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004277 unsigned int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004279 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4280 cmd = (cmd | PCI_COMMAND_MEMORY );
4281 pci_write_config_dword(dev, PCI_COMMAND, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282#endif
4283}
4284
4285static int __devinit bttv_probe(struct pci_dev *dev,
4286 const struct pci_device_id *pci_id)
4287{
4288 int result;
4289 unsigned char lat;
4290 struct bttv *btv;
4291
4292 if (bttv_num == BTTV_MAX)
4293 return -ENOMEM;
4294 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004295 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004296 if (btv == NULL) {
4297 printk(KERN_ERR "bttv: out of memory.\n");
4298 return -ENOMEM;
4299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 btv->c.nr = bttv_num;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004301 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4302 "bttv%d", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
4304 /* initialize structs / fill in defaults */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02004305 mutex_init(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004306 spin_lock_init(&btv->s_lock);
4307 spin_lock_init(&btv->gpio_lock);
4308 init_waitqueue_head(&btv->gpioq);
4309 init_waitqueue_head(&btv->i2c_queue);
4310 INIT_LIST_HEAD(&btv->c.subs);
4311 INIT_LIST_HEAD(&btv->capture);
4312 INIT_LIST_HEAD(&btv->vcapture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 v4l2_prio_init(&btv->prio);
4314
4315 init_timer(&btv->timeout);
4316 btv->timeout.function = bttv_irq_timeout;
4317 btv->timeout.data = (unsigned long)btv;
4318
Michael Krufky7c08fb02005-11-08 21:36:21 -08004319 btv->i2c_rc = -1;
4320 btv->tuner_type = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 btv->new_input = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 btv->has_radio=radio[btv->c.nr];
4323
4324 /* pci stuff (init, get irq/mmio, ... */
4325 btv->c.pci = dev;
Michael Krufky7c08fb02005-11-08 21:36:21 -08004326 btv->id = dev->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 if (pci_enable_device(dev)) {
Michael Krufky7c08fb02005-11-08 21:36:21 -08004328 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 btv->c.nr);
4330 return -EIO;
4331 }
Yang Hongyang284901a2009-04-06 19:01:15 -07004332 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004333 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 btv->c.nr);
4335 return -EIO;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 if (!request_mem_region(pci_resource_start(dev,0),
4338 pci_resource_len(dev,0),
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004339 btv->c.v4l2_dev.name)) {
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004340 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4341 btv->c.nr,
4342 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return -EBUSY;
4344 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004345 pci_set_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 pci_set_command(dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004347
4348 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4349 if (result < 0) {
4350 printk(KERN_WARNING "bttv%d: v4l2_device_register() failed\n", btv->c.nr);
4351 goto fail0;
4352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004354 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4355 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4356 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4357 bttv_num,btv->id, btv->revision, pci_name(dev));
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004358 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4359 btv->c.pci->irq, lat,
4360 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 schedule();
4362
Akinobu Mita5f1693f2006-12-20 10:08:56 -03004363 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4364 if (NULL == btv->bt848_mmio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4366 result = -EIO;
4367 goto fail1;
4368 }
4369
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004370 /* identify card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 bttv_idcard(btv);
4372
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004373 /* disable irqs, register irq handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 btwrite(0, BT848_INT_MASK);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004375 result = request_irq(btv->c.pci->irq, bttv_irq,
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004376 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004377 if (result < 0) {
4378 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 bttv_num,btv->c.pci->irq);
4380 goto fail1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
4383 if (0 != bttv_handle_chipset(btv)) {
4384 result = -EIO;
4385 goto fail2;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
4388 /* init options from insmod args */
4389 btv->opt_combfilter = combfilter;
4390 btv->opt_lumafilter = lumafilter;
4391 btv->opt_automute = automute;
4392 btv->opt_chroma_agc = chroma_agc;
4393 btv->opt_adc_crush = adc_crush;
4394 btv->opt_vcr_hack = vcr_hack;
4395 btv->opt_whitecrush_upper = whitecrush_upper;
4396 btv->opt_whitecrush_lower = whitecrush_lower;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07004397 btv->opt_uv_ratio = uv_ratio;
4398 btv->opt_full_luma_range = full_luma_range;
4399 btv->opt_coring = coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
4401 /* fill struct bttv with some useful defaults */
4402 btv->init.btv = btv;
4403 btv->init.ov.w.width = 320;
4404 btv->init.ov.w.height = 240;
Mauro Carvalho Chehabc96dd072007-10-26 16:51:47 -03004405 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 btv->init.width = 320;
4407 btv->init.height = 240;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 btv->input = 0;
4409
4410 /* initialize hardware */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004411 if (bttv_gpio)
4412 bttv_gpio_tracking(btv,"pre-init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
4414 bttv_risc_init_main(btv);
4415 init_bt848(btv);
4416
4417 /* gpio */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004418 btwrite(0x00, BT848_GPIO_REG_INP);
4419 btwrite(0x00, BT848_GPIO_OUT_EN);
4420 if (bttv_verbose)
4421 bttv_gpio_tracking(btv,"init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004423 /* needs to be done before i2c is registered */
4424 bttv_init_card1(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004426 /* register i2c + gpio */
4427 init_bttv_i2c(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004429 /* some card-specific stuff (needs working i2c) */
4430 bttv_init_card2(btv);
Hans Verkuil2c905772009-07-20 08:14:17 -03004431 bttv_init_tuner(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 init_irqreg(btv);
4433
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004434 /* register video4linux + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 if (!bttv_tvcards[btv->c.type].no_video) {
4436 bttv_register_video(btv);
4437 bt848_bright(btv,32768);
4438 bt848_contrast(btv,32768);
4439 bt848_hue(btv,32768);
4440 bt848_sat(btv,32768);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004441 audio_mute(btv, 1);
Trent Piepho333408f2007-07-03 15:08:10 -03004442 set_input(btv, 0, btv->tvnorm);
Michael Schimeke5bd0262007-01-18 16:17:39 -03004443 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4444 btv->crop[1] = btv->crop[0]; /* current = default */
4445 disclaim_vbi_lines(btv);
4446 disclaim_video_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 }
4448
Jarod Wilsonf992a492007-03-24 15:23:50 -03004449 /* add subdevices and autoload dvb-bt8xx if needed */
4450 if (bttv_tvcards[btv->c.type].has_dvb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 bttv_sub_add_device(&btv->c, "dvb");
Jarod Wilsonf992a492007-03-24 15:23:50 -03004452 request_modules(btv);
4453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Jean Delvared90a4ae2010-02-16 14:22:37 -03004455 if (!disable_ir) {
4456 init_bttv_i2c_ir(btv);
4457 bttv_input_init(btv);
4458 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 /* everything is fine */
4461 bttv_num++;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004464fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004465 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004467fail1:
4468 v4l2_device_unregister(&btv->c.v4l2_dev);
4469
4470fail0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 if (btv->bt848_mmio)
4472 iounmap(btv->bt848_mmio);
4473 release_mem_region(pci_resource_start(btv->c.pci,0),
4474 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 return result;
4476}
4477
4478static void __devexit bttv_remove(struct pci_dev *pci_dev)
4479{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004480 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4481 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
4483 if (bttv_verbose)
4484 printk("bttv%d: unloading\n",btv->c.nr);
4485
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004486 /* shutdown everything (DMA+IRQs) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 btand(~15, BT848_GPIO_DMA_CTL);
4488 btwrite(0, BT848_INT_MASK);
4489 btwrite(~0x0, BT848_INT_STAT);
4490 btwrite(0x0, BT848_GPIO_OUT_EN);
4491 if (bttv_gpio)
4492 bttv_gpio_tracking(btv,"cleanup");
4493
4494 /* tell gpio modules we are leaving ... */
4495 btv->shutdown=1;
4496 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004497 bttv_input_fini(btv);
Christopher Pascoe889aee82006-01-09 15:25:28 -02004498 bttv_sub_del_devices(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004500 /* unregister i2c_bus + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 fini_bttv_i2c(btv);
4502
4503 /* unregister video4linux */
4504 bttv_unregister_video(btv);
4505
4506 /* free allocated memory */
4507 btcx_riscmem_free(btv->c.pci,&btv->main);
4508
4509 /* free ressources */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004510 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 iounmap(btv->bt848_mmio);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004512 release_mem_region(pci_resource_start(btv->c.pci,0),
4513 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004515 v4l2_device_unregister(&btv->c.v4l2_dev);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004516 bttvs[btv->c.nr] = NULL;
4517 kfree(btv);
4518
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004519 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520}
4521
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004522#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4524{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004525 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4526 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 struct bttv_buffer_set idle;
4528 unsigned long flags;
4529
Mauro Carvalho Chehab0f97a932005-09-09 13:04:05 -07004530 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
4532 /* stop dma + irqs */
4533 spin_lock_irqsave(&btv->s_lock,flags);
4534 memset(&idle, 0, sizeof(idle));
4535 btv->state.video = btv->curr;
4536 btv->state.vbi = btv->cvbi;
4537 btv->state.loop_irq = btv->loop_irq;
4538 btv->curr = idle;
4539 btv->loop_irq = 0;
4540 bttv_buffer_activate_video(btv, &idle);
4541 bttv_buffer_activate_vbi(btv, NULL);
4542 bttv_set_dma(btv, 0);
4543 btwrite(0, BT848_INT_MASK);
4544 spin_unlock_irqrestore(&btv->s_lock,flags);
4545
4546 /* save bt878 state */
4547 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4548 btv->state.gpio_data = gpio_read();
4549
4550 /* save pci state */
4551 pci_save_state(pci_dev);
4552 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4553 pci_disable_device(pci_dev);
4554 btv->state.disabled = 1;
4555 }
4556 return 0;
4557}
4558
4559static int bttv_resume(struct pci_dev *pci_dev)
4560{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004561 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4562 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004564 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
4566 dprintk("bttv%d: resume\n", btv->c.nr);
4567
4568 /* restore pci state */
4569 if (btv->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004570 err=pci_enable_device(pci_dev);
4571 if (err) {
4572 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4573 btv->c.nr);
4574 return err;
4575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 btv->state.disabled = 0;
4577 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004578 err=pci_set_power_state(pci_dev, PCI_D0);
4579 if (err) {
4580 pci_disable_device(pci_dev);
4581 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4582 btv->c.nr);
4583 btv->state.disabled = 1;
4584 return err;
4585 }
4586
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 pci_restore_state(pci_dev);
4588
4589 /* restore bt878 state */
4590 bttv_reinit_bt848(btv);
4591 gpio_inout(0xffffff, btv->state.gpio_enable);
4592 gpio_write(btv->state.gpio_data);
4593
4594 /* restart dma */
4595 spin_lock_irqsave(&btv->s_lock,flags);
4596 btv->curr = btv->state.video;
4597 btv->cvbi = btv->state.vbi;
4598 btv->loop_irq = btv->state.loop_irq;
4599 bttv_buffer_activate_video(btv, &btv->curr);
4600 bttv_buffer_activate_vbi(btv, btv->cvbi);
4601 bttv_set_dma(btv, 0);
4602 spin_unlock_irqrestore(&btv->s_lock,flags);
4603 return 0;
4604}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004605#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
4607static struct pci_device_id bttv_pci_tbl[] = {
Joe Perches76e97412009-07-05 15:59:21 -03004608 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4609 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4610 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4611 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004612 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613};
4614
4615MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4616
4617static struct pci_driver bttv_pci_driver = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004618 .name = "bttv",
4619 .id_table = bttv_pci_tbl,
4620 .probe = bttv_probe,
4621 .remove = __devexit_p(bttv_remove),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004622#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 .suspend = bttv_suspend,
4624 .resume = bttv_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004625#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626};
4627
Adrian Bunk7d44e892007-12-11 19:23:43 -03004628static int __init bttv_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629{
Randy Dunlapc526e222006-07-15 09:08:26 -03004630 int ret;
4631
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 bttv_num = 0;
4633
4634 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4635 (BTTV_VERSION_CODE >> 16) & 0xff,
4636 (BTTV_VERSION_CODE >> 8) & 0xff,
4637 BTTV_VERSION_CODE & 0xff);
4638#ifdef SNAPSHOT
4639 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4640 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4641#endif
4642 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4643 gbuffers = 2;
Filipe Rossetf41b6962009-05-28 11:11:53 -03004644 if (gbufsize > BTTV_MAX_FBUF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 gbufsize = BTTV_MAX_FBUF;
4646 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4647 if (bttv_verbose)
4648 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4649 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4650
4651 bttv_check_chipset();
4652
Randy Dunlapc526e222006-07-15 09:08:26 -03004653 ret = bus_register(&bttv_sub_bus_type);
4654 if (ret < 0) {
4655 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4656 return ret;
4657 }
Akinobu Mita9e7e85e2007-12-17 14:26:29 -03004658 ret = pci_register_driver(&bttv_pci_driver);
4659 if (ret < 0)
4660 bus_unregister(&bttv_sub_bus_type);
4661
4662 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663}
4664
Adrian Bunk7d44e892007-12-11 19:23:43 -03004665static void __exit bttv_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666{
4667 pci_unregister_driver(&bttv_pci_driver);
4668 bus_unregister(&bttv_sub_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669}
4670
4671module_init(bttv_init_module);
4672module_exit(bttv_cleanup_module);
4673
4674/*
4675 * Local variables:
4676 * c-basic-offset: 8
4677 * End:
4678 */