blob: 284bdf2d2432b7d0840ea9d2f711ddba1d5a211a [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>
33#include <linux/moduleparam.h>
34#include <linux/delay.h>
35#include <linux/errno.h>
36#include <linux/fs.h>
37#include <linux/kernel.h>
38#include <linux/sched.h>
39#include <linux/interrupt.h>
40#include <linux/kdev_t.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020041#include "bttvp.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020042#include <media/v4l2-common.h>
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -030043#include <media/tvaudio.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030044#include <media/msp3400.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020045
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -070046#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <asm/io.h>
49#include <asm/byteorder.h>
50
Mauro Carvalho Chehabfa3fcce2006-03-23 21:45:24 -030051#include <media/rds.h>
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -070052
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054unsigned int bttv_num; /* number of Bt848s in use */
55struct bttv bttvs[BTTV_MAX];
56
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020057unsigned int bttv_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058unsigned int bttv_verbose = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020059unsigned int bttv_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/* config variables */
62#ifdef __BIG_ENDIAN
63static unsigned int bigendian=1;
64#else
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020065static unsigned int bigendian;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#endif
67static unsigned int radio[BTTV_MAX];
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020068static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static unsigned int gbuffers = 8;
70static unsigned int gbufsize = 0x208000;
Michael Schimeke5bd0262007-01-18 16:17:39 -030071static unsigned int reset_crop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73static int video_nr = -1;
74static int radio_nr = -1;
75static int vbi_nr = -1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020076static int debug_latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020078static unsigned int fdsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* options */
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020081static unsigned int combfilter;
82static unsigned int lumafilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static unsigned int automute = 1;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020084static unsigned int chroma_agc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static unsigned int adc_crush = 1;
86static unsigned int whitecrush_upper = 0xCF;
87static unsigned int whitecrush_lower = 0x7F;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020088static unsigned int vcr_hack;
89static unsigned int irq_iswitch;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -070090static unsigned int uv_ratio = 50;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020091static unsigned int full_luma_range;
92static unsigned int coring;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -070093extern int no_overlay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/* API features (turn on/off stuff for testing) */
96static unsigned int v4l2 = 1;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* insmod args */
99module_param(bttv_verbose, int, 0644);
100module_param(bttv_gpio, int, 0644);
101module_param(bttv_debug, int, 0644);
102module_param(irq_debug, int, 0644);
103module_param(debug_latency, int, 0644);
104
105module_param(fdsr, int, 0444);
106module_param(video_nr, int, 0444);
107module_param(radio_nr, int, 0444);
108module_param(vbi_nr, int, 0444);
109module_param(gbuffers, int, 0444);
110module_param(gbufsize, int, 0444);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300111module_param(reset_crop, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113module_param(v4l2, int, 0644);
114module_param(bigendian, int, 0644);
115module_param(irq_iswitch, int, 0644);
116module_param(combfilter, int, 0444);
117module_param(lumafilter, int, 0444);
118module_param(automute, int, 0444);
119module_param(chroma_agc, int, 0444);
120module_param(adc_crush, int, 0444);
121module_param(whitecrush_upper, int, 0444);
122module_param(whitecrush_lower, int, 0444);
123module_param(vcr_hack, int, 0444);
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700124module_param(uv_ratio, int, 0444);
125module_param(full_luma_range, int, 0444);
126module_param(coring, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128module_param_array(radio, int, NULL, 0444);
129
130MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
131MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
132MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
133MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
134MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
135MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
136MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
137MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
Michael Schimeke5bd0262007-01-18 16:17:39 -0300138MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
139 "is 1 (yes) for compatibility with older applications");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
141MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
142MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
143MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
144MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
145MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
146MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700147MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
148MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
149MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
152MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
153MODULE_LICENSE("GPL");
154
155/* ----------------------------------------------------------------------- */
156/* sysfs */
157
158static ssize_t show_card(struct class_device *cd, char *buf)
159{
160 struct video_device *vfd = to_video_device(cd);
161 struct bttv *btv = dev_get_drvdata(vfd->dev);
162 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
163}
164static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
165
166/* ----------------------------------------------------------------------- */
Jarod Wilsonf992a492007-03-24 15:23:50 -0300167/* dvb auto-load setup */
168#if defined(CONFIG_MODULES) && defined(MODULE)
169static void request_module_async(struct work_struct *work)
170{
171 request_module("dvb-bt8xx");
172}
173
174static void request_modules(struct bttv *dev)
175{
176 INIT_WORK(&dev->request_module_wk, request_module_async);
177 schedule_work(&dev->request_module_wk);
178}
179#else
180#define request_modules(dev)
181#endif /* CONFIG_MODULES */
182
183
184/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* static data */
186
187/* special timing tables from conexant... */
188static u8 SRAM_Table[][60] =
189{
190 /* PAL digital input over GPIO[7:0] */
191 {
192 45, // 45 bytes following
193 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
194 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
195 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
196 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
197 0x37,0x00,0xAF,0x21,0x00
198 },
199 /* NTSC digital input over GPIO[7:0] */
200 {
201 51, // 51 bytes following
202 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
203 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
204 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
205 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
206 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
207 0x00,
208 },
209 // TGB_NTSC392 // quartzsight
210 // This table has been modified to be used for Fusion Rev D
211 {
212 0x2A, // size of table = 42
213 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
214 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
215 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
216 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
217 0x20, 0x00
218 }
219};
220
Michael Schimeke5bd0262007-01-18 16:17:39 -0300221/* minhdelayx1 first video pixel we can capture on a line and
222 hdelayx1 start of active video, both relative to rising edge of
223 /HRESET pulse (0H) in 1 / fCLKx1.
224 swidth width of active video and
225 totalwidth total line width, both in 1 / fCLKx1.
226 sqwidth total line width in square pixels.
227 vdelay start of active video in 2 * field lines relative to
228 trailing edge of /VRESET pulse (VDELAY register).
229 sheight height of active video in 2 * field lines.
230 videostart0 ITU-R frame line number of the line corresponding
231 to vdelay in the first field. */
232#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
233 vdelay, sheight, videostart0) \
234 .cropcap.bounds.left = minhdelayx1, \
235 /* * 2 because vertically we count field lines times two, */ \
236 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
237 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
238 /* 4 is a safety margin at the end of the line. */ \
239 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
240 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
241 .cropcap.defrect.left = hdelayx1, \
242 .cropcap.defrect.top = (videostart0) * 2, \
243 .cropcap.defrect.width = swidth, \
244 .cropcap.defrect.height = sheight, \
245 .cropcap.pixelaspect.numerator = totalwidth, \
246 .cropcap.pixelaspect.denominator = sqwidth,
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248const struct bttv_tvnorm bttv_tvnorms[] = {
249 /* PAL-BDGHI */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800250 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
251 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 {
253 .v4l2_id = V4L2_STD_PAL,
254 .name = "PAL",
255 .Fsc = 35468950,
256 .swidth = 924,
257 .sheight = 576,
258 .totalwidth = 1135,
259 .adelay = 0x7f,
260 .bdelay = 0x72,
261 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
262 .scaledtwidth = 1135,
263 .hdelayx1 = 186,
264 .hactivex1 = 924,
265 .vdelay = 0x20,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300266 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 .sram = 0,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200268 /* ITU-R frame line number of the first VBI line
Michael Schimeke5bd0262007-01-18 16:17:39 -0300269 we can capture, of the first and second field.
270 The last line is determined by cropcap.bounds. */
271 .vbistart = { 7, 320 },
272 CROPCAP(/* minhdelayx1 */ 68,
273 /* hdelayx1 */ 186,
274 /* Should be (768 * 1135 + 944 / 2) / 944.
275 cropcap.defrect is used for image width
276 checks, so we keep the old value 924. */
277 /* swidth */ 924,
278 /* totalwidth */ 1135,
279 /* sqwidth */ 944,
280 /* vdelay */ 0x20,
281 /* sheight */ 576,
282 /* videostart0 */ 23)
283 /* bt878 (and bt848?) can capture another
284 line below active video. */
285 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 },{
Hans Verkuild97a11e2006-02-07 06:48:40 -0200287 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 .name = "NTSC",
289 .Fsc = 28636363,
290 .swidth = 768,
291 .sheight = 480,
292 .totalwidth = 910,
293 .adelay = 0x68,
294 .bdelay = 0x5d,
295 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
296 .scaledtwidth = 910,
297 .hdelayx1 = 128,
298 .hactivex1 = 910,
299 .vdelay = 0x1a,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300300 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 .sram = 1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200302 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300303 CROPCAP(/* minhdelayx1 */ 68,
304 /* hdelayx1 */ 128,
305 /* Should be (640 * 910 + 780 / 2) / 780? */
306 /* swidth */ 768,
307 /* totalwidth */ 910,
308 /* sqwidth */ 780,
309 /* vdelay */ 0x1a,
310 /* sheight */ 480,
311 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 },{
313 .v4l2_id = V4L2_STD_SECAM,
314 .name = "SECAM",
315 .Fsc = 35468950,
316 .swidth = 924,
317 .sheight = 576,
318 .totalwidth = 1135,
319 .adelay = 0x7f,
320 .bdelay = 0xb0,
321 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
322 .scaledtwidth = 1135,
323 .hdelayx1 = 186,
324 .hactivex1 = 922,
325 .vdelay = 0x20,
326 .vbipack = 255,
327 .sram = 0, /* like PAL, correct? */
Michael H. Schimek67f15702006-01-09 15:25:27 -0200328 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300329 CROPCAP(/* minhdelayx1 */ 68,
330 /* hdelayx1 */ 186,
331 /* swidth */ 924,
332 /* totalwidth */ 1135,
333 /* sqwidth */ 944,
334 /* vdelay */ 0x20,
335 /* sheight */ 576,
336 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 },{
338 .v4l2_id = V4L2_STD_PAL_Nc,
339 .name = "PAL-Nc",
340 .Fsc = 28636363,
341 .swidth = 640,
342 .sheight = 576,
343 .totalwidth = 910,
344 .adelay = 0x68,
345 .bdelay = 0x5d,
346 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
347 .scaledtwidth = 780,
348 .hdelayx1 = 130,
349 .hactivex1 = 734,
350 .vdelay = 0x1a,
351 .vbipack = 144,
352 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200353 .vbistart = { 7, 320 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300354 CROPCAP(/* minhdelayx1 */ 68,
355 /* hdelayx1 */ 130,
356 /* swidth */ (640 * 910 + 780 / 2) / 780,
357 /* totalwidth */ 910,
358 /* sqwidth */ 780,
359 /* vdelay */ 0x1a,
360 /* sheight */ 576,
361 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 },{
363 .v4l2_id = V4L2_STD_PAL_M,
364 .name = "PAL-M",
365 .Fsc = 28636363,
366 .swidth = 640,
367 .sheight = 480,
368 .totalwidth = 910,
369 .adelay = 0x68,
370 .bdelay = 0x5d,
371 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
372 .scaledtwidth = 780,
373 .hdelayx1 = 135,
374 .hactivex1 = 754,
375 .vdelay = 0x1a,
376 .vbipack = 144,
377 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200378 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300379 CROPCAP(/* minhdelayx1 */ 68,
380 /* hdelayx1 */ 135,
381 /* swidth */ (640 * 910 + 780 / 2) / 780,
382 /* totalwidth */ 910,
383 /* sqwidth */ 780,
384 /* vdelay */ 0x1a,
385 /* sheight */ 480,
386 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 },{
388 .v4l2_id = V4L2_STD_PAL_N,
389 .name = "PAL-N",
390 .Fsc = 35468950,
391 .swidth = 768,
392 .sheight = 576,
393 .totalwidth = 1135,
394 .adelay = 0x7f,
395 .bdelay = 0x72,
396 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
397 .scaledtwidth = 944,
398 .hdelayx1 = 186,
399 .hactivex1 = 922,
400 .vdelay = 0x20,
401 .vbipack = 144,
402 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300403 .vbistart = { 7, 320 },
404 CROPCAP(/* minhdelayx1 */ 68,
405 /* hdelayx1 */ 186,
406 /* swidth */ (768 * 1135 + 944 / 2) / 944,
407 /* totalwidth */ 1135,
408 /* sqwidth */ 944,
409 /* vdelay */ 0x20,
410 /* sheight */ 576,
411 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 },{
413 .v4l2_id = V4L2_STD_NTSC_M_JP,
414 .name = "NTSC-JP",
415 .Fsc = 28636363,
416 .swidth = 640,
417 .sheight = 480,
418 .totalwidth = 910,
419 .adelay = 0x68,
420 .bdelay = 0x5d,
421 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
422 .scaledtwidth = 780,
423 .hdelayx1 = 135,
424 .hactivex1 = 754,
425 .vdelay = 0x16,
426 .vbipack = 144,
427 .sram = -1,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300428 .vbistart = { 10, 273 },
429 CROPCAP(/* minhdelayx1 */ 68,
430 /* hdelayx1 */ 135,
431 /* swidth */ (640 * 910 + 780 / 2) / 780,
432 /* totalwidth */ 910,
433 /* sqwidth */ 780,
434 /* vdelay */ 0x16,
435 /* sheight */ 480,
436 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 },{
438 /* that one hopefully works with the strange timing
439 * which video recorders produce when playing a NTSC
440 * tape on a PAL TV ... */
441 .v4l2_id = V4L2_STD_PAL_60,
442 .name = "PAL-60",
443 .Fsc = 35468950,
444 .swidth = 924,
445 .sheight = 480,
446 .totalwidth = 1135,
447 .adelay = 0x7f,
448 .bdelay = 0x72,
449 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
450 .scaledtwidth = 1135,
451 .hdelayx1 = 186,
452 .hactivex1 = 924,
453 .vdelay = 0x1a,
454 .vbipack = 255,
455 .vtotal = 524,
456 .sram = -1,
Michael H. Schimek67f15702006-01-09 15:25:27 -0200457 .vbistart = { 10, 273 },
Michael Schimeke5bd0262007-01-18 16:17:39 -0300458 CROPCAP(/* minhdelayx1 */ 68,
459 /* hdelayx1 */ 186,
460 /* swidth */ 924,
461 /* totalwidth */ 1135,
462 /* sqwidth */ 944,
463 /* vdelay */ 0x1a,
464 /* sheight */ 480,
465 /* videostart0 */ 23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467};
468static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
469
470/* ----------------------------------------------------------------------- */
471/* bttv format list
472 packed pixel formats must come first */
473static const struct bttv_format bttv_formats[] = {
474 {
475 .name = "8 bpp, gray",
476 .palette = VIDEO_PALETTE_GREY,
477 .fourcc = V4L2_PIX_FMT_GREY,
478 .btformat = BT848_COLOR_FMT_Y8,
479 .depth = 8,
480 .flags = FORMAT_FLAGS_PACKED,
481 },{
482 .name = "8 bpp, dithered color",
483 .palette = VIDEO_PALETTE_HI240,
484 .fourcc = V4L2_PIX_FMT_HI240,
485 .btformat = BT848_COLOR_FMT_RGB8,
486 .depth = 8,
487 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
488 },{
489 .name = "15 bpp RGB, le",
490 .palette = VIDEO_PALETTE_RGB555,
491 .fourcc = V4L2_PIX_FMT_RGB555,
492 .btformat = BT848_COLOR_FMT_RGB15,
493 .depth = 16,
494 .flags = FORMAT_FLAGS_PACKED,
495 },{
496 .name = "15 bpp RGB, be",
497 .palette = -1,
498 .fourcc = V4L2_PIX_FMT_RGB555X,
499 .btformat = BT848_COLOR_FMT_RGB15,
500 .btswap = 0x03, /* byteswap */
501 .depth = 16,
502 .flags = FORMAT_FLAGS_PACKED,
503 },{
504 .name = "16 bpp RGB, le",
505 .palette = VIDEO_PALETTE_RGB565,
506 .fourcc = V4L2_PIX_FMT_RGB565,
507 .btformat = BT848_COLOR_FMT_RGB16,
508 .depth = 16,
509 .flags = FORMAT_FLAGS_PACKED,
510 },{
511 .name = "16 bpp RGB, be",
512 .palette = -1,
513 .fourcc = V4L2_PIX_FMT_RGB565X,
514 .btformat = BT848_COLOR_FMT_RGB16,
515 .btswap = 0x03, /* byteswap */
516 .depth = 16,
517 .flags = FORMAT_FLAGS_PACKED,
518 },{
519 .name = "24 bpp RGB, le",
520 .palette = VIDEO_PALETTE_RGB24,
521 .fourcc = V4L2_PIX_FMT_BGR24,
522 .btformat = BT848_COLOR_FMT_RGB24,
523 .depth = 24,
524 .flags = FORMAT_FLAGS_PACKED,
525 },{
526 .name = "32 bpp RGB, le",
527 .palette = VIDEO_PALETTE_RGB32,
528 .fourcc = V4L2_PIX_FMT_BGR32,
529 .btformat = BT848_COLOR_FMT_RGB32,
530 .depth = 32,
531 .flags = FORMAT_FLAGS_PACKED,
532 },{
533 .name = "32 bpp RGB, be",
534 .palette = -1,
535 .fourcc = V4L2_PIX_FMT_RGB32,
536 .btformat = BT848_COLOR_FMT_RGB32,
537 .btswap = 0x0f, /* byte+word swap */
538 .depth = 32,
539 .flags = FORMAT_FLAGS_PACKED,
540 },{
541 .name = "4:2:2, packed, YUYV",
542 .palette = VIDEO_PALETTE_YUV422,
543 .fourcc = V4L2_PIX_FMT_YUYV,
544 .btformat = BT848_COLOR_FMT_YUY2,
545 .depth = 16,
546 .flags = FORMAT_FLAGS_PACKED,
547 },{
548 .name = "4:2:2, packed, YUYV",
549 .palette = VIDEO_PALETTE_YUYV,
550 .fourcc = V4L2_PIX_FMT_YUYV,
551 .btformat = BT848_COLOR_FMT_YUY2,
552 .depth = 16,
553 .flags = FORMAT_FLAGS_PACKED,
554 },{
555 .name = "4:2:2, packed, UYVY",
556 .palette = VIDEO_PALETTE_UYVY,
557 .fourcc = V4L2_PIX_FMT_UYVY,
558 .btformat = BT848_COLOR_FMT_YUY2,
559 .btswap = 0x03, /* byteswap */
560 .depth = 16,
561 .flags = FORMAT_FLAGS_PACKED,
562 },{
563 .name = "4:2:2, planar, Y-Cb-Cr",
564 .palette = VIDEO_PALETTE_YUV422P,
565 .fourcc = V4L2_PIX_FMT_YUV422P,
566 .btformat = BT848_COLOR_FMT_YCrCb422,
567 .depth = 16,
568 .flags = FORMAT_FLAGS_PLANAR,
569 .hshift = 1,
570 .vshift = 0,
571 },{
572 .name = "4:2:0, planar, Y-Cb-Cr",
573 .palette = VIDEO_PALETTE_YUV420P,
574 .fourcc = V4L2_PIX_FMT_YUV420,
575 .btformat = BT848_COLOR_FMT_YCrCb422,
576 .depth = 12,
577 .flags = FORMAT_FLAGS_PLANAR,
578 .hshift = 1,
579 .vshift = 1,
580 },{
581 .name = "4:2:0, planar, Y-Cr-Cb",
582 .palette = -1,
583 .fourcc = V4L2_PIX_FMT_YVU420,
584 .btformat = BT848_COLOR_FMT_YCrCb422,
585 .depth = 12,
586 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
587 .hshift = 1,
588 .vshift = 1,
589 },{
590 .name = "4:1:1, planar, Y-Cb-Cr",
591 .palette = VIDEO_PALETTE_YUV411P,
592 .fourcc = V4L2_PIX_FMT_YUV411P,
593 .btformat = BT848_COLOR_FMT_YCrCb411,
594 .depth = 12,
595 .flags = FORMAT_FLAGS_PLANAR,
596 .hshift = 2,
597 .vshift = 0,
598 },{
599 .name = "4:1:0, planar, Y-Cb-Cr",
600 .palette = VIDEO_PALETTE_YUV410P,
601 .fourcc = V4L2_PIX_FMT_YUV410,
602 .btformat = BT848_COLOR_FMT_YCrCb411,
603 .depth = 9,
604 .flags = FORMAT_FLAGS_PLANAR,
605 .hshift = 2,
606 .vshift = 2,
607 },{
608 .name = "4:1:0, planar, Y-Cr-Cb",
609 .palette = -1,
610 .fourcc = V4L2_PIX_FMT_YVU410,
611 .btformat = BT848_COLOR_FMT_YCrCb411,
612 .depth = 9,
613 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
614 .hshift = 2,
615 .vshift = 2,
616 },{
617 .name = "raw scanlines",
618 .palette = VIDEO_PALETTE_RAW,
619 .fourcc = -1,
620 .btformat = BT848_COLOR_FMT_RAW,
621 .depth = 8,
622 .flags = FORMAT_FLAGS_RAW,
623 }
624};
625static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
626
627/* ----------------------------------------------------------------------- */
628
629#define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
630#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
631#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
632#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
633#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
634#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
635#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
636#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700637#define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
638#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
639#define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
640#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642static const struct v4l2_queryctrl no_ctl = {
643 .name = "42",
644 .flags = V4L2_CTRL_FLAG_DISABLED,
645};
646static const struct v4l2_queryctrl bttv_ctls[] = {
647 /* --- video --- */
648 {
649 .id = V4L2_CID_BRIGHTNESS,
650 .name = "Brightness",
651 .minimum = 0,
652 .maximum = 65535,
653 .step = 256,
654 .default_value = 32768,
655 .type = V4L2_CTRL_TYPE_INTEGER,
656 },{
657 .id = V4L2_CID_CONTRAST,
658 .name = "Contrast",
659 .minimum = 0,
660 .maximum = 65535,
661 .step = 128,
662 .default_value = 32768,
663 .type = V4L2_CTRL_TYPE_INTEGER,
664 },{
665 .id = V4L2_CID_SATURATION,
666 .name = "Saturation",
667 .minimum = 0,
668 .maximum = 65535,
669 .step = 128,
670 .default_value = 32768,
671 .type = V4L2_CTRL_TYPE_INTEGER,
672 },{
673 .id = V4L2_CID_HUE,
674 .name = "Hue",
675 .minimum = 0,
676 .maximum = 65535,
677 .step = 256,
678 .default_value = 32768,
679 .type = V4L2_CTRL_TYPE_INTEGER,
680 },
681 /* --- audio --- */
682 {
683 .id = V4L2_CID_AUDIO_MUTE,
684 .name = "Mute",
685 .minimum = 0,
686 .maximum = 1,
687 .type = V4L2_CTRL_TYPE_BOOLEAN,
688 },{
689 .id = V4L2_CID_AUDIO_VOLUME,
690 .name = "Volume",
691 .minimum = 0,
692 .maximum = 65535,
693 .step = 65535/100,
694 .default_value = 65535,
695 .type = V4L2_CTRL_TYPE_INTEGER,
696 },{
697 .id = V4L2_CID_AUDIO_BALANCE,
698 .name = "Balance",
699 .minimum = 0,
700 .maximum = 65535,
701 .step = 65535/100,
702 .default_value = 32768,
703 .type = V4L2_CTRL_TYPE_INTEGER,
704 },{
705 .id = V4L2_CID_AUDIO_BASS,
706 .name = "Bass",
707 .minimum = 0,
708 .maximum = 65535,
709 .step = 65535/100,
710 .default_value = 32768,
711 .type = V4L2_CTRL_TYPE_INTEGER,
712 },{
713 .id = V4L2_CID_AUDIO_TREBLE,
714 .name = "Treble",
715 .minimum = 0,
716 .maximum = 65535,
717 .step = 65535/100,
718 .default_value = 32768,
719 .type = V4L2_CTRL_TYPE_INTEGER,
720 },
721 /* --- private --- */
722 {
723 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
724 .name = "chroma agc",
725 .minimum = 0,
726 .maximum = 1,
727 .type = V4L2_CTRL_TYPE_BOOLEAN,
728 },{
729 .id = V4L2_CID_PRIVATE_COMBFILTER,
730 .name = "combfilter",
731 .minimum = 0,
732 .maximum = 1,
733 .type = V4L2_CTRL_TYPE_BOOLEAN,
734 },{
735 .id = V4L2_CID_PRIVATE_AUTOMUTE,
736 .name = "automute",
737 .minimum = 0,
738 .maximum = 1,
739 .type = V4L2_CTRL_TYPE_BOOLEAN,
740 },{
741 .id = V4L2_CID_PRIVATE_LUMAFILTER,
742 .name = "luma decimation filter",
743 .minimum = 0,
744 .maximum = 1,
745 .type = V4L2_CTRL_TYPE_BOOLEAN,
746 },{
747 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
748 .name = "agc crush",
749 .minimum = 0,
750 .maximum = 1,
751 .type = V4L2_CTRL_TYPE_BOOLEAN,
752 },{
753 .id = V4L2_CID_PRIVATE_VCR_HACK,
754 .name = "vcr hack",
755 .minimum = 0,
756 .maximum = 1,
757 .type = V4L2_CTRL_TYPE_BOOLEAN,
758 },{
759 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
760 .name = "whitecrush upper",
761 .minimum = 0,
762 .maximum = 255,
763 .step = 1,
764 .default_value = 0xCF,
765 .type = V4L2_CTRL_TYPE_INTEGER,
766 },{
767 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
768 .name = "whitecrush lower",
769 .minimum = 0,
770 .maximum = 255,
771 .step = 1,
772 .default_value = 0x7F,
773 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700774 },{
775 .id = V4L2_CID_PRIVATE_UV_RATIO,
776 .name = "uv ratio",
777 .minimum = 0,
778 .maximum = 100,
779 .step = 1,
780 .default_value = 50,
781 .type = V4L2_CTRL_TYPE_INTEGER,
782 },{
783 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
784 .name = "full luma range",
785 .minimum = 0,
786 .maximum = 1,
787 .type = V4L2_CTRL_TYPE_BOOLEAN,
788 },{
789 .id = V4L2_CID_PRIVATE_CORING,
790 .name = "coring",
791 .minimum = 0,
792 .maximum = 3,
793 .step = 1,
794 .default_value = 0,
795 .type = V4L2_CTRL_TYPE_INTEGER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -0700798
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800};
801static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
802
803/* ----------------------------------------------------------------------- */
804/* resource management */
805
Michael Schimeke5bd0262007-01-18 16:17:39 -0300806/*
807 RESOURCE_ allocated by freed by
808
809 VIDEO_READ bttv_read 1) bttv_read 2)
810
811 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
812 VIDIOC_QBUF 1) bttv_release
813 VIDIOCMCAPTURE 1)
814
815 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
816 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
817 3) bttv_release
818
819 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
820 VIDIOC_QBUF 1) bttv_release
821 bttv_read, bttv_poll 1) 4)
822
823 1) The resource must be allocated when we enter buffer prepare functions
824 and remain allocated while buffers are in the DMA queue.
825 2) This is a single frame read.
826 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
827 RESOURCE_OVERLAY is allocated.
828 4) This is a continuous read, implies VIDIOC_STREAMON.
829
830 Note this driver permits video input and standard changes regardless if
831 resources are allocated.
832*/
833
834#define VBI_RESOURCES (RESOURCE_VBI)
835#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
836 RESOURCE_VIDEO_STREAM | \
837 RESOURCE_OVERLAY)
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839static
840int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
841{
Michael Schimeke5bd0262007-01-18 16:17:39 -0300842 int xbits; /* mutual exclusive resources */
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (fh->resources & bit)
845 /* have it already allocated */
846 return 1;
847
Michael Schimeke5bd0262007-01-18 16:17:39 -0300848 xbits = bit;
849 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
850 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* is it free? */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300853 mutex_lock(&btv->lock);
854 if (btv->resources & xbits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /* no, someone else uses it */
Michael Schimeke5bd0262007-01-18 16:17:39 -0300856 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300858
859 if ((bit & VIDEO_RESOURCES)
860 && 0 == (btv->resources & VIDEO_RESOURCES)) {
861 /* Do crop - use current, don't - use default parameters. */
862 __s32 top = btv->crop[!!fh->do_crop].rect.top;
863
864 if (btv->vbi_end > top)
865 goto fail;
866
867 /* We cannot capture the same line as video and VBI data.
868 Claim scan lines crop[].rect.top to bottom. */
869 btv->crop_start = top;
870 } else if (bit & VBI_RESOURCES) {
871 __s32 end = fh->vbi_fmt.end;
872
873 if (end > btv->crop_start)
874 goto fail;
875
876 /* Claim scan lines above fh->vbi_fmt.end. */
877 btv->vbi_end = end;
878 }
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* it's free, grab it */
881 fh->resources |= bit;
882 btv->resources |= bit;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300883 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return 1;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300885
886 fail:
887 mutex_unlock(&btv->lock);
888 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891static
892int check_btres(struct bttv_fh *fh, int bit)
893{
894 return (fh->resources & bit);
895}
896
897static
898int locked_btres(struct bttv *btv, int bit)
899{
900 return (btv->resources & bit);
901}
902
Michael Schimeke5bd0262007-01-18 16:17:39 -0300903/* Call with btv->lock down. */
904static void
905disclaim_vbi_lines(struct bttv *btv)
906{
907 btv->vbi_end = 0;
908}
909
910/* Call with btv->lock down. */
911static void
912disclaim_video_lines(struct bttv *btv)
913{
914 const struct bttv_tvnorm *tvnorm;
915 u8 crop;
916
917 tvnorm = &bttv_tvnorms[btv->tvnorm];
918 btv->crop_start = tvnorm->cropcap.bounds.top
919 + tvnorm->cropcap.bounds.height;
920
921 /* VBI capturing ends at VDELAY, start of video capturing, no
922 matter how many lines the VBI RISC program expects. When video
923 capturing is off, it shall no longer "preempt" VBI capturing,
924 so we set VDELAY to maximum. */
925 crop = btread(BT848_E_CROP) | 0xc0;
926 btwrite(crop, BT848_E_CROP);
927 btwrite(0xfe, BT848_E_VDELAY_LO);
928 btwrite(crop, BT848_O_CROP);
929 btwrite(0xfe, BT848_O_VDELAY_LO);
930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932static
933void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
934{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if ((fh->resources & bits) != bits) {
936 /* trying to free ressources not allocated by us ... */
937 printk("bttv: BUG! (btres)\n");
938 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300939 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 fh->resources &= ~bits;
941 btv->resources &= ~bits;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300942
943 bits = btv->resources;
944
945 if (0 == (bits & VIDEO_RESOURCES))
946 disclaim_video_lines(btv);
947
948 if (0 == (bits & VBI_RESOURCES))
949 disclaim_vbi_lines(btv);
950
951 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
954/* ----------------------------------------------------------------------- */
955/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
956
957/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
958 PLL_X = Reference pre-divider (0=1, 1=2)
959 PLL_C = Post divider (0=6, 1=4)
960 PLL_I = Integer input
961 PLL_F = Fractional input
962
963 F_input = 28.636363 MHz:
964 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
965*/
966
967static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
968{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800969 unsigned char fl, fh, fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800971 /* prevent overflows */
972 fin/=4;
973 fout/=4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800975 fout*=12;
976 fi=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800978 fout=(fout%fin)*256;
979 fh=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800981 fout=(fout%fin)*256;
982 fl=fout/fin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800984 btwrite(fl, BT848_PLL_F_LO);
985 btwrite(fh, BT848_PLL_F_HI);
986 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989static void set_pll(struct bttv *btv)
990{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800991 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800993 if (!btv->pll.pll_crystal)
994 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
997 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800998 return;
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001001 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1002 /* no PLL needed */
1003 if (btv->pll.pll_current == 0)
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001004 return;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001005 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001006 btv->c.nr,btv->pll.pll_ifreq);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001007 btwrite(0x00,BT848_TGCTRL);
1008 btwrite(0x00,BT848_PLL_XCI);
1009 btv->pll.pll_current = 0;
1010 return;
1011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001013 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001014 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1016
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001017 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /* Let other people run while the PLL stabilizes */
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001019 bttv_printk(".");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 msleep(10);
1021
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001022 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 btwrite(0,BT848_DSTATUS);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001024 } else {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001025 btwrite(0x08,BT848_TGCTRL);
1026 btv->pll.pll_current = btv->pll.pll_ofreq;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001027 bttv_printk(" ok\n");
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001028 return;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001029 }
1030 }
1031 btv->pll.pll_current = -1;
Bernd Petrovitsche1e70a22005-09-22 21:43:53 -07001032 bttv_printk("failed\n");
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001033 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036/* used to switch between the bt848's analog/digital video capture modes */
1037static void bt848A_set_timing(struct bttv *btv)
1038{
1039 int i, len;
1040 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1041 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1042
1043 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
1044 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1045 btv->c.nr,table_idx);
1046
1047 /* timing change...reset timing generator address */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001048 btwrite(0x00, BT848_TGCTRL);
1049 btwrite(0x02, BT848_TGCTRL);
1050 btwrite(0x00, BT848_TGCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 len=SRAM_Table[table_idx][0];
1053 for(i = 1; i <= len; i++)
1054 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1055 btv->pll.pll_ofreq = 27000000;
1056
1057 set_pll(btv);
1058 btwrite(0x11, BT848_TGCTRL);
1059 btwrite(0x41, BT848_DVSIF);
1060 } else {
1061 btv->pll.pll_ofreq = fsc;
1062 set_pll(btv);
1063 btwrite(0x0, BT848_DVSIF);
1064 }
1065}
1066
1067/* ----------------------------------------------------------------------- */
1068
1069static void bt848_bright(struct bttv *btv, int bright)
1070{
1071 int value;
1072
1073 // printk("bttv: set bright: %d\n",bright); // DEBUG
1074 btv->bright = bright;
1075
1076 /* We want -128 to 127 we get 0-65535 */
1077 value = (bright >> 8) - 128;
1078 btwrite(value & 0xff, BT848_BRIGHT);
1079}
1080
1081static void bt848_hue(struct bttv *btv, int hue)
1082{
1083 int value;
1084
1085 btv->hue = hue;
1086
1087 /* -128 to 127 */
1088 value = (hue >> 8) - 128;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001089 btwrite(value & 0xff, BT848_HUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092static void bt848_contrast(struct bttv *btv, int cont)
1093{
1094 int value,hibit;
1095
1096 btv->contrast = cont;
1097
1098 /* 0-511 */
1099 value = (cont >> 7);
1100 hibit = (value >> 6) & 4;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001101 btwrite(value & 0xff, BT848_CONTRAST_LO);
1102 btaor(hibit, ~4, BT848_E_CONTROL);
1103 btaor(hibit, ~4, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106static void bt848_sat(struct bttv *btv, int color)
1107{
1108 int val_u,val_v,hibits;
1109
1110 btv->saturation = color;
1111
1112 /* 0-511 for the color */
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001113 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1114 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001115 hibits = (val_u >> 7) & 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 hibits |= (val_v >> 8) & 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001117 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1118 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1119 btaor(hibits, ~3, BT848_E_CONTROL);
1120 btaor(hibits, ~3, BT848_O_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
1123/* ----------------------------------------------------------------------- */
1124
1125static int
1126video_mux(struct bttv *btv, unsigned int input)
1127{
1128 int mux,mask2;
1129
1130 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1131 return -EINVAL;
1132
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001133 /* needed by RemoteVideo MX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1135 if (mask2)
1136 gpio_inout(mask2,mask2);
1137
1138 if (input == btv->svhs) {
1139 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1140 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1141 } else {
1142 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1143 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1144 }
1145 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
1146 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1147 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1148 btv->c.nr,input,mux);
1149
1150 /* card specific hook */
1151 if(bttv_tvcards[btv->c.type].muxsel_hook)
1152 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1153 return 0;
1154}
1155
1156static char *audio_modes[] = {
1157 "audio: tuner", "audio: radio", "audio: extern",
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001158 "audio: intern", "audio: mute"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159};
1160
1161static int
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001162audio_mux(struct bttv *btv, int input, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001164 int gpio_val, signal;
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001165 struct v4l2_control ctrl;
1166 struct i2c_client *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1169 bttv_tvcards[btv->c.type].gpiomask);
1170 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1171
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001172 btv->mute = mute;
1173 btv->audio = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001175 /* automute */
1176 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1177
1178 if (mute)
1179 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1180 else
1181 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001182
1183 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (bttv_gpio)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001185 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1186 if (in_interrupt())
1187 return 0;
1188
1189 ctrl.id = V4L2_CID_AUDIO_MUTE;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001190 ctrl.value = btv->mute;
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001191 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, &ctrl);
1192 c = btv->i2c_msp34xx_client;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001193 if (c) {
1194 struct v4l2_routing route;
1195
1196 /* Note: the inputs tuner/radio/extern/intern are translated
1197 to msp routings. This assumes common behavior for all msp3400
1198 based TV cards. When this assumption fails, then the
1199 specific MSP routing must be added to the card table.
1200 For now this is sufficient. */
1201 switch (input) {
1202 case TVAUDIO_INPUT_RADIO:
Hans Verkuil07151722006-04-01 18:03:23 -03001203 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1204 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001205 break;
1206 case TVAUDIO_INPUT_EXTERN:
Hans Verkuil07151722006-04-01 18:03:23 -03001207 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1208 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001209 break;
1210 case TVAUDIO_INPUT_INTERN:
1211 /* Yes, this is the same input as for RADIO. I doubt
1212 if this is ever used. The only board with an INTERN
1213 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1214 that was tested. My guess is that the whole INTERN
1215 input does not work. */
Hans Verkuil07151722006-04-01 18:03:23 -03001216 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1217 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
Hans Verkuil2474ed42006-03-19 12:35:57 -03001218 break;
1219 case TVAUDIO_INPUT_TUNER:
1220 default:
Wade Berrier434b2522007-06-25 13:02:16 -03001221 /* This is the only card that uses TUNER2, and afaik,
1222 is the only difference between the VOODOOTV_FM
1223 and VOODOOTV_200 */
1224 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1225 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1226 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1227 else
1228 route.input = MSP_INPUT_DEFAULT;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001229 break;
1230 }
1231 route.output = MSP_OUTPUT_DEFAULT;
1232 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1233 }
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001234 c = btv->i2c_tvaudio_client;
Hans Verkuil2474ed42006-03-19 12:35:57 -03001235 if (c) {
1236 struct v4l2_routing route;
1237
1238 route.input = input;
1239 route.output = 0;
1240 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return 0;
1243}
1244
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001245static inline int
1246audio_mute(struct bttv *btv, int mute)
1247{
1248 return audio_mux(btv, btv->audio, mute);
1249}
1250
1251static inline int
1252audio_input(struct bttv *btv, int input)
1253{
1254 return audio_mux(btv, input, btv->mute);
1255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257static void
1258i2c_vidiocschan(struct bttv *btv)
1259{
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001260 v4l2_std_id std = bttv_tvnorms[btv->tvnorm].v4l2_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001262 bttv_call_i2c_clients(btv, VIDIOC_S_STD, &std);
Wade Berrier434b2522007-06-25 13:02:16 -03001263 if (btv->c.type == BTTV_BOARD_VOODOOTV_FM || btv->c.type == BTTV_BOARD_VOODOOTV_200)
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001264 bttv_tda9880_setnorm(btv,btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Michael Schimeke5bd0262007-01-18 16:17:39 -03001267static void
1268bttv_crop_calc_limits(struct bttv_crop *c)
1269{
1270 /* Scale factor min. 1:1, max. 16:1. Min. image size
1271 48 x 32. Scaled width must be a multiple of 4. */
1272
1273 if (1) {
1274 /* For bug compatibility with VIDIOCGCAP and image
1275 size checks in earlier driver versions. */
1276 c->min_scaled_width = 48;
1277 c->min_scaled_height = 32;
1278 } else {
1279 c->min_scaled_width =
1280 (max(48, c->rect.width >> 4) + 3) & ~3;
1281 c->min_scaled_height =
1282 max(32, c->rect.height >> 4);
1283 }
1284
1285 c->max_scaled_width = c->rect.width & ~3;
1286 c->max_scaled_height = c->rect.height;
1287}
1288
1289static void
1290bttv_crop_reset(struct bttv_crop *c, int norm)
1291{
1292 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1293 bttv_crop_calc_limits(c);
1294}
1295
1296/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297static int
1298set_tvnorm(struct bttv *btv, unsigned int norm)
1299{
1300 const struct bttv_tvnorm *tvnorm;
1301
1302 if (norm < 0 || norm >= BTTV_TVNORMS)
1303 return -EINVAL;
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 tvnorm = &bttv_tvnorms[norm];
1306
Michael Schimeke5bd0262007-01-18 16:17:39 -03001307 if (btv->tvnorm < 0 ||
1308 btv->tvnorm >= BTTV_TVNORMS ||
1309 0 != memcmp(&bttv_tvnorms[btv->tvnorm].cropcap,
1310 &tvnorm->cropcap,
1311 sizeof (tvnorm->cropcap))) {
1312 bttv_crop_reset(&btv->crop[0], norm);
1313 btv->crop[1] = btv->crop[0]; /* current = default */
1314
1315 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1316 btv->crop_start = tvnorm->cropcap.bounds.top
1317 + tvnorm->cropcap.bounds.height;
1318 }
1319 }
1320
1321 btv->tvnorm = norm;
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 btwrite(tvnorm->adelay, BT848_ADELAY);
1324 btwrite(tvnorm->bdelay, BT848_BDELAY);
1325 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1326 BT848_IFORM);
1327 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1328 btwrite(1, BT848_VBI_PACK_DEL);
1329 bt848A_set_timing(btv);
1330
1331 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -08001332 case BTTV_BOARD_VOODOOTV_FM:
Wade Berrier434b2522007-06-25 13:02:16 -03001333 case BTTV_BOARD_VOODOOTV_200:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 bttv_tda9880_setnorm(btv,norm);
1335 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337 return 0;
1338}
1339
Michael Schimeke5bd0262007-01-18 16:17:39 -03001340/* Call with btv->lock down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341static void
Trent Piepho333408f2007-07-03 15:08:10 -03001342set_input(struct bttv *btv, unsigned int input, unsigned int norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 unsigned long flags;
1345
1346 btv->input = input;
1347 if (irq_iswitch) {
1348 spin_lock_irqsave(&btv->s_lock,flags);
1349 if (btv->curr.frame_irq) {
1350 /* active capture -> delayed input switch */
1351 btv->new_input = input;
1352 } else {
1353 video_mux(btv,input);
1354 }
1355 spin_unlock_irqrestore(&btv->s_lock,flags);
1356 } else {
1357 video_mux(btv,input);
1358 }
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03001359 audio_input(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1360 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN));
Trent Piepho333408f2007-07-03 15:08:10 -03001361 set_tvnorm(btv, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 i2c_vidiocschan(btv);
1363}
1364
1365static void init_irqreg(struct bttv *btv)
1366{
1367 /* clear status */
1368 btwrite(0xfffffUL, BT848_INT_STAT);
1369
1370 if (bttv_tvcards[btv->c.type].no_video) {
1371 /* i2c only */
1372 btwrite(BT848_INT_I2CDONE,
1373 BT848_INT_MASK);
1374 } else {
1375 /* full video */
1376 btwrite((btv->triton1) |
1377 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1378 BT848_INT_SCERR |
1379 (fdsr ? BT848_INT_FDSR : 0) |
1380 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1381 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1382 BT848_INT_I2CDONE,
1383 BT848_INT_MASK);
1384 }
1385}
1386
1387static void init_bt848(struct bttv *btv)
1388{
1389 int val;
1390
1391 if (bttv_tvcards[btv->c.type].no_video) {
1392 /* very basic init only */
1393 init_irqreg(btv);
1394 return;
1395 }
1396
1397 btwrite(0x00, BT848_CAP_CTL);
1398 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1399 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1400
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001401 /* set planar and packed mode trigger points and */
1402 /* set rising edge of inverted GPINTR pin as irq trigger */
1403 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1404 BT848_GPIO_DMA_CTL_PLTP1_16|
1405 BT848_GPIO_DMA_CTL_PLTP23_16|
1406 BT848_GPIO_DMA_CTL_GPINTC|
1407 BT848_GPIO_DMA_CTL_GPINTI,
1408 BT848_GPIO_DMA_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001411 btwrite(val, BT848_E_SCLOOP);
1412 btwrite(val, BT848_O_SCLOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001414 btwrite(0x20, BT848_E_VSCALE_HI);
1415 btwrite(0x20, BT848_O_VSCALE_HI);
1416 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 BT848_ADC);
1418
1419 btwrite(whitecrush_upper, BT848_WC_UP);
1420 btwrite(whitecrush_lower, BT848_WC_DOWN);
1421
1422 if (btv->opt_lumafilter) {
1423 btwrite(0, BT848_E_CONTROL);
1424 btwrite(0, BT848_O_CONTROL);
1425 } else {
1426 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1427 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1428 }
1429
1430 bt848_bright(btv, btv->bright);
1431 bt848_hue(btv, btv->hue);
1432 bt848_contrast(btv, btv->contrast);
1433 bt848_sat(btv, btv->saturation);
1434
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001435 /* interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 init_irqreg(btv);
1437}
1438
1439static void bttv_reinit_bt848(struct bttv *btv)
1440{
1441 unsigned long flags;
1442
1443 if (bttv_verbose)
1444 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1445 spin_lock_irqsave(&btv->s_lock,flags);
1446 btv->errors=0;
1447 bttv_set_dma(btv,0);
1448 spin_unlock_irqrestore(&btv->s_lock,flags);
1449
1450 init_bt848(btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001451 btv->pll.pll_current = -1;
Trent Piepho333408f2007-07-03 15:08:10 -03001452 set_input(btv, btv->input, btv->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455static int get_control(struct bttv *btv, struct v4l2_control *c)
1456{
1457 struct video_audio va;
1458 int i;
1459
1460 for (i = 0; i < BTTV_CTLS; i++)
1461 if (bttv_ctls[i].id == c->id)
1462 break;
1463 if (i == BTTV_CTLS)
1464 return -EINVAL;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001465 if (btv->audio_hook && i >= 4 && i <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 memset(&va,0,sizeof(va));
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001467 btv->audio_hook(btv,&va,0);
1468 switch (c->id) {
1469 case V4L2_CID_AUDIO_MUTE:
1470 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1471 break;
1472 case V4L2_CID_AUDIO_VOLUME:
1473 c->value = va.volume;
1474 break;
1475 case V4L2_CID_AUDIO_BALANCE:
1476 c->value = va.balance;
1477 break;
1478 case V4L2_CID_AUDIO_BASS:
1479 c->value = va.bass;
1480 break;
1481 case V4L2_CID_AUDIO_TREBLE:
1482 c->value = va.treble;
1483 break;
1484 }
1485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487 switch (c->id) {
1488 case V4L2_CID_BRIGHTNESS:
1489 c->value = btv->bright;
1490 break;
1491 case V4L2_CID_HUE:
1492 c->value = btv->hue;
1493 break;
1494 case V4L2_CID_CONTRAST:
1495 c->value = btv->contrast;
1496 break;
1497 case V4L2_CID_SATURATION:
1498 c->value = btv->saturation;
1499 break;
1500
1501 case V4L2_CID_AUDIO_MUTE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 case V4L2_CID_AUDIO_VOLUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 case V4L2_CID_AUDIO_BALANCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 case V4L2_CID_AUDIO_BASS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001506 bttv_call_i2c_clients(btv,VIDIOC_G_CTRL,c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 break;
1508
1509 case V4L2_CID_PRIVATE_CHROMA_AGC:
1510 c->value = btv->opt_chroma_agc;
1511 break;
1512 case V4L2_CID_PRIVATE_COMBFILTER:
1513 c->value = btv->opt_combfilter;
1514 break;
1515 case V4L2_CID_PRIVATE_LUMAFILTER:
1516 c->value = btv->opt_lumafilter;
1517 break;
1518 case V4L2_CID_PRIVATE_AUTOMUTE:
1519 c->value = btv->opt_automute;
1520 break;
1521 case V4L2_CID_PRIVATE_AGC_CRUSH:
1522 c->value = btv->opt_adc_crush;
1523 break;
1524 case V4L2_CID_PRIVATE_VCR_HACK:
1525 c->value = btv->opt_vcr_hack;
1526 break;
1527 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1528 c->value = btv->opt_whitecrush_upper;
1529 break;
1530 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1531 c->value = btv->opt_whitecrush_lower;
1532 break;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001533 case V4L2_CID_PRIVATE_UV_RATIO:
1534 c->value = btv->opt_uv_ratio;
1535 break;
1536 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1537 c->value = btv->opt_full_luma_range;
1538 break;
1539 case V4L2_CID_PRIVATE_CORING:
1540 c->value = btv->opt_coring;
1541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 default:
1543 return -EINVAL;
1544 }
1545 return 0;
1546}
1547
1548static int set_control(struct bttv *btv, struct v4l2_control *c)
1549{
1550 struct video_audio va;
1551 int i,val;
1552
1553 for (i = 0; i < BTTV_CTLS; i++)
1554 if (bttv_ctls[i].id == c->id)
1555 break;
1556 if (i == BTTV_CTLS)
1557 return -EINVAL;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001558 if (btv->audio_hook && i >= 4 && i <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 memset(&va,0,sizeof(va));
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001560 btv->audio_hook(btv,&va,0);
1561 switch (c->id) {
1562 case V4L2_CID_AUDIO_MUTE:
1563 if (c->value) {
1564 va.flags |= VIDEO_AUDIO_MUTE;
1565 audio_mute(btv, 1);
1566 } else {
1567 va.flags &= ~VIDEO_AUDIO_MUTE;
1568 audio_mute(btv, 0);
1569 }
1570 break;
1571
1572 case V4L2_CID_AUDIO_VOLUME:
1573 va.volume = c->value;
1574 break;
1575 case V4L2_CID_AUDIO_BALANCE:
1576 va.balance = c->value;
1577 break;
1578 case V4L2_CID_AUDIO_BASS:
1579 va.bass = c->value;
1580 break;
1581 case V4L2_CID_AUDIO_TREBLE:
1582 va.treble = c->value;
1583 break;
1584 }
1585 btv->audio_hook(btv,&va,1);
1586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
1588 switch (c->id) {
1589 case V4L2_CID_BRIGHTNESS:
1590 bt848_bright(btv,c->value);
1591 break;
1592 case V4L2_CID_HUE:
1593 bt848_hue(btv,c->value);
1594 break;
1595 case V4L2_CID_CONTRAST:
1596 bt848_contrast(btv,c->value);
1597 break;
1598 case V4L2_CID_SATURATION:
1599 bt848_sat(btv,c->value);
1600 break;
1601 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001602 audio_mute(btv, c->value);
1603 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 case V4L2_CID_AUDIO_VOLUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 case V4L2_CID_AUDIO_BALANCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 case V4L2_CID_AUDIO_BASS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil0020d3e2006-03-30 19:50:34 -03001608 bttv_call_i2c_clients(btv,VIDIOC_S_CTRL,c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 break;
1610
1611 case V4L2_CID_PRIVATE_CHROMA_AGC:
1612 btv->opt_chroma_agc = c->value;
1613 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1614 btwrite(val, BT848_E_SCLOOP);
1615 btwrite(val, BT848_O_SCLOOP);
1616 break;
1617 case V4L2_CID_PRIVATE_COMBFILTER:
1618 btv->opt_combfilter = c->value;
1619 break;
1620 case V4L2_CID_PRIVATE_LUMAFILTER:
1621 btv->opt_lumafilter = c->value;
1622 if (btv->opt_lumafilter) {
1623 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1624 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1625 } else {
1626 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1627 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1628 }
1629 break;
1630 case V4L2_CID_PRIVATE_AUTOMUTE:
1631 btv->opt_automute = c->value;
1632 break;
1633 case V4L2_CID_PRIVATE_AGC_CRUSH:
1634 btv->opt_adc_crush = c->value;
1635 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1636 BT848_ADC);
1637 break;
1638 case V4L2_CID_PRIVATE_VCR_HACK:
1639 btv->opt_vcr_hack = c->value;
1640 break;
1641 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1642 btv->opt_whitecrush_upper = c->value;
1643 btwrite(c->value, BT848_WC_UP);
1644 break;
1645 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1646 btv->opt_whitecrush_lower = c->value;
1647 btwrite(c->value, BT848_WC_DOWN);
1648 break;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07001649 case V4L2_CID_PRIVATE_UV_RATIO:
1650 btv->opt_uv_ratio = c->value;
1651 bt848_sat(btv, btv->saturation);
1652 break;
1653 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1654 btv->opt_full_luma_range = c->value;
1655 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1656 break;
1657 case V4L2_CID_PRIVATE_CORING:
1658 btv->opt_coring = c->value;
1659 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1660 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 default:
1662 return -EINVAL;
1663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return 0;
1665}
1666
1667/* ----------------------------------------------------------------------- */
1668
1669void bttv_gpio_tracking(struct bttv *btv, char *comment)
1670{
1671 unsigned int outbits, data;
1672 outbits = btread(BT848_GPIO_OUT_EN);
1673 data = btread(BT848_GPIO_DATA);
1674 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1675 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1676}
1677
1678static void bttv_field_count(struct bttv *btv)
1679{
1680 int need_count = 0;
1681
1682 if (btv->users)
1683 need_count++;
1684
1685 if (need_count) {
1686 /* start field counter */
1687 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1688 } else {
1689 /* stop field counter */
1690 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1691 btv->field_count = 0;
1692 }
1693}
1694
1695static const struct bttv_format*
1696format_by_palette(int palette)
1697{
1698 unsigned int i;
1699
1700 for (i = 0; i < BTTV_FORMATS; i++) {
1701 if (-1 == bttv_formats[i].palette)
1702 continue;
1703 if (bttv_formats[i].palette == palette)
1704 return bttv_formats+i;
1705 }
1706 return NULL;
1707}
1708
1709static const struct bttv_format*
1710format_by_fourcc(int fourcc)
1711{
1712 unsigned int i;
1713
1714 for (i = 0; i < BTTV_FORMATS; i++) {
1715 if (-1 == bttv_formats[i].fourcc)
1716 continue;
1717 if (bttv_formats[i].fourcc == fourcc)
1718 return bttv_formats+i;
1719 }
1720 return NULL;
1721}
1722
1723/* ----------------------------------------------------------------------- */
1724/* misc helpers */
1725
1726static int
1727bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1728 struct bttv_buffer *new)
1729{
1730 struct bttv_buffer *old;
1731 unsigned long flags;
1732 int retval = 0;
1733
1734 dprintk("switch_overlay: enter [new=%p]\n",new);
1735 if (new)
1736 new->vb.state = STATE_DONE;
1737 spin_lock_irqsave(&btv->s_lock,flags);
1738 old = btv->screen;
1739 btv->screen = new;
1740 btv->loop_irq |= 1;
1741 bttv_set_dma(btv, 0x03);
1742 spin_unlock_irqrestore(&btv->s_lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (NULL != old) {
1744 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001745 bttv_dma_free(&fh->cap,btv, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 kfree(old);
1747 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03001748 if (NULL == new)
1749 free_btres(btv,fh,RESOURCE_OVERLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 dprintk("switch_overlay: done\n");
1751 return retval;
1752}
1753
1754/* ----------------------------------------------------------------------- */
1755/* video4linux (1) interface */
1756
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001757static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1758 struct bttv_buffer *buf,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001759 const struct bttv_format *fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 unsigned int width, unsigned int height,
1761 enum v4l2_field field)
1762{
Michael Schimeke5bd0262007-01-18 16:17:39 -03001763 struct bttv_fh *fh = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 int redo_dma_risc = 0;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001765 struct bttv_crop c;
1766 int norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 int rc;
1768
1769 /* check settings */
1770 if (NULL == fmt)
1771 return -EINVAL;
1772 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1773 width = RAW_BPL;
1774 height = RAW_LINES*2;
1775 if (width*height > buf->vb.bsize)
1776 return -EINVAL;
1777 buf->vb.size = buf->vb.bsize;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001778
1779 /* Make sure tvnorm and vbi_end remain consistent
1780 until we're done. */
1781 mutex_lock(&btv->lock);
1782
1783 norm = btv->tvnorm;
1784
1785 /* In this mode capturing always starts at defrect.top
1786 (default VDELAY), ignoring cropping parameters. */
1787 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1788 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001790 }
1791
1792 mutex_unlock(&btv->lock);
1793
1794 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1795 } else {
1796 mutex_lock(&btv->lock);
1797
1798 norm = btv->tvnorm;
1799 c = btv->crop[!!fh->do_crop];
1800
1801 mutex_unlock(&btv->lock);
1802
1803 if (width < c.min_scaled_width ||
1804 width > c.max_scaled_width ||
1805 height < c.min_scaled_height)
1806 return -EINVAL;
1807
1808 switch (field) {
1809 case V4L2_FIELD_TOP:
1810 case V4L2_FIELD_BOTTOM:
1811 case V4L2_FIELD_ALTERNATE:
1812 /* btv->crop counts frame lines. Max. scale
1813 factor is 16:1 for frames, 8:1 for fields. */
1814 if (height * 2 > c.max_scaled_height)
1815 return -EINVAL;
1816 break;
1817
1818 default:
1819 if (height > c.max_scaled_height)
1820 return -EINVAL;
1821 break;
1822 }
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 buf->vb.size = (width * height * fmt->depth) >> 3;
1825 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1826 return -EINVAL;
1827 }
1828
1829 /* alloc + fill struct bttv_buffer (if changed) */
1830 if (buf->vb.width != width || buf->vb.height != height ||
1831 buf->vb.field != field ||
Michael Schimeke5bd0262007-01-18 16:17:39 -03001832 buf->tvnorm != norm || buf->fmt != fmt ||
1833 buf->crop.top != c.rect.top ||
1834 buf->crop.left != c.rect.left ||
1835 buf->crop.width != c.rect.width ||
1836 buf->crop.height != c.rect.height) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 buf->vb.width = width;
1838 buf->vb.height = height;
1839 buf->vb.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001840 buf->tvnorm = norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 buf->fmt = fmt;
Michael Schimeke5bd0262007-01-18 16:17:39 -03001842 buf->crop = c.rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 redo_dma_risc = 1;
1844 }
1845
1846 /* alloc risc memory */
1847 if (STATE_NEEDS_INIT == buf->vb.state) {
1848 redo_dma_risc = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001849 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 goto fail;
1851 }
1852
1853 if (redo_dma_risc)
1854 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1855 goto fail;
1856
1857 buf->vb.state = STATE_PREPARED;
1858 return 0;
1859
1860 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001861 bttv_dma_free(q,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return rc;
1863}
1864
1865static int
1866buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1867{
1868 struct bttv_fh *fh = q->priv_data;
1869
1870 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1871 if (0 == *count)
1872 *count = gbuffers;
1873 while (*size * *count > gbuffers * gbufsize)
1874 (*count)--;
1875 return 0;
1876}
1877
1878static int
1879buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1880 enum v4l2_field field)
1881{
1882 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1883 struct bttv_fh *fh = q->priv_data;
1884
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001885 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 fh->width, fh->height, field);
1887}
1888
1889static void
1890buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1891{
1892 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1893 struct bttv_fh *fh = q->priv_data;
1894 struct bttv *btv = fh->btv;
1895
1896 buf->vb.state = STATE_QUEUED;
1897 list_add_tail(&buf->vb.queue,&btv->capture);
1898 if (!btv->curr.frame_irq) {
1899 btv->loop_irq |= 1;
1900 bttv_set_dma(btv, 0x03);
1901 }
1902}
1903
1904static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1905{
1906 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1907 struct bttv_fh *fh = q->priv_data;
1908
Michael Schimekfeaba7a2007-01-26 08:30:05 -03001909 bttv_dma_free(q,fh->btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
1912static struct videobuf_queue_ops bttv_video_qops = {
1913 .buf_setup = buffer_setup,
1914 .buf_prepare = buffer_prepare,
1915 .buf_queue = buffer_queue,
1916 .buf_release = buffer_release,
1917};
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1920{
1921 switch (cmd) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001922 case BTTV_VERSION:
1923 return BTTV_VERSION_CODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 /* *** v4l1 *** ************************************************ */
1926 case VIDIOCGFREQ:
1927 {
1928 unsigned long *freq = arg;
1929 *freq = btv->freq;
1930 return 0;
1931 }
1932 case VIDIOCSFREQ:
1933 {
Hans Verkuil3bbe5a82006-04-01 15:27:52 -03001934 struct v4l2_frequency freq;
1935
1936 memset(&freq, 0, sizeof(freq));
1937 freq.frequency = *(unsigned long *)arg;
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001938 mutex_lock(&btv->lock);
Hans Verkuil3bbe5a82006-04-01 15:27:52 -03001939 freq.type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1940 btv->freq = *(unsigned long *)arg;
1941 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,&freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (btv->has_matchbox && btv->radio_user)
Hans Verkuil3bbe5a82006-04-01 15:27:52 -03001943 tea5757_set_freq(btv,*(unsigned long *)arg);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001944 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return 0;
1946 }
1947
1948 case VIDIOCGTUNER:
1949 {
1950 struct video_tuner *v = arg;
1951
1952 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1953 return -EINVAL;
1954 if (v->tuner) /* Only tuner 0 */
1955 return -EINVAL;
1956 strcpy(v->name, "Television");
1957 v->rangelow = 0;
1958 v->rangehigh = 0x7FFFFFFF;
1959 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1960 v->mode = btv->tvnorm;
1961 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1962 bttv_call_i2c_clients(btv,cmd,v);
1963 return 0;
1964 }
1965 case VIDIOCSTUNER:
1966 {
1967 struct video_tuner *v = arg;
1968
1969 if (v->tuner) /* Only tuner 0 */
1970 return -EINVAL;
1971 if (v->mode >= BTTV_TVNORMS)
1972 return -EINVAL;
1973
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001974 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 set_tvnorm(btv,v->mode);
1976 bttv_call_i2c_clients(btv,cmd,v);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02001977 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return 0;
1979 }
1980
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001981 case VIDIOCGCHAN:
1982 {
1983 struct video_channel *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 unsigned int channel = v->channel;
1985
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001986 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1987 return -EINVAL;
1988 v->tuners=0;
1989 v->flags = VIDEO_VC_AUDIO;
1990 v->type = VIDEO_TYPE_CAMERA;
1991 v->norm = btv->tvnorm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (channel == bttv_tvcards[btv->c.type].tuner) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001993 strcpy(v->name,"Television");
1994 v->flags|=VIDEO_VC_TUNER;
1995 v->type=VIDEO_TYPE_TV;
1996 v->tuners=1;
1997 } else if (channel == btv->svhs) {
1998 strcpy(v->name,"S-Video");
1999 } else {
2000 sprintf(v->name,"Composite%d",channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
2002 return 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002003 }
2004 case VIDIOCSCHAN:
2005 {
2006 struct video_channel *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 unsigned int channel = v->channel;
2008
2009 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
2010 return -EINVAL;
2011 if (v->norm >= BTTV_TVNORMS)
2012 return -EINVAL;
2013
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002014 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (channel == btv->input &&
2016 v->norm == btv->tvnorm) {
2017 /* nothing to do */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002018 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return 0;
2020 }
2021
Trent Piepho333408f2007-07-03 15:08:10 -03002022 set_input(btv, v->channel, v->norm);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002023 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return 0;
2025 }
2026
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002027 case VIDIOCGAUDIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 {
2029 struct video_audio *v = arg;
2030
2031 memset(v,0,sizeof(*v));
2032 strcpy(v->name,"Television");
2033 v->flags |= VIDEO_AUDIO_MUTABLE;
2034 v->mode = VIDEO_SOUND_MONO;
2035
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002036 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 bttv_call_i2c_clients(btv,cmd,v);
2038
2039 /* card specific hooks */
2040 if (btv->audio_hook)
2041 btv->audio_hook(btv,v,0);
2042
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002043 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return 0;
2045 }
2046 case VIDIOCSAUDIO:
2047 {
2048 struct video_audio *v = arg;
2049 unsigned int audio = v->audio;
2050
2051 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
2052 return -EINVAL;
2053
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002054 mutex_lock(&btv->lock);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03002055 audio_mute(btv, (v->flags&VIDEO_AUDIO_MUTE) ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 bttv_call_i2c_clients(btv,cmd,v);
2057
2058 /* card specific hooks */
2059 if (btv->audio_hook)
2060 btv->audio_hook(btv,v,1);
2061
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002062 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return 0;
2064 }
2065
2066 /* *** v4l2 *** ************************************************ */
2067 case VIDIOC_ENUMSTD:
2068 {
2069 struct v4l2_standard *e = arg;
2070 unsigned int index = e->index;
2071
2072 if (index >= BTTV_TVNORMS)
2073 return -EINVAL;
2074 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
2075 bttv_tvnorms[e->index].name);
2076 e->index = index;
2077 return 0;
2078 }
2079 case VIDIOC_G_STD:
2080 {
2081 v4l2_std_id *id = arg;
2082 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
2083 return 0;
2084 }
2085 case VIDIOC_S_STD:
2086 {
2087 v4l2_std_id *id = arg;
2088 unsigned int i;
2089
2090 for (i = 0; i < BTTV_TVNORMS; i++)
2091 if (*id & bttv_tvnorms[i].v4l2_id)
2092 break;
2093 if (i == BTTV_TVNORMS)
2094 return -EINVAL;
2095
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002096 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 set_tvnorm(btv,i);
2098 i2c_vidiocschan(btv);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002099 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return 0;
2101 }
2102 case VIDIOC_QUERYSTD:
2103 {
2104 v4l2_std_id *id = arg;
2105
2106 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
2107 *id = V4L2_STD_625_50;
2108 else
2109 *id = V4L2_STD_525_60;
2110 return 0;
2111 }
2112
2113 case VIDIOC_ENUMINPUT:
2114 {
2115 struct v4l2_input *i = arg;
2116 unsigned int n;
2117
2118 n = i->index;
2119 if (n >= bttv_tvcards[btv->c.type].video_inputs)
2120 return -EINVAL;
2121 memset(i,0,sizeof(*i));
2122 i->index = n;
2123 i->type = V4L2_INPUT_TYPE_CAMERA;
Mauro Carvalho Chehab5d9d1712006-11-14 12:40:07 -03002124 i->audioset = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (i->index == bttv_tvcards[btv->c.type].tuner) {
2126 sprintf(i->name, "Television");
2127 i->type = V4L2_INPUT_TYPE_TUNER;
2128 i->tuner = 0;
2129 } else if (i->index == btv->svhs) {
2130 sprintf(i->name, "S-Video");
2131 } else {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002132 sprintf(i->name,"Composite%d",i->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134 if (i->index == btv->input) {
2135 __u32 dstatus = btread(BT848_DSTATUS);
2136 if (0 == (dstatus & BT848_DSTATUS_PRES))
2137 i->status |= V4L2_IN_ST_NO_SIGNAL;
2138 if (0 == (dstatus & BT848_DSTATUS_HLOC))
2139 i->status |= V4L2_IN_ST_NO_H_LOCK;
2140 }
2141 for (n = 0; n < BTTV_TVNORMS; n++)
2142 i->std |= bttv_tvnorms[n].v4l2_id;
2143 return 0;
2144 }
2145 case VIDIOC_G_INPUT:
2146 {
2147 int *i = arg;
2148 *i = btv->input;
2149 return 0;
2150 }
2151 case VIDIOC_S_INPUT:
2152 {
2153 unsigned int *i = arg;
2154
2155 if (*i > bttv_tvcards[btv->c.type].video_inputs)
2156 return -EINVAL;
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002157 mutex_lock(&btv->lock);
Trent Piepho333408f2007-07-03 15:08:10 -03002158 set_input(btv, *i, btv->tvnorm);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002159 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return 0;
2161 }
2162
2163 case VIDIOC_G_TUNER:
2164 {
2165 struct v4l2_tuner *t = arg;
2166
2167 if (UNSET == bttv_tvcards[btv->c.type].tuner)
2168 return -EINVAL;
2169 if (0 != t->index)
2170 return -EINVAL;
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002171 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 memset(t,0,sizeof(*t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 t->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002174 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
2175 strcpy(t->name, "Television");
2176 t->capability = V4L2_TUNER_CAP_NORM;
2177 t->type = V4L2_TUNER_ANALOG_TV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2179 t->signal = 0xffff;
Michael H. Schimekbbf78712005-12-01 00:51:40 -08002180
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002181 if (btv->audio_hook) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 /* Hmmm ... */
2183 struct video_audio va;
2184 memset(&va, 0, sizeof(struct video_audio));
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002185 btv->audio_hook(btv,&va,0);
2186 t->audmode = V4L2_TUNER_MODE_MONO;
2187 t->rxsubchans = V4L2_TUNER_SUB_MONO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if(va.mode & VIDEO_SOUND_STEREO) {
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002189 t->audmode = V4L2_TUNER_MODE_STEREO;
2190 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002192 if(va.mode & VIDEO_SOUND_LANG2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 t->audmode = V4L2_TUNER_MODE_LANG1;
2194 t->rxsubchans = V4L2_TUNER_SUB_LANG1
2195 | V4L2_TUNER_SUB_LANG2;
2196 }
2197 }
2198 /* FIXME: fill capability+audmode */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002199 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return 0;
2201 }
2202 case VIDIOC_S_TUNER:
2203 {
2204 struct v4l2_tuner *t = arg;
2205
2206 if (UNSET == bttv_tvcards[btv->c.type].tuner)
2207 return -EINVAL;
2208 if (0 != t->index)
2209 return -EINVAL;
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002210 mutex_lock(&btv->lock);
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002211 bttv_call_i2c_clients(btv, VIDIOC_S_TUNER, t);
2212 if (btv->audio_hook) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 struct video_audio va;
2214 memset(&va, 0, sizeof(struct video_audio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (t->audmode == V4L2_TUNER_MODE_MONO)
2216 va.mode = VIDEO_SOUND_MONO;
Hans Verkuil301e22d2006-03-18 17:15:00 -03002217 else if (t->audmode == V4L2_TUNER_MODE_STEREO ||
2218 t->audmode == V4L2_TUNER_MODE_LANG1_LANG2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 va.mode = VIDEO_SOUND_STEREO;
2220 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
2221 va.mode = VIDEO_SOUND_LANG1;
2222 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
2223 va.mode = VIDEO_SOUND_LANG2;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002224 btv->audio_hook(btv,&va,1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002226 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return 0;
2228 }
2229
2230 case VIDIOC_G_FREQUENCY:
2231 {
2232 struct v4l2_frequency *f = arg;
2233
2234 memset(f,0,sizeof(*f));
2235 f->type = V4L2_TUNER_ANALOG_TV;
2236 f->frequency = btv->freq;
2237 return 0;
2238 }
2239 case VIDIOC_S_FREQUENCY:
2240 {
2241 struct v4l2_frequency *f = arg;
2242
2243 if (unlikely(f->tuner != 0))
2244 return -EINVAL;
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -07002245 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return -EINVAL;
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002247 mutex_lock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 btv->freq = f->frequency;
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002249 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (btv->has_matchbox && btv->radio_user)
2251 tea5757_set_freq(btv,btv->freq);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02002252 mutex_unlock(&btv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return 0;
2254 }
Hans Verkuil299392b2005-11-08 21:37:42 -08002255 case VIDIOC_LOG_STATUS:
2256 {
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002257 printk(KERN_INFO "bttv%d: ================= START STATUS CARD #%d =================\n", btv->c.nr, btv->c.nr);
Luiz Capitulino97cb4452005-12-01 00:51:24 -08002258 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
Hans Verkuil0020d3e2006-03-30 19:50:34 -03002259 printk(KERN_INFO "bttv%d: ================== END STATUS CARD #%d ==================\n", btv->c.nr, btv->c.nr);
Hans Verkuil299392b2005-11-08 21:37:42 -08002260 return 0;
2261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 default:
2264 return -ENOIOCTLCMD;
2265
2266 }
2267 return 0;
2268}
2269
Michael Schimeke5bd0262007-01-18 16:17:39 -03002270/* Given cropping boundaries b and the scaled width and height of a
2271 single field or frame, which must not exceed hardware limits, this
2272 function adjusts the cropping parameters c. */
2273static void
2274bttv_crop_adjust (struct bttv_crop * c,
2275 const struct v4l2_rect * b,
2276 __s32 width,
2277 __s32 height,
2278 enum v4l2_field field)
2279{
2280 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2281 __s32 max_left;
2282 __s32 max_top;
2283
2284 if (width < c->min_scaled_width) {
2285 /* Max. hor. scale factor 16:1. */
2286 c->rect.width = width * 16;
2287 } else if (width > c->max_scaled_width) {
2288 /* Min. hor. scale factor 1:1. */
2289 c->rect.width = width;
2290
2291 max_left = b->left + b->width - width;
2292 max_left = min(max_left, (__s32) MAX_HDELAY);
2293 if (c->rect.left > max_left)
2294 c->rect.left = max_left;
2295 }
2296
2297 if (height < c->min_scaled_height) {
2298 /* Max. vert. scale factor 16:1, single fields 8:1. */
2299 c->rect.height = height * 16;
2300 } else if (frame_height > c->max_scaled_height) {
2301 /* Min. vert. scale factor 1:1.
2302 Top and height count field lines times two. */
2303 c->rect.height = (frame_height + 1) & ~1;
2304
2305 max_top = b->top + b->height - c->rect.height;
2306 if (c->rect.top > max_top)
2307 c->rect.top = max_top;
2308 }
2309
2310 bttv_crop_calc_limits(c);
2311}
2312
2313/* Returns an error if scaling to a frame or single field with the given
2314 width and height is not possible with the current cropping parameters
2315 and width aligned according to width_mask. If adjust_size is TRUE the
2316 function may adjust the width and/or height instead, rounding width
2317 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2318 also adjust the current cropping parameters to get closer to the
2319 desired image size. */
2320static int
2321limit_scaled_size (struct bttv_fh * fh,
2322 __s32 * width,
2323 __s32 * height,
2324 enum v4l2_field field,
2325 unsigned int width_mask,
2326 unsigned int width_bias,
2327 int adjust_size,
2328 int adjust_crop)
2329{
2330 struct bttv *btv = fh->btv;
2331 const struct v4l2_rect *b;
2332 struct bttv_crop *c;
2333 __s32 min_width;
2334 __s32 min_height;
2335 __s32 max_width;
2336 __s32 max_height;
2337 int rc;
2338
2339 BUG_ON((int) width_mask >= 0 ||
2340 width_bias >= (unsigned int) -width_mask);
2341
2342 /* Make sure tvnorm, vbi_end and the current cropping parameters
2343 remain consistent until we're done. */
2344 mutex_lock(&btv->lock);
2345
2346 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2347
2348 /* Do crop - use current, don't - use default parameters. */
2349 c = &btv->crop[!!fh->do_crop];
2350
2351 if (fh->do_crop
2352 && adjust_size
2353 && adjust_crop
2354 && !locked_btres(btv, VIDEO_RESOURCES)) {
2355 min_width = 48;
2356 min_height = 32;
2357
2358 /* We cannot scale up. When the scaled image is larger
2359 than crop.rect we adjust the crop.rect as required
2360 by the V4L2 spec, hence cropcap.bounds are our limit. */
2361 max_width = min(b->width, (__s32) MAX_HACTIVE);
2362 max_height = b->height;
2363
2364 /* We cannot capture the same line as video and VBI data.
2365 Note btv->vbi_end is really a minimum, see
2366 bttv_vbi_try_fmt(). */
2367 if (btv->vbi_end > b->top) {
2368 max_height -= btv->vbi_end - b->top;
2369 rc = -EBUSY;
2370 if (min_height > max_height)
2371 goto fail;
2372 }
2373 } else {
2374 rc = -EBUSY;
2375 if (btv->vbi_end > c->rect.top)
2376 goto fail;
2377
2378 min_width = c->min_scaled_width;
2379 min_height = c->min_scaled_height;
2380 max_width = c->max_scaled_width;
2381 max_height = c->max_scaled_height;
2382
2383 adjust_crop = 0;
2384 }
2385
2386 min_width = (min_width - width_mask - 1) & width_mask;
2387 max_width = max_width & width_mask;
2388
2389 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2390 min_height = min_height;
2391 /* Min. scale factor is 1:1. */
2392 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2393
2394 if (adjust_size) {
2395 *width = clamp(*width, min_width, max_width);
2396 *height = clamp(*height, min_height, max_height);
2397
2398 /* Round after clamping to avoid overflow. */
2399 *width = (*width + width_bias) & width_mask;
2400
2401 if (adjust_crop) {
2402 bttv_crop_adjust(c, b, *width, *height, field);
2403
2404 if (btv->vbi_end > c->rect.top) {
2405 /* Move the crop window out of the way. */
2406 c->rect.top = btv->vbi_end;
2407 }
2408 }
2409 } else {
2410 rc = -EINVAL;
2411 if (*width < min_width ||
2412 *height < min_height ||
2413 *width > max_width ||
2414 *height > max_height ||
2415 0 != (*width & ~width_mask))
2416 goto fail;
2417 }
2418
2419 rc = 0; /* success */
2420
2421 fail:
2422 mutex_unlock(&btv->lock);
2423
2424 return rc;
2425}
2426
2427/* Returns an error if the given overlay window dimensions are not
2428 possible with the current cropping parameters. If adjust_size is
2429 TRUE the function may adjust the window width and/or height
2430 instead, however it always rounds the horizontal position and
2431 width as btcx_align() does. If adjust_crop is TRUE the function
2432 may also adjust the current cropping parameters to get closer
2433 to the desired window size. */
2434static int
2435verify_window (struct bttv_fh * fh,
2436 struct v4l2_window * win,
2437 int adjust_size,
2438 int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
2440 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002441 unsigned int width_mask;
2442 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 if (win->w.width < 48 || win->w.height < 32)
2445 return -EINVAL;
2446 if (win->clipcount > 2048)
2447 return -EINVAL;
2448
2449 field = win->field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 if (V4L2_FIELD_ANY == field) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03002452 __s32 height2;
2453
2454 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2455 field = (win->w.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 ? V4L2_FIELD_INTERLACED
2457 : V4L2_FIELD_TOP;
2458 }
2459 switch (field) {
2460 case V4L2_FIELD_TOP:
2461 case V4L2_FIELD_BOTTOM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 case V4L2_FIELD_INTERLACED:
2463 break;
2464 default:
2465 return -EINVAL;
2466 }
2467
Michael Schimeke5bd0262007-01-18 16:17:39 -03002468 /* 4-byte alignment. */
2469 if (NULL == fh->ovfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002471 width_mask = ~0;
2472 switch (fh->ovfmt->depth) {
2473 case 8:
2474 case 24:
2475 width_mask = ~3;
2476 break;
2477 case 16:
2478 width_mask = ~1;
2479 break;
2480 case 32:
2481 break;
2482 default:
2483 BUG();
2484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Michael Schimeke5bd0262007-01-18 16:17:39 -03002486 win->w.width -= win->w.left & ~width_mask;
2487 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2488
2489 rc = limit_scaled_size(fh, &win->w.width, &win->w.height,
2490 field, width_mask,
2491 /* width_bias: round down */ 0,
2492 adjust_size, adjust_crop);
2493 if (0 != rc)
2494 return rc;
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 win->field = field;
2497 return 0;
2498}
2499
2500static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2501 struct v4l2_window *win, int fixup)
2502{
2503 struct v4l2_clip *clips = NULL;
2504 int n,size,retval = 0;
2505
2506 if (NULL == fh->ovfmt)
2507 return -EINVAL;
2508 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2509 return -EINVAL;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002510 retval = verify_window(fh, win,
2511 /* adjust_size */ fixup,
2512 /* adjust_crop */ fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (0 != retval)
2514 return retval;
2515
2516 /* copy clips -- luckily v4l1 + v4l2 are binary
2517 compatible here ...*/
2518 n = win->clipcount;
2519 size = sizeof(*clips)*(n+4);
2520 clips = kmalloc(size,GFP_KERNEL);
2521 if (NULL == clips)
2522 return -ENOMEM;
2523 if (n > 0) {
2524 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2525 kfree(clips);
2526 return -EFAULT;
2527 }
2528 }
2529 /* clip against screen */
2530 if (NULL != btv->fbuf.base)
2531 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2532 &win->w, clips, n);
2533 btcx_sort_clips(clips,n);
2534
2535 /* 4-byte alignments */
2536 switch (fh->ovfmt->depth) {
2537 case 8:
2538 case 24:
2539 btcx_align(&win->w, clips, n, 3);
2540 break;
2541 case 16:
2542 btcx_align(&win->w, clips, n, 1);
2543 break;
2544 case 32:
2545 /* no alignment fixups needed */
2546 break;
2547 default:
2548 BUG();
2549 }
2550
Ingo Molnar3593cab2006-02-07 06:49:14 -02002551 mutex_lock(&fh->cap.lock);
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08002552 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 fh->ov.clips = clips;
2554 fh->ov.nclips = n;
2555
2556 fh->ov.w = win->w;
2557 fh->ov.field = win->field;
2558 fh->ov.setup_ok = 1;
2559 btv->init.ov.w.width = win->w.width;
2560 btv->init.ov.w.height = win->w.height;
2561 btv->init.ov.field = win->field;
2562
2563 /* update overlay if needed */
2564 retval = 0;
2565 if (check_btres(fh, RESOURCE_OVERLAY)) {
2566 struct bttv_buffer *new;
2567
2568 new = videobuf_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03002569 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2571 retval = bttv_switch_overlay(btv,fh,new);
2572 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002573 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return retval;
2575}
2576
2577/* ----------------------------------------------------------------------- */
2578
2579static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2580{
2581 struct videobuf_queue* q = NULL;
2582
2583 switch (fh->type) {
2584 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2585 q = &fh->cap;
2586 break;
2587 case V4L2_BUF_TYPE_VBI_CAPTURE:
2588 q = &fh->vbi;
2589 break;
2590 default:
2591 BUG();
2592 }
2593 return q;
2594}
2595
2596static int bttv_resource(struct bttv_fh *fh)
2597{
2598 int res = 0;
2599
2600 switch (fh->type) {
2601 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002602 res = RESOURCE_VIDEO_STREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 break;
2604 case V4L2_BUF_TYPE_VBI_CAPTURE:
2605 res = RESOURCE_VBI;
2606 break;
2607 default:
2608 BUG();
2609 }
2610 return res;
2611}
2612
2613static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2614{
2615 struct videobuf_queue *q = bttv_queue(fh);
2616 int res = bttv_resource(fh);
2617
2618 if (check_btres(fh,res))
2619 return -EBUSY;
2620 if (videobuf_queue_is_busy(q))
2621 return -EBUSY;
2622 fh->type = type;
2623 return 0;
2624}
2625
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002626static void
2627pix_format_set_size (struct v4l2_pix_format * f,
2628 const struct bttv_format * fmt,
2629 unsigned int width,
2630 unsigned int height)
2631{
2632 f->width = width;
2633 f->height = height;
2634
2635 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2636 f->bytesperline = width; /* Y plane */
2637 f->sizeimage = (width * height * fmt->depth) >> 3;
2638 } else {
2639 f->bytesperline = (width * fmt->depth) >> 3;
2640 f->sizeimage = height * f->bytesperline;
2641 }
2642}
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2645{
2646 switch (f->type) {
2647 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2648 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
Michael H. Schimekc87c9482005-12-01 00:51:33 -08002649 pix_format_set_size (&f->fmt.pix, fh->fmt,
2650 fh->width, fh->height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 f->fmt.pix.field = fh->cap.field;
2652 f->fmt.pix.pixelformat = fh->fmt->fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return 0;
2654 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2655 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2656 f->fmt.win.w = fh->ov.w;
2657 f->fmt.win.field = fh->ov.field;
2658 return 0;
2659 case V4L2_BUF_TYPE_VBI_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002660 bttv_vbi_get_fmt(fh, &f->fmt.vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 return 0;
2662 default:
2663 return -EINVAL;
2664 }
2665}
2666
2667static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
Michael Schimeke5bd0262007-01-18 16:17:39 -03002668 struct v4l2_format *f, int adjust_crop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
2670 switch (f->type) {
2671 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2672 {
2673 const struct bttv_format *fmt;
2674 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002675 __s32 width, height;
2676 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2679 if (NULL == fmt)
2680 return -EINVAL;
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 field = f->fmt.pix.field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002683 if (V4L2_FIELD_ANY == field) {
2684 __s32 height2;
2685
2686 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2687 field = (f->fmt.pix.height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 ? V4L2_FIELD_INTERLACED
2689 : V4L2_FIELD_BOTTOM;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 if (V4L2_FIELD_SEQ_BT == field)
2692 field = V4L2_FIELD_SEQ_TB;
2693 switch (field) {
2694 case V4L2_FIELD_TOP:
2695 case V4L2_FIELD_BOTTOM:
2696 case V4L2_FIELD_ALTERNATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 case V4L2_FIELD_INTERLACED:
2698 break;
2699 case V4L2_FIELD_SEQ_TB:
2700 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2701 return -EINVAL;
2702 break;
2703 default:
2704 return -EINVAL;
2705 }
2706
Michael Schimeke5bd0262007-01-18 16:17:39 -03002707 width = f->fmt.pix.width;
2708 height = f->fmt.pix.height;
2709
2710 rc = limit_scaled_size(fh, &width, &height, field,
2711 /* width_mask: 4 pixels */ ~3,
2712 /* width_bias: nearest */ 2,
2713 /* adjust_size */ 1,
2714 adjust_crop);
2715 if (0 != rc)
2716 return rc;
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 /* update data for the application */
2719 f->fmt.pix.field = field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002720 pix_format_set_size(&f->fmt.pix, fmt, width, height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 return 0;
2723 }
2724 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002725 return verify_window(fh, &f->fmt.win,
2726 /* adjust_size */ 1,
2727 /* adjust_crop */ 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 case V4L2_BUF_TYPE_VBI_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03002729 return bttv_vbi_try_fmt(fh, &f->fmt.vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 default:
2731 return -EINVAL;
2732 }
2733}
2734
2735static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2736 struct v4l2_format *f)
2737{
2738 int retval;
2739
2740 switch (f->type) {
2741 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2742 {
2743 const struct bttv_format *fmt;
2744
2745 retval = bttv_switch_type(fh,f->type);
2746 if (0 != retval)
2747 return retval;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002748 retval = bttv_try_fmt(fh,btv,f, /* adjust_crop */ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (0 != retval)
2750 return retval;
2751 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2752
2753 /* update our state informations */
Ingo Molnar3593cab2006-02-07 06:49:14 -02002754 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 fh->fmt = fmt;
2756 fh->cap.field = f->fmt.pix.field;
2757 fh->cap.last = V4L2_FIELD_NONE;
2758 fh->width = f->fmt.pix.width;
2759 fh->height = f->fmt.pix.height;
2760 btv->init.fmt = fmt;
2761 btv->init.width = f->fmt.pix.width;
2762 btv->init.height = f->fmt.pix.height;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002763 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 return 0;
2766 }
2767 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002768 if (no_overlay > 0) {
2769 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2770 return -EINVAL;
2771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return setup_window(fh, btv, &f->fmt.win, 1);
2773 case V4L2_BUF_TYPE_VBI_CAPTURE:
2774 retval = bttv_switch_type(fh,f->type);
2775 if (0 != retval)
2776 return retval;
Michael Schimeke5bd0262007-01-18 16:17:39 -03002777 return bttv_vbi_set_fmt(fh, &f->fmt.vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 default:
2779 return -EINVAL;
2780 }
2781}
2782
2783static int bttv_do_ioctl(struct inode *inode, struct file *file,
2784 unsigned int cmd, void *arg)
2785{
2786 struct bttv_fh *fh = file->private_data;
2787 struct bttv *btv = fh->btv;
2788 unsigned long flags;
2789 int retval = 0;
2790
Michael Krufky5e453dc2006-01-09 15:32:31 -02002791 if (bttv_debug > 1)
2792 v4l_print_ioctl(btv->c.name, cmd);
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (btv->errors)
2795 bttv_reinit_bt848(btv);
2796
2797 switch (cmd) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002798 case VIDIOCSFREQ:
2799 case VIDIOCSTUNER:
2800 case VIDIOCSCHAN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 case VIDIOC_S_CTRL:
2802 case VIDIOC_S_STD:
2803 case VIDIOC_S_INPUT:
2804 case VIDIOC_S_TUNER:
2805 case VIDIOC_S_FREQUENCY:
2806 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2807 if (0 != retval)
2808 return retval;
2809 };
2810
2811 switch (cmd) {
2812
2813 /* *** v4l1 *** ************************************************ */
2814 case VIDIOCGCAP:
2815 {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002816 struct video_capability *cap = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 memset(cap,0,sizeof(*cap));
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002819 strcpy(cap->name,btv->video_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2821 /* vbi */
2822 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2823 } else {
2824 /* others */
2825 cap->type = VID_TYPE_CAPTURE|
2826 VID_TYPE_TUNER|
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 VID_TYPE_CLIPPING|
2828 VID_TYPE_SCALES;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002829 if (no_overlay <= 0)
2830 cap->type |= VID_TYPE_OVERLAY;
2831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2833 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2834 cap->minwidth = 48;
2835 cap->minheight = 32;
2836 }
2837 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2838 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
2841
2842 case VIDIOCGPICT:
2843 {
2844 struct video_picture *pic = arg;
2845
2846 memset(pic,0,sizeof(*pic));
2847 pic->brightness = btv->bright;
2848 pic->contrast = btv->contrast;
2849 pic->hue = btv->hue;
2850 pic->colour = btv->saturation;
2851 if (fh->fmt) {
2852 pic->depth = fh->fmt->depth;
2853 pic->palette = fh->fmt->palette;
2854 }
2855 return 0;
2856 }
2857 case VIDIOCSPICT:
2858 {
2859 struct video_picture *pic = arg;
2860 const struct bttv_format *fmt;
2861
2862 fmt = format_by_palette(pic->palette);
2863 if (NULL == fmt)
2864 return -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002865 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (fmt->depth != pic->depth) {
2867 retval = -EINVAL;
2868 goto fh_unlock_and_return;
2869 }
Michael H. Schimek13c72802005-12-01 00:51:37 -08002870 if (fmt->flags & FORMAT_FLAGS_RAW) {
2871 /* VIDIOCMCAPTURE uses gbufsize, not RAW_BPL *
2872 RAW_LINES * 2. F1 is stored at offset 0, F2
2873 at buffer size / 2. */
2874 fh->width = RAW_BPL;
2875 fh->height = gbufsize / RAW_BPL;
2876 btv->init.width = RAW_BPL;
2877 btv->init.height = gbufsize / RAW_BPL;
2878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 fh->ovfmt = fmt;
2880 fh->fmt = fmt;
2881 btv->init.ovfmt = fmt;
2882 btv->init.fmt = fmt;
2883 if (bigendian) {
2884 /* dirty hack time: swap bytes for overlay if the
2885 display adaptor is big endian (insmod option) */
2886 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2887 fmt->palette == VIDEO_PALETTE_RGB565 ||
2888 fmt->palette == VIDEO_PALETTE_RGB32) {
2889 fh->ovfmt = fmt+1;
2890 }
2891 }
2892 bt848_bright(btv,pic->brightness);
2893 bt848_contrast(btv,pic->contrast);
2894 bt848_hue(btv,pic->hue);
2895 bt848_sat(btv,pic->colour);
Ingo Molnar3593cab2006-02-07 06:49:14 -02002896 mutex_unlock(&fh->cap.lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 }
2899
2900 case VIDIOCGWIN:
2901 {
2902 struct video_window *win = arg;
2903
2904 memset(win,0,sizeof(*win));
2905 win->x = fh->ov.w.left;
2906 win->y = fh->ov.w.top;
2907 win->width = fh->ov.w.width;
2908 win->height = fh->ov.w.height;
2909 return 0;
2910 }
2911 case VIDIOCSWIN:
2912 {
2913 struct video_window *win = arg;
2914 struct v4l2_window w2;
2915
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07002916 if (no_overlay > 0) {
2917 printk ("VIDIOCSWIN: no_overlay\n");
2918 return -EINVAL;
2919 }
2920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 w2.field = V4L2_FIELD_ANY;
2922 w2.w.left = win->x;
2923 w2.w.top = win->y;
2924 w2.w.width = win->width;
2925 w2.w.height = win->height;
2926 w2.clipcount = win->clipcount;
2927 w2.clips = (struct v4l2_clip __user *)win->clips;
2928 retval = setup_window(fh, btv, &w2, 0);
2929 if (0 == retval) {
2930 /* on v4l1 this ioctl affects the read() size too */
2931 fh->width = fh->ov.w.width;
2932 fh->height = fh->ov.w.height;
2933 btv->init.width = fh->ov.w.width;
2934 btv->init.height = fh->ov.w.height;
2935 }
2936 return retval;
2937 }
2938
2939 case VIDIOCGFBUF:
2940 {
2941 struct video_buffer *fbuf = arg;
2942
2943 fbuf->base = btv->fbuf.base;
2944 fbuf->width = btv->fbuf.fmt.width;
2945 fbuf->height = btv->fbuf.fmt.height;
2946 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2947 if (fh->ovfmt)
2948 fbuf->depth = fh->ovfmt->depth;
Mauro Carvalho Chehab37026272006-08-06 09:10:06 -03002949 else {
2950 if (fbuf->width)
2951 fbuf->depth = ((fbuf->bytesperline<<3)
Trent Piephoa2b9e3e2006-08-07 20:01:01 -03002952 + (fbuf->width-1) )
2953 /fbuf->width;
Mauro Carvalho Chehab37026272006-08-06 09:10:06 -03002954 else
2955 fbuf->depth = 0;
2956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return 0;
2958 }
2959 case VIDIOCSFBUF:
2960 {
2961 struct video_buffer *fbuf = arg;
2962 const struct bttv_format *fmt;
2963 unsigned long end;
2964
2965 if(!capable(CAP_SYS_ADMIN) &&
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002966 !capable(CAP_SYS_RAWIO))
2967 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 end = (unsigned long)fbuf->base +
2969 fbuf->height * fbuf->bytesperline;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002970 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 retval = -EINVAL;
2972
2973 switch (fbuf->depth) {
2974 case 8:
2975 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2976 break;
2977 case 16:
2978 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2979 break;
2980 case 24:
2981 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2982 break;
2983 case 32:
2984 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2985 break;
2986 case 15:
2987 fbuf->depth = 16;
2988 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2989 break;
2990 default:
2991 fmt = NULL;
2992 break;
2993 }
2994 if (NULL == fmt)
2995 goto fh_unlock_and_return;
2996
2997 fh->ovfmt = fmt;
2998 fh->fmt = fmt;
2999 btv->init.ovfmt = fmt;
3000 btv->init.fmt = fmt;
3001 btv->fbuf.base = fbuf->base;
3002 btv->fbuf.fmt.width = fbuf->width;
3003 btv->fbuf.fmt.height = fbuf->height;
3004 if (fbuf->bytesperline)
3005 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
3006 else
3007 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
Ingo Molnar3593cab2006-02-07 06:49:14 -02003008 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return 0;
3010 }
3011
3012 case VIDIOCCAPTURE:
3013 case VIDIOC_OVERLAY:
3014 {
3015 struct bttv_buffer *new;
3016 int *on = arg;
3017
3018 if (*on) {
3019 /* verify args */
3020 if (NULL == btv->fbuf.base)
3021 return -EINVAL;
3022 if (!fh->ov.setup_ok) {
3023 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
3024 return -EINVAL;
3025 }
3026 }
3027
3028 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
3029 return -EBUSY;
3030
Ingo Molnar3593cab2006-02-07 06:49:14 -02003031 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 if (*on) {
3033 fh->ov.tvnorm = btv->tvnorm;
3034 new = videobuf_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03003035 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
3037 } else {
3038 new = NULL;
3039 }
3040
3041 /* switch over */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003042 retval = bttv_switch_overlay(btv,fh,new);
Ingo Molnar3593cab2006-02-07 06:49:14 -02003043 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return retval;
3045 }
3046
3047 case VIDIOCGMBUF:
3048 {
3049 struct video_mbuf *mbuf = arg;
3050 unsigned int i;
3051
Ingo Molnar3593cab2006-02-07 06:49:14 -02003052 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
3054 V4L2_MEMORY_MMAP);
3055 if (retval < 0)
3056 goto fh_unlock_and_return;
3057 memset(mbuf,0,sizeof(*mbuf));
3058 mbuf->frames = gbuffers;
3059 mbuf->size = gbuffers * gbufsize;
3060 for (i = 0; i < gbuffers; i++)
3061 mbuf->offsets[i] = i * gbufsize;
Ingo Molnar3593cab2006-02-07 06:49:14 -02003062 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 return 0;
3064 }
3065 case VIDIOCMCAPTURE:
3066 {
3067 struct video_mmap *vm = arg;
3068 struct bttv_buffer *buf;
3069 enum v4l2_field field;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003070 __s32 height2;
3071 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 if (vm->frame >= VIDEO_MAX_FRAME)
3074 return -EINVAL;
3075
Michael Schimeke5bd0262007-01-18 16:17:39 -03003076 res = bttv_resource(fh);
3077 if (!check_alloc_btres(btv, fh, res))
3078 return -EBUSY;
3079
Ingo Molnar3593cab2006-02-07 06:49:14 -02003080 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 retval = -EINVAL;
3082 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
3083 if (NULL == buf)
3084 goto fh_unlock_and_return;
3085 if (0 == buf->vb.baddr)
3086 goto fh_unlock_and_return;
3087 if (buf->vb.state == STATE_QUEUED ||
3088 buf->vb.state == STATE_ACTIVE)
3089 goto fh_unlock_and_return;
3090
Michael Schimeke5bd0262007-01-18 16:17:39 -03003091 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
3092 field = (vm->height > height2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 ? V4L2_FIELD_INTERLACED
3094 : V4L2_FIELD_BOTTOM;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03003095 retval = bttv_prepare_buffer(&fh->cap,btv,buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 format_by_palette(vm->format),
3097 vm->width,vm->height,field);
3098 if (0 != retval)
3099 goto fh_unlock_and_return;
3100 spin_lock_irqsave(&btv->s_lock,flags);
3101 buffer_queue(&fh->cap,&buf->vb);
3102 spin_unlock_irqrestore(&btv->s_lock,flags);
Ingo Molnar3593cab2006-02-07 06:49:14 -02003103 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return 0;
3105 }
3106 case VIDIOCSYNC:
3107 {
3108 int *frame = arg;
3109 struct bttv_buffer *buf;
3110
3111 if (*frame >= VIDEO_MAX_FRAME)
3112 return -EINVAL;
3113
Ingo Molnar3593cab2006-02-07 06:49:14 -02003114 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 retval = -EINVAL;
3116 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
3117 if (NULL == buf)
3118 goto fh_unlock_and_return;
3119 retval = videobuf_waiton(&buf->vb,0,1);
3120 if (0 != retval)
3121 goto fh_unlock_and_return;
3122 switch (buf->vb.state) {
3123 case STATE_ERROR:
3124 retval = -EIO;
3125 /* fall through */
3126 case STATE_DONE:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03003127 videobuf_dma_sync(&fh->cap,&buf->vb.dma);
3128 bttv_dma_free(&fh->cap,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 break;
3130 default:
3131 retval = -EINVAL;
3132 break;
3133 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02003134 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return retval;
3136 }
3137
3138 case VIDIOCGVBIFMT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
3140 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
3141 if (0 != retval)
3142 return retval;
3143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Michael Schimeke5bd0262007-01-18 16:17:39 -03003145 /* fall through */
3146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 case VIDIOCSVBIFMT:
Michael Schimeke5bd0262007-01-18 16:17:39 -03003148 return v4l_compat_translate_ioctl(inode, file, cmd,
3149 arg, bttv_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003151 case BTTV_VERSION:
3152 case VIDIOCGFREQ:
3153 case VIDIOCSFREQ:
3154 case VIDIOCGTUNER:
3155 case VIDIOCSTUNER:
3156 case VIDIOCGCHAN:
3157 case VIDIOCSCHAN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 case VIDIOCGAUDIO:
3159 case VIDIOCSAUDIO:
3160 return bttv_common_ioctls(btv,cmd,arg);
3161
3162 /* *** v4l2 *** ************************************************ */
3163 case VIDIOC_QUERYCAP:
3164 {
3165 struct v4l2_capability *cap = arg;
3166
3167 if (0 == v4l2)
3168 return -EINVAL;
Michael H. Schimekbbf78712005-12-01 00:51:40 -08003169 memset(cap, 0, sizeof (*cap));
3170 strlcpy(cap->driver, "bttv", sizeof (cap->driver));
3171 strlcpy(cap->card, btv->video_dev->name, sizeof (cap->card));
3172 snprintf(cap->bus_info, sizeof (cap->bus_info),
3173 "PCI:%s", pci_name(btv->c.pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 cap->version = BTTV_VERSION_CODE;
3175 cap->capabilities =
3176 V4L2_CAP_VIDEO_CAPTURE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 V4L2_CAP_VBI_CAPTURE |
3178 V4L2_CAP_READWRITE |
3179 V4L2_CAP_STREAMING;
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07003180 if (no_overlay <= 0)
3181 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
3184 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
3185 cap->capabilities |= V4L2_CAP_TUNER;
3186 return 0;
3187 }
3188
3189 case VIDIOC_ENUM_FMT:
3190 {
3191 struct v4l2_fmtdesc *f = arg;
3192 enum v4l2_buf_type type;
3193 unsigned int i;
3194 int index;
3195
3196 type = f->type;
3197 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
3198 /* vbi */
3199 index = f->index;
3200 if (0 != index)
3201 return -EINVAL;
3202 memset(f,0,sizeof(*f));
3203 f->index = index;
3204 f->type = type;
3205 f->pixelformat = V4L2_PIX_FMT_GREY;
3206 strcpy(f->description,"vbi data");
3207 return 0;
3208 }
3209
3210 /* video capture + overlay */
3211 index = -1;
3212 for (i = 0; i < BTTV_FORMATS; i++) {
3213 if (bttv_formats[i].fourcc != -1)
3214 index++;
3215 if ((unsigned int)index == f->index)
3216 break;
3217 }
3218 if (BTTV_FORMATS == i)
3219 return -EINVAL;
3220
3221 switch (f->type) {
3222 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3223 break;
3224 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3225 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
3226 return -EINVAL;
3227 break;
3228 default:
3229 return -EINVAL;
3230 }
3231 memset(f,0,sizeof(*f));
3232 f->index = index;
3233 f->type = type;
3234 f->pixelformat = bttv_formats[i].fourcc;
3235 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
3236 return 0;
3237 }
3238
3239 case VIDIOC_TRY_FMT:
3240 {
3241 struct v4l2_format *f = arg;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003242 return bttv_try_fmt(fh,btv,f, /* adjust_crop */ 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 }
3244 case VIDIOC_G_FMT:
3245 {
3246 struct v4l2_format *f = arg;
3247 return bttv_g_fmt(fh,f);
3248 }
3249 case VIDIOC_S_FMT:
3250 {
3251 struct v4l2_format *f = arg;
3252 return bttv_s_fmt(fh,btv,f);
3253 }
3254
3255 case VIDIOC_G_FBUF:
3256 {
3257 struct v4l2_framebuffer *fb = arg;
3258
3259 *fb = btv->fbuf;
3260 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3261 if (fh->ovfmt)
3262 fb->fmt.pixelformat = fh->ovfmt->fourcc;
3263 return 0;
3264 }
3265 case VIDIOC_S_FBUF:
3266 {
3267 struct v4l2_framebuffer *fb = arg;
3268 const struct bttv_format *fmt;
3269
3270 if(!capable(CAP_SYS_ADMIN) &&
3271 !capable(CAP_SYS_RAWIO))
3272 return -EPERM;
3273
3274 /* check args */
3275 fmt = format_by_fourcc(fb->fmt.pixelformat);
3276 if (NULL == fmt)
3277 return -EINVAL;
3278 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
3279 return -EINVAL;
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 retval = -EINVAL;
3282 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
Michael Schimeke5bd0262007-01-18 16:17:39 -03003283 __s32 width = fb->fmt.width;
3284 __s32 height = fb->fmt.height;
3285
3286 retval = limit_scaled_size(fh, &width, &height,
3287 V4L2_FIELD_INTERLACED,
3288 /* width_mask */ ~3,
3289 /* width_bias */ 2,
3290 /* adjust_size */ 0,
3291 /* adjust_crop */ 0);
3292 if (0 != retval)
3293 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 }
3295
3296 /* ok, accept it */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003297 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 btv->fbuf.base = fb->base;
3299 btv->fbuf.fmt.width = fb->fmt.width;
3300 btv->fbuf.fmt.height = fb->fmt.height;
3301 if (0 != fb->fmt.bytesperline)
3302 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
3303 else
3304 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
3305
3306 retval = 0;
3307 fh->ovfmt = fmt;
3308 btv->init.ovfmt = fmt;
3309 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
3310 fh->ov.w.left = 0;
3311 fh->ov.w.top = 0;
3312 fh->ov.w.width = fb->fmt.width;
3313 fh->ov.w.height = fb->fmt.height;
3314 btv->init.ov.w.width = fb->fmt.width;
3315 btv->init.ov.w.height = fb->fmt.height;
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08003316 kfree(fh->ov.clips);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 fh->ov.clips = NULL;
3318 fh->ov.nclips = 0;
3319
3320 if (check_btres(fh, RESOURCE_OVERLAY)) {
3321 struct bttv_buffer *new;
3322
3323 new = videobuf_alloc(sizeof(*new));
Michael Schimeke5bd0262007-01-18 16:17:39 -03003324 new->crop = btv->crop[!!fh->do_crop].rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
3326 retval = bttv_switch_overlay(btv,fh,new);
3327 }
3328 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02003329 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return retval;
3331 }
3332
3333 case VIDIOC_REQBUFS:
3334 return videobuf_reqbufs(bttv_queue(fh),arg);
3335
3336 case VIDIOC_QUERYBUF:
3337 return videobuf_querybuf(bttv_queue(fh),arg);
3338
3339 case VIDIOC_QBUF:
Michael Schimeke5bd0262007-01-18 16:17:39 -03003340 {
3341 int res = bttv_resource(fh);
3342
3343 if (!check_alloc_btres(btv, fh, res))
3344 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 return videobuf_qbuf(bttv_queue(fh),arg);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
3348 case VIDIOC_DQBUF:
3349 return videobuf_dqbuf(bttv_queue(fh),arg,
3350 file->f_flags & O_NONBLOCK);
3351
3352 case VIDIOC_STREAMON:
3353 {
3354 int res = bttv_resource(fh);
3355
3356 if (!check_alloc_btres(btv,fh,res))
3357 return -EBUSY;
3358 return videobuf_streamon(bttv_queue(fh));
3359 }
3360 case VIDIOC_STREAMOFF:
3361 {
3362 int res = bttv_resource(fh);
3363
3364 retval = videobuf_streamoff(bttv_queue(fh));
3365 if (retval < 0)
3366 return retval;
3367 free_btres(btv,fh,res);
3368 return 0;
3369 }
3370
3371 case VIDIOC_QUERYCTRL:
3372 {
3373 struct v4l2_queryctrl *c = arg;
3374 int i;
3375
3376 if ((c->id < V4L2_CID_BASE ||
3377 c->id >= V4L2_CID_LASTP1) &&
3378 (c->id < V4L2_CID_PRIVATE_BASE ||
3379 c->id >= V4L2_CID_PRIVATE_LASTP1))
3380 return -EINVAL;
3381 for (i = 0; i < BTTV_CTLS; i++)
3382 if (bttv_ctls[i].id == c->id)
3383 break;
3384 if (i == BTTV_CTLS) {
3385 *c = no_ctl;
3386 return 0;
3387 }
3388 *c = bttv_ctls[i];
Hans Verkuil0020d3e2006-03-30 19:50:34 -03003389 if (btv->audio_hook && i >= 4 && i <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 struct video_audio va;
3391 memset(&va,0,sizeof(va));
Hans Verkuil0020d3e2006-03-30 19:50:34 -03003392 btv->audio_hook(btv,&va,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 switch (bttv_ctls[i].id) {
3394 case V4L2_CID_AUDIO_VOLUME:
3395 if (!(va.flags & VIDEO_AUDIO_VOLUME))
3396 *c = no_ctl;
3397 break;
3398 case V4L2_CID_AUDIO_BALANCE:
3399 if (!(va.flags & VIDEO_AUDIO_BALANCE))
3400 *c = no_ctl;
3401 break;
3402 case V4L2_CID_AUDIO_BASS:
3403 if (!(va.flags & VIDEO_AUDIO_BASS))
3404 *c = no_ctl;
3405 break;
3406 case V4L2_CID_AUDIO_TREBLE:
3407 if (!(va.flags & VIDEO_AUDIO_TREBLE))
3408 *c = no_ctl;
3409 break;
3410 }
3411 }
3412 return 0;
3413 }
3414 case VIDIOC_G_CTRL:
3415 return get_control(btv,arg);
3416 case VIDIOC_S_CTRL:
3417 return set_control(btv,arg);
3418 case VIDIOC_G_PARM:
3419 {
3420 struct v4l2_streamparm *parm = arg;
3421 struct v4l2_standard s;
3422 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
3423 return -EINVAL;
3424 memset(parm,0,sizeof(*parm));
3425 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
3426 bttv_tvnorms[btv->tvnorm].name);
3427 parm->parm.capture.timeperframe = s.frameperiod;
3428 return 0;
3429 }
3430
3431 case VIDIOC_G_PRIORITY:
3432 {
3433 enum v4l2_priority *p = arg;
3434
3435 *p = v4l2_prio_max(&btv->prio);
3436 return 0;
3437 }
3438 case VIDIOC_S_PRIORITY:
3439 {
3440 enum v4l2_priority *prio = arg;
3441
3442 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
3443 }
3444
Michael Schimeke5bd0262007-01-18 16:17:39 -03003445 case VIDIOC_CROPCAP:
3446 {
3447 struct v4l2_cropcap *cap = arg;
3448 enum v4l2_buf_type type;
3449
3450 type = cap->type;
3451
3452 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3453 type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3454 return -EINVAL;
3455
3456 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3457 cap->type = type;
3458
3459 return 0;
3460 }
3461 case VIDIOC_G_CROP:
3462 {
3463 struct v4l2_crop * crop = arg;
3464
3465 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3466 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3467 return -EINVAL;
3468
3469 /* No fh->do_crop = 1; because btv->crop[1] may be
3470 inconsistent with fh->width or fh->height and apps
3471 do not expect a change here. */
3472
3473 crop->c = btv->crop[!!fh->do_crop].rect;
3474
3475 return 0;
3476 }
3477 case VIDIOC_S_CROP:
3478 {
3479 struct v4l2_crop *crop = arg;
3480 const struct v4l2_rect *b;
3481 struct bttv_crop c;
3482 __s32 b_left;
3483 __s32 b_top;
3484 __s32 b_right;
3485 __s32 b_bottom;
3486
3487 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3488 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3489 return -EINVAL;
3490
3491 retval = v4l2_prio_check(&btv->prio,&fh->prio);
3492 if (0 != retval)
3493 return retval;
3494
3495 /* Make sure tvnorm, vbi_end and the current cropping
3496 parameters remain consistent until we're done. Note
3497 read() may change vbi_end in check_alloc_btres(). */
3498 mutex_lock(&btv->lock);
3499
3500 retval = -EBUSY;
3501
3502 if (locked_btres(fh->btv, VIDEO_RESOURCES))
3503 goto btv_unlock_and_return;
3504
3505 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3506
3507 b_left = b->left;
3508 b_right = b_left + b->width;
3509 b_bottom = b->top + b->height;
3510
3511 b_top = max(b->top, btv->vbi_end);
3512 if (b_top + 32 >= b_bottom)
3513 goto btv_unlock_and_return;
3514
3515 /* Min. scaled size 48 x 32. */
3516 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3517 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3518
3519 c.rect.width = clamp(crop->c.width,
3520 48, b_right - c.rect.left);
3521
3522 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3523 /* Top and height must be a multiple of two. */
3524 c.rect.top = (c.rect.top + 1) & ~1;
3525
3526 c.rect.height = clamp(crop->c.height,
3527 32, b_bottom - c.rect.top);
3528 c.rect.height = (c.rect.height + 1) & ~1;
3529
3530 bttv_crop_calc_limits(&c);
3531
3532 btv->crop[1] = c;
3533
3534 mutex_unlock(&btv->lock);
3535
3536 fh->do_crop = 1;
3537
3538 mutex_lock(&fh->cap.lock);
3539
3540 if (fh->width < c.min_scaled_width) {
3541 fh->width = c.min_scaled_width;
3542 btv->init.width = c.min_scaled_width;
3543 } else if (fh->width > c.max_scaled_width) {
3544 fh->width = c.max_scaled_width;
3545 btv->init.width = c.max_scaled_width;
3546 }
3547
3548 if (fh->height < c.min_scaled_height) {
3549 fh->height = c.min_scaled_height;
3550 btv->init.height = c.min_scaled_height;
3551 } else if (fh->height > c.max_scaled_height) {
3552 fh->height = c.max_scaled_height;
3553 btv->init.height = c.max_scaled_height;
3554 }
3555
3556 mutex_unlock(&fh->cap.lock);
3557
3558 return 0;
3559 }
3560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 case VIDIOC_ENUMSTD:
3562 case VIDIOC_G_STD:
3563 case VIDIOC_S_STD:
3564 case VIDIOC_ENUMINPUT:
3565 case VIDIOC_G_INPUT:
3566 case VIDIOC_S_INPUT:
3567 case VIDIOC_G_TUNER:
3568 case VIDIOC_S_TUNER:
3569 case VIDIOC_G_FREQUENCY:
3570 case VIDIOC_S_FREQUENCY:
Hans Verkuil299392b2005-11-08 21:37:42 -08003571 case VIDIOC_LOG_STATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 return bttv_common_ioctls(btv,cmd,arg);
3573
3574 default:
3575 return -ENOIOCTLCMD;
3576 }
3577 return 0;
3578
3579 fh_unlock_and_return:
Ingo Molnar3593cab2006-02-07 06:49:14 -02003580 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 return retval;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003582
3583 btv_unlock_and_return:
3584 mutex_unlock(&btv->lock);
3585 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
3588static int bttv_ioctl(struct inode *inode, struct file *file,
3589 unsigned int cmd, unsigned long arg)
3590{
3591 struct bttv_fh *fh = file->private_data;
3592
3593 switch (cmd) {
3594 case BTTV_VBISIZE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03003595 {
3596 const struct bttv_tvnorm *tvnorm;
3597
3598 tvnorm = fh->vbi_fmt.tvnorm;
3599
3600 if (fh->vbi_fmt.fmt.start[0] != tvnorm->vbistart[0] ||
3601 fh->vbi_fmt.fmt.start[1] != tvnorm->vbistart[1] ||
3602 fh->vbi_fmt.fmt.count[0] != fh->vbi_fmt.fmt.count[1]) {
3603 /* BTTV_VBISIZE cannot express these parameters,
3604 however open() resets the paramters to defaults
3605 and apps shouldn't call BTTV_VBISIZE after
3606 VIDIOC_S_FMT. */
3607 return -EINVAL;
3608 }
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003611 return (fh->vbi_fmt.fmt.count[0] * 2
3612 * fh->vbi_fmt.fmt.samples_per_line);
3613 }
3614
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 default:
3616 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
3617 }
3618}
3619
3620static ssize_t bttv_read(struct file *file, char __user *data,
3621 size_t count, loff_t *ppos)
3622{
3623 struct bttv_fh *fh = file->private_data;
3624 int retval = 0;
3625
3626 if (fh->btv->errors)
3627 bttv_reinit_bt848(fh->btv);
3628 dprintk("bttv%d: read count=%d type=%s\n",
3629 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3630
3631 switch (fh->type) {
3632 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Michael Schimeke5bd0262007-01-18 16:17:39 -03003633 if (!check_alloc_btres(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3634 /* VIDEO_READ in use by another fh,
3635 or VIDEO_STREAM by any fh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 return -EBUSY;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3639 file->f_flags & O_NONBLOCK);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003640 free_btres(fh->btv, fh, RESOURCE_VIDEO_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 break;
3642 case V4L2_BUF_TYPE_VBI_CAPTURE:
3643 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3644 return -EBUSY;
3645 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3646 file->f_flags & O_NONBLOCK);
3647 break;
3648 default:
3649 BUG();
3650 }
3651 return retval;
3652}
3653
3654static unsigned int bttv_poll(struct file *file, poll_table *wait)
3655{
3656 struct bttv_fh *fh = file->private_data;
3657 struct bttv_buffer *buf;
3658 enum v4l2_field field;
3659
3660 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3661 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3662 return POLLERR;
3663 return videobuf_poll_stream(file, &fh->vbi, wait);
3664 }
3665
Michael Schimeke5bd0262007-01-18 16:17:39 -03003666 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 /* streaming capture */
3668 if (list_empty(&fh->cap.stream))
3669 return POLLERR;
3670 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3671 } else {
3672 /* read() capture */
Ingo Molnar3593cab2006-02-07 06:49:14 -02003673 mutex_lock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 if (NULL == fh->cap.read_buf) {
3675 /* need to capture a new frame */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003676 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02003677 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return POLLERR;
3679 }
3680 fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
3681 if (NULL == fh->cap.read_buf) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02003682 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 return POLLERR;
3684 }
3685 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3686 field = videobuf_next_field(&fh->cap);
3687 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -08003688 kfree (fh->cap.read_buf);
3689 fh->cap.read_buf = NULL;
Ingo Molnar3593cab2006-02-07 06:49:14 -02003690 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 return POLLERR;
3692 }
3693 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3694 fh->cap.read_off = 0;
3695 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02003696 mutex_unlock(&fh->cap.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 buf = (struct bttv_buffer*)fh->cap.read_buf;
3698 }
3699
3700 poll_wait(file, &buf->vb.done, wait);
3701 if (buf->vb.state == STATE_DONE ||
3702 buf->vb.state == STATE_ERROR)
3703 return POLLIN|POLLRDNORM;
3704 return 0;
3705}
3706
3707static int bttv_open(struct inode *inode, struct file *file)
3708{
3709 int minor = iminor(inode);
3710 struct bttv *btv = NULL;
3711 struct bttv_fh *fh;
3712 enum v4l2_buf_type type = 0;
3713 unsigned int i;
3714
3715 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3716
3717 for (i = 0; i < bttv_num; i++) {
3718 if (bttvs[i].video_dev &&
3719 bttvs[i].video_dev->minor == minor) {
3720 btv = &bttvs[i];
3721 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3722 break;
3723 }
3724 if (bttvs[i].vbi_dev &&
3725 bttvs[i].vbi_dev->minor == minor) {
3726 btv = &bttvs[i];
3727 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3728 break;
3729 }
3730 }
3731 if (NULL == btv)
3732 return -ENODEV;
3733
3734 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3735 btv->c.nr,v4l2_type_names[type]);
3736
3737 /* allocate per filehandle data */
3738 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3739 if (NULL == fh)
3740 return -ENOMEM;
3741 file->private_data = fh;
3742 *fh = btv->init;
3743 fh->type = type;
3744 fh->ov.setup_ok = 0;
3745 v4l2_prio_open(&btv->prio,&fh->prio);
3746
3747 videobuf_queue_init(&fh->cap, &bttv_video_qops,
3748 btv->c.pci, &btv->s_lock,
3749 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3750 V4L2_FIELD_INTERLACED,
3751 sizeof(struct bttv_buffer),
3752 fh);
3753 videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
3754 btv->c.pci, &btv->s_lock,
3755 V4L2_BUF_TYPE_VBI_CAPTURE,
3756 V4L2_FIELD_SEQ_TB,
3757 sizeof(struct bttv_buffer),
3758 fh);
3759 i2c_vidiocschan(btv);
3760
3761 btv->users++;
Michael Schimeke5bd0262007-01-18 16:17:39 -03003762
3763 /* The V4L2 spec requires one global set of cropping parameters
3764 which only change on request. These are stored in btv->crop[1].
3765 However for compatibility with V4L apps and cropping unaware
3766 V4L2 apps we now reset the cropping parameters as seen through
3767 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3768 will use btv->crop[0], the default cropping parameters for the
3769 current video standard, and VIDIOC_S_FMT will not implicitely
3770 change the cropping parameters until VIDIOC_S_CROP has been
3771 called. */
3772 fh->do_crop = !reset_crop; /* module parameter */
3773
3774 /* Likewise there should be one global set of VBI capture
3775 parameters, but for compatibility with V4L apps and earlier
3776 driver versions each fh has its own parameters. */
3777 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3778
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 bttv_field_count(btv);
3780 return 0;
3781}
3782
3783static int bttv_release(struct inode *inode, struct file *file)
3784{
3785 struct bttv_fh *fh = file->private_data;
3786 struct bttv *btv = fh->btv;
3787
3788 /* turn off overlay */
3789 if (check_btres(fh, RESOURCE_OVERLAY))
3790 bttv_switch_overlay(btv,fh,NULL);
3791
3792 /* stop video capture */
Michael Schimeke5bd0262007-01-18 16:17:39 -03003793 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 videobuf_streamoff(&fh->cap);
Michael Schimeke5bd0262007-01-18 16:17:39 -03003795 free_btres(btv,fh,RESOURCE_VIDEO_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 }
3797 if (fh->cap.read_buf) {
3798 buffer_release(&fh->cap,fh->cap.read_buf);
3799 kfree(fh->cap.read_buf);
3800 }
Michael Schimeke5bd0262007-01-18 16:17:39 -03003801 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3802 free_btres(btv, fh, RESOURCE_VIDEO_READ);
3803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
3805 /* stop vbi capture */
3806 if (check_btres(fh, RESOURCE_VBI)) {
3807 if (fh->vbi.streaming)
3808 videobuf_streamoff(&fh->vbi);
3809 if (fh->vbi.reading)
3810 videobuf_read_stop(&fh->vbi);
3811 free_btres(btv,fh,RESOURCE_VBI);
3812 }
3813
3814 /* free stuff */
3815 videobuf_mmap_free(&fh->cap);
3816 videobuf_mmap_free(&fh->vbi);
3817 v4l2_prio_close(&btv->prio,&fh->prio);
3818 file->private_data = NULL;
3819 kfree(fh);
3820
3821 btv->users--;
3822 bttv_field_count(btv);
3823 return 0;
3824}
3825
3826static int
3827bttv_mmap(struct file *file, struct vm_area_struct *vma)
3828{
3829 struct bttv_fh *fh = file->private_data;
3830
3831 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3832 fh->btv->c.nr, v4l2_type_names[fh->type],
3833 vma->vm_start, vma->vm_end - vma->vm_start);
3834 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3835}
3836
Arjan van de Venfa027c22007-02-12 00:55:33 -08003837static const struct file_operations bttv_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
3839 .owner = THIS_MODULE,
3840 .open = bttv_open,
3841 .release = bttv_release,
3842 .ioctl = bttv_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02003843 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 .llseek = no_llseek,
3845 .read = bttv_read,
3846 .mmap = bttv_mmap,
3847 .poll = bttv_poll,
3848};
3849
3850static struct video_device bttv_video_template =
3851{
3852 .name = "UNSET",
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07003853 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003854 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 .hardware = VID_HARDWARE_BT848,
3856 .fops = &bttv_fops,
3857 .minor = -1,
3858};
3859
3860static struct video_device bttv_vbi_template =
3861{
3862 .name = "bt848/878 vbi",
3863 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3864 .hardware = VID_HARDWARE_BT848,
3865 .fops = &bttv_fops,
3866 .minor = -1,
3867};
3868
3869/* ----------------------------------------------------------------------- */
3870/* radio interface */
3871
3872static int radio_open(struct inode *inode, struct file *file)
3873{
3874 int minor = iminor(inode);
3875 struct bttv *btv = NULL;
3876 unsigned int i;
3877
3878 dprintk("bttv: open minor=%d\n",minor);
3879
3880 for (i = 0; i < bttv_num; i++) {
3881 if (bttvs[i].radio_dev->minor == minor) {
3882 btv = &bttvs[i];
3883 break;
3884 }
3885 }
3886 if (NULL == btv)
3887 return -ENODEV;
3888
3889 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02003890 mutex_lock(&btv->lock);
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 btv->radio_user++;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003893
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 file->private_data = btv;
3895
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03003896 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,NULL);
3897 audio_input(btv,TVAUDIO_INPUT_RADIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02003899 mutex_unlock(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901}
3902
3903static int radio_release(struct inode *inode, struct file *file)
3904{
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003905 struct bttv *btv = file->private_data;
3906 struct rds_command cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
3908 btv->radio_user--;
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003909
3910 bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 return 0;
3913}
3914
3915static int radio_do_ioctl(struct inode *inode, struct file *file,
3916 unsigned int cmd, void *arg)
3917{
3918 struct bttv *btv = file->private_data;
3919
3920 switch (cmd) {
3921 case VIDIOCGCAP:
3922 {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003923 struct video_capability *cap = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924
3925 memset(cap,0,sizeof(*cap));
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003926 strcpy(cap->name,btv->radio_dev->name);
3927 cap->type = VID_TYPE_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 cap->channels = 1;
3929 cap->audios = 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
3932
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003933 case VIDIOCGTUNER:
3934 {
3935 struct video_tuner *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003937 if(v->tuner)
3938 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 memset(v,0,sizeof(*v));
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003940 strcpy(v->name, "Radio");
3941 bttv_call_i2c_clients(btv,cmd,v);
3942 return 0;
3943 }
3944 case VIDIOCSTUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 /* nothing to do */
3946 return 0;
3947
3948 case BTTV_VERSION:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08003949 case VIDIOCGFREQ:
3950 case VIDIOCSFREQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 case VIDIOCGAUDIO:
3952 case VIDIOCSAUDIO:
Hans Verkuil5af0c8f2006-01-09 18:21:37 -02003953 case VIDIOC_LOG_STATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 return bttv_common_ioctls(btv,cmd,arg);
3955
3956 default:
3957 return -ENOIOCTLCMD;
3958 }
3959 return 0;
3960}
3961
3962static int radio_ioctl(struct inode *inode, struct file *file,
3963 unsigned int cmd, unsigned long arg)
3964{
3965 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3966}
3967
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003968static ssize_t radio_read(struct file *file, char __user *data,
3969 size_t count, loff_t *ppos)
3970{
3971 struct bttv *btv = file->private_data;
3972 struct rds_command cmd;
3973 cmd.block_count = count/3;
3974 cmd.buffer = data;
3975 cmd.instance = file;
3976 cmd.result = -ENODEV;
3977
3978 bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
3979
3980 return cmd.result;
3981}
3982
3983static unsigned int radio_poll(struct file *file, poll_table *wait)
3984{
3985 struct bttv *btv = file->private_data;
3986 struct rds_command cmd;
3987 cmd.instance = file;
3988 cmd.event_list = wait;
3989 cmd.result = -ENODEV;
3990 bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
3991
3992 return cmd.result;
3993}
3994
Arjan van de Venfa027c22007-02-12 00:55:33 -08003995static const struct file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
3997 .owner = THIS_MODULE,
3998 .open = radio_open,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07003999 .read = radio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 .release = radio_release,
4001 .ioctl = radio_ioctl,
4002 .llseek = no_llseek,
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -07004003 .poll = radio_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004};
4005
4006static struct video_device radio_template =
4007{
4008 .name = "bt848/878 radio",
4009 .type = VID_TYPE_TUNER,
4010 .hardware = VID_HARDWARE_BT848,
4011 .fops = &radio_fops,
4012 .minor = -1,
4013};
4014
4015/* ----------------------------------------------------------------------- */
4016/* some debug code */
4017
Adrian Bunk408b6642005-05-01 08:59:29 -07004018static int bttv_risc_decode(u32 risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
4020 static char *instr[16] = {
4021 [ BT848_RISC_WRITE >> 28 ] = "write",
4022 [ BT848_RISC_SKIP >> 28 ] = "skip",
4023 [ BT848_RISC_WRITEC >> 28 ] = "writec",
4024 [ BT848_RISC_JUMP >> 28 ] = "jump",
4025 [ BT848_RISC_SYNC >> 28 ] = "sync",
4026 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
4027 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
4028 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
4029 };
4030 static int incr[16] = {
4031 [ BT848_RISC_WRITE >> 28 ] = 2,
4032 [ BT848_RISC_JUMP >> 28 ] = 2,
4033 [ BT848_RISC_SYNC >> 28 ] = 2,
4034 [ BT848_RISC_WRITE123 >> 28 ] = 5,
4035 [ BT848_RISC_SKIP123 >> 28 ] = 2,
4036 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
4037 };
4038 static char *bits[] = {
4039 "be0", "be1", "be2", "be3/resync",
4040 "set0", "set1", "set2", "set3",
4041 "clr0", "clr1", "clr2", "clr3",
4042 "irq", "res", "eol", "sol",
4043 };
4044 int i;
4045
4046 printk("0x%08x [ %s", risc,
4047 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
4048 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
4049 if (risc & (1 << (i + 12)))
4050 printk(" %s",bits[i]);
4051 printk(" count=%d ]\n", risc & 0xfff);
4052 return incr[risc >> 28] ? incr[risc >> 28] : 1;
4053}
4054
Adrian Bunk408b6642005-05-01 08:59:29 -07004055static void bttv_risc_disasm(struct bttv *btv,
4056 struct btcx_riscmem *risc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
4058 unsigned int i,j,n;
4059
4060 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
4061 btv->c.name, risc->cpu, (unsigned long)risc->dma);
4062 for (i = 0; i < (risc->size >> 2); i += n) {
4063 printk("%s: 0x%lx: ", btv->c.name,
4064 (unsigned long)(risc->dma + (i<<2)));
4065 n = bttv_risc_decode(risc->cpu[i]);
4066 for (j = 1; j < n; j++)
4067 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
4068 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
4069 risc->cpu[i+j], j);
4070 if (0 == risc->cpu[i])
4071 break;
4072 }
4073}
4074
4075static void bttv_print_riscaddr(struct bttv *btv)
4076{
4077 printk(" main: %08Lx\n",
4078 (unsigned long long)btv->main.dma);
4079 printk(" vbi : o=%08Lx e=%08Lx\n",
4080 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
4081 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
4082 printk(" cap : o=%08Lx e=%08Lx\n",
4083 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
4084 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
4085 printk(" scr : o=%08Lx e=%08Lx\n",
4086 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
4087 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
4088 bttv_risc_disasm(btv, &btv->main);
4089}
4090
4091/* ----------------------------------------------------------------------- */
4092/* irq handler */
4093
4094static char *irq_name[] = {
4095 "FMTCHG", // format change detected (525 vs. 625)
4096 "VSYNC", // vertical sync (new field)
4097 "HSYNC", // horizontal sync
4098 "OFLOW", // chroma/luma AGC overflow
4099 "HLOCK", // horizontal lock changed
4100 "VPRES", // video presence changed
4101 "6", "7",
4102 "I2CDONE", // hw irc operation finished
4103 "GPINT", // gpio port triggered irq
4104 "10",
4105 "RISCI", // risc instruction triggered irq
4106 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
4107 "FTRGT", // pixel data fifo overrun
4108 "FDSR", // fifo data stream resyncronisation
4109 "PPERR", // parity error (data transfer)
4110 "RIPERR", // parity error (read risc instructions)
4111 "PABORT", // pci abort
4112 "OCERR", // risc instruction error
4113 "SCERR", // syncronisation error
4114};
4115
4116static void bttv_print_irqbits(u32 print, u32 mark)
4117{
4118 unsigned int i;
4119
4120 printk("bits:");
4121 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
4122 if (print & (1 << i))
4123 printk(" %s",irq_name[i]);
4124 if (mark & (1 << i))
4125 printk("*");
4126 }
4127}
4128
4129static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
4130{
4131 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
4132 btv->c.nr,
4133 (unsigned long)btv->main.dma,
4134 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
4135 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
4136 (unsigned long)rc);
4137
4138 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
4139 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
4140 "Ok, then this is harmless, don't worry ;)\n",
4141 btv->c.nr);
4142 return;
4143 }
4144 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
4145 btv->c.nr);
4146 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
4147 btv->c.nr);
4148 dump_stack();
4149}
4150
4151static int
4152bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
4153{
4154 struct bttv_buffer *item;
4155
4156 memset(set,0,sizeof(*set));
4157
4158 /* capture request ? */
4159 if (!list_empty(&btv->capture)) {
4160 set->frame_irq = 1;
4161 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
4162 if (V4L2_FIELD_HAS_TOP(item->vb.field))
4163 set->top = item;
4164 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
4165 set->bottom = item;
4166
4167 /* capture request for other field ? */
4168 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
4169 (item->vb.queue.next != &btv->capture)) {
4170 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
4171 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
4172 if (NULL == set->top &&
4173 V4L2_FIELD_TOP == item->vb.field) {
4174 set->top = item;
4175 }
4176 if (NULL == set->bottom &&
4177 V4L2_FIELD_BOTTOM == item->vb.field) {
4178 set->bottom = item;
4179 }
4180 if (NULL != set->top && NULL != set->bottom)
4181 set->top_irq = 2;
4182 }
4183 }
4184 }
4185
4186 /* screen overlay ? */
4187 if (NULL != btv->screen) {
4188 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
4189 if (NULL == set->top && NULL == set->bottom) {
4190 set->top = btv->screen;
4191 set->bottom = btv->screen;
4192 }
4193 } else {
4194 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
4195 NULL == set->top) {
4196 set->top = btv->screen;
4197 }
4198 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
4199 NULL == set->bottom) {
4200 set->bottom = btv->screen;
4201 }
4202 }
4203 }
4204
4205 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
4206 btv->c.nr,set->top, set->bottom,
4207 btv->screen,set->frame_irq,set->top_irq);
4208 return 0;
4209}
4210
4211static void
4212bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
4213 struct bttv_buffer_set *curr, unsigned int state)
4214{
4215 struct timeval ts;
4216
4217 do_gettimeofday(&ts);
4218
4219 if (wakeup->top == wakeup->bottom) {
4220 if (NULL != wakeup->top && curr->top != wakeup->top) {
4221 if (irq_debug > 1)
4222 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
4223 wakeup->top->vb.ts = ts;
4224 wakeup->top->vb.field_count = btv->field_count;
4225 wakeup->top->vb.state = state;
4226 wake_up(&wakeup->top->vb.done);
4227 }
4228 } else {
4229 if (NULL != wakeup->top && curr->top != wakeup->top) {
4230 if (irq_debug > 1)
4231 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
4232 wakeup->top->vb.ts = ts;
4233 wakeup->top->vb.field_count = btv->field_count;
4234 wakeup->top->vb.state = state;
4235 wake_up(&wakeup->top->vb.done);
4236 }
4237 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
4238 if (irq_debug > 1)
4239 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
4240 wakeup->bottom->vb.ts = ts;
4241 wakeup->bottom->vb.field_count = btv->field_count;
4242 wakeup->bottom->vb.state = state;
4243 wake_up(&wakeup->bottom->vb.done);
4244 }
4245 }
4246}
4247
4248static void
4249bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
4250 unsigned int state)
4251{
4252 struct timeval ts;
4253
4254 if (NULL == wakeup)
4255 return;
4256
4257 do_gettimeofday(&ts);
4258 wakeup->vb.ts = ts;
4259 wakeup->vb.field_count = btv->field_count;
4260 wakeup->vb.state = state;
4261 wake_up(&wakeup->vb.done);
4262}
4263
4264static void bttv_irq_timeout(unsigned long data)
4265{
4266 struct bttv *btv = (struct bttv *)data;
4267 struct bttv_buffer_set old,new;
4268 struct bttv_buffer *ovbi;
4269 struct bttv_buffer *item;
4270 unsigned long flags;
4271
4272 if (bttv_verbose) {
4273 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
4274 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
4275 btread(BT848_RISC_COUNT));
4276 bttv_print_irqbits(btread(BT848_INT_STAT),0);
4277 printk("\n");
4278 }
4279
4280 spin_lock_irqsave(&btv->s_lock,flags);
4281
4282 /* deactivate stuff */
4283 memset(&new,0,sizeof(new));
4284 old = btv->curr;
4285 ovbi = btv->cvbi;
4286 btv->curr = new;
4287 btv->cvbi = NULL;
4288 btv->loop_irq = 0;
4289 bttv_buffer_activate_video(btv, &new);
4290 bttv_buffer_activate_vbi(btv, NULL);
4291 bttv_set_dma(btv, 0);
4292
4293 /* wake up */
4294 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
4295 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
4296
4297 /* cancel all outstanding capture / vbi requests */
4298 while (!list_empty(&btv->capture)) {
4299 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
4300 list_del(&item->vb.queue);
4301 item->vb.state = STATE_ERROR;
4302 wake_up(&item->vb.done);
4303 }
4304 while (!list_empty(&btv->vcapture)) {
4305 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4306 list_del(&item->vb.queue);
4307 item->vb.state = STATE_ERROR;
4308 wake_up(&item->vb.done);
4309 }
4310
4311 btv->errors++;
4312 spin_unlock_irqrestore(&btv->s_lock,flags);
4313}
4314
4315static void
4316bttv_irq_wakeup_top(struct bttv *btv)
4317{
4318 struct bttv_buffer *wakeup = btv->curr.top;
4319
4320 if (NULL == wakeup)
4321 return;
4322
4323 spin_lock(&btv->s_lock);
4324 btv->curr.top_irq = 0;
4325 btv->curr.top = NULL;
4326 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
4327
4328 do_gettimeofday(&wakeup->vb.ts);
4329 wakeup->vb.field_count = btv->field_count;
4330 wakeup->vb.state = STATE_DONE;
4331 wake_up(&wakeup->vb.done);
4332 spin_unlock(&btv->s_lock);
4333}
4334
4335static inline int is_active(struct btcx_riscmem *risc, u32 rc)
4336{
4337 if (rc < risc->dma)
4338 return 0;
4339 if (rc > risc->dma + risc->size)
4340 return 0;
4341 return 1;
4342}
4343
4344static void
4345bttv_irq_switch_video(struct bttv *btv)
4346{
4347 struct bttv_buffer_set new;
4348 struct bttv_buffer_set old;
4349 dma_addr_t rc;
4350
4351 spin_lock(&btv->s_lock);
4352
4353 /* new buffer set */
4354 bttv_irq_next_video(btv, &new);
4355 rc = btread(BT848_RISC_COUNT);
4356 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
4357 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
4358 btv->framedrop++;
4359 if (debug_latency)
4360 bttv_irq_debug_low_latency(btv, rc);
4361 spin_unlock(&btv->s_lock);
4362 return;
4363 }
4364
4365 /* switch over */
4366 old = btv->curr;
4367 btv->curr = new;
4368 btv->loop_irq &= ~1;
4369 bttv_buffer_activate_video(btv, &new);
4370 bttv_set_dma(btv, 0);
4371
4372 /* switch input */
4373 if (UNSET != btv->new_input) {
4374 video_mux(btv,btv->new_input);
4375 btv->new_input = UNSET;
4376 }
4377
4378 /* wake up finished buffers */
4379 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
4380 spin_unlock(&btv->s_lock);
4381}
4382
4383static void
4384bttv_irq_switch_vbi(struct bttv *btv)
4385{
4386 struct bttv_buffer *new = NULL;
4387 struct bttv_buffer *old;
4388 u32 rc;
4389
4390 spin_lock(&btv->s_lock);
4391
4392 if (!list_empty(&btv->vcapture))
4393 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4394 old = btv->cvbi;
4395
4396 rc = btread(BT848_RISC_COUNT);
4397 if (NULL != old && (is_active(&old->top, rc) ||
4398 is_active(&old->bottom, rc))) {
4399 btv->framedrop++;
4400 if (debug_latency)
4401 bttv_irq_debug_low_latency(btv, rc);
4402 spin_unlock(&btv->s_lock);
4403 return;
4404 }
4405
4406 /* switch */
4407 btv->cvbi = new;
4408 btv->loop_irq &= ~4;
4409 bttv_buffer_activate_vbi(btv, new);
4410 bttv_set_dma(btv, 0);
4411
4412 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
4413 spin_unlock(&btv->s_lock);
4414}
4415
David Howells7d12e782006-10-05 14:55:46 +01004416static irqreturn_t bttv_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417{
4418 u32 stat,astat;
4419 u32 dstat;
4420 int count;
4421 struct bttv *btv;
4422 int handled = 0;
4423
4424 btv=(struct bttv *)dev_id;
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004425
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004426 if (btv->custom_irq)
4427 handled = btv->custom_irq(btv);
Mark Weaver6c6c0b22005-11-13 16:07:52 -08004428
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 count=0;
4430 while (1) {
4431 /* get/clear interrupt status bits */
4432 stat=btread(BT848_INT_STAT);
4433 astat=stat&btread(BT848_INT_MASK);
4434 if (!astat)
4435 break;
4436 handled = 1;
4437 btwrite(stat,BT848_INT_STAT);
4438
4439 /* get device status bits */
4440 dstat=btread(BT848_DSTATUS);
4441
4442 if (irq_debug) {
4443 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4444 "riscs=%x, riscc=%08x, ",
4445 btv->c.nr, count, btv->field_count,
4446 stat>>28, btread(BT848_RISC_COUNT));
4447 bttv_print_irqbits(stat,astat);
4448 if (stat & BT848_INT_HLOCK)
4449 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4450 ? "yes" : "no");
4451 if (stat & BT848_INT_VPRES)
4452 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4453 ? "yes" : "no");
4454 if (stat & BT848_INT_FMTCHG)
4455 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4456 ? "625" : "525");
4457 printk("\n");
4458 }
4459
4460 if (astat&BT848_INT_VSYNC)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004461 btv->field_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004463 if ((astat & BT848_INT_GPINT) && btv->remote) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004465 bttv_input_irq(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 }
4467
4468 if (astat & BT848_INT_I2CDONE) {
4469 btv->i2c_done = stat;
4470 wake_up(&btv->i2c_queue);
4471 }
4472
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004473 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 bttv_irq_switch_vbi(btv);
4475
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004476 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 bttv_irq_wakeup_top(btv);
4478
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004479 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 bttv_irq_switch_video(btv);
4481
4482 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004483 audio_mute(btv, btv->mute); /* trigger automute */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
4485 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4486 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4487 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4488 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4489 btread(BT848_RISC_COUNT));
4490 bttv_print_irqbits(stat,astat);
4491 printk("\n");
4492 if (bttv_debug)
4493 bttv_print_riscaddr(btv);
4494 }
4495 if (fdsr && astat & BT848_INT_FDSR) {
4496 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4497 btv->c.nr,btread(BT848_RISC_COUNT));
4498 if (bttv_debug)
4499 bttv_print_riscaddr(btv);
4500 }
4501
4502 count++;
4503 if (count > 4) {
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004504
4505 if (count > 8 || !(astat & BT848_INT_GPINT)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004506 btwrite(0, BT848_INT_MASK);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004507
4508 printk(KERN_ERR
4509 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4510 } else {
4511 printk(KERN_ERR
4512 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4513
4514 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4515 BT848_INT_MASK);
4516 };
4517
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 bttv_print_irqbits(stat,astat);
nshmyrev@yandex.ruc58c21c2005-11-08 21:37:41 -08004519
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 printk("]\n");
4521 }
4522 }
4523 btv->irq_total++;
4524 if (handled)
4525 btv->irq_me++;
4526 return IRQ_RETVAL(handled);
4527}
4528
4529
4530/* ----------------------------------------------------------------------- */
4531/* initialitation */
4532
4533static struct video_device *vdev_init(struct bttv *btv,
4534 struct video_device *template,
4535 char *type)
4536{
4537 struct video_device *vfd;
4538
4539 vfd = video_device_alloc();
4540 if (NULL == vfd)
4541 return NULL;
4542 *vfd = *template;
4543 vfd->minor = -1;
4544 vfd->dev = &btv->c.pci->dev;
4545 vfd->release = video_device_release;
4546 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4547 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4548 type, bttv_tvcards[btv->c.type].name);
4549 return vfd;
4550}
4551
4552static void bttv_unregister_video(struct bttv *btv)
4553{
4554 if (btv->video_dev) {
4555 if (-1 != btv->video_dev->minor)
4556 video_unregister_device(btv->video_dev);
4557 else
4558 video_device_release(btv->video_dev);
4559 btv->video_dev = NULL;
4560 }
4561 if (btv->vbi_dev) {
4562 if (-1 != btv->vbi_dev->minor)
4563 video_unregister_device(btv->vbi_dev);
4564 else
4565 video_device_release(btv->vbi_dev);
4566 btv->vbi_dev = NULL;
4567 }
4568 if (btv->radio_dev) {
4569 if (-1 != btv->radio_dev->minor)
4570 video_unregister_device(btv->radio_dev);
4571 else
4572 video_device_release(btv->radio_dev);
4573 btv->radio_dev = NULL;
4574 }
4575}
4576
4577/* register video4linux devices */
4578static int __devinit bttv_register_video(struct bttv *btv)
4579{
Mauro Carvalho Chehab4dcef522005-08-04 12:53:30 -07004580 if (no_overlay <= 0) {
4581 bttv_video_template.type |= VID_TYPE_OVERLAY;
4582 } else {
4583 printk("bttv: Overlay support disabled.\n");
4584 }
4585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 /* video */
4587 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004588 if (NULL == btv->video_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 goto err;
4590 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
4591 goto err;
4592 printk(KERN_INFO "bttv%d: registered device video%d\n",
4593 btv->c.nr,btv->video_dev->minor & 0x1f);
Trent Piephod94fc9a2006-07-29 17:18:06 -03004594 if (class_device_create_file(&btv->video_dev->class_dev,
4595 &class_device_attr_card)<0) {
4596 printk(KERN_ERR "bttv%d: class_device_create_file 'card' "
4597 "failed\n", btv->c.nr);
4598 goto err;
4599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
4601 /* vbi */
4602 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004603 if (NULL == btv->vbi_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 goto err;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004605 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 goto err;
4607 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
4608 btv->c.nr,btv->vbi_dev->minor & 0x1f);
4609
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004610 if (!btv->has_radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 return 0;
4612 /* radio */
4613 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004614 if (NULL == btv->radio_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 goto err;
4616 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
4617 goto err;
4618 printk(KERN_INFO "bttv%d: registered device radio%d\n",
4619 btv->c.nr,btv->radio_dev->minor & 0x1f);
4620
4621 /* all done */
4622 return 0;
4623
4624 err:
4625 bttv_unregister_video(btv);
4626 return -1;
4627}
4628
4629
4630/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4631/* response on cards with no firmware is not enabled by OF */
4632static void pci_set_command(struct pci_dev *dev)
4633{
4634#if defined(__powerpc__)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004635 unsigned int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004637 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4638 cmd = (cmd | PCI_COMMAND_MEMORY );
4639 pci_write_config_dword(dev, PCI_COMMAND, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640#endif
4641}
4642
4643static int __devinit bttv_probe(struct pci_dev *dev,
4644 const struct pci_device_id *pci_id)
4645{
4646 int result;
4647 unsigned char lat;
4648 struct bttv *btv;
4649
4650 if (bttv_num == BTTV_MAX)
4651 return -ENOMEM;
4652 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004653 btv=&bttvs[bttv_num];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 memset(btv,0,sizeof(*btv));
4655 btv->c.nr = bttv_num;
4656 sprintf(btv->c.name,"bttv%d",btv->c.nr);
4657
4658 /* initialize structs / fill in defaults */
Ingo Molnarbd5f0ac2006-01-13 14:10:24 -02004659 mutex_init(&btv->lock);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004660 spin_lock_init(&btv->s_lock);
4661 spin_lock_init(&btv->gpio_lock);
4662 init_waitqueue_head(&btv->gpioq);
4663 init_waitqueue_head(&btv->i2c_queue);
4664 INIT_LIST_HEAD(&btv->c.subs);
4665 INIT_LIST_HEAD(&btv->capture);
4666 INIT_LIST_HEAD(&btv->vcapture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 v4l2_prio_init(&btv->prio);
4668
4669 init_timer(&btv->timeout);
4670 btv->timeout.function = bttv_irq_timeout;
4671 btv->timeout.data = (unsigned long)btv;
4672
Michael Krufky7c08fb02005-11-08 21:36:21 -08004673 btv->i2c_rc = -1;
4674 btv->tuner_type = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 btv->new_input = UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 btv->has_radio=radio[btv->c.nr];
4677
4678 /* pci stuff (init, get irq/mmio, ... */
4679 btv->c.pci = dev;
Michael Krufky7c08fb02005-11-08 21:36:21 -08004680 btv->id = dev->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 if (pci_enable_device(dev)) {
Michael Krufky7c08fb02005-11-08 21:36:21 -08004682 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 btv->c.nr);
4684 return -EIO;
4685 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004686 if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
4687 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 btv->c.nr);
4689 return -EIO;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 if (!request_mem_region(pci_resource_start(dev,0),
4692 pci_resource_len(dev,0),
4693 btv->c.name)) {
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004694 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4695 btv->c.nr,
4696 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 return -EBUSY;
4698 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004699 pci_set_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 pci_set_command(dev);
4701 pci_set_drvdata(dev,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004703 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4704 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4705 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4706 bttv_num,btv->id, btv->revision, pci_name(dev));
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07004707 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4708 btv->c.pci->irq, lat,
4709 (unsigned long long)pci_resource_start(dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 schedule();
4711
Akinobu Mita5f1693f2006-12-20 10:08:56 -03004712 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4713 if (NULL == btv->bt848_mmio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4715 result = -EIO;
4716 goto fail1;
4717 }
4718
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004719 /* identify card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 bttv_idcard(btv);
4721
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004722 /* disable irqs, register irq handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 btwrite(0, BT848_INT_MASK);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004724 result = request_irq(btv->c.pci->irq, bttv_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -07004725 IRQF_SHARED | IRQF_DISABLED,btv->c.name,(void *)btv);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004726 if (result < 0) {
4727 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 bttv_num,btv->c.pci->irq);
4729 goto fail1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
4732 if (0 != bttv_handle_chipset(btv)) {
4733 result = -EIO;
4734 goto fail2;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
4737 /* init options from insmod args */
4738 btv->opt_combfilter = combfilter;
4739 btv->opt_lumafilter = lumafilter;
4740 btv->opt_automute = automute;
4741 btv->opt_chroma_agc = chroma_agc;
4742 btv->opt_adc_crush = adc_crush;
4743 btv->opt_vcr_hack = vcr_hack;
4744 btv->opt_whitecrush_upper = whitecrush_upper;
4745 btv->opt_whitecrush_lower = whitecrush_lower;
Mauro Carvalho Chehab060d3022005-06-28 20:45:25 -07004746 btv->opt_uv_ratio = uv_ratio;
4747 btv->opt_full_luma_range = full_luma_range;
4748 btv->opt_coring = coring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
4750 /* fill struct bttv with some useful defaults */
4751 btv->init.btv = btv;
4752 btv->init.ov.w.width = 320;
4753 btv->init.ov.w.height = 240;
4754 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
4755 btv->init.width = 320;
4756 btv->init.height = 240;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 btv->input = 0;
4758
4759 /* initialize hardware */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004760 if (bttv_gpio)
4761 bttv_gpio_tracking(btv,"pre-init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
4763 bttv_risc_init_main(btv);
4764 init_bt848(btv);
4765
4766 /* gpio */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004767 btwrite(0x00, BT848_GPIO_REG_INP);
4768 btwrite(0x00, BT848_GPIO_OUT_EN);
4769 if (bttv_verbose)
4770 bttv_gpio_tracking(btv,"init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004772 /* needs to be done before i2c is registered */
4773 bttv_init_card1(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004775 /* register i2c + gpio */
4776 init_bttv_i2c(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004778 /* some card-specific stuff (needs working i2c) */
4779 bttv_init_card2(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 init_irqreg(btv);
4781
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004782 /* register video4linux + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 if (!bttv_tvcards[btv->c.type].no_video) {
4784 bttv_register_video(btv);
4785 bt848_bright(btv,32768);
4786 bt848_contrast(btv,32768);
4787 bt848_hue(btv,32768);
4788 bt848_sat(btv,32768);
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -03004789 audio_mute(btv, 1);
Trent Piepho333408f2007-07-03 15:08:10 -03004790 set_input(btv, 0, btv->tvnorm);
Michael Schimeke5bd0262007-01-18 16:17:39 -03004791 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4792 btv->crop[1] = btv->crop[0]; /* current = default */
4793 disclaim_vbi_lines(btv);
4794 disclaim_video_lines(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 }
4796
Jarod Wilsonf992a492007-03-24 15:23:50 -03004797 /* add subdevices and autoload dvb-bt8xx if needed */
4798 if (bttv_tvcards[btv->c.type].has_dvb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 bttv_sub_add_device(&btv->c, "dvb");
Jarod Wilsonf992a492007-03-24 15:23:50 -03004800 request_modules(btv);
4801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004803 bttv_input_init(btv);
4804
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 /* everything is fine */
4806 bttv_num++;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004807 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
4809 fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004810 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
4812 fail1:
4813 if (btv->bt848_mmio)
4814 iounmap(btv->bt848_mmio);
4815 release_mem_region(pci_resource_start(btv->c.pci,0),
4816 pci_resource_len(btv->c.pci,0));
4817 pci_set_drvdata(dev,NULL);
4818 return result;
4819}
4820
4821static void __devexit bttv_remove(struct pci_dev *pci_dev)
4822{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004823 struct bttv *btv = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824
4825 if (bttv_verbose)
4826 printk("bttv%d: unloading\n",btv->c.nr);
4827
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004828 /* shutdown everything (DMA+IRQs) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 btand(~15, BT848_GPIO_DMA_CTL);
4830 btwrite(0, BT848_INT_MASK);
4831 btwrite(~0x0, BT848_INT_STAT);
4832 btwrite(0x0, BT848_GPIO_OUT_EN);
4833 if (bttv_gpio)
4834 bttv_gpio_tracking(btv,"cleanup");
4835
4836 /* tell gpio modules we are leaving ... */
4837 btv->shutdown=1;
4838 wake_up(&btv->gpioq);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -02004839 bttv_input_fini(btv);
Christopher Pascoe889aee82006-01-09 15:25:28 -02004840 bttv_sub_del_devices(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004842 /* unregister i2c_bus + input */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 fini_bttv_i2c(btv);
4844
4845 /* unregister video4linux */
4846 bttv_unregister_video(btv);
4847
4848 /* free allocated memory */
4849 btcx_riscmem_free(btv->c.pci,&btv->main);
4850
4851 /* free ressources */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004852 free_irq(btv->c.pci->irq,btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 iounmap(btv->bt848_mmio);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004854 release_mem_region(pci_resource_start(btv->c.pci,0),
4855 pci_resource_len(btv->c.pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
4857 pci_set_drvdata(pci_dev, NULL);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004858 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859}
4860
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004861#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4863{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004864 struct bttv *btv = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 struct bttv_buffer_set idle;
4866 unsigned long flags;
4867
Mauro Carvalho Chehab0f97a932005-09-09 13:04:05 -07004868 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
4870 /* stop dma + irqs */
4871 spin_lock_irqsave(&btv->s_lock,flags);
4872 memset(&idle, 0, sizeof(idle));
4873 btv->state.video = btv->curr;
4874 btv->state.vbi = btv->cvbi;
4875 btv->state.loop_irq = btv->loop_irq;
4876 btv->curr = idle;
4877 btv->loop_irq = 0;
4878 bttv_buffer_activate_video(btv, &idle);
4879 bttv_buffer_activate_vbi(btv, NULL);
4880 bttv_set_dma(btv, 0);
4881 btwrite(0, BT848_INT_MASK);
4882 spin_unlock_irqrestore(&btv->s_lock,flags);
4883
4884 /* save bt878 state */
4885 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4886 btv->state.gpio_data = gpio_read();
4887
4888 /* save pci state */
4889 pci_save_state(pci_dev);
4890 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4891 pci_disable_device(pci_dev);
4892 btv->state.disabled = 1;
4893 }
4894 return 0;
4895}
4896
4897static int bttv_resume(struct pci_dev *pci_dev)
4898{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004899 struct bttv *btv = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004901 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
4903 dprintk("bttv%d: resume\n", btv->c.nr);
4904
4905 /* restore pci state */
4906 if (btv->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004907 err=pci_enable_device(pci_dev);
4908 if (err) {
4909 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4910 btv->c.nr);
4911 return err;
4912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 btv->state.disabled = 0;
4914 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07004915 err=pci_set_power_state(pci_dev, PCI_D0);
4916 if (err) {
4917 pci_disable_device(pci_dev);
4918 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4919 btv->c.nr);
4920 btv->state.disabled = 1;
4921 return err;
4922 }
4923
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 pci_restore_state(pci_dev);
4925
4926 /* restore bt878 state */
4927 bttv_reinit_bt848(btv);
4928 gpio_inout(0xffffff, btv->state.gpio_enable);
4929 gpio_write(btv->state.gpio_data);
4930
4931 /* restart dma */
4932 spin_lock_irqsave(&btv->s_lock,flags);
4933 btv->curr = btv->state.video;
4934 btv->cvbi = btv->state.vbi;
4935 btv->loop_irq = btv->state.loop_irq;
4936 bttv_buffer_activate_video(btv, &btv->curr);
4937 bttv_buffer_activate_vbi(btv, btv->cvbi);
4938 bttv_set_dma(btv, 0);
4939 spin_unlock_irqrestore(&btv->s_lock,flags);
4940 return 0;
4941}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004942#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
4944static struct pci_device_id bttv_pci_tbl[] = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004945 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4946 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004948 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004950 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004952 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4953 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954};
4955
4956MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4957
4958static struct pci_driver bttv_pci_driver = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08004959 .name = "bttv",
4960 .id_table = bttv_pci_tbl,
4961 .probe = bttv_probe,
4962 .remove = __devexit_p(bttv_remove),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004963#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 .suspend = bttv_suspend,
4965 .resume = bttv_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03004966#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967};
4968
4969static int bttv_init_module(void)
4970{
Randy Dunlapc526e222006-07-15 09:08:26 -03004971 int ret;
4972
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 bttv_num = 0;
4974
4975 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4976 (BTTV_VERSION_CODE >> 16) & 0xff,
4977 (BTTV_VERSION_CODE >> 8) & 0xff,
4978 BTTV_VERSION_CODE & 0xff);
4979#ifdef SNAPSHOT
4980 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4981 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4982#endif
4983 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4984 gbuffers = 2;
4985 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4986 gbufsize = BTTV_MAX_FBUF;
4987 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4988 if (bttv_verbose)
4989 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4990 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4991
4992 bttv_check_chipset();
4993
Randy Dunlapc526e222006-07-15 09:08:26 -03004994 ret = bus_register(&bttv_sub_bus_type);
4995 if (ret < 0) {
4996 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4997 return ret;
4998 }
Otavio Salvador23047592006-01-09 15:25:17 -02004999 return pci_register_driver(&bttv_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000}
5001
5002static void bttv_cleanup_module(void)
5003{
5004 pci_unregister_driver(&bttv_pci_driver);
5005 bus_unregister(&bttv_sub_bus_type);
5006 return;
5007}
5008
5009module_init(bttv_init_module);
5010module_exit(bttv_cleanup_module);
5011
5012/*
5013 * Local variables:
5014 * c-basic-offset: 8
5015 * End:
5016 */