media/video: explicitly flush request_module work
[deliverable/linux.git] / drivers / media / video / bt8xx / bttv-driver.c
1 /*
2
3 bttv - Bt848 frame grabber driver
4
5 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
6 & Marcus Metzler <mocm@thp.uni-koeln.de>
7 (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
12 V4L1 removal from:
13 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
14
15 Fixes to be fully V4L2 compliant by
16 (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
17
18 Cropping and overscan support
19 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
20 Sponsored by OPQ Systems AB
21
22 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 */
36
37 #include <linux/init.h>
38 #include <linux/module.h>
39 #include <linux/delay.h>
40 #include <linux/slab.h>
41 #include <linux/errno.h>
42 #include <linux/fs.h>
43 #include <linux/kernel.h>
44 #include <linux/sched.h>
45 #include <linux/interrupt.h>
46 #include <linux/kdev_t.h>
47 #include "bttvp.h"
48 #include <media/v4l2-common.h>
49 #include <media/v4l2-ioctl.h>
50 #include <media/tvaudio.h>
51 #include <media/msp3400.h>
52
53 #include <linux/dma-mapping.h>
54
55 #include <asm/io.h>
56 #include <asm/byteorder.h>
57
58 #include <media/rds.h>
59
60
61 unsigned int bttv_num; /* number of Bt848s in use */
62 struct bttv *bttvs[BTTV_MAX];
63
64 unsigned int bttv_debug;
65 unsigned int bttv_verbose = 1;
66 unsigned int bttv_gpio;
67
68 /* config variables */
69 #ifdef __BIG_ENDIAN
70 static unsigned int bigendian=1;
71 #else
72 static unsigned int bigendian;
73 #endif
74 static unsigned int radio[BTTV_MAX];
75 static unsigned int irq_debug;
76 static unsigned int gbuffers = 8;
77 static unsigned int gbufsize = 0x208000;
78 static unsigned int reset_crop = 1;
79
80 static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
81 static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
82 static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
83 static int debug_latency;
84 static int disable_ir;
85
86 static unsigned int fdsr;
87
88 /* options */
89 static unsigned int combfilter;
90 static unsigned int lumafilter;
91 static unsigned int automute = 1;
92 static unsigned int chroma_agc;
93 static unsigned int adc_crush = 1;
94 static unsigned int whitecrush_upper = 0xCF;
95 static unsigned int whitecrush_lower = 0x7F;
96 static unsigned int vcr_hack;
97 static unsigned int irq_iswitch;
98 static unsigned int uv_ratio = 50;
99 static unsigned int full_luma_range;
100 static unsigned int coring;
101
102 /* API features (turn on/off stuff for testing) */
103 static unsigned int v4l2 = 1;
104
105 /* insmod args */
106 module_param(bttv_verbose, int, 0644);
107 module_param(bttv_gpio, int, 0644);
108 module_param(bttv_debug, int, 0644);
109 module_param(irq_debug, int, 0644);
110 module_param(debug_latency, int, 0644);
111 module_param(disable_ir, int, 0444);
112
113 module_param(fdsr, int, 0444);
114 module_param(gbuffers, int, 0444);
115 module_param(gbufsize, int, 0444);
116 module_param(reset_crop, int, 0444);
117
118 module_param(v4l2, int, 0644);
119 module_param(bigendian, int, 0644);
120 module_param(irq_iswitch, int, 0644);
121 module_param(combfilter, int, 0444);
122 module_param(lumafilter, int, 0444);
123 module_param(automute, int, 0444);
124 module_param(chroma_agc, int, 0444);
125 module_param(adc_crush, int, 0444);
126 module_param(whitecrush_upper, int, 0444);
127 module_param(whitecrush_lower, int, 0444);
128 module_param(vcr_hack, int, 0444);
129 module_param(uv_ratio, int, 0444);
130 module_param(full_luma_range, int, 0444);
131 module_param(coring, int, 0444);
132
133 module_param_array(radio, int, NULL, 0444);
134 module_param_array(video_nr, int, NULL, 0444);
135 module_param_array(radio_nr, int, NULL, 0444);
136 module_param_array(vbi_nr, int, NULL, 0444);
137
138 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
139 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
140 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
141 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
142 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
143 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
144 MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
145 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
146 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
147 MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
148 "is 1 (yes) for compatibility with older applications");
149 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
150 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
151 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
152 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
153 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
154 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
155 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
156 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
157 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
158 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
159 MODULE_PARM_DESC(video_nr, "video device numbers");
160 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
161 MODULE_PARM_DESC(radio_nr, "radio device numbers");
162
163 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
164 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
165 MODULE_LICENSE("GPL");
166
167 /* ----------------------------------------------------------------------- */
168 /* sysfs */
169
170 static ssize_t show_card(struct device *cd,
171 struct device_attribute *attr, char *buf)
172 {
173 struct video_device *vfd = container_of(cd, struct video_device, dev);
174 struct bttv *btv = video_get_drvdata(vfd);
175 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
176 }
177 static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
178
179 /* ----------------------------------------------------------------------- */
180 /* dvb auto-load setup */
181 #if defined(CONFIG_MODULES) && defined(MODULE)
182 static void request_module_async(struct work_struct *work)
183 {
184 request_module("dvb-bt8xx");
185 }
186
187 static void request_modules(struct bttv *dev)
188 {
189 INIT_WORK(&dev->request_module_wk, request_module_async);
190 schedule_work(&dev->request_module_wk);
191 }
192
193 static void flush_request_modules(struct bttv *dev)
194 {
195 flush_work_sync(&dev->request_module_wk);
196 }
197 #else
198 #define request_modules(dev)
199 #define flush_request_modules(dev)
200 #endif /* CONFIG_MODULES */
201
202
203 /* ----------------------------------------------------------------------- */
204 /* static data */
205
206 /* special timing tables from conexant... */
207 static u8 SRAM_Table[][60] =
208 {
209 /* PAL digital input over GPIO[7:0] */
210 {
211 45, // 45 bytes following
212 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
213 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
214 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
215 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
216 0x37,0x00,0xAF,0x21,0x00
217 },
218 /* NTSC digital input over GPIO[7:0] */
219 {
220 51, // 51 bytes following
221 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
222 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
223 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
224 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
225 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
226 0x00,
227 },
228 // TGB_NTSC392 // quartzsight
229 // This table has been modified to be used for Fusion Rev D
230 {
231 0x2A, // size of table = 42
232 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
233 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
234 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
235 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
236 0x20, 0x00
237 }
238 };
239
240 /* minhdelayx1 first video pixel we can capture on a line and
241 hdelayx1 start of active video, both relative to rising edge of
242 /HRESET pulse (0H) in 1 / fCLKx1.
243 swidth width of active video and
244 totalwidth total line width, both in 1 / fCLKx1.
245 sqwidth total line width in square pixels.
246 vdelay start of active video in 2 * field lines relative to
247 trailing edge of /VRESET pulse (VDELAY register).
248 sheight height of active video in 2 * field lines.
249 videostart0 ITU-R frame line number of the line corresponding
250 to vdelay in the first field. */
251 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
252 vdelay, sheight, videostart0) \
253 .cropcap.bounds.left = minhdelayx1, \
254 /* * 2 because vertically we count field lines times two, */ \
255 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
256 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
257 /* 4 is a safety margin at the end of the line. */ \
258 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
259 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
260 .cropcap.defrect.left = hdelayx1, \
261 .cropcap.defrect.top = (videostart0) * 2, \
262 .cropcap.defrect.width = swidth, \
263 .cropcap.defrect.height = sheight, \
264 .cropcap.pixelaspect.numerator = totalwidth, \
265 .cropcap.pixelaspect.denominator = sqwidth,
266
267 const struct bttv_tvnorm bttv_tvnorms[] = {
268 /* PAL-BDGHI */
269 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
270 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
271 {
272 .v4l2_id = V4L2_STD_PAL,
273 .name = "PAL",
274 .Fsc = 35468950,
275 .swidth = 924,
276 .sheight = 576,
277 .totalwidth = 1135,
278 .adelay = 0x7f,
279 .bdelay = 0x72,
280 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
281 .scaledtwidth = 1135,
282 .hdelayx1 = 186,
283 .hactivex1 = 924,
284 .vdelay = 0x20,
285 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
286 .sram = 0,
287 /* ITU-R frame line number of the first VBI line
288 we can capture, of the first and second field.
289 The last line is determined by cropcap.bounds. */
290 .vbistart = { 7, 320 },
291 CROPCAP(/* minhdelayx1 */ 68,
292 /* hdelayx1 */ 186,
293 /* Should be (768 * 1135 + 944 / 2) / 944.
294 cropcap.defrect is used for image width
295 checks, so we keep the old value 924. */
296 /* swidth */ 924,
297 /* totalwidth */ 1135,
298 /* sqwidth */ 944,
299 /* vdelay */ 0x20,
300 /* sheight */ 576,
301 /* videostart0 */ 23)
302 /* bt878 (and bt848?) can capture another
303 line below active video. */
304 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
305 },{
306 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
307 .name = "NTSC",
308 .Fsc = 28636363,
309 .swidth = 768,
310 .sheight = 480,
311 .totalwidth = 910,
312 .adelay = 0x68,
313 .bdelay = 0x5d,
314 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
315 .scaledtwidth = 910,
316 .hdelayx1 = 128,
317 .hactivex1 = 910,
318 .vdelay = 0x1a,
319 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
320 .sram = 1,
321 .vbistart = { 10, 273 },
322 CROPCAP(/* minhdelayx1 */ 68,
323 /* hdelayx1 */ 128,
324 /* Should be (640 * 910 + 780 / 2) / 780? */
325 /* swidth */ 768,
326 /* totalwidth */ 910,
327 /* sqwidth */ 780,
328 /* vdelay */ 0x1a,
329 /* sheight */ 480,
330 /* videostart0 */ 23)
331 },{
332 .v4l2_id = V4L2_STD_SECAM,
333 .name = "SECAM",
334 .Fsc = 35468950,
335 .swidth = 924,
336 .sheight = 576,
337 .totalwidth = 1135,
338 .adelay = 0x7f,
339 .bdelay = 0xb0,
340 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
341 .scaledtwidth = 1135,
342 .hdelayx1 = 186,
343 .hactivex1 = 922,
344 .vdelay = 0x20,
345 .vbipack = 255,
346 .sram = 0, /* like PAL, correct? */
347 .vbistart = { 7, 320 },
348 CROPCAP(/* minhdelayx1 */ 68,
349 /* hdelayx1 */ 186,
350 /* swidth */ 924,
351 /* totalwidth */ 1135,
352 /* sqwidth */ 944,
353 /* vdelay */ 0x20,
354 /* sheight */ 576,
355 /* videostart0 */ 23)
356 },{
357 .v4l2_id = V4L2_STD_PAL_Nc,
358 .name = "PAL-Nc",
359 .Fsc = 28636363,
360 .swidth = 640,
361 .sheight = 576,
362 .totalwidth = 910,
363 .adelay = 0x68,
364 .bdelay = 0x5d,
365 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
366 .scaledtwidth = 780,
367 .hdelayx1 = 130,
368 .hactivex1 = 734,
369 .vdelay = 0x1a,
370 .vbipack = 144,
371 .sram = -1,
372 .vbistart = { 7, 320 },
373 CROPCAP(/* minhdelayx1 */ 68,
374 /* hdelayx1 */ 130,
375 /* swidth */ (640 * 910 + 780 / 2) / 780,
376 /* totalwidth */ 910,
377 /* sqwidth */ 780,
378 /* vdelay */ 0x1a,
379 /* sheight */ 576,
380 /* videostart0 */ 23)
381 },{
382 .v4l2_id = V4L2_STD_PAL_M,
383 .name = "PAL-M",
384 .Fsc = 28636363,
385 .swidth = 640,
386 .sheight = 480,
387 .totalwidth = 910,
388 .adelay = 0x68,
389 .bdelay = 0x5d,
390 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
391 .scaledtwidth = 780,
392 .hdelayx1 = 135,
393 .hactivex1 = 754,
394 .vdelay = 0x1a,
395 .vbipack = 144,
396 .sram = -1,
397 .vbistart = { 10, 273 },
398 CROPCAP(/* minhdelayx1 */ 68,
399 /* hdelayx1 */ 135,
400 /* swidth */ (640 * 910 + 780 / 2) / 780,
401 /* totalwidth */ 910,
402 /* sqwidth */ 780,
403 /* vdelay */ 0x1a,
404 /* sheight */ 480,
405 /* videostart0 */ 23)
406 },{
407 .v4l2_id = V4L2_STD_PAL_N,
408 .name = "PAL-N",
409 .Fsc = 35468950,
410 .swidth = 768,
411 .sheight = 576,
412 .totalwidth = 1135,
413 .adelay = 0x7f,
414 .bdelay = 0x72,
415 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
416 .scaledtwidth = 944,
417 .hdelayx1 = 186,
418 .hactivex1 = 922,
419 .vdelay = 0x20,
420 .vbipack = 144,
421 .sram = -1,
422 .vbistart = { 7, 320 },
423 CROPCAP(/* minhdelayx1 */ 68,
424 /* hdelayx1 */ 186,
425 /* swidth */ (768 * 1135 + 944 / 2) / 944,
426 /* totalwidth */ 1135,
427 /* sqwidth */ 944,
428 /* vdelay */ 0x20,
429 /* sheight */ 576,
430 /* videostart0 */ 23)
431 },{
432 .v4l2_id = V4L2_STD_NTSC_M_JP,
433 .name = "NTSC-JP",
434 .Fsc = 28636363,
435 .swidth = 640,
436 .sheight = 480,
437 .totalwidth = 910,
438 .adelay = 0x68,
439 .bdelay = 0x5d,
440 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
441 .scaledtwidth = 780,
442 .hdelayx1 = 135,
443 .hactivex1 = 754,
444 .vdelay = 0x16,
445 .vbipack = 144,
446 .sram = -1,
447 .vbistart = { 10, 273 },
448 CROPCAP(/* minhdelayx1 */ 68,
449 /* hdelayx1 */ 135,
450 /* swidth */ (640 * 910 + 780 / 2) / 780,
451 /* totalwidth */ 910,
452 /* sqwidth */ 780,
453 /* vdelay */ 0x16,
454 /* sheight */ 480,
455 /* videostart0 */ 23)
456 },{
457 /* that one hopefully works with the strange timing
458 * which video recorders produce when playing a NTSC
459 * tape on a PAL TV ... */
460 .v4l2_id = V4L2_STD_PAL_60,
461 .name = "PAL-60",
462 .Fsc = 35468950,
463 .swidth = 924,
464 .sheight = 480,
465 .totalwidth = 1135,
466 .adelay = 0x7f,
467 .bdelay = 0x72,
468 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
469 .scaledtwidth = 1135,
470 .hdelayx1 = 186,
471 .hactivex1 = 924,
472 .vdelay = 0x1a,
473 .vbipack = 255,
474 .vtotal = 524,
475 .sram = -1,
476 .vbistart = { 10, 273 },
477 CROPCAP(/* minhdelayx1 */ 68,
478 /* hdelayx1 */ 186,
479 /* swidth */ 924,
480 /* totalwidth */ 1135,
481 /* sqwidth */ 944,
482 /* vdelay */ 0x1a,
483 /* sheight */ 480,
484 /* videostart0 */ 23)
485 }
486 };
487 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
488
489 /* ----------------------------------------------------------------------- */
490 /* bttv format list
491 packed pixel formats must come first */
492 static const struct bttv_format formats[] = {
493 {
494 .name = "8 bpp, gray",
495 .fourcc = V4L2_PIX_FMT_GREY,
496 .btformat = BT848_COLOR_FMT_Y8,
497 .depth = 8,
498 .flags = FORMAT_FLAGS_PACKED,
499 },{
500 .name = "8 bpp, dithered color",
501 .fourcc = V4L2_PIX_FMT_HI240,
502 .btformat = BT848_COLOR_FMT_RGB8,
503 .depth = 8,
504 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
505 },{
506 .name = "15 bpp RGB, le",
507 .fourcc = V4L2_PIX_FMT_RGB555,
508 .btformat = BT848_COLOR_FMT_RGB15,
509 .depth = 16,
510 .flags = FORMAT_FLAGS_PACKED,
511 },{
512 .name = "15 bpp RGB, be",
513 .fourcc = V4L2_PIX_FMT_RGB555X,
514 .btformat = BT848_COLOR_FMT_RGB15,
515 .btswap = 0x03, /* byteswap */
516 .depth = 16,
517 .flags = FORMAT_FLAGS_PACKED,
518 },{
519 .name = "16 bpp RGB, le",
520 .fourcc = V4L2_PIX_FMT_RGB565,
521 .btformat = BT848_COLOR_FMT_RGB16,
522 .depth = 16,
523 .flags = FORMAT_FLAGS_PACKED,
524 },{
525 .name = "16 bpp RGB, be",
526 .fourcc = V4L2_PIX_FMT_RGB565X,
527 .btformat = BT848_COLOR_FMT_RGB16,
528 .btswap = 0x03, /* byteswap */
529 .depth = 16,
530 .flags = FORMAT_FLAGS_PACKED,
531 },{
532 .name = "24 bpp RGB, le",
533 .fourcc = V4L2_PIX_FMT_BGR24,
534 .btformat = BT848_COLOR_FMT_RGB24,
535 .depth = 24,
536 .flags = FORMAT_FLAGS_PACKED,
537 },{
538 .name = "32 bpp RGB, le",
539 .fourcc = V4L2_PIX_FMT_BGR32,
540 .btformat = BT848_COLOR_FMT_RGB32,
541 .depth = 32,
542 .flags = FORMAT_FLAGS_PACKED,
543 },{
544 .name = "32 bpp RGB, be",
545 .fourcc = V4L2_PIX_FMT_RGB32,
546 .btformat = BT848_COLOR_FMT_RGB32,
547 .btswap = 0x0f, /* byte+word swap */
548 .depth = 32,
549 .flags = FORMAT_FLAGS_PACKED,
550 },{
551 .name = "4:2:2, packed, YUYV",
552 .fourcc = V4L2_PIX_FMT_YUYV,
553 .btformat = BT848_COLOR_FMT_YUY2,
554 .depth = 16,
555 .flags = FORMAT_FLAGS_PACKED,
556 },{
557 .name = "4:2:2, packed, YUYV",
558 .fourcc = V4L2_PIX_FMT_YUYV,
559 .btformat = BT848_COLOR_FMT_YUY2,
560 .depth = 16,
561 .flags = FORMAT_FLAGS_PACKED,
562 },{
563 .name = "4:2:2, packed, UYVY",
564 .fourcc = V4L2_PIX_FMT_UYVY,
565 .btformat = BT848_COLOR_FMT_YUY2,
566 .btswap = 0x03, /* byteswap */
567 .depth = 16,
568 .flags = FORMAT_FLAGS_PACKED,
569 },{
570 .name = "4:2:2, planar, Y-Cb-Cr",
571 .fourcc = V4L2_PIX_FMT_YUV422P,
572 .btformat = BT848_COLOR_FMT_YCrCb422,
573 .depth = 16,
574 .flags = FORMAT_FLAGS_PLANAR,
575 .hshift = 1,
576 .vshift = 0,
577 },{
578 .name = "4:2:0, planar, Y-Cb-Cr",
579 .fourcc = V4L2_PIX_FMT_YUV420,
580 .btformat = BT848_COLOR_FMT_YCrCb422,
581 .depth = 12,
582 .flags = FORMAT_FLAGS_PLANAR,
583 .hshift = 1,
584 .vshift = 1,
585 },{
586 .name = "4:2:0, planar, Y-Cr-Cb",
587 .fourcc = V4L2_PIX_FMT_YVU420,
588 .btformat = BT848_COLOR_FMT_YCrCb422,
589 .depth = 12,
590 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
591 .hshift = 1,
592 .vshift = 1,
593 },{
594 .name = "4:1:1, planar, Y-Cb-Cr",
595 .fourcc = V4L2_PIX_FMT_YUV411P,
596 .btformat = BT848_COLOR_FMT_YCrCb411,
597 .depth = 12,
598 .flags = FORMAT_FLAGS_PLANAR,
599 .hshift = 2,
600 .vshift = 0,
601 },{
602 .name = "4:1:0, planar, Y-Cb-Cr",
603 .fourcc = V4L2_PIX_FMT_YUV410,
604 .btformat = BT848_COLOR_FMT_YCrCb411,
605 .depth = 9,
606 .flags = FORMAT_FLAGS_PLANAR,
607 .hshift = 2,
608 .vshift = 2,
609 },{
610 .name = "4:1:0, planar, Y-Cr-Cb",
611 .fourcc = V4L2_PIX_FMT_YVU410,
612 .btformat = BT848_COLOR_FMT_YCrCb411,
613 .depth = 9,
614 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
615 .hshift = 2,
616 .vshift = 2,
617 },{
618 .name = "raw scanlines",
619 .fourcc = -1,
620 .btformat = BT848_COLOR_FMT_RAW,
621 .depth = 8,
622 .flags = FORMAT_FLAGS_RAW,
623 }
624 };
625 static const unsigned int FORMATS = ARRAY_SIZE(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)
637 #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)
641
642 static const struct v4l2_queryctrl no_ctl = {
643 .name = "42",
644 .flags = V4L2_CTRL_FLAG_DISABLED,
645 };
646 static 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,
774 },{
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,
796 }
797
798
799
800 };
801
802 static const struct v4l2_queryctrl *ctrl_by_id(int id)
803 {
804 int i;
805
806 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
807 if (bttv_ctls[i].id == id)
808 return bttv_ctls+i;
809
810 return NULL;
811 }
812
813 /* ----------------------------------------------------------------------- */
814 /* resource management */
815
816 /*
817 RESOURCE_ allocated by freed by
818
819 VIDEO_READ bttv_read 1) bttv_read 2)
820
821 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
822 VIDIOC_QBUF 1) bttv_release
823 VIDIOCMCAPTURE 1)
824
825 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
826 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
827 3) bttv_release
828
829 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
830 VIDIOC_QBUF 1) bttv_release
831 bttv_read, bttv_poll 1) 4)
832
833 1) The resource must be allocated when we enter buffer prepare functions
834 and remain allocated while buffers are in the DMA queue.
835 2) This is a single frame read.
836 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
837 RESOURCE_OVERLAY is allocated.
838 4) This is a continuous read, implies VIDIOC_STREAMON.
839
840 Note this driver permits video input and standard changes regardless if
841 resources are allocated.
842 */
843
844 #define VBI_RESOURCES (RESOURCE_VBI)
845 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
846 RESOURCE_VIDEO_STREAM | \
847 RESOURCE_OVERLAY)
848
849 static
850 int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
851 {
852 int xbits; /* mutual exclusive resources */
853
854 if (fh->resources & bit)
855 /* have it already allocated */
856 return 1;
857
858 xbits = bit;
859 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
860 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
861
862 /* is it free? */
863 mutex_lock(&btv->lock);
864 if (btv->resources & xbits) {
865 /* no, someone else uses it */
866 goto fail;
867 }
868
869 if ((bit & VIDEO_RESOURCES)
870 && 0 == (btv->resources & VIDEO_RESOURCES)) {
871 /* Do crop - use current, don't - use default parameters. */
872 __s32 top = btv->crop[!!fh->do_crop].rect.top;
873
874 if (btv->vbi_end > top)
875 goto fail;
876
877 /* We cannot capture the same line as video and VBI data.
878 Claim scan lines crop[].rect.top to bottom. */
879 btv->crop_start = top;
880 } else if (bit & VBI_RESOURCES) {
881 __s32 end = fh->vbi_fmt.end;
882
883 if (end > btv->crop_start)
884 goto fail;
885
886 /* Claim scan lines above fh->vbi_fmt.end. */
887 btv->vbi_end = end;
888 }
889
890 /* it's free, grab it */
891 fh->resources |= bit;
892 btv->resources |= bit;
893 mutex_unlock(&btv->lock);
894 return 1;
895
896 fail:
897 mutex_unlock(&btv->lock);
898 return 0;
899 }
900
901 static
902 int check_btres(struct bttv_fh *fh, int bit)
903 {
904 return (fh->resources & bit);
905 }
906
907 static
908 int locked_btres(struct bttv *btv, int bit)
909 {
910 return (btv->resources & bit);
911 }
912
913 /* Call with btv->lock down. */
914 static void
915 disclaim_vbi_lines(struct bttv *btv)
916 {
917 btv->vbi_end = 0;
918 }
919
920 /* Call with btv->lock down. */
921 static void
922 disclaim_video_lines(struct bttv *btv)
923 {
924 const struct bttv_tvnorm *tvnorm;
925 u8 crop;
926
927 tvnorm = &bttv_tvnorms[btv->tvnorm];
928 btv->crop_start = tvnorm->cropcap.bounds.top
929 + tvnorm->cropcap.bounds.height;
930
931 /* VBI capturing ends at VDELAY, start of video capturing, no
932 matter how many lines the VBI RISC program expects. When video
933 capturing is off, it shall no longer "preempt" VBI capturing,
934 so we set VDELAY to maximum. */
935 crop = btread(BT848_E_CROP) | 0xc0;
936 btwrite(crop, BT848_E_CROP);
937 btwrite(0xfe, BT848_E_VDELAY_LO);
938 btwrite(crop, BT848_O_CROP);
939 btwrite(0xfe, BT848_O_VDELAY_LO);
940 }
941
942 static
943 void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
944 {
945 if ((fh->resources & bits) != bits) {
946 /* trying to free ressources not allocated by us ... */
947 printk("bttv: BUG! (btres)\n");
948 }
949 mutex_lock(&btv->lock);
950 fh->resources &= ~bits;
951 btv->resources &= ~bits;
952
953 bits = btv->resources;
954
955 if (0 == (bits & VIDEO_RESOURCES))
956 disclaim_video_lines(btv);
957
958 if (0 == (bits & VBI_RESOURCES))
959 disclaim_vbi_lines(btv);
960
961 mutex_unlock(&btv->lock);
962 }
963
964 /* ----------------------------------------------------------------------- */
965 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
966
967 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
968 PLL_X = Reference pre-divider (0=1, 1=2)
969 PLL_C = Post divider (0=6, 1=4)
970 PLL_I = Integer input
971 PLL_F = Fractional input
972
973 F_input = 28.636363 MHz:
974 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
975 */
976
977 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
978 {
979 unsigned char fl, fh, fi;
980
981 /* prevent overflows */
982 fin/=4;
983 fout/=4;
984
985 fout*=12;
986 fi=fout/fin;
987
988 fout=(fout%fin)*256;
989 fh=fout/fin;
990
991 fout=(fout%fin)*256;
992 fl=fout/fin;
993
994 btwrite(fl, BT848_PLL_F_LO);
995 btwrite(fh, BT848_PLL_F_HI);
996 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
997 }
998
999 static void set_pll(struct bttv *btv)
1000 {
1001 int i;
1002
1003 if (!btv->pll.pll_crystal)
1004 return;
1005
1006 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
1007 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
1008 return;
1009 }
1010
1011 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1012 /* no PLL needed */
1013 if (btv->pll.pll_current == 0)
1014 return;
1015 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
1016 btv->c.nr,btv->pll.pll_ifreq);
1017 btwrite(0x00,BT848_TGCTRL);
1018 btwrite(0x00,BT848_PLL_XCI);
1019 btv->pll.pll_current = 0;
1020 return;
1021 }
1022
1023 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
1024 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1025 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1026
1027 for (i=0; i<10; i++) {
1028 /* Let other people run while the PLL stabilizes */
1029 bttv_printk(".");
1030 msleep(10);
1031
1032 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
1033 btwrite(0,BT848_DSTATUS);
1034 } else {
1035 btwrite(0x08,BT848_TGCTRL);
1036 btv->pll.pll_current = btv->pll.pll_ofreq;
1037 bttv_printk(" ok\n");
1038 return;
1039 }
1040 }
1041 btv->pll.pll_current = -1;
1042 bttv_printk("failed\n");
1043 return;
1044 }
1045
1046 /* used to switch between the bt848's analog/digital video capture modes */
1047 static void bt848A_set_timing(struct bttv *btv)
1048 {
1049 int i, len;
1050 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1051 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1052
1053 if (btv->input == btv->dig) {
1054 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1055 btv->c.nr,table_idx);
1056
1057 /* timing change...reset timing generator address */
1058 btwrite(0x00, BT848_TGCTRL);
1059 btwrite(0x02, BT848_TGCTRL);
1060 btwrite(0x00, BT848_TGCTRL);
1061
1062 len=SRAM_Table[table_idx][0];
1063 for(i = 1; i <= len; i++)
1064 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1065 btv->pll.pll_ofreq = 27000000;
1066
1067 set_pll(btv);
1068 btwrite(0x11, BT848_TGCTRL);
1069 btwrite(0x41, BT848_DVSIF);
1070 } else {
1071 btv->pll.pll_ofreq = fsc;
1072 set_pll(btv);
1073 btwrite(0x0, BT848_DVSIF);
1074 }
1075 }
1076
1077 /* ----------------------------------------------------------------------- */
1078
1079 static void bt848_bright(struct bttv *btv, int bright)
1080 {
1081 int value;
1082
1083 // printk("bttv: set bright: %d\n",bright); // DEBUG
1084 btv->bright = bright;
1085
1086 /* We want -128 to 127 we get 0-65535 */
1087 value = (bright >> 8) - 128;
1088 btwrite(value & 0xff, BT848_BRIGHT);
1089 }
1090
1091 static void bt848_hue(struct bttv *btv, int hue)
1092 {
1093 int value;
1094
1095 btv->hue = hue;
1096
1097 /* -128 to 127 */
1098 value = (hue >> 8) - 128;
1099 btwrite(value & 0xff, BT848_HUE);
1100 }
1101
1102 static void bt848_contrast(struct bttv *btv, int cont)
1103 {
1104 int value,hibit;
1105
1106 btv->contrast = cont;
1107
1108 /* 0-511 */
1109 value = (cont >> 7);
1110 hibit = (value >> 6) & 4;
1111 btwrite(value & 0xff, BT848_CONTRAST_LO);
1112 btaor(hibit, ~4, BT848_E_CONTROL);
1113 btaor(hibit, ~4, BT848_O_CONTROL);
1114 }
1115
1116 static void bt848_sat(struct bttv *btv, int color)
1117 {
1118 int val_u,val_v,hibits;
1119
1120 btv->saturation = color;
1121
1122 /* 0-511 for the color */
1123 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1124 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
1125 hibits = (val_u >> 7) & 2;
1126 hibits |= (val_v >> 8) & 1;
1127 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1128 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1129 btaor(hibits, ~3, BT848_E_CONTROL);
1130 btaor(hibits, ~3, BT848_O_CONTROL);
1131 }
1132
1133 /* ----------------------------------------------------------------------- */
1134
1135 static int
1136 video_mux(struct bttv *btv, unsigned int input)
1137 {
1138 int mux,mask2;
1139
1140 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1141 return -EINVAL;
1142
1143 /* needed by RemoteVideo MX */
1144 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1145 if (mask2)
1146 gpio_inout(mask2,mask2);
1147
1148 if (input == btv->svhs) {
1149 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1150 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1151 } else {
1152 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1153 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1154 }
1155 mux = bttv_muxsel(btv, input);
1156 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1157 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1158 btv->c.nr,input,mux);
1159
1160 /* card specific hook */
1161 if(bttv_tvcards[btv->c.type].muxsel_hook)
1162 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1163 return 0;
1164 }
1165
1166 static char *audio_modes[] = {
1167 "audio: tuner", "audio: radio", "audio: extern",
1168 "audio: intern", "audio: mute"
1169 };
1170
1171 static int
1172 audio_mux(struct bttv *btv, int input, int mute)
1173 {
1174 int gpio_val, signal;
1175 struct v4l2_control ctrl;
1176
1177 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1178 bttv_tvcards[btv->c.type].gpiomask);
1179 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1180
1181 btv->mute = mute;
1182 btv->audio = input;
1183
1184 /* automute */
1185 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1186
1187 if (mute)
1188 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1189 else
1190 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
1191
1192 switch (btv->c.type) {
1193 case BTTV_BOARD_VOODOOTV_FM:
1194 case BTTV_BOARD_VOODOOTV_200:
1195 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1196 break;
1197
1198 default:
1199 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1200 }
1201
1202 if (bttv_gpio)
1203 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1204 if (in_interrupt())
1205 return 0;
1206
1207 ctrl.id = V4L2_CID_AUDIO_MUTE;
1208 ctrl.value = btv->mute;
1209 bttv_call_all(btv, core, s_ctrl, &ctrl);
1210 if (btv->sd_msp34xx) {
1211 u32 in;
1212
1213 /* Note: the inputs tuner/radio/extern/intern are translated
1214 to msp routings. This assumes common behavior for all msp3400
1215 based TV cards. When this assumption fails, then the
1216 specific MSP routing must be added to the card table.
1217 For now this is sufficient. */
1218 switch (input) {
1219 case TVAUDIO_INPUT_RADIO:
1220 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1221 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1222 break;
1223 case TVAUDIO_INPUT_EXTERN:
1224 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1225 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1226 break;
1227 case TVAUDIO_INPUT_INTERN:
1228 /* Yes, this is the same input as for RADIO. I doubt
1229 if this is ever used. The only board with an INTERN
1230 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1231 that was tested. My guess is that the whole INTERN
1232 input does not work. */
1233 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1234 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1235 break;
1236 case TVAUDIO_INPUT_TUNER:
1237 default:
1238 /* This is the only card that uses TUNER2, and afaik,
1239 is the only difference between the VOODOOTV_FM
1240 and VOODOOTV_200 */
1241 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1242 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1243 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1244 else
1245 in = MSP_INPUT_DEFAULT;
1246 break;
1247 }
1248 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1249 in, MSP_OUTPUT_DEFAULT, 0);
1250 }
1251 if (btv->sd_tvaudio) {
1252 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1253 input, 0, 0);
1254 }
1255 return 0;
1256 }
1257
1258 static inline int
1259 audio_mute(struct bttv *btv, int mute)
1260 {
1261 return audio_mux(btv, btv->audio, mute);
1262 }
1263
1264 static inline int
1265 audio_input(struct bttv *btv, int input)
1266 {
1267 return audio_mux(btv, input, btv->mute);
1268 }
1269
1270 static void
1271 bttv_crop_calc_limits(struct bttv_crop *c)
1272 {
1273 /* Scale factor min. 1:1, max. 16:1. Min. image size
1274 48 x 32. Scaled width must be a multiple of 4. */
1275
1276 if (1) {
1277 /* For bug compatibility with VIDIOCGCAP and image
1278 size checks in earlier driver versions. */
1279 c->min_scaled_width = 48;
1280 c->min_scaled_height = 32;
1281 } else {
1282 c->min_scaled_width =
1283 (max(48, c->rect.width >> 4) + 3) & ~3;
1284 c->min_scaled_height =
1285 max(32, c->rect.height >> 4);
1286 }
1287
1288 c->max_scaled_width = c->rect.width & ~3;
1289 c->max_scaled_height = c->rect.height;
1290 }
1291
1292 static void
1293 bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
1294 {
1295 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1296 bttv_crop_calc_limits(c);
1297 }
1298
1299 /* Call with btv->lock down. */
1300 static int
1301 set_tvnorm(struct bttv *btv, unsigned int norm)
1302 {
1303 const struct bttv_tvnorm *tvnorm;
1304 v4l2_std_id id;
1305
1306 BUG_ON(norm >= BTTV_TVNORMS);
1307 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
1308
1309 tvnorm = &bttv_tvnorms[norm];
1310
1311 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
1312 sizeof (tvnorm->cropcap))) {
1313 bttv_crop_reset(&btv->crop[0], norm);
1314 btv->crop[1] = btv->crop[0]; /* current = default */
1315
1316 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1317 btv->crop_start = tvnorm->cropcap.bounds.top
1318 + tvnorm->cropcap.bounds.height;
1319 }
1320 }
1321
1322 btv->tvnorm = norm;
1323
1324 btwrite(tvnorm->adelay, BT848_ADELAY);
1325 btwrite(tvnorm->bdelay, BT848_BDELAY);
1326 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1327 BT848_IFORM);
1328 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1329 btwrite(1, BT848_VBI_PACK_DEL);
1330 bt848A_set_timing(btv);
1331
1332 switch (btv->c.type) {
1333 case BTTV_BOARD_VOODOOTV_FM:
1334 case BTTV_BOARD_VOODOOTV_200:
1335 bttv_tda9880_setnorm(btv, gpio_read());
1336 break;
1337 }
1338 id = tvnorm->v4l2_id;
1339 bttv_call_all(btv, core, s_std, id);
1340
1341 return 0;
1342 }
1343
1344 /* Call with btv->lock down. */
1345 static void
1346 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1347 {
1348 unsigned long flags;
1349
1350 btv->input = input;
1351 if (irq_iswitch) {
1352 spin_lock_irqsave(&btv->s_lock,flags);
1353 if (btv->curr.frame_irq) {
1354 /* active capture -> delayed input switch */
1355 btv->new_input = input;
1356 } else {
1357 video_mux(btv,input);
1358 }
1359 spin_unlock_irqrestore(&btv->s_lock,flags);
1360 } else {
1361 video_mux(btv,input);
1362 }
1363 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1364 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
1365 set_tvnorm(btv, norm);
1366 }
1367
1368 static void init_irqreg(struct bttv *btv)
1369 {
1370 /* clear status */
1371 btwrite(0xfffffUL, BT848_INT_STAT);
1372
1373 if (bttv_tvcards[btv->c.type].no_video) {
1374 /* i2c only */
1375 btwrite(BT848_INT_I2CDONE,
1376 BT848_INT_MASK);
1377 } else {
1378 /* full video */
1379 btwrite((btv->triton1) |
1380 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1381 BT848_INT_SCERR |
1382 (fdsr ? BT848_INT_FDSR : 0) |
1383 BT848_INT_RISCI | BT848_INT_OCERR |
1384 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1385 BT848_INT_I2CDONE,
1386 BT848_INT_MASK);
1387 }
1388 }
1389
1390 static void init_bt848(struct bttv *btv)
1391 {
1392 int val;
1393
1394 if (bttv_tvcards[btv->c.type].no_video) {
1395 /* very basic init only */
1396 init_irqreg(btv);
1397 return;
1398 }
1399
1400 btwrite(0x00, BT848_CAP_CTL);
1401 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1402 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1403
1404 /* set planar and packed mode trigger points and */
1405 /* set rising edge of inverted GPINTR pin as irq trigger */
1406 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1407 BT848_GPIO_DMA_CTL_PLTP1_16|
1408 BT848_GPIO_DMA_CTL_PLTP23_16|
1409 BT848_GPIO_DMA_CTL_GPINTC|
1410 BT848_GPIO_DMA_CTL_GPINTI,
1411 BT848_GPIO_DMA_CTL);
1412
1413 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1414 btwrite(val, BT848_E_SCLOOP);
1415 btwrite(val, BT848_O_SCLOOP);
1416
1417 btwrite(0x20, BT848_E_VSCALE_HI);
1418 btwrite(0x20, BT848_O_VSCALE_HI);
1419 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1420 BT848_ADC);
1421
1422 btwrite(whitecrush_upper, BT848_WC_UP);
1423 btwrite(whitecrush_lower, BT848_WC_DOWN);
1424
1425 if (btv->opt_lumafilter) {
1426 btwrite(0, BT848_E_CONTROL);
1427 btwrite(0, BT848_O_CONTROL);
1428 } else {
1429 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1430 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1431 }
1432
1433 bt848_bright(btv, btv->bright);
1434 bt848_hue(btv, btv->hue);
1435 bt848_contrast(btv, btv->contrast);
1436 bt848_sat(btv, btv->saturation);
1437
1438 /* interrupt */
1439 init_irqreg(btv);
1440 }
1441
1442 static void bttv_reinit_bt848(struct bttv *btv)
1443 {
1444 unsigned long flags;
1445
1446 if (bttv_verbose)
1447 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1448 spin_lock_irqsave(&btv->s_lock,flags);
1449 btv->errors=0;
1450 bttv_set_dma(btv,0);
1451 spin_unlock_irqrestore(&btv->s_lock,flags);
1452
1453 init_bt848(btv);
1454 btv->pll.pll_current = -1;
1455 set_input(btv, btv->input, btv->tvnorm);
1456 }
1457
1458 static int bttv_g_ctrl(struct file *file, void *priv,
1459 struct v4l2_control *c)
1460 {
1461 struct bttv_fh *fh = priv;
1462 struct bttv *btv = fh->btv;
1463
1464 switch (c->id) {
1465 case V4L2_CID_BRIGHTNESS:
1466 c->value = btv->bright;
1467 break;
1468 case V4L2_CID_HUE:
1469 c->value = btv->hue;
1470 break;
1471 case V4L2_CID_CONTRAST:
1472 c->value = btv->contrast;
1473 break;
1474 case V4L2_CID_SATURATION:
1475 c->value = btv->saturation;
1476 break;
1477
1478 case V4L2_CID_AUDIO_MUTE:
1479 case V4L2_CID_AUDIO_VOLUME:
1480 case V4L2_CID_AUDIO_BALANCE:
1481 case V4L2_CID_AUDIO_BASS:
1482 case V4L2_CID_AUDIO_TREBLE:
1483 bttv_call_all(btv, core, g_ctrl, c);
1484 break;
1485
1486 case V4L2_CID_PRIVATE_CHROMA_AGC:
1487 c->value = btv->opt_chroma_agc;
1488 break;
1489 case V4L2_CID_PRIVATE_COMBFILTER:
1490 c->value = btv->opt_combfilter;
1491 break;
1492 case V4L2_CID_PRIVATE_LUMAFILTER:
1493 c->value = btv->opt_lumafilter;
1494 break;
1495 case V4L2_CID_PRIVATE_AUTOMUTE:
1496 c->value = btv->opt_automute;
1497 break;
1498 case V4L2_CID_PRIVATE_AGC_CRUSH:
1499 c->value = btv->opt_adc_crush;
1500 break;
1501 case V4L2_CID_PRIVATE_VCR_HACK:
1502 c->value = btv->opt_vcr_hack;
1503 break;
1504 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1505 c->value = btv->opt_whitecrush_upper;
1506 break;
1507 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1508 c->value = btv->opt_whitecrush_lower;
1509 break;
1510 case V4L2_CID_PRIVATE_UV_RATIO:
1511 c->value = btv->opt_uv_ratio;
1512 break;
1513 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1514 c->value = btv->opt_full_luma_range;
1515 break;
1516 case V4L2_CID_PRIVATE_CORING:
1517 c->value = btv->opt_coring;
1518 break;
1519 default:
1520 return -EINVAL;
1521 }
1522 return 0;
1523 }
1524
1525 static int bttv_s_ctrl(struct file *file, void *f,
1526 struct v4l2_control *c)
1527 {
1528 int err;
1529 int val;
1530 struct bttv_fh *fh = f;
1531 struct bttv *btv = fh->btv;
1532
1533 err = v4l2_prio_check(&btv->prio, fh->prio);
1534 if (0 != err)
1535 return err;
1536
1537 switch (c->id) {
1538 case V4L2_CID_BRIGHTNESS:
1539 bt848_bright(btv, c->value);
1540 break;
1541 case V4L2_CID_HUE:
1542 bt848_hue(btv, c->value);
1543 break;
1544 case V4L2_CID_CONTRAST:
1545 bt848_contrast(btv, c->value);
1546 break;
1547 case V4L2_CID_SATURATION:
1548 bt848_sat(btv, c->value);
1549 break;
1550 case V4L2_CID_AUDIO_MUTE:
1551 audio_mute(btv, c->value);
1552 /* fall through */
1553 case V4L2_CID_AUDIO_VOLUME:
1554 if (btv->volume_gpio)
1555 btv->volume_gpio(btv, c->value);
1556
1557 bttv_call_all(btv, core, s_ctrl, c);
1558 break;
1559 case V4L2_CID_AUDIO_BALANCE:
1560 case V4L2_CID_AUDIO_BASS:
1561 case V4L2_CID_AUDIO_TREBLE:
1562 bttv_call_all(btv, core, s_ctrl, c);
1563 break;
1564
1565 case V4L2_CID_PRIVATE_CHROMA_AGC:
1566 btv->opt_chroma_agc = c->value;
1567 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1568 btwrite(val, BT848_E_SCLOOP);
1569 btwrite(val, BT848_O_SCLOOP);
1570 break;
1571 case V4L2_CID_PRIVATE_COMBFILTER:
1572 btv->opt_combfilter = c->value;
1573 break;
1574 case V4L2_CID_PRIVATE_LUMAFILTER:
1575 btv->opt_lumafilter = c->value;
1576 if (btv->opt_lumafilter) {
1577 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1578 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1579 } else {
1580 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1581 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1582 }
1583 break;
1584 case V4L2_CID_PRIVATE_AUTOMUTE:
1585 btv->opt_automute = c->value;
1586 break;
1587 case V4L2_CID_PRIVATE_AGC_CRUSH:
1588 btv->opt_adc_crush = c->value;
1589 btwrite(BT848_ADC_RESERVED |
1590 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1591 BT848_ADC);
1592 break;
1593 case V4L2_CID_PRIVATE_VCR_HACK:
1594 btv->opt_vcr_hack = c->value;
1595 break;
1596 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1597 btv->opt_whitecrush_upper = c->value;
1598 btwrite(c->value, BT848_WC_UP);
1599 break;
1600 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1601 btv->opt_whitecrush_lower = c->value;
1602 btwrite(c->value, BT848_WC_DOWN);
1603 break;
1604 case V4L2_CID_PRIVATE_UV_RATIO:
1605 btv->opt_uv_ratio = c->value;
1606 bt848_sat(btv, btv->saturation);
1607 break;
1608 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1609 btv->opt_full_luma_range = c->value;
1610 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1611 break;
1612 case V4L2_CID_PRIVATE_CORING:
1613 btv->opt_coring = c->value;
1614 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1615 break;
1616 default:
1617 return -EINVAL;
1618 }
1619 return 0;
1620 }
1621
1622 /* ----------------------------------------------------------------------- */
1623
1624 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1625 {
1626 unsigned int outbits, data;
1627 outbits = btread(BT848_GPIO_OUT_EN);
1628 data = btread(BT848_GPIO_DATA);
1629 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1630 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1631 }
1632
1633 static void bttv_field_count(struct bttv *btv)
1634 {
1635 int need_count = 0;
1636
1637 if (btv->users)
1638 need_count++;
1639
1640 if (need_count) {
1641 /* start field counter */
1642 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1643 } else {
1644 /* stop field counter */
1645 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1646 btv->field_count = 0;
1647 }
1648 }
1649
1650 static const struct bttv_format*
1651 format_by_fourcc(int fourcc)
1652 {
1653 unsigned int i;
1654
1655 for (i = 0; i < FORMATS; i++) {
1656 if (-1 == formats[i].fourcc)
1657 continue;
1658 if (formats[i].fourcc == fourcc)
1659 return formats+i;
1660 }
1661 return NULL;
1662 }
1663
1664 /* ----------------------------------------------------------------------- */
1665 /* misc helpers */
1666
1667 static int
1668 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1669 struct bttv_buffer *new)
1670 {
1671 struct bttv_buffer *old;
1672 unsigned long flags;
1673 int retval = 0;
1674
1675 dprintk("switch_overlay: enter [new=%p]\n",new);
1676 if (new)
1677 new->vb.state = VIDEOBUF_DONE;
1678 spin_lock_irqsave(&btv->s_lock,flags);
1679 old = btv->screen;
1680 btv->screen = new;
1681 btv->loop_irq |= 1;
1682 bttv_set_dma(btv, 0x03);
1683 spin_unlock_irqrestore(&btv->s_lock,flags);
1684 if (NULL != old) {
1685 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1686 bttv_dma_free(&fh->cap,btv, old);
1687 kfree(old);
1688 }
1689 if (NULL == new)
1690 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
1691 dprintk("switch_overlay: done\n");
1692 return retval;
1693 }
1694
1695 /* ----------------------------------------------------------------------- */
1696 /* video4linux (1) interface */
1697
1698 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1699 struct bttv_buffer *buf,
1700 const struct bttv_format *fmt,
1701 unsigned int width, unsigned int height,
1702 enum v4l2_field field)
1703 {
1704 struct bttv_fh *fh = q->priv_data;
1705 int redo_dma_risc = 0;
1706 struct bttv_crop c;
1707 int norm;
1708 int rc;
1709
1710 /* check settings */
1711 if (NULL == fmt)
1712 return -EINVAL;
1713 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1714 width = RAW_BPL;
1715 height = RAW_LINES*2;
1716 if (width*height > buf->vb.bsize)
1717 return -EINVAL;
1718 buf->vb.size = buf->vb.bsize;
1719
1720 /* Make sure tvnorm and vbi_end remain consistent
1721 until we're done. */
1722 mutex_lock(&btv->lock);
1723
1724 norm = btv->tvnorm;
1725
1726 /* In this mode capturing always starts at defrect.top
1727 (default VDELAY), ignoring cropping parameters. */
1728 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1729 mutex_unlock(&btv->lock);
1730 return -EINVAL;
1731 }
1732
1733 mutex_unlock(&btv->lock);
1734
1735 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1736 } else {
1737 mutex_lock(&btv->lock);
1738
1739 norm = btv->tvnorm;
1740 c = btv->crop[!!fh->do_crop];
1741
1742 mutex_unlock(&btv->lock);
1743
1744 if (width < c.min_scaled_width ||
1745 width > c.max_scaled_width ||
1746 height < c.min_scaled_height)
1747 return -EINVAL;
1748
1749 switch (field) {
1750 case V4L2_FIELD_TOP:
1751 case V4L2_FIELD_BOTTOM:
1752 case V4L2_FIELD_ALTERNATE:
1753 /* btv->crop counts frame lines. Max. scale
1754 factor is 16:1 for frames, 8:1 for fields. */
1755 if (height * 2 > c.max_scaled_height)
1756 return -EINVAL;
1757 break;
1758
1759 default:
1760 if (height > c.max_scaled_height)
1761 return -EINVAL;
1762 break;
1763 }
1764
1765 buf->vb.size = (width * height * fmt->depth) >> 3;
1766 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1767 return -EINVAL;
1768 }
1769
1770 /* alloc + fill struct bttv_buffer (if changed) */
1771 if (buf->vb.width != width || buf->vb.height != height ||
1772 buf->vb.field != field ||
1773 buf->tvnorm != norm || buf->fmt != fmt ||
1774 buf->crop.top != c.rect.top ||
1775 buf->crop.left != c.rect.left ||
1776 buf->crop.width != c.rect.width ||
1777 buf->crop.height != c.rect.height) {
1778 buf->vb.width = width;
1779 buf->vb.height = height;
1780 buf->vb.field = field;
1781 buf->tvnorm = norm;
1782 buf->fmt = fmt;
1783 buf->crop = c.rect;
1784 redo_dma_risc = 1;
1785 }
1786
1787 /* alloc risc memory */
1788 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1789 redo_dma_risc = 1;
1790 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1791 goto fail;
1792 }
1793
1794 if (redo_dma_risc)
1795 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1796 goto fail;
1797
1798 buf->vb.state = VIDEOBUF_PREPARED;
1799 return 0;
1800
1801 fail:
1802 bttv_dma_free(q,btv,buf);
1803 return rc;
1804 }
1805
1806 static int
1807 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1808 {
1809 struct bttv_fh *fh = q->priv_data;
1810
1811 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1812 if (0 == *count)
1813 *count = gbuffers;
1814 if (*size * *count > gbuffers * gbufsize)
1815 *count = (gbuffers * gbufsize) / *size;
1816 return 0;
1817 }
1818
1819 static int
1820 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1821 enum v4l2_field field)
1822 {
1823 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1824 struct bttv_fh *fh = q->priv_data;
1825
1826 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1827 fh->width, fh->height, field);
1828 }
1829
1830 static void
1831 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1832 {
1833 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1834 struct bttv_fh *fh = q->priv_data;
1835 struct bttv *btv = fh->btv;
1836
1837 buf->vb.state = VIDEOBUF_QUEUED;
1838 list_add_tail(&buf->vb.queue,&btv->capture);
1839 if (!btv->curr.frame_irq) {
1840 btv->loop_irq |= 1;
1841 bttv_set_dma(btv, 0x03);
1842 }
1843 }
1844
1845 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1846 {
1847 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1848 struct bttv_fh *fh = q->priv_data;
1849
1850 bttv_dma_free(q,fh->btv,buf);
1851 }
1852
1853 static struct videobuf_queue_ops bttv_video_qops = {
1854 .buf_setup = buffer_setup,
1855 .buf_prepare = buffer_prepare,
1856 .buf_queue = buffer_queue,
1857 .buf_release = buffer_release,
1858 };
1859
1860 static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
1861 {
1862 struct bttv_fh *fh = priv;
1863 struct bttv *btv = fh->btv;
1864 unsigned int i;
1865 int err;
1866
1867 mutex_lock(&btv->lock);
1868 err = v4l2_prio_check(&btv->prio, fh->prio);
1869 if (err)
1870 goto err;
1871
1872 for (i = 0; i < BTTV_TVNORMS; i++)
1873 if (*id & bttv_tvnorms[i].v4l2_id)
1874 break;
1875 if (i == BTTV_TVNORMS) {
1876 err = -EINVAL;
1877 goto err;
1878 }
1879
1880 set_tvnorm(btv, i);
1881
1882 err:
1883 mutex_unlock(&btv->lock);
1884
1885 return err;
1886 }
1887
1888 static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1889 {
1890 struct bttv_fh *fh = f;
1891 struct bttv *btv = fh->btv;
1892
1893 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1894 *id = V4L2_STD_625_50;
1895 else
1896 *id = V4L2_STD_525_60;
1897 return 0;
1898 }
1899
1900 static int bttv_enum_input(struct file *file, void *priv,
1901 struct v4l2_input *i)
1902 {
1903 struct bttv_fh *fh = priv;
1904 struct bttv *btv = fh->btv;
1905 int rc = 0;
1906
1907 mutex_lock(&btv->lock);
1908 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) {
1909 rc = -EINVAL;
1910 goto err;
1911 }
1912
1913 i->type = V4L2_INPUT_TYPE_CAMERA;
1914 i->audioset = 1;
1915
1916 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
1917 sprintf(i->name, "Television");
1918 i->type = V4L2_INPUT_TYPE_TUNER;
1919 i->tuner = 0;
1920 } else if (i->index == btv->svhs) {
1921 sprintf(i->name, "S-Video");
1922 } else {
1923 sprintf(i->name, "Composite%d", i->index);
1924 }
1925
1926 if (i->index == btv->input) {
1927 __u32 dstatus = btread(BT848_DSTATUS);
1928 if (0 == (dstatus & BT848_DSTATUS_PRES))
1929 i->status |= V4L2_IN_ST_NO_SIGNAL;
1930 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1931 i->status |= V4L2_IN_ST_NO_H_LOCK;
1932 }
1933
1934 i->std = BTTV_NORMS;
1935
1936 err:
1937 mutex_unlock(&btv->lock);
1938
1939 return rc;
1940 }
1941
1942 static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1943 {
1944 struct bttv_fh *fh = priv;
1945 struct bttv *btv = fh->btv;
1946
1947 mutex_lock(&btv->lock);
1948 *i = btv->input;
1949 mutex_unlock(&btv->lock);
1950
1951 return 0;
1952 }
1953
1954 static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1955 {
1956 struct bttv_fh *fh = priv;
1957 struct bttv *btv = fh->btv;
1958
1959 int err;
1960
1961 mutex_lock(&btv->lock);
1962 err = v4l2_prio_check(&btv->prio, fh->prio);
1963 if (unlikely(err))
1964 goto err;
1965
1966 if (i > bttv_tvcards[btv->c.type].video_inputs) {
1967 err = -EINVAL;
1968 goto err;
1969 }
1970
1971 set_input(btv, i, btv->tvnorm);
1972
1973 err:
1974 mutex_unlock(&btv->lock);
1975 return 0;
1976 }
1977
1978 static int bttv_s_tuner(struct file *file, void *priv,
1979 struct v4l2_tuner *t)
1980 {
1981 struct bttv_fh *fh = priv;
1982 struct bttv *btv = fh->btv;
1983 int err;
1984
1985 if (unlikely(0 != t->index))
1986 return -EINVAL;
1987
1988 mutex_lock(&btv->lock);
1989 if (unlikely(btv->tuner_type == TUNER_ABSENT)) {
1990 err = -EINVAL;
1991 goto err;
1992 }
1993
1994 err = v4l2_prio_check(&btv->prio, fh->prio);
1995 if (unlikely(err))
1996 goto err;
1997
1998 bttv_call_all(btv, tuner, s_tuner, t);
1999
2000 if (btv->audio_mode_gpio)
2001 btv->audio_mode_gpio(btv, t, 1);
2002
2003 err:
2004 mutex_unlock(&btv->lock);
2005
2006 return 0;
2007 }
2008
2009 static int bttv_g_frequency(struct file *file, void *priv,
2010 struct v4l2_frequency *f)
2011 {
2012 struct bttv_fh *fh = priv;
2013 struct bttv *btv = fh->btv;
2014
2015 mutex_lock(&btv->lock);
2016 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2017 f->frequency = btv->freq;
2018 mutex_unlock(&btv->lock);
2019
2020 return 0;
2021 }
2022
2023 static int bttv_s_frequency(struct file *file, void *priv,
2024 struct v4l2_frequency *f)
2025 {
2026 struct bttv_fh *fh = priv;
2027 struct bttv *btv = fh->btv;
2028 int err;
2029
2030 if (unlikely(f->tuner != 0))
2031 return -EINVAL;
2032
2033 mutex_lock(&btv->lock);
2034 err = v4l2_prio_check(&btv->prio, fh->prio);
2035 if (unlikely(err))
2036 goto err;
2037
2038 if (unlikely(f->type != (btv->radio_user
2039 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV))) {
2040 err = -EINVAL;
2041 goto err;
2042 }
2043 btv->freq = f->frequency;
2044 bttv_call_all(btv, tuner, s_frequency, f);
2045 if (btv->has_matchbox && btv->radio_user)
2046 tea5757_set_freq(btv, btv->freq);
2047 err:
2048 mutex_unlock(&btv->lock);
2049
2050 return 0;
2051 }
2052
2053 static int bttv_log_status(struct file *file, void *f)
2054 {
2055 struct bttv_fh *fh = f;
2056 struct bttv *btv = fh->btv;
2057
2058 printk(KERN_INFO "bttv%d: ======== START STATUS CARD #%d ========\n",
2059 btv->c.nr, btv->c.nr);
2060 bttv_call_all(btv, core, log_status);
2061 printk(KERN_INFO "bttv%d: ======== END STATUS CARD #%d ========\n",
2062 btv->c.nr, btv->c.nr);
2063 return 0;
2064 }
2065
2066 #ifdef CONFIG_VIDEO_ADV_DEBUG
2067 static int bttv_g_register(struct file *file, void *f,
2068 struct v4l2_dbg_register *reg)
2069 {
2070 struct bttv_fh *fh = f;
2071 struct bttv *btv = fh->btv;
2072
2073 if (!capable(CAP_SYS_ADMIN))
2074 return -EPERM;
2075
2076 if (!v4l2_chip_match_host(&reg->match))
2077 return -EINVAL;
2078
2079 /* bt848 has a 12-bit register space */
2080 reg->reg &= 0xfff;
2081 reg->val = btread(reg->reg);
2082 reg->size = 1;
2083
2084 return 0;
2085 }
2086
2087 static int bttv_s_register(struct file *file, void *f,
2088 struct v4l2_dbg_register *reg)
2089 {
2090 struct bttv_fh *fh = f;
2091 struct bttv *btv = fh->btv;
2092
2093 if (!capable(CAP_SYS_ADMIN))
2094 return -EPERM;
2095
2096 if (!v4l2_chip_match_host(&reg->match))
2097 return -EINVAL;
2098
2099 /* bt848 has a 12-bit register space */
2100 reg->reg &= 0xfff;
2101 btwrite(reg->val, reg->reg);
2102
2103 return 0;
2104 }
2105 #endif
2106
2107 /* Given cropping boundaries b and the scaled width and height of a
2108 single field or frame, which must not exceed hardware limits, this
2109 function adjusts the cropping parameters c. */
2110 static void
2111 bttv_crop_adjust (struct bttv_crop * c,
2112 const struct v4l2_rect * b,
2113 __s32 width,
2114 __s32 height,
2115 enum v4l2_field field)
2116 {
2117 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2118 __s32 max_left;
2119 __s32 max_top;
2120
2121 if (width < c->min_scaled_width) {
2122 /* Max. hor. scale factor 16:1. */
2123 c->rect.width = width * 16;
2124 } else if (width > c->max_scaled_width) {
2125 /* Min. hor. scale factor 1:1. */
2126 c->rect.width = width;
2127
2128 max_left = b->left + b->width - width;
2129 max_left = min(max_left, (__s32) MAX_HDELAY);
2130 if (c->rect.left > max_left)
2131 c->rect.left = max_left;
2132 }
2133
2134 if (height < c->min_scaled_height) {
2135 /* Max. vert. scale factor 16:1, single fields 8:1. */
2136 c->rect.height = height * 16;
2137 } else if (frame_height > c->max_scaled_height) {
2138 /* Min. vert. scale factor 1:1.
2139 Top and height count field lines times two. */
2140 c->rect.height = (frame_height + 1) & ~1;
2141
2142 max_top = b->top + b->height - c->rect.height;
2143 if (c->rect.top > max_top)
2144 c->rect.top = max_top;
2145 }
2146
2147 bttv_crop_calc_limits(c);
2148 }
2149
2150 /* Returns an error if scaling to a frame or single field with the given
2151 width and height is not possible with the current cropping parameters
2152 and width aligned according to width_mask. If adjust_size is TRUE the
2153 function may adjust the width and/or height instead, rounding width
2154 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2155 also adjust the current cropping parameters to get closer to the
2156 desired image size. */
2157 static int
2158 limit_scaled_size_lock (struct bttv_fh * fh,
2159 __s32 * width,
2160 __s32 * height,
2161 enum v4l2_field field,
2162 unsigned int width_mask,
2163 unsigned int width_bias,
2164 int adjust_size,
2165 int adjust_crop)
2166 {
2167 struct bttv *btv = fh->btv;
2168 const struct v4l2_rect *b;
2169 struct bttv_crop *c;
2170 __s32 min_width;
2171 __s32 min_height;
2172 __s32 max_width;
2173 __s32 max_height;
2174 int rc;
2175
2176 BUG_ON((int) width_mask >= 0 ||
2177 width_bias >= (unsigned int) -width_mask);
2178
2179 /* Make sure tvnorm, vbi_end and the current cropping parameters
2180 remain consistent until we're done. */
2181 mutex_lock(&btv->lock);
2182
2183 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2184
2185 /* Do crop - use current, don't - use default parameters. */
2186 c = &btv->crop[!!fh->do_crop];
2187
2188 if (fh->do_crop
2189 && adjust_size
2190 && adjust_crop
2191 && !locked_btres(btv, VIDEO_RESOURCES)) {
2192 min_width = 48;
2193 min_height = 32;
2194
2195 /* We cannot scale up. When the scaled image is larger
2196 than crop.rect we adjust the crop.rect as required
2197 by the V4L2 spec, hence cropcap.bounds are our limit. */
2198 max_width = min(b->width, (__s32) MAX_HACTIVE);
2199 max_height = b->height;
2200
2201 /* We cannot capture the same line as video and VBI data.
2202 Note btv->vbi_end is really a minimum, see
2203 bttv_vbi_try_fmt(). */
2204 if (btv->vbi_end > b->top) {
2205 max_height -= btv->vbi_end - b->top;
2206 rc = -EBUSY;
2207 if (min_height > max_height)
2208 goto fail;
2209 }
2210 } else {
2211 rc = -EBUSY;
2212 if (btv->vbi_end > c->rect.top)
2213 goto fail;
2214
2215 min_width = c->min_scaled_width;
2216 min_height = c->min_scaled_height;
2217 max_width = c->max_scaled_width;
2218 max_height = c->max_scaled_height;
2219
2220 adjust_crop = 0;
2221 }
2222
2223 min_width = (min_width - width_mask - 1) & width_mask;
2224 max_width = max_width & width_mask;
2225
2226 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2227 min_height = min_height;
2228 /* Min. scale factor is 1:1. */
2229 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2230
2231 if (adjust_size) {
2232 *width = clamp(*width, min_width, max_width);
2233 *height = clamp(*height, min_height, max_height);
2234
2235 /* Round after clamping to avoid overflow. */
2236 *width = (*width + width_bias) & width_mask;
2237
2238 if (adjust_crop) {
2239 bttv_crop_adjust(c, b, *width, *height, field);
2240
2241 if (btv->vbi_end > c->rect.top) {
2242 /* Move the crop window out of the way. */
2243 c->rect.top = btv->vbi_end;
2244 }
2245 }
2246 } else {
2247 rc = -EINVAL;
2248 if (*width < min_width ||
2249 *height < min_height ||
2250 *width > max_width ||
2251 *height > max_height ||
2252 0 != (*width & ~width_mask))
2253 goto fail;
2254 }
2255
2256 rc = 0; /* success */
2257
2258 fail:
2259 mutex_unlock(&btv->lock);
2260
2261 return rc;
2262 }
2263
2264 /* Returns an error if the given overlay window dimensions are not
2265 possible with the current cropping parameters. If adjust_size is
2266 TRUE the function may adjust the window width and/or height
2267 instead, however it always rounds the horizontal position and
2268 width as btcx_align() does. If adjust_crop is TRUE the function
2269 may also adjust the current cropping parameters to get closer
2270 to the desired window size. */
2271 static int
2272 verify_window_lock (struct bttv_fh * fh,
2273 struct v4l2_window * win,
2274 int adjust_size,
2275 int adjust_crop)
2276 {
2277 enum v4l2_field field;
2278 unsigned int width_mask;
2279 int rc;
2280
2281 if (win->w.width < 48 || win->w.height < 32)
2282 return -EINVAL;
2283 if (win->clipcount > 2048)
2284 return -EINVAL;
2285
2286 field = win->field;
2287
2288 if (V4L2_FIELD_ANY == field) {
2289 __s32 height2;
2290
2291 mutex_lock(&fh->btv->lock);
2292 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2293 mutex_unlock(&fh->btv->lock);
2294 field = (win->w.height > height2)
2295 ? V4L2_FIELD_INTERLACED
2296 : V4L2_FIELD_TOP;
2297 }
2298 switch (field) {
2299 case V4L2_FIELD_TOP:
2300 case V4L2_FIELD_BOTTOM:
2301 case V4L2_FIELD_INTERLACED:
2302 break;
2303 default:
2304 return -EINVAL;
2305 }
2306
2307 /* 4-byte alignment. */
2308 if (NULL == fh->ovfmt)
2309 return -EINVAL;
2310 width_mask = ~0;
2311 switch (fh->ovfmt->depth) {
2312 case 8:
2313 case 24:
2314 width_mask = ~3;
2315 break;
2316 case 16:
2317 width_mask = ~1;
2318 break;
2319 case 32:
2320 break;
2321 default:
2322 BUG();
2323 }
2324
2325 win->w.width -= win->w.left & ~width_mask;
2326 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2327
2328 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
2329 field, width_mask,
2330 /* width_bias: round down */ 0,
2331 adjust_size, adjust_crop);
2332 if (0 != rc)
2333 return rc;
2334
2335 win->field = field;
2336 return 0;
2337 }
2338
2339 static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
2340 struct v4l2_window *win, int fixup)
2341 {
2342 struct v4l2_clip *clips = NULL;
2343 int n,size,retval = 0;
2344
2345 if (NULL == fh->ovfmt)
2346 return -EINVAL;
2347 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2348 return -EINVAL;
2349 retval = verify_window_lock(fh, win,
2350 /* adjust_size */ fixup,
2351 /* adjust_crop */ fixup);
2352 if (0 != retval)
2353 return retval;
2354
2355 /* copy clips -- luckily v4l1 + v4l2 are binary
2356 compatible here ...*/
2357 n = win->clipcount;
2358 size = sizeof(*clips)*(n+4);
2359 clips = kmalloc(size,GFP_KERNEL);
2360 if (NULL == clips)
2361 return -ENOMEM;
2362 if (n > 0) {
2363 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2364 kfree(clips);
2365 return -EFAULT;
2366 }
2367 }
2368
2369 mutex_lock(&fh->cap.vb_lock);
2370 /* clip against screen */
2371 if (NULL != btv->fbuf.base)
2372 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2373 &win->w, clips, n);
2374 btcx_sort_clips(clips,n);
2375
2376 /* 4-byte alignments */
2377 switch (fh->ovfmt->depth) {
2378 case 8:
2379 case 24:
2380 btcx_align(&win->w, clips, n, 3);
2381 break;
2382 case 16:
2383 btcx_align(&win->w, clips, n, 1);
2384 break;
2385 case 32:
2386 /* no alignment fixups needed */
2387 break;
2388 default:
2389 BUG();
2390 }
2391
2392 kfree(fh->ov.clips);
2393 fh->ov.clips = clips;
2394 fh->ov.nclips = n;
2395
2396 fh->ov.w = win->w;
2397 fh->ov.field = win->field;
2398 fh->ov.setup_ok = 1;
2399
2400 /*
2401 * FIXME: btv is protected by btv->lock mutex, while btv->init
2402 * is protected by fh->cap.vb_lock. This seems to open the
2403 * possibility for some race situations. Maybe the better would
2404 * be to unify those locks or to use another way to store the
2405 * init values that will be consumed by videobuf callbacks
2406 */
2407 btv->init.ov.w.width = win->w.width;
2408 btv->init.ov.w.height = win->w.height;
2409 btv->init.ov.field = win->field;
2410
2411 /* update overlay if needed */
2412 retval = 0;
2413 if (check_btres(fh, RESOURCE_OVERLAY)) {
2414 struct bttv_buffer *new;
2415
2416 new = videobuf_sg_alloc(sizeof(*new));
2417 new->crop = btv->crop[!!fh->do_crop].rect;
2418 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2419 retval = bttv_switch_overlay(btv,fh,new);
2420 }
2421 mutex_unlock(&fh->cap.vb_lock);
2422 return retval;
2423 }
2424
2425 /* ----------------------------------------------------------------------- */
2426
2427 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2428 {
2429 struct videobuf_queue* q = NULL;
2430
2431 switch (fh->type) {
2432 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2433 q = &fh->cap;
2434 break;
2435 case V4L2_BUF_TYPE_VBI_CAPTURE:
2436 q = &fh->vbi;
2437 break;
2438 default:
2439 BUG();
2440 }
2441 return q;
2442 }
2443
2444 static int bttv_resource(struct bttv_fh *fh)
2445 {
2446 int res = 0;
2447
2448 switch (fh->type) {
2449 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2450 res = RESOURCE_VIDEO_STREAM;
2451 break;
2452 case V4L2_BUF_TYPE_VBI_CAPTURE:
2453 res = RESOURCE_VBI;
2454 break;
2455 default:
2456 BUG();
2457 }
2458 return res;
2459 }
2460
2461 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2462 {
2463 struct videobuf_queue *q = bttv_queue(fh);
2464 int res = bttv_resource(fh);
2465
2466 if (check_btres(fh,res))
2467 return -EBUSY;
2468 if (videobuf_queue_is_busy(q))
2469 return -EBUSY;
2470 fh->type = type;
2471 return 0;
2472 }
2473
2474 static void
2475 pix_format_set_size (struct v4l2_pix_format * f,
2476 const struct bttv_format * fmt,
2477 unsigned int width,
2478 unsigned int height)
2479 {
2480 f->width = width;
2481 f->height = height;
2482
2483 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2484 f->bytesperline = width; /* Y plane */
2485 f->sizeimage = (width * height * fmt->depth) >> 3;
2486 } else {
2487 f->bytesperline = (width * fmt->depth) >> 3;
2488 f->sizeimage = height * f->bytesperline;
2489 }
2490 }
2491
2492 static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
2493 struct v4l2_format *f)
2494 {
2495 struct bttv_fh *fh = priv;
2496
2497 pix_format_set_size(&f->fmt.pix, fh->fmt,
2498 fh->width, fh->height);
2499 f->fmt.pix.field = fh->cap.field;
2500 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2501
2502 return 0;
2503 }
2504
2505 static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
2506 struct v4l2_format *f)
2507 {
2508 struct bttv_fh *fh = priv;
2509
2510 f->fmt.win.w = fh->ov.w;
2511 f->fmt.win.field = fh->ov.field;
2512
2513 return 0;
2514 }
2515
2516 static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2517 struct v4l2_format *f)
2518 {
2519 const struct bttv_format *fmt;
2520 struct bttv_fh *fh = priv;
2521 struct bttv *btv = fh->btv;
2522 enum v4l2_field field;
2523 __s32 width, height;
2524 int rc;
2525
2526 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2527 if (NULL == fmt)
2528 return -EINVAL;
2529
2530 field = f->fmt.pix.field;
2531
2532 if (V4L2_FIELD_ANY == field) {
2533 __s32 height2;
2534
2535 mutex_lock(&btv->lock);
2536 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2537 mutex_unlock(&btv->lock);
2538 field = (f->fmt.pix.height > height2)
2539 ? V4L2_FIELD_INTERLACED
2540 : V4L2_FIELD_BOTTOM;
2541 }
2542
2543 if (V4L2_FIELD_SEQ_BT == field)
2544 field = V4L2_FIELD_SEQ_TB;
2545
2546 switch (field) {
2547 case V4L2_FIELD_TOP:
2548 case V4L2_FIELD_BOTTOM:
2549 case V4L2_FIELD_ALTERNATE:
2550 case V4L2_FIELD_INTERLACED:
2551 break;
2552 case V4L2_FIELD_SEQ_TB:
2553 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2554 return -EINVAL;
2555 break;
2556 default:
2557 return -EINVAL;
2558 }
2559
2560 width = f->fmt.pix.width;
2561 height = f->fmt.pix.height;
2562
2563 rc = limit_scaled_size_lock(fh, &width, &height, field,
2564 /* width_mask: 4 pixels */ ~3,
2565 /* width_bias: nearest */ 2,
2566 /* adjust_size */ 1,
2567 /* adjust_crop */ 0);
2568 if (0 != rc)
2569 return rc;
2570
2571 /* update data for the application */
2572 f->fmt.pix.field = field;
2573 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2574
2575 return 0;
2576 }
2577
2578 static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
2579 struct v4l2_format *f)
2580 {
2581 struct bttv_fh *fh = priv;
2582
2583 return verify_window_lock(fh, &f->fmt.win,
2584 /* adjust_size */ 1,
2585 /* adjust_crop */ 0);
2586 }
2587
2588 static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2589 struct v4l2_format *f)
2590 {
2591 int retval;
2592 const struct bttv_format *fmt;
2593 struct bttv_fh *fh = priv;
2594 struct bttv *btv = fh->btv;
2595 __s32 width, height;
2596 enum v4l2_field field;
2597
2598 retval = bttv_switch_type(fh, f->type);
2599 if (0 != retval)
2600 return retval;
2601
2602 retval = bttv_try_fmt_vid_cap(file, priv, f);
2603 if (0 != retval)
2604 return retval;
2605
2606 width = f->fmt.pix.width;
2607 height = f->fmt.pix.height;
2608 field = f->fmt.pix.field;
2609
2610 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
2611 /* width_mask: 4 pixels */ ~3,
2612 /* width_bias: nearest */ 2,
2613 /* adjust_size */ 1,
2614 /* adjust_crop */ 1);
2615 if (0 != retval)
2616 return retval;
2617
2618 f->fmt.pix.field = field;
2619
2620 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2621
2622 /* update our state informations */
2623 mutex_lock(&fh->cap.vb_lock);
2624 fh->fmt = fmt;
2625 fh->cap.field = f->fmt.pix.field;
2626 fh->cap.last = V4L2_FIELD_NONE;
2627 fh->width = f->fmt.pix.width;
2628 fh->height = f->fmt.pix.height;
2629 btv->init.fmt = fmt;
2630 btv->init.width = f->fmt.pix.width;
2631 btv->init.height = f->fmt.pix.height;
2632 mutex_unlock(&fh->cap.vb_lock);
2633
2634 return 0;
2635 }
2636
2637 static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
2638 struct v4l2_format *f)
2639 {
2640 struct bttv_fh *fh = priv;
2641 struct bttv *btv = fh->btv;
2642
2643 if (no_overlay > 0) {
2644 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2645 return -EINVAL;
2646 }
2647
2648 return setup_window_lock(fh, btv, &f->fmt.win, 1);
2649 }
2650
2651 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2652 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2653 {
2654 int retval;
2655 unsigned int i;
2656 struct bttv_fh *fh = priv;
2657
2658 mutex_lock(&fh->cap.vb_lock);
2659 retval = __videobuf_mmap_setup(&fh->cap, gbuffers, gbufsize,
2660 V4L2_MEMORY_MMAP);
2661 if (retval < 0) {
2662 mutex_unlock(&fh->cap.vb_lock);
2663 return retval;
2664 }
2665
2666 gbuffers = retval;
2667 memset(mbuf, 0, sizeof(*mbuf));
2668 mbuf->frames = gbuffers;
2669 mbuf->size = gbuffers * gbufsize;
2670
2671 for (i = 0; i < gbuffers; i++)
2672 mbuf->offsets[i] = i * gbufsize;
2673
2674 mutex_unlock(&fh->cap.vb_lock);
2675 return 0;
2676 }
2677 #endif
2678
2679 static int bttv_querycap(struct file *file, void *priv,
2680 struct v4l2_capability *cap)
2681 {
2682 struct bttv_fh *fh = priv;
2683 struct bttv *btv = fh->btv;
2684
2685 if (0 == v4l2)
2686 return -EINVAL;
2687
2688 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2689 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2690 snprintf(cap->bus_info, sizeof(cap->bus_info),
2691 "PCI:%s", pci_name(btv->c.pci));
2692 cap->version = BTTV_VERSION_CODE;
2693 cap->capabilities =
2694 V4L2_CAP_VIDEO_CAPTURE |
2695 V4L2_CAP_VBI_CAPTURE |
2696 V4L2_CAP_READWRITE |
2697 V4L2_CAP_STREAMING;
2698 if (no_overlay <= 0)
2699 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2700
2701 /*
2702 * No need to lock here: those vars are initialized during board
2703 * probe and remains untouched during the rest of the driver lifecycle
2704 */
2705 if (btv->has_saa6588)
2706 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
2707 if (btv->tuner_type != TUNER_ABSENT)
2708 cap->capabilities |= V4L2_CAP_TUNER;
2709 return 0;
2710 }
2711
2712 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2713 {
2714 int index = -1, i;
2715
2716 for (i = 0; i < FORMATS; i++) {
2717 if (formats[i].fourcc != -1)
2718 index++;
2719 if ((unsigned int)index == f->index)
2720 break;
2721 }
2722 if (FORMATS == i)
2723 return -EINVAL;
2724
2725 f->pixelformat = formats[i].fourcc;
2726 strlcpy(f->description, formats[i].name, sizeof(f->description));
2727
2728 return i;
2729 }
2730
2731 static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
2732 struct v4l2_fmtdesc *f)
2733 {
2734 int rc = bttv_enum_fmt_cap_ovr(f);
2735
2736 if (rc < 0)
2737 return rc;
2738
2739 return 0;
2740 }
2741
2742 static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
2743 struct v4l2_fmtdesc *f)
2744 {
2745 int rc;
2746
2747 if (no_overlay > 0) {
2748 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2749 return -EINVAL;
2750 }
2751
2752 rc = bttv_enum_fmt_cap_ovr(f);
2753
2754 if (rc < 0)
2755 return rc;
2756
2757 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
2758 return -EINVAL;
2759
2760 return 0;
2761 }
2762
2763 static int bttv_g_fbuf(struct file *file, void *f,
2764 struct v4l2_framebuffer *fb)
2765 {
2766 struct bttv_fh *fh = f;
2767 struct bttv *btv = fh->btv;
2768
2769 *fb = btv->fbuf;
2770 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2771 if (fh->ovfmt)
2772 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2773 return 0;
2774 }
2775
2776 static int bttv_overlay(struct file *file, void *f, unsigned int on)
2777 {
2778 struct bttv_fh *fh = f;
2779 struct bttv *btv = fh->btv;
2780 struct bttv_buffer *new;
2781 int retval = 0;
2782
2783 if (on) {
2784 mutex_lock(&fh->cap.vb_lock);
2785 /* verify args */
2786 if (unlikely(!btv->fbuf.base)) {
2787 mutex_unlock(&fh->cap.vb_lock);
2788 return -EINVAL;
2789 }
2790 if (unlikely(!fh->ov.setup_ok)) {
2791 dprintk("bttv%d: overlay: !setup_ok\n", btv->c.nr);
2792 retval = -EINVAL;
2793 }
2794 if (retval)
2795 return retval;
2796 mutex_unlock(&fh->cap.vb_lock);
2797 }
2798
2799 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
2800 return -EBUSY;
2801
2802 mutex_lock(&fh->cap.vb_lock);
2803 if (on) {
2804 fh->ov.tvnorm = btv->tvnorm;
2805 new = videobuf_sg_alloc(sizeof(*new));
2806 new->crop = btv->crop[!!fh->do_crop].rect;
2807 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2808 } else {
2809 new = NULL;
2810 }
2811
2812 /* switch over */
2813 retval = bttv_switch_overlay(btv, fh, new);
2814 mutex_unlock(&fh->cap.vb_lock);
2815 return retval;
2816 }
2817
2818 static int bttv_s_fbuf(struct file *file, void *f,
2819 struct v4l2_framebuffer *fb)
2820 {
2821 struct bttv_fh *fh = f;
2822 struct bttv *btv = fh->btv;
2823 const struct bttv_format *fmt;
2824 int retval;
2825
2826 if (!capable(CAP_SYS_ADMIN) &&
2827 !capable(CAP_SYS_RAWIO))
2828 return -EPERM;
2829
2830 /* check args */
2831 fmt = format_by_fourcc(fb->fmt.pixelformat);
2832 if (NULL == fmt)
2833 return -EINVAL;
2834 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2835 return -EINVAL;
2836
2837 retval = -EINVAL;
2838 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2839 __s32 width = fb->fmt.width;
2840 __s32 height = fb->fmt.height;
2841
2842 retval = limit_scaled_size_lock(fh, &width, &height,
2843 V4L2_FIELD_INTERLACED,
2844 /* width_mask */ ~3,
2845 /* width_bias */ 2,
2846 /* adjust_size */ 0,
2847 /* adjust_crop */ 0);
2848 if (0 != retval)
2849 return retval;
2850 }
2851
2852 /* ok, accept it */
2853 mutex_lock(&fh->cap.vb_lock);
2854 btv->fbuf.base = fb->base;
2855 btv->fbuf.fmt.width = fb->fmt.width;
2856 btv->fbuf.fmt.height = fb->fmt.height;
2857 if (0 != fb->fmt.bytesperline)
2858 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2859 else
2860 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2861
2862 retval = 0;
2863 fh->ovfmt = fmt;
2864 btv->init.ovfmt = fmt;
2865 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2866 fh->ov.w.left = 0;
2867 fh->ov.w.top = 0;
2868 fh->ov.w.width = fb->fmt.width;
2869 fh->ov.w.height = fb->fmt.height;
2870 btv->init.ov.w.width = fb->fmt.width;
2871 btv->init.ov.w.height = fb->fmt.height;
2872 kfree(fh->ov.clips);
2873 fh->ov.clips = NULL;
2874 fh->ov.nclips = 0;
2875
2876 if (check_btres(fh, RESOURCE_OVERLAY)) {
2877 struct bttv_buffer *new;
2878
2879 new = videobuf_sg_alloc(sizeof(*new));
2880 new->crop = btv->crop[!!fh->do_crop].rect;
2881 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2882 retval = bttv_switch_overlay(btv, fh, new);
2883 }
2884 }
2885 mutex_unlock(&fh->cap.vb_lock);
2886 return retval;
2887 }
2888
2889 static int bttv_reqbufs(struct file *file, void *priv,
2890 struct v4l2_requestbuffers *p)
2891 {
2892 struct bttv_fh *fh = priv;
2893 return videobuf_reqbufs(bttv_queue(fh), p);
2894 }
2895
2896 static int bttv_querybuf(struct file *file, void *priv,
2897 struct v4l2_buffer *b)
2898 {
2899 struct bttv_fh *fh = priv;
2900 return videobuf_querybuf(bttv_queue(fh), b);
2901 }
2902
2903 static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2904 {
2905 struct bttv_fh *fh = priv;
2906 struct bttv *btv = fh->btv;
2907 int res = bttv_resource(fh);
2908
2909 if (!check_alloc_btres_lock(btv, fh, res))
2910 return -EBUSY;
2911
2912 return videobuf_qbuf(bttv_queue(fh), b);
2913 }
2914
2915 static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2916 {
2917 struct bttv_fh *fh = priv;
2918 return videobuf_dqbuf(bttv_queue(fh), b,
2919 file->f_flags & O_NONBLOCK);
2920 }
2921
2922 static int bttv_streamon(struct file *file, void *priv,
2923 enum v4l2_buf_type type)
2924 {
2925 struct bttv_fh *fh = priv;
2926 struct bttv *btv = fh->btv;
2927 int res = bttv_resource(fh);
2928
2929 if (!check_alloc_btres_lock(btv, fh, res))
2930 return -EBUSY;
2931 return videobuf_streamon(bttv_queue(fh));
2932 }
2933
2934
2935 static int bttv_streamoff(struct file *file, void *priv,
2936 enum v4l2_buf_type type)
2937 {
2938 struct bttv_fh *fh = priv;
2939 struct bttv *btv = fh->btv;
2940 int retval;
2941 int res = bttv_resource(fh);
2942
2943
2944 retval = videobuf_streamoff(bttv_queue(fh));
2945 if (retval < 0)
2946 return retval;
2947 free_btres_lock(btv, fh, res);
2948 return 0;
2949 }
2950
2951 static int bttv_queryctrl(struct file *file, void *priv,
2952 struct v4l2_queryctrl *c)
2953 {
2954 struct bttv_fh *fh = priv;
2955 struct bttv *btv = fh->btv;
2956 const struct v4l2_queryctrl *ctrl;
2957
2958 if ((c->id < V4L2_CID_BASE ||
2959 c->id >= V4L2_CID_LASTP1) &&
2960 (c->id < V4L2_CID_PRIVATE_BASE ||
2961 c->id >= V4L2_CID_PRIVATE_LASTP1))
2962 return -EINVAL;
2963
2964 mutex_lock(&btv->lock);
2965 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2966 *c = no_ctl;
2967 else {
2968 ctrl = ctrl_by_id(c->id);
2969
2970 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2971 }
2972 mutex_unlock(&btv->lock);
2973
2974 return 0;
2975 }
2976
2977 static int bttv_g_parm(struct file *file, void *f,
2978 struct v4l2_streamparm *parm)
2979 {
2980 struct bttv_fh *fh = f;
2981 struct bttv *btv = fh->btv;
2982
2983 mutex_lock(&btv->lock);
2984 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2985 &parm->parm.capture.timeperframe);
2986 mutex_unlock(&btv->lock);
2987
2988 return 0;
2989 }
2990
2991 static int bttv_g_tuner(struct file *file, void *priv,
2992 struct v4l2_tuner *t)
2993 {
2994 struct bttv_fh *fh = priv;
2995 struct bttv *btv = fh->btv;
2996
2997 if (btv->tuner_type == TUNER_ABSENT)
2998 return -EINVAL;
2999 if (0 != t->index)
3000 return -EINVAL;
3001
3002 mutex_lock(&btv->lock);
3003 t->rxsubchans = V4L2_TUNER_SUB_MONO;
3004 bttv_call_all(btv, tuner, g_tuner, t);
3005 strcpy(t->name, "Television");
3006 t->capability = V4L2_TUNER_CAP_NORM;
3007 t->type = V4L2_TUNER_ANALOG_TV;
3008 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
3009 t->signal = 0xffff;
3010
3011 if (btv->audio_mode_gpio)
3012 btv->audio_mode_gpio(btv, t, 0);
3013
3014 mutex_unlock(&btv->lock);
3015 return 0;
3016 }
3017
3018 static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
3019 {
3020 struct bttv_fh *fh = f;
3021 struct bttv *btv = fh->btv;
3022
3023 mutex_lock(&btv->lock);
3024 *p = v4l2_prio_max(&btv->prio);
3025 mutex_unlock(&btv->lock);
3026
3027 return 0;
3028 }
3029
3030 static int bttv_s_priority(struct file *file, void *f,
3031 enum v4l2_priority prio)
3032 {
3033 struct bttv_fh *fh = f;
3034 struct bttv *btv = fh->btv;
3035 int rc;
3036
3037 mutex_lock(&btv->lock);
3038 rc = v4l2_prio_change(&btv->prio, &fh->prio, prio);
3039 mutex_unlock(&btv->lock);
3040
3041 return rc;
3042 }
3043
3044 static int bttv_cropcap(struct file *file, void *priv,
3045 struct v4l2_cropcap *cap)
3046 {
3047 struct bttv_fh *fh = priv;
3048 struct bttv *btv = fh->btv;
3049
3050 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3051 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3052 return -EINVAL;
3053
3054 mutex_lock(&btv->lock);
3055 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3056 mutex_unlock(&btv->lock);
3057
3058 return 0;
3059 }
3060
3061 static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
3062 {
3063 struct bttv_fh *fh = f;
3064 struct bttv *btv = fh->btv;
3065
3066 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3067 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3068 return -EINVAL;
3069
3070 /* No fh->do_crop = 1; because btv->crop[1] may be
3071 inconsistent with fh->width or fh->height and apps
3072 do not expect a change here. */
3073
3074 mutex_lock(&btv->lock);
3075 crop->c = btv->crop[!!fh->do_crop].rect;
3076 mutex_unlock(&btv->lock);
3077
3078 return 0;
3079 }
3080
3081 static int bttv_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
3082 {
3083 struct bttv_fh *fh = f;
3084 struct bttv *btv = fh->btv;
3085 const struct v4l2_rect *b;
3086 int retval;
3087 struct bttv_crop c;
3088 __s32 b_left;
3089 __s32 b_top;
3090 __s32 b_right;
3091 __s32 b_bottom;
3092
3093 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3094 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3095 return -EINVAL;
3096
3097 /* Make sure tvnorm, vbi_end and the current cropping
3098 parameters remain consistent until we're done. Note
3099 read() may change vbi_end in check_alloc_btres_lock(). */
3100 mutex_lock(&btv->lock);
3101 retval = v4l2_prio_check(&btv->prio, fh->prio);
3102 if (0 != retval) {
3103 mutex_unlock(&btv->lock);
3104 return retval;
3105 }
3106
3107 retval = -EBUSY;
3108
3109 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
3110 mutex_unlock(&btv->lock);
3111 return retval;
3112 }
3113
3114 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3115
3116 b_left = b->left;
3117 b_right = b_left + b->width;
3118 b_bottom = b->top + b->height;
3119
3120 b_top = max(b->top, btv->vbi_end);
3121 if (b_top + 32 >= b_bottom) {
3122 mutex_unlock(&btv->lock);
3123 return retval;
3124 }
3125
3126 /* Min. scaled size 48 x 32. */
3127 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3128 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3129
3130 c.rect.width = clamp(crop->c.width,
3131 48, b_right - c.rect.left);
3132
3133 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3134 /* Top and height must be a multiple of two. */
3135 c.rect.top = (c.rect.top + 1) & ~1;
3136
3137 c.rect.height = clamp(crop->c.height,
3138 32, b_bottom - c.rect.top);
3139 c.rect.height = (c.rect.height + 1) & ~1;
3140
3141 bttv_crop_calc_limits(&c);
3142
3143 btv->crop[1] = c;
3144
3145 mutex_unlock(&btv->lock);
3146
3147 fh->do_crop = 1;
3148
3149 mutex_lock(&fh->cap.vb_lock);
3150
3151 if (fh->width < c.min_scaled_width) {
3152 fh->width = c.min_scaled_width;
3153 btv->init.width = c.min_scaled_width;
3154 } else if (fh->width > c.max_scaled_width) {
3155 fh->width = c.max_scaled_width;
3156 btv->init.width = c.max_scaled_width;
3157 }
3158
3159 if (fh->height < c.min_scaled_height) {
3160 fh->height = c.min_scaled_height;
3161 btv->init.height = c.min_scaled_height;
3162 } else if (fh->height > c.max_scaled_height) {
3163 fh->height = c.max_scaled_height;
3164 btv->init.height = c.max_scaled_height;
3165 }
3166
3167 mutex_unlock(&fh->cap.vb_lock);
3168
3169 return 0;
3170 }
3171
3172 static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
3173 {
3174 if (unlikely(a->index))
3175 return -EINVAL;
3176
3177 strcpy(a->name, "audio");
3178 return 0;
3179 }
3180
3181 static int bttv_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
3182 {
3183 if (unlikely(a->index))
3184 return -EINVAL;
3185
3186 return 0;
3187 }
3188
3189 static ssize_t bttv_read(struct file *file, char __user *data,
3190 size_t count, loff_t *ppos)
3191 {
3192 struct bttv_fh *fh = file->private_data;
3193 int retval = 0;
3194
3195 if (fh->btv->errors)
3196 bttv_reinit_bt848(fh->btv);
3197 dprintk("bttv%d: read count=%d type=%s\n",
3198 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3199
3200 switch (fh->type) {
3201 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3202 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3203 /* VIDEO_READ in use by another fh,
3204 or VIDEO_STREAM by any fh. */
3205 return -EBUSY;
3206 }
3207 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3208 file->f_flags & O_NONBLOCK);
3209 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
3210 break;
3211 case V4L2_BUF_TYPE_VBI_CAPTURE:
3212 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
3213 return -EBUSY;
3214 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3215 file->f_flags & O_NONBLOCK);
3216 break;
3217 default:
3218 BUG();
3219 }
3220 return retval;
3221 }
3222
3223 static unsigned int bttv_poll(struct file *file, poll_table *wait)
3224 {
3225 struct bttv_fh *fh = file->private_data;
3226 struct bttv_buffer *buf;
3227 enum v4l2_field field;
3228 unsigned int rc = POLLERR;
3229
3230 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3231 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
3232 return POLLERR;
3233 return videobuf_poll_stream(file, &fh->vbi, wait);
3234 }
3235
3236 mutex_lock(&fh->cap.vb_lock);
3237 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
3238 /* streaming capture */
3239 if (list_empty(&fh->cap.stream))
3240 goto err;
3241 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3242 } else {
3243 /* read() capture */
3244 if (NULL == fh->cap.read_buf) {
3245 /* need to capture a new frame */
3246 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3247 goto err;
3248 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
3249 if (NULL == fh->cap.read_buf)
3250 goto err;
3251 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3252 field = videobuf_next_field(&fh->cap);
3253 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
3254 kfree (fh->cap.read_buf);
3255 fh->cap.read_buf = NULL;
3256 goto err;
3257 }
3258 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3259 fh->cap.read_off = 0;
3260 }
3261 buf = (struct bttv_buffer*)fh->cap.read_buf;
3262 }
3263
3264 poll_wait(file, &buf->vb.done, wait);
3265 if (buf->vb.state == VIDEOBUF_DONE ||
3266 buf->vb.state == VIDEOBUF_ERROR)
3267 rc = POLLIN|POLLRDNORM;
3268 else
3269 rc = 0;
3270 err:
3271 mutex_unlock(&fh->cap.vb_lock);
3272 return rc;
3273 }
3274
3275 static int bttv_open(struct file *file)
3276 {
3277 struct video_device *vdev = video_devdata(file);
3278 struct bttv *btv = video_drvdata(file);
3279 struct bttv_fh *fh;
3280 enum v4l2_buf_type type = 0;
3281
3282 dprintk(KERN_DEBUG "bttv: open dev=%s\n", video_device_node_name(vdev));
3283
3284 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
3285 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3286 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
3287 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3288 } else {
3289 WARN_ON(1);
3290 return -ENODEV;
3291 }
3292
3293 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3294 btv->c.nr,v4l2_type_names[type]);
3295
3296 /* allocate per filehandle data */
3297 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3298 if (unlikely(!fh))
3299 return -ENOMEM;
3300 file->private_data = fh;
3301
3302 /*
3303 * btv is protected by btv->lock mutex, while btv->init and other
3304 * streaming vars are protected by fh->cap.vb_lock. We need to take
3305 * care of both locks to avoid troubles. However, vb_lock is used also
3306 * inside videobuf, without calling buf->lock. So, it is a very bad
3307 * idea to hold both locks at the same time.
3308 * Let's first copy btv->init at fh, holding cap.vb_lock, and then work
3309 * with the rest of init, holding btv->lock.
3310 */
3311 mutex_lock(&fh->cap.vb_lock);
3312 *fh = btv->init;
3313 mutex_unlock(&fh->cap.vb_lock);
3314
3315 fh->type = type;
3316 fh->ov.setup_ok = 0;
3317
3318 mutex_lock(&btv->lock);
3319 v4l2_prio_open(&btv->prio, &fh->prio);
3320
3321 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3322 &btv->c.pci->dev, &btv->s_lock,
3323 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3324 V4L2_FIELD_INTERLACED,
3325 sizeof(struct bttv_buffer),
3326 fh, NULL);
3327 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3328 &btv->c.pci->dev, &btv->s_lock,
3329 V4L2_BUF_TYPE_VBI_CAPTURE,
3330 V4L2_FIELD_SEQ_TB,
3331 sizeof(struct bttv_buffer),
3332 fh, NULL);
3333 set_tvnorm(btv,btv->tvnorm);
3334 set_input(btv, btv->input, btv->tvnorm);
3335
3336 btv->users++;
3337
3338 /* The V4L2 spec requires one global set of cropping parameters
3339 which only change on request. These are stored in btv->crop[1].
3340 However for compatibility with V4L apps and cropping unaware
3341 V4L2 apps we now reset the cropping parameters as seen through
3342 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3343 will use btv->crop[0], the default cropping parameters for the
3344 current video standard, and VIDIOC_S_FMT will not implicitely
3345 change the cropping parameters until VIDIOC_S_CROP has been
3346 called. */
3347 fh->do_crop = !reset_crop; /* module parameter */
3348
3349 /* Likewise there should be one global set of VBI capture
3350 parameters, but for compatibility with V4L apps and earlier
3351 driver versions each fh has its own parameters. */
3352 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3353
3354 bttv_field_count(btv);
3355 mutex_unlock(&btv->lock);
3356 return 0;
3357 }
3358
3359 static int bttv_release(struct file *file)
3360 {
3361 struct bttv_fh *fh = file->private_data;
3362 struct bttv *btv = fh->btv;
3363
3364 mutex_lock(&btv->lock);
3365 /* turn off overlay */
3366 if (check_btres(fh, RESOURCE_OVERLAY))
3367 bttv_switch_overlay(btv,fh,NULL);
3368
3369 /* stop video capture */
3370 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
3371 videobuf_streamoff(&fh->cap);
3372 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
3373 }
3374 if (fh->cap.read_buf) {
3375 buffer_release(&fh->cap,fh->cap.read_buf);
3376 kfree(fh->cap.read_buf);
3377 }
3378 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3379 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
3380 }
3381
3382 /* stop vbi capture */
3383 if (check_btres(fh, RESOURCE_VBI)) {
3384 videobuf_stop(&fh->vbi);
3385 free_btres_lock(btv,fh,RESOURCE_VBI);
3386 }
3387
3388 /* free stuff */
3389
3390 /*
3391 * videobuf uses cap.vb_lock - we should avoid holding btv->lock,
3392 * otherwise we may have dead lock conditions
3393 */
3394 mutex_unlock(&btv->lock);
3395 videobuf_mmap_free(&fh->cap);
3396 videobuf_mmap_free(&fh->vbi);
3397 mutex_lock(&btv->lock);
3398 v4l2_prio_close(&btv->prio, fh->prio);
3399 file->private_data = NULL;
3400 kfree(fh);
3401
3402 btv->users--;
3403 bttv_field_count(btv);
3404
3405 if (!btv->users)
3406 audio_mute(btv, 1);
3407 mutex_unlock(&btv->lock);
3408
3409 return 0;
3410 }
3411
3412 static int
3413 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3414 {
3415 struct bttv_fh *fh = file->private_data;
3416
3417 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3418 fh->btv->c.nr, v4l2_type_names[fh->type],
3419 vma->vm_start, vma->vm_end - vma->vm_start);
3420 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3421 }
3422
3423 static const struct v4l2_file_operations bttv_fops =
3424 {
3425 .owner = THIS_MODULE,
3426 .open = bttv_open,
3427 .release = bttv_release,
3428 .unlocked_ioctl = video_ioctl2,
3429 .read = bttv_read,
3430 .mmap = bttv_mmap,
3431 .poll = bttv_poll,
3432 };
3433
3434 static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
3435 .vidioc_querycap = bttv_querycap,
3436 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3437 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3438 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3439 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3440 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3441 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3442 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3443 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
3444 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3445 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3446 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
3447 .vidioc_g_audio = bttv_g_audio,
3448 .vidioc_s_audio = bttv_s_audio,
3449 .vidioc_cropcap = bttv_cropcap,
3450 .vidioc_reqbufs = bttv_reqbufs,
3451 .vidioc_querybuf = bttv_querybuf,
3452 .vidioc_qbuf = bttv_qbuf,
3453 .vidioc_dqbuf = bttv_dqbuf,
3454 .vidioc_s_std = bttv_s_std,
3455 .vidioc_enum_input = bttv_enum_input,
3456 .vidioc_g_input = bttv_g_input,
3457 .vidioc_s_input = bttv_s_input,
3458 .vidioc_queryctrl = bttv_queryctrl,
3459 .vidioc_g_ctrl = bttv_g_ctrl,
3460 .vidioc_s_ctrl = bttv_s_ctrl,
3461 .vidioc_streamon = bttv_streamon,
3462 .vidioc_streamoff = bttv_streamoff,
3463 .vidioc_g_tuner = bttv_g_tuner,
3464 .vidioc_s_tuner = bttv_s_tuner,
3465 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3466 .vidiocgmbuf = vidiocgmbuf,
3467 #endif
3468 .vidioc_g_crop = bttv_g_crop,
3469 .vidioc_s_crop = bttv_s_crop,
3470 .vidioc_g_fbuf = bttv_g_fbuf,
3471 .vidioc_s_fbuf = bttv_s_fbuf,
3472 .vidioc_overlay = bttv_overlay,
3473 .vidioc_g_priority = bttv_g_priority,
3474 .vidioc_s_priority = bttv_s_priority,
3475 .vidioc_g_parm = bttv_g_parm,
3476 .vidioc_g_frequency = bttv_g_frequency,
3477 .vidioc_s_frequency = bttv_s_frequency,
3478 .vidioc_log_status = bttv_log_status,
3479 .vidioc_querystd = bttv_querystd,
3480 #ifdef CONFIG_VIDEO_ADV_DEBUG
3481 .vidioc_g_register = bttv_g_register,
3482 .vidioc_s_register = bttv_s_register,
3483 #endif
3484 };
3485
3486 static struct video_device bttv_video_template = {
3487 .fops = &bttv_fops,
3488 .ioctl_ops = &bttv_ioctl_ops,
3489 .tvnorms = BTTV_NORMS,
3490 .current_norm = V4L2_STD_PAL,
3491 };
3492
3493 /* ----------------------------------------------------------------------- */
3494 /* radio interface */
3495
3496 static int radio_open(struct file *file)
3497 {
3498 struct video_device *vdev = video_devdata(file);
3499 struct bttv *btv = video_drvdata(file);
3500 struct bttv_fh *fh;
3501
3502 dprintk("bttv: open dev=%s\n", video_device_node_name(vdev));
3503
3504 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3505
3506 /* allocate per filehandle data */
3507 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3508 if (unlikely(!fh))
3509 return -ENOMEM;
3510 file->private_data = fh;
3511 mutex_lock(&fh->cap.vb_lock);
3512 *fh = btv->init;
3513 mutex_unlock(&fh->cap.vb_lock);
3514
3515 mutex_lock(&btv->lock);
3516 v4l2_prio_open(&btv->prio, &fh->prio);
3517
3518 btv->radio_user++;
3519
3520 bttv_call_all(btv, tuner, s_radio);
3521 audio_input(btv,TVAUDIO_INPUT_RADIO);
3522
3523 mutex_unlock(&btv->lock);
3524 return 0;
3525 }
3526
3527 static int radio_release(struct file *file)
3528 {
3529 struct bttv_fh *fh = file->private_data;
3530 struct bttv *btv = fh->btv;
3531 struct rds_command cmd;
3532
3533 mutex_lock(&btv->lock);
3534 v4l2_prio_close(&btv->prio, fh->prio);
3535 file->private_data = NULL;
3536 kfree(fh);
3537
3538 btv->radio_user--;
3539
3540 bttv_call_all(btv, core, ioctl, RDS_CMD_CLOSE, &cmd);
3541 mutex_unlock(&btv->lock);
3542
3543 return 0;
3544 }
3545
3546 static int radio_querycap(struct file *file, void *priv,
3547 struct v4l2_capability *cap)
3548 {
3549 struct bttv_fh *fh = priv;
3550 struct bttv *btv = fh->btv;
3551
3552 strcpy(cap->driver, "bttv");
3553 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
3554 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
3555 cap->version = BTTV_VERSION_CODE;
3556 cap->capabilities = V4L2_CAP_TUNER;
3557
3558 return 0;
3559 }
3560
3561 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
3562 {
3563 struct bttv_fh *fh = priv;
3564 struct bttv *btv = fh->btv;
3565
3566 if (btv->tuner_type == TUNER_ABSENT)
3567 return -EINVAL;
3568 if (0 != t->index)
3569 return -EINVAL;
3570 mutex_lock(&btv->lock);
3571 strcpy(t->name, "Radio");
3572 t->type = V4L2_TUNER_RADIO;
3573
3574 bttv_call_all(btv, tuner, g_tuner, t);
3575
3576 if (btv->audio_mode_gpio)
3577 btv->audio_mode_gpio(btv, t, 0);
3578
3579 mutex_unlock(&btv->lock);
3580
3581 return 0;
3582 }
3583
3584 static int radio_enum_input(struct file *file, void *priv,
3585 struct v4l2_input *i)
3586 {
3587 if (i->index != 0)
3588 return -EINVAL;
3589
3590 strcpy(i->name, "Radio");
3591 i->type = V4L2_INPUT_TYPE_TUNER;
3592
3593 return 0;
3594 }
3595
3596 static int radio_g_audio(struct file *file, void *priv,
3597 struct v4l2_audio *a)
3598 {
3599 if (unlikely(a->index))
3600 return -EINVAL;
3601
3602 strcpy(a->name, "Radio");
3603
3604 return 0;
3605 }
3606
3607 static int radio_s_tuner(struct file *file, void *priv,
3608 struct v4l2_tuner *t)
3609 {
3610 struct bttv_fh *fh = priv;
3611 struct bttv *btv = fh->btv;
3612
3613 if (0 != t->index)
3614 return -EINVAL;
3615
3616 bttv_call_all(btv, tuner, g_tuner, t);
3617 return 0;
3618 }
3619
3620 static int radio_s_audio(struct file *file, void *priv,
3621 struct v4l2_audio *a)
3622 {
3623 if (unlikely(a->index))
3624 return -EINVAL;
3625
3626 return 0;
3627 }
3628
3629 static int radio_s_input(struct file *filp, void *priv, unsigned int i)
3630 {
3631 if (unlikely(i))
3632 return -EINVAL;
3633
3634 return 0;
3635 }
3636
3637 static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
3638 {
3639 return 0;
3640 }
3641
3642 static int radio_queryctrl(struct file *file, void *priv,
3643 struct v4l2_queryctrl *c)
3644 {
3645 const struct v4l2_queryctrl *ctrl;
3646
3647 if (c->id < V4L2_CID_BASE ||
3648 c->id >= V4L2_CID_LASTP1)
3649 return -EINVAL;
3650
3651 if (c->id == V4L2_CID_AUDIO_MUTE) {
3652 ctrl = ctrl_by_id(c->id);
3653 *c = *ctrl;
3654 } else
3655 *c = no_ctl;
3656
3657 return 0;
3658 }
3659
3660 static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
3661 {
3662 *i = 0;
3663 return 0;
3664 }
3665
3666 static ssize_t radio_read(struct file *file, char __user *data,
3667 size_t count, loff_t *ppos)
3668 {
3669 struct bttv_fh *fh = file->private_data;
3670 struct bttv *btv = fh->btv;
3671 struct rds_command cmd;
3672 cmd.block_count = count/3;
3673 cmd.buffer = data;
3674 cmd.instance = file;
3675 cmd.result = -ENODEV;
3676
3677 bttv_call_all(btv, core, ioctl, RDS_CMD_READ, &cmd);
3678
3679 return cmd.result;
3680 }
3681
3682 static unsigned int radio_poll(struct file *file, poll_table *wait)
3683 {
3684 struct bttv_fh *fh = file->private_data;
3685 struct bttv *btv = fh->btv;
3686 struct rds_command cmd;
3687 cmd.instance = file;
3688 cmd.event_list = wait;
3689 cmd.result = -ENODEV;
3690 bttv_call_all(btv, core, ioctl, RDS_CMD_POLL, &cmd);
3691
3692 return cmd.result;
3693 }
3694
3695 static const struct v4l2_file_operations radio_fops =
3696 {
3697 .owner = THIS_MODULE,
3698 .open = radio_open,
3699 .read = radio_read,
3700 .release = radio_release,
3701 .ioctl = video_ioctl2,
3702 .poll = radio_poll,
3703 };
3704
3705 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
3706 .vidioc_querycap = radio_querycap,
3707 .vidioc_g_tuner = radio_g_tuner,
3708 .vidioc_enum_input = radio_enum_input,
3709 .vidioc_g_audio = radio_g_audio,
3710 .vidioc_s_tuner = radio_s_tuner,
3711 .vidioc_s_audio = radio_s_audio,
3712 .vidioc_s_input = radio_s_input,
3713 .vidioc_s_std = radio_s_std,
3714 .vidioc_queryctrl = radio_queryctrl,
3715 .vidioc_g_input = radio_g_input,
3716 .vidioc_g_ctrl = bttv_g_ctrl,
3717 .vidioc_s_ctrl = bttv_s_ctrl,
3718 .vidioc_g_frequency = bttv_g_frequency,
3719 .vidioc_s_frequency = bttv_s_frequency,
3720 };
3721
3722 static struct video_device radio_template = {
3723 .fops = &radio_fops,
3724 .ioctl_ops = &radio_ioctl_ops,
3725 };
3726
3727 /* ----------------------------------------------------------------------- */
3728 /* some debug code */
3729
3730 static int bttv_risc_decode(u32 risc)
3731 {
3732 static char *instr[16] = {
3733 [ BT848_RISC_WRITE >> 28 ] = "write",
3734 [ BT848_RISC_SKIP >> 28 ] = "skip",
3735 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3736 [ BT848_RISC_JUMP >> 28 ] = "jump",
3737 [ BT848_RISC_SYNC >> 28 ] = "sync",
3738 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3739 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3740 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3741 };
3742 static int incr[16] = {
3743 [ BT848_RISC_WRITE >> 28 ] = 2,
3744 [ BT848_RISC_JUMP >> 28 ] = 2,
3745 [ BT848_RISC_SYNC >> 28 ] = 2,
3746 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3747 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3748 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3749 };
3750 static char *bits[] = {
3751 "be0", "be1", "be2", "be3/resync",
3752 "set0", "set1", "set2", "set3",
3753 "clr0", "clr1", "clr2", "clr3",
3754 "irq", "res", "eol", "sol",
3755 };
3756 int i;
3757
3758 printk("0x%08x [ %s", risc,
3759 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3760 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3761 if (risc & (1 << (i + 12)))
3762 printk(" %s",bits[i]);
3763 printk(" count=%d ]\n", risc & 0xfff);
3764 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3765 }
3766
3767 static void bttv_risc_disasm(struct bttv *btv,
3768 struct btcx_riscmem *risc)
3769 {
3770 unsigned int i,j,n;
3771
3772 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3773 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
3774 for (i = 0; i < (risc->size >> 2); i += n) {
3775 printk("%s: 0x%lx: ", btv->c.v4l2_dev.name,
3776 (unsigned long)(risc->dma + (i<<2)));
3777 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
3778 for (j = 1; j < n; j++)
3779 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3780 btv->c.v4l2_dev.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3781 risc->cpu[i+j], j);
3782 if (0 == risc->cpu[i])
3783 break;
3784 }
3785 }
3786
3787 static void bttv_print_riscaddr(struct bttv *btv)
3788 {
3789 printk(" main: %08Lx\n",
3790 (unsigned long long)btv->main.dma);
3791 printk(" vbi : o=%08Lx e=%08Lx\n",
3792 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3793 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3794 printk(" cap : o=%08Lx e=%08Lx\n",
3795 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3796 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3797 printk(" scr : o=%08Lx e=%08Lx\n",
3798 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3799 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3800 bttv_risc_disasm(btv, &btv->main);
3801 }
3802
3803 /* ----------------------------------------------------------------------- */
3804 /* irq handler */
3805
3806 static char *irq_name[] = {
3807 "FMTCHG", // format change detected (525 vs. 625)
3808 "VSYNC", // vertical sync (new field)
3809 "HSYNC", // horizontal sync
3810 "OFLOW", // chroma/luma AGC overflow
3811 "HLOCK", // horizontal lock changed
3812 "VPRES", // video presence changed
3813 "6", "7",
3814 "I2CDONE", // hw irc operation finished
3815 "GPINT", // gpio port triggered irq
3816 "10",
3817 "RISCI", // risc instruction triggered irq
3818 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3819 "FTRGT", // pixel data fifo overrun
3820 "FDSR", // fifo data stream resyncronisation
3821 "PPERR", // parity error (data transfer)
3822 "RIPERR", // parity error (read risc instructions)
3823 "PABORT", // pci abort
3824 "OCERR", // risc instruction error
3825 "SCERR", // syncronisation error
3826 };
3827
3828 static void bttv_print_irqbits(u32 print, u32 mark)
3829 {
3830 unsigned int i;
3831
3832 printk("bits:");
3833 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3834 if (print & (1 << i))
3835 printk(" %s",irq_name[i]);
3836 if (mark & (1 << i))
3837 printk("*");
3838 }
3839 }
3840
3841 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3842 {
3843 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3844 btv->c.nr,
3845 (unsigned long)btv->main.dma,
3846 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3847 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3848 (unsigned long)rc);
3849
3850 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3851 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3852 "Ok, then this is harmless, don't worry ;)\n",
3853 btv->c.nr);
3854 return;
3855 }
3856 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3857 btv->c.nr);
3858 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3859 btv->c.nr);
3860 dump_stack();
3861 }
3862
3863 static int
3864 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3865 {
3866 struct bttv_buffer *item;
3867
3868 memset(set,0,sizeof(*set));
3869
3870 /* capture request ? */
3871 if (!list_empty(&btv->capture)) {
3872 set->frame_irq = 1;
3873 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3874 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3875 set->top = item;
3876 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3877 set->bottom = item;
3878
3879 /* capture request for other field ? */
3880 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3881 (item->vb.queue.next != &btv->capture)) {
3882 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3883 /* Mike Isely <isely@pobox.com> - Only check
3884 * and set up the bottom field in the logic
3885 * below. Don't ever do the top field. This
3886 * of course means that if we set up the
3887 * bottom field in the above code that we'll
3888 * actually skip a field. But that's OK.
3889 * Having processed only a single buffer this
3890 * time, then the next time around the first
3891 * available buffer should be for a top field.
3892 * That will then cause us here to set up a
3893 * top then a bottom field in the normal way.
3894 * The alternative to this understanding is
3895 * that we set up the second available buffer
3896 * as a top field, but that's out of order
3897 * since this driver always processes the top
3898 * field first - the effect will be the two
3899 * buffers being returned in the wrong order,
3900 * with the second buffer also being delayed
3901 * by one field time (owing to the fifo nature
3902 * of videobuf). Worse still, we'll be stuck
3903 * doing fields out of order now every time
3904 * until something else causes a field to be
3905 * dropped. By effectively forcing a field to
3906 * drop this way then we always get back into
3907 * sync within a single frame time. (Out of
3908 * order fields can screw up deinterlacing
3909 * algorithms.) */
3910 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3911 if (NULL == set->bottom &&
3912 V4L2_FIELD_BOTTOM == item->vb.field) {
3913 set->bottom = item;
3914 }
3915 if (NULL != set->top && NULL != set->bottom)
3916 set->top_irq = 2;
3917 }
3918 }
3919 }
3920
3921 /* screen overlay ? */
3922 if (NULL != btv->screen) {
3923 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3924 if (NULL == set->top && NULL == set->bottom) {
3925 set->top = btv->screen;
3926 set->bottom = btv->screen;
3927 }
3928 } else {
3929 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3930 NULL == set->top) {
3931 set->top = btv->screen;
3932 }
3933 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3934 NULL == set->bottom) {
3935 set->bottom = btv->screen;
3936 }
3937 }
3938 }
3939
3940 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3941 btv->c.nr,set->top, set->bottom,
3942 btv->screen,set->frame_irq,set->top_irq);
3943 return 0;
3944 }
3945
3946 static void
3947 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3948 struct bttv_buffer_set *curr, unsigned int state)
3949 {
3950 struct timeval ts;
3951
3952 do_gettimeofday(&ts);
3953
3954 if (wakeup->top == wakeup->bottom) {
3955 if (NULL != wakeup->top && curr->top != wakeup->top) {
3956 if (irq_debug > 1)
3957 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3958 wakeup->top->vb.ts = ts;
3959 wakeup->top->vb.field_count = btv->field_count;
3960 wakeup->top->vb.state = state;
3961 wake_up(&wakeup->top->vb.done);
3962 }
3963 } else {
3964 if (NULL != wakeup->top && curr->top != wakeup->top) {
3965 if (irq_debug > 1)
3966 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3967 wakeup->top->vb.ts = ts;
3968 wakeup->top->vb.field_count = btv->field_count;
3969 wakeup->top->vb.state = state;
3970 wake_up(&wakeup->top->vb.done);
3971 }
3972 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3973 if (irq_debug > 1)
3974 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3975 wakeup->bottom->vb.ts = ts;
3976 wakeup->bottom->vb.field_count = btv->field_count;
3977 wakeup->bottom->vb.state = state;
3978 wake_up(&wakeup->bottom->vb.done);
3979 }
3980 }
3981 }
3982
3983 static void
3984 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3985 unsigned int state)
3986 {
3987 struct timeval ts;
3988
3989 if (NULL == wakeup)
3990 return;
3991
3992 do_gettimeofday(&ts);
3993 wakeup->vb.ts = ts;
3994 wakeup->vb.field_count = btv->field_count;
3995 wakeup->vb.state = state;
3996 wake_up(&wakeup->vb.done);
3997 }
3998
3999 static void bttv_irq_timeout(unsigned long data)
4000 {
4001 struct bttv *btv = (struct bttv *)data;
4002 struct bttv_buffer_set old,new;
4003 struct bttv_buffer *ovbi;
4004 struct bttv_buffer *item;
4005 unsigned long flags;
4006
4007 if (bttv_verbose) {
4008 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
4009 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
4010 btread(BT848_RISC_COUNT));
4011 bttv_print_irqbits(btread(BT848_INT_STAT),0);
4012 printk("\n");
4013 }
4014
4015 spin_lock_irqsave(&btv->s_lock,flags);
4016
4017 /* deactivate stuff */
4018 memset(&new,0,sizeof(new));
4019 old = btv->curr;
4020 ovbi = btv->cvbi;
4021 btv->curr = new;
4022 btv->cvbi = NULL;
4023 btv->loop_irq = 0;
4024 bttv_buffer_activate_video(btv, &new);
4025 bttv_buffer_activate_vbi(btv, NULL);
4026 bttv_set_dma(btv, 0);
4027
4028 /* wake up */
4029 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
4030 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
4031
4032 /* cancel all outstanding capture / vbi requests */
4033 while (!list_empty(&btv->capture)) {
4034 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
4035 list_del(&item->vb.queue);
4036 item->vb.state = VIDEOBUF_ERROR;
4037 wake_up(&item->vb.done);
4038 }
4039 while (!list_empty(&btv->vcapture)) {
4040 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4041 list_del(&item->vb.queue);
4042 item->vb.state = VIDEOBUF_ERROR;
4043 wake_up(&item->vb.done);
4044 }
4045
4046 btv->errors++;
4047 spin_unlock_irqrestore(&btv->s_lock,flags);
4048 }
4049
4050 static void
4051 bttv_irq_wakeup_top(struct bttv *btv)
4052 {
4053 struct bttv_buffer *wakeup = btv->curr.top;
4054
4055 if (NULL == wakeup)
4056 return;
4057
4058 spin_lock(&btv->s_lock);
4059 btv->curr.top_irq = 0;
4060 btv->curr.top = NULL;
4061 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
4062
4063 do_gettimeofday(&wakeup->vb.ts);
4064 wakeup->vb.field_count = btv->field_count;
4065 wakeup->vb.state = VIDEOBUF_DONE;
4066 wake_up(&wakeup->vb.done);
4067 spin_unlock(&btv->s_lock);
4068 }
4069
4070 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
4071 {
4072 if (rc < risc->dma)
4073 return 0;
4074 if (rc > risc->dma + risc->size)
4075 return 0;
4076 return 1;
4077 }
4078
4079 static void
4080 bttv_irq_switch_video(struct bttv *btv)
4081 {
4082 struct bttv_buffer_set new;
4083 struct bttv_buffer_set old;
4084 dma_addr_t rc;
4085
4086 spin_lock(&btv->s_lock);
4087
4088 /* new buffer set */
4089 bttv_irq_next_video(btv, &new);
4090 rc = btread(BT848_RISC_COUNT);
4091 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
4092 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
4093 btv->framedrop++;
4094 if (debug_latency)
4095 bttv_irq_debug_low_latency(btv, rc);
4096 spin_unlock(&btv->s_lock);
4097 return;
4098 }
4099
4100 /* switch over */
4101 old = btv->curr;
4102 btv->curr = new;
4103 btv->loop_irq &= ~1;
4104 bttv_buffer_activate_video(btv, &new);
4105 bttv_set_dma(btv, 0);
4106
4107 /* switch input */
4108 if (UNSET != btv->new_input) {
4109 video_mux(btv,btv->new_input);
4110 btv->new_input = UNSET;
4111 }
4112
4113 /* wake up finished buffers */
4114 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
4115 spin_unlock(&btv->s_lock);
4116 }
4117
4118 static void
4119 bttv_irq_switch_vbi(struct bttv *btv)
4120 {
4121 struct bttv_buffer *new = NULL;
4122 struct bttv_buffer *old;
4123 u32 rc;
4124
4125 spin_lock(&btv->s_lock);
4126
4127 if (!list_empty(&btv->vcapture))
4128 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4129 old = btv->cvbi;
4130
4131 rc = btread(BT848_RISC_COUNT);
4132 if (NULL != old && (is_active(&old->top, rc) ||
4133 is_active(&old->bottom, rc))) {
4134 btv->framedrop++;
4135 if (debug_latency)
4136 bttv_irq_debug_low_latency(btv, rc);
4137 spin_unlock(&btv->s_lock);
4138 return;
4139 }
4140
4141 /* switch */
4142 btv->cvbi = new;
4143 btv->loop_irq &= ~4;
4144 bttv_buffer_activate_vbi(btv, new);
4145 bttv_set_dma(btv, 0);
4146
4147 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
4148 spin_unlock(&btv->s_lock);
4149 }
4150
4151 static irqreturn_t bttv_irq(int irq, void *dev_id)
4152 {
4153 u32 stat,astat;
4154 u32 dstat;
4155 int count;
4156 struct bttv *btv;
4157 int handled = 0;
4158
4159 btv=(struct bttv *)dev_id;
4160
4161 if (btv->custom_irq)
4162 handled = btv->custom_irq(btv);
4163
4164 count=0;
4165 while (1) {
4166 /* get/clear interrupt status bits */
4167 stat=btread(BT848_INT_STAT);
4168 astat=stat&btread(BT848_INT_MASK);
4169 if (!astat)
4170 break;
4171 handled = 1;
4172 btwrite(stat,BT848_INT_STAT);
4173
4174 /* get device status bits */
4175 dstat=btread(BT848_DSTATUS);
4176
4177 if (irq_debug) {
4178 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4179 "riscs=%x, riscc=%08x, ",
4180 btv->c.nr, count, btv->field_count,
4181 stat>>28, btread(BT848_RISC_COUNT));
4182 bttv_print_irqbits(stat,astat);
4183 if (stat & BT848_INT_HLOCK)
4184 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4185 ? "yes" : "no");
4186 if (stat & BT848_INT_VPRES)
4187 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4188 ? "yes" : "no");
4189 if (stat & BT848_INT_FMTCHG)
4190 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4191 ? "625" : "525");
4192 printk("\n");
4193 }
4194
4195 if (astat&BT848_INT_VSYNC)
4196 btv->field_count++;
4197
4198 if ((astat & BT848_INT_GPINT) && btv->remote) {
4199 wake_up(&btv->gpioq);
4200 bttv_input_irq(btv);
4201 }
4202
4203 if (astat & BT848_INT_I2CDONE) {
4204 btv->i2c_done = stat;
4205 wake_up(&btv->i2c_queue);
4206 }
4207
4208 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
4209 bttv_irq_switch_vbi(btv);
4210
4211 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
4212 bttv_irq_wakeup_top(btv);
4213
4214 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
4215 bttv_irq_switch_video(btv);
4216
4217 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
4218 audio_mute(btv, btv->mute); /* trigger automute */
4219
4220 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4221 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4222 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4223 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4224 btread(BT848_RISC_COUNT));
4225 bttv_print_irqbits(stat,astat);
4226 printk("\n");
4227 if (bttv_debug)
4228 bttv_print_riscaddr(btv);
4229 }
4230 if (fdsr && astat & BT848_INT_FDSR) {
4231 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4232 btv->c.nr,btread(BT848_RISC_COUNT));
4233 if (bttv_debug)
4234 bttv_print_riscaddr(btv);
4235 }
4236
4237 count++;
4238 if (count > 4) {
4239
4240 if (count > 8 || !(astat & BT848_INT_GPINT)) {
4241 btwrite(0, BT848_INT_MASK);
4242
4243 printk(KERN_ERR
4244 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4245 } else {
4246 printk(KERN_ERR
4247 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4248
4249 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4250 BT848_INT_MASK);
4251 };
4252
4253 bttv_print_irqbits(stat,astat);
4254
4255 printk("]\n");
4256 }
4257 }
4258 btv->irq_total++;
4259 if (handled)
4260 btv->irq_me++;
4261 return IRQ_RETVAL(handled);
4262 }
4263
4264
4265 /* ----------------------------------------------------------------------- */
4266 /* initialitation */
4267
4268 static struct video_device *vdev_init(struct bttv *btv,
4269 const struct video_device *template,
4270 const char *type_name)
4271 {
4272 struct video_device *vfd;
4273
4274 vfd = video_device_alloc();
4275 if (NULL == vfd)
4276 return NULL;
4277 *vfd = *template;
4278 vfd->v4l2_dev = &btv->c.v4l2_dev;
4279 vfd->release = video_device_release;
4280 vfd->debug = bttv_debug;
4281 video_set_drvdata(vfd, btv);
4282 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4283 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4284 type_name, bttv_tvcards[btv->c.type].name);
4285 return vfd;
4286 }
4287
4288 static void bttv_unregister_video(struct bttv *btv)
4289 {
4290 if (btv->video_dev) {
4291 if (video_is_registered(btv->video_dev))
4292 video_unregister_device(btv->video_dev);
4293 else
4294 video_device_release(btv->video_dev);
4295 btv->video_dev = NULL;
4296 }
4297 if (btv->vbi_dev) {
4298 if (video_is_registered(btv->vbi_dev))
4299 video_unregister_device(btv->vbi_dev);
4300 else
4301 video_device_release(btv->vbi_dev);
4302 btv->vbi_dev = NULL;
4303 }
4304 if (btv->radio_dev) {
4305 if (video_is_registered(btv->radio_dev))
4306 video_unregister_device(btv->radio_dev);
4307 else
4308 video_device_release(btv->radio_dev);
4309 btv->radio_dev = NULL;
4310 }
4311 }
4312
4313 /* register video4linux devices */
4314 static int __devinit bttv_register_video(struct bttv *btv)
4315 {
4316 if (no_overlay > 0)
4317 printk("bttv: Overlay support disabled.\n");
4318
4319 /* video */
4320 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
4321
4322 if (NULL == btv->video_dev)
4323 goto err;
4324 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
4325 video_nr[btv->c.nr]) < 0)
4326 goto err;
4327 printk(KERN_INFO "bttv%d: registered device %s\n",
4328 btv->c.nr, video_device_node_name(btv->video_dev));
4329 if (device_create_file(&btv->video_dev->dev,
4330 &dev_attr_card)<0) {
4331 printk(KERN_ERR "bttv%d: device_create_file 'card' "
4332 "failed\n", btv->c.nr);
4333 goto err;
4334 }
4335
4336 /* vbi */
4337 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
4338
4339 if (NULL == btv->vbi_dev)
4340 goto err;
4341 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
4342 vbi_nr[btv->c.nr]) < 0)
4343 goto err;
4344 printk(KERN_INFO "bttv%d: registered device %s\n",
4345 btv->c.nr, video_device_node_name(btv->vbi_dev));
4346
4347 if (!btv->has_radio)
4348 return 0;
4349 /* radio */
4350 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
4351 if (NULL == btv->radio_dev)
4352 goto err;
4353 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4354 radio_nr[btv->c.nr]) < 0)
4355 goto err;
4356 printk(KERN_INFO "bttv%d: registered device %s\n",
4357 btv->c.nr, video_device_node_name(btv->radio_dev));
4358
4359 /* all done */
4360 return 0;
4361
4362 err:
4363 bttv_unregister_video(btv);
4364 return -1;
4365 }
4366
4367
4368 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4369 /* response on cards with no firmware is not enabled by OF */
4370 static void pci_set_command(struct pci_dev *dev)
4371 {
4372 #if defined(__powerpc__)
4373 unsigned int cmd;
4374
4375 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4376 cmd = (cmd | PCI_COMMAND_MEMORY );
4377 pci_write_config_dword(dev, PCI_COMMAND, cmd);
4378 #endif
4379 }
4380
4381 static int __devinit bttv_probe(struct pci_dev *dev,
4382 const struct pci_device_id *pci_id)
4383 {
4384 int result;
4385 unsigned char lat;
4386 struct bttv *btv;
4387
4388 if (bttv_num == BTTV_MAX)
4389 return -ENOMEM;
4390 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
4391 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
4392 if (btv == NULL) {
4393 printk(KERN_ERR "bttv: out of memory.\n");
4394 return -ENOMEM;
4395 }
4396 btv->c.nr = bttv_num;
4397 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4398 "bttv%d", btv->c.nr);
4399
4400 /* initialize structs / fill in defaults */
4401 mutex_init(&btv->lock);
4402 spin_lock_init(&btv->s_lock);
4403 spin_lock_init(&btv->gpio_lock);
4404 init_waitqueue_head(&btv->gpioq);
4405 init_waitqueue_head(&btv->i2c_queue);
4406 INIT_LIST_HEAD(&btv->c.subs);
4407 INIT_LIST_HEAD(&btv->capture);
4408 INIT_LIST_HEAD(&btv->vcapture);
4409 v4l2_prio_init(&btv->prio);
4410
4411 init_timer(&btv->timeout);
4412 btv->timeout.function = bttv_irq_timeout;
4413 btv->timeout.data = (unsigned long)btv;
4414
4415 btv->i2c_rc = -1;
4416 btv->tuner_type = UNSET;
4417 btv->new_input = UNSET;
4418 btv->has_radio=radio[btv->c.nr];
4419
4420 /* pci stuff (init, get irq/mmio, ... */
4421 btv->c.pci = dev;
4422 btv->id = dev->device;
4423 if (pci_enable_device(dev)) {
4424 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4425 btv->c.nr);
4426 return -EIO;
4427 }
4428 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
4429 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
4430 btv->c.nr);
4431 return -EIO;
4432 }
4433 if (!request_mem_region(pci_resource_start(dev,0),
4434 pci_resource_len(dev,0),
4435 btv->c.v4l2_dev.name)) {
4436 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4437 btv->c.nr,
4438 (unsigned long long)pci_resource_start(dev,0));
4439 return -EBUSY;
4440 }
4441 pci_set_master(dev);
4442 pci_set_command(dev);
4443
4444 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4445 if (result < 0) {
4446 printk(KERN_WARNING "bttv%d: v4l2_device_register() failed\n", btv->c.nr);
4447 goto fail0;
4448 }
4449
4450 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4451 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4452 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4453 bttv_num,btv->id, btv->revision, pci_name(dev));
4454 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4455 btv->c.pci->irq, lat,
4456 (unsigned long long)pci_resource_start(dev,0));
4457 schedule();
4458
4459 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4460 if (NULL == btv->bt848_mmio) {
4461 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4462 result = -EIO;
4463 goto fail1;
4464 }
4465
4466 /* identify card */
4467 bttv_idcard(btv);
4468
4469 /* disable irqs, register irq handler */
4470 btwrite(0, BT848_INT_MASK);
4471 result = request_irq(btv->c.pci->irq, bttv_irq,
4472 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
4473 if (result < 0) {
4474 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
4475 bttv_num,btv->c.pci->irq);
4476 goto fail1;
4477 }
4478
4479 if (0 != bttv_handle_chipset(btv)) {
4480 result = -EIO;
4481 goto fail2;
4482 }
4483
4484 /* init options from insmod args */
4485 btv->opt_combfilter = combfilter;
4486 btv->opt_lumafilter = lumafilter;
4487 btv->opt_automute = automute;
4488 btv->opt_chroma_agc = chroma_agc;
4489 btv->opt_adc_crush = adc_crush;
4490 btv->opt_vcr_hack = vcr_hack;
4491 btv->opt_whitecrush_upper = whitecrush_upper;
4492 btv->opt_whitecrush_lower = whitecrush_lower;
4493 btv->opt_uv_ratio = uv_ratio;
4494 btv->opt_full_luma_range = full_luma_range;
4495 btv->opt_coring = coring;
4496
4497 /* fill struct bttv with some useful defaults */
4498 btv->init.btv = btv;
4499 btv->init.ov.w.width = 320;
4500 btv->init.ov.w.height = 240;
4501 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
4502 btv->init.width = 320;
4503 btv->init.height = 240;
4504 btv->input = 0;
4505
4506 /* initialize hardware */
4507 if (bttv_gpio)
4508 bttv_gpio_tracking(btv,"pre-init");
4509
4510 bttv_risc_init_main(btv);
4511 init_bt848(btv);
4512
4513 /* gpio */
4514 btwrite(0x00, BT848_GPIO_REG_INP);
4515 btwrite(0x00, BT848_GPIO_OUT_EN);
4516 if (bttv_verbose)
4517 bttv_gpio_tracking(btv,"init");
4518
4519 /* needs to be done before i2c is registered */
4520 bttv_init_card1(btv);
4521
4522 /* register i2c + gpio */
4523 init_bttv_i2c(btv);
4524
4525 /* some card-specific stuff (needs working i2c) */
4526 bttv_init_card2(btv);
4527 bttv_init_tuner(btv);
4528 init_irqreg(btv);
4529
4530 /* register video4linux + input */
4531 if (!bttv_tvcards[btv->c.type].no_video) {
4532 bttv_register_video(btv);
4533 bt848_bright(btv,32768);
4534 bt848_contrast(btv,32768);
4535 bt848_hue(btv,32768);
4536 bt848_sat(btv,32768);
4537 audio_mute(btv, 1);
4538 set_input(btv, 0, btv->tvnorm);
4539 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4540 btv->crop[1] = btv->crop[0]; /* current = default */
4541 disclaim_vbi_lines(btv);
4542 disclaim_video_lines(btv);
4543 }
4544
4545 /* add subdevices and autoload dvb-bt8xx if needed */
4546 if (bttv_tvcards[btv->c.type].has_dvb) {
4547 bttv_sub_add_device(&btv->c, "dvb");
4548 request_modules(btv);
4549 }
4550
4551 if (!disable_ir) {
4552 init_bttv_i2c_ir(btv);
4553 bttv_input_init(btv);
4554 }
4555
4556 /* everything is fine */
4557 bttv_num++;
4558 return 0;
4559
4560 fail2:
4561 free_irq(btv->c.pci->irq,btv);
4562
4563 fail1:
4564 v4l2_device_unregister(&btv->c.v4l2_dev);
4565
4566 fail0:
4567 if (btv->bt848_mmio)
4568 iounmap(btv->bt848_mmio);
4569 release_mem_region(pci_resource_start(btv->c.pci,0),
4570 pci_resource_len(btv->c.pci,0));
4571 return result;
4572 }
4573
4574 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4575 {
4576 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4577 struct bttv *btv = to_bttv(v4l2_dev);
4578
4579 if (bttv_verbose)
4580 printk("bttv%d: unloading\n",btv->c.nr);
4581
4582 if (bttv_tvcards[btv->c.type].has_dvb)
4583 flush_request_modules(btv);
4584
4585 /* shutdown everything (DMA+IRQs) */
4586 btand(~15, BT848_GPIO_DMA_CTL);
4587 btwrite(0, BT848_INT_MASK);
4588 btwrite(~0x0, BT848_INT_STAT);
4589 btwrite(0x0, BT848_GPIO_OUT_EN);
4590 if (bttv_gpio)
4591 bttv_gpio_tracking(btv,"cleanup");
4592
4593 /* tell gpio modules we are leaving ... */
4594 btv->shutdown=1;
4595 wake_up(&btv->gpioq);
4596 bttv_input_fini(btv);
4597 bttv_sub_del_devices(&btv->c);
4598
4599 /* unregister i2c_bus + input */
4600 fini_bttv_i2c(btv);
4601
4602 /* unregister video4linux */
4603 bttv_unregister_video(btv);
4604
4605 /* free allocated memory */
4606 btcx_riscmem_free(btv->c.pci,&btv->main);
4607
4608 /* free ressources */
4609 free_irq(btv->c.pci->irq,btv);
4610 iounmap(btv->bt848_mmio);
4611 release_mem_region(pci_resource_start(btv->c.pci,0),
4612 pci_resource_len(btv->c.pci,0));
4613
4614 v4l2_device_unregister(&btv->c.v4l2_dev);
4615 bttvs[btv->c.nr] = NULL;
4616 kfree(btv);
4617
4618 return;
4619 }
4620
4621 #ifdef CONFIG_PM
4622 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4623 {
4624 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4625 struct bttv *btv = to_bttv(v4l2_dev);
4626 struct bttv_buffer_set idle;
4627 unsigned long flags;
4628
4629 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4630
4631 /* stop dma + irqs */
4632 spin_lock_irqsave(&btv->s_lock,flags);
4633 memset(&idle, 0, sizeof(idle));
4634 btv->state.video = btv->curr;
4635 btv->state.vbi = btv->cvbi;
4636 btv->state.loop_irq = btv->loop_irq;
4637 btv->curr = idle;
4638 btv->loop_irq = 0;
4639 bttv_buffer_activate_video(btv, &idle);
4640 bttv_buffer_activate_vbi(btv, NULL);
4641 bttv_set_dma(btv, 0);
4642 btwrite(0, BT848_INT_MASK);
4643 spin_unlock_irqrestore(&btv->s_lock,flags);
4644
4645 /* save bt878 state */
4646 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4647 btv->state.gpio_data = gpio_read();
4648
4649 /* save pci state */
4650 pci_save_state(pci_dev);
4651 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4652 pci_disable_device(pci_dev);
4653 btv->state.disabled = 1;
4654 }
4655 return 0;
4656 }
4657
4658 static int bttv_resume(struct pci_dev *pci_dev)
4659 {
4660 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4661 struct bttv *btv = to_bttv(v4l2_dev);
4662 unsigned long flags;
4663 int err;
4664
4665 dprintk("bttv%d: resume\n", btv->c.nr);
4666
4667 /* restore pci state */
4668 if (btv->state.disabled) {
4669 err=pci_enable_device(pci_dev);
4670 if (err) {
4671 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4672 btv->c.nr);
4673 return err;
4674 }
4675 btv->state.disabled = 0;
4676 }
4677 err=pci_set_power_state(pci_dev, PCI_D0);
4678 if (err) {
4679 pci_disable_device(pci_dev);
4680 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4681 btv->c.nr);
4682 btv->state.disabled = 1;
4683 return err;
4684 }
4685
4686 pci_restore_state(pci_dev);
4687
4688 /* restore bt878 state */
4689 bttv_reinit_bt848(btv);
4690 gpio_inout(0xffffff, btv->state.gpio_enable);
4691 gpio_write(btv->state.gpio_data);
4692
4693 /* restart dma */
4694 spin_lock_irqsave(&btv->s_lock,flags);
4695 btv->curr = btv->state.video;
4696 btv->cvbi = btv->state.vbi;
4697 btv->loop_irq = btv->state.loop_irq;
4698 bttv_buffer_activate_video(btv, &btv->curr);
4699 bttv_buffer_activate_vbi(btv, btv->cvbi);
4700 bttv_set_dma(btv, 0);
4701 spin_unlock_irqrestore(&btv->s_lock,flags);
4702 return 0;
4703 }
4704 #endif
4705
4706 static struct pci_device_id bttv_pci_tbl[] = {
4707 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4708 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4709 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4710 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
4711 {0,}
4712 };
4713
4714 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4715
4716 static struct pci_driver bttv_pci_driver = {
4717 .name = "bttv",
4718 .id_table = bttv_pci_tbl,
4719 .probe = bttv_probe,
4720 .remove = __devexit_p(bttv_remove),
4721 #ifdef CONFIG_PM
4722 .suspend = bttv_suspend,
4723 .resume = bttv_resume,
4724 #endif
4725 };
4726
4727 static int __init bttv_init_module(void)
4728 {
4729 int ret;
4730
4731 bttv_num = 0;
4732
4733 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4734 (BTTV_VERSION_CODE >> 16) & 0xff,
4735 (BTTV_VERSION_CODE >> 8) & 0xff,
4736 BTTV_VERSION_CODE & 0xff);
4737 #ifdef SNAPSHOT
4738 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4739 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4740 #endif
4741 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4742 gbuffers = 2;
4743 if (gbufsize > BTTV_MAX_FBUF)
4744 gbufsize = BTTV_MAX_FBUF;
4745 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4746 if (bttv_verbose)
4747 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4748 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4749
4750 bttv_check_chipset();
4751
4752 ret = bus_register(&bttv_sub_bus_type);
4753 if (ret < 0) {
4754 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4755 return ret;
4756 }
4757 ret = pci_register_driver(&bttv_pci_driver);
4758 if (ret < 0)
4759 bus_unregister(&bttv_sub_bus_type);
4760
4761 return ret;
4762 }
4763
4764 static void __exit bttv_cleanup_module(void)
4765 {
4766 pci_unregister_driver(&bttv_pci_driver);
4767 bus_unregister(&bttv_sub_bus_type);
4768 }
4769
4770 module_init(bttv_init_module);
4771 module_exit(bttv_cleanup_module);
4772
4773 /*
4774 * Local variables:
4775 * c-basic-offset: 8
4776 * End:
4777 */
This page took 0.20558 seconds and 5 git commands to generate.