blob: d74dfa6d1a554d0cdad0b39004778fdd878c9d9b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
3 bttv - Bt848 frame grabber driver
4
5 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08006 & Marcus Metzler <mocm@thp.uni-koeln.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
8
9 some v4l2 code lines are taken from Justin's bttv2 driver which is
10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
11
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -030012 V4L1 removal from:
13 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
14
Mauro Carvalho Chehabc96dd072007-10-26 16:51:47 -030015 Fixes to be fully V4L2 compliant by
16 (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
17
Michael Schimeke5bd0262007-01-18 16:17:39 -030018 Cropping and overscan support
19 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
20 Sponsored by OPQ Systems AB
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35*/
36
37#include <linux/init.h>
38#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/errno.h>
42#include <linux/fs.h>
43#include <linux/kernel.h>
44#include <linux/sched.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040045#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/interrupt.h>
47#include <linux/kdev_t.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020048#include "bttvp.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020049#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030050#include <media/v4l2-ioctl.h>
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -030051#include <media/tvaudio.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030052#include <media/msp3400.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020053
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -070054#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <asm/io.h>
57#include <asm/byteorder.h>
58
Mauro Carvalho Chehabfa3fcce2006-03-23 21:45:24 -030059#include <media/rds.h>
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070060
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062unsigned int bttv_num; /* number of Bt848s in use */
Trent Piepho4b10d3b2009-01-28 21:32:59 -030063struct bttv *bttvs[BTTV_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020065unsigned int bttv_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066unsigned int bttv_verbose = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020067unsigned int bttv_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/* config variables */
70#ifdef __BIG_ENDIAN
71static unsigned int bigendian=1;
72#else
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020073static unsigned int bigendian;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif
75static unsigned int radio[BTTV_MAX];
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020076static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static unsigned int gbuffers = 8;
78static unsigned int gbufsize = 0x208000;
Michael Schimeke5bd0262007-01-18 16:17:39 -030079static unsigned int reset_crop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Jean Delvare176c2f32008-09-07 12:49:59 -030081static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
82static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
83static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020084static int debug_latency;
Jean Delvared90a4ae2010-02-16 14:22:37 -030085static int disable_ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020087static unsigned int fdsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/* options */
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020090static unsigned int combfilter;
91static unsigned int lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static unsigned int automute = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020093static unsigned int chroma_agc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static unsigned int adc_crush = 1;
95static unsigned int whitecrush_upper = 0xCF;
96static unsigned int whitecrush_lower = 0x7F;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020097static unsigned int vcr_hack;
98static unsigned int irq_iswitch;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -070099static unsigned int uv_ratio = 50;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200100static unsigned int full_luma_range;
101static unsigned int coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/* API features (turn on/off stuff for testing) */
104static unsigned int v4l2 = 1;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* insmod args */
107module_param(bttv_verbose, int, 0644);
108module_param(bttv_gpio, int, 0644);
109module_param(bttv_debug, int, 0644);
110module_param(irq_debug, int, 0644);
111module_param(debug_latency, int, 0644);
Jean Delvared90a4ae2010-02-16 14:22:37 -0300112module_param(disable_ir, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114module_param(fdsr, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115module_param(gbuffers, int, 0444);
116module_param(gbufsize, int, 0444);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300117module_param(reset_crop, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119module_param(v4l2, int, 0644);
120module_param(bigendian, int, 0644);
121module_param(irq_iswitch, int, 0644);
122module_param(combfilter, int, 0444);
123module_param(lumafilter, int, 0444);
124module_param(automute, int, 0444);
125module_param(chroma_agc, int, 0444);
126module_param(adc_crush, int, 0444);
127module_param(whitecrush_upper, int, 0444);
128module_param(whitecrush_lower, int, 0444);
129module_param(vcr_hack, int, 0444);
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700130module_param(uv_ratio, int, 0444);
131module_param(full_luma_range, int, 0444);
132module_param(coring, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Jean Delvare176c2f32008-09-07 12:49:59 -0300134module_param_array(radio, int, NULL, 0444);
135module_param_array(video_nr, int, NULL, 0444);
136module_param_array(radio_nr, int, NULL, 0444);
137module_param_array(vbi_nr, int, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
140MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
141MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
142MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
143MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
144MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
Jean Delvared90a4ae2010-02-16 14:22:37 -0300145MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
147MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
Michael Schimeke5bd0262007-01-18 16:17:39 -0300148MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
149 "is 1 (yes) for compatibility with older applications");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
151MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
152MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
153MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
154MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
155MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
156MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700157MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
158MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
159MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
Jean Delvare176c2f32008-09-07 12:49:59 -0300160MODULE_PARM_DESC(video_nr, "video device numbers");
161MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
162MODULE_PARM_DESC(radio_nr, "radio device numbers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
165MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
166MODULE_LICENSE("GPL");
167
168/* ----------------------------------------------------------------------- */
169/* sysfs */
170
Kay Sievers54bd5b62007-10-08 16:26:13 -0300171static ssize_t show_card(struct device *cd,
172 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300174 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -0300175 struct bttv *btv = video_get_drvdata(vfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
177}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300178static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180/* ----------------------------------------------------------------------- */
Jarod Wilsonf992a492007-03-24 15:23:50 -0300181/* dvb auto-load setup */
182#if defined(CONFIG_MODULES) && defined(MODULE)
183static void request_module_async(struct work_struct *work)
184{
185 request_module("dvb-bt8xx");
186}
187
188static void request_modules(struct bttv *dev)
189{
190 INIT_WORK(&dev->request_module_wk, request_module_async);
191 schedule_work(&dev->request_module_wk);
192}
193#else
194#define request_modules(dev)
195#endif /* CONFIG_MODULES */
196
197
198/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* static data */
200
201/* special timing tables from conexant... */
202static u8 SRAM_Table[][60] =
203{
204 /* PAL digital input over GPIO[7:0] */
205 {
206 45, // 45 bytes following
207 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
208 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
209 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
210 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
211 0x37,0x00,0xAF,0x21,0x00
212 },
213 /* NTSC digital input over GPIO[7:0] */
214 {
215 51, // 51 bytes following
216 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
217 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
218 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
219 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
220 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
221 0x00,
222 },
223 // TGB_NTSC392 // quartzsight
224 // This table has been modified to be used for Fusion Rev D
225 {
226 0x2A, // size of table = 42
227 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
228 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
229 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
230 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
231 0x20, 0x00
232 }
233};
234
Michael Schimeke5bd0262007-01-18 16:17:39 -0300235/* minhdelayx1 first video pixel we can capture on a line and
236 hdelayx1 start of active video, both relative to rising edge of
237 /HRESET pulse (0H) in 1 / fCLKx1.
238 swidth width of active video and
239 totalwidth total line width, both in 1 / fCLKx1.
240 sqwidth total line width in square pixels.
241 vdelay start of active video in 2 * field lines relative to
242 trailing edge of /VRESET pulse (VDELAY register).
243 sheight height of active video in 2 * field lines.
244 videostart0 ITU-R frame line number of the line corresponding
245 to vdelay in the first field. */
246#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
247 vdelay, sheight, videostart0) \
248 .cropcap.bounds.left = minhdelayx1, \
249 /* * 2 because vertically we count field lines times two, */ \
250 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
251 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
252 /* 4 is a safety margin at the end of the line. */ \
253 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
254 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
255 .cropcap.defrect.left = hdelayx1, \
256 .cropcap.defrect.top = (videostart0) * 2, \
257 .cropcap.defrect.width = swidth, \
258 .cropcap.defrect.height = sheight, \
259 .cropcap.pixelaspect.numerator = totalwidth, \
260 .cropcap.pixelaspect.denominator = sqwidth,
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262const struct bttv_tvnorm bttv_tvnorms[] = {
263 /* PAL-BDGHI */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800264 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
265 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 {
267 .v4l2_id = V4L2_STD_PAL,
268 .name = "PAL",
269 .Fsc = 35468950,
270 .swidth = 924,
271 .sheight = 576,
272 .totalwidth = 1135,
273 .adelay = 0x7f,
274 .bdelay = 0x72,
275 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
276 .scaledtwidth = 1135,
277 .hdelayx1 = 186,
278 .hactivex1 = 924,
279 .vdelay = 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300280 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 .sram = 0,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200282 /* ITU-R frame line number of the first VBI line
Michael Schimeke5bd0262007-01-18 16:17:39 -0300283 we can capture, of the first and second field.
284 The last line is determined by cropcap.bounds. */
285 .vbistart = { 7, 320 },
286 CROPCAP(/* minhdelayx1 */ 68,
287 /* hdelayx1 */ 186,
288 /* Should be (768 * 1135 + 944 / 2) / 944.
289 cropcap.defrect is used for image width
290 checks, so we keep the old value 924. */
291 /* swidth */ 924,
292 /* totalwidth */ 1135,
293 /* sqwidth */ 944,
294 /* vdelay */ 0x20,
295 /* sheight */ 576,
296 /* videostart0 */ 23)
297 /* bt878 (and bt848?) can capture another
298 line below active video. */
299 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 },{
Hans Verkuild97a11e2006-02-07 06:48:40 -0200301 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 .name = "NTSC",
303 .Fsc = 28636363,
304 .swidth = 768,
305 .sheight = 480,
306 .totalwidth = 910,
307 .adelay = 0x68,
308 .bdelay = 0x5d,
309 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
310 .scaledtwidth = 910,
311 .hdelayx1 = 128,
312 .hactivex1 = 910,
313 .vdelay = 0x1a,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300314 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 .sram = 1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200316 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300317 CROPCAP(/* minhdelayx1 */ 68,
318 /* hdelayx1 */ 128,
319 /* Should be (640 * 910 + 780 / 2) / 780? */
320 /* swidth */ 768,
321 /* totalwidth */ 910,
322 /* sqwidth */ 780,
323 /* vdelay */ 0x1a,
324 /* sheight */ 480,
325 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 },{
327 .v4l2_id = V4L2_STD_SECAM,
328 .name = "SECAM",
329 .Fsc = 35468950,
330 .swidth = 924,
331 .sheight = 576,
332 .totalwidth = 1135,
333 .adelay = 0x7f,
334 .bdelay = 0xb0,
335 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
336 .scaledtwidth = 1135,
337 .hdelayx1 = 186,
338 .hactivex1 = 922,
339 .vdelay = 0x20,
340 .vbipack = 255,
341 .sram = 0, /* like PAL, correct? */
Michael H. Schimek67f15702006-01-09 15:25:27 -0200342 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300343 CROPCAP(/* minhdelayx1 */ 68,
344 /* hdelayx1 */ 186,
345 /* swidth */ 924,
346 /* totalwidth */ 1135,
347 /* sqwidth */ 944,
348 /* vdelay */ 0x20,
349 /* sheight */ 576,
350 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 },{
352 .v4l2_id = V4L2_STD_PAL_Nc,
353 .name = "PAL-Nc",
354 .Fsc = 28636363,
355 .swidth = 640,
356 .sheight = 576,
357 .totalwidth = 910,
358 .adelay = 0x68,
359 .bdelay = 0x5d,
360 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
361 .scaledtwidth = 780,
362 .hdelayx1 = 130,
363 .hactivex1 = 734,
364 .vdelay = 0x1a,
365 .vbipack = 144,
366 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200367 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300368 CROPCAP(/* minhdelayx1 */ 68,
369 /* hdelayx1 */ 130,
370 /* swidth */ (640 * 910 + 780 / 2) / 780,
371 /* totalwidth */ 910,
372 /* sqwidth */ 780,
373 /* vdelay */ 0x1a,
374 /* sheight */ 576,
375 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 },{
377 .v4l2_id = V4L2_STD_PAL_M,
378 .name = "PAL-M",
379 .Fsc = 28636363,
380 .swidth = 640,
381 .sheight = 480,
382 .totalwidth = 910,
383 .adelay = 0x68,
384 .bdelay = 0x5d,
385 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
386 .scaledtwidth = 780,
387 .hdelayx1 = 135,
388 .hactivex1 = 754,
389 .vdelay = 0x1a,
390 .vbipack = 144,
391 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200392 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300393 CROPCAP(/* minhdelayx1 */ 68,
394 /* hdelayx1 */ 135,
395 /* swidth */ (640 * 910 + 780 / 2) / 780,
396 /* totalwidth */ 910,
397 /* sqwidth */ 780,
398 /* vdelay */ 0x1a,
399 /* sheight */ 480,
400 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 },{
402 .v4l2_id = V4L2_STD_PAL_N,
403 .name = "PAL-N",
404 .Fsc = 35468950,
405 .swidth = 768,
406 .sheight = 576,
407 .totalwidth = 1135,
408 .adelay = 0x7f,
409 .bdelay = 0x72,
410 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
411 .scaledtwidth = 944,
412 .hdelayx1 = 186,
413 .hactivex1 = 922,
414 .vdelay = 0x20,
415 .vbipack = 144,
416 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300417 .vbistart = { 7, 320 },
418 CROPCAP(/* minhdelayx1 */ 68,
419 /* hdelayx1 */ 186,
420 /* swidth */ (768 * 1135 + 944 / 2) / 944,
421 /* totalwidth */ 1135,
422 /* sqwidth */ 944,
423 /* vdelay */ 0x20,
424 /* sheight */ 576,
425 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 },{
427 .v4l2_id = V4L2_STD_NTSC_M_JP,
428 .name = "NTSC-JP",
429 .Fsc = 28636363,
430 .swidth = 640,
431 .sheight = 480,
432 .totalwidth = 910,
433 .adelay = 0x68,
434 .bdelay = 0x5d,
435 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
436 .scaledtwidth = 780,
437 .hdelayx1 = 135,
438 .hactivex1 = 754,
439 .vdelay = 0x16,
440 .vbipack = 144,
441 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300442 .vbistart = { 10, 273 },
443 CROPCAP(/* minhdelayx1 */ 68,
444 /* hdelayx1 */ 135,
445 /* swidth */ (640 * 910 + 780 / 2) / 780,
446 /* totalwidth */ 910,
447 /* sqwidth */ 780,
448 /* vdelay */ 0x16,
449 /* sheight */ 480,
450 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 },{
452 /* that one hopefully works with the strange timing
453 * which video recorders produce when playing a NTSC
454 * tape on a PAL TV ... */
455 .v4l2_id = V4L2_STD_PAL_60,
456 .name = "PAL-60",
457 .Fsc = 35468950,
458 .swidth = 924,
459 .sheight = 480,
460 .totalwidth = 1135,
461 .adelay = 0x7f,
462 .bdelay = 0x72,
463 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
464 .scaledtwidth = 1135,
465 .hdelayx1 = 186,
466 .hactivex1 = 924,
467 .vdelay = 0x1a,
468 .vbipack = 255,
469 .vtotal = 524,
470 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200471 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300472 CROPCAP(/* minhdelayx1 */ 68,
473 /* hdelayx1 */ 186,
474 /* swidth */ 924,
475 /* totalwidth */ 1135,
476 /* sqwidth */ 944,
477 /* vdelay */ 0x1a,
478 /* sheight */ 480,
479 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481};
482static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
483
484/* ----------------------------------------------------------------------- */
485/* bttv format list
486 packed pixel formats must come first */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300487static const struct bttv_format formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 {
489 .name = "8 bpp, gray",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 .fourcc = V4L2_PIX_FMT_GREY,
491 .btformat = BT848_COLOR_FMT_Y8,
492 .depth = 8,
493 .flags = FORMAT_FLAGS_PACKED,
494 },{
495 .name = "8 bpp, dithered color",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 .fourcc = V4L2_PIX_FMT_HI240,
497 .btformat = BT848_COLOR_FMT_RGB8,
498 .depth = 8,
499 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
500 },{
501 .name = "15 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 .fourcc = V4L2_PIX_FMT_RGB555,
503 .btformat = BT848_COLOR_FMT_RGB15,
504 .depth = 16,
505 .flags = FORMAT_FLAGS_PACKED,
506 },{
507 .name = "15 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 .fourcc = V4L2_PIX_FMT_RGB555X,
509 .btformat = BT848_COLOR_FMT_RGB15,
510 .btswap = 0x03, /* byteswap */
511 .depth = 16,
512 .flags = FORMAT_FLAGS_PACKED,
513 },{
514 .name = "16 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 .fourcc = V4L2_PIX_FMT_RGB565,
516 .btformat = BT848_COLOR_FMT_RGB16,
517 .depth = 16,
518 .flags = FORMAT_FLAGS_PACKED,
519 },{
520 .name = "16 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 .fourcc = V4L2_PIX_FMT_RGB565X,
522 .btformat = BT848_COLOR_FMT_RGB16,
523 .btswap = 0x03, /* byteswap */
524 .depth = 16,
525 .flags = FORMAT_FLAGS_PACKED,
526 },{
527 .name = "24 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 .fourcc = V4L2_PIX_FMT_BGR24,
529 .btformat = BT848_COLOR_FMT_RGB24,
530 .depth = 24,
531 .flags = FORMAT_FLAGS_PACKED,
532 },{
533 .name = "32 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 .fourcc = V4L2_PIX_FMT_BGR32,
535 .btformat = BT848_COLOR_FMT_RGB32,
536 .depth = 32,
537 .flags = FORMAT_FLAGS_PACKED,
538 },{
539 .name = "32 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 .fourcc = V4L2_PIX_FMT_RGB32,
541 .btformat = BT848_COLOR_FMT_RGB32,
542 .btswap = 0x0f, /* byte+word swap */
543 .depth = 32,
544 .flags = FORMAT_FLAGS_PACKED,
545 },{
546 .name = "4:2:2, packed, YUYV",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 .fourcc = V4L2_PIX_FMT_YUYV,
548 .btformat = BT848_COLOR_FMT_YUY2,
549 .depth = 16,
550 .flags = FORMAT_FLAGS_PACKED,
551 },{
552 .name = "4:2:2, packed, YUYV",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 .fourcc = V4L2_PIX_FMT_YUYV,
554 .btformat = BT848_COLOR_FMT_YUY2,
555 .depth = 16,
556 .flags = FORMAT_FLAGS_PACKED,
557 },{
558 .name = "4:2:2, packed, UYVY",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 .fourcc = V4L2_PIX_FMT_UYVY,
560 .btformat = BT848_COLOR_FMT_YUY2,
561 .btswap = 0x03, /* byteswap */
562 .depth = 16,
563 .flags = FORMAT_FLAGS_PACKED,
564 },{
565 .name = "4:2:2, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 .fourcc = V4L2_PIX_FMT_YUV422P,
567 .btformat = BT848_COLOR_FMT_YCrCb422,
568 .depth = 16,
569 .flags = FORMAT_FLAGS_PLANAR,
570 .hshift = 1,
571 .vshift = 0,
572 },{
573 .name = "4:2:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 .fourcc = V4L2_PIX_FMT_YUV420,
575 .btformat = BT848_COLOR_FMT_YCrCb422,
576 .depth = 12,
577 .flags = FORMAT_FLAGS_PLANAR,
578 .hshift = 1,
579 .vshift = 1,
580 },{
581 .name = "4:2:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 .fourcc = V4L2_PIX_FMT_YVU420,
583 .btformat = BT848_COLOR_FMT_YCrCb422,
584 .depth = 12,
585 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
586 .hshift = 1,
587 .vshift = 1,
588 },{
589 .name = "4:1:1, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 .fourcc = V4L2_PIX_FMT_YUV411P,
591 .btformat = BT848_COLOR_FMT_YCrCb411,
592 .depth = 12,
593 .flags = FORMAT_FLAGS_PLANAR,
594 .hshift = 2,
595 .vshift = 0,
596 },{
597 .name = "4:1:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 .fourcc = V4L2_PIX_FMT_YUV410,
599 .btformat = BT848_COLOR_FMT_YCrCb411,
600 .depth = 9,
601 .flags = FORMAT_FLAGS_PLANAR,
602 .hshift = 2,
603 .vshift = 2,
604 },{
605 .name = "4:1:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 .fourcc = V4L2_PIX_FMT_YVU410,
607 .btformat = BT848_COLOR_FMT_YCrCb411,
608 .depth = 9,
609 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
610 .hshift = 2,
611 .vshift = 2,
612 },{
613 .name = "raw scanlines",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 .fourcc = -1,
615 .btformat = BT848_COLOR_FMT_RAW,
616 .depth = 8,
617 .flags = FORMAT_FLAGS_RAW,
618 }
619};
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300620static const unsigned int FORMATS = ARRAY_SIZE(formats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622/* ----------------------------------------------------------------------- */
623
624#define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
625#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
626#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
627#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
628#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
629#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
630#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
631#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700632#define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
633#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
634#define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
635#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637static const struct v4l2_queryctrl no_ctl = {
638 .name = "42",
639 .flags = V4L2_CTRL_FLAG_DISABLED,
640};
641static const struct v4l2_queryctrl bttv_ctls[] = {
642 /* --- video --- */
643 {
644 .id = V4L2_CID_BRIGHTNESS,
645 .name = "Brightness",
646 .minimum = 0,
647 .maximum = 65535,
648 .step = 256,
649 .default_value = 32768,
650 .type = V4L2_CTRL_TYPE_INTEGER,
651 },{
652 .id = V4L2_CID_CONTRAST,
653 .name = "Contrast",
654 .minimum = 0,
655 .maximum = 65535,
656 .step = 128,
657 .default_value = 32768,
658 .type = V4L2_CTRL_TYPE_INTEGER,
659 },{
660 .id = V4L2_CID_SATURATION,
661 .name = "Saturation",
662 .minimum = 0,
663 .maximum = 65535,
664 .step = 128,
665 .default_value = 32768,
666 .type = V4L2_CTRL_TYPE_INTEGER,
667 },{
668 .id = V4L2_CID_HUE,
669 .name = "Hue",
670 .minimum = 0,
671 .maximum = 65535,
672 .step = 256,
673 .default_value = 32768,
674 .type = V4L2_CTRL_TYPE_INTEGER,
675 },
676 /* --- audio --- */
677 {
678 .id = V4L2_CID_AUDIO_MUTE,
679 .name = "Mute",
680 .minimum = 0,
681 .maximum = 1,
682 .type = V4L2_CTRL_TYPE_BOOLEAN,
683 },{
684 .id = V4L2_CID_AUDIO_VOLUME,
685 .name = "Volume",
686 .minimum = 0,
687 .maximum = 65535,
688 .step = 65535/100,
689 .default_value = 65535,
690 .type = V4L2_CTRL_TYPE_INTEGER,
691 },{
692 .id = V4L2_CID_AUDIO_BALANCE,
693 .name = "Balance",
694 .minimum = 0,
695 .maximum = 65535,
696 .step = 65535/100,
697 .default_value = 32768,
698 .type = V4L2_CTRL_TYPE_INTEGER,
699 },{
700 .id = V4L2_CID_AUDIO_BASS,
701 .name = "Bass",
702 .minimum = 0,
703 .maximum = 65535,
704 .step = 65535/100,
705 .default_value = 32768,
706 .type = V4L2_CTRL_TYPE_INTEGER,
707 },{
708 .id = V4L2_CID_AUDIO_TREBLE,
709 .name = "Treble",
710 .minimum = 0,
711 .maximum = 65535,
712 .step = 65535/100,
713 .default_value = 32768,
714 .type = V4L2_CTRL_TYPE_INTEGER,
715 },
716 /* --- private --- */
717 {
718 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
719 .name = "chroma agc",
720 .minimum = 0,
721 .maximum = 1,
722 .type = V4L2_CTRL_TYPE_BOOLEAN,
723 },{
724 .id = V4L2_CID_PRIVATE_COMBFILTER,
725 .name = "combfilter",
726 .minimum = 0,
727 .maximum = 1,
728 .type = V4L2_CTRL_TYPE_BOOLEAN,
729 },{
730 .id = V4L2_CID_PRIVATE_AUTOMUTE,
731 .name = "automute",
732 .minimum = 0,
733 .maximum = 1,
734 .type = V4L2_CTRL_TYPE_BOOLEAN,
735 },{
736 .id = V4L2_CID_PRIVATE_LUMAFILTER,
737 .name = "luma decimation filter",
738 .minimum = 0,
739 .maximum = 1,
740 .type = V4L2_CTRL_TYPE_BOOLEAN,
741 },{
742 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
743 .name = "agc crush",
744 .minimum = 0,
745 .maximum = 1,
746 .type = V4L2_CTRL_TYPE_BOOLEAN,
747 },{
748 .id = V4L2_CID_PRIVATE_VCR_HACK,
749 .name = "vcr hack",
750 .minimum = 0,
751 .maximum = 1,
752 .type = V4L2_CTRL_TYPE_BOOLEAN,
753 },{
754 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
755 .name = "whitecrush upper",
756 .minimum = 0,
757 .maximum = 255,
758 .step = 1,
759 .default_value = 0xCF,
760 .type = V4L2_CTRL_TYPE_INTEGER,
761 },{
762 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
763 .name = "whitecrush lower",
764 .minimum = 0,
765 .maximum = 255,
766 .step = 1,
767 .default_value = 0x7F,
768 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700769 },{
770 .id = V4L2_CID_PRIVATE_UV_RATIO,
771 .name = "uv ratio",
772 .minimum = 0,
773 .maximum = 100,
774 .step = 1,
775 .default_value = 50,
776 .type = V4L2_CTRL_TYPE_INTEGER,
777 },{
778 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
779 .name = "full luma range",
780 .minimum = 0,
781 .maximum = 1,
782 .type = V4L2_CTRL_TYPE_BOOLEAN,
783 },{
784 .id = V4L2_CID_PRIVATE_CORING,
785 .name = "coring",
786 .minimum = 0,
787 .maximum = 3,
788 .step = 1,
789 .default_value = 0,
790 .type = V4L2_CTRL_TYPE_INTEGER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700793
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300797static const struct v4l2_queryctrl *ctrl_by_id(int id)
798{
799 int i;
800
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300801 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300802 if (bttv_ctls[i].id == id)
803 return bttv_ctls+i;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300804
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300805 return NULL;
806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/* ----------------------------------------------------------------------- */
809/* resource management */
810
Michael Schimeke5bd0262007-01-18 16:17:39 -0300811/*
812 RESOURCE_ allocated by freed by
813
814 VIDEO_READ bttv_read 1) bttv_read 2)
815
816 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
817 VIDIOC_QBUF 1) bttv_release
818 VIDIOCMCAPTURE 1)
819
820 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
821 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
822 3) bttv_release
823
824 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
825 VIDIOC_QBUF 1) bttv_release
826 bttv_read, bttv_poll 1) 4)
827
828 1) The resource must be allocated when we enter buffer prepare functions
829 and remain allocated while buffers are in the DMA queue.
830 2) This is a single frame read.
831 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
832 RESOURCE_OVERLAY is allocated.
833 4) This is a continuous read, implies VIDIOC_STREAMON.
834
835 Note this driver permits video input and standard changes regardless if
836 resources are allocated.
837*/
838
839#define VBI_RESOURCES (RESOURCE_VBI)
840#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
841 RESOURCE_VIDEO_STREAM | \
842 RESOURCE_OVERLAY)
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300845int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Michael Schimeke5bd0262007-01-18 16:17:39 -0300847 int xbits; /* mutual exclusive resources */
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (fh->resources & bit)
850 /* have it already allocated */
851 return 1;
852
Michael Schimeke5bd0262007-01-18 16:17:39 -0300853 xbits = bit;
854 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
855 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /* is it free? */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300858 mutex_lock(&btv->lock);
859 if (btv->resources & xbits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /* no, someone else uses it */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300861 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300863
864 if ((bit & VIDEO_RESOURCES)
865 && 0 == (btv->resources & VIDEO_RESOURCES)) {
866 /* Do crop - use current, don't - use default parameters. */
867 __s32 top = btv->crop[!!fh->do_crop].rect.top;
868
869 if (btv->vbi_end > top)
870 goto fail;
871
872 /* We cannot capture the same line as video and VBI data.
873 Claim scan lines crop[].rect.top to bottom. */
874 btv->crop_start = top;
875 } else if (bit & VBI_RESOURCES) {
876 __s32 end = fh->vbi_fmt.end;
877
878 if (end > btv->crop_start)
879 goto fail;
880
881 /* Claim scan lines above fh->vbi_fmt.end. */
882 btv->vbi_end = end;
883 }
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /* it's free, grab it */
886 fh->resources |= bit;
887 btv->resources |= bit;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300888 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return 1;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300890
891 fail:
892 mutex_unlock(&btv->lock);
893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896static
897int check_btres(struct bttv_fh *fh, int bit)
898{
899 return (fh->resources & bit);
900}
901
902static
903int locked_btres(struct bttv *btv, int bit)
904{
905 return (btv->resources & bit);
906}
907
Michael Schimeke5bd0262007-01-18 16:17:39 -0300908/* Call with btv->lock down. */
909static void
910disclaim_vbi_lines(struct bttv *btv)
911{
912 btv->vbi_end = 0;
913}
914
915/* Call with btv->lock down. */
916static void
917disclaim_video_lines(struct bttv *btv)
918{
919 const struct bttv_tvnorm *tvnorm;
920 u8 crop;
921
922 tvnorm = &bttv_tvnorms[btv->tvnorm];
923 btv->crop_start = tvnorm->cropcap.bounds.top
924 + tvnorm->cropcap.bounds.height;
925
926 /* VBI capturing ends at VDELAY, start of video capturing, no
927 matter how many lines the VBI RISC program expects. When video
928 capturing is off, it shall no longer "preempt" VBI capturing,
929 so we set VDELAY to maximum. */
930 crop = btread(BT848_E_CROP) | 0xc0;
931 btwrite(crop, BT848_E_CROP);
932 btwrite(0xfe, BT848_E_VDELAY_LO);
933 btwrite(crop, BT848_O_CROP);
934 btwrite(0xfe, BT848_O_VDELAY_LO);
935}
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937static
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -0300938void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if ((fh->resources & bits) != bits) {
941 /* trying to free ressources not allocated by us ... */
942 printk("bttv: BUG! (btres)\n");
943 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300944 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 fh->resources &= ~bits;
946 btv->resources &= ~bits;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300947
948 bits = btv->resources;
949
950 if (0 == (bits & VIDEO_RESOURCES))
951 disclaim_video_lines(btv);
952
953 if (0 == (bits & VBI_RESOURCES))
954 disclaim_vbi_lines(btv);
955
956 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959/* ----------------------------------------------------------------------- */
960/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
961
962/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
963 PLL_X = Reference pre-divider (0=1, 1=2)
964 PLL_C = Post divider (0=6, 1=4)
965 PLL_I = Integer input
966 PLL_F = Fractional input
967
968 F_input = 28.636363 MHz:
969 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
970*/
971
972static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
973{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800974 unsigned char fl, fh, fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800976 /* prevent overflows */
977 fin/=4;
978 fout/=4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800980 fout*=12;
981 fi=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800983 fout=(fout%fin)*256;
984 fh=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800986 fout=(fout%fin)*256;
987 fl=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800989 btwrite(fl, BT848_PLL_F_LO);
990 btwrite(fh, BT848_PLL_F_HI);
991 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994static void set_pll(struct bttv *btv)
995{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800996 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800998 if (!btv->pll.pll_crystal)
999 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
1002 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001003 return;
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001006 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1007 /* no PLL needed */
1008 if (btv->pll.pll_current == 0)
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001009 return;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001010 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001011 btv->c.nr,btv->pll.pll_ifreq);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001012 btwrite(0x00,BT848_TGCTRL);
1013 btwrite(0x00,BT848_PLL_XCI);
1014 btv->pll.pll_current = 0;
1015 return;
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001018 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001019 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1021
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001022 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /* Let other people run while the PLL stabilizes */
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001024 bttv_printk(".");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 msleep(10);
1026
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001027 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 btwrite(0,BT848_DSTATUS);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001029 } else {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001030 btwrite(0x08,BT848_TGCTRL);
1031 btv->pll.pll_current = btv->pll.pll_ofreq;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001032 bttv_printk(" ok\n");
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001033 return;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001034 }
1035 }
1036 btv->pll.pll_current = -1;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001037 bttv_printk("failed\n");
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001038 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
1041/* used to switch between the bt848's analog/digital video capture modes */
1042static void bt848A_set_timing(struct bttv *btv)
1043{
1044 int i, len;
1045 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1046 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1047
Trent Piepho5221e212009-01-28 21:32:59 -03001048 if (btv->input == btv->dig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1050 btv->c.nr,table_idx);
1051
1052 /* timing change...reset timing generator address */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001053 btwrite(0x00, BT848_TGCTRL);
1054 btwrite(0x02, BT848_TGCTRL);
1055 btwrite(0x00, BT848_TGCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 len=SRAM_Table[table_idx][0];
1058 for(i = 1; i <= len; i++)
1059 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1060 btv->pll.pll_ofreq = 27000000;
1061
1062 set_pll(btv);
1063 btwrite(0x11, BT848_TGCTRL);
1064 btwrite(0x41, BT848_DVSIF);
1065 } else {
1066 btv->pll.pll_ofreq = fsc;
1067 set_pll(btv);
1068 btwrite(0x0, BT848_DVSIF);
1069 }
1070}
1071
1072/* ----------------------------------------------------------------------- */
1073
1074static void bt848_bright(struct bttv *btv, int bright)
1075{
1076 int value;
1077
1078 // printk("bttv: set bright: %d\n",bright); // DEBUG
1079 btv->bright = bright;
1080
1081 /* We want -128 to 127 we get 0-65535 */
1082 value = (bright >> 8) - 128;
1083 btwrite(value & 0xff, BT848_BRIGHT);
1084}
1085
1086static void bt848_hue(struct bttv *btv, int hue)
1087{
1088 int value;
1089
1090 btv->hue = hue;
1091
1092 /* -128 to 127 */
1093 value = (hue >> 8) - 128;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001094 btwrite(value & 0xff, BT848_HUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097static void bt848_contrast(struct bttv *btv, int cont)
1098{
1099 int value,hibit;
1100
1101 btv->contrast = cont;
1102
1103 /* 0-511 */
1104 value = (cont >> 7);
1105 hibit = (value >> 6) & 4;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001106 btwrite(value & 0xff, BT848_CONTRAST_LO);
1107 btaor(hibit, ~4, BT848_E_CONTROL);
1108 btaor(hibit, ~4, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
1111static void bt848_sat(struct bttv *btv, int color)
1112{
1113 int val_u,val_v,hibits;
1114
1115 btv->saturation = color;
1116
1117 /* 0-511 for the color */
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001118 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1119 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001120 hibits = (val_u >> 7) & 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 hibits |= (val_v >> 8) & 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001122 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1123 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1124 btaor(hibits, ~3, BT848_E_CONTROL);
1125 btaor(hibits, ~3, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128/* ----------------------------------------------------------------------- */
1129
1130static int
1131video_mux(struct bttv *btv, unsigned int input)
1132{
1133 int mux,mask2;
1134
1135 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1136 return -EINVAL;
1137
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001138 /* needed by RemoteVideo MX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1140 if (mask2)
1141 gpio_inout(mask2,mask2);
1142
1143 if (input == btv->svhs) {
1144 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1145 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1146 } else {
1147 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1148 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1149 }
Trent Piepho6f987002009-01-28 21:32:59 -03001150 mux = bttv_muxsel(btv, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1152 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1153 btv->c.nr,input,mux);
1154
1155 /* card specific hook */
1156 if(bttv_tvcards[btv->c.type].muxsel_hook)
1157 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1158 return 0;
1159}
1160
1161static char *audio_modes[] = {
1162 "audio: tuner", "audio: radio", "audio: extern",
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001163 "audio: intern", "audio: mute"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164};
1165
1166static int
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001167audio_mux(struct bttv *btv, int input, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001169 int gpio_val, signal;
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001170 struct v4l2_control ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1173 bttv_tvcards[btv->c.type].gpiomask);
1174 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1175
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001176 btv->mute = mute;
1177 btv->audio = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001179 /* automute */
1180 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1181
1182 if (mute)
1183 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1184 else
1185 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001186
Trent Piepho72134a62009-01-28 21:32:59 -03001187 switch (btv->c.type) {
1188 case BTTV_BOARD_VOODOOTV_FM:
1189 case BTTV_BOARD_VOODOOTV_200:
1190 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1191 break;
1192
1193 default:
1194 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1195 }
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (bttv_gpio)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001198 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1199 if (in_interrupt())
1200 return 0;
1201
1202 ctrl.id = V4L2_CID_AUDIO_MUTE;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001203 ctrl.value = btv->mute;
Hans Verkuil859f0272009-03-28 08:29:00 -03001204 bttv_call_all(btv, core, s_ctrl, &ctrl);
1205 if (btv->sd_msp34xx) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001206 u32 in;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001207
1208 /* Note: the inputs tuner/radio/extern/intern are translated
1209 to msp routings. This assumes common behavior for all msp3400
1210 based TV cards. When this assumption fails, then the
1211 specific MSP routing must be added to the card table.
1212 For now this is sufficient. */
1213 switch (input) {
1214 case TVAUDIO_INPUT_RADIO:
Hans Verkuil5325b422009-04-02 11:26:22 -03001215 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001216 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001217 break;
1218 case TVAUDIO_INPUT_EXTERN:
Hans Verkuil5325b422009-04-02 11:26:22 -03001219 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001220 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001221 break;
1222 case TVAUDIO_INPUT_INTERN:
1223 /* Yes, this is the same input as for RADIO. I doubt
1224 if this is ever used. The only board with an INTERN
1225 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1226 that was tested. My guess is that the whole INTERN
1227 input does not work. */
Hans Verkuil5325b422009-04-02 11:26:22 -03001228 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001229 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001230 break;
1231 case TVAUDIO_INPUT_TUNER:
1232 default:
Wade Berrier434b2522007-06-25 13:02:16 -03001233 /* This is the only card that uses TUNER2, and afaik,
1234 is the only difference between the VOODOOTV_FM
1235 and VOODOOTV_200 */
1236 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
Hans Verkuil5325b422009-04-02 11:26:22 -03001237 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
Wade Berrier434b2522007-06-25 13:02:16 -03001238 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1239 else
Hans Verkuil5325b422009-04-02 11:26:22 -03001240 in = MSP_INPUT_DEFAULT;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001241 break;
1242 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001243 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1244 in, MSP_OUTPUT_DEFAULT, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001245 }
Hans Verkuil859f0272009-03-28 08:29:00 -03001246 if (btv->sd_tvaudio) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001247 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1248 input, 0, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 0;
1251}
1252
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001253static inline int
1254audio_mute(struct bttv *btv, int mute)
1255{
1256 return audio_mux(btv, btv->audio, mute);
1257}
1258
1259static inline int
1260audio_input(struct bttv *btv, int input)
1261{
1262 return audio_mux(btv, input, btv->mute);
1263}
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265static void
Michael Schimeke5bd0262007-01-18 16:17:39 -03001266bttv_crop_calc_limits(struct bttv_crop *c)
1267{
1268 /* Scale factor min. 1:1, max. 16:1. Min. image size
1269 48 x 32. Scaled width must be a multiple of 4. */
1270
1271 if (1) {
1272 /* For bug compatibility with VIDIOCGCAP and image
1273 size checks in earlier driver versions. */
1274 c->min_scaled_width = 48;
1275 c->min_scaled_height = 32;
1276 } else {
1277 c->min_scaled_width =
1278 (max(48, c->rect.width >> 4) + 3) & ~3;
1279 c->min_scaled_height =
1280 max(32, c->rect.height >> 4);
1281 }
1282
1283 c->max_scaled_width = c->rect.width & ~3;
1284 c->max_scaled_height = c->rect.height;
1285}
1286
1287static void
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001288bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
Michael Schimeke5bd0262007-01-18 16:17:39 -03001289{
1290 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1291 bttv_crop_calc_limits(c);
1292}
1293
1294/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295static int
1296set_tvnorm(struct bttv *btv, unsigned int norm)
1297{
1298 const struct bttv_tvnorm *tvnorm;
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001299 v4l2_std_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001301 BUG_ON(norm >= BTTV_TVNORMS);
1302 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 tvnorm = &bttv_tvnorms[norm];
1305
Mike Isely2de26c02009-09-21 12:42:22 -03001306 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001307 sizeof (tvnorm->cropcap))) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001308 bttv_crop_reset(&btv->crop[0], norm);
1309 btv->crop[1] = btv->crop[0]; /* current = default */
1310
1311 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1312 btv->crop_start = tvnorm->cropcap.bounds.top
1313 + tvnorm->cropcap.bounds.height;
1314 }
1315 }
1316
1317 btv->tvnorm = norm;
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 btwrite(tvnorm->adelay, BT848_ADELAY);
1320 btwrite(tvnorm->bdelay, BT848_BDELAY);
1321 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1322 BT848_IFORM);
1323 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1324 btwrite(1, BT848_VBI_PACK_DEL);
1325 bt848A_set_timing(btv);
1326
1327 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -08001328 case BTTV_BOARD_VOODOOTV_FM:
Wade Berrier434b2522007-06-25 13:02:16 -03001329 case BTTV_BOARD_VOODOOTV_200:
Trent Piepho72134a62009-01-28 21:32:59 -03001330 bttv_tda9880_setnorm(btv, gpio_read());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001333 id = tvnorm->v4l2_id;
Hans Verkuilf41737e2009-04-01 03:52:39 -03001334 bttv_call_all(btv, core, s_std, id);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return 0;
1337}
1338
Michael Schimeke5bd0262007-01-18 16:17:39 -03001339/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340static void
Trent Piepho333408f2007-07-03 15:08:10 -03001341set_input(struct bttv *btv, unsigned int input, unsigned int norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 unsigned long flags;
1344
1345 btv->input = input;
1346 if (irq_iswitch) {
1347 spin_lock_irqsave(&btv->s_lock,flags);
1348 if (btv->curr.frame_irq) {
1349 /* active capture -> delayed input switch */
1350 btv->new_input = input;
1351 } else {
1352 video_mux(btv,input);
1353 }
1354 spin_unlock_irqrestore(&btv->s_lock,flags);
1355 } else {
1356 video_mux(btv,input);
1357 }
Trent Piephoabb03622009-01-28 21:32:59 -03001358 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1359 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
Trent Piepho333408f2007-07-03 15:08:10 -03001360 set_tvnorm(btv, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
1363static void init_irqreg(struct bttv *btv)
1364{
1365 /* clear status */
1366 btwrite(0xfffffUL, BT848_INT_STAT);
1367
1368 if (bttv_tvcards[btv->c.type].no_video) {
1369 /* i2c only */
1370 btwrite(BT848_INT_I2CDONE,
1371 BT848_INT_MASK);
1372 } else {
1373 /* full video */
1374 btwrite((btv->triton1) |
1375 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1376 BT848_INT_SCERR |
1377 (fdsr ? BT848_INT_FDSR : 0) |
Jean Delvareeb1b27b2008-08-30 10:18:21 -03001378 BT848_INT_RISCI | BT848_INT_OCERR |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1380 BT848_INT_I2CDONE,
1381 BT848_INT_MASK);
1382 }
1383}
1384
1385static void init_bt848(struct bttv *btv)
1386{
1387 int val;
1388
1389 if (bttv_tvcards[btv->c.type].no_video) {
1390 /* very basic init only */
1391 init_irqreg(btv);
1392 return;
1393 }
1394
1395 btwrite(0x00, BT848_CAP_CTL);
1396 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1397 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1398
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001399 /* set planar and packed mode trigger points and */
1400 /* set rising edge of inverted GPINTR pin as irq trigger */
1401 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1402 BT848_GPIO_DMA_CTL_PLTP1_16|
1403 BT848_GPIO_DMA_CTL_PLTP23_16|
1404 BT848_GPIO_DMA_CTL_GPINTC|
1405 BT848_GPIO_DMA_CTL_GPINTI,
1406 BT848_GPIO_DMA_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001409 btwrite(val, BT848_E_SCLOOP);
1410 btwrite(val, BT848_O_SCLOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001412 btwrite(0x20, BT848_E_VSCALE_HI);
1413 btwrite(0x20, BT848_O_VSCALE_HI);
1414 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 BT848_ADC);
1416
1417 btwrite(whitecrush_upper, BT848_WC_UP);
1418 btwrite(whitecrush_lower, BT848_WC_DOWN);
1419
1420 if (btv->opt_lumafilter) {
1421 btwrite(0, BT848_E_CONTROL);
1422 btwrite(0, BT848_O_CONTROL);
1423 } else {
1424 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1425 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1426 }
1427
1428 bt848_bright(btv, btv->bright);
1429 bt848_hue(btv, btv->hue);
1430 bt848_contrast(btv, btv->contrast);
1431 bt848_sat(btv, btv->saturation);
1432
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001433 /* interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 init_irqreg(btv);
1435}
1436
1437static void bttv_reinit_bt848(struct bttv *btv)
1438{
1439 unsigned long flags;
1440
1441 if (bttv_verbose)
1442 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1443 spin_lock_irqsave(&btv->s_lock,flags);
1444 btv->errors=0;
1445 bttv_set_dma(btv,0);
1446 spin_unlock_irqrestore(&btv->s_lock,flags);
1447
1448 init_bt848(btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001449 btv->pll.pll_current = -1;
Trent Piepho333408f2007-07-03 15:08:10 -03001450 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001453static int bttv_g_ctrl(struct file *file, void *priv,
1454 struct v4l2_control *c)
1455{
1456 struct bttv_fh *fh = priv;
1457 struct bttv *btv = fh->btv;
1458
1459 switch (c->id) {
1460 case V4L2_CID_BRIGHTNESS:
1461 c->value = btv->bright;
1462 break;
1463 case V4L2_CID_HUE:
1464 c->value = btv->hue;
1465 break;
1466 case V4L2_CID_CONTRAST:
1467 c->value = btv->contrast;
1468 break;
1469 case V4L2_CID_SATURATION:
1470 c->value = btv->saturation;
1471 break;
1472
1473 case V4L2_CID_AUDIO_MUTE:
1474 case V4L2_CID_AUDIO_VOLUME:
1475 case V4L2_CID_AUDIO_BALANCE:
1476 case V4L2_CID_AUDIO_BASS:
1477 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil859f0272009-03-28 08:29:00 -03001478 bttv_call_all(btv, core, g_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001479 break;
1480
1481 case V4L2_CID_PRIVATE_CHROMA_AGC:
1482 c->value = btv->opt_chroma_agc;
1483 break;
1484 case V4L2_CID_PRIVATE_COMBFILTER:
1485 c->value = btv->opt_combfilter;
1486 break;
1487 case V4L2_CID_PRIVATE_LUMAFILTER:
1488 c->value = btv->opt_lumafilter;
1489 break;
1490 case V4L2_CID_PRIVATE_AUTOMUTE:
1491 c->value = btv->opt_automute;
1492 break;
1493 case V4L2_CID_PRIVATE_AGC_CRUSH:
1494 c->value = btv->opt_adc_crush;
1495 break;
1496 case V4L2_CID_PRIVATE_VCR_HACK:
1497 c->value = btv->opt_vcr_hack;
1498 break;
1499 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1500 c->value = btv->opt_whitecrush_upper;
1501 break;
1502 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1503 c->value = btv->opt_whitecrush_lower;
1504 break;
1505 case V4L2_CID_PRIVATE_UV_RATIO:
1506 c->value = btv->opt_uv_ratio;
1507 break;
1508 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1509 c->value = btv->opt_full_luma_range;
1510 break;
1511 case V4L2_CID_PRIVATE_CORING:
1512 c->value = btv->opt_coring;
1513 break;
1514 default:
1515 return -EINVAL;
1516 }
1517 return 0;
1518}
1519
1520static int bttv_s_ctrl(struct file *file, void *f,
1521 struct v4l2_control *c)
1522{
1523 int err;
1524 int val;
1525 struct bttv_fh *fh = f;
1526 struct bttv *btv = fh->btv;
1527
Hans Verkuilffb48772010-05-01 08:03:24 -03001528 err = v4l2_prio_check(&btv->prio, fh->prio);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001529 if (0 != err)
1530 return err;
1531
1532 switch (c->id) {
1533 case V4L2_CID_BRIGHTNESS:
1534 bt848_bright(btv, c->value);
1535 break;
1536 case V4L2_CID_HUE:
1537 bt848_hue(btv, c->value);
1538 break;
1539 case V4L2_CID_CONTRAST:
1540 bt848_contrast(btv, c->value);
1541 break;
1542 case V4L2_CID_SATURATION:
1543 bt848_sat(btv, c->value);
1544 break;
1545 case V4L2_CID_AUDIO_MUTE:
1546 audio_mute(btv, c->value);
1547 /* fall through */
1548 case V4L2_CID_AUDIO_VOLUME:
1549 if (btv->volume_gpio)
1550 btv->volume_gpio(btv, c->value);
1551
Hans Verkuil859f0272009-03-28 08:29:00 -03001552 bttv_call_all(btv, core, s_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001553 break;
1554 case V4L2_CID_AUDIO_BALANCE:
1555 case V4L2_CID_AUDIO_BASS:
1556 case V4L2_CID_AUDIO_TREBLE:
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
1560 case V4L2_CID_PRIVATE_CHROMA_AGC:
1561 btv->opt_chroma_agc = c->value;
1562 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1563 btwrite(val, BT848_E_SCLOOP);
1564 btwrite(val, BT848_O_SCLOOP);
1565 break;
1566 case V4L2_CID_PRIVATE_COMBFILTER:
1567 btv->opt_combfilter = c->value;
1568 break;
1569 case V4L2_CID_PRIVATE_LUMAFILTER:
1570 btv->opt_lumafilter = c->value;
1571 if (btv->opt_lumafilter) {
1572 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1573 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1574 } else {
1575 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1576 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1577 }
1578 break;
1579 case V4L2_CID_PRIVATE_AUTOMUTE:
1580 btv->opt_automute = c->value;
1581 break;
1582 case V4L2_CID_PRIVATE_AGC_CRUSH:
1583 btv->opt_adc_crush = c->value;
1584 btwrite(BT848_ADC_RESERVED |
1585 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1586 BT848_ADC);
1587 break;
1588 case V4L2_CID_PRIVATE_VCR_HACK:
1589 btv->opt_vcr_hack = c->value;
1590 break;
1591 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1592 btv->opt_whitecrush_upper = c->value;
1593 btwrite(c->value, BT848_WC_UP);
1594 break;
1595 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1596 btv->opt_whitecrush_lower = c->value;
1597 btwrite(c->value, BT848_WC_DOWN);
1598 break;
1599 case V4L2_CID_PRIVATE_UV_RATIO:
1600 btv->opt_uv_ratio = c->value;
1601 bt848_sat(btv, btv->saturation);
1602 break;
1603 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1604 btv->opt_full_luma_range = c->value;
1605 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1606 break;
1607 case V4L2_CID_PRIVATE_CORING:
1608 btv->opt_coring = c->value;
1609 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1610 break;
1611 default:
1612 return -EINVAL;
1613 }
1614 return 0;
1615}
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617/* ----------------------------------------------------------------------- */
1618
1619void bttv_gpio_tracking(struct bttv *btv, char *comment)
1620{
1621 unsigned int outbits, data;
1622 outbits = btread(BT848_GPIO_OUT_EN);
1623 data = btread(BT848_GPIO_DATA);
1624 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1625 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1626}
1627
1628static void bttv_field_count(struct bttv *btv)
1629{
1630 int need_count = 0;
1631
1632 if (btv->users)
1633 need_count++;
1634
1635 if (need_count) {
1636 /* start field counter */
1637 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1638 } else {
1639 /* stop field counter */
1640 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1641 btv->field_count = 0;
1642 }
1643}
1644
1645static const struct bttv_format*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646format_by_fourcc(int fourcc)
1647{
1648 unsigned int i;
1649
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001650 for (i = 0; i < FORMATS; i++) {
1651 if (-1 == formats[i].fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 continue;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001653 if (formats[i].fourcc == fourcc)
1654 return formats+i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
1656 return NULL;
1657}
1658
1659/* ----------------------------------------------------------------------- */
1660/* misc helpers */
1661
1662static int
1663bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1664 struct bttv_buffer *new)
1665{
1666 struct bttv_buffer *old;
1667 unsigned long flags;
1668 int retval = 0;
1669
1670 dprintk("switch_overlay: enter [new=%p]\n",new);
1671 if (new)
Brandon Philips0fc06862007-11-06 20:02:36 -03001672 new->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 spin_lock_irqsave(&btv->s_lock,flags);
1674 old = btv->screen;
1675 btv->screen = new;
1676 btv->loop_irq |= 1;
1677 bttv_set_dma(btv, 0x03);
1678 spin_unlock_irqrestore(&btv->s_lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (NULL != old) {
1680 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001681 bttv_dma_free(&fh->cap,btv, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 kfree(old);
1683 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03001684 if (NULL == new)
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03001685 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 dprintk("switch_overlay: done\n");
1687 return retval;
1688}
1689
1690/* ----------------------------------------------------------------------- */
1691/* video4linux (1) interface */
1692
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001693static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1694 struct bttv_buffer *buf,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001695 const struct bttv_format *fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 unsigned int width, unsigned int height,
1697 enum v4l2_field field)
1698{
Michael Schimeke5bd0262007-01-18 16:17:39 -03001699 struct bttv_fh *fh = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 int redo_dma_risc = 0;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001701 struct bttv_crop c;
1702 int norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 int rc;
1704
1705 /* check settings */
1706 if (NULL == fmt)
1707 return -EINVAL;
1708 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1709 width = RAW_BPL;
1710 height = RAW_LINES*2;
1711 if (width*height > buf->vb.bsize)
1712 return -EINVAL;
1713 buf->vb.size = buf->vb.bsize;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001714
1715 /* Make sure tvnorm and vbi_end remain consistent
1716 until we're done. */
1717 mutex_lock(&btv->lock);
1718
1719 norm = btv->tvnorm;
1720
1721 /* In this mode capturing always starts at defrect.top
1722 (default VDELAY), ignoring cropping parameters. */
1723 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1724 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001726 }
1727
1728 mutex_unlock(&btv->lock);
1729
1730 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1731 } else {
1732 mutex_lock(&btv->lock);
1733
1734 norm = btv->tvnorm;
1735 c = btv->crop[!!fh->do_crop];
1736
1737 mutex_unlock(&btv->lock);
1738
1739 if (width < c.min_scaled_width ||
1740 width > c.max_scaled_width ||
1741 height < c.min_scaled_height)
1742 return -EINVAL;
1743
1744 switch (field) {
1745 case V4L2_FIELD_TOP:
1746 case V4L2_FIELD_BOTTOM:
1747 case V4L2_FIELD_ALTERNATE:
1748 /* btv->crop counts frame lines. Max. scale
1749 factor is 16:1 for frames, 8:1 for fields. */
1750 if (height * 2 > c.max_scaled_height)
1751 return -EINVAL;
1752 break;
1753
1754 default:
1755 if (height > c.max_scaled_height)
1756 return -EINVAL;
1757 break;
1758 }
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 buf->vb.size = (width * height * fmt->depth) >> 3;
1761 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1762 return -EINVAL;
1763 }
1764
1765 /* alloc + fill struct bttv_buffer (if changed) */
1766 if (buf->vb.width != width || buf->vb.height != height ||
1767 buf->vb.field != field ||
Michael Schimeke5bd0262007-01-18 16:17:39 -03001768 buf->tvnorm != norm || buf->fmt != fmt ||
1769 buf->crop.top != c.rect.top ||
1770 buf->crop.left != c.rect.left ||
1771 buf->crop.width != c.rect.width ||
1772 buf->crop.height != c.rect.height) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 buf->vb.width = width;
1774 buf->vb.height = height;
1775 buf->vb.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001776 buf->tvnorm = norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 buf->fmt = fmt;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001778 buf->crop = c.rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 redo_dma_risc = 1;
1780 }
1781
1782 /* alloc risc memory */
Brandon Philips0fc06862007-11-06 20:02:36 -03001783 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 redo_dma_risc = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001785 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 goto fail;
1787 }
1788
1789 if (redo_dma_risc)
1790 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1791 goto fail;
1792
Brandon Philips0fc06862007-11-06 20:02:36 -03001793 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return 0;
1795
1796 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001797 bttv_dma_free(q,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return rc;
1799}
1800
1801static int
1802buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1803{
1804 struct bttv_fh *fh = q->priv_data;
1805
1806 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1807 if (0 == *count)
1808 *count = gbuffers;
Andreas Bombedab7e312010-03-21 16:02:45 -03001809 if (*size * *count > gbuffers * gbufsize)
1810 *count = (gbuffers * gbufsize) / *size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return 0;
1812}
1813
1814static int
1815buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1816 enum v4l2_field field)
1817{
1818 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1819 struct bttv_fh *fh = q->priv_data;
1820
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001821 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 fh->width, fh->height, field);
1823}
1824
1825static void
1826buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1827{
1828 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1829 struct bttv_fh *fh = q->priv_data;
1830 struct bttv *btv = fh->btv;
1831
Brandon Philips0fc06862007-11-06 20:02:36 -03001832 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 list_add_tail(&buf->vb.queue,&btv->capture);
1834 if (!btv->curr.frame_irq) {
1835 btv->loop_irq |= 1;
1836 bttv_set_dma(btv, 0x03);
1837 }
1838}
1839
1840static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1841{
1842 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1843 struct bttv_fh *fh = q->priv_data;
1844
Michael Schimekfeaba7a2007-01-26 08:30:05 -03001845 bttv_dma_free(q,fh->btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
1848static struct videobuf_queue_ops bttv_video_qops = {
1849 .buf_setup = buffer_setup,
1850 .buf_prepare = buffer_prepare,
1851 .buf_queue = buffer_queue,
1852 .buf_release = buffer_release,
1853};
1854
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001855static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001857 struct bttv_fh *fh = priv;
1858 struct bttv *btv = fh->btv;
1859 unsigned int i;
1860 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Hans Verkuilffb48772010-05-01 08:03:24 -03001862 err = v4l2_prio_check(&btv->prio, fh->prio);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001863 if (0 != err)
1864 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001866 for (i = 0; i < BTTV_TVNORMS; i++)
1867 if (*id & bttv_tvnorms[i].v4l2_id)
1868 break;
1869 if (i == BTTV_TVNORMS)
1870 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001872 mutex_lock(&btv->lock);
1873 set_tvnorm(btv, i);
1874 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001876 return 0;
1877}
1878
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001879static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001880{
1881 struct bttv_fh *fh = f;
1882 struct bttv *btv = fh->btv;
1883
1884 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1885 *id = V4L2_STD_625_50;
1886 else
1887 *id = V4L2_STD_525_60;
1888 return 0;
1889}
1890
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001891static int bttv_enum_input(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001892 struct v4l2_input *i)
1893{
1894 struct bttv_fh *fh = priv;
1895 struct bttv *btv = fh->btv;
Trent Piepho522a5f12009-03-04 01:21:03 -03001896 int n;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001897
Trent Piepho522a5f12009-03-04 01:21:03 -03001898 if (i->index >= bttv_tvcards[btv->c.type].video_inputs)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001899 return -EINVAL;
1900
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001901 i->type = V4L2_INPUT_TYPE_CAMERA;
1902 i->audioset = 1;
1903
Trent Piephoabb03622009-01-28 21:32:59 -03001904 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001905 sprintf(i->name, "Television");
1906 i->type = V4L2_INPUT_TYPE_TUNER;
1907 i->tuner = 0;
1908 } else if (i->index == btv->svhs) {
1909 sprintf(i->name, "S-Video");
1910 } else {
1911 sprintf(i->name, "Composite%d", i->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001914 if (i->index == btv->input) {
1915 __u32 dstatus = btread(BT848_DSTATUS);
1916 if (0 == (dstatus & BT848_DSTATUS_PRES))
1917 i->status |= V4L2_IN_ST_NO_SIGNAL;
1918 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1919 i->status |= V4L2_IN_ST_NO_H_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001922 for (n = 0; n < BTTV_TVNORMS; n++)
1923 i->std |= bttv_tvnorms[n].v4l2_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001925 return 0;
1926}
Nickolay V. Shmyrev4b9b9362006-08-25 16:53:04 -03001927
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001928static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001929{
1930 struct bttv_fh *fh = priv;
1931 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001933 *i = btv->input;
1934 return 0;
1935}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001937static int bttv_s_input(struct file *file, void *priv, unsigned int i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001938{
1939 struct bttv_fh *fh = priv;
1940 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001942 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Hans Verkuilffb48772010-05-01 08:03:24 -03001944 err = v4l2_prio_check(&btv->prio, fh->prio);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001945 if (0 != err)
1946 return err;
1947
1948 if (i > bttv_tvcards[btv->c.type].video_inputs)
1949 return -EINVAL;
1950
1951 mutex_lock(&btv->lock);
1952 set_input(btv, i, btv->tvnorm);
1953 mutex_unlock(&btv->lock);
1954 return 0;
1955}
1956
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001957static int bttv_s_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001958 struct v4l2_tuner *t)
1959{
1960 struct bttv_fh *fh = priv;
1961 struct bttv *btv = fh->btv;
1962 int err;
1963
Hans Verkuilffb48772010-05-01 08:03:24 -03001964 err = v4l2_prio_check(&btv->prio, fh->prio);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001965 if (0 != err)
1966 return err;
1967
Trent Piephoabb03622009-01-28 21:32:59 -03001968 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001969 return -EINVAL;
1970
1971 if (0 != t->index)
1972 return -EINVAL;
1973
1974 mutex_lock(&btv->lock);
Hans Verkuil859f0272009-03-28 08:29:00 -03001975 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001976
1977 if (btv->audio_mode_gpio)
1978 btv->audio_mode_gpio(btv, t, 1);
1979
1980 mutex_unlock(&btv->lock);
1981
1982 return 0;
1983}
1984
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001985static int bttv_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001986 struct v4l2_frequency *f)
1987{
1988 struct bttv_fh *fh = priv;
1989 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001990
Robert Fitzsimons1b069012008-04-01 11:41:54 -03001991 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001992 f->frequency = btv->freq;
1993
1994 return 0;
1995}
1996
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001997static int bttv_s_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001998 struct v4l2_frequency *f)
1999{
2000 struct bttv_fh *fh = priv;
2001 struct bttv *btv = fh->btv;
2002 int err;
2003
Hans Verkuilffb48772010-05-01 08:03:24 -03002004 err = v4l2_prio_check(&btv->prio, fh->prio);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002005 if (0 != err)
2006 return err;
2007
2008 if (unlikely(f->tuner != 0))
2009 return -EINVAL;
Robert Fitzsimons1b069012008-04-01 11:41:54 -03002010 if (unlikely(f->type != (btv->radio_user
2011 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV)))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002012 return -EINVAL;
2013 mutex_lock(&btv->lock);
2014 btv->freq = f->frequency;
Hans Verkuil859f0272009-03-28 08:29:00 -03002015 bttv_call_all(btv, tuner, s_frequency, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002016 if (btv->has_matchbox && btv->radio_user)
2017 tea5757_set_freq(btv, btv->freq);
2018 mutex_unlock(&btv->lock);
2019 return 0;
2020}
2021
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002022static int bttv_log_status(struct file *file, void *f)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002023{
2024 struct bttv_fh *fh = f;
2025 struct bttv *btv = fh->btv;
2026
2027 printk(KERN_INFO "bttv%d: ======== START STATUS CARD #%d ========\n",
2028 btv->c.nr, btv->c.nr);
Hans Verkuil859f0272009-03-28 08:29:00 -03002029 bttv_call_all(btv, core, log_status);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002030 printk(KERN_INFO "bttv%d: ======== END STATUS CARD #%d ========\n",
2031 btv->c.nr, btv->c.nr);
2032 return 0;
2033}
2034
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002035#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002036static int bttv_g_register(struct file *file, void *f,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002037 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002038{
2039 struct bttv_fh *fh = f;
2040 struct bttv *btv = fh->btv;
2041
2042 if (!capable(CAP_SYS_ADMIN))
2043 return -EPERM;
2044
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002045 if (!v4l2_chip_match_host(&reg->match))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002046 return -EINVAL;
2047
2048 /* bt848 has a 12-bit register space */
2049 reg->reg &= 0xfff;
2050 reg->val = btread(reg->reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002051 reg->size = 1;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002052
2053 return 0;
2054}
2055
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002056static int bttv_s_register(struct file *file, void *f,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002057 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002058{
2059 struct bttv_fh *fh = f;
2060 struct bttv *btv = fh->btv;
2061
2062 if (!capable(CAP_SYS_ADMIN))
2063 return -EPERM;
2064
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002065 if (!v4l2_chip_match_host(&reg->match))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002066 return -EINVAL;
2067
2068 /* bt848 has a 12-bit register space */
2069 reg->reg &= 0xfff;
2070 btwrite(reg->val, reg->reg);
2071
2072 return 0;
2073}
2074#endif
2075
Michael Schimeke5bd0262007-01-18 16:17:39 -03002076/* Given cropping boundaries b and the scaled width and height of a
2077 single field or frame, which must not exceed hardware limits, this
2078 function adjusts the cropping parameters c. */
2079static void
2080bttv_crop_adjust (struct bttv_crop * c,
2081 const struct v4l2_rect * b,
2082 __s32 width,
2083 __s32 height,
2084 enum v4l2_field field)
2085{
2086 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2087 __s32 max_left;
2088 __s32 max_top;
2089
2090 if (width < c->min_scaled_width) {
2091 /* Max. hor. scale factor 16:1. */
2092 c->rect.width = width * 16;
2093 } else if (width > c->max_scaled_width) {
2094 /* Min. hor. scale factor 1:1. */
2095 c->rect.width = width;
2096
2097 max_left = b->left + b->width - width;
2098 max_left = min(max_left, (__s32) MAX_HDELAY);
2099 if (c->rect.left > max_left)
2100 c->rect.left = max_left;
2101 }
2102
2103 if (height < c->min_scaled_height) {
2104 /* Max. vert. scale factor 16:1, single fields 8:1. */
2105 c->rect.height = height * 16;
2106 } else if (frame_height > c->max_scaled_height) {
2107 /* Min. vert. scale factor 1:1.
2108 Top and height count field lines times two. */
2109 c->rect.height = (frame_height + 1) & ~1;
2110
2111 max_top = b->top + b->height - c->rect.height;
2112 if (c->rect.top > max_top)
2113 c->rect.top = max_top;
2114 }
2115
2116 bttv_crop_calc_limits(c);
2117}
2118
2119/* Returns an error if scaling to a frame or single field with the given
2120 width and height is not possible with the current cropping parameters
2121 and width aligned according to width_mask. If adjust_size is TRUE the
2122 function may adjust the width and/or height instead, rounding width
2123 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2124 also adjust the current cropping parameters to get closer to the
2125 desired image size. */
2126static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002127limit_scaled_size_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002128 __s32 * width,
2129 __s32 * height,
2130 enum v4l2_field field,
2131 unsigned int width_mask,
2132 unsigned int width_bias,
2133 int adjust_size,
2134 int adjust_crop)
2135{
2136 struct bttv *btv = fh->btv;
2137 const struct v4l2_rect *b;
2138 struct bttv_crop *c;
2139 __s32 min_width;
2140 __s32 min_height;
2141 __s32 max_width;
2142 __s32 max_height;
2143 int rc;
2144
2145 BUG_ON((int) width_mask >= 0 ||
2146 width_bias >= (unsigned int) -width_mask);
2147
2148 /* Make sure tvnorm, vbi_end and the current cropping parameters
2149 remain consistent until we're done. */
2150 mutex_lock(&btv->lock);
2151
2152 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2153
2154 /* Do crop - use current, don't - use default parameters. */
2155 c = &btv->crop[!!fh->do_crop];
2156
2157 if (fh->do_crop
2158 && adjust_size
2159 && adjust_crop
2160 && !locked_btres(btv, VIDEO_RESOURCES)) {
2161 min_width = 48;
2162 min_height = 32;
2163
2164 /* We cannot scale up. When the scaled image is larger
2165 than crop.rect we adjust the crop.rect as required
2166 by the V4L2 spec, hence cropcap.bounds are our limit. */
2167 max_width = min(b->width, (__s32) MAX_HACTIVE);
2168 max_height = b->height;
2169
2170 /* We cannot capture the same line as video and VBI data.
2171 Note btv->vbi_end is really a minimum, see
2172 bttv_vbi_try_fmt(). */
2173 if (btv->vbi_end > b->top) {
2174 max_height -= btv->vbi_end - b->top;
2175 rc = -EBUSY;
2176 if (min_height > max_height)
2177 goto fail;
2178 }
2179 } else {
2180 rc = -EBUSY;
2181 if (btv->vbi_end > c->rect.top)
2182 goto fail;
2183
2184 min_width = c->min_scaled_width;
2185 min_height = c->min_scaled_height;
2186 max_width = c->max_scaled_width;
2187 max_height = c->max_scaled_height;
2188
2189 adjust_crop = 0;
2190 }
2191
2192 min_width = (min_width - width_mask - 1) & width_mask;
2193 max_width = max_width & width_mask;
2194
2195 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2196 min_height = min_height;
2197 /* Min. scale factor is 1:1. */
2198 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2199
2200 if (adjust_size) {
2201 *width = clamp(*width, min_width, max_width);
2202 *height = clamp(*height, min_height, max_height);
2203
2204 /* Round after clamping to avoid overflow. */
2205 *width = (*width + width_bias) & width_mask;
2206
2207 if (adjust_crop) {
2208 bttv_crop_adjust(c, b, *width, *height, field);
2209
2210 if (btv->vbi_end > c->rect.top) {
2211 /* Move the crop window out of the way. */
2212 c->rect.top = btv->vbi_end;
2213 }
2214 }
2215 } else {
2216 rc = -EINVAL;
2217 if (*width < min_width ||
2218 *height < min_height ||
2219 *width > max_width ||
2220 *height > max_height ||
2221 0 != (*width & ~width_mask))
2222 goto fail;
2223 }
2224
2225 rc = 0; /* success */
2226
2227 fail:
2228 mutex_unlock(&btv->lock);
2229
2230 return rc;
2231}
2232
2233/* Returns an error if the given overlay window dimensions are not
2234 possible with the current cropping parameters. If adjust_size is
2235 TRUE the function may adjust the window width and/or height
2236 instead, however it always rounds the horizontal position and
2237 width as btcx_align() does. If adjust_crop is TRUE the function
2238 may also adjust the current cropping parameters to get closer
2239 to the desired window size. */
2240static int
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002241verify_window_lock (struct bttv_fh * fh,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002242 struct v4l2_window * win,
2243 int adjust_size,
2244 int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
2246 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002247 unsigned int width_mask;
2248 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 if (win->w.width < 48 || win->w.height < 32)
2251 return -EINVAL;
2252 if (win->clipcount > 2048)
2253 return -EINVAL;
2254
2255 field = win->field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 if (V4L2_FIELD_ANY == field) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002258 __s32 height2;
2259
2260 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2261 field = (win->w.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 ? V4L2_FIELD_INTERLACED
2263 : V4L2_FIELD_TOP;
2264 }
2265 switch (field) {
2266 case V4L2_FIELD_TOP:
2267 case V4L2_FIELD_BOTTOM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 case V4L2_FIELD_INTERLACED:
2269 break;
2270 default:
2271 return -EINVAL;
2272 }
2273
Michael Schimeke5bd0262007-01-18 16:17:39 -03002274 /* 4-byte alignment. */
2275 if (NULL == fh->ovfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002277 width_mask = ~0;
2278 switch (fh->ovfmt->depth) {
2279 case 8:
2280 case 24:
2281 width_mask = ~3;
2282 break;
2283 case 16:
2284 width_mask = ~1;
2285 break;
2286 case 32:
2287 break;
2288 default:
2289 BUG();
2290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Michael Schimeke5bd0262007-01-18 16:17:39 -03002292 win->w.width -= win->w.left & ~width_mask;
2293 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2294
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002295 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002296 field, width_mask,
2297 /* width_bias: round down */ 0,
2298 adjust_size, adjust_crop);
2299 if (0 != rc)
2300 return rc;
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 win->field = field;
2303 return 0;
2304}
2305
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002306static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 struct v4l2_window *win, int fixup)
2308{
2309 struct v4l2_clip *clips = NULL;
2310 int n,size,retval = 0;
2311
2312 if (NULL == fh->ovfmt)
2313 return -EINVAL;
2314 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2315 return -EINVAL;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002316 retval = verify_window_lock(fh, win,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002317 /* adjust_size */ fixup,
2318 /* adjust_crop */ fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (0 != retval)
2320 return retval;
2321
2322 /* copy clips -- luckily v4l1 + v4l2 are binary
2323 compatible here ...*/
2324 n = win->clipcount;
2325 size = sizeof(*clips)*(n+4);
2326 clips = kmalloc(size,GFP_KERNEL);
2327 if (NULL == clips)
2328 return -ENOMEM;
2329 if (n > 0) {
2330 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2331 kfree(clips);
2332 return -EFAULT;
2333 }
2334 }
2335 /* clip against screen */
2336 if (NULL != btv->fbuf.base)
2337 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2338 &win->w, clips, n);
2339 btcx_sort_clips(clips,n);
2340
2341 /* 4-byte alignments */
2342 switch (fh->ovfmt->depth) {
2343 case 8:
2344 case 24:
2345 btcx_align(&win->w, clips, n, 3);
2346 break;
2347 case 16:
2348 btcx_align(&win->w, clips, n, 1);
2349 break;
2350 case 32:
2351 /* no alignment fixups needed */
2352 break;
2353 default:
2354 BUG();
2355 }
2356
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002357 mutex_lock(&fh->cap.vb_lock);
2358 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 fh->ov.clips = clips;
2360 fh->ov.nclips = n;
2361
2362 fh->ov.w = win->w;
2363 fh->ov.field = win->field;
2364 fh->ov.setup_ok = 1;
2365 btv->init.ov.w.width = win->w.width;
2366 btv->init.ov.w.height = win->w.height;
2367 btv->init.ov.field = win->field;
2368
2369 /* update overlay if needed */
2370 retval = 0;
2371 if (check_btres(fh, RESOURCE_OVERLAY)) {
2372 struct bttv_buffer *new;
2373
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002374 new = videobuf_sg_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03002375 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2377 retval = bttv_switch_overlay(btv,fh,new);
2378 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002379 mutex_unlock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return retval;
2381}
2382
2383/* ----------------------------------------------------------------------- */
2384
2385static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2386{
2387 struct videobuf_queue* q = NULL;
2388
2389 switch (fh->type) {
2390 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2391 q = &fh->cap;
2392 break;
2393 case V4L2_BUF_TYPE_VBI_CAPTURE:
2394 q = &fh->vbi;
2395 break;
2396 default:
2397 BUG();
2398 }
2399 return q;
2400}
2401
2402static int bttv_resource(struct bttv_fh *fh)
2403{
2404 int res = 0;
2405
2406 switch (fh->type) {
2407 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002408 res = RESOURCE_VIDEO_STREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 break;
2410 case V4L2_BUF_TYPE_VBI_CAPTURE:
2411 res = RESOURCE_VBI;
2412 break;
2413 default:
2414 BUG();
2415 }
2416 return res;
2417}
2418
2419static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2420{
2421 struct videobuf_queue *q = bttv_queue(fh);
2422 int res = bttv_resource(fh);
2423
2424 if (check_btres(fh,res))
2425 return -EBUSY;
2426 if (videobuf_queue_is_busy(q))
2427 return -EBUSY;
2428 fh->type = type;
2429 return 0;
2430}
2431
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002432static void
2433pix_format_set_size (struct v4l2_pix_format * f,
2434 const struct bttv_format * fmt,
2435 unsigned int width,
2436 unsigned int height)
2437{
2438 f->width = width;
2439 f->height = height;
2440
2441 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2442 f->bytesperline = width; /* Y plane */
2443 f->sizeimage = (width * height * fmt->depth) >> 3;
2444 } else {
2445 f->bytesperline = (width * fmt->depth) >> 3;
2446 f->sizeimage = height * f->bytesperline;
2447 }
2448}
2449
Hans Verkuil78b526a2008-05-28 12:16:41 -03002450static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002451 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002453 struct bttv_fh *fh = priv;
2454
2455 pix_format_set_size(&f->fmt.pix, fh->fmt,
2456 fh->width, fh->height);
2457 f->fmt.pix.field = fh->cap.field;
2458 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2459
2460 return 0;
2461}
2462
Hans Verkuil78b526a2008-05-28 12:16:41 -03002463static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002464 struct v4l2_format *f)
2465{
2466 struct bttv_fh *fh = priv;
2467
2468 f->fmt.win.w = fh->ov.w;
2469 f->fmt.win.field = fh->ov.field;
2470
2471 return 0;
2472}
2473
Hans Verkuil78b526a2008-05-28 12:16:41 -03002474static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002475 struct v4l2_format *f)
2476{
2477 const struct bttv_format *fmt;
2478 struct bttv_fh *fh = priv;
2479 struct bttv *btv = fh->btv;
2480 enum v4l2_field field;
2481 __s32 width, height;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002482 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002483
2484 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2485 if (NULL == fmt)
2486 return -EINVAL;
2487
2488 field = f->fmt.pix.field;
2489
2490 if (V4L2_FIELD_ANY == field) {
2491 __s32 height2;
2492
2493 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2494 field = (f->fmt.pix.height > height2)
2495 ? V4L2_FIELD_INTERLACED
2496 : V4L2_FIELD_BOTTOM;
2497 }
2498
2499 if (V4L2_FIELD_SEQ_BT == field)
2500 field = V4L2_FIELD_SEQ_TB;
2501
2502 switch (field) {
2503 case V4L2_FIELD_TOP:
2504 case V4L2_FIELD_BOTTOM:
2505 case V4L2_FIELD_ALTERNATE:
2506 case V4L2_FIELD_INTERLACED:
2507 break;
2508 case V4L2_FIELD_SEQ_TB:
2509 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2510 return -EINVAL;
2511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 default:
2513 return -EINVAL;
2514 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002515
2516 width = f->fmt.pix.width;
2517 height = f->fmt.pix.height;
2518
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002519 rc = limit_scaled_size_lock(fh, &width, &height, field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002520 /* width_mask: 4 pixels */ ~3,
2521 /* width_bias: nearest */ 2,
2522 /* adjust_size */ 1,
2523 /* adjust_crop */ 0);
2524 if (0 != rc)
2525 return rc;
2526
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002527 /* update data for the application */
2528 f->fmt.pix.field = field;
2529 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2530
2531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
Hans Verkuil78b526a2008-05-28 12:16:41 -03002534static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002535 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002537 struct bttv_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002539 return verify_window_lock(fh, &f->fmt.win,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002540 /* adjust_size */ 1,
2541 /* adjust_crop */ 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542}
2543
Hans Verkuil78b526a2008-05-28 12:16:41 -03002544static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002545 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
2547 int retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002548 const struct bttv_format *fmt;
2549 struct bttv_fh *fh = priv;
2550 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002551 __s32 width, height;
2552 enum v4l2_field field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002554 retval = bttv_switch_type(fh, f->type);
2555 if (0 != retval)
2556 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Hans Verkuil78b526a2008-05-28 12:16:41 -03002558 retval = bttv_try_fmt_vid_cap(file, priv, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002559 if (0 != retval)
2560 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002562 width = f->fmt.pix.width;
2563 height = f->fmt.pix.height;
2564 field = f->fmt.pix.field;
2565
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002566 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002567 /* width_mask: 4 pixels */ ~3,
2568 /* width_bias: nearest */ 2,
2569 /* adjust_size */ 1,
2570 /* adjust_crop */ 1);
2571 if (0 != retval)
2572 return retval;
2573
2574 f->fmt.pix.field = field;
2575
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002576 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002578 /* update our state informations */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002579 mutex_lock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002580 fh->fmt = fmt;
2581 fh->cap.field = f->fmt.pix.field;
2582 fh->cap.last = V4L2_FIELD_NONE;
2583 fh->width = f->fmt.pix.width;
2584 fh->height = f->fmt.pix.height;
2585 btv->init.fmt = fmt;
2586 btv->init.width = f->fmt.pix.width;
2587 btv->init.height = f->fmt.pix.height;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002588 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002589
2590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
Hans Verkuil78b526a2008-05-28 12:16:41 -03002593static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002594 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002596 struct bttv_fh *fh = priv;
2597 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002599 if (no_overlay > 0) {
2600 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002601 return -EINVAL;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002602 }
Michael Krufky5e453dc2006-01-09 15:32:31 -02002603
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002604 return setup_window_lock(fh, btv, &f->fmt.win, 1);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002605}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002607#ifdef CONFIG_VIDEO_V4L1_COMPAT
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002608static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2609{
2610 int retval;
2611 unsigned int i;
2612 struct bttv_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002614 mutex_lock(&fh->cap.vb_lock);
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -07002615 retval = __videobuf_mmap_setup(&fh->cap, gbuffers, gbufsize,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002616 V4L2_MEMORY_MMAP);
2617 if (retval < 0) {
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002618 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002619 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002621
2622 gbuffers = retval;
2623 memset(mbuf, 0, sizeof(*mbuf));
2624 mbuf->frames = gbuffers;
2625 mbuf->size = gbuffers * gbufsize;
2626
2627 for (i = 0; i < gbuffers; i++)
2628 mbuf->offsets[i] = i * gbufsize;
2629
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002630 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002631 return 0;
2632}
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002633#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002635static int bttv_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002636 struct v4l2_capability *cap)
2637{
2638 struct bttv_fh *fh = priv;
2639 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002641 if (0 == v4l2)
2642 return -EINVAL;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002643
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002644 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2645 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2646 snprintf(cap->bus_info, sizeof(cap->bus_info),
2647 "PCI:%s", pci_name(btv->c.pci));
2648 cap->version = BTTV_VERSION_CODE;
2649 cap->capabilities =
2650 V4L2_CAP_VIDEO_CAPTURE |
2651 V4L2_CAP_VBI_CAPTURE |
2652 V4L2_CAP_READWRITE |
2653 V4L2_CAP_STREAMING;
Hans Verkuil6a052c82009-06-20 06:09:33 -03002654 if (btv->has_saa6588)
2655 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002656 if (no_overlay <= 0)
2657 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Trent Piephoabb03622009-01-28 21:32:59 -03002659 if (btv->tuner_type != TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002660 cap->capabilities |= V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 return 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002664static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2665{
2666 int index = -1, i;
2667
2668 for (i = 0; i < FORMATS; i++) {
2669 if (formats[i].fourcc != -1)
2670 index++;
2671 if ((unsigned int)index == f->index)
2672 break;
2673 }
2674 if (FORMATS == i)
2675 return -EINVAL;
2676
2677 f->pixelformat = formats[i].fourcc;
2678 strlcpy(f->description, formats[i].name, sizeof(f->description));
2679
2680 return i;
2681}
2682
Hans Verkuil78b526a2008-05-28 12:16:41 -03002683static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002684 struct v4l2_fmtdesc *f)
2685{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002686 int rc = bttv_enum_fmt_cap_ovr(f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002687
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002688 if (rc < 0)
2689 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002690
2691 return 0;
2692}
2693
Hans Verkuil78b526a2008-05-28 12:16:41 -03002694static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002695 struct v4l2_fmtdesc *f)
2696{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002697 int rc;
2698
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002699 if (no_overlay > 0) {
2700 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2701 return -EINVAL;
2702 }
2703
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002704 rc = bttv_enum_fmt_cap_ovr(f);
2705
2706 if (rc < 0)
2707 return rc;
2708
2709 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002710 return -EINVAL;
2711
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002712 return 0;
2713}
2714
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002715static int bttv_g_fbuf(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002716 struct v4l2_framebuffer *fb)
2717{
2718 struct bttv_fh *fh = f;
2719 struct bttv *btv = fh->btv;
2720
2721 *fb = btv->fbuf;
2722 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2723 if (fh->ovfmt)
2724 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2725 return 0;
2726}
2727
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002728static int bttv_overlay(struct file *file, void *f, unsigned int on)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002729{
2730 struct bttv_fh *fh = f;
2731 struct bttv *btv = fh->btv;
2732 struct bttv_buffer *new;
2733 int retval;
2734
2735 if (on) {
2736 /* verify args */
2737 if (NULL == btv->fbuf.base)
2738 return -EINVAL;
2739 if (!fh->ov.setup_ok) {
2740 dprintk("bttv%d: overlay: !setup_ok\n", btv->c.nr);
2741 return -EINVAL;
2742 }
2743 }
2744
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002745 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002746 return -EBUSY;
2747
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002748 mutex_lock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002749 if (on) {
2750 fh->ov.tvnorm = btv->tvnorm;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002751 new = videobuf_sg_alloc(sizeof(*new));
Robert Fitzsimons7c018802008-02-13 16:38:11 -03002752 new->crop = btv->crop[!!fh->do_crop].rect;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002753 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2754 } else {
2755 new = NULL;
2756 }
2757
2758 /* switch over */
2759 retval = bttv_switch_overlay(btv, fh, new);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002760 mutex_unlock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return retval;
2762}
2763
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002764static int bttv_s_fbuf(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002765 struct v4l2_framebuffer *fb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002767 struct bttv_fh *fh = f;
2768 struct bttv *btv = fh->btv;
2769 const struct bttv_format *fmt;
2770 int retval;
2771
2772 if (!capable(CAP_SYS_ADMIN) &&
2773 !capable(CAP_SYS_RAWIO))
2774 return -EPERM;
2775
2776 /* check args */
2777 fmt = format_by_fourcc(fb->fmt.pixelformat);
2778 if (NULL == fmt)
2779 return -EINVAL;
2780 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2781 return -EINVAL;
2782
2783 retval = -EINVAL;
2784 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2785 __s32 width = fb->fmt.width;
2786 __s32 height = fb->fmt.height;
2787
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002788 retval = limit_scaled_size_lock(fh, &width, &height,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002789 V4L2_FIELD_INTERLACED,
2790 /* width_mask */ ~3,
2791 /* width_bias */ 2,
2792 /* adjust_size */ 0,
2793 /* adjust_crop */ 0);
2794 if (0 != retval)
2795 return retval;
2796 }
2797
2798 /* ok, accept it */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002799 mutex_lock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002800 btv->fbuf.base = fb->base;
2801 btv->fbuf.fmt.width = fb->fmt.width;
2802 btv->fbuf.fmt.height = fb->fmt.height;
2803 if (0 != fb->fmt.bytesperline)
2804 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2805 else
2806 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2807
2808 retval = 0;
2809 fh->ovfmt = fmt;
2810 btv->init.ovfmt = fmt;
2811 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2812 fh->ov.w.left = 0;
2813 fh->ov.w.top = 0;
2814 fh->ov.w.width = fb->fmt.width;
2815 fh->ov.w.height = fb->fmt.height;
2816 btv->init.ov.w.width = fb->fmt.width;
2817 btv->init.ov.w.height = fb->fmt.height;
2818 kfree(fh->ov.clips);
2819 fh->ov.clips = NULL;
2820 fh->ov.nclips = 0;
2821
2822 if (check_btres(fh, RESOURCE_OVERLAY)) {
2823 struct bttv_buffer *new;
2824
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002825 new = videobuf_sg_alloc(sizeof(*new));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002826 new->crop = btv->crop[!!fh->do_crop].rect;
2827 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2828 retval = bttv_switch_overlay(btv, fh, new);
2829 }
2830 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002831 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002832 return retval;
2833}
2834
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002835static int bttv_reqbufs(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002836 struct v4l2_requestbuffers *p)
2837{
2838 struct bttv_fh *fh = priv;
2839 return videobuf_reqbufs(bttv_queue(fh), p);
2840}
2841
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002842static int bttv_querybuf(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002843 struct v4l2_buffer *b)
2844{
2845 struct bttv_fh *fh = priv;
2846 return videobuf_querybuf(bttv_queue(fh), b);
2847}
2848
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002849static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002850{
2851 struct bttv_fh *fh = priv;
2852 struct bttv *btv = fh->btv;
2853 int res = bttv_resource(fh);
2854
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002855 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002856 return -EBUSY;
2857
2858 return videobuf_qbuf(bttv_queue(fh), b);
2859}
2860
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002861static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002862{
2863 struct bttv_fh *fh = priv;
2864 return videobuf_dqbuf(bttv_queue(fh), b,
2865 file->f_flags & O_NONBLOCK);
2866}
2867
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002868static int bttv_streamon(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002869 enum v4l2_buf_type type)
2870{
2871 struct bttv_fh *fh = priv;
2872 struct bttv *btv = fh->btv;
2873 int res = bttv_resource(fh);
2874
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002875 if (!check_alloc_btres_lock(btv, fh, res))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002876 return -EBUSY;
2877 return videobuf_streamon(bttv_queue(fh));
2878}
2879
2880
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002881static int bttv_streamoff(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002882 enum v4l2_buf_type type)
2883{
2884 struct bttv_fh *fh = priv;
2885 struct bttv *btv = fh->btv;
2886 int retval;
2887 int res = bttv_resource(fh);
2888
2889
2890 retval = videobuf_streamoff(bttv_queue(fh));
2891 if (retval < 0)
2892 return retval;
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03002893 free_btres_lock(btv, fh, res);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002894 return 0;
2895}
2896
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002897static int bttv_queryctrl(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002898 struct v4l2_queryctrl *c)
2899{
2900 struct bttv_fh *fh = priv;
2901 struct bttv *btv = fh->btv;
2902 const struct v4l2_queryctrl *ctrl;
2903
2904 if ((c->id < V4L2_CID_BASE ||
2905 c->id >= V4L2_CID_LASTP1) &&
2906 (c->id < V4L2_CID_PRIVATE_BASE ||
2907 c->id >= V4L2_CID_PRIVATE_LASTP1))
2908 return -EINVAL;
2909
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002910 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2911 *c = no_ctl;
2912 else {
2913 ctrl = ctrl_by_id(c->id);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002914
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002915 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2916 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002917
2918 return 0;
2919}
2920
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002921static int bttv_g_parm(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002922 struct v4l2_streamparm *parm)
2923{
2924 struct bttv_fh *fh = f;
2925 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002926
Trent Piepho51f0b8d2009-03-04 01:21:02 -03002927 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2928 &parm->parm.capture.timeperframe);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002929 return 0;
2930}
2931
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002932static int bttv_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002933 struct v4l2_tuner *t)
2934{
2935 struct bttv_fh *fh = priv;
2936 struct bttv *btv = fh->btv;
2937
Trent Piephoabb03622009-01-28 21:32:59 -03002938 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002939 return -EINVAL;
2940 if (0 != t->index)
2941 return -EINVAL;
2942
2943 mutex_lock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002944 t->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil859f0272009-03-28 08:29:00 -03002945 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002946 strcpy(t->name, "Television");
2947 t->capability = V4L2_TUNER_CAP_NORM;
2948 t->type = V4L2_TUNER_ANALOG_TV;
2949 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2950 t->signal = 0xffff;
2951
2952 if (btv->audio_mode_gpio)
2953 btv->audio_mode_gpio(btv, t, 0);
2954
2955 mutex_unlock(&btv->lock);
2956 return 0;
2957}
2958
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002959static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002960{
2961 struct bttv_fh *fh = f;
2962 struct bttv *btv = fh->btv;
2963
2964 *p = v4l2_prio_max(&btv->prio);
2965
2966 return 0;
2967}
2968
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002969static int bttv_s_priority(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002970 enum v4l2_priority prio)
2971{
2972 struct bttv_fh *fh = f;
2973 struct bttv *btv = fh->btv;
2974
2975 return v4l2_prio_change(&btv->prio, &fh->prio, prio);
2976}
2977
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002978static int bttv_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002979 struct v4l2_cropcap *cap)
2980{
2981 struct bttv_fh *fh = priv;
2982 struct bttv *btv = fh->btv;
2983
2984 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2985 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2986 return -EINVAL;
2987
2988 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
2989
2990 return 0;
2991}
2992
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002993static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002994{
2995 struct bttv_fh *fh = f;
2996 struct bttv *btv = fh->btv;
2997
2998 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2999 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3000 return -EINVAL;
3001
3002 /* No fh->do_crop = 1; because btv->crop[1] may be
3003 inconsistent with fh->width or fh->height and apps
3004 do not expect a change here. */
3005
3006 crop->c = btv->crop[!!fh->do_crop].rect;
3007
3008 return 0;
3009}
3010
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003011static int bttv_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003012{
3013 struct bttv_fh *fh = f;
3014 struct bttv *btv = fh->btv;
3015 const struct v4l2_rect *b;
3016 int retval;
3017 struct bttv_crop c;
3018 __s32 b_left;
3019 __s32 b_top;
3020 __s32 b_right;
3021 __s32 b_bottom;
3022
3023 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3024 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3025 return -EINVAL;
3026
Hans Verkuilffb48772010-05-01 08:03:24 -03003027 retval = v4l2_prio_check(&btv->prio, fh->prio);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003028 if (0 != retval)
3029 return retval;
3030
3031 /* Make sure tvnorm, vbi_end and the current cropping
3032 parameters remain consistent until we're done. Note
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003033 read() may change vbi_end in check_alloc_btres_lock(). */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003034 mutex_lock(&btv->lock);
3035
3036 retval = -EBUSY;
3037
3038 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
3039 mutex_unlock(&btv->lock);
3040 return retval;
3041 }
3042
3043 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3044
3045 b_left = b->left;
3046 b_right = b_left + b->width;
3047 b_bottom = b->top + b->height;
3048
3049 b_top = max(b->top, btv->vbi_end);
3050 if (b_top + 32 >= b_bottom) {
3051 mutex_unlock(&btv->lock);
3052 return retval;
3053 }
3054
3055 /* Min. scaled size 48 x 32. */
3056 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3057 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3058
3059 c.rect.width = clamp(crop->c.width,
3060 48, b_right - c.rect.left);
3061
3062 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3063 /* Top and height must be a multiple of two. */
3064 c.rect.top = (c.rect.top + 1) & ~1;
3065
3066 c.rect.height = clamp(crop->c.height,
3067 32, b_bottom - c.rect.top);
3068 c.rect.height = (c.rect.height + 1) & ~1;
3069
3070 bttv_crop_calc_limits(&c);
3071
3072 btv->crop[1] = c;
3073
3074 mutex_unlock(&btv->lock);
3075
3076 fh->do_crop = 1;
3077
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003078 mutex_lock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003079
3080 if (fh->width < c.min_scaled_width) {
3081 fh->width = c.min_scaled_width;
3082 btv->init.width = c.min_scaled_width;
3083 } else if (fh->width > c.max_scaled_width) {
3084 fh->width = c.max_scaled_width;
3085 btv->init.width = c.max_scaled_width;
3086 }
3087
3088 if (fh->height < c.min_scaled_height) {
3089 fh->height = c.min_scaled_height;
3090 btv->init.height = c.min_scaled_height;
3091 } else if (fh->height > c.max_scaled_height) {
3092 fh->height = c.max_scaled_height;
3093 btv->init.height = c.max_scaled_height;
3094 }
3095
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003096 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003097
3098 return 0;
3099}
3100
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003101static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003102{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003103 if (unlikely(a->index))
3104 return -EINVAL;
3105
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003106 strcpy(a->name, "audio");
3107 return 0;
3108}
3109
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003110static int bttv_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003111{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003112 if (unlikely(a->index))
3113 return -EINVAL;
3114
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116}
3117
3118static ssize_t bttv_read(struct file *file, char __user *data,
3119 size_t count, loff_t *ppos)
3120{
3121 struct bttv_fh *fh = file->private_data;
3122 int retval = 0;
3123
3124 if (fh->btv->errors)
3125 bttv_reinit_bt848(fh->btv);
3126 dprintk("bttv%d: read count=%d type=%s\n",
3127 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3128
3129 switch (fh->type) {
3130 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003131 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03003132 /* VIDEO_READ in use by another fh,
3133 or VIDEO_STREAM by any fh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return -EBUSY;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3137 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003138 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 break;
3140 case V4L2_BUF_TYPE_VBI_CAPTURE:
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003141 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return -EBUSY;
3143 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3144 file->f_flags & O_NONBLOCK);
3145 break;
3146 default:
3147 BUG();
3148 }
3149 return retval;
3150}
3151
3152static unsigned int bttv_poll(struct file *file, poll_table *wait)
3153{
3154 struct bttv_fh *fh = file->private_data;
3155 struct bttv_buffer *buf;
3156 enum v4l2_field field;
Figo.zhang9fd64182009-06-16 13:31:29 -03003157 unsigned int rc = POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
3159 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003160 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 return POLLERR;
3162 return videobuf_poll_stream(file, &fh->vbi, wait);
3163 }
3164
Mauro Carvalho Chehabca960bf2010-09-18 08:35:57 -03003165 mutex_lock(&fh->cap.vb_lock);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003166 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 /* streaming capture */
3168 if (list_empty(&fh->cap.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -03003169 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3171 } else {
3172 /* read() capture */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if (NULL == fh->cap.read_buf) {
3174 /* need to capture a new frame */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003175 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3176 goto err;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003177 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003178 if (NULL == fh->cap.read_buf)
3179 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3181 field = videobuf_next_field(&fh->cap);
3182 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -08003183 kfree (fh->cap.read_buf);
3184 fh->cap.read_buf = NULL;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003185 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 }
3187 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3188 fh->cap.read_off = 0;
3189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 buf = (struct bttv_buffer*)fh->cap.read_buf;
3191 }
3192
3193 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03003194 if (buf->vb.state == VIDEOBUF_DONE ||
3195 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -03003196 rc = POLLIN|POLLRDNORM;
3197 else
3198 rc = 0;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003199err:
3200 mutex_unlock(&fh->cap.vb_lock);
Figo.zhang9fd64182009-06-16 13:31:29 -03003201 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202}
3203
Hans Verkuilbec43662008-12-30 06:58:20 -03003204static int bttv_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003206 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003207 struct bttv *btv = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 struct bttv_fh *fh;
3209 enum v4l2_buf_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003211 dprintk(KERN_DEBUG "bttv: open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Laurent Pinchart327ae592009-11-27 13:57:55 -03003213 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003214 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Laurent Pinchart327ae592009-11-27 13:57:55 -03003215 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003216 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3217 } else {
3218 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -03003220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Laurent Pinchart327ae592009-11-27 13:57:55 -03003222 lock_kernel();
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3225 btv->c.nr,v4l2_type_names[type]);
3226
3227 /* allocate per filehandle data */
3228 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
Hans Verkuild56dc612008-07-30 08:43:36 -03003229 if (NULL == fh) {
3230 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return -ENOMEM;
Hans Verkuild56dc612008-07-30 08:43:36 -03003232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 file->private_data = fh;
3234 *fh = btv->init;
3235 fh->type = type;
3236 fh->ov.setup_ok = 0;
Hans Verkuilffb48772010-05-01 08:03:24 -03003237 v4l2_prio_open(&btv->prio, &fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003239 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3240 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3242 V4L2_FIELD_INTERLACED,
3243 sizeof(struct bttv_buffer),
3244 fh);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003245 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3246 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 V4L2_BUF_TYPE_VBI_CAPTURE,
3248 V4L2_FIELD_SEQ_TB,
3249 sizeof(struct bttv_buffer),
3250 fh);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03003251 set_tvnorm(btv,btv->tvnorm);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003252 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
3254 btv->users++;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003255
3256 /* The V4L2 spec requires one global set of cropping parameters
3257 which only change on request. These are stored in btv->crop[1].
3258 However for compatibility with V4L apps and cropping unaware
3259 V4L2 apps we now reset the cropping parameters as seen through
3260 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3261 will use btv->crop[0], the default cropping parameters for the
3262 current video standard, and VIDIOC_S_FMT will not implicitely
3263 change the cropping parameters until VIDIOC_S_CROP has been
3264 called. */
3265 fh->do_crop = !reset_crop; /* module parameter */
3266
3267 /* Likewise there should be one global set of VBI capture
3268 parameters, but for compatibility with V4L apps and earlier
3269 driver versions each fh has its own parameters. */
3270 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 bttv_field_count(btv);
Hans Verkuild56dc612008-07-30 08:43:36 -03003273 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 return 0;
3275}
3276
Hans Verkuilbec43662008-12-30 06:58:20 -03003277static int bttv_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
3279 struct bttv_fh *fh = file->private_data;
3280 struct bttv *btv = fh->btv;
3281
3282 /* turn off overlay */
3283 if (check_btres(fh, RESOURCE_OVERLAY))
3284 bttv_switch_overlay(btv,fh,NULL);
3285
3286 /* stop video capture */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003287 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 videobuf_streamoff(&fh->cap);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003289 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 }
3291 if (fh->cap.read_buf) {
3292 buffer_release(&fh->cap,fh->cap.read_buf);
3293 kfree(fh->cap.read_buf);
3294 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03003295 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003296 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
3299 /* stop vbi capture */
3300 if (check_btres(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03003301 videobuf_stop(&fh->vbi);
Mauro Carvalho Chehab8822f0d2010-09-14 12:19:51 -03003302 free_btres_lock(btv,fh,RESOURCE_VBI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
3304
3305 /* free stuff */
3306 videobuf_mmap_free(&fh->cap);
3307 videobuf_mmap_free(&fh->vbi);
Hans Verkuilffb48772010-05-01 08:03:24 -03003308 v4l2_prio_close(&btv->prio, fh->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 file->private_data = NULL;
3310 kfree(fh);
3311
3312 btv->users--;
3313 bttv_field_count(btv);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003314
3315 if (!btv->users)
3316 audio_mute(btv, 1);
3317
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 return 0;
3319}
3320
3321static int
3322bttv_mmap(struct file *file, struct vm_area_struct *vma)
3323{
3324 struct bttv_fh *fh = file->private_data;
3325
3326 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3327 fh->btv->c.nr, v4l2_type_names[fh->type],
3328 vma->vm_start, vma->vm_end - vma->vm_start);
3329 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3330}
3331
Hans Verkuilbec43662008-12-30 06:58:20 -03003332static const struct v4l2_file_operations bttv_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
3334 .owner = THIS_MODULE,
3335 .open = bttv_open,
3336 .release = bttv_release,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003337 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 .read = bttv_read,
3339 .mmap = bttv_mmap,
3340 .poll = bttv_poll,
3341};
3342
Hans Verkuila3998102008-07-21 02:57:38 -03003343static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003344 .vidioc_querycap = bttv_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003345 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3346 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3347 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3348 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3349 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3350 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3351 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3352 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003353 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3354 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3355 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003356 .vidioc_g_audio = bttv_g_audio,
3357 .vidioc_s_audio = bttv_s_audio,
3358 .vidioc_cropcap = bttv_cropcap,
3359 .vidioc_reqbufs = bttv_reqbufs,
3360 .vidioc_querybuf = bttv_querybuf,
3361 .vidioc_qbuf = bttv_qbuf,
3362 .vidioc_dqbuf = bttv_dqbuf,
3363 .vidioc_s_std = bttv_s_std,
3364 .vidioc_enum_input = bttv_enum_input,
3365 .vidioc_g_input = bttv_g_input,
3366 .vidioc_s_input = bttv_s_input,
3367 .vidioc_queryctrl = bttv_queryctrl,
3368 .vidioc_g_ctrl = bttv_g_ctrl,
3369 .vidioc_s_ctrl = bttv_s_ctrl,
3370 .vidioc_streamon = bttv_streamon,
3371 .vidioc_streamoff = bttv_streamoff,
3372 .vidioc_g_tuner = bttv_g_tuner,
3373 .vidioc_s_tuner = bttv_s_tuner,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003374#ifdef CONFIG_VIDEO_V4L1_COMPAT
3375 .vidiocgmbuf = vidiocgmbuf,
3376#endif
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003377 .vidioc_g_crop = bttv_g_crop,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003378 .vidioc_s_crop = bttv_s_crop,
3379 .vidioc_g_fbuf = bttv_g_fbuf,
3380 .vidioc_s_fbuf = bttv_s_fbuf,
3381 .vidioc_overlay = bttv_overlay,
3382 .vidioc_g_priority = bttv_g_priority,
3383 .vidioc_s_priority = bttv_s_priority,
3384 .vidioc_g_parm = bttv_g_parm,
3385 .vidioc_g_frequency = bttv_g_frequency,
3386 .vidioc_s_frequency = bttv_s_frequency,
3387 .vidioc_log_status = bttv_log_status,
3388 .vidioc_querystd = bttv_querystd,
Zoltan Devai43846832008-01-14 13:24:38 -03003389#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003390 .vidioc_g_register = bttv_g_register,
3391 .vidioc_s_register = bttv_s_register,
Zoltan Devai43846832008-01-14 13:24:38 -03003392#endif
Hans Verkuila3998102008-07-21 02:57:38 -03003393};
3394
3395static struct video_device bttv_video_template = {
3396 .fops = &bttv_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003397 .ioctl_ops = &bttv_ioctl_ops,
3398 .tvnorms = BTTV_NORMS,
3399 .current_norm = V4L2_STD_PAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400};
3401
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402/* ----------------------------------------------------------------------- */
3403/* radio interface */
3404
Hans Verkuilbec43662008-12-30 06:58:20 -03003405static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003407 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003408 struct bttv *btv = video_drvdata(file);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003409 struct bttv_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003411 dprintk("bttv: open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Hans Verkuild56dc612008-07-30 08:43:36 -03003413 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
3415 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003416
3417 /* allocate per filehandle data */
3418 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
Hans Verkuild56dc612008-07-30 08:43:36 -03003419 if (NULL == fh) {
3420 unlock_kernel();
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003421 return -ENOMEM;
Hans Verkuild56dc612008-07-30 08:43:36 -03003422 }
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003423 file->private_data = fh;
3424 *fh = btv->init;
3425 v4l2_prio_open(&btv->prio, &fh->prio);
3426
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02003427 mutex_lock(&btv->lock);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003428
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 btv->radio_user++;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003430
Hans Verkuil859f0272009-03-28 08:29:00 -03003431 bttv_call_all(btv, tuner, s_radio);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03003432 audio_input(btv,TVAUDIO_INPUT_RADIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02003434 mutex_unlock(&btv->lock);
Hans Verkuild56dc612008-07-30 08:43:36 -03003435 unlock_kernel();
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437}
3438
Hans Verkuilbec43662008-12-30 06:58:20 -03003439static int radio_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003441 struct bttv_fh *fh = file->private_data;
3442 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003443 struct rds_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Hans Verkuilffb48772010-05-01 08:03:24 -03003445 v4l2_prio_close(&btv->prio, fh->prio);
Robert Fitzsimonsb9bc07a2008-04-10 09:40:31 -03003446 file->private_data = NULL;
3447 kfree(fh);
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 btv->radio_user--;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003450
Hans Verkuil859f0272009-03-28 08:29:00 -03003451 bttv_call_all(btv, core, ioctl, RDS_CMD_CLOSE, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 return 0;
3454}
3455
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003456static int radio_querycap(struct file *file, void *priv,
3457 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003459 struct bttv_fh *fh = priv;
3460 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003462 strcpy(cap->driver, "bttv");
3463 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
3464 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
3465 cap->version = BTTV_VERSION_CODE;
3466 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 return 0;
3469}
3470
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003471static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003473 struct bttv_fh *fh = priv;
3474 struct bttv *btv = fh->btv;
3475
Trent Piephoabb03622009-01-28 21:32:59 -03003476 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003477 return -EINVAL;
3478 if (0 != t->index)
3479 return -EINVAL;
3480 mutex_lock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003481 strcpy(t->name, "Radio");
3482 t->type = V4L2_TUNER_RADIO;
3483
Hans Verkuil859f0272009-03-28 08:29:00 -03003484 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003485
3486 if (btv->audio_mode_gpio)
3487 btv->audio_mode_gpio(btv, t, 0);
3488
3489 mutex_unlock(&btv->lock);
3490
3491 return 0;
3492}
3493
3494static int radio_enum_input(struct file *file, void *priv,
3495 struct v4l2_input *i)
3496{
3497 if (i->index != 0)
3498 return -EINVAL;
3499
3500 strcpy(i->name, "Radio");
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003501 i->type = V4L2_INPUT_TYPE_TUNER;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003502
3503 return 0;
3504}
3505
3506static int radio_g_audio(struct file *file, void *priv,
3507 struct v4l2_audio *a)
3508{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003509 if (unlikely(a->index))
Cyrill Gorcunov1a002eb2008-04-01 17:49:13 -03003510 return -EINVAL;
3511
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003512 strcpy(a->name, "Radio");
Cyrill Gorcunov1a002eb2008-04-01 17:49:13 -03003513
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003514 return 0;
3515}
3516
3517static int radio_s_tuner(struct file *file, void *priv,
3518 struct v4l2_tuner *t)
3519{
3520 struct bttv_fh *fh = priv;
3521 struct bttv *btv = fh->btv;
3522
3523 if (0 != t->index)
3524 return -EINVAL;
3525
Hans Verkuil859f0272009-03-28 08:29:00 -03003526 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003527 return 0;
3528}
3529
3530static int radio_s_audio(struct file *file, void *priv,
3531 struct v4l2_audio *a)
3532{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003533 if (unlikely(a->index))
3534 return -EINVAL;
3535
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003536 return 0;
3537}
3538
3539static int radio_s_input(struct file *filp, void *priv, unsigned int i)
3540{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003541 if (unlikely(i))
3542 return -EINVAL;
3543
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003544 return 0;
3545}
3546
3547static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
3548{
3549 return 0;
3550}
3551
3552static int radio_queryctrl(struct file *file, void *priv,
3553 struct v4l2_queryctrl *c)
3554{
3555 const struct v4l2_queryctrl *ctrl;
3556
3557 if (c->id < V4L2_CID_BASE ||
3558 c->id >= V4L2_CID_LASTP1)
3559 return -EINVAL;
3560
3561 if (c->id == V4L2_CID_AUDIO_MUTE) {
3562 ctrl = ctrl_by_id(c->id);
3563 *c = *ctrl;
3564 } else
3565 *c = no_ctl;
3566
3567 return 0;
3568}
3569
3570static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
3571{
3572 *i = 0;
3573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003576static ssize_t radio_read(struct file *file, char __user *data,
3577 size_t count, loff_t *ppos)
3578{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003579 struct bttv_fh *fh = file->private_data;
3580 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003581 struct rds_command cmd;
3582 cmd.block_count = count/3;
3583 cmd.buffer = data;
3584 cmd.instance = file;
3585 cmd.result = -ENODEV;
3586
Hans Verkuil859f0272009-03-28 08:29:00 -03003587 bttv_call_all(btv, core, ioctl, RDS_CMD_READ, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003588
3589 return cmd.result;
3590}
3591
3592static unsigned int radio_poll(struct file *file, poll_table *wait)
3593{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003594 struct bttv_fh *fh = file->private_data;
3595 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003596 struct rds_command cmd;
3597 cmd.instance = file;
3598 cmd.event_list = wait;
3599 cmd.result = -ENODEV;
Hans Verkuil859f0272009-03-28 08:29:00 -03003600 bttv_call_all(btv, core, ioctl, RDS_CMD_POLL, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003601
3602 return cmd.result;
3603}
3604
Hans Verkuilbec43662008-12-30 06:58:20 -03003605static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
3607 .owner = THIS_MODULE,
3608 .open = radio_open,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003609 .read = radio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 .release = radio_release,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003611 .ioctl = video_ioctl2,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003612 .poll = radio_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613};
3614
Hans Verkuila3998102008-07-21 02:57:38 -03003615static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003616 .vidioc_querycap = radio_querycap,
3617 .vidioc_g_tuner = radio_g_tuner,
3618 .vidioc_enum_input = radio_enum_input,
3619 .vidioc_g_audio = radio_g_audio,
3620 .vidioc_s_tuner = radio_s_tuner,
3621 .vidioc_s_audio = radio_s_audio,
3622 .vidioc_s_input = radio_s_input,
3623 .vidioc_s_std = radio_s_std,
3624 .vidioc_queryctrl = radio_queryctrl,
3625 .vidioc_g_input = radio_g_input,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003626 .vidioc_g_ctrl = bttv_g_ctrl,
3627 .vidioc_s_ctrl = bttv_s_ctrl,
3628 .vidioc_g_frequency = bttv_g_frequency,
3629 .vidioc_s_frequency = bttv_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630};
3631
Hans Verkuila3998102008-07-21 02:57:38 -03003632static struct video_device radio_template = {
3633 .fops = &radio_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03003634 .ioctl_ops = &radio_ioctl_ops,
3635};
3636
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637/* ----------------------------------------------------------------------- */
3638/* some debug code */
3639
Adrian Bunk408b6642005-05-01 08:59:29 -07003640static int bttv_risc_decode(u32 risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641{
3642 static char *instr[16] = {
3643 [ BT848_RISC_WRITE >> 28 ] = "write",
3644 [ BT848_RISC_SKIP >> 28 ] = "skip",
3645 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3646 [ BT848_RISC_JUMP >> 28 ] = "jump",
3647 [ BT848_RISC_SYNC >> 28 ] = "sync",
3648 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3649 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3650 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3651 };
3652 static int incr[16] = {
3653 [ BT848_RISC_WRITE >> 28 ] = 2,
3654 [ BT848_RISC_JUMP >> 28 ] = 2,
3655 [ BT848_RISC_SYNC >> 28 ] = 2,
3656 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3657 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3658 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3659 };
3660 static char *bits[] = {
3661 "be0", "be1", "be2", "be3/resync",
3662 "set0", "set1", "set2", "set3",
3663 "clr0", "clr1", "clr2", "clr3",
3664 "irq", "res", "eol", "sol",
3665 };
3666 int i;
3667
3668 printk("0x%08x [ %s", risc,
3669 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3670 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3671 if (risc & (1 << (i + 12)))
3672 printk(" %s",bits[i]);
3673 printk(" count=%d ]\n", risc & 0xfff);
3674 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3675}
3676
Adrian Bunk408b6642005-05-01 08:59:29 -07003677static void bttv_risc_disasm(struct bttv *btv,
3678 struct btcx_riscmem *risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
3680 unsigned int i,j,n;
3681
3682 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003683 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 for (i = 0; i < (risc->size >> 2); i += n) {
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003685 printk("%s: 0x%lx: ", btv->c.v4l2_dev.name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 (unsigned long)(risc->dma + (i<<2)));
Al Viro3aa71102008-06-22 14:20:09 -03003687 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 for (j = 1; j < n; j++)
3689 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003690 btv->c.v4l2_dev.name, (unsigned long)(risc->dma + ((i+j)<<2)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 risc->cpu[i+j], j);
3692 if (0 == risc->cpu[i])
3693 break;
3694 }
3695}
3696
3697static void bttv_print_riscaddr(struct bttv *btv)
3698{
3699 printk(" main: %08Lx\n",
3700 (unsigned long long)btv->main.dma);
3701 printk(" vbi : o=%08Lx e=%08Lx\n",
3702 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3703 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3704 printk(" cap : o=%08Lx e=%08Lx\n",
3705 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3706 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3707 printk(" scr : o=%08Lx e=%08Lx\n",
3708 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3709 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3710 bttv_risc_disasm(btv, &btv->main);
3711}
3712
3713/* ----------------------------------------------------------------------- */
3714/* irq handler */
3715
3716static char *irq_name[] = {
3717 "FMTCHG", // format change detected (525 vs. 625)
3718 "VSYNC", // vertical sync (new field)
3719 "HSYNC", // horizontal sync
3720 "OFLOW", // chroma/luma AGC overflow
3721 "HLOCK", // horizontal lock changed
3722 "VPRES", // video presence changed
3723 "6", "7",
3724 "I2CDONE", // hw irc operation finished
3725 "GPINT", // gpio port triggered irq
3726 "10",
3727 "RISCI", // risc instruction triggered irq
3728 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3729 "FTRGT", // pixel data fifo overrun
3730 "FDSR", // fifo data stream resyncronisation
3731 "PPERR", // parity error (data transfer)
3732 "RIPERR", // parity error (read risc instructions)
3733 "PABORT", // pci abort
3734 "OCERR", // risc instruction error
3735 "SCERR", // syncronisation error
3736};
3737
3738static void bttv_print_irqbits(u32 print, u32 mark)
3739{
3740 unsigned int i;
3741
3742 printk("bits:");
3743 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3744 if (print & (1 << i))
3745 printk(" %s",irq_name[i]);
3746 if (mark & (1 << i))
3747 printk("*");
3748 }
3749}
3750
3751static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3752{
3753 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3754 btv->c.nr,
3755 (unsigned long)btv->main.dma,
Al Viro3aa71102008-06-22 14:20:09 -03003756 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3757 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 (unsigned long)rc);
3759
3760 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3761 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3762 "Ok, then this is harmless, don't worry ;)\n",
3763 btv->c.nr);
3764 return;
3765 }
3766 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3767 btv->c.nr);
3768 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3769 btv->c.nr);
3770 dump_stack();
3771}
3772
3773static int
3774bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3775{
3776 struct bttv_buffer *item;
3777
3778 memset(set,0,sizeof(*set));
3779
3780 /* capture request ? */
3781 if (!list_empty(&btv->capture)) {
3782 set->frame_irq = 1;
3783 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3784 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3785 set->top = item;
3786 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3787 set->bottom = item;
3788
3789 /* capture request for other field ? */
3790 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3791 (item->vb.queue.next != &btv->capture)) {
3792 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
Mike Isely66349b42009-09-21 12:09:08 -03003793 /* Mike Isely <isely@pobox.com> - Only check
3794 * and set up the bottom field in the logic
3795 * below. Don't ever do the top field. This
3796 * of course means that if we set up the
3797 * bottom field in the above code that we'll
3798 * actually skip a field. But that's OK.
3799 * Having processed only a single buffer this
3800 * time, then the next time around the first
3801 * available buffer should be for a top field.
3802 * That will then cause us here to set up a
3803 * top then a bottom field in the normal way.
3804 * The alternative to this understanding is
3805 * that we set up the second available buffer
3806 * as a top field, but that's out of order
3807 * since this driver always processes the top
3808 * field first - the effect will be the two
3809 * buffers being returned in the wrong order,
3810 * with the second buffer also being delayed
3811 * by one field time (owing to the fifo nature
3812 * of videobuf). Worse still, we'll be stuck
3813 * doing fields out of order now every time
3814 * until something else causes a field to be
3815 * dropped. By effectively forcing a field to
3816 * drop this way then we always get back into
3817 * sync within a single frame time. (Out of
3818 * order fields can screw up deinterlacing
3819 * algorithms.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 if (NULL == set->bottom &&
3822 V4L2_FIELD_BOTTOM == item->vb.field) {
3823 set->bottom = item;
3824 }
3825 if (NULL != set->top && NULL != set->bottom)
3826 set->top_irq = 2;
3827 }
3828 }
3829 }
3830
3831 /* screen overlay ? */
3832 if (NULL != btv->screen) {
3833 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3834 if (NULL == set->top && NULL == set->bottom) {
3835 set->top = btv->screen;
3836 set->bottom = btv->screen;
3837 }
3838 } else {
3839 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3840 NULL == set->top) {
3841 set->top = btv->screen;
3842 }
3843 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3844 NULL == set->bottom) {
3845 set->bottom = btv->screen;
3846 }
3847 }
3848 }
3849
3850 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3851 btv->c.nr,set->top, set->bottom,
3852 btv->screen,set->frame_irq,set->top_irq);
3853 return 0;
3854}
3855
3856static void
3857bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3858 struct bttv_buffer_set *curr, unsigned int state)
3859{
3860 struct timeval ts;
3861
3862 do_gettimeofday(&ts);
3863
3864 if (wakeup->top == wakeup->bottom) {
3865 if (NULL != wakeup->top && curr->top != wakeup->top) {
3866 if (irq_debug > 1)
3867 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3868 wakeup->top->vb.ts = ts;
3869 wakeup->top->vb.field_count = btv->field_count;
3870 wakeup->top->vb.state = state;
3871 wake_up(&wakeup->top->vb.done);
3872 }
3873 } else {
3874 if (NULL != wakeup->top && curr->top != wakeup->top) {
3875 if (irq_debug > 1)
3876 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3877 wakeup->top->vb.ts = ts;
3878 wakeup->top->vb.field_count = btv->field_count;
3879 wakeup->top->vb.state = state;
3880 wake_up(&wakeup->top->vb.done);
3881 }
3882 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3883 if (irq_debug > 1)
3884 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3885 wakeup->bottom->vb.ts = ts;
3886 wakeup->bottom->vb.field_count = btv->field_count;
3887 wakeup->bottom->vb.state = state;
3888 wake_up(&wakeup->bottom->vb.done);
3889 }
3890 }
3891}
3892
3893static void
3894bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3895 unsigned int state)
3896{
3897 struct timeval ts;
3898
3899 if (NULL == wakeup)
3900 return;
3901
3902 do_gettimeofday(&ts);
3903 wakeup->vb.ts = ts;
3904 wakeup->vb.field_count = btv->field_count;
3905 wakeup->vb.state = state;
3906 wake_up(&wakeup->vb.done);
3907}
3908
3909static void bttv_irq_timeout(unsigned long data)
3910{
3911 struct bttv *btv = (struct bttv *)data;
3912 struct bttv_buffer_set old,new;
3913 struct bttv_buffer *ovbi;
3914 struct bttv_buffer *item;
3915 unsigned long flags;
3916
3917 if (bttv_verbose) {
3918 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3919 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3920 btread(BT848_RISC_COUNT));
3921 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3922 printk("\n");
3923 }
3924
3925 spin_lock_irqsave(&btv->s_lock,flags);
3926
3927 /* deactivate stuff */
3928 memset(&new,0,sizeof(new));
3929 old = btv->curr;
3930 ovbi = btv->cvbi;
3931 btv->curr = new;
3932 btv->cvbi = NULL;
3933 btv->loop_irq = 0;
3934 bttv_buffer_activate_video(btv, &new);
3935 bttv_buffer_activate_vbi(btv, NULL);
3936 bttv_set_dma(btv, 0);
3937
3938 /* wake up */
Brandon Philips0fc06862007-11-06 20:02:36 -03003939 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3940 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
3942 /* cancel all outstanding capture / vbi requests */
3943 while (!list_empty(&btv->capture)) {
3944 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3945 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003946 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 wake_up(&item->vb.done);
3948 }
3949 while (!list_empty(&btv->vcapture)) {
3950 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3951 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003952 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 wake_up(&item->vb.done);
3954 }
3955
3956 btv->errors++;
3957 spin_unlock_irqrestore(&btv->s_lock,flags);
3958}
3959
3960static void
3961bttv_irq_wakeup_top(struct bttv *btv)
3962{
3963 struct bttv_buffer *wakeup = btv->curr.top;
3964
3965 if (NULL == wakeup)
3966 return;
3967
3968 spin_lock(&btv->s_lock);
3969 btv->curr.top_irq = 0;
3970 btv->curr.top = NULL;
3971 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3972
3973 do_gettimeofday(&wakeup->vb.ts);
3974 wakeup->vb.field_count = btv->field_count;
Brandon Philips0fc06862007-11-06 20:02:36 -03003975 wakeup->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 wake_up(&wakeup->vb.done);
3977 spin_unlock(&btv->s_lock);
3978}
3979
3980static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3981{
3982 if (rc < risc->dma)
3983 return 0;
3984 if (rc > risc->dma + risc->size)
3985 return 0;
3986 return 1;
3987}
3988
3989static void
3990bttv_irq_switch_video(struct bttv *btv)
3991{
3992 struct bttv_buffer_set new;
3993 struct bttv_buffer_set old;
3994 dma_addr_t rc;
3995
3996 spin_lock(&btv->s_lock);
3997
3998 /* new buffer set */
3999 bttv_irq_next_video(btv, &new);
4000 rc = btread(BT848_RISC_COUNT);
4001 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
4002 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
4003 btv->framedrop++;
4004 if (debug_latency)
4005 bttv_irq_debug_low_latency(btv, rc);
4006 spin_unlock(&btv->s_lock);
4007 return;
4008 }
4009
4010 /* switch over */
4011 old = btv->curr;
4012 btv->curr = new;
4013 btv->loop_irq &= ~1;
4014 bttv_buffer_activate_video(btv, &new);
4015 bttv_set_dma(btv, 0);
4016
4017 /* switch input */
4018 if (UNSET != btv->new_input) {
4019 video_mux(btv,btv->new_input);
4020 btv->new_input = UNSET;
4021 }
4022
4023 /* wake up finished buffers */
Brandon Philips0fc06862007-11-06 20:02:36 -03004024 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 spin_unlock(&btv->s_lock);
4026}
4027
4028static void
4029bttv_irq_switch_vbi(struct bttv *btv)
4030{
4031 struct bttv_buffer *new = NULL;
4032 struct bttv_buffer *old;
4033 u32 rc;
4034
4035 spin_lock(&btv->s_lock);
4036
4037 if (!list_empty(&btv->vcapture))
4038 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4039 old = btv->cvbi;
4040
4041 rc = btread(BT848_RISC_COUNT);
4042 if (NULL != old && (is_active(&old->top, rc) ||
4043 is_active(&old->bottom, rc))) {
4044 btv->framedrop++;
4045 if (debug_latency)
4046 bttv_irq_debug_low_latency(btv, rc);
4047 spin_unlock(&btv->s_lock);
4048 return;
4049 }
4050
4051 /* switch */
4052 btv->cvbi = new;
4053 btv->loop_irq &= ~4;
4054 bttv_buffer_activate_vbi(btv, new);
4055 bttv_set_dma(btv, 0);
4056
Brandon Philips0fc06862007-11-06 20:02:36 -03004057 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 spin_unlock(&btv->s_lock);
4059}
4060
David Howells7d12e782006-10-05 14:55:46 +01004061static irqreturn_t bttv_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
4063 u32 stat,astat;
4064 u32 dstat;
4065 int count;
4066 struct bttv *btv;
4067 int handled = 0;
4068
4069 btv=(struct bttv *)dev_id;
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004070
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004071 if (btv->custom_irq)
4072 handled = btv->custom_irq(btv);
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004073
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 count=0;
4075 while (1) {
4076 /* get/clear interrupt status bits */
4077 stat=btread(BT848_INT_STAT);
4078 astat=stat&btread(BT848_INT_MASK);
4079 if (!astat)
4080 break;
4081 handled = 1;
4082 btwrite(stat,BT848_INT_STAT);
4083
4084 /* get device status bits */
4085 dstat=btread(BT848_DSTATUS);
4086
4087 if (irq_debug) {
4088 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4089 "riscs=%x, riscc=%08x, ",
4090 btv->c.nr, count, btv->field_count,
4091 stat>>28, btread(BT848_RISC_COUNT));
4092 bttv_print_irqbits(stat,astat);
4093 if (stat & BT848_INT_HLOCK)
4094 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4095 ? "yes" : "no");
4096 if (stat & BT848_INT_VPRES)
4097 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4098 ? "yes" : "no");
4099 if (stat & BT848_INT_FMTCHG)
4100 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4101 ? "625" : "525");
4102 printk("\n");
4103 }
4104
4105 if (astat&BT848_INT_VSYNC)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004106 btv->field_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004108 if ((astat & BT848_INT_GPINT) && btv->remote) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004110 bttv_input_irq(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 }
4112
4113 if (astat & BT848_INT_I2CDONE) {
4114 btv->i2c_done = stat;
4115 wake_up(&btv->i2c_queue);
4116 }
4117
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004118 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 bttv_irq_switch_vbi(btv);
4120
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004121 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 bttv_irq_wakeup_top(btv);
4123
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004124 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 bttv_irq_switch_video(btv);
4126
4127 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004128 audio_mute(btv, btv->mute); /* trigger automute */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
4130 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4131 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4132 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4133 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4134 btread(BT848_RISC_COUNT));
4135 bttv_print_irqbits(stat,astat);
4136 printk("\n");
4137 if (bttv_debug)
4138 bttv_print_riscaddr(btv);
4139 }
4140 if (fdsr && astat & BT848_INT_FDSR) {
4141 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4142 btv->c.nr,btread(BT848_RISC_COUNT));
4143 if (bttv_debug)
4144 bttv_print_riscaddr(btv);
4145 }
4146
4147 count++;
4148 if (count > 4) {
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004149
4150 if (count > 8 || !(astat & BT848_INT_GPINT)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004151 btwrite(0, BT848_INT_MASK);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004152
4153 printk(KERN_ERR
4154 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4155 } else {
4156 printk(KERN_ERR
4157 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4158
4159 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4160 BT848_INT_MASK);
4161 };
4162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 bttv_print_irqbits(stat,astat);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004164
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 printk("]\n");
4166 }
4167 }
4168 btv->irq_total++;
4169 if (handled)
4170 btv->irq_me++;
4171 return IRQ_RETVAL(handled);
4172}
4173
4174
4175/* ----------------------------------------------------------------------- */
4176/* initialitation */
4177
4178static struct video_device *vdev_init(struct bttv *btv,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004179 const struct video_device *template,
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004180 const char *type_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181{
4182 struct video_device *vfd;
4183
4184 vfd = video_device_alloc();
4185 if (NULL == vfd)
4186 return NULL;
4187 *vfd = *template;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004188 vfd->v4l2_dev = &btv->c.v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 vfd->release = video_device_release;
Mauro Carvalho Chehab1d0a4362008-06-23 12:31:29 -03004190 vfd->debug = bttv_debug;
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004191 video_set_drvdata(vfd, btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4193 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004194 type_name, bttv_tvcards[btv->c.type].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 return vfd;
4196}
4197
4198static void bttv_unregister_video(struct bttv *btv)
4199{
4200 if (btv->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004201 if (video_is_registered(btv->video_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 video_unregister_device(btv->video_dev);
4203 else
4204 video_device_release(btv->video_dev);
4205 btv->video_dev = NULL;
4206 }
4207 if (btv->vbi_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004208 if (video_is_registered(btv->vbi_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 video_unregister_device(btv->vbi_dev);
4210 else
4211 video_device_release(btv->vbi_dev);
4212 btv->vbi_dev = NULL;
4213 }
4214 if (btv->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004215 if (video_is_registered(btv->radio_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 video_unregister_device(btv->radio_dev);
4217 else
4218 video_device_release(btv->radio_dev);
4219 btv->radio_dev = NULL;
4220 }
4221}
4222
4223/* register video4linux devices */
4224static int __devinit bttv_register_video(struct bttv *btv)
4225{
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004226 if (no_overlay > 0)
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004227 printk("bttv: Overlay support disabled.\n");
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004228
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 /* video */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004230 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004231
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004232 if (NULL == btv->video_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004234 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
4235 video_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 goto err;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03004237 printk(KERN_INFO "bttv%d: registered device %s\n",
4238 btv->c.nr, video_device_node_name(btv->video_dev));
Hans Verkuil22a04f12008-07-20 06:35:02 -03004239 if (device_create_file(&btv->video_dev->dev,
Kay Sievers54bd5b62007-10-08 16:26:13 -03004240 &dev_attr_card)<0) {
4241 printk(KERN_ERR "bttv%d: device_create_file 'card' "
Trent Piephod94fc9a2006-07-29 17:18:06 -03004242 "failed\n", btv->c.nr);
4243 goto err;
4244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
4246 /* vbi */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004247 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004248
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004249 if (NULL == btv->vbi_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004251 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
4252 vbi_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 goto err;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03004254 printk(KERN_INFO "bttv%d: registered device %s\n",
4255 btv->c.nr, video_device_node_name(btv->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004257 if (!btv->has_radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 return 0;
4259 /* radio */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004260 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004261 if (NULL == btv->radio_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004263 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4264 radio_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 goto err;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03004266 printk(KERN_INFO "bttv%d: registered device %s\n",
4267 btv->c.nr, video_device_node_name(btv->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
4269 /* all done */
4270 return 0;
4271
4272 err:
4273 bttv_unregister_video(btv);
4274 return -1;
4275}
4276
4277
4278/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4279/* response on cards with no firmware is not enabled by OF */
4280static void pci_set_command(struct pci_dev *dev)
4281{
4282#if defined(__powerpc__)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004283 unsigned int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004285 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4286 cmd = (cmd | PCI_COMMAND_MEMORY );
4287 pci_write_config_dword(dev, PCI_COMMAND, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288#endif
4289}
4290
4291static int __devinit bttv_probe(struct pci_dev *dev,
4292 const struct pci_device_id *pci_id)
4293{
4294 int result;
4295 unsigned char lat;
4296 struct bttv *btv;
4297
4298 if (bttv_num == BTTV_MAX)
4299 return -ENOMEM;
4300 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004301 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004302 if (btv == NULL) {
4303 printk(KERN_ERR "bttv: out of memory.\n");
4304 return -ENOMEM;
4305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 btv->c.nr = bttv_num;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004307 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4308 "bttv%d", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
4310 /* initialize structs / fill in defaults */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02004311 mutex_init(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004312 spin_lock_init(&btv->s_lock);
4313 spin_lock_init(&btv->gpio_lock);
4314 init_waitqueue_head(&btv->gpioq);
4315 init_waitqueue_head(&btv->i2c_queue);
4316 INIT_LIST_HEAD(&btv->c.subs);
4317 INIT_LIST_HEAD(&btv->capture);
4318 INIT_LIST_HEAD(&btv->vcapture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 v4l2_prio_init(&btv->prio);
4320
4321 init_timer(&btv->timeout);
4322 btv->timeout.function = bttv_irq_timeout;
4323 btv->timeout.data = (unsigned long)btv;
4324
Michael Krufky7c08fb02005-11-08 21:36:21 -08004325 btv->i2c_rc = -1;
4326 btv->tuner_type = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 btv->new_input = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 btv->has_radio=radio[btv->c.nr];
4329
4330 /* pci stuff (init, get irq/mmio, ... */
4331 btv->c.pci = dev;
Michael Krufky7c08fb02005-11-08 21:36:21 -08004332 btv->id = dev->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 if (pci_enable_device(dev)) {
Michael Krufky7c08fb02005-11-08 21:36:21 -08004334 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 btv->c.nr);
4336 return -EIO;
4337 }
Yang Hongyang284901a2009-04-06 19:01:15 -07004338 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004339 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 btv->c.nr);
4341 return -EIO;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 if (!request_mem_region(pci_resource_start(dev,0),
4344 pci_resource_len(dev,0),
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004345 btv->c.v4l2_dev.name)) {
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004346 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4347 btv->c.nr,
4348 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 return -EBUSY;
4350 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004351 pci_set_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 pci_set_command(dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004353
4354 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4355 if (result < 0) {
4356 printk(KERN_WARNING "bttv%d: v4l2_device_register() failed\n", btv->c.nr);
4357 goto fail0;
4358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004360 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4361 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4362 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4363 bttv_num,btv->id, btv->revision, pci_name(dev));
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004364 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4365 btv->c.pci->irq, lat,
4366 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 schedule();
4368
Akinobu Mita5f1693f2006-12-20 10:08:56 -03004369 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4370 if (NULL == btv->bt848_mmio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4372 result = -EIO;
4373 goto fail1;
4374 }
4375
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004376 /* identify card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 bttv_idcard(btv);
4378
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004379 /* disable irqs, register irq handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 btwrite(0, BT848_INT_MASK);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004381 result = request_irq(btv->c.pci->irq, bttv_irq,
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004382 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004383 if (result < 0) {
4384 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 bttv_num,btv->c.pci->irq);
4386 goto fail1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
4389 if (0 != bttv_handle_chipset(btv)) {
4390 result = -EIO;
4391 goto fail2;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
4394 /* init options from insmod args */
4395 btv->opt_combfilter = combfilter;
4396 btv->opt_lumafilter = lumafilter;
4397 btv->opt_automute = automute;
4398 btv->opt_chroma_agc = chroma_agc;
4399 btv->opt_adc_crush = adc_crush;
4400 btv->opt_vcr_hack = vcr_hack;
4401 btv->opt_whitecrush_upper = whitecrush_upper;
4402 btv->opt_whitecrush_lower = whitecrush_lower;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07004403 btv->opt_uv_ratio = uv_ratio;
4404 btv->opt_full_luma_range = full_luma_range;
4405 btv->opt_coring = coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406
4407 /* fill struct bttv with some useful defaults */
4408 btv->init.btv = btv;
4409 btv->init.ov.w.width = 320;
4410 btv->init.ov.w.height = 240;
Mauro Carvalho Chehabc96dd072007-10-26 16:51:47 -03004411 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 btv->init.width = 320;
4413 btv->init.height = 240;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 btv->input = 0;
4415
4416 /* initialize hardware */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004417 if (bttv_gpio)
4418 bttv_gpio_tracking(btv,"pre-init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
4420 bttv_risc_init_main(btv);
4421 init_bt848(btv);
4422
4423 /* gpio */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004424 btwrite(0x00, BT848_GPIO_REG_INP);
4425 btwrite(0x00, BT848_GPIO_OUT_EN);
4426 if (bttv_verbose)
4427 bttv_gpio_tracking(btv,"init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004429 /* needs to be done before i2c is registered */
4430 bttv_init_card1(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004432 /* register i2c + gpio */
4433 init_bttv_i2c(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004435 /* some card-specific stuff (needs working i2c) */
4436 bttv_init_card2(btv);
Hans Verkuil2c905772009-07-20 08:14:17 -03004437 bttv_init_tuner(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 init_irqreg(btv);
4439
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004440 /* register video4linux + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 if (!bttv_tvcards[btv->c.type].no_video) {
4442 bttv_register_video(btv);
4443 bt848_bright(btv,32768);
4444 bt848_contrast(btv,32768);
4445 bt848_hue(btv,32768);
4446 bt848_sat(btv,32768);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004447 audio_mute(btv, 1);
Trent Piepho333408f2007-07-03 15:08:10 -03004448 set_input(btv, 0, btv->tvnorm);
Michael Schimeke5bd0262007-01-18 16:17:39 -03004449 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4450 btv->crop[1] = btv->crop[0]; /* current = default */
4451 disclaim_vbi_lines(btv);
4452 disclaim_video_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 }
4454
Jarod Wilsonf992a492007-03-24 15:23:50 -03004455 /* add subdevices and autoload dvb-bt8xx if needed */
4456 if (bttv_tvcards[btv->c.type].has_dvb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 bttv_sub_add_device(&btv->c, "dvb");
Jarod Wilsonf992a492007-03-24 15:23:50 -03004458 request_modules(btv);
4459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Jean Delvared90a4ae2010-02-16 14:22:37 -03004461 if (!disable_ir) {
4462 init_bttv_i2c_ir(btv);
4463 bttv_input_init(btv);
4464 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004465
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 /* everything is fine */
4467 bttv_num++;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004470fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004471 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004473fail1:
4474 v4l2_device_unregister(&btv->c.v4l2_dev);
4475
4476fail0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 if (btv->bt848_mmio)
4478 iounmap(btv->bt848_mmio);
4479 release_mem_region(pci_resource_start(btv->c.pci,0),
4480 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 return result;
4482}
4483
4484static void __devexit bttv_remove(struct pci_dev *pci_dev)
4485{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004486 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4487 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
4489 if (bttv_verbose)
4490 printk("bttv%d: unloading\n",btv->c.nr);
4491
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004492 /* shutdown everything (DMA+IRQs) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 btand(~15, BT848_GPIO_DMA_CTL);
4494 btwrite(0, BT848_INT_MASK);
4495 btwrite(~0x0, BT848_INT_STAT);
4496 btwrite(0x0, BT848_GPIO_OUT_EN);
4497 if (bttv_gpio)
4498 bttv_gpio_tracking(btv,"cleanup");
4499
4500 /* tell gpio modules we are leaving ... */
4501 btv->shutdown=1;
4502 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004503 bttv_input_fini(btv);
Christopher Pascoe889aee82006-01-09 15:25:28 -02004504 bttv_sub_del_devices(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004506 /* unregister i2c_bus + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 fini_bttv_i2c(btv);
4508
4509 /* unregister video4linux */
4510 bttv_unregister_video(btv);
4511
4512 /* free allocated memory */
4513 btcx_riscmem_free(btv->c.pci,&btv->main);
4514
4515 /* free ressources */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004516 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 iounmap(btv->bt848_mmio);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004518 release_mem_region(pci_resource_start(btv->c.pci,0),
4519 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004521 v4l2_device_unregister(&btv->c.v4l2_dev);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004522 bttvs[btv->c.nr] = NULL;
4523 kfree(btv);
4524
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004525 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526}
4527
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004528#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4530{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004531 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4532 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 struct bttv_buffer_set idle;
4534 unsigned long flags;
4535
Mauro Carvalho Chehab0f97a932005-09-09 13:04:05 -07004536 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
4538 /* stop dma + irqs */
4539 spin_lock_irqsave(&btv->s_lock,flags);
4540 memset(&idle, 0, sizeof(idle));
4541 btv->state.video = btv->curr;
4542 btv->state.vbi = btv->cvbi;
4543 btv->state.loop_irq = btv->loop_irq;
4544 btv->curr = idle;
4545 btv->loop_irq = 0;
4546 bttv_buffer_activate_video(btv, &idle);
4547 bttv_buffer_activate_vbi(btv, NULL);
4548 bttv_set_dma(btv, 0);
4549 btwrite(0, BT848_INT_MASK);
4550 spin_unlock_irqrestore(&btv->s_lock,flags);
4551
4552 /* save bt878 state */
4553 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4554 btv->state.gpio_data = gpio_read();
4555
4556 /* save pci state */
4557 pci_save_state(pci_dev);
4558 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4559 pci_disable_device(pci_dev);
4560 btv->state.disabled = 1;
4561 }
4562 return 0;
4563}
4564
4565static int bttv_resume(struct pci_dev *pci_dev)
4566{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004567 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4568 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004570 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571
4572 dprintk("bttv%d: resume\n", btv->c.nr);
4573
4574 /* restore pci state */
4575 if (btv->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004576 err=pci_enable_device(pci_dev);
4577 if (err) {
4578 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4579 btv->c.nr);
4580 return err;
4581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 btv->state.disabled = 0;
4583 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004584 err=pci_set_power_state(pci_dev, PCI_D0);
4585 if (err) {
4586 pci_disable_device(pci_dev);
4587 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4588 btv->c.nr);
4589 btv->state.disabled = 1;
4590 return err;
4591 }
4592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 pci_restore_state(pci_dev);
4594
4595 /* restore bt878 state */
4596 bttv_reinit_bt848(btv);
4597 gpio_inout(0xffffff, btv->state.gpio_enable);
4598 gpio_write(btv->state.gpio_data);
4599
4600 /* restart dma */
4601 spin_lock_irqsave(&btv->s_lock,flags);
4602 btv->curr = btv->state.video;
4603 btv->cvbi = btv->state.vbi;
4604 btv->loop_irq = btv->state.loop_irq;
4605 bttv_buffer_activate_video(btv, &btv->curr);
4606 bttv_buffer_activate_vbi(btv, btv->cvbi);
4607 bttv_set_dma(btv, 0);
4608 spin_unlock_irqrestore(&btv->s_lock,flags);
4609 return 0;
4610}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004611#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
4613static struct pci_device_id bttv_pci_tbl[] = {
Joe Perches76e97412009-07-05 15:59:21 -03004614 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4615 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4616 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4617 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004618 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619};
4620
4621MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4622
4623static struct pci_driver bttv_pci_driver = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004624 .name = "bttv",
4625 .id_table = bttv_pci_tbl,
4626 .probe = bttv_probe,
4627 .remove = __devexit_p(bttv_remove),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004628#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 .suspend = bttv_suspend,
4630 .resume = bttv_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004631#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632};
4633
Adrian Bunk7d44e892007-12-11 19:23:43 -03004634static int __init bttv_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
Randy Dunlapc526e222006-07-15 09:08:26 -03004636 int ret;
4637
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 bttv_num = 0;
4639
4640 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4641 (BTTV_VERSION_CODE >> 16) & 0xff,
4642 (BTTV_VERSION_CODE >> 8) & 0xff,
4643 BTTV_VERSION_CODE & 0xff);
4644#ifdef SNAPSHOT
4645 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4646 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4647#endif
4648 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4649 gbuffers = 2;
Filipe Rossetf41b6962009-05-28 11:11:53 -03004650 if (gbufsize > BTTV_MAX_FBUF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 gbufsize = BTTV_MAX_FBUF;
4652 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4653 if (bttv_verbose)
4654 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4655 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4656
4657 bttv_check_chipset();
4658
Randy Dunlapc526e222006-07-15 09:08:26 -03004659 ret = bus_register(&bttv_sub_bus_type);
4660 if (ret < 0) {
4661 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4662 return ret;
4663 }
Akinobu Mita9e7e85e2007-12-17 14:26:29 -03004664 ret = pci_register_driver(&bttv_pci_driver);
4665 if (ret < 0)
4666 bus_unregister(&bttv_sub_bus_type);
4667
4668 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669}
4670
Adrian Bunk7d44e892007-12-11 19:23:43 -03004671static void __exit bttv_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672{
4673 pci_unregister_driver(&bttv_pci_driver);
4674 bus_unregister(&bttv_sub_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675}
4676
4677module_init(bttv_init_module);
4678module_exit(bttv_cleanup_module);
4679
4680/*
4681 * Local variables:
4682 * c-basic-offset: 8
4683 * End:
4684 */