blob: 53285aa6e72c9520b6e811bb38ebdd4f876b34c8 [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>
45#include <linux/interrupt.h>
46#include <linux/kdev_t.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020047#include "bttvp.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020048#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030049#include <media/v4l2-ioctl.h>
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -030050#include <media/tvaudio.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030051#include <media/msp3400.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020052
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -070053#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include <asm/io.h>
56#include <asm/byteorder.h>
57
Mauro Carvalho Chehabfa3fcce2006-03-23 21:45:24 -030058#include <media/rds.h>
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070059
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061unsigned int bttv_num; /* number of Bt848s in use */
Trent Piepho4b10d3b2009-01-28 21:32:59 -030062struct bttv *bttvs[BTTV_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020064unsigned int bttv_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065unsigned int bttv_verbose = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020066unsigned int bttv_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* config variables */
69#ifdef __BIG_ENDIAN
70static unsigned int bigendian=1;
71#else
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020072static unsigned int bigendian;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
74static unsigned int radio[BTTV_MAX];
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020075static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static unsigned int gbuffers = 8;
77static unsigned int gbufsize = 0x208000;
Michael Schimeke5bd0262007-01-18 16:17:39 -030078static unsigned int reset_crop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Jean Delvare176c2f32008-09-07 12:49:59 -030080static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
81static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
82static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020083static int debug_latency;
Jean Delvared90a4ae2010-02-16 14:22:37 -030084static int disable_ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020086static unsigned int fdsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88/* options */
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020089static unsigned int combfilter;
90static unsigned int lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static unsigned int automute = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020092static unsigned int chroma_agc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static unsigned int adc_crush = 1;
94static unsigned int whitecrush_upper = 0xCF;
95static unsigned int whitecrush_lower = 0x7F;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020096static unsigned int vcr_hack;
97static unsigned int irq_iswitch;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -070098static unsigned int uv_ratio = 50;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020099static unsigned int full_luma_range;
100static unsigned int coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/* API features (turn on/off stuff for testing) */
103static unsigned int v4l2 = 1;
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/* insmod args */
106module_param(bttv_verbose, int, 0644);
107module_param(bttv_gpio, int, 0644);
108module_param(bttv_debug, int, 0644);
109module_param(irq_debug, int, 0644);
110module_param(debug_latency, int, 0644);
Jean Delvared90a4ae2010-02-16 14:22:37 -0300111module_param(disable_ir, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113module_param(fdsr, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114module_param(gbuffers, int, 0444);
115module_param(gbufsize, int, 0444);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300116module_param(reset_crop, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118module_param(v4l2, int, 0644);
119module_param(bigendian, int, 0644);
120module_param(irq_iswitch, int, 0644);
121module_param(combfilter, int, 0444);
122module_param(lumafilter, int, 0444);
123module_param(automute, int, 0444);
124module_param(chroma_agc, int, 0444);
125module_param(adc_crush, int, 0444);
126module_param(whitecrush_upper, int, 0444);
127module_param(whitecrush_lower, int, 0444);
128module_param(vcr_hack, int, 0444);
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700129module_param(uv_ratio, int, 0444);
130module_param(full_luma_range, int, 0444);
131module_param(coring, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jean Delvare176c2f32008-09-07 12:49:59 -0300133module_param_array(radio, int, NULL, 0444);
134module_param_array(video_nr, int, NULL, 0444);
135module_param_array(radio_nr, int, NULL, 0444);
136module_param_array(vbi_nr, int, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
139MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
140MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
141MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
142MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
143MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
Jean Delvared90a4ae2010-02-16 14:22:37 -0300144MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
146MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
Michael Schimeke5bd0262007-01-18 16:17:39 -0300147MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
148 "is 1 (yes) for compatibility with older applications");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
150MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
151MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
152MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
153MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
154MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
155MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700156MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
157MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
158MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
Jean Delvare176c2f32008-09-07 12:49:59 -0300159MODULE_PARM_DESC(video_nr, "video device numbers");
160MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
161MODULE_PARM_DESC(radio_nr, "radio device numbers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
164MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
165MODULE_LICENSE("GPL");
166
167/* ----------------------------------------------------------------------- */
168/* sysfs */
169
Kay Sievers54bd5b62007-10-08 16:26:13 -0300170static ssize_t show_card(struct device *cd,
171 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300173 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -0300174 struct bttv *btv = video_get_drvdata(vfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
176}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300177static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/* ----------------------------------------------------------------------- */
Jarod Wilsonf992a492007-03-24 15:23:50 -0300180/* dvb auto-load setup */
181#if defined(CONFIG_MODULES) && defined(MODULE)
182static void request_module_async(struct work_struct *work)
183{
184 request_module("dvb-bt8xx");
185}
186
187static void request_modules(struct bttv *dev)
188{
189 INIT_WORK(&dev->request_module_wk, request_module_async);
190 schedule_work(&dev->request_module_wk);
191}
Tejun Heo707bcf32010-12-24 16:14:20 +0100192
193static void flush_request_modules(struct bttv *dev)
194{
195 flush_work_sync(&dev->request_module_wk);
196}
Jarod Wilsonf992a492007-03-24 15:23:50 -0300197#else
198#define request_modules(dev)
Tejun Heo707bcf32010-12-24 16:14:20 +0100199#define flush_request_modules(dev)
Jarod Wilsonf992a492007-03-24 15:23:50 -0300200#endif /* CONFIG_MODULES */
201
202
203/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* static data */
205
206/* special timing tables from conexant... */
207static u8 SRAM_Table[][60] =
208{
209 /* PAL digital input over GPIO[7:0] */
210 {
211 45, // 45 bytes following
212 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
213 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
214 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
215 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
216 0x37,0x00,0xAF,0x21,0x00
217 },
218 /* NTSC digital input over GPIO[7:0] */
219 {
220 51, // 51 bytes following
221 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
222 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
223 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
224 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
225 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
226 0x00,
227 },
228 // TGB_NTSC392 // quartzsight
229 // This table has been modified to be used for Fusion Rev D
230 {
231 0x2A, // size of table = 42
232 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
233 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
234 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
235 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
236 0x20, 0x00
237 }
238};
239
Michael Schimeke5bd0262007-01-18 16:17:39 -0300240/* minhdelayx1 first video pixel we can capture on a line and
241 hdelayx1 start of active video, both relative to rising edge of
242 /HRESET pulse (0H) in 1 / fCLKx1.
243 swidth width of active video and
244 totalwidth total line width, both in 1 / fCLKx1.
245 sqwidth total line width in square pixels.
246 vdelay start of active video in 2 * field lines relative to
247 trailing edge of /VRESET pulse (VDELAY register).
248 sheight height of active video in 2 * field lines.
249 videostart0 ITU-R frame line number of the line corresponding
250 to vdelay in the first field. */
251#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
252 vdelay, sheight, videostart0) \
253 .cropcap.bounds.left = minhdelayx1, \
254 /* * 2 because vertically we count field lines times two, */ \
255 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
256 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
257 /* 4 is a safety margin at the end of the line. */ \
258 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
259 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
260 .cropcap.defrect.left = hdelayx1, \
261 .cropcap.defrect.top = (videostart0) * 2, \
262 .cropcap.defrect.width = swidth, \
263 .cropcap.defrect.height = sheight, \
264 .cropcap.pixelaspect.numerator = totalwidth, \
265 .cropcap.pixelaspect.denominator = sqwidth,
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267const struct bttv_tvnorm bttv_tvnorms[] = {
268 /* PAL-BDGHI */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800269 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
270 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 {
272 .v4l2_id = V4L2_STD_PAL,
273 .name = "PAL",
274 .Fsc = 35468950,
275 .swidth = 924,
276 .sheight = 576,
277 .totalwidth = 1135,
278 .adelay = 0x7f,
279 .bdelay = 0x72,
280 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
281 .scaledtwidth = 1135,
282 .hdelayx1 = 186,
283 .hactivex1 = 924,
284 .vdelay = 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300285 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 .sram = 0,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200287 /* ITU-R frame line number of the first VBI line
Michael Schimeke5bd0262007-01-18 16:17:39 -0300288 we can capture, of the first and second field.
289 The last line is determined by cropcap.bounds. */
290 .vbistart = { 7, 320 },
291 CROPCAP(/* minhdelayx1 */ 68,
292 /* hdelayx1 */ 186,
293 /* Should be (768 * 1135 + 944 / 2) / 944.
294 cropcap.defrect is used for image width
295 checks, so we keep the old value 924. */
296 /* swidth */ 924,
297 /* totalwidth */ 1135,
298 /* sqwidth */ 944,
299 /* vdelay */ 0x20,
300 /* sheight */ 576,
301 /* videostart0 */ 23)
302 /* bt878 (and bt848?) can capture another
303 line below active video. */
304 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 },{
Hans Verkuild97a11e2006-02-07 06:48:40 -0200306 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 .name = "NTSC",
308 .Fsc = 28636363,
309 .swidth = 768,
310 .sheight = 480,
311 .totalwidth = 910,
312 .adelay = 0x68,
313 .bdelay = 0x5d,
314 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
315 .scaledtwidth = 910,
316 .hdelayx1 = 128,
317 .hactivex1 = 910,
318 .vdelay = 0x1a,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300319 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 .sram = 1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200321 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300322 CROPCAP(/* minhdelayx1 */ 68,
323 /* hdelayx1 */ 128,
324 /* Should be (640 * 910 + 780 / 2) / 780? */
325 /* swidth */ 768,
326 /* totalwidth */ 910,
327 /* sqwidth */ 780,
328 /* vdelay */ 0x1a,
329 /* sheight */ 480,
330 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 },{
332 .v4l2_id = V4L2_STD_SECAM,
333 .name = "SECAM",
334 .Fsc = 35468950,
335 .swidth = 924,
336 .sheight = 576,
337 .totalwidth = 1135,
338 .adelay = 0x7f,
339 .bdelay = 0xb0,
340 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
341 .scaledtwidth = 1135,
342 .hdelayx1 = 186,
343 .hactivex1 = 922,
344 .vdelay = 0x20,
345 .vbipack = 255,
346 .sram = 0, /* like PAL, correct? */
Michael H. Schimek67f15702006-01-09 15:25:27 -0200347 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300348 CROPCAP(/* minhdelayx1 */ 68,
349 /* hdelayx1 */ 186,
350 /* swidth */ 924,
351 /* totalwidth */ 1135,
352 /* sqwidth */ 944,
353 /* vdelay */ 0x20,
354 /* sheight */ 576,
355 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 },{
357 .v4l2_id = V4L2_STD_PAL_Nc,
358 .name = "PAL-Nc",
359 .Fsc = 28636363,
360 .swidth = 640,
361 .sheight = 576,
362 .totalwidth = 910,
363 .adelay = 0x68,
364 .bdelay = 0x5d,
365 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
366 .scaledtwidth = 780,
367 .hdelayx1 = 130,
368 .hactivex1 = 734,
369 .vdelay = 0x1a,
370 .vbipack = 144,
371 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200372 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300373 CROPCAP(/* minhdelayx1 */ 68,
374 /* hdelayx1 */ 130,
375 /* swidth */ (640 * 910 + 780 / 2) / 780,
376 /* totalwidth */ 910,
377 /* sqwidth */ 780,
378 /* vdelay */ 0x1a,
379 /* sheight */ 576,
380 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 },{
382 .v4l2_id = V4L2_STD_PAL_M,
383 .name = "PAL-M",
384 .Fsc = 28636363,
385 .swidth = 640,
386 .sheight = 480,
387 .totalwidth = 910,
388 .adelay = 0x68,
389 .bdelay = 0x5d,
390 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
391 .scaledtwidth = 780,
392 .hdelayx1 = 135,
393 .hactivex1 = 754,
394 .vdelay = 0x1a,
395 .vbipack = 144,
396 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200397 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300398 CROPCAP(/* minhdelayx1 */ 68,
399 /* hdelayx1 */ 135,
400 /* swidth */ (640 * 910 + 780 / 2) / 780,
401 /* totalwidth */ 910,
402 /* sqwidth */ 780,
403 /* vdelay */ 0x1a,
404 /* sheight */ 480,
405 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 },{
407 .v4l2_id = V4L2_STD_PAL_N,
408 .name = "PAL-N",
409 .Fsc = 35468950,
410 .swidth = 768,
411 .sheight = 576,
412 .totalwidth = 1135,
413 .adelay = 0x7f,
414 .bdelay = 0x72,
415 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
416 .scaledtwidth = 944,
417 .hdelayx1 = 186,
418 .hactivex1 = 922,
419 .vdelay = 0x20,
420 .vbipack = 144,
421 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300422 .vbistart = { 7, 320 },
423 CROPCAP(/* minhdelayx1 */ 68,
424 /* hdelayx1 */ 186,
425 /* swidth */ (768 * 1135 + 944 / 2) / 944,
426 /* totalwidth */ 1135,
427 /* sqwidth */ 944,
428 /* vdelay */ 0x20,
429 /* sheight */ 576,
430 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 },{
432 .v4l2_id = V4L2_STD_NTSC_M_JP,
433 .name = "NTSC-JP",
434 .Fsc = 28636363,
435 .swidth = 640,
436 .sheight = 480,
437 .totalwidth = 910,
438 .adelay = 0x68,
439 .bdelay = 0x5d,
440 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
441 .scaledtwidth = 780,
442 .hdelayx1 = 135,
443 .hactivex1 = 754,
444 .vdelay = 0x16,
445 .vbipack = 144,
446 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300447 .vbistart = { 10, 273 },
448 CROPCAP(/* minhdelayx1 */ 68,
449 /* hdelayx1 */ 135,
450 /* swidth */ (640 * 910 + 780 / 2) / 780,
451 /* totalwidth */ 910,
452 /* sqwidth */ 780,
453 /* vdelay */ 0x16,
454 /* sheight */ 480,
455 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 },{
457 /* that one hopefully works with the strange timing
458 * which video recorders produce when playing a NTSC
459 * tape on a PAL TV ... */
460 .v4l2_id = V4L2_STD_PAL_60,
461 .name = "PAL-60",
462 .Fsc = 35468950,
463 .swidth = 924,
464 .sheight = 480,
465 .totalwidth = 1135,
466 .adelay = 0x7f,
467 .bdelay = 0x72,
468 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
469 .scaledtwidth = 1135,
470 .hdelayx1 = 186,
471 .hactivex1 = 924,
472 .vdelay = 0x1a,
473 .vbipack = 255,
474 .vtotal = 524,
475 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200476 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300477 CROPCAP(/* minhdelayx1 */ 68,
478 /* hdelayx1 */ 186,
479 /* swidth */ 924,
480 /* totalwidth */ 1135,
481 /* sqwidth */ 944,
482 /* vdelay */ 0x1a,
483 /* sheight */ 480,
484 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486};
487static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
488
489/* ----------------------------------------------------------------------- */
490/* bttv format list
491 packed pixel formats must come first */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300492static const struct bttv_format formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 {
494 .name = "8 bpp, gray",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 .fourcc = V4L2_PIX_FMT_GREY,
496 .btformat = BT848_COLOR_FMT_Y8,
497 .depth = 8,
498 .flags = FORMAT_FLAGS_PACKED,
499 },{
500 .name = "8 bpp, dithered color",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 .fourcc = V4L2_PIX_FMT_HI240,
502 .btformat = BT848_COLOR_FMT_RGB8,
503 .depth = 8,
504 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
505 },{
506 .name = "15 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 .fourcc = V4L2_PIX_FMT_RGB555,
508 .btformat = BT848_COLOR_FMT_RGB15,
509 .depth = 16,
510 .flags = FORMAT_FLAGS_PACKED,
511 },{
512 .name = "15 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 .fourcc = V4L2_PIX_FMT_RGB555X,
514 .btformat = BT848_COLOR_FMT_RGB15,
515 .btswap = 0x03, /* byteswap */
516 .depth = 16,
517 .flags = FORMAT_FLAGS_PACKED,
518 },{
519 .name = "16 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 .fourcc = V4L2_PIX_FMT_RGB565,
521 .btformat = BT848_COLOR_FMT_RGB16,
522 .depth = 16,
523 .flags = FORMAT_FLAGS_PACKED,
524 },{
525 .name = "16 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 .fourcc = V4L2_PIX_FMT_RGB565X,
527 .btformat = BT848_COLOR_FMT_RGB16,
528 .btswap = 0x03, /* byteswap */
529 .depth = 16,
530 .flags = FORMAT_FLAGS_PACKED,
531 },{
532 .name = "24 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 .fourcc = V4L2_PIX_FMT_BGR24,
534 .btformat = BT848_COLOR_FMT_RGB24,
535 .depth = 24,
536 .flags = FORMAT_FLAGS_PACKED,
537 },{
538 .name = "32 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 .fourcc = V4L2_PIX_FMT_BGR32,
540 .btformat = BT848_COLOR_FMT_RGB32,
541 .depth = 32,
542 .flags = FORMAT_FLAGS_PACKED,
543 },{
544 .name = "32 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 .fourcc = V4L2_PIX_FMT_RGB32,
546 .btformat = BT848_COLOR_FMT_RGB32,
547 .btswap = 0x0f, /* byte+word swap */
548 .depth = 32,
549 .flags = FORMAT_FLAGS_PACKED,
550 },{
551 .name = "4:2:2, packed, YUYV",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 .fourcc = V4L2_PIX_FMT_YUYV,
553 .btformat = BT848_COLOR_FMT_YUY2,
554 .depth = 16,
555 .flags = FORMAT_FLAGS_PACKED,
556 },{
557 .name = "4:2:2, packed, YUYV",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 .fourcc = V4L2_PIX_FMT_YUYV,
559 .btformat = BT848_COLOR_FMT_YUY2,
560 .depth = 16,
561 .flags = FORMAT_FLAGS_PACKED,
562 },{
563 .name = "4:2:2, packed, UYVY",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 .fourcc = V4L2_PIX_FMT_UYVY,
565 .btformat = BT848_COLOR_FMT_YUY2,
566 .btswap = 0x03, /* byteswap */
567 .depth = 16,
568 .flags = FORMAT_FLAGS_PACKED,
569 },{
570 .name = "4:2:2, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 .fourcc = V4L2_PIX_FMT_YUV422P,
572 .btformat = BT848_COLOR_FMT_YCrCb422,
573 .depth = 16,
574 .flags = FORMAT_FLAGS_PLANAR,
575 .hshift = 1,
576 .vshift = 0,
577 },{
578 .name = "4:2:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 .fourcc = V4L2_PIX_FMT_YUV420,
580 .btformat = BT848_COLOR_FMT_YCrCb422,
581 .depth = 12,
582 .flags = FORMAT_FLAGS_PLANAR,
583 .hshift = 1,
584 .vshift = 1,
585 },{
586 .name = "4:2:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 .fourcc = V4L2_PIX_FMT_YVU420,
588 .btformat = BT848_COLOR_FMT_YCrCb422,
589 .depth = 12,
590 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
591 .hshift = 1,
592 .vshift = 1,
593 },{
594 .name = "4:1:1, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 .fourcc = V4L2_PIX_FMT_YUV411P,
596 .btformat = BT848_COLOR_FMT_YCrCb411,
597 .depth = 12,
598 .flags = FORMAT_FLAGS_PLANAR,
599 .hshift = 2,
600 .vshift = 0,
601 },{
602 .name = "4:1:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 .fourcc = V4L2_PIX_FMT_YUV410,
604 .btformat = BT848_COLOR_FMT_YCrCb411,
605 .depth = 9,
606 .flags = FORMAT_FLAGS_PLANAR,
607 .hshift = 2,
608 .vshift = 2,
609 },{
610 .name = "4:1:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 .fourcc = V4L2_PIX_FMT_YVU410,
612 .btformat = BT848_COLOR_FMT_YCrCb411,
613 .depth = 9,
614 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
615 .hshift = 2,
616 .vshift = 2,
617 },{
618 .name = "raw scanlines",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 .fourcc = -1,
620 .btformat = BT848_COLOR_FMT_RAW,
621 .depth = 8,
622 .flags = FORMAT_FLAGS_RAW,
623 }
624};
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300625static const unsigned int FORMATS = ARRAY_SIZE(formats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627/* ----------------------------------------------------------------------- */
628
629#define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
630#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
631#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
632#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
633#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
634#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
635#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
636#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700637#define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
638#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
639#define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
640#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642static const struct v4l2_queryctrl no_ctl = {
643 .name = "42",
644 .flags = V4L2_CTRL_FLAG_DISABLED,
645};
646static const struct v4l2_queryctrl bttv_ctls[] = {
647 /* --- video --- */
648 {
649 .id = V4L2_CID_BRIGHTNESS,
650 .name = "Brightness",
651 .minimum = 0,
652 .maximum = 65535,
653 .step = 256,
654 .default_value = 32768,
655 .type = V4L2_CTRL_TYPE_INTEGER,
656 },{
657 .id = V4L2_CID_CONTRAST,
658 .name = "Contrast",
659 .minimum = 0,
660 .maximum = 65535,
661 .step = 128,
662 .default_value = 32768,
663 .type = V4L2_CTRL_TYPE_INTEGER,
664 },{
665 .id = V4L2_CID_SATURATION,
666 .name = "Saturation",
667 .minimum = 0,
668 .maximum = 65535,
669 .step = 128,
670 .default_value = 32768,
671 .type = V4L2_CTRL_TYPE_INTEGER,
672 },{
673 .id = V4L2_CID_HUE,
674 .name = "Hue",
675 .minimum = 0,
676 .maximum = 65535,
677 .step = 256,
678 .default_value = 32768,
679 .type = V4L2_CTRL_TYPE_INTEGER,
680 },
681 /* --- audio --- */
682 {
683 .id = V4L2_CID_AUDIO_MUTE,
684 .name = "Mute",
685 .minimum = 0,
686 .maximum = 1,
687 .type = V4L2_CTRL_TYPE_BOOLEAN,
688 },{
689 .id = V4L2_CID_AUDIO_VOLUME,
690 .name = "Volume",
691 .minimum = 0,
692 .maximum = 65535,
693 .step = 65535/100,
694 .default_value = 65535,
695 .type = V4L2_CTRL_TYPE_INTEGER,
696 },{
697 .id = V4L2_CID_AUDIO_BALANCE,
698 .name = "Balance",
699 .minimum = 0,
700 .maximum = 65535,
701 .step = 65535/100,
702 .default_value = 32768,
703 .type = V4L2_CTRL_TYPE_INTEGER,
704 },{
705 .id = V4L2_CID_AUDIO_BASS,
706 .name = "Bass",
707 .minimum = 0,
708 .maximum = 65535,
709 .step = 65535/100,
710 .default_value = 32768,
711 .type = V4L2_CTRL_TYPE_INTEGER,
712 },{
713 .id = V4L2_CID_AUDIO_TREBLE,
714 .name = "Treble",
715 .minimum = 0,
716 .maximum = 65535,
717 .step = 65535/100,
718 .default_value = 32768,
719 .type = V4L2_CTRL_TYPE_INTEGER,
720 },
721 /* --- private --- */
722 {
723 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
724 .name = "chroma agc",
725 .minimum = 0,
726 .maximum = 1,
727 .type = V4L2_CTRL_TYPE_BOOLEAN,
728 },{
729 .id = V4L2_CID_PRIVATE_COMBFILTER,
730 .name = "combfilter",
731 .minimum = 0,
732 .maximum = 1,
733 .type = V4L2_CTRL_TYPE_BOOLEAN,
734 },{
735 .id = V4L2_CID_PRIVATE_AUTOMUTE,
736 .name = "automute",
737 .minimum = 0,
738 .maximum = 1,
739 .type = V4L2_CTRL_TYPE_BOOLEAN,
740 },{
741 .id = V4L2_CID_PRIVATE_LUMAFILTER,
742 .name = "luma decimation filter",
743 .minimum = 0,
744 .maximum = 1,
745 .type = V4L2_CTRL_TYPE_BOOLEAN,
746 },{
747 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
748 .name = "agc crush",
749 .minimum = 0,
750 .maximum = 1,
751 .type = V4L2_CTRL_TYPE_BOOLEAN,
752 },{
753 .id = V4L2_CID_PRIVATE_VCR_HACK,
754 .name = "vcr hack",
755 .minimum = 0,
756 .maximum = 1,
757 .type = V4L2_CTRL_TYPE_BOOLEAN,
758 },{
759 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
760 .name = "whitecrush upper",
761 .minimum = 0,
762 .maximum = 255,
763 .step = 1,
764 .default_value = 0xCF,
765 .type = V4L2_CTRL_TYPE_INTEGER,
766 },{
767 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
768 .name = "whitecrush lower",
769 .minimum = 0,
770 .maximum = 255,
771 .step = 1,
772 .default_value = 0x7F,
773 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700774 },{
775 .id = V4L2_CID_PRIVATE_UV_RATIO,
776 .name = "uv ratio",
777 .minimum = 0,
778 .maximum = 100,
779 .step = 1,
780 .default_value = 50,
781 .type = V4L2_CTRL_TYPE_INTEGER,
782 },{
783 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
784 .name = "full luma range",
785 .minimum = 0,
786 .maximum = 1,
787 .type = V4L2_CTRL_TYPE_BOOLEAN,
788 },{
789 .id = V4L2_CID_PRIVATE_CORING,
790 .name = "coring",
791 .minimum = 0,
792 .maximum = 3,
793 .step = 1,
794 .default_value = 0,
795 .type = V4L2_CTRL_TYPE_INTEGER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700798
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300802static const struct v4l2_queryctrl *ctrl_by_id(int id)
803{
804 int i;
805
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300806 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300807 if (bttv_ctls[i].id == id)
808 return bttv_ctls+i;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300809
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300810 return NULL;
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/* ----------------------------------------------------------------------- */
814/* resource management */
815
Michael Schimeke5bd0262007-01-18 16:17:39 -0300816/*
817 RESOURCE_ allocated by freed by
818
819 VIDEO_READ bttv_read 1) bttv_read 2)
820
821 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
822 VIDIOC_QBUF 1) bttv_release
823 VIDIOCMCAPTURE 1)
824
825 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
826 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
827 3) bttv_release
828
829 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
830 VIDIOC_QBUF 1) bttv_release
831 bttv_read, bttv_poll 1) 4)
832
833 1) The resource must be allocated when we enter buffer prepare functions
834 and remain allocated while buffers are in the DMA queue.
835 2) This is a single frame read.
836 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
837 RESOURCE_OVERLAY is allocated.
838 4) This is a continuous read, implies VIDIOC_STREAMON.
839
840 Note this driver permits video input and standard changes regardless if
841 resources are allocated.
842*/
843
844#define VBI_RESOURCES (RESOURCE_VBI)
845#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
846 RESOURCE_VIDEO_STREAM | \
847 RESOURCE_OVERLAY)
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300850int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Michael Schimeke5bd0262007-01-18 16:17:39 -0300852 int xbits; /* mutual exclusive resources */
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (fh->resources & bit)
855 /* have it already allocated */
856 return 1;
857
Michael Schimeke5bd0262007-01-18 16:17:39 -0300858 xbits = bit;
859 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
860 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /* is it free? */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300863 mutex_lock(&btv->lock);
864 if (btv->resources & xbits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 /* no, someone else uses it */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300866 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300868
869 if ((bit & VIDEO_RESOURCES)
870 && 0 == (btv->resources & VIDEO_RESOURCES)) {
871 /* Do crop - use current, don't - use default parameters. */
872 __s32 top = btv->crop[!!fh->do_crop].rect.top;
873
874 if (btv->vbi_end > top)
875 goto fail;
876
877 /* We cannot capture the same line as video and VBI data.
878 Claim scan lines crop[].rect.top to bottom. */
879 btv->crop_start = top;
880 } else if (bit & VBI_RESOURCES) {
881 __s32 end = fh->vbi_fmt.end;
882
883 if (end > btv->crop_start)
884 goto fail;
885
886 /* Claim scan lines above fh->vbi_fmt.end. */
887 btv->vbi_end = end;
888 }
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /* it's free, grab it */
891 fh->resources |= bit;
892 btv->resources |= bit;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300893 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return 1;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300895
896 fail:
897 mutex_unlock(&btv->lock);
898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901static
902int check_btres(struct bttv_fh *fh, int bit)
903{
904 return (fh->resources & bit);
905}
906
907static
908int locked_btres(struct bttv *btv, int bit)
909{
910 return (btv->resources & bit);
911}
912
Michael Schimeke5bd0262007-01-18 16:17:39 -0300913/* Call with btv->lock down. */
914static void
915disclaim_vbi_lines(struct bttv *btv)
916{
917 btv->vbi_end = 0;
918}
919
920/* Call with btv->lock down. */
921static void
922disclaim_video_lines(struct bttv *btv)
923{
924 const struct bttv_tvnorm *tvnorm;
925 u8 crop;
926
927 tvnorm = &bttv_tvnorms[btv->tvnorm];
928 btv->crop_start = tvnorm->cropcap.bounds.top
929 + tvnorm->cropcap.bounds.height;
930
931 /* VBI capturing ends at VDELAY, start of video capturing, no
932 matter how many lines the VBI RISC program expects. When video
933 capturing is off, it shall no longer "preempt" VBI capturing,
934 so we set VDELAY to maximum. */
935 crop = btread(BT848_E_CROP) | 0xc0;
936 btwrite(crop, BT848_E_CROP);
937 btwrite(0xfe, BT848_E_VDELAY_LO);
938 btwrite(crop, BT848_O_CROP);
939 btwrite(0xfe, BT848_O_VDELAY_LO);
940}
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300943void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if ((fh->resources & bits) != bits) {
946 /* trying to free ressources not allocated by us ... */
947 printk("bttv: BUG! (btres)\n");
948 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300949 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 fh->resources &= ~bits;
951 btv->resources &= ~bits;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300952
953 bits = btv->resources;
954
955 if (0 == (bits & VIDEO_RESOURCES))
956 disclaim_video_lines(btv);
957
958 if (0 == (bits & VBI_RESOURCES))
959 disclaim_vbi_lines(btv);
960
961 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964/* ----------------------------------------------------------------------- */
965/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
966
967/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
968 PLL_X = Reference pre-divider (0=1, 1=2)
969 PLL_C = Post divider (0=6, 1=4)
970 PLL_I = Integer input
971 PLL_F = Fractional input
972
973 F_input = 28.636363 MHz:
974 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
975*/
976
977static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
978{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800979 unsigned char fl, fh, fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800981 /* prevent overflows */
982 fin/=4;
983 fout/=4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800985 fout*=12;
986 fi=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800988 fout=(fout%fin)*256;
989 fh=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800991 fout=(fout%fin)*256;
992 fl=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800994 btwrite(fl, BT848_PLL_F_LO);
995 btwrite(fh, BT848_PLL_F_HI);
996 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999static void set_pll(struct bttv *btv)
1000{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001001 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001003 if (!btv->pll.pll_crystal)
1004 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
1007 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001008 return;
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001011 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1012 /* no PLL needed */
1013 if (btv->pll.pll_current == 0)
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001014 return;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001015 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001016 btv->c.nr,btv->pll.pll_ifreq);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001017 btwrite(0x00,BT848_TGCTRL);
1018 btwrite(0x00,BT848_PLL_XCI);
1019 btv->pll.pll_current = 0;
1020 return;
1021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001023 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001024 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1026
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001027 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /* Let other people run while the PLL stabilizes */
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001029 bttv_printk(".");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 msleep(10);
1031
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001032 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 btwrite(0,BT848_DSTATUS);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001034 } else {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001035 btwrite(0x08,BT848_TGCTRL);
1036 btv->pll.pll_current = btv->pll.pll_ofreq;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001037 bttv_printk(" ok\n");
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001038 return;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001039 }
1040 }
1041 btv->pll.pll_current = -1;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001042 bttv_printk("failed\n");
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001043 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046/* used to switch between the bt848's analog/digital video capture modes */
1047static void bt848A_set_timing(struct bttv *btv)
1048{
1049 int i, len;
1050 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1051 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1052
Trent Piepho5221e212009-01-28 21:32:59 -03001053 if (btv->input == btv->dig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1055 btv->c.nr,table_idx);
1056
1057 /* timing change...reset timing generator address */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001058 btwrite(0x00, BT848_TGCTRL);
1059 btwrite(0x02, BT848_TGCTRL);
1060 btwrite(0x00, BT848_TGCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 len=SRAM_Table[table_idx][0];
1063 for(i = 1; i <= len; i++)
1064 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1065 btv->pll.pll_ofreq = 27000000;
1066
1067 set_pll(btv);
1068 btwrite(0x11, BT848_TGCTRL);
1069 btwrite(0x41, BT848_DVSIF);
1070 } else {
1071 btv->pll.pll_ofreq = fsc;
1072 set_pll(btv);
1073 btwrite(0x0, BT848_DVSIF);
1074 }
1075}
1076
1077/* ----------------------------------------------------------------------- */
1078
1079static void bt848_bright(struct bttv *btv, int bright)
1080{
1081 int value;
1082
1083 // printk("bttv: set bright: %d\n",bright); // DEBUG
1084 btv->bright = bright;
1085
1086 /* We want -128 to 127 we get 0-65535 */
1087 value = (bright >> 8) - 128;
1088 btwrite(value & 0xff, BT848_BRIGHT);
1089}
1090
1091static void bt848_hue(struct bttv *btv, int hue)
1092{
1093 int value;
1094
1095 btv->hue = hue;
1096
1097 /* -128 to 127 */
1098 value = (hue >> 8) - 128;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001099 btwrite(value & 0xff, BT848_HUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
1102static void bt848_contrast(struct bttv *btv, int cont)
1103{
1104 int value,hibit;
1105
1106 btv->contrast = cont;
1107
1108 /* 0-511 */
1109 value = (cont >> 7);
1110 hibit = (value >> 6) & 4;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001111 btwrite(value & 0xff, BT848_CONTRAST_LO);
1112 btaor(hibit, ~4, BT848_E_CONTROL);
1113 btaor(hibit, ~4, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
1116static void bt848_sat(struct bttv *btv, int color)
1117{
1118 int val_u,val_v,hibits;
1119
1120 btv->saturation = color;
1121
1122 /* 0-511 for the color */
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001123 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1124 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001125 hibits = (val_u >> 7) & 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 hibits |= (val_v >> 8) & 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001127 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1128 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1129 btaor(hibits, ~3, BT848_E_CONTROL);
1130 btaor(hibits, ~3, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
1133/* ----------------------------------------------------------------------- */
1134
1135static int
1136video_mux(struct bttv *btv, unsigned int input)
1137{
1138 int mux,mask2;
1139
1140 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1141 return -EINVAL;
1142
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001143 /* needed by RemoteVideo MX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1145 if (mask2)
1146 gpio_inout(mask2,mask2);
1147
1148 if (input == btv->svhs) {
1149 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1150 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1151 } else {
1152 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1153 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1154 }
Trent Piepho6f987002009-01-28 21:32:59 -03001155 mux = bttv_muxsel(btv, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1157 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1158 btv->c.nr,input,mux);
1159
1160 /* card specific hook */
1161 if(bttv_tvcards[btv->c.type].muxsel_hook)
1162 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1163 return 0;
1164}
1165
1166static char *audio_modes[] = {
1167 "audio: tuner", "audio: radio", "audio: extern",
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001168 "audio: intern", "audio: mute"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169};
1170
1171static int
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001172audio_mux(struct bttv *btv, int input, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001174 int gpio_val, signal;
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001175 struct v4l2_control ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1178 bttv_tvcards[btv->c.type].gpiomask);
1179 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1180
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001181 btv->mute = mute;
1182 btv->audio = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001184 /* automute */
1185 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1186
1187 if (mute)
1188 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1189 else
1190 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001191
Trent Piepho72134a62009-01-28 21:32:59 -03001192 switch (btv->c.type) {
1193 case BTTV_BOARD_VOODOOTV_FM:
1194 case BTTV_BOARD_VOODOOTV_200:
1195 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1196 break;
1197
1198 default:
1199 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1200 }
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (bttv_gpio)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001203 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1204 if (in_interrupt())
1205 return 0;
1206
1207 ctrl.id = V4L2_CID_AUDIO_MUTE;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001208 ctrl.value = btv->mute;
Hans Verkuil859f0272009-03-28 08:29:00 -03001209 bttv_call_all(btv, core, s_ctrl, &ctrl);
1210 if (btv->sd_msp34xx) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001211 u32 in;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001212
1213 /* Note: the inputs tuner/radio/extern/intern are translated
1214 to msp routings. This assumes common behavior for all msp3400
1215 based TV cards. When this assumption fails, then the
1216 specific MSP routing must be added to the card table.
1217 For now this is sufficient. */
1218 switch (input) {
1219 case TVAUDIO_INPUT_RADIO:
Hans Verkuil5325b422009-04-02 11:26:22 -03001220 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001221 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001222 break;
1223 case TVAUDIO_INPUT_EXTERN:
Hans Verkuil5325b422009-04-02 11:26:22 -03001224 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001225 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001226 break;
1227 case TVAUDIO_INPUT_INTERN:
1228 /* Yes, this is the same input as for RADIO. I doubt
1229 if this is ever used. The only board with an INTERN
1230 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1231 that was tested. My guess is that the whole INTERN
1232 input does not work. */
Hans Verkuil5325b422009-04-02 11:26:22 -03001233 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001234 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001235 break;
1236 case TVAUDIO_INPUT_TUNER:
1237 default:
Wade Berrier434b2522007-06-25 13:02:16 -03001238 /* This is the only card that uses TUNER2, and afaik,
1239 is the only difference between the VOODOOTV_FM
1240 and VOODOOTV_200 */
1241 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
Hans Verkuil5325b422009-04-02 11:26:22 -03001242 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
Wade Berrier434b2522007-06-25 13:02:16 -03001243 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1244 else
Hans Verkuil5325b422009-04-02 11:26:22 -03001245 in = MSP_INPUT_DEFAULT;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001246 break;
1247 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001248 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1249 in, MSP_OUTPUT_DEFAULT, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001250 }
Hans Verkuil859f0272009-03-28 08:29:00 -03001251 if (btv->sd_tvaudio) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001252 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1253 input, 0, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return 0;
1256}
1257
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001258static inline int
1259audio_mute(struct bttv *btv, int mute)
1260{
1261 return audio_mux(btv, btv->audio, mute);
1262}
1263
1264static inline int
1265audio_input(struct bttv *btv, int input)
1266{
1267 return audio_mux(btv, input, btv->mute);
1268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270static void
Michael Schimeke5bd0262007-01-18 16:17:39 -03001271bttv_crop_calc_limits(struct bttv_crop *c)
1272{
1273 /* Scale factor min. 1:1, max. 16:1. Min. image size
1274 48 x 32. Scaled width must be a multiple of 4. */
1275
1276 if (1) {
1277 /* For bug compatibility with VIDIOCGCAP and image
1278 size checks in earlier driver versions. */
1279 c->min_scaled_width = 48;
1280 c->min_scaled_height = 32;
1281 } else {
1282 c->min_scaled_width =
1283 (max(48, c->rect.width >> 4) + 3) & ~3;
1284 c->min_scaled_height =
1285 max(32, c->rect.height >> 4);
1286 }
1287
1288 c->max_scaled_width = c->rect.width & ~3;
1289 c->max_scaled_height = c->rect.height;
1290}
1291
1292static void
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001293bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
Michael Schimeke5bd0262007-01-18 16:17:39 -03001294{
1295 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1296 bttv_crop_calc_limits(c);
1297}
1298
1299/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300static int
1301set_tvnorm(struct bttv *btv, unsigned int norm)
1302{
1303 const struct bttv_tvnorm *tvnorm;
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001304 v4l2_std_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001306 BUG_ON(norm >= BTTV_TVNORMS);
1307 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 tvnorm = &bttv_tvnorms[norm];
1310
Mike Isely2de26c02009-09-21 12:42:22 -03001311 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001312 sizeof (tvnorm->cropcap))) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001313 bttv_crop_reset(&btv->crop[0], norm);
1314 btv->crop[1] = btv->crop[0]; /* current = default */
1315
1316 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1317 btv->crop_start = tvnorm->cropcap.bounds.top
1318 + tvnorm->cropcap.bounds.height;
1319 }
1320 }
1321
1322 btv->tvnorm = norm;
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 btwrite(tvnorm->adelay, BT848_ADELAY);
1325 btwrite(tvnorm->bdelay, BT848_BDELAY);
1326 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1327 BT848_IFORM);
1328 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1329 btwrite(1, BT848_VBI_PACK_DEL);
1330 bt848A_set_timing(btv);
1331
1332 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -08001333 case BTTV_BOARD_VOODOOTV_FM:
Wade Berrier434b2522007-06-25 13:02:16 -03001334 case BTTV_BOARD_VOODOOTV_200:
Trent Piepho72134a62009-01-28 21:32:59 -03001335 bttv_tda9880_setnorm(btv, gpio_read());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001338 id = tvnorm->v4l2_id;
Hans Verkuilf41737e2009-04-01 03:52:39 -03001339 bttv_call_all(btv, core, s_std, id);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return 0;
1342}
1343
Michael Schimeke5bd0262007-01-18 16:17:39 -03001344/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345static void
Trent Piepho333408f2007-07-03 15:08:10 -03001346set_input(struct bttv *btv, unsigned int input, unsigned int norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 unsigned long flags;
1349
1350 btv->input = input;
1351 if (irq_iswitch) {
1352 spin_lock_irqsave(&btv->s_lock,flags);
1353 if (btv->curr.frame_irq) {
1354 /* active capture -> delayed input switch */
1355 btv->new_input = input;
1356 } else {
1357 video_mux(btv,input);
1358 }
1359 spin_unlock_irqrestore(&btv->s_lock,flags);
1360 } else {
1361 video_mux(btv,input);
1362 }
Trent Piephoabb03622009-01-28 21:32:59 -03001363 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1364 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
Trent Piepho333408f2007-07-03 15:08:10 -03001365 set_tvnorm(btv, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
1368static void init_irqreg(struct bttv *btv)
1369{
1370 /* clear status */
1371 btwrite(0xfffffUL, BT848_INT_STAT);
1372
1373 if (bttv_tvcards[btv->c.type].no_video) {
1374 /* i2c only */
1375 btwrite(BT848_INT_I2CDONE,
1376 BT848_INT_MASK);
1377 } else {
1378 /* full video */
1379 btwrite((btv->triton1) |
1380 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1381 BT848_INT_SCERR |
1382 (fdsr ? BT848_INT_FDSR : 0) |
Jean Delvareeb1b27b2008-08-30 10:18:21 -03001383 BT848_INT_RISCI | BT848_INT_OCERR |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1385 BT848_INT_I2CDONE,
1386 BT848_INT_MASK);
1387 }
1388}
1389
1390static void init_bt848(struct bttv *btv)
1391{
1392 int val;
1393
1394 if (bttv_tvcards[btv->c.type].no_video) {
1395 /* very basic init only */
1396 init_irqreg(btv);
1397 return;
1398 }
1399
1400 btwrite(0x00, BT848_CAP_CTL);
1401 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1402 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1403
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001404 /* set planar and packed mode trigger points and */
1405 /* set rising edge of inverted GPINTR pin as irq trigger */
1406 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1407 BT848_GPIO_DMA_CTL_PLTP1_16|
1408 BT848_GPIO_DMA_CTL_PLTP23_16|
1409 BT848_GPIO_DMA_CTL_GPINTC|
1410 BT848_GPIO_DMA_CTL_GPINTI,
1411 BT848_GPIO_DMA_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001414 btwrite(val, BT848_E_SCLOOP);
1415 btwrite(val, BT848_O_SCLOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001417 btwrite(0x20, BT848_E_VSCALE_HI);
1418 btwrite(0x20, BT848_O_VSCALE_HI);
1419 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 BT848_ADC);
1421
1422 btwrite(whitecrush_upper, BT848_WC_UP);
1423 btwrite(whitecrush_lower, BT848_WC_DOWN);
1424
1425 if (btv->opt_lumafilter) {
1426 btwrite(0, BT848_E_CONTROL);
1427 btwrite(0, BT848_O_CONTROL);
1428 } else {
1429 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1430 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1431 }
1432
1433 bt848_bright(btv, btv->bright);
1434 bt848_hue(btv, btv->hue);
1435 bt848_contrast(btv, btv->contrast);
1436 bt848_sat(btv, btv->saturation);
1437
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001438 /* interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 init_irqreg(btv);
1440}
1441
1442static void bttv_reinit_bt848(struct bttv *btv)
1443{
1444 unsigned long flags;
1445
1446 if (bttv_verbose)
1447 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1448 spin_lock_irqsave(&btv->s_lock,flags);
1449 btv->errors=0;
1450 bttv_set_dma(btv,0);
1451 spin_unlock_irqrestore(&btv->s_lock,flags);
1452
1453 init_bt848(btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001454 btv->pll.pll_current = -1;
Trent Piepho333408f2007-07-03 15:08:10 -03001455 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001458static int bttv_g_ctrl(struct file *file, void *priv,
1459 struct v4l2_control *c)
1460{
1461 struct bttv_fh *fh = priv;
1462 struct bttv *btv = fh->btv;
1463
1464 switch (c->id) {
1465 case V4L2_CID_BRIGHTNESS:
1466 c->value = btv->bright;
1467 break;
1468 case V4L2_CID_HUE:
1469 c->value = btv->hue;
1470 break;
1471 case V4L2_CID_CONTRAST:
1472 c->value = btv->contrast;
1473 break;
1474 case V4L2_CID_SATURATION:
1475 c->value = btv->saturation;
1476 break;
1477
1478 case V4L2_CID_AUDIO_MUTE:
1479 case V4L2_CID_AUDIO_VOLUME:
1480 case V4L2_CID_AUDIO_BALANCE:
1481 case V4L2_CID_AUDIO_BASS:
1482 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil859f0272009-03-28 08:29:00 -03001483 bttv_call_all(btv, core, g_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001484 break;
1485
1486 case V4L2_CID_PRIVATE_CHROMA_AGC:
1487 c->value = btv->opt_chroma_agc;
1488 break;
1489 case V4L2_CID_PRIVATE_COMBFILTER:
1490 c->value = btv->opt_combfilter;
1491 break;
1492 case V4L2_CID_PRIVATE_LUMAFILTER:
1493 c->value = btv->opt_lumafilter;
1494 break;
1495 case V4L2_CID_PRIVATE_AUTOMUTE:
1496 c->value = btv->opt_automute;
1497 break;
1498 case V4L2_CID_PRIVATE_AGC_CRUSH:
1499 c->value = btv->opt_adc_crush;
1500 break;
1501 case V4L2_CID_PRIVATE_VCR_HACK:
1502 c->value = btv->opt_vcr_hack;
1503 break;
1504 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1505 c->value = btv->opt_whitecrush_upper;
1506 break;
1507 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1508 c->value = btv->opt_whitecrush_lower;
1509 break;
1510 case V4L2_CID_PRIVATE_UV_RATIO:
1511 c->value = btv->opt_uv_ratio;
1512 break;
1513 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1514 c->value = btv->opt_full_luma_range;
1515 break;
1516 case V4L2_CID_PRIVATE_CORING:
1517 c->value = btv->opt_coring;
1518 break;
1519 default:
1520 return -EINVAL;
1521 }
1522 return 0;
1523}
1524
1525static int bttv_s_ctrl(struct file *file, void *f,
1526 struct v4l2_control *c)
1527{
1528 int err;
1529 int val;
1530 struct bttv_fh *fh = f;
1531 struct bttv *btv = fh->btv;
1532
Hans Verkuilffb48772010-05-01 08:03:24 -03001533 err = v4l2_prio_check(&btv->prio, fh->prio);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001534 if (0 != err)
1535 return err;
1536
1537 switch (c->id) {
1538 case V4L2_CID_BRIGHTNESS:
1539 bt848_bright(btv, c->value);
1540 break;
1541 case V4L2_CID_HUE:
1542 bt848_hue(btv, c->value);
1543 break;
1544 case V4L2_CID_CONTRAST:
1545 bt848_contrast(btv, c->value);
1546 break;
1547 case V4L2_CID_SATURATION:
1548 bt848_sat(btv, c->value);
1549 break;
1550 case V4L2_CID_AUDIO_MUTE:
1551 audio_mute(btv, c->value);
1552 /* fall through */
1553 case V4L2_CID_AUDIO_VOLUME:
1554 if (btv->volume_gpio)
1555 btv->volume_gpio(btv, c->value);
1556
Hans Verkuil859f0272009-03-28 08:29:00 -03001557 bttv_call_all(btv, core, s_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001558 break;
1559 case V4L2_CID_AUDIO_BALANCE:
1560 case V4L2_CID_AUDIO_BASS:
1561 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil859f0272009-03-28 08:29:00 -03001562 bttv_call_all(btv, core, s_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001563 break;
1564
1565 case V4L2_CID_PRIVATE_CHROMA_AGC:
1566 btv->opt_chroma_agc = c->value;
1567 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1568 btwrite(val, BT848_E_SCLOOP);
1569 btwrite(val, BT848_O_SCLOOP);
1570 break;
1571 case V4L2_CID_PRIVATE_COMBFILTER:
1572 btv->opt_combfilter = c->value;
1573 break;
1574 case V4L2_CID_PRIVATE_LUMAFILTER:
1575 btv->opt_lumafilter = c->value;
1576 if (btv->opt_lumafilter) {
1577 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1578 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1579 } else {
1580 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1581 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1582 }
1583 break;
1584 case V4L2_CID_PRIVATE_AUTOMUTE:
1585 btv->opt_automute = c->value;
1586 break;
1587 case V4L2_CID_PRIVATE_AGC_CRUSH:
1588 btv->opt_adc_crush = c->value;
1589 btwrite(BT848_ADC_RESERVED |
1590 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1591 BT848_ADC);
1592 break;
1593 case V4L2_CID_PRIVATE_VCR_HACK:
1594 btv->opt_vcr_hack = c->value;
1595 break;
1596 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1597 btv->opt_whitecrush_upper = c->value;
1598 btwrite(c->value, BT848_WC_UP);
1599 break;
1600 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1601 btv->opt_whitecrush_lower = c->value;
1602 btwrite(c->value, BT848_WC_DOWN);
1603 break;
1604 case V4L2_CID_PRIVATE_UV_RATIO:
1605 btv->opt_uv_ratio = c->value;
1606 bt848_sat(btv, btv->saturation);
1607 break;
1608 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1609 btv->opt_full_luma_range = c->value;
1610 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1611 break;
1612 case V4L2_CID_PRIVATE_CORING:
1613 btv->opt_coring = c->value;
1614 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1615 break;
1616 default:
1617 return -EINVAL;
1618 }
1619 return 0;
1620}
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622/* ----------------------------------------------------------------------- */
1623
1624void bttv_gpio_tracking(struct bttv *btv, char *comment)
1625{
1626 unsigned int outbits, data;
1627 outbits = btread(BT848_GPIO_OUT_EN);
1628 data = btread(BT848_GPIO_DATA);
1629 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1630 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1631}
1632
1633static void bttv_field_count(struct bttv *btv)
1634{
1635 int need_count = 0;
1636
1637 if (btv->users)
1638 need_count++;
1639
1640 if (need_count) {
1641 /* start field counter */
1642 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1643 } else {
1644 /* stop field counter */
1645 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1646 btv->field_count = 0;
1647 }
1648}
1649
1650static const struct bttv_format*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651format_by_fourcc(int fourcc)
1652{
1653 unsigned int i;
1654
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001655 for (i = 0; i < FORMATS; i++) {
1656 if (-1 == formats[i].fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 continue;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001658 if (formats[i].fourcc == fourcc)
1659 return formats+i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
1661 return NULL;
1662}
1663
1664/* ----------------------------------------------------------------------- */
1665/* misc helpers */
1666
1667static int
1668bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1669 struct bttv_buffer *new)
1670{
1671 struct bttv_buffer *old;
1672 unsigned long flags;
1673 int retval = 0;
1674
1675 dprintk("switch_overlay: enter [new=%p]\n",new);
1676 if (new)
Brandon Philips0fc06862007-11-06 20:02:36 -03001677 new->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 spin_lock_irqsave(&btv->s_lock,flags);
1679 old = btv->screen;
1680 btv->screen = new;
1681 btv->loop_irq |= 1;
1682 bttv_set_dma(btv, 0x03);
1683 spin_unlock_irqrestore(&btv->s_lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (NULL != old) {
1685 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001686 bttv_dma_free(&fh->cap,btv, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 kfree(old);
1688 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03001689 if (NULL == new)
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03001690 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 dprintk("switch_overlay: done\n");
1692 return retval;
1693}
1694
1695/* ----------------------------------------------------------------------- */
1696/* video4linux (1) interface */
1697
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001698static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1699 struct bttv_buffer *buf,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001700 const struct bttv_format *fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 unsigned int width, unsigned int height,
1702 enum v4l2_field field)
1703{
Michael Schimeke5bd0262007-01-18 16:17:39 -03001704 struct bttv_fh *fh = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 int redo_dma_risc = 0;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001706 struct bttv_crop c;
1707 int norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 int rc;
1709
1710 /* check settings */
1711 if (NULL == fmt)
1712 return -EINVAL;
1713 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1714 width = RAW_BPL;
1715 height = RAW_LINES*2;
1716 if (width*height > buf->vb.bsize)
1717 return -EINVAL;
1718 buf->vb.size = buf->vb.bsize;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001719
1720 /* Make sure tvnorm and vbi_end remain consistent
1721 until we're done. */
1722 mutex_lock(&btv->lock);
1723
1724 norm = btv->tvnorm;
1725
1726 /* In this mode capturing always starts at defrect.top
1727 (default VDELAY), ignoring cropping parameters. */
1728 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1729 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001731 }
1732
1733 mutex_unlock(&btv->lock);
1734
1735 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1736 } else {
1737 mutex_lock(&btv->lock);
1738
1739 norm = btv->tvnorm;
1740 c = btv->crop[!!fh->do_crop];
1741
1742 mutex_unlock(&btv->lock);
1743
1744 if (width < c.min_scaled_width ||
1745 width > c.max_scaled_width ||
1746 height < c.min_scaled_height)
1747 return -EINVAL;
1748
1749 switch (field) {
1750 case V4L2_FIELD_TOP:
1751 case V4L2_FIELD_BOTTOM:
1752 case V4L2_FIELD_ALTERNATE:
1753 /* btv->crop counts frame lines. Max. scale
1754 factor is 16:1 for frames, 8:1 for fields. */
1755 if (height * 2 > c.max_scaled_height)
1756 return -EINVAL;
1757 break;
1758
1759 default:
1760 if (height > c.max_scaled_height)
1761 return -EINVAL;
1762 break;
1763 }
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 buf->vb.size = (width * height * fmt->depth) >> 3;
1766 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1767 return -EINVAL;
1768 }
1769
1770 /* alloc + fill struct bttv_buffer (if changed) */
1771 if (buf->vb.width != width || buf->vb.height != height ||
1772 buf->vb.field != field ||
Michael Schimeke5bd0262007-01-18 16:17:39 -03001773 buf->tvnorm != norm || buf->fmt != fmt ||
1774 buf->crop.top != c.rect.top ||
1775 buf->crop.left != c.rect.left ||
1776 buf->crop.width != c.rect.width ||
1777 buf->crop.height != c.rect.height) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 buf->vb.width = width;
1779 buf->vb.height = height;
1780 buf->vb.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001781 buf->tvnorm = norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 buf->fmt = fmt;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001783 buf->crop = c.rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 redo_dma_risc = 1;
1785 }
1786
1787 /* alloc risc memory */
Brandon Philips0fc06862007-11-06 20:02:36 -03001788 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 redo_dma_risc = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001790 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 goto fail;
1792 }
1793
1794 if (redo_dma_risc)
1795 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1796 goto fail;
1797
Brandon Philips0fc06862007-11-06 20:02:36 -03001798 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return 0;
1800
1801 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001802 bttv_dma_free(q,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return rc;
1804}
1805
1806static int
1807buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1808{
1809 struct bttv_fh *fh = q->priv_data;
1810
1811 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1812 if (0 == *count)
1813 *count = gbuffers;
Andreas Bombedab7e312010-03-21 16:02:45 -03001814 if (*size * *count > gbuffers * gbufsize)
1815 *count = (gbuffers * gbufsize) / *size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return 0;
1817}
1818
1819static int
1820buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1821 enum v4l2_field field)
1822{
1823 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1824 struct bttv_fh *fh = q->priv_data;
1825
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001826 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 fh->width, fh->height, field);
1828}
1829
1830static void
1831buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1832{
1833 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1834 struct bttv_fh *fh = q->priv_data;
1835 struct bttv *btv = fh->btv;
1836
Brandon Philips0fc06862007-11-06 20:02:36 -03001837 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 list_add_tail(&buf->vb.queue,&btv->capture);
1839 if (!btv->curr.frame_irq) {
1840 btv->loop_irq |= 1;
1841 bttv_set_dma(btv, 0x03);
1842 }
1843}
1844
1845static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1846{
1847 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1848 struct bttv_fh *fh = q->priv_data;
1849
Michael Schimekfeaba7a2007-01-26 08:30:05 -03001850 bttv_dma_free(q,fh->btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
1853static struct videobuf_queue_ops bttv_video_qops = {
1854 .buf_setup = buffer_setup,
1855 .buf_prepare = buffer_prepare,
1856 .buf_queue = buffer_queue,
1857 .buf_release = buffer_release,
1858};
1859
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001860static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001862 struct bttv_fh *fh = priv;
1863 struct bttv *btv = fh->btv;
1864 unsigned int i;
1865 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001867 mutex_lock(&btv->lock);
Hans Verkuilffb48772010-05-01 08:03:24 -03001868 err = v4l2_prio_check(&btv->prio, fh->prio);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001869 if (err)
1870 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001872 for (i = 0; i < BTTV_TVNORMS; i++)
1873 if (*id & bttv_tvnorms[i].v4l2_id)
1874 break;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001875 if (i == BTTV_TVNORMS) {
1876 err = -EINVAL;
1877 goto err;
1878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001880 set_tvnorm(btv, i);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001881
1882err:
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001883 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001885 return err;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001886}
1887
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001888static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001889{
1890 struct bttv_fh *fh = f;
1891 struct bttv *btv = fh->btv;
1892
1893 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1894 *id = V4L2_STD_625_50;
1895 else
1896 *id = V4L2_STD_525_60;
1897 return 0;
1898}
1899
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001900static int bttv_enum_input(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001901 struct v4l2_input *i)
1902{
1903 struct bttv_fh *fh = priv;
1904 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001905 int rc = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001906
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001907 mutex_lock(&btv->lock);
1908 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) {
1909 rc = -EINVAL;
1910 goto err;
1911 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001912
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001913 i->type = V4L2_INPUT_TYPE_CAMERA;
1914 i->audioset = 1;
1915
Trent Piephoabb03622009-01-28 21:32:59 -03001916 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001917 sprintf(i->name, "Television");
1918 i->type = V4L2_INPUT_TYPE_TUNER;
1919 i->tuner = 0;
1920 } else if (i->index == btv->svhs) {
1921 sprintf(i->name, "S-Video");
1922 } else {
1923 sprintf(i->name, "Composite%d", i->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001926 if (i->index == btv->input) {
1927 __u32 dstatus = btread(BT848_DSTATUS);
1928 if (0 == (dstatus & BT848_DSTATUS_PRES))
1929 i->status |= V4L2_IN_ST_NO_SIGNAL;
1930 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1931 i->status |= V4L2_IN_ST_NO_H_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001934 i->std = BTTV_NORMS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001936err:
1937 mutex_unlock(&btv->lock);
1938
1939 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001940}
Nickolay V. Shmyrev4b9b9362006-08-25 16:53:04 -03001941
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001942static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001943{
1944 struct bttv_fh *fh = priv;
1945 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001947 mutex_lock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001948 *i = btv->input;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001949 mutex_unlock(&btv->lock);
1950
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001951 return 0;
1952}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001954static int bttv_s_input(struct file *file, void *priv, unsigned int i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001955{
1956 struct bttv_fh *fh = priv;
1957 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001959 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001961 mutex_lock(&btv->lock);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001962 err = v4l2_prio_check(&btv->prio, fh->prio);
1963 if (unlikely(err))
1964 goto err;
1965
1966 if (i > bttv_tvcards[btv->c.type].video_inputs) {
1967 err = -EINVAL;
1968 goto err;
1969 }
1970
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001971 set_input(btv, i, btv->tvnorm);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001972
1973err:
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001974 mutex_unlock(&btv->lock);
1975 return 0;
1976}
1977
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001978static int bttv_s_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001979 struct v4l2_tuner *t)
1980{
1981 struct bttv_fh *fh = priv;
1982 struct bttv *btv = fh->btv;
1983 int err;
1984
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001985 if (unlikely(0 != t->index))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001986 return -EINVAL;
1987
1988 mutex_lock(&btv->lock);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03001989 if (unlikely(btv->tuner_type == TUNER_ABSENT)) {
1990 err = -EINVAL;
1991 goto err;
1992 }
1993
1994 err = v4l2_prio_check(&btv->prio, fh->prio);
1995 if (unlikely(err))
1996 goto err;
1997
Hans Verkuil859f0272009-03-28 08:29:00 -03001998 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001999
2000 if (btv->audio_mode_gpio)
2001 btv->audio_mode_gpio(btv, t, 1);
2002
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002003err:
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002004 mutex_unlock(&btv->lock);
2005
2006 return 0;
2007}
2008
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002009static int bttv_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002010 struct v4l2_frequency *f)
2011{
2012 struct bttv_fh *fh = priv;
2013 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002014
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002015 mutex_lock(&btv->lock);
Robert Fitzsimons1b069012008-04-01 11:41:54 -03002016 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002017 f->frequency = btv->freq;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002018 mutex_unlock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002019
2020 return 0;
2021}
2022
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002023static int bttv_s_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002024 struct v4l2_frequency *f)
2025{
2026 struct bttv_fh *fh = priv;
2027 struct bttv *btv = fh->btv;
2028 int err;
2029
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002030 if (unlikely(f->tuner != 0))
2031 return -EINVAL;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002032
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002033 mutex_lock(&btv->lock);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002034 err = v4l2_prio_check(&btv->prio, fh->prio);
2035 if (unlikely(err))
2036 goto err;
2037
2038 if (unlikely(f->type != (btv->radio_user
2039 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV))) {
2040 err = -EINVAL;
2041 goto err;
2042 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002043 btv->freq = f->frequency;
Hans Verkuil859f0272009-03-28 08:29:00 -03002044 bttv_call_all(btv, tuner, s_frequency, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002045 if (btv->has_matchbox && btv->radio_user)
2046 tea5757_set_freq(btv, btv->freq);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002047err:
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002048 mutex_unlock(&btv->lock);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002049
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002050 return 0;
2051}
2052
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002053static int bttv_log_status(struct file *file, void *f)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002054{
2055 struct bttv_fh *fh = f;
2056 struct bttv *btv = fh->btv;
2057
2058 printk(KERN_INFO "bttv%d: ======== START STATUS CARD #%d ========\n",
2059 btv->c.nr, btv->c.nr);
Hans Verkuil859f0272009-03-28 08:29:00 -03002060 bttv_call_all(btv, core, log_status);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002061 printk(KERN_INFO "bttv%d: ======== END STATUS CARD #%d ========\n",
2062 btv->c.nr, btv->c.nr);
2063 return 0;
2064}
2065
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002066#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002067static int bttv_g_register(struct file *file, void *f,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002068 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002069{
2070 struct bttv_fh *fh = f;
2071 struct bttv *btv = fh->btv;
2072
2073 if (!capable(CAP_SYS_ADMIN))
2074 return -EPERM;
2075
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002076 if (!v4l2_chip_match_host(&reg->match))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002077 return -EINVAL;
2078
2079 /* bt848 has a 12-bit register space */
2080 reg->reg &= 0xfff;
2081 reg->val = btread(reg->reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002082 reg->size = 1;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002083
2084 return 0;
2085}
2086
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002087static int bttv_s_register(struct file *file, void *f,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002088 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002089{
2090 struct bttv_fh *fh = f;
2091 struct bttv *btv = fh->btv;
2092
2093 if (!capable(CAP_SYS_ADMIN))
2094 return -EPERM;
2095
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002096 if (!v4l2_chip_match_host(&reg->match))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002097 return -EINVAL;
2098
2099 /* bt848 has a 12-bit register space */
2100 reg->reg &= 0xfff;
2101 btwrite(reg->val, reg->reg);
2102
2103 return 0;
2104}
2105#endif
2106
Michael Schimeke5bd0262007-01-18 16:17:39 -03002107/* Given cropping boundaries b and the scaled width and height of a
2108 single field or frame, which must not exceed hardware limits, this
2109 function adjusts the cropping parameters c. */
2110static void
2111bttv_crop_adjust (struct bttv_crop * c,
2112 const struct v4l2_rect * b,
2113 __s32 width,
2114 __s32 height,
2115 enum v4l2_field field)
2116{
2117 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2118 __s32 max_left;
2119 __s32 max_top;
2120
2121 if (width < c->min_scaled_width) {
2122 /* Max. hor. scale factor 16:1. */
2123 c->rect.width = width * 16;
2124 } else if (width > c->max_scaled_width) {
2125 /* Min. hor. scale factor 1:1. */
2126 c->rect.width = width;
2127
2128 max_left = b->left + b->width - width;
2129 max_left = min(max_left, (__s32) MAX_HDELAY);
2130 if (c->rect.left > max_left)
2131 c->rect.left = max_left;
2132 }
2133
2134 if (height < c->min_scaled_height) {
2135 /* Max. vert. scale factor 16:1, single fields 8:1. */
2136 c->rect.height = height * 16;
2137 } else if (frame_height > c->max_scaled_height) {
2138 /* Min. vert. scale factor 1:1.
2139 Top and height count field lines times two. */
2140 c->rect.height = (frame_height + 1) & ~1;
2141
2142 max_top = b->top + b->height - c->rect.height;
2143 if (c->rect.top > max_top)
2144 c->rect.top = max_top;
2145 }
2146
2147 bttv_crop_calc_limits(c);
2148}
2149
2150/* Returns an error if scaling to a frame or single field with the given
2151 width and height is not possible with the current cropping parameters
2152 and width aligned according to width_mask. If adjust_size is TRUE the
2153 function may adjust the width and/or height instead, rounding width
2154 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2155 also adjust the current cropping parameters to get closer to the
2156 desired image size. */
2157static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002158limit_scaled_size_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002159 __s32 * width,
2160 __s32 * height,
2161 enum v4l2_field field,
2162 unsigned int width_mask,
2163 unsigned int width_bias,
2164 int adjust_size,
2165 int adjust_crop)
2166{
2167 struct bttv *btv = fh->btv;
2168 const struct v4l2_rect *b;
2169 struct bttv_crop *c;
2170 __s32 min_width;
2171 __s32 min_height;
2172 __s32 max_width;
2173 __s32 max_height;
2174 int rc;
2175
2176 BUG_ON((int) width_mask >= 0 ||
2177 width_bias >= (unsigned int) -width_mask);
2178
2179 /* Make sure tvnorm, vbi_end and the current cropping parameters
2180 remain consistent until we're done. */
2181 mutex_lock(&btv->lock);
2182
2183 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2184
2185 /* Do crop - use current, don't - use default parameters. */
2186 c = &btv->crop[!!fh->do_crop];
2187
2188 if (fh->do_crop
2189 && adjust_size
2190 && adjust_crop
2191 && !locked_btres(btv, VIDEO_RESOURCES)) {
2192 min_width = 48;
2193 min_height = 32;
2194
2195 /* We cannot scale up. When the scaled image is larger
2196 than crop.rect we adjust the crop.rect as required
2197 by the V4L2 spec, hence cropcap.bounds are our limit. */
2198 max_width = min(b->width, (__s32) MAX_HACTIVE);
2199 max_height = b->height;
2200
2201 /* We cannot capture the same line as video and VBI data.
2202 Note btv->vbi_end is really a minimum, see
2203 bttv_vbi_try_fmt(). */
2204 if (btv->vbi_end > b->top) {
2205 max_height -= btv->vbi_end - b->top;
2206 rc = -EBUSY;
2207 if (min_height > max_height)
2208 goto fail;
2209 }
2210 } else {
2211 rc = -EBUSY;
2212 if (btv->vbi_end > c->rect.top)
2213 goto fail;
2214
2215 min_width = c->min_scaled_width;
2216 min_height = c->min_scaled_height;
2217 max_width = c->max_scaled_width;
2218 max_height = c->max_scaled_height;
2219
2220 adjust_crop = 0;
2221 }
2222
2223 min_width = (min_width - width_mask - 1) & width_mask;
2224 max_width = max_width & width_mask;
2225
2226 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2227 min_height = min_height;
2228 /* Min. scale factor is 1:1. */
2229 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2230
2231 if (adjust_size) {
2232 *width = clamp(*width, min_width, max_width);
2233 *height = clamp(*height, min_height, max_height);
2234
2235 /* Round after clamping to avoid overflow. */
2236 *width = (*width + width_bias) & width_mask;
2237
2238 if (adjust_crop) {
2239 bttv_crop_adjust(c, b, *width, *height, field);
2240
2241 if (btv->vbi_end > c->rect.top) {
2242 /* Move the crop window out of the way. */
2243 c->rect.top = btv->vbi_end;
2244 }
2245 }
2246 } else {
2247 rc = -EINVAL;
2248 if (*width < min_width ||
2249 *height < min_height ||
2250 *width > max_width ||
2251 *height > max_height ||
2252 0 != (*width & ~width_mask))
2253 goto fail;
2254 }
2255
2256 rc = 0; /* success */
2257
2258 fail:
2259 mutex_unlock(&btv->lock);
2260
2261 return rc;
2262}
2263
2264/* Returns an error if the given overlay window dimensions are not
2265 possible with the current cropping parameters. If adjust_size is
2266 TRUE the function may adjust the window width and/or height
2267 instead, however it always rounds the horizontal position and
2268 width as btcx_align() does. If adjust_crop is TRUE the function
2269 may also adjust the current cropping parameters to get closer
2270 to the desired window size. */
2271static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002272verify_window_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002273 struct v4l2_window * win,
2274 int adjust_size,
2275 int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002278 unsigned int width_mask;
2279 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 if (win->w.width < 48 || win->w.height < 32)
2282 return -EINVAL;
2283 if (win->clipcount > 2048)
2284 return -EINVAL;
2285
2286 field = win->field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 if (V4L2_FIELD_ANY == field) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002289 __s32 height2;
2290
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002291 mutex_lock(&fh->btv->lock);
Michael Schimeke5bd0262007-01-18 16:17:39 -03002292 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002293 mutex_unlock(&fh->btv->lock);
Michael Schimeke5bd0262007-01-18 16:17:39 -03002294 field = (win->w.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 ? V4L2_FIELD_INTERLACED
2296 : V4L2_FIELD_TOP;
2297 }
2298 switch (field) {
2299 case V4L2_FIELD_TOP:
2300 case V4L2_FIELD_BOTTOM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 case V4L2_FIELD_INTERLACED:
2302 break;
2303 default:
2304 return -EINVAL;
2305 }
2306
Michael Schimeke5bd0262007-01-18 16:17:39 -03002307 /* 4-byte alignment. */
2308 if (NULL == fh->ovfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002310 width_mask = ~0;
2311 switch (fh->ovfmt->depth) {
2312 case 8:
2313 case 24:
2314 width_mask = ~3;
2315 break;
2316 case 16:
2317 width_mask = ~1;
2318 break;
2319 case 32:
2320 break;
2321 default:
2322 BUG();
2323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Michael Schimeke5bd0262007-01-18 16:17:39 -03002325 win->w.width -= win->w.left & ~width_mask;
2326 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2327
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002328 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002329 field, width_mask,
2330 /* width_bias: round down */ 0,
2331 adjust_size, adjust_crop);
2332 if (0 != rc)
2333 return rc;
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 win->field = field;
2336 return 0;
2337}
2338
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002339static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 struct v4l2_window *win, int fixup)
2341{
2342 struct v4l2_clip *clips = NULL;
2343 int n,size,retval = 0;
2344
2345 if (NULL == fh->ovfmt)
2346 return -EINVAL;
2347 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2348 return -EINVAL;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002349 retval = verify_window_lock(fh, win,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002350 /* adjust_size */ fixup,
2351 /* adjust_crop */ fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (0 != retval)
2353 return retval;
2354
2355 /* copy clips -- luckily v4l1 + v4l2 are binary
2356 compatible here ...*/
2357 n = win->clipcount;
2358 size = sizeof(*clips)*(n+4);
2359 clips = kmalloc(size,GFP_KERNEL);
2360 if (NULL == clips)
2361 return -ENOMEM;
2362 if (n > 0) {
2363 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2364 kfree(clips);
2365 return -EFAULT;
2366 }
2367 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002368
2369 mutex_lock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 /* clip against screen */
2371 if (NULL != btv->fbuf.base)
2372 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2373 &win->w, clips, n);
2374 btcx_sort_clips(clips,n);
2375
2376 /* 4-byte alignments */
2377 switch (fh->ovfmt->depth) {
2378 case 8:
2379 case 24:
2380 btcx_align(&win->w, clips, n, 3);
2381 break;
2382 case 16:
2383 btcx_align(&win->w, clips, n, 1);
2384 break;
2385 case 32:
2386 /* no alignment fixups needed */
2387 break;
2388 default:
2389 BUG();
2390 }
2391
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002392 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 fh->ov.clips = clips;
2394 fh->ov.nclips = n;
2395
2396 fh->ov.w = win->w;
2397 fh->ov.field = win->field;
2398 fh->ov.setup_ok = 1;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002399
2400 /*
2401 * FIXME: btv is protected by btv->lock mutex, while btv->init
2402 * is protected by fh->cap.vb_lock. This seems to open the
2403 * possibility for some race situations. Maybe the better would
2404 * be to unify those locks or to use another way to store the
2405 * init values that will be consumed by videobuf callbacks
2406 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 btv->init.ov.w.width = win->w.width;
2408 btv->init.ov.w.height = win->w.height;
2409 btv->init.ov.field = win->field;
2410
2411 /* update overlay if needed */
2412 retval = 0;
2413 if (check_btres(fh, RESOURCE_OVERLAY)) {
2414 struct bttv_buffer *new;
2415
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002416 new = videobuf_sg_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03002417 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2419 retval = bttv_switch_overlay(btv,fh,new);
2420 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002421 mutex_unlock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return retval;
2423}
2424
2425/* ----------------------------------------------------------------------- */
2426
2427static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2428{
2429 struct videobuf_queue* q = NULL;
2430
2431 switch (fh->type) {
2432 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2433 q = &fh->cap;
2434 break;
2435 case V4L2_BUF_TYPE_VBI_CAPTURE:
2436 q = &fh->vbi;
2437 break;
2438 default:
2439 BUG();
2440 }
2441 return q;
2442}
2443
2444static int bttv_resource(struct bttv_fh *fh)
2445{
2446 int res = 0;
2447
2448 switch (fh->type) {
2449 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002450 res = RESOURCE_VIDEO_STREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 break;
2452 case V4L2_BUF_TYPE_VBI_CAPTURE:
2453 res = RESOURCE_VBI;
2454 break;
2455 default:
2456 BUG();
2457 }
2458 return res;
2459}
2460
2461static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2462{
2463 struct videobuf_queue *q = bttv_queue(fh);
2464 int res = bttv_resource(fh);
2465
2466 if (check_btres(fh,res))
2467 return -EBUSY;
2468 if (videobuf_queue_is_busy(q))
2469 return -EBUSY;
2470 fh->type = type;
2471 return 0;
2472}
2473
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002474static void
2475pix_format_set_size (struct v4l2_pix_format * f,
2476 const struct bttv_format * fmt,
2477 unsigned int width,
2478 unsigned int height)
2479{
2480 f->width = width;
2481 f->height = height;
2482
2483 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2484 f->bytesperline = width; /* Y plane */
2485 f->sizeimage = (width * height * fmt->depth) >> 3;
2486 } else {
2487 f->bytesperline = (width * fmt->depth) >> 3;
2488 f->sizeimage = height * f->bytesperline;
2489 }
2490}
2491
Hans Verkuil78b526a2008-05-28 12:16:41 -03002492static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002493 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002495 struct bttv_fh *fh = priv;
2496
2497 pix_format_set_size(&f->fmt.pix, fh->fmt,
2498 fh->width, fh->height);
2499 f->fmt.pix.field = fh->cap.field;
2500 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2501
2502 return 0;
2503}
2504
Hans Verkuil78b526a2008-05-28 12:16:41 -03002505static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002506 struct v4l2_format *f)
2507{
2508 struct bttv_fh *fh = priv;
2509
2510 f->fmt.win.w = fh->ov.w;
2511 f->fmt.win.field = fh->ov.field;
2512
2513 return 0;
2514}
2515
Hans Verkuil78b526a2008-05-28 12:16:41 -03002516static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002517 struct v4l2_format *f)
2518{
2519 const struct bttv_format *fmt;
2520 struct bttv_fh *fh = priv;
2521 struct bttv *btv = fh->btv;
2522 enum v4l2_field field;
2523 __s32 width, height;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002524 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002525
2526 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2527 if (NULL == fmt)
2528 return -EINVAL;
2529
2530 field = f->fmt.pix.field;
2531
2532 if (V4L2_FIELD_ANY == field) {
2533 __s32 height2;
2534
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002535 mutex_lock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002536 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002537 mutex_unlock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002538 field = (f->fmt.pix.height > height2)
2539 ? V4L2_FIELD_INTERLACED
2540 : V4L2_FIELD_BOTTOM;
2541 }
2542
2543 if (V4L2_FIELD_SEQ_BT == field)
2544 field = V4L2_FIELD_SEQ_TB;
2545
2546 switch (field) {
2547 case V4L2_FIELD_TOP:
2548 case V4L2_FIELD_BOTTOM:
2549 case V4L2_FIELD_ALTERNATE:
2550 case V4L2_FIELD_INTERLACED:
2551 break;
2552 case V4L2_FIELD_SEQ_TB:
2553 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2554 return -EINVAL;
2555 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 default:
2557 return -EINVAL;
2558 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002559
2560 width = f->fmt.pix.width;
2561 height = f->fmt.pix.height;
2562
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002563 rc = limit_scaled_size_lock(fh, &width, &height, field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002564 /* width_mask: 4 pixels */ ~3,
2565 /* width_bias: nearest */ 2,
2566 /* adjust_size */ 1,
2567 /* adjust_crop */ 0);
2568 if (0 != rc)
2569 return rc;
2570
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002571 /* update data for the application */
2572 f->fmt.pix.field = field;
2573 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2574
2575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
Hans Verkuil78b526a2008-05-28 12:16:41 -03002578static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002579 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002581 struct bttv_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002583 return verify_window_lock(fh, &f->fmt.win,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002584 /* adjust_size */ 1,
2585 /* adjust_crop */ 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
2587
Hans Verkuil78b526a2008-05-28 12:16:41 -03002588static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002589 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
2591 int retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002592 const struct bttv_format *fmt;
2593 struct bttv_fh *fh = priv;
2594 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002595 __s32 width, height;
2596 enum v4l2_field field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002598 retval = bttv_switch_type(fh, f->type);
2599 if (0 != retval)
2600 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Hans Verkuil78b526a2008-05-28 12:16:41 -03002602 retval = bttv_try_fmt_vid_cap(file, priv, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002603 if (0 != retval)
2604 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002606 width = f->fmt.pix.width;
2607 height = f->fmt.pix.height;
2608 field = f->fmt.pix.field;
2609
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002610 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002611 /* width_mask: 4 pixels */ ~3,
2612 /* width_bias: nearest */ 2,
2613 /* adjust_size */ 1,
2614 /* adjust_crop */ 1);
2615 if (0 != retval)
2616 return retval;
2617
2618 f->fmt.pix.field = field;
2619
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002620 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002622 /* update our state informations */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002623 mutex_lock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002624 fh->fmt = fmt;
2625 fh->cap.field = f->fmt.pix.field;
2626 fh->cap.last = V4L2_FIELD_NONE;
2627 fh->width = f->fmt.pix.width;
2628 fh->height = f->fmt.pix.height;
2629 btv->init.fmt = fmt;
2630 btv->init.width = f->fmt.pix.width;
2631 btv->init.height = f->fmt.pix.height;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002632 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002633
2634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
Hans Verkuil78b526a2008-05-28 12:16:41 -03002637static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002638 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002640 struct bttv_fh *fh = priv;
2641 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002643 if (no_overlay > 0) {
2644 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002645 return -EINVAL;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002646 }
Michael Krufky5e453dc2006-01-09 15:32:31 -02002647
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002648 return setup_window_lock(fh, btv, &f->fmt.win, 1);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002649}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002651#ifdef CONFIG_VIDEO_V4L1_COMPAT
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002652static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2653{
2654 int retval;
2655 unsigned int i;
2656 struct bttv_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002658 mutex_lock(&fh->cap.vb_lock);
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -07002659 retval = __videobuf_mmap_setup(&fh->cap, gbuffers, gbufsize,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002660 V4L2_MEMORY_MMAP);
2661 if (retval < 0) {
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002662 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002663 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002665
2666 gbuffers = retval;
2667 memset(mbuf, 0, sizeof(*mbuf));
2668 mbuf->frames = gbuffers;
2669 mbuf->size = gbuffers * gbufsize;
2670
2671 for (i = 0; i < gbuffers; i++)
2672 mbuf->offsets[i] = i * gbufsize;
2673
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002674 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002675 return 0;
2676}
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002677#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002679static int bttv_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002680 struct v4l2_capability *cap)
2681{
2682 struct bttv_fh *fh = priv;
2683 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002685 if (0 == v4l2)
2686 return -EINVAL;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002687
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002688 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2689 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2690 snprintf(cap->bus_info, sizeof(cap->bus_info),
2691 "PCI:%s", pci_name(btv->c.pci));
2692 cap->version = BTTV_VERSION_CODE;
2693 cap->capabilities =
2694 V4L2_CAP_VIDEO_CAPTURE |
2695 V4L2_CAP_VBI_CAPTURE |
2696 V4L2_CAP_READWRITE |
2697 V4L2_CAP_STREAMING;
2698 if (no_overlay <= 0)
2699 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002701 /*
2702 * No need to lock here: those vars are initialized during board
2703 * probe and remains untouched during the rest of the driver lifecycle
2704 */
2705 if (btv->has_saa6588)
2706 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
Trent Piephoabb03622009-01-28 21:32:59 -03002707 if (btv->tuner_type != TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002708 cap->capabilities |= V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 return 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002710}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002712static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2713{
2714 int index = -1, i;
2715
2716 for (i = 0; i < FORMATS; i++) {
2717 if (formats[i].fourcc != -1)
2718 index++;
2719 if ((unsigned int)index == f->index)
2720 break;
2721 }
2722 if (FORMATS == i)
2723 return -EINVAL;
2724
2725 f->pixelformat = formats[i].fourcc;
2726 strlcpy(f->description, formats[i].name, sizeof(f->description));
2727
2728 return i;
2729}
2730
Hans Verkuil78b526a2008-05-28 12:16:41 -03002731static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002732 struct v4l2_fmtdesc *f)
2733{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002734 int rc = bttv_enum_fmt_cap_ovr(f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002735
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002736 if (rc < 0)
2737 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002738
2739 return 0;
2740}
2741
Hans Verkuil78b526a2008-05-28 12:16:41 -03002742static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002743 struct v4l2_fmtdesc *f)
2744{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002745 int rc;
2746
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002747 if (no_overlay > 0) {
2748 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2749 return -EINVAL;
2750 }
2751
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002752 rc = bttv_enum_fmt_cap_ovr(f);
2753
2754 if (rc < 0)
2755 return rc;
2756
2757 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002758 return -EINVAL;
2759
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002760 return 0;
2761}
2762
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002763static int bttv_g_fbuf(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002764 struct v4l2_framebuffer *fb)
2765{
2766 struct bttv_fh *fh = f;
2767 struct bttv *btv = fh->btv;
2768
2769 *fb = btv->fbuf;
2770 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2771 if (fh->ovfmt)
2772 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2773 return 0;
2774}
2775
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002776static int bttv_overlay(struct file *file, void *f, unsigned int on)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002777{
2778 struct bttv_fh *fh = f;
2779 struct bttv *btv = fh->btv;
2780 struct bttv_buffer *new;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002781 int retval = 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002782
2783 if (on) {
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002784 mutex_lock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002785 /* verify args */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002786 if (unlikely(!btv->fbuf.base)) {
2787 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002788 return -EINVAL;
2789 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002790 if (unlikely(!fh->ov.setup_ok)) {
2791 dprintk("bttv%d: overlay: !setup_ok\n", btv->c.nr);
2792 retval = -EINVAL;
2793 }
2794 if (retval)
2795 return retval;
2796 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002797 }
2798
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002799 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002800 return -EBUSY;
2801
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002802 mutex_lock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002803 if (on) {
2804 fh->ov.tvnorm = btv->tvnorm;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002805 new = videobuf_sg_alloc(sizeof(*new));
Robert Fitzsimons7c018802008-02-13 16:38:11 -03002806 new->crop = btv->crop[!!fh->do_crop].rect;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002807 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2808 } else {
2809 new = NULL;
2810 }
2811
2812 /* switch over */
2813 retval = bttv_switch_overlay(btv, fh, new);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002814 mutex_unlock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return retval;
2816}
2817
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002818static int bttv_s_fbuf(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002819 struct v4l2_framebuffer *fb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002821 struct bttv_fh *fh = f;
2822 struct bttv *btv = fh->btv;
2823 const struct bttv_format *fmt;
2824 int retval;
2825
2826 if (!capable(CAP_SYS_ADMIN) &&
2827 !capable(CAP_SYS_RAWIO))
2828 return -EPERM;
2829
2830 /* check args */
2831 fmt = format_by_fourcc(fb->fmt.pixelformat);
2832 if (NULL == fmt)
2833 return -EINVAL;
2834 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2835 return -EINVAL;
2836
2837 retval = -EINVAL;
2838 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2839 __s32 width = fb->fmt.width;
2840 __s32 height = fb->fmt.height;
2841
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002842 retval = limit_scaled_size_lock(fh, &width, &height,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002843 V4L2_FIELD_INTERLACED,
2844 /* width_mask */ ~3,
2845 /* width_bias */ 2,
2846 /* adjust_size */ 0,
2847 /* adjust_crop */ 0);
2848 if (0 != retval)
2849 return retval;
2850 }
2851
2852 /* ok, accept it */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002853 mutex_lock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002854 btv->fbuf.base = fb->base;
2855 btv->fbuf.fmt.width = fb->fmt.width;
2856 btv->fbuf.fmt.height = fb->fmt.height;
2857 if (0 != fb->fmt.bytesperline)
2858 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2859 else
2860 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2861
2862 retval = 0;
2863 fh->ovfmt = fmt;
2864 btv->init.ovfmt = fmt;
2865 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2866 fh->ov.w.left = 0;
2867 fh->ov.w.top = 0;
2868 fh->ov.w.width = fb->fmt.width;
2869 fh->ov.w.height = fb->fmt.height;
2870 btv->init.ov.w.width = fb->fmt.width;
2871 btv->init.ov.w.height = fb->fmt.height;
2872 kfree(fh->ov.clips);
2873 fh->ov.clips = NULL;
2874 fh->ov.nclips = 0;
2875
2876 if (check_btres(fh, RESOURCE_OVERLAY)) {
2877 struct bttv_buffer *new;
2878
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002879 new = videobuf_sg_alloc(sizeof(*new));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002880 new->crop = btv->crop[!!fh->do_crop].rect;
2881 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2882 retval = bttv_switch_overlay(btv, fh, new);
2883 }
2884 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002885 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002886 return retval;
2887}
2888
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002889static int bttv_reqbufs(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002890 struct v4l2_requestbuffers *p)
2891{
2892 struct bttv_fh *fh = priv;
2893 return videobuf_reqbufs(bttv_queue(fh), p);
2894}
2895
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002896static int bttv_querybuf(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002897 struct v4l2_buffer *b)
2898{
2899 struct bttv_fh *fh = priv;
2900 return videobuf_querybuf(bttv_queue(fh), b);
2901}
2902
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002903static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002904{
2905 struct bttv_fh *fh = priv;
2906 struct bttv *btv = fh->btv;
2907 int res = bttv_resource(fh);
2908
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002909 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002910 return -EBUSY;
2911
2912 return videobuf_qbuf(bttv_queue(fh), b);
2913}
2914
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002915static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002916{
2917 struct bttv_fh *fh = priv;
2918 return videobuf_dqbuf(bttv_queue(fh), b,
2919 file->f_flags & O_NONBLOCK);
2920}
2921
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002922static int bttv_streamon(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002923 enum v4l2_buf_type type)
2924{
2925 struct bttv_fh *fh = priv;
2926 struct bttv *btv = fh->btv;
2927 int res = bttv_resource(fh);
2928
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002929 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002930 return -EBUSY;
2931 return videobuf_streamon(bttv_queue(fh));
2932}
2933
2934
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002935static int bttv_streamoff(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002936 enum v4l2_buf_type type)
2937{
2938 struct bttv_fh *fh = priv;
2939 struct bttv *btv = fh->btv;
2940 int retval;
2941 int res = bttv_resource(fh);
2942
2943
2944 retval = videobuf_streamoff(bttv_queue(fh));
2945 if (retval < 0)
2946 return retval;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002947 free_btres_lock(btv, fh, res);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002948 return 0;
2949}
2950
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002951static int bttv_queryctrl(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002952 struct v4l2_queryctrl *c)
2953{
2954 struct bttv_fh *fh = priv;
2955 struct bttv *btv = fh->btv;
2956 const struct v4l2_queryctrl *ctrl;
2957
2958 if ((c->id < V4L2_CID_BASE ||
2959 c->id >= V4L2_CID_LASTP1) &&
2960 (c->id < V4L2_CID_PRIVATE_BASE ||
2961 c->id >= V4L2_CID_PRIVATE_LASTP1))
2962 return -EINVAL;
2963
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002964 mutex_lock(&btv->lock);
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002965 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2966 *c = no_ctl;
2967 else {
2968 ctrl = ctrl_by_id(c->id);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002969
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002970 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2971 }
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002972 mutex_unlock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002973
2974 return 0;
2975}
2976
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002977static int bttv_g_parm(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002978 struct v4l2_streamparm *parm)
2979{
2980 struct bttv_fh *fh = f;
2981 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002982
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002983 mutex_lock(&btv->lock);
Trent Piepho51f0b8d2009-03-04 01:21:02 -03002984 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2985 &parm->parm.capture.timeperframe);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03002986 mutex_unlock(&btv->lock);
2987
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002988 return 0;
2989}
2990
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002991static int bttv_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002992 struct v4l2_tuner *t)
2993{
2994 struct bttv_fh *fh = priv;
2995 struct bttv *btv = fh->btv;
2996
Trent Piephoabb03622009-01-28 21:32:59 -03002997 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002998 return -EINVAL;
2999 if (0 != t->index)
3000 return -EINVAL;
3001
3002 mutex_lock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003003 t->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil859f0272009-03-28 08:29:00 -03003004 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003005 strcpy(t->name, "Television");
3006 t->capability = V4L2_TUNER_CAP_NORM;
3007 t->type = V4L2_TUNER_ANALOG_TV;
3008 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
3009 t->signal = 0xffff;
3010
3011 if (btv->audio_mode_gpio)
3012 btv->audio_mode_gpio(btv, t, 0);
3013
3014 mutex_unlock(&btv->lock);
3015 return 0;
3016}
3017
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003018static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003019{
3020 struct bttv_fh *fh = f;
3021 struct bttv *btv = fh->btv;
3022
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003023 mutex_lock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003024 *p = v4l2_prio_max(&btv->prio);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003025 mutex_unlock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003026
3027 return 0;
3028}
3029
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003030static int bttv_s_priority(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003031 enum v4l2_priority prio)
3032{
3033 struct bttv_fh *fh = f;
3034 struct bttv *btv = fh->btv;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003035 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003036
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003037 mutex_lock(&btv->lock);
3038 rc = v4l2_prio_change(&btv->prio, &fh->prio, prio);
3039 mutex_unlock(&btv->lock);
3040
3041 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003042}
3043
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003044static int bttv_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003045 struct v4l2_cropcap *cap)
3046{
3047 struct bttv_fh *fh = priv;
3048 struct bttv *btv = fh->btv;
3049
3050 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3051 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3052 return -EINVAL;
3053
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003054 mutex_lock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003055 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003056 mutex_unlock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003057
3058 return 0;
3059}
3060
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003061static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003062{
3063 struct bttv_fh *fh = f;
3064 struct bttv *btv = fh->btv;
3065
3066 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3067 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3068 return -EINVAL;
3069
3070 /* No fh->do_crop = 1; because btv->crop[1] may be
3071 inconsistent with fh->width or fh->height and apps
3072 do not expect a change here. */
3073
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003074 mutex_lock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003075 crop->c = btv->crop[!!fh->do_crop].rect;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003076 mutex_unlock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003077
3078 return 0;
3079}
3080
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003081static int bttv_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003082{
3083 struct bttv_fh *fh = f;
3084 struct bttv *btv = fh->btv;
3085 const struct v4l2_rect *b;
3086 int retval;
3087 struct bttv_crop c;
3088 __s32 b_left;
3089 __s32 b_top;
3090 __s32 b_right;
3091 __s32 b_bottom;
3092
3093 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3094 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3095 return -EINVAL;
3096
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003097 /* Make sure tvnorm, vbi_end and the current cropping
3098 parameters remain consistent until we're done. Note
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003099 read() may change vbi_end in check_alloc_btres_lock(). */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003100 mutex_lock(&btv->lock);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003101 retval = v4l2_prio_check(&btv->prio, fh->prio);
3102 if (0 != retval) {
3103 mutex_unlock(&btv->lock);
3104 return retval;
3105 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003106
3107 retval = -EBUSY;
3108
3109 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
3110 mutex_unlock(&btv->lock);
3111 return retval;
3112 }
3113
3114 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3115
3116 b_left = b->left;
3117 b_right = b_left + b->width;
3118 b_bottom = b->top + b->height;
3119
3120 b_top = max(b->top, btv->vbi_end);
3121 if (b_top + 32 >= b_bottom) {
3122 mutex_unlock(&btv->lock);
3123 return retval;
3124 }
3125
3126 /* Min. scaled size 48 x 32. */
3127 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3128 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3129
3130 c.rect.width = clamp(crop->c.width,
3131 48, b_right - c.rect.left);
3132
3133 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3134 /* Top and height must be a multiple of two. */
3135 c.rect.top = (c.rect.top + 1) & ~1;
3136
3137 c.rect.height = clamp(crop->c.height,
3138 32, b_bottom - c.rect.top);
3139 c.rect.height = (c.rect.height + 1) & ~1;
3140
3141 bttv_crop_calc_limits(&c);
3142
3143 btv->crop[1] = c;
3144
3145 mutex_unlock(&btv->lock);
3146
3147 fh->do_crop = 1;
3148
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003149 mutex_lock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003150
3151 if (fh->width < c.min_scaled_width) {
3152 fh->width = c.min_scaled_width;
3153 btv->init.width = c.min_scaled_width;
3154 } else if (fh->width > c.max_scaled_width) {
3155 fh->width = c.max_scaled_width;
3156 btv->init.width = c.max_scaled_width;
3157 }
3158
3159 if (fh->height < c.min_scaled_height) {
3160 fh->height = c.min_scaled_height;
3161 btv->init.height = c.min_scaled_height;
3162 } else if (fh->height > c.max_scaled_height) {
3163 fh->height = c.max_scaled_height;
3164 btv->init.height = c.max_scaled_height;
3165 }
3166
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003167 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003168
3169 return 0;
3170}
3171
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003172static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003173{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003174 if (unlikely(a->index))
3175 return -EINVAL;
3176
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003177 strcpy(a->name, "audio");
3178 return 0;
3179}
3180
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003181static int bttv_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003182{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003183 if (unlikely(a->index))
3184 return -EINVAL;
3185
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
3189static ssize_t bttv_read(struct file *file, char __user *data,
3190 size_t count, loff_t *ppos)
3191{
3192 struct bttv_fh *fh = file->private_data;
3193 int retval = 0;
3194
3195 if (fh->btv->errors)
3196 bttv_reinit_bt848(fh->btv);
3197 dprintk("bttv%d: read count=%d type=%s\n",
3198 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3199
3200 switch (fh->type) {
3201 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003202 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03003203 /* VIDEO_READ in use by another fh,
3204 or VIDEO_STREAM by any fh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return -EBUSY;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3208 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003209 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 break;
3211 case V4L2_BUF_TYPE_VBI_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003212 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return -EBUSY;
3214 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3215 file->f_flags & O_NONBLOCK);
3216 break;
3217 default:
3218 BUG();
3219 }
3220 return retval;
3221}
3222
3223static unsigned int bttv_poll(struct file *file, poll_table *wait)
3224{
3225 struct bttv_fh *fh = file->private_data;
3226 struct bttv_buffer *buf;
3227 enum v4l2_field field;
Figo.zhang9fd64182009-06-16 13:31:29 -03003228 unsigned int rc = POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
3230 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003231 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return POLLERR;
3233 return videobuf_poll_stream(file, &fh->vbi, wait);
3234 }
3235
Mauro Carvalho Chehabca960bf2010-09-18 08:35:57 -03003236 mutex_lock(&fh->cap.vb_lock);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003237 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 /* streaming capture */
3239 if (list_empty(&fh->cap.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -03003240 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3242 } else {
3243 /* read() capture */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (NULL == fh->cap.read_buf) {
3245 /* need to capture a new frame */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003246 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3247 goto err;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003248 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003249 if (NULL == fh->cap.read_buf)
3250 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3252 field = videobuf_next_field(&fh->cap);
3253 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -08003254 kfree (fh->cap.read_buf);
3255 fh->cap.read_buf = NULL;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003256 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
3258 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3259 fh->cap.read_off = 0;
3260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 buf = (struct bttv_buffer*)fh->cap.read_buf;
3262 }
3263
3264 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03003265 if (buf->vb.state == VIDEOBUF_DONE ||
3266 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -03003267 rc = POLLIN|POLLRDNORM;
3268 else
3269 rc = 0;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003270err:
3271 mutex_unlock(&fh->cap.vb_lock);
Figo.zhang9fd64182009-06-16 13:31:29 -03003272 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
Hans Verkuilbec43662008-12-30 06:58:20 -03003275static int bttv_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003277 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003278 struct bttv *btv = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 struct bttv_fh *fh;
3280 enum v4l2_buf_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003282 dprintk(KERN_DEBUG "bttv: open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Laurent Pinchart327ae592009-11-27 13:57:55 -03003284 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003285 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Laurent Pinchart327ae592009-11-27 13:57:55 -03003286 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003287 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3288 } else {
3289 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -03003291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
3293 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3294 btv->c.nr,v4l2_type_names[type]);
3295
3296 /* allocate per filehandle data */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003297 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3298 if (unlikely(!fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 return -ENOMEM;
3300 file->private_data = fh;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003301
3302 /*
3303 * btv is protected by btv->lock mutex, while btv->init and other
3304 * streaming vars are protected by fh->cap.vb_lock. We need to take
3305 * care of both locks to avoid troubles. However, vb_lock is used also
3306 * inside videobuf, without calling buf->lock. So, it is a very bad
3307 * idea to hold both locks at the same time.
3308 * Let's first copy btv->init at fh, holding cap.vb_lock, and then work
3309 * with the rest of init, holding btv->lock.
3310 */
3311 mutex_lock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 *fh = btv->init;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003313 mutex_unlock(&fh->cap.vb_lock);
3314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 fh->type = type;
3316 fh->ov.setup_ok = 0;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003317
3318 mutex_lock(&btv->lock);
Hans Verkuilffb48772010-05-01 08:03:24 -03003319 v4l2_prio_open(&btv->prio, &fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003321 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3322 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3324 V4L2_FIELD_INTERLACED,
3325 sizeof(struct bttv_buffer),
Hans Verkuil08bff032010-09-20 17:39:46 -03003326 fh, NULL);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003327 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3328 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 V4L2_BUF_TYPE_VBI_CAPTURE,
3330 V4L2_FIELD_SEQ_TB,
3331 sizeof(struct bttv_buffer),
Hans Verkuil08bff032010-09-20 17:39:46 -03003332 fh, NULL);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03003333 set_tvnorm(btv,btv->tvnorm);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003334 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336 btv->users++;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003337
3338 /* The V4L2 spec requires one global set of cropping parameters
3339 which only change on request. These are stored in btv->crop[1].
3340 However for compatibility with V4L apps and cropping unaware
3341 V4L2 apps we now reset the cropping parameters as seen through
3342 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3343 will use btv->crop[0], the default cropping parameters for the
3344 current video standard, and VIDIOC_S_FMT will not implicitely
3345 change the cropping parameters until VIDIOC_S_CROP has been
3346 called. */
3347 fh->do_crop = !reset_crop; /* module parameter */
3348
3349 /* Likewise there should be one global set of VBI capture
3350 parameters, but for compatibility with V4L apps and earlier
3351 driver versions each fh has its own parameters. */
3352 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 bttv_field_count(btv);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003355 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return 0;
3357}
3358
Hans Verkuilbec43662008-12-30 06:58:20 -03003359static int bttv_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360{
3361 struct bttv_fh *fh = file->private_data;
3362 struct bttv *btv = fh->btv;
3363
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003364 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 /* turn off overlay */
3366 if (check_btres(fh, RESOURCE_OVERLAY))
3367 bttv_switch_overlay(btv,fh,NULL);
3368
3369 /* stop video capture */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003370 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 videobuf_streamoff(&fh->cap);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003372 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 }
3374 if (fh->cap.read_buf) {
3375 buffer_release(&fh->cap,fh->cap.read_buf);
3376 kfree(fh->cap.read_buf);
3377 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03003378 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003379 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
3382 /* stop vbi capture */
3383 if (check_btres(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03003384 videobuf_stop(&fh->vbi);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003385 free_btres_lock(btv,fh,RESOURCE_VBI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 }
3387
3388 /* free stuff */
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003389
3390 /*
3391 * videobuf uses cap.vb_lock - we should avoid holding btv->lock,
3392 * otherwise we may have dead lock conditions
3393 */
3394 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 videobuf_mmap_free(&fh->cap);
3396 videobuf_mmap_free(&fh->vbi);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003397 mutex_lock(&btv->lock);
Hans Verkuilffb48772010-05-01 08:03:24 -03003398 v4l2_prio_close(&btv->prio, fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 file->private_data = NULL;
3400 kfree(fh);
3401
3402 btv->users--;
3403 bttv_field_count(btv);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003404
3405 if (!btv->users)
3406 audio_mute(btv, 1);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003407 mutex_unlock(&btv->lock);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 return 0;
3410}
3411
3412static int
3413bttv_mmap(struct file *file, struct vm_area_struct *vma)
3414{
3415 struct bttv_fh *fh = file->private_data;
3416
3417 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3418 fh->btv->c.nr, v4l2_type_names[fh->type],
3419 vma->vm_start, vma->vm_end - vma->vm_start);
3420 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3421}
3422
Hans Verkuilbec43662008-12-30 06:58:20 -03003423static const struct v4l2_file_operations bttv_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424{
Mauro Carvalho Chehab8979e9d2010-09-15 08:22:09 -03003425 .owner = THIS_MODULE,
3426 .open = bttv_open,
3427 .release = bttv_release,
3428 .unlocked_ioctl = video_ioctl2,
3429 .read = bttv_read,
3430 .mmap = bttv_mmap,
3431 .poll = bttv_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432};
3433
Hans Verkuila3998102008-07-21 02:57:38 -03003434static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003435 .vidioc_querycap = bttv_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003436 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3437 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3438 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3439 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3440 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3441 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3442 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3443 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003444 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3445 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3446 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003447 .vidioc_g_audio = bttv_g_audio,
3448 .vidioc_s_audio = bttv_s_audio,
3449 .vidioc_cropcap = bttv_cropcap,
3450 .vidioc_reqbufs = bttv_reqbufs,
3451 .vidioc_querybuf = bttv_querybuf,
3452 .vidioc_qbuf = bttv_qbuf,
3453 .vidioc_dqbuf = bttv_dqbuf,
3454 .vidioc_s_std = bttv_s_std,
3455 .vidioc_enum_input = bttv_enum_input,
3456 .vidioc_g_input = bttv_g_input,
3457 .vidioc_s_input = bttv_s_input,
3458 .vidioc_queryctrl = bttv_queryctrl,
3459 .vidioc_g_ctrl = bttv_g_ctrl,
3460 .vidioc_s_ctrl = bttv_s_ctrl,
3461 .vidioc_streamon = bttv_streamon,
3462 .vidioc_streamoff = bttv_streamoff,
3463 .vidioc_g_tuner = bttv_g_tuner,
3464 .vidioc_s_tuner = bttv_s_tuner,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003465#ifdef CONFIG_VIDEO_V4L1_COMPAT
3466 .vidiocgmbuf = vidiocgmbuf,
3467#endif
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003468 .vidioc_g_crop = bttv_g_crop,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003469 .vidioc_s_crop = bttv_s_crop,
3470 .vidioc_g_fbuf = bttv_g_fbuf,
3471 .vidioc_s_fbuf = bttv_s_fbuf,
3472 .vidioc_overlay = bttv_overlay,
3473 .vidioc_g_priority = bttv_g_priority,
3474 .vidioc_s_priority = bttv_s_priority,
3475 .vidioc_g_parm = bttv_g_parm,
3476 .vidioc_g_frequency = bttv_g_frequency,
3477 .vidioc_s_frequency = bttv_s_frequency,
3478 .vidioc_log_status = bttv_log_status,
3479 .vidioc_querystd = bttv_querystd,
Zoltan Devai43846832008-01-14 13:24:38 -03003480#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003481 .vidioc_g_register = bttv_g_register,
3482 .vidioc_s_register = bttv_s_register,
Zoltan Devai43846832008-01-14 13:24:38 -03003483#endif
Hans Verkuila3998102008-07-21 02:57:38 -03003484};
3485
3486static struct video_device bttv_video_template = {
3487 .fops = &bttv_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003488 .ioctl_ops = &bttv_ioctl_ops,
3489 .tvnorms = BTTV_NORMS,
3490 .current_norm = V4L2_STD_PAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491};
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493/* ----------------------------------------------------------------------- */
3494/* radio interface */
3495
Hans Verkuilbec43662008-12-30 06:58:20 -03003496static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003498 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003499 struct bttv *btv = video_drvdata(file);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003500 struct bttv_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003502 dprintk("bttv: open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003505
3506 /* allocate per filehandle data */
3507 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003508 if (unlikely(!fh))
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003509 return -ENOMEM;
3510 file->private_data = fh;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003511 mutex_lock(&fh->cap.vb_lock);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003512 *fh = btv->init;
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003513 mutex_unlock(&fh->cap.vb_lock);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003514
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02003515 mutex_lock(&btv->lock);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003516 v4l2_prio_open(&btv->prio, &fh->prio);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 btv->radio_user++;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003519
Hans Verkuil859f0272009-03-28 08:29:00 -03003520 bttv_call_all(btv, tuner, s_radio);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03003521 audio_input(btv,TVAUDIO_INPUT_RADIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02003523 mutex_unlock(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525}
3526
Hans Verkuilbec43662008-12-30 06:58:20 -03003527static int radio_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003529 struct bttv_fh *fh = file->private_data;
3530 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003531 struct rds_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003533 mutex_lock(&btv->lock);
Hans Verkuilffb48772010-05-01 08:03:24 -03003534 v4l2_prio_close(&btv->prio, fh->prio);
Robert Fitzsimonsb9bc07a2008-04-10 09:40:31 -03003535 file->private_data = NULL;
3536 kfree(fh);
3537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 btv->radio_user--;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003539
Hans Verkuil859f0272009-03-28 08:29:00 -03003540 bttv_call_all(btv, core, ioctl, RDS_CMD_CLOSE, &cmd);
Mauro Carvalho Chehabc37db912010-09-15 08:18:31 -03003541 mutex_unlock(&btv->lock);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 return 0;
3544}
3545
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003546static int radio_querycap(struct file *file, void *priv,
3547 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003549 struct bttv_fh *fh = priv;
3550 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003552 strcpy(cap->driver, "bttv");
3553 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
3554 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
3555 cap->version = BTTV_VERSION_CODE;
3556 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 return 0;
3559}
3560
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003561static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003563 struct bttv_fh *fh = priv;
3564 struct bttv *btv = fh->btv;
3565
Trent Piephoabb03622009-01-28 21:32:59 -03003566 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003567 return -EINVAL;
3568 if (0 != t->index)
3569 return -EINVAL;
3570 mutex_lock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003571 strcpy(t->name, "Radio");
3572 t->type = V4L2_TUNER_RADIO;
3573
Hans Verkuil859f0272009-03-28 08:29:00 -03003574 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003575
3576 if (btv->audio_mode_gpio)
3577 btv->audio_mode_gpio(btv, t, 0);
3578
3579 mutex_unlock(&btv->lock);
3580
3581 return 0;
3582}
3583
3584static int radio_enum_input(struct file *file, void *priv,
3585 struct v4l2_input *i)
3586{
3587 if (i->index != 0)
3588 return -EINVAL;
3589
3590 strcpy(i->name, "Radio");
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003591 i->type = V4L2_INPUT_TYPE_TUNER;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003592
3593 return 0;
3594}
3595
3596static int radio_g_audio(struct file *file, void *priv,
3597 struct v4l2_audio *a)
3598{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003599 if (unlikely(a->index))
Cyrill Gorcunov1a002eb2008-04-01 17:49:13 -03003600 return -EINVAL;
3601
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003602 strcpy(a->name, "Radio");
Cyrill Gorcunov1a002eb2008-04-01 17:49:13 -03003603
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003604 return 0;
3605}
3606
3607static int radio_s_tuner(struct file *file, void *priv,
3608 struct v4l2_tuner *t)
3609{
3610 struct bttv_fh *fh = priv;
3611 struct bttv *btv = fh->btv;
3612
3613 if (0 != t->index)
3614 return -EINVAL;
3615
Hans Verkuil859f0272009-03-28 08:29:00 -03003616 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003617 return 0;
3618}
3619
3620static int radio_s_audio(struct file *file, void *priv,
3621 struct v4l2_audio *a)
3622{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003623 if (unlikely(a->index))
3624 return -EINVAL;
3625
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003626 return 0;
3627}
3628
3629static int radio_s_input(struct file *filp, void *priv, unsigned int i)
3630{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003631 if (unlikely(i))
3632 return -EINVAL;
3633
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003634 return 0;
3635}
3636
3637static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
3638{
3639 return 0;
3640}
3641
3642static int radio_queryctrl(struct file *file, void *priv,
3643 struct v4l2_queryctrl *c)
3644{
3645 const struct v4l2_queryctrl *ctrl;
3646
3647 if (c->id < V4L2_CID_BASE ||
3648 c->id >= V4L2_CID_LASTP1)
3649 return -EINVAL;
3650
3651 if (c->id == V4L2_CID_AUDIO_MUTE) {
3652 ctrl = ctrl_by_id(c->id);
3653 *c = *ctrl;
3654 } else
3655 *c = no_ctl;
3656
3657 return 0;
3658}
3659
3660static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
3661{
3662 *i = 0;
3663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664}
3665
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003666static ssize_t radio_read(struct file *file, char __user *data,
3667 size_t count, loff_t *ppos)
3668{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003669 struct bttv_fh *fh = file->private_data;
3670 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003671 struct rds_command cmd;
3672 cmd.block_count = count/3;
3673 cmd.buffer = data;
3674 cmd.instance = file;
3675 cmd.result = -ENODEV;
3676
Hans Verkuil859f0272009-03-28 08:29:00 -03003677 bttv_call_all(btv, core, ioctl, RDS_CMD_READ, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003678
3679 return cmd.result;
3680}
3681
3682static unsigned int radio_poll(struct file *file, poll_table *wait)
3683{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003684 struct bttv_fh *fh = file->private_data;
3685 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003686 struct rds_command cmd;
3687 cmd.instance = file;
3688 cmd.event_list = wait;
3689 cmd.result = -ENODEV;
Hans Verkuil859f0272009-03-28 08:29:00 -03003690 bttv_call_all(btv, core, ioctl, RDS_CMD_POLL, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003691
3692 return cmd.result;
3693}
3694
Hans Verkuilbec43662008-12-30 06:58:20 -03003695static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
3697 .owner = THIS_MODULE,
3698 .open = radio_open,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003699 .read = radio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 .release = radio_release,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003701 .ioctl = video_ioctl2,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003702 .poll = radio_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703};
3704
Hans Verkuila3998102008-07-21 02:57:38 -03003705static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003706 .vidioc_querycap = radio_querycap,
3707 .vidioc_g_tuner = radio_g_tuner,
3708 .vidioc_enum_input = radio_enum_input,
3709 .vidioc_g_audio = radio_g_audio,
3710 .vidioc_s_tuner = radio_s_tuner,
3711 .vidioc_s_audio = radio_s_audio,
3712 .vidioc_s_input = radio_s_input,
3713 .vidioc_s_std = radio_s_std,
3714 .vidioc_queryctrl = radio_queryctrl,
3715 .vidioc_g_input = radio_g_input,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003716 .vidioc_g_ctrl = bttv_g_ctrl,
3717 .vidioc_s_ctrl = bttv_s_ctrl,
3718 .vidioc_g_frequency = bttv_g_frequency,
3719 .vidioc_s_frequency = bttv_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720};
3721
Hans Verkuila3998102008-07-21 02:57:38 -03003722static struct video_device radio_template = {
3723 .fops = &radio_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003724 .ioctl_ops = &radio_ioctl_ops,
3725};
3726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727/* ----------------------------------------------------------------------- */
3728/* some debug code */
3729
Adrian Bunk408b6642005-05-01 08:59:29 -07003730static int bttv_risc_decode(u32 risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731{
3732 static char *instr[16] = {
3733 [ BT848_RISC_WRITE >> 28 ] = "write",
3734 [ BT848_RISC_SKIP >> 28 ] = "skip",
3735 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3736 [ BT848_RISC_JUMP >> 28 ] = "jump",
3737 [ BT848_RISC_SYNC >> 28 ] = "sync",
3738 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3739 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3740 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3741 };
3742 static int incr[16] = {
3743 [ BT848_RISC_WRITE >> 28 ] = 2,
3744 [ BT848_RISC_JUMP >> 28 ] = 2,
3745 [ BT848_RISC_SYNC >> 28 ] = 2,
3746 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3747 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3748 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3749 };
3750 static char *bits[] = {
3751 "be0", "be1", "be2", "be3/resync",
3752 "set0", "set1", "set2", "set3",
3753 "clr0", "clr1", "clr2", "clr3",
3754 "irq", "res", "eol", "sol",
3755 };
3756 int i;
3757
3758 printk("0x%08x [ %s", risc,
3759 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3760 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3761 if (risc & (1 << (i + 12)))
3762 printk(" %s",bits[i]);
3763 printk(" count=%d ]\n", risc & 0xfff);
3764 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3765}
3766
Adrian Bunk408b6642005-05-01 08:59:29 -07003767static void bttv_risc_disasm(struct bttv *btv,
3768 struct btcx_riscmem *risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
3770 unsigned int i,j,n;
3771
3772 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003773 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 for (i = 0; i < (risc->size >> 2); i += n) {
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003775 printk("%s: 0x%lx: ", btv->c.v4l2_dev.name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 (unsigned long)(risc->dma + (i<<2)));
Al Viro3aa71102008-06-22 14:20:09 -03003777 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 for (j = 1; j < n; j++)
3779 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003780 btv->c.v4l2_dev.name, (unsigned long)(risc->dma + ((i+j)<<2)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 risc->cpu[i+j], j);
3782 if (0 == risc->cpu[i])
3783 break;
3784 }
3785}
3786
3787static void bttv_print_riscaddr(struct bttv *btv)
3788{
3789 printk(" main: %08Lx\n",
3790 (unsigned long long)btv->main.dma);
3791 printk(" vbi : o=%08Lx e=%08Lx\n",
3792 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3793 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3794 printk(" cap : o=%08Lx e=%08Lx\n",
3795 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3796 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3797 printk(" scr : o=%08Lx e=%08Lx\n",
3798 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3799 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3800 bttv_risc_disasm(btv, &btv->main);
3801}
3802
3803/* ----------------------------------------------------------------------- */
3804/* irq handler */
3805
3806static char *irq_name[] = {
3807 "FMTCHG", // format change detected (525 vs. 625)
3808 "VSYNC", // vertical sync (new field)
3809 "HSYNC", // horizontal sync
3810 "OFLOW", // chroma/luma AGC overflow
3811 "HLOCK", // horizontal lock changed
3812 "VPRES", // video presence changed
3813 "6", "7",
3814 "I2CDONE", // hw irc operation finished
3815 "GPINT", // gpio port triggered irq
3816 "10",
3817 "RISCI", // risc instruction triggered irq
3818 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3819 "FTRGT", // pixel data fifo overrun
3820 "FDSR", // fifo data stream resyncronisation
3821 "PPERR", // parity error (data transfer)
3822 "RIPERR", // parity error (read risc instructions)
3823 "PABORT", // pci abort
3824 "OCERR", // risc instruction error
3825 "SCERR", // syncronisation error
3826};
3827
3828static void bttv_print_irqbits(u32 print, u32 mark)
3829{
3830 unsigned int i;
3831
3832 printk("bits:");
3833 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3834 if (print & (1 << i))
3835 printk(" %s",irq_name[i]);
3836 if (mark & (1 << i))
3837 printk("*");
3838 }
3839}
3840
3841static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3842{
3843 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3844 btv->c.nr,
3845 (unsigned long)btv->main.dma,
Al Viro3aa71102008-06-22 14:20:09 -03003846 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3847 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 (unsigned long)rc);
3849
3850 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3851 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3852 "Ok, then this is harmless, don't worry ;)\n",
3853 btv->c.nr);
3854 return;
3855 }
3856 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3857 btv->c.nr);
3858 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3859 btv->c.nr);
3860 dump_stack();
3861}
3862
3863static int
3864bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3865{
3866 struct bttv_buffer *item;
3867
3868 memset(set,0,sizeof(*set));
3869
3870 /* capture request ? */
3871 if (!list_empty(&btv->capture)) {
3872 set->frame_irq = 1;
3873 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3874 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3875 set->top = item;
3876 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3877 set->bottom = item;
3878
3879 /* capture request for other field ? */
3880 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3881 (item->vb.queue.next != &btv->capture)) {
3882 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
Mike Isely66349b42009-09-21 12:09:08 -03003883 /* Mike Isely <isely@pobox.com> - Only check
3884 * and set up the bottom field in the logic
3885 * below. Don't ever do the top field. This
3886 * of course means that if we set up the
3887 * bottom field in the above code that we'll
3888 * actually skip a field. But that's OK.
3889 * Having processed only a single buffer this
3890 * time, then the next time around the first
3891 * available buffer should be for a top field.
3892 * That will then cause us here to set up a
3893 * top then a bottom field in the normal way.
3894 * The alternative to this understanding is
3895 * that we set up the second available buffer
3896 * as a top field, but that's out of order
3897 * since this driver always processes the top
3898 * field first - the effect will be the two
3899 * buffers being returned in the wrong order,
3900 * with the second buffer also being delayed
3901 * by one field time (owing to the fifo nature
3902 * of videobuf). Worse still, we'll be stuck
3903 * doing fields out of order now every time
3904 * until something else causes a field to be
3905 * dropped. By effectively forcing a field to
3906 * drop this way then we always get back into
3907 * sync within a single frame time. (Out of
3908 * order fields can screw up deinterlacing
3909 * algorithms.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 if (NULL == set->bottom &&
3912 V4L2_FIELD_BOTTOM == item->vb.field) {
3913 set->bottom = item;
3914 }
3915 if (NULL != set->top && NULL != set->bottom)
3916 set->top_irq = 2;
3917 }
3918 }
3919 }
3920
3921 /* screen overlay ? */
3922 if (NULL != btv->screen) {
3923 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3924 if (NULL == set->top && NULL == set->bottom) {
3925 set->top = btv->screen;
3926 set->bottom = btv->screen;
3927 }
3928 } else {
3929 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3930 NULL == set->top) {
3931 set->top = btv->screen;
3932 }
3933 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3934 NULL == set->bottom) {
3935 set->bottom = btv->screen;
3936 }
3937 }
3938 }
3939
3940 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3941 btv->c.nr,set->top, set->bottom,
3942 btv->screen,set->frame_irq,set->top_irq);
3943 return 0;
3944}
3945
3946static void
3947bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3948 struct bttv_buffer_set *curr, unsigned int state)
3949{
3950 struct timeval ts;
3951
3952 do_gettimeofday(&ts);
3953
3954 if (wakeup->top == wakeup->bottom) {
3955 if (NULL != wakeup->top && curr->top != wakeup->top) {
3956 if (irq_debug > 1)
3957 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3958 wakeup->top->vb.ts = ts;
3959 wakeup->top->vb.field_count = btv->field_count;
3960 wakeup->top->vb.state = state;
3961 wake_up(&wakeup->top->vb.done);
3962 }
3963 } else {
3964 if (NULL != wakeup->top && curr->top != wakeup->top) {
3965 if (irq_debug > 1)
3966 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3967 wakeup->top->vb.ts = ts;
3968 wakeup->top->vb.field_count = btv->field_count;
3969 wakeup->top->vb.state = state;
3970 wake_up(&wakeup->top->vb.done);
3971 }
3972 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3973 if (irq_debug > 1)
3974 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3975 wakeup->bottom->vb.ts = ts;
3976 wakeup->bottom->vb.field_count = btv->field_count;
3977 wakeup->bottom->vb.state = state;
3978 wake_up(&wakeup->bottom->vb.done);
3979 }
3980 }
3981}
3982
3983static void
3984bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3985 unsigned int state)
3986{
3987 struct timeval ts;
3988
3989 if (NULL == wakeup)
3990 return;
3991
3992 do_gettimeofday(&ts);
3993 wakeup->vb.ts = ts;
3994 wakeup->vb.field_count = btv->field_count;
3995 wakeup->vb.state = state;
3996 wake_up(&wakeup->vb.done);
3997}
3998
3999static void bttv_irq_timeout(unsigned long data)
4000{
4001 struct bttv *btv = (struct bttv *)data;
4002 struct bttv_buffer_set old,new;
4003 struct bttv_buffer *ovbi;
4004 struct bttv_buffer *item;
4005 unsigned long flags;
4006
4007 if (bttv_verbose) {
4008 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
4009 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
4010 btread(BT848_RISC_COUNT));
4011 bttv_print_irqbits(btread(BT848_INT_STAT),0);
4012 printk("\n");
4013 }
4014
4015 spin_lock_irqsave(&btv->s_lock,flags);
4016
4017 /* deactivate stuff */
4018 memset(&new,0,sizeof(new));
4019 old = btv->curr;
4020 ovbi = btv->cvbi;
4021 btv->curr = new;
4022 btv->cvbi = NULL;
4023 btv->loop_irq = 0;
4024 bttv_buffer_activate_video(btv, &new);
4025 bttv_buffer_activate_vbi(btv, NULL);
4026 bttv_set_dma(btv, 0);
4027
4028 /* wake up */
Brandon Philips0fc06862007-11-06 20:02:36 -03004029 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
4030 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
4032 /* cancel all outstanding capture / vbi requests */
4033 while (!list_empty(&btv->capture)) {
4034 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
4035 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03004036 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 wake_up(&item->vb.done);
4038 }
4039 while (!list_empty(&btv->vcapture)) {
4040 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4041 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03004042 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 wake_up(&item->vb.done);
4044 }
4045
4046 btv->errors++;
4047 spin_unlock_irqrestore(&btv->s_lock,flags);
4048}
4049
4050static void
4051bttv_irq_wakeup_top(struct bttv *btv)
4052{
4053 struct bttv_buffer *wakeup = btv->curr.top;
4054
4055 if (NULL == wakeup)
4056 return;
4057
4058 spin_lock(&btv->s_lock);
4059 btv->curr.top_irq = 0;
4060 btv->curr.top = NULL;
4061 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
4062
4063 do_gettimeofday(&wakeup->vb.ts);
4064 wakeup->vb.field_count = btv->field_count;
Brandon Philips0fc06862007-11-06 20:02:36 -03004065 wakeup->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 wake_up(&wakeup->vb.done);
4067 spin_unlock(&btv->s_lock);
4068}
4069
4070static inline int is_active(struct btcx_riscmem *risc, u32 rc)
4071{
4072 if (rc < risc->dma)
4073 return 0;
4074 if (rc > risc->dma + risc->size)
4075 return 0;
4076 return 1;
4077}
4078
4079static void
4080bttv_irq_switch_video(struct bttv *btv)
4081{
4082 struct bttv_buffer_set new;
4083 struct bttv_buffer_set old;
4084 dma_addr_t rc;
4085
4086 spin_lock(&btv->s_lock);
4087
4088 /* new buffer set */
4089 bttv_irq_next_video(btv, &new);
4090 rc = btread(BT848_RISC_COUNT);
4091 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
4092 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
4093 btv->framedrop++;
4094 if (debug_latency)
4095 bttv_irq_debug_low_latency(btv, rc);
4096 spin_unlock(&btv->s_lock);
4097 return;
4098 }
4099
4100 /* switch over */
4101 old = btv->curr;
4102 btv->curr = new;
4103 btv->loop_irq &= ~1;
4104 bttv_buffer_activate_video(btv, &new);
4105 bttv_set_dma(btv, 0);
4106
4107 /* switch input */
4108 if (UNSET != btv->new_input) {
4109 video_mux(btv,btv->new_input);
4110 btv->new_input = UNSET;
4111 }
4112
4113 /* wake up finished buffers */
Brandon Philips0fc06862007-11-06 20:02:36 -03004114 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 spin_unlock(&btv->s_lock);
4116}
4117
4118static void
4119bttv_irq_switch_vbi(struct bttv *btv)
4120{
4121 struct bttv_buffer *new = NULL;
4122 struct bttv_buffer *old;
4123 u32 rc;
4124
4125 spin_lock(&btv->s_lock);
4126
4127 if (!list_empty(&btv->vcapture))
4128 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4129 old = btv->cvbi;
4130
4131 rc = btread(BT848_RISC_COUNT);
4132 if (NULL != old && (is_active(&old->top, rc) ||
4133 is_active(&old->bottom, rc))) {
4134 btv->framedrop++;
4135 if (debug_latency)
4136 bttv_irq_debug_low_latency(btv, rc);
4137 spin_unlock(&btv->s_lock);
4138 return;
4139 }
4140
4141 /* switch */
4142 btv->cvbi = new;
4143 btv->loop_irq &= ~4;
4144 bttv_buffer_activate_vbi(btv, new);
4145 bttv_set_dma(btv, 0);
4146
Brandon Philips0fc06862007-11-06 20:02:36 -03004147 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 spin_unlock(&btv->s_lock);
4149}
4150
David Howells7d12e782006-10-05 14:55:46 +01004151static irqreturn_t bttv_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152{
4153 u32 stat,astat;
4154 u32 dstat;
4155 int count;
4156 struct bttv *btv;
4157 int handled = 0;
4158
4159 btv=(struct bttv *)dev_id;
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004160
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004161 if (btv->custom_irq)
4162 handled = btv->custom_irq(btv);
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004163
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 count=0;
4165 while (1) {
4166 /* get/clear interrupt status bits */
4167 stat=btread(BT848_INT_STAT);
4168 astat=stat&btread(BT848_INT_MASK);
4169 if (!astat)
4170 break;
4171 handled = 1;
4172 btwrite(stat,BT848_INT_STAT);
4173
4174 /* get device status bits */
4175 dstat=btread(BT848_DSTATUS);
4176
4177 if (irq_debug) {
4178 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4179 "riscs=%x, riscc=%08x, ",
4180 btv->c.nr, count, btv->field_count,
4181 stat>>28, btread(BT848_RISC_COUNT));
4182 bttv_print_irqbits(stat,astat);
4183 if (stat & BT848_INT_HLOCK)
4184 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4185 ? "yes" : "no");
4186 if (stat & BT848_INT_VPRES)
4187 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4188 ? "yes" : "no");
4189 if (stat & BT848_INT_FMTCHG)
4190 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4191 ? "625" : "525");
4192 printk("\n");
4193 }
4194
4195 if (astat&BT848_INT_VSYNC)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004196 btv->field_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004198 if ((astat & BT848_INT_GPINT) && btv->remote) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004200 bttv_input_irq(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 }
4202
4203 if (astat & BT848_INT_I2CDONE) {
4204 btv->i2c_done = stat;
4205 wake_up(&btv->i2c_queue);
4206 }
4207
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004208 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 bttv_irq_switch_vbi(btv);
4210
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004211 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 bttv_irq_wakeup_top(btv);
4213
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004214 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 bttv_irq_switch_video(btv);
4216
4217 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004218 audio_mute(btv, btv->mute); /* trigger automute */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
4220 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4221 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4222 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4223 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4224 btread(BT848_RISC_COUNT));
4225 bttv_print_irqbits(stat,astat);
4226 printk("\n");
4227 if (bttv_debug)
4228 bttv_print_riscaddr(btv);
4229 }
4230 if (fdsr && astat & BT848_INT_FDSR) {
4231 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4232 btv->c.nr,btread(BT848_RISC_COUNT));
4233 if (bttv_debug)
4234 bttv_print_riscaddr(btv);
4235 }
4236
4237 count++;
4238 if (count > 4) {
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004239
4240 if (count > 8 || !(astat & BT848_INT_GPINT)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004241 btwrite(0, BT848_INT_MASK);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004242
4243 printk(KERN_ERR
4244 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4245 } else {
4246 printk(KERN_ERR
4247 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4248
4249 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4250 BT848_INT_MASK);
4251 };
4252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 bttv_print_irqbits(stat,astat);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 printk("]\n");
4256 }
4257 }
4258 btv->irq_total++;
4259 if (handled)
4260 btv->irq_me++;
4261 return IRQ_RETVAL(handled);
4262}
4263
4264
4265/* ----------------------------------------------------------------------- */
4266/* initialitation */
4267
4268static struct video_device *vdev_init(struct bttv *btv,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004269 const struct video_device *template,
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004270 const char *type_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
4272 struct video_device *vfd;
4273
4274 vfd = video_device_alloc();
4275 if (NULL == vfd)
4276 return NULL;
4277 *vfd = *template;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004278 vfd->v4l2_dev = &btv->c.v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 vfd->release = video_device_release;
Mauro Carvalho Chehab1d0a4362008-06-23 12:31:29 -03004280 vfd->debug = bttv_debug;
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004281 video_set_drvdata(vfd, btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4283 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004284 type_name, bttv_tvcards[btv->c.type].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 return vfd;
4286}
4287
4288static void bttv_unregister_video(struct bttv *btv)
4289{
4290 if (btv->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004291 if (video_is_registered(btv->video_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 video_unregister_device(btv->video_dev);
4293 else
4294 video_device_release(btv->video_dev);
4295 btv->video_dev = NULL;
4296 }
4297 if (btv->vbi_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004298 if (video_is_registered(btv->vbi_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 video_unregister_device(btv->vbi_dev);
4300 else
4301 video_device_release(btv->vbi_dev);
4302 btv->vbi_dev = NULL;
4303 }
4304 if (btv->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004305 if (video_is_registered(btv->radio_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 video_unregister_device(btv->radio_dev);
4307 else
4308 video_device_release(btv->radio_dev);
4309 btv->radio_dev = NULL;
4310 }
4311}
4312
4313/* register video4linux devices */
4314static int __devinit bttv_register_video(struct bttv *btv)
4315{
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004316 if (no_overlay > 0)
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004317 printk("bttv: Overlay support disabled.\n");
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 /* video */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004320 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004321
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004322 if (NULL == btv->video_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004324 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
4325 video_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 goto err;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03004327 printk(KERN_INFO "bttv%d: registered device %s\n",
4328 btv->c.nr, video_device_node_name(btv->video_dev));
Hans Verkuil22a04f12008-07-20 06:35:02 -03004329 if (device_create_file(&btv->video_dev->dev,
Kay Sievers54bd5b62007-10-08 16:26:13 -03004330 &dev_attr_card)<0) {
4331 printk(KERN_ERR "bttv%d: device_create_file 'card' "
Trent Piephod94fc9a2006-07-29 17:18:06 -03004332 "failed\n", btv->c.nr);
4333 goto err;
4334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
4336 /* vbi */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004337 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004338
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004339 if (NULL == btv->vbi_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004341 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
4342 vbi_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 goto err;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03004344 printk(KERN_INFO "bttv%d: registered device %s\n",
4345 btv->c.nr, video_device_node_name(btv->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004347 if (!btv->has_radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 return 0;
4349 /* radio */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004350 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004351 if (NULL == btv->radio_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004353 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4354 radio_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 goto err;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03004356 printk(KERN_INFO "bttv%d: registered device %s\n",
4357 btv->c.nr, video_device_node_name(btv->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
4359 /* all done */
4360 return 0;
4361
4362 err:
4363 bttv_unregister_video(btv);
4364 return -1;
4365}
4366
4367
4368/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4369/* response on cards with no firmware is not enabled by OF */
4370static void pci_set_command(struct pci_dev *dev)
4371{
4372#if defined(__powerpc__)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004373 unsigned int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004375 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4376 cmd = (cmd | PCI_COMMAND_MEMORY );
4377 pci_write_config_dword(dev, PCI_COMMAND, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378#endif
4379}
4380
4381static int __devinit bttv_probe(struct pci_dev *dev,
4382 const struct pci_device_id *pci_id)
4383{
4384 int result;
4385 unsigned char lat;
4386 struct bttv *btv;
4387
4388 if (bttv_num == BTTV_MAX)
4389 return -ENOMEM;
4390 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004391 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004392 if (btv == NULL) {
4393 printk(KERN_ERR "bttv: out of memory.\n");
4394 return -ENOMEM;
4395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 btv->c.nr = bttv_num;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004397 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4398 "bttv%d", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
4400 /* initialize structs / fill in defaults */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02004401 mutex_init(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004402 spin_lock_init(&btv->s_lock);
4403 spin_lock_init(&btv->gpio_lock);
4404 init_waitqueue_head(&btv->gpioq);
4405 init_waitqueue_head(&btv->i2c_queue);
4406 INIT_LIST_HEAD(&btv->c.subs);
4407 INIT_LIST_HEAD(&btv->capture);
4408 INIT_LIST_HEAD(&btv->vcapture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 v4l2_prio_init(&btv->prio);
4410
4411 init_timer(&btv->timeout);
4412 btv->timeout.function = bttv_irq_timeout;
4413 btv->timeout.data = (unsigned long)btv;
4414
Michael Krufky7c08fb02005-11-08 21:36:21 -08004415 btv->i2c_rc = -1;
4416 btv->tuner_type = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 btv->new_input = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 btv->has_radio=radio[btv->c.nr];
4419
4420 /* pci stuff (init, get irq/mmio, ... */
4421 btv->c.pci = dev;
Michael Krufky7c08fb02005-11-08 21:36:21 -08004422 btv->id = dev->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 if (pci_enable_device(dev)) {
Michael Krufky7c08fb02005-11-08 21:36:21 -08004424 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 btv->c.nr);
4426 return -EIO;
4427 }
Yang Hongyang284901a2009-04-06 19:01:15 -07004428 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004429 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 btv->c.nr);
4431 return -EIO;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 if (!request_mem_region(pci_resource_start(dev,0),
4434 pci_resource_len(dev,0),
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004435 btv->c.v4l2_dev.name)) {
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004436 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4437 btv->c.nr,
4438 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 return -EBUSY;
4440 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004441 pci_set_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 pci_set_command(dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004443
4444 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4445 if (result < 0) {
4446 printk(KERN_WARNING "bttv%d: v4l2_device_register() failed\n", btv->c.nr);
4447 goto fail0;
4448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004450 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4451 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4452 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4453 bttv_num,btv->id, btv->revision, pci_name(dev));
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004454 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4455 btv->c.pci->irq, lat,
4456 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 schedule();
4458
Akinobu Mita5f1693f2006-12-20 10:08:56 -03004459 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4460 if (NULL == btv->bt848_mmio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4462 result = -EIO;
4463 goto fail1;
4464 }
4465
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004466 /* identify card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 bttv_idcard(btv);
4468
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004469 /* disable irqs, register irq handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 btwrite(0, BT848_INT_MASK);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004471 result = request_irq(btv->c.pci->irq, bttv_irq,
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004472 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004473 if (result < 0) {
4474 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 bttv_num,btv->c.pci->irq);
4476 goto fail1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
4479 if (0 != bttv_handle_chipset(btv)) {
4480 result = -EIO;
4481 goto fail2;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
4484 /* init options from insmod args */
4485 btv->opt_combfilter = combfilter;
4486 btv->opt_lumafilter = lumafilter;
4487 btv->opt_automute = automute;
4488 btv->opt_chroma_agc = chroma_agc;
4489 btv->opt_adc_crush = adc_crush;
4490 btv->opt_vcr_hack = vcr_hack;
4491 btv->opt_whitecrush_upper = whitecrush_upper;
4492 btv->opt_whitecrush_lower = whitecrush_lower;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07004493 btv->opt_uv_ratio = uv_ratio;
4494 btv->opt_full_luma_range = full_luma_range;
4495 btv->opt_coring = coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
4497 /* fill struct bttv with some useful defaults */
4498 btv->init.btv = btv;
4499 btv->init.ov.w.width = 320;
4500 btv->init.ov.w.height = 240;
Mauro Carvalho Chehabc96dd072007-10-26 16:51:47 -03004501 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 btv->init.width = 320;
4503 btv->init.height = 240;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 btv->input = 0;
4505
4506 /* initialize hardware */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004507 if (bttv_gpio)
4508 bttv_gpio_tracking(btv,"pre-init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
4510 bttv_risc_init_main(btv);
4511 init_bt848(btv);
4512
4513 /* gpio */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004514 btwrite(0x00, BT848_GPIO_REG_INP);
4515 btwrite(0x00, BT848_GPIO_OUT_EN);
4516 if (bttv_verbose)
4517 bttv_gpio_tracking(btv,"init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004519 /* needs to be done before i2c is registered */
4520 bttv_init_card1(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004522 /* register i2c + gpio */
4523 init_bttv_i2c(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004525 /* some card-specific stuff (needs working i2c) */
4526 bttv_init_card2(btv);
Hans Verkuil2c905772009-07-20 08:14:17 -03004527 bttv_init_tuner(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 init_irqreg(btv);
4529
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004530 /* register video4linux + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 if (!bttv_tvcards[btv->c.type].no_video) {
4532 bttv_register_video(btv);
4533 bt848_bright(btv,32768);
4534 bt848_contrast(btv,32768);
4535 bt848_hue(btv,32768);
4536 bt848_sat(btv,32768);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004537 audio_mute(btv, 1);
Trent Piepho333408f2007-07-03 15:08:10 -03004538 set_input(btv, 0, btv->tvnorm);
Michael Schimeke5bd0262007-01-18 16:17:39 -03004539 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4540 btv->crop[1] = btv->crop[0]; /* current = default */
4541 disclaim_vbi_lines(btv);
4542 disclaim_video_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 }
4544
Jarod Wilsonf992a492007-03-24 15:23:50 -03004545 /* add subdevices and autoload dvb-bt8xx if needed */
4546 if (bttv_tvcards[btv->c.type].has_dvb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 bttv_sub_add_device(&btv->c, "dvb");
Jarod Wilsonf992a492007-03-24 15:23:50 -03004548 request_modules(btv);
4549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
Jean Delvared90a4ae2010-02-16 14:22:37 -03004551 if (!disable_ir) {
4552 init_bttv_i2c_ir(btv);
4553 bttv_input_init(btv);
4554 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004555
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 /* everything is fine */
4557 bttv_num++;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004560fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004561 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004563fail1:
4564 v4l2_device_unregister(&btv->c.v4l2_dev);
4565
4566fail0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 if (btv->bt848_mmio)
4568 iounmap(btv->bt848_mmio);
4569 release_mem_region(pci_resource_start(btv->c.pci,0),
4570 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 return result;
4572}
4573
4574static void __devexit bttv_remove(struct pci_dev *pci_dev)
4575{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004576 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4577 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
4579 if (bttv_verbose)
4580 printk("bttv%d: unloading\n",btv->c.nr);
4581
Tejun Heo707bcf32010-12-24 16:14:20 +01004582 if (bttv_tvcards[btv->c.type].has_dvb)
4583 flush_request_modules(btv);
4584
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004585 /* shutdown everything (DMA+IRQs) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 btand(~15, BT848_GPIO_DMA_CTL);
4587 btwrite(0, BT848_INT_MASK);
4588 btwrite(~0x0, BT848_INT_STAT);
4589 btwrite(0x0, BT848_GPIO_OUT_EN);
4590 if (bttv_gpio)
4591 bttv_gpio_tracking(btv,"cleanup");
4592
4593 /* tell gpio modules we are leaving ... */
4594 btv->shutdown=1;
4595 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004596 bttv_input_fini(btv);
Christopher Pascoe889aee82006-01-09 15:25:28 -02004597 bttv_sub_del_devices(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004599 /* unregister i2c_bus + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 fini_bttv_i2c(btv);
4601
4602 /* unregister video4linux */
4603 bttv_unregister_video(btv);
4604
4605 /* free allocated memory */
4606 btcx_riscmem_free(btv->c.pci,&btv->main);
4607
4608 /* free ressources */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004609 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 iounmap(btv->bt848_mmio);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004611 release_mem_region(pci_resource_start(btv->c.pci,0),
4612 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004614 v4l2_device_unregister(&btv->c.v4l2_dev);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004615 bttvs[btv->c.nr] = NULL;
4616 kfree(btv);
4617
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004618 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619}
4620
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004621#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4623{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004624 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4625 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 struct bttv_buffer_set idle;
4627 unsigned long flags;
4628
Mauro Carvalho Chehab0f97a932005-09-09 13:04:05 -07004629 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
4631 /* stop dma + irqs */
4632 spin_lock_irqsave(&btv->s_lock,flags);
4633 memset(&idle, 0, sizeof(idle));
4634 btv->state.video = btv->curr;
4635 btv->state.vbi = btv->cvbi;
4636 btv->state.loop_irq = btv->loop_irq;
4637 btv->curr = idle;
4638 btv->loop_irq = 0;
4639 bttv_buffer_activate_video(btv, &idle);
4640 bttv_buffer_activate_vbi(btv, NULL);
4641 bttv_set_dma(btv, 0);
4642 btwrite(0, BT848_INT_MASK);
4643 spin_unlock_irqrestore(&btv->s_lock,flags);
4644
4645 /* save bt878 state */
4646 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4647 btv->state.gpio_data = gpio_read();
4648
4649 /* save pci state */
4650 pci_save_state(pci_dev);
4651 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4652 pci_disable_device(pci_dev);
4653 btv->state.disabled = 1;
4654 }
4655 return 0;
4656}
4657
4658static int bttv_resume(struct pci_dev *pci_dev)
4659{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004660 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4661 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004663 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
4665 dprintk("bttv%d: resume\n", btv->c.nr);
4666
4667 /* restore pci state */
4668 if (btv->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004669 err=pci_enable_device(pci_dev);
4670 if (err) {
4671 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4672 btv->c.nr);
4673 return err;
4674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 btv->state.disabled = 0;
4676 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004677 err=pci_set_power_state(pci_dev, PCI_D0);
4678 if (err) {
4679 pci_disable_device(pci_dev);
4680 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4681 btv->c.nr);
4682 btv->state.disabled = 1;
4683 return err;
4684 }
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 pci_restore_state(pci_dev);
4687
4688 /* restore bt878 state */
4689 bttv_reinit_bt848(btv);
4690 gpio_inout(0xffffff, btv->state.gpio_enable);
4691 gpio_write(btv->state.gpio_data);
4692
4693 /* restart dma */
4694 spin_lock_irqsave(&btv->s_lock,flags);
4695 btv->curr = btv->state.video;
4696 btv->cvbi = btv->state.vbi;
4697 btv->loop_irq = btv->state.loop_irq;
4698 bttv_buffer_activate_video(btv, &btv->curr);
4699 bttv_buffer_activate_vbi(btv, btv->cvbi);
4700 bttv_set_dma(btv, 0);
4701 spin_unlock_irqrestore(&btv->s_lock,flags);
4702 return 0;
4703}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004704#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
4706static struct pci_device_id bttv_pci_tbl[] = {
Joe Perches76e97412009-07-05 15:59:21 -03004707 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4708 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4709 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4710 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004711 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712};
4713
4714MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4715
4716static struct pci_driver bttv_pci_driver = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004717 .name = "bttv",
4718 .id_table = bttv_pci_tbl,
4719 .probe = bttv_probe,
4720 .remove = __devexit_p(bttv_remove),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004721#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 .suspend = bttv_suspend,
4723 .resume = bttv_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004724#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725};
4726
Adrian Bunk7d44e892007-12-11 19:23:43 -03004727static int __init bttv_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728{
Randy Dunlapc526e222006-07-15 09:08:26 -03004729 int ret;
4730
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 bttv_num = 0;
4732
4733 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4734 (BTTV_VERSION_CODE >> 16) & 0xff,
4735 (BTTV_VERSION_CODE >> 8) & 0xff,
4736 BTTV_VERSION_CODE & 0xff);
4737#ifdef SNAPSHOT
4738 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4739 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4740#endif
4741 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4742 gbuffers = 2;
Filipe Rossetf41b6962009-05-28 11:11:53 -03004743 if (gbufsize > BTTV_MAX_FBUF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 gbufsize = BTTV_MAX_FBUF;
4745 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4746 if (bttv_verbose)
4747 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4748 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4749
4750 bttv_check_chipset();
4751
Randy Dunlapc526e222006-07-15 09:08:26 -03004752 ret = bus_register(&bttv_sub_bus_type);
4753 if (ret < 0) {
4754 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4755 return ret;
4756 }
Akinobu Mita9e7e85e2007-12-17 14:26:29 -03004757 ret = pci_register_driver(&bttv_pci_driver);
4758 if (ret < 0)
4759 bus_unregister(&bttv_sub_bus_type);
4760
4761 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
Adrian Bunk7d44e892007-12-11 19:23:43 -03004764static void __exit bttv_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
4766 pci_unregister_driver(&bttv_pci_driver);
4767 bus_unregister(&bttv_sub_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768}
4769
4770module_init(bttv_init_module);
4771module_exit(bttv_cleanup_module);
4772
4773/*
4774 * Local variables:
4775 * c-basic-offset: 8
4776 * End:
4777 */