blob: cb50fde2be8628f943080dc5cf61e4215c791d10 [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
Michael Schimeke5bd0262007-01-18 16:17:39 -030015 Cropping and overscan support
16 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
17 Sponsored by OPQ Systems AB
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
23
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
28
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32*/
33
34#include <linux/init.h>
35#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/errno.h>
38#include <linux/fs.h>
39#include <linux/kernel.h>
40#include <linux/sched.h>
41#include <linux/interrupt.h>
42#include <linux/kdev_t.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020043#include "bttvp.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020044#include <media/v4l2-common.h>
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -030045#include <media/tvaudio.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030046#include <media/msp3400.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020047
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -070048#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/io.h>
51#include <asm/byteorder.h>
52
Mauro Carvalho Chehabfa3fcce2006-03-23 21:45:24 -030053#include <media/rds.h>
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070054
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056unsigned int bttv_num; /* number of Bt848s in use */
57struct bttv bttvs[BTTV_MAX];
58
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020059unsigned int bttv_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060unsigned int bttv_verbose = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020061unsigned int bttv_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* config variables */
64#ifdef __BIG_ENDIAN
65static unsigned int bigendian=1;
66#else
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020067static unsigned int bigendian;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#endif
69static unsigned int radio[BTTV_MAX];
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020070static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static unsigned int gbuffers = 8;
72static unsigned int gbufsize = 0x208000;
Michael Schimeke5bd0262007-01-18 16:17:39 -030073static unsigned int reset_crop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75static int video_nr = -1;
76static int radio_nr = -1;
77static int vbi_nr = -1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020078static int debug_latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020080static unsigned int fdsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* options */
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020083static unsigned int combfilter;
84static unsigned int lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static unsigned int automute = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020086static unsigned int chroma_agc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static unsigned int adc_crush = 1;
88static unsigned int whitecrush_upper = 0xCF;
89static unsigned int whitecrush_lower = 0x7F;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020090static unsigned int vcr_hack;
91static unsigned int irq_iswitch;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -070092static unsigned int uv_ratio = 50;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020093static unsigned int full_luma_range;
94static unsigned int coring;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -070095extern int no_overlay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* API features (turn on/off stuff for testing) */
98static unsigned int v4l2 = 1;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/* insmod args */
101module_param(bttv_verbose, int, 0644);
102module_param(bttv_gpio, int, 0644);
103module_param(bttv_debug, int, 0644);
104module_param(irq_debug, int, 0644);
105module_param(debug_latency, int, 0644);
106
107module_param(fdsr, int, 0444);
108module_param(video_nr, int, 0444);
109module_param(radio_nr, int, 0444);
110module_param(vbi_nr, int, 0444);
111module_param(gbuffers, int, 0444);
112module_param(gbufsize, int, 0444);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300113module_param(reset_crop, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115module_param(v4l2, int, 0644);
116module_param(bigendian, int, 0644);
117module_param(irq_iswitch, int, 0644);
118module_param(combfilter, int, 0444);
119module_param(lumafilter, int, 0444);
120module_param(automute, int, 0444);
121module_param(chroma_agc, int, 0444);
122module_param(adc_crush, int, 0444);
123module_param(whitecrush_upper, int, 0444);
124module_param(whitecrush_lower, int, 0444);
125module_param(vcr_hack, int, 0444);
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700126module_param(uv_ratio, int, 0444);
127module_param(full_luma_range, int, 0444);
128module_param(coring, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130module_param_array(radio, int, NULL, 0444);
131
132MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
133MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
134MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
135MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
136MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
137MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
138MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
139MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
Michael Schimeke5bd0262007-01-18 16:17:39 -0300140MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
141 "is 1 (yes) for compatibility with older applications");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
143MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
144MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
145MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
146MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
147MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
148MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700149MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
150MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
151MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
154MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
155MODULE_LICENSE("GPL");
156
157/* ----------------------------------------------------------------------- */
158/* sysfs */
159
Kay Sievers54bd5b62007-10-08 16:26:13 -0300160static ssize_t show_card(struct device *cd,
161 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct video_device *vfd = to_video_device(cd);
164 struct bttv *btv = dev_get_drvdata(vfd->dev);
165 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
166}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300167static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169/* ----------------------------------------------------------------------- */
Jarod Wilsonf992a492007-03-24 15:23:50 -0300170/* dvb auto-load setup */
171#if defined(CONFIG_MODULES) && defined(MODULE)
172static void request_module_async(struct work_struct *work)
173{
174 request_module("dvb-bt8xx");
175}
176
177static void request_modules(struct bttv *dev)
178{
179 INIT_WORK(&dev->request_module_wk, request_module_async);
180 schedule_work(&dev->request_module_wk);
181}
182#else
183#define request_modules(dev)
184#endif /* CONFIG_MODULES */
185
186
187/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/* static data */
189
190/* special timing tables from conexant... */
191static u8 SRAM_Table[][60] =
192{
193 /* PAL digital input over GPIO[7:0] */
194 {
195 45, // 45 bytes following
196 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
197 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
198 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
199 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
200 0x37,0x00,0xAF,0x21,0x00
201 },
202 /* NTSC digital input over GPIO[7:0] */
203 {
204 51, // 51 bytes following
205 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
206 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
207 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
208 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
209 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
210 0x00,
211 },
212 // TGB_NTSC392 // quartzsight
213 // This table has been modified to be used for Fusion Rev D
214 {
215 0x2A, // size of table = 42
216 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
217 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
218 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
219 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
220 0x20, 0x00
221 }
222};
223
Michael Schimeke5bd0262007-01-18 16:17:39 -0300224/* minhdelayx1 first video pixel we can capture on a line and
225 hdelayx1 start of active video, both relative to rising edge of
226 /HRESET pulse (0H) in 1 / fCLKx1.
227 swidth width of active video and
228 totalwidth total line width, both in 1 / fCLKx1.
229 sqwidth total line width in square pixels.
230 vdelay start of active video in 2 * field lines relative to
231 trailing edge of /VRESET pulse (VDELAY register).
232 sheight height of active video in 2 * field lines.
233 videostart0 ITU-R frame line number of the line corresponding
234 to vdelay in the first field. */
235#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
236 vdelay, sheight, videostart0) \
237 .cropcap.bounds.left = minhdelayx1, \
238 /* * 2 because vertically we count field lines times two, */ \
239 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
240 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
241 /* 4 is a safety margin at the end of the line. */ \
242 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
243 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
244 .cropcap.defrect.left = hdelayx1, \
245 .cropcap.defrect.top = (videostart0) * 2, \
246 .cropcap.defrect.width = swidth, \
247 .cropcap.defrect.height = sheight, \
248 .cropcap.pixelaspect.numerator = totalwidth, \
249 .cropcap.pixelaspect.denominator = sqwidth,
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251const struct bttv_tvnorm bttv_tvnorms[] = {
252 /* PAL-BDGHI */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800253 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
254 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 {
256 .v4l2_id = V4L2_STD_PAL,
257 .name = "PAL",
258 .Fsc = 35468950,
259 .swidth = 924,
260 .sheight = 576,
261 .totalwidth = 1135,
262 .adelay = 0x7f,
263 .bdelay = 0x72,
264 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
265 .scaledtwidth = 1135,
266 .hdelayx1 = 186,
267 .hactivex1 = 924,
268 .vdelay = 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300269 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .sram = 0,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200271 /* ITU-R frame line number of the first VBI line
Michael Schimeke5bd0262007-01-18 16:17:39 -0300272 we can capture, of the first and second field.
273 The last line is determined by cropcap.bounds. */
274 .vbistart = { 7, 320 },
275 CROPCAP(/* minhdelayx1 */ 68,
276 /* hdelayx1 */ 186,
277 /* Should be (768 * 1135 + 944 / 2) / 944.
278 cropcap.defrect is used for image width
279 checks, so we keep the old value 924. */
280 /* swidth */ 924,
281 /* totalwidth */ 1135,
282 /* sqwidth */ 944,
283 /* vdelay */ 0x20,
284 /* sheight */ 576,
285 /* videostart0 */ 23)
286 /* bt878 (and bt848?) can capture another
287 line below active video. */
288 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 },{
Hans Verkuild97a11e2006-02-07 06:48:40 -0200290 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 .name = "NTSC",
292 .Fsc = 28636363,
293 .swidth = 768,
294 .sheight = 480,
295 .totalwidth = 910,
296 .adelay = 0x68,
297 .bdelay = 0x5d,
298 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
299 .scaledtwidth = 910,
300 .hdelayx1 = 128,
301 .hactivex1 = 910,
302 .vdelay = 0x1a,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300303 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 .sram = 1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200305 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300306 CROPCAP(/* minhdelayx1 */ 68,
307 /* hdelayx1 */ 128,
308 /* Should be (640 * 910 + 780 / 2) / 780? */
309 /* swidth */ 768,
310 /* totalwidth */ 910,
311 /* sqwidth */ 780,
312 /* vdelay */ 0x1a,
313 /* sheight */ 480,
314 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 },{
316 .v4l2_id = V4L2_STD_SECAM,
317 .name = "SECAM",
318 .Fsc = 35468950,
319 .swidth = 924,
320 .sheight = 576,
321 .totalwidth = 1135,
322 .adelay = 0x7f,
323 .bdelay = 0xb0,
324 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
325 .scaledtwidth = 1135,
326 .hdelayx1 = 186,
327 .hactivex1 = 922,
328 .vdelay = 0x20,
329 .vbipack = 255,
330 .sram = 0, /* like PAL, correct? */
Michael H. Schimek67f15702006-01-09 15:25:27 -0200331 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300332 CROPCAP(/* minhdelayx1 */ 68,
333 /* hdelayx1 */ 186,
334 /* swidth */ 924,
335 /* totalwidth */ 1135,
336 /* sqwidth */ 944,
337 /* vdelay */ 0x20,
338 /* sheight */ 576,
339 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 },{
341 .v4l2_id = V4L2_STD_PAL_Nc,
342 .name = "PAL-Nc",
343 .Fsc = 28636363,
344 .swidth = 640,
345 .sheight = 576,
346 .totalwidth = 910,
347 .adelay = 0x68,
348 .bdelay = 0x5d,
349 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
350 .scaledtwidth = 780,
351 .hdelayx1 = 130,
352 .hactivex1 = 734,
353 .vdelay = 0x1a,
354 .vbipack = 144,
355 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200356 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300357 CROPCAP(/* minhdelayx1 */ 68,
358 /* hdelayx1 */ 130,
359 /* swidth */ (640 * 910 + 780 / 2) / 780,
360 /* totalwidth */ 910,
361 /* sqwidth */ 780,
362 /* vdelay */ 0x1a,
363 /* sheight */ 576,
364 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 },{
366 .v4l2_id = V4L2_STD_PAL_M,
367 .name = "PAL-M",
368 .Fsc = 28636363,
369 .swidth = 640,
370 .sheight = 480,
371 .totalwidth = 910,
372 .adelay = 0x68,
373 .bdelay = 0x5d,
374 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
375 .scaledtwidth = 780,
376 .hdelayx1 = 135,
377 .hactivex1 = 754,
378 .vdelay = 0x1a,
379 .vbipack = 144,
380 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200381 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300382 CROPCAP(/* minhdelayx1 */ 68,
383 /* hdelayx1 */ 135,
384 /* swidth */ (640 * 910 + 780 / 2) / 780,
385 /* totalwidth */ 910,
386 /* sqwidth */ 780,
387 /* vdelay */ 0x1a,
388 /* sheight */ 480,
389 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 },{
391 .v4l2_id = V4L2_STD_PAL_N,
392 .name = "PAL-N",
393 .Fsc = 35468950,
394 .swidth = 768,
395 .sheight = 576,
396 .totalwidth = 1135,
397 .adelay = 0x7f,
398 .bdelay = 0x72,
399 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
400 .scaledtwidth = 944,
401 .hdelayx1 = 186,
402 .hactivex1 = 922,
403 .vdelay = 0x20,
404 .vbipack = 144,
405 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300406 .vbistart = { 7, 320 },
407 CROPCAP(/* minhdelayx1 */ 68,
408 /* hdelayx1 */ 186,
409 /* swidth */ (768 * 1135 + 944 / 2) / 944,
410 /* totalwidth */ 1135,
411 /* sqwidth */ 944,
412 /* vdelay */ 0x20,
413 /* sheight */ 576,
414 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 },{
416 .v4l2_id = V4L2_STD_NTSC_M_JP,
417 .name = "NTSC-JP",
418 .Fsc = 28636363,
419 .swidth = 640,
420 .sheight = 480,
421 .totalwidth = 910,
422 .adelay = 0x68,
423 .bdelay = 0x5d,
424 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
425 .scaledtwidth = 780,
426 .hdelayx1 = 135,
427 .hactivex1 = 754,
428 .vdelay = 0x16,
429 .vbipack = 144,
430 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300431 .vbistart = { 10, 273 },
432 CROPCAP(/* minhdelayx1 */ 68,
433 /* hdelayx1 */ 135,
434 /* swidth */ (640 * 910 + 780 / 2) / 780,
435 /* totalwidth */ 910,
436 /* sqwidth */ 780,
437 /* vdelay */ 0x16,
438 /* sheight */ 480,
439 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 },{
441 /* that one hopefully works with the strange timing
442 * which video recorders produce when playing a NTSC
443 * tape on a PAL TV ... */
444 .v4l2_id = V4L2_STD_PAL_60,
445 .name = "PAL-60",
446 .Fsc = 35468950,
447 .swidth = 924,
448 .sheight = 480,
449 .totalwidth = 1135,
450 .adelay = 0x7f,
451 .bdelay = 0x72,
452 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
453 .scaledtwidth = 1135,
454 .hdelayx1 = 186,
455 .hactivex1 = 924,
456 .vdelay = 0x1a,
457 .vbipack = 255,
458 .vtotal = 524,
459 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200460 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300461 CROPCAP(/* minhdelayx1 */ 68,
462 /* hdelayx1 */ 186,
463 /* swidth */ 924,
464 /* totalwidth */ 1135,
465 /* sqwidth */ 944,
466 /* vdelay */ 0x1a,
467 /* sheight */ 480,
468 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470};
471static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
472
473/* ----------------------------------------------------------------------- */
474/* bttv format list
475 packed pixel formats must come first */
476static const struct bttv_format bttv_formats[] = {
477 {
478 .name = "8 bpp, gray",
479 .palette = VIDEO_PALETTE_GREY,
480 .fourcc = V4L2_PIX_FMT_GREY,
481 .btformat = BT848_COLOR_FMT_Y8,
482 .depth = 8,
483 .flags = FORMAT_FLAGS_PACKED,
484 },{
485 .name = "8 bpp, dithered color",
486 .palette = VIDEO_PALETTE_HI240,
487 .fourcc = V4L2_PIX_FMT_HI240,
488 .btformat = BT848_COLOR_FMT_RGB8,
489 .depth = 8,
490 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
491 },{
492 .name = "15 bpp RGB, le",
493 .palette = VIDEO_PALETTE_RGB555,
494 .fourcc = V4L2_PIX_FMT_RGB555,
495 .btformat = BT848_COLOR_FMT_RGB15,
496 .depth = 16,
497 .flags = FORMAT_FLAGS_PACKED,
498 },{
499 .name = "15 bpp RGB, be",
500 .palette = -1,
501 .fourcc = V4L2_PIX_FMT_RGB555X,
502 .btformat = BT848_COLOR_FMT_RGB15,
503 .btswap = 0x03, /* byteswap */
504 .depth = 16,
505 .flags = FORMAT_FLAGS_PACKED,
506 },{
507 .name = "16 bpp RGB, le",
508 .palette = VIDEO_PALETTE_RGB565,
509 .fourcc = V4L2_PIX_FMT_RGB565,
510 .btformat = BT848_COLOR_FMT_RGB16,
511 .depth = 16,
512 .flags = FORMAT_FLAGS_PACKED,
513 },{
514 .name = "16 bpp RGB, be",
515 .palette = -1,
516 .fourcc = V4L2_PIX_FMT_RGB565X,
517 .btformat = BT848_COLOR_FMT_RGB16,
518 .btswap = 0x03, /* byteswap */
519 .depth = 16,
520 .flags = FORMAT_FLAGS_PACKED,
521 },{
522 .name = "24 bpp RGB, le",
523 .palette = VIDEO_PALETTE_RGB24,
524 .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",
530 .palette = VIDEO_PALETTE_RGB32,
531 .fourcc = V4L2_PIX_FMT_BGR32,
532 .btformat = BT848_COLOR_FMT_RGB32,
533 .depth = 32,
534 .flags = FORMAT_FLAGS_PACKED,
535 },{
536 .name = "32 bpp RGB, be",
537 .palette = -1,
538 .fourcc = V4L2_PIX_FMT_RGB32,
539 .btformat = BT848_COLOR_FMT_RGB32,
540 .btswap = 0x0f, /* byte+word swap */
541 .depth = 32,
542 .flags = FORMAT_FLAGS_PACKED,
543 },{
544 .name = "4:2:2, packed, YUYV",
545 .palette = VIDEO_PALETTE_YUV422,
546 .fourcc = V4L2_PIX_FMT_YUYV,
547 .btformat = BT848_COLOR_FMT_YUY2,
548 .depth = 16,
549 .flags = FORMAT_FLAGS_PACKED,
550 },{
551 .name = "4:2:2, packed, YUYV",
552 .palette = VIDEO_PALETTE_YUYV,
553 .fourcc = V4L2_PIX_FMT_YUYV,
554 .btformat = BT848_COLOR_FMT_YUY2,
555 .depth = 16,
556 .flags = FORMAT_FLAGS_PACKED,
557 },{
558 .name = "4:2:2, packed, UYVY",
559 .palette = VIDEO_PALETTE_UYVY,
560 .fourcc = V4L2_PIX_FMT_UYVY,
561 .btformat = BT848_COLOR_FMT_YUY2,
562 .btswap = 0x03, /* byteswap */
563 .depth = 16,
564 .flags = FORMAT_FLAGS_PACKED,
565 },{
566 .name = "4:2:2, planar, Y-Cb-Cr",
567 .palette = VIDEO_PALETTE_YUV422P,
568 .fourcc = V4L2_PIX_FMT_YUV422P,
569 .btformat = BT848_COLOR_FMT_YCrCb422,
570 .depth = 16,
571 .flags = FORMAT_FLAGS_PLANAR,
572 .hshift = 1,
573 .vshift = 0,
574 },{
575 .name = "4:2:0, planar, Y-Cb-Cr",
576 .palette = VIDEO_PALETTE_YUV420P,
577 .fourcc = V4L2_PIX_FMT_YUV420,
578 .btformat = BT848_COLOR_FMT_YCrCb422,
579 .depth = 12,
580 .flags = FORMAT_FLAGS_PLANAR,
581 .hshift = 1,
582 .vshift = 1,
583 },{
584 .name = "4:2:0, planar, Y-Cr-Cb",
585 .palette = -1,
586 .fourcc = V4L2_PIX_FMT_YVU420,
587 .btformat = BT848_COLOR_FMT_YCrCb422,
588 .depth = 12,
589 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
590 .hshift = 1,
591 .vshift = 1,
592 },{
593 .name = "4:1:1, planar, Y-Cb-Cr",
594 .palette = VIDEO_PALETTE_YUV411P,
595 .fourcc = V4L2_PIX_FMT_YUV411P,
596 .btformat = BT848_COLOR_FMT_YCrCb411,
597 .depth = 12,
598 .flags = FORMAT_FLAGS_PLANAR,
599 .hshift = 2,
600 .vshift = 0,
601 },{
602 .name = "4:1:0, planar, Y-Cb-Cr",
603 .palette = VIDEO_PALETTE_YUV410P,
604 .fourcc = V4L2_PIX_FMT_YUV410,
605 .btformat = BT848_COLOR_FMT_YCrCb411,
606 .depth = 9,
607 .flags = FORMAT_FLAGS_PLANAR,
608 .hshift = 2,
609 .vshift = 2,
610 },{
611 .name = "4:1:0, planar, Y-Cr-Cb",
612 .palette = -1,
613 .fourcc = V4L2_PIX_FMT_YVU410,
614 .btformat = BT848_COLOR_FMT_YCrCb411,
615 .depth = 9,
616 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
617 .hshift = 2,
618 .vshift = 2,
619 },{
620 .name = "raw scanlines",
621 .palette = VIDEO_PALETTE_RAW,
622 .fourcc = -1,
623 .btformat = BT848_COLOR_FMT_RAW,
624 .depth = 8,
625 .flags = FORMAT_FLAGS_RAW,
626 }
627};
628static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
629
630/* ----------------------------------------------------------------------- */
631
632#define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
633#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
634#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
635#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
636#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
637#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
638#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
639#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700640#define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
641#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
642#define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
643#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645static const struct v4l2_queryctrl no_ctl = {
646 .name = "42",
647 .flags = V4L2_CTRL_FLAG_DISABLED,
648};
649static const struct v4l2_queryctrl bttv_ctls[] = {
650 /* --- video --- */
651 {
652 .id = V4L2_CID_BRIGHTNESS,
653 .name = "Brightness",
654 .minimum = 0,
655 .maximum = 65535,
656 .step = 256,
657 .default_value = 32768,
658 .type = V4L2_CTRL_TYPE_INTEGER,
659 },{
660 .id = V4L2_CID_CONTRAST,
661 .name = "Contrast",
662 .minimum = 0,
663 .maximum = 65535,
664 .step = 128,
665 .default_value = 32768,
666 .type = V4L2_CTRL_TYPE_INTEGER,
667 },{
668 .id = V4L2_CID_SATURATION,
669 .name = "Saturation",
670 .minimum = 0,
671 .maximum = 65535,
672 .step = 128,
673 .default_value = 32768,
674 .type = V4L2_CTRL_TYPE_INTEGER,
675 },{
676 .id = V4L2_CID_HUE,
677 .name = "Hue",
678 .minimum = 0,
679 .maximum = 65535,
680 .step = 256,
681 .default_value = 32768,
682 .type = V4L2_CTRL_TYPE_INTEGER,
683 },
684 /* --- audio --- */
685 {
686 .id = V4L2_CID_AUDIO_MUTE,
687 .name = "Mute",
688 .minimum = 0,
689 .maximum = 1,
690 .type = V4L2_CTRL_TYPE_BOOLEAN,
691 },{
692 .id = V4L2_CID_AUDIO_VOLUME,
693 .name = "Volume",
694 .minimum = 0,
695 .maximum = 65535,
696 .step = 65535/100,
697 .default_value = 65535,
698 .type = V4L2_CTRL_TYPE_INTEGER,
699 },{
700 .id = V4L2_CID_AUDIO_BALANCE,
701 .name = "Balance",
702 .minimum = 0,
703 .maximum = 65535,
704 .step = 65535/100,
705 .default_value = 32768,
706 .type = V4L2_CTRL_TYPE_INTEGER,
707 },{
708 .id = V4L2_CID_AUDIO_BASS,
709 .name = "Bass",
710 .minimum = 0,
711 .maximum = 65535,
712 .step = 65535/100,
713 .default_value = 32768,
714 .type = V4L2_CTRL_TYPE_INTEGER,
715 },{
716 .id = V4L2_CID_AUDIO_TREBLE,
717 .name = "Treble",
718 .minimum = 0,
719 .maximum = 65535,
720 .step = 65535/100,
721 .default_value = 32768,
722 .type = V4L2_CTRL_TYPE_INTEGER,
723 },
724 /* --- private --- */
725 {
726 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
727 .name = "chroma agc",
728 .minimum = 0,
729 .maximum = 1,
730 .type = V4L2_CTRL_TYPE_BOOLEAN,
731 },{
732 .id = V4L2_CID_PRIVATE_COMBFILTER,
733 .name = "combfilter",
734 .minimum = 0,
735 .maximum = 1,
736 .type = V4L2_CTRL_TYPE_BOOLEAN,
737 },{
738 .id = V4L2_CID_PRIVATE_AUTOMUTE,
739 .name = "automute",
740 .minimum = 0,
741 .maximum = 1,
742 .type = V4L2_CTRL_TYPE_BOOLEAN,
743 },{
744 .id = V4L2_CID_PRIVATE_LUMAFILTER,
745 .name = "luma decimation filter",
746 .minimum = 0,
747 .maximum = 1,
748 .type = V4L2_CTRL_TYPE_BOOLEAN,
749 },{
750 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
751 .name = "agc crush",
752 .minimum = 0,
753 .maximum = 1,
754 .type = V4L2_CTRL_TYPE_BOOLEAN,
755 },{
756 .id = V4L2_CID_PRIVATE_VCR_HACK,
757 .name = "vcr hack",
758 .minimum = 0,
759 .maximum = 1,
760 .type = V4L2_CTRL_TYPE_BOOLEAN,
761 },{
762 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
763 .name = "whitecrush upper",
764 .minimum = 0,
765 .maximum = 255,
766 .step = 1,
767 .default_value = 0xCF,
768 .type = V4L2_CTRL_TYPE_INTEGER,
769 },{
770 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
771 .name = "whitecrush lower",
772 .minimum = 0,
773 .maximum = 255,
774 .step = 1,
775 .default_value = 0x7F,
776 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700777 },{
778 .id = V4L2_CID_PRIVATE_UV_RATIO,
779 .name = "uv ratio",
780 .minimum = 0,
781 .maximum = 100,
782 .step = 1,
783 .default_value = 50,
784 .type = V4L2_CTRL_TYPE_INTEGER,
785 },{
786 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
787 .name = "full luma range",
788 .minimum = 0,
789 .maximum = 1,
790 .type = V4L2_CTRL_TYPE_BOOLEAN,
791 },{
792 .id = V4L2_CID_PRIVATE_CORING,
793 .name = "coring",
794 .minimum = 0,
795 .maximum = 3,
796 .step = 1,
797 .default_value = 0,
798 .type = V4L2_CTRL_TYPE_INTEGER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700801
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803};
804static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
805
806/* ----------------------------------------------------------------------- */
807/* resource management */
808
Michael Schimeke5bd0262007-01-18 16:17:39 -0300809/*
810 RESOURCE_ allocated by freed by
811
812 VIDEO_READ bttv_read 1) bttv_read 2)
813
814 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
815 VIDIOC_QBUF 1) bttv_release
816 VIDIOCMCAPTURE 1)
817
818 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
819 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
820 3) bttv_release
821
822 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
823 VIDIOC_QBUF 1) bttv_release
824 bttv_read, bttv_poll 1) 4)
825
826 1) The resource must be allocated when we enter buffer prepare functions
827 and remain allocated while buffers are in the DMA queue.
828 2) This is a single frame read.
829 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
830 RESOURCE_OVERLAY is allocated.
831 4) This is a continuous read, implies VIDIOC_STREAMON.
832
833 Note this driver permits video input and standard changes regardless if
834 resources are allocated.
835*/
836
837#define VBI_RESOURCES (RESOURCE_VBI)
838#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
839 RESOURCE_VIDEO_STREAM | \
840 RESOURCE_OVERLAY)
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842static
843int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
844{
Michael Schimeke5bd0262007-01-18 16:17:39 -0300845 int xbits; /* mutual exclusive resources */
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (fh->resources & bit)
848 /* have it already allocated */
849 return 1;
850
Michael Schimeke5bd0262007-01-18 16:17:39 -0300851 xbits = bit;
852 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
853 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /* is it free? */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300856 mutex_lock(&btv->lock);
857 if (btv->resources & xbits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* no, someone else uses it */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300859 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300861
862 if ((bit & VIDEO_RESOURCES)
863 && 0 == (btv->resources & VIDEO_RESOURCES)) {
864 /* Do crop - use current, don't - use default parameters. */
865 __s32 top = btv->crop[!!fh->do_crop].rect.top;
866
867 if (btv->vbi_end > top)
868 goto fail;
869
870 /* We cannot capture the same line as video and VBI data.
871 Claim scan lines crop[].rect.top to bottom. */
872 btv->crop_start = top;
873 } else if (bit & VBI_RESOURCES) {
874 __s32 end = fh->vbi_fmt.end;
875
876 if (end > btv->crop_start)
877 goto fail;
878
879 /* Claim scan lines above fh->vbi_fmt.end. */
880 btv->vbi_end = end;
881 }
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /* it's free, grab it */
884 fh->resources |= bit;
885 btv->resources |= bit;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300886 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return 1;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300888
889 fail:
890 mutex_unlock(&btv->lock);
891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894static
895int check_btres(struct bttv_fh *fh, int bit)
896{
897 return (fh->resources & bit);
898}
899
900static
901int locked_btres(struct bttv *btv, int bit)
902{
903 return (btv->resources & bit);
904}
905
Michael Schimeke5bd0262007-01-18 16:17:39 -0300906/* Call with btv->lock down. */
907static void
908disclaim_vbi_lines(struct bttv *btv)
909{
910 btv->vbi_end = 0;
911}
912
913/* Call with btv->lock down. */
914static void
915disclaim_video_lines(struct bttv *btv)
916{
917 const struct bttv_tvnorm *tvnorm;
918 u8 crop;
919
920 tvnorm = &bttv_tvnorms[btv->tvnorm];
921 btv->crop_start = tvnorm->cropcap.bounds.top
922 + tvnorm->cropcap.bounds.height;
923
924 /* VBI capturing ends at VDELAY, start of video capturing, no
925 matter how many lines the VBI RISC program expects. When video
926 capturing is off, it shall no longer "preempt" VBI capturing,
927 so we set VDELAY to maximum. */
928 crop = btread(BT848_E_CROP) | 0xc0;
929 btwrite(crop, BT848_E_CROP);
930 btwrite(0xfe, BT848_E_VDELAY_LO);
931 btwrite(crop, BT848_O_CROP);
932 btwrite(0xfe, BT848_O_VDELAY_LO);
933}
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935static
936void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
937{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if ((fh->resources & bits) != bits) {
939 /* trying to free ressources not allocated by us ... */
940 printk("bttv: BUG! (btres)\n");
941 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300942 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 fh->resources &= ~bits;
944 btv->resources &= ~bits;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300945
946 bits = btv->resources;
947
948 if (0 == (bits & VIDEO_RESOURCES))
949 disclaim_video_lines(btv);
950
951 if (0 == (bits & VBI_RESOURCES))
952 disclaim_vbi_lines(btv);
953
954 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957/* ----------------------------------------------------------------------- */
958/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
959
960/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
961 PLL_X = Reference pre-divider (0=1, 1=2)
962 PLL_C = Post divider (0=6, 1=4)
963 PLL_I = Integer input
964 PLL_F = Fractional input
965
966 F_input = 28.636363 MHz:
967 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
968*/
969
970static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
971{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800972 unsigned char fl, fh, fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800974 /* prevent overflows */
975 fin/=4;
976 fout/=4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800978 fout*=12;
979 fi=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800981 fout=(fout%fin)*256;
982 fh=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800984 fout=(fout%fin)*256;
985 fl=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800987 btwrite(fl, BT848_PLL_F_LO);
988 btwrite(fh, BT848_PLL_F_HI);
989 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992static void set_pll(struct bttv *btv)
993{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800994 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800996 if (!btv->pll.pll_crystal)
997 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
1000 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001001 return;
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001004 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1005 /* no PLL needed */
1006 if (btv->pll.pll_current == 0)
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001007 return;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001008 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001009 btv->c.nr,btv->pll.pll_ifreq);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001010 btwrite(0x00,BT848_TGCTRL);
1011 btwrite(0x00,BT848_PLL_XCI);
1012 btv->pll.pll_current = 0;
1013 return;
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001016 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001017 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1019
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001020 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /* Let other people run while the PLL stabilizes */
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001022 bttv_printk(".");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 msleep(10);
1024
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001025 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 btwrite(0,BT848_DSTATUS);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001027 } else {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001028 btwrite(0x08,BT848_TGCTRL);
1029 btv->pll.pll_current = btv->pll.pll_ofreq;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001030 bttv_printk(" ok\n");
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001031 return;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001032 }
1033 }
1034 btv->pll.pll_current = -1;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001035 bttv_printk("failed\n");
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001036 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039/* used to switch between the bt848's analog/digital video capture modes */
1040static void bt848A_set_timing(struct bttv *btv)
1041{
1042 int i, len;
1043 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1044 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1045
1046 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
1047 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1048 btv->c.nr,table_idx);
1049
1050 /* timing change...reset timing generator address */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001051 btwrite(0x00, BT848_TGCTRL);
1052 btwrite(0x02, BT848_TGCTRL);
1053 btwrite(0x00, BT848_TGCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 len=SRAM_Table[table_idx][0];
1056 for(i = 1; i <= len; i++)
1057 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1058 btv->pll.pll_ofreq = 27000000;
1059
1060 set_pll(btv);
1061 btwrite(0x11, BT848_TGCTRL);
1062 btwrite(0x41, BT848_DVSIF);
1063 } else {
1064 btv->pll.pll_ofreq = fsc;
1065 set_pll(btv);
1066 btwrite(0x0, BT848_DVSIF);
1067 }
1068}
1069
1070/* ----------------------------------------------------------------------- */
1071
1072static void bt848_bright(struct bttv *btv, int bright)
1073{
1074 int value;
1075
1076 // printk("bttv: set bright: %d\n",bright); // DEBUG
1077 btv->bright = bright;
1078
1079 /* We want -128 to 127 we get 0-65535 */
1080 value = (bright >> 8) - 128;
1081 btwrite(value & 0xff, BT848_BRIGHT);
1082}
1083
1084static void bt848_hue(struct bttv *btv, int hue)
1085{
1086 int value;
1087
1088 btv->hue = hue;
1089
1090 /* -128 to 127 */
1091 value = (hue >> 8) - 128;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001092 btwrite(value & 0xff, BT848_HUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095static void bt848_contrast(struct bttv *btv, int cont)
1096{
1097 int value,hibit;
1098
1099 btv->contrast = cont;
1100
1101 /* 0-511 */
1102 value = (cont >> 7);
1103 hibit = (value >> 6) & 4;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001104 btwrite(value & 0xff, BT848_CONTRAST_LO);
1105 btaor(hibit, ~4, BT848_E_CONTROL);
1106 btaor(hibit, ~4, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
1109static void bt848_sat(struct bttv *btv, int color)
1110{
1111 int val_u,val_v,hibits;
1112
1113 btv->saturation = color;
1114
1115 /* 0-511 for the color */
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001116 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1117 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001118 hibits = (val_u >> 7) & 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 hibits |= (val_v >> 8) & 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001120 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1121 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1122 btaor(hibits, ~3, BT848_E_CONTROL);
1123 btaor(hibits, ~3, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126/* ----------------------------------------------------------------------- */
1127
1128static int
1129video_mux(struct bttv *btv, unsigned int input)
1130{
1131 int mux,mask2;
1132
1133 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1134 return -EINVAL;
1135
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001136 /* needed by RemoteVideo MX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1138 if (mask2)
1139 gpio_inout(mask2,mask2);
1140
1141 if (input == btv->svhs) {
1142 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1143 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1144 } else {
1145 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1146 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1147 }
1148 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
1149 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1150 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1151 btv->c.nr,input,mux);
1152
1153 /* card specific hook */
1154 if(bttv_tvcards[btv->c.type].muxsel_hook)
1155 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1156 return 0;
1157}
1158
1159static char *audio_modes[] = {
1160 "audio: tuner", "audio: radio", "audio: extern",
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001161 "audio: intern", "audio: mute"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162};
1163
1164static int
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001165audio_mux(struct bttv *btv, int input, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001167 int gpio_val, signal;
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001168 struct v4l2_control ctrl;
1169 struct i2c_client *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1172 bttv_tvcards[btv->c.type].gpiomask);
1173 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1174
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001175 btv->mute = mute;
1176 btv->audio = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001178 /* automute */
1179 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1180
1181 if (mute)
1182 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1183 else
1184 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001185
1186 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (bttv_gpio)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001188 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1189 if (in_interrupt())
1190 return 0;
1191
1192 ctrl.id = V4L2_CID_AUDIO_MUTE;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001193 ctrl.value = btv->mute;
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001194 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, &ctrl);
1195 c = btv->i2c_msp34xx_client;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001196 if (c) {
1197 struct v4l2_routing route;
1198
1199 /* Note: the inputs tuner/radio/extern/intern are translated
1200 to msp routings. This assumes common behavior for all msp3400
1201 based TV cards. When this assumption fails, then the
1202 specific MSP routing must be added to the card table.
1203 For now this is sufficient. */
1204 switch (input) {
1205 case TVAUDIO_INPUT_RADIO:
Hans Verkuil07151722006-04-01 18:03:23 -03001206 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1207 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001208 break;
1209 case TVAUDIO_INPUT_EXTERN:
Hans Verkuil07151722006-04-01 18:03:23 -03001210 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1211 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001212 break;
1213 case TVAUDIO_INPUT_INTERN:
1214 /* Yes, this is the same input as for RADIO. I doubt
1215 if this is ever used. The only board with an INTERN
1216 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1217 that was tested. My guess is that the whole INTERN
1218 input does not work. */
Hans Verkuil07151722006-04-01 18:03:23 -03001219 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1220 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001221 break;
1222 case TVAUDIO_INPUT_TUNER:
1223 default:
Wade Berrier434b2522007-06-25 13:02:16 -03001224 /* This is the only card that uses TUNER2, and afaik,
1225 is the only difference between the VOODOOTV_FM
1226 and VOODOOTV_200 */
1227 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1228 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1229 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1230 else
1231 route.input = MSP_INPUT_DEFAULT;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001232 break;
1233 }
1234 route.output = MSP_OUTPUT_DEFAULT;
1235 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1236 }
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001237 c = btv->i2c_tvaudio_client;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001238 if (c) {
1239 struct v4l2_routing route;
1240
1241 route.input = input;
1242 route.output = 0;
1243 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return 0;
1246}
1247
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001248static inline int
1249audio_mute(struct bttv *btv, int mute)
1250{
1251 return audio_mux(btv, btv->audio, mute);
1252}
1253
1254static inline int
1255audio_input(struct bttv *btv, int input)
1256{
1257 return audio_mux(btv, input, btv->mute);
1258}
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260static void
Michael Schimeke5bd0262007-01-18 16:17:39 -03001261bttv_crop_calc_limits(struct bttv_crop *c)
1262{
1263 /* Scale factor min. 1:1, max. 16:1. Min. image size
1264 48 x 32. Scaled width must be a multiple of 4. */
1265
1266 if (1) {
1267 /* For bug compatibility with VIDIOCGCAP and image
1268 size checks in earlier driver versions. */
1269 c->min_scaled_width = 48;
1270 c->min_scaled_height = 32;
1271 } else {
1272 c->min_scaled_width =
1273 (max(48, c->rect.width >> 4) + 3) & ~3;
1274 c->min_scaled_height =
1275 max(32, c->rect.height >> 4);
1276 }
1277
1278 c->max_scaled_width = c->rect.width & ~3;
1279 c->max_scaled_height = c->rect.height;
1280}
1281
1282static void
1283bttv_crop_reset(struct bttv_crop *c, int norm)
1284{
1285 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1286 bttv_crop_calc_limits(c);
1287}
1288
1289/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290static int
1291set_tvnorm(struct bttv *btv, unsigned int norm)
1292{
1293 const struct bttv_tvnorm *tvnorm;
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001294 v4l2_std_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 if (norm < 0 || norm >= BTTV_TVNORMS)
1297 return -EINVAL;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 tvnorm = &bttv_tvnorms[norm];
1300
Michael Schimeke5bd0262007-01-18 16:17:39 -03001301 if (btv->tvnorm < 0 ||
1302 btv->tvnorm >= BTTV_TVNORMS ||
1303 0 != memcmp(&bttv_tvnorms[btv->tvnorm].cropcap,
1304 &tvnorm->cropcap,
1305 sizeof (tvnorm->cropcap))) {
1306 bttv_crop_reset(&btv->crop[0], norm);
1307 btv->crop[1] = btv->crop[0]; /* current = default */
1308
1309 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1310 btv->crop_start = tvnorm->cropcap.bounds.top
1311 + tvnorm->cropcap.bounds.height;
1312 }
1313 }
1314
1315 btv->tvnorm = norm;
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 btwrite(tvnorm->adelay, BT848_ADELAY);
1318 btwrite(tvnorm->bdelay, BT848_BDELAY);
1319 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1320 BT848_IFORM);
1321 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1322 btwrite(1, BT848_VBI_PACK_DEL);
1323 bt848A_set_timing(btv);
1324
1325 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -08001326 case BTTV_BOARD_VOODOOTV_FM:
Wade Berrier434b2522007-06-25 13:02:16 -03001327 case BTTV_BOARD_VOODOOTV_200:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 bttv_tda9880_setnorm(btv,norm);
1329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03001331 id = tvnorm->v4l2_id;
1332 bttv_call_i2c_clients(btv, VIDIOC_S_STD, &id);
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return 0;
1335}
1336
Michael Schimeke5bd0262007-01-18 16:17:39 -03001337/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338static void
Trent Piepho333408f2007-07-03 15:08:10 -03001339set_input(struct bttv *btv, unsigned int input, unsigned int norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 unsigned long flags;
1342
1343 btv->input = input;
1344 if (irq_iswitch) {
1345 spin_lock_irqsave(&btv->s_lock,flags);
1346 if (btv->curr.frame_irq) {
1347 /* active capture -> delayed input switch */
1348 btv->new_input = input;
1349 } else {
1350 video_mux(btv,input);
1351 }
1352 spin_unlock_irqrestore(&btv->s_lock,flags);
1353 } else {
1354 video_mux(btv,input);
1355 }
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001356 audio_input(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1357 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN));
Trent Piepho333408f2007-07-03 15:08:10 -03001358 set_tvnorm(btv, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
1361static void init_irqreg(struct bttv *btv)
1362{
1363 /* clear status */
1364 btwrite(0xfffffUL, BT848_INT_STAT);
1365
1366 if (bttv_tvcards[btv->c.type].no_video) {
1367 /* i2c only */
1368 btwrite(BT848_INT_I2CDONE,
1369 BT848_INT_MASK);
1370 } else {
1371 /* full video */
1372 btwrite((btv->triton1) |
1373 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1374 BT848_INT_SCERR |
1375 (fdsr ? BT848_INT_FDSR : 0) |
1376 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1377 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1378 BT848_INT_I2CDONE,
1379 BT848_INT_MASK);
1380 }
1381}
1382
1383static void init_bt848(struct bttv *btv)
1384{
1385 int val;
1386
1387 if (bttv_tvcards[btv->c.type].no_video) {
1388 /* very basic init only */
1389 init_irqreg(btv);
1390 return;
1391 }
1392
1393 btwrite(0x00, BT848_CAP_CTL);
1394 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1395 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1396
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001397 /* set planar and packed mode trigger points and */
1398 /* set rising edge of inverted GPINTR pin as irq trigger */
1399 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1400 BT848_GPIO_DMA_CTL_PLTP1_16|
1401 BT848_GPIO_DMA_CTL_PLTP23_16|
1402 BT848_GPIO_DMA_CTL_GPINTC|
1403 BT848_GPIO_DMA_CTL_GPINTI,
1404 BT848_GPIO_DMA_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001407 btwrite(val, BT848_E_SCLOOP);
1408 btwrite(val, BT848_O_SCLOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001410 btwrite(0x20, BT848_E_VSCALE_HI);
1411 btwrite(0x20, BT848_O_VSCALE_HI);
1412 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 BT848_ADC);
1414
1415 btwrite(whitecrush_upper, BT848_WC_UP);
1416 btwrite(whitecrush_lower, BT848_WC_DOWN);
1417
1418 if (btv->opt_lumafilter) {
1419 btwrite(0, BT848_E_CONTROL);
1420 btwrite(0, BT848_O_CONTROL);
1421 } else {
1422 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1423 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1424 }
1425
1426 bt848_bright(btv, btv->bright);
1427 bt848_hue(btv, btv->hue);
1428 bt848_contrast(btv, btv->contrast);
1429 bt848_sat(btv, btv->saturation);
1430
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001431 /* interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 init_irqreg(btv);
1433}
1434
1435static void bttv_reinit_bt848(struct bttv *btv)
1436{
1437 unsigned long flags;
1438
1439 if (bttv_verbose)
1440 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1441 spin_lock_irqsave(&btv->s_lock,flags);
1442 btv->errors=0;
1443 bttv_set_dma(btv,0);
1444 spin_unlock_irqrestore(&btv->s_lock,flags);
1445
1446 init_bt848(btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001447 btv->pll.pll_current = -1;
Trent Piepho333408f2007-07-03 15:08:10 -03001448 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
1451static int get_control(struct bttv *btv, struct v4l2_control *c)
1452{
1453 struct video_audio va;
1454 int i;
1455
1456 for (i = 0; i < BTTV_CTLS; i++)
1457 if (bttv_ctls[i].id == c->id)
1458 break;
1459 if (i == BTTV_CTLS)
1460 return -EINVAL;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001461 if (btv->audio_hook && i >= 4 && i <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 memset(&va,0,sizeof(va));
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001463 btv->audio_hook(btv,&va,0);
1464 switch (c->id) {
1465 case V4L2_CID_AUDIO_MUTE:
1466 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1467 break;
1468 case V4L2_CID_AUDIO_VOLUME:
1469 c->value = va.volume;
1470 break;
1471 case V4L2_CID_AUDIO_BALANCE:
1472 c->value = va.balance;
1473 break;
1474 case V4L2_CID_AUDIO_BASS:
1475 c->value = va.bass;
1476 break;
1477 case V4L2_CID_AUDIO_TREBLE:
1478 c->value = va.treble;
1479 break;
1480 }
1481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483 switch (c->id) {
1484 case V4L2_CID_BRIGHTNESS:
1485 c->value = btv->bright;
1486 break;
1487 case V4L2_CID_HUE:
1488 c->value = btv->hue;
1489 break;
1490 case V4L2_CID_CONTRAST:
1491 c->value = btv->contrast;
1492 break;
1493 case V4L2_CID_SATURATION:
1494 c->value = btv->saturation;
1495 break;
1496
1497 case V4L2_CID_AUDIO_MUTE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 case V4L2_CID_AUDIO_VOLUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 case V4L2_CID_AUDIO_BALANCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 case V4L2_CID_AUDIO_BASS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001502 bttv_call_i2c_clients(btv,VIDIOC_G_CTRL,c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 break;
1504
1505 case V4L2_CID_PRIVATE_CHROMA_AGC:
1506 c->value = btv->opt_chroma_agc;
1507 break;
1508 case V4L2_CID_PRIVATE_COMBFILTER:
1509 c->value = btv->opt_combfilter;
1510 break;
1511 case V4L2_CID_PRIVATE_LUMAFILTER:
1512 c->value = btv->opt_lumafilter;
1513 break;
1514 case V4L2_CID_PRIVATE_AUTOMUTE:
1515 c->value = btv->opt_automute;
1516 break;
1517 case V4L2_CID_PRIVATE_AGC_CRUSH:
1518 c->value = btv->opt_adc_crush;
1519 break;
1520 case V4L2_CID_PRIVATE_VCR_HACK:
1521 c->value = btv->opt_vcr_hack;
1522 break;
1523 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1524 c->value = btv->opt_whitecrush_upper;
1525 break;
1526 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1527 c->value = btv->opt_whitecrush_lower;
1528 break;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001529 case V4L2_CID_PRIVATE_UV_RATIO:
1530 c->value = btv->opt_uv_ratio;
1531 break;
1532 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1533 c->value = btv->opt_full_luma_range;
1534 break;
1535 case V4L2_CID_PRIVATE_CORING:
1536 c->value = btv->opt_coring;
1537 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 default:
1539 return -EINVAL;
1540 }
1541 return 0;
1542}
1543
1544static int set_control(struct bttv *btv, struct v4l2_control *c)
1545{
1546 struct video_audio va;
1547 int i,val;
1548
1549 for (i = 0; i < BTTV_CTLS; i++)
1550 if (bttv_ctls[i].id == c->id)
1551 break;
1552 if (i == BTTV_CTLS)
1553 return -EINVAL;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001554 if (btv->audio_hook && i >= 4 && i <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 memset(&va,0,sizeof(va));
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001556 btv->audio_hook(btv,&va,0);
1557 switch (c->id) {
1558 case V4L2_CID_AUDIO_MUTE:
1559 if (c->value) {
1560 va.flags |= VIDEO_AUDIO_MUTE;
1561 audio_mute(btv, 1);
1562 } else {
1563 va.flags &= ~VIDEO_AUDIO_MUTE;
1564 audio_mute(btv, 0);
1565 }
1566 break;
1567
1568 case V4L2_CID_AUDIO_VOLUME:
1569 va.volume = c->value;
1570 break;
1571 case V4L2_CID_AUDIO_BALANCE:
1572 va.balance = c->value;
1573 break;
1574 case V4L2_CID_AUDIO_BASS:
1575 va.bass = c->value;
1576 break;
1577 case V4L2_CID_AUDIO_TREBLE:
1578 va.treble = c->value;
1579 break;
1580 }
1581 btv->audio_hook(btv,&va,1);
1582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
1584 switch (c->id) {
1585 case V4L2_CID_BRIGHTNESS:
1586 bt848_bright(btv,c->value);
1587 break;
1588 case V4L2_CID_HUE:
1589 bt848_hue(btv,c->value);
1590 break;
1591 case V4L2_CID_CONTRAST:
1592 bt848_contrast(btv,c->value);
1593 break;
1594 case V4L2_CID_SATURATION:
1595 bt848_sat(btv,c->value);
1596 break;
1597 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001598 audio_mute(btv, c->value);
1599 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 case V4L2_CID_AUDIO_VOLUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 case V4L2_CID_AUDIO_BALANCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 case V4L2_CID_AUDIO_BASS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001604 bttv_call_i2c_clients(btv,VIDIOC_S_CTRL,c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 break;
1606
1607 case V4L2_CID_PRIVATE_CHROMA_AGC:
1608 btv->opt_chroma_agc = c->value;
1609 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1610 btwrite(val, BT848_E_SCLOOP);
1611 btwrite(val, BT848_O_SCLOOP);
1612 break;
1613 case V4L2_CID_PRIVATE_COMBFILTER:
1614 btv->opt_combfilter = c->value;
1615 break;
1616 case V4L2_CID_PRIVATE_LUMAFILTER:
1617 btv->opt_lumafilter = c->value;
1618 if (btv->opt_lumafilter) {
1619 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1620 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1621 } else {
1622 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1623 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1624 }
1625 break;
1626 case V4L2_CID_PRIVATE_AUTOMUTE:
1627 btv->opt_automute = c->value;
1628 break;
1629 case V4L2_CID_PRIVATE_AGC_CRUSH:
1630 btv->opt_adc_crush = c->value;
1631 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1632 BT848_ADC);
1633 break;
1634 case V4L2_CID_PRIVATE_VCR_HACK:
1635 btv->opt_vcr_hack = c->value;
1636 break;
1637 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1638 btv->opt_whitecrush_upper = c->value;
1639 btwrite(c->value, BT848_WC_UP);
1640 break;
1641 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1642 btv->opt_whitecrush_lower = c->value;
1643 btwrite(c->value, BT848_WC_DOWN);
1644 break;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001645 case V4L2_CID_PRIVATE_UV_RATIO:
1646 btv->opt_uv_ratio = c->value;
1647 bt848_sat(btv, btv->saturation);
1648 break;
1649 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1650 btv->opt_full_luma_range = c->value;
1651 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1652 break;
1653 case V4L2_CID_PRIVATE_CORING:
1654 btv->opt_coring = c->value;
1655 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 default:
1658 return -EINVAL;
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return 0;
1661}
1662
1663/* ----------------------------------------------------------------------- */
1664
1665void bttv_gpio_tracking(struct bttv *btv, char *comment)
1666{
1667 unsigned int outbits, data;
1668 outbits = btread(BT848_GPIO_OUT_EN);
1669 data = btread(BT848_GPIO_DATA);
1670 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1671 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1672}
1673
1674static void bttv_field_count(struct bttv *btv)
1675{
1676 int need_count = 0;
1677
1678 if (btv->users)
1679 need_count++;
1680
1681 if (need_count) {
1682 /* start field counter */
1683 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1684 } else {
1685 /* stop field counter */
1686 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1687 btv->field_count = 0;
1688 }
1689}
1690
1691static const struct bttv_format*
1692format_by_palette(int palette)
1693{
1694 unsigned int i;
1695
1696 for (i = 0; i < BTTV_FORMATS; i++) {
1697 if (-1 == bttv_formats[i].palette)
1698 continue;
1699 if (bttv_formats[i].palette == palette)
1700 return bttv_formats+i;
1701 }
1702 return NULL;
1703}
1704
1705static const struct bttv_format*
1706format_by_fourcc(int fourcc)
1707{
1708 unsigned int i;
1709
1710 for (i = 0; i < BTTV_FORMATS; i++) {
1711 if (-1 == bttv_formats[i].fourcc)
1712 continue;
1713 if (bttv_formats[i].fourcc == fourcc)
1714 return bttv_formats+i;
1715 }
1716 return NULL;
1717}
1718
1719/* ----------------------------------------------------------------------- */
1720/* misc helpers */
1721
1722static int
1723bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1724 struct bttv_buffer *new)
1725{
1726 struct bttv_buffer *old;
1727 unsigned long flags;
1728 int retval = 0;
1729
1730 dprintk("switch_overlay: enter [new=%p]\n",new);
1731 if (new)
1732 new->vb.state = STATE_DONE;
1733 spin_lock_irqsave(&btv->s_lock,flags);
1734 old = btv->screen;
1735 btv->screen = new;
1736 btv->loop_irq |= 1;
1737 bttv_set_dma(btv, 0x03);
1738 spin_unlock_irqrestore(&btv->s_lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (NULL != old) {
1740 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001741 bttv_dma_free(&fh->cap,btv, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 kfree(old);
1743 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03001744 if (NULL == new)
1745 free_btres(btv,fh,RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 dprintk("switch_overlay: done\n");
1747 return retval;
1748}
1749
1750/* ----------------------------------------------------------------------- */
1751/* video4linux (1) interface */
1752
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001753static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1754 struct bttv_buffer *buf,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001755 const struct bttv_format *fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 unsigned int width, unsigned int height,
1757 enum v4l2_field field)
1758{
Michael Schimeke5bd0262007-01-18 16:17:39 -03001759 struct bttv_fh *fh = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 int redo_dma_risc = 0;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001761 struct bttv_crop c;
1762 int norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 int rc;
1764
1765 /* check settings */
1766 if (NULL == fmt)
1767 return -EINVAL;
1768 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1769 width = RAW_BPL;
1770 height = RAW_LINES*2;
1771 if (width*height > buf->vb.bsize)
1772 return -EINVAL;
1773 buf->vb.size = buf->vb.bsize;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001774
1775 /* Make sure tvnorm and vbi_end remain consistent
1776 until we're done. */
1777 mutex_lock(&btv->lock);
1778
1779 norm = btv->tvnorm;
1780
1781 /* In this mode capturing always starts at defrect.top
1782 (default VDELAY), ignoring cropping parameters. */
1783 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1784 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001786 }
1787
1788 mutex_unlock(&btv->lock);
1789
1790 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1791 } else {
1792 mutex_lock(&btv->lock);
1793
1794 norm = btv->tvnorm;
1795 c = btv->crop[!!fh->do_crop];
1796
1797 mutex_unlock(&btv->lock);
1798
1799 if (width < c.min_scaled_width ||
1800 width > c.max_scaled_width ||
1801 height < c.min_scaled_height)
1802 return -EINVAL;
1803
1804 switch (field) {
1805 case V4L2_FIELD_TOP:
1806 case V4L2_FIELD_BOTTOM:
1807 case V4L2_FIELD_ALTERNATE:
1808 /* btv->crop counts frame lines. Max. scale
1809 factor is 16:1 for frames, 8:1 for fields. */
1810 if (height * 2 > c.max_scaled_height)
1811 return -EINVAL;
1812 break;
1813
1814 default:
1815 if (height > c.max_scaled_height)
1816 return -EINVAL;
1817 break;
1818 }
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 buf->vb.size = (width * height * fmt->depth) >> 3;
1821 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1822 return -EINVAL;
1823 }
1824
1825 /* alloc + fill struct bttv_buffer (if changed) */
1826 if (buf->vb.width != width || buf->vb.height != height ||
1827 buf->vb.field != field ||
Michael Schimeke5bd0262007-01-18 16:17:39 -03001828 buf->tvnorm != norm || buf->fmt != fmt ||
1829 buf->crop.top != c.rect.top ||
1830 buf->crop.left != c.rect.left ||
1831 buf->crop.width != c.rect.width ||
1832 buf->crop.height != c.rect.height) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 buf->vb.width = width;
1834 buf->vb.height = height;
1835 buf->vb.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001836 buf->tvnorm = norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 buf->fmt = fmt;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001838 buf->crop = c.rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 redo_dma_risc = 1;
1840 }
1841
1842 /* alloc risc memory */
1843 if (STATE_NEEDS_INIT == buf->vb.state) {
1844 redo_dma_risc = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001845 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 goto fail;
1847 }
1848
1849 if (redo_dma_risc)
1850 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1851 goto fail;
1852
1853 buf->vb.state = STATE_PREPARED;
1854 return 0;
1855
1856 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001857 bttv_dma_free(q,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return rc;
1859}
1860
1861static int
1862buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1863{
1864 struct bttv_fh *fh = q->priv_data;
1865
1866 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1867 if (0 == *count)
1868 *count = gbuffers;
1869 while (*size * *count > gbuffers * gbufsize)
1870 (*count)--;
1871 return 0;
1872}
1873
1874static int
1875buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1876 enum v4l2_field field)
1877{
1878 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1879 struct bttv_fh *fh = q->priv_data;
1880
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001881 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 fh->width, fh->height, field);
1883}
1884
1885static void
1886buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1887{
1888 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1889 struct bttv_fh *fh = q->priv_data;
1890 struct bttv *btv = fh->btv;
1891
1892 buf->vb.state = STATE_QUEUED;
1893 list_add_tail(&buf->vb.queue,&btv->capture);
1894 if (!btv->curr.frame_irq) {
1895 btv->loop_irq |= 1;
1896 bttv_set_dma(btv, 0x03);
1897 }
1898}
1899
1900static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1901{
1902 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1903 struct bttv_fh *fh = q->priv_data;
1904
Michael Schimekfeaba7a2007-01-26 08:30:05 -03001905 bttv_dma_free(q,fh->btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
1908static struct videobuf_queue_ops bttv_video_qops = {
1909 .buf_setup = buffer_setup,
1910 .buf_prepare = buffer_prepare,
1911 .buf_queue = buffer_queue,
1912 .buf_release = buffer_release,
1913};
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1916{
1917 switch (cmd) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001918 case VIDIOCGAUDIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 {
1920 struct video_audio *v = arg;
1921
1922 memset(v,0,sizeof(*v));
1923 strcpy(v->name,"Television");
1924 v->flags |= VIDEO_AUDIO_MUTABLE;
1925 v->mode = VIDEO_SOUND_MONO;
1926
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001927 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 bttv_call_i2c_clients(btv,cmd,v);
1929
1930 /* card specific hooks */
1931 if (btv->audio_hook)
1932 btv->audio_hook(btv,v,0);
1933
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001934 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return 0;
1936 }
1937 case VIDIOCSAUDIO:
1938 {
1939 struct video_audio *v = arg;
1940 unsigned int audio = v->audio;
1941
1942 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
1943 return -EINVAL;
1944
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001945 mutex_lock(&btv->lock);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001946 audio_mute(btv, (v->flags&VIDEO_AUDIO_MUTE) ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 bttv_call_i2c_clients(btv,cmd,v);
1948
1949 /* card specific hooks */
1950 if (btv->audio_hook)
1951 btv->audio_hook(btv,v,1);
1952
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001953 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return 0;
1955 }
1956
1957 /* *** v4l2 *** ************************************************ */
1958 case VIDIOC_ENUMSTD:
1959 {
1960 struct v4l2_standard *e = arg;
1961 unsigned int index = e->index;
1962
1963 if (index >= BTTV_TVNORMS)
1964 return -EINVAL;
1965 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1966 bttv_tvnorms[e->index].name);
1967 e->index = index;
1968 return 0;
1969 }
1970 case VIDIOC_G_STD:
1971 {
1972 v4l2_std_id *id = arg;
1973 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1974 return 0;
1975 }
1976 case VIDIOC_S_STD:
1977 {
1978 v4l2_std_id *id = arg;
1979 unsigned int i;
1980
1981 for (i = 0; i < BTTV_TVNORMS; i++)
1982 if (*id & bttv_tvnorms[i].v4l2_id)
1983 break;
1984 if (i == BTTV_TVNORMS)
1985 return -EINVAL;
1986
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001987 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 set_tvnorm(btv,i);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001989 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return 0;
1991 }
1992 case VIDIOC_QUERYSTD:
1993 {
1994 v4l2_std_id *id = arg;
1995
1996 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1997 *id = V4L2_STD_625_50;
1998 else
1999 *id = V4L2_STD_525_60;
2000 return 0;
2001 }
2002
2003 case VIDIOC_ENUMINPUT:
2004 {
2005 struct v4l2_input *i = arg;
2006 unsigned int n;
2007
2008 n = i->index;
2009 if (n >= bttv_tvcards[btv->c.type].video_inputs)
2010 return -EINVAL;
2011 memset(i,0,sizeof(*i));
2012 i->index = n;
2013 i->type = V4L2_INPUT_TYPE_CAMERA;
Mauro Carvalho Chehab5d9d1712006-11-14 12:40:07 -03002014 i->audioset = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (i->index == bttv_tvcards[btv->c.type].tuner) {
2016 sprintf(i->name, "Television");
2017 i->type = V4L2_INPUT_TYPE_TUNER;
2018 i->tuner = 0;
2019 } else if (i->index == btv->svhs) {
2020 sprintf(i->name, "S-Video");
2021 } else {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002022 sprintf(i->name,"Composite%d",i->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
2024 if (i->index == btv->input) {
2025 __u32 dstatus = btread(BT848_DSTATUS);
2026 if (0 == (dstatus & BT848_DSTATUS_PRES))
2027 i->status |= V4L2_IN_ST_NO_SIGNAL;
2028 if (0 == (dstatus & BT848_DSTATUS_HLOC))
2029 i->status |= V4L2_IN_ST_NO_H_LOCK;
2030 }
2031 for (n = 0; n < BTTV_TVNORMS; n++)
2032 i->std |= bttv_tvnorms[n].v4l2_id;
2033 return 0;
2034 }
2035 case VIDIOC_G_INPUT:
2036 {
2037 int *i = arg;
2038 *i = btv->input;
2039 return 0;
2040 }
2041 case VIDIOC_S_INPUT:
2042 {
2043 unsigned int *i = arg;
2044
2045 if (*i > bttv_tvcards[btv->c.type].video_inputs)
2046 return -EINVAL;
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002047 mutex_lock(&btv->lock);
Trent Piepho333408f2007-07-03 15:08:10 -03002048 set_input(btv, *i, btv->tvnorm);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002049 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 return 0;
2051 }
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 case VIDIOC_S_TUNER:
2054 {
2055 struct v4l2_tuner *t = arg;
2056
2057 if (UNSET == bttv_tvcards[btv->c.type].tuner)
2058 return -EINVAL;
2059 if (0 != t->index)
2060 return -EINVAL;
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002061 mutex_lock(&btv->lock);
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002062 bttv_call_i2c_clients(btv, VIDIOC_S_TUNER, t);
2063 if (btv->audio_hook) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 struct video_audio va;
2065 memset(&va, 0, sizeof(struct video_audio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (t->audmode == V4L2_TUNER_MODE_MONO)
2067 va.mode = VIDEO_SOUND_MONO;
Hans Verkuil301e22d2006-03-18 17:15:00 -03002068 else if (t->audmode == V4L2_TUNER_MODE_STEREO ||
2069 t->audmode == V4L2_TUNER_MODE_LANG1_LANG2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 va.mode = VIDEO_SOUND_STEREO;
2071 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
2072 va.mode = VIDEO_SOUND_LANG1;
2073 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
2074 va.mode = VIDEO_SOUND_LANG2;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002075 btv->audio_hook(btv,&va,1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002077 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return 0;
2079 }
2080
2081 case VIDIOC_G_FREQUENCY:
2082 {
2083 struct v4l2_frequency *f = arg;
2084
2085 memset(f,0,sizeof(*f));
2086 f->type = V4L2_TUNER_ANALOG_TV;
2087 f->frequency = btv->freq;
2088 return 0;
2089 }
2090 case VIDIOC_S_FREQUENCY:
2091 {
2092 struct v4l2_frequency *f = arg;
2093
2094 if (unlikely(f->tuner != 0))
2095 return -EINVAL;
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -07002096 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return -EINVAL;
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002098 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 btv->freq = f->frequency;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002100 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (btv->has_matchbox && btv->radio_user)
2102 tea5757_set_freq(btv,btv->freq);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002103 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return 0;
2105 }
Hans Verkuil299392b2005-11-08 21:37:42 -08002106 case VIDIOC_LOG_STATUS:
2107 {
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002108 printk(KERN_INFO "bttv%d: ================= START STATUS CARD #%d =================\n", btv->c.nr, btv->c.nr);
Luiz Capitulino97cb4452005-12-01 00:51:24 -08002109 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002110 printk(KERN_INFO "bttv%d: ================== END STATUS CARD #%d ==================\n", btv->c.nr, btv->c.nr);
Hans Verkuil299392b2005-11-08 21:37:42 -08002111 return 0;
2112 }
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002113 case VIDIOC_G_CTRL:
2114 return get_control(btv,arg);
2115 case VIDIOC_S_CTRL:
2116 return set_control(btv,arg);
Trent Piepho55c0d102007-06-29 00:17:36 -03002117#ifdef CONFIG_VIDEO_ADV_DEBUG
2118 case VIDIOC_DBG_G_REGISTER:
2119 case VIDIOC_DBG_S_REGISTER:
2120 {
2121 struct v4l2_register *reg = arg;
2122 if (!capable(CAP_SYS_ADMIN))
2123 return -EPERM;
2124 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
2125 return -EINVAL;
2126 /* bt848 has a 12-bit register space */
2127 reg->reg &= 0xfff;
2128 if (cmd == VIDIOC_DBG_G_REGISTER)
2129 reg->val = btread(reg->reg);
2130 else
2131 btwrite(reg->val, reg->reg);
2132 return 0;
2133 }
2134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 default:
2137 return -ENOIOCTLCMD;
2138
2139 }
2140 return 0;
2141}
2142
Michael Schimeke5bd0262007-01-18 16:17:39 -03002143/* Given cropping boundaries b and the scaled width and height of a
2144 single field or frame, which must not exceed hardware limits, this
2145 function adjusts the cropping parameters c. */
2146static void
2147bttv_crop_adjust (struct bttv_crop * c,
2148 const struct v4l2_rect * b,
2149 __s32 width,
2150 __s32 height,
2151 enum v4l2_field field)
2152{
2153 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2154 __s32 max_left;
2155 __s32 max_top;
2156
2157 if (width < c->min_scaled_width) {
2158 /* Max. hor. scale factor 16:1. */
2159 c->rect.width = width * 16;
2160 } else if (width > c->max_scaled_width) {
2161 /* Min. hor. scale factor 1:1. */
2162 c->rect.width = width;
2163
2164 max_left = b->left + b->width - width;
2165 max_left = min(max_left, (__s32) MAX_HDELAY);
2166 if (c->rect.left > max_left)
2167 c->rect.left = max_left;
2168 }
2169
2170 if (height < c->min_scaled_height) {
2171 /* Max. vert. scale factor 16:1, single fields 8:1. */
2172 c->rect.height = height * 16;
2173 } else if (frame_height > c->max_scaled_height) {
2174 /* Min. vert. scale factor 1:1.
2175 Top and height count field lines times two. */
2176 c->rect.height = (frame_height + 1) & ~1;
2177
2178 max_top = b->top + b->height - c->rect.height;
2179 if (c->rect.top > max_top)
2180 c->rect.top = max_top;
2181 }
2182
2183 bttv_crop_calc_limits(c);
2184}
2185
2186/* Returns an error if scaling to a frame or single field with the given
2187 width and height is not possible with the current cropping parameters
2188 and width aligned according to width_mask. If adjust_size is TRUE the
2189 function may adjust the width and/or height instead, rounding width
2190 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2191 also adjust the current cropping parameters to get closer to the
2192 desired image size. */
2193static int
2194limit_scaled_size (struct bttv_fh * fh,
2195 __s32 * width,
2196 __s32 * height,
2197 enum v4l2_field field,
2198 unsigned int width_mask,
2199 unsigned int width_bias,
2200 int adjust_size,
2201 int adjust_crop)
2202{
2203 struct bttv *btv = fh->btv;
2204 const struct v4l2_rect *b;
2205 struct bttv_crop *c;
2206 __s32 min_width;
2207 __s32 min_height;
2208 __s32 max_width;
2209 __s32 max_height;
2210 int rc;
2211
2212 BUG_ON((int) width_mask >= 0 ||
2213 width_bias >= (unsigned int) -width_mask);
2214
2215 /* Make sure tvnorm, vbi_end and the current cropping parameters
2216 remain consistent until we're done. */
2217 mutex_lock(&btv->lock);
2218
2219 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2220
2221 /* Do crop - use current, don't - use default parameters. */
2222 c = &btv->crop[!!fh->do_crop];
2223
2224 if (fh->do_crop
2225 && adjust_size
2226 && adjust_crop
2227 && !locked_btres(btv, VIDEO_RESOURCES)) {
2228 min_width = 48;
2229 min_height = 32;
2230
2231 /* We cannot scale up. When the scaled image is larger
2232 than crop.rect we adjust the crop.rect as required
2233 by the V4L2 spec, hence cropcap.bounds are our limit. */
2234 max_width = min(b->width, (__s32) MAX_HACTIVE);
2235 max_height = b->height;
2236
2237 /* We cannot capture the same line as video and VBI data.
2238 Note btv->vbi_end is really a minimum, see
2239 bttv_vbi_try_fmt(). */
2240 if (btv->vbi_end > b->top) {
2241 max_height -= btv->vbi_end - b->top;
2242 rc = -EBUSY;
2243 if (min_height > max_height)
2244 goto fail;
2245 }
2246 } else {
2247 rc = -EBUSY;
2248 if (btv->vbi_end > c->rect.top)
2249 goto fail;
2250
2251 min_width = c->min_scaled_width;
2252 min_height = c->min_scaled_height;
2253 max_width = c->max_scaled_width;
2254 max_height = c->max_scaled_height;
2255
2256 adjust_crop = 0;
2257 }
2258
2259 min_width = (min_width - width_mask - 1) & width_mask;
2260 max_width = max_width & width_mask;
2261
2262 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2263 min_height = min_height;
2264 /* Min. scale factor is 1:1. */
2265 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2266
2267 if (adjust_size) {
2268 *width = clamp(*width, min_width, max_width);
2269 *height = clamp(*height, min_height, max_height);
2270
2271 /* Round after clamping to avoid overflow. */
2272 *width = (*width + width_bias) & width_mask;
2273
2274 if (adjust_crop) {
2275 bttv_crop_adjust(c, b, *width, *height, field);
2276
2277 if (btv->vbi_end > c->rect.top) {
2278 /* Move the crop window out of the way. */
2279 c->rect.top = btv->vbi_end;
2280 }
2281 }
2282 } else {
2283 rc = -EINVAL;
2284 if (*width < min_width ||
2285 *height < min_height ||
2286 *width > max_width ||
2287 *height > max_height ||
2288 0 != (*width & ~width_mask))
2289 goto fail;
2290 }
2291
2292 rc = 0; /* success */
2293
2294 fail:
2295 mutex_unlock(&btv->lock);
2296
2297 return rc;
2298}
2299
2300/* Returns an error if the given overlay window dimensions are not
2301 possible with the current cropping parameters. If adjust_size is
2302 TRUE the function may adjust the window width and/or height
2303 instead, however it always rounds the horizontal position and
2304 width as btcx_align() does. If adjust_crop is TRUE the function
2305 may also adjust the current cropping parameters to get closer
2306 to the desired window size. */
2307static int
2308verify_window (struct bttv_fh * fh,
2309 struct v4l2_window * win,
2310 int adjust_size,
2311 int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
2313 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002314 unsigned int width_mask;
2315 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 if (win->w.width < 48 || win->w.height < 32)
2318 return -EINVAL;
2319 if (win->clipcount > 2048)
2320 return -EINVAL;
2321
2322 field = win->field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 if (V4L2_FIELD_ANY == field) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002325 __s32 height2;
2326
2327 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2328 field = (win->w.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 ? V4L2_FIELD_INTERLACED
2330 : V4L2_FIELD_TOP;
2331 }
2332 switch (field) {
2333 case V4L2_FIELD_TOP:
2334 case V4L2_FIELD_BOTTOM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 case V4L2_FIELD_INTERLACED:
2336 break;
2337 default:
2338 return -EINVAL;
2339 }
2340
Michael Schimeke5bd0262007-01-18 16:17:39 -03002341 /* 4-byte alignment. */
2342 if (NULL == fh->ovfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002344 width_mask = ~0;
2345 switch (fh->ovfmt->depth) {
2346 case 8:
2347 case 24:
2348 width_mask = ~3;
2349 break;
2350 case 16:
2351 width_mask = ~1;
2352 break;
2353 case 32:
2354 break;
2355 default:
2356 BUG();
2357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Michael Schimeke5bd0262007-01-18 16:17:39 -03002359 win->w.width -= win->w.left & ~width_mask;
2360 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2361
2362 rc = limit_scaled_size(fh, &win->w.width, &win->w.height,
2363 field, width_mask,
2364 /* width_bias: round down */ 0,
2365 adjust_size, adjust_crop);
2366 if (0 != rc)
2367 return rc;
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 win->field = field;
2370 return 0;
2371}
2372
2373static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2374 struct v4l2_window *win, int fixup)
2375{
2376 struct v4l2_clip *clips = NULL;
2377 int n,size,retval = 0;
2378
2379 if (NULL == fh->ovfmt)
2380 return -EINVAL;
2381 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2382 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002383 retval = verify_window(fh, win,
2384 /* adjust_size */ fixup,
2385 /* adjust_crop */ fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (0 != retval)
2387 return retval;
2388
2389 /* copy clips -- luckily v4l1 + v4l2 are binary
2390 compatible here ...*/
2391 n = win->clipcount;
2392 size = sizeof(*clips)*(n+4);
2393 clips = kmalloc(size,GFP_KERNEL);
2394 if (NULL == clips)
2395 return -ENOMEM;
2396 if (n > 0) {
2397 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2398 kfree(clips);
2399 return -EFAULT;
2400 }
2401 }
2402 /* clip against screen */
2403 if (NULL != btv->fbuf.base)
2404 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2405 &win->w, clips, n);
2406 btcx_sort_clips(clips,n);
2407
2408 /* 4-byte alignments */
2409 switch (fh->ovfmt->depth) {
2410 case 8:
2411 case 24:
2412 btcx_align(&win->w, clips, n, 3);
2413 break;
2414 case 16:
2415 btcx_align(&win->w, clips, n, 1);
2416 break;
2417 case 32:
2418 /* no alignment fixups needed */
2419 break;
2420 default:
2421 BUG();
2422 }
2423
Ingo Molnar3593cab2006-02-07 06:49:14 -02002424 mutex_lock(&fh->cap.lock);
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08002425 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 fh->ov.clips = clips;
2427 fh->ov.nclips = n;
2428
2429 fh->ov.w = win->w;
2430 fh->ov.field = win->field;
2431 fh->ov.setup_ok = 1;
2432 btv->init.ov.w.width = win->w.width;
2433 btv->init.ov.w.height = win->w.height;
2434 btv->init.ov.field = win->field;
2435
2436 /* update overlay if needed */
2437 retval = 0;
2438 if (check_btres(fh, RESOURCE_OVERLAY)) {
2439 struct bttv_buffer *new;
2440
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03002441 new = videobuf_pci_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03002442 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2444 retval = bttv_switch_overlay(btv,fh,new);
2445 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002446 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return retval;
2448}
2449
2450/* ----------------------------------------------------------------------- */
2451
2452static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2453{
2454 struct videobuf_queue* q = NULL;
2455
2456 switch (fh->type) {
2457 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2458 q = &fh->cap;
2459 break;
2460 case V4L2_BUF_TYPE_VBI_CAPTURE:
2461 q = &fh->vbi;
2462 break;
2463 default:
2464 BUG();
2465 }
2466 return q;
2467}
2468
2469static int bttv_resource(struct bttv_fh *fh)
2470{
2471 int res = 0;
2472
2473 switch (fh->type) {
2474 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002475 res = RESOURCE_VIDEO_STREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 break;
2477 case V4L2_BUF_TYPE_VBI_CAPTURE:
2478 res = RESOURCE_VBI;
2479 break;
2480 default:
2481 BUG();
2482 }
2483 return res;
2484}
2485
2486static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2487{
2488 struct videobuf_queue *q = bttv_queue(fh);
2489 int res = bttv_resource(fh);
2490
2491 if (check_btres(fh,res))
2492 return -EBUSY;
2493 if (videobuf_queue_is_busy(q))
2494 return -EBUSY;
2495 fh->type = type;
2496 return 0;
2497}
2498
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002499static void
2500pix_format_set_size (struct v4l2_pix_format * f,
2501 const struct bttv_format * fmt,
2502 unsigned int width,
2503 unsigned int height)
2504{
2505 f->width = width;
2506 f->height = height;
2507
2508 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2509 f->bytesperline = width; /* Y plane */
2510 f->sizeimage = (width * height * fmt->depth) >> 3;
2511 } else {
2512 f->bytesperline = (width * fmt->depth) >> 3;
2513 f->sizeimage = height * f->bytesperline;
2514 }
2515}
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2518{
2519 switch (f->type) {
2520 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2521 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002522 pix_format_set_size (&f->fmt.pix, fh->fmt,
2523 fh->width, fh->height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 f->fmt.pix.field = fh->cap.field;
2525 f->fmt.pix.pixelformat = fh->fmt->fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return 0;
2527 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2528 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2529 f->fmt.win.w = fh->ov.w;
2530 f->fmt.win.field = fh->ov.field;
2531 return 0;
2532 case V4L2_BUF_TYPE_VBI_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002533 bttv_vbi_get_fmt(fh, &f->fmt.vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return 0;
2535 default:
2536 return -EINVAL;
2537 }
2538}
2539
2540static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002541 struct v4l2_format *f, int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 switch (f->type) {
2544 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2545 {
2546 const struct bttv_format *fmt;
2547 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002548 __s32 width, height;
2549 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2552 if (NULL == fmt)
2553 return -EINVAL;
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 field = f->fmt.pix.field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002556 if (V4L2_FIELD_ANY == field) {
2557 __s32 height2;
2558
2559 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2560 field = (f->fmt.pix.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 ? V4L2_FIELD_INTERLACED
2562 : V4L2_FIELD_BOTTOM;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 if (V4L2_FIELD_SEQ_BT == field)
2565 field = V4L2_FIELD_SEQ_TB;
2566 switch (field) {
2567 case V4L2_FIELD_TOP:
2568 case V4L2_FIELD_BOTTOM:
2569 case V4L2_FIELD_ALTERNATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 case V4L2_FIELD_INTERLACED:
2571 break;
2572 case V4L2_FIELD_SEQ_TB:
2573 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2574 return -EINVAL;
2575 break;
2576 default:
2577 return -EINVAL;
2578 }
2579
Michael Schimeke5bd0262007-01-18 16:17:39 -03002580 width = f->fmt.pix.width;
2581 height = f->fmt.pix.height;
2582
2583 rc = limit_scaled_size(fh, &width, &height, field,
2584 /* width_mask: 4 pixels */ ~3,
2585 /* width_bias: nearest */ 2,
2586 /* adjust_size */ 1,
2587 adjust_crop);
2588 if (0 != rc)
2589 return rc;
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 /* update data for the application */
2592 f->fmt.pix.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002593 pix_format_set_size(&f->fmt.pix, fmt, width, height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 return 0;
2596 }
2597 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002598 return verify_window(fh, &f->fmt.win,
2599 /* adjust_size */ 1,
2600 /* adjust_crop */ 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 case V4L2_BUF_TYPE_VBI_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002602 return bttv_vbi_try_fmt(fh, &f->fmt.vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 default:
2604 return -EINVAL;
2605 }
2606}
2607
2608static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2609 struct v4l2_format *f)
2610{
2611 int retval;
2612
2613 switch (f->type) {
2614 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2615 {
2616 const struct bttv_format *fmt;
2617
2618 retval = bttv_switch_type(fh,f->type);
2619 if (0 != retval)
2620 return retval;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002621 retval = bttv_try_fmt(fh,btv,f, /* adjust_crop */ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (0 != retval)
2623 return retval;
2624 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2625
2626 /* update our state informations */
Ingo Molnar3593cab2006-02-07 06:49:14 -02002627 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 fh->fmt = fmt;
2629 fh->cap.field = f->fmt.pix.field;
2630 fh->cap.last = V4L2_FIELD_NONE;
2631 fh->width = f->fmt.pix.width;
2632 fh->height = f->fmt.pix.height;
2633 btv->init.fmt = fmt;
2634 btv->init.width = f->fmt.pix.width;
2635 btv->init.height = f->fmt.pix.height;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002636 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 return 0;
2639 }
2640 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002641 if (no_overlay > 0) {
2642 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2643 return -EINVAL;
2644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 return setup_window(fh, btv, &f->fmt.win, 1);
2646 case V4L2_BUF_TYPE_VBI_CAPTURE:
2647 retval = bttv_switch_type(fh,f->type);
2648 if (0 != retval)
2649 return retval;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002650 return bttv_vbi_set_fmt(fh, &f->fmt.vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 default:
2652 return -EINVAL;
2653 }
2654}
2655
2656static int bttv_do_ioctl(struct inode *inode, struct file *file,
2657 unsigned int cmd, void *arg)
2658{
2659 struct bttv_fh *fh = file->private_data;
2660 struct bttv *btv = fh->btv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 int retval = 0;
2662
Michael Krufky5e453dc2006-01-09 15:32:31 -02002663 if (bttv_debug > 1)
2664 v4l_print_ioctl(btv->c.name, cmd);
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (btv->errors)
2667 bttv_reinit_bt848(btv);
2668
2669 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 case VIDIOC_S_CTRL:
2671 case VIDIOC_S_STD:
2672 case VIDIOC_S_INPUT:
2673 case VIDIOC_S_TUNER:
2674 case VIDIOC_S_FREQUENCY:
2675 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2676 if (0 != retval)
2677 return retval;
2678 };
2679
2680 switch (cmd) {
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002681#ifdef CONFIG_VIDEO_V4L1_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 case VIDIOCGMBUF:
2683 {
2684 struct video_mbuf *mbuf = arg;
2685 unsigned int i;
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
2688 V4L2_MEMORY_MMAP);
2689 if (retval < 0)
Gregor Jasnyd9030f52008-01-06 11:15:54 -03002690 return retval;
Brandon Philips49ee7182007-10-05 16:26:27 -03002691
2692 gbuffers = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 memset(mbuf,0,sizeof(*mbuf));
2694 mbuf->frames = gbuffers;
2695 mbuf->size = gbuffers * gbufsize;
2696 for (i = 0; i < gbuffers; i++)
2697 mbuf->offsets[i] = i * gbufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return 0;
2699 }
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002700#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 case VIDIOCGVBIFMT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 case VIDIOCSVBIFMT:
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002703 case VIDIOCSYNC:
2704 case VIDIOCMCAPTURE:
2705 case VIDIOCCAPTURE:
2706 case VIDIOCGFBUF:
2707 case VIDIOCSFBUF:
2708 case VIDIOCGWIN:
2709 case VIDIOCSWIN:
2710 case VIDIOCGCAP:
2711 case VIDIOCGPICT:
2712 case VIDIOCSPICT:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002713 case VIDIOCGFREQ:
2714 case VIDIOCSFREQ:
2715 case VIDIOCGTUNER:
2716 case VIDIOCSTUNER:
2717 case VIDIOCGCHAN:
2718 case VIDIOCSCHAN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 case VIDIOCGAUDIO:
2720 case VIDIOCSAUDIO:
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002721 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2722 bttv_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
2724 /* *** v4l2 *** ************************************************ */
2725 case VIDIOC_QUERYCAP:
2726 {
2727 struct v4l2_capability *cap = arg;
2728
2729 if (0 == v4l2)
2730 return -EINVAL;
Michael H. Schimekbbf78712005-12-01 00:51:40 -08002731 memset(cap, 0, sizeof (*cap));
2732 strlcpy(cap->driver, "bttv", sizeof (cap->driver));
2733 strlcpy(cap->card, btv->video_dev->name, sizeof (cap->card));
2734 snprintf(cap->bus_info, sizeof (cap->bus_info),
2735 "PCI:%s", pci_name(btv->c.pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 cap->version = BTTV_VERSION_CODE;
2737 cap->capabilities =
2738 V4L2_CAP_VIDEO_CAPTURE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 V4L2_CAP_VBI_CAPTURE |
2740 V4L2_CAP_READWRITE |
2741 V4L2_CAP_STREAMING;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002742 if (no_overlay <= 0)
2743 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
2746 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
2747 cap->capabilities |= V4L2_CAP_TUNER;
2748 return 0;
2749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 case VIDIOC_ENUM_FMT:
2751 {
2752 struct v4l2_fmtdesc *f = arg;
2753 enum v4l2_buf_type type;
2754 unsigned int i;
2755 int index;
2756
2757 type = f->type;
2758 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2759 /* vbi */
2760 index = f->index;
2761 if (0 != index)
2762 return -EINVAL;
2763 memset(f,0,sizeof(*f));
2764 f->index = index;
2765 f->type = type;
2766 f->pixelformat = V4L2_PIX_FMT_GREY;
2767 strcpy(f->description,"vbi data");
2768 return 0;
2769 }
2770
2771 /* video capture + overlay */
2772 index = -1;
2773 for (i = 0; i < BTTV_FORMATS; i++) {
2774 if (bttv_formats[i].fourcc != -1)
2775 index++;
2776 if ((unsigned int)index == f->index)
2777 break;
2778 }
2779 if (BTTV_FORMATS == i)
2780 return -EINVAL;
2781
2782 switch (f->type) {
2783 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2784 break;
2785 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2786 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2787 return -EINVAL;
2788 break;
2789 default:
2790 return -EINVAL;
2791 }
2792 memset(f,0,sizeof(*f));
2793 f->index = index;
2794 f->type = type;
2795 f->pixelformat = bttv_formats[i].fourcc;
2796 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2797 return 0;
2798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 case VIDIOC_TRY_FMT:
2800 {
2801 struct v4l2_format *f = arg;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002802 return bttv_try_fmt(fh,btv,f, /* adjust_crop */ 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
2804 case VIDIOC_G_FMT:
2805 {
2806 struct v4l2_format *f = arg;
2807 return bttv_g_fmt(fh,f);
2808 }
2809 case VIDIOC_S_FMT:
2810 {
2811 struct v4l2_format *f = arg;
2812 return bttv_s_fmt(fh,btv,f);
2813 }
2814
2815 case VIDIOC_G_FBUF:
2816 {
2817 struct v4l2_framebuffer *fb = arg;
2818
2819 *fb = btv->fbuf;
2820 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2821 if (fh->ovfmt)
2822 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2823 return 0;
2824 }
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03002825 case VIDIOC_OVERLAY:
2826 {
2827 struct bttv_buffer *new;
2828 int *on = arg;
2829
2830 if (*on) {
2831 /* verify args */
2832 if (NULL == btv->fbuf.base)
2833 return -EINVAL;
2834 if (!fh->ov.setup_ok) {
2835 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2836 return -EINVAL;
2837 }
2838 }
2839
2840 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2841 return -EBUSY;
2842
2843 mutex_lock(&fh->cap.lock);
2844 if (*on) {
2845 fh->ov.tvnorm = btv->tvnorm;
2846 new = videobuf_pci_alloc(sizeof(*new));
2847 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2848 } else {
2849 new = NULL;
2850 }
2851
2852 /* switch over */
2853 retval = bttv_switch_overlay(btv,fh,new);
2854 mutex_unlock(&fh->cap.lock);
2855 return retval;
2856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 case VIDIOC_S_FBUF:
2858 {
2859 struct v4l2_framebuffer *fb = arg;
2860 const struct bttv_format *fmt;
2861
2862 if(!capable(CAP_SYS_ADMIN) &&
2863 !capable(CAP_SYS_RAWIO))
2864 return -EPERM;
2865
2866 /* check args */
2867 fmt = format_by_fourcc(fb->fmt.pixelformat);
2868 if (NULL == fmt)
2869 return -EINVAL;
2870 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2871 return -EINVAL;
2872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 retval = -EINVAL;
2874 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002875 __s32 width = fb->fmt.width;
2876 __s32 height = fb->fmt.height;
2877
2878 retval = limit_scaled_size(fh, &width, &height,
2879 V4L2_FIELD_INTERLACED,
2880 /* width_mask */ ~3,
2881 /* width_bias */ 2,
2882 /* adjust_size */ 0,
2883 /* adjust_crop */ 0);
2884 if (0 != retval)
2885 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
2887
2888 /* ok, accept it */
Michael Schimeke5bd0262007-01-18 16:17:39 -03002889 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 btv->fbuf.base = fb->base;
2891 btv->fbuf.fmt.width = fb->fmt.width;
2892 btv->fbuf.fmt.height = fb->fmt.height;
2893 if (0 != fb->fmt.bytesperline)
2894 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2895 else
2896 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2897
2898 retval = 0;
2899 fh->ovfmt = fmt;
2900 btv->init.ovfmt = fmt;
2901 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2902 fh->ov.w.left = 0;
2903 fh->ov.w.top = 0;
2904 fh->ov.w.width = fb->fmt.width;
2905 fh->ov.w.height = fb->fmt.height;
2906 btv->init.ov.w.width = fb->fmt.width;
2907 btv->init.ov.w.height = fb->fmt.height;
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08002908 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 fh->ov.clips = NULL;
2910 fh->ov.nclips = 0;
2911
2912 if (check_btres(fh, RESOURCE_OVERLAY)) {
2913 struct bttv_buffer *new;
2914
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03002915 new = videobuf_pci_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03002916 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2918 retval = bttv_switch_overlay(btv,fh,new);
2919 }
2920 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002921 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return retval;
2923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 case VIDIOC_REQBUFS:
2925 return videobuf_reqbufs(bttv_queue(fh),arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 case VIDIOC_QUERYBUF:
2927 return videobuf_querybuf(bttv_queue(fh),arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 case VIDIOC_QBUF:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002929 {
2930 int res = bttv_resource(fh);
2931
2932 if (!check_alloc_btres(btv, fh, res))
2933 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 return videobuf_qbuf(bttv_queue(fh),arg);
Michael Schimeke5bd0262007-01-18 16:17:39 -03002935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 case VIDIOC_DQBUF:
2937 return videobuf_dqbuf(bttv_queue(fh),arg,
2938 file->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 case VIDIOC_STREAMON:
2940 {
2941 int res = bttv_resource(fh);
2942
2943 if (!check_alloc_btres(btv,fh,res))
2944 return -EBUSY;
2945 return videobuf_streamon(bttv_queue(fh));
2946 }
2947 case VIDIOC_STREAMOFF:
2948 {
2949 int res = bttv_resource(fh);
2950
2951 retval = videobuf_streamoff(bttv_queue(fh));
2952 if (retval < 0)
2953 return retval;
2954 free_btres(btv,fh,res);
2955 return 0;
2956 }
2957
2958 case VIDIOC_QUERYCTRL:
2959 {
2960 struct v4l2_queryctrl *c = arg;
2961 int i;
2962
2963 if ((c->id < V4L2_CID_BASE ||
2964 c->id >= V4L2_CID_LASTP1) &&
2965 (c->id < V4L2_CID_PRIVATE_BASE ||
2966 c->id >= V4L2_CID_PRIVATE_LASTP1))
2967 return -EINVAL;
2968 for (i = 0; i < BTTV_CTLS; i++)
2969 if (bttv_ctls[i].id == c->id)
2970 break;
2971 if (i == BTTV_CTLS) {
2972 *c = no_ctl;
2973 return 0;
2974 }
2975 *c = bttv_ctls[i];
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002976 if (btv->audio_hook && i >= 4 && i <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 struct video_audio va;
2978 memset(&va,0,sizeof(va));
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002979 btv->audio_hook(btv,&va,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 switch (bttv_ctls[i].id) {
2981 case V4L2_CID_AUDIO_VOLUME:
2982 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2983 *c = no_ctl;
2984 break;
2985 case V4L2_CID_AUDIO_BALANCE:
2986 if (!(va.flags & VIDEO_AUDIO_BALANCE))
2987 *c = no_ctl;
2988 break;
2989 case V4L2_CID_AUDIO_BASS:
2990 if (!(va.flags & VIDEO_AUDIO_BASS))
2991 *c = no_ctl;
2992 break;
2993 case V4L2_CID_AUDIO_TREBLE:
2994 if (!(va.flags & VIDEO_AUDIO_TREBLE))
2995 *c = no_ctl;
2996 break;
2997 }
2998 }
2999 return 0;
3000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 case VIDIOC_G_PARM:
3002 {
3003 struct v4l2_streamparm *parm = arg;
3004 struct v4l2_standard s;
3005 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
3006 return -EINVAL;
3007 memset(parm,0,sizeof(*parm));
3008 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
3009 bttv_tvnorms[btv->tvnorm].name);
3010 parm->parm.capture.timeperframe = s.frameperiod;
3011 return 0;
3012 }
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03003013 case VIDIOC_G_TUNER:
3014 {
3015 struct v4l2_tuner *t = arg;
3016
3017 if (UNSET == bttv_tvcards[btv->c.type].tuner)
3018 return -EINVAL;
3019 if (0 != t->index)
3020 return -EINVAL;
3021 mutex_lock(&btv->lock);
3022 memset(t,0,sizeof(*t));
3023 t->rxsubchans = V4L2_TUNER_SUB_MONO;
3024 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
3025 strcpy(t->name, "Television");
3026 t->capability = V4L2_TUNER_CAP_NORM;
3027 t->type = V4L2_TUNER_ANALOG_TV;
3028 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
3029 t->signal = 0xffff;
3030
3031 if (btv->audio_hook) {
3032 /* Hmmm ... */
3033 struct video_audio va;
3034 memset(&va, 0, sizeof(struct video_audio));
3035 btv->audio_hook(btv,&va,0);
3036 t->audmode = V4L2_TUNER_MODE_MONO;
3037 t->rxsubchans = V4L2_TUNER_SUB_MONO;
3038 if(va.mode & VIDEO_SOUND_STEREO) {
3039 t->audmode = V4L2_TUNER_MODE_STEREO;
3040 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
3041 }
3042 if(va.mode & VIDEO_SOUND_LANG2) {
3043 t->audmode = V4L2_TUNER_MODE_LANG1;
3044 t->rxsubchans = V4L2_TUNER_SUB_LANG1
3045 | V4L2_TUNER_SUB_LANG2;
3046 }
3047 }
3048 /* FIXME: fill capability+audmode */
3049 mutex_unlock(&btv->lock);
3050 return 0;
3051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
3053 case VIDIOC_G_PRIORITY:
3054 {
3055 enum v4l2_priority *p = arg;
3056
3057 *p = v4l2_prio_max(&btv->prio);
3058 return 0;
3059 }
3060 case VIDIOC_S_PRIORITY:
3061 {
3062 enum v4l2_priority *prio = arg;
3063
3064 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
3065 }
3066
Michael Schimeke5bd0262007-01-18 16:17:39 -03003067 case VIDIOC_CROPCAP:
3068 {
3069 struct v4l2_cropcap *cap = arg;
3070 enum v4l2_buf_type type;
3071
3072 type = cap->type;
3073
3074 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3075 type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3076 return -EINVAL;
3077
3078 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3079 cap->type = type;
3080
3081 return 0;
3082 }
3083 case VIDIOC_G_CROP:
3084 {
3085 struct v4l2_crop * crop = arg;
3086
3087 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3088 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3089 return -EINVAL;
3090
3091 /* No fh->do_crop = 1; because btv->crop[1] may be
3092 inconsistent with fh->width or fh->height and apps
3093 do not expect a change here. */
3094
3095 crop->c = btv->crop[!!fh->do_crop].rect;
3096
3097 return 0;
3098 }
3099 case VIDIOC_S_CROP:
3100 {
3101 struct v4l2_crop *crop = arg;
3102 const struct v4l2_rect *b;
3103 struct bttv_crop c;
3104 __s32 b_left;
3105 __s32 b_top;
3106 __s32 b_right;
3107 __s32 b_bottom;
3108
3109 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3110 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3111 return -EINVAL;
3112
3113 retval = v4l2_prio_check(&btv->prio,&fh->prio);
3114 if (0 != retval)
3115 return retval;
3116
3117 /* Make sure tvnorm, vbi_end and the current cropping
3118 parameters remain consistent until we're done. Note
3119 read() may change vbi_end in check_alloc_btres(). */
3120 mutex_lock(&btv->lock);
3121
3122 retval = -EBUSY;
3123
3124 if (locked_btres(fh->btv, VIDEO_RESOURCES))
3125 goto btv_unlock_and_return;
3126
3127 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3128
3129 b_left = b->left;
3130 b_right = b_left + b->width;
3131 b_bottom = b->top + b->height;
3132
3133 b_top = max(b->top, btv->vbi_end);
3134 if (b_top + 32 >= b_bottom)
3135 goto btv_unlock_and_return;
3136
3137 /* Min. scaled size 48 x 32. */
3138 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3139 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3140
3141 c.rect.width = clamp(crop->c.width,
3142 48, b_right - c.rect.left);
3143
3144 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3145 /* Top and height must be a multiple of two. */
3146 c.rect.top = (c.rect.top + 1) & ~1;
3147
3148 c.rect.height = clamp(crop->c.height,
3149 32, b_bottom - c.rect.top);
3150 c.rect.height = (c.rect.height + 1) & ~1;
3151
3152 bttv_crop_calc_limits(&c);
3153
3154 btv->crop[1] = c;
3155
3156 mutex_unlock(&btv->lock);
3157
3158 fh->do_crop = 1;
3159
3160 mutex_lock(&fh->cap.lock);
3161
3162 if (fh->width < c.min_scaled_width) {
3163 fh->width = c.min_scaled_width;
3164 btv->init.width = c.min_scaled_width;
3165 } else if (fh->width > c.max_scaled_width) {
3166 fh->width = c.max_scaled_width;
3167 btv->init.width = c.max_scaled_width;
3168 }
3169
3170 if (fh->height < c.min_scaled_height) {
3171 fh->height = c.min_scaled_height;
3172 btv->init.height = c.min_scaled_height;
3173 } else if (fh->height > c.max_scaled_height) {
3174 fh->height = c.max_scaled_height;
3175 btv->init.height = c.max_scaled_height;
3176 }
3177
3178 mutex_unlock(&fh->cap.lock);
3179
3180 return 0;
3181 }
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 case VIDIOC_ENUMSTD:
3184 case VIDIOC_G_STD:
3185 case VIDIOC_S_STD:
3186 case VIDIOC_ENUMINPUT:
3187 case VIDIOC_G_INPUT:
3188 case VIDIOC_S_INPUT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 case VIDIOC_S_TUNER:
3190 case VIDIOC_G_FREQUENCY:
3191 case VIDIOC_S_FREQUENCY:
Hans Verkuil299392b2005-11-08 21:37:42 -08003192 case VIDIOC_LOG_STATUS:
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03003193 case VIDIOC_G_CTRL:
3194 case VIDIOC_S_CTRL:
Trent Piepho55c0d102007-06-29 00:17:36 -03003195 case VIDIOC_DBG_G_REGISTER:
3196 case VIDIOC_DBG_S_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return bttv_common_ioctls(btv,cmd,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 default:
3199 return -ENOIOCTLCMD;
3200 }
3201 return 0;
3202
3203 fh_unlock_and_return:
Ingo Molnar3593cab2006-02-07 06:49:14 -02003204 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return retval;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003206
3207 btv_unlock_and_return:
3208 mutex_unlock(&btv->lock);
3209 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
3212static int bttv_ioctl(struct inode *inode, struct file *file,
3213 unsigned int cmd, unsigned long arg)
3214{
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03003215 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216}
3217
3218static ssize_t bttv_read(struct file *file, char __user *data,
3219 size_t count, loff_t *ppos)
3220{
3221 struct bttv_fh *fh = file->private_data;
3222 int retval = 0;
3223
3224 if (fh->btv->errors)
3225 bttv_reinit_bt848(fh->btv);
3226 dprintk("bttv%d: read count=%d type=%s\n",
3227 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3228
3229 switch (fh->type) {
3230 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03003231 if (!check_alloc_btres(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3232 /* VIDEO_READ in use by another fh,
3233 or VIDEO_STREAM by any fh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return -EBUSY;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3237 file->f_flags & O_NONBLOCK);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003238 free_btres(fh->btv, fh, RESOURCE_VIDEO_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 break;
3240 case V4L2_BUF_TYPE_VBI_CAPTURE:
3241 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3242 return -EBUSY;
3243 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3244 file->f_flags & O_NONBLOCK);
3245 break;
3246 default:
3247 BUG();
3248 }
3249 return retval;
3250}
3251
3252static unsigned int bttv_poll(struct file *file, poll_table *wait)
3253{
3254 struct bttv_fh *fh = file->private_data;
3255 struct bttv_buffer *buf;
3256 enum v4l2_field field;
3257
3258 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3259 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3260 return POLLERR;
3261 return videobuf_poll_stream(file, &fh->vbi, wait);
3262 }
3263
Michael Schimeke5bd0262007-01-18 16:17:39 -03003264 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 /* streaming capture */
3266 if (list_empty(&fh->cap.stream))
3267 return POLLERR;
3268 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3269 } else {
3270 /* read() capture */
Ingo Molnar3593cab2006-02-07 06:49:14 -02003271 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 if (NULL == fh->cap.read_buf) {
3273 /* need to capture a new frame */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003274 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02003275 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 return POLLERR;
3277 }
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03003278 fh->cap.read_buf = videobuf_pci_alloc(fh->cap.msize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 if (NULL == fh->cap.read_buf) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02003280 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return POLLERR;
3282 }
3283 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3284 field = videobuf_next_field(&fh->cap);
3285 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -08003286 kfree (fh->cap.read_buf);
3287 fh->cap.read_buf = NULL;
Ingo Molnar3593cab2006-02-07 06:49:14 -02003288 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 return POLLERR;
3290 }
3291 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3292 fh->cap.read_off = 0;
3293 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02003294 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 buf = (struct bttv_buffer*)fh->cap.read_buf;
3296 }
3297
3298 poll_wait(file, &buf->vb.done, wait);
3299 if (buf->vb.state == STATE_DONE ||
3300 buf->vb.state == STATE_ERROR)
3301 return POLLIN|POLLRDNORM;
3302 return 0;
3303}
3304
3305static int bttv_open(struct inode *inode, struct file *file)
3306{
3307 int minor = iminor(inode);
3308 struct bttv *btv = NULL;
3309 struct bttv_fh *fh;
3310 enum v4l2_buf_type type = 0;
3311 unsigned int i;
3312
3313 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3314
3315 for (i = 0; i < bttv_num; i++) {
3316 if (bttvs[i].video_dev &&
3317 bttvs[i].video_dev->minor == minor) {
3318 btv = &bttvs[i];
3319 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3320 break;
3321 }
3322 if (bttvs[i].vbi_dev &&
3323 bttvs[i].vbi_dev->minor == minor) {
3324 btv = &bttvs[i];
3325 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3326 break;
3327 }
3328 }
3329 if (NULL == btv)
3330 return -ENODEV;
3331
3332 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3333 btv->c.nr,v4l2_type_names[type]);
3334
3335 /* allocate per filehandle data */
3336 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3337 if (NULL == fh)
3338 return -ENOMEM;
3339 file->private_data = fh;
3340 *fh = btv->init;
3341 fh->type = type;
3342 fh->ov.setup_ok = 0;
3343 v4l2_prio_open(&btv->prio,&fh->prio);
3344
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03003345 videobuf_queue_pci_init(&fh->cap, &bttv_video_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 btv->c.pci, &btv->s_lock,
3347 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3348 V4L2_FIELD_INTERLACED,
3349 sizeof(struct bttv_buffer),
3350 fh);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03003351 videobuf_queue_pci_init(&fh->vbi, &bttv_vbi_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 btv->c.pci, &btv->s_lock,
3353 V4L2_BUF_TYPE_VBI_CAPTURE,
3354 V4L2_FIELD_SEQ_TB,
3355 sizeof(struct bttv_buffer),
3356 fh);
Nickolay V. Shmyrev302f61a2007-10-26 10:53:21 -03003357 set_tvnorm(btv,btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
3359 btv->users++;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003360
3361 /* The V4L2 spec requires one global set of cropping parameters
3362 which only change on request. These are stored in btv->crop[1].
3363 However for compatibility with V4L apps and cropping unaware
3364 V4L2 apps we now reset the cropping parameters as seen through
3365 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3366 will use btv->crop[0], the default cropping parameters for the
3367 current video standard, and VIDIOC_S_FMT will not implicitely
3368 change the cropping parameters until VIDIOC_S_CROP has been
3369 called. */
3370 fh->do_crop = !reset_crop; /* module parameter */
3371
3372 /* Likewise there should be one global set of VBI capture
3373 parameters, but for compatibility with V4L apps and earlier
3374 driver versions each fh has its own parameters. */
3375 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 bttv_field_count(btv);
3378 return 0;
3379}
3380
3381static int bttv_release(struct inode *inode, struct file *file)
3382{
3383 struct bttv_fh *fh = file->private_data;
3384 struct bttv *btv = fh->btv;
3385
3386 /* turn off overlay */
3387 if (check_btres(fh, RESOURCE_OVERLAY))
3388 bttv_switch_overlay(btv,fh,NULL);
3389
3390 /* stop video capture */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003391 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 videobuf_streamoff(&fh->cap);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003393 free_btres(btv,fh,RESOURCE_VIDEO_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 }
3395 if (fh->cap.read_buf) {
3396 buffer_release(&fh->cap,fh->cap.read_buf);
3397 kfree(fh->cap.read_buf);
3398 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03003399 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3400 free_btres(btv, fh, RESOURCE_VIDEO_READ);
3401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
3403 /* stop vbi capture */
3404 if (check_btres(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -03003405 videobuf_stop(&fh->vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 free_btres(btv,fh,RESOURCE_VBI);
3407 }
3408
3409 /* free stuff */
3410 videobuf_mmap_free(&fh->cap);
3411 videobuf_mmap_free(&fh->vbi);
3412 v4l2_prio_close(&btv->prio,&fh->prio);
3413 file->private_data = NULL;
3414 kfree(fh);
3415
3416 btv->users--;
3417 bttv_field_count(btv);
3418 return 0;
3419}
3420
3421static int
3422bttv_mmap(struct file *file, struct vm_area_struct *vma)
3423{
3424 struct bttv_fh *fh = file->private_data;
3425
3426 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3427 fh->btv->c.nr, v4l2_type_names[fh->type],
3428 vma->vm_start, vma->vm_end - vma->vm_start);
3429 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3430}
3431
Arjan van de Venfa027c22007-02-12 00:55:33 -08003432static const struct file_operations bttv_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
3434 .owner = THIS_MODULE,
3435 .open = bttv_open,
3436 .release = bttv_release,
3437 .ioctl = bttv_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02003438 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 .llseek = no_llseek,
3440 .read = bttv_read,
3441 .mmap = bttv_mmap,
3442 .poll = bttv_poll,
3443};
3444
3445static struct video_device bttv_video_template =
3446{
3447 .name = "UNSET",
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07003448 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003449 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 .fops = &bttv_fops,
3451 .minor = -1,
3452};
3453
3454static struct video_device bttv_vbi_template =
3455{
3456 .name = "bt848/878 vbi",
3457 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 .fops = &bttv_fops,
3459 .minor = -1,
3460};
3461
3462/* ----------------------------------------------------------------------- */
3463/* radio interface */
3464
3465static int radio_open(struct inode *inode, struct file *file)
3466{
3467 int minor = iminor(inode);
3468 struct bttv *btv = NULL;
3469 unsigned int i;
3470
3471 dprintk("bttv: open minor=%d\n",minor);
3472
3473 for (i = 0; i < bttv_num; i++) {
3474 if (bttvs[i].radio_dev->minor == minor) {
3475 btv = &bttvs[i];
3476 break;
3477 }
3478 }
3479 if (NULL == btv)
3480 return -ENODEV;
3481
3482 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02003483 mutex_lock(&btv->lock);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003484
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 btv->radio_user++;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 file->private_data = btv;
3488
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03003489 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,NULL);
3490 audio_input(btv,TVAUDIO_INPUT_RADIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02003492 mutex_unlock(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494}
3495
3496static int radio_release(struct inode *inode, struct file *file)
3497{
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003498 struct bttv *btv = file->private_data;
3499 struct rds_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
3501 btv->radio_user--;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003502
3503 bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 return 0;
3506}
3507
3508static int radio_do_ioctl(struct inode *inode, struct file *file,
3509 unsigned int cmd, void *arg)
3510{
3511 struct bttv *btv = file->private_data;
3512
3513 switch (cmd) {
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03003514 case VIDIOC_QUERYCAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 {
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03003516 struct v4l2_capability *cap = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
3518 memset(cap,0,sizeof(*cap));
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03003519 strcpy(cap->driver, "bttv");
3520 strlcpy(cap->card, btv->radio_dev->name,sizeof(cap->card));
3521 sprintf(cap->bus_info,"PCI:%s",pci_name(btv->c.pci));
3522 cap->version = BTTV_VERSION_CODE;
3523 cap->capabilities = V4L2_CAP_TUNER;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 }
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03003526 case VIDIOC_G_TUNER:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003527 {
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03003528 struct v4l2_tuner *t = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03003530 if (UNSET == bttv_tvcards[btv->c.type].tuner)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003531 return -EINVAL;
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03003532 if (0 != t->index)
3533 return -EINVAL;
3534 mutex_lock(&btv->lock);
3535 memset(t,0,sizeof(*t));
3536 strcpy(t->name, "Radio");
3537 t->type = V4L2_TUNER_RADIO;
3538
3539 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
3540
3541 mutex_unlock(&btv->lock);
3542
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003543 return 0;
3544 }
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03003545 case VIDIOC_S_TUNER:
3546 case VIDIOC_G_FREQUENCY:
3547 case VIDIOC_S_FREQUENCY:
3548 case VIDIOC_G_CTRL:
3549 case VIDIOC_S_CTRL:
Hans Verkuil5af0c8f2006-01-09 18:21:37 -02003550 case VIDIOC_LOG_STATUS:
Trent Piepho55c0d102007-06-29 00:17:36 -03003551 case VIDIOC_DBG_G_REGISTER:
3552 case VIDIOC_DBG_S_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return bttv_common_ioctls(btv,cmd,arg);
Nickolay V. Shmyreve84619b2007-10-26 11:01:08 -03003554 case VIDIOCGCAP:
3555 case VIDIOCGFREQ:
3556 case VIDIOCSFREQ:
3557 case VIDIOCGTUNER:
3558 case VIDIOCSTUNER:
3559 case VIDIOCGAUDIO:
3560 case VIDIOCSAUDIO:
3561 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
3562 radio_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 default:
3564 return -ENOIOCTLCMD;
3565 }
3566 return 0;
3567}
3568
3569static int radio_ioctl(struct inode *inode, struct file *file,
3570 unsigned int cmd, unsigned long arg)
3571{
3572 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3573}
3574
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003575static ssize_t radio_read(struct file *file, char __user *data,
3576 size_t count, loff_t *ppos)
3577{
3578 struct bttv *btv = file->private_data;
3579 struct rds_command cmd;
3580 cmd.block_count = count/3;
3581 cmd.buffer = data;
3582 cmd.instance = file;
3583 cmd.result = -ENODEV;
3584
3585 bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
3586
3587 return cmd.result;
3588}
3589
3590static unsigned int radio_poll(struct file *file, poll_table *wait)
3591{
3592 struct bttv *btv = file->private_data;
3593 struct rds_command cmd;
3594 cmd.instance = file;
3595 cmd.event_list = wait;
3596 cmd.result = -ENODEV;
3597 bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
3598
3599 return cmd.result;
3600}
3601
Arjan van de Venfa027c22007-02-12 00:55:33 -08003602static const struct file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603{
3604 .owner = THIS_MODULE,
3605 .open = radio_open,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003606 .read = radio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 .release = radio_release,
3608 .ioctl = radio_ioctl,
3609 .llseek = no_llseek,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003610 .poll = radio_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611};
3612
3613static struct video_device radio_template =
3614{
3615 .name = "bt848/878 radio",
3616 .type = VID_TYPE_TUNER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 .fops = &radio_fops,
3618 .minor = -1,
3619};
3620
3621/* ----------------------------------------------------------------------- */
3622/* some debug code */
3623
Adrian Bunk408b6642005-05-01 08:59:29 -07003624static int bttv_risc_decode(u32 risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
3626 static char *instr[16] = {
3627 [ BT848_RISC_WRITE >> 28 ] = "write",
3628 [ BT848_RISC_SKIP >> 28 ] = "skip",
3629 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3630 [ BT848_RISC_JUMP >> 28 ] = "jump",
3631 [ BT848_RISC_SYNC >> 28 ] = "sync",
3632 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3633 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3634 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3635 };
3636 static int incr[16] = {
3637 [ BT848_RISC_WRITE >> 28 ] = 2,
3638 [ BT848_RISC_JUMP >> 28 ] = 2,
3639 [ BT848_RISC_SYNC >> 28 ] = 2,
3640 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3641 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3642 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3643 };
3644 static char *bits[] = {
3645 "be0", "be1", "be2", "be3/resync",
3646 "set0", "set1", "set2", "set3",
3647 "clr0", "clr1", "clr2", "clr3",
3648 "irq", "res", "eol", "sol",
3649 };
3650 int i;
3651
3652 printk("0x%08x [ %s", risc,
3653 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3654 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3655 if (risc & (1 << (i + 12)))
3656 printk(" %s",bits[i]);
3657 printk(" count=%d ]\n", risc & 0xfff);
3658 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3659}
3660
Adrian Bunk408b6642005-05-01 08:59:29 -07003661static void bttv_risc_disasm(struct bttv *btv,
3662 struct btcx_riscmem *risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663{
3664 unsigned int i,j,n;
3665
3666 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3667 btv->c.name, risc->cpu, (unsigned long)risc->dma);
3668 for (i = 0; i < (risc->size >> 2); i += n) {
3669 printk("%s: 0x%lx: ", btv->c.name,
3670 (unsigned long)(risc->dma + (i<<2)));
3671 n = bttv_risc_decode(risc->cpu[i]);
3672 for (j = 1; j < n; j++)
3673 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3674 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3675 risc->cpu[i+j], j);
3676 if (0 == risc->cpu[i])
3677 break;
3678 }
3679}
3680
3681static void bttv_print_riscaddr(struct bttv *btv)
3682{
3683 printk(" main: %08Lx\n",
3684 (unsigned long long)btv->main.dma);
3685 printk(" vbi : o=%08Lx e=%08Lx\n",
3686 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3687 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3688 printk(" cap : o=%08Lx e=%08Lx\n",
3689 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3690 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3691 printk(" scr : o=%08Lx e=%08Lx\n",
3692 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3693 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3694 bttv_risc_disasm(btv, &btv->main);
3695}
3696
3697/* ----------------------------------------------------------------------- */
3698/* irq handler */
3699
3700static char *irq_name[] = {
3701 "FMTCHG", // format change detected (525 vs. 625)
3702 "VSYNC", // vertical sync (new field)
3703 "HSYNC", // horizontal sync
3704 "OFLOW", // chroma/luma AGC overflow
3705 "HLOCK", // horizontal lock changed
3706 "VPRES", // video presence changed
3707 "6", "7",
3708 "I2CDONE", // hw irc operation finished
3709 "GPINT", // gpio port triggered irq
3710 "10",
3711 "RISCI", // risc instruction triggered irq
3712 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3713 "FTRGT", // pixel data fifo overrun
3714 "FDSR", // fifo data stream resyncronisation
3715 "PPERR", // parity error (data transfer)
3716 "RIPERR", // parity error (read risc instructions)
3717 "PABORT", // pci abort
3718 "OCERR", // risc instruction error
3719 "SCERR", // syncronisation error
3720};
3721
3722static void bttv_print_irqbits(u32 print, u32 mark)
3723{
3724 unsigned int i;
3725
3726 printk("bits:");
3727 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3728 if (print & (1 << i))
3729 printk(" %s",irq_name[i]);
3730 if (mark & (1 << i))
3731 printk("*");
3732 }
3733}
3734
3735static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3736{
3737 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3738 btv->c.nr,
3739 (unsigned long)btv->main.dma,
3740 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
3741 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
3742 (unsigned long)rc);
3743
3744 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3745 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3746 "Ok, then this is harmless, don't worry ;)\n",
3747 btv->c.nr);
3748 return;
3749 }
3750 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3751 btv->c.nr);
3752 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3753 btv->c.nr);
3754 dump_stack();
3755}
3756
3757static int
3758bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3759{
3760 struct bttv_buffer *item;
3761
3762 memset(set,0,sizeof(*set));
3763
3764 /* capture request ? */
3765 if (!list_empty(&btv->capture)) {
3766 set->frame_irq = 1;
3767 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3768 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3769 set->top = item;
3770 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3771 set->bottom = item;
3772
3773 /* capture request for other field ? */
3774 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3775 (item->vb.queue.next != &btv->capture)) {
3776 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3777 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3778 if (NULL == set->top &&
3779 V4L2_FIELD_TOP == item->vb.field) {
3780 set->top = item;
3781 }
3782 if (NULL == set->bottom &&
3783 V4L2_FIELD_BOTTOM == item->vb.field) {
3784 set->bottom = item;
3785 }
3786 if (NULL != set->top && NULL != set->bottom)
3787 set->top_irq = 2;
3788 }
3789 }
3790 }
3791
3792 /* screen overlay ? */
3793 if (NULL != btv->screen) {
3794 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3795 if (NULL == set->top && NULL == set->bottom) {
3796 set->top = btv->screen;
3797 set->bottom = btv->screen;
3798 }
3799 } else {
3800 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3801 NULL == set->top) {
3802 set->top = btv->screen;
3803 }
3804 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3805 NULL == set->bottom) {
3806 set->bottom = btv->screen;
3807 }
3808 }
3809 }
3810
3811 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3812 btv->c.nr,set->top, set->bottom,
3813 btv->screen,set->frame_irq,set->top_irq);
3814 return 0;
3815}
3816
3817static void
3818bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3819 struct bttv_buffer_set *curr, unsigned int state)
3820{
3821 struct timeval ts;
3822
3823 do_gettimeofday(&ts);
3824
3825 if (wakeup->top == wakeup->bottom) {
3826 if (NULL != wakeup->top && curr->top != wakeup->top) {
3827 if (irq_debug > 1)
3828 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3829 wakeup->top->vb.ts = ts;
3830 wakeup->top->vb.field_count = btv->field_count;
3831 wakeup->top->vb.state = state;
3832 wake_up(&wakeup->top->vb.done);
3833 }
3834 } else {
3835 if (NULL != wakeup->top && curr->top != wakeup->top) {
3836 if (irq_debug > 1)
3837 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3838 wakeup->top->vb.ts = ts;
3839 wakeup->top->vb.field_count = btv->field_count;
3840 wakeup->top->vb.state = state;
3841 wake_up(&wakeup->top->vb.done);
3842 }
3843 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3844 if (irq_debug > 1)
3845 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3846 wakeup->bottom->vb.ts = ts;
3847 wakeup->bottom->vb.field_count = btv->field_count;
3848 wakeup->bottom->vb.state = state;
3849 wake_up(&wakeup->bottom->vb.done);
3850 }
3851 }
3852}
3853
3854static void
3855bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3856 unsigned int state)
3857{
3858 struct timeval ts;
3859
3860 if (NULL == wakeup)
3861 return;
3862
3863 do_gettimeofday(&ts);
3864 wakeup->vb.ts = ts;
3865 wakeup->vb.field_count = btv->field_count;
3866 wakeup->vb.state = state;
3867 wake_up(&wakeup->vb.done);
3868}
3869
3870static void bttv_irq_timeout(unsigned long data)
3871{
3872 struct bttv *btv = (struct bttv *)data;
3873 struct bttv_buffer_set old,new;
3874 struct bttv_buffer *ovbi;
3875 struct bttv_buffer *item;
3876 unsigned long flags;
3877
3878 if (bttv_verbose) {
3879 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3880 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3881 btread(BT848_RISC_COUNT));
3882 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3883 printk("\n");
3884 }
3885
3886 spin_lock_irqsave(&btv->s_lock,flags);
3887
3888 /* deactivate stuff */
3889 memset(&new,0,sizeof(new));
3890 old = btv->curr;
3891 ovbi = btv->cvbi;
3892 btv->curr = new;
3893 btv->cvbi = NULL;
3894 btv->loop_irq = 0;
3895 bttv_buffer_activate_video(btv, &new);
3896 bttv_buffer_activate_vbi(btv, NULL);
3897 bttv_set_dma(btv, 0);
3898
3899 /* wake up */
3900 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
3901 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
3902
3903 /* cancel all outstanding capture / vbi requests */
3904 while (!list_empty(&btv->capture)) {
3905 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3906 list_del(&item->vb.queue);
3907 item->vb.state = STATE_ERROR;
3908 wake_up(&item->vb.done);
3909 }
3910 while (!list_empty(&btv->vcapture)) {
3911 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3912 list_del(&item->vb.queue);
3913 item->vb.state = STATE_ERROR;
3914 wake_up(&item->vb.done);
3915 }
3916
3917 btv->errors++;
3918 spin_unlock_irqrestore(&btv->s_lock,flags);
3919}
3920
3921static void
3922bttv_irq_wakeup_top(struct bttv *btv)
3923{
3924 struct bttv_buffer *wakeup = btv->curr.top;
3925
3926 if (NULL == wakeup)
3927 return;
3928
3929 spin_lock(&btv->s_lock);
3930 btv->curr.top_irq = 0;
3931 btv->curr.top = NULL;
3932 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3933
3934 do_gettimeofday(&wakeup->vb.ts);
3935 wakeup->vb.field_count = btv->field_count;
3936 wakeup->vb.state = STATE_DONE;
3937 wake_up(&wakeup->vb.done);
3938 spin_unlock(&btv->s_lock);
3939}
3940
3941static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3942{
3943 if (rc < risc->dma)
3944 return 0;
3945 if (rc > risc->dma + risc->size)
3946 return 0;
3947 return 1;
3948}
3949
3950static void
3951bttv_irq_switch_video(struct bttv *btv)
3952{
3953 struct bttv_buffer_set new;
3954 struct bttv_buffer_set old;
3955 dma_addr_t rc;
3956
3957 spin_lock(&btv->s_lock);
3958
3959 /* new buffer set */
3960 bttv_irq_next_video(btv, &new);
3961 rc = btread(BT848_RISC_COUNT);
3962 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3963 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3964 btv->framedrop++;
3965 if (debug_latency)
3966 bttv_irq_debug_low_latency(btv, rc);
3967 spin_unlock(&btv->s_lock);
3968 return;
3969 }
3970
3971 /* switch over */
3972 old = btv->curr;
3973 btv->curr = new;
3974 btv->loop_irq &= ~1;
3975 bttv_buffer_activate_video(btv, &new);
3976 bttv_set_dma(btv, 0);
3977
3978 /* switch input */
3979 if (UNSET != btv->new_input) {
3980 video_mux(btv,btv->new_input);
3981 btv->new_input = UNSET;
3982 }
3983
3984 /* wake up finished buffers */
3985 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
3986 spin_unlock(&btv->s_lock);
3987}
3988
3989static void
3990bttv_irq_switch_vbi(struct bttv *btv)
3991{
3992 struct bttv_buffer *new = NULL;
3993 struct bttv_buffer *old;
3994 u32 rc;
3995
3996 spin_lock(&btv->s_lock);
3997
3998 if (!list_empty(&btv->vcapture))
3999 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4000 old = btv->cvbi;
4001
4002 rc = btread(BT848_RISC_COUNT);
4003 if (NULL != old && (is_active(&old->top, rc) ||
4004 is_active(&old->bottom, rc))) {
4005 btv->framedrop++;
4006 if (debug_latency)
4007 bttv_irq_debug_low_latency(btv, rc);
4008 spin_unlock(&btv->s_lock);
4009 return;
4010 }
4011
4012 /* switch */
4013 btv->cvbi = new;
4014 btv->loop_irq &= ~4;
4015 bttv_buffer_activate_vbi(btv, new);
4016 bttv_set_dma(btv, 0);
4017
4018 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
4019 spin_unlock(&btv->s_lock);
4020}
4021
David Howells7d12e782006-10-05 14:55:46 +01004022static irqreturn_t bttv_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
4024 u32 stat,astat;
4025 u32 dstat;
4026 int count;
4027 struct bttv *btv;
4028 int handled = 0;
4029
4030 btv=(struct bttv *)dev_id;
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004031
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004032 if (btv->custom_irq)
4033 handled = btv->custom_irq(btv);
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004034
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 count=0;
4036 while (1) {
4037 /* get/clear interrupt status bits */
4038 stat=btread(BT848_INT_STAT);
4039 astat=stat&btread(BT848_INT_MASK);
4040 if (!astat)
4041 break;
4042 handled = 1;
4043 btwrite(stat,BT848_INT_STAT);
4044
4045 /* get device status bits */
4046 dstat=btread(BT848_DSTATUS);
4047
4048 if (irq_debug) {
4049 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4050 "riscs=%x, riscc=%08x, ",
4051 btv->c.nr, count, btv->field_count,
4052 stat>>28, btread(BT848_RISC_COUNT));
4053 bttv_print_irqbits(stat,astat);
4054 if (stat & BT848_INT_HLOCK)
4055 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4056 ? "yes" : "no");
4057 if (stat & BT848_INT_VPRES)
4058 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4059 ? "yes" : "no");
4060 if (stat & BT848_INT_FMTCHG)
4061 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4062 ? "625" : "525");
4063 printk("\n");
4064 }
4065
4066 if (astat&BT848_INT_VSYNC)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004067 btv->field_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004069 if ((astat & BT848_INT_GPINT) && btv->remote) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004071 bttv_input_irq(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
4073
4074 if (astat & BT848_INT_I2CDONE) {
4075 btv->i2c_done = stat;
4076 wake_up(&btv->i2c_queue);
4077 }
4078
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004079 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 bttv_irq_switch_vbi(btv);
4081
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004082 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 bttv_irq_wakeup_top(btv);
4084
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004085 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 bttv_irq_switch_video(btv);
4087
4088 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004089 audio_mute(btv, btv->mute); /* trigger automute */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
4091 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4092 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4093 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4094 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4095 btread(BT848_RISC_COUNT));
4096 bttv_print_irqbits(stat,astat);
4097 printk("\n");
4098 if (bttv_debug)
4099 bttv_print_riscaddr(btv);
4100 }
4101 if (fdsr && astat & BT848_INT_FDSR) {
4102 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4103 btv->c.nr,btread(BT848_RISC_COUNT));
4104 if (bttv_debug)
4105 bttv_print_riscaddr(btv);
4106 }
4107
4108 count++;
4109 if (count > 4) {
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004110
4111 if (count > 8 || !(astat & BT848_INT_GPINT)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004112 btwrite(0, BT848_INT_MASK);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004113
4114 printk(KERN_ERR
4115 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4116 } else {
4117 printk(KERN_ERR
4118 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4119
4120 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4121 BT848_INT_MASK);
4122 };
4123
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 bttv_print_irqbits(stat,astat);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 printk("]\n");
4127 }
4128 }
4129 btv->irq_total++;
4130 if (handled)
4131 btv->irq_me++;
4132 return IRQ_RETVAL(handled);
4133}
4134
4135
4136/* ----------------------------------------------------------------------- */
4137/* initialitation */
4138
4139static struct video_device *vdev_init(struct bttv *btv,
4140 struct video_device *template,
4141 char *type)
4142{
4143 struct video_device *vfd;
4144
4145 vfd = video_device_alloc();
4146 if (NULL == vfd)
4147 return NULL;
4148 *vfd = *template;
4149 vfd->minor = -1;
4150 vfd->dev = &btv->c.pci->dev;
4151 vfd->release = video_device_release;
4152 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4153 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4154 type, bttv_tvcards[btv->c.type].name);
4155 return vfd;
4156}
4157
4158static void bttv_unregister_video(struct bttv *btv)
4159{
4160 if (btv->video_dev) {
4161 if (-1 != btv->video_dev->minor)
4162 video_unregister_device(btv->video_dev);
4163 else
4164 video_device_release(btv->video_dev);
4165 btv->video_dev = NULL;
4166 }
4167 if (btv->vbi_dev) {
4168 if (-1 != btv->vbi_dev->minor)
4169 video_unregister_device(btv->vbi_dev);
4170 else
4171 video_device_release(btv->vbi_dev);
4172 btv->vbi_dev = NULL;
4173 }
4174 if (btv->radio_dev) {
4175 if (-1 != btv->radio_dev->minor)
4176 video_unregister_device(btv->radio_dev);
4177 else
4178 video_device_release(btv->radio_dev);
4179 btv->radio_dev = NULL;
4180 }
4181}
4182
4183/* register video4linux devices */
4184static int __devinit bttv_register_video(struct bttv *btv)
4185{
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004186 if (no_overlay <= 0) {
4187 bttv_video_template.type |= VID_TYPE_OVERLAY;
4188 } else {
4189 printk("bttv: Overlay support disabled.\n");
4190 }
4191
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 /* video */
4193 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004194 if (NULL == btv->video_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 goto err;
4196 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
4197 goto err;
4198 printk(KERN_INFO "bttv%d: registered device video%d\n",
4199 btv->c.nr,btv->video_dev->minor & 0x1f);
Kay Sievers54bd5b62007-10-08 16:26:13 -03004200 if (device_create_file(&btv->video_dev->class_dev,
4201 &dev_attr_card)<0) {
4202 printk(KERN_ERR "bttv%d: device_create_file 'card' "
Trent Piephod94fc9a2006-07-29 17:18:06 -03004203 "failed\n", btv->c.nr);
4204 goto err;
4205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
4207 /* vbi */
4208 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004209 if (NULL == btv->vbi_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 goto err;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004211 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 goto err;
4213 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
4214 btv->c.nr,btv->vbi_dev->minor & 0x1f);
4215
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004216 if (!btv->has_radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 return 0;
4218 /* radio */
4219 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004220 if (NULL == btv->radio_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 goto err;
4222 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
4223 goto err;
4224 printk(KERN_INFO "bttv%d: registered device radio%d\n",
4225 btv->c.nr,btv->radio_dev->minor & 0x1f);
4226
4227 /* all done */
4228 return 0;
4229
4230 err:
4231 bttv_unregister_video(btv);
4232 return -1;
4233}
4234
4235
4236/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4237/* response on cards with no firmware is not enabled by OF */
4238static void pci_set_command(struct pci_dev *dev)
4239{
4240#if defined(__powerpc__)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004241 unsigned int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004243 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4244 cmd = (cmd | PCI_COMMAND_MEMORY );
4245 pci_write_config_dword(dev, PCI_COMMAND, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246#endif
4247}
4248
4249static int __devinit bttv_probe(struct pci_dev *dev,
4250 const struct pci_device_id *pci_id)
4251{
4252 int result;
4253 unsigned char lat;
4254 struct bttv *btv;
4255
4256 if (bttv_num == BTTV_MAX)
4257 return -ENOMEM;
4258 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004259 btv=&bttvs[bttv_num];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 memset(btv,0,sizeof(*btv));
4261 btv->c.nr = bttv_num;
4262 sprintf(btv->c.name,"bttv%d",btv->c.nr);
4263
4264 /* initialize structs / fill in defaults */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02004265 mutex_init(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004266 spin_lock_init(&btv->s_lock);
4267 spin_lock_init(&btv->gpio_lock);
4268 init_waitqueue_head(&btv->gpioq);
4269 init_waitqueue_head(&btv->i2c_queue);
4270 INIT_LIST_HEAD(&btv->c.subs);
4271 INIT_LIST_HEAD(&btv->capture);
4272 INIT_LIST_HEAD(&btv->vcapture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 v4l2_prio_init(&btv->prio);
4274
4275 init_timer(&btv->timeout);
4276 btv->timeout.function = bttv_irq_timeout;
4277 btv->timeout.data = (unsigned long)btv;
4278
Michael Krufky7c08fb02005-11-08 21:36:21 -08004279 btv->i2c_rc = -1;
4280 btv->tuner_type = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 btv->new_input = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 btv->has_radio=radio[btv->c.nr];
4283
4284 /* pci stuff (init, get irq/mmio, ... */
4285 btv->c.pci = dev;
Michael Krufky7c08fb02005-11-08 21:36:21 -08004286 btv->id = dev->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 if (pci_enable_device(dev)) {
Michael Krufky7c08fb02005-11-08 21:36:21 -08004288 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 btv->c.nr);
4290 return -EIO;
4291 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004292 if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
4293 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 btv->c.nr);
4295 return -EIO;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 if (!request_mem_region(pci_resource_start(dev,0),
4298 pci_resource_len(dev,0),
4299 btv->c.name)) {
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004300 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4301 btv->c.nr,
4302 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 return -EBUSY;
4304 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004305 pci_set_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 pci_set_command(dev);
4307 pci_set_drvdata(dev,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004309 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4310 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4311 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4312 bttv_num,btv->id, btv->revision, pci_name(dev));
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004313 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4314 btv->c.pci->irq, lat,
4315 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 schedule();
4317
Akinobu Mita5f1693f2006-12-20 10:08:56 -03004318 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4319 if (NULL == btv->bt848_mmio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4321 result = -EIO;
4322 goto fail1;
4323 }
4324
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004325 /* identify card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 bttv_idcard(btv);
4327
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004328 /* disable irqs, register irq handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 btwrite(0, BT848_INT_MASK);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004330 result = request_irq(btv->c.pci->irq, bttv_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -07004331 IRQF_SHARED | IRQF_DISABLED,btv->c.name,(void *)btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004332 if (result < 0) {
4333 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 bttv_num,btv->c.pci->irq);
4335 goto fail1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
4338 if (0 != bttv_handle_chipset(btv)) {
4339 result = -EIO;
4340 goto fail2;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
4343 /* init options from insmod args */
4344 btv->opt_combfilter = combfilter;
4345 btv->opt_lumafilter = lumafilter;
4346 btv->opt_automute = automute;
4347 btv->opt_chroma_agc = chroma_agc;
4348 btv->opt_adc_crush = adc_crush;
4349 btv->opt_vcr_hack = vcr_hack;
4350 btv->opt_whitecrush_upper = whitecrush_upper;
4351 btv->opt_whitecrush_lower = whitecrush_lower;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07004352 btv->opt_uv_ratio = uv_ratio;
4353 btv->opt_full_luma_range = full_luma_range;
4354 btv->opt_coring = coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
4356 /* fill struct bttv with some useful defaults */
4357 btv->init.btv = btv;
4358 btv->init.ov.w.width = 320;
4359 btv->init.ov.w.height = 240;
4360 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
4361 btv->init.width = 320;
4362 btv->init.height = 240;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 btv->input = 0;
4364
4365 /* initialize hardware */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004366 if (bttv_gpio)
4367 bttv_gpio_tracking(btv,"pre-init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
4369 bttv_risc_init_main(btv);
4370 init_bt848(btv);
4371
4372 /* gpio */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004373 btwrite(0x00, BT848_GPIO_REG_INP);
4374 btwrite(0x00, BT848_GPIO_OUT_EN);
4375 if (bttv_verbose)
4376 bttv_gpio_tracking(btv,"init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004378 /* needs to be done before i2c is registered */
4379 bttv_init_card1(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004381 /* register i2c + gpio */
4382 init_bttv_i2c(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004384 /* some card-specific stuff (needs working i2c) */
4385 bttv_init_card2(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 init_irqreg(btv);
4387
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004388 /* register video4linux + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 if (!bttv_tvcards[btv->c.type].no_video) {
4390 bttv_register_video(btv);
4391 bt848_bright(btv,32768);
4392 bt848_contrast(btv,32768);
4393 bt848_hue(btv,32768);
4394 bt848_sat(btv,32768);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004395 audio_mute(btv, 1);
Trent Piepho333408f2007-07-03 15:08:10 -03004396 set_input(btv, 0, btv->tvnorm);
Michael Schimeke5bd0262007-01-18 16:17:39 -03004397 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4398 btv->crop[1] = btv->crop[0]; /* current = default */
4399 disclaim_vbi_lines(btv);
4400 disclaim_video_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 }
4402
Jarod Wilsonf992a492007-03-24 15:23:50 -03004403 /* add subdevices and autoload dvb-bt8xx if needed */
4404 if (bttv_tvcards[btv->c.type].has_dvb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 bttv_sub_add_device(&btv->c, "dvb");
Jarod Wilsonf992a492007-03-24 15:23:50 -03004406 request_modules(btv);
4407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004409 bttv_input_init(btv);
4410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 /* everything is fine */
4412 bttv_num++;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
4415 fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004416 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
4418 fail1:
4419 if (btv->bt848_mmio)
4420 iounmap(btv->bt848_mmio);
4421 release_mem_region(pci_resource_start(btv->c.pci,0),
4422 pci_resource_len(btv->c.pci,0));
4423 pci_set_drvdata(dev,NULL);
4424 return result;
4425}
4426
4427static void __devexit bttv_remove(struct pci_dev *pci_dev)
4428{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004429 struct bttv *btv = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
4431 if (bttv_verbose)
4432 printk("bttv%d: unloading\n",btv->c.nr);
4433
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004434 /* shutdown everything (DMA+IRQs) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 btand(~15, BT848_GPIO_DMA_CTL);
4436 btwrite(0, BT848_INT_MASK);
4437 btwrite(~0x0, BT848_INT_STAT);
4438 btwrite(0x0, BT848_GPIO_OUT_EN);
4439 if (bttv_gpio)
4440 bttv_gpio_tracking(btv,"cleanup");
4441
4442 /* tell gpio modules we are leaving ... */
4443 btv->shutdown=1;
4444 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004445 bttv_input_fini(btv);
Christopher Pascoe889aee82006-01-09 15:25:28 -02004446 bttv_sub_del_devices(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004448 /* unregister i2c_bus + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 fini_bttv_i2c(btv);
4450
4451 /* unregister video4linux */
4452 bttv_unregister_video(btv);
4453
4454 /* free allocated memory */
4455 btcx_riscmem_free(btv->c.pci,&btv->main);
4456
4457 /* free ressources */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004458 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 iounmap(btv->bt848_mmio);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004460 release_mem_region(pci_resource_start(btv->c.pci,0),
4461 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
4463 pci_set_drvdata(pci_dev, NULL);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004464 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465}
4466
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004467#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4469{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004470 struct bttv *btv = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 struct bttv_buffer_set idle;
4472 unsigned long flags;
4473
Mauro Carvalho Chehab0f97a932005-09-09 13:04:05 -07004474 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
4476 /* stop dma + irqs */
4477 spin_lock_irqsave(&btv->s_lock,flags);
4478 memset(&idle, 0, sizeof(idle));
4479 btv->state.video = btv->curr;
4480 btv->state.vbi = btv->cvbi;
4481 btv->state.loop_irq = btv->loop_irq;
4482 btv->curr = idle;
4483 btv->loop_irq = 0;
4484 bttv_buffer_activate_video(btv, &idle);
4485 bttv_buffer_activate_vbi(btv, NULL);
4486 bttv_set_dma(btv, 0);
4487 btwrite(0, BT848_INT_MASK);
4488 spin_unlock_irqrestore(&btv->s_lock,flags);
4489
4490 /* save bt878 state */
4491 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4492 btv->state.gpio_data = gpio_read();
4493
4494 /* save pci state */
4495 pci_save_state(pci_dev);
4496 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4497 pci_disable_device(pci_dev);
4498 btv->state.disabled = 1;
4499 }
4500 return 0;
4501}
4502
4503static int bttv_resume(struct pci_dev *pci_dev)
4504{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004505 struct bttv *btv = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004507 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
4509 dprintk("bttv%d: resume\n", btv->c.nr);
4510
4511 /* restore pci state */
4512 if (btv->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004513 err=pci_enable_device(pci_dev);
4514 if (err) {
4515 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4516 btv->c.nr);
4517 return err;
4518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 btv->state.disabled = 0;
4520 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004521 err=pci_set_power_state(pci_dev, PCI_D0);
4522 if (err) {
4523 pci_disable_device(pci_dev);
4524 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4525 btv->c.nr);
4526 btv->state.disabled = 1;
4527 return err;
4528 }
4529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 pci_restore_state(pci_dev);
4531
4532 /* restore bt878 state */
4533 bttv_reinit_bt848(btv);
4534 gpio_inout(0xffffff, btv->state.gpio_enable);
4535 gpio_write(btv->state.gpio_data);
4536
4537 /* restart dma */
4538 spin_lock_irqsave(&btv->s_lock,flags);
4539 btv->curr = btv->state.video;
4540 btv->cvbi = btv->state.vbi;
4541 btv->loop_irq = btv->state.loop_irq;
4542 bttv_buffer_activate_video(btv, &btv->curr);
4543 bttv_buffer_activate_vbi(btv, btv->cvbi);
4544 bttv_set_dma(btv, 0);
4545 spin_unlock_irqrestore(&btv->s_lock,flags);
4546 return 0;
4547}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004548#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
4550static struct pci_device_id bttv_pci_tbl[] = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004551 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4552 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004554 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004556 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004558 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4559 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560};
4561
4562MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4563
4564static struct pci_driver bttv_pci_driver = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004565 .name = "bttv",
4566 .id_table = bttv_pci_tbl,
4567 .probe = bttv_probe,
4568 .remove = __devexit_p(bttv_remove),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004569#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 .suspend = bttv_suspend,
4571 .resume = bttv_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004572#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573};
4574
Adrian Bunk7d44e892007-12-11 19:23:43 -03004575static int __init bttv_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576{
Randy Dunlapc526e222006-07-15 09:08:26 -03004577 int ret;
4578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 bttv_num = 0;
4580
4581 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4582 (BTTV_VERSION_CODE >> 16) & 0xff,
4583 (BTTV_VERSION_CODE >> 8) & 0xff,
4584 BTTV_VERSION_CODE & 0xff);
4585#ifdef SNAPSHOT
4586 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4587 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4588#endif
4589 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4590 gbuffers = 2;
4591 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4592 gbufsize = BTTV_MAX_FBUF;
4593 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4594 if (bttv_verbose)
4595 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4596 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4597
4598 bttv_check_chipset();
4599
Randy Dunlapc526e222006-07-15 09:08:26 -03004600 ret = bus_register(&bttv_sub_bus_type);
4601 if (ret < 0) {
4602 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4603 return ret;
4604 }
Otavio Salvador23047592006-01-09 15:25:17 -02004605 return pci_register_driver(&bttv_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606}
4607
Adrian Bunk7d44e892007-12-11 19:23:43 -03004608static void __exit bttv_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609{
4610 pci_unregister_driver(&bttv_pci_driver);
4611 bus_unregister(&bttv_sub_bus_type);
4612 return;
4613}
4614
4615module_init(bttv_init_module);
4616module_exit(bttv_cleanup_module);
4617
4618/*
4619 * Local variables:
4620 * c-basic-offset: 8
4621 * End:
4622 */