blob: 4ab4e14b5c645261e041812940c60bd6cbf83276 [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
Michael Schimeke5bd0262007-01-18 16:17:39 -030012 Cropping and overscan support
13 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
14 Sponsored by OPQ Systems AB
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29*/
30
31#include <linux/init.h>
32#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
35#include <linux/fs.h>
36#include <linux/kernel.h>
37#include <linux/sched.h>
38#include <linux/interrupt.h>
39#include <linux/kdev_t.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020040#include "bttvp.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020041#include <media/v4l2-common.h>
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -030042#include <media/tvaudio.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030043#include <media/msp3400.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020044
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -070045#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <asm/io.h>
48#include <asm/byteorder.h>
49
Mauro Carvalho Chehabfa3fcce2006-03-23 21:45:24 -030050#include <media/rds.h>
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070051
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053unsigned int bttv_num; /* number of Bt848s in use */
54struct bttv bttvs[BTTV_MAX];
55
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020056unsigned int bttv_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057unsigned int bttv_verbose = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020058unsigned int bttv_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/* config variables */
61#ifdef __BIG_ENDIAN
62static unsigned int bigendian=1;
63#else
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020064static unsigned int bigendian;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#endif
66static unsigned int radio[BTTV_MAX];
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020067static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static unsigned int gbuffers = 8;
69static unsigned int gbufsize = 0x208000;
Michael Schimeke5bd0262007-01-18 16:17:39 -030070static unsigned int reset_crop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static int video_nr = -1;
73static int radio_nr = -1;
74static int vbi_nr = -1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020075static int debug_latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020077static unsigned int fdsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* options */
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020080static unsigned int combfilter;
81static unsigned int lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static unsigned int automute = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020083static unsigned int chroma_agc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static unsigned int adc_crush = 1;
85static unsigned int whitecrush_upper = 0xCF;
86static unsigned int whitecrush_lower = 0x7F;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020087static unsigned int vcr_hack;
88static unsigned int irq_iswitch;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -070089static unsigned int uv_ratio = 50;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020090static unsigned int full_luma_range;
91static unsigned int coring;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -070092extern int no_overlay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/* API features (turn on/off stuff for testing) */
95static unsigned int v4l2 = 1;
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/* insmod args */
98module_param(bttv_verbose, int, 0644);
99module_param(bttv_gpio, int, 0644);
100module_param(bttv_debug, int, 0644);
101module_param(irq_debug, int, 0644);
102module_param(debug_latency, int, 0644);
103
104module_param(fdsr, int, 0444);
105module_param(video_nr, int, 0444);
106module_param(radio_nr, int, 0444);
107module_param(vbi_nr, int, 0444);
108module_param(gbuffers, int, 0444);
109module_param(gbufsize, int, 0444);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300110module_param(reset_crop, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112module_param(v4l2, int, 0644);
113module_param(bigendian, int, 0644);
114module_param(irq_iswitch, int, 0644);
115module_param(combfilter, int, 0444);
116module_param(lumafilter, int, 0444);
117module_param(automute, int, 0444);
118module_param(chroma_agc, int, 0444);
119module_param(adc_crush, int, 0444);
120module_param(whitecrush_upper, int, 0444);
121module_param(whitecrush_lower, int, 0444);
122module_param(vcr_hack, int, 0444);
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700123module_param(uv_ratio, int, 0444);
124module_param(full_luma_range, int, 0444);
125module_param(coring, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127module_param_array(radio, int, NULL, 0444);
128
129MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
130MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
131MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
132MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
133MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
134MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
135MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
136MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
Michael Schimeke5bd0262007-01-18 16:17:39 -0300137MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
138 "is 1 (yes) for compatibility with older applications");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
140MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
141MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
142MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
143MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
144MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
145MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700146MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
147MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
148MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
151MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
152MODULE_LICENSE("GPL");
153
154/* ----------------------------------------------------------------------- */
155/* sysfs */
156
157static ssize_t show_card(struct class_device *cd, char *buf)
158{
159 struct video_device *vfd = to_video_device(cd);
160 struct bttv *btv = dev_get_drvdata(vfd->dev);
161 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
162}
163static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
164
165/* ----------------------------------------------------------------------- */
Jarod Wilsonf992a492007-03-24 15:23:50 -0300166/* dvb auto-load setup */
167#if defined(CONFIG_MODULES) && defined(MODULE)
168static void request_module_async(struct work_struct *work)
169{
170 request_module("dvb-bt8xx");
171}
172
173static void request_modules(struct bttv *dev)
174{
175 INIT_WORK(&dev->request_module_wk, request_module_async);
176 schedule_work(&dev->request_module_wk);
177}
178#else
179#define request_modules(dev)
180#endif /* CONFIG_MODULES */
181
182
183/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/* static data */
185
186/* special timing tables from conexant... */
187static u8 SRAM_Table[][60] =
188{
189 /* PAL digital input over GPIO[7:0] */
190 {
191 45, // 45 bytes following
192 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
193 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
194 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
195 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
196 0x37,0x00,0xAF,0x21,0x00
197 },
198 /* NTSC digital input over GPIO[7:0] */
199 {
200 51, // 51 bytes following
201 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
202 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
203 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
204 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
205 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
206 0x00,
207 },
208 // TGB_NTSC392 // quartzsight
209 // This table has been modified to be used for Fusion Rev D
210 {
211 0x2A, // size of table = 42
212 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
213 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
214 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
215 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
216 0x20, 0x00
217 }
218};
219
Michael Schimeke5bd0262007-01-18 16:17:39 -0300220/* minhdelayx1 first video pixel we can capture on a line and
221 hdelayx1 start of active video, both relative to rising edge of
222 /HRESET pulse (0H) in 1 / fCLKx1.
223 swidth width of active video and
224 totalwidth total line width, both in 1 / fCLKx1.
225 sqwidth total line width in square pixels.
226 vdelay start of active video in 2 * field lines relative to
227 trailing edge of /VRESET pulse (VDELAY register).
228 sheight height of active video in 2 * field lines.
229 videostart0 ITU-R frame line number of the line corresponding
230 to vdelay in the first field. */
231#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
232 vdelay, sheight, videostart0) \
233 .cropcap.bounds.left = minhdelayx1, \
234 /* * 2 because vertically we count field lines times two, */ \
235 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
236 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
237 /* 4 is a safety margin at the end of the line. */ \
238 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
239 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
240 .cropcap.defrect.left = hdelayx1, \
241 .cropcap.defrect.top = (videostart0) * 2, \
242 .cropcap.defrect.width = swidth, \
243 .cropcap.defrect.height = sheight, \
244 .cropcap.pixelaspect.numerator = totalwidth, \
245 .cropcap.pixelaspect.denominator = sqwidth,
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247const struct bttv_tvnorm bttv_tvnorms[] = {
248 /* PAL-BDGHI */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800249 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
250 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 {
252 .v4l2_id = V4L2_STD_PAL,
253 .name = "PAL",
254 .Fsc = 35468950,
255 .swidth = 924,
256 .sheight = 576,
257 .totalwidth = 1135,
258 .adelay = 0x7f,
259 .bdelay = 0x72,
260 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
261 .scaledtwidth = 1135,
262 .hdelayx1 = 186,
263 .hactivex1 = 924,
264 .vdelay = 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300265 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .sram = 0,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200267 /* ITU-R frame line number of the first VBI line
Michael Schimeke5bd0262007-01-18 16:17:39 -0300268 we can capture, of the first and second field.
269 The last line is determined by cropcap.bounds. */
270 .vbistart = { 7, 320 },
271 CROPCAP(/* minhdelayx1 */ 68,
272 /* hdelayx1 */ 186,
273 /* Should be (768 * 1135 + 944 / 2) / 944.
274 cropcap.defrect is used for image width
275 checks, so we keep the old value 924. */
276 /* swidth */ 924,
277 /* totalwidth */ 1135,
278 /* sqwidth */ 944,
279 /* vdelay */ 0x20,
280 /* sheight */ 576,
281 /* videostart0 */ 23)
282 /* bt878 (and bt848?) can capture another
283 line below active video. */
284 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 },{
Hans Verkuild97a11e2006-02-07 06:48:40 -0200286 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 .name = "NTSC",
288 .Fsc = 28636363,
289 .swidth = 768,
290 .sheight = 480,
291 .totalwidth = 910,
292 .adelay = 0x68,
293 .bdelay = 0x5d,
294 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
295 .scaledtwidth = 910,
296 .hdelayx1 = 128,
297 .hactivex1 = 910,
298 .vdelay = 0x1a,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300299 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 .sram = 1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200301 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300302 CROPCAP(/* minhdelayx1 */ 68,
303 /* hdelayx1 */ 128,
304 /* Should be (640 * 910 + 780 / 2) / 780? */
305 /* swidth */ 768,
306 /* totalwidth */ 910,
307 /* sqwidth */ 780,
308 /* vdelay */ 0x1a,
309 /* sheight */ 480,
310 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 },{
312 .v4l2_id = V4L2_STD_SECAM,
313 .name = "SECAM",
314 .Fsc = 35468950,
315 .swidth = 924,
316 .sheight = 576,
317 .totalwidth = 1135,
318 .adelay = 0x7f,
319 .bdelay = 0xb0,
320 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
321 .scaledtwidth = 1135,
322 .hdelayx1 = 186,
323 .hactivex1 = 922,
324 .vdelay = 0x20,
325 .vbipack = 255,
326 .sram = 0, /* like PAL, correct? */
Michael H. Schimek67f15702006-01-09 15:25:27 -0200327 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300328 CROPCAP(/* minhdelayx1 */ 68,
329 /* hdelayx1 */ 186,
330 /* swidth */ 924,
331 /* totalwidth */ 1135,
332 /* sqwidth */ 944,
333 /* vdelay */ 0x20,
334 /* sheight */ 576,
335 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 },{
337 .v4l2_id = V4L2_STD_PAL_Nc,
338 .name = "PAL-Nc",
339 .Fsc = 28636363,
340 .swidth = 640,
341 .sheight = 576,
342 .totalwidth = 910,
343 .adelay = 0x68,
344 .bdelay = 0x5d,
345 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
346 .scaledtwidth = 780,
347 .hdelayx1 = 130,
348 .hactivex1 = 734,
349 .vdelay = 0x1a,
350 .vbipack = 144,
351 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200352 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300353 CROPCAP(/* minhdelayx1 */ 68,
354 /* hdelayx1 */ 130,
355 /* swidth */ (640 * 910 + 780 / 2) / 780,
356 /* totalwidth */ 910,
357 /* sqwidth */ 780,
358 /* vdelay */ 0x1a,
359 /* sheight */ 576,
360 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 },{
362 .v4l2_id = V4L2_STD_PAL_M,
363 .name = "PAL-M",
364 .Fsc = 28636363,
365 .swidth = 640,
366 .sheight = 480,
367 .totalwidth = 910,
368 .adelay = 0x68,
369 .bdelay = 0x5d,
370 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
371 .scaledtwidth = 780,
372 .hdelayx1 = 135,
373 .hactivex1 = 754,
374 .vdelay = 0x1a,
375 .vbipack = 144,
376 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200377 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300378 CROPCAP(/* minhdelayx1 */ 68,
379 /* hdelayx1 */ 135,
380 /* swidth */ (640 * 910 + 780 / 2) / 780,
381 /* totalwidth */ 910,
382 /* sqwidth */ 780,
383 /* vdelay */ 0x1a,
384 /* sheight */ 480,
385 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 },{
387 .v4l2_id = V4L2_STD_PAL_N,
388 .name = "PAL-N",
389 .Fsc = 35468950,
390 .swidth = 768,
391 .sheight = 576,
392 .totalwidth = 1135,
393 .adelay = 0x7f,
394 .bdelay = 0x72,
395 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
396 .scaledtwidth = 944,
397 .hdelayx1 = 186,
398 .hactivex1 = 922,
399 .vdelay = 0x20,
400 .vbipack = 144,
401 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300402 .vbistart = { 7, 320 },
403 CROPCAP(/* minhdelayx1 */ 68,
404 /* hdelayx1 */ 186,
405 /* swidth */ (768 * 1135 + 944 / 2) / 944,
406 /* totalwidth */ 1135,
407 /* sqwidth */ 944,
408 /* vdelay */ 0x20,
409 /* sheight */ 576,
410 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 },{
412 .v4l2_id = V4L2_STD_NTSC_M_JP,
413 .name = "NTSC-JP",
414 .Fsc = 28636363,
415 .swidth = 640,
416 .sheight = 480,
417 .totalwidth = 910,
418 .adelay = 0x68,
419 .bdelay = 0x5d,
420 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
421 .scaledtwidth = 780,
422 .hdelayx1 = 135,
423 .hactivex1 = 754,
424 .vdelay = 0x16,
425 .vbipack = 144,
426 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300427 .vbistart = { 10, 273 },
428 CROPCAP(/* minhdelayx1 */ 68,
429 /* hdelayx1 */ 135,
430 /* swidth */ (640 * 910 + 780 / 2) / 780,
431 /* totalwidth */ 910,
432 /* sqwidth */ 780,
433 /* vdelay */ 0x16,
434 /* sheight */ 480,
435 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 },{
437 /* that one hopefully works with the strange timing
438 * which video recorders produce when playing a NTSC
439 * tape on a PAL TV ... */
440 .v4l2_id = V4L2_STD_PAL_60,
441 .name = "PAL-60",
442 .Fsc = 35468950,
443 .swidth = 924,
444 .sheight = 480,
445 .totalwidth = 1135,
446 .adelay = 0x7f,
447 .bdelay = 0x72,
448 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
449 .scaledtwidth = 1135,
450 .hdelayx1 = 186,
451 .hactivex1 = 924,
452 .vdelay = 0x1a,
453 .vbipack = 255,
454 .vtotal = 524,
455 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200456 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300457 CROPCAP(/* minhdelayx1 */ 68,
458 /* hdelayx1 */ 186,
459 /* swidth */ 924,
460 /* totalwidth */ 1135,
461 /* sqwidth */ 944,
462 /* vdelay */ 0x1a,
463 /* sheight */ 480,
464 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466};
467static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
468
469/* ----------------------------------------------------------------------- */
470/* bttv format list
471 packed pixel formats must come first */
472static const struct bttv_format bttv_formats[] = {
473 {
474 .name = "8 bpp, gray",
475 .palette = VIDEO_PALETTE_GREY,
476 .fourcc = V4L2_PIX_FMT_GREY,
477 .btformat = BT848_COLOR_FMT_Y8,
478 .depth = 8,
479 .flags = FORMAT_FLAGS_PACKED,
480 },{
481 .name = "8 bpp, dithered color",
482 .palette = VIDEO_PALETTE_HI240,
483 .fourcc = V4L2_PIX_FMT_HI240,
484 .btformat = BT848_COLOR_FMT_RGB8,
485 .depth = 8,
486 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
487 },{
488 .name = "15 bpp RGB, le",
489 .palette = VIDEO_PALETTE_RGB555,
490 .fourcc = V4L2_PIX_FMT_RGB555,
491 .btformat = BT848_COLOR_FMT_RGB15,
492 .depth = 16,
493 .flags = FORMAT_FLAGS_PACKED,
494 },{
495 .name = "15 bpp RGB, be",
496 .palette = -1,
497 .fourcc = V4L2_PIX_FMT_RGB555X,
498 .btformat = BT848_COLOR_FMT_RGB15,
499 .btswap = 0x03, /* byteswap */
500 .depth = 16,
501 .flags = FORMAT_FLAGS_PACKED,
502 },{
503 .name = "16 bpp RGB, le",
504 .palette = VIDEO_PALETTE_RGB565,
505 .fourcc = V4L2_PIX_FMT_RGB565,
506 .btformat = BT848_COLOR_FMT_RGB16,
507 .depth = 16,
508 .flags = FORMAT_FLAGS_PACKED,
509 },{
510 .name = "16 bpp RGB, be",
511 .palette = -1,
512 .fourcc = V4L2_PIX_FMT_RGB565X,
513 .btformat = BT848_COLOR_FMT_RGB16,
514 .btswap = 0x03, /* byteswap */
515 .depth = 16,
516 .flags = FORMAT_FLAGS_PACKED,
517 },{
518 .name = "24 bpp RGB, le",
519 .palette = VIDEO_PALETTE_RGB24,
520 .fourcc = V4L2_PIX_FMT_BGR24,
521 .btformat = BT848_COLOR_FMT_RGB24,
522 .depth = 24,
523 .flags = FORMAT_FLAGS_PACKED,
524 },{
525 .name = "32 bpp RGB, le",
526 .palette = VIDEO_PALETTE_RGB32,
527 .fourcc = V4L2_PIX_FMT_BGR32,
528 .btformat = BT848_COLOR_FMT_RGB32,
529 .depth = 32,
530 .flags = FORMAT_FLAGS_PACKED,
531 },{
532 .name = "32 bpp RGB, be",
533 .palette = -1,
534 .fourcc = V4L2_PIX_FMT_RGB32,
535 .btformat = BT848_COLOR_FMT_RGB32,
536 .btswap = 0x0f, /* byte+word swap */
537 .depth = 32,
538 .flags = FORMAT_FLAGS_PACKED,
539 },{
540 .name = "4:2:2, packed, YUYV",
541 .palette = VIDEO_PALETTE_YUV422,
542 .fourcc = V4L2_PIX_FMT_YUYV,
543 .btformat = BT848_COLOR_FMT_YUY2,
544 .depth = 16,
545 .flags = FORMAT_FLAGS_PACKED,
546 },{
547 .name = "4:2:2, packed, YUYV",
548 .palette = VIDEO_PALETTE_YUYV,
549 .fourcc = V4L2_PIX_FMT_YUYV,
550 .btformat = BT848_COLOR_FMT_YUY2,
551 .depth = 16,
552 .flags = FORMAT_FLAGS_PACKED,
553 },{
554 .name = "4:2:2, packed, UYVY",
555 .palette = VIDEO_PALETTE_UYVY,
556 .fourcc = V4L2_PIX_FMT_UYVY,
557 .btformat = BT848_COLOR_FMT_YUY2,
558 .btswap = 0x03, /* byteswap */
559 .depth = 16,
560 .flags = FORMAT_FLAGS_PACKED,
561 },{
562 .name = "4:2:2, planar, Y-Cb-Cr",
563 .palette = VIDEO_PALETTE_YUV422P,
564 .fourcc = V4L2_PIX_FMT_YUV422P,
565 .btformat = BT848_COLOR_FMT_YCrCb422,
566 .depth = 16,
567 .flags = FORMAT_FLAGS_PLANAR,
568 .hshift = 1,
569 .vshift = 0,
570 },{
571 .name = "4:2:0, planar, Y-Cb-Cr",
572 .palette = VIDEO_PALETTE_YUV420P,
573 .fourcc = V4L2_PIX_FMT_YUV420,
574 .btformat = BT848_COLOR_FMT_YCrCb422,
575 .depth = 12,
576 .flags = FORMAT_FLAGS_PLANAR,
577 .hshift = 1,
578 .vshift = 1,
579 },{
580 .name = "4:2:0, planar, Y-Cr-Cb",
581 .palette = -1,
582 .fourcc = V4L2_PIX_FMT_YVU420,
583 .btformat = BT848_COLOR_FMT_YCrCb422,
584 .depth = 12,
585 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
586 .hshift = 1,
587 .vshift = 1,
588 },{
589 .name = "4:1:1, planar, Y-Cb-Cr",
590 .palette = VIDEO_PALETTE_YUV411P,
591 .fourcc = V4L2_PIX_FMT_YUV411P,
592 .btformat = BT848_COLOR_FMT_YCrCb411,
593 .depth = 12,
594 .flags = FORMAT_FLAGS_PLANAR,
595 .hshift = 2,
596 .vshift = 0,
597 },{
598 .name = "4:1:0, planar, Y-Cb-Cr",
599 .palette = VIDEO_PALETTE_YUV410P,
600 .fourcc = V4L2_PIX_FMT_YUV410,
601 .btformat = BT848_COLOR_FMT_YCrCb411,
602 .depth = 9,
603 .flags = FORMAT_FLAGS_PLANAR,
604 .hshift = 2,
605 .vshift = 2,
606 },{
607 .name = "4:1:0, planar, Y-Cr-Cb",
608 .palette = -1,
609 .fourcc = V4L2_PIX_FMT_YVU410,
610 .btformat = BT848_COLOR_FMT_YCrCb411,
611 .depth = 9,
612 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
613 .hshift = 2,
614 .vshift = 2,
615 },{
616 .name = "raw scanlines",
617 .palette = VIDEO_PALETTE_RAW,
618 .fourcc = -1,
619 .btformat = BT848_COLOR_FMT_RAW,
620 .depth = 8,
621 .flags = FORMAT_FLAGS_RAW,
622 }
623};
624static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
625
626/* ----------------------------------------------------------------------- */
627
628#define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
629#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
630#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
631#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
632#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
633#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
634#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
635#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700636#define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
637#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
638#define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
639#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641static const struct v4l2_queryctrl no_ctl = {
642 .name = "42",
643 .flags = V4L2_CTRL_FLAG_DISABLED,
644};
645static const struct v4l2_queryctrl bttv_ctls[] = {
646 /* --- video --- */
647 {
648 .id = V4L2_CID_BRIGHTNESS,
649 .name = "Brightness",
650 .minimum = 0,
651 .maximum = 65535,
652 .step = 256,
653 .default_value = 32768,
654 .type = V4L2_CTRL_TYPE_INTEGER,
655 },{
656 .id = V4L2_CID_CONTRAST,
657 .name = "Contrast",
658 .minimum = 0,
659 .maximum = 65535,
660 .step = 128,
661 .default_value = 32768,
662 .type = V4L2_CTRL_TYPE_INTEGER,
663 },{
664 .id = V4L2_CID_SATURATION,
665 .name = "Saturation",
666 .minimum = 0,
667 .maximum = 65535,
668 .step = 128,
669 .default_value = 32768,
670 .type = V4L2_CTRL_TYPE_INTEGER,
671 },{
672 .id = V4L2_CID_HUE,
673 .name = "Hue",
674 .minimum = 0,
675 .maximum = 65535,
676 .step = 256,
677 .default_value = 32768,
678 .type = V4L2_CTRL_TYPE_INTEGER,
679 },
680 /* --- audio --- */
681 {
682 .id = V4L2_CID_AUDIO_MUTE,
683 .name = "Mute",
684 .minimum = 0,
685 .maximum = 1,
686 .type = V4L2_CTRL_TYPE_BOOLEAN,
687 },{
688 .id = V4L2_CID_AUDIO_VOLUME,
689 .name = "Volume",
690 .minimum = 0,
691 .maximum = 65535,
692 .step = 65535/100,
693 .default_value = 65535,
694 .type = V4L2_CTRL_TYPE_INTEGER,
695 },{
696 .id = V4L2_CID_AUDIO_BALANCE,
697 .name = "Balance",
698 .minimum = 0,
699 .maximum = 65535,
700 .step = 65535/100,
701 .default_value = 32768,
702 .type = V4L2_CTRL_TYPE_INTEGER,
703 },{
704 .id = V4L2_CID_AUDIO_BASS,
705 .name = "Bass",
706 .minimum = 0,
707 .maximum = 65535,
708 .step = 65535/100,
709 .default_value = 32768,
710 .type = V4L2_CTRL_TYPE_INTEGER,
711 },{
712 .id = V4L2_CID_AUDIO_TREBLE,
713 .name = "Treble",
714 .minimum = 0,
715 .maximum = 65535,
716 .step = 65535/100,
717 .default_value = 32768,
718 .type = V4L2_CTRL_TYPE_INTEGER,
719 },
720 /* --- private --- */
721 {
722 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
723 .name = "chroma agc",
724 .minimum = 0,
725 .maximum = 1,
726 .type = V4L2_CTRL_TYPE_BOOLEAN,
727 },{
728 .id = V4L2_CID_PRIVATE_COMBFILTER,
729 .name = "combfilter",
730 .minimum = 0,
731 .maximum = 1,
732 .type = V4L2_CTRL_TYPE_BOOLEAN,
733 },{
734 .id = V4L2_CID_PRIVATE_AUTOMUTE,
735 .name = "automute",
736 .minimum = 0,
737 .maximum = 1,
738 .type = V4L2_CTRL_TYPE_BOOLEAN,
739 },{
740 .id = V4L2_CID_PRIVATE_LUMAFILTER,
741 .name = "luma decimation filter",
742 .minimum = 0,
743 .maximum = 1,
744 .type = V4L2_CTRL_TYPE_BOOLEAN,
745 },{
746 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
747 .name = "agc crush",
748 .minimum = 0,
749 .maximum = 1,
750 .type = V4L2_CTRL_TYPE_BOOLEAN,
751 },{
752 .id = V4L2_CID_PRIVATE_VCR_HACK,
753 .name = "vcr hack",
754 .minimum = 0,
755 .maximum = 1,
756 .type = V4L2_CTRL_TYPE_BOOLEAN,
757 },{
758 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
759 .name = "whitecrush upper",
760 .minimum = 0,
761 .maximum = 255,
762 .step = 1,
763 .default_value = 0xCF,
764 .type = V4L2_CTRL_TYPE_INTEGER,
765 },{
766 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
767 .name = "whitecrush lower",
768 .minimum = 0,
769 .maximum = 255,
770 .step = 1,
771 .default_value = 0x7F,
772 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700773 },{
774 .id = V4L2_CID_PRIVATE_UV_RATIO,
775 .name = "uv ratio",
776 .minimum = 0,
777 .maximum = 100,
778 .step = 1,
779 .default_value = 50,
780 .type = V4L2_CTRL_TYPE_INTEGER,
781 },{
782 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
783 .name = "full luma range",
784 .minimum = 0,
785 .maximum = 1,
786 .type = V4L2_CTRL_TYPE_BOOLEAN,
787 },{
788 .id = V4L2_CID_PRIVATE_CORING,
789 .name = "coring",
790 .minimum = 0,
791 .maximum = 3,
792 .step = 1,
793 .default_value = 0,
794 .type = V4L2_CTRL_TYPE_INTEGER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700797
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799};
800static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
801
802/* ----------------------------------------------------------------------- */
803/* resource management */
804
Michael Schimeke5bd0262007-01-18 16:17:39 -0300805/*
806 RESOURCE_ allocated by freed by
807
808 VIDEO_READ bttv_read 1) bttv_read 2)
809
810 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
811 VIDIOC_QBUF 1) bttv_release
812 VIDIOCMCAPTURE 1)
813
814 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
815 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
816 3) bttv_release
817
818 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
819 VIDIOC_QBUF 1) bttv_release
820 bttv_read, bttv_poll 1) 4)
821
822 1) The resource must be allocated when we enter buffer prepare functions
823 and remain allocated while buffers are in the DMA queue.
824 2) This is a single frame read.
825 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
826 RESOURCE_OVERLAY is allocated.
827 4) This is a continuous read, implies VIDIOC_STREAMON.
828
829 Note this driver permits video input and standard changes regardless if
830 resources are allocated.
831*/
832
833#define VBI_RESOURCES (RESOURCE_VBI)
834#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
835 RESOURCE_VIDEO_STREAM | \
836 RESOURCE_OVERLAY)
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838static
839int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
840{
Michael Schimeke5bd0262007-01-18 16:17:39 -0300841 int xbits; /* mutual exclusive resources */
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (fh->resources & bit)
844 /* have it already allocated */
845 return 1;
846
Michael Schimeke5bd0262007-01-18 16:17:39 -0300847 xbits = bit;
848 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
849 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* is it free? */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300852 mutex_lock(&btv->lock);
853 if (btv->resources & xbits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* no, someone else uses it */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300855 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300857
858 if ((bit & VIDEO_RESOURCES)
859 && 0 == (btv->resources & VIDEO_RESOURCES)) {
860 /* Do crop - use current, don't - use default parameters. */
861 __s32 top = btv->crop[!!fh->do_crop].rect.top;
862
863 if (btv->vbi_end > top)
864 goto fail;
865
866 /* We cannot capture the same line as video and VBI data.
867 Claim scan lines crop[].rect.top to bottom. */
868 btv->crop_start = top;
869 } else if (bit & VBI_RESOURCES) {
870 __s32 end = fh->vbi_fmt.end;
871
872 if (end > btv->crop_start)
873 goto fail;
874
875 /* Claim scan lines above fh->vbi_fmt.end. */
876 btv->vbi_end = end;
877 }
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* it's free, grab it */
880 fh->resources |= bit;
881 btv->resources |= bit;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300882 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return 1;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300884
885 fail:
886 mutex_unlock(&btv->lock);
887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890static
891int check_btres(struct bttv_fh *fh, int bit)
892{
893 return (fh->resources & bit);
894}
895
896static
897int locked_btres(struct bttv *btv, int bit)
898{
899 return (btv->resources & bit);
900}
901
Michael Schimeke5bd0262007-01-18 16:17:39 -0300902/* Call with btv->lock down. */
903static void
904disclaim_vbi_lines(struct bttv *btv)
905{
906 btv->vbi_end = 0;
907}
908
909/* Call with btv->lock down. */
910static void
911disclaim_video_lines(struct bttv *btv)
912{
913 const struct bttv_tvnorm *tvnorm;
914 u8 crop;
915
916 tvnorm = &bttv_tvnorms[btv->tvnorm];
917 btv->crop_start = tvnorm->cropcap.bounds.top
918 + tvnorm->cropcap.bounds.height;
919
920 /* VBI capturing ends at VDELAY, start of video capturing, no
921 matter how many lines the VBI RISC program expects. When video
922 capturing is off, it shall no longer "preempt" VBI capturing,
923 so we set VDELAY to maximum. */
924 crop = btread(BT848_E_CROP) | 0xc0;
925 btwrite(crop, BT848_E_CROP);
926 btwrite(0xfe, BT848_E_VDELAY_LO);
927 btwrite(crop, BT848_O_CROP);
928 btwrite(0xfe, BT848_O_VDELAY_LO);
929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931static
932void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
933{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if ((fh->resources & bits) != bits) {
935 /* trying to free ressources not allocated by us ... */
936 printk("bttv: BUG! (btres)\n");
937 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300938 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 fh->resources &= ~bits;
940 btv->resources &= ~bits;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300941
942 bits = btv->resources;
943
944 if (0 == (bits & VIDEO_RESOURCES))
945 disclaim_video_lines(btv);
946
947 if (0 == (bits & VBI_RESOURCES))
948 disclaim_vbi_lines(btv);
949
950 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953/* ----------------------------------------------------------------------- */
954/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
955
956/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
957 PLL_X = Reference pre-divider (0=1, 1=2)
958 PLL_C = Post divider (0=6, 1=4)
959 PLL_I = Integer input
960 PLL_F = Fractional input
961
962 F_input = 28.636363 MHz:
963 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
964*/
965
966static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
967{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800968 unsigned char fl, fh, fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800970 /* prevent overflows */
971 fin/=4;
972 fout/=4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800974 fout*=12;
975 fi=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800977 fout=(fout%fin)*256;
978 fh=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800980 fout=(fout%fin)*256;
981 fl=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800983 btwrite(fl, BT848_PLL_F_LO);
984 btwrite(fh, BT848_PLL_F_HI);
985 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
988static void set_pll(struct bttv *btv)
989{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800990 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800992 if (!btv->pll.pll_crystal)
993 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
996 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800997 return;
998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001000 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1001 /* no PLL needed */
1002 if (btv->pll.pll_current == 0)
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001003 return;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001004 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001005 btv->c.nr,btv->pll.pll_ifreq);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001006 btwrite(0x00,BT848_TGCTRL);
1007 btwrite(0x00,BT848_PLL_XCI);
1008 btv->pll.pll_current = 0;
1009 return;
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001012 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001013 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1015
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001016 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 /* Let other people run while the PLL stabilizes */
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001018 bttv_printk(".");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 msleep(10);
1020
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001021 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 btwrite(0,BT848_DSTATUS);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001023 } else {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001024 btwrite(0x08,BT848_TGCTRL);
1025 btv->pll.pll_current = btv->pll.pll_ofreq;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001026 bttv_printk(" ok\n");
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001027 return;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001028 }
1029 }
1030 btv->pll.pll_current = -1;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001031 bttv_printk("failed\n");
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001032 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035/* used to switch between the bt848's analog/digital video capture modes */
1036static void bt848A_set_timing(struct bttv *btv)
1037{
1038 int i, len;
1039 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1040 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1041
1042 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
1043 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1044 btv->c.nr,table_idx);
1045
1046 /* timing change...reset timing generator address */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001047 btwrite(0x00, BT848_TGCTRL);
1048 btwrite(0x02, BT848_TGCTRL);
1049 btwrite(0x00, BT848_TGCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 len=SRAM_Table[table_idx][0];
1052 for(i = 1; i <= len; i++)
1053 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1054 btv->pll.pll_ofreq = 27000000;
1055
1056 set_pll(btv);
1057 btwrite(0x11, BT848_TGCTRL);
1058 btwrite(0x41, BT848_DVSIF);
1059 } else {
1060 btv->pll.pll_ofreq = fsc;
1061 set_pll(btv);
1062 btwrite(0x0, BT848_DVSIF);
1063 }
1064}
1065
1066/* ----------------------------------------------------------------------- */
1067
1068static void bt848_bright(struct bttv *btv, int bright)
1069{
1070 int value;
1071
1072 // printk("bttv: set bright: %d\n",bright); // DEBUG
1073 btv->bright = bright;
1074
1075 /* We want -128 to 127 we get 0-65535 */
1076 value = (bright >> 8) - 128;
1077 btwrite(value & 0xff, BT848_BRIGHT);
1078}
1079
1080static void bt848_hue(struct bttv *btv, int hue)
1081{
1082 int value;
1083
1084 btv->hue = hue;
1085
1086 /* -128 to 127 */
1087 value = (hue >> 8) - 128;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001088 btwrite(value & 0xff, BT848_HUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091static void bt848_contrast(struct bttv *btv, int cont)
1092{
1093 int value,hibit;
1094
1095 btv->contrast = cont;
1096
1097 /* 0-511 */
1098 value = (cont >> 7);
1099 hibit = (value >> 6) & 4;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001100 btwrite(value & 0xff, BT848_CONTRAST_LO);
1101 btaor(hibit, ~4, BT848_E_CONTROL);
1102 btaor(hibit, ~4, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
1105static void bt848_sat(struct bttv *btv, int color)
1106{
1107 int val_u,val_v,hibits;
1108
1109 btv->saturation = color;
1110
1111 /* 0-511 for the color */
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001112 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1113 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001114 hibits = (val_u >> 7) & 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 hibits |= (val_v >> 8) & 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001116 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1117 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1118 btaor(hibits, ~3, BT848_E_CONTROL);
1119 btaor(hibits, ~3, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122/* ----------------------------------------------------------------------- */
1123
1124static int
1125video_mux(struct bttv *btv, unsigned int input)
1126{
1127 int mux,mask2;
1128
1129 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1130 return -EINVAL;
1131
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001132 /* needed by RemoteVideo MX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1134 if (mask2)
1135 gpio_inout(mask2,mask2);
1136
1137 if (input == btv->svhs) {
1138 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1139 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1140 } else {
1141 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1142 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1143 }
1144 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
1145 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1146 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1147 btv->c.nr,input,mux);
1148
1149 /* card specific hook */
1150 if(bttv_tvcards[btv->c.type].muxsel_hook)
1151 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1152 return 0;
1153}
1154
1155static char *audio_modes[] = {
1156 "audio: tuner", "audio: radio", "audio: extern",
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001157 "audio: intern", "audio: mute"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158};
1159
1160static int
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001161audio_mux(struct bttv *btv, int input, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001163 int gpio_val, signal;
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001164 struct v4l2_control ctrl;
1165 struct i2c_client *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1168 bttv_tvcards[btv->c.type].gpiomask);
1169 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1170
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001171 btv->mute = mute;
1172 btv->audio = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001174 /* automute */
1175 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1176
1177 if (mute)
1178 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1179 else
1180 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001181
1182 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (bttv_gpio)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001184 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1185 if (in_interrupt())
1186 return 0;
1187
1188 ctrl.id = V4L2_CID_AUDIO_MUTE;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001189 ctrl.value = btv->mute;
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001190 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, &ctrl);
1191 c = btv->i2c_msp34xx_client;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001192 if (c) {
1193 struct v4l2_routing route;
1194
1195 /* Note: the inputs tuner/radio/extern/intern are translated
1196 to msp routings. This assumes common behavior for all msp3400
1197 based TV cards. When this assumption fails, then the
1198 specific MSP routing must be added to the card table.
1199 For now this is sufficient. */
1200 switch (input) {
1201 case TVAUDIO_INPUT_RADIO:
Hans Verkuil07151722006-04-01 18:03:23 -03001202 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1203 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001204 break;
1205 case TVAUDIO_INPUT_EXTERN:
Hans Verkuil07151722006-04-01 18:03:23 -03001206 route.input = MSP_INPUT(MSP_IN_SCART1, 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_INTERN:
1210 /* Yes, this is the same input as for RADIO. I doubt
1211 if this is ever used. The only board with an INTERN
1212 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1213 that was tested. My guess is that the whole INTERN
1214 input does not work. */
Hans Verkuil07151722006-04-01 18:03:23 -03001215 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1216 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001217 break;
1218 case TVAUDIO_INPUT_TUNER:
1219 default:
Wade Berrier434b2522007-06-25 13:02:16 -03001220 /* This is the only card that uses TUNER2, and afaik,
1221 is the only difference between the VOODOOTV_FM
1222 and VOODOOTV_200 */
1223 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1224 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1225 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1226 else
1227 route.input = MSP_INPUT_DEFAULT;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001228 break;
1229 }
1230 route.output = MSP_OUTPUT_DEFAULT;
1231 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1232 }
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001233 c = btv->i2c_tvaudio_client;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001234 if (c) {
1235 struct v4l2_routing route;
1236
1237 route.input = input;
1238 route.output = 0;
1239 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return 0;
1242}
1243
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001244static inline int
1245audio_mute(struct bttv *btv, int mute)
1246{
1247 return audio_mux(btv, btv->audio, mute);
1248}
1249
1250static inline int
1251audio_input(struct bttv *btv, int input)
1252{
1253 return audio_mux(btv, input, btv->mute);
1254}
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256static void
1257i2c_vidiocschan(struct bttv *btv)
1258{
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001259 v4l2_std_id std = bttv_tvnorms[btv->tvnorm].v4l2_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001261 bttv_call_i2c_clients(btv, VIDIOC_S_STD, &std);
Wade Berrier434b2522007-06-25 13:02:16 -03001262 if (btv->c.type == BTTV_BOARD_VOODOOTV_FM || btv->c.type == BTTV_BOARD_VOODOOTV_200)
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001263 bttv_tda9880_setnorm(btv,btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Michael Schimeke5bd0262007-01-18 16:17:39 -03001266static void
1267bttv_crop_calc_limits(struct bttv_crop *c)
1268{
1269 /* Scale factor min. 1:1, max. 16:1. Min. image size
1270 48 x 32. Scaled width must be a multiple of 4. */
1271
1272 if (1) {
1273 /* For bug compatibility with VIDIOCGCAP and image
1274 size checks in earlier driver versions. */
1275 c->min_scaled_width = 48;
1276 c->min_scaled_height = 32;
1277 } else {
1278 c->min_scaled_width =
1279 (max(48, c->rect.width >> 4) + 3) & ~3;
1280 c->min_scaled_height =
1281 max(32, c->rect.height >> 4);
1282 }
1283
1284 c->max_scaled_width = c->rect.width & ~3;
1285 c->max_scaled_height = c->rect.height;
1286}
1287
1288static void
1289bttv_crop_reset(struct bttv_crop *c, int norm)
1290{
1291 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1292 bttv_crop_calc_limits(c);
1293}
1294
1295/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296static int
1297set_tvnorm(struct bttv *btv, unsigned int norm)
1298{
1299 const struct bttv_tvnorm *tvnorm;
1300
1301 if (norm < 0 || norm >= BTTV_TVNORMS)
1302 return -EINVAL;
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 tvnorm = &bttv_tvnorms[norm];
1305
Michael Schimeke5bd0262007-01-18 16:17:39 -03001306 if (btv->tvnorm < 0 ||
1307 btv->tvnorm >= BTTV_TVNORMS ||
1308 0 != memcmp(&bttv_tvnorms[btv->tvnorm].cropcap,
1309 &tvnorm->cropcap,
1310 sizeof (tvnorm->cropcap))) {
1311 bttv_crop_reset(&btv->crop[0], norm);
1312 btv->crop[1] = btv->crop[0]; /* current = default */
1313
1314 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1315 btv->crop_start = tvnorm->cropcap.bounds.top
1316 + tvnorm->cropcap.bounds.height;
1317 }
1318 }
1319
1320 btv->tvnorm = norm;
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 btwrite(tvnorm->adelay, BT848_ADELAY);
1323 btwrite(tvnorm->bdelay, BT848_BDELAY);
1324 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1325 BT848_IFORM);
1326 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1327 btwrite(1, BT848_VBI_PACK_DEL);
1328 bt848A_set_timing(btv);
1329
1330 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -08001331 case BTTV_BOARD_VOODOOTV_FM:
Wade Berrier434b2522007-06-25 13:02:16 -03001332 case BTTV_BOARD_VOODOOTV_200:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 bttv_tda9880_setnorm(btv,norm);
1334 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336 return 0;
1337}
1338
Michael Schimeke5bd0262007-01-18 16:17:39 -03001339/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340static void
Trent Piepho333408f2007-07-03 15:08:10 -03001341set_input(struct bttv *btv, unsigned int input, unsigned int norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 unsigned long flags;
1344
1345 btv->input = input;
1346 if (irq_iswitch) {
1347 spin_lock_irqsave(&btv->s_lock,flags);
1348 if (btv->curr.frame_irq) {
1349 /* active capture -> delayed input switch */
1350 btv->new_input = input;
1351 } else {
1352 video_mux(btv,input);
1353 }
1354 spin_unlock_irqrestore(&btv->s_lock,flags);
1355 } else {
1356 video_mux(btv,input);
1357 }
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001358 audio_input(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1359 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN));
Trent Piepho333408f2007-07-03 15:08:10 -03001360 set_tvnorm(btv, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 i2c_vidiocschan(btv);
1362}
1363
1364static void init_irqreg(struct bttv *btv)
1365{
1366 /* clear status */
1367 btwrite(0xfffffUL, BT848_INT_STAT);
1368
1369 if (bttv_tvcards[btv->c.type].no_video) {
1370 /* i2c only */
1371 btwrite(BT848_INT_I2CDONE,
1372 BT848_INT_MASK);
1373 } else {
1374 /* full video */
1375 btwrite((btv->triton1) |
1376 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1377 BT848_INT_SCERR |
1378 (fdsr ? BT848_INT_FDSR : 0) |
1379 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1380 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1381 BT848_INT_I2CDONE,
1382 BT848_INT_MASK);
1383 }
1384}
1385
1386static void init_bt848(struct bttv *btv)
1387{
1388 int val;
1389
1390 if (bttv_tvcards[btv->c.type].no_video) {
1391 /* very basic init only */
1392 init_irqreg(btv);
1393 return;
1394 }
1395
1396 btwrite(0x00, BT848_CAP_CTL);
1397 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1398 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1399
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001400 /* set planar and packed mode trigger points and */
1401 /* set rising edge of inverted GPINTR pin as irq trigger */
1402 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1403 BT848_GPIO_DMA_CTL_PLTP1_16|
1404 BT848_GPIO_DMA_CTL_PLTP23_16|
1405 BT848_GPIO_DMA_CTL_GPINTC|
1406 BT848_GPIO_DMA_CTL_GPINTI,
1407 BT848_GPIO_DMA_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001410 btwrite(val, BT848_E_SCLOOP);
1411 btwrite(val, BT848_O_SCLOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001413 btwrite(0x20, BT848_E_VSCALE_HI);
1414 btwrite(0x20, BT848_O_VSCALE_HI);
1415 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 BT848_ADC);
1417
1418 btwrite(whitecrush_upper, BT848_WC_UP);
1419 btwrite(whitecrush_lower, BT848_WC_DOWN);
1420
1421 if (btv->opt_lumafilter) {
1422 btwrite(0, BT848_E_CONTROL);
1423 btwrite(0, BT848_O_CONTROL);
1424 } else {
1425 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1426 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1427 }
1428
1429 bt848_bright(btv, btv->bright);
1430 bt848_hue(btv, btv->hue);
1431 bt848_contrast(btv, btv->contrast);
1432 bt848_sat(btv, btv->saturation);
1433
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001434 /* interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 init_irqreg(btv);
1436}
1437
1438static void bttv_reinit_bt848(struct bttv *btv)
1439{
1440 unsigned long flags;
1441
1442 if (bttv_verbose)
1443 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1444 spin_lock_irqsave(&btv->s_lock,flags);
1445 btv->errors=0;
1446 bttv_set_dma(btv,0);
1447 spin_unlock_irqrestore(&btv->s_lock,flags);
1448
1449 init_bt848(btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001450 btv->pll.pll_current = -1;
Trent Piepho333408f2007-07-03 15:08:10 -03001451 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
1454static int get_control(struct bttv *btv, struct v4l2_control *c)
1455{
1456 struct video_audio va;
1457 int i;
1458
1459 for (i = 0; i < BTTV_CTLS; i++)
1460 if (bttv_ctls[i].id == c->id)
1461 break;
1462 if (i == BTTV_CTLS)
1463 return -EINVAL;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001464 if (btv->audio_hook && i >= 4 && i <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 memset(&va,0,sizeof(va));
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001466 btv->audio_hook(btv,&va,0);
1467 switch (c->id) {
1468 case V4L2_CID_AUDIO_MUTE:
1469 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1470 break;
1471 case V4L2_CID_AUDIO_VOLUME:
1472 c->value = va.volume;
1473 break;
1474 case V4L2_CID_AUDIO_BALANCE:
1475 c->value = va.balance;
1476 break;
1477 case V4L2_CID_AUDIO_BASS:
1478 c->value = va.bass;
1479 break;
1480 case V4L2_CID_AUDIO_TREBLE:
1481 c->value = va.treble;
1482 break;
1483 }
1484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486 switch (c->id) {
1487 case V4L2_CID_BRIGHTNESS:
1488 c->value = btv->bright;
1489 break;
1490 case V4L2_CID_HUE:
1491 c->value = btv->hue;
1492 break;
1493 case V4L2_CID_CONTRAST:
1494 c->value = btv->contrast;
1495 break;
1496 case V4L2_CID_SATURATION:
1497 c->value = btv->saturation;
1498 break;
1499
1500 case V4L2_CID_AUDIO_MUTE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 case V4L2_CID_AUDIO_VOLUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 case V4L2_CID_AUDIO_BALANCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 case V4L2_CID_AUDIO_BASS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001505 bttv_call_i2c_clients(btv,VIDIOC_G_CTRL,c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 break;
1507
1508 case V4L2_CID_PRIVATE_CHROMA_AGC:
1509 c->value = btv->opt_chroma_agc;
1510 break;
1511 case V4L2_CID_PRIVATE_COMBFILTER:
1512 c->value = btv->opt_combfilter;
1513 break;
1514 case V4L2_CID_PRIVATE_LUMAFILTER:
1515 c->value = btv->opt_lumafilter;
1516 break;
1517 case V4L2_CID_PRIVATE_AUTOMUTE:
1518 c->value = btv->opt_automute;
1519 break;
1520 case V4L2_CID_PRIVATE_AGC_CRUSH:
1521 c->value = btv->opt_adc_crush;
1522 break;
1523 case V4L2_CID_PRIVATE_VCR_HACK:
1524 c->value = btv->opt_vcr_hack;
1525 break;
1526 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1527 c->value = btv->opt_whitecrush_upper;
1528 break;
1529 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1530 c->value = btv->opt_whitecrush_lower;
1531 break;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001532 case V4L2_CID_PRIVATE_UV_RATIO:
1533 c->value = btv->opt_uv_ratio;
1534 break;
1535 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1536 c->value = btv->opt_full_luma_range;
1537 break;
1538 case V4L2_CID_PRIVATE_CORING:
1539 c->value = btv->opt_coring;
1540 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 default:
1542 return -EINVAL;
1543 }
1544 return 0;
1545}
1546
1547static int set_control(struct bttv *btv, struct v4l2_control *c)
1548{
1549 struct video_audio va;
1550 int i,val;
1551
1552 for (i = 0; i < BTTV_CTLS; i++)
1553 if (bttv_ctls[i].id == c->id)
1554 break;
1555 if (i == BTTV_CTLS)
1556 return -EINVAL;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001557 if (btv->audio_hook && i >= 4 && i <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 memset(&va,0,sizeof(va));
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001559 btv->audio_hook(btv,&va,0);
1560 switch (c->id) {
1561 case V4L2_CID_AUDIO_MUTE:
1562 if (c->value) {
1563 va.flags |= VIDEO_AUDIO_MUTE;
1564 audio_mute(btv, 1);
1565 } else {
1566 va.flags &= ~VIDEO_AUDIO_MUTE;
1567 audio_mute(btv, 0);
1568 }
1569 break;
1570
1571 case V4L2_CID_AUDIO_VOLUME:
1572 va.volume = c->value;
1573 break;
1574 case V4L2_CID_AUDIO_BALANCE:
1575 va.balance = c->value;
1576 break;
1577 case V4L2_CID_AUDIO_BASS:
1578 va.bass = c->value;
1579 break;
1580 case V4L2_CID_AUDIO_TREBLE:
1581 va.treble = c->value;
1582 break;
1583 }
1584 btv->audio_hook(btv,&va,1);
1585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587 switch (c->id) {
1588 case V4L2_CID_BRIGHTNESS:
1589 bt848_bright(btv,c->value);
1590 break;
1591 case V4L2_CID_HUE:
1592 bt848_hue(btv,c->value);
1593 break;
1594 case V4L2_CID_CONTRAST:
1595 bt848_contrast(btv,c->value);
1596 break;
1597 case V4L2_CID_SATURATION:
1598 bt848_sat(btv,c->value);
1599 break;
1600 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001601 audio_mute(btv, c->value);
1602 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 case V4L2_CID_AUDIO_VOLUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 case V4L2_CID_AUDIO_BALANCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 case V4L2_CID_AUDIO_BASS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001607 bttv_call_i2c_clients(btv,VIDIOC_S_CTRL,c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 break;
1609
1610 case V4L2_CID_PRIVATE_CHROMA_AGC:
1611 btv->opt_chroma_agc = c->value;
1612 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1613 btwrite(val, BT848_E_SCLOOP);
1614 btwrite(val, BT848_O_SCLOOP);
1615 break;
1616 case V4L2_CID_PRIVATE_COMBFILTER:
1617 btv->opt_combfilter = c->value;
1618 break;
1619 case V4L2_CID_PRIVATE_LUMAFILTER:
1620 btv->opt_lumafilter = c->value;
1621 if (btv->opt_lumafilter) {
1622 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1623 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1624 } else {
1625 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1626 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1627 }
1628 break;
1629 case V4L2_CID_PRIVATE_AUTOMUTE:
1630 btv->opt_automute = c->value;
1631 break;
1632 case V4L2_CID_PRIVATE_AGC_CRUSH:
1633 btv->opt_adc_crush = c->value;
1634 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1635 BT848_ADC);
1636 break;
1637 case V4L2_CID_PRIVATE_VCR_HACK:
1638 btv->opt_vcr_hack = c->value;
1639 break;
1640 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1641 btv->opt_whitecrush_upper = c->value;
1642 btwrite(c->value, BT848_WC_UP);
1643 break;
1644 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1645 btv->opt_whitecrush_lower = c->value;
1646 btwrite(c->value, BT848_WC_DOWN);
1647 break;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001648 case V4L2_CID_PRIVATE_UV_RATIO:
1649 btv->opt_uv_ratio = c->value;
1650 bt848_sat(btv, btv->saturation);
1651 break;
1652 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1653 btv->opt_full_luma_range = c->value;
1654 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1655 break;
1656 case V4L2_CID_PRIVATE_CORING:
1657 btv->opt_coring = c->value;
1658 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1659 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 default:
1661 return -EINVAL;
1662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return 0;
1664}
1665
1666/* ----------------------------------------------------------------------- */
1667
1668void bttv_gpio_tracking(struct bttv *btv, char *comment)
1669{
1670 unsigned int outbits, data;
1671 outbits = btread(BT848_GPIO_OUT_EN);
1672 data = btread(BT848_GPIO_DATA);
1673 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1674 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1675}
1676
1677static void bttv_field_count(struct bttv *btv)
1678{
1679 int need_count = 0;
1680
1681 if (btv->users)
1682 need_count++;
1683
1684 if (need_count) {
1685 /* start field counter */
1686 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1687 } else {
1688 /* stop field counter */
1689 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1690 btv->field_count = 0;
1691 }
1692}
1693
1694static const struct bttv_format*
1695format_by_palette(int palette)
1696{
1697 unsigned int i;
1698
1699 for (i = 0; i < BTTV_FORMATS; i++) {
1700 if (-1 == bttv_formats[i].palette)
1701 continue;
1702 if (bttv_formats[i].palette == palette)
1703 return bttv_formats+i;
1704 }
1705 return NULL;
1706}
1707
1708static const struct bttv_format*
1709format_by_fourcc(int fourcc)
1710{
1711 unsigned int i;
1712
1713 for (i = 0; i < BTTV_FORMATS; i++) {
1714 if (-1 == bttv_formats[i].fourcc)
1715 continue;
1716 if (bttv_formats[i].fourcc == fourcc)
1717 return bttv_formats+i;
1718 }
1719 return NULL;
1720}
1721
1722/* ----------------------------------------------------------------------- */
1723/* misc helpers */
1724
1725static int
1726bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1727 struct bttv_buffer *new)
1728{
1729 struct bttv_buffer *old;
1730 unsigned long flags;
1731 int retval = 0;
1732
1733 dprintk("switch_overlay: enter [new=%p]\n",new);
1734 if (new)
1735 new->vb.state = STATE_DONE;
1736 spin_lock_irqsave(&btv->s_lock,flags);
1737 old = btv->screen;
1738 btv->screen = new;
1739 btv->loop_irq |= 1;
1740 bttv_set_dma(btv, 0x03);
1741 spin_unlock_irqrestore(&btv->s_lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (NULL != old) {
1743 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001744 bttv_dma_free(&fh->cap,btv, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 kfree(old);
1746 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03001747 if (NULL == new)
1748 free_btres(btv,fh,RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 dprintk("switch_overlay: done\n");
1750 return retval;
1751}
1752
1753/* ----------------------------------------------------------------------- */
1754/* video4linux (1) interface */
1755
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001756static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1757 struct bttv_buffer *buf,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001758 const struct bttv_format *fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 unsigned int width, unsigned int height,
1760 enum v4l2_field field)
1761{
Michael Schimeke5bd0262007-01-18 16:17:39 -03001762 struct bttv_fh *fh = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 int redo_dma_risc = 0;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001764 struct bttv_crop c;
1765 int norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 int rc;
1767
1768 /* check settings */
1769 if (NULL == fmt)
1770 return -EINVAL;
1771 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1772 width = RAW_BPL;
1773 height = RAW_LINES*2;
1774 if (width*height > buf->vb.bsize)
1775 return -EINVAL;
1776 buf->vb.size = buf->vb.bsize;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001777
1778 /* Make sure tvnorm and vbi_end remain consistent
1779 until we're done. */
1780 mutex_lock(&btv->lock);
1781
1782 norm = btv->tvnorm;
1783
1784 /* In this mode capturing always starts at defrect.top
1785 (default VDELAY), ignoring cropping parameters. */
1786 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1787 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001789 }
1790
1791 mutex_unlock(&btv->lock);
1792
1793 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1794 } else {
1795 mutex_lock(&btv->lock);
1796
1797 norm = btv->tvnorm;
1798 c = btv->crop[!!fh->do_crop];
1799
1800 mutex_unlock(&btv->lock);
1801
1802 if (width < c.min_scaled_width ||
1803 width > c.max_scaled_width ||
1804 height < c.min_scaled_height)
1805 return -EINVAL;
1806
1807 switch (field) {
1808 case V4L2_FIELD_TOP:
1809 case V4L2_FIELD_BOTTOM:
1810 case V4L2_FIELD_ALTERNATE:
1811 /* btv->crop counts frame lines. Max. scale
1812 factor is 16:1 for frames, 8:1 for fields. */
1813 if (height * 2 > c.max_scaled_height)
1814 return -EINVAL;
1815 break;
1816
1817 default:
1818 if (height > c.max_scaled_height)
1819 return -EINVAL;
1820 break;
1821 }
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 buf->vb.size = (width * height * fmt->depth) >> 3;
1824 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1825 return -EINVAL;
1826 }
1827
1828 /* alloc + fill struct bttv_buffer (if changed) */
1829 if (buf->vb.width != width || buf->vb.height != height ||
1830 buf->vb.field != field ||
Michael Schimeke5bd0262007-01-18 16:17:39 -03001831 buf->tvnorm != norm || buf->fmt != fmt ||
1832 buf->crop.top != c.rect.top ||
1833 buf->crop.left != c.rect.left ||
1834 buf->crop.width != c.rect.width ||
1835 buf->crop.height != c.rect.height) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 buf->vb.width = width;
1837 buf->vb.height = height;
1838 buf->vb.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001839 buf->tvnorm = norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 buf->fmt = fmt;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001841 buf->crop = c.rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 redo_dma_risc = 1;
1843 }
1844
1845 /* alloc risc memory */
1846 if (STATE_NEEDS_INIT == buf->vb.state) {
1847 redo_dma_risc = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001848 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto fail;
1850 }
1851
1852 if (redo_dma_risc)
1853 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1854 goto fail;
1855
1856 buf->vb.state = STATE_PREPARED;
1857 return 0;
1858
1859 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001860 bttv_dma_free(q,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return rc;
1862}
1863
1864static int
1865buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1866{
1867 struct bttv_fh *fh = q->priv_data;
1868
1869 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1870 if (0 == *count)
1871 *count = gbuffers;
1872 while (*size * *count > gbuffers * gbufsize)
1873 (*count)--;
1874 return 0;
1875}
1876
1877static int
1878buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1879 enum v4l2_field field)
1880{
1881 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1882 struct bttv_fh *fh = q->priv_data;
1883
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001884 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 fh->width, fh->height, field);
1886}
1887
1888static void
1889buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1890{
1891 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1892 struct bttv_fh *fh = q->priv_data;
1893 struct bttv *btv = fh->btv;
1894
1895 buf->vb.state = STATE_QUEUED;
1896 list_add_tail(&buf->vb.queue,&btv->capture);
1897 if (!btv->curr.frame_irq) {
1898 btv->loop_irq |= 1;
1899 bttv_set_dma(btv, 0x03);
1900 }
1901}
1902
1903static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1904{
1905 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1906 struct bttv_fh *fh = q->priv_data;
1907
Michael Schimekfeaba7a2007-01-26 08:30:05 -03001908 bttv_dma_free(q,fh->btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
1911static struct videobuf_queue_ops bttv_video_qops = {
1912 .buf_setup = buffer_setup,
1913 .buf_prepare = buffer_prepare,
1914 .buf_queue = buffer_queue,
1915 .buf_release = buffer_release,
1916};
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1919{
1920 switch (cmd) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001921 case BTTV_VERSION:
1922 return BTTV_VERSION_CODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 /* *** v4l1 *** ************************************************ */
1925 case VIDIOCGFREQ:
1926 {
1927 unsigned long *freq = arg;
1928 *freq = btv->freq;
1929 return 0;
1930 }
1931 case VIDIOCSFREQ:
1932 {
Hans Verkuil3bbe5a82006-04-01 15:27:52 -03001933 struct v4l2_frequency freq;
1934
1935 memset(&freq, 0, sizeof(freq));
1936 freq.frequency = *(unsigned long *)arg;
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001937 mutex_lock(&btv->lock);
Hans Verkuil3bbe5a82006-04-01 15:27:52 -03001938 freq.type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1939 btv->freq = *(unsigned long *)arg;
1940 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,&freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (btv->has_matchbox && btv->radio_user)
Hans Verkuil3bbe5a82006-04-01 15:27:52 -03001942 tea5757_set_freq(btv,*(unsigned long *)arg);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001943 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return 0;
1945 }
1946
1947 case VIDIOCGTUNER:
1948 {
1949 struct video_tuner *v = arg;
1950
1951 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1952 return -EINVAL;
1953 if (v->tuner) /* Only tuner 0 */
1954 return -EINVAL;
1955 strcpy(v->name, "Television");
1956 v->rangelow = 0;
1957 v->rangehigh = 0x7FFFFFFF;
1958 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1959 v->mode = btv->tvnorm;
1960 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1961 bttv_call_i2c_clients(btv,cmd,v);
1962 return 0;
1963 }
1964 case VIDIOCSTUNER:
1965 {
1966 struct video_tuner *v = arg;
1967
1968 if (v->tuner) /* Only tuner 0 */
1969 return -EINVAL;
1970 if (v->mode >= BTTV_TVNORMS)
1971 return -EINVAL;
1972
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001973 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 set_tvnorm(btv,v->mode);
1975 bttv_call_i2c_clients(btv,cmd,v);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001976 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return 0;
1978 }
1979
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001980 case VIDIOCGCHAN:
1981 {
1982 struct video_channel *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 unsigned int channel = v->channel;
1984
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001985 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1986 return -EINVAL;
1987 v->tuners=0;
1988 v->flags = VIDEO_VC_AUDIO;
1989 v->type = VIDEO_TYPE_CAMERA;
1990 v->norm = btv->tvnorm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (channel == bttv_tvcards[btv->c.type].tuner) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001992 strcpy(v->name,"Television");
1993 v->flags|=VIDEO_VC_TUNER;
1994 v->type=VIDEO_TYPE_TV;
1995 v->tuners=1;
1996 } else if (channel == btv->svhs) {
1997 strcpy(v->name,"S-Video");
1998 } else {
1999 sprintf(v->name,"Composite%d",channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001 return 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002002 }
2003 case VIDIOCSCHAN:
2004 {
2005 struct video_channel *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 unsigned int channel = v->channel;
2007
2008 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
2009 return -EINVAL;
2010 if (v->norm >= BTTV_TVNORMS)
2011 return -EINVAL;
2012
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002013 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (channel == btv->input &&
2015 v->norm == btv->tvnorm) {
2016 /* nothing to do */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002017 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return 0;
2019 }
2020
Trent Piepho333408f2007-07-03 15:08:10 -03002021 set_input(btv, v->channel, v->norm);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002022 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return 0;
2024 }
2025
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002026 case VIDIOCGAUDIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 {
2028 struct video_audio *v = arg;
2029
2030 memset(v,0,sizeof(*v));
2031 strcpy(v->name,"Television");
2032 v->flags |= VIDEO_AUDIO_MUTABLE;
2033 v->mode = VIDEO_SOUND_MONO;
2034
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002035 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 bttv_call_i2c_clients(btv,cmd,v);
2037
2038 /* card specific hooks */
2039 if (btv->audio_hook)
2040 btv->audio_hook(btv,v,0);
2041
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002042 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return 0;
2044 }
2045 case VIDIOCSAUDIO:
2046 {
2047 struct video_audio *v = arg;
2048 unsigned int audio = v->audio;
2049
2050 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
2051 return -EINVAL;
2052
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002053 mutex_lock(&btv->lock);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03002054 audio_mute(btv, (v->flags&VIDEO_AUDIO_MUTE) ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 bttv_call_i2c_clients(btv,cmd,v);
2056
2057 /* card specific hooks */
2058 if (btv->audio_hook)
2059 btv->audio_hook(btv,v,1);
2060
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002061 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return 0;
2063 }
2064
2065 /* *** v4l2 *** ************************************************ */
2066 case VIDIOC_ENUMSTD:
2067 {
2068 struct v4l2_standard *e = arg;
2069 unsigned int index = e->index;
2070
2071 if (index >= BTTV_TVNORMS)
2072 return -EINVAL;
2073 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
2074 bttv_tvnorms[e->index].name);
2075 e->index = index;
2076 return 0;
2077 }
2078 case VIDIOC_G_STD:
2079 {
2080 v4l2_std_id *id = arg;
2081 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
2082 return 0;
2083 }
2084 case VIDIOC_S_STD:
2085 {
2086 v4l2_std_id *id = arg;
2087 unsigned int i;
2088
2089 for (i = 0; i < BTTV_TVNORMS; i++)
2090 if (*id & bttv_tvnorms[i].v4l2_id)
2091 break;
2092 if (i == BTTV_TVNORMS)
2093 return -EINVAL;
2094
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002095 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 set_tvnorm(btv,i);
2097 i2c_vidiocschan(btv);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002098 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return 0;
2100 }
2101 case VIDIOC_QUERYSTD:
2102 {
2103 v4l2_std_id *id = arg;
2104
2105 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
2106 *id = V4L2_STD_625_50;
2107 else
2108 *id = V4L2_STD_525_60;
2109 return 0;
2110 }
2111
2112 case VIDIOC_ENUMINPUT:
2113 {
2114 struct v4l2_input *i = arg;
2115 unsigned int n;
2116
2117 n = i->index;
2118 if (n >= bttv_tvcards[btv->c.type].video_inputs)
2119 return -EINVAL;
2120 memset(i,0,sizeof(*i));
2121 i->index = n;
2122 i->type = V4L2_INPUT_TYPE_CAMERA;
Mauro Carvalho Chehab5d9d1712006-11-14 12:40:07 -03002123 i->audioset = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (i->index == bttv_tvcards[btv->c.type].tuner) {
2125 sprintf(i->name, "Television");
2126 i->type = V4L2_INPUT_TYPE_TUNER;
2127 i->tuner = 0;
2128 } else if (i->index == btv->svhs) {
2129 sprintf(i->name, "S-Video");
2130 } else {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002131 sprintf(i->name,"Composite%d",i->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133 if (i->index == btv->input) {
2134 __u32 dstatus = btread(BT848_DSTATUS);
2135 if (0 == (dstatus & BT848_DSTATUS_PRES))
2136 i->status |= V4L2_IN_ST_NO_SIGNAL;
2137 if (0 == (dstatus & BT848_DSTATUS_HLOC))
2138 i->status |= V4L2_IN_ST_NO_H_LOCK;
2139 }
2140 for (n = 0; n < BTTV_TVNORMS; n++)
2141 i->std |= bttv_tvnorms[n].v4l2_id;
2142 return 0;
2143 }
2144 case VIDIOC_G_INPUT:
2145 {
2146 int *i = arg;
2147 *i = btv->input;
2148 return 0;
2149 }
2150 case VIDIOC_S_INPUT:
2151 {
2152 unsigned int *i = arg;
2153
2154 if (*i > bttv_tvcards[btv->c.type].video_inputs)
2155 return -EINVAL;
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002156 mutex_lock(&btv->lock);
Trent Piepho333408f2007-07-03 15:08:10 -03002157 set_input(btv, *i, btv->tvnorm);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002158 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return 0;
2160 }
2161
2162 case VIDIOC_G_TUNER:
2163 {
2164 struct v4l2_tuner *t = arg;
2165
2166 if (UNSET == bttv_tvcards[btv->c.type].tuner)
2167 return -EINVAL;
2168 if (0 != t->index)
2169 return -EINVAL;
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002170 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 memset(t,0,sizeof(*t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 t->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002173 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
2174 strcpy(t->name, "Television");
2175 t->capability = V4L2_TUNER_CAP_NORM;
2176 t->type = V4L2_TUNER_ANALOG_TV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2178 t->signal = 0xffff;
Michael H. Schimekbbf78712005-12-01 00:51:40 -08002179
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002180 if (btv->audio_hook) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /* Hmmm ... */
2182 struct video_audio va;
2183 memset(&va, 0, sizeof(struct video_audio));
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002184 btv->audio_hook(btv,&va,0);
2185 t->audmode = V4L2_TUNER_MODE_MONO;
2186 t->rxsubchans = V4L2_TUNER_SUB_MONO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if(va.mode & VIDEO_SOUND_STEREO) {
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002188 t->audmode = V4L2_TUNER_MODE_STEREO;
2189 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002191 if(va.mode & VIDEO_SOUND_LANG2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 t->audmode = V4L2_TUNER_MODE_LANG1;
2193 t->rxsubchans = V4L2_TUNER_SUB_LANG1
2194 | V4L2_TUNER_SUB_LANG2;
2195 }
2196 }
2197 /* FIXME: fill capability+audmode */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002198 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return 0;
2200 }
2201 case VIDIOC_S_TUNER:
2202 {
2203 struct v4l2_tuner *t = arg;
2204
2205 if (UNSET == bttv_tvcards[btv->c.type].tuner)
2206 return -EINVAL;
2207 if (0 != t->index)
2208 return -EINVAL;
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002209 mutex_lock(&btv->lock);
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002210 bttv_call_i2c_clients(btv, VIDIOC_S_TUNER, t);
2211 if (btv->audio_hook) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 struct video_audio va;
2213 memset(&va, 0, sizeof(struct video_audio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (t->audmode == V4L2_TUNER_MODE_MONO)
2215 va.mode = VIDEO_SOUND_MONO;
Hans Verkuil301e22d2006-03-18 17:15:00 -03002216 else if (t->audmode == V4L2_TUNER_MODE_STEREO ||
2217 t->audmode == V4L2_TUNER_MODE_LANG1_LANG2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 va.mode = VIDEO_SOUND_STEREO;
2219 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
2220 va.mode = VIDEO_SOUND_LANG1;
2221 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
2222 va.mode = VIDEO_SOUND_LANG2;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002223 btv->audio_hook(btv,&va,1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002225 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return 0;
2227 }
2228
2229 case VIDIOC_G_FREQUENCY:
2230 {
2231 struct v4l2_frequency *f = arg;
2232
2233 memset(f,0,sizeof(*f));
2234 f->type = V4L2_TUNER_ANALOG_TV;
2235 f->frequency = btv->freq;
2236 return 0;
2237 }
2238 case VIDIOC_S_FREQUENCY:
2239 {
2240 struct v4l2_frequency *f = arg;
2241
2242 if (unlikely(f->tuner != 0))
2243 return -EINVAL;
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -07002244 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return -EINVAL;
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002246 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 btv->freq = f->frequency;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002248 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (btv->has_matchbox && btv->radio_user)
2250 tea5757_set_freq(btv,btv->freq);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002251 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 return 0;
2253 }
Hans Verkuil299392b2005-11-08 21:37:42 -08002254 case VIDIOC_LOG_STATUS:
2255 {
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002256 printk(KERN_INFO "bttv%d: ================= START STATUS CARD #%d =================\n", btv->c.nr, btv->c.nr);
Luiz Capitulino97cb4452005-12-01 00:51:24 -08002257 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002258 printk(KERN_INFO "bttv%d: ================== END STATUS CARD #%d ==================\n", btv->c.nr, btv->c.nr);
Hans Verkuil299392b2005-11-08 21:37:42 -08002259 return 0;
2260 }
Trent Piepho55c0d102007-06-29 00:17:36 -03002261#ifdef CONFIG_VIDEO_ADV_DEBUG
2262 case VIDIOC_DBG_G_REGISTER:
2263 case VIDIOC_DBG_S_REGISTER:
2264 {
2265 struct v4l2_register *reg = arg;
2266 if (!capable(CAP_SYS_ADMIN))
2267 return -EPERM;
2268 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
2269 return -EINVAL;
2270 /* bt848 has a 12-bit register space */
2271 reg->reg &= 0xfff;
2272 if (cmd == VIDIOC_DBG_G_REGISTER)
2273 reg->val = btread(reg->reg);
2274 else
2275 btwrite(reg->val, reg->reg);
2276 return 0;
2277 }
2278#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 default:
2281 return -ENOIOCTLCMD;
2282
2283 }
2284 return 0;
2285}
2286
Michael Schimeke5bd0262007-01-18 16:17:39 -03002287/* Given cropping boundaries b and the scaled width and height of a
2288 single field or frame, which must not exceed hardware limits, this
2289 function adjusts the cropping parameters c. */
2290static void
2291bttv_crop_adjust (struct bttv_crop * c,
2292 const struct v4l2_rect * b,
2293 __s32 width,
2294 __s32 height,
2295 enum v4l2_field field)
2296{
2297 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2298 __s32 max_left;
2299 __s32 max_top;
2300
2301 if (width < c->min_scaled_width) {
2302 /* Max. hor. scale factor 16:1. */
2303 c->rect.width = width * 16;
2304 } else if (width > c->max_scaled_width) {
2305 /* Min. hor. scale factor 1:1. */
2306 c->rect.width = width;
2307
2308 max_left = b->left + b->width - width;
2309 max_left = min(max_left, (__s32) MAX_HDELAY);
2310 if (c->rect.left > max_left)
2311 c->rect.left = max_left;
2312 }
2313
2314 if (height < c->min_scaled_height) {
2315 /* Max. vert. scale factor 16:1, single fields 8:1. */
2316 c->rect.height = height * 16;
2317 } else if (frame_height > c->max_scaled_height) {
2318 /* Min. vert. scale factor 1:1.
2319 Top and height count field lines times two. */
2320 c->rect.height = (frame_height + 1) & ~1;
2321
2322 max_top = b->top + b->height - c->rect.height;
2323 if (c->rect.top > max_top)
2324 c->rect.top = max_top;
2325 }
2326
2327 bttv_crop_calc_limits(c);
2328}
2329
2330/* Returns an error if scaling to a frame or single field with the given
2331 width and height is not possible with the current cropping parameters
2332 and width aligned according to width_mask. If adjust_size is TRUE the
2333 function may adjust the width and/or height instead, rounding width
2334 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2335 also adjust the current cropping parameters to get closer to the
2336 desired image size. */
2337static int
2338limit_scaled_size (struct bttv_fh * fh,
2339 __s32 * width,
2340 __s32 * height,
2341 enum v4l2_field field,
2342 unsigned int width_mask,
2343 unsigned int width_bias,
2344 int adjust_size,
2345 int adjust_crop)
2346{
2347 struct bttv *btv = fh->btv;
2348 const struct v4l2_rect *b;
2349 struct bttv_crop *c;
2350 __s32 min_width;
2351 __s32 min_height;
2352 __s32 max_width;
2353 __s32 max_height;
2354 int rc;
2355
2356 BUG_ON((int) width_mask >= 0 ||
2357 width_bias >= (unsigned int) -width_mask);
2358
2359 /* Make sure tvnorm, vbi_end and the current cropping parameters
2360 remain consistent until we're done. */
2361 mutex_lock(&btv->lock);
2362
2363 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2364
2365 /* Do crop - use current, don't - use default parameters. */
2366 c = &btv->crop[!!fh->do_crop];
2367
2368 if (fh->do_crop
2369 && adjust_size
2370 && adjust_crop
2371 && !locked_btres(btv, VIDEO_RESOURCES)) {
2372 min_width = 48;
2373 min_height = 32;
2374
2375 /* We cannot scale up. When the scaled image is larger
2376 than crop.rect we adjust the crop.rect as required
2377 by the V4L2 spec, hence cropcap.bounds are our limit. */
2378 max_width = min(b->width, (__s32) MAX_HACTIVE);
2379 max_height = b->height;
2380
2381 /* We cannot capture the same line as video and VBI data.
2382 Note btv->vbi_end is really a minimum, see
2383 bttv_vbi_try_fmt(). */
2384 if (btv->vbi_end > b->top) {
2385 max_height -= btv->vbi_end - b->top;
2386 rc = -EBUSY;
2387 if (min_height > max_height)
2388 goto fail;
2389 }
2390 } else {
2391 rc = -EBUSY;
2392 if (btv->vbi_end > c->rect.top)
2393 goto fail;
2394
2395 min_width = c->min_scaled_width;
2396 min_height = c->min_scaled_height;
2397 max_width = c->max_scaled_width;
2398 max_height = c->max_scaled_height;
2399
2400 adjust_crop = 0;
2401 }
2402
2403 min_width = (min_width - width_mask - 1) & width_mask;
2404 max_width = max_width & width_mask;
2405
2406 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2407 min_height = min_height;
2408 /* Min. scale factor is 1:1. */
2409 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2410
2411 if (adjust_size) {
2412 *width = clamp(*width, min_width, max_width);
2413 *height = clamp(*height, min_height, max_height);
2414
2415 /* Round after clamping to avoid overflow. */
2416 *width = (*width + width_bias) & width_mask;
2417
2418 if (adjust_crop) {
2419 bttv_crop_adjust(c, b, *width, *height, field);
2420
2421 if (btv->vbi_end > c->rect.top) {
2422 /* Move the crop window out of the way. */
2423 c->rect.top = btv->vbi_end;
2424 }
2425 }
2426 } else {
2427 rc = -EINVAL;
2428 if (*width < min_width ||
2429 *height < min_height ||
2430 *width > max_width ||
2431 *height > max_height ||
2432 0 != (*width & ~width_mask))
2433 goto fail;
2434 }
2435
2436 rc = 0; /* success */
2437
2438 fail:
2439 mutex_unlock(&btv->lock);
2440
2441 return rc;
2442}
2443
2444/* Returns an error if the given overlay window dimensions are not
2445 possible with the current cropping parameters. If adjust_size is
2446 TRUE the function may adjust the window width and/or height
2447 instead, however it always rounds the horizontal position and
2448 width as btcx_align() does. If adjust_crop is TRUE the function
2449 may also adjust the current cropping parameters to get closer
2450 to the desired window size. */
2451static int
2452verify_window (struct bttv_fh * fh,
2453 struct v4l2_window * win,
2454 int adjust_size,
2455 int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
2457 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002458 unsigned int width_mask;
2459 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 if (win->w.width < 48 || win->w.height < 32)
2462 return -EINVAL;
2463 if (win->clipcount > 2048)
2464 return -EINVAL;
2465
2466 field = win->field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 if (V4L2_FIELD_ANY == field) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002469 __s32 height2;
2470
2471 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2472 field = (win->w.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 ? V4L2_FIELD_INTERLACED
2474 : V4L2_FIELD_TOP;
2475 }
2476 switch (field) {
2477 case V4L2_FIELD_TOP:
2478 case V4L2_FIELD_BOTTOM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 case V4L2_FIELD_INTERLACED:
2480 break;
2481 default:
2482 return -EINVAL;
2483 }
2484
Michael Schimeke5bd0262007-01-18 16:17:39 -03002485 /* 4-byte alignment. */
2486 if (NULL == fh->ovfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002488 width_mask = ~0;
2489 switch (fh->ovfmt->depth) {
2490 case 8:
2491 case 24:
2492 width_mask = ~3;
2493 break;
2494 case 16:
2495 width_mask = ~1;
2496 break;
2497 case 32:
2498 break;
2499 default:
2500 BUG();
2501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Michael Schimeke5bd0262007-01-18 16:17:39 -03002503 win->w.width -= win->w.left & ~width_mask;
2504 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2505
2506 rc = limit_scaled_size(fh, &win->w.width, &win->w.height,
2507 field, width_mask,
2508 /* width_bias: round down */ 0,
2509 adjust_size, adjust_crop);
2510 if (0 != rc)
2511 return rc;
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 win->field = field;
2514 return 0;
2515}
2516
2517static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2518 struct v4l2_window *win, int fixup)
2519{
2520 struct v4l2_clip *clips = NULL;
2521 int n,size,retval = 0;
2522
2523 if (NULL == fh->ovfmt)
2524 return -EINVAL;
2525 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2526 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002527 retval = verify_window(fh, win,
2528 /* adjust_size */ fixup,
2529 /* adjust_crop */ fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (0 != retval)
2531 return retval;
2532
2533 /* copy clips -- luckily v4l1 + v4l2 are binary
2534 compatible here ...*/
2535 n = win->clipcount;
2536 size = sizeof(*clips)*(n+4);
2537 clips = kmalloc(size,GFP_KERNEL);
2538 if (NULL == clips)
2539 return -ENOMEM;
2540 if (n > 0) {
2541 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2542 kfree(clips);
2543 return -EFAULT;
2544 }
2545 }
2546 /* clip against screen */
2547 if (NULL != btv->fbuf.base)
2548 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2549 &win->w, clips, n);
2550 btcx_sort_clips(clips,n);
2551
2552 /* 4-byte alignments */
2553 switch (fh->ovfmt->depth) {
2554 case 8:
2555 case 24:
2556 btcx_align(&win->w, clips, n, 3);
2557 break;
2558 case 16:
2559 btcx_align(&win->w, clips, n, 1);
2560 break;
2561 case 32:
2562 /* no alignment fixups needed */
2563 break;
2564 default:
2565 BUG();
2566 }
2567
Ingo Molnar3593cab2006-02-07 06:49:14 -02002568 mutex_lock(&fh->cap.lock);
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08002569 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 fh->ov.clips = clips;
2571 fh->ov.nclips = n;
2572
2573 fh->ov.w = win->w;
2574 fh->ov.field = win->field;
2575 fh->ov.setup_ok = 1;
2576 btv->init.ov.w.width = win->w.width;
2577 btv->init.ov.w.height = win->w.height;
2578 btv->init.ov.field = win->field;
2579
2580 /* update overlay if needed */
2581 retval = 0;
2582 if (check_btres(fh, RESOURCE_OVERLAY)) {
2583 struct bttv_buffer *new;
2584
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03002585 new = videobuf_pci_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03002586 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2588 retval = bttv_switch_overlay(btv,fh,new);
2589 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002590 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return retval;
2592}
2593
2594/* ----------------------------------------------------------------------- */
2595
2596static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2597{
2598 struct videobuf_queue* q = NULL;
2599
2600 switch (fh->type) {
2601 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2602 q = &fh->cap;
2603 break;
2604 case V4L2_BUF_TYPE_VBI_CAPTURE:
2605 q = &fh->vbi;
2606 break;
2607 default:
2608 BUG();
2609 }
2610 return q;
2611}
2612
2613static int bttv_resource(struct bttv_fh *fh)
2614{
2615 int res = 0;
2616
2617 switch (fh->type) {
2618 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002619 res = RESOURCE_VIDEO_STREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 break;
2621 case V4L2_BUF_TYPE_VBI_CAPTURE:
2622 res = RESOURCE_VBI;
2623 break;
2624 default:
2625 BUG();
2626 }
2627 return res;
2628}
2629
2630static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2631{
2632 struct videobuf_queue *q = bttv_queue(fh);
2633 int res = bttv_resource(fh);
2634
2635 if (check_btres(fh,res))
2636 return -EBUSY;
2637 if (videobuf_queue_is_busy(q))
2638 return -EBUSY;
2639 fh->type = type;
2640 return 0;
2641}
2642
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002643static void
2644pix_format_set_size (struct v4l2_pix_format * f,
2645 const struct bttv_format * fmt,
2646 unsigned int width,
2647 unsigned int height)
2648{
2649 f->width = width;
2650 f->height = height;
2651
2652 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2653 f->bytesperline = width; /* Y plane */
2654 f->sizeimage = (width * height * fmt->depth) >> 3;
2655 } else {
2656 f->bytesperline = (width * fmt->depth) >> 3;
2657 f->sizeimage = height * f->bytesperline;
2658 }
2659}
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2662{
2663 switch (f->type) {
2664 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2665 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002666 pix_format_set_size (&f->fmt.pix, fh->fmt,
2667 fh->width, fh->height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 f->fmt.pix.field = fh->cap.field;
2669 f->fmt.pix.pixelformat = fh->fmt->fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return 0;
2671 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2672 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2673 f->fmt.win.w = fh->ov.w;
2674 f->fmt.win.field = fh->ov.field;
2675 return 0;
2676 case V4L2_BUF_TYPE_VBI_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002677 bttv_vbi_get_fmt(fh, &f->fmt.vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return 0;
2679 default:
2680 return -EINVAL;
2681 }
2682}
2683
2684static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002685 struct v4l2_format *f, int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
2687 switch (f->type) {
2688 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2689 {
2690 const struct bttv_format *fmt;
2691 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002692 __s32 width, height;
2693 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2696 if (NULL == fmt)
2697 return -EINVAL;
2698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 field = f->fmt.pix.field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002700 if (V4L2_FIELD_ANY == field) {
2701 __s32 height2;
2702
2703 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2704 field = (f->fmt.pix.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 ? V4L2_FIELD_INTERLACED
2706 : V4L2_FIELD_BOTTOM;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 if (V4L2_FIELD_SEQ_BT == field)
2709 field = V4L2_FIELD_SEQ_TB;
2710 switch (field) {
2711 case V4L2_FIELD_TOP:
2712 case V4L2_FIELD_BOTTOM:
2713 case V4L2_FIELD_ALTERNATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 case V4L2_FIELD_INTERLACED:
2715 break;
2716 case V4L2_FIELD_SEQ_TB:
2717 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2718 return -EINVAL;
2719 break;
2720 default:
2721 return -EINVAL;
2722 }
2723
Michael Schimeke5bd0262007-01-18 16:17:39 -03002724 width = f->fmt.pix.width;
2725 height = f->fmt.pix.height;
2726
2727 rc = limit_scaled_size(fh, &width, &height, field,
2728 /* width_mask: 4 pixels */ ~3,
2729 /* width_bias: nearest */ 2,
2730 /* adjust_size */ 1,
2731 adjust_crop);
2732 if (0 != rc)
2733 return rc;
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 /* update data for the application */
2736 f->fmt.pix.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002737 pix_format_set_size(&f->fmt.pix, fmt, width, height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 return 0;
2740 }
2741 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002742 return verify_window(fh, &f->fmt.win,
2743 /* adjust_size */ 1,
2744 /* adjust_crop */ 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 case V4L2_BUF_TYPE_VBI_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002746 return bttv_vbi_try_fmt(fh, &f->fmt.vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 default:
2748 return -EINVAL;
2749 }
2750}
2751
2752static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2753 struct v4l2_format *f)
2754{
2755 int retval;
2756
2757 switch (f->type) {
2758 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2759 {
2760 const struct bttv_format *fmt;
2761
2762 retval = bttv_switch_type(fh,f->type);
2763 if (0 != retval)
2764 return retval;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002765 retval = bttv_try_fmt(fh,btv,f, /* adjust_crop */ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 if (0 != retval)
2767 return retval;
2768 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2769
2770 /* update our state informations */
Ingo Molnar3593cab2006-02-07 06:49:14 -02002771 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 fh->fmt = fmt;
2773 fh->cap.field = f->fmt.pix.field;
2774 fh->cap.last = V4L2_FIELD_NONE;
2775 fh->width = f->fmt.pix.width;
2776 fh->height = f->fmt.pix.height;
2777 btv->init.fmt = fmt;
2778 btv->init.width = f->fmt.pix.width;
2779 btv->init.height = f->fmt.pix.height;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002780 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 return 0;
2783 }
2784 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002785 if (no_overlay > 0) {
2786 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2787 return -EINVAL;
2788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 return setup_window(fh, btv, &f->fmt.win, 1);
2790 case V4L2_BUF_TYPE_VBI_CAPTURE:
2791 retval = bttv_switch_type(fh,f->type);
2792 if (0 != retval)
2793 return retval;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002794 return bttv_vbi_set_fmt(fh, &f->fmt.vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 default:
2796 return -EINVAL;
2797 }
2798}
2799
2800static int bttv_do_ioctl(struct inode *inode, struct file *file,
2801 unsigned int cmd, void *arg)
2802{
2803 struct bttv_fh *fh = file->private_data;
2804 struct bttv *btv = fh->btv;
2805 unsigned long flags;
2806 int retval = 0;
2807
Michael Krufky5e453dc2006-01-09 15:32:31 -02002808 if (bttv_debug > 1)
2809 v4l_print_ioctl(btv->c.name, cmd);
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 if (btv->errors)
2812 bttv_reinit_bt848(btv);
2813
2814 switch (cmd) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002815 case VIDIOCSFREQ:
2816 case VIDIOCSTUNER:
2817 case VIDIOCSCHAN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 case VIDIOC_S_CTRL:
2819 case VIDIOC_S_STD:
2820 case VIDIOC_S_INPUT:
2821 case VIDIOC_S_TUNER:
2822 case VIDIOC_S_FREQUENCY:
2823 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2824 if (0 != retval)
2825 return retval;
2826 };
2827
2828 switch (cmd) {
2829
2830 /* *** v4l1 *** ************************************************ */
2831 case VIDIOCGCAP:
2832 {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002833 struct video_capability *cap = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 memset(cap,0,sizeof(*cap));
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002836 strcpy(cap->name,btv->video_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2838 /* vbi */
2839 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2840 } else {
2841 /* others */
2842 cap->type = VID_TYPE_CAPTURE|
2843 VID_TYPE_TUNER|
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 VID_TYPE_CLIPPING|
2845 VID_TYPE_SCALES;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002846 if (no_overlay <= 0)
2847 cap->type |= VID_TYPE_OVERLAY;
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2850 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2851 cap->minwidth = 48;
2852 cap->minheight = 32;
2853 }
2854 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2855 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 }
2858
2859 case VIDIOCGPICT:
2860 {
2861 struct video_picture *pic = arg;
2862
2863 memset(pic,0,sizeof(*pic));
2864 pic->brightness = btv->bright;
2865 pic->contrast = btv->contrast;
2866 pic->hue = btv->hue;
2867 pic->colour = btv->saturation;
2868 if (fh->fmt) {
2869 pic->depth = fh->fmt->depth;
2870 pic->palette = fh->fmt->palette;
2871 }
2872 return 0;
2873 }
2874 case VIDIOCSPICT:
2875 {
2876 struct video_picture *pic = arg;
2877 const struct bttv_format *fmt;
2878
2879 fmt = format_by_palette(pic->palette);
2880 if (NULL == fmt)
2881 return -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002882 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (fmt->depth != pic->depth) {
2884 retval = -EINVAL;
2885 goto fh_unlock_and_return;
2886 }
Michael H. Schimek13c72802005-12-01 00:51:37 -08002887 if (fmt->flags & FORMAT_FLAGS_RAW) {
2888 /* VIDIOCMCAPTURE uses gbufsize, not RAW_BPL *
2889 RAW_LINES * 2. F1 is stored at offset 0, F2
2890 at buffer size / 2. */
2891 fh->width = RAW_BPL;
2892 fh->height = gbufsize / RAW_BPL;
2893 btv->init.width = RAW_BPL;
2894 btv->init.height = gbufsize / RAW_BPL;
2895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 fh->ovfmt = fmt;
2897 fh->fmt = fmt;
2898 btv->init.ovfmt = fmt;
2899 btv->init.fmt = fmt;
2900 if (bigendian) {
2901 /* dirty hack time: swap bytes for overlay if the
2902 display adaptor is big endian (insmod option) */
2903 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2904 fmt->palette == VIDEO_PALETTE_RGB565 ||
2905 fmt->palette == VIDEO_PALETTE_RGB32) {
2906 fh->ovfmt = fmt+1;
2907 }
2908 }
2909 bt848_bright(btv,pic->brightness);
2910 bt848_contrast(btv,pic->contrast);
2911 bt848_hue(btv,pic->hue);
2912 bt848_sat(btv,pic->colour);
Ingo Molnar3593cab2006-02-07 06:49:14 -02002913 mutex_unlock(&fh->cap.lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002914 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 }
2916
2917 case VIDIOCGWIN:
2918 {
2919 struct video_window *win = arg;
2920
2921 memset(win,0,sizeof(*win));
2922 win->x = fh->ov.w.left;
2923 win->y = fh->ov.w.top;
2924 win->width = fh->ov.w.width;
2925 win->height = fh->ov.w.height;
2926 return 0;
2927 }
2928 case VIDIOCSWIN:
2929 {
2930 struct video_window *win = arg;
2931 struct v4l2_window w2;
2932
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002933 if (no_overlay > 0) {
2934 printk ("VIDIOCSWIN: no_overlay\n");
2935 return -EINVAL;
2936 }
2937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 w2.field = V4L2_FIELD_ANY;
2939 w2.w.left = win->x;
2940 w2.w.top = win->y;
2941 w2.w.width = win->width;
2942 w2.w.height = win->height;
2943 w2.clipcount = win->clipcount;
2944 w2.clips = (struct v4l2_clip __user *)win->clips;
2945 retval = setup_window(fh, btv, &w2, 0);
2946 if (0 == retval) {
2947 /* on v4l1 this ioctl affects the read() size too */
2948 fh->width = fh->ov.w.width;
2949 fh->height = fh->ov.w.height;
2950 btv->init.width = fh->ov.w.width;
2951 btv->init.height = fh->ov.w.height;
2952 }
2953 return retval;
2954 }
2955
2956 case VIDIOCGFBUF:
2957 {
2958 struct video_buffer *fbuf = arg;
2959
2960 fbuf->base = btv->fbuf.base;
2961 fbuf->width = btv->fbuf.fmt.width;
2962 fbuf->height = btv->fbuf.fmt.height;
2963 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2964 if (fh->ovfmt)
2965 fbuf->depth = fh->ovfmt->depth;
Mauro Carvalho Chehab37026272006-08-06 09:10:06 -03002966 else {
2967 if (fbuf->width)
2968 fbuf->depth = ((fbuf->bytesperline<<3)
Trent Piephoa2b9e3e2006-08-07 20:01:01 -03002969 + (fbuf->width-1) )
2970 /fbuf->width;
Mauro Carvalho Chehab37026272006-08-06 09:10:06 -03002971 else
2972 fbuf->depth = 0;
2973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return 0;
2975 }
2976 case VIDIOCSFBUF:
2977 {
2978 struct video_buffer *fbuf = arg;
2979 const struct bttv_format *fmt;
2980 unsigned long end;
2981
2982 if(!capable(CAP_SYS_ADMIN) &&
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002983 !capable(CAP_SYS_RAWIO))
2984 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 end = (unsigned long)fbuf->base +
2986 fbuf->height * fbuf->bytesperline;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002987 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 retval = -EINVAL;
2989
2990 switch (fbuf->depth) {
2991 case 8:
2992 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2993 break;
2994 case 16:
2995 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2996 break;
2997 case 24:
2998 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2999 break;
3000 case 32:
3001 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
3002 break;
3003 case 15:
3004 fbuf->depth = 16;
3005 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
3006 break;
3007 default:
3008 fmt = NULL;
3009 break;
3010 }
3011 if (NULL == fmt)
3012 goto fh_unlock_and_return;
3013
3014 fh->ovfmt = fmt;
3015 fh->fmt = fmt;
3016 btv->init.ovfmt = fmt;
3017 btv->init.fmt = fmt;
3018 btv->fbuf.base = fbuf->base;
3019 btv->fbuf.fmt.width = fbuf->width;
3020 btv->fbuf.fmt.height = fbuf->height;
3021 if (fbuf->bytesperline)
3022 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
3023 else
3024 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
Ingo Molnar3593cab2006-02-07 06:49:14 -02003025 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return 0;
3027 }
3028
3029 case VIDIOCCAPTURE:
3030 case VIDIOC_OVERLAY:
3031 {
3032 struct bttv_buffer *new;
3033 int *on = arg;
3034
3035 if (*on) {
3036 /* verify args */
3037 if (NULL == btv->fbuf.base)
3038 return -EINVAL;
3039 if (!fh->ov.setup_ok) {
3040 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
3041 return -EINVAL;
3042 }
3043 }
3044
3045 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
3046 return -EBUSY;
3047
Ingo Molnar3593cab2006-02-07 06:49:14 -02003048 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 if (*on) {
3050 fh->ov.tvnorm = btv->tvnorm;
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03003051 new = videobuf_pci_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03003052 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
3054 } else {
3055 new = NULL;
3056 }
3057
3058 /* switch over */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003059 retval = bttv_switch_overlay(btv,fh,new);
Ingo Molnar3593cab2006-02-07 06:49:14 -02003060 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return retval;
3062 }
3063
3064 case VIDIOCGMBUF:
3065 {
3066 struct video_mbuf *mbuf = arg;
3067 unsigned int i;
3068
Ingo Molnar3593cab2006-02-07 06:49:14 -02003069 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
3071 V4L2_MEMORY_MMAP);
3072 if (retval < 0)
3073 goto fh_unlock_and_return;
3074 memset(mbuf,0,sizeof(*mbuf));
3075 mbuf->frames = gbuffers;
3076 mbuf->size = gbuffers * gbufsize;
3077 for (i = 0; i < gbuffers; i++)
3078 mbuf->offsets[i] = i * gbufsize;
Ingo Molnar3593cab2006-02-07 06:49:14 -02003079 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 return 0;
3081 }
3082 case VIDIOCMCAPTURE:
3083 {
3084 struct video_mmap *vm = arg;
3085 struct bttv_buffer *buf;
3086 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003087 __s32 height2;
3088 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
3090 if (vm->frame >= VIDEO_MAX_FRAME)
3091 return -EINVAL;
3092
Michael Schimeke5bd0262007-01-18 16:17:39 -03003093 res = bttv_resource(fh);
3094 if (!check_alloc_btres(btv, fh, res))
3095 return -EBUSY;
3096
Ingo Molnar3593cab2006-02-07 06:49:14 -02003097 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 retval = -EINVAL;
3099 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
3100 if (NULL == buf)
3101 goto fh_unlock_and_return;
3102 if (0 == buf->vb.baddr)
3103 goto fh_unlock_and_return;
3104 if (buf->vb.state == STATE_QUEUED ||
3105 buf->vb.state == STATE_ACTIVE)
3106 goto fh_unlock_and_return;
3107
Michael Schimeke5bd0262007-01-18 16:17:39 -03003108 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
3109 field = (vm->height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 ? V4L2_FIELD_INTERLACED
3111 : V4L2_FIELD_BOTTOM;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03003112 retval = bttv_prepare_buffer(&fh->cap,btv,buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 format_by_palette(vm->format),
3114 vm->width,vm->height,field);
3115 if (0 != retval)
3116 goto fh_unlock_and_return;
3117 spin_lock_irqsave(&btv->s_lock,flags);
3118 buffer_queue(&fh->cap,&buf->vb);
3119 spin_unlock_irqrestore(&btv->s_lock,flags);
Ingo Molnar3593cab2006-02-07 06:49:14 -02003120 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return 0;
3122 }
3123 case VIDIOCSYNC:
3124 {
3125 int *frame = arg;
3126 struct bttv_buffer *buf;
3127
3128 if (*frame >= VIDEO_MAX_FRAME)
3129 return -EINVAL;
3130
Ingo Molnar3593cab2006-02-07 06:49:14 -02003131 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 retval = -EINVAL;
3133 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
3134 if (NULL == buf)
3135 goto fh_unlock_and_return;
3136 retval = videobuf_waiton(&buf->vb,0,1);
3137 if (0 != retval)
3138 goto fh_unlock_and_return;
3139 switch (buf->vb.state) {
3140 case STATE_ERROR:
3141 retval = -EIO;
3142 /* fall through */
3143 case STATE_DONE:
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03003144 {
3145 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
3146 videobuf_dma_sync(&fh->cap,dma);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03003147 bttv_dma_free(&fh->cap,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 break;
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03003149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 default:
3151 retval = -EINVAL;
3152 break;
3153 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02003154 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return retval;
3156 }
3157
3158 case VIDIOCGVBIFMT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
3160 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
3161 if (0 != retval)
3162 return retval;
3163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Michael Schimeke5bd0262007-01-18 16:17:39 -03003165 /* fall through */
3166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 case VIDIOCSVBIFMT:
Michael Schimeke5bd0262007-01-18 16:17:39 -03003168 return v4l_compat_translate_ioctl(inode, file, cmd,
3169 arg, bttv_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003171 case BTTV_VERSION:
3172 case VIDIOCGFREQ:
3173 case VIDIOCSFREQ:
3174 case VIDIOCGTUNER:
3175 case VIDIOCSTUNER:
3176 case VIDIOCGCHAN:
3177 case VIDIOCSCHAN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 case VIDIOCGAUDIO:
3179 case VIDIOCSAUDIO:
3180 return bttv_common_ioctls(btv,cmd,arg);
3181
3182 /* *** v4l2 *** ************************************************ */
3183 case VIDIOC_QUERYCAP:
3184 {
3185 struct v4l2_capability *cap = arg;
3186
3187 if (0 == v4l2)
3188 return -EINVAL;
Michael H. Schimekbbf78712005-12-01 00:51:40 -08003189 memset(cap, 0, sizeof (*cap));
3190 strlcpy(cap->driver, "bttv", sizeof (cap->driver));
3191 strlcpy(cap->card, btv->video_dev->name, sizeof (cap->card));
3192 snprintf(cap->bus_info, sizeof (cap->bus_info),
3193 "PCI:%s", pci_name(btv->c.pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 cap->version = BTTV_VERSION_CODE;
3195 cap->capabilities =
3196 V4L2_CAP_VIDEO_CAPTURE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 V4L2_CAP_VBI_CAPTURE |
3198 V4L2_CAP_READWRITE |
3199 V4L2_CAP_STREAMING;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07003200 if (no_overlay <= 0)
3201 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
3204 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
3205 cap->capabilities |= V4L2_CAP_TUNER;
3206 return 0;
3207 }
3208
3209 case VIDIOC_ENUM_FMT:
3210 {
3211 struct v4l2_fmtdesc *f = arg;
3212 enum v4l2_buf_type type;
3213 unsigned int i;
3214 int index;
3215
3216 type = f->type;
3217 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
3218 /* vbi */
3219 index = f->index;
3220 if (0 != index)
3221 return -EINVAL;
3222 memset(f,0,sizeof(*f));
3223 f->index = index;
3224 f->type = type;
3225 f->pixelformat = V4L2_PIX_FMT_GREY;
3226 strcpy(f->description,"vbi data");
3227 return 0;
3228 }
3229
3230 /* video capture + overlay */
3231 index = -1;
3232 for (i = 0; i < BTTV_FORMATS; i++) {
3233 if (bttv_formats[i].fourcc != -1)
3234 index++;
3235 if ((unsigned int)index == f->index)
3236 break;
3237 }
3238 if (BTTV_FORMATS == i)
3239 return -EINVAL;
3240
3241 switch (f->type) {
3242 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3243 break;
3244 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3245 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
3246 return -EINVAL;
3247 break;
3248 default:
3249 return -EINVAL;
3250 }
3251 memset(f,0,sizeof(*f));
3252 f->index = index;
3253 f->type = type;
3254 f->pixelformat = bttv_formats[i].fourcc;
3255 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
3256 return 0;
3257 }
3258
3259 case VIDIOC_TRY_FMT:
3260 {
3261 struct v4l2_format *f = arg;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003262 return bttv_try_fmt(fh,btv,f, /* adjust_crop */ 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 }
3264 case VIDIOC_G_FMT:
3265 {
3266 struct v4l2_format *f = arg;
3267 return bttv_g_fmt(fh,f);
3268 }
3269 case VIDIOC_S_FMT:
3270 {
3271 struct v4l2_format *f = arg;
3272 return bttv_s_fmt(fh,btv,f);
3273 }
3274
3275 case VIDIOC_G_FBUF:
3276 {
3277 struct v4l2_framebuffer *fb = arg;
3278
3279 *fb = btv->fbuf;
3280 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3281 if (fh->ovfmt)
3282 fb->fmt.pixelformat = fh->ovfmt->fourcc;
3283 return 0;
3284 }
3285 case VIDIOC_S_FBUF:
3286 {
3287 struct v4l2_framebuffer *fb = arg;
3288 const struct bttv_format *fmt;
3289
3290 if(!capable(CAP_SYS_ADMIN) &&
3291 !capable(CAP_SYS_RAWIO))
3292 return -EPERM;
3293
3294 /* check args */
3295 fmt = format_by_fourcc(fb->fmt.pixelformat);
3296 if (NULL == fmt)
3297 return -EINVAL;
3298 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
3299 return -EINVAL;
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 retval = -EINVAL;
3302 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03003303 __s32 width = fb->fmt.width;
3304 __s32 height = fb->fmt.height;
3305
3306 retval = limit_scaled_size(fh, &width, &height,
3307 V4L2_FIELD_INTERLACED,
3308 /* width_mask */ ~3,
3309 /* width_bias */ 2,
3310 /* adjust_size */ 0,
3311 /* adjust_crop */ 0);
3312 if (0 != retval)
3313 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
3315
3316 /* ok, accept it */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003317 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 btv->fbuf.base = fb->base;
3319 btv->fbuf.fmt.width = fb->fmt.width;
3320 btv->fbuf.fmt.height = fb->fmt.height;
3321 if (0 != fb->fmt.bytesperline)
3322 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
3323 else
3324 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
3325
3326 retval = 0;
3327 fh->ovfmt = fmt;
3328 btv->init.ovfmt = fmt;
3329 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
3330 fh->ov.w.left = 0;
3331 fh->ov.w.top = 0;
3332 fh->ov.w.width = fb->fmt.width;
3333 fh->ov.w.height = fb->fmt.height;
3334 btv->init.ov.w.width = fb->fmt.width;
3335 btv->init.ov.w.height = fb->fmt.height;
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08003336 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 fh->ov.clips = NULL;
3338 fh->ov.nclips = 0;
3339
3340 if (check_btres(fh, RESOURCE_OVERLAY)) {
3341 struct bttv_buffer *new;
3342
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03003343 new = videobuf_pci_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03003344 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
3346 retval = bttv_switch_overlay(btv,fh,new);
3347 }
3348 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02003349 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 return retval;
3351 }
3352
3353 case VIDIOC_REQBUFS:
3354 return videobuf_reqbufs(bttv_queue(fh),arg);
3355
3356 case VIDIOC_QUERYBUF:
3357 return videobuf_querybuf(bttv_queue(fh),arg);
3358
3359 case VIDIOC_QBUF:
Michael Schimeke5bd0262007-01-18 16:17:39 -03003360 {
3361 int res = bttv_resource(fh);
3362
3363 if (!check_alloc_btres(btv, fh, res))
3364 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 return videobuf_qbuf(bttv_queue(fh),arg);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
3368 case VIDIOC_DQBUF:
3369 return videobuf_dqbuf(bttv_queue(fh),arg,
3370 file->f_flags & O_NONBLOCK);
3371
3372 case VIDIOC_STREAMON:
3373 {
3374 int res = bttv_resource(fh);
3375
3376 if (!check_alloc_btres(btv,fh,res))
3377 return -EBUSY;
3378 return videobuf_streamon(bttv_queue(fh));
3379 }
3380 case VIDIOC_STREAMOFF:
3381 {
3382 int res = bttv_resource(fh);
3383
3384 retval = videobuf_streamoff(bttv_queue(fh));
3385 if (retval < 0)
3386 return retval;
3387 free_btres(btv,fh,res);
3388 return 0;
3389 }
3390
3391 case VIDIOC_QUERYCTRL:
3392 {
3393 struct v4l2_queryctrl *c = arg;
3394 int i;
3395
3396 if ((c->id < V4L2_CID_BASE ||
3397 c->id >= V4L2_CID_LASTP1) &&
3398 (c->id < V4L2_CID_PRIVATE_BASE ||
3399 c->id >= V4L2_CID_PRIVATE_LASTP1))
3400 return -EINVAL;
3401 for (i = 0; i < BTTV_CTLS; i++)
3402 if (bttv_ctls[i].id == c->id)
3403 break;
3404 if (i == BTTV_CTLS) {
3405 *c = no_ctl;
3406 return 0;
3407 }
3408 *c = bttv_ctls[i];
Hans Verkuil0020d3e2006-03-30 19:50:34 -03003409 if (btv->audio_hook && i >= 4 && i <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 struct video_audio va;
3411 memset(&va,0,sizeof(va));
Hans Verkuil0020d3e2006-03-30 19:50:34 -03003412 btv->audio_hook(btv,&va,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 switch (bttv_ctls[i].id) {
3414 case V4L2_CID_AUDIO_VOLUME:
3415 if (!(va.flags & VIDEO_AUDIO_VOLUME))
3416 *c = no_ctl;
3417 break;
3418 case V4L2_CID_AUDIO_BALANCE:
3419 if (!(va.flags & VIDEO_AUDIO_BALANCE))
3420 *c = no_ctl;
3421 break;
3422 case V4L2_CID_AUDIO_BASS:
3423 if (!(va.flags & VIDEO_AUDIO_BASS))
3424 *c = no_ctl;
3425 break;
3426 case V4L2_CID_AUDIO_TREBLE:
3427 if (!(va.flags & VIDEO_AUDIO_TREBLE))
3428 *c = no_ctl;
3429 break;
3430 }
3431 }
3432 return 0;
3433 }
3434 case VIDIOC_G_CTRL:
3435 return get_control(btv,arg);
3436 case VIDIOC_S_CTRL:
3437 return set_control(btv,arg);
3438 case VIDIOC_G_PARM:
3439 {
3440 struct v4l2_streamparm *parm = arg;
3441 struct v4l2_standard s;
3442 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
3443 return -EINVAL;
3444 memset(parm,0,sizeof(*parm));
3445 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
3446 bttv_tvnorms[btv->tvnorm].name);
3447 parm->parm.capture.timeperframe = s.frameperiod;
3448 return 0;
3449 }
3450
3451 case VIDIOC_G_PRIORITY:
3452 {
3453 enum v4l2_priority *p = arg;
3454
3455 *p = v4l2_prio_max(&btv->prio);
3456 return 0;
3457 }
3458 case VIDIOC_S_PRIORITY:
3459 {
3460 enum v4l2_priority *prio = arg;
3461
3462 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
3463 }
3464
Michael Schimeke5bd0262007-01-18 16:17:39 -03003465 case VIDIOC_CROPCAP:
3466 {
3467 struct v4l2_cropcap *cap = arg;
3468 enum v4l2_buf_type type;
3469
3470 type = cap->type;
3471
3472 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3473 type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3474 return -EINVAL;
3475
3476 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3477 cap->type = type;
3478
3479 return 0;
3480 }
3481 case VIDIOC_G_CROP:
3482 {
3483 struct v4l2_crop * crop = arg;
3484
3485 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3486 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3487 return -EINVAL;
3488
3489 /* No fh->do_crop = 1; because btv->crop[1] may be
3490 inconsistent with fh->width or fh->height and apps
3491 do not expect a change here. */
3492
3493 crop->c = btv->crop[!!fh->do_crop].rect;
3494
3495 return 0;
3496 }
3497 case VIDIOC_S_CROP:
3498 {
3499 struct v4l2_crop *crop = arg;
3500 const struct v4l2_rect *b;
3501 struct bttv_crop c;
3502 __s32 b_left;
3503 __s32 b_top;
3504 __s32 b_right;
3505 __s32 b_bottom;
3506
3507 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3508 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3509 return -EINVAL;
3510
3511 retval = v4l2_prio_check(&btv->prio,&fh->prio);
3512 if (0 != retval)
3513 return retval;
3514
3515 /* Make sure tvnorm, vbi_end and the current cropping
3516 parameters remain consistent until we're done. Note
3517 read() may change vbi_end in check_alloc_btres(). */
3518 mutex_lock(&btv->lock);
3519
3520 retval = -EBUSY;
3521
3522 if (locked_btres(fh->btv, VIDEO_RESOURCES))
3523 goto btv_unlock_and_return;
3524
3525 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3526
3527 b_left = b->left;
3528 b_right = b_left + b->width;
3529 b_bottom = b->top + b->height;
3530
3531 b_top = max(b->top, btv->vbi_end);
3532 if (b_top + 32 >= b_bottom)
3533 goto btv_unlock_and_return;
3534
3535 /* Min. scaled size 48 x 32. */
3536 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3537 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3538
3539 c.rect.width = clamp(crop->c.width,
3540 48, b_right - c.rect.left);
3541
3542 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3543 /* Top and height must be a multiple of two. */
3544 c.rect.top = (c.rect.top + 1) & ~1;
3545
3546 c.rect.height = clamp(crop->c.height,
3547 32, b_bottom - c.rect.top);
3548 c.rect.height = (c.rect.height + 1) & ~1;
3549
3550 bttv_crop_calc_limits(&c);
3551
3552 btv->crop[1] = c;
3553
3554 mutex_unlock(&btv->lock);
3555
3556 fh->do_crop = 1;
3557
3558 mutex_lock(&fh->cap.lock);
3559
3560 if (fh->width < c.min_scaled_width) {
3561 fh->width = c.min_scaled_width;
3562 btv->init.width = c.min_scaled_width;
3563 } else if (fh->width > c.max_scaled_width) {
3564 fh->width = c.max_scaled_width;
3565 btv->init.width = c.max_scaled_width;
3566 }
3567
3568 if (fh->height < c.min_scaled_height) {
3569 fh->height = c.min_scaled_height;
3570 btv->init.height = c.min_scaled_height;
3571 } else if (fh->height > c.max_scaled_height) {
3572 fh->height = c.max_scaled_height;
3573 btv->init.height = c.max_scaled_height;
3574 }
3575
3576 mutex_unlock(&fh->cap.lock);
3577
3578 return 0;
3579 }
3580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 case VIDIOC_ENUMSTD:
3582 case VIDIOC_G_STD:
3583 case VIDIOC_S_STD:
3584 case VIDIOC_ENUMINPUT:
3585 case VIDIOC_G_INPUT:
3586 case VIDIOC_S_INPUT:
3587 case VIDIOC_G_TUNER:
3588 case VIDIOC_S_TUNER:
3589 case VIDIOC_G_FREQUENCY:
3590 case VIDIOC_S_FREQUENCY:
Hans Verkuil299392b2005-11-08 21:37:42 -08003591 case VIDIOC_LOG_STATUS:
Trent Piepho55c0d102007-06-29 00:17:36 -03003592 case VIDIOC_DBG_G_REGISTER:
3593 case VIDIOC_DBG_S_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 return bttv_common_ioctls(btv,cmd,arg);
3595
3596 default:
3597 return -ENOIOCTLCMD;
3598 }
3599 return 0;
3600
3601 fh_unlock_and_return:
Ingo Molnar3593cab2006-02-07 06:49:14 -02003602 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 return retval;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003604
3605 btv_unlock_and_return:
3606 mutex_unlock(&btv->lock);
3607 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608}
3609
3610static int bttv_ioctl(struct inode *inode, struct file *file,
3611 unsigned int cmd, unsigned long arg)
3612{
3613 struct bttv_fh *fh = file->private_data;
3614
3615 switch (cmd) {
3616 case BTTV_VBISIZE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03003617 {
3618 const struct bttv_tvnorm *tvnorm;
3619
3620 tvnorm = fh->vbi_fmt.tvnorm;
3621
3622 if (fh->vbi_fmt.fmt.start[0] != tvnorm->vbistart[0] ||
3623 fh->vbi_fmt.fmt.start[1] != tvnorm->vbistart[1] ||
3624 fh->vbi_fmt.fmt.count[0] != fh->vbi_fmt.fmt.count[1]) {
3625 /* BTTV_VBISIZE cannot express these parameters,
3626 however open() resets the paramters to defaults
3627 and apps shouldn't call BTTV_VBISIZE after
3628 VIDIOC_S_FMT. */
3629 return -EINVAL;
3630 }
3631
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003633 return (fh->vbi_fmt.fmt.count[0] * 2
3634 * fh->vbi_fmt.fmt.samples_per_line);
3635 }
3636
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 default:
3638 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
3639 }
3640}
3641
3642static ssize_t bttv_read(struct file *file, char __user *data,
3643 size_t count, loff_t *ppos)
3644{
3645 struct bttv_fh *fh = file->private_data;
3646 int retval = 0;
3647
3648 if (fh->btv->errors)
3649 bttv_reinit_bt848(fh->btv);
3650 dprintk("bttv%d: read count=%d type=%s\n",
3651 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3652
3653 switch (fh->type) {
3654 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03003655 if (!check_alloc_btres(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3656 /* VIDEO_READ in use by another fh,
3657 or VIDEO_STREAM by any fh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 return -EBUSY;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3661 file->f_flags & O_NONBLOCK);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003662 free_btres(fh->btv, fh, RESOURCE_VIDEO_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 break;
3664 case V4L2_BUF_TYPE_VBI_CAPTURE:
3665 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3666 return -EBUSY;
3667 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3668 file->f_flags & O_NONBLOCK);
3669 break;
3670 default:
3671 BUG();
3672 }
3673 return retval;
3674}
3675
3676static unsigned int bttv_poll(struct file *file, poll_table *wait)
3677{
3678 struct bttv_fh *fh = file->private_data;
3679 struct bttv_buffer *buf;
3680 enum v4l2_field field;
3681
3682 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3683 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3684 return POLLERR;
3685 return videobuf_poll_stream(file, &fh->vbi, wait);
3686 }
3687
Michael Schimeke5bd0262007-01-18 16:17:39 -03003688 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 /* streaming capture */
3690 if (list_empty(&fh->cap.stream))
3691 return POLLERR;
3692 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3693 } else {
3694 /* read() capture */
Ingo Molnar3593cab2006-02-07 06:49:14 -02003695 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 if (NULL == fh->cap.read_buf) {
3697 /* need to capture a new frame */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003698 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02003699 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 return POLLERR;
3701 }
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03003702 fh->cap.read_buf = videobuf_pci_alloc(fh->cap.msize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 if (NULL == fh->cap.read_buf) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02003704 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 return POLLERR;
3706 }
3707 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3708 field = videobuf_next_field(&fh->cap);
3709 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -08003710 kfree (fh->cap.read_buf);
3711 fh->cap.read_buf = NULL;
Ingo Molnar3593cab2006-02-07 06:49:14 -02003712 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 return POLLERR;
3714 }
3715 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3716 fh->cap.read_off = 0;
3717 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02003718 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 buf = (struct bttv_buffer*)fh->cap.read_buf;
3720 }
3721
3722 poll_wait(file, &buf->vb.done, wait);
3723 if (buf->vb.state == STATE_DONE ||
3724 buf->vb.state == STATE_ERROR)
3725 return POLLIN|POLLRDNORM;
3726 return 0;
3727}
3728
3729static int bttv_open(struct inode *inode, struct file *file)
3730{
3731 int minor = iminor(inode);
3732 struct bttv *btv = NULL;
3733 struct bttv_fh *fh;
3734 enum v4l2_buf_type type = 0;
3735 unsigned int i;
3736
3737 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3738
3739 for (i = 0; i < bttv_num; i++) {
3740 if (bttvs[i].video_dev &&
3741 bttvs[i].video_dev->minor == minor) {
3742 btv = &bttvs[i];
3743 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3744 break;
3745 }
3746 if (bttvs[i].vbi_dev &&
3747 bttvs[i].vbi_dev->minor == minor) {
3748 btv = &bttvs[i];
3749 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3750 break;
3751 }
3752 }
3753 if (NULL == btv)
3754 return -ENODEV;
3755
3756 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3757 btv->c.nr,v4l2_type_names[type]);
3758
3759 /* allocate per filehandle data */
3760 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3761 if (NULL == fh)
3762 return -ENOMEM;
3763 file->private_data = fh;
3764 *fh = btv->init;
3765 fh->type = type;
3766 fh->ov.setup_ok = 0;
3767 v4l2_prio_open(&btv->prio,&fh->prio);
3768
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03003769 videobuf_queue_pci_init(&fh->cap, &bttv_video_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 btv->c.pci, &btv->s_lock,
3771 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3772 V4L2_FIELD_INTERLACED,
3773 sizeof(struct bttv_buffer),
3774 fh);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03003775 videobuf_queue_pci_init(&fh->vbi, &bttv_vbi_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 btv->c.pci, &btv->s_lock,
3777 V4L2_BUF_TYPE_VBI_CAPTURE,
3778 V4L2_FIELD_SEQ_TB,
3779 sizeof(struct bttv_buffer),
3780 fh);
3781 i2c_vidiocschan(btv);
3782
3783 btv->users++;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003784
3785 /* The V4L2 spec requires one global set of cropping parameters
3786 which only change on request. These are stored in btv->crop[1].
3787 However for compatibility with V4L apps and cropping unaware
3788 V4L2 apps we now reset the cropping parameters as seen through
3789 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3790 will use btv->crop[0], the default cropping parameters for the
3791 current video standard, and VIDIOC_S_FMT will not implicitely
3792 change the cropping parameters until VIDIOC_S_CROP has been
3793 called. */
3794 fh->do_crop = !reset_crop; /* module parameter */
3795
3796 /* Likewise there should be one global set of VBI capture
3797 parameters, but for compatibility with V4L apps and earlier
3798 driver versions each fh has its own parameters. */
3799 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3800
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 bttv_field_count(btv);
3802 return 0;
3803}
3804
3805static int bttv_release(struct inode *inode, struct file *file)
3806{
3807 struct bttv_fh *fh = file->private_data;
3808 struct bttv *btv = fh->btv;
3809
3810 /* turn off overlay */
3811 if (check_btres(fh, RESOURCE_OVERLAY))
3812 bttv_switch_overlay(btv,fh,NULL);
3813
3814 /* stop video capture */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003815 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 videobuf_streamoff(&fh->cap);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003817 free_btres(btv,fh,RESOURCE_VIDEO_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 }
3819 if (fh->cap.read_buf) {
3820 buffer_release(&fh->cap,fh->cap.read_buf);
3821 kfree(fh->cap.read_buf);
3822 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03003823 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3824 free_btres(btv, fh, RESOURCE_VIDEO_READ);
3825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
3827 /* stop vbi capture */
3828 if (check_btres(fh, RESOURCE_VBI)) {
3829 if (fh->vbi.streaming)
3830 videobuf_streamoff(&fh->vbi);
3831 if (fh->vbi.reading)
3832 videobuf_read_stop(&fh->vbi);
3833 free_btres(btv,fh,RESOURCE_VBI);
3834 }
3835
3836 /* free stuff */
3837 videobuf_mmap_free(&fh->cap);
3838 videobuf_mmap_free(&fh->vbi);
3839 v4l2_prio_close(&btv->prio,&fh->prio);
3840 file->private_data = NULL;
3841 kfree(fh);
3842
3843 btv->users--;
3844 bttv_field_count(btv);
3845 return 0;
3846}
3847
3848static int
3849bttv_mmap(struct file *file, struct vm_area_struct *vma)
3850{
3851 struct bttv_fh *fh = file->private_data;
3852
3853 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3854 fh->btv->c.nr, v4l2_type_names[fh->type],
3855 vma->vm_start, vma->vm_end - vma->vm_start);
3856 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3857}
3858
Arjan van de Venfa027c22007-02-12 00:55:33 -08003859static const struct file_operations bttv_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860{
3861 .owner = THIS_MODULE,
3862 .open = bttv_open,
3863 .release = bttv_release,
3864 .ioctl = bttv_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02003865 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 .llseek = no_llseek,
3867 .read = bttv_read,
3868 .mmap = bttv_mmap,
3869 .poll = bttv_poll,
3870};
3871
3872static struct video_device bttv_video_template =
3873{
3874 .name = "UNSET",
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07003875 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003876 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 .hardware = VID_HARDWARE_BT848,
3878 .fops = &bttv_fops,
3879 .minor = -1,
3880};
3881
3882static struct video_device bttv_vbi_template =
3883{
3884 .name = "bt848/878 vbi",
3885 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3886 .hardware = VID_HARDWARE_BT848,
3887 .fops = &bttv_fops,
3888 .minor = -1,
3889};
3890
3891/* ----------------------------------------------------------------------- */
3892/* radio interface */
3893
3894static int radio_open(struct inode *inode, struct file *file)
3895{
3896 int minor = iminor(inode);
3897 struct bttv *btv = NULL;
3898 unsigned int i;
3899
3900 dprintk("bttv: open minor=%d\n",minor);
3901
3902 for (i = 0; i < bttv_num; i++) {
3903 if (bttvs[i].radio_dev->minor == minor) {
3904 btv = &bttvs[i];
3905 break;
3906 }
3907 }
3908 if (NULL == btv)
3909 return -ENODEV;
3910
3911 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02003912 mutex_lock(&btv->lock);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003913
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 btv->radio_user++;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003915
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 file->private_data = btv;
3917
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03003918 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,NULL);
3919 audio_input(btv,TVAUDIO_INPUT_RADIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02003921 mutex_unlock(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923}
3924
3925static int radio_release(struct inode *inode, struct file *file)
3926{
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003927 struct bttv *btv = file->private_data;
3928 struct rds_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
3930 btv->radio_user--;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003931
3932 bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 return 0;
3935}
3936
3937static int radio_do_ioctl(struct inode *inode, struct file *file,
3938 unsigned int cmd, void *arg)
3939{
3940 struct bttv *btv = file->private_data;
3941
3942 switch (cmd) {
3943 case VIDIOCGCAP:
3944 {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003945 struct video_capability *cap = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
3947 memset(cap,0,sizeof(*cap));
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003948 strcpy(cap->name,btv->radio_dev->name);
3949 cap->type = VID_TYPE_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 cap->channels = 1;
3951 cap->audios = 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003952 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 }
3954
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003955 case VIDIOCGTUNER:
3956 {
3957 struct video_tuner *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003959 if(v->tuner)
3960 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 memset(v,0,sizeof(*v));
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003962 strcpy(v->name, "Radio");
3963 bttv_call_i2c_clients(btv,cmd,v);
3964 return 0;
3965 }
3966 case VIDIOCSTUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 /* nothing to do */
3968 return 0;
3969
3970 case BTTV_VERSION:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003971 case VIDIOCGFREQ:
3972 case VIDIOCSFREQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 case VIDIOCGAUDIO:
3974 case VIDIOCSAUDIO:
Hans Verkuil5af0c8f2006-01-09 18:21:37 -02003975 case VIDIOC_LOG_STATUS:
Trent Piepho55c0d102007-06-29 00:17:36 -03003976 case VIDIOC_DBG_G_REGISTER:
3977 case VIDIOC_DBG_S_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 return bttv_common_ioctls(btv,cmd,arg);
3979
3980 default:
3981 return -ENOIOCTLCMD;
3982 }
3983 return 0;
3984}
3985
3986static int radio_ioctl(struct inode *inode, struct file *file,
3987 unsigned int cmd, unsigned long arg)
3988{
3989 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3990}
3991
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003992static ssize_t radio_read(struct file *file, char __user *data,
3993 size_t count, loff_t *ppos)
3994{
3995 struct bttv *btv = file->private_data;
3996 struct rds_command cmd;
3997 cmd.block_count = count/3;
3998 cmd.buffer = data;
3999 cmd.instance = file;
4000 cmd.result = -ENODEV;
4001
4002 bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
4003
4004 return cmd.result;
4005}
4006
4007static unsigned int radio_poll(struct file *file, poll_table *wait)
4008{
4009 struct bttv *btv = file->private_data;
4010 struct rds_command cmd;
4011 cmd.instance = file;
4012 cmd.event_list = wait;
4013 cmd.result = -ENODEV;
4014 bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
4015
4016 return cmd.result;
4017}
4018
Arjan van de Venfa027c22007-02-12 00:55:33 -08004019static const struct file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
4021 .owner = THIS_MODULE,
4022 .open = radio_open,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07004023 .read = radio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 .release = radio_release,
4025 .ioctl = radio_ioctl,
4026 .llseek = no_llseek,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07004027 .poll = radio_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028};
4029
4030static struct video_device radio_template =
4031{
4032 .name = "bt848/878 radio",
4033 .type = VID_TYPE_TUNER,
4034 .hardware = VID_HARDWARE_BT848,
4035 .fops = &radio_fops,
4036 .minor = -1,
4037};
4038
4039/* ----------------------------------------------------------------------- */
4040/* some debug code */
4041
Adrian Bunk408b6642005-05-01 08:59:29 -07004042static int bttv_risc_decode(u32 risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
4044 static char *instr[16] = {
4045 [ BT848_RISC_WRITE >> 28 ] = "write",
4046 [ BT848_RISC_SKIP >> 28 ] = "skip",
4047 [ BT848_RISC_WRITEC >> 28 ] = "writec",
4048 [ BT848_RISC_JUMP >> 28 ] = "jump",
4049 [ BT848_RISC_SYNC >> 28 ] = "sync",
4050 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
4051 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
4052 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
4053 };
4054 static int incr[16] = {
4055 [ BT848_RISC_WRITE >> 28 ] = 2,
4056 [ BT848_RISC_JUMP >> 28 ] = 2,
4057 [ BT848_RISC_SYNC >> 28 ] = 2,
4058 [ BT848_RISC_WRITE123 >> 28 ] = 5,
4059 [ BT848_RISC_SKIP123 >> 28 ] = 2,
4060 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
4061 };
4062 static char *bits[] = {
4063 "be0", "be1", "be2", "be3/resync",
4064 "set0", "set1", "set2", "set3",
4065 "clr0", "clr1", "clr2", "clr3",
4066 "irq", "res", "eol", "sol",
4067 };
4068 int i;
4069
4070 printk("0x%08x [ %s", risc,
4071 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
4072 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
4073 if (risc & (1 << (i + 12)))
4074 printk(" %s",bits[i]);
4075 printk(" count=%d ]\n", risc & 0xfff);
4076 return incr[risc >> 28] ? incr[risc >> 28] : 1;
4077}
4078
Adrian Bunk408b6642005-05-01 08:59:29 -07004079static void bttv_risc_disasm(struct bttv *btv,
4080 struct btcx_riscmem *risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081{
4082 unsigned int i,j,n;
4083
4084 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
4085 btv->c.name, risc->cpu, (unsigned long)risc->dma);
4086 for (i = 0; i < (risc->size >> 2); i += n) {
4087 printk("%s: 0x%lx: ", btv->c.name,
4088 (unsigned long)(risc->dma + (i<<2)));
4089 n = bttv_risc_decode(risc->cpu[i]);
4090 for (j = 1; j < n; j++)
4091 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
4092 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
4093 risc->cpu[i+j], j);
4094 if (0 == risc->cpu[i])
4095 break;
4096 }
4097}
4098
4099static void bttv_print_riscaddr(struct bttv *btv)
4100{
4101 printk(" main: %08Lx\n",
4102 (unsigned long long)btv->main.dma);
4103 printk(" vbi : o=%08Lx e=%08Lx\n",
4104 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
4105 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
4106 printk(" cap : o=%08Lx e=%08Lx\n",
4107 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
4108 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
4109 printk(" scr : o=%08Lx e=%08Lx\n",
4110 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
4111 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
4112 bttv_risc_disasm(btv, &btv->main);
4113}
4114
4115/* ----------------------------------------------------------------------- */
4116/* irq handler */
4117
4118static char *irq_name[] = {
4119 "FMTCHG", // format change detected (525 vs. 625)
4120 "VSYNC", // vertical sync (new field)
4121 "HSYNC", // horizontal sync
4122 "OFLOW", // chroma/luma AGC overflow
4123 "HLOCK", // horizontal lock changed
4124 "VPRES", // video presence changed
4125 "6", "7",
4126 "I2CDONE", // hw irc operation finished
4127 "GPINT", // gpio port triggered irq
4128 "10",
4129 "RISCI", // risc instruction triggered irq
4130 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
4131 "FTRGT", // pixel data fifo overrun
4132 "FDSR", // fifo data stream resyncronisation
4133 "PPERR", // parity error (data transfer)
4134 "RIPERR", // parity error (read risc instructions)
4135 "PABORT", // pci abort
4136 "OCERR", // risc instruction error
4137 "SCERR", // syncronisation error
4138};
4139
4140static void bttv_print_irqbits(u32 print, u32 mark)
4141{
4142 unsigned int i;
4143
4144 printk("bits:");
4145 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
4146 if (print & (1 << i))
4147 printk(" %s",irq_name[i]);
4148 if (mark & (1 << i))
4149 printk("*");
4150 }
4151}
4152
4153static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
4154{
4155 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
4156 btv->c.nr,
4157 (unsigned long)btv->main.dma,
4158 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
4159 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
4160 (unsigned long)rc);
4161
4162 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
4163 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
4164 "Ok, then this is harmless, don't worry ;)\n",
4165 btv->c.nr);
4166 return;
4167 }
4168 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
4169 btv->c.nr);
4170 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
4171 btv->c.nr);
4172 dump_stack();
4173}
4174
4175static int
4176bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
4177{
4178 struct bttv_buffer *item;
4179
4180 memset(set,0,sizeof(*set));
4181
4182 /* capture request ? */
4183 if (!list_empty(&btv->capture)) {
4184 set->frame_irq = 1;
4185 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
4186 if (V4L2_FIELD_HAS_TOP(item->vb.field))
4187 set->top = item;
4188 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
4189 set->bottom = item;
4190
4191 /* capture request for other field ? */
4192 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
4193 (item->vb.queue.next != &btv->capture)) {
4194 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
4195 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
4196 if (NULL == set->top &&
4197 V4L2_FIELD_TOP == item->vb.field) {
4198 set->top = item;
4199 }
4200 if (NULL == set->bottom &&
4201 V4L2_FIELD_BOTTOM == item->vb.field) {
4202 set->bottom = item;
4203 }
4204 if (NULL != set->top && NULL != set->bottom)
4205 set->top_irq = 2;
4206 }
4207 }
4208 }
4209
4210 /* screen overlay ? */
4211 if (NULL != btv->screen) {
4212 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
4213 if (NULL == set->top && NULL == set->bottom) {
4214 set->top = btv->screen;
4215 set->bottom = btv->screen;
4216 }
4217 } else {
4218 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
4219 NULL == set->top) {
4220 set->top = btv->screen;
4221 }
4222 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
4223 NULL == set->bottom) {
4224 set->bottom = btv->screen;
4225 }
4226 }
4227 }
4228
4229 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
4230 btv->c.nr,set->top, set->bottom,
4231 btv->screen,set->frame_irq,set->top_irq);
4232 return 0;
4233}
4234
4235static void
4236bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
4237 struct bttv_buffer_set *curr, unsigned int state)
4238{
4239 struct timeval ts;
4240
4241 do_gettimeofday(&ts);
4242
4243 if (wakeup->top == wakeup->bottom) {
4244 if (NULL != wakeup->top && curr->top != wakeup->top) {
4245 if (irq_debug > 1)
4246 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
4247 wakeup->top->vb.ts = ts;
4248 wakeup->top->vb.field_count = btv->field_count;
4249 wakeup->top->vb.state = state;
4250 wake_up(&wakeup->top->vb.done);
4251 }
4252 } else {
4253 if (NULL != wakeup->top && curr->top != wakeup->top) {
4254 if (irq_debug > 1)
4255 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
4256 wakeup->top->vb.ts = ts;
4257 wakeup->top->vb.field_count = btv->field_count;
4258 wakeup->top->vb.state = state;
4259 wake_up(&wakeup->top->vb.done);
4260 }
4261 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
4262 if (irq_debug > 1)
4263 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
4264 wakeup->bottom->vb.ts = ts;
4265 wakeup->bottom->vb.field_count = btv->field_count;
4266 wakeup->bottom->vb.state = state;
4267 wake_up(&wakeup->bottom->vb.done);
4268 }
4269 }
4270}
4271
4272static void
4273bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
4274 unsigned int state)
4275{
4276 struct timeval ts;
4277
4278 if (NULL == wakeup)
4279 return;
4280
4281 do_gettimeofday(&ts);
4282 wakeup->vb.ts = ts;
4283 wakeup->vb.field_count = btv->field_count;
4284 wakeup->vb.state = state;
4285 wake_up(&wakeup->vb.done);
4286}
4287
4288static void bttv_irq_timeout(unsigned long data)
4289{
4290 struct bttv *btv = (struct bttv *)data;
4291 struct bttv_buffer_set old,new;
4292 struct bttv_buffer *ovbi;
4293 struct bttv_buffer *item;
4294 unsigned long flags;
4295
4296 if (bttv_verbose) {
4297 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
4298 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
4299 btread(BT848_RISC_COUNT));
4300 bttv_print_irqbits(btread(BT848_INT_STAT),0);
4301 printk("\n");
4302 }
4303
4304 spin_lock_irqsave(&btv->s_lock,flags);
4305
4306 /* deactivate stuff */
4307 memset(&new,0,sizeof(new));
4308 old = btv->curr;
4309 ovbi = btv->cvbi;
4310 btv->curr = new;
4311 btv->cvbi = NULL;
4312 btv->loop_irq = 0;
4313 bttv_buffer_activate_video(btv, &new);
4314 bttv_buffer_activate_vbi(btv, NULL);
4315 bttv_set_dma(btv, 0);
4316
4317 /* wake up */
4318 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
4319 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
4320
4321 /* cancel all outstanding capture / vbi requests */
4322 while (!list_empty(&btv->capture)) {
4323 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
4324 list_del(&item->vb.queue);
4325 item->vb.state = STATE_ERROR;
4326 wake_up(&item->vb.done);
4327 }
4328 while (!list_empty(&btv->vcapture)) {
4329 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4330 list_del(&item->vb.queue);
4331 item->vb.state = STATE_ERROR;
4332 wake_up(&item->vb.done);
4333 }
4334
4335 btv->errors++;
4336 spin_unlock_irqrestore(&btv->s_lock,flags);
4337}
4338
4339static void
4340bttv_irq_wakeup_top(struct bttv *btv)
4341{
4342 struct bttv_buffer *wakeup = btv->curr.top;
4343
4344 if (NULL == wakeup)
4345 return;
4346
4347 spin_lock(&btv->s_lock);
4348 btv->curr.top_irq = 0;
4349 btv->curr.top = NULL;
4350 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
4351
4352 do_gettimeofday(&wakeup->vb.ts);
4353 wakeup->vb.field_count = btv->field_count;
4354 wakeup->vb.state = STATE_DONE;
4355 wake_up(&wakeup->vb.done);
4356 spin_unlock(&btv->s_lock);
4357}
4358
4359static inline int is_active(struct btcx_riscmem *risc, u32 rc)
4360{
4361 if (rc < risc->dma)
4362 return 0;
4363 if (rc > risc->dma + risc->size)
4364 return 0;
4365 return 1;
4366}
4367
4368static void
4369bttv_irq_switch_video(struct bttv *btv)
4370{
4371 struct bttv_buffer_set new;
4372 struct bttv_buffer_set old;
4373 dma_addr_t rc;
4374
4375 spin_lock(&btv->s_lock);
4376
4377 /* new buffer set */
4378 bttv_irq_next_video(btv, &new);
4379 rc = btread(BT848_RISC_COUNT);
4380 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
4381 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
4382 btv->framedrop++;
4383 if (debug_latency)
4384 bttv_irq_debug_low_latency(btv, rc);
4385 spin_unlock(&btv->s_lock);
4386 return;
4387 }
4388
4389 /* switch over */
4390 old = btv->curr;
4391 btv->curr = new;
4392 btv->loop_irq &= ~1;
4393 bttv_buffer_activate_video(btv, &new);
4394 bttv_set_dma(btv, 0);
4395
4396 /* switch input */
4397 if (UNSET != btv->new_input) {
4398 video_mux(btv,btv->new_input);
4399 btv->new_input = UNSET;
4400 }
4401
4402 /* wake up finished buffers */
4403 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
4404 spin_unlock(&btv->s_lock);
4405}
4406
4407static void
4408bttv_irq_switch_vbi(struct bttv *btv)
4409{
4410 struct bttv_buffer *new = NULL;
4411 struct bttv_buffer *old;
4412 u32 rc;
4413
4414 spin_lock(&btv->s_lock);
4415
4416 if (!list_empty(&btv->vcapture))
4417 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4418 old = btv->cvbi;
4419
4420 rc = btread(BT848_RISC_COUNT);
4421 if (NULL != old && (is_active(&old->top, rc) ||
4422 is_active(&old->bottom, rc))) {
4423 btv->framedrop++;
4424 if (debug_latency)
4425 bttv_irq_debug_low_latency(btv, rc);
4426 spin_unlock(&btv->s_lock);
4427 return;
4428 }
4429
4430 /* switch */
4431 btv->cvbi = new;
4432 btv->loop_irq &= ~4;
4433 bttv_buffer_activate_vbi(btv, new);
4434 bttv_set_dma(btv, 0);
4435
4436 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
4437 spin_unlock(&btv->s_lock);
4438}
4439
David Howells7d12e782006-10-05 14:55:46 +01004440static irqreturn_t bttv_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441{
4442 u32 stat,astat;
4443 u32 dstat;
4444 int count;
4445 struct bttv *btv;
4446 int handled = 0;
4447
4448 btv=(struct bttv *)dev_id;
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004449
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004450 if (btv->custom_irq)
4451 handled = btv->custom_irq(btv);
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 count=0;
4454 while (1) {
4455 /* get/clear interrupt status bits */
4456 stat=btread(BT848_INT_STAT);
4457 astat=stat&btread(BT848_INT_MASK);
4458 if (!astat)
4459 break;
4460 handled = 1;
4461 btwrite(stat,BT848_INT_STAT);
4462
4463 /* get device status bits */
4464 dstat=btread(BT848_DSTATUS);
4465
4466 if (irq_debug) {
4467 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4468 "riscs=%x, riscc=%08x, ",
4469 btv->c.nr, count, btv->field_count,
4470 stat>>28, btread(BT848_RISC_COUNT));
4471 bttv_print_irqbits(stat,astat);
4472 if (stat & BT848_INT_HLOCK)
4473 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4474 ? "yes" : "no");
4475 if (stat & BT848_INT_VPRES)
4476 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4477 ? "yes" : "no");
4478 if (stat & BT848_INT_FMTCHG)
4479 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4480 ? "625" : "525");
4481 printk("\n");
4482 }
4483
4484 if (astat&BT848_INT_VSYNC)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004485 btv->field_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004487 if ((astat & BT848_INT_GPINT) && btv->remote) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004489 bttv_input_irq(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 }
4491
4492 if (astat & BT848_INT_I2CDONE) {
4493 btv->i2c_done = stat;
4494 wake_up(&btv->i2c_queue);
4495 }
4496
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004497 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 bttv_irq_switch_vbi(btv);
4499
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004500 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 bttv_irq_wakeup_top(btv);
4502
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004503 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 bttv_irq_switch_video(btv);
4505
4506 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004507 audio_mute(btv, btv->mute); /* trigger automute */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
4509 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4510 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4511 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4512 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4513 btread(BT848_RISC_COUNT));
4514 bttv_print_irqbits(stat,astat);
4515 printk("\n");
4516 if (bttv_debug)
4517 bttv_print_riscaddr(btv);
4518 }
4519 if (fdsr && astat & BT848_INT_FDSR) {
4520 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4521 btv->c.nr,btread(BT848_RISC_COUNT));
4522 if (bttv_debug)
4523 bttv_print_riscaddr(btv);
4524 }
4525
4526 count++;
4527 if (count > 4) {
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004528
4529 if (count > 8 || !(astat & BT848_INT_GPINT)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004530 btwrite(0, BT848_INT_MASK);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004531
4532 printk(KERN_ERR
4533 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4534 } else {
4535 printk(KERN_ERR
4536 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4537
4538 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4539 BT848_INT_MASK);
4540 };
4541
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 bttv_print_irqbits(stat,astat);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004543
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 printk("]\n");
4545 }
4546 }
4547 btv->irq_total++;
4548 if (handled)
4549 btv->irq_me++;
4550 return IRQ_RETVAL(handled);
4551}
4552
4553
4554/* ----------------------------------------------------------------------- */
4555/* initialitation */
4556
4557static struct video_device *vdev_init(struct bttv *btv,
4558 struct video_device *template,
4559 char *type)
4560{
4561 struct video_device *vfd;
4562
4563 vfd = video_device_alloc();
4564 if (NULL == vfd)
4565 return NULL;
4566 *vfd = *template;
4567 vfd->minor = -1;
4568 vfd->dev = &btv->c.pci->dev;
4569 vfd->release = video_device_release;
4570 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4571 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4572 type, bttv_tvcards[btv->c.type].name);
4573 return vfd;
4574}
4575
4576static void bttv_unregister_video(struct bttv *btv)
4577{
4578 if (btv->video_dev) {
4579 if (-1 != btv->video_dev->minor)
4580 video_unregister_device(btv->video_dev);
4581 else
4582 video_device_release(btv->video_dev);
4583 btv->video_dev = NULL;
4584 }
4585 if (btv->vbi_dev) {
4586 if (-1 != btv->vbi_dev->minor)
4587 video_unregister_device(btv->vbi_dev);
4588 else
4589 video_device_release(btv->vbi_dev);
4590 btv->vbi_dev = NULL;
4591 }
4592 if (btv->radio_dev) {
4593 if (-1 != btv->radio_dev->minor)
4594 video_unregister_device(btv->radio_dev);
4595 else
4596 video_device_release(btv->radio_dev);
4597 btv->radio_dev = NULL;
4598 }
4599}
4600
4601/* register video4linux devices */
4602static int __devinit bttv_register_video(struct bttv *btv)
4603{
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004604 if (no_overlay <= 0) {
4605 bttv_video_template.type |= VID_TYPE_OVERLAY;
4606 } else {
4607 printk("bttv: Overlay support disabled.\n");
4608 }
4609
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 /* video */
4611 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004612 if (NULL == btv->video_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 goto err;
4614 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
4615 goto err;
4616 printk(KERN_INFO "bttv%d: registered device video%d\n",
4617 btv->c.nr,btv->video_dev->minor & 0x1f);
Trent Piephod94fc9a2006-07-29 17:18:06 -03004618 if (class_device_create_file(&btv->video_dev->class_dev,
4619 &class_device_attr_card)<0) {
4620 printk(KERN_ERR "bttv%d: class_device_create_file 'card' "
4621 "failed\n", btv->c.nr);
4622 goto err;
4623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
4625 /* vbi */
4626 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004627 if (NULL == btv->vbi_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 goto err;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004629 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 goto err;
4631 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
4632 btv->c.nr,btv->vbi_dev->minor & 0x1f);
4633
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004634 if (!btv->has_radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 return 0;
4636 /* radio */
4637 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004638 if (NULL == btv->radio_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 goto err;
4640 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
4641 goto err;
4642 printk(KERN_INFO "bttv%d: registered device radio%d\n",
4643 btv->c.nr,btv->radio_dev->minor & 0x1f);
4644
4645 /* all done */
4646 return 0;
4647
4648 err:
4649 bttv_unregister_video(btv);
4650 return -1;
4651}
4652
4653
4654/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4655/* response on cards with no firmware is not enabled by OF */
4656static void pci_set_command(struct pci_dev *dev)
4657{
4658#if defined(__powerpc__)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004659 unsigned int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004661 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4662 cmd = (cmd | PCI_COMMAND_MEMORY );
4663 pci_write_config_dword(dev, PCI_COMMAND, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664#endif
4665}
4666
4667static int __devinit bttv_probe(struct pci_dev *dev,
4668 const struct pci_device_id *pci_id)
4669{
4670 int result;
4671 unsigned char lat;
4672 struct bttv *btv;
4673
4674 if (bttv_num == BTTV_MAX)
4675 return -ENOMEM;
4676 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004677 btv=&bttvs[bttv_num];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 memset(btv,0,sizeof(*btv));
4679 btv->c.nr = bttv_num;
4680 sprintf(btv->c.name,"bttv%d",btv->c.nr);
4681
4682 /* initialize structs / fill in defaults */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02004683 mutex_init(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004684 spin_lock_init(&btv->s_lock);
4685 spin_lock_init(&btv->gpio_lock);
4686 init_waitqueue_head(&btv->gpioq);
4687 init_waitqueue_head(&btv->i2c_queue);
4688 INIT_LIST_HEAD(&btv->c.subs);
4689 INIT_LIST_HEAD(&btv->capture);
4690 INIT_LIST_HEAD(&btv->vcapture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 v4l2_prio_init(&btv->prio);
4692
4693 init_timer(&btv->timeout);
4694 btv->timeout.function = bttv_irq_timeout;
4695 btv->timeout.data = (unsigned long)btv;
4696
Michael Krufky7c08fb02005-11-08 21:36:21 -08004697 btv->i2c_rc = -1;
4698 btv->tuner_type = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 btv->new_input = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 btv->has_radio=radio[btv->c.nr];
4701
4702 /* pci stuff (init, get irq/mmio, ... */
4703 btv->c.pci = dev;
Michael Krufky7c08fb02005-11-08 21:36:21 -08004704 btv->id = dev->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 if (pci_enable_device(dev)) {
Michael Krufky7c08fb02005-11-08 21:36:21 -08004706 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 btv->c.nr);
4708 return -EIO;
4709 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004710 if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
4711 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 btv->c.nr);
4713 return -EIO;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 if (!request_mem_region(pci_resource_start(dev,0),
4716 pci_resource_len(dev,0),
4717 btv->c.name)) {
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004718 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4719 btv->c.nr,
4720 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 return -EBUSY;
4722 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004723 pci_set_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 pci_set_command(dev);
4725 pci_set_drvdata(dev,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004727 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4728 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4729 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4730 bttv_num,btv->id, btv->revision, pci_name(dev));
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004731 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4732 btv->c.pci->irq, lat,
4733 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 schedule();
4735
Akinobu Mita5f1693f2006-12-20 10:08:56 -03004736 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4737 if (NULL == btv->bt848_mmio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4739 result = -EIO;
4740 goto fail1;
4741 }
4742
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004743 /* identify card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 bttv_idcard(btv);
4745
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004746 /* disable irqs, register irq handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 btwrite(0, BT848_INT_MASK);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004748 result = request_irq(btv->c.pci->irq, bttv_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -07004749 IRQF_SHARED | IRQF_DISABLED,btv->c.name,(void *)btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004750 if (result < 0) {
4751 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 bttv_num,btv->c.pci->irq);
4753 goto fail1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
4756 if (0 != bttv_handle_chipset(btv)) {
4757 result = -EIO;
4758 goto fail2;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
4761 /* init options from insmod args */
4762 btv->opt_combfilter = combfilter;
4763 btv->opt_lumafilter = lumafilter;
4764 btv->opt_automute = automute;
4765 btv->opt_chroma_agc = chroma_agc;
4766 btv->opt_adc_crush = adc_crush;
4767 btv->opt_vcr_hack = vcr_hack;
4768 btv->opt_whitecrush_upper = whitecrush_upper;
4769 btv->opt_whitecrush_lower = whitecrush_lower;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07004770 btv->opt_uv_ratio = uv_ratio;
4771 btv->opt_full_luma_range = full_luma_range;
4772 btv->opt_coring = coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
4774 /* fill struct bttv with some useful defaults */
4775 btv->init.btv = btv;
4776 btv->init.ov.w.width = 320;
4777 btv->init.ov.w.height = 240;
4778 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
4779 btv->init.width = 320;
4780 btv->init.height = 240;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 btv->input = 0;
4782
4783 /* initialize hardware */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004784 if (bttv_gpio)
4785 bttv_gpio_tracking(btv,"pre-init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
4787 bttv_risc_init_main(btv);
4788 init_bt848(btv);
4789
4790 /* gpio */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004791 btwrite(0x00, BT848_GPIO_REG_INP);
4792 btwrite(0x00, BT848_GPIO_OUT_EN);
4793 if (bttv_verbose)
4794 bttv_gpio_tracking(btv,"init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004796 /* needs to be done before i2c is registered */
4797 bttv_init_card1(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004799 /* register i2c + gpio */
4800 init_bttv_i2c(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004802 /* some card-specific stuff (needs working i2c) */
4803 bttv_init_card2(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 init_irqreg(btv);
4805
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004806 /* register video4linux + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 if (!bttv_tvcards[btv->c.type].no_video) {
4808 bttv_register_video(btv);
4809 bt848_bright(btv,32768);
4810 bt848_contrast(btv,32768);
4811 bt848_hue(btv,32768);
4812 bt848_sat(btv,32768);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004813 audio_mute(btv, 1);
Trent Piepho333408f2007-07-03 15:08:10 -03004814 set_input(btv, 0, btv->tvnorm);
Michael Schimeke5bd0262007-01-18 16:17:39 -03004815 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4816 btv->crop[1] = btv->crop[0]; /* current = default */
4817 disclaim_vbi_lines(btv);
4818 disclaim_video_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 }
4820
Jarod Wilsonf992a492007-03-24 15:23:50 -03004821 /* add subdevices and autoload dvb-bt8xx if needed */
4822 if (bttv_tvcards[btv->c.type].has_dvb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 bttv_sub_add_device(&btv->c, "dvb");
Jarod Wilsonf992a492007-03-24 15:23:50 -03004824 request_modules(btv);
4825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004827 bttv_input_init(btv);
4828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 /* everything is fine */
4830 bttv_num++;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
4833 fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004834 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
4836 fail1:
4837 if (btv->bt848_mmio)
4838 iounmap(btv->bt848_mmio);
4839 release_mem_region(pci_resource_start(btv->c.pci,0),
4840 pci_resource_len(btv->c.pci,0));
4841 pci_set_drvdata(dev,NULL);
4842 return result;
4843}
4844
4845static void __devexit bttv_remove(struct pci_dev *pci_dev)
4846{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004847 struct bttv *btv = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
4849 if (bttv_verbose)
4850 printk("bttv%d: unloading\n",btv->c.nr);
4851
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004852 /* shutdown everything (DMA+IRQs) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 btand(~15, BT848_GPIO_DMA_CTL);
4854 btwrite(0, BT848_INT_MASK);
4855 btwrite(~0x0, BT848_INT_STAT);
4856 btwrite(0x0, BT848_GPIO_OUT_EN);
4857 if (bttv_gpio)
4858 bttv_gpio_tracking(btv,"cleanup");
4859
4860 /* tell gpio modules we are leaving ... */
4861 btv->shutdown=1;
4862 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004863 bttv_input_fini(btv);
Christopher Pascoe889aee82006-01-09 15:25:28 -02004864 bttv_sub_del_devices(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004866 /* unregister i2c_bus + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 fini_bttv_i2c(btv);
4868
4869 /* unregister video4linux */
4870 bttv_unregister_video(btv);
4871
4872 /* free allocated memory */
4873 btcx_riscmem_free(btv->c.pci,&btv->main);
4874
4875 /* free ressources */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004876 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 iounmap(btv->bt848_mmio);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004878 release_mem_region(pci_resource_start(btv->c.pci,0),
4879 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
4881 pci_set_drvdata(pci_dev, NULL);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004882 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883}
4884
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004885#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4887{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004888 struct bttv *btv = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 struct bttv_buffer_set idle;
4890 unsigned long flags;
4891
Mauro Carvalho Chehab0f97a932005-09-09 13:04:05 -07004892 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
4894 /* stop dma + irqs */
4895 spin_lock_irqsave(&btv->s_lock,flags);
4896 memset(&idle, 0, sizeof(idle));
4897 btv->state.video = btv->curr;
4898 btv->state.vbi = btv->cvbi;
4899 btv->state.loop_irq = btv->loop_irq;
4900 btv->curr = idle;
4901 btv->loop_irq = 0;
4902 bttv_buffer_activate_video(btv, &idle);
4903 bttv_buffer_activate_vbi(btv, NULL);
4904 bttv_set_dma(btv, 0);
4905 btwrite(0, BT848_INT_MASK);
4906 spin_unlock_irqrestore(&btv->s_lock,flags);
4907
4908 /* save bt878 state */
4909 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4910 btv->state.gpio_data = gpio_read();
4911
4912 /* save pci state */
4913 pci_save_state(pci_dev);
4914 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4915 pci_disable_device(pci_dev);
4916 btv->state.disabled = 1;
4917 }
4918 return 0;
4919}
4920
4921static int bttv_resume(struct pci_dev *pci_dev)
4922{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004923 struct bttv *btv = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004925 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
4927 dprintk("bttv%d: resume\n", btv->c.nr);
4928
4929 /* restore pci state */
4930 if (btv->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004931 err=pci_enable_device(pci_dev);
4932 if (err) {
4933 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4934 btv->c.nr);
4935 return err;
4936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 btv->state.disabled = 0;
4938 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004939 err=pci_set_power_state(pci_dev, PCI_D0);
4940 if (err) {
4941 pci_disable_device(pci_dev);
4942 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4943 btv->c.nr);
4944 btv->state.disabled = 1;
4945 return err;
4946 }
4947
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 pci_restore_state(pci_dev);
4949
4950 /* restore bt878 state */
4951 bttv_reinit_bt848(btv);
4952 gpio_inout(0xffffff, btv->state.gpio_enable);
4953 gpio_write(btv->state.gpio_data);
4954
4955 /* restart dma */
4956 spin_lock_irqsave(&btv->s_lock,flags);
4957 btv->curr = btv->state.video;
4958 btv->cvbi = btv->state.vbi;
4959 btv->loop_irq = btv->state.loop_irq;
4960 bttv_buffer_activate_video(btv, &btv->curr);
4961 bttv_buffer_activate_vbi(btv, btv->cvbi);
4962 bttv_set_dma(btv, 0);
4963 spin_unlock_irqrestore(&btv->s_lock,flags);
4964 return 0;
4965}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004966#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
4968static struct pci_device_id bttv_pci_tbl[] = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004969 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4970 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004972 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004974 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004976 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4977 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978};
4979
4980MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4981
4982static struct pci_driver bttv_pci_driver = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004983 .name = "bttv",
4984 .id_table = bttv_pci_tbl,
4985 .probe = bttv_probe,
4986 .remove = __devexit_p(bttv_remove),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004987#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 .suspend = bttv_suspend,
4989 .resume = bttv_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004990#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991};
4992
4993static int bttv_init_module(void)
4994{
Randy Dunlapc526e222006-07-15 09:08:26 -03004995 int ret;
4996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 bttv_num = 0;
4998
4999 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
5000 (BTTV_VERSION_CODE >> 16) & 0xff,
5001 (BTTV_VERSION_CODE >> 8) & 0xff,
5002 BTTV_VERSION_CODE & 0xff);
5003#ifdef SNAPSHOT
5004 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
5005 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
5006#endif
5007 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
5008 gbuffers = 2;
5009 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
5010 gbufsize = BTTV_MAX_FBUF;
5011 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
5012 if (bttv_verbose)
5013 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
5014 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
5015
5016 bttv_check_chipset();
5017
Randy Dunlapc526e222006-07-15 09:08:26 -03005018 ret = bus_register(&bttv_sub_bus_type);
5019 if (ret < 0) {
5020 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
5021 return ret;
5022 }
Otavio Salvador23047592006-01-09 15:25:17 -02005023 return pci_register_driver(&bttv_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024}
5025
5026static void bttv_cleanup_module(void)
5027{
5028 pci_unregister_driver(&bttv_pci_driver);
5029 bus_unregister(&bttv_sub_bus_type);
5030 return;
5031}
5032
5033module_init(bttv_init_module);
5034module_exit(bttv_cleanup_module);
5035
5036/*
5037 * Local variables:
5038 * c-basic-offset: 8
5039 * End:
5040 */