blob: dcad2f7a995c70bfa1c017d7550e69347d7d0d1e [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>
40#include <linux/errno.h>
41#include <linux/fs.h>
42#include <linux/kernel.h>
43#include <linux/sched.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040044#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/interrupt.h>
46#include <linux/kdev_t.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020047#include "bttvp.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020048#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030049#include <media/v4l2-ioctl.h>
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -030050#include <media/tvaudio.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030051#include <media/msp3400.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020052
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -070053#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include <asm/io.h>
56#include <asm/byteorder.h>
57
Mauro Carvalho Chehabfa3fcce2006-03-23 21:45:24 -030058#include <media/rds.h>
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070059
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061unsigned int bttv_num; /* number of Bt848s in use */
Trent Piepho4b10d3b2009-01-28 21:32:59 -030062struct bttv *bttvs[BTTV_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020064unsigned int bttv_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065unsigned int bttv_verbose = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020066unsigned int bttv_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* config variables */
69#ifdef __BIG_ENDIAN
70static unsigned int bigendian=1;
71#else
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020072static unsigned int bigendian;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
74static unsigned int radio[BTTV_MAX];
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020075static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static unsigned int gbuffers = 8;
77static unsigned int gbufsize = 0x208000;
Michael Schimeke5bd0262007-01-18 16:17:39 -030078static unsigned int reset_crop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Jean Delvare176c2f32008-09-07 12:49:59 -030080static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
81static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
82static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020083static int debug_latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020085static unsigned int fdsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/* options */
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020088static unsigned int combfilter;
89static unsigned int lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static unsigned int automute = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020091static unsigned int chroma_agc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static unsigned int adc_crush = 1;
93static unsigned int whitecrush_upper = 0xCF;
94static unsigned int whitecrush_lower = 0x7F;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020095static unsigned int vcr_hack;
96static unsigned int irq_iswitch;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -070097static unsigned int uv_ratio = 50;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020098static unsigned int full_luma_range;
99static unsigned int coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/* API features (turn on/off stuff for testing) */
102static unsigned int v4l2 = 1;
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* insmod args */
105module_param(bttv_verbose, int, 0644);
106module_param(bttv_gpio, int, 0644);
107module_param(bttv_debug, int, 0644);
108module_param(irq_debug, int, 0644);
109module_param(debug_latency, int, 0644);
110
111module_param(fdsr, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112module_param(gbuffers, int, 0444);
113module_param(gbufsize, int, 0444);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300114module_param(reset_crop, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116module_param(v4l2, int, 0644);
117module_param(bigendian, int, 0644);
118module_param(irq_iswitch, int, 0644);
119module_param(combfilter, int, 0444);
120module_param(lumafilter, int, 0444);
121module_param(automute, int, 0444);
122module_param(chroma_agc, int, 0444);
123module_param(adc_crush, int, 0444);
124module_param(whitecrush_upper, int, 0444);
125module_param(whitecrush_lower, int, 0444);
126module_param(vcr_hack, int, 0444);
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700127module_param(uv_ratio, int, 0444);
128module_param(full_luma_range, int, 0444);
129module_param(coring, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Jean Delvare176c2f32008-09-07 12:49:59 -0300131module_param_array(radio, int, NULL, 0444);
132module_param_array(video_nr, int, NULL, 0444);
133module_param_array(radio_nr, int, NULL, 0444);
134module_param_array(vbi_nr, int, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
137MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
138MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
139MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
140MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
141MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
142MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
143MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
Michael Schimeke5bd0262007-01-18 16:17:39 -0300144MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
145 "is 1 (yes) for compatibility with older applications");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
147MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
148MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
149MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
150MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
151MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
152MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700153MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
154MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
155MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
Jean Delvare176c2f32008-09-07 12:49:59 -0300156MODULE_PARM_DESC(video_nr, "video device numbers");
157MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
158MODULE_PARM_DESC(radio_nr, "radio device numbers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
161MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
162MODULE_LICENSE("GPL");
163
164/* ----------------------------------------------------------------------- */
165/* sysfs */
166
Kay Sievers54bd5b62007-10-08 16:26:13 -0300167static ssize_t show_card(struct device *cd,
168 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300170 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -0300171 struct bttv *btv = video_get_drvdata(vfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
173}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300174static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/* ----------------------------------------------------------------------- */
Jarod Wilsonf992a492007-03-24 15:23:50 -0300177/* dvb auto-load setup */
178#if defined(CONFIG_MODULES) && defined(MODULE)
179static void request_module_async(struct work_struct *work)
180{
181 request_module("dvb-bt8xx");
182}
183
184static void request_modules(struct bttv *dev)
185{
186 INIT_WORK(&dev->request_module_wk, request_module_async);
187 schedule_work(&dev->request_module_wk);
188}
189#else
190#define request_modules(dev)
191#endif /* CONFIG_MODULES */
192
193
194/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/* static data */
196
197/* special timing tables from conexant... */
198static u8 SRAM_Table[][60] =
199{
200 /* PAL digital input over GPIO[7:0] */
201 {
202 45, // 45 bytes following
203 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
204 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
205 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
206 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
207 0x37,0x00,0xAF,0x21,0x00
208 },
209 /* NTSC digital input over GPIO[7:0] */
210 {
211 51, // 51 bytes following
212 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
213 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
214 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
215 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
216 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
217 0x00,
218 },
219 // TGB_NTSC392 // quartzsight
220 // This table has been modified to be used for Fusion Rev D
221 {
222 0x2A, // size of table = 42
223 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
224 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
225 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
226 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
227 0x20, 0x00
228 }
229};
230
Michael Schimeke5bd0262007-01-18 16:17:39 -0300231/* minhdelayx1 first video pixel we can capture on a line and
232 hdelayx1 start of active video, both relative to rising edge of
233 /HRESET pulse (0H) in 1 / fCLKx1.
234 swidth width of active video and
235 totalwidth total line width, both in 1 / fCLKx1.
236 sqwidth total line width in square pixels.
237 vdelay start of active video in 2 * field lines relative to
238 trailing edge of /VRESET pulse (VDELAY register).
239 sheight height of active video in 2 * field lines.
240 videostart0 ITU-R frame line number of the line corresponding
241 to vdelay in the first field. */
242#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
243 vdelay, sheight, videostart0) \
244 .cropcap.bounds.left = minhdelayx1, \
245 /* * 2 because vertically we count field lines times two, */ \
246 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
247 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
248 /* 4 is a safety margin at the end of the line. */ \
249 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
250 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
251 .cropcap.defrect.left = hdelayx1, \
252 .cropcap.defrect.top = (videostart0) * 2, \
253 .cropcap.defrect.width = swidth, \
254 .cropcap.defrect.height = sheight, \
255 .cropcap.pixelaspect.numerator = totalwidth, \
256 .cropcap.pixelaspect.denominator = sqwidth,
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258const struct bttv_tvnorm bttv_tvnorms[] = {
259 /* PAL-BDGHI */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800260 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
261 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 {
263 .v4l2_id = V4L2_STD_PAL,
264 .name = "PAL",
265 .Fsc = 35468950,
266 .swidth = 924,
267 .sheight = 576,
268 .totalwidth = 1135,
269 .adelay = 0x7f,
270 .bdelay = 0x72,
271 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
272 .scaledtwidth = 1135,
273 .hdelayx1 = 186,
274 .hactivex1 = 924,
275 .vdelay = 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300276 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 .sram = 0,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200278 /* ITU-R frame line number of the first VBI line
Michael Schimeke5bd0262007-01-18 16:17:39 -0300279 we can capture, of the first and second field.
280 The last line is determined by cropcap.bounds. */
281 .vbistart = { 7, 320 },
282 CROPCAP(/* minhdelayx1 */ 68,
283 /* hdelayx1 */ 186,
284 /* Should be (768 * 1135 + 944 / 2) / 944.
285 cropcap.defrect is used for image width
286 checks, so we keep the old value 924. */
287 /* swidth */ 924,
288 /* totalwidth */ 1135,
289 /* sqwidth */ 944,
290 /* vdelay */ 0x20,
291 /* sheight */ 576,
292 /* videostart0 */ 23)
293 /* bt878 (and bt848?) can capture another
294 line below active video. */
295 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 },{
Hans Verkuild97a11e2006-02-07 06:48:40 -0200297 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 .name = "NTSC",
299 .Fsc = 28636363,
300 .swidth = 768,
301 .sheight = 480,
302 .totalwidth = 910,
303 .adelay = 0x68,
304 .bdelay = 0x5d,
305 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
306 .scaledtwidth = 910,
307 .hdelayx1 = 128,
308 .hactivex1 = 910,
309 .vdelay = 0x1a,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300310 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 .sram = 1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200312 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300313 CROPCAP(/* minhdelayx1 */ 68,
314 /* hdelayx1 */ 128,
315 /* Should be (640 * 910 + 780 / 2) / 780? */
316 /* swidth */ 768,
317 /* totalwidth */ 910,
318 /* sqwidth */ 780,
319 /* vdelay */ 0x1a,
320 /* sheight */ 480,
321 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 },{
323 .v4l2_id = V4L2_STD_SECAM,
324 .name = "SECAM",
325 .Fsc = 35468950,
326 .swidth = 924,
327 .sheight = 576,
328 .totalwidth = 1135,
329 .adelay = 0x7f,
330 .bdelay = 0xb0,
331 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
332 .scaledtwidth = 1135,
333 .hdelayx1 = 186,
334 .hactivex1 = 922,
335 .vdelay = 0x20,
336 .vbipack = 255,
337 .sram = 0, /* like PAL, correct? */
Michael H. Schimek67f15702006-01-09 15:25:27 -0200338 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300339 CROPCAP(/* minhdelayx1 */ 68,
340 /* hdelayx1 */ 186,
341 /* swidth */ 924,
342 /* totalwidth */ 1135,
343 /* sqwidth */ 944,
344 /* vdelay */ 0x20,
345 /* sheight */ 576,
346 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 },{
348 .v4l2_id = V4L2_STD_PAL_Nc,
349 .name = "PAL-Nc",
350 .Fsc = 28636363,
351 .swidth = 640,
352 .sheight = 576,
353 .totalwidth = 910,
354 .adelay = 0x68,
355 .bdelay = 0x5d,
356 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
357 .scaledtwidth = 780,
358 .hdelayx1 = 130,
359 .hactivex1 = 734,
360 .vdelay = 0x1a,
361 .vbipack = 144,
362 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200363 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300364 CROPCAP(/* minhdelayx1 */ 68,
365 /* hdelayx1 */ 130,
366 /* swidth */ (640 * 910 + 780 / 2) / 780,
367 /* totalwidth */ 910,
368 /* sqwidth */ 780,
369 /* vdelay */ 0x1a,
370 /* sheight */ 576,
371 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 },{
373 .v4l2_id = V4L2_STD_PAL_M,
374 .name = "PAL-M",
375 .Fsc = 28636363,
376 .swidth = 640,
377 .sheight = 480,
378 .totalwidth = 910,
379 .adelay = 0x68,
380 .bdelay = 0x5d,
381 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
382 .scaledtwidth = 780,
383 .hdelayx1 = 135,
384 .hactivex1 = 754,
385 .vdelay = 0x1a,
386 .vbipack = 144,
387 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200388 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300389 CROPCAP(/* minhdelayx1 */ 68,
390 /* hdelayx1 */ 135,
391 /* swidth */ (640 * 910 + 780 / 2) / 780,
392 /* totalwidth */ 910,
393 /* sqwidth */ 780,
394 /* vdelay */ 0x1a,
395 /* sheight */ 480,
396 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 },{
398 .v4l2_id = V4L2_STD_PAL_N,
399 .name = "PAL-N",
400 .Fsc = 35468950,
401 .swidth = 768,
402 .sheight = 576,
403 .totalwidth = 1135,
404 .adelay = 0x7f,
405 .bdelay = 0x72,
406 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
407 .scaledtwidth = 944,
408 .hdelayx1 = 186,
409 .hactivex1 = 922,
410 .vdelay = 0x20,
411 .vbipack = 144,
412 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300413 .vbistart = { 7, 320 },
414 CROPCAP(/* minhdelayx1 */ 68,
415 /* hdelayx1 */ 186,
416 /* swidth */ (768 * 1135 + 944 / 2) / 944,
417 /* totalwidth */ 1135,
418 /* sqwidth */ 944,
419 /* vdelay */ 0x20,
420 /* sheight */ 576,
421 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 },{
423 .v4l2_id = V4L2_STD_NTSC_M_JP,
424 .name = "NTSC-JP",
425 .Fsc = 28636363,
426 .swidth = 640,
427 .sheight = 480,
428 .totalwidth = 910,
429 .adelay = 0x68,
430 .bdelay = 0x5d,
431 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
432 .scaledtwidth = 780,
433 .hdelayx1 = 135,
434 .hactivex1 = 754,
435 .vdelay = 0x16,
436 .vbipack = 144,
437 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300438 .vbistart = { 10, 273 },
439 CROPCAP(/* minhdelayx1 */ 68,
440 /* hdelayx1 */ 135,
441 /* swidth */ (640 * 910 + 780 / 2) / 780,
442 /* totalwidth */ 910,
443 /* sqwidth */ 780,
444 /* vdelay */ 0x16,
445 /* sheight */ 480,
446 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 },{
448 /* that one hopefully works with the strange timing
449 * which video recorders produce when playing a NTSC
450 * tape on a PAL TV ... */
451 .v4l2_id = V4L2_STD_PAL_60,
452 .name = "PAL-60",
453 .Fsc = 35468950,
454 .swidth = 924,
455 .sheight = 480,
456 .totalwidth = 1135,
457 .adelay = 0x7f,
458 .bdelay = 0x72,
459 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
460 .scaledtwidth = 1135,
461 .hdelayx1 = 186,
462 .hactivex1 = 924,
463 .vdelay = 0x1a,
464 .vbipack = 255,
465 .vtotal = 524,
466 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200467 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300468 CROPCAP(/* minhdelayx1 */ 68,
469 /* hdelayx1 */ 186,
470 /* swidth */ 924,
471 /* totalwidth */ 1135,
472 /* sqwidth */ 944,
473 /* vdelay */ 0x1a,
474 /* sheight */ 480,
475 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477};
478static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
479
480/* ----------------------------------------------------------------------- */
481/* bttv format list
482 packed pixel formats must come first */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300483static const struct bttv_format formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 {
485 .name = "8 bpp, gray",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 .fourcc = V4L2_PIX_FMT_GREY,
487 .btformat = BT848_COLOR_FMT_Y8,
488 .depth = 8,
489 .flags = FORMAT_FLAGS_PACKED,
490 },{
491 .name = "8 bpp, dithered color",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 .fourcc = V4L2_PIX_FMT_HI240,
493 .btformat = BT848_COLOR_FMT_RGB8,
494 .depth = 8,
495 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
496 },{
497 .name = "15 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 .fourcc = V4L2_PIX_FMT_RGB555,
499 .btformat = BT848_COLOR_FMT_RGB15,
500 .depth = 16,
501 .flags = FORMAT_FLAGS_PACKED,
502 },{
503 .name = "15 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 .fourcc = V4L2_PIX_FMT_RGB555X,
505 .btformat = BT848_COLOR_FMT_RGB15,
506 .btswap = 0x03, /* byteswap */
507 .depth = 16,
508 .flags = FORMAT_FLAGS_PACKED,
509 },{
510 .name = "16 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 .fourcc = V4L2_PIX_FMT_RGB565,
512 .btformat = BT848_COLOR_FMT_RGB16,
513 .depth = 16,
514 .flags = FORMAT_FLAGS_PACKED,
515 },{
516 .name = "16 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 .fourcc = V4L2_PIX_FMT_RGB565X,
518 .btformat = BT848_COLOR_FMT_RGB16,
519 .btswap = 0x03, /* byteswap */
520 .depth = 16,
521 .flags = FORMAT_FLAGS_PACKED,
522 },{
523 .name = "24 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 .fourcc = V4L2_PIX_FMT_BGR24,
525 .btformat = BT848_COLOR_FMT_RGB24,
526 .depth = 24,
527 .flags = FORMAT_FLAGS_PACKED,
528 },{
529 .name = "32 bpp RGB, le",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 .fourcc = V4L2_PIX_FMT_BGR32,
531 .btformat = BT848_COLOR_FMT_RGB32,
532 .depth = 32,
533 .flags = FORMAT_FLAGS_PACKED,
534 },{
535 .name = "32 bpp RGB, be",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 .fourcc = V4L2_PIX_FMT_RGB32,
537 .btformat = BT848_COLOR_FMT_RGB32,
538 .btswap = 0x0f, /* byte+word swap */
539 .depth = 32,
540 .flags = FORMAT_FLAGS_PACKED,
541 },{
542 .name = "4:2:2, packed, YUYV",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 .fourcc = V4L2_PIX_FMT_YUYV,
544 .btformat = BT848_COLOR_FMT_YUY2,
545 .depth = 16,
546 .flags = FORMAT_FLAGS_PACKED,
547 },{
548 .name = "4:2:2, packed, YUYV",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 .fourcc = V4L2_PIX_FMT_YUYV,
550 .btformat = BT848_COLOR_FMT_YUY2,
551 .depth = 16,
552 .flags = FORMAT_FLAGS_PACKED,
553 },{
554 .name = "4:2:2, packed, UYVY",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 .fourcc = V4L2_PIX_FMT_UYVY,
556 .btformat = BT848_COLOR_FMT_YUY2,
557 .btswap = 0x03, /* byteswap */
558 .depth = 16,
559 .flags = FORMAT_FLAGS_PACKED,
560 },{
561 .name = "4:2:2, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 .fourcc = V4L2_PIX_FMT_YUV422P,
563 .btformat = BT848_COLOR_FMT_YCrCb422,
564 .depth = 16,
565 .flags = FORMAT_FLAGS_PLANAR,
566 .hshift = 1,
567 .vshift = 0,
568 },{
569 .name = "4:2:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 .fourcc = V4L2_PIX_FMT_YUV420,
571 .btformat = BT848_COLOR_FMT_YCrCb422,
572 .depth = 12,
573 .flags = FORMAT_FLAGS_PLANAR,
574 .hshift = 1,
575 .vshift = 1,
576 },{
577 .name = "4:2:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 .fourcc = V4L2_PIX_FMT_YVU420,
579 .btformat = BT848_COLOR_FMT_YCrCb422,
580 .depth = 12,
581 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
582 .hshift = 1,
583 .vshift = 1,
584 },{
585 .name = "4:1:1, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 .fourcc = V4L2_PIX_FMT_YUV411P,
587 .btformat = BT848_COLOR_FMT_YCrCb411,
588 .depth = 12,
589 .flags = FORMAT_FLAGS_PLANAR,
590 .hshift = 2,
591 .vshift = 0,
592 },{
593 .name = "4:1:0, planar, Y-Cb-Cr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 .fourcc = V4L2_PIX_FMT_YUV410,
595 .btformat = BT848_COLOR_FMT_YCrCb411,
596 .depth = 9,
597 .flags = FORMAT_FLAGS_PLANAR,
598 .hshift = 2,
599 .vshift = 2,
600 },{
601 .name = "4:1:0, planar, Y-Cr-Cb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 .fourcc = V4L2_PIX_FMT_YVU410,
603 .btformat = BT848_COLOR_FMT_YCrCb411,
604 .depth = 9,
605 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
606 .hshift = 2,
607 .vshift = 2,
608 },{
609 .name = "raw scanlines",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 .fourcc = -1,
611 .btformat = BT848_COLOR_FMT_RAW,
612 .depth = 8,
613 .flags = FORMAT_FLAGS_RAW,
614 }
615};
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300616static const unsigned int FORMATS = ARRAY_SIZE(formats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618/* ----------------------------------------------------------------------- */
619
620#define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
621#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
622#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
623#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
624#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
625#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
626#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
627#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700628#define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
629#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
630#define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
631#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633static const struct v4l2_queryctrl no_ctl = {
634 .name = "42",
635 .flags = V4L2_CTRL_FLAG_DISABLED,
636};
637static const struct v4l2_queryctrl bttv_ctls[] = {
638 /* --- video --- */
639 {
640 .id = V4L2_CID_BRIGHTNESS,
641 .name = "Brightness",
642 .minimum = 0,
643 .maximum = 65535,
644 .step = 256,
645 .default_value = 32768,
646 .type = V4L2_CTRL_TYPE_INTEGER,
647 },{
648 .id = V4L2_CID_CONTRAST,
649 .name = "Contrast",
650 .minimum = 0,
651 .maximum = 65535,
652 .step = 128,
653 .default_value = 32768,
654 .type = V4L2_CTRL_TYPE_INTEGER,
655 },{
656 .id = V4L2_CID_SATURATION,
657 .name = "Saturation",
658 .minimum = 0,
659 .maximum = 65535,
660 .step = 128,
661 .default_value = 32768,
662 .type = V4L2_CTRL_TYPE_INTEGER,
663 },{
664 .id = V4L2_CID_HUE,
665 .name = "Hue",
666 .minimum = 0,
667 .maximum = 65535,
668 .step = 256,
669 .default_value = 32768,
670 .type = V4L2_CTRL_TYPE_INTEGER,
671 },
672 /* --- audio --- */
673 {
674 .id = V4L2_CID_AUDIO_MUTE,
675 .name = "Mute",
676 .minimum = 0,
677 .maximum = 1,
678 .type = V4L2_CTRL_TYPE_BOOLEAN,
679 },{
680 .id = V4L2_CID_AUDIO_VOLUME,
681 .name = "Volume",
682 .minimum = 0,
683 .maximum = 65535,
684 .step = 65535/100,
685 .default_value = 65535,
686 .type = V4L2_CTRL_TYPE_INTEGER,
687 },{
688 .id = V4L2_CID_AUDIO_BALANCE,
689 .name = "Balance",
690 .minimum = 0,
691 .maximum = 65535,
692 .step = 65535/100,
693 .default_value = 32768,
694 .type = V4L2_CTRL_TYPE_INTEGER,
695 },{
696 .id = V4L2_CID_AUDIO_BASS,
697 .name = "Bass",
698 .minimum = 0,
699 .maximum = 65535,
700 .step = 65535/100,
701 .default_value = 32768,
702 .type = V4L2_CTRL_TYPE_INTEGER,
703 },{
704 .id = V4L2_CID_AUDIO_TREBLE,
705 .name = "Treble",
706 .minimum = 0,
707 .maximum = 65535,
708 .step = 65535/100,
709 .default_value = 32768,
710 .type = V4L2_CTRL_TYPE_INTEGER,
711 },
712 /* --- private --- */
713 {
714 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
715 .name = "chroma agc",
716 .minimum = 0,
717 .maximum = 1,
718 .type = V4L2_CTRL_TYPE_BOOLEAN,
719 },{
720 .id = V4L2_CID_PRIVATE_COMBFILTER,
721 .name = "combfilter",
722 .minimum = 0,
723 .maximum = 1,
724 .type = V4L2_CTRL_TYPE_BOOLEAN,
725 },{
726 .id = V4L2_CID_PRIVATE_AUTOMUTE,
727 .name = "automute",
728 .minimum = 0,
729 .maximum = 1,
730 .type = V4L2_CTRL_TYPE_BOOLEAN,
731 },{
732 .id = V4L2_CID_PRIVATE_LUMAFILTER,
733 .name = "luma decimation filter",
734 .minimum = 0,
735 .maximum = 1,
736 .type = V4L2_CTRL_TYPE_BOOLEAN,
737 },{
738 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
739 .name = "agc crush",
740 .minimum = 0,
741 .maximum = 1,
742 .type = V4L2_CTRL_TYPE_BOOLEAN,
743 },{
744 .id = V4L2_CID_PRIVATE_VCR_HACK,
745 .name = "vcr hack",
746 .minimum = 0,
747 .maximum = 1,
748 .type = V4L2_CTRL_TYPE_BOOLEAN,
749 },{
750 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
751 .name = "whitecrush upper",
752 .minimum = 0,
753 .maximum = 255,
754 .step = 1,
755 .default_value = 0xCF,
756 .type = V4L2_CTRL_TYPE_INTEGER,
757 },{
758 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
759 .name = "whitecrush lower",
760 .minimum = 0,
761 .maximum = 255,
762 .step = 1,
763 .default_value = 0x7F,
764 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700765 },{
766 .id = V4L2_CID_PRIVATE_UV_RATIO,
767 .name = "uv ratio",
768 .minimum = 0,
769 .maximum = 100,
770 .step = 1,
771 .default_value = 50,
772 .type = V4L2_CTRL_TYPE_INTEGER,
773 },{
774 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
775 .name = "full luma range",
776 .minimum = 0,
777 .maximum = 1,
778 .type = V4L2_CTRL_TYPE_BOOLEAN,
779 },{
780 .id = V4L2_CID_PRIVATE_CORING,
781 .name = "coring",
782 .minimum = 0,
783 .maximum = 3,
784 .step = 1,
785 .default_value = 0,
786 .type = V4L2_CTRL_TYPE_INTEGER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700789
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300793static const struct v4l2_queryctrl *ctrl_by_id(int id)
794{
795 int i;
796
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300797 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300798 if (bttv_ctls[i].id == id)
799 return bttv_ctls+i;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -0300800
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300801 return NULL;
802}
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804/* ----------------------------------------------------------------------- */
805/* resource management */
806
Michael Schimeke5bd0262007-01-18 16:17:39 -0300807/*
808 RESOURCE_ allocated by freed by
809
810 VIDEO_READ bttv_read 1) bttv_read 2)
811
812 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
813 VIDIOC_QBUF 1) bttv_release
814 VIDIOCMCAPTURE 1)
815
816 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
817 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
818 3) bttv_release
819
820 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
821 VIDIOC_QBUF 1) bttv_release
822 bttv_read, bttv_poll 1) 4)
823
824 1) The resource must be allocated when we enter buffer prepare functions
825 and remain allocated while buffers are in the DMA queue.
826 2) This is a single frame read.
827 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
828 RESOURCE_OVERLAY is allocated.
829 4) This is a continuous read, implies VIDIOC_STREAMON.
830
831 Note this driver permits video input and standard changes regardless if
832 resources are allocated.
833*/
834
835#define VBI_RESOURCES (RESOURCE_VBI)
836#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
837 RESOURCE_VIDEO_STREAM | \
838 RESOURCE_OVERLAY)
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840static
841int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
842{
Michael Schimeke5bd0262007-01-18 16:17:39 -0300843 int xbits; /* mutual exclusive resources */
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (fh->resources & bit)
846 /* have it already allocated */
847 return 1;
848
Michael Schimeke5bd0262007-01-18 16:17:39 -0300849 xbits = bit;
850 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
851 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /* is it free? */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300854 mutex_lock(&btv->lock);
855 if (btv->resources & xbits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /* no, someone else uses it */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300857 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300859
860 if ((bit & VIDEO_RESOURCES)
861 && 0 == (btv->resources & VIDEO_RESOURCES)) {
862 /* Do crop - use current, don't - use default parameters. */
863 __s32 top = btv->crop[!!fh->do_crop].rect.top;
864
865 if (btv->vbi_end > top)
866 goto fail;
867
868 /* We cannot capture the same line as video and VBI data.
869 Claim scan lines crop[].rect.top to bottom. */
870 btv->crop_start = top;
871 } else if (bit & VBI_RESOURCES) {
872 __s32 end = fh->vbi_fmt.end;
873
874 if (end > btv->crop_start)
875 goto fail;
876
877 /* Claim scan lines above fh->vbi_fmt.end. */
878 btv->vbi_end = end;
879 }
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* it's free, grab it */
882 fh->resources |= bit;
883 btv->resources |= bit;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300884 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return 1;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300886
887 fail:
888 mutex_unlock(&btv->lock);
889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892static
893int check_btres(struct bttv_fh *fh, int bit)
894{
895 return (fh->resources & bit);
896}
897
898static
899int locked_btres(struct bttv *btv, int bit)
900{
901 return (btv->resources & bit);
902}
903
Michael Schimeke5bd0262007-01-18 16:17:39 -0300904/* Call with btv->lock down. */
905static void
906disclaim_vbi_lines(struct bttv *btv)
907{
908 btv->vbi_end = 0;
909}
910
911/* Call with btv->lock down. */
912static void
913disclaim_video_lines(struct bttv *btv)
914{
915 const struct bttv_tvnorm *tvnorm;
916 u8 crop;
917
918 tvnorm = &bttv_tvnorms[btv->tvnorm];
919 btv->crop_start = tvnorm->cropcap.bounds.top
920 + tvnorm->cropcap.bounds.height;
921
922 /* VBI capturing ends at VDELAY, start of video capturing, no
923 matter how many lines the VBI RISC program expects. When video
924 capturing is off, it shall no longer "preempt" VBI capturing,
925 so we set VDELAY to maximum. */
926 crop = btread(BT848_E_CROP) | 0xc0;
927 btwrite(crop, BT848_E_CROP);
928 btwrite(0xfe, BT848_E_VDELAY_LO);
929 btwrite(crop, BT848_O_CROP);
930 btwrite(0xfe, BT848_O_VDELAY_LO);
931}
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933static
934void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
935{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if ((fh->resources & bits) != bits) {
937 /* trying to free ressources not allocated by us ... */
938 printk("bttv: BUG! (btres)\n");
939 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300940 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 fh->resources &= ~bits;
942 btv->resources &= ~bits;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300943
944 bits = btv->resources;
945
946 if (0 == (bits & VIDEO_RESOURCES))
947 disclaim_video_lines(btv);
948
949 if (0 == (bits & VBI_RESOURCES))
950 disclaim_vbi_lines(btv);
951
952 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955/* ----------------------------------------------------------------------- */
956/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
957
958/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
959 PLL_X = Reference pre-divider (0=1, 1=2)
960 PLL_C = Post divider (0=6, 1=4)
961 PLL_I = Integer input
962 PLL_F = Fractional input
963
964 F_input = 28.636363 MHz:
965 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
966*/
967
968static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
969{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800970 unsigned char fl, fh, fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800972 /* prevent overflows */
973 fin/=4;
974 fout/=4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800976 fout*=12;
977 fi=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800979 fout=(fout%fin)*256;
980 fh=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800982 fout=(fout%fin)*256;
983 fl=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800985 btwrite(fl, BT848_PLL_F_LO);
986 btwrite(fh, BT848_PLL_F_HI);
987 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990static void set_pll(struct bttv *btv)
991{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800992 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800994 if (!btv->pll.pll_crystal)
995 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
998 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800999 return;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001002 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1003 /* no PLL needed */
1004 if (btv->pll.pll_current == 0)
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001005 return;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001006 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001007 btv->c.nr,btv->pll.pll_ifreq);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001008 btwrite(0x00,BT848_TGCTRL);
1009 btwrite(0x00,BT848_PLL_XCI);
1010 btv->pll.pll_current = 0;
1011 return;
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001014 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001015 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1017
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001018 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /* Let other people run while the PLL stabilizes */
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001020 bttv_printk(".");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 msleep(10);
1022
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001023 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 btwrite(0,BT848_DSTATUS);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001025 } else {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001026 btwrite(0x08,BT848_TGCTRL);
1027 btv->pll.pll_current = btv->pll.pll_ofreq;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001028 bttv_printk(" ok\n");
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001029 return;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001030 }
1031 }
1032 btv->pll.pll_current = -1;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001033 bttv_printk("failed\n");
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001034 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037/* used to switch between the bt848's analog/digital video capture modes */
1038static void bt848A_set_timing(struct bttv *btv)
1039{
1040 int i, len;
1041 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1042 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1043
Trent Piepho5221e212009-01-28 21:32:59 -03001044 if (btv->input == btv->dig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1046 btv->c.nr,table_idx);
1047
1048 /* timing change...reset timing generator address */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001049 btwrite(0x00, BT848_TGCTRL);
1050 btwrite(0x02, BT848_TGCTRL);
1051 btwrite(0x00, BT848_TGCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 len=SRAM_Table[table_idx][0];
1054 for(i = 1; i <= len; i++)
1055 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1056 btv->pll.pll_ofreq = 27000000;
1057
1058 set_pll(btv);
1059 btwrite(0x11, BT848_TGCTRL);
1060 btwrite(0x41, BT848_DVSIF);
1061 } else {
1062 btv->pll.pll_ofreq = fsc;
1063 set_pll(btv);
1064 btwrite(0x0, BT848_DVSIF);
1065 }
1066}
1067
1068/* ----------------------------------------------------------------------- */
1069
1070static void bt848_bright(struct bttv *btv, int bright)
1071{
1072 int value;
1073
1074 // printk("bttv: set bright: %d\n",bright); // DEBUG
1075 btv->bright = bright;
1076
1077 /* We want -128 to 127 we get 0-65535 */
1078 value = (bright >> 8) - 128;
1079 btwrite(value & 0xff, BT848_BRIGHT);
1080}
1081
1082static void bt848_hue(struct bttv *btv, int hue)
1083{
1084 int value;
1085
1086 btv->hue = hue;
1087
1088 /* -128 to 127 */
1089 value = (hue >> 8) - 128;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001090 btwrite(value & 0xff, BT848_HUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093static void bt848_contrast(struct bttv *btv, int cont)
1094{
1095 int value,hibit;
1096
1097 btv->contrast = cont;
1098
1099 /* 0-511 */
1100 value = (cont >> 7);
1101 hibit = (value >> 6) & 4;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001102 btwrite(value & 0xff, BT848_CONTRAST_LO);
1103 btaor(hibit, ~4, BT848_E_CONTROL);
1104 btaor(hibit, ~4, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107static void bt848_sat(struct bttv *btv, int color)
1108{
1109 int val_u,val_v,hibits;
1110
1111 btv->saturation = color;
1112
1113 /* 0-511 for the color */
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001114 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1115 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001116 hibits = (val_u >> 7) & 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 hibits |= (val_v >> 8) & 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001118 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1119 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1120 btaor(hibits, ~3, BT848_E_CONTROL);
1121 btaor(hibits, ~3, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
1124/* ----------------------------------------------------------------------- */
1125
1126static int
1127video_mux(struct bttv *btv, unsigned int input)
1128{
1129 int mux,mask2;
1130
1131 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1132 return -EINVAL;
1133
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001134 /* needed by RemoteVideo MX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1136 if (mask2)
1137 gpio_inout(mask2,mask2);
1138
1139 if (input == btv->svhs) {
1140 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1141 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1142 } else {
1143 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1144 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1145 }
Trent Piepho6f987002009-01-28 21:32:59 -03001146 mux = bttv_muxsel(btv, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1148 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1149 btv->c.nr,input,mux);
1150
1151 /* card specific hook */
1152 if(bttv_tvcards[btv->c.type].muxsel_hook)
1153 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1154 return 0;
1155}
1156
1157static char *audio_modes[] = {
1158 "audio: tuner", "audio: radio", "audio: extern",
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001159 "audio: intern", "audio: mute"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160};
1161
1162static int
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001163audio_mux(struct bttv *btv, int input, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001165 int gpio_val, signal;
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001166 struct v4l2_control ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1169 bttv_tvcards[btv->c.type].gpiomask);
1170 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1171
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001172 btv->mute = mute;
1173 btv->audio = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001175 /* automute */
1176 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1177
1178 if (mute)
1179 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1180 else
1181 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001182
Trent Piepho72134a62009-01-28 21:32:59 -03001183 switch (btv->c.type) {
1184 case BTTV_BOARD_VOODOOTV_FM:
1185 case BTTV_BOARD_VOODOOTV_200:
1186 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1187 break;
1188
1189 default:
1190 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1191 }
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (bttv_gpio)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001194 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1195 if (in_interrupt())
1196 return 0;
1197
1198 ctrl.id = V4L2_CID_AUDIO_MUTE;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001199 ctrl.value = btv->mute;
Hans Verkuil859f0272009-03-28 08:29:00 -03001200 bttv_call_all(btv, core, s_ctrl, &ctrl);
1201 if (btv->sd_msp34xx) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001202 u32 in;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001203
1204 /* Note: the inputs tuner/radio/extern/intern are translated
1205 to msp routings. This assumes common behavior for all msp3400
1206 based TV cards. When this assumption fails, then the
1207 specific MSP routing must be added to the card table.
1208 For now this is sufficient. */
1209 switch (input) {
1210 case TVAUDIO_INPUT_RADIO:
Hans Verkuil5325b422009-04-02 11:26:22 -03001211 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001212 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001213 break;
1214 case TVAUDIO_INPUT_EXTERN:
Hans Verkuil5325b422009-04-02 11:26:22 -03001215 in = MSP_INPUT(MSP_IN_SCART1, 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_INTERN:
1219 /* Yes, this is the same input as for RADIO. I doubt
1220 if this is ever used. The only board with an INTERN
1221 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1222 that was tested. My guess is that the whole INTERN
1223 input does not work. */
Hans Verkuil5325b422009-04-02 11:26:22 -03001224 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
Hans Verkuil07151722006-04-01 18:03:23 -03001225 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001226 break;
1227 case TVAUDIO_INPUT_TUNER:
1228 default:
Wade Berrier434b2522007-06-25 13:02:16 -03001229 /* This is the only card that uses TUNER2, and afaik,
1230 is the only difference between the VOODOOTV_FM
1231 and VOODOOTV_200 */
1232 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
Hans Verkuil5325b422009-04-02 11:26:22 -03001233 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
Wade Berrier434b2522007-06-25 13:02:16 -03001234 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1235 else
Hans Verkuil5325b422009-04-02 11:26:22 -03001236 in = MSP_INPUT_DEFAULT;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001237 break;
1238 }
Hans Verkuil5325b422009-04-02 11:26:22 -03001239 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1240 in, MSP_OUTPUT_DEFAULT, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001241 }
Hans Verkuil859f0272009-03-28 08:29:00 -03001242 if (btv->sd_tvaudio) {
Hans Verkuil5325b422009-04-02 11:26:22 -03001243 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1244 input, 0, 0);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return 0;
1247}
1248
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001249static inline int
1250audio_mute(struct bttv *btv, int mute)
1251{
1252 return audio_mux(btv, btv->audio, mute);
1253}
1254
1255static inline int
1256audio_input(struct bttv *btv, int input)
1257{
1258 return audio_mux(btv, input, btv->mute);
1259}
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261static void
Michael Schimeke5bd0262007-01-18 16:17:39 -03001262bttv_crop_calc_limits(struct bttv_crop *c)
1263{
1264 /* Scale factor min. 1:1, max. 16:1. Min. image size
1265 48 x 32. Scaled width must be a multiple of 4. */
1266
1267 if (1) {
1268 /* For bug compatibility with VIDIOCGCAP and image
1269 size checks in earlier driver versions. */
1270 c->min_scaled_width = 48;
1271 c->min_scaled_height = 32;
1272 } else {
1273 c->min_scaled_width =
1274 (max(48, c->rect.width >> 4) + 3) & ~3;
1275 c->min_scaled_height =
1276 max(32, c->rect.height >> 4);
1277 }
1278
1279 c->max_scaled_width = c->rect.width & ~3;
1280 c->max_scaled_height = c->rect.height;
1281}
1282
1283static void
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001284bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
Michael Schimeke5bd0262007-01-18 16:17:39 -03001285{
1286 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1287 bttv_crop_calc_limits(c);
1288}
1289
1290/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291static int
1292set_tvnorm(struct bttv *btv, unsigned int norm)
1293{
1294 const struct bttv_tvnorm *tvnorm;
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001295 v4l2_std_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001297 BUG_ON(norm >= BTTV_TVNORMS);
1298 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 tvnorm = &bttv_tvnorms[norm];
1301
Mike Isely2de26c02009-09-21 12:42:22 -03001302 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
Trent Piepho4ef2ccc2009-01-28 21:32:58 -03001303 sizeof (tvnorm->cropcap))) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03001304 bttv_crop_reset(&btv->crop[0], norm);
1305 btv->crop[1] = btv->crop[0]; /* current = default */
1306
1307 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1308 btv->crop_start = tvnorm->cropcap.bounds.top
1309 + tvnorm->cropcap.bounds.height;
1310 }
1311 }
1312
1313 btv->tvnorm = norm;
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 btwrite(tvnorm->adelay, BT848_ADELAY);
1316 btwrite(tvnorm->bdelay, BT848_BDELAY);
1317 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1318 BT848_IFORM);
1319 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1320 btwrite(1, BT848_VBI_PACK_DEL);
1321 bt848A_set_timing(btv);
1322
1323 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -08001324 case BTTV_BOARD_VOODOOTV_FM:
Wade Berrier434b2522007-06-25 13:02:16 -03001325 case BTTV_BOARD_VOODOOTV_200:
Trent Piepho72134a62009-01-28 21:32:59 -03001326 bttv_tda9880_setnorm(btv, gpio_read());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001329 id = tvnorm->v4l2_id;
Hans Verkuilf41737e2009-04-01 03:52:39 -03001330 bttv_call_all(btv, core, s_std, id);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return 0;
1333}
1334
Michael Schimeke5bd0262007-01-18 16:17:39 -03001335/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336static void
Trent Piepho333408f2007-07-03 15:08:10 -03001337set_input(struct bttv *btv, unsigned int input, unsigned int norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 unsigned long flags;
1340
1341 btv->input = input;
1342 if (irq_iswitch) {
1343 spin_lock_irqsave(&btv->s_lock,flags);
1344 if (btv->curr.frame_irq) {
1345 /* active capture -> delayed input switch */
1346 btv->new_input = input;
1347 } else {
1348 video_mux(btv,input);
1349 }
1350 spin_unlock_irqrestore(&btv->s_lock,flags);
1351 } else {
1352 video_mux(btv,input);
1353 }
Trent Piephoabb03622009-01-28 21:32:59 -03001354 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1355 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
Trent Piepho333408f2007-07-03 15:08:10 -03001356 set_tvnorm(btv, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
1359static void init_irqreg(struct bttv *btv)
1360{
1361 /* clear status */
1362 btwrite(0xfffffUL, BT848_INT_STAT);
1363
1364 if (bttv_tvcards[btv->c.type].no_video) {
1365 /* i2c only */
1366 btwrite(BT848_INT_I2CDONE,
1367 BT848_INT_MASK);
1368 } else {
1369 /* full video */
1370 btwrite((btv->triton1) |
1371 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1372 BT848_INT_SCERR |
1373 (fdsr ? BT848_INT_FDSR : 0) |
Jean Delvareeb1b27b2008-08-30 10:18:21 -03001374 BT848_INT_RISCI | BT848_INT_OCERR |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1376 BT848_INT_I2CDONE,
1377 BT848_INT_MASK);
1378 }
1379}
1380
1381static void init_bt848(struct bttv *btv)
1382{
1383 int val;
1384
1385 if (bttv_tvcards[btv->c.type].no_video) {
1386 /* very basic init only */
1387 init_irqreg(btv);
1388 return;
1389 }
1390
1391 btwrite(0x00, BT848_CAP_CTL);
1392 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1393 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1394
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001395 /* set planar and packed mode trigger points and */
1396 /* set rising edge of inverted GPINTR pin as irq trigger */
1397 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1398 BT848_GPIO_DMA_CTL_PLTP1_16|
1399 BT848_GPIO_DMA_CTL_PLTP23_16|
1400 BT848_GPIO_DMA_CTL_GPINTC|
1401 BT848_GPIO_DMA_CTL_GPINTI,
1402 BT848_GPIO_DMA_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001405 btwrite(val, BT848_E_SCLOOP);
1406 btwrite(val, BT848_O_SCLOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001408 btwrite(0x20, BT848_E_VSCALE_HI);
1409 btwrite(0x20, BT848_O_VSCALE_HI);
1410 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 BT848_ADC);
1412
1413 btwrite(whitecrush_upper, BT848_WC_UP);
1414 btwrite(whitecrush_lower, BT848_WC_DOWN);
1415
1416 if (btv->opt_lumafilter) {
1417 btwrite(0, BT848_E_CONTROL);
1418 btwrite(0, BT848_O_CONTROL);
1419 } else {
1420 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1421 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1422 }
1423
1424 bt848_bright(btv, btv->bright);
1425 bt848_hue(btv, btv->hue);
1426 bt848_contrast(btv, btv->contrast);
1427 bt848_sat(btv, btv->saturation);
1428
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001429 /* interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 init_irqreg(btv);
1431}
1432
1433static void bttv_reinit_bt848(struct bttv *btv)
1434{
1435 unsigned long flags;
1436
1437 if (bttv_verbose)
1438 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1439 spin_lock_irqsave(&btv->s_lock,flags);
1440 btv->errors=0;
1441 bttv_set_dma(btv,0);
1442 spin_unlock_irqrestore(&btv->s_lock,flags);
1443
1444 init_bt848(btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001445 btv->pll.pll_current = -1;
Trent Piepho333408f2007-07-03 15:08:10 -03001446 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001449static int bttv_g_ctrl(struct file *file, void *priv,
1450 struct v4l2_control *c)
1451{
1452 struct bttv_fh *fh = priv;
1453 struct bttv *btv = fh->btv;
1454
1455 switch (c->id) {
1456 case V4L2_CID_BRIGHTNESS:
1457 c->value = btv->bright;
1458 break;
1459 case V4L2_CID_HUE:
1460 c->value = btv->hue;
1461 break;
1462 case V4L2_CID_CONTRAST:
1463 c->value = btv->contrast;
1464 break;
1465 case V4L2_CID_SATURATION:
1466 c->value = btv->saturation;
1467 break;
1468
1469 case V4L2_CID_AUDIO_MUTE:
1470 case V4L2_CID_AUDIO_VOLUME:
1471 case V4L2_CID_AUDIO_BALANCE:
1472 case V4L2_CID_AUDIO_BASS:
1473 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil859f0272009-03-28 08:29:00 -03001474 bttv_call_all(btv, core, g_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001475 break;
1476
1477 case V4L2_CID_PRIVATE_CHROMA_AGC:
1478 c->value = btv->opt_chroma_agc;
1479 break;
1480 case V4L2_CID_PRIVATE_COMBFILTER:
1481 c->value = btv->opt_combfilter;
1482 break;
1483 case V4L2_CID_PRIVATE_LUMAFILTER:
1484 c->value = btv->opt_lumafilter;
1485 break;
1486 case V4L2_CID_PRIVATE_AUTOMUTE:
1487 c->value = btv->opt_automute;
1488 break;
1489 case V4L2_CID_PRIVATE_AGC_CRUSH:
1490 c->value = btv->opt_adc_crush;
1491 break;
1492 case V4L2_CID_PRIVATE_VCR_HACK:
1493 c->value = btv->opt_vcr_hack;
1494 break;
1495 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1496 c->value = btv->opt_whitecrush_upper;
1497 break;
1498 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1499 c->value = btv->opt_whitecrush_lower;
1500 break;
1501 case V4L2_CID_PRIVATE_UV_RATIO:
1502 c->value = btv->opt_uv_ratio;
1503 break;
1504 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1505 c->value = btv->opt_full_luma_range;
1506 break;
1507 case V4L2_CID_PRIVATE_CORING:
1508 c->value = btv->opt_coring;
1509 break;
1510 default:
1511 return -EINVAL;
1512 }
1513 return 0;
1514}
1515
1516static int bttv_s_ctrl(struct file *file, void *f,
1517 struct v4l2_control *c)
1518{
1519 int err;
1520 int val;
1521 struct bttv_fh *fh = f;
1522 struct bttv *btv = fh->btv;
1523
1524 err = v4l2_prio_check(&btv->prio, &fh->prio);
1525 if (0 != err)
1526 return err;
1527
1528 switch (c->id) {
1529 case V4L2_CID_BRIGHTNESS:
1530 bt848_bright(btv, c->value);
1531 break;
1532 case V4L2_CID_HUE:
1533 bt848_hue(btv, c->value);
1534 break;
1535 case V4L2_CID_CONTRAST:
1536 bt848_contrast(btv, c->value);
1537 break;
1538 case V4L2_CID_SATURATION:
1539 bt848_sat(btv, c->value);
1540 break;
1541 case V4L2_CID_AUDIO_MUTE:
1542 audio_mute(btv, c->value);
1543 /* fall through */
1544 case V4L2_CID_AUDIO_VOLUME:
1545 if (btv->volume_gpio)
1546 btv->volume_gpio(btv, c->value);
1547
Hans Verkuil859f0272009-03-28 08:29:00 -03001548 bttv_call_all(btv, core, s_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001549 break;
1550 case V4L2_CID_AUDIO_BALANCE:
1551 case V4L2_CID_AUDIO_BASS:
1552 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil859f0272009-03-28 08:29:00 -03001553 bttv_call_all(btv, core, s_ctrl, c);
Mauro Carvalho Chehab04a94d32007-12-27 22:23:34 -03001554 break;
1555
1556 case V4L2_CID_PRIVATE_CHROMA_AGC:
1557 btv->opt_chroma_agc = c->value;
1558 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1559 btwrite(val, BT848_E_SCLOOP);
1560 btwrite(val, BT848_O_SCLOOP);
1561 break;
1562 case V4L2_CID_PRIVATE_COMBFILTER:
1563 btv->opt_combfilter = c->value;
1564 break;
1565 case V4L2_CID_PRIVATE_LUMAFILTER:
1566 btv->opt_lumafilter = c->value;
1567 if (btv->opt_lumafilter) {
1568 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1569 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1570 } else {
1571 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1572 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1573 }
1574 break;
1575 case V4L2_CID_PRIVATE_AUTOMUTE:
1576 btv->opt_automute = c->value;
1577 break;
1578 case V4L2_CID_PRIVATE_AGC_CRUSH:
1579 btv->opt_adc_crush = c->value;
1580 btwrite(BT848_ADC_RESERVED |
1581 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1582 BT848_ADC);
1583 break;
1584 case V4L2_CID_PRIVATE_VCR_HACK:
1585 btv->opt_vcr_hack = c->value;
1586 break;
1587 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1588 btv->opt_whitecrush_upper = c->value;
1589 btwrite(c->value, BT848_WC_UP);
1590 break;
1591 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1592 btv->opt_whitecrush_lower = c->value;
1593 btwrite(c->value, BT848_WC_DOWN);
1594 break;
1595 case V4L2_CID_PRIVATE_UV_RATIO:
1596 btv->opt_uv_ratio = c->value;
1597 bt848_sat(btv, btv->saturation);
1598 break;
1599 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1600 btv->opt_full_luma_range = c->value;
1601 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1602 break;
1603 case V4L2_CID_PRIVATE_CORING:
1604 btv->opt_coring = c->value;
1605 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1606 break;
1607 default:
1608 return -EINVAL;
1609 }
1610 return 0;
1611}
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613/* ----------------------------------------------------------------------- */
1614
1615void bttv_gpio_tracking(struct bttv *btv, char *comment)
1616{
1617 unsigned int outbits, data;
1618 outbits = btread(BT848_GPIO_OUT_EN);
1619 data = btread(BT848_GPIO_DATA);
1620 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1621 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1622}
1623
1624static void bttv_field_count(struct bttv *btv)
1625{
1626 int need_count = 0;
1627
1628 if (btv->users)
1629 need_count++;
1630
1631 if (need_count) {
1632 /* start field counter */
1633 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1634 } else {
1635 /* stop field counter */
1636 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1637 btv->field_count = 0;
1638 }
1639}
1640
1641static const struct bttv_format*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642format_by_fourcc(int fourcc)
1643{
1644 unsigned int i;
1645
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001646 for (i = 0; i < FORMATS; i++) {
1647 if (-1 == formats[i].fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 continue;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001649 if (formats[i].fourcc == fourcc)
1650 return formats+i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652 return NULL;
1653}
1654
1655/* ----------------------------------------------------------------------- */
1656/* misc helpers */
1657
1658static int
1659bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1660 struct bttv_buffer *new)
1661{
1662 struct bttv_buffer *old;
1663 unsigned long flags;
1664 int retval = 0;
1665
1666 dprintk("switch_overlay: enter [new=%p]\n",new);
1667 if (new)
Brandon Philips0fc06862007-11-06 20:02:36 -03001668 new->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 spin_lock_irqsave(&btv->s_lock,flags);
1670 old = btv->screen;
1671 btv->screen = new;
1672 btv->loop_irq |= 1;
1673 bttv_set_dma(btv, 0x03);
1674 spin_unlock_irqrestore(&btv->s_lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (NULL != old) {
1676 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001677 bttv_dma_free(&fh->cap,btv, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 kfree(old);
1679 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03001680 if (NULL == new)
1681 free_btres(btv,fh,RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 dprintk("switch_overlay: done\n");
1683 return retval;
1684}
1685
1686/* ----------------------------------------------------------------------- */
1687/* video4linux (1) interface */
1688
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001689static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1690 struct bttv_buffer *buf,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001691 const struct bttv_format *fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 unsigned int width, unsigned int height,
1693 enum v4l2_field field)
1694{
Michael Schimeke5bd0262007-01-18 16:17:39 -03001695 struct bttv_fh *fh = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 int redo_dma_risc = 0;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001697 struct bttv_crop c;
1698 int norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 int rc;
1700
1701 /* check settings */
1702 if (NULL == fmt)
1703 return -EINVAL;
1704 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1705 width = RAW_BPL;
1706 height = RAW_LINES*2;
1707 if (width*height > buf->vb.bsize)
1708 return -EINVAL;
1709 buf->vb.size = buf->vb.bsize;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001710
1711 /* Make sure tvnorm and vbi_end remain consistent
1712 until we're done. */
1713 mutex_lock(&btv->lock);
1714
1715 norm = btv->tvnorm;
1716
1717 /* In this mode capturing always starts at defrect.top
1718 (default VDELAY), ignoring cropping parameters. */
1719 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1720 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001722 }
1723
1724 mutex_unlock(&btv->lock);
1725
1726 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1727 } else {
1728 mutex_lock(&btv->lock);
1729
1730 norm = btv->tvnorm;
1731 c = btv->crop[!!fh->do_crop];
1732
1733 mutex_unlock(&btv->lock);
1734
1735 if (width < c.min_scaled_width ||
1736 width > c.max_scaled_width ||
1737 height < c.min_scaled_height)
1738 return -EINVAL;
1739
1740 switch (field) {
1741 case V4L2_FIELD_TOP:
1742 case V4L2_FIELD_BOTTOM:
1743 case V4L2_FIELD_ALTERNATE:
1744 /* btv->crop counts frame lines. Max. scale
1745 factor is 16:1 for frames, 8:1 for fields. */
1746 if (height * 2 > c.max_scaled_height)
1747 return -EINVAL;
1748 break;
1749
1750 default:
1751 if (height > c.max_scaled_height)
1752 return -EINVAL;
1753 break;
1754 }
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 buf->vb.size = (width * height * fmt->depth) >> 3;
1757 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1758 return -EINVAL;
1759 }
1760
1761 /* alloc + fill struct bttv_buffer (if changed) */
1762 if (buf->vb.width != width || buf->vb.height != height ||
1763 buf->vb.field != field ||
Michael Schimeke5bd0262007-01-18 16:17:39 -03001764 buf->tvnorm != norm || buf->fmt != fmt ||
1765 buf->crop.top != c.rect.top ||
1766 buf->crop.left != c.rect.left ||
1767 buf->crop.width != c.rect.width ||
1768 buf->crop.height != c.rect.height) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 buf->vb.width = width;
1770 buf->vb.height = height;
1771 buf->vb.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001772 buf->tvnorm = norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 buf->fmt = fmt;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001774 buf->crop = c.rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 redo_dma_risc = 1;
1776 }
1777
1778 /* alloc risc memory */
Brandon Philips0fc06862007-11-06 20:02:36 -03001779 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 redo_dma_risc = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001781 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 goto fail;
1783 }
1784
1785 if (redo_dma_risc)
1786 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1787 goto fail;
1788
Brandon Philips0fc06862007-11-06 20:02:36 -03001789 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
1791
1792 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001793 bttv_dma_free(q,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return rc;
1795}
1796
1797static int
1798buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1799{
1800 struct bttv_fh *fh = q->priv_data;
1801
1802 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1803 if (0 == *count)
1804 *count = gbuffers;
1805 while (*size * *count > gbuffers * gbufsize)
1806 (*count)--;
1807 return 0;
1808}
1809
1810static int
1811buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1812 enum v4l2_field field)
1813{
1814 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1815 struct bttv_fh *fh = q->priv_data;
1816
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001817 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 fh->width, fh->height, field);
1819}
1820
1821static void
1822buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1823{
1824 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1825 struct bttv_fh *fh = q->priv_data;
1826 struct bttv *btv = fh->btv;
1827
Brandon Philips0fc06862007-11-06 20:02:36 -03001828 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 list_add_tail(&buf->vb.queue,&btv->capture);
1830 if (!btv->curr.frame_irq) {
1831 btv->loop_irq |= 1;
1832 bttv_set_dma(btv, 0x03);
1833 }
1834}
1835
1836static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1837{
1838 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1839 struct bttv_fh *fh = q->priv_data;
1840
Michael Schimekfeaba7a2007-01-26 08:30:05 -03001841 bttv_dma_free(q,fh->btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844static struct videobuf_queue_ops bttv_video_qops = {
1845 .buf_setup = buffer_setup,
1846 .buf_prepare = buffer_prepare,
1847 .buf_queue = buffer_queue,
1848 .buf_release = buffer_release,
1849};
1850
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001851static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001853 struct bttv_fh *fh = priv;
1854 struct bttv *btv = fh->btv;
1855 unsigned int i;
1856 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001858 err = v4l2_prio_check(&btv->prio, &fh->prio);
1859 if (0 != err)
1860 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001862 for (i = 0; i < BTTV_TVNORMS; i++)
1863 if (*id & bttv_tvnorms[i].v4l2_id)
1864 break;
1865 if (i == BTTV_TVNORMS)
1866 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001868 mutex_lock(&btv->lock);
1869 set_tvnorm(btv, i);
1870 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001872 return 0;
1873}
1874
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001875static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001876{
1877 struct bttv_fh *fh = f;
1878 struct bttv *btv = fh->btv;
1879
1880 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1881 *id = V4L2_STD_625_50;
1882 else
1883 *id = V4L2_STD_525_60;
1884 return 0;
1885}
1886
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001887static int bttv_enum_input(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001888 struct v4l2_input *i)
1889{
1890 struct bttv_fh *fh = priv;
1891 struct bttv *btv = fh->btv;
Trent Piepho522a5f12009-03-04 01:21:03 -03001892 int n;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001893
Trent Piepho522a5f12009-03-04 01:21:03 -03001894 if (i->index >= bttv_tvcards[btv->c.type].video_inputs)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001895 return -EINVAL;
1896
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001897 i->type = V4L2_INPUT_TYPE_CAMERA;
1898 i->audioset = 1;
1899
Trent Piephoabb03622009-01-28 21:32:59 -03001900 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001901 sprintf(i->name, "Television");
1902 i->type = V4L2_INPUT_TYPE_TUNER;
1903 i->tuner = 0;
1904 } else if (i->index == btv->svhs) {
1905 sprintf(i->name, "S-Video");
1906 } else {
1907 sprintf(i->name, "Composite%d", i->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001910 if (i->index == btv->input) {
1911 __u32 dstatus = btread(BT848_DSTATUS);
1912 if (0 == (dstatus & BT848_DSTATUS_PRES))
1913 i->status |= V4L2_IN_ST_NO_SIGNAL;
1914 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1915 i->status |= V4L2_IN_ST_NO_H_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001918 for (n = 0; n < BTTV_TVNORMS; n++)
1919 i->std |= bttv_tvnorms[n].v4l2_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001921 return 0;
1922}
Nickolay V. Shmyrev4b9b9362006-08-25 16:53:04 -03001923
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001924static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001925{
1926 struct bttv_fh *fh = priv;
1927 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001929 *i = btv->input;
1930 return 0;
1931}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001933static int bttv_s_input(struct file *file, void *priv, unsigned int i)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001934{
1935 struct bttv_fh *fh = priv;
1936 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001938 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001940 err = v4l2_prio_check(&btv->prio, &fh->prio);
1941 if (0 != err)
1942 return err;
1943
1944 if (i > bttv_tvcards[btv->c.type].video_inputs)
1945 return -EINVAL;
1946
1947 mutex_lock(&btv->lock);
1948 set_input(btv, i, btv->tvnorm);
1949 mutex_unlock(&btv->lock);
1950 return 0;
1951}
1952
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001953static int bttv_s_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001954 struct v4l2_tuner *t)
1955{
1956 struct bttv_fh *fh = priv;
1957 struct bttv *btv = fh->btv;
1958 int err;
1959
1960 err = v4l2_prio_check(&btv->prio, &fh->prio);
1961 if (0 != err)
1962 return err;
1963
Trent Piephoabb03622009-01-28 21:32:59 -03001964 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001965 return -EINVAL;
1966
1967 if (0 != t->index)
1968 return -EINVAL;
1969
1970 mutex_lock(&btv->lock);
Hans Verkuil859f0272009-03-28 08:29:00 -03001971 bttv_call_all(btv, tuner, s_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001972
1973 if (btv->audio_mode_gpio)
1974 btv->audio_mode_gpio(btv, t, 1);
1975
1976 mutex_unlock(&btv->lock);
1977
1978 return 0;
1979}
1980
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001981static int bttv_g_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001982 struct v4l2_frequency *f)
1983{
1984 struct bttv_fh *fh = priv;
1985 struct bttv *btv = fh->btv;
1986 int err;
1987
1988 err = v4l2_prio_check(&btv->prio, &fh->prio);
1989 if (0 != err)
1990 return err;
1991
Robert Fitzsimons1b069012008-04-01 11:41:54 -03001992 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001993 f->frequency = btv->freq;
1994
1995 return 0;
1996}
1997
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03001998static int bttv_s_frequency(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03001999 struct v4l2_frequency *f)
2000{
2001 struct bttv_fh *fh = priv;
2002 struct bttv *btv = fh->btv;
2003 int err;
2004
2005 err = v4l2_prio_check(&btv->prio, &fh->prio);
2006 if (0 != err)
2007 return err;
2008
2009 if (unlikely(f->tuner != 0))
2010 return -EINVAL;
Robert Fitzsimons1b069012008-04-01 11:41:54 -03002011 if (unlikely(f->type != (btv->radio_user
2012 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV)))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002013 return -EINVAL;
2014 mutex_lock(&btv->lock);
2015 btv->freq = f->frequency;
Hans Verkuil859f0272009-03-28 08:29:00 -03002016 bttv_call_all(btv, tuner, s_frequency, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002017 if (btv->has_matchbox && btv->radio_user)
2018 tea5757_set_freq(btv, btv->freq);
2019 mutex_unlock(&btv->lock);
2020 return 0;
2021}
2022
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002023static int bttv_log_status(struct file *file, void *f)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002024{
2025 struct bttv_fh *fh = f;
2026 struct bttv *btv = fh->btv;
2027
2028 printk(KERN_INFO "bttv%d: ======== START STATUS CARD #%d ========\n",
2029 btv->c.nr, btv->c.nr);
Hans Verkuil859f0272009-03-28 08:29:00 -03002030 bttv_call_all(btv, core, log_status);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002031 printk(KERN_INFO "bttv%d: ======== END STATUS CARD #%d ========\n",
2032 btv->c.nr, btv->c.nr);
2033 return 0;
2034}
2035
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002036#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002037static int bttv_g_register(struct file *file, void *f,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002038 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002039{
2040 struct bttv_fh *fh = f;
2041 struct bttv *btv = fh->btv;
2042
2043 if (!capable(CAP_SYS_ADMIN))
2044 return -EPERM;
2045
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002046 if (!v4l2_chip_match_host(&reg->match))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002047 return -EINVAL;
2048
2049 /* bt848 has a 12-bit register space */
2050 reg->reg &= 0xfff;
2051 reg->val = btread(reg->reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002052 reg->size = 1;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002053
2054 return 0;
2055}
2056
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002057static int bttv_s_register(struct file *file, void *f,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002058 struct v4l2_dbg_register *reg)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002059{
2060 struct bttv_fh *fh = f;
2061 struct bttv *btv = fh->btv;
2062
2063 if (!capable(CAP_SYS_ADMIN))
2064 return -EPERM;
2065
Hans Verkuilaecde8b2008-12-30 07:14:19 -03002066 if (!v4l2_chip_match_host(&reg->match))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002067 return -EINVAL;
2068
2069 /* bt848 has a 12-bit register space */
2070 reg->reg &= 0xfff;
2071 btwrite(reg->val, reg->reg);
2072
2073 return 0;
2074}
2075#endif
2076
Michael Schimeke5bd0262007-01-18 16:17:39 -03002077/* Given cropping boundaries b and the scaled width and height of a
2078 single field or frame, which must not exceed hardware limits, this
2079 function adjusts the cropping parameters c. */
2080static void
2081bttv_crop_adjust (struct bttv_crop * c,
2082 const struct v4l2_rect * b,
2083 __s32 width,
2084 __s32 height,
2085 enum v4l2_field field)
2086{
2087 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2088 __s32 max_left;
2089 __s32 max_top;
2090
2091 if (width < c->min_scaled_width) {
2092 /* Max. hor. scale factor 16:1. */
2093 c->rect.width = width * 16;
2094 } else if (width > c->max_scaled_width) {
2095 /* Min. hor. scale factor 1:1. */
2096 c->rect.width = width;
2097
2098 max_left = b->left + b->width - width;
2099 max_left = min(max_left, (__s32) MAX_HDELAY);
2100 if (c->rect.left > max_left)
2101 c->rect.left = max_left;
2102 }
2103
2104 if (height < c->min_scaled_height) {
2105 /* Max. vert. scale factor 16:1, single fields 8:1. */
2106 c->rect.height = height * 16;
2107 } else if (frame_height > c->max_scaled_height) {
2108 /* Min. vert. scale factor 1:1.
2109 Top and height count field lines times two. */
2110 c->rect.height = (frame_height + 1) & ~1;
2111
2112 max_top = b->top + b->height - c->rect.height;
2113 if (c->rect.top > max_top)
2114 c->rect.top = max_top;
2115 }
2116
2117 bttv_crop_calc_limits(c);
2118}
2119
2120/* Returns an error if scaling to a frame or single field with the given
2121 width and height is not possible with the current cropping parameters
2122 and width aligned according to width_mask. If adjust_size is TRUE the
2123 function may adjust the width and/or height instead, rounding width
2124 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2125 also adjust the current cropping parameters to get closer to the
2126 desired image size. */
2127static int
2128limit_scaled_size (struct bttv_fh * fh,
2129 __s32 * width,
2130 __s32 * height,
2131 enum v4l2_field field,
2132 unsigned int width_mask,
2133 unsigned int width_bias,
2134 int adjust_size,
2135 int adjust_crop)
2136{
2137 struct bttv *btv = fh->btv;
2138 const struct v4l2_rect *b;
2139 struct bttv_crop *c;
2140 __s32 min_width;
2141 __s32 min_height;
2142 __s32 max_width;
2143 __s32 max_height;
2144 int rc;
2145
2146 BUG_ON((int) width_mask >= 0 ||
2147 width_bias >= (unsigned int) -width_mask);
2148
2149 /* Make sure tvnorm, vbi_end and the current cropping parameters
2150 remain consistent until we're done. */
2151 mutex_lock(&btv->lock);
2152
2153 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2154
2155 /* Do crop - use current, don't - use default parameters. */
2156 c = &btv->crop[!!fh->do_crop];
2157
2158 if (fh->do_crop
2159 && adjust_size
2160 && adjust_crop
2161 && !locked_btres(btv, VIDEO_RESOURCES)) {
2162 min_width = 48;
2163 min_height = 32;
2164
2165 /* We cannot scale up. When the scaled image is larger
2166 than crop.rect we adjust the crop.rect as required
2167 by the V4L2 spec, hence cropcap.bounds are our limit. */
2168 max_width = min(b->width, (__s32) MAX_HACTIVE);
2169 max_height = b->height;
2170
2171 /* We cannot capture the same line as video and VBI data.
2172 Note btv->vbi_end is really a minimum, see
2173 bttv_vbi_try_fmt(). */
2174 if (btv->vbi_end > b->top) {
2175 max_height -= btv->vbi_end - b->top;
2176 rc = -EBUSY;
2177 if (min_height > max_height)
2178 goto fail;
2179 }
2180 } else {
2181 rc = -EBUSY;
2182 if (btv->vbi_end > c->rect.top)
2183 goto fail;
2184
2185 min_width = c->min_scaled_width;
2186 min_height = c->min_scaled_height;
2187 max_width = c->max_scaled_width;
2188 max_height = c->max_scaled_height;
2189
2190 adjust_crop = 0;
2191 }
2192
2193 min_width = (min_width - width_mask - 1) & width_mask;
2194 max_width = max_width & width_mask;
2195
2196 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2197 min_height = min_height;
2198 /* Min. scale factor is 1:1. */
2199 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2200
2201 if (adjust_size) {
2202 *width = clamp(*width, min_width, max_width);
2203 *height = clamp(*height, min_height, max_height);
2204
2205 /* Round after clamping to avoid overflow. */
2206 *width = (*width + width_bias) & width_mask;
2207
2208 if (adjust_crop) {
2209 bttv_crop_adjust(c, b, *width, *height, field);
2210
2211 if (btv->vbi_end > c->rect.top) {
2212 /* Move the crop window out of the way. */
2213 c->rect.top = btv->vbi_end;
2214 }
2215 }
2216 } else {
2217 rc = -EINVAL;
2218 if (*width < min_width ||
2219 *height < min_height ||
2220 *width > max_width ||
2221 *height > max_height ||
2222 0 != (*width & ~width_mask))
2223 goto fail;
2224 }
2225
2226 rc = 0; /* success */
2227
2228 fail:
2229 mutex_unlock(&btv->lock);
2230
2231 return rc;
2232}
2233
2234/* Returns an error if the given overlay window dimensions are not
2235 possible with the current cropping parameters. If adjust_size is
2236 TRUE the function may adjust the window width and/or height
2237 instead, however it always rounds the horizontal position and
2238 width as btcx_align() does. If adjust_crop is TRUE the function
2239 may also adjust the current cropping parameters to get closer
2240 to the desired window size. */
2241static int
2242verify_window (struct bttv_fh * fh,
2243 struct v4l2_window * win,
2244 int adjust_size,
2245 int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
2247 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002248 unsigned int width_mask;
2249 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 if (win->w.width < 48 || win->w.height < 32)
2252 return -EINVAL;
2253 if (win->clipcount > 2048)
2254 return -EINVAL;
2255
2256 field = win->field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 if (V4L2_FIELD_ANY == field) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002259 __s32 height2;
2260
2261 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2262 field = (win->w.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 ? V4L2_FIELD_INTERLACED
2264 : V4L2_FIELD_TOP;
2265 }
2266 switch (field) {
2267 case V4L2_FIELD_TOP:
2268 case V4L2_FIELD_BOTTOM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 case V4L2_FIELD_INTERLACED:
2270 break;
2271 default:
2272 return -EINVAL;
2273 }
2274
Michael Schimeke5bd0262007-01-18 16:17:39 -03002275 /* 4-byte alignment. */
2276 if (NULL == fh->ovfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002278 width_mask = ~0;
2279 switch (fh->ovfmt->depth) {
2280 case 8:
2281 case 24:
2282 width_mask = ~3;
2283 break;
2284 case 16:
2285 width_mask = ~1;
2286 break;
2287 case 32:
2288 break;
2289 default:
2290 BUG();
2291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Michael Schimeke5bd0262007-01-18 16:17:39 -03002293 win->w.width -= win->w.left & ~width_mask;
2294 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2295
2296 rc = limit_scaled_size(fh, &win->w.width, &win->w.height,
2297 field, width_mask,
2298 /* width_bias: round down */ 0,
2299 adjust_size, adjust_crop);
2300 if (0 != rc)
2301 return rc;
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 win->field = field;
2304 return 0;
2305}
2306
2307static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2308 struct v4l2_window *win, int fixup)
2309{
2310 struct v4l2_clip *clips = NULL;
2311 int n,size,retval = 0;
2312
2313 if (NULL == fh->ovfmt)
2314 return -EINVAL;
2315 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2316 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002317 retval = verify_window(fh, win,
2318 /* adjust_size */ fixup,
2319 /* adjust_crop */ fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (0 != retval)
2321 return retval;
2322
2323 /* copy clips -- luckily v4l1 + v4l2 are binary
2324 compatible here ...*/
2325 n = win->clipcount;
2326 size = sizeof(*clips)*(n+4);
2327 clips = kmalloc(size,GFP_KERNEL);
2328 if (NULL == clips)
2329 return -ENOMEM;
2330 if (n > 0) {
2331 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2332 kfree(clips);
2333 return -EFAULT;
2334 }
2335 }
2336 /* clip against screen */
2337 if (NULL != btv->fbuf.base)
2338 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2339 &win->w, clips, n);
2340 btcx_sort_clips(clips,n);
2341
2342 /* 4-byte alignments */
2343 switch (fh->ovfmt->depth) {
2344 case 8:
2345 case 24:
2346 btcx_align(&win->w, clips, n, 3);
2347 break;
2348 case 16:
2349 btcx_align(&win->w, clips, n, 1);
2350 break;
2351 case 32:
2352 /* no alignment fixups needed */
2353 break;
2354 default:
2355 BUG();
2356 }
2357
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002358 mutex_lock(&fh->cap.vb_lock);
2359 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 fh->ov.clips = clips;
2361 fh->ov.nclips = n;
2362
2363 fh->ov.w = win->w;
2364 fh->ov.field = win->field;
2365 fh->ov.setup_ok = 1;
2366 btv->init.ov.w.width = win->w.width;
2367 btv->init.ov.w.height = win->w.height;
2368 btv->init.ov.field = win->field;
2369
2370 /* update overlay if needed */
2371 retval = 0;
2372 if (check_btres(fh, RESOURCE_OVERLAY)) {
2373 struct bttv_buffer *new;
2374
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002375 new = videobuf_sg_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03002376 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2378 retval = bttv_switch_overlay(btv,fh,new);
2379 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002380 mutex_unlock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return retval;
2382}
2383
2384/* ----------------------------------------------------------------------- */
2385
2386static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2387{
2388 struct videobuf_queue* q = NULL;
2389
2390 switch (fh->type) {
2391 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2392 q = &fh->cap;
2393 break;
2394 case V4L2_BUF_TYPE_VBI_CAPTURE:
2395 q = &fh->vbi;
2396 break;
2397 default:
2398 BUG();
2399 }
2400 return q;
2401}
2402
2403static int bttv_resource(struct bttv_fh *fh)
2404{
2405 int res = 0;
2406
2407 switch (fh->type) {
2408 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002409 res = RESOURCE_VIDEO_STREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 break;
2411 case V4L2_BUF_TYPE_VBI_CAPTURE:
2412 res = RESOURCE_VBI;
2413 break;
2414 default:
2415 BUG();
2416 }
2417 return res;
2418}
2419
2420static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2421{
2422 struct videobuf_queue *q = bttv_queue(fh);
2423 int res = bttv_resource(fh);
2424
2425 if (check_btres(fh,res))
2426 return -EBUSY;
2427 if (videobuf_queue_is_busy(q))
2428 return -EBUSY;
2429 fh->type = type;
2430 return 0;
2431}
2432
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002433static void
2434pix_format_set_size (struct v4l2_pix_format * f,
2435 const struct bttv_format * fmt,
2436 unsigned int width,
2437 unsigned int height)
2438{
2439 f->width = width;
2440 f->height = height;
2441
2442 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2443 f->bytesperline = width; /* Y plane */
2444 f->sizeimage = (width * height * fmt->depth) >> 3;
2445 } else {
2446 f->bytesperline = (width * fmt->depth) >> 3;
2447 f->sizeimage = height * f->bytesperline;
2448 }
2449}
2450
Hans Verkuil78b526a2008-05-28 12:16:41 -03002451static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002452 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002454 struct bttv_fh *fh = priv;
2455
2456 pix_format_set_size(&f->fmt.pix, fh->fmt,
2457 fh->width, fh->height);
2458 f->fmt.pix.field = fh->cap.field;
2459 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2460
2461 return 0;
2462}
2463
Hans Verkuil78b526a2008-05-28 12:16:41 -03002464static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002465 struct v4l2_format *f)
2466{
2467 struct bttv_fh *fh = priv;
2468
2469 f->fmt.win.w = fh->ov.w;
2470 f->fmt.win.field = fh->ov.field;
2471
2472 return 0;
2473}
2474
Hans Verkuil78b526a2008-05-28 12:16:41 -03002475static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002476 struct v4l2_format *f)
2477{
2478 const struct bttv_format *fmt;
2479 struct bttv_fh *fh = priv;
2480 struct bttv *btv = fh->btv;
2481 enum v4l2_field field;
2482 __s32 width, height;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002483 int rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002484
2485 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2486 if (NULL == fmt)
2487 return -EINVAL;
2488
2489 field = f->fmt.pix.field;
2490
2491 if (V4L2_FIELD_ANY == field) {
2492 __s32 height2;
2493
2494 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2495 field = (f->fmt.pix.height > height2)
2496 ? V4L2_FIELD_INTERLACED
2497 : V4L2_FIELD_BOTTOM;
2498 }
2499
2500 if (V4L2_FIELD_SEQ_BT == field)
2501 field = V4L2_FIELD_SEQ_TB;
2502
2503 switch (field) {
2504 case V4L2_FIELD_TOP:
2505 case V4L2_FIELD_BOTTOM:
2506 case V4L2_FIELD_ALTERNATE:
2507 case V4L2_FIELD_INTERLACED:
2508 break;
2509 case V4L2_FIELD_SEQ_TB:
2510 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2511 return -EINVAL;
2512 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 default:
2514 return -EINVAL;
2515 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002516
2517 width = f->fmt.pix.width;
2518 height = f->fmt.pix.height;
2519
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002520 rc = limit_scaled_size(fh, &width, &height, field,
2521 /* width_mask: 4 pixels */ ~3,
2522 /* width_bias: nearest */ 2,
2523 /* adjust_size */ 1,
2524 /* adjust_crop */ 0);
2525 if (0 != rc)
2526 return rc;
2527
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002528 /* update data for the application */
2529 f->fmt.pix.field = field;
2530 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2531
2532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
2534
Hans Verkuil78b526a2008-05-28 12:16:41 -03002535static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002536 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002538 struct bttv_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002540 return verify_window(fh, &f->fmt.win,
2541 /* adjust_size */ 1,
2542 /* adjust_crop */ 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
2544
Hans Verkuil78b526a2008-05-28 12:16:41 -03002545static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002546 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
2548 int retval;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002549 const struct bttv_format *fmt;
2550 struct bttv_fh *fh = priv;
2551 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002552 __s32 width, height;
2553 enum v4l2_field field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002555 retval = bttv_switch_type(fh, f->type);
2556 if (0 != retval)
2557 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Hans Verkuil78b526a2008-05-28 12:16:41 -03002559 retval = bttv_try_fmt_vid_cap(file, priv, f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002560 if (0 != retval)
2561 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002563 width = f->fmt.pix.width;
2564 height = f->fmt.pix.height;
2565 field = f->fmt.pix.field;
2566
2567 retval = limit_scaled_size(fh, &width, &height, f->fmt.pix.field,
2568 /* width_mask: 4 pixels */ ~3,
2569 /* width_bias: nearest */ 2,
2570 /* adjust_size */ 1,
2571 /* adjust_crop */ 1);
2572 if (0 != retval)
2573 return retval;
2574
2575 f->fmt.pix.field = field;
2576
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002577 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002579 /* update our state informations */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002580 mutex_lock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002581 fh->fmt = fmt;
2582 fh->cap.field = f->fmt.pix.field;
2583 fh->cap.last = V4L2_FIELD_NONE;
2584 fh->width = f->fmt.pix.width;
2585 fh->height = f->fmt.pix.height;
2586 btv->init.fmt = fmt;
2587 btv->init.width = f->fmt.pix.width;
2588 btv->init.height = f->fmt.pix.height;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002589 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002590
2591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
Hans Verkuil78b526a2008-05-28 12:16:41 -03002594static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002595 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002597 struct bttv_fh *fh = priv;
2598 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002600 if (no_overlay > 0) {
2601 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002602 return -EINVAL;
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002603 }
Michael Krufky5e453dc2006-01-09 15:32:31 -02002604
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002605 return setup_window(fh, btv, &f->fmt.win, 1);
2606}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002608#ifdef CONFIG_VIDEO_V4L1_COMPAT
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002609static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2610{
2611 int retval;
2612 unsigned int i;
2613 struct bttv_fh *fh = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002615 mutex_lock(&fh->cap.vb_lock);
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -07002616 retval = __videobuf_mmap_setup(&fh->cap, gbuffers, gbufsize,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002617 V4L2_MEMORY_MMAP);
2618 if (retval < 0) {
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002619 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002620 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002622
2623 gbuffers = retval;
2624 memset(mbuf, 0, sizeof(*mbuf));
2625 mbuf->frames = gbuffers;
2626 mbuf->size = gbuffers * gbufsize;
2627
2628 for (i = 0; i < gbuffers; i++)
2629 mbuf->offsets[i] = i * gbufsize;
2630
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002631 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002632 return 0;
2633}
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002634#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002636static int bttv_querycap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002637 struct v4l2_capability *cap)
2638{
2639 struct bttv_fh *fh = priv;
2640 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002642 if (0 == v4l2)
2643 return -EINVAL;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002644
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002645 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2646 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2647 snprintf(cap->bus_info, sizeof(cap->bus_info),
2648 "PCI:%s", pci_name(btv->c.pci));
2649 cap->version = BTTV_VERSION_CODE;
2650 cap->capabilities =
2651 V4L2_CAP_VIDEO_CAPTURE |
2652 V4L2_CAP_VBI_CAPTURE |
2653 V4L2_CAP_READWRITE |
2654 V4L2_CAP_STREAMING;
Hans Verkuil6a052c82009-06-20 06:09:33 -03002655 if (btv->has_saa6588)
2656 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002657 if (no_overlay <= 0)
2658 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Trent Piephoabb03622009-01-28 21:32:59 -03002660 if (btv->tuner_type != TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002661 cap->capabilities |= V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return 0;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002663}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002665static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2666{
2667 int index = -1, i;
2668
2669 for (i = 0; i < FORMATS; i++) {
2670 if (formats[i].fourcc != -1)
2671 index++;
2672 if ((unsigned int)index == f->index)
2673 break;
2674 }
2675 if (FORMATS == i)
2676 return -EINVAL;
2677
2678 f->pixelformat = formats[i].fourcc;
2679 strlcpy(f->description, formats[i].name, sizeof(f->description));
2680
2681 return i;
2682}
2683
Hans Verkuil78b526a2008-05-28 12:16:41 -03002684static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002685 struct v4l2_fmtdesc *f)
2686{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002687 int rc = bttv_enum_fmt_cap_ovr(f);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002688
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002689 if (rc < 0)
2690 return rc;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002691
2692 return 0;
2693}
2694
Hans Verkuil78b526a2008-05-28 12:16:41 -03002695static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002696 struct v4l2_fmtdesc *f)
2697{
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002698 int rc;
2699
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002700 if (no_overlay > 0) {
2701 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2702 return -EINVAL;
2703 }
2704
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002705 rc = bttv_enum_fmt_cap_ovr(f);
2706
2707 if (rc < 0)
2708 return rc;
2709
2710 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002711 return -EINVAL;
2712
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002713 return 0;
2714}
2715
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002716static int bttv_g_fbuf(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002717 struct v4l2_framebuffer *fb)
2718{
2719 struct bttv_fh *fh = f;
2720 struct bttv *btv = fh->btv;
2721
2722 *fb = btv->fbuf;
2723 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2724 if (fh->ovfmt)
2725 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2726 return 0;
2727}
2728
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002729static int bttv_overlay(struct file *file, void *f, unsigned int on)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002730{
2731 struct bttv_fh *fh = f;
2732 struct bttv *btv = fh->btv;
2733 struct bttv_buffer *new;
2734 int retval;
2735
2736 if (on) {
2737 /* verify args */
2738 if (NULL == btv->fbuf.base)
2739 return -EINVAL;
2740 if (!fh->ov.setup_ok) {
2741 dprintk("bttv%d: overlay: !setup_ok\n", btv->c.nr);
2742 return -EINVAL;
2743 }
2744 }
2745
2746 if (!check_alloc_btres(btv, fh, RESOURCE_OVERLAY))
2747 return -EBUSY;
2748
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002749 mutex_lock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002750 if (on) {
2751 fh->ov.tvnorm = btv->tvnorm;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002752 new = videobuf_sg_alloc(sizeof(*new));
Robert Fitzsimons7c018802008-02-13 16:38:11 -03002753 new->crop = btv->crop[!!fh->do_crop].rect;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002754 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2755 } else {
2756 new = NULL;
2757 }
2758
2759 /* switch over */
2760 retval = bttv_switch_overlay(btv, fh, new);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002761 mutex_unlock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 return retval;
2763}
2764
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002765static int bttv_s_fbuf(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002766 struct v4l2_framebuffer *fb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002768 struct bttv_fh *fh = f;
2769 struct bttv *btv = fh->btv;
2770 const struct bttv_format *fmt;
2771 int retval;
2772
2773 if (!capable(CAP_SYS_ADMIN) &&
2774 !capable(CAP_SYS_RAWIO))
2775 return -EPERM;
2776
2777 /* check args */
2778 fmt = format_by_fourcc(fb->fmt.pixelformat);
2779 if (NULL == fmt)
2780 return -EINVAL;
2781 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2782 return -EINVAL;
2783
2784 retval = -EINVAL;
2785 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2786 __s32 width = fb->fmt.width;
2787 __s32 height = fb->fmt.height;
2788
2789 retval = limit_scaled_size(fh, &width, &height,
2790 V4L2_FIELD_INTERLACED,
2791 /* width_mask */ ~3,
2792 /* width_bias */ 2,
2793 /* adjust_size */ 0,
2794 /* adjust_crop */ 0);
2795 if (0 != retval)
2796 return retval;
2797 }
2798
2799 /* ok, accept it */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002800 mutex_lock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002801 btv->fbuf.base = fb->base;
2802 btv->fbuf.fmt.width = fb->fmt.width;
2803 btv->fbuf.fmt.height = fb->fmt.height;
2804 if (0 != fb->fmt.bytesperline)
2805 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2806 else
2807 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2808
2809 retval = 0;
2810 fh->ovfmt = fmt;
2811 btv->init.ovfmt = fmt;
2812 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2813 fh->ov.w.left = 0;
2814 fh->ov.w.top = 0;
2815 fh->ov.w.width = fb->fmt.width;
2816 fh->ov.w.height = fb->fmt.height;
2817 btv->init.ov.w.width = fb->fmt.width;
2818 btv->init.ov.w.height = fb->fmt.height;
2819 kfree(fh->ov.clips);
2820 fh->ov.clips = NULL;
2821 fh->ov.nclips = 0;
2822
2823 if (check_btres(fh, RESOURCE_OVERLAY)) {
2824 struct bttv_buffer *new;
2825
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002826 new = videobuf_sg_alloc(sizeof(*new));
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002827 new->crop = btv->crop[!!fh->do_crop].rect;
2828 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2829 retval = bttv_switch_overlay(btv, fh, new);
2830 }
2831 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03002832 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002833 return retval;
2834}
2835
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002836static int bttv_reqbufs(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002837 struct v4l2_requestbuffers *p)
2838{
2839 struct bttv_fh *fh = priv;
2840 return videobuf_reqbufs(bttv_queue(fh), p);
2841}
2842
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002843static int bttv_querybuf(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002844 struct v4l2_buffer *b)
2845{
2846 struct bttv_fh *fh = priv;
2847 return videobuf_querybuf(bttv_queue(fh), b);
2848}
2849
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002850static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002851{
2852 struct bttv_fh *fh = priv;
2853 struct bttv *btv = fh->btv;
2854 int res = bttv_resource(fh);
2855
2856 if (!check_alloc_btres(btv, fh, res))
2857 return -EBUSY;
2858
2859 return videobuf_qbuf(bttv_queue(fh), b);
2860}
2861
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002862static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002863{
2864 struct bttv_fh *fh = priv;
2865 return videobuf_dqbuf(bttv_queue(fh), b,
2866 file->f_flags & O_NONBLOCK);
2867}
2868
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002869static int bttv_streamon(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002870 enum v4l2_buf_type type)
2871{
2872 struct bttv_fh *fh = priv;
2873 struct bttv *btv = fh->btv;
2874 int res = bttv_resource(fh);
2875
2876 if (!check_alloc_btres(btv, fh, res))
2877 return -EBUSY;
2878 return videobuf_streamon(bttv_queue(fh));
2879}
2880
2881
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002882static int bttv_streamoff(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002883 enum v4l2_buf_type type)
2884{
2885 struct bttv_fh *fh = priv;
2886 struct bttv *btv = fh->btv;
2887 int retval;
2888 int res = bttv_resource(fh);
2889
2890
2891 retval = videobuf_streamoff(bttv_queue(fh));
2892 if (retval < 0)
2893 return retval;
2894 free_btres(btv, fh, res);
2895 return 0;
2896}
2897
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002898static int bttv_queryctrl(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002899 struct v4l2_queryctrl *c)
2900{
2901 struct bttv_fh *fh = priv;
2902 struct bttv *btv = fh->btv;
2903 const struct v4l2_queryctrl *ctrl;
2904
2905 if ((c->id < V4L2_CID_BASE ||
2906 c->id >= V4L2_CID_LASTP1) &&
2907 (c->id < V4L2_CID_PRIVATE_BASE ||
2908 c->id >= V4L2_CID_PRIVATE_LASTP1))
2909 return -EINVAL;
2910
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002911 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2912 *c = no_ctl;
2913 else {
2914 ctrl = ctrl_by_id(c->id);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002915
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03002916 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2917 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002918
2919 return 0;
2920}
2921
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002922static int bttv_g_parm(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002923 struct v4l2_streamparm *parm)
2924{
2925 struct bttv_fh *fh = f;
2926 struct bttv *btv = fh->btv;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002927
Trent Piepho51f0b8d2009-03-04 01:21:02 -03002928 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2929 &parm->parm.capture.timeperframe);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002930 return 0;
2931}
2932
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002933static int bttv_g_tuner(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002934 struct v4l2_tuner *t)
2935{
2936 struct bttv_fh *fh = priv;
2937 struct bttv *btv = fh->btv;
2938
Trent Piephoabb03622009-01-28 21:32:59 -03002939 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002940 return -EINVAL;
2941 if (0 != t->index)
2942 return -EINVAL;
2943
2944 mutex_lock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002945 t->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil859f0272009-03-28 08:29:00 -03002946 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002947 strcpy(t->name, "Television");
2948 t->capability = V4L2_TUNER_CAP_NORM;
2949 t->type = V4L2_TUNER_ANALOG_TV;
2950 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2951 t->signal = 0xffff;
2952
2953 if (btv->audio_mode_gpio)
2954 btv->audio_mode_gpio(btv, t, 0);
2955
2956 mutex_unlock(&btv->lock);
2957 return 0;
2958}
2959
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002960static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002961{
2962 struct bttv_fh *fh = f;
2963 struct bttv *btv = fh->btv;
2964
2965 *p = v4l2_prio_max(&btv->prio);
2966
2967 return 0;
2968}
2969
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002970static int bttv_s_priority(struct file *file, void *f,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002971 enum v4l2_priority prio)
2972{
2973 struct bttv_fh *fh = f;
2974 struct bttv *btv = fh->btv;
2975
2976 return v4l2_prio_change(&btv->prio, &fh->prio, prio);
2977}
2978
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002979static int bttv_cropcap(struct file *file, void *priv,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002980 struct v4l2_cropcap *cap)
2981{
2982 struct bttv_fh *fh = priv;
2983 struct bttv *btv = fh->btv;
2984
2985 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2986 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2987 return -EINVAL;
2988
2989 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
2990
2991 return 0;
2992}
2993
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03002994static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03002995{
2996 struct bttv_fh *fh = f;
2997 struct bttv *btv = fh->btv;
2998
2999 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3000 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3001 return -EINVAL;
3002
3003 /* No fh->do_crop = 1; because btv->crop[1] may be
3004 inconsistent with fh->width or fh->height and apps
3005 do not expect a change here. */
3006
3007 crop->c = btv->crop[!!fh->do_crop].rect;
3008
3009 return 0;
3010}
3011
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003012static int bttv_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003013{
3014 struct bttv_fh *fh = f;
3015 struct bttv *btv = fh->btv;
3016 const struct v4l2_rect *b;
3017 int retval;
3018 struct bttv_crop c;
3019 __s32 b_left;
3020 __s32 b_top;
3021 __s32 b_right;
3022 __s32 b_bottom;
3023
3024 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3025 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3026 return -EINVAL;
3027
3028 retval = v4l2_prio_check(&btv->prio, &fh->prio);
3029 if (0 != retval)
3030 return retval;
3031
3032 /* Make sure tvnorm, vbi_end and the current cropping
3033 parameters remain consistent until we're done. Note
3034 read() may change vbi_end in check_alloc_btres(). */
3035 mutex_lock(&btv->lock);
3036
3037 retval = -EBUSY;
3038
3039 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
3040 mutex_unlock(&btv->lock);
3041 return retval;
3042 }
3043
3044 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3045
3046 b_left = b->left;
3047 b_right = b_left + b->width;
3048 b_bottom = b->top + b->height;
3049
3050 b_top = max(b->top, btv->vbi_end);
3051 if (b_top + 32 >= b_bottom) {
3052 mutex_unlock(&btv->lock);
3053 return retval;
3054 }
3055
3056 /* Min. scaled size 48 x 32. */
3057 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3058 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3059
3060 c.rect.width = clamp(crop->c.width,
3061 48, b_right - c.rect.left);
3062
3063 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3064 /* Top and height must be a multiple of two. */
3065 c.rect.top = (c.rect.top + 1) & ~1;
3066
3067 c.rect.height = clamp(crop->c.height,
3068 32, b_bottom - c.rect.top);
3069 c.rect.height = (c.rect.height + 1) & ~1;
3070
3071 bttv_crop_calc_limits(&c);
3072
3073 btv->crop[1] = c;
3074
3075 mutex_unlock(&btv->lock);
3076
3077 fh->do_crop = 1;
3078
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003079 mutex_lock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003080
3081 if (fh->width < c.min_scaled_width) {
3082 fh->width = c.min_scaled_width;
3083 btv->init.width = c.min_scaled_width;
3084 } else if (fh->width > c.max_scaled_width) {
3085 fh->width = c.max_scaled_width;
3086 btv->init.width = c.max_scaled_width;
3087 }
3088
3089 if (fh->height < c.min_scaled_height) {
3090 fh->height = c.min_scaled_height;
3091 btv->init.height = c.min_scaled_height;
3092 } else if (fh->height > c.max_scaled_height) {
3093 fh->height = c.max_scaled_height;
3094 btv->init.height = c.max_scaled_height;
3095 }
3096
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003097 mutex_unlock(&fh->cap.vb_lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003098
3099 return 0;
3100}
3101
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003102static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003103{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003104 if (unlikely(a->index))
3105 return -EINVAL;
3106
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003107 strcpy(a->name, "audio");
3108 return 0;
3109}
3110
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003111static int bttv_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003112{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003113 if (unlikely(a->index))
3114 return -EINVAL;
3115
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117}
3118
3119static ssize_t bttv_read(struct file *file, char __user *data,
3120 size_t count, loff_t *ppos)
3121{
3122 struct bttv_fh *fh = file->private_data;
3123 int retval = 0;
3124
3125 if (fh->btv->errors)
3126 bttv_reinit_bt848(fh->btv);
3127 dprintk("bttv%d: read count=%d type=%s\n",
3128 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3129
3130 switch (fh->type) {
3131 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03003132 if (!check_alloc_btres(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3133 /* VIDEO_READ in use by another fh,
3134 or VIDEO_STREAM by any fh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return -EBUSY;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3138 file->f_flags & O_NONBLOCK);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003139 free_btres(fh->btv, fh, RESOURCE_VIDEO_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 break;
3141 case V4L2_BUF_TYPE_VBI_CAPTURE:
3142 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3143 return -EBUSY;
3144 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3145 file->f_flags & O_NONBLOCK);
3146 break;
3147 default:
3148 BUG();
3149 }
3150 return retval;
3151}
3152
3153static unsigned int bttv_poll(struct file *file, poll_table *wait)
3154{
3155 struct bttv_fh *fh = file->private_data;
3156 struct bttv_buffer *buf;
3157 enum v4l2_field field;
Figo.zhang9fd64182009-06-16 13:31:29 -03003158 unsigned int rc = POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3161 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3162 return POLLERR;
3163 return videobuf_poll_stream(file, &fh->vbi, wait);
3164 }
3165
Michael Schimeke5bd0262007-01-18 16:17:39 -03003166 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
Figo.zhang9fd64182009-06-16 13:31:29 -03003167 mutex_lock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 /* streaming capture */
3169 if (list_empty(&fh->cap.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -03003170 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3172 } else {
3173 /* read() capture */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003174 mutex_lock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 if (NULL == fh->cap.read_buf) {
3176 /* need to capture a new frame */
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003177 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3178 goto err;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003179 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003180 if (NULL == fh->cap.read_buf)
3181 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3183 field = videobuf_next_field(&fh->cap);
3184 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -08003185 kfree (fh->cap.read_buf);
3186 fh->cap.read_buf = NULL;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003187 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
3189 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3190 fh->cap.read_off = 0;
3191 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003192 mutex_unlock(&fh->cap.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 buf = (struct bttv_buffer*)fh->cap.read_buf;
3194 }
3195
3196 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03003197 if (buf->vb.state == VIDEOBUF_DONE ||
3198 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -03003199 rc = POLLIN|POLLRDNORM;
3200 else
3201 rc = 0;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03003202err:
3203 mutex_unlock(&fh->cap.vb_lock);
Figo.zhang9fd64182009-06-16 13:31:29 -03003204 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Hans Verkuilbec43662008-12-30 06:58:20 -03003207static int bttv_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
Hans Verkuilbec43662008-12-30 06:58:20 -03003209 int minor = video_devdata(file)->minor;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003210 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003211 struct bttv *btv = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 struct bttv_fh *fh;
3213 enum v4l2_buf_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003215 dprintk(KERN_DEBUG "bttv: open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Hans Verkuild56dc612008-07-30 08:43:36 -03003217 lock_kernel();
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003218 if (btv->video_dev->minor == minor) {
3219 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3220 } else if (btv->vbi_dev->minor == minor) {
3221 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3222 } else {
3223 WARN_ON(1);
Hans Verkuild56dc612008-07-30 08:43:36 -03003224 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -03003226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3229 btv->c.nr,v4l2_type_names[type]);
3230
3231 /* allocate per filehandle data */
3232 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
Hans Verkuild56dc612008-07-30 08:43:36 -03003233 if (NULL == fh) {
3234 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 return -ENOMEM;
Hans Verkuild56dc612008-07-30 08:43:36 -03003236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 file->private_data = fh;
3238 *fh = btv->init;
3239 fh->type = type;
3240 fh->ov.setup_ok = 0;
3241 v4l2_prio_open(&btv->prio,&fh->prio);
3242
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003243 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3244 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3246 V4L2_FIELD_INTERLACED,
3247 sizeof(struct bttv_buffer),
3248 fh);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03003249 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3250 &btv->c.pci->dev, &btv->s_lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 V4L2_BUF_TYPE_VBI_CAPTURE,
3252 V4L2_FIELD_SEQ_TB,
3253 sizeof(struct bttv_buffer),
3254 fh);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03003255 set_tvnorm(btv,btv->tvnorm);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003256 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
3258 btv->users++;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003259
3260 /* The V4L2 spec requires one global set of cropping parameters
3261 which only change on request. These are stored in btv->crop[1].
3262 However for compatibility with V4L apps and cropping unaware
3263 V4L2 apps we now reset the cropping parameters as seen through
3264 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3265 will use btv->crop[0], the default cropping parameters for the
3266 current video standard, and VIDIOC_S_FMT will not implicitely
3267 change the cropping parameters until VIDIOC_S_CROP has been
3268 called. */
3269 fh->do_crop = !reset_crop; /* module parameter */
3270
3271 /* Likewise there should be one global set of VBI capture
3272 parameters, but for compatibility with V4L apps and earlier
3273 driver versions each fh has its own parameters. */
3274 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 bttv_field_count(btv);
Hans Verkuild56dc612008-07-30 08:43:36 -03003277 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return 0;
3279}
3280
Hans Verkuilbec43662008-12-30 06:58:20 -03003281static int bttv_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
3283 struct bttv_fh *fh = file->private_data;
3284 struct bttv *btv = fh->btv;
3285
3286 /* turn off overlay */
3287 if (check_btres(fh, RESOURCE_OVERLAY))
3288 bttv_switch_overlay(btv,fh,NULL);
3289
3290 /* stop video capture */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003291 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 videobuf_streamoff(&fh->cap);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003293 free_btres(btv,fh,RESOURCE_VIDEO_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 }
3295 if (fh->cap.read_buf) {
3296 buffer_release(&fh->cap,fh->cap.read_buf);
3297 kfree(fh->cap.read_buf);
3298 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03003299 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3300 free_btres(btv, fh, RESOURCE_VIDEO_READ);
3301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
3303 /* stop vbi capture */
3304 if (check_btres(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03003305 videobuf_stop(&fh->vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 free_btres(btv,fh,RESOURCE_VBI);
3307 }
3308
3309 /* free stuff */
3310 videobuf_mmap_free(&fh->cap);
3311 videobuf_mmap_free(&fh->vbi);
3312 v4l2_prio_close(&btv->prio,&fh->prio);
3313 file->private_data = NULL;
3314 kfree(fh);
3315
3316 btv->users--;
3317 bttv_field_count(btv);
Mauro Carvalho Chehabb46a9c82008-08-05 10:12:35 -03003318
3319 if (!btv->users)
3320 audio_mute(btv, 1);
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return 0;
3323}
3324
3325static int
3326bttv_mmap(struct file *file, struct vm_area_struct *vma)
3327{
3328 struct bttv_fh *fh = file->private_data;
3329
3330 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3331 fh->btv->c.nr, v4l2_type_names[fh->type],
3332 vma->vm_start, vma->vm_end - vma->vm_start);
3333 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3334}
3335
Hans Verkuilbec43662008-12-30 06:58:20 -03003336static const struct v4l2_file_operations bttv_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
3338 .owner = THIS_MODULE,
3339 .open = bttv_open,
3340 .release = bttv_release,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003341 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 .read = bttv_read,
3343 .mmap = bttv_mmap,
3344 .poll = bttv_poll,
3345};
3346
Hans Verkuila3998102008-07-21 02:57:38 -03003347static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003348 .vidioc_querycap = bttv_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003349 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3350 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3351 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3352 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3353 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3354 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3355 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3356 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
Hans Verkuil78b526a2008-05-28 12:16:41 -03003357 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3358 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3359 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003360 .vidioc_g_audio = bttv_g_audio,
3361 .vidioc_s_audio = bttv_s_audio,
3362 .vidioc_cropcap = bttv_cropcap,
3363 .vidioc_reqbufs = bttv_reqbufs,
3364 .vidioc_querybuf = bttv_querybuf,
3365 .vidioc_qbuf = bttv_qbuf,
3366 .vidioc_dqbuf = bttv_dqbuf,
3367 .vidioc_s_std = bttv_s_std,
3368 .vidioc_enum_input = bttv_enum_input,
3369 .vidioc_g_input = bttv_g_input,
3370 .vidioc_s_input = bttv_s_input,
3371 .vidioc_queryctrl = bttv_queryctrl,
3372 .vidioc_g_ctrl = bttv_g_ctrl,
3373 .vidioc_s_ctrl = bttv_s_ctrl,
3374 .vidioc_streamon = bttv_streamon,
3375 .vidioc_streamoff = bttv_streamoff,
3376 .vidioc_g_tuner = bttv_g_tuner,
3377 .vidioc_s_tuner = bttv_s_tuner,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003378#ifdef CONFIG_VIDEO_V4L1_COMPAT
3379 .vidiocgmbuf = vidiocgmbuf,
3380#endif
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003381 .vidioc_g_crop = bttv_g_crop,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003382 .vidioc_s_crop = bttv_s_crop,
3383 .vidioc_g_fbuf = bttv_g_fbuf,
3384 .vidioc_s_fbuf = bttv_s_fbuf,
3385 .vidioc_overlay = bttv_overlay,
3386 .vidioc_g_priority = bttv_g_priority,
3387 .vidioc_s_priority = bttv_s_priority,
3388 .vidioc_g_parm = bttv_g_parm,
3389 .vidioc_g_frequency = bttv_g_frequency,
3390 .vidioc_s_frequency = bttv_s_frequency,
3391 .vidioc_log_status = bttv_log_status,
3392 .vidioc_querystd = bttv_querystd,
Zoltan Devai43846832008-01-14 13:24:38 -03003393#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003394 .vidioc_g_register = bttv_g_register,
3395 .vidioc_s_register = bttv_s_register,
Zoltan Devai43846832008-01-14 13:24:38 -03003396#endif
Hans Verkuila3998102008-07-21 02:57:38 -03003397};
3398
3399static struct video_device bttv_video_template = {
3400 .fops = &bttv_fops,
3401 .minor = -1,
3402 .ioctl_ops = &bttv_ioctl_ops,
3403 .tvnorms = BTTV_NORMS,
3404 .current_norm = V4L2_STD_PAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405};
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407/* ----------------------------------------------------------------------- */
3408/* radio interface */
3409
Hans Verkuilbec43662008-12-30 06:58:20 -03003410static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411{
Hans Verkuilbec43662008-12-30 06:58:20 -03003412 int minor = video_devdata(file)->minor;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003413 struct video_device *vdev = video_devdata(file);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003414 struct bttv *btv = video_drvdata(file);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003415 struct bttv_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Laurent Pinchart50462eb2009-12-10 11:47:13 -02003417 dprintk("bttv: open dev=%s\n", video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Hans Verkuild56dc612008-07-30 08:43:36 -03003419 lock_kernel();
Trent Piepho4b10d3b2009-01-28 21:32:59 -03003420 WARN_ON(btv->radio_dev && btv->radio_dev->minor != minor);
3421 if (!btv->radio_dev || btv->radio_dev->minor != minor) {
Hans Verkuild56dc612008-07-30 08:43:36 -03003422 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -03003424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
3426 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003427
3428 /* allocate per filehandle data */
3429 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
Hans Verkuild56dc612008-07-30 08:43:36 -03003430 if (NULL == fh) {
3431 unlock_kernel();
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003432 return -ENOMEM;
Hans Verkuild56dc612008-07-30 08:43:36 -03003433 }
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003434 file->private_data = fh;
3435 *fh = btv->init;
3436 v4l2_prio_open(&btv->prio, &fh->prio);
3437
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02003438 mutex_lock(&btv->lock);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003439
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 btv->radio_user++;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003441
Hans Verkuil859f0272009-03-28 08:29:00 -03003442 bttv_call_all(btv, tuner, s_radio);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03003443 audio_input(btv,TVAUDIO_INPUT_RADIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02003445 mutex_unlock(&btv->lock);
Hans Verkuild56dc612008-07-30 08:43:36 -03003446 unlock_kernel();
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448}
3449
Hans Verkuilbec43662008-12-30 06:58:20 -03003450static int radio_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003452 struct bttv_fh *fh = file->private_data;
3453 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003454 struct rds_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Udo Steinbergb15dd792009-01-02 17:34:28 -03003456 v4l2_prio_close(&btv->prio,&fh->prio);
Robert Fitzsimonsb9bc07a2008-04-10 09:40:31 -03003457 file->private_data = NULL;
3458 kfree(fh);
3459
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 btv->radio_user--;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003461
Hans Verkuil859f0272009-03-28 08:29:00 -03003462 bttv_call_all(btv, core, ioctl, RDS_CMD_CLOSE, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 return 0;
3465}
3466
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003467static int radio_querycap(struct file *file, void *priv,
3468 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003470 struct bttv_fh *fh = priv;
3471 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003473 strcpy(cap->driver, "bttv");
3474 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
3475 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
3476 cap->version = BTTV_VERSION_CODE;
3477 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 return 0;
3480}
3481
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003482static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003484 struct bttv_fh *fh = priv;
3485 struct bttv *btv = fh->btv;
3486
Trent Piephoabb03622009-01-28 21:32:59 -03003487 if (btv->tuner_type == TUNER_ABSENT)
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003488 return -EINVAL;
3489 if (0 != t->index)
3490 return -EINVAL;
3491 mutex_lock(&btv->lock);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003492 strcpy(t->name, "Radio");
3493 t->type = V4L2_TUNER_RADIO;
3494
Hans Verkuil859f0272009-03-28 08:29:00 -03003495 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003496
3497 if (btv->audio_mode_gpio)
3498 btv->audio_mode_gpio(btv, t, 0);
3499
3500 mutex_unlock(&btv->lock);
3501
3502 return 0;
3503}
3504
3505static int radio_enum_input(struct file *file, void *priv,
3506 struct v4l2_input *i)
3507{
3508 if (i->index != 0)
3509 return -EINVAL;
3510
3511 strcpy(i->name, "Radio");
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003512 i->type = V4L2_INPUT_TYPE_TUNER;
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003513
3514 return 0;
3515}
3516
3517static int radio_g_audio(struct file *file, void *priv,
3518 struct v4l2_audio *a)
3519{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003520 if (unlikely(a->index))
Cyrill Gorcunov1a002eb2008-04-01 17:49:13 -03003521 return -EINVAL;
3522
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003523 strcpy(a->name, "Radio");
Cyrill Gorcunov1a002eb2008-04-01 17:49:13 -03003524
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003525 return 0;
3526}
3527
3528static int radio_s_tuner(struct file *file, void *priv,
3529 struct v4l2_tuner *t)
3530{
3531 struct bttv_fh *fh = priv;
3532 struct bttv *btv = fh->btv;
3533
3534 if (0 != t->index)
3535 return -EINVAL;
3536
Hans Verkuil859f0272009-03-28 08:29:00 -03003537 bttv_call_all(btv, tuner, g_tuner, t);
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003538 return 0;
3539}
3540
3541static int radio_s_audio(struct file *file, void *priv,
3542 struct v4l2_audio *a)
3543{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003544 if (unlikely(a->index))
3545 return -EINVAL;
3546
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003547 return 0;
3548}
3549
3550static int radio_s_input(struct file *filp, void *priv, unsigned int i)
3551{
Mauro Carvalho Chehabd69b2e42008-04-01 20:30:24 -03003552 if (unlikely(i))
3553 return -EINVAL;
3554
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003555 return 0;
3556}
3557
3558static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
3559{
3560 return 0;
3561}
3562
3563static int radio_queryctrl(struct file *file, void *priv,
3564 struct v4l2_queryctrl *c)
3565{
3566 const struct v4l2_queryctrl *ctrl;
3567
3568 if (c->id < V4L2_CID_BASE ||
3569 c->id >= V4L2_CID_LASTP1)
3570 return -EINVAL;
3571
3572 if (c->id == V4L2_CID_AUDIO_MUTE) {
3573 ctrl = ctrl_by_id(c->id);
3574 *c = *ctrl;
3575 } else
3576 *c = no_ctl;
3577
3578 return 0;
3579}
3580
3581static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
3582{
3583 *i = 0;
3584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585}
3586
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003587static ssize_t radio_read(struct file *file, char __user *data,
3588 size_t count, loff_t *ppos)
3589{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003590 struct bttv_fh *fh = file->private_data;
3591 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003592 struct rds_command cmd;
3593 cmd.block_count = count/3;
3594 cmd.buffer = data;
3595 cmd.instance = file;
3596 cmd.result = -ENODEV;
3597
Hans Verkuil859f0272009-03-28 08:29:00 -03003598 bttv_call_all(btv, core, ioctl, RDS_CMD_READ, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003599
3600 return cmd.result;
3601}
3602
3603static unsigned int radio_poll(struct file *file, poll_table *wait)
3604{
Robert Fitzsimons5cd39552008-04-01 11:41:54 -03003605 struct bttv_fh *fh = file->private_data;
3606 struct bttv *btv = fh->btv;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003607 struct rds_command cmd;
3608 cmd.instance = file;
3609 cmd.event_list = wait;
3610 cmd.result = -ENODEV;
Hans Verkuil859f0272009-03-28 08:29:00 -03003611 bttv_call_all(btv, core, ioctl, RDS_CMD_POLL, &cmd);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003612
3613 return cmd.result;
3614}
3615
Hans Verkuilbec43662008-12-30 06:58:20 -03003616static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617{
3618 .owner = THIS_MODULE,
3619 .open = radio_open,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003620 .read = radio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 .release = radio_release,
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003622 .ioctl = video_ioctl2,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003623 .poll = radio_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624};
3625
Hans Verkuila3998102008-07-21 02:57:38 -03003626static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -03003627 .vidioc_querycap = radio_querycap,
3628 .vidioc_g_tuner = radio_g_tuner,
3629 .vidioc_enum_input = radio_enum_input,
3630 .vidioc_g_audio = radio_g_audio,
3631 .vidioc_s_tuner = radio_s_tuner,
3632 .vidioc_s_audio = radio_s_audio,
3633 .vidioc_s_input = radio_s_input,
3634 .vidioc_s_std = radio_s_std,
3635 .vidioc_queryctrl = radio_queryctrl,
3636 .vidioc_g_input = radio_g_input,
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -03003637 .vidioc_g_ctrl = bttv_g_ctrl,
3638 .vidioc_s_ctrl = bttv_s_ctrl,
3639 .vidioc_g_frequency = bttv_g_frequency,
3640 .vidioc_s_frequency = bttv_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641};
3642
Hans Verkuila3998102008-07-21 02:57:38 -03003643static struct video_device radio_template = {
3644 .fops = &radio_fops,
3645 .minor = -1,
3646 .ioctl_ops = &radio_ioctl_ops,
3647};
3648
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649/* ----------------------------------------------------------------------- */
3650/* some debug code */
3651
Adrian Bunk408b6642005-05-01 08:59:29 -07003652static int bttv_risc_decode(u32 risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
3654 static char *instr[16] = {
3655 [ BT848_RISC_WRITE >> 28 ] = "write",
3656 [ BT848_RISC_SKIP >> 28 ] = "skip",
3657 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3658 [ BT848_RISC_JUMP >> 28 ] = "jump",
3659 [ BT848_RISC_SYNC >> 28 ] = "sync",
3660 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3661 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3662 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3663 };
3664 static int incr[16] = {
3665 [ BT848_RISC_WRITE >> 28 ] = 2,
3666 [ BT848_RISC_JUMP >> 28 ] = 2,
3667 [ BT848_RISC_SYNC >> 28 ] = 2,
3668 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3669 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3670 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3671 };
3672 static char *bits[] = {
3673 "be0", "be1", "be2", "be3/resync",
3674 "set0", "set1", "set2", "set3",
3675 "clr0", "clr1", "clr2", "clr3",
3676 "irq", "res", "eol", "sol",
3677 };
3678 int i;
3679
3680 printk("0x%08x [ %s", risc,
3681 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3682 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3683 if (risc & (1 << (i + 12)))
3684 printk(" %s",bits[i]);
3685 printk(" count=%d ]\n", risc & 0xfff);
3686 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3687}
3688
Adrian Bunk408b6642005-05-01 08:59:29 -07003689static void bttv_risc_disasm(struct bttv *btv,
3690 struct btcx_riscmem *risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
3692 unsigned int i,j,n;
3693
3694 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003695 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 for (i = 0; i < (risc->size >> 2); i += n) {
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003697 printk("%s: 0x%lx: ", btv->c.v4l2_dev.name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 (unsigned long)(risc->dma + (i<<2)));
Al Viro3aa71102008-06-22 14:20:09 -03003699 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 for (j = 1; j < n; j++)
3701 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03003702 btv->c.v4l2_dev.name, (unsigned long)(risc->dma + ((i+j)<<2)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 risc->cpu[i+j], j);
3704 if (0 == risc->cpu[i])
3705 break;
3706 }
3707}
3708
3709static void bttv_print_riscaddr(struct bttv *btv)
3710{
3711 printk(" main: %08Lx\n",
3712 (unsigned long long)btv->main.dma);
3713 printk(" vbi : o=%08Lx e=%08Lx\n",
3714 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3715 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3716 printk(" cap : o=%08Lx e=%08Lx\n",
3717 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3718 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3719 printk(" scr : o=%08Lx e=%08Lx\n",
3720 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3721 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3722 bttv_risc_disasm(btv, &btv->main);
3723}
3724
3725/* ----------------------------------------------------------------------- */
3726/* irq handler */
3727
3728static char *irq_name[] = {
3729 "FMTCHG", // format change detected (525 vs. 625)
3730 "VSYNC", // vertical sync (new field)
3731 "HSYNC", // horizontal sync
3732 "OFLOW", // chroma/luma AGC overflow
3733 "HLOCK", // horizontal lock changed
3734 "VPRES", // video presence changed
3735 "6", "7",
3736 "I2CDONE", // hw irc operation finished
3737 "GPINT", // gpio port triggered irq
3738 "10",
3739 "RISCI", // risc instruction triggered irq
3740 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3741 "FTRGT", // pixel data fifo overrun
3742 "FDSR", // fifo data stream resyncronisation
3743 "PPERR", // parity error (data transfer)
3744 "RIPERR", // parity error (read risc instructions)
3745 "PABORT", // pci abort
3746 "OCERR", // risc instruction error
3747 "SCERR", // syncronisation error
3748};
3749
3750static void bttv_print_irqbits(u32 print, u32 mark)
3751{
3752 unsigned int i;
3753
3754 printk("bits:");
3755 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3756 if (print & (1 << i))
3757 printk(" %s",irq_name[i]);
3758 if (mark & (1 << i))
3759 printk("*");
3760 }
3761}
3762
3763static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3764{
3765 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3766 btv->c.nr,
3767 (unsigned long)btv->main.dma,
Al Viro3aa71102008-06-22 14:20:09 -03003768 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3769 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 (unsigned long)rc);
3771
3772 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3773 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3774 "Ok, then this is harmless, don't worry ;)\n",
3775 btv->c.nr);
3776 return;
3777 }
3778 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3779 btv->c.nr);
3780 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3781 btv->c.nr);
3782 dump_stack();
3783}
3784
3785static int
3786bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3787{
3788 struct bttv_buffer *item;
3789
3790 memset(set,0,sizeof(*set));
3791
3792 /* capture request ? */
3793 if (!list_empty(&btv->capture)) {
3794 set->frame_irq = 1;
3795 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3796 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3797 set->top = item;
3798 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3799 set->bottom = item;
3800
3801 /* capture request for other field ? */
3802 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3803 (item->vb.queue.next != &btv->capture)) {
3804 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
Mike Isely66349b42009-09-21 12:09:08 -03003805 /* Mike Isely <isely@pobox.com> - Only check
3806 * and set up the bottom field in the logic
3807 * below. Don't ever do the top field. This
3808 * of course means that if we set up the
3809 * bottom field in the above code that we'll
3810 * actually skip a field. But that's OK.
3811 * Having processed only a single buffer this
3812 * time, then the next time around the first
3813 * available buffer should be for a top field.
3814 * That will then cause us here to set up a
3815 * top then a bottom field in the normal way.
3816 * The alternative to this understanding is
3817 * that we set up the second available buffer
3818 * as a top field, but that's out of order
3819 * since this driver always processes the top
3820 * field first - the effect will be the two
3821 * buffers being returned in the wrong order,
3822 * with the second buffer also being delayed
3823 * by one field time (owing to the fifo nature
3824 * of videobuf). Worse still, we'll be stuck
3825 * doing fields out of order now every time
3826 * until something else causes a field to be
3827 * dropped. By effectively forcing a field to
3828 * drop this way then we always get back into
3829 * sync within a single frame time. (Out of
3830 * order fields can screw up deinterlacing
3831 * algorithms.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 if (NULL == set->bottom &&
3834 V4L2_FIELD_BOTTOM == item->vb.field) {
3835 set->bottom = item;
3836 }
3837 if (NULL != set->top && NULL != set->bottom)
3838 set->top_irq = 2;
3839 }
3840 }
3841 }
3842
3843 /* screen overlay ? */
3844 if (NULL != btv->screen) {
3845 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3846 if (NULL == set->top && NULL == set->bottom) {
3847 set->top = btv->screen;
3848 set->bottom = btv->screen;
3849 }
3850 } else {
3851 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3852 NULL == set->top) {
3853 set->top = btv->screen;
3854 }
3855 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3856 NULL == set->bottom) {
3857 set->bottom = btv->screen;
3858 }
3859 }
3860 }
3861
3862 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3863 btv->c.nr,set->top, set->bottom,
3864 btv->screen,set->frame_irq,set->top_irq);
3865 return 0;
3866}
3867
3868static void
3869bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3870 struct bttv_buffer_set *curr, unsigned int state)
3871{
3872 struct timeval ts;
3873
3874 do_gettimeofday(&ts);
3875
3876 if (wakeup->top == wakeup->bottom) {
3877 if (NULL != wakeup->top && curr->top != wakeup->top) {
3878 if (irq_debug > 1)
3879 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3880 wakeup->top->vb.ts = ts;
3881 wakeup->top->vb.field_count = btv->field_count;
3882 wakeup->top->vb.state = state;
3883 wake_up(&wakeup->top->vb.done);
3884 }
3885 } else {
3886 if (NULL != wakeup->top && curr->top != wakeup->top) {
3887 if (irq_debug > 1)
3888 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3889 wakeup->top->vb.ts = ts;
3890 wakeup->top->vb.field_count = btv->field_count;
3891 wakeup->top->vb.state = state;
3892 wake_up(&wakeup->top->vb.done);
3893 }
3894 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3895 if (irq_debug > 1)
3896 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3897 wakeup->bottom->vb.ts = ts;
3898 wakeup->bottom->vb.field_count = btv->field_count;
3899 wakeup->bottom->vb.state = state;
3900 wake_up(&wakeup->bottom->vb.done);
3901 }
3902 }
3903}
3904
3905static void
3906bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3907 unsigned int state)
3908{
3909 struct timeval ts;
3910
3911 if (NULL == wakeup)
3912 return;
3913
3914 do_gettimeofday(&ts);
3915 wakeup->vb.ts = ts;
3916 wakeup->vb.field_count = btv->field_count;
3917 wakeup->vb.state = state;
3918 wake_up(&wakeup->vb.done);
3919}
3920
3921static void bttv_irq_timeout(unsigned long data)
3922{
3923 struct bttv *btv = (struct bttv *)data;
3924 struct bttv_buffer_set old,new;
3925 struct bttv_buffer *ovbi;
3926 struct bttv_buffer *item;
3927 unsigned long flags;
3928
3929 if (bttv_verbose) {
3930 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3931 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3932 btread(BT848_RISC_COUNT));
3933 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3934 printk("\n");
3935 }
3936
3937 spin_lock_irqsave(&btv->s_lock,flags);
3938
3939 /* deactivate stuff */
3940 memset(&new,0,sizeof(new));
3941 old = btv->curr;
3942 ovbi = btv->cvbi;
3943 btv->curr = new;
3944 btv->cvbi = NULL;
3945 btv->loop_irq = 0;
3946 bttv_buffer_activate_video(btv, &new);
3947 bttv_buffer_activate_vbi(btv, NULL);
3948 bttv_set_dma(btv, 0);
3949
3950 /* wake up */
Brandon Philips0fc06862007-11-06 20:02:36 -03003951 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3952 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
3954 /* cancel all outstanding capture / vbi requests */
3955 while (!list_empty(&btv->capture)) {
3956 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3957 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003958 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 wake_up(&item->vb.done);
3960 }
3961 while (!list_empty(&btv->vcapture)) {
3962 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3963 list_del(&item->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03003964 item->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 wake_up(&item->vb.done);
3966 }
3967
3968 btv->errors++;
3969 spin_unlock_irqrestore(&btv->s_lock,flags);
3970}
3971
3972static void
3973bttv_irq_wakeup_top(struct bttv *btv)
3974{
3975 struct bttv_buffer *wakeup = btv->curr.top;
3976
3977 if (NULL == wakeup)
3978 return;
3979
3980 spin_lock(&btv->s_lock);
3981 btv->curr.top_irq = 0;
3982 btv->curr.top = NULL;
3983 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3984
3985 do_gettimeofday(&wakeup->vb.ts);
3986 wakeup->vb.field_count = btv->field_count;
Brandon Philips0fc06862007-11-06 20:02:36 -03003987 wakeup->vb.state = VIDEOBUF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 wake_up(&wakeup->vb.done);
3989 spin_unlock(&btv->s_lock);
3990}
3991
3992static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3993{
3994 if (rc < risc->dma)
3995 return 0;
3996 if (rc > risc->dma + risc->size)
3997 return 0;
3998 return 1;
3999}
4000
4001static void
4002bttv_irq_switch_video(struct bttv *btv)
4003{
4004 struct bttv_buffer_set new;
4005 struct bttv_buffer_set old;
4006 dma_addr_t rc;
4007
4008 spin_lock(&btv->s_lock);
4009
4010 /* new buffer set */
4011 bttv_irq_next_video(btv, &new);
4012 rc = btread(BT848_RISC_COUNT);
4013 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
4014 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
4015 btv->framedrop++;
4016 if (debug_latency)
4017 bttv_irq_debug_low_latency(btv, rc);
4018 spin_unlock(&btv->s_lock);
4019 return;
4020 }
4021
4022 /* switch over */
4023 old = btv->curr;
4024 btv->curr = new;
4025 btv->loop_irq &= ~1;
4026 bttv_buffer_activate_video(btv, &new);
4027 bttv_set_dma(btv, 0);
4028
4029 /* switch input */
4030 if (UNSET != btv->new_input) {
4031 video_mux(btv,btv->new_input);
4032 btv->new_input = UNSET;
4033 }
4034
4035 /* wake up finished buffers */
Brandon Philips0fc06862007-11-06 20:02:36 -03004036 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 spin_unlock(&btv->s_lock);
4038}
4039
4040static void
4041bttv_irq_switch_vbi(struct bttv *btv)
4042{
4043 struct bttv_buffer *new = NULL;
4044 struct bttv_buffer *old;
4045 u32 rc;
4046
4047 spin_lock(&btv->s_lock);
4048
4049 if (!list_empty(&btv->vcapture))
4050 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4051 old = btv->cvbi;
4052
4053 rc = btread(BT848_RISC_COUNT);
4054 if (NULL != old && (is_active(&old->top, rc) ||
4055 is_active(&old->bottom, rc))) {
4056 btv->framedrop++;
4057 if (debug_latency)
4058 bttv_irq_debug_low_latency(btv, rc);
4059 spin_unlock(&btv->s_lock);
4060 return;
4061 }
4062
4063 /* switch */
4064 btv->cvbi = new;
4065 btv->loop_irq &= ~4;
4066 bttv_buffer_activate_vbi(btv, new);
4067 bttv_set_dma(btv, 0);
4068
Brandon Philips0fc06862007-11-06 20:02:36 -03004069 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 spin_unlock(&btv->s_lock);
4071}
4072
David Howells7d12e782006-10-05 14:55:46 +01004073static irqreturn_t bttv_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074{
4075 u32 stat,astat;
4076 u32 dstat;
4077 int count;
4078 struct bttv *btv;
4079 int handled = 0;
4080
4081 btv=(struct bttv *)dev_id;
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004082
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004083 if (btv->custom_irq)
4084 handled = btv->custom_irq(btv);
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004085
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 count=0;
4087 while (1) {
4088 /* get/clear interrupt status bits */
4089 stat=btread(BT848_INT_STAT);
4090 astat=stat&btread(BT848_INT_MASK);
4091 if (!astat)
4092 break;
4093 handled = 1;
4094 btwrite(stat,BT848_INT_STAT);
4095
4096 /* get device status bits */
4097 dstat=btread(BT848_DSTATUS);
4098
4099 if (irq_debug) {
4100 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4101 "riscs=%x, riscc=%08x, ",
4102 btv->c.nr, count, btv->field_count,
4103 stat>>28, btread(BT848_RISC_COUNT));
4104 bttv_print_irqbits(stat,astat);
4105 if (stat & BT848_INT_HLOCK)
4106 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4107 ? "yes" : "no");
4108 if (stat & BT848_INT_VPRES)
4109 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4110 ? "yes" : "no");
4111 if (stat & BT848_INT_FMTCHG)
4112 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4113 ? "625" : "525");
4114 printk("\n");
4115 }
4116
4117 if (astat&BT848_INT_VSYNC)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004118 btv->field_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004120 if ((astat & BT848_INT_GPINT) && btv->remote) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004122 bttv_input_irq(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 }
4124
4125 if (astat & BT848_INT_I2CDONE) {
4126 btv->i2c_done = stat;
4127 wake_up(&btv->i2c_queue);
4128 }
4129
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004130 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 bttv_irq_switch_vbi(btv);
4132
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004133 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 bttv_irq_wakeup_top(btv);
4135
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004136 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 bttv_irq_switch_video(btv);
4138
4139 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004140 audio_mute(btv, btv->mute); /* trigger automute */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
4142 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4143 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4144 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4145 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4146 btread(BT848_RISC_COUNT));
4147 bttv_print_irqbits(stat,astat);
4148 printk("\n");
4149 if (bttv_debug)
4150 bttv_print_riscaddr(btv);
4151 }
4152 if (fdsr && astat & BT848_INT_FDSR) {
4153 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4154 btv->c.nr,btread(BT848_RISC_COUNT));
4155 if (bttv_debug)
4156 bttv_print_riscaddr(btv);
4157 }
4158
4159 count++;
4160 if (count > 4) {
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004161
4162 if (count > 8 || !(astat & BT848_INT_GPINT)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004163 btwrite(0, BT848_INT_MASK);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004164
4165 printk(KERN_ERR
4166 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4167 } else {
4168 printk(KERN_ERR
4169 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4170
4171 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4172 BT848_INT_MASK);
4173 };
4174
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 bttv_print_irqbits(stat,astat);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004176
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 printk("]\n");
4178 }
4179 }
4180 btv->irq_total++;
4181 if (handled)
4182 btv->irq_me++;
4183 return IRQ_RETVAL(handled);
4184}
4185
4186
4187/* ----------------------------------------------------------------------- */
4188/* initialitation */
4189
4190static struct video_device *vdev_init(struct bttv *btv,
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004191 const struct video_device *template,
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004192 const char *type_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
4194 struct video_device *vfd;
4195
4196 vfd = video_device_alloc();
4197 if (NULL == vfd)
4198 return NULL;
4199 *vfd = *template;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004200 vfd->v4l2_dev = &btv->c.v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 vfd->release = video_device_release;
Mauro Carvalho Chehab1d0a4362008-06-23 12:31:29 -03004202 vfd->debug = bttv_debug;
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004203 video_set_drvdata(vfd, btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4205 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004206 type_name, bttv_tvcards[btv->c.type].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 return vfd;
4208}
4209
4210static void bttv_unregister_video(struct bttv *btv)
4211{
4212 if (btv->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004213 if (video_is_registered(btv->video_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 video_unregister_device(btv->video_dev);
4215 else
4216 video_device_release(btv->video_dev);
4217 btv->video_dev = NULL;
4218 }
4219 if (btv->vbi_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004220 if (video_is_registered(btv->vbi_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 video_unregister_device(btv->vbi_dev);
4222 else
4223 video_device_release(btv->vbi_dev);
4224 btv->vbi_dev = NULL;
4225 }
4226 if (btv->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03004227 if (video_is_registered(btv->radio_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 video_unregister_device(btv->radio_dev);
4229 else
4230 video_device_release(btv->radio_dev);
4231 btv->radio_dev = NULL;
4232 }
4233}
4234
4235/* register video4linux devices */
4236static int __devinit bttv_register_video(struct bttv *btv)
4237{
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004238 if (no_overlay > 0)
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004239 printk("bttv: Overlay support disabled.\n");
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 /* video */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004242 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004243
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004244 if (NULL == btv->video_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004246 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
4247 video_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 goto err;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03004249 printk(KERN_INFO "bttv%d: registered device %s\n",
4250 btv->c.nr, video_device_node_name(btv->video_dev));
Hans Verkuil22a04f12008-07-20 06:35:02 -03004251 if (device_create_file(&btv->video_dev->dev,
Kay Sievers54bd5b62007-10-08 16:26:13 -03004252 &dev_attr_card)<0) {
4253 printk(KERN_ERR "bttv%d: device_create_file 'card' "
Trent Piephod94fc9a2006-07-29 17:18:06 -03004254 "failed\n", btv->c.nr);
4255 goto err;
4256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
4258 /* vbi */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004259 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
Mauro Carvalho Chehab9134be02007-12-27 22:28:31 -03004260
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004261 if (NULL == btv->vbi_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004263 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
4264 vbi_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->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004269 if (!btv->has_radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 return 0;
4271 /* radio */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03004272 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004273 if (NULL == btv->radio_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 goto err;
Jean Delvare176c2f32008-09-07 12:49:59 -03004275 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4276 radio_nr[btv->c.nr]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 goto err;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03004278 printk(KERN_INFO "bttv%d: registered device %s\n",
4279 btv->c.nr, video_device_node_name(btv->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
4281 /* all done */
4282 return 0;
4283
4284 err:
4285 bttv_unregister_video(btv);
4286 return -1;
4287}
4288
4289
4290/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4291/* response on cards with no firmware is not enabled by OF */
4292static void pci_set_command(struct pci_dev *dev)
4293{
4294#if defined(__powerpc__)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004295 unsigned int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004297 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4298 cmd = (cmd | PCI_COMMAND_MEMORY );
4299 pci_write_config_dword(dev, PCI_COMMAND, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300#endif
4301}
4302
4303static int __devinit bttv_probe(struct pci_dev *dev,
4304 const struct pci_device_id *pci_id)
4305{
4306 int result;
4307 unsigned char lat;
4308 struct bttv *btv;
4309
4310 if (bttv_num == BTTV_MAX)
4311 return -ENOMEM;
4312 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004313 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004314 if (btv == NULL) {
4315 printk(KERN_ERR "bttv: out of memory.\n");
4316 return -ENOMEM;
4317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 btv->c.nr = bttv_num;
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004319 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4320 "bttv%d", btv->c.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
4322 /* initialize structs / fill in defaults */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02004323 mutex_init(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004324 spin_lock_init(&btv->s_lock);
4325 spin_lock_init(&btv->gpio_lock);
4326 init_waitqueue_head(&btv->gpioq);
4327 init_waitqueue_head(&btv->i2c_queue);
4328 INIT_LIST_HEAD(&btv->c.subs);
4329 INIT_LIST_HEAD(&btv->capture);
4330 INIT_LIST_HEAD(&btv->vcapture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 v4l2_prio_init(&btv->prio);
4332
4333 init_timer(&btv->timeout);
4334 btv->timeout.function = bttv_irq_timeout;
4335 btv->timeout.data = (unsigned long)btv;
4336
Michael Krufky7c08fb02005-11-08 21:36:21 -08004337 btv->i2c_rc = -1;
4338 btv->tuner_type = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 btv->new_input = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 btv->has_radio=radio[btv->c.nr];
4341
4342 /* pci stuff (init, get irq/mmio, ... */
4343 btv->c.pci = dev;
Michael Krufky7c08fb02005-11-08 21:36:21 -08004344 btv->id = dev->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 if (pci_enable_device(dev)) {
Michael Krufky7c08fb02005-11-08 21:36:21 -08004346 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 btv->c.nr);
4348 return -EIO;
4349 }
Yang Hongyang284901a2009-04-06 19:01:15 -07004350 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004351 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 btv->c.nr);
4353 return -EIO;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 if (!request_mem_region(pci_resource_start(dev,0),
4356 pci_resource_len(dev,0),
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004357 btv->c.v4l2_dev.name)) {
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004358 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4359 btv->c.nr,
4360 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 return -EBUSY;
4362 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004363 pci_set_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 pci_set_command(dev);
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004365
4366 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4367 if (result < 0) {
4368 printk(KERN_WARNING "bttv%d: v4l2_device_register() failed\n", btv->c.nr);
4369 goto fail0;
4370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004372 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4373 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4374 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4375 bttv_num,btv->id, btv->revision, pci_name(dev));
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004376 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4377 btv->c.pci->irq, lat,
4378 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 schedule();
4380
Akinobu Mita5f1693f2006-12-20 10:08:56 -03004381 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4382 if (NULL == btv->bt848_mmio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4384 result = -EIO;
4385 goto fail1;
4386 }
4387
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004388 /* identify card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 bttv_idcard(btv);
4390
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004391 /* disable irqs, register irq handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 btwrite(0, BT848_INT_MASK);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004393 result = request_irq(btv->c.pci->irq, bttv_irq,
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004394 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004395 if (result < 0) {
4396 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 bttv_num,btv->c.pci->irq);
4398 goto fail1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
4401 if (0 != bttv_handle_chipset(btv)) {
4402 result = -EIO;
4403 goto fail2;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
4406 /* init options from insmod args */
4407 btv->opt_combfilter = combfilter;
4408 btv->opt_lumafilter = lumafilter;
4409 btv->opt_automute = automute;
4410 btv->opt_chroma_agc = chroma_agc;
4411 btv->opt_adc_crush = adc_crush;
4412 btv->opt_vcr_hack = vcr_hack;
4413 btv->opt_whitecrush_upper = whitecrush_upper;
4414 btv->opt_whitecrush_lower = whitecrush_lower;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07004415 btv->opt_uv_ratio = uv_ratio;
4416 btv->opt_full_luma_range = full_luma_range;
4417 btv->opt_coring = coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
4419 /* fill struct bttv with some useful defaults */
4420 btv->init.btv = btv;
4421 btv->init.ov.w.width = 320;
4422 btv->init.ov.w.height = 240;
Mauro Carvalho Chehabc96dd072007-10-26 16:51:47 -03004423 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 btv->init.width = 320;
4425 btv->init.height = 240;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 btv->input = 0;
4427
4428 /* initialize hardware */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004429 if (bttv_gpio)
4430 bttv_gpio_tracking(btv,"pre-init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
4432 bttv_risc_init_main(btv);
4433 init_bt848(btv);
4434
4435 /* gpio */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004436 btwrite(0x00, BT848_GPIO_REG_INP);
4437 btwrite(0x00, BT848_GPIO_OUT_EN);
4438 if (bttv_verbose)
4439 bttv_gpio_tracking(btv,"init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004441 /* needs to be done before i2c is registered */
4442 bttv_init_card1(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004444 /* register i2c + gpio */
4445 init_bttv_i2c(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004447 /* some card-specific stuff (needs working i2c) */
4448 bttv_init_card2(btv);
Hans Verkuil2c905772009-07-20 08:14:17 -03004449 bttv_init_tuner(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 init_irqreg(btv);
4451
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004452 /* register video4linux + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 if (!bttv_tvcards[btv->c.type].no_video) {
4454 bttv_register_video(btv);
4455 bt848_bright(btv,32768);
4456 bt848_contrast(btv,32768);
4457 bt848_hue(btv,32768);
4458 bt848_sat(btv,32768);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004459 audio_mute(btv, 1);
Trent Piepho333408f2007-07-03 15:08:10 -03004460 set_input(btv, 0, btv->tvnorm);
Michael Schimeke5bd0262007-01-18 16:17:39 -03004461 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4462 btv->crop[1] = btv->crop[0]; /* current = default */
4463 disclaim_vbi_lines(btv);
4464 disclaim_video_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 }
4466
Jarod Wilsonf992a492007-03-24 15:23:50 -03004467 /* add subdevices and autoload dvb-bt8xx if needed */
4468 if (bttv_tvcards[btv->c.type].has_dvb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 bttv_sub_add_device(&btv->c, "dvb");
Jarod Wilsonf992a492007-03-24 15:23:50 -03004470 request_modules(btv);
4471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004473 bttv_input_init(btv);
4474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 /* everything is fine */
4476 bttv_num++;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004479fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004480 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004482fail1:
4483 v4l2_device_unregister(&btv->c.v4l2_dev);
4484
4485fail0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 if (btv->bt848_mmio)
4487 iounmap(btv->bt848_mmio);
4488 release_mem_region(pci_resource_start(btv->c.pci,0),
4489 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 return result;
4491}
4492
4493static void __devexit bttv_remove(struct pci_dev *pci_dev)
4494{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004495 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4496 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
4498 if (bttv_verbose)
4499 printk("bttv%d: unloading\n",btv->c.nr);
4500
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004501 /* shutdown everything (DMA+IRQs) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 btand(~15, BT848_GPIO_DMA_CTL);
4503 btwrite(0, BT848_INT_MASK);
4504 btwrite(~0x0, BT848_INT_STAT);
4505 btwrite(0x0, BT848_GPIO_OUT_EN);
4506 if (bttv_gpio)
4507 bttv_gpio_tracking(btv,"cleanup");
4508
4509 /* tell gpio modules we are leaving ... */
4510 btv->shutdown=1;
4511 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004512 bttv_input_fini(btv);
Christopher Pascoe889aee82006-01-09 15:25:28 -02004513 bttv_sub_del_devices(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004515 /* unregister i2c_bus + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 fini_bttv_i2c(btv);
4517
4518 /* unregister video4linux */
4519 bttv_unregister_video(btv);
4520
4521 /* free allocated memory */
4522 btcx_riscmem_free(btv->c.pci,&btv->main);
4523
4524 /* free ressources */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004525 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 iounmap(btv->bt848_mmio);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004527 release_mem_region(pci_resource_start(btv->c.pci,0),
4528 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004530 v4l2_device_unregister(&btv->c.v4l2_dev);
Trent Piepho4b10d3b2009-01-28 21:32:59 -03004531 bttvs[btv->c.nr] = NULL;
4532 kfree(btv);
4533
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004534 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535}
4536
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004537#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4539{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004540 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4541 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 struct bttv_buffer_set idle;
4543 unsigned long flags;
4544
Mauro Carvalho Chehab0f97a932005-09-09 13:04:05 -07004545 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
4547 /* stop dma + irqs */
4548 spin_lock_irqsave(&btv->s_lock,flags);
4549 memset(&idle, 0, sizeof(idle));
4550 btv->state.video = btv->curr;
4551 btv->state.vbi = btv->cvbi;
4552 btv->state.loop_irq = btv->loop_irq;
4553 btv->curr = idle;
4554 btv->loop_irq = 0;
4555 bttv_buffer_activate_video(btv, &idle);
4556 bttv_buffer_activate_vbi(btv, NULL);
4557 bttv_set_dma(btv, 0);
4558 btwrite(0, BT848_INT_MASK);
4559 spin_unlock_irqrestore(&btv->s_lock,flags);
4560
4561 /* save bt878 state */
4562 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4563 btv->state.gpio_data = gpio_read();
4564
4565 /* save pci state */
4566 pci_save_state(pci_dev);
4567 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4568 pci_disable_device(pci_dev);
4569 btv->state.disabled = 1;
4570 }
4571 return 0;
4572}
4573
4574static int bttv_resume(struct pci_dev *pci_dev)
4575{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -03004576 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4577 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004579 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
4581 dprintk("bttv%d: resume\n", btv->c.nr);
4582
4583 /* restore pci state */
4584 if (btv->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004585 err=pci_enable_device(pci_dev);
4586 if (err) {
4587 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4588 btv->c.nr);
4589 return err;
4590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 btv->state.disabled = 0;
4592 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004593 err=pci_set_power_state(pci_dev, PCI_D0);
4594 if (err) {
4595 pci_disable_device(pci_dev);
4596 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4597 btv->c.nr);
4598 btv->state.disabled = 1;
4599 return err;
4600 }
4601
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 pci_restore_state(pci_dev);
4603
4604 /* restore bt878 state */
4605 bttv_reinit_bt848(btv);
4606 gpio_inout(0xffffff, btv->state.gpio_enable);
4607 gpio_write(btv->state.gpio_data);
4608
4609 /* restart dma */
4610 spin_lock_irqsave(&btv->s_lock,flags);
4611 btv->curr = btv->state.video;
4612 btv->cvbi = btv->state.vbi;
4613 btv->loop_irq = btv->state.loop_irq;
4614 bttv_buffer_activate_video(btv, &btv->curr);
4615 bttv_buffer_activate_vbi(btv, btv->cvbi);
4616 bttv_set_dma(btv, 0);
4617 spin_unlock_irqrestore(&btv->s_lock,flags);
4618 return 0;
4619}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004620#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
4622static struct pci_device_id bttv_pci_tbl[] = {
Joe Perches76e97412009-07-05 15:59:21 -03004623 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4624 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4625 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4626 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004627 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628};
4629
4630MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4631
4632static struct pci_driver bttv_pci_driver = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004633 .name = "bttv",
4634 .id_table = bttv_pci_tbl,
4635 .probe = bttv_probe,
4636 .remove = __devexit_p(bttv_remove),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004637#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 .suspend = bttv_suspend,
4639 .resume = bttv_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004640#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641};
4642
Adrian Bunk7d44e892007-12-11 19:23:43 -03004643static int __init bttv_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644{
Randy Dunlapc526e222006-07-15 09:08:26 -03004645 int ret;
4646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 bttv_num = 0;
4648
4649 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4650 (BTTV_VERSION_CODE >> 16) & 0xff,
4651 (BTTV_VERSION_CODE >> 8) & 0xff,
4652 BTTV_VERSION_CODE & 0xff);
4653#ifdef SNAPSHOT
4654 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4655 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4656#endif
4657 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4658 gbuffers = 2;
Filipe Rossetf41b6962009-05-28 11:11:53 -03004659 if (gbufsize > BTTV_MAX_FBUF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 gbufsize = BTTV_MAX_FBUF;
4661 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4662 if (bttv_verbose)
4663 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4664 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4665
4666 bttv_check_chipset();
4667
Randy Dunlapc526e222006-07-15 09:08:26 -03004668 ret = bus_register(&bttv_sub_bus_type);
4669 if (ret < 0) {
4670 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4671 return ret;
4672 }
Akinobu Mita9e7e85e2007-12-17 14:26:29 -03004673 ret = pci_register_driver(&bttv_pci_driver);
4674 if (ret < 0)
4675 bus_unregister(&bttv_sub_bus_type);
4676
4677 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678}
4679
Adrian Bunk7d44e892007-12-11 19:23:43 -03004680static void __exit bttv_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681{
4682 pci_unregister_driver(&bttv_pci_driver);
4683 bus_unregister(&bttv_sub_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
4686module_init(bttv_init_module);
4687module_exit(bttv_cleanup_module);
4688
4689/*
4690 * Local variables:
4691 * c-basic-offset: 8
4692 * End:
4693 */