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